[
  {
    "path": ".github/ISSUE_TEMPLATE/bug_report.md",
    "content": "---\nname: Bug report\nabout: Create a report to help us improve\ntitle: ''\nlabels: bug\nassignees: ''\n\n---\n\n**Describe the bug**\nA clear and concise description of what the bug is.\n\n**To Reproduce**\nSteps to reproduce the behaviour:\n1. Go to '...'\n2. Click on '....'\n3. Scroll down to '....'\n4. See error\n\n**Expected behaviour**\nA clear and concise description of what you expected to happen.\n\n**Screenshots**\nIf applicable, add screenshots to help explain your problem.\n\n**Desktop (please complete the following information):**\n - OS: [e.g. iOS]\n - Version [e.g. 22]\n - Hardware [e.g. CPU:6700k, GPU:RTX 2080ti, RAM:1GB]\n\n**Additional context**\nAdd any other context about the problem here.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/design-change.md",
    "content": "---\nname: Design change\nabout: Suggest a design change for the project\ntitle: ''\nlabels: design change\nassignees: ''\n\n---\n\n**Is your design request related to a problem? Please describe.**\nA clear and concise description of what the problem is. Ex. I'm always frustrated when [...]\n\n**Describe the solution you'd like**\nA clear and concise description of what you want to happen.\n\n**Describe alternatives you've considered**\nA clear and concise description of any alternative solutions or designs you've considered.\n\n**Additional context**\nAdd any other context or screenshots about the request here.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature_request.md",
    "content": "---\nname: Feature request\nabout: Suggest an idea for this project\ntitle: ''\nlabels: ''\nassignees: ''\n\n---\n\n**Is your feature request related to a problem? Please describe.**\nA clear and concise description of what the problem is. Ex. I'm always frustrated when [...]\n\n**Describe the solution you'd like**\nA clear and concise description of what you want to happen.\n\n**Describe alternatives you've considered**\nA clear and concise description of any alternative solutions or features you've considered.\n\n**Additional context**\nAdd any other context or screenshots about the feature request here.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/re-factor-request.md",
    "content": "---\nname: Re-factor request\nabout: Suggest a re-factor of code for the project\ntitle: ''\nlabels: code change\nassignees: ''\n\n---\n\n**Is your request related to a problem? Please describe.**\nA clear and concise description of what the problem is. Ex. I'm always frustrated when [...]\n\n**Describe the solution you'd like**\nA clear and concise description of what you want to happen.\n\n**Describe alternatives you've considered**\nA clear and concise description of any alternative solutions you've considered.\n\n**Additional context**\nAdd any other context or screenshots about the request here.\n"
  },
  {
    "path": ".github/PULL_REQUEST_TEMPLATE/pull_request_template.md",
    "content": "**Description**\nGive a clear description of the changes\n\n**Issues**\nreference related issues if any\n\n**Possible conflicts**\nlist any possible conflicts\n"
  },
  {
    "path": ".gitignore",
    "content": "/build/\n/build_vs2017_arm/\n/build_vs2017_win64/\n/build_vs2019_win64/\n/build_vs2019_win32/\n/build_vs2017_win32/\n*.swp\n*.swo\n/cmake-build-debug/\n/.idea/\nperf_framerate.txt\n*.dll\n*.lib\n/resources/models \n/resources/materials \n/deps/ansel/\n/deps/hbao+/\n/benchmark_images/\n/CrashPadDB/\n/logs/\n"
  },
  {
    "path": ".gitmodules",
    "content": "[submodule \"deps/fmt\"]\n\tpath = deps/fmt\n\turl = https://github.com/fmtlib/fmt.git\n[submodule \"deps/DirectXTex\"]\n\tpath = deps/DirectXTex\n\turl = https://github.com/TeamWisp/DirectXTex.git\n\tbranch = master\n[submodule \"deps/assimp\"]\n\tpath = deps/assimp\n\turl = https://github.com/assimp/assimp\n[submodule \"deps/fallback\"]\n\tpath = deps/fallback\n\turl = https://github.com/TeamWisp/DXR-Fallback-Layer.git\n[submodule \"deps/Wisp-LFS\"]\n\tpath = deps/Wisp-LFS\n\turl = https://github.com/TeamWisp/Wisp-LFS.git\n[submodule \"deps/crashpad\"]\n\tpath = deps/crashpad\n\turl = https://github.com/TeamWisp/Crashpad.git\n[submodule \"deps/tinygltf\"]\n\tpath = deps/tinygltf\n\turl = https://github.com/syoyo/tinygltf\n[submodule \"deps/bullet3\"]\n\tpath = deps/bullet3\n\turl = https://github.com/TeamWisp/bullet3\n"
  },
  {
    "path": "CMakeLists.txt",
    "content": "cmake_minimum_required(VERSION 3.13)\n\nset_property(GLOBAL PROPERTY USE_FOLDERS ON)\nadd_compile_definitions(_ENABLE_EXTENDED_ALIGNED_STORAGE )\n\nproject(Wisp)\n\noption(WISP_BUILD_TESTS \"Enable Wisp Tests\" ON)\noption(WISP_LOG_TO_STDOUT \"Allow printing to stdout\" ON)\noption(WISP_BUILD_SHARED \"Build Wisp as a shared library\" OFF)\nif (WISP_BUILD_SHARED)\n\tset(BUILD_SHARED_LIBS ON)\n\tmessage(STATUS \"Building wisp as a SHARED library\")\nelse()\n\tadd_definitions(-DWISPRENDERER_STATIC_DEFINE)\n\tmessage(STATUS \"Building wisp as a STATIC library\")\n\tset(BUILD_SHARED_LIBS OFF)\nendif()\n\nif (WISP_LOG_TO_STDOUT)\n\tmessage(STATUS \"Wisp Renderer is allowed to print to stdout\")\n\tadd_definitions(-DWISPRENDERER_LOG_TO_STDOUT)\nendif()\n\n#Detect whether we have HBAO+ SDK available.\nif(EXISTS  ${CMAKE_SOURCE_DIR}/deps/hbao+)\n\tmessage(STATUS \"Found NVIDIA Gameworks HBAO+\")\n\tset(NVIDIA_GAMEWORKS_HBAO_LIB ${CMAKE_SOURCE_DIR}/GFSDK_SSAO_D3D12.win64.lib)\n\tadd_definitions(-DNVIDIA_GAMEWORKS_HBAO)\n\tconfigure_file(${CMAKE_SOURCE_DIR}/deps/hbao+/lib/GFSDK_SSAO_D3D12.win64.dll ${CMAKE_SOURCE_DIR} COPYONLY)\n\tconfigure_file(${CMAKE_SOURCE_DIR}/deps/hbao+/lib/GFSDK_SSAO_D3D12.win64.lib ${CMAKE_SOURCE_DIR} COPYONLY)\nelse()\n\tmessage(STATUS \"Failed to find NVIDIA Gameworks HBAO+\")\nendif()\n\n#Detect whether we have the AnselSDK available.\nif(EXISTS  ${CMAKE_SOURCE_DIR}/deps/ansel)\n\tmessage(STATUS \"Found NVIDIA Gameworks Ansel\")\n\tset(NVIDIA_GAMEWORKS_ANSEL_LIB ${CMAKE_SOURCE_DIR}/AnselSDK64.lib)\n\tadd_definitions(-DNVIDIA_GAMEWORKS_ANSEL)\n\tconfigure_file(${CMAKE_SOURCE_DIR}/deps/ansel/lib/AnselSDK64.lib ${CMAKE_SOURCE_DIR} COPYONLY)\n\tconfigure_file(${CMAKE_SOURCE_DIR}/deps/ansel/redist/AnselSDK64.dll ${CMAKE_SOURCE_DIR} COPYONLY)\nelse()\n\tmessage(STATUS \"Failed to find NVIDIA Gameworks Ansel\")\nendif()\n\n##### OUTPUT DIRECTORIES #####\nset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)\nset(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)\nset(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)\nconfigure_file(${CMAKE_SOURCE_DIR}/deps/fallback/Bin/dxrfallbackcompiler.dll ${CMAKE_SOURCE_DIR} COPYONLY)\nconfigure_file(${CMAKE_SOURCE_DIR}/deps/fallback/Bin/dxil.dll ${CMAKE_SOURCE_DIR} COPYONLY)\nconfigure_file(${CMAKE_SOURCE_DIR}/deps/fallback/Bin/dxcompiler.dll ${CMAKE_SOURCE_DIR} COPYONLY)\n\n##### SOURCES #####\nfile(GLOB HEADERS CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp\")\nfile(GLOB SOURCES CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp\")\nfile(GLOB FG_HEADERS CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/frame_graph/*.hpp\")\nfile(GLOB FG_SOURCES CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/frame_graph/*.cpp\")\nfile(GLOB SG_HEADERS CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/scene_graph/*.hpp\")\nfile(GLOB SG_SOURCES CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/scene_graph/*.cpp\")\nfile(GLOB RT_HEADERS CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/render_tasks/*.hpp\")\nfile(GLOB RT_SOURCES CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/render_tasks/*.cpp\")\nfile(GLOB D3D12_HEADERS CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/d3d12/*.hpp\")\nfile(GLOB D3D12_SOURCES CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/d3d12/*.cpp\")\nfile(GLOB UTIL_HEADERS CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/util/*.hpp\")\nfile(GLOB UTIL_SOURCES CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/util/*.cpp\")\nfile(GLOB IMGUI_HEADERS CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/imgui/*.hpp\")\nfile(GLOB IMGUI_SOURCES CONFIGURE_DEPENDS] \"${CMAKE_CURRENT_SOURCE_DIR}/src/imgui/*.cpp\")\n\nsource_group(\"High Level API\" FILES ${SOURCES} ${HEADERS})\nsource_group(\"Frame Graph\" FILES ${FG_SOURCES} ${FG_HEADERS})\nsource_group(\"Scene Graph\" FILES ${SG_SOURCES} ${SG_HEADERS})\nsource_group(\"Render Tasks\" FILES ${RT_SOURCES} ${RT_HEADERS})\nsource_group(\"D3D12\" FILES ${D3D12_SOURCES} ${D3D12_HEADERS})\nsource_group(\"Utility\" FILES ${UTIL_SOURCES} ${UTIL_HEADERS})\nsource_group(\"ImGui\" FILES ${IMGUI_SOURCES} ${IMGUI_HEADERS})\n\nset (CMAKE_CXX_FLAGS_RELEASE \"${CMAKE_CXX_FLAGS_RELEASE} /MT\")\nset (CMAKE_CXX_FLAGS_DEBUG \"${CMAKE_CXX_FLAGS_DEBUG} /MTd\")\nset (CMAKE_CXX_FLAGS_RELWITHDEBINFO \"${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT\")\n\n# Bullet Options\nset(BUILD_CPU_DEMOSON OFF CACHE BOOL \"\" FORCE)\nset(USE_GRAPHICAL_BENCHMARKON OFF CACHE BOOL \"\" FORCE)\nset(BUILD_ENETON OFF CACHE BOOL \"\" FORCE)\nset(BUILD_CLSOCKETON OFF CACHE BOOL \"\" FORCE)\nset(BUILD_BULLET2_DEMOSON OFF CACHE BOOL \"\" FORCE)\nset(BUILD_EXTRASON OFF CACHE BOOL \"\" FORCE)\nset(BUILD_UNIT_TESTSON OFF CACHE BOOL \"\" FORCE)\nset(USE_GRAPHICAL_BENCHMARKON OFF CACHE BOOL \"\" FORCE)\nset(BUILD_STATIC_LIBON ON CACHE BOOL \"\" FORCE)\nset(NO_EXPORTON ON CACHE BOOL \"\" FORCE)\n\n## dependencies ##\nadd_subdirectory(${CMAKE_SOURCE_DIR}/deps/fmt ${CMAKE_BINARY_DIR}/fmt)\nadd_subdirectory(${CMAKE_SOURCE_DIR}/deps/DirectXTex ${CMAKE_BINARY_DIR}/DirectXTex)\nadd_subdirectory(${CMAKE_SOURCE_DIR}/deps/fallback ${CMAKE_BINARY_DIR}/fallback)\nadd_subdirectory(${CMAKE_SOURCE_DIR}/deps/bullet3 ${CMAKE_BINARY_DIR}/bullet3)\n\nset(ASSIMP_BUILD_SHARED_LIBS OFF)\n\nset(ASSIMP_NO_EXPORT ON CACHE BOOL \"\" FORCE)\nset(ASSIMP_BUILD_TESTS OFF CACHE BOOL \"\" FORCE)\nset(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF)\nset(ASSIMP_BUILD_ALL_EXPORTERS_BY_DEFAULT OFF)\n#IMPORTERS\nset( ASSIMP_BUILD_AMF_IMPORTER      OFF )\nset( ASSIMP_BUILD_3DS_IMPORTER      OFF )\nset( ASSIMP_BUILD_AC_IMPORTER       OFF )\nset( ASSIMP_BUILD_ASE_IMPORTER      OFF )\nset( ASSIMP_BUILD_ASSBIN_IMPORTER   OFF )\nset( ASSIMP_BUILD_ASSXML_IMPORTER   OFF )\nset( ASSIMP_BUILD_B3D_IMPORTER      OFF )\nset( ASSIMP_BUILD_BVH_IMPORTER      OFF )\nset( ASSIMP_BUILD_COLLADA_IMPORTER  OFF )\nset( ASSIMP_BUILD_DXF_IMPORTER      OFF )\nset( ASSIMP_BUILD_CSM_IMPORTER      OFF )\nset( ASSIMP_BUILD_HMP_IMPORTER      OFF )\nset( ASSIMP_BUILD_IRRMESH_IMPORTER  OFF )\nset( ASSIMP_BUILD_IRR_IMPORTER      OFF )\nset( ASSIMP_BUILD_LWO_IMPORTER      OFF )\nset( ASSIMP_BUILD_LWS_IMPORTER      OFF )\nset( ASSIMP_BUILD_MD2_IMPORTER      OFF )\nset( ASSIMP_BUILD_MD3_IMPORTER      OFF )\nset( ASSIMP_BUILD_MD5_IMPORTER      OFF )\nset( ASSIMP_BUILD_MDC_IMPORTER      OFF )\nset( ASSIMP_BUILD_MDL_IMPORTER      OFF )\nset( ASSIMP_BUILD_NFF_IMPORTER      OFF )\nset( ASSIMP_BUILD_NDO_IMPORTER      OFF )\nset( ASSIMP_BUILD_OFF_IMPORTER      OFF )\nset( ASSIMMP_BUILD_OBJ_IMPORTER     ON  ) #ON\nset( ASSIMP_BUILD_OGRE_IMPORTER     OFF )\nset( ASSIMP_BUILD_OPENGEX_IMPORTER  OFF )\nset( ASSIMP_BUILD_PLY_IMPORTER      OFF )\nset( ASSIMP_BUILD_MS3D_IMPORTER     OFF )\nset( ASSIMP_BUILD_COB_IMPORTER      OFF )\nset( ASSIMP_BUILD_BLEND_IMPORTER    OFF )\nset( ASSIMP_BUILD_IFC_IMPORTER      OFF )\nset( ASSIMP_BUILD_XGL_IMPORTER      OFF )\nset( ASSIMMP_BUILD_FBX_IMPORTER     ON  ) #ON\nset( ASSIMP_BUILD_Q3D_IMPORTER      OFF )\nset( ASSIMP_BUILD_Q3BSP_IMPORTER    OFF )\nset( ASSIMP_BUILD_RAW_IMPORTER      ON  ) #ON\nset( ASSIMP_BUILD_SIB_IMPORTER      ON  ) #ON\nset( ASSIMP_BUILD_SMD_IMPORTER      ON  ) #ON\nset( ASSIMP_BUILD_STL_IMPORTER      OFF )\nset( ASSIMP_BUILD_TERRAGEN_IMPORTER OFF )\nset( ASSIMP_BUILD_3D_IMPORTER       OFF )\nset( ASSIMP_BUILD_X_IMPORTER        OFF )\nset( ASSIMP_BUILD_X3D_IMPORTER      OFF )\nset( ASSIMP_BUILD_GLTF_IMPORTER     ON  ) #ON\nset( ASSIMP_BUILD_3MF_IMPORTER      OFF )\nset( ASSIMP_BUILD_MMD_IMPORTER      OFF )\n# END IMPORTERS\n\n#set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT CACHE INTERNAL FALSE)\n#set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF)\nadd_subdirectory(${CMAKE_SOURCE_DIR}/deps/assimp ${CMAKE_BINARY_DIR}/assimp)\n\n## dependencies options ##\nset(FMT_TEST OFF)\nset(FMT_DOC OFF)\nset(FMT_PEDANTIC OFF)\nset(FMT_WERROR OFF)\n## dependencies sorting ##\nset_target_properties (fmt PROPERTIES FOLDER ThirdParty)\nset_target_properties (DirectXTex PROPERTIES FOLDER ThirdParty)\n\nif (MSVC)\n\ttarget_compile_options(fmt PRIVATE /W0)\n\ttarget_compile_options(DirectXTex PRIVATE /W0)\nendif()\n\nset_target_properties(Bullet3Common PROPERTIES FOLDER \"ThirdParty/Bullet\")\nset_target_properties(BulletCollision PROPERTIES FOLDER \"ThirdParty/Bullet\")\nset_target_properties(BulletDynamics PROPERTIES FOLDER \"ThirdParty/Bullet\")\nset_target_properties(BulletInverseDynamics PROPERTIES FOLDER \"ThirdParty/Bullet\")\nset_target_properties(BulletSoftBody PROPERTIES FOLDER \"ThirdParty/Bullet\")\nset_target_properties(LinearMath PROPERTIES FOLDER \"ThirdParty/Bullet\")\nset_target_properties(assimp PROPERTIES FOLDER ThirdParty)\nset_target_properties(assimp_cmd PROPERTIES FOLDER ThirdParty)\nset_target_properties(IrrXML PROPERTIES FOLDER ThirdParty)\nset_target_properties(UpdateAssimpLibsDebugSymbolsAndDLLs PROPERTIES FOLDER ThirdParty)\nset_target_properties(zlib PROPERTIES FOLDER ThirdParty)\nset_target_properties(zlibstatic PROPERTIES FOLDER ThirdParty)\n#set_target_properties(unit PROPERTIES FOLDER ThirdParty)\nset_target_properties(uninstall PROPERTIES FOLDER ThirdParty)\n\nif(WISP_BUILD_SHARED)\n\tset(WISP_LIB_TYPE SHARED)\nelse()\n\tset(WISP_LIB_TYPE STATIC)\nendif()\n\nadd_library(WispRenderer ${WISP_LIB_TYPE} ${HEADERS} ${SOURCES} ${IMGUI_HEADERS} ${IMGUI_SOURCES} ${UTIL_HEADERS} ${UTIL_SOURCES} ${RT_HEADERS} ${RT_SOURCES} ${FG_HEADERS} ${FG_SOURCES} ${SG_HEADERS} ${SG_SOURCES} ${D3D12_SOURCES} ${D3D12_HEADERS})\nset_target_properties(WispRenderer PROPERTIES CXX_STANDARD 20)\nset_target_properties(WispRenderer PROPERTIES CXX_EXTENSIONS OFF)\nset_target_properties(WispRenderer PROPERTIES CMAKE_CXX_STANDARD_REQUIRED ON)\nif (MSVC)\ntarget_compile_options(WispRenderer PRIVATE /W4 /permissive- /MP /Gm-)\nendif()\n\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/DirectXTex/DirectXTex/)\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/fmt/include)\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/tinygltf)\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/assimp/include)\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/fallback/Include)\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/hbao+/include)\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/ansel/include)\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/crashpad)\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/crashpad/third_party/mini_chromium/mini_chromium)\ntarget_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/bullet3/src)\n\nlink_directories(${CMAKE_SOURCE_DIR}/deps/crashpad/out/$(ConfigurationName)/obj/ ${CMAKE_SOURCE_DIR}/deps/crashpad/out/$(ConfigurationName)/obj/client/ ${CMAKE_SOURCE_DIR}/deps/crashpad/out/$(ConfigurationName)/obj/util/ ${CMAKE_SOURCE_DIR}/deps/crashpad/out/$(ConfigurationName)/obj/third_party/mini_chromium/mini_chromium/base/)\n\ntarget_link_libraries(WispRenderer DXRFallback dxguid.lib d3d12.lib dxgi.lib d3dcompiler.lib dxcompiler DirectXTex fmt assimp ${NVIDIA_GAMEWORKS_HBAO_LIB} ${NVIDIA_GAMEWORKS_ANSEL_LIB}  crashpad_client crashpad_util base)\n\nset_target_properties(WispRenderer PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY \"${CMAKE_BINARY_DIR}/../\")\n\nif (WISP_BUILD_TESTS)\n\tadd_subdirectory(tests ${CMAKE_BINARY_DIR}/tests)\n\tset_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT Demo)\nendif()\n"
  },
  {
    "path": "CODE_OF_CONDUCT.md",
    "content": "# Contributor Covenant Code of Conduct\n\n## Our Pledge\n\nIn the interest of fostering an open and welcoming environment, we as\ncontributors and maintainers pledge to making participation in our project and\nour community a harassment-free experience for everyone, regardless of age, body\nsize, disability, ethnicity, sex characteristics, gender identity and expression,\nlevel of experience, education, socio-economic status, nationality, personal\nappearance, race, religion, or sexual identity and orientation.\n\n## Our Standards\n\nExamples of behavior that contributes to creating a positive environment\ninclude:\n\n* Using welcoming and inclusive language\n* Being respectful of differing viewpoints and experiences\n* Gracefully accepting constructive criticism\n* Focusing on what is best for the community\n* Showing empathy towards other community members\n\nExamples of unacceptable behavior by participants include:\n\n* The use of sexualized language or imagery and unwelcome sexual attention or\n advances\n* Trolling, insulting/derogatory comments, and personal or political attacks\n* Public or private harassment\n* Publishing others' private information, such as a physical or electronic\n address, without explicit permission\n* Other conduct which could reasonably be considered inappropriate in a\n professional setting\n\n## Our Responsibilities\n\nProject maintainers are responsible for clarifying the standards of acceptable\nbehavior and are expected to take appropriate and fair corrective action in\nresponse to any instances of unacceptable behavior.\n\nProject maintainers have the right and responsibility to remove, edit, or\nreject comments, commits, code, wiki edits, issues, and other contributions\nthat are not aligned to this Code of Conduct, or to ban temporarily or\npermanently any contributor for other behaviors that they deem inappropriate,\nthreatening, offensive, or harmful.\n\n## Scope\n\nThis Code of Conduct applies both within project spaces and in public spaces\nwhen an individual is representing the project or its community. Examples of\nrepresenting a project or community include using an official project e-mail\naddress, posting via an official social media account, or acting as an appointed\nrepresentative at an online or offline event. Representation of a project may be\nfurther defined and clarified by project maintainers.\n\n## Enforcement\n\nInstances of abusive, harassing, or otherwise unacceptable behavior may be\nreported by contacting the project team at viktorzoutman@vzout.com. All\ncomplaints will be reviewed and investigated and will result in a response that\nis deemed necessary and appropriate to the circumstances. The project team is\nobligated to maintain confidentiality with regard to the reporter of an incident.\nFurther details of specific enforcement policies may be posted separately.\n\nProject maintainers who do not follow or enforce the Code of Conduct in good\nfaith may face temporary or permanent repercussions as determined by other\nmembers of the project's leadership.\n\n## Attribution\n\nThis Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,\navailable at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html\n\n[homepage]: https://www.contributor-covenant.org\n\nFor answers to common questions about this code of conduct, see\nhttps://www.contributor-covenant.org/faq\n"
  },
  {
    "path": "Jenkinsfile",
    "content": "pipeline {\n    agent any\n    stages {\n\t\tstage('Install'){\n\t\t\tsteps{\n\t\t\t\techo \"Running ${env.BUILD_ID} on ${env.JENKINS_URL}\"\n\t\t\t\tbat '''cd \"%WORKSPACE%\\\\Scripts\"\n\t\t\t\tcall JenkinsWebhook.bat \":bulb: Building: %JOB_NAME%. Jenkins build nr: %BUILD_NUMBER%\"\n\t\t\t\tcd \"%WORKSPACE%\n\t\t\t\tinstall -remote \"%WORKSPACE%\" \n\t\t\t\tif errorlevel 1 (\n\t\t\t\t\tcd \"%WORKSPACE%\\\\Scripts\" \n\t\t\t\t\tJenkinsWebhook \":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - install failed\"\n\t\t\t\t\tEXIT 1\n\t\t\t\t)'''\n\t\t\t}\n\t\t}\n        stage('Build') {\n            steps {\n\t\t\t\t/*bat '''\n\t\t\t\tcd \"%WORKSPACE%\"\n\t\t\t\tcmake --build ./build_vs2017_win32 \n\t\t\t\tif errorlevel 1 (\n\t\t\t\t\tcd \"%WORKSPACE%\\\\Scripts\" \n\t\t\t\t\tJenkinsWebhook \":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 32bit build failed\"\n\t\t\t\t\tEXIT 1\n\t\t\t\t)\n\t\t\t\t'''*/\n\n\t\t\t\tbat'''\n\t\t\t\tcd \"%WORKSPACE%\"\n\t\t\t\tcmake --build ./build_vs2017_win64 \n\t\t\t\tif errorlevel 1 (\n\t\t\t\t\tcd \"%WORKSPACE%\\\\Scripts\" \n\t\t\t\t\tJenkinsWebhook \":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 64bit-debug build failed\"\n\t\t\t\t\tEXIT 1\n\t\t\t\t)\n\t\t\t\t'''\n\n\t\t\t\tbat'''\n\t\t\t\tcd \"%WORKSPACE%\"\n\t\t\t\tcmake --build ./build_vs2017_win64 --config Release\n\t\t\t\tif errorlevel 1 (\n\t\t\t\t\tcd \"%WORKSPACE%\\\\Scripts\" \n\t\t\t\t\tJenkinsWebhook \":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 64bit-release build failed\"\n\t\t\t\t\tEXIT 1\n\t\t\t\t)\n\t\t\t\t'''\n            }\n        }\n\t\tstage('test'){\n\t\t\tsteps{\t\n\t\t\t\tscript{\n\t\t\t\t\tdef has_failed = false\n\t\t\t\t\ttry{\n\t\t\t\t\t\tbat'''\n\t\t\t\t\t\tcd \"%WORKSPACE%\"\n\t\t\t\t\t\tcd build_vs2017_win64/bin/debug\n\t\t\t\t\t\tWispTest.exe\n\t\t\t\t\t\tif errorlevel 1 (\n\t\t\t\t\t\t\tEXIT 1\n\t\t\t\t\t\t)\n\t\t\t\t\t\t'''\n\t\t\t\t\t}\n\t\t\t\t\tcatch( exc ){\n\t\t\t\t\t\thas_failed = true\n\t\t\t\t\t\tbat'''\n\t\t\t\t\t\tcd \"%WORKSPACE%\\\\Scripts\" \n\t\t\t\t\t\t\tJenkinsWebhook \":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 64bit-debug Tests failed\"\n\t\t\t\t\t\t'''\n\t\t\t\t\t}\n\t\t\t\t\ttry{\n\t\t\t\t\t\tbat'''\n\t\t\t\t\t\tcd \"%WORKSPACE%\"\n\t\t\t\t\t\tcd build_vs2017_win64/bin/release\n\t\t\t\t\t\tWispTest.exe\n\t\t\t\t\t\tif errorlevel 1 (\n\t\t\t\t\t\t\tEXIT 1\n\t\t\t\t\t\t)\n\t\t\t\t\t\t'''\n\t\t\t\t\t}\n\t\t\t\t\tcatch( exc ){\n\t\t\t\t\t\thas_failed = true\n\t\t\t\t\t\tbat'''\n\t\t\t\t\t\tcd \"%WORKSPACE%\\\\Scripts\" \n\t\t\t\t\t\t\tJenkinsWebhook \":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 64bit-release Tests failed\"\n\t\t\t\t\t\t'''\n\t\t\t\t\t}\n\t\t\t\t\tif(has_failed){\n\t\t\t\t\t\tEXIT 1\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tstage('finalize'){\n\t\t\tsteps{\n\t\t\t\tbat '''\n\t\t\t\trem mkdir builds\n\t\t\t\trem move ./RayTracingLib/Debug \"./builds/build_%BUILD_NUMBER%\"\n\t\t\t\tcd \"%WORKSPACE%\\\\scripts\n\t\t\t\tcall \"JenkinsWebhook.bat\" \":white_check_mark: %JOB_NAME% Build Succesfull!! Jenkins build nr: %BUILD_NUMBER%\"\n\t\t\t\t'''\n\t\t\t}\n\t\t}\n    }\n}\n"
  },
  {
    "path": "LICENSE",
    "content": "                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"[]\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n"
  },
  {
    "path": "README.md",
    "content": "# [<img src=\"http://upload.vzout.com/wisp/logo.png\" width=\"40\"> WispRenderer](https://teamwisp.github.io) - Real-Time Raytracing Renderer\n\n[![Release](https://img.shields.io/github/release/TeamWisp/WispRenderer.svg)](https://github.com/TeamWisp/WispRenderer/releases)\n[![Issues](https://img.shields.io/github/issues/TeamWisp/WispRenderer.svg)](https://github.com/TeamWisp/WispRenderer/issues)\n[![License](https://img.shields.io/badge/license-EPL%202.0-red.svg)](https://opensource.org/licenses/EPL-2.0)\n[![Discord](https://img.shields.io/discord/486967125504688128.svg?color=blueviolet&label=Discord)](https://discord.gg/Q3vDfqR)\n[![Twitter](https://img.shields.io/twitter/follow/wisprenderer.svg?style=social)](https://twitter.com/wisprenderer)\n![](https://img.shields.io/github/stars/TeamWisp/WispRenderer.svg?style=social)\n\n## [What is it?](https://teamwisp.github.io/product/)\n\nWisp is a general purpose high level rendering library. Specially made for real time raytracing with NVIDIA RTX graphics cards. Made by a group of students from [Breda University of Applied Sciences](https://www.buas.nl/) specializing in graphics programming.\n\n**Features**\n\n* Physically Based Rendering\n* Ray Traced Global Illumination\n* Path Traced Global Illumination\n* Ray Traced Ambient Occlusion\n* Ray Traced Reflections\n* Ray Traced Shadows\n* Translucency & Transparency.\n* NVIDIA's HBAO+\n* NVIDIA's AnselSDK\n\n**Supported Rendering Backends**\n\n* DirectX 12\n\n**Supported Platforms**\n\n* Windows 10 (Version 1903)\n\n**Supported Compilers**\n\n* Visual Studio 2017\n* Visual Studio 2019\n\n\n## [Installation](https://teamwisp.github.io/workspace_setup/)\n\n### Installer\n\n```\ngit clone https://github.com/TeamWisp/WispRenderer.git\n```\n\nThen run `installer.exe` located in the new `WispRenderer` folder and follow the on-screen prompts. When finished you can find the `.sln` file in the `build_vs2019_win64` folder.\n\n\n### Manual\n\n```\ngit clone https://github.com/TeamWisp/WispRenderer.git\ncd WispRenderer\nmkdir build\ncd build\ncmake -G \"Visual Studio 16 2019\" ..\n```\n\nNow you can run the `.sln` file inside the `build` folder. Want to use NVIDIA Gameworks? See the [advanced documation](https://teamwisp.github.io/workspace_setup/).\n\n# [Documentation](https://teamwisp.github.io/)\n\n# [Example](https://github.com/TeamWisp/WispRenderer/tree/master/demo)\n\n# [Getting Involved](https://teamwisp.github.io/)\n\nWant to help us out? That's definatly possible! Check out our [contribution page](https://teamwisp.github.io/) on how.\n\n# [Development Blog](https://teamwisp.github.io/WispBlog/)\n\n# [Discord](https://discord.gg/Q3vDfqR)\n\nNeed help, want to get updates as soon as they happen or just want a chat? Join our [Discord Server](https://discord.gg/Q3vDfqR)!\n\n## Trailer\n\n<a href=\"http://www.youtube.com/watch?feature=player_embedded&v=cOxVArIFmEg\" \ntarget=\"_blank\"><img src=\"http://img.youtube.com/vi/cOxVArIFmEg/0.jpg\" border=\"0\" /></a>\n\n## Media\n\n<img src=\"http://upload.vzout.com/wisp/sponza.png\" width=\"300\"> <img src=\"http://upload.vzout.com/wisp/sun_temple.jpg\" width=\"300\">\n\n## [License (Eclipse Public License version 2.0)](https://opensource.org/licenses/EPL-2.0)\n\n<a href=\"https://opensource.org/licenses/EPL-2.0\" target=\"_blank\">\n<img align=\"right\" src=\"http://opensource.org/trademarks/opensource/OSI-Approved-License-100x137.png\">\n</a>\n\n```\nCopyright 2018-2019 Breda University of Applied Sciences\n\nIf a Contributor Distributes the Program in any form, then:\n   a) the Program must also be made available as Source Code, in accordance with section 3.2, and the Contributor must accompany the Program with a statement that the Source Code for the Program is available under this Agreement, and informs Recipients how to obtain it in a reasonable manner on or through a medium customarily used for software exchange; and\n   b) the Contributor may Distribute the Program under a license different than this Agreement, provided that such license:\n      i) effectively disclaims on behalf of all other Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose;\n      ii) effectively excludes on behalf of all other Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits;\n      iii) does not attempt to limit or alter the recipients' rights in the Source Code under section 3.2; and\n      iv) requires any subsequent distribution of the Program by any party to be under a license that satisfies the requirements of this section 3.\n\nWhen the Program is Distributed as Source Code:\n   a) it must be made available under this Agreement, or if the Program (i) is combined with other material in a separate file or files made available under a Secondary License, and (ii) the initial Contributor attached to the Source Code the notice described in Exhibit A of this Agreement, then the Program may be made available under the terms of such Secondary Licenses, and\n   b) a copy of this Agreement must be included with each copy of the Program.\n\nContributors may not remove or alter any copyright, patent, trademark, attribution notices, disclaimers of warranty, or limitations of liability (‘notices’) contained within the Program from any copy of the Program which they Distribute, provided that Contributors may add their own appropriate notices.\n```\n"
  },
  {
    "path": "imgui.ini",
    "content": "[Window][DockspaceViewport_11111111]\r\nPos=0,21\r\nSize=1280,699\r\nCollapsed=0\r\n\r\n[Window][Debug##Default]\r\nPos=1022,401\r\nSize=228,150\r\nCollapsed=1\r\n\r\n[Window][Theme]\r\nPos=1032,376\r\nSize=248,201\r\nCollapsed=0\r\nDockId=0x00000009,0\r\n\r\n[Window][ImGui Details]\r\nPos=1032,607\r\nSize=248,113\r\nCollapsed=0\r\n\r\n[Window][Camera Settings]\r\nPos=0,342\r\nSize=270,378\r\nCollapsed=0\r\nDockId=0x00000008,0\r\n\r\n[Window][Light Editor]\r\nPos=0,21\r\nSize=32,32\r\nCollapsed=0\r\n\r\n[Window][Shader Registry]\r\nPos=340,105\r\nSize=590,455\r\nCollapsed=0\r\n\r\n[Window][Pipeline Registry]\r\nPos=317,134\r\nSize=312,443\r\nCollapsed=0\r\n\r\n[Window][Root Signature Registry]\r\nPos=748,338\r\nSize=169,259\r\nCollapsed=0\r\n\r\n[Window][DirectX 12 Settings]\r\nPos=1032,376\r\nSize=248,201\r\nCollapsed=0\r\nDockId=0x00000009,2\r\n\r\n[Window][Hardware Info]\r\nPos=1032,376\r\nSize=248,201\r\nCollapsed=0\r\nDockId=0x00000009,1\r\n\r\n[Window][Model Editor]\r\nPos=0,31\r\nSize=32,32\r\nCollapsed=0\r\n\r\n[Window][Light Details]\r\nPos=0,403\r\nSize=220,129\r\nCollapsed=0\r\n\r\n[Window][Inspect]\r\nPos=26,21\r\nSize=32,32\r\nCollapsed=0\r\n\r\n[Window][Viewport]\r\nPos=272,21\r\nSize=758,699\r\nCollapsed=0\r\nDockId=0x00000002,0\r\n\r\n[Window][Scene Graph Editor]\r\nPos=0,21\r\nSize=270,319\r\nCollapsed=0\r\nDockId=0x00000007,1\r\n\r\n[Window][Inspector]\r\nPos=1032,21\r\nSize=248,353\r\nCollapsed=0\r\nDockId=0x00000006,0\r\n\r\n[Window][DockSpaceViewport_11111111]\r\nPos=0,21\r\nSize=1280,699\r\nCollapsed=0\r\n\r\n[Window][Stats]\r\nPos=280,121\r\nSize=205,85\r\nCollapsed=0\r\n\r\n[Window][Test popup]\r\nViewportPos=1503,395\r\nViewportId=0xB4FE7E97\r\nSize=151,177\r\nCollapsed=0\r\n\r\n[Window][Emissive Multiplier]\r\nPos=944,589\r\nSize=303,134\r\nCollapsed=0\r\n\r\n[Window][RTAO Settings]\r\nPos=1032,579\r\nSize=248,141\r\nCollapsed=0\r\nDockId=0x0000000A,0\r\n\r\n[Window][HBAO+ Settings]\r\nPos=1032,579\r\nSize=248,141\r\nCollapsed=0\r\nDockId=0x0000000A,0\r\n\r\n[Window][NVIDIA Ansel Settings]\r\nPos=1032,376\r\nSize=248,201\r\nCollapsed=0\r\nDockId=0x00000009,3\r\n\r\n[Window][Acceleration Structure Settings]\r\nPos=1032,579\r\nSize=248,141\r\nCollapsed=0\r\nDockId=0x0000000A,1\r\n\r\n[Window][Shadow Settings]\r\nPos=1032,579\r\nSize=248,141\r\nCollapsed=0\r\nDockId=0x0000000A,2\r\n\r\n[Window][Demo Scene]\r\nPos=0,21\r\nSize=270,319\r\nCollapsed=0\r\nDockId=0x00000007,0\r\n\r\n[Docking][Data]\r\nDockSpace       ID=0x8B93E3BD Pos=0,21 Size=1280,699 Split=X\r\n  DockNode      ID=0x00000003 Parent=0x8B93E3BD SizeRef=1030,699 Split=X\r\n    DockNode    ID=0x00000001 Parent=0x00000003 SizeRef=270,699 Split=Y SelectedTab=0xF4865B00\r\n      DockNode  ID=0x00000007 Parent=0x00000001 SizeRef=219,319 SelectedTab=0xF4865B00\r\n      DockNode  ID=0x00000008 Parent=0x00000001 SizeRef=219,378 SelectedTab=0x8722C6A1\r\n    DockNode    ID=0x00000002 Parent=0x00000003 SizeRef=758,699 CentralNode=1 SelectedTab=0x995B0CF8\r\n  DockNode      ID=0x00000004 Parent=0x8B93E3BD SizeRef=248,699 Split=Y SelectedTab=0x070A839D\r\n    DockNode    ID=0x00000006 Parent=0x00000004 SizeRef=248,353 SelectedTab=0xF02CD328\r\n    DockNode    ID=0x00000005 Parent=0x00000004 SizeRef=248,344 Split=Y SelectedTab=0xDA48A090\r\n      DockNode  ID=0x00000009 Parent=0x00000005 SizeRef=248,201 SelectedTab=0x4BB5AED3\r\n      DockNode  ID=0x0000000A Parent=0x00000005 SizeRef=248,141 SelectedTab=0xDCA1C528\r\n\r\n"
  },
  {
    "path": "resources/alien_lights.json",
    "content": "{\r\n    \"lights\": [\r\n        {\r\n            \"angle\": 69.0,\r\n            \"color\": [\r\n                0.0,\r\n                7.843137264251709,\r\n                11.764705657958984\r\n            ],\r\n            \"pos\": [\r\n                0.5994455218315125,\r\n                1.0072081089019775,\r\n                0.7158395648002625,\r\n                0.0\r\n            ],\r\n            \"radius\": 200.0,\r\n            \"rot\": [\r\n                -0.1899939924478531,\r\n                0.02998405694961548,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"size\": 0.0872664600610733,\r\n            \"type\": 0\r\n        },\r\n        {\r\n            \"angle\": 0.6981316804885864,\r\n            \"color\": [\r\n                1.0,\r\n                0.9999899864196777,\r\n                0.9999899864196777\r\n            ],\r\n            \"pos\": [\r\n                0.8884486556053162,\r\n                1.0663642883300781,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"radius\": 0.0,\r\n            \"rot\": [\r\n                0.0,\r\n                0.0,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"size\": 0.0,\r\n            \"type\": 0\r\n        }\r\n    ]\r\n}\r\n"
  },
  {
    "path": "resources/shaders/deferred_composition_pass.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DEFERRED_COMPOSITION_PASS_HLSL__\n#define __DEFERRED_COMPOSITION_PASS_HLSL__\n\n#define LIGHTS_REGISTER register(t4)\n#define MAX_REFLECTION_LOD 6\n\n#include \"fullscreen_quad.hlsl\"\n#include \"rand_util.hlsl\"\n#include \"pbr_util.hlsl\"\n#include \"lighting.hlsl\"\n\nTexture2D gbuffer_albedo_roughness : register(t0);\nTexture2D gbuffer_normal_metallic : register(t1);\nTexture2D gbuffer_emissive_ao : register(t2);\nTexture2D gbuffer_depth : register(t3);\n//Consider SRV for light buffer in register t4\nTextureCube skybox : register(t5);\nTextureCube irradiance_map   : register(t6);\nTextureCube pref_env_map\t : register(t7);\nTexture2D brdf_lut\t\t\t : register(t8);\nTexture2D buffer_reflection : register(t9);\t\t//rgb: reflection, a : 1 / pdf\nTexture2D buffer_shadow : register(t10);\t\t//r: shadow factor\nTexture2D screen_space_irradiance : register(t11);\nTexture2D screen_space_ao : register(t12);\nRWTexture2D<float4> output   : register(u0);\nSamplerState point_sampler   : register(s0);\nSamplerState linear_sampler  : register(s1);\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat4x4 view;\n\n\tfloat4x4 projection;\n\n\tfloat4x4 inv_projection;\n\n\tfloat4x4 inv_view;\n\n\tfloat4x4 prev_projection;\n\n\tfloat4x4 prev_view;\n\n\tuint is_hybrid;\n\tuint is_path_tracer;\n\tuint is_ao;\n\tuint has_shadows;\n\n\tfloat3 padding1;\n\tuint has_reflections;\n};\n\nstatic uint min_depth = 0xFFFFFFFF;\nstatic uint max_depth = 0x0;\n\nfloat3 unpack_position(float2 uv, float depth, float4x4 proj_inv, float4x4 view_inv) {\n\tconst float4 ndc = float4(uv * 2.0f - 1.f, depth, 1.0f);\n\tconst float4 pos = mul( view_inv, mul(proj_inv, ndc));\n\treturn (pos / pos.w).xyz;\n}\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\t// added offset of 0.5f to select proper pixel to sample from.\n\t// screen coords always get floored, therefore if the coords would be (1.9, 1.0),\n\t// it would sample (1.0, 1.0) instead of the intended (2.0, 1.0). Adding the offset solves this problem.\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f;\n\n\tfloat2 uv = screen_coord / screen_size;\n\n\tconst float depth_f = gbuffer_depth.SampleLevel(point_sampler, uv, 0.0f).r;\n\n\t// View position and camera position\n\tfloat3 pos = unpack_position(float2(uv.x, 1.f - uv.y), depth_f, inv_projection, inv_view);\n\tfloat3 camera_pos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]);\n\tfloat3 V = normalize(camera_pos - pos);\n\t\n\tfloat3 retval;\n\t\n\tif(depth_f != 1.0f)\n\t{\n\t\t// GBuffer contents\n\t\tfloat4 albedo_roughness = gbuffer_albedo_roughness.SampleLevel(point_sampler, uv, 0.0f);\n\t\tfloat3 albedo = albedo_roughness.xyz;\n\t\tconst float roughness = albedo_roughness.w;\n\n\t\tfloat4 normal_metallic = gbuffer_normal_metallic.SampleLevel(point_sampler, uv, 0.0f);\n\t\tfloat3 normal = normalize(normal_metallic.xyz);\n\t\tconst float metallic = normal_metallic.w;\n\n\t\tfloat4 emissive_ao = gbuffer_emissive_ao.SampleLevel(point_sampler, uv, 0.0f);\n\t\tfloat3 emissive = emissive_ao.xyz;\n\t\tfloat gbuffer_ao = emissive_ao.w;\n\n\t\tfloat3 flipped_N = normal;\n\t\tflipped_N.y *= -1.0f;\n\t\t\n\t\tconst float2 sampled_brdf = brdf_lut.SampleLevel(point_sampler, float2(max(dot(normal, V), 0.01f), roughness), 0).rg;\n\t\tfloat3 sampled_environment_map = pref_env_map.SampleLevel(linear_sampler, reflect(-V, normal), roughness * MAX_REFLECTION_LOD);\n\t\t\n\t\t// Get irradiance\n\t\tfloat3 irradiance = lerp(\n\t\t\tirradiance_map.SampleLevel(linear_sampler, flipped_N, 0.0f).xyz,\n\t\t\tscreen_space_irradiance.SampleLevel(point_sampler, uv, 0.0f).xyz,\n\t\t\tis_path_tracer);\n\n\t\t// Get ao\n\t\tfloat ao = lerp(\n\t\t\t1,\n\t\t\tscreen_space_ao.SampleLevel(point_sampler, uv, 0.0f).xyz,\n\t\t\t// Lerp factor (0: env map, 1: path traced)\n\t\t\tis_ao);\n\n\t\t//Ao is multiplied with material texture ao, if present\n\t\tao *= gbuffer_ao;\n\n\t\t// Get shadow factor (0: fully shadowed, 1: no shadow)\n\t\tfloat3 shadow_factor = lerp(\n\t\t\t// Do deferred shadow (fully lit for now)\n\t\t\tfloat3(1, 1, 1),\n\t\t\t// Shadow buffer if its hybrid rendering\n\t\t\tbuffer_shadow.SampleLevel(linear_sampler, uv, 0.0f).rgb,\n\t\t\t// Lerp factor (0: no hybrid, 1: hybrid)\n\t\t\thas_shadows);\n\t\t\n\t\t// Get reflection\n\t\tfloat3 reflection = lerp(\n\t\t\t// Sample from environment if it IS NOT hybrid rendering\n\t\t\tsampled_environment_map,\n\t\t\t// Reflection buffer if it IS hybrid rendering\n\t\t\tbuffer_reflection.SampleLevel(linear_sampler, uv, 0).xyz,\n\t\t\t// Lerp factor (0: no hybrid, 1: hybrid)\n\t\t\thas_reflections);\n\n\t\t// Shade pixel\n\t\tretval = shade_pixel(pos, V, albedo, metallic, roughness, emissive, normal, irradiance, ao, reflection, sampled_brdf, shadow_factor, has_shadows);\n\t}\n\telse\n\t{\t\n\t\tretval = skybox.SampleLevel(linear_sampler, -V, 0.0f);\n\t}\n\n\t//Temporary hackfix for NaN pixels\n\tif (isnan(retval).x == true)\n\t{\n\t\tretval = float3(0.0f, 0.0f, 0.0f);\n\t}\n\n\t//Do shading\n\toutput[int2(dispatch_thread_id.xy)] = float4(retval, 1.f);\n}\n\n\n#endif //__DEFERRED_COMPOSITION_PASS_HLSL__\n"
  },
  {
    "path": "resources/shaders/deferred_geometry_pass.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DEFERRED_GEOMETRY_PASS_HLSL__\n#define __DEFERRED_GEOMETRY_PASS_HLSL__\n\n//48 KiB; 48 * 1024 / sizeof(MeshNode)\n//48 * 1024 / (4 * 4 * 4) = 48 * 1024 / 64 = 48 * 16 = 768\n#define MAX_INSTANCES 768\n\n#include \"material_util.hlsl\"\n\nstruct VS_INPUT\n{\n\tfloat3 pos : POSITION;\n\tfloat2 uv : TEXCOORD;\n\tfloat3 normal : NORMAL;\n\tfloat3 tangent : TANGENT;\n\tfloat3 bitangent : BITANGENT;\n};\n\nstruct VS_OUTPUT\n{\n\tfloat4 pos : SV_POSITION;\n\t#ifdef IS_HYBRID\n\tfloat4 prev_pos : PREV_POSITION;\n\tfloat4 curr_pos : CURR_POSITION;\n\tfloat4 world_pos : WORLD_POSITION;\n\t#endif\n\tfloat2 uv : TEXCOORD;\n\tfloat3 normal : NORMAL;\n\tfloat3 tangent : TANGENT;\n\tfloat3 bitangent : BITANGENT;\n\t#ifdef IS_HYBRID\n\tfloat3 obj_normal : OBJECT_NORMAL;\n\tfloat3 obj_tangent : OBJECT_TANGENT;\n\tfloat3 obj_bitangent : OBJECT_BITANGENT;\n\t#endif\n};\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat4x4 view;\n\n\tfloat4x4 projection;\n\n\tfloat4x4 inv_projection;\n\n\tfloat4x4 inv_view;\n\n\tfloat4x4 prev_projection;\n\n\tfloat4x4 prev_view;\n\n\tuint is_hybrid;\n\tuint is_path_tracer;\n\tuint is_ao;\n\tuint has_shadows;\n\n\tfloat3 padding1;\n\tuint has_reflections;\n};\n\nstruct ObjectData\n{\n\tfloat4x4 model;\n\tfloat4x4 prev_model;\n};\n\ncbuffer ObjectProperties : register(b1)\n{\n\tObjectData instances[MAX_INSTANCES];\n};\n\nVS_OUTPUT main_vs(VS_INPUT input, uint instid : SV_InstanceId)\n{\n\tVS_OUTPUT output;\n\n\tfloat3 pos = input.pos;\n\n\tObjectData inst = instances[instid];\n\n\t//TODO: Use precalculated MVP or at least VP\n\tfloat4x4 vm = mul(view, inst.model);\n\tfloat4x4 mvp = mul(projection, vm);\n\n\tfloat4x4 prev_mvp = mul(prev_projection, mul(prev_view, inst.prev_model));\n\t\n\toutput.pos =  mul(mvp, float4(pos, 1.0f));\n\t#ifdef IS_HYBRID\n\toutput.curr_pos = output.pos;\n\toutput.prev_pos = mul(prev_mvp, float4(pos, 1.0f));\n\toutput.world_pos = mul(inst.model, float4(pos, 1.0f));\n\t#endif\n\toutput.uv = float2(input.uv.x, 1.0f - input.uv.y);\n\toutput.tangent = normalize(mul(inst.model, float4(input.tangent, 0))).xyz;\n\toutput.bitangent = normalize(mul(inst.model, float4(input.bitangent, 0))).xyz;\n\toutput.normal = normalize(mul(inst.model, float4(input.normal, 0))).xyz;\n\t#ifdef IS_HYBRID\n\toutput.obj_normal = input.normal.xyz;\n\toutput.obj_tangent = input.tangent.xyz;\n\toutput.obj_bitangent = input.bitangent.xyz;\n\t#endif\n\n\treturn output;\n}\n\nstruct PS_OUTPUT\n{\n\tfloat4 albedo_roughness : SV_TARGET0;\n\tfloat4 normal_metallic : SV_TARGET1;\n\tfloat4 emissive_ao : SV_TARGET2;\n\t#ifdef IS_HYBRID\n\tfloat4 velocity : SV_TARGET3;\n\tfloat4 depth : SV_TARGET4;\n\tfloat4 world_position : SV_TARGET5;\n\t#endif\n};\n\nTexture2D material_albedo : register(t0);\nTexture2D material_normal : register(t1);\nTexture2D material_roughness : register(t2);\nTexture2D material_metallic : register(t3);\nTexture2D material_ao : register(t4);\nTexture2D material_emissive : register(t5);\n\nSamplerState s0 : register(s0);\n\ncbuffer MaterialProperties : register(b2)\n{\n\tMaterialData data;\n}\n\nuint dirToOct(float3 normal)\n{\n\tfloat2 p = normal.xy * (1.0 / dot(abs(normal), 1.0.xxx));\n\tfloat2 e = normal.z > 0.0 ? p : (1.0 - abs(p.yx)) * (step(0.0,p)*2.0-(float2)(1.0)); \n\treturn (asuint(f32tof16(e.y)) << 16) + (asuint(f32tof16(e.x)));\n}\n\nPS_OUTPUT main_ps(VS_OUTPUT input) : SV_TARGET\n{\n\tPS_OUTPUT output;\n\tfloat3x3 tbn = {input.tangent, input.bitangent, input.normal};\n\n\tOutputMaterialData output_data = InterpretMaterialData(data,\n\t\tmaterial_albedo,\n\t\tmaterial_normal,\n\t\tmaterial_roughness,\n\t\tmaterial_metallic,\n\t\tmaterial_emissive,\n\t\tmaterial_ao,\n\t\ts0,\n\t\tinput.uv);\n\n\tif (output_data.alpha <= 0.5f)\n\t{\n\t\tdiscard;\n\t}\n\n\tfloat3 normal = normalize(mul(output_data.normal, tbn));\n\n\toutput.albedo_roughness = float4(output_data.albedo.xyz, output_data.roughness);\n\toutput.normal_metallic = float4(normal, output_data.metallic);\n\toutput.emissive_ao = float4(output_data.emissive, output_data.ao);\n\n\t#ifdef IS_HYBRID\n\tfloat3x3 obj_tbn = {input.obj_tangent, input.obj_bitangent, input.obj_normal};\n\n\tfloat3 obj_normal = normalize(mul(output_data.normal, obj_tbn));\n\n\tfloat2 curr_pos = float2(input.curr_pos.xy / input.curr_pos.w) * 0.5 + 0.5;\n\tfloat2 prev_pos = float2(input.prev_pos.xy / input.prev_pos.w) * 0.5 + 0.5;\n\n\tconst float epsilon = 1e-5;\n\n\tfloat2 motion_vec = lerp(float2(curr_pos.x - prev_pos.x, -(curr_pos.y - prev_pos.y)), float2(0.0, 0.0), input.prev_pos.w < epsilon);\n\n\toutput.velocity = float4(motion_vec.xy, length(fwidth(input.world_pos.xyz)), length(fwidth(normal)));\n\n\tfloat linear_z = input.pos.z * input.pos.w;\n\tfloat prev_z = input.prev_pos.z;\n\tfloat max_change_z = max(abs(ddx(linear_z)), abs(ddy(linear_z)));\n\tfloat compressed_obj_normal = asfloat(dirToOct(normalize(obj_normal)));\n\toutput.depth = float4(linear_z, max_change_z, prev_z, compressed_obj_normal);\n\n\toutput.world_position = input.world_pos;\n\t#endif\n\n\treturn output;\n}\n\n#endif //__DEFERRED_GEOMETRY_PASS_HLSL__\n"
  },
  {
    "path": "resources/shaders/denoising_SVGF.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DENOISING_SVGF_HLSL__\n#define __DENOISING_SVGF_HLSL__\n\nTexture2D input_texture : register(t0);\nTexture2D motion_texture : register(t1);\nTexture2D normal_texture : register(t2);\nTexture2D depth_texture : register(t3);\n\nTexture2D in_hist_length_texture : register(t4);\n\nTexture2D prev_input_texture : register(t5);\nTexture2D prev_moments_texture : register(t6);\nTexture2D prev_normal_texture : register(t7);\nTexture2D prev_depth_texture : register(t8);\n\nRWTexture2D<float4> out_color_texture : register(u0);\nRWTexture2D<float2> out_moments_texture : register(u1);\nRWTexture2D<float> out_hist_length_texture : register(u2);\n\nSamplerState point_sampler   : register(s0);\nSamplerState linear_sampler  : register(s1);\n\n\ncbuffer DenoiserSettings : register(b0)\n{\n\tfloat blending_alpha;\n\tfloat blending_moments_alpha;\n    float l_phi;\n    float n_phi;\n    float z_phi;\n    float step_distance;\n\tfloat2 padding_2;\n};\n\nconst static float VARIANCE_CLIPPING_GAMMA = 8.0;\n\nfloat3 OctToDir(uint octo)\n{\n\tfloat2 e = float2( f16tof32(octo & 0xFFFF), f16tof32((octo>>16) & 0xFFFF) ); \n\tfloat3 v = float3(e, 1.0 - abs(e.x) - abs(e.y));\n\tif (v.z < 0.0)\n\t\tv.xy = (1.0 - abs(v.yx)) * (step(0.0, v.xy)*2.0 - (float2)(1.0));\n\treturn normalize(v);\n}\n\nfloat Luminance(float3 color)\n{\n\treturn (color.r + color.r + color.b + color.g + color.g + color.g) / 6.0;\n}\n\nuint DirToOct(float3 normal)\n{\n\tfloat2 p = normal.xy * (1.0 / dot(abs(normal), 1.0.xxx));\n\tfloat2 e = normal.z > 0.0 ? p : (1.0 - abs(p.yx)) * (step(0.0, p)*2.0 - (float2)(1.0));\n\treturn (asuint(f32tof16(e.y)) << 16) + (asuint(f32tof16(e.x)));\n}\n\nvoid FetchNormalAndLinearZ(in int2 ipos, out float3 norm, out float2 zLinear)\n{\n\tnorm = normal_texture[ipos];\n\tzLinear = depth_texture[ipos].xy;\n}\n\nfloat NormalDistanceCos(float3 n1, float3 n2, float power)\n{\n\treturn pow(max(0.0, dot(n1, n2)), 128.0);\n\t//return pow( saturate(dot(n1,n2)), power);\n\t//return 1.0f;\n}\n\nfloat NormalDistanceTan(float3 a, float3 b)\n{\n\tconst float d = max(1e-8, dot(a, b));\n\treturn sqrt(max(0.0, 1.0 - d * d)) / d;\n}\n\nfloat CalcWeights(\n\tfloat depth_center, float depth_p, float phi_depth,\n\tfloat3 normal_center, float3 normal_p, float norm_power, \n\tfloat luminance_direct_center, float luminance_direct_p, float phi_direct)\n{\n\tconst float w_normal    = NormalDistanceCos(normal_center, normal_p, norm_power);\n\tconst float w_z         = (phi_depth == 0) ? 0.0f : abs(depth_center - depth_p) / phi_depth;\n\tconst float w_l_direct   = abs(luminance_direct_center - luminance_direct_p) / phi_direct;\n\n\tconst float w_direct   = exp(0.0 - max(w_l_direct, 0.0)   - max(w_z, 0.0)) * w_normal;\n\n\treturn w_direct;\n}\n\nfloat ComputeWeightNoLuminance(float depth_center, float depth_p, float phi_depth, float3 normal_center, float3 normal_p)\n{\n\tconst float w_normal    = NormalDistanceCos(normal_center, normal_p, n_phi);\n\tconst float w_z         = abs(depth_center - depth_p) / phi_depth;\n\n\treturn exp(-max(w_z, 0.0)) * w_normal;\n}\n\nbool IsReprojectionValid(int2 coord, float z, float z_prev, float fwidth_z, float3 normal, float3 normal_prev, float fwidth_normal)\n{\n\tint2 screen_size = int2(0, 0);\n\tinput_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\tbool ret = (coord.x > -1 && coord.x < screen_size.x && coord.y > -1 && coord.y < screen_size.y);\n\n\tret = ret && ((abs(z_prev - z) / (fwidth_z + 1e-4)) < 2.0);\n\n\tret = ret && ((distance(normal, normal_prev) / (fwidth_normal + 1e-2)) < 16.0);\n\n\treturn ret;\n}\n\nbool LoadPrevData(float2 screen_coord, out float4 prev_direct, out float2 prev_moments, out float history_length)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\tinput_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 uv = screen_coord / screen_size;\n\n\tfloat4 motion = motion_texture.SampleLevel(point_sampler, uv, 0);\n\tfloat2 q_uv = uv - motion.xy;\n\n\tfloat2 prev_coords = q_uv * screen_size;\n\n\tfloat4 depth = depth_texture[prev_coords];\n\tfloat3 normal = OctToDir(asuint(depth.w));\n\n\tprev_direct = float4(0, 0, 0, 0);\n\tprev_moments = float2(0, 0);\n\n\tbool v[4];\n\tconst float2 pos_prev = prev_coords;\n\tint2 offset[4] = {int2(0, 0), int2(1, 0), int2(0, 1), int2(1, 1)};\n\n\tbool valid = false;\n\t[unroll]\n\tfor(int sample_idx = 0; sample_idx < 4; ++sample_idx)\n\t{\n\t\tint2 loc = int2(pos_prev) + offset[sample_idx];\n\t\tfloat4 depth_prev = prev_depth_texture[loc];\n\t\tfloat3 normal_prev = OctToDir(asuint(depth_prev.w));\n\n\t\tv[sample_idx] = IsReprojectionValid(loc, depth.z, depth_prev.x, depth.y, normal, normal_prev, motion.w);\n\n\t\tvalid = valid || v[sample_idx];\n\t}\n\n\tif(valid)\n\t{\n\t\tfloat sum_weights = 0;\n\t\tfloat x = frac(pos_prev.x);\n\t\tfloat y = frac(pos_prev.y);\n\n\t\tfloat weights[4] = {(1 - x) * (1 - y),\n\t\t\t\t\t\t\tx * (1 - y),\n\t\t\t\t\t\t\t(1 - x) * y,\n\t\t\t\t\t\t\tx * y };\n\n\t\t[unroll]\n\t\tfor(int sample_idx = 0; sample_idx < 4; ++sample_idx)\n\t\t{\n\t\t\tint2 loc = int2(pos_prev) + offset[sample_idx];\n\n\t\t\tprev_direct += weights[sample_idx] * prev_input_texture[loc] * float(v[sample_idx]);\n\t\t\tprev_moments += weights[sample_idx] * prev_moments_texture[loc] * float(v[sample_idx]);\n\t\t\tsum_weights += weights[sample_idx] * float(v[sample_idx]);\n\t\t}\n\n\t\tvalid = (sum_weights >= 0.01);\n\t\tprev_direct = lerp(float4(0, 0, 0, 0), prev_direct / sum_weights, valid);\n\t\tprev_moments = lerp(float2(0, 0), prev_moments / sum_weights, valid);\n\n\t}\n\tif(!valid)\n\t{\n\t\tfloat cnt = 0.0;\n\n\t\tconst int radius = 1;\n\t\tfor(int y = -radius; y <= radius; ++y)\n\t\t{\n\t\t\tfor(int x = -radius; x <= radius; ++x)\n\t\t\t{\n\t\t\t\tint2 p = prev_coords + int2(x, y);\n\t\t\t\tfloat4 depth_filter = prev_depth_texture[p];\n\t\t\t\tfloat3 normal_filter = OctToDir(asuint(depth_filter.w));\n\n\t\t\t\tif(IsReprojectionValid(prev_coords, depth.z, depth_filter.x, depth.y, normal, normal_filter, motion.w))\n\t\t\t\t{\n\t\t\t\t\tprev_direct += prev_input_texture[p];\n\t\t\t\t\tprev_moments += prev_moments_texture[p];\n\t\t\t\t\tcnt += 1.0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tvalid = cnt > 0;\n\t\tprev_direct /= lerp(1, cnt, cnt > 0);\n\t\tprev_moments /= lerp(1, cnt, cnt > 0);\n\t}\n\n\tif(valid)\n\t{\n\t\thistory_length = in_hist_length_texture[prev_coords].r;\n\t}\n\telse\n\t{\n\t\tprev_direct = float4(0, 0, 0, 0);\n\t\tprev_moments = float2(0, 0);\n\t\thistory_length = 0;\n\t}\n\n\treturn valid;\n}\n\nfloat ComputeVarianceCenter(int2 center)\n{\n\tfloat sum = 0.0;\n\n\tconst float kernel[2][2] = {\n\t\t{1.0 / 4.0, 1.0 / 8.0},\n\t\t{1.0 / 8.0, 1.0 / 16.0}\n\t};\n\n\tconst int radius = 1;\n\n\t[unroll]\n\tfor(int y = -radius; y <= radius; ++y)\n\t{\n\t\t[unroll]\n\t\tfor(int x = -radius; x <= radius; ++x)\n\t\t{\n\t\t\tint2 p  = center + int2(x, y);\n\n\t\t\tfloat k = kernel[abs(x)][abs(y)];\n\n\t\t\tsum += input_texture[p].w * k;\n\t\t}\n\t}\n\n\treturn sum;\n}\n\nfloat4 LineBoxIntersection(float3 box_min, float3 box_max, float3 c_in, float3 c_hist)\n{\n    float3 p_clip = 0.5 * (box_max + box_min);\n    float3 e_clip = 0.5 * (box_max - box_min);\n\n    float3 v_clip = c_hist - p_clip;\n    float3 v_unit = v_clip.xyz / e_clip;\n    float3 a_unit = abs(v_unit);\n    float ma_unit = max(a_unit.x, max(a_unit.y, a_unit.z));\n\n    if(ma_unit > 1.0)\n    {\n        return float4((p_clip + v_clip / ma_unit).xyz, ma_unit);\n    }\n    else\n    {\n        return float4(c_hist.xyz, ma_unit);\n    }\n}\n\n[numthreads(16,16,1)]\nvoid reprojection_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\tout_color_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 uv = float2(dispatch_thread_id.x / screen_size.x, dispatch_thread_id.y / screen_size.y);\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y);\n\n\tfloat4 direct = input_texture[screen_coord];\n\n\tfloat4 prev_direct = float4(0.0, 0.0, 0.0, 0.0);\n\tfloat2 prev_moments = float2(0.0, 0.0);\n\tfloat history_length = 0.0;\n\n\tbool success = LoadPrevData(screen_coord, prev_direct, prev_moments, history_length);\n\n\tif(isnan(prev_direct.x) || isnan(prev_direct.y) || isnan(prev_direct.z))\n\t{\n\t\tsuccess = false;\n\t\tprev_direct = 0.f;\n\t\tprev_moments = 0.f;\n\t\thistory_length = 0.f;\n\t}\n\n\tfloat3 moment_1 = float3(0.0, 0.0, 0.0);\n\tfloat3 moment_2 = float3(0.0, 0.0, 0.0);\n\n\tfloat3 clamp_min = 1.0;\n\tfloat3 clamp_max = 0.0;\n\n\t[unroll]\n\tfor(int y = -2; y <= 2; ++y)\n\t{\n\t\t[unroll]\n\t\tfor(int x = -2; x <= 2; ++x)\n\t\t{\n\t\t\tfloat3 color = input_texture[screen_coord + int2(x, y)].xyz;\n\t\t\tmoment_1 += color;\n\t\t\tmoment_2 += color * color;\n\t\t\tclamp_min = min(color, clamp_min);\n\t\t\tclamp_max = max(color, clamp_max);\n\t\t}\n\t}\n\n\tfloat3 mu = moment_1 / 25.0;\n\tfloat3 sigma = sqrt(moment_2 / 25.0 - mu*mu);\n\n\tfloat3 box_min = max(mu - VARIANCE_CLIPPING_GAMMA * sigma, clamp_min);\n\tfloat3 box_max = min(mu + VARIANCE_CLIPPING_GAMMA * sigma, clamp_max);\n\n\tfloat4 clipped = LineBoxIntersection(box_min, box_max, direct.xyz, prev_direct.xyz);\n\n\t//success = clipped.w > 1.0;\n\n\tprev_direct = float4(clipped.xyz, prev_direct.w);\n\n\thistory_length = min(32.0, success ? (history_length + 1.0) : 1.0);\n\n\tconst float alpha = lerp(1.0, max(blending_alpha, 1.0/history_length), success);\n\tconst float moments_alpha = lerp(1.0, max(blending_moments_alpha, 1.0/history_length), success);\n\n\tfloat2 moments = float2(0, 0);\n\tmoments.r = Luminance(direct.xyz);\n\tmoments.g = moments.r * moments.r;\n\n\tmoments = lerp(prev_moments, moments, moments_alpha);\n\n\tout_moments_texture[screen_coord] = moments;\n\tout_hist_length_texture[screen_coord] = history_length;\n\n\tfloat variance = max(0.f, moments.g - moments.r * moments.r);\n\n\tdirect = lerp(prev_direct, direct, alpha);\n\n\tout_color_texture[screen_coord] = float4(direct.xyz, variance);\n}\n\n[numthreads(16,16,1)]\nvoid filter_moments_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\tout_color_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 uv = float2(dispatch_thread_id.x / screen_size.x, dispatch_thread_id.y / screen_size.y);\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y);\n\n\tfloat h = in_hist_length_texture[screen_coord].r;\n\n\tif(h < 4.0)\n\t{\n\t\tfloat sum_weights = 0.0;\n\t\tfloat3 sum_direct = float3(0.0, 0.0, 0.0);\n\t\tfloat2 sum_moments = float2(0.0, 0.0);\n\n\t\tconst float4 direct_center = input_texture[screen_coord];\n\t\tconst float luminance_direct_center = Luminance(direct_center.xyz);\n\n\t\tfloat3 normal_center = float3(0.0, 0.0, 0.0);\n\t\tfloat2 depth_center = float2(0.0, 0.0);\n\n\t\tFetchNormalAndLinearZ(screen_coord, normal_center, depth_center);\n\n\n\t\tif(depth_center.x < 0.0)\n\t\t{\n\t\t\tout_color_texture[screen_coord] = direct_center;\n\t\t\treturn;\n\t\t}\n\n\t\tconst float phi_direct = l_phi;\n\t\tconst float phi_depth = max(depth_center.y, 1e-8) * 3.0;\n\n\t\tconst int radius = 3;\n\t\t[unroll]\n\t\tfor(int y = -radius; y <= radius; ++y)\n\t\t{\n\t\t\t[unroll]\n\t\t\tfor(int x = -radius; x <= radius; ++x)\n\t\t\t{\n\t\t\t\tconst int2 p = screen_coord + int2(x, y);\n\t\t\t\tconst bool inside = p.x >= 0 && p.x < screen_size.x && p.y >= 0 && p.y < screen_size.y;\n\t\t\t\tconst bool same_pixel = (x==0) && (y==0);\n\t\t\t\tconst float kernel = 1.0;\n\n\t\t\t\tif(inside)\n\t\t\t\t{\n\t\t\t\t\tconst float3 direct_p = input_texture[p].xyz;\n\t\t\t\t\tconst float2 moments_p = prev_moments_texture[p].xy;\n\n\t\t\t\t\tif(isnan(direct_p.x) || isnan(direct_p.y) || isnan(direct_p.z) || isnan(moments_p.x) || isnan(moments_p.y))\n\t\t\t\t\t{\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst float l_direct_p = Luminance(direct_p);\n\n\t\t\t\t\tfloat3 normal_p;\n\t\t\t\t\tfloat2 z_p;\n\t\t\t\t\tFetchNormalAndLinearZ(p, normal_p, z_p);\n\n\t\t\t\t\tconst float w = CalcWeights(\n\t\t\t\t\t\tdepth_center.x, z_p.x, phi_depth * length(float2(x, y)),\n\t\t\t\t\t\tnormal_center, normal_p, n_phi,\n\t\t\t\t\t\tluminance_direct_center, l_direct_p, l_phi);\n\n\t\t\t\t\tif(isnan(w))\n\t\t\t\t\t{\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\tsum_weights += w;\n\t\t\t\t\tsum_direct += direct_p * w;\n\n\t\t\t\t\tsum_moments += moments_p * float2(w.xx);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tsum_weights = max(sum_weights, 1e-6f);\n\n\t\tsum_direct /= sum_weights;\n\t\tsum_moments /= float2(sum_weights.xx);\n\n\t\tfloat variance = sum_moments.y - sum_moments.x * sum_moments.x;\n\n\t\tvariance *= 4.0/h;\n\n\t\tout_color_texture[screen_coord] = float4(sum_direct.xyz, variance);\n\t}\n\telse\n\t{\n\t\tout_color_texture[screen_coord] = input_texture[screen_coord];\n\t}\n}\n\n[numthreads(16,16,1)]\nvoid wavelet_filter_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\tout_color_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 uv = float2(dispatch_thread_id.x / screen_size.x, dispatch_thread_id.y / screen_size.y);\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y);\n\n\tconst float eps_variance = 1e-10;\n\tconst float kernel_weights[3] = {1.0, 2.0 / 3.0, 1.0 / 6.0};\n\n\tconst float4 direct_center = input_texture[screen_coord];\n\n\tconst float luminance_direct_center = Luminance(direct_center.xyz);\n\n\tconst float variance = ComputeVarianceCenter(int2(screen_coord.xy));\n\n\tout_color_texture[screen_coord] = direct_center;\n\t\n\tconst float history_length = in_hist_length_texture[screen_coord].r;\n\n\tfloat3 normal_center;\n\tfloat2 depth_center;\n\tFetchNormalAndLinearZ(screen_coord, normal_center, depth_center);\n\n\tif(depth_center.x < 0)\n\t{\n\t\tout_color_texture[screen_coord] = direct_center;\n\t\treturn;\n\t}\n\n\tconst float phi_l_direct = l_phi * sqrt(max(0.0, eps_variance + variance));\n\tconst float phi_depth = max(depth_center.y, 1e-8) * step_distance;\n\n\tfloat sum_weights = 1.0;\n\tfloat4 sum_direct = direct_center;\n\n\t[unroll]\n\tfor(int y = -2; y <= 2; ++y)\n\t{\n\t\t[unroll]\n\t\tfor(int x = -2; x <= 2; ++x)\n\t\t{\n\t\t\tconst int2 p = int2(screen_coord.xy) + int2(x, y) * step_distance;\n\t\t\tconst bool inside = p.x >= 0 && p.x < screen_size.x && p.y >= 0 && p.y < screen_size.y;\n\n\t\t\tconst float kernel = kernel_weights[abs(x)] * kernel_weights[abs(y)];\n\n\t\t\tif(inside && (x != 0 || y != 0))\n\t\t\t{\n\t\t\t\tconst float4 direct_p = input_texture[p];\n\n\t\t\t\tif(isnan(direct_p.x) || isnan(direct_p.y) || isnan(direct_p.z) || isnan(direct_p.w))\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tfloat3 normal_p;\n\t\t\t\tfloat2 depth_p;\n\t\t\t\tFetchNormalAndLinearZ(p, normal_p, depth_p);\n\n\t\t\t\tconst float luminance_direct_p = Luminance(direct_p.xyz);\n\n\t\t\t\tconst float w = CalcWeights(\n\t\t\t\t\tdepth_center.x, depth_p.x, phi_depth*length(float2(x, y)),\n\t\t\t\t\tnormal_center, normal_p, n_phi,\n\t\t\t\t\tluminance_direct_center, luminance_direct_p, phi_l_direct\n\t\t\t\t);\n\n\t\t\t\tif(isnan(w))\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst float w_direct = w * kernel;\n\n\t\t\t\tsum_weights += w_direct;\n\t\t\t\tsum_direct += float4(w_direct.xxx, w_direct * w_direct) * direct_p;\n\t\t\t}\n\t\t}\n\t}\n\n\tsum_weights = max(sum_weights, 1e-6f);\n\n\tout_color_texture[screen_coord] = float4(sum_direct / float4(sum_weights.xxx, sum_weights * sum_weights));\n}\n\n#endif //__DENOISING_SVGF_HLSL__"
  },
  {
    "path": "resources/shaders/denoising_reflections.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DENOISING_REFLECTIONS_HLSL__\n#define __DENOISING_REFLECTIONS_HLSL__\n#include \"pbr_util.hlsl\"\n#include \"rand_util.hlsl\"\n\nTexture2D input_texture : register(t0);\nTexture2D ray_raw_texture : register(t1);\nTexture2D dir_hit_t_texture : register(t2);\nTexture2D albedo_roughness_texture : register(t3);\nTexture2D normal_metallic_texture : register(t4);\nTexture2D motion_texture : register(t5); // xy: motion, z: fwidth position, w: fwidth normal\nTexture2D linear_depth_texture : register(t6);\nTexture2D world_position_texture : register(t7);\n\nTexture2D in_history_texture : register(t8);\n\nTexture2D accum_texture : register(t9);\nTexture2D prev_normal_texture : register(t10);\nTexture2D prev_depth_texture : register(t11);\nTexture2D in_moments_texture : register(t12);\n\nRWTexture2D<float4> output_texture : register(u0);\nRWTexture2D<float> out_history_texture : register(u1);\nRWTexture2D<float2> out_moments_texture : register(u2);\n\nSamplerState point_sampler : register(s0);\nSamplerState linear_sampler : register(s0);\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat4x4 view;\n\n\tfloat4x4 projection;\n\n\tfloat4x4 inv_projection;\n\n\tfloat4x4 inv_view;\n\n\tfloat4x4 prev_projection;\n\n\tfloat4x4 prev_view;\n\n\tuint is_hybrid;\n\tuint is_path_tracer;\n\tuint is_ao;\n\tuint has_shadows;\n\n\tfloat3 padding1;\n\tuint has_reflections;\n};\n\ncbuffer DenoiserSettings : register(b1)\n{\n\tfloat color_integration_alpha;\n\tfloat moments_integration_alpha;\n\tfloat variance_clipping_sigma;\n\tfloat roughness_reprojection_threshold;\n\tint max_history_samples;\n\tfloat n_phi;\n\tfloat z_phi;\n\tfloat l_phi;\n};\n\ncbuffer WaveletPass : register(b2)\n{\n\tfloat wavelet_size;\n}\n\nfloat3 OctToDir(uint octo)\n{\n\tfloat2 e = float2( f16tof32(octo & 0xFFFF), f16tof32((octo>>16) & 0xFFFF) ); \n\tfloat3 v = float3(e, 1.0 - abs(e.x) - abs(e.y));\n\tif (v.z < 0.0f)\n\t\tv.xy = (1.0f - abs(v.yx)) * (step(0.0f, v.xy) * 2.0f - float2(1.0f,1.0f));\n\treturn normalize(v);\n}\n\nfloat Luminance(float3 color)\n{\n\treturn (color.r + color.r + color.b + color.g + color.g + color.g) / 6.0f;\n}\n\nvoid FetchNormalAndLinearZ(in float2 ipos, out float3 norm, out float2 zLinear)\n{\n\tnorm = normal_metallic_texture[ipos].xyz;\n\tzLinear = linear_depth_texture[ipos].xy;\n}\n\nfloat NormalDistanceCos(float3 n1, float3 n2, float power)\n{\n\treturn pow(max(0.0f, dot(n1, n2)), power);\n}\n\nfloat ComputeWeight(\n\tfloat depth_center, float depth_p, float phi_depth,\n\tfloat3 normal_center, float3 normal_p, float norm_power, \n\tfloat luminance_direct_center, float luminance_direct_p, float phi_direct)\n{\n\tconst float w_normal    = NormalDistanceCos(normal_center, normal_p, norm_power);\n\tconst float w_z         = (phi_depth == 0.f) ? 0.0f : abs(depth_center - depth_p) / phi_depth;\n\tconst float w_l_direct   = abs(luminance_direct_center - luminance_direct_p) / phi_direct;\n\n\tconst float w_direct   = exp(0.0f - max(w_l_direct, 0.0f)   - max(w_z, 0.0f)) * w_normal;\n\n\treturn w_direct;\n}\n\nfloat ComputeWeightNoLuminance(float depth_center, float depth_p, float phi_depth, float3 normal_center, float3 normal_p)\n{\n\tconst float w_normal    = NormalDistanceCos(normal_center, normal_p, 128.f);\n\tconst float w_z         = abs(depth_center - depth_p) / phi_depth;\n\n\treturn exp(-max(w_z, 0.0f)) * w_normal;\n}\n\nbool IsReprojectionValid(float2 coord, float z, float z_prev, float fwidth_z, float3 normal, float3 normal_prev, float fwidth_normal)\n{\n\tint2 screen_size = int2(0, 0);\n\toutput_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\tbool ret = (coord.x >= 0.f && coord.x < screen_size.x && coord.y >= 0.f && coord.y < screen_size.y);\n\n\tret = ret && ((distance(normal, normal_prev) / (fwidth_normal + 1e-2)) < 16.0);\n\n\treturn ret;\n}\n\nbool LoadPrevData(float2 screen_coord, inout float2 found_pos, out float4 prev_direct, out float2 prev_moments, out float history_length)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 uv = screen_coord / screen_size;\n\n\tfloat4 motion = motion_texture.SampleLevel(point_sampler, uv, 0);\n\tfloat2 q_uv = uv - motion.xy;\n\n\tfloat2 prev_coords = q_uv * screen_size;\n\n\tconst float roughness = albedo_roughness_texture[screen_coord].w;\n\n\tfloat4 depth = linear_depth_texture[prev_coords];\n\tfloat3 normal = OctToDir(asuint(depth.w));\n\tprev_direct = float4(0.0f, 0.0f, 0.0f, 0.0f);\n\tprev_moments = float2(0.0f, 0.0f);\n\tbool v[4];\n\tconst float2 pos_prev = prev_coords;\n\tint2 offset[4] = {int2(0, 0), int2(1, 0), int2(0, 1), int2(1, 1)};\n\tbool valid = false;\n\t[unroll]\n\tfor(int sample_idx = 0; sample_idx < 4; ++sample_idx)\n\t{\n\t\tfloat2 loc = pos_prev + offset[sample_idx];\n\t\tfloat4 depth_prev = prev_depth_texture[loc];\n\t\tfloat3 normal_prev = OctToDir(asuint(depth_prev.w));\n\t\tv[sample_idx] = IsReprojectionValid(loc, depth.z, depth_prev.x, depth.y, normal, normal_prev, motion.w);\n\t\tvalid = valid || v[sample_idx];\n\t}\n\tif(valid)\n\t{\n\t\tfloat sum_weights = 0.0f;\n\t\tfloat x = frac(pos_prev.x);\n\t\tfloat y = frac(pos_prev.y);\n\t\tfloat weights[4] = {(1.0f - x) * (1.0f - y),\n\t\t\t\t\t\t\tx * (1.0f - y),\n\t\t\t\t\t\t\t(1.0f - x) * y,\n\t\t\t\t\t\t\tx * y };\n\t\t[unroll]\n\t\tfor(int sample_idx = 0; sample_idx < 4; ++sample_idx)\n\t\t{\n\t\t\tfloat2 loc = pos_prev + offset[sample_idx];\n\t\t\tprev_direct += weights[sample_idx] * accum_texture[loc] * float(v[sample_idx]);\n\t\t\tprev_moments += weights[sample_idx] * in_moments_texture[loc].xy * float(v[sample_idx]);\n\t\t\tsum_weights += weights[sample_idx] * float(v[sample_idx]);\n\t\t}\n\t\tvalid = (sum_weights >= 0.01f);\n\t\tprev_direct = lerp(float4(0.0f, 0.0f, 0.0f, 0.0f), prev_direct / sum_weights, valid);\n\t\tprev_moments = lerp(float2(0.0f, 0.0f), prev_moments / sum_weights, valid);\n\t}\n\tif(!valid)\n\t{\n\t\tfloat cnt = 0.0f;\n\t\tconst int radius = 1;\n\t\tfor(int y = -radius; y <= radius; ++y)\n\t\t{\n\t\t\tfor(int x = -radius; x <= radius; ++x)\n\t\t\t{\n\t\t\t\tfloat2 p = prev_coords + float2(x, y);\n\t\t\t\tfloat4 depth_filter = prev_depth_texture[p];\n\t\t\t\tfloat3 normal_filter = OctToDir(asuint(depth_filter.w));\n\t\t\t\tif(IsReprojectionValid(prev_coords, depth.z, depth_filter.x, depth.y, normal, normal_filter, motion.w))\n\t\t\t\t{\n\t\t\t\t\tprev_direct += accum_texture[p];\n\t\t\t\t\tprev_moments += in_moments_texture[p].xy;\n\t\t\t\t\tcnt += 1.0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tvalid = cnt > 0;\n\t\tprev_direct /= lerp(1, cnt, cnt > 0);\n\t\tprev_moments /= lerp(1, cnt, cnt > 0);\n\t}\n\tif(valid)\n\t{\n\t\thistory_length = in_history_texture[prev_coords].r;\n\t}\n\telse\n\t{\n\t\tprev_direct = float4(0.0f, 0.0f, 0.0f, 0.0f);\n\t\tprev_moments = float2(0.0f, 0.0f);\n\t\thistory_length = 0;\n\t}\n\tfound_pos = lerp(float2(-1.0f, -1.0f), pos_prev, valid);\n\treturn valid;\n}\n\nfloat ComputeVarianceCenter(int2 center)\n{\n\tfloat sum = 0.0f;\n\n\tconst float kernel[2][2] = {\n\t\t{1.0f / 4.0f, 1.0f / 8.0f},\n\t\t{1.0f / 8.0f, 1.0f / 16.0f}\n\t};\n\n\tconst int radius = 1;\n\n\t[unroll]\n\tfor(int y = -radius; y <= radius; ++y)\n\t{\n\t\t[unroll]\n\t\tfor(int x = -radius; x <= radius; ++x)\n\t\t{\n\t\t\tint2 p  = center + int2(x, y);\n\n\t\t\tfloat k = kernel[abs(x)][abs(y)];\n\n\t\t\tsum += input_texture[p].w * k;\n\t\t}\n\t}\n\n\treturn sum;\n}\n\nfloat4 LineBoxIntersection(float3 box_min, float3 box_max, float3 c_in, float3 c_hist)\n{\n    float3 p_clip = 0.5f * (box_max + box_min);\n    float3 e_clip = 0.5f * (box_max - box_min);\n\n    float3 v_clip = c_hist - p_clip;\n    float3 v_unit = v_clip.xyz / e_clip;\n    float3 a_unit = abs(v_unit);\n    float ma_unit = max(a_unit.x, max(a_unit.y, a_unit.z));\n\n    if(ma_unit > 1.0f)\n    {\n        return float4((p_clip + v_clip / ma_unit).xyz, ma_unit);\n    }\n    else\n    {\n        return float4(c_hist.xyz, ma_unit);\n    }\n}\n\n[numthreads(16, 16, 1)]\nvoid temporal_denoiser_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_coord = float2(dispatch_thread_id.xy);\n\tfloat2 screen_size = float2(0.0f, 0.0f);\n\toutput_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 uv = screen_coord / (screen_size - 1.0f);\n\n\tfloat4 accum_color = float4(0.0f, 0.0f, 0.0f, 0.0f);\n\tfloat2 prev_moments = float2(0.0f, 0.0f);\n\tfloat history = 0;\n\n\tfloat2 prev_pos = float2(0.0f, 0.0f);\n\n\tbool valid = LoadPrevData(screen_coord, prev_pos, accum_color, prev_moments, history);\n\thistory = lerp(0, history, valid);\n\t\n\tfloat4 input_color = input_texture[screen_coord];\n\n\tfloat pdf = ray_raw_texture.SampleLevel(point_sampler, uv, 0).w;\n\n\tif(pdf <= 0.0f)\n\t{\n\t\toutput_texture[screen_coord] = float4(input_color.xyz, 0.f);\n\t\treturn;\n\t}\n\n\tfloat3 moment_1 = float3(0.0f, 0.0f, 0.0f);\n\tfloat3 moment_2 = float3(0.0f, 0.0f, 0.0f);\n\n\tfloat3 clamp_min = 1.0f;\n\tfloat3 clamp_max = 0.0f;\n\n\t[unroll]\n\tfor(int y = -3; y <= 3; ++y)\n\t{\n\t\t[unroll]\n\t\tfor(int x = -3; x <= 3; ++x)\n\t\t{\n\t\t\tfloat3 color = input_texture[screen_coord + int2(x, y)].xyz;\n\t\t\tmoment_1 += color;\n\t\t\tmoment_2 += color * color;\n\t\t\tclamp_min = min(color, clamp_min);\n\t\t\tclamp_max = max(color, clamp_max);\n\t\t}\n\t}\n\n\tfloat3 mu = moment_1 / 49.0f;\n\tfloat3 sigma = sqrt(moment_2 / 49.0f - mu*mu);\n\n\tfloat3 box_min = mu - variance_clipping_sigma * sigma;\n\tfloat3 box_max = mu + variance_clipping_sigma * sigma;\n\n\tfloat4 clipped = LineBoxIntersection(box_min, box_max, input_color.xyz, accum_color.xyz);\n\n\tconst float roughness = albedo_roughness_texture[screen_coord].w;\n\n\t//perhaps replace the 0.25 with a clamping strength variable for the settings screen\n\taccum_color = lerp(clipped, accum_color, pow(clamp(roughness, 0.0f, 1.0f), 0.25f));\n\n\thistory = min(max_history_samples, history + 1);\n\n\tconst float alpha = lerp(1.0f, max(color_integration_alpha, 1.0f/history), valid);\n\tconst float moments_alpha = lerp(1.0f, max(moments_integration_alpha, 1.0f/history), valid);\n\n\tfloat2 cur_moments = float2(0.0f, 0.0f);\n\tcur_moments.x = Luminance(input_color.xyz);\n\tcur_moments.y = cur_moments.x * cur_moments.x;\n\n\tcur_moments = lerp(prev_moments, cur_moments, moments_alpha);\n\n\tfloat variance = cur_moments.y - cur_moments.x * cur_moments.x;\n\n\tfloat3 output = lerp(accum_color, input_color, alpha);\n\t\n\toutput_texture[floor(screen_coord)] = float4(output.xyz, variance);\n\tout_history_texture[floor(screen_coord)] = history;\n\tout_moments_texture[floor(screen_coord)] = cur_moments;\n}\n\n[numthreads(16, 16, 1)]\nvoid variance_estimator_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_coord = float2(dispatch_thread_id.xy);\n\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 uv = screen_coord / screen_size;\n\n\tfloat history = in_history_texture[screen_coord].r;\n\t[branch]\n\tif(history < 5.f)\n\t{\t\t\n\t\tfloat sum_weights = 0.0f;\n\t\tfloat3 sum_direct = float3(0.0f, 0.0f, 0.0f);\n\t\tfloat2 sum_moments = float2(0.0f, 0.0f);\n\n\t\tconst float4 direct_center = input_texture[screen_coord];\n\n\t\tfloat3 normal_center = float3(0.0f, 0.0f, 0.0f);\n\t\tfloat2 depth_center = float2(0.0f, 0.0f);\n\n\t\tFetchNormalAndLinearZ(screen_coord, normal_center, depth_center);\n\n\t\tif(depth_center.x < 0.0f)\n\t\t{\n\t\t\toutput_texture[screen_coord] = direct_center;\n\t\t\treturn;\n\t\t}\n\t\t\n\t\tconst float phi_direct = l_phi;\n\t\tconst float phi_depth = max(depth_center.y, 1e-8) * 3.0f;\n\n\t\tconst int radius = 3.f;\n\t\t[unroll]\n\t\tfor(int y = -radius; y <= radius; ++y)\n\t\t{\n\t\t\t[unroll]\n\t\t\tfor(int x = -radius; x <= radius; ++x)\n\t\t\t{\t\t\t\t\n\t\t\t\tconst int2 p = screen_coord + int2(x, y);\n\t\t\t\tconst bool inside = p.x >= 0.f && p.x < screen_size.x && p.y >= 0.f && p.y < screen_size.y;\n\t\t\t\tconst bool same_pixel = (x==0.f) && (y==0.f);\n\t\t\t\tconst float kernel = 1.0f;\n\n\t\t\t\tif(inside)\n\t\t\t\t{\n\t\t\t\t\tconst float3 direct_p = input_texture[p].xyz;\n\t\t\t\t\tconst float2 moments_p = in_moments_texture[p].xy;\n\n\t\t\t\t\tfloat3 normal_p;\n\t\t\t\t\tfloat2 z_p;\n\t\t\t\t\tFetchNormalAndLinearZ(p, normal_p, z_p);\n\n\t\t\t\t\tconst float w = ComputeWeightNoLuminance(\n\t\t\t\t\t\tdepth_center.x, z_p.x, phi_depth * length(float2(x, y)),\n\t\t\t\t\t\tnormal_center, normal_p);\n\n\t\t\t\t\tif(isnan(direct_p.x) || isnan(direct_p.y) || isnan(direct_p.z) || isnan(w))\n\t\t\t\t\t{\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\n\t\t\t\t\tsum_weights += w;\n\t\t\t\t\tsum_direct += direct_p * w;\n\n\t\t\t\t\tsum_moments += moments_p * float2(w.xx);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tsum_weights = max(sum_weights, 1e-6f);\n\n\t\tsum_direct /= sum_weights;\n\t\tsum_moments /= float2(sum_weights.xx);\n\n\t\tfloat variance = sum_moments.y - sum_moments.x * sum_moments.x;\n\n\t\tvariance *= 5.0f/history;\n\n\t\toutput_texture[screen_coord] = float4(sum_direct.xyz, variance);\n\t}\n\telse\n\t{\n\t\toutput_texture[screen_coord] = input_texture[screen_coord];\n\t}\n}\n\n[numthreads(16, 16, 1)]\nvoid spatial_denoiser_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n    float2 screen_coord = float2(dispatch_thread_id.xy);\n\n    float2 screen_size = float2(0.0f, 0.0f);\n    output_texture.GetDimensions(screen_size.x, screen_size.y);\n\n\t\n    float2 uv = screen_coord / screen_size;\n\t\n\tconst float eps_variance = 1e-10;\n\tconst float kernel_weights[3] = {1.0f, 2.0f / 3.0f, 1.0f / 6.0f};\n\n\tconst float4 direct_center = input_texture[screen_coord];\n\tconst float luminance_direct_center = Luminance(direct_center.xyz);\n\n\tconst float variance = ComputeVarianceCenter(int2(screen_coord.xy));\n\n\tif(isnan(variance))\n\t{\n\t\toutput_texture[screen_coord] = float4(0.0f, 1.0f, 0.0f, 0.0f);\n\t\treturn;\n\t}\n\n\toutput_texture[screen_coord] = direct_center;\n\t\n\tconst float history_length = in_history_texture[screen_coord].r;\n\n\tfloat3 normal_center;\n\tfloat2 depth_center;\n\tFetchNormalAndLinearZ(screen_coord, normal_center, depth_center);\n\n\tif(depth_center.x < 0.0f)\n\t{\n\t\toutput_texture[screen_coord] = direct_center;\n\t\treturn;\n\t}\n\n\tconst float roughness = albedo_roughness_texture[screen_coord].w;\n\n\tconst float phi_l_direct = l_phi * sqrt(max(0.0f, variance));\n\tconst float phi_depth = max(depth_center.y, 1e-8) * wavelet_size;\n\n\tfloat sum_weights = 1.0f;\n\tfloat4 sum_direct = direct_center;\n\n\t[unroll]\n\tfor(int y = -2; y <= 2; ++y)\n\t{\n\t\t[unroll]\n\t\tfor(int x = -2; x <= 2; ++x)\n\t\t{\t\t\t\n\t\t\tconst int2 p = int2(screen_coord.xy) + int2(x, y) * wavelet_size;\n\t\t\tconst bool inside = p.x >= 0 && p.x < screen_size.x && p.y >= 0 && p.y < screen_size.y;\n\n\t\t\tconst float kernel = kernel_weights[abs(x)] * kernel_weights[abs(y)];\n\n\t\t\tif(inside && (x != 0 || y != 0))\n\t\t\t{\t\t\t\t\n\t\t\t\tconst float4 direct_p = input_texture[p];\n\t\t\t\tconst float luminance_direct_p = Luminance(direct_p.xyz);\n\n\t\t\t\tfloat3 normal_p;\n\t\t\t\tfloat2 depth_p;\n\t\t\t\tFetchNormalAndLinearZ(p, normal_p, depth_p);\t\t\n\n\t\t\t\tconst float w = ComputeWeight(\n\t\t\t\t\tdepth_center.x, depth_p.x, phi_depth*length(float2(x, y)),\n\t\t\t\t\tnormal_center, normal_p, n_phi,\n\t\t\t\t\tluminance_direct_center, luminance_direct_p, phi_l_direct) * sqrt(max(1e-15f, roughness)); \n\n\t\t\t\tconst float w_direct = w * kernel;\n\n\t\t\t\tsum_weights += w_direct;\n\t\t\t\tsum_direct += float4(w_direct.xxx, w_direct * w_direct) * direct_p;\n\t\t\t}\n\t\t}\n\t}\n\n\toutput_texture[screen_coord] = sum_direct / sum_weights;\n}\n\n#endif"
  },
  {
    "path": "resources/shaders/denoising_spatial_reconstruction.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n #ifndef __DENOISING_SPATIAL_RECONSTRUCTION_HLSL__\n#define __DENOISING_SPATIAL_RECONSTRUCTION_HLSL__\n#include \"pbr_util.hlsl\"\n#include \"rand_util.hlsl\"\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat4x4 inv_vp;\n\tfloat4x4 inv_view;\n\n\tfloat padding;\n\tuint frame_idx;\n\tfloat near_plane, far_plane;\n};\n\nRWTexture2D<float4> filtered : register(u0);\nTexture2D reflection_pdf : register(t0);\nTexture2D dir_hit_t : register(t1);\nTexture2D albedo_roughness : register(t2);\nTexture2D normal_metallic : register(t3);\nTexture2D depth_buffer : register(t4);\nSamplerState nearest_sampler  : register(s0);\nSamplerState linear_sampler : register(s1);\n\nfloat linearize_depth(float D)\n{\n\tfloat z_n = D * 2.0f - 1.0f;\n\treturn 2.0f * near_plane * far_plane / (far_plane + near_plane - z_n * (far_plane - near_plane));\n}\n\n//Get total weight from neighbor using normal and normalized depth from both neighbor and center\nfloat neighbor_edge_weight(float3 N, float3 N_neighbor, float D, float D_neighbor, float2 uv)\n{\n\treturn float(uv.x >= 0.f && uv.y >= 0.f && uv.x <= 1.f && uv.y <= 1.f) * (D != 1.0f && D_neighbor != 1.0f);\n}\n\n//Hardcode the samples for now; settings: kernelSize=5, points=16\n//https://github.com/Nielsbishere/NoisePlayground/blob/master/bluenoise_test.py\n\nstatic const uint sample_count = 16;\nstatic const float2 samples[4][16] = {\n\t\t{\n\t\t\t\tfloat2(0.f ,  0.f), float2(-1.f ,  -1.f), float2(1.f ,  0.f), float2(-2.f ,  -1.f),\n\t\t\t\tfloat2(-2.f ,  1.f), float2(0.f ,  -3.f), float2(2.f ,  -2.f), float2(1.f ,  2.f),\n\t\t\t\tfloat2(0.f ,  3.f), float2(-4.f ,  -1.f), float2(0.f ,  -4.f), float2(-2.f ,  3.f),\n\t\t\t\tfloat2(-4.f ,  1.f), float2(3.f ,  1.f), float2(3.f ,  -3.f), float2(4.f ,  0.f)\n\n\t\t},\n\t\t{\n\t\t\t\tfloat2(0.f ,  0.f), float2(1.f ,  -1.f), float2(0.f ,  -2.f), float2(-1.f ,  -2.f),\n\t\t\t\tfloat2(-1.f ,  2.f), float2(0.f ,  2.f), float2(2.f ,  0.f), float2(-3.f ,  1.f),\n\t\t\t\tfloat2(-3.f ,  -2.f), float2(1.f ,  -3.f), float2(-3.f ,  2.f), float2(3.f ,  0.f),\n\t\t\t\tfloat2(-2.f ,  -4.f), float2(1.f ,  3.f), float2(-4.f ,  -2.f), float2(2.f ,  3.f)\n\n\t\t},\n\t\t{\n\t\t\t\tfloat2(0.f ,  0.f), float2(-1.f ,  0.f), float2(0.f ,  1.f), float2(-2.f ,  0.f),\n\t\t\t\tfloat2(2.f ,  -1.f), float2(-1.f ,  -3.f), float2(-3.f ,  -1.f), float2(2.f ,  2.f),\n\t\t\t\tfloat2(2.f ,  -3.f), float2(3.f ,  -1.f), float2(-3.f ,  -3.f), float2(-4.f ,  0.f),\n\t\t\t\tfloat2(-1.f ,  -4.f), float2(-4.f ,  2.f), float2(-3.f ,  3.f), float2(4.f ,  -1.f)\n\n\t\t},\n\t\t{\n\t\t\t\tfloat2(0.f ,  0.f), float2(0.f ,  -1.f), float2(-1.f ,  1.f), float2(-2.f ,  -2.f),\n\t\t\t\tfloat2(1.f ,  1.f), float2(1.f ,  -2.f), float2(-3.f ,  0.f), float2(2.f ,  1.f),\n\t\t\t\tfloat2(-2.f ,  -3.f), float2(-2.f ,  2.f), float2(-1.f ,  3.f), float2(1.f ,  -4.f),\n\t\t\t\tfloat2(3.f ,  -2.f), float2(3.f ,  2.f), float2(-4.f ,  -3.f), float2(0.f ,  4.f)\n\n\t\t}\n};\n\n//Sample a neighbor; 0,0 -> 1,1; outside of that range indicates an invalid uv\nfloat2 sample_neighbor_uv(uint sampleId, uint2 full_res_pixel, uint2 resolution, float2 rand, float kernelSize)\n{\n\tuint pixId = full_res_pixel.x % 2 + full_res_pixel.y % 2 * 2;\n\tfloat2 offset = samples[pixId][sampleId] * kernelSize;\n\toffset = mul(float2x2(rand.x, rand.y, -rand.y, rand.x), offset);\n\treturn (float2(full_res_pixel / 2.f) + offset) / float2(resolution / 2.f - 1.f);\n}\n\nstatic const float3 luminance = float3(0.2126f, 0.7152f, 0.0722f);\n\nfloat3 unpack_position(float2 uv, float depth)\n{\n  // Get world space position\n  const float4 ndc = float4(uv * 2.0f - 1.0f, depth, 1.0f);\n  float4 wpos = mul(inv_vp, ndc);\n  return (wpos.xyz / wpos.w).xyz;\n}\n\n[numthreads(16, 16, 1)]\nvoid main(int3 pix3 : SV_DispatchThreadID)\n{\n\t//Get dispatch dimensions\n\n\tuint2 pix = uint2(pix3.xy);\n\tuint width, height;\n\tdepth_buffer.GetDimensions(width, height);\n\n\t//Get per pixel values\n\n\tconst float depth = depth_buffer[pix].r;\n\tconst float2 uv = float2(pix.xy) / float2(width - 1.f, height - 1.f);\n\tconst float3 pos = unpack_position(uv, depth);\n\n\tconst float3 camera_pos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]);\n\tconst float3 V = normalize(camera_pos - pos);\n\n\tfloat roughness = albedo_roughness[pix].w;\n\tconst float3 N = normalize(normal_metallic[pix].xyz);\n\n\tconst float pdf = reflection_pdf.SampleLevel(nearest_sampler, uv, 0).w;\n\n\tfloat3 result3;\n\n\t//pdf < 0 disables spatial reconstruction\n\tif (pdf >= 0)\n\t{\n\n\t\t//Weigh the samples correctly\n\n\t\tfloat3 result = float3(0.f, 0.f, 0.f);\n\t\tfloat weight_sum = 0.0f;\n\n\t\tuint rand_seed = initRand(pix.x + pix.y * width, frame_idx);\n\n\t\tfloat distance = length(camera_pos - pos);\n\t\tfloat sampleCountScalar = lerp(1.f, (1.f - distance / far_plane) * roughness, 1);\n\t\troughness = max(roughness, 1e-2);\n\n\t\tfloat kernel_size = 8.f;\n\n\t\tfor (uint i = 0; i < max(ceil(kernel_size), 1); ++i) {\n\n\t\t\t//Get sample related data\n\n\t\t\tfloat2 random = float2(nextRand(rand_seed), nextRand(rand_seed));\n\n\t\t\tconst float2 neighbor_uv = sample_neighbor_uv(i, pix, uint2(width, height), random, 1);\n\n\t\t\tconst float depth_neighbor = depth_buffer.SampleLevel(nearest_sampler, neighbor_uv, 0).r;\n\t\t\tconst float3 pos_neighbor = unpack_position(neighbor_uv, depth_neighbor);\n\n\t\t\tconst float4 hit_t = dir_hit_t.SampleLevel(nearest_sampler, neighbor_uv, 0);\n\n\t\t\tconst float3 V_neighbor = normalize(camera_pos - pos_neighbor);\n\n\t\t\tconst float4 reflection_pdf_neighbor = reflection_pdf.SampleLevel(nearest_sampler, neighbor_uv, 0);\n\t\t\tif(reflection_pdf_neighbor.w>0.0)\n\t\t\t{\n\t\t\t\tconst float3 color = clamp(reflection_pdf_neighbor.xyz, 0, 1);\n\t\t\t\tconst float3 L = hit_t.xyz;\n\t\t\t\tconst float pdf_neighbor = max(reflection_pdf_neighbor.w, 1e-5);\n\t\t\t\tconst float3 N_neighbor = normalize(normal_metallic.SampleLevel(nearest_sampler, neighbor_uv, 0).xyz);\n\t\n\t\t\t\t//Calculate weight and weight sum\n\t\n\t\t\t\tconst float neighbor_weight = neighbor_edge_weight(N, N_neighbor, depth, depth_neighbor, neighbor_uv);\n\t\t\t\t\n\t\t\t\tfloat weight = (brdf_weight(-V, L, N, roughness) / pdf_neighbor) * neighbor_weight;\n\t\t\t\tweight = isnan(weight) || isinf(weight) ? 0 : weight;\n\t\t\t\tresult += color * weight;\n\t\t\t\tweight_sum += weight;\n\t\t\t}\n\t\t}\n\n\t\tresult3 = result / weight_sum;\n\t\tif(weight_sum == 0.0f)\n\t\t{\n\t\t\tresult3 = reflection_pdf.SampleLevel(nearest_sampler, uv, 0).xyz;\n\t\t}\n\n\t\t\n\t\tif(isnan(weight_sum) == true)\n\t\t{\n\t\t\tresult3 = float3(0, 0, 1);\n\t\t\tfiltered[pix] = float4(result3, 1);\n\t\t\treturn;\n\t\t}\n\t}\n\telse\n\t{\n\t\tresult3 = reflection_pdf.SampleLevel(nearest_sampler, uv, 0).xyz;\n\t}\n\n\tfiltered[pix] = float4(result3, 1);\n\n}\n\n#endif"
  },
  {
    "path": "resources/shaders/dxr_ambient_occlusion.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_AMBIENT_OCCLUSION_HLSL__\n#define __DXR_AMBIENT_OCCLUSION_HLSL__\n\n#include \"rand_util.hlsl\"\n#include \"dxr_global.hlsl\"\n\nRWTexture2D<float4> output : register(u0); // x: AO value\nTexture2D gbuffer_normal : register(t1);\nTexture2D gbuffer_depth : register(t2);\n\nstruct AOHitInfo\n{\n  float is_hit;\n  float thisvariablesomehowmakeshybridrenderingwork_killme;\n};\n\ncbuffer CBData : register(b0)\n{\n\tfloat4x4 inv_vp;\n\tfloat4x4 inv_view;\n\n\tfloat bias;\n\tfloat radius;\n\tfloat power;\n\tfloat max_distance;\n\t\n\tfloat2 padding;\n\tfloat frame_idx;\n\tunsigned int sample_count;\n};\n\nstruct Attributes { };\n\nfloat3 unpack_position(float2 uv, float depth)\n{\n\t// Get world space position\n\tconst float4 ndc = float4(uv * 2.0 - 1.0, depth, 1.0);\n\tfloat4 wpos = mul(inv_vp, ndc);\n\treturn (wpos.xyz / wpos.w).xyz;\n}\n\nbool TraceAORay(uint idx, float3 origin, float3 direction, float far, unsigned int depth)\n{\n\t// Define a ray, consisting of origin, direction, and the min-max distance values\n\tRayDesc ray;\n\tray.Origin = origin;\n\tray.Direction = direction;\n\tray.TMin = 0.f;\n\tray.TMax = far;\n\n\tAOHitInfo payload = { 1.0f, 0.0f };\n\n\t// Trace the ray\n\tTraceRay(\n\t\tScene,\n\t\tRAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER,\n\t\t~0, // InstanceInclusionMask\n\t\t0, // RayContributionToHitGroupIndex\n\t\t0, // MultiplierForGeometryContributionToHitGroupIndex\n\t\t0, // miss shader index is set to idx but can probably be anything.\n\t\tray,\n\t\tpayload);\n\n\treturn payload.is_hit;\n}\n\n\n[shader(\"raygeneration\")]\nvoid AORaygenEntry()\n{\n\t// Texture UV coordinates [0, 1]\n\tfloat2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1);\n\n    uint rand_seed = initRand(DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x, frame_idx);\n\n\t// Screen coordinates [0, resolution] (inverted y)\n\tint2 screen_co = DispatchRaysIndex().xy;\n\n    float3 normal = gbuffer_normal[screen_co].xyz;\n\tfloat depth = gbuffer_depth[screen_co].x;\n\tfloat3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth);\n\n\tfloat3 camera_pos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]);\n\tfloat cam_distance = length(wpos-camera_pos);\n\tif(cam_distance < max_distance)\n\t{\n\t\t//SPP decreases the closer a pixel is to the max distance\n\t\t//Total is always calculated using the full sample count to have further pixels less occluded\n\t\tint spp = min(sample_count, round(sample_count * ((max_distance - cam_distance)/max_distance))); \n\t\tint ao_value = sample_count;\n\t\tfor(uint i = 0; i< spp; i++)\n\t\t{\n\t\t\t ao_value -= TraceAORay(0, wpos + normal * bias , getCosHemisphereSample(rand_seed, normal), radius, 0);\n\t\t}\n\t\toutput[DispatchRaysIndex().xy].x = pow(ao_value/float(sample_count), power);\n\t}\n\telse\n\t{\n\t\toutput[DispatchRaysIndex().xy].x = 1.f;\n\t}\n\n}\n\n[shader(\"miss\")]\nvoid MissEntry(inout AOHitInfo hit : SV_RayPayload)\n{\n    hit.is_hit = 0.0f;\n}\n\n#endif //__DXR_AMBIENT_OCCLUSION_HLSL__"
  },
  {
    "path": "resources/shaders/dxr_functions.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_FUNCTIONS_HLSL__\n#define __DXR_FUNCTIONS_HLSL__\n\nfloat3 HitAttribute(float3 a, float3 b, float3 c, BuiltInTriangleIntersectionAttributes attr)\n{\n\tfloat3 vertexAttribute[3];\n\tvertexAttribute[0] = a;\n\tvertexAttribute[1] = b;\n\tvertexAttribute[2] = c;\n\n\treturn vertexAttribute[0] +\n\t\tattr.barycentrics.x * (vertexAttribute[1] - vertexAttribute[0]) +\n\t\tattr.barycentrics.y * (vertexAttribute[2] - vertexAttribute[0]);\n}\n\nuint3 Load3x32BitIndices(ByteAddressBuffer buffer, uint offsetBytes)\n{\n\t// Load first 2 indices\n\treturn buffer.Load3(offsetBytes);\n}\n\n// Retrieve hit world position.\nfloat3 HitWorldPosition()\n{\n\treturn WorldRayOrigin() + RayTCurrent() * WorldRayDirection();\n}\n\nfloat3 unpack_position(float2 uv, float depth, float4x4 inv_vp)\n{\n\t// Get world space position\n\tconst float4 ndc = float4(uv * 2.0 - 1.0, depth, 1.0);\n\tfloat4 wpos = mul(inv_vp, ndc);\n\treturn (wpos.xyz / wpos.w).xyz;\n}\n\nfloat3 CalcPeturbedNormal(float3 normal, float3 normal_map, float3 tangent, float3 bitangent, float3 V, out float3 world_normal)\n{\n\tfloat3x4 object_to_world = ObjectToWorld3x4();\n\tfloat3 N = normalize(mul(object_to_world, float4(normal, 0)));\n\tfloat3 T = normalize(mul(object_to_world, float4(tangent, 0)));\n#ifndef CALC_BITANGENT\n\tconst float3 B = normalize(mul(object_to_world, float4(bitangent, 0)));\n#else\n\tT = normalize(T - dot(T, N) * N);\n\tfloat3 B = cross(N, T);\n#endif\n\tconst float3x3 TBN = float3x3(T, B, N);\n\n\tfloat3 fN = normalize(mul(normal_map, TBN));\n\n\tworld_normal = N;\n\n\treturn fN;\n}\n\nfloat3 CalcPeturbedNormal(float3 normal, float3 normal_map, float3 tangent, float3 bitangent, float3 V)\n{\n\tfloat3 temp = 0;\n\treturn CalcPeturbedNormal(normal, normal_map, tangent, bitangent, V, temp);\n}\n\n\n#endif //__DXR_FUNCTIONS_HLSL__\n"
  },
  {
    "path": "resources/shaders/dxr_global.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_GLOBAL_HLSL__\n#define __DXR_GLOBAL_HLSL__\n\n#define MAX_RECURSION 2\n//#define FOUR_X_A\n//#define PATH_TRACING\n//#define DEPTH_OF_FIELD\n#define EPSILON 0.1\n#define SOFT_SHADOWS\n#define MAX_SHADOW_SAMPLES 1\n\n//#define PERFECT_MIRROR_REFLECTIONS\n#define GROUND_TRUTH_REFLECTIONS\n#define MAX_GT_REFLECTION_SAMPLES 4\n//#define DISABLE_SPATIAL_RECONSTRUCTION\n\n#define RUSSIAN_ROULETTE\n#define NO_PATH_TRACED_NORMALS\n#define CALC_BITANGENT // calculate bitangent in the shader instead of using the bitangent uploaded\n\n#ifdef FALLBACK\n\t#undef MAX_RECURSION\n\t#define MAX_RECURSION 1\n\n\t#undef GROUND_TRUTH_REFLECTIONS\n\t#undef MAX_GT_REFLECTION_SAMPLES\n\t#define MAX_GT_REFLECTION_SAMPLES 2\n\t\n\t//#define NO_SHADOWS\n#endif\n\nRaytracingAccelerationStructure Scene : register(t0, space0);\n\n#endif //__DXR_GLOBAL_HLSL__\n"
  },
  {
    "path": "resources/shaders/dxr_pathtracer_accumulation.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_PATHTRACER_ACCUMULATION_HLSL__\n#define __DXR_PATHTRACER_ACCUMULATION_HLSL__\n\n#include \"pp_util.hlsl\"\n#include \"pp_hdr_util.hlsl\"\n\nTexture2D<float4> input : register(t0);\nRWTexture2D<float4> output : register(u0);\nSamplerState s0 : register(s0);\n\ncbuffer Properties : register(b0)\n{\n\tfloat frame_idx;\n};\n\n\n[numthreads(16, 16, 1)]\nvoid main(uint3 DTid : SV_DispatchThreadID)\n{\n\tfloat4 current = input[DTid.xy]; // Last path tracer result\n\tfloat4 prev = output[DTid.xy]; // Previous path tracer output\n\n\tfloat accum_count = frame_idx; // 0 to x, the number of times the accumulation has ran.\n\n\tfloat4 color = (accum_count * prev + current) / (accum_count + 1); // accumulate\n\n\toutput[DTid.xy] = color; // update the output with the accumulated result.\n}\n\n#endif //__DXR_PATHTRACER_ACCUMULATION_HLSL__"
  },
  {
    "path": "resources/shaders/dxr_pathtracer_entries.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_PATHTRACER_ENTRIES_HLSL__\n#define __DXR_PATHTRACER_ENTRIES_HLSL__\n\n#include \"dxr_shadow_entries.hlsl\"\n\n//Reflections\n[shader(\"closesthit\")]\nvoid ReflectionHit(inout PathTracingHitInfoCone payload, in Attributes attr)\n{\n\t// Calculate the essentials\n\tconst Offset offset = g_offsets[InstanceID()];\n\tconst Material material = g_materials[offset.material_idx];\n\tconst float3 hit_pos = HitWorldPosition();\n\tconst float index_offset = offset.idx_offset;\n\tconst float vertex_offset = offset.vertex_offset;\n\n\t// Find first index location\n\tconst uint index_size = 4;\n\tconst uint indices_per_triangle = 3;\n\tconst uint triangle_idx_stride = indices_per_triangle * index_size;\n\n\tuint base_idx = PrimitiveIndex() * triangle_idx_stride;\n\tbase_idx += index_offset * 4; // offset the start\n\n\tuint3 indices = Load3x32BitIndices(g_indices, base_idx);\n\tindices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start\n\n\t// Gather triangle vertices\n\tconst Vertex v0 = g_vertices[indices.x];\n\tconst Vertex v1 = g_vertices[indices.y];\n\tconst Vertex v2 = g_vertices[indices.z];\n\n\t// Variables\n\tconst float3 V = normalize(payload.origin - hit_pos);\n\n\t// Calculate actual \"fragment\" attributes.\n\tconst float3 frag_pos = HitAttribute(v0.pos, v1.pos, v2.pos, attr);\n\tconst float3 normal = normalize(HitAttribute(v0.normal, v1.normal, v2.normal, attr));\n\tconst float3 tangent = HitAttribute(v0.tangent, v1.tangent, v2.tangent, attr);\n\tconst float3 bitangent = HitAttribute(v0.bitangent, v1.bitangent, v2.bitangent, attr);\n\n\tfloat2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy;\n\tuv.y = 1.0f - uv.y;\n\n\tfloat mip_level = payload.depth + 1;\n\n\tOutputMaterialData output_data = InterpretMaterialDataRT(material.data,\n\t\tg_textures[material.albedo_id],\n\t\tg_textures[material.normal_id],\n\t\tg_textures[material.roughness_id],\n\t\tg_textures[material.metalicness_id],\n\t\tg_textures[material.emissive_id],\n\t\tg_textures[material.ao_id],\n\t\tmip_level,\n\t\ts0,\n\t\tuv);\n\n\tfloat3 albedo = output_data.albedo;\n\tfloat roughness = output_data.roughness;\n\tfloat metal = output_data.metallic;\n\tfloat3 emissive = output_data.emissive;\n\tfloat ao = output_data.ao;\n\n#ifdef NO_PATH_TRACED_NORMALS\n\tfloat3 N = normalize(mul(ObjectToWorld3x4(), float4(normal, 0)));\n\tfloat3 fN = N;\n#else\n\tfloat3 N = 0;\n\tfloat3 fN = CalcPeturbedNormal(normal, output_data.normal, tangent, bitangent, V, N);\n#endif\n\n\t// #################### GGX #####################\n\tnextRand(payload.seed);\n\tpayload.color = ggxIndirect(hit_pos, fN, N, V, albedo, metal, roughness, ao, payload.seed, payload.depth + 1, true, payload.cone);\n\tpayload.color += ggxDirect(hit_pos, fN, N, V, albedo, metal, roughness, payload.seed, payload.depth + 1);\n\tpayload.color += emissive;\n}\n\n//Reflection skybox\n[shader(\"miss\")]\nvoid ReflectionMiss(inout PathTracingHitInfoCone payload)\n{\n\tpayload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0).rgb;\n}\n\n#endif //__DXR_PATHTRACER_ENTRIES_HLSL__\n"
  },
  {
    "path": "resources/shaders/dxr_pathtracer_functions.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_PATHTRACER_FUNCTIONS_HLSL__\n#define __DXR_PATHTRACER_FUNCTIONS_HLSL__\n\n#include \"pbr_util.hlsl\"\n\nfloat3 TraceColorRay(float3 origin, float3 direction, unsigned int depth, unsigned int seed)\n{\n\tif (depth >= MAX_RECURSION)\n\t{\n\t\treturn float3(0, 0, 0);\n\t}\n\n\t// Define a ray, consisting of origin, direction, and the min-max distance values\n\tRayDesc ray;\n\tray.Origin = origin;\n\tray.Direction = direction;\n\tray.TMin = 0;\n\tray.TMax = 1.0e38f;\n\n\tPathTracingHitInfo payload = { float3(1, 1, 1), seed, origin, depth };\n\n\t// Trace the ray\n\tTraceRay(\n\t\tScene,\n\t\tRAY_FLAG_NONE,\n\t\t~0, // InstanceInclusionMask\n\t\t0, // RayContributionToHitGroupIndex\n\t\t0, // MultiplierForGeometryContributionToHitGroupIndex\n\t\t0, // miss shader index\n\t\tray,\n\t\tpayload);\n\n\treturn payload.color;\n}\n\nfloat3 TraceColorRayCone(float3 origin, float3 direction, unsigned int depth, unsigned int seed, RayCone cone)\n{\n\tif (depth >= MAX_RECURSION)\n\t{\n\t\treturn float3(0, 0, 0);\n\t}\n\n\t// Define a ray, consisting of origin, direction, and the min-max distance values\n\tRayDesc ray;\n\tray.Origin = origin;\n\tray.Direction = direction;\n\tray.TMin = 0;\n\tray.TMax = 1.0e38f;\n\n\tPathTracingHitInfoCone payload = { float3(1, 1, 1), seed, origin, depth, cone };\n\n\t// Trace the ray\n\tTraceRay(\n\t\tScene,\n\t\tRAY_FLAG_NONE,\n\t\t~0, // InstanceInclusionMask\n\t\t0, // RayContributionToHitGroupIndex\n\t\t0, // MultiplierForGeometryContributionToHitGroupIndex\n\t\t0, // miss shader index\n\t\tray,\n\t\tpayload);\n\n\treturn payload.color;\n}\n\n// NVIDIA's luminance function\ninline float luminance(float3 rgb)\n{\n    return dot(rgb, float3(0.2126f, 0.7152f, 0.0722f));\n}\n\n// NVIDIA's probability function\nfloat probabilityToSampleDiffuse(float3 difColor, float3 specColor)\n{\n\tfloat lumDiffuse = max(0.01f, luminance(difColor.rgb));\n\tfloat lumSpecular = max(0.01f, luminance(specColor.rgb));\n\treturn lumDiffuse / (lumDiffuse + lumSpecular);\n}\n\nstatic RayCone null_cone = { 0, 0 };\n\nfloat3 ggxDirect(float3 hit_pos, float3 fN, float3 N, float3 V, float3 albedo, float metal, float roughness, unsigned int seed, unsigned int depth)\n{\n\t// #################### GGX #####################\n\tuint light_count = lights[0].tid >> 2;\n\tif (light_count < 1) return 0;\n\n\tint light_to_sample = min(int(nextRand(seed) * light_count), light_count - 1);\n\tLight light = lights[light_to_sample];\n\n\tfloat3 L = 0;\n\tfloat max_light_dist = 0;\n\tfloat3 light_intensity = 0;\n\t{\n\t\t// Calculate light properties\n\t\tuint tid = light.tid & 3;\n\n\t\t//Light direction (constant with directional, position dependent with other)\n\t\tL = (lerp(light.pos - hit_pos, light.dir, tid == light_type_directional));\n\t\tfloat light_dist = length(L);\n\t\tL /= light_dist;\n\n\t\t//Spot intensity (only used with spot; but always calculated)\n\t\tfloat min_cos = cos(light.ang);\n\t\tfloat max_cos = lerp(min_cos, 1, 0.5f);\n\t\tfloat cos_angle = dot(light.dir, L);\n\t\tfloat spot_intensity = lerp(smoothstep(min_cos, max_cos, cos_angle), 1, tid != light_type_spot);\n\n\t\t//Attenuation & spot intensity (only used with point or spot)\n\t\tfloat attenuation = lerp(1.0f - smoothstep(0, light.rad, light_dist), 1, tid == light_type_directional);\n\n\t\tlight_intensity = (light.col * spot_intensity) * attenuation;\n\t\tmax_light_dist = lerp(light_dist, 100000, tid == light_type_directional);\n\t}\n\n\tfloat3 H = normalize(V + L);\n\n\t// Shadow\n\tfloat shadow_mult = float(light_count) * GetShadowFactor(hit_pos + (L * EPSILON), L, light.light_size, max_light_dist, MAX_SHADOW_SAMPLES, depth, CALLINGPASS_PATHTRACING, seed);\n\n\t// Compute some dot products needed for shading\n\tfloat NdotV = saturate(dot(fN, V));\n\tfloat NdotL = saturate(dot(fN, L));\n\tfloat NdotH = saturate(dot(fN, H));\n\tfloat LdotH = saturate(dot(L, H));\n\n\t// D = Normal distribution (Distribution of the microfacets)\n\tfloat D = D_GGX(NdotH, max(0.05, roughness)); \n\t// G = Geometric shadowing term (Microfacets shadowing)\n\tfloat G = G_SchlicksmithGGX(NdotL, NdotV, max(0.05, roughness));\n\t// F = Fresnel factor (Reflectance depending on angle of incidence)\n\tfloat3 F = F_Schlick(LdotH, metal, albedo);\n\t//float3 F = F_ShlickSimple(metal, LdotH);\n\n\tfloat3 kS = F_SchlickRoughness(NdotV, metal, albedo, roughness);\n\tfloat3 kD = (1.f - kS) * (1.0 - metal);\n\tfloat3 spec = (D * F * G) / (4.0 * NdotV * NdotL + 0.001f);\n\n\tfloat3 lighting = (light_intensity * (NdotL * spec + NdotL * albedo / M_PI));\n\n\treturn (shadow_mult * lighting) * kD;\n}\n\nfloat3 ggxIndirect(float3 hit_pos, float3 fN, float3 N, float3 V, float3 albedo, float metal, float roughness, float ao, unsigned int seed, unsigned int depth, bool use_raycone = false, RayCone cone = null_cone)\n{\n\t// #################### GGX #####################\n\tfloat diffuse_probability = probabilityToSampleDiffuse(albedo, metal);\n\tfloat choose_diffuse = (nextRand(seed) < diffuse_probability);\n\n\t// Diffuse lobe\n\tif (choose_diffuse)\n\t{\n\t\tfloat3 color = (albedo / diffuse_probability) * ao;\n\n#ifdef RUSSIAN_ROULETTE\n\t\t// ############## RUSSIAN ROULETTE ###############\n\t\t// Russian Roulette\n\t\t// Randomly terminate a path with a probability inversely equal to the throughput\n\t\tfloat p = max(color.x, max(color.y, color.z));\n\t\t// Add the energy we 'lose' by randomly terminating paths\n\t\tcolor *= 1 / p;\n\t\tif (nextRand(seed) > p) {\n\t\t\treturn 0;\n\t\t}\n#endif\n\n\t\t// ##### BOUNCE #####\n\t\tnextRand(seed);\n\t\tconst float3 rand_dir = getCosHemisphereSample(seed, N);\n\t\tfloat3 irradiance = 0;\n\t\tif (use_raycone)\n\t\t{\n\t\t\tirradiance = TraceColorRayCone(hit_pos + (EPSILON * N), rand_dir, depth, seed, cone);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tirradiance = TraceColorRay(hit_pos + (EPSILON * N), rand_dir, depth, seed);\n\t\t}\n\n\t\tif (dot(N, rand_dir) <= 0.0f) irradiance = float3(0, 0, 0);\n\n\t\tfloat3 kS = F_SchlickRoughness(max(dot(fN, V), 0.0f), metal, albedo, roughness);\n\t\tfloat3 kD = 1.0f - kS;\n\t\tkD *= 1.0f - metal;\n\n\t\treturn kD * (irradiance * color);\n\t}\n\telse\n\t{\n\t\tnextRand(seed);\n\t\tfloat3 H = getGGXMicrofacet(seed, roughness, N);\n\n\t\t// ### BRDF ###\n\t\tfloat3 L = normalize(2.f * dot(V, H) * H - V);\n\n\t\t// Compute some dot products needed for shading\n\t\tfloat NdotV = saturate(dot(N, V));\n\t\tfloat NdotL = saturate(dot(N, L));\n\t\tfloat NdotH = saturate(dot(N, H));\n\t\tfloat LdotH = saturate(dot(L, H));\n\n\t\t// D = Normal distribution (Distribution of the microfacets)\n\t\tfloat D = D_GGX(NdotH, max(0.05, roughness)); \n\t\t// G = Geometric shadowing term (Microfacets shadowing)\n\t\tfloat G = G_SchlicksmithGGX(NdotL, NdotV, max(0.05, roughness));\n\t\t// F = Fresnel factor (Reflectance depending on angle of incidence)\n\t\tfloat3 F = F_Schlick(NdotH, metal, albedo);\n \n\t\tfloat3 spec = (D * F * G) / ((4.0 * NdotL * NdotV + 0.001f));\n\t\tfloat  ggx_probability = D * NdotH / (4 * LdotH);\n\n\t\tfloat3 color = (NdotL * spec / (ggx_probability * (1.0f - diffuse_probability)));\n\n#ifdef RUSSIAN_ROULETTE\n\t\t// ############## RUSSIAN ROULETTE ###############\n\t\t// Russian Roulette\n\t\t// Randomly terminate a path with a probability inversely equal to the throughput\n\t\tfloat p = max(color.x, max(color.y, color.z));\n\t\t// Add the energy we 'lose' by randomly terminating paths\n\t\tcolor *= 1 / p;\n\t\tif (nextRand(seed) > p) {\n\t\t\treturn 0;\n\t\t}\n#endif\n\n\t\t// #### BOUNCE ####\n\t\tfloat3 irradiance = 0;\n\t\tif (use_raycone)\n\t\t{\n\t\t\tirradiance = TraceColorRayCone(hit_pos + (EPSILON * N), L, depth, seed, cone);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tirradiance = TraceColorRay(hit_pos + (EPSILON * N), L, depth, seed);\n\t\t}\n\t\tif (dot(N, L) <= 0.0f) irradiance = float3(0, 0, 0);\n\n\t\treturn color * irradiance;\n\t}\n}\n\n#endif //__DXR_PATHTRACER_FUNCTIONS_HLSL__\n"
  },
  {
    "path": "resources/shaders/dxr_pathtracer_main.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_PATHTRACER_MAIN_HLSL__\n#define __DXR_PATHTRACER_MAIN_HLSL__\n\n#define LIGHTS_REGISTER register(t2)\n#include \"rand_util.hlsl\"\n#include \"pbr_util.hlsl\"\n#include \"material_util.hlsl\"\n#include \"lighting.hlsl\"\n#include \"dxr_texture_lod.hlsl\"\n\n// Definitions for: \n// - Vertex, Material, Offset\n// - Ray, RayCone, ReflectionHitInfo, etc\n#include \"dxr_structs.hlsl\"\n\n// Definitions for: \n// - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute\n#include \"dxr_functions.hlsl\"\n\nRWTexture2D<float4> output : register(u0); // xyz: reflection, a: shadow factor\nByteAddressBuffer g_indices : register(t1);\nStructuredBuffer<Vertex> g_vertices : register(t3);\nStructuredBuffer<Material> g_materials : register(t4);\nStructuredBuffer<Offset> g_offsets : register(t5);\n\nTexture2D g_textures[1000] : register(t10);\nTexture2D gbuffer_albedo : register(t1010);\nTexture2D gbuffer_normal : register(t1011);\nTexture2D gbuffer_emissive : register(t1012);\nTexture2D gbuffer_depth : register(t1013);\nTextureCube skybox : register(t6);\nTextureCube irradiance_map : register(t9);\nSamplerState s0 : register(s0);\n\ntypedef BuiltInTriangleIntersectionAttributes Attributes;\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat4x4 inv_view;\n\tfloat4x4 inv_projection;\n\tfloat4x4 inv_vp;\n\n\tfloat frame_idx;\n\tfloat3 padding;\n};\n\n#include \"dxr_pathtracer_functions.hlsl\"\n#include \"dxr_pathtracer_entries.hlsl\"\n\n[shader(\"raygeneration\")]\nvoid RaygenEntry()\n{\n\tuint rand_seed = initRand((DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x), frame_idx);\n\n\t// Texture UV coordinates [0, 1]\n\tfloat2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1);\n\n\t// Screen coordinates [0, resolution] (inverted y)\n\tint2 screen_co = DispatchRaysIndex().xy;\n\n\t// Get g-buffer information\n\tfloat4 albedo_roughness = gbuffer_albedo[screen_co];\n\tfloat4 normal_metallic = gbuffer_normal[screen_co];\n\tfloat4 emissive_ao = gbuffer_emissive[screen_co];\n\n\t// Unpack G-Buffer\n\tfloat depth = gbuffer_depth[screen_co].x;\n\tfloat3 albedo = albedo_roughness.rgb;\n\tfloat3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth, inv_vp);\n\tfloat3 normal = normalize(normal_metallic.xyz);\n\tfloat metallic = normal_metallic.w;\n\tfloat roughness = albedo_roughness.w;\n\tfloat3 emissive = emissive_ao.xyz;\n\tfloat ao = emissive_ao.w;\n\n\t// Do lighting\n\tfloat3 cpos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]);\n\tfloat3 V = normalize(cpos - wpos);\n\n\tfloat3 result = float3(0, 0, 0);\n\n\tSurfaceHit sfhit;\n\tsfhit.pos = wpos;\n\tsfhit.normal = normal;\n\tsfhit.dist = length(cpos - wpos);\n\tsfhit.surface_spread_angle = ComputeSurfaceSpreadAngle(gbuffer_depth, gbuffer_normal, inv_vp, wpos, normal);\n\n\t// Compute the initial ray cone from the gbuffers.\n \tRayCone cone = ComputeRayConeFromGBuffer(sfhit, 1.39626, DispatchRaysDimensions().y);\n\n\tnextRand(rand_seed);\n\tconst float3 rand_dir = getCosHemisphereSample(rand_seed, normal);\n\tconst float cos_theta = cos(dot(rand_dir, normal));\n\tresult = TraceColorRayCone(wpos + (EPSILON * normal), rand_dir, 0, rand_seed, cone);\n\n\tif (any(isnan(result)))\n\t{\n\t\tresult = 0;\n\t}\n\n\tresult = clamp(result, 0, 100);\n\t\n\toutput[DispatchRaysIndex().xy] = float4(result, 1);\n}\n\n#endif //__DXR_PATHTRACER_MAIN_HLSL__\n"
  },
  {
    "path": "resources/shaders/dxr_raytracing.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_RAYTRACING_HLSL__\n#define __DXR_RAYTRACING_HLSL__\n\n#define LIGHTS_REGISTER register(t2)\n#include \"rand_util.hlsl\"\n#include \"pbr_util.hlsl\"\n#include \"lighting.hlsl\"\n#include \"material_util.hlsl\"\n\n// Definitions for: \n// - Vertex, Material, Offset\n// - Ray, RayCone, ReflectionHitInfo\n#include \"dxr_structs.hlsl\"\n\n// Definitions for: \n// - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute\n#include \"dxr_functions.hlsl\"\n\nRWTexture2D<float4> gOutput : register(u0);\nByteAddressBuffer g_indices : register(t1);\nStructuredBuffer<Vertex> g_vertices : register(t3);\nStructuredBuffer<Material> g_materials : register(t4);\nStructuredBuffer<Offset> g_offsets : register(t5);\n\nTextureCube skybox : register(t6);\nTexture2D brdf_lut : register(t7);\nTextureCube irradiance_map : register(t8);\nTexture2D g_textures[1000] : register(t9);\nSamplerState s0 : register(s0);\nSamplerState point_sampler : register(s1);\n\ntypedef BuiltInTriangleIntersectionAttributes MyAttributes;\n\n#include \"dxr_pathtracer_functions.hlsl\"\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat4x4 view;\n\tfloat4x4 inv_projection_view;\n\tfloat3 camera_position;\n\tfloat padding;\n\n\tfloat focal_radius;\n\tfloat focal_len;\n\tfloat frame_idx;\n\tfloat intensity;\n};\n\ninline Ray GenerateCameraRay(uint2 index, in float3 cameraPosition, in float4x4 projectionToWorld, in float2 offset, unsigned int seed)\n{\n#ifdef DEPTH_OF_FIELD\n\tfloat2 pixelOff = float2(nextRand(seed), nextRand(seed));  // Random offset in pixel to reduce floating point error.\n\n\tfloat3 cameraU = float3(1, 0, 0);\n\tfloat3 cameraV = float3(0, 1, 0);\n\tfloat3 cameraW = float3(0, 0, 1);\n\n\tfloat2 xy = (index + offset + pixelOff) + 0.5f; // center in the middle of the pixel.\n\tfloat2 screenPos = xy / DispatchRaysDimensions().xy;\n\n\t// Invert Y for DirectX-style coordinates.\n\tscreenPos.y = -screenPos.y;\n\n\t// Unproject the pixel coordinate into a world positon.\n\tfloat4 world = mul(float4(screenPos, 0, 1), projectionToWorld);\n\tworld.xyz = world.x * cameraU + world.y * cameraV + cameraW;\n\tworld.xyz /= 1;\n\n\tfloat2 pixelCenter = (index + offset) / DispatchRaysDimensions().xy;            // Pixel ID -> [0..1] over screen\n\tfloat2 ndc = float2(2, -2) * pixelCenter + float2(-1, 1);             // Convert to [-1..1]\n\tfloat3 rayDir = ndc.x * cameraU + ndc.y * cameraV + cameraW;  // Ray to point on near plane\n\trayDir /= 1;\n\n\tfloat focallen = focal_len;\n\tfloat lensrad = focal_len / (2.0f * 16);\n\tfloat3 focalPt = cameraPosition + focallen * world;\n\n\tfloat2 rngLens = float2(6.2831853f * nextRand(seed), lensrad * nextRand(seed));\n\tfloat2 lensPos = float2(cos(rngLens.x) * rngLens.y, sin(rngLens.x) * rngLens.y);\n\n\t//lensPos = mul(float4(lensPos, 0, 0), projectionToWorld);\n\n\tRay ray;\n\tray.origin = cameraPosition + float3(lensPos, 0);\n\tray.direction = normalize(focalPt.xyz - ray.origin);\n#else\n\tfloat2 xy = (index + offset) + 0.5f; // center in the middle of the pixel.\n\tfloat2 screenPos = xy / DispatchRaysDimensions().xy * 2.0 - 1.0;\n\n\t// Invert Y for DirectX-style coordinates.\n\tscreenPos.y = -screenPos.y;\n\n\t// Unproject the pixel coordinate into a world positon.\n\tfloat4 world = mul(float4(screenPos, 0, 1), projectionToWorld);\n\tworld.xyz /= world.w;\n\n\tRay ray;\n\tray.origin = cameraPosition;\n\tray.direction = normalize(world.xyz - ray.origin);\n\n\treturn ray;\n#endif\n}\n\n[shader(\"raygeneration\")]\nvoid RaygenEntry()\n{\n\tuint rand_seed = initRand(DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x, frame_idx);\n\n#ifdef FOUR_X_AA\n\tRay a = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(0.5, 0), rand_seed);\n\tRay b = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(-0.5, 0), rand_seed);\n\tRay c = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(0.0, 0.5), rand_seed);\n\tRay d = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(0.0, -0.5), rand_seed);\n\n\tfloat3 result_a = TraceColorRay(a.origin, a.direction, 0, rand_seed);\n\tnextRand(rand_seed);\n\tfloat3 result_b = TraceColorRay(b.origin, b.direction, 0, rand_seed);\n\tnextRand(rand_seed);\n\tfloat3 result_c = TraceColorRay(c.origin, c.direction, 0, rand_seed);\n\tnextRand(rand_seed);\n\tfloat3 result_d = TraceColorRay(d.origin, d.direction, 0, rand_seed);\n\n\tfloat3 result = (result_a + result_b + result_c + result_d) / 4;\n#else\n\tRay ray = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(0, 0), rand_seed);\n\tfloat3 result = TraceColorRay(ray.origin, ray.direction, 0, rand_seed);\n#endif\n\n\tif (any(isnan(result)))\n\t{\n\t\tresult = float3(1000, 0, 0);\n\t}\n\n\tfloat4 prev = gOutput[DispatchRaysIndex().xy];\n\n\tfloat4 color = (frame_idx * prev + float4(result, 1)) / (frame_idx + 1); // accumulate\n\n\tgOutput[DispatchRaysIndex().xy] = color;\n}\n\n[shader(\"miss\")]\nvoid MissEntry(inout FullRTHitInfo payload)\n{\n\tpayload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0).rgb;\n}\n\n[shader(\"closesthit\")]\nvoid ClosestHitEntry(inout FullRTHitInfo payload, in MyAttributes attr)\n{\n\t// Calculate the essentials\n\tconst Offset offset = g_offsets[InstanceID()];\n\tconst Material material = g_materials[offset.material_idx];\n\tconst float3 hit_pos = HitWorldPosition();\n\tconst float index_offset = offset.idx_offset;\n\tconst float vertex_offset = offset.vertex_offset;\n\n\t// Find first index location\n\tconst uint index_size = 4;\n\tconst uint indices_per_triangle = 3;\n\tconst uint triangle_idx_stride = indices_per_triangle * index_size;\n\n\tuint base_idx = PrimitiveIndex() * triangle_idx_stride;\n\tbase_idx += index_offset * 4; // offset the start\n\n\tuint3 indices = Load3x32BitIndices(g_indices, base_idx);\n\tindices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start\n\n\t// Gather triangle vertices\n\tconst Vertex v0 = g_vertices[indices.x];\n\tconst Vertex v1 = g_vertices[indices.y];\n\tconst Vertex v2 = g_vertices[indices.z];\n\n\t// Variables\n\tconst float3 V = normalize(payload.origin - hit_pos);\n\n\t// Calculate actual \"fragment\" attributes.\n\tconst float3 frag_pos = HitAttribute(v0.pos, v1.pos, v2.pos, attr);\n\tconst float3 normal = normalize(HitAttribute(v0.normal, v1.normal, v2.normal, attr));\n\tconst float3 tangent = HitAttribute(v0.tangent, v1.tangent, v2.tangent, attr);\n\tconst float3 bitangent = HitAttribute(v0.bitangent, v1.bitangent, v2.bitangent, attr);\n\n\tfloat2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy;\n\tuv.y = 1.0f - uv.y;\n\n\tfloat mip_level = payload.depth;\n\n\tOutputMaterialData output_data = InterpretMaterialDataRT(material.data,\n\t\tg_textures[material.albedo_id],\n\t\tg_textures[material.normal_id],\n\t\tg_textures[material.roughness_id],\n\t\tg_textures[material.metalicness_id],\n\t\tg_textures[material.emissive_id],\n\t\tg_textures[material.ao_id],\n\t\tmip_level,\n\t\ts0,\n\t\tuv);\n\n\tfloat3 albedo = output_data.albedo;\n\tfloat roughness = output_data.roughness;\n\tfloat metal = output_data.metallic;\n\tfloat3 emissive = output_data.emissive;\n\tfloat ao = output_data.ao;\n\n\tfloat3 N = 0;\n\tfloat3 fN = 0;\n\tif (payload.depth > 0)\n\t{\n\t\tN = normalize(mul(ObjectToWorld3x4(), float4(normal, 0)));\n\t\tfN = N;\n\t}\n\telse\n\t{\n\t\tN = 0;\n\t\tfN = CalcPeturbedNormal(normal, output_data.normal, tangent, bitangent, V, N);\n\t}\n\n\tnextRand(payload.seed);\n\tpayload.color = ggxIndirect(hit_pos, fN, N, V, albedo, metal, roughness, ao, payload.seed, payload.depth + 1);\n\tpayload.color += ggxDirect(hit_pos, fN, N, V, albedo, metal, roughness, payload.seed, payload.depth + 1);\n\tpayload.color += emissive;\n}\n\n[shader(\"closesthit\")]\nvoid ShadowClosestHitEntry(inout ShadowHitInfo hit, MyAttributes bary)\n{\n\thit.is_hit = true;\n}\n\n[shader(\"miss\")]\nvoid ShadowMissEntry(inout ShadowHitInfo hit : SV_RayPayload)\n{\n\thit.is_hit = false;\n}\n\n#endif //__DXR_RAYTRACING_HLSL__\n"
  },
  {
    "path": "resources/shaders/dxr_reflection_entries.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_REFLECTION_ENTRIES_HLSL__\n#define __DXR_REFLECTION_ENTRIES_HLSL__\n\n#include \"rand_util.hlsl\"\n#include \"pbr_util.hlsl\"\n#include \"material_util.hlsl\"\n#include \"lighting.hlsl\"\n#include \"dxr_texture_lod.hlsl\"\n\n// Definitions for: \n// - Vertex, Material, Offset\n// - Ray, RayCone, ReflectionHitInfo\n#include \"dxr_structs.hlsl\"\n\n// Definitions for: \n// - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute\n#include \"dxr_functions.hlsl\"\n\n//Reflections\n[shader(\"closesthit\")]\nvoid ReflectionHit(inout ReflectionHitInfo payload, in Attributes attr)\n{\n\t// Calculate the essentials\n\tconst Offset offset = g_offsets[InstanceID()];\n\tconst Material material = g_materials[offset.material_idx];\n\tconst float3 hit_pos = HitWorldPosition();\n\tconst float index_offset = offset.idx_offset;\n\tconst float vertex_offset = offset.vertex_offset;\n\tconst float3x4 model_matrix = ObjectToWorld3x4();\n\n\t// Find first index location\n\tconst uint index_size = 4;\n\tconst uint indices_per_triangle = 3;\n\tconst uint triangle_idx_stride = indices_per_triangle * index_size;\n\n\tuint base_idx = PrimitiveIndex() * triangle_idx_stride;\n\tbase_idx += index_offset * 4; // offset the start\n\n\tuint3 indices = Load3x32BitIndices(g_indices, base_idx);\n\tindices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start\n\n\t// Gather triangle vertices\n\tconst Vertex v0 = g_vertices[indices.x];\n\tconst Vertex v1 = g_vertices[indices.y];\n\tconst Vertex v2 = g_vertices[indices.z];\n\n\t//Direction & position\n\tfloat3 V = normalize(payload.origin - hit_pos);\n\n\t//Normal mapping\n\tconst float3 frag_pos = HitAttribute(v0.pos, v1.pos, v2.pos, attr);\n\tfloat3 normal = normalize(HitAttribute(v0.normal, v1.normal, v2.normal, attr));\n\tfloat3 tangent = HitAttribute(v0.tangent, v1.tangent, v2.tangent, attr);\n\tfloat3 bitangent = HitAttribute(v0.bitangent, v1.bitangent, v2.bitangent, attr);\n\n\t//Get data from VBO\n\tfloat2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy;\n\tuv.y = 1.0f - uv.y;\n\n\t// Propogate the ray cone\n\tpayload.cone = Propagate(payload.cone, 0, length(payload.origin - hit_pos));\n\n\t// Calculate the texture LOD level \n\t// float mip_level = ComputeTextureLOD(\n\t// \tpayload.cone,\n\t// \tV,\n\t// \tnormalize(mul(model_matrix, float4(normal, 0))),\n\t// \tmul(model_matrix, float4(v0.pos, 1)),\n\t// \tmul(model_matrix, float4(v1.pos, 1)),\n\t// \tmul(model_matrix, float4(v2.pos, 1)),\n\t// \tv0.uv,\n\t// \tv1.uv,\n\t// \tv2.uv,\n\t// \tg_textures[material.albedo_id]);\n\n\t//TODO: Fixme\n\tfloat mip_level = 0;\n\t\n\tOutputMaterialData output_data = InterpretMaterialDataRT(material.data,\n\t\tg_textures[material.albedo_id],\n\t\tg_textures[material.normal_id],\n\t\tg_textures[material.roughness_id],\n\t\tg_textures[material.metalicness_id],\n\t\tg_textures[material.emissive_id],\n\t\tg_textures[material.ao_id],\n\t\tmip_level,\n\t\ts0,\n\t\tuv);\n\n\tfloat3 albedo = output_data.albedo;\n\tfloat roughness = output_data.roughness;\n\tfloat metal = output_data.metallic;\n\tfloat3 emissive = output_data.emissive;\n\tfloat ao = output_data.ao;\n\n\t// Normals\n\tfloat3 fN = CalcPeturbedNormal(normal, output_data.normal, tangent, bitangent, V);\n\tfloat3 flipped_N = fN * -1;\n\t\n\tconst float3 sampled_irradiance = irradiance_map.SampleLevel(s0, flipped_N, 0).xyz;\n\n\t// TODO: reflections in reflections\n\n\tconst float3 F = F_SchlickRoughness(max(dot(fN, V), 0.0), metal, albedo, roughness);\n\tfloat3 kS = F;\n    float3 kD = 1.0 - kS;\n    kD *= 1.0 - metal;\n\n\tconst float2 sampled_brdf = brdf_lut.SampleLevel(s0, float2(max(dot(fN, V), 0.01f), roughness), 0).rg;\n\n\n\t//Reflection in reflections\n\tfloat4 dir_t = float4(0, 0, 0, 0);\n\tfloat3 reflection = DoReflection(hit_pos, V, fN, payload.seed, payload.depth + 1, roughness, metal, payload.cone, dir_t).xyz;\n\n\t//Lighting\n\t#undef SOFT_SHADOWS\n\tfloat3 lighting = shade_pixel(hit_pos, V, albedo, metal, roughness, emissive, fN, payload.seed, shadow_sample_count, payload.depth + 1, CALLINGPASS_REFLECTIONS);\n\t#define SOFT_SHADOWS\n\n\tfloat3 specular = reflection * (kS * sampled_brdf.x + sampled_brdf.y);\n\tfloat3 diffuse = albedo * sampled_irradiance;\n\tfloat3 ambient = (kD * diffuse + specular) * ao;\n\n\t// Output the final reflections here\n\tpayload.color = ambient + lighting;\n\tpayload.hit_t = RayTCurrent();\n}\n\n//Reflection skybox\n[shader(\"miss\")]\nvoid ReflectionMiss(inout ReflectionHitInfo payload)\n{\n\tpayload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0);\n\tpayload.hit_t = RayTCurrent();\n}\n\n#endif //__DXR_REFLECTION_ENTRIES_HLSL__\n"
  },
  {
    "path": "resources/shaders/dxr_reflection_functions.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_REFLECTION_FUNCTIONS_HLSL__\n#define __DXR_REFLECTION_FUNCTIONS_HLSL__\n\n#include \"dxr_global.hlsl\"\n#include \"dxr_structs.hlsl\"\n\n// Definitions for: \n// - Vertex, Material, Offset\n// - Ray, RayCone, ReflectionHitInfo\n#include \"dxr_structs.hlsl\"\n\n// Definitions for: \n// - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute\n#include \"dxr_functions.hlsl\"\n\n#include \"pbr_util.hlsl\"\n\nfloat3 TraceReflectionRay(float3 origin, float3 norm, float3 direction, uint rand_seed, uint depth, RayCone cone, inout float4 dir_t)\n{\n\n\tif (depth >= MAX_RECURSION)\n\t{\n\t\treturn skybox.SampleLevel(s0, direction, 0).rgb;\n\t}\n\n\torigin += norm * EPSILON;\n\n\tReflectionHitInfo payload = {origin, float3(0,0,1), rand_seed, depth, 0, cone};\n\n\t// Define a ray, consisting of origin, direction, and the min-max distance values\n\tRayDesc ray;\n\tray.Origin = origin;\n\tray.Direction = direction;\n\tray.TMin = 0;\n\tray.TMax = 10000.0;\n\n\tbool nan = isnan(origin.x) == true || isnan(origin.y) == true || isnan(origin.z) == true;\n\tnan = nan || isnan(direction.x) == true || isnan(direction.y) == true || isnan(direction.z) == true;\n\tif(nan)\n\t{\n\t\treturn skybox.SampleLevel(s0, direction, 0).rgb;\n\t}\n\n\t// Trace the ray\n\tTraceRay(\n\t\tScene,\n\t\tRAY_FLAG_NONE,\n\t\t//RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH\n\t\t//RAY_FLAG_CULL_BACK_FACING_TRIANGLES,\n\t\t0xFF, // InstanceInclusionMask\n\t\t0, // RayContributionToHitGroupIndex\n\t\t1, // MultiplierForGeometryContributionToHitGroupIndex\n\t\t0, // miss shader index\n\t\tray,\n\t\tpayload);\n\n\tdir_t = float4(direction, payload.hit_t);\n\treturn payload.color;\n}\n\nfloat4 DoReflection(float3 wpos, float3 V, float3 N, uint rand_seed, uint depth, float roughness, float metallic, RayCone cone, inout float4 dir_t)\n{\n\t// Calculate ray info\n\tfloat3 reflected = reflect(-V, N);\n\n\t// Shoot an importance sampled ray\n\n\t#ifndef PERFECT_MIRROR_REFLECTIONS\n\n\n\t\t// Shoot perfect mirror ray if enabled or if it's a recursion or it's almost a perfect mirror\n\n\t\tif (depth > 0 || roughness < 0.05f)\n\t\t\treturn float4(TraceReflectionRay(wpos, N, reflected, rand_seed, depth, cone, dir_t), 1);\n\n\t\t#ifdef GROUND_TRUTH_REFLECTIONS\n\n\t\t\tfloat3 reflection = float3(0.f, 0.f, 0.f);\n\t\t\tfloat weight_sum = 0.f;\n\t\t\tfloat sampled_count = 0.f;\n\t\t\tfloat pdf_weight = 0.f;\t/* Used for further weighting by spatial reconstruction */\n\t\t\tfloat3 total_hit = float3(0.f, 0.f, 0.f);\n\n\t\t\t//[unroll]\n\t\t\tfor (uint i = 0; i < max(MAX_GT_REFLECTION_SAMPLES * metallic, 2); ++i) {\n\n\t\t\t\tnextRand(rand_seed);\n\t\t\t\tfloat2 xi = hammersley2d(rand_seed, 8192);\n\t\t\t\tfloat pdf = 0.f;\n\t\t\t\tfloat3 H = importanceSamplePdf(xi, roughness, N, pdf);\n\t\t\t\tfloat3 L = reflect(-V, H);\n\n\t\t\t\tfloat NdotL = max(dot(N, L), 0.f);\n\n\t\t\t\tif (NdotL <= 0.f) {\n\t\t\t\t\tnextRand(rand_seed);\n\t\t\t\t\txi = hammersley2d(rand_seed, 8192);\n\t\t\t\t\tH = importanceSamplePdf(xi, roughness, N, pdf);\n\t\t\t\t\tL = reflect(-V, H);\n\t\t\t\t\tNdotL = max(dot(N, L), 0.f);\n\t\t\t\t}\n\n\t\t\t\tif (NdotL >= 0.f) {\n\t\t\t\t\tfloat weight = brdf_weight(V, L, N, roughness) / pdf;\n\t\t\t\t\tfloat4 ray_dir_t = float4(0.f, 0.f, 0.f, 0.f);\n\t\t\t\t\treflection += TraceReflectionRay(wpos, N, L, rand_seed, depth, cone, ray_dir_t) * weight;\n\t\t\t\t\tweight_sum += weight;\n\t\t\t\t\tpdf_weight += pdf;\n\t\t\t\t\ttotal_hit += ray_dir_t.xyz * ray_dir_t.w;\n\t\t\t\t\t++sampled_count;\n\t\t\t\t}\n\t\t\t}\n\t\t\ttotal_hit /= sampled_count;\n\t\t\tdir_t = float4(normalize(total_hit), length(total_hit));\n\t\t\treturn float4(reflection / weight_sum, pdf_weight / sampled_count);\n\n\t\t#else\n\n\t\t\t//Calculate an importance sampled ray\n\n\t\t\tnextRand(rand_seed);\n\t\t\tfloat2 xi = hammersley2d(rand_seed, 8192);\n\t\t\tfloat pdf = 0;\n\t\t\tfloat3 H = importanceSamplePdf(xi, roughness, N, pdf);\n\t\t\tfloat3 L = reflect(-V, H);\n\n\t\t\tfloat NdotL = max(dot(N, L), 0.f);\n\n\t\t\tif(NdotL <= 0.f)\n\t\t\t{\n\t\t\t\tnextRand(rand_seed);\n\t\t\t\txi = hammersley2d(rand_seed, 8192);\n\t\t\t\tH = importanceSamplePdf(xi, roughness, N, pdf);\n\t\t\t\tL = reflect(-V, H);\n\t\t\t\tNdotL = max(dot(N, L), 0.f);\n\t\t\t}\n\n\t\t\tfloat3 reflection = float3(0.f, 0.f, 0.f);\n\t\t\t\n\t\t\tif (NdotL >= 0.f)\n\t\t\t{\n\t\t\t\treflection = TraceReflectionRay(wpos, N, L, rand_seed, depth, cone, dir_t);\n\t\t\t}\n\n\t\t\t#ifndef DISABLE_SPATIAL_RECONSTRUCTION\n\t\t\treturn float4(reflection, pdf);\n\t\t\t#else\n\t\t\treturn float4(reflection, -1.f);\n\t\t\t#endif\n\n\t\t#endif\n\n\t#else\n\t\t// Shoot perfect mirror ray if enabled or if it's a recursion or it's almost a perfect mirror\n\t\treturn float4(TraceReflectionRay(wpos, N, reflected, rand_seed, depth, cone, dir_t), -1.f);\n\t#endif\n}\n#endif //__DXR_REFLECTION_FUNCTIONS_HLSL__"
  },
  {
    "path": "resources/shaders/dxr_reflection_main.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_REFLECTION_MAIN_HLSL__\n#define __DXR_REFLECTION_MAIN_HLSL__\n\n#define LIGHTS_REGISTER register(t2)\n#include \"rand_util.hlsl\"\n#include \"pbr_util.hlsl\"\n#include \"material_util.hlsl\"\n#include \"lighting.hlsl\"\n#include \"dxr_texture_lod.hlsl\"\n\n// Definitions for: \n// - Vertex, Material, Offset\n// - Ray, RayCone, ReflectionHitInfo\n#include \"dxr_structs.hlsl\"\n\n// Definitions for: \n// - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute\n#include \"dxr_functions.hlsl\"\n\nRWTexture2D<float4> output_reflection : register(u0); // rgb: reflection, a: pdf\nRWTexture2D<unorm float> output_shadow : register(u1); // r: shadow factor\nRWTexture2D<float4> output_dir_t_buffer : register(u2); // xyz: direction, w: hitT; dirT to calculate hit position\nByteAddressBuffer g_indices : register(t1);\nStructuredBuffer<Vertex> g_vertices : register(t3);\nStructuredBuffer<Material> g_materials : register(t4);\nStructuredBuffer<Offset> g_offsets : register(t5);\n\nTexture2D g_textures[1000] : register(t10);\nTexture2D gbuffer_albedo : register(t1010);\nTexture2D gbuffer_normal : register(t1011);\nTexture2D gbuffer_depth : register(t1012);\nTextureCube skybox : register(t6);\nTexture2D brdf_lut : register(t8);\nTextureCube irradiance_map : register(t9);\nSamplerState s0 : register(s0);\n\ntypedef BuiltInTriangleIntersectionAttributes Attributes;\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat4x4 inv_view;\n\tfloat4x4 inv_projection;\n\tfloat4x4 inv_vp;\n\n\tfloat frame_idx;\n\tfloat intensity;\n\tfloat epsilon;\n\tunsigned int shadow_sample_count;\n};\n\n#include \"dxr_reflection_functions.hlsl\"\n#include \"dxr_reflection_entries.hlsl\"\n#include \"dxr_shadow_entries.hlsl\"\n\n[shader(\"raygeneration\")]\nvoid ReflectionRaygenEntry()\n{\n\tuint rand_seed = initRand(DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x, frame_idx);\n\n\t// Texture UV coordinates [0, 1]\n\tfloat2 uv = float2(DispatchRaysIndex().xy + 0.5) / float2(DispatchRaysDimensions().xy);\n\n\t// Screen coordinates [0, resolution] (inverted y)\n\tint2 screen_co = DispatchRaysIndex().xy;\n\n\t// Get g-buffer information\n\tfloat4 albedo_roughness = gbuffer_albedo.SampleLevel(s0, uv, 0);\n\tfloat4 normal_metallic = gbuffer_normal.SampleLevel(s0, uv, 0);\n\n\t// Unpack G-Buffer\n\tfloat depth = gbuffer_depth.SampleLevel(s0, uv, 0).x;\n\tfloat3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth, inv_vp);\n\tfloat3 albedo = albedo_roughness.rgb;\n\tfloat roughness = albedo_roughness.w;\n\tfloat3 normal = normal_metallic.xyz;\n\tfloat metallic = normal_metallic.w;\n\n\t// Do lighting\n\tfloat3 cpos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]);\n\tfloat3 V = normalize(cpos - wpos);\n\n\tif (length(normal) == 0)\t\t//TODO: Could be optimized by only marking pixels that need lighting, but that would require execute rays indirect\n\t{\n\t\t// A value of 1 in the output buffer, means that there is shadow\n\t\t// So, the far plane pixels are set to 0\n\t\toutput_reflection[screen_co] = float4(0, 0, 0, 0);\n\t\toutput_shadow[screen_co] = 0;\n\t\toutput_dir_t_buffer[screen_co] = float4(0, 0, 0, 0);\n\t\treturn;\n\t}\n\n\tnormal = lerp(normal, -normal, dot(normal, V) < 0);\n\n\t// Describe the surface for mip level generation\n\tSurfaceHit sfhit;\n\tsfhit.pos = wpos;\n\tsfhit.normal = normal;\n\tsfhit.dist = length(cpos - wpos);\n\tsfhit.surface_spread_angle = ComputeSurfaceSpreadAngle(gbuffer_depth, gbuffer_normal, inv_vp, wpos, normal);\n\n\t// Compute the initial ray cone from the gbuffers.\n \tRayCone cone = ComputeRayConeFromGBuffer(sfhit, 1.39626, DispatchRaysDimensions().y);\n\n\t// Get reflection result\n\tfloat4 dir_t = float4(0, 0, 0, 0);\n\tfloat4 reflection_result = min(DoReflection(wpos, V, normal, rand_seed, 0, roughness, metallic, cone, dir_t), 10000);\n\n\t// xyz: reflection, a: shadow factor\n\toutput_reflection[screen_co] = reflection_result;\n\toutput_dir_t_buffer[screen_co] = dir_t;\n}\n\n#endif //__DXR_REFLECTION_MAIN_HLSL__"
  },
  {
    "path": "resources/shaders/dxr_shadow_entries.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_SHADOW_ENTRIES_HLSL__\n#define __DXR_SHADOW_ENTRIES_HLSL__\n\n#include \"pbr_util.hlsl\"\n#include \"material_util.hlsl\"\n\n// Definitions for: \n// - Vertex, Material, Offset\n// - Ray, RayCone, ReflectionHitInfo\n#include \"dxr_structs.hlsl\"\n\n[shader(\"closesthit\")]\nvoid ShadowClosestHitEntry(inout ShadowHitInfo hit, Attributes bary)\n{\n\thit.is_hit = true;\n}\n\n[shader(\"miss\")]\nvoid ShadowMissEntry(inout ShadowHitInfo hit : SV_RayPayload)\n{\n\thit.is_hit = false;\n}\n\n#endif //__DXR_SHADOW_ENTRIES_HLSL__"
  },
  {
    "path": "resources/shaders/dxr_shadow_functions.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_SHADOWS_FUNCTIONS__\n#define __DXR_SHADOWS_FUNCTIONS__\n\n#include \"dxr_global.hlsl\"\n#include \"dxr_structs.hlsl\"\n\nbool TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pass, unsigned int depth)\n{\n\tif (depth >= MAX_RECURSION)\n\t{\n\t\treturn false;\n\t}\n\n\t// Define a ray, consisting of origin, direction, and the min-max distance values\n\tRayDesc ray;\n\tray.Origin = origin;\n\tray.Direction = direction;\n\tray.TMin = 0;\n\tray.TMax = far;\n\n\tShadowHitInfo payload = { false, 0 };\n\n\tuint ray_contr_idx = 1;\n\tuint miss_idx = 1;\n\n\tif (calling_pass == CALLINGPASS_SHADOWS)\n\t{\n\t\tray_contr_idx = 0;\n\t\tmiss_idx = 0;\n\t}\n\n\t// Trace the ray\n\tTraceRay(\n\t\tScene,\n\t\tRAY_FLAG_NONE,\n\t\t~0, // InstanceInclusionMask\n\t\tray_contr_idx, // RayContributionToHitGroupIndex\n\t\t0, // MultiplierForGeometryContributionToHitGroupIndex\n\t\tmiss_idx, // miss shader index is set to idx but can probably be anything.\n\t\tray,\n\t\tpayload);\n\n\treturn payload.is_hit;\n}\n\n// Get shadow factor\nfloat GetShadowFactor(float3 wpos, float3 light_dir, float light_size, float t_max, uint sample_count, uint depth, uint calling_pass, inout uint rand_seed)\n{\n\tfloat shadow_factor = 0.0f;\n\n#ifdef SOFT_SHADOWS\n\tfor (uint i = 0; i < sample_count; ++i)\n\t{\n\t\t//float3 offset = normalize(float3(nextRand(rand_seed), nextRand(rand_seed), nextRand(rand_seed))) - 0.5;\n\t\tfloat3 dir = perturbDirectionVector(rand_seed, light_dir, light_size);\n\t\tfloat3 ray_direction = normalize(dir);\n\n\t\tbool shadow = TraceShadowRay(wpos, ray_direction, t_max, calling_pass, depth);\n\n\t\tshadow_factor += lerp(1.0, 0.0, shadow);\n\t}\n\n\tshadow_factor /= float(sample_count);\n\n#else //SOFT_SHADOWS\n\n\tbool shadow = TraceShadowRay(wpos, light_dir, t_max, calling_pass, depth);\n\n\tshadow_factor = !shadow;\n\n#endif //SOFT_SHADOWS\n\n\treturn shadow_factor;\n}\n\n#endif //__DXR_SHADOWS_FUNCTIONS__"
  },
  {
    "path": "resources/shaders/dxr_shadow_main.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_SHADOW_MAIN_HLSL__\n#define __DXR_SHADOW_MAIN_HLSL__\n\n#define LIGHTS_REGISTER register(t2)\n#include \"rand_util.hlsl\"\n#include \"pbr_util.hlsl\"\n#include \"math.hlsl\"\n#include \"material_util.hlsl\"\n#include \"lighting.hlsl\"\n#include \"dxr_texture_lod.hlsl\"\n#include \"dxr_global.hlsl\"\n\n// Definitions for: \n// - Vertex, Material, Offset\n// - Ray, RayCone, ReflectionHitInfo\n#include \"dxr_structs.hlsl\"\n\n// Definitions for: \n// - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute\n#include \"dxr_functions.hlsl\"\n\nRWTexture2D<float4> output_refl_shadow : register(u0); // xyz: reflection, a: shadow factor\nByteAddressBuffer g_indices : register(t1);\nStructuredBuffer<Vertex> g_vertices : register(t3);\nStructuredBuffer<Material> g_materials : register(t4);\nStructuredBuffer<Offset> g_offsets : register(t5);\n\nTexture2D g_textures[1000] : register(t10);\nTexture2D gbuffer_albedo : register(t1010);\nTexture2D gbuffer_normal : register(t1011);\nTexture2D gbuffer_depth : register(t1012);\nTextureCube skybox : register(t6);\nTexture2D brdf_lut : register(t8);\nTextureCube irradiance_map : register(t9);\nSamplerState s0 : register(s0);\n\ntypedef BuiltInTriangleIntersectionAttributes Attributes;\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat4x4 inv_view;\n\tfloat4x4 inv_projection;\n\tfloat4x4 inv_vp;\n\n\tfloat frame_idx;\n\tfloat intensity;\n\tfloat epsilon;\n\tunsigned int shadow_sample_count;\n};\n\n#include \"dxr_shadow_functions.hlsl\"\n#include \"dxr_shadow_entries.hlsl\"\n\n[shader(\"raygeneration\")]\nvoid ShadowRaygenEntry()\n{\n\tuint rand_seed = initRand(DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x, frame_idx);\n\n\t// Texture UV coordinates [0, 1]\n\tfloat2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1);\n\n\t// Screen coordinates [0, resolution] (inverted y)\n\tint2 screen_co = DispatchRaysIndex().xy;\n\n\t// Get g-buffer information\n\tfloat4 albedo_roughness = gbuffer_albedo[screen_co];\n\tfloat4 normal_metallic = gbuffer_normal[screen_co];\n\n\t// Unpack G-Buffer\n\tfloat depth = gbuffer_depth[screen_co].x;\n\tfloat3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth, inv_vp);\n\tfloat3 normal = normal_metallic.xyz;\n\n\t// Do lighting\n\tfloat3 cpos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]);\n\tfloat3 V = normalize(cpos - wpos);\n\n\tif (length(normal) == 0)\t\t//TODO: Could be optimized by only marking pixels that need lighting, but that would require execute rays indirect\n\t{\n\t\t// A value of 1 in the output buffer, means that there is shadow\n\t\t// So, the far plane pixels are set to 0\n\t\toutput_refl_shadow[screen_co] = float4(1, 1, 1, 1);\n\t\treturn;\n\t}\n\n\twpos += normal * epsilon;\n\t// Get shadow factor\n\tfloat4 shadow_result = DoShadowAllLights(wpos, V, normal, normal_metallic.w, albedo_roughness.w, albedo_roughness.xyz, shadow_sample_count, 0, CALLINGPASS_SHADOWS, rand_seed);\n\n\t// xyz: reflection, a: shadow factor\n\toutput_refl_shadow[screen_co] = shadow_result;\n}\n\n#endif //__DXR_SHADOW_MAIN_HLSL__"
  },
  {
    "path": "resources/shaders/dxr_structs.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_STRUCTS_HLSL__\n#define __DXR_STRUCTS_HLSL__\n\nstruct Vertex\n{\n\tfloat3 pos;\n\tfloat2 uv;\n\tfloat3 normal;\n\tfloat3 tangent;\n\tfloat3 bitangent;\n};\n\nstruct MaterialData\n{\n\tfloat3 color;\n\tfloat metallic;\n\n\tfloat roughness;\n\tfloat emissive_multiplier;\n\tfloat is_double_sided;\n\tfloat use_alpha_masking;\n\n\tfloat albedo_uv_scale;\n\tfloat normal_uv_scale;\n\tfloat roughness_uv_scale;\n\tfloat metallic_uv_scale;\n\n\tfloat emissive_uv_scale;\n\tfloat ao_uv_scale;\n\tfloat padding;\n\tuint flags;\n};\n\nstruct Material\n{\n\tuint albedo_id;\n\tuint normal_id;\n\tuint roughness_id;\n\tuint metalicness_id;\n\tuint emissive_id;\n\tuint ao_id;\n\tfloat2 padding;\n\n\tMaterialData data;\n};\n\nstruct Offset\n{\n\tuint material_idx;\n\tuint idx_offset;\n\tuint vertex_offset;\n};\n\nstruct Ray\n{\n\tfloat3 origin;\n\tfloat3 direction;\n};\n\nstruct RayCone\n{\n\tfloat width;\n\tfloat spread_angle;\n};\n\nstruct ReflectionHitInfo\n{\n\tfloat3 origin;\n\tfloat3 color;\n\tunsigned int seed;\n\tunsigned int depth;\n\tfloat hit_t;\n\tRayCone cone;\n};\n\nstruct ShadowHitInfo\n{\n\tfloat is_hit;\n\tfloat thisvariablesomehowmakeshybridrenderingwork_killme;\n};\n\nstruct PathTracingHitInfo\n{\n\tfloat3 color;\n\tunsigned int seed;\n\tfloat3 origin;\n\tunsigned int depth;\n};\n\nstruct PathTracingHitInfoCone\n{\n\tfloat3 color;\n\tunsigned int seed;\n\tfloat3 origin;\n\tunsigned int depth;\n\tRayCone cone;\n};\n\nstruct FullRTHitInfo\n{\n\tfloat3 color;\n\tunsigned int seed;\n\tfloat3 origin;\n\tunsigned int depth;\n};\n\nstruct SurfaceHit\n{\n\tfloat3 pos;\n\tfloat3 normal;\n\tfloat surface_spread_angle;\n\tfloat dist;\n};\n\n\n#endif //__DXR_STRUCTS_HLSL__\n"
  },
  {
    "path": "resources/shaders/dxr_texture_lod.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __DXR_TEXTURE_LOD_HLSL__\n#define __DXR_TEXTURE_LOD_HLSL__\n\n//Definition for RayCone\n#include \"dxr_structs.hlsl\" \n\n// numbers prefixed with Cha mean its chapter x.x\n// numbers prefixed with Fig means its figure x.x\n\n// pa Fig 20.5\nfloat ComputeTriangleArea(float3 P0, float3 P1, float3 P2)\n{\n\treturn length(cross((P1 - P0), (P2 - P0)));\n}\n\n// ta Fig 20.4\nfloat ComputeTextureCoordsArea(float2 UV0, float2 UV1, float2 UV2, Texture2D T)\n{\n\tfloat w, h;\n\tT.GetDimensions(w, h);\n\n\treturn abs((UV1.x - UV0.x) * (UV2.y - UV0.y) - (UV2.x - UV0.x) * (UV1.y - UV0.y));           // Removed with and height to get mipmapping working in my code.\n\treturn w * h * abs((UV1.x - UV0.x) * (UV2.y - UV0.y) - (UV2.x - UV0.x) * (UV1.y - UV0.y));   // Proper formula from Fig 20.4\n}\n\n// Ch 20.6\nfloat GetTriangleLODConstant(float3 P0, float3 P1, float3 P2, float2 UV0, float2 UV1, float2 UV2, Texture2D T)\n{\n\tfloat P_a = ComputeTriangleArea(P0, P1, P2);\n\tfloat T_a = ComputeTextureCoordsArea(UV0, UV1, UV2, T);\n\treturn 0.5 * log2(T_a / P_a);\n}\n\n// Ch 20.6\nfloat ComputeTextureLOD(RayCone cone, float3 V, float3 N, float3 P0, float3 P1, float3 P2, float2 UV0, float2 UV1, float2 UV2, Texture2D T)\n{\n\tfloat w, h;\n\tT.GetDimensions(w, h);\n\n\tfloat lambda = GetTriangleLODConstant(P0, P1, P2, UV0, UV1, UV2, T);\n\tlambda += log2(abs(cone.width));\n\tlambda += 0.5 * log2(w * h);\n\tlambda -= log2(abs(dot(V, N)));\n\treturn lambda;\n}\n\n// Fig 20.30\nfloat PixelSpreadAngle(float vertical_fov, float output_height)\n{\n\treturn atan((2.f * tan(vertical_fov / 2.f)) / output_height);\n}\n\nfloat3 dumb_ddx(Texture2D t, float3 v)\n{\n\tfloat2 pixel = DispatchRaysIndex().xy;\n\n\tfloat3 top_left = t[float2(pixel.x, pixel.y)].xyz;\n\tfloat3 top_right = t[float2(pixel.x+1, pixel.y)].xyz;\n\tfloat3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz;\n\tfloat3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz;\n\n\tfloat3 v1 = top_right - top_left;\n\t\n\treturn v1;\n}\n\nfloat3 dumb_ddy(Texture2D t, float3 v)\n{\n\tfloat2 pixel = DispatchRaysIndex().xy;\n\n\tfloat3 top_left = t[float2(pixel.x, pixel.y)].xyz;\n\tfloat3 top_right = t[float2(pixel.x+1, pixel.y)].xyz;\n\tfloat3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz;\n\tfloat3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz;\n\n\tfloat3 v2 = bottom_left - top_left;\n\t\n\treturn v2;\n}\n\nfloat3 dumb_ddx_depth(Texture2D t, float4x4 inv_vp, float3 v)\n{\n\tfloat2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1);\n\tuv.y = 1.f - uv.y;\n\n\tfloat2 pixel = DispatchRaysIndex().xy;\n\n\tfloat3 top_left = t[float2(pixel.x, pixel.y)].xyz;\n\tfloat3 top_right = t[float2(pixel.x+1, pixel.y)].xyz;\n\tfloat3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz;\n\tfloat3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz;\n\n\t// Get world space position\n\tconst float4 ndc = float4(uv * 2.0 - 1.0, top_left.x, 1.0);\n\tfloat4 wpos = mul(inv_vp, ndc);\n\tfloat3 retval = (wpos.xyz / wpos.w).xyz;\n\n\tconst float4 _ndc = float4(uv * 2.0 - 1.0, top_right.x, 1.0);\n\tfloat4 _wpos = mul(inv_vp, _ndc);\n\tfloat3 _retval = (_wpos.xyz / _wpos.w).xyz;\n\n\treturn _retval - retval;\n}\n\nfloat3 dumb_ddy_depth(Texture2D t, float4x4 inv_vp, float3 v)\n{\n\tfloat2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1);\n\tuv.y = 1.f - uv.y;\n\n\tfloat2 pixel = DispatchRaysIndex().xy;\n\n\tfloat3 top_left = t[float2(pixel.x, pixel.y)].xyz;\n\tfloat3 top_right = t[float2(pixel.x+1, pixel.y)].xyz;\n\tfloat3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz;\n\tfloat3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz;\n\n\t// Get world space position\n\tconst float4 ndc = float4(uv * 2.0 - 1.0, bottom_left.x, 1.0);\n\tfloat4 wpos = mul(inv_vp, ndc);\n\tfloat3 retval = (wpos.xyz / wpos.w).xyz;\n\n\tconst float4 _ndc = float4(uv * 2.0 - 1.0, top_right.x, 1.0);\n\tfloat4 _wpos = mul(inv_vp, _ndc);\n\tfloat3 _retval = (_wpos.xyz / _wpos.w).xyz;\n\n\treturn retval - _retval;\n}\n\nfloat3 dumb_ddx_depth2(Texture2D t, float4x4 inv_vp, float3 v)\n{\n\tfloat2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1);\n\tuv.y = 1.f - uv.y;\n\n\tfloat2 pixel = DispatchRaysIndex().xy;\n\n\tfloat3 top_left = t[float2(pixel.x, pixel.y)].xyz;\n\tfloat3 top_right = t[float2(pixel.x+1, pixel.y)].xyz;\n\tfloat3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz;\n\tfloat3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz;\n\n\tfloat3 v1 = top_right - top_left;\n\n\t// Get world space position\n\tconst float4 ndc = float4(uv * 2.0 - 1.0, v1.x, 1.0);\n\tfloat4 wpos = mul(inv_vp, ndc);\n\tfloat3 retval = (wpos.xyz / wpos.w).xyz;\n\n\treturn retval;\n}\n\nfloat3 dumb_ddy_depth2(Texture2D t, float4x4 inv_vp, float3 v)\n{\n\tfloat2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1);\n\tuv.y = 1.f - uv.y;\n\n\tfloat2 pixel = DispatchRaysIndex().xy;\n\n\tfloat3 top_left = t[float2(pixel.x, pixel.y)].xyz;\n\tfloat3 top_right = t[float2(pixel.x+1, pixel.y)].xyz;\n\tfloat3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz;\n\tfloat3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz;\n\n\tfloat3 v2 = bottom_left - top_right;\n\n\t// Get world space position\n\tconst float4 ndc = float4(uv * 2.0 - 1.0, v2.x, 1.0);\n\tfloat4 wpos = mul(inv_vp, ndc);\n\tfloat3 retval = (wpos.xyz / wpos.w).xyz;\n\n\treturn retval;\n}\n\n// Fig 20.23\nfloat ComputeSurfaceSpreadAngle(Texture2D g_P, Texture2D g_N, /*Texture2D g_DY, Texture2D g_DY2,*/ float4x4 inv_vp, float3 P, float3 N)\n{\n\tfloat2 pixel = DispatchRaysIndex().xy;\n\n\tfloat3 aPx = dumb_ddx_depth2(g_P, inv_vp, P);\n\tfloat3 aPy = dumb_ddy_depth2(g_P, inv_vp, P);\n\n\t//float3 aPx = dumb_ddx(g_P, P);\n\t//float3 aPy = dumb_ddy(g_P, P);\n\n\tfloat3 aNx = dumb_ddx(g_N, N);\n\tfloat3 aNy = dumb_ddy(g_N, N);\n\n\tfloat k1 = 1;\n\tfloat k2 = 0;\n\t\n\tfloat s = sign(dot(aPx, aNx) + dot(aPy, aNy));\n\t// s = g_DY2[pixel].x; // Sign calculated in the deferred main shader.\n\t// s = 1 // Sets the sign to convex only. can fix a lot of issues.\n\n\treturn 2.f * k1 * s * sqrt(dot(aNx, aNx) + dot(aNy, aNy)) + k2;\n}\n\n// Ch 20.6\nRayCone Propagate(RayCone cone, float surface_spread_angle, float hit_dist)\n{\n\tRayCone new_cone;\n \tnew_cone.width = cone.spread_angle * hit_dist + cone.width;\n\tnew_cone.spread_angle = cone.spread_angle + surface_spread_angle;\n\n\treturn new_cone;\n}\n\n// Ch 20.6\nRayCone ComputeRayConeFromGBuffer(SurfaceHit hit, float vertical_fov, float height)\n{\n\tRayCone cone;\n\tcone.width = 0;\n\tcone.spread_angle = PixelSpreadAngle(vertical_fov, height);\n\n\treturn Propagate(cone, hit.surface_spread_angle, hit.dist);\n}\n\n#endif //__DXR_TEXTURE_LOD_HLSL__"
  },
  {
    "path": "resources/shaders/fullscreen_quad.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __FULLSCREEN_QUAD_HLSL__\n#define __FULLSCREEN_QUAD_HLSL__\n\nstruct VS_OUTPUT\n{\n\tfloat4 pos : SV_POSITION;\n\tfloat2 uv : TEXCOORD;\n};\n\nVS_OUTPUT main_vs(float2 pos : POSITION)\n{\n\tVS_OUTPUT output;\n\toutput.pos = float4(pos.x, pos.y, 0.0f, 1.0f);\n\toutput.uv = 0.5 * (pos.xy + float2(1.0, 1.0));\n    return output;\n}\n\n#endif //__FULLSCREEN_QUAD_HLSL__"
  },
  {
    "path": "resources/shaders/generate_mips_cs.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n/**\n* Compute shader to generate mipmaps for a given texture.\n* Source: https://github.com/Microsoft/DirectX-Graphics-Samples/blob/master/MiniEngine/Core/Shaders/GenerateMipsCS.hlsli\n*/\n\n#define BLOCK_SIZE 8\n\n// When reducing the size of a texture, it could be that downscaling the texture \n// will result in a less than exactly 50% (1/2) of the original texture size.\n// This happens if either the width, or the height (or both) dimensions of the texture\n// are odd. For example, downscaling a 5x3 texture will result in a 2x1 texture which\n// has a 60% reduction in the texture width and 66% reduction in the height.\n// When this happens, we need to take more samples from the source texture to \n// determine the pixel value in the destination texture.\n\n#define WIDTH_HEIGHT_EVEN 0     // Both the width and the height of the texture are even.\n#define WIDTH_ODD_HEIGHT_EVEN 1 // The texture width is odd and the height is even.\n#define WIDTH_EVEN_HEIGHT_ODD 2 // The texture width is even and teh height is odd.\n#define WIDTH_HEIGHT_ODD 3      // Both the width and height of the texture are odd.\n\nstruct ComputeShaderInput\n{\n\tuint3 GroupID           : SV_GroupID;           // 3D index of the thread group in the dispatch.\n\tuint3 GroupThreadID     : SV_GroupThreadID;     // 3D index of local thread ID in a thread group.\n\tuint3 DispatchThreadID  : SV_DispatchThreadID;  // 3D index of global thread ID in the dispatch.\n\tuint  GroupIndex        : SV_GroupIndex;        // Flattened local index of the thread within a thread group.\n};\n\ncbuffer GenerateMipsCB : register(b0)\n{\n\tuint SrcMipLevel;\t// Texture level of source mip\n\tuint NumMipLevels;\t// Number of OutMips to write: [1-4]\n\tuint SrcDimension;  // Width and height of the source texture are even or odd.\n\tuint Padding;       // Pad to 16 byte alignment.\n\tfloat2 TexelSize;\t// 1.0 / OutMip1.Dimensions\n}\n\n// Source mip map.\nTexture2D<float4> SrcMip : register(t0);\n\n// Write up to 4 mip map levels.\nRWTexture2D<float4> OutMip1 : register(u0);\nRWTexture2D<float4> OutMip2 : register(u1);\nRWTexture2D<float4> OutMip3 : register(u2);\nRWTexture2D<float4> OutMip4 : register(u3);\n\n// Linear clamp sampler.\nSamplerState LinearClampSampler : register(s0);\n\n// The reason for separating channels is to reduce bank conflicts in the\n// local data memory controller.  A large stride will cause more threads\n// to collide on the same memory bank.\ngroupshared float gs_R[64];\ngroupshared float gs_G[64];\ngroupshared float gs_B[64];\ngroupshared float gs_A[64];\n\nvoid StoreColor(uint Index, float4 Color)\n{\n\tgs_R[Index] = Color.r;\n\tgs_G[Index] = Color.g;\n\tgs_B[Index] = Color.b;\n\tgs_A[Index] = Color.a;\n}\n\nfloat4 LoadColor(uint Index)\n{\n\treturn float4(gs_R[Index], gs_G[Index], gs_B[Index], gs_A[Index]);\n}\n\nfloat3 LinearToSRGB(float3 x)\n{\n\t// This is exactly the sRGB curve\n\t//return x < 0.0031308 ? 12.92 * x : 1.055 * pow(abs(x), 1.0 / 2.4) - 0.055;\n\n\t// This is cheaper but nearly equivalent\n\treturn x < 0.0031308 ? 12.92 * x : 1.13005 * sqrt(abs(x - 0.00228)) - 0.13448 * x + 0.005719;\n}\n\nfloat4 PackColor(float4 Linear)\n{\n#if defined(CONVERT_TO_SRGB)\n\treturn float4(LinearToSRGB(Linear.rgb), Linear.a);\n#else\n\treturn Linear;\n#endif\n}\n\n[numthreads(BLOCK_SIZE, BLOCK_SIZE, 1)]\nvoid main(ComputeShaderInput IN)\n{\n\tfloat4 Src1 = (float4)0;\n\n\t// One bilinear sample is insufficient when scaling down by more than 2x.\n\t// You will slightly undersample in the case where the source dimension\n\t// is odd.  This is why it's a really good idea to only generate mips on\n\t// power-of-two sized textures.  Trying to handle the undersampling case\n\t// will force this shader to be slower and more complicated as it will\n\t// have to take more source texture samples.\n\n\t// Determine the path to use based on the dimension of the \n\t// source texture.\n\t// 0b00(0): Both width and height are even.\n\t// 0b01(1): Width is odd, height is even.\n\t// 0b10(2): Width is even, height is odd.\n\t// 0b11(3): Both width and height are odd.\n\tswitch (SrcDimension)\n\t{\n\tcase WIDTH_HEIGHT_EVEN:\n\t{\n\t\tfloat2 UV = TexelSize * (IN.DispatchThreadID.xy + 0.5);\n\n\t\tSrc1 = SrcMip.SampleLevel(LinearClampSampler, UV, SrcMipLevel);\n\t}\n\tbreak;\n\tcase WIDTH_ODD_HEIGHT_EVEN:\n\t{\n\t\t// > 2:1 in X dimension\n\t\t// Use 2 bilinear samples to guarantee we don't undersample when downsizing by more than 2x\n\t\t// horizontally.\n\t\tfloat2 UV1 = TexelSize * (IN.DispatchThreadID.xy + float2(0.25, 0.5));\n\t\tfloat2 Off = TexelSize * float2(0.5, 0.0);\n\n\t\tSrc1 = 0.5 * (SrcMip.SampleLevel(LinearClampSampler, UV1, SrcMipLevel) +\n\t\t\tSrcMip.SampleLevel(LinearClampSampler, UV1 + Off, SrcMipLevel));\n\t}\n\tbreak;\n\tcase WIDTH_EVEN_HEIGHT_ODD:\n\t{\n\t\t// > 2:1 in Y dimension\n\t\t// Use 2 bilinear samples to guarantee we don't undersample when downsizing by more than 2x\n\t\t// vertically.\n\t\tfloat2 UV1 = TexelSize * (IN.DispatchThreadID.xy + float2(0.5, 0.25));\n\t\tfloat2 Off = TexelSize * float2(0.0, 0.5);\n\n\t\tSrc1 = 0.5 * (SrcMip.SampleLevel(LinearClampSampler, UV1, SrcMipLevel) +\n\t\t\tSrcMip.SampleLevel(LinearClampSampler, UV1 + Off, SrcMipLevel));\n\t}\n\tbreak;\n\tcase WIDTH_HEIGHT_ODD:\n\t{\n\t\t// > 2:1 in in both dimensions\n\t\t// Use 4 bilinear samples to guarantee we don't undersample when downsizing by more than 2x\n\t\t// in both directions.\n\t\tfloat2 UV1 = TexelSize * (IN.DispatchThreadID.xy + float2(0.25, 0.25));\n\t\tfloat2 Off = TexelSize * 0.5;\n\n\t\tSrc1 = SrcMip.SampleLevel(LinearClampSampler, UV1, SrcMipLevel);\n\t\tSrc1 += SrcMip.SampleLevel(LinearClampSampler, UV1 + float2(Off.x, 0.0), SrcMipLevel);\n\t\tSrc1 += SrcMip.SampleLevel(LinearClampSampler, UV1 + float2(0.0, Off.y), SrcMipLevel);\n\t\tSrc1 += SrcMip.SampleLevel(LinearClampSampler, UV1 + float2(Off.x, Off.y), SrcMipLevel);\n\t\tSrc1 *= 0.25;\n\t}\n\tbreak;\n\t}\n\n\tOutMip1[IN.DispatchThreadID.xy] = PackColor(Src1);\n\n\t// A scalar (constant) branch can exit all threads coherently.\n\tif (NumMipLevels == 1)\n\t\treturn;\n\n\t// Without lane swizzle operations, the only way to share data with other\n\t// threads is through LDS.\n\tStoreColor(IN.GroupIndex, Src1);\n\n\t// This guarantees all LDS writes are complete and that all threads have\n\t// executed all instructions so far (and therefore have issued their LDS\n\t// write instructions.)\n\tGroupMemoryBarrierWithGroupSync();\n\n\t// With low three bits for X and high three bits for Y, this bit mask\n\t// (binary: 001001) checks that X and Y are even.\n\tif ((IN.GroupIndex & 0x9) == 0)\n\t{\n\t\tfloat4 Src2 = LoadColor(IN.GroupIndex + 0x01);\n\t\tfloat4 Src3 = LoadColor(IN.GroupIndex + 0x08);\n\t\tfloat4 Src4 = LoadColor(IN.GroupIndex + 0x09);\n\t\tSrc1 = 0.25 * (Src1 + Src2 + Src3 + Src4);\n\n\t\tOutMip2[IN.DispatchThreadID.xy / 2] = PackColor(Src1);\n\t\tStoreColor(IN.GroupIndex, Src1);\n\t}\n\n\tif (NumMipLevels == 2)\n\t\treturn;\n\n\tGroupMemoryBarrierWithGroupSync();\n\n\t// This bit mask (binary: 011011) checks that X and Y are multiples of four.\n\tif ((IN.GroupIndex & 0x1B) == 0)\n\t{\n\t\tfloat4 Src2 = LoadColor(IN.GroupIndex + 0x02);\n\t\tfloat4 Src3 = LoadColor(IN.GroupIndex + 0x10);\n\t\tfloat4 Src4 = LoadColor(IN.GroupIndex + 0x12);\n\t\tSrc1 = 0.25 * (Src1 + Src2 + Src3 + Src4);\n\n\t\tOutMip3[IN.DispatchThreadID.xy / 4] = PackColor(Src1);\n\t\tStoreColor(IN.GroupIndex, Src1);\n\t}\n\n\tif (NumMipLevels == 3)\n\t\treturn;\n\n\tGroupMemoryBarrierWithGroupSync();\n\n\t// This bit mask would be 111111 (X & Y multiples of 8), but only one\n\t// thread fits that criteria.\n\tif (IN.GroupIndex == 0)\n\t{\n\t\tfloat4 Src2 = LoadColor(IN.GroupIndex + 0x04);\n\t\tfloat4 Src3 = LoadColor(IN.GroupIndex + 0x20);\n\t\tfloat4 Src4 = LoadColor(IN.GroupIndex + 0x24);\n\t\tSrc1 = 0.25 * (Src1 + Src2 + Src3 + Src4);\n\n\t\tOutMip4[IN.DispatchThreadID.xy / 8] = PackColor(Src1);\n\t}\n}"
  },
  {
    "path": "resources/shaders/lighting.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __LIGHTING_HLSL__\n#define __LIGHTING_HLSL__\n\n#define CALLINGPASS_SHADOWS 0\n#define CALLINGPASS_REFLECTIONS 1\n#define CALLINGPASS_PATHTRACING 2\n#define CALLINGPASS_FULLRAYTRACING 3\n\n#include \"dxr_shadow_functions.hlsl\"\n\nstruct Light\n{\n\tfloat3 pos;\t\t\t//Position in world space for spot & point\n\tfloat rad;\t\t\t//Radius for point, height for spot\n\n\tfloat3 col;\t\t\t//Color\n\tuint tid;\t\t\t//Type id; light_type_x\n\n\tfloat3 dir;\t\t\t//Direction for spot & directional\n\tfloat ang;\t\t\t//Angle for spot; in radians\n\n\tfloat3 padding;\n\tfloat light_size;\n};\n\nStructuredBuffer<Light> lights : LIGHTS_REGISTER;\n\nstatic uint light_type_point = 0;\nstatic uint light_type_directional = 1;\nstatic uint light_type_spot = 2;\n\nfloat calc_attenuation(Light light, float3 L, float light_dist)\n{\n\tuint tid = light.tid & 3;\n\n\tfloat min_cos = cos(light.ang);\n\tfloat max_cos = lerp(min_cos, 1, 0.5f);\n\tfloat cos_angle = dot(light.dir, L);\n\treturn lerp(smoothstep(min_cos, max_cos, cos_angle), 1.0f - smoothstep(0, light.rad, light_dist), tid != light_type_spot);\n}\n\n//Copied version for testing stuff\nfloat3 shade_light(float3 pos, float3 V, float3 albedo, float3 normal, float metallic, float roughness, Light light)\n{\n\tuint tid = light.tid & 3;\n\n\t//Light direction (constant with directional, position dependent with other)\n\tfloat3 L = (lerp(light.pos - pos, light.dir, tid == light_type_directional));\n\tfloat light_dist = length(L);\n\tL /= light_dist;\n\n\tfloat attenuation = calc_attenuation(light, L, light_dist);\n\n\tfloat3 radiance = light.col * attenuation;\n\n\tfloat3 lighting = BRDF(L, V, normal, metallic, roughness, albedo, radiance);\n\n\treturn lighting;\n}\n\nfloat3 shade_pixel(float3 pos, float3 V, float3 albedo, float metallic, float roughness, float3 emissive, float3 normal, float3 irradiance, float ao, float3 reflection, float2 brdf, float3 shadow_factor, bool uses_luminance)\n{\n\tfloat3 res = float3(0.0f, 0.0f, 0.0f);\n\n\tuint light_count = lights[0].tid >> 2;\t//Light count is stored in 30 upper-bits of first light\n\n\tif(!uses_luminance)\n\t{\n\t\tfor (uint i = 0; i < light_count; i++)\n\t\t{\n\t\t\tres += shade_light(pos, V, albedo, normal, metallic, roughness, lights[i]);\n\t\t}\n\t}\n\telse\n\t{\n\t\tres = albedo * shadow_factor;\n\t}\n\n\t// Ambient Lighting using Irradiance for Diffuse\n\tfloat3 kS = F_SchlickRoughness(max(dot(normal, V), 0.0f), metallic, albedo, roughness);\n\tfloat3 kD = 1.0f - kS;\n\tkD *= 1.0f - metallic;\n\n\tfloat3 diffuse = irradiance * albedo;\n\n\t// Image-Based Lighting using Prefiltered Environment Map and BRDF LUT for Specular\n\tfloat3 prefiltered_color = reflection;\n\tfloat2 sampled_brdf = brdf;\n\t\n\tfloat3 specular = prefiltered_color * (kS * sampled_brdf.x + sampled_brdf.y);\n\t//float3 specular = reflection * kS;\n\t\n\tfloat3 ambient = (kD * diffuse + specular) * ao;\n\n\treturn ambient + res + emissive;\n}\n\nfloat3 shade_light(float3 pos, float3 V, float3 albedo, float3 normal, float metallic, float roughness, Light light, inout uint rand_seed, uint shadow_sample_count, uint depth, uint calling_pass)\n{\n\tuint tid = light.tid & 3;\n\n\t//Light direction (constant with directional, position dependent with other)\n\tfloat3 L = (lerp(light.pos - pos, light.dir, tid == light_type_directional));\n\tfloat light_dist = length(L);\n\tL /= light_dist;\n\n\tfloat attenuation = calc_attenuation(light, L, light_dist);\n\n\t// Maybe change hard-coded 100000 to be dynamic according to the scene size?\n\tfloat t_max = lerp(light_dist, 100000, tid == light_type_directional);\n\n\tfloat3 radiance = light.col * attenuation;\n\n\tfloat3 lighting = BRDF(L, V, normal, metallic, roughness, albedo, radiance);\n\n\tfloat3 wpos = pos + (normal * EPSILON);\n\t\n\tfloat shadow_factor = GetShadowFactor(wpos, L, light.light_size, t_max, shadow_sample_count, depth, calling_pass, rand_seed);\n\n\tlighting *= shadow_factor;\n\n\treturn lighting;\n}\n\nfloat3 shade_pixel(float3 pos, float3 V, float3 albedo, float metallic, float roughness, float3 emissive, float3 normal, inout uint rand_seed, uint shadow_sample_count, uint depth, uint calling_pass)\n{\n\tuint light_count = lights[0].tid >> 2;\t//Light count is stored in 30 upper-bits of first light\n\n\tfloat3 res = float3(0, 0, 0);\n\n\t[unroll]\n\tfor (uint i = 0; i < light_count; i++)\n\t{\n\t\tres += shade_light(pos, V, albedo, normal, metallic, roughness, lights[i], rand_seed, shadow_sample_count, depth, calling_pass);\n\t}\n\n\treturn res + emissive;\n}\n\nfloat4 DoShadowAllLights(float3 wpos, float3 V, float3 normal, float metallic, float roughness, float3 albedo, uint shadow_sample_count, uint depth, uint calling_pass, inout float rand_seed)\n{\n\tuint light_count = lights[0].tid >> 2;\t//Light count is stored in 30 upper-bits of first light\n\n\tfloat4 res = float4(0.0, 0.0, 0.0, 0.0);\n\tuint sampled_lights = 0;\n\n\tfor (uint i = 0; i < light_count; i++)\n\t{\n\t\t// Get light and light type\n\t\tLight light = lights[i];\n\t\tuint tid = light.tid & 3;\n\n\t\t//Light direction (constant with directional, position dependent with other)\n\t\tfloat3 L = (lerp(light.pos - wpos, light.dir, tid == light_type_directional));\n\t\tfloat light_dist = length(L);\n\t\tL /= light_dist;\n\n\t\tfloat dir_dot = dot(L, normal);\n\n\t\tif (dir_dot < 0.0f)\n\t\t{\n\t\t\tcontinue;\n\t\t}\n\n\t\tfloat attenuation = calc_attenuation(light, L, light_dist);\n\n\t\tif (light_dist > light.rad && tid != light_type_directional)\n\t\t{\n\t\t\tcontinue;\n\t\t}\n\n\t\tfloat3 radiance = attenuation * light.col;\n\n\t\tfloat3 lighting = BRDF(L, V, normal, metallic, roughness, float3(1.0, 1.0, 1.0), radiance) /* / max(albedo, float3(0.001, 0.001, 0.001)) */;\n\n\t\t// Get maxium ray length (depending on type)\n\t\tfloat t_max = lerp(light_dist, 100000, tid == light_type_directional);\n\n\t\t// Add shadow factor to final result\n\t\tfloat shadow = GetShadowFactor(wpos, L, light.light_size, t_max, shadow_sample_count, depth, calling_pass, rand_seed);\n\n\t\tres.w += shadow;\n\n\t\tres.rgb += lighting * shadow;\n\n\t\tsampled_lights++;\n\t}\n\n\t// return final res\n\tres.w = res.w / float(sampled_lights);\n\n\treturn res;\n}\n\n#endif //__LIGHTING_HLSL__\n"
  },
  {
    "path": "resources/shaders/material_util.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __MATERIAL_UTIL_HLSL__\n#define __MATERIAL_UTIL_HLSL__\n\n#define MATERIAL_HAS_ALBEDO_TEXTURE 1<<0\n#define MATERIAL_HAS_NORMAL_TEXTURE 1<<1\n#define MATERIAL_HAS_ROUGHNESS_TEXTURE 1<<2\n#define MATERIAL_HAS_METALLIC_TEXTURE 1<<3\n#define MATERIAL_HAS_EMISSIVE_TEXTURE 1<<4\n#define MATERIAL_HAS_AO_TEXTURE 1<<5\n\n#include \"dxr_structs.hlsl\"\n\nstruct OutputMaterialData\n{\n\tfloat3 albedo;\n\tfloat alpha;\n\tfloat roughness;\n\tfloat3 normal;\n\tfloat metallic;\n\tfloat3 emissive;\n\tfloat ao;\n};\n\nOutputMaterialData InterpretMaterialData(MaterialData data,\n\tTexture2D material_albedo,\n\tTexture2D material_normal,\n\tTexture2D material_roughness,\n\tTexture2D material_metallic,\n\tTexture2D material_emissive,\n\tTexture2D material_ambient_occlusion,\n\tSamplerState s0,\n\tfloat2 uv\n)\n{\n\tOutputMaterialData output;\n\n\tfloat use_albedo_texture = float((data.flags & MATERIAL_HAS_ALBEDO_TEXTURE) != 0);\n\tfloat use_roughness_texture = float((data.flags & MATERIAL_HAS_ROUGHNESS_TEXTURE) != 0);\n\tfloat use_metallic_texture = float((data.flags & MATERIAL_HAS_METALLIC_TEXTURE) != 0);\n\tfloat use_normal_texture = float((data.flags & MATERIAL_HAS_NORMAL_TEXTURE) != 0);\n\tfloat use_emissive_texture = float((data.flags & MATERIAL_HAS_EMISSIVE_TEXTURE) != 0);\n\tfloat use_ao_texture = float((data.flags & MATERIAL_HAS_AO_TEXTURE) != 0);\n\n\tfloat4 albedo = lerp(float4(data.color, 1), material_albedo.Sample(s0, uv * data.albedo_uv_scale), use_albedo_texture);\n#ifdef COMPRESSED\n\tfloat roughness = lerp(data.roughness, max(0.05f, material_roughness.Sample(s0, uv * data.roughness_uv_scale).y), use_roughness_texture);\n\tfloat metallic = lerp(data.metallic, material_metallic.Sample(s0, uv * data.metallic_uv_scale).z, use_metallic_texture);\n#else\n\tfloat roughness = lerp(data.roughness, max(0.05f, material_roughness.Sample(s0, uv * data.roughness_uv_scale).x), use_roughness_texture);\n\tfloat metallic = lerp(data.metallic, material_metallic.Sample(s0, uv * data.metallic_uv_scale).x, use_metallic_texture);\n#endif\n\n\tfloat3 tex_normal = lerp(float3(0.0f, 0.0f, 1.0f), material_normal.Sample(s0, uv * data.normal_uv_scale).rgb * 2.0f - float3(1.0f, 1.0f, 1.0f), use_normal_texture);\n\tfloat3 emissive = lerp(float3(0.0f, 0.0f, 0.0f), material_emissive.Sample(s0, uv * data.emissive_uv_scale).xyz, use_emissive_texture);\n\tfloat ao = lerp(1.0f, material_ambient_occlusion.Sample(s0, uv * data.ao_uv_scale).x, use_ao_texture);\n\n\toutput.albedo = pow(albedo.xyz, 2.2f);\n\toutput.alpha = albedo.w;\n\toutput.roughness = roughness;\n\toutput.normal = tex_normal;\n\toutput.metallic = metallic;\n\toutput.emissive = pow(emissive,2.2f) * data.emissive_multiplier;\n\toutput.ao = ao;\n\n\treturn output;\n}\n\nOutputMaterialData InterpretMaterialDataRT(MaterialData data,\n\tTexture2D material_albedo,\n\tTexture2D material_normal,\n\tTexture2D material_roughness,\n\tTexture2D material_metallic,\n\tTexture2D material_emissive,\n\tTexture2D material_ambient_occlusion,\n\tfloat mip_level,\n\tSamplerState s0,\n\tfloat2 uv)\n{\n\tOutputMaterialData output;\n\n\tfloat use_albedo_texture = float((data.flags & MATERIAL_HAS_ALBEDO_TEXTURE) != 0);\n\tfloat use_roughness_texture = float((data.flags & MATERIAL_HAS_ROUGHNESS_TEXTURE) != 0);\n\tfloat use_metallic_texture = float((data.flags & MATERIAL_HAS_METALLIC_TEXTURE) != 0);\n\tfloat use_normal_texture = float((data.flags & MATERIAL_HAS_NORMAL_TEXTURE) != 0);\n\tfloat use_emissive_texture = float((data.flags & MATERIAL_HAS_EMISSIVE_TEXTURE) != 0);\n\tfloat use_ao_texture = float((data.flags & MATERIAL_HAS_AO_TEXTURE) != 0);\n\n\tconst float4 albedo = lerp(float4(data.color, 1),\n\t\tmaterial_albedo.SampleLevel(s0, uv * (data.albedo_uv_scale), mip_level),\n\t\tuse_albedo_texture);\n\n\t#ifdef COMPRESSED\n\tconst float roughness = lerp(data.roughness, max(0.05, material_roughness.SampleLevel(s0, uv * data.roughness_uv_scale, mip_level).z), use_roughness_texture);\n\tconst float metallic = lerp(data.metallic, material_metallic.SampleLevel(s0, uv * data.metallic_uv_scale, mip_level).y, use_metallic_texture); \n\t#else\n\tconst float roughness = lerp(data.roughness, max(0.05, material_roughness.SampleLevel(s0, uv * data.roughness_uv_scale, mip_level).x), use_roughness_texture);\n\tconst float metallic = lerp(data.metallic, material_metallic.SampleLevel(s0, uv * data.metallic_uv_scale, mip_level).x, use_metallic_texture); \n\t#endif\n\t\n\tconst float3 normal_t = lerp(float3(0.0, 0.0, 1.0),\n\t\tmaterial_normal.SampleLevel(s0, uv * data.normal_uv_scale, mip_level).xyz * 2 - 1,\n\t\tuse_normal_texture);\n\n\tfloat3 emissive = lerp(float3(0.0f, 0.0f, 0.0f), \n\t\tmaterial_emissive.SampleLevel(s0, uv * data.emissive_uv_scale, mip_level).xyz, use_emissive_texture);\n\n\tfloat ao = lerp(1.0f, \n\t\tmaterial_ambient_occlusion.SampleLevel(s0, uv * data.ao_uv_scale, mip_level).x, use_ao_texture);\n\n\toutput.albedo = pow(albedo.xyz, 2.2f);\n\toutput.alpha = albedo.w;\n\toutput.roughness = roughness;\n\toutput.normal = normal_t;\n\toutput.metallic = metallic;\n\toutput.emissive = pow(emissive, 2.2f) * data.emissive_multiplier;\n\toutput.ao = ao;\n\n\treturn output;\n}\n\n#endif //__MATERIAL_UTIL_HLSL__"
  },
  {
    "path": "resources/shaders/math.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __MATH_HLSL__\n#define __MATH_HLSL__\n\n#define M_PI 3.14159265358979\n\nfloat linterp(float t) {\n\treturn saturate(1.0 - abs(2.0 * t - 1.0));\n}\n\nfloat remap(float t, float a, float b) \n{\n\treturn saturate((t - a) / (b - a));\n}\n\nfloat4 spectrum_offset(float t) \n{\n\tfloat4 ret;\n\tfloat lo = step(t, 0.5);\n\tfloat hi = 1.0 - lo;\n\tfloat w = linterp(remap(t, 1.0 / 6.0, 5.0 / 6.0));\n\tret = float4(lo, 1.0, hi, 1.) * float4(1.0 - w, w, 1.0 - w, 1.);\n\n\treturn pow(ret, float4(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2));\n}\n\n#endif //__MATH_HLSL__"
  },
  {
    "path": "resources/shaders/pbr_brdf_lut.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"rand_util.hlsl\"\n#include \"pbr_util.hlsl\"\n\nRWTexture2D<float2> output : register(u0);\n\nfloat2 IntegrateBRDF(float NdotV, float roughness)\n{\n\tfloat3 V;\n\tV.x = sqrt(1.0f - NdotV * NdotV);\n\tV.y = 0.0f;\n\tV.z = NdotV;\n\n\tfloat A = 0.0f;\n\tfloat B = 0.0f;\n\n\tfloat3 N = float3(0.0f, 0.0f, 1.0f);\n\n\tconst uint SAMPLE_COUNT = 1024u;\n\n\tfor (uint i = 0; i < SAMPLE_COUNT; ++i)\n\t{\n\t\tfloat2 Xi = hammersley2d(i, SAMPLE_COUNT);\n\t\tfloat3 H = importanceSample_GGX(Xi, roughness, N);\n\t\tfloat3 L = normalize(2.0f * dot(V, H) * H - V);\n\n\t\tfloat NdotL = max(L.z, 0.0f);\n\t\tfloat NdotH = max(H.z, 0.0f);\n\t\tfloat VdotH = max(dot(V, H), 0.0f);\n\t\tfloat NdotV = max(dot(N, V), 0.0f);\n\n\t\tif (NdotL > 0.0f)\n\t\t{\n\t\t\tfloat G = GeometrySmith_IBL(NdotV, NdotL, roughness);\n\t\t\tfloat G_Vis = (G * VdotH) / (NdotH * NdotV);\n\t\t\tfloat Fc = pow(1.0f - VdotH, 5.0f);\n\n\t\t\tA += (1.0f - Fc) * G_Vis;\n\t\t\tB += Fc * G_Vis;\n\t\t}\n\t}\n\n\tA /= float(SAMPLE_COUNT);\n\tB /= float(SAMPLE_COUNT);\n\n\treturn float2(A, B);\n}\n\n[numthreads(16, 16, 1)]\nvoid main_cs(uint3 dt_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 screen_coord = int2(dt_id.x, dt_id.y) + 0.5f;\n\tfloat2 uv = screen_coord / screen_size;\n\n\toutput[dt_id.xy] = IntegrateBRDF(uv.x, uv.y);\n}\n"
  },
  {
    "path": "resources/shaders/pbr_cubemap_conversion.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nstruct VS_INPUT\n{\n\tfloat3 pos : POSITION;\n\t//float2 uv : TEXCOORD;\n\t//float3 normal : NORMAL;\n\t//float3 tangent : TANGENT;\n\t//float3 bitangent : BITANGENT;\n};\n\nstruct VS_OUTPUT\n{\n\tfloat4 pos : SV_POSITION;\n\tfloat3 local_pos : LOCPOS;\n};\n\ncbuffer PassIndex : register (b0)\n{\n\tint idx;\n}\n\ncbuffer CameraProperties : register(b1)\n{\n\tfloat4x4 projection;\n\tfloat4x4 view[6];\n};\n\nVS_OUTPUT main_vs(VS_INPUT input)\n{\n\tVS_OUTPUT output;\n\n\toutput.local_pos = input.pos.xyz;\n\n\tfloat4x4 vp = mul(projection, view[idx]);\n\toutput.pos =  mul(vp, float4(output.local_pos, 1.0f));\n\n\treturn output;\n}\n\nstruct PS_OUTPUT\n{\n\tfloat4 color;\n};\n\nTexture2D equirectangular_texture : register(t0);\nSamplerState s0 : register(s0);\n\nfloat2 SampleSphericalMap(float3 v)\n{\n\tfloat2 inv_atan = float2(0.1591f, 0.3183f);\n\n\tfloat2 uv = float2(atan2(v.z, v.x), asin(v.y));\n\tuv *= inv_atan;\n\tuv += 0.5f;\n\n\treturn uv;\n}\n\nPS_OUTPUT main_ps(VS_OUTPUT input) : SV_TARGET\n{\n\tPS_OUTPUT output;\n\t\n\tfloat2 uv = SampleSphericalMap(normalize(input.local_pos));\n\n\tfloat3 color = equirectangular_texture.Sample(s0, uv).rgb;\n\n\toutput.color = float4(color, 1.0f);\n\n\treturn output;\n}\n"
  },
  {
    "path": "resources/shaders/pbr_cubemap_convolution.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nstruct VS_INPUT\n{\n\tfloat3 pos : POSITION;\n\tfloat2 uv : TEXCOORD;\n\tfloat3 normal : NORMAL;\n\tfloat3 tangent : TANGENT;\n\tfloat3 bitangent : BITANGENT;\n};\n\nstruct VS_OUTPUT\n{\n\tfloat4 pos : SV_POSITION;\n\tfloat3 local_pos : LOCPOS;\n};\n\ncbuffer PassIndex : register (b0)\n{\n\tint idx;\n}\n\ncbuffer CameraProperties : register(b1)\n{\n\tfloat4x4 projection;\n\tfloat4x4 view[6];\n};\n\nVS_OUTPUT main_vs(VS_INPUT input)\n{\n\tVS_OUTPUT output;\n\n\toutput.local_pos = input.pos.xyz;\n\n\tfloat4x4 vp = mul(projection, view[idx]);\n\toutput.pos =  mul(vp, float4(output.local_pos, 1.0f));\n\n\treturn output;\n}\n\nstruct PS_OUTPUT\n{\n\tfloat4 color;\n};\n\nTextureCube environment_cubemap : register(t0);\nSamplerState s0 : register(s0);\n\nPS_OUTPUT main_ps(VS_OUTPUT input) : SV_TARGET\n{\n\tPS_OUTPUT output;\n\n\tconst float PI = 3.14159265359f;\n\n\tfloat3 normal = normalize(input.local_pos);\n\tfloat3 irradiance = float3(0.0f, 0.0f, 0.0f);\n\n\tfloat3 up = float3(0.0f, 1.0f, 0.0f);\n\tfloat3 right = cross(up, normal);\n\tup = cross(normal, right);\n\n\tfloat sample_delta = 0.025f;\n\tfloat nr_samples = 0.0f;\n\n\tfor (float phi = 0.0f; phi < 2.0f * PI; phi += sample_delta)\n\t{\n\t\tfor (float theta = 0.0f; theta < 0.5f * PI; theta += sample_delta)\n\t\t{\n\t\t\tfloat cos_theta = cos(theta);\n\t\t\tfloat sin_theta = sin(theta);\n\n\t\t\tfloat3 tangent_sample = float3(sin_theta * cos(phi), sin_theta * sin(phi), cos_theta);\n\t\t\tfloat3 sample_vec = tangent_sample.x * right + tangent_sample.y * up + tangent_sample.z * normal;\n\n\t\t\tirradiance += environment_cubemap.Sample(s0, sample_vec).rgb * cos_theta * sin_theta;\n\n\t\t\tnr_samples++;\n\t\t}\n\t}\n\n\tirradiance = PI * irradiance * (1.0f / float(nr_samples));\n\t\n\toutput.color = float4(irradiance.rgb, 1.0f);\n\n\treturn output;\n}\n"
  },
  {
    "path": "resources/shaders/pbr_prefilter_env_map.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"rand_util.hlsl\"\n#include \"pbr_util.hlsl\"\n\nTextureCube<float4> src_texture : register(t0);\nRWTexture2D<float4> dst_texture : register(u0);\nSamplerState s0 : register(s0);\n\ncbuffer CB : register(b0)\n{\n\tfloat2 texture_size;\n\tfloat2 skybox_res;\n\tfloat  roughness;\n\tuint   cubemap_face;\n}\n\n[numthreads(8, 8, 1)]\nvoid main_cs(uint3 dt_id : SV_DispatchThreadID)\n{\n\tfloat2 position = float2(dt_id.xy + 0.5f) / texture_size;\n\tposition.y = 1.0f - position.y;\n\tposition = (position - 0.5f) * 2.0f;\n\n\tfloat3 direction = float3(0.0f, 0.0f, 0.0f);\n\tfloat3 up = float3(0.0f, 0.0f, 0.0f);\n\n\tswitch (cubemap_face)\n\t{\n\t\tcase 0:  direction = float3(1.0f, position.y, -position.x);  up = float3(0.0f, 1.0f, 0.0f); break;\t// +X\n\t\tcase 1:  direction = float3(-1.0f, position.y, position.x);  up = float3(0.0f, 1.0f, 0.0f); break;\t// -X\n\t\tcase 2:  direction = float3(position.x, 1.0f, -position.y);  up = float3(0.0f, 0.0f, -1.0f); break;\t// +Y\n\t\tcase 3:  direction = float3(position.x, -1.0f, position.y);  up = float3(0.0f, 0.0f, 1.0f); break;\t// -Y\n\t\tcase 4:  direction = float3(position.x, position.y, 1.0f);   up = float3(0.0f, 1.0f, 0.0f); break;\t// +Z\n\t\tcase 5:  direction = float3(-position.x, position.y, -1.0f); up = float3(0.0f, 1.0f, 0.0f); break;\t// -Z\n\t}\n\n\tfloat3 N = normalize(direction);\n\tfloat3 right = normalize(cross(up, N));\n\tup = cross(N, right);\n\n\tfloat3 R = N;\n\tfloat3 V = R;\n\n\tconst uint SAMPLE_COUNT = 1024u;\n\tfloat total_weight = 0.0f;\n\tfloat3 prefiltered_color = float3(0.0f, 0.0f, 0.0f);\n\n\tfor (uint i = 0u; i < SAMPLE_COUNT; i++)\n\t{\n\t\tfloat2 Xi = hammersley2d(i, SAMPLE_COUNT);\n\t\tfloat3 H = importanceSample_GGX(Xi, roughness, N);\n\t\tfloat3 L = normalize(2.0f * dot(V, H) * H - V);\n\n\t\tfloat NdotL = max(dot(N, L), 0.0f);\n\t\tif (NdotL > 0.0f)\n\t\t{\n\t\t\tfloat NdotH = max(dot(N, H), 0.0f);\n\t\t\tfloat HdotV = max(dot(H, V), 0.0f);\n\n\t\t\tfloat D = D_GGX(NdotH, roughness);\n\n\t\t\tfloat pdf = D * NdotH / (4.0f * HdotV) + 0.0001f;\n\n\t\t\tfloat sa_texel = 4.0f * M_PI / (6.0f * skybox_res.x * skybox_res.y);\n\t\t\tfloat sa_sample = 1.0f / (float(SAMPLE_COUNT) * pdf + 0.0001f);\n\n\t\t\tfloat mip_level = roughness == 0.0f ? 0.0f : 0.5f * log2(sa_sample / sa_texel);\n\n\t\t\tprefiltered_color += src_texture.SampleLevel(s0, L, mip_level).rgb * NdotL;\n\t\t\ttotal_weight += NdotL;\n\t\t}\n\t}\n\n\tprefiltered_color = prefiltered_color / total_weight;\n\n\t//Write the final color into the destination texture.\n\tdst_texture[dt_id.xy] = float4(prefiltered_color, 1.0f);\n}\n"
  },
  {
    "path": "resources/shaders/pbr_util.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PBR_UTILS_HLSL__\n#define __PBR_UTILS_HLSL__\n\n#include \"math.hlsl\"\n\n#include \"rand_util.hlsl\"\n \n// Based omn http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/\nfloat random(float2 co)\n{\n\tfloat a = 12.9898;\n\tfloat b = 78.233;\n\tfloat c = 43758.5453;\n\tfloat dt = dot(co.xy, float2(a, b));\n\tfloat sn = fmod(dt, 3.14);\n\treturn frac(sin(sn) * c);\n}\n \n// Radical inverse based on http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html\nfloat2 hammersley2d(uint i, uint num)\n{\n\tuint bits = (i << 16u) | (i >> 16u);\n\tbits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);\n\tbits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);\n\tbits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);\n\tbits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);\n\tfloat rdi = float(bits) * 2.3283064365386963e-10;\n\treturn float2(float(i) / float(num), rdi);\n}\n \n// Based on http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_slides.pdf\nfloat3 importanceSample_GGX(float2 Xi, float roughness, float3 normal)\n{\n\t// Maps a 2D point to a hemisphere with spread based on roughness\n\tfloat alpha = roughness * roughness;\n\t//float phi = 2.f * M_PI * Xi.x + random(normal.xz) * 0.1;\n\tfloat phi = 2.f * M_PI * Xi.x;\n\tfloat cosTheta = sqrt((1.f - Xi.y) / (1.f + (alpha*alpha - 1.f) * Xi.y));\n\tfloat sinTheta = sqrt(1.f - cosTheta * cosTheta);\n\t\n\tfloat3 H;\n\tH.x = sinTheta * cos(phi);\n\tH.y = sinTheta * sin(phi);\n\tH.z = cosTheta;\n \n\t// Tangent space\n\tfloat3 up = abs(normal.z) < 0.999 ? float3(0.f, 0.f, 1.f) : float3(1.f, 0.f, 0.f);\n\tfloat3 tangentX = normalize(cross(up, normal));\n\tfloat3 tangentY = cross(normal, tangentX);\n \n\t// Convert to world Space\n\treturn normalize(tangentX * H.x + tangentY * H.y + normal * H.z);\n}\n \n// Normal distribution\nfloat D_GGX(float dotNH, float roughness)\n{\n\tfloat alpha = roughness * roughness;\n\tfloat alpha2 = alpha * alpha;\n\tfloat denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;\n\treturn (alpha2)/(M_PI * denom * denom);\n}\n\n// Get a GGX half vector / microfacet normal, sampled according to the distribution computed by\n//     the function ggxNormalDistribution() above.  \n//\n// When using this function to sample, the probability density is pdf = D * NdotH / (4 * HdotV)\nfloat3 getGGXMicrofacet(inout uint randSeed, float roughness, float3 hitNorm)\n{\n\t// Get our uniform random numbers\n\tfloat2 randVal = float2(nextRand(randSeed), nextRand(randSeed));\n\n\t// Get an orthonormal basis from the normal\n\tfloat3 B = getPerpendicularVector(hitNorm);\n\tfloat3 T = cross(B, hitNorm);\n\n\t// GGX NDF sampling\n\tfloat a2 = roughness * roughness;\n\tfloat cosThetaH = sqrt(max(0.0f, (1.0 - randVal.x) / ((a2 - 1.0) * randVal.x + 1)));\n\tfloat sinThetaH = sqrt(max(0.0f, 1.0f - cosThetaH * cosThetaH));\n\tfloat phiH = randVal.y * M_PI * 2.0f;\n\n\t// Get our GGX NDF sample (i.e., the half vector)\n\treturn T * (sinThetaH * cos(phiH)) + B * (sinThetaH * sin(phiH)) + hitNorm * cosThetaH;\n}\n \n// Geometric Shadowing function\nfloat G_SchlicksmithGGX(float NdotL, float NdotV, float roughness)\n{\n\tfloat r = (roughness + 1.0f);\n\tfloat k = (r*r) / 8.0f;\n\tfloat GL = NdotL / (NdotL * (1.0f - k) + k);\n\tfloat GV = NdotV / (NdotV * (1.0f - k) + k);\n\treturn GL * GV;\n}\n\nfloat GeometrySchlickGGX_IBL(float NdotV, float roughness_squared)\n{\n\t// Different k for IBL\n\tfloat k = roughness_squared / 2.0;\n\n\tfloat nom = NdotV;\n\tfloat denom = NdotV * (1.0 - k) + k;\n\n\treturn nom / denom;\n}\n// ----------------------------------------------------------------------------\nfloat GeometrySmith_IBL(float NdotV, float NdotL, float roughness)\n{\n\tfloat roughness_squared = roughness * roughness;\n\n\tfloat ggx2 = GeometrySchlickGGX_IBL(NdotV, roughness_squared);\n\tfloat ggx1 = GeometrySchlickGGX_IBL(NdotL, roughness_squared);\n\n\treturn ggx1 * ggx2;\n}\n\n // Fresnel function\nfloat3 F_Schlick(float cos_theta, float metallic, float3 material_color)\n{\n\tfloat3 F0 = lerp(float3(0.04, 0.04, 0.04), material_color, metallic); // * material.specular\n\tfloat3 F = F0 + (1.0 - F0) * pow(1.0 - cos_theta, 5.0); \n\treturn F;\n}\n \nfloat3 F_SchlickRoughness(float cos_theta, float metallic, float3 material_color, float roughness)\n{\n\tfloat3 F0 = lerp(float3(0.04f, 0.04f, 0.04f), material_color, metallic); // * material.specular\n\tfloat3 F = F0 + (max(float3(1.0f - roughness, 1.0f - roughness, 1.0f - roughness), F0) - F0) * pow(1.0f - cos_theta, 5.0f);\n\treturn F;\n}\n \nfloat3 BRDF(float3 L, float3 V, float3 N, float metallic, float roughness, float3 albedo, float3 radiance)\n{\n\t// Precalculate vectors and dot products\t\n\tfloat3 H = normalize(V + L);\n\tfloat dotNV = clamp(dot(N, V), 0.0, 1.0);\n\tfloat dotNL = clamp(dot(N, L), 0.0, 1.0);\n\tfloat dotNH = clamp(dot(N, H), 0.0, 1.0);\n \n\t// Light color fixed\n\tfloat3 color = float3(0.0, 0.0, 0.0);\n \n\tif (dotNL > 0.0)\n\t{\n\t\tfloat rroughness = max(0.05f, roughness);\n\t\t// D = Normal distribution (Distribution of the microfacets)\n\t\tfloat D = D_GGX(dotNH, roughness); \n\t\t// G = Geometric shadowing term (Microfacets shadowing)\n\t\tfloat G = G_SchlicksmithGGX(dotNL, dotNV, roughness);\n\t\t// F = Fresnel factor (Reflectance depending on angle of incidence)\n\t\tfloat3 F = F_Schlick(dotNH, metallic, albedo);\n \n\t\tfloat3 spec = (D * F * G) / ((4.0 * dotNL * dotNV + 0.001f));\n \n\t\tfloat3 kD = (float3(1, 1, 1) - F) * (1.0 - metallic);\n \n\t\tcolor += (kD * albedo / M_PI + spec) * radiance * dotNL;\n\t}\n \n\treturn color;\n}\n \nstatic const float2 inv_atan = float2(0.1591f, 0.3183f);\nfloat2 SampleSphericalMap(float3 v)\n{\n\tfloat2 uv = float2(atan2(v.z, v.x), asin(v.y * -1.f));\n\tuv *= inv_atan;\n\tuv += 0.5f;\n\treturn uv;\n}\n\n// Brian Karis, Epic Games \"Real Shading in Unreal Engine 4\"\n// Modified version to do pdf and tangent to world conversions\nfloat3 importanceSamplePdf(float2 xi, float a, float3 N, inout float pdf) {\n\tfloat m = a * a;\n\tfloat m2 = m * m;\n\n\tfloat phi = 2 * M_PI * xi.x;\n\tfloat cosTheta = sqrt((1.0 - xi.y) / (1.0 + (m2 - 1.0) * xi.y));\n\tfloat sinTheta = sqrt(max(1e-5, 1.0 - cosTheta * cosTheta));\n\n\tfloat3 H;\n\tH.x = sinTheta * cos(phi);\n\tH.y = sinTheta * sin(phi);\n\tH.z = cosTheta;\n\n\tfloat d = (cosTheta * m2 - cosTheta) * cosTheta + 1;\n\tfloat D = m2 / (M_PI * d * d);\n\tpdf = D * cosTheta;\n\n\tfloat3 up = lerp(float3(1.0, 0.0, 0.0), float3(0.0, 0.0, 1.0), float(abs(N.z) < 0.999));\n\tfloat3 T = normalize(cross(up, N));\n\tfloat3 B = cross(N, T);\n\n\treturn normalize(T * H.x + B * H.y + N * H.z);\n}\n\n//Get weight from roughness, view direction, light direction and normal (view space)\nfloat brdf_weight(float3 V, float3 L, float3 N, float roughness) {\n\tfloat3 H = normalize(V + L);\n\n\tfloat NdotH = saturate(dot(N, H));\n\tfloat NdotL = saturate(dot(N, L));\n\tfloat NdotV = saturate(dot(N, V));\n\n\tfloat G = G_SchlicksmithGGX(NdotL, NdotV, roughness);\t\t//This causes issues\n\tfloat D = D_GGX(NdotH, roughness);\n\n\tfloat weight = G * D * M_PI / 4;\n\n\treturn max(weight, 1e-5);\t\t//Perfect mirrors can have weights too\n}\n\n#endif"
  },
  {
    "path": "resources/shaders/pp_bloom_blur.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_BLOOM_BLUR_HLSL__\n#define __PP_BLOOM_BLUR_HLSL__\n\n#include \"pp_dof_util.hlsl\"\n#include \"pp_bloom_util.hlsl\"\n\nTexture2D source : register(t0);\nRWTexture2D<float4> output : register(u0);\nSamplerState s0 : register(s0);\n\ncbuffer BloomDirection : register(b0)\n{\n\tint blur_direction;\n\tfloat sigma_amt;\n};\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f;\n\tfloat2 texel_size = 1.0f / screen_size;\n\n\tfloat2 uv = (screen_coord) / screen_size;\n\n\tfloat sigma = sigma_amt - 1.0f;\n\n\tfloat2 blur_dir = float2(0.0f, 1.0f);\n\n\tif (blur_direction == 1)\n\t{\n\t\tblur_dir = float2(1.0f, 0.0f);\n\t}\n\n\tfloat4 color = 0;\n\tfloat weightSum = 0.0f;\n\tfor (int i = -7; i < 7; i++)\n\t{\n\t\tfloat weight = CalcGaussianWeight(i, sigma);\n\t\tweightSum += weight;\n\t\tfloat2 o_uv = saturate((screen_coord + (blur_dir * i)) / screen_size);\n\t\tfloat4 s = source.SampleLevel(s0, o_uv, 0);\n\t\tcolor += s * weight;\n\t}\n\n\toutput[int2(dispatch_thread_id.xy)] = color;\n}\n\n#endif //__PP_BLOOM_BLUR_HLSL__"
  },
  {
    "path": "resources/shaders/pp_bloom_blur_horizontal.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_BLOOM_BLUR_HORIZONTAL_HLSL__\n#define __PP_BLOOM_BLUR_HORIZONTAL_HLSL__\n\n#include \"pp_dof_util.hlsl\"\n#include \"pp_bloom_util.hlsl\"\n\nTexture2D source : register(t0);\nRWTexture2D<float4> output : register(u0);\nRWTexture2D<float4> output_qes : register(u1);\nSamplerState s0 : register(s0);\n\ncbuffer BloomDirection : register(b0)\n{\n\tfloat2 blur_direction;\n\tfloat _pad;\n\tfloat sigma;\n};\n\nfloat4 GetBlurFactor(float2 screen_coord, float res_scale)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 blur_dir = float2(1.0f, 0.0f);\n\tfloat r_sigma = 3.0f;\n\tfloat4 color = 0;\n\tfloat weightSum = 0.0f;\n\tfor (int i = -7; i < 7; i++)\n\t{\n\t\tfloat weight = CalcGaussianWeight(i, r_sigma);\n\t\tweightSum += weight;\n\t\tfloat2 o_uv = saturate((screen_coord + (blur_dir * i)) / (screen_size / res_scale));\n\t\tfloat4 s = source.SampleLevel(s0, o_uv, 0);\n\t\tcolor += s * weight;\n\t}\n\n\treturn color;\n}\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f;\n\n\tfloat4 color = 0;\n\n\tif (screen_coord.x > screen_size.x)\n\t{\n\t\tif (screen_coord.x > (screen_size.x * 1.875f))\n\t\t{\n\t\t\tcolor = GetBlurFactor(screen_coord - screen_size * 1.875f, 16.0f);\n\t\t}\n\t\tif (screen_coord.x > (screen_size.x * 1.75))\n\t\t{\n\t\t\tcolor = GetBlurFactor(screen_coord - screen_size * 1.75f, 8.0f);\n\t\t}\n\t\telse if (screen_coord.x > (screen_size.x * 1.5))\n\t\t{\n\t\t\tcolor = GetBlurFactor(screen_coord - screen_size * 1.5f, 4.0f);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcolor = GetBlurFactor(screen_coord - screen_size, 2.0f);\n\t\t}\n\n\t\toutput_qes[screen_coord - int2(screen_size)] = color;\n\t}\n\telse\n\t{\n\t\tcolor = GetBlurFactor(screen_coord, 1.0f);\n\t\toutput[screen_coord] = color;\n\t}\n}\n\n\n#endif //__PP_BLOOM_BLUR_HORIZONTAL_HLSL__"
  },
  {
    "path": "resources/shaders/pp_bloom_blur_vertical.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_BLOOM_BLUR_VERTICAL_HLSL__\n#define __PP_BLOOM_BLUR_VERTICAL_HLSL__\n\n#include \"pp_dof_util.hlsl\"\n#include \"pp_bloom_util.hlsl\"\n\nTexture2D source : register(t0);\nTexture2D source_qes : register(t1);\nRWTexture2D<float4> output : register(u0);\nRWTexture2D<float4> output_qes : register(u1);\nSamplerState s0 : register(s0);\n\ncbuffer BloomDirection : register(b0)\n{\n\tfloat2 blur_direction;\n\tfloat _pad;\n\tfloat sigma;\n};\n\n\nfloat4 GetBlurFactor(float2 screen_coord, float res_scale)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 blur_dir = float2(0.0f, 1.0f);\n\tfloat r_sigma = 3.0f;\n\tfloat4 color = 0;\n\tfloat weightSum = 0.0f;\n\tfor (int i = -7; i < 7; i++)\n\t{\n\t\tfloat weight = CalcGaussianWeight(i, r_sigma);\n\t\tweightSum += weight;\n\t\tfloat2 o_uv = saturate((screen_coord + (blur_dir * i)) / (screen_size / res_scale));\n\t\tfloat4 s = source.SampleLevel(s0, o_uv, 0);\n\t\tcolor += s * weight;\n\t}\n\n\treturn color;\n}\n\nfloat4 GetBlurFactorQES(float2 screen_coord)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 blur_dir = float2(0.0f, 1.0f);\n\tfloat r_sigma = 3.0f;\n\tfloat4 color = 0;\n\tfloat weightSum = 0.0f;\n\tfor (int i = -7; i < 7; i++)\n\t{\n\t\tfloat weight = CalcGaussianWeight(i, r_sigma);\n\t\tweightSum += weight;\n\t\tfloat4 s = source_qes[screen_coord + blur_dir * i].rgba;\n\t\tcolor += s * weight;\n\t}\n\n\treturn color;\n}\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f;\n\n\tfloat4 color = 0;\n\n\tif (screen_coord.x > screen_size.x)\n\t{\n\n\t\tcolor = GetBlurFactorQES(screen_coord - screen_size);\n\t\toutput_qes[screen_coord - int2(screen_size)] = color;\n\t}\n\telse\n\t{\n\t\tcolor = GetBlurFactor(screen_coord, 1.0f);\n\t\toutput[screen_coord] = color;\n\t}\n}\n\n#endif //__PP_BLOOM_BLUR_VERTICAL_HLSL__"
  },
  {
    "path": "resources/shaders/pp_bloom_composition.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_BLOOM_COMPOSITION_HLSL__\n#define __PP_BLOOM_COMPOSITION_HLSL__\n\n#include \"pp_hdr_util.hlsl\"\n\nTexture2D source_main : register(t0);\nTexture2D source_bloom_half : register(t1);\nTexture2D source_bloom_qes : register(t2);\nRWTexture2D<float4> output : register(u0);\nSamplerState linear_sampler : register(s0);\nSamplerState point_sampler : register(s1);\n\ncbuffer Bloomproperties : register(b0)\n{\n\tint enable_bloom;\n};\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f;\n\tfloat2 texel_size = 1.0f / screen_size;\n\n\tfloat2 uv = screen_coord / screen_size;\n\tfloat2 uv_half = (screen_coord / 2 ) / screen_size;\n\tfloat2 uv_quarter = (screen_coord / 4) / screen_size + 0.5f;\n\tfloat2 uv_eighth = (screen_coord / 8) / screen_size + 0.75f;\n\n\tfloat3 finalcolor = float3(0, 0, 0);\n\tfloat bloom_intensity = 1.f;\n\n\tif (enable_bloom > 0)\n\t{\n\t\tfinalcolor += source_bloom_half.SampleLevel(linear_sampler, uv, 0).rgb;\n\t\tfinalcolor += source_bloom_qes.SampleLevel(linear_sampler, uv_half, 0).rgb;\n\t\tfinalcolor += source_bloom_qes.SampleLevel(linear_sampler, uv_quarter, 0).rgb;\n\t\tfinalcolor += source_bloom_qes.SampleLevel(linear_sampler, uv_eighth, 0).rgb;\n\t\t\n\t\tfinalcolor *= 0.25f;\n\t}\n\n\tfinalcolor += source_main.SampleLevel(point_sampler, uv, 0).rgb;\n\n\toutput[int2(dispatch_thread_id.xy)] = float4(finalcolor, 1.0f);\n}\n\n#endif //__PP_BLOOM_COMPOSITION_HLSL__"
  },
  {
    "path": "resources/shaders/pp_bloom_extract_bright.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_BLOOM_EXTRACT_BRIGHT_HLSL__\n#define __PP_BLOOM_EXTRACT_BRIGHT_HLSL__\n\n#include \"pp_dof_util.hlsl\"\n\nTexture2D source : register(t0);\nTexture2D g_emissive : register(t1);\nTexture2D g_depth : register(t2);\n\nRWTexture2D<float4> output_bright : register(u0);\nSamplerState s0 : register(s0);\nSamplerState s1 : register(s1);\n\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput_bright.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f;\n\tfloat2 uv = screen_coord / screen_size;\n\n\tfloat4 out_bright = float4(0.0f, 0.0f, 0.0f, 1.0f);\n\tfloat3 final_color = source.SampleLevel(s1, uv, 0).rgb;\n\n\tfloat brightness = dot(final_color, float3(0.2126f, 0.7152f, 0.0722f));\n\n\tfor (int i = -1; i < 2; ++i)\n\t{\n\t\tuv = float2(screen_coord.x + i, screen_coord.y + i) / screen_size;\n\n\t\tif (brightness > 1.0f && g_depth.SampleLevel(s1, uv, 0).r < 1)\n\t\t{\n\t\t\tout_bright = saturate(float4(final_color, 1.0f));\n\t\t}\n\n\t\tout_bright += float4(g_emissive.SampleLevel(s0, uv, 0).rgb, 1.0f);\n\t}\n\n\tout_bright /= 3;\n\n\toutput_bright[int2(dispatch_thread_id.xy)] = out_bright;\n}\n\n#endif //__PP_BLOOM_EXTRACT_BRIGHT_HLSL__"
  },
  {
    "path": "resources/shaders/pp_bloom_util.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_BLOOM_UTIL_HLSL__\n#define __PP_BLOOM_UTIL_HLSL__\n\nstatic const float gaussian_weights[15] = { 0.023089f, 0.034587f,\t0.048689f,\t0.064408f,\t0.080066f,\t0.093531f,\t0.102673f,\t0.105915f,\t0.102673f,\t0.093531f,\t0.080066f,\t0.064408f,\t0.048689f,\t0.034587f,\t0.023089f };\n\n#endif //__PP_BLOOM_UTIL_HLSL__"
  },
  {
    "path": "resources/shaders/pp_dof_bokeh.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_DOF_BOKEH_HLSL__\n#define __PP_DOF_BOKEH_HLSL__\n\n#include \"pp_dof_properties.hlsl\"\n#include \"pp_dof_util.hlsl\"\n\nTexture2D source_near : register(t0);\nTexture2D source_far : register(t1);\nTexture2D near_mask :register(t2);\nRWTexture2D<float4> output_near : register(u0);\nRWTexture2D<float4> output_far : register(u1);\nSamplerState s0 : register(s0);\nSamplerState s1 : register(s1);\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat f_number;\n\tfloat shape_curve;\n\tfloat bokeh_poly_amount;\n\tuint num_blades;\n\tfloat m_padding;\n\tfloat2 m_bokeh_shape_modifier;\n\tint enable_dof;\n};\n\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\tsource_near.GetDimensions(screen_size.x, screen_size.y);\n\tscreen_size -= 1.0f;\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y);\n\n\tfloat2 texel_size = 1.0f / screen_size;\n\n\tfloat2 uv = screen_coord / (screen_size) ;\n\n\tconst uint NUMSAMPLES = NUMDOFSAMPLES * NUMDOFSAMPLES;\n\tconst float MAXKERNELSIZE = MAXBOKEHSIZE * 0.5f;\n\tconst float SHAPECURVE = 2.0f;\n\n\tfloat4 fgcolor = float4(0, 0, 0, 0);\n\tfloat4 bgcolor = float4(0, 0, 0, 0);\n\n\t//Kernel gather method credits to Matt Pettineo and David Neubelt.\n\tif (enable_dof > 0)\n\t{\n\t\tfloat far_coc = source_far.SampleLevel(s1, uv, 0).w;\n\t\tfloat near_coc = source_near.SampleLevel(s1, uv, 0).w;\n\t\tfloat kernel_radius = MAXKERNELSIZE * far_coc;\n\n\t\t[branch]\n\t\tif (kernel_radius > 0.5f)\n\t\t{\n\t\t\tfloat weightsum = 0.0001f;\n\t\t\t[unroll]\n\t\t\tfor (uint i = 0; i < NUMSAMPLES; ++i)\n\t\t\t{\n\t\t\t\tfloat lensX = saturate((i % NUMDOFSAMPLES) / max(NUMDOFSAMPLES - 1.0f, 1.0f));\n\t\t\t\tfloat lensY = saturate((i / NUMDOFSAMPLES) / max(NUMDOFSAMPLES - 1.0f, 1.0f));\n\t\t\t\tfloat2 kernel_offset = SquareToConcentricDiskMapping(lensX, lensY, float(num_blades), bokeh_poly_amount) * m_bokeh_shape_modifier;\n\t\t\t\tfloat4 s = source_far.SampleLevel(s0, (screen_coord + kernel_offset * kernel_radius) / screen_size, 0.0f);\n\t\t\t\tfloat samplecoc = s.w;\n\n\t\t\t\ts *= saturate(1.0f + (samplecoc - far_coc));\n\t\t\t\ts *= (1.0f - shape_curve) + pow(max(length(kernel_offset), 0.01f), SHAPECURVE) * shape_curve;\n\n\t\t\t\tbgcolor += s;\n\t\t\t}\n\n\t\t\tbgcolor /= NUMSAMPLES;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tbgcolor = source_far.SampleLevel(s0, uv, 0);\n\t\t}\n\n\t\tfloat nearMask = SampleTextureBSpline(near_mask, s0, uv).x;\n\t\tnearMask = saturate(nearMask * 1.0f);\n\t\tnear_coc = max(near_coc, nearMask);\n\t\tkernel_radius = near_coc * MAXKERNELSIZE;\n\t\t[branch]\n\t\tif (kernel_radius > 0.25f)\n\t\t{\n\t\t\tfloat weightsum = 0.0001f;\n\n\t\t\t[unroll]\n\t\t\tfor (uint i = 0; i < NUMSAMPLES; ++i)\n\t\t\t{\n\t\t\t\tfloat lensX = saturate((i % NUMDOFSAMPLES) / max(NUMDOFSAMPLES - 1.0f, 1.0f));\n\t\t\t\tfloat lensY = saturate((i / NUMDOFSAMPLES) / max(NUMDOFSAMPLES - 1.0f, 1.0f));\n\n\t\t\t\tfloat2 kernel_offset = SquareToConcentricDiskMapping(lensX, lensY, float(num_blades), bokeh_poly_amount) * m_bokeh_shape_modifier;\n\t\t\t\tfloat4 s = source_near.SampleLevel(s0, (screen_coord + kernel_offset * kernel_radius) / screen_size , 0.0f);\n\t\t\t\tfloat samplecoc = saturate(s.w * MAXKERNELSIZE);\n\n\t\t\t\tfgcolor.xyz += s.xyz * s.w;\n\n\t\t\t\tfloat samplealpha = 1.0f;\n\t\t\t\tsamplealpha *= saturate(s.w * 1.0f);\n\t\t\t\tfgcolor.w += samplealpha;\n\n\t\t\t\tweightsum += s.w;\n\t\t\t}\n\n\t\t\tfgcolor.xyz /= weightsum;\n\t\t\tfgcolor.w = saturate(fgcolor.w * (1.0f / NUMSAMPLES));\n\t\t\tfgcolor.w = max(fgcolor.w, source_near.SampleLevel(s0, uv, 0).w);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfgcolor = float4(source_near.SampleLevel(s1, uv, 0).rgb, 0.0f);\n\t\t}\n\t}\n\n\toutput_near[int2(dispatch_thread_id.xy)] = fgcolor;\n\toutput_far[int2(dispatch_thread_id.xy)] = bgcolor;\n}\n\n#endif //__PP_DOF_BOKEH_HLSL__"
  },
  {
    "path": "resources/shaders/pp_dof_bokeh_post_filter.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_DOF_BOKEH_POST_FILTER_HLSL__\n#define __PP_DOF_BOKEH_POST_FILTER_HLSL__\n\nTexture2D source_near : register(t0);\nTexture2D source_far : register(t1);\nRWTexture2D<float4> output_near : register(u0);\nRWTexture2D<float4> output_far : register(u1);\nSamplerState s0 : register(s0);\nSamplerState s1 : register(s1);\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput_near.GetDimensions(screen_size.x, screen_size.y);\n\tscreen_size -= 1.0f;\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y);\n\n\n\tfloat2 uv = screen_coord / screen_size;\n\n\tfloat nearcoc = source_near.SampleLevel(s1, uv, 0).a;\n\tfloat farcoc = source_far.SampleLevel(s1, uv, 0).a;\n\n\tstatic const int SampleRadius = 2;\n\tstatic const int SampleDiameter = SampleRadius * 2 + 1;\n\n\tfloat3 near_color = 0;\n\tfloat3 far_color = 0;\n\n\t[unroll]\n\tfor (int y = -SampleRadius; y <= SampleRadius; ++y)\n\t{\n\t\t[unroll]\n\t\tfor (int x = -SampleRadius; x <= SampleRadius; ++x)\n\t\t{\n\t\t\tnear_color += source_near.SampleLevel(s0, (screen_coord + float2(x, y)) / screen_size, 0).rgb;\n\t\t\tfar_color += source_far.SampleLevel(s0, (screen_coord + float2(x, y)) / screen_size, 0).rgb;\n\t\t}\n\t}\n\n\tnear_color /= float(SampleDiameter * SampleDiameter);\n\tfar_color /= float(SampleDiameter * SampleDiameter);\n\n\toutput_near[int2(dispatch_thread_id.xy)] = float4(near_color, nearcoc);\n\toutput_far[int2(dispatch_thread_id.xy)] = float4(far_color, farcoc);\n}\n\n#endif //__PP_DOF_BOKEH_POST_FILTER_HLSL__"
  },
  {
    "path": "resources/shaders/pp_dof_coc.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_DOF_COC_HLSL__\n#define __PP_DOF_COC_HLSL__\n\n#include \"pp_dof_properties.hlsl\"\n#include \"pp_dof_util.hlsl\"\n\nTexture2D gbuffer_depth : register(t0);\nRWTexture2D<float2> output : register(u0);\nSamplerState s0 : register(s0);\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat4x4 projection;\n\tfloat focal_length;\n\tfloat f_number;\n\tfloat film_size;\n\tfloat focus_dist;\n\tfloat2 m_pad;\n\tint enable_dof;\n\tfloat dof_range;\n};\n\nfloat GetCoC(float lineardepth, float focusdist)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\n\tfloat fstop = focal_length / f_number * 0.5f;\n\t////// Compute CoC in meters\n\tfloat coc = -fstop * (focal_length * (focusdist - lineardepth)) / (lineardepth * (focusdist - focal_length));\n\n\t//// Convert to pixels\n\tcoc = (coc / film_size) * screen_size.x;\n\n\tcoc = clamp(coc / (MAXCOCSIZE * dof_range), -1.f, 1.f);\n\treturn coc;\n}\n\n\nfloat GetAutoFocusDepth(float2 screen_dimensions)\n{\n\tfloat depth_focus = gbuffer_depth[float2(0.5f * screen_dimensions.x, 0.5f * screen_dimensions.y)].r;\n\n\tdepth_focus += gbuffer_depth[float2(0.51f * screen_dimensions.x, 0.51f * screen_dimensions.y)].r;\n\tdepth_focus += gbuffer_depth[float2(0.49f * screen_dimensions.x, 0.51f * screen_dimensions.y)].r;\n\tdepth_focus += gbuffer_depth[float2(0.51f * screen_dimensions.x, 0.49f * screen_dimensions.y)].r;\n\tdepth_focus += gbuffer_depth[float2(0.49f * screen_dimensions.x, 0.49f * screen_dimensions.y)].r;\n\n\tdepth_focus += gbuffer_depth[float2(0.52f * screen_dimensions.x, 0.52f * screen_dimensions.y)].r;\n\tdepth_focus += gbuffer_depth[float2(0.48f * screen_dimensions.x, 0.52f * screen_dimensions.y)].r;\n\tdepth_focus += gbuffer_depth[float2(0.52f * screen_dimensions.x, 0.48f * screen_dimensions.y)].r;\n\tdepth_focus += gbuffer_depth[float2(0.48f * screen_dimensions.x, 0.48f * screen_dimensions.y)].r;\n\n\tdepth_focus = (depth_focus / 9.0f);\n\n\treturn depth_focus;\n}\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\tscreen_size -= 1.0f;\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y);\n\n\tfloat2 uv = screen_coord / screen_size;\n\t\n\tfloat sample_depth = gbuffer_depth.SampleLevel(s0, uv, 0).r;\n\tfloat focus_depth = GetAutoFocusDepth(screen_size);\n\n\tsample_depth = GetLinearDepth(sample_depth) * FFAR;\n\n\tfloat coc = GetCoC(sample_depth, focus_dist);\n\n\tif (focus_dist < 1)\n\t{\n\t\tfocus_depth = GetLinearDepth(focus_depth) * FFAR;\n\t\tcoc = GetCoC(sample_depth, focus_depth);\n\t}\n\tif (enable_dof == 0)\n\t{\n\t\tcoc = 0.0f;\n\t}\n\n\tfloat2 result = float2(coc, gbuffer_depth.SampleLevel(s0, uv, 0).r);\n\toutput[int2(dispatch_thread_id.xy)] = result;\n}\n\n#endif //__PP_DOF_COC_HLSL__"
  },
  {
    "path": "resources/shaders/pp_dof_composition.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_DOF_COMPOSITION_HLSL__\n#define __PP_DOF_COMPOSITION_HLSL__\n\n#include \"pp_dof_properties.hlsl\"\n\nTexture2D source : register(t0);\nRWTexture2D<float4> output : register(u0);\nTexture2D bokeh_near : register(t1);\nTexture2D bokeh_far : register(t2);\nTexture2D coc_buffer : register(t3);\nSamplerState s0 : register(s0);\nSamplerState s1 : register(s1);\n\nfloat GetDownSampledCoC(float2 uv , float2 texel_size)\n{\n\tfloat4 offset = texel_size.xyxy * float2(-0.5f, 0.5f).xxyy;\n\tfloat coc0 = coc_buffer.SampleLevel(s1, uv + offset.xy, 0);\n\tfloat coc1 = coc_buffer.SampleLevel(s1, uv + offset.zy, 0);\n\tfloat coc2 = coc_buffer.SampleLevel(s1, uv + offset.xw, 0);\n\tfloat coc3 = coc_buffer.SampleLevel(s1, uv + offset.zw, 0);\n\n\tfloat coc_min = min(min(min(coc0, coc1), coc2), coc3);\n\tfloat coc_max = max(max(max(coc0, coc1), coc2), coc3);\n\n\tfloat coc = coc_max >= -coc_min ? coc_max : coc_min;\n\treturn coc;\n}\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput.GetDimensions(screen_size.x, screen_size.y);\n\tscreen_size -= 1.0f;\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y);\n\n\n\tfloat2 texel_size = 1.0 / screen_size;\n\tfloat2 uv = (screen_coord) / (screen_size);\n\n\tfloat coc = coc_buffer.SampleLevel(s0, uv, 0);\n\t\n\tfloat3 original_sample = source.SampleLevel(s0, uv, 0).rgb;\n\tfloat4 near_sample = bokeh_near.SampleLevel(s1, uv, 0);\n\tfloat4 far_sample = bokeh_far.SampleLevel(s1, uv, 0);\n\n\tfloat near_w = bokeh_near.SampleLevel(s0, uv, 0).a;\n\n\tfloat3 near = near_sample.rgb;\n\tfloat3 far = original_sample.rgb; \n\n\tif (far_sample.w > 0.0f)\n\t{\n\t\tfar = far_sample.rgb / far_sample.w;\n\t}\n\n\tfloat far_blend = saturate(saturate(coc) * MAXCOCSIZE - 0.5f);\n\n\tfloat3 result = lerp(original_sample, far.rgb, far_blend);\n\n\tfloat near_blend = saturate(near_sample.w * 2.0f);\n\n\tresult = lerp(result, near.rgb, smoothstep(0.0f, 1.0f, near_blend));\n\n\toutput[int2(dispatch_thread_id.xy)] = float4(result, coc);\n}\n\n#endif //__PP_DOF_COMPOSITION_HLSL__"
  },
  {
    "path": "resources/shaders/pp_dof_compute_near_mask.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n//=================================================================================================\n//\n//  Baking Lab\n//  by MJP and David Neubelt\n//  http://mynameismjp.wordpress.com/\n//\n//  All code licensed under the MIT license\n//\n//=================================================================================================\n\n#ifndef __PP_DOF_COMPUTE_NEAR_MASK_HLSL__\n#define __PP_DOF_COMPUTE_NEAR_MASK_HLSL__\n\n#include \"pp_dof_properties.hlsl\"\n#include \"pp_dof_util.hlsl\"\n\nTexture2D input : register(t0);\nRWTexture2D<float2> output : register(u0);\nSamplerState s0 : register(s0);\n\n\n//=================================================================================================\n// Constants\n//=================================================================================================\nstatic const uint NumThreads = 16 * 16;\n\n\n\n// -- shared memory\ngroupshared float Samples[NumThreads];\n\n//=================================================================================================\n// Computes a downscaled mask for the near field\n//=================================================================================================\n[numthreads(32, 32, 1)]\nvoid main_cs(in uint3 GroupID : SV_GroupID, in uint3 GroupThreadID : SV_GroupThreadID,\n\tin uint ThreadIndex : SV_GroupIndex)\n{\n\tuint2 textureSize;\n\tinput.GetDimensions(textureSize.x, textureSize.y);\n\n\tuint2 samplePos = GroupID.xy * 16 + GroupThreadID.xy;\n\tsamplePos = min(samplePos, textureSize - 1);\n\n\tfloat cocSample = input[samplePos].w;\n\n\t// -- store in shared memory\n\tSamples[ThreadIndex] = cocSample;\n\tGroupMemoryBarrierWithGroupSync();\n\n\t// -- reduce\n\t[unroll]\n\tfor (uint s = NumThreads / 2; s > 0; s >>= 1)\n\t{\n\t\tif (ThreadIndex < s)\n\t\t\tSamples[ThreadIndex] = max(Samples[ThreadIndex], Samples[ThreadIndex + s]);\n\n\t\tGroupMemoryBarrierWithGroupSync();\n\t}\n\n\tif (ThreadIndex == 0)\n\t\toutput[GroupID.xy] = Samples[0];\n}\n\n#endif //__PP_DOF_COC_HLSL__"
  },
  {
    "path": "resources/shaders/pp_dof_dilate.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_DOF_DILATE_HLSL__\n#define __PP_DOF_DILATE_HLSL__\n\n#include \"pp_dof_properties.hlsl\"\n#include \"pp_dof_util.hlsl\"\n\nTexture2D source_near : register(t0);\nRWTexture2D<float4> output_near : register(u0);\nSamplerState s0 : register(s0);\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput_near.GetDimensions(screen_size.x, screen_size.y);\n\tscreen_size -= 1.0f;\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y);\n\n\n\tfloat2 uv = (screen_coord) / screen_size;\n\n\tstatic const int sample_radius = 3;\n\n\tfloat output = source_near.SampleLevel(s0, uv , 0).r;\n\n\t[unroll]\n\tfor (int y = -sample_radius; y <= sample_radius; ++y)\n\t{\n\t\t[unroll]\n\t\tfor (int x = -sample_radius; x <= sample_radius; ++x)\n\t\t{\n\t\t\toutput = max(output, source_near.SampleLevel(s0, saturate((screen_coord + float2(x, y)) / screen_size), 0).r);\n\t\t}\n\t}\n\n\toutput_near[int2(dispatch_thread_id.xy)] = output;\n}\n\n#endif //__PP_DOF_DILATE_HLSL__"
  },
  {
    "path": "resources/shaders/pp_dof_downscale.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_DOF_DOWNSCALE_HLSL__\n#define __PP_DOF_DOWNSCALE_HLSL__\n\n#include \"pp_dof_util.hlsl\"\n\nTexture2D source : register(t0);\nRWTexture2D<float4> output_near : register(u0);\nRWTexture2D<float4> output_far : register(u1);\nTexture2D cocbuffer : register(t1);\nSamplerState s0 : register(s0);\nSamplerState s1 : register(s1);\n\nfloat GetDownSampledCoC(float2 uv, float2 texelSize)\n{\n\tfloat4 offset = texelSize.xyxy * float2(-0.5f, 0.5f).xxyy;\n\n\tfloat coc0 = cocbuffer.SampleLevel(s1, uv + offset.xy, 0).r;\n\tfloat coc1 = cocbuffer.SampleLevel(s1, uv + offset.zy, 0).r;\n\tfloat coc2 = cocbuffer.SampleLevel(s1, uv + offset.xw, 0).r;\n\tfloat coc3 = cocbuffer.SampleLevel(s1, uv + offset.zw, 0).r;\n\tfloat coc4 = cocbuffer.SampleLevel(s1, uv, 0).r;\n\n\tfloat cocMin = min(min(min(coc0, coc1), coc2), coc3);\n\tfloat cocMax = max(max(max(coc0, coc1), coc2), coc3);\n\n\tfloat coc = cocMax >= -cocMin ? cocMax : cocMin;\n\treturn coc;\n}\n\n[numthreads(16, 16, 1)]\nvoid main_cs(int3 dispatch_thread_id : SV_DispatchThreadID)\n{\n\tfloat2 screen_size = float2(0.f, 0.f);\n\toutput_far.GetDimensions(screen_size.x, screen_size.y);\n\tscreen_size -= 1.0f;\n\n\tfloat2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y);\n\n\n\tfloat2 texel_size = 1.0f / screen_size;\n\tfloat4 offset = texel_size.xyxy * float2(-0.5f, 0.5f).xxyy;\n\n\tfloat2 uv = screen_coord / (screen_size);\n\n\tfloat3 source11 = source.SampleLevel(s0, uv, 0).rgb;\n\tfloat3 source0 = source.SampleLevel(s0, uv + offset.xy, 0).rgb;\n\tfloat3 source1 = source.SampleLevel(s0, uv + offset.zy, 0).rgb;\n\tfloat3 source2 = source.SampleLevel(s0, uv + offset.xw, 0).rgb;\n\tfloat3 source3 = source.SampleLevel(s0, uv + offset.zw, 0).rgb;\n\n\tfloat3 finalcolor = (source11 + source0 + source1 + source2 + source3) * 0.2f;\n\tfinalcolor = source.SampleLevel(s0, uv, 0).rgb;\n\n\tfloat coc = GetDownSampledCoC(uv, texel_size);\n\n\tfloat4 out_near = max(0,float4(finalcolor, 1.0f) * max(-coc, 0.0f));\n\tout_near.rgb = finalcolor;\n\n\tcoc = cocbuffer.SampleLevel(s1, uv, 0).r;\n\tfloat4 out_far = max(0,float4(finalcolor, 1.0f) * max(coc, 0.0f));\n\n\toutput_near[int2(dispatch_thread_id.xy)] = out_near;\n\toutput_far[int2(dispatch_thread_id.xy)] = out_far;\n}\n\n#endif //__PP_DOF_DOWNSCALE_HLSL__\n"
  },
  {
    "path": "resources/shaders/pp_dof_properties.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_DOF_PROPERTIES_HLSL__\n#define __PP_DOF_PROPERTIES_HLSL__\n\nstatic const float MAXBOKEHSIZE = 21.f;\nstatic const float MAXCOCSIZE = 4.0f;\nstatic const uint NUMDOFSAMPLES = 9;\n#endif //__PP_DOF_PROPERTIES_HLSL__"
  },
  {
    "path": "resources/shaders/pp_dof_util.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_DOF_UTIL_HLSL__\n#define __PP_DOF_UTIL_HLSL__\n\nstatic const float FNEAR = 0.1f;\nstatic const float FFAR = 10000.f;\n\nstatic const float PI = 3.141592654f;\nstatic const float PI2 = 6.283185308f;\n\nfloat WeighCoC(float coc, float radius)\n{\n\t//return coc >= radius;\n\treturn saturate((coc - radius + 2) / 2);\n}\n\nfloat WeighColor(float3 color)\n{\n\treturn 1 / (1 + max(max(color.r, color.g), color.b));\n}\n\nfloat GetLinearDepth(float depth)\n{\n\tfloat z = (2 * FNEAR) / (FFAR + FNEAR - (depth * (FFAR - FNEAR)));\n\treturn z;\n}\n\n// Calculates the gaussian blur weight for a given distance and sigmas\nfloat CalcGaussianWeight(int sampleDist, float sigma)\n{\n\tfloat g = 1.0f / sqrt(2.0f * 3.14159 * sigma * sigma);\n\treturn (g * exp(-(sampleDist * sampleDist) / (2 * sigma * sigma)));\n}\n\n\n// ------------------------------------------------------------------------------------------------\n// Samples a texture with B-spline (bicubic) filtering\n// ------------------------------------------------------------------------------------------------\nfloat4 SampleTextureBSpline(in Texture2D textureMap, in SamplerState linearSampler, in float2 uv) {\n\tfloat2 texSize;\n\ttextureMap.GetDimensions(texSize.x, texSize.y);\n\tfloat2 invTexSize = 1.0f / texSize;\n\n\tfloat2 a = frac(uv * texSize - 0.5f);\n\tfloat2 a2 = a * a;\n\tfloat2 a3 = a2 * a;\n\tfloat2 w0 = (1.0f / 6.0f) * (-a3 + 3 * a2 - 3 * a + 1);\n\tfloat2 w1 = (1.0f / 6.0f) * (3 * a3 - 6 * a2 + 4);\n\tfloat2 w2 = (1.0f / 6.0f) * (-3 * a3 + 3 * a2 + 3 * a + 1);\n\tfloat2 w3 = (1.0f / 6.0f) * a3;\n\tfloat2 g0 = w0 + w1;\n\tfloat2 g1 = w2 + w3;\n\tfloat2 h0 = 1.0f - (w1 / (w0 + w1)) + a;\n\tfloat2 h1 = 1.0f - (w3 / (w2 + w3)) - a;\n\n\tfloat2 ex = float2(invTexSize.x, 0.0f);\n\tfloat2 ey = float2(0.0f, invTexSize.y);\n\n\tw0 = 0.5f;\n\tw1 = 0.5f;\n\tg0 = 0.5f;\n\n\tfloat2 uv10 = uv + h0.x * ex;\n\tfloat2 uv00 = uv - h1.x * ex;\n\tfloat2 uv11 = uv10 + h0.y * ey;\n\tfloat2 uv01 = uv00 + h0.y * ey;\n\tuv10 = uv10 - h1.y * ey;\n\tuv00 = uv00 - h1.y * ey;\n\n\tuv00 = uv + float2(-0.75f, -0.75f) * invTexSize;\n\tuv10 = uv + float2(0.75f, -0.75f) * invTexSize;\n\tuv01 = uv + float2(-0.75f, 0.75f) * invTexSize;\n\tuv11 = uv + float2(0.75f, 0.75f) * invTexSize;\n\n\tfloat4 sample00 = textureMap.SampleLevel(linearSampler, uv00, 0.0f);\n\tfloat4 sample10 = textureMap.SampleLevel(linearSampler, uv10, 0.0f);\n\tfloat4 sample01 = textureMap.SampleLevel(linearSampler, uv01, 0.0f);\n\tfloat4 sample11 = textureMap.SampleLevel(linearSampler, uv11, 0.0f);\n\n\tsample00 = lerp(sample00, sample01, g0.y);\n\tsample10 = lerp(sample10, sample11, g0.y);\n\treturn lerp(sample00, sample10, g0.x);\n}\n\n// Maps a value inside the square [0,1]x[0,1] to a value in a disk of radius 1 using concentric squares.\n// This mapPIng preserves area, bi continuity, and minimizes deformation.\n// Based off the algorithm \"A Low Distortion Map Between Disk and Square\" by Peter Shirley and\n// Kenneth Chiu. Also includes polygon morphing modification from \"CryEngine3 Graphics Gems\"\n// by Tiago Sousa\nfloat2 SquareToConcentricDiskMapping(float x, float y, float numSides, float polygonAmount)\n{\n\tfloat phi, r;\n\n\t// -- (a,b) is now on [-1,1]2\n\tfloat a = 2.0f * x - 1.0f;\n\tfloat b = 2.0f * y - 1.0f;\n\n\tif (a > -b)                      // region 1 or 2\n\t{\n\t\tif (a > b)                   // region 1, also |a| > |b|\n\t\t{\n\t\t\tr = a;\n\t\t\tphi = (PI / 4.0f) * (b / a);\n\t\t}\n\t\telse                        // region 2, also |b| > |a|\n\t\t{\n\t\t\tr = b;\n\t\t\tphi = (PI / 4.0f) * (2.0f - (a / b));\n\t\t}\n\t}\n\telse                            // region 3 or 4\n\t{\n\t\tif (a < b)                   // region 3, also |a| >= |b|, a != 0\n\t\t{\n\t\t\tr = -a;\n\t\t\tphi = (PI / 4.0f) * (4.0f + (b / a));\n\t\t}\n\t\telse                        // region 4, |b| >= |a|, but a==0 and b==0 could occur.\n\t\t{\n\t\t\tr = -b;\n\t\t\tif (abs(b) > 0.0f)\n\t\t\t\tphi = (PI / 4.0f) * (6.0f - (a / b));\n\t\t\telse\n\t\t\t\tphi = 0;\n\t\t}\n\t}\n\n\tconst float N = numSides;\n\tfloat polyModifier = cos(PI / N) / cos(phi - (PI2 / N) * floor((N * phi + PI) / PI2));\n\tr *= lerp(1.0f, polyModifier, polygonAmount);\n\n\tfloat2 result;\n\tresult.x = r * cos(phi);\n\tresult.y = r * sin(phi);\n\n\treturn result;\n}\n\n#endif //__PP_DOF_UTIL_HLSL__"
  },
  {
    "path": "resources/shaders/pp_fxaa.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_FXAA_HLSL__\n#define __PP_FXAA_HLSL__\n\n#ifndef FXAA_REDUCE_MIN\n    #define FXAA_REDUCE_MIN   (1.0/ 128.0)\n#endif\n#ifndef FXAA_REDUCE_MUL\n    #define FXAA_REDUCE_MUL   (1.0 / 8.0)\n#endif\n#ifndef FXAA_SPAN_MAX\n    #define FXAA_SPAN_MAX     8.0\n#endif\n\n//optimized version for mobile, where dependent \n//texture reads can be a bottleneck\nfloat4 fxaa(Texture2D tex, SamplerState s, float2 fragCoord, float2 resolution,\n            float2 v_rgbNW, float2 v_rgbNE, \n            float2 v_rgbSW, float2 v_rgbSE, \n            float2 v_rgbM) {\n    float4 color;\n    float2 inverseVP = float2(1.0 / resolution.x, 1.0 / resolution.y);\n    float3 rgbNW = tex.SampleLevel(s, v_rgbNW, 0).xyz;\n    float3 rgbNE = tex.SampleLevel(s, v_rgbNE, 0).xyz;\n    float3 rgbSW = tex.SampleLevel(s, v_rgbSW, 0).xyz;\n    float3 rgbSE = tex.SampleLevel(s, v_rgbSE, 0).xyz;\n    float4 texColor = tex.SampleLevel(s, v_rgbM, 0);\n    float3 rgbM  = texColor.xyz;\n    float3 luma = float3(0.299, 0.587, 0.114);\n    float lumaNW = dot(rgbNW, luma);\n    float lumaNE = dot(rgbNE, luma);\n    float lumaSW = dot(rgbSW, luma);\n    float lumaSE = dot(rgbSE, luma);\n    float lumaM  = dot(rgbM,  luma);\n    float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));\n    float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));\n    \n    float2 dir;\n    dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));\n    dir.y =  ((lumaNW + lumaSW) - (lumaNE + lumaSE));\n    \n    float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *\n                          (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);\n    \n    float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);\n    dir = min(float2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),\n              max(float2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),\n              dir * rcpDirMin)) * inverseVP;\n    \n    float3 rgbA = 0.5 * (\n\t\ttex.SampleLevel(s, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5), 0).xyz +\n\t\ttex.SampleLevel(s, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5), 0).xyz);\n    float3 rgbB = rgbA * 0.5 + 0.25 * (\n        tex.SampleLevel(s, fragCoord * inverseVP + dir * -0.5, 0).xyz +\n        tex.SampleLevel(s, fragCoord * inverseVP + dir * 0.5, 0).xyz);\n\n    float lumaB = dot(rgbB, luma);\n    if ((lumaB < lumaMin) || (lumaB > lumaMax))\n        color = float4(rgbA, texColor.a);\n    else\n        color = float4(rgbB, texColor.a);\n    return color;\n}\n\nvoid texcoords(float2 fragCoord, float2 resolution,\n\t\t\tout float2 v_rgbNW, out float2 v_rgbNE,\n\t\t\tout float2 v_rgbSW, out float2 v_rgbSE,\n\t\t\tout float2 v_rgbM) {\n\tfloat2 inverseVP = 1.0 / resolution.xy;\n\tv_rgbNW = (fragCoord + float2(-1.0, -1.0)) * inverseVP;\n\tv_rgbNE = (fragCoord + float2(1.0, -1.0)) * inverseVP;\n\tv_rgbSW = (fragCoord + float2(-1.0, 1.0)) * inverseVP;\n\tv_rgbSE = (fragCoord + float2(1.0, 1.0)) * inverseVP;\n\tv_rgbM = float2(fragCoord * inverseVP);\n}\n\nfloat4 SampleFXAA(Texture2D tex, SamplerState s, float2 frag_coord, float2 resolution)\n{\n\tfloat2 v_rgbNW;\n\tfloat2 v_rgbNE;\n\tfloat2 v_rgbSW;\n\tfloat2 v_rgbSE;\n\tfloat2 v_rgbM;\n\n\ttexcoords(frag_coord, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM);\n\treturn fxaa(tex, s, frag_coord, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM);\n}\n\n#endif //__PP_FXAA_HLSL__"
  },
  {
    "path": "resources/shaders/pp_hdr_util.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_HDR_UTIL_HLSL__\n#define __PP_HDR_UTIL_HLSL__\n\nfloat3 linearToneMapping(float3 color, float exposure, float gamma)\n{\n\tcolor = clamp(exposure * color, 0.f, 1.f);\n\tcolor = pow(color, 1.f / gamma);\n\treturn color;\n}\n\nfloat3 simpleReinhardToneMapping(float3 color, float exposure, float gamma)\n{\n\tcolor *= exposure / (1. + color / exposure);\n\tcolor = pow(color, 1. / gamma);\n\treturn color;\n}\n\nfloat3 lumaBasedReinhardToneMapping(float3 color, float gamma)\n{\n\tfloat luma = dot(color, float3(0.2126, 0.7152, 0.0722));\n\tfloat toneMappedLuma = luma / (1. + luma);\n\tcolor *= toneMappedLuma / luma;\n\tcolor = pow(color, (1. / gamma));\n\treturn color;\n}\n\nfloat3 whitePreservingLumaBasedReinhardToneMapping(float3 color, float gamma)\n{\n\tfloat white = 2.;\n\tfloat luma = dot(color, float3(0.2126, 0.7152, 0.0722));\n\tfloat toneMappedLuma = luma * (1. + luma / (white*white)) / (1. + luma);\n\tcolor *= toneMappedLuma / luma;\n\tcolor = pow(color, (1. / gamma));\n\treturn color;\n}\n\nfloat3 RomBinDaHouseToneMapping(float3 color, float gamma)\n{\n\tcolor = exp(-1.0 / (2.72*color + 0.15));\n\tcolor = pow(color, (1. / gamma));\n\treturn color;\n}\n\nfloat3 filmicToneMapping(float3 color)\n{\n\tcolor = max(float3(0., 0., 0.), color - float3(0.004, 0.004, 0.004));\n\tcolor = (color * (6.2 * color + .5)) / (color * (6.2 * color + 1.7) + 0.06);\n\treturn color;\n}\n\nfloat3 GrayscaleToneMapping(float3 color) {\n\tfloat gray = 0.299 * color.r + 0.587 * color.g + 0.114 * color.b;\n\tcolor.r = gray;\n\tcolor.g = gray;\n\tcolor.b = gray;\n\treturn color;\n}\n\nfloat3 ACESToneMapping(float3 color, float exposure, float gamma)\n{\n\tcolor = clamp(exposure * color, 0.f, 1.f);\n\tcolor = pow(color, 1.f / gamma);\n\n    float a = 2.51f;\n    float b = 0.03f;\n    float c = 2.43f;\n    float d = 0.59f;\n    float e = 0.14f;\n    return saturate((color*(a*color+b))/(color*(c*color+d)+e));\n}\n\nfloat3 Uncharted2ToneMapping(float3 color, float gamma, float exposure)\n{\n\tfloat A = 0.15;\n\tfloat B = 0.50;\n\tfloat C = 0.10;\n\tfloat D = 0.20;\n\tfloat E = 0.02;\n\tfloat F = 0.30;\n\tfloat W = 11.2;\n\n\tcolor *= exposure;\n\tcolor = (((color*2) * (A * (color*2) + C * B) + D * E) / ((color*2) * (A * (color*2) + B) + D * F)) - E / F;\n\t\n\tfloat white = ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F;\n\t\n\tcolor /= white;\n\tcolor = pow(color, (1. / gamma));\n\t\n\treturn color;\n}\n\nfloat4x4 brightnessMatrix(float brightness)\n{\n\treturn float4x4(1, 0, 0, 0,\n\t\t0, 1, 0, 0,\n\t\t0, 0, 1, 0,\n\t\tbrightness, brightness, brightness, 1);\n}\n\nfloat3 ApplyHue(float3 color, float hue_in) {\n\tfloat3 result = color;\n\n\tconst float4  kRGBToYPrime = float4(0.299, 0.587, 0.114, 0.0);\n\tconst float4  kRGBToI = float4(0.596, -0.275, -0.321, 0.0);\n\tconst float4  kRGBToQ = float4(0.212, -0.523, 0.311, 0.0);\n\n\tconst float4  kYIQToR = float4(1.0, 0.956, 0.621, 0.0);\n\tconst float4  kYIQToG = float4(1.0, -0.272, -0.647, 0.0);\n\tconst float4  kYIQToB = float4(1.0, -1.107, 1.704, 0.0);\n\n\tfloat YPrime = dot(color, kRGBToYPrime);\n\tfloat I = dot(color, kRGBToI);\n\tfloat Q = dot(color, kRGBToQ);\n\n\tfloat hue = atan2(Q, I);\n\tfloat chroma = sqrt(I * I + Q * Q);\n\n\thue += hue_in;\n\n\tQ = chroma * sin(hue);\n\tI = chroma * cos(hue);\n\n\tfloat4 yIQ = float4(YPrime, I, Q, 0.0);\n\tresult.r = dot(yIQ, kYIQToR);\n\tresult.g = dot(yIQ, kYIQToG);\n\tresult.b = dot(yIQ, kYIQToB);\n\n\treturn result;\n}\n\nfloat4x4 ContrastMatrix(float contrast)\n{\n\tfloat t = (1.0 - contrast) / 2.0;\n\n\treturn float4x4(contrast, 0, 0, 0,\n\t\t0, contrast, 0, 0,\n\t\t0, 0, contrast, 0,\n\t\tt, t, t, 1);\n\n}\n\nfloat3 AllTonemappingAlgorithms(float3 color, float rotation, float exposure, float gamma) {\n\tfloat3 result = color;\n\n\tfloat n = 8;\n\tint i = int(n * (rotation / 2));\n\n\tif (i == 0) result = linearToneMapping(color, exposure, gamma);\n\tif (i == 1) result = simpleReinhardToneMapping(color, exposure, gamma);\n\tif (i == 2) result = lumaBasedReinhardToneMapping(color, gamma);\n\tif (i == 3) result = whitePreservingLumaBasedReinhardToneMapping(color, gamma);\n\tif (i == 4) result = RomBinDaHouseToneMapping(color, gamma);\n\tif (i == 5) result = filmicToneMapping(color);\n\tif (i == 6) result = ACESToneMapping(color, exposure, gamma);\n\tif (i == 7) result = Uncharted2ToneMapping(color, gamma, exposure);\n\tif (i == 8) result = GrayscaleToneMapping(color);\n\n\treturn result;\n}\n\n#endif //__PP_HDR_UTIL_HLSL__\n"
  },
  {
    "path": "resources/shaders/pp_tonemapping.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_TONEMAPPING_HLSL__\n#define __PP_TONEMAPPING_HLSL__\n\n#include \"pp_util.hlsl\"\n#include \"pp_hdr_util.hlsl\"\n\nTexture2D<float4> input : register(t0);\nRWTexture2D<float4> output : register(u0);\nSamplerState s0 : register(s0);\n\ncbuffer CameraProperties : register(b0)\n{\n\tfloat hdr;\n};\n\n[numthreads(16, 16, 1)]\nvoid main(uint3 DTid : SV_DispatchThreadID)\n{\n\tfloat2 resolution;\n\tinput.GetDimensions(resolution.x, resolution.y);\n\t\n\tfloat2 uv = (float2(DTid.xy + 0.5f) / resolution);\n\n\tfloat gamma = 2.2;\n\tfloat exposure = 1;\n\n\tfloat3 color = input.SampleLevel(s0, uv, 0).rgb;\n\t//color = SampleFXAA(input, s0, DTid.xy + 0.5f, resolution);\n\t//uv = ZoomUV(uv, 0.75);\n\t//float3 color = input.SampleLevel(s0, BarrelDistortUV(uv, 2), 0);\n\t//float3 color = ChromaticAberrationV2(input, s0, uv, 0.2, 0.96f).rgb;\n\n\tif (hdr == 0)\n\t{\n\t\t//color = linearToneMapping(color, exposure, gamma);\n\t\t//color = simpleReinhardToneMapping(color, exposure, gamma);\n\t\t//color = lumaBasedReinhardToneMapping(color, gamma);\n\t\t//color = whitePreservingLumaBasedReinhardToneMapping(color, gamma);\n\t\t//color = RomBinDaHouseToneMapping(color, gamma);\n\t\t//color = filmicToneMapping(color);\n\t\t//color = Uncharted2ToneMapping(color, gamma, exposure);\n\t\t//color = GrayscaleToneMapping(color);\n\t\tcolor = ACESToneMapping(color, exposure, gamma);\n\t\t//color = AllTonemappingAlgorithms(color.rgb, uv.x + uv.y, exposure, gamma);\n\t\t//color = Vignette(color, uv, 1.5, 0.5, 0.5);\n\t}\n\n\toutput[DTid.xy] = float4(color, 1);\n}\n\n#endif //__PP_TONEMAPPING_HLSL__\n"
  },
  {
    "path": "resources/shaders/pp_util.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __PP_UTIL_HLSL__\n#define __PP_UTIL_HLSL__\n\n#include \"pp_fxaa.hlsl\"\n#include \"math.hlsl\"\n\nfloat3 Vignette(float3 input, float2 pos, float radius, float softness, float strength)\n{\n\tfloat len = length(pos * 2 - 1);\n\tfloat vignette = smoothstep(radius, radius -  softness, len);\n\treturn lerp(input, input * vignette, strength);\n}\n\n\n// Old lens_effects.hlsl file\nfloat2 barrelDistortion(float2 coord, float amt) {\n\tfloat2 cc = coord - 0.5;\n\tfloat dist = dot(cc, cc);\n\treturn coord + cc * dist * amt;\n}\n\nstatic const int num_iter = 11;\nstatic const float reci_num_iter_f = 1.0 / float(num_iter);\n\n// Old naive chromatic aberration.\nfloat4 ChromaticAberration(Texture2D tex, SamplerState s, float2 uv, float strength) {\n\tfloat2 r_offset = float2(strength, 0);\n\tfloat2 g_offset = float2(-strength, 0);\n\tfloat2 b_offset = float2(0, 0);\n\n\tfloat4 r = float4(1, 1, 1, 1);\n\tr.x = tex.SampleLevel(s, uv + r_offset, 0).x;\n\tr.y = tex.SampleLevel(s, uv + g_offset, 0).y;\n\tr.z = tex.SampleLevel(s, uv + b_offset, 0).z;\n\tr.a = tex.SampleLevel(s, uv, 0).a;\n\n\treturn r;\n}\n\n// Zoom into a image.\nfloat2 ZoomUV(float2 uv, float zoom)\n{\n\treturn (uv * zoom) + ((1 - (zoom)) / 2);\n}\n\nfloat4 ChromaticAberrationV2(Texture2D tex, SamplerState s, float2 uv, float strength, float zoom) {\n\tuv = ZoomUV(uv, zoom);\n\tfloat4 sumcol = 0.0;\n\tfloat4 sumw = 0.0;\n\n\tfloat2 resolution;\n\ttex.GetDimensions(resolution.x, resolution.y);\n\n\tfor (int i = 0; i < num_iter; ++i)\n\t{\n\t\tfloat t = float(i) * reci_num_iter_f;\n\t\tfloat4 w = spectrum_offset(t);\n\t\tsumw += w;\n\t\t//sumcol += w * tex.SampleLevel(s, barrelDistortion(uv, 0.6 * strength*t ), 0);\n\t\tsumcol += w * SampleFXAA(tex, s, barrelDistortion(uv, 0.6 * strength * t) * resolution, resolution);\n\t}\n\n\treturn sumcol / sumw;\n}\n\nfloat2 BarrelDistortUV(float2 uv, float kcube)\n{\n\tfloat k = -0.15;\n\n\tfloat r2 = (uv.x - 0.5) * (uv.x - 0.5) + (uv.y - 0.5) * (uv.y - 0.5);\n\tfloat f = 0;\n\n\t//only compute the cubic distortion if necessary\n\tif (kcube == 0.0)\n\t{\n\t\tf = 1 + r2 * k;\n\t}\n\telse\n\t{\n\t\tf = 1 + r2 * (k + kcube * sqrt(r2));\n\t};\n\n\t// get the right pixel for the current position\n\tfloat x = f * (uv.x - 0.5) + 0.5;\n\tfloat y = f * (uv.y - 0.5) + 0.5;\n\n\treturn float2(x, y);\n}\n\n#endif //__PP_UTIL_HLSL__"
  },
  {
    "path": "resources/shaders/rand_util.hlsl",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#ifndef __RAND_UTIL_HLSL__\n#define __RAND_UTIL_HLSL__\n\n#include \"math.hlsl\"\n\n// Initialize random seed\nuint initRand(uint val0, uint val1, uint backoff = 16)\n{\n\tuint v0 = val0, v1 = val1, s0 = 0;\n\n\t[unroll]\n\tfor (uint n = 0; n < backoff; n++)\n\t{\n\t\ts0 += 0x9e3779b9;\n\t\tv0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s0) ^ ((v1 >> 5) + 0xc8013ea4);\n\t\tv1 += ((v0 << 4) + 0xad90777d) ^ (v0 + s0) ^ ((v0 >> 5) + 0x7e95761e);\n\t}\n\treturn v0;\n}\n\n// Get 'random' value [0, 1]\nfloat nextRand(inout uint s)\n{\n\ts = (1664525u * s + 1013904223u);\n\treturn float(s & 0x00FFFFFF) / float(0x01000000);\n}\n\nfloat3 rand_in_unit_sphere(inout uint rng_state)\n{\n\tfloat z = nextRand(rng_state) * 2.0f - 1.0f;\n\tfloat t = nextRand(rng_state) * 2.0f * M_PI;\n\tfloat r = sqrt(max(0.0, 1.0f - z * z));\n\tfloat x = r * cos(t);\n\tfloat y = r * sin(t);\n\tfloat3 res = float3(x, y, z);\n\tres *= pow(nextRand(rng_state), 1.0 / 3.0);\n\treturn res;\n}\n\nfloat3 getPerpendicularVector(float3 u)\n{\n\tfloat3 a = abs(u);\n\tuint xm = ((a.x - a.y)<0 && (a.x - a.z)<0) ? 1 : 0;\n\tuint ym = (a.y - a.z)<0 ? (1 ^ xm) : 0;\n\tuint zm = 1 ^ (xm | ym);\n\treturn cross(u, float3(xm, ym, zm));\n}\n\n// Get a cosine-weighted random vector centered around a specified normal direction.\nfloat3 getCosHemisphereSample(inout uint randSeed, float3 hitNorm)\n{\n\t// Get 2 random numbers to select our sample with\n\tfloat2 randVal = float2(nextRand(randSeed), nextRand(randSeed));\n\n\t// Cosine weighted hemisphere sample from RNG\n\tfloat3 bitangent = getPerpendicularVector(hitNorm);\n\tfloat3 tangent = cross(bitangent, hitNorm);\n\tfloat r = sqrt(randVal.x);\n\tfloat phi = 2.0f * M_PI * randVal.y;\n\n\t// Get our cosine-weighted hemisphere lobe sample direction\n\treturn tangent * (r * cos(phi).x) + bitangent * (r * sin(phi)) + hitNorm.xyz * sqrt(max(0.0, 1.0f - randVal.x));\n}\n\n// Get a uniform weighted random vector centered around a specified normal direction.\nfloat3 getUniformHemisphereSample(inout uint randSeed, float3 hitNorm)\n{\n\t// Get 2 random numbers to select our sample with\n\tfloat2 randVal = float2(nextRand(randSeed), nextRand(randSeed));\n\n\t// Cosine weighted hemisphere sample from RNG\n\tfloat3 bitangent = getPerpendicularVector(hitNorm);\n\tfloat3 tangent = cross(bitangent, hitNorm);\n\tfloat r = sqrt(max(0.0f,1.0f - randVal.x*randVal.x));\n\tfloat phi = 2.0f * M_PI * randVal.y;\n\n\t// Get our cosine-weighted hemisphere lobe sample direction\n\treturn tangent * (r * cos(phi).x) + bitangent * (r * sin(phi)) + hitNorm.xyz * randVal.x;\n}\n\nfloat3 getUniformHemisphereSample(inout uint randSeed, float3 hitNorm, float angle)\n{\n\t// Get 2 random numbers to select our sample with\n\tfloat2 randVal = float2(nextRand(randSeed), nextRand(randSeed));\n\n\t// Cosine weighted hemisphere sample from RNG\n\tfloat3 bitangent = getPerpendicularVector(hitNorm);\n\tfloat3 tangent = cross(bitangent, hitNorm);\n\n\tfloat r = sqrt(max(0.0f, 1.0f - randVal.x * randVal.x)) * sin(angle);\n\tfloat phi = 2.0f * M_PI * randVal.y;\n\n\t// Get our cosine-weighted hemisphere lobe sample direction\n\treturn tangent * (r * cos(phi).x) + bitangent * (r * sin(phi)) + hitNorm.xyz * cos(asin(r));\n}\n\nfloat3 perturbDirectionVector(inout uint randSeed, float3 direction, float angle)\n{\n\tfloat s = nextRand(randSeed);\n\tfloat r = nextRand(randSeed);\n\n\tfloat h = cos(angle);\n\n\tfloat phi = 2.0f * M_PI * s;\n\n\tfloat z = h + (1.0f - h) * r;\n\tfloat sinT = sqrt(1.0f - z * z);\n\n\tfloat x = cos(phi) * sinT;\n\tfloat y = sin(phi) * sinT;\n\n\tfloat3 bitangent = getPerpendicularVector(direction);\n\tfloat3 tangent = cross(bitangent, direction);\n\n\treturn bitangent * x + tangent * y + direction * z;\n}\n\n#endif // __RAND_UTIL_HLSL__\n"
  },
  {
    "path": "resources/sponza_lights.json",
    "content": "{\r\n    \"lights\": [\r\n        {\r\n            \"angle\": 69.0,\r\n            \"color\": [\r\n                0.9999899864196777,\r\n                0.9999899864196777,\r\n                1.0\r\n            ],\r\n            \"pos\": [\r\n                0.0,\r\n                3.562610149383545,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"radius\": 200.0,\r\n            \"rot\": [\r\n                0.0,\r\n                0.0,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"size\": 0.0,\r\n            \"type\": 0\r\n        },\r\n        {\r\n            \"angle\": 0.0,\r\n            \"color\": [\r\n                1.0,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"pos\": [\r\n                -4.0,\r\n                1.0,\r\n                -7.193277359008789,\r\n                0.0\r\n            ],\r\n            \"radius\": 5.0,\r\n            \"rot\": [\r\n                -1.0399991273880005,\r\n                7.299992084503174,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"size\": 0.0,\r\n            \"type\": 0\r\n        },\r\n        {\r\n            \"angle\": 0.0,\r\n            \"color\": [\r\n                0.0,\r\n                1.0,\r\n                0.0\r\n            ],\r\n            \"pos\": [\r\n                4.0,\r\n                1.0,\r\n                -7.192999839782715,\r\n                0.0\r\n            ],\r\n            \"radius\": 5.0,\r\n            \"rot\": [\r\n                0.0,\r\n                0.0,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"size\": 0.0,\r\n            \"type\": 0\r\n        }\r\n    ]\r\n}\r\n"
  },
  {
    "path": "resources/viknell_lights.json",
    "content": "{\r\n    \"lights\": [\r\n        {\r\n            \"angle\": 69.0,\r\n            \"color\": [\r\n                0.0,\r\n                0.0,\r\n                1.0\r\n            ],\r\n            \"pos\": [\r\n                0.75,\r\n                0.0,\r\n                0.75,\r\n                0.0\r\n            ],\r\n            \"radius\": 6.0,\r\n            \"rot\": [\r\n                0.0,\r\n                0.0,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"size\": 0.0,\r\n            \"type\": 0\r\n        },\r\n        {\r\n            \"angle\": 0.0,\r\n            \"color\": [\r\n                1.0,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"pos\": [\r\n                -0.75,\r\n                0.0,\r\n                0.75,\r\n                0.0\r\n            ],\r\n            \"radius\": 5.0,\r\n            \"rot\": [\r\n                0.0,\r\n                0.0,\r\n                0.0,\r\n                0.0\r\n            ],\r\n            \"size\": 0.0,\r\n            \"type\": 0\r\n        }\r\n    ]\r\n}\r\n"
  },
  {
    "path": "scripts/JenkinsWebhook.bat",
    "content": "@echo off\nset \"str=%~1\"\n\"C:\\Program Files\\cURL\\bin\\curl.exe\" -X POST --data \"{ \\\"content\\\": \\\"%str%\\\", \\\"username\\\": \\\"Jenkins\\\" }\" -H \"Content-Type: application/json\" https://discordapp.com/api/webhooks/488672255321309185/GdU9rve6wW5rcbk6xcDx4TX8AKez5Yfej8kfKco17qRvHTlTuB6bdziQIDDYBHW8HuES\n@echo on"
  },
  {
    "path": "src/constant_buffer_pool.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"constant_buffer_pool.hpp\"\n\nnamespace wr \n{\n\tConstantBufferPool::ConstantBufferPool(std::size_t size_in_bytes) : m_size_in_bytes(size_in_bytes)\n\t{\n\t}\n\n\tConstantBufferHandle* ConstantBufferPool::Create(std::size_t buffer_size)\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_mutex);\n\n\t\treturn AllocateConstantBuffer(buffer_size);\n\t}\n\n\tvoid ConstantBufferPool::Update(ConstantBufferHandle* handle, size_t size, size_t offset, std::uint8_t * data)\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_mutex);\n\n\t\tWriteConstantBufferData(handle, size, offset, data);\n\t}\n\n\tvoid ConstantBufferPool::Destroy(ConstantBufferHandle* handle)\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_mutex);\n\n\t\tDeallocateConstantBuffer(handle);\n\t}\n\tvoid ConstantBufferPool::Update(ConstantBufferHandle * handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t * data)\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_mutex);\n\n\t\tWriteConstantBufferData(handle, size, offset, frame_idx, data);\n\t}\n} /* wr */"
  },
  {
    "path": "src/constant_buffer_pool.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <string_view>\n#include <vector>\n#include <optional>\n#include <mutex>\n#include <d3d12.h>\n\n#include \"util/defines.hpp\"\n\nnamespace wr \n{\n\n\tclass ConstantBufferPool;\n\t\n\tstruct ConstantBufferHandle \n\t{\n\t\tConstantBufferPool* m_pool;\n\t};\n\n\tclass ConstantBufferPool\n\t{\n\tpublic:\n\t\texplicit ConstantBufferPool(std::size_t size_in_bytes);\n\t\tvirtual ~ConstantBufferPool() = default;\n\n\t\tConstantBufferPool(ConstantBufferPool const &) = delete;\n\t\tConstantBufferPool& operator=(ConstantBufferPool const &) = delete;\n\t\tConstantBufferPool(ConstantBufferPool&&) = delete;\n\t\tConstantBufferPool& operator=(ConstantBufferPool&&) = delete;\n\n\t\t[[nodiscard]] ConstantBufferHandle* Create(std::size_t buffer_size);\n\n\t\tvoid Update(ConstantBufferHandle* handle, size_t size, size_t offset, std::uint8_t* data);\n\t\tvoid Update(ConstantBufferHandle* handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t* data);\n\n\t\tvoid Destroy(ConstantBufferHandle* handle);\n\n\t\tvirtual void Evict() = 0;\n\t\tvirtual void MakeResident() = 0;\n\n\tprotected:\n\t\tvirtual ConstantBufferHandle* AllocateConstantBuffer(std::size_t buffer_size) = 0;\n\t\tvirtual void WriteConstantBufferData(ConstantBufferHandle* handle, size_t size, size_t offset, std::uint8_t* data) = 0;\n\t\tvirtual void WriteConstantBufferData(ConstantBufferHandle* handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t* data) = 0;\n\t\tvirtual void DeallocateConstantBuffer(ConstantBufferHandle* handle) = 0;\n\n\t\tstd::size_t m_size_in_bytes;\n\t\tstd::mutex m_mutex;\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/d3d12/d3d12_acceleration_structure..cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include <variant>\n\n#include \"d3d12_defines.hpp\"\n#include \"d3d12_rt_descriptor_heap.hpp\"\n\nnamespace wr::d3d12\n{\n\n\t/*!\n\tNOTE!\n\tTop level has scratch and instance descs.\n\tblas does not!!!!!!!!\n\t*/\n\n\tnamespace internal\n\t{\n\n\t\tinline void AllocateUAVBuffer(Device* device, UINT64 size, ID3D12Resource** resource, D3D12_RESOURCE_STATES initial_state = D3D12_RESOURCE_STATE_COMMON, const wchar_t* name = nullptr)\n\t\t{\n\t\t\tauto heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\t\tauto buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(size, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);\n\t\t\tTRY(device->m_native->CreateCommittedResource(\n\t\t\t\t&heap_properties,\n\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t&buffer_desc,\n\t\t\t\tinitial_state,\n\t\t\t\tnullptr,\n\t\t\t\tIID_PPV_ARGS(resource)));\n\t\t\tif (name)\n\t\t\t{\n\t\t\t\tNAME_D3D12RESOURCE((*resource), name);\n\t\t\t}\n\t\t}\n\n\t\tinline void AllocateUploadBuffer(Device* device, void* data, UINT64 size, ID3D12Resource** resource, const wchar_t* name = nullptr)\n\t\t{\n\t\t\tauto heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\t\tauto buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(size);\n\t\t\tTRY(device->m_native->CreateCommittedResource(\n\t\t\t\t&heap_properties,\n\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t&buffer_desc,\n\t\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\t\tnullptr,\n\t\t\t\tIID_PPV_ARGS(resource)));\n\t\t\tif (name)\n\t\t\t{\n\t\t\t\tNAME_D3D12RESOURCE((*resource), name);\n\t\t\t}\n\n\t\t\tvoid *mapped_data;\n\t\t\t(*resource)->Map(0, nullptr, &mapped_data);\n\t\t\tmemcpy(mapped_data, data, size);\n\t\t\t(*resource)->Unmap(0, nullptr);\n\t\t}\n\n\t\tinline void UpdateUploadbuffer(void* data, UINT64 size, ID3D12Resource* resource)\n\t\t{\n\t\t\tvoid *mapped_data;\n\t\t\tresource->Map(0, nullptr, &mapped_data);\n\t\t\tmemcpy(mapped_data, data, size);\n\t\t\tresource->Unmap(0, nullptr);\n\t\t}\n\n\t\tWRAPPED_GPU_POINTER CreateFallbackWrappedPointer(\n\t\t\tDevice* device,\n\t\t\tDescriptorHeap* heap,\n\t\t\tstd::uint32_t index,\n\t\t\tID3D12Resource* resource,\n\t\t\tUINT buffer_num_elements)\n\t\t{\n\n\t\t\tif (GetRaytracingType(device) != RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\tLOGW(\"CreateFallbackWrappedPointer got called but the device isn't setup for fallback.\");\n\t\t\t}\n\n\t\t\tD3D12_UNORDERED_ACCESS_VIEW_DESC rawBufferUavDesc = {};\n\t\t\trawBufferUavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;\n\t\t\trawBufferUavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;\n\t\t\trawBufferUavDesc.Format = DXGI_FORMAT_R32_TYPELESS;\n\t\t\trawBufferUavDesc.Buffer.NumElements = buffer_num_elements;\n\n\t\t\td3d12::DescHeapCPUHandle bottom_level_descriptor;\n\n\t\t\t// Only compute fallback requires a valid descriptor index when creating a wrapped pointer.\n\t\t\tUINT desc_heap_idx = index; // TODO don't hardcode this.\n\t\t\tif (!device->m_fallback_native->UsingRaytracingDriver())\n\t\t\t{\n\t\t\t\tfor (auto frame_idx = 0; frame_idx < d3d12::settings::num_back_buffers; frame_idx++)\n\t\t\t\t{\n\t\t\t\t\tbottom_level_descriptor = d3d12::GetCPUHandle(heap, frame_idx, 0); // TODO: Don't harcode this.\n\t\t\t\t\td3d12::Offset(bottom_level_descriptor, desc_heap_idx, heap->m_increment_size);\n\t\t\t\t\tdevice->m_native->CreateUnorderedAccessView(resource, nullptr, &rawBufferUavDesc, bottom_level_descriptor.m_native);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn device->m_fallback_native->GetWrappedPointerSimple(desc_heap_idx, resource->GetGPUVirtualAddress());\n\t\t}\n\n\t\tinline void UpdatePrebuildInfo(Device* device, AccelerationStructure& as, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs)\n\t\t{\n\t\t\tif (GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t\t{\n\t\t\t\tdevice->m_native->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &as.m_prebuild_info);\n\t\t\t}\n\t\t\telse if (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\tdevice->m_fallback_native->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &as.m_prebuild_info);\n\t\t\t}\n\t\t\tif (!(as.m_prebuild_info.ResultDataMaxSizeInBytes > 0)) LOGW(\"Result data max size in bytes is more than zero. accel structure\");\n\t\t}\n\n\t\tinline void BuildAS(Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC const & desc)\n\t\t{\n\t\t\tauto BuildAccelerationStructure = [&](auto * raytracingCommandList)\n\t\t\t{\n\t\t\t\traytracingCommandList->BuildRaytracingAccelerationStructure(&desc, 0, nullptr);\n\t\t\t};\n\n\t\t\t// Build acceleration structure.\n\t\t\tif (GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t\t{\n\t\t\t\tBuildAccelerationStructure(cmd_list->m_native);\n\t\t\t}\n\t\t\telse if (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\t// Set the descriptor heaps to be used during acceleration structure build for the Fallback Layer.\n\t\t\t\td3d12::BindDescriptorHeap(cmd_list, desc_heap, desc_heap->m_create_info.m_type, 0, true); //TODO: note this non frame idx\n\t\t\t\tBuildAccelerationStructure(cmd_list->m_native_fallback);\n\t\t\t}\n\t\t}\n\n\t\tinline void CreateInstancesForTLAS(Device* device, AccelerationStructure& tlas, DescriptorHeap* desc_heap, std::vector<desc::BlasDesc> blas_list, std::uint32_t frame_idx, bool update)\n\t\t{\n\t\t\t// Falback layer heap offset\n\t\t\tauto fallback_heap_idx = d3d12::settings::fallback_ptrs_offset;\n\n\t\t\t// Create the instances to the bottom level instances.\n\t\t\tif (GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t\t{\n\t\t\t\tstd::vector<D3D12_RAYTRACING_INSTANCE_DESC> instance_descs;\n\t\t\t\tfor (auto it : blas_list)\n\t\t\t\t{\n\t\t\t\t\tauto blas = it.m_as;\n\t\t\t\t\tauto material = it.m_material;\n\t\t\t\t\tauto transform = it.m_transform;\n\n\t\t\t\t\tD3D12_RAYTRACING_INSTANCE_DESC instance_desc = {};\n\n\t\t\t\t\tXMStoreFloat3x4(reinterpret_cast<DirectX::XMFLOAT3X4*>(instance_desc.Transform), transform);\n\n\t\t\t\t\tinstance_desc.InstanceMask = 1;\n\t\t\t\t\tinstance_desc.InstanceID = material;\n\t\t\t\t\tinstance_desc.AccelerationStructure = blas.m_natives[frame_idx]->GetGPUVirtualAddress();\n\t\t\t\t\tinstance_descs.push_back(instance_desc);\n\t\t\t\t}\n\n\t\t\t\tif (update)\n\t\t\t\t{\n\t\t\t\t\tinternal::UpdateUploadbuffer(instance_descs.data(), sizeof(D3D12_RAYTRACING_INSTANCE_DESC) * blas_list.size(), tlas.m_instance_descs[frame_idx]);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tfor (auto& inst_desc : tlas.m_instance_descs)\n\t\t\t\t\t{\n\t\t\t\t\t\tinternal::AllocateUploadBuffer(device, instance_descs.data(), sizeof(D3D12_RAYTRACING_INSTANCE_DESC) * blas_list.size(), &inst_desc, L\"InstanceDescs\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\tstd::vector<D3D12_RAYTRACING_FALLBACK_INSTANCE_DESC> instance_descs;\n\t\t\t\tfor (auto it : blas_list)\n\t\t\t\t{\n\t\t\t\t\tauto blas = it.m_as;\n\t\t\t\t\tauto material = it.m_material;\n\t\t\t\t\tauto transform = it.m_transform;\n\n\t\t\t\t\tD3D12_RAYTRACING_FALLBACK_INSTANCE_DESC instance_desc = {};\n\n\t\t\t\t\tXMStoreFloat3x4(reinterpret_cast<DirectX::XMFLOAT3X4*>(instance_desc.Transform), transform);\n\n\t\t\t\t\tinstance_desc.InstanceMask = 1;\n\t\t\t\t\tinstance_desc.InstanceID = material;\n\t\t\t\t\tstd::uint32_t num_buffer_elements = static_cast<std::uint32_t>(blas.m_prebuild_info.ResultDataMaxSizeInBytes) / sizeof(std::uint32_t);\n\t\t\t\t\tinstance_desc.AccelerationStructure = internal::CreateFallbackWrappedPointer(device, desc_heap, fallback_heap_idx, blas.m_natives[frame_idx], num_buffer_elements);\n\n\t\t\t\t\tinstance_descs.push_back(instance_desc);\n\n\t\t\t\t\tfallback_heap_idx++;\n\t\t\t\t}\n\n\t\t\t\tif (update)\n\t\t\t\t{\n\t\t\t\t\tinternal::UpdateUploadbuffer(instance_descs.data(), sizeof(D3D12_RAYTRACING_FALLBACK_INSTANCE_DESC) * instance_descs.size(), tlas.m_instance_descs[frame_idx]);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tfor (auto& inst_desc : tlas.m_instance_descs)\n\t\t\t\t\t{\n\t\t\t\t\t\tinternal::AllocateUploadBuffer(device, instance_descs.data(), sizeof(D3D12_RAYTRACING_FALLBACK_INSTANCE_DESC) * instance_descs.size(), &inst_desc, L\"InstanceDescs\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Create a wrapped pointer to the acceleration structure.\n\t\t\tif (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\tstd::uint32_t num_buffer_elements = static_cast<std::uint32_t>(tlas.m_prebuild_info.ResultDataMaxSizeInBytes) / sizeof(std::uint32_t);\n\t\t\t\ttlas.m_fallback_tlas_ptr = internal::CreateFallbackWrappedPointer(device, desc_heap, fallback_heap_idx, tlas.m_natives[0], num_buffer_elements);\n\t\t\t}\n\t\t}\n\n\t\tinline void CopyInstDescResource(CommandList* cmd_list, AccelerationStructure& as, std::uint32_t source_index, std::uint32_t target_index)\n\t\t{\n\t\t\tcmd_list->m_native->CopyResource(as.m_instance_descs[target_index], as.m_instance_descs[source_index]);\n\t\t}\n\n\t\tinline void CopyAS(Device* device, CommandList* cmd_list, AccelerationStructure& as, std::uint32_t source_index, std::uint32_t target_index)\n\t\t{\n\t\t\tif (GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t\t{\n\t\t\t\tcmd_list->m_native->CopyRaytracingAccelerationStructure(as.m_natives[target_index]->GetGPUVirtualAddress(), as.m_natives[source_index]->GetGPUVirtualAddress(), D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE);\n\t\t\t}\n\t\t\telse if (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\tcmd_list->m_native_fallback->CopyRaytracingAccelerationStructure(as.m_natives[target_index]->GetGPUVirtualAddress(), as.m_natives[source_index]->GetGPUVirtualAddress(), D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE);\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\n#include <DirectXMath.h>\n\n\t[[nodiscard]] AccelerationStructure CreateBottomLevelAccelerationStructures(Device* device,\n\t\tCommandList* cmd_list,\n\t\tDescriptorHeap* desc_heap,\n\t\tstd::vector<desc::GeometryDesc> geometry)\n\t{\n\t\tAccelerationStructure blas = {};\n\n\t\tstd::vector<D3D12_RAYTRACING_GEOMETRY_DESC> geometry_descs(geometry.size());\n\t\tfor (auto i = 0; i < geometry.size(); i++)\n\t\t{\n\t\t\tauto geom = geometry[i];\n\n\t\t\tgeometry_descs[i].Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;\n\t\t\tif (auto index_buffer = geom.index_buffer.value_or(nullptr))\n\t\t\t{\n\t\t\t\tgeometry_descs[i].Triangles.IndexBuffer = index_buffer->m_buffer->GetGPUVirtualAddress() + (geom.m_indices_offset * index_buffer->m_stride_in_bytes);\n\t\t\t\tgeometry_descs[i].Triangles.IndexCount = geom.m_num_indices;\n\t\t\t\tgeometry_descs[i].Triangles.IndexFormat = DXGI_FORMAT_R32_UINT;\n\t\t\t\tgeometry_descs[i].Triangles.Transform3x4 = 0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tgeometry_descs[i].Triangles.IndexBuffer = 0;\n\t\t\t\tgeometry_descs[i].Triangles.IndexCount = 0;\n\t\t\t\tgeometry_descs[i].Triangles.IndexFormat = DXGI_FORMAT_UNKNOWN;\n\t\t\t\tgeometry_descs[i].Triangles.Transform3x4 = 0;\n\t\t\t}\n\t\t\tgeometry_descs[i].Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;\n\t\t\tgeometry_descs[i].Triangles.VertexCount = geom.m_num_vertices;\n\t\t\tgeometry_descs[i].Triangles.VertexBuffer.StartAddress = geom.vertex_buffer->m_buffer->GetGPUVirtualAddress() + (geom.m_vertices_offset * geom.m_vertex_stride);\n\t\t\tgeometry_descs[i].Triangles.VertexBuffer.StrideInBytes = geom.m_vertex_stride;\n\t\t\tgeometry_descs[i].Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE;\n\t\t}\n\n\t\tD3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS build_flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE;\n\n\t\t// Get prebuild info bottom level\n\t\tD3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS bottom_level_inputs;\n\t\tbottom_level_inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;\n\t\tbottom_level_inputs.NumDescs = static_cast<std::uint32_t>(geometry.size());\n\t\tbottom_level_inputs.pGeometryDescs = geometry_descs.data();\n\t\tbottom_level_inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;\n\t\tbottom_level_inputs.Flags = build_flags;\n\n\t\t// Get bottom level prebuild info\n\t\tinternal::UpdatePrebuildInfo(device, blas, bottom_level_inputs);\n\n\t\t// Allocate scratch resource\n\t\tinternal::AllocateUAVBuffer(device,\n\t\t\tblas.m_prebuild_info.ScratchDataSizeInBytes,\n\t\t\t&blas.m_scratch,\n\t\t\tD3D12_RESOURCE_STATE_UNORDERED_ACCESS,\n\t\t\tL\"Acceleration Structure Scratch Resource\");\n\n\t\t// Allocate resources for acceleration structures.\n\t\tfor (auto& as : blas.m_natives) {\n\t\t\tD3D12_RESOURCE_STATES initial_resource_state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE;\n\t\t\tif (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\tinitial_resource_state = device->m_fallback_native->GetAccelerationStructureResourceState();\n\t\t\t}\n\n\t\t\tinternal::AllocateUAVBuffer(device, blas.m_prebuild_info.ResultDataMaxSizeInBytes, &as, initial_resource_state, L\"BottomLevelAccelerationStructure\");\n\t\t}\n\n\t\t// Bottom Level Acceleration Structure desc\n\t\tD3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC bottom_level_build_desc = {};\n\t\t{\n\t\t\tbottom_level_build_desc.Inputs = bottom_level_inputs;\n\t\t\tbottom_level_build_desc.ScratchAccelerationStructureData = blas.m_scratch->GetGPUVirtualAddress();\n\t\t\tbottom_level_build_desc.DestAccelerationStructureData = blas.m_natives[0]->GetGPUVirtualAddress();\n\t\t}\n\n\t\tinternal::BuildAS(device, cmd_list, desc_heap, bottom_level_build_desc);\n\t\td3d12::UAVBarrierAS(cmd_list, blas, 0);\n\n\t\tfor (std::uint8_t i = 1; i < settings::num_back_buffers; i++)\n\t\t{\n\t\t\tinternal::CopyAS(device, cmd_list, blas, 0, i);\n\t\t}\n\n\t\treturn blas;\n\t}\n\n\tAccelerationStructure CreateTopLevelAccelerationStructure(Device* device,\n\t\tCommandList* cmd_list,\n\t\tDescriptorHeap* desc_heap,\n\t\tstd::vector<desc::BlasDesc> blas_list)\n\t{\n\t\tAccelerationStructure tlas = {};\n\n\t\tD3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS build_flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE | D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE;\n\n\t\tD3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS top_level_inputs;\n\t\ttop_level_inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;\n\t\ttop_level_inputs.Flags = build_flags;\n\t\ttop_level_inputs.NumDescs = static_cast<std::uint32_t>(blas_list.size());\n\t\ttop_level_inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;\n\n\t\t// Get prebuild info top level\n\t\tinternal::UpdatePrebuildInfo(device, tlas, top_level_inputs);\n\n\t\t// Allocate scratch resource\n\t\tinternal::AllocateUAVBuffer(device,\n\t\t\ttlas.m_prebuild_info.ScratchDataSizeInBytes,\n\t\t\t&tlas.m_scratch,\n\t\t\tD3D12_RESOURCE_STATE_UNORDERED_ACCESS,\n\t\t\tL\"Acceleration Structure Scratch Resource\");\n\n\n\t\t// Allocate acceleration structure buffer\n\t\tfor (auto& as : tlas.m_natives) {\n\t\t\tD3D12_RESOURCE_STATES initial_resoruce_state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE;\n\t\t\tif (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\tinitial_resoruce_state = device->m_fallback_native->GetAccelerationStructureResourceState();\n\t\t\t}\n\n\t\t\tinternal::AllocateUAVBuffer(device, tlas.m_prebuild_info.ResultDataMaxSizeInBytes, &as, initial_resoruce_state, L\"TopLevelAccelerationStructure\");\n\t\t}\n\n\t\t// Create the instances to the bottom level instances\n\t\tif (!blas_list.empty())\n\t\t{\n\t\t\tinternal::CreateInstancesForTLAS(device, tlas, desc_heap, blas_list, 0, false);\n\t\t}\n\n\t\t// Top Level Acceleration Structure desc\n\t\tD3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC top_level_build_desc = {};\n\t\t{\n\t\t\tif (!blas_list.empty())\n\t\t\t{\n\t\t\t\ttop_level_inputs.InstanceDescs = tlas.m_instance_descs[0]->GetGPUVirtualAddress();\n\t\t\t}\n\t\t\ttop_level_build_desc.Inputs = top_level_inputs;\n\t\t\ttop_level_build_desc.DestAccelerationStructureData = tlas.m_natives[0]->GetGPUVirtualAddress();\n\t\t\ttop_level_build_desc.ScratchAccelerationStructureData = tlas.m_scratch->GetGPUVirtualAddress();\n\t\t}\n\n\t\tinternal::BuildAS(device, cmd_list, desc_heap, top_level_build_desc);\n\n\t\td3d12::UAVBarrierAS(cmd_list, tlas, 0);\n\t\tfor (std::uint8_t i = 1; i < settings::num_back_buffers; i++)\n\t\t{\n\t\t\tinternal::CopyAS(device, cmd_list, tlas, 0, i);\n\t\t}\n\n\t\treturn tlas;\n\t}\n\n\tvoid DestroyAccelerationStructure(AccelerationStructure& structure)\n\t{\n\t\tSAFE_RELEASE(structure.m_scratch);\n\n\t\tfor (auto& as : structure.m_natives)\n\t\t{\n\t\t\tSAFE_RELEASE(as);\n\t\t}\n\t\tfor (auto& inst_desc : structure.m_instance_descs)\n\t\t{\n\t\t\tSAFE_RELEASE(inst_desc);\n\t\t}\n\t}\n\n\tvoid UAVBarrierAS(CommandList* cmd_list, AccelerationStructure const & structure, std::uint32_t frame_idx)\n\t{\n\t\tauto barrier = CD3DX12_RESOURCE_BARRIER::UAV(structure.m_natives[frame_idx]);\n\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\t}\n\n\tvoid UpdateTopLevelAccelerationStructure(AccelerationStructure& tlas, Device* device,\n\t\tCommandList* cmd_list,\n\t\tDescriptorHeap* desc_heap,\n\t\tstd::vector<desc::BlasDesc> blas_list,\n\t\tstd::uint32_t frame_idx)\n\t{\n\t\tD3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS build_flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE | D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE;\n\n\t\tD3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS top_level_inputs;\n\t\ttop_level_inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;\n\t\ttop_level_inputs.Flags = build_flags;\n\t\ttop_level_inputs.NumDescs = static_cast<std::uint32_t>(blas_list.size());\n\t\ttop_level_inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;\n\n\t\tD3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO old_prebuild_info = tlas.m_prebuild_info;\n\n\t\tinternal::UpdatePrebuildInfo(device, tlas, top_level_inputs);\n\n\t\ttop_level_inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE;\n\n\t\tbool rebuild_accel_structure = old_prebuild_info.ResultDataMaxSizeInBytes != tlas.m_prebuild_info.ResultDataMaxSizeInBytes ||\n\t\t\told_prebuild_info.ScratchDataSizeInBytes != tlas.m_prebuild_info.ScratchDataSizeInBytes ||\n\t\t\told_prebuild_info.UpdateScratchDataSizeInBytes != tlas.m_prebuild_info.UpdateScratchDataSizeInBytes;\n\n\t\tif (rebuild_accel_structure)\n\t\t{\n\t\t\tLOGW(\"Complete AS rebuild triggered. This might break versioining\");\n\t\t\ttlas = CreateTopLevelAccelerationStructure(device, cmd_list, desc_heap, blas_list);\n\t\t}\n\t\telse\n\t\t{\n\t\t\t// Create the instances to the bottom level instances.\n\t\t\tif (!blas_list.empty())\n\t\t\t{\n\t\t\t\tinternal::CreateInstancesForTLAS(device, tlas, desc_heap, blas_list, frame_idx, true);\n\t\t\t}\n\n\t\t\t// Top Level Acceleration Structure desc\n\t\t\tD3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC top_level_build_desc = {};\n\t\t\t{\n\t\t\t\tauto barrier = CD3DX12_RESOURCE_BARRIER::UAV(tlas.m_natives[frame_idx]);\n\t\t\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\n\t\t\t\tif (!blas_list.empty())\n\t\t\t\t{\n\t\t\t\t\ttop_level_inputs.InstanceDescs = tlas.m_instance_descs[frame_idx]->GetGPUVirtualAddress();\n\t\t\t\t}\n\t\t\t\ttop_level_build_desc.Inputs = top_level_inputs;\n\t\t\t\ttop_level_build_desc.SourceAccelerationStructureData = tlas.m_natives[frame_idx]->GetGPUVirtualAddress(); //TODO: Benchmark performance when taking the previous source.\n\t\t\t\ttop_level_build_desc.DestAccelerationStructureData = tlas.m_natives[frame_idx]->GetGPUVirtualAddress();\n\t\t\t\ttop_level_build_desc.ScratchAccelerationStructureData = tlas.m_scratch->GetGPUVirtualAddress();\n\t\t\t}\n\n\t\t\tinternal::BuildAS(device, cmd_list, desc_heap, top_level_build_desc);\n\t\t}\n\t}\n\n\tvoid SetName(AccelerationStructure& acceleration_structure, std::wstring name)\n\t{\n\t\tfor (auto& as : acceleration_structure.m_natives)\n\t\t{\n\t\t\tas->SetName((name + L\" - Acceleration Structure\").c_str());\n\t\t}\n\t\tfor (auto& inst_desc : acceleration_structure.m_instance_descs)\n\t\t{\n\t\t\tif (inst_desc)\n\t\t\t{\n\t\t\t\tinst_desc->SetName((name + L\" - Acceleration Structure\").c_str());\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid CreateOrUpdateTLAS(Device* device, CommandList* cmd_list, bool& requires_init, d3d12::AccelerationStructure& out_tlas,\n\t\tstd::vector<desc::BlasDesc> blas_list, std::uint32_t frame_idx)\n\t{\n\t\td3d12::DescriptorHeap* heap = static_cast<d3d12::CommandList*>(cmd_list)->m_rt_descriptor_heap->GetHeap();\n\n\t\tif (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t{\n\t\t\tif (requires_init)\n\t\t\t{\n\t\t\t\tout_tlas = d3d12::CreateTopLevelAccelerationStructure(device, cmd_list, heap, blas_list);\n\n\t\t\t\trequires_init = false;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\td3d12::UpdateTopLevelAccelerationStructure(out_tlas, device, cmd_list, heap, blas_list, frame_idx);\n\t\t\t}\n\t\t}\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_command_list.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n#include \"d3d12_dynamic_descriptor_heap.hpp\"\n#include \"d3d12_rt_descriptor_heap.hpp\"\n#include \"d3d12_texture_resources.hpp\"\n\n#include \"../util/log.hpp\"\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\tCommandList* CreateCommandList(Device* device, unsigned int num_allocators, CmdListType type)\n\t{\n\t\tauto* cmd_list = new CommandList();\n\t\tconst auto n_device = device->m_native;\n\t\tauto& n_cmd_list = cmd_list->m_native;\n\n\t\tcmd_list->m_allocators.resize(num_allocators);\n\n\t\t// Create the allocators\n\t\tfor (auto& allocator : cmd_list->m_allocators)\n\t\t{\n\t\t\tTRY_M(n_device->CreateCommandAllocator((D3D12_COMMAND_LIST_TYPE)type, IID_PPV_ARGS(&allocator)),\n\t\t\t\t\"Failed to create command allocator\");\n\t\t\tNAME_D3D12RESOURCE(allocator);\n\t\t}\n\n\t\t// Create the command lists\n\t\tTRY_M(device->m_native->CreateCommandList(\n\t\t\t0,\n\t\t\t(D3D12_COMMAND_LIST_TYPE)type,\n\t\t\tcmd_list->m_allocators[0],\n\t\t\tNULL,\n\t\t\tIID_PPV_ARGS(&n_cmd_list)\n\t\t), \"Failed to create command list\");\n\t\tNAME_D3D12RESOURCE(n_cmd_list);\n\t\tn_cmd_list->Close(); // TODO: Can be optimized away.\n\n\t\tif (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t{\n\t\t\tdevice->m_fallback_native->QueryRaytracingCommandList(n_cmd_list, IID_PPV_ARGS(&cmd_list->m_native_fallback));\n\t\t}\n\n\t\t//Create the heaps\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tcmd_list->m_dynamic_descriptor_heaps[i] = std::make_unique<DynamicDescriptorHeap>(device, static_cast<DescriptorHeapType>(i));\n\t\t\tcmd_list->m_descriptor_heaps[i] = nullptr;\n\t\t}\n\n\t\tcmd_list->m_rt_descriptor_heap = std::make_shared<RTDescriptorHeap>(device, DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\n\t\treturn cmd_list;\n\t}\n\n\tvoid SetName(CommandList* cmd_list, std::string const& name)\n\t{\n\t\tSetName(cmd_list, std::wstring(name.begin(), name.end()));\n\t}\n\n\tvoid SetName(CommandList* cmd_list, std::wstring const& name)\n\t{\n\t\tcmd_list->m_native->SetName(name.c_str());\n\n\t\tfor (auto& allocator : cmd_list->m_allocators)\n\t\t{\n\t\t\tallocator->SetName((name + L\" Allocator\").c_str());\n\t\t}\n\t}\n\n\tvoid Begin(CommandList* cmd_list, unsigned int frame_idx)\n\t{\n\t\t// TODO: move resetting to when the command list is executed. This is how vulkan does it.\n\t\tTRY_M(cmd_list->m_allocators[frame_idx]->Reset(),\n\t\t\t\"Failed to reset cmd allocators\");\n\n\t\t// Only reset with pipeline state if using bundles since only then this will impact fps.\n\t\t// Otherwise its just easier to pass NULL and suffer the insignificant performance loss.\n\t\tTRY_M(cmd_list->m_native->Reset(cmd_list->m_allocators[frame_idx], NULL),\n\t\t\t\"Failed to reset command list.\");\n\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tcmd_list->m_dynamic_descriptor_heaps[i]->Reset();\n\t\t\tcmd_list->m_descriptor_heaps[i] = nullptr;\n\t\t}\n\n\t\tcmd_list->m_rt_descriptor_heap->Reset(frame_idx);\n\t}\n\n\tvoid End(CommandList* cmd_list)\n\t{\n\t\tcmd_list->m_native->Close();\n\t}\n\n\tvoid ExecuteBundle(CommandList* cmd_list, CommandList* bundle)\n\t{\n\t\tcmd_list->m_native->ExecuteBundle(bundle->m_native);\n\t}\n\n\tvoid ExecuteIndirect(CommandList* cmd_list, CommandSignature* cmd_signature, IndirectCommandBuffer* buffer, uint32_t frame_idx)\n\t{\n\t\tcmd_list->m_native->ExecuteIndirect(cmd_signature->m_native, static_cast<unsigned int>(buffer->m_num_commands), buffer->m_native[frame_idx], 0, nullptr, 0);\n\t}\n\n\tvoid BindRenderTarget(CommandList* cmd_list, RenderTarget* render_target, bool clear, bool clear_depth)\n\t{\n\t\tstd::vector<CD3DX12_CPU_DESCRIPTOR_HANDLE> handles;\n\t\thandles.resize(render_target->m_render_targets.size());\n\n\t\tfor (auto i = 0; i < handles.size(); i++)\n\t\t{\n\t\t\thandles[i] = CD3DX12_CPU_DESCRIPTOR_HANDLE(render_target->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart(), i, render_target->m_rtv_descriptor_increment_size);\n\t\t}\n\n\t\tCD3DX12_CPU_DESCRIPTOR_HANDLE dsv_handle;\n\n\t\tif (render_target->m_create_info.m_create_dsv_buffer)\n\t\t{\n\t\t\tdsv_handle = render_target->m_depth_stencil_resource_heap->GetCPUDescriptorHandleForHeapStart();\n\t\t}\n\n\t\tcmd_list->m_native->OMSetRenderTargets(static_cast<unsigned int>(handles.size()), handles.data(), false, render_target->m_create_info.m_create_dsv_buffer ? &dsv_handle : nullptr);\n\t\tif (clear)\n\t\t{\n\t\t\tfor (auto& handle : handles)\n\t\t\t{\n\t\t\t\tcmd_list->m_native->ClearRenderTargetView(handle, render_target->m_create_info.m_clear_color, 0, nullptr);\n\t\t\t}\n\t\t}\n\t\tif (clear_depth && render_target->m_create_info.m_create_dsv_buffer)\n\t\t{\n\t\t\tcmd_list->m_native->ClearDepthStencilView(dsv_handle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);\n\t\t}\n\t}\n\n\tvoid BindRenderTargetVersioned(CommandList* cmd_list, RenderTarget* render_target, unsigned int frame_idx, bool clear, bool clear_depth)\n\t{\n\t\tCD3DX12_CPU_DESCRIPTOR_HANDLE rtv_handle(render_target->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart(),\n\t\t\tframe_idx % render_target->m_render_targets.size(), render_target->m_rtv_descriptor_increment_size);\n\n\t\tCD3DX12_CPU_DESCRIPTOR_HANDLE dsv_handle;\n\n\t\tif (render_target->m_create_info.m_create_dsv_buffer)\n\t\t{\n\t\t\tdsv_handle = render_target->m_depth_stencil_resource_heap->GetCPUDescriptorHandleForHeapStart();\n\t\t}\n\n\t\tcmd_list->m_native->OMSetRenderTargets(1, &rtv_handle, false, render_target->m_create_info.m_create_dsv_buffer ? &dsv_handle : nullptr);\n\n\t\tif (clear)\n\t\t{\n\t\t\tcmd_list->m_native->ClearRenderTargetView(rtv_handle, render_target->m_create_info.m_clear_color, 0, nullptr);\n\t\t}\n\n\t\tif (clear_depth && render_target->m_create_info.m_create_dsv_buffer)\n\t\t{\n\t\t\tcmd_list->m_native->ClearDepthStencilView(dsv_handle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);\n\t\t}\n\t}\n\n\tvoid BindRenderTargetOnlyDepth(CommandList* cmd_list, RenderTarget* render_target, bool clear)\n\t{\n\t\tCD3DX12_CPU_DESCRIPTOR_HANDLE dsv_handle;\n\n\t\tif (render_target->m_create_info.m_create_dsv_buffer)\n\t\t{\n\t\t\tdsv_handle = render_target->m_depth_stencil_resource_heap->GetCPUDescriptorHandleForHeapStart();\n\t\t}\n\n\t\tcmd_list->m_native->OMSetRenderTargets(0, nullptr, false, render_target->m_create_info.m_create_dsv_buffer ? &dsv_handle : nullptr);\n\n\t\tif (clear)\n\t\t{\n\t\t\tcmd_list->m_native->ClearDepthStencilView(dsv_handle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);\n\t\t}\n\t}\n\n\tvoid BindPipeline(CommandList* cmd_list, PipelineState* pipeline_state) // TODO: Binding the root signature seperatly can improve perf if done right.\n\t{\n\t\tcmd_list->m_native->SetPipelineState(pipeline_state->m_native);\n\t\tcmd_list->m_native->SetGraphicsRootSignature(pipeline_state->m_root_signature->m_native);\n\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tcmd_list->m_dynamic_descriptor_heaps[i]->ParseRootSignature(*pipeline_state->m_root_signature);\n\t\t}\n\t}\n\n\tvoid BindDescriptorHeap(CommandList* cmd_list, DescriptorHeap* heap, DescriptorHeapType type, unsigned int frame_idx, bool fallback)\n\t{\n\t\tstd::uint32_t heap_idx = frame_idx % heap->m_create_info.m_versions;\n\n\t\tif (cmd_list->m_descriptor_heaps[static_cast<size_t>(type)] != heap->m_native[heap_idx])\n\t\t{\n\t\t\tcmd_list->m_descriptor_heaps[static_cast<size_t>(type)] = heap->m_native[heap_idx];\n\t\t\tBindDescriptorHeaps(cmd_list, fallback);\n\t\t}\n\t}\n\n\tvoid BindDescriptorHeaps(CommandList* cmd_list, bool fallback)\n\t{\n\t\tstd::uint32_t num_heaps = 0;\n\t\tID3D12DescriptorHeap* n_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES] = {};\n\n\t\tfor (uint32_t i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tID3D12DescriptorHeap* heap = cmd_list->m_descriptor_heaps[i];\n\t\t\tif (heap)\n\t\t\t{\n\t\t\t\tn_heaps[num_heaps++] = heap;\n\t\t\t}\n\t\t}\n\n\t\tif (fallback)\n\t\t{\n\t\t\tcmd_list->m_native_fallback->SetDescriptorHeaps(num_heaps, n_heaps);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcmd_list->m_native->SetDescriptorHeaps(num_heaps, n_heaps);\n\t\t}\n\t}\n\n\tvoid BindComputePipeline(CommandList* cmd_list, PipelineState * pipeline_state)\n\t{\n\t\tif (pipeline_state->m_desc.m_type != PipelineType::COMPUTE_PIPELINE)\n\t\t\tLOGW(\"Tried to bind a graphics pipeline as a compute pipeline\");\n\t\tcmd_list->m_native->SetPipelineState(pipeline_state->m_native);\n\t\tcmd_list->m_native->SetComputeRootSignature(pipeline_state->m_root_signature->m_native);\n\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tcmd_list->m_dynamic_descriptor_heaps[i]->ParseRootSignature(*pipeline_state->m_root_signature);\n\t\t}\n\t}\n\n\tvoid BindRaytracingPipeline(CommandList* cmd_list, StateObject* state_object, bool fallback)\n\t{\n\t\tcmd_list->m_native->SetComputeRootSignature(state_object->m_global_root_signature->m_native);\n\t\tif (fallback)\n\t\t{\n\t\t\tcmd_list->m_native_fallback->SetPipelineState1(state_object->m_fallback_native);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcmd_list->m_native->SetPipelineState1(state_object->m_native);\n\t\t}\n\n\t\tcmd_list->m_rt_descriptor_heap->ParseRootSignature(*state_object->m_global_root_signature);\n\t}\n\n\tvoid BindViewport(CommandList* cmd_list, Viewport const & viewport)\n\t{\n\t\tcmd_list->m_native->RSSetViewports(1, &viewport.m_viewport);\n\t\tcmd_list->m_native->RSSetScissorRects(1, &viewport.m_scissor_rect);\n\t}\n\n\tvoid BindVertexBuffer(CommandList* cmd_list, StagingBuffer* buffer, std::size_t offset, std::size_t size, std::size_t stride)\n\t{\n\t\tif (!buffer->m_gpu_address)\n\t\t{\n\t\t\tLOGC(\"No GPU address found. Has the buffer beens staged?\");\n\t\t}\n\n\t\tD3D12_VERTEX_BUFFER_VIEW view;\n\t\tview.BufferLocation = buffer->m_gpu_address + offset;\n\t\tview.StrideInBytes = static_cast<UINT>(stride);\n\t\tview.SizeInBytes = static_cast<UINT>(size);\n\n\t\tcmd_list->m_native->IASetVertexBuffers(0, 1, &view);\n\t}\n\n\tvoid BindIndexBuffer(CommandList* cmd_list, StagingBuffer* buffer, std::uint32_t offset, std::uint32_t size)\n\t{\n\t\tif (!buffer->m_gpu_address)\n\t\t{\n\t\t\tthrow \"No GPU address found. Has the buffer beens staged?\";\n\t\t}\n\n\t\tD3D12_INDEX_BUFFER_VIEW view;\n\t\tview.BufferLocation = buffer->m_gpu_address + offset;\n\t\tview.Format = DXGI_FORMAT_R32_UINT;\n\t\tview.SizeInBytes = size;\n\n\t\tcmd_list->m_native->IASetIndexBuffer(&view);\n\t}\n\n\tvoid BindConstantBuffer(CommandList* cmd_list, HeapResource* buffer, unsigned int root_parameter_idx, unsigned int frame_idx)\n\t{\n\t\tcmd_list->m_native->SetGraphicsRootConstantBufferView(root_parameter_idx, buffer->m_gpu_addresses[frame_idx]);\n\t}\n\n\tvoid Bind32BitConstants(CommandList* cmd_list, const void* data_to_set, unsigned int num_of_values_to_set, unsigned int dest_offset_in_32bit_values, unsigned int root_parameter_idx)\n\t{\n\t\tcmd_list->m_native->SetGraphicsRoot32BitConstants(root_parameter_idx, num_of_values_to_set, data_to_set, dest_offset_in_32bit_values);\n\t}\n\n\tvoid BindCompute32BitConstants(CommandList* cmd_list, const void* data_to_set, unsigned int num_of_values_to_set, unsigned int dest_offset_in_32bit_values, unsigned int root_parameter_idx)\n\t{\n\t\tcmd_list->m_native->SetComputeRoot32BitConstants(root_parameter_idx, num_of_values_to_set, data_to_set, dest_offset_in_32bit_values);\n\t}\n\n\tvoid BindComputeConstantBuffer(CommandList * cmd_list, HeapResource* buffer, unsigned int root_parameter_idx, unsigned int frame_idx)\n\t{\n\t\tcmd_list->m_native->SetComputeRootConstantBufferView(root_parameter_idx,\n\t\t\tbuffer->m_gpu_addresses[frame_idx]);\n\t}\n\n\tvoid BindComputeShaderResourceView(CommandList * cmd_list, ID3D12Resource* resource, unsigned int root_parameter_idx)\n\t{\n\t\tcmd_list->m_native->SetComputeRootShaderResourceView(root_parameter_idx, resource->GetGPUVirtualAddress());\n\t}\n\n\tvoid BindComputeUnorederedAccessView(CommandList * cmd_list, ID3D12Resource* resource, unsigned int root_parameter_idx)\n\t{\n\t\tcmd_list->m_native->SetComputeRootUnorderedAccessView(root_parameter_idx, resource->GetGPUVirtualAddress());\n\t}\n\n\tvoid BindDescriptorTable(CommandList* cmd_list, DescHeapGPUHandle& handle, unsigned int root_param_index)\n\t{\n\t\tcmd_list->m_native->SetGraphicsRootDescriptorTable(root_param_index, handle.m_native);\n\t}\n\n\tvoid BindComputeDescriptorTable(CommandList * cmd_list, DescHeapGPUHandle & handle, unsigned int root_param_index)\n\t{\n\t\tcmd_list->m_native->SetComputeRootDescriptorTable(root_param_index, handle.m_native);\n\t}\n\n\tvoid SetPrimitiveTopology(CommandList* cmd_list, D3D12_PRIMITIVE_TOPOLOGY topology)\n\t{\n\t\tcmd_list->m_native->IASetPrimitiveTopology(topology);\n\t}\n\n\tvoid Draw(CommandList* cmd_list, std::uint32_t vertex_count, std::uint32_t inst_count, std::uint32_t vertex_start)\n\t{\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tcmd_list->m_dynamic_descriptor_heaps[i]->CommitStagedDescriptorsForDraw(*cmd_list);\n\t\t}\n\n\t\tcmd_list->m_native->DrawInstanced(vertex_count, inst_count, vertex_start, 0);\n\t}\n\n\tvoid DrawIndexed(CommandList* cmd_list, std::uint32_t idx_count, std::uint32_t inst_count, std::uint32_t idx_start, std::uint32_t vertex_start)\n\t{\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tcmd_list->m_dynamic_descriptor_heaps[i]->CommitStagedDescriptorsForDraw(*cmd_list);\n\t\t}\n\n\t\tcmd_list->m_native->DrawIndexedInstanced(idx_count, inst_count, idx_start, vertex_start, 0);\n\t}\n\n\tvoid Dispatch(CommandList * cmd_list, unsigned int thread_group_count_x, unsigned int thread_group_count_y, unsigned int thread_group_count_z)\n\t{\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tcmd_list->m_dynamic_descriptor_heaps[i]->CommitStagedDescriptorsForDispatch(*cmd_list);\n\t\t}\n\n\t\tcmd_list->m_native->Dispatch(thread_group_count_x, thread_group_count_y, thread_group_count_z);\n\t}\n\n\tvoid Transition(CommandList* cmd_list, RenderTarget* render_target, unsigned int frame_index, ResourceState from, ResourceState to)\n\t{\n\t\tCD3DX12_RESOURCE_BARRIER end_transition = CD3DX12_RESOURCE_BARRIER::Transition(\n\t\t\trender_target->m_render_targets[frame_index % render_target->m_render_targets.size()],\n\t\t\t(D3D12_RESOURCE_STATES)from,\n\t\t\t(D3D12_RESOURCE_STATES)to\n\t\t);\n\n\t\tcmd_list->m_native->ResourceBarrier(1, &end_transition);\n\t}\n\n\tvoid Transition(CommandList* cmd_list, RenderTarget* render_target, ResourceState from, ResourceState to)\n\t{\n\t\tstd::vector<CD3DX12_RESOURCE_BARRIER> barriers;\n\t\tbarriers.resize(render_target->m_num_render_targets);\n\t\tfor (auto i = 0u; i < render_target->m_num_render_targets; i++)\n\t\t{\n\t\t\tCD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(\n\t\t\t\trender_target->m_render_targets[i],\n\t\t\t\t(D3D12_RESOURCE_STATES)from,\n\t\t\t\t(D3D12_RESOURCE_STATES)to\n\t\t\t);\n\n\t\t\tbarriers[i] = barrier;\n\t\t}\n\t\tcmd_list->m_native->ResourceBarrier(static_cast<unsigned int>(barriers.size()), barriers.data());\n\t}\n\n\tvoid Transition(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to)\n\t{\n\t\tif (texture->m_subresource_states[0] != to)\n\t\t{\n\t\t\tCD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(texture->m_resource,\n\t\t\t\t(D3D12_RESOURCE_STATES)from,\n\t\t\t\t(D3D12_RESOURCE_STATES)to);\n\n\t\t\tstd::fill(texture->m_subresource_states.begin(), texture->m_subresource_states.end(), to);\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\t\t}\n\t}\n\n\tvoid Transition(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to, unsigned int first_subresource, unsigned int num_subresources)\n\t{\n\t\tif (num_subresources < D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES)\n\t\t{\n\t\t\tfor (uint32_t i = 0; i < num_subresources; ++i)\n\t\t\t{\n\t\t\t\tTransitionSubresource(cmd_list, texture, from, to, first_subresource + i);\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tTransition(cmd_list, texture, from, to);\n\t\t}\n\t}\n\n\tvoid TransitionSubresource(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to, unsigned int subresource)\n\t{\n\t\tif (texture->m_subresource_states[subresource] != to)\n\t\t{\n\t\t\tCD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(texture->m_resource,\n\t\t\t\t(D3D12_RESOURCE_STATES)from,\n\t\t\t\t(D3D12_RESOURCE_STATES)to, subresource);\n\n\t\t\ttexture->m_subresource_states[subresource] = to;\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\t\t}\n\t}\n\n\tvoid Transition(CommandList* cmd_list, std::vector<TextureResource*> const& textures, ResourceState from, ResourceState to)\n\t{\n\t\tstd::vector<CD3DX12_RESOURCE_BARRIER> barriers;\n\n\t\tfor (auto texture : textures)\n\t\t{\n\t\t\tif (texture->m_subresource_states[0] != to)\n\t\t\t{\n\t\t\t\tCD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(\n\t\t\t\t\ttexture->m_resource,\n\t\t\t\t\t(D3D12_RESOURCE_STATES)from,\n\t\t\t\t\t(D3D12_RESOURCE_STATES)to\n\t\t\t\t);\n\n\t\t\t\tstd::fill(texture->m_subresource_states.begin(), texture->m_subresource_states.end(), to);\n\n\t\t\t\tbarriers.push_back(barrier);\n\t\t\t}\n\t\t}\n\n\t\tcmd_list->m_native->ResourceBarrier(static_cast<unsigned int>(barriers.size()), barriers.data());\n\t}\n\n\tvoid Transition(CommandList* cmd_list, IndirectCommandBuffer* buffer, ResourceState from, ResourceState to, uint32_t frame_idx)\n\t{\n\t\tCD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(\n\t\t\tbuffer->m_native[frame_idx],\n\t\t\t(D3D12_RESOURCE_STATES)from,\n\t\t\t(D3D12_RESOURCE_STATES)to\n\t\t);\n\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\t}\n\n\tvoid Transition(CommandList* cmd_list, StagingBuffer* buffer, ResourceState from, ResourceState to)\n\t{\n\t\tCD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(\n\t\t\tbuffer->m_buffer,\n\t\t\t(D3D12_RESOURCE_STATES)from,\n\t\t\t(D3D12_RESOURCE_STATES)to\n\t\t);\n\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\t}\n\n\tvoid TransitionDepth(CommandList* cmd_list, RenderTarget* render_target, ResourceState from, ResourceState to)\n\t{\n\t\tCD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(\n\t\t\trender_target->m_depth_stencil_buffer,\n\t\t\t(D3D12_RESOURCE_STATES)from,\n\t\t\t(D3D12_RESOURCE_STATES)to\n\t\t);\n\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\t}\n\n\tvoid UAVBarrier(CommandList* cmd_list, std::vector<TextureResource*> const & resources)\n\t{\n\t\tstd::vector<CD3DX12_RESOURCE_BARRIER> barriers;\n\t\tbarriers.reserve(resources.size());\n\t\tfor (auto& resource : resources)\n\t\t{\n\t\t\tbarriers.emplace_back(CD3DX12_RESOURCE_BARRIER::UAV(resource->m_resource));\n\t\t}\n\n\t\tcmd_list->m_native->ResourceBarrier(static_cast<unsigned int>(barriers.size()), barriers.data());\n\t}\n\n\tvoid UAVBarrier(CommandList* cmd_list, std::vector<ID3D12Resource*> const & resources)\n\t{\n\t\tstd::vector<CD3DX12_RESOURCE_BARRIER> barriers;\n\t\tbarriers.reserve(resources.size());\n\t\tfor (auto& resource : resources)\n\t\t{\n\t\t\tbarriers.emplace_back(CD3DX12_RESOURCE_BARRIER::UAV(resource));\n\t\t}\n\n\t\tcmd_list->m_native->ResourceBarrier(static_cast<unsigned int>(barriers.size()), barriers.data());\n\t}\n\n\tvoid Alias(CommandList* cmd_list, TextureResource* resource_before, TextureResource* resource_after)\n\t{\n\t\tauto before = (resource_before) ? resource_before->m_resource : nullptr;\n\t\tauto after = (resource_after) ? resource_after->m_resource : nullptr;\n\n\t\tCD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Aliasing(before, after);\n\n\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\t}\n\n\tvoid Destroy(CommandList* cmd_list)\n\t{\n\t\tSAFE_RELEASE(cmd_list->m_native);\n\t\tSAFE_RELEASE(cmd_list->m_native_fallback)\n\t\t\tfor (auto& allocator : cmd_list->m_allocators)\n\t\t\t{\n\t\t\t\tSAFE_RELEASE(allocator);\n\t\t\t}\n\t\tdelete cmd_list;\n\t}\n\n\tCommandSignature* CreateCommandSignature(Device* device, RootSignature* root_signature, std::vector<D3D12_INDIRECT_ARGUMENT_DESC> arg_descs, size_t byte_stride)\n\t{\n\t\tauto cmd_sig = new CommandSignature();\n\n\t\tD3D12_COMMAND_SIGNATURE_DESC cmd_signature_desc = {};\n\t\tcmd_signature_desc.pArgumentDescs = arg_descs.data();\n\t\tcmd_signature_desc.NumArgumentDescs = static_cast<unsigned int>(arg_descs.size());\n\t\tcmd_signature_desc.ByteStride = static_cast<unsigned int>(byte_stride);\n\n\t\tTRY_M(device->m_native->CreateCommandSignature(&cmd_signature_desc, root_signature->m_native, IID_PPV_ARGS(&cmd_sig->m_native))\n\t\t\t, \"Failed to create command signature\");\n\n\t\treturn cmd_sig;\n\t}\n\n\tvoid DispatchRays(CommandList* cmd_list, ShaderTable* hitgroup_table, ShaderTable* miss_table, ShaderTable* raygen_table, std::uint32_t width, std::uint32_t height, std::uint32_t depth, unsigned int frame_idx)\n\t{\n\t\tD3D12_DISPATCH_RAYS_DESC desc = {};\n\t\tif (hitgroup_table != nullptr)\n\t\t{\n\t\t\tdesc.HitGroupTable.StartAddress = hitgroup_table->m_resource->GetGPUVirtualAddress();\n\t\t\tdesc.HitGroupTable.SizeInBytes = hitgroup_table->m_resource->GetDesc().Width;\n\t\t\tdesc.HitGroupTable.StrideInBytes = hitgroup_table->m_shader_record_size;\n\t\t}\n\n\t\tdesc.MissShaderTable.StartAddress = miss_table->m_resource->GetGPUVirtualAddress();\n\t\tdesc.MissShaderTable.SizeInBytes = miss_table->m_resource->GetDesc().Width;\n\t\tdesc.MissShaderTable.StrideInBytes = miss_table->m_shader_record_size;\n\n\t\tdesc.RayGenerationShaderRecord.StartAddress = raygen_table->m_resource->GetGPUVirtualAddress();\n\t\tdesc.RayGenerationShaderRecord.SizeInBytes = raygen_table->m_resource->GetDesc().Width;\n\t\t// Dimensions of the image to render, identical to a window dimensions\n\t\tdesc.Width = width;\n\t\tdesc.Height = height;\n\t\tdesc.Depth = depth;\n\n\t\tcmd_list->m_rt_descriptor_heap->CommitStagedDescriptorsForDispatch(*cmd_list, frame_idx);\n\n\t\tif (cmd_list->m_native_fallback)\n\t\t{\n\t\t\tcmd_list->m_native_fallback->DispatchRays(&desc);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcmd_list->m_native->DispatchRays(&desc);\n\t\t}\n\t}\n\n\tvoid SetName(CommandSignature * cmd_signature, std::wstring name)\n\t{\n\t\tcmd_signature->m_native->SetName(name.c_str());\n\t}\n\n\tvoid Destroy(CommandSignature* cmd_signature)\n\t{\n\t\tSAFE_RELEASE(cmd_signature->m_native);\n\t\tdelete cmd_signature;\n\t}\n\n} /* wr::d3d12 */\n"
  },
  {
    "path": "src/d3d12/d3d12_command_queue.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"../util/log.hpp\"\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tCommandQueue* CreateCommandQueue(Device* device, CmdListType type)\n\t{\n\t\tauto cmd_queue = new CommandQueue();\n\t\tconst auto n_device = device->m_native;\n\n\t\tD3D12_COMMAND_QUEUE_DESC cmd_queue_desc = {};\n\t\tcmd_queue_desc.Flags = settings::enable_gpu_timeout ? D3D12_COMMAND_QUEUE_FLAG_NONE : D3D12_COMMAND_QUEUE_FLAG_DISABLE_GPU_TIMEOUT;\n\t\tcmd_queue_desc.Type = static_cast<D3D12_COMMAND_LIST_TYPE>(type);\n\n\t\tTRY_M(n_device->CreateCommandQueue(&cmd_queue_desc, IID_PPV_ARGS(&cmd_queue->m_native)),\n\t\t\t\"Failed to create DX12 command queue.\");\n\t\tNAME_D3D12RESOURCE(cmd_queue->m_native);\n\n\t\treturn cmd_queue;\n\t}\n\n\tvoid Execute(CommandQueue* cmd_queue, std::vector<CommandList*> const & cmd_lists, Fence* fence)\n\t{\n\t\tstd::vector<ID3D12CommandList*> native_lists;\n\t\tnative_lists.resize(cmd_lists.size());\n\t\tfor (auto i = 0; i < native_lists.size(); i++)\n\t\t{\n\t\t\tnative_lists[i] = cmd_lists[i]->m_native;\n\t\t}\n\n\t\tcmd_queue->m_native->ExecuteCommandLists(static_cast<unsigned int>(native_lists.size()), native_lists.data());\n\n\t\tfence->m_fence_value++;\n\t\tSignal(fence, cmd_queue);\n\n\t}\n\n\tvoid Destroy(CommandQueue* cmd_queue)\n\t{\n\t\tSAFE_RELEASE(cmd_queue->m_native);\n\t\tdelete cmd_queue;\n\t}\n\tvoid SetName(CommandQueue * cmd_queue, std::wstring name)\n\t{\n\t\tcmd_queue->m_native->SetName(name.c_str());\n\t}\n\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_constant_buffer_pool.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_constant_buffer_pool.hpp\"\n#include \"d3d12_functions.hpp\"\n#include \"d3d12_settings.hpp\"\n#include \"d3d12_renderer.hpp\"\n#include \"d3d12_defines.hpp\"\n\nnamespace wr \n{\n\tD3D12ConstantBufferPool::D3D12ConstantBufferPool(D3D12RenderSystem& render_system, std::size_t size_in_bytes) :\n\tConstantBufferPool(SizeAlignTwoPower(size_in_bytes, 65536)),\n\tm_render_system(render_system)\n\t{\n\t\tm_heap = d3d12::CreateHeap_SBO(render_system.m_device, SizeAlignTwoPower(size_in_bytes, 65536), ResourceType::BUFFER, d3d12::settings::num_back_buffers);\n\t\tSetName(m_heap, L\"Default SBO Heap\");\n\t\td3d12::MapHeap(m_heap);\n\t}\n\n\tD3D12ConstantBufferPool::~D3D12ConstantBufferPool()\n\t{\n\t\td3d12::Destroy(m_heap);\n\n\t\tfor (int i = 0; i < m_constant_buffer_handles.size(); ++i)\n\t\t{\n\t\t\tdelete m_constant_buffer_handles[i];\n\t\t}\n\t}\n\n\tvoid D3D12ConstantBufferPool::Evict()\n\t{\n\t\td3d12::Evict(m_heap);\n\t}\n\n\tvoid D3D12ConstantBufferPool::MakeResident()\n\t{\n\t\td3d12::MakeResident(m_heap);\n\t}\n\n\tConstantBufferHandle* D3D12ConstantBufferPool::AllocateConstantBuffer(std::size_t buffer_size)\n\t{\n\t\tD3D12ConstantBufferHandle* handle = new D3D12ConstantBufferHandle();\n\t\thandle->m_pool = this;\n\t\thandle->m_native = d3d12::AllocConstantBuffer(m_heap, buffer_size);\n\t\tif (handle->m_native == nullptr) \n\t\t{\n\t\t\tdelete handle;\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tm_constant_buffer_handles.push_back(handle);\n\n\t\treturn handle;\n\t}\n\n\tvoid D3D12ConstantBufferPool::WriteConstantBufferData(ConstantBufferHandle * handle, size_t size, size_t offset, std::uint8_t * data)\n\t{\n\t\tauto frame_index = m_render_system.GetFrameIdx();\n\t\tstd::uint8_t* cpu_address = static_cast<D3D12ConstantBufferHandle*>(handle)->m_native->m_cpu_addresses->operator[](frame_index);\n\t\tmemcpy(cpu_address + offset, data, size);\n\t}\n\n\tvoid D3D12ConstantBufferPool::WriteConstantBufferData(ConstantBufferHandle * handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t * data)\n\t{\n\t\tauto frame_index = frame_idx;\n\t\tstd::uint8_t* cpu_address = static_cast<D3D12ConstantBufferHandle*>(handle)->m_native->m_cpu_addresses->operator[](frame_index);\n\t\tmemcpy(cpu_address + offset, data, size);\n\t}\n\n\tvoid D3D12ConstantBufferPool::DeallocateConstantBuffer(ConstantBufferHandle* handle)\n\t{\n\t\td3d12::DeallocConstantBuffer(m_heap, static_cast<D3D12ConstantBufferHandle*>(handle)->m_native);\n\n\t\tstd::vector<ConstantBufferHandle*>::iterator it;\n\t\tfor (it = m_constant_buffer_handles.begin(); it != m_constant_buffer_handles.end(); ++it)\n\t\t{\n\t\t\tif ((*it) == handle)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (it != m_constant_buffer_handles.end())\n\t\t{\n\t\t\tm_constant_buffer_handles.erase(it);\n\t\t\tdelete handle;\n\t\t}\n\t}\n} /* wr */\n"
  },
  {
    "path": "src/d3d12/d3d12_constant_buffer_pool.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include \"../constant_buffer_pool.hpp\"\n#include \"d3d12_structs.hpp\"\n\nnamespace wr \n{\n\n\tstruct D3D12ConstantBufferHandle : ConstantBufferHandle \n\t{\n\t\td3d12::HeapResource* m_native;\n\t};\n\n\tclass D3D12RenderSystem;\n\n\tclass D3D12ConstantBufferPool : public ConstantBufferPool \n\t{\n\tpublic:\n\t\texplicit D3D12ConstantBufferPool(D3D12RenderSystem& render_system, std::size_t size_in_bytes);\n\t\t~D3D12ConstantBufferPool() final;\n\n\t\tvoid Evict() final;\n\t\tvoid MakeResident() final;\n\t\t\n\tprotected:\n\t\tConstantBufferHandle* AllocateConstantBuffer(std::size_t buffer_size) final;\n\t\tvoid WriteConstantBufferData(ConstantBufferHandle* handle, size_t size, size_t offset, std::uint8_t* data) final;\n\t\tvoid WriteConstantBufferData(ConstantBufferHandle* handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t* data) final;\n\t\tvoid DeallocateConstantBuffer(ConstantBufferHandle* handle) final;\n\n\t\tstd::vector<ConstantBufferHandle*> m_constant_buffer_handles;\n\n\t\td3d12::Heap<HeapOptimization::SMALL_BUFFERS>* m_heap;\n\t\tD3D12RenderSystem& m_render_system;\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/d3d12/d3d12_defines.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../util/log.hpp\"\n\n#pragma warning(push, 0)\n\n#define D3DX12_INC d3dx12_rt.h\n\n/*Helper function to get readable error messages from HResults\ncode originated from https://docs.microsoft.com/en-us/windows/desktop/cossdk/interpreting-error-codes\n*/\ninline std::string HResultToString(HRESULT hr)\n{\n\tif (FACILITY_WINDOWS == HRESULT_FACILITY(hr))\n\t{\n\t\thr = HRESULT_CODE(hr);\n\t}\n\tTCHAR* sz_err_msg;\n\n\tif (FormatMessage(\n\t\tFORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,\n\t\tNULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),\n\t\t(LPTSTR)&sz_err_msg, 0, NULL) != 0)\n\t{\n\t\tstd::string retval = sz_err_msg;\n\t\tLocalFree(sz_err_msg);\n\t\treturn(retval);\n\t}\n\telse\n\t{\n\t\treturn(std::string(\"[Could not find a description for error # %#x.\", hr));\n\t}\n}\n\n//! Checks whether the d3d12 object exists before releasing it.\n#define SAFE_RELEASE(obj) { if ( obj ) { obj->Release(); obj = NULL; } }\n\n//! Handles a hresult.\n#define TRY(result) if (FAILED(result)) { LOGC(\"An hresult returned a error!. File: \" + std::string(__FILE__) + \" Line: \" + std::to_string(__LINE__) + \" HRResult: \" +  HResultToString(result)); }\n\n//! Handles a hresult and outputs a specific message.\n#define TRY_M(result, msg) if (FAILED(result)) { LOGC(static_cast<std::string>(msg) + \" HRResult: \" + HResultToString(result)); }\n\n//! This macro is used to name d3d12 resources.\n#define NAME_D3D12RESOURCE(r, n) { auto temp = std::string(__FILE__); \\\nr->SetName(std::wstring(std::wstring(n) + L\" (line: \" + std::to_wstring(__LINE__) + L\" file: \" + std::wstring(temp.begin(), temp.end())).c_str()); }\n\n//! This macro is used to name d3d12 resources with a placeholder name. TODO: \"Unamed Resource\" should be \"Unamed [typename]\"\n#define NAME_D3D12RESOURCE(r) { auto temp = std::string(__FILE__); \\\nr->SetName(std::wstring(L\"Unnamed Resource (line: \" + std::to_wstring(__LINE__) + L\" file: \" + std::wstring(temp.begin(), temp.end())).c_str()); }\n\n// Particular version automatically rounds the alignment to a two power.\ntemplate<typename T, typename A>\nconstexpr inline T SizeAlignTwoPower(T size, A alignment)\n{\n\treturn (size + (alignment - 1U)) & ~(alignment - 1U);\n}\n\n// Particular version always aligns to the provided alignment\ntemplate<typename T, typename A>\nconstexpr inline T SizeAlignAnyAlignment(T size, A alignment)\n{\n\treturn (size / alignment + (size%alignment > 0))*alignment;\n}\n\n#pragma warning(pop)\n"
  },
  {
    "path": "src/d3d12/d3d12_descriptor_heap.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"../util/log.hpp\"\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\t\n\tDescriptorHeap* CreateDescriptorHeap(Device* device, desc::DescriptorHeapDesc const & descriptor)\n\t{\n\t\tauto heap = new DescriptorHeap();\n\t\tconst auto n_device = device->m_native;\n\n\t\theap->m_create_info = descriptor;\n\t\theap->m_increment_size = n_device->GetDescriptorHandleIncrementSize(static_cast<D3D12_DESCRIPTOR_HEAP_TYPE>(descriptor.m_type));\n\t\theap->m_native.resize(descriptor.m_versions);\n\n\t\tfor (uint32_t i = 0; i < descriptor.m_versions; ++i)\n\t\t{\n\n\t\t\tD3D12_DESCRIPTOR_HEAP_DESC heap_desc;\n\t\t\theap_desc.NumDescriptors = descriptor.m_num_descriptors;\n\t\t\theap_desc.Type = (D3D12_DESCRIPTOR_HEAP_TYPE)descriptor.m_type;\n\t\t\theap_desc.Flags = descriptor.m_shader_visible ? D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE : D3D12_DESCRIPTOR_HEAP_FLAG_NONE;\n\t\t\theap_desc.NodeMask = 0;\n\t\t\tTRY_M(n_device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap->m_native[i])), \"Couldn't create descriptor heap\");\n\n\t\t}\n\n\t\treturn heap;\n\t}\n\n\tDescHeapGPUHandle GetGPUHandle(DescriptorHeap* desc_heap, unsigned int frame_idx, unsigned int index)\n\t{\n\t\tDescHeapGPUHandle retval;\n\n\t\tretval.m_native = desc_heap->m_native[frame_idx % desc_heap->m_create_info.m_versions]->GetGPUDescriptorHandleForHeapStart();\n\n\t\tif (index > 0)\n\t\t{\n\t\t\tOffset(retval, index, desc_heap->m_increment_size);\n\t\t}\n\n\t\treturn retval;\n\t}\n\n\tDescHeapCPUHandle GetCPUHandle(DescriptorHeap* desc_heap, unsigned int frame_idx, unsigned int index)\n\t{\n\t\tDescHeapCPUHandle retval;\n\n\t\tretval.m_native = desc_heap->m_native[frame_idx % desc_heap->m_create_info.m_versions]->GetCPUDescriptorHandleForHeapStart();\n\n\t\tif (index > 0)\n\t\t{\n\t\t\tOffset(retval, index, desc_heap->m_increment_size);\n\t\t}\n\n\t\treturn retval;\n\t}\n\n\tvoid SetName(DescriptorHeap * desc_heap, std::wstring name)\n\t{\n\t\tfor (int i = 0; i < desc_heap->m_native.size(); i++)\n\t\t{\n\t\t\tdesc_heap->m_native[i]->SetName((name + L\" Descriptor Version \" + std::to_wstring(i)).c_str());\n\t\t}\n\t}\n\n\tvoid Offset(DescHeapGPUHandle& handle, unsigned int index, unsigned int increment_size)\n\t{\n\t\thandle.m_native.ptr += static_cast<UINT64>(index) * static_cast<UINT64>(increment_size);\n\t}\n\n\tvoid Offset(DescHeapCPUHandle& handle, unsigned int index, unsigned int increment_size)\n\t{\n\t\thandle.m_native.ptr += static_cast<UINT64>(index) * static_cast<UINT64>(increment_size);\n\t}\n\n\tvoid Destroy(DescriptorHeap* desc_heap)\n\t{\n\t\tfor (auto desc : desc_heap->m_native)\n\t\t{\n\t\t\tSAFE_RELEASE(desc);\n\t\t}\n\n\t\tdelete desc_heap;\n\t}\n\n} /* wr::d3d12 */\n"
  },
  {
    "path": "src/d3d12/d3d12_descriptors_allocations.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_descriptors_allocations.hpp\"\n#include \"d3d12_renderer.hpp\"\n#include \"d3d12_functions.hpp\"\n#include \"../util/log.hpp\"\n\n// DESCRIPTOR ALLOCATION FUNCTIONS\n\nnamespace wr\n{\n\n\tDescriptorAllocation::DescriptorAllocation()\n\t\t: m_descriptor{ 0 }\n\t\t, m_num_handles(0)\n\t\t, m_descriptor_size(0)\n\t\t, m_page(nullptr)\n\t{}\n\n\tDescriptorAllocation::DescriptorAllocation(d3d12::DescHeapCPUHandle descriptor,\n\t\tuint32_t num_handles,\n\t\tuint32_t descriptor_size,\n\t\tstd::shared_ptr<DescriptorAllocatorPage> page)\n\t\t: m_descriptor(descriptor)\n\t\t, m_num_handles(num_handles)\n\t\t, m_descriptor_size(descriptor_size)\n\t\t, m_page(page)\n\t{}\n\n\n\tDescriptorAllocation::~DescriptorAllocation()\n\t{\n\t\tFree();\n\t}\n\n\tDescriptorAllocation::DescriptorAllocation(DescriptorAllocation&& allocation)\n\t\t: m_descriptor(allocation.m_descriptor)\n\t\t, m_num_handles(allocation.m_num_handles)\n\t\t, m_descriptor_size(allocation.m_descriptor_size)\n\t\t, m_page(std::move(allocation.m_page))\n\t{\n\t\tallocation.m_descriptor.m_native.ptr = 0;\n\t\tallocation.m_num_handles = 0;\n\t\tallocation.m_descriptor_size = 0;\n\t}\n\n\tDescriptorAllocation& DescriptorAllocation::operator=(DescriptorAllocation&& other)\n\t{\n\t\t// Free this descriptor if it points to anything.\n\t\tFree();\n\n\t\tm_descriptor = other.m_descriptor;\n\t\tm_num_handles = other.m_num_handles;\n\t\tm_descriptor_size = other.m_descriptor_size;\n\t\tm_page = std::move(other.m_page);\n\n\t\tother.m_descriptor.m_native.ptr = 0;\n\t\tother.m_num_handles = 0;\n\t\tother.m_descriptor_size = 0;\n\n\t\treturn *this;\n\t}\n\n\tvoid DescriptorAllocation::Free()\n\t{\n\t\tif (!IsNull() && m_page)\n\t\t{\n\t\t\tm_page->Free(std::move(*this));\n\t\t\tm_descriptor.m_native.ptr = 0;\n\t\t\tm_num_handles = 0;\n\t\t\tm_descriptor_size = 0;\n\t\t\tm_page.reset();\n\t\t}\n\t}\n\n\t// Check if this a valid descriptor.\n\tbool DescriptorAllocation::IsNull() const\n\t{\n\t\treturn m_descriptor.m_native.ptr == 0;\n\t}\n\n\t// Get a descriptor at a particular offset in the allocation.\n\td3d12::DescHeapCPUHandle DescriptorAllocation::GetDescriptorHandle(uint32_t offset) const\n\t{\n\t\tif (offset > m_num_handles)\n\t\t{\n\t\t\tLOGC(\"Specified offset is greater than the number of handles in this Descriptor Allocation\");\n\t\t}\n\n\t\treturn { m_descriptor.m_native.ptr + (m_descriptor_size * offset) };\n\t}\n\n\tuint32_t DescriptorAllocation::GetNumHandles() const\n\t{\n\t\treturn m_num_handles;\n\t}\n\n\tstd::shared_ptr<DescriptorAllocatorPage> DescriptorAllocation::GetDescriptorAllocatorPage() const\n\t{\n\t\treturn m_page;\n\t}\n}\n\n// DESCRIPTOR ALLOCATOR PAGE FUNCTIONS\n\nnamespace wr\n{\n\tDescriptorAllocatorPage::DescriptorAllocatorPage(D3D12RenderSystem& render_system, DescriptorHeapType type, uint32_t num_descriptors)\n\t\t: m_render_system(render_system)\n\t\t, m_heap_type(type)\n\t\t, m_num_descriptors_in_heap(num_descriptors)\n\t{\n\n\t\td3d12::desc::DescriptorHeapDesc desc;\n\t\tdesc.m_type = m_heap_type;\n\t\tdesc.m_num_descriptors = m_num_descriptors_in_heap;\n\t\tdesc.m_versions = 1;\n\t\tdesc.m_shader_visible = false;\n\n\t\tm_descriptor_heap = d3d12::CreateDescriptorHeap(m_render_system.m_device, desc);\n\n\t\tm_base_descriptor = d3d12::GetCPUHandle(m_descriptor_heap, 0);\n\n\t\tm_num_free_handles = m_num_descriptors_in_heap;\n\n\t\t// Initialize the free lists\n\t\tAddNewBlock(0, m_num_free_handles);\n\t}\n\n\tDescriptorAllocatorPage::~DescriptorAllocatorPage()\n\t{\n\t\td3d12::Destroy(m_descriptor_heap);\n\n\t\tm_num_free_handles = 0;\n\t\tm_num_descriptors_in_heap = 0;\n\n\t\tm_freelist_by_offset.clear();\n\t\tm_freelist_by_size.clear();\n\t}\n\n\tDescriptorHeapType DescriptorAllocatorPage::GetHeapType() const\n\t{\n\t\treturn m_heap_type;\n\t}\n\n\tuint32_t DescriptorAllocatorPage::NumFreeHandles() const\n\t{\n\t\treturn m_num_free_handles;\n\t}\n\n\tbool DescriptorAllocatorPage::HasSpace(uint32_t num_descriptors) const\n\t{\n\t\treturn m_freelist_by_size.lower_bound(num_descriptors) != m_freelist_by_size.end();\n\t}\n\n\tvoid DescriptorAllocatorPage::AddNewBlock(uint32_t offset, uint32_t num_descriptors)\n\t{\n\t\tauto offsetIt = m_freelist_by_offset.emplace(offset, num_descriptors);\n\t\tauto sizeIt = m_freelist_by_size.emplace(num_descriptors, offsetIt.first);\n\t\toffsetIt.first->second.m_freelist_by_size_itr = sizeIt;\n\t}\n\n\tDescriptorAllocation DescriptorAllocatorPage::Allocate(uint32_t num_descriptors)\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_allocation_mutex);\n\n\t\t// There are less than the requested number of descriptors left in the heap.\n\t\t// Return a NULL descriptor and try another heap.\n\t\tif (num_descriptors > m_num_free_handles)\n\t\t{\n\t\t\treturn DescriptorAllocation();\n\t\t}\n\n\t\t// Get the first block that is large enough to satisfy the request.\n\t\tauto smallestBlockIt = m_freelist_by_size.lower_bound(num_descriptors);\n\t\tif (smallestBlockIt == m_freelist_by_size.end())\n\t\t{\n\t\t\t// There was no free block that could satisfy the request.\n\t\t\treturn DescriptorAllocation();\n\t\t}\n\n\t\t// The size of the smallest block that satisfies the request.\n\t\tauto blockSize = smallestBlockIt->first;\n\n\t\t// The pointer to the same entry in the FreeListByOffset map.\n\t\tauto offsetIt = smallestBlockIt->second;\n\n\t\t// The offset in the descriptor heap.\n\t\tauto offset = offsetIt->first;\n\n\t\t// Remove the existing free block from the free list.\n\t\tm_freelist_by_size.erase(smallestBlockIt);\n\t\tm_freelist_by_offset.erase(offsetIt);\n\n\t\t// Compute the new free block that results from splitting this block.\n\t\tauto newOffset = offset + num_descriptors;\n\t\tauto newSize = blockSize - num_descriptors;\n\n\t\tif (newSize > 0)\n\t\t{\n\t\t\t// If the allocation didn't exactly match the requested size,\n\t\t\t// return the left-over to the free list.\n\t\t\tAddNewBlock(newOffset, newSize);\n\t\t}\n\n\t\t// Decrement free handles.\n\t\tm_num_free_handles -= num_descriptors;\n\n\t\td3d12::DescHeapCPUHandle new_handle;\n\t\tnew_handle.m_native = CD3DX12_CPU_DESCRIPTOR_HANDLE(m_base_descriptor.m_native, offset, m_descriptor_heap->m_increment_size);\n\n\t\treturn DescriptorAllocation(new_handle, num_descriptors, m_descriptor_heap->m_increment_size, shared_from_this());\n\t}\n\n\tuint32_t DescriptorAllocatorPage::ComputeOffset(d3d12::DescHeapCPUHandle handle)\n\t{\n\t\treturn static_cast<uint32_t>(handle.m_native.ptr - m_base_descriptor.m_native.ptr) / m_descriptor_heap->m_increment_size;\n\t}\n\n\tvoid DescriptorAllocatorPage::Free(DescriptorAllocation&& allocation_handle)\n\t{\n\t\t// Compute the offset of the descriptor within the descriptor heap.\n\t\tauto offset = ComputeOffset(allocation_handle.GetDescriptorHandle());\n\n\t\tstd::lock_guard<std::mutex> lock(m_allocation_mutex);\n\n\t\t// Don't add the block directly to the free list until the frame has completed.\n\t\tm_stale_descriptors.emplace(offset, allocation_handle.GetNumHandles(), m_render_system.GetFrameIdx());\n\t}\n\n\tvoid DescriptorAllocatorPage::FreeBlock(uint32_t offset, uint32_t num_descriptors)\n\t{\n\t\t// Find the first element whose offset is greater than the specified offset.\n\t\t// This is the block that should appear after the block that is being freed.\n\t\tauto next_block_it = m_freelist_by_offset.upper_bound(offset);\n\n\t\t// Find the block that appears before the block being freed.\n\t\tauto prev_block_it = next_block_it;\n\t\t// If it's not the first block in the list.\n\t\tif (prev_block_it != m_freelist_by_offset.begin())\n\t\t{\n\t\t\t// Go to the previous block in the list.\n\t\t\t--prev_block_it;\n\t\t}\n\t\telse\n\t\t{\n\t\t\t// Otherwise, just set it to the end of the list to indicate that no\n\t\t\t// block comes before the one being freed.\n\t\t\tprev_block_it = m_freelist_by_offset.end();\n\t\t}\n\n\t\t// Add the number of free handles back to the heap.\n\t\t// This needs to be done before merging any blocks since merging\n\t\t// blocks modifies the numDescriptors variable.\n\t\tm_num_free_handles += num_descriptors;\n\n\t\tif (prev_block_it != m_freelist_by_offset.end() &&\n\t\t\toffset == prev_block_it->first + prev_block_it->second.m_size)\n\t\t{\n\t\t\t// The previous block is exactly behind the block that is to be freed.\n\t\t\t//\n\t\t\t// PrevBlock.Offset           Offset\n\t\t\t// |                          |\n\t\t\t// |<-----PrevBlock.Size----->|<------Size-------->|\n\t\t\t//\n\n\t\t\t// Increase the block size by the size of merging with the previous block.\n\t\t\toffset = prev_block_it->first;\n\t\t\tnum_descriptors += prev_block_it->second.m_size;\n\n\t\t\t// Remove the previous block from the free list.\n\t\t\tm_freelist_by_size.erase(prev_block_it->second.m_freelist_by_size_itr);\n\t\t\tm_freelist_by_offset.erase(prev_block_it);\n\t\t}\n\n\t\tif (next_block_it != m_freelist_by_offset.end() &&\n\t\t\toffset + num_descriptors == next_block_it->first)\n\t\t{\n\t\t\t// The next block is exactly in front of the block that is to be freed.\n\t\t\t//\n\t\t\t// Offset               NextBlock.Offset \n\t\t\t// |                    |\n\t\t\t// |<------Size-------->|<-----NextBlock.Size----->|\n\n\t\t\t// Increase the block size by the size of merging with the next block.\n\t\t\tnum_descriptors += next_block_it->second.m_size;\n\n\t\t\t// Remove the next block from the free list.\n\t\t\tm_freelist_by_size.erase(next_block_it->second.m_freelist_by_size_itr);\n\t\t\tm_freelist_by_offset.erase(next_block_it);\n\t\t}\n\n\t\t// Add the freed block to the free list.\n\t\tAddNewBlock(offset, num_descriptors);\n\t}\n\n\tvoid DescriptorAllocatorPage::ReleaseStaleDescriptors()\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_allocation_mutex);\n\n\t\twhile (!m_stale_descriptors.empty() && m_stale_descriptors.front().m_frame_number <= m_render_system.GetFrameIdx())\n\t\t{\n\t\t\tauto& staleDescriptor = m_stale_descriptors.front();\n\n\t\t\t// The offset of the descriptor in the heap.\n\t\t\tauto offset = staleDescriptor.m_offset;\n\t\t\t// The number of descriptors that were allocated.\n\t\t\tauto numDescriptors = staleDescriptor.m_size;\n\n\t\t\tFreeBlock(offset, numDescriptors);\n\n\t\t\tm_stale_descriptors.pop();\n\t\t}\n\t}\n}\n\n// DESCRIPTOR ALLOCATOR FUNCTIONS\n\nnamespace wr\n{\n\tDescriptorAllocator::DescriptorAllocator(D3D12RenderSystem& render_system, DescriptorHeapType type, uint32_t num_descriptors_per_heap)\n\t\t: m_render_system(render_system)\n\t\t, m_heap_type(type)\n\t\t, m_num_descriptors_per_heap(num_descriptors_per_heap)\n\t{\n\t}\n\n\tDescriptorAllocator::~DescriptorAllocator()\n\t{\n\t\tReleaseStaleDescriptors();\n\n\t\tfor (auto heap : m_heap_pool)\n\t\t{\n\t\t\theap.reset();\n\t\t}\n\t}\n\n\tstd::shared_ptr<DescriptorAllocatorPage> DescriptorAllocator::CreateAllocatorPage()\n\t{\n\t\tauto new_page = std::make_shared<DescriptorAllocatorPage>(m_render_system, m_heap_type, m_num_descriptors_per_heap);\n\n\t\tm_heap_pool.emplace_back(new_page);\n\t\tm_available_heaps.insert(m_heap_pool.size() - 1);\n\n\t\treturn new_page;\n\t}\n\n\tDescriptorAllocation DescriptorAllocator::Allocate(uint32_t num_dscriptors)\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_allocation_mutex);\n\n\t\tDescriptorAllocation allocation;\n\n\t\tfor (auto iter = m_available_heaps.begin(); iter != m_available_heaps.end(); ++iter)\n\t\t{\n\t\t\tauto allocator_page = m_heap_pool[*iter];\n\n\t\t\tallocation = allocator_page->Allocate(num_dscriptors);\n\n\t\t\tif (allocator_page->NumFreeHandles() == 0)\n\t\t\t{\n\t\t\t\titer = m_available_heaps.erase(iter);\n\t\t\t}\n\n\t\t\t// A valid allocation has been found.\n\t\t\tif (!allocation.IsNull())\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// No available heap could satisfy the requested number of descriptors.\n\t\tif (allocation.IsNull())\n\t\t{\n\t\t\tm_num_descriptors_per_heap = std::max(m_num_descriptors_per_heap, num_dscriptors);\n\t\t\tauto new_page = CreateAllocatorPage();\n\n\t\t\tallocation = new_page->Allocate(num_dscriptors);\n\t\t}\n\n\t\treturn allocation;\n\t}\n\n\tvoid DescriptorAllocator::ReleaseStaleDescriptors()\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_allocation_mutex);\n\n\t\tfor (size_t i = 0; i < m_heap_pool.size(); ++i)\n\t\t{\n\t\t\tauto page = m_heap_pool[i];\n\n\t\t\tpage->ReleaseStaleDescriptors();\n\n\t\t\tif (page->NumFreeHandles() > 0)\n\t\t\t{\n\t\t\t\tm_available_heaps.insert(i);\n\t\t\t}\n\t\t}\n\t}\n\n\n\n\n}"
  },
  {
    "path": "src/d3d12/d3d12_descriptors_allocations.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"d3d12_structs.hpp\"\n#include \"d3d12_enums.hpp\"\n\n#include <cstdint>\n#include <mutex>\n#include <memory>\n#include <set>\n#include <map>\n#include <vector>\n#include <queue>\n\nnamespace wr\n{\n\t//Forward declaration of a page, the DescriptorAllocation class needs it.\n\tclass DescriptorAllocatorPage;\n\n\t//Forward declaration of the render system needed by the page\n\tclass D3D12RenderSystem;\n\n\t///////////////////////////////\n\t//   DESCRIPTOR ALLOCATION   //\n\t///////////////////////////////\n\n\t//Represent a single allocation of contiguous descriptors in a descriptor heap.\n\tclass DescriptorAllocation\n\t{\n\tpublic:\n\t\t// Creates a NULL descriptor.\n\t\tDescriptorAllocation();\n\n\t\tDescriptorAllocation(d3d12::DescHeapCPUHandle descriptor, \n\t\t\t\t\t\t\t uint32_t num_handles, \n\t\t\t\t\t\t\t uint32_t descriptor_size, \n\t\t\t\t\t\t\t std::shared_ptr<DescriptorAllocatorPage> page);\n\n\t\t// The destructor will automatically free the allocation.\n\t\t~DescriptorAllocation();\n\n\t\t// Only move is allowed.\n\t\tDescriptorAllocation(const DescriptorAllocation&) = delete;\n\t\tDescriptorAllocation& operator=(const DescriptorAllocation&) = delete;\n\n\t\tDescriptorAllocation(DescriptorAllocation&& allocation);\n\t\tDescriptorAllocation& operator=(DescriptorAllocation&& other);\n\n\n\t\t// Check if this a valid descriptor.\n\t\tbool IsNull() const;\n\n\t\t// Get a descriptor at a particular offset in the allocation.\n\t\td3d12::DescHeapCPUHandle GetDescriptorHandle(uint32_t offset = 0) const;\n\n\t\t// Get the number of (consecutive) handles for this allocation.\n\t\tuint32_t GetNumHandles() const;\n\n\t\t// Get the heap that this allocation came from.\n\t\t// (For internal use only).\n\t\tstd::shared_ptr<DescriptorAllocatorPage> GetDescriptorAllocatorPage() const;\n\n\tprivate:\n\t\t// Free the descriptor back to the heap it came from.\n\t\tvoid Free();\n\n\t\t// The base descriptor.\n\t\td3d12::DescHeapCPUHandle m_descriptor;\n\t\t// The number of descriptors in this allocation.\n\t\tuint32_t m_num_handles;\n\t\t// The offset to the next descriptor.\n\t\tuint32_t m_descriptor_size;\n\n\t\t// A pointer back to the original page where this allocation came from.\n\t\tstd::shared_ptr<DescriptorAllocatorPage> m_page;\n\t};\n\n\t///////////////////////////////\n\t// DESCRIPTOR ALLOCATOR PAGE //\n\t///////////////////////////////\n\n\tclass DescriptorAllocatorPage : public std::enable_shared_from_this<DescriptorAllocatorPage>\n\t{\n\tpublic:\n\t\tDescriptorAllocatorPage(D3D12RenderSystem& render_system, DescriptorHeapType type, uint32_t num_descriptors);\n\n\t\t~DescriptorAllocatorPage();\n\n\t\tDescriptorHeapType GetHeapType() const;\n\n\t\t/**\n\t\t* Check to see if this descriptor page has a contiguous block of descriptors\n\t\t* large enough to satisfy the request.\n\t\t*/\n\t\tbool HasSpace(uint32_t num_descriptors) const;\n\n\t\t/**\n\t\t* Get the number of available handles in the heap.\n\t\t*/\n\t\tuint32_t NumFreeHandles() const;\n\n\t\t/**\n\t\t* Allocate a number of descriptors from this descriptor heap.\n\t\t* If the allocation cannot be satisfied, then a NULL descriptor\n\t\t* is returned.\n\t\t*/\n\t\tDescriptorAllocation Allocate(uint32_t num_descriptors);\n\n\t\t/**\n\t\t* Return a descriptor back to the heap.\n\t\t* Stale descriptors are not freed directly, but put\n\t\t* on a stale allocations queue. Stale allocations are returned to the heap\n\t\t* using the DescriptorAllocatorPage::ReleaseStaleAllocations method.\n\t\t*/\n\t\tvoid Free(DescriptorAllocation&& allocation_handle);\n\n\t\t/**\n\t\t* Returned the stale descriptors back to the descriptor heap.\n\t\t*/\n\t\tvoid ReleaseStaleDescriptors();\n\n\tprotected:\n\n\t\t// Compute the offset of the descriptor handle from the start of the heap.\n\t\tuint32_t ComputeOffset(d3d12::DescHeapCPUHandle handle);\n\n\t\t// Adds a new block to the free list.\n\t\tvoid AddNewBlock(uint32_t offset, uint32_t num_descriptors);\n\n\t\t// Free a block of descriptors.\n\t\t// This will also merge free blocks in the free list to form larger blocks\n\t\t// that can be reused.\n\t\tvoid FreeBlock(uint32_t offset, uint32_t num_descriptors);\n\n\tprivate:\n\t\t// The offset (in descriptors) within the descriptor heap.\n\t\tusing OffsetType = uint32_t;\n\t\t// The number of descriptors that are available.\n\t\tusing SizeType = uint32_t;\n\n\t\tstruct FreeBlockInfo;\n\n\t\t// A map that lists the free blocks by the offset within the descriptor heap.\n\t\tusing FreeListByOffset = std::map<OffsetType, FreeBlockInfo>;\n\n\t\t// A map that lists the free blocks by size.\n\t\t// Needs to be a multimap since multiple blocks can have the same size.\n\t\tusing FreeListBySize = std::multimap<SizeType, FreeListByOffset::iterator>;\n\n\t\tstruct FreeBlockInfo\n\t\t{\n\t\t\tFreeBlockInfo(SizeType size)\n\t\t\t\t: m_size(size)\n\t\t\t{}\n\n\t\t\tSizeType m_size;\n\t\t\tFreeListBySize::iterator m_freelist_by_size_itr;\n\t\t};\n\n\t\tstruct StaleDescriptorInfo\n\t\t{\n\t\t\tStaleDescriptorInfo(OffsetType offset, SizeType size, uint64_t frame)\n\t\t\t\t: m_offset(offset)\n\t\t\t\t, m_size(size)\n\t\t\t\t, m_frame_number(frame)\n\t\t\t{}\n\n\t\t\t// The offset within the descriptor heap.\n\t\t\tOffsetType m_offset;\n\t\t\t// The number of descriptors\n\t\t\tSizeType m_size;\n\t\t\t// The frame number that the descriptor was freed.\n\t\t\tuint64_t m_frame_number;\n\t\t};\n\n\t\t// Stale descriptors are queued for release until the frame that they were freed\n\t\t// has completed.\n\t\tusing StaleDescriptorQueue = std::queue<StaleDescriptorInfo>;\n\n\t\tFreeListByOffset m_freelist_by_offset;\n\t\tFreeListBySize m_freelist_by_size;\n\t\tStaleDescriptorQueue m_stale_descriptors;\n\n\t\td3d12::DescriptorHeap* m_descriptor_heap;\n\t\tDescriptorHeapType m_heap_type;\n\t\td3d12::DescHeapCPUHandle m_base_descriptor;\n\t\tuint32_t m_num_descriptors_in_heap;\n\t\tuint32_t m_num_free_handles;\n\n\t\tstd::mutex m_allocation_mutex;\n\n\t\tD3D12RenderSystem& m_render_system;\n\t};\n}\n\n\n//////////////////////////\n// DESCRIPTOR ALLOCATOR //\n//////////////////////////\n\nnamespace wr\n{\n\tclass DescriptorAllocator\n\t{\n\tpublic:\n\t\tDescriptorAllocator(D3D12RenderSystem& render_system, DescriptorHeapType type, uint32_t num_descriptors_per_heap = 256);\n\t\tvirtual ~DescriptorAllocator();\n\n\t\t/**\n\t\t * Allocate a number of contiguous descriptors from a CPU visible descriptor heap.\n\t\t *\n\t\t * @param numDescriptors The number of contiguous descriptors to allocate.\n\t\t * Cannot be more than the number of descriptors per descriptor heap.\n\t\t */\n\t\tDescriptorAllocation Allocate(uint32_t num_descriptors = 1);\n\n\t\t/**\n\t\t * When the frame has completed, the stale descriptors can be released.\n\t\t */\n\t\tvoid ReleaseStaleDescriptors();\n\n\tprivate:\n\t\tusing DescriptorHeapPool = std::vector<std::shared_ptr<DescriptorAllocatorPage>>;\n\n\t\t// Create a new heap with a specific number of descriptors.\n\t\tstd::shared_ptr<DescriptorAllocatorPage> CreateAllocatorPage();\n\n\t\tDescriptorHeapType m_heap_type;\n\t\tuint32_t m_num_descriptors_per_heap;\n\n\t\tDescriptorHeapPool m_heap_pool;\n\n\t\t// Indices of available heaps in the heap pool.\n\t\tstd::set<size_t> m_available_heaps;\n\n\t\tstd::mutex m_allocation_mutex;\n\n\t\tD3D12RenderSystem& m_render_system;\n\t};\n}"
  },
  {
    "path": "src/d3d12/d3d12_device.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include <DXGIDebug.h>\n#include <functional>\n#include <dxcapi.h>\n\n#include \"../util/log.hpp\"\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tIDxcCompiler2* Device::m_compiler = nullptr;\n\n\tnamespace internal\n\t{\n\t\tvoid EnableDebugLayer(Device* device)\n\t\t{\n\t\t\tif (settings::enable_debug_layer != settings::DebugLayer::DISABLE) // If the debug layer isn't disabled\n\t\t\t{\n\t\t\t\t// Setup debug layers\n\t\t\t\tID3D12Debug* temp_debug_controller;\n\t\t\t\tif (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&temp_debug_controller))) && SUCCEEDED(temp_debug_controller->QueryInterface(IID_PPV_ARGS(&device->m_debug_controller))))\n\t\t\t\t{\n\t\t\t\t\tif (settings::enable_debug_layer == settings::DebugLayer::ENABLE_WITH_GPU_VALIDATION) // If GPU validation is requested.\n\t\t\t\t\t{\n\t\t\t\t\t\tdevice->m_debug_controller->SetEnableSynchronizedCommandQueueValidation(true);\n\t\t\t\t\t\tdevice->m_debug_controller->SetEnableGPUBasedValidation(true);\n\t\t\t\t\t}\n\t\t\t\t\tdevice->m_debug_controller->EnableDebugLayer();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tvoid EnableGpuErrorBreaking(Device* device)\n\t\t{\n#ifdef _DEBUG\n\t\t\t// Set error behaviour\n\t\t\tif (SUCCEEDED(device->m_native->QueryInterface(IID_PPV_ARGS(&device->m_info_queue))))\n\t\t\t{\n\t\t\t\tdevice->m_info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, !device->m_dxr_support);\n\t\t\t\tdevice->m_info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, !device->m_dxr_support);\n\t\t\t}\n#endif\n\t\t}\n\n\t\tvoid GetSysInfo(Device* device)\n\t\t{\n\t\t\tGetNativeSystemInfo(&device->m_sys_info);\n\t\t\tdevice->m_adapter->GetDesc3(&device->m_adapter_info);\n\t\t}\n\n\t\tvoid CreateFactory(Device* device)\n\t\t{\n\t\t\tTRY_M(CreateDXGIFactory2(settings::enable_debug_factory ? DXGI_CREATE_FACTORY_DEBUG : 0, IID_PPV_ARGS(&device->m_dxgi_factory)),\n\t\t\t\t\"Failed to create DXGIFactory.\");\n\t\t}\n\n\t\tvoid FindAdapter(Device* device)\n\t\t{\n\t\t\tIDXGIAdapter1* adapter = nullptr;\n\t\t\tint adapter_idx = 0;\n\n\t\t\t// Find a compatible adapter.\n\t\t\t//while (device->m_dxgi_factory->EnumAdapterByGpuPreference(adapter_idx, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)) != DXGI_ERROR_NOT_FOUND)\n\t\t\twhile ((device->m_dxgi_factory)->EnumAdapters1(adapter_idx, &adapter) != DXGI_ERROR_NOT_FOUND)\n\t\t\t{\n\t\t\t\tDXGI_ADAPTER_DESC1 desc;\n\t\t\t\tadapter->GetDesc1(&desc);\n\n\t\t\t\t// Skip software adapters.\n\t\t\t\tif (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)\n\t\t\t\t{\n\t\t\t\t\tadapter_idx++;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tstd::function<bool(int)> recursive_dc = [&](int i) -> bool\n\t\t\t\t{\n\t\t\t\t\t// Create a device to test if the adapter supports the specified feature level.\n\t\t\t\t\tHRESULT hr = D3D12CreateDevice(adapter, settings::possible_feature_levels[i], _uuidof(ID3D12Device), nullptr);\n\t\t\t\t\tif (SUCCEEDED(hr))\n\t\t\t\t\t{\n\t\t\t\t\t\tdevice->m_feature_level = settings::possible_feature_levels[i];\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (i + 1 >= settings::possible_feature_levels.size())\n\t\t\t\t\t{\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\ti++;\n\t\t\t\t\t\trecursive_dc(i);\n\t\t\t\t\t}\n\n\t\t\t\t\treturn true;\n\t\t\t\t};\n\n\t\t\t\tif (recursive_dc(0))\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tadapter_idx++;\n\t\t\t}\n\n\t\t\tif (adapter == nullptr)\n\t\t\t{\n\t\t\t\tdevice->m_dxgi_factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter));\n\n\t\t\t\tLOGW(\"Using Warp Adapter!\");\n\n\t\t\t\tdevice->m_feature_level = settings::possible_feature_levels[0];\n\t\t\t\tTRY_M(D3D12CreateDevice(adapter, settings::possible_feature_levels[0], _uuidof(ID3D12Device), nullptr),\n\t\t\t\t\t\"Failed to create warp adapter.\");\n\t\t\t}\n\n\t\t\tif (adapter == nullptr)\n\t\t\t{\n\t\t\t\tLOGC(\"Failed to find hardware adapter or create warp adapter.\");\n\t\t\t}\n\n\t\t\tdevice->m_adapter = (IDXGIAdapter4*)adapter;\n\t\t}\n\n\t\tvoid QueryForOptionalFormats(Device* device)\n\t\t{\n\t\t\tD3D12_FEATURE_DATA_D3D12_OPTIONS feature_data;\n\t\t\tZeroMemory(&feature_data, sizeof(feature_data));\n\t\t\tHRESULT hr = device->m_native->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &feature_data, sizeof(feature_data));\n\n\t\t\tif (SUCCEEDED(hr))\n\t\t\t{\n\t\t\t\t// TypedUAVLoadAdditionalFormats contains a Boolean that tells you whether the feature is supported or not\n\t\t\t\tif (feature_data.TypedUAVLoadAdditionalFormats)\n\t\t\t\t{\n\t\t\t\t\t// Can assume all-or-nothing subset is supported (e.g. R32G32B32A32_FLOAT)\n\t\t\t\t\t// Cannot assume other formats are supported, so we check:\n\n\t\t\t\t\tauto CheckAndSetFormat = [](Device* device, DXGI_FORMAT format)\n\t\t\t\t\t{\n\t\t\t\t\t\tD3D12_FEATURE_DATA_FORMAT_SUPPORT format_support = { format, D3D12_FORMAT_SUPPORT1_NONE, D3D12_FORMAT_SUPPORT2_NONE };\n\t\t\t\t\t\tHRESULT hr = device->m_native->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &format_support, sizeof(format_support));\n\t\t\t\t\t\tif (SUCCEEDED(hr) && (format_support.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD) != 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tdevice->m_optional_formats.set(format);\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R16G16B16A16_UNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R16G16B16A16_SNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R32G32_FLOAT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R32G32_UINT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R32G32_SINT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R10G10B10A2_UNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R10G10B10A2_UINT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R11G11B10_FLOAT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R8G8B8A8_SNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R16G16_FLOAT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R16G16_UNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R16G16_UINT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R16G16_SNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R16G16_SINT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R8G8_UNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R8G8_UINT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R8G8_SNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R8G8_SINT);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R16_UNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R16_SNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_R8_SNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_A8_UNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_B5G6R5_UNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_B5G5R5A1_UNORM);\n\t\t\t\t\tCheckAndSetFormat(device, DXGI_FORMAT_B4G4R4A4_UNORM);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Returns bool whether the device supports DirectX Raytracing tier.\n\t\tinline bool IsDXRSupported(IDXGIAdapter1* adapter)\n\t\t{\n\t\t\tID3D12Device* test_device;\n\t\t\tD3D12_FEATURE_DATA_D3D12_OPTIONS5 feature_support_data = {};\n\n\t\t\tauto retval = SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&test_device)))\n\t\t\t\t&& SUCCEEDED(test_device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5, &feature_support_data, sizeof(feature_support_data)))\n\t\t\t\t&& feature_support_data.RaytracingTier != D3D12_RAYTRACING_TIER_NOT_SUPPORTED;\n\n\t\t\tSAFE_RELEASE(test_device);\n\n\t\t\treturn retval;\n\t\t}\n\n\t\t// Enable experimental features required for compute-based raytracing fallback.\n\t\t// This will set active D3D12 devices to DEVICE_REMOVED state.\n\t\t// Returns bool whether the call succeeded and the device supports the feature.\n\t\tinline bool IsDXRFallbackSupported(IDXGIAdapter1* adapter)\n\t\t{\n\t\t\tID3D12Device* test_device;\n\t\t\tUUID experimentalFeatures[] = { D3D12ExperimentalShaderModels };\n\n\t\t\tauto retval = SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&test_device)));\n\n\t\t\tSAFE_RELEASE(test_device);\n\n\t\t\treturn retval;\n\t\t}\n\n\t\tinline void EnableDXRFallback()\n\t\t{\n\t\t\tUUID experimental_features[] = { D3D12ExperimentalShaderModels };\n\t\t\tTRY_M(D3D12EnableExperimentalFeatures(1, experimental_features, nullptr, nullptr), \"Failed to enable experimantal dxr fallback features.\");\n\t\t}\n\n\t\tinline void SetRaytracingType(Device* device)\n\t\t{\n\t\t\tif (d3d12::settings::disable_rtx)\n\t\t\t{\n\t\t\t\tdevice->m_rt_type = RaytracingType::NONE;\n\t\t\t}\n\t\t\telse if (device->m_dxr_support && !d3d12::settings::force_dxr_fallback)\n\t\t\t{\n\t\t\t\tdevice->m_rt_type = RaytracingType::NATIVE;\n\t\t\t}\n\t\t\telse if (device->m_dxr_fallback_support)\n\t\t\t{\n\t\t\t\tdevice->m_rt_type = RaytracingType::FALLBACK;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tdevice->m_rt_type = RaytracingType::NONE;\n\t\t\t}\n\t\t}\n\t}\n\n\tDevice* CreateDevice()\n\t{\n\t\tauto device = new Device();\n\n\t\tinternal::EnableDebugLayer(device);\n\t\tinternal::CreateFactory(device);\n\t\tinternal::FindAdapter(device);\n\n\t\tdevice->m_dxr_support = internal::IsDXRSupported(device->m_adapter);\n\t\tdevice->m_dxr_fallback_support = internal::IsDXRFallbackSupported(device->m_adapter);\n\t\tinternal::SetRaytracingType(device);\n\n\t\tif (!device->m_dxr_support)\n\t\t{\n\t\t\tLOGW(\n\t\t\t\t\"No DXR support detected.\\n\"\n\t\t\t\t\"Possible Reasons:\\n\"\n\t\t\t\t\"\\t 1) Wrong SDK version. (Required: `Windows 10 October 2018 Update SDK (17763)`)\\n\"\n\t\t\t\t\"\\t 2) Wrong OS version. (Required: 1809 (17763.107))\\n\"\n\t\t\t\t\"\\t 3) DX12 GPU with a incompatible DirectX Raytracing driver. (NVIDIA: driver version 415 or higher, AMD: Consult Vendor for availability)\"\n\t\t\t);\n\t\t}\n\t\tif (!device->m_dxr_fallback_support)\n\t\t{\n\t\t\tLOGW(\n\t\t\t\t\"No DXR Fallback support detected.\\n\"\n\t\t\t\t\"Possible Reasons:\\n\"\n\t\t\t\t\"GPU without feature level 11.1 or Resource Binding Tier 3.\"\n\t\t\t);\n\t\t}\n\n\t\tif (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t{\n\t\t\tLOGW(\"Enabling DXR Fallback.\");\n\t\t\t//internal::EnableDXRFallback();\n\t\t}\n\n\t\tTRY_M(D3D12CreateDevice(device->m_adapter, device->m_feature_level, IID_PPV_ARGS(&device->m_native)),\n\t\t\t\"Failed to create D3D12Device.\");\n\n\t\tif (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t{\n\t\t\tauto fallback_device_flags = d3d12::settings::force_dxr_fallback ? CreateRaytracingFallbackDeviceFlags::ForceComputeFallback : CreateRaytracingFallbackDeviceFlags::None;\n\t\t\tTRY_M(D3D12CreateRaytracingFallbackDevice(device->m_native, fallback_device_flags, 0, IID_PPV_ARGS(&device->m_fallback_native)), \"Failed to create fallback layer.\");\n\t\t}\n\n\t\t// Create shader compiler\n\t\tif (!Device::m_compiler)\n\t\t{\n\t\t\tDxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&Device::m_compiler));\n\t\t}\n\n\t\tinternal::EnableGpuErrorBreaking(device);\n\t\tinternal::GetSysInfo(device);\n\n\t\tstd::wstring g = device->m_adapter_info.Description;\n\t\tLOG(\"{}\", std::string(g.begin(), g.end()));\n\n\t\tinternal::QueryForOptionalFormats(device);\n\n\t\treturn device;\n\t}\n\n\tRaytracingType GetRaytracingType(Device* device)\n\t{\n\t\treturn device->m_rt_type;\n\t}\n\n\tvoid Destroy(Device* device)\n\t{\n\t\tSAFE_RELEASE(device->m_adapter);\n\t\tSAFE_RELEASE(device->m_native);\n\t\tSAFE_RELEASE(device->m_dxgi_factory);\n\t\tSAFE_RELEASE(device->m_debug_controller);\n\t\tSAFE_RELEASE(device->m_info_queue);\n\t\tSAFE_RELEASE(device->m_fallback_native);\n\t\tSAFE_RELEASE(device->m_compiler);\n\t\tdelete device;\n\t}\n\n\tvoid SetName(Device * device, std::wstring name)\n\t{\n\t\tdevice->m_native->SetName(name.c_str());\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_dynamic_descriptor_heap.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_dynamic_descriptor_heap.hpp\"\n#include \"d3d12_renderer.hpp\"\n#include \"d3d12_functions.hpp\"\n#include \"../util/log.hpp\"\n\nnamespace wr\n{\n\tDynamicDescriptorHeap::DynamicDescriptorHeap(wr::d3d12::Device* device, DescriptorHeapType type, uint32_t num_descriptors_per_heap)\n\t\t: m_desc_heap_type(type)\n\t\t, m_num_descr_per_heap(num_descriptors_per_heap)\n\t\t, m_descriptor_table_bit_mask(0)\n\t\t, m_stale_descriptor_table_bit_mask(0)\n\t\t, m_num_free_handles(0)\n\t\t, m_device(device)\n\t{\n\t\tm_current_cpu_desc_handle.m_native.ptr = 0;\n\t\tm_current_gpu_desc_handle.m_native.ptr = 0;\n\n\t\tm_descriptor_handle_increment_size = m_device->m_native->GetDescriptorHandleIncrementSize((D3D12_DESCRIPTOR_HEAP_TYPE)type);\n\n\t\t// Allocate space for staging CPU visible descriptors.\n\t\tm_descriptor_handle_cache = std::make_unique<D3D12_CPU_DESCRIPTOR_HANDLE[]>(m_num_descr_per_heap);\n\t}\n\n\tDynamicDescriptorHeap::~DynamicDescriptorHeap()\n\t{\n\t\twhile(!m_descriptor_heap_pool.empty())\n\t\t{\n\t\t\tdelete m_descriptor_heap_pool.front();\n\t\t\tm_descriptor_heap_pool.pop();\n\t\t}\n\t}\n\n\tvoid DynamicDescriptorHeap::ParseRootSignature(const d3d12::RootSignature& root_signature)\n\t{\n\t\t// If the root signature changes, all descriptors must be (re)bound to the\n\t\t// command list.\n\t\tm_stale_descriptor_table_bit_mask = 0;\n\n\t\tconst auto& root_signature_desc = root_signature.m_create_info;\n\n\t\t// Get a bit mask that represents the root parameter indices that match the \n\t\t// descriptor heap type for this dynamic descriptor heap.\n\t\tswitch (m_desc_heap_type)\n\t\t{\n\t\tcase wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV:\n\t\t\tm_descriptor_table_bit_mask = root_signature.m_descriptor_table_bit_mask;\n\t\t\tbreak;\n\t\tcase wr::DescriptorHeapType::DESC_HEAP_TYPE_SAMPLER:\n\t\t\tm_descriptor_table_bit_mask = root_signature.m_sampler_table_bit_mask;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\t//LOGC(\"You can't use a different type other than CBC_SRV_UAV or SAMPLER for dynamic descriptor heaps\");\n\t\t\tbreak;\n\t\t}\n\n\t\tuint32_t descriptor_table_bitmask = m_descriptor_table_bit_mask;\n\n\t\tuint32_t current_offset = 0;\n\t\tDWORD root_idx;\n\t\tsize_t num_parameters = root_signature_desc.m_parameters.size();\n\n\t\twhile (_BitScanForward(&root_idx, descriptor_table_bitmask) && root_idx < num_parameters)\n\t\t{\n\t\t\t//Since a 32 bit mask is used to represent the descriptor tables in the root signature\n\t\t\t//I want to break if the root_idx is bigger than 32. If that ever happens, we might consider\n\t\t\t//switching to a 64bit mask.\n\t\t\tif (root_idx > 32) { LOGC(\"A maximum of 32 descriptor tables are supported\"); }\n\n\t\t\tuint32_t num_descriptors = root_signature.m_num_descriptors_per_table[root_idx];\n\n\t\t\tDescriptorTableCache& descriptorTableCache = m_descriptor_table_cache[root_idx];\n\t\t\tdescriptorTableCache.m_num_descriptors = num_descriptors;\n\t\t\tdescriptorTableCache.m_base_descriptor = m_descriptor_handle_cache.get() + current_offset;\n\n\t\t\tcurrent_offset += num_descriptors;\n\n\t\t\t// Flip the descriptor table bit so it's not scanned again for the current index.\n\t\t\tdescriptor_table_bitmask ^= (1 << root_idx);\n\t\t}\n\n\t\t// Make sure the maximum number of descriptors per descriptor heap has not been exceeded.\n\t\tif (current_offset > m_num_descr_per_heap) { LOGC(\"The root signature requires more than the maximum number of descriptors per descriptor heap. Consider increasing the maximum number of descriptors per descriptor heap.\"); }\n\t}\n\n\tvoid DynamicDescriptorHeap::StageDescriptors(uint32_t root_param_idx, uint32_t offset, uint32_t num_descriptors, const d3d12::DescHeapCPUHandle src_desc)\n\t{\n\t\tif (num_descriptors > m_num_descr_per_heap || root_param_idx >= MaxDescriptorTables)\n\t\t{\n\t\t\tLOGC(\"Cannot stage more than the maximum number of descriptors per heap. Cannot stage more than MaxDescriptorTables root parameters\");\n\t\t}\n\n\t\tDescriptorTableCache& descriptor_table_cache = m_descriptor_table_cache[root_param_idx];\n\n\t\t// Check that the number of descriptors to copy does not exceed the number\n\t\t// of descriptors expected in the descriptor table.\n\t\tif ((offset + num_descriptors) > descriptor_table_cache.m_num_descriptors)\n\t\t{\n\t\t\tLOGC(\"Number of descriptors exceeds the number of descriptors in the descriptor table.\");\n\t\t}\n\n\t\tD3D12_CPU_DESCRIPTOR_HANDLE* dst_descriptor = (descriptor_table_cache.m_base_descriptor + offset);\n\t\tfor (uint32_t i = 0; i < num_descriptors; ++i)\n\t\t{\n\t\t\tdst_descriptor[i] = CD3DX12_CPU_DESCRIPTOR_HANDLE(src_desc.m_native, i, m_descriptor_handle_increment_size);\n\t\t}\n\n\t\t// Set the root parameter index bit to make sure the descriptor table \n\t\t// at that index is bound to the command list.\n\t\tm_stale_descriptor_table_bit_mask |= (1 << root_param_idx);\n\t}\n\n\tuint32_t DynamicDescriptorHeap::ComputeStaleDescriptorCount() const\n\t{\n\t\tuint32_t num_stale_desc = 0;\n\t\tDWORD i;\n\t\tDWORD stale_desc_bitmask = m_stale_descriptor_table_bit_mask;\n\n\t\twhile (_BitScanForward(&i, stale_desc_bitmask))\n\t\t{\n\t\t\tnum_stale_desc += m_descriptor_table_cache[i].m_num_descriptors;\n\t\t\tstale_desc_bitmask ^= (1 << i);\n\t\t}\n\n\t\treturn num_stale_desc;\n\t}\n\n\td3d12::DescriptorHeap* DynamicDescriptorHeap::RequestDescriptorHeap()\n\t{\n\t\td3d12::DescriptorHeap* descriptor_heap;\n\n\t\tif (!m_available_descriptor_heaps.empty())\n\t\t{\n\t\t\tdescriptor_heap = m_available_descriptor_heaps.front();\n\t\t\tm_available_descriptor_heaps.pop();\n\t\t}\n\t\telse\n\t\t{\n\t\t\tdescriptor_heap = CreateDescriptorHeap();\n\t\t\tm_descriptor_heap_pool.push(descriptor_heap);\n\t\t}\n\n\t\treturn descriptor_heap;\n\t}\n\n\td3d12::DescriptorHeap* DynamicDescriptorHeap::CreateDescriptorHeap()\n\t{\n\t\td3d12::desc::DescriptorHeapDesc desc;\n\t\tdesc.m_type = m_desc_heap_type;\n\t\tdesc.m_num_descriptors = m_num_descr_per_heap;\n\t\tdesc.m_shader_visible = true;\n\t\tdesc.m_versions = 1;\n\n\t\td3d12::DescriptorHeap* descriptor_heap = d3d12::CreateDescriptorHeap(m_device, desc);\n\n\t\treturn descriptor_heap;\n\t}\n\n\tvoid DynamicDescriptorHeap::CommitStagedDescriptorsForDraw(d3d12::CommandList& cmd_list)\n\t{\n\t\t// Compute the number of descriptors that need to be copied \n\t\tuint32_t num_descriptors_to_commit = ComputeStaleDescriptorCount();\n\t\t\n\t\tif (num_descriptors_to_commit > 0)\n\t\t{\n\t\t\tif (!m_current_descriptor_heap || m_num_free_handles < num_descriptors_to_commit)\n\t\t\t{\n\t\t\t\tm_current_descriptor_heap = RequestDescriptorHeap();\n\t\t\t\tm_current_cpu_desc_handle = d3d12::GetCPUHandle(m_current_descriptor_heap, 0);\n\t\t\t\tm_current_gpu_desc_handle = d3d12::GetGPUHandle(m_current_descriptor_heap, 0);\n\t\t\t\tm_num_free_handles = m_num_descr_per_heap;\n\n\t\t\t\td3d12::BindDescriptorHeap(&cmd_list, m_current_descriptor_heap, m_desc_heap_type, 0);\n\n\t\t\t\t// When updating the descriptor heap on the command list, all descriptor\n\t\t\t\t// tables must be (re)recopied to the new descriptor heap (not just\n\t\t\t\t// the stale descriptor tables).\n\t\t\t\tm_stale_descriptor_table_bit_mask = m_descriptor_table_bit_mask;\n\t\t\t}\n\n\t\t\tDWORD root_idx;\n\t\t\t// Scan from LSB to MSB for a bit set in staleDescriptorsBitMask\n\t\t\twhile (_BitScanForward(&root_idx, m_stale_descriptor_table_bit_mask))\n\t\t\t{\n\t\t\t\tstd::uint32_t num_src_desc = m_descriptor_table_cache[root_idx].m_num_descriptors;\n\t\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE* pSrcDescriptorHandles = m_descriptor_table_cache[root_idx].m_base_descriptor;\n\n\t\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE pDestDescriptorRangeStarts[] =\n\t\t\t\t{\n\t\t\t\t\tm_current_cpu_desc_handle.m_native\n\t\t\t\t};\n\t\t\t\tstd::uint32_t pDestDescriptorRangeSizes[] =\n\t\t\t\t{\n\t\t\t\t\tnum_src_desc\n\t\t\t\t};\n\n\t\t\t\t// Copy the staged CPU visible descriptors to the GPU visible descriptor heap.\n\t\t\t\tm_device->m_native->CopyDescriptors(1, pDestDescriptorRangeStarts, pDestDescriptorRangeSizes,\n\t\t\t\t\tnum_src_desc, pSrcDescriptorHandles, nullptr, static_cast<D3D12_DESCRIPTOR_HEAP_TYPE>(m_desc_heap_type));\n\n\t\t\t\t// Set the descriptors on the command list using the passed-in setter function.\n\t\t\t\td3d12::BindDescriptorTable(&cmd_list, m_current_gpu_desc_handle, root_idx);\n\n\t\t\t\t// Offset current CPU and GPU descriptor handles.\n\t\t\t\td3d12::Offset(m_current_cpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size);\n\t\t\t\td3d12::Offset(m_current_gpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size);\n\n\t\t\t\tm_num_free_handles -= num_src_desc;\n\n\t\t\t\t// Flip the stale bit so the descriptor table is not recopied again unless it is updated with a new descriptor.\n\t\t\t\tm_stale_descriptor_table_bit_mask ^= (1 << root_idx);\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid DynamicDescriptorHeap::CommitStagedDescriptorsForDispatch(d3d12::CommandList& cmd_list)\n\t{\n\t\t// Compute the number of descriptors that need to be copied \n\t\tstd::uint32_t num_descriptors_to_commit = ComputeStaleDescriptorCount();\n\n\t\tif (num_descriptors_to_commit > 0)\n\t\t{\n\t\t\tif (!m_current_descriptor_heap || m_num_free_handles < num_descriptors_to_commit)\n\t\t\t{\n\t\t\t\tm_current_descriptor_heap = RequestDescriptorHeap();\n\t\t\t\tm_current_cpu_desc_handle = d3d12::GetCPUHandle(m_current_descriptor_heap, 0);\n\t\t\t\tm_current_gpu_desc_handle = d3d12::GetGPUHandle(m_current_descriptor_heap, 0);\n\t\t\t\tm_num_free_handles = m_num_descr_per_heap;\n\n\t\t\t\td3d12::BindDescriptorHeap(&cmd_list, m_current_descriptor_heap, m_desc_heap_type, 0, d3d12::GetRaytracingType(m_device) == RaytracingType::FALLBACK);\n\n\t\t\t\t// When updating the descriptor heap on the command list, all descriptor\n\t\t\t\t// tables must be (re)recopied to the new descriptor heap (not just\n\t\t\t\t// the stale descriptor tables).\n\t\t\t\tm_stale_descriptor_table_bit_mask = m_descriptor_table_bit_mask;\n\t\t\t}\n\n\t\t\tDWORD root_idx;\n\t\t\t// Scan from LSB to MSB for a bit set in staleDescriptorsBitMask\n\t\t\twhile (_BitScanForward(&root_idx, m_stale_descriptor_table_bit_mask))\n\t\t\t{\n\t\t\t\tstd::uint32_t num_src_desc = m_descriptor_table_cache[root_idx].m_num_descriptors;\n\t\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE* pSrcDescriptorHandles = m_descriptor_table_cache[root_idx].m_base_descriptor;\n\n\t\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE pDestDescriptorRangeStarts[] =\n\t\t\t\t{\n\t\t\t\t\tm_current_cpu_desc_handle.m_native\n\t\t\t\t};\n\t\t\t\tstd::uint32_t pDestDescriptorRangeSizes[] =\n\t\t\t\t{\n\t\t\t\t\tnum_src_desc\n\t\t\t\t};\n\n\t\t\t\t// Copy the staged CPU visible descriptors to the GPU visible descriptor heap.\n\t\t\t\tm_device->m_native->CopyDescriptors(1, pDestDescriptorRangeStarts, pDestDescriptorRangeSizes,\n\t\t\t\t\tnum_src_desc, pSrcDescriptorHandles, nullptr, static_cast<D3D12_DESCRIPTOR_HEAP_TYPE>(m_desc_heap_type));\n\n\t\t\t\t// Set the descriptors on the command list using the passed-in setter function.\n\t\t\t\td3d12::BindComputeDescriptorTable(&cmd_list, m_current_gpu_desc_handle, root_idx);\n\n\t\t\t\t// Offset current CPU and GPU descriptor handles.\n\t\t\t\td3d12::Offset(m_current_cpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size);\n\t\t\t\td3d12::Offset(m_current_gpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size);\n\n\t\t\t\tm_num_free_handles -= num_src_desc;\n\n\t\t\t\t// Flip the stale bit so the descriptor table is not recopied again unless it is updated with a new descriptor.\n\t\t\t\tm_stale_descriptor_table_bit_mask ^= (1 << root_idx);\n\t\t\t}\n\t\t}\n\t}\n\n\td3d12::DescHeapGPUHandle DynamicDescriptorHeap::CopyDescriptor(d3d12::CommandList& cmd_list, d3d12::DescHeapCPUHandle cpu_desc)\n\t{\n\t\tif (!m_current_descriptor_heap || m_num_free_handles < 1)\n\t\t{\n\t\t\tm_current_descriptor_heap = RequestDescriptorHeap();\n\t\t\tm_current_cpu_desc_handle = d3d12::GetCPUHandle(m_current_descriptor_heap, 0);\n\t\t\tm_current_gpu_desc_handle = d3d12::GetGPUHandle(m_current_descriptor_heap, 0);\n\n\t\t\tm_num_free_handles = m_num_descr_per_heap;\n\n\t\t\td3d12::BindDescriptorHeap(&cmd_list, m_current_descriptor_heap, m_desc_heap_type, 0);\n\n\t\t\t// When updating the descriptor heap on the command list, all descriptor\n\t\t\t// tables must be (re)recopied to the new descriptor heap (not just\n\t\t\t// the stale descriptor tables).\n\t\t\tm_stale_descriptor_table_bit_mask = m_descriptor_table_bit_mask;\n\t\t}\n\n\t\td3d12::DescHeapGPUHandle h_gpu = m_current_gpu_desc_handle;\n\n\t\tm_device->m_native->CopyDescriptorsSimple(1, m_current_cpu_desc_handle.m_native, cpu_desc.m_native, static_cast<D3D12_DESCRIPTOR_HEAP_TYPE>(m_desc_heap_type));\n\n\t\td3d12::Offset(m_current_cpu_desc_handle, 1, m_descriptor_handle_increment_size);\n\t\td3d12::Offset(m_current_gpu_desc_handle, 1, m_descriptor_handle_increment_size);\n\n\t\tm_num_free_handles -= 1;\n\n\t\treturn h_gpu;\n\t}\n\n\tvoid DynamicDescriptorHeap::Reset()\n\t{\n\t\tm_available_descriptor_heaps = m_descriptor_heap_pool;\n\t\t\n\t\tm_current_descriptor_heap = nullptr;\n\n\t\t/*if (m_current_descriptor_heap)\n\t\t{\n\t\t\tID3D12DescriptorHeap* temp = m_current_descriptor_heap->m_native[0];\n\t\t\tif (temp)\n\t\t\t{\n\t\t\t\tm_current_descriptor_heap->m_native[0] = nullptr;\n\t\t\t\ttemp->Release();\n\t\t\t}\n\n\t\t\tm_current_descriptor_heap = nullptr;\n\t\t}*/\n\n\t\tm_current_cpu_desc_handle.m_native = CD3DX12_CPU_DESCRIPTOR_HANDLE(D3D12_DEFAULT);\n\t\tm_current_gpu_desc_handle.m_native = CD3DX12_GPU_DESCRIPTOR_HANDLE(D3D12_DEFAULT);\n\t\tm_num_free_handles = 0;\n\t\tm_descriptor_table_bit_mask = 0;\n\t\tm_stale_descriptor_table_bit_mask = 0;\n\n\t\t// Reset the table cache\n\t\tfor (int i = 0; i < MaxDescriptorTables; ++i)\n\t\t{\n\t\t\tm_descriptor_table_cache[i].Reset();\n\t\t}\n\t}\n\n\n}"
  },
  {
    "path": "src/d3d12/d3d12_dynamic_descriptor_heap.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"d3d12_structs.hpp\"\n#include \"d3d12_enums.hpp\"\n\n#include <cstdint>\n#include <memory>\n#include <queue>\n\n\n\nnamespace wr\n{\n\tnamespace d3d12\n\t{\n\t\tstruct Device;\n\t};\n\n\tclass DynamicDescriptorHeap\n\t{\n\tpublic:\n\t\tDynamicDescriptorHeap(wr::d3d12::Device* device, DescriptorHeapType type, uint32_t num_descriptors_per_heap = 1024);\n\n\t\tvirtual ~DynamicDescriptorHeap();\n\n\t\t/**\n\t\t * Stages a contiguous range of CPU visible descriptors.\n\t\t * Descriptors are not copied to the GPU visible descriptor heap until\n\t\t * the CommitStagedDescriptors function is called.\n\t\t */\n\t\tvoid StageDescriptors(uint32_t root_param_idx, uint32_t offset, uint32_t num_descriptors, const d3d12::DescHeapCPUHandle src_desc);\n\n\t\t/**\n\t\t * Copy all of the staged descriptors to the GPU visible descriptor heap and\n\t\t * bind the descriptor heap and the descriptor tables to the command list.\n\t\t * The passed-in function object is used to set the GPU visible descriptors\n\t\t * on the command list. Two possible functions are:\n\t\t *   * Before a draw    : ID3D12GraphicsCommandList::SetGraphicsRootDescriptorTable\n\t\t *   * Before a dispatch: ID3D12GraphicsCommandList::SetComputeRootDescriptorTable\n\t\t */\n\t\tvoid CommitStagedDescriptorsForDraw(d3d12::CommandList& cmd_list);\n\t\tvoid CommitStagedDescriptorsForDispatch(d3d12::CommandList& cmd_list);\n\n\t\t/**\n\t\t * Copies a single CPU visible descriptor to a GPU visible descriptor heap.\n\t\t * This is useful for the\n\t\t *   * ID3D12GraphicsCommandList::ClearUnorderedAccessViewFloat\n\t\t *   * ID3D12GraphicsCommandList::ClearUnorderedAccessViewUint\n\t\t * methods which require both a CPU and GPU visible descriptors for a UAV\n\t\t * resource.\n\t\t *\n\t\t * @param commandList The command list is required in case the GPU visible\n\t\t * descriptor heap needs to be updated on the command list.\n\t\t * @param cpuDescriptor The CPU descriptor to copy into a GPU visible\n\t\t * descriptor heap.\n\t\t *\n\t\t * @return The GPU visible descriptor.\n\t\t */\n\t\td3d12::DescHeapGPUHandle CopyDescriptor(d3d12::CommandList& cmd_list, d3d12::DescHeapCPUHandle cpu_desc);\n\n\t\t/**\n\t\t * Parse the root signature to determine which root parameters contain\n\t\t * descriptor tables and determine the number of descriptors needed for\n\t\t * each table.\n\t\t */\n\t\tvoid ParseRootSignature(const d3d12::RootSignature& root_signature);\n\n\t\t/**\n\t\t * Reset used descriptors. This should only be done if any descriptors\n\t\t * that are being referenced by a command list has finished executing on the\n\t\t * command queue.\n\t\t */\n\t\tvoid Reset();\n\n\tprotected:\n\n\tprivate:\n\n\t\t// Request a descriptor heap if one is available.\n\t\td3d12::DescriptorHeap* RequestDescriptorHeap();\n\n\t\t// Create a new descriptor heap of no descriptor heap is available.\n\t\td3d12::DescriptorHeap* CreateDescriptorHeap();\n\n\t\t// Compute the number of stale descriptors that need to be copied\n\t\t// to GPU visible descriptor heap.\n\t\tuint32_t ComputeStaleDescriptorCount() const;\n\n\t\t/**\n\t\t * The maximum number of descriptor tables per root signature.\n\t\t * A 32-bit mask is used to keep track of the root parameter indices that\n\t\t * are descriptor tables.\n\t\t */\n\t\tstatic const uint32_t MaxDescriptorTables = 32;\n\n\t\t/**\n\t\t * A structure that represents a descriptor table entry in the root signature.\n\t\t */\n\t\tstruct DescriptorTableCache\n\t\t{\n\t\t\tDescriptorTableCache()\n\t\t\t\t: m_num_descriptors(0)\n\t\t\t\t, m_base_descriptor(nullptr)\n\t\t\t{}\n\n\t\t\t// Reset the table cache.\n\t\t\tvoid Reset()\n\t\t\t{\n\t\t\t\tm_num_descriptors = 0;\n\t\t\t\tm_base_descriptor = nullptr;\n\t\t\t}\n\n\t\t\t// The number of descriptors in this descriptor table.\n\t\t\tuint32_t m_num_descriptors;\n\t\t\t// The pointer to the descriptor in the descriptor handle cache.\n\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE* m_base_descriptor;\n\t\t};\n\n\t\t// Describes the type of descriptors that can be staged using this \n\t\t// dynamic descriptor heap.\n\t\t// Valid values are:\n\t\t//   * D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV\n\t\t//   * D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER\n\t\t// This parameter also determines the type of GPU visible descriptor heap to \n\t\t// create.\n\t\tDescriptorHeapType m_desc_heap_type;\n\n\t\t// The number of descriptors to allocate in new GPU visible descriptor heaps.\n\t\tuint32_t m_num_descr_per_heap;\n\n\t\tuint32_t m_descriptor_handle_increment_size;\n\n\t\t// The descriptor handle cache.\n\t\tstd::unique_ptr<D3D12_CPU_DESCRIPTOR_HANDLE[]> m_descriptor_handle_cache;\n\n\t\t// Descriptor handle cache per descriptor table.\n\t\tDescriptorTableCache m_descriptor_table_cache[MaxDescriptorTables];\n\n\t\t// Each bit in the bit mask represents the index in the root signature\n\t\t// that contains a descriptor table.\n\t\tuint32_t m_descriptor_table_bit_mask;\n\t\t// Each bit set in the bit mask represents a descriptor table\n\t\t// in the root signature that has changed since the last time the \n\t\t// descriptors were copied.\n\t\tuint32_t m_stale_descriptor_table_bit_mask;\n\n\t\tusing DescriptorHeapPool = std::queue< d3d12::DescriptorHeap* >;\n\n\t\tDescriptorHeapPool m_descriptor_heap_pool;\n\t\tDescriptorHeapPool m_available_descriptor_heaps;\n\n\t\td3d12::DescriptorHeap* m_current_descriptor_heap = nullptr;\n\t\td3d12::DescHeapGPUHandle m_current_gpu_desc_handle;\n\t\td3d12::DescHeapCPUHandle m_current_cpu_desc_handle;\n\n\t\tuint32_t m_num_free_handles;\n\n\t\twr::d3d12::Device* m_device;\n\t};\n}"
  },
  {
    "path": "src/d3d12/d3d12_enums.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <dxgi1_5.h>\n#include <d3d12.h>\n\nnamespace wr\n{\n\n\tenum class RaytracingType\n\t{\n\t\tFALLBACK,\n\t\tNATIVE,\n\t\tNONE,\n\t};\n\n\tenum class PipelineType\n\t{\n\t\tGRAPHICS_PIPELINE,\n\t\tCOMPUTE_PIPELINE,\n\t};\n\n\tenum class ShaderType\n\t{\n\t\tVERTEX_SHADER,\n\t\tPIXEL_SHADER,\n\t\tDOMAIN_SHADER,\n\t\tGEOMETRY_SHADER,\n\t\tHULL_SHADER,\n\t\tDIRECT_COMPUTE_SHADER,\n\t\tLIBRARY_SHADER,\n\t};\n\n\tenum class CmdListType\n\t{\n\t\tCMD_LIST_DIRECT = (int)D3D12_COMMAND_LIST_TYPE_DIRECT,\n\t\tCMD_LIST_COMPUTE = (int)D3D12_COMMAND_LIST_TYPE_COMPUTE,\n\t\tCMD_LIST_COPY = (int)D3D12_COMMAND_LIST_TYPE_COPY,\n\t\tCMD_LIST_BUNDLE = (int)D3D12_COMMAND_LIST_TYPE_BUNDLE,\n\t};\n\n\tenum class StateObjType\n\t{\n\t\tRAYTRACING_PIPELINE = (int)D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE,\n\t\tRAYTRACING, D3D12_STATE_OBJECT_TYPE_RAYTRACING,\n\t\tCOLLECTION, D3D12_STATE_OBJECT_TYPE_COLLECTION,\n\t};\n\n\tenum HeapOptimization\n\t{\n\t\tSMALL_BUFFERS = 0,\n\t\tSMALL_STATIC_BUFFERS = 1,\n\t\tBIG_BUFFERS = 2,\n\t\tBIG_STATIC_BUFFERS = 3,\n\t};\n\n\tenum class HeapType\n\t{\n\t\tHEAP_DEFAULT = (int)D3D12_HEAP_TYPE_DEFAULT,\n\t\tHEAP_READBACK = (int)D3D12_HEAP_TYPE_READBACK,\n\t\tHEAP_UPLOAD = (int)D3D12_HEAP_TYPE_UPLOAD,\n\t\tHEAP_CUSTOM = (int)D3D12_HEAP_TYPE_CUSTOM,\n\t};\n\n\tenum class ResourceType\n\t{\n\t\tBUFFER,\n\t\tTEXTURE,\n\t\tRT_DS,\n\t};\n\n\tenum class TopologyType\n\t{\n\t\tTRIANGLE = (int)D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,\n\t\tPATCH = (int)D3D12_PRIMITIVE_TOPOLOGY_TYPE_PATCH,\n\t\tPOINT = (int)D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT,\n\t\tLINE = (int)D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE,\n\t};\n\n\tenum class CullMode\n\t{\n\t\tCULL_FRONT = (int)D3D12_CULL_MODE_FRONT,\n\t\tCULL_BACK = (int)D3D12_CULL_MODE_BACK,\n\t\tCULL_NONE = (int)D3D12_CULL_MODE_NONE,\n\t};\n\n\tenum class TextureFilter\n\t{\n\t\tFILTER_LINEAR = (int)D3D12_FILTER_MIN_MAG_MIP_LINEAR,\n\t\tFILTER_POINT = (int)D3D12_FILTER_MIN_MAG_MIP_POINT,\n\t\tFILTER_LINEAR_POINT = (int)D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT,\n\t\tFILTER_ANISOTROPIC = (INT)D3D12_FILTER_ANISOTROPIC,\n\t};\n\n\tenum class SRVDimension\n\t{\n\t\tDIMENSION_BUFFER = (int)D3D12_SRV_DIMENSION_BUFFER,\n\t\tDIMENSION_TEXTURE1D = (int)D3D12_SRV_DIMENSION_TEXTURE1D,\n\t\tDIMENSION_TEXTURE1DARRAY = (int)D3D12_SRV_DIMENSION_TEXTURE1DARRAY,\n\t\tDIMENSION_TEXTURE2D = (int)D3D12_SRV_DIMENSION_TEXTURE2D,\n\t\tDIMENSION_TEXTURE2DARRAY = (int)D3D12_SRV_DIMENSION_TEXTURE2DARRAY,\n\t\tDIMENSION_TEXTURE2DMS = (int)D3D12_SRV_DIMENSION_TEXTURE2DMS,\n\t\tDIMENSION_TEXTURE2DMSARRAY = (int)D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY,\n\t\tDIMENSION_TEXTURE3D = (int)D3D12_SRV_DIMENSION_TEXTURE3D,\n\t\tDIMENSION_TEXTURECUBE = (int)D3D12_SRV_DIMENSION_TEXTURECUBE,\n\t\tDIMENSION_TEXTURECUBEARRAY = (int)D3D12_SRV_DIMENSION_TEXTURECUBEARRAY,\n\t};\n\n\tenum class UAVDimension\n\t{\n\t\tDIMENSION_BUFFER = (int)D3D12_UAV_DIMENSION_BUFFER,\n\t\tDIMENSION_TEXTURE1D = (int)D3D12_UAV_DIMENSION_TEXTURE1D,\n\t\tDIMENSION_TEXTURE1DARRAY = (int)D3D12_UAV_DIMENSION_TEXTURE1DARRAY,\n\t\tDIMENSION_TEXTURE2D = (int)D3D12_UAV_DIMENSION_TEXTURE2D,\n\t\tDIMENSION_TEXTURE2DARRAY = (int)D3D12_UAV_DIMENSION_TEXTURE2DARRAY,\n\t\tDIMENSION_TEXTURE3D = (int)D3D12_UAV_DIMENSION_TEXTURE3D,\n\t};\n\n\tenum class TextureAddressMode\n\t{\n\t\tTAM_MIRROR_ONCE = (int)D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE,\n\t\tTAM_MIRROR = (int)D3D12_TEXTURE_ADDRESS_MODE_MIRROR,\n\t\tTAM_CLAMP = (int)D3D12_TEXTURE_ADDRESS_MODE_CLAMP,\n\t\tTAM_BORDER = (int)D3D12_TEXTURE_ADDRESS_MODE_BORDER,\n\t\tTAM_WRAP = (int)D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n\t};\n\n\tenum class BorderColor\n\t{\n\t\tBORDER_TRANSPARENT = (int)D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK,\n\t\tBORDER_BLACK = (int)D3D12_STATIC_BORDER_COLOR_OPAQUE_BLACK,\n\t\tBORDER_WHITE = (int)D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE,\n\t};\n\n\tenum class DescriptorHeapType\n\t{\n\t\tDESC_HEAP_TYPE_CBV_SRV_UAV = (int)D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,\n\t\tDESC_HEAP_TYPE_SAMPLER = (int)D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,\n\t\tDESC_HEAP_TYPE_RTV = (int)D3D12_DESCRIPTOR_HEAP_TYPE_RTV,\n\t\tDESC_HEAP_TYPE_DSV = (int)D3D12_DESCRIPTOR_HEAP_TYPE_DSV,\n\t\tDESC_HEAP_TYPE_NUM_TYPES = (int)D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES,\n\t};\n\n\tenum class ResourceState\n\t{\n\t\tVERTEX_AND_CONSTANT_BUFFER = (int)D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER,\n\t\tINDEX_BUFFER = (int)D3D12_RESOURCE_STATE_INDEX_BUFFER,\n\t\tCOMMON = (int)D3D12_RESOURCE_STATE_COMMON,\n\t\tPRESENT = (int)D3D12_RESOURCE_STATE_PRESENT,\n\t\tRENDER_TARGET = (int)D3D12_RESOURCE_STATE_RENDER_TARGET,\n\t\tPIXEL_SHADER_RESOURCE = (int)D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,\n\t\tNON_PIXEL_SHADER_RESOURCE = (int)D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,\n\t\tUNORDERED_ACCESS = (int)D3D12_RESOURCE_STATE_UNORDERED_ACCESS,\n\t\tCOPY_SOURCE = (int)D3D12_RESOURCE_STATE_COPY_SOURCE,\n\t\tCOPY_DEST = (int)D3D12_RESOURCE_STATE_COPY_DEST,\n\t\tDEPTH_WRITE = (int)D3D12_RESOURCE_STATE_DEPTH_WRITE,\n\t\tDEPTH_READ = (int)D3D12_RESOURCE_STATE_DEPTH_READ,\n\t\tINDIRECT_ARGUMENT = (int)D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,\n\t};\n\n\tenum class BufferUsageFlag\n\t{\n\t\tINDEX_BUFFER = (int)D3D12_RESOURCE_STATE_INDEX_BUFFER,\n\t\tVERTEX_BUFFER = (int)D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER,\n\t};\n\n\tenum class Format\n\t{\n\t\tUNKNOWN = (int)DXGI_FORMAT_UNKNOWN,\n\t\tR10G10B10A2_UNORM = (int)DXGI_FORMAT_R10G10B10A2_UNORM,\n\t\tR10G10B10A2_UINT = (int)DXGI_FORMAT_R10G10B10A2_UINT,\n\t\tR32G32B32A32_FLOAT = (int)DXGI_FORMAT_R32G32B32A32_FLOAT,\n\t\tR32G32B32A32_UINT = (int)DXGI_FORMAT_R32G32B32A32_UINT,\n\t\tR32G32B32A32_SINT = (int)DXGI_FORMAT_R32G32B32A32_SINT,\n\t\tR32G32B32_FLOAT = (int)DXGI_FORMAT_R32G32B32_FLOAT,\n\t\tR32G32B32_UINT = (int)DXGI_FORMAT_R32G32B32_UINT,\n\t\tR32G32B32_SINT = (int)DXGI_FORMAT_R32G32B32_SINT,\n\t\tR16G16B16A16_FLOAT = (int)DXGI_FORMAT_R16G16B16A16_FLOAT,\n\t\tR16G16B16A16_UINT = (int)DXGI_FORMAT_R16G16B16A16_UINT,\n\t\tR16G16B16A16_SINT = (int)DXGI_FORMAT_R16G16B16A16_SINT,\n\t\tR16G16B16A16_UNORM = (int)DXGI_FORMAT_R16G16B16A16_UNORM,\n\t\tR16G16B16A16_SNORM = (int)DXGI_FORMAT_R16G16B16A16_SNORM,\n\t\tR32G32_FLOAT = (int)DXGI_FORMAT_R32G32_FLOAT,\n\t\tR32G32_UINT = (int)DXGI_FORMAT_R32G32_UINT,\n\t\tR32G32_SINT = (int)DXGI_FORMAT_R32G32_SINT,\n\t\t//R10G10B10_UNORM = (int)DXGI_FORMAT_R10G10B10_UNORM,\n\t\t//R10G10B10_UINT = (int)vk::Format::eA2R10G10B10UintPack32, //FIXME: Their are more vulcan variants?\n\t\tR11G11B10_FLOAT = (int)DXGI_FORMAT_R11G11B10_FLOAT,\n\t\tR8G8B8A8_UNORM = (int)DXGI_FORMAT_R8G8B8A8_UNORM,\n\t\tR8G8B8A8_UNORM_SRGB = (int)DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,\n\t\tR8G8B8A8_SNORM = (int)DXGI_FORMAT_R8G8B8A8_SNORM,\n\t\tR8G8B8A8_UINT = (int)DXGI_FORMAT_R8G8B8A8_UINT,\n\t\tR8G8B8A8_SINT = (int)DXGI_FORMAT_R8G8B8A8_SINT,\n\t\tR16G16_FLOAT = (int)DXGI_FORMAT_R16G16_FLOAT,\n\t\tR16G16_UINT = (int)DXGI_FORMAT_R16G16_UINT,\n\t\tR16G16_UNORM = (int)DXGI_FORMAT_R16G16_UNORM,\n\t\tR16G16_SNORM = (int)DXGI_FORMAT_R16G16_SNORM,\n\t\tR16G16_SINT = (int)DXGI_FORMAT_R16G16_SINT,\n\t\tD32_FLOAT = (int)DXGI_FORMAT_D32_FLOAT,\n\t\tR32_UINT = (int)DXGI_FORMAT_R32_UINT,\n\t\tR32_TYPELESS = (int)DXGI_FORMAT_R32_TYPELESS,\n\t\tR32_SINT = (int)DXGI_FORMAT_R32_SINT,\n\t\tR32_FLOAT = (int)DXGI_FORMAT_R32_FLOAT,\n\t\tR16_UNORM = (int)DXGI_FORMAT_R16_UNORM,\n\t\tD24_UNFORM_S8_UINT = (int)DXGI_FORMAT_D24_UNORM_S8_UINT,\n\t\tR8G8_UNORM = (int)DXGI_FORMAT_R8G8_UNORM,\n\t\tR8G8_UINT = (int)DXGI_FORMAT_R8G8_UINT,\n\t\tR8G8_SNORM = (int)DXGI_FORMAT_R8G8_SNORM,\n\t\tR8G8_SINT = (int)DXGI_FORMAT_R8G8_SINT,\n\t\tR16_FLOAT = (int)DXGI_FORMAT_R16_FLOAT,\n\t\t//D16_UNORM = (int)vk::Format::eD16Unorm,\n\t\t//R16_UNORM = (int)vk::Format::eR16Unorm,\n\t\tR16_UINT = (int)DXGI_FORMAT_R16_UINT,\n\t\tR16_SNORM = (int)DXGI_FORMAT_R16_SNORM,\n\t\tR16_SINT = (int)DXGI_FORMAT_R16_SINT,\n\t\tR8_UNORM = (int)DXGI_FORMAT_R8_UNORM,\n\t\tR8_UINT = (int)DXGI_FORMAT_R8_UINT,\n\t\tR8_SNORM = (int)DXGI_FORMAT_R8_SNORM,\n\t\tR8_SINT = (int)DXGI_FORMAT_R8_SINT,\n\t\tA8_UNORM = (int)DXGI_FORMAT_A8_UNORM,\n\t\t//BC1_UNORM = (int)vk::Format::eBc1RgbUnormBlock, //FIXME: is this correct?\n\t\t//BC1_UNORM_SRGB = (int)vk::Format::eBc1RgbSrgbBlock, //FIXME: is this correct?\n\t\t//BC2_UNORM = (int)vk::Format::eBc2UnormBlock,\n\t\t//BC2_UNORM_SRGB = (int)vk::Format::eBc2SrgbBlock,\n\t\t//BC3_UNORM = (int)vk::Format::eBc3UnormBlock,\n\t\t//BC3_UNORM_SRGB = (int)vk::Format::eBc3SrgbBlock,\n\t\t//BC4_UNORM = (int)vk::Format::eBc4UnormBlock,\n\t\t//BC4_SNORM = (int)vk::Format::eBc4SnormBlock,\n\t\t//BC5_UNORM = (int)vk::Format::eBc5UnormBlock,\n\t\t//BC5_SNORM = (int)vk::Format::eBc5SnormBlock,\n\t\tB5G6R5_UNORM = (int)DXGI_FORMAT_B5G6R5_UNORM,\n\t\tB5G5R5A1_UNORM = (int)DXGI_FORMAT_B5G5R5A1_UNORM,\n\t\tB8G8R8A8_UNORM = (int)DXGI_FORMAT_B8G8R8A8_UNORM,\n\t\tB8G8R8A8_UNORM_SRGB = (int)DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,\n\t\t//B8G8R8A8_SNORM = (int)vk::Format::eB8G8R8A8Snorm,\n\t\t//B8G8R8A8_UINT = (int)vk::Format::eB8G8R8A8Uint,\n\t\t//B8G8R8A8_SINT = (int)vk::Format::eB8G8R8A8Sint,\n\t\tB8G8R8X8_UNORM = (int)DXGI_FORMAT_B8G8R8X8_UNORM,\n\t\tB8G8R8X8_UNORM_SRGB = (int)DXGI_FORMAT_B8G8R8X8_UNORM_SRGB,\n\t\t//BC6H_UF16 = (int)vk::Format::eBc6HUfloatBlock,\n\t\t//BC6H_SF16 = (int)vk::Format::eBc6HSfloatBlock,\n\t\t//BC7_UNORM = (int)vk::Format::eBc7UnormBlock,\n\t\t//BC7_UNORM_SRGB = (int)vk::Format::eBc7SrgbBlock,\n\t\tB4G4R4A4_UNORM = (int)DXGI_FORMAT_B4G4R4A4_UNORM,\n\t\tD32_FLOAT_S8X24_UINT = (int)DXGI_FORMAT_D32_FLOAT_S8X24_UINT,\n\t};\n\n\tstatic inline std::string FormatToStr(Format format)\n\t{\n\t\tswitch (format)\n\t\t{\n\t\tcase Format::UNKNOWN: return \"UNKNOWN\";\n\t\tcase Format::R32G32B32A32_FLOAT: return \"R32G32B32A32_FLOAT\";\n\t\tcase Format::R32G32B32A32_UINT: return \"R32G32B32A32_UINT\";\n\t\tcase Format::R32G32B32A32_SINT: return \"R32G32B32A32_SINT\";\n\t\tcase Format::R32G32B32_FLOAT: return \"R32G32B32_FLOAT\";\n\t\tcase Format::R32G32B32_UINT: return \"R32G32B32_UINT\";\n\t\tcase Format::R32G32B32_SINT: return \"R32G32B32_SINT\";\n\t\tcase Format::R16G16B16A16_FLOAT: return \"R16G16B16A16_FLOAT\";\n\t\tcase Format::R16G16B16A16_UINT: return \"R16G16B16A16_UINT\";\n\t\tcase Format::R16G16B16A16_SINT: return \"R16G16B16A16_SINT\";\n\t\tcase Format::R16G16B16A16_UNORM: return \"R16G16B16A16_UNORM\";\n\t\tcase Format::R16G16B16A16_SNORM: return \"R16G16B16A16_SNORM\";\n\t\tcase Format::R32G32_FLOAT: return \"R32G32_FLOAT\";\n\t\tcase Format::R32G32_UINT: return \"R32G32_UINT\";\n\t\tcase Format::R32G32_SINT: return \"R32G32_SINT\";\n\t\tcase Format::R8G8B8A8_UNORM: return \"R8G8B8A8_UNORM\";\n\t\tcase Format::R8G8B8A8_UNORM_SRGB: return \"R8G8B8A8_UNORM_SRGB\";\n\t\tcase Format::R8G8B8A8_SNORM: return \"R8G8B8A8_SNORM\";\n\t\tcase Format::R8G8B8A8_UINT: return \"R8G8B8A8_UINT\";\n\t\tcase Format::R8G8B8A8_SINT: return \"R8G8B8A8_SINT\";\n\t\tcase Format::D32_FLOAT: return \"D32_FLOAT\";\n\t\tcase Format::R32_UINT: return \"R32_UINT\";\n\t\tcase Format::R32_SINT: return \"R32_SINT\";\n\t\tcase Format::R32_FLOAT: return \"R32_FLOAT\";\n\t\tcase Format::D24_UNFORM_S8_UINT: return \"D24_UNFORM_S8_UINT\";\n\t\tcase Format::R8_UNORM: return \"R8_UNORM\";\n\t\tcase Format::R10G10B10A2_UNORM: return \"R10G10B10A2_UNORM\";\n\t\tcase Format::R10G10B10A2_UINT: return \"R10G10B10A2_UINT\";\n\t\tcase Format::R11G11B10_FLOAT: return \"R11G11B10_FLOAT\";\n\t\tcase Format::R16G16_FLOAT: return \"R16G16_FLOAT\";\n\t\tcase Format::R16G16_UNORM: return \"R16G16_UNORM\";\n\t\tcase Format::R16G16_UINT: return \"R16G16_UINT\";\n\t\tcase Format::R16G16_SNORM: return \"R16G16_SNORM\";\n\t\tcase Format::R16G16_SINT: return \"R16G16_SINT\";\n\t\tcase Format::R8G8_UNORM: return \"R8G8_UNORM\";\n\t\tcase Format::R8G8_UINT: return \"R8G8_UINT\";\n\t\tcase Format::R8G8_SNORM: return \"R8G8_SNORM\";\n\t\tcase Format::R8G8_SINT: return \"R8G8_SINT\";\n\t\tcase Format::R16_UNORM: return \"R16_UNORM\";\n\t\tcase Format::R16_SNORM: return \"R16_SNORM\";\n\t\tcase Format::R8_SNORM: return \"R8_SNORM\";\n\t\tcase Format::A8_UNORM: return \"A8_UNORM\";\n\t\tcase Format::B5G6R5_UNORM: return \"B5G6R5_UNORM\";\n\t\tcase Format::B5G5R5A1_UNORM: return \"B5G5R5A1_UNORM\";\n\t\tcase Format::B4G4R4A4_UNORM: return \"B4G4R4A4_UNORM\";\n\n\t\tdefault: return \"UNKNOWN (DEFAULT SWITCH STATEMENT)\";\n\t\t}\n\t}\n\n\t// From: https://github.com/Microsoft/DirectXTK/blob/master/Src/LoaderHelpers.h\n\tstatic inline unsigned int BitsPerPixel(Format format)\n\t{\n\t\tswitch (format)\n\t\t{\n\t\t//case Format::R32G32B32A32_TYPELESS:\n\t\tcase Format::R32G32B32A32_FLOAT:\n\t\tcase Format::R32G32B32A32_UINT:\n\t\tcase Format::R32G32B32A32_SINT:\n\t\t\treturn 128;\n\n\t\t//case Format::R32G32B32_TYPELESS:\n\t\tcase Format::R32G32B32_FLOAT:\n\t\tcase Format::R32G32B32_UINT:\n\t\tcase Format::R32G32B32_SINT:\n\t\t\treturn 96;\n\n\t\t//case Format::R16G16B16A16_TYPELESS:\n\t\tcase Format::R16G16B16A16_FLOAT:\n\t\tcase Format::R16G16B16A16_UNORM:\n\t\tcase Format::R16G16B16A16_UINT:\n\t\tcase Format::R16G16B16A16_SNORM:\n\t\tcase Format::R16G16B16A16_SINT:\n\t\t//case Format::R32G32_TYPELESS:\n\t\tcase Format::R32G32_FLOAT:\n\t\tcase Format::R32G32_UINT:\n\t\tcase Format::R32G32_SINT:\n\t\t//case Format::R32G8X24_TYPELESS:\n\t\tcase Format::D32_FLOAT_S8X24_UINT:\n\t\t//case Format::R32_FLOAT_X8X24_TYPELESS:\n\t\t//case Format::X32_TYPELESS_G8X24_UINT:\n\t\t//case Format::Y416:\n\t\t//case Format::Y210:\n\t\t//case Format::Y216:\n\t\t\treturn 64;\n\n\t\t//case Format::R10G10B10A2_TYPELESS:\n\t\tcase Format::R10G10B10A2_UNORM:\n\t\tcase Format::R10G10B10A2_UINT:\n\t\tcase Format::R11G11B10_FLOAT:\n\t\t//case Format::R8G8B8A8_TYPELESS:\n\t\tcase Format::R8G8B8A8_UNORM:\n\t\tcase Format::R8G8B8A8_UNORM_SRGB:\n\t\tcase Format::R8G8B8A8_UINT:\n\t\tcase Format::R8G8B8A8_SNORM:\n\t\tcase Format::R8G8B8A8_SINT:\n\t\t//case Format::R16G16_TYPELESS:\n\t\tcase Format::R16G16_FLOAT:\n\t\tcase Format::R16G16_UNORM:\n\t\tcase Format::R16G16_UINT:\n\t\tcase Format::R16G16_SNORM:\n\t\tcase Format::R16G16_SINT:\n\t\tcase Format::R32_TYPELESS:\n\t\tcase Format::D32_FLOAT:\n\t\tcase Format::R32_FLOAT:\n\t\tcase Format::R32_UINT:\n\t\tcase Format::R32_SINT:\n\t\t//case Format::R24G8_TYPELESS:\n\t\t//case Format::D24_UNORM_S8_UINT:\n\t\t//case Format::R24_UNORM_X8_TYPELESS:\n\t\t//case Format::X24_TYPELESS_G8_UINT:\n\t\t//case Format::R9G9B9E5_SHAREDEXP:\n\t\t//case Format::R8G8_B8G8_UNORM:\n\t\t//case Format::G8R8_G8B8_UNORM:\n\t\tcase Format::B8G8R8A8_UNORM:\n\t\tcase Format::B8G8R8X8_UNORM:\n\t\t//case Format::R10G10B10_XR_BIAS_A2_UNORM:\n\t\t//case Format::B8G8R8A8_TYPELESS:\n\t\tcase Format::B8G8R8A8_UNORM_SRGB:\n\t\t//case Format::B8G8R8X8_TYPELESS:\n\t\tcase Format::B8G8R8X8_UNORM_SRGB:\n\t\t//case Format::AYUV:\n\t\t//case Format::Y410:\n\t\t//case Format::YUY2:\n\t\t\treturn 32;\n\n\t\t//case Format::P010:\n\t\t//case Format::P016:\n\t\t//\treturn 24;\n\n\t\t//case Format::R8G8_TYPELESS:\n\t\tcase Format::R8G8_UNORM:\n\t\tcase Format::R8G8_UINT:\n\t\tcase Format::R8G8_SNORM:\n\t\tcase Format::R8G8_SINT:\n\t\t//case Format::R16_TYPELESS:\n\t\tcase Format::R16_FLOAT:\n\t\t//case Format::D16_UNORM:\n\t\tcase Format::R16_UNORM:\n\t\tcase Format::R16_UINT:\n\t\tcase Format::R16_SNORM:\n\t\tcase Format::R16_SINT:\n\t\tcase Format::B5G6R5_UNORM:\n\t\tcase Format::B5G5R5A1_UNORM:\n\t\t//case Format::A8P8:\n\t\tcase Format::B4G4R4A4_UNORM:\n\t\t\treturn 16;\n\n\t\t//case Format::NV12:\n\t\t//case Format::420_OPAQUE:\n\t\t//case Format::NV11:\n\t\t//\treturn 12;\n\n\t\t//case Format::R8_TYPELESS:\n\t\tcase Format::R8_UNORM:\n\t\tcase Format::R8_UINT:\n\t\tcase Format::R8_SNORM:\n\t\tcase Format::R8_SINT:\n\t\tcase Format::A8_UNORM:\n\t\t//case Format::AI44:\n\t\t//case Format::IA44:\n\t\t//case Format::P8:\n\t\t\treturn 8;\n\n\t\t//case Format::R1_UNORM:\n\t\t//\treturn 1;\n\n\t\t//case Format::BC1_TYPELESS:\n\t\t//case Format::BC1_UNORM:\n\t\t//case Format::BC1_UNORM_SRGB:\n\t\t//case Format::BC4_TYPELESS:\n\t\t//case Format::BC4_UNORM:\n\t\t//case Format::BC4_SNORM:\n\t\t//\treturn 4;\n\n\t\t//case Format::BC2_TYPELESS:\n\t\t//case Format::BC2_UNORM:\n\t\t//case Format::BC2_UNORM_SRGB:\n\t\t//case Format::BC3_TYPELESS:\n\t\t//case Format::BC3_UNORM:\n\t\t//case Format::BC3_UNORM_SRGB:\n\t\t//case Format::BC5_TYPELESS:\n\t\t//case Format::BC5_UNORM:\n\t\t//case Format::BC5_SNORM:\n\t\t//case Format::BC6H_TYPELESS:\n\t\t//case Format::BC6H_UF16:\n\t\t//case Format::BC6H_SF16:\n\t\t//case Format::BC7_TYPELESS:\n\t\t//case Format::BC7_UNORM:\n\t\t//case Format::BC7_UNORM_SRGB:\n\t\t//\treturn 8;\n\n//#if (_WIN32_WINNT >= _WIN32_WINNT_WIN10)\n//\n//\t\tcase Format::V408:\n//\t\t\treturn 24;\n//\n//\t\tcase Format::P208:\n//\t\tcase Format::V208:\n//\t\t\treturn 16;\n//\n//#endif // (_WIN32_WINNT >= _WIN32_WINNT_WIN10)\n//\n//#if defined(_XBOX_ONE) && defined(_TITLE)\n//\n//\t\tcase Format::R10G10B10_7E3_A2_FLOAT:\n//\t\tcase Format::R10G10B10_6E4_A2_FLOAT:\n//\t\tcase Format::R10G10B10_SNORM_A2_UNORM:\n//\t\t\treturn 32;\n//\n//\t\tcase Format::D16_UNORM_S8_UINT:\n//\t\tcase Format::R16_UNORM_X8_TYPELESS:\n//\t\tcase Format::X16_TYPELESS_G8_UINT:\n//\t\t\treturn 24;\n//\n//\t\tcase Format::R4G4_UNORM:\n//\t\t\treturn 8;\n//\n//#endif // _XBOX_ONE && _TITLE\n\n\t\tdefault:\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\tstatic inline unsigned int BytesPerPixel(Format format)\n\t{\n\t\treturn BitsPerPixel(format) / 8;\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/d3d12/d3d12_fence.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"../util/log.hpp\"\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tFence* CreateFence(Device* device)\n\t{\n\t\tauto fence = new Fence();\n\t\tauto n_device = device->m_native;\n\n\t\t// create the fences\n\t\tTRY_M(n_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence->m_native)),\n\t\t\t\"Failed to create fence\");\n\t\tfence->m_native->SetName(L\"SimpleFence\");\n\n\t\t// create a handle to a fence event\n\t\tfence->m_fence_event = CreateEvent(nullptr, FALSE, FALSE, nullptr);\n\t\tif (fence->m_fence_event == nullptr)\n\t\t{\n\t\t\tLOGC(\"Failed to create fence event\");\n\t\t}\n\n\t\treturn fence;\n\t}\n\n\tvoid SetName(Fence * fence, std::wstring name)\n\t{\n\t\tfence->m_native->SetName(name.c_str());\n\t}\n\n\tvoid Signal(Fence* fence, CommandQueue* cmd_queue)\n\t{\n\t\tTRY_M(cmd_queue->m_native->Signal(fence->m_native, fence->m_fence_value),\n\t\t\t\"Failed to set fence signal\");\n\t}\n\n\tvoid WaitFor(Fence* fence)\n\t{\n\t\tif (fence->m_native->GetCompletedValue() < fence->m_fence_value)\n\t\t{\n\t\t\t// we have the fence create an event which is signaled once the fence's current value is \"fenceValue\"\n\t\t\tTRY_M(fence->m_native->SetEventOnCompletion(fence->m_fence_value, fence->m_fence_event),\n\t\t\t\t\"Failed to set fence event.\");\n\n\t\t\tWaitForSingleObject(fence->m_fence_event, INFINITE);\n\t\t}\n\n\t\t// increment fenceValue for next frame (or usage)\n\t\tfence->m_fence_value++;\n\t}\n\n\tvoid Destroy(Fence* fence)\n\t{\n\t\tSAFE_RELEASE(fence->m_native)\n\t\tCloseHandle(fence->m_fence_event);\n\t\tdelete fence;\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_functions.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <memory>\n#include <variant>\n#include <DirectXMath.h>\n\n#include \"d3d12_structs.hpp\"\n#include \"d3d12_constant_buffer_pool.hpp\"\n#include \"d3d12_dynamic_descriptor_heap.hpp\"\n#include \"d3d12_descriptors_allocations.hpp\"\n\nnamespace wr::d3d12\n{\n\tstruct TextureResource;\n\n\t// Device\n\t[[nodiscard]] Device* CreateDevice();\n\tRaytracingType GetRaytracingType(Device* device);\n\tvoid Destroy(Device* device);\n\tvoid SetName(Device* device, std::wstring name);\n\n\t// CommandQueue\n\t[[nodiscard]] CommandQueue* CreateCommandQueue(Device* device, CmdListType type);\n\tvoid Execute(CommandQueue* cmd_queue, std::vector<CommandList*> const & cmd_lists, Fence* fence);\n\tvoid Destroy(CommandQueue* cmd_queue);\n\tvoid SetName(CommandQueue* cmd_queue, std::wstring name);\n\n\t// CommandList\n\t[[nodiscard]] CommandList* CreateCommandList(Device* device, unsigned int num_allocators, CmdListType type);\n\tvoid SetName(CommandList* cmd_list, std::string const & name);\n\tvoid SetName(CommandList* cmd_list, std::wstring const & name);\n\tvoid Begin(CommandList* cmd_list, unsigned int frame_idx);\n\tvoid End(CommandList* cmd_list);\n\tvoid ExecuteBundle(CommandList* cmd_list, CommandList* bundle);\n\tvoid ExecuteIndirect(CommandList* cmd_list, CommandSignature* cmd_signature, IndirectCommandBuffer* buffer, uint32_t frame_idx);\n\tvoid BindRenderTarget(CommandList* cmd_list, RenderTarget* render_target, bool clear = true, bool clear_depth = true);\n\tvoid BindRenderTargetVersioned(CommandList* cmd_list, RenderTarget* render_target, unsigned int frame_idx, bool clear = true, bool clear_depth = true);\n\tvoid BindRenderTargetOnlyDepth(CommandList* cmd_list, RenderTarget* render_target, bool clear = true);\n\tvoid BindViewport(CommandList* cmd_list, Viewport const & viewport);\n\tvoid BindPipeline(CommandList* cmd_list, PipelineState* pipeline_state);\n\tvoid BindComputePipeline(CommandList* cmd_list, PipelineState* pipeline_state);\n\tvoid BindRaytracingPipeline(CommandList* cmd_list, StateObject* state_object, bool fallback = false);\n\tvoid BindDescriptorHeap(CommandList* cmd_list, DescriptorHeap* heap, DescriptorHeapType type, unsigned int frame_idx, bool fallback = false);\n\tvoid BindDescriptorHeaps(CommandList* cmd_list, bool fallback = false);\n\tvoid SetPrimitiveTopology(CommandList* cmd_list, D3D12_PRIMITIVE_TOPOLOGY topology);\n\tvoid BindConstantBuffer(CommandList* cmd_list, HeapResource* buffer, unsigned int root_parameter_idx, unsigned int frame_idx);\n\tvoid Bind32BitConstants(CommandList* cmd_list, const void* data_to_set, unsigned int num_of_values_to_set, unsigned int dest_offset_in_32bit_values, unsigned int root_parameter_idx);\n\tvoid BindCompute32BitConstants(CommandList* cmd_list, const void* data_to_set, unsigned int num_of_values_to_set, unsigned int dest_offset_in_32bit_values, unsigned int root_parameter_idx);\n\tvoid BindComputeConstantBuffer(CommandList* cmd_list, HeapResource* buffer, unsigned int root_parameter_idx, unsigned int frame_idx);\n\tvoid BindComputeShaderResourceView(CommandList* cmd_list, ID3D12Resource* resource, unsigned int root_parameter_idx);\n\tvoid BindComputeUnorederedAccessView(CommandList* cmd_list, ID3D12Resource* resource, unsigned int root_parameter_idx);\n\t//void Bind(CommandList& cmd_list, TextureArray& ta, unsigned int root_param_index);\n\tvoid BindDescriptorTable(CommandList* cmd_list, DescHeapGPUHandle& handle, unsigned int root_param_index);\n\tvoid BindComputeDescriptorTable(CommandList* cmd_list, DescHeapGPUHandle& handle, unsigned int root_param_index);\n\t//void Bind(CommandList& cmd_list, std::vector<DescriptorHeap*> const & heaps);\n\tvoid BindVertexBuffer(CommandList* cmd_list, StagingBuffer* buffer, std::size_t offset, std::size_t size, std::size_t m_stride);\n\tvoid BindIndexBuffer(CommandList* cmd_list, StagingBuffer* buffer, std::uint32_t offset, std::uint32_t size);\n\tvoid Draw(CommandList* cmd_list, std::uint32_t vertex_count, std::uint32_t inst_count, std::uint32_t vertex_start);\n\tvoid DrawIndexed(CommandList* cmd_list, std::uint32_t idx_count, std::uint32_t inst_count, std::uint32_t idx_start, std::uint32_t vertex_start);\n\tvoid Dispatch(CommandList* cmd_list, unsigned int thread_group_count_x, unsigned int thread_group_count_y, unsigned int thread_group_count_z);\n\tvoid Transition(CommandList* cmd_list, RenderTarget* render_target, unsigned int frame_index, ResourceState from, ResourceState to);\n\tvoid Transition(CommandList* cmd_list, RenderTarget* render_target, ResourceState from, ResourceState to);\n\tvoid Transition(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to);\n\tvoid Transition(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to, unsigned int first_subresource, unsigned int num_subresources);\n\tvoid TransitionSubresource(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to, unsigned int subresource);\n\tvoid Transition(CommandList* cmd_list, std::vector<TextureResource*> const& textures, ResourceState from, ResourceState to);\n\tvoid Transition(CommandList* cmd_list, IndirectCommandBuffer* buffer, ResourceState from, ResourceState to, uint32_t frame_idx);\n\tvoid Transition(CommandList* cmd_list, StagingBuffer* buffer, ResourceState from, ResourceState to);\n\tvoid TransitionDepth(CommandList* cmd_list, RenderTarget* render_target, ResourceState from, ResourceState to);\n\tvoid Alias(CommandList* cmd_list, TextureResource* resource_before, TextureResource* resource_after);\n\tvoid UAVBarrier(CommandList* cmd_list, std::vector<TextureResource*> const & resources);\n\tvoid UAVBarrier(CommandList* cmd_list, std::vector<ID3D12Resource*> const & resources);\n\tvoid DispatchRays(CommandList* cmd_list, ShaderTable* hitgroup_table, ShaderTable* miss_table, ShaderTable* raygen_table, std::uint32_t width, std::uint32_t height, std::uint32_t depth, unsigned int frame_idx);\n\t// void Transition(CommandList* cmd_list, Texture* texture, ResourceState from, ResourceState to);\n\tvoid Destroy(CommandList* cmd_list);\n\n\t// Command List Signature\n\tCommandSignature* CreateCommandSignature(Device* device, RootSignature* root_signature, std::vector<D3D12_INDIRECT_ARGUMENT_DESC> arg_descs, size_t byte_stride);\n\tvoid SetName(CommandSignature* cmd_signature, std::wstring name);\n\tvoid Destroy(CommandSignature* cmd_signature);\n\n\t// RenderTarget\n\t[[nodiscard]] RenderTarget* CreateRenderTarget(Device* device, unsigned int width, unsigned int height, desc::RenderTargetDesc descriptor);\n\tvoid SetName(RenderTarget* render_target, std::wstring name);\n\tvoid SetName(RenderTarget* render_target, std::string name);\n\tunsigned int GetRenderTargetWidth(RenderTarget* render_target);\n\tunsigned int GetRenderTargetHeight(RenderTarget* render_target);\n\tvoid CreateRenderTargetViews(RenderTarget* render_target, Device* device);\n\tvoid CreateDepthStencilBuffer(RenderTarget* render_target, Device* device, unsigned int width, unsigned int height);\n\tvoid CreateSRVFromDSV(RenderTarget* render_target, DescHeapCPUHandle& handle);\n\tvoid CreateSRVFromRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int num, Format formats[8]);\n\tvoid CreateUAVFromRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int num, Format formats[8]);\n\tvoid CreateUAVFromSpecificRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int id, Format format);\n\tvoid CreateSRVFromSpecificRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int id, Format format);\n\tvoid CreateSRVFromStructuredBuffer(HeapResource* structured_buffer, DescHeapCPUHandle& handle, unsigned int id); // FIXME: Wrong location\n\t// void CreateUAVFromTexture(Texture* tex, DescHeapCPUHandle& handle, unsigned int mip_slice = 0, unsigned int array_slice = 0);\n\t// void CreateSRVFromTexture(Texture* tex, DescHeapCPUHandle& handle);\n\tvoid Resize(RenderTarget** render_target, Device* device, unsigned int width, unsigned int height);\n\tvoid IncrementFrameIdx(RenderTarget* render_target);\n\tvoid DestroyDepthStencilBuffer(RenderTarget* render_target);\n\tvoid DestroyRenderTargetViews(RenderTarget* render_target);\n\tvoid Destroy(RenderTarget* render_target);\n\n\t// Texture\n\t[[nodiscard]] TextureResource* CreateTexture(Device* device, desc::TextureDesc* description, bool allow_uav);\n\t[[nodiscard]] TextureResource* CreatePlacedTexture(Device* device, desc::TextureDesc* description, bool allow_uav, Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap);\n\tvoid SetName(TextureResource* tex, std::wstring name);\n\tvoid CreateSRVFromTexture(TextureResource* tex);\n\tvoid CreateSRVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle);\n\tvoid CreateSRVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_levels, unsigned int most_detailed_mip);\n\tvoid CreateSRVFromCubemapFace(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_levels, unsigned int most_detailed_mip, unsigned int face_idx);\n\tvoid CreateUAVFromTexture(TextureResource* tex, unsigned int mip_slice);\n\tvoid CreateUAVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_slice);\n\tvoid CreateUAVFromCubemapFace(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_slice, unsigned int face_idx);\n\tvoid CreateRTVFromTexture2D(TextureResource* tex);\n\tvoid CreateRTVFromCubemap(TextureResource* tex);\n\t//void CreateUAVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_slice = 0, unsigned int array_slice = 0);\n\tvoid SetShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex);\n\tvoid SetShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count = 1);\n\tvoid SetShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex);\n\tvoid SetShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count = 1);\n\tvoid SetRTShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex);\n\tvoid SetRTShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count = 1);\n\tvoid SetRTShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex);\n\tvoid SetRTShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count = 1);\n\tvoid CopyResource(wr::d3d12::CommandList* cmd_list, TextureResource* src_texture, TextureResource* dst_texture);\n\tvoid Destroy(TextureResource* tex);\n\n\t// Format test and support functions\n\tbool CheckUAVCompatibility(Format format);\n\tbool CheckOptionalUAVFormat(Format format);\n\tbool CheckBGRFormat(Format format);\n\tbool CheckSRGBFormat(Format format);\n\tbool IsOptionalFormatSupported(Device* device, Format format);\n\tFormat RemoveSRGB(Format format);\n\tFormat BGRtoRGB(Format format);\n\n\t// Read-back buffer\n\t//! Create a readback buffer\n\t/*!\n\t\t\\param aligned_buffer_size The size of the buffer you want to create aligned to 256.\n\t*/\n\t[[nodiscard]] ReadbackBufferResource* CreateReadbackBuffer(Device* device, std::uint32_t aligned_buffer_size);\n\t//! Map a readback buffer\n\t/*!\n\t\t\\param aligned_buffer_size The size of the buffer you want to create aligned to 256.\n\t*/\n\tvoid* MapReadbackBuffer(ReadbackBufferResource* const readback_buffer, std::uint32_t aligned_buffer_size);\n\t//! Unmap a readback buffer\n\tvoid UnmapReadbackBuffer(ReadbackBufferResource* const readback_buffer);\n\tvoid SetName(ReadbackBufferResource* readback_buffer, std::wstring name);\n\tvoid Destroy(ReadbackBufferResource* readback_buffer);\n\n\t// RenderWindow\n\t[[nodiscard]] RenderWindow* CreateRenderWindow(Device* device, HWND window, CommandQueue* cmd_queue, unsigned int num_back_buffers);\n\t[[nodiscard]] RenderWindow* CreateRenderWindow(Device* device, IUnknown* window, CommandQueue* cmd_queue, unsigned int num_back_buffers);\n\tvoid Resize(RenderWindow* render_window, Device* device, unsigned int width, unsigned int height);\n\tvoid Present(RenderWindow* render_window);\n\tvoid Destroy(RenderWindow* render_window);\n\n\t// Descriptor Heap\n\t[[nodiscard]] DescriptorHeap* CreateDescriptorHeap(Device* device, desc::DescriptorHeapDesc const & descriptor);\n\t[[nodiscard]] DescHeapGPUHandle GetGPUHandle(DescriptorHeap* desc_heap, unsigned int frame_idx, unsigned int index = 0);\n\t[[nodiscard]] DescHeapCPUHandle GetCPUHandle(DescriptorHeap* desc_heap, unsigned int frame_idx, unsigned int index = 0);\n\tvoid SetName(DescriptorHeap* desc_heap, std::wstring name);\n\tvoid Offset(DescHeapGPUHandle& handle, unsigned int index, unsigned int increment_size);\n\tvoid Offset(DescHeapCPUHandle& handle, unsigned int index, unsigned int increment_size);\n\tvoid Destroy(DescriptorHeap* desc_heap);\n\n\t// Fence\n\t[[nodiscard]] Fence* CreateFence(Device* device);\n\tvoid SetName(Fence* fence, std::wstring name);\n\tvoid Signal(Fence* fence, CommandQueue* cmd_queue);\n\tvoid WaitFor(Fence* fence);\n\tvoid Destroy(Fence* fence);\n\n\t// Viewport\n\t[[nodiscard]] Viewport CreateViewport(int width, int height);\n\tvoid ResizeViewport(Viewport& viewport, int width, int height);\n\n\t// Shader\n\t[[nodiscard]] std::variant<Shader*, std::string> LoadShader(Device* device, ShaderType type, std::string const& path, std::string const& entry = \"main\", std::vector<std::pair<std::wstring, std::wstring>> defines = { {} });\n\tvoid Destroy(Shader* shader);\n\n\t// Root Signature\n\t[[nodiscard]] RootSignature* CreateRootSignature(desc::RootSignatureDesc create_info);\n\tvoid SetName(RootSignature* root_signature, std::wstring name);\n\tvoid FinalizeRootSignature(RootSignature* root_signature, Device* device);\n\tvoid RefinalizeRootSignature(RootSignature* root_signature, Device* device);\n\tvoid Destroy(RootSignature* root_signature);\n\n\t// Pipeline State\n\t[[nodiscard]] PipelineState* CreatePipelineState(); // TODO: Creation of root signature and pipeline are not the same related to the descriptor.\n\tvoid SetName(PipelineState* pipeline_state, std::wstring name);\n\tvoid SetVertexShader(PipelineState* pipeline_state, Shader* shader);\n\tvoid SetFragmentShader(PipelineState* pipeline_state, Shader* shader);\n\tvoid SetComputeShader(PipelineState* pipeline_state, Shader* shader);\n\tvoid SetRootSignature(PipelineState* pipeline_state, RootSignature* root_signature);\n\tvoid FinalizePipeline(PipelineState* pipeline_state, Device* device, desc::PipelineStateDesc desc);\n\tvoid RefinalizePipeline(PipelineState* pipeline_state); // TODO: Deprecate this. This should be part of create so it\n\tvoid Destroy(PipelineState* pipeline_state);\n\n\t// Staging Buffer\n\t[[nodiscard]] StagingBuffer* CreateStagingBuffer(Device* device, void* data, std::uint64_t size, std::uint64_t m_stride, ResourceState resource_state);\n\tvoid SetName(StagingBuffer* buffer, std::wstring name);\n\tvoid UpdateStagingBuffer(StagingBuffer* buffer, void* data, std::uint64_t size, std::uint64_t offset);\n\tvoid StageBuffer(StagingBuffer* buffer, CommandList* cmd_list);\n\tvoid StageBufferRegion(StagingBuffer* buffer, std::uint64_t size, std::uint64_t offset, CommandList* cmd_list);\n\tvoid FreeStagingBuffer(StagingBuffer* buffer);\n\tvoid Evict(StagingBuffer* buffer);\n\tvoid CreateRawSRVFromStagingBuffer(StagingBuffer* buffer, DescHeapCPUHandle& handle, unsigned int count, Format format = Format::R32_TYPELESS);\n\tvoid CreateStructuredBufferSRVFromStagingBuffer(StagingBuffer* buffer, DescHeapCPUHandle& handle, unsigned int count, Format format = Format::R32_TYPELESS);\n\tvoid MakeResident(StagingBuffer* buffer);\n\tvoid Destroy(StagingBuffer* buffer);\n\n\t// Heap\n\t[[nodiscard]] Heap<HeapOptimization::SMALL_BUFFERS>* CreateHeap_SBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count);\n\t[[nodiscard]] Heap<HeapOptimization::BIG_BUFFERS>* CreateHeap_BBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count);\n\t[[nodiscard]] Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* CreateHeap_SSBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count);\n\t[[nodiscard]] Heap<HeapOptimization::BIG_STATIC_BUFFERS>* CreateHeap_BSBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count);\n\t[[nodiscard]] HeapResource* AllocConstantBuffer(Heap<HeapOptimization::SMALL_BUFFERS>* heap, std::uint64_t size_in_bytes);\n\t[[nodiscard]] HeapResource* AllocConstantBuffer(Heap<HeapOptimization::BIG_BUFFERS>* heap, std::uint64_t size_in_bytes);\n\t[[nodiscard]] HeapResource* AllocByteAddressBuffer(Heap<HeapOptimization::BIG_BUFFERS>* heap, std::uint64_t size_in_bytes);\n\t[[nodiscard]] HeapResource* AllocStructuredBuffer(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, std::uint64_t size_in_bytes, std::uint64_t stride, bool used_as_uav);\n\t[[nodiscard]] HeapResource* AllocGenericBuffer(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, std::uint64_t size_in_bytes);\n\tvoid SetName(Heap<HeapOptimization::SMALL_BUFFERS>* heap, std::wstring name);\n\tvoid SetName(Heap<HeapOptimization::BIG_BUFFERS>* heap, std::wstring name);\n\tvoid SetName(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap, std::wstring name);\n\tvoid SetName(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, std::wstring name);\n\tvoid DeallocConstantBuffer(Heap<HeapOptimization::SMALL_BUFFERS>* heap, HeapResource* heapResource);\n\tvoid DeallocConstantBuffer(Heap<HeapOptimization::BIG_BUFFERS>* heap, HeapResource* heapResource);\n\tvoid DeallocBuffer(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, HeapResource* heapResource);\n\tvoid MapHeap(Heap<HeapOptimization::SMALL_BUFFERS>* heap);\n\tvoid MapHeap(Heap<HeapOptimization::BIG_BUFFERS>* heap);\n\tvoid UnmapHeap(Heap<HeapOptimization::SMALL_BUFFERS>* heap);\n\tvoid UnmapHeap(Heap<HeapOptimization::BIG_BUFFERS>* heap);\n\tvoid MakeResident(Heap<HeapOptimization::SMALL_BUFFERS>* heap);\n\tvoid MakeResident(Heap<HeapOptimization::BIG_BUFFERS>* heap);\n\tvoid MakeResident(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap);\n\tvoid MakeResident(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap);\n\tvoid EnqueueMakeResident(Heap<HeapOptimization::SMALL_BUFFERS>* heap, Fence* fence); // Untested\n\tvoid EnqueueMakeResident(Heap<HeapOptimization::BIG_BUFFERS>* heap, Fence* fence);  // Untested\n\tvoid EnqueueMakeResident(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap, Fence* fence); // Untested\n\tvoid EnqueueMakeResident(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, Fence* fence);  // Untested\n\tvoid Evict(Heap<HeapOptimization::SMALL_BUFFERS>* heap);\n\tvoid Evict(Heap<HeapOptimization::BIG_BUFFERS>* heap);\n\tvoid Evict(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap);\n\tvoid Evict(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap);\n\tvoid Destroy(Heap<HeapOptimization::SMALL_BUFFERS>* heap);\n\tvoid Destroy(Heap<HeapOptimization::BIG_BUFFERS>* heap);\n\tvoid Destroy(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap);\n\tvoid Destroy(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap);\n\n\t// Resources\n\tvoid UpdateConstantBuffer(HeapResource* buffer, unsigned int frame_idx, void* data, std::uint64_t size_in_bytes);\n\tvoid UpdateStructuredBuffer(HeapResource* buffer,\n\t\tunsigned int frame_idx,\n\t\tvoid* data,\n\t\tstd::uint64_t size_in_bytes,\n\t\tstd::uint64_t offset,\n\t\tstd::uint64_t stride,\n\t\tCommandList* cmd_list);\n\tvoid UpdateByteAddressBuffer(HeapResource* buffer, unsigned int frame_idx, void* data, std::uint64_t size_in_bytes);\n\tvoid CreateSRVFromByteAddressBuffer(HeapResource* resource, DescHeapCPUHandle& handle, unsigned int id, unsigned int count);\n\n\t// Indirect Command Buffer\n\t[[nodiscard]] IndirectCommandBuffer* CreateIndirectCommandBuffer(Device* device, std::size_t max_num_buffers, std::size_t command_size, uint32_t versions);\n\tvoid SetName(IndirectCommandBuffer* buffer, std::wstring name);\n\tvoid StageBuffer(CommandList* cmd_list, IndirectCommandBuffer* buffer, void* data, std::size_t num_commands, uint32_t frame_idx);\n\n\t// State Object\n\t[[nodiscard]] StateObject* CreateStateObject(Device* device, desc::StateObjectDesc desc);\n\tvoid RecreateStateObject(StateObject* state_object);\n\tvoid SetGlobalRootSignature(StateObject* state_object, RootSignature* global_root_signature);\n\t[[nodiscard]] std::uint64_t GetShaderIdentifierSize(Device* device);\n\t[[nodiscard]] void* GetShaderIdentifier(Device* device, StateObject* obj, std::string const & name);\n\tvoid SetName(StateObject* obj, std::wstring name);\n\tvoid Destroy(StateObject* obj);\n\n\t// Acceelration Structure\n\t[[nodiscard]] AccelerationStructure CreateBottomLevelAccelerationStructures(Device* device,\n\t\tCommandList* cmd_list,\n\t\tDescriptorHeap* desc_heap,\n\t\tstd::vector<desc::GeometryDesc> geometry);\n\n\t[[nodiscard]] AccelerationStructure CreateTopLevelAccelerationStructure(Device* device,\n\t\tCommandList* cmd_list,\n\t\tDescriptorHeap* desc_heap,\n\t\tstd::vector<desc::BlasDesc> blas_list);\n\n\tvoid DestroyAccelerationStructure(AccelerationStructure& structure);\n\tvoid UAVBarrierAS(CommandList* cmd_list, AccelerationStructure const & structure, std::uint32_t frame_idx);\n\n\tvoid UpdateTopLevelAccelerationStructure(AccelerationStructure& tlas, Device* device,\n\t\tCommandList* cmd_list,\n\t\tDescriptorHeap* desc_heap,\n\t\tstd::vector<desc::BlasDesc> blas_list, std::uint32_t frame_idx);\n\n\tvoid CreateOrUpdateTLAS(Device* device, CommandList* cmd_list, bool& requires_init, d3d12::AccelerationStructure& out_tlas,\n\t\tstd::vector<desc::BlasDesc> blas_list, std::uint32_t frame_idx);\n\n\t// Shader Record\n\t[[nodiscard]] ShaderRecord CreateShaderRecord(void* identifier, std::uint64_t identifier_size, void* local_root_args = nullptr, std::uint64_t local_root_args_size = 0);\n\tvoid CopyShaderRecord(ShaderRecord src, void* dest);\n\n\t// Shader Table\n\t[[nodiscard]] ShaderTable* CreateShaderTable(Device* device,\n\t\t\tstd::uint64_t num_shader_records,\n\t\t\tstd::uint64_t shader_record_size);\n\tvoid AddShaderRecord(ShaderTable* table, ShaderRecord record);\n\tvoid SetName(AccelerationStructure& acceleration_structure, std::wstring name);\n\tvoid Destroy(ShaderTable* table);\n\n} /* wr::d3d12 */\n"
  },
  {
    "path": "src/d3d12/d3d12_heap.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"d3d12_defines.hpp\"\n#include \"../util/bitmap_allocator.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tnamespace internal\n\t{\n\t\tstatic const auto MakeResidentSingle = [](auto heap)\n\t\t{\n\t\t\tdecltype(Device::m_native) n_device;\n\t\t\theap->m_native->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\t\tstd::array<ID3D12Pageable*, 1> objects{ heap->m_native };\n\t\t\tn_device->MakeResident(1, objects.data());\n\t\t};\n\n\t\tstatic const auto EnqueueMakeResidentSingle = [](auto heap, auto fence)\n\t\t{\n\t\t\tdecltype(Device::m_native) n_device;\n\t\t\theap->m_native->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\t\tstd::array<ID3D12Pageable*, 1> objects{ heap->m_native };\n\t\t\tn_device->EnqueueMakeResident(D3D12_RESIDENCY_FLAG_DENY_OVERBUDGET, 1, objects.data(), fence->m_native, fence->m_fence_value);\n\t\t};\n\n\t\tstatic const auto EvictSingle = [](auto heap)\n\t\t{\n\t\t\tdecltype(Device::m_native) n_device;\n\t\t\theap->m_native->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\t\tstd::array<ID3D12Pageable*, 1> objects{ heap->m_native };\n\t\t\tn_device->Evict(1, objects.data());\n\t\t};\n\t}\n\n\tHeap<HeapOptimization::SMALL_BUFFERS>* CreateHeap_SBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource , unsigned int versioning_count)\n\t{\n\t\tauto heap = new Heap<HeapOptimization::SMALL_BUFFERS>();\n\t\theap->m_mapped = false;\n\t\theap->m_versioning_count = versioning_count;\n\t\theap->m_current_offset = 0;\n\t\theap->m_alignment = 256;\n\n\t\tauto aligned_size = SizeAlignTwoPower(size_in_bytes, 65536);\n\n\t\theap->m_heap_size = aligned_size;\n\n\t\tauto page_frame_count = SizeAlignTwoPower(heap->m_heap_size / heap->m_alignment, 64) / 64;\n\n\t\theap->m_bitmap.resize(page_frame_count);\n\n\t\tfor (int i = 0; i < heap->m_bitmap.size(); ++i) \n\t\t{\n\t\t\theap->m_bitmap[i] = 0xffffffffffffffff;\n\t\t}\n\n\t\tauto heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\tauto resource_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size, D3D12_RESOURCE_FLAG_NONE);\n\n\t\tTRY_M(device->m_native->CreateCommittedResource(\n\t\t\t&heap_properties,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&resource_desc,\n\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&heap->m_native)),\n\t\t\t\"Failed to create small buffer optimized heap.\");\n\n\t\treturn heap;\n\t}\n\n\tHeap<HeapOptimization::BIG_BUFFERS>* CreateHeap_BBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count)\n\t{\n\t\tauto heap = new Heap<HeapOptimization::BIG_BUFFERS>();\n\t\theap->m_mapped = false;\n\t\theap->m_versioning_count = versioning_count;\n\t\theap->m_current_offset = 0;\n\t\theap->m_alignment = 65536;\n\n\t\tauto aligned_size = SizeAlignTwoPower(size_in_bytes, 65536);\n\n\t\theap->m_heap_size = aligned_size;\n\n\t\tauto page_frame_count = SizeAlignTwoPower(heap->m_heap_size / heap->m_alignment, 64) / 64;\n\n\t\theap->m_bitmap.resize(page_frame_count);\n\n\t\tfor (int i = 0; i < heap->m_bitmap.size(); ++i) \n\t\t{\n\t\t\theap->m_bitmap[i] = 0xffffffffffffffff;\n\t\t}\n\n\t\tD3D12_HEAP_PROPERTIES heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\n\t\tCD3DX12_HEAP_DESC desc = {};\n\t\tdesc.SizeInBytes = heap->m_heap_size;\n\t\tdesc.Properties = heap_properties;\n\t\tdesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;\n\t\tswitch (resource_type)\n\t\t{\n\t\tcase ResourceType::BUFFER:\n\t\t\tdesc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;\n\t\t\tbreak;\n\t\tcase ResourceType::TEXTURE:\n\t\t\tdesc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES;\n\t\t\tbreak;\n\t\tcase ResourceType::RT_DS:\n\t\t\tdesc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES;\n\t\t\tbreak;\n\t\t}\n\n\t\tTRY_M(device->m_native->CreateHeap(&desc, IID_PPV_ARGS(&heap->m_native)),\n\t\t\t\"Failed to create heap.\");\n\n\t\treturn heap;\n\t}\n\n\tHeap<HeapOptimization::SMALL_STATIC_BUFFERS>* CreateHeap_SSBO(Device * device, std::uint64_t size_in_bytes, unsigned int versioning_count)\n\t{\n\t\tauto heap = new Heap<HeapOptimization::SMALL_STATIC_BUFFERS>();\n\t\theap->m_mapped = false;\n\t\theap->m_versioning_count = versioning_count;\n\t\theap->m_current_offset = 0;\n\t\theap->m_alignment = 256;\n\n\t\tauto aligned_size = SizeAlignTwoPower(size_in_bytes, 65536);\n\n\t\theap->m_heap_size = aligned_size;\n\n\t\tauto page_frame_count = SizeAlignTwoPower(heap->m_heap_size / heap->m_alignment, 64) / 64;\n\n\t\theap->m_bitmap.resize(page_frame_count);\n\n\t\tfor (int i = 0; i < heap->m_bitmap.size(); ++i) \n\t\t{\n\t\t\theap->m_bitmap[i] = 0xffffffffffffffff;\n\t\t}\n\n\t\tauto heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\tauto resource_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size, D3D12_RESOURCE_FLAG_NONE);\n\n\t\tTRY_M(device->m_native->CreateCommittedResource(\n\t\t\t&heap_properties,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&resource_desc,\n\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&heap->m_native)),\n\t\t\t\"Failed to create small buffer optimized heap.\");\n\n\t\theap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\tresource_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size, D3D12_RESOURCE_FLAG_NONE);\n\n\t\tTRY_M(device->m_native->CreateCommittedResource(\n\t\t\t&heap_properties,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&resource_desc,\n\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&heap->m_staging_buffer)),\n\t\t\t\"Failed to create small buffer optimized heap.\");\n\n\t\tauto range = CD3DX12_RANGE(0, 0);\n\t\theap->m_staging_buffer->Map(0, &range, reinterpret_cast<void**>(&heap->m_cpu_address));\n\n\t\treturn heap;\n\t}\n\n\tHeap<HeapOptimization::BIG_STATIC_BUFFERS>* CreateHeap_BSBO(Device * device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count)\n\t{\n\t\tauto heap = new Heap<HeapOptimization::BIG_STATIC_BUFFERS>();\n\t\theap->m_mapped = false;\n\t\theap->m_versioning_count = versioning_count;\n\t\theap->m_current_offset = 0;\n\t\theap->m_alignment = 65536;\n\n\t\tauto aligned_size = SizeAlignTwoPower(size_in_bytes, 65536);\n\n\t\theap->m_heap_size = aligned_size;\n\n\t\tauto page_frame_count = SizeAlignTwoPower(heap->m_heap_size / heap->m_alignment, 64) / 64;\n\n\t\theap->m_bitmap.resize(page_frame_count);\n\n\t\tfor (int i = 0; i < heap->m_bitmap.size(); ++i) \n\t\t{\n\t\t\theap->m_bitmap[i] = 0xffffffffffffffff;\n\t\t}\n\n\t\tD3D12_HEAP_PROPERTIES heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\n\t\tCD3DX12_HEAP_DESC desc = {};\n\t\tdesc.SizeInBytes = heap->m_heap_size;\n\t\tdesc.Properties = heap_properties;\n\t\tdesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;\n\t\tswitch (resource_type)\n\t\t{\n\t\tcase ResourceType::BUFFER:\n\t\t\tdesc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;\n\t\t\tbreak;\n\t\tcase ResourceType::TEXTURE:\n\t\t\tdesc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES;\n\t\t\tbreak;\n\t\tcase ResourceType::RT_DS:\n\t\t\tdesc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES;\n\t\t\tbreak;\n\t\t}\n\n\t\tTRY_M(device->m_native->CreateHeap(&desc, IID_PPV_ARGS(&heap->m_native)),\n\t\t\t\"Failed to create heap.\");\n\n\t\theap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\tauto resource_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size, D3D12_RESOURCE_FLAG_NONE);\n\n\t\tTRY_M(device->m_native->CreateCommittedResource(\n\t\t\t&heap_properties,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&resource_desc,\n\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&heap->m_staging_buffer)),\n\t\t\t\"Failed to create heap staging buffer.\");\n\n\t\tauto range = CD3DX12_RANGE(0, 0);\n\t\theap->m_staging_buffer->Map(0, &range, reinterpret_cast<void**>(&heap->m_cpu_address));\n\n\t\treturn heap;\n\t}\n\n\tHeapResource* AllocConstantBuffer(Heap<HeapOptimization::SMALL_BUFFERS>* heap, std::uint64_t size_in_bytes)\n\t{\n\t\tauto cb = new HeapResource();\n\n\t\tauto aligned_size = SizeAlignTwoPower(size_in_bytes, 256);\n\t\tcb->m_unaligned_size = size_in_bytes;\n\t\tcb->m_gpu_addresses.resize(heap->m_versioning_count);\n\n\t\tauto frame_count = heap->m_heap_size / heap->m_alignment;\n\t\tauto needed_frames = aligned_size / heap->m_alignment*heap->m_versioning_count;\n\n\t\tauto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames);\n\n\t\tif (!start_frame.has_value())\n\t\t{\n\t\t\tdelete cb;\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tfor (std::uint64_t i = 0; i < needed_frames; ++i) \n\t\t{\n\t\t\tutil::ClearPage(heap->m_bitmap, start_frame.value() + i);\n\t\t}\n\n\t\theap->m_current_offset = start_frame.value() * heap->m_alignment;\n\n\t\tcb->m_begin_offset = heap->m_current_offset;\n\n\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t{\n\t\t\tcb->m_gpu_addresses[i] = heap->m_native->GetGPUVirtualAddress() + heap->m_current_offset;\n\t\t\theap->m_current_offset += aligned_size;\n\t\t}\n\n\t\t// If the heap is supposed to be mapped immediatly map the new resource.\n\t\tif (heap->m_mapped)\n\t\t{\n\t\t\tcb->m_cpu_addresses = std::vector<std::uint8_t*>(heap->m_versioning_count);\n\t\t\tauto&& addresses = cb->m_cpu_addresses.value();\n\t\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t\t{\n\t\t\t\taddresses[i] = heap->m_cpu_address + (cb->m_begin_offset + (SizeAlignTwoPower(cb->m_unaligned_size, 255) * i));\n\t\t\t}\n\t\t}\n\t\t\n\t\tcb->m_heap_vector_location = heap->m_resources.size();\n\t\theap->m_resources.push_back(cb);\n\n\t\tcb->m_heap_sbo = heap;\n\t\tcb->m_resource_heap_optimization = HeapOptimization::SMALL_BUFFERS;\n\n\t\treturn cb;\n\t}\n\n\tHeapResource* AllocConstantBuffer(Heap<HeapOptimization::BIG_BUFFERS>* heap, std::uint64_t size_in_bytes)\n\t{\n\t\tauto cb = new HeapResource();\n\t\tdecltype(Device::m_native) n_device;\n\t\theap->m_native->GetDevice(IID_PPV_ARGS(&n_device));\n\t\tcb->m_unaligned_size = size_in_bytes;\n\n\t\tauto aligned_size_in_bytes = SizeAlignTwoPower(size_in_bytes, 65536);\n\n\t\tauto frame_count = heap->m_heap_size / heap->m_alignment;\n\t\tauto needed_frames = aligned_size_in_bytes / heap->m_alignment*heap->m_versioning_count;\n\n\t\tauto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames);\n\n\t\tif (!start_frame.has_value())\n\t\t{\n\t\t\tdelete cb;\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tfor (std::uint64_t i = 0; i < needed_frames; ++i) \n\t\t{\n\t\t\tutil::ClearPage(heap->m_bitmap, start_frame.value() + i);\n\t\t}\n\n\t\theap->m_current_offset = start_frame.value() * heap->m_alignment;\n\n\t\tCD3DX12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size_in_bytes, D3D12_RESOURCE_FLAG_NONE);\n\t\tcb->m_gpu_addresses.resize(heap->m_versioning_count);\n\t\tcb->m_heap_vector_location = heap->m_resources.size();\n\n\t\tcb->m_begin_offset = heap->m_current_offset;\n\n\t\tstd::vector<ID3D12Resource*> temp_resources(heap->m_versioning_count);\n\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t{\n\t\t\tTRY_M(n_device->CreatePlacedResource(heap->m_native, heap->m_current_offset, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&temp_resources[i])),\n\t\t\t\t\"Failed to create constant buffer placed resource.\");\n\n\t\t\theap->m_current_offset += aligned_size_in_bytes;\n\n\t\t\tcb->m_gpu_addresses[i] = temp_resources[i]->GetGPUVirtualAddress();\n\t\t}\n\n\t\t// If the heap is supposed to be mapped immediatly map the new resource.\n\t\tif (heap->m_mapped)\n\t\t{\n\t\t\tcb->m_cpu_addresses = std::vector<std::uint8_t*>(heap->m_versioning_count);\n\t\t\tauto&& addresses = cb->m_cpu_addresses.value();\n\n\t\t\tCD3DX12_RANGE read_range(0, 0);\n\t\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t\t{\n\t\t\t\tTRY_M(temp_resources[i]->Map(0, &read_range, reinterpret_cast<void**>(&addresses[i])),\n\t\t\t\t\t\"Failed to map resource.\");\n\t\t\t}\n\t\t}\n\n\t\theap->m_resources.push_back(std::make_pair(cb, temp_resources));\n\n\t\tcb->m_heap_bbo = heap;\n\t\tcb->m_resource_heap_optimization = HeapOptimization::BIG_BUFFERS;\n\n\t\treturn cb;\n\t}\n\n\tHeapResource* AllocByteAddressBuffer(Heap<HeapOptimization::BIG_BUFFERS>* heap, std::uint64_t size_in_bytes)\n\t{\n\t\tauto cb = new HeapResource();\n\t\tdecltype(Device::m_native) n_device;\n\t\theap->m_native->GetDevice(IID_PPV_ARGS(&n_device));\n\t\tcb->m_unaligned_size = size_in_bytes;\n\n\t\tauto aligned_size_in_bytes = SizeAlignTwoPower(size_in_bytes, 65536);\n\n\t\tauto frame_count = heap->m_heap_size / heap->m_alignment;\n\t\tauto needed_frames = aligned_size_in_bytes / heap->m_alignment*heap->m_versioning_count;\n\n\t\tauto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames);\n\n\t\tif (!start_frame.has_value())\n\t\t{\n\t\t\tdelete cb;\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tfor (std::uint64_t i = 0; i < needed_frames; ++i)\n\t\t{\n\t\t\tutil::ClearPage(heap->m_bitmap, start_frame.value() + i);\n\t\t}\n\n\t\theap->m_current_offset = start_frame.value() * heap->m_alignment;\n\n\t\tCD3DX12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size_in_bytes, D3D12_RESOURCE_FLAG_NONE);\n\t\tcb->m_gpu_addresses.resize(heap->m_versioning_count);\n\t\tcb->m_heap_vector_location = heap->m_resources.size();\n\n\t\tcb->m_begin_offset = heap->m_current_offset;\n\n\t\tstd::vector<ID3D12Resource*> temp_resources(heap->m_versioning_count);\n\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t{\n\t\t\tTRY_M(n_device->CreatePlacedResource(heap->m_native, heap->m_current_offset, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&temp_resources[i])),\n\t\t\t\t\"Failed to create constant buffer placed resource.\");\n\n\t\t\theap->m_current_offset += aligned_size_in_bytes;\n\n\t\t\tcb->m_gpu_addresses[i] = temp_resources[i]->GetGPUVirtualAddress();\n\t\t}\n\n\t\t// If the heap is supposed to be mapped immediatly map the new resource.\n\t\tif (heap->m_mapped)\n\t\t{\n\t\t\tcb->m_cpu_addresses = std::vector<std::uint8_t*>(heap->m_versioning_count);\n\t\t\tauto&& addresses = cb->m_cpu_addresses.value();\n\n\t\t\tCD3DX12_RANGE read_range(0, 0);\n\t\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t\t{\n\t\t\t\tTRY_M(temp_resources[i]->Map(0, &read_range, reinterpret_cast<void**>(&addresses[i])),\n\t\t\t\t\t\"Failed to map resource.\");\n\t\t\t}\n\t\t}\n\n\t\theap->m_resources.push_back(std::make_pair(cb, temp_resources));\n\n\t\tcb->m_heap_bbo = heap;\n\t\tcb->m_resource_heap_optimization = HeapOptimization::BIG_BUFFERS;\n\n\t\treturn cb;\n\t}\n\n\tHeapResource* AllocStructuredBuffer(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, std::uint64_t size_in_bytes, std::uint64_t stride, bool used_as_uav)\n\t{\n\t\tauto cb = new HeapResource();\n\t\tdecltype(Device::m_native) n_device;\n\t\theap->m_native->GetDevice(IID_PPV_ARGS(&n_device));\n\t\tcb->m_unaligned_size = size_in_bytes;\n\t\tcb->m_stride = stride;\n\t\tcb->m_used_as_uav = used_as_uav;\n\n\t\tauto aligned_size_in_bytes = SizeAlignTwoPower(size_in_bytes, 65536);\n\n\t\tauto frame_count = heap->m_heap_size / heap->m_alignment;\n\t\tauto needed_frames = aligned_size_in_bytes / heap->m_alignment*heap->m_versioning_count;\n\n\t\tauto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames);\n\n\t\tif (!start_frame.has_value())\n\t\t{\n\t\t\tdelete cb;\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tfor (std::uint64_t i = 0; i < needed_frames; ++i) \n\t\t{\n\t\t\tutil::ClearPage(heap->m_bitmap, start_frame.value() + i);\n\t\t}\n\n\t\theap->m_current_offset = start_frame.value() * heap->m_alignment;\n\t\t\t\t\n\t\tCD3DX12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(\n\t\t\taligned_size_in_bytes, \n\t\t\tused_as_uav ? D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS : D3D12_RESOURCE_FLAG_NONE);\n\n\t\tcb->m_gpu_addresses.resize(heap->m_versioning_count);\n\t\tcb->m_heap_vector_location = heap->m_resources.size();\n\n\t\tcb->m_begin_offset = heap->m_current_offset;\n\n\t\tstd::vector<ID3D12Resource*> temp_resources(heap->m_versioning_count);\n\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t{\n\t\t\tTRY_M(n_device->CreatePlacedResource(\n\t\t\t\theap->m_native, \n\t\t\t\theap->m_current_offset, \n\t\t\t\t&desc, \n\t\t\t\tused_as_uav ? D3D12_RESOURCE_STATE_UNORDERED_ACCESS : D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, \n\t\t\t\tnullptr, \n\t\t\t\tIID_PPV_ARGS(&temp_resources[i])),\n\t\t\t\t\"Failed to create constant buffer placed resource.\");\t\t\t\n\n\t\t\theap->m_current_offset += aligned_size_in_bytes;\n\n\t\t\tcb->m_gpu_addresses[i] = temp_resources[i]->GetGPUVirtualAddress();\n\t\t\tcb->m_states.push_back(used_as_uav ? ResourceState::UNORDERED_ACCESS : ResourceState::PIXEL_SHADER_RESOURCE);\n\t\t}\n\t\t\n\n\t\theap->m_resources.push_back(std::make_pair(cb, temp_resources));\n\n\t\tcb->m_heap_bsbo = heap;\n\t\tcb->m_resource_heap_optimization = HeapOptimization::BIG_STATIC_BUFFERS;\n\n\t\treturn cb;\n\t}\n\n\tHeapResource * AllocGenericBuffer(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, std::uint64_t size_in_bytes)\n\t{\n\t\tauto cb = new HeapResource();\n\t\tdecltype(Device::m_native) n_device;\n\t\theap->m_native->GetDevice(IID_PPV_ARGS(&n_device));\n\t\tcb->m_unaligned_size = size_in_bytes;\n\n\t\tauto aligned_size_in_bytes = SizeAlignTwoPower(size_in_bytes, 65536);\n\n\t\tauto frame_count = heap->m_heap_size / heap->m_alignment;\n\t\tauto needed_frames = aligned_size_in_bytes / heap->m_alignment*heap->m_versioning_count;\n\n\t\tauto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames);\n\n\t\tif (!start_frame.has_value())\n\t\t{\n\t\t\tdelete cb;\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tfor (std::uint64_t i = 0; i < needed_frames; ++i) \n\t\t{\n\t\t\tutil::ClearPage(heap->m_bitmap, start_frame.value() + i);\n\t\t}\n\n\t\theap->m_current_offset = start_frame.value() * heap->m_alignment;\n\n\t\tCD3DX12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size_in_bytes, D3D12_RESOURCE_FLAG_NONE);\n\t\tcb->m_gpu_addresses.resize(heap->m_versioning_count);\n\t\tcb->m_heap_vector_location = heap->m_resources.size();\n\n\t\tcb->m_begin_offset = heap->m_current_offset;\n\n\t\tstd::vector<ID3D12Resource*> temp_resources(heap->m_versioning_count);\n\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t{\n\t\t\tTRY_M(n_device->CreatePlacedResource(heap->m_native, heap->m_current_offset, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&temp_resources[i])),\n\t\t\t\t\"Failed to create constant buffer placed resource.\");\n\n\t\t\theap->m_current_offset += aligned_size_in_bytes;\n\n\t\t\tcb->m_gpu_addresses[i] = temp_resources[i]->GetGPUVirtualAddress();\n\t\t}\n\n\t\theap->m_resources.push_back(std::make_pair(cb, temp_resources));\n\n\t\tcb->m_heap_bsbo = heap;\n\t\tcb->m_resource_heap_optimization = HeapOptimization::BIG_STATIC_BUFFERS;\n\n\t\treturn cb;\n\t}\n\n\tvoid SetName(Heap<HeapOptimization::SMALL_BUFFERS>* heap, std::wstring name)\n\t{\n\t\theap->m_native->SetName(name.c_str());\n\t}\n\n\tvoid SetName(Heap<HeapOptimization::BIG_BUFFERS>* heap, std::wstring name)\n\t{\n\t\theap->m_native->SetName(name.c_str());\n\t}\n\n\tvoid SetName(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap, std::wstring name)\n\t{\n\t\theap->m_native->SetName(name.c_str());\n\t}\n\n\tvoid SetName(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, std::wstring name)\n\t{\n\t\theap->m_native->SetName(name.c_str());\n\t}\n\n\n\tvoid DeallocConstantBuffer(Heap<HeapOptimization::SMALL_BUFFERS>* heap, HeapResource * heapResource)\n\t{\n\t\t\n\n\t\tstd::vector<HeapResource*>::iterator it;\n\n\t\tfor (it = heap->m_resources.begin(); it != heap->m_resources.end(); ++it)\n\t\t{\n\t\t\tif ((*it) == heapResource)\n\t\t\t\tbreak;\n\t\t}\n\n\t\tif (it == heap->m_resources.end()) \n\t\t{\n\t\t\treturn;\n\t\t}\n\t\t\n\t\theap->m_resources.erase(it);\n\t\t\n\n\t\tfor (int i = 0; i < heap->m_resources.size(); ++i)\n\t\t{\n\t\t\theap->m_resources[i]->m_heap_vector_location = i;\n\t\t}\n\n\t\tstd::uint64_t frame = heapResource->m_begin_offset / heap->m_alignment;\n\t\tstd::uint64_t frame_count = SizeAlignTwoPower(heapResource->m_unaligned_size, 256) / heap->m_alignment * heap->m_versioning_count;\n\n\t\tfor (int i = 0; i < frame_count; ++i) \n\t\t{\n\t\t\tutil::SetPage(heap->m_bitmap, frame + i);\n\t\t}\n\n\t\tdelete heapResource;\n\t}\n\n\tvoid DeallocConstantBuffer(Heap<HeapOptimization::BIG_BUFFERS>* heap, HeapResource * heapResource)\n\t{\n\t\tstd::vector<std::pair<HeapResource*, std::vector<ID3D12Resource*>>>::iterator it;\n\n\t\tfor (it = heap->m_resources.begin(); it != heap->m_resources.end(); ++it) \n\t\t{\n\t\t\tif ((*it).first == heapResource)\n\t\t\t\tbreak;\n\t\t}\n\n\t\tif (it == heap->m_resources.end()) \n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\t\n\t\tfor (int i = 0; i < (*it).second.size(); ++i) \n\t\t{\n\t\t\tdelete (*it).second[i];\n\t\t}\n\t\theap->m_resources.erase(it);\n\t\t\n\n\t\tfor (int i = 0; i < heap->m_resources.size(); ++i) \n\t\t{\n\t\t\theap->m_resources[i].first->m_heap_vector_location = i;\n\t\t}\n\n\t\tstd::uint64_t frame = heapResource->m_begin_offset / heap->m_alignment;\n\t\tstd::uint64_t frame_count = SizeAlignTwoPower(heapResource->m_unaligned_size, heap->m_alignment) / heap->m_alignment * heap->m_versioning_count;\n\n\t\tfor (int i = 0; i < frame_count; ++i) \n\t\t{\n\t\t\tutil::SetPage(heap->m_bitmap, frame + i);\n\t\t}\n\n\t\tdelete heapResource;\n\n\t}\n\n\tvoid DeallocBuffer(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, HeapResource * heapResource)\n\t{\n\t\tstd::vector<std::pair<HeapResource*, std::vector<ID3D12Resource*>>>::iterator it;\n\n\t\tfor (it = heap->m_resources.begin(); it != heap->m_resources.end(); ++it) \n\t\t{\n\t\t\tif ((*it).first == heapResource)\n\t\t\t\tbreak;\n\t\t}\n\n\t\tif (it == heap->m_resources.end())\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tfor (int i = 0; i < (*it).second.size(); ++i) \n\t\t{\n\t\t\tdelete (*it).second[i];\n\t\t}\n\t\theap->m_resources.erase(it);\n\n\n\t\tfor (int i = 0; i < heap->m_resources.size(); ++i) \n\t\t{\n\t\t\theap->m_resources[i].first->m_heap_vector_location = i;\n\t\t}\n\n\t\tstd::uint64_t frame = heapResource->m_begin_offset / heap->m_alignment;\n\t\tstd::uint64_t frame_count = SizeAlignTwoPower(heapResource->m_unaligned_size, heap->m_alignment) / heap->m_alignment * heap->m_versioning_count;\n\n\t\tfor (int i = 0; i < frame_count; ++i) \n\t\t{\n\t\t\tutil::SetPage(heap->m_bitmap, frame + i);\n\t\t}\n\n\t\tdelete heapResource;\n\t}\n\n\tvoid MapHeap(Heap<HeapOptimization::SMALL_BUFFERS>* heap)\n\t{\n\t\theap->m_mapped = true;\n\n\t\tCD3DX12_RANGE read_range(0, 0);\n\t\tstd::uint8_t* address;\n\t\tTRY_M(heap->m_native->Map(0, &read_range, reinterpret_cast<void**>(&address)),\n\t\t\t\"Failed to map resource.\");\n\n\t\theap->m_cpu_address = address;\n\n\t\tfor (auto& handle : heap->m_resources)\n\t\t{\n\t\t\thandle->m_cpu_addresses = std::vector<std::uint8_t*>(heap->m_versioning_count);\n\t\t\tauto&& addresses = handle->m_cpu_addresses.value();\n\n\t\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t\t{\n\t\t\t\taddresses[i] = address + (handle->m_begin_offset + (SizeAlignTwoPower(handle->m_unaligned_size, 255) * i));\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid MapHeap(Heap<HeapOptimization::BIG_BUFFERS>* heap)\n\t{\n\t\theap->m_mapped = true;\n\n\t\tfor (auto resource : heap->m_resources)\n\t\t{\n\t\t\tresource.first->m_cpu_addresses = std::vector<std::uint8_t*>(heap->m_versioning_count);\n\t\t\tauto&& addresses = resource.first->m_cpu_addresses.value();\n\n\t\t\tCD3DX12_RANGE read_range(0, 0);\n\t\t\tfor (auto i = 0u; i < heap->m_versioning_count; i++)\n\t\t\t{\n\t\t\t\tTRY_M(resource.second[i]->Map(0, &read_range, reinterpret_cast<void**>(&addresses[i])),\n\t\t\t\t\t\"Failed to map resource.\");\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid UnmapHeap(Heap<HeapOptimization::SMALL_BUFFERS>* heap)\n\t{\n\t\theap->m_mapped = false;\n\n\t\tCD3DX12_RANGE read_range(0, 0);\n\t\theap->m_native->Unmap(0, &read_range);\n\n\t\t// For safety set the optional cpu addresses back to nullopt.\n\t\tfor (auto& resource : heap->m_resources)\n\t\t{\n\t\t\tresource->m_cpu_addresses = std::nullopt;\n\t\t}\n\t}\n\n\tvoid UnmapHeap(Heap<HeapOptimization::BIG_BUFFERS>* heap)\n\t{\n\t\theap->m_mapped = false;\n\n\t\tCD3DX12_RANGE read_range(0, 0);\n\n\t\tfor (auto& resource : heap->m_resources)\n\t\t{\n\t\t\tresource.first->m_cpu_addresses = std::nullopt;\n\t\t\tfor (auto& n_resource : resource.second)\n\t\t\t{\n\t\t\t\tn_resource->Unmap(0, &read_range);\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid MakeResident(Heap<HeapOptimization::SMALL_BUFFERS>* heap)\n\t{\n\t\tinternal::MakeResidentSingle(heap);\n\t}\n\n\tvoid MakeResident(Heap<HeapOptimization::BIG_BUFFERS>* heap)\n\t{\n\t\tinternal::MakeResidentSingle(heap);\n\t}\n\n\tvoid MakeResident(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap)\n\t{\n\t\tinternal::MakeResidentSingle(heap);\n\t}\n\n\tvoid MakeResident(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap)\n\t{\n\t\tinternal::MakeResidentSingle(heap);\n\t}\n\n\tvoid EnqueueMakeResident(Heap<HeapOptimization::SMALL_BUFFERS>* heap, Fence* fence)\n\t{\n\t\tinternal::EnqueueMakeResidentSingle(heap, fence);\n\t}\n\n\tvoid EnqueueMakeResident(Heap<HeapOptimization::BIG_BUFFERS>* heap, Fence* fence)\n\t{\n\t\tinternal::EnqueueMakeResidentSingle(heap, fence);\n\t}\n\n\tvoid EnqueueMakeResident(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap, Fence* fence)\n\t{\n\t\tinternal::EnqueueMakeResidentSingle(heap, fence);\n\t}\n\n\tvoid EnqueueMakeResident(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap, Fence* fence)\n\t{\n\t\tinternal::EnqueueMakeResidentSingle(heap, fence);\n\t}\n\n\tvoid Evict(Heap<HeapOptimization::SMALL_BUFFERS>* heap)\n\t{\n\t\tinternal::EvictSingle(heap);\n\t}\n\n\tvoid Evict(Heap<HeapOptimization::BIG_BUFFERS>* heap)\n\t{\n\t\tinternal::EvictSingle(heap);\n\t}\n\n\tvoid Evict(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap)\n\t{\n\t\tinternal::EvictSingle(heap);\n\t}\n\n\tvoid Evict(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap)\n\t{\n\t\tinternal::EvictSingle(heap);\n\t}\n\n\tvoid Destroy(Heap<HeapOptimization::SMALL_BUFFERS>* heap)\n\t{\n\t\tUnmapHeap(heap);\n\n\t\tSAFE_RELEASE(heap->m_native);\n\t\tfor (auto& resource : heap->m_resources)\n\t\t{\n\t\t\tdelete resource;\n\t\t}\n\t\tdelete heap;\n\t}\n\n\tvoid Destroy(Heap<HeapOptimization::BIG_BUFFERS>* heap)\n\t{\n\t\tUnmapHeap(heap);\n\n\t\tSAFE_RELEASE(heap->m_native);\n\t\tfor (auto& resource : heap->m_resources)\n\t\t{\n\t\t\tdelete resource.first;\n\t\t\tfor (auto& n_resource : resource.second)\n\t\t\t{\n\t\t\t\tdelete n_resource;\n\t\t\t}\n\t\t}\n\t\tdelete heap;\n\t}\n\n\tvoid Destroy(Heap<HeapOptimization::SMALL_STATIC_BUFFERS>* heap)\n\t{\n\t\t//UnmapHeap(heap);\n\n\t\tSAFE_RELEASE(heap->m_native);\n\t\tSAFE_RELEASE(heap->m_staging_buffer);\n\t\tfor (auto& resource : heap->m_resources)\n\t\t{\n\t\t\tdelete resource;\n\t\t}\n\t\tdelete heap;\n\t}\n\n\tvoid Destroy(Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap)\n\t{\n\t\t//UnmapHeap(heap);\n\n\t\tSAFE_RELEASE(heap->m_native);\n\t\tSAFE_RELEASE(heap->m_staging_buffer);\n\t\tfor (auto& resource : heap->m_resources)\n\t\t{\n\t\t\tdelete resource.first;\n\t\t\tfor (auto& n_resource : resource.second)\n\t\t\t{\n\t\t\t\tSAFE_RELEASE(n_resource);\n\t\t\t}\n\t\t}\n\t\tdelete heap;\n\t}\n\n\tvoid UpdateConstantBuffer(HeapResource* buffer, unsigned int frame_idx, void* data, std::uint64_t size_in_bytes)\n\t{\n\t\tif (buffer->m_cpu_addresses.has_value())\n\t\t{\n\t\t\tauto&& addresses = buffer->m_cpu_addresses.value();\n\t\t\tstd::memcpy(addresses[frame_idx], data, size_in_bytes);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tLOGW(\"Tried updating a unmapped constant buffer resource!\");\n\t\t}\n\t}\n\n\tvoid UpdateStructuredBuffer(HeapResource * buffer, unsigned int frame_idx, void * data, std::uint64_t size_in_bytes, std::uint64_t offset, std::uint64_t stride, CommandList * cmd_list)\n\t{\n\t\tif (buffer->m_resource_heap_optimization != HeapOptimization::BIG_STATIC_BUFFERS)\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tstd::size_t aligned_size = SizeAlignTwoPower(buffer->m_unaligned_size, 65536);\n\n\t\tmemcpy(buffer->m_heap_bsbo->m_cpu_address + buffer->m_begin_offset + offset + aligned_size * frame_idx, data, size_in_bytes);\n\n\t\tbuffer->m_stride = stride;\n\n\t\tif (size_in_bytes != 0) {\n\t\t\tID3D12Resource* resource = buffer->m_heap_bsbo->m_resources[buffer->m_heap_vector_location].second[frame_idx];\n\n\t\t\tCD3DX12_RESOURCE_BARRIER resource_barrier = CD3DX12_RESOURCE_BARRIER::Transition(resource,\n\t\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(buffer->m_states[frame_idx]),\n\t\t\t\tD3D12_RESOURCE_STATE_COPY_DEST);\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &resource_barrier);\n\t\t\t\n\t\t\tcmd_list->m_native->CopyBufferRegion(resource, \n\t\t\t\toffset, \n\t\t\t\tbuffer->m_heap_bsbo->m_staging_buffer, \n\t\t\t\tbuffer->m_begin_offset + offset + aligned_size * frame_idx, \n\t\t\t\tsize_in_bytes);\n\n\t\t\tCD3DX12_RESOURCE_BARRIER copy_barrier = CD3DX12_RESOURCE_BARRIER::Transition(resource,\n\t\t\t\tD3D12_RESOURCE_STATE_COPY_DEST,\n\t\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(buffer->m_states[frame_idx]));\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1,\n\t\t\t\t&copy_barrier);\n\t\t}\n\t}\n\n\tvoid UpdateByteAddressBuffer(HeapResource* buffer, unsigned int frame_idx, void* data, std::uint64_t size_in_bytes)\n\t{\n\t\tUpdateConstantBuffer(buffer, frame_idx, data, size_in_bytes);\n\t}\n\n\tvoid CreateSRVFromByteAddressBuffer(HeapResource* resource, DescHeapCPUHandle& handle, unsigned int id, unsigned int count)\n\t{\n\t\tauto& n_resources = resource->m_heap_bbo->m_resources[resource->m_heap_vector_location];\n\t\tauto& n_resource = n_resources.second[id];\n\n\t\tdecltype(Device::m_native) n_device;\n\t\tn_resource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tauto increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);\n\n\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\n\t\tsrv_desc.Format = DXGI_FORMAT_R32_TYPELESS;\n\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\n\t\tsrv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;\n\t\tsrv_desc.Buffer.NumElements = count;\n\t\tsrv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW;\n\n\t\tn_device->CreateShaderResourceView(n_resource, &srv_desc, handle.m_native);\n\t\tOffset(handle, 1, increment_size);\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_indirect_command_buffer.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"../util/log.hpp\"\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tIndirectCommandBuffer* CreateIndirectCommandBuffer(Device* device, std::size_t max_commands, std::size_t command_size, uint32_t versions)\n\t{\n\t\tauto buffer = new IndirectCommandBuffer();\n\n\t\tbuffer->m_num_max_commands = max_commands;\n\t\tbuffer->m_num_commands = 0;\n\t\tbuffer->m_command_size = command_size;\n\n\t\tbuffer->m_native.resize(versions);\n\t\tbuffer->m_native_upload.resize(versions);\n\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(max_commands * command_size);\n\n\t\tfor (uint32_t i = 0; i < versions; ++i)\n\t\t{\n\n\t\t\tTRY(device->m_native->CreateCommittedResource(\n\t\t\t\t&heap_properties_default,\n\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t&buffer_desc,\n\t\t\t\tD3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,\n\t\t\t\tnullptr,\n\t\t\t\tIID_PPV_ARGS(buffer->m_native.data() + i)));\n\n\t\t\tTRY(device->m_native->CreateCommittedResource(\n\t\t\t\t&heap_properties_upload,\n\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t&buffer_desc,\n\t\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\t\tnullptr,\n\t\t\t\tIID_PPV_ARGS(buffer->m_native_upload.data() + i)));\n\n\t\t}\n\n\t\treturn buffer;\n\t}\n\n\tvoid SetName(IndirectCommandBuffer* buffer, std::wstring name)\n\t{\n\t\tfor (uint32_t i = 0, j = (uint32_t)buffer->m_native.size(); i < j; ++i)\n\t\t{\n\t\t\tbuffer->m_native[i]->SetName((name + L\" #\" + std::to_wstring(i)).c_str());\n\t\t\tbuffer->m_native_upload[i]->SetName((name + L\" #\" + std::to_wstring(i) + L\" Upload Buffer\").c_str());\n\t\t}\n\t}\n\n\tvoid StageBuffer(CommandList* cmd_list, IndirectCommandBuffer* buffer, void* data, std::size_t num_commands, uint32_t frame_idx)\n\t{\n\t\tD3D12_SUBRESOURCE_DATA cmd_data = {};\n\t\tcmd_data.pData = reinterpret_cast<UINT8*>(data);\n\t\tcmd_data.RowPitch = num_commands * buffer->m_command_size;\n\t\tcmd_data.SlicePitch = cmd_data.RowPitch;\n\n\t\tbuffer->m_num_commands = num_commands;\n\n\t\tUpdateSubresources<1>(cmd_list->m_native, buffer->m_native[frame_idx], buffer->m_native_upload[frame_idx], 0, 0, 1, &cmd_data);\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_material_pool.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_material_pool.hpp\"\n\nnamespace wr\n{\n\tD3D12MaterialPool::D3D12MaterialPool(D3D12RenderSystem & render_system) :\n\t\tm_render_system(render_system)\n\t{\n\t\tm_constant_buffer_pool = m_render_system.CreateConstantBufferPool(1_mb);\t\t\n\t}\n\n\tD3D12MaterialPool::~D3D12MaterialPool()\n\t{\n\n\t}\n\n\tvoid D3D12MaterialPool::Evict()\n\t{\n\t\tm_constant_buffer_pool->Evict();\n\t}\n\n\tvoid D3D12MaterialPool::MakeResident()\n\t{\n\t\tm_constant_buffer_pool->MakeResident();\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/d3d12/d3d12_material_pool.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../material_pool.hpp\"\n\n#include \"d3d12_renderer.hpp\"\n\nnamespace wr\n{\n\n\tclass D3D12MaterialPool : public MaterialPool\n\t{\n\tpublic:\n\t\texplicit D3D12MaterialPool(D3D12RenderSystem& render_system);\n\t\t~D3D12MaterialPool() final;\n\n\t\tvoid Evict() final;\n\t\tvoid MakeResident() final;\n\n\tprotected:\n\t\tD3D12RenderSystem& m_render_system;\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/d3d12/d3d12_model_pool.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_model_pool.hpp\"\n\n#include \"../util/bitmap_allocator.hpp\"\n\n#include \"d3d12_functions.hpp\"\n#include \"d3d12_defines.hpp\"\n#include \"d3d12_renderer.hpp\"\n\nnamespace wr\n{\n\n\tD3D12ModelPool::D3D12ModelPool(D3D12RenderSystem& render_system,\n\t\tstd::size_t vertex_buffer_size_in_bytes,\n\t\tstd::size_t index_buffer_size_in_bytes) :\n\t\tModelPool(SizeAlignAnyAlignment(vertex_buffer_size_in_bytes, 65536), SizeAlignAnyAlignment(index_buffer_size_in_bytes, 65536)),\n\t\tm_render_system(render_system),\n\t\tm_intermediate_size(0)\n\t{\n\t\tm_vertex_buffer = d3d12::CreateStagingBuffer(render_system.m_device,\n\t\t\tnullptr,\n\t\t\tSizeAlignAnyAlignment(vertex_buffer_size_in_bytes, 65536),\n\t\t\tsizeof(Vertex),\n\t\t\tResourceState::VERTEX_AND_CONSTANT_BUFFER);\n\t\tSetName(m_vertex_buffer, L\"Model Pool Vertex Buffer\");\n\t\tm_vertex_buffer_size = SizeAlignAnyAlignment(vertex_buffer_size_in_bytes, 65536);\n\n\t\tm_index_buffer = d3d12::CreateStagingBuffer(render_system.m_device,\n\t\t\tnullptr,\n\t\t\tSizeAlignAnyAlignment(index_buffer_size_in_bytes, 65536),\n\t\t\tsizeof(std::uint32_t),\n\t\t\tResourceState::INDEX_BUFFER);\n\t\tSetName(m_index_buffer, L\"Model Pool Index Buffer\");\n\t\tm_index_buffer_size = SizeAlignAnyAlignment(index_buffer_size_in_bytes, 65536);\n\n\t\tm_vertex_heap_start_block = new MemoryBlock;\n\t\tm_vertex_heap_start_block->m_free = true;\n\t\tm_vertex_heap_start_block->m_next_block = nullptr;\n\t\tm_vertex_heap_start_block->m_prev_block = nullptr;\n\t\tm_vertex_heap_start_block->m_offset = 0;\n\t\tm_vertex_heap_start_block->m_size = m_vertex_buffer_size;\n\n\t\tm_index_heap_start_block = new MemoryBlock;\n\t\tm_index_heap_start_block->m_free = true;\n\t\tm_index_heap_start_block->m_next_block = nullptr;\n\t\tm_index_heap_start_block->m_prev_block = nullptr;\n\t\tm_index_heap_start_block->m_offset = 0;\n\t\tm_index_heap_start_block->m_size = m_index_buffer_size;\n\n\t\tm_intermediate_buffer = NULL;\n\t}\n\n\tD3D12ModelPool::~D3D12ModelPool()\n\t{\n\t\td3d12::Destroy(m_vertex_buffer);\n\t\td3d12::Destroy(m_index_buffer);\n\n\t\twhile (m_vertex_heap_start_block != nullptr) \n\t\t{\n\t\t\tMemoryBlock* temp = m_vertex_heap_start_block;\n\t\t\tm_vertex_heap_start_block = m_vertex_heap_start_block->m_next_block;\n\t\t\tdelete temp;\n\t\t}\n\n\t\twhile (m_index_heap_start_block != nullptr)\n\t\t{\n\t\t\tMemoryBlock* temp = m_index_heap_start_block;\n\t\t\tm_index_heap_start_block = m_index_heap_start_block->m_next_block;\n\t\t\tdelete temp;\n\t\t}\n\n\t\tfor (auto& handle : m_loaded_meshes)\n\t\t{\n\t\t\tdelete handle.second;\n\t\t}\n\t}\n\n\tvoid D3D12ModelPool::Evict()\n\t{\n\t\td3d12::Evict(m_vertex_buffer);\n\t\td3d12::Evict(m_index_buffer);\n\t}\n\n\tvoid D3D12ModelPool::MakeResident()\n\t{\n\t\td3d12::MakeResident(m_vertex_buffer);\n\t\td3d12::MakeResident(m_index_buffer);\n\t}\n\n\tvoid D3D12ModelPool::StageMeshes(d3d12::CommandList * cmd_list)\n\t{\n\t\twhile (!m_command_queue.empty())\n\t\t{\n\t\t\tinternal::Command* command = static_cast<internal::Command*>(m_command_queue.front());\n\n\t\t\tswitch (command->m_type)\n\t\t\t{\n\t\t\tcase internal::CommandType::STAGE:\n\t\t\t{\n\t\t\t\tinternal::StageCommand* stage_command = static_cast<internal::StageCommand*>(command);\n\t\t\t\td3d12::StageBufferRegion(stage_command->m_buffer,\n\t\t\t\t\tstage_command->m_size,\n\t\t\t\t\tstage_command->m_offset,\n\t\t\t\t\tcmd_list);\n\n\t\t\t\tdelete stage_command;\n\n\t\t\t\tm_command_queue.pop();\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase internal::CommandType::COPY:\n\t\t\t{\n\t\t\t\tinternal::CopyCommand* copy_command = static_cast<internal::CopyCommand*>(command);\n\n\t\t\t\tcmd_list->m_native->CopyBufferRegion(copy_command->m_dest,\n\t\t\t\t\tcopy_command->m_dest_offset,\n\t\t\t\t\tcopy_command->m_source,\n\t\t\t\t\tcopy_command->m_source_offset,\n\t\t\t\t\tcopy_command->m_size);\n\n\t\t\t\tdelete copy_command;\n\t\t\t\tm_command_queue.pop();\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase internal::CommandType::TRANSITION:\n\t\t\t{\n\t\t\t\tinternal::TransitionCommand* transition_command = static_cast<internal::TransitionCommand*>(command);\n\n\t\t\t\tCD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(transition_command->m_buffer,\n\t\t\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(transition_command->m_old_state),\n\t\t\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(transition_command->m_new_state));\n\n\t\t\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\n\t\t\t\tdelete transition_command;\n\t\t\t\tm_command_queue.pop();\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase internal::CommandType::READ:\n\t\t\t{\n\t\t\t\tinternal::ReadCommand* read_command = static_cast<internal::ReadCommand*>(command);\n\n\t\t\t\tcmd_list->m_native->CopyBufferRegion(read_command->m_buffer->m_staging,\n\t\t\t\t\tread_command->m_offset,\n\t\t\t\t\tread_command->m_buffer->m_buffer,\n\t\t\t\t\tread_command->m_offset,\n\t\t\t\t\tread_command->m_size);\n\n\t\t\t\tdelete read_command;\n\t\t\t\tm_command_queue.pop();\n\t\t\t}\n\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t{\n\t\t\t\tdelete command;\n\t\t\t\tm_command_queue.pop();\n\t\t\t}\n\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\td3d12::StagingBuffer * D3D12ModelPool::GetVertexStagingBuffer()\n\t{\n\t\treturn m_vertex_buffer;\n\t}\n\n\td3d12::StagingBuffer * D3D12ModelPool::GetIndexStagingBuffer()\n\t{\n\t\treturn m_index_buffer;\n\t}\n\n\tinternal::D3D12MeshInternal * D3D12ModelPool::GetMeshData(std::uint64_t mesh_handle)\n\t{\n\t\treturn static_cast<internal::D3D12MeshInternal*>(m_loaded_meshes[mesh_handle]);\n\t}\n\n\tvoid D3D12ModelPool::ShrinkToFit()\n\t{\n\t\tShrinkVertexHeapToFit();\n\t\tShrinkIndexHeapToFit();\n\t}\n\n\tvoid D3D12ModelPool::ShrinkVertexHeapToFit()\n\t{\n\t\tMemoryBlock* last_occupied_block = nullptr;\n\t\tfor (MemoryBlock* mem_block = m_vertex_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block)\n\t\t{\n\t\t\tif (mem_block->m_free == false)\n\t\t\t{\n\t\t\t\tlast_occupied_block = mem_block;\n\t\t\t}\n\t\t}\n\n\t\tif (last_occupied_block == nullptr)\n\t\t{\n\t\t\tLOGW(\"You're trying to shrink an empty vertex heap, returning instead.\");\n\t\t\treturn;\n\t\t}\n\t\tsize_t new_size = last_occupied_block->m_offset + last_occupied_block->m_size;\n\t\tnew_size = SizeAlignAnyAlignment(new_size, 65536);\n\n\t\tif (new_size == m_vertex_buffer->m_size)\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tID3D12Resource* old_buffer = m_vertex_buffer->m_buffer;\n\t\tID3D12Resource* new_buffer = nullptr;\n\t\tID3D12Resource* old_staging = m_vertex_buffer->m_staging;\n\t\tID3D12Resource* new_staging = nullptr;\n\n\t\tuint8_t* cpu_address;\n\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(new_size);\n\n\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t&heap_properties_upload,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&buffer_desc,\n\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&new_staging));\n\t\tNAME_D3D12RESOURCE(new_staging);\n\n\t\tCD3DX12_RANGE read_range(0, new_size);\n\n\t\tnew_staging->Map(0, &read_range, reinterpret_cast<void**>(&(cpu_address)));\n\n\t\tmemcpy(cpu_address, m_vertex_buffer->m_cpu_address, new_size);\n\n\t\tm_vertex_buffer->m_size = static_cast<std::uint32_t>(new_size);\n\t\tm_vertex_buffer->m_is_staged = true;\n\t\tm_vertex_buffer->m_cpu_address = cpu_address;\n\n\t\tm_render_system.WaitForAllPreviousWork();\n\n\t\tSAFE_RELEASE(m_vertex_buffer->m_buffer);\n\t\tSAFE_RELEASE(m_vertex_buffer->m_staging);\n\n\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t&heap_properties_default,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&buffer_desc,\n\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(m_vertex_buffer->m_target_resource_state),\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&new_buffer));\n\t\tNAME_D3D12RESOURCE(new_buffer);\n\n\t\tm_vertex_buffer->m_buffer = new_buffer;\n\t\tm_vertex_buffer->m_staging = new_staging;\n\t\tm_vertex_buffer->m_gpu_address = new_buffer->GetGPUVirtualAddress();\n\n\t\tfor (int i = 0; i < m_command_queue.size(); ++i)\n\t\t{\n\t\t\tinternal::Command* command = m_command_queue.front();\n\t\t\tm_command_queue.pop();\n\t\t\tm_command_queue.push(command);\n\n\t\t\tswitch (command->m_type)\n\t\t\t{\n\t\t\tcase internal::CommandType::COPY:\n\t\t\t{\n\t\t\t\tinternal::CopyCommand* copy_command = static_cast<internal::CopyCommand*>(command);\n\t\t\t\tif (copy_command->m_dest == old_staging)\n\t\t\t\t{\n\t\t\t\t\tcopy_command->m_dest = new_staging;\n\t\t\t\t}\n\t\t\t\tif (copy_command->m_dest == old_buffer)\n\t\t\t\t{\n\t\t\t\t\tcopy_command->m_dest = new_buffer;\n\t\t\t\t}\n\t\t\t\tif (copy_command->m_source == old_staging)\n\t\t\t\t{\n\t\t\t\t\tcopy_command->m_source = new_staging;\n\t\t\t\t}\n\t\t\t\tif (copy_command->m_source == old_buffer)\n\t\t\t\t{\n\t\t\t\t\tcopy_command->m_source = new_buffer;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase internal::CommandType::TRANSITION:\n\t\t\t{\n\t\t\t\tinternal::TransitionCommand* transition_command = static_cast<internal::TransitionCommand*>(command);\n\t\t\t\tif (transition_command->m_buffer == old_staging)\n\t\t\t\t{\n\t\t\t\t\ttransition_command->m_buffer = new_staging;\n\t\t\t\t}\n\t\t\t\tif (transition_command->m_buffer == old_buffer)\n\t\t\t\t{\n\t\t\t\t\ttransition_command->m_buffer = new_buffer;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tinternal::StageCommand* stageCommand = new internal::StageCommand;\n\n\t\tstageCommand->m_type = internal::STAGE;\n\t\tstageCommand->m_buffer = m_vertex_buffer;\n\t\tstageCommand->m_offset = 0;\n\t\tstageCommand->m_size = new_size;\n\n\t\tm_command_queue.push(stageCommand);\n\n\t\tMemoryBlock* mem_block = last_occupied_block->m_next_block;\n\t\twhile (mem_block != nullptr)\n\t\t{\n\t\t\tMemoryBlock* old_block = mem_block;\n\t\t\tmem_block = mem_block->m_next_block;\n\t\t\tif (old_block->m_free)\n\t\t\t{\n\t\t\t\tdelete old_block;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGE(\"Last occupied block wasn't last occupied block.\");\n\t\t\t}\n\t\t}\n\t\tlast_occupied_block->m_next_block = nullptr;\n\n\t\tif (last_occupied_block->m_offset + last_occupied_block->m_size < new_size)\n\t\t{\n\t\t\tMemoryBlock* new_block = new MemoryBlock;\n\t\t\tZeroMemory(new_block, sizeof(MemoryBlock));\n\t\t\tnew_block->m_alignment = 1;\n\t\t\tnew_block->m_free = true;\n\t\t\tnew_block->m_next_block = nullptr;\n\t\t\tnew_block->m_prev_block = last_occupied_block;\n\t\t\tnew_block->m_offset = last_occupied_block->m_offset + last_occupied_block->m_size;\n\t\t\tnew_block->m_size = new_size - new_block->m_offset;\n\n\t\t\tlast_occupied_block->m_next_block = new_block;\n\t\t}\n\t\telse if (last_occupied_block->m_offset + last_occupied_block->m_size > new_size)\n\t\t{\n\t\t\tLOGE(\"Vertex buffer has shrunken too much and doesn't fit.\");\n\t\t}\n\n\t\tm_updated = true;\n\t}\n\n\tvoid D3D12ModelPool::ShrinkIndexHeapToFit()\n\t{\n\t\tMemoryBlock* last_occupied_block = nullptr;\n\t\tfor (MemoryBlock* mem_block = m_index_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block)\n\t\t{\n\t\t\tif (mem_block->m_free == false)\n\t\t\t{\n\t\t\t\tlast_occupied_block = mem_block;\n\t\t\t}\n\t\t}\n\t\tif (last_occupied_block == nullptr)\n\t\t{\n\t\t\tLOGW(\"You're trying to shrink an empty index heap, returning instead.\");\n\t\t}\n\t\tsize_t new_size = last_occupied_block->m_offset + last_occupied_block->m_size;\n\t\tnew_size = SizeAlignAnyAlignment(new_size, 65536);\n\n\t\tif (new_size == m_index_buffer->m_size)\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tID3D12Resource* old_buffer = m_index_buffer->m_buffer;\n\t\tID3D12Resource* new_buffer;\n\t\tID3D12Resource* old_staging = m_index_buffer->m_staging;\n\t\tID3D12Resource* new_staging;\n\n\t\tuint8_t* cpu_address;\n\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(new_size);\n\n\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t&heap_properties_upload,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&buffer_desc,\n\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&new_staging));\n\t\tNAME_D3D12RESOURCE(new_staging);\n\n\t\tCD3DX12_RANGE read_range(0, new_size);\n\n\t\tnew_staging->Map(0, &read_range, reinterpret_cast<void**>(&(cpu_address)));\n\n\t\tmemcpy(cpu_address, m_index_buffer->m_cpu_address, new_size);\n\n\t\tm_index_buffer->m_size = static_cast<std::uint32_t>(new_size);\n\t\tm_index_buffer->m_is_staged = true;\n\t\tm_index_buffer->m_cpu_address = cpu_address;\n\n\t\tm_render_system.WaitForAllPreviousWork();\n\n\t\tSAFE_RELEASE(m_index_buffer->m_buffer);\n\t\tSAFE_RELEASE(m_index_buffer->m_staging);\n\n\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t&heap_properties_default,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&buffer_desc,\n\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(m_index_buffer->m_target_resource_state),\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&new_buffer));\n\t\tNAME_D3D12RESOURCE(new_buffer);\n\n\t\tm_index_buffer->m_buffer = new_buffer;\n\t\tm_index_buffer->m_staging = new_staging;\n\t\tm_index_buffer->m_gpu_address = new_buffer->GetGPUVirtualAddress();\n\n\t\tfor (int i = 0; i < m_command_queue.size(); ++i)\n\t\t{\n\t\t\tinternal::Command* command = m_command_queue.front();\n\t\t\tm_command_queue.pop();\n\t\t\tm_command_queue.push(command);\n\n\t\t\tswitch (command->m_type)\n\t\t\t{\n\t\t\tcase internal::CommandType::COPY:\n\t\t\t{\n\t\t\t\tinternal::CopyCommand* copy_command = static_cast<internal::CopyCommand*>(command);\n\t\t\t\tif (copy_command->m_dest == old_staging)\n\t\t\t\t{\n\t\t\t\t\tcopy_command->m_dest = new_staging;\n\t\t\t\t}\n\t\t\t\tif (copy_command->m_dest == old_buffer)\n\t\t\t\t{\n\t\t\t\t\tcopy_command->m_dest = new_buffer;\n\t\t\t\t}\n\t\t\t\tif (copy_command->m_source == old_staging)\n\t\t\t\t{\n\t\t\t\t\tcopy_command->m_source = new_staging;\n\t\t\t\t}\n\t\t\t\tif (copy_command->m_source == old_buffer)\n\t\t\t\t{\n\t\t\t\t\tcopy_command->m_source = new_buffer;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\tcase internal::CommandType::TRANSITION:\n\t\t\t{\n\t\t\t\tinternal::TransitionCommand* transition_command = static_cast<internal::TransitionCommand*>(command);\n\t\t\t\tif (transition_command->m_buffer == old_staging)\n\t\t\t\t{\n\t\t\t\t\ttransition_command->m_buffer = new_staging;\n\t\t\t\t}\n\t\t\t\tif (transition_command->m_buffer == old_buffer)\n\t\t\t\t{\n\t\t\t\t\ttransition_command->m_buffer = new_buffer;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tinternal::StageCommand* stageCommand = new internal::StageCommand;\n\n\t\tstageCommand->m_type = internal::STAGE;\n\t\tstageCommand->m_buffer = m_index_buffer;\n\t\tstageCommand->m_offset = 0;\n\t\tstageCommand->m_size = new_size;\n\n\t\tm_command_queue.push(stageCommand); \n\t\t\n\t\tMemoryBlock* mem_block = last_occupied_block->m_next_block;\n\t\twhile (mem_block != nullptr)\n\t\t{\n\t\t\tMemoryBlock* old_block = mem_block;\n\t\t\tmem_block = mem_block->m_next_block;\n\t\t\tif (old_block->m_free)\n\t\t\t{\n\t\t\t\tdelete old_block;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGE(\"Last occupied block wasn't last occupied block.\");\n\t\t\t}\n\t\t}\n\t\tlast_occupied_block->m_next_block = nullptr;\n\n\t\tif (last_occupied_block->m_offset + last_occupied_block->m_size < new_size)\n\t\t{\n\t\t\tMemoryBlock* new_block = new MemoryBlock;\n\t\t\tZeroMemory(new_block, sizeof(MemoryBlock));\n\t\t\tnew_block->m_alignment = 1;\n\t\t\tnew_block->m_free = true;\n\t\t\tnew_block->m_next_block = nullptr;\n\t\t\tnew_block->m_prev_block = last_occupied_block;\n\t\t\tnew_block->m_offset = last_occupied_block->m_offset + last_occupied_block->m_size;\n\t\t\tnew_block->m_size = new_size - new_block->m_offset;\n\n\t\t\tlast_occupied_block->m_next_block = new_block;\n\t\t}\n\t\telse if (last_occupied_block->m_offset + last_occupied_block->m_size > new_size)\n\t\t{\n\t\t\tLOGE(\"Vertex buffer has shrunken too much and doesn't fit.\");\n\t\t}\n\n\t\tm_updated = true;\n\t}\n\n\tvoid D3D12ModelPool::Defragment()\n\t{\n\t\tDefragmentVertexHeap();\n\t\tDefragmentIndexHeap();\n\t}\n\n\tvoid D3D12ModelPool::DefragmentVertexHeap()\n\t{\n\t\t{\n\t\t\tinternal::TransitionCommand* transition_command = new internal::TransitionCommand;\n\t\t\ttransition_command->m_type = internal::CommandType::TRANSITION;\n\t\t\ttransition_command->m_buffer = m_vertex_buffer->m_buffer;\n\t\t\tif (m_vertex_buffer->m_is_staged)\n\t\t\t{\n\t\t\t\ttransition_command->m_old_state = m_vertex_buffer->m_target_resource_state;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\ttransition_command->m_old_state = ResourceState::COPY_DEST;\n\t\t\t}\n\t\t\ttransition_command->m_new_state = ResourceState::COPY_SOURCE;\n\n\t\t\tm_command_queue.push(transition_command);\n\t\t}\n\n\t\tMemoryBlock* largest_block = m_vertex_heap_start_block;\n\t\tfor (MemoryBlock* mem_block = m_vertex_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block)\n\t\t{\n\t\t\tif (!mem_block->m_free)\n\t\t\t{\n\t\t\t\tif (largest_block->m_free || mem_block->m_size > largest_block->m_size)\n\t\t\t\t{\n\t\t\t\t\tlargest_block = mem_block;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (!largest_block->m_free)\n\t\t{\n\t\t\tif (largest_block->m_size > m_intermediate_size)\n\t\t\t{\n\t\t\t\tID3D12Resource* buffer;\n\t\t\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\t\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(SizeAlignAnyAlignment(largest_block->m_size, 65536));\n\n\t\t\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t\t\t&heap_properties_default,\n\t\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t\t&buffer_desc,\n\t\t\t\t\tD3D12_RESOURCE_STATE_COPY_DEST,\n\t\t\t\t\tnullptr,\n\t\t\t\t\tIID_PPV_ARGS(&buffer));\n\t\t\t\tbuffer->SetName(L\"Memory pool intermediate buffer\");\n\n\t\t\t\tm_render_system.WaitForAllPreviousWork();\n\n\t\t\t\tfor (int i = 0; i < m_command_queue.size(); ++i)\n\t\t\t\t{\n\t\t\t\t\tinternal::Command* command = m_command_queue.front();\n\t\t\t\t\tm_command_queue.pop();\n\t\t\t\t\tm_command_queue.push(command);\n\n\t\t\t\t\tswitch (command->m_type)\n\t\t\t\t\t{\n\t\t\t\t\tcase internal::CommandType::COPY:\n\t\t\t\t\t{\n\t\t\t\t\t\tinternal::CopyCommand* copy_command = static_cast<internal::CopyCommand*>(command);\n\t\t\t\t\t\tif (copy_command->m_dest == m_intermediate_buffer)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcopy_command->m_dest = buffer;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (copy_command->m_source == m_intermediate_buffer)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcopy_command->m_source = buffer;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t\tcase internal::CommandType::TRANSITION:\n\t\t\t\t\t{\n\t\t\t\t\t\tinternal::TransitionCommand* transition_command = static_cast<internal::TransitionCommand*>(command);\n\t\t\t\t\t\tif (transition_command->m_buffer == m_intermediate_buffer)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttransition_command->m_buffer = buffer;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tSAFE_RELEASE(m_intermediate_buffer);\n\t\t\t\tm_intermediate_buffer = buffer;\n\t\t\t\tm_intermediate_size = SizeAlignAnyAlignment(largest_block->m_size, 65536);\n\t\t\t}\n\t\t}\n\n\t\tbool contents_changed = false;\n\n\t\tMemoryBlock* mem_block = m_vertex_heap_start_block;\n\t\twhile (mem_block->m_next_block != nullptr)\n\t\t{\n\t\t\tif (mem_block->m_free)\n\t\t\t{\n\t\t\t\tif (mem_block->m_next_block->m_free)\n\t\t\t\t{\n\t\t\t\t\tmem_block->m_size += mem_block->m_next_block->m_size;\n\n\t\t\t\t\tif (mem_block->m_next_block->m_next_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tmem_block->m_next_block = mem_block->m_next_block->m_next_block;\n\t\t\t\t\t\tdelete mem_block->m_next_block->m_prev_block;\n\t\t\t\t\t\tmem_block->m_next_block->m_prev_block = mem_block;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdelete mem_block->m_next_block;\n\t\t\t\t\t\tmem_block->m_next_block = nullptr;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tMemoryBlock* next_block = mem_block->m_next_block;\n\n\t\t\t\t\tsize_t original_offset = next_block->m_offset;\n\n\t\t\t\t\tif (mem_block->m_prev_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tmem_block->m_prev_block->m_next_block = mem_block->m_next_block;\n\t\t\t\t\t\tnext_block->m_prev_block = mem_block->m_prev_block;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tm_vertex_heap_start_block = next_block;\n\t\t\t\t\t\tnext_block->m_prev_block = nullptr;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (next_block->m_next_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tnext_block->m_next_block->m_prev_block = mem_block;\n\t\t\t\t\t\tmem_block->m_next_block = next_block->m_next_block;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tmem_block->m_next_block = nullptr;\n\t\t\t\t\t}\n\n\t\t\t\t\tnext_block->m_next_block = mem_block;\n\t\t\t\t\tmem_block->m_prev_block = next_block;\n\n\t\t\t\t\tnext_block->m_offset = SizeAlignAnyAlignment(mem_block->m_offset, next_block->m_alignment);\n\t\t\t\t\tif (next_block->m_offset%next_block->m_alignment != 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tLOGW(\"Wrong alignment after trying to align\");\n\t\t\t\t\t}\n\t\t\t\t\tif (next_block->m_prev_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tnext_block->m_size += SizeAlignAnyAlignment(next_block->m_offset - mem_block->m_offset, next_block->m_alignment);\n\t\t\t\t\t}\n\t\t\t\t\tmem_block->m_size -= SizeAlignAnyAlignment(next_block->m_offset - mem_block->m_offset, next_block->m_alignment);\n\t\t\t\t\tmem_block->m_offset = next_block->m_offset + next_block->m_size;\n\n\t\t\t\t\tstd::map<std::uint64_t, internal::MeshInternal*>::iterator it = m_loaded_meshes.begin();\n\n\t\t\t\t\tfor (; it != m_loaded_meshes.end(); ++it)\n\t\t\t\t\t{\n\t\t\t\t\t\tinternal::D3D12MeshInternal* mesh = static_cast<internal::D3D12MeshInternal*>((*it).second);\n\t\t\t\t\t\tif (mesh->m_vertex_memory_block == next_block)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmesh->m_vertex_staging_buffer_offset = next_block->m_offset / next_block->m_alignment;\n\t\t\t\t\t\t\tif (next_block->m_offset%next_block->m_alignment != 0)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tLOGW(\"Wrong alignment\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tinternal::CopyCommand* copy_to_intermediate_command = new internal::CopyCommand;\n\n\t\t\t\t\tcopy_to_intermediate_command->m_type = internal::CommandType::COPY;\n\t\t\t\t\tcopy_to_intermediate_command->m_source = m_vertex_buffer->m_buffer;\n\t\t\t\t\tcopy_to_intermediate_command->m_dest = m_intermediate_buffer;\n\t\t\t\t\tcopy_to_intermediate_command->m_source_offset = original_offset;\n\t\t\t\t\tcopy_to_intermediate_command->m_dest_offset = 0;\n\t\t\t\t\tcopy_to_intermediate_command->m_size = next_block->m_size;\n\n\t\t\t\t\tm_command_queue.push(copy_to_intermediate_command);\n\n\t\t\t\t\tinternal::TransitionCommand* transition_intermediate_read_command = new internal::TransitionCommand;\n\n\t\t\t\t\ttransition_intermediate_read_command->m_type = internal::TRANSITION;\n\t\t\t\t\ttransition_intermediate_read_command->m_buffer = m_intermediate_buffer;\n\t\t\t\t\ttransition_intermediate_read_command->m_old_state = ResourceState::COPY_DEST;\n\t\t\t\t\ttransition_intermediate_read_command->m_new_state = ResourceState::COPY_SOURCE;\n\n\t\t\t\t\tm_command_queue.push(transition_intermediate_read_command);\n\n\t\t\t\t\tinternal::TransitionCommand* transition_buffer_write_command = new internal::TransitionCommand;\n\n\t\t\t\t\ttransition_buffer_write_command->m_type = internal::TRANSITION;\n\t\t\t\t\ttransition_buffer_write_command->m_buffer = m_vertex_buffer->m_buffer;\n\t\t\t\t\ttransition_buffer_write_command->m_old_state = ResourceState::COPY_SOURCE;\n\t\t\t\t\ttransition_buffer_write_command->m_new_state = ResourceState::COPY_DEST;\n\n\t\t\t\t\tm_command_queue.push(transition_buffer_write_command);\n\n\t\t\t\t\tinternal::CopyCommand* copy_to_buffer_command = new internal::CopyCommand;\n\n\t\t\t\t\tcopy_to_buffer_command->m_type = internal::COPY;\n\t\t\t\t\tcopy_to_buffer_command->m_source = m_intermediate_buffer;\n\t\t\t\t\tcopy_to_buffer_command->m_source_offset = 0;\n\t\t\t\t\tcopy_to_buffer_command->m_dest = m_vertex_buffer->m_buffer;\n\t\t\t\t\tcopy_to_buffer_command->m_dest_offset = next_block->m_offset;\n\t\t\t\t\tcopy_to_buffer_command->m_size = next_block->m_size;\n\n\t\t\t\t\tm_command_queue.push(copy_to_buffer_command);\n\n\t\t\t\t\tinternal::TransitionCommand* transition_intermediate_write_command = new internal::TransitionCommand;\n\n\t\t\t\t\ttransition_intermediate_write_command->m_type = internal::TRANSITION;\n\t\t\t\t\ttransition_intermediate_write_command->m_buffer = m_intermediate_buffer;\n\t\t\t\t\ttransition_intermediate_write_command->m_old_state = ResourceState::COPY_SOURCE;\n\t\t\t\t\ttransition_intermediate_write_command->m_new_state = ResourceState::COPY_DEST;\n\n\t\t\t\t\tm_command_queue.push(transition_intermediate_write_command);\n\n\t\t\t\t\tinternal::TransitionCommand* transition_buffer_read_command = new internal::TransitionCommand;\n\n\t\t\t\t\ttransition_buffer_read_command->m_type = internal::TRANSITION;\n\t\t\t\t\ttransition_buffer_read_command->m_buffer = m_vertex_buffer->m_buffer;\n\t\t\t\t\ttransition_buffer_read_command->m_old_state = ResourceState::COPY_DEST;\n\t\t\t\t\ttransition_buffer_read_command->m_new_state = ResourceState::COPY_SOURCE;\n\n\t\t\t\t\tm_command_queue.push(transition_buffer_read_command);\n\n\t\t\t\t\tmemcpy(m_vertex_buffer->m_cpu_address + next_block->m_offset, m_vertex_buffer->m_cpu_address + original_offset, next_block->m_size);\n\n\t\t\t\t\tcontents_changed = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmem_block = mem_block->m_next_block;\n\t\t\t}\n\t\t}\n\n\t\t{\n\t\t\tinternal::TransitionCommand* transition_command = new internal::TransitionCommand;\n\t\t\ttransition_command->m_type = internal::CommandType::TRANSITION;\n\t\t\ttransition_command->m_buffer = m_vertex_buffer->m_buffer;\n\t\t\tif (m_vertex_buffer->m_is_staged)\n\t\t\t{\n\t\t\t\ttransition_command->m_new_state = m_vertex_buffer->m_target_resource_state;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\ttransition_command->m_new_state = ResourceState::COPY_DEST;\n\t\t\t}\n\t\t\ttransition_command->m_old_state = ResourceState::COPY_SOURCE;\n\n\t\t\tm_command_queue.push(transition_command);\n\t\t}\n\n\t\tif (contents_changed)\n\t\t{\n\t\t\tm_updated = true;\n\t\t}\n\t}\n\n\tvoid D3D12ModelPool::DefragmentIndexHeap()\n\t{\n\t\t{\n\t\t\tinternal::TransitionCommand* transition_command = new internal::TransitionCommand;\n\t\t\ttransition_command->m_type = internal::CommandType::TRANSITION;\n\t\t\ttransition_command->m_buffer = m_index_buffer->m_buffer;\n\t\t\tif (m_index_buffer->m_is_staged)\n\t\t\t{\n\t\t\t\ttransition_command->m_old_state = m_index_buffer->m_target_resource_state;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\ttransition_command->m_old_state = ResourceState::COPY_DEST;\n\t\t\t}\n\t\t\ttransition_command->m_new_state = ResourceState::COPY_SOURCE;\n\n\t\t\tm_command_queue.push(transition_command);\n\t\t}\n\n\t\tMemoryBlock* largest_block = m_index_heap_start_block;\n\t\tfor (MemoryBlock* mem_block = m_index_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block)\n\t\t{\n\t\t\tif (!mem_block->m_free)\n\t\t\t{\n\t\t\t\tif (largest_block->m_free || mem_block->m_size > largest_block->m_size)\n\t\t\t\t{\n\t\t\t\t\tlargest_block = mem_block;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (!largest_block->m_free)\n\t\t{\n\t\t\tif (largest_block->m_size > m_intermediate_size)\n\t\t\t{\n\t\t\t\tID3D12Resource* buffer;\n\t\t\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\t\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(SizeAlignAnyAlignment(largest_block->m_size, 65536));\n\n\t\t\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t\t\t&heap_properties_default,\n\t\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t\t&buffer_desc,\n\t\t\t\t\tD3D12_RESOURCE_STATE_COPY_DEST,\n\t\t\t\t\tnullptr,\n\t\t\t\t\tIID_PPV_ARGS(&buffer));\n\t\t\t\tbuffer->SetName(L\"Memory pool intermediate buffer\");\n\n\t\t\t\tm_render_system.WaitForAllPreviousWork();\n\n\t\t\t\tfor (int i = 0; i < m_command_queue.size(); ++i)\n\t\t\t\t{\n\t\t\t\t\tinternal::Command* command = m_command_queue.front();\n\t\t\t\t\tm_command_queue.pop();\n\t\t\t\t\tm_command_queue.push(command);\n\n\t\t\t\t\tswitch (command->m_type)\n\t\t\t\t\t{\n\t\t\t\t\tcase internal::CommandType::COPY:\n\t\t\t\t\t{\n\t\t\t\t\t\tinternal::CopyCommand* copy_command = static_cast<internal::CopyCommand*>(command);\n\t\t\t\t\t\tif (copy_command->m_dest == m_intermediate_buffer)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcopy_command->m_dest = buffer;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (copy_command->m_source == m_intermediate_buffer)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcopy_command->m_source = buffer;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t\tcase internal::CommandType::TRANSITION:\n\t\t\t\t\t{\n\t\t\t\t\t\tinternal::TransitionCommand* transition_command = static_cast<internal::TransitionCommand*>(command);\n\t\t\t\t\t\tif (transition_command->m_buffer == m_intermediate_buffer)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttransition_command->m_buffer = buffer;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tSAFE_RELEASE(m_intermediate_buffer);\n\t\t\t\tm_intermediate_buffer = buffer;\n\t\t\t\tm_intermediate_size = SizeAlignAnyAlignment(largest_block->m_size, 65536);\n\t\t\t}\n\t\t}\n\n\t\tbool contents_changed = false;\n\n\t\tMemoryBlock* mem_block = m_index_heap_start_block;\n\t\twhile (mem_block->m_next_block != nullptr)\n\t\t{\n\t\t\tif (mem_block->m_free)\n\t\t\t{\n\t\t\t\tif (mem_block->m_next_block->m_free)\n\t\t\t\t{\n\t\t\t\t\tmem_block->m_size += mem_block->m_next_block->m_size;\n\n\t\t\t\t\tif (mem_block->m_next_block->m_next_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tmem_block->m_next_block = mem_block->m_next_block->m_next_block;\n\t\t\t\t\t\tdelete mem_block->m_next_block->m_prev_block;\n\t\t\t\t\t\tmem_block->m_next_block->m_prev_block = mem_block;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdelete mem_block->m_next_block;\n\t\t\t\t\t\tmem_block->m_next_block = nullptr;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tMemoryBlock* next_block = mem_block->m_next_block;\n\n\t\t\t\t\tsize_t original_offset = next_block->m_offset;\n\n\t\t\t\t\tif (mem_block->m_prev_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tmem_block->m_prev_block->m_next_block = mem_block->m_next_block;\n\t\t\t\t\t\tnext_block->m_prev_block = mem_block->m_prev_block;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tm_index_heap_start_block = next_block;\n\t\t\t\t\t\tnext_block->m_prev_block = nullptr;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (next_block->m_next_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tnext_block->m_next_block->m_prev_block = mem_block;\n\t\t\t\t\t\tmem_block->m_next_block = next_block->m_next_block;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tmem_block->m_next_block = nullptr;\n\t\t\t\t\t}\n\n\t\t\t\t\tnext_block->m_next_block = mem_block;\n\t\t\t\t\tmem_block->m_prev_block = next_block;\n\n\t\t\t\t\tnext_block->m_offset = SizeAlignAnyAlignment(mem_block->m_offset, next_block->m_alignment);\n\t\t\t\t\tif (next_block->m_prev_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tnext_block->m_size += next_block->m_offset - mem_block->m_offset;\n\t\t\t\t\t}\n\t\t\t\t\tmem_block->m_size -= next_block->m_offset - mem_block->m_offset;\n\t\t\t\t\tmem_block->m_offset = next_block->m_offset + next_block->m_size;\n\n\t\t\t\t\tstd::map<std::uint64_t, internal::MeshInternal*>::iterator it = m_loaded_meshes.begin();\n\n\t\t\t\t\tfor (; it != m_loaded_meshes.end(); ++it)\n\t\t\t\t\t{\n\t\t\t\t\t\tinternal::D3D12MeshInternal* mesh = static_cast<internal::D3D12MeshInternal*>((*it).second);\n\t\t\t\t\t\tif (mesh->m_index_memory_block == next_block)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmesh->m_index_staging_buffer_offset = SizeAlignAnyAlignment(next_block->m_offset, next_block->m_alignment)\n\t\t\t\t\t\t\t\t/ next_block->m_alignment;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tinternal::CopyCommand* copy_to_intermediate_command = new internal::CopyCommand;\n\n\t\t\t\t\tcopy_to_intermediate_command->m_type = internal::CommandType::COPY;\n\t\t\t\t\tcopy_to_intermediate_command->m_source = m_index_buffer->m_buffer;\n\t\t\t\t\tcopy_to_intermediate_command->m_dest = m_intermediate_buffer;\n\t\t\t\t\tcopy_to_intermediate_command->m_source_offset = original_offset;\n\t\t\t\t\tcopy_to_intermediate_command->m_dest_offset = 0;\n\t\t\t\t\tcopy_to_intermediate_command->m_size = next_block->m_size;\n\n\t\t\t\t\tm_command_queue.push(copy_to_intermediate_command);\n\n\t\t\t\t\tinternal::TransitionCommand* transition_intermediate_read_command = new internal::TransitionCommand;\n\t\t\t\t\ttransition_intermediate_read_command->m_type = internal::TRANSITION;\n\t\t\t\t\ttransition_intermediate_read_command->m_buffer = m_intermediate_buffer;\n\t\t\t\t\ttransition_intermediate_read_command->m_old_state = ResourceState::COPY_DEST;\n\t\t\t\t\ttransition_intermediate_read_command->m_new_state = ResourceState::COPY_SOURCE;\n\n\t\t\t\t\tm_command_queue.push(transition_intermediate_read_command);\n\n\t\t\t\t\tinternal::TransitionCommand* transition_buffer_write_command = new internal::TransitionCommand;\n\t\t\t\t\ttransition_buffer_write_command->m_type = internal::TRANSITION;\n\t\t\t\t\ttransition_buffer_write_command->m_buffer = m_index_buffer->m_buffer;\n\t\t\t\t\ttransition_buffer_write_command->m_old_state = ResourceState::COPY_SOURCE;\n\t\t\t\t\ttransition_buffer_write_command->m_new_state = ResourceState::COPY_DEST;\n\n\t\t\t\t\tm_command_queue.push(transition_buffer_write_command);\n\n\t\t\t\t\tinternal::CopyCommand* copy_to_buffer_command = new internal::CopyCommand;\n\n\t\t\t\t\tcopy_to_buffer_command->m_type = internal::COPY;\n\t\t\t\t\tcopy_to_buffer_command->m_source = m_intermediate_buffer;\n\t\t\t\t\tcopy_to_buffer_command->m_source_offset = 0;\n\t\t\t\t\tcopy_to_buffer_command->m_dest = m_index_buffer->m_buffer;\n\t\t\t\t\tcopy_to_buffer_command->m_dest_offset = next_block->m_offset;\n\t\t\t\t\tcopy_to_buffer_command->m_size = next_block->m_size;\n\n\t\t\t\t\tm_command_queue.push(copy_to_buffer_command);\n\n\t\t\t\t\tinternal::TransitionCommand* transition_intermediate_write_command = new internal::TransitionCommand;\n\t\t\t\t\ttransition_intermediate_write_command->m_type = internal::TRANSITION;\n\t\t\t\t\ttransition_intermediate_write_command->m_buffer = m_intermediate_buffer;\n\t\t\t\t\ttransition_intermediate_write_command->m_old_state = ResourceState::COPY_SOURCE;\n\t\t\t\t\ttransition_intermediate_write_command->m_new_state = ResourceState::COPY_DEST;\n\n\t\t\t\t\tm_command_queue.push(transition_intermediate_write_command);\n\n\t\t\t\t\tinternal::TransitionCommand* transition_buffer_read_command = new internal::TransitionCommand;\n\t\t\t\t\ttransition_buffer_read_command->m_type = internal::TRANSITION;\n\t\t\t\t\ttransition_buffer_read_command->m_buffer = m_index_buffer->m_buffer;\n\t\t\t\t\ttransition_buffer_read_command->m_old_state = ResourceState::COPY_DEST;\n\t\t\t\t\ttransition_buffer_read_command->m_new_state = ResourceState::COPY_SOURCE;\n\n\t\t\t\t\tm_command_queue.push(transition_buffer_read_command);\n\n\t\t\t\t\tmemcpy(m_index_buffer->m_cpu_address + next_block->m_offset, m_index_buffer->m_cpu_address + original_offset, next_block->m_size);\n\n\t\t\t\t\tcontents_changed = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmem_block = mem_block->m_next_block;\n\t\t\t}\n\t\t}\n\n\t\t{\n\t\t\tinternal::TransitionCommand* transition_command = new internal::TransitionCommand;\n\t\t\ttransition_command->m_type = internal::CommandType::TRANSITION;\n\t\t\ttransition_command->m_buffer = m_index_buffer->m_buffer;\n\t\t\tif (m_index_buffer->m_is_staged)\n\t\t\t{\n\t\t\t\ttransition_command->m_new_state = m_index_buffer->m_target_resource_state;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\ttransition_command->m_new_state = ResourceState::COPY_DEST;\n\t\t\t}\n\t\t\ttransition_command->m_old_state = ResourceState::COPY_SOURCE;\n\n\t\t\tm_command_queue.push(transition_command);\n\t\t}\n\n\t\tif (contents_changed)\n\t\t{\n\t\t\tm_updated = true;\n\t\t}\n\t}\n\t\n\tvoid D3D12ModelPool::Resize(size_t vertex_heap_new_size, size_t index_heap_new_size)\n\t{\n\t\tResizeVertexHeap(vertex_heap_new_size);\n\t\tResizeIndexHeap(index_heap_new_size);\t\t\n\t}\n\n\tvoid D3D12ModelPool::ResizeVertexHeap(size_t vertex_heap_new_size)\n\t{\n\t\tMemoryBlock* mem_block = m_vertex_heap_start_block;\n\t\twhile (mem_block->m_next_block != nullptr)\n\t\t{\n\t\t\tif (mem_block->m_free)\n\t\t\t{\n\t\t\t\tif (mem_block->m_next_block->m_free)\n\t\t\t\t{\n\t\t\t\t\tmem_block->m_size += mem_block->m_next_block->m_size;\n\n\t\t\t\t\tif (mem_block->m_next_block->m_next_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tmem_block->m_next_block = mem_block->m_next_block->m_next_block;\n\t\t\t\t\t\tdelete mem_block->m_next_block->m_prev_block;\n\t\t\t\t\t\tmem_block->m_next_block->m_prev_block = mem_block;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdelete mem_block->m_next_block;\n\t\t\t\t\t\tmem_block->m_next_block = nullptr;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmem_block = mem_block->m_next_block;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmem_block = mem_block->m_next_block;\n\t\t\t}\n\t\t}\n\n\t\tMemoryBlock* last_occupied_block = nullptr;\n\t\tfor (mem_block = m_vertex_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block)\n\t\t{\n\t\t\tif (mem_block->m_free == false)\n\t\t\t{\n\t\t\t\tlast_occupied_block = mem_block;\n\t\t\t}\n\t\t}\n\n\t\tsize_t new_size;\n\t\tif (last_occupied_block != nullptr)\n\t\t{\n\t\t\tnew_size = last_occupied_block->m_offset + last_occupied_block->m_size;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tnew_size = 0;\n\t\t}\n\t\tsize_t old_size = m_vertex_buffer->m_size;\n\n\t\tif (new_size > SizeAlignAnyAlignment(vertex_heap_new_size, 65536))\n\t\t{\n\t\t\tShrinkVertexHeapToFit();\n\t\t}\n\t\telse if (SizeAlignAnyAlignment(vertex_heap_new_size, 65536) == m_vertex_buffer->m_size)\n\t\t{\n\t\t\treturn;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tnew_size = SizeAlignAnyAlignment(vertex_heap_new_size, 65536);\n\n\t\t\tID3D12Resource* old_buffer = m_vertex_buffer->m_buffer;\n\t\t\tID3D12Resource* new_buffer;\n\t\t\tID3D12Resource* old_staging = m_vertex_buffer->m_staging;\n\t\t\tID3D12Resource* new_staging;\n\n\t\t\tuint8_t* cpu_address;\n\n\t\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\t\tCD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(new_size);\n\n\t\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t\t&heap_properties_upload,\n\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t&buffer_desc,\n\t\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\t\tnullptr,\n\t\t\t\tIID_PPV_ARGS(&new_staging));\n\t\t\tNAME_D3D12RESOURCE(new_staging);\n\n\t\t\tCD3DX12_RANGE read_range(0, new_size);\n\n\t\t\tnew_staging->Map(0, &read_range, reinterpret_cast<void**>(&(cpu_address)));\n\n\t\t\tmemcpy(cpu_address, m_vertex_buffer->m_cpu_address, std::min(new_size, m_vertex_buffer->m_size));\n\n\t\t\tm_vertex_buffer->m_size = static_cast<std::uint32_t>(new_size);\n\t\t\tm_vertex_buffer->m_is_staged = true;\n\t\t\tm_vertex_buffer->m_cpu_address = cpu_address;\n\n\t\t\tm_render_system.WaitForAllPreviousWork();\n\n\t\t\tSAFE_RELEASE(m_vertex_buffer->m_buffer);\n\t\t\tSAFE_RELEASE(m_vertex_buffer->m_staging);\n\n\t\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t\t&heap_properties_default,\n\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t&buffer_desc,\n\t\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(m_vertex_buffer->m_target_resource_state),\n\t\t\t\tnullptr,\n\t\t\t\tIID_PPV_ARGS(&new_buffer));\n\t\t\tNAME_D3D12RESOURCE(new_buffer);\n\n\t\t\tm_vertex_buffer->m_buffer = new_buffer;\n\t\t\tm_vertex_buffer->m_staging = new_staging;\n\t\t\tm_vertex_buffer->m_gpu_address = m_vertex_buffer->m_buffer->GetGPUVirtualAddress();\n\n\t\t\tfor (int i = 0; i < m_command_queue.size(); ++i)\n\t\t\t{\n\t\t\t\tinternal::Command* command = m_command_queue.front();\n\t\t\t\tm_command_queue.pop();\n\t\t\t\tm_command_queue.push(command);\n\n\t\t\t\tswitch (command->m_type)\n\t\t\t\t{\n\t\t\t\tcase internal::CommandType::COPY:\n\t\t\t\t{\n\t\t\t\t\tinternal::CopyCommand* copy_command = static_cast<internal::CopyCommand*>(command);\n\t\t\t\t\tif (copy_command->m_dest == old_staging)\n\t\t\t\t\t{\n\t\t\t\t\t\tcopy_command->m_dest = new_staging;\n\t\t\t\t\t}\n\t\t\t\t\tif (copy_command->m_dest == old_buffer)\n\t\t\t\t\t{\n\t\t\t\t\t\tcopy_command->m_dest = new_buffer;\n\t\t\t\t\t}\n\t\t\t\t\tif (copy_command->m_source == old_staging)\n\t\t\t\t\t{\n\t\t\t\t\t\tcopy_command->m_source = new_staging;\n\t\t\t\t\t}\n\t\t\t\t\tif (copy_command->m_source == old_buffer)\n\t\t\t\t\t{\n\t\t\t\t\t\tcopy_command->m_source = new_buffer;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t\tcase internal::CommandType::TRANSITION:\n\t\t\t\t{\n\t\t\t\t\tinternal::TransitionCommand* transition_command = static_cast<internal::TransitionCommand*>(command);\n\t\t\t\t\tif (transition_command->m_buffer == old_staging)\n\t\t\t\t\t{\n\t\t\t\t\t\ttransition_command->m_buffer = new_staging;\n\t\t\t\t\t}\n\t\t\t\t\tif (transition_command->m_buffer == old_buffer)\n\t\t\t\t\t{\n\t\t\t\t\t\ttransition_command->m_buffer = new_buffer;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tinternal::StageCommand* stageCommand = new internal::StageCommand;\n\n\t\t\tstageCommand->m_type = internal::STAGE;\n\t\t\tstageCommand->m_buffer = m_vertex_buffer;\n\t\t\tstageCommand->m_offset = 0;\n\t\t\tstageCommand->m_size = new_size;\n\n\t\t\tm_command_queue.push(stageCommand);\n\n\t\t\tif (last_occupied_block != nullptr)\n\t\t\t{\n\t\t\t\tif (last_occupied_block->m_offset + last_occupied_block->m_size < new_size)\n\t\t\t\t{\n\t\t\t\t\tif (last_occupied_block->m_next_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tlast_occupied_block->m_next_block->m_size = new_size - (last_occupied_block->m_offset + last_occupied_block->m_size);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tMemoryBlock* new_block = new MemoryBlock;\n\n\t\t\t\t\t\tlast_occupied_block->m_next_block = new_block;\n\t\t\t\t\t\tnew_block->m_prev_block = last_occupied_block;\n\t\t\t\t\t\tnew_block->m_free = true;\n\t\t\t\t\t\tnew_block->m_alignment = 1;\n\t\t\t\t\t\tnew_block->m_offset = last_occupied_block->m_offset + last_occupied_block->m_size;\n\t\t\t\t\t\tnew_block->m_next_block = nullptr;\n\t\t\t\t\t\tnew_block->m_size = new_size - new_block->m_offset;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tm_vertex_heap_start_block->m_size = new_size;\n\t\t\t}\n\t\t}\n\t\tm_updated = true;\n\t}\n\n\tvoid D3D12ModelPool::ResizeIndexHeap(size_t index_heap_new_size)\n\t{\n\t\tMemoryBlock* mem_block = m_index_heap_start_block;\n\t\twhile (mem_block->m_next_block != nullptr)\n\t\t{\n\t\t\tif (mem_block->m_free)\n\t\t\t{\n\t\t\t\tif (mem_block->m_next_block->m_free)\n\t\t\t\t{\n\t\t\t\t\tmem_block->m_size += mem_block->m_next_block->m_size;\n\n\t\t\t\t\tif (mem_block->m_next_block->m_next_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tmem_block->m_next_block = mem_block->m_next_block->m_next_block;\n\t\t\t\t\t\tdelete mem_block->m_next_block->m_prev_block;\n\t\t\t\t\t\tmem_block->m_next_block->m_prev_block = mem_block;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdelete mem_block->m_next_block;\n\t\t\t\t\t\tmem_block->m_next_block = nullptr;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmem_block = mem_block->m_next_block;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmem_block = mem_block->m_next_block;\n\t\t\t}\n\t\t}\n\n\t\tMemoryBlock* last_occupied_block = nullptr;\n\t\tfor (mem_block = m_index_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block)\n\t\t{\n\t\t\tif (mem_block->m_free == false)\n\t\t\t{\n\t\t\t\tlast_occupied_block = mem_block;\n\t\t\t}\n\t\t}\n\n\t\tsize_t new_size;\n\t\tif (last_occupied_block != nullptr)\n\t\t{\n\t\t\tnew_size = last_occupied_block->m_offset + last_occupied_block->m_size;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tnew_size = 0;\n\t\t}\n\n\t\tif (new_size > SizeAlignAnyAlignment(index_heap_new_size, 65536))\n\t\t{\n\t\t\tShrinkIndexHeapToFit();\n\t\t}\n\t\telse if (SizeAlignAnyAlignment(index_heap_new_size, 65536) == m_index_buffer->m_size)\n\t\t{\n\t\t\treturn;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tnew_size = SizeAlignAnyAlignment(index_heap_new_size, 65536);\n\n\t\t\tID3D12Resource* old_buffer = m_index_buffer->m_buffer;\n\t\t\tID3D12Resource* new_buffer;\n\t\t\tID3D12Resource* old_staging = m_index_buffer->m_staging;\n\t\t\tID3D12Resource* new_staging;\n\n\t\t\tuint8_t* cpu_address;\n\n\t\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\t\tCD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(new_size);\n\n\t\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t\t&heap_properties_upload,\n\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t&buffer_desc,\n\t\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\t\tnullptr,\n\t\t\t\tIID_PPV_ARGS(&new_staging));\n\t\t\tNAME_D3D12RESOURCE(new_staging);\n\n\t\t\tCD3DX12_RANGE read_range(0, new_size);\n\n\t\t\tnew_staging->Map(0, &read_range, reinterpret_cast<void**>(&(cpu_address)));\n\n\t\t\tmemcpy(cpu_address, m_index_buffer->m_cpu_address, std::min(new_size, m_index_buffer->m_size));\n\n\t\t\tm_index_buffer->m_size = static_cast<std::uint32_t>(new_size);\n\t\t\tm_index_buffer->m_is_staged = true;\n\t\t\tm_index_buffer->m_cpu_address = cpu_address;\n\n\t\t\tm_render_system.WaitForAllPreviousWork();\n\n\t\t\tSAFE_RELEASE(m_index_buffer->m_buffer);\n\t\t\tSAFE_RELEASE(m_index_buffer->m_staging);\n\n\t\t\tm_render_system.m_device->m_native->CreateCommittedResource(\n\t\t\t\t&heap_properties_default,\n\t\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t\t&buffer_desc,\n\t\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(m_index_buffer->m_target_resource_state),\n\t\t\t\tnullptr,\n\t\t\t\tIID_PPV_ARGS(&new_buffer));\n\t\t\tNAME_D3D12RESOURCE(new_buffer);\n\n\t\t\tm_index_buffer->m_buffer = new_buffer;\n\t\t\tm_index_buffer->m_staging = new_staging;\n\t\t\tm_index_buffer->m_gpu_address = m_index_buffer->m_buffer->GetGPUVirtualAddress();\n\n\t\t\tfor (int i = 0; i < m_command_queue.size(); ++i)\n\t\t\t{\n\t\t\t\tinternal::Command* command = m_command_queue.front();\n\t\t\t\tm_command_queue.pop();\n\t\t\t\tm_command_queue.push(command);\n\n\t\t\t\tswitch (command->m_type)\n\t\t\t\t{\n\t\t\t\tcase internal::CommandType::COPY:\n\t\t\t\t{\n\t\t\t\t\tinternal::CopyCommand* copy_command = static_cast<internal::CopyCommand*>(command);\n\t\t\t\t\tif (copy_command->m_dest == old_staging)\n\t\t\t\t\t{\n\t\t\t\t\t\tcopy_command->m_dest = new_staging;\n\t\t\t\t\t}\n\t\t\t\t\tif (copy_command->m_dest == old_buffer)\n\t\t\t\t\t{\n\t\t\t\t\t\tcopy_command->m_dest = new_buffer;\n\t\t\t\t\t}\n\t\t\t\t\tif (copy_command->m_source == old_staging)\n\t\t\t\t\t{\n\t\t\t\t\t\tcopy_command->m_source = new_staging;\n\t\t\t\t\t}\n\t\t\t\t\tif (copy_command->m_source == old_buffer)\n\t\t\t\t\t{\n\t\t\t\t\t\tcopy_command->m_source = new_buffer;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t\tcase internal::CommandType::TRANSITION:\n\t\t\t\t{\n\t\t\t\t\tinternal::TransitionCommand* transition_command = static_cast<internal::TransitionCommand*>(command);\n\t\t\t\t\tif (transition_command->m_buffer == old_staging)\n\t\t\t\t\t{\n\t\t\t\t\t\ttransition_command->m_buffer = new_staging;\n\t\t\t\t\t}\n\t\t\t\t\tif (transition_command->m_buffer == old_buffer)\n\t\t\t\t\t{\n\t\t\t\t\t\ttransition_command->m_buffer = new_buffer;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tinternal::StageCommand* stageCommand = new internal::StageCommand;\n\n\t\t\tstageCommand->m_type = internal::STAGE;\n\t\t\tstageCommand->m_buffer = m_index_buffer;\n\t\t\tstageCommand->m_offset = 0;\n\t\t\tstageCommand->m_size = new_size;\n\n\t\t\tm_command_queue.push(stageCommand);\n\n\t\t\tif (last_occupied_block != nullptr)\n\t\t\t{\n\t\t\t\tif (last_occupied_block->m_offset + last_occupied_block->m_size < new_size)\n\t\t\t\t{\n\t\t\t\t\tif (last_occupied_block->m_next_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tlast_occupied_block->m_next_block->m_size = new_size - (last_occupied_block->m_offset + last_occupied_block->m_size);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tMemoryBlock* new_block = new MemoryBlock;\n\n\t\t\t\t\t\tlast_occupied_block->m_next_block = new_block;\n\t\t\t\t\t\tnew_block->m_prev_block = last_occupied_block;\n\t\t\t\t\t\tnew_block->m_free = true;\n\t\t\t\t\t\tnew_block->m_alignment = 1;\n\t\t\t\t\t\tnew_block->m_offset = last_occupied_block->m_offset + last_occupied_block->m_size;\n\t\t\t\t\t\tnew_block->m_next_block = nullptr;\n\t\t\t\t\t\tnew_block->m_size = new_size - new_block->m_offset;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tm_index_heap_start_block->m_size = new_size;\n\t\t\t}\n\t\t}\n\t\tm_updated = true;\n\t}\n\n\tvoid D3D12ModelPool::MakeSpaceForModel(size_t vertex_size, size_t index_size)\n\t{\n\t\tif (GetVertexHeapFreeSpace() < vertex_size)\n\t\t{\n\t\t\tResizeVertexHeap(vertex_size + GetVertexHeapOccupiedSpace());\n\t\t}\n\t\tif (GetIndexHeapFreeSpace() < index_size)\n\t\t{\n\t\t\tResizeIndexHeap(index_size + GetIndexHeapOccupiedSpace());\n\t\t}\n\t}\n\n\tinternal::MeshInternal* D3D12ModelPool::LoadCustom_VerticesAndIndices(void* vertices_data, std::size_t num_vertices, std::size_t vertex_size, void* indices_data, std::size_t num_indices, std::size_t index_size)\n\t{\n\t\tinternal::D3D12MeshInternal* mesh = new internal::D3D12MeshInternal();\n\t\tmemset(mesh, 0, sizeof(internal::D3D12MeshInternal));\n\n\t\t//Allocate vertex buffer memory\n\n\t\t// Find Free Page\n\t\tMemoryBlock* vertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size);\n\n\t\t// Check if we found a page.\n\t\tif (vertex_memory_block == nullptr)\n\t\t{\n\t\t\tLOGW(\"Allocating memory for vertex buffer failed, trying to defragment.\");\n\t\t\tDefragmentVertexHeap();\n\t\t\tvertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size);\n\t\t\tif (vertex_memory_block == nullptr)\n\t\t\t{\n\t\t\t\tLOGW(\"Defragmenting failed, allocating more memory.\");\n\t\t\t\tResizeVertexHeap(std::max(GetVertexHeapSize() + vertex_size * (num_vertices + 1), GetVertexHeapSize() * 2));\n\t\t\t\tvertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size);\n\t\t\t\tif (vertex_memory_block == nullptr)\n\t\t\t\t{\n\t\t\t\t\tLOGE(\"Allocating memory for vertex buffer failed.\");\n\t\t\t\t\tdelete mesh;\n\t\t\t\t\treturn nullptr;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\t//Repeat the same procedure as before, but now for the index buffer\n\t\tMemoryBlock* index_memory_block = AllocateMemory(m_index_heap_start_block, num_indices*index_size, index_size);\n\n\t\t// Check if we found a page.\n\t\tif (index_memory_block == nullptr)\n\t\t{\n\t\t\tLOGW(\"Allocating memory for index buffer failed, trying to defragment.\");\n\t\t\tDefragmentIndexHeap();\n\t\t\tindex_memory_block = AllocateMemory(m_index_heap_start_block, num_indices*index_size, index_size);\n\t\t\tif (index_memory_block == nullptr)\n\t\t\t{\n\t\t\t\tLOGW(\"Defragmenting failed, allocating more memory.\");\n\t\t\t\tResizeIndexHeap(std::max(GetIndexHeapSize() + index_size * (num_indices + 1), GetIndexHeapSize() * 2));\n\t\t\t\tindex_memory_block = AllocateMemory(m_index_heap_start_block, num_indices*index_size, index_size);\n\t\t\t\tif (index_memory_block == nullptr)\n\t\t\t\t{\n\t\t\t\t\tLOGE(\"Allocating memory for index buffer failed.\");\n\t\t\t\t\tFreeMemory(m_vertex_heap_start_block, vertex_memory_block);\n\t\t\t\t\tdelete mesh;\n\t\t\t\t\treturn nullptr;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Store the offset of the allocated memory from the start of the staging buffer\n\t\tmesh->m_vertex_staging_buffer_offset = SizeAlignAnyAlignment(vertex_memory_block->m_offset, vertex_size) / vertex_size;\n\t\tmesh->m_vertex_staging_buffer_size = num_vertices * vertex_size;\n\t\tmesh->m_vertex_staging_buffer_stride = vertex_size;\n\t\tmesh->m_vertex_count = num_vertices;\n\t\tmesh->m_vertex_memory_block = vertex_memory_block;\n\n\t\tmesh->m_vertex_buffer_base_address = m_vertex_buffer->m_gpu_address;\n\n\t\t//Send the vertex data to the vertex staging buffer\n\t\td3d12::UpdateStagingBuffer(m_vertex_buffer, vertices_data, num_vertices*vertex_size, mesh->m_vertex_staging_buffer_offset * vertex_size);\n\n\t\t//Store the offset of the allocated memory from the start of the staging buffer\n\t\tmesh->m_index_staging_buffer_offset = SizeAlignAnyAlignment(index_memory_block->m_offset, index_size) / index_size;\n\t\tmesh->m_index_staging_buffer_size = num_indices * index_size;\n\t\tmesh->m_index_count = num_indices;\n\t\tmesh->m_index_memory_block = index_memory_block;\n\n\t\tmesh->m_index_buffer_base_address = m_index_buffer->m_gpu_address;\n\n\t\t//Send the index data to the index staging buffer\n\t\td3d12::UpdateStagingBuffer(m_index_buffer, indices_data, num_indices*index_size, mesh->m_index_staging_buffer_offset * index_size);\n\t\t\n\t\tinternal::StageCommand* vertex_command = new internal::StageCommand;\n\t\tvertex_command->m_type = internal::CommandType::STAGE;\n\t\tvertex_command->m_buffer = m_vertex_buffer;\n\t\tvertex_command->m_offset = mesh->m_vertex_staging_buffer_offset * mesh->m_vertex_staging_buffer_stride;\n\t\tvertex_command->m_size = mesh->m_vertex_staging_buffer_size;\n\n\t\tm_command_queue.push(vertex_command);\n\t\t\n\t\tinternal::StageCommand* index_command = new internal::StageCommand;\n\t\tindex_command->m_type = internal::CommandType::STAGE;\n\t\tindex_command->m_buffer = m_index_buffer;\n\t\tindex_command->m_offset = mesh->m_index_staging_buffer_offset * index_size;\n\t\tindex_command->m_size = mesh->m_index_staging_buffer_size;\n\n\t\tm_command_queue.push(index_command);\n\n\t\treturn mesh;\n\t}\n\n\tinternal::MeshInternal* D3D12ModelPool::LoadCustom_VerticesOnly(void* vertices_data, std::size_t num_vertices, std::size_t vertex_size)\n\t{\n\t\tinternal::D3D12MeshInternal* mesh = new internal::D3D12MeshInternal();\n\t\tmemset(mesh, 0, sizeof(internal::D3D12MeshInternal));\n\n\t\t//Allocate vertex buffer memory\n\t\t\n\t\tMemoryBlock* vertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size);\n\n\t\t//The loop has exited, see if we've found enough free pages\n\t\tif (vertex_memory_block == nullptr)\n\t\t{\n\t\t\tDefragmentVertexHeap();\n\t\t\tvertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size);\n\t\t\tif (vertex_memory_block == nullptr)\n\t\t\t{\n\t\t\t\tResizeVertexHeap(std::max(GetVertexHeapSize() + (num_vertices + 1) * vertex_size, GetVertexHeapSize() * 2));\n\t\t\t\tvertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size);\n\t\t\t\tif (vertex_memory_block == nullptr)\n\t\t\t\t{\n\t\t\t\t\t//We haven't found enough pages, so delete the mesh and return a nullptr\n\t\t\t\t\tLOGE(\"Allocating memory for vertex buffer failed.\");\n\t\t\t\t\tdelete mesh;\n\t\t\t\t\treturn nullptr;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t//Store the offset of the allocated memory from the start of the staging buffer\n\t\tmesh->m_vertex_staging_buffer_offset = SizeAlignAnyAlignment(vertex_memory_block->m_offset, vertex_size) / vertex_size;\n\t\tmesh->m_vertex_staging_buffer_size = num_vertices * vertex_size;\n\t\tmesh->m_vertex_staging_buffer_stride = vertex_size;\n\t\tmesh->m_vertex_count = num_vertices;\n\t\tmesh->m_vertex_memory_block = vertex_memory_block;\n\n\t\tmesh->m_vertex_buffer_base_address = m_vertex_buffer->m_gpu_address;\n\n\t\t//Send the vertex data to the vertex staging buffer\n\t\td3d12::UpdateStagingBuffer(m_vertex_buffer, vertices_data, num_vertices*vertex_size, mesh->m_vertex_staging_buffer_offset*vertex_size);\n\n\t\tinternal::StageCommand* vertex_command = new internal::StageCommand;\n\t\tvertex_command->m_type = internal::CommandType::STAGE;\n\t\tvertex_command->m_buffer = m_vertex_buffer;\n\t\tvertex_command->m_offset = mesh->m_vertex_staging_buffer_offset * mesh->m_vertex_staging_buffer_stride;\n\t\tvertex_command->m_size = mesh->m_vertex_staging_buffer_size;\n\n\t\tm_command_queue.push(vertex_command);\n\n\t\treturn mesh;\n\t}\n\n\tvoid D3D12ModelPool::UpdateMeshData(Mesh * mesh, void * vertices_data, std::size_t num_vertices, std::size_t vertex_size, void * indices_data, std::size_t num_indices, std::size_t index_size)\n\t{\n\t\tUpdateMeshVertexData(mesh, vertices_data, num_vertices, vertex_size);\n\t\tUpdateMeshIndexData(mesh, indices_data, num_indices, index_size);\n\t}\n\n\tvoid D3D12ModelPool::UpdateMeshVertexData(Mesh * mesh, void * vertices_data, std::size_t num_vertices, std::size_t vertex_size)\n\t{\n\t\tinternal::D3D12MeshInternal* mesh_data = GetMeshData(mesh->id);\n\t\tif (vertex_size == mesh_data->m_vertex_staging_buffer_stride&&num_vertices == mesh_data->m_vertex_count)\n\t\t{\n\t\t\td3d12::UpdateStagingBuffer(m_vertex_buffer, vertices_data, num_vertices*vertex_size, mesh_data->m_vertex_staging_buffer_offset*vertex_size);\n\n\t\t\tinternal::StageCommand* vertex_command = new internal::StageCommand;\n\t\t\tvertex_command->m_type = internal::STAGE;\n\t\t\tvertex_command->m_buffer = m_vertex_buffer;\n\t\t\tvertex_command->m_offset = mesh_data->m_vertex_staging_buffer_offset*vertex_size;\n\t\t\tvertex_command->m_size = mesh_data->m_vertex_staging_buffer_size;\n\n\t\t\tm_command_queue.push(vertex_command);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (num_vertices*vertex_size <= mesh_data->m_vertex_staging_buffer_size)\n\t\t\t{\n\t\t\t\tLOGW(\"New vertex count is smaller than old vertex count.\");\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGW(\"New vertex count is larger than old vertex count.\");\n\t\t\t}\n\n\t\t\tFreeMemory(m_vertex_heap_start_block, static_cast<MemoryBlock*>(mesh_data->m_vertex_memory_block));\n\n\t\t\tMemoryBlock* new_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size);\n\n\t\t\tif (new_block == nullptr)\n\t\t\t{\n\t\t\t\tDefragmentVertexHeap();\n\t\t\t\tnew_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size);\n\t\t\t\tif (new_block == nullptr)\n\t\t\t\t{\n\t\t\t\t\tResizeVertexHeap(GetVertexHeapSize() + (num_vertices + 1) * vertex_size);\n\t\t\t\t\tnew_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size);\n\t\t\t\t\tif (new_block == nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tLOGE(\"Unable to allocate memory for edited mesh.\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tmesh_data->m_vertex_memory_block = new_block;\n\t\t\tmesh_data->m_vertex_staging_buffer_offset = SizeAlignAnyAlignment(new_block->m_offset, vertex_size) / vertex_size;\n\t\t\tmesh_data->m_vertex_staging_buffer_size = num_vertices * vertex_size;\n\t\t\tmesh_data->m_vertex_staging_buffer_stride = vertex_size;\n\t\t\tmesh_data->m_vertex_count = num_vertices;\n\n\t\t\td3d12::UpdateStagingBuffer(m_vertex_buffer, vertices_data, num_vertices*vertex_size, mesh_data->m_vertex_staging_buffer_offset*vertex_size);\n\n\t\t\tinternal::StageCommand* vertex_command = new internal::StageCommand;\n\t\t\tvertex_command->m_type = internal::STAGE;\n\t\t\tvertex_command->m_buffer = m_vertex_buffer;\n\t\t\tvertex_command->m_offset = mesh_data->m_vertex_staging_buffer_offset*vertex_size;\n\t\t\tvertex_command->m_size = mesh_data->m_vertex_staging_buffer_size;\n\n\t\t\tm_command_queue.push(vertex_command);\n\t\t}\n\n\t\tmesh_data->data_changed = true;\n\t}\n\n\tvoid D3D12ModelPool::UpdateMeshIndexData(Mesh * mesh, void * indices_data, std::size_t num_indices, std::size_t indices_size)\n\t{\n\t\tinternal::D3D12MeshInternal* mesh_data = GetMeshData(mesh->id);\n\t\tif (num_indices == mesh_data->m_index_count)\n\t\t{\n\t\t\td3d12::UpdateStagingBuffer(m_index_buffer, indices_data, num_indices*indices_size, mesh_data->m_index_staging_buffer_offset*indices_size);\n\n\t\t\tinternal::StageCommand* index_command = new internal::StageCommand;\n\t\t\tindex_command->m_type = internal::STAGE;\n\t\t\tindex_command->m_buffer = m_index_buffer;\n\t\t\tindex_command->m_offset = mesh_data->m_index_staging_buffer_offset*indices_size;\n\t\t\tindex_command->m_size = mesh_data->m_index_staging_buffer_size;\n\n\t\t\tm_command_queue.push(index_command);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (num_indices*indices_size <= mesh_data->m_index_staging_buffer_size)\n\t\t\t{\n\t\t\t\tLOGW(\"New index count is smaller than old index count.\");\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGW(\"New index count is larger than old index count.\");\n\t\t\t}\n\n\t\t\tFreeMemory(m_index_heap_start_block, static_cast<MemoryBlock*>(mesh_data->m_index_memory_block));\n\n\t\t\tMemoryBlock* new_block = AllocateMemory(m_index_heap_start_block, num_indices*indices_size, indices_size);\n\n\t\t\tif (new_block == nullptr)\n\t\t\t{\n\t\t\t\tDefragmentIndexHeap();\n\t\t\t\tnew_block = AllocateMemory(m_index_heap_start_block, num_indices*indices_size, indices_size);\n\t\t\t\tif (new_block == nullptr)\n\t\t\t\t{\n\t\t\t\t\tResizeIndexHeap(GetVertexHeapSize() + (num_indices + 1) * indices_size);\n\t\t\t\t\tnew_block = AllocateMemory(m_index_heap_start_block, num_indices*indices_size, indices_size);\n\t\t\t\t\tif (new_block == nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tLOGE(\"Unable to allocate memory for edited mesh.\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tmesh_data->m_index_memory_block = new_block;\n\t\t\tmesh_data->m_index_staging_buffer_offset = SizeAlignAnyAlignment(new_block->m_offset, indices_size) / indices_size;\n\t\t\tmesh_data->m_index_staging_buffer_size = num_indices * indices_size;\n\t\t\tmesh_data->m_index_count = num_indices;\n\n\t\t\td3d12::UpdateStagingBuffer(m_index_buffer, indices_data, num_indices*indices_size, mesh_data->m_index_staging_buffer_offset*indices_size);\n\n\t\t\tinternal::StageCommand* index_command = new internal::StageCommand;\n\t\t\tindex_command->m_type = internal::STAGE;\n\t\t\tindex_command->m_buffer = m_index_buffer;\n\t\t\tindex_command->m_offset = mesh_data->m_index_staging_buffer_offset*indices_size;\n\t\t\tindex_command->m_size = mesh_data->m_index_staging_buffer_size;\n\n\t\t\tm_command_queue.push(index_command);\n\t\t}\n\n\t\tmesh_data->data_changed = true;\n\t}\n\n\tvoid D3D12ModelPool::DestroyModel(Model * model)\n\t{\n\t\tfor (auto& mesh : model->m_meshes)\n\t\t{\n\t\t\tDestroyMesh(m_loaded_meshes[mesh.first->id]);\n\t\t\tdelete mesh.first;\n\t\t}\n\n\t\tstd::vector<Model*>::iterator it = m_loaded_models.begin();\n\t\tfor (; (*it) != model && it != m_loaded_models.end(); ++it);\n\n\t\tif (it != m_loaded_models.end())\n\t\t{\n\t\t\tm_loaded_models.erase(it);\n\t\t}\n\n\t\tif(model->m_owns_materials)\n\t\t{\n\t\t\tfor(auto& mesh : model->m_meshes)\n\t\t\t{\n\t\t\t\tmesh.second.m_pool->DestroyMaterial(mesh.second);\n\t\t\t}\n\t\t}\n\n\t\tdelete model;\n\t}\n\n\tvoid D3D12ModelPool::DestroyMesh(internal::MeshInternal * mesh)\n\t{\n\t\t//Check for null pointers\n\t\tif (mesh == nullptr)\n\t\t{\n\t\t\tLOGW(\"Tried to destroy a mesh that was a nullptr\")\n\t\t\treturn;\n\t\t}\n\t\t\n\t\tstd::map<std::uint64_t, internal::MeshInternal*>::iterator it;\n\t\tfor (it = m_loaded_meshes.begin(); it != m_loaded_meshes.end(); ++it)\n\t\t{\n\t\t\tif (it->second == mesh)\n\t\t\t\tbreak;\n\t\t}\n\n\t\tif (it != m_loaded_meshes.end())\n\t\t{\n\t\t\tFreeID(it->first);\n\n\t\t\tm_loaded_meshes.erase(it);\n\n\t\t\tinternal::D3D12MeshInternal* n_mesh = static_cast<internal::D3D12MeshInternal*>(mesh);\n\n\t\t\tFreeMemory(m_vertex_heap_start_block, static_cast<MemoryBlock*>(n_mesh->m_vertex_memory_block));\n\n\t\t\tif (n_mesh->m_index_memory_block != nullptr)\n\t\t\t{\n\t\t\t\tFreeMemory(m_index_heap_start_block, static_cast<MemoryBlock*>(n_mesh->m_index_memory_block));\n\t\t\t}\n\n\t\t\tm_updated = true;\n\n\t\t\t//Delete the mesh\n\t\t\tdelete mesh;\n\t\t}\n\t}\n\n\tD3D12ModelPool::MemoryBlock * D3D12ModelPool::AllocateMemory(MemoryBlock * start_block, std::size_t size, std::size_t alignment)\n\t{\n\t\twhile (start_block != nullptr) \n\t\t{\n\t\t\tif (start_block->m_size >= size + (start_block->m_offset%alignment == 0 ? 0 : (alignment - start_block->m_offset%alignment)) &&\n\t\t\t\tstart_block->m_free)\n\t\t\t{\n\t\t\t\tstd::size_t needed_size = size + (start_block->m_offset%alignment == 0 ? 0 : (alignment - start_block->m_offset%alignment));\n\n\t\t\t\tif (start_block->m_size == needed_size)\n\t\t\t\t{\n\t\t\t\t\tstart_block->m_free = false;\n\t\t\t\t\tstart_block->m_alignment = alignment;\n\t\t\t\t\treturn start_block;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tMemoryBlock* new_block = new MemoryBlock;\n\t\t\t\t\tnew_block->m_prev_block = start_block;\n\t\t\t\t\tnew_block->m_next_block = start_block->m_next_block;\n\t\t\t\t\tstart_block->m_next_block = new_block;\n\t\t\t\t\tnew_block->m_free = true;\n\t\t\t\t\tnew_block->m_size = start_block->m_size - needed_size;\n\t\t\t\t\tnew_block->m_offset = start_block->m_offset + needed_size;\n\n\t\t\t\t\tif (new_block->m_next_block != nullptr) \n\t\t\t\t\t{\n\t\t\t\t\t\tnew_block->m_next_block->m_prev_block = new_block;\n\t\t\t\t\t}\n\n\t\t\t\t\tstart_block->m_free = false;\n\t\t\t\t\tstart_block->m_size = needed_size;\n\t\t\t\t\tstart_block->m_alignment = alignment;\n\t\t\t\t\treturn start_block;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tstart_block = start_block->m_next_block;\n\t\t\t}\n\t\t}\n\t\treturn nullptr;\n\t}\n\n\tvoid D3D12ModelPool::FreeMemory(MemoryBlock * heap_start_block, MemoryBlock * block)\n\t{\n\t\tMemoryBlock* heap_block = heap_start_block;\n\t\twhile (heap_block != nullptr)\n\t\t{\n\t\t\tif (heap_block == block) \n\t\t\t{\n\t\t\t\tif (heap_block->m_prev_block != nullptr)\n\t\t\t\t{\n\t\t\t\t\tif (heap_block->m_prev_block->m_free)\n\t\t\t\t\t{\n\t\t\t\t\t\theap_block->m_prev_block->m_size += heap_block->m_size;\n\t\t\t\t\t\tif (heap_block->m_next_block != nullptr)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif (heap_block->m_next_block->m_free)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\theap_block->m_prev_block->m_size += heap_block->m_next_block->m_size;\n\t\t\t\t\t\t\t\theap_block->m_prev_block->m_next_block = heap_block->m_next_block->m_next_block;\n\t\t\t\t\t\t\t\tif (heap_block->m_prev_block->m_next_block != nullptr)\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\theap_block->m_prev_block->m_next_block->m_prev_block = heap_block->m_prev_block;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tdelete heap_block->m_next_block;\n\t\t\t\t\t\t\t\tdelete heap_block;\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\theap_block->m_next_block->m_prev_block = heap_block->m_prev_block;\n\t\t\t\t\t\t\t\theap_block->m_prev_block->m_next_block = heap_block->m_next_block;\n\n\t\t\t\t\t\t\t\tdelete heap_block;\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\theap_block->m_prev_block->m_next_block = heap_block->m_next_block;\n\t\t\t\t\t\t\tdelete heap_block;\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tif (heap_block->m_next_block != nullptr)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif (heap_block->m_next_block->m_free)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tMemoryBlock* temp = heap_block->m_next_block;\n\t\t\t\t\t\t\t\theap_block->m_size += heap_block->m_next_block->m_size;\n\t\t\t\t\t\t\t\theap_block->m_next_block = heap_block->m_next_block->m_next_block;\n\t\t\t\t\t\t\t\tif (heap_block->m_next_block != nullptr)\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\theap_block->m_next_block->m_prev_block = heap_block;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tdelete temp;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\theap_block->m_free = true;\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\theap_block->m_free = true;\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (heap_block->m_next_block != nullptr)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (heap_block->m_next_block->m_free)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tMemoryBlock* temp = heap_block->m_next_block;\n\t\t\t\t\t\t\theap_block->m_size += heap_block->m_next_block->m_size;\n\t\t\t\t\t\t\theap_block->m_next_block = heap_block->m_next_block->m_next_block;\n\t\t\t\t\t\t\tif (heap_block->m_next_block != nullptr)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\theap_block->m_next_block->m_prev_block = heap_block;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tdelete temp;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\theap_block->m_free = true;\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\theap_block->m_free = true;\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\theap_block = heap_block->m_next_block;\n\t\t\t}\n\t\t}\n\t}\n\n\tsize_t D3D12ModelPool::GetVertexHeapOccupiedSpace()\n\t{\n\t\tMemoryBlock* mem_block = m_vertex_heap_start_block;\n\t\tsize_t size = 0;\n\t\twhile (mem_block != nullptr)\n\t\t{\n\t\t\tif (!mem_block->m_free)\n\t\t\t{\n\t\t\t\tsize += mem_block->m_size;\n\t\t\t}\n\t\t\tmem_block = mem_block->m_next_block;\n\t\t}\n\t\treturn size;\n\t}\n\n\tsize_t D3D12ModelPool::GetIndexHeapOccupiedSpace()\n\t{\n\t\tMemoryBlock* mem_block = m_index_heap_start_block;\n\t\tsize_t size = 0;\n\t\twhile (mem_block != nullptr)\n\t\t{\n\t\t\tif (!mem_block->m_free)\n\t\t\t{\n\t\t\t\tsize += mem_block->m_size;\n\t\t\t}\n\t\t\tmem_block = mem_block->m_next_block;\n\t\t}\n\t\treturn size;\n\t}\n\n\tsize_t D3D12ModelPool::GetVertexHeapFreeSpace()\n\t{\n\t\tMemoryBlock* mem_block = m_vertex_heap_start_block;\n\t\tsize_t size = 0;\n\t\twhile (mem_block != nullptr)\n\t\t{\n\t\t\tif (mem_block->m_free)\n\t\t\t{\n\t\t\t\tsize += mem_block->m_size;\n\t\t\t}\n\t\t\tmem_block = mem_block->m_next_block;\n\t\t}\n\t\treturn size;\n\t}\n\n\tsize_t D3D12ModelPool::GetIndexHeapFreeSpace()\n\t{\n\t\tMemoryBlock* mem_block = m_index_heap_start_block;\n\t\tsize_t size = 0;\n\t\twhile (mem_block != nullptr)\n\t\t{\n\t\t\tif (mem_block->m_free)\n\t\t\t{\n\t\t\t\tsize += mem_block->m_size;\n\t\t\t}\n\t\t\tmem_block = mem_block->m_next_block;\n\t\t}\n\t\treturn size;\n\t}\n\n\tsize_t D3D12ModelPool::GetVertexHeapSize()\n\t{\n\t\tMemoryBlock* mem_block = m_vertex_heap_start_block;\n\t\tsize_t size = 0;\n\t\twhile (mem_block != nullptr)\n\t\t{\n\t\t\tsize += mem_block->m_size;\n\t\t\tmem_block = mem_block->m_next_block;\n\t\t}\n\t\treturn size;\n\t}\n\n\tsize_t D3D12ModelPool::GetIndexHeapSize()\n\t{\n\t\tMemoryBlock* mem_block = m_index_heap_start_block;\n\t\tsize_t size = 0;\n\t\twhile (mem_block != nullptr)\n\t\t{\n\t\t\tsize += mem_block->m_size;\n\t\t\tmem_block = mem_block->m_next_block;\n\t\t}\n\t\treturn size;\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/d3d12/d3d12_model_pool.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../model_pool.hpp\"\n#include \"d3d12_structs.hpp\"\n\n#include <queue>\n\nnamespace wr::d3d12\n{\n\tstruct HeapResource;\n\tstruct StagingBuffer;\n}\n\nnamespace wr\n{\n\n\tclass D3D12RenderSystem;\n\n\tnamespace internal\n\t{\n\t\tstruct D3D12MeshInternal : MeshInternal\n\t\t{\n\t\t\tD3D12_GPU_VIRTUAL_ADDRESS m_vertex_buffer_base_address;\n\t\t\tstd::size_t m_vertex_staging_buffer_offset;\n\t\t\tstd::size_t m_vertex_staging_buffer_size;\n\t\t\tstd::size_t m_vertex_staging_buffer_stride;\n\t\t\tstd::size_t m_vertex_count;\n\t\t\tvoid* m_vertex_memory_block;\n\t\t\tD3D12_GPU_VIRTUAL_ADDRESS m_index_buffer_base_address;\n\t\t\tstd::size_t m_index_staging_buffer_offset;\n\t\t\tstd::size_t m_index_count_offset;\n\t\t\tstd::size_t m_index_staging_buffer_size;\n\t\t\tstd::size_t m_index_count;\n\t\t\tvoid* m_index_memory_block;\n\n\t\t\tbool data_changed;\n\t\t};\n\n\t\tenum CommandType\n\t\t{\n\t\t\tSTAGE,\n\t\t\tCOPY,\n\t\t\tREAD,\n\t\t\tTRANSITION,\n\t\t};\n\n\t\tstruct Command\n\t\t{\n\t\t\tCommandType m_type;\n\t\t};\n\n\t\tstruct StageCommand : Command\n\t\t{\n\t\t\td3d12::StagingBuffer* m_buffer;\n\t\t\tstd::size_t m_size;\n\t\t\tstd::size_t m_offset;\n\t\t};\n\n\t\tstruct CopyCommand : Command\n\t\t{\n\t\t\tID3D12Resource* m_source;\n\t\t\tID3D12Resource* m_dest;\n\t\t\tstd::size_t m_size;\n\t\t\tstd::size_t m_source_offset;\n\t\t\tstd::size_t m_dest_offset;\n\t\t};\n\n\t\tstruct ReadCommand : Command\n\t\t{\n\t\t\td3d12::StagingBuffer* m_buffer;\n\t\t\tstd::size_t m_size;\n\t\t\tstd::size_t m_offset;\n\t\t};\n\n\t\tstruct TransitionCommand : Command\n\t\t{\n\t\t\tID3D12Resource* m_buffer;\n\t\t\tResourceState m_old_state;\n\t\t\tResourceState m_new_state;\n\t\t};\n\t}\n\n\tclass D3D12ModelPool : public ModelPool\n\t{\n\tpublic:\n\t\texplicit D3D12ModelPool(D3D12RenderSystem& render_system,\n\t\t\tstd::size_t vertex_buffer_pool_size_in_mb,\n\t\t\tstd::size_t index_buffer_pool_size_in_mb);\n\t\t~D3D12ModelPool() final;\n\n\t\tvoid Evict() final;\n\t\tvoid MakeResident() final;\n\n\t\tvoid StageMeshes(d3d12::CommandList* cmd_list);\n\n\t\td3d12::StagingBuffer* GetVertexStagingBuffer();\n\t\td3d12::StagingBuffer* GetIndexStagingBuffer();\n\n\t\tinternal::D3D12MeshInternal* GetMeshData(std::uint64_t mesh_handle);\n\n\t\t// Shrinks down both heaps to the minimum size required. \n\t\t// Does not rearrange the contents of the heaps, meaning that it doesn't shrink to the absolute minimum size.\n\t\t// To do that, call Defragment first.\n\t\tvoid ShrinkToFit() final;\n\t\tvoid ShrinkVertexHeapToFit() final;\n\t\tvoid ShrinkIndexHeapToFit() final;\n\n\t\t// Removes any holes in the memory, stitching all allocations back together to maximize the amount of contiguous free space.\n\t\t// These functions are called automatically if the allocator has enough free space but no large enough free blocks.\n\t\tvoid Defragment() final;\n\t\tvoid DefragmentVertexHeap() final;\n\t\tvoid DefragmentIndexHeap() final;\n\n\t\tsize_t GetVertexHeapOccupiedSpace() final;\n\t\tsize_t GetIndexHeapOccupiedSpace() final;\n\n\t\tsize_t GetVertexHeapFreeSpace() final;\n\t\tsize_t GetIndexHeapFreeSpace() final;\n\n\t\tsize_t GetVertexHeapSize() final;\n\t\tsize_t GetIndexHeapSize() final;\n\n\t\t// Resizes both heaps to the supplied sizes. \n\t\t// If the supplied size is smaller than the required size the heaps will resize to the required size instead.\n\t\tvoid Resize(size_t vertex_heap_new_size, size_t index_heap_new_size) final;\n\t\tvoid ResizeVertexHeap(size_t vertex_heap_new_size) final;\n\t\tvoid ResizeIndexHeap(size_t index_heap_new_size) final;\n\n\t\t// Returns if the model pool data has been changed. Use this to see if additional data needs to be altered.\n\t\tbool IsUpdated() { return m_updated; };\n\n\t\tvoid SetUpdated(bool updated) { m_updated = updated; };\n\n\t\tvoid MakeSpaceForModel(size_t vertex_size, size_t index_size) final;\n\n\tprivate:\n\t\tinternal::MeshInternal* LoadCustom_VerticesAndIndices(void* vertices_data, std::size_t num_vertices, std::size_t vertex_size, void* indices_data, std::size_t num_indices, std::size_t index_size) final;\n\t\tinternal::MeshInternal* LoadCustom_VerticesOnly(void* vertices_data, std::size_t num_vertices, std::size_t vertex_size) final;\n\n\t\tvirtual void UpdateMeshData(Mesh* mesh, void* vertices_data, std::size_t num_vertices, std::size_t vertex_size, void* indices_data, std::size_t num_indices, std::size_t index_size) final;\n\n\t\tvoid UpdateMeshVertexData(Mesh* mesh, void* vertices_data, std::size_t num_vertices, std::size_t vertex_size);\n\t\tvoid UpdateMeshIndexData(Mesh* mesh, void* indices_data, std::size_t num_indices, std::size_t indices_size);\n\n\t\tvoid DestroyModel(Model* model) final;\n\t\tvoid DestroyMesh(internal::MeshInternal* mesh) final;\n\n\t\td3d12::StagingBuffer* m_vertex_buffer;\n\t\td3d12::StagingBuffer* m_index_buffer;\n\n\t\tstruct MemoryBlock\n\t\t{\n\t\t\tMemoryBlock* m_prev_block;\n\t\t\tMemoryBlock* m_next_block;\n\t\t\tstd::size_t m_size;\n\t\t\tstd::size_t m_offset;\n\t\t\tstd::size_t m_alignment;\n\t\t\tbool m_free;\n\t\t};\n\n\t\tMemoryBlock* m_vertex_heap_start_block;\n\t\tMemoryBlock* m_index_heap_start_block;\n\n\t\tMemoryBlock* AllocateMemory(MemoryBlock* start_block, std::size_t size, std::size_t alignment);\n\t\tvoid FreeMemory(MemoryBlock* heap_start_block,  MemoryBlock* block);\n\t\t\n\t\tstd::queue<internal::Command*> m_command_queue;\n\n\t\tID3D12Resource* m_intermediate_buffer;\n\t\tstd::size_t m_intermediate_size;\n\n\t\tstd::uint64_t m_vertex_buffer_size;\n\t\tstd::uint64_t m_index_buffer_size;\n\n\t\tbool m_updated;\n\n\t\tD3D12RenderSystem& m_render_system;\n\t};\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_pipeline_state.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include <variant>\n\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tnamespace internal\n\t{\n\n\t\t[[nodiscard]] D3D12_GRAPHICS_PIPELINE_STATE_DESC GetGraphicsPipelineStateDescriptor(desc::PipelineStateDesc descriptor,\n\t\t\tstd::vector<D3D12_INPUT_ELEMENT_DESC> const & input_layout,\n\t\t\tRootSignature* root_signature,\n\t\t\tShader* vertex_shader,\n\t\t\tShader* pixel_shader)\n\t\t{\n\t\t\tD3D12_BLEND_DESC blend_desc = CD3DX12_BLEND_DESC(D3D12_DEFAULT);\n\n\t\t\tD3D12_DEPTH_STENCIL_DESC depth_stencil_state = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);\n\t\t\tif (descriptor.m_dsv_format == Format::UNKNOWN)\n\t\t\t{\n\t\t\t\tdepth_stencil_state.DepthEnable = false;\n\t\t\t\tdepth_stencil_state.StencilEnable = false;\n\t\t\t}\n\n\t\t\tD3D12_RASTERIZER_DESC rasterize_desc = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);\n\t\t\trasterize_desc.FrontCounterClockwise = descriptor.m_counter_clockwise;\n\t\t\tdepth_stencil_state.DepthFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL;\n\t\t\trasterize_desc.CullMode = (D3D12_CULL_MODE)descriptor.m_cull_mode;\n\n\t\t\tDXGI_SAMPLE_DESC sample_desc = { 1, 0 };\n\n\t\t\tD3D12_INPUT_LAYOUT_DESC input_layout_desc = {};\n\t\t\tinput_layout_desc.NumElements = static_cast<std::uint32_t>(input_layout.size());\n\t\t\tinput_layout_desc.pInputElementDescs = input_layout.data();\n\n\t\t\tD3D12_SHADER_BYTECODE vs_bytecode = {};\n\t\t\tvs_bytecode.BytecodeLength = vertex_shader->m_native->GetBufferSize();\n\t\t\tvs_bytecode.pShaderBytecode = vertex_shader->m_native->GetBufferPointer();\n\n\t\t\tD3D12_SHADER_BYTECODE ps_bytecode = {};\n\t\t\tps_bytecode.BytecodeLength = pixel_shader->m_native->GetBufferSize();\n\t\t\tps_bytecode.pShaderBytecode = pixel_shader->m_native->GetBufferPointer();\n\n\t\t\tD3D12_GRAPHICS_PIPELINE_STATE_DESC pso_desc = {};\n\t\t\tpso_desc.PrimitiveTopologyType = (D3D12_PRIMITIVE_TOPOLOGY_TYPE)descriptor.m_topology_type;\n\t\t\tfor (auto i = 0u; i < descriptor.m_num_rtv_formats; i++)\n\t\t\t{ // FIXME: memcpy\n\t\t\t\tpso_desc.RTVFormats[i] = (DXGI_FORMAT)descriptor.m_rtv_formats[i];\n\t\t\t}\n\t\t\tpso_desc.DSVFormat = (DXGI_FORMAT)descriptor.m_dsv_format;\n\t\t\tpso_desc.SampleDesc = sample_desc;\n\t\t\tpso_desc.SampleMask = 0xffffffff;\n\t\t\tpso_desc.RasterizerState = rasterize_desc;\n\t\t\tpso_desc.BlendState = blend_desc;\n\t\t\tpso_desc.DepthStencilState = depth_stencil_state;\n\t\t\tpso_desc.NumRenderTargets = descriptor.m_num_rtv_formats;\n\t\t\tpso_desc.pRootSignature = root_signature->m_native;\n\t\t\tpso_desc.VS = vs_bytecode;\n\t\t\tpso_desc.PS = ps_bytecode;\n\t\t\tpso_desc.InputLayout = input_layout_desc;\n\n\t\t\treturn pso_desc;\n\t\t}\n\n\t\t[[nodiscard]] D3D12_COMPUTE_PIPELINE_STATE_DESC GetComputePipelineStateDescriptor(RootSignature* root_signature, Shader* compute_shader)\n\t\t{\n\t\t\tD3D12_SHADER_BYTECODE cs_bytecode = {};\n\t\t\tcs_bytecode.BytecodeLength = compute_shader->m_native->GetBufferSize();\n\t\t\tcs_bytecode.pShaderBytecode = compute_shader->m_native->GetBufferPointer();\n\n\t\t\tD3D12_COMPUTE_PIPELINE_STATE_DESC pso_desc = {};\n\t\t\tpso_desc.pRootSignature = root_signature->m_native;\n\t\t\tpso_desc.CS = cs_bytecode;\n\n\t\t\treturn pso_desc;\n\t\t}\n\n\t} /* internal */\n\n\tPipelineState* CreatePipelineState()\n\t{\n\t\treturn new PipelineState();\n\t}\n\n\tvoid SetName(PipelineState * pipeline_state, std::wstring name)\n\t{\n\t\tpipeline_state->m_native->SetName(name.c_str());\n\t}\n\n\tvoid SetVertexShader(PipelineState* pipeline_state, Shader* shader)\n\t{\n\t\tpipeline_state->m_vertex_shader = shader;\n\t}\n\n\tvoid SetFragmentShader(PipelineState* pipeline_state, Shader* shader)\n\t{\n\t\tpipeline_state->m_pixel_shader = shader;\n\t}\n\n\tvoid SetComputeShader(PipelineState* pipeline_state, Shader* shader)\n\t{\n\t\tpipeline_state->m_compute_shader = shader;\n\t}\n\n\tvoid SetRootSignature(PipelineState* pipeline_state, RootSignature* root_signature)\n\t{\n\t\tpipeline_state->m_root_signature = root_signature;\n\t}\n\n\tvoid FinalizePipeline(PipelineState* pipeline_state, Device* device, desc::PipelineStateDesc desc)\n\t{\n\t\tauto n_device = device->m_native;\n\n\t\tpipeline_state->m_device = device;\n\t\tpipeline_state->m_desc = desc;\n\n\t\tstd::variant<D3D12_GRAPHICS_PIPELINE_STATE_DESC, D3D12_COMPUTE_PIPELINE_STATE_DESC> pso_desc;\n\t\tswitch (desc.m_type)\n\t\t{\n\t\tcase PipelineType::GRAPHICS_PIPELINE:\n\t\t{\n\t\t\tpso_desc = internal::GetGraphicsPipelineStateDescriptor(desc,\n\t\t\t\tdesc.m_input_layout,\n\t\t\t\tpipeline_state->m_root_signature,\n\t\t\t\tpipeline_state->m_vertex_shader,\n\t\t\t\tpipeline_state->m_pixel_shader);\n\t\t}\n\t\tbreak;\n\t\tcase PipelineType::COMPUTE_PIPELINE:\n\t\t{\n\t\t\tpso_desc = internal::GetComputePipelineStateDescriptor(pipeline_state->m_root_signature,\n\t\t\t\tpipeline_state->m_compute_shader);\n\t\t}\n\t\tbreak;\n\t\t}\n\n\t\tHRESULT hr = E_FAIL;\n\t\tif (std::holds_alternative<D3D12_GRAPHICS_PIPELINE_STATE_DESC>(pso_desc))\n\t\t{\n\t\t\thr = n_device->CreateGraphicsPipelineState(&std::get<D3D12_GRAPHICS_PIPELINE_STATE_DESC>(pso_desc), IID_PPV_ARGS(&pipeline_state->m_native));\n\t\t}\n\t\telse if (std::holds_alternative<D3D12_COMPUTE_PIPELINE_STATE_DESC>(pso_desc))\n\t\t{\n\t\t\thr = n_device->CreateComputePipelineState(&std::get<D3D12_COMPUTE_PIPELINE_STATE_DESC>(pso_desc), IID_PPV_ARGS(&pipeline_state->m_native));\n\t\t}\n\t\telse\n\t\t{\n\t\t\tLOGC(\"Variant seems to be empty...\");\n\t\t}\n\t\tif (FAILED(hr))\n\t\t{\n\t\t\tLOGC(\"Failed to create graphics pipeline\");\n\t\t}\n\t\tNAME_D3D12RESOURCE(pipeline_state->m_native)\n\t}\n\n\tvoid RefinalizePipeline(PipelineState* pipeline_state)\n\t{\n\t\tFinalizePipeline(pipeline_state, pipeline_state->m_device, pipeline_state->m_desc);\n\t}\n\n\tvoid Destroy(PipelineState* pipeline_state)\n\t{\n\t\tSAFE_RELEASE(pipeline_state->m_native);\n\t\tdelete pipeline_state;\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_readback_buffer.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"d3d12_defines.hpp\"\n#include \"d3dx12.hpp\"\n#include \"../util/log.hpp\"\n\nnamespace wr::d3d12\n{\n\tReadbackBufferResource* wr::d3d12::CreateReadbackBuffer(Device* device, std::uint32_t aligned_buffer_size)\n\t{\n\t\tauto native_device = device->m_native;\n\n\t\tauto* readbackBuffer = new ReadbackBufferResource();\n\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_readback = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK);\n\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_buffer_size);\n\n\t\tHRESULT res = native_device->CreateCommittedResource(\n\t\t\t&heap_properties_readback,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&buffer_desc,\n\t\t\tD3D12_RESOURCE_STATE_COPY_DEST,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&readbackBuffer->m_resource));\n\n\t\tif (FAILED(res))\n\t\t{\n\t\t\tLOGC(\"Error: Could not create a readback buffer!\");\n\t\t}\n\n\t\treturn readbackBuffer;\n\t}\n\n\tvoid* MapReadbackBuffer(ReadbackBufferResource* const readback_buffer, std::uint32_t buffer_size)\n\t{\n\t\tif (!readback_buffer)\n\t\t\treturn nullptr;\n\n\t\tvoid* memory = nullptr;\n\t\tCD3DX12_RANGE buffer_range = CD3DX12_RANGE(0, buffer_size);\n\n\t\treadback_buffer->m_resource->Map(0, &buffer_range, &memory);\n\n\t\treturn memory;\n\t}\n\n\tvoid UnmapReadbackBuffer(ReadbackBufferResource* const readback_buffer)\n\t{\n\t\tif (!readback_buffer)\n\t\t\treturn;\n\n\t\tCD3DX12_RANGE buffer_range = CD3DX12_RANGE(0, 0);\n\t\treadback_buffer->m_resource->Unmap(0, &buffer_range);\n\t}\n\n\tvoid SetName(ReadbackBufferResource* readback_buffer, std::wstring name)\n\t{\n\t\treadback_buffer->m_resource->SetName(name.c_str());\n\t}\n\n\tvoid Destroy(ReadbackBufferResource* readback_buffer)\n\t{\n\t\tSAFE_RELEASE(readback_buffer->m_resource);\n\t\tdelete readback_buffer;\n\t}\n\n} /* wr::d3d12 */\n"
  },
  {
    "path": "src/d3d12/d3d12_render_target.cpp",
    "content": "/*!\r\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\r\n *\r\n * Licensed under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n *     http://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an \"AS IS\" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n */\r\n#include \"d3d12_functions.hpp\"\r\n\r\n#include \"../util/log.hpp\"\r\n#include \"d3d12_defines.hpp\"\r\n#include \"d3dx12.hpp\"\r\n\r\nnamespace wr::d3d12\r\n{\r\n\t\r\n\tRenderTarget* CreateRenderTarget(Device* device, unsigned int width, unsigned int height, desc::RenderTargetDesc descriptor)\r\n\t{\r\n\t\tauto render_target = new RenderTarget();\r\n\t\tconst auto n_device = device->m_native;\r\n\r\n\t\trender_target->m_render_targets.resize(descriptor.m_num_rtv_formats);\r\n\t\trender_target->m_create_info = descriptor;\r\n\t\trender_target->m_num_render_targets = descriptor.m_num_rtv_formats;\r\n\t\trender_target->m_width = width;\r\n\t\trender_target->m_height = height;\r\n\r\n\t\tfor (auto i = 0u; i < descriptor.m_num_rtv_formats; i++)\r\n\t\t{\r\n\t\t\tCD3DX12_RESOURCE_DESC resource_desc = CD3DX12_RESOURCE_DESC::Tex2D((DXGI_FORMAT)descriptor.m_rtv_formats[i], \r\n\t\t\t\twidth, \r\n\t\t\t\theight, \r\n\t\t\t\t1, \r\n\t\t\t\t1, \r\n\t\t\t\t1,\r\n\t\t\t\t0, \r\n\t\t\t\tD3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);\r\n\r\n\t\t\tD3D12_CLEAR_VALUE optimized_clear_value = {\r\n\t\t\t\t(DXGI_FORMAT)descriptor.m_rtv_formats[i],\r\n\t\t\t\tdescriptor.m_clear_color[0],\r\n\t\t\t\tdescriptor.m_clear_color[1],\r\n\t\t\t\tdescriptor.m_clear_color[2],\r\n\t\t\t\tdescriptor.m_clear_color[3]\r\n\t\t\t};\r\n\r\n\t\t\t// Create default heap\r\n\t\t\tD3D12_RESOURCE_STATES state = (D3D12_RESOURCE_STATES)descriptor.m_initial_state;\r\n\r\n\t\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\r\n\r\n\t\t\tTRY_M(n_device->CreateCommittedResource(\r\n\t\t\t\t&heap_properties_default,\r\n\t\t\t\tD3D12_HEAP_FLAG_NONE,\r\n\t\t\t\t&resource_desc,\r\n\t\t\t\tstate,\r\n\t\t\t\t&optimized_clear_value, // optimizes draw call\r\n\t\t\t\tIID_PPV_ARGS(&render_target->m_render_targets[i])\r\n\t\t\t), \"Failed to create render target.\");\r\n\r\n\t\t\tNAME_D3D12RESOURCE(render_target->m_render_targets[i], L\"Unnamed Render Target (\" + std::to_wstring(i).c_str() + L\")\");\r\n\t\t\tUINT64 textureUploadBufferSize;\r\n\t\t\tn_device->GetCopyableFootprints(&resource_desc, 0, 1, 0, nullptr, nullptr, nullptr, &textureUploadBufferSize);\r\n\t\t}\r\n\r\n\t\tCreateRenderTargetViews(render_target, device);\r\n\r\n\t\tif (descriptor.m_create_dsv_buffer)\r\n\t\t{\r\n\t\t\tCreateDepthStencilBuffer(render_target, device, width, height);\r\n\t\t}\r\n\r\n\t\treturn render_target;\r\n\t}\r\n\r\n\tvoid SetName(RenderTarget* render_target, std::wstring name)\r\n\t{\r\n\t\tfor (auto i = 0u; i < render_target->m_num_render_targets; i++)\r\n\t\t{\r\n\t\t\trender_target->m_render_targets[i]->SetName((name + std::to_wstring(i)).c_str());\r\n\t\t}\r\n\t}\r\n\r\n\tvoid SetName(RenderTarget* render_target, std::string name)\r\n\t{\r\n\t\tSetName(render_target, std::wstring(name.begin(), name.end()));\r\n\t}\r\n\r\n\tunsigned int GetRenderTargetWidth(RenderTarget* render_target)\r\n\t{\r\n\t\treturn render_target->m_width;\r\n\t}\r\n\r\n\tunsigned int GetRenderTargetHeight(RenderTarget* render_target)\r\n\t{\r\n\t\treturn render_target->m_height;\r\n\t}\r\n\r\n\tvoid CreateRenderTargetViews(RenderTarget* render_target, Device* device)\r\n\t{\r\n\t\tconst auto n_device = device->m_native;\r\n\r\n\t\t// Create views\r\n\t\tD3D12_DESCRIPTOR_HEAP_DESC back_buffer_heap_desc = {};\r\n\t\tback_buffer_heap_desc.NumDescriptors = render_target->m_num_render_targets;\r\n\t\tback_buffer_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;\r\n\t\tback_buffer_heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;\r\n\r\n\t\tTRY_M(n_device->CreateDescriptorHeap(&back_buffer_heap_desc, IID_PPV_ARGS(&render_target->m_rtv_descriptor_heap)),\r\n\t\t\t\"Failed to create descriptor heap.\");\r\n\r\n\t\trender_target->m_rtv_descriptor_increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);\r\n\r\n\t\t// Create render target view with the handle to the heap descriptor.\r\n\t\trender_target->m_render_targets.resize(render_target->m_num_render_targets);\r\n\r\n\t\tCD3DX12_CPU_DESCRIPTOR_HANDLE rtv_handle(render_target->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart());\r\n\t\tfor (auto& rt : render_target->m_render_targets)\r\n\t\t{\r\n\t\t\tn_device->CreateRenderTargetView(rt, nullptr, rtv_handle);\r\n\t\t\trtv_handle.Offset(1, render_target->m_rtv_descriptor_increment_size);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid CreateDepthStencilBuffer(RenderTarget* render_target, Device* device, unsigned int width, unsigned int height)\r\n\t{\r\n\t\tconst auto n_device = device->m_native;\r\n\t\tauto depth_format = DXGI_FORMAT_R32_TYPELESS;\r\n\r\n\t\t// TODO: Seperate the descriptor heap because that one might not need to be recreated when resizing.\r\n\t\t// create a depth stencil descriptor heap so we can get a pointer to the depth stencil buffer\r\n\t\tD3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {};\r\n\t\tdsvHeapDesc.NumDescriptors = 1;\r\n\t\tdsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;\r\n\t\tdsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;\r\n\t\tTRY_M(n_device->CreateDescriptorHeap(&dsvHeapDesc, IID_PPV_ARGS(&render_target->m_depth_stencil_resource_heap)),\r\n\t\t\t\"Failed to create descriptor heap for depth buffer\");\r\n\t\tNAME_D3D12RESOURCE(render_target->m_depth_stencil_resource_heap)\r\n\r\n\t\tD3D12_DEPTH_STENCIL_VIEW_DESC depthStencilDesc = {};\r\n\t\tdepthStencilDesc.Format = DXGI_FORMAT_D32_FLOAT;\r\n\t\tdepthStencilDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;\r\n\t\tdepthStencilDesc.Flags = D3D12_DSV_FLAG_NONE;\r\n\r\n\t\tD3D12_CLEAR_VALUE depthOptimizedClearValue = {};\r\n\t\tdepthOptimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT;\r\n\t\tdepthOptimizedClearValue.DepthStencil.Depth = 1.0f;\r\n\t\tdepthOptimizedClearValue.DepthStencil.Stencil = 0;\r\n\r\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\r\n\t\tCD3DX12_RESOURCE_DESC tex_desc = CD3DX12_RESOURCE_DESC::Tex2D(depth_format, width, height, 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);\r\n\r\n\t\tTRY_M(n_device->CreateCommittedResource(\r\n\t\t\t&heap_properties_default,\r\n\t\t\tD3D12_HEAP_FLAG_NONE,\r\n\t\t\t&tex_desc,\r\n\t\t\tD3D12_RESOURCE_STATE_DEPTH_WRITE,\r\n\t\t\t&depthOptimizedClearValue,\r\n\t\t\tIID_PPV_ARGS(&render_target->m_depth_stencil_buffer)\r\n\t\t), \"Failed to create commited resource.\");\r\n\t\tNAME_D3D12RESOURCE(render_target->m_depth_stencil_buffer)\r\n\r\n\t\tn_device->CreateDepthStencilView(render_target->m_depth_stencil_buffer, &depthStencilDesc, render_target->m_depth_stencil_resource_heap->GetCPUDescriptorHandleForHeapStart());\r\n\t}\r\n\r\n\tvoid CreateSRVFromDSV(RenderTarget* render_target, DescHeapCPUHandle& handle)\r\n\t{\r\n\t\tdecltype(Device::m_native) n_device;\r\n\t\trender_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device));\r\n\r\n\t\tunsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);\r\n\r\n\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\r\n\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\r\n\t\tsrv_desc.Format = DXGI_FORMAT_R32_FLOAT;\r\n\t\tsrv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;\r\n\t\tsrv_desc.Texture2D.MipLevels = 1;\r\n\r\n\t\tn_device->CreateShaderResourceView(render_target->m_depth_stencil_buffer, &srv_desc, handle.m_native);\r\n\t\tOffset(handle, 1, increment_size);\r\n\t}\r\n\r\n\tvoid CreateSRVFromRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int num, Format formats[8])\r\n\t{\r\n\t\tdecltype(Device::m_native) n_device;\r\n\t\trender_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device));\r\n\r\n\t\tunsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);\r\n\r\n\t\tfor (unsigned int i = 0; i < num; i++)\r\n\t\t{\r\n\t\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\r\n\t\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\r\n\t\t\tsrv_desc.Format = (DXGI_FORMAT)formats[i];\r\n\t\t\tsrv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;\r\n\t\t\tsrv_desc.Texture2D.MipLevels = 1;\r\n\r\n\t\t\tn_device->CreateShaderResourceView(render_target->m_render_targets[i], &srv_desc, handle.m_native);\r\n\t\t\tOffset(handle, 1, increment_size);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid CreateUAVFromRTV(RenderTarget* render_target, DescHeapCPUHandle & handle, unsigned int num, Format formats[8])\r\n\t{\r\n\t\tdecltype(Device::m_native) n_device;\r\n\t\trender_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device));\r\n\r\n\t\tunsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);\r\n\r\n\t\tfor (unsigned int i = 0; i < num; i++)\r\n\t\t{\r\n\t\t\tD3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {};\r\n\t\t\tuav_desc.Format = (DXGI_FORMAT)formats[i];\r\n\t\t\tuav_desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;\r\n\t\t\tuav_desc.Texture2D.MipSlice = 0;\r\n\r\n\t\t\tn_device->CreateUnorderedAccessView(render_target->m_render_targets[i], nullptr, &uav_desc, handle.m_native);\r\n\t\t\tOffset(handle, 1, increment_size);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid CreateSRVFromSpecificRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int id, Format format)\r\n\t{\r\n\t\tdecltype(Device::m_native) n_device;\r\n\t\trender_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device));\r\n\r\n\t\tunsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);\r\n\r\n\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\r\n\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\r\n\t\tsrv_desc.Format = (DXGI_FORMAT)format;\r\n\t\tsrv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;\r\n\t\tsrv_desc.Texture2D.MipLevels = 1;\r\n\r\n\t\tn_device->CreateShaderResourceView(render_target->m_render_targets[id], &srv_desc, handle.m_native);\r\n\t\tOffset(handle, 1, increment_size);\r\n\t}\r\n\r\n\tvoid CreateUAVFromSpecificRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int id, Format format)\r\n\t{\r\n\t\tdecltype(Device::m_native) n_device;\r\n\t\trender_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device));\r\n\r\n\t\tunsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);\r\n\r\n\t\tD3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {};\r\n\t\tuav_desc.Format = (DXGI_FORMAT)format;\r\n\t\tuav_desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;\r\n\t\tuav_desc.Texture2D.MipSlice = 0;\r\n\r\n\t\tn_device->CreateUnorderedAccessView(render_target->m_render_targets[id], nullptr, &uav_desc, handle.m_native);\r\n\t\tOffset(handle, 1, increment_size);\r\n\t}\r\n\r\n\tvoid Resize(RenderTarget** render_target, Device* device, unsigned int width, unsigned int height)\r\n\t{\r\n\t\tauto create_info = (*render_target)->m_create_info;\r\n\t\tDestroy(*render_target);\r\n\t\t(*render_target) = CreateRenderTarget(device, width, height, create_info);\r\n\t}\r\n\r\n\tvoid IncrementFrameIdx(RenderTarget* render_target)\r\n\t{\r\n\t\trender_target->m_frame_idx = (render_target->m_frame_idx + 1) % render_target->m_num_render_targets;\r\n\t}\r\n\r\n\tvoid DestroyDepthStencilBuffer(RenderTarget* render_target)\r\n\t{\r\n\t\tSAFE_RELEASE(render_target->m_depth_stencil_buffer);\r\n\t\tSAFE_RELEASE(render_target->m_depth_stencil_resource_heap);\r\n\t}\r\n\r\n\tvoid DestroyRenderTargetViews(RenderTarget* render_target)\r\n\t{\r\n\t\tSAFE_RELEASE(render_target->m_rtv_descriptor_heap);\r\n\t\trender_target->m_frame_idx = 0;\r\n\t\tfor (auto i = 0; i < render_target->m_render_targets.size(); i++)\r\n\t\t{\r\n\t\t\tSAFE_RELEASE(render_target->m_render_targets[i]);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid Destroy(RenderTarget* render_target)\r\n\t{\r\n\t\tif(!render_target)\r\n\t\t{\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tif (render_target->m_create_info.m_create_dsv_buffer)\r\n\t\t{\r\n\t\t\tDestroyDepthStencilBuffer(render_target);\r\n\t\t}\r\n\t\tDestroyRenderTargetViews(render_target);\r\n\r\n\t\tdelete render_target;\r\n\t}\r\n\r\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_render_window.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"../util/log.hpp\"\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tnamespace internal\n\t{\n\t\tinline DXGI_SWAP_CHAIN_DESC1 GetSwapChainDesc(unsigned int width, unsigned int height, unsigned int num_back_buffers)\n\t\t{\n\t\t\tDXGI_SAMPLE_DESC sample_desc = {};\n\t\t\tsample_desc.Count = 1;\n\t\t\tsample_desc.Quality = 0;\n\n\t\t\tDXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {};\n\t\t\tswap_chain_desc.Width = width;\n\t\t\tswap_chain_desc.Height = height;\n\t\t\tswap_chain_desc.Format = (DXGI_FORMAT)settings::back_buffer_format;\n\t\t\tswap_chain_desc.SampleDesc = sample_desc;\n\t\t\tswap_chain_desc.BufferCount = num_back_buffers;\n\t\t\tswap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;\n\t\t\tswap_chain_desc.SwapEffect = settings::flip_mode;\n\t\t\tswap_chain_desc.AlphaMode = settings::swapchain_alpha_mode;\n\t\t\tswap_chain_desc.Flags = settings::swapchain_flags;\n\t\t\tswap_chain_desc.Scaling = settings::swapchain_scaling;\n\n\t\t\treturn swap_chain_desc;\n\t\t}\n\n\t\tinline void EnsureSwapchainColorSpace(RenderWindow* render_window, std::uint32_t swapchain_bit_depth, bool enable_st2084)\n\t\t{\n\t\t\tDXGI_COLOR_SPACE_TYPE color_space = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;\n\n\t\t\tswitch (swapchain_bit_depth)\n\t\t\t{\n\t\t\tdefault:\n\t\t\t\tbreak;\n\n\t\t\tcase 10:\n\t\t\t\tcolor_space = enable_st2084 ? DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020 : DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;\n\t\t\t\tbreak;\n\n\t\t\tcase 16:\n\t\t\t\tcolor_space = DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tUINT support = 0;\n\t\t\tif (SUCCEEDED(render_window->m_swap_chain->CheckColorSpaceSupport(color_space, &support)) &&\n\t\t\t\t((support & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT) == DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT))\n\t\t\t{\n\t\t\t\trender_window->m_swap_chain->SetColorSpace1(color_space);\n\t\t\t}\n\t\t}\n\n\t\tinline void SetHDRMetaData(RenderWindow* render_window, std::uint32_t swapchain_bit_depth,\n\t\t\tfloat max_output_nits,\n\t\t\tfloat min_output_nits,\n\t\t\tfloat max_cll,\n\t\t\tfloat max_fall)\n\t\t{\n\t\t\tstruct DisplayChromacities\n\t\t\t{\n\t\t\t\tfloat m_red_x;\n\t\t\t\tfloat m_red_y;\n\t\t\t\tfloat m_green_x;\n\t\t\t\tfloat m_green_y;\n\t\t\t\tfloat m_blue_x;\n\t\t\t\tfloat m_blue_y;\n\t\t\t\tfloat m_white_x;\n\t\t\t\tfloat m_white_y;\n\t\t\t};\n\n\t\t\tstatic const DisplayChromacities chromaticity[] =\n\t\t\t{\n\t\t\t\t{ 0.64000f, 0.33000f, 0.30000f, 0.60000f, 0.15000f, 0.06000f, 0.31270f, 0.32900f }, // Display Gamut Rec709 \n\t\t\t\t{ 0.70800f, 0.29200f, 0.17000f, 0.79700f, 0.13100f, 0.04600f, 0.31270f, 0.32900f }, // Display Gamut Rec2020\n\t\t\t};\n\n\t\t\t// Select the chromaticity based on HDR format of the DWM.\n\t\t\tint selected_chroma = 0;\n\t\t\tif (swapchain_bit_depth == 16)\n\t\t\t{\n\t\t\t\tselected_chroma = 0;\n\t\t\t}\n\t\t\telse if (swapchain_bit_depth == 10)\n\t\t\t{\n\t\t\t\tselected_chroma = 1;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t// Reset the metadata since this is not a supported HDR format.\n\t\t\t\tTRY_M(render_window->m_swap_chain->SetHDRMetaData(DXGI_HDR_METADATA_TYPE_NONE, 0, nullptr), \"Failed to reset hdr meta data\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Set HDR meta data\n\t\t\tconst DisplayChromacities& chroma = chromaticity[selected_chroma];\n\t\t\tDXGI_HDR_METADATA_HDR10 hdr10_metadata = {};\n\t\t\thdr10_metadata.RedPrimary[0] = static_cast<std::uint16_t>(chroma.m_red_x * 50000.0f);\n\t\t\thdr10_metadata.RedPrimary[1] = static_cast<std::uint16_t>(chroma.m_red_y * 50000.0f);\n\t\t\thdr10_metadata.GreenPrimary[0] = static_cast<std::uint16_t>(chroma.m_green_x * 50000.0f);\n\t\t\thdr10_metadata.GreenPrimary[1] = static_cast<std::uint16_t>(chroma.m_green_y * 50000.0f);\n\t\t\thdr10_metadata.BluePrimary[0] = static_cast<std::uint16_t>(chroma.m_blue_x * 50000.0f);\n\t\t\thdr10_metadata.BluePrimary[1] = static_cast<std::uint16_t>(chroma.m_blue_y * 50000.0f);\n\t\t\thdr10_metadata.WhitePoint[0] = static_cast<std::uint16_t>(chroma.m_white_x * 50000.0f);\n\t\t\thdr10_metadata.WhitePoint[1] = static_cast<std::uint16_t>(chroma.m_white_y * 50000.0f);\n\t\t\thdr10_metadata.MaxMasteringLuminance = static_cast<std::uint32_t>(max_output_nits * 10000.0f);\n\t\t\thdr10_metadata.MinMasteringLuminance = static_cast<std::uint32_t>(min_output_nits * 10000.0f);\n\t\t\thdr10_metadata.MaxContentLightLevel = static_cast<std::uint16_t>(max_cll);\n\t\t\thdr10_metadata.MaxFrameAverageLightLevel = static_cast<std::uint16_t>(max_fall);\n\t\t\tTRY_M(render_window->m_swap_chain->SetHDRMetaData(DXGI_HDR_METADATA_TYPE_HDR10, sizeof(DXGI_HDR_METADATA_HDR10), &hdr10_metadata), \"Failed to set hdr meta data\");\n\t\t}\n\t}\n\n\tRenderWindow* CreateRenderWindow(Device* device, HWND window, CommandQueue* cmd_queue, unsigned int num_back_buffers)\n\t{\n\t\tauto render_window = new RenderWindow();\n\t\trender_window->m_num_render_targets = num_back_buffers;\n\n\t\t// Get client area for the swap chain size\n\t\tRECT r;\n\t\tGetClientRect(window, &r);\n\t\tunsigned int width = static_cast<decltype(width)>(r.right - r.left);\n\t\tunsigned int height = static_cast<decltype(height)>(r.bottom - r.top);\n\n\t\trender_window->m_width = width;\n\t\trender_window->m_height = height;\n\n\t\tconst auto swap_chain_desc = internal::GetSwapChainDesc(width, height, num_back_buffers);\n\n\t\tIDXGISwapChain1* temp_swap_chain;\n\t\tTRY_M(device->m_dxgi_factory->CreateSwapChainForHwnd(\n\t\t\tcmd_queue->m_native,\n\t\t\twindow,\n\t\t\t&swap_chain_desc,\n\t\t\tNULL,\n\t\t\tNULL,\n\t\t\t&temp_swap_chain\n\t\t), \"Failed to create swap chain for HWND.\");\n\n\t\trender_window->m_swap_chain = static_cast<IDXGISwapChain4*>(temp_swap_chain);\n\t\trender_window->m_frame_idx = (render_window->m_swap_chain)->GetCurrentBackBufferIndex();\n\n\t\tconst float meta_data_args_pool[4][4] =\n\t\t{\n\t\t\t// MaxOutputNits, MinOutputNits, MaxCLL, MaxFALL\n\t\t\t// These values are made up for testing. You need to figure out those numbers for your app.\n\t\t\t{ 1000.0f, 0.001f, 2000.0f, 500.0f },\n\t\t\t{ 500.0f, 0.001f, 2000.0f, 500.0f },\n\t\t\t{ 500.0f, 0.100f, 500.0f, 100.0f },\n\t\t\t{ 2000.0f, 1.000f, 2000.0f, 1000.0f }\n\t\t};\n\t\tint meta_data_args_idx = 0;\n\n\t\tif (d3d12::settings::output_hdr)\n\t\t{\n\t\t\tinternal::EnsureSwapchainColorSpace(render_window, 16, true);\n\t\t\tinternal::SetHDRMetaData(render_window, 16,\n\t\t\t\tmeta_data_args_pool[meta_data_args_idx][0],\n\t\t\t\tmeta_data_args_pool[meta_data_args_idx][1],\n\t\t\t\tmeta_data_args_pool[meta_data_args_idx][2],\n\t\t\t\tmeta_data_args_pool[meta_data_args_idx][3]);\n\t\t}\n\n\t\trender_window->m_render_targets.resize(num_back_buffers);\n\t\tfor (decltype(num_back_buffers) i = 0; i < num_back_buffers; i++)\n\t\t{\n\t\t\tTRY_M(render_window->m_swap_chain->GetBuffer(i, IID_PPV_ARGS(&render_window->m_render_targets[i])),\n\t\t\t\t\"Failed to get swap chain buffer.\");\n\t\t}\n\n\t\tCreateRenderTargetViews(render_window, device);\n\t\tCreateDepthStencilBuffer(render_window, device, width, height);\n\n\t\treturn render_window;\n\t}\n\n\tRenderWindow* CreateRenderWindow(Device* device, IUnknown* window, CommandQueue* cmd_queue, unsigned int num_back_buffers)\n\t{\n\t\tauto render_window = new RenderWindow();\n\t\trender_window->m_num_render_targets = num_back_buffers;\n\n\t\tunsigned int width = 100;\n\t\tunsigned int height = 100; // TODO: Get the actual size.\n\t\tauto swap_chain_desc = internal::GetSwapChainDesc(width, height, num_back_buffers);\n\n\t\tIDXGISwapChain1* temp_swap_chain;\n\t\tTRY_M(device->m_dxgi_factory->CreateSwapChainForCoreWindow(\n\t\t\tcmd_queue->m_native,\n\t\t\twindow,\n\t\t\t&swap_chain_desc,\n\t\t\tNULL,\n\t\t\t&temp_swap_chain\n\t\t), \"Failed to create swap chain for Core Window (UWP)\");\n\n\t\trender_window->m_swap_chain = static_cast<IDXGISwapChain4*>(temp_swap_chain);\n\t\trender_window->m_frame_idx = (render_window->m_swap_chain)->GetCurrentBackBufferIndex();\n\n\t\trender_window->m_swap_chain->SetMaximumFrameLatency(num_back_buffers);\n\n\t\trender_window->m_render_targets.resize(num_back_buffers);\n\t\tfor (decltype(num_back_buffers) i = 0; i < num_back_buffers; i++)\n\t\t{\n\t\t\tTRY_M(render_window->m_swap_chain->GetBuffer(i, IID_PPV_ARGS(&render_window->m_render_targets[i])),\n\t\t\t\t\"Failed to get swap chain buffer.\");\n\t\t}\n\n\t\tCreateRenderTargetViews(render_window, device);\n\t\tCreateDepthStencilBuffer(render_window, device, width, height);\n\n\t\treturn render_window;\n\t}\n\n\tvoid Resize(RenderWindow* render_window, Device* device, unsigned int width, unsigned int height)\n\t{\n\t\tDestroyDepthStencilBuffer(render_window);\n\t\tDestroyRenderTargetViews(render_window);\n\n\t\trender_window->m_width = width;\n\t\trender_window->m_height = height;\n\t\trender_window->m_swap_chain->ResizeBuffers(render_window->m_num_render_targets, width, height, DXGI_FORMAT_UNKNOWN, 0);\n\n\t\trender_window->m_render_targets.resize(render_window->m_num_render_targets);\n\t\tfor (auto i = 0u; i < render_window->m_num_render_targets; i++)\n\t\t{\n\t\t\tTRY_M(render_window->m_swap_chain->GetBuffer(i, IID_PPV_ARGS(&render_window->m_render_targets[i])),\n\t\t\t\t\"Failed to get swap chain buffer.\");\n\t\t}\n\n\t\tCreateRenderTargetViews(render_window, device);\n\t\tCreateDepthStencilBuffer(render_window, device, width, height);\n\t}\n\n\tvoid Present(RenderWindow* render_window)\n\t{\n\t\trender_window->m_swap_chain->Present(0, 0);\n\t\trender_window->m_frame_idx = render_window->m_swap_chain->GetCurrentBackBufferIndex();\n\t}\n\n\tvoid Destroy(RenderWindow* render_window)\n\t{\n\t\tDestroyDepthStencilBuffer(render_window);\n\t\trender_window->m_rtv_descriptor_heap->Release();\n\t\trender_window->m_frame_idx = 0;\n\n\t\trender_window->m_swap_chain->Release();\n\t\tdelete render_window;\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_renderer.cpp",
    "content": "/*!\r\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\r\n *\r\n * Licensed under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n *     http://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an \"AS IS\" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n */\r\n#include \"d3d12_renderer.hpp\"\r\n\r\n#include \"../util/defines.hpp\"\r\n#include \"../util/log.hpp\"\r\n#include \"../scene_graph/scene_graph.hpp\"\r\n#include \"../frame_graph/frame_graph.hpp\"\r\n#include \"../window.hpp\"\r\n\r\n#include \"d3d12_defines.hpp\"\r\n#include \"d3d12_material_pool.hpp\"\r\n#include \"d3d12_resource_pool_texture.hpp\"\r\n#include \"d3d12_model_pool.hpp\"\r\n#include \"d3d12_constant_buffer_pool.hpp\"\r\n#include \"d3d12_structured_buffer_pool.hpp\"\r\n#include \"d3d12_functions.hpp\"\r\n#include \"../pipeline_registry.hpp\"\r\n#include \"../rt_pipeline_registry.hpp\"\r\n#include \"../shader_registry.hpp\"\r\n#include \"../root_signature_registry.hpp\"\r\n#include \"d3d12_resource_pool_texture.hpp\"\r\n#include \"d3d12_dynamic_descriptor_heap.hpp\"\r\n\r\n#include \"../scene_graph/mesh_node.hpp\"\r\n#include \"../scene_graph/camera_node.hpp\"\r\n#include \"../scene_graph/light_node.hpp\"\r\n#include \"../scene_graph/skybox_node.hpp\"\r\n\r\n#include \"../render_tasks/d3d12_equirect_to_cubemap.hpp\"\r\n#include \"../render_tasks/d3d12_cubemap_convolution.hpp\"\r\n\r\n#include <iostream>\r\n#include <string>\r\n\r\nnamespace wr\r\n{\r\n\tLINK_SG_RENDER_MESHES(D3D12RenderSystem, Render_MeshNodes)\r\n\tLINK_SG_INIT_MESHES(D3D12RenderSystem, Init_MeshNodes)\r\n\tLINK_SG_INIT_CAMERAS(D3D12RenderSystem, Init_CameraNodes)\r\n\tLINK_SG_INIT_LIGHTS(D3D12RenderSystem, Init_LightNodes)\r\n\tLINK_SG_UPDATE_MESHES(D3D12RenderSystem, Update_MeshNodes)\r\n\tLINK_SG_UPDATE_CAMERAS(D3D12RenderSystem, Update_CameraNodes)\r\n\tLINK_SG_UPDATE_LIGHTS(D3D12RenderSystem, Update_LightNodes)\r\n\tLINK_SG_UPDATE_TRANSFORMS(D3D12RenderSystem, Update_Transforms)\r\n\tLINK_SG_DELETE_SKYBOX(D3D12RenderSystem, Delete_Skybox)\r\n\r\n\tD3D12RenderSystem::~D3D12RenderSystem()\r\n\t{\r\n\t\tfor (int i = 0; i < m_structured_buffer_pools.size(); ++i)\r\n\t\t{\r\n\t\t\tm_structured_buffer_pools[i].reset();\r\n\t\t}\r\n\r\n\t\tfor (auto* shape : m_simple_shapes)\r\n\t\t{\r\n\t\t\tm_shapes_pool->Destroy(shape);\r\n\t\t}\r\n\r\n\t\tfor (int i = 0; i < m_model_pools.size(); ++i)\r\n\t\t{\r\n\t\t\tm_model_pools[i].reset();\r\n\t\t}\r\n\r\n\t\tfor (int i = 0; i < m_texture_pools.size(); ++i)\r\n\t\t{\r\n\t\t\tm_texture_pools[i].reset();\r\n\t\t}\r\n\r\n\t\tfor (auto* fence : m_fences)\r\n\t\t{\r\n\t\t\tSAFE_RELEASE(fence->m_native);\r\n\t\t\tdelete fence;\r\n\t\t}\r\n\r\n\t\tDestroyShaderRegistry();\r\n\t\tDestroyRootSignatureRegistry();\r\n\t\tDestroyPipelineRegistry();\r\n\t\tDestroyRTPipelineRegistry();\r\n\r\n\t\td3d12::Destroy(m_fullscreen_quad_vb);\r\n\t\td3d12::Destroy(m_direct_cmd_list);\r\n\t\td3d12::Destroy(m_device);\r\n\t\td3d12::Destroy(m_direct_queue);\r\n\t\td3d12::Destroy(m_copy_queue);\r\n\t\td3d12::Destroy(m_compute_queue);\r\n\t\tif (m_render_window.has_value())\r\n\t\t{\r\n\t\t\td3d12::Destroy(m_render_window.value());\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Init(std::optional<Window*> window)\r\n\t{\r\n\t\tm_window = window;\r\n\t\tm_device = d3d12::CreateDevice();\r\n\t\tSetName(m_device, L\"Default D3D12 Device\");\r\n\t\tm_direct_queue = d3d12::CreateCommandQueue(m_device, CmdListType::CMD_LIST_DIRECT);\r\n\t\tm_compute_queue = d3d12::CreateCommandQueue(m_device, CmdListType::CMD_LIST_COMPUTE);\r\n\t\tm_copy_queue = d3d12::CreateCommandQueue(m_device, CmdListType::CMD_LIST_COPY);\r\n\t\tSetName(m_direct_queue, L\"Default D3D12 Direct Command Queue\");\r\n\t\tSetName(m_compute_queue, L\"Default D3D12 Compute Command Queue\");\r\n\t\tSetName(m_copy_queue, L\"Default D3D12 Copy Command Queue\");\r\n\r\n\t\tif (window.has_value())\r\n\t\t{\r\n\t\t\tm_render_window = d3d12::CreateRenderWindow(m_device, window.value()->GetWindowHandle(), m_direct_queue, d3d12::settings::num_back_buffers);\r\n\t\t}\r\n\r\n\t\tPrepareShaderRegistry();\r\n\t\tPrepareRootSignatureRegistry();\r\n\t\tPreparePipelineRegistry();\r\n\t\tPrepareRTPipelineRegistry();\r\n\r\n\t\t// Create fences\r\n\t\tfor (auto i = 0; i < m_fences.size(); i++)\r\n\t\t{\r\n\t\t\tm_fences[i] = d3d12::CreateFence(m_device);\r\n\t\t\tSetName(m_fences[i], (L\"Fence \" + std::to_wstring(i)));\r\n\t\t}\r\n\r\n\t\t// Create viewport\r\n\t\tm_viewport = d3d12::CreateViewport(window.has_value() ? window.value()->GetWidth() : 400, window.has_value() ? window.value()->GetHeight() : 400);\r\n\r\n\t\t// Create screen quad\r\n\t\tm_fullscreen_quad_vb = d3d12::CreateStagingBuffer(m_device, (void*)temp::quad_vertices, 4 * sizeof(Vertex2D), sizeof(Vertex2D), ResourceState::VERTEX_AND_CONSTANT_BUFFER);\r\n\t\tSetName(m_fullscreen_quad_vb, L\"Fullscreen quad vertex buffer\");\r\n\r\n\t\t// Create Command List\r\n\t\tm_direct_cmd_list = d3d12::CreateCommandList(m_device, d3d12::settings::num_back_buffers, CmdListType::CMD_LIST_DIRECT);\r\n\t\tSetName(m_direct_cmd_list, L\"Defauld DX12 Command List\");\r\n\r\n\t\t// Raytracing cb pool\r\n\t\tm_raytracing_cb_pool = CreateConstantBufferPool(1_mb);\r\n\r\n\t\t// Simple Shapes Model Pool\r\n\t\tm_shapes_pool = CreateModelPool(8_mb, 8_mb);\r\n\t\tLoadPrimitiveShapes();\r\n\r\n\t\t// Material raytracing sb pool\r\n\t\tsize_t rt_mat_align_size = SizeAlignTwoPower((sizeof(temp::RayTracingMaterial_CBData) * d3d12::settings::num_max_rt_materials), 65536) * d3d12::settings::num_back_buffers;\r\n\t\tm_raytracing_material_sb_pool = CreateStructuredBufferPool(rt_mat_align_size);\r\n\r\n\t\t// Offset raytracing sb pool\r\n\t\tsize_t rt_offset_align_size = SizeAlignTwoPower((sizeof(temp::RayTracingOffset_CBData) * d3d12::settings::num_max_rt_materials), 65536) * d3d12::settings::num_back_buffers;\r\n\t\tm_raytracing_offset_sb_pool = CreateStructuredBufferPool(rt_offset_align_size);\r\n\r\n\t\t// Begin Recording\r\n\t\tauto frame_idx = m_render_window.has_value() ? m_render_window.value()->m_frame_idx : 0;\r\n\t\td3d12::Begin(m_direct_cmd_list, frame_idx);\r\n\r\n\t\t// Stage fullscreen quad\r\n\t\td3d12::StageBuffer(m_fullscreen_quad_vb, m_direct_cmd_list);\r\n\t\t\r\n\t\t// Execute\r\n\t\td3d12::End(m_direct_cmd_list);\r\n\t\td3d12::Execute(m_direct_queue, { m_direct_cmd_list }, m_fences[frame_idx]);\r\n\r\n\t\tm_buffer_frame_graph_uids.resize(d3d12::settings::num_back_buffers);\r\n\r\n\t\t//Rendering engine creates a texture pool that will be used by the render tasks.\r\n\t\tm_texture_pools.push_back(CreateTexturePool());\r\n\t\tCreateDefaultResources();\r\n\t}\r\n\r\n\tCPUTextures D3D12RenderSystem::Render(SceneGraph& scene_graph, FrameGraph& frame_graph)\r\n\t{\r\n\t\tif (m_skybox_changed)\r\n\t\t{\r\n\t\t\tframe_graph.SetShouldExecute<wr::EquirectToCubemapTaskData>(true);\r\n\t\t\tframe_graph.SetShouldExecute<wr::CubemapConvolutionTaskData>(true);\r\n\r\n\t\t\tm_skybox_changed = false;\r\n\t\t}\r\n\r\n\t\t// Perform render target save requests\r\n\t\twhile (!m_requested_rt_saves.empty())\r\n\t\t{\r\n\t\t\tWaitForAllPreviousWork();\r\n\t\t\tauto back = m_requested_rt_saves.back();\r\n\t\t\tSaveRenderTargetToDisc(back.m_path, back.m_render_target, back.m_index);\r\n\t\t\tm_requested_rt_saves.pop();\r\n\t\t}\r\n\r\n\t\tif (m_requested_fullscreen_state.has_value())\r\n\t\t{\r\n\t\t\tWaitForAllPreviousWork();\r\n\t\t\tm_render_window.value()->m_swap_chain->SetFullscreenState(m_requested_fullscreen_state.value(), nullptr);\r\n\t\t\tResize(m_window.value()->GetWidth(), m_window.value()->GetHeight());\r\n\t\t\tm_requested_fullscreen_state = std::nullopt;\r\n\t\t}\r\n\r\n\t\tauto frame_idx = GetFrameIdx();\r\n\t\td3d12::WaitFor(m_fences[frame_idx]);\r\n\r\n\t\t//Signal to the texture pool that we waited for the previous frame \r\n\t\t//so that stale descriptors and temporary textures can be freed.\r\n\t\tfor (auto pool : m_texture_pools)\r\n\t\t{\r\n\t\t\tpool->ReleaseTemporaryResources();\r\n\t\t\tpool->UnloadTextures(frame_idx);\r\n\t\t}\r\n\r\n\t\t// Perform reload requests\r\n\t\t{\r\n\t\t\t// Root Signatures\r\n\t\t\tauto& rs_registry = RootSignatureRegistry::Get();\r\n\t\t\trs_registry.Lock();\r\n\t\t\tfor (auto request : rs_registry.GetReloadRequests())\r\n\t\t\t{\r\n\t\t\t\tReloadRootSignatureRegistryEntry(request);\r\n\t\t\t}\r\n\t\t\trs_registry.ClearReloadRequests();\r\n\t\t\trs_registry.Unlock();\r\n\r\n\t\t\t// Shaders\r\n\t\t\tauto& shader_registry = ShaderRegistry::Get();\r\n\t\t\tshader_registry.Lock();\r\n\t\t\tfor (auto request : shader_registry.GetReloadRequests())\r\n\t\t\t{\r\n\t\t\t\tReloadShaderRegistryEntry(request);\r\n\t\t\t}\r\n\t\t\tshader_registry.ClearReloadRequests();\r\n\t\t\tshader_registry.Unlock();\r\n\r\n\t\t\t// Pipelines\r\n\t\t\tauto& pipeline_registry = PipelineRegistry::Get();\r\n\t\t\tpipeline_registry.Lock();\r\n\t\t\tfor (auto request : pipeline_registry.GetReloadRequests())\r\n\t\t\t{\r\n\t\t\t\tReloadPipelineRegistryEntry(request);\r\n\t\t\t}\r\n\t\t\tpipeline_registry.ClearReloadRequests();\r\n\t\t\tpipeline_registry.Unlock();\r\n\r\n\t\t\t// RT Pipelines\r\n\t\t\tauto& rt_pipeline_registry = RTPipelineRegistry::Get();\r\n\t\t\trt_pipeline_registry.Lock();\r\n\t\t\tfor (auto request : rt_pipeline_registry.GetReloadRequests())\r\n\t\t\t{\r\n\t\t\t\tReloadRTPipelineRegistryEntry(request);\r\n\t\t\t}\r\n\t\t\trt_pipeline_registry.ClearReloadRequests();\r\n\t\t\trt_pipeline_registry.Unlock();\r\n\t\t}\r\n\r\n\r\n\t\tbool clear_frame_buffer = false;\r\n\r\n\t\tif (frame_graph.GetUID() != m_buffer_frame_graph_uids[frame_idx])\r\n\t\t{\r\n\t\t\tm_buffer_frame_graph_uids[frame_idx] = frame_graph.GetUID();\r\n\t\t\tclear_frame_buffer = true;\r\n\t\t}\r\n\r\n\t\tPreparePreRenderCommands(clear_frame_buffer, frame_idx);\r\n\r\n\t\tscene_graph.Update();\r\n\t\tscene_graph.Optimize();\r\n\r\n\t\tframe_graph.Execute(scene_graph);\r\n\r\n\t\tauto cmd_lists = frame_graph.GetAllCommandLists<d3d12::CommandList>();\r\n\t\tstd::vector<d3d12::CommandList*> n_cmd_lists;\r\n\t\tn_cmd_lists.reserve(cmd_lists.size());\r\n\r\n\t\tn_cmd_lists.push_back(m_direct_cmd_list);\r\n\r\n\t\tfor (auto& list : cmd_lists)\r\n\t\t{\r\n\t\t\tn_cmd_lists.push_back(list);\r\n\t\t}\r\n\r\n\t\t// Reset the batches.\r\n\t\tResetBatches(scene_graph);\r\n\r\n\t\td3d12::Execute(m_direct_queue, n_cmd_lists, m_fences[frame_idx]);\r\n\r\n\t\tif (m_render_window.has_value())\r\n\t\t{\r\n\t\t\td3d12::Present(m_render_window.value());\r\n\t\t}\r\n\r\n\t\tm_bound_model_pool = nullptr;\r\n\r\n\t\tfor (int i = 0; i < m_model_pools.size(); ++i)\r\n\t\t{\r\n\t\t\tm_model_pools[i]->SetUpdated(false);\r\n\t\t}\r\n\r\n\t\t// Optional CPU-visible copy of the render target pixel data\r\n\t\tconst auto cpu_output_texture = frame_graph.GetOutputTexture();\r\n\r\n\t\t// Optional CPU-visible copy of the render target pixel and/or depth data\r\n\t\treturn frame_graph.GetOutputTexture();\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Resize(std::uint32_t width, std::uint32_t height)\r\n\t{\r\n\t\td3d12::ResizeViewport(m_viewport, (int)width, (int)height);\r\n\t\tif (m_render_window.has_value())\r\n\t\t{\r\n\t\t\td3d12::Resize(m_render_window.value(), m_device, width, height);\r\n\t\t}\r\n\t}\r\n\r\n\tstd::shared_ptr<TexturePool> D3D12RenderSystem::CreateTexturePool()\r\n\t{\r\n\t\tstd::shared_ptr<D3D12TexturePool> pool = std::make_shared<D3D12TexturePool>(*this);\r\n\t\tm_texture_pools.push_back(pool);\r\n\t\treturn pool;\r\n\t}\r\n\r\n\tstd::shared_ptr<MaterialPool> D3D12RenderSystem::CreateMaterialPool(std::size_t size_in_bytes)\r\n\t{\r\n\t\treturn std::make_shared<D3D12MaterialPool>(*this);\r\n\t}\r\n\r\n\tstd::shared_ptr<ModelPool> D3D12RenderSystem::CreateModelPool(std::size_t vertex_buffer_pool_size_in_bytes, std::size_t index_buffer_pool_size_in_bytes)\r\n\t{\r\n\t\tstd::shared_ptr<D3D12ModelPool> pool = std::make_shared<D3D12ModelPool>(*this, vertex_buffer_pool_size_in_bytes, index_buffer_pool_size_in_bytes);\r\n\t\tm_model_pools.push_back(pool);\r\n\t\treturn pool;\r\n\t}\r\n\r\n\tstd::shared_ptr<ConstantBufferPool> D3D12RenderSystem::CreateConstantBufferPool(std::size_t size_in_bytes)\r\n\t{\r\n\t\treturn std::make_shared<D3D12ConstantBufferPool>(*this, size_in_bytes);\r\n\t}\r\n\r\n\tstd::shared_ptr<StructuredBufferPool> D3D12RenderSystem::CreateStructuredBufferPool(std::size_t size_in_bytes)\r\n\t{\r\n\t\tstd::shared_ptr<D3D12StructuredBufferPool> pool = std::make_shared<D3D12StructuredBufferPool>(*this, size_in_bytes);\r\n\t\tm_structured_buffer_pools.push_back(pool);\r\n\t\treturn pool;\r\n\t}\r\n\r\n\tstd::shared_ptr<TexturePool> D3D12RenderSystem::GetDefaultTexturePool()\r\n\t{\r\n\t\tif (m_texture_pools.size() > 0)\r\n\t\t{\r\n\t\t\treturn m_texture_pools[0];\r\n\t\t}\r\n\r\n\t\treturn std::shared_ptr<TexturePool>();\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::WaitForAllPreviousWork()\r\n\t{\r\n\t\tfor (auto& fence : m_fences)\r\n\t\t{\r\n\t\t\td3d12::WaitFor(fence);\r\n\t\t\tSignal(fence, m_direct_queue);\r\n\t\t}\r\n\t}\r\n\r\n\tCommandList* D3D12RenderSystem::GetDirectCommandList(unsigned int num_allocators)\r\n\t{\r\n\t\treturn d3d12::CreateCommandList(m_device, num_allocators, CmdListType::CMD_LIST_DIRECT);\r\n\t}\r\n\r\n\tCommandList* D3D12RenderSystem::GetBundleCommandList(unsigned int num_allocators)\r\n\t{\r\n\t\treturn d3d12::CreateCommandList(m_device, num_allocators, CmdListType::CMD_LIST_BUNDLE);\r\n\t}\r\n\r\n\tCommandList* D3D12RenderSystem::GetComputeCommandList(unsigned int num_allocators)\r\n\t{\r\n\t\treturn d3d12::CreateCommandList(m_device, num_allocators, CmdListType::CMD_LIST_DIRECT);\r\n\t}\r\n\r\n\tCommandList* D3D12RenderSystem::GetCopyCommandList(unsigned int num_allocators)\r\n\t{\r\n\t\treturn d3d12::CreateCommandList(m_device, num_allocators, CmdListType::CMD_LIST_DIRECT);\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::SetCommandListName(CommandList* cmd_list, std::wstring const& name)\r\n\t{\r\n\t\td3d12::SetName(static_cast<d3d12::CommandList*>(cmd_list), name);\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::DestroyCommandList(CommandList* cmd_list)\r\n\t{\r\n\t\tDestroy(static_cast<wr::d3d12::CommandList*>(cmd_list));\r\n\t}\r\n\r\n\tRenderTarget* D3D12RenderSystem::GetRenderTarget(RenderTargetProperties properties)\r\n\t{\r\n\t\tif (properties.m_is_render_window)\r\n\t\t{\r\n\t\t\tif (!m_render_window.has_value())\r\n\t\t\t{\r\n\t\t\t\tLOGC(\"Tried using a render task which depends on the render window.\");\r\n\t\t\t\treturn nullptr;\r\n\t\t\t}\r\n\t\t\treturn m_render_window.value();\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\td3d12::desc::RenderTargetDesc desc;\r\n\t\t\tdesc.m_initial_state = properties.m_state_finished.Get().value_or(ResourceState::RENDER_TARGET);\r\n\t\t\tdesc.m_create_dsv_buffer = properties.m_create_dsv_buffer;\r\n\t\t\tdesc.m_num_rtv_formats = properties.m_num_rtv_formats;\r\n\t\t\tdesc.m_rtv_formats = properties.m_rtv_formats;\r\n\t\t\tdesc.m_dsv_format = properties.m_dsv_format;\r\n\r\n\t\t\tif (properties.m_width.Get().has_value() || properties.m_height.Get().has_value())\r\n\t\t\t{\r\n\t\t\t\tauto retval = d3d12::CreateRenderTarget(m_device, \r\n\t\t\t\t\tstatic_cast<std::uint32_t>(properties.m_width.Get().value() * properties.m_resolution_scale.Get()),\r\n\t\t\t\t\tstatic_cast<std::uint32_t>(properties.m_height.Get().value() * properties.m_resolution_scale.Get()),\r\n\t\t\t\t\tdesc);\r\n\r\n\t\t\t\treturn retval;\r\n\t\t\t}\r\n\t\t\telse if (m_window.has_value())\r\n\t\t\t{\r\n\t\t\t\tauto retval = d3d12::CreateRenderTarget(m_device, \r\n\t\t\t\t\tstatic_cast<std::uint32_t>(m_window.value()->GetWidth() * properties.m_resolution_scale.Get()),\r\n\t\t\t\t\tstatic_cast<std::uint32_t>(m_window.value()->GetHeight() * properties.m_resolution_scale.Get()),\r\n\t\t\t\t\tdesc);\r\n\r\n\t\t\t\treturn retval;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tLOGC(\"Render target doesn't have a width or height specified. And there is no window to take the window size from. Hence can't create a proper render target.\");\r\n\t\t\t\treturn nullptr;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::SetRenderTargetName(RenderTarget* render_target, std::wstring const& name)\r\n\t{\r\n\t\td3d12::SetName(static_cast<d3d12::RenderTarget*>(render_target), name);\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::ResizeRenderTarget(RenderTarget** render_target, std::uint32_t width, std::uint32_t height)\r\n\t{\r\n\t\tauto n_render_target = static_cast<d3d12::RenderTarget*>(*render_target);\r\n\t\td3d12::Resize((d3d12::RenderTarget**)&n_render_target, m_device, width, height);\r\n\r\n\t\t(*render_target) = n_render_target;\r\n\t}\r\n\tvoid D3D12RenderSystem::DestroyRenderTarget(RenderTarget** render_target)\r\n\t{\r\n\t\tDestroy((d3d12::RenderTarget*)*render_target);\r\n\t\t*render_target = nullptr;\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::RequestFullscreenChange(bool fullscreen_state)\r\n\t{\r\n\t\tm_requested_fullscreen_state = fullscreen_state;\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::ResetCommandList(CommandList* cmd_list)\r\n\t{\r\n\t\tauto n_cmd_list = static_cast<d3d12::CommandList*>(cmd_list);\r\n\t\tauto frame_idx = GetFrameIdx();\r\n\t\td3d12::Begin(n_cmd_list, frame_idx);\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::CloseCommandList(CommandList* cmd_list)\r\n\t{\r\n\t\tauto n_cmd_list = static_cast<d3d12::CommandList*>(cmd_list);\r\n\t\td3d12::End(n_cmd_list);\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::StartRenderTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target)\r\n\t{\r\n\t\tauto n_cmd_list = static_cast<d3d12::CommandList*>(cmd_list);\r\n\t\tauto n_render_target = static_cast<d3d12::RenderTarget*>(render_target.first);\r\n\t\tauto frame_idx = GetFrameIdx();\r\n\r\n\t\tif (render_target.second.m_is_render_window) // TODO: do once at the beginning of the frame.\r\n\t\t{\r\n\t\t\td3d12::Transition(n_cmd_list, n_render_target, frame_idx, ResourceState::PRESENT, ResourceState::RENDER_TARGET);\r\n\t\t}\r\n\t\telse if (render_target.second.m_state_finished.Get().has_value() && render_target.second.m_state_execute.Get().has_value())\r\n\t\t{\r\n\t\t\td3d12::Transition(n_cmd_list, n_render_target, render_target.second.m_state_finished.Get().value(), render_target.second.m_state_execute.Get().value());\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tLOGW(\"A render target has no transitions specified. Is this correct?\");\r\n\t\t}\r\n\r\n\t\tif (render_target.second.m_is_render_window)\r\n\t\t{\r\n\t\t\td3d12::BindRenderTargetVersioned(n_cmd_list, n_render_target, frame_idx, render_target.second.m_clear, render_target.second.m_clear_depth);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\td3d12::BindRenderTarget(n_cmd_list, n_render_target, render_target.second.m_clear, render_target.second.m_clear_depth);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::StopRenderTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target)\r\n\t{\r\n\t\tauto n_cmd_list = static_cast<d3d12::CommandList*>(cmd_list);\r\n\t\tauto n_render_target = static_cast<d3d12::RenderTarget*>(render_target.first);\r\n\t\tunsigned int frame_idx = GetFrameIdx();\r\n\r\n\t\tif (render_target.second.m_is_render_window)\r\n\t\t{\r\n\t\t\td3d12::Transition(n_cmd_list, n_render_target, frame_idx, ResourceState::RENDER_TARGET, ResourceState::PRESENT);\r\n\t\t}\r\n\t\telse if (render_target.second.m_state_finished.Get().has_value() && render_target.second.m_state_execute.Get().has_value())\r\n\t\t{\r\n\t\t\td3d12::Transition(n_cmd_list, n_render_target, render_target.second.m_state_execute.Get().value(), render_target.second.m_state_finished.Get().value());\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tLOGW(\"A render target has no transitions specified. Is this correct?\");\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::StartComputeTask(CommandList * cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target)\r\n\t{\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::StopComputeTask(CommandList * cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target)\r\n\t{\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::StartCopyTask(CommandList * cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target)\r\n\t{\r\n\t\tauto n_cmd_list = static_cast<d3d12::CommandList*>(cmd_list);\r\n\t\tauto n_render_target = static_cast<d3d12::RenderTarget*>(render_target.first);\r\n\t\tauto frame_idx = GetFrameIdx();\r\n\r\n\t\tif (render_target.second.m_is_render_window) // TODO: do once at the beginning of the frame.\r\n\t\t{\r\n\t\t\td3d12::Transition(n_cmd_list, n_render_target, frame_idx, ResourceState::PRESENT, render_target.second.m_state_execute.Get().value());\r\n\t\t}\r\n\t\telse if (render_target.second.m_state_finished.Get().has_value() && render_target.second.m_state_execute.Get().has_value())\r\n\t\t{\r\n\t\t\td3d12::Transition(n_cmd_list, n_render_target, render_target.second.m_state_finished.Get().value(), render_target.second.m_state_execute.Get().value());\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::StopCopyTask(CommandList * cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target)\r\n\t{\r\n\t\tauto n_cmd_list = static_cast<d3d12::CommandList*>(cmd_list);\r\n\t\tauto n_render_target = static_cast<d3d12::RenderTarget*>(render_target.first);\r\n\t\tauto frame_idx = GetFrameIdx();\r\n\r\n\t\tif (render_target.second.m_is_render_window)\r\n\t\t{\r\n\t\t\td3d12::Transition(n_cmd_list, n_render_target, frame_idx, render_target.second.m_state_execute.Get().value(), ResourceState::PRESENT);\r\n\t\t}\r\n\t\telse if (render_target.second.m_state_finished.Get().has_value() && render_target.second.m_state_execute.Get().has_value())\r\n\t\t{\r\n\t\t\td3d12::Transition(n_cmd_list, n_render_target, render_target.second.m_state_execute.Get().value(), render_target.second.m_state_finished.Get().value());\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::SaveRenderTargetToDisc(std::string const& path, RenderTarget* render_target, unsigned int index)\r\n\t{\r\n\t\tauto n_render_target = static_cast<d3d12::RenderTarget*>(render_target);\r\n\t\tauto format = static_cast<Format>(n_render_target->m_render_targets[index]->GetDesc().Format);\r\n\t\t\r\n\t\tauto rt_resource = n_render_target->m_render_targets[index];\r\n\t\tauto n_device = m_device->m_native;\r\n\t\tauto width = d3d12::GetRenderTargetWidth(n_render_target);\r\n\t\tauto height = d3d12::GetRenderTargetHeight(n_render_target);\r\n\t\tauto bytes_per_pixel = BytesPerPixel(format);\r\n\t\tstd::uint64_t bytes_per_row = SizeAlignTwoPower(static_cast<std::uint64_t>(width) * static_cast<std::uint64_t>(bytes_per_pixel), 256);\r\n\t\tstd::uint64_t texture_size = bytes_per_row * static_cast<std::uint64_t>(height);\r\n\r\n\t\tauto queue = d3d12::CreateCommandQueue(m_device, CmdListType::CMD_LIST_DIRECT);\r\n\t\tSetName(queue, L\"Screenshot Command Queue\");\r\n\r\n\t\tauto cmd_list = d3d12::CreateCommandList(m_device, 1, CmdListType::CMD_LIST_DIRECT);\r\n\t\tSetName(cmd_list, L\"Screenshot Command List\");\r\n\r\n\t\tauto fence = d3d12::CreateFence(m_device);\r\n\r\n\t\td3d12::Begin(cmd_list, 0);\r\n\r\n\t\t// Create the actual read back buffer\r\n\t\tauto readback_buffer = d3d12::CreateReadbackBuffer(m_device, texture_size);\r\n\t\td3d12::SetName(readback_buffer, L\"Texture ReadBack Buffer (Used for saving to disc)\");\r\n\r\n\t\t// Copy data to cpu\r\n\t\tD3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint;\r\n\t\tauto rt_desc = rt_resource->GetDesc();\r\n\t\tn_device->GetCopyableFootprints(&rt_desc, 0, 1, 0, &footprint, nullptr, nullptr, (std::uint64_t*)&texture_size);\r\n\r\n\t\tCD3DX12_TEXTURE_COPY_LOCATION dest_loc(readback_buffer->m_resource, footprint);\r\n\t\tCD3DX12_TEXTURE_COPY_LOCATION src_loc(rt_resource, 0);\r\n\t\tcmd_list->m_native->CopyTextureRegion(&dest_loc, 0, 0, 0, &src_loc, nullptr);\r\n\r\n\t\td3d12::End(cmd_list);\r\n\r\n\t\t// Execute\r\n\t\tExecute(queue, { cmd_list }, fence);\r\n\t\tfence->m_fence_value++;\r\n\t\tSignal(fence, queue);\r\n\t\tWaitFor(fence);\r\n\r\n\t\t// Store data\r\n\t\tauto pixels = static_cast<std::uint8_t*>(d3d12::MapReadbackBuffer(readback_buffer, texture_size));\r\n\t\td3d12::UnmapReadbackBuffer(readback_buffer);\r\n\r\n\t\tDirectX::Image img;\r\n\t\timg.format = static_cast<DXGI_FORMAT>(format);\r\n\t\timg.width = width;\r\n\t\timg.height = height;\r\n\t\timg.rowPitch = bytes_per_row;\r\n\t\timg.slicePitch = texture_size;\r\n\t\timg.pixels = pixels;\r\n\r\n\t\tauto wpath = std::wstring(path.begin(), path.end());\r\n\t\tTRY_M(DirectX::SaveToTGAFile(img, wpath.c_str()), \"Failed to save image to disc\");\r\n\r\n\t\td3d12::Destroy(readback_buffer);\r\n\t\td3d12::Destroy(cmd_list);\r\n\t\td3d12::Destroy(fence);\r\n\t\td3d12::Destroy(queue);\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::PrepareRootSignatureRegistry()\r\n\t{\r\n\t\tauto& registry = RootSignatureRegistry::Get();\r\n\r\n\t\tfor (auto desc : registry.m_descriptions)\r\n\t\t{\r\n\t\t\td3d12::desc::RootSignatureDesc n_desc;\r\n\t\t\tn_desc.m_parameters = desc.second.m_parameters;\r\n\t\t\tn_desc.m_samplers = desc.second.m_samplers;\r\n\t\t\tn_desc.m_rtx = desc.second.m_rtx;\r\n\t\t\tn_desc.m_rt_local = desc.second.m_rtx_local;\r\n\r\n\t\t\tauto n_rs = d3d12::CreateRootSignature(n_desc);\r\n\t\t\td3d12::FinalizeRootSignature(n_rs, m_device);\r\n\t\t\tSetName(n_rs, (L\"Root Signature \" + desc.second.name));\r\n\r\n\t\t\tregistry.m_objects.insert({ desc.first, n_rs });\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::PrepareShaderRegistry()\r\n\t{\r\n\t\tauto& registry = ShaderRegistry::Get();\r\n\r\n\t\tfor (auto desc : registry.m_descriptions)\r\n\t\t{\r\n\t\t\tauto shader_error = d3d12::LoadShader(m_device, desc.second.type, desc.second.path, desc.second.entry, desc.second.defines);\r\n\r\n\t\t\tif (std::holds_alternative<d3d12::Shader*>(shader_error))\r\n\t\t\t{\r\n\t\t\t\tauto shader = std::get<d3d12::Shader*>(shader_error);\r\n\t\t\t\tregistry.m_objects.insert({ desc.first, shader });\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\ttry\r\n\t\t\t\t{\r\n\t\t\t\t\tLOGC(std::get<std::string>(shader_error));\r\n\t\t\t\t}\r\n\t\t\t\tcatch(std::exception e)\r\n\t\t\t\t{\r\n\t\t\t\t\tLOGW(\"Seems like FMT failed to format the error message. Using cout instead.\");\r\n\t\t\t\t\tstd::cerr << std::get<std::string>(shader_error) << std::endl;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::PreparePipelineRegistry()\r\n\t{\r\n\t\tauto& registry = PipelineRegistry::Get();\r\n\r\n\t\tfor (auto desc : registry.m_descriptions)\r\n\t\t{\r\n\t\t\td3d12::desc::PipelineStateDesc n_desc;\r\n\t\t\tn_desc.m_counter_clockwise = desc.second.m_counter_clockwise;\r\n\t\t\tn_desc.m_cull_mode = desc.second.m_cull_mode;\r\n\t\t\tn_desc.m_depth_enabled = desc.second.m_depth_enabled;\r\n\t\t\tn_desc.m_dsv_format = desc.second.m_dsv_format;\r\n\t\t\tn_desc.m_input_layout = desc.second.m_input_layout;\r\n\t\t\tn_desc.m_num_rtv_formats = desc.second.m_num_rtv_formats;\r\n\t\t\tn_desc.m_rtv_formats = desc.second.m_rtv_formats;\r\n\t\t\tn_desc.m_topology_type = desc.second.m_topology_type;\r\n\t\t\tn_desc.m_type = desc.second.m_type;\r\n\r\n\t\t\tauto n_pipeline = d3d12::CreatePipelineState();\r\n\r\n\t\t\tif (desc.second.m_vertex_shader_handle.has_value())\r\n\t\t\t{\r\n\t\t\t\tauto obj = ShaderRegistry::Get().Find(desc.second.m_vertex_shader_handle.value());\r\n\t\t\t\tauto shader = static_cast<d3d12::Shader*>(obj);\r\n\t\t\t\td3d12::SetVertexShader(n_pipeline, shader);\r\n\t\t\t}\r\n\t\t\tif (desc.second.m_pixel_shader_handle.has_value())\r\n\t\t\t{\r\n\t\t\t\tauto obj = ShaderRegistry::Get().Find(desc.second.m_pixel_shader_handle.value());\r\n\t\t\t\tauto shader = static_cast<d3d12::Shader*>(obj);\r\n\t\t\t\td3d12::SetFragmentShader(n_pipeline, shader);\r\n\t\t\t}\r\n\t\t\tif (desc.second.m_compute_shader_handle.has_value())\r\n\t\t\t{\r\n\t\t\t\tauto obj = ShaderRegistry::Get().Find(desc.second.m_compute_shader_handle.value());\r\n\t\t\t\tauto shader = static_cast<d3d12::Shader*>(obj);\r\n\t\t\t\td3d12::SetComputeShader(n_pipeline, shader);\r\n\t\t\t}\r\n\t\t\t{\r\n\t\t\t\tauto obj = RootSignatureRegistry::Get().Find(desc.second.m_root_signature_handle);\r\n\t\t\t\td3d12::SetRootSignature(n_pipeline, static_cast<d3d12::RootSignature*>(obj));\r\n\t\t\t}\r\n\r\n\t\t\td3d12::FinalizePipeline(n_pipeline, m_device, n_desc);\r\n\t\t\tSetName(n_pipeline, L\"Default pipeline state\");\r\n\r\n\t\t\tregistry.m_objects.insert({ desc.first, n_pipeline });\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::ReloadPipelineRegistryEntry(RegistryHandle handle)\r\n\t{\r\n\t\tauto& registry = PipelineRegistry::Get();\r\n\t\tstd::optional<std::string> error_msg = std::nullopt;\r\n\t\tauto n_pipeline = static_cast<d3d12::PipelineState*>(registry.Find(handle));\r\n\r\n\t\tauto recompile_shader = [&error_msg, this](auto& pipeline_shader)\r\n\t\t{\r\n\t\t\tif (!pipeline_shader) return;\r\n\r\n\t\t\tauto new_shader_variant = d3d12::LoadShader(m_device, pipeline_shader->m_type,\r\n\t\t\t\tpipeline_shader->m_path,\r\n\t\t\t\tpipeline_shader->m_entry,\r\n\t\t\t\tpipeline_shader->m_defines);\r\n\r\n\t\t\tif (std::holds_alternative<d3d12::Shader*>(new_shader_variant))\r\n\t\t\t{\r\n\t\t\t\tpipeline_shader = std::get<d3d12::Shader*>(new_shader_variant);\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\terror_msg = std::get<std::string>(new_shader_variant);\r\n\t\t\t}\r\n\t\t};\r\n\r\n\t\t// Vertex Shader\r\n\t\t{\r\n\t\t\trecompile_shader(n_pipeline->m_vertex_shader);\r\n\t\t}\r\n\t\t// Pixel Shader\r\n\t\tif (!error_msg.has_value()) {\r\n\t\t\trecompile_shader(n_pipeline->m_pixel_shader);\r\n\t\t}\r\n\t\t// Compute Shader\r\n\t\tif (!error_msg.has_value()) {\r\n\t\t\trecompile_shader(n_pipeline->m_compute_shader);\r\n\t\t}\r\n\r\n\t\tif (error_msg.has_value())\r\n\t\t{\r\n\t\t\tLOGW(error_msg.value());\r\n\t\t\t//open_shader_compiler_popup = true;\r\n\t\t\t//shader_compiler_error = error_msg.value();\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\td3d12::RefinalizePipeline(n_pipeline);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::ReloadRTPipelineRegistryEntry(RegistryHandle handle)\r\n\t{\r\n\t\tauto& registry = RTPipelineRegistry::Get();\r\n\t\tstd::optional<std::string> error_msg = std::nullopt;\r\n\t\tauto n_pipeline = static_cast<d3d12::StateObject*>(registry.Find(handle));\r\n\r\n\t\tauto recompile_shader = [&error_msg, this](auto& pipeline_shader)\r\n\t\t{\r\n\t\t\tauto new_shader_variant = d3d12::LoadShader(m_device, pipeline_shader->m_type,\r\n\t\t\t\tpipeline_shader->m_path,\r\n\t\t\t\tpipeline_shader->m_entry,\r\n\t\t\t\tpipeline_shader->m_defines);\r\n\r\n\t\t\tif (std::holds_alternative<d3d12::Shader*>(new_shader_variant))\r\n\t\t\t{\r\n\t\t\t\tpipeline_shader = std::get<d3d12::Shader*>(new_shader_variant);\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\terror_msg = std::get<std::string>(new_shader_variant);\r\n\t\t\t}\r\n\t\t};\r\n\r\n\t\t// Library Shader\r\n\t\t{\r\n\t\t\trecompile_shader(n_pipeline->m_desc.m_library);\r\n\t\t}\r\n\r\n\t\tif (error_msg.has_value())\r\n\t\t{\r\n\t\t\tLOGW(error_msg.value());\r\n\t\t\t//open_shader_compiler_popup = true;\r\n\t\t\t//shader_compiler_error = error_msg.value();\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\td3d12::RecreateStateObject(n_pipeline);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::ReloadShaderRegistryEntry(RegistryHandle handle)\r\n\t{\r\n\t\tauto& registry = ShaderRegistry::Get();\r\n\t\tstd::optional<std::string> error_msg = std::nullopt;\r\n\t\tauto n_shader = static_cast<d3d12::Shader*>(registry.Find(handle));\r\n\r\n\t\tauto new_shader_variant = d3d12::LoadShader(m_device, n_shader->m_type,\r\n\t\t\tn_shader->m_path,\r\n\t\t\tn_shader->m_entry);\r\n\r\n\t\tif (std::holds_alternative<d3d12::Shader*>(new_shader_variant))\r\n\t\t{\r\n\t\t\td3d12::Destroy(n_shader);\r\n\t\t\tn_shader = std::get<d3d12::Shader*>(new_shader_variant);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tLOGW(std::get<std::string>(new_shader_variant));\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::ReloadRootSignatureRegistryEntry(RegistryHandle handle)\r\n\t{\r\n\t\tauto& registry = RootSignatureRegistry::Get();\r\n\t\tauto n_root_signature = static_cast<d3d12::RootSignature*>(registry.Find(handle));\r\n\r\n\t\td3d12::RefinalizeRootSignature(n_root_signature, m_device);\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::PrepareRTPipelineRegistry()\r\n\t{\r\n\t\tauto& registry = RTPipelineRegistry::Get();\r\n\r\n\t\tfor (auto it : registry.m_descriptions)\r\n\t\t{\r\n\t\t\tauto desc = it.second;\r\n\t\t\tauto library = static_cast<d3d12::Shader*>(ShaderRegistry::Get().Find(desc.library_desc.shader_handle));\r\n\r\n\t\t\td3d12::desc::StateObjectDesc n_desc;\r\n\t\t\tn_desc.m_library = library;\r\n\t\t\tn_desc.m_library_exports = desc.library_desc.exports;\r\n\t\t\tn_desc.max_attributes_size = static_cast<std::uint32_t>(desc.max_attributes_size);\r\n\t\t\tn_desc.max_payload_size = static_cast<std::uint32_t>(desc.max_payload_size);\r\n\t\t\tn_desc.max_recursion_depth = static_cast<std::uint32_t>(desc.max_recursion_depth);\r\n\t\t\tn_desc.m_hit_groups = desc.library_desc.m_hit_groups;\r\n\r\n\t\t\tif (auto rt_handle = desc.global_root_signature.value(); desc.global_root_signature.has_value())\r\n\t\t\t{\r\n\t\t\t\tn_desc.global_root_signature = static_cast<d3d12::RootSignature*>(RootSignatureRegistry::Get().Find(rt_handle));\r\n\t\t\t}\r\n\r\n\t\t\tn_desc.local_root_signatures = std::vector<d3d12::RootSignature*>();\r\n\t\t\tfor (auto rt_handle : desc.local_root_signatures)\r\n\t\t\t{\r\n\t\t\t\tauto rs = static_cast<d3d12::RootSignature*>(RootSignatureRegistry::Get().Find(rt_handle));\r\n\t\t\t\tn_desc.local_root_signatures.value().push_back(rs);\r\n\t\t\t}\r\n\r\n\t\t\tauto n_state_object = d3d12::CreateStateObject(m_device, n_desc);\r\n\r\n\t\t\tregistry.m_objects.insert({ it.first, n_state_object });\r\n\t\t}\r\n\t}\r\n\r\n\tnamespace internal\r\n\t{\r\n\r\n\t\ttemplate<typename R, typename T>\r\n\t\tvoid DestroyGenericRegistry()\r\n\t\t{\r\n\t\t\tauto& registry = R::Get();\r\n\r\n\t\t\tfor (auto it : registry.m_objects)\r\n\t\t\t{\r\n\t\t\t\tauto native = static_cast<T*>(it.second);\r\n\t\t\t\tif (native)\r\n\t\t\t\t{\r\n\t\t\t\t\td3d12::Destroy(native);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t} /* internal */\r\n\r\n\tvoid D3D12RenderSystem::DestroyRootSignatureRegistry()\r\n\t{\r\n\t\tinternal::DestroyGenericRegistry<RootSignatureRegistry, d3d12::RootSignature>();\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::DestroyShaderRegistry()\r\n\t{\r\n\t\tinternal::DestroyGenericRegistry<ShaderRegistry, d3d12::Shader>();\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::DestroyPipelineRegistry()\r\n\t{\r\n\t\tinternal::DestroyGenericRegistry<PipelineRegistry, d3d12::PipelineState>();\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::DestroyRTPipelineRegistry()\r\n\t{\r\n\t\tinternal::DestroyGenericRegistry<RTPipelineRegistry, d3d12::StateObject>();\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::InitSceneGraph(SceneGraph& scene_graph)\r\n\t{\r\n\t\tauto frame_idx = GetFrameIdx();\r\n\t\td3d12::WaitFor(m_fences[frame_idx]);\r\n\t\td3d12::Begin(m_direct_cmd_list, frame_idx);\r\n\r\n\t\tscene_graph.Init();\r\n\r\n\t\td3d12::End(m_direct_cmd_list);\r\n\r\n\t\t// Execute\r\n\t\td3d12::Execute(m_direct_queue, { m_direct_cmd_list }, m_fences[frame_idx]);\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Init_MeshNodes(std::vector<std::shared_ptr<MeshNode>>& nodes)\r\n\t{\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Init_CameraNodes(std::vector<std::shared_ptr<CameraNode>>& nodes)\r\n\t{\r\n\t\tif (nodes.empty()) return;\r\n\r\n\t\tsize_t cam_align_size = SizeAlignTwoPower(nodes.size() * sizeof(temp::ProjectionView_CBData), 256) * d3d12::settings::num_back_buffers;\r\n\t\tm_camera_pool = CreateConstantBufferPool((size_t)std::ceil(cam_align_size));\r\n\r\n\t\tfor (auto& node : nodes)\r\n\t\t{\r\n\t\t\tnode->m_camera_cb = m_camera_pool->Create(sizeof(temp::ProjectionView_CBData));\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Init_LightNodes(std::vector<std::shared_ptr<LightNode>>& nodes, std::vector<Light>& lights)\r\n\t{\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Update_Transforms(SceneGraph& scene_graph, std::shared_ptr<Node>& node)\r\n\t{\r\n\r\n\t\tif (node->RequiresTransformUpdate(GetFrameIdx()))\r\n\t\t{\r\n\t\t\tnode->UpdateTransform();\r\n\t\t\tnode->SignalTransformUpdate(GetFrameIdx());\r\n\t\t}\r\n\r\n\t\tauto& children = node->m_children;\r\n\t\tauto it = children.begin();\r\n\r\n\t\twhile (it != children.end())\r\n\t\t{\r\n\t\t\tUpdate_Transforms(scene_graph, *it);\r\n\t\t\t++it;\r\n\t\t}\r\n\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Delete_Skybox(SceneGraph& scene_graph, std::shared_ptr<SkyboxNode>& skybox_node)\r\n\t{\r\n\t\tunsigned int frame_idx = GetFrameIdx();\r\n\r\n\t\tskybox_node->m_irradiance.value().m_pool->MarkForUnload(skybox_node->m_irradiance.value(), frame_idx);\r\n\t\tskybox_node->m_skybox.value().m_pool->MarkForUnload(skybox_node->m_skybox.value(), frame_idx);\r\n\t\tskybox_node->m_prefiltered_env_map.value().m_pool->MarkForUnload(skybox_node->m_prefiltered_env_map.value(), frame_idx);\r\n\r\n\t\tif (skybox_node->m_hdr.m_pool)\r\n\t\t{\r\n\t\t\tskybox_node->m_hdr.m_pool->MarkForUnload(skybox_node->m_hdr, frame_idx);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::PreparePreRenderCommands(bool clear_frame_buffer, int frame_idx)\r\n\t{\r\n\t\td3d12::Begin(m_direct_cmd_list, frame_idx);\r\n\r\n\t\tif (clear_frame_buffer)\r\n\t\t{\r\n\t\t\tCD3DX12_CPU_DESCRIPTOR_HANDLE rtv_descriptor(m_render_window.value()->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart());\r\n\r\n\t\t\trtv_descriptor.Offset(frame_idx, m_render_window.value()->m_rtv_descriptor_increment_size);\r\n\t\t}\r\n\r\n\t\tfor (int i = 0; i < m_structured_buffer_pools.size(); ++i)\r\n\t\t{\r\n\t\t\tm_structured_buffer_pools[i]->UpdateBuffers(m_direct_cmd_list, frame_idx);\r\n\t\t}\r\n\r\n\t\tfor (int i = 0; i < m_model_pools.size(); ++i)\r\n\t\t{\r\n\t\t\tm_model_pools[i]->StageMeshes(m_direct_cmd_list);\r\n\t\t}\r\n\r\n\t\tfor (auto pool : m_texture_pools)\r\n\t\t{\r\n\t\t\tpool->Stage(m_direct_cmd_list);\r\n\t\t}\r\n\r\n\t\td3d12::End(m_direct_cmd_list);\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Update_MeshNodes(std::vector<std::shared_ptr<MeshNode>>& nodes)\r\n\t{\r\n\t\tfor (auto& node : nodes)\r\n\t\t{\r\n\t\t\tif (!node->RequiresUpdate(GetFrameIdx()))\r\n\t\t\t{\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\r\n\t\t\tnode->Update(GetFrameIdx());\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Update_CameraNodes(std::vector<std::shared_ptr<CameraNode>>& nodes)\r\n\t{\r\n\t\tfor (auto& node : nodes)\r\n\t\t{\r\n\t\t\tif (!node->RequiresUpdate(GetFrameIdx()))\r\n\t\t\t{\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\r\n\t\t\tnode->UpdateTemp(GetFrameIdx());\r\n\r\n\t\t\ttemp::ProjectionView_CBData data;\r\n\t\t\tdata.m_projection = node->m_projection;\r\n\t\t\tdata.m_inverse_projection = node->m_inverse_projection;\r\n\t\t\tdata.m_prev_projection = node->m_prev_projection;\r\n\t\t\tdata.m_view = node->m_view;\r\n\t\t\tdata.m_inverse_view = node->m_inverse_view;\r\n\t\t\tdata.m_prev_view = node->m_prev_view;\r\n\t\t\t\r\n\t\t\tdata.m_is_hybrid = 0;\r\n\r\n\t\t\tnode->m_camera_cb->m_pool->Update(node->m_camera_cb, sizeof(temp::ProjectionView_CBData), 0, (uint8_t*)&data);\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Update_LightNodes(SceneGraph& scene_graph)\r\n\t{\r\n\t\tbool should_update = false;\r\n\t\tuint32_t offset_start = 0, offset_end = 0;\r\n\r\n\t\tstd::vector<std::shared_ptr<LightNode>>& light_nodes = scene_graph.GetLightNodes();\r\n\r\n\t\tfor (uint32_t i = 0, j = (uint32_t)light_nodes.size(); i < j; ++i)\r\n\t\t{\r\n\t\t\tstd::shared_ptr<LightNode>& node = light_nodes[i];\r\n\r\n\t\t\tif (!node->RequiresUpdate(GetFrameIdx()))\r\n\t\t\t{\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\r\n\t\t\tif (!should_update)\r\n\t\t\t{\r\n\t\t\t\tshould_update = true;\r\n\t\t\t\toffset_start = i;\r\n\t\t\t}\r\n\r\n\t\t\tnode->Update(GetFrameIdx());\r\n\r\n\t\t\toffset_end = i;\r\n\t\t}\r\n\r\n\t\tif (!should_update && !(offset_end == offset_start && offset_start == 0))\r\n\t\t\treturn;\r\n\r\n\t\t//Update light count\r\n\r\n\t\tscene_graph.GetLight(0)->tid &= 3;\r\n\t\tscene_graph.GetLight(0)->tid |= scene_graph.GetCurrentLightSize() << 2;\r\n\r\n\t\t//Update structured buffer\r\n\r\n\t\tStructuredBufferHandle* structured_buffer = scene_graph.GetLightBuffer();\r\n\r\n\t\tstructured_buffer->m_pool->Update(structured_buffer, scene_graph.GetLight(offset_start), sizeof(Light) * (offset_end - offset_start + 1), sizeof(Light) * offset_start);\r\n\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::Render_MeshNodes(temp::MeshBatches& batches, CameraNode* camera, CommandList* cmd_list)\r\n\t{\r\n\t\tauto n_cmd_list = static_cast<d3d12::CommandList*>(cmd_list);\r\n\t\tauto d3d12_camera_cb = static_cast<D3D12ConstantBufferHandle*>(camera->m_camera_cb);\r\n\t\r\n\t\td3d12::BindConstantBuffer(n_cmd_list, d3d12_camera_cb->m_native, 0, GetFrameIdx());\r\n\r\n\t\t//Render batches\r\n\t\tfor (auto& elem : batches)\r\n\t\t{\r\n\t\t\tauto model = elem.first.first;\r\n\t\t\tauto materials = elem.first.second;\r\n\t\t\ttemp::MeshBatch& batch = elem.second;\r\n\r\n\t\t\t//Bind object data\r\n\t\t\tauto d3d12_cb_handle = static_cast<D3D12ConstantBufferHandle*>(batch.batch_buffer);\r\n\t\t\td3d12::BindConstantBuffer(n_cmd_list, d3d12_cb_handle->m_native, 1, GetFrameIdx());\r\n\r\n\t\t\t//Render meshes\r\n\t\t\tfor (std::size_t mesh_i = 0; mesh_i < model->m_meshes.size(); mesh_i++)\r\n\t\t\t{\r\n\t\t\t\tauto mesh = model->m_meshes[mesh_i];\r\n\t\t\t\tauto n_mesh = static_cast<D3D12ModelPool*>(model->m_model_pool)->GetMeshData(mesh.first->id);\r\n\t\t\t\tif (model->m_model_pool != m_bound_model_pool || n_mesh->m_vertex_staging_buffer_stride != m_bound_model_pool_stride)\r\n\t\t\t\t{\r\n\t\t\t\t\tD3D12ModelPool* model_pool = static_cast<D3D12ModelPool*>(model->m_model_pool);\r\n\r\n\t\t\t\t\td3d12::BindVertexBuffer(n_cmd_list,\r\n\t\t\t\t\t\tmodel_pool->GetVertexStagingBuffer(),\r\n\t\t\t\t\t\t0,\r\n\t\t\t\t\t\tmodel_pool->GetVertexStagingBuffer()->m_size,\r\n\t\t\t\t\t\tn_mesh->m_vertex_staging_buffer_stride);\r\n\r\n\t\t\t\t\td3d12::BindIndexBuffer(n_cmd_list,\r\n\t\t\t\t\t\tmodel_pool->GetIndexStagingBuffer(),\r\n\t\t\t\t\t\t0,\r\n\t\t\t\t\t\tstatic_cast<std::uint32_t>(model_pool->GetIndexStagingBuffer()->m_size));\r\n\r\n\t\t\t\t\tm_bound_model_pool = static_cast<D3D12ModelPool*>(model->m_model_pool);\r\n\t\t\t\t\tm_bound_model_pool_stride = n_mesh->m_vertex_staging_buffer_stride;\r\n\t\t\t\t}\r\n\r\n\t\t\t\td3d12::BindDescriptorHeaps(n_cmd_list);\r\n\r\n\t\t\t\t// Pick the standard material or if available a user defined material.\r\n\t\t\t\tauto material_handle = mesh.second;\r\n\t\t\t\tif (materials.size() > mesh_i)\r\n\t\t\t\t{\r\n\t\t\t\t\tmaterial_handle = materials[mesh_i];\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (material_handle != m_last_material)\r\n\t\t\t\t{\r\n\t\t\t\t\tm_last_material = material_handle;\r\n\r\n\t\t\t\t\tBindMaterial(material_handle, cmd_list);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (n_mesh->m_index_count != 0)\r\n\t\t\t\t{\r\n\t\t\t\t\td3d12::DrawIndexed(n_cmd_list,\r\n\t\t\t\t\t\tstatic_cast<std::uint32_t>(n_mesh->m_index_count),\r\n\t\t\t\t\t\tbatch.num_instances,\r\n\t\t\t\t\t\tstatic_cast<std::uint32_t>(n_mesh->m_index_staging_buffer_offset),\r\n\t\t\t\t\t\tstatic_cast<std::uint32_t>(n_mesh->m_vertex_staging_buffer_offset));\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\td3d12::Draw(n_cmd_list, \r\n\t\t\t\t\t\tstatic_cast<std::uint32_t>(n_mesh->m_vertex_count), \r\n\t\t\t\t\t\tbatch.num_instances, \r\n\t\t\t\t\t\tstatic_cast<std::uint32_t>(n_mesh->m_vertex_staging_buffer_offset));\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t// Reset frame specific variables\r\n\t\tm_last_material.m_id = 0;\r\n\t\tm_last_material.m_pool = nullptr;\r\n\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::BindMaterial(MaterialHandle material_handle, CommandList* cmd_list)\r\n\t{\r\n\t\tauto n_cmd_list = static_cast<d3d12::CommandList*>(cmd_list);\r\n\r\n\t\tauto* material_internal = material_handle.m_pool->GetMaterial(material_handle);\r\n\r\n\t\tmaterial_internal->UpdateConstantBuffer();\r\n\r\n\t\tD3D12ConstantBufferHandle* handle = static_cast<D3D12ConstantBufferHandle*>(material_internal->GetConstantBufferHandle());\r\n\r\n\t\tauto albedo_handle = material_internal->GetTexture(TextureType::ALBEDO);\r\n\t\twr::d3d12::TextureResource* albedo_internal;\r\n\t\tif (albedo_handle.m_pool == nullptr)\r\n\t\t{\r\n\t\t\tauto default_albedo_handle = GetDefaultAlbedo();\r\n\t\t\talbedo_internal = static_cast<wr::d3d12::TextureResource*>(default_albedo_handle.m_pool->GetTextureResource(default_albedo_handle));\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\talbedo_internal = static_cast<wr::d3d12::TextureResource*>(albedo_handle.m_pool->GetTextureResource(albedo_handle));\r\n\t\t}\r\n\r\n\t\tauto normal_handle = material_internal->GetTexture(TextureType::NORMAL);\r\n\t\twr::d3d12::TextureResource* normal_internal;\r\n\t\tif (normal_handle.m_pool == nullptr)\r\n\t\t{\r\n\t\t\tauto default_normal_handle = GetDefaultNormal();\r\n\t\t\tnormal_internal = static_cast<wr::d3d12::TextureResource*>(default_normal_handle.m_pool->GetTextureResource(default_normal_handle));\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tnormal_internal = static_cast<wr::d3d12::TextureResource*>(normal_handle.m_pool->GetTextureResource(normal_handle));\r\n\t\t}\r\n\r\n\t\tauto roughness_handle = material_internal->GetTexture(TextureType::ROUGHNESS);\r\n\t\twr::d3d12::TextureResource* roughness_internal;\r\n\t\tif (roughness_handle.m_pool == nullptr)\r\n\t\t{\r\n\t\t\tauto default_roughness_handle = GetDefaultRoughness();\r\n\t\t\troughness_internal = static_cast<wr::d3d12::TextureResource*>(default_roughness_handle.m_pool->GetTextureResource(default_roughness_handle));\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\troughness_internal = static_cast<wr::d3d12::TextureResource*>(roughness_handle.m_pool->GetTextureResource(roughness_handle));\r\n\t\t}\r\n\r\n\t\tauto metallic_handle = material_internal->GetTexture(TextureType::METALLIC);\r\n\t\twr::d3d12::TextureResource* metallic_internal;\r\n\t\tif (metallic_handle.m_pool == nullptr)\r\n\t\t{\r\n\t\t\tauto default_metallic_handle = GetDefaultMetalic();\r\n\t\t\tmetallic_internal = static_cast<wr::d3d12::TextureResource*>(default_metallic_handle.m_pool->GetTextureResource(default_metallic_handle));\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tmetallic_internal = static_cast<wr::d3d12::TextureResource*>(metallic_handle.m_pool->GetTextureResource(metallic_handle));\r\n\t\t}\r\n\r\n\t\tauto emissive_handle = material_internal->GetTexture(TextureType::EMISSIVE);\r\n\t\twr::d3d12::TextureResource* emissive_internal;\r\n\t\tif (emissive_handle.m_pool == nullptr)\r\n\t\t{\r\n\t\t\tauto default_emissive_handle = GetDefaultEmissive();\r\n\t\t\temissive_internal = static_cast<wr::d3d12::TextureResource*>(default_emissive_handle.m_pool->GetTextureResource(default_emissive_handle));\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\temissive_internal = static_cast<wr::d3d12::TextureResource*>(emissive_handle.m_pool->GetTextureResource(emissive_handle));\r\n\t\t}\r\n\r\n\t\tauto ao_handle = material_internal->GetTexture(TextureType::AO);\r\n\t\twr::d3d12::TextureResource* ao_internal;\r\n\t\tif (ao_handle.m_pool == nullptr)\r\n\t\t{\r\n\t\t\tauto default_ao_handle = GetDefaultAO();\r\n\t\t\tao_internal = static_cast<wr::d3d12::TextureResource*>(default_ao_handle.m_pool->GetTextureResource(default_ao_handle));\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tao_internal = static_cast<wr::d3d12::TextureResource*>(ao_handle.m_pool->GetTextureResource(ao_handle));\r\n\t\t}\r\n\r\n\t\td3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::ALBEDO)), albedo_internal);\r\n\t\td3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::NORMAL)), normal_internal);\r\n\t\td3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::ROUGHNESS)), roughness_internal);\r\n\t\td3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::METALLIC)), metallic_internal);\r\n\t\td3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::EMISSIVE)), emissive_internal);\r\n\t\td3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::AMBIENT_OCCLUSION)), ao_internal);\r\n\r\n\t\td3d12::BindConstantBuffer(n_cmd_list, handle->m_native, 3, GetFrameIdx());\r\n\t}\r\n\r\n\tunsigned int D3D12RenderSystem::GetFrameIdx()\r\n\t{\r\n\t\tif (m_render_window.has_value())\r\n\t\t{\r\n\t\t\treturn m_render_window.value()->m_frame_idx;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tLOGW(\"Called `D3D12RenderSystem::GetFrameIdx` without a window!\");\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t}\r\n\r\n\td3d12::RenderWindow* D3D12RenderSystem::GetRenderWindow()\r\n\t{\r\n\t\tif (m_render_window.has_value())\r\n\t\t{\r\n\t\t\treturn m_render_window.value();\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tLOGW(\"Called `D3D12RenderSystem::GetRenderWindow` without a window!\");\r\n\t\t\treturn nullptr;\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::RequestSkyboxReload()\r\n\t{\r\n\t\tm_skybox_changed = true;\r\n\t}\r\n\r\n\twr::Model* D3D12RenderSystem::GetSimpleShape(SimpleShapes type)\r\n\t{\r\n\t\tif (type == SimpleShapes::COUNT)\r\n\t\t{\r\n\t\t\tLOGC(\"Nice try boiii! That's not a shape.\");\r\n\t\t}\r\n\r\n\t\treturn m_simple_shapes[static_cast<std::size_t>(type)];\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::ResetBatches(SceneGraph& sg)\r\n\t{\r\n\t\tfor (auto& batch : sg.GetBatches())\r\n\t\t{\r\n\t\t\tbatch.second.num_instances = 0;\r\n\t\t\tbatch.second.num_global_instances = 0;\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::LoadPrimitiveShapes()\r\n\t{\r\n\t\t// Load Cube.\r\n\t\t{\r\n\t\t\twr::MeshData<wr::Vertex> mesh;\r\n\r\n\t\t\tmesh.m_indices = {\r\n\t\t\t\t2, 1, 0, 3, 2, 0, 6, 5,\r\n\t\t\t\t4, 7, 6, 4, 10, 9, 8, 11,\r\n\t\t\t\t10, 8, 14, 13, 12, 15, 14, 12,\r\n\t\t\t\t18, 17, 16, 19, 18, 16, 22, 21,\r\n\t\t\t\t20, 23, 22, 20\r\n\t\t\t};\r\n\r\n\t\t\tmesh.m_vertices = {\r\n\t\t\t\t{ 1, 1, -1,\t\t1, 1,\t\t0, 0, -1,\t\t0, 0, 0,\t0, 0, 0 },\r\n\t\t\t\t{ 1, -1, -1,\t0, 1,\t\t0, 0, -1,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, -1, -1,\t0, 0,\t\t0, 0, -1,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, 1, -1,\t1, 0,\t\t0, 0, -1,\t\t0, 0, 0,\t0, 0, 0  },\r\n\r\n\t\t\t\t{ 1, 1, 1,\t\t1, 1,\t\t0, 0, 1,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, 1, 1,\t\t0, 1,\t\t0, 0, 1,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, -1, 1,\t0, 0,\t\t0, 0, 1,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ 1, -1, 1,\t\t1, 0,\t\t0, 0, 1,\t\t0, 0, 0,\t0, 0, 0  },\r\n\r\n\t\t\t\t{ 1, 1, -1,\t\t1, 0,\t\t1, 0, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ 1, 1, 1,\t\t1, 1,\t\t1, 0, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ 1, -1, 1,\t\t0, 1,\t\t1, 0, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ 1, -1, -1,\t0, 0,\t\t1, 0, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\r\n\t\t\t\t{ 1, -1, -1,\t1, 0,\t\t0, -1, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ 1, -1, 1,\t\t1, 1,\t\t0, -1, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, -1, 1,\t0, 1,\t\t0, -1, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, -1, -1,\t0, 0,\t\t0, -1, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\r\n\t\t\t\t{ -1, -1, -1,\t0, 1,\t\t-1, 0, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, -1, 1,\t0, 0,\t\t-1, 0, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, 1, 1,\t\t1, 0,\t\t-1, 0, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, 1, -1,\t1, 1,\t\t-1, 0, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\r\n\t\t\t\t{ 1, 1, 1,\t\t1, 0,\t\t0, 1, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ 1, 1, -1,\t\t1, 1,\t\t0, 1, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, 1, -1,\t0, 1,\t\t0, 1, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t\t{ -1, 1, 1,\t\t0, 0,\t\t0, 1, 0,\t\t0, 0, 0,\t0, 0, 0  },\r\n\t\t\t};\r\n\r\n\t\t\tm_simple_shapes[static_cast<std::size_t>(SimpleShapes::CUBE)] = m_shapes_pool->LoadCustom<wr::Vertex>({ mesh });\r\n\t\t}\r\n\r\n\t\t{\r\n\t\t\twr::MeshData<wr::Vertex> mesh;\r\n\r\n\t\t\tmesh.m_indices = {\r\n\t\t\t\t2, 1, 0, 3, 2, 0\r\n\t\t\t};\r\n\r\n\t\t\tmesh.m_vertices = {\r\n\t\t\t\t//POS\t\t\t\tUV\t\t\tNORMAL\t\t\t\tTANGENT\t\t\tBINORMAL\t\tCOLOR\r\n\t\t\t\t{  1,  1,  0,\t\t1, 1,\t\t0, 0, -1,\t\t\t0, 0, 1,\t\t0, 1, 0 },\r\n\t\t\t\t{  1, -1,  0,\t\t1, 0,\t\t0, 0, -1,\t\t\t0, 0, 1,\t\t0, 1, 0 },\r\n\t\t\t\t{ -1, -1,  0,\t\t0, 0,\t\t0, 0, -1,\t\t\t0, 0, 1,\t\t0, 1, 0 },\r\n\t\t\t\t{ -1,  1,  0,\t\t0, 1,\t\t0, 0, -1,\t\t\t0, 0, 1,\t\t0, 1, 0 },\r\n\t\t\t};\r\n\r\n\t\t\tm_simple_shapes[static_cast<std::size_t>(SimpleShapes::PLANE)] = m_shapes_pool->LoadCustom<wr::Vertex>({ mesh });\r\n\t\t}\r\n\t}\r\n\r\n\tvoid D3D12RenderSystem::CreateDefaultResources()\r\n\t{\r\n\t\tauto default_texture_pool = m_texture_pools.at(0);\r\n\r\n\t\tm_default_cubemap = default_texture_pool->CreateCubemap(\"DefaultResource_Cubemap\", 2, 2, 1, wr::Format::R8G8B8A8_UNORM, false);\r\n\r\n\t\tm_default_albedo = default_texture_pool->LoadFromFile(settings::default_albedo_path, false, false);\r\n\t\tm_default_normal = default_texture_pool->LoadFromFile(settings::default_normal_path, false, false);\r\n\t\tm_default_white = default_texture_pool->LoadFromFile(settings::default_white_texture, false, false);\r\n\t\tm_default_black = default_texture_pool->LoadFromFile(settings::default_black_texture, false, false);\r\n\t}\r\n\r\n} /*  */\r\n"
  },
  {
    "path": "src/d3d12/d3d12_renderer.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../renderer.hpp\"\n\n#include <DirectXMath.h>\n\n#include \"../scene_graph/scene_graph.hpp\"\n#include \"../scene_graph/light_node.hpp\"\n#include \"../vertex.hpp\"\n#include \"d3d12_structs.hpp\"\n\n\nnamespace wr\n{\n\tnamespace d3d12\n\t{\n\t\tstruct CommandList;\n\t\tstruct RenderTarget;\n\t}\n  \n\tstruct MeshNode;\n\tstruct CameraNode;\n\tstruct D3D12ConstantBufferHandle;\n\n\tclass D3D12StructuredBufferPool;\n\tclass D3D12ModelPool;\n\tclass D3D12TexturePool;\n\tclass DynamicDescriptorHeap;\n\n\tnamespace temp\n\t{\n\t\tstruct ProjectionView_CBData\n\t\t{\n\t\t\tDirectX::XMMATRIX m_view = DirectX::XMMatrixIdentity();\n\n\t\t\tDirectX::XMMATRIX m_projection = DirectX::XMMatrixIdentity();\n\n\t\t\tDirectX::XMMATRIX m_inverse_projection = DirectX::XMMatrixIdentity();\n\n\t\t\tDirectX::XMMATRIX m_inverse_view = DirectX::XMMatrixIdentity();\n\n\t\t\tDirectX::XMMATRIX m_prev_projection = DirectX::XMMatrixIdentity();\n\n\t\t\tDirectX::XMMATRIX m_prev_view = DirectX::XMMatrixIdentity();\n\n\t\t\tunsigned int m_is_hybrid = 0u;\n\t\t\tunsigned int m_is_path_tracer = 0u;\n\t\t\tunsigned int m_is_ao = 0u;\n\t\t\tunsigned int m_has_shadows = 0u;\n\n\t\t\tint padding[3];\n\t\t\tunsigned int m_has_reflections = 0u;\n\t\t};\n\n\t\tstruct ShadowDenoiserSettings_CBData\n\t\t{\n\t\t\tfloat m_alpha;\n\t\t\tfloat m_moments_alpha;\n\t\t\tfloat m_l_phi;\n\t\t\tfloat m_n_phi;\n\t\t\tfloat m_z_phi;\n\t\t\tfloat m_step_distance;\n\t\t\tfloat m_padding[2];\n\t\t};\n\n\t\tstruct ReflectionDenoiserSettings_CBData\n\t\t{\n\t\t\tfloat m_color_integration_alpha;\n\t\t\tfloat m_moments_integration_alpha;\n\t\t\tfloat m_variance_clipping_sigma;\n\t\t\tfloat m_roughness_reprojection_threshold;\n\t\t\tint m_max_history_samples;\n\t\t\tfloat m_n_phi;\n\t\t\tfloat m_z_phi;\n\t\t\tfloat m_l_phi;\n\t\t};\n\n\t\tstruct DenoiserCamera_CBData\n\t\t{\n\t\t\tDirectX::XMMATRIX m_view;\n\t\t\tDirectX::XMMATRIX m_prev_view;\n\t\t\tDirectX::XMMATRIX m_inverse_view;\n\t\t\tDirectX::XMMATRIX m_projection;\n\t\t\tDirectX::XMMATRIX m_prev_projection;\n\t\t\tDirectX::XMMATRIX m_inverse_projection;\n\t\t\tfloat m_padding[2];\n\t\t\tfloat m_near_plane;\n\t\t\tfloat m_far_plane;\n\t\t};\n\n\t\tstruct RTHybridCamera_CBData\n\t\t{\n\t\t\tDirectX::XMMATRIX m_inverse_view = DirectX::XMMatrixIdentity();\n\t\t\tDirectX::XMMATRIX m_inverse_projection = DirectX::XMMatrixIdentity();\n\t\t\tDirectX::XMMATRIX m_inv_vp = DirectX::XMMatrixIdentity();\n\n\t\t\tfloat m_frame_idx = 0.0f;\n\t\t\tfloat m_intensity = 0.0f;\n\t\t\tfloat m_epsilon = 0.01f;\n\t\t\tstd::uint32_t m_sample_count = 1u;\n\t\t};\n\n\t\tstruct RTAO_CBData\n\t\t{\n\t\t\tDirectX::XMMATRIX m_inv_vp = DirectX::XMMatrixIdentity();\n\t\t\tDirectX::XMMATRIX m_inv_view = DirectX::XMMatrixIdentity();\n\n\t\t\tfloat m_bias = 0.0f;\n\t\t\tfloat m_radius = 0.0f;\n\t\t\tfloat m_power = 0.0f;\n\t\t\tfloat m_max_distance = 0.0f;\n\n\t\t\tfloat m_padding[2];\n\t\t\tfloat m_frame_idx = 0.0f;\n\t\t\tstd::uint32_t m_sample_count = 0u;\n\t\t};\n\n\t\tstruct RayTracingCamera_CBData\n\t\t{\n\t\t\tDirectX::XMMATRIX m_view = DirectX::XMMatrixIdentity();\n\t\t\tDirectX::XMMATRIX m_inverse_view_projection = DirectX::XMMatrixIdentity();\n\t\t\tDirectX::XMVECTOR m_camera_position = DirectX::XMVectorZero();\n\n\t\t\tfloat focal_radius = 0.0f;\n\t\t\tfloat focal_length = 0.0f;\n\t\t\tfloat frame_idx = 0.0f;\n\t\t\tfloat intensity = 0.0f;\n\t\t};\n\n\t\tstruct RayTracingMaterial_CBData\n\t\t{\n\t\t\tstd::uint32_t albedo_id = 0u;\n\t\t\tstd::uint32_t normal_id = 0u;\n\t\t\tstd::uint32_t roughness_id = 0u;\n\t\t\tstd::uint32_t metallicness_id = 0u;\n\t\t\tstd::uint32_t emissive_id = 0u;\n\t\t\tstd::uint32_t ao_id = 0u;\n\n\t\t\tstd::uint32_t padding[2] = { 0u };\n\t\t\tMaterial::MaterialData material_data;\n\t\t};\n\n\t\tstruct RayTracingOffset_CBData\n\t\t{\n\t\t\tstd::uint32_t material_idx = 0u;\n\t\t\tstd::uint32_t idx_offset = 0u;\n\t\t\tstd::uint32_t vertex_offset = 0u;\n\t\t};\n\n\n\n\t\tstatic const constexpr float size = 1.0f;\n\t\tstatic const constexpr Vertex2D quad_vertices[] = {\n\t\t\t{ -size, -size },\n\t\t\t{ size, -size },\n\t\t\t{ -size, size },\n\t\t\t{ size, size },\n\t\t};\n\n\t} /* temp */\n\n\tclass D3D12RenderSystem final : public RenderSystem\n\t{\n\tpublic:\n\t\t~D3D12RenderSystem();\n\n\t\tvoid Init(std::optional<Window*> window);\n\t\tCPUTextures Render(SceneGraph& scene_graph, FrameGraph& frame_graph);\n\t\tvoid Resize(std::uint32_t width, std::uint32_t height);\n\n\t\tstd::shared_ptr<TexturePool> CreateTexturePool();\n\t\tstd::shared_ptr<MaterialPool> CreateMaterialPool(std::size_t size_in_bytes);\n\t\tstd::shared_ptr<ModelPool> CreateModelPool(std::size_t vertex_buffer_pool_size_in_bytes, std::size_t index_buffer_pool_size_in_bytes);\n\t\tstd::shared_ptr<ConstantBufferPool> CreateConstantBufferPool(std::size_t size_in_bytes);\n\t\tstd::shared_ptr<StructuredBufferPool> CreateStructuredBufferPool(std::size_t size_in_bytes);\n\n\t\tstd::shared_ptr<TexturePool> GetDefaultTexturePool();\n\n\t\tvoid PrepareRootSignatureRegistry();\n\t\tvoid PrepareShaderRegistry();\n\t\tvoid PreparePipelineRegistry();\n\t\tvoid PrepareRTPipelineRegistry();\n\t\tvoid ReloadPipelineRegistryEntry(RegistryHandle handle);\n\t\tvoid ReloadRTPipelineRegistryEntry(RegistryHandle handle);\n\t\tvoid ReloadShaderRegistryEntry(RegistryHandle handle);\n\t\tvoid ReloadRootSignatureRegistryEntry(RegistryHandle handle);\n\t\tvoid DestroyRootSignatureRegistry();\n\t\tvoid DestroyShaderRegistry();\n\t\tvoid DestroyPipelineRegistry();\n\t\tvoid DestroyRTPipelineRegistry();\n\n\t\tvoid WaitForAllPreviousWork();\n\n\t\twr::CommandList* GetDirectCommandList(unsigned int num_allocators);\n\t\twr::CommandList* GetBundleCommandList(unsigned int num_allocators);\n\t\twr::CommandList* GetComputeCommandList(unsigned int num_allocators);\n\t\twr::CommandList* GetCopyCommandList(unsigned int num_allocators);\n\t\tvoid SetCommandListName(CommandList* cmd_list, std::wstring const& name);\n\t\tvoid DestroyCommandList(CommandList* cmd_list);\n\t\tRenderTarget* GetRenderTarget(RenderTargetProperties properties);\n\t\tvoid SetRenderTargetName(RenderTarget* cmd_list, std::wstring const& name);\n\t\tvoid ResizeRenderTarget(RenderTarget** render_target, std::uint32_t width, std::uint32_t height);\n\t\tvoid RequestFullscreenChange(bool fullscreen_state);\n\t\tvoid DestroyRenderTarget(RenderTarget **render_target) override;\n\n\t\tvoid ResetCommandList(CommandList* cmd_list);\n\t\tvoid CloseCommandList(CommandList* cmd_list);\n\t\tvoid StartRenderTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target);\n\t\tvoid StopRenderTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target);\n\t\tvoid StartComputeTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target);\n\t\tvoid StopComputeTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target);\n\t\tvoid StartCopyTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target);\n\t\tvoid StopCopyTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target);\n\n\t\tvoid InitSceneGraph(SceneGraph& scene_graph);\n\n\t\tvoid Init_MeshNodes(std::vector<std::shared_ptr<MeshNode>>& nodes);\n\t\tvoid Init_CameraNodes(std::vector<std::shared_ptr<CameraNode>>& nodes);\n\t\tvoid Init_LightNodes(std::vector<std::shared_ptr<LightNode>>& nodes, std::vector<Light>& lights);\n\n\t\tvoid Update_MeshNodes(std::vector<std::shared_ptr<MeshNode>>& nodes);\n\t\tvoid Update_CameraNodes(std::vector<std::shared_ptr<CameraNode>>& nodes);\n\t\tvoid Update_LightNodes(SceneGraph& scene_graph);\n\t\tvoid Update_Transforms(SceneGraph& scene_graph, std::shared_ptr<Node>& node);\n\t\tvoid Delete_Skybox(SceneGraph& scene_graph, std::shared_ptr<SkyboxNode>& skybox_node);\n\n\t\tvoid PreparePreRenderCommands(bool clear_frame_buffer, int frame_idx);\n\n\t\tvoid Render_MeshNodes(temp::MeshBatches& batches, CameraNode* camera, CommandList* cmd_list);\n\t\tvoid BindMaterial(MaterialHandle material_handle, CommandList* cmd_list);\n\n\t\tunsigned int GetFrameIdx();\n\t\td3d12::RenderWindow* GetRenderWindow();\n\n\t\tvoid RequestSkyboxReload(); \n\n\t\t//SimpleShapes don't have a material attached to them. The user is expected to provide one.\n\t\twr::Model* GetSimpleShape(SimpleShapes type);\n\n\tpublic:\n\t\td3d12::Device* m_device;\n\t\tstd::optional<d3d12::RenderWindow*> m_render_window;\n\t\td3d12::CommandQueue* m_direct_queue;\n\t\td3d12::CommandQueue* m_compute_queue;\n\t\td3d12::CommandQueue* m_copy_queue;\n\t\tstd::array<d3d12::Fence*, d3d12::settings::num_back_buffers> m_fences;\n\n\t\td3d12::Viewport m_viewport;\n\t\td3d12::CommandList* m_direct_cmd_list;\n\t\td3d12::StagingBuffer* m_fullscreen_quad_vb;\n\n\t\tstd::vector<std::uint64_t> m_buffer_frame_graph_uids;\n\n\t\tstd::vector <std::shared_ptr<TexturePool>> m_texture_pools;\n\n\t\td3d12::HeapResource* m_light_buffer;\n\t\tstd::shared_ptr<ConstantBufferPool> m_camera_pool; // TODO: Should be part of the scene graph\n\n\t\tstd::shared_ptr<ConstantBufferPool> m_raytracing_cb_pool;\n\t\tstd::shared_ptr<StructuredBufferPool> m_raytracing_material_sb_pool;\n\t\tstd::shared_ptr<StructuredBufferPool> m_raytracing_offset_sb_pool;\n\n\t\tstd::vector<std::shared_ptr<D3D12StructuredBufferPool>> m_structured_buffer_pools;\n\t\tstd::vector<std::shared_ptr<D3D12ModelPool>> m_model_pools;\n\t\tD3D12ModelPool* m_bound_model_pool;\n\t\tstd::size_t m_bound_model_pool_stride;\n\n\t\tstd::optional<wr::TextureHandle> m_brdf_lut = std::nullopt;\n\t\tbool m_brdf_lut_generated = false;\n    \n\t\tfloat temp_metal = 1.0f;\n\t\tfloat temp_rough = -3;\n\t\tbool clear_path = false;\n\t\tfloat light_radius = 50;\n\t\tfloat temp_intensity = 1;\n\n\tprotected:\n\t\tvoid SaveRenderTargetToDisc(std::string const& path, RenderTarget* render_target, unsigned int index);\n\n\tprivate:\n\t\tvoid ResetBatches(SceneGraph& sg);\n\t\tvoid LoadPrimitiveShapes();\n\t\tvoid CreateDefaultResources();\n\n\t\td3d12::CommandSignature* m_cmd_signature;\n\t\td3d12::CommandSignature* m_cmd_signature_indexed;\n\n\t\tstd::optional<bool> m_requested_fullscreen_state;\n\n\t\tMaterialHandle m_last_material = { nullptr, 0 };\n\n\t\tbool m_skybox_changed = false;\n\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/d3d12/d3d12_resource_pool_texture.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n/*\nThe mipmapping implementation used in this framework is a ported version of \nMiniEngine's implementation.\n*/\n/*\nThe MIT License(MIT)\n\nCopyright(c) 2013 - 2015 Microsoft\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files(the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions :\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n*/\n\n#include \"d3d12_resource_pool_texture.hpp\"\n\n#include \"d3d12_functions.hpp\"\n#include \"d3d12_defines.hpp\"\n#include \"d3d12_renderer.hpp\"\n#include \"d3d12_defines.hpp\"\n\n#include \"../renderer.hpp\"\n#include \"../settings.hpp\"\n#include \"d3d12_renderer.hpp\"\n#include \"d3d12_structs.hpp\"\n#include \"../pipeline_registry.hpp\"\n#include \"d3d12_descriptors_allocations.hpp\"\n\n#include <DirectXTex.h>\n#include <comdef.h>\n\n\nnamespace wr\n{\n\tD3D12TexturePool::D3D12TexturePool(D3D12RenderSystem& render_system)\n\t\t: TexturePool()\n\t\t, m_render_system(render_system)\n\t{\n\t\tauto device = m_render_system.m_device;\n\n\t\tm_staging_textures.resize(d3d12::settings::num_back_buffers);\n\n\t\t//Staging heap\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tm_allocators[i] = new DescriptorAllocator(render_system, static_cast<DescriptorHeapType>(i));\n\t\t}\n\n\t\tm_mipmapping_allocator = new DescriptorAllocator(render_system, DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\n\t\t// Default UAVs\n\t\tm_default_uav = m_mipmapping_allocator->Allocate(4);\n\n\t\tfor (uint8_t i = 0; i < 4; ++i)\n\t\t{\n\t\t\tD3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};\n\t\t\tuavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;\n\t\t\tuavDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;\n\t\t\tuavDesc.Texture2D.MipSlice = i;\n\t\t\tuavDesc.Texture2D.PlaneSlice = 0;\n\n\t\t\td3d12::DescHeapCPUHandle handle = m_default_uav.GetDescriptorHandle(i);\n\t\t\tdevice->m_native->CreateUnorderedAccessView(nullptr, nullptr, &uavDesc, handle.m_native);\n\t\t}\n\t}\n\n\tD3D12TexturePool::~D3D12TexturePool()\n\t{\n\t\t{\n\t\t\t//Let the allocation go out of scope to clear it before the texture pool and its allocators are destroyed\n\t\t\tDescriptorAllocation alloc = std::move(m_default_uav);\n\t\t}\n\t\t\n\t\tdelete m_mipmapping_allocator;\n\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tdelete m_allocators[i];\n\t\t}\n\n\t\twhile (m_unstaged_textures.size() > 0)\n\t\t{\n\t\t\tuint64_t texture_id = m_unstaged_textures.begin()->first;\n\n\t\t\td3d12::TextureResource* texture = static_cast<d3d12::TextureResource*>(m_unstaged_textures.at(texture_id).first);\n\t\t\tm_unstaged_textures.erase(texture_id);\n\n\t\t\tSAFE_RELEASE(texture->m_resource);\n\t\t\tSAFE_RELEASE(texture->m_intermediate);\n\n\t\t\tdelete texture;\n\t\t}\n\t\t\n\t\twhile(m_staged_textures.size() > 0)\n\t\t{\n\t\t\tTextureHandle handle = { this, static_cast<uint32_t>(m_staged_textures.begin()->first) };\n\t\t\tD3D12TexturePool::MarkForUnload(handle, 0);\n\t\t}\n\n\t\tD3D12TexturePool::UnloadTextures(0);\n\t}\n\n\tvoid D3D12TexturePool::Evict()\n\t{\n\t}\n\n\tvoid D3D12TexturePool::MakeResident()\n\t{\n\t}\n\n\tvoid D3D12TexturePool::Stage(CommandList* cmd_list)\n\t{\n\t\tsize_t unstaged_number = m_unstaged_textures.size();\n\n\t\tif (unstaged_number > 0)\n\t\t{\n\t\t\td3d12::CommandList* cmdlist = static_cast<d3d12::CommandList*>(cmd_list);\n\n\t\t\tstd::vector<d3d12::TextureResource*> unstaged_textures;\n\t\t\tstd::vector<d3d12::TextureResource*> need_mipmapping;\n\n\t\t\tauto itr = m_unstaged_textures.begin();\n\n\t\t\tfor (itr; itr != m_unstaged_textures.end(); ++itr)\n\t\t\t{\n\t\t\t\td3d12::TextureResource* texture = static_cast<d3d12::TextureResource*>(itr->second.first);\n\n\t\t\t\tunstaged_textures.push_back(texture);\n\n\t\t\t\tdecltype(d3d12::Device::m_native) n_device;\n\t\t\t\ttexture->m_resource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\n\t\t\t\tDirectX::ScratchImage* image = itr->second.second;\n\n\t\t\t\ttexture->m_subresources.resize(image->GetImageCount());\n\t\t\t\tconst DirectX::Image* pImages = image->GetImages();\n\t\t\t\tfor (int i = 0; i < image->GetImageCount(); ++i)\n\t\t\t\t{\n\t\t\t\t\tauto& subresource = texture->m_subresources[i];\n\t\t\t\t\tsubresource.RowPitch = pImages[i].rowPitch;\n\t\t\t\t\tsubresource.SlicePitch = pImages[i].slicePitch;\n\t\t\t\t\tsubresource.pData = pImages[i].pixels;\n\t\t\t\t}\n\n\n\t\t\t\tstd::vector<D3D12_SUBRESOURCE_DATA> subresources = texture->m_subresources;\n\t\t\t\tUpdateSubresources(cmdlist->m_native, texture->m_resource, texture->m_intermediate, 0, 0, static_cast<uint32_t>(subresources.size()), subresources.data());\n\n\t\t\t\ttexture->m_is_staged = true;\n\n\t\t\t\tif (texture->m_need_mips)\n\t\t\t\t{\n\t\t\t\t\tneed_mipmapping.push_back(texture);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\td3d12::Transition(cmdlist, unstaged_textures, wr::ResourceState::COPY_DEST, wr::ResourceState::PIXEL_SHADER_RESOURCE);\n\n\t\t\tfor (auto* t : need_mipmapping)\n\t\t\t{\n\t\t\t\tGenerateMips(t, cmd_list);\n\t\t\t}\n\n\t\t\tMoveStagedTextures(m_render_system.GetFrameIdx());\n\t\t}\n\t}\n\n\tvoid D3D12TexturePool::PostStageClear()\n\t{\n\t\t\n\t}\n\n\tvoid D3D12TexturePool::ReleaseTemporaryResources()\n\t{\n\t\tm_mipmapping_allocator->ReleaseStaleDescriptors();\n\n\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t{\n\t\t\tm_allocators[i]->ReleaseStaleDescriptors();\n\t\t}\n\n\t\tunsigned int frame_idx = m_render_system.GetFrameIdx();\n\n\t\t//Release temporary textures\n\t\tfor (auto* t : m_temporary_textures[frame_idx])\n\t\t{\n\t\t\td3d12::Destroy(t);\n\t\t}\n\n\t\tm_temporary_textures[frame_idx].clear();\n\n\t\t//Release temporary heaps\n\t\tfor (auto* h : m_temporary_heaps[frame_idx])\n\t\t{\n\t\t\td3d12::Destroy(h);\n\t\t}\n\n\n\t\tfor (auto& map : m_staging_textures[frame_idx])\n\t\t{\n\t\t\tauto* texture = (d3d12::TextureResource*) map.second;\n\n\t\t\tif (texture->m_intermediate)\n\t\t\t\tSAFE_RELEASE(texture->m_intermediate);\n\n\t\t\t((d3d12::TextureResource*)m_staged_textures[map.first])->m_intermediate = nullptr;\n\t\t}\n\n\t\tm_staging_textures[frame_idx].clear();\n\t\tm_temporary_heaps[frame_idx].clear();\n\t}\n\n\td3d12::TextureResource* D3D12TexturePool::GetTextureResource(TextureHandle handle)\n\t{\n\t\treturn static_cast<d3d12::TextureResource*>(m_staged_textures.at(handle.m_id));\n\t}\n\n\tTextureHandle D3D12TexturePool::CreateCubemap(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest)\n\t{\n\t\tauto device = m_render_system.m_device;\n\n\t\td3d12::desc::TextureDesc desc;\n\n\t\tuint32_t mip_lvl_final = mip_levels;\n\n\t\t// 0 means generate max number of mip levels\n\t\tif (mip_lvl_final == 0)\n\t\t{\n\t\t\tmip_lvl_final = static_cast<uint32_t>(std::floor(std::log2(std::max(width, height)))) + 1;\n\t\t}\n\n\t\tdesc.m_width = width;\n\t\tdesc.m_height = height;\n\t\tdesc.m_is_cubemap = true;\n\t\tdesc.m_depth = 1;\n\t\tdesc.m_array_size = 6;\n\t\tdesc.m_mip_levels = mip_lvl_final;\n\t\tdesc.m_texture_format = format;\n\t\tdesc.m_initial_state = allow_render_dest ? ResourceState::RENDER_TARGET : ResourceState::COPY_DEST;\n\n\t\td3d12::TextureResource* texture = d3d12::CreateTexture(device, &desc, true);\n\n\t\ttexture->m_allow_render_dest = allow_render_dest;\n\t\ttexture->m_is_staged = true;\n\t\ttexture->m_need_mips = false;\n\n\t\tstd::wstring wide_string(name.begin(), name.end());\n\n\t\td3d12::SetName(texture, wide_string);\n\n\t\tDescriptorAllocation srv_alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate();\n\t\tDescriptorAllocation uav_alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate();\n\n\t\tif (srv_alloc.IsNull())\n\t\t{\n\t\t\tLOGC(\"Couldn't allocate descriptor for the texture resource\");\n\t\t}\n\t\tif (uav_alloc.IsNull())\n\t\t{\n\t\t\tLOGC(\"Couldn't allocate descriptor for the texture resource\");\n\t\t}\n\n\t\ttexture->m_srv_allocation = std::move(srv_alloc);\n\t\ttexture->m_uav_allocation = std::move(uav_alloc);\n\n\t\td3d12::CreateSRVFromTexture(texture);\n\n\t\tif (allow_render_dest)\n\t\t{\n\t\t\tDescriptorAllocation alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_RTV]->Allocate(6);\n\n\t\t\tif (alloc.IsNull())\n\t\t\t{\n\t\t\t\tLOGC(\"Couldn't allocate descriptor for the texture resource\");\n\t\t\t}\n\n\t\t\ttexture->m_rtv_allocation = std::move(alloc);\n\n\t\t\td3d12::CreateRTVFromCubemap(texture);\n\t\t}\n\n\t\tm_loaded_textures++;\n\n\t\tuint64_t texture_id = m_id_factory.GetUnusedID();\n\n\t\tTextureHandle texture_handle;\n\t\ttexture_handle.m_pool = this;\n\t\ttexture_handle.m_id = static_cast<std::uint32_t>(texture_id);\n\n\t\tm_staged_textures.insert(std::make_pair(texture_id, texture));\n\n\t\treturn texture_handle;\n\t}\n\n\tTextureHandle D3D12TexturePool::CreateTexture(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest)\n\t{\n\t\tauto device = m_render_system.m_device;\n\n\t\td3d12::desc::TextureDesc desc;\n\n\t\tuint32_t mip_lvl_final = mip_levels;\n\n\t\t// 0 means generate max number of mip levels\n\t\tif (mip_lvl_final == 0)\n\t\t{\n\t\t\tmip_lvl_final = static_cast<uint32_t>(std::floor(std::log2(std::max(width, height)))) + 1;\n\t\t}\n\n\t\tdesc.m_width = width;\n\t\tdesc.m_height = height;\n\t\tdesc.m_is_cubemap = false;\n\t\tdesc.m_depth = 1;\n\t\tdesc.m_array_size = 1;\n\t\tdesc.m_mip_levels = mip_lvl_final;\n\t\tdesc.m_texture_format = format;\n\t\tdesc.m_initial_state = allow_render_dest ? ResourceState::RENDER_TARGET : ResourceState::COPY_DEST;\n\n\t\td3d12::TextureResource* texture = d3d12::CreateTexture(device, &desc, true);\n\n\t\ttexture->m_allow_render_dest = allow_render_dest;\n\t\ttexture->m_is_staged = true;\n\t\ttexture->m_need_mips = false;\n\n\t\tstd::wstring wide_string(name.begin(), name.end());\n\n\t\td3d12::SetName(texture, wide_string);\n\n\t\tDescriptorAllocation srv_alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate();\n\t\tDescriptorAllocation uav_alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate();\n\n\t\tif (srv_alloc.IsNull())\n\t\t{\n\t\t\tLOGC(\"Couldn't allocate descriptor for the texture resource\");\n\t\t}\n\t\tif (uav_alloc.IsNull())\n\t\t{\n\t\t\tLOGC(\"Couldn't allocate descriptor for the texture resource\");\n\t\t}\n\n\t\ttexture->m_srv_allocation = std::move(srv_alloc);\n\t\ttexture->m_uav_allocation = std::move(uav_alloc);\n\n\t\td3d12::CreateSRVFromTexture(texture);\n\t\td3d12::CreateUAVFromTexture(texture, 0);\n\n\t\tif (allow_render_dest)\n\t\t{\n\t\t\tDescriptorAllocation alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_RTV]->Allocate(6);\n\n\t\t\tif (alloc.IsNull())\n\t\t\t{\n\t\t\t\tLOGC(\"Couldn't allocate descriptor for the texture resource\");\n\t\t\t}\n\n\t\t\ttexture->m_rtv_allocation = std::move(alloc);\n\n\t\t\td3d12::CreateRTVFromTexture2D(texture);\n\t\t}\n\n\t\tm_loaded_textures++;\n\n\t\tuint64_t texture_id = m_id_factory.GetUnusedID();\n\n\t\tTextureHandle texture_handle;\n\t\ttexture_handle.m_pool = this;\n\t\ttexture_handle.m_id = static_cast<std::uint32_t>(texture_id);\n\n\t\tm_staged_textures.insert(std::make_pair(texture_id, texture));\n\n\t\treturn texture_handle;\n\t}\n\n\tDescriptorAllocator * D3D12TexturePool::GetAllocator(DescriptorHeapType type)\n\t{\n\t\treturn m_allocators[static_cast<size_t>(type)];\n\t}\n\n\tvoid D3D12TexturePool::MarkForUnload(TextureHandle& handle, unsigned int frame_idx)\n\t{\n\t\tuint64_t texture_id = handle.m_id;\n\n\t\td3d12::TextureResource* texture = static_cast<d3d12::TextureResource*>(m_staged_textures.at(texture_id));\n\t\tm_staged_textures.erase(texture_id);\n\t\tm_staging_textures.at(frame_idx).erase(texture_id);\n\n\t\tm_marked_for_unload.at(frame_idx).push_back(texture);\n\n#ifdef _DEBUG\n\t\tLOGW(\"[DEBUG MESSAGE]: Handle {} from {} invalidated.\", texture_id, m_name);\n#endif\n\n\t\thandle.m_pool = nullptr;\n\t\thandle.m_id = -UINT_MAX;\n\t}\n\n\tvoid D3D12TexturePool::UnloadTextures(unsigned int frame_idx)\n\t{\n\t\tauto& vec = m_marked_for_unload.at(frame_idx);\n\n\t\tfor (auto* texture : vec)\n\t\t{\n\t\t\tSAFE_RELEASE(texture->m_resource);\n\n\t\t\tif (texture->m_intermediate)\n\t\t\t\tSAFE_RELEASE(texture->m_intermediate);\n\n\t\t\tdelete texture;\n\t\t}\n\n\t\tm_marked_for_unload.at(frame_idx).clear();\n\t}\n\n\tTextureHandle D3D12TexturePool::LoadFromFile(std::string_view path, bool srgb, bool generate_mips)\n\t{\n\t\tauto device = m_render_system.m_device;\n\n\t\tDirectX::TexMetadata metadata;\n\t\tDirectX::ScratchImage* image = new DirectX::ScratchImage;\n\n\t\tstd::optional<std::string_view> extension = util::GetFileExtension(path);\n\t\tstd::wstring wide_string(path.begin(), path.end());\n\n\t\tif (std::string_view ext_int = extension.value(); extension.has_value())\n\t\t{\n\t\t\tHRESULT hr = S_OK;\n\n\t\t\tif (ext_int.find(\"png\") != std::string_view::npos\n\t\t\t\t|| ext_int.find(\"jpeg\") != std::string_view::npos\n\t\t\t\t|| ext_int.find(\"jpg\") != std::string_view::npos\n\t\t\t\t|| ext_int.find(\"bmp\") != std::string_view::npos)\n\t\t\t{\n\t\t\t\thr = LoadFromWICFile(wide_string.c_str(),\n\t\t\t\t\tDirectX::WIC_FLAGS_NONE, &metadata, *image);\n\t\t\t}\n\t\t\telse if (ext_int.find(\"dds\") != std::string_view::npos)\n\t\t\t{\n\t\t\t\thr = LoadFromDDSFile(wide_string.c_str(),\n\t\t\t\t\tDirectX::DDS_FLAGS_NONE, &metadata, *image);\n\t\t\t}\n\t\t\telse if (ext_int.find(\"hdr\") != std::string_view::npos)\n\t\t\t{\n\t\t\t\thr = LoadFromHDRFile(wide_string.c_str(), &metadata, *image);\n\t\t\t}\n\t\t\telse if (ext_int.find(\"tga\") != std::string_view::npos)\n\t\t\t{\n\t\t\t\thr = DirectX::LoadFromTGAFile(wide_string.c_str(), &metadata, *image);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t// Return an invalid texture handle when format is not supported\n\t\t\t\tLOGE(\"Texture {} not loaded. Format not supported.\", path);\n\t\t\t\treturn {};\n\t\t\t}\n\n\t\t\tif (FAILED(hr))\n\t\t\t{\n\t\t\t\t_com_error err(hr);\n\t\t\t\tLPCTSTR errMsg = err.ErrorMessage();\n\n\t\t\t\t// Return an invalid texture handle when texture couldn't be loaded\n\t\t\t\tLOGE(\"ERROR: DirectXTex error: {}\", errMsg);\n\t\t\t\treturn {};\n\t\t\t}\n\t\t}\n\n\t\tuint32_t mip_lvls;\n\n\t\tbool mip_generation = (metadata.mipLevels > 1) ? false : generate_mips;\n\n\t\tif (mip_generation)\n\t\t{\n\t\t\tmip_lvls = static_cast<uint32_t>(std::floor(std::log2(std::max(metadata.width, metadata.height)))) + 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmip_lvls = static_cast<std::uint32_t>(metadata.mipLevels);\n\t\t}\n\n\t\tFormat texture_format = static_cast<wr::Format>(metadata.format);\n\t\t\n\t\td3d12::desc::TextureDesc desc;\n\n\t\tdesc.m_width = static_cast<std::uint32_t>(metadata.width);\n\t\tdesc.m_height = static_cast<std::uint32_t>(metadata.height);\n\t\tdesc.m_is_cubemap = metadata.IsCubemap();\n\t\tdesc.m_depth = static_cast<std::uint32_t>(metadata.depth);\n\t\tdesc.m_array_size = static_cast<std::uint32_t>(metadata.arraySize);\n\t\tdesc.m_mip_levels = mip_lvls;\n\t\tdesc.m_texture_format = texture_format;\n\t\tdesc.m_initial_state = ResourceState::COPY_DEST;\n\n\t\td3d12::TextureResource* texture = d3d12::CreateTexture(device, &desc, mip_generation);\n\n\t\tDescriptorAllocation alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate();\n\n\t\tif (alloc.IsNull())\n\t\t{\n\t\t\tLOGC(\"Couldn't allocate descriptor for the texture resource\");\n\t\t}\n\n\t\ttexture->m_need_mips = mip_generation;\n\t\ttexture->m_srv_allocation = std::move(alloc);\n\t\ttexture->m_resource->SetName(wide_string.c_str());\n\n\t\td3d12::CreateSRVFromTexture(texture);\n\n\t\tm_loaded_textures++;\n\n\t\tLOG(\"[TEXTURE LOADED] {}\", path);\n\n\t\tuint64_t texture_id = m_id_factory.GetUnusedID();\n\n\t\tTextureHandle texture_handle;\n\t\ttexture_handle.m_pool = this;\n\t\ttexture_handle.m_id = static_cast<std::uint32_t>(texture_id);\n\n\t\tm_unstaged_textures.insert(std::make_pair(texture_id, std::make_pair(texture, image)));\n\n\t\treturn texture_handle;\n\t}\n\t\n\tTextureHandle D3D12TexturePool::LoadFromMemory(unsigned char* data, size_t width, size_t height, TextureFormat type, bool srgb, bool generate_mips)\n\t{\n\t\tauto device = m_render_system.m_device;\n\n\t\tDirectX::TexMetadata metadata;\n\t\tDirectX::ScratchImage* image = new DirectX::ScratchImage;\n\n\t\tHRESULT hr;\n\n\t\tswitch (type)\n\t\t{\n\t\tcase wr::TextureFormat::WIC:\n\t\t{\n\t\t\t//Assuming RGBA8_UNORM for the time being, need to find a solution\n\t\t\thr = LoadFromWICMemory(data, width * height * 8/*bits*/ * 4/*channels*/,\n\t\t\t\tDirectX::WIC_FLAGS_NONE, &metadata, *image);\n\n\t\t\tbreak;\n\t\t}\n\t\tcase wr::TextureFormat::DDS:\n\t\t{\n\t\t\t//Assuming RGBA8_UNORM for the time being, need to find a solution\n\t\t\thr = LoadFromDDSMemory(data, width * height * 8/*bits*/ * 4/*channels*/,\n\t\t\t\tDirectX::DDS_FLAGS_NONE, &metadata, *image);\n\n\t\t\tbreak;\n\t\t}\n\n\t\tcase wr::TextureFormat::HDR:\n\t\t{\n\t\t\t//Assuming RGBA16_FLOAT for the time being, need to find a solution\n\t\t\thr = LoadFromHDRMemory(data, width * height * 16/*bits*/ * 4/*channels*/,\n\t\t\t\t&metadata, *image);\n\n\t\t\tbreak;\n\t\t}\n\t\tcase wr::TextureFormat::RAW:\n\t\t{\n\t\t\tDirectX::Image temp_image = {};\n\n\t\t\ttemp_image.format = srgb ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM;\n\t\t\ttemp_image.width = width;\n\t\t\ttemp_image.height = height;\n\t\t\ttemp_image.rowPitch = width * 4;\n\t\t\ttemp_image.slicePitch = width * height * 4;\n\t\t\ttemp_image.pixels = data;\n\n\t\t\thr = image->InitializeFromImage(temp_image);\n\n\t\t\tmetadata.width = width;\n\t\t\tmetadata.height = height;\n\t\t\tmetadata.arraySize = 1;\n\t\t\tmetadata.format = srgb ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM;\n\t\t\tmetadata.mipLevels = 1;\n\t\t\tmetadata.depth = 1;\n\t\t\tmetadata.dimension = DirectX::TEX_DIMENSION::TEX_DIMENSION_TEXTURE2D;\n\t\t\tmetadata.miscFlags = 0;\n\t\t\tmetadata.miscFlags2 = 0;\n\n\t\t\tbreak;\n\t\t}\n\n\t\tdefault:\n\t\t\tLOGC(\"[ERROR]: How did we even get here?\")\n\t\t\tbreak;\n\t\t}\n\n\t\tif (FAILED(hr))\n\t\t{\n\t\t\t_com_error err(hr);\n\t\t\tLPCTSTR errMsg = err.ErrorMessage();\n\n\t\t\tLOGC(\"ERROR: DirectXTex error: {}\", errMsg);\n\t\t}\n\n\t\tuint32_t mip_lvls;\n\n\t\tbool mip_generation = (metadata.mipLevels > 1) ? false : generate_mips;\n\n\t\tif (mip_generation)\n\t\t{\n\t\t\tmip_lvls = static_cast<uint32_t>(std::floor(std::log2(std::max(metadata.width, metadata.height)))) + 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmip_lvls = static_cast<std::uint32_t>(metadata.mipLevels);\n\t\t}\n\n\t\tFormat texture_format = static_cast<wr::Format>(metadata.format);\n\n\t\td3d12::desc::TextureDesc desc;\n\n\t\tdesc.m_width = static_cast<std::uint32_t>(metadata.width);\n\t\tdesc.m_height = static_cast<std::uint32_t>(metadata.height);\n\t\tdesc.m_is_cubemap = metadata.IsCubemap();\n\t\tdesc.m_depth = static_cast<std::uint32_t>(metadata.depth);\n\t\tdesc.m_array_size = static_cast<std::uint32_t>(metadata.arraySize);\n\t\tdesc.m_mip_levels = mip_lvls;\n\t\tdesc.m_texture_format = texture_format;\n\t\tdesc.m_initial_state = ResourceState::COPY_DEST;\n\n\t\td3d12::TextureResource* texture = d3d12::CreateTexture(device, &desc, mip_generation);\n\n\t\tDescriptorAllocation alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate();\n\n\t\tif (alloc.IsNull())\n\t\t{\n\t\t\tLOGC(\"Couldn't allocate descriptor for the texture resource\");\n\t\t}\n\n\t\ttexture->m_need_mips = mip_generation;\n\t\ttexture->m_srv_allocation = std::move(alloc);\n\n\t\tstd::wstring name = L\"TextureFromMemory\" + std::to_wstring(m_loaded_textures);\n\t\ttexture->m_resource->SetName(name.c_str());\n\n\t\td3d12::CreateSRVFromTexture(texture);\n\n\t\tm_loaded_textures++;\n\n\t\tLOG(\"[TEXTURE LOADED]: Texture from Memory\");\n\n\t\tuint64_t texture_id = m_id_factory.GetUnusedID();\n\n\t\tTextureHandle texture_handle;\n\t\ttexture_handle.m_pool = this;\n\t\ttexture_handle.m_id = static_cast<std::uint32_t>(texture_id);\n\n\t\tm_unstaged_textures.insert(std::make_pair(texture_id, std::make_pair(texture, image)));\n\n\t\treturn texture_handle;\n\n\t}\n\n\tvoid D3D12TexturePool::MoveStagedTextures(unsigned int frame_idx)\n\t{\n\t\tfor (auto itr = m_unstaged_textures.begin(); itr != m_unstaged_textures.end(); ++itr)\n\t\t{\n\t\t\tm_staged_textures.insert(std::make_pair(itr->first, itr->second.first));\n\t\t\tm_staging_textures[frame_idx].insert(std::make_pair(itr->first, itr->second.first));\n\n\t\t\t//Free the ScratchImage as it's not needed anymore after staging\n\t\t\tdelete itr->second.second;\n\t\t}\n\n\t\tm_unstaged_textures.clear();\n\t}\n\n\tvoid D3D12TexturePool::GenerateMips(d3d12::TextureResource* texture, CommandList* cmd_list)\n\t{\n\t\twr::d3d12::CommandList* d3d12_cmd_list = static_cast<wr::d3d12::CommandList*>(cmd_list);\n\t\tauto pipeline = static_cast<d3d12::PipelineState*>(PipelineRegistry::Get().Find(pipelines::mip_mapping));\n\n\t\td3d12::BindComputePipeline(d3d12_cmd_list, pipeline);\n\n\t\tif (texture->m_need_mips)\n\t\t{\n\t\t\tif (d3d12::CheckUAVCompatibility(texture->m_format) || d3d12::CheckOptionalUAVFormat(texture->m_format))\n\t\t\t{\n\t\t\t\tGenerateMips_UAV(texture, cmd_list);\n\t\t\t}\n\t\t\telse if (d3d12::CheckBGRFormat(texture->m_format))\n\t\t\t{\n\t\t\t\tGenerateMips_BGR(texture, cmd_list);\n\t\t\t}\n\t\t\telse if (d3d12::CheckSRGBFormat(texture->m_format))\n\t\t\t{\n\t\t\t\tGenerateMips_SRGB(texture, cmd_list);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGC(\"[ERROR]: GenerateMips-> I don't know how we ended up here!\");\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid D3D12TexturePool::GenerateMips_UAV(d3d12::TextureResource* texture, CommandList* cmd_list)\n\t{\n\t\twr::d3d12::CommandList* d3d12_cmd_list = static_cast<wr::d3d12::CommandList*>(cmd_list);\n\t\t\n\t\t//Create shader resource view for the source texture in the descriptor heap\n\t\tDescriptorAllocation srv_alloc = m_mipmapping_allocator->Allocate();\n\t\td3d12::DescHeapCPUHandle srv_handle = srv_alloc.GetDescriptorHandle();\n\n\t\td3d12::CreateSRVFromTexture(texture, srv_handle);\n\n\t\tMipMapping_CB generate_mips_cb;\n\n\t\tfor (uint32_t src_mip = 0; src_mip < texture->m_mip_levels - 1u;)\n\t\t{\n\t\t\tuint32_t src_width = static_cast<std::uint32_t>(texture->m_width) >> src_mip;\n\t\t\tuint32_t src_height = static_cast<std::uint32_t>(texture->m_height) >> src_mip;\n\t\t\tuint32_t dst_width = src_width >> 1;\n\t\t\tuint32_t dst_height = src_height >> 1;\n\n\t\t\t// Determine the compute shader to use based on the dimension of the \n\t\t\t// source texture.\n\t\t\t// 0b00(0): Both width and height are even.\n\t\t\t// 0b01(1): Width is odd, height is even.\n\t\t\t// 0b10(2): Width is even, height is odd.\n\t\t\t// 0b11(3): Both width and height are odd.\n\t\t\tgenerate_mips_cb.src_dimension = (src_height & 1) << 1 | (src_width & 1);\n\n\t\t\t// Max amount of mip levels to compute in a pass is 4.\n\t\t\tDWORD mip_count;\n\n\t\t\t// The number of times we can half the size of the texture and get\n\t\t\t// exactly a 50% reduction in size.\n\t\t\t// A 1 bit in the width or height indicates an odd dimension.\n\t\t\t// The case where either the width or the height is exactly 1 is handled\n\t\t\t// as a special case (as the dimension does not require reduction).\n\t\t\t_BitScanForward(&mip_count, (dst_width == 1 ? dst_height : dst_width) |\n\t\t\t\t\t\t\t\t\t\t(dst_height == 1 ? dst_width : dst_height));\n\n\t\t\t// Maximum number of mips to generate is 4.\n\t\t\tmip_count = std::min<DWORD>(4, mip_count + 1);\n\t\t\t// Clamp to total number of mips left over.\n\t\t\tmip_count = ((static_cast<DWORD>(src_mip) + mip_count) > texture->m_mip_levels) ? static_cast<DWORD>(texture->m_mip_levels - src_mip) : mip_count;\n\n\t\t\t// Dimensions should not reduce to 0.\n\t\t\t// This can happen if the width and height are not the same.\n\t\t\tdst_width = std::max<DWORD>(1, dst_width);\n\t\t\tdst_height = std::max<DWORD>(1, dst_height);\n\n\t\t\tgenerate_mips_cb.src_mip_level = src_mip;\n\t\t\tgenerate_mips_cb.num_mip_levels = mip_count;\n\t\t\tgenerate_mips_cb.texel_size.x = 1.0f / (float)dst_width;\n\t\t\tgenerate_mips_cb.texel_size.y = 1.0f / (float)dst_height;\n\n\t\t\td3d12::BindCompute32BitConstants(d3d12_cmd_list, &generate_mips_cb, sizeof(MipMapping_CB) / sizeof(uint32_t), 0, 0);\n\n\t\t\td3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[src_mip], ResourceState::PIXEL_SHADER_RESOURCE, src_mip, 1);\n\n\t\t\td3d12::SetShaderSRV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::SOURCE)), srv_handle);\n\n\t\t\tfor (uint32_t mip = 0; mip < mip_count; ++mip)\n\t\t\t{\n\t\t\t\tstd::uint32_t idx = src_mip + mip + 1u;\n\n\t\t\t\td3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[idx], ResourceState::UNORDERED_ACCESS, idx, 1);\n\n\t\t\t\tDescriptorAllocation uav_alloc = m_mipmapping_allocator->Allocate();\n\t\t\t\td3d12::DescHeapCPUHandle uav_handle = uav_alloc.GetDescriptorHandle();\n\n\t\t\t\td3d12::CreateUAVFromTexture(texture, uav_handle, src_mip + mip + 1);\n\n\t\t\t\td3d12::SetShaderUAV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)) + mip, uav_handle);\n\t\t\t}\n\t\t\t// Pad any unused mip levels with a default UAV to keeps the DX12 runtime happy.\n\t\t\tif (mip_count < 4)\n\t\t\t{\n\t\t\t\td3d12_cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(\n\t\t\t\t\tCOMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)),\n\t\t\t\t\tmip_count + 1, 4 - mip_count, m_default_uav.GetDescriptorHandle());\n\t\t\t}\n\n\t\t\td3d12::Dispatch(d3d12_cmd_list, ((dst_width + 8 - 1) / 8), ((dst_height + 8 - 1) / 8), 1u);\n\n\t\t\t//Wait for all accesses to the destination texture UAV to be finished before generating the next mipmap, as it will be the source texture for the next mipmap\n\t\t\td3d12::UAVBarrier(d3d12_cmd_list, { texture });\n\n\t\t\tfor (uint32_t mip = 0; mip < mip_count; ++mip)\n\t\t\t{\n\t\t\t\tstd::uint32_t idx = src_mip + mip + 1u;\n\n\t\t\t\td3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[idx], ResourceState::PIXEL_SHADER_RESOURCE, idx, 1);\n\t\t\t}\n\n\t\t\tsrc_mip += mip_count;\n\t\t}\n\n\t\ttexture->m_need_mips = false;\n\t}\n\n\tvoid D3D12TexturePool::GenerateMips_BGR(d3d12::TextureResource* texture, CommandList* cmd_list)\n\t{\n\t\tauto device = m_render_system.m_device;\n\t\twr::d3d12::CommandList* d3d12_cmd_list = static_cast<wr::d3d12::CommandList*>(cmd_list);\n\t\tunsigned int frame_idx = m_render_system.GetFrameIdx();\n\n\t\t//Create new resource with UAV compatible format\n\t\td3d12::desc::TextureDesc copy_desc;\n\t\tcopy_desc.m_width = static_cast<std::uint32_t>(texture->m_width);\n\t\tcopy_desc.m_height = static_cast<std::uint32_t>(texture->m_height);\n\t\tcopy_desc.m_depth = static_cast<std::uint32_t>(texture->m_depth);\n\t\tcopy_desc.m_array_size = static_cast<std::uint32_t>(texture->m_array_size);\n\t\tcopy_desc.m_initial_state = ResourceState::COMMON;\n\t\tcopy_desc.m_is_cubemap = texture->m_is_cubemap;\n\t\tcopy_desc.m_mip_levels = static_cast<std::uint32_t>(texture->m_mip_levels);\n\t\tcopy_desc.m_texture_format = Format::R8G8B8A8_UNORM;\n\n\t\t// Create a heap to alias the resource. This is used to copy the resource without \n\t\t// failing GPU validation.\n\t\tauto resource = texture->m_resource;\n\t\tauto resourceDesc = resource->GetDesc();\n\n\t\tauto allocation_info = device->m_native->GetResourceAllocationInfo(0, 1, &resourceDesc);\n\n\t\td3d12::Heap<wr::HeapOptimization::BIG_STATIC_BUFFERS>* heap;\n\t\theap = d3d12::CreateHeap_BSBO(device, allocation_info.SizeInBytes, ResourceType::TEXTURE, 1);\n\n\t\tm_temporary_heaps[frame_idx].push_back(heap);\n\n\t\t//Create the copy resource placed in the heap.\n\t\td3d12::TextureResource* copy_texture = d3d12::CreatePlacedTexture(device, &copy_desc, true, heap);\n\n\t\tm_temporary_textures[frame_idx].push_back(copy_texture);\n\n\t\t// Create an alias for which to perform the copy operation.\n\t\td3d12::desc::TextureDesc alias_desc = copy_desc;\n\t\talias_desc.m_texture_format = (texture->m_format == Format::B8G8R8X8_UNORM ||\n\t\t\t\t\t\t\t\t\t\ttexture->m_format == Format::B8G8R8X8_UNORM_SRGB) ?\n\t\t\t\t\t\t\t\t\t\t\tFormat::B8G8R8X8_UNORM : Format::B8G8R8A8_UNORM;\n\n\t\td3d12::TextureResource* alias_texture = d3d12::CreatePlacedTexture(device, &alias_desc, true, heap);\n\n\t\tm_temporary_textures[frame_idx].push_back(alias_texture);\n\n\t\td3d12::Alias(d3d12_cmd_list, nullptr, alias_texture);\n\n\t\t//Copy resource\n\t\td3d12::CopyResource(d3d12_cmd_list, texture, alias_texture);\n\n\t\t// Alias the UAV compatible texture back.\n\t\td3d12::Alias(d3d12_cmd_list, alias_texture, copy_texture);\n\n\t\t//Now use the resource copy to generate the mips\n\t\tGenerateMips_UAV(copy_texture, cmd_list);\n\n\t\t// Copy back to the original (via the alias to ensure GPU validation)\n\t\td3d12::Alias(d3d12_cmd_list, copy_texture, alias_texture);\n\n\t\td3d12::CopyResource(d3d12_cmd_list, alias_texture, texture);\n\t}\n\n\tvoid D3D12TexturePool::GenerateMips_SRGB(d3d12::TextureResource* texture, CommandList* cmd_list)\n\t{\n\t\tauto device = m_render_system.m_device;\n\t\twr::d3d12::CommandList* d3d12_cmd_list = static_cast<wr::d3d12::CommandList*>(cmd_list);\n\n\t\t//Create copy of the texture and store it for later deletion.\n\t\td3d12::desc::TextureDesc copy_desc;\n\n\t\tcopy_desc.m_width = static_cast<std::uint32_t>(texture->m_width);\n\t\tcopy_desc.m_height = static_cast<std::uint32_t>(texture->m_height);\n\t\tcopy_desc.m_is_cubemap = texture->m_is_cubemap;\n\t\tcopy_desc.m_depth = static_cast<std::uint32_t>(texture->m_depth);\n\t\tcopy_desc.m_array_size = static_cast<std::uint32_t>(texture->m_array_size);\n\t\tcopy_desc.m_mip_levels = static_cast<std::uint32_t>(texture->m_mip_levels);\n\t\tcopy_desc.m_texture_format = d3d12::RemoveSRGB(texture->m_format);\n\t\tcopy_desc.m_initial_state = ResourceState::COPY_DEST;\n\n\t\tauto texture_copy = d3d12::CreateTexture(device, &copy_desc, true);\n\n\t\tunsigned int frame_idx = m_render_system.GetFrameIdx();\n\n\t\tm_temporary_textures[frame_idx].push_back(texture_copy);\n\n\t\td3d12::CopyResource(d3d12_cmd_list, texture, texture_copy);\n\n\t\tGenerateMips(texture_copy, cmd_list);\n\n\t\td3d12::CopyResource(d3d12_cmd_list, texture_copy, texture);\n\t}\n\n\tvoid D3D12TexturePool::GenerateMips_Cubemap(d3d12::TextureResource* texture, CommandList* cmd_list, unsigned int array_slice)\n\t{\n\t\twr::d3d12::CommandList* d3d12_cmd_list = static_cast<wr::d3d12::CommandList*>(cmd_list);\n\n\t\t//Create shader resource view for the source texture in the descriptor heap\n\t\tDescriptorAllocation srv_alloc = m_mipmapping_allocator->Allocate();\n\t\td3d12::DescHeapCPUHandle srv_handle = srv_alloc.GetDescriptorHandle();\n\n\t\td3d12::CreateSRVFromCubemapFace(texture, srv_handle, static_cast<unsigned int>(texture->m_mip_levels), 0, array_slice);\n\n\t\tMipMapping_CB generate_mips_cb;\n\n\t\tfor (uint32_t src_mip = 0; src_mip < texture->m_mip_levels - 1u;)\n\t\t{\n\t\t\tuint32_t src_width = static_cast<std::uint32_t>(texture->m_width) >> src_mip;\n\t\t\tuint32_t src_height = static_cast<std::uint32_t>(texture->m_height) >> src_mip;\n\t\t\tuint32_t dst_width = src_width >> 1;\n\t\t\tuint32_t dst_height = src_height >> 1;\n\n\t\t\t// Determine the compute shader to use based on the dimension of the \n\t\t\t// source texture.\n\t\t\t// 0b00(0): Both width and height are even.\n\t\t\t// 0b01(1): Width is odd, height is even.\n\t\t\t// 0b10(2): Width is even, height is odd.\n\t\t\t// 0b11(3): Both width and height are odd.\n\t\t\tgenerate_mips_cb.src_dimension = (src_height & 1) << 1 | (src_width & 1);\n\n\t\t\t// Max amount of mip levels to compute in a pass is 4.\n\t\t\tDWORD mip_count;\n\n\t\t\t// The number of times we can half the size of the texture and get\n\t\t\t// exactly a 50% reduction in size.\n\t\t\t// A 1 bit in the width or height indicates an odd dimension.\n\t\t\t// The case where either the width or the height is exactly 1 is handled\n\t\t\t// as a special case (as the dimension does not require reduction).\n\t\t\t_BitScanForward(&mip_count, (dst_width == 1 ? dst_height : dst_width) |\n\t\t\t\t(dst_height == 1 ? dst_width : dst_height));\n\n\t\t\t// Maximum number of mips to generate is 4.\n\t\t\tmip_count = std::min<DWORD>(4, mip_count + 1);\n\t\t\t// Clamp to total number of mips left over.\n\t\t\tmip_count = ((static_cast<DWORD>(src_mip) + mip_count) > texture->m_mip_levels) ? static_cast<DWORD>(texture->m_mip_levels - src_mip) : mip_count;\n\n\t\t\t// Dimensions should not reduce to 0.\n\t\t\t// This can happen if the width and height are not the same.\n\t\t\tdst_width = std::max<DWORD>(1, dst_width);\n\t\t\tdst_height = std::max<DWORD>(1, dst_height);\n\n\t\t\tgenerate_mips_cb.src_mip_level = src_mip;\n\t\t\tgenerate_mips_cb.num_mip_levels = mip_count;\n\t\t\tgenerate_mips_cb.texel_size.x = 1.0f / (float)dst_width;\n\t\t\tgenerate_mips_cb.texel_size.y = 1.0f / (float)dst_height;\n\n\t\t\td3d12::BindCompute32BitConstants(d3d12_cmd_list, &generate_mips_cb, sizeof(MipMapping_CB) / sizeof(uint32_t), 0, 0);\n\n\t\t\td3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[src_mip], ResourceState::PIXEL_SHADER_RESOURCE, src_mip, 1);\n\n\t\t\td3d12::SetShaderSRV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::SOURCE)), srv_handle);\n\n\t\t\tfor (uint32_t mip = 0; mip < mip_count; ++mip)\n\t\t\t{\n\t\t\t\tuint32_t idx = src_mip + mip + 1u;\n\n\t\t\t\td3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[idx], ResourceState::UNORDERED_ACCESS, idx, 1);\n\n\t\t\t\tDescriptorAllocation uav_alloc = m_mipmapping_allocator->Allocate();\n\t\t\t\td3d12::DescHeapCPUHandle uav_handle = uav_alloc.GetDescriptorHandle();\n\n\t\t\t\td3d12::CreateUAVFromCubemapFace(texture, uav_handle, src_mip + mip + 1, array_slice);\n\n\t\t\t\td3d12::SetShaderUAV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)) + mip, uav_handle);\n\t\t\t}\n\t\t\t// Pad any unused mip levels with a default UAV to keeps the DX12 runtime happy.\n\t\t\tif (mip_count < 4)\n\t\t\t{\n\t\t\t\td3d12_cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(\n\t\t\t\t\tCOMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)),\n\t\t\t\t\tmip_count + 1, 4 - mip_count, m_default_uav.GetDescriptorHandle());\n\t\t\t}\n\n\t\t\td3d12::Dispatch(d3d12_cmd_list, ((dst_width + 8 - 1) / 8), ((dst_height + 8 - 1) / 8), 1u);\n\n\t\t\t//Wait for all accesses to the destination texture UAV to be finished before generating the next mipmap, as it will be the source texture for the next mipmap\n\t\t\td3d12::UAVBarrier(d3d12_cmd_list, { texture });\n\n\t\t\tfor (uint32_t mip = 0; mip < mip_count; ++mip)\n\t\t\t{\n\t\t\t\tuint32_t idx = src_mip + mip + 1u;\n\n\t\t\t\td3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[idx], ResourceState::PIXEL_SHADER_RESOURCE, idx, 1);\n\t\t\t}\n\n\t\t\tsrc_mip += mip_count;\n\t\t}\n\n\t\ttexture->m_need_mips = false;\n\t}\n}\n"
  },
  {
    "path": "src/d3d12/d3d12_resource_pool_texture.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n/*\nThe mipmapping implementation used in this framework is a ported version of\nMiniEngine's implementation.\n*/\n/*\nThe MIT License(MIT)\n\nCopyright(c) 2013 - 2015 Microsoft\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files(the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions :\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n*/\n#pragma once\n\n#include \"../resource_pool_texture.hpp\"\n#include \"d3d12_enums.hpp\"\n#include \"d3d12_texture_resources.hpp\"\n#include <DirectXMath.h>\n\n\nnamespace wr\n{\n\tstruct MipMapping_CB\n\t{\n\t\tuint32_t src_mip_level;\t\t\t// Texture level of source mip\n\t\tuint32_t num_mip_levels;\t\t// Number of OutMips to write: [1-4]\n\t\tuint32_t src_dimension;\t\t\t// Width and height of the source texture are even or odd.\n\t\tuint32_t padding;\t\t\t\t// Pad to 16 byte alignment.\n\t\tDirectX::XMFLOAT2 texel_size;\t// 1.0 / OutMip1.Dimensions\n\t};\n\n\tclass D3D12RenderSystem;\n\tclass DescriptorAllocator;\n\n\tclass D3D12TexturePool : public TexturePool\n\t{\n\tpublic:\n\t\texplicit D3D12TexturePool(D3D12RenderSystem& render_system);\n\t\t~D3D12TexturePool() final;\n\n\t\tvoid Evict() final;\n\t\tvoid MakeResident() final;\n\t\tvoid Stage(CommandList* cmd_list) final;\n\t\tvoid PostStageClear() final;\n\t\tvoid ReleaseTemporaryResources() final;\n\n\t\td3d12::TextureResource* GetTextureResource(TextureHandle handle) final;\n\n\t\t//! Loads a texture from file.\n\t\t/*!\n\t\t  \\param path std::string that contains a path to the texture file location.\n\t\t  \\param srgb Defines if the texture should be loaded as srgb texture or not (currently not supported). \n\t\t  \\param generate_mips Defines if mipmaps should be created.\n\t\t  \\return The texture handle to the loaded texture.\n\t\t  \\sa wr::TextureHandle\n\t\t*/\n\t\t[[nodiscard]] TextureHandle LoadFromFile(std::string_view path, bool srgb, bool generate_mips) final;\n\t\t[[nodiscard]] TextureHandle LoadFromMemory(unsigned char* data, size_t width, size_t height, TextureFormat type, bool srgb, bool generate_mips) final;\n\t\t[[nodiscard]] TextureHandle CreateCubemap(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest) final;\n\t\t[[nodiscard]] TextureHandle CreateTexture(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest) final;\n\n\t\tDescriptorAllocator* GetAllocator(DescriptorHeapType type);\n\t\tDescriptorAllocator* GetMipmappingAllocator() { return m_mipmapping_allocator; }\n\n\t\tvoid MarkForUnload(TextureHandle& handle, unsigned int frame_idx) final;\n\t\tvoid UnloadTextures(unsigned int frame_idx) final;\n\n\t\tvoid GenerateMips_Cubemap(d3d12::TextureResource* texture, CommandList* cmd_list, unsigned int array_slice);\n\n\tprotected:\n\n\t\tvoid MoveStagedTextures(unsigned int frame_idx);\n\t\tvoid GenerateMips(d3d12::TextureResource* texture, CommandList* cmd_list);\n\n\t\tvoid GenerateMips_UAV(d3d12::TextureResource* texture, CommandList* cmd_list);\n\t\tvoid GenerateMips_BGR(d3d12::TextureResource* texture, CommandList* cmd_list);\n\t\tvoid GenerateMips_SRGB(d3d12::TextureResource* texture, CommandList* cmd_list);\n\n\t\t//Unstaged textures are stored as pairs in a map. This removes the necessity of having a ScratchImage\n\t\t//pointer in the Texture struct. Once the textures are staged the ScratchImages are deleted.\n\t\tusing UnstagedTextures = std::unordered_map<uint64_t, std::pair<Texture*, DirectX::ScratchImage*>>;\n\t\tUnstagedTextures m_unstaged_textures;\n\n\t\tusing StagedTextures = std::unordered_map<uint64_t, Texture*>;\n\t\tStagedTextures m_staged_textures;\n\t\tstd::vector<StagedTextures> m_staging_textures;\n\n\t\tD3D12RenderSystem& m_render_system;\n\n\t\t//CPU only visible heaps used for staging of descriptors.\n\t\t//Renderer will copy the descriptor it needs to the GPU visible heap used for rendering.\n\t\tDescriptorAllocator* m_allocators[static_cast<size_t>(DescriptorHeapType::DESC_HEAP_TYPE_NUM_TYPES)];\n\n\t\tDescriptorAllocator* m_mipmapping_allocator;\n\n\t\t//Track resources that are created in one frame and destroyed after\n\t\tstd::array<std::vector<d3d12::TextureResource*>, d3d12::settings::num_back_buffers> m_temporary_textures;\n\t\tstd::array< std::vector<d3d12::Heap<wr::HeapOptimization::BIG_STATIC_BUFFERS>*>, d3d12::settings::num_back_buffers> m_temporary_heaps;\n\n\t\t//Resources marked for deletion\n\t\tstd::array<std::vector<d3d12::TextureResource*>, d3d12::settings::num_back_buffers> m_marked_for_unload;\n\n\t\t//Default UAV used for padding\n\t\tDescriptorAllocation m_default_uav;\n\t};\n\n\n\n\n}"
  },
  {
    "path": "src/d3d12/d3d12_root_signature.cpp",
    "content": "/*!\r\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\r\n *\r\n * Licensed under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n *     http://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an \"AS IS\" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n */\r\n#include \"d3d12_functions.hpp\"\r\n\r\n#include \"d3d12_defines.hpp\"\r\n\r\nnamespace wr::d3d12\r\n{\r\n\r\n\tRootSignature* CreateRootSignature(desc::RootSignatureDesc create_info)\r\n\t{\r\n\t\tauto root_signature = new RootSignature();\r\n\r\n\t\troot_signature->m_create_info = create_info;\r\n\r\n\t\treturn root_signature;\r\n\t}\r\n\r\n\tvoid SetName(RootSignature * root_signature, std::wstring name)\r\n\t{\r\n\t\troot_signature->m_native->SetName(name.c_str());\r\n\t}\r\n\r\n\tvoid FinalizeRootSignature(RootSignature* root_signature, Device* device)\r\n\t{\r\n\t\tauto n_device = device->m_native;\r\n\t\tauto n_fb_device = device->m_fallback_native;\r\n\t\tauto create_info = root_signature->m_create_info;\r\n\r\n\t\t// Check capabilities TODO: Actually use capabilities.\r\n\t\tD3D12_FEATURE_DATA_ROOT_SIGNATURE feature_data = {};\r\n\t\tfeature_data.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1;\r\n\t\tif (FAILED(device->m_native->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &feature_data, sizeof(feature_data))))\r\n\t\t{\r\n\t\t\tfeature_data.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_0;\r\n\t\t}\r\n\r\n\t\tauto num_samplers = static_cast<std::uint32_t>(create_info.m_samplers.size());\r\n\t\tstd::vector<D3D12_STATIC_SAMPLER_DESC> samplers(num_samplers);\r\n\t\tfor (auto i = 0u; i < num_samplers; i++)\r\n\t\t{\r\n\t\t\tauto sampler_info = create_info.m_samplers[i];\r\n\r\n\t\t\tsamplers[i].Filter = (D3D12_FILTER)sampler_info.m_filter;\r\n\t\t\tsamplers[i].AddressU = (D3D12_TEXTURE_ADDRESS_MODE)sampler_info.m_address_mode;\r\n\t\t\tsamplers[i].AddressV = (D3D12_TEXTURE_ADDRESS_MODE)sampler_info.m_address_mode;\r\n\t\t\tsamplers[i].AddressW = (D3D12_TEXTURE_ADDRESS_MODE)sampler_info.m_address_mode;\r\n\t\t\tsamplers[i].MipLODBias = 0;\r\n\t\t\tsamplers[i].MaxAnisotropy = 0;\r\n\t\t\tsamplers[i].ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;\r\n\t\t\tsamplers[i].BorderColor = (D3D12_STATIC_BORDER_COLOR)sampler_info.m_border_color;\r\n\t\t\tsamplers[i].MinLOD = 0.0f;\r\n\t\t\tsamplers[i].MaxLOD = D3D12_FLOAT32_MAX;\r\n\t\t\tsamplers[i].ShaderRegister = i;\r\n\t\t\tsamplers[i].RegisterSpace = 0;\r\n\t\t\tsamplers[i].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; // TODO: very inneficient. plz fix\r\n\t\t}\r\n\r\n\t\tD3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;\r\n\t\tif (create_info.m_rt_local)\r\n\t\t{\r\n\t\t\tflags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE;\r\n\t\t}\r\n\r\n\t\tCD3DX12_ROOT_SIGNATURE_DESC root_signature_desc;\r\n\t\troot_signature_desc.Init(static_cast<UINT>(create_info.m_parameters.size()),\r\n\t\t\tcreate_info.m_parameters.data(),\r\n\t\t\tnum_samplers,\r\n\t\t\tsamplers.data(),\r\n\t\t\tflags);\r\n\r\n\t\t//Create root signature bit masks, used for dynamic gpu descriptor heaps\r\n\t\tauto num_params = create_info.m_parameters.size();\r\n\r\n\t\tfor (size_t i = 0; i < num_params; ++i)\r\n\t\t{\r\n\t\t\tauto param = create_info.m_parameters.at(i);\r\n\r\n\t\t\tif (param.ParameterType == D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE)\r\n\t\t\t{\r\n\t\t\t\tauto num_desc_ranges = param.DescriptorTable.NumDescriptorRanges;\r\n\r\n\t\t\t\t// Set the bit mask depending on the type of descriptor table.\r\n\t\t\t\tif (num_desc_ranges > 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tswitch (param.DescriptorTable.pDescriptorRanges[0].RangeType)\r\n\t\t\t\t\t{\r\n\t\t\t\t\tcase D3D12_DESCRIPTOR_RANGE_TYPE_CBV:\r\n\t\t\t\t\tcase D3D12_DESCRIPTOR_RANGE_TYPE_SRV:\r\n\t\t\t\t\tcase D3D12_DESCRIPTOR_RANGE_TYPE_UAV:\r\n\t\t\t\t\t\troot_signature->m_descriptor_table_bit_mask |= (1 << i);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\tcase D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER:\r\n\t\t\t\t\t\troot_signature->m_sampler_table_bit_mask |= (1 << i);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\r\n\t\t\t\tfor (size_t j = 0; j < num_desc_ranges; ++j)\r\n\t\t\t\t{\r\n\t\t\t\t\troot_signature->m_num_descriptors_per_table[i] += param.DescriptorTable.pDescriptorRanges[j].NumDescriptors;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif (create_info.m_rtx && GetRaytracingType(device) == RaytracingType::FALLBACK)\r\n\t\t{\r\n\t\t\t// Fallback\r\n\t\t\tID3DBlob* signature;\r\n\t\t\tID3DBlob* error = nullptr;\r\n\t\t\tHRESULT hr = n_fb_device->D3D12SerializeRootSignature(&root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error); //TODO: FIX error parameter\r\n\t\t\tif (FAILED(hr))\r\n\t\t\t{\r\n\t\t\t\tLOGC(\"Failed to serialize root signature. Error: \\n {}\", (char*)error->GetBufferPointer());\r\n\t\t\t}\r\n\r\n\t\t\tTRY_M(n_fb_device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&root_signature->m_native)),\r\n\t\t\t\t\"Failed to create root signature\");\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tID3DBlob* signature;\r\n\t\t\tID3DBlob* error = nullptr;\r\n\t\t\tHRESULT hr = D3D12SerializeRootSignature(&root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error); //TODO: FIX error parameter\r\n\t\t\tif (FAILED(hr))\r\n\t\t\t{\r\n\t\t\t\tchar * err = (char*)error->GetBufferPointer();\r\n\t\t\t\tLOGC(\"Failed to serialize root signature. Error: \\n {}\", err);\r\n\t\t\t}\r\n\r\n\t\t\tTRY_M(n_device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&root_signature->m_native)),\r\n\t\t\t\t\"Failed to create root signature\");\r\n\t\t}\r\n\t\tNAME_D3D12RESOURCE(root_signature->m_native);\r\n\t}\r\n\r\n\tvoid RefinalizeRootSignature(RootSignature* root_signature, Device* device)\r\n\t{\r\n\t\tSAFE_RELEASE(root_signature->m_native)\r\n\t\tFinalizeRootSignature(root_signature, device);\r\n\t}\r\n\r\n\tvoid Destroy(RootSignature* root_signature)\r\n\t{\r\n\t\tSAFE_RELEASE(root_signature->m_native);\r\n\t\tdelete root_signature;\r\n\t}\r\n\r\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_rt_descriptor_heap.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_rt_descriptor_heap.hpp\"\n#include \"d3d12_renderer.hpp\"\n#include \"d3d12_functions.hpp\"\n#include \"../util/log.hpp\"\n\nnamespace wr\n{\n\tRTDescriptorHeap::RTDescriptorHeap(wr::d3d12::Device* device, DescriptorHeapType type, uint32_t num_descriptors_per_heap)\n\t\t: m_desc_heap_type(type)\n\t\t, m_num_descr_per_heap(num_descriptors_per_heap)\n\t\t, m_descriptor_table_bit_mask(0)\n\t\t, m_stale_descriptor_table_bit_mask(0)\n\t\t, m_device(device)\n\t{\n\t\tm_current_cpu_desc_handle.m_native.ptr = 0;\n\t\tm_current_gpu_desc_handle.m_native.ptr = 0;\n\n\t\tm_descriptor_handle_increment_size = m_device->m_native->GetDescriptorHandleIncrementSize((D3D12_DESCRIPTOR_HEAP_TYPE)type);\n\n\t\t// Allocate space for staging CPU visible descriptors.\n\t\tm_descriptor_handle_cache = std::make_unique<D3D12_CPU_DESCRIPTOR_HANDLE[]>(m_num_descr_per_heap);\n\n\t\tfor (size_t i = 0; i < d3d12::settings::num_back_buffers; ++i)\n\t\t{\n\t\t\tm_num_free_handles[i] = num_descriptors_per_heap;\n\t\t}\n\n\t\tm_heap = CreateDescriptorHeap();\n\t}\n\n\tRTDescriptorHeap::~RTDescriptorHeap()\n\t{\n\t\tdelete m_heap;\n\t}\n\n\tvoid RTDescriptorHeap::ParseRootSignature(const d3d12::RootSignature& root_signature)\n\t{\n\t\t// If the root signature changes, all descriptors must be (re)bound to the\n\t\t// command list.\n\t\tm_stale_descriptor_table_bit_mask = 0;\n\n\t\tconst auto& root_signature_desc = root_signature.m_create_info;\n\n\t\t// Get a bit mask that represents the root parameter indices that match the \n\t\t// descriptor heap type for this dynamic descriptor heap.\n\t\tswitch (m_desc_heap_type)\n\t\t{\n\t\tcase wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV:\n\t\t\tm_descriptor_table_bit_mask = root_signature.m_descriptor_table_bit_mask;\n\t\t\tbreak;\n\t\tcase wr::DescriptorHeapType::DESC_HEAP_TYPE_SAMPLER:\n\t\t\tm_descriptor_table_bit_mask = root_signature.m_sampler_table_bit_mask;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\t//LOGC(\"You can't use a different type other than CBC_SRV_UAV or SAMPLER for dynamic descriptor heaps\");\n\t\t\tbreak;\n\t\t}\n\n\t\tuint32_t descriptor_table_bitmask = m_descriptor_table_bit_mask;\n\n\t\tuint32_t current_offset = 0;\n\t\tDWORD root_idx;\n\t\tsize_t num_parameters = root_signature_desc.m_parameters.size();\n\n\t\twhile (_BitScanForward(&root_idx, descriptor_table_bitmask) && root_idx < num_parameters)\n\t\t{\n\t\t\t//Since a 32 bit mask is used to represent the descriptor tables in the root signature\n\t\t\t//I want to break if the root_idx is bigger than 32. If that ever happens, we might consider\n\t\t\t//switching to a 64bit mask.\n\t\t\tif (root_idx > 32) { LOGC(\"A maximum of 32 descriptor tables are supported\"); }\n\n\t\t\tuint32_t num_descriptors = root_signature.m_num_descriptors_per_table[root_idx];\n\n\t\t\tDescriptorTableCache& descriptorTableCache = m_descriptor_table_cache[root_idx];\n\t\t\tdescriptorTableCache.m_num_descriptors = num_descriptors;\n\t\t\tdescriptorTableCache.m_base_descriptor = m_descriptor_handle_cache.get() + current_offset;\n\n\t\t\tcurrent_offset += num_descriptors;\n\n\t\t\t// Flip the descriptor table bit so it's not scanned again for the current index.\n\t\t\tdescriptor_table_bitmask ^= (1 << root_idx);\n\t\t}\n\n\t\t// Make sure the maximum number of descriptors per descriptor heap has not been exceeded.\n\t\tif (current_offset > m_num_descr_per_heap) { LOGC(\"The root signature requires more than the maximum number of descriptors per descriptor heap. Consider increasing the maximum number of descriptors per descriptor heap.\"); }\n\t}\n\n\tvoid RTDescriptorHeap::StageDescriptors(uint32_t root_param_idx, uint32_t offset, uint32_t num_descriptors, const d3d12::DescHeapCPUHandle src_desc)\n\t{\n\t\tif (num_descriptors > m_num_descr_per_heap || root_param_idx >= MaxDescriptorTables)\n\t\t{\n\t\t\tLOGC(\"Cannot stage more than the maximum number of descriptors per heap. Cannot stage more than MaxDescriptorTables root parameters\");\n\t\t}\n\n\t\tDescriptorTableCache& descriptor_table_cache = m_descriptor_table_cache[root_param_idx];\n\n\t\t// Check that the number of descriptors to copy does not exceed the number\n\t\t// of descriptors expected in the descriptor table.\n\t\tif ((offset + num_descriptors) > descriptor_table_cache.m_num_descriptors)\n\t\t{\n\t\t\tLOGC(\"Number of descriptors exceeds the number of descriptors in the descriptor table.\");\n\t\t}\n\n\t\tD3D12_CPU_DESCRIPTOR_HANDLE* dst_descriptor = (descriptor_table_cache.m_base_descriptor + offset);\n\t\tfor (uint32_t i = 0; i < num_descriptors; ++i)\n\t\t{\n\t\t\tdst_descriptor[i] = CD3DX12_CPU_DESCRIPTOR_HANDLE(src_desc.m_native, i, m_descriptor_handle_increment_size);\n\t\t}\n\n\t\t// Set the root parameter index bit to make sure the descriptor table \n\t\t// at that index is bound to the command list.\n\t\tm_stale_descriptor_table_bit_mask |= (1 << root_param_idx);\n\t}\n\n\tuint32_t RTDescriptorHeap::ComputeStaleDescriptorCount() const\n\t{\n\t\tuint32_t num_stale_desc = 0;\n\t\tDWORD i;\n\t\tDWORD stale_desc_bitmask = m_stale_descriptor_table_bit_mask;\n\n\t\twhile (_BitScanForward(&i, stale_desc_bitmask))\n\t\t{\n\t\t\tnum_stale_desc += m_descriptor_table_cache[i].m_num_descriptors;\n\t\t\tstale_desc_bitmask ^= (1 << i);\n\t\t}\n\n\t\treturn num_stale_desc;\n\t}\n\n\td3d12::DescriptorHeap* RTDescriptorHeap::CreateDescriptorHeap()\n\t{\n\t\td3d12::desc::DescriptorHeapDesc desc;\n\t\tdesc.m_type = m_desc_heap_type;\n\t\tdesc.m_num_descriptors = m_num_descr_per_heap;\n\t\tdesc.m_shader_visible = true;\n\t\tdesc.m_versions = 1;\n\n\t\td3d12::DescriptorHeap* descriptor_heap = d3d12::CreateDescriptorHeap(m_device, desc);\n\n\t\treturn descriptor_heap;\n\t}\n\n\tvoid RTDescriptorHeap::CommitStagedDescriptorsForDraw(d3d12::CommandList& cmd_list, unsigned int frame_idx)\n\t{\n\t\t// Compute the number of descriptors that need to be copied \n\t\tuint32_t num_descriptors_to_commit = ComputeStaleDescriptorCount();\n\n\t\tif (num_descriptors_to_commit > 0)\n\t\t{\n\t\t\tif (m_num_free_handles[frame_idx] < num_descriptors_to_commit)\n\t\t\t{\n\t\t\t\tLOGC(\"ERROR: Descriptor Heap for RT is full\");\n\t\t\t}\n\n\t\t\td3d12::BindDescriptorHeap(&cmd_list, m_heap, m_desc_heap_type, frame_idx, d3d12::GetRaytracingType(m_device) == RaytracingType::FALLBACK);\n\n\t\t\tDWORD root_idx;\n\t\t\t// Scan from LSB to MSB for a bit set in staleDescriptorsBitMask\n\t\t\twhile (_BitScanForward(&root_idx, m_stale_descriptor_table_bit_mask))\n\t\t\t{\n\t\t\t\tstd::uint32_t num_src_desc = m_descriptor_table_cache[root_idx].m_num_descriptors;\n\t\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE* pSrcDescriptorHandles = m_descriptor_table_cache[root_idx].m_base_descriptor;\n\n\t\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE pDestDescriptorRangeStarts[] =\n\t\t\t\t{\n\t\t\t\t\tm_current_cpu_desc_handle.m_native\n\t\t\t\t};\n\t\t\t\tstd::uint32_t pDestDescriptorRangeSizes[] =\n\t\t\t\t{\n\t\t\t\t\tnum_src_desc\n\t\t\t\t};\n\n\t\t\t\t// Copy the staged CPU visible descriptors to the GPU visible descriptor heap.\n\t\t\t\tm_device->m_native->CopyDescriptors(1, pDestDescriptorRangeStarts, pDestDescriptorRangeSizes,\n\t\t\t\t\tnum_src_desc, pSrcDescriptorHandles, nullptr, static_cast<D3D12_DESCRIPTOR_HEAP_TYPE>(m_desc_heap_type));\n\n\t\t\t\t// Set the descriptors on the command list using the passed-in setter function.\n\t\t\t\td3d12::BindDescriptorTable(&cmd_list, m_current_gpu_desc_handle, root_idx);\n\n\t\t\t\t// Offset current CPU and GPU descriptor handles.\n\t\t\t\td3d12::Offset(m_current_cpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size);\n\t\t\t\td3d12::Offset(m_current_gpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size);\n\n\t\t\t\tm_num_free_handles[frame_idx] -= num_src_desc;\n\n\t\t\t\t// Flip the stale bit so the descriptor table is not recopied again unless it is updated with a new descriptor.\n\t\t\t\tm_stale_descriptor_table_bit_mask ^= (1 << root_idx);\n\t\t\t}\n\t\t}\n\t}\n\n\tvoid RTDescriptorHeap::CommitStagedDescriptorsForDispatch(d3d12::CommandList& cmd_list, unsigned int frame_idx)\n\t{\n\t\tm_current_gpu_desc_handle = d3d12::GetGPUHandle(m_heap, frame_idx);\n\t\tm_current_cpu_desc_handle = d3d12::GetCPUHandle(m_heap, frame_idx);\n\n\t\t// Compute the number of descriptors that need to be copied \n\t\tuint32_t num_descriptors_to_commit = ComputeStaleDescriptorCount();\n\n\t\tif (num_descriptors_to_commit > 0)\n\t\t{\n\t\t\tif (m_num_free_handles[frame_idx] < num_descriptors_to_commit)\n\t\t\t{\n\t\t\t\tLOGC(\"ERROR: Descriptor Heap for RT is full\");\n\t\t\t}\n\n\t\t\td3d12::BindDescriptorHeap(&cmd_list, m_heap, m_desc_heap_type, frame_idx, d3d12::GetRaytracingType(m_device) == RaytracingType::FALLBACK);\n\n\t\t\tDWORD root_idx;\n\t\t\t// Scan from LSB to MSB for a bit set in staleDescriptorsBitMask\n\t\t\twhile (_BitScanForward(&root_idx, m_stale_descriptor_table_bit_mask))\n\t\t\t{\n\t\t\t\tstd::uint32_t num_src_desc = m_descriptor_table_cache[root_idx].m_num_descriptors - 9;\n\t\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE* pSrcDescriptorHandles = m_descriptor_table_cache[root_idx].m_base_descriptor;\n\n\t\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE pDestDescriptorRangeStarts[] =\n\t\t\t\t{\n\t\t\t\t\tm_current_cpu_desc_handle.m_native\n\t\t\t\t};\n\t\t\t\tstd::uint32_t pDestDescriptorRangeSizes[] =\n\t\t\t\t{\n\t\t\t\t\tnum_src_desc\n\t\t\t\t};\n\n\t\t\t\t// Copy the staged CPU visible descriptors to the GPU visible descriptor heap.\n\t\t\t\tm_device->m_native->CopyDescriptors(1, pDestDescriptorRangeStarts, pDestDescriptorRangeSizes,\n\t\t\t\t\tnum_src_desc, pSrcDescriptorHandles, nullptr, static_cast<D3D12_DESCRIPTOR_HEAP_TYPE>(m_desc_heap_type));\n\n\t\t\t\t// Set the descriptors on the command list using the setter function.\n\t\t\t\td3d12::BindComputeDescriptorTable(&cmd_list, m_current_gpu_desc_handle, root_idx);\n\n\t\t\t\t// Offset current CPU and GPU descriptor handles.\n\t\t\t\td3d12::Offset(m_current_cpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size);\n\t\t\t\td3d12::Offset(m_current_gpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size);\n\n\t\t\t\tm_num_free_handles[frame_idx] -= num_src_desc;\n\n\t\t\t\t// Flip the stale bit so the descriptor table is not recopied again unless it is updated with a new descriptor.\n\t\t\t\tm_stale_descriptor_table_bit_mask ^= (1 << root_idx);\n\t\t\t}\n\t\t}\n\t}\n\n\td3d12::DescHeapGPUHandle RTDescriptorHeap::CopyDescriptor(d3d12::CommandList& cmd_list, d3d12::DescHeapCPUHandle cpu_desc, unsigned int frame_idx)\n\t{\n\t\tif (m_num_free_handles[frame_idx] < 1)\n\t\t{\n\t\t\tLOGC(\"ERROR: Descriptor Heap for RT is full\");\n\t\t}\n\n\t\td3d12::BindDescriptorHeap(&cmd_list, m_heap, m_desc_heap_type, frame_idx, d3d12::GetRaytracingType(m_device) == RaytracingType::FALLBACK);\n\n\t\t// When updating the descriptor heap on the command list, all descriptor\n\t\t// tables must be (re)recopied to the new descriptor heap (not just\n\t\t// the stale descriptor tables).\n\t\tm_stale_descriptor_table_bit_mask = m_descriptor_table_bit_mask;\n\n\t\td3d12::DescHeapGPUHandle h_gpu = m_current_gpu_desc_handle;\n\n\t\tm_device->m_native->CopyDescriptorsSimple(1, m_current_cpu_desc_handle.m_native, cpu_desc.m_native, static_cast<D3D12_DESCRIPTOR_HEAP_TYPE>(m_desc_heap_type));\n\n\t\td3d12::Offset(m_current_cpu_desc_handle, 1, m_descriptor_handle_increment_size);\n\t\td3d12::Offset(m_current_gpu_desc_handle, 1, m_descriptor_handle_increment_size);\n\n\t\tm_num_free_handles[frame_idx] -= 1;\n\n\t\treturn h_gpu;\n\t}\n\n\tvoid RTDescriptorHeap::Reset(unsigned int frame_idx)\n\t{\n\t\tm_current_cpu_desc_handle.m_native = CD3DX12_CPU_DESCRIPTOR_HANDLE(D3D12_DEFAULT);\n\t\tm_current_gpu_desc_handle.m_native = CD3DX12_GPU_DESCRIPTOR_HANDLE(D3D12_DEFAULT);\n\t\tm_num_free_handles[frame_idx] = m_num_descr_per_heap;\n\t\tm_descriptor_table_bit_mask = 0;\n\t\tm_stale_descriptor_table_bit_mask = 0;\n\n\t\t// Reset the table cache\n\t\tfor (int i = 0; i < MaxDescriptorTables; ++i)\n\t\t{\n\t\t\tm_descriptor_table_cache[i].Reset();\n\t\t}\n\t}\n\n\n}"
  },
  {
    "path": "src/d3d12/d3d12_rt_descriptor_heap.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"d3d12_structs.hpp\"\n#include \"d3d12_enums.hpp\"\n\n#include <cstdint>\n#include <memory>\n#include <queue>\n\n\n\nnamespace wr\n{\n\tnamespace d3d12\n\t{\n\t\tstruct Device;\n\t};\n\t\n\tclass RTDescriptorHeap\n\t{\n\tpublic:\n\t\tRTDescriptorHeap(wr::d3d12::Device* device, DescriptorHeapType type, uint32_t num_descriptors_per_heap = 5000);\n\n\t\tvirtual ~RTDescriptorHeap();\n\n\t\t/**\n\t\t * Stages a contiguous range of CPU visible descriptors.\n\t\t * Descriptors are not copied to the GPU visible descriptor heap until\n\t\t * the CommitStagedDescriptors function is called.\n\t\t */\n\t\tvoid StageDescriptors(uint32_t root_param_idx, uint32_t offset, uint32_t num_descriptors, const d3d12::DescHeapCPUHandle src_desc);\n\n\t\t/**\n\t\t * Copy all of the staged descriptors to the GPU visible descriptor heap and\n\t\t * bind the descriptor heap and the descriptor tables to the command list.\n\t\t * The passed-in function object is used to set the GPU visible descriptors\n\t\t * on the command list. Two possible functions are:\n\t\t *   * Before a draw    : ID3D12GraphicsCommandList::SetGraphicsRootDescriptorTable\n\t\t *   * Before a dispatch: ID3D12GraphicsCommandList::SetComputeRootDescriptorTable\n\t\t */\n\t\tvoid CommitStagedDescriptorsForDraw(d3d12::CommandList& cmd_list, unsigned int frame_idx);\n\t\tvoid CommitStagedDescriptorsForDispatch(d3d12::CommandList& cmd_list, unsigned int frame_idx);\n\n\t\t/**\n\t\t * Copies a single CPU visible descriptor to a GPU visible descriptor heap.\n\t\t * This is useful for the\n\t\t *   * ID3D12GraphicsCommandList::ClearUnorderedAccessViewFloat\n\t\t *   * ID3D12GraphicsCommandList::ClearUnorderedAccessViewUint\n\t\t * methods which require both a CPU and GPU visible descriptors for a UAV\n\t\t * resource.\n\t\t *\n\t\t * @param commandList The command list is required in case the GPU visible\n\t\t * descriptor heap needs to be updated on the command list.\n\t\t * @param cpuDescriptor The CPU descriptor to copy into a GPU visible\n\t\t * descriptor heap.\n\t\t *\n\t\t * @return The GPU visible descriptor.\n\t\t */\n\t\td3d12::DescHeapGPUHandle CopyDescriptor(d3d12::CommandList& cmd_list, d3d12::DescHeapCPUHandle cpu_desc, unsigned int frame_idx);\n\n\t\t/**\n\t\t * Parse the root signature to determine which root parameters contain\n\t\t * descriptor tables and determine the number of descriptors needed for\n\t\t * each table.\n\t\t */\n\t\tvoid ParseRootSignature(const d3d12::RootSignature& root_signature);\n\n\t\t/**\n\t\t * Reset used descriptors. This should only be done if any descriptors\n\t\t * that are being referenced by a command list has finished executing on the\n\t\t * command queue.\n\t\t */\n\t\tvoid Reset(unsigned int frame_idx);\n\n\t\t/**\n\t\t* Ideally, this is only used when building the acceleration structures.\n\t\t* Don't call it otherwise.\n\t\t*/\n\t\td3d12::DescriptorHeap* GetHeap() { return m_heap; }\n\n\tprotected:\n\n\tprivate:\n\t\t// Create a new descriptor heap of no descriptor heap is available.\n\t\td3d12::DescriptorHeap* CreateDescriptorHeap();\n\n\t\t// Compute the number of stale descriptors that need to be copied\n\t\t// to GPU visible descriptor heap.\n\t\tuint32_t ComputeStaleDescriptorCount() const;\n\n\t\t/**\n\t\t * The maximum number of descriptor tables per root signature.\n\t\t * A 32-bit mask is used to keep track of the root parameter indices that\n\t\t * are descriptor tables.\n\t\t */\n\t\tstatic const uint32_t MaxDescriptorTables = 32;\n\n\t\t/**\n\t\t * A structure that represents a descriptor table entry in the root signature.\n\t\t */\n\t\tstruct DescriptorTableCache\n\t\t{\n\t\t\tDescriptorTableCache()\n\t\t\t\t: m_num_descriptors(0)\n\t\t\t\t, m_base_descriptor(nullptr)\n\t\t\t{}\n\n\t\t\t// Reset the table cache.\n\t\t\tvoid Reset()\n\t\t\t{\n\t\t\t\tm_num_descriptors = 0;\n\t\t\t\tm_base_descriptor = nullptr;\n\t\t\t}\n\n\t\t\t// The number of descriptors in this descriptor table.\n\t\t\tuint32_t m_num_descriptors;\n\t\t\t// The pointer to the descriptor in the descriptor handle cache.\n\t\t\tD3D12_CPU_DESCRIPTOR_HANDLE* m_base_descriptor;\n\t\t};\n\n\t\t// Describes the type of descriptors that can be staged using this \n\t\t// dynamic descriptor heap.\n\t\t// Valid values are:\n\t\t//   * D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV\n\t\t//   * D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER\n\t\t// This parameter also determines the type of GPU visible descriptor heap to \n\t\t// create.\n\t\tDescriptorHeapType m_desc_heap_type;\n\n\t\t// The number of descriptors to allocate in new GPU visible descriptor heaps.\n\t\tuint32_t m_num_descr_per_heap;\n\n\t\tuint32_t m_descriptor_handle_increment_size;\n\n\t\t// The descriptor handle cache.\n\t\tstd::unique_ptr<D3D12_CPU_DESCRIPTOR_HANDLE[]> m_descriptor_handle_cache;\n\n\t\t// Descriptor handle cache per descriptor table.\n\t\tDescriptorTableCache m_descriptor_table_cache[MaxDescriptorTables];\n\n\t\t// Each bit in the bit mask represents the index in the root signature\n\t\t// that contains a descriptor table.\n\t\tuint32_t m_descriptor_table_bit_mask;\n\n\t\t// Each bit set in the bit mask represents a descriptor table\n\t\t// in the root signature that has changed since the last time the \n\t\t// descriptors were copied.\n\t\tuint32_t m_stale_descriptor_table_bit_mask;\n\n\t\td3d12::DescriptorHeap* m_heap;\n\t\td3d12::DescHeapGPUHandle m_current_gpu_desc_handle;\n\t\td3d12::DescHeapCPUHandle m_current_cpu_desc_handle;\n\n\t\tuint32_t m_num_free_handles[d3d12::settings::num_back_buffers];\n\n\t\twr::d3d12::Device* m_device;\n\t};\n}"
  },
  {
    "path": "src/d3d12/d3d12_settings.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <d3d12.h>\n#include <cstdint>\n#include <dxgi1_5.h>\n\n#include \"d3d12_enums.hpp\"\n\nnamespace wr::d3d12::settings\n{\n\n\tenum class DebugLayer\n\t{\n\t\tENABLE,\n\t\tDISABLE, \n\t\tENABLE_WITH_GPU_VALIDATION\n\t};\n\n\tstatic const std::vector<D3D_FEATURE_LEVEL> possible_feature_levels =\n\t{\n\t\tD3D_FEATURE_LEVEL_12_1,\n\t\tD3D_FEATURE_LEVEL_12_0,\n\t\tD3D_FEATURE_LEVEL_11_1,\n\t\tD3D_FEATURE_LEVEL_11_0\n\t};\n\n\tstatic const constexpr bool output_hdr = false;\n\tstatic const constexpr Format back_buffer_format = output_hdr ? Format::R16G16B16A16_FLOAT : Format::R8G8B8A8_UNORM;\n\tstatic const constexpr DXGI_SWAP_EFFECT flip_mode = DXGI_SWAP_EFFECT_FLIP_DISCARD;\n\tstatic const constexpr DXGI_SWAP_CHAIN_FLAG swapchain_flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;\n\tstatic const constexpr DXGI_SCALING swapchain_scaling = DXGI_SCALING_STRETCH;\n\tstatic const constexpr DXGI_ALPHA_MODE swapchain_alpha_mode = DXGI_ALPHA_MODE_UNSPECIFIED;\n\tstatic const constexpr bool enable_gpu_timeout = false;\n\tstatic const constexpr bool enable_debug_factory = false;\n\tstatic const constexpr DebugLayer enable_debug_layer = DebugLayer::DISABLE;\t//Don't use ENABLE_WITH_GPU_VALIDATION (Raytracing); it breaks\n\tstatic const constexpr char* default_shader_model = \"6_3\";\n\tstatic std::array<LPCWSTR, 1> debug_shader_args = { L\"/O3\" };\n\tstatic std::array<LPCWSTR, 1> release_shader_args = { L\"/O3\" };\n\tstatic const constexpr std::uint8_t num_back_buffers = 3;\n\tstatic const constexpr std::uint32_t num_instances_per_batch = 768U;\t\t//48 KiB for ObjectData[]\n\tstatic const constexpr std::uint32_t num_lights = 21'845;\t\t\t\t\t//1 MiB for StructuredBuffer<Light>\n\tstatic const constexpr std::uint32_t num_indirect_draw_commands = 8;\t\t//Allow 8 different meshes non-indexed\n\tstatic const constexpr std::uint32_t num_indirect_index_commands = 32;\t\t//Allow 32 different meshes indexed\n\tstatic const constexpr bool use_bundles = false;\n\tstatic const constexpr bool force_dxr_fallback = false;\n\tstatic const constexpr bool disable_rtx = false;\n\tstatic const constexpr bool enable_object_culling = true;\n\tstatic const constexpr unsigned int num_max_rt_materials = 3000;\n\tstatic const constexpr unsigned int num_max_rt_textures = 1000;\n\tstatic const constexpr unsigned int fallback_ptrs_offset = 3500;\n\tstatic const constexpr std::uint32_t res_skybox = 1024;\n\tstatic const constexpr std::uint32_t res_envmap = 512;\n\tstatic const constexpr unsigned int shadow_denoiser_wavelet_iterations = 4; // controls the number of iterations of the shadow denoiser, controlling the effective size of the kernel (size = 2^i + 1)\n\tstatic const constexpr unsigned int shadow_denoiser_feedback_tap = 1; // After which of the iterations should the result be stored for denoising the next frame.\n\t\n} /* wr::d3d12::settings */\n"
  },
  {
    "path": "src/d3d12/d3d12_shader.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include <D3Dcompiler.h>\n#include <dxcapi.h>\n\n#include \"../util/log.hpp\"\n#include \"d3d12_defines.hpp\"\n#include <sstream>\n\nnamespace wr::d3d12\n{\n\n\tnamespace internal\n\t{\n\n\t\tstd::string ShaderTypeToString(ShaderType type)\n\t\t{\n\t\t\tstd::string prefix = \"unknown\";\n\n\t\t\tswitch (type)\n\t\t\t{\n\t\t\tcase ShaderType::VERTEX_SHADER:\n\t\t\t\tprefix = \"vs_\";\n\t\t\t\tbreak;\n\t\t\tcase ShaderType::PIXEL_SHADER:\n\t\t\t\tprefix = \"ps_\";\n\t\t\t\tbreak;\n\t\t\tcase ShaderType::DOMAIN_SHADER:\n\t\t\t\tprefix = \"ds_\";\n\t\t\t\tbreak;\n\t\t\tcase ShaderType::GEOMETRY_SHADER:\n\t\t\t\tprefix = \"gs_\";\n\t\t\t\tbreak;\n\t\t\tcase ShaderType::HULL_SHADER:\n\t\t\t\tprefix = \"hs_\";\n\t\t\t\tbreak;\n\t\t\tcase ShaderType::DIRECT_COMPUTE_SHADER:\n\t\t\t\tprefix = \"cs_\";\n\t\t\t\tbreak;\n\t\t\tcase ShaderType::LIBRARY_SHADER:\n\t\t\t\tprefix = \"lib_\";\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\treturn prefix + std::string(d3d12::settings::default_shader_model);\n\t\t}\n\n\t} /* internal */\n\n\tstd::variant<Shader*, std::string> LoadShader(Device* device, ShaderType type, std::string const & path, std::string const & entry, std::vector<std::pair<std::wstring, std::wstring>> user_defines)\n\t{\n\t\tauto shader = new Shader();\n\n\t\tshader->m_entry = entry;\n\t\tshader->m_path = path;\n\t\tshader->m_type = type;\n\t\tshader->m_defines = user_defines;\n\n\t\tstd::wstring wpath(path.begin(), path.end());\n\t\tstd::wstring wentry(entry.begin(), entry.end());\n\t\tstd::string temp_shader_type(internal::ShaderTypeToString(type));\n\t\tstd::wstring wshader_type(temp_shader_type.begin(), temp_shader_type.end());\n\n\t\tIDxcLibrary* library = nullptr;\n\t\tDxcCreateInstance(CLSID_DxcLibrary, __uuidof(IDxcLibrary), (void **)&library);\n\n\t\tIDxcBlobEncoding* source;\n\t\tstd::uint32_t code_page = CP_ACP;\n\t\tlibrary->CreateBlobFromFile(wpath.c_str(), &code_page, &source);\n\n\t\tIDxcIncludeHandler* include_handler;\n\t\tTRY_M(library->CreateIncludeHandler(&include_handler), \"Failed to create default include handler.\");\n\n\t\tstd::vector<DxcDefine> defines;\n\t\tif (GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t{\n\t\t\tdefines.push_back({L\"FALLBACK\", L\"1\"});\n\t\t}\n\n\t\tfor (auto& define : user_defines)\n\t\t{\n\t\t\tdefines.push_back({ define.first.c_str(), define.second.c_str() });\n\t\t}\n\n\t\tIDxcOperationResult* result;\n\t\tHRESULT hr = Device::m_compiler->Compile(\n\t\t\tsource,          // program text\n\t\t\twpath.c_str(),   // file name, mostly for error messages\n\t\t\twentry.c_str(),         // entry point function\n\t\t\twshader_type.c_str(),   // target profile\n#ifdef _DEBUG\n\t\t\td3d12::settings::debug_shader_args.data(), static_cast<uint32_t>(d3d12::settings::debug_shader_args.size()),\n#else\n\t\t\td3d12::settings::release_shader_args.data(), static_cast<uint32_t>(d3d12::settings::release_shader_args.size()),\n#endif\n\t\t\tdefines.data(), static_cast<std::uint32_t>(defines.size()),       // name/value defines and their count\n\t\t\tinclude_handler,          // handler for #include directives\n\t\t\t&result);\n\n\t\tif (FAILED(hr))\n\t\t{\n\t\t\tstd::string error_msg = \"Failed to compile \" + path + \" (entry: \" + entry + \"), Incorrect entry or path?\";\n\t\t\treturn error_msg;\n\t\t}\n\n\t\t// compiler output\n\t\tresult->GetStatus(&hr);\n\t\tif (FAILED(hr))\n\t\t{\n\t\t\tdelete shader;\n\n\t\t\tIDxcBlobEncoding* error;\n\t\t\tresult->GetErrorBuffer(&error);\n\t\t\treturn std::string((char*)error->GetBufferPointer());\n\t\t}\n\n\t\tresult->GetResult(&shader->m_native);\n\n\t\treturn shader;\n\t}\n\n\tvoid Destroy(Shader* shader)\n\t{\n\t\tSAFE_RELEASE(shader->m_native);\n\t\tdelete shader;\n\t}\n\n} /* wr::d3d12 */\n"
  },
  {
    "path": "src/d3d12/d3d12_shader_table.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include <variant>\n\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\t\n\tShaderRecord CreateShaderRecord(void* identifier, std::uint64_t identifier_size, void* local_root_args, std::uint64_t local_root_args_size)\n\t{\n\t\tShaderRecord record;\n\n\t\trecord.m_shader_identifier = { identifier, identifier_size };\n\t\trecord.m_local_root_args = { local_root_args, local_root_args_size };\n\n\t\treturn record;\n\t}\n\n\t// TODO: This might be problematic\n\tvoid CopyShaderRecord(ShaderRecord src, void* dest)\n\t{\n\t\tuint8_t* byte_dest = static_cast<uint8_t*>(dest);\n\t\tmemcpy(byte_dest, src.m_shader_identifier.first, src.m_shader_identifier.second);\n\t\tif (src.m_local_root_args.first)\n\t\t{\n\t\t\t// byte_dest = static_cast<uint8_t*>(dest.m_local_root_args.first);\n\t\t\tmemcpy(byte_dest + src.m_shader_identifier.second, src.m_local_root_args.first, src.m_local_root_args.second);\n\t\t}\n\t}\n\n\tShaderTable* CreateShaderTable(Device* device,\n\t\t\tstd::uint64_t num_shader_records,\n\t\t\tstd::uint64_t shader_record_size)\n\t{\n\t\tauto table = new ShaderTable();\n\n\t\ttable->m_shader_record_size = SizeAlignTwoPower(shader_record_size, D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT);\n\t\ttable->m_shader_records.reserve(num_shader_records);\n\t\tstd::uint64_t buffer_size = num_shader_records * table->m_shader_record_size;\n\t\t\n\t\tauto uploadHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\n\t\tauto bufferDesc = CD3DX12_RESOURCE_DESC::Buffer(buffer_size);\n\t\tTRY(device->m_native->CreateCommittedResource(\n\t\t\t&uploadHeapProperties,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&bufferDesc,\n\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&table->m_resource)));\n\t\ttable->m_resource->SetName(L\"Shader table resource\");\n\n\n\t\tuint8_t* mappedData;\n\t\t// We don't unmap this until the app closes. Keeping buffer mapped for the lifetime of the resource is okay.\n\t\tCD3DX12_RANGE readRange(0, 0);        // We do not intend to read from this resource on the CPU.\n\t\tTRY_M(table->m_resource->Map(0, &readRange, reinterpret_cast<void**>(&mappedData)), \"Failed to map shader table resource\");\n\n\t\ttable->m_mapped_shader_records = mappedData;\n\n\t\treturn table;\n\t}\n\n\tvoid AddShaderRecord(ShaderTable* table, ShaderRecord record)\n\t{\t\t\n\t\tassert(table->m_shader_records.size() < table->m_shader_records.capacity());\n\t\ttable->m_shader_records.push_back(record);\n\t\tCopyShaderRecord(record, table->m_mapped_shader_records);\n\t\ttable->m_mapped_shader_records += table->m_shader_record_size;\n\t}\n\n\tvoid Destroy(ShaderTable* table)\n\t{\n\t\tSAFE_RELEASE(table->m_resource);\n\t\tdelete table;\n\t}\n\n} /* wr::d3d12 */\n"
  },
  {
    "path": "src/d3d12/d3d12_staging_buffer.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"d3d12_defines.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tStagingBuffer* CreateStagingBuffer(Device* device, void* data, uint64_t size, uint64_t stride, ResourceState resource_state)\n\t{\n\t\tauto buffer = new StagingBuffer();\n\n\t\tbuffer->m_target_resource_state = resource_state;\n\t\tbuffer->m_size = size;\n\t\tbuffer->m_stride_in_bytes = stride;\n\t\tbuffer->m_is_staged = true;\n\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);\n\t\tCD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);\n\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(size);\n\n\t\tdevice->m_native->CreateCommittedResource(\n\t\t\t&heap_properties_default,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&buffer_desc,\n\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(resource_state),\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&buffer->m_buffer));\n\t\tNAME_D3D12RESOURCE(buffer->m_buffer)\n\n\t\tdevice->m_native->CreateCommittedResource(\n\t\t\t&heap_properties_upload,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&buffer_desc,\n\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&buffer->m_staging));\n\t\tNAME_D3D12RESOURCE(buffer->m_staging);\n\n\t\tCD3DX12_RANGE read_range(0, size);\n\n\t\tbuffer->m_staging->Map(0, &read_range, reinterpret_cast<void**>(&(buffer->m_cpu_address)));\n\n\t\tif (data != nullptr)\n\t\t{\n\t\t\tmemcpy(buffer->m_cpu_address, data, size);\n\t\t}\n\n\t\treturn buffer;\n\t}\n\n\tvoid SetName(StagingBuffer * buffer, std::wstring name)\n\t{\n\t\tbuffer->m_buffer->SetName((name + L\" Destination buffer\").c_str());\n\t\tbuffer->m_staging->SetName((name + L\" Intermediate buffer\").c_str());\n\t}\n\n\tvoid UpdateStagingBuffer(StagingBuffer* buffer, void * data, std::uint64_t size, std::uint64_t offset)\n\t{\n\t\tmemcpy(static_cast<std::uint8_t*>(buffer->m_cpu_address) + offset, data, size);\n\t}\n\n\tvoid StageBuffer(StagingBuffer* buffer, CommandList* cmd_list)\n\t{\n\n\t\tif (buffer->m_is_staged)\n\t\t{\n\t\t\tCD3DX12_RESOURCE_BARRIER resource_barrier = CD3DX12_RESOURCE_BARRIER::Transition(buffer->m_buffer, (D3D12_RESOURCE_STATES)buffer->m_target_resource_state, D3D12_RESOURCE_STATE_COPY_DEST);\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &resource_barrier);\n\t\t}\n\n\t\tcmd_list->m_native->CopyBufferRegion(buffer->m_buffer, 0, buffer->m_staging, 0, buffer->m_size);\n\n\t\t// transition the vertex buffer data from copy destination state to vertex buffer state\n\t\tCD3DX12_RESOURCE_BARRIER vertex_barrier = CD3DX12_RESOURCE_BARRIER::Transition(buffer->m_buffer, D3D12_RESOURCE_STATE_COPY_DEST, (D3D12_RESOURCE_STATES)buffer->m_target_resource_state);\n\t\tcmd_list->m_native->ResourceBarrier(1, &vertex_barrier);\n\n\t\tbuffer->m_gpu_address = buffer->m_buffer->GetGPUVirtualAddress();\n\t\tbuffer->m_is_staged = true;\n\t}\n\n\tvoid StageBufferRegion(StagingBuffer * buffer, std::uint64_t size, std::uint64_t offset, CommandList * cmd_list)\n\t{\n\t\tif (buffer->m_is_staged)\n\t\t{\n\t\t\tCD3DX12_RESOURCE_BARRIER resource_barrier = CD3DX12_RESOURCE_BARRIER::Transition(buffer->m_buffer, (D3D12_RESOURCE_STATES)buffer->m_target_resource_state, D3D12_RESOURCE_STATE_COPY_DEST);\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &resource_barrier);\n\t\t}\n\n\t\tcmd_list->m_native->CopyBufferRegion(buffer->m_buffer, offset, buffer->m_staging, offset, size);\n\n\t\t// transition the vertex buffer data from copy destination state to vertex buffer state\n\t\tCD3DX12_RESOURCE_BARRIER vertex_barrier = CD3DX12_RESOURCE_BARRIER::Transition(buffer->m_buffer, D3D12_RESOURCE_STATE_COPY_DEST, (D3D12_RESOURCE_STATES)buffer->m_target_resource_state);\n\t\tcmd_list->m_native->ResourceBarrier(1, &vertex_barrier);\n\n\t\tbuffer->m_gpu_address = buffer->m_buffer->GetGPUVirtualAddress();\n\t\tbuffer->m_is_staged = true;\n\t}\n\n\tvoid FreeStagingBuffer(StagingBuffer* buffer)\n\t{\n\t\tSAFE_RELEASE(buffer->m_staging);\n\t\tbuffer->m_is_staged = false;\n\t}\n\n\tvoid Evict(StagingBuffer * buffer)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\t\tbuffer->m_staging->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tstd::array<ID3D12Pageable*, 2> objects{ buffer->m_staging, buffer->m_buffer };\n\t\tn_device->Evict(static_cast<unsigned int>(objects.size()), objects.data());\n\t}\n\n\tvoid MakeResident(StagingBuffer * buffer)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\t\tbuffer->m_staging->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tstd::array<ID3D12Pageable*, 2> objects{ buffer->m_staging, buffer->m_buffer };\n\t\tn_device->MakeResident(static_cast<unsigned int>(objects.size()), objects.data());\n\t}\n\n\tvoid CreateRawSRVFromStagingBuffer(StagingBuffer* buffer, DescHeapCPUHandle& handle, unsigned int count, Format format)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\t\tbuffer->m_buffer->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tauto increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);\n\n\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\n\t\tsrv_desc.Format = static_cast<DXGI_FORMAT>(format);\n\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\n\t\tsrv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;\n\t\tsrv_desc.Buffer.NumElements = count;\n\t\tsrv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW;\n\n\t\tn_device->CreateShaderResourceView(buffer->m_buffer, &srv_desc, handle.m_native);\n\t\tOffset(handle, 1, increment_size);\n\t}\n\n#include \"../vertex.hpp\"\n\n\tvoid CreateStructuredBufferSRVFromStagingBuffer(StagingBuffer* buffer, DescHeapCPUHandle& handle, unsigned int count, Format format)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\t\tbuffer->m_buffer->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tauto increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);\n\n\t\tauto stride = static_cast<std::uint32_t>(sizeof(wr::Vertex));\n\n\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\n\t\tsrv_desc.Format = static_cast<DXGI_FORMAT>(format);\n\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\n\t\tsrv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;\n\t\tsrv_desc.Buffer.FirstElement = 0;\n\t\tsrv_desc.Buffer.NumElements = count;\n\t\tsrv_desc.Buffer.StructureByteStride = stride;\n\t\tsrv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE;\n\n\t\tn_device->CreateShaderResourceView(buffer->m_buffer, &srv_desc, handle.m_native);\n\t\tOffset(handle, 1, increment_size);\n\t}\n\n\tvoid Destroy(StagingBuffer* buffer)\n\t{\n\t\tSAFE_RELEASE(buffer->m_staging);\n\t\tSAFE_RELEASE(buffer->m_buffer);\n\t\tdelete buffer;\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_state_object.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"d3d12_defines.hpp\"\n#include \"d3dx12.hpp\"\n\n#include <wrl/client.h>\n\nnamespace wr::d3d12\n{\n\n\t[[nodiscard]] StateObject* CreateStateObject(Device* device, desc::StateObjectDesc desc)\n\t{\n\t\tauto state_object = new StateObject();\n\n\t\tstate_object->m_desc = desc;\n\t\tstate_object->m_device = device;\n\n\t\t// Create CD3DX12_STATE_OBJECT_DESC\n\t\tCD3DX12_STATE_OBJECT_DESC n_desc = { D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE };\n\n\t\t// Shader Library\n\t\t{\n\t\t\tD3D12_SHADER_BYTECODE bytecode = {};\n\t\t\tbytecode.BytecodeLength = desc.m_library->m_native->GetBufferSize();\n\t\t\tbytecode.pShaderBytecode = desc.m_library->m_native->GetBufferPointer();\n\t\t\tauto lib = n_desc.CreateSubobject<CD3DX12_DXIL_LIBRARY_SUBOBJECT>();\n\t\t\tfor (auto exp : desc.m_library_exports)\n\t\t\t{\n\t\t\t\tlib->DefineExport(exp.c_str());\n\t\t\t}\n\t\t\tlib->SetDXILLibrary(&bytecode);\n\t\t}\n\n\t\t// Shader Config\n\t\t{\n\t\t\tauto shader_config = n_desc.CreateSubobject<CD3DX12_RAYTRACING_SHADER_CONFIG_SUBOBJECT>();\n\t\t\tshader_config->Config(desc.max_payload_size, desc.max_attributes_size);\n\t\t}\n\n\t\t// Hitgroup\n\t\t{\n\t\t\tfor (auto def : desc.m_hit_groups)\n\t\t\t{\n\t\t\t\tauto hitGroup = n_desc.CreateSubobject<CD3DX12_HIT_GROUP_SUBOBJECT>();\n\t\t\t\thitGroup->SetClosestHitShaderImport(def.second.c_str());\n\t\t\t\thitGroup->SetHitGroupExport(def.first.c_str());\n\t\t\t\thitGroup->SetHitGroupType(D3D12_HIT_GROUP_TYPE_TRIANGLES);\n\t\t\t}\n\t\t}\n\n\t\t// Global Root Signature\n\t\tif (auto rs = desc.global_root_signature.value_or(nullptr); desc.global_root_signature.has_value())\n\t\t{\n\t\t\tauto global_rs = n_desc.CreateSubobject<CD3DX12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT>();\n\t\t\tglobal_rs->SetRootSignature(rs->m_native);\n\t\t\tstate_object->m_global_root_signature = rs;\n\t\t}\n\n\t\t// Local Root Signatures\n\t\tif (desc.local_root_signatures.has_value())\n\t\t{\n\t\t\tfor (auto& rs : desc.local_root_signatures.value())\n\t\t\t{\n\t\t\t\tauto local_rs = n_desc.CreateSubobject<CD3DX12_LOCAL_ROOT_SIGNATURE_SUBOBJECT>();\n\t\t\t\tlocal_rs->SetRootSignature(rs->m_native);\n\t\t\t\t// Define explicit shader association for the local root signature.\n\t\t\t\t{\n\t\t\t\t\t//auto rootSignatureAssociation = desc.desc.CreateSubobject<CD3DX12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT>();\n\t\t\t\t\t//rootSignatureAssociation->SetSubobjectToAssociate(*local_rs);\n\t\t\t\t\t//rootSignatureAssociation->AddExport(L\"MyHitGroup\");\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Pipeline Config\n\t\t{\n\t\t\tauto pipeline_config = n_desc.CreateSubobject<CD3DX12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT>();\n\t\t\tpipeline_config->Config(desc.max_recursion_depth);\n\t\t}\n\n\t\t// Create the state object.\n\t\tif (GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t{\n\t\t\tHRESULT hr;\n\t\t\tTRY_M(hr = device->m_native->CreateStateObject(n_desc, IID_PPV_ARGS(&state_object->m_native)),\n\t\t\t\t\"Couldn't create DirectX Raytracing state object.\");\n\n\t\t\t// Shitty code because I don't know what As does.\n\t\t\tMicrosoft::WRL::ComPtr<ID3D12StateObject> temp_state_object(state_object->m_native);\n\t\t\tMicrosoft::WRL::ComPtr<ID3D12StateObjectProperties> temp_properties;\n\n\t\t\tTRY_M(temp_state_object.As(&temp_properties), \"Failed to do ComPtr.As\");\n\t\t\tstate_object->m_properties = temp_properties.Get();\n\n\t\t\ttemp_state_object.Detach();\n\t\t\ttemp_properties.Detach();\n\t\t}\n\t\telse if(GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t{\n\t\t\tTRY_M(device->m_fallback_native->CreateStateObject(n_desc, IID_PPV_ARGS(&state_object->m_fallback_native)),\n\t\t\t\t\"Couldn't create DirectX Fallback Raytracing state object.\");\n\t\t}\n\n\t\tn_desc.DeleteHelpers();\n\n\t\treturn state_object;\n\t}\n\n\tvoid RecreateStateObject(StateObject* state_object)\n\t{\n\t\t*state_object = *CreateStateObject(state_object->m_device, state_object->m_desc);\n\t}\n\n\tstd::uint64_t GetShaderIdentifierSize(Device* device)\n\t{\n\t\t// Get shader identifiers.\n\t\tstd::uint64_t retval = 0;\n\t\tif (GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t{\n\t\t\tretval = D3D12_SHADER_IDENTIFIER_SIZE_IN_BYTES;\n\t\t}\n\t\telse if (GetRaytracingType(device) == RaytracingType::FALLBACK) // DirectX Raytracing\n\t\t{\n\t\t\tretval = device->m_fallback_native->GetShaderIdentifierSize();\n\t\t}\n\n\t\treturn retval;\n\t}\n\n\tvoid SetGlobalRootSignature(StateObject* state_object, RootSignature* global_root_signature)\n\t{\n\t\tstate_object->m_global_root_signature = global_root_signature;\n\t}\n\n\t[[nodiscard]] void* GetShaderIdentifier(Device* device, StateObject* obj, std::string const & name)\n\t{\t\n\t\tstd::wstring wname(name.begin(), name.end());\n\n\t\t// Reusable lambda\n\t\tauto GetShaderIdentifiers = [&](auto* stateObjectProperties)\n\t\t{\n\t\t\treturn stateObjectProperties->GetShaderIdentifier(wname.c_str());\n\t\t};\n\n\t\t// Get shader identifiers.\n\t\tif (GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t{\n\t\t\treturn GetShaderIdentifiers(obj->m_properties);\n\t\t}\n\t\telse if (GetRaytracingType(device) == RaytracingType::FALLBACK) // DirectX Raytracing\n\t\t{\n\t\t\treturn GetShaderIdentifiers(obj->m_fallback_native);\n\t\t}\n\n\t\tLOGC(\"GetShaderIdentifier Called but raytracing isn't enabled!\");\n\n\t\treturn nullptr;\n\t}\n\n\tvoid SetName(StateObject * obj, std::wstring name)\n\t{\n\t\tobj->m_native->SetName(name.c_str());\n\t}\n\n\tvoid Destroy(StateObject* obj)\n\t{\n\t\tSAFE_RELEASE(obj->m_native);\n\t\tSAFE_RELEASE(obj->m_properties);\n\t\tSAFE_RELEASE(obj->m_fallback_native);\n\t\tdelete obj;\n\t}\n\n} /* wr::d3d12 */\n"
  },
  {
    "path": "src/d3d12/d3d12_structs.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <d3d12.h>\n#include <dxgi1_6.h>\n#include <vector>\n#include <optional>\n#include <utility>\n#include <dxcapi.h>\n#include <array>\n#include <bitset>\n#include <D3D12RaytracingFallback.h>\n#include <DirectXMath.h>\n\n#include \"../structs.hpp\"\n\n#include \"d3d12_enums.hpp\"\n#include \"d3d12_settings.hpp\"\n#include \"d3dx12.hpp\"\n\nnamespace wr\n{\n\tclass DynamicDescriptorHeap;\n\tclass DescriptorAllocation;\n\tclass RTDescriptorHeap;\n}\n\nnamespace wr::d3d12\n{\n\n\t// Forward declare\n\tstruct StagingBuffer;\n\tstruct Shader;\n\tstruct RootSignature;\n\n\tnamespace desc\n\t{\n\n\t\tstruct RenderTargetDesc\n\t\t{\n\t\t\tResourceState m_initial_state = ResourceState::RENDER_TARGET;\n\t\t\tbool m_create_dsv_buffer = true;\n\t\t\tFormat m_dsv_format = wr::Format::UNKNOWN;\n\t\t\tstd::array<Format, 8> m_rtv_formats = { wr::Format::UNKNOWN };\n\t\t\tstd::uint32_t m_num_rtv_formats = 0u;\n\t\t\tfloat m_clear_color[4] = { 0.f, 0.f, 0.f, 0.f };\n\t\t};\n\n\t\tstruct TextureDesc\n\t\t{\n\t\t\tResourceState m_initial_state = ResourceState::COPY_DEST;\n\t\t\tFormat m_texture_format = Format::UNKNOWN;\n\n\t\t\tstd::uint32_t m_width = 0u;\n\t\t\tstd::uint32_t m_height = 0u;\n\t\t\tstd::uint32_t m_depth = 0u;\n\t\t\tstd::uint32_t m_array_size = 0u;\n\t\t\tstd::uint32_t m_mip_levels = 0u;\n\n\t\t\tbool m_is_cubemap = false;\n\t\t};\n\n\t\tstruct PipelineStateDesc\n\t\t{\n\t\t\tFormat m_dsv_format;\n\t\t\tstd::array<Format, 8> m_rtv_formats = { wr::Format::UNKNOWN };\n\t\t\tstd::uint32_t m_num_rtv_formats = 0U;\n\n\t\t\tPipelineType m_type = PipelineType::GRAPHICS_PIPELINE;\n\t\t\tCullMode m_cull_mode = CullMode::CULL_BACK;\n\t\t\tbool m_depth_enabled = false;\n\t\t\tbool m_counter_clockwise = false;\n\t\t\tTopologyType m_topology_type = TopologyType::TRIANGLE;\n\n\t\t\tstd::vector<D3D12_INPUT_ELEMENT_DESC> m_input_layout;\n\t\t};\n\n\t\tstruct SamplerDesc\n\t\t{\n\t\t\tTextureFilter m_filter;\n\t\t\tTextureAddressMode m_address_mode;\n\t\t\tBorderColor m_border_color = BorderColor::BORDER_WHITE;\n\t\t};\n\n\t\tstruct RootSignatureDesc\n\t\t{\n\t\t\tstd::vector<CD3DX12_ROOT_PARAMETER> m_parameters;\n\t\t\tstd::vector<desc::SamplerDesc> m_samplers;\n\t\t\tbool m_rtx = false;\n\t\t\tbool m_rt_local = false;\n\t\t};\n\n\t\tstruct DescriptorHeapDesc\n\t\t{\n\t\t\tstd::uint32_t m_num_descriptors = 1u;\n\t\t\tDescriptorHeapType m_type = DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV;\n\t\t\tbool m_shader_visible = true;\n\t\t\tuint32_t m_versions = 1u;\n\t\t};\n\n\t\tstruct GeometryDesc\n\t\t{\n\t\t\tStagingBuffer* vertex_buffer = nullptr;\n\t\t\tstd::optional<StagingBuffer*> index_buffer = std::nullopt;\n\n\t\t\tstd::uint32_t m_num_vertices = 0u;\n\t\t\tstd::uint32_t m_num_indices = 0u;\n\t\t\tstd::uint32_t m_vertices_offset = 0u;\n\t\t\tstd::uint32_t m_indices_offset = 0u;\n\n\t\t\tstd::uint32_t m_vertex_stride = 0u;\n\t\t};\n\n\t\tstruct StateObjectDesc\n\t\t{\n\t\t\tShader* m_library = nullptr;\n\t\t\tstd::vector<std::wstring> m_library_exports;\n\t\t\tstd::vector<std::pair<std::wstring, std::wstring>> m_hit_groups; // first = hit group | second = entry\n\n\t\t\tstd::uint32_t max_payload_size = 0u;\n\t\t\tstd::uint32_t max_attributes_size = 0u;\n\t\t\tstd::uint32_t max_recursion_depth = 0u;\n\n\t\t\tstd::optional<RootSignature*> global_root_signature = std::nullopt;\n\t\t\tstd::optional<std::vector<RootSignature*>> local_root_signatures = { std::nullopt };\n\t\t};\n\n\t} /* desc */\n\n\tstruct Device\n\t{\n\t\tIDXGIAdapter4* m_adapter = nullptr;\n\t\tID3D12Device5* m_native = nullptr;\n\t\tIDXGIFactory6* m_dxgi_factory = nullptr;\n\t\tD3D_FEATURE_LEVEL m_feature_level;\n\n\t\tSYSTEM_INFO m_sys_info;\n\t\tDXGI_ADAPTER_DESC3 m_adapter_info;\n\t\tID3D12Debug1* m_debug_controller = nullptr;\n\t\tID3D12InfoQueue* m_info_queue = nullptr;\n\t\t\n\t\tstatic IDxcCompiler2* m_compiler;\n\n\t\t// Optional supported formats\n\t\tstd::bitset<DXGI_FORMAT_V408> m_optional_formats;\n\n\t\t// Fallback\n\t\tbool m_dxr_fallback_support = false;\n\t\tbool m_dxr_support = false;\n\t\tRaytracingType m_rt_type;\n\t\tID3D12RaytracingFallbackDevice* m_fallback_native = nullptr;\n\n\t\tbool IsFallback() \n\t\t{\n\t\t\treturn m_rt_type == RaytracingType::FALLBACK;\n\t\t}\n\t};\n\n\tstruct CommandQueue\n\t{\n\t\tID3D12CommandQueue* m_native = nullptr;\n\t};\n\n\tstruct CommandList\n\t{\n\t\tstd::vector<ID3D12CommandAllocator*> m_allocators;\n\t\tID3D12GraphicsCommandList4* m_native = nullptr;\n\t\tID3D12RaytracingFallbackCommandList* m_native_fallback = nullptr;\n\n\t\t// Dynamic descriptor heap where staging happens\n\t\tstd::unique_ptr<DynamicDescriptorHeap> m_dynamic_descriptor_heaps[static_cast<size_t>(DescriptorHeapType::DESC_HEAP_TYPE_NUM_TYPES)];\n\t\t\n\t\t// Heap used for RT, shared between all the ray tracing command lists\n\t\tstd::shared_ptr<RTDescriptorHeap> m_rt_descriptor_heap;\n\n\t\t// Keep track of currently bound heaps, so that we can avoid changing when not needed\n\t\tID3D12DescriptorHeap* m_descriptor_heaps[static_cast<size_t>(DescriptorHeapType::DESC_HEAP_TYPE_NUM_TYPES)];\n\t};\n\n\tstruct CommandSignature\n\t{\n\t\tID3D12CommandSignature* m_native = nullptr;\n\t};\n\n\tstruct RenderTarget\n\t{\n\t\tdesc::RenderTargetDesc m_create_info;\n\t\tstd::uint32_t m_frame_idx = 0u;\n\t\tstd::uint32_t m_num_render_targets = 0u;\n\t\tstd::uint32_t m_width = 0u;\n\t\tstd::uint32_t m_height = 0u;\n\n\t\tstd::vector<ID3D12Resource*> m_render_targets;\n\t\tID3D12DescriptorHeap* m_rtv_descriptor_heap = nullptr;\n\t\tstd::uint32_t m_rtv_descriptor_increment_size = 0u;\n\n\t\tID3D12Resource* m_depth_stencil_buffer = nullptr;\n\t\tID3D12DescriptorHeap* m_depth_stencil_resource_heap = nullptr;\n\t};\n\n\tstruct RenderWindow : public RenderTarget\n\t{\n\t\tIDXGISwapChain4* m_swap_chain = nullptr;\n\t};\n\n\tstruct Fence\n\t{\n\t\tID3D12Fence1* m_native = nullptr;\n\t\tHANDLE m_fence_event = nullptr;\n\t\tUINT64 m_fence_value = static_cast<UINT64>(0u);\n\t};\n\n\tstruct RootSignature\n\t{\n\t\tdesc::RootSignatureDesc m_create_info;\n\t\tID3D12RootSignature* m_native = nullptr;\n\n\t\tstd::uint32_t m_num_descriptors_per_table[32] = { 0u };\n\t\tstd::uint32_t m_sampler_table_bit_mask = 0u;\n\t\tstd::uint32_t m_descriptor_table_bit_mask = 0u;\n\t};\n\n\tstruct PipelineState;\n\tstruct StateObject;\n\tstruct Shader\n\t{\n\t\tIDxcBlob* m_native = nullptr;\n\t\tstd::string m_path = \"\";\n\t\tstd::string m_entry = \"\";\n\t\tShaderType m_type;\n\t\tstd::vector<std::pair<std::wstring,std::wstring>> m_defines;\n\t};\n\n\tstruct PipelineState\n\t{\n\t\tID3D12PipelineState* m_native = nullptr;\n\t\tRootSignature* m_root_signature = nullptr;\n\t\tShader* m_vertex_shader = nullptr;\n\t\tShader* m_pixel_shader = nullptr;\n\t\tShader* m_compute_shader = nullptr;\n\t\tDevice* m_device = nullptr; // only used for refinalization.\n\t\tdesc::PipelineStateDesc m_desc;\n\t};\n\n\tstruct Viewport\n\t{\n\t\tD3D12_VIEWPORT m_viewport;\n\t\tD3D12_RECT m_scissor_rect;\n\t};\n\n\tstruct DescriptorHeap\n\t{\n\t\tdesc::DescriptorHeapDesc m_create_info;\n\t\tstd::vector<ID3D12DescriptorHeap*> m_native;\n\t\tstd::uint32_t m_increment_size = 0u;\n\t};\n\n\tstruct DescHeapCPUHandle\n\t{\n\t\tD3D12_CPU_DESCRIPTOR_HANDLE m_native;\n\t};\n\n\tstruct DescHeapGPUHandle\n\t{\n\t\tD3D12_GPU_DESCRIPTOR_HANDLE m_native;\n\t};\n\n\tstruct StagingBuffer\n\t{\n\t\tID3D12Resource* m_buffer = nullptr;\n\t\tID3D12Resource* m_staging = nullptr;\n\t\tstd::uint64_t m_size = 0u;\n\t\tstd::uint64_t m_stride_in_bytes = 0u;\n\t\tResourceState m_target_resource_state;\n\t\tD3D12_GPU_VIRTUAL_ADDRESS m_gpu_address;\n\t\tstd::uint8_t* m_cpu_address = nullptr;\n\t\tbool m_is_staged = false;\n\t};\n\n\tstruct ReadbackBufferResource : ReadbackBuffer\n\t{\n\t\tID3D12Resource* m_resource = nullptr;\n\t};\n\n\tstruct HeapResource;\n\tnamespace detail\n\t{\n\n\t\t/*! Fallback detail structure */\n\t\ttemplate<HeapOptimization O>\n\t\tstruct Heap\n\t\t{\n\t\t\t\n\t\t};\n\n\t\t/*! Small Buffer Optimization */\n\t\t// TODO: Which one should we version? The heap or the resource? test perf!\n\t\ttemplate<>\n\t\tstruct Heap<HeapOptimization::SMALL_BUFFERS>\n\t\t{\n\t\t\tstd::vector<HeapResource*> m_resources;\n\t\t\tstd::uint8_t* m_cpu_address = nullptr;\n\t\t\tID3D12Resource* m_native = nullptr;\n\t\t};\n\n\t\t/*! Big Buffer Optimization */\n\t\t// TODO: Which one should we version? The heap or the resource? test perf!\n\t\ttemplate<>\n\t\tstruct Heap<HeapOptimization::BIG_BUFFERS>\n\t\t{\n\t\t\tstd::vector<std::pair<HeapResource*, std::vector<ID3D12Resource*>>> m_resources;\n\t\t\tID3D12Heap* m_native = nullptr;\n\t\t};\n\n\t\ttemplate<>\n\t\tstruct Heap<HeapOptimization::SMALL_STATIC_BUFFERS>\n\t\t{\n\t\t\tstd::vector<HeapResource*> m_resources;\n\t\t\tD3D12_GPU_VIRTUAL_ADDRESS m_gpu_address;\n\t\t\tID3D12Resource* m_native = nullptr;\n\t\t\tID3D12Resource* m_staging_buffer = nullptr;\n\t\t\tstd::uint8_t* m_cpu_address = nullptr;\n\t\t};\n\n\t\ttemplate<>\n\t\tstruct Heap<HeapOptimization::BIG_STATIC_BUFFERS> \n\t\t{\n\t\t\tstd::vector<std::pair<HeapResource*, std::vector<ID3D12Resource*>>> m_resources;\n\t\t\tID3D12Heap* m_native = nullptr;\n\t\t\tID3D12Resource* m_staging_buffer = nullptr;\n\t\t\tstd::uint8_t* m_cpu_address = nullptr;\n\t\t};\n\n\t} /* detail */\n\n\ttemplate<HeapOptimization O>\n\tstruct Heap : detail::Heap<O>\n\t{\n\t\tbool m_mapped;\n\t\tstd::uint32_t m_versioning_count = 0u;\n\t\tstd::uint64_t m_current_offset = 0u;\n\t\tstd::uint64_t m_heap_size = 0u;\n\t\tstd::uint64_t m_alignment = 0u;\n\t\tstd::vector<std::uint64_t> m_bitmap;\n\t};\n\n\tstruct HeapResource\n\t{\n\t\tunion\n\t\t{\n\t\t\tHeap<HeapOptimization::SMALL_BUFFERS>* m_heap_sbo;\n\t\t\tHeap<HeapOptimization::SMALL_STATIC_BUFFERS>* m_heap_ssbo;\n\t\t\tHeap<HeapOptimization::BIG_BUFFERS>* m_heap_bbo;\n\t\t\tHeap<HeapOptimization::BIG_STATIC_BUFFERS>* m_heap_bsbo;\n\t\t};\n\n\t\tstd::vector<D3D12_GPU_VIRTUAL_ADDRESS> m_gpu_addresses;\n\t\tstd::optional<std::vector<std::uint8_t*>> m_cpu_addresses;\n\t\tstd::uint64_t m_unaligned_size = 0u;\n\t\tstd::uint64_t m_begin_offset = 0u;\n\t\tstd::size_t m_heap_vector_location = 0;\n\t\tstd::size_t m_stride = 0;\n\t\tbool m_used_as_uav = false;\n\t\tHeapOptimization m_resource_heap_optimization;\n\t\tstd::vector<ResourceState> m_states;\n\t};\n\n\tstruct IndirectCommandBuffer\n\t{\n\t\tstd::size_t m_num_commands = 0u;\n\t\tstd::size_t m_num_max_commands = 0u;\n\t\tstd::size_t m_command_size = 0u;\n\t\tstd::vector<ID3D12Resource*> m_native;\n\t\tstd::vector<ID3D12Resource*> m_native_upload;\n\t};\n\n\tstruct StateObject\n\t{\n\t\tID3D12StateObject* m_native = nullptr;\n\t\tRootSignature* m_global_root_signature = nullptr;\n\t\tID3D12StateObjectProperties* m_properties = nullptr;\n\n\t\tID3D12RaytracingFallbackStateObject* m_fallback_native = nullptr;\n\n\t\tdesc::StateObjectDesc m_desc;\n\t\tDevice* m_device = nullptr; // Only for refinalization.\n\t};\n\n\tstruct AccelerationStructure\n\t{\n\t\tID3D12Resource* m_scratch = nullptr;\t\t\t\t\t\t\t\t\t\t\t\t\t\t // Scratch memory for AS builder\n\t\tstd::array<ID3D12Resource*, d3d12::settings::num_back_buffers> m_natives = { nullptr };        // Where the AS is\n\t\tstd::array<ID3D12Resource*, d3d12::settings::num_back_buffers> m_instance_descs = { nullptr }; // Hold the matrices of the instances\n\t\tWRAPPED_GPU_POINTER m_fallback_tlas_ptr;\n\t\tD3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO m_prebuild_info;\n\t};\n\n\tnamespace desc\n\t{\n\t\tstruct BlasDesc\n\t\t{\n\t\t\td3d12::AccelerationStructure m_as;\n\t\t\tstd::uint64_t m_material = 0u;\n\t\t\tDirectX::XMMATRIX m_transform;\n\t\t};\n\t} /* desc */\n\n\tstruct ShaderRecord\n\t{\n\t\tstd::pair<void*, std::uint64_t> m_shader_identifier;\n\t\tstd::pair<void*, std::uint64_t> m_local_root_args;\n\t};\n\n\tstruct ShaderTable\n\t{\n\t\tstd::uint8_t* m_mapped_shader_records = nullptr;\n\t\tstd::uint64_t m_shader_record_size = 0u;\n\t\tstd::vector<ShaderRecord> m_shader_records;\n\t\tID3D12Resource* m_resource = nullptr;\n\t};\n\n} /* wr::d3d12 */\n"
  },
  {
    "path": "src/d3d12/d3d12_structured_buffer.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tvoid CreateSRVFromStructuredBuffer(HeapResource* structured_buffer, DescHeapCPUHandle& handle, unsigned int id)\n\t{\n\t\tauto& resources = structured_buffer->m_heap_bsbo->m_resources[structured_buffer->m_heap_vector_location];\n\t\tauto& resource = resources.second[id];\n\n\t\tdecltype(Device::m_native) n_device;\n\t\tresource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tunsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);\n\n\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\n\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\n\t\tsrv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;\n\t\tsrv_desc.Buffer.FirstElement = 0;\n\t\tsrv_desc.Buffer.NumElements = static_cast<UINT>(structured_buffer->m_unaligned_size / structured_buffer->m_stride);\n\t\tsrv_desc.Buffer.StructureByteStride = static_cast<UINT>(structured_buffer->m_stride);\n\t\tsrv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE;\n\n\t\tn_device->CreateShaderResourceView(resource, &srv_desc, handle.m_native);\n\t\tOffset(handle, 1, increment_size);\n\t}\n\n}"
  },
  {
    "path": "src/d3d12/d3d12_structured_buffer_pool.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_structured_buffer_pool.hpp\"\n#include \"d3d12_functions.hpp\"\n#include \"d3d12_renderer.hpp\"\n#include \"d3d12_defines.hpp\"\n\nnamespace wr\n{\n\tD3D12StructuredBufferPool::D3D12StructuredBufferPool(D3D12RenderSystem & render_system, std::size_t size_in_bytes) :\n\t\tStructuredBufferPool(SizeAlignTwoPower(size_in_bytes, 65536)),\n\t\tm_render_system(render_system)\n\t{\n\t\tm_heap = d3d12::CreateHeap_BSBO(m_render_system.m_device, SizeAlignTwoPower(size_in_bytes, 65536), ResourceType::BUFFER, d3d12::settings::num_back_buffers);\n\t\tSetName(m_heap, L\"Structured buffer heap\");\n\t\tm_buffer_update_queues.resize(d3d12::settings::num_back_buffers);\n\t}\n\n\tD3D12StructuredBufferPool::~D3D12StructuredBufferPool()\n\t{\n\t\tfor (D3D12StructuredBufferHandle* handle : m_handles) \n\t\t{\n\t\t\tdelete handle;\n\t\t}\n\n\t\td3d12::Destroy(m_heap);\n\t}\n\n\tvoid D3D12StructuredBufferPool::UpdateBuffers(d3d12::CommandList * cmd_list, std::size_t frame_idx)\n\t{\n\t\twhile (!m_buffer_update_queues[frame_idx].empty()) \n\t\t{\n\t\t\tBufferUpdateInfo info = m_buffer_update_queues[frame_idx].front();\n\n\t\t\tif (info.m_data.size() > 0)\n\t\t\t{\n\t\t\t\td3d12::UpdateStructuredBuffer(info.m_buffer_handle->m_native,\n\t\t\t\t\tstatic_cast<std::uint32_t>(frame_idx),\n\t\t\t\t\tinfo.m_data.data(),\n\t\t\t\t\tinfo.m_size,\n\t\t\t\t\tinfo.m_offset,\n\t\t\t\t\tinfo.m_buffer_handle->m_native->m_stride,\n\t\t\t\t\tcmd_list);\n\t\t\t}\n\t\t\telse if (info.m_buffer_handle->m_native->m_states[frame_idx] != info.m_new_state)\n\t\t\t{\n\t\t\t\tID3D12Resource * resource = info.m_buffer_handle->m_native->m_heap_bsbo->m_resources[info.m_buffer_handle->m_native->m_heap_vector_location].second[frame_idx];\n\n\t\t\t\tif (info.m_buffer_handle->m_native->m_states[frame_idx] != info.m_new_state)\n\t\t\t\t{\n\t\t\t\t\tCD3DX12_RESOURCE_BARRIER transition_barrier = CD3DX12_RESOURCE_BARRIER::Transition(\n\t\t\t\t\t\tresource,\n\t\t\t\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(info.m_buffer_handle->m_native->m_states[frame_idx]),\n\t\t\t\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(info.m_new_state));\n\n\t\t\t\t\tcmd_list->m_native->ResourceBarrier(1,\n\t\t\t\t\t\t&transition_barrier);\n\t\t\t\t\tinfo.m_buffer_handle->m_native->m_states[frame_idx] = info.m_new_state;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tLOG(\"Trying to transition a resource to a state is already in. This should be avoided.\");\n\t\t\t\t}\n\t\t\t}\n\t\t\tm_buffer_update_queues[frame_idx].pop();\n\t\t}\n\t}\n\n\tvoid D3D12StructuredBufferPool::SetBufferState(StructuredBufferHandle * handle, ResourceState state)\n\t{\n\t\tBufferUpdateInfo info = {};\n\t\tinfo.m_buffer_handle = static_cast<D3D12StructuredBufferHandle*>(handle);\n\t\tinfo.m_new_state = state;\n\n\t\tfor (int i = 0; i < m_buffer_update_queues.size(); ++i) {\n\t\t\tm_buffer_update_queues[i].push(info);\n\t\t}\n\t}\n\n\tvoid D3D12StructuredBufferPool::Evict()\n\t{\n\t\td3d12::Evict(m_heap);\n\t}\n\n\tvoid D3D12StructuredBufferPool::MakeResident()\n\t{\n\t\td3d12::MakeResident(m_heap);\n\t}\n\n\tStructuredBufferHandle * D3D12StructuredBufferPool::CreateBuffer(std::size_t size, std::size_t stride, bool used_as_uav)\n\t{\n\t\tD3D12StructuredBufferHandle* handle = new D3D12StructuredBufferHandle();\n\t\thandle->m_pool = this;\n\t\thandle->m_native = d3d12::AllocStructuredBuffer(m_heap, size, stride, used_as_uav);\n\n\t\tif (handle->m_native == nullptr) \n\t\t{\n\t\t\tdelete handle;\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tm_handles.push_back(handle);\n\n\t\treturn handle;\n\t}\n\n\tvoid D3D12StructuredBufferPool::DestroyBuffer(StructuredBufferHandle * handle)\n\t{\n\t\tstd::vector<D3D12StructuredBufferHandle*>::iterator it;\n\t\tfor (it = m_handles.begin(); it != m_handles.end(); ++it) \n\t\t{\n\t\t\tif ((*it) == handle)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (it == m_handles.end())\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tm_handles.erase(it);\n\n\t\td3d12::DeallocBuffer(m_heap, static_cast<D3D12StructuredBufferHandle*>(handle)->m_native);\n\n\t\tdelete static_cast<D3D12StructuredBufferHandle*>(handle);\n\t}\n\n\tvoid D3D12StructuredBufferPool::UpdateBuffer(StructuredBufferHandle * handle, void * data, std::size_t size, std::size_t offset)\n\t{\n\t\tif (handle->m_pool == this) \n\t\t{\n\t\t\tBufferUpdateInfo info = {};\n\t\t\tinfo.m_buffer_handle = static_cast<D3D12StructuredBufferHandle*>(handle);\n\t\t\tinfo.m_data.resize(size);\n\t\t\tmemcpy(info.m_data.data(), data, size);\n\t\t\tinfo.m_size = size;\n\t\t\tinfo.m_offset = offset;\n\n\t\t\tfor (int i = 0; i < d3d12::settings::num_back_buffers; ++i) \n\t\t\t{\n\t\t\t\tm_buffer_update_queues[i].push(info);\n\t\t\t}\n\t\t}\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/d3d12/d3d12_structured_buffer_pool.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../structured_buffer_pool.hpp\"\n#include \"d3d12_structs.hpp\"\n\n#include <queue>\n\n\nnamespace wr\n{\n\tstruct D3D12StructuredBufferHandle : StructuredBufferHandle\n\t{\n\t\td3d12::HeapResource* m_native;\n\t};\n\n\tstruct BufferUpdateInfo\n\t{\n\t\tD3D12StructuredBufferHandle* m_buffer_handle;\n\t\tstd::vector<std::uint8_t> m_data;\n\t\tstd::size_t m_offset;\n\t\tstd::size_t m_size;\n\t\tResourceState m_new_state;\n\t};\n\n\tclass D3D12RenderSystem;\n\n\tclass D3D12StructuredBufferPool : public StructuredBufferPool\n\t{\n\tpublic:\n\t\texplicit D3D12StructuredBufferPool(D3D12RenderSystem& render_system,\n\t\t\tstd::size_t size_in_bytes);\n\n\t\t~D3D12StructuredBufferPool() final;\n\n\t\tvoid UpdateBuffers(d3d12::CommandList* cmd_list, std::size_t frame_idx);\n\n\t\tvoid SetBufferState(StructuredBufferHandle* handle, ResourceState state);\n\n\t\tvoid Evict() final;\n\t\tvoid MakeResident() final;\n\n\tprotected:\n\t\t[[nodiscard]] StructuredBufferHandle* CreateBuffer(std::size_t size, std::size_t stride, bool used_as_uav) final;\n\t\tvoid DestroyBuffer(StructuredBufferHandle* handle) final;\n\t\tvoid UpdateBuffer(StructuredBufferHandle* handle, void* data, std::size_t size, std::size_t offset) final;\n\n\t\tD3D12RenderSystem& m_render_system;\n\n\t\td3d12::Heap<HeapOptimization::BIG_STATIC_BUFFERS>* m_heap;\n\n\t\tstd::vector<D3D12StructuredBufferHandle*> m_handles;\n\t\tstd::vector<std::queue<BufferUpdateInfo>> m_buffer_update_queues;\n\t};\n\n\n} /* wr */"
  },
  {
    "path": "src/d3d12/d3d12_texture_resources.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include \"../structs.hpp\"\n\n#include \"d3d12_enums.hpp\"\n#include \"d3d12_settings.hpp\"\n#include \"d3dx12.hpp\"\n\n#include \"d3d12_descriptors_allocations.hpp\"\n\n#include <DirectXTex.h>\n\nnamespace wr::d3d12\n{\n\tstruct TextureResource : Texture\n\t{\n\t\tstd::size_t m_width = 0;\n\t\tstd::size_t m_height = 0;\n\t\tstd::size_t m_depth = 0;\n\t\tstd::size_t m_array_size = 0;\n\t\tstd::size_t m_mip_levels = 0;\n\n\t\tFormat m_format = wr::Format::UNKNOWN;\n\n\t\tID3D12Resource* m_resource = nullptr;\n\t\tID3D12Resource* m_intermediate = nullptr;\n\t\tstd::vector<ResourceState> m_subresource_states;\n\t\tDescriptorAllocation m_srv_allocation;\n\t\tDescriptorAllocation m_uav_allocation;\n\n\t\tstd::vector<D3D12_SUBRESOURCE_DATA> m_subresources;\n\n\t\t//This allocation can be either 1 or 6 continous descriptors based on m_is_cubemap\n\t\tstd::optional<DescriptorAllocation> m_rtv_allocation = std::nullopt;\n\n\t\tbool m_is_staged = false;\n\t\tbool m_need_mips = false;\n\t\tbool m_is_cubemap = false;\n\t\tbool m_allow_render_dest = false;\n\t};\n}"
  },
  {
    "path": "src/d3d12/d3d12_textures.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\n#include \"../util/log.hpp\"\n#include \"d3d12_defines.hpp\"\n#include \"d3dx12.hpp\"\n#include \"d3d12_texture_resources.hpp\"\n#include \"d3d12_rt_descriptor_heap.hpp\"\n\nnamespace wr::d3d12\n{\n\tTextureResource* CreateTexture(Device* device, desc::TextureDesc* description, bool allow_uav)\n\t{\n\t\tD3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE;\n\n\t\tbool is_uav_compatible_format = (d3d12::CheckUAVCompatibility(description->m_texture_format) \n\t\t\t\t\t\t\t\t\t     || d3d12::CheckOptionalUAVFormat(description->m_texture_format));\n\n\t\tif (allow_uav && is_uav_compatible_format)\n\t\t{ \n\t\t\tflags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; \n\t\t}\n\n\t\tif (description->m_initial_state == ResourceState::RENDER_TARGET) { flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; }\n\n\t\tD3D12_RESOURCE_DESC desc = {};\n\t\tdesc.Width = description->m_width;\n\t\tdesc.Height = description->m_height;\n\t\tdesc.MipLevels = static_cast<std::uint16_t>(description->m_mip_levels);\n\t\tdesc.DepthOrArraySize = static_cast<UINT16>((description->m_depth > 1) ? description->m_depth : description->m_array_size);\n\t\tdesc.Format = static_cast<DXGI_FORMAT>(description->m_texture_format);\n\t\tdesc.SampleDesc.Count = 1;\n\t\tdesc.SampleDesc.Quality = 0;\n\t\tdesc.Flags = flags;\n\t\tdesc.Dimension = (description->m_depth > 1) ? D3D12_RESOURCE_DIMENSION_TEXTURE3D : D3D12_RESOURCE_DIMENSION_TEXTURE2D;\n\n\t\tCD3DX12_HEAP_PROPERTIES defaultHeapProperties(D3D12_HEAP_TYPE_DEFAULT);\n\n\t\tauto native_device = device->m_native;\n\n\t\tID3D12Resource* resource;\n\n\t\tHRESULT res = native_device->CreateCommittedResource(&defaultHeapProperties,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&desc,\n\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(description->m_initial_state),\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&resource));\n\n\t\tif (FAILED(res))\n\t\t{\n\t\t\tLOGC(\"Error: Couldn't create texture\");\n\t\t}\n\n\t\t// Create intermediate resource on upload heap for staging\n\t\tuint64_t textureUploadBufferSize;\n\t\tdevice->m_native->GetCopyableFootprints(&desc, 0, desc.MipLevels * desc.DepthOrArraySize, 0, nullptr, nullptr, nullptr, &textureUploadBufferSize);\n\n\t\tID3D12Resource* intermediate;\n\n\t\tCD3DX12_HEAP_PROPERTIES uploadHeapProperties(D3D12_HEAP_TYPE_UPLOAD);\n\t\tCD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(textureUploadBufferSize);\n\n\t\tdevice->m_native->CreateCommittedResource(\n\t\t\t&uploadHeapProperties,\n\t\t\tD3D12_HEAP_FLAG_NONE,\n\t\t\t&buffer_desc,\n\t\t\tD3D12_RESOURCE_STATE_GENERIC_READ,\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&intermediate));\n\n\n\t\tTextureResource* texture = new TextureResource();\n\n\t\ttexture->m_width = description->m_width;\n\t\ttexture->m_height = description->m_height;\n\t\ttexture->m_depth = description->m_depth;\n\t\ttexture->m_array_size = description->m_array_size;\n\t\ttexture->m_mip_levels = description->m_mip_levels;\n\t\ttexture->m_format = description->m_texture_format;\n\t\ttexture->m_resource = resource;\n\t\ttexture->m_intermediate = intermediate;\n\t\ttexture->m_need_mips = (texture->m_mip_levels > 1);\n\t\ttexture->m_is_cubemap = description->m_is_cubemap;\n\t\ttexture->m_is_staged = false;\n\n\t\tfor (uint32_t i = 0; i < description->m_mip_levels; ++i)\n\t\t{\n\t\t\ttexture->m_subresource_states.push_back(description->m_initial_state);\n\t\t}\n\n\t\treturn texture;\n\t}\n\n\tTextureResource* CreatePlacedTexture(Device* device, desc::TextureDesc* description, bool allow_uav, Heap<HeapOptimization::BIG_STATIC_BUFFERS>* heap)\n\t{\n\t\tD3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE;\n\n\t\tbool is_uav_compatible_format = (d3d12::CheckUAVCompatibility(description->m_texture_format)\n\t\t\t|| d3d12::CheckOptionalUAVFormat(description->m_texture_format));\n\n\t\tif (allow_uav && is_uav_compatible_format)\n\t\t{\n\t\t\tflags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;\n\t\t}\n\n\t\tif (description->m_initial_state == ResourceState::RENDER_TARGET) { flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; }\n\n\t\tD3D12_RESOURCE_DESC desc = {};\n\t\tdesc.Width = description->m_width;\n\t\tdesc.Height = description->m_height;\n\t\tdesc.MipLevels = static_cast<std::uint16_t>(description->m_mip_levels);\n\t\tdesc.DepthOrArraySize = static_cast<std::uint16_t>((description->m_depth > 1) ? description->m_depth : description->m_array_size);\n\t\tdesc.Format = static_cast<DXGI_FORMAT>(description->m_texture_format);\n\t\tdesc.SampleDesc.Count = 1;\n\t\tdesc.SampleDesc.Quality = 0;\n\t\tdesc.Flags = flags;\n\t\tdesc.Dimension = (description->m_depth > 1) ? D3D12_RESOURCE_DIMENSION_TEXTURE3D : D3D12_RESOURCE_DIMENSION_TEXTURE2D;\n\n\n\t\tauto native_device = device->m_native;\n\n\t\tID3D12Resource* resource;\n\n\t\tHRESULT res = native_device->CreatePlacedResource(\n\t\t\theap->m_native, \n\t\t\t0,\n\t\t\t&desc,\n\t\t\tstatic_cast<D3D12_RESOURCE_STATES>(description->m_initial_state),\n\t\t\tnullptr,\n\t\t\tIID_PPV_ARGS(&resource));\n\n\t\tif (FAILED(res))\n\t\t{\n\t\t\tLOGC(\"Error: Couldn't create texture\");\n\t\t}\n\n\t\tTextureResource* texture = new TextureResource();\n\n\t\ttexture->m_width = description->m_width;\n\t\ttexture->m_height = description->m_height;\n\t\ttexture->m_depth = description->m_depth;\n\t\ttexture->m_array_size = description->m_array_size;\n\t\ttexture->m_mip_levels = description->m_mip_levels;\n\t\ttexture->m_format = description->m_texture_format;\n\t\ttexture->m_resource = resource;\n\t\ttexture->m_intermediate = nullptr;\n\t\ttexture->m_need_mips = (texture->m_mip_levels > 1);\n\t\ttexture->m_is_cubemap = description->m_is_cubemap;\n\t\ttexture->m_is_staged = false;\n\n\t\tfor (uint32_t i = 0; i < description->m_mip_levels; ++i)\n\t\t{\n\t\t\ttexture->m_subresource_states.push_back(description->m_initial_state);\n\t\t}\n\n\t\treturn texture;\n\t}\n\n\tvoid SetName(TextureResource* tex, std::wstring name)\n\t{\n\t\ttex->m_resource->SetName(name.c_str());\n\t}\n\n\tvoid CreateSRVFromTexture(TextureResource* tex)\n\t{\n\t\td3d12::DescHeapCPUHandle handle = tex->m_srv_allocation.GetDescriptorHandle();\n\n\t\tCreateSRVFromTexture(tex, handle);\n\t}\n\n\tvoid CreateSRVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\n\t\ttex->m_resource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\n\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\n\t\tsrv_desc.Format = (DXGI_FORMAT)tex->m_format;\n\n\t\t//Calculate dimension\n\t\tD3D12_SRV_DIMENSION dimension;\n\n\t\tif (tex->m_is_cubemap)\n\t\t{\n\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURECUBE;\n\t\t\tsrv_desc.TextureCube.MipLevels = static_cast<std::uint32_t>(tex->m_mip_levels);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (tex->m_depth > 1)\n\t\t\t{\n\t\t\t\t//Then it's a 3D texture\n\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE3D;\n\t\t\t\tsrv_desc.Texture3D.MipLevels = static_cast<std::uint32_t>(tex->m_mip_levels);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (tex->m_height > 1)\n\t\t\t\t{\n\t\t\t\t\tif (tex->m_array_size > 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;\n\t\t\t\t\t\tsrv_desc.Texture2DArray.MipLevels = static_cast<std::uint32_t>(tex->m_mip_levels);\n\t\t\t\t\t\tsrv_desc.Texture2DArray.ArraySize = static_cast<std::uint32_t>(tex->m_array_size);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE2D;\n\t\t\t\t\t\tsrv_desc.Texture2D.MipLevels = static_cast<std::uint32_t>(tex->m_mip_levels);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\t//Then it's a 1D texture\n\t\t\t\t\tif (tex->m_array_size > 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE1DARRAY;\n\n\t\t\t\t\t\tsrv_desc.Texture1DArray.MipLevels = static_cast<std::uint32_t>(tex->m_mip_levels);\n\t\t\t\t\t\tsrv_desc.Texture1DArray.ArraySize = static_cast<std::uint32_t>(tex->m_array_size);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE1D;\n\n\t\t\t\t\t\tsrv_desc.Texture1D.MipLevels = static_cast<std::uint32_t>(tex->m_mip_levels);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\n\t\tsrv_desc.ViewDimension = dimension;\n\n\t\tn_device->CreateShaderResourceView(tex->m_resource, &srv_desc, handle.m_native);\n\t}\n\n\tvoid CreateSRVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_levels, unsigned int most_detailed_mip)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\n\t\ttex->m_resource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\n\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\n\t\tsrv_desc.Format = (DXGI_FORMAT)tex->m_format;\n\n\t\t//Calculate dimension\n\t\tD3D12_SRV_DIMENSION dimension;\n\n\t\tif (tex->m_is_cubemap)\n\t\t{\n\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURECUBE;\n\t\t\tsrv_desc.TextureCube.MipLevels = mip_levels;\n\t\t\tsrv_desc.TextureCube.MostDetailedMip = most_detailed_mip;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (tex->m_depth > 1)\n\t\t\t{\n\t\t\t\t//Then it's a 3D texture\n\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE3D;\n\t\t\t\tsrv_desc.Texture3D.MipLevels = mip_levels;\n\t\t\t\tsrv_desc.Texture3D.MostDetailedMip = most_detailed_mip;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (tex->m_height > 1)\n\t\t\t\t{\n\t\t\t\t\tif (tex->m_array_size > 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;\n\t\t\t\t\t\tsrv_desc.Texture2DArray.MipLevels = mip_levels;\n\t\t\t\t\t\tsrv_desc.Texture2DArray.MostDetailedMip = most_detailed_mip;\n\t\t\t\t\t\tsrv_desc.Texture2DArray.ArraySize = static_cast<std::uint32_t>(tex->m_array_size);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE2D;\n\t\t\t\t\t\tsrv_desc.Texture2D.MipLevels = mip_levels;\n\t\t\t\t\t\tsrv_desc.Texture2D.MostDetailedMip = most_detailed_mip;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\t//Then it's a 1D texture\n\t\t\t\t\tif (tex->m_array_size > 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE1DARRAY;\n\n\t\t\t\t\t\tsrv_desc.Texture1DArray.MipLevels = mip_levels;\n\t\t\t\t\t\tsrv_desc.Texture1DArray.MostDetailedMip = most_detailed_mip;\n\t\t\t\t\t\tsrv_desc.Texture1DArray.ArraySize = static_cast<std::uint32_t>(tex->m_array_size);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE1D;\n\n\t\t\t\t\t\tsrv_desc.Texture1D.MipLevels = mip_levels;\n\t\t\t\t\t\tsrv_desc.Texture1D.MostDetailedMip = most_detailed_mip;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\n\t\tsrv_desc.ViewDimension = dimension;\n\n\t\tn_device->CreateShaderResourceView(tex->m_resource, &srv_desc, handle.m_native);\n\t}\n\n\tvoid CreateSRVFromCubemapFace(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_levels, unsigned int most_detailed_mip, unsigned int face_idx)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\n\t\ttex->m_resource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tD3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};\n\t\tsrv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\n\t\tsrv_desc.Format = (DXGI_FORMAT)tex->m_format;\n\n\t\t//Calculate dimension\n\t\tD3D12_SRV_DIMENSION dimension;\n\n\t\tif (tex->m_is_cubemap)\n\t\t{\n\t\t\tdimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;\n\t\t\tsrv_desc.Texture2DArray.MipLevels = mip_levels;\n\t\t\tsrv_desc.Texture2DArray.MostDetailedMip = most_detailed_mip;\n\t\t\tsrv_desc.Texture2DArray.ArraySize = 1;\n\t\t\tsrv_desc.Texture2DArray.FirstArraySlice = face_idx;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tLOGC(\"[ERROR]: Texture is not a cubemap\");\n\t\t\treturn;\n\t\t}\n\n\t\tsrv_desc.ViewDimension = dimension;\n\n\t\tn_device->CreateShaderResourceView(tex->m_resource, &srv_desc, handle.m_native);\n\t}\n\n\tvoid CreateUAVFromTexture(TextureResource* tex, unsigned int mip_slice)\n\t{\n\t\td3d12::DescHeapCPUHandle handle = tex->m_uav_allocation.GetDescriptorHandle();\n\n\t\tCreateUAVFromTexture(tex, handle, mip_slice);\n\t}\n\n\tvoid CreateUAVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_slice)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\n\t\ttex->m_resource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tD3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {};\n\t\t\n\t\tuav_desc.Format = (DXGI_FORMAT)tex->m_format;\n\n\t\t//Calculate dimension\n\t\tD3D12_UAV_DIMENSION dimension;\n\n\t\tif (tex->m_is_cubemap)\n\t\t{\n\t\t\tdimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY;\n\t\t\tuav_desc.Texture2DArray.MipSlice = mip_slice;\n\t\t\tuav_desc.Texture2DArray.ArraySize = static_cast<std::uint32_t>(tex->m_array_size);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (tex->m_depth > 1)\n\t\t\t{\n\t\t\t\t//Then it's a 3D texture\n\t\t\t\tdimension = D3D12_UAV_DIMENSION_TEXTURE3D;\n\n\t\t\t\tuav_desc.Texture3D.MipSlice = mip_slice;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (tex->m_height > 1)\n\t\t\t\t{\n\t\t\t\t\tif (tex->m_array_size > 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY;\n\n\t\t\t\t\t\tuav_desc.Texture2DArray.MipSlice = mip_slice;\n\t\t\t\t\t\tuav_desc.Texture2DArray.ArraySize = static_cast<std::uint32_t>(tex->m_array_size);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_UAV_DIMENSION_TEXTURE2D;\n\n\t\t\t\t\t\tuav_desc.Texture2D.MipSlice = mip_slice;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\t//Then it's a 1D texture\n\t\t\t\t\tif (tex->m_array_size > 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_UAV_DIMENSION_TEXTURE1DARRAY;\n\n\t\t\t\t\t\tuav_desc.Texture1DArray.MipSlice = mip_slice;\n\t\t\t\t\t\tuav_desc.Texture1DArray.ArraySize = static_cast<std::uint32_t>(tex->m_array_size);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tdimension = D3D12_UAV_DIMENSION_TEXTURE1D;\n\n\t\t\t\t\t\tuav_desc.Texture1D.MipSlice = mip_slice;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\n\t\tuav_desc.ViewDimension = dimension;\n\n\t\tn_device->CreateUnorderedAccessView(tex->m_resource, nullptr, &uav_desc, handle.m_native);\n\t}\n\n\tvoid CreateUAVFromCubemapFace(TextureResource * tex, DescHeapCPUHandle & handle, unsigned int mip_slice, unsigned int face_idx)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\n\t\ttex->m_resource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tD3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {};\n\n\t\tuav_desc.Format = (DXGI_FORMAT)tex->m_format;\n\n\t\tif (tex->m_is_cubemap)\n\t\t{\n\t\t\tuav_desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY;\n\t\t\tuav_desc.Texture2DArray.MipSlice = mip_slice;\n\t\t\tuav_desc.Texture2DArray.FirstArraySlice = face_idx;\n\t\t\tuav_desc.Texture2DArray.ArraySize = 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tLOGC(\"[ERROR]: Texture is not a cubemap\");\n\t\t}\n\n\t\tn_device->CreateUnorderedAccessView(tex->m_resource, nullptr, &uav_desc, handle.m_native);\n\t}\n\n\tvoid CreateRTVFromTexture2D(TextureResource* tex)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\n\t\ttex->m_resource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\n\t\tif (tex->m_rtv_allocation == std::nullopt)\n\t\t{\n\t\t\tLOGC(\"This texture was not created with the intent of being used as a RTV.\");\n\t\t}\n\n\t\tD3D12_RENDER_TARGET_VIEW_DESC desc;\n\t\tdesc.Format = static_cast<DXGI_FORMAT>(tex->m_format);\n\t\tdesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;\n\t\tdesc.Texture2D.MipSlice = 0;\n\n\t\tDescHeapCPUHandle handle = tex->m_rtv_allocation->GetDescriptorHandle();\n\n\t\tn_device->CreateRenderTargetView(tex->m_resource, &desc, handle.m_native);\n\t}\n\n\tvoid CreateRTVFromCubemap(TextureResource* tex)\n\t{\n\t\tdecltype(Device::m_native) n_device;\n\n\t\ttex->m_resource->GetDevice(IID_PPV_ARGS(&n_device));\n\n\t\tif (!tex->m_is_cubemap)\n\t\t{\n\t\t\tLOGC(\"This texture is not a cubemap.\");\n\t\t}\n\n\t\tif (tex->m_rtv_allocation == std::nullopt)\n\t\t{\n\t\t\tLOGC(\"This texture was not created with the intent of being used as a RTV.\");\n\t\t}\n\n\t\tfor (uint8_t i = 0; i < 6; ++i)\n\t\t{\n\t\t\tD3D12_RENDER_TARGET_VIEW_DESC desc;\n\t\t\tdesc.Format = static_cast<DXGI_FORMAT>(tex->m_format);\n\t\t\tdesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DARRAY;\n\t\t\tdesc.Texture2DArray.MipSlice = 0;\n\t\t\tdesc.Texture2DArray.PlaneSlice = 0;\n\n\t\t\t//Render target to ith element\n\t\t\tdesc.Texture2DArray.FirstArraySlice = i;\n\n\t\t\t//Only view one element of the array\n\t\t\tdesc.Texture2DArray.ArraySize = 1;\n\n\t\t\tDescHeapCPUHandle handle = tex->m_rtv_allocation->GetDescriptorHandle(i);\n\n\t\t\tn_device->CreateRenderTargetView(tex->m_resource, &desc, handle.m_native);\n\t\t}\n\t}\n\n\tvoid SetShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex)\n\t{\n\t\td3d12::DescHeapCPUHandle handle = tex->m_srv_allocation.GetDescriptorHandle();\n\n\t\tcmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(rootParameterIndex, descriptorOffset, 1, handle);\n\t}\n\n\tvoid SetShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count)\n\t{\n\t\tcmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(root_parameter_index, descriptor_offset, descriptor_count, handle);\n\t}\n\n\tvoid SetShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex)\n\t{\n\t\td3d12::DescHeapCPUHandle handle = tex->m_uav_allocation.GetDescriptorHandle();\n\n\t\tcmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(rootParameterIndex, descriptorOffset, 1, handle);\n\t}\n\n\tvoid SetShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count)\n\t{\n\t\tcmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(root_parameter_index, descriptor_offset, descriptor_count, handle);\n\t}\n\n\tvoid SetRTShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex)\n\t{\n\t\td3d12::DescHeapCPUHandle handle = tex->m_srv_allocation.GetDescriptorHandle();\n\n\t\tcmd_list->m_rt_descriptor_heap->StageDescriptors(rootParameterIndex, descriptorOffset, 1, handle);\n\t}\n\n\tvoid SetRTShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count)\n\t{\n\t\tcmd_list->m_rt_descriptor_heap->StageDescriptors(root_parameter_index, descriptor_offset, descriptor_count, handle);\n\t}\n\n\tvoid SetRTShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex)\n\t{\n\t\td3d12::DescHeapCPUHandle handle = tex->m_uav_allocation.GetDescriptorHandle();\n\n\t\tcmd_list->m_rt_descriptor_heap->StageDescriptors(rootParameterIndex, descriptorOffset, 1, handle);\n\t}\n\n\tvoid SetRTShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count)\n\t{\n\t\tcmd_list->m_rt_descriptor_heap->StageDescriptors(root_parameter_index, descriptor_offset, descriptor_count, handle);\n\t}\n\n\tvoid CopyResource(wr::d3d12::CommandList* cmd_list, TextureResource* src_texture, TextureResource* dst_texture)\n\t{\n\t\tResourceState src_original_state = src_texture->m_subresource_states[0];\n\t\tResourceState dst_original_state = dst_texture->m_subresource_states[0];\n\n\t\td3d12::Transition(cmd_list, src_texture, src_original_state, ResourceState::COPY_SOURCE);\n\t\td3d12::Transition(cmd_list, dst_texture, dst_original_state, ResourceState::COPY_DEST);\n\n\t\tcmd_list->m_native->CopyResource(dst_texture->m_resource, src_texture->m_resource);\n\n\t\td3d12::Transition(cmd_list, src_texture, ResourceState::COPY_SOURCE, src_original_state);\n\t\td3d12::Transition(cmd_list, dst_texture, ResourceState::COPY_DEST, dst_original_state);\n\t}\n\n\tvoid Destroy(TextureResource* tex)\n\t{\n\t\tif (tex != nullptr)\n\t\t{\n\t\t\tSAFE_RELEASE(tex->m_resource);\n\t\t\tdelete tex;\n\t\t}\n\t}\n\n\tbool CheckUAVCompatibility(Format format)\n\t{\n\t\tswitch (format)\n\t\t{\n\t\tcase Format::R32G32B32A32_FLOAT:\n\t\tcase Format::R32G32B32A32_UINT:\n\t\tcase Format::R32G32B32A32_SINT:\n\t\tcase Format::R16G16B16A16_FLOAT:\n\t\tcase Format::R16G16B16A16_UINT:\n\t\tcase Format::R16G16B16A16_SINT:\n\t\tcase Format::R8G8B8A8_UNORM:\n\t\tcase Format::R8G8B8A8_UINT:\n\t\tcase Format::R8G8B8A8_SINT:\n\t\tcase Format::R32_FLOAT:\n\t\tcase Format::R32_UINT:\n\t\tcase Format::R32_SINT:\n\t\tcase Format::R16_FLOAT:\n\t\tcase Format::R16_UINT:\n\t\tcase Format::R16_SINT:\n\t\tcase Format::R8_UNORM:\n\t\tcase Format::R8_UINT:\n\t\tcase Format::R8_SINT:\n\t\t\treturn true;\n\t\tdefault:\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tbool CheckOptionalUAVFormat(Format format)\n\t{\n\t\tswitch (format)\n\t\t{\n\t\tcase Format::R16G16B16A16_UNORM:\n\t\tcase Format::R16G16B16A16_SNORM:\n\t\tcase Format::R32G32_FLOAT:\n\t\tcase Format::R32G32_UINT:\n\t\tcase Format::R32G32_SINT:\n\t\tcase Format::R10G10B10A2_UNORM:\n\t\tcase Format::R10G10B10A2_UINT:\n\t\tcase Format::R11G11B10_FLOAT:\n\t\tcase Format::R8G8B8A8_SNORM:\n\t\tcase Format::R16G16_FLOAT:\n\t\tcase Format::R16G16_UNORM:\n\t\tcase Format::R16G16_UINT:\n\t\tcase Format::R16G16_SNORM:\n\t\tcase Format::R16G16_SINT:\n\t\tcase Format::R8G8_UNORM:\n\t\tcase Format::R8G8_UINT:\n\t\tcase Format::R8G8_SNORM:\n\t\tcase Format::R8G8_SINT:\n\t\tcase Format::R16_UNORM:\n\t\tcase Format::R16_SNORM:\n\t\tcase Format::R8_SNORM:\n\t\tcase Format::A8_UNORM:\n\t\tcase Format::B5G6R5_UNORM:\n\t\tcase Format::B5G5R5A1_UNORM:\n\t\tcase Format::B4G4R4A4_UNORM:\n\t\t\treturn true;\n\t\tdefault:\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tbool CheckBGRFormat(Format format)\n\t{\n\t\tswitch (format)\n\t\t{\n\t\tcase Format::B8G8R8A8_UNORM:\n\t\tcase Format::B8G8R8X8_UNORM:\n\t\tcase Format::B8G8R8A8_UNORM_SRGB:\n\t\tcase Format::B8G8R8X8_UNORM_SRGB:\n\t\t\treturn true;\n\t\tdefault:\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tbool CheckSRGBFormat(Format format)\n\t{\n\t\tswitch (format)\n\t\t{\n\t\tcase Format::R8G8B8A8_UNORM_SRGB:\n\t\tcase Format::B8G8R8A8_UNORM_SRGB:\n\t\tcase Format::B8G8R8X8_UNORM_SRGB:\n\t\t\treturn true;\n\t\tdefault:\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tbool IsOptionalFormatSupported(Device* device, Format format)\n\t{\n\t\treturn device->m_optional_formats.test(static_cast<DXGI_FORMAT>(format));\n\t}\n\n\tFormat RemoveSRGB(Format format)\n\t{\n\t\tFormat out_format = Format::UNKNOWN;\n\n\t\tswitch (format)\n\t\t{\n\t\tcase wr::Format::R8G8B8A8_UNORM_SRGB:\n\t\t\tout_format = Format::R8G8B8A8_UNORM;\n\t\t\tbreak;\t\t\n\t\tcase wr::Format::B8G8R8A8_UNORM_SRGB:\n\t\t\tout_format = Format::B8G8R8A8_UNORM;\n\t\t\tbreak;\n\t\tcase wr::Format::B8G8R8X8_UNORM_SRGB:\n\t\t\tout_format = Format::B8G8R8X8_UNORM;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tbreak;\n\t\t}\n\n\t\treturn out_format;\n\t}\n\n\tFormat BGRtoRGB(Format format)\n\t{\n\t\tFormat out_format = Format::UNKNOWN;\n\n\t\tswitch (format)\n\t\t{\n\t\tcase Format::B8G8R8A8_UNORM:\n\t\t\tout_format = Format::R8G8B8A8_UNORM;\n\t\t\tbreak;\n\t\tcase Format::B8G8R8X8_UNORM:\n\t\t\tout_format = Format::R8G8B8A8_UNORM;\n\t\t\tbreak;\n\t\tcase Format::B8G8R8A8_UNORM_SRGB:\n\t\t\tout_format = Format::R8G8B8A8_UNORM_SRGB;\n\t\t\tbreak;\n\t\tcase Format::B8G8R8X8_UNORM_SRGB:\n\t\t\tout_format = Format::R8G8B8A8_UNORM_SRGB;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tbreak;\n\t\t}\n\n\t\treturn out_format;\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3d12_viewport.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"d3d12_functions.hpp\"\n\nnamespace wr::d3d12\n{\n\n\tViewport CreateViewport(int width, int height)\n\t{\n\t\tViewport viewport;\n\n\t\t// Define viewport.\n\t\tviewport.m_viewport.TopLeftX = 0;\n\t\tviewport.m_viewport.TopLeftY = 0;\n\t\tviewport.m_viewport.Width = static_cast<float>(width);\n\t\tviewport.m_viewport.Height = static_cast<float>(height);\n\t\tviewport.m_viewport.MinDepth = 0.0f;\n\t\tviewport.m_viewport.MaxDepth = 1.0f;\n\n\t\t// Define scissor rect\n\t\tviewport.m_scissor_rect.left = 0;\n\t\tviewport.m_scissor_rect.top = 0;\n\t\tviewport.m_scissor_rect.right = width;\n\t\tviewport.m_scissor_rect.bottom = height;\n\n\t\treturn viewport;\n\t}\n\n\tvoid ResizeViewport(Viewport& viewport, int width, int height)\n\t{\n\t\t// Define viewport.\n\t\tviewport.m_viewport.Width = static_cast<float>(width);\n\t\tviewport.m_viewport.Height = static_cast<float>(height);\n\n\t\t// Define scissor rect\n\t\tviewport.m_scissor_rect.right = width;\n\t\tviewport.m_scissor_rect.bottom = height;\n\t}\n\n} /* wr::d3d12 */"
  },
  {
    "path": "src/d3d12/d3dx12.hpp",
    "content": "//*********************************************************\n//\n// Copyright (c) Microsoft. All rights reserved.\n// This code is licensed under the MIT License (MIT).\n// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF\n// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY\n// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR\n// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.\n//\n//*********************************************************\n\n#ifndef __D3DX12_H__\n#define __D3DX12_H__\n\n#pragma warning(push, 0)\n\n#include \"d3d12.h\"\n\n#if defined( __cplusplus )\n\nstruct CD3DX12_DEFAULT {};\nextern const DECLSPEC_SELECTANY CD3DX12_DEFAULT D3D12_DEFAULT;\n\n//------------------------------------------------------------------------------------------------\ninline bool operator==( const D3D12_VIEWPORT& l, const D3D12_VIEWPORT& r )\n{\n    return l.TopLeftX == r.TopLeftX && l.TopLeftY == r.TopLeftY && l.Width == r.Width &&\n        l.Height == r.Height && l.MinDepth == r.MinDepth && l.MaxDepth == r.MaxDepth;\n}\n\n//------------------------------------------------------------------------------------------------\ninline bool operator!=( const D3D12_VIEWPORT& l, const D3D12_VIEWPORT& r )\n{ return !( l == r ); }\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_RECT : public D3D12_RECT\n{\n    CD3DX12_RECT() = default;\n    explicit CD3DX12_RECT( const D3D12_RECT& o ) :\n        D3D12_RECT( o )\n    {}\n    explicit CD3DX12_RECT(\n        LONG Left,\n        LONG Top,\n        LONG Right,\n        LONG Bottom )\n    {\n        left = Left;\n        top = Top;\n        right = Right;\n        bottom = Bottom;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_VIEWPORT : public D3D12_VIEWPORT\n{\n    CD3DX12_VIEWPORT() = default;\n    explicit CD3DX12_VIEWPORT( const D3D12_VIEWPORT& o ) :\n        D3D12_VIEWPORT( o )\n    {}\n    explicit CD3DX12_VIEWPORT(\n        FLOAT topLeftX,\n        FLOAT topLeftY,\n        FLOAT width,\n        FLOAT height,\n        FLOAT minDepth = D3D12_MIN_DEPTH,\n        FLOAT maxDepth = D3D12_MAX_DEPTH )\n    {\n        TopLeftX = topLeftX;\n        TopLeftY = topLeftY;\n        Width = width;\n        Height = height;\n        MinDepth = minDepth;\n        MaxDepth = maxDepth;\n    }\n    explicit CD3DX12_VIEWPORT(\n        _In_ ID3D12Resource* pResource,\n        UINT mipSlice = 0,\n        FLOAT topLeftX = 0.0f,\n        FLOAT topLeftY = 0.0f,\n        FLOAT minDepth = D3D12_MIN_DEPTH,\n        FLOAT maxDepth = D3D12_MAX_DEPTH )\n    {\n        auto Desc = pResource->GetDesc();\n        const UINT64 SubresourceWidth = Desc.Width >> mipSlice;\n        const UINT64 SubresourceHeight = Desc.Height >> mipSlice;\n        switch (Desc.Dimension)\n        {\n        case D3D12_RESOURCE_DIMENSION_BUFFER:\n            TopLeftX = topLeftX;\n            TopLeftY = 0.0f;\n            Width = Desc.Width - topLeftX;\n            Height = 1.0f;\n            break;\n        case D3D12_RESOURCE_DIMENSION_TEXTURE1D:\n            TopLeftX = topLeftX;\n            TopLeftY = 0.0f;\n            Width = (SubresourceWidth ? SubresourceWidth : 1.0f) - topLeftX;\n            Height = 1.0f;\n            break;\n        case D3D12_RESOURCE_DIMENSION_TEXTURE2D:\n        case D3D12_RESOURCE_DIMENSION_TEXTURE3D:\n            TopLeftX = topLeftX;\n            TopLeftY = topLeftY;\n            Width = (SubresourceWidth ? SubresourceWidth : 1.0f) - topLeftX;\n            Height = (SubresourceHeight ? SubresourceHeight: 1.0f) - topLeftY;\n            break;\n        default: break;\n        }\n\n        MinDepth = minDepth;\n        MaxDepth = maxDepth;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_BOX : public D3D12_BOX\n{\n    CD3DX12_BOX() = default;\n    explicit CD3DX12_BOX( const D3D12_BOX& o ) :\n        D3D12_BOX( o )\n    {}\n    explicit CD3DX12_BOX(\n        LONG Left,\n        LONG Right )\n    {\n        left = Left;\n        top = 0;\n        front = 0;\n        right = Right;\n        bottom = 1;\n        back = 1;\n    }\n    explicit CD3DX12_BOX(\n        LONG Left,\n        LONG Top,\n        LONG Right,\n        LONG Bottom )\n    {\n        left = Left;\n        top = Top;\n        front = 0;\n        right = Right;\n        bottom = Bottom;\n        back = 1;\n    }\n    explicit CD3DX12_BOX(\n        LONG Left,\n        LONG Top,\n        LONG Front,\n        LONG Right,\n        LONG Bottom,\n        LONG Back )\n    {\n        left = Left;\n        top = Top;\n        front = Front;\n        right = Right;\n        bottom = Bottom;\n        back = Back;\n    }\n};\ninline bool operator==( const D3D12_BOX& l, const D3D12_BOX& r )\n{\n    return l.left == r.left && l.top == r.top && l.front == r.front &&\n        l.right == r.right && l.bottom == r.bottom && l.back == r.back;\n}\ninline bool operator!=( const D3D12_BOX& l, const D3D12_BOX& r )\n{ return !( l == r ); }\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_DEPTH_STENCIL_DESC : public D3D12_DEPTH_STENCIL_DESC\n{\n    CD3DX12_DEPTH_STENCIL_DESC() = default;\n    explicit CD3DX12_DEPTH_STENCIL_DESC( const D3D12_DEPTH_STENCIL_DESC& o ) :\n        D3D12_DEPTH_STENCIL_DESC( o )\n    {}\n    explicit CD3DX12_DEPTH_STENCIL_DESC( CD3DX12_DEFAULT )\n    {\n        DepthEnable = TRUE;\n        DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;\n        DepthFunc = D3D12_COMPARISON_FUNC_LESS;\n        StencilEnable = FALSE;\n        StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;\n        StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;\n        const D3D12_DEPTH_STENCILOP_DESC defaultStencilOp =\n        { D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS };\n        FrontFace = defaultStencilOp;\n        BackFace = defaultStencilOp;\n    }\n    explicit CD3DX12_DEPTH_STENCIL_DESC(\n        BOOL depthEnable,\n        D3D12_DEPTH_WRITE_MASK depthWriteMask,\n        D3D12_COMPARISON_FUNC depthFunc,\n        BOOL stencilEnable,\n        UINT8 stencilReadMask,\n        UINT8 stencilWriteMask,\n        D3D12_STENCIL_OP frontStencilFailOp,\n        D3D12_STENCIL_OP frontStencilDepthFailOp,\n        D3D12_STENCIL_OP frontStencilPassOp,\n        D3D12_COMPARISON_FUNC frontStencilFunc,\n        D3D12_STENCIL_OP backStencilFailOp,\n        D3D12_STENCIL_OP backStencilDepthFailOp,\n        D3D12_STENCIL_OP backStencilPassOp,\n        D3D12_COMPARISON_FUNC backStencilFunc )\n    {\n        DepthEnable = depthEnable;\n        DepthWriteMask = depthWriteMask;\n        DepthFunc = depthFunc;\n        StencilEnable = stencilEnable;\n        StencilReadMask = stencilReadMask;\n        StencilWriteMask = stencilWriteMask;\n        FrontFace.StencilFailOp = frontStencilFailOp;\n        FrontFace.StencilDepthFailOp = frontStencilDepthFailOp;\n        FrontFace.StencilPassOp = frontStencilPassOp;\n        FrontFace.StencilFunc = frontStencilFunc;\n        BackFace.StencilFailOp = backStencilFailOp;\n        BackFace.StencilDepthFailOp = backStencilDepthFailOp;\n        BackFace.StencilPassOp = backStencilPassOp;\n        BackFace.StencilFunc = backStencilFunc;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_DEPTH_STENCIL_DESC1 : public D3D12_DEPTH_STENCIL_DESC1\n{\n    CD3DX12_DEPTH_STENCIL_DESC1() = default;\n    explicit CD3DX12_DEPTH_STENCIL_DESC1( const D3D12_DEPTH_STENCIL_DESC1& o ) :\n        D3D12_DEPTH_STENCIL_DESC1( o )\n    {}\n    explicit CD3DX12_DEPTH_STENCIL_DESC1( const D3D12_DEPTH_STENCIL_DESC& o )\n    {\n        DepthEnable                  = o.DepthEnable;\n        DepthWriteMask               = o.DepthWriteMask;\n        DepthFunc                    = o.DepthFunc;\n        StencilEnable                = o.StencilEnable;\n        StencilReadMask              = o.StencilReadMask;\n        StencilWriteMask             = o.StencilWriteMask;\n        FrontFace.StencilFailOp      = o.FrontFace.StencilFailOp;\n        FrontFace.StencilDepthFailOp = o.FrontFace.StencilDepthFailOp;\n        FrontFace.StencilPassOp      = o.FrontFace.StencilPassOp;\n        FrontFace.StencilFunc        = o.FrontFace.StencilFunc;\n        BackFace.StencilFailOp       = o.BackFace.StencilFailOp;\n        BackFace.StencilDepthFailOp  = o.BackFace.StencilDepthFailOp;\n        BackFace.StencilPassOp       = o.BackFace.StencilPassOp;\n        BackFace.StencilFunc         = o.BackFace.StencilFunc;\n        DepthBoundsTestEnable        = FALSE;\n    }\n    explicit CD3DX12_DEPTH_STENCIL_DESC1( CD3DX12_DEFAULT )\n    {\n        DepthEnable = TRUE;\n        DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;\n        DepthFunc = D3D12_COMPARISON_FUNC_LESS;\n        StencilEnable = FALSE;\n        StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;\n        StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;\n        const D3D12_DEPTH_STENCILOP_DESC defaultStencilOp =\n        { D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS };\n        FrontFace = defaultStencilOp;\n        BackFace = defaultStencilOp;\n        DepthBoundsTestEnable = FALSE;\n    }\n    explicit CD3DX12_DEPTH_STENCIL_DESC1(\n        BOOL depthEnable,\n        D3D12_DEPTH_WRITE_MASK depthWriteMask,\n        D3D12_COMPARISON_FUNC depthFunc,\n        BOOL stencilEnable,\n        UINT8 stencilReadMask,\n        UINT8 stencilWriteMask,\n        D3D12_STENCIL_OP frontStencilFailOp,\n        D3D12_STENCIL_OP frontStencilDepthFailOp,\n        D3D12_STENCIL_OP frontStencilPassOp,\n        D3D12_COMPARISON_FUNC frontStencilFunc,\n        D3D12_STENCIL_OP backStencilFailOp,\n        D3D12_STENCIL_OP backStencilDepthFailOp,\n        D3D12_STENCIL_OP backStencilPassOp,\n        D3D12_COMPARISON_FUNC backStencilFunc,\n        BOOL depthBoundsTestEnable )\n    {\n        DepthEnable = depthEnable;\n        DepthWriteMask = depthWriteMask;\n        DepthFunc = depthFunc;\n        StencilEnable = stencilEnable;\n        StencilReadMask = stencilReadMask;\n        StencilWriteMask = stencilWriteMask;\n        FrontFace.StencilFailOp = frontStencilFailOp;\n        FrontFace.StencilDepthFailOp = frontStencilDepthFailOp;\n        FrontFace.StencilPassOp = frontStencilPassOp;\n        FrontFace.StencilFunc = frontStencilFunc;\n        BackFace.StencilFailOp = backStencilFailOp;\n        BackFace.StencilDepthFailOp = backStencilDepthFailOp;\n        BackFace.StencilPassOp = backStencilPassOp;\n        BackFace.StencilFunc = backStencilFunc;\n        DepthBoundsTestEnable = depthBoundsTestEnable;\n    }\n    operator D3D12_DEPTH_STENCIL_DESC() const\n    {\n        D3D12_DEPTH_STENCIL_DESC D;\n        D.DepthEnable                  = DepthEnable;\n        D.DepthWriteMask               = DepthWriteMask;\n        D.DepthFunc                    = DepthFunc;\n        D.StencilEnable                = StencilEnable;\n        D.StencilReadMask              = StencilReadMask;\n        D.StencilWriteMask             = StencilWriteMask;\n        D.FrontFace.StencilFailOp      = FrontFace.StencilFailOp;\n        D.FrontFace.StencilDepthFailOp = FrontFace.StencilDepthFailOp;\n        D.FrontFace.StencilPassOp      = FrontFace.StencilPassOp;\n        D.FrontFace.StencilFunc        = FrontFace.StencilFunc;\n        D.BackFace.StencilFailOp       = BackFace.StencilFailOp;\n        D.BackFace.StencilDepthFailOp  = BackFace.StencilDepthFailOp;\n        D.BackFace.StencilPassOp       = BackFace.StencilPassOp;\n        D.BackFace.StencilFunc         = BackFace.StencilFunc;\n        return D;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_BLEND_DESC : public D3D12_BLEND_DESC\n{\n    CD3DX12_BLEND_DESC() = default;\n    explicit CD3DX12_BLEND_DESC( const D3D12_BLEND_DESC& o ) :\n        D3D12_BLEND_DESC( o )\n    {}\n    explicit CD3DX12_BLEND_DESC( CD3DX12_DEFAULT )\n    {\n        AlphaToCoverageEnable = FALSE;\n        IndependentBlendEnable = FALSE;\n        const D3D12_RENDER_TARGET_BLEND_DESC defaultRenderTargetBlendDesc =\n        {\n            FALSE,FALSE,\n            D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,\n            D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,\n            D3D12_LOGIC_OP_NOOP,\n            D3D12_COLOR_WRITE_ENABLE_ALL,\n        };\n        for (UINT i = 0; i < D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i)\n            RenderTarget[ i ] = defaultRenderTargetBlendDesc;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_RASTERIZER_DESC : public D3D12_RASTERIZER_DESC\n{\n    CD3DX12_RASTERIZER_DESC() = default;\n    explicit CD3DX12_RASTERIZER_DESC( const D3D12_RASTERIZER_DESC& o ) :\n        D3D12_RASTERIZER_DESC( o )\n    {}\n    explicit CD3DX12_RASTERIZER_DESC( CD3DX12_DEFAULT )\n    {\n        FillMode = D3D12_FILL_MODE_SOLID;\n        CullMode = D3D12_CULL_MODE_BACK;\n        FrontCounterClockwise = FALSE;\n        DepthBias = D3D12_DEFAULT_DEPTH_BIAS;\n        DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;\n        SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;\n        DepthClipEnable = TRUE;\n        MultisampleEnable = FALSE;\n        AntialiasedLineEnable = FALSE;\n        ForcedSampleCount = 0;\n        ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;\n    }\n    explicit CD3DX12_RASTERIZER_DESC(\n        D3D12_FILL_MODE fillMode,\n        D3D12_CULL_MODE cullMode,\n        BOOL frontCounterClockwise,\n        INT depthBias,\n        FLOAT depthBiasClamp,\n        FLOAT slopeScaledDepthBias,\n        BOOL depthClipEnable,\n        BOOL multisampleEnable,\n        BOOL antialiasedLineEnable, \n        UINT forcedSampleCount, \n        D3D12_CONSERVATIVE_RASTERIZATION_MODE conservativeRaster)\n    {\n        FillMode = fillMode;\n        CullMode = cullMode;\n        FrontCounterClockwise = frontCounterClockwise;\n        DepthBias = depthBias;\n        DepthBiasClamp = depthBiasClamp;\n        SlopeScaledDepthBias = slopeScaledDepthBias;\n        DepthClipEnable = depthClipEnable;\n        MultisampleEnable = multisampleEnable;\n        AntialiasedLineEnable = antialiasedLineEnable;\n        ForcedSampleCount = forcedSampleCount;\n        ConservativeRaster = conservativeRaster;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_RESOURCE_ALLOCATION_INFO : public D3D12_RESOURCE_ALLOCATION_INFO\n{\n    CD3DX12_RESOURCE_ALLOCATION_INFO() = default;\n    explicit CD3DX12_RESOURCE_ALLOCATION_INFO( const D3D12_RESOURCE_ALLOCATION_INFO& o ) :\n        D3D12_RESOURCE_ALLOCATION_INFO( o )\n    {}\n    CD3DX12_RESOURCE_ALLOCATION_INFO(\n        UINT64 size,\n        UINT64 alignment )\n    {\n        SizeInBytes = size;\n        Alignment = alignment;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_HEAP_PROPERTIES : public D3D12_HEAP_PROPERTIES\n{\n    CD3DX12_HEAP_PROPERTIES() = default;\n    explicit CD3DX12_HEAP_PROPERTIES(const D3D12_HEAP_PROPERTIES &o) :\n        D3D12_HEAP_PROPERTIES(o)\n    {}\n    CD3DX12_HEAP_PROPERTIES( \n        D3D12_CPU_PAGE_PROPERTY cpuPageProperty, \n        D3D12_MEMORY_POOL memoryPoolPreference,\n        UINT creationNodeMask = 1, \n        UINT nodeMask = 1 )\n    {\n        Type = D3D12_HEAP_TYPE_CUSTOM;\n        CPUPageProperty = cpuPageProperty;\n        MemoryPoolPreference = memoryPoolPreference;\n        CreationNodeMask = creationNodeMask;\n        VisibleNodeMask = nodeMask;\n    }\n    explicit CD3DX12_HEAP_PROPERTIES( \n        D3D12_HEAP_TYPE type, \n        UINT creationNodeMask = 1, \n        UINT nodeMask = 1 )\n    {\n        Type = type;\n        CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;\n        MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;\n        CreationNodeMask = creationNodeMask;\n        VisibleNodeMask = nodeMask;\n    }\n    bool IsCPUAccessible() const\n    {\n        return Type == D3D12_HEAP_TYPE_UPLOAD || Type == D3D12_HEAP_TYPE_READBACK || (Type == D3D12_HEAP_TYPE_CUSTOM &&\n            (CPUPageProperty == D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE || CPUPageProperty == D3D12_CPU_PAGE_PROPERTY_WRITE_BACK));\n    }\n};\ninline bool operator==( const D3D12_HEAP_PROPERTIES& l, const D3D12_HEAP_PROPERTIES& r )\n{\n    return l.Type == r.Type && l.CPUPageProperty == r.CPUPageProperty && \n        l.MemoryPoolPreference == r.MemoryPoolPreference &&\n        l.CreationNodeMask == r.CreationNodeMask &&\n        l.VisibleNodeMask == r.VisibleNodeMask;\n}\ninline bool operator!=( const D3D12_HEAP_PROPERTIES& l, const D3D12_HEAP_PROPERTIES& r )\n{ return !( l == r ); }\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_HEAP_DESC : public D3D12_HEAP_DESC\n{\n    CD3DX12_HEAP_DESC() = default;\n    explicit CD3DX12_HEAP_DESC(const D3D12_HEAP_DESC &o) :\n        D3D12_HEAP_DESC(o)\n    {}\n    CD3DX12_HEAP_DESC( \n        UINT64 size, \n        D3D12_HEAP_PROPERTIES properties, \n        UINT64 alignment = 0, \n        D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )\n    {\n        SizeInBytes = size;\n        Properties = properties;\n        Alignment = alignment;\n        Flags = flags;\n    }\n    CD3DX12_HEAP_DESC( \n        UINT64 size, \n        D3D12_HEAP_TYPE type, \n        UINT64 alignment = 0, \n        D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )\n    {\n        SizeInBytes = size;\n        Properties = CD3DX12_HEAP_PROPERTIES( type );\n        Alignment = alignment;\n        Flags = flags;\n    }\n    CD3DX12_HEAP_DESC( \n        UINT64 size, \n        D3D12_CPU_PAGE_PROPERTY cpuPageProperty, \n        D3D12_MEMORY_POOL memoryPoolPreference, \n        UINT64 alignment = 0, \n        D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )\n    {\n        SizeInBytes = size;\n        Properties = CD3DX12_HEAP_PROPERTIES( cpuPageProperty, memoryPoolPreference );\n        Alignment = alignment;\n        Flags = flags;\n    }\n    CD3DX12_HEAP_DESC( \n        const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo,\n        D3D12_HEAP_PROPERTIES properties, \n        D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )\n    {\n        SizeInBytes = resAllocInfo.SizeInBytes;\n        Properties = properties;\n        Alignment = resAllocInfo.Alignment;\n        Flags = flags;\n    }\n    CD3DX12_HEAP_DESC( \n        const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo,\n        D3D12_HEAP_TYPE type, \n        D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )\n    {\n        SizeInBytes = resAllocInfo.SizeInBytes;\n        Properties = CD3DX12_HEAP_PROPERTIES( type );\n        Alignment = resAllocInfo.Alignment;\n        Flags = flags;\n    }\n    CD3DX12_HEAP_DESC( \n        const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo,\n        D3D12_CPU_PAGE_PROPERTY cpuPageProperty, \n        D3D12_MEMORY_POOL memoryPoolPreference, \n        D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )\n    {\n        SizeInBytes = resAllocInfo.SizeInBytes;\n        Properties = CD3DX12_HEAP_PROPERTIES( cpuPageProperty, memoryPoolPreference );\n        Alignment = resAllocInfo.Alignment;\n        Flags = flags;\n    }\n    bool IsCPUAccessible() const\n    { return static_cast< const CD3DX12_HEAP_PROPERTIES* >( &Properties )->IsCPUAccessible(); }\n};\ninline bool operator==( const D3D12_HEAP_DESC& l, const D3D12_HEAP_DESC& r )\n{\n    return l.SizeInBytes == r.SizeInBytes &&\n        l.Properties == r.Properties && \n        l.Alignment == r.Alignment &&\n        l.Flags == r.Flags;\n}\ninline bool operator!=( const D3D12_HEAP_DESC& l, const D3D12_HEAP_DESC& r )\n{ return !( l == r ); }\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_CLEAR_VALUE : public D3D12_CLEAR_VALUE\n{\n    CD3DX12_CLEAR_VALUE() = default;\n    explicit CD3DX12_CLEAR_VALUE(const D3D12_CLEAR_VALUE &o) :\n        D3D12_CLEAR_VALUE(o)\n    {}\n    CD3DX12_CLEAR_VALUE( \n        DXGI_FORMAT format, \n        const FLOAT color[4] )\n    {\n        Format = format;\n        memcpy( Color, color, sizeof( Color ) );\n    }\n    CD3DX12_CLEAR_VALUE( \n        DXGI_FORMAT format, \n        FLOAT depth,\n        UINT8 stencil )\n    {\n        Format = format;\n        /* Use memcpy to preserve NAN values */\n        memcpy( &DepthStencil.Depth, &depth, sizeof( depth ) );\n        DepthStencil.Stencil = stencil;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_RANGE : public D3D12_RANGE\n{\n    CD3DX12_RANGE() = default;\n    explicit CD3DX12_RANGE(const D3D12_RANGE &o) :\n        D3D12_RANGE(o)\n    {}\n    CD3DX12_RANGE( \n        SIZE_T begin, \n        SIZE_T end )\n    {\n        Begin = begin;\n        End = end;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_RANGE_UINT64 : public D3D12_RANGE_UINT64\n{\n    CD3DX12_RANGE_UINT64() = default;\n    explicit CD3DX12_RANGE_UINT64(const D3D12_RANGE_UINT64 &o) :\n        D3D12_RANGE_UINT64(o)\n    {}\n    CD3DX12_RANGE_UINT64( \n        UINT64 begin, \n        UINT64 end )\n    {\n        Begin = begin;\n        End = end;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_SUBRESOURCE_RANGE_UINT64 : public D3D12_SUBRESOURCE_RANGE_UINT64\n{\n    CD3DX12_SUBRESOURCE_RANGE_UINT64() = default;\n    explicit CD3DX12_SUBRESOURCE_RANGE_UINT64(const D3D12_SUBRESOURCE_RANGE_UINT64 &o) :\n        D3D12_SUBRESOURCE_RANGE_UINT64(o)\n    {}\n    CD3DX12_SUBRESOURCE_RANGE_UINT64( \n        UINT subresource,\n        const D3D12_RANGE_UINT64& range )\n    {\n        Subresource = subresource;\n        Range = range;\n    }\n    CD3DX12_SUBRESOURCE_RANGE_UINT64( \n        UINT subresource,\n        UINT64 begin, \n        UINT64 end )\n    {\n        Subresource = subresource;\n        Range.Begin = begin;\n        Range.End = end;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_SHADER_BYTECODE : public D3D12_SHADER_BYTECODE\n{\n    CD3DX12_SHADER_BYTECODE() = default;\n    explicit CD3DX12_SHADER_BYTECODE(const D3D12_SHADER_BYTECODE &o) :\n        D3D12_SHADER_BYTECODE(o)\n    {}\n    CD3DX12_SHADER_BYTECODE(\n        _In_ ID3DBlob* pShaderBlob )\n    {\n        pShaderBytecode = pShaderBlob->GetBufferPointer();\n        BytecodeLength = pShaderBlob->GetBufferSize();\n    }\n    CD3DX12_SHADER_BYTECODE(\n        const void* _pShaderBytecode,\n        SIZE_T bytecodeLength )\n    {\n        pShaderBytecode = _pShaderBytecode;\n        BytecodeLength = bytecodeLength;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_TILED_RESOURCE_COORDINATE : public D3D12_TILED_RESOURCE_COORDINATE\n{\n    CD3DX12_TILED_RESOURCE_COORDINATE() = default;\n    explicit CD3DX12_TILED_RESOURCE_COORDINATE(const D3D12_TILED_RESOURCE_COORDINATE &o) :\n        D3D12_TILED_RESOURCE_COORDINATE(o)\n    {}\n    CD3DX12_TILED_RESOURCE_COORDINATE( \n        UINT x, \n        UINT y, \n        UINT z, \n        UINT subresource ) \n    {\n        X = x;\n        Y = y;\n        Z = z;\n        Subresource = subresource;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_TILE_REGION_SIZE : public D3D12_TILE_REGION_SIZE\n{\n    CD3DX12_TILE_REGION_SIZE() = default;\n    explicit CD3DX12_TILE_REGION_SIZE(const D3D12_TILE_REGION_SIZE &o) :\n        D3D12_TILE_REGION_SIZE(o)\n    {}\n    CD3DX12_TILE_REGION_SIZE( \n        UINT numTiles, \n        BOOL useBox, \n        UINT width, \n        UINT16 height, \n        UINT16 depth ) \n    {\n        NumTiles = numTiles;\n        UseBox = useBox;\n        Width = width;\n        Height = height;\n        Depth = depth;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_SUBRESOURCE_TILING : public D3D12_SUBRESOURCE_TILING\n{\n    CD3DX12_SUBRESOURCE_TILING() = default;\n    explicit CD3DX12_SUBRESOURCE_TILING(const D3D12_SUBRESOURCE_TILING &o) :\n        D3D12_SUBRESOURCE_TILING(o)\n    {}\n    CD3DX12_SUBRESOURCE_TILING( \n        UINT widthInTiles, \n        UINT16 heightInTiles, \n        UINT16 depthInTiles, \n        UINT startTileIndexInOverallResource ) \n    {\n        WidthInTiles = widthInTiles;\n        HeightInTiles = heightInTiles;\n        DepthInTiles = depthInTiles;\n        StartTileIndexInOverallResource = startTileIndexInOverallResource;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_TILE_SHAPE : public D3D12_TILE_SHAPE\n{\n    CD3DX12_TILE_SHAPE() = default;\n    explicit CD3DX12_TILE_SHAPE(const D3D12_TILE_SHAPE &o) :\n        D3D12_TILE_SHAPE(o)\n    {}\n    CD3DX12_TILE_SHAPE( \n        UINT widthInTexels, \n        UINT heightInTexels, \n        UINT depthInTexels ) \n    {\n        WidthInTexels = widthInTexels;\n        HeightInTexels = heightInTexels;\n        DepthInTexels = depthInTexels;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_RESOURCE_BARRIER : public D3D12_RESOURCE_BARRIER\n{\n    CD3DX12_RESOURCE_BARRIER() = default;\n    explicit CD3DX12_RESOURCE_BARRIER(const D3D12_RESOURCE_BARRIER &o) :\n        D3D12_RESOURCE_BARRIER(o)\n    {}\n    static inline CD3DX12_RESOURCE_BARRIER Transition(\n        _In_ ID3D12Resource* pResource,\n        D3D12_RESOURCE_STATES stateBefore,\n        D3D12_RESOURCE_STATES stateAfter,\n        UINT subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,\n        D3D12_RESOURCE_BARRIER_FLAGS flags = D3D12_RESOURCE_BARRIER_FLAG_NONE)\n    {\n        CD3DX12_RESOURCE_BARRIER result = {};\n        D3D12_RESOURCE_BARRIER &barrier = result;\n        result.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;\n        result.Flags = flags;\n        barrier.Transition.pResource = pResource;\n        barrier.Transition.StateBefore = stateBefore;\n        barrier.Transition.StateAfter = stateAfter;\n        barrier.Transition.Subresource = subresource;\n        return result;\n    }\n    static inline CD3DX12_RESOURCE_BARRIER Aliasing(\n        _In_ ID3D12Resource* pResourceBefore,\n        _In_ ID3D12Resource* pResourceAfter)\n    {\n        CD3DX12_RESOURCE_BARRIER result = {};\n        D3D12_RESOURCE_BARRIER &barrier = result;\n        result.Type = D3D12_RESOURCE_BARRIER_TYPE_ALIASING;\n        barrier.Aliasing.pResourceBefore = pResourceBefore;\n        barrier.Aliasing.pResourceAfter = pResourceAfter;\n        return result;\n    }\n    static inline CD3DX12_RESOURCE_BARRIER UAV(\n        _In_ ID3D12Resource* pResource)\n    {\n        CD3DX12_RESOURCE_BARRIER result = {};\n        D3D12_RESOURCE_BARRIER &barrier = result;\n        result.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;\n        barrier.UAV.pResource = pResource;\n        return result;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_PACKED_MIP_INFO : public D3D12_PACKED_MIP_INFO\n{\n    CD3DX12_PACKED_MIP_INFO() = default;\n    explicit CD3DX12_PACKED_MIP_INFO(const D3D12_PACKED_MIP_INFO &o) :\n        D3D12_PACKED_MIP_INFO(o)\n    {}\n    CD3DX12_PACKED_MIP_INFO( \n        UINT8 numStandardMips, \n        UINT8 numPackedMips, \n        UINT numTilesForPackedMips, \n        UINT startTileIndexInOverallResource ) \n    {\n        NumStandardMips = numStandardMips;\n        NumPackedMips = numPackedMips;\n        NumTilesForPackedMips = numTilesForPackedMips;\n        StartTileIndexInOverallResource = startTileIndexInOverallResource;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_SUBRESOURCE_FOOTPRINT : public D3D12_SUBRESOURCE_FOOTPRINT\n{\n    CD3DX12_SUBRESOURCE_FOOTPRINT() = default;\n    explicit CD3DX12_SUBRESOURCE_FOOTPRINT(const D3D12_SUBRESOURCE_FOOTPRINT &o) :\n        D3D12_SUBRESOURCE_FOOTPRINT(o)\n    {}\n    CD3DX12_SUBRESOURCE_FOOTPRINT( \n        DXGI_FORMAT format, \n        UINT width, \n        UINT height, \n        UINT depth, \n        UINT rowPitch ) \n    {\n        Format = format;\n        Width = width;\n        Height = height;\n        Depth = depth;\n        RowPitch = rowPitch;\n    }\n    explicit CD3DX12_SUBRESOURCE_FOOTPRINT( \n        const D3D12_RESOURCE_DESC& resDesc, \n        UINT rowPitch ) \n    {\n        Format = resDesc.Format;\n        Width = UINT( resDesc.Width );\n        Height = resDesc.Height;\n        Depth = (resDesc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? resDesc.DepthOrArraySize : 1);\n        RowPitch = rowPitch;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_TEXTURE_COPY_LOCATION : public D3D12_TEXTURE_COPY_LOCATION\n{ \n    CD3DX12_TEXTURE_COPY_LOCATION() = default;\n    explicit CD3DX12_TEXTURE_COPY_LOCATION(const D3D12_TEXTURE_COPY_LOCATION &o) :\n        D3D12_TEXTURE_COPY_LOCATION(o)\n    {}\n    CD3DX12_TEXTURE_COPY_LOCATION(_In_ ID3D12Resource* pRes)\n    {\n        pResource = pRes;\n        Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;\n        PlacedFootprint = {};\n    }\n    CD3DX12_TEXTURE_COPY_LOCATION(_In_ ID3D12Resource* pRes, D3D12_PLACED_SUBRESOURCE_FOOTPRINT const& Footprint)\n    {\n        pResource = pRes;\n        Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;\n        PlacedFootprint = Footprint;\n    }\n    CD3DX12_TEXTURE_COPY_LOCATION(_In_ ID3D12Resource* pRes, UINT Sub)\n    {\n        pResource = pRes;\n        Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;\n        SubresourceIndex = Sub;\n    }\n}; \n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_DESCRIPTOR_RANGE : public D3D12_DESCRIPTOR_RANGE\n{\n    CD3DX12_DESCRIPTOR_RANGE() = default;\n    explicit CD3DX12_DESCRIPTOR_RANGE(const D3D12_DESCRIPTOR_RANGE &o) :\n        D3D12_DESCRIPTOR_RANGE(o)\n    {}\n    CD3DX12_DESCRIPTOR_RANGE(\n        D3D12_DESCRIPTOR_RANGE_TYPE rangeType,\n        UINT numDescriptors,\n        UINT baseShaderRegister,\n        UINT registerSpace = 0,\n        UINT offsetInDescriptorsFromTableStart =\n        D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND)\n    {\n        Init(rangeType, numDescriptors, baseShaderRegister, registerSpace, offsetInDescriptorsFromTableStart);\n    }\n    \n    inline void Init(\n        D3D12_DESCRIPTOR_RANGE_TYPE rangeType,\n        UINT numDescriptors,\n        UINT baseShaderRegister,\n        UINT registerSpace = 0,\n        UINT offsetInDescriptorsFromTableStart =\n        D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND)\n    {\n        Init(*this, rangeType, numDescriptors, baseShaderRegister, registerSpace, offsetInDescriptorsFromTableStart);\n    }\n    \n    static inline void Init(\n        _Out_ D3D12_DESCRIPTOR_RANGE &range,\n        D3D12_DESCRIPTOR_RANGE_TYPE rangeType,\n        UINT numDescriptors,\n        UINT baseShaderRegister,\n        UINT registerSpace = 0,\n        UINT offsetInDescriptorsFromTableStart =\n        D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND)\n    {\n        range.RangeType = rangeType;\n        range.NumDescriptors = numDescriptors;\n        range.BaseShaderRegister = baseShaderRegister;\n        range.RegisterSpace = registerSpace;\n        range.OffsetInDescriptorsFromTableStart = offsetInDescriptorsFromTableStart;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_ROOT_DESCRIPTOR_TABLE : public D3D12_ROOT_DESCRIPTOR_TABLE\n{\n    CD3DX12_ROOT_DESCRIPTOR_TABLE() = default;\n    explicit CD3DX12_ROOT_DESCRIPTOR_TABLE(const D3D12_ROOT_DESCRIPTOR_TABLE &o) :\n        D3D12_ROOT_DESCRIPTOR_TABLE(o)\n    {}\n    CD3DX12_ROOT_DESCRIPTOR_TABLE(\n        UINT numDescriptorRanges,\n        _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges)\n    {\n        Init(numDescriptorRanges, _pDescriptorRanges);\n    }\n    \n    inline void Init(\n        UINT numDescriptorRanges,\n        _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges)\n    {\n        Init(*this, numDescriptorRanges, _pDescriptorRanges);\n    }\n    \n    static inline void Init(\n        _Out_ D3D12_ROOT_DESCRIPTOR_TABLE &rootDescriptorTable,\n        UINT numDescriptorRanges,\n        _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges)\n    {\n        rootDescriptorTable.NumDescriptorRanges = numDescriptorRanges;\n        rootDescriptorTable.pDescriptorRanges = _pDescriptorRanges;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_ROOT_CONSTANTS : public D3D12_ROOT_CONSTANTS\n{\n    CD3DX12_ROOT_CONSTANTS() = default;\n    explicit CD3DX12_ROOT_CONSTANTS(const D3D12_ROOT_CONSTANTS &o) :\n        D3D12_ROOT_CONSTANTS(o)\n    {}\n    CD3DX12_ROOT_CONSTANTS(\n        UINT num32BitValues,\n        UINT shaderRegister,\n        UINT registerSpace = 0)\n    {\n        Init(num32BitValues, shaderRegister, registerSpace);\n    }\n    \n    inline void Init(\n        UINT num32BitValues,\n        UINT shaderRegister,\n        UINT registerSpace = 0)\n    {\n        Init(*this, num32BitValues, shaderRegister, registerSpace);\n    }\n    \n    static inline void Init(\n        _Out_ D3D12_ROOT_CONSTANTS &rootConstants,\n        UINT num32BitValues,\n        UINT shaderRegister,\n        UINT registerSpace = 0)\n    {\n        rootConstants.Num32BitValues = num32BitValues;\n        rootConstants.ShaderRegister = shaderRegister;\n        rootConstants.RegisterSpace = registerSpace;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_ROOT_DESCRIPTOR : public D3D12_ROOT_DESCRIPTOR\n{\n    CD3DX12_ROOT_DESCRIPTOR() = default;\n    explicit CD3DX12_ROOT_DESCRIPTOR(const D3D12_ROOT_DESCRIPTOR &o) :\n        D3D12_ROOT_DESCRIPTOR(o)\n    {}\n    CD3DX12_ROOT_DESCRIPTOR(\n        UINT shaderRegister,\n        UINT registerSpace = 0)\n    {\n        Init(shaderRegister, registerSpace);\n    }\n    \n    inline void Init(\n        UINT shaderRegister,\n        UINT registerSpace = 0)\n    {\n        Init(*this, shaderRegister, registerSpace);\n    }\n    \n    static inline void Init(_Out_ D3D12_ROOT_DESCRIPTOR &table, UINT shaderRegister, UINT registerSpace = 0)\n    {\n        table.ShaderRegister = shaderRegister;\n        table.RegisterSpace = registerSpace;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_ROOT_PARAMETER : public D3D12_ROOT_PARAMETER\n{\n    CD3DX12_ROOT_PARAMETER() = default;\n    explicit CD3DX12_ROOT_PARAMETER(const D3D12_ROOT_PARAMETER &o) :\n        D3D12_ROOT_PARAMETER(o)\n    {}\n    \n    static inline void InitAsDescriptorTable(\n        _Out_ D3D12_ROOT_PARAMETER &rootParam,\n        UINT numDescriptorRanges,\n        _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* pDescriptorRanges,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_DESCRIPTOR_TABLE::Init(rootParam.DescriptorTable, numDescriptorRanges, pDescriptorRanges);\n    }\n\n    static inline void InitAsConstants(\n        _Out_ D3D12_ROOT_PARAMETER &rootParam,\n        UINT num32BitValues,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_CONSTANTS::Init(rootParam.Constants, num32BitValues, shaderRegister, registerSpace);\n    }\n\n    static inline void InitAsConstantBufferView(\n        _Out_ D3D12_ROOT_PARAMETER &rootParam,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_DESCRIPTOR::Init(rootParam.Descriptor, shaderRegister, registerSpace);\n    }\n\n    static inline void InitAsShaderResourceView(\n        _Out_ D3D12_ROOT_PARAMETER &rootParam,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_DESCRIPTOR::Init(rootParam.Descriptor, shaderRegister, registerSpace);\n    }\n\n    static inline void InitAsUnorderedAccessView(\n        _Out_ D3D12_ROOT_PARAMETER &rootParam,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_DESCRIPTOR::Init(rootParam.Descriptor, shaderRegister, registerSpace);\n    }\n    \n    inline void InitAsDescriptorTable(\n        UINT numDescriptorRanges,\n        _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* pDescriptorRanges,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsDescriptorTable(*this, numDescriptorRanges, pDescriptorRanges, visibility);\n    }\n    \n    inline void InitAsConstants(\n        UINT num32BitValues,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsConstants(*this, num32BitValues, shaderRegister, registerSpace, visibility);\n    }\n\n    inline void InitAsConstantBufferView(\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsConstantBufferView(*this, shaderRegister, registerSpace, visibility);\n    }\n\n    inline void InitAsShaderResourceView(\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsShaderResourceView(*this, shaderRegister, registerSpace, visibility);\n    }\n\n    inline void InitAsUnorderedAccessView(\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsUnorderedAccessView(*this, shaderRegister, registerSpace, visibility);\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_STATIC_SAMPLER_DESC : public D3D12_STATIC_SAMPLER_DESC\n{\n    CD3DX12_STATIC_SAMPLER_DESC() = default;\n    explicit CD3DX12_STATIC_SAMPLER_DESC(const D3D12_STATIC_SAMPLER_DESC &o) :\n        D3D12_STATIC_SAMPLER_DESC(o)\n    {}\n    CD3DX12_STATIC_SAMPLER_DESC(\n         UINT shaderRegister,\n         D3D12_FILTER filter = D3D12_FILTER_ANISOTROPIC,\n         D3D12_TEXTURE_ADDRESS_MODE addressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n         D3D12_TEXTURE_ADDRESS_MODE addressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n         D3D12_TEXTURE_ADDRESS_MODE addressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n         FLOAT mipLODBias = 0,\n         UINT maxAnisotropy = 16,\n         D3D12_COMPARISON_FUNC comparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL,\n         D3D12_STATIC_BORDER_COLOR borderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE,\n         FLOAT minLOD = 0.f,\n         FLOAT maxLOD = D3D12_FLOAT32_MAX,\n         D3D12_SHADER_VISIBILITY shaderVisibility = D3D12_SHADER_VISIBILITY_ALL, \n         UINT registerSpace = 0)\n    {\n        Init(\n            shaderRegister,\n            filter,\n            addressU,\n            addressV,\n            addressW,\n            mipLODBias,\n            maxAnisotropy,\n            comparisonFunc,\n            borderColor,\n            minLOD,\n            maxLOD,\n            shaderVisibility,\n            registerSpace);\n    }\n    \n    static inline void Init(\n        _Out_ D3D12_STATIC_SAMPLER_DESC &samplerDesc,\n         UINT shaderRegister,\n         D3D12_FILTER filter = D3D12_FILTER_ANISOTROPIC,\n         D3D12_TEXTURE_ADDRESS_MODE addressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n         D3D12_TEXTURE_ADDRESS_MODE addressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n         D3D12_TEXTURE_ADDRESS_MODE addressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n         FLOAT mipLODBias = 0,\n         UINT maxAnisotropy = 16,\n         D3D12_COMPARISON_FUNC comparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL,\n         D3D12_STATIC_BORDER_COLOR borderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE,\n         FLOAT minLOD = 0.f,\n         FLOAT maxLOD = D3D12_FLOAT32_MAX,\n         D3D12_SHADER_VISIBILITY shaderVisibility = D3D12_SHADER_VISIBILITY_ALL, \n         UINT registerSpace = 0)\n    {\n        samplerDesc.ShaderRegister = shaderRegister;\n        samplerDesc.Filter = filter;\n        samplerDesc.AddressU = addressU;\n        samplerDesc.AddressV = addressV;\n        samplerDesc.AddressW = addressW;\n        samplerDesc.MipLODBias = mipLODBias;\n        samplerDesc.MaxAnisotropy = maxAnisotropy;\n        samplerDesc.ComparisonFunc = comparisonFunc;\n        samplerDesc.BorderColor = borderColor;\n        samplerDesc.MinLOD = minLOD;\n        samplerDesc.MaxLOD = maxLOD;\n        samplerDesc.ShaderVisibility = shaderVisibility;\n        samplerDesc.RegisterSpace = registerSpace;\n    }\n    inline void Init(\n         UINT shaderRegister,\n         D3D12_FILTER filter = D3D12_FILTER_ANISOTROPIC,\n         D3D12_TEXTURE_ADDRESS_MODE addressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n         D3D12_TEXTURE_ADDRESS_MODE addressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n         D3D12_TEXTURE_ADDRESS_MODE addressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP,\n         FLOAT mipLODBias = 0,\n         UINT maxAnisotropy = 16,\n         D3D12_COMPARISON_FUNC comparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL,\n         D3D12_STATIC_BORDER_COLOR borderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE,\n         FLOAT minLOD = 0.f,\n         FLOAT maxLOD = D3D12_FLOAT32_MAX,\n         D3D12_SHADER_VISIBILITY shaderVisibility = D3D12_SHADER_VISIBILITY_ALL, \n         UINT registerSpace = 0)\n    {\n        Init(\n            *this,\n            shaderRegister,\n            filter,\n            addressU,\n            addressV,\n            addressW,\n            mipLODBias,\n            maxAnisotropy,\n            comparisonFunc,\n            borderColor,\n            minLOD,\n            maxLOD,\n            shaderVisibility,\n            registerSpace);\n    }\n    \n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_ROOT_SIGNATURE_DESC : public D3D12_ROOT_SIGNATURE_DESC\n{\n    CD3DX12_ROOT_SIGNATURE_DESC() = default;\n    explicit CD3DX12_ROOT_SIGNATURE_DESC(const D3D12_ROOT_SIGNATURE_DESC &o) :\n        D3D12_ROOT_SIGNATURE_DESC(o)\n    {}\n    CD3DX12_ROOT_SIGNATURE_DESC(\n        UINT numParameters,\n        _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters,\n        UINT numStaticSamplers = 0,\n        _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr,\n        D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)\n    {\n        Init(numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags);\n    }\n    CD3DX12_ROOT_SIGNATURE_DESC(CD3DX12_DEFAULT)\n    {\n        Init(0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE);\n    }\n    \n    inline void Init(\n        UINT numParameters,\n        _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters,\n        UINT numStaticSamplers = 0,\n        _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr,\n        D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)\n    {\n        Init(*this, numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags);\n    }\n\n    static inline void Init(\n        _Out_ D3D12_ROOT_SIGNATURE_DESC &desc,\n        UINT numParameters,\n        _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters,\n        UINT numStaticSamplers = 0,\n        _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr,\n        D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)\n    {\n        desc.NumParameters = numParameters;\n        desc.pParameters = _pParameters;\n        desc.NumStaticSamplers = numStaticSamplers;\n        desc.pStaticSamplers = _pStaticSamplers;\n        desc.Flags = flags;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_DESCRIPTOR_RANGE1 : public D3D12_DESCRIPTOR_RANGE1\n{\n    CD3DX12_DESCRIPTOR_RANGE1() = default;\n    explicit CD3DX12_DESCRIPTOR_RANGE1(const D3D12_DESCRIPTOR_RANGE1 &o) :\n        D3D12_DESCRIPTOR_RANGE1(o)\n    {}\n    CD3DX12_DESCRIPTOR_RANGE1(\n        D3D12_DESCRIPTOR_RANGE_TYPE rangeType,\n        UINT numDescriptors,\n        UINT baseShaderRegister,\n        UINT registerSpace = 0,\n        D3D12_DESCRIPTOR_RANGE_FLAGS flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE,\n        UINT offsetInDescriptorsFromTableStart =\n        D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND)\n    {\n        Init(rangeType, numDescriptors, baseShaderRegister, registerSpace, flags, offsetInDescriptorsFromTableStart);\n    }\n    \n    inline void Init(\n        D3D12_DESCRIPTOR_RANGE_TYPE rangeType,\n        UINT numDescriptors,\n        UINT baseShaderRegister,\n        UINT registerSpace = 0,\n        D3D12_DESCRIPTOR_RANGE_FLAGS flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE,\n        UINT offsetInDescriptorsFromTableStart =\n        D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND)\n    {\n        Init(*this, rangeType, numDescriptors, baseShaderRegister, registerSpace, flags, offsetInDescriptorsFromTableStart);\n    }\n    \n    static inline void Init(\n        _Out_ D3D12_DESCRIPTOR_RANGE1 &range,\n        D3D12_DESCRIPTOR_RANGE_TYPE rangeType,\n        UINT numDescriptors,\n        UINT baseShaderRegister,\n        UINT registerSpace = 0,\n        D3D12_DESCRIPTOR_RANGE_FLAGS flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE,\n        UINT offsetInDescriptorsFromTableStart =\n        D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND)\n    {\n        range.RangeType = rangeType;\n        range.NumDescriptors = numDescriptors;\n        range.BaseShaderRegister = baseShaderRegister;\n        range.RegisterSpace = registerSpace;\n        range.Flags = flags;\n        range.OffsetInDescriptorsFromTableStart = offsetInDescriptorsFromTableStart;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_ROOT_DESCRIPTOR_TABLE1 : public D3D12_ROOT_DESCRIPTOR_TABLE1\n{\n    CD3DX12_ROOT_DESCRIPTOR_TABLE1() = default;\n    explicit CD3DX12_ROOT_DESCRIPTOR_TABLE1(const D3D12_ROOT_DESCRIPTOR_TABLE1 &o) :\n        D3D12_ROOT_DESCRIPTOR_TABLE1(o)\n    {}\n    CD3DX12_ROOT_DESCRIPTOR_TABLE1(\n        UINT numDescriptorRanges,\n        _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* _pDescriptorRanges)\n    {\n        Init(numDescriptorRanges, _pDescriptorRanges);\n    }\n    \n    inline void Init(\n        UINT numDescriptorRanges,\n        _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* _pDescriptorRanges)\n    {\n        Init(*this, numDescriptorRanges, _pDescriptorRanges);\n    }\n    \n    static inline void Init(\n        _Out_ D3D12_ROOT_DESCRIPTOR_TABLE1 &rootDescriptorTable,\n        UINT numDescriptorRanges,\n        _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* _pDescriptorRanges)\n    {\n        rootDescriptorTable.NumDescriptorRanges = numDescriptorRanges;\n        rootDescriptorTable.pDescriptorRanges = _pDescriptorRanges;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_ROOT_DESCRIPTOR1 : public D3D12_ROOT_DESCRIPTOR1\n{\n    CD3DX12_ROOT_DESCRIPTOR1() = default;\n    explicit CD3DX12_ROOT_DESCRIPTOR1(const D3D12_ROOT_DESCRIPTOR1 &o) :\n        D3D12_ROOT_DESCRIPTOR1(o)\n    {}\n    CD3DX12_ROOT_DESCRIPTOR1(\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE)\n    {\n        Init(shaderRegister, registerSpace, flags);\n    }\n    \n    inline void Init(\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE)\n    {\n        Init(*this, shaderRegister, registerSpace, flags);\n    }\n    \n    static inline void Init(\n        _Out_ D3D12_ROOT_DESCRIPTOR1 &table, \n        UINT shaderRegister, \n        UINT registerSpace = 0, \n        D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE)\n    {\n        table.ShaderRegister = shaderRegister;\n        table.RegisterSpace = registerSpace;\n        table.Flags = flags;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_ROOT_PARAMETER1 : public D3D12_ROOT_PARAMETER1\n{\n    CD3DX12_ROOT_PARAMETER1() = default;\n    explicit CD3DX12_ROOT_PARAMETER1(const D3D12_ROOT_PARAMETER1 &o) :\n        D3D12_ROOT_PARAMETER1(o)\n    {}\n    \n    static inline void InitAsDescriptorTable(\n        _Out_ D3D12_ROOT_PARAMETER1 &rootParam,\n        UINT numDescriptorRanges,\n        _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* pDescriptorRanges,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_DESCRIPTOR_TABLE1::Init(rootParam.DescriptorTable, numDescriptorRanges, pDescriptorRanges);\n    }\n\n    static inline void InitAsConstants(\n        _Out_ D3D12_ROOT_PARAMETER1 &rootParam,\n        UINT num32BitValues,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_CONSTANTS::Init(rootParam.Constants, num32BitValues, shaderRegister, registerSpace);\n    }\n\n    static inline void InitAsConstantBufferView(\n        _Out_ D3D12_ROOT_PARAMETER1 &rootParam,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_DESCRIPTOR1::Init(rootParam.Descriptor, shaderRegister, registerSpace, flags);\n    }\n\n    static inline void InitAsShaderResourceView(\n        _Out_ D3D12_ROOT_PARAMETER1 &rootParam,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_DESCRIPTOR1::Init(rootParam.Descriptor, shaderRegister, registerSpace, flags);\n    }\n\n    static inline void InitAsUnorderedAccessView(\n        _Out_ D3D12_ROOT_PARAMETER1 &rootParam,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV;\n        rootParam.ShaderVisibility = visibility;\n        CD3DX12_ROOT_DESCRIPTOR1::Init(rootParam.Descriptor, shaderRegister, registerSpace, flags);\n    }\n    \n    inline void InitAsDescriptorTable(\n        UINT numDescriptorRanges,\n        _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* pDescriptorRanges,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsDescriptorTable(*this, numDescriptorRanges, pDescriptorRanges, visibility);\n    }\n    \n    inline void InitAsConstants(\n        UINT num32BitValues,\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsConstants(*this, num32BitValues, shaderRegister, registerSpace, visibility);\n    }\n\n    inline void InitAsConstantBufferView(\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsConstantBufferView(*this, shaderRegister, registerSpace, flags, visibility);\n    }\n\n    inline void InitAsShaderResourceView(\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsShaderResourceView(*this, shaderRegister, registerSpace, flags, visibility);\n    }\n\n    inline void InitAsUnorderedAccessView(\n        UINT shaderRegister,\n        UINT registerSpace = 0,\n        D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE,\n        D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n    {\n        InitAsUnorderedAccessView(*this, shaderRegister, registerSpace, flags, visibility);\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC : public D3D12_VERSIONED_ROOT_SIGNATURE_DESC\n{\n    CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC() = default;\n    explicit CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC &o) :\n        D3D12_VERSIONED_ROOT_SIGNATURE_DESC(o)\n    {}\n    explicit CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(const D3D12_ROOT_SIGNATURE_DESC &o)\n    {\n        Version = D3D_ROOT_SIGNATURE_VERSION_1_0;\n        Desc_1_0 = o;\n    }\n    explicit CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(const D3D12_ROOT_SIGNATURE_DESC1 &o)\n    {\n        Version = D3D_ROOT_SIGNATURE_VERSION_1_1;\n        Desc_1_1 = o;\n    }\n    CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(\n        UINT numParameters,\n        _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters,\n        UINT numStaticSamplers = 0,\n        _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr,\n        D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)\n    {\n        Init_1_0(numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags);\n    }\n    CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(\n        UINT numParameters,\n        _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER1* _pParameters,\n        UINT numStaticSamplers = 0,\n        _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr,\n        D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)\n    {\n        Init_1_1(numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags);\n    }\n    CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(CD3DX12_DEFAULT)\n    {\n        Init_1_1(0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE);\n    }\n    \n    inline void Init_1_0(\n        UINT numParameters,\n        _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters,\n        UINT numStaticSamplers = 0,\n        _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr,\n        D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)\n    {\n        Init_1_0(*this, numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags);\n    }\n\n    static inline void Init_1_0(\n        _Out_ D3D12_VERSIONED_ROOT_SIGNATURE_DESC &desc,\n        UINT numParameters,\n        _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters,\n        UINT numStaticSamplers = 0,\n        _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr,\n        D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)\n    {\n        desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0;\n        desc.Desc_1_0.NumParameters = numParameters;\n        desc.Desc_1_0.pParameters = _pParameters;\n        desc.Desc_1_0.NumStaticSamplers = numStaticSamplers;\n        desc.Desc_1_0.pStaticSamplers = _pStaticSamplers;\n        desc.Desc_1_0.Flags = flags;\n    }\n\n    inline void Init_1_1(\n        UINT numParameters,\n        _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER1* _pParameters,\n        UINT numStaticSamplers = 0,\n        _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr,\n        D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)\n    {\n        Init_1_1(*this, numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags);\n    }\n\n    static inline void Init_1_1(\n        _Out_ D3D12_VERSIONED_ROOT_SIGNATURE_DESC &desc,\n        UINT numParameters,\n        _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER1* _pParameters,\n        UINT numStaticSamplers = 0,\n        _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr,\n        D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)\n    {\n        desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;\n        desc.Desc_1_1.NumParameters = numParameters;\n        desc.Desc_1_1.pParameters = _pParameters;\n        desc.Desc_1_1.NumStaticSamplers = numStaticSamplers;\n        desc.Desc_1_1.pStaticSamplers = _pStaticSamplers;\n        desc.Desc_1_1.Flags = flags;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_CPU_DESCRIPTOR_HANDLE : public D3D12_CPU_DESCRIPTOR_HANDLE\n{\n    CD3DX12_CPU_DESCRIPTOR_HANDLE() = default;\n    explicit CD3DX12_CPU_DESCRIPTOR_HANDLE(const D3D12_CPU_DESCRIPTOR_HANDLE &o) :\n        D3D12_CPU_DESCRIPTOR_HANDLE(o)\n    {}\n    CD3DX12_CPU_DESCRIPTOR_HANDLE(CD3DX12_DEFAULT) { ptr = 0; }\n    CD3DX12_CPU_DESCRIPTOR_HANDLE(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &other, INT offsetScaledByIncrementSize)\n    {\n        InitOffsetted(other, offsetScaledByIncrementSize);\n    }\n    CD3DX12_CPU_DESCRIPTOR_HANDLE(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &other, INT offsetInDescriptors, UINT descriptorIncrementSize)\n    {\n        InitOffsetted(other, offsetInDescriptors, descriptorIncrementSize);\n    }\n    CD3DX12_CPU_DESCRIPTOR_HANDLE& Offset(INT offsetInDescriptors, UINT descriptorIncrementSize)\n    { \n        ptr += INT64(offsetInDescriptors) * UINT64(descriptorIncrementSize);\n        return *this;\n    }\n    CD3DX12_CPU_DESCRIPTOR_HANDLE& Offset(INT offsetScaledByIncrementSize) \n    { \n        ptr += offsetScaledByIncrementSize;\n        return *this;\n    }\n    bool operator==(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE& other) const\n    {\n        return (ptr == other.ptr);\n    }\n    bool operator!=(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE& other) const\n    {\n        return (ptr != other.ptr);\n    }\n    CD3DX12_CPU_DESCRIPTOR_HANDLE &operator=(const D3D12_CPU_DESCRIPTOR_HANDLE &other)\n    {\n        ptr = other.ptr;\n        return *this;\n    }\n\n    inline void InitOffsetted(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize)\n    {\n        InitOffsetted(*this, base, offsetScaledByIncrementSize);\n    }\n    \n    inline void InitOffsetted(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize)\n    {\n        InitOffsetted(*this, base, offsetInDescriptors, descriptorIncrementSize);\n    }\n    \n    static inline void InitOffsetted(_Out_ D3D12_CPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize)\n    {\n        handle.ptr = base.ptr + offsetScaledByIncrementSize;\n    }\n    \n    static inline void InitOffsetted(_Out_ D3D12_CPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize)\n    {\n        handle.ptr = static_cast<SIZE_T>(base.ptr + INT64(offsetInDescriptors) * UINT64(descriptorIncrementSize));\n    }\n};\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_GPU_DESCRIPTOR_HANDLE : public D3D12_GPU_DESCRIPTOR_HANDLE\n{\n    CD3DX12_GPU_DESCRIPTOR_HANDLE() = default;\n    explicit CD3DX12_GPU_DESCRIPTOR_HANDLE(const D3D12_GPU_DESCRIPTOR_HANDLE &o) :\n        D3D12_GPU_DESCRIPTOR_HANDLE(o)\n    {}\n    CD3DX12_GPU_DESCRIPTOR_HANDLE(CD3DX12_DEFAULT) { ptr = 0; }\n    CD3DX12_GPU_DESCRIPTOR_HANDLE(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &other, INT offsetScaledByIncrementSize)\n    {\n        InitOffsetted(other, offsetScaledByIncrementSize);\n    }\n    CD3DX12_GPU_DESCRIPTOR_HANDLE(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &other, INT offsetInDescriptors, UINT descriptorIncrementSize)\n    {\n        InitOffsetted(other, offsetInDescriptors, descriptorIncrementSize);\n    }\n    CD3DX12_GPU_DESCRIPTOR_HANDLE& Offset(INT offsetInDescriptors, UINT descriptorIncrementSize)\n    { \n        ptr += INT64(offsetInDescriptors) * UINT64(descriptorIncrementSize);\n        return *this;\n    }\n    CD3DX12_GPU_DESCRIPTOR_HANDLE& Offset(INT offsetScaledByIncrementSize) \n    { \n        ptr += offsetScaledByIncrementSize;\n        return *this;\n    }\n    inline bool operator==(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE& other) const\n    {\n        return (ptr == other.ptr);\n    }\n    inline bool operator!=(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE& other) const\n    {\n        return (ptr != other.ptr);\n    }\n    CD3DX12_GPU_DESCRIPTOR_HANDLE &operator=(const D3D12_GPU_DESCRIPTOR_HANDLE &other)\n    {\n        ptr = other.ptr;\n        return *this;\n    }\n\n    inline void InitOffsetted(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize)\n    {\n        InitOffsetted(*this, base, offsetScaledByIncrementSize);\n    }\n    \n    inline void InitOffsetted(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize)\n    {\n        InitOffsetted(*this, base, offsetInDescriptors, descriptorIncrementSize);\n    }\n    \n    static inline void InitOffsetted(_Out_ D3D12_GPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize)\n    {\n        handle.ptr = base.ptr + offsetScaledByIncrementSize;\n    }\n    \n    static inline void InitOffsetted(_Out_ D3D12_GPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize)\n    {\n        handle.ptr = static_cast<UINT64>(base.ptr + INT64(offsetInDescriptors) * UINT64(descriptorIncrementSize));\n    }\n};\n\n//------------------------------------------------------------------------------------------------\ninline UINT D3D12CalcSubresource( UINT MipSlice, UINT ArraySlice, UINT PlaneSlice, UINT MipLevels, UINT ArraySize )\n{ \n    return MipSlice + ArraySlice * MipLevels + PlaneSlice * MipLevels * ArraySize; \n}\n\n//------------------------------------------------------------------------------------------------\ntemplate <typename T, typename U, typename V>\ninline void D3D12DecomposeSubresource( UINT Subresource, UINT MipLevels, UINT ArraySize, _Out_ T& MipSlice, _Out_ U& ArraySlice, _Out_ V& PlaneSlice )\n{\n    MipSlice = static_cast<T>(Subresource % MipLevels);\n    ArraySlice = static_cast<U>((Subresource / MipLevels) % ArraySize);\n    PlaneSlice = static_cast<V>(Subresource / (MipLevels * ArraySize));\n}\n\n//------------------------------------------------------------------------------------------------\ninline UINT8 D3D12GetFormatPlaneCount(\n    _In_ ID3D12Device* pDevice,\n    DXGI_FORMAT Format\n    )\n{\n    D3D12_FEATURE_DATA_FORMAT_INFO formatInfo = { Format, 0 };\n    if (FAILED(pDevice->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, &formatInfo, sizeof(formatInfo))))\n    {\n        return 0;\n    }\n    return formatInfo.PlaneCount;\n}\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_RESOURCE_DESC : public D3D12_RESOURCE_DESC\n{\n    CD3DX12_RESOURCE_DESC() = default;\n    explicit CD3DX12_RESOURCE_DESC( const D3D12_RESOURCE_DESC& o ) :\n        D3D12_RESOURCE_DESC( o )\n    {}\n    CD3DX12_RESOURCE_DESC( \n        D3D12_RESOURCE_DIMENSION dimension,\n        UINT64 alignment,\n        UINT64 width,\n        UINT height,\n        UINT16 depthOrArraySize,\n        UINT16 mipLevels,\n        DXGI_FORMAT format,\n        UINT sampleCount,\n        UINT sampleQuality,\n        D3D12_TEXTURE_LAYOUT layout,\n        D3D12_RESOURCE_FLAGS flags )\n    {\n        Dimension = dimension;\n        Alignment = alignment;\n        Width = width;\n        Height = height;\n        DepthOrArraySize = depthOrArraySize;\n        MipLevels = mipLevels;\n        Format = format;\n        SampleDesc.Count = sampleCount;\n        SampleDesc.Quality = sampleQuality;\n        Layout = layout;\n        Flags = flags;\n    }\n    static inline CD3DX12_RESOURCE_DESC Buffer( \n        const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo,\n        D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE )\n    {\n        return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_BUFFER, resAllocInfo.Alignment, resAllocInfo.SizeInBytes, \n            1, 1, 1, DXGI_FORMAT_UNKNOWN, 1, 0, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, flags );\n    }\n    static inline CD3DX12_RESOURCE_DESC Buffer( \n        UINT64 width,\n        D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE,\n        UINT64 alignment = 0 )\n    {\n        return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_BUFFER, alignment, width, 1, 1, 1, \n            DXGI_FORMAT_UNKNOWN, 1, 0, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, flags );\n    }\n    static inline CD3DX12_RESOURCE_DESC Tex1D( \n        DXGI_FORMAT format,\n        UINT64 width,\n        UINT16 arraySize = 1,\n        UINT16 mipLevels = 0,\n        D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE,\n        D3D12_TEXTURE_LAYOUT layout = D3D12_TEXTURE_LAYOUT_UNKNOWN,\n        UINT64 alignment = 0 )\n    {\n        return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_TEXTURE1D, alignment, width, 1, arraySize, \n            mipLevels, format, 1, 0, layout, flags );\n    }\n    static inline CD3DX12_RESOURCE_DESC Tex2D( \n        DXGI_FORMAT format,\n        UINT64 width,\n        UINT height,\n        UINT16 arraySize = 1,\n        UINT16 mipLevels = 0,\n        UINT sampleCount = 1,\n        UINT sampleQuality = 0,\n        D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE,\n        D3D12_TEXTURE_LAYOUT layout = D3D12_TEXTURE_LAYOUT_UNKNOWN,\n        UINT64 alignment = 0 )\n    {\n        return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_TEXTURE2D, alignment, width, height, arraySize, \n            mipLevels, format, sampleCount, sampleQuality, layout, flags );\n    }\n    static inline CD3DX12_RESOURCE_DESC Tex3D( \n        DXGI_FORMAT format,\n        UINT64 width,\n        UINT height,\n        UINT16 depth,\n        UINT16 mipLevels = 0,\n        D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE,\n        D3D12_TEXTURE_LAYOUT layout = D3D12_TEXTURE_LAYOUT_UNKNOWN,\n        UINT64 alignment = 0 )\n    {\n        return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_TEXTURE3D, alignment, width, height, depth, \n            mipLevels, format, 1, 0, layout, flags );\n    }\n    inline UINT16 Depth() const\n    { return (Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? DepthOrArraySize : 1); }\n    inline UINT16 ArraySize() const\n    { return (Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D ? DepthOrArraySize : 1); }\n    inline UINT8 PlaneCount(_In_ ID3D12Device* pDevice) const\n    { return D3D12GetFormatPlaneCount(pDevice, Format); }\n    inline UINT Subresources(_In_ ID3D12Device* pDevice) const\n    { return MipLevels * ArraySize() * PlaneCount(pDevice); }\n    inline UINT CalcSubresource(UINT MipSlice, UINT ArraySlice, UINT PlaneSlice)\n    { return D3D12CalcSubresource(MipSlice, ArraySlice, PlaneSlice, MipLevels, ArraySize()); }\n};\ninline bool operator==( const D3D12_RESOURCE_DESC& l, const D3D12_RESOURCE_DESC& r )\n{\n    return l.Dimension == r.Dimension &&\n        l.Alignment == r.Alignment &&\n        l.Width == r.Width &&\n        l.Height == r.Height &&\n        l.DepthOrArraySize == r.DepthOrArraySize &&\n        l.MipLevels == r.MipLevels &&\n        l.Format == r.Format &&\n        l.SampleDesc.Count == r.SampleDesc.Count &&\n        l.SampleDesc.Quality == r.SampleDesc.Quality &&\n        l.Layout == r.Layout &&\n        l.Flags == r.Flags;\n}\ninline bool operator!=( const D3D12_RESOURCE_DESC& l, const D3D12_RESOURCE_DESC& r )\n{ return !( l == r ); }\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_VIEW_INSTANCING_DESC : public D3D12_VIEW_INSTANCING_DESC\n{\n    CD3DX12_VIEW_INSTANCING_DESC() = default;\n    explicit CD3DX12_VIEW_INSTANCING_DESC( const D3D12_VIEW_INSTANCING_DESC& o ) :\n        D3D12_VIEW_INSTANCING_DESC( o )\n    {}\n    explicit CD3DX12_VIEW_INSTANCING_DESC( CD3DX12_DEFAULT )\n    {\n        ViewInstanceCount = 0;\n        pViewInstanceLocations = nullptr;\n        Flags = D3D12_VIEW_INSTANCING_FLAG_NONE;\n    }\n    explicit CD3DX12_VIEW_INSTANCING_DESC( \n        UINT InViewInstanceCount,\n        const D3D12_VIEW_INSTANCE_LOCATION* InViewInstanceLocations,\n        D3D12_VIEW_INSTANCING_FLAGS InFlags)\n    {\n        ViewInstanceCount = InViewInstanceCount;\n        pViewInstanceLocations = InViewInstanceLocations;\n        Flags = InFlags;\n    }\n};\n\n//------------------------------------------------------------------------------------------------\n// Row-by-row memcpy\ninline void MemcpySubresource(\n    _In_ const D3D12_MEMCPY_DEST* pDest,\n    _In_ const D3D12_SUBRESOURCE_DATA* pSrc,\n    SIZE_T RowSizeInBytes,\n    UINT NumRows,\n    UINT NumSlices)\n{\n    for (UINT z = 0; z < NumSlices; ++z)\n    {\n        BYTE* pDestSlice = reinterpret_cast<BYTE*>(pDest->pData) + pDest->SlicePitch * z;\n        const BYTE* pSrcSlice = reinterpret_cast<const BYTE*>(pSrc->pData) + pSrc->SlicePitch * z;\n        for (UINT y = 0; y < NumRows; ++y)\n        {\n            memcpy(pDestSlice + pDest->RowPitch * y,\n                   pSrcSlice + pSrc->RowPitch * y,\n                   RowSizeInBytes);\n        }\n    }\n}\n\n//------------------------------------------------------------------------------------------------\n// Returns required size of a buffer to be used for data upload\ninline UINT64 GetRequiredIntermediateSize(\n    _In_ ID3D12Resource* pDestinationResource,\n    _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource,\n    _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources)\n{\n    auto Desc = pDestinationResource->GetDesc();\n    UINT64 RequiredSize = 0;\n    \n    ID3D12Device* pDevice = nullptr;\n    pDestinationResource->GetDevice(__uuidof(*pDevice), reinterpret_cast<void**>(&pDevice));\n    pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, 0, nullptr, nullptr, nullptr, &RequiredSize);\n    pDevice->Release();\n    \n    return RequiredSize;\n}\n\n//------------------------------------------------------------------------------------------------\n// All arrays must be populated (e.g. by calling GetCopyableFootprints)\ninline UINT64 UpdateSubresources(\n    _In_ ID3D12GraphicsCommandList* pCmdList,\n    _In_ ID3D12Resource* pDestinationResource,\n    _In_ ID3D12Resource* pIntermediate,\n    _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource,\n    _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources,\n    UINT64 RequiredSize,\n    _In_reads_(NumSubresources) const D3D12_PLACED_SUBRESOURCE_FOOTPRINT* pLayouts,\n    _In_reads_(NumSubresources) const UINT* pNumRows,\n    _In_reads_(NumSubresources) const UINT64* pRowSizesInBytes,\n    _In_reads_(NumSubresources) const D3D12_SUBRESOURCE_DATA* pSrcData)\n{\n    // Minor validation\n    auto IntermediateDesc = pIntermediate->GetDesc();\n    auto DestinationDesc = pDestinationResource->GetDesc();\n    if (IntermediateDesc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || \n        IntermediateDesc.Width < RequiredSize + pLayouts[0].Offset || \n        RequiredSize > SIZE_T(-1) || \n        (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && \n            (FirstSubresource != 0 || NumSubresources != 1)))\n    {\n        return 0;\n    }\n    \n    BYTE* pData;\n    HRESULT hr = pIntermediate->Map(0, nullptr, reinterpret_cast<void**>(&pData));\n    if (FAILED(hr))\n    {\n        return 0;\n    }\n    \n    for (UINT i = 0; i < NumSubresources; ++i)\n    {\n        if (pRowSizesInBytes[i] > SIZE_T(-1)) return 0;\n        D3D12_MEMCPY_DEST DestData = { pData + pLayouts[i].Offset, pLayouts[i].Footprint.RowPitch, SIZE_T(pLayouts[i].Footprint.RowPitch) * SIZE_T(pNumRows[i]) };\n        MemcpySubresource(&DestData, &pSrcData[i], static_cast<SIZE_T>(pRowSizesInBytes[i]), pNumRows[i], pLayouts[i].Footprint.Depth);\n    }\n    pIntermediate->Unmap(0, nullptr);\n    \n    if (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)\n    {\n        pCmdList->CopyBufferRegion(\n            pDestinationResource, 0, pIntermediate, pLayouts[0].Offset, pLayouts[0].Footprint.Width);\n    }\n    else\n    {\n        for (UINT i = 0; i < NumSubresources; ++i)\n        {\n            CD3DX12_TEXTURE_COPY_LOCATION Dst(pDestinationResource, i + FirstSubresource);\n            CD3DX12_TEXTURE_COPY_LOCATION Src(pIntermediate, pLayouts[i]);\n            pCmdList->CopyTextureRegion(&Dst, 0, 0, 0, &Src, nullptr);\n        }\n    }\n    return RequiredSize;\n}\n\n//------------------------------------------------------------------------------------------------\n// Heap-allocating UpdateSubresources implementation\ninline UINT64 UpdateSubresources( \n    _In_ ID3D12GraphicsCommandList* pCmdList,\n    _In_ ID3D12Resource* pDestinationResource,\n    _In_ ID3D12Resource* pIntermediate,\n    UINT64 IntermediateOffset,\n    _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource,\n    _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources,\n    _In_reads_(NumSubresources) D3D12_SUBRESOURCE_DATA* pSrcData)\n{\n    UINT64 RequiredSize = 0;\n    UINT64 MemToAlloc = static_cast<UINT64>(sizeof(D3D12_PLACED_SUBRESOURCE_FOOTPRINT) + sizeof(UINT) + sizeof(UINT64)) * NumSubresources;\n    if (MemToAlloc > SIZE_MAX)\n    {\n       return 0;\n    }\n    void* pMem = HeapAlloc(GetProcessHeap(), 0, static_cast<SIZE_T>(MemToAlloc));\n    if (pMem == nullptr)\n    {\n       return 0;\n    }\n    auto pLayouts = reinterpret_cast<D3D12_PLACED_SUBRESOURCE_FOOTPRINT*>(pMem);\n    UINT64* pRowSizesInBytes = reinterpret_cast<UINT64*>(pLayouts + NumSubresources);\n    UINT* pNumRows = reinterpret_cast<UINT*>(pRowSizesInBytes + NumSubresources);\n    \n    auto Desc = pDestinationResource->GetDesc();\n    ID3D12Device* pDevice = nullptr;\n    pDestinationResource->GetDevice(__uuidof(*pDevice), reinterpret_cast<void**>(&pDevice));\n    pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, pLayouts, pNumRows, pRowSizesInBytes, &RequiredSize);\n    pDevice->Release();\n    \n    UINT64 Result = UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, pLayouts, pNumRows, pRowSizesInBytes, pSrcData);\n    HeapFree(GetProcessHeap(), 0, pMem);\n    return Result;\n}\n\n//------------------------------------------------------------------------------------------------\n// Stack-allocating UpdateSubresources implementation\ntemplate <UINT MaxSubresources>\ninline UINT64 UpdateSubresources( \n    _In_ ID3D12GraphicsCommandList* pCmdList,\n    _In_ ID3D12Resource* pDestinationResource,\n    _In_ ID3D12Resource* pIntermediate,\n    UINT64 IntermediateOffset,\n    _In_range_(0, MaxSubresources) UINT FirstSubresource,\n    _In_range_(1, MaxSubresources - FirstSubresource) UINT NumSubresources,\n    _In_reads_(NumSubresources) D3D12_SUBRESOURCE_DATA* pSrcData)\n{\n    UINT64 RequiredSize = 0;\n    D3D12_PLACED_SUBRESOURCE_FOOTPRINT Layouts[MaxSubresources];\n    UINT NumRows[MaxSubresources];\n    UINT64 RowSizesInBytes[MaxSubresources];\n    \n    auto Desc = pDestinationResource->GetDesc();\n    ID3D12Device* pDevice = nullptr;\n    pDestinationResource->GetDevice(__uuidof(*pDevice), reinterpret_cast<void**>(&pDevice));\n    pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, Layouts, NumRows, RowSizesInBytes, &RequiredSize);\n    pDevice->Release();\n    \n    return UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, Layouts, NumRows, RowSizesInBytes, pSrcData);\n}\n\n//------------------------------------------------------------------------------------------------\ninline bool D3D12IsLayoutOpaque( D3D12_TEXTURE_LAYOUT Layout )\n{ return Layout == D3D12_TEXTURE_LAYOUT_UNKNOWN || Layout == D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE; }\n\n//------------------------------------------------------------------------------------------------\ntemplate <typename t_CommandListType>\ninline ID3D12CommandList * const * CommandListCast(t_CommandListType * const * pp)\n{\n    // This cast is useful for passing strongly typed command list pointers into\n    // ExecuteCommandLists.\n    // This cast is valid as long as the const-ness is respected. D3D12 APIs do\n    // respect the const-ness of their arguments.\n    return reinterpret_cast<ID3D12CommandList * const *>(pp);\n}\n\n//------------------------------------------------------------------------------------------------\n// D3D12 exports a new method for serializing root signatures in the Windows 10 Anniversary Update.\n// To help enable root signature 1.1 features when they are available and not require maintaining\n// two code paths for building root signatures, this helper method reconstructs a 1.0 signature when\n// 1.1 is not supported.\ninline HRESULT D3DX12SerializeVersionedRootSignature(\n    _In_ const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* pRootSignatureDesc,\n    D3D_ROOT_SIGNATURE_VERSION MaxVersion,\n    _Outptr_ ID3DBlob** ppBlob,\n    _Always_(_Outptr_opt_result_maybenull_) ID3DBlob** ppErrorBlob)\n{\n    if (ppErrorBlob != nullptr)\n    {\n        *ppErrorBlob = nullptr;\n    }\n\n    switch (MaxVersion)\n    {\n        case D3D_ROOT_SIGNATURE_VERSION_1_0:\n            switch (pRootSignatureDesc->Version)\n            {\n                case D3D_ROOT_SIGNATURE_VERSION_1_0:\n                    return D3D12SerializeRootSignature(&pRootSignatureDesc->Desc_1_0, D3D_ROOT_SIGNATURE_VERSION_1, ppBlob, ppErrorBlob);\n\n                case D3D_ROOT_SIGNATURE_VERSION_1_1:\n                {\n                    HRESULT hr = S_OK;\n                    const D3D12_ROOT_SIGNATURE_DESC1& desc_1_1 = pRootSignatureDesc->Desc_1_1;\n\n                    const SIZE_T ParametersSize = sizeof(D3D12_ROOT_PARAMETER) * desc_1_1.NumParameters;\n                    void* pParameters = (ParametersSize > 0) ? HeapAlloc(GetProcessHeap(), 0, ParametersSize) : nullptr;\n                    if (ParametersSize > 0 && pParameters == nullptr)\n                    {\n                        hr = E_OUTOFMEMORY;\n                    }\n                    auto pParameters_1_0 = reinterpret_cast<D3D12_ROOT_PARAMETER*>(pParameters);\n\n                    if (SUCCEEDED(hr))\n                    {\n                        for (UINT n = 0; n < desc_1_1.NumParameters; n++)\n                        {\n                            __analysis_assume(ParametersSize == sizeof(D3D12_ROOT_PARAMETER) * desc_1_1.NumParameters);\n                            pParameters_1_0[n].ParameterType = desc_1_1.pParameters[n].ParameterType;\n                            pParameters_1_0[n].ShaderVisibility = desc_1_1.pParameters[n].ShaderVisibility;\n\n                            switch (desc_1_1.pParameters[n].ParameterType)\n                            {\n                            case D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS:\n                                pParameters_1_0[n].Constants.Num32BitValues = desc_1_1.pParameters[n].Constants.Num32BitValues;\n                                pParameters_1_0[n].Constants.RegisterSpace = desc_1_1.pParameters[n].Constants.RegisterSpace;\n                                pParameters_1_0[n].Constants.ShaderRegister = desc_1_1.pParameters[n].Constants.ShaderRegister;\n                                break;\n\n                            case D3D12_ROOT_PARAMETER_TYPE_CBV:\n                            case D3D12_ROOT_PARAMETER_TYPE_SRV:\n                            case D3D12_ROOT_PARAMETER_TYPE_UAV:\n                                pParameters_1_0[n].Descriptor.RegisterSpace = desc_1_1.pParameters[n].Descriptor.RegisterSpace;\n                                pParameters_1_0[n].Descriptor.ShaderRegister = desc_1_1.pParameters[n].Descriptor.ShaderRegister;\n                                break;\n\n                            case D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE:\n                                const D3D12_ROOT_DESCRIPTOR_TABLE1& table_1_1 = desc_1_1.pParameters[n].DescriptorTable;\n\n                                const SIZE_T DescriptorRangesSize = sizeof(D3D12_DESCRIPTOR_RANGE) * table_1_1.NumDescriptorRanges;\n                                void* pDescriptorRanges = (DescriptorRangesSize > 0 && SUCCEEDED(hr)) ? HeapAlloc(GetProcessHeap(), 0, DescriptorRangesSize) : nullptr;\n                                if (DescriptorRangesSize > 0 && pDescriptorRanges == nullptr)\n                                {\n                                    hr = E_OUTOFMEMORY;\n                                }\n                                auto pDescriptorRanges_1_0 = reinterpret_cast<D3D12_DESCRIPTOR_RANGE*>(pDescriptorRanges);\n\n                                if (SUCCEEDED(hr))\n                                {\n                                    for (UINT x = 0; x < table_1_1.NumDescriptorRanges; x++)\n                                    {\n                                        __analysis_assume(DescriptorRangesSize == sizeof(D3D12_DESCRIPTOR_RANGE) * table_1_1.NumDescriptorRanges);\n                                        pDescriptorRanges_1_0[x].BaseShaderRegister = table_1_1.pDescriptorRanges[x].BaseShaderRegister;\n                                        pDescriptorRanges_1_0[x].NumDescriptors = table_1_1.pDescriptorRanges[x].NumDescriptors;\n                                        pDescriptorRanges_1_0[x].OffsetInDescriptorsFromTableStart = table_1_1.pDescriptorRanges[x].OffsetInDescriptorsFromTableStart;\n                                        pDescriptorRanges_1_0[x].RangeType = table_1_1.pDescriptorRanges[x].RangeType;\n                                        pDescriptorRanges_1_0[x].RegisterSpace = table_1_1.pDescriptorRanges[x].RegisterSpace;\n                                    }\n                                }\n\n                                D3D12_ROOT_DESCRIPTOR_TABLE& table_1_0 = pParameters_1_0[n].DescriptorTable;\n                                table_1_0.NumDescriptorRanges = table_1_1.NumDescriptorRanges;\n                                table_1_0.pDescriptorRanges = pDescriptorRanges_1_0;\n                            }\n                        }\n                    }\n\n                    if (SUCCEEDED(hr))\n                    {\n                        CD3DX12_ROOT_SIGNATURE_DESC desc_1_0(desc_1_1.NumParameters, pParameters_1_0, desc_1_1.NumStaticSamplers, desc_1_1.pStaticSamplers, desc_1_1.Flags);\n                        hr = D3D12SerializeRootSignature(&desc_1_0, D3D_ROOT_SIGNATURE_VERSION_1, ppBlob, ppErrorBlob);\n                    }\n\n                    if (pParameters)\n                    {\n                        for (UINT n = 0; n < desc_1_1.NumParameters; n++)\n                        {\n                            if (desc_1_1.pParameters[n].ParameterType == D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE)\n                            {\n                                HeapFree(GetProcessHeap(), 0, reinterpret_cast<void*>(const_cast<D3D12_DESCRIPTOR_RANGE*>(pParameters_1_0[n].DescriptorTable.pDescriptorRanges)));\n                            }\n                        }\n                        HeapFree(GetProcessHeap(), 0, pParameters);\n                    }\n                    return hr;\n                }\n            }\n            break;\n\n        case D3D_ROOT_SIGNATURE_VERSION_1_1:\n            return D3D12SerializeVersionedRootSignature(pRootSignatureDesc, ppBlob, ppErrorBlob);\n    }\n\n    return E_INVALIDARG;\n}\n\n//------------------------------------------------------------------------------------------------\nstruct CD3DX12_RT_FORMAT_ARRAY : public D3D12_RT_FORMAT_ARRAY\n{\n    CD3DX12_RT_FORMAT_ARRAY() = default;\n    explicit CD3DX12_RT_FORMAT_ARRAY(const D3D12_RT_FORMAT_ARRAY& o)\n        : D3D12_RT_FORMAT_ARRAY(o)\n    {}\n    explicit CD3DX12_RT_FORMAT_ARRAY(_In_reads_(NumFormats) const DXGI_FORMAT* pFormats, UINT NumFormats)\n    {\n        NumRenderTargets = NumFormats;\n        memcpy(RTFormats, pFormats, sizeof(RTFormats));\n        // assumes ARRAY_SIZE(pFormats) == ARRAY_SIZE(RTFormats)\n    }\n};\n\n//------------------------------------------------------------------------------------------------\n// Pipeline State Stream Helpers\n//------------------------------------------------------------------------------------------------\n\n//------------------------------------------------------------------------------------------------\n// Stream Subobjects, i.e. elements of a stream\n\nstruct DefaultSampleMask { operator UINT() { return UINT_MAX; } };\nstruct DefaultSampleDesc { operator DXGI_SAMPLE_DESC() { return DXGI_SAMPLE_DESC{1, 0}; } };\n\n#pragma warning(push)\n#pragma warning(disable : 4324)\ntemplate <typename InnerStructType, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE Type, typename DefaultArg = InnerStructType>\nclass alignas(void*) CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT\n{\nprivate:\n    D3D12_PIPELINE_STATE_SUBOBJECT_TYPE _Type;\n    InnerStructType _Inner;\npublic:\n    CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT() noexcept : _Type(Type), _Inner(DefaultArg()) {}\n    CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT(InnerStructType const& i) : _Type(Type), _Inner(i) {}\n    CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT& operator=(InnerStructType const& i) { _Inner = i; return *this; }\n    operator InnerStructType() const { return _Inner; }\n    operator InnerStructType&() { return _Inner; }\n};\n#pragma warning(pop)\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_PIPELINE_STATE_FLAGS,         D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_FLAGS>                             CD3DX12_PIPELINE_STATE_STREAM_FLAGS;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< UINT,                               D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_NODE_MASK>                         CD3DX12_PIPELINE_STATE_STREAM_NODE_MASK;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< ID3D12RootSignature*,               D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE>                    CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_INPUT_LAYOUT_DESC,            D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT>                      CD3DX12_PIPELINE_STATE_STREAM_INPUT_LAYOUT;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_INDEX_BUFFER_STRIP_CUT_VALUE, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_IB_STRIP_CUT_VALUE>                CD3DX12_PIPELINE_STATE_STREAM_IB_STRIP_CUT_VALUE;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_PRIMITIVE_TOPOLOGY_TYPE,      D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY>                CD3DX12_PIPELINE_STATE_STREAM_PRIMITIVE_TOPOLOGY;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE,              D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS>                                CD3DX12_PIPELINE_STATE_STREAM_VS;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE,              D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_GS>                                CD3DX12_PIPELINE_STATE_STREAM_GS;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_STREAM_OUTPUT_DESC,           D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_STREAM_OUTPUT>                     CD3DX12_PIPELINE_STATE_STREAM_STREAM_OUTPUT;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE,              D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_HS>                                CD3DX12_PIPELINE_STATE_STREAM_HS;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE,              D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DS>                                CD3DX12_PIPELINE_STATE_STREAM_DS;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE,              D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS>                                CD3DX12_PIPELINE_STATE_STREAM_PS;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE,              D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CS>                                CD3DX12_PIPELINE_STATE_STREAM_CS;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_BLEND_DESC,                 D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND,          CD3DX12_DEFAULT>   CD3DX12_PIPELINE_STATE_STREAM_BLEND_DESC;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_DEPTH_STENCIL_DESC,         D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL,  CD3DX12_DEFAULT>   CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_DEPTH_STENCIL_DESC1,        D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1, CD3DX12_DEFAULT>   CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL1;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< DXGI_FORMAT,                        D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT>              CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL_FORMAT;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_RASTERIZER_DESC,            D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER,     CD3DX12_DEFAULT>   CD3DX12_PIPELINE_STATE_STREAM_RASTERIZER;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_RT_FORMAT_ARRAY,              D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS>             CD3DX12_PIPELINE_STATE_STREAM_RENDER_TARGET_FORMATS;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< DXGI_SAMPLE_DESC,                   D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC,    DefaultSampleDesc> CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_DESC;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< UINT,                               D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK,    DefaultSampleMask> CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_MASK;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_CACHED_PIPELINE_STATE,        D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CACHED_PSO>                        CD3DX12_PIPELINE_STATE_STREAM_CACHED_PSO;\ntypedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_VIEW_INSTANCING_DESC,       D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VIEW_INSTANCING, CD3DX12_DEFAULT>  CD3DX12_PIPELINE_STATE_STREAM_VIEW_INSTANCING;\n\n//------------------------------------------------------------------------------------------------\n// Stream Parser Helpers\n\nstruct ID3DX12PipelineParserCallbacks\n{\n    // Subobject Callbacks\n    virtual void FlagsCb(D3D12_PIPELINE_STATE_FLAGS) {}\n    virtual void NodeMaskCb(UINT) {}\n    virtual void RootSignatureCb(ID3D12RootSignature*) {}\n    virtual void InputLayoutCb(const D3D12_INPUT_LAYOUT_DESC&) {}\n    virtual void IBStripCutValueCb(D3D12_INDEX_BUFFER_STRIP_CUT_VALUE) {}\n    virtual void PrimitiveTopologyTypeCb(D3D12_PRIMITIVE_TOPOLOGY_TYPE) {}\n    virtual void VSCb(const D3D12_SHADER_BYTECODE&) {}\n    virtual void GSCb(const D3D12_SHADER_BYTECODE&) {}\n    virtual void StreamOutputCb(const D3D12_STREAM_OUTPUT_DESC&) {}\n    virtual void HSCb(const D3D12_SHADER_BYTECODE&) {}\n    virtual void DSCb(const D3D12_SHADER_BYTECODE&) {}\n    virtual void PSCb(const D3D12_SHADER_BYTECODE&) {}\n    virtual void CSCb(const D3D12_SHADER_BYTECODE&) {}\n    virtual void BlendStateCb(const D3D12_BLEND_DESC&) {}\n    virtual void DepthStencilStateCb(const D3D12_DEPTH_STENCIL_DESC&) {}\n    virtual void DepthStencilState1Cb(const D3D12_DEPTH_STENCIL_DESC1&) {}\n    virtual void DSVFormatCb(DXGI_FORMAT) {}\n    virtual void RasterizerStateCb(const D3D12_RASTERIZER_DESC&) {}\n    virtual void RTVFormatsCb(const D3D12_RT_FORMAT_ARRAY&) {}\n    virtual void SampleDescCb(const DXGI_SAMPLE_DESC&) {}\n    virtual void SampleMaskCb(UINT) {}\n    virtual void ViewInstancingCb(const D3D12_VIEW_INSTANCING_DESC&) {}\n    virtual void CachedPSOCb(const D3D12_CACHED_PIPELINE_STATE&) {}\n\n    // Error Callbacks\n    virtual void ErrorBadInputParameter(UINT /*ParameterIndex*/) {}\n    virtual void ErrorDuplicateSubobject(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE /*DuplicateType*/) {}\n    virtual void ErrorUnknownSubobject(UINT /*UnknownTypeValue*/) {}\n\n    virtual ~ID3DX12PipelineParserCallbacks() = default;\n};\n\n// CD3DX12_PIPELINE_STATE_STREAM1 Works on RS3+ (where there is a new view instancing subobject).  \n// Use CD3DX12_PIPELINE_STATE_STREAM for RS2+ support.\nstruct CD3DX12_PIPELINE_STATE_STREAM1\n{\n    CD3DX12_PIPELINE_STATE_STREAM1() = default;\n    CD3DX12_PIPELINE_STATE_STREAM1(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& Desc)\n        : Flags(Desc.Flags)\n        , NodeMask(Desc.NodeMask)\n        , pRootSignature(Desc.pRootSignature)\n        , InputLayout(Desc.InputLayout)\n        , IBStripCutValue(Desc.IBStripCutValue)\n        , PrimitiveTopologyType(Desc.PrimitiveTopologyType)\n        , VS(Desc.VS)\n        , GS(Desc.GS)\n        , StreamOutput(Desc.StreamOutput)\n        , HS(Desc.HS)\n        , DS(Desc.DS)\n        , PS(Desc.PS)\n        , BlendState(CD3DX12_BLEND_DESC(Desc.BlendState))\n        , DepthStencilState(CD3DX12_DEPTH_STENCIL_DESC1(Desc.DepthStencilState))\n        , DSVFormat(Desc.DSVFormat)\n        , RasterizerState(CD3DX12_RASTERIZER_DESC(Desc.RasterizerState))\n        , RTVFormats(CD3DX12_RT_FORMAT_ARRAY(Desc.RTVFormats, Desc.NumRenderTargets))\n        , SampleDesc(Desc.SampleDesc)\n        , SampleMask(Desc.SampleMask)\n        , CachedPSO(Desc.CachedPSO)\n        , ViewInstancingDesc(CD3DX12_VIEW_INSTANCING_DESC(CD3DX12_DEFAULT()))\n    {}\n    CD3DX12_PIPELINE_STATE_STREAM1(const D3D12_COMPUTE_PIPELINE_STATE_DESC& Desc)\n        : Flags(Desc.Flags)\n        , NodeMask(Desc.NodeMask)\n        , pRootSignature(Desc.pRootSignature)\n        , CS(CD3DX12_SHADER_BYTECODE(Desc.CS))\n        , CachedPSO(Desc.CachedPSO)\n    {\n        static_cast<D3D12_DEPTH_STENCIL_DESC1&>(DepthStencilState).DepthEnable = false;\n    }\n    CD3DX12_PIPELINE_STATE_STREAM_FLAGS Flags;\n    CD3DX12_PIPELINE_STATE_STREAM_NODE_MASK NodeMask;\n    CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE pRootSignature;\n    CD3DX12_PIPELINE_STATE_STREAM_INPUT_LAYOUT InputLayout;\n    CD3DX12_PIPELINE_STATE_STREAM_IB_STRIP_CUT_VALUE IBStripCutValue;\n    CD3DX12_PIPELINE_STATE_STREAM_PRIMITIVE_TOPOLOGY PrimitiveTopologyType;\n    CD3DX12_PIPELINE_STATE_STREAM_VS VS;\n    CD3DX12_PIPELINE_STATE_STREAM_GS GS;\n    CD3DX12_PIPELINE_STATE_STREAM_STREAM_OUTPUT StreamOutput;\n    CD3DX12_PIPELINE_STATE_STREAM_HS HS;\n    CD3DX12_PIPELINE_STATE_STREAM_DS DS;\n    CD3DX12_PIPELINE_STATE_STREAM_PS PS;\n    CD3DX12_PIPELINE_STATE_STREAM_CS CS;\n    CD3DX12_PIPELINE_STATE_STREAM_BLEND_DESC BlendState;\n    CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL1 DepthStencilState;\n    CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL_FORMAT DSVFormat;\n    CD3DX12_PIPELINE_STATE_STREAM_RASTERIZER RasterizerState;\n    CD3DX12_PIPELINE_STATE_STREAM_RENDER_TARGET_FORMATS RTVFormats;\n    CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_DESC SampleDesc;\n    CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_MASK SampleMask;\n    CD3DX12_PIPELINE_STATE_STREAM_CACHED_PSO CachedPSO;\n    CD3DX12_PIPELINE_STATE_STREAM_VIEW_INSTANCING ViewInstancingDesc;\n    D3D12_GRAPHICS_PIPELINE_STATE_DESC GraphicsDescV0() const\n    {\n        D3D12_GRAPHICS_PIPELINE_STATE_DESC D;\n        D.Flags                 = this->Flags;\n        D.NodeMask              = this->NodeMask;\n        D.pRootSignature        = this->pRootSignature;\n        D.InputLayout           = this->InputLayout;\n        D.IBStripCutValue       = this->IBStripCutValue;\n        D.PrimitiveTopologyType = this->PrimitiveTopologyType;\n        D.VS                    = this->VS;\n        D.GS                    = this->GS;\n        D.StreamOutput          = this->StreamOutput;\n        D.HS                    = this->HS;\n        D.DS                    = this->DS;\n        D.PS                    = this->PS;\n        D.BlendState            = this->BlendState;\n        D.DepthStencilState     = CD3DX12_DEPTH_STENCIL_DESC1(D3D12_DEPTH_STENCIL_DESC1(this->DepthStencilState));\n        D.DSVFormat             = this->DSVFormat;\n        D.RasterizerState       = this->RasterizerState;\n        D.NumRenderTargets      = D3D12_RT_FORMAT_ARRAY(this->RTVFormats).NumRenderTargets;\n        memcpy(D.RTVFormats, D3D12_RT_FORMAT_ARRAY(this->RTVFormats).RTFormats, sizeof(D.RTVFormats));\n        D.SampleDesc            = this->SampleDesc;\n        D.SampleMask            = this->SampleMask;\n        D.CachedPSO             = this->CachedPSO;\n        return D;\n    }\n    D3D12_COMPUTE_PIPELINE_STATE_DESC ComputeDescV0() const\n    {\n        D3D12_COMPUTE_PIPELINE_STATE_DESC D;\n        D.Flags                 = this->Flags;\n        D.NodeMask              = this->NodeMask;\n        D.pRootSignature        = this->pRootSignature;\n        D.CS                    = this->CS;\n        D.CachedPSO             = this->CachedPSO;\n        return D;\n    }\n};\n\n// CD3DX12_PIPELINE_STATE_STREAM works on RS2+ but does not support new subobject(s) added in RS3+.\n// See CD3DX12_PIPELINE_STATE_STREAM1 for instance.\nstruct CD3DX12_PIPELINE_STATE_STREAM\n{\n    CD3DX12_PIPELINE_STATE_STREAM() = default;\n    CD3DX12_PIPELINE_STATE_STREAM(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& Desc)\n        : Flags(Desc.Flags)\n        , NodeMask(Desc.NodeMask)\n        , pRootSignature(Desc.pRootSignature)\n        , InputLayout(Desc.InputLayout)\n        , IBStripCutValue(Desc.IBStripCutValue)\n        , PrimitiveTopologyType(Desc.PrimitiveTopologyType)\n        , VS(Desc.VS)\n        , GS(Desc.GS)\n        , StreamOutput(Desc.StreamOutput)\n        , HS(Desc.HS)\n        , DS(Desc.DS)\n        , PS(Desc.PS)\n        , BlendState(CD3DX12_BLEND_DESC(Desc.BlendState))\n        , DepthStencilState(CD3DX12_DEPTH_STENCIL_DESC1(Desc.DepthStencilState))\n        , DSVFormat(Desc.DSVFormat)\n        , RasterizerState(CD3DX12_RASTERIZER_DESC(Desc.RasterizerState))\n        , RTVFormats(CD3DX12_RT_FORMAT_ARRAY(Desc.RTVFormats, Desc.NumRenderTargets))\n        , SampleDesc(Desc.SampleDesc)\n        , SampleMask(Desc.SampleMask)\n        , CachedPSO(Desc.CachedPSO)\n    {}\n    CD3DX12_PIPELINE_STATE_STREAM(const D3D12_COMPUTE_PIPELINE_STATE_DESC& Desc)\n        : Flags(Desc.Flags)\n        , NodeMask(Desc.NodeMask)\n        , pRootSignature(Desc.pRootSignature)\n        , CS(CD3DX12_SHADER_BYTECODE(Desc.CS))\n        , CachedPSO(Desc.CachedPSO)\n    {}\n    CD3DX12_PIPELINE_STATE_STREAM_FLAGS Flags;\n    CD3DX12_PIPELINE_STATE_STREAM_NODE_MASK NodeMask;\n    CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE pRootSignature;\n    CD3DX12_PIPELINE_STATE_STREAM_INPUT_LAYOUT InputLayout;\n    CD3DX12_PIPELINE_STATE_STREAM_IB_STRIP_CUT_VALUE IBStripCutValue;\n    CD3DX12_PIPELINE_STATE_STREAM_PRIMITIVE_TOPOLOGY PrimitiveTopologyType;\n    CD3DX12_PIPELINE_STATE_STREAM_VS VS;\n    CD3DX12_PIPELINE_STATE_STREAM_GS GS;\n    CD3DX12_PIPELINE_STATE_STREAM_STREAM_OUTPUT StreamOutput;\n    CD3DX12_PIPELINE_STATE_STREAM_HS HS;\n    CD3DX12_PIPELINE_STATE_STREAM_DS DS;\n    CD3DX12_PIPELINE_STATE_STREAM_PS PS;\n    CD3DX12_PIPELINE_STATE_STREAM_CS CS;\n    CD3DX12_PIPELINE_STATE_STREAM_BLEND_DESC BlendState;\n    CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL1 DepthStencilState;\n    CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL_FORMAT DSVFormat;\n    CD3DX12_PIPELINE_STATE_STREAM_RASTERIZER RasterizerState;\n    CD3DX12_PIPELINE_STATE_STREAM_RENDER_TARGET_FORMATS RTVFormats;\n    CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_DESC SampleDesc;\n    CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_MASK SampleMask;\n    CD3DX12_PIPELINE_STATE_STREAM_CACHED_PSO CachedPSO;\n    D3D12_GRAPHICS_PIPELINE_STATE_DESC GraphicsDescV0() const\n    {\n        D3D12_GRAPHICS_PIPELINE_STATE_DESC D;\n        D.Flags                 = this->Flags;\n        D.NodeMask              = this->NodeMask;\n        D.pRootSignature        = this->pRootSignature;\n        D.InputLayout           = this->InputLayout;\n        D.IBStripCutValue       = this->IBStripCutValue;\n        D.PrimitiveTopologyType = this->PrimitiveTopologyType;\n        D.VS                    = this->VS;\n        D.GS                    = this->GS;\n        D.StreamOutput          = this->StreamOutput;\n        D.HS                    = this->HS;\n        D.DS                    = this->DS;\n        D.PS                    = this->PS;\n        D.BlendState            = this->BlendState;\n        D.DepthStencilState     = CD3DX12_DEPTH_STENCIL_DESC1(D3D12_DEPTH_STENCIL_DESC1(this->DepthStencilState));\n        D.DSVFormat             = this->DSVFormat;\n        D.RasterizerState       = this->RasterizerState;\n        D.NumRenderTargets      = D3D12_RT_FORMAT_ARRAY(this->RTVFormats).NumRenderTargets;\n        memcpy(D.RTVFormats, D3D12_RT_FORMAT_ARRAY(this->RTVFormats).RTFormats, sizeof(D.RTVFormats));\n        D.SampleDesc            = this->SampleDesc;\n        D.SampleMask            = this->SampleMask;\n        D.CachedPSO             = this->CachedPSO;\n        return D;\n    }\n    D3D12_COMPUTE_PIPELINE_STATE_DESC ComputeDescV0() const\n    {\n        D3D12_COMPUTE_PIPELINE_STATE_DESC D;\n        D.Flags                 = this->Flags;\n        D.NodeMask              = this->NodeMask;\n        D.pRootSignature        = this->pRootSignature;\n        D.CS                    = this->CS;\n        D.CachedPSO             = this->CachedPSO;\n        return D;\n    }\n};\n\nstruct CD3DX12_PIPELINE_STATE_STREAM_PARSE_HELPER : public ID3DX12PipelineParserCallbacks\n{\n    CD3DX12_PIPELINE_STATE_STREAM1 PipelineStream;\n    CD3DX12_PIPELINE_STATE_STREAM_PARSE_HELPER() noexcept\n        : SeenDSS(false)\n    {\n        // Adjust defaults to account for absent members.\n        PipelineStream.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;\n\n        // Depth disabled if no DSV format specified.\n        static_cast<D3D12_DEPTH_STENCIL_DESC1&>(PipelineStream.DepthStencilState).DepthEnable = false;\n    }\n\n    // ID3DX12PipelineParserCallbacks\n    void FlagsCb(D3D12_PIPELINE_STATE_FLAGS Flags) override {PipelineStream.Flags = Flags;}\n    void NodeMaskCb(UINT NodeMask) override {PipelineStream.NodeMask = NodeMask;}\n    void RootSignatureCb(ID3D12RootSignature* pRootSignature) override {PipelineStream.pRootSignature = pRootSignature;}\n    void InputLayoutCb(const D3D12_INPUT_LAYOUT_DESC& InputLayout) override {PipelineStream.InputLayout = InputLayout;}\n    void IBStripCutValueCb(D3D12_INDEX_BUFFER_STRIP_CUT_VALUE IBStripCutValue) override {PipelineStream.IBStripCutValue = IBStripCutValue;}\n    void PrimitiveTopologyTypeCb(D3D12_PRIMITIVE_TOPOLOGY_TYPE PrimitiveTopologyType) override {PipelineStream.PrimitiveTopologyType = PrimitiveTopologyType;}\n    void VSCb(const D3D12_SHADER_BYTECODE& VS) override {PipelineStream.VS = VS;}\n    void GSCb(const D3D12_SHADER_BYTECODE& GS) override {PipelineStream.GS = GS;}\n    void StreamOutputCb(const D3D12_STREAM_OUTPUT_DESC& StreamOutput) override {PipelineStream.StreamOutput = StreamOutput;}\n    void HSCb(const D3D12_SHADER_BYTECODE& HS) override {PipelineStream.HS = HS;}\n    void DSCb(const D3D12_SHADER_BYTECODE& DS) override {PipelineStream.DS = DS;}\n    void PSCb(const D3D12_SHADER_BYTECODE& PS) override {PipelineStream.PS = PS;}\n    void CSCb(const D3D12_SHADER_BYTECODE& CS) override {PipelineStream.CS = CS;}\n    void BlendStateCb(const D3D12_BLEND_DESC& BlendState) override {PipelineStream.BlendState = CD3DX12_BLEND_DESC(BlendState);}\n    void DepthStencilStateCb(const D3D12_DEPTH_STENCIL_DESC& DepthStencilState) override\n    {\n        PipelineStream.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC1(DepthStencilState);\n        SeenDSS = true;\n    }\n    void DepthStencilState1Cb(const D3D12_DEPTH_STENCIL_DESC1& DepthStencilState) override\n    {\n        PipelineStream.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC1(DepthStencilState);\n        SeenDSS = true;\n    }\n    void DSVFormatCb(DXGI_FORMAT DSVFormat) override\n    {\n        PipelineStream.DSVFormat = DSVFormat;\n        if (!SeenDSS && DSVFormat != DXGI_FORMAT_UNKNOWN)\n        {\n            // Re-enable depth for the default state.\n            static_cast<D3D12_DEPTH_STENCIL_DESC1&>(PipelineStream.DepthStencilState).DepthEnable = true;\n        }\n    }\n    void RasterizerStateCb(const D3D12_RASTERIZER_DESC& RasterizerState) override {PipelineStream.RasterizerState = CD3DX12_RASTERIZER_DESC(RasterizerState);}\n    void RTVFormatsCb(const D3D12_RT_FORMAT_ARRAY& RTVFormats) override {PipelineStream.RTVFormats = RTVFormats;}\n    void SampleDescCb(const DXGI_SAMPLE_DESC& SampleDesc) override {PipelineStream.SampleDesc = SampleDesc;}\n    void SampleMaskCb(UINT SampleMask) override {PipelineStream.SampleMask = SampleMask;}\n    void ViewInstancingCb(const D3D12_VIEW_INSTANCING_DESC& ViewInstancingDesc) override {PipelineStream.ViewInstancingDesc = CD3DX12_VIEW_INSTANCING_DESC(ViewInstancingDesc);}\n    void CachedPSOCb(const D3D12_CACHED_PIPELINE_STATE& CachedPSO) override {PipelineStream.CachedPSO = CachedPSO;}\n\nprivate:\n    bool SeenDSS;\n};\n\ninline D3D12_PIPELINE_STATE_SUBOBJECT_TYPE D3DX12GetBaseSubobjectType(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE SubobjectType)\n{\n    switch (SubobjectType)\n    {\n    case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1: \n        return D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL;\n    default:\n        return SubobjectType;\n    }\n}\n\ninline HRESULT D3DX12ParsePipelineStream(const D3D12_PIPELINE_STATE_STREAM_DESC& Desc, ID3DX12PipelineParserCallbacks* pCallbacks)\n{\n    if (pCallbacks == nullptr)\n    {\n        return E_INVALIDARG;\n    }\n\n    if (Desc.SizeInBytes == 0 || Desc.pPipelineStateSubobjectStream == nullptr)\n    {\n        pCallbacks->ErrorBadInputParameter(1); // first parameter issue\n        return E_INVALIDARG;\n    }\n\n    bool SubobjectSeen[D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MAX_VALID] = {};\n    for (SIZE_T CurOffset = 0, SizeOfSubobject = 0; CurOffset < Desc.SizeInBytes; CurOffset += SizeOfSubobject)\n    {\n        BYTE* pStream = static_cast<BYTE*>(Desc.pPipelineStateSubobjectStream)+CurOffset;\n        auto SubobjectType = *reinterpret_cast<D3D12_PIPELINE_STATE_SUBOBJECT_TYPE*>(pStream);\n        if (SubobjectType < 0 || SubobjectType >= D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MAX_VALID)\n        {\n            pCallbacks->ErrorUnknownSubobject(SubobjectType);\n            return E_INVALIDARG;\n        } \n        if (SubobjectSeen[D3DX12GetBaseSubobjectType(SubobjectType)])\n        {\n            pCallbacks->ErrorDuplicateSubobject(SubobjectType);\n            return E_INVALIDARG; // disallow subobject duplicates in a stream\n        }\n        SubobjectSeen[SubobjectType] = true;\n        switch (SubobjectType)\n        {\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE: \n            pCallbacks->RootSignatureCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::pRootSignature)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::pRootSignature);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS:\n            pCallbacks->VSCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::VS)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::VS);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS: \n            pCallbacks->PSCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::PS)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::PS);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DS: \n            pCallbacks->DSCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::DS)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::DS);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_HS: \n            pCallbacks->HSCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::HS)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::HS);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_GS: \n            pCallbacks->GSCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::GS)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::GS);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CS:\n            pCallbacks->CSCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::CS)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::CS);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_STREAM_OUTPUT: \n            pCallbacks->StreamOutputCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::StreamOutput)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::StreamOutput);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND: \n            pCallbacks->BlendStateCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::BlendState)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::BlendState);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK: \n            pCallbacks->SampleMaskCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::SampleMask)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::SampleMask);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER: \n            pCallbacks->RasterizerStateCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::RasterizerState)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::RasterizerState);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL: \n            pCallbacks->DepthStencilStateCb(*reinterpret_cast<CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1: \n            pCallbacks->DepthStencilState1Cb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::DepthStencilState)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::DepthStencilState);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT: \n            pCallbacks->InputLayoutCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::InputLayout)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::InputLayout);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_IB_STRIP_CUT_VALUE: \n            pCallbacks->IBStripCutValueCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::IBStripCutValue)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::IBStripCutValue);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY: \n            pCallbacks->PrimitiveTopologyTypeCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::PrimitiveTopologyType)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::PrimitiveTopologyType);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS: \n            pCallbacks->RTVFormatsCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::RTVFormats)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::RTVFormats);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT: \n            pCallbacks->DSVFormatCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::DSVFormat)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::DSVFormat);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC: \n            pCallbacks->SampleDescCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::SampleDesc)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::SampleDesc);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_NODE_MASK: \n            pCallbacks->NodeMaskCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::NodeMask)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::NodeMask);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CACHED_PSO: \n            pCallbacks->CachedPSOCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::CachedPSO)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::CachedPSO);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_FLAGS:\n            pCallbacks->FlagsCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM::Flags)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::Flags);\n            break;\n        case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VIEW_INSTANCING:\n            pCallbacks->ViewInstancingCb(*reinterpret_cast<decltype(CD3DX12_PIPELINE_STATE_STREAM1::ViewInstancingDesc)*>(pStream));\n            SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM1::ViewInstancingDesc);\n            break;\n        default:\n            pCallbacks->ErrorUnknownSubobject(SubobjectType);\n            return E_INVALIDARG;\n            break;\n        }\n    }\n\n    return S_OK;\n}\n\n//------------------------------------------------------------------------------------------------\ninline bool operator==( const D3D12_CLEAR_VALUE &a, const D3D12_CLEAR_VALUE &b)\n{\n    if (a.Format != b.Format) return false;\n    if (a.Format == DXGI_FORMAT_D24_UNORM_S8_UINT\n     || a.Format == DXGI_FORMAT_D16_UNORM\n     || a.Format == DXGI_FORMAT_D32_FLOAT\n     || a.Format == DXGI_FORMAT_D32_FLOAT_S8X24_UINT)\n    {\n        return (a.DepthStencil.Depth == b.DepthStencil.Depth) && \n          (a.DepthStencil.Stencil == b.DepthStencil.Stencil);\n    } else {\n        return (a.Color[0] == b.Color[0]) && \n               (a.Color[1] == b.Color[1]) && \n               (a.Color[2] == b.Color[2]) && \n               (a.Color[3] == b.Color[3]);\n    }\n}\ninline bool operator==( const D3D12_RENDER_PASS_BEGINNING_ACCESS_CLEAR_PARAMETERS &a, const D3D12_RENDER_PASS_BEGINNING_ACCESS_CLEAR_PARAMETERS &b)\n{\n    return a.ClearValue == b.ClearValue;\n}\ninline bool operator==( const D3D12_RENDER_PASS_ENDING_ACCESS_RESOLVE_PARAMETERS &a, const D3D12_RENDER_PASS_ENDING_ACCESS_RESOLVE_PARAMETERS &b)\n{\n    if (a.pSrcResource != b.pSrcResource) return false;\n    if (a.pDstResource != b.pDstResource) return false;\n    if (a.SubresourceCount != b.SubresourceCount) return false;\n    if (a.Format != b.Format) return false;\n    if (a.ResolveMode != b.ResolveMode) return false; \n    if (a.PreserveResolveSource != b.PreserveResolveSource) return false; \n    return true;\n}\ninline bool operator==( const D3D12_RENDER_PASS_BEGINNING_ACCESS &a, const D3D12_RENDER_PASS_BEGINNING_ACCESS &b)\n{\n    if (a.Type != b.Type) return false;\n    if (a.Type == D3D12_RENDER_PASS_BEGINNING_ACCESS_TYPE_CLEAR && !(a.Clear == b.Clear)) return false; \n    return true;\n}\ninline bool operator==( const D3D12_RENDER_PASS_ENDING_ACCESS &a, const D3D12_RENDER_PASS_ENDING_ACCESS &b)\n{\n    if (a.Type != b.Type) return false;\n    if (a.Type == D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_RESOLVE && !(a.Resolve == b.Resolve)) return false; \n    return true;\n}\ninline bool operator==( const D3D12_RENDER_PASS_RENDER_TARGET_DESC &a, const D3D12_RENDER_PASS_RENDER_TARGET_DESC &b)\n{\n    if (a.cpuDescriptor.ptr != b.cpuDescriptor.ptr) return false;\n    if (!(a.BeginningAccess == b.BeginningAccess)) return false;\n    if (!(a.EndingAccess == b.EndingAccess)) return false;\n    return true;\n}\ninline bool operator==( const D3D12_RENDER_PASS_DEPTH_STENCIL_DESC &a, const D3D12_RENDER_PASS_DEPTH_STENCIL_DESC &b)\n{\n    if (a.cpuDescriptor.ptr != b.cpuDescriptor.ptr) return false;\n    if (!(a.DepthBeginningAccess == b.DepthBeginningAccess)) return false;\n    if (!(a.StencilBeginningAccess == b.DepthBeginningAccess)) return false;\n    if (!(a.DepthEndingAccess == b.DepthEndingAccess)) return false;\n    if (!(a.StencilEndingAccess == b.StencilEndingAccess)) return false;\n    return true;\n}\n\n\n#ifndef D3DX12_NO_STATE_OBJECT_HELPERS\n\n//================================================================================================\n// D3DX12 State Object Creation Helpers\n// \n// Helper classes for creating new style state objects out of an arbitrary set of subobjects.\n// Uses STL\n//\n// Start by instantiating CD3DX12_STATE_OBJECT_DESC (see it's public methods).\n// One of its methods is CreateSubobject(), which has a comment showing a couple of options for\n// defining subobjects using the helper classes for each subobject (CD3DX12_DXIL_LIBRARY_SUBOBJECT \n// etc.). The subobject helpers each have methods specific to the subobject for configuring it's \n// contents.\n// \n//================================================================================================\n#include <list>\n#include <vector>\n#include <string>\n#include <memory>\n#include <wrl/client.h>\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_STATE_OBJECT_DESC\n{\npublic:\n    CD3DX12_STATE_OBJECT_DESC()\n    {\n        Init(D3D12_STATE_OBJECT_TYPE_COLLECTION);\n    }\n\t~CD3DX12_STATE_OBJECT_DESC()\n\t{\n\t}\n    CD3DX12_STATE_OBJECT_DESC(D3D12_STATE_OBJECT_TYPE Type)\n    {\n        Init(Type);\n    }\n    void SetStateObjectType(D3D12_STATE_OBJECT_TYPE Type) { m_Desc.Type = Type; }\n    operator const D3D12_STATE_OBJECT_DESC&()\n    {\n        // Do final preparation work\n        m_RepointedAssociations.clear();\n        m_SubobjectArray.clear();\n        m_SubobjectArray.reserve(m_Desc.NumSubobjects);\n        // Flatten subobjects into an array (each flattened subobject still has a \n        // member that's a pointer to it's desc that's not flattened)\n        for (auto Iter = m_SubobjectList.begin();\n            Iter != m_SubobjectList.end(); Iter++)\n        {\n            m_SubobjectArray.push_back(*Iter);\n            // Store new location in array so we can redirect pointers contained in subobjects \n            Iter->pSubobjectArrayLocation = &m_SubobjectArray.back(); \n        }\n        // For subobjects with pointer fields, create a new copy of those subobject definitions\n        // with fixed pointers\n        for (UINT i = 0; i < m_Desc.NumSubobjects; i++)\n        {\n            if (m_SubobjectArray[i].Type == D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION)\n            {\n                auto pOriginalSubobjectAssociation = \n                    reinterpret_cast<const D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION*>(m_SubobjectArray[i].pDesc);\n                D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION Repointed = *pOriginalSubobjectAssociation;\n                auto pWrapper = \n                    static_cast<const SUBOBJECT_WRAPPER*>(pOriginalSubobjectAssociation->pSubobjectToAssociate);\n                Repointed.pSubobjectToAssociate = pWrapper->pSubobjectArrayLocation;\n                m_RepointedAssociations.push_back(Repointed);\n                m_SubobjectArray[i].pDesc = &m_RepointedAssociations.back();\n            }\n        }\n        // Below: using ugly way to get pointer in case .data() is not defined\n        m_Desc.pSubobjects = m_Desc.NumSubobjects ? &m_SubobjectArray[0] : nullptr; \n        return m_Desc;\n    }\n    operator const D3D12_STATE_OBJECT_DESC*()\n    {\n        // Cast calls the above final preparation work\n        return &static_cast<const D3D12_STATE_OBJECT_DESC&>(*this);\n    }\n\n    // CreateSubobject creates a sububject helper (e.g. CD3DX12_HIT_GROUP_SUBOBJECT) \n    // whose lifetime is owned by this class.\n    // e.g. \n    // \n    //    CD3DX12_STATE_OBJECT_DESC Collection1(D3D12_STATE_OBJECT_TYPE_COLLECTION);\n    //    auto Lib0 = Collection1.CreateSubobject<CD3DX12_DXIL_LIBRARY_SUBOBJECT>();\n    //    Lib0->SetDXILLibrary(&pMyAppDxilLibs[0]);\n    //    Lib0->DefineExport(L\"rayGenShader0\"); // in practice these export listings might be \n    //                                          // data/engine driven\n    //    etc.\n    //\n    // Alternatively, users can instantiate sububject helpers explicitly, such as via local \n    // variables instead, passing the state object desc that should point to it into the helper \n    // constructor (or call mySubobjectHelper.AddToStateObject(Collection1)).  \n    // In this alternative scenario, the user must keep the subobject alive as long as the state \n    // object it is associated with is alive, else it's pointer references will be stale.\n    // e.g.\n    //\n    //    CD3DX12_STATE_OBJECT_DESC RaytracingState2(D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE);\n    //    CD3DX12_DXIL_LIBRARY_SUBOBJECT LibA(RaytracingState2);\n    //    LibA.SetDXILLibrary(&pMyAppDxilLibs[4]); // not manually specifying exports \n    //                                             // - meaning all exports in the libraries \n    //                                             // are exported\n    //    etc.\n\n    template<typename T>\n    T* CreateSubobject()\n    {\n        T* pSubobject = new T(*this);\n        m_OwnedSubobjectHelpers.emplace_back(pSubobject);\n        return pSubobject;\n    }\n\nprivate:\n    D3D12_STATE_SUBOBJECT* TrackSubobject(D3D12_STATE_SUBOBJECT_TYPE Type, void* pDesc)\n    {\n        SUBOBJECT_WRAPPER Subobject;\n        Subobject.pSubobjectArrayLocation = nullptr;\n        Subobject.Type = Type;\n        Subobject.pDesc = pDesc;\n        m_SubobjectList.push_back(Subobject);\n        m_Desc.NumSubobjects++;\n        return &m_SubobjectList.back();\n    }\n    void Init(D3D12_STATE_OBJECT_TYPE Type)\n    {\n        SetStateObjectType(Type);\n        m_Desc.pSubobjects = nullptr;\n        m_Desc.NumSubobjects = 0;\n        m_SubobjectList.clear();\n        m_SubobjectArray.clear();\n        m_RepointedAssociations.clear();\n    }\n    typedef struct SUBOBJECT_WRAPPER : public D3D12_STATE_SUBOBJECT\n    {\n        D3D12_STATE_SUBOBJECT* pSubobjectArrayLocation; // new location when flattened into array \n                                                        // for repointing pointers in subobjects\n    } SUBOBJECT_WRAPPER;\n    D3D12_STATE_OBJECT_DESC m_Desc;\n    std::list<SUBOBJECT_WRAPPER>   m_SubobjectList; // Pointers to list nodes handed out so \n                                                    // these can be edited live\n    std::vector<D3D12_STATE_SUBOBJECT> m_SubobjectArray; // Built at the end, copying list contents\n\n    std::list<D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION> \n            m_RepointedAssociations; // subobject type that contains pointers to other subobjects, \n                                     // repointed to flattened array\n\n    class StringContainer\n    {\n    public:\n        LPCWSTR LocalCopy(LPCWSTR string, bool bSingleString = false)\n        {\n            if (string)\n            {\n                if (bSingleString)\n                {\n                    m_Strings.clear();\n                    m_Strings.push_back(string);\n                }\n                else\n                {\n                    m_Strings.push_back(string);\n                }\n                return m_Strings.back().c_str();\n            }\n            else\n            {\n                return nullptr;\n            }\n        }\n        void clear() { m_Strings.clear(); }\n    private:\n        std::list<std::wstring> m_Strings;\n    };\n\n    class SUBOBJECT_HELPER_BASE\n    {\n    public:\n        SUBOBJECT_HELPER_BASE() { Init(); };\n        virtual ~SUBOBJECT_HELPER_BASE() {};\n        virtual D3D12_STATE_SUBOBJECT_TYPE Type() const = 0;\n        void AddToStateObject(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n        {\n            m_pSubobject = ContainingStateObject.TrackSubobject(Type(), Data());\n        }\n    protected:\n        virtual void* Data() = 0;\n        void Init() { m_pSubobject = nullptr; }\n        D3D12_STATE_SUBOBJECT* m_pSubobject;\n    };\n\n#if(__cplusplus >= 201103L)\n\t// FIXME: This would break with the current impl\n    std::list<std::unique_ptr<const SUBOBJECT_HELPER_BASE>> m_OwnedSubobjectHelpers;\n#else\n    class OWNED_HELPER\n    {\n    public:\n        OWNED_HELPER(const SUBOBJECT_HELPER_BASE* pHelper) { m_pHelper = pHelper; }\n        ~OWNED_HELPER() { /*delete m_pHelper;*/ }\n        const SUBOBJECT_HELPER_BASE* m_pHelper;\n    };\n\n    std::list<OWNED_HELPER> m_OwnedSubobjectHelpers;\n\tpublic:\n\tvoid DeleteHelpers()\n\t{\n\t\tfor (auto& it : m_OwnedSubobjectHelpers)\n\t\t{\n\t\t\tdelete it.m_pHelper;\n\t\t}\n\t}\n\tprivate:\n#endif\n\n    friend class CD3DX12_DXIL_LIBRARY_SUBOBJECT;\n    friend class CD3DX12_EXISTING_COLLECTION_SUBOBJECT;\n    friend class CD3DX12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT;\n    friend class CD3DX12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION;\n    friend class CD3DX12_HIT_GROUP_SUBOBJECT;\n    friend class CD3DX12_RAYTRACING_SHADER_CONFIG_SUBOBJECT;\n    friend class CD3DX12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT;\n    friend class CD3DX12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT;\n    friend class CD3DX12_LOCAL_ROOT_SIGNATURE_SUBOBJECT;\n    friend class CD3DX12_STATE_OBJECT_CONFIG_SUBOBJECT;\n    friend class CD3DX12_NODE_MASK_SUBOBJECT;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_DXIL_LIBRARY_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_DXIL_LIBRARY_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_DXIL_LIBRARY_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void SetDXILLibrary(D3D12_SHADER_BYTECODE*pCode) \n    { \n        static const D3D12_SHADER_BYTECODE Default = {}; \n        m_Desc.DXILLibrary = pCode ? *pCode : Default; \n    }\n    void DefineExport(\n        LPCWSTR Name, \n        LPCWSTR ExportToRename = nullptr, \n        D3D12_EXPORT_FLAGS Flags = D3D12_EXPORT_FLAG_NONE)\n    {\n        D3D12_EXPORT_DESC Export;\n        Export.Name = m_Strings.LocalCopy(Name);\n        Export.ExportToRename = m_Strings.LocalCopy(ExportToRename);\n        Export.Flags = Flags;\n        m_Exports.push_back(Export);\n        m_Desc.pExports = &m_Exports[0];  // using ugly way to get pointer in case .data() is not defined\n        m_Desc.NumExports = static_cast<UINT>(m_Exports.size());\n    }\n    template<size_t N>\n    void DefineExports(LPCWSTR(&Exports)[N])\n    {\n        for (UINT i = 0; i < N; i++)\n        {\n            DefineExport(Exports[i]);\n        }\n    }\n    void DefineExports(LPCWSTR* Exports, UINT N)\n    {\n        for (UINT i = 0; i < N; i++)\n        {\n            DefineExport(Exports[i]);\n        }\n    }    \n    D3D12_STATE_SUBOBJECT_TYPE Type() const \n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_DXIL_LIBRARY; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator const D3D12_DXIL_LIBRARY_DESC&() const { return m_Desc; }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_Desc = {};\n        m_Strings.clear();\n        m_Exports.clear();\n    }\n    void* Data() { return &m_Desc; }\n    D3D12_DXIL_LIBRARY_DESC m_Desc;\n    CD3DX12_STATE_OBJECT_DESC::StringContainer m_Strings;\n    std::vector<D3D12_EXPORT_DESC> m_Exports;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_EXISTING_COLLECTION_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_EXISTING_COLLECTION_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_EXISTING_COLLECTION_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void SetExistingCollection(ID3D12StateObject*pExistingCollection) \n    { \n        m_Desc.pExistingCollection = pExistingCollection; \n        m_CollectionRef = pExistingCollection; \n    }\n    void DefineExport(\n        LPCWSTR Name, \n        LPCWSTR ExportToRename = nullptr, \n        D3D12_EXPORT_FLAGS Flags = D3D12_EXPORT_FLAG_NONE)\n    {\n        D3D12_EXPORT_DESC Export;\n        Export.Name = m_Strings.LocalCopy(Name);\n        Export.ExportToRename = m_Strings.LocalCopy(ExportToRename);\n        Export.Flags = Flags;\n        m_Exports.push_back(Export);\n        m_Desc.pExports = &m_Exports[0]; // using ugly way to get pointer in case .data() is not defined\n        m_Desc.NumExports = static_cast<UINT>(m_Exports.size());\n    }\n    template<size_t N>\n    void DefineExports(LPCWSTR(&Exports)[N])\n    {\n        for (UINT i = 0; i < N; i++)\n        {\n            DefineExport(Exports[i]);\n        }\n    }\n    void DefineExports(LPCWSTR* Exports, UINT N)\n    {\n        for (UINT i = 0; i < N; i++)\n        {\n            DefineExport(Exports[i]);\n        }\n    }    \n    D3D12_STATE_SUBOBJECT_TYPE Type() const\n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_EXISTING_COLLECTION; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator const D3D12_EXISTING_COLLECTION_DESC&() const { return m_Desc; }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_Desc = {};\n        m_CollectionRef = nullptr;\n        m_Strings.clear();\n        m_Exports.clear();\n    }\n    void* Data() { return &m_Desc; }\n    D3D12_EXISTING_COLLECTION_DESC m_Desc;\n    Microsoft::WRL::ComPtr<ID3D12StateObject> m_CollectionRef;\n    CD3DX12_STATE_OBJECT_DESC::StringContainer m_Strings;\n    std::vector<D3D12_EXPORT_DESC> m_Exports;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void SetSubobjectToAssociate(const D3D12_STATE_SUBOBJECT& SubobjectToAssociate) \n    { \n        m_Desc.pSubobjectToAssociate = &SubobjectToAssociate; \n    }\n    void AddExport(LPCWSTR Export)\n    {\n        m_Desc.NumExports++;\n        m_Exports.push_back(m_Strings.LocalCopy(Export));\n        m_Desc.pExports = &m_Exports[0];  // using ugly way to get pointer in case .data() is not defined\n    }\n    template<size_t N>\n    void AddExports(LPCWSTR (&Exports)[N])\n    {\n        for (UINT i = 0; i < N; i++)\n        {\n            AddExport(Exports[i]);\n        }\n    }\n    void AddExports(LPCWSTR* Exports, UINT N)\n    {\n        for (UINT i = 0; i < N; i++)\n        {\n            AddExport(Exports[i]);\n        }\n    }    \n    D3D12_STATE_SUBOBJECT_TYPE Type() const \n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator const D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION&() const { return m_Desc; }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_Desc = {};\n        m_Strings.clear();\n        m_Exports.clear();\n    }\n    void* Data() { return &m_Desc; }\n    D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION m_Desc;\n    CD3DX12_STATE_OBJECT_DESC::StringContainer m_Strings;\n    std::vector<LPCWSTR> m_Exports;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION()\n    {\n        Init();\n    }\n    CD3DX12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void SetSubobjectNameToAssociate(LPCWSTR SubobjectToAssociate) \n    {\n        m_Desc.SubobjectToAssociate = m_SubobjectName.LocalCopy(SubobjectToAssociate, true); \n    }\n    void AddExport(LPCWSTR Export)\n    {\n        m_Desc.NumExports++;\n        m_Exports.push_back(m_Strings.LocalCopy(Export));\n        m_Desc.pExports = &m_Exports[0];  // using ugly way to get pointer in case .data() is not defined\n    }\n    template<size_t N>\n    void AddExports(LPCWSTR (&Exports)[N])\n    {\n        for (UINT i = 0; i < N; i++)\n        {\n            AddExport(Exports[i]);\n        }\n    }\n    void AddExports(LPCWSTR* Exports, UINT N)\n    {\n        for (UINT i = 0; i < N; i++)\n        {\n            AddExport(Exports[i]);\n        }\n    }    \n    D3D12_STATE_SUBOBJECT_TYPE Type() const\n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator const D3D12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION&() const { return m_Desc; }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_Desc = {};\n        m_Strings.clear();\n        m_SubobjectName.clear();\n        m_Exports.clear();\n    }\n    void* Data() { return &m_Desc; }\n    D3D12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION m_Desc;\n    CD3DX12_STATE_OBJECT_DESC::StringContainer m_Strings;\n    CD3DX12_STATE_OBJECT_DESC::StringContainer m_SubobjectName;\n    std::vector<LPCWSTR> m_Exports;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_HIT_GROUP_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_HIT_GROUP_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_HIT_GROUP_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void SetHitGroupExport(LPCWSTR exportName)\n    { \n        m_Desc.HitGroupExport = m_Strings[0].LocalCopy(exportName, true); \n    }\n    void SetHitGroupType(D3D12_HIT_GROUP_TYPE Type) { m_Desc.Type = Type; }\n    void SetAnyHitShaderImport(LPCWSTR importName)\n    { \n        m_Desc.AnyHitShaderImport = m_Strings[1].LocalCopy(importName, true); \n    }\n    void SetClosestHitShaderImport(LPCWSTR importName)\n    { \n        m_Desc.ClosestHitShaderImport = m_Strings[2].LocalCopy(importName, true); \n    }\n    void SetIntersectionShaderImport(LPCWSTR importName)\n    {\n        m_Desc.IntersectionShaderImport = m_Strings[3].LocalCopy(importName, true); \n    }\n    D3D12_STATE_SUBOBJECT_TYPE Type() const\n    {\n        return D3D12_STATE_SUBOBJECT_TYPE_HIT_GROUP; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator const D3D12_HIT_GROUP_DESC&() const { return m_Desc; }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_Desc = {};\n        for (UINT i = 0; i < m_NumStrings; i++)\n        {\n            m_Strings[i].clear();\n        }\n    }\n    void* Data() { return &m_Desc; }\n    D3D12_HIT_GROUP_DESC m_Desc;\n    static const UINT m_NumStrings = 4;\n    CD3DX12_STATE_OBJECT_DESC::StringContainer \n        m_Strings[m_NumStrings]; // one string for every entrypoint name\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_RAYTRACING_SHADER_CONFIG_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_RAYTRACING_SHADER_CONFIG_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_RAYTRACING_SHADER_CONFIG_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void Config(UINT MaxPayloadSizeInBytes, UINT MaxAttributeSizeInBytes)\n    {\n        m_Desc.MaxPayloadSizeInBytes = MaxPayloadSizeInBytes;\n        m_Desc.MaxAttributeSizeInBytes = MaxAttributeSizeInBytes;\n    }\n    D3D12_STATE_SUBOBJECT_TYPE Type() const\n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_SHADER_CONFIG; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator const D3D12_RAYTRACING_SHADER_CONFIG&() const { return m_Desc; }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_Desc = {};\n    }\n    void* Data() { return &m_Desc; }\n    D3D12_RAYTRACING_SHADER_CONFIG m_Desc;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void Config(UINT MaxTraceRecursionDepth)\n    {\n        m_Desc.MaxTraceRecursionDepth = MaxTraceRecursionDepth;\n    }\n    D3D12_STATE_SUBOBJECT_TYPE Type() const\n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_PIPELINE_CONFIG; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator const D3D12_RAYTRACING_PIPELINE_CONFIG&() const { return m_Desc; }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_Desc = {};\n    }\n    void* Data() { return &m_Desc; }\n    D3D12_RAYTRACING_PIPELINE_CONFIG m_Desc;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void SetRootSignature(ID3D12RootSignature* pRootSig)\n    {\n        m_pRootSig = pRootSig;\n    }\n    D3D12_STATE_SUBOBJECT_TYPE Type() const\n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_GLOBAL_ROOT_SIGNATURE; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator ID3D12RootSignature*() const { return m_pRootSig.Get(); }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_pRootSig = nullptr;\n    }\n    void* Data() { return m_pRootSig.GetAddressOf(); }\n    Microsoft::WRL::ComPtr<ID3D12RootSignature> m_pRootSig;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_LOCAL_ROOT_SIGNATURE_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_LOCAL_ROOT_SIGNATURE_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_LOCAL_ROOT_SIGNATURE_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void SetRootSignature(ID3D12RootSignature* pRootSig)\n    {\n        m_pRootSig = pRootSig;\n    }\n    D3D12_STATE_SUBOBJECT_TYPE Type() const\n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_LOCAL_ROOT_SIGNATURE; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator ID3D12RootSignature*() const { return m_pRootSig.Get(); }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_pRootSig = nullptr;\n    }\n    void* Data() { return m_pRootSig.GetAddressOf(); }\n    Microsoft::WRL::ComPtr<ID3D12RootSignature> m_pRootSig;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_STATE_OBJECT_CONFIG_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_STATE_OBJECT_CONFIG_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_STATE_OBJECT_CONFIG_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void SetFlags(D3D12_STATE_OBJECT_FLAGS Flags)\n    {\n        m_Desc.Flags = Flags;\n    }\n    D3D12_STATE_SUBOBJECT_TYPE Type() const\n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_STATE_OBJECT_CONFIG; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator const D3D12_STATE_OBJECT_CONFIG&() const { return m_Desc; }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_Desc = {};\n    }\n    void* Data() { return &m_Desc; }\n    D3D12_STATE_OBJECT_CONFIG m_Desc;\n};\n\n//------------------------------------------------------------------------------------------------\nclass CD3DX12_NODE_MASK_SUBOBJECT \n    : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE\n{\npublic:\n    CD3DX12_NODE_MASK_SUBOBJECT()\n    {\n        Init();\n    }\n    CD3DX12_NODE_MASK_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject)\n    {\n        Init();\n        AddToStateObject(ContainingStateObject);\n    }\n    void SetNodeMask(UINT NodeMask)\n    {\n        m_Desc.NodeMask = NodeMask;\n    }\n    D3D12_STATE_SUBOBJECT_TYPE Type() const\n    { \n        return D3D12_STATE_SUBOBJECT_TYPE_NODE_MASK; \n    }\n    operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; }\n    operator const D3D12_NODE_MASK&() const { return m_Desc; }\nprivate:\n    void Init()\n    {\n        SUBOBJECT_HELPER_BASE::Init();\n        m_Desc = {};\n    }\n    void* Data() { return &m_Desc; }\n    D3D12_NODE_MASK m_Desc;\n};\n\n#endif // #ifndef D3DX12_NO_STATE_OBJECT_HELPERS\n\n#endif // defined( __cplusplus )\n\n#pragma warning(pop)\n\n#endif //__D3DX12_H__\n\n\n\n"
  },
  {
    "path": "src/engine_registry.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"engine_registry.hpp\"\n\n#include \"pipeline_registry.hpp\"\n#include \"root_signature_registry.hpp\"\n#include \"shader_registry.hpp\"\n#include \"rt_pipeline_registry.hpp\"\n#include \"d3d12/d3d12_structs.hpp\"\n\n// Register something into a registry.\n#define REGISTER(type, registry) decltype(type) type = registry::Get().Register\n// Decscriptor Range Array\n#define DESC_RANGE_ARRAY(name, ...) std::vector<CD3DX12_DESCRIPTOR_RANGE> name { __VA_ARGS__ };\n// Descriptor Range\n#define DESC_RANGE(...) [] { return GetRange(__VA_ARGS__); }()\n// Descriptor Range Hardcoded\n#define DESC_RANGE_H(...) [] { CD3DX12_DESCRIPTOR_RANGE macro_range; macro_range.Init(__VA_ARGS__); return macro_range; }()\n// Root parameter\n#define ROOT_PARAM(func) [] { return func; }()\n// Root Parameter for descriptor tables\n#define ROOT_PARAM_DESC_TABLE(arr, visibility) [] { CD3DX12_ROOT_PARAMETER d; d.InitAsDescriptorTable(static_cast<unsigned int>(arr.size()), arr.data(), visibility); return d; }()\n\nnamespace wr\n{\n\tusing namespace rs_layout;\n\n\t//BDRF Lut Precalculation Root Signature\n\tDESC_RANGE_ARRAY(ranges_brdf,\n\t\tDESC_RANGE(params::brdf_lut, Type::UAV_RANGE, params::BRDF_LutE::OUTPUT),\n\t\t);\n\n\tREGISTER(root_signatures::brdf_lut, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(ranges_brdf, D3D12_SHADER_VISIBILITY_ALL)\n\t\t},\n\t\t.m_samplers = { }\n\t});\n\n\n\t//Basic Deferred Pass Root Signature\n\tDESC_RANGE_ARRAY(ranges_deferred_main,\n\t\tDESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::ALBEDO),\n\t\tDESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::NORMAL),\n\t\tDESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::ROUGHNESS),\n\t\tDESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::METALLIC),\n\t\tDESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::AMBIENT_OCCLUSION),\n\t\tDESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::EMISSIVE),\n\t);\n\n\tREGISTER(root_signatures::basic, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM(GetCBV(params::deferred_main, params::DeferredMainE::CAMERA_PROPERTIES, D3D12_SHADER_VISIBILITY_VERTEX)),\n\t\t\tROOT_PARAM(GetCBV(params::deferred_main, params::DeferredMainE::OBJECT_PROPERTIES, D3D12_SHADER_VISIBILITY_VERTEX)),\n\t\t\tROOT_PARAM_DESC_TABLE(ranges_deferred_main, D3D12_SHADER_VISIBILITY_PIXEL),\n\t\t\tROOT_PARAM(GetCBV(params::deferred_main, params::DeferredMainE::MATERIAL_PROPERTIES, D3D12_SHADER_VISIBILITY_PIXEL)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_WRAP }\n\t\t}\n\t});\n\n\tDESC_RANGE_ARRAY(svgf_denoiser_ranges,\n\t\tDESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::INPUT),\n\t\tDESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::MOTION),\n\t\tDESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::NORMAL),\n\t\tDESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::DEPTH),\n\n\t\tDESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::IN_HIST_LENGTH),\n\n\t\tDESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::PREV_INPUT),\n\t\tDESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::PREV_MOMENTS),\n\t\tDESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::PREV_NORMAL),\n\t\tDESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::PREV_DEPTH),\n\n\t\tDESC_RANGE(params::svgf_denoiser, Type::UAV_RANGE, params::SVGFDenoiserE::OUT_COLOR),\n\t\tDESC_RANGE(params::svgf_denoiser, Type::UAV_RANGE, params::SVGFDenoiserE::OUT_MOMENTS),\n\t\tDESC_RANGE(params::svgf_denoiser, Type::UAV_RANGE, params::SVGFDenoiserE::OUT_HIST_LENGTH),\n\t\t);\n\n\tREGISTER(root_signatures::svgf_denoiser, RootSignatureRegistry)({\n\t\t.m_parameters ={\n\t\t\tROOT_PARAM_DESC_TABLE(svgf_denoiser_ranges, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetCBV(params::svgf_denoiser, params::SVGFDenoiserE::SVGF_PROPERTIES)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP},\n\t\t\t{TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t});\n\n\t//Deferred Composition Root Signature\n\tDESC_RANGE_ARRAY(srv_ranges,\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::GBUFFER_ALBEDO_ROUGHNESS),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::GBUFFER_NORMAL_METALLIC),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::GBUFFER_EMISSIVE_AO),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::GBUFFER_DEPTH),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::LIGHT_BUFFER),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::SKY_BOX),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::IRRADIANCE_MAP),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::PREF_ENV_MAP),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BRDF_LUT),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BUFFER_REFLECTION),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BUFFER_SHADOW),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BUFFER_SCREEN_SPACE_IRRADIANCE),\n\t\tDESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BUFFER_AO),\n\t\tDESC_RANGE(params::deferred_composition, Type::UAV_RANGE, params::DeferredCompositionE::OUTPUT),\n\t);\n\n\tREGISTER(root_signatures::deferred_composition, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM(GetCBV(params::deferred_composition, params::DeferredCompositionE::CAMERA_PROPERTIES)),\n\t\t\tROOT_PARAM_DESC_TABLE(srv_ranges, D3D12_SHADER_VISIBILITY_ALL),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP },\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP }\n\t\t}\n\t});\n\n\t//MipMapping Root Signature\n\tDESC_RANGE_ARRAY(mip_in_out_ranges,\n\t\tDESC_RANGE(params::mip_mapping, Type::SRV_RANGE, params::MipMappingE::SOURCE),\n\t\tDESC_RANGE(params::mip_mapping, Type::UAV_RANGE, params::MipMappingE::DEST),\n\t);\n\tREGISTER(root_signatures::mip_mapping, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM(GetConstants(params::mip_mapping, params::MipMappingE::CBUFFER)),\n\t\t\tROOT_PARAM_DESC_TABLE(mip_in_out_ranges, D3D12_SHADER_VISIBILITY_ALL)\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP }\n\t\t}\n\t});\n\n\t//Prefiltering Root Signature\n\tDESC_RANGE_ARRAY(prefilter_in_out_ranges,\n\t\tDESC_RANGE(params::cubemap_prefiltering, Type::SRV_RANGE, params::CubemapPrefilteringE::SOURCE),\n\t\tDESC_RANGE(params::cubemap_prefiltering, Type::UAV_RANGE, params::CubemapPrefilteringE::DEST),\n\t\t);\n\tREGISTER(root_signatures::cubemap_prefiltering, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM(GetConstants(params::cubemap_prefiltering, params::CubemapPrefilteringE::CBUFFER)),\n\t\t\tROOT_PARAM_DESC_TABLE(prefilter_in_out_ranges, D3D12_SHADER_VISIBILITY_ALL)\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP }\n\t\t}\n\t});\n\n\n\t//Cubemap conversion root signature\n\tDESC_RANGE_ARRAY(cubemap_tasks_ranges,\n\t\tDESC_RANGE(params::cubemap_conversion, Type::SRV_RANGE, params::CubemapConversionE::EQUIRECTANGULAR_TEXTURE),\n\t\t);\n\tREGISTER(root_signatures::cubemap_conversion, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM(GetConstants(params::cubemap_conversion, params::CubemapConversionE::IDX)),\n\t\t\tROOT_PARAM(GetCBV(params::cubemap_conversion, params::CubemapConversionE::CAMERA_PROPERTIES)),\n\t\t\tROOT_PARAM_DESC_TABLE(cubemap_tasks_ranges, D3D12_SHADER_VISIBILITY_PIXEL),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP }\n\t\t}\n\t});\n\n\t//Cubemap convolution root signature\n\tDESC_RANGE_ARRAY(cubemap_convolution_ranges,\n\t\tDESC_RANGE(params::cubemap_conversion, Type::SRV_RANGE, params::CubemapConvolutionE::ENVIRONMENT_CUBEMAP),\n\t\t);\n\tREGISTER(root_signatures::cubemap_convolution, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM(GetConstants(params::cubemap_convolution, params::CubemapConvolutionE::IDX)),\n\t\t\tROOT_PARAM(GetCBV(params::cubemap_convolution, params::CubemapConvolutionE::CAMERA_PROPERTIES)),\n\t\t\tROOT_PARAM_DESC_TABLE(cubemap_tasks_ranges, D3D12_SHADER_VISIBILITY_PIXEL),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP }\n\t\t}\n\t});\n\n\n\tREGISTER(shaders::brdf_lut_cs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pbr_brdf_lut.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::basic_deferred_vs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/deferred_geometry_pass.hlsl\",\n\t\t.entry = \"main_vs\",\n\t\t.type = ShaderType::VERTEX_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::basic_deferred_ps, ShaderRegistry)({\n\t\t.path = \"resources/shaders/deferred_geometry_pass.hlsl\",\n\t\t.entry = \"main_ps\",\n\t\t.type = ShaderType::PIXEL_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::basic_hybrid_vs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/deferred_geometry_pass.hlsl\",\n\t\t.entry = \"main_vs\",\n\t\t.type = ShaderType::VERTEX_SHADER,\n\t\t.defines = {{L\"IS_HYBRID\", L\"1\"}}\n\t\t});\n\n\tREGISTER(shaders::basic_hybrid_ps, ShaderRegistry)({\n\t\t.path = \"resources/shaders/deferred_geometry_pass.hlsl\",\n\t\t.entry = \"main_ps\",\n\t\t.type = ShaderType::PIXEL_SHADER,\n\t\t.defines = {{L\"IS_HYBRID\", L\"1\"}}\n\t\t});\n\n\tREGISTER(shaders::fullscreen_quad_vs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/fullscreen_quad.hlsl\",\n\t\t.entry = \"main_vs\",\n\t\t.type = ShaderType::VERTEX_SHADER,\n\t\t.defines = {}\n\t\t});\n\n\tREGISTER(shaders::svgf_denoiser_reprojection_cs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/denoising_SVGF.hlsl\",\n\t\t.entry = \"reprojection_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::svgf_denoiser_filter_moments_cs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/denoising_SVGF.hlsl\",\n\t\t.entry = \"filter_moments_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::svgf_denoiser_wavelet_filter_cs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/denoising_SVGF.hlsl\",\n\t\t.entry = \"wavelet_filter_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::deferred_composition_cs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/deferred_composition_pass.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::mip_mapping_cs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/generate_mips_cs.hlsl\",\n\t\t.entry = \"main\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::equirect_to_cubemap_vs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pbr_cubemap_conversion.hlsl\",\n\t\t.entry = \"main_vs\",\n\t\t.type = ShaderType::VERTEX_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::equirect_to_cubemap_ps, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pbr_cubemap_conversion.hlsl\",\n\t\t.entry = \"main_ps\",\n\t\t.type = ShaderType::PIXEL_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::cubemap_convolution_ps, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pbr_cubemap_convolution.hlsl\",\n\t\t.entry = \"main_ps\",\n\t\t.type = ShaderType::PIXEL_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::cubemap_prefiltering_cs, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pbr_prefilter_env_map.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(pipelines::brdf_lut_precalculation, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::brdf_lut_cs,\n\t\t.m_root_signature_handle = root_signatures::brdf_lut,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { Format::R16G16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(pipelines::basic_deferred, PipelineRegistry) < Vertex > ({\n\t\t.m_vertex_shader_handle = shaders::basic_deferred_vs,\n\t\t.m_pixel_shader_handle = shaders::basic_deferred_ps,\n\t\t.m_compute_shader_handle = std::nullopt,\n\t\t.m_root_signature_handle = root_signatures::basic,\n\t\t.m_dsv_format = Format::D32_FLOAT,\n\t\t.m_rtv_formats = { Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 3,\n\t\t.m_type = PipelineType::GRAPHICS_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_NONE,\n\t\t.m_depth_enabled = true,\n\t\t.m_counter_clockwise = false,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t\t});\n\n\tREGISTER(pipelines::basic_hybrid, PipelineRegistry) < Vertex > ({\n\t\t.m_vertex_shader_handle = shaders::basic_hybrid_vs,\n\t\t.m_pixel_shader_handle = shaders::basic_hybrid_ps,\n\t\t.m_compute_shader_handle = std::nullopt,\n\t\t.m_root_signature_handle = root_signatures::basic,\n\t\t.m_dsv_format = Format::D32_FLOAT,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT, wr::Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT, wr::Format::R16G16B16A16_FLOAT, wr::Format::R32G32B32A32_FLOAT, wr::Format::R32G32B32A32_FLOAT },\n\t\t.m_num_rtv_formats = 6,\n\t\t.m_type = PipelineType::GRAPHICS_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_NONE,\n\t\t.m_depth_enabled = true,\n\t\t.m_counter_clockwise = false,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t\t});\n\n\tREGISTER(pipelines::svgf_denoiser_reprojection, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::svgf_denoiser_reprojection_cs,\n\t\t.m_root_signature_handle = root_signatures::svgf_denoiser,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = {Format::R16G16B16A16_FLOAT},\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(pipelines::svgf_denoiser_filter_moments, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::svgf_denoiser_filter_moments_cs,\n\t\t.m_root_signature_handle = root_signatures::svgf_denoiser,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = {Format::R16G16B16A16_FLOAT},\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(pipelines::svgf_denoiser_wavelet_filter, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::svgf_denoiser_wavelet_filter_cs,\n\t\t.m_root_signature_handle = root_signatures::svgf_denoiser,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = {Format::R16G16B16A16_FLOAT},\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(pipelines::deferred_composition, PipelineRegistry)<Vertex2D>({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::deferred_composition_cs,\n\t\t.m_root_signature_handle = root_signatures::deferred_composition,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t\t});\n\n\tREGISTER(pipelines::mip_mapping, PipelineRegistry) < Vertex > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::mip_mapping_cs,\n\t\t.m_root_signature_handle = root_signatures::mip_mapping,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { },\n\t\t.m_num_rtv_formats = 0,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(pipelines::equirect_to_cubemap, PipelineRegistry) < Vertex > (\n\t{\n\t\t.m_vertex_shader_handle = shaders::equirect_to_cubemap_vs,\n\t\t.m_pixel_shader_handle = shaders::equirect_to_cubemap_ps,\n\t\t.m_compute_shader_handle = std::nullopt,\n\t\t.m_root_signature_handle = root_signatures::cubemap_conversion,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::GRAPHICS_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_NONE,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = false,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(pipelines::cubemap_convolution, PipelineRegistry) < Vertex > (\n\t{\n\t\t.m_vertex_shader_handle = shaders::equirect_to_cubemap_vs,\n\t\t.m_pixel_shader_handle = shaders::cubemap_convolution_ps,\n\t\t.m_compute_shader_handle = std::nullopt,\n\t\t.m_root_signature_handle = root_signatures::cubemap_convolution,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::GRAPHICS_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_NONE,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = false,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(pipelines::cubemap_prefiltering, PipelineRegistry) < Vertex > (\n\t{\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::cubemap_prefiltering_cs,\n\t\t.m_root_signature_handle = root_signatures::cubemap_prefiltering,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { Format::UNKNOWN },\n\t\t.m_num_rtv_formats = 0,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_NONE,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = false,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\t/* ### Raytracing ### */\n\tREGISTER(shaders::post_processing, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_tonemapping.hlsl\",\n\t\t.entry = \"main\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::accumulation, ShaderRegistry)({\n\t\t.path = \"resources/shaders/dxr_pathtracer_accumulation.hlsl\",\n\t\t.entry = \"main\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tREGISTER(shaders::rt_lib, ShaderRegistry)({\n\t\t.path = \"resources/shaders/dxr_raytracing.hlsl\",\n\t\t.entry = \"RaygenEntry\",\n\t\t.type = ShaderType::LIBRARY_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(post_r,\n\t\tDESC_RANGE(params::post_processing, Type::SRV_RANGE, params::PostProcessingE::SOURCE),\n\t\tDESC_RANGE(params::post_processing, Type::UAV_RANGE, params::PostProcessingE::DEST),\n\t);\n\n\tREGISTER(root_signatures::post_processing, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(post_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetConstants(params::post_processing, params::PostProcessingE::HDR_SUPPORT)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_BORDER }\n\t\t}\n\t});\n\n\tDESC_RANGE_ARRAY(accum_r,\n\t\tDESC_RANGE(params::accumulation, Type::SRV_RANGE, params::AccumulationE::SOURCE),\n\t\tDESC_RANGE(params::accumulation, Type::UAV_RANGE, params::AccumulationE::DEST),\n\t);\n\n\tREGISTER(root_signatures::accumulation, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(accum_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetConstants(params::accumulation, params::AccumulationE::FRAME_IDX)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_BORDER }\n\t\t}\n\t});\n\n\tREGISTER(pipelines::post_processing, PipelineRegistry) < Vertex2D > (\n\t{\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::post_processing,\n\t\t.m_root_signature_handle = root_signatures::post_processing,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { d3d12::settings::back_buffer_format },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_NONE,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(pipelines::accumulation, PipelineRegistry) < Vertex2D > (\n\t{\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::accumulation,\n\t\t.m_root_signature_handle = root_signatures::accumulation,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_NONE,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tDESC_RANGE_ARRAY(r_full_rt,\n\t\tDESC_RANGE(params::full_raytracing, Type::UAV_RANGE, params::FullRaytracingE::OUTPUT),\n\t\tDESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::INDICES),\n\t\tDESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::LIGHTS),\n\t\tDESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::MATERIALS),\n\t\tDESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::OFFSETS),\n\t\tDESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::SKYBOX),\n\t\tDESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::BRDF_LUT),\n\t\tDESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::IRRADIANCE_MAP),\n\t\tDESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::TEXTURES),\n\t\tDESC_RANGE_H(D3D12_DESCRIPTOR_RANGE_TYPE::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 5, d3d12::settings::fallback_ptrs_offset),\n\t);\n\n\tREGISTER(root_signatures::rt_test_global, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(r_full_rt, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetSRV(params::full_raytracing, params::FullRaytracingE::ACCELERATION_STRUCTURE)),\n\t\t\tROOT_PARAM(GetCBV(params::full_raytracing, params::FullRaytracingE::CAMERA_PROPERTIES)),\n\t\t\tROOT_PARAM(GetSRV(params::full_raytracing, params::FullRaytracingE::VERTICES)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_ANISOTROPIC, TextureAddressMode::TAM_WRAP },\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP }\n\t\t},\n\t\t.m_rtx = true\n\t});\n\n\tStateObjectDescription::LibraryDesc rt_full_lib = []()\n\t{\n\t\tStateObjectDescription::LibraryDesc lib;\n\t\tlib.shader_handle = shaders::rt_lib;\n\t\tlib.exports.push_back(L\"RaygenEntry\");\n\t\tlib.exports.push_back(L\"ClosestHitEntry\");\n\t\tlib.exports.push_back(L\"MissEntry\");\n\t\tlib.exports.push_back(L\"ShadowClosestHitEntry\");\n\t\tlib.exports.push_back(L\"ShadowMissEntry\");\n\t\tlib.m_hit_groups.push_back({ L\"MyHitGroup\", L\"ClosestHitEntry\" });\n\t\tlib.m_hit_groups.push_back({ L\"ShadowHitGroup\", L\"ShadowClosestHitEntry\" });\n\n\t\treturn lib;\n\t}();\n\n\tREGISTER(state_objects::state_object, RTPipelineRegistry)(\n\t{\n\t\t.desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE,\n\t\t.library_desc = rt_full_lib,\n\t\t.max_payload_size = (sizeof(float) * 7) + sizeof(unsigned int),\n\t\t.max_attributes_size = sizeof(float) * 4,\n\t\t.max_recursion_depth = 3,\n\t\t.global_root_signature = root_signatures::rt_test_global,\n\t\t.local_root_signatures = {},\n\t});\n\n\t/* ### Depth of field ### */\n\n\t// Cone of confusion\n\tREGISTER(shaders::dof_coc, ShaderRegistry) ({\n\t\t.path = \"resources/shaders/pp_dof_coc.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(dofcoc_r,\n\t\tDESC_RANGE(params::dof_coc, Type::SRV_RANGE, params::DoFCoCE::GDEPTH),\n\t\tDESC_RANGE(params::dof_coc, Type::UAV_RANGE, params::DoFCoCE::OUTPUT),\n\t);\n\n\tREGISTER(root_signatures::dof_coc, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(dofcoc_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetCBV(params::dof_coc, params::DoFCoCE::CAMERA_PROPERTIES)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t});\n\n\tREGISTER(pipelines::dof_coc, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::dof_coc,\n\t\t.m_root_signature_handle = root_signatures::dof_coc,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { Format::R16G16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\t// Compute Near mask\n\tREGISTER(shaders::dof_near_mask, ShaderRegistry) ({\n\t\t.path = \"resources/shaders/pp_dof_compute_near_mask.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t\t});\n\n\tDESC_RANGE_ARRAY(dofnear_r,\n\t\tDESC_RANGE(params::dof_near_mask, Type::SRV_RANGE, params::DoFNearMaskE::INPUT),\n\t\tDESC_RANGE(params::dof_near_mask, Type::UAV_RANGE, params::DoFNearMaskE::OUTPUT),\n\t\t);\n\n\tREGISTER(root_signatures::dof_near_mask, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(dofnear_r, D3D12_SHADER_VISIBILITY_ALL),\n\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t\t});\n\n\tREGISTER(pipelines::dof_near_mask, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::dof_near_mask,\n\t\t.m_root_signature_handle = root_signatures::dof_near_mask,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { Format::R16G16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t\t});\n\n\t// Down Scale texture\n\tREGISTER(shaders::down_scale, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_dof_downscale.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(dscale_r,\n\t\tDESC_RANGE(params::down_scale, Type::SRV_RANGE, params::DownScaleE::SOURCE),\n\t\tDESC_RANGE(params::down_scale, Type::UAV_RANGE, params::DownScaleE::OUTPUT_NEAR),\n\t\tDESC_RANGE(params::down_scale, Type::UAV_RANGE, params::DownScaleE::OUTPUT_FAR),\n\t\tDESC_RANGE(params::down_scale, Type::SRV_RANGE, params::DownScaleE::COC),\n\t);\n\n\tREGISTER(root_signatures::down_scale, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(dscale_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP},\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t});\n\n\tREGISTER(pipelines::down_scale, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::down_scale,\n\t\t.m_root_signature_handle = root_signatures::down_scale,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT},\n\t\t.m_num_rtv_formats = 2,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\t// dof dilate\n\tREGISTER(shaders::dof_dilate, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_dof_dilate.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(dilate_r,\n\t\tDESC_RANGE(params::dof_dilate, Type::SRV_RANGE, params::DoFDilateE::SOURCE),\n\t\tDESC_RANGE(params::dof_dilate, Type::UAV_RANGE, params::DoFDilateE::OUTPUT),\n\t);\n\n\tREGISTER(root_signatures::dof_dilate, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(dilate_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t});\n\n\tREGISTER(pipelines::dof_dilate, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::dof_dilate,\n\t\t.m_root_signature_handle = root_signatures::dof_dilate,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { Format::R32_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\t//dof bokeh\n\tREGISTER(shaders::dof_bokeh, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_dof_bokeh.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(dof_bokeh_r,\n\t\tDESC_RANGE(params::dof_bokeh, Type::SRV_RANGE, params::DoFBokehE::SOURCE_NEAR),\n\t\tDESC_RANGE(params::dof_bokeh, Type::SRV_RANGE, params::DoFBokehE::SOURCE_FAR),\n\t\tDESC_RANGE(params::dof_bokeh, Type::UAV_RANGE, params::DoFBokehE::OUTPUT_NEAR),\n\t\tDESC_RANGE(params::dof_bokeh, Type::UAV_RANGE, params::DoFBokehE::OUTPUT_FAR),\n\t\tDESC_RANGE(params::dof_bokeh, Type::SRV_RANGE, params::DoFBokehE::COC),\n\t);\n\n\tREGISTER(root_signatures::dof_bokeh, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(dof_bokeh_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetCBV(params::dof_bokeh, params::DoFBokehE::CAMERA_PROPERTIES)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP},\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t});\n\n\tREGISTER(pipelines::dof_bokeh, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::dof_bokeh,\n\t\t.m_root_signature_handle = root_signatures::dof_bokeh,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 2,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\t//dof bokeh post filter\n\tREGISTER(shaders::dof_bokeh_post_filter, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_dof_bokeh_post_filter.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(dof_bokeh_post_filter_r,\n\t\tDESC_RANGE(params::dof_bokeh_post_filter, Type::SRV_RANGE, params::DoFBokehPostFilterE::SOURCE_NEAR),\n\t\tDESC_RANGE(params::dof_bokeh_post_filter, Type::SRV_RANGE, params::DoFBokehPostFilterE::SOURCE_FAR),\n\t\tDESC_RANGE(params::dof_bokeh_post_filter, Type::UAV_RANGE, params::DoFBokehPostFilterE::OUTPUT_NEAR),\n\t\tDESC_RANGE(params::dof_bokeh_post_filter, Type::UAV_RANGE, params::DoFBokehPostFilterE::OUTPUT_FAR),\n\t);\n\n\tREGISTER(root_signatures::dof_bokeh_post_filter, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(dof_bokeh_post_filter_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP},\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t});\n\n\tREGISTER(pipelines::dof_bokeh_post_filter, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::dof_bokeh_post_filter,\n\t\t.m_root_signature_handle = root_signatures::dof_bokeh_post_filter,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT, wr::Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 2,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\t// depth of field composition\n\tREGISTER(shaders::dof_composition, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_dof_composition.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(dof_composition_r,\n\t\tDESC_RANGE(params::dof_composition, Type::SRV_RANGE, params::DoFCompositionE::SOURCE),\n\t\tDESC_RANGE(params::dof_composition, Type::UAV_RANGE, params::DoFCompositionE::OUTPUT),\n\t\tDESC_RANGE(params::dof_composition, Type::SRV_RANGE, params::DoFCompositionE::BOKEH_NEAR),\n\t\tDESC_RANGE(params::dof_composition, Type::SRV_RANGE, params::DoFCompositionE::BOKEH_FAR),\n\t\tDESC_RANGE(params::dof_composition, Type::SRV_RANGE, params::DoFCompositionE::COC),\n\t);\n\n\tREGISTER(root_signatures::dof_composition, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(dof_composition_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP},\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t});\n\n\tREGISTER(pipelines::dof_composition, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::dof_composition,\n\t\t.m_root_signature_handle = root_signatures::dof_composition,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\t// Extract bright\n\tREGISTER(shaders::bloom_extract_bright, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_bloom_extract_bright.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER\n\t\t});\n\n\n\n\tDESC_RANGE_ARRAY(bloombright_r,\n\t\tDESC_RANGE(params::bloom_extract_bright, Type::SRV_RANGE, params::BloomExtractBrightE::SOURCE),\n\t\tDESC_RANGE(params::bloom_extract_bright, Type::SRV_RANGE, params::BloomExtractBrightE::G_EMISSIVE),\n\t\tDESC_RANGE(params::bloom_extract_bright, Type::SRV_RANGE, params::BloomExtractBrightE::G_DEPTH),\n\t\tDESC_RANGE(params::bloom_extract_bright, Type::UAV_RANGE, params::BloomExtractBrightE::OUTPUT_BRIGHT),\n\t\t);\n\n\tREGISTER(root_signatures::bloom_extract_bright, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(bloombright_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP},\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t\t});\n\n\tREGISTER(pipelines::bloom_extract_bright, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::bloom_extract_bright,\n\t\t.m_root_signature_handle = root_signatures::bloom_extract_bright,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t\t});\n\n\t// bloom blur\n\tREGISTER(shaders::bloom_blur, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_bloom_blur.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t\t});\n\n\n\tDESC_RANGE_ARRAY(bloom_blur_r,\n\t\tDESC_RANGE(params::bloom_blur, Type::SRV_RANGE, params::BloomBlurE::SOURCE),\n\t\tDESC_RANGE(params::bloom_blur, Type::UAV_RANGE, params::BloomBlurE::OUTPUT),\n\t\t);\n\n\tREGISTER(root_signatures::bloom_blur, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(bloom_blur_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetCBV(params::bloom_blur, params::BloomBlurE::BLUR_DIRECTION)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP},\n\t\t}\n\t\t});\n\n\tREGISTER(pipelines::bloom_blur, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::bloom_blur,\n\t\t.m_root_signature_handle = root_signatures::bloom_blur,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = {wr::Format::R16G16B16A16_FLOAT},\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t\t});\n\n\t// bloom blur horizontal\n\tREGISTER(shaders::bloom_blur_horizontal, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_bloom_blur_horizontal.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t\t});\n\n\n\tDESC_RANGE_ARRAY(bloom_blur_t_r,\n\t\tDESC_RANGE(params::bloom_blur_horizontal, Type::SRV_RANGE, params::BloomBlurHorizontalE::SOURCE_MAIN),\n\t\tDESC_RANGE(params::bloom_blur_horizontal, Type::UAV_RANGE, params::BloomBlurHorizontalE::OUTPUT),\n\t\tDESC_RANGE(params::bloom_blur_horizontal, Type::UAV_RANGE, params::BloomBlurHorizontalE::OUTPUT_QES),\n\t\t);\n\n\tREGISTER(root_signatures::bloom_blur_horizontal, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(bloom_blur_t_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetCBV(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::BLOOM_PROPERTIES)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP},\n\t\t}\n\t\t});\n\n\tREGISTER(pipelines::bloom_blur_horizontal, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::bloom_blur_horizontal,\n\t\t.m_root_signature_handle = root_signatures::bloom_blur_horizontal,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = {wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT},\n\t\t.m_num_rtv_formats = 2,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t\t});\n\n\t// bloom blur test\n\tREGISTER(shaders::bloom_blur_vertical, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_bloom_blur_vertical.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t\t});\n\n\n\tDESC_RANGE_ARRAY(bloom_blur_v_r,\n\t\tDESC_RANGE(params::bloom_blur_vertical, Type::SRV_RANGE, params::BloomBlurVerticalE::SOURCE_MAIN),\n\t\tDESC_RANGE(params::bloom_blur_vertical, Type::SRV_RANGE, params::BloomBlurVerticalE::SOURCE_QES),\n\t\tDESC_RANGE(params::bloom_blur_vertical, Type::UAV_RANGE, params::BloomBlurVerticalE::OUTPUT),\n\t\tDESC_RANGE(params::bloom_blur_vertical, Type::UAV_RANGE, params::BloomBlurVerticalE::OUTPUT_QES),\n\t\t);\n\n\tREGISTER(root_signatures::bloom_blur_vertical, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(bloom_blur_v_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetCBV(params::bloom_blur_vertical, params::BloomBlurVerticalE::BLOOM_PROPERTIES)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP},\n\t\t}\n\t\t});\n\n\tREGISTER(pipelines::bloom_blur_vertical, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::bloom_blur_vertical,\n\t\t.m_root_signature_handle = root_signatures::bloom_blur_vertical,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = {wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT},\n\t\t.m_num_rtv_formats = 2,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t\t});\n\n\t// bloom composition\n\tREGISTER(shaders::bloom_composition, ShaderRegistry)({\n\t\t.path = \"resources/shaders/pp_bloom_composition.hlsl\",\n\t\t.entry = \"main_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(bloom_comp_r,\n\t\tDESC_RANGE(params::bloom_composition, Type::SRV_RANGE, params::BloomCompositionE::SOURCE_MAIN),\n\t\tDESC_RANGE(params::bloom_composition, Type::SRV_RANGE, params::BloomCompositionE::SOURCE_BLOOM_HALF),\n\t\tDESC_RANGE(params::bloom_composition, Type::SRV_RANGE, params::BloomCompositionE::SOURCE_BLOOM_QES),\n\t\tDESC_RANGE(params::bloom_composition, Type::UAV_RANGE, params::BloomCompositionE::OUTPUT),\n\t);\n\n\tREGISTER(root_signatures::bloom_composition, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(bloom_comp_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetConstants(params::bloom_composition, params::BloomCompositionE::BLOOM_PROPERTIES)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP},\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}\n\t\t}\n\t});\n\n\tREGISTER(pipelines::bloom_composition, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::bloom_composition,\n\t\t.m_root_signature_handle = root_signatures::bloom_composition,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = {wr::Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\t// spatial reconstruction\n  REGISTER(shaders::spatial_reconstruction, ShaderRegistry)({\n    .path = \"resources/shaders/denoising_spatial_reconstruction.hlsl\",\n    .entry = \"main\",\n    .type = ShaderType::DIRECT_COMPUTE_SHADER,\n    .defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(spatial_reconstruction_r,\n\t\tDESC_RANGE(params::spatial_reconstruction, Type::UAV_RANGE, params::SpatialReconstructionE::OUTPUT),\n\t\tDESC_RANGE(params::spatial_reconstruction, Type::SRV_RANGE, params::SpatialReconstructionE::REFLECTION_BUFFER),\n\t\tDESC_RANGE(params::spatial_reconstruction, Type::SRV_RANGE, params::SpatialReconstructionE::GBUFFERS)\n\t);\n\n\tREGISTER(root_signatures::spatial_reconstruction, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(spatial_reconstruction_r, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetCBV(params::spatial_reconstruction, params::SpatialReconstructionE::CAMERA_PROPERTIES))\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_BORDER },\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_BORDER}\n\t\t}\n\t});\n\n\tREGISTER(pipelines::spatial_reconstruction, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::spatial_reconstruction,\n\t\t.m_root_signature_handle = root_signatures::spatial_reconstruction,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(shaders::reflection_temporal_denoiser, ShaderRegistry)({\n\t\t.path = \"resources/shaders/denoising_reflections.hlsl\",\n\t\t.entry = \"temporal_denoiser_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t\t});\n\n\tREGISTER(shaders::reflection_variance_estimator, ShaderRegistry)({\n\t\t.path = \"resources/shaders/denoising_reflections.hlsl\",\n\t\t.entry = \"variance_estimator_cs\",\n\t\t.type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t\t.defines = {}\n\t\t});\n\n\tREGISTER(shaders::reflection_spatial_denoiser, ShaderRegistry)({\n\t  .path = \"resources/shaders/denoising_reflections.hlsl\",\n\t  .entry = \"spatial_denoiser_cs\",\n\t  .type = ShaderType::DIRECT_COMPUTE_SHADER,\n\t  .defines = {}\n\t\t});\n\n\tDESC_RANGE_ARRAY(reflection_denoiser_ranges,\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::INPUT),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::RAY_RAW),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::RAY_DIR),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::ALBEDO_ROUGHNESS),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::NORMAL_METALLIC),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::MOTION),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::LINEAR_DEPTH),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::WORLD_POS),\n\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::IN_HISTORY),\n\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::ACCUM),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::PREV_NORMAL),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::PREV_DEPTH),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::IN_MOMENTS),\n\n\t\tDESC_RANGE(params::reflection_denoiser, Type::UAV_RANGE, params::ReflectionDenoiserE::OUTPUT),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::UAV_RANGE, params::ReflectionDenoiserE::OUT_HISTORY),\n\t\tDESC_RANGE(params::reflection_denoiser, Type::UAV_RANGE, params::ReflectionDenoiserE::OUT_MOMENTS)\n\t);\n  \n\tREGISTER(root_signatures::reflection_denoiser, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(reflection_denoiser_ranges, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetCBV(params::reflection_denoiser, params::ReflectionDenoiserE::CAMERA_PROPERTIES)),\n\t\t\tROOT_PARAM(GetCBV(params::reflection_denoiser, params::ReflectionDenoiserE::DENOISER_SETTINGS)),\n\t\t\tROOT_PARAM(GetCBV(params::reflection_denoiser, params::ReflectionDenoiserE::WAVELET_ITERATION))\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_POINT, TextureAddressMode::TAM_BORDER },\n\t\t\t{ TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_BORDER }\n\t\t}\n\t});\n\n\tREGISTER(pipelines::reflection_temporal_denoiser, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::reflection_temporal_denoiser,\n\t\t.m_root_signature_handle = root_signatures::reflection_denoiser,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\tREGISTER(pipelines::reflection_variance_estimator, PipelineRegistry) < Vertex2D > ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::reflection_variance_estimator,\n\t\t.m_root_signature_handle = root_signatures::reflection_denoiser,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t\t});\n\n\tREGISTER(pipelines::reflection_spatial_denoiser, PipelineRegistry) <Vertex2D> ({\n\t\t.m_vertex_shader_handle = std::nullopt,\n\t\t.m_pixel_shader_handle = std::nullopt,\n\t\t.m_compute_shader_handle = shaders::reflection_spatial_denoiser,\n\t\t.m_root_signature_handle = root_signatures::reflection_denoiser,\n\t\t.m_dsv_format = Format::UNKNOWN,\n\t\t.m_rtv_formats = { Format::R16G16B16A16_FLOAT },\n\t\t.m_num_rtv_formats = 1,\n\t\t.m_type = PipelineType::COMPUTE_PIPELINE,\n\t\t.m_cull_mode = CullMode::CULL_BACK,\n\t\t.m_depth_enabled = false,\n\t\t.m_counter_clockwise = true,\n\t\t.m_topology_type = TopologyType::TRIANGLE\n\t});\n\n\n\t/* ### Hybrid Raytracing ### */\n\tDESC_RANGE_ARRAY(rt_hybrid_ranges,\n\t\tDESC_RANGE(params::rt_hybrid, Type::UAV_RANGE, params::RTHybridE::OUTPUT),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::INDICES),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::LIGHTS),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::MATERIALS),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::OFFSETS),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::SKYBOX),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::PREF_ENV_MAP),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::BRDF_LUT),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::IRRADIANCE_MAP),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::TEXTURES),\n\t\tDESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::GBUFFERS),\n\t\tDESC_RANGE_H(D3D12_DESCRIPTOR_RANGE_TYPE::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 9, d3d12::settings::fallback_ptrs_offset),\n\t);\n\n\tREGISTER(root_signatures::rt_hybrid_global, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(rt_hybrid_ranges, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetSRV(params::rt_hybrid, params::RTHybridE::ACCELERATION_STRUCTURE)),\n\t\t\tROOT_PARAM(GetCBV(params::rt_hybrid, params::RTHybridE::CAMERA_PROPERTIES)),\n\t\t\tROOT_PARAM(GetSRV(params::rt_hybrid, params::RTHybridE::VERTICES)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_ANISOTROPIC, TextureAddressMode::TAM_WRAP }\n\t\t},\n\t\t.m_rtx = true\n\t});\n\n\t/*### Raytraced Ambient Oclusion ### */\n\tREGISTER(shaders::rt_ao_lib, ShaderRegistry)({\n\t\t.path = \"resources/shaders/dxr_ambient_occlusion.hlsl\",\n\t\t.entry = \"AORaygenEntry\",\n\t\t.type = ShaderType::LIBRARY_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(rt_ao_ranges,\n\t\tDESC_RANGE(params::rt_ao, Type::UAV_RANGE, params::RTAOE::OUTPUT),\n\t\tDESC_RANGE(params::rt_ao, Type::SRV_RANGE, params::RTAOE::GBUFFERS),\n\t\tDESC_RANGE_H(D3D12_DESCRIPTOR_RANGE_TYPE::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 9, d3d12::settings::fallback_ptrs_offset),\n\t);\n\n\tREGISTER(root_signatures::rt_ao_global, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(rt_ao_ranges, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetSRV(params::rt_ao, params::RTAOE::ACCELERATION_STRUCTURE)),\n\t\t\tROOT_PARAM(GetCBV(params::rt_ao, params::RTAOE::CAMERA_PROPERTIES)),\n\t\t},\n\t\t.m_samplers = {},\n\t\t.m_rtx = true\n\t});\n\n\tStateObjectDescription::LibraryDesc rt_ao_so_library = []()\n\t{\n\t\tStateObjectDescription::LibraryDesc lib;\n\t\tlib.shader_handle = shaders::rt_ao_lib;\n\t\tlib.exports.push_back(L\"AORaygenEntry\");\n\t\tlib.exports.push_back(L\"MissEntry\");\n\t\treturn lib;\n\t}();\n\n\tREGISTER(state_objects::rt_ao_state_opbject, RTPipelineRegistry)(\n\t{\n\t\t.desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE,\n\t\t.library_desc = rt_ao_so_library,\n\t\t.max_payload_size =  (sizeof(float) * 2), \n\t\t.max_attributes_size = sizeof(float) * 4,\n\t\t.max_recursion_depth = 1,\n\t\t.global_root_signature = root_signatures::rt_ao_global,\n\t\t.local_root_signatures = {},\n\t});\n\t\n\t/* ### Path Tracer ### */\n\tREGISTER(shaders::path_tracer_lib, ShaderRegistry)({\n\t\t.path = \"resources/shaders/dxr_pathtracer_main.hlsl\",\n\t\t.entry = \"RaygenEntry\",\n\t\t.type = ShaderType::LIBRARY_SHADER,\n\t\t.defines = {}\n\t});\n\n\tDESC_RANGE_ARRAY(path_tracer_ranges,\n\t\tDESC_RANGE(params::path_tracing, Type::UAV_RANGE, params::PathTracingE::OUTPUT),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::INDICES),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::LIGHTS),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::MATERIALS),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::OFFSETS),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::SKYBOX),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::PREF_ENV_MAP),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::BRDF_LUT),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::IRRADIANCE_MAP),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::TEXTURES),\n\t\tDESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::GBUFFERS),\n\t\tDESC_RANGE_H(D3D12_DESCRIPTOR_RANGE_TYPE::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 9, d3d12::settings::fallback_ptrs_offset),\n\t);\n\n\tREGISTER(root_signatures::path_tracing_global, RootSignatureRegistry)({\n\t\t.m_parameters = {\n\t\t\tROOT_PARAM_DESC_TABLE(path_tracer_ranges, D3D12_SHADER_VISIBILITY_ALL),\n\t\t\tROOT_PARAM(GetSRV(params::path_tracing, params::PathTracingE::ACCELERATION_STRUCTURE)),\n\t\t\tROOT_PARAM(GetCBV(params::path_tracing, params::PathTracingE::CAMERA_PROPERTIES)),\n\t\t\tROOT_PARAM(GetSRV(params::path_tracing, params::PathTracingE::VERTICES)),\n\t\t},\n\t\t.m_samplers = {\n\t\t\t{ TextureFilter::FILTER_ANISOTROPIC, TextureAddressMode::TAM_WRAP }\n\t\t},\n\t\t.m_rtx = true\n\t});\n\n\tStateObjectDescription::LibraryDesc path_tracer_so_library = []()\n\t{\n\t\tStateObjectDescription::LibraryDesc lib;\n\t\tlib.shader_handle = shaders::path_tracer_lib;\n\t\tlib.exports.push_back(L\"RaygenEntry\");\n\t\tlib.exports.push_back(L\"ReflectionHit\");\n\t\tlib.exports.push_back(L\"ReflectionMiss\");\n\t\tlib.exports.push_back(L\"ShadowClosestHitEntry\");\n\t\tlib.exports.push_back(L\"ShadowMissEntry\");\n\t\tlib.m_hit_groups.push_back({ L\"ReflectionHitGroup\", L\"ReflectionHit\" });\n\t\tlib.m_hit_groups.push_back({ L\"ShadowHitGroup\", L\"ShadowClosestHitEntry\" });\n\n\t\treturn lib;\n\t}();\n\n\tREGISTER(state_objects::path_tracer_state_object, RTPipelineRegistry)(\n\t{\n\t\t.desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE,\n\t\t.library_desc = path_tracer_so_library,\n\t\t.max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2),\n\t\t.max_attributes_size = sizeof(float) * 4,\n\t\t.max_recursion_depth = 6,\n\t\t.global_root_signature = root_signatures::path_tracing_global,\n\t\t.local_root_signatures = {},\n\t});\n\n\t/* ### Shadow Raytracing ### */\n\tREGISTER(shaders::rt_shadow_lib, ShaderRegistry)({\n\t\t.path = \"resources/shaders/dxr_shadow_main.hlsl\",\n\t\t.entry = \"ShadowRaygenEntry\",\n\t\t.type = ShaderType::LIBRARY_SHADER,\n\t\t.defines = {}\n\t\t});\n\t\n\tStateObjectDescription::LibraryDesc rt_shadow_so_library = []()\n\t{\n\t\tStateObjectDescription::LibraryDesc lib;\n\t\tlib.shader_handle = shaders::rt_shadow_lib;\n\t\tlib.exports.push_back(L\"ShadowRaygenEntry\");\n\t\tlib.exports.push_back(L\"ShadowClosestHitEntry\");\n\t\tlib.exports.push_back(L\"ShadowMissEntry\");\n\t\tlib.m_hit_groups.push_back({ L\"ShadowHitGroup\", L\"ShadowClosestHitEntry\" });\n\n\t\treturn lib;\n\t}();\n\tREGISTER(state_objects::rt_shadow_state_object, RTPipelineRegistry)(\n\t\t{\n\t\t\t.desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE,\n\t\t\t.library_desc = rt_shadow_so_library,\n\t\t\t.max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 3),\n\t\t\t.max_attributes_size = sizeof(float) * 4,\n\t\t\t.max_recursion_depth = 1,\n\t\t\t.global_root_signature = root_signatures::rt_hybrid_global,\n\t\t\t.local_root_signatures = {}\n\t\t});\n\n\t/* ### Reflection Raytracing ### */\n\tREGISTER(shaders::rt_reflection_lib, ShaderRegistry)({\n\t\t.path = \"resources/shaders/dxr_reflection_main.hlsl\",\n\t\t.entry = \"ReflectionRaygenEntry\",\n\t\t.type = ShaderType::LIBRARY_SHADER,\n\t\t.defines = {}\n\t\t});\n\n\tStateObjectDescription::LibraryDesc rt_reflection_so_library = []()\n\t{\n\t\tStateObjectDescription::LibraryDesc lib;\n\t\tlib.shader_handle = shaders::rt_reflection_lib;\n\t\tlib.exports.push_back(L\"ReflectionRaygenEntry\");\n\t\tlib.exports.push_back(L\"ReflectionHit\");\n\t\tlib.exports.push_back(L\"ReflectionMiss\");\n\t\tlib.exports.push_back(L\"ShadowClosestHitEntry\");\n\t\tlib.exports.push_back(L\"ShadowMissEntry\");\n\t\tlib.m_hit_groups.push_back({ L\"ReflectionHitGroup\", L\"ReflectionHit\" });\n\t\tlib.m_hit_groups.push_back({ L\"ShadowHitGroup\", L\"ShadowClosestHitEntry\" });\n\n\t\treturn lib;\n\t}();\n\tREGISTER(state_objects::rt_reflection_state_object, RTPipelineRegistry)(\n\t\t{\n\t\t\t.desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE,\n\t\t\t.library_desc = rt_reflection_so_library,\n\t\t\t.max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 3),\n\t\t\t.max_attributes_size = sizeof(float) * 4,\n\t\t\t.max_recursion_depth = 3,\n\t\t\t.global_root_signature = root_signatures::rt_hybrid_global,\n\t\t\t.local_root_signatures = {}\n\t\t});\n\n} /* wr */\n"
  },
  {
    "path": "src/engine_registry.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <array>\n\n#include \"registry.hpp\"\n#include \"d3d12/d3dx12.hpp\"\n#include \"d3d12/d3d12_settings.hpp\"\n\n#define COMPILATION_EVAL(e) (std::integral_constant<decltype(e), e>::value)\n\nnamespace wr\n{\n\n\tnamespace rs_layout\n\t{\n\n\t\tenum class Type\n\t\t{\n\t\t\tSRV,\n\t\t\tSRV_RANGE,\n\t\t\tCBV_OR_CONST,\n\t\t\tCBV_RANGE,\n\t\t\tUAV,\n\t\t\tUAV_RANGE,\n\t\t};\n\n\t\tstruct Entry\n\t\t{\n\t\t\tint name;\n\t\t\tint size;\n\t\t\tType type;\n\t\t};\n\n\t\ttemplate<typename T, typename E>\n\t\tconstexpr unsigned int GetStart(const T data, const E name)\n\t\t{\n\t\t\tType type = Type::CBV_OR_CONST;\n\t\t\tunsigned int start = 0;\n\n\t\t\t// Find Type\n\t\t\tfor (std::size_t i = 0; i < data.size(); i++)\n\t\t\t{\n\t\t\t\tauto entry = data[i];\n\t\t\t\tif (static_cast<E>(entry.name) == name)\n\t\t\t\t{\n\t\t\t\t\ttype = entry.type;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Find Start\n\t\t\tfor (std::size_t i = 0; i < data.size(); i++)\n\t\t\t{\n\t\t\t\tauto entry = data[i];\n\t\t\t\tif (static_cast<E>(entry.name) == name)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (entry.type == type)\n\t\t\t\t{\n\t\t\t\t\tstart += entry.size;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn start;\n\t\t}\n\n\t\ttemplate<typename T, typename E>\n\t\tconstexpr unsigned int GetHeapLoc(const T data, const E name)\n\t\t{\n\t\t\tunsigned int start = 0;\n\n\t\t\t// Find Start\n\t\t\tfor (std::size_t i = 0; i < data.size(); i++)\n\t\t\t{\n\t\t\t\tauto entry = data[i];\n\t\t\t\tif (static_cast<E>(entry.name) == name)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (entry.type == Type::CBV_RANGE || entry.type == Type::SRV_RANGE || entry.type == Type::UAV_RANGE)\n\t\t\t\t{\n\t\t\t\t\tstart += entry.size;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn start;\n\t\t}\n\n\t\ttemplate<typename T, typename E>\n\t\tconstexpr unsigned int GetSize(const T data, const E name)\n\t\t{\n\t\t\tfor (std::size_t i = 0; i < data.size(); i++)\n\t\t\t{\n\t\t\t\tauto entry = data[i];\n\t\t\t\tif (static_cast<E>(entry.name) == name)\n\t\t\t\t{\n\t\t\t\t\treturn entry.size;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn 0;\n\t\t}\n\n\t\ttemplate<typename T, typename E>\n\t\tconstexpr CD3DX12_DESCRIPTOR_RANGE GetRange(const T data, const Type type, const E name)\n\t\t{\n\t\t\tunsigned int start = 0;\n\t\t\tunsigned int size = 0;\n\n\t\t\t// Find its range equivelant or visa versa\n            Type other_type = Type::SRV;\n\t\t\tswitch (type)\n\t\t\t{\n\t\t\tcase Type::SRV:\n\t\t\t\tother_type = Type::SRV_RANGE;\n\t\t\t\tbreak;\n\t\t\tcase Type::SRV_RANGE:\n\t\t\t\tother_type = Type::SRV;\n\t\t\t\tbreak;\n\t\t\tcase Type::CBV_OR_CONST:\n\t\t\t\tother_type = Type::CBV_RANGE;\n\t\t\t\tbreak;\n\t\t\tcase Type::CBV_RANGE:\n\t\t\t\tother_type = Type::CBV_OR_CONST;\n\t\t\t\tbreak;\n\t\t\tcase Type::UAV:\n\t\t\t\tother_type = Type::UAV_RANGE;\n\t\t\t\tbreak;\n\t\t\tcase Type::UAV_RANGE:\n\t\t\t\tother_type = Type::UAV;\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\t// Find Start & Size\n\t\t\tfor (std::size_t i = 0; i < data.size(); i++)\n\t\t\t{\n\t\t\t\tauto entry = data[i];\n\t\t\t\tif (static_cast<E>(entry.name) == name)\n\t\t\t\t{\n\t\t\t\t\tsize = entry.size;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (entry.type == type || entry.type == other_type)\n\t\t\t\t{\n\t\t\t\t\tstart += entry.size;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tD3D12_DESCRIPTOR_RANGE_TYPE d3d12_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;\n\t\t\tswitch (type)\n\t\t\t{\n\t\t\tcase Type::SRV_RANGE:\n\t\t\t\td3d12_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;\n\t\t\t\tbreak;\n\t\t\tcase Type::UAV_RANGE:\n\t\t\t\td3d12_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;\n\t\t\t\tbreak;\n\t\t\tcase Type::CBV_RANGE:\n\t\t\t\td3d12_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tLOGW(\"Unknown range type\");\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tCD3DX12_DESCRIPTOR_RANGE retval;\n\t\t\tretval.Init(d3d12_range_type, size, start);\n\t\t\treturn retval;\n\t\t}\n\n\t\ttemplate<typename T, typename E>\n\t\tconstexpr CD3DX12_ROOT_PARAMETER GetCBV(const T data, const E name, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n\t\t{\n\t\t\tunsigned int start = 0;\n\n\t\t\t// Find Start & Size\n\t\t\tfor (std::size_t i = 0; i < data.size(); i++)\n\t\t\t{\n\t\t\t\tauto entry = data[i];\n\t\t\t\tif (static_cast<E>(entry.name) == name)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (entry.type == Type::CBV_OR_CONST || entry.type == Type::CBV_RANGE)\n\t\t\t\t{\n\t\t\t\t\tstart += entry.size;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tCD3DX12_ROOT_PARAMETER retval;\n\t\t\tretval.InitAsConstantBufferView(start, 0, visibility);\n\n\t\t\treturn retval;\n\t\t}\n\n\t\ttemplate<typename T, typename E>\n\t\tconstexpr CD3DX12_ROOT_PARAMETER GetSRV(const T data, const E name, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n\t\t{\n\t\t\tunsigned int start = 0;\n\n\t\t\t// Find Start & Size\n\t\t\tfor (std::size_t i = 0; i < data.size(); i++)\n\t\t\t{\n\t\t\t\tauto entry = data[i];\n\t\t\t\tif (static_cast<E>(entry.name) == name)\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (entry.type == Type::SRV || entry.type == Type::SRV_RANGE)\n\t\t\t\t{\n\t\t\t\t\tstart += entry.size;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tCD3DX12_ROOT_PARAMETER retval;\n\t\t\tretval.InitAsShaderResourceView(start, 0, visibility);\n\n\t\t\treturn retval;\n\t\t}\n\n\t\ttemplate<typename T, typename E>\n\t\tconstexpr CD3DX12_ROOT_PARAMETER GetConstants(const T data, const E name, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)\n\t\t{\n\t\t\tunsigned int start = 0;\n\t\t\tunsigned int size = 0;\n\n\t\t\t// Find Start\n\t\t\tfor (std::size_t i = 0; i < data.size(); i++)\n\t\t\t{\n\t\t\t\tauto entry = data[i];\n\t\t\t\tif (static_cast<E>(entry.name) == name)\n\t\t\t\t{\n\t\t\t\t\tsize = entry.size;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse if (entry.type == Type::CBV_OR_CONST || entry.type == Type::CBV_RANGE)\n\t\t\t\t{\n\t\t\t\t\tstart += entry.size;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tCD3DX12_ROOT_PARAMETER retval;\n\t\t\tretval.InitAsConstants(size, start, 0, visibility);\n\n\t\t\treturn retval;\n\t\t}\n\n\t} /* rs_layout */\n\n\tnamespace params\n\t{\n\t\tenum class BRDF_LutE\n\t\t{\n\t\t\tOUTPUT,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 1> brdf_lut = {\n\t\t\trs_layout::Entry{(int)BRDF_LutE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t};\n\n\n\t\tenum class DeferredMainE\n\t\t{\n\t\t\tCAMERA_PROPERTIES,\n\t\t\tOBJECT_PROPERTIES,\n\t\t\tALBEDO,\n\t\t\tNORMAL,\n\t\t\tROUGHNESS,\n\t\t\tMETALLIC,\n\t\t\tAMBIENT_OCCLUSION,\n\t\t\tEMISSIVE,\n\t\t\tMATERIAL_PROPERTIES,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 9> deferred_main = {\n\t\t\trs_layout::Entry{(int)DeferredMainE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)DeferredMainE::OBJECT_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)DeferredMainE::ALBEDO, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredMainE::NORMAL, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredMainE::ROUGHNESS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredMainE::METALLIC, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredMainE::AMBIENT_OCCLUSION, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredMainE::EMISSIVE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredMainE::MATERIAL_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class SVGFDenoiserE\n\t\t{\n\t\t\tINPUT,\n\t\t\tMOTION,\n\t\t\tNORMAL,\n\t\t\tDEPTH,\n\n\t\t\tIN_HIST_LENGTH,\n\n\t\t\tPREV_INPUT,\n\t\t\tPREV_MOMENTS,\n\t\t\tPREV_NORMAL,\n\t\t\tPREV_DEPTH,\n\n\t\t\tOUT_COLOR,\n\t\t\tOUT_MOMENTS,\n\t\t\tOUT_HIST_LENGTH,\n\n\t\t\tSVGF_PROPERTIES,\n\n\t\t\tPING_PONG_UAV,\n\t\t\tPING_PONG_SRV,\n\t\t\tOUTPUT_SRV,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 16> svgf_denoiser = {\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::INPUT, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::MOTION, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::NORMAL, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::DEPTH, 1, rs_layout::Type::SRV_RANGE},\n\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::IN_HIST_LENGTH, 1, rs_layout::Type::SRV_RANGE},\n\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::PREV_INPUT, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::PREV_MOMENTS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::PREV_NORMAL, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::PREV_DEPTH, 1, rs_layout::Type::SRV_RANGE},\n\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::OUT_COLOR, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::OUT_MOMENTS, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::OUT_HIST_LENGTH, 1, rs_layout::Type::UAV_RANGE},\n\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::SVGF_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::PING_PONG_UAV, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::PING_PONG_SRV, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)SVGFDenoiserE::OUTPUT_SRV, 1, rs_layout::Type::SRV_RANGE},\n\t\t};\n\n\t\tenum class DeferredCompositionE\n\t\t{\n\t\t\tCAMERA_PROPERTIES,\n\t\t\tGBUFFER_ALBEDO_ROUGHNESS,\n\t\t\tGBUFFER_NORMAL_METALLIC,\n\t\t\tGBUFFER_EMISSIVE_AO,\n\t\t\tGBUFFER_DEPTH,\n\t\t\tLIGHT_BUFFER,\n\t\t\tSKY_BOX,\n\t\t\tIRRADIANCE_MAP,\n\t\t\tPREF_ENV_MAP,\n\t\t\tBRDF_LUT,\n\t\t\tBUFFER_REFLECTION,\n\t\t\tBUFFER_SHADOW,\n\t\t\tBUFFER_SCREEN_SPACE_IRRADIANCE,\n\t\t\tBUFFER_AO,\n\t\t\tOUTPUT,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 15> deferred_composition = {\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::GBUFFER_ALBEDO_ROUGHNESS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::GBUFFER_NORMAL_METALLIC, 1, rs_layout::Type::SRV_RANGE},\t\t\t\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::GBUFFER_EMISSIVE_AO, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::GBUFFER_DEPTH, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::LIGHT_BUFFER, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::SKY_BOX, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::IRRADIANCE_MAP, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::PREF_ENV_MAP, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::BRDF_LUT, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::BUFFER_REFLECTION, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::BUFFER_SHADOW, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::BUFFER_SCREEN_SPACE_IRRADIANCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::BUFFER_AO, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DeferredCompositionE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}\n\t\t};\n\n\t\tenum class MipMappingE\n\t\t{\n\t\t\tSOURCE,\n\t\t\tDEST,\n\t\t\tCBUFFER,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 3> mip_mapping = {\n\t\t\trs_layout::Entry{(int)MipMappingE::SOURCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)MipMappingE::DEST, 4, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)MipMappingE::CBUFFER, 6, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class CubemapConversionE\n\t\t{\n\t\t\tEQUIRECTANGULAR_TEXTURE,\n\t\t\tIDX,\n\t\t\tCAMERA_PROPERTIES,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 3> cubemap_conversion = {\n\t\t\trs_layout::Entry{(int)CubemapConversionE::EQUIRECTANGULAR_TEXTURE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)CubemapConversionE::IDX, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)CubemapConversionE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class CubemapConvolutionE\n\t\t{\n\t\t\tENVIRONMENT_CUBEMAP,\n\t\t\tIDX,\n\t\t\tCAMERA_PROPERTIES,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 3> cubemap_convolution = {\n\t\t\trs_layout::Entry{(int)CubemapConvolutionE::ENVIRONMENT_CUBEMAP, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)CubemapConvolutionE::IDX, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)CubemapConvolutionE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class CubemapPrefilteringE\n\t\t{\n\t\t\tSOURCE,\n\t\t\tDEST,\n\t\t\tCBUFFER,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 3> cubemap_prefiltering = {\n\t\t\trs_layout::Entry{(int)CubemapPrefilteringE::SOURCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)CubemapPrefilteringE::DEST, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)CubemapPrefilteringE::CBUFFER, 6, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class PostProcessingE\n\t\t{\n\t\t\tSOURCE,\n\t\t\tDEST,\n\t\t\tHDR_SUPPORT,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 3> post_processing = {\n\t\t\trs_layout::Entry{(int)PostProcessingE::SOURCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PostProcessingE::DEST, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)PostProcessingE::HDR_SUPPORT, 2, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class AccumulationE\n\t\t{\n\t\t\tSOURCE,\n\t\t\tDEST,\n\t\t\tFRAME_IDX,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 3> accumulation = {\n\t\t\trs_layout::Entry{(int)AccumulationE::SOURCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)AccumulationE::DEST, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)AccumulationE::FRAME_IDX, 2, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class FullRaytracingE\n\t\t{\n\t\t\tCAMERA_PROPERTIES,\n\t\t\tACCELERATION_STRUCTURE,\n\t\t\tOUTPUT,\n\t\t\tINDICES,\n\t\t\tVERTICES,\n\t\t\tLIGHTS,\n\t\t\tMATERIALS,\n\t\t\tOFFSETS,\n\t\t\tSKYBOX,\n\t\t\tBRDF_LUT,\n\t\t\tIRRADIANCE_MAP,\n\t\t\tTEXTURES,\n\t\t\tFALLBACK_PTRS\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 13> full_raytracing = {\n\t\t\trs_layout::Entry{(int)FullRaytracingE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::ACCELERATION_STRUCTURE, 1, rs_layout::Type::SRV},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::INDICES, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::LIGHTS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::VERTICES, 1, rs_layout::Type::SRV},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::MATERIALS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::OFFSETS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::SKYBOX, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::BRDF_LUT, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::IRRADIANCE_MAP, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::TEXTURES, d3d12::settings::num_max_rt_textures, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)FullRaytracingE::FALLBACK_PTRS, 5, rs_layout::Type::SRV_RANGE},\n\t\t};\n\n\t\tenum class RTHybridE\n\t\t{\n\t\t\tCAMERA_PROPERTIES,\n\t\t\tACCELERATION_STRUCTURE,\n\t\t\tOUTPUT,\n\t\t\tINDICES,\n\t\t\tVERTICES,\n\t\t\tLIGHTS,\n\t\t\tMATERIALS,\n\t\t\tOFFSETS,\n\t\t\tSKYBOX,\n\t\t\tPREF_ENV_MAP,\n\t\t\tBRDF_LUT,\n\t\t\tIRRADIANCE_MAP,\n\t\t\tTEXTURES,\n\t\t\tGBUFFERS,\n\t\t\tFALLBACK_PTRS\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 20> rt_hybrid = {\n\t\t\trs_layout::Entry{(int)RTHybridE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)RTHybridE::OUTPUT, 3, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::ACCELERATION_STRUCTURE, 1, rs_layout::Type::SRV},\n\t\t\trs_layout::Entry{(int)RTHybridE::INDICES, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::LIGHTS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::VERTICES, 1, rs_layout::Type::SRV},\n\t\t\trs_layout::Entry{(int)RTHybridE::MATERIALS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::OFFSETS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::SKYBOX, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::PREF_ENV_MAP, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::BRDF_LUT, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::IRRADIANCE_MAP, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::TEXTURES, d3d12::settings::num_max_rt_textures, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::GBUFFERS, 3, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTHybridE::FALLBACK_PTRS, 9, rs_layout::Type::SRV_RANGE},\n\t\t};\n\n\t\tenum class SpatialReconstructionE\n\t\t{\n\t\t\tCAMERA_PROPERTIES,\n\t\t\tOUTPUT,\n\t\t\tREFLECTION_BUFFER,\n\t\t\tGBUFFERS\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 4> spatial_reconstruction = {\n\t\t\trs_layout::Entry{(int)SpatialReconstructionE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)SpatialReconstructionE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)SpatialReconstructionE::REFLECTION_BUFFER, 2, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)SpatialReconstructionE::GBUFFERS, 3, rs_layout::Type::SRV_RANGE}\n\t\t};\n\n    enum class ReflectionDenoiserE\n    {\n\t\tINPUT,\n\t\tRAY_RAW,\n\t\tRAY_DIR,\n\t\tALBEDO_ROUGHNESS,\n\t\tNORMAL_METALLIC,\n\t\tMOTION,\n\t\tLINEAR_DEPTH,\n\t\tWORLD_POS,\n\n\t\tIN_HISTORY,\n\n\t\tACCUM,\n\t\tPREV_NORMAL,\n\t\tPREV_DEPTH,\n\t\tIN_MOMENTS,\n\n\t\tOUTPUT,\n\t\tOUT_HISTORY,\n\t\tOUT_MOMENTS,\n\n\t\tCAMERA_PROPERTIES,\n\t\tDENOISER_SETTINGS,\n\t\tWAVELET_ITERATION,\n    };\n\n    constexpr std::array<rs_layout::Entry, 19> reflection_denoiser = {\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::INPUT, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::RAY_RAW, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::RAY_DIR, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::ALBEDO_ROUGHNESS, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::NORMAL_METALLIC, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::MOTION, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::LINEAR_DEPTH, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::WORLD_POS, 1, rs_layout::Type::SRV_RANGE},\n\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::IN_HISTORY, 1, rs_layout::Type::SRV_RANGE},\n\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::ACCUM, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::PREV_NORMAL, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::PREV_DEPTH, 1, rs_layout::Type::SRV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::IN_MOMENTS, 1, rs_layout::Type::SRV_RANGE},\n\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::OUT_HISTORY, 1, rs_layout::Type::UAV_RANGE},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::OUT_MOMENTS, 1, rs_layout::Type::UAV_RANGE},\n\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::DENOISER_SETTINGS, 1, rs_layout::Type::CBV_OR_CONST},\n\t\trs_layout::Entry{(int)ReflectionDenoiserE::WAVELET_ITERATION, 1, rs_layout::Type::CBV_OR_CONST},\n    };\n\n\t\tenum class RTAOE\n\t\t{\n\t\t\tCAMERA_PROPERTIES,\n\t\t\tACCELERATION_STRUCTURE,\n\t\t\tOUTPUT,\n\t\t\tGBUFFERS,\n\t\t\tFALLBACK_PTRS\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 5> rt_ao = {\n\t\t\trs_layout::Entry{(int)RTAOE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)RTAOE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)RTAOE::ACCELERATION_STRUCTURE, 1, rs_layout::Type::SRV},\n\t\t\trs_layout::Entry{(int)RTAOE::GBUFFERS, 2, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)RTAOE::FALLBACK_PTRS, 9, rs_layout::Type::SRV_RANGE},\n\t\t};\n\n\t\tenum class PathTracingE\n\t\t{\n\t\t\tCAMERA_PROPERTIES,\n\t\t\tACCELERATION_STRUCTURE,\n\t\t\tOUTPUT,\n\t\t\tINDICES,\n\t\t\tVERTICES,\n\t\t\tLIGHTS,\n\t\t\tMATERIALS,\n\t\t\tOFFSETS,\n\t\t\tSKYBOX,\n\t\t\tPREF_ENV_MAP,\n\t\t\tBRDF_LUT,\n\t\t\tIRRADIANCE_MAP,\n\t\t\tTEXTURES,\n\t\t\tGBUFFERS,\n\t\t\tFALLBACK_PTRS\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 20> path_tracing = {\n\t\t\trs_layout::Entry{(int)PathTracingE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t\trs_layout::Entry{(int)PathTracingE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, // TEMPORARY: This should be 1. its 2 so the path tracer doesn't overwrite it.\n\t\t\trs_layout::Entry{(int)PathTracingE::ACCELERATION_STRUCTURE, 1, rs_layout::Type::SRV},\n\t\t\trs_layout::Entry{(int)PathTracingE::INDICES, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::LIGHTS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::VERTICES, 1, rs_layout::Type::SRV},\n\t\t\trs_layout::Entry{(int)PathTracingE::MATERIALS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::OFFSETS, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::SKYBOX, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::PREF_ENV_MAP, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::BRDF_LUT, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::IRRADIANCE_MAP, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::TEXTURES, d3d12::settings::num_max_rt_textures, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::GBUFFERS, 4, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)PathTracingE::FALLBACK_PTRS, 9, rs_layout::Type::SRV_RANGE},\n\t\t};\n\n\t\tenum class DoFCoCE\n\t\t{\n\t\t\tCAMERA_PROPERTIES,\n\t\t\tGDEPTH,\n\t\t\tOUTPUT\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 3> dof_coc = {\n\t\t\trs_layout::Entry{(int)DoFCoCE::GDEPTH, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFCoCE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFCoCE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}\n\t\t};\n\n\t\tenum class DoFNearMaskE\n\t\t{\n\t\t\tINPUT,\n\t\t\tOUTPUT\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 2> dof_near_mask = {\n\t\t\trs_layout::Entry{(int)DoFNearMaskE::INPUT, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFNearMaskE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\n\t\t};\n\n\t\tenum class DownScaleE\n\t\t{\n\t\t\tSOURCE,\n\t\t\tOUTPUT_NEAR,\n\t\t\tOUTPUT_FAR,\n\t\t\tCOC,\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry,4> down_scale = {\n\t\t\trs_layout::Entry{(int)DownScaleE::SOURCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DownScaleE::OUTPUT_NEAR, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)DownScaleE::OUTPUT_FAR, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)DownScaleE::COC, 1, rs_layout::Type::SRV_RANGE},\n\t\t};\n\n\n\n\t\tenum class DoFDilateE\n\t\t{\n\t\t\tSOURCE,\n\t\t\tOUTPUT\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 2> dof_dilate = {\n\t\t\trs_layout::Entry{(int)DoFDilateE::SOURCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFDilateE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t};\n\n\t\tenum class DoFBokehE\n\t\t{\n\t\t\tCAMERA_PROPERTIES,\n\t\t\tSOURCE_NEAR,\n\t\t\tSOURCE_FAR,\n\t\t\tOUTPUT_NEAR,\n\t\t\tOUTPUT_FAR,\n\t\t\tCOC\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 6> dof_bokeh = {\n\t\t\trs_layout::Entry{(int)DoFBokehE::SOURCE_NEAR, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFBokehE::SOURCE_FAR, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFBokehE::OUTPUT_NEAR, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFBokehE::OUTPUT_FAR, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFBokehE::COC, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFBokehE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class DoFBokehPostFilterE\n\t\t{\n\t\t\tSOURCE_NEAR,\n\t\t\tSOURCE_FAR,\n\t\t\tOUTPUT_NEAR,\n\t\t\tOUTPUT_FAR\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 4> dof_bokeh_post_filter = {\n\t\t\trs_layout::Entry{(int)DoFBokehPostFilterE::SOURCE_NEAR, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFBokehPostFilterE::SOURCE_FAR, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFBokehPostFilterE::OUTPUT_NEAR, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFBokehPostFilterE::OUTPUT_FAR, 1, rs_layout::Type::UAV_RANGE}\n\t\t};\n\n\t\tenum class DoFCompositionE\n\t\t{\n\t\t\tSOURCE,\n\t\t\tOUTPUT,\n\t\t\tBOKEH_NEAR,\n\t\t\tBOKEH_FAR,\n\t\t\tCOC\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 5> dof_composition = {\n\t\t\trs_layout::Entry{(int)DoFCompositionE::SOURCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFCompositionE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFCompositionE::BOKEH_NEAR, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFCompositionE::BOKEH_FAR, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)DoFCompositionE::COC, 1, rs_layout::Type::SRV_RANGE},\n\t\t};\n\n\t\tenum class BloomExtractBrightE\n\t\t{\n\t\t\tSOURCE,\n\t\t\tG_EMISSIVE,\n\t\t\tG_DEPTH,\n\t\t\tOUTPUT_BRIGHT\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 4> bloom_extract_bright = {\n\t\t\trs_layout::Entry{(int)BloomExtractBrightE::SOURCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomExtractBrightE::G_EMISSIVE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomExtractBrightE::G_DEPTH, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomExtractBrightE::OUTPUT_BRIGHT, 1, rs_layout::Type::UAV_RANGE},\n\t\t};\n\t\tenum class BloomBlurE\n\t\t{\n\t\t\tSOURCE,\n\t\t\tOUTPUT,\n\t\t\tBLUR_DIRECTION\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 3> bloom_blur = {\n\t\t\trs_layout::Entry{(int)BloomBlurE::SOURCE, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomBlurE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomBlurE::BLUR_DIRECTION, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class BloomBlurHorizontalE\n\t\t{\n\t\t\tBLOOM_PROPERTIES,\n\t\t\tSOURCE_MAIN,\n\t\t\tOUTPUT,\n\t\t\tOUTPUT_QES\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 4> bloom_blur_horizontal = {\n\t\t\trs_layout::Entry{(int)BloomBlurHorizontalE::SOURCE_MAIN, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomBlurHorizontalE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomBlurHorizontalE::OUTPUT_QES, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomBlurHorizontalE::BLOOM_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class BloomBlurVerticalE\n\t\t{\n\t\t\tBLOOM_PROPERTIES,\n\t\t\tSOURCE_MAIN,\n\t\t\tSOURCE_QES,\n\t\t\tOUTPUT,\n\t\t\tOUTPUT_QES\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 5> bloom_blur_vertical = {\n\t\t\trs_layout::Entry{(int)BloomBlurVerticalE::SOURCE_MAIN, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomBlurVerticalE::SOURCE_QES, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomBlurVerticalE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomBlurVerticalE::OUTPUT_QES, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomBlurVerticalE::BLOOM_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t\tenum class BloomCompositionE\n\t\t{\n\t\t\tBLOOM_PROPERTIES,\n\t\t\tSOURCE_MAIN,\n\t\t\tSOURCE_BLOOM_HALF,\n\t\t\tSOURCE_BLOOM_QES,\n\t\t\tOUTPUT\n\t\t};\n\n\t\tconstexpr std::array<rs_layout::Entry, 5> bloom_composition = {\n\t\t\trs_layout::Entry{(int)BloomCompositionE::SOURCE_MAIN, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomCompositionE::SOURCE_BLOOM_HALF, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomCompositionE::SOURCE_BLOOM_QES, 1, rs_layout::Type::SRV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomCompositionE::OUTPUT, 1, rs_layout::Type::UAV_RANGE},\n\t\t\trs_layout::Entry{(int)BloomCompositionE::BLOOM_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST},\n\t\t};\n\n\t} /* srv */\n\n\tstruct root_signatures\n\t{\n\t\tWISPRENDERER_EXPORT static RegistryHandle brdf_lut;\n\t\tWISPRENDERER_EXPORT static RegistryHandle basic;\n\t\tWISPRENDERER_EXPORT static RegistryHandle svgf_denoiser;\n\t\tWISPRENDERER_EXPORT static RegistryHandle deferred_composition;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_test_global;\n\t\tWISPRENDERER_EXPORT static RegistryHandle mip_mapping;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_hybrid_global;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_ao_global;\n\t\tWISPRENDERER_EXPORT static RegistryHandle path_tracing_global;\n\t\tWISPRENDERER_EXPORT static RegistryHandle cubemap_conversion;\n\t\tWISPRENDERER_EXPORT static RegistryHandle cubemap_convolution;\n\t\tWISPRENDERER_EXPORT static RegistryHandle cubemap_prefiltering;\n\t\tWISPRENDERER_EXPORT static RegistryHandle post_processing;\n\t\tWISPRENDERER_EXPORT static RegistryHandle accumulation;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_coc;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_near_mask;\n\t\tWISPRENDERER_EXPORT static RegistryHandle down_scale;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_dilate;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_bokeh;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_bokeh_post_filter;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_composition;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_extract_bright;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_blur;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_blur_horizontal;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_blur_vertical;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_composition;\n\t\tWISPRENDERER_EXPORT static RegistryHandle spatial_reconstruction;\n    WISPRENDERER_EXPORT static RegistryHandle reflection_denoiser;\n\t};\n\n\tstruct shaders\n\t{\n\t\tWISPRENDERER_EXPORT static RegistryHandle brdf_lut_cs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle basic_deferred_vs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle basic_deferred_ps;\n\t\tWISPRENDERER_EXPORT static RegistryHandle basic_hybrid_vs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle basic_hybrid_ps;\n\t\tWISPRENDERER_EXPORT static RegistryHandle fullscreen_quad_vs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_reprojection_cs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_filter_moments_cs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_wavelet_filter_cs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle deferred_composition_cs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_lib;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_ao_lib;\n\t\tWISPRENDERER_EXPORT static RegistryHandle path_tracer_lib;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_shadow_lib;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_reflection_lib;\n\t\tWISPRENDERER_EXPORT static RegistryHandle mip_mapping_cs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle equirect_to_cubemap_vs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle equirect_to_cubemap_ps;\n\t\tWISPRENDERER_EXPORT static RegistryHandle cubemap_convolution_ps;\n\t\tWISPRENDERER_EXPORT static RegistryHandle cubemap_prefiltering_cs;\n\t\tWISPRENDERER_EXPORT static RegistryHandle post_processing;\n\t\tWISPRENDERER_EXPORT static RegistryHandle accumulation;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_coc;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_near_mask;\n\t\tWISPRENDERER_EXPORT static RegistryHandle down_scale;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_dilate;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_bokeh;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_bokeh_post_filter;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_composition;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_extract_bright;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_blur;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_blur_horizontal;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_blur_vertical;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_composition;\n\t\tWISPRENDERER_EXPORT static RegistryHandle spatial_reconstruction;\n\t\tWISPRENDERER_EXPORT static RegistryHandle reflection_temporal_denoiser;\n\t\tWISPRENDERER_EXPORT static RegistryHandle reflection_variance_estimator;\n\t\tWISPRENDERER_EXPORT static RegistryHandle reflection_spatial_denoiser;\n\t};\n\n\tstruct pipelines\n\t{\n\t\tWISPRENDERER_EXPORT static RegistryHandle brdf_lut_precalculation;\n\t\tWISPRENDERER_EXPORT static RegistryHandle basic_deferred;\n\t\tWISPRENDERER_EXPORT static RegistryHandle basic_hybrid;\n\t\tWISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_reprojection;\n\t\tWISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_filter_moments;\n\t\tWISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_wavelet_filter;\n\t\tWISPRENDERER_EXPORT static RegistryHandle deferred_composition;\n\t\tWISPRENDERER_EXPORT static RegistryHandle mip_mapping;\n\t\tWISPRENDERER_EXPORT static RegistryHandle equirect_to_cubemap;\n\t\tWISPRENDERER_EXPORT static RegistryHandle cubemap_convolution;\n\t\tWISPRENDERER_EXPORT static RegistryHandle cubemap_prefiltering;\n\t\tWISPRENDERER_EXPORT static RegistryHandle post_processing;\n\t\tWISPRENDERER_EXPORT static RegistryHandle accumulation;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_coc;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_near_mask;\n\t\tWISPRENDERER_EXPORT static RegistryHandle down_scale;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_dilate;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_bokeh;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_bokeh_post_filter;\n\t\tWISPRENDERER_EXPORT static RegistryHandle dof_composition;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_extract_bright;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_blur;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_blur_horizontal;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_blur_vertical;\n\t\tWISPRENDERER_EXPORT static RegistryHandle bloom_composition;\n\t\tWISPRENDERER_EXPORT static RegistryHandle spatial_reconstruction;\n\t\tWISPRENDERER_EXPORT static RegistryHandle reflection_temporal_denoiser;\n\t\tWISPRENDERER_EXPORT static RegistryHandle reflection_variance_estimator;\n\t\tWISPRENDERER_EXPORT static RegistryHandle reflection_spatial_denoiser;\n\t};\n\n\tstruct state_objects\n\t{\n\t\tWISPRENDERER_EXPORT static RegistryHandle state_object;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_ao_state_opbject;\n\t\tWISPRENDERER_EXPORT static RegistryHandle path_tracing_state_object;\n\t\tWISPRENDERER_EXPORT static RegistryHandle path_tracer_state_object;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_shadow_state_object;\n\t\tWISPRENDERER_EXPORT static RegistryHandle rt_reflection_state_object;\n\t};\n\n} /* wr */\n\n"
  },
  {
    "path": "src/entry.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#define WISP_ENTRY(func)\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\nint main()\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n\treturn func();\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\nint CALLBACK WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR, _In_ int)\t\\\n{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n\treturn main();\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n}"
  },
  {
    "path": "src/frame_graph/frame_graph.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <vector>\n#include <string>\n#include <type_traits>\n#include <stack>\n#include <deque>\n#include <future>\n#include <any>\n\n#include \"../util/thread_pool.hpp\"\n#include \"../util/delegate.hpp\"\n#include \"../renderer.hpp\"\n#include \"../platform_independend_structs.hpp\"\n#include \"../settings.hpp\"\n#include \"../d3d12/d3d12_settings.hpp\"\n#include \"../structs.hpp\"\n#include \"../wisprenderer_export.hpp\"\n\n#ifndef _DEBUG\n#define FG_MAX_PERFORMANCE\n#endif\n\ntemplate<typename ...Ts>\nstd::vector<std::reference_wrapper<const std::type_info>> FG_DEPS() {\n\treturn { (typeid(Ts))... };\n}\n\nnamespace wr\n{\n\tenum class RenderTaskType\n\t{\n\t\tDIRECT,\n\t\tCOMPUTE,\n\t\tCOPY\n\t};\n\n\tenum class CPUTextureType\n\t{\n\t\tPIXEL_DATA,\n\t\tDEPTH_DATA\n\t};\n\n\tstruct CPUTextures\n\t{\n\t\tstd::optional<CPUTexture> pixel_data = std::nullopt;\n\t\tstd::optional<CPUTexture> depth_data = std::nullopt;\n\t};\n\n\t//! Typedef for the render task handle.\n\tusing RenderTaskHandle = std::uint32_t;\n\n\t// Forward declarations.\n\tclass FrameGraph;\n\n\t/*! Structure that describes a render task */\n\t/*!\n\t\tAll non default initialized member variables should be fully initialized to prevent undifined behaviour.\n\t*/\n\tstruct RenderTaskDesc\n\t{\n\t\t// Typedef the function pointer types to keep the code readable.\n\t\tusing setup_func_t =   util::Delegate<void(RenderSystem&, FrameGraph&, RenderTaskHandle, bool)>;\n\t\tusing execute_func_t = util::Delegate<void(RenderSystem&, FrameGraph&, SceneGraph&, RenderTaskHandle)>;\n\t\tusing destroy_func_t = util::Delegate<void(FrameGraph&, RenderTaskHandle, bool)>;\n\n\t\t/*! The type of the render task.*/\n\t\tRenderTaskType m_type = RenderTaskType::DIRECT;\n\t\t/*! The function pointers for the task.*/\n\t\tsetup_func_t m_setup_func;\n\t\texecute_func_t m_execute_func;\n\t\tdestroy_func_t m_destroy_func;\n\n\t\t/*! The properties for the render target this task renders to. If this is `std::nullopt` no render target will be created. */\n\t\tstd::optional<RenderTargetProperties> m_properties;\n\n\t\tbool m_allow_multithreading = true;\n\t};\n\n\t//!  Frame Graph \n\t/*!\n\t  The Frame Graph is responsible for managing all tasks the renderer should perform.\n\t  The idea is you can add tasks to the scene graph and when you call `RenderSystem::Render` it will run the tasks added.\n\t  It will not just run tasks but will also assign command lists and render targets to the tasks.\n\t  The Frame Graph is also capable of mulithreaded execution.\n\t  It will split the command lists that are allowed to be multithreaded on X amount of threads specified in `settings.hpp`\n\t*/\n\tclass FrameGraph\n\t{\n\t\t// Obtain the type definitions from `RenderTaskDesc` to keep the code readable.\n\t\tusing setup_func_t   = RenderTaskDesc::setup_func_t;\n\t\tusing execute_func_t = RenderTaskDesc::execute_func_t;\n\t\tusing destroy_func_t = RenderTaskDesc::destroy_func_t;\n\tpublic:\n\n\t\t//! Constructor.\n\t\t/*!\n\t\t\tThis constructor is able to reserve space for render tasks.\n\t\t\tThis works by calling `std::vector::reserve`.\n\t\t\t\\param num_reserved_tasks Amount of tasks we should reserve space for.\n\t\t*/\n\t\tFrameGraph(std::size_t num_reserved_tasks = 1) :\n\t\t\tm_render_system(nullptr),\n\t\t\tm_num_tasks(0),\n\t\t\tm_thread_pool(new util::ThreadPool(settings::num_frame_graph_threads)),\n\t\t\tm_uid(GetFreeUID())\n\t\t{\n\t\t\t// lambda to simplify reserving space.\n\t\t\tauto reserve = [num_reserved_tasks](auto v) { v.reserve(num_reserved_tasks); };\n\n\t\t\t// Reserve space for all vectors.\n\t\t\treserve(m_setup_funcs);\n\t\t\treserve(m_execute_funcs);\n\t\t\treserve(m_destroy_funcs);\n\t\t\treserve(m_cmd_lists);\n\t\t\treserve(m_render_targets);\n\t\t\treserve(m_data);\n\t\t\treserve(m_data_type_info);\n#ifndef FG_MAX_PERFORMANCE\n\t\t\treserve(m_dependencies);\n\t\t\treserve(m_names);\n#endif\n\t\t\treserve(m_types);\n\t\t\treserve(m_rt_properties);\n\t\t\tm_settings = decltype(m_settings)(num_reserved_tasks, std::nullopt); // Resizing so I can initialize it with null since this is an optional value.\n\t\t\tm_futures.resize(num_reserved_tasks); // std::thread doesn't allow me to reserve memory for the vector. Hence I'm resizing.\n\t\t}\n\n\t\t//! Destructor\n\t\t/*!\n\t\t\tThis destructor destroys all the task data and the thread pool.\n\t\t\tIf you want to reuse the frame graph I recommend calling `FrameGraph::Destroy`\n\t\t*/\n\t\t~FrameGraph()\n\t\t{\n\t\t\tdelete m_thread_pool;\n\t\t\tDestroy();\n\t\t}\n\n\t\tFrameGraph(const FrameGraph&) = delete;\n\t\tFrameGraph(FrameGraph&&)\t  = delete;\n\n\t\tFrameGraph& operator=(const FrameGraph&) = delete;\n\t\tFrameGraph& operator=(FrameGraph&&)\t\t = delete;\n\n\t\t//! Setup the render tasks\n\t\t/*!\n\t\t\tCalls all setup function pointers and obtains the required render targets and command lists.\n\t\t\tIt is recommended to try to avoid calling this during runtime because it can cause stalls if the setup functions are expensive.\n\t\t\t\\param render_system The render system we want to use for rendering.\n\t\t*/\n\t\tinline void Setup(RenderSystem& render_system)\n\t\t{\n\t\t\tbool is_valid = Validate();\n\n\t\t\tif (!is_valid)\n\t\t\t{\n\t\t\t\tLOGE(\"Framegraph validation failed. Aborting setup.\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Resize these vectors since we know the end size already.\n\t\t\tm_cmd_lists.resize(m_num_tasks);\n\t\t\tm_should_execute.resize(m_num_tasks, true); // All tasks should execute by default.\n\t\t\tm_render_targets.resize(m_num_tasks);\n\t\t\tm_futures.resize(m_num_tasks);\n\t\t\tm_render_system = &render_system;\n\n\t\t\tauto get_command_list_from_render_system = [this](auto type)\n\t\t\t{\n\t\t\t\tswitch (type)\n\t\t\t\t{\n\t\t\t\tcase RenderTaskType::DIRECT:\n\t\t\t\t\treturn m_render_system->GetDirectCommandList(d3d12::settings::num_back_buffers);\n\t\t\t\tcase RenderTaskType::COMPUTE:\n\t\t\t\t\treturn m_render_system->GetComputeCommandList(d3d12::settings::num_back_buffers);\n\t\t\t\tcase RenderTaskType::COPY:\n\t\t\t\t\treturn m_render_system->GetCopyCommandList(d3d12::settings::num_back_buffers);\n\t\t\t\tdefault:\n\t\t\t\t\tLOGC(\"Tried creating a command list of a type that is not supported.\");\n\t\t\t\t\treturn static_cast<wr::CommandList*>(nullptr);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tif constexpr (settings::use_multithreading)\n\t\t\t{\n\t\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t\t{\n\t\t\t\t\t// Get the proper command list from the render system.\n\t\t\t\t\tm_cmd_lists[i] = get_command_list_from_render_system(m_types[i]);\n#ifndef FG_MAX_PERFORMANCE\n\t\t\t\t\trender_system.SetCommandListName(m_cmd_lists[i], m_names[i]);\n#endif\n\n\t\t\t\t\t// Get a render target from the render system.\n\t\t\t\t\tif (m_rt_properties[i].has_value())\n\t\t\t\t\t{\n\t\t\t\t\t\tm_render_targets[i] = render_system.GetRenderTarget(m_rt_properties[i].value());\n#ifndef FG_MAX_PERFORMANCE\n\t\t\t\t\t\trender_system.SetRenderTargetName(m_render_targets[i], m_names[i]);\n#endif\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tSetup_MT_Impl();\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t// Itterate over all the tasks.\n\t\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t\t{\n\t\t\t\t\t// Get the proper command list from the render system.\n\t\t\t\t\tm_cmd_lists[i] = get_command_list_from_render_system(m_types[i]);\n#ifndef FG_MAX_PERFORMANCE\n\t\t\t\t\trender_system.SetCommandListName(m_cmd_lists[i], m_names[i]);\n#endif\n\n\t\t\t\t\t// Get a render target from the render system.\n\t\t\t\t\tif (m_rt_properties[i].has_value())\n\t\t\t\t\t{\n\t\t\t\t\t\tm_render_targets[i] = render_system.GetRenderTarget(m_rt_properties[i].value());\n#ifndef FG_MAX_PERFORMANCE\n\t\t\t\t\t\trender_system.SetRenderTargetName(m_render_targets[i], m_names[i]);\n#endif\n\t\t\t\t\t}\n\n\t\t\t\t\t// Call the setup function pointer.\n\t\t\t\t\tm_setup_funcs[i](render_system, *this, i, false);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Finish the setup before continuing for savety.\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t{\n\t\t\t\tWaitForCompletion(i);\n\t\t\t}\n\t\t}\n\n\t\t/*! Execute all render tasks */\n\t\t/*!\n\t\t\tFor every render task call the setup function pointers and tell the render system we started a render task of a certain type.\n\t\t\t\\param render_system The render system we want to use for rendering.\n\t\t\t\\param scene_graph The scene graph we want to render.\n\t\t*/\n\t\tinline void Execute(SceneGraph& scene_graph)\n\t\t{\n\t\t\tResetOutputTexture();\n\n\t\t\t// Check if we need to disable some tasks\n\t\t\twhile (!m_should_execute_change_request.empty())\n\t\t\t{\n\t\t\t\tauto front = m_should_execute_change_request.front();\n\t\t\t\tm_should_execute[front.first] = front.second;\n\t\t\t\tm_should_execute_change_request.pop();\n\t\t\t}\n\n\t\t\tif constexpr (settings::use_multithreading)\n\t\t\t{\n\t\t\t\tExecute_MT_Impl(scene_graph);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tExecute_ST_Impl(scene_graph);\n\t\t\t}\n\t\t}\n\n\t\t/*! Resize all render tasks */\n\t\t/*!\n\t\t\tThis function calls resize all render tasks to a specific width and height.\n\t\t\tThe width and height parameters should be the output size.\n\t\t\tPlease note this function calls Destroy than setup with the resize boolean set to true.\n\t\t*/\n\t\tinline void Resize(std::uint32_t width, std::uint32_t height)\n\t\t{\n\t\t\t// Make sure the tasks are finished executing\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t{\n\t\t\t\tWaitForCompletion(i);\n\t\t\t}\n\n\t\t\t// Make sure the GPU has finished with the tasks\n\t\t\tm_render_system->WaitForAllPreviousWork();\n\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t{\n\t\t\t\tm_destroy_funcs[i](*this, i, true);\n\n\t\t\t\tif (m_rt_properties[i].has_value() && !m_rt_properties[i].value().m_is_render_window)\n\t\t\t\t{\n\t\t\t\t\tm_render_system->ResizeRenderTarget(&m_render_targets[i],\n\t\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(width * m_rt_properties[i].value().m_resolution_scale.Get())),\n\t\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(height * m_rt_properties[i].value().m_resolution_scale.Get())));\n\t\t\t\t}\n\n\t\t\t\tm_setup_funcs[i](*m_render_system, *this, i, true);\n\t\t\t}\n\t\t}\n\n\t\t/*! Get Resolution scale of specified Render Task */\n\t\t/*!\n\t\t\tChecks if specified RenderTask has valid properties and returns it's resolution scalar.\n\t\t*/\n\t\t[[nodiscard]] inline const float GetRenderTargetResolutionScale(RenderTaskHandle handle) const\n\t\t{\n\t\t\tif (m_rt_properties[handle].has_value())\n\t\t\t{\n\t\t\t\treturn m_rt_properties[handle].value().m_resolution_scale.Get();\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGW(\"Error: GetResolutionScale tried accessing invalid data!\")\n\t\t\t}\n\t\t\treturn 1.0f;\n\t\t}\n\n\t\t/*! Destroy all tasks */\n\t\t/*!\n\t\t\tCalls all destroy functions and release any allocated data.\n\t\t*/\n\t\tvoid Destroy()\n\t\t{\n\t\t\t// Make sure all tasks finished executing\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t{\n\t\t\t\tWaitForCompletion(i);\n\t\t\t}\n\n\t\t\tm_render_system->WaitForAllPreviousWork();\n\n\t\t\t// Send the destroy events to the render tasks.\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t{\n\t\t\t\tm_destroy_funcs[i](*this, i, false);\n\t\t\t}\n\n\t\t\t// Make sure we free the data objects we allocated.\n\t\t\tfor (auto& data : m_data)\n\t\t\t{\n\t\t\t\tdata.reset();\n\t\t\t}\n\n\t\t\tfor (auto& cmd_list : m_cmd_lists)\n\t\t\t{\n\t\t\t\tm_render_system->DestroyCommandList(cmd_list);\n\t\t\t}\n\n\t\t\tfor(decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t{\n\t\t\t\tif(m_rt_properties[i].has_value() && !m_rt_properties[i]->m_is_render_window)\n\t\t\t\t{\n\t\t\t\t\tm_render_system->DestroyRenderTarget(&m_render_targets[i]);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Reset all members in the case of the user wanting to reuse this frame graph after `FrameGraph::Destroy`.\n\t\t\tm_setup_funcs.clear();\n\t\t\tm_execute_funcs.clear();\n\t\t\tm_destroy_funcs.clear();\n\t\t\tm_cmd_lists.clear();\n\t\t\tm_render_targets.clear();\n\t\t\tm_data.clear();\n\t\t\tm_data_type_info.clear();\n\t\t\tm_settings.clear();\n#ifndef FG_MAX_PERFORMANCE\n\t\t\tm_dependencies.clear();\n\t\t\tm_names.clear();\n#endif\n\t\t\tm_types.clear();\n\t\t\tm_rt_properties.clear();\n\t\t\tm_futures.clear();\n\n\t\t\tm_num_tasks = 0;\n\t\t}\n\n\t\t/* Stall the current thread until the render task has finished. */\n\t\tinline void WaitForCompletion(RenderTaskHandle handle)\n\t\t{\n\t\t\t// If we are not allowed to use multithreading let the compiler optimize this away completely.\n\t\t\tif constexpr (settings::use_multithreading)\n\t\t\t{\n\t\t\t\tif (auto& future = m_futures[handle]; future.valid())\n\t\t\t\t{\n\t\t\t\t\tfuture.wait();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t/*! Wait for a previous task. */\n\t\t/*!\n\t\t\tThis function loops over all tasks and checks whether it has the same type information as the template variable.\n\t\t\tIf a task was found it waits for it.\n\t\t\tIf no task is found with the type specified a nullptr will be returned and a error message send to the logging system.\n\t\t\tThe template parameter should be a Data struct of a the task you want to wait for.\n\t\t*/\n\t\ttemplate<typename T>\n\t\tinline void WaitForPredecessorTask()\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value ||\n\t\t\t\tstd::is_floating_point<T>::value ||\n\t\t\t\tstd::is_integral<T>::value,\n\t\t\t\t\"The template variable should be a class, struct, floating point value or a integral value.\");\n\t\t\tstatic_assert(!std::is_pointer<T>::value,\n\t\t\t\t\"The template variable type should not be a pointer. Its implicitly converted to a pointer.\");\n\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++)\n\t\t\t{\n\t\t\t\tif (typeid(T) == m_data_type_info[i])\n\t\t\t\t{\n\t\t\t\t\tWaitForCompletion(i);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tLOGC(\"Failed to find predecessor data! Please check your task order.\");\n\t\t\treturn;\n\t\t}\n\n\t\t/*! Get the data of a task. (Modifyable) */\n\t\t/*!\n\t\t\tThe template variable is used to specify the type of the data structure.\n\t\t\t\\param handle The handle to the render task. (Given by the `Setup`, `Execute` and `Destroy` functions)\n\t\t*/\n\t\ttemplate<typename T = void*>\n\t\t[[nodiscard]] inline auto & GetData(RenderTaskHandle handle) const\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value ||\n\t\t\t\tstd::is_floating_point<T>::value ||\n\t\t\t\tstd::is_integral<T>::value,\n\t\t\t\t\"The template variable should be a class, struct, floating point value or a integral value.\");\n\t\t\tstatic_assert(!std::is_pointer<T>::value,\n\t\t\t\t\"The template variable type should not be a pointer. Its implicitly converted to a pointer.\");\n\n\t\t\treturn *static_cast<T*>(m_data[handle].get());\n\t\t}\n\n\t\t/*! Get the data of a previously ran task. (Constant) */\n\t\t/*!\n\t\t\tThis function loops over all tasks and checks whether it has the same type information as the template variable.\n\t\t\tIf no task is found with the type specified a nullptr will be returned and a error message send to the logging system.\n\t\t\t\\param handle The handle to the render task. (Given by the `Setup`, `Execute` and `Destroy` functions)\n\t\t*/\n\t\ttemplate<typename T>\n\t\t[[nodiscard]] inline auto const & GetPredecessorData()\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value ||\n\t\t\t\tstd::is_floating_point<T>::value ||\n\t\t\t\tstd::is_integral<T>::value,\n\t\t\t\t\"The template variable should be a class, struct, floating point value or a integral value.\");\n\t\t\tstatic_assert(!std::is_pointer<T>::value,\n\t\t\t\t\"The template variable type should not be a pointer. Its implicitly converted to a pointer.\");\n\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++)\n\t\t\t{\n\t\t\t\tif (typeid(T) == m_data_type_info[i])\n\t\t\t\t{\n\t\t\t\t\tWaitForCompletion(i);\n\n\t\t\t\t\treturn *static_cast<T*>(m_data[i].get());\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tLOGC(\"Failed to find predecessor data! Please check your task order.\")\n\t\t\treturn *static_cast<T*>(nullptr);\n\t\t}\n\n\t\t/*! Get the render target of a previously ran task. (Constant) */\n\t\t/*!\n\t\t\tThis function loops over all tasks and checks whether it has the same type information as the template variable.\n\t\t\tIf no task is found with the type specified a nullptr will be returned and a error message send to the logging system.\n\t\t*/\n\t\ttemplate<typename T>\n\t\t[[nodiscard]] inline RenderTarget* GetPredecessorRenderTarget()\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value,\n\t\t\t\t\"The template variable should be a class or struct.\");\n\t\t\tstatic_assert(!std::is_pointer<T>::value,\n\t\t\t\t\"The template variable type should not be a pointer. Its implicitly converted to a pointer.\");\n\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++)\n\t\t\t{\n\t\t\t\tif (typeid(T) == m_data_type_info[i])\n\t\t\t\t{\n\t\t\t\t\tWaitForCompletion(i);\n\n\t\t\t\t\treturn m_render_targets[i];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tLOGC(\"Failed to find predecessor render target! Please check your task order.\");\n\t\t\treturn nullptr;\n\t\t}\n\n\t\t/*! Get the command list of a task. */\n\t\t/*!\n\t\t\tThe template variable allows you to cast the command list to a \"non platform independent\" different type. For example a `D3D12CommandList`.\n\t\t\t\\param handle The handle to the render task. (Given by the `Setup`, `Execute` and `Destroy` functions)\n\t\t*/\n\t\ttemplate<typename T = CommandList>\n\t\t[[nodiscard]] inline auto GetCommandList(RenderTaskHandle handle) const\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value || std::is_void<T>::value,\n\t\t\t\t\"The template variable should be a void, class or struct.\");\n\t\t\tstatic_assert(!std::is_pointer<T>::value,\n\t\t\t\t\"The template variable type should not be a pointer. Its implicitly converted to a pointer.\");\n\n\t\t\treturn static_cast<T*>(m_cmd_lists[handle]);\n\t\t}\n\n\t\t/*! Get the command list of a previously ran task. */\n\t\t/*!\n\t\t\tThe function allows the user to get a command list from another render task. These command lists are not meant\n\t\t\tto be used as they could be closed or in flight. This function was created only so that ray tracing tasks could get\n\t\t\tthe heap from the acceleration structure command list.\n\t\t*/\n\t\ttemplate<typename T>\n\t\t[[nodiscard]] inline wr::CommandList* GetPredecessorCommandList()\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value,\n\t\t\t\t\"The template variable should be a void, class or struct.\");\n\t\t\tstatic_assert(!std::is_pointer<T>::value,\n\t\t\t\t\"The template variable type should not be a pointer. Its implicitly converted to a pointer.\");\n\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++)\n\t\t\t{\n\t\t\t\tif (typeid(T) == m_data_type_info[i])\n\t\t\t\t{\n\t\t\t\t\tWaitForCompletion(i);\n\n\t\t\t\t\treturn m_cmd_lists[i];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tLOGC(\"Failed to find predecessor command list! Please check your task order.\");\n\t\t\treturn nullptr;\n\t\t}\n\n\t\ttemplate<typename T>\n\t\t[[nodiscard]] std::vector<T*> GetAllCommandLists()\n\t\t{\n\t\t\tstd::vector<T*> retval;\n\t\t\tretval.reserve(m_num_tasks);\n\n\t\t\t// TODO: Just return the fucking vector as const ref.\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++)\n\t\t\t{\n\t\t\t\t// Don't return command lists from tasks that don't require to be executed.\n\t\t\t\tif (!m_should_execute[i])\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tWaitForCompletion(i);\n\t\t\t\tretval.push_back(static_cast<T*>(m_cmd_lists[i]));\n\t\t\t}\n\n\t\t\treturn retval;\n\t\t}\n\n\t\t/*! Get the render target of a task. */\n\t\t/*!\n\t\t\tThe template variable allows you to cast the render target to a \"non platform independent\" different type. For example a `D3D12RenderTarget`.\n\t\t\t\\param handle The handle to the render task. (Given by the `Setup`, `Execute` and `Destroy` functions)\n\t\t*/\n\t\ttemplate<typename T = RenderTarget>\n\t\t[[nodiscard]] inline auto GetRenderTarget(RenderTaskHandle handle) const\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value || std::is_void<T>::value,\n\t\t\t\t\"The template variable should be a void, class or struct.\");\n\t\t\tstatic_assert(!std::is_pointer<T>::value,\n\t\t\t\t\"The template variable type should not be a pointer. Its implicitly converted to a pointer.\");\n\n\t\t\treturn static_cast<T*>(m_render_targets[handle]);\n\t\t}\n\n\t\t/*! Check if this frame graph has a task. */\n\t\t/*!\n\t\t\tThis checks if the frame graph has the task that has been given as the template variable.\n\t\t*/\n\t\ttemplate<typename T>\n\t\tinline bool HasTask() const\n\t\t{\n\t\t\treturn GetHandleFromType<T>().has_value();\n\t\t}\n\n\t\t/*! Validates the frame graph for correctness */\n\t\t/*!\n\t\t\tThis function uses the dependencies to check whether the frame graph is constructed properly by the user.\n\t\t\tNote: This function only works when `FG_MAX_PERFORMANCE` is defined.\n\t\t*/\n\t\tbool Validate()\n\t\t{\n\t\t\tbool result = true;\n#ifndef FG_MAX_PERFORMANCE\n\n\t\t\t// Loop over all the tasks.\n\t\t\tfor (decltype(m_num_tasks) handle = 0; handle < m_num_tasks; ++handle)\n\t\t\t{\n\t\t\t\t// Loop over the task's dependencies.\n\t\t\t\tfor (auto dependency : m_dependencies[handle])\n\t\t\t\t{\n\t\t\t\t\tbool found_dependency = false;\n\n\t\t\t\t\t// Loop over the predecessor tasks.\n\t\t\t\t\tfor (decltype(m_num_tasks) prev_handle = 0; prev_handle < handle; ++prev_handle)\n\t\t\t\t\t{\n\t\t\t\t\t\tconst auto& task_type_info = m_data_type_info[prev_handle].get();\n\t\t\t\t\t\tif (task_type_info == dependency.get())\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tfound_dependency = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!found_dependency)\n\t\t\t\t\t{\n\t\t\t\t\t\tLOGW(\"Framegraph validation: Failed to find dependency {}\", dependency.get().name());\n\t\t\t\t\t\tresult = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n#endif\n\n\t\t\treturn result;\n\t\t}\n\n\t\t/*! Add a task to the Frame Graph. */\n\t\t/*!\n\t\t\tThis creates a new render task based on a description.\n\t\t\tThe dependencies parameters can contain a list of typeid's of render tasks this task depends on.\n\t\t\tYou can use the FG_DEPS macro as followed: `AddTask<desc, FG_DEPS(OtherTaskData)>`\n\t\t\t\\param desc A description of the render task.\n\t\t*/\n\t\ttemplate<typename T>\n\t\tinline void AddTask(RenderTaskDesc& desc, std::wstring const & name, std::vector<std::reference_wrapper<const std::type_info>> dependencies = {})\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value ||\n\t\t\t\tstd::is_floating_point<T>::value ||\n\t\t\t\tstd::is_integral<T>::value,\n\t\t\t\t\"The template variable should be a class, struct, floating point value or a integral value.\");\n\t\t\tstatic_assert(std::is_default_constructible<T>::value,\n\t\t\t\t\"The template variable is not default constructible or nothrow consructible!\");\n\t\t\tstatic_assert(!std::is_pointer<T>::value,\n\t\t\t\t\"The template variable type should not be a pointer. Its implicitly converted to a pointer.\");\n\n\t\t\tm_setup_funcs.emplace_back(desc.m_setup_func);\n\t\t\tm_execute_funcs.emplace_back(desc.m_execute_func);\n\t\t\tm_destroy_funcs.emplace_back(desc.m_destroy_func);\n#ifndef FG_MAX_PERFORMANCE\n\t\t\tm_dependencies.emplace_back(dependencies);\n\t\t\tm_names.emplace_back(name);\n#endif\n\t\t\tm_settings.resize(m_num_tasks + 1ull);\n\t\t\tm_types.emplace_back(desc.m_type);\n\t\t\tm_rt_properties.emplace_back(desc.m_properties);\n\t\t\tm_data.emplace_back(std::make_shared<T>());\n\t\t\tm_data_type_info.emplace_back(typeid(T));\n\n\t\t\t// If we are allowed to do multithreading place the task in the appropriate vector\n\t\t\tif constexpr (settings::use_multithreading)\n\t\t\t{\n\t\t\t\tif (desc.m_allow_multithreading)\n\t\t\t\t{\n\t\t\t\t\tm_multi_threaded_tasks.emplace_back(m_num_tasks);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tm_single_threaded_tasks.emplace_back(m_num_tasks);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tm_num_tasks++;\n\t\t}\n\n\t\t/*! Return the frame graph's unique id.*/\n\t\t[[nodiscard]] const std::uint64_t GetUID() const noexcept\n\t\t{\n\t\t\treturn m_uid;\n\t\t};\n\n\t\t/*! Return the cpu texture. */\n\t\t[[nodiscard]] CPUTextures const & GetOutputTexture() const noexcept\n\t\t{\n\t\t\treturn m_output_cpu_textures;\n\t\t}\n\n\t\t/*! Save a render target to disc */\n\t\t/*\n\t\t\tTells the render system to save a render target to disc as a image.\n\t\t\t\\param index The index of the render target from the task you want to save.\n\t\t*/\n\t\ttemplate<typename T>\n\t\tvoid SaveTaskToDisc(std::string const & path, int index = 0)\n\t\t{\n\t\t\tauto handle = GetHandleFromType<T>();\n\n\t\t\tif (handle.has_value())\n\t\t\t{\n\t\t\t\tm_render_system->RequestRenderTargetSaveToDisc(path, m_render_targets[handle.value()], index);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGW(\"Failed to save render task to disc, Task was not found.\");\n\t\t\t}\n\t\t}\n\n\t\t/*! Set the cpu texture's data. */\n\t\tvoid SetOutputTexture(const CPUTexture& output_texture, CPUTextureType type)\n\t\t{\n\t\t\tswitch (type)\n\t\t\t{\n\t\t\tcase wr::CPUTextureType::PIXEL_DATA:\n\t\t\t\tif (m_output_cpu_textures.pixel_data != std::nullopt)\n\t\t\t\t{\n\t\t\t\t\tLOGW(\"Warning: CPU texture pixel data is written to more than once a frame!\");\n\t\t\t\t}\n\t\t\t\t// Save the pixel data\n\t\t\t\tm_output_cpu_textures.pixel_data = output_texture;\n\t\t\t\tbreak;\n\n\t\t\tcase wr::CPUTextureType::DEPTH_DATA:\n\t\t\t\tif (m_output_cpu_textures.depth_data != std::nullopt)\n\t\t\t\t{\n\t\t\t\t\tLOGW(\"Warning: CPU texture depth data is written to more than once a frame!\");\n\t\t\t\t}\n\t\t\t\t// Save the depth data\n\t\t\t\tm_output_cpu_textures.depth_data = output_texture;\n\t\t\t\tbreak;\n\n\t\t\tdefault:\n\t\t\t\t// Should never happen\n\t\t\t\tLOGC(\"Invalid CPU texture type supplied!\")\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t/*! Enable or disable execution of a task. */\n\t\tinline void SetShouldExecute(RenderTaskHandle handle, bool value)\n\t\t{\n\t\t\tm_should_execute_change_request.emplace(std::make_pair(handle, value));\n\t\t}\n\n\t\t/*! Enable or disable execution of a task. Templated version */\n\t\ttemplate<typename T>\n\t\tinline void SetShouldExecute(bool value)\n\t\t{\n\t\t\tauto handle = GetHandleFromType<T>();\n\n\t\t\tif (handle.has_value())\n\t\t\t{\n\t\t\t\tSetShouldExecute(handle.value(), value);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGW(\"Failed to mark the task for execution, Task was not found.\");\n\t\t\t}\n\t\t}\n\n\n\t\t/*! Update the settings of a task. */\n\t\t/*!\n\t\t\tThis is used to update settings of a render task.\n\t\t\tThis must ge called BEFORE `FrameGraph::Setup` or `RenderSystem::Render`.\n\t\t*/\n\t\ttemplate<typename T>\n\t\tinline void UpdateSettings(std::any settings)\n\t\t{\n\t\t\tauto handle = GetHandleFromType<T>();\n\n\t\t\tif (handle.has_value())\n\t\t\t{\n\t\t\t\tm_settings[handle.value()] = settings;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGW(\"Failed to update settings, Could not find render task\");\n\t\t\t}\n\t\t}\n\n\t\t/*! Gives you the settings of a task by handle. */\n\t\t/*!\n\t\t\tThis gives you the settings for a render task casted to `R`.\n\t\t\tMeant to be used for INSIDE the tasks.\n\t\t\tThe return value can be a nullptr.\n\t\t\t\\tparam T The render task data type used for identification.\n\t\t\t\\tparam R The type of the settings object.\n\n\t\t*/\n\t\ttemplate<typename T, typename R>\n\t\t[[nodiscard]] inline R GetSettings() const try\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value ||\n\t\t\t\tstd::is_floating_point<T>::value ||\n\t\t\t\tstd::is_integral<T>::value,\n\t\t\t\t\"The first template variable should be a class, struct, floating point value or a integral value.\");\n\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++)\n\t\t\t{\n\t\t\t\tif (typeid(T) == m_data_type_info[i])\n\t\t\t\t{\n\t\t\t\t\treturn std::any_cast<R>(m_settings[i].value());\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tLOGC(\"Failed to find task settings! Does your frame graph contain this task?\");\n\t\t\treturn R();\n\t\t}\n\t\tcatch (const std::bad_any_cast & e) {\n\t\t\tLOGC(\"A task settings requested failed to cast to T. ({})\", e.what());\n\t\t\treturn R();\n\t\t}\n\n\t\t/*! Gives you the settings of a task by handle. */\n\t\t/*!\n\t\t\tThis gives you the settings for a render task casted to `T`.\n\t\t\tMeant to be used for INSIDE the tasks.\n\t\t\tThe return value can be a nullptr.\n\t\t*/\n\t\ttemplate<typename T>\n\t\t[[nodiscard]] inline T GetSettings(RenderTaskHandle handle) const try\n\t\t{\n\t\t\tstatic_assert(std::is_class<T>::value ||\n\t\t\t\tstd::is_floating_point<T>::value ||\n\t\t\t\tstd::is_integral<T>::value,\n\t\t\t\t\"The template variable should be a class, struct, floating point value or a integral value.\");\n\n\t\t\treturn std::any_cast<T>(m_settings[handle].value());\n\t\t}\n\t\tcatch (const std::bad_any_cast& e) {\n\t\t\tLOGW(\"A task settings requested failed to cast to T. ({})\", e.what());\n\t\t\treturn T();\n\t\t}\n\n\t\t/*! Resets the CPU texture data for this frame. */\n\t\tinline void ResetOutputTexture()\n\t\t{\n\t\t\t// Frame has been rendered, allow a task to write to the CPU texture in the next frame\n\t\t\tm_output_cpu_textures.pixel_data = std::nullopt;\n\t\t\tm_output_cpu_textures.depth_data = std::nullopt;\n\t\t}\n\n\tprivate:\n\n\t\t/*! Get the handle from a task by data type */\n\t\t/* This function is zero overhead with GCC-8.2, -03 */\n\t\ttemplate<typename T>\n\t\tinline std::optional<RenderTaskHandle> GetHandleFromType() const\n\t\t{\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t{\n\t\t\t\tif (m_data_type_info[i].get() == typeid(T))\n\t\t\t\t{\n\t\t\t\t\treturn i;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn std::nullopt;\n\t\t}\n\n\t\t/*! Setup tasks multi threaded */\n\t\tinline void Setup_MT_Impl()\n\t\t{\n\t\t\t// Multithreading behaviour\n\t\t\tfor (const auto handle : m_multi_threaded_tasks)\n\t\t\t{\n\t\t\t\tm_futures[handle] = m_thread_pool->Enqueue([this, handle]\n\t\t\t\t{\n\t\t\t\t\tm_setup_funcs[handle](*m_render_system, *this, handle, false);\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Singlethreading behaviour\n\t\t\tfor (const auto handle : m_single_threaded_tasks)\n\t\t\t{\n\t\t\t\tm_setup_funcs[handle](*m_render_system, *this, handle, false);\n\t\t\t}\n\t\t}\n\n\t\t/*! Execute tasks multi threaded */\n\t\tinline void Execute_MT_Impl(SceneGraph& scene_graph)\n\t\t{\n\t\t\t// Multithreading behaviour\n\t\t\tfor (const auto handle : m_multi_threaded_tasks)\n\t\t\t{\n\t\t\t\t// Skip this task if it doesn't need to be executed\n\t\t\t\tif (!m_should_execute[handle])\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tm_futures[handle] = m_thread_pool->Enqueue([this, handle, &scene_graph]\n\t\t\t\t{\n\t\t\t\t\tExecuteSingleTask(scene_graph, handle);\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Singlethreading behaviour\n\t\t\tfor (const auto handle : m_single_threaded_tasks)\n\t\t\t{\n\t\t\t\t// Skip this task if it doesn't need to be executed\n\t\t\t\tif (!m_should_execute[handle])\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tExecuteSingleTask(scene_graph, handle);\n\t\t\t}\n\t\t}\n\n\t\t/*! Execute tasks single threaded */\n\t\tinline void Execute_ST_Impl(SceneGraph& scene_graph)\n\t\t{\n\t\t\tfor (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i)\n\t\t\t{\n\t\t\t\t// Skip this task if it doesn't need to be executed\n\t\t\t\tif (!m_should_execute[i])\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tExecuteSingleTask(scene_graph, i);\n\t\t\t}\n\t\t}\n\n\t\t/*! Execute a single task */\n\t\tinline void ExecuteSingleTask(SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto cmd_list = m_cmd_lists[handle];\n\t\t\tauto render_target = m_render_targets[handle];\n\t\t\tauto rt_properties = m_rt_properties[handle];\n\n\t\t\tm_render_system->ResetCommandList(cmd_list);\n\n\t\t\tswitch (m_types[handle])\n\t\t\t{\n\t\t\tcase RenderTaskType::DIRECT:\n\t\t\t\tif (rt_properties.has_value())\n\t\t\t\t{\n\t\t\t\t\tm_render_system->StartRenderTask(cmd_list, { render_target, rt_properties.value() });\n\t\t\t\t}\n\t\t\t\tm_execute_funcs[handle](*m_render_system, *this, sg, handle);\n\t\t\t\tif (rt_properties.has_value())\n\t\t\t\t{\n\t\t\t\t\tm_render_system->StopRenderTask(cmd_list, { render_target, rt_properties.value() });\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase RenderTaskType::COMPUTE:\n\t\t\t\tif (rt_properties.has_value())\n\t\t\t\t{\n\t\t\t\t\tm_render_system->StartComputeTask(cmd_list, { render_target, rt_properties.value() });\n\t\t\t\t}\n\t\t\t\tm_execute_funcs[handle](*m_render_system, *this, sg, handle);\n\t\t\t\tif (rt_properties.has_value())\n\t\t\t\t{\n\t\t\t\t\tm_render_system->StopComputeTask(cmd_list, { render_target, rt_properties.value() });\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase RenderTaskType::COPY:\n\t\t\t\tif (rt_properties.has_value())\n\t\t\t\t{\n\t\t\t\t\tm_render_system->StartCopyTask(cmd_list, { render_target, rt_properties.value() });\n\t\t\t\t}\n\t\t\t\tm_execute_funcs[handle](*m_render_system, *this, sg, handle);\n\t\t\t\tif (rt_properties.has_value())\n\t\t\t\t{\n\t\t\t\t\tm_render_system->StopCopyTask(cmd_list, { render_target, rt_properties.value() });\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tm_render_system->CloseCommandList(cmd_list);\n\t\t}\n\n\t\t/*! Get a free unique ID. */\n\t\tstatic std::uint64_t GetFreeUID()\n\t\t{\n\t\t\tif (!m_free_uids.empty())\n\t\t\t{\n\t\t\t\tstd::uint64_t uid = m_free_uids.top();\n\t\t\t\tm_free_uids.pop();\n\t\t\t\treturn uid;\n\t\t\t}\n\t\t\tstd::uint64_t uid = m_largest_uid;\n\t\t\tm_largest_uid++;\n\t\t\treturn uid;\n\t\t}\n\n\t\t/*! Get a release a unique ID for reuse. */\n\t\tWISPRENDERER_EXPORT static void ReleaseUID(std::uint64_t uid)\n\t\t{\n\t\t\tm_free_uids.push(uid);\n\t\t}\n\n\t\tRenderSystem* m_render_system;\n\t\t/*! The number of tasks we have added. */\n\t\tstd::uint32_t m_num_tasks;\n\t\t/*! The thread pool used for multithreading */\n\t\tutil::ThreadPool* m_thread_pool;\n\n\t\t/*! Vectors which allow us to itterate over only single threader or only multithreaded tasks. */\n\t\tstd::vector<RenderTaskHandle> m_multi_threaded_tasks;\n\t\tstd::vector<RenderTaskHandle> m_single_threaded_tasks;\n\n\t\t/*! Holds the textures that can be written to memory. */\n\t\tCPUTextures m_output_cpu_textures;\n\n\t\t/*! Task function pointers. */\n\t\tstd::vector<setup_func_t> m_setup_funcs;\n\t\tstd::vector<execute_func_t> m_execute_funcs;\n\t\tstd::vector<destroy_func_t> m_destroy_funcs;\n\t\t/*! Task target and command list. */\n\t\tstd::vector<CommandList*> m_cmd_lists;\n\t\tstd::vector<RenderTarget*> m_render_targets;\n\t\t/*! Task data and the type information of the original data structure. */\n\t\tstd::vector<std::shared_ptr<void>> m_data;\n\t\tstd::vector<std::reference_wrapper<const std::type_info>> m_data_type_info;\n\t\t/*! Task settings that can be passed to the frame graph from outside the task. */\n\t\tstd::vector<std::optional<std::any>> m_settings;\n\t\t/*! Defines whether a task should execute or not. */\n\t\tstd::vector<bool> m_should_execute;\n\t\t/*! Used to queue a request to change the should execute value */\n\t\tstd::queue<std::pair<RenderTaskHandle, bool>> m_should_execute_change_request;\n\t\t/*! Descriptions of the tasks. */\n#ifndef FG_MAX_PERFORMANCE\n\t\t/*! Stored the dependencies of a task. */\n\t\tstd::vector<std::vector<std::reference_wrapper<const std::type_info>>> m_dependencies;\n\t\t/*! The names of the render targets meant for debugging */\n\t\tstd::vector<std::wstring> m_names;\n#endif\n\t\tstd::vector<RenderTaskType> m_types;\n\t\tstd::vector<std::optional<RenderTargetProperties>> m_rt_properties;\n\t\tstd::vector<std::future<void>> m_futures;\n\n\t\tconst std::uint64_t m_uid;\n\t\tstatic inline std::uint64_t m_largest_uid = 0;\n\t\tstatic inline std::stack<std::uint64_t> m_free_uids = {};\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/id_factory.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"id_factory.hpp\"\n\nnamespace wr\n{\n\n\tIDFactory::IDFactory()\n\t\t: m_id(0)\n\t{\n\t}\n\n\tvoid IDFactory::MakeIDAvailable(std::uint32_t unused_id)\n\t{\n\t\tm_unused_ids.push_back(unused_id);\n\t}\n\n\tstd::uint32_t IDFactory::GetUnusedID()\n\t{\n\t\tstd::uint32_t ret_id;\n\n\t\tif (m_unused_ids.empty())\n\t\t{\n\t\t\tret_id = m_id;\n\t\t\tm_id++;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tret_id = *(m_unused_ids.end() - 1);\n\t\t\tm_unused_ids.pop_back();\n\t\t}\n\n\t\treturn ret_id;\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/id_factory.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include <stdint.h>\n#include <vector>\n\nnamespace wr\n{\n\n\tclass IDFactory\n\t{\n\tpublic:\n\n\t\tIDFactory();\n\t\tvirtual ~IDFactory() = default;\n\n\t\tIDFactory(IDFactory const &) = delete;\n\t\tIDFactory& operator=(IDFactory const &) = delete;\n\t\tIDFactory(IDFactory&&) = delete;\n\t\tIDFactory& operator=(IDFactory&&) = delete;\n\n\t\tvoid MakeIDAvailable(std::uint32_t unused_id);\n\t\tstd::uint32_t GetUnusedID();\n\n\tprotected:\n\n\t\tstd::uint32_t m_id;\n\t\tstd::vector<uint32_t> m_unused_ids;\n\t};\n\n} /* wr */"
  },
  {
    "path": "src/imgui/ImGuizmo.cpp",
    "content": "// The MIT License(MIT)\n//\n// Copyright(c) 2016 Cedric Guillemet\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files(the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions :\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\n#include \"imgui.hpp\"\n#ifndef IMGUI_DEFINE_MATH_OPERATORS\n#define IMGUI_DEFINE_MATH_OPERATORS\n#endif\n#include \"imgui_internal.hpp\"\n#include \"ImGuizmo.h\"\n\n// includes patches for multiview from\n// https://github.com/CedricGuillemet/ImGuizmo/issues/15\n\nnamespace ImGuizmo\n{\n   static const float ZPI = 3.14159265358979323846f;\n   static const float RAD2DEG = (180.f / ZPI);\n   static const float DEG2RAD = (ZPI / 180.f);\n   static const float gGizmoSizeClipSpace = 0.1f;\n   const float screenRotateSize = 0.06f;\n\n   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n   // utility and math\n\n   void FPU_MatrixF_x_MatrixF(const float *a, const float *b, float *r)\n   {\n      r[0] = a[0] * b[0] + a[1] * b[4] + a[2] * b[8] + a[3] * b[12];\n      r[1] = a[0] * b[1] + a[1] * b[5] + a[2] * b[9] + a[3] * b[13];\n      r[2] = a[0] * b[2] + a[1] * b[6] + a[2] * b[10] + a[3] * b[14];\n      r[3] = a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3] * b[15];\n\n      r[4] = a[4] * b[0] + a[5] * b[4] + a[6] * b[8] + a[7] * b[12];\n      r[5] = a[4] * b[1] + a[5] * b[5] + a[6] * b[9] + a[7] * b[13];\n      r[6] = a[4] * b[2] + a[5] * b[6] + a[6] * b[10] + a[7] * b[14];\n      r[7] = a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7] * b[15];\n\n      r[8] = a[8] * b[0] + a[9] * b[4] + a[10] * b[8] + a[11] * b[12];\n      r[9] = a[8] * b[1] + a[9] * b[5] + a[10] * b[9] + a[11] * b[13];\n      r[10] = a[8] * b[2] + a[9] * b[6] + a[10] * b[10] + a[11] * b[14];\n      r[11] = a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11] * b[15];\n\n      r[12] = a[12] * b[0] + a[13] * b[4] + a[14] * b[8] + a[15] * b[12];\n      r[13] = a[12] * b[1] + a[13] * b[5] + a[14] * b[9] + a[15] * b[13];\n      r[14] = a[12] * b[2] + a[13] * b[6] + a[14] * b[10] + a[15] * b[14];\n      r[15] = a[12] * b[3] + a[13] * b[7] + a[14] * b[11] + a[15] * b[15];\n   }\n\n   //template <typename T> T LERP(T x, T y, float z) { return (x + (y - x)*z); }\n   template <typename T> T Clamp(T x, T y, T z) { return ((x<y) ? y : ((x>z) ? z : x)); }\n   template <typename T> T max(T x, T y) { return (x > y) ? x : y; }\n   template <typename T> T min(T x, T y) { return (x < y) ? x : y; }\n   template <typename T> bool IsWithin(T x, T y, T z) { return (x>=y) && (x<=z); }\n\n   struct matrix_t;\n   struct vec_t\n   {\n   public:\n      float x, y, z, w;\n\n      void Lerp(const vec_t& v, float t)\n      {\n         x += (v.x - x) * t;\n         y += (v.y - y) * t;\n         z += (v.z - z) * t;\n         w += (v.w - w) * t;\n      }\n\n      void Set(float v) { x = y = z = w = v; }\n      void Set(float _x, float _y, float _z = 0.f, float _w = 0.f) { x = _x; y = _y; z = _z; w = _w; }\n\n      vec_t& operator -= (const vec_t& v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; }\n      vec_t& operator += (const vec_t& v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; }\n      vec_t& operator *= (const vec_t& v) { x *= v.x; y *= v.y; z *= v.z; w *= v.w; return *this; }\n      vec_t& operator *= (float v) { x *= v;    y *= v;    z *= v;    w *= v;    return *this; }\n\n      vec_t operator * (float f) const;\n      vec_t operator - () const;\n      vec_t operator - (const vec_t& v) const;\n      vec_t operator + (const vec_t& v) const;\n      vec_t operator * (const vec_t& v) const;\n\n      const vec_t& operator + () const { return (*this); }\n      float Length() const { return sqrtf(x*x + y*y + z*z); };\n      float LengthSq() const { return (x*x + y*y + z*z); };\n      vec_t Normalize() { (*this) *= (1.f / Length()); return (*this); }\n      vec_t Normalize(const vec_t& v) { this->Set(v.x, v.y, v.z, v.w); this->Normalize(); return (*this); }\n      vec_t Abs() const;\n      void Cross(const vec_t& v)\n      {\n         vec_t res;\n         res.x = y * v.z - z * v.y;\n         res.y = z * v.x - x * v.z;\n         res.z = x * v.y - y * v.x;\n\n         x = res.x;\n         y = res.y;\n         z = res.z;\n         w = 0.f;\n      }\n      void Cross(const vec_t& v1, const vec_t& v2)\n      {\n         x = v1.y * v2.z - v1.z * v2.y;\n         y = v1.z * v2.x - v1.x * v2.z;\n         z = v1.x * v2.y - v1.y * v2.x;\n         w = 0.f;\n      }\n      float Dot(const vec_t &v) const\n      {\n         return (x * v.x) + (y * v.y) + (z * v.z) + (w * v.w);\n      }\n      float Dot3(const vec_t &v) const\n      {\n         return (x * v.x) + (y * v.y) + (z * v.z);\n      }\n\n      void Transform(const matrix_t& matrix);\n      void Transform(const vec_t & s, const matrix_t& matrix);\n\n      void TransformVector(const matrix_t& matrix);\n      void TransformPoint(const matrix_t& matrix);\n      void TransformVector(const vec_t& v, const matrix_t& matrix) { (*this) = v; this->TransformVector(matrix); }\n      void TransformPoint(const vec_t& v, const matrix_t& matrix) { (*this) = v; this->TransformPoint(matrix); }\n\n      float& operator [] (size_t index) { return ((float*)&x)[index]; }\n      const float& operator [] (size_t index) const { return ((float*)&x)[index]; }\n   };\n\n   vec_t makeVect(float _x, float _y, float _z = 0.f, float _w = 0.f) { vec_t res; res.x = _x; res.y = _y; res.z = _z; res.w = _w; return res; }\n   vec_t makeVect(ImVec2 v) { vec_t res; res.x = v.x; res.y = v.y; res.z = 0.f; res.w = 0.f; return res; }\n   vec_t vec_t::operator * (float f) const { return makeVect(x * f, y * f, z * f, w *f); }\n   vec_t vec_t::operator - () const { return makeVect(-x, -y, -z, -w); }\n   vec_t vec_t::operator - (const vec_t& v) const { return makeVect(x - v.x, y - v.y, z - v.z, w - v.w); }\n   vec_t vec_t::operator + (const vec_t& v) const { return makeVect(x + v.x, y + v.y, z + v.z, w + v.w); }\n   vec_t vec_t::operator * (const vec_t& v) const { return makeVect(x * v.x, y * v.y, z * v.z, w * v.w); }\n   vec_t vec_t::Abs() const { return makeVect(fabsf(x), fabsf(y), fabsf(z)); }\n\n   vec_t Normalized(const vec_t& v) { vec_t res; res = v; res.Normalize(); return res; }\n   vec_t Cross(const vec_t& v1, const vec_t& v2)\n   {\n      vec_t res;\n      res.x = v1.y * v2.z - v1.z * v2.y;\n      res.y = v1.z * v2.x - v1.x * v2.z;\n      res.z = v1.x * v2.y - v1.y * v2.x;\n      res.w = 0.f;\n      return res;\n   }\n\n   float Dot(const vec_t &v1, const vec_t &v2)\n   {\n      return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);\n   }\n\n   vec_t BuildPlan(const vec_t & p_point1, const vec_t & p_normal)\n   {\n      vec_t normal, res;\n      normal.Normalize(p_normal);\n      res.w = normal.Dot(p_point1);\n      res.x = normal.x;\n      res.y = normal.y;\n      res.z = normal.z;\n      return res;\n   }\n\n   struct matrix_t\n   {\n   public:\n\n      union\n      {\n         float m[4][4];\n         float m16[16];\n         struct\n         {\n            vec_t right, up, dir, position;\n         } v;\n         vec_t component[4];\n      };\n\n      matrix_t(const matrix_t& other) { memcpy(&m16[0], &other.m16[0], sizeof(float) * 16); }\n      matrix_t() {}\n\n      operator float * () { return m16; }\n      operator const float* () const { return m16; }\n      void Translation(float _x, float _y, float _z) { this->Translation(makeVect(_x, _y, _z)); }\n\n      void Translation(const vec_t& vt)\n      {\n         v.right.Set(1.f, 0.f, 0.f, 0.f);\n         v.up.Set(0.f, 1.f, 0.f, 0.f);\n         v.dir.Set(0.f, 0.f, 1.f, 0.f);\n         v.position.Set(vt.x, vt.y, vt.z, 1.f);\n      }\n\n      void Scale(float _x, float _y, float _z)\n      {\n         v.right.Set(_x, 0.f, 0.f, 0.f);\n         v.up.Set(0.f, _y, 0.f, 0.f);\n         v.dir.Set(0.f, 0.f, _z, 0.f);\n         v.position.Set(0.f, 0.f, 0.f, 1.f);\n      }\n      void Scale(const vec_t& s) { Scale(s.x, s.y, s.z); }\n\n      matrix_t& operator *= (const matrix_t& mat)\n      {\n         matrix_t tmpMat;\n         tmpMat = *this;\n         tmpMat.Multiply(mat);\n         *this = tmpMat;\n         return *this;\n      }\n      matrix_t operator * (const matrix_t& mat) const\n      {\n         matrix_t matT;\n         matT.Multiply(*this, mat);\n         return matT;\n      }\n\n      void Multiply(const matrix_t &matrix)\n      {\n         matrix_t tmp;\n         tmp = *this;\n\n         FPU_MatrixF_x_MatrixF((float*)&tmp, (float*)&matrix, (float*)this);\n      }\n\n      void Multiply(const matrix_t &m1, const matrix_t &m2)\n      {\n         FPU_MatrixF_x_MatrixF((float*)&m1, (float*)&m2, (float*)this);\n      }\n\n      float GetDeterminant() const\n      {\n         return m[0][0] * m[1][1] * m[2][2] + m[0][1] * m[1][2] * m[2][0] + m[0][2] * m[1][0] * m[2][1] -\n            m[0][2] * m[1][1] * m[2][0] - m[0][1] * m[1][0] * m[2][2] - m[0][0] * m[1][2] * m[2][1];\n      }\n\n      float Inverse(const matrix_t &srcMatrix, bool affine = false);\n      void SetToIdentity()\n      {\n         v.right.Set(1.f, 0.f, 0.f, 0.f);\n         v.up.Set(0.f, 1.f, 0.f, 0.f);\n         v.dir.Set(0.f, 0.f, 1.f, 0.f);\n         v.position.Set(0.f, 0.f, 0.f, 1.f);\n      }\n      void Transpose()\n      {\n         matrix_t tmpm;\n         for (int l = 0; l < 4; l++)\n         {\n            for (int c = 0; c < 4; c++)\n            {\n               tmpm.m[l][c] = m[c][l];\n            }\n         }\n         (*this) = tmpm;\n      }\n\n      void RotationAxis(const vec_t & axis, float angle);\n\n      void OrthoNormalize()\n      {\n         v.right.Normalize();\n         v.up.Normalize();\n         v.dir.Normalize();\n      }\n   };\n\n   void vec_t::Transform(const matrix_t& matrix)\n   {\n      vec_t out;\n\n      out.x = x * matrix.m[0][0] + y * matrix.m[1][0] + z * matrix.m[2][0] + w * matrix.m[3][0];\n      out.y = x * matrix.m[0][1] + y * matrix.m[1][1] + z * matrix.m[2][1] + w * matrix.m[3][1];\n      out.z = x * matrix.m[0][2] + y * matrix.m[1][2] + z * matrix.m[2][2] + w * matrix.m[3][2];\n      out.w = x * matrix.m[0][3] + y * matrix.m[1][3] + z * matrix.m[2][3] + w * matrix.m[3][3];\n\n      x = out.x;\n      y = out.y;\n      z = out.z;\n      w = out.w;\n   }\n\n   void vec_t::Transform(const vec_t & s, const matrix_t& matrix)\n   {\n      *this = s;\n      Transform(matrix);\n   }\n\n   void vec_t::TransformPoint(const matrix_t& matrix)\n   {\n      vec_t out;\n\n      out.x = x * matrix.m[0][0] + y * matrix.m[1][0] + z * matrix.m[2][0] + matrix.m[3][0];\n      out.y = x * matrix.m[0][1] + y * matrix.m[1][1] + z * matrix.m[2][1] + matrix.m[3][1];\n      out.z = x * matrix.m[0][2] + y * matrix.m[1][2] + z * matrix.m[2][2] + matrix.m[3][2];\n      out.w = x * matrix.m[0][3] + y * matrix.m[1][3] + z * matrix.m[2][3] + matrix.m[3][3];\n\n      x = out.x;\n      y = out.y;\n      z = out.z;\n      w = out.w;\n   }\n\n\n   void vec_t::TransformVector(const matrix_t& matrix)\n   {\n      vec_t out;\n\n      out.x = x * matrix.m[0][0] + y * matrix.m[1][0] + z * matrix.m[2][0];\n      out.y = x * matrix.m[0][1] + y * matrix.m[1][1] + z * matrix.m[2][1];\n      out.z = x * matrix.m[0][2] + y * matrix.m[1][2] + z * matrix.m[2][2];\n      out.w = x * matrix.m[0][3] + y * matrix.m[1][3] + z * matrix.m[2][3];\n\n      x = out.x;\n      y = out.y;\n      z = out.z;\n      w = out.w;\n   }\n\n   float matrix_t::Inverse(const matrix_t &srcMatrix, bool affine)\n   {\n      float det = 0;\n\n      if (affine)\n      {\n         det = GetDeterminant();\n         float s = 1 / det;\n         m[0][0] = (srcMatrix.m[1][1] * srcMatrix.m[2][2] - srcMatrix.m[1][2] * srcMatrix.m[2][1]) * s;\n         m[0][1] = (srcMatrix.m[2][1] * srcMatrix.m[0][2] - srcMatrix.m[2][2] * srcMatrix.m[0][1]) * s;\n         m[0][2] = (srcMatrix.m[0][1] * srcMatrix.m[1][2] - srcMatrix.m[0][2] * srcMatrix.m[1][1]) * s;\n         m[1][0] = (srcMatrix.m[1][2] * srcMatrix.m[2][0] - srcMatrix.m[1][0] * srcMatrix.m[2][2]) * s;\n         m[1][1] = (srcMatrix.m[2][2] * srcMatrix.m[0][0] - srcMatrix.m[2][0] * srcMatrix.m[0][2]) * s;\n         m[1][2] = (srcMatrix.m[0][2] * srcMatrix.m[1][0] - srcMatrix.m[0][0] * srcMatrix.m[1][2]) * s;\n         m[2][0] = (srcMatrix.m[1][0] * srcMatrix.m[2][1] - srcMatrix.m[1][1] * srcMatrix.m[2][0]) * s;\n         m[2][1] = (srcMatrix.m[2][0] * srcMatrix.m[0][1] - srcMatrix.m[2][1] * srcMatrix.m[0][0]) * s;\n         m[2][2] = (srcMatrix.m[0][0] * srcMatrix.m[1][1] - srcMatrix.m[0][1] * srcMatrix.m[1][0]) * s;\n         m[3][0] = -(m[0][0] * srcMatrix.m[3][0] + m[1][0] * srcMatrix.m[3][1] + m[2][0] * srcMatrix.m[3][2]);\n         m[3][1] = -(m[0][1] * srcMatrix.m[3][0] + m[1][1] * srcMatrix.m[3][1] + m[2][1] * srcMatrix.m[3][2]);\n         m[3][2] = -(m[0][2] * srcMatrix.m[3][0] + m[1][2] * srcMatrix.m[3][1] + m[2][2] * srcMatrix.m[3][2]);\n      }\n      else\n      {\n         // transpose matrix\n         float src[16];\n         for (int i = 0; i < 4; ++i)\n         {\n            src[i] = srcMatrix.m16[i * 4];\n            src[i + 4] = srcMatrix.m16[i * 4 + 1];\n            src[i + 8] = srcMatrix.m16[i * 4 + 2];\n            src[i + 12] = srcMatrix.m16[i * 4 + 3];\n         }\n\n         // calculate pairs for first 8 elements (cofactors)\n         float tmp[12]; // temp array for pairs\n         tmp[0] = src[10] * src[15];\n         tmp[1] = src[11] * src[14];\n         tmp[2] = src[9] * src[15];\n         tmp[3] = src[11] * src[13];\n         tmp[4] = src[9] * src[14];\n         tmp[5] = src[10] * src[13];\n         tmp[6] = src[8] * src[15];\n         tmp[7] = src[11] * src[12];\n         tmp[8] = src[8] * src[14];\n         tmp[9] = src[10] * src[12];\n         tmp[10] = src[8] * src[13];\n         tmp[11] = src[9] * src[12];\n\n         // calculate first 8 elements (cofactors)\n         m16[0] = (tmp[0] * src[5] + tmp[3] * src[6] + tmp[4] * src[7]) - (tmp[1] * src[5] + tmp[2] * src[6] + tmp[5] * src[7]);\n         m16[1] = (tmp[1] * src[4] + tmp[6] * src[6] + tmp[9] * src[7]) - (tmp[0] * src[4] + tmp[7] * src[6] + tmp[8] * src[7]);\n         m16[2] = (tmp[2] * src[4] + tmp[7] * src[5] + tmp[10] * src[7]) - (tmp[3] * src[4] + tmp[6] * src[5] + tmp[11] * src[7]);\n         m16[3] = (tmp[5] * src[4] + tmp[8] * src[5] + tmp[11] * src[6]) - (tmp[4] * src[4] + tmp[9] * src[5] + tmp[10] * src[6]);\n         m16[4] = (tmp[1] * src[1] + tmp[2] * src[2] + tmp[5] * src[3]) - (tmp[0] * src[1] + tmp[3] * src[2] + tmp[4] * src[3]);\n         m16[5] = (tmp[0] * src[0] + tmp[7] * src[2] + tmp[8] * src[3]) - (tmp[1] * src[0] + tmp[6] * src[2] + tmp[9] * src[3]);\n         m16[6] = (tmp[3] * src[0] + tmp[6] * src[1] + tmp[11] * src[3]) - (tmp[2] * src[0] + tmp[7] * src[1] + tmp[10] * src[3]);\n         m16[7] = (tmp[4] * src[0] + tmp[9] * src[1] + tmp[10] * src[2]) - (tmp[5] * src[0] + tmp[8] * src[1] + tmp[11] * src[2]);\n\n         // calculate pairs for second 8 elements (cofactors)\n         tmp[0] = src[2] * src[7];\n         tmp[1] = src[3] * src[6];\n         tmp[2] = src[1] * src[7];\n         tmp[3] = src[3] * src[5];\n         tmp[4] = src[1] * src[6];\n         tmp[5] = src[2] * src[5];\n         tmp[6] = src[0] * src[7];\n         tmp[7] = src[3] * src[4];\n         tmp[8] = src[0] * src[6];\n         tmp[9] = src[2] * src[4];\n         tmp[10] = src[0] * src[5];\n         tmp[11] = src[1] * src[4];\n\n         // calculate second 8 elements (cofactors)\n         m16[8] = (tmp[0] * src[13] + tmp[3] * src[14] + tmp[4] * src[15]) - (tmp[1] * src[13] + tmp[2] * src[14] + tmp[5] * src[15]);\n         m16[9] = (tmp[1] * src[12] + tmp[6] * src[14] + tmp[9] * src[15]) - (tmp[0] * src[12] + tmp[7] * src[14] + tmp[8] * src[15]);\n         m16[10] = (tmp[2] * src[12] + tmp[7] * src[13] + tmp[10] * src[15]) - (tmp[3] * src[12] + tmp[6] * src[13] + tmp[11] * src[15]);\n         m16[11] = (tmp[5] * src[12] + tmp[8] * src[13] + tmp[11] * src[14]) - (tmp[4] * src[12] + tmp[9] * src[13] + tmp[10] * src[14]);\n         m16[12] = (tmp[2] * src[10] + tmp[5] * src[11] + tmp[1] * src[9]) - (tmp[4] * src[11] + tmp[0] * src[9] + tmp[3] * src[10]);\n         m16[13] = (tmp[8] * src[11] + tmp[0] * src[8] + tmp[7] * src[10]) - (tmp[6] * src[10] + tmp[9] * src[11] + tmp[1] * src[8]);\n         m16[14] = (tmp[6] * src[9] + tmp[11] * src[11] + tmp[3] * src[8]) - (tmp[10] * src[11] + tmp[2] * src[8] + tmp[7] * src[9]);\n         m16[15] = (tmp[10] * src[10] + tmp[4] * src[8] + tmp[9] * src[9]) - (tmp[8] * src[9] + tmp[11] * src[10] + tmp[5] * src[8]);\n\n         // calculate determinant\n         det = src[0] * m16[0] + src[1] * m16[1] + src[2] * m16[2] + src[3] * m16[3];\n\n         // calculate matrix inverse\n         float invdet = 1 / det;\n         for (int j = 0; j < 16; ++j)\n         {\n            m16[j] *= invdet;\n         }\n      }\n\n      return det;\n   }\n\n   void matrix_t::RotationAxis(const vec_t & axis, float angle)\n   {\n      float length2 = axis.LengthSq();\n      if (length2 < FLT_EPSILON)\n      {\n         SetToIdentity();\n         return;\n      }\n\n      vec_t n = axis * (1.f / sqrtf(length2));\n      float s = sinf(angle);\n      float c = cosf(angle);\n      float k = 1.f - c;\n\n      float xx = n.x * n.x * k + c;\n      float yy = n.y * n.y * k + c;\n      float zz = n.z * n.z * k + c;\n      float xy = n.x * n.y * k;\n      float yz = n.y * n.z * k;\n      float zx = n.z * n.x * k;\n      float xs = n.x * s;\n      float ys = n.y * s;\n      float zs = n.z * s;\n\n      m[0][0] = xx;\n      m[0][1] = xy + zs;\n      m[0][2] = zx - ys;\n      m[0][3] = 0.f;\n      m[1][0] = xy - zs;\n      m[1][1] = yy;\n      m[1][2] = yz + xs;\n      m[1][3] = 0.f;\n      m[2][0] = zx + ys;\n      m[2][1] = yz - xs;\n      m[2][2] = zz;\n      m[2][3] = 0.f;\n      m[3][0] = 0.f;\n      m[3][1] = 0.f;\n      m[3][2] = 0.f;\n      m[3][3] = 1.f;\n   }\n\n   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n   //\n\n   enum MOVETYPE\n   {\n      NONE,\n      MOVE_X,\n      MOVE_Y,\n      MOVE_Z,\n      MOVE_YZ,\n      MOVE_ZX,\n      MOVE_XY,\n      MOVE_SCREEN,\n      ROTATE_X,\n      ROTATE_Y,\n      ROTATE_Z,\n      ROTATE_SCREEN,\n      SCALE_X,\n      SCALE_Y,\n      SCALE_Z,\n      SCALE_XYZ\n   };\n\n   struct Context\n   {\n      Context() : mbUsing(false), mbEnable(true), mbUsingBounds(false)\n      {\n      }\n\n      ImDrawList* mDrawList;\n\n      MODE mMode;\n      matrix_t mViewMat;\n      matrix_t mProjectionMat;\n      matrix_t mModel;\n      matrix_t mModelInverse;\n      matrix_t mModelSource;\n      matrix_t mModelSourceInverse;\n      matrix_t mMVP;\n      matrix_t mViewProjection;\n\n      vec_t mModelScaleOrigin;\n      vec_t mCameraEye;\n      vec_t mCameraRight;\n      vec_t mCameraDir;\n      vec_t mCameraUp;\n      vec_t mRayOrigin;\n      vec_t mRayVector;\n\n      float  mRadiusSquareCenter;\n      ImVec2 mScreenSquareCenter;\n      ImVec2 mScreenSquareMin;\n      ImVec2 mScreenSquareMax;\n\n      float mScreenFactor;\n      vec_t mRelativeOrigin;\n\n      bool mbUsing;\n      bool mbEnable;\n\n      // translation\n      vec_t mTranslationPlan;\n      vec_t mTranslationPlanOrigin;\n      vec_t mMatrixOrigin;\n\n      // rotation\n      vec_t mRotationVectorSource;\n      float mRotationAngle;\n      float mRotationAngleOrigin;\n      //vec_t mWorldToLocalAxis;\n\n      // scale\n      vec_t mScale;\n      vec_t mScaleValueOrigin;\n      float mSaveMousePosx;\n\n      // save axis factor when using gizmo\n      bool mBelowAxisLimit[3];\n      bool mBelowPlaneLimit[3];\n      float mAxisFactor[3];\n\n      // bounds stretching\n      vec_t mBoundsPivot;\n      vec_t mBoundsAnchor;\n      vec_t mBoundsPlan;\n      vec_t mBoundsLocalPivot;\n      int mBoundsBestAxis;\n      int mBoundsAxis[2];\n      bool mbUsingBounds;\n      matrix_t mBoundsMatrix;\n\n      //\n      int mCurrentOperation;\n\n      float mX = 0.f;\n      float mY = 0.f;\n      float mWidth = 0.f;\n      float mHeight = 0.f;\n      float mXMax = 0.f;\n      float mYMax = 0.f;\n     float mDisplayRatio = 1.f;\n\n     bool mIsOrthographic = false;\n   };\n\n   static Context gContext;\n\n   static const float angleLimit = 0.96f;\n   static const float planeLimit = 0.2f;\n\n   static const vec_t directionUnary[3] = { makeVect(1.f, 0.f, 0.f), makeVect(0.f, 1.f, 0.f), makeVect(0.f, 0.f, 1.f) };\n   static const ImU32 directionColor[3] = { 0xFF0000AA, 0xFF00AA00, 0xFFAA0000 };\n\n   // Alpha: 100%: FF, 87%: DE, 70%: B3, 54%: 8A, 50%: 80, 38%: 61, 12%: 1F\n   static const ImU32 planeColor[3] = { 0x610000AA, 0x6100AA00, 0x61AA0000 };\n   static const ImU32 selectionColor = 0x8A1080FF;\n   static const ImU32 inactiveColor = 0x99999999;\n   static const ImU32 translationLineColor = 0xAAAAAAAA;\n   static const char *translationInfoMask[] = { \"X : %5.3f\", \"Y : %5.3f\", \"Z : %5.3f\",\n      \"Y : %5.3f Z : %5.3f\", \"X : %5.3f Z : %5.3f\", \"X : %5.3f Y : %5.3f\",\n      \"X : %5.3f Y : %5.3f Z : %5.3f\" };\n   static const char *scaleInfoMask[] = { \"X : %5.2f\", \"Y : %5.2f\", \"Z : %5.2f\", \"XYZ : %5.2f\" };\n   static const char *rotationInfoMask[] = { \"X : %5.2f deg %5.2f rad\", \"Y : %5.2f deg %5.2f rad\", \"Z : %5.2f deg %5.2f rad\", \"Screen : %5.2f deg %5.2f rad\" };\n   static const int translationInfoIndex[] = { 0,0,0, 1,0,0, 2,0,0, 1,2,0, 0,2,0, 0,1,0, 0,1,2 };\n   static const float quadMin = 0.5f;\n   static const float quadMax = 0.8f;\n   static const float quadUV[8] = { quadMin, quadMin, quadMin, quadMax, quadMax, quadMax, quadMax, quadMin };\n   static const int halfCircleSegmentCount = 64;\n   static const float snapTension = 0.5f;\n\n   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n   //\n   static int GetMoveType(vec_t *gizmoHitProportion);\n   static int GetRotateType();\n   static int GetScaleType();\n\n   static ImVec2 worldToPos(const vec_t& worldPos, const matrix_t& mat)\n   {\n      vec_t trans;\n      trans.TransformPoint(worldPos, mat);\n      trans *= 0.5f / trans.w;\n      trans += makeVect(0.5f, 0.5f);\n      trans.y = 1.f - trans.y;\n      trans.x *= gContext.mWidth;\n      trans.y *= gContext.mHeight;\n      trans.x += gContext.mX;\n      trans.y += gContext.mY;\n      return ImVec2(trans.x, trans.y);\n   }\n\n   static void ComputeCameraRay(vec_t &rayOrigin, vec_t &rayDir)\n   {\n      ImGuiIO& io = ImGui::GetIO();\n\n      matrix_t mViewProjInverse;\n      mViewProjInverse.Inverse(gContext.mViewMat * gContext.mProjectionMat);\n\n      float mox = ((io.MousePos.x - gContext.mX) / gContext.mWidth) * 2.f - 1.f;\n      float moy = (1.f - ((io.MousePos.y - gContext.mY) / gContext.mHeight)) * 2.f - 1.f;\n\n      rayOrigin.Transform(makeVect(mox, moy, 0.f, 1.f), mViewProjInverse);\n      rayOrigin *= 1.f / rayOrigin.w;\n      vec_t rayEnd;\n      rayEnd.Transform(makeVect(mox, moy, 1.f, 1.f), mViewProjInverse);\n      rayEnd *= 1.f / rayEnd.w;\n      rayDir = Normalized(rayEnd - rayOrigin);\n   }\n\n   static float GetSegmentLengthClipSpace(const vec_t& start, const vec_t& end)\n   {\n      vec_t startOfSegment = start;\n      startOfSegment.TransformPoint(gContext.mMVP);\n      if (fabsf(startOfSegment.w)> FLT_EPSILON) // check for axis aligned with camera direction\n         startOfSegment *= 1.f / startOfSegment.w;\n\n      vec_t endOfSegment = end;\n      endOfSegment.TransformPoint(gContext.mMVP);\n      if (fabsf(endOfSegment.w)> FLT_EPSILON) // check for axis aligned with camera direction\n         endOfSegment *= 1.f / endOfSegment.w;\n\n      vec_t clipSpaceAxis = endOfSegment - startOfSegment;\n      clipSpaceAxis.y /= gContext.mDisplayRatio;\n      float segmentLengthInClipSpace = sqrtf(clipSpaceAxis.x*clipSpaceAxis.x + clipSpaceAxis.y*clipSpaceAxis.y);\n      return segmentLengthInClipSpace;\n   }\n\n   static float GetParallelogram(const vec_t& ptO, const vec_t& ptA, const vec_t& ptB)\n   {\n      vec_t pts[] = { ptO, ptA, ptB };\n      for (unsigned int i = 0; i < 3; i++)\n      {\n         pts[i].TransformPoint(gContext.mMVP);\n         if (fabsf(pts[i].w)> FLT_EPSILON) // check for axis aligned with camera direction\n            pts[i] *= 1.f / pts[i].w;\n      }\n      vec_t segA = pts[1] - pts[0];\n      vec_t segB = pts[2] - pts[0];\n      segA.y /= gContext.mDisplayRatio;\n      segB.y /= gContext.mDisplayRatio;\n      vec_t segAOrtho = makeVect(-segA.y, segA.x);\n      segAOrtho.Normalize();\n      float dt = segAOrtho.Dot3(segB);\n      float surface = sqrtf(segA.x*segA.x + segA.y*segA.y) * fabsf(dt);\n      return surface;\n   }\n\n   inline vec_t PointOnSegment(const vec_t & point, const vec_t & vertPos1, const vec_t & vertPos2)\n   {\n      vec_t c = point - vertPos1;\n      vec_t V;\n\n      V.Normalize(vertPos2 - vertPos1);\n      float d = (vertPos2 - vertPos1).Length();\n      float t = V.Dot3(c);\n\n      if (t < 0.f)\n         return vertPos1;\n\n      if (t > d)\n         return vertPos2;\n\n      return vertPos1 + V * t;\n   }\n\n   static float IntersectRayPlane(const vec_t & rOrigin, const vec_t& rVector, const vec_t& plan)\n   {\n      float numer = plan.Dot3(rOrigin) - plan.w;\n      float denom = plan.Dot3(rVector);\n\n      if (fabsf(denom) < FLT_EPSILON)  // normal is orthogonal to vector, cant intersect\n         return -1.0f;\n\n      return -(numer / denom);\n   }\n\n   static bool IsInContextRect( ImVec2 p )\n   {\n       return IsWithin( p.x, gContext.mX, gContext.mXMax ) && IsWithin(p.y, gContext.mY, gContext.mYMax );\n   }\n\n   void SetRect(float x, float y, float width, float height)\n   {\n       gContext.mX = x;\n       gContext.mY = y;\n       gContext.mWidth = width;\n       gContext.mHeight = height;\n       gContext.mXMax = gContext.mX + gContext.mWidth;\n       gContext.mYMax = gContext.mY + gContext.mXMax;\n      gContext.mDisplayRatio = width / height;\n   }\n\n   IMGUI_API void SetOrthographic(bool isOrthographic)\n   {\n      gContext.mIsOrthographic = isOrthographic;\n   }\n\n   void SetDrawlist()\n   {\n      gContext.mDrawList = ImGui::GetWindowDrawList();\n   }\n\n   void BeginFrame()\n   {\n      ImGuiIO& io = ImGui::GetIO();\n\n      const ImU32 flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoBringToFrontOnFocus;\n      ImGui::SetNextWindowSize(io.DisplaySize);\n      ImGui::SetNextWindowPos(ImVec2(0, 0));\n      \n      ImGui::PushStyleColor(ImGuiCol_WindowBg, 0);\n      ImGui::PushStyleColor(ImGuiCol_Border, 0);\n      ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);\n     \n      ImGui::Begin(\"gizmo\", NULL, flags);\n      gContext.mDrawList = ImGui::GetWindowDrawList();\n      ImGui::End();\n      ImGui::PopStyleVar();\n      ImGui::PopStyleColor(2);\n   }\n\n   bool IsUsing()\n   {\n      return gContext.mbUsing||gContext.mbUsingBounds;\n   }\n\n   bool IsOver()\n   {\n      return (GetMoveType(NULL) != NONE) || GetRotateType() != NONE || GetScaleType() != NONE || IsUsing();\n   }\n\n   void Enable(bool enable)\n   {\n      gContext.mbEnable = enable;\n      if (!enable)\n      {\n          gContext.mbUsing = false;\n          gContext.mbUsingBounds = false;\n      }\n   }\n\n   static void ComputeContext(const float *view, const float *projection, float *matrix, MODE mode)\n   {\n      gContext.mMode = mode;\n      gContext.mViewMat = *(matrix_t*)view;\n      gContext.mProjectionMat = *(matrix_t*)projection;\n\n      if (mode == LOCAL)\n      {\n         gContext.mModel = *(matrix_t*)matrix;\n         gContext.mModel.OrthoNormalize();\n      }\n      else\n      {\n         gContext.mModel.Translation(((matrix_t*)matrix)->v.position);\n      }\n      gContext.mModelSource = *(matrix_t*)matrix;\n      gContext.mModelScaleOrigin.Set(gContext.mModelSource.v.right.Length(), gContext.mModelSource.v.up.Length(), gContext.mModelSource.v.dir.Length());\n\n      gContext.mModelInverse.Inverse(gContext.mModel);\n      gContext.mModelSourceInverse.Inverse(gContext.mModelSource);\n      gContext.mViewProjection = gContext.mViewMat * gContext.mProjectionMat;\n      gContext.mMVP = gContext.mModel * gContext.mViewProjection;\n\n      matrix_t viewInverse;\n      viewInverse.Inverse(gContext.mViewMat);\n      gContext.mCameraDir = viewInverse.v.dir;\n      gContext.mCameraEye = viewInverse.v.position;\n      gContext.mCameraRight = viewInverse.v.right;\n      gContext.mCameraUp = viewInverse.v.up;\n\n     // compute scale from the size of camera right vector projected on screen at the matrix position\n     vec_t pointRight = viewInverse.v.right;\n     pointRight.TransformPoint(gContext.mViewProjection);\n     gContext.mScreenFactor = gGizmoSizeClipSpace / (pointRight.x / pointRight.w - gContext.mMVP.v.position.x / gContext.mMVP.v.position.w);\n\n     vec_t rightViewInverse = viewInverse.v.right;\n     rightViewInverse.TransformVector(gContext.mModelInverse);\n     float rightLength = GetSegmentLengthClipSpace(makeVect(0.f, 0.f), rightViewInverse);\n     gContext.mScreenFactor = gGizmoSizeClipSpace / rightLength;\n\n      ImVec2 centerSSpace = worldToPos(makeVect(0.f, 0.f), gContext.mMVP);\n      gContext.mScreenSquareCenter = centerSSpace;\n      gContext.mScreenSquareMin = ImVec2(centerSSpace.x - 10.f, centerSSpace.y - 10.f);\n      gContext.mScreenSquareMax = ImVec2(centerSSpace.x + 10.f, centerSSpace.y + 10.f);\n\n      ComputeCameraRay(gContext.mRayOrigin, gContext.mRayVector);\n   }\n\n   static void ComputeColors(ImU32 *colors, int type, OPERATION operation)\n   {\n      if (gContext.mbEnable)\n      {\n         switch (operation)\n         {\n         case TRANSLATE:\n            colors[0] = (type == MOVE_SCREEN) ? selectionColor : 0xFFFFFFFF;\n            for (int i = 0; i < 3; i++)\n            {\n               colors[i + 1] = (type == (int)(MOVE_X + i)) ? selectionColor : directionColor[i];\n               colors[i + 4] = (type == (int)(MOVE_YZ + i)) ? selectionColor : planeColor[i];\n               colors[i + 4] = (type == MOVE_SCREEN) ? selectionColor : colors[i + 4];\n            }\n            break;\n         case ROTATE:\n            colors[0] = (type == ROTATE_SCREEN) ? selectionColor : 0xFFFFFFFF;\n            for (int i = 0; i < 3; i++)\n               colors[i + 1] = (type == (int)(ROTATE_X + i)) ? selectionColor : directionColor[i];\n            break;\n         case SCALE:\n            colors[0] = (type == SCALE_XYZ) ? selectionColor : 0xFFFFFFFF;\n            for (int i = 0; i < 3; i++)\n               colors[i + 1] = (type == (int)(SCALE_X + i)) ? selectionColor : directionColor[i];\n            break;\n         case BOUNDS:\n            break;\n         }\n      }\n      else\n      {\n         for (int i = 0; i < 7; i++)\n            colors[i] = inactiveColor;\n      }\n   }\n\n   static void ComputeTripodAxisAndVisibility(int axisIndex, vec_t& dirAxis, vec_t& dirPlaneX, vec_t& dirPlaneY, bool& belowAxisLimit, bool& belowPlaneLimit)\n   {\n      dirAxis = directionUnary[axisIndex];\n      dirPlaneX = directionUnary[(axisIndex + 1) % 3];\n      dirPlaneY = directionUnary[(axisIndex + 2) % 3];\n\n      if (gContext.mbUsing)\n      {\n         // when using, use stored factors so the gizmo doesn't flip when we translate\n         belowAxisLimit = gContext.mBelowAxisLimit[axisIndex];\n         belowPlaneLimit = gContext.mBelowPlaneLimit[axisIndex];\n\n         dirAxis *= gContext.mAxisFactor[axisIndex];\n         dirPlaneX *= gContext.mAxisFactor[(axisIndex + 1) % 3];\n         dirPlaneY *= gContext.mAxisFactor[(axisIndex + 2) % 3];\n      }\n      else\n      {\n         // new method\n         float lenDir = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), dirAxis);\n         float lenDirMinus = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), -dirAxis);\n\n         float lenDirPlaneX = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), dirPlaneX);\n         float lenDirMinusPlaneX = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), -dirPlaneX);\n\n         float lenDirPlaneY = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), dirPlaneY);\n         float lenDirMinusPlaneY = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), -dirPlaneY);\n\n         float mulAxis = (lenDir < lenDirMinus && fabsf(lenDir - lenDirMinus) > FLT_EPSILON) ? -1.f : 1.f;\n         float mulAxisX = (lenDirPlaneX < lenDirMinusPlaneX && fabsf(lenDirPlaneX - lenDirMinusPlaneX) > FLT_EPSILON) ? -1.f : 1.f;\n         float mulAxisY = (lenDirPlaneY < lenDirMinusPlaneY && fabsf(lenDirPlaneY - lenDirMinusPlaneY) > FLT_EPSILON) ? -1.f : 1.f;\n         dirAxis *= mulAxis;\n         dirPlaneX *= mulAxisX;\n         dirPlaneY *= mulAxisY;\n\n         // for axis\n         float axisLengthInClipSpace = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), dirAxis * gContext.mScreenFactor);\n\n         float paraSurf = GetParallelogram(makeVect(0.f, 0.f, 0.f), dirPlaneX * gContext.mScreenFactor, dirPlaneY * gContext.mScreenFactor);\n         belowPlaneLimit = (paraSurf > 0.0025f);\n         belowAxisLimit = (axisLengthInClipSpace > 0.02f);\n\n         // and store values\n         gContext.mAxisFactor[axisIndex] = mulAxis;\n         gContext.mAxisFactor[(axisIndex + 1) % 3] = mulAxisX;\n         gContext.mAxisFactor[(axisIndex + 2) % 3] = mulAxisY;\n         gContext.mBelowAxisLimit[axisIndex] = belowAxisLimit;\n         gContext.mBelowPlaneLimit[axisIndex] = belowPlaneLimit;\n      }\n   }\n\n   static void ComputeSnap(float*value, float snap)\n   {\n      if (snap <= FLT_EPSILON)\n         return;\n      float modulo = fmodf(*value, snap);\n      float moduloRatio = fabsf(modulo) / snap;\n      if (moduloRatio < snapTension)\n         *value -= modulo;\n      else if (moduloRatio >(1.f - snapTension))\n         *value = *value - modulo + snap * ((*value<0.f) ? -1.f : 1.f);\n   }\n   static void ComputeSnap(vec_t& value, float *snap)\n   {\n      for (int i = 0; i < 3; i++)\n      {\n         ComputeSnap(&value[i], snap[i]);\n      }\n   }\n\n   static float ComputeAngleOnPlan()\n   {\n      const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan);\n      vec_t localPos = Normalized(gContext.mRayOrigin + gContext.mRayVector * len - gContext.mModel.v.position);\n\n      vec_t perpendicularVector;\n      perpendicularVector.Cross(gContext.mRotationVectorSource, gContext.mTranslationPlan);\n      perpendicularVector.Normalize();\n      float acosAngle = Clamp(Dot(localPos, gContext.mRotationVectorSource), -0.9999f, 0.9999f);\n      float angle = acosf(acosAngle);\n      angle *= (Dot(localPos, perpendicularVector) < 0.f) ? 1.f : -1.f;\n      return angle;\n   }\n\n   static void DrawRotationGizmo(int type)\n   {\n      ImDrawList* drawList = gContext.mDrawList;\n\n      // colors\n      ImU32 colors[7];\n      ComputeColors(colors, type, ROTATE);\n\n     vec_t cameraToModelNormalized;\n     if (gContext.mIsOrthographic)\n     {\n        matrix_t viewInverse;\n        viewInverse.Inverse(*(matrix_t*)&gContext.mViewMat);\n        cameraToModelNormalized = viewInverse.v.dir;\n     }\n     else\n     {\n        cameraToModelNormalized = Normalized(gContext.mModel.v.position - gContext.mCameraEye);\n     }\n\n      cameraToModelNormalized.TransformVector(gContext.mModelInverse);\n\n      gContext.mRadiusSquareCenter = screenRotateSize * gContext.mHeight;\n\n     for (int axis = 0; axis < 3; axis++)\n      {\n         ImVec2 circlePos[halfCircleSegmentCount];\n\n         float angleStart = atan2f(cameraToModelNormalized[(4-axis)%3], cameraToModelNormalized[(3 - axis) % 3]) + ZPI * 0.5f;\n\n         for (unsigned int i = 0; i < halfCircleSegmentCount; i++)\n         {\n            float ng = angleStart + ZPI * ((float)i / (float)halfCircleSegmentCount);\n            vec_t axisPos = makeVect(cosf(ng), sinf(ng), 0.f);\n            vec_t pos = makeVect(axisPos[axis], axisPos[(axis+1)%3], axisPos[(axis+2)%3]) * gContext.mScreenFactor;\n            circlePos[i] = worldToPos(pos, gContext.mMVP);\n         }\n\n         float radiusAxis = sqrtf( (ImLengthSqr(worldToPos(gContext.mModel.v.position, gContext.mViewProjection) - circlePos[0]) ));\n         if(radiusAxis > gContext.mRadiusSquareCenter)\n           gContext.mRadiusSquareCenter = radiusAxis;\n\n         drawList->AddPolyline(circlePos, halfCircleSegmentCount, colors[3 - axis], false, 2);\n      }\n      drawList->AddCircle(worldToPos(gContext.mModel.v.position, gContext.mViewProjection), gContext.mRadiusSquareCenter, colors[0], 64, 3.f);\n\n      if (gContext.mbUsing)\n      {\n         ImVec2 circlePos[halfCircleSegmentCount +1];\n\n         circlePos[0] = worldToPos(gContext.mModel.v.position, gContext.mViewProjection);\n         for (unsigned int i = 1; i < halfCircleSegmentCount; i++)\n         {\n            float ng = gContext.mRotationAngle * ((float)(i-1) / (float)(halfCircleSegmentCount -1));\n            matrix_t rotateVectorMatrix;\n            rotateVectorMatrix.RotationAxis(gContext.mTranslationPlan, ng);\n            vec_t pos;\n            pos.TransformPoint(gContext.mRotationVectorSource, rotateVectorMatrix);\n            pos *= gContext.mScreenFactor;\n            circlePos[i] = worldToPos(pos + gContext.mModel.v.position, gContext.mViewProjection);\n         }\n         drawList->AddConvexPolyFilled(circlePos, halfCircleSegmentCount, 0x801080FF);\n         drawList->AddPolyline(circlePos, halfCircleSegmentCount, 0xFF1080FF, true, 2);\n\n         ImVec2 destinationPosOnScreen = circlePos[1];\n         char tmps[512];\n         ImFormatString(tmps, sizeof(tmps), rotationInfoMask[type - ROTATE_X], (gContext.mRotationAngle/ZPI)*180.f, gContext.mRotationAngle);\n         drawList->AddText(ImVec2(destinationPosOnScreen.x + 15, destinationPosOnScreen.y + 15), 0xFF000000, tmps);\n         drawList->AddText(ImVec2(destinationPosOnScreen.x + 14, destinationPosOnScreen.y + 14), 0xFFFFFFFF, tmps);\n      }\n   }\n\n   static void DrawHatchedAxis(const vec_t& axis)\n   {\n      for (int j = 1; j < 10; j++)\n      {\n         ImVec2 baseSSpace2 = worldToPos(axis * 0.05f * (float)(j * 2) * gContext.mScreenFactor, gContext.mMVP);\n         ImVec2 worldDirSSpace2 = worldToPos(axis * 0.05f * (float)(j * 2 + 1) * gContext.mScreenFactor, gContext.mMVP);\n         gContext.mDrawList->AddLine(baseSSpace2, worldDirSSpace2, 0x80000000, 6.f);\n      }\n   }\n\n   static void DrawScaleGizmo(int type)\n   {\n      ImDrawList* drawList = gContext.mDrawList;\n\n      // colors\n      ImU32 colors[7];\n      ComputeColors(colors, type, SCALE);\n\n      // draw\n      vec_t scaleDisplay = { 1.f, 1.f, 1.f, 1.f };\n\n      if (gContext.mbUsing)\n         scaleDisplay = gContext.mScale;\n\n      for (unsigned int i = 0; i < 3; i++)\n      {\n        vec_t dirPlaneX, dirPlaneY, dirAxis;\n         bool belowAxisLimit, belowPlaneLimit;\n         ComputeTripodAxisAndVisibility(i, dirAxis, dirPlaneX, dirPlaneY, belowAxisLimit, belowPlaneLimit);\n\n         // draw axis\n         if (belowAxisLimit)\n         {\n            ImVec2 baseSSpace = worldToPos(dirAxis * 0.1f * gContext.mScreenFactor, gContext.mMVP);\n            ImVec2 worldDirSSpaceNoScale = worldToPos(dirAxis * gContext.mScreenFactor, gContext.mMVP);\n            ImVec2 worldDirSSpace = worldToPos((dirAxis * scaleDisplay[i]) * gContext.mScreenFactor, gContext.mMVP);\n\n            if (gContext.mbUsing)\n            {\n               drawList->AddLine(baseSSpace, worldDirSSpaceNoScale, 0xFF404040, 3.f);\n               drawList->AddCircleFilled(worldDirSSpaceNoScale, 6.f, 0xFF404040);\n            }\n\n            drawList->AddLine(baseSSpace, worldDirSSpace, colors[i + 1], 3.f);\n            drawList->AddCircleFilled(worldDirSSpace, 6.f, colors[i + 1]);\n\n            if (gContext.mAxisFactor[i] < 0.f)\n               DrawHatchedAxis(dirAxis * scaleDisplay[i]);\n         }\n      }\n\n      // draw screen cirle\n      drawList->AddCircleFilled(gContext.mScreenSquareCenter, 6.f, colors[0], 32);\n\n      if (gContext.mbUsing)\n      {\n         //ImVec2 sourcePosOnScreen = worldToPos(gContext.mMatrixOrigin, gContext.mViewProjection);\n         ImVec2 destinationPosOnScreen = worldToPos(gContext.mModel.v.position, gContext.mViewProjection);\n         /*vec_t dif(destinationPosOnScreen.x - sourcePosOnScreen.x, destinationPosOnScreen.y - sourcePosOnScreen.y);\n         dif.Normalize();\n         dif *= 5.f;\n         drawList->AddCircle(sourcePosOnScreen, 6.f, translationLineColor);\n         drawList->AddCircle(destinationPosOnScreen, 6.f, translationLineColor);\n         drawList->AddLine(ImVec2(sourcePosOnScreen.x + dif.x, sourcePosOnScreen.y + dif.y), ImVec2(destinationPosOnScreen.x - dif.x, destinationPosOnScreen.y - dif.y), translationLineColor, 2.f);\n         */\n         char tmps[512];\n         //vec_t deltaInfo = gContext.mModel.v.position - gContext.mMatrixOrigin;\n         int componentInfoIndex = (type - SCALE_X) * 3;\n         ImFormatString(tmps, sizeof(tmps), scaleInfoMask[type - SCALE_X], scaleDisplay[translationInfoIndex[componentInfoIndex]]);\n         drawList->AddText(ImVec2(destinationPosOnScreen.x + 15, destinationPosOnScreen.y + 15), 0xFF000000, tmps);\n         drawList->AddText(ImVec2(destinationPosOnScreen.x + 14, destinationPosOnScreen.y + 14), 0xFFFFFFFF, tmps);\n      }\n   }\n\n\n   static void DrawTranslationGizmo(int type)\n   {\n      ImDrawList* drawList = gContext.mDrawList;\n      if (!drawList)\n          return;\n\n      // colors\n      ImU32 colors[7];\n      ComputeColors(colors, type, TRANSLATE);\n\n      const ImVec2 origin = worldToPos(gContext.mModel.v.position, gContext.mViewProjection);\n\n      // draw\n      bool belowAxisLimit = false;\n      bool belowPlaneLimit = false;\n      for (unsigned int i = 0; i < 3; ++i)\n      {\n         vec_t dirPlaneX, dirPlaneY, dirAxis;\n         ComputeTripodAxisAndVisibility(i, dirAxis, dirPlaneX, dirPlaneY, belowAxisLimit, belowPlaneLimit);\n\n         // draw axis\n         if (belowAxisLimit)\n         {\n            ImVec2 baseSSpace = worldToPos(dirAxis * 0.1f * gContext.mScreenFactor, gContext.mMVP);\n            ImVec2 worldDirSSpace = worldToPos(dirAxis * gContext.mScreenFactor, gContext.mMVP);\n\n            drawList->AddLine(baseSSpace, worldDirSSpace, colors[i + 1], 3.f);\n\n            // Arrow head begin\n            ImVec2 dir(origin - worldDirSSpace);\n\n            float d = sqrtf(ImLengthSqr(dir));\n            dir /= d; // Normalize\n            dir *= 6.0f;\n\n            ImVec2 ortogonalDir(dir.y, -dir.x); // Perpendicular vector\n            ImVec2 a(worldDirSSpace + dir);\n            drawList->AddTriangleFilled(worldDirSSpace - dir, a + ortogonalDir, a - ortogonalDir, colors[i + 1]);\n            // Arrow head end\n\n            if (gContext.mAxisFactor[i] < 0.f)\n               DrawHatchedAxis(dirAxis);\n         }\n\n         // draw plane\n         if (belowPlaneLimit)\n         {\n            ImVec2 screenQuadPts[4];\n            for (int j = 0; j < 4; ++j)\n            {\n               vec_t cornerWorldPos = (dirPlaneX * quadUV[j * 2] + dirPlaneY  * quadUV[j * 2 + 1]) * gContext.mScreenFactor;\n               screenQuadPts[j] = worldToPos(cornerWorldPos, gContext.mMVP);\n            }\n            drawList->AddPolyline(screenQuadPts, 4, directionColor[i], true, 1.0f);\n            drawList->AddConvexPolyFilled(screenQuadPts, 4, colors[i + 4]);\n         }\n      }\n\n      drawList->AddCircleFilled(gContext.mScreenSquareCenter, 6.f, colors[0], 32);\n\n      if (gContext.mbUsing)\n      {\n         ImVec2 sourcePosOnScreen = worldToPos(gContext.mMatrixOrigin, gContext.mViewProjection);\n         ImVec2 destinationPosOnScreen = worldToPos(gContext.mModel.v.position, gContext.mViewProjection);\n         vec_t dif = { destinationPosOnScreen.x - sourcePosOnScreen.x, destinationPosOnScreen.y - sourcePosOnScreen.y, 0.f, 0.f };\n         dif.Normalize();\n         dif *= 5.f;\n         drawList->AddCircle(sourcePosOnScreen, 6.f, translationLineColor);\n         drawList->AddCircle(destinationPosOnScreen, 6.f, translationLineColor);\n         drawList->AddLine(ImVec2(sourcePosOnScreen.x + dif.x, sourcePosOnScreen.y + dif.y), ImVec2(destinationPosOnScreen.x - dif.x, destinationPosOnScreen.y - dif.y), translationLineColor, 2.f);\n\n         char tmps[512];\n         vec_t deltaInfo = gContext.mModel.v.position - gContext.mMatrixOrigin;\n         int componentInfoIndex = (type - MOVE_X) * 3;\n         ImFormatString(tmps, sizeof(tmps), translationInfoMask[type - MOVE_X], deltaInfo[translationInfoIndex[componentInfoIndex]], deltaInfo[translationInfoIndex[componentInfoIndex + 1]], deltaInfo[translationInfoIndex[componentInfoIndex + 2]]);\n         drawList->AddText(ImVec2(destinationPosOnScreen.x + 15, destinationPosOnScreen.y + 15), 0xFF000000, tmps);\n         drawList->AddText(ImVec2(destinationPosOnScreen.x + 14, destinationPosOnScreen.y + 14), 0xFFFFFFFF, tmps);\n      }\n   }\n\n   static bool CanActivate()\n   {\n      if (ImGui::IsMouseClicked(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemActive())\n         return true;\n      return false;\n   }\n\n   static void HandleAndDrawLocalBounds(float *bounds, matrix_t *matrix, float *snapValues, OPERATION operation)\n   {\n       ImGuiIO& io = ImGui::GetIO();\n       ImDrawList* drawList = gContext.mDrawList;\n\n       // compute best projection axis\n       vec_t axesWorldDirections[3];\n       vec_t bestAxisWorldDirection = { 0.0f, 0.0f, 0.0f, 0.0f };\n       int axes[3];\n       unsigned int numAxes = 1;\n       axes[0] = gContext.mBoundsBestAxis;\n       int bestAxis = axes[0];\n       if (!gContext.mbUsingBounds)\n       {\n           numAxes = 0;\n           float bestDot = 0.f;\n           for (unsigned int i = 0; i < 3; i++)\n           {\n               vec_t dirPlaneNormalWorld;\n               dirPlaneNormalWorld.TransformVector(directionUnary[i], gContext.mModelSource);\n               dirPlaneNormalWorld.Normalize();\n\n               float dt = fabsf( Dot(Normalized(gContext.mCameraEye - gContext.mModelSource.v.position), dirPlaneNormalWorld) );\n               if ( dt >= bestDot )\n               {\n                   bestDot = dt;\n                   bestAxis = i;\n                   bestAxisWorldDirection = dirPlaneNormalWorld;\n               }\n\n               if( dt >= 0.1f )\n               {\n                   axes[numAxes] = i;\n                   axesWorldDirections[numAxes] = dirPlaneNormalWorld;\n                   ++numAxes;\n               }\n           }\n       }\n\n       if( numAxes == 0 )\n       {\n            axes[0] = bestAxis;\n            axesWorldDirections[0] = bestAxisWorldDirection;\n            numAxes = 1;\n       }\n       else if( bestAxis != axes[0] )\n       {\n          unsigned int bestIndex = 0;\n          for (unsigned int i = 0; i < numAxes; i++)\n          {\n              if( axes[i] == bestAxis )\n              {\n                  bestIndex = i;\n                  break;\n              }\n          }\n          int tempAxis = axes[0];\n          axes[0] = axes[bestIndex];\n          axes[bestIndex] = tempAxis;\n          vec_t tempDirection = axesWorldDirections[0];\n          axesWorldDirections[0] = axesWorldDirections[bestIndex];\n          axesWorldDirections[bestIndex] = tempDirection;\n       }\n\n       for (unsigned int axisIndex = 0; axisIndex < numAxes; ++axisIndex)\n       {\n           bestAxis = axes[axisIndex];\n           bestAxisWorldDirection = axesWorldDirections[axisIndex];\n\n           // corners\n           vec_t aabb[4];\n\n           int secondAxis = (bestAxis + 1) % 3;\n           int thirdAxis = (bestAxis + 2) % 3;\n\n           for (int i = 0; i < 4; i++)\n           {\n               aabb[i][3] = aabb[i][bestAxis] = 0.f;\n               aabb[i][secondAxis] = bounds[secondAxis + 3 * (i >> 1)];\n               aabb[i][thirdAxis] = bounds[thirdAxis + 3 * ((i >> 1) ^ (i & 1))];\n           }\n\n           // draw bounds\n           unsigned int anchorAlpha = gContext.mbEnable ? 0xFF000000 : 0x80000000;\n\n           matrix_t boundsMVP = gContext.mModelSource * gContext.mViewProjection;\n           for (int i = 0; i < 4;i++)\n           {\n               ImVec2 worldBound1 = worldToPos(aabb[i], boundsMVP);\n               ImVec2 worldBound2 = worldToPos(aabb[(i+1)%4], boundsMVP);\n               if( !IsInContextRect( worldBound1 ) || !IsInContextRect( worldBound2 ) )\n               {\n                   continue;\n               }\n               float boundDistance = sqrtf(ImLengthSqr(worldBound1 - worldBound2));\n               int stepCount = (int)(boundDistance / 10.f);\n               stepCount = min( stepCount, 1000 );\n               float stepLength = 1.f / (float)stepCount;\n               for (int j = 0; j < stepCount; j++)\n               {\n                   float t1 = (float)j * stepLength;\n                   float t2 = (float)j * stepLength + stepLength * 0.5f;\n                   ImVec2 worldBoundSS1 = ImLerp(worldBound1, worldBound2, ImVec2(t1, t1));\n                   ImVec2 worldBoundSS2 = ImLerp(worldBound1, worldBound2, ImVec2(t2, t2));\n                   //drawList->AddLine(worldBoundSS1, worldBoundSS2, 0x000000 + anchorAlpha, 3.f);\n               drawList->AddLine(worldBoundSS1, worldBoundSS2, 0xAAAAAA + anchorAlpha, 2.f);\n               }\n               vec_t midPoint = (aabb[i] + aabb[(i + 1) % 4] ) * 0.5f;\n               ImVec2 midBound = worldToPos(midPoint, boundsMVP);\n               static const float AnchorBigRadius = 8.f;\n               static const float AnchorSmallRadius = 6.f;\n               bool overBigAnchor = ImLengthSqr(worldBound1 - io.MousePos) <= (AnchorBigRadius*AnchorBigRadius);\n               bool overSmallAnchor = ImLengthSqr(midBound - io.MousePos) <= (AnchorBigRadius*AnchorBigRadius);\n\n            int type = NONE;\n            vec_t gizmoHitProportion;\n\n            switch (operation)\n            {\n            case TRANSLATE: type = GetMoveType(&gizmoHitProportion); break;\n            case ROTATE: type = GetRotateType(); break;\n            case SCALE: type = GetScaleType(); break;\n            case BOUNDS: break;\n            }\n            if (type != NONE)\n            {\n               overBigAnchor = false;\n               overSmallAnchor = false;\n            }\n\n\n               unsigned int bigAnchorColor = overBigAnchor ? selectionColor : (0xAAAAAA + anchorAlpha);\n               unsigned int smallAnchorColor = overSmallAnchor ? selectionColor : (0xAAAAAA + anchorAlpha);\n\n               drawList->AddCircleFilled(worldBound1, AnchorBigRadius, 0xFF000000);\n            drawList->AddCircleFilled(worldBound1, AnchorBigRadius-1.2f, bigAnchorColor);\n\n               drawList->AddCircleFilled(midBound, AnchorSmallRadius, 0xFF000000);\n            drawList->AddCircleFilled(midBound, AnchorSmallRadius-1.2f, smallAnchorColor);\n               int oppositeIndex = (i + 2) % 4;\n               // big anchor on corners\n               if (!gContext.mbUsingBounds && gContext.mbEnable && overBigAnchor && CanActivate())\n               {\n                   gContext.mBoundsPivot.TransformPoint(aabb[(i + 2) % 4], gContext.mModelSource);\n                   gContext.mBoundsAnchor.TransformPoint(aabb[i], gContext.mModelSource);\n                   gContext.mBoundsPlan = BuildPlan(gContext.mBoundsAnchor, bestAxisWorldDirection);\n                   gContext.mBoundsBestAxis = bestAxis;\n                   gContext.mBoundsAxis[0] = secondAxis;\n                   gContext.mBoundsAxis[1] = thirdAxis;\n\n                   gContext.mBoundsLocalPivot.Set(0.f);\n                   gContext.mBoundsLocalPivot[secondAxis] = aabb[oppositeIndex][secondAxis];\n                   gContext.mBoundsLocalPivot[thirdAxis] = aabb[oppositeIndex][thirdAxis];\n\n                   gContext.mbUsingBounds = true;\n                   gContext.mBoundsMatrix = gContext.mModelSource;\n               }\n               // small anchor on middle of segment\n               if (!gContext.mbUsingBounds && gContext.mbEnable && overSmallAnchor && CanActivate())\n               {\n                   vec_t midPointOpposite = (aabb[(i + 2) % 4] + aabb[(i + 3) % 4]) * 0.5f;\n                   gContext.mBoundsPivot.TransformPoint(midPointOpposite, gContext.mModelSource);\n                   gContext.mBoundsAnchor.TransformPoint(midPoint, gContext.mModelSource);\n                   gContext.mBoundsPlan = BuildPlan(gContext.mBoundsAnchor, bestAxisWorldDirection);\n                   gContext.mBoundsBestAxis = bestAxis;\n                   int indices[] = { secondAxis , thirdAxis };\n                   gContext.mBoundsAxis[0] = indices[i%2];\n                   gContext.mBoundsAxis[1] = -1;\n\n                   gContext.mBoundsLocalPivot.Set(0.f);\n                   gContext.mBoundsLocalPivot[gContext.mBoundsAxis[0]] = aabb[oppositeIndex][indices[i % 2]];// bounds[gContext.mBoundsAxis[0]] * (((i + 1) & 2) ? 1.f : -1.f);\n\n                   gContext.mbUsingBounds = true;\n                   gContext.mBoundsMatrix = gContext.mModelSource;\n               }\n           }\n\n           if (gContext.mbUsingBounds)\n           {\n               matrix_t scale;\n               scale.SetToIdentity();\n\n               // compute projected mouse position on plan\n               const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mBoundsPlan);\n               vec_t newPos = gContext.mRayOrigin + gContext.mRayVector * len;\n\n               // compute a reference and delta vectors base on mouse move\n               vec_t deltaVector = (newPos - gContext.mBoundsPivot).Abs();\n               vec_t referenceVector = (gContext.mBoundsAnchor - gContext.mBoundsPivot).Abs();\n\n               // for 1 or 2 axes, compute a ratio that's used for scale and snap it based on resulting length\n               for (int i = 0; i < 2; i++)\n               {\n                   int axisIndex1 = gContext.mBoundsAxis[i];\n                   if (axisIndex1 == -1)\n                       continue;\n\n                   float ratioAxis = 1.f;\n                   vec_t axisDir = gContext.mBoundsMatrix.component[axisIndex1].Abs();\n\n                   float dtAxis = axisDir.Dot(referenceVector);\n                   float boundSize = bounds[axisIndex1 + 3] - bounds[axisIndex1];\n                   if (dtAxis > FLT_EPSILON)\n                       ratioAxis = axisDir.Dot(deltaVector) / dtAxis;\n\n                   if (snapValues)\n                   {\n                       float length = boundSize * ratioAxis;\n                       ComputeSnap(&length, snapValues[axisIndex1]);\n                       if (boundSize > FLT_EPSILON)\n                           ratioAxis = length / boundSize;\n                   }\n                   scale.component[axisIndex1] *= ratioAxis;\n               }\n\n               // transform matrix\n               matrix_t preScale, postScale;\n               preScale.Translation(-gContext.mBoundsLocalPivot);\n               postScale.Translation(gContext.mBoundsLocalPivot);\n               matrix_t res = preScale * scale * postScale * gContext.mBoundsMatrix;\n               *matrix = res;\n\n               // info text\n               char tmps[512];\n               ImVec2 destinationPosOnScreen = worldToPos(gContext.mModel.v.position, gContext.mViewProjection);\n               ImFormatString(tmps, sizeof(tmps), \"X: %.2f Y: %.2f Z:%.2f\"\n                   , (bounds[3] - bounds[0]) * gContext.mBoundsMatrix.component[0].Length() * scale.component[0].Length()\n                   , (bounds[4] - bounds[1]) * gContext.mBoundsMatrix.component[1].Length() * scale.component[1].Length()\n                   , (bounds[5] - bounds[2]) * gContext.mBoundsMatrix.component[2].Length() * scale.component[2].Length()\n               );\n               drawList->AddText(ImVec2(destinationPosOnScreen.x + 15, destinationPosOnScreen.y + 15), 0xFF000000, tmps);\n               drawList->AddText(ImVec2(destinationPosOnScreen.x + 14, destinationPosOnScreen.y + 14), 0xFFFFFFFF, tmps);\n            }\n\n           if (!io.MouseDown[0])\n               gContext.mbUsingBounds = false;\n\n           if( gContext.mbUsingBounds )\n               break;\n       }\n   }\n\n   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n   //\n\n   static int GetScaleType()\n   {\n      ImGuiIO& io = ImGui::GetIO();\n      int type = NONE;\n\n      // screen\n      if (io.MousePos.x >= gContext.mScreenSquareMin.x && io.MousePos.x <= gContext.mScreenSquareMax.x &&\n         io.MousePos.y >= gContext.mScreenSquareMin.y && io.MousePos.y <= gContext.mScreenSquareMax.y)\n         type = SCALE_XYZ;\n\n      // compute\n      for (unsigned int i = 0; i < 3 && type == NONE; i++)\n      {\n         vec_t dirPlaneX, dirPlaneY, dirAxis;\n         bool belowAxisLimit, belowPlaneLimit;\n         ComputeTripodAxisAndVisibility(i, dirAxis, dirPlaneX, dirPlaneY, belowAxisLimit, belowPlaneLimit);\n\n\t\t dirAxis.TransformVector(gContext.mModel);\n\t\t dirPlaneX.TransformVector(gContext.mModel);\n\t\t dirPlaneY.TransformVector(gContext.mModel);\n\n       const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, BuildPlan(gContext.mModel.v.position, dirAxis));\n       vec_t posOnPlan = gContext.mRayOrigin + gContext.mRayVector * len;\n\n       const ImVec2 posOnPlanScreen = worldToPos(posOnPlan, gContext.mViewProjection);\n       const ImVec2 axisStartOnScreen = worldToPos(gContext.mModel.v.position + dirAxis * gContext.mScreenFactor * 0.1f, gContext.mViewProjection);\n       const ImVec2 axisEndOnScreen = worldToPos(gContext.mModel.v.position + dirAxis * gContext.mScreenFactor, gContext.mViewProjection);\n\n       vec_t closestPointOnAxis = PointOnSegment(makeVect(posOnPlanScreen), makeVect(axisStartOnScreen), makeVect(axisEndOnScreen));\n\n       if ((closestPointOnAxis - makeVect(posOnPlanScreen)).Length() < 12.f) // pixel size\n          type = SCALE_X + i;\n      }\n      return type;\n   }\n\n   static int GetRotateType()\n   {\n      ImGuiIO& io = ImGui::GetIO();\n      int type = NONE;\n\n      vec_t deltaScreen = { io.MousePos.x - gContext.mScreenSquareCenter.x, io.MousePos.y - gContext.mScreenSquareCenter.y, 0.f, 0.f };\n      float dist = deltaScreen.Length();\n      if (dist >= (gContext.mRadiusSquareCenter - 1.0f) && dist < (gContext.mRadiusSquareCenter + 1.0f))\n         type = ROTATE_SCREEN;\n\n      const vec_t planNormals[] = { gContext.mModel.v.right, gContext.mModel.v.up, gContext.mModel.v.dir};\n\n      for (unsigned int i = 0; i < 3 && type == NONE; i++)\n      {\n         // pickup plan\n         vec_t pickupPlan = BuildPlan(gContext.mModel.v.position, planNormals[i]);\n\n         const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, pickupPlan);\n         vec_t localPos = gContext.mRayOrigin + gContext.mRayVector * len - gContext.mModel.v.position;\n\n         if (Dot(Normalized(localPos), gContext.mRayVector) > FLT_EPSILON)\n            continue;\n       vec_t idealPosOnCircle = Normalized(localPos);\n       idealPosOnCircle.TransformVector(gContext.mModelInverse);\n       ImVec2 idealPosOnCircleScreen = worldToPos(idealPosOnCircle * gContext.mScreenFactor, gContext.mMVP);\n\n       //gContext.mDrawList->AddCircle(idealPosOnCircleScreen, 5.f, 0xFFFFFFFF);\n       ImVec2 distanceOnScreen = idealPosOnCircleScreen - io.MousePos;\n\n         float distance = makeVect(distanceOnScreen).Length();\n         if (distance < 8.f) // pixel size\n            type = ROTATE_X + i;\n      }\n\n      return type;\n   }\n\n   static int GetMoveType(vec_t *gizmoHitProportion)\n   {\n      ImGuiIO& io = ImGui::GetIO();\n      int type = NONE;\n\n      // screen\n      if (io.MousePos.x >= gContext.mScreenSquareMin.x && io.MousePos.x <= gContext.mScreenSquareMax.x &&\n         io.MousePos.y >= gContext.mScreenSquareMin.y && io.MousePos.y <= gContext.mScreenSquareMax.y)\n         type = MOVE_SCREEN;\n\n      // compute\n      for (unsigned int i = 0; i < 3 && type == NONE; i++)\n      {\n         vec_t dirPlaneX, dirPlaneY, dirAxis;\n         bool belowAxisLimit, belowPlaneLimit;\n         ComputeTripodAxisAndVisibility(i, dirAxis, dirPlaneX, dirPlaneY, belowAxisLimit, belowPlaneLimit);\n       dirAxis.TransformVector(gContext.mModel);\n         dirPlaneX.TransformVector(gContext.mModel);\n         dirPlaneY.TransformVector(gContext.mModel);\n\n         const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, BuildPlan(gContext.mModel.v.position, dirAxis));\n         vec_t posOnPlan = gContext.mRayOrigin + gContext.mRayVector * len;\n\n       const ImVec2 posOnPlanScreen = worldToPos(posOnPlan, gContext.mViewProjection);\n       const ImVec2 axisStartOnScreen = worldToPos(gContext.mModel.v.position + dirAxis * gContext.mScreenFactor * 0.1f, gContext.mViewProjection);\n       const ImVec2 axisEndOnScreen = worldToPos(gContext.mModel.v.position + dirAxis * gContext.mScreenFactor, gContext.mViewProjection);\n\n       vec_t closestPointOnAxis = PointOnSegment(makeVect(posOnPlanScreen), makeVect(axisStartOnScreen), makeVect(axisEndOnScreen));\n\n       if ((closestPointOnAxis - makeVect(posOnPlanScreen)).Length() < 12.f) // pixel size\n            type = MOVE_X + i;\n\n       const float dx = dirPlaneX.Dot3((posOnPlan - gContext.mModel.v.position) * (1.f / gContext.mScreenFactor));\n       const float dy = dirPlaneY.Dot3((posOnPlan - gContext.mModel.v.position) * (1.f / gContext.mScreenFactor));\n         if (belowPlaneLimit && dx >= quadUV[0] && dx <= quadUV[4] && dy >= quadUV[1] && dy <= quadUV[3])\n            type = MOVE_YZ + i;\n\n         if (gizmoHitProportion)\n            *gizmoHitProportion = makeVect(dx, dy, 0.f);\n      }\n      return type;\n   }\n\n   static void HandleTranslation(float *matrix, float *deltaMatrix, int& type, float *snap)\n   {\n      ImGuiIO& io = ImGui::GetIO();\n      bool applyRotationLocaly = gContext.mMode == LOCAL || type == MOVE_SCREEN;\n\n      // move\n      if (gContext.mbUsing)\n      {\n         ImGui::CaptureMouseFromApp();\n         const float len = fabsf(IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan)); // near plan\n         vec_t newPos = gContext.mRayOrigin + gContext.mRayVector * len;\n\n\n\n         // compute delta\n         vec_t newOrigin = newPos - gContext.mRelativeOrigin * gContext.mScreenFactor;\n         vec_t delta = newOrigin - gContext.mModel.v.position;\n\n         // 1 axis constraint\n         if (gContext.mCurrentOperation >= MOVE_X && gContext.mCurrentOperation <= MOVE_Z)\n         {\n            int axisIndex = gContext.mCurrentOperation - MOVE_X;\n            const vec_t& axisValue = *(vec_t*)&gContext.mModel.m[axisIndex];\n            float lengthOnAxis = Dot(axisValue, delta);\n            delta = axisValue * lengthOnAxis;\n         }\n\n         // snap\n         if (snap)\n         {\n            vec_t cumulativeDelta = gContext.mModel.v.position + delta - gContext.mMatrixOrigin;\n            if (applyRotationLocaly)\n            {\n               matrix_t modelSourceNormalized = gContext.mModelSource;\n               modelSourceNormalized.OrthoNormalize();\n               matrix_t modelSourceNormalizedInverse;\n               modelSourceNormalizedInverse.Inverse(modelSourceNormalized);\n               cumulativeDelta.TransformVector(modelSourceNormalizedInverse);\n               ComputeSnap(cumulativeDelta, snap);\n               cumulativeDelta.TransformVector(modelSourceNormalized);\n            }\n            else\n            {\n               ComputeSnap(cumulativeDelta, snap);\n            }\n            delta = gContext.mMatrixOrigin + cumulativeDelta - gContext.mModel.v.position;\n\n         }\n\n         // compute matrix & delta\n         matrix_t deltaMatrixTranslation;\n         deltaMatrixTranslation.Translation(delta);\n         if (deltaMatrix)\n            memcpy(deltaMatrix, deltaMatrixTranslation.m16, sizeof(float) * 16);\n\n\n         matrix_t res = gContext.mModelSource * deltaMatrixTranslation;\n         *(matrix_t*)matrix = res;\n\n         if (!io.MouseDown[0])\n            gContext.mbUsing = false;\n\n         type = gContext.mCurrentOperation;\n      }\n      else\n      {\n         // find new possible way to move\n         vec_t gizmoHitProportion;\n         type = GetMoveType(&gizmoHitProportion);\n         if(type != NONE)\n         {\n            ImGui::CaptureMouseFromApp();\n         }\n       if (CanActivate() && type != NONE)\n       {\n          gContext.mbUsing = true;\n          gContext.mCurrentOperation = type;\n          vec_t movePlanNormal[] = { gContext.mModel.v.right, gContext.mModel.v.up, gContext.mModel.v.dir,\n             gContext.mModel.v.right, gContext.mModel.v.up, gContext.mModel.v.dir,\n             -gContext.mCameraDir };\n\n          vec_t cameraToModelNormalized = Normalized(gContext.mModel.v.position - gContext.mCameraEye);\n          for (unsigned int i = 0; i < 3; i++)\n          {\n             vec_t orthoVector = Cross(movePlanNormal[i], cameraToModelNormalized);\n             movePlanNormal[i].Cross(orthoVector);\n             movePlanNormal[i].Normalize();\n          }\n            // pickup plan\n            gContext.mTranslationPlan = BuildPlan(gContext.mModel.v.position, movePlanNormal[type - MOVE_X]);\n            const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan);\n            gContext.mTranslationPlanOrigin = gContext.mRayOrigin + gContext.mRayVector * len;\n            gContext.mMatrixOrigin = gContext.mModel.v.position;\n\n            gContext.mRelativeOrigin = (gContext.mTranslationPlanOrigin - gContext.mModel.v.position) * (1.f / gContext.mScreenFactor);\n         }\n      }\n   }\n\n   static void HandleScale(float *matrix, float *deltaMatrix, int& type, float *snap)\n   {\n      ImGuiIO& io = ImGui::GetIO();\n\n      if (!gContext.mbUsing)\n      {\n         // find new possible way to scale\n         type = GetScaleType();\n         if(type != NONE)\n         {\n            ImGui::CaptureMouseFromApp();\n         }\n         if (CanActivate() && type != NONE)\n         {\n            gContext.mbUsing = true;\n            gContext.mCurrentOperation = type;\n            const vec_t movePlanNormal[] = { gContext.mModel.v.up, gContext.mModel.v.dir, gContext.mModel.v.right, gContext.mModel.v.dir, gContext.mModel.v.up, gContext.mModel.v.right, -gContext.mCameraDir };\n            // pickup plan\n\n            gContext.mTranslationPlan = BuildPlan(gContext.mModel.v.position, movePlanNormal[type - SCALE_X]);\n            const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan);\n            gContext.mTranslationPlanOrigin = gContext.mRayOrigin + gContext.mRayVector * len;\n            gContext.mMatrixOrigin = gContext.mModel.v.position;\n            gContext.mScale.Set(1.f, 1.f, 1.f);\n            gContext.mRelativeOrigin = (gContext.mTranslationPlanOrigin - gContext.mModel.v.position) * (1.f / gContext.mScreenFactor);\n            gContext.mScaleValueOrigin = makeVect(gContext.mModelSource.v.right.Length(), gContext.mModelSource.v.up.Length(), gContext.mModelSource.v.dir.Length());\n            gContext.mSaveMousePosx = io.MousePos.x;\n         }\n      }\n      // scale\n      if (gContext.mbUsing)\n      {\n         ImGui::CaptureMouseFromApp();\n         const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan);\n         vec_t newPos = gContext.mRayOrigin + gContext.mRayVector * len;\n         vec_t newOrigin = newPos - gContext.mRelativeOrigin * gContext.mScreenFactor;\n         vec_t delta = newOrigin - gContext.mModel.v.position;\n\n         // 1 axis constraint\n         if (gContext.mCurrentOperation >= SCALE_X && gContext.mCurrentOperation <= SCALE_Z)\n         {\n            int axisIndex = gContext.mCurrentOperation - SCALE_X;\n            const vec_t& axisValue = *(vec_t*)&gContext.mModel.m[axisIndex];\n            float lengthOnAxis = Dot(axisValue, delta);\n            delta = axisValue * lengthOnAxis;\n\n            vec_t baseVector = gContext.mTranslationPlanOrigin - gContext.mModel.v.position;\n            float ratio = Dot(axisValue, baseVector + delta) / Dot(axisValue, baseVector);\n\n            gContext.mScale[axisIndex] = max(ratio, 0.001f);\n         }\n         else\n         {\n            float scaleDelta = (io.MousePos.x - gContext.mSaveMousePosx)  * 0.01f;\n            gContext.mScale.Set(max(1.f + scaleDelta, 0.001f));\n         }\n\n         // snap\n         if (snap)\n         {\n            float scaleSnap[] = { snap[0], snap[0], snap[0] };\n            ComputeSnap(gContext.mScale, scaleSnap);\n         }\n\n         // no 0 allowed\n         for (int i = 0; i < 3;i++)\n            gContext.mScale[i] = max(gContext.mScale[i], 0.001f);\n\n         // compute matrix & delta\n         matrix_t deltaMatrixScale;\n         deltaMatrixScale.Scale(gContext.mScale * gContext.mScaleValueOrigin);\n\n         matrix_t res = deltaMatrixScale * gContext.mModel;\n         *(matrix_t*)matrix = res;\n\n         if (deltaMatrix)\n         {\n            deltaMatrixScale.Scale(gContext.mScale);\n            memcpy(deltaMatrix, deltaMatrixScale.m16, sizeof(float) * 16);\n         }\n\n         if (!io.MouseDown[0])\n            gContext.mbUsing = false;\n\n         type = gContext.mCurrentOperation;\n      }\n   }\n\n   static void HandleRotation(float *matrix, float *deltaMatrix, int& type, float *snap)\n   {\n      ImGuiIO& io = ImGui::GetIO();\n      bool applyRotationLocaly = gContext.mMode == LOCAL;\n\n      if (!gContext.mbUsing)\n      {\n         type = GetRotateType();\n\n         if(type != NONE)\n         {\n            ImGui::CaptureMouseFromApp();\n         }\n\n         if (type == ROTATE_SCREEN)\n         {\n            applyRotationLocaly = true;\n         }\n\n         if (CanActivate() && type != NONE)\n         {\n            gContext.mbUsing = true;\n            gContext.mCurrentOperation = type;\n            const vec_t rotatePlanNormal[] = { gContext.mModel.v.right, gContext.mModel.v.up, gContext.mModel.v.dir, -gContext.mCameraDir };\n            // pickup plan\n            if (applyRotationLocaly)\n            {\n               gContext.mTranslationPlan = BuildPlan(gContext.mModel.v.position, rotatePlanNormal[type - ROTATE_X]);\n            }\n            else\n            {\n               gContext.mTranslationPlan = BuildPlan(gContext.mModelSource.v.position, directionUnary[type - ROTATE_X]);\n            }\n\n            const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan);\n            vec_t localPos = gContext.mRayOrigin + gContext.mRayVector * len - gContext.mModel.v.position;\n            gContext.mRotationVectorSource = Normalized(localPos);\n            gContext.mRotationAngleOrigin = ComputeAngleOnPlan();\n         }\n      }\n\n      // rotation\n      if (gContext.mbUsing)\n      {\n         ImGui::CaptureMouseFromApp();\n         gContext.mRotationAngle = ComputeAngleOnPlan();\n         if (snap)\n         {\n            float snapInRadian = snap[0] * DEG2RAD;\n            ComputeSnap(&gContext.mRotationAngle, snapInRadian);\n         }\n         vec_t rotationAxisLocalSpace;\n\n         rotationAxisLocalSpace.TransformVector(makeVect(gContext.mTranslationPlan.x, gContext.mTranslationPlan.y, gContext.mTranslationPlan.z, 0.f), gContext.mModelInverse);\n         rotationAxisLocalSpace.Normalize();\n\n         matrix_t deltaRotation;\n         deltaRotation.RotationAxis(rotationAxisLocalSpace, gContext.mRotationAngle - gContext.mRotationAngleOrigin);\n         gContext.mRotationAngleOrigin = gContext.mRotationAngle;\n\n         matrix_t scaleOrigin;\n         scaleOrigin.Scale(gContext.mModelScaleOrigin);\n\n         if (applyRotationLocaly)\n         {\n            *(matrix_t*)matrix = scaleOrigin * deltaRotation * gContext.mModel;\n         }\n         else\n         {\n            matrix_t res = gContext.mModelSource;\n            res.v.position.Set(0.f);\n\n            *(matrix_t*)matrix = res * deltaRotation;\n            ((matrix_t*)matrix)->v.position = gContext.mModelSource.v.position;\n         }\n\n         if (deltaMatrix)\n         {\n            *(matrix_t*)deltaMatrix = gContext.mModelInverse * deltaRotation * gContext.mModel;\n         }\n\n         if (!io.MouseDown[0])\n            gContext.mbUsing = false;\n\n         type = gContext.mCurrentOperation;\n      }\n   }\n\n   void DecomposeMatrixToComponents(const float *matrix, float *translation, float *rotation, float *scale)\n   {\n      matrix_t mat = *(matrix_t*)matrix;\n\n      scale[0] = mat.v.right.Length();\n      scale[1] = mat.v.up.Length();\n      scale[2] = mat.v.dir.Length();\n\n      mat.OrthoNormalize();\n\n      rotation[0] = RAD2DEG * atan2f(mat.m[1][2], mat.m[2][2]);\n      rotation[1] = RAD2DEG * atan2f(-mat.m[0][2], sqrtf(mat.m[1][2] * mat.m[1][2] + mat.m[2][2]* mat.m[2][2]));\n      rotation[2] = RAD2DEG * atan2f(mat.m[0][1], mat.m[0][0]);\n\n      translation[0] = mat.v.position.x;\n      translation[1] = mat.v.position.y;\n      translation[2] = mat.v.position.z;\n   }\n\n   void RecomposeMatrixFromComponents(const float *translation, const float *rotation, const float *scale, float *matrix)\n   {\n      matrix_t& mat = *(matrix_t*)matrix;\n\n      matrix_t rot[3];\n      for (int i = 0; i < 3;i++)\n         rot[i].RotationAxis(directionUnary[i], rotation[i] * DEG2RAD);\n\n      mat = rot[0] * rot[1] * rot[2];\n\n      float validScale[3];\n      for (int i = 0; i < 3; i++)\n      {\n         if (fabsf(scale[i]) < FLT_EPSILON)\n            validScale[i] = 0.001f;\n         else\n            validScale[i] = scale[i];\n      }\n      mat.v.right *= validScale[0];\n      mat.v.up *= validScale[1];\n      mat.v.dir *= validScale[2];\n      mat.v.position.Set(translation[0], translation[1], translation[2], 1.f);\n   }\n\n   void Manipulate(const float *view, const float *projection, OPERATION operation, MODE mode, float *matrix, float *deltaMatrix, float *snap, float *localBounds, float *boundsSnap)\n   {\n      ComputeContext(view, projection, matrix, mode);\n\n      // set delta to identity\n      if (deltaMatrix)\n         ((matrix_t*)deltaMatrix)->SetToIdentity();\n\n      // behind camera\n      vec_t camSpacePosition;\n      camSpacePosition.TransformPoint(makeVect(0.f, 0.f, 0.f), gContext.mMVP);\n      if (!gContext.mIsOrthographic && camSpacePosition.z < 0.001f)\n         return;\n\n      // --\n      int type = NONE;\n      if (gContext.mbEnable)\n      {\n          if (!gContext.mbUsingBounds)\n          {\n              switch (operation)\n              {\n              case ROTATE:\n                  HandleRotation(matrix, deltaMatrix, type, snap);\n                  break;\n              case TRANSLATE:\n                  HandleTranslation(matrix, deltaMatrix, type, snap);\n                  break;\n              case SCALE:\n                  HandleScale(matrix, deltaMatrix, type, snap);\n                  break;\n              case BOUNDS:\n                  break;\n              }\n          }\n      }\n\n      if (localBounds && !gContext.mbUsing)\n          HandleAndDrawLocalBounds(localBounds, (matrix_t*)matrix, boundsSnap, operation);\n\n      if (!gContext.mbUsingBounds)\n      {\n          switch (operation)\n          {\n          case ROTATE:\n              DrawRotationGizmo(type);\n              break;\n          case TRANSLATE:\n              DrawTranslationGizmo(type);\n              break;\n          case SCALE:\n              DrawScaleGizmo(type);\n              break;\n          case BOUNDS:\n              break;\n          }\n      }\n   }\n\n   void DrawCube(const float *view, const float *projection, const float *matrix)\n   {\n      matrix_t viewInverse;\n      viewInverse.Inverse(*(matrix_t*)view);\n      const matrix_t& model = *(matrix_t*)matrix;\n      matrix_t res = *(matrix_t*)matrix * *(matrix_t*)view * *(matrix_t*)projection;\n\n      for (int iFace = 0; iFace < 6; iFace++)\n      {\n         const int normalIndex = (iFace % 3);\n         const int perpXIndex = (normalIndex + 1) % 3;\n         const int perpYIndex = (normalIndex + 2) % 3;\n         const float invert = (iFace > 2) ? -1.f : 1.f;\n\n         const vec_t faceCoords[4] = { directionUnary[normalIndex] + directionUnary[perpXIndex] + directionUnary[perpYIndex],\n            directionUnary[normalIndex] + directionUnary[perpXIndex] - directionUnary[perpYIndex],\n            directionUnary[normalIndex] - directionUnary[perpXIndex] - directionUnary[perpYIndex],\n            directionUnary[normalIndex] - directionUnary[perpXIndex] + directionUnary[perpYIndex],\n         };\n\n         // clipping\n         bool skipFace = false;\n         for (unsigned int iCoord = 0; iCoord < 4; iCoord++)\n         {\n            vec_t camSpacePosition;\n            camSpacePosition.TransformPoint(faceCoords[iCoord] * 0.5f * invert, gContext.mMVP);\n            if (camSpacePosition.z < 0.001f)\n            {\n               skipFace = true;\n               break;\n            }\n         }\n         if (skipFace)\n            continue;\n\n         // 3D->2D\n         ImVec2 faceCoordsScreen[4];\n         for (unsigned int iCoord = 0; iCoord < 4; iCoord++)\n            faceCoordsScreen[iCoord] = worldToPos(faceCoords[iCoord] * 0.5f * invert, res);\n\n         // back face culling\n         vec_t cullPos, cullNormal;\n         cullPos.TransformPoint(faceCoords[0] * 0.5f * invert, model);\n         cullNormal.TransformVector(directionUnary[normalIndex] * invert, model);\n         float dt = Dot(Normalized(cullPos - viewInverse.v.position), Normalized(cullNormal));\n         if (dt>0.f)\n            continue;\n\n         // draw face with lighter color\n         gContext.mDrawList->AddConvexPolyFilled(faceCoordsScreen, 4, directionColor[normalIndex] | 0x808080);\n      }\n   }\n\n   void DrawGrid(const float *view, const float *projection, const float *matrix, const float gridSize)\n   {\n      matrix_t res = *(matrix_t*)matrix * *(matrix_t*)view * *(matrix_t*)projection;\n\n      for (float f = -gridSize; f <= gridSize; f += 1.f)\n      {\n         gContext.mDrawList->AddLine(worldToPos(makeVect(f, 0.f, -gridSize), res), worldToPos(makeVect(f, 0.f, gridSize), res), 0xFF808080);\n         gContext.mDrawList->AddLine(worldToPos(makeVect(-gridSize, 0.f, f), res), worldToPos(makeVect(gridSize, 0.f, f), res), 0xFF808080);\n      }\n   }\n};\n\n"
  },
  {
    "path": "src/imgui/ImGuizmo.h",
    "content": "// https://github.com/CedricGuillemet/ImGuizmo\n// v 1.61 WIP\n//\n// The MIT License(MIT)\n// \n// Copyright(c) 2016 Cedric Guillemet\n// \n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files(the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions :\n// \n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n// \n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n//\n// -------------------------------------------------------------------------------------------\n// History : \n// 2016/09/11 Behind camera culling. Scaling Delta matrix not multiplied by source matrix scales. local/world rotation and translation fixed. Display message is incorrect (X: ... Y:...) in local mode.\n// 2016/09/09 Hatched negative axis. Snapping. Documentation update.\n// 2016/09/04 Axis switch and translation plan autohiding. Scale transform stability improved\n// 2016/09/01 Mogwai changed to Manipulate. Draw debug cube. Fixed inverted scale. Mixing scale and translation/rotation gives bad results.\n// 2016/08/31 First version\n//\n// -------------------------------------------------------------------------------------------\n// Future (no order):\n//\n// - Multi view\n// - display rotation/translation/scale infos in local/world space and not only local\n// - finish local/world matrix application\n// - OPERATION as bitmask\n// \n// -------------------------------------------------------------------------------------------\n// Example \n#if 0\nvoid EditTransform(const Camera& camera, matrix_t& matrix)\n{\n\tstatic ImGuizmo::OPERATION mCurrentGizmoOperation(ImGuizmo::ROTATE);\n\tstatic ImGuizmo::MODE mCurrentGizmoMode(ImGuizmo::WORLD);\n\tif (ImGui::IsKeyPressed(90))\n\t\tmCurrentGizmoOperation = ImGuizmo::TRANSLATE;\n\tif (ImGui::IsKeyPressed(69))\n\t\tmCurrentGizmoOperation = ImGuizmo::ROTATE;\n\tif (ImGui::IsKeyPressed(82)) // r Key\n\t\tmCurrentGizmoOperation = ImGuizmo::SCALE;\n\tif (ImGui::RadioButton(\"Translate\", mCurrentGizmoOperation == ImGuizmo::TRANSLATE))\n\t\tmCurrentGizmoOperation = ImGuizmo::TRANSLATE;\n\tImGui::SameLine();\n\tif (ImGui::RadioButton(\"Rotate\", mCurrentGizmoOperation == ImGuizmo::ROTATE))\n\t\tmCurrentGizmoOperation = ImGuizmo::ROTATE;\n\tImGui::SameLine();\n\tif (ImGui::RadioButton(\"Scale\", mCurrentGizmoOperation == ImGuizmo::SCALE))\n\t\tmCurrentGizmoOperation = ImGuizmo::SCALE;\n\tfloat matrixTranslation[3], matrixRotation[3], matrixScale[3];\n\tImGuizmo::DecomposeMatrixToComponents(matrix.m16, matrixTranslation, matrixRotation, matrixScale);\n\tImGui::InputFloat3(\"Tr\", matrixTranslation, 3);\n\tImGui::InputFloat3(\"Rt\", matrixRotation, 3);\n\tImGui::InputFloat3(\"Sc\", matrixScale, 3);\n\tImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, matrix.m16);\n\n\tif (mCurrentGizmoOperation != ImGuizmo::SCALE)\n\t{\n\t\tif (ImGui::RadioButton(\"Local\", mCurrentGizmoMode == ImGuizmo::LOCAL))\n\t\t\tmCurrentGizmoMode = ImGuizmo::LOCAL;\n\t\tImGui::SameLine();\n\t\tif (ImGui::RadioButton(\"World\", mCurrentGizmoMode == ImGuizmo::WORLD))\n\t\t\tmCurrentGizmoMode = ImGuizmo::WORLD;\n\t}\n\tstatic bool useSnap(false);\n\tif (ImGui::IsKeyPressed(83))\n\t\tuseSnap = !useSnap;\n\tImGui::Checkbox(\"\", &useSnap);\n\tImGui::SameLine();\n\tvec_t snap;\n\tswitch (mCurrentGizmoOperation)\n\t{\n\tcase ImGuizmo::TRANSLATE:\n\t\tsnap = config.mSnapTranslation;\n\t\tImGui::InputFloat3(\"Snap\", &snap.x);\n\t\tbreak;\n\tcase ImGuizmo::ROTATE:\n\t\tsnap = config.mSnapRotation;\n\t\tImGui::InputFloat(\"Angle Snap\", &snap.x);\n\t\tbreak;\n\tcase ImGuizmo::SCALE:\n\t\tsnap = config.mSnapScale;\n\t\tImGui::InputFloat(\"Scale Snap\", &snap.x);\n\t\tbreak;\n\t}\n\tImGuiIO& io = ImGui::GetIO();\n\tImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);\n\tImGuizmo::Manipulate(camera.mView.m16, camera.mProjection.m16, mCurrentGizmoOperation, mCurrentGizmoMode, matrix.m16, NULL, useSnap ? &snap.x : NULL);\n}\n#endif\n#pragma once\n\n#ifdef USE_IMGUI_API\n#include \"imconfig.h\"\n#endif\n#ifndef IMGUI_API\n#define IMGUI_API\n#endif\n\nnamespace ImGuizmo\n{\n\t// call inside your own window and before Manipulate() in order to draw gizmo to that window.\n\tIMGUI_API void SetDrawlist();\n\n\t// call BeginFrame right after ImGui_XXXX_NewFrame();\n\tIMGUI_API void BeginFrame();\n\n\t// return true if mouse cursor is over any gizmo control (axis, plan or screen component)\n\tIMGUI_API bool IsOver();\n\n\t// return true if mouse IsOver or if the gizmo is in moving state\n\tIMGUI_API bool IsUsing();\n\n\t// enable/disable the gizmo. Stay in the state until next call to Enable.\n\t// gizmo is rendered with gray half transparent color when disabled\n\tIMGUI_API void Enable(bool enable);\n\n\t// helper functions for manualy editing translation/rotation/scale with an input float\n\t// translation, rotation and scale float points to 3 floats each\n\t// Angles are in degrees (more suitable for human editing)\n\t// example:\n\t// float matrixTranslation[3], matrixRotation[3], matrixScale[3];\n\t// ImGuizmo::DecomposeMatrixToComponents(gizmoMatrix.m16, matrixTranslation, matrixRotation, matrixScale);\n\t// ImGui::InputFloat3(\"Tr\", matrixTranslation, 3);\n\t// ImGui::InputFloat3(\"Rt\", matrixRotation, 3);\n\t// ImGui::InputFloat3(\"Sc\", matrixScale, 3);\n\t// ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, gizmoMatrix.m16);\n\t//\n\t// These functions have some numerical stability issues for now. Use with caution.\n\tIMGUI_API void DecomposeMatrixToComponents(const float *matrix, float *translation, float *rotation, float *scale);\n\tIMGUI_API void RecomposeMatrixFromComponents(const float *translation, const float *rotation, const float *scale, float *matrix);\n\n\tIMGUI_API void SetRect(float x, float y, float width, float height);\n\t// default is false\n\tIMGUI_API void SetOrthographic(bool isOrthographic);\n\n\t// Render a cube with face color corresponding to face normal. Usefull for debug/tests\n\tIMGUI_API void DrawCube(const float *view, const float *projection, const float *matrix);\n\tIMGUI_API void DrawGrid(const float *view, const float *projection, const float *matrix, const float gridSize);\n\n\t// call it when you want a gizmo\n\t// Needs view and projection matrices. \n\t// matrix parameter is the source matrix (where will be gizmo be drawn) and might be transformed by the function. Return deltaMatrix is optional\n\t// translation is applied in world space\n\tenum OPERATION\n\t{\n\t\tTRANSLATE,\n\t\tROTATE,\n\t\tSCALE,\n\t\tBOUNDS,\n\t};\n\n\tenum MODE\n\t{\n\t\tLOCAL,\n\t\tWORLD\n\t};\n\n\tIMGUI_API void Manipulate(const float *view, const float *projection, OPERATION operation, MODE mode, float *matrix, float *deltaMatrix = 0, float *snap = 0, float *localBounds = NULL, float *boundsSnap = NULL);\n};\n"
  },
  {
    "path": "src/imgui/imconfig.hpp",
    "content": "//-----------------------------------------------------------------------------\n// COMPILE-TIME OPTIONS FOR DEAR IMGUI\n// Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure.\n// You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions.\n//-----------------------------------------------------------------------------\n// A) You may edit imconfig.h (and not overwrite it when updating imgui, or maintain a patch/branch with your modifications to imconfig.h)\n// B) or add configuration directives in your own file and compile with #define IMGUI_USER_CONFIG \"myfilename.h\"\n// If you do so you need to make sure that configuration settings are defined consistently _everywhere_ dear imgui is used, which include\n// the imgui*.cpp files but also _any_ of your code that uses imgui. This is because some compile-time options have an affect on data structures.\n// Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts.\n// Call IMGUI_CHECKVERSION() from your .cpp files to verify that the data structures your files are using are matching the ones imgui.cpp is using.\n//-----------------------------------------------------------------------------\n\n#pragma once\n\n//---- Define assertion handler. Defaults to calling assert().\n//#define IM_ASSERT(_EXPR)  MyAssert(_EXPR)\n//#define IM_ASSERT(_EXPR)  ((void)(_EXPR))     // Disable asserts\n\n//---- Define attributes of all API symbols declarations, e.g. for DLL under Windows.\n//#define IMGUI_API __declspec( dllexport )\n//#define IMGUI_API __declspec( dllimport )\n\n//---- Don't define obsolete functions/enums names. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names.\n//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n\n//---- Don't implement demo windows functionality (ShowDemoWindow()/ShowStyleEditor()/ShowUserGuide() methods will be empty)\n//---- It is very strongly recommended to NOT disable the demo windows during development. Please read the comments in imgui_demo.cpp.\n//#define IMGUI_DISABLE_DEMO_WINDOWS\n\n//---- Don't implement some functions to reduce linkage requirements.\n//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS   // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc.\n//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS         // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow.\n//#define IMGUI_DISABLE_WIN32_FUNCTIONS                     // [Win32] Won't use and link with any Win32 function.\n//#define IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS             // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself if you don't want to link with vsnprintf.\n//#define IMGUI_DISABLE_MATH_FUNCTIONS                      // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 wrapper so you can implement them yourself. Declare your prototypes in imconfig.h.\n//#define IMGUI_DISABLE_DEFAULT_ALLOCATORS                  // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions().\n\n//---- Include imgui_user.h at the end of imgui.h as a convenience\n//#define IMGUI_INCLUDE_IMGUI_USER_H\n\n//---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another)\n//#define IMGUI_USE_BGRA_PACKED_COLOR\n\n//---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version\n// By default the embedded implementations are declared static and not available outside of imgui cpp files.\n//#define IMGUI_STB_TRUETYPE_FILENAME   \"my_folder/stb_truetype.h\"\n//#define IMGUI_STB_RECT_PACK_FILENAME  \"my_folder/stb_rect_pack.h\"\n//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION\n//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION\n\n//---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4.\n// This will be inlined as part of ImVec2 and ImVec4 class declarations.\n/*\n#define IM_VEC2_CLASS_EXTRA                                                 \\\n        ImVec2(const MyVec2& f) { x = f.x; y = f.y; }                       \\\n        operator MyVec2() const { return MyVec2(x,y); }\n\n#define IM_VEC4_CLASS_EXTRA                                                 \\\n        ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; }     \\\n        operator MyVec4() const { return MyVec4(x,y,z,w); }\n*/\n\n//---- Use 32-bit vertex indices (default is 16-bit) to allow meshes with more than 64K vertices. Render function needs to support it.\n//#define ImDrawIdx unsigned int\n\n//---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files.\n/*\nnamespace ImGui\n{\n    void MyFunction(const char* name, const MyMatrix44& v);\n}\n*/\n"
  },
  {
    "path": "src/imgui/imgui.cpp",
    "content": "// dear imgui, v1.70 WIP\n// (main code and documentation)\n\n// Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code.\n// Newcomers, read 'Programmer guide' below for notes on how to setup Dear ImGui in your codebase.\n// Get latest version at https://github.com/ocornut/imgui\n// Releases change-log at https://github.com/ocornut/imgui/releases\n// Technical Support for Getting Started https://discourse.dearimgui.org/c/getting-started\n// Gallery (please post your screenshots/video there!): https://github.com/ocornut/imgui/issues/1269\n\n// Developed by Omar Cornut and every direct or indirect contributors to the GitHub.\n// See LICENSE.txt for copyright and licensing details (standard MIT License).\n// This library is free but I need your support to sustain development and maintenance.\n// Businesses: you can support continued maintenance and development via support contracts or sponsoring, see docs/README.\n// Individuals: you can support continued maintenance and development via donations or Patreon https://www.patreon.com/imgui.\n\n// It is recommended that you don't modify imgui.cpp! It will become difficult for you to update the library.\n// Note that 'ImGui::' being a namespace, you can add functions into the namespace from your own source files, without\n// modifying imgui.h or imgui.cpp. You may include imgui_internal.h to access internal data structures, but it doesn't\n// come with any guarantee of forward compatibility. Discussing your changes on the GitHub Issue Tracker may lead you\n// to a better solution or official support for them.\n\n/*\n\nIndex of this file:\n\nDOCUMENTATION\n\n- MISSION STATEMENT\n- END-USER GUIDE\n- PROGRAMMER GUIDE (read me!)\n  - Read first.\n  - How to update to a newer version of Dear ImGui.\n  - Getting started with integrating Dear ImGui in your code/engine.\n  - This is how a simple application may look like (2 variations).\n  - This is how a simple rendering function may look like.\n  - Using gamepad/keyboard navigation controls.\n- API BREAKING CHANGES (read me when you update!)\n- FREQUENTLY ASKED QUESTIONS (FAQ), TIPS\n  - Where is the documentation?\n  - Which version should I get?\n  - Who uses Dear ImGui?\n  - Why the odd dual naming, \"Dear ImGui\" vs \"ImGui\"?\n  - How can I tell whether to dispatch mouse/keyboard to imgui or to my application?\n  - How can I display an image? What is ImTextureID, how does it works?\n  - Why are multiple widgets reacting when I interact with a single one? How can I have\n    multiple widgets with the same label or with an empty label? A primer on labels and the ID Stack...\n  - How can I use my own math types instead of ImVec2/ImVec4?\n  - How can I load a different font than the default?\n  - How can I easily use icons in my application?\n  - How can I load multiple fonts?\n  - How can I display and input non-latin characters such as Chinese, Japanese, Korean, Cyrillic?\n  - How can I interact with standard C++ types (such as std::string and std::vector)?\n  - How can I use the drawing facilities without an ImGui window? (using ImDrawList API)\n  - How can I use Dear ImGui on a platform that doesn't have a mouse or a keyboard? (input share, remoting, gamepad)\n  - I integrated Dear ImGui in my engine and the text or lines are blurry..\n  - I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around..\n  - How can I help?\n\nCODE\n(search for \"[SECTION]\" in the code to find them)\n\n// [SECTION] FORWARD DECLARATIONS\n// [SECTION] CONTEXT AND MEMORY ALLOCATORS\n// [SECTION] MAIN USER FACING STRUCTURES (ImGuiStyle, ImGuiIO)\n// [SECTION] MISC HELPERS/UTILITIES (Maths, String, Format, Hash, File functions)\n// [SECTION] MISC HELPERS/UTILITIES (ImText* functions)\n// [SECTION] MISC HELPERS/UTILITIES (Color functions)\n// [SECTION] ImGuiStorage\n// [SECTION] ImGuiTextFilter\n// [SECTION] ImGuiTextBuffer\n// [SECTION] ImGuiListClipper\n// [SECTION] RENDER HELPERS\n// [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!)\n// [SECTION] TOOLTIPS\n// [SECTION] POPUPS\n// [SECTION] KEYBOARD/GAMEPAD NAVIGATION\n// [SECTION] COLUMNS\n// [SECTION] DRAG AND DROP\n// [SECTION] LOGGING/CAPTURING\n// [SECTION] SETTINGS\n// [SECTION] VIEWPORTS, PLATFORM WINDOWS\n// [SECTION] DOCKING\n// [SECTION] PLATFORM DEPENDENT HELPERS\n// [SECTION] METRICS/DEBUG WINDOW\n\n*/\n\n//-----------------------------------------------------------------------------\n// DOCUMENTATION\n//-----------------------------------------------------------------------------\n\n/*\n\n MISSION STATEMENT\n =================\n\n - Easy to use to create code-driven and data-driven tools.\n - Easy to use to create ad hoc short-lived tools and long-lived, more elaborate tools.\n - Easy to hack and improve.\n - Minimize screen real-estate usage.\n - Minimize setup and maintenance.\n - Minimize state storage on user side.\n - Portable, minimize dependencies, run on target (consoles, phones, etc.).\n - Efficient runtime and memory consumption (NB- we do allocate when \"growing\" content e.g. creating a window,.\n   opening a tree node for the first time, etc. but a typical frame should not allocate anything).\n\n Designed for developers and content-creators, not the typical end-user! Some of the weaknesses includes:\n - Doesn't look fancy, doesn't animate.\n - Limited layout features, intricate layouts are typically crafted in code.\n\n\n END-USER GUIDE\n ==============\n\n - Double-click on title bar to collapse window.\n - Click upper right corner to close a window, available when 'bool* p_open' is passed to ImGui::Begin().\n - Click and drag on lower right corner to resize window (double-click to auto fit window to its contents).\n - Click and drag on any empty space to move window.\n - TAB/SHIFT+TAB to cycle through keyboard editable fields.\n - CTRL+Click on a slider or drag box to input value as text.\n - Use mouse wheel to scroll.\n - Text editor:\n   - Hold SHIFT or use mouse to select text.\n   - CTRL+Left/Right to word jump.\n   - CTRL+Shift+Left/Right to select words.\n   - CTRL+A our Double-Click to select all.\n   - CTRL+X,CTRL+C,CTRL+V to use OS clipboard/\n   - CTRL+Z,CTRL+Y to undo/redo.\n   - ESCAPE to revert text to its original value.\n   - You can apply arithmetic operators +,*,/ on numerical values. Use +- to subtract (because - would set a negative value!)\n   - Controls are automatically adjusted for OSX to match standard OSX text editing operations.\n - General Keyboard controls: enable with ImGuiConfigFlags_NavEnableKeyboard.\n - General Gamepad controls: enable with ImGuiConfigFlags_NavEnableGamepad. See suggested mappings in imgui.h ImGuiNavInput_ + download PNG/PSD at http://goo.gl/9LgVZW\n\n\n PROGRAMMER GUIDE\n ================\n\n READ FIRST:\n\n - Read the FAQ below this section!\n - Your code creates the UI, if your code doesn't run the UI is gone! The UI can be highly dynamic, there are no construction\n   or destruction steps, less superfluous data retention on your side, less state duplication, less state synchronization, less bugs.\n - Call and read ImGui::ShowDemoWindow() for demo code demonstrating most features.\n - The library is designed to be built from sources. Avoid pre-compiled binaries and packaged versions. See imconfig.h to configure your build.\n - Dear ImGui is an implementation of the IMGUI paradigm (immediate-mode graphical user interface, a term coined by Casey Muratori).\n   You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links docs/README.md.\n - Dear ImGui is a \"single pass\" rasterizing implementation of the IMGUI paradigm, aimed at ease of use and high-performances.\n   For every application frame your UI code will be called only once. This is in contrast to e.g. Unity's own implementation of an IMGUI,\n   where the UI code is called multiple times (\"multiple passes\") from a single entry point. There are pros and cons to both approaches.\n - Our origin are on the top-left. In axis aligned bounding boxes, Min = top-left, Max = bottom-right.\n - This codebase is also optimized to yield decent performances with typical \"Debug\" builds settings.\n - Please make sure you have asserts enabled (IM_ASSERT redirects to assert() by default, but can be redirected).\n   If you get an assert, read the messages and comments around the assert.\n - C++: this is a very C-ish codebase: we don't rely on C++11, we don't include any C++ headers, and ImGui:: is a namespace.\n - C++: ImVec2/ImVec4 do not expose math operators by default, because it is expected that you use your own math types.\n   See FAQ \"How can I use my own math types instead of ImVec2/ImVec4?\" for details about setting up imconfig.h for that.\n   However, imgui_internal.h can optionally export math operators for ImVec2/ImVec4, which we use in this codebase.\n - C++: pay attention that ImVector<> manipulates plain-old-data and does not honor construction/destruction (avoid using it in your code!).\n\n HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI:\n\n - Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h)\n - Or maintain your own branch where you have imconfig.h modified.\n - Read the \"API BREAKING CHANGES\" section (below). This is where we list occasional API breaking changes.\n   If a function/type has been renamed / or marked obsolete, try to fix the name in your code before it is permanently removed\n   from the public API. If you have a problem with a missing function/symbols, search for its name in the code, there will\n   likely be a comment about it. Please report any issue to the GitHub page!\n - Try to keep your copy of dear imgui reasonably up to date.\n\n GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE:\n\n - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library.\n - Add the Dear ImGui source files to your projects or using your preferred build system.\n   It is recommended you build and statically link the .cpp files as part of your project and not as shared library (DLL).\n - You can later customize the imconfig.h file to tweak some compile-time behavior, such as integrating imgui types with your own maths types.\n - When using Dear ImGui, your programming IDE is your friend: follow the declaration of variables, functions and types to find comments about them.\n - Dear ImGui never touches or knows about your GPU state. The only function that knows about GPU is the draw function that you provide.\n   Effectively it means you can create widgets at any time in your code, regardless of considerations of being in \"update\" vs \"render\"\n   phases of your own application. All rendering informatioe are stored into command-lists that you will retrieve after calling ImGui::Render().\n - Refer to the bindings and demo applications in the examples/ folder for instruction on how to setup your code.\n - If you are running over a standard OS with a common graphics API, you should be able to use unmodified imgui_impl_*** files from the examples/ folder.\n\n HOW A SIMPLE APPLICATION MAY LOOK LIKE:\n EXHIBIT 1: USING THE EXAMPLE BINDINGS (imgui_impl_XXX.cpp files from the examples/ folder).\n\n     // Application init: create a dear imgui context, setup some options, load fonts\n     ImGui::CreateContext();\n     ImGuiIO& io = ImGui::GetIO();\n     // TODO: Set optional io.ConfigFlags values, e.g. 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard' to enable keyboard controls.\n     // TODO: Fill optional fields of the io structure later.\n     // TODO: Load TTF/OTF fonts if you don't want to use the default font.\n\n     // Initialize helper Platform and Renderer bindings (here we are using imgui_impl_win32 and imgui_impl_dx11)\n     ImGui_ImplWin32_Init(hwnd);\n     ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);\n\n     // Application main loop\n     while (true)\n     {\n         // Feed inputs to dear imgui, start new frame\n         ImGui_ImplDX11_NewFrame();\n         ImGui_ImplWin32_NewFrame();\n         ImGui::NewFrame();\n\n         // Any application code here\n         ImGui::Text(\"Hello, world!\");\n\n         // Render dear imgui into screen\n         ImGui::Render();\n         ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());\n         g_pSwapChain->Present(1, 0);\n     }\n\n     // Shutdown\n     ImGui_ImplDX11_Shutdown();\n     ImGui_ImplWin32_Shutdown();\n     ImGui::DestroyContext();\n\n HOW A SIMPLE APPLICATION MAY LOOK LIKE:\n EXHIBIT 2: IMPLEMENTING CUSTOM BINDING / CUSTOM ENGINE.\n\n     // Application init: create a dear imgui context, setup some options, load fonts\n     ImGui::CreateContext();\n     ImGuiIO& io = ImGui::GetIO();\n     // TODO: Set optional io.ConfigFlags values, e.g. 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard' to enable keyboard controls.\n     // TODO: Fill optional fields of the io structure later.\n     // TODO: Load TTF/OTF fonts if you don't want to use the default font.\n\n     // Build and load the texture atlas into a texture\n     // (In the examples/ app this is usually done within the ImGui_ImplXXX_Init() function from one of the demo Renderer)\n     int width, height;\n     unsigned char* pixels = NULL;\n     io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);\n\n     // At this point you've got the texture data and you need to upload that your your graphic system:\n     // After we have created the texture, store its pointer/identifier (_in whichever format your engine uses_) in 'io.Fonts->TexID'.\n     // This will be passed back to your via the renderer. Basically ImTextureID == void*. Read FAQ below for details about ImTextureID.\n     MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA32)\n     io.Fonts->TexID = (void*)texture;\n\n     // Application main loop\n     while (true)\n     {\n        // Setup low-level inputs, e.g. on Win32: calling GetKeyboardState(), or write to those fields from your Windows message handlers, etc.\n        // (In the examples/ app this is usually done within the ImGui_ImplXXX_NewFrame() function from one of the demo Platform bindings)\n        io.DeltaTime = 1.0f/60.0f;              // set the time elapsed since the previous frame (in seconds)\n        io.DisplaySize.x = 1920.0f;             // set the current display width\n        io.DisplaySize.y = 1280.0f;             // set the current display height here\n        io.MousePos = my_mouse_pos;             // set the mouse position\n        io.MouseDown[0] = my_mouse_buttons[0];  // set the mouse button states\n        io.MouseDown[1] = my_mouse_buttons[1];\n\n        // Call NewFrame(), after this point you can use ImGui::* functions anytime\n        // (So you want to try calling NewFrame() as early as you can in your mainloop to be able to use imgui everywhere)\n        ImGui::NewFrame();\n\n        // Most of your application code here\n        ImGui::Text(\"Hello, world!\");\n        MyGameUpdate(); // may use any ImGui functions, e.g. ImGui::Begin(\"My window\"); ImGui::Text(\"Hello, world!\"); ImGui::End();\n        MyGameRender(); // may use any ImGui functions as well!\n\n        // Render imgui, swap buffers\n        // (You want to try calling EndFrame/Render as late as you can, to be able to use imgui in your own game rendering code)\n        ImGui::EndFrame();\n        ImGui::Render();\n        ImDrawData* draw_data = ImGui::GetDrawData();\n        MyImGuiRenderFunction(draw_data);\n        SwapBuffers();\n     }\n\n     // Shutdown\n     ImGui::DestroyContext();\n\n HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE:\n\n    void void MyImGuiRenderFunction(ImDrawData* draw_data)\n    {\n       // TODO: Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled\n       // TODO: Setup viewport covering draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize\n       // TODO: Setup orthographic projection matrix cover draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize\n       // TODO: Setup shader: vertex { float2 pos, float2 uv, u32 color }, fragment shader sample color from 1 texture, multiply by vertex color.\n       for (int n = 0; n < draw_data->CmdListsCount; n++)\n       {\n          const ImDrawList* cmd_list = draw_data->CmdLists[n];\n          const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data;  // vertex buffer generated by ImGui\n          const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data;   // index buffer generated by ImGui\n          for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)\n          {\n             const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];\n             if (pcmd->UserCallback)\n             {\n                 pcmd->UserCallback(cmd_list, pcmd);\n             }\n             else\n             {\n                 // The texture for the draw call is specified by pcmd->TextureId.\n                 // The vast majority of draw calls will use the imgui texture atlas, which value you have set yourself during initialization.\n                 MyEngineBindTexture((MyTexture*)pcmd->TextureId);\n\n                 // We are using scissoring to clip some objects. All low-level graphics API should supports it.\n                 // - If your engine doesn't support scissoring yet, you may ignore this at first. You will get some small glitches\n                 //   (some elements visible outside their bounds) but you can fix that once everything else works!\n                 // - Clipping coordinates are provided in imgui coordinates space (from draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize)\n                 //   In a single viewport application, draw_data->DisplayPos will always be (0,0) and draw_data->DisplaySize will always be == io.DisplaySize.\n                 //   However, in the interest of supporting multi-viewport applications in the future (see 'viewport' branch on github),\n                 //   always subtract draw_data->DisplayPos from clipping bounds to convert them to your viewport space.\n                 // - Note that pcmd->ClipRect contains Min+Max bounds. Some graphics API may use Min+Max, other may use Min+Size (size being Max-Min)\n                 ImVec2 pos = draw_data->DisplayPos;\n                 MyEngineScissor((int)(pcmd->ClipRect.x - pos.x), (int)(pcmd->ClipRect.y - pos.y), (int)(pcmd->ClipRect.z - pos.x), (int)(pcmd->ClipRect.w - pos.y));\n\n                 // Render 'pcmd->ElemCount/3' indexed triangles.\n                 // By default the indices ImDrawIdx are 16-bits, you can change them to 32-bits in imconfig.h if your engine doesn't support 16-bits indices.\n                 MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer, vtx_buffer);\n             }\n             idx_buffer += pcmd->ElemCount;\n          }\n       }\n    }\n\n - The examples/ folders contains many actual implementation of the pseudo-codes above.\n - When calling NewFrame(), the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags are updated.\n   They tell you if Dear ImGui intends to use your inputs. When a flag is set you want to hide the corresponding inputs\n   from the rest of your application. In every cases you need to pass on the inputs to imgui. Refer to the FAQ for more information.\n - Please read the FAQ below!. Amusingly, it is called a FAQ because people frequently run into the same issues!\n\n USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS\n\n - The gamepad/keyboard navigation is fairly functional and keeps being improved.\n - Gamepad support is particularly useful to use dear imgui on a console system (e.g. PS4, Switch, XB1) without a mouse!\n - You can ask questions and report issues at https://github.com/ocornut/imgui/issues/787\n - The initial focus was to support game controllers, but keyboard is becoming increasingly and decently usable.\n - Gamepad:\n    - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable.\n    - Backend: Set io.BackendFlags |= ImGuiBackendFlags_HasGamepad + fill the io.NavInputs[] fields before calling NewFrame().\n      Note that io.NavInputs[] is cleared by EndFrame().\n    - See 'enum ImGuiNavInput_' in imgui.h for a description of inputs. For each entry of io.NavInputs[], set the following values:\n         0.0f= not held. 1.0f= fully held. Pass intermediate 0.0f..1.0f values for analog triggers/sticks.\n    - We uses a simple >0.0f test for activation testing, and won't attempt to test for a dead-zone.\n      Your code will probably need to transform your raw inputs (such as e.g. remapping your 0.2..0.9 raw input range to 0.0..1.0 imgui range, etc.).\n    - You can download PNG/PSD files depicting the gamepad controls for common controllers at: http://goo.gl/9LgVZW.\n    - If you need to share inputs between your game and the imgui parts, the easiest approach is to go all-or-nothing, with a buttons combo\n      to toggle the target. Please reach out if you think the game vs navigation input sharing could be improved.\n - Keyboard:\n    - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable.\n      NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays.\n    - When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), the io.WantCaptureKeyboard flag\n      will be set. For more advanced uses, you may want to read from:\n       - io.NavActive: true when a window is focused and it doesn't have the ImGuiWindowFlags_NoNavInputs flag set.\n       - io.NavVisible: true when the navigation cursor is visible (and usually goes false when mouse is used).\n       - or query focus information with e.g. IsWindowFocused(ImGuiFocusedFlags_AnyWindow), IsItemFocused() etc. functions.\n      Please reach out if you think the game vs navigation input sharing could be improved.\n - Mouse:\n    - PS4 users: Consider emulating a mouse cursor with DualShock4 touch pad or a spare analog stick as a mouse-emulation fallback.\n    - Consoles/Tablet/Phone users: Consider using a Synergy 1.x server (on your PC) + uSynergy.c (on your console/tablet/phone app) to share your PC mouse/keyboard.\n    - On a TV/console system where readability may be lower or mouse inputs may be awkward, you may want to set the ImGuiConfigFlags_NavEnableSetMousePos flag.\n      Enabling ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs dear imgui to move your mouse cursor along with navigation movements.\n      When enabled, the NewFrame() function may alter 'io.MousePos' and set 'io.WantSetMousePos' to notify you that it wants the mouse cursor to be moved.\n      When that happens your back-end NEEDS to move the OS or underlying mouse cursor on the next frame. Some of the binding in examples/ do that.\n      (If you set the NavEnableSetMousePos flag but don't honor 'io.WantSetMousePos' properly, imgui will misbehave as it will see your mouse as moving back and forth!)\n      (In a setup when you may not have easy control over the mouse cursor, e.g. uSynergy.c doesn't expose moving remote mouse cursor, you may want\n       to set a boolean to ignore your other external mouse positions until the external source is moved again.)\n\n\n API BREAKING CHANGES\n ====================\n\n Occasionally introducing changes that are breaking the API. We try to make the breakage minor and easy to fix.\n Below is a change-log of API breaking changes only. If you are using one of the functions listed, expect to have to fix some code.\n When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files.\n You can read releases logs https://github.com/ocornut/imgui/releases for more details.\n\n (Viewport Branch)\n - 2018/XX/XX (1.XX) - when multi-viewports are enabled, all positions will be in your natural OS coordinates space. It means that:\n                        - reference to hard-coded positions such as in SetNextWindowPos(ImVec2(0,0)) are probably not what you want anymore. \n                          you may use GetMainViewport()->Pos to offset hard-coded positions, e.g. SetNextWindowPos(GetMainViewport()->Pos)\n                        - likewise io.MousePos and GetMousePos() will use OS coordinates. \n                          If you query mouse positions to interact with non-imgui coordinates you will need to offset them, e.g. subtract GetWindowViewport()->Pos.\n - 2018/XX/XX (1.XX) - Moved IME support functions from io.ImeSetInputScreenPosFn, io.ImeWindowHandle to the PlatformIO api.\n \n\n - 2019/04/29 (1.70) - fixed ImDrawList rectangles with thick lines (>1.0f) not being as thick as requested. If you have custom rendering using rectangles with thick lines, they will appear thicker now.\n - 2019/03/04 (1.69) - renamed GetOverlayDrawList() to GetForegroundDrawList(). Kept redirection function (will obsolete).\n - 2019/02/26 (1.69) - renamed ImGuiColorEditFlags_RGB/ImGuiColorEditFlags_HSV/ImGuiColorEditFlags_HEX to ImGuiColorEditFlags_DisplayRGB/ImGuiColorEditFlags_DisplayHSV/ImGuiColorEditFlags_DisplayHex. Kept redirection enums (will obsolete).\n - 2019/02/14 (1.68) - made it illegal/assert when io.DisplayTime == 0.0f (with an exception for the first frame). If for some reason your time step calculation gives you a zero value, replace it with a dummy small value!\n - 2019/02/01 (1.68) - removed io.DisplayVisibleMin/DisplayVisibleMax (which were marked obsolete and removed from viewport/docking branch already).\n - 2019/01/06 (1.67) - renamed io.InputCharacters[], marked internal as was always intended. Please don't access directly, and use AddInputCharacter() instead!\n - 2019/01/06 (1.67) - renamed ImFontAtlas::GlyphRangesBuilder to ImFontGlyphRangesBuilder. Keep redirection typedef (will obsolete).\n - 2018/12/20 (1.67) - made it illegal to call Begin(\"\") with an empty string. This somehow half-worked before but had various undesirable side-effects.\n - 2018/12/10 (1.67) - renamed io.ConfigResizeWindowsFromEdges to io.ConfigWindowsResizeFromEdges as we are doing a large pass on configuration flags.\n - 2018/10/12 (1.66) - renamed misc/stl/imgui_stl.* to misc/cpp/imgui_stdlib.* in prevision for other C++ helper files.\n - 2018/09/28 (1.66) - renamed SetScrollHere() to SetScrollHereY(). Kept redirection function (will obsolete).\n - 2018/09/06 (1.65) - renamed stb_truetype.h to imstb_truetype.h, stb_textedit.h to imstb_textedit.h, and stb_rect_pack.h to imstb_rectpack.h.\n                       If you were conveniently using the imgui copy of those STB headers in your project you will have to update your include paths.\n - 2018/09/05 (1.65) - renamed io.OptCursorBlink/io.ConfigCursorBlink to io.ConfigInputTextCursorBlink. (#1427)\n - 2018/08/31 (1.64) - added imgui_widgets.cpp file, extracted and moved widgets code out of imgui.cpp into imgui_widgets.cpp. Re-ordered some of the code remaining in imgui.cpp.\n                       NONE OF THE FUNCTIONS HAVE CHANGED. THE CODE IS SEMANTICALLY 100% IDENTICAL, BUT _EVERY_ FUNCTION HAS BEEN MOVED.\n                       Because of this, any local modifications to imgui.cpp will likely conflict when you update. Read docs/CHANGELOG.txt for suggestions.\n - 2018/08/22 (1.63) - renamed IsItemDeactivatedAfterChange() to IsItemDeactivatedAfterEdit() for consistency with new IsItemEdited() API. Kept redirection function (will obsolete soonish as IsItemDeactivatedAfterChange() is very recent).\n - 2018/08/21 (1.63) - renamed ImGuiTextEditCallback to ImGuiInputTextCallback, ImGuiTextEditCallbackData to ImGuiInputTextCallbackData for consistency. Kept redirection types (will obsolete).\n - 2018/08/21 (1.63) - removed ImGuiInputTextCallbackData::ReadOnly since it is a duplication of (ImGuiInputTextCallbackData::Flags & ImGuiInputTextFlags_ReadOnly).\n - 2018/08/01 (1.63) - removed per-window ImGuiWindowFlags_ResizeFromAnySide beta flag in favor of a global io.ConfigResizeWindowsFromEdges [update 1.67 renamed to ConfigWindowsResizeFromEdges] to enable the feature.\n - 2018/08/01 (1.63) - renamed io.OptCursorBlink to io.ConfigCursorBlink [-> io.ConfigInputTextCursorBlink in 1.65], io.OptMacOSXBehaviors to ConfigMacOSXBehaviors for consistency.\n - 2018/07/22 (1.63) - changed ImGui::GetTime() return value from float to double to avoid accumulating floating point imprecisions over time.\n - 2018/07/08 (1.63) - style: renamed ImGuiCol_ModalWindowDarkening to ImGuiCol_ModalWindowDimBg for consistency with other features. Kept redirection enum (will obsolete).\n - 2018/06/08 (1.62) - examples: the imgui_impl_xxx files have been split to separate platform (Win32, Glfw, SDL2, etc.) from renderer (DX11, OpenGL, Vulkan,  etc.).\n                       old bindings will still work as is, however prefer using the separated bindings as they will be updated to support multi-viewports.\n                       when adopting new bindings follow the main.cpp code of your preferred examples/ folder to know which functions to call.\n                       in particular, note that old bindings called ImGui::NewFrame() at the end of their ImGui_ImplXXXX_NewFrame() function.\n - 2018/06/06 (1.62) - renamed GetGlyphRangesChinese() to GetGlyphRangesChineseFull() to distinguish other variants and discourage using the full set.\n - 2018/06/06 (1.62) - TreeNodeEx()/TreeNodeBehavior(): the ImGuiTreeNodeFlags_CollapsingHeader helper now include the ImGuiTreeNodeFlags_NoTreePushOnOpen flag. See Changelog for details.\n - 2018/05/03 (1.61) - DragInt(): the default compile-time format string has been changed from \"%.0f\" to \"%d\", as we are not using integers internally any more.\n                       If you used DragInt() with custom format strings, make sure you change them to use %d or an integer-compatible format.\n                       To honor backward-compatibility, the DragInt() code will currently parse and modify format strings to replace %*f with %d, giving time to users to upgrade their code.\n                       If you have IMGUI_DISABLE_OBSOLETE_FUNCTIONS enabled, the code will instead assert! You may run a reg-exp search on your codebase for e.g. \"DragInt.*%f\" to help you find them.\n - 2018/04/28 (1.61) - obsoleted InputFloat() functions taking an optional \"int decimal_precision\" in favor of an equivalent and more flexible \"const char* format\",\n                       consistent with other functions. Kept redirection functions (will obsolete).\n - 2018/04/09 (1.61) - IM_DELETE() helper function added in 1.60 doesn't clear the input _pointer_ reference, more consistent with expectation and allows passing r-value.\n - 2018/03/20 (1.60) - renamed io.WantMoveMouse to io.WantSetMousePos for consistency and ease of understanding (was added in 1.52, _not_ used by core and only honored by some binding ahead of merging the Nav branch).\n - 2018/03/12 (1.60) - removed ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered as the closing cross uses regular button colors now.\n - 2018/03/08 (1.60) - changed ImFont::DisplayOffset.y to default to 0 instead of +1. Fixed rounding of Ascent/Descent to match TrueType renderer. If you were adding or subtracting to ImFont::DisplayOffset check if your fonts are correctly aligned vertically.\n - 2018/03/03 (1.60) - renamed ImGuiStyleVar_Count_ to ImGuiStyleVar_COUNT and ImGuiMouseCursor_Count_ to ImGuiMouseCursor_COUNT for consistency with other public enums.\n - 2018/02/18 (1.60) - BeginDragDropSource(): temporarily removed the optional mouse_button=0 parameter because it is not really usable in many situations at the moment.\n - 2018/02/16 (1.60) - obsoleted the io.RenderDrawListsFn callback, you can call your graphics engine render function after ImGui::Render(). Use ImGui::GetDrawData() to retrieve the ImDrawData* to display.\n - 2018/02/07 (1.60) - reorganized context handling to be more explicit,\n                       - YOU NOW NEED TO CALL ImGui::CreateContext() AT THE BEGINNING OF YOUR APP, AND CALL ImGui::DestroyContext() AT THE END.\n                       - removed Shutdown() function, as DestroyContext() serve this purpose.\n                       - you may pass a ImFontAtlas* pointer to CreateContext() to share a font atlas between contexts. Otherwise CreateContext() will create its own font atlas instance.\n                       - removed allocator parameters from CreateContext(), they are now setup with SetAllocatorFunctions(), and shared by all contexts.\n                       - removed the default global context and font atlas instance, which were confusing for users of DLL reloading and users of multiple contexts.\n - 2018/01/31 (1.60) - moved sample TTF files from extra_fonts/ to misc/fonts/. If you loaded files directly from the imgui repo you may need to update your paths.\n - 2018/01/11 (1.60) - obsoleted IsAnyWindowHovered() in favor of IsWindowHovered(ImGuiHoveredFlags_AnyWindow). Kept redirection function (will obsolete).\n - 2018/01/11 (1.60) - obsoleted IsAnyWindowFocused() in favor of IsWindowFocused(ImGuiFocusedFlags_AnyWindow). Kept redirection function (will obsolete).\n - 2018/01/03 (1.60) - renamed ImGuiSizeConstraintCallback to ImGuiSizeCallback, ImGuiSizeConstraintCallbackData to ImGuiSizeCallbackData.\n - 2017/12/29 (1.60) - removed CalcItemRectClosestPoint() which was weird and not really used by anyone except demo code. If you need it it's easy to replicate on your side.\n - 2017/12/24 (1.53) - renamed the emblematic ShowTestWindow() function to ShowDemoWindow(). Kept redirection function (will obsolete).\n - 2017/12/21 (1.53) - ImDrawList: renamed style.AntiAliasedShapes to style.AntiAliasedFill for consistency and as a way to explicitly break code that manipulate those flag at runtime. You can now manipulate ImDrawList::Flags\n - 2017/12/21 (1.53) - ImDrawList: removed 'bool anti_aliased = true' final parameter of ImDrawList::AddPolyline() and ImDrawList::AddConvexPolyFilled(). Prefer manipulating ImDrawList::Flags if you need to toggle them during the frame.\n - 2017/12/14 (1.53) - using the ImGuiWindowFlags_NoScrollWithMouse flag on a child window forwards the mouse wheel event to the parent window, unless either ImGuiWindowFlags_NoInputs or ImGuiWindowFlags_NoScrollbar are also set.\n - 2017/12/13 (1.53) - renamed GetItemsLineHeightWithSpacing() to GetFrameHeightWithSpacing(). Kept redirection function (will obsolete).\n - 2017/12/13 (1.53) - obsoleted IsRootWindowFocused() in favor of using IsWindowFocused(ImGuiFocusedFlags_RootWindow). Kept redirection function (will obsolete).\n                     - obsoleted IsRootWindowOrAnyChildFocused() in favor of using IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows). Kept redirection function (will obsolete).\n - 2017/12/12 (1.53) - renamed ImGuiTreeNodeFlags_AllowOverlapMode to ImGuiTreeNodeFlags_AllowItemOverlap. Kept redirection enum (will obsolete).\n - 2017/12/10 (1.53) - removed SetNextWindowContentWidth(), prefer using SetNextWindowContentSize(). Kept redirection function (will obsolete).\n - 2017/11/27 (1.53) - renamed ImGuiTextBuffer::append() helper to appendf(), appendv() to appendfv(). If you copied the 'Log' demo in your code, it uses appendv() so that needs to be renamed.\n - 2017/11/18 (1.53) - Style, Begin: removed ImGuiWindowFlags_ShowBorders window flag. Borders are now fully set up in the ImGuiStyle structure (see e.g. style.FrameBorderSize, style.WindowBorderSize). Use ImGui::ShowStyleEditor() to look them up.\n                       Please note that the style system will keep evolving (hopefully stabilizing in Q1 2018), and so custom styles will probably subtly break over time. It is recommended you use the StyleColorsClassic(), StyleColorsDark(), StyleColorsLight() functions.\n - 2017/11/18 (1.53) - Style: removed ImGuiCol_ComboBg in favor of combo boxes using ImGuiCol_PopupBg for consistency.\n - 2017/11/18 (1.53) - Style: renamed ImGuiCol_ChildWindowBg to ImGuiCol_ChildBg.\n - 2017/11/18 (1.53) - Style: renamed style.ChildWindowRounding to style.ChildRounding, ImGuiStyleVar_ChildWindowRounding to ImGuiStyleVar_ChildRounding.\n - 2017/11/02 (1.53) - obsoleted IsRootWindowOrAnyChildHovered() in favor of using IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows);\n - 2017/10/24 (1.52) - renamed IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS/IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCS to IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS/IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS for consistency.\n - 2017/10/20 (1.52) - changed IsWindowHovered() default parameters behavior to return false if an item is active in another window (e.g. click-dragging item from another window to this window). You can use the newly introduced IsWindowHovered() flags to requests this specific behavior if you need it.\n - 2017/10/20 (1.52) - marked IsItemHoveredRect()/IsMouseHoveringWindow() as obsolete, in favor of using the newly introduced flags for IsItemHovered() and IsWindowHovered(). See https://github.com/ocornut/imgui/issues/1382 for details.\n                       removed the IsItemRectHovered()/IsWindowRectHovered() names introduced in 1.51 since they were merely more consistent names for the two functions we are now obsoleting.\n - 2017/10/17 (1.52) - marked the old 5-parameters version of Begin() as obsolete (still available). Use SetNextWindowSize()+Begin() instead!\n - 2017/10/11 (1.52) - renamed AlignFirstTextHeightToWidgets() to AlignTextToFramePadding(). Kept inline redirection function (will obsolete).\n - 2017/09/26 (1.52) - renamed ImFont::Glyph to ImFontGlyph. Keep redirection typedef (will obsolete).\n - 2017/09/25 (1.52) - removed SetNextWindowPosCenter() because SetNextWindowPos() now has the optional pivot information to do the same and more. Kept redirection function (will obsolete).\n - 2017/08/25 (1.52) - io.MousePos needs to be set to ImVec2(-FLT_MAX,-FLT_MAX) when mouse is unavailable/missing. Previously ImVec2(-1,-1) was enough but we now accept negative mouse coordinates. In your binding if you need to support unavailable mouse, make sure to replace \"io.MousePos = ImVec2(-1,-1)\" with \"io.MousePos = ImVec2(-FLT_MAX,-FLT_MAX)\".\n - 2017/08/22 (1.51) - renamed IsItemHoveredRect() to IsItemRectHovered(). Kept inline redirection function (will obsolete). -> (1.52) use IsItemHovered(ImGuiHoveredFlags_RectOnly)!\n                     - renamed IsMouseHoveringAnyWindow() to IsAnyWindowHovered() for consistency. Kept inline redirection function (will obsolete).\n                     - renamed IsMouseHoveringWindow() to IsWindowRectHovered() for consistency. Kept inline redirection function (will obsolete).\n - 2017/08/20 (1.51) - renamed GetStyleColName() to GetStyleColorName() for consistency.\n - 2017/08/20 (1.51) - added PushStyleColor(ImGuiCol idx, ImU32 col) overload, which _might_ cause an \"ambiguous call\" compilation error if you are using ImColor() with implicit cast. Cast to ImU32 or ImVec4 explicily to fix.\n - 2017/08/15 (1.51) - marked the weird IMGUI_ONCE_UPON_A_FRAME helper macro as obsolete. prefer using the more explicit ImGuiOnceUponAFrame.\n - 2017/08/15 (1.51) - changed parameter order for BeginPopupContextWindow() from (const char*,int buttons,bool also_over_items) to (const char*,int buttons,bool also_over_items). Note that most calls relied on default parameters completely.\n - 2017/08/13 (1.51) - renamed ImGuiCol_Columns*** to ImGuiCol_Separator***. Kept redirection enums (will obsolete).\n - 2017/08/11 (1.51) - renamed ImGuiSetCond_*** types and flags to ImGuiCond_***. Kept redirection enums (will obsolete).\n - 2017/08/09 (1.51) - removed ValueColor() helpers, they are equivalent to calling Text(label) + SameLine() + ColorButton().\n - 2017/08/08 (1.51) - removed ColorEditMode() and ImGuiColorEditMode in favor of ImGuiColorEditFlags and parameters to the various Color*() functions. The SetColorEditOptions() allows to initialize default but the user can still change them with right-click context menu.\n                     - changed prototype of 'ColorEdit4(const char* label, float col[4], bool show_alpha = true)' to 'ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0)', where passing flags = 0x01 is a safe no-op (hello dodgy backward compatibility!). - check and run the demo window, under \"Color/Picker Widgets\", to understand the various new options.\n                     - changed prototype of rarely used 'ColorButton(ImVec4 col, bool small_height = false, bool outline_border = true)' to 'ColorButton(const char* desc_id, ImVec4 col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0,0))'\n - 2017/07/20 (1.51) - removed IsPosHoveringAnyWindow(ImVec2), which was partly broken and misleading. ASSERT + redirect user to io.WantCaptureMouse\n - 2017/05/26 (1.50) - removed ImFontConfig::MergeGlyphCenterV in favor of a more multipurpose ImFontConfig::GlyphOffset.\n - 2017/05/01 (1.50) - renamed ImDrawList::PathFill() (rarely used directly) to ImDrawList::PathFillConvex() for clarity.\n - 2016/11/06 (1.50) - BeginChild(const char*) now applies the stack id to the provided label, consistently with other functions as it should always have been. It shouldn't affect you unless (extremely unlikely) you were appending multiple times to a same child from different locations of the stack id. If that's the case, generate an id with GetId() and use it instead of passing string to BeginChild().\n - 2016/10/15 (1.50) - avoid 'void* user_data' parameter to io.SetClipboardTextFn/io.GetClipboardTextFn pointers. We pass io.ClipboardUserData to it.\n - 2016/09/25 (1.50) - style.WindowTitleAlign is now a ImVec2 (ImGuiAlign enum was removed). set to (0.5f,0.5f) for horizontal+vertical centering, (0.0f,0.0f) for upper-left, etc.\n - 2016/07/30 (1.50) - SameLine(x) with x>0.0f is now relative to left of column/group if any, and not always to left of window. This was sort of always the intent and hopefully breakage should be minimal.\n - 2016/05/12 (1.49) - title bar (using ImGuiCol_TitleBg/ImGuiCol_TitleBgActive colors) isn't rendered over a window background (ImGuiCol_WindowBg color) anymore.\n                       If your TitleBg/TitleBgActive alpha was 1.0f or you are using the default theme it will not affect you.\n                       If your TitleBg/TitleBgActive alpha was <1.0f you need to tweak your custom theme to readjust for the fact that we don't draw a WindowBg background behind the title bar.\n                       This helper function will convert an old TitleBg/TitleBgActive color into a new one with the same visual output, given the OLD color and the OLD WindowBg color.\n                           ImVec4 ConvertTitleBgCol(const ImVec4& win_bg_col, const ImVec4& title_bg_col)\n                           {\n                               float new_a = 1.0f - ((1.0f - win_bg_col.w) * (1.0f - title_bg_col.w)), k = title_bg_col.w / new_a;\n                               return ImVec4((win_bg_col.x * win_bg_col.w + title_bg_col.x) * k, (win_bg_col.y * win_bg_col.w + title_bg_col.y) * k, (win_bg_col.z * win_bg_col.w + title_bg_col.z) * k, new_a);\n                           }\n                       If this is confusing, pick the RGB value from title bar from an old screenshot and apply this as TitleBg/TitleBgActive. Or you may just create TitleBgActive from a tweaked TitleBg color.\n - 2016/05/07 (1.49) - removed confusing set of GetInternalState(), GetInternalStateSize(), SetInternalState() functions. Now using CreateContext(), DestroyContext(), GetCurrentContext(), SetCurrentContext().\n - 2016/05/02 (1.49) - renamed SetNextTreeNodeOpened() to SetNextTreeNodeOpen(), no redirection.\n - 2016/05/01 (1.49) - obsoleted old signature of CollapsingHeader(const char* label, const char* str_id = NULL, bool display_frame = true, bool default_open = false) as extra parameters were badly designed and rarely used. You can replace the \"default_open = true\" flag in new API with CollapsingHeader(label, ImGuiTreeNodeFlags_DefaultOpen).\n - 2016/04/26 (1.49) - changed ImDrawList::PushClipRect(ImVec4 rect) to ImDrawList::PushClipRect(Imvec2 min,ImVec2 max,bool intersect_with_current_clip_rect=false). Note that higher-level ImGui::PushClipRect() is preferable because it will clip at logic/widget level, whereas ImDrawList::PushClipRect() only affect your renderer.\n - 2016/04/03 (1.48) - removed style.WindowFillAlphaDefault setting which was redundant. Bake default BG alpha inside style.Colors[ImGuiCol_WindowBg] and all other Bg color values. (ref github issue #337).\n - 2016/04/03 (1.48) - renamed ImGuiCol_TooltipBg to ImGuiCol_PopupBg, used by popups/menus and tooltips. popups/menus were previously using ImGuiCol_WindowBg. (ref github issue #337)\n - 2016/03/21 (1.48) - renamed GetWindowFont() to GetFont(), GetWindowFontSize() to GetFontSize(). Kept inline redirection function (will obsolete).\n - 2016/03/02 (1.48) - InputText() completion/history/always callbacks: if you modify the text buffer manually (without using DeleteChars()/InsertChars() helper) you need to maintain the BufTextLen field. added an assert.\n - 2016/01/23 (1.48) - fixed not honoring exact width passed to PushItemWidth(), previously it would add extra FramePadding.x*2 over that width. if you had manual pixel-perfect alignment in place it might affect you.\n - 2015/12/27 (1.48) - fixed ImDrawList::AddRect() which used to render a rectangle 1 px too large on each axis.\n - 2015/12/04 (1.47) - renamed Color() helpers to ValueColor() - dangerously named, rarely used and probably to be made obsolete.\n - 2015/08/29 (1.45) - with the addition of horizontal scrollbar we made various fixes to inconsistencies with dealing with cursor position.\n                       GetCursorPos()/SetCursorPos() functions now include the scrolled amount. It shouldn't affect the majority of users, but take note that SetCursorPosX(100.0f) puts you at +100 from the starting x position which may include scrolling, not at +100 from the window left side.\n                       GetContentRegionMax()/GetWindowContentRegionMin()/GetWindowContentRegionMax() functions allow include the scrolled amount. Typically those were used in cases where no scrolling would happen so it may not be a problem, but watch out!\n - 2015/08/29 (1.45) - renamed style.ScrollbarWidth to style.ScrollbarSize\n - 2015/08/05 (1.44) - split imgui.cpp into extra files: imgui_demo.cpp imgui_draw.cpp imgui_internal.h that you need to add to your project.\n - 2015/07/18 (1.44) - fixed angles in ImDrawList::PathArcTo(), PathArcToFast() (introduced in 1.43) being off by an extra PI for no justifiable reason\n - 2015/07/14 (1.43) - add new ImFontAtlas::AddFont() API. For the old AddFont***, moved the 'font_no' parameter of ImFontAtlas::AddFont** functions to the ImFontConfig structure.\n                       you need to render your textured triangles with bilinear filtering to benefit from sub-pixel positioning of text.\n - 2015/07/08 (1.43) - switched rendering data to use indexed rendering. this is saving a fair amount of CPU/GPU and enables us to get anti-aliasing for a marginal cost.\n                       this necessary change will break your rendering function! the fix should be very easy. sorry for that :(\n                     - if you are using a vanilla copy of one of the imgui_impl_XXXX.cpp provided in the example, you just need to update your copy and you can ignore the rest.\n                     - the signature of the io.RenderDrawListsFn handler has changed!\n                       old: ImGui_XXXX_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)\n                       new: ImGui_XXXX_RenderDrawLists(ImDrawData* draw_data).\n                         parameters: 'cmd_lists' becomes 'draw_data->CmdLists', 'cmd_lists_count' becomes 'draw_data->CmdListsCount'\n                         ImDrawList: 'commands' becomes 'CmdBuffer', 'vtx_buffer' becomes 'VtxBuffer', 'IdxBuffer' is new.\n                         ImDrawCmd:  'vtx_count' becomes 'ElemCount', 'clip_rect' becomes 'ClipRect', 'user_callback' becomes 'UserCallback', 'texture_id' becomes 'TextureId'.\n                     - each ImDrawList now contains both a vertex buffer and an index buffer. For each command, render ElemCount/3 triangles using indices from the index buffer.\n                     - if you REALLY cannot render indexed primitives, you can call the draw_data->DeIndexAllBuffers() method to de-index the buffers. This is slow and a waste of CPU/GPU. Prefer using indexed rendering!\n                     - refer to code in the examples/ folder or ask on the GitHub if you are unsure of how to upgrade. please upgrade!\n - 2015/07/10 (1.43) - changed SameLine() parameters from int to float.\n - 2015/07/02 (1.42) - renamed SetScrollPosHere() to SetScrollFromCursorPos(). Kept inline redirection function (will obsolete).\n - 2015/07/02 (1.42) - renamed GetScrollPosY() to GetScrollY(). Necessary to reduce confusion along with other scrolling functions, because positions (e.g. cursor position) are not equivalent to scrolling amount.\n - 2015/06/14 (1.41) - changed ImageButton() default bg_col parameter from (0,0,0,1) (black) to (0,0,0,0) (transparent) - makes a difference when texture have transparence\n - 2015/06/14 (1.41) - changed Selectable() API from (label, selected, size) to (label, selected, flags, size). Size override should have been rarely be used. Sorry!\n - 2015/05/31 (1.40) - renamed GetWindowCollapsed() to IsWindowCollapsed() for consistency. Kept inline redirection function (will obsolete).\n - 2015/05/31 (1.40) - renamed IsRectClipped() to IsRectVisible() for consistency. Note that return value is opposite! Kept inline redirection function (will obsolete).\n - 2015/05/27 (1.40) - removed the third 'repeat_if_held' parameter from Button() - sorry! it was rarely used and inconsistent. Use PushButtonRepeat(true) / PopButtonRepeat() to enable repeat on desired buttons.\n - 2015/05/11 (1.40) - changed BeginPopup() API, takes a string identifier instead of a bool. ImGui needs to manage the open/closed state of popups. Call OpenPopup() to actually set the \"open\" state of a popup. BeginPopup() returns true if the popup is opened.\n - 2015/05/03 (1.40) - removed style.AutoFitPadding, using style.WindowPadding makes more sense (the default values were already the same).\n - 2015/04/13 (1.38) - renamed IsClipped() to IsRectClipped(). Kept inline redirection function until 1.50.\n - 2015/04/09 (1.38) - renamed ImDrawList::AddArc() to ImDrawList::AddArcFast() for compatibility with future API\n - 2015/04/03 (1.38) - removed ImGuiCol_CheckHovered, ImGuiCol_CheckActive, replaced with the more general ImGuiCol_FrameBgHovered, ImGuiCol_FrameBgActive.\n - 2014/04/03 (1.38) - removed support for passing -FLT_MAX..+FLT_MAX as the range for a SliderFloat(). Use DragFloat() or Inputfloat() instead.\n - 2015/03/17 (1.36) - renamed GetItemBoxMin()/GetItemBoxMax()/IsMouseHoveringBox() to GetItemRectMin()/GetItemRectMax()/IsMouseHoveringRect(). Kept inline redirection function until 1.50.\n - 2015/03/15 (1.36) - renamed style.TreeNodeSpacing to style.IndentSpacing, ImGuiStyleVar_TreeNodeSpacing to ImGuiStyleVar_IndentSpacing\n - 2015/03/13 (1.36) - renamed GetWindowIsFocused() to IsWindowFocused(). Kept inline redirection function until 1.50.\n - 2015/03/08 (1.35) - renamed style.ScrollBarWidth to style.ScrollbarWidth (casing)\n - 2015/02/27 (1.34) - renamed OpenNextNode(bool) to SetNextTreeNodeOpened(bool, ImGuiSetCond). Kept inline redirection function until 1.50.\n - 2015/02/27 (1.34) - renamed ImGuiSetCondition_*** to ImGuiSetCond_***, and _FirstUseThisSession becomes _Once.\n - 2015/02/11 (1.32) - changed text input callback ImGuiTextEditCallback return type from void-->int. reserved for future use, return 0 for now.\n - 2015/02/10 (1.32) - renamed GetItemWidth() to CalcItemWidth() to clarify its evolving behavior\n - 2015/02/08 (1.31) - renamed GetTextLineSpacing() to GetTextLineHeightWithSpacing()\n - 2015/02/01 (1.31) - removed IO.MemReallocFn (unused)\n - 2015/01/19 (1.30) - renamed ImGuiStorage::GetIntPtr()/GetFloatPtr() to GetIntRef()/GetIntRef() because Ptr was conflicting with actual pointer storage functions.\n - 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader.\n              (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels.\n                       font init:  { const void* png_data; unsigned int png_size; ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); <..Upload texture to GPU..>; }\n                       became:     { unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); <..Upload texture to GPU>; io.Fonts->TexId = YourTextureIdentifier; }\n                       you now have more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs.\n                       it is now recommended that you sample the font texture with bilinear interpolation.\n              (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.Fonts->TexID.\n              (1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix)\n              (1.30) - removed ImGui::IsItemFocused() in favor of ImGui::IsItemActive() which handles all widgets\n - 2014/12/10 (1.18) - removed SetNewWindowDefaultPos() in favor of new generic API SetNextWindowPos(pos, ImGuiSetCondition_FirstUseEver)\n - 2014/11/28 (1.17) - moved IO.Font*** options to inside the IO.Font-> structure (FontYOffset, FontTexUvForWhite, FontBaseScale, FontFallbackGlyph)\n - 2014/11/26 (1.17) - reworked syntax of IMGUI_ONCE_UPON_A_FRAME helper macro to increase compiler compatibility\n - 2014/11/07 (1.15) - renamed IsHovered() to IsItemHovered()\n - 2014/10/02 (1.14) - renamed IMGUI_INCLUDE_IMGUI_USER_CPP to IMGUI_INCLUDE_IMGUI_USER_INL and imgui_user.cpp to imgui_user.inl (more IDE friendly)\n - 2014/09/25 (1.13) - removed 'text_end' parameter from IO.SetClipboardTextFn (the string is now always zero-terminated for simplicity)\n - 2014/09/24 (1.12) - renamed SetFontScale() to SetWindowFontScale()\n - 2014/09/24 (1.12) - moved IM_MALLOC/IM_REALLOC/IM_FREE preprocessor defines to IO.MemAllocFn/IO.MemReallocFn/IO.MemFreeFn\n - 2014/08/30 (1.09) - removed IO.FontHeight (now computed automatically)\n - 2014/08/30 (1.09) - moved IMGUI_FONT_TEX_UV_FOR_WHITE preprocessor define to IO.FontTexUvForWhite\n - 2014/08/28 (1.09) - changed the behavior of IO.PixelCenterOffset following various rendering fixes\n\n\n FREQUENTLY ASKED QUESTIONS (FAQ), TIPS\n ======================================\n\n Q: Where is the documentation?\n A: This library is poorly documented at the moment and expects of the user to be acquainted with C/C++.\n    - Run the examples/ and explore them.\n    - See demo code in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. \n    - The demo covers most features of Dear ImGui, so you can read the code and see its output. \n    - See documentation and comments at the top of imgui.cpp + effectively imgui.h.\n    - Dozens of standalone example applications using e.g. OpenGL/DirectX are provided in the examples/ \n      folder to explain how to integrate Dear ImGui with your own engine/application.\n    - Your programming IDE is your friend, find the type or function declaration to find comments \n      associated to it.\n\n Q: Which version should I get?\n A: I occasionally tag Releases (https://github.com/ocornut/imgui/releases) but it is generally safe \n    and recommended to sync to master/latest. The library is fairly stable and regressions tend to be \n    fixed fast when reported. You may also peak at the 'docking' branch which includes:\n    - Docking/Merging features (https://github.com/ocornut/imgui/issues/2109)\n    - Multi-viewport features (https://github.com/ocornut/imgui/issues/1542)\n    Many projects are using this branch and it is kept in sync with master regularly.\n\n Q: Who uses Dear ImGui?\n A: See \"Quotes\" (https://github.com/ocornut/imgui/wiki/Quotes) and\n    \"Software using Dear ImGui\" (https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) Wiki pages\n    for a list of games/software which are publicly known to use dear imgui. Please add yours if you can!\n\n Q: Why the odd dual naming, \"Dear ImGui\" vs \"ImGui\"?\n A: The library started its life as \"ImGui\" due to the fact that I didn't give it a proper name when \n    when I released 1.0, and had no particular expectation that it would take off. However, the term IMGUI \n    (immediate-mode graphical user interface) was coined before and is being used in variety of other \n    situations (e.g. Unity uses it own implementation of the IMGUI paradigm). \n    To reduce the ambiguity without affecting existing code bases, I have decided on an alternate, \n    longer name \"Dear ImGui\" that people can use to refer to this specific library.\n    Please try to refer to this library as \"Dear ImGui\".\n\n Q: How can I tell whether to dispatch mouse/keyboard to imgui or to my application?\n A: You can read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags from the ImGuiIO structure (e.g. if (ImGui::GetIO().WantCaptureMouse) { ... } )\n    - When 'io.WantCaptureMouse' is set, imgui wants to use your mouse state, and you may want to discard/hide the inputs from the rest of your application.\n    - When 'io.WantCaptureKeyboard' is set, imgui wants to use your keyboard state, and you may want to discard/hide the inputs from the rest of your application.\n    - When 'io.WantTextInput' is set to may want to notify your OS to popup an on-screen keyboard, if available (e.g. on a mobile phone, or console OS).\n    Note: you should always pass your mouse/keyboard inputs to imgui, even when the io.WantCaptureXXX flag are set false.\n     This is because imgui needs to detect that you clicked in the void to unfocus its own windows.\n    Note: The 'io.WantCaptureMouse' is more accurate that any attempt to \"check if the mouse is hovering a window\" (don't do that!).\n     It handle mouse dragging correctly (both dragging that started over your application or over an imgui window) and handle e.g. modal windows blocking inputs.\n     Those flags are updated by ImGui::NewFrame(). Preferably read the flags after calling NewFrame() if you can afford it, but reading them before is also\n     perfectly fine, as the bool toggle fairly rarely. If you have on a touch device, you might find use for an early call to UpdateHoveredWindowAndCaptureFlags().\n    Note: Text input widget releases focus on \"Return KeyDown\", so the subsequent \"Return KeyUp\" event that your application receive will typically\n     have 'io.WantCaptureKeyboard=false'. Depending on your application logic it may or not be inconvenient. You might want to track which key-downs\n     were targeted for Dear ImGui, e.g. with an array of bool, and filter out the corresponding key-ups.)\n\n Q: How can I display an image? What is ImTextureID, how does it works?\n A: Short explanation:\n    - You may use functions such as ImGui::Image(), ImGui::ImageButton() or lower-level ImDrawList::AddImage() to emit draw calls that will use your own textures.\n    - Actual textures are identified in a way that is up to the user/engine. Those identifiers are stored and passed as ImTextureID (void*) value.\n    - Loading image files from the disk and turning them into a texture is not within the scope of Dear ImGui (for a good reason).\n      Please read documentations or tutorials on your graphics API to understand how to display textures on the screen before moving onward.\n\n    Long explanation:\n    - Dear ImGui's job is to create \"meshes\", defined in a renderer-agnostic format made of draw commands and vertices.\n      At the end of the frame those meshes (ImDrawList) will be displayed by your rendering function. They are made up of textured polygons and the code\n      to render them is generally fairly short (a few dozen lines). In the examples/ folder we provide functions for popular graphics API (OpenGL, DirectX, etc.).\n    - Each rendering function decides on a data type to represent \"textures\". The concept of what is a \"texture\" is entirely tied to your underlying engine/graphics API.\n      We carry the information to identify a \"texture\" in the ImTextureID type.\n      ImTextureID is nothing more that a void*, aka 4/8 bytes worth of data: just enough to store 1 pointer or 1 integer of your choice.\n      Dear ImGui doesn't know or understand what you are storing in ImTextureID, it merely pass ImTextureID values until they reach your rendering function.\n    - In the examples/ bindings, for each graphics API binding we decided on a type that is likely to be a good representation for specifying\n      an image from the end-user perspective. This is what the _examples_ rendering functions are using:\n\n         OpenGL:     ImTextureID = GLuint                       (see ImGui_ImplGlfwGL3_RenderDrawData() function in imgui_impl_glfw_gl3.cpp)\n         DirectX9:   ImTextureID = LPDIRECT3DTEXTURE9           (see ImGui_ImplDX9_RenderDrawData()     function in imgui_impl_dx9.cpp)\n         DirectX11:  ImTextureID = ID3D11ShaderResourceView*    (see ImGui_ImplDX11_RenderDrawData()    function in imgui_impl_dx11.cpp)\n         DirectX12:  ImTextureID = D3D12_GPU_DESCRIPTOR_HANDLE  (see ImGui_ImplDX12_RenderDrawData()    function in imgui_impl_dx12.cpp)\n\n      For example, in the OpenGL example binding we store raw OpenGL texture identifier (GLuint) inside ImTextureID.\n      Whereas in the DirectX11 example binding we store a pointer to ID3D11ShaderResourceView inside ImTextureID, which is a higher-level structure\n      tying together both the texture and information about its format and how to read it.\n    - If you have a custom engine built over e.g. OpenGL, instead of passing GLuint around you may decide to use a high-level data type to carry information about\n      the texture as well as how to display it (shaders, etc.). The decision of what to use as ImTextureID can always be made better knowing how your codebase\n      is designed. If your engine has high-level data types for \"textures\" and \"material\" then you may want to use them.\n      If you are starting with OpenGL or DirectX or Vulkan and haven't built much of a rendering engine over them, keeping the default ImTextureID\n      representation suggested by the example bindings is probably the best choice.\n      (Advanced users may also decide to keep a low-level type in ImTextureID, and use ImDrawList callback and pass information to their renderer)\n\n    User code may do:\n\n        // Cast our texture type to ImTextureID / void*\n        MyTexture* texture = g_CoffeeTableTexture;\n        ImGui::Image((void*)texture, ImVec2(texture->Width, texture->Height));\n\n    The renderer function called after ImGui::Render() will receive that same value that the user code passed:\n\n        // Cast ImTextureID / void* stored in the draw command as our texture type\n        MyTexture* texture = (MyTexture*)pcmd->TextureId;\n        MyEngineBindTexture2D(texture);\n\n    Once you understand this design you will understand that loading image files and turning them into displayable textures is not within the scope of Dear ImGui.\n    This is by design and is actually a good thing, because it means your code has full control over your data types and how you display them.\n    If you want to display an image file (e.g. PNG file) into the screen, please refer to documentation and tutorials for the graphics API you are using.\n\n    Here's a simplified OpenGL example using stb_image.h:\n\n        // Use stb_image.h to load a PNG from disk and turn it into raw RGBA pixel data:\n        #define STB_IMAGE_IMPLEMENTATION\n        #include <stb_image.h>\n        [...]\n        int my_image_width, my_image_height;\n        unsigned char* my_image_data = stbi_load(\"my_image.png\", &my_image_width, &my_image_height, NULL, 4);\n\n        // Turn the RGBA pixel data into an OpenGL texture:\n        GLuint my_opengl_texture;\n        glGenTextures(1, &my_opengl_texture);\n        glBindTexture(GL_TEXTURE_2D, my_opengl_texture);\n        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\n        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\n        glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);\n        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);\n\n        // Now that we have an OpenGL texture, assuming our imgui rendering function (imgui_impl_xxx.cpp file) takes GLuint as ImTextureID, we can display it:\n        ImGui::Image((void*)(intptr_t)my_opengl_texture, ImVec2(my_image_width, my_image_height));\n\n    C/C++ tip: a void* is pointer-sized storage. You may safely store any pointer or integer into it by casting your value to ImTextureID / void*, and vice-versa.\n    Because both end-points (user code and rendering function) are under your control, you know exactly what is stored inside the ImTextureID / void*.\n    Examples:\n\n        GLuint my_tex = XXX;\n        void* my_void_ptr;\n        my_void_ptr = (void*)(intptr_t)my_tex;                  // cast a GLuint into a void* (we don't take its address! we literally store the value inside the pointer)\n        my_tex = (GLuint)(intptr_t)my_void_ptr;                 // cast a void* into a GLuint\n\n        ID3D11ShaderResourceView* my_dx11_srv = XXX;\n        void* my_void_ptr;\n        my_void_ptr = (void*)my_dx11_srv;                       // cast a ID3D11ShaderResourceView* into an opaque void*\n        my_dx11_srv = (ID3D11ShaderResourceView*)my_void_ptr;   // cast a void* into a ID3D11ShaderResourceView*\n\n    Finally, you may call ImGui::ShowMetricsWindow() to explore/visualize/understand how the ImDrawList are generated.\n\n Q: Why are multiple widgets reacting when I interact with a single one?\n Q: How can I have multiple widgets with the same label or with an empty label?\n A: A primer on labels and the ID Stack...\n\n    Dear ImGui internally need to uniquely identify UI elements.\n    Elements that are typically not clickable (such as calls to the Text functions) don't need an ID.\n    Interactive widgets (such as calls to Button buttons) need a unique ID.\n    Unique ID are used internally to track active widgets and occasionally associate state to widgets.\n    Unique ID are implicitly built from the hash of multiple elements that identify the \"path\" to the UI element.\n\n   - Unique ID are often derived from a string label:\n\n       Button(\"OK\");          // Label = \"OK\",     ID = hash of (..., \"OK\")\n       Button(\"Cancel\");      // Label = \"Cancel\", ID = hash of (..., \"Cancel\")\n\n   - ID are uniquely scoped within windows, tree nodes, etc. which all pushes to the ID stack. Having\n     two buttons labeled \"OK\" in different windows or different tree locations is fine.\n     We used \"...\" above to signify whatever was already pushed to the ID stack previously:\n\n       Begin(\"MyWindow\");\n       Button(\"OK\");          // Label = \"OK\",     ID = hash of (\"MyWindow\", \"OK\")\n       End();\n       Begin(\"MyOtherWindow\");\n       Button(\"OK\");          // Label = \"OK\",     ID = hash of (\"MyOtherWindow\", \"OK\")\n       End();\n\n   - If you have a same ID twice in the same location, you'll have a conflict:\n\n       Button(\"OK\");\n       Button(\"OK\");          // ID collision! Interacting with either button will trigger the first one.\n\n     Fear not! this is easy to solve and there are many ways to solve it!\n\n   - Solving ID conflict in a simple/local context:\n     When passing a label you can optionally specify extra ID information within string itself.\n     Use \"##\" to pass a complement to the ID that won't be visible to the end-user.\n     This helps solving the simple collision cases when you know e.g. at compilation time which items\n     are going to be created:\n\n       Begin(\"MyWindow\");\n       Button(\"Play\");        // Label = \"Play\",   ID = hash of (\"MyWindow\", \"Play\")\n       Button(\"Play##foo1\");  // Label = \"Play\",   ID = hash of (\"MyWindow\", \"Play##foo1\")  // Different from above\n       Button(\"Play##foo2\");  // Label = \"Play\",   ID = hash of (\"MyWindow\", \"Play##foo2\")  // Different from above\n       End();\n\n   - If you want to completely hide the label, but still need an ID:\n\n       Checkbox(\"##On\", &b);  // Label = \"\",       ID = hash of (..., \"##On\")   // No visible label, just a checkbox!\n\n   - Occasionally/rarely you might want change a label while preserving a constant ID. This allows\n     you to animate labels. For example you may want to include varying information in a window title bar,\n     but windows are uniquely identified by their ID. Use \"###\" to pass a label that isn't part of ID:\n\n       Button(\"Hello###ID\");  // Label = \"Hello\",  ID = hash of (..., \"###ID\")\n       Button(\"World###ID\");  // Label = \"World\",  ID = hash of (..., \"###ID\")  // Same as above, even though the label looks different\n\n       sprintf(buf, \"My game (%f FPS)###MyGame\", fps);\n       Begin(buf);            // Variable title,   ID = hash of \"MyGame\"\n\n   - Solving ID conflict in a more general manner:\n     Use PushID() / PopID() to create scopes and manipulate the ID stack, as to avoid ID conflicts\n     within the same window. This is the most convenient way of distinguishing ID when iterating and\n     creating many UI elements programmatically.\n     You can push a pointer, a string or an integer value into the ID stack.\n     Remember that ID are formed from the concatenation of _everything_ pushed into the ID stack.\n     At each level of the stack we store the seed used for items at this level of the ID stack.\n\n     Begin(\"Window\");\n       for (int i = 0; i < 100; i++)\n       {\n         PushID(i);           // Push i to the id tack\n         Button(\"Click\");     // Label = \"Click\",  ID = hash of (\"Window\", i, \"Click\")\n         PopID();\n       }\n       for (int i = 0; i < 100; i++)\n       {\n         MyObject* obj = Objects[i];\n         PushID(obj);\n         Button(\"Click\");     // Label = \"Click\",  ID = hash of (\"Window\", obj pointer, \"Click\")\n         PopID();\n       }\n       for (int i = 0; i < 100; i++)\n       {\n         MyObject* obj = Objects[i];\n         PushID(obj->Name);\n         Button(\"Click\");     // Label = \"Click\",  ID = hash of (\"Window\", obj->Name, \"Click\")\n         PopID();\n       }\n       End();\n\n   - You can stack multiple prefixes into the ID stack:\n\n       Button(\"Click\");       // Label = \"Click\",  ID = hash of (..., \"Click\")\n       PushID(\"node\");\n       Button(\"Click\");       // Label = \"Click\",  ID = hash of (..., \"node\", \"Click\")\n         PushID(my_ptr);\n           Button(\"Click\");   // Label = \"Click\",  ID = hash of (..., \"node\", my_ptr, \"Click\")\n         PopID();\n       PopID();\n\n   - Tree nodes implicitly creates a scope for you by calling PushID().\n\n       Button(\"Click\");       // Label = \"Click\",  ID = hash of (..., \"Click\")\n       if (TreeNode(\"node\"))  // <-- this function call will do a PushID() for you (unless instructed not to, with a special flag)\n       {\n         Button(\"Click\");     // Label = \"Click\",  ID = hash of (..., \"node\", \"Click\")\n         TreePop();\n       }\n\n   - When working with trees, ID are used to preserve the open/close state of each tree node.\n     Depending on your use cases you may want to use strings, indices or pointers as ID.\n      e.g. when following a single pointer that may change over time, using a static string as ID\n       will preserve your node open/closed state when the targeted object change.\n      e.g. when displaying a list of objects, using indices or pointers as ID will preserve the\n       node open/closed state differently. See what makes more sense in your situation!\n\n Q: How can I use my own math types instead of ImVec2/ImVec4?\n A: You can edit imconfig.h and setup the IM_VEC2_CLASS_EXTRA/IM_VEC4_CLASS_EXTRA macros to add implicit type conversions.\n    This way you'll be able to use your own types everywhere, e.g. passing glm::vec2 to ImGui functions instead of ImVec2.\n\n Q: How can I load a different font than the default?\n A: Use the font atlas to load the TTF/OTF file you want:\n      ImGuiIO& io = ImGui::GetIO();\n      io.Fonts->AddFontFromFileTTF(\"myfontfile.ttf\", size_in_pixels);\n      io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8()\n    Default is ProggyClean.ttf, monospace, rendered at size 13, embedded in dear imgui's source code.\n    (Tip: monospace fonts are convenient because they allow to facilitate horizontal alignment directly at the string level.)\n    (Read the 'misc/fonts/README.txt' file for more details about font loading.)\n\n    New programmers: remember that in C/C++ and most programming languages if you want to use a\n    backslash \\ within a string literal, you need to write it double backslash \"\\\\\":\n      io.Fonts->AddFontFromFileTTF(\"MyDataFolder\\MyFontFile.ttf\", size_in_pixels);   // WRONG (you are escape the M here!)\n      io.Fonts->AddFontFromFileTTF(\"MyDataFolder\\\\MyFontFile.ttf\", size_in_pixels);  // CORRECT\n      io.Fonts->AddFontFromFileTTF(\"MyDataFolder/MyFontFile.ttf\", size_in_pixels);   // ALSO CORRECT\n\n Q: How can I easily use icons in my application?\n A: The most convenient and practical way is to merge an icon font such as FontAwesome inside you\n    main font. Then you can refer to icons within your strings.\n    You may want to see ImFontConfig::GlyphMinAdvanceX to make your icon look monospace to facilitate alignment.\n    (Read the 'misc/fonts/README.txt' file for more details about icons font loading.)\n\n Q: How can I load multiple fonts?\n A: Use the font atlas to pack them into a single texture:\n    (Read the 'misc/fonts/README.txt' file and the code in ImFontAtlas for more details.)\n\n      ImGuiIO& io = ImGui::GetIO();\n      ImFont* font0 = io.Fonts->AddFontDefault();\n      ImFont* font1 = io.Fonts->AddFontFromFileTTF(\"myfontfile.ttf\", size_in_pixels);\n      ImFont* font2 = io.Fonts->AddFontFromFileTTF(\"myfontfile2.ttf\", size_in_pixels);\n      io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8()\n      // the first loaded font gets used by default\n      // use ImGui::PushFont()/ImGui::PopFont() to change the font at runtime\n\n      // Options\n      ImFontConfig config;\n      config.OversampleH = 2;\n      config.OversampleV = 1;\n      config.GlyphOffset.y -= 1.0f;      // Move everything by 1 pixels up\n      config.GlyphExtraSpacing.x = 1.0f; // Increase spacing between characters\n      io.Fonts->AddFontFromFileTTF(\"myfontfile.ttf\", size_pixels, &config);\n\n      // Combine multiple fonts into one (e.g. for icon fonts)\n      static ImWchar ranges[] = { 0xf000, 0xf3ff, 0 };\n      ImFontConfig config;\n      config.MergeMode = true;\n      io.Fonts->AddFontDefault();\n      io.Fonts->AddFontFromFileTTF(\"fontawesome-webfont.ttf\", 16.0f, &config, ranges); // Merge icon font\n      io.Fonts->AddFontFromFileTTF(\"myfontfile.ttf\", size_pixels, NULL, &config, io.Fonts->GetGlyphRangesJapanese()); // Merge japanese glyphs\n\n Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic?\n A: When loading a font, pass custom Unicode ranges to specify the glyphs to load.\n\n      // Add default Japanese ranges\n      io.Fonts->AddFontFromFileTTF(\"myfontfile.ttf\", size_in_pixels, NULL, io.Fonts->GetGlyphRangesJapanese());\n\n      // Or create your own custom ranges (e.g. for a game you can feed your entire game script and only build the characters the game need)\n      ImVector<ImWchar> ranges;\n      ImFontGlyphRangesBuilder builder;\n      builder.AddText(\"Hello world\");                        // Add a string (here \"Hello world\" contains 7 unique characters)\n      builder.AddChar(0x7262);                               // Add a specific character\n      builder.AddRanges(io.Fonts->GetGlyphRangesJapanese()); // Add one of the default ranges\n      builder.BuildRanges(&ranges);                          // Build the final result (ordered ranges with all the unique characters submitted)\n      io.Fonts->AddFontFromFileTTF(\"myfontfile.ttf\", size_in_pixels, NULL, ranges.Data);\n\n    All your strings needs to use UTF-8 encoding. In C++11 you can encode a string literal in UTF-8\n    by using the u8\"hello\" syntax. Specifying literal in your source code using a local code page\n    (such as CP-923 for Japanese or CP-1251 for Cyrillic) will NOT work!\n    Otherwise you can convert yourself to UTF-8 or load text data from file already saved as UTF-8.\n\n    Text input: it is up to your application to pass the right character code by calling io.AddInputCharacter().\n    The applications in examples/ are doing that.\n    Windows: you can use the WM_CHAR or WM_UNICHAR or WM_IME_CHAR message (depending if your app is built using Unicode or MultiByte mode).\n    You may also use MultiByteToWideChar() or ToUnicode() to retrieve Unicode codepoints from MultiByte characters or keyboard state.\n    Windows: if your language is relying on an Input Method Editor (IME), you copy the HWND of your window to io.ImeWindowHandle in order for\n    the default implementation of io.ImeSetInputScreenPosFn() to set your Microsoft IME position correctly.\n\n Q: How can I interact with standard C++ types (such as std::string and std::vector)?\n A: - Being highly portable (bindings for several languages, frameworks, programming style, obscure or older platforms/compilers),\n      and aiming for compatibility & performance suitable for every modern real-time game engines, dear imgui does not use\n      any of std C++ types. We use raw types (e.g. char* instead of std::string) because they adapt to more use cases.\n    - To use ImGui::InputText() with a std::string or any resizable string class, see misc/cpp/imgui_stdlib.h.\n    - To use combo boxes and list boxes with std::vector or any other data structure: the BeginCombo()/EndCombo() API\n      lets you iterate and submit items yourself, so does the ListBoxHeader()/ListBoxFooter() API.\n      Prefer using them over the old and awkward Combo()/ListBox() api.\n    - Generally for most high-level types you should be able to access the underlying data type.\n      You may write your own one-liner wrappers to facilitate user code (tip: add new functions in ImGui:: namespace from your code).\n    - Dear ImGui applications often need to make intensive use of strings. It is expected that many of the strings you will pass\n      to the API are raw literals (free in C/C++) or allocated in a manner that won't incur a large cost on your application.\n      Please bear in mind that using std::string on applications with large amount of UI may incur unsatisfactory performances.\n      Modern implementations of std::string often include small-string optimization (which is often a local buffer) but those\n      are not configurable and not the same across implementations.\n    - If you are finding your UI traversal cost to be too large, make sure your string usage is not leading to excessive amount\n      of heap allocations. Consider using literals, statically sized buffers and your own helper functions. A common pattern\n      is that you will need to build lots of strings on the fly, and their maximum length can be easily be scoped ahead.\n      One possible implementation of a helper to facilitate printf-style building of strings: https://github.com/ocornut/Str\n      This is a small helper where you can instance strings with configurable local buffers length. Many game engines will\n      provide similar or better string helpers.\n\n Q: How can I use the drawing facilities without an ImGui window? (using ImDrawList API)\n A: - You can create a dummy window. Call Begin() with the NoBackground | NoDecoration | NoSavedSettings | NoInputs flags.\n      (The ImGuiWindowFlags_NoDecoration flag itself is a shortcut for NoTitleBar | NoResize | NoScrollbar | NoCollapse)\n      Then you can retrieve the ImDrawList* via GetWindowDrawList() and draw to it in any way you like.\n    - You can call ImGui::GetBackgroundDrawList() or ImGui::GetForegroundDrawList() and use those draw list to display\n      contents behind or over every other imgui windows (one bg/fg drawlist per viewport).\n    - You can create your own ImDrawList instance. You'll need to initialize them ImGui::GetDrawListSharedData(), or create\n      your own ImDrawListSharedData, and then call your rendered code with your own ImDrawList or ImDrawData data.\n\n Q: How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display)\n A: - You can control Dear ImGui with a gamepad. Read about navigation in \"Using gamepad/keyboard navigation controls\".\n      (short version: map gamepad inputs into the io.NavInputs[] array + set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad)\n    - You can share your computer mouse seamlessly with your console/tablet/phone using Synergy (https://symless.com/synergy)\n      This is the preferred solution for developer productivity.\n      In particular, the \"micro-synergy-client\" repository (https://github.com/symless/micro-synergy-client) has simple\n      and portable source code (uSynergy.c/.h) for a small embeddable client that you can use on any platform to connect\n      to your host computer, based on the Synergy 1.x protocol. Make sure you download the Synergy 1 server on your computer.\n      Console SDK also sometimes provide equivalent tooling or wrapper for Synergy-like protocols.\n    - You may also use a third party solution such as Remote ImGui (https://github.com/JordiRos/remoteimgui) which sends\n      the vertices to render over the local network, allowing you to use Dear ImGui even on a screen-less machine.\n    - For touch inputs, you can increase the hit box of widgets (via the style.TouchPadding setting) to accommodate\n      for the lack of precision of touch inputs, but it is recommended you use a mouse or gamepad to allow optimizing\n      for screen real-estate and precision.\n\n Q: I integrated Dear ImGui in my engine and the text or lines are blurry..\n A: In your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f).\n    Also make sure your orthographic projection matrix and io.DisplaySize matches your actual framebuffer dimension.\n\n Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around..\n A: You are probably mishandling the clipping rectangles in your render function.\n    Rectangles provided by ImGui are defined as (x1=left,y1=top,x2=right,y2=bottom) and NOT as (x1,y1,width,height).\n\n Q: How can I help?\n A: - If you are experienced with Dear ImGui and C++, look at the github issues, look at the Wiki, read docs/TODO.txt\n      and see how you want to help and can help!\n    - Businesses: convince your company to fund development via support contracts/sponsoring! This is among the most useful thing you can do for dear imgui.\n    - Individuals: you can also become a Patron (http://www.patreon.com/imgui) or donate on PayPal! See README.\n    - Disclose your usage of dear imgui via a dev blog post, a tweet, a screenshot, a mention somewhere etc.\n      You may post screenshot or links in the gallery threads (github.com/ocornut/imgui/issues/1902). Visuals are ideal as they inspire other programmers.\n      But even without visuals, disclosing your use of dear imgui help the library grow credibility, and help other teams and programmers with taking decisions.\n    - If you have issues or if you need to hack into the library, even if you don't expect any support it is useful that you share your issues (on github or privately).\n\n - tip: you can call Begin() multiple times with the same name during the same frame, it will keep appending to the same window.\n        this is also useful to set yourself in the context of another window (to get/set other settings)\n - tip: you can create widgets without a Begin()/End() block, they will go in an implicit window called \"Debug\".\n - tip: the ImGuiOnceUponAFrame helper will allow run the block of code only once a frame. You can use it to quickly add custom UI in the middle\n        of a deep nested inner loop in your code.\n - tip: you can call Render() multiple times (e.g for VR renders).\n - tip: call and read the ShowDemoWindow() code in imgui_demo.cpp for more example of how to use ImGui!\n\n*/\n\n#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)\n#define _CRT_SECURE_NO_WARNINGS\n#endif\n\n#include \"imgui.hpp\"\n#ifndef IMGUI_DEFINE_MATH_OPERATORS\n#define IMGUI_DEFINE_MATH_OPERATORS\n#endif\n#include \"imgui_internal.hpp\"\n\n#include <ctype.h>      // toupper\n#include <stdio.h>      // vsnprintf, sscanf, printf\n#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier\n#include <stddef.h>     // intptr_t\n#else\n#include <stdint.h>     // intptr_t\n#endif\n\n// Debug options\n#define IMGUI_DEBUG_NAV_SCORING     0   // Display navigation scoring preview when hovering items. Display last moving direction matches when holding CTRL\n#define IMGUI_DEBUG_NAV_RECTS       0   // Display the reference navigation rectangle for each window\n#define IMGUI_DEBUG_DOCKING_INI     0   // Save additional comments in .ini file (makes saving slower)\n\n// Visual Studio warnings\n#ifdef _MSC_VER\n#pragma warning (disable: 4127)     // condition expression is constant\n#pragma warning (disable: 4996)     // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen\n#endif\n\n// Clang/GCC warnings with -Weverything\n#ifdef __clang__\n#pragma clang diagnostic ignored \"-Wunknown-pragmas\"        // warning : unknown warning group '-Wformat-pedantic *'        // not all warnings are known by all clang versions.. so ignoring warnings triggers new warnings on some configuration. great!\n#pragma clang diagnostic ignored \"-Wold-style-cast\"         // warning : use of old-style cast                              // yes, they are more terse.\n#pragma clang diagnostic ignored \"-Wfloat-equal\"            // warning : comparing floating point with == or != is unsafe   // storing and comparing against same constants (typically 0.0f) is ok.\n#pragma clang diagnostic ignored \"-Wformat-nonliteral\"      // warning : format string is not a string literal              // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code.\n#pragma clang diagnostic ignored \"-Wexit-time-destructors\"  // warning : declaration requires an exit-time destructor       // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals.\n#pragma clang diagnostic ignored \"-Wglobal-constructors\"    // warning : declaration requires a global destructor           // similar to above, not sure what the exact difference is.\n#pragma clang diagnostic ignored \"-Wsign-conversion\"        // warning : implicit conversion changes signedness             //\n#pragma clang diagnostic ignored \"-Wformat-pedantic\"        // warning : format specifies type 'void *' but the argument has type 'xxxx *' // unreasonable, would lead to casting every %p arg to void*. probably enabled by -pedantic.\n#pragma clang diagnostic ignored \"-Wint-to-void-pointer-cast\"       // warning : cast to 'void *' from smaller integer type 'int'\n#if __has_warning(\"-Wzero-as-null-pointer-constant\")\n#pragma clang diagnostic ignored \"-Wzero-as-null-pointer-constant\"  // warning : zero as null pointer constant              // some standard header variations use #define NULL 0\n#endif\n#if __has_warning(\"-Wdouble-promotion\")\n#pragma clang diagnostic ignored \"-Wdouble-promotion\"       // warning: implicit conversion from 'float' to 'double' when passing argument to function  // using printf() is a misery with this as C++ va_arg ellipsis changes float to double.\n#endif\n#elif defined(__GNUC__)\n#pragma GCC diagnostic ignored \"-Wunused-function\"          // warning: 'xxxx' defined but not used\n#pragma GCC diagnostic ignored \"-Wint-to-pointer-cast\"      // warning: cast to pointer from integer of different size\n#pragma GCC diagnostic ignored \"-Wformat\"                   // warning: format '%p' expects argument of type 'void*', but argument 6 has type 'ImGuiWindow*'\n#pragma GCC diagnostic ignored \"-Wdouble-promotion\"         // warning: implicit conversion from 'float' to 'double' when passing argument to function\n#pragma GCC diagnostic ignored \"-Wconversion\"               // warning: conversion to 'xxxx' from 'xxxx' may alter its value\n#pragma GCC diagnostic ignored \"-Wformat-nonliteral\"        // warning: format not a string literal, format string not checked\n#pragma GCC diagnostic ignored \"-Wstrict-overflow\"          // warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false\n#if __GNUC__ >= 8\n#pragma GCC diagnostic ignored \"-Wclass-memaccess\"          // warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead\n#endif\n#endif\n\n// When using CTRL+TAB (or Gamepad Square+L/R) we delay the visual a little in order to reduce visual noise doing a fast switch.\nstatic const float NAV_WINDOWING_HIGHLIGHT_DELAY            = 0.20f;    // Time before the highlight and screen dimming starts fading in\nstatic const float NAV_WINDOWING_LIST_APPEAR_DELAY          = 0.15f;    // Time before the window list starts to appear\n\n// Window resizing from edges (when io.ConfigWindowsResizeFromEdges = true and ImGuiBackendFlags_HasMouseCursors is set in io.BackendFlags by back-end)\nstatic const float WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS = 4.0f;     // Extend outside and inside windows. Affect FindHoveredWindow().\nstatic const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f;    // Reduce visual noise by only highlighting the border after a certain time.\n\n// Docking\nstatic const float DOCKING_TRANSPARENT_PAYLOAD_ALPHA        = 0.50f;    // For use with io.ConfigDockingTransparentPayload. Apply to Viewport _or_ WindowBg in host viewport.\n\n//-------------------------------------------------------------------------\n// [SECTION] FORWARD DECLARATIONS\n//-------------------------------------------------------------------------\n\nstatic void             SetCurrentWindow(ImGuiWindow* window);\nstatic void             SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size);\nstatic void             FindHoveredWindow();\nstatic ImGuiWindow*     CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags);\nstatic void             CheckStacksSize(ImGuiWindow* window, bool write);\nstatic ImVec2           CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges);\n\nstatic void             AddDrawListToDrawData(ImVector<ImDrawList*>* out_list, ImDrawList* draw_list);\nstatic void             AddWindowToSortBuffer(ImVector<ImGuiWindow*>* out_sorted_windows, ImGuiWindow* window);\n\n// Settings\nstatic void*            SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name);\nstatic void             SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line);\nstatic void             SettingsHandlerWindow_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf);\n\n// Platform Dependents default implementation for IO functions\nstatic const char*      GetClipboardTextFn_DefaultImpl(void* user_data);\nstatic void             SetClipboardTextFn_DefaultImpl(void* user_data, const char* text);\n\nnamespace ImGui\n{\nstatic bool             BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags);\n\n// Navigation\nstatic void             NavUpdate();\nstatic void             NavUpdateWindowing();\nstatic void             NavUpdateWindowingList();\nstatic void             NavUpdateMoveResult();\nstatic float            NavUpdatePageUpPageDown(int allowed_dir_flags);\nstatic inline void      NavUpdateAnyRequestFlag();\nstatic void             NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, ImGuiID id);\nstatic ImVec2           NavCalcPreferredRefPos();\nstatic void             NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window);\nstatic ImGuiWindow*     NavRestoreLastChildNavWindow(ImGuiWindow* window);\nstatic int              FindWindowFocusIndex(ImGuiWindow* window);\n\n// Misc\nstatic void             UpdateMouseInputs();\nstatic void             UpdateMouseWheel();\nstatic void             UpdateManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]);\nstatic void             RenderOuterBorders(ImGuiWindow* window);\nstatic void             EndFrameDrawDimmedBackgrounds();\n\n// Viewports\nconst ImGuiID           IMGUI_VIEWPORT_DEFAULT_ID = 0x11111111; // Using an arbitrary constant instead of e.g. ImHashStr(\"ViewportDefault\", 0); so it's easier to spot in the debugger. The exact value doesn't matter.\nstatic ImGuiViewportP*  AddUpdateViewport(ImGuiWindow* window, ImGuiID id, const ImVec2& platform_pos, const ImVec2& size, ImGuiViewportFlags flags);\nstatic void             UpdateViewportsNewFrame();\nstatic void             UpdateViewportsEndFrame();\nstatic void             UpdateSelectWindowViewport(ImGuiWindow* window);\nstatic bool             UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImGuiViewportP* host_viewport);\nstatic void             SetCurrentViewport(ImGuiWindow* window, ImGuiViewportP* viewport);\nstatic bool             GetWindowAlwaysWantOwnViewport(ImGuiWindow* window);\nstatic int              FindPlatformMonitorForPos(const ImVec2& pos);\nstatic int              FindPlatformMonitorForRect(const ImRect& r);\nstatic void             UpdateViewportPlatformMonitor(ImGuiViewportP* viewport);\n\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] CONTEXT AND MEMORY ALLOCATORS\n//-----------------------------------------------------------------------------\n\n// Current context pointer. Implicitly used by all Dear ImGui functions. Always assumed to be != NULL.\n// ImGui::CreateContext() will automatically set this pointer if it is NULL. Change to a different context by calling ImGui::SetCurrentContext().\n// 1) Important: globals are not shared across DLL boundaries! If you use DLLs or any form of hot-reloading: you will need to call\n//    SetCurrentContext() (with the pointer you got from CreateContext) from each unique static/DLL boundary, and after each hot-reloading.\n//    In your debugger, add GImGui to your watch window and notice how its value changes depending on which location you are currently stepping into.\n// 2) Important: Dear ImGui functions are not thread-safe because of this pointer.\n//    If you want thread-safety to allow N threads to access N different contexts, you can:\n//    - Change this variable to use thread local storage so each thread can refer to a different context, in imconfig.h:\n//          struct ImGuiContext;\n//          extern thread_local ImGuiContext* MyImGuiTLS;\n//          #define GImGui MyImGuiTLS\n//      And then define MyImGuiTLS in one of your cpp file. Note that thread_local is a C++11 keyword, earlier C++ uses compiler-specific keyword.\n//    - Future development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586\n//    - If you need a finite number of contexts, you may compile and use multiple instances of the ImGui code from different namespace.\n#ifndef GImGui\nImGuiContext*   GImGui = NULL;\n#endif\n\n// Memory Allocator functions. Use SetAllocatorFunctions() to change them.\n// If you use DLL hotreloading you might need to call SetAllocatorFunctions() after reloading code from this file.\n// Otherwise, you probably don't want to modify them mid-program, and if you use global/static e.g. ImVector<> instances you may need to keep them accessible during program destruction.\n#ifndef IMGUI_DISABLE_DEFAULT_ALLOCATORS\nstatic void*   MallocWrapper(size_t size, void* user_data)    { IM_UNUSED(user_data); return malloc(size); }\nstatic void    FreeWrapper(void* ptr, void* user_data)        { IM_UNUSED(user_data); free(ptr); }\n#else\nstatic void*   MallocWrapper(size_t size, void* user_data)    { IM_UNUSED(user_data); IM_UNUSED(size); IM_ASSERT(0); return NULL; }\nstatic void    FreeWrapper(void* ptr, void* user_data)        { IM_UNUSED(user_data); IM_UNUSED(ptr); IM_ASSERT(0); }\n#endif\n\nstatic void*  (*GImAllocatorAllocFunc)(size_t size, void* user_data) = MallocWrapper;\nstatic void   (*GImAllocatorFreeFunc)(void* ptr, void* user_data) = FreeWrapper;\nstatic void*    GImAllocatorUserData = NULL;\n\n//-----------------------------------------------------------------------------\n// [SECTION] MAIN USER FACING STRUCTURES (ImGuiStyle, ImGuiIO)\n//-----------------------------------------------------------------------------\n\nImGuiStyle::ImGuiStyle()\n{\n    Alpha                   = 1.0f;             // Global alpha applies to everything in ImGui\n    WindowPadding           = ImVec2(8,8);      // Padding within a window\n    WindowRounding          = 7.0f;             // Radius of window corners rounding. Set to 0.0f to have rectangular windows\n    WindowBorderSize        = 1.0f;             // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested.\n    WindowMinSize           = ImVec2(32,32);    // Minimum window size\n    WindowTitleAlign        = ImVec2(0.0f,0.5f);// Alignment for title bar text\n    ChildRounding           = 0.0f;             // Radius of child window corners rounding. Set to 0.0f to have rectangular child windows\n    ChildBorderSize         = 1.0f;             // Thickness of border around child windows. Generally set to 0.0f or 1.0f. Other values not well tested.\n    PopupRounding           = 0.0f;             // Radius of popup window corners rounding. Set to 0.0f to have rectangular child windows\n    PopupBorderSize         = 1.0f;             // Thickness of border around popup or tooltip windows. Generally set to 0.0f or 1.0f. Other values not well tested.\n    FramePadding            = ImVec2(4,3);      // Padding within a framed rectangle (used by most widgets)\n    FrameRounding           = 0.0f;             // Radius of frame corners rounding. Set to 0.0f to have rectangular frames (used by most widgets).\n    FrameBorderSize         = 0.0f;             // Thickness of border around frames. Generally set to 0.0f or 1.0f. Other values not well tested.\n    ItemSpacing             = ImVec2(8,4);      // Horizontal and vertical spacing between widgets/lines\n    ItemInnerSpacing        = ImVec2(4,4);      // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label)\n    TouchExtraPadding       = ImVec2(0,0);      // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much!\n    IndentSpacing           = 21.0f;            // Horizontal spacing when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2).\n    ColumnsMinSpacing       = 6.0f;             // Minimum horizontal spacing between two columns\n    ScrollbarSize           = 16.0f;            // Width of the vertical scrollbar, Height of the horizontal scrollbar\n    ScrollbarRounding       = 9.0f;             // Radius of grab corners rounding for scrollbar\n    GrabMinSize             = 10.0f;            // Minimum width/height of a grab box for slider/scrollbar\n    GrabRounding            = 0.0f;             // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.\n    TabRounding             = 4.0f;             // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.\n    TabBorderSize           = 0.0f;             // Thickness of border around tabs.\n    ButtonTextAlign         = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text.\n    SelectableTextAlign     = ImVec2(0.0f,0.0f);// Alignment of selectable text when button is larger than text.\n    DisplayWindowPadding    = ImVec2(19,19);    // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows.\n    DisplaySafeAreaPadding  = ImVec2(3,3);      // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows.\n    MouseCursorScale        = 1.0f;             // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later.\n    AntiAliasedLines        = true;             // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU.\n    AntiAliasedFill         = true;             // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.)\n    CurveTessellationTol    = 1.25f;            // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.\n\n    // Default theme\n    ImGui::StyleColorsDark(this);\n}\n\n// To scale your entire UI (e.g. if you want your app to use High DPI or generally be DPI aware) you may use this helper function. Scaling the fonts is done separately and is up to you.\n// Important: This operation is lossy because we round all sizes to integer. If you need to change your scale multiples, call this over a freshly initialized ImGuiStyle structure rather than scaling multiple times.\nvoid ImGuiStyle::ScaleAllSizes(float scale_factor)\n{\n    WindowPadding = ImFloor(WindowPadding * scale_factor);\n    WindowRounding = ImFloor(WindowRounding * scale_factor);\n    WindowMinSize = ImFloor(WindowMinSize * scale_factor);\n    ChildRounding = ImFloor(ChildRounding * scale_factor);\n    PopupRounding = ImFloor(PopupRounding * scale_factor);\n    FramePadding = ImFloor(FramePadding * scale_factor);\n    FrameRounding = ImFloor(FrameRounding * scale_factor);\n    ItemSpacing = ImFloor(ItemSpacing * scale_factor);\n    ItemInnerSpacing = ImFloor(ItemInnerSpacing * scale_factor);\n    TouchExtraPadding = ImFloor(TouchExtraPadding * scale_factor);\n    IndentSpacing = ImFloor(IndentSpacing * scale_factor);\n    ColumnsMinSpacing = ImFloor(ColumnsMinSpacing * scale_factor);\n    ScrollbarSize = ImFloor(ScrollbarSize * scale_factor);\n    ScrollbarRounding = ImFloor(ScrollbarRounding * scale_factor);\n    GrabMinSize = ImFloor(GrabMinSize * scale_factor);\n    GrabRounding = ImFloor(GrabRounding * scale_factor);\n    TabRounding = ImFloor(TabRounding * scale_factor);\n    DisplayWindowPadding = ImFloor(DisplayWindowPadding * scale_factor);\n    DisplaySafeAreaPadding = ImFloor(DisplaySafeAreaPadding * scale_factor);\n    MouseCursorScale = ImFloor(MouseCursorScale * scale_factor);\n}\n\nImGuiIO::ImGuiIO()\n{\n    // Most fields are initialized with zero\n    memset(this, 0, sizeof(*this));\n\n    // Settings\n    ConfigFlags = ImGuiConfigFlags_None;\n    BackendFlags = ImGuiBackendFlags_None;\n    DisplaySize = ImVec2(-1.0f, -1.0f);\n    DeltaTime = 1.0f/60.0f;\n    IniSavingRate = 5.0f;\n    IniFilename = \"imgui.ini\";\n    LogFilename = \"imgui_log.txt\";\n    MouseDoubleClickTime = 0.30f;\n    MouseDoubleClickMaxDist = 6.0f;\n    for (int i = 0; i < ImGuiKey_COUNT; i++)\n        KeyMap[i] = -1;\n    KeyRepeatDelay = 0.250f;\n    KeyRepeatRate = 0.050f;\n    UserData = NULL;\n\n    Fonts = NULL;\n    FontGlobalScale = 1.0f;\n    FontDefault = NULL;\n    FontAllowUserScaling = false;\n    DisplayFramebufferScale = ImVec2(1.0f, 1.0f);\n\n    // Docking options (when ImGuiConfigFlags_DockingEnable is set)\n    ConfigDockingNoSplit = false;\n    ConfigDockingWithShift = false;\n    ConfigDockingTabBarOnSingleWindows = false;\n    ConfigDockingTransparentPayload = false;\n\n    // Viewport options (when ImGuiConfigFlags_ViewportsEnable is set)\n    ConfigViewportsNoAutoMerge = false;\n    ConfigViewportsNoTaskBarIcon = false;\n    ConfigViewportsNoDecoration = true;\n    ConfigViewportsNoDefaultParent = false;\n\n    // Miscellaneous options\n    MouseDrawCursor = false;\n#ifdef __APPLE__\n    ConfigMacOSXBehaviors = true;  // Set Mac OS X style defaults based on __APPLE__ compile time flag\n#else\n    ConfigMacOSXBehaviors = false;\n#endif\n    ConfigInputTextCursorBlink = true;\n    ConfigWindowsResizeFromEdges = true;\n    ConfigWindowsMoveFromTitleBarOnly = false;\n\n    // Platform Functions\n    BackendPlatformName = BackendRendererName = NULL;\n    BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL;\n    GetClipboardTextFn = GetClipboardTextFn_DefaultImpl;   // Platform dependent default implementations\n    SetClipboardTextFn = SetClipboardTextFn_DefaultImpl;\n    ClipboardUserData = NULL;\n\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    RenderDrawListsFn = NULL;\n#endif\n\n    // Input (NB: we already have memset zero the entire structure!)\n    MousePos = ImVec2(-FLT_MAX, -FLT_MAX);\n    MousePosPrev = ImVec2(-FLT_MAX, -FLT_MAX);\n    MouseDragThreshold = 6.0f;\n    for (int i = 0; i < IM_ARRAYSIZE(MouseDownDuration); i++) MouseDownDuration[i] = MouseDownDurationPrev[i] = -1.0f;\n    for (int i = 0; i < IM_ARRAYSIZE(KeysDownDuration); i++) KeysDownDuration[i]  = KeysDownDurationPrev[i] = -1.0f;\n    for (int i = 0; i < IM_ARRAYSIZE(NavInputsDownDuration); i++) NavInputsDownDuration[i] = -1.0f;\n}\n\n// Pass in translated ASCII characters for text input.\n// - with glfw you can get those from the callback set in glfwSetCharCallback()\n// - on Windows you can get those using ToAscii+keyboard state, or via the WM_CHAR message\nvoid ImGuiIO::AddInputCharacter(ImWchar c)\n{\n    InputQueueCharacters.push_back(c);\n}\n\nvoid ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars)\n{\n    while (*utf8_chars != 0)\n    {\n        unsigned int c = 0;\n        utf8_chars += ImTextCharFromUtf8(&c, utf8_chars, NULL);\n        if (c > 0 && c <= 0xFFFF)\n            InputQueueCharacters.push_back((ImWchar)c);\n    }\n}\n\nvoid ImGuiIO::ClearInputCharacters()\n{\n    InputQueueCharacters.resize(0);\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] MISC HELPERS/UTILITIES (Maths, String, Format, Hash, File functions)\n//-----------------------------------------------------------------------------\n\nImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p)\n{\n    ImVec2 ap = p - a;\n    ImVec2 ab_dir = b - a;\n    float dot = ap.x * ab_dir.x + ap.y * ab_dir.y;\n    if (dot < 0.0f)\n        return a;\n    float ab_len_sqr = ab_dir.x * ab_dir.x + ab_dir.y * ab_dir.y;\n    if (dot > ab_len_sqr)\n        return b;\n    return a + ab_dir * dot / ab_len_sqr;\n}\n\nbool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p)\n{\n    bool b1 = ((p.x - b.x) * (a.y - b.y) - (p.y - b.y) * (a.x - b.x)) < 0.0f;\n    bool b2 = ((p.x - c.x) * (b.y - c.y) - (p.y - c.y) * (b.x - c.x)) < 0.0f;\n    bool b3 = ((p.x - a.x) * (c.y - a.y) - (p.y - a.y) * (c.x - a.x)) < 0.0f;\n    return ((b1 == b2) && (b2 == b3));\n}\n\nvoid ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w)\n{\n    ImVec2 v0 = b - a;\n    ImVec2 v1 = c - a;\n    ImVec2 v2 = p - a;\n    const float denom = v0.x * v1.y - v1.x * v0.y;\n    out_v = (v2.x * v1.y - v1.x * v2.y) / denom;\n    out_w = (v0.x * v2.y - v2.x * v0.y) / denom;\n    out_u = 1.0f - out_v - out_w;\n}\n\nImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p)\n{\n    ImVec2 proj_ab = ImLineClosestPoint(a, b, p);\n    ImVec2 proj_bc = ImLineClosestPoint(b, c, p);\n    ImVec2 proj_ca = ImLineClosestPoint(c, a, p);\n    float dist2_ab = ImLengthSqr(p - proj_ab);\n    float dist2_bc = ImLengthSqr(p - proj_bc);\n    float dist2_ca = ImLengthSqr(p - proj_ca);\n    float m = ImMin(dist2_ab, ImMin(dist2_bc, dist2_ca));\n    if (m == dist2_ab)\n        return proj_ab;\n    if (m == dist2_bc)\n        return proj_bc;\n    return proj_ca;\n}\n\n// Consider using _stricmp/_strnicmp under Windows or strcasecmp/strncasecmp. We don't actually use either ImStricmp/ImStrnicmp in the codebase any more.\nint ImStricmp(const char* str1, const char* str2)\n{\n    int d;\n    while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; }\n    return d;\n}\n\nint ImStrnicmp(const char* str1, const char* str2, size_t count)\n{\n    int d = 0;\n    while (count > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; count--; }\n    return d;\n}\n\nvoid ImStrncpy(char* dst, const char* src, size_t count)\n{\n    if (count < 1)\n        return;\n    if (count > 1)\n        strncpy(dst, src, count - 1);\n    dst[count - 1] = 0;\n}\n\nchar* ImStrdup(const char* str)\n{\n    size_t len = strlen(str);\n    void* buf = IM_ALLOC(len + 1);\n    return (char*)memcpy(buf, (const void*)str, len + 1);\n}\n\nchar* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src)\n{\n    size_t dst_buf_size = p_dst_size ? *p_dst_size : strlen(dst) + 1;\n    size_t src_size = strlen(src) + 1;\n    if (dst_buf_size < src_size)\n    {\n        IM_FREE(dst);\n        dst = (char*)IM_ALLOC(src_size);\n        if (p_dst_size)\n            *p_dst_size = src_size;\n    }\n    return (char*)memcpy(dst, (const void*)src, src_size);\n}\n\nconst char* ImStrchrRange(const char* str, const char* str_end, char c)\n{\n    const char* p = (const char*)memchr(str, (int)c, str_end - str);\n    return p;\n}\n\nint ImStrlenW(const ImWchar* str)\n{\n    //return (int)wcslen((const wchar_t*)str);  // FIXME-OPT: Could use this when wchar_t are 16-bits\n    int n = 0;\n    while (*str++) n++;\n    return n;\n}\n\n// Find end-of-line. Return pointer will point to either first \\n, either str_end.\nconst char* ImStreolRange(const char* str, const char* str_end)\n{\n    const char* p = (const char*)memchr(str, '\\n', str_end - str);\n    return p ? p : str_end;\n}\n\nconst ImWchar* ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin) // find beginning-of-line\n{\n    while (buf_mid_line > buf_begin && buf_mid_line[-1] != '\\n')\n        buf_mid_line--;\n    return buf_mid_line;\n}\n\nconst char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end)\n{\n    if (!needle_end)\n        needle_end = needle + strlen(needle);\n\n    const char un0 = (char)toupper(*needle);\n    while ((!haystack_end && *haystack) || (haystack_end && haystack < haystack_end))\n    {\n        if (toupper(*haystack) == un0)\n        {\n            const char* b = needle + 1;\n            for (const char* a = haystack + 1; b < needle_end; a++, b++)\n                if (toupper(*a) != toupper(*b))\n                    break;\n            if (b == needle_end)\n                return haystack;\n        }\n        haystack++;\n    }\n    return NULL;\n}\n\n// Trim str by offsetting contents when there's leading data + writing a \\0 at the trailing position. We use this in situation where the cost is negligible.\nvoid ImStrTrimBlanks(char* buf)\n{\n    char* p = buf;\n    while (p[0] == ' ' || p[0] == '\\t')     // Leading blanks\n        p++;\n    char* p_start = p;\n    while (*p != 0)                         // Find end of string\n        p++;\n    while (p > p_start && (p[-1] == ' ' || p[-1] == '\\t'))  // Trailing blanks\n        p--;\n    if (p_start != buf)                     // Copy memory if we had leading blanks\n        memmove(buf, p_start, p - p_start);\n    buf[p - p_start] = 0;                   // Zero terminate\n}\n\nconst char* ImStrSkipBlank(const char* str)\n{\n    while (str[0] == ' ' || str[0] == '\\t')\n        str++;\n    return str;\n}\n\n// A) MSVC version appears to return -1 on overflow, whereas glibc appears to return total count (which may be >= buf_size).\n// Ideally we would test for only one of those limits at runtime depending on the behavior the vsnprintf(), but trying to deduct it at compile time sounds like a pandora can of worm.\n// B) When buf==NULL vsnprintf() will return the output size.\n#ifndef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS\n\n//#define IMGUI_USE_STB_SPRINTF\n#ifdef IMGUI_USE_STB_SPRINTF\n#define STB_SPRINTF_IMPLEMENTATION\n#include \"imstb_sprintf.h\"\n#endif\n\n#if defined(_MSC_VER) && !defined(vsnprintf)\n#define vsnprintf _vsnprintf\n#endif\n\nint ImFormatString(char* buf, size_t buf_size, const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n#ifdef IMGUI_USE_STB_SPRINTF\n    int w = stbsp_vsnprintf(buf, (int)buf_size, fmt, args);\n#else\n    int w = vsnprintf(buf, buf_size, fmt, args);\n#endif\n    va_end(args);\n    if (buf == NULL)\n        return w;\n    if (w == -1 || w >= (int)buf_size)\n        w = (int)buf_size - 1;\n    buf[w] = 0;\n    return w;\n}\n\nint ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args)\n{\n#ifdef IMGUI_USE_STB_SPRINTF\n    int w = stbsp_vsnprintf(buf, (int)buf_size, fmt, args);\n#else\n    int w = vsnprintf(buf, buf_size, fmt, args);\n#endif\n    if (buf == NULL)\n        return w;\n    if (w == -1 || w >= (int)buf_size)\n        w = (int)buf_size - 1;\n    buf[w] = 0;\n    return w;\n}\n#endif // #ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS\n\n// CRC32 needs a 1KB lookup table (not cache friendly)\n// Although the code to generate the table is simple and shorter than the table itself, using a const table allows us to easily:\n// - avoid an unnecessary branch/memory tap, - keep the ImHashXXX functions usable by static constructors, - make it thread-safe.\nstatic const ImU32 GCrc32LookupTable[256] =\n{\n    0x00000000,0x77073096,0xEE0E612C,0x990951BA,0x076DC419,0x706AF48F,0xE963A535,0x9E6495A3,0x0EDB8832,0x79DCB8A4,0xE0D5E91E,0x97D2D988,0x09B64C2B,0x7EB17CBD,0xE7B82D07,0x90BF1D91,\n    0x1DB71064,0x6AB020F2,0xF3B97148,0x84BE41DE,0x1ADAD47D,0x6DDDE4EB,0xF4D4B551,0x83D385C7,0x136C9856,0x646BA8C0,0xFD62F97A,0x8A65C9EC,0x14015C4F,0x63066CD9,0xFA0F3D63,0x8D080DF5,\n    0x3B6E20C8,0x4C69105E,0xD56041E4,0xA2677172,0x3C03E4D1,0x4B04D447,0xD20D85FD,0xA50AB56B,0x35B5A8FA,0x42B2986C,0xDBBBC9D6,0xACBCF940,0x32D86CE3,0x45DF5C75,0xDCD60DCF,0xABD13D59,\n    0x26D930AC,0x51DE003A,0xC8D75180,0xBFD06116,0x21B4F4B5,0x56B3C423,0xCFBA9599,0xB8BDA50F,0x2802B89E,0x5F058808,0xC60CD9B2,0xB10BE924,0x2F6F7C87,0x58684C11,0xC1611DAB,0xB6662D3D,\n    0x76DC4190,0x01DB7106,0x98D220BC,0xEFD5102A,0x71B18589,0x06B6B51F,0x9FBFE4A5,0xE8B8D433,0x7807C9A2,0x0F00F934,0x9609A88E,0xE10E9818,0x7F6A0DBB,0x086D3D2D,0x91646C97,0xE6635C01,\n    0x6B6B51F4,0x1C6C6162,0x856530D8,0xF262004E,0x6C0695ED,0x1B01A57B,0x8208F4C1,0xF50FC457,0x65B0D9C6,0x12B7E950,0x8BBEB8EA,0xFCB9887C,0x62DD1DDF,0x15DA2D49,0x8CD37CF3,0xFBD44C65,\n    0x4DB26158,0x3AB551CE,0xA3BC0074,0xD4BB30E2,0x4ADFA541,0x3DD895D7,0xA4D1C46D,0xD3D6F4FB,0x4369E96A,0x346ED9FC,0xAD678846,0xDA60B8D0,0x44042D73,0x33031DE5,0xAA0A4C5F,0xDD0D7CC9,\n    0x5005713C,0x270241AA,0xBE0B1010,0xC90C2086,0x5768B525,0x206F85B3,0xB966D409,0xCE61E49F,0x5EDEF90E,0x29D9C998,0xB0D09822,0xC7D7A8B4,0x59B33D17,0x2EB40D81,0xB7BD5C3B,0xC0BA6CAD,\n    0xEDB88320,0x9ABFB3B6,0x03B6E20C,0x74B1D29A,0xEAD54739,0x9DD277AF,0x04DB2615,0x73DC1683,0xE3630B12,0x94643B84,0x0D6D6A3E,0x7A6A5AA8,0xE40ECF0B,0x9309FF9D,0x0A00AE27,0x7D079EB1,\n    0xF00F9344,0x8708A3D2,0x1E01F268,0x6906C2FE,0xF762575D,0x806567CB,0x196C3671,0x6E6B06E7,0xFED41B76,0x89D32BE0,0x10DA7A5A,0x67DD4ACC,0xF9B9DF6F,0x8EBEEFF9,0x17B7BE43,0x60B08ED5,\n    0xD6D6A3E8,0xA1D1937E,0x38D8C2C4,0x4FDFF252,0xD1BB67F1,0xA6BC5767,0x3FB506DD,0x48B2364B,0xD80D2BDA,0xAF0A1B4C,0x36034AF6,0x41047A60,0xDF60EFC3,0xA867DF55,0x316E8EEF,0x4669BE79,\n    0xCB61B38C,0xBC66831A,0x256FD2A0,0x5268E236,0xCC0C7795,0xBB0B4703,0x220216B9,0x5505262F,0xC5BA3BBE,0xB2BD0B28,0x2BB45A92,0x5CB36A04,0xC2D7FFA7,0xB5D0CF31,0x2CD99E8B,0x5BDEAE1D,\n    0x9B64C2B0,0xEC63F226,0x756AA39C,0x026D930A,0x9C0906A9,0xEB0E363F,0x72076785,0x05005713,0x95BF4A82,0xE2B87A14,0x7BB12BAE,0x0CB61B38,0x92D28E9B,0xE5D5BE0D,0x7CDCEFB7,0x0BDBDF21,\n    0x86D3D2D4,0xF1D4E242,0x68DDB3F8,0x1FDA836E,0x81BE16CD,0xF6B9265B,0x6FB077E1,0x18B74777,0x88085AE6,0xFF0F6A70,0x66063BCA,0x11010B5C,0x8F659EFF,0xF862AE69,0x616BFFD3,0x166CCF45,\n    0xA00AE278,0xD70DD2EE,0x4E048354,0x3903B3C2,0xA7672661,0xD06016F7,0x4969474D,0x3E6E77DB,0xAED16A4A,0xD9D65ADC,0x40DF0B66,0x37D83BF0,0xA9BCAE53,0xDEBB9EC5,0x47B2CF7F,0x30B5FFE9,\n    0xBDBDF21C,0xCABAC28A,0x53B39330,0x24B4A3A6,0xBAD03605,0xCDD70693,0x54DE5729,0x23D967BF,0xB3667A2E,0xC4614AB8,0x5D681B02,0x2A6F2B94,0xB40BBE37,0xC30C8EA1,0x5A05DF1B,0x2D02EF8D,\n};\n\n// Known size hash\n// It is ok to call ImHashData on a string with known length but the ### operator won't be supported.\n// FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements.\nImU32 ImHashData(const void* data_p, size_t data_size, ImU32 seed)\n{\n    ImU32 crc = ~seed;\n    const unsigned char* data = (const unsigned char*)data_p;\n    const ImU32* crc32_lut = GCrc32LookupTable;\n    while (data_size-- != 0)\n        crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ *data++];\n    return ~crc;\n}\n\n// Zero-terminated string hash, with support for ### to reset back to seed value\n// We support a syntax of \"label###id\" where only \"###id\" is included in the hash, and only \"label\" gets displayed.\n// Because this syntax is rarely used we are optimizing for the common case.\n// - If we reach ### in the string we discard the hash so far and reset to the seed.\n// - We don't do 'current += 2; continue;' after handling ### to keep the code smaller/faster (measured ~10% diff in Debug build)\n// FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements.\nImU32 ImHashStr(const char* data_p, size_t data_size, ImU32 seed)\n{\n    seed = ~seed;\n    ImU32 crc = seed;\n    const unsigned char* data = (const unsigned char*)data_p;\n    const ImU32* crc32_lut = GCrc32LookupTable;\n    if (data_size != 0)\n    {\n        while (data_size-- != 0)\n        {\n            unsigned char c = *data++;\n            if (c == '#' && data_size >= 2 && data[0] == '#' && data[1] == '#')\n                crc = seed;\n            crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ c];\n        }\n    }\n    else\n    {\n        while (unsigned char c = *data++)\n        {\n            if (c == '#' && data[0] == '#' && data[1] == '#')\n                crc = seed;\n            crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ c];\n        }\n    }\n    return ~crc;\n}\n\nFILE* ImFileOpen(const char* filename, const char* mode)\n{\n#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__GNUC__)\n    // We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames. Converting both strings from UTF-8 to wchar format (using a single allocation, because we can)\n    const int filename_wsize = ImTextCountCharsFromUtf8(filename, NULL) + 1;\n    const int mode_wsize = ImTextCountCharsFromUtf8(mode, NULL) + 1;\n    ImVector<ImWchar> buf;\n    buf.resize(filename_wsize + mode_wsize);\n    ImTextStrFromUtf8(&buf[0], filename_wsize, filename, NULL);\n    ImTextStrFromUtf8(&buf[filename_wsize], mode_wsize, mode, NULL);\n    return _wfopen((wchar_t*)&buf[0], (wchar_t*)&buf[filename_wsize]);\n#else\n    return fopen(filename, mode);\n#endif\n}\n\n// Load file content into memory\n// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree()\nvoid* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size, int padding_bytes)\n{\n    IM_ASSERT(filename && file_open_mode);\n    if (out_file_size)\n        *out_file_size = 0;\n\n    FILE* f;\n    if ((f = ImFileOpen(filename, file_open_mode)) == NULL)\n        return NULL;\n\n    long file_size_signed;\n    if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET))\n    {\n        fclose(f);\n        return NULL;\n    }\n\n    size_t file_size = (size_t)file_size_signed;\n    void* file_data = IM_ALLOC(file_size + padding_bytes);\n    if (file_data == NULL)\n    {\n        fclose(f);\n        return NULL;\n    }\n    if (fread(file_data, 1, file_size, f) != file_size)\n    {\n        fclose(f);\n        IM_FREE(file_data);\n        return NULL;\n    }\n    if (padding_bytes > 0)\n        memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes);\n\n    fclose(f);\n    if (out_file_size)\n        *out_file_size = file_size;\n\n    return file_data;\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] MISC HELPERS/UTILITIES (ImText* functions)\n//-----------------------------------------------------------------------------\n\n// Convert UTF-8 to 32-bits character, process single character input.\n// Based on stb_from_utf8() from github.com/nothings/stb/\n// We handle UTF-8 decoding error by skipping forward.\nint ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end)\n{\n    unsigned int c = (unsigned int)-1;\n    const unsigned char* str = (const unsigned char*)in_text;\n    if (!(*str & 0x80))\n    {\n        c = (unsigned int)(*str++);\n        *out_char = c;\n        return 1;\n    }\n    if ((*str & 0xe0) == 0xc0)\n    {\n        *out_char = 0xFFFD; // will be invalid but not end of string\n        if (in_text_end && in_text_end - (const char*)str < 2) return 1;\n        if (*str < 0xc2) return 2;\n        c = (unsigned int)((*str++ & 0x1f) << 6);\n        if ((*str & 0xc0) != 0x80) return 2;\n        c += (*str++ & 0x3f);\n        *out_char = c;\n        return 2;\n    }\n    if ((*str & 0xf0) == 0xe0)\n    {\n        *out_char = 0xFFFD; // will be invalid but not end of string\n        if (in_text_end && in_text_end - (const char*)str < 3) return 1;\n        if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return 3;\n        if (*str == 0xed && str[1] > 0x9f) return 3; // str[1] < 0x80 is checked below\n        c = (unsigned int)((*str++ & 0x0f) << 12);\n        if ((*str & 0xc0) != 0x80) return 3;\n        c += (unsigned int)((*str++ & 0x3f) << 6);\n        if ((*str & 0xc0) != 0x80) return 3;\n        c += (*str++ & 0x3f);\n        *out_char = c;\n        return 3;\n    }\n    if ((*str & 0xf8) == 0xf0)\n    {\n        *out_char = 0xFFFD; // will be invalid but not end of string\n        if (in_text_end && in_text_end - (const char*)str < 4) return 1;\n        if (*str > 0xf4) return 4;\n        if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return 4;\n        if (*str == 0xf4 && str[1] > 0x8f) return 4; // str[1] < 0x80 is checked below\n        c = (unsigned int)((*str++ & 0x07) << 18);\n        if ((*str & 0xc0) != 0x80) return 4;\n        c += (unsigned int)((*str++ & 0x3f) << 12);\n        if ((*str & 0xc0) != 0x80) return 4;\n        c += (unsigned int)((*str++ & 0x3f) << 6);\n        if ((*str & 0xc0) != 0x80) return 4;\n        c += (*str++ & 0x3f);\n        // utf-8 encodings of values used in surrogate pairs are invalid\n        if ((c & 0xFFFFF800) == 0xD800) return 4;\n        *out_char = c;\n        return 4;\n    }\n    *out_char = 0;\n    return 0;\n}\n\nint ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining)\n{\n    ImWchar* buf_out = buf;\n    ImWchar* buf_end = buf + buf_size;\n    while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text)\n    {\n        unsigned int c;\n        in_text += ImTextCharFromUtf8(&c, in_text, in_text_end);\n        if (c == 0)\n            break;\n        if (c < 0x10000)    // FIXME: Losing characters that don't fit in 2 bytes\n            *buf_out++ = (ImWchar)c;\n    }\n    *buf_out = 0;\n    if (in_text_remaining)\n        *in_text_remaining = in_text;\n    return (int)(buf_out - buf);\n}\n\nint ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end)\n{\n    int char_count = 0;\n    while ((!in_text_end || in_text < in_text_end) && *in_text)\n    {\n        unsigned int c;\n        in_text += ImTextCharFromUtf8(&c, in_text, in_text_end);\n        if (c == 0)\n            break;\n        if (c < 0x10000)\n            char_count++;\n    }\n    return char_count;\n}\n\n// Based on stb_to_utf8() from github.com/nothings/stb/\nstatic inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c)\n{\n    if (c < 0x80)\n    {\n        buf[0] = (char)c;\n        return 1;\n    }\n    if (c < 0x800)\n    {\n        if (buf_size < 2) return 0;\n        buf[0] = (char)(0xc0 + (c >> 6));\n        buf[1] = (char)(0x80 + (c & 0x3f));\n        return 2;\n    }\n    if (c >= 0xdc00 && c < 0xe000)\n    {\n        return 0;\n    }\n    if (c >= 0xd800 && c < 0xdc00)\n    {\n        if (buf_size < 4) return 0;\n        buf[0] = (char)(0xf0 + (c >> 18));\n        buf[1] = (char)(0x80 + ((c >> 12) & 0x3f));\n        buf[2] = (char)(0x80 + ((c >> 6) & 0x3f));\n        buf[3] = (char)(0x80 + ((c ) & 0x3f));\n        return 4;\n    }\n    //else if (c < 0x10000)\n    {\n        if (buf_size < 3) return 0;\n        buf[0] = (char)(0xe0 + (c >> 12));\n        buf[1] = (char)(0x80 + ((c>> 6) & 0x3f));\n        buf[2] = (char)(0x80 + ((c ) & 0x3f));\n        return 3;\n    }\n}\n\n// Not optimal but we very rarely use this function.\nint ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end)\n{\n    unsigned int dummy = 0;\n    return ImTextCharFromUtf8(&dummy, in_text, in_text_end);\n}\n\nstatic inline int ImTextCountUtf8BytesFromChar(unsigned int c)\n{\n    if (c < 0x80) return 1;\n    if (c < 0x800) return 2;\n    if (c >= 0xdc00 && c < 0xe000) return 0;\n    if (c >= 0xd800 && c < 0xdc00) return 4;\n    return 3;\n}\n\nint ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end)\n{\n    char* buf_out = buf;\n    const char* buf_end = buf + buf_size;\n    while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text)\n    {\n        unsigned int c = (unsigned int)(*in_text++);\n        if (c < 0x80)\n            *buf_out++ = (char)c;\n        else\n            buf_out += ImTextCharToUtf8(buf_out, (int)(buf_end-buf_out-1), c);\n    }\n    *buf_out = 0;\n    return (int)(buf_out - buf);\n}\n\nint ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end)\n{\n    int bytes_count = 0;\n    while ((!in_text_end || in_text < in_text_end) && *in_text)\n    {\n        unsigned int c = (unsigned int)(*in_text++);\n        if (c < 0x80)\n            bytes_count++;\n        else\n            bytes_count += ImTextCountUtf8BytesFromChar(c);\n    }\n    return bytes_count;\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] MISC HELPERS/UTILTIES (Color functions)\n// Note: The Convert functions are early design which are not consistent with other API.\n//-----------------------------------------------------------------------------\n\nImVec4 ImGui::ColorConvertU32ToFloat4(ImU32 in)\n{\n    float s = 1.0f/255.0f;\n    return ImVec4(\n        ((in >> IM_COL32_R_SHIFT) & 0xFF) * s,\n        ((in >> IM_COL32_G_SHIFT) & 0xFF) * s,\n        ((in >> IM_COL32_B_SHIFT) & 0xFF) * s,\n        ((in >> IM_COL32_A_SHIFT) & 0xFF) * s);\n}\n\nImU32 ImGui::ColorConvertFloat4ToU32(const ImVec4& in)\n{\n    ImU32 out;\n    out  = ((ImU32)IM_F32_TO_INT8_SAT(in.x)) << IM_COL32_R_SHIFT;\n    out |= ((ImU32)IM_F32_TO_INT8_SAT(in.y)) << IM_COL32_G_SHIFT;\n    out |= ((ImU32)IM_F32_TO_INT8_SAT(in.z)) << IM_COL32_B_SHIFT;\n    out |= ((ImU32)IM_F32_TO_INT8_SAT(in.w)) << IM_COL32_A_SHIFT;\n    return out;\n}\n\n// Convert rgb floats ([0-1],[0-1],[0-1]) to hsv floats ([0-1],[0-1],[0-1]), from Foley & van Dam p592\n// Optimized http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv\nvoid ImGui::ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v)\n{\n    float K = 0.f;\n    if (g < b)\n    {\n        ImSwap(g, b);\n        K = -1.f;\n    }\n    if (r < g)\n    {\n        ImSwap(r, g);\n        K = -2.f / 6.f - K;\n    }\n\n    const float chroma = r - (g < b ? g : b);\n    out_h = ImFabs(K + (g - b) / (6.f * chroma + 1e-20f));\n    out_s = chroma / (r + 1e-20f);\n    out_v = r;\n}\n\n// Convert hsv floats ([0-1],[0-1],[0-1]) to rgb floats ([0-1],[0-1],[0-1]), from Foley & van Dam p593\n// also http://en.wikipedia.org/wiki/HSL_and_HSV\nvoid ImGui::ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b)\n{\n    if (s == 0.0f)\n    {\n        // gray\n        out_r = out_g = out_b = v;\n        return;\n    }\n\n    h = ImFmod(h, 1.0f) / (60.0f/360.0f);\n    int   i = (int)h;\n    float f = h - (float)i;\n    float p = v * (1.0f - s);\n    float q = v * (1.0f - s * f);\n    float t = v * (1.0f - s * (1.0f - f));\n\n    switch (i)\n    {\n    case 0: out_r = v; out_g = t; out_b = p; break;\n    case 1: out_r = q; out_g = v; out_b = p; break;\n    case 2: out_r = p; out_g = v; out_b = t; break;\n    case 3: out_r = p; out_g = q; out_b = v; break;\n    case 4: out_r = t; out_g = p; out_b = v; break;\n    case 5: default: out_r = v; out_g = p; out_b = q; break;\n    }\n}\n\nImU32 ImGui::GetColorU32(ImGuiCol idx, float alpha_mul)\n{\n    ImGuiStyle& style = GImGui->Style;\n    ImVec4 c = style.Colors[idx];\n    c.w *= style.Alpha * alpha_mul;\n    return ColorConvertFloat4ToU32(c);\n}\n\nImU32 ImGui::GetColorU32(const ImVec4& col)\n{\n    ImGuiStyle& style = GImGui->Style;\n    ImVec4 c = col;\n    c.w *= style.Alpha;\n    return ColorConvertFloat4ToU32(c);\n}\n\nconst ImVec4& ImGui::GetStyleColorVec4(ImGuiCol idx)\n{\n    ImGuiStyle& style = GImGui->Style;\n    return style.Colors[idx];\n}\n\nImU32 ImGui::GetColorU32(ImU32 col)\n{\n    float style_alpha = GImGui->Style.Alpha;\n    if (style_alpha >= 1.0f)\n        return col;\n    ImU32 a = (col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT;\n    a = (ImU32)(a * style_alpha); // We don't need to clamp 0..255 because Style.Alpha is in 0..1 range.\n    return (col & ~IM_COL32_A_MASK) | (a << IM_COL32_A_SHIFT);\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] ImGuiStorage\n// Helper: Key->value storage\n//-----------------------------------------------------------------------------\n\n// std::lower_bound but without the bullshit\nstatic ImGuiStorage::Pair* LowerBound(ImVector<ImGuiStorage::Pair>& data, ImGuiID key)\n{\n    ImGuiStorage::Pair* first = data.Data;\n    ImGuiStorage::Pair* last = data.Data + data.Size;\n    size_t count = (size_t)(last - first);\n    while (count > 0)\n    {\n        size_t count2 = count >> 1;\n        ImGuiStorage::Pair* mid = first + count2;\n        if (mid->key < key)\n        {\n            first = ++mid;\n            count -= count2 + 1;\n        }\n        else\n        {\n            count = count2;\n        }\n    }\n    return first;\n}\n\n// For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once.\nvoid ImGuiStorage::BuildSortByKey()\n{\n    struct StaticFunc\n    {\n        static int IMGUI_CDECL PairCompareByID(const void* lhs, const void* rhs)\n        {\n            // We can't just do a subtraction because qsort uses signed integers and subtracting our ID doesn't play well with that.\n            if (((const Pair*)lhs)->key > ((const Pair*)rhs)->key) return +1;\n            if (((const Pair*)lhs)->key < ((const Pair*)rhs)->key) return -1;\n            return 0;\n        }\n    };\n    if (Data.Size > 1)\n        ImQsort(Data.Data, (size_t)Data.Size, sizeof(Pair), StaticFunc::PairCompareByID);\n}\n\nint ImGuiStorage::GetInt(ImGuiID key, int default_val) const\n{\n    ImGuiStorage::Pair* it = LowerBound(const_cast<ImVector<ImGuiStorage::Pair>&>(Data), key);\n    if (it == Data.end() || it->key != key)\n        return default_val;\n    return it->val_i;\n}\n\nbool ImGuiStorage::GetBool(ImGuiID key, bool default_val) const\n{\n    return GetInt(key, default_val ? 1 : 0) != 0;\n}\n\nfloat ImGuiStorage::GetFloat(ImGuiID key, float default_val) const\n{\n    ImGuiStorage::Pair* it = LowerBound(const_cast<ImVector<ImGuiStorage::Pair>&>(Data), key);\n    if (it == Data.end() || it->key != key)\n        return default_val;\n    return it->val_f;\n}\n\nvoid* ImGuiStorage::GetVoidPtr(ImGuiID key) const\n{\n    ImGuiStorage::Pair* it = LowerBound(const_cast<ImVector<ImGuiStorage::Pair>&>(Data), key);\n    if (it == Data.end() || it->key != key)\n        return NULL;\n    return it->val_p;\n}\n\n// References are only valid until a new value is added to the storage. Calling a Set***() function or a Get***Ref() function invalidates the pointer.\nint* ImGuiStorage::GetIntRef(ImGuiID key, int default_val)\n{\n    ImGuiStorage::Pair* it = LowerBound(Data, key);\n    if (it == Data.end() || it->key != key)\n        it = Data.insert(it, Pair(key, default_val));\n    return &it->val_i;\n}\n\nbool* ImGuiStorage::GetBoolRef(ImGuiID key, bool default_val)\n{\n    return (bool*)GetIntRef(key, default_val ? 1 : 0);\n}\n\nfloat* ImGuiStorage::GetFloatRef(ImGuiID key, float default_val)\n{\n    ImGuiStorage::Pair* it = LowerBound(Data, key);\n    if (it == Data.end() || it->key != key)\n        it = Data.insert(it, Pair(key, default_val));\n    return &it->val_f;\n}\n\nvoid** ImGuiStorage::GetVoidPtrRef(ImGuiID key, void* default_val)\n{\n    ImGuiStorage::Pair* it = LowerBound(Data, key);\n    if (it == Data.end() || it->key != key)\n        it = Data.insert(it, Pair(key, default_val));\n    return &it->val_p;\n}\n\n// FIXME-OPT: Need a way to reuse the result of lower_bound when doing GetInt()/SetInt() - not too bad because it only happens on explicit interaction (maximum one a frame)\nvoid ImGuiStorage::SetInt(ImGuiID key, int val)\n{\n    ImGuiStorage::Pair* it = LowerBound(Data, key);\n    if (it == Data.end() || it->key != key)\n    {\n        Data.insert(it, Pair(key, val));\n        return;\n    }\n    it->val_i = val;\n}\n\nvoid ImGuiStorage::SetBool(ImGuiID key, bool val)\n{\n    SetInt(key, val ? 1 : 0);\n}\n\nvoid ImGuiStorage::SetFloat(ImGuiID key, float val)\n{\n    ImGuiStorage::Pair* it = LowerBound(Data, key);\n    if (it == Data.end() || it->key != key)\n    {\n        Data.insert(it, Pair(key, val));\n        return;\n    }\n    it->val_f = val;\n}\n\nvoid ImGuiStorage::SetVoidPtr(ImGuiID key, void* val)\n{\n    ImGuiStorage::Pair* it = LowerBound(Data, key);\n    if (it == Data.end() || it->key != key)\n    {\n        Data.insert(it, Pair(key, val));\n        return;\n    }\n    it->val_p = val;\n}\n\nvoid ImGuiStorage::SetAllInt(int v)\n{\n    for (int i = 0; i < Data.Size; i++)\n        Data[i].val_i = v;\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] ImGuiTextFilter\n//-----------------------------------------------------------------------------\n\n// Helper: Parse and apply text filters. In format \"aaaaa[,bbbb][,ccccc]\"\nImGuiTextFilter::ImGuiTextFilter(const char* default_filter)\n{\n    if (default_filter)\n    {\n        ImStrncpy(InputBuf, default_filter, IM_ARRAYSIZE(InputBuf));\n        Build();\n    }\n    else\n    {\n        InputBuf[0] = 0;\n        CountGrep = 0;\n    }\n}\n\nbool ImGuiTextFilter::Draw(const char* label, float width)\n{\n    if (width != 0.0f)\n        ImGui::SetNextItemWidth(width);\n    bool value_changed = ImGui::InputText(label, InputBuf, IM_ARRAYSIZE(InputBuf));\n    if (value_changed)\n        Build();\n    return value_changed;\n}\n\nvoid ImGuiTextFilter::TextRange::split(char separator, ImVector<TextRange>* out) const\n{\n    out->resize(0);\n    const char* wb = b;\n    const char* we = wb;\n    while (we < e)\n    {\n        if (*we == separator)\n        {\n            out->push_back(TextRange(wb, we));\n            wb = we + 1;\n        }\n        we++;\n    }\n    if (wb != we)\n        out->push_back(TextRange(wb, we));\n}\n\nvoid ImGuiTextFilter::Build()\n{\n    Filters.resize(0);\n    TextRange input_range(InputBuf, InputBuf+strlen(InputBuf));\n    input_range.split(',', &Filters);\n\n    CountGrep = 0;\n    for (int i = 0; i != Filters.Size; i++)\n    {\n        TextRange& f = Filters[i];\n        while (f.b < f.e && ImCharIsBlankA(f.b[0]))\n            f.b++;\n        while (f.e > f.b && ImCharIsBlankA(f.e[-1]))\n            f.e--;\n        if (f.empty())\n            continue;\n        if (Filters[i].b[0] != '-')\n            CountGrep += 1;\n    }\n}\n\nbool ImGuiTextFilter::PassFilter(const char* text, const char* text_end) const\n{\n    if (Filters.empty())\n        return true;\n\n    if (text == NULL)\n        text = \"\";\n\n    for (int i = 0; i != Filters.Size; i++)\n    {\n        const TextRange& f = Filters[i];\n        if (f.empty())\n            continue;\n        if (f.b[0] == '-')\n        {\n            // Subtract\n            if (ImStristr(text, text_end, f.begin()+1, f.end()) != NULL)\n                return false;\n        }\n        else\n        {\n            // Grep\n            if (ImStristr(text, text_end, f.begin(), f.end()) != NULL)\n                return true;\n        }\n    }\n\n    // Implicit * grep\n    if (CountGrep == 0)\n        return true;\n\n    return false;\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] ImGuiTextBuffer\n//-----------------------------------------------------------------------------\n\n// On some platform vsnprintf() takes va_list by reference and modifies it.\n// va_copy is the 'correct' way to copy a va_list but Visual Studio prior to 2013 doesn't have it.\n#ifndef va_copy\n#if defined(__GNUC__) || defined(__clang__)\n#define va_copy(dest, src) __builtin_va_copy(dest, src)\n#else\n#define va_copy(dest, src) (dest = src)\n#endif\n#endif\n\nchar ImGuiTextBuffer::EmptyString[1] = { 0 };\n\nvoid ImGuiTextBuffer::append(const char* str, const char* str_end)\n{\n    int len = str_end ? (int)(str_end - str) : (int)strlen(str);\n\n    // Add zero-terminator the first time\n    const int write_off = (Buf.Size != 0) ? Buf.Size : 1;\n    const int needed_sz = write_off + len;\n    if (write_off + len >= Buf.Capacity)\n    {\n        int new_capacity = Buf.Capacity * 2;\n        Buf.reserve(needed_sz > new_capacity ? needed_sz : new_capacity);\n    }\n\n    Buf.resize(needed_sz);\n    memcpy(&Buf[write_off - 1], str, (size_t)len);\n    Buf[write_off - 1 + len] = 0;\n}\n\nvoid ImGuiTextBuffer::appendf(const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    appendfv(fmt, args);\n    va_end(args);\n}\n\n// Helper: Text buffer for logging/accumulating text\nvoid ImGuiTextBuffer::appendfv(const char* fmt, va_list args)\n{\n    va_list args_copy;\n    va_copy(args_copy, args);\n\n    int len = ImFormatStringV(NULL, 0, fmt, args);         // FIXME-OPT: could do a first pass write attempt, likely successful on first pass.\n    if (len <= 0)\n    {\n        va_end(args_copy);\n        return;\n    }\n\n    // Add zero-terminator the first time\n    const int write_off = (Buf.Size != 0) ? Buf.Size : 1;\n    const int needed_sz = write_off + len;\n    if (write_off + len >= Buf.Capacity)\n    {\n        int new_capacity = Buf.Capacity * 2;\n        Buf.reserve(needed_sz > new_capacity ? needed_sz : new_capacity);\n    }\n\n    Buf.resize(needed_sz);\n    ImFormatStringV(&Buf[write_off - 1], (size_t)len + 1, fmt, args_copy);\n    va_end(args_copy);\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] ImGuiListClipper\n// This is currently not as flexible/powerful as it should be, needs some rework (see TODO)\n//-----------------------------------------------------------------------------\n\nstatic void SetCursorPosYAndSetupDummyPrevLine(float pos_y, float line_height)\n{\n    // Set cursor position and a few other things so that SetScrollHereY() and Columns() can work when seeking cursor.\n    // FIXME: It is problematic that we have to do that here, because custom/equivalent end-user code would stumble on the same issue.\n    // The clipper should probably have a 4th step to display the last item in a regular manner.\n    ImGui::SetCursorPosY(pos_y);\n    ImGuiWindow* window = ImGui::GetCurrentWindow();\n    window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y - line_height;      // Setting those fields so that SetScrollHereY() can properly function after the end of our clipper usage.\n    window->DC.PrevLineSize.y = (line_height - GImGui->Style.ItemSpacing.y);    // If we end up needing more accurate data (to e.g. use SameLine) we may as well make the clipper have a fourth step to let user process and display the last item in their list.\n    if (window->DC.CurrentColumns)\n        window->DC.CurrentColumns->LineMinY = window->DC.CursorPos.y;           // Setting this so that cell Y position are set properly\n}\n\n// Use case A: Begin() called from constructor with items_height<0, then called again from Sync() in StepNo 1\n// Use case B: Begin() called from constructor with items_height>0\n// FIXME-LEGACY: Ideally we should remove the Begin/End functions but they are part of the legacy API we still support. This is why some of the code in Step() calling Begin() and reassign some fields, spaghetti style.\nvoid ImGuiListClipper::Begin(int count, float items_height)\n{\n    StartPosY = ImGui::GetCursorPosY();\n    ItemsHeight = items_height;\n    ItemsCount = count;\n    StepNo = 0;\n    DisplayEnd = DisplayStart = -1;\n    if (ItemsHeight > 0.0f)\n    {\n        ImGui::CalcListClipping(ItemsCount, ItemsHeight, &DisplayStart, &DisplayEnd); // calculate how many to clip/display\n        if (DisplayStart > 0)\n            SetCursorPosYAndSetupDummyPrevLine(StartPosY + DisplayStart * ItemsHeight, ItemsHeight); // advance cursor\n        StepNo = 2;\n    }\n}\n\nvoid ImGuiListClipper::End()\n{\n    if (ItemsCount < 0)\n        return;\n    // In theory here we should assert that ImGui::GetCursorPosY() == StartPosY + DisplayEnd * ItemsHeight, but it feels saner to just seek at the end and not assert/crash the user.\n    if (ItemsCount < INT_MAX)\n        SetCursorPosYAndSetupDummyPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); // advance cursor\n    ItemsCount = -1;\n    StepNo = 3;\n}\n\nbool ImGuiListClipper::Step()\n{\n    if (ItemsCount == 0 || ImGui::GetCurrentWindowRead()->SkipItems)\n    {\n        ItemsCount = -1;\n        return false;\n    }\n    if (StepNo == 0) // Step 0: the clipper let you process the first element, regardless of it being visible or not, so we can measure the element height.\n    {\n        DisplayStart = 0;\n        DisplayEnd = 1;\n        StartPosY = ImGui::GetCursorPosY();\n        StepNo = 1;\n        return true;\n    }\n    if (StepNo == 1) // Step 1: the clipper infer height from first element, calculate the actual range of elements to display, and position the cursor before the first element.\n    {\n        if (ItemsCount == 1) { ItemsCount = -1; return false; }\n        float items_height = ImGui::GetCursorPosY() - StartPosY;\n        IM_ASSERT(items_height > 0.0f);   // If this triggers, it means Item 0 hasn't moved the cursor vertically\n        Begin(ItemsCount-1, items_height);\n        DisplayStart++;\n        DisplayEnd++;\n        StepNo = 3;\n        return true;\n    }\n    if (StepNo == 2) // Step 2: dummy step only required if an explicit items_height was passed to constructor or Begin() and user still call Step(). Does nothing and switch to Step 3.\n    {\n        IM_ASSERT(DisplayStart >= 0 && DisplayEnd >= 0);\n        StepNo = 3;\n        return true;\n    }\n    if (StepNo == 3) // Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), advance the cursor to the end of the list and then returns 'false' to end the loop.\n        End();\n    return false;\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] RENDER HELPERS\n// Those (internal) functions are currently quite a legacy mess - their signature and behavior will change.\n// Also see imgui_draw.cpp for some more which have been reworked to not rely on ImGui:: state.\n//-----------------------------------------------------------------------------\n\nconst char* ImGui::FindRenderedTextEnd(const char* text, const char* text_end)\n{\n    const char* text_display_end = text;\n    if (!text_end)\n        text_end = (const char*)-1;\n\n    while (text_display_end < text_end && *text_display_end != '\\0' && (text_display_end[0] != '#' || text_display_end[1] != '#'))\n        text_display_end++;\n    return text_display_end;\n}\n\n// Internal ImGui functions to render text\n// RenderText***() functions calls ImDrawList::AddText() calls ImBitmapFont::RenderText()\nvoid ImGui::RenderText(ImVec2 pos, const char* text, const char* text_end, bool hide_text_after_hash)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    // Hide anything after a '##' string\n    const char* text_display_end;\n    if (hide_text_after_hash)\n    {\n        text_display_end = FindRenderedTextEnd(text, text_end);\n    }\n    else\n    {\n        if (!text_end)\n            text_end = text + strlen(text); // FIXME-OPT\n        text_display_end = text_end;\n    }\n\n    if (text != text_display_end)\n    {\n        window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_display_end);\n        if (g.LogEnabled)\n            LogRenderedText(&pos, text, text_display_end);\n    }\n}\n\nvoid ImGui::RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    if (!text_end)\n        text_end = text + strlen(text); // FIXME-OPT\n\n    if (text != text_end)\n    {\n        window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_end, wrap_width);\n        if (g.LogEnabled)\n            LogRenderedText(&pos, text, text_end);\n    }\n}\n\n// Default clip_rect uses (pos_min,pos_max)\n// Handle clipping on CPU immediately (vs typically let the GPU clip the triangles that are overlapping the clipping rectangle edges)\nvoid ImGui::RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_display_end, const ImVec2* text_size_if_known, const ImVec2& align, const ImRect* clip_rect)\n{\n    // Perform CPU side clipping for single clipped element to avoid using scissor state\n    ImVec2 pos = pos_min;\n    const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_display_end, false, 0.0f);\n\n    const ImVec2* clip_min = clip_rect ? &clip_rect->Min : &pos_min;\n    const ImVec2* clip_max = clip_rect ? &clip_rect->Max : &pos_max;\n    bool need_clipping = (pos.x + text_size.x >= clip_max->x) || (pos.y + text_size.y >= clip_max->y);\n    if (clip_rect) // If we had no explicit clipping rectangle then pos==clip_min\n        need_clipping |= (pos.x < clip_min->x) || (pos.y < clip_min->y);\n\n    // Align whole block. We should defer that to the better rendering function when we'll have support for individual line alignment.\n    if (align.x > 0.0f) pos.x = ImMax(pos.x, pos.x + (pos_max.x - pos.x - text_size.x) * align.x);\n    if (align.y > 0.0f) pos.y = ImMax(pos.y, pos.y + (pos_max.y - pos.y - text_size.y) * align.y);\n\n    // Render\n    if (need_clipping)\n    {\n        ImVec4 fine_clip_rect(clip_min->x, clip_min->y, clip_max->x, clip_max->y);\n        draw_list->AddText(NULL, 0.0f, pos, GetColorU32(ImGuiCol_Text), text, text_display_end, 0.0f, &fine_clip_rect);\n    }\n    else\n    {\n        draw_list->AddText(NULL, 0.0f, pos, GetColorU32(ImGuiCol_Text), text, text_display_end, 0.0f, NULL);\n    }\n}\n\nvoid ImGui::RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align, const ImRect* clip_rect)\n{\n    // Hide anything after a '##' string\n    const char* text_display_end = FindRenderedTextEnd(text, text_end);\n    const int text_len = (int)(text_display_end - text);\n    if (text_len == 0)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    RenderTextClippedEx(window->DrawList, pos_min, pos_max, text, text_display_end, text_size_if_known, align, clip_rect);\n    if (g.LogEnabled)\n        LogRenderedText(&pos_min, text, text_display_end);\n}\n\n// Render a rectangle shaped with optional rounding and borders\nvoid ImGui::RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    window->DrawList->AddRectFilled(p_min, p_max, fill_col, rounding);\n    const float border_size = g.Style.FrameBorderSize;\n    if (border && border_size > 0.0f)\n    {\n        window->DrawList->AddRect(p_min+ImVec2(1,1), p_max+ImVec2(1,1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size);\n        window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size);\n    }\n}\n\nvoid ImGui::RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    const float border_size = g.Style.FrameBorderSize;\n    if (border_size > 0.0f)\n    {\n        window->DrawList->AddRect(p_min+ImVec2(1,1), p_max+ImVec2(1,1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size);\n        window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size);\n    }\n}\n\n// Render an arrow aimed to be aligned with text (p_min is a position in the same space text would be positioned). To e.g. denote expanded/collapsed state\nvoid ImGui::RenderArrow(ImVec2 p_min, ImGuiDir dir, float scale)\n{\n    ImGuiContext& g = *GImGui;\n\n    const float h = g.FontSize * 1.00f;\n    float r = h * 0.40f * scale;\n    ImVec2 center = p_min + ImVec2(h * 0.50f, h * 0.50f * scale);\n\n    ImVec2 a, b, c;\n    switch (dir)\n    {\n    case ImGuiDir_Up:\n    case ImGuiDir_Down:\n        if (dir == ImGuiDir_Up) r = -r;\n        a = ImVec2(+0.000f,+0.750f) * r;\n        b = ImVec2(-0.866f,-0.750f) * r;\n        c = ImVec2(+0.866f,-0.750f) * r;\n        break;\n    case ImGuiDir_Left:\n    case ImGuiDir_Right:\n        if (dir == ImGuiDir_Left) r = -r;\n        a = ImVec2(+0.750f,+0.000f) * r;\n        b = ImVec2(-0.750f,+0.866f) * r;\n        c = ImVec2(-0.750f,-0.866f) * r;\n        break;\n    case ImGuiDir_None:\n    case ImGuiDir_COUNT:\n        IM_ASSERT(0);\n        break;\n    }\n\n    g.CurrentWindow->DrawList->AddTriangleFilled(center + a, center + b, center + c, GetColorU32(ImGuiCol_Text));\n}\n\nvoid ImGui::RenderBullet(ImVec2 pos)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    window->DrawList->AddCircleFilled(pos, g.FontSize*0.20f, GetColorU32(ImGuiCol_Text), 8);\n}\n\nvoid ImGui::RenderCheckMark(ImVec2 pos, ImU32 col, float sz)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    float thickness = ImMax(sz / 5.0f, 1.0f);\n    sz -= thickness*0.5f;\n    pos += ImVec2(thickness*0.25f, thickness*0.25f);\n\n    float third = sz / 3.0f;\n    float bx = pos.x + third;\n    float by = pos.y + sz - third*0.5f;\n    window->DrawList->PathLineTo(ImVec2(bx - third, by - third));\n    window->DrawList->PathLineTo(ImVec2(bx, by));\n    window->DrawList->PathLineTo(ImVec2(bx + third*2, by - third*2));\n    window->DrawList->PathStroke(col, false, thickness);\n}\n\nvoid ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    if (id != g.NavId)\n        return;\n    if (g.NavDisableHighlight && !(flags & ImGuiNavHighlightFlags_AlwaysDraw))\n        return;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (window->DC.NavHideHighlightOneFrame)\n        return;\n\n    float rounding = (flags & ImGuiNavHighlightFlags_NoRounding) ? 0.0f : g.Style.FrameRounding;\n    ImRect display_rect = bb;\n    display_rect.ClipWith(window->ClipRect);\n    if (flags & ImGuiNavHighlightFlags_TypeDefault)\n    {\n        const float THICKNESS = 2.0f;\n        const float DISTANCE = 3.0f + THICKNESS * 0.5f;\n        display_rect.Expand(ImVec2(DISTANCE,DISTANCE));\n        bool fully_visible = window->ClipRect.Contains(display_rect);\n        if (!fully_visible)\n            window->DrawList->PushClipRect(display_rect.Min, display_rect.Max);\n        window->DrawList->AddRect(display_rect.Min + ImVec2(THICKNESS*0.5f,THICKNESS*0.5f), display_rect.Max - ImVec2(THICKNESS*0.5f,THICKNESS*0.5f), GetColorU32(ImGuiCol_NavHighlight), rounding, ImDrawCornerFlags_All, THICKNESS);\n        if (!fully_visible)\n            window->DrawList->PopClipRect();\n    }\n    if (flags & ImGuiNavHighlightFlags_TypeThin)\n    {\n        window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, ~0, 1.0f);\n    }\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!)\n//-----------------------------------------------------------------------------\n\n// ImGuiWindow is mostly a dumb struct. It merely has a constructor and a few helper methods\nImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name)\n    : DrawListInst(&context->DrawListSharedData)\n{\n    Name = ImStrdup(name);\n    ID = ImHashStr(name);\n    IDStack.push_back(ID);\n    Flags = FlagsPreviousFrame = ImGuiWindowFlags_None;\n    Viewport = NULL;\n    ViewportId = 0;\n    ViewportAllowPlatformMonitorExtend = -1;\n    ViewportPos = ImVec2(FLT_MAX, FLT_MAX);\n    Pos = ImVec2(0.0f, 0.0f);\n    Size = SizeFull = ImVec2(0.0f, 0.0f);\n    SizeContents = SizeContentsExplicit = ImVec2(0.0f, 0.0f);\n    WindowPadding = ImVec2(0.0f, 0.0f);\n    WindowRounding = 0.0f;\n    WindowBorderSize = 0.0f;\n    NameBufLen = (int)strlen(name) + 1;\n    MoveId = GetID(\"#MOVE\");\n    ChildId = 0;\n    Scroll = ImVec2(0.0f, 0.0f);\n    ScrollTarget = ImVec2(FLT_MAX, FLT_MAX);\n    ScrollTargetCenterRatio = ImVec2(0.5f, 0.5f);\n    ScrollbarSizes = ImVec2(0.0f, 0.0f);\n    ScrollbarX = ScrollbarY = false;\n    ViewportOwned = false;\n    Active = WasActive = false;\n    WriteAccessed = false;\n    Collapsed = false;\n    WantCollapseToggle = false;\n    SkipItems = false;\n    Appearing = false;\n    Hidden = false;\n    HasCloseButton = false;\n    ResizeBorderHeld = -1;\n    BeginCount = 0;\n    BeginOrderWithinParent = -1;\n    BeginOrderWithinContext = -1;\n    PopupId = 0;\n    AutoFitFramesX = AutoFitFramesY = -1;\n    AutoFitOnlyGrows = false;\n    AutoFitChildAxises = 0x00;\n    AutoPosLastDirection = ImGuiDir_None;\n    HiddenFramesCanSkipItems = HiddenFramesCannotSkipItems = 0;\n    SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = SetWindowDockAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing;\n    SetWindowPosVal = SetWindowPosPivot = ImVec2(FLT_MAX, FLT_MAX);\n\n    LastFrameActive = -1;\n    LastFrameJustFocused = -1;\n    ItemWidthDefault = 0.0f;\n    FontWindowScale = FontDpiScale = 1.0f;\n    SettingsIdx = -1;\n\n    DrawList = &DrawListInst;\n    DrawList->_OwnerName = Name;\n    ParentWindow = NULL;\n    RootWindow = NULL;\n    RootWindowDockStop = NULL;\n    RootWindowForTitleBarHighlight = NULL;\n    RootWindowForNav = NULL;\n\n    NavLastIds[0] = NavLastIds[1] = 0;\n    NavRectRel[0] = NavRectRel[1] = ImRect();\n    NavLastChildNavWindow = NULL;\n\n    DockNode = DockNodeAsHost = NULL;\n    DockId = 0;\n    DockTabItemStatusFlags = ImGuiItemStatusFlags_None;\n    DockOrder = -1;\n    DockIsActive = DockTabIsVisible = DockTabWantClose = false;\n}\n\nImGuiWindow::~ImGuiWindow()\n{\n    IM_ASSERT(DrawList == &DrawListInst);\n    IM_DELETE(Name);\n    for (int i = 0; i != ColumnsStorage.Size; i++)\n        ColumnsStorage[i].~ImGuiColumns();\n}\n\nImGuiID ImGuiWindow::GetID(const char* str, const char* str_end)\n{\n    ImGuiID seed = IDStack.back();\n    ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed);\n    ImGui::KeepAliveID(id);\n    return id;\n}\n\nImGuiID ImGuiWindow::GetID(const void* ptr)\n{\n    ImGuiID seed = IDStack.back();\n    ImGuiID id = ImHashData(&ptr, sizeof(void*), seed);\n    ImGui::KeepAliveID(id);\n    return id;\n}\n\nImGuiID ImGuiWindow::GetIDNoKeepAlive(const char* str, const char* str_end)\n{\n    ImGuiID seed = IDStack.back();\n    return ImHashStr(str, str_end ? (str_end - str) : 0, seed);\n}\n\nImGuiID ImGuiWindow::GetIDNoKeepAlive(const void* ptr)\n{\n    ImGuiID seed = IDStack.back();\n    return ImHashData(&ptr, sizeof(void*), seed);\n}\n\n// This is only used in rare/specific situations to manufacture an ID out of nowhere.\nImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs)\n{\n    ImGuiID seed = IDStack.back();\n    const int r_rel[4] = { (int)(r_abs.Min.x - Pos.x), (int)(r_abs.Min.y - Pos.y), (int)(r_abs.Max.x - Pos.x), (int)(r_abs.Max.y - Pos.y) };\n    ImGuiID id = ImHashData(&r_rel, sizeof(r_rel), seed);\n    ImGui::KeepAliveID(id);\n    return id;\n}\n\nstatic void SetCurrentWindow(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    g.CurrentWindow = window;\n    if (window)\n        g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize();\n}\n\nvoid ImGui::SetNavID(ImGuiID id, int nav_layer)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.NavWindow);\n    IM_ASSERT(nav_layer == 0 || nav_layer == 1);\n    g.NavId = id;\n    g.NavWindow->NavLastIds[nav_layer] = id;\n}\n\nvoid ImGui::SetNavIDWithRectRel(ImGuiID id, int nav_layer, const ImRect& rect_rel)\n{\n    ImGuiContext& g = *GImGui;\n    SetNavID(id, nav_layer);\n    g.NavWindow->NavRectRel[nav_layer] = rect_rel;\n    g.NavMousePosDirty = true;\n    g.NavDisableHighlight = false;\n    g.NavDisableMouseHover = true;\n}\n\nvoid ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    g.ActiveIdIsJustActivated = (g.ActiveId != id);\n    if (g.ActiveIdIsJustActivated)\n    {\n        g.ActiveIdTimer = 0.0f;\n        g.ActiveIdHasBeenPressed = false;\n        g.ActiveIdHasBeenEdited = false;\n        if (id != 0)\n        {\n            g.LastActiveId = id;\n            g.LastActiveIdTimer = 0.0f;\n        }\n    }\n    g.ActiveId = id;\n    g.ActiveIdAllowNavDirFlags = 0;\n    g.ActiveIdBlockNavInputFlags = 0;\n    g.ActiveIdAllowOverlap = false;\n    g.ActiveIdWindow = window;\n    if (id)\n    {\n        g.ActiveIdIsAlive = id;\n        g.ActiveIdSource = (g.NavActivateId == id || g.NavInputId == id || g.NavJustTabbedId == id || g.NavJustMovedToId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse;\n    }\n}\n\n// FIXME-NAV: The existence of SetNavID/SetNavIDWithRectRel/SetFocusID is incredibly messy and confusing and needs some explanation or refactoring.\nvoid ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(id != 0);\n\n    // Assume that SetFocusID() is called in the context where its NavLayer is the current layer, which is the case everywhere we call it.\n    const ImGuiNavLayer nav_layer = window->DC.NavLayerCurrent;\n    if (g.NavWindow != window)\n        g.NavInitRequest = false;\n    g.NavId = id;\n    g.NavWindow = window;\n    g.NavLayer = nav_layer;\n    window->NavLastIds[nav_layer] = id;\n    if (window->DC.LastItemId == id)\n        window->NavRectRel[nav_layer] = ImRect(window->DC.LastItemRect.Min - window->Pos, window->DC.LastItemRect.Max - window->Pos);\n\n    if (g.ActiveIdSource == ImGuiInputSource_Nav)\n        g.NavDisableMouseHover = true;\n    else\n        g.NavDisableHighlight = true;\n}\n\nvoid ImGui::ClearActiveID()\n{\n    SetActiveID(0, NULL);\n}\n\nvoid ImGui::SetHoveredID(ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    g.HoveredId = id;\n    g.HoveredIdAllowOverlap = false;\n    if (id != 0 && g.HoveredIdPreviousFrame != id)\n        g.HoveredIdTimer = g.HoveredIdNotActiveTimer = 0.0f;\n}\n\nImGuiID ImGui::GetHoveredID()\n{\n    ImGuiContext& g = *GImGui;\n    return g.HoveredId ? g.HoveredId : g.HoveredIdPreviousFrame;\n}\n\nvoid ImGui::KeepAliveID(ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.ActiveId == id)\n        g.ActiveIdIsAlive = id;\n    if (g.ActiveIdPreviousFrame == id)\n        g.ActiveIdPreviousFrameIsAlive = true;\n}\n\nvoid ImGui::MarkItemEdited(ImGuiID id)\n{\n    // This marking is solely to be able to provide info for IsItemDeactivatedAfterEdit().\n    // ActiveId might have been released by the time we call this (as in the typical press/release button behavior) but still need need to fill the data.\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.ActiveId == id || g.ActiveId == 0 || g.DragDropActive);\n    IM_UNUSED(id); // Avoid unused variable warnings when asserts are compiled out.\n    //IM_ASSERT(g.CurrentWindow->DC.LastItemId == id);\n    g.ActiveIdHasBeenEdited = true;\n    g.CurrentWindow->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Edited;\n}\n\nstatic inline bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFlags flags)\n{\n    // An active popup disable hovering on other windows (apart from its own children)\n    // FIXME-OPT: This could be cached/stored within the window.\n    ImGuiContext& g = *GImGui;\n    if (g.NavWindow)\n        if (ImGuiWindow* focused_root_window = g.NavWindow->RootWindow)\n            if (focused_root_window->WasActive && focused_root_window != window->RootWindow)\n            {\n                // For the purpose of those flags we differentiate \"standard popup\" from \"modal popup\"\n                // NB: The order of those two tests is important because Modal windows are also Popups.\n                if (focused_root_window->Flags & ImGuiWindowFlags_Modal)\n                    return false;\n                if ((focused_root_window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiHoveredFlags_AllowWhenBlockedByPopup))\n                    return false;\n            }\n\n    // Filter by viewport\n    if (window->Viewport != g.MouseViewport)\n        if (g.MovingWindow == NULL || window->RootWindow != g.MovingWindow->RootWindow)\n            return false;\n\n    return true;\n}\n\n// Advance cursor given item size for layout.\nvoid ImGui::ItemSize(const ImVec2& size, float text_offset_y)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (window->SkipItems)\n        return;\n\n    // Always align ourselves on pixel boundaries\n    const float line_height = ImMax(window->DC.CurrentLineSize.y, size.y);\n    const float text_base_offset = ImMax(window->DC.CurrentLineTextBaseOffset, text_offset_y);\n    //if (g.IO.KeyAlt) window->DrawList->AddRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(size.x, line_height), IM_COL32(255,0,0,200)); // [DEBUG]\n    window->DC.CursorPosPrevLine = ImVec2(window->DC.CursorPos.x + size.x, window->DC.CursorPos.y);\n    window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);\n    window->DC.CursorPos.y = (float)(int)(window->DC.CursorPos.y + line_height + g.Style.ItemSpacing.y);\n    window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x);\n    window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y);\n    //if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG]\n\n    window->DC.PrevLineSize.y = line_height;\n    window->DC.PrevLineTextBaseOffset = text_base_offset;\n    window->DC.CurrentLineSize.y = window->DC.CurrentLineTextBaseOffset = 0.0f;\n\n    // Horizontal layout mode\n    if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)\n        SameLine();\n}\n\nvoid ImGui::ItemSize(const ImRect& bb, float text_offset_y)\n{\n    ItemSize(bb.GetSize(), text_offset_y);\n}\n\n// Declare item bounding box for clipping and interaction.\n// Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface\n// declare their minimum size requirement to ItemSize() and then use a larger region for drawing/interaction, which is passed to ItemAdd().\nbool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    if (id != 0)\n    {\n        // Navigation processing runs prior to clipping early-out\n        //  (a) So that NavInitRequest can be honored, for newly opened windows to select a default widget\n        //  (b) So that we can scroll up/down past clipped items. This adds a small O(N) cost to regular navigation requests unfortunately, but it is still limited to one window.\n        //      it may not scale very well for windows with ten of thousands of item, but at least NavMoveRequest is only set on user interaction, aka maximum once a frame.\n        //      We could early out with \"if (is_clipped && !g.NavInitRequest) return false;\" but when we wouldn't be able to reach unclipped widgets. This would work if user had explicit scrolling control (e.g. mapped on a stick)\n        window->DC.NavLayerActiveMaskNext |= window->DC.NavLayerCurrentMask;\n        if (g.NavId == id || g.NavAnyRequest)\n            if (g.NavWindow->RootWindowForNav == window->RootWindowForNav)\n                if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened))\n                    NavProcessItem(window, nav_bb_arg ? *nav_bb_arg : bb, id);\n    }\n\n    window->DC.LastItemId = id;\n    window->DC.LastItemRect = bb;\n    window->DC.LastItemStatusFlags = ImGuiItemStatusFlags_None;\n\n#ifdef IMGUI_ENABLE_TEST_ENGINE\n    if (id != 0)\n        IMGUI_TEST_ENGINE_ITEM_ADD(nav_bb_arg ? *nav_bb_arg : bb, id);\n#endif\n\n    // Clipping test\n    const bool is_clipped = IsClippedEx(bb, id, false);\n    if (is_clipped)\n        return false;\n    //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG]\n\n    // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them)\n    if (IsMouseHoveringRect(bb.Min, bb.Max))\n        window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredRect;\n    return true;\n}\n\n// This is roughly matching the behavior of internal-facing ItemHoverable()\n// - we allow hovering to be true when ActiveId==window->MoveID, so that clicking on non-interactive items such as a Text() item still returns true with IsItemHovered()\n// - this should work even for non-interactive items that have no ID, so we cannot use LastItemId\nbool ImGui::IsItemHovered(ImGuiHoveredFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (g.NavDisableMouseHover && !g.NavDisableHighlight)\n        return IsItemFocused();\n\n    // Test for bounding box overlap, as updated as ItemAdd()\n    if (!(window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect))\n        return false;\n    IM_ASSERT((flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) == 0);   // Flags not supported by this function\n\n    // Test if we are hovering the right window (our window could be behind another window)\n    // [2017/10/16] Reverted commit 344d48be3 and testing RootWindow instead. I believe it is correct to NOT test for RootWindow but this leaves us unable to use IsItemHovered() after EndChild() itself.\n    // Until a solution is found I believe reverting to the test from 2017/09/27 is safe since this was the test that has been running for a long while.\n    //if (g.HoveredWindow != window)\n    //    return false;\n    if (g.HoveredRootWindow != window->RootWindow && !(flags & ImGuiHoveredFlags_AllowWhenOverlapped))\n        return false;\n\n    // Test if another item is active (e.g. being dragged)\n    if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))\n        if (g.ActiveId != 0 && g.ActiveId != window->DC.LastItemId && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId)\n            return false;\n\n    // Test if interactions on this window are blocked by an active popup or modal.\n    // The ImGuiHoveredFlags_AllowWhenBlockedByPopup flag will be tested here.\n    if (!IsWindowContentHoverable(window, flags))\n        return false;\n\n    // Test if the item is disabled\n    if ((window->DC.ItemFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled))\n        return false;\n\n    // Special handling for the dummy item after Begin() which represent the title bar or tab.\n    // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case.\n    if ((window->DC.LastItemId == window->ID || window->DC.LastItemId == window->MoveId) && window->WriteAccessed)\n        return false;\n    return true;\n}\n\n// Internal facing ItemHoverable() used when submitting widgets. Differs slightly from IsItemHovered().\nbool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.HoveredId != 0 && g.HoveredId != id && !g.HoveredIdAllowOverlap)\n        return false;\n\n    ImGuiWindow* window = g.CurrentWindow;\n    if (g.HoveredWindow != window)\n        return false;\n    if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap)\n        return false;\n    if (!IsMouseHoveringRect(bb.Min, bb.Max))\n        return false;\n    if (g.NavDisableMouseHover || !IsWindowContentHoverable(window, ImGuiHoveredFlags_None))\n        return false;\n    if (window->DC.ItemFlags & ImGuiItemFlags_Disabled)\n        return false;\n\n    SetHoveredID(id);\n    return true;\n}\n\nbool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (!bb.Overlaps(window->ClipRect))\n        if (id == 0 || id != g.ActiveId)\n            if (clip_even_when_logged || !g.LogEnabled)\n                return true;\n    return false;\n}\n\n// Process TAB/Shift+TAB. Be mindful that this function may _clear_ the ActiveID when tabbing out.\nbool ImGui::FocusableItemRegister(ImGuiWindow* window, ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n\n    // Increment counters\n    const bool is_tab_stop = (window->DC.ItemFlags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0;\n    window->DC.FocusCounterAll++;\n    if (is_tab_stop)\n        window->DC.FocusCounterTab++;\n\n    // Process TAB/Shift-TAB to tab *OUT* of the currently focused item.\n    // (Note that we can always TAB out of a widget that doesn't allow tabbing in)\n    if (g.ActiveId == id && g.FocusTabPressed && !(g.ActiveIdBlockNavInputFlags & (1 << ImGuiNavInput_KeyTab_)) && g.FocusRequestNextWindow == NULL)\n    {\n        g.FocusRequestNextWindow = window;\n        g.FocusRequestNextCounterTab = window->DC.FocusCounterTab + (g.IO.KeyShift ? (is_tab_stop ? -1 : 0) : +1); // Modulo on index will be applied at the end of frame once we've got the total counter of items.\n    }\n\n    // Handle focus requests\n    if (g.FocusRequestCurrWindow == window)\n    {\n        if (window->DC.FocusCounterAll == g.FocusRequestCurrCounterAll)\n            return true;\n        if (is_tab_stop && window->DC.FocusCounterTab == g.FocusRequestCurrCounterTab)\n        {\n            g.NavJustTabbedId = id;\n            return true;\n        }\n\n        // If another item is about to be focused, we clear our own active id\n        if (g.ActiveId == id)\n            ClearActiveID();\n    }\n\n    return false;\n}\n\nvoid ImGui::FocusableItemUnregister(ImGuiWindow* window)\n{\n    window->DC.FocusCounterAll--;\n    window->DC.FocusCounterTab--;\n}\n\nfloat ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x)\n{\n    if (wrap_pos_x < 0.0f)\n        return 0.0f;\n\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    if (wrap_pos_x == 0.0f)\n        wrap_pos_x = GetContentRegionMaxScreen().x;\n    else if (wrap_pos_x > 0.0f)\n        wrap_pos_x += window->Pos.x - window->Scroll.x; // wrap_pos_x is provided is window local space\n\n    return ImMax(wrap_pos_x - pos.x, 1.0f);\n}\n\n// IM_ALLOC() == ImGui::MemAlloc()\nvoid* ImGui::MemAlloc(size_t size)\n{\n    if (ImGuiContext* ctx = GImGui)\n        ctx->IO.MetricsActiveAllocations++;\n    return GImAllocatorAllocFunc(size, GImAllocatorUserData);\n}\n\n// IM_FREE() == ImGui::MemFree()\nvoid ImGui::MemFree(void* ptr)\n{\n    if (ptr)\n        if (ImGuiContext* ctx = GImGui)\n            ctx->IO.MetricsActiveAllocations--;\n    return GImAllocatorFreeFunc(ptr, GImAllocatorUserData);\n}\n\nconst char* ImGui::GetClipboardText()\n{\n    return GImGui->IO.GetClipboardTextFn ? GImGui->IO.GetClipboardTextFn(GImGui->IO.ClipboardUserData) : \"\";\n}\n\nvoid ImGui::SetClipboardText(const char* text)\n{\n    if (GImGui->IO.SetClipboardTextFn)\n        GImGui->IO.SetClipboardTextFn(GImGui->IO.ClipboardUserData, text);\n}\n\nconst char* ImGui::GetVersion()\n{\n    return IMGUI_VERSION;\n}\n\n// Internal state access - if you want to share ImGui state between modules (e.g. DLL) or allocate it yourself\n// Note that we still point to some static data and members (such as GFontAtlas), so the state instance you end up using will point to the static data within its module\nImGuiContext* ImGui::GetCurrentContext()\n{\n    return GImGui;\n}\n\nvoid ImGui::SetCurrentContext(ImGuiContext* ctx)\n{\n#ifdef IMGUI_SET_CURRENT_CONTEXT_FUNC\n    IMGUI_SET_CURRENT_CONTEXT_FUNC(ctx); // For custom thread-based hackery you may want to have control over this.\n#else\n    GImGui = ctx;\n#endif\n}\n\n// Helper function to verify that the type sizes are matching between the calling file's compilation unit and imgui.cpp's compilation unit\n// If the user has inconsistent compilation settings, imgui configuration #define, packing pragma, etc. you may see different structures from what imgui.cpp sees which is highly problematic.\nbool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_vert)\n{\n    bool error = false;\n    if (strcmp(version, IMGUI_VERSION)!=0) { error = true; IM_ASSERT(strcmp(version,IMGUI_VERSION)==0 && \"Mismatched version string!\");  }\n    if (sz_io    != sizeof(ImGuiIO))       { error = true; IM_ASSERT(sz_io    == sizeof(ImGuiIO)      && \"Mismatched struct layout!\"); }\n    if (sz_style != sizeof(ImGuiStyle))    { error = true; IM_ASSERT(sz_style == sizeof(ImGuiStyle)   && \"Mismatched struct layout!\"); }\n    if (sz_vec2  != sizeof(ImVec2))        { error = true; IM_ASSERT(sz_vec2  == sizeof(ImVec2)       && \"Mismatched struct layout!\"); }\n    if (sz_vec4  != sizeof(ImVec4))        { error = true; IM_ASSERT(sz_vec4  == sizeof(ImVec4)       && \"Mismatched struct layout!\"); }\n    if (sz_vert  != sizeof(ImDrawVert))    { error = true; IM_ASSERT(sz_vert  == sizeof(ImDrawVert)   && \"Mismatched struct layout!\"); }\n    return !error;\n}\n\nvoid ImGui::SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data)\n{\n    GImAllocatorAllocFunc = alloc_func;\n    GImAllocatorFreeFunc = free_func;\n    GImAllocatorUserData = user_data;\n}\n\nImGuiContext* ImGui::CreateContext(ImFontAtlas* shared_font_atlas)\n{\n    ImGuiContext* ctx = IM_NEW(ImGuiContext)(shared_font_atlas);\n    if (GImGui == NULL)\n        SetCurrentContext(ctx);\n    Initialize(ctx);\n    return ctx;\n}\n\nvoid ImGui::DestroyContext(ImGuiContext* ctx)\n{\n    if (ctx == NULL)\n        ctx = GImGui;\n    Shutdown(ctx);\n    if (GImGui == ctx)\n        SetCurrentContext(NULL);\n    IM_DELETE(ctx);\n}\n\nImGuiIO& ImGui::GetIO()\n{\n    IM_ASSERT(GImGui != NULL && \"No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?\");\n    return GImGui->IO;\n}\n\nImGuiPlatformIO& ImGui::GetPlatformIO()\n{\n    IM_ASSERT(GImGui != NULL && \"No current context. Did you call ImGui::CreateContext() or ImGui::SetCurrentContext()?\");\n    return GImGui->PlatformIO;\n}\n\nImGuiStyle& ImGui::GetStyle()\n{\n    IM_ASSERT(GImGui != NULL && \"No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?\");\n    return GImGui->Style;\n}\n\n// Same value as passed to the old io.RenderDrawListsFn function. Valid after Render() and until the next call to NewFrame()\nImDrawData* ImGui::GetDrawData()\n{\n    ImGuiContext& g = *GImGui;\n    return g.Viewports[0]->DrawDataP.Valid ? &g.Viewports[0]->DrawDataP : NULL;\n}\n\ndouble ImGui::GetTime()\n{\n    return GImGui->Time;\n}\n\nint ImGui::GetFrameCount()\n{\n    return GImGui->FrameCount;\n}\n\nstatic ImDrawList* GetViewportDrawList(ImGuiViewportP* viewport, size_t drawlist_no, const char* drawlist_name)\n{\n    // Create the draw list on demand, because they are not frequently used for all viewports\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(drawlist_no >= 0 && drawlist_no < IM_ARRAYSIZE(viewport->DrawLists));\n    ImDrawList* draw_list = viewport->DrawLists[drawlist_no];\n    if (draw_list == NULL)\n    {\n        draw_list = IM_NEW(ImDrawList)(&g.DrawListSharedData);\n        draw_list->_OwnerName = drawlist_name;\n        viewport->DrawLists[drawlist_no] = draw_list;\n    }\n\n    // Our ImDrawList system requires that there is always a command\n    if (viewport->LastFrameDrawLists[drawlist_no] != g.FrameCount)\n    {\n        draw_list->Clear();\n        draw_list->PushTextureID(g.IO.Fonts->TexID);\n        draw_list->PushClipRect(viewport->Pos, viewport->Pos + viewport->Size, false);\n        draw_list->Flags = (g.Style.AntiAliasedLines ? ImDrawListFlags_AntiAliasedLines : 0) | (g.Style.AntiAliasedFill ? ImDrawListFlags_AntiAliasedFill : 0);\n        viewport->LastFrameDrawLists[drawlist_no] = g.FrameCount;\n    }\n    return draw_list;\n}\n\nImDrawList* ImGui::GetBackgroundDrawList(ImGuiViewport* viewport)\n{\n    return GetViewportDrawList((ImGuiViewportP*)viewport, 0, \"##Background\");\n}\n\nImDrawList* ImGui::GetBackgroundDrawList()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return GetBackgroundDrawList(window->Viewport);\n}\n\nImDrawList* ImGui::GetForegroundDrawList(ImGuiViewport* viewport)\n{\n    return GetViewportDrawList((ImGuiViewportP*)viewport, 1, \"##Foreground\");\n}\n\nImDrawList* ImGui::GetForegroundDrawList()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return GetForegroundDrawList(window->Viewport);\n}\n\nImDrawListSharedData* ImGui::GetDrawListSharedData()\n{\n    return &GImGui->DrawListSharedData;\n}\n\nvoid ImGui::StartMouseMovingWindow(ImGuiWindow* window)\n{\n    // Set ActiveId even if the _NoMove flag is set. Without it, dragging away from a window with _NoMove would activate hover on other windows.\n    // We _also_ call this when clicking in a window empty space when io.ConfigWindowsMoveFromTitleBarOnly is set, but clear g.MovingWindow afterward.\n    // This is because we want ActiveId to be set even when the window is not permitted to move.\n    ImGuiContext& g = *GImGui;\n    FocusWindow(window);\n    SetActiveID(window->MoveId, window);\n    g.NavDisableHighlight = true;\n    g.ActiveIdClickOffset = g.IO.MousePos - window->RootWindow->Pos;\n\n    bool can_move_window = true;\n    if ((window->Flags & ImGuiWindowFlags_NoMove) || (window->RootWindow->Flags & ImGuiWindowFlags_NoMove))\n        can_move_window = false;\n    if (ImGuiDockNode* node = window->DockNodeAsHost)\n        if (node->VisibleWindow && (node->VisibleWindow->Flags & ImGuiWindowFlags_NoMove))\n            can_move_window = false;\n    if (can_move_window)\n        g.MovingWindow = window;\n}\n\n// Handle mouse moving window\n// Note: moving window with the navigation keys (Square + d-pad / CTRL+TAB + Arrows) are processed in NavUpdateWindowing()\nvoid ImGui::UpdateMouseMovingWindowNewFrame()\n{\n    ImGuiContext& g = *GImGui;\n    if (g.MovingWindow != NULL)\n    {\n        // We actually want to move the root window. g.MovingWindow == window we clicked on (could be a child window).\n        // We track it to preserve Focus and so that generally ActiveIdWindow == MovingWindow and ActiveId == MovingWindow->MoveId for consistency.\n        KeepAliveID(g.ActiveId);\n        IM_ASSERT(g.MovingWindow && g.MovingWindow->RootWindow);\n        ImGuiWindow* moving_window = g.MovingWindow->RootWindow;\n        if (g.IO.MouseDown[0] && IsMousePosValid(&g.IO.MousePos))\n        {\n            ImVec2 pos = g.IO.MousePos - g.ActiveIdClickOffset;\n            if (moving_window->Pos.x != pos.x || moving_window->Pos.y != pos.y)\n            {\n                MarkIniSettingsDirty(moving_window);\n                SetWindowPos(moving_window, pos, ImGuiCond_Always);\n                if (moving_window->ViewportOwned) // Synchronize viewport immediately because some overlays may relies on clipping rectangle before we Begin() into the window.\n                    moving_window->Viewport->Pos = pos;\n            }\n            FocusWindow(g.MovingWindow);\n        }\n        else\n        {\n            // Try to merge the window back into the main viewport. \n            // This works because MouseViewport should be != MovingWindow->Viewport on release (as per code in UpdateViewports)\n            if (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)\n                UpdateTryMergeWindowIntoHostViewport(moving_window, g.MouseViewport);\n\n            // Restore the mouse viewport so that we don't hover the viewport _under_ the moved window during the frame we released the mouse button.\n            if (!IsDragDropPayloadBeingAccepted())\n                g.MouseViewport = moving_window->Viewport;\n\n            // Clear the NoInput window flag set by the Viewport system\n            moving_window->Viewport->Flags &= ~ImGuiViewportFlags_NoInputs;\n\n            ClearActiveID();\n            g.MovingWindow = NULL;\n        }\n    }\n    else\n    {\n        // When clicking/dragging from a window that has the _NoMove flag, we still set the ActiveId in order to prevent hovering others.\n        if (g.ActiveIdWindow && g.ActiveIdWindow->MoveId == g.ActiveId)\n        {\n            KeepAliveID(g.ActiveId);\n            if (!g.IO.MouseDown[0])\n                ClearActiveID();\n        }\n    }\n}\n\n// Initiate moving window, handle left-click and right-click focus\nvoid ImGui::UpdateMouseMovingWindowEndFrame()\n{\n    // Initiate moving window\n    ImGuiContext& g = *GImGui;\n    if (g.ActiveId != 0 || g.HoveredId != 0)\n        return;\n\n    // Unless we just made a window/popup appear\n    if (g.NavWindow && g.NavWindow->Appearing)\n        return;\n\n    // Click to focus window and start moving (after we're done with all our widgets)\n    if (g.IO.MouseClicked[0])\n    {\n        if (g.HoveredRootWindow != NULL)\n        {\n            StartMouseMovingWindow(g.HoveredWindow);\n            if (g.IO.ConfigWindowsMoveFromTitleBarOnly && (!(g.HoveredRootWindow->Flags & ImGuiWindowFlags_NoTitleBar) || g.HoveredWindow->RootWindowDockStop->DockIsActive))\n                if (!g.HoveredRootWindow->TitleBarRect().Contains(g.IO.MouseClickedPos[0]))\n                    g.MovingWindow = NULL;\n        }\n        else if (g.NavWindow != NULL && GetFrontMostPopupModal() == NULL)\n        {\n            // Clicking on void disable focus\n            FocusWindow(NULL);\n        }\n    }\n\n    // With right mouse button we close popups without changing focus based on where the mouse is aimed\n    // Instead, focus will be restored to the window under the bottom-most closed popup.\n    // (The left mouse button path calls FocusWindow on the hovered window, which will lead NewFrame->ClosePopupsOverWindow to trigger)\n    if (g.IO.MouseClicked[1])\n    {\n        // Find the top-most window between HoveredWindow and the front most Modal Window.\n        // This is where we can trim the popup stack.\n        ImGuiWindow* modal = GetFrontMostPopupModal();\n        bool hovered_window_above_modal = false;\n        if (modal == NULL)\n            hovered_window_above_modal = true;\n        for (int i = g.Windows.Size - 1; i >= 0 && hovered_window_above_modal == false; i--)\n        {\n            ImGuiWindow* window = g.Windows[i];\n            if (window == modal)\n                break;\n            if (window == g.HoveredWindow)\n                hovered_window_above_modal = true;\n        }\n        ClosePopupsOverWindow(hovered_window_above_modal ? g.HoveredWindow : modal, true);\n    }\n}\n\nstatic void TranslateWindow(ImGuiWindow* window, const ImVec2& delta)\n{\n    window->Pos += delta;\n    window->ClipRect.Translate(delta);\n    window->OuterRectClipped.Translate(delta);\n    window->InnerMainRect.Translate(delta);\n    window->DC.CursorPos += delta;\n    window->DC.CursorStartPos += delta;\n    window->DC.CursorMaxPos += delta;\n    window->DC.LastItemRect.Translate(delta);\n    window->DC.LastItemDisplayRect.Translate(delta);\n}\n\nstatic void ScaleWindow(ImGuiWindow* window, float scale)\n{\n    ImVec2 origin = window->Viewport->Pos;\n    window->Pos = ImFloor((window->Pos - origin) * scale + origin);\n    window->Size = ImFloor(window->Size * scale);\n    window->SizeFull = ImFloor(window->SizeFull * scale);\n    window->SizeContents = ImFloor(window->SizeContents * scale);\n}\n\nstatic bool IsWindowActiveAndVisible(ImGuiWindow* window)\n{\n    return (window->Active) && (!window->Hidden);\n}\n\nstatic void ImGui::UpdateMouseInputs()\n{\n    ImGuiContext& g = *GImGui;\n\n    // Round mouse position to avoid spreading non-rounded position (e.g. UpdateManualResize doesn't support them well)\n    if (IsMousePosValid(&g.IO.MousePos))\n        g.IO.MousePos = g.LastValidMousePos = ImFloor(g.IO.MousePos);\n\n    // If mouse just appeared or disappeared (usually denoted by -FLT_MAX components) we cancel out movement in MouseDelta\n    if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MousePosPrev))\n        g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev;\n    else\n        g.IO.MouseDelta = ImVec2(0.0f, 0.0f);\n    if (g.IO.MouseDelta.x != 0.0f || g.IO.MouseDelta.y != 0.0f)\n        g.NavDisableMouseHover = false;\n\n    g.IO.MousePosPrev = g.IO.MousePos;\n    for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++)\n    {\n        g.IO.MouseClicked[i] = g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] < 0.0f;\n        g.IO.MouseReleased[i] = !g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] >= 0.0f;\n        g.IO.MouseDownDurationPrev[i] = g.IO.MouseDownDuration[i];\n        g.IO.MouseDownDuration[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownDuration[i] < 0.0f ? 0.0f : g.IO.MouseDownDuration[i] + g.IO.DeltaTime) : -1.0f;\n        g.IO.MouseDoubleClicked[i] = false;\n        if (g.IO.MouseClicked[i])\n        {\n            if ((float)(g.Time - g.IO.MouseClickedTime[i]) < g.IO.MouseDoubleClickTime)\n            {\n                ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f);\n                if (ImLengthSqr(delta_from_click_pos) < g.IO.MouseDoubleClickMaxDist * g.IO.MouseDoubleClickMaxDist)\n                    g.IO.MouseDoubleClicked[i] = true;\n                g.IO.MouseClickedTime[i] = -FLT_MAX;    // so the third click isn't turned into a double-click\n            }\n            else\n            {\n                g.IO.MouseClickedTime[i] = g.Time;\n            }\n            g.IO.MouseClickedPos[i] = g.IO.MousePos;\n            g.IO.MouseDragMaxDistanceAbs[i] = ImVec2(0.0f, 0.0f);\n            g.IO.MouseDragMaxDistanceSqr[i] = 0.0f;\n        }\n        else if (g.IO.MouseDown[i])\n        {\n            // Maintain the maximum distance we reaching from the initial click position, which is used with dragging threshold\n            ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f);\n            g.IO.MouseDragMaxDistanceSqr[i] = ImMax(g.IO.MouseDragMaxDistanceSqr[i], ImLengthSqr(delta_from_click_pos));\n            g.IO.MouseDragMaxDistanceAbs[i].x = ImMax(g.IO.MouseDragMaxDistanceAbs[i].x, delta_from_click_pos.x < 0.0f ? -delta_from_click_pos.x : delta_from_click_pos.x);\n            g.IO.MouseDragMaxDistanceAbs[i].y = ImMax(g.IO.MouseDragMaxDistanceAbs[i].y, delta_from_click_pos.y < 0.0f ? -delta_from_click_pos.y : delta_from_click_pos.y);\n        }\n        if (g.IO.MouseClicked[i]) // Clicking any mouse button reactivate mouse hovering which may have been deactivated by gamepad/keyboard navigation\n            g.NavDisableMouseHover = false;\n    }\n}\n\nvoid ImGui::UpdateMouseWheel()\n{\n    ImGuiContext& g = *GImGui;\n    if (!g.HoveredWindow || g.HoveredWindow->Collapsed)\n        return;\n    if (g.IO.MouseWheel == 0.0f && g.IO.MouseWheelH == 0.0f)\n        return;\n    ImGuiWindow* window = g.HoveredWindow;\n\n    // Zoom / Scale window\n    // FIXME-OBSOLETE: This is an old feature, it still works but pretty much nobody is using it and may be best redesigned.\n    if (g.IO.MouseWheel != 0.0f && g.IO.KeyCtrl && g.IO.FontAllowUserScaling)\n    {\n        const float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f);\n        const float scale = new_font_scale / window->FontWindowScale;\n        window->FontWindowScale = new_font_scale;\n        if (!(window->Flags & ImGuiWindowFlags_ChildWindow))\n        {\n            const ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size;\n            window->Pos = ImFloor(window->Pos + offset);\n            window->Size = ImFloor(window->Size * scale);\n            window->SizeFull = ImFloor(window->SizeFull * scale);\n        }\n        return;\n    }\n\n    // Mouse wheel scrolling\n    // If a child window has the ImGuiWindowFlags_NoScrollWithMouse flag, we give a chance to scroll its parent (unless either ImGuiWindowFlags_NoInputs or ImGuiWindowFlags_NoScrollbar are also set).\n    while ((window->Flags & ImGuiWindowFlags_ChildWindow) && (window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs) && window->ParentWindow)\n        window = window->ParentWindow;\n    const bool scroll_allowed = !(window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs);\n    if (scroll_allowed && (g.IO.MouseWheel != 0.0f || g.IO.MouseWheelH != 0.0f) && !g.IO.KeyCtrl)\n    {\n        ImVec2 max_step = (window->ContentsRegionRect.GetSize() + window->WindowPadding * 2.0f) * 0.67f;\n\n        // Vertical Mouse Wheel Scrolling (hold Shift to scroll horizontally)\n        if (g.IO.MouseWheel != 0.0f && !g.IO.KeyShift)\n        {\n            float scroll_step = ImFloor(ImMin(5 * window->CalcFontSize(), max_step.y));\n            SetWindowScrollY(window, window->Scroll.y - g.IO.MouseWheel * scroll_step);\n        }\n        else if (g.IO.MouseWheel != 0.0f && g.IO.KeyShift)\n        {\n            float scroll_step = ImFloor(ImMin(2 * window->CalcFontSize(), max_step.x));\n            SetWindowScrollX(window, window->Scroll.x - g.IO.MouseWheel * scroll_step);\n        }\n\n        // Horizontal Mouse Wheel Scrolling (for hardware that supports it)\n        if (g.IO.MouseWheelH != 0.0f && !g.IO.KeyShift)\n        {\n            float scroll_step = ImFloor(ImMin(2 * window->CalcFontSize(), max_step.x));\n            SetWindowScrollX(window, window->Scroll.x - g.IO.MouseWheelH * scroll_step);\n        }\n    }\n}\n\n// The reason this is exposed in imgui_internal.h is: on touch-based system that don't have hovering, we want to dispatch inputs to the right target (imgui vs imgui+app)\nvoid ImGui::UpdateHoveredWindowAndCaptureFlags()\n{\n    ImGuiContext& g = *GImGui;\n\n    // Find the window hovered by mouse:\n    // - Child windows can extend beyond the limit of their parent so we need to derive HoveredRootWindow from HoveredWindow.\n    // - When moving a window we can skip the search, which also conveniently bypasses the fact that window->WindowRectClipped is lagging as this point of the frame.\n    // - We also support the moved window toggling the NoInputs flag after moving has started in order to be able to detect windows below it, which is useful for e.g. docking mechanisms.\n    FindHoveredWindow();\n    IM_ASSERT(g.HoveredWindow == NULL || g.HoveredWindow == g.MovingWindow || g.HoveredWindow->Viewport == g.MouseViewport);\n\n    // Modal windows prevents cursor from hovering behind them.\n    ImGuiWindow* modal_window = GetFrontMostPopupModal();\n    if (modal_window)\n        if (g.HoveredRootWindow && !IsWindowChildOf(g.HoveredRootWindow, modal_window))\n            g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL;\n\n    // Disabled mouse?\n    if (g.IO.ConfigFlags & ImGuiConfigFlags_NoMouse)\n        g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL;\n\n    // We track click ownership. When clicked outside of a window the click is owned by the application and won't report hovering nor request capture even while dragging over our windows afterward.\n    int mouse_earliest_button_down = -1;\n    bool mouse_any_down = false;\n    for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++)\n    {\n        if (g.IO.MouseClicked[i])\n            g.IO.MouseDownOwned[i] = (g.HoveredWindow != NULL) || (!g.OpenPopupStack.empty());\n        mouse_any_down |= g.IO.MouseDown[i];\n        if (g.IO.MouseDown[i])\n            if (mouse_earliest_button_down == -1 || g.IO.MouseClickedTime[i] < g.IO.MouseClickedTime[mouse_earliest_button_down])\n                mouse_earliest_button_down = i;\n    }\n    const bool mouse_avail_to_imgui = (mouse_earliest_button_down == -1) || g.IO.MouseDownOwned[mouse_earliest_button_down];\n\n    // If mouse was first clicked outside of ImGui bounds we also cancel out hovering.\n    // FIXME: For patterns of drag and drop across OS windows, we may need to rework/remove this test (first committed 311c0ca9 on 2015/02)\n    const bool mouse_dragging_extern_payload = g.DragDropActive && (g.DragDropSourceFlags & ImGuiDragDropFlags_SourceExtern) != 0;\n    if (!mouse_avail_to_imgui && !mouse_dragging_extern_payload)\n        g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL;\n\n    // Update io.WantCaptureMouse for the user application (true = dispatch mouse info to imgui, false = dispatch mouse info to imgui + app)\n    if (g.WantCaptureMouseNextFrame != -1)\n        g.IO.WantCaptureMouse = (g.WantCaptureMouseNextFrame != 0);\n    else\n        g.IO.WantCaptureMouse = (mouse_avail_to_imgui && (g.HoveredWindow != NULL || mouse_any_down)) || (!g.OpenPopupStack.empty());\n\n    // Update io.WantCaptureKeyboard for the user application (true = dispatch keyboard info to imgui, false = dispatch keyboard info to imgui + app)\n    if (g.WantCaptureKeyboardNextFrame != -1)\n        g.IO.WantCaptureKeyboard = (g.WantCaptureKeyboardNextFrame != 0);\n    else\n        g.IO.WantCaptureKeyboard = (g.ActiveId != 0) || (modal_window != NULL);\n    if (g.IO.NavActive && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && !(g.IO.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard))\n        g.IO.WantCaptureKeyboard = true;\n\n    // Update io.WantTextInput flag, this is to allow systems without a keyboard (e.g. mobile, hand-held) to show a software keyboard if possible\n    g.IO.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false;\n}\n\nvoid ImGui::NewFrame()\n{\n    IM_ASSERT(GImGui != NULL && \"No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?\");\n    ImGuiContext& g = *GImGui;\n\n#ifdef IMGUI_ENABLE_TEST_ENGINE\n    ImGuiTestEngineHook_PreNewFrame(&g);\n#endif\n\n    // Check user data\n    // (We pass an error message in the assert expression to make it visible to programmers who are not using a debugger, as most assert handlers display their argument)\n    IM_ASSERT(g.Initialized);\n    IM_ASSERT((g.IO.DeltaTime > 0.0f || g.FrameCount == 0)              && \"Need a positive DeltaTime!\");\n    IM_ASSERT((g.FrameCount == 0 || g.FrameCountEnded == g.FrameCount)  && \"Forgot to call Render() or EndFrame() at the end of the previous frame?\");\n    IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f  && \"Invalid DisplaySize value!\");\n    IM_ASSERT(g.IO.Fonts->Fonts.Size > 0                                && \"Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?\");\n    IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded()                          && \"Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?\");\n    IM_ASSERT(g.Style.CurveTessellationTol > 0.0f                       && \"Invalid style setting!\");\n    IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f            && \"Invalid style setting. Alpha cannot be negative (allows us to avoid a few clamps in color computations)!\");\n    IM_ASSERT(g.Style.WindowMinSize.x >= 1.0f && g.Style.WindowMinSize.y >= 1.0f && \"Invalid style setting.\");\n    for (int n = 0; n < ImGuiKey_COUNT; n++)\n        IM_ASSERT(g.IO.KeyMap[n] >= -1 && g.IO.KeyMap[n] < IM_ARRAYSIZE(g.IO.KeysDown) && \"io.KeyMap[] contains an out of bound value (need to be 0..512, or -1 for unmapped key)\");\n\n    // Perform simple check: required key mapping (we intentionally do NOT check all keys to not pressure user into setting up everything, but Space is required and was only recently added in 1.60 WIP)\n    if (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard)\n        IM_ASSERT(g.IO.KeyMap[ImGuiKey_Space] != -1 && \"ImGuiKey_Space is not mapped, required for keyboard navigation.\");\n\n    // Perform simple check: the beta io.ConfigWindowsResizeFromEdges option requires back-end to honor mouse cursor changes and set the ImGuiBackendFlags_HasMouseCursors flag accordingly.\n    if (g.IO.ConfigWindowsResizeFromEdges && !(g.IO.BackendFlags & ImGuiBackendFlags_HasMouseCursors))\n        g.IO.ConfigWindowsResizeFromEdges = false;\n\n    // Perform simple checks: multi-viewport and platform windows support\n    if (g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)\n    {\n        if ((g.IO.BackendFlags & ImGuiBackendFlags_PlatformHasViewports) && (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasViewports))\n        {\n            IM_ASSERT((g.FrameCount == 0 || g.FrameCount == g.FrameCountPlatformEnded) && \"Forgot to call UpdatePlatformWindows() in main loop after EndFrame()? Check examples/ applications for reference.\");\n            IM_ASSERT(g.PlatformIO.Platform_CreateWindow != NULL  && \"Platform init didn't install handlers?\");\n            IM_ASSERT(g.PlatformIO.Platform_DestroyWindow != NULL && \"Platform init didn't install handlers?\");\n            IM_ASSERT(g.PlatformIO.Platform_GetWindowPos != NULL  && \"Platform init didn't install handlers?\");\n            IM_ASSERT(g.PlatformIO.Platform_SetWindowPos != NULL  && \"Platform init didn't install handlers?\");\n            IM_ASSERT(g.PlatformIO.Platform_GetWindowSize != NULL  && \"Platform init didn't install handlers?\");\n            IM_ASSERT(g.PlatformIO.Platform_SetWindowSize != NULL  && \"Platform init didn't install handlers?\");\n            IM_ASSERT(g.PlatformIO.Monitors.Size > 0 && \"Platform init didn't setup Monitors list?\");\n            IM_ASSERT((g.Viewports[0]->PlatformUserData != NULL || g.Viewports[0]->PlatformHandle != NULL) && \"Platform init didn't setup main viewport.\");\n            if (g.IO.ConfigDockingTransparentPayload && (g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable))\n                IM_ASSERT(g.PlatformIO.Platform_SetWindowAlpha != NULL && \"Platform_SetWindowAlpha handler is required to use io.ConfigDockingTransparent!\");\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n            IM_ASSERT(g.IO.RenderDrawListsFn == NULL);  // Call ImGui::Render() then pass ImGui::GetDrawData() yourself to your render function!\n#endif\n        }\n        else\n        {\n            // Disable feature, our back-ends do not support it\n            g.IO.ConfigFlags &= ~ImGuiConfigFlags_ViewportsEnable;\n        }\n\n        // Perform simple checks on platform monitor data + compute a total bounding box for quick early outs\n        for (int monitor_n = 0; monitor_n < g.PlatformIO.Monitors.Size; monitor_n++)\n        {\n            ImGuiPlatformMonitor& mon = g.PlatformIO.Monitors[monitor_n];\n            IM_ASSERT(mon.MainSize.x > 0.0f && mon.MainSize.y > 0.0f && \"Monitor bounds not setup properly.\");\n            IM_ASSERT(mon.WorkSize.x > 0.0f && mon.WorkSize.y > 0.0f && \"Monitor bounds not setup properly. If you don't have work area information, just copy Min/Max into them.\");\n            IM_ASSERT(mon.DpiScale != 0.0f);\n        }\n    }\n\n    // Load settings on first frame (if not explicitly loaded manually before)\n    if (!g.SettingsLoaded)\n    {\n        IM_ASSERT(g.SettingsWindows.empty());\n        if (g.IO.IniFilename)\n            LoadIniSettingsFromDisk(g.IO.IniFilename);\n        g.SettingsLoaded = true;\n    }\n\n    // Save settings (with a delay after the last modification, so we don't spam disk too much)\n    if (g.SettingsDirtyTimer > 0.0f)\n    {\n        g.SettingsDirtyTimer -= g.IO.DeltaTime;\n        if (g.SettingsDirtyTimer <= 0.0f)\n        {\n            if (g.IO.IniFilename != NULL)\n                SaveIniSettingsToDisk(g.IO.IniFilename);\n            else\n                g.IO.WantSaveIniSettings = true;  // Let user know they can call SaveIniSettingsToMemory(). user will need to clear io.WantSaveIniSettings themselves.\n            g.SettingsDirtyTimer = 0.0f;\n        }\n    }\n\n    g.Time += g.IO.DeltaTime;\n    g.FrameScopeActive = true;\n    g.FrameCount += 1;\n    g.TooltipOverrideCount = 0;\n    g.WindowsActiveCount = 0;\n    g.ConfigFlagsForFrame = g.IO.ConfigFlags;\n\n    UpdateViewportsNewFrame();\n\n    // Setup current font and draw list shared data\n    // FIXME-VIEWPORT: the concept of a single ClipRectFullscreen is not ideal!\n    g.IO.Fonts->Locked = true;\n    SetCurrentFont(GetDefaultFont());\n    IM_ASSERT(g.Font->IsLoaded());\n    ImRect virtual_space(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX);\n    for (int n = 0; n < g.Viewports.Size; n++)\n        virtual_space.Add(g.Viewports[n]->GetRect());\n    g.DrawListSharedData.ClipRectFullscreen = ImVec4(virtual_space.Min.x, virtual_space.Min.y, virtual_space.Max.x, virtual_space.Max.y);\n    g.DrawListSharedData.CurveTessellationTol = g.Style.CurveTessellationTol;\n\n    // Mark rendering data as invalid to prevent user who may have a handle on it to use it.\n    for (int n = 0; n < g.Viewports.Size; n++)\n    {\n        ImGuiViewportP* viewport = g.Viewports[n];\n        viewport->DrawData = NULL;\n        viewport->DrawDataP.Clear();\n    }\n\n    // Drag and drop keep the source ID alive so even if the source disappear our state is consistent\n    if (g.DragDropActive && g.DragDropPayload.SourceId == g.ActiveId)\n        KeepAliveID(g.DragDropPayload.SourceId);\n\n    // Clear reference to active widget if the widget isn't alive anymore\n    if (!g.HoveredIdPreviousFrame)\n        g.HoveredIdTimer = 0.0f;\n    if (!g.HoveredIdPreviousFrame || (g.HoveredId && g.ActiveId == g.HoveredId))\n        g.HoveredIdNotActiveTimer = 0.0f;\n    if (g.HoveredId)\n        g.HoveredIdTimer += g.IO.DeltaTime;\n    if (g.HoveredId && g.ActiveId != g.HoveredId)\n        g.HoveredIdNotActiveTimer += g.IO.DeltaTime;\n    g.HoveredIdPreviousFrame = g.HoveredId;\n    g.HoveredId = 0;\n    g.HoveredIdAllowOverlap = false;\n    if (g.ActiveIdIsAlive != g.ActiveId && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0)\n        ClearActiveID();\n    if (g.ActiveId)\n        g.ActiveIdTimer += g.IO.DeltaTime;\n    g.LastActiveIdTimer += g.IO.DeltaTime;\n    g.ActiveIdPreviousFrame = g.ActiveId;\n    g.ActiveIdPreviousFrameWindow = g.ActiveIdWindow;\n    g.ActiveIdPreviousFrameHasBeenEdited = g.ActiveIdHasBeenEdited;\n    g.ActiveIdIsAlive = 0;\n    g.ActiveIdPreviousFrameIsAlive = false;\n    g.ActiveIdIsJustActivated = false;\n    if (g.TempInputTextId != 0 && g.ActiveId != g.TempInputTextId)\n        g.TempInputTextId = 0;\n\n    // Drag and drop\n    g.DragDropAcceptIdPrev = g.DragDropAcceptIdCurr;\n    g.DragDropAcceptIdCurr = 0;\n    g.DragDropAcceptIdCurrRectSurface = FLT_MAX;\n    g.DragDropWithinSourceOrTarget = false;\n\n    // Update keyboard input state\n    memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration));\n    for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++)\n        g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f;\n\n    // Update gamepad/keyboard directional navigation\n    NavUpdate();\n\n    // Update mouse input state\n    UpdateMouseInputs();\n\n    // Calculate frame-rate for the user, as a purely luxurious feature\n    g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx];\n    g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx] = g.IO.DeltaTime;\n    g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame);\n    g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame))) : FLT_MAX;\n\n    // Undocking\n    // (needs to be before UpdateMouseMovingWindowNewFrame so the window is already offset and following the mouse on the detaching frame)\n    DockContextUpdateUndocking(&g);\n\n    // Find hovered window\n    // (needs to be before UpdateMouseMovingWindowNewFrame so we fill g.HoveredWindowUnderMovingWindow on the mouse release frame)\n    UpdateHoveredWindowAndCaptureFlags();\n\n    // Handle user moving window with mouse (at the beginning of the frame to avoid input lag or sheering)\n    UpdateMouseMovingWindowNewFrame();\n\n    // Background darkening/whitening\n    if (GetFrontMostPopupModal() != NULL || (g.NavWindowingTarget != NULL && g.NavWindowingHighlightAlpha > 0.0f))\n        g.DimBgRatio = ImMin(g.DimBgRatio + g.IO.DeltaTime * 6.0f, 1.0f);\n    else\n        g.DimBgRatio = ImMax(g.DimBgRatio - g.IO.DeltaTime * 10.0f, 0.0f);\n\n    g.MouseCursor = ImGuiMouseCursor_Arrow;\n    g.WantCaptureMouseNextFrame = g.WantCaptureKeyboardNextFrame = g.WantTextInputNextFrame = -1;\n    g.PlatformImePos = ImVec2(1.0f, 1.0f); // OS Input Method Editor showing on top-left of our window by default\n    g.PlatformImePosViewport = NULL;\n\n    // Mouse wheel scrolling, scale\n    UpdateMouseWheel();\n\n    // Pressing TAB activate widget focus\n    g.FocusTabPressed = (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab));\n    if (g.ActiveId == 0 && g.FocusTabPressed)\n    {\n        // Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also\n        // manipulate the Next fields even, even though they will be turned into Curr fields by the code below.\n        g.FocusRequestNextWindow = g.NavWindow;\n        g.FocusRequestNextCounterAll = INT_MAX;\n        if (g.NavId != 0 && g.NavIdTabCounter != INT_MAX)\n            g.FocusRequestNextCounterTab = g.NavIdTabCounter + 1 + (g.IO.KeyShift ? -1 : 1);\n        else\n            g.FocusRequestNextCounterTab = g.IO.KeyShift ? -1 : 0;\n    }\n\n    // Turn queued focus request into current one\n    g.FocusRequestCurrWindow = NULL;\n    g.FocusRequestCurrCounterAll = g.FocusRequestCurrCounterTab = INT_MAX;\n    if (g.FocusRequestNextWindow != NULL)\n    {\n        ImGuiWindow* window = g.FocusRequestNextWindow;\n        g.FocusRequestCurrWindow = window;\n        if (g.FocusRequestNextCounterAll != INT_MAX && window->DC.FocusCounterAll != -1)\n            g.FocusRequestCurrCounterAll = ImModPositive(g.FocusRequestNextCounterAll, window->DC.FocusCounterAll + 1);\n        if (g.FocusRequestNextCounterTab != INT_MAX && window->DC.FocusCounterTab != -1)\n            g.FocusRequestCurrCounterTab = ImModPositive(g.FocusRequestNextCounterTab, window->DC.FocusCounterTab + 1);\n        g.FocusRequestNextWindow = NULL;\n        g.FocusRequestNextCounterAll = g.FocusRequestNextCounterTab = INT_MAX;\n    }\n\n    g.NavIdTabCounter = INT_MAX;\n\n    // Mark all windows as not visible\n    IM_ASSERT(g.WindowsFocusOrder.Size == g.Windows.Size);\n    for (int i = 0; i != g.Windows.Size; i++)\n    {\n        ImGuiWindow* window = g.Windows[i];\n        window->WasActive = window->Active;\n        window->BeginCount = 0;\n        window->Active = false;\n        window->WriteAccessed = false;\n    }\n\n    // Closing the focused window restore focus to the first active root window in descending z-order\n    if (g.NavWindow && !g.NavWindow->WasActive)\n        FocusTopMostWindowUnderOne(NULL, NULL);\n\n    // No window should be open at the beginning of the frame.\n    // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear.\n    g.CurrentWindowStack.resize(0);\n    g.BeginPopupStack.resize(0);\n    ClosePopupsOverWindow(g.NavWindow, false);\n\n    // Docking\n    DockContextUpdateDocking(&g);\n\n    // Create implicit/fallback window - which we will only render it if the user has added something to it.\n    // We don't use \"Debug\" to avoid colliding with user trying to create a \"Debug\" window with custom flags.\n    // This fallback is particularly important as it avoid ImGui:: calls from crashing.\n    SetNextWindowSize(ImVec2(400,400), ImGuiCond_FirstUseEver);\n    Begin(\"Debug##Default\");\n    g.FrameScopePushedImplicitWindow = true;\n\n#ifdef IMGUI_ENABLE_TEST_ENGINE\n    ImGuiTestEngineHook_PostNewFrame(&g);\n#endif\n}\n\nvoid ImGui::Initialize(ImGuiContext* context)\n{\n    ImGuiContext& g = *context;\n    IM_ASSERT(!g.Initialized && !g.SettingsLoaded);\n\n    // Add .ini handle for ImGuiWindow type\n    ImGuiSettingsHandler ini_handler;\n    ini_handler.TypeName = \"Window\";\n    ini_handler.TypeHash = ImHashStr(\"Window\");\n    ini_handler.ReadOpenFn = SettingsHandlerWindow_ReadOpen;\n    ini_handler.ReadLineFn = SettingsHandlerWindow_ReadLine;\n    ini_handler.WriteAllFn = SettingsHandlerWindow_WriteAll;\n    g.SettingsHandlers.push_back(ini_handler);\n\n    // Create default viewport\n    ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)();\n    viewport->ID = IMGUI_VIEWPORT_DEFAULT_ID;\n    viewport->Idx = 0;\n    viewport->PlatformWindowCreated = true;\n    g.Viewports.push_back(viewport);\n    g.PlatformIO.MainViewport = g.Viewports[0]; // Make it accessible in public-facing GetPlatformIO() immediately (before the first call to EndFrame)\n    g.PlatformIO.Viewports.push_back(g.Viewports[0]);\n\n    // Extensions\n    IM_ASSERT(g.DockContext == NULL);\n    DockContextInitialize(&g);\n\n    g.Initialized = true;\n}\n\n// This function is merely here to free heap allocations.\nvoid ImGui::Shutdown(ImGuiContext* context)\n{\n    // The fonts atlas can be used prior to calling NewFrame(), so we clear it even if g.Initialized is FALSE (which would happen if we never called NewFrame)\n    ImGuiContext& g = *context;\n    if (g.IO.Fonts && g.FontAtlasOwnedByContext)\n    {\n        g.IO.Fonts->Locked = false;\n        IM_DELETE(g.IO.Fonts);\n    }\n    g.IO.Fonts = NULL;\n\n    // Cleanup of other data are conditional on actually having initialized ImGui.\n    if (!g.Initialized)\n        return;\n\n    // Save settings (unless we haven't attempted to load them: CreateContext/DestroyContext without a call to NewFrame shouldn't save an empty file)\n    if (g.SettingsLoaded && g.IO.IniFilename != NULL)\n    {\n        ImGuiContext* backup_context = GImGui;\n        SetCurrentContext(context);\n        SaveIniSettingsToDisk(g.IO.IniFilename);\n        SetCurrentContext(backup_context);\n    }\n\n    // Destroy platform windows\n    ImGuiContext* backup_context = ImGui::GetCurrentContext();\n    SetCurrentContext(context);\n    DestroyPlatformWindows();\n    SetCurrentContext(backup_context);\n\n    // Shutdown extensions\n    IM_ASSERT(g.DockContext != NULL);\n    DockContextShutdown(&g);\n\n    // Clear everything else\n    for (int i = 0; i < g.Windows.Size; i++)\n        IM_DELETE(g.Windows[i]);\n    g.Windows.clear();\n    g.WindowsFocusOrder.clear();\n    g.WindowsSortBuffer.clear();\n    g.CurrentWindow = NULL;\n    g.CurrentWindowStack.clear();\n    g.WindowsById.Clear();\n    g.NavWindow = NULL;\n    g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL;\n    g.ActiveIdWindow = g.ActiveIdPreviousFrameWindow = NULL;\n    g.MovingWindow = NULL;\n    g.ColorModifiers.clear();\n    g.StyleModifiers.clear();\n    g.FontStack.clear();\n    g.OpenPopupStack.clear();\n    g.BeginPopupStack.clear();\n    g.CurrentViewport = g.MouseViewport = g.MouseLastHoveredViewport = NULL;\n    for (int i = 0; i < g.Viewports.Size; i++)\n        IM_DELETE(g.Viewports[i]);\n    g.Viewports.clear();\n    g.PrivateClipboard.clear();\n    g.InputTextState.ClearFreeMemory();\n\n    for (int i = 0; i < g.SettingsWindows.Size; i++)\n        IM_DELETE(g.SettingsWindows[i].Name);\n    g.SettingsWindows.clear();\n    g.SettingsHandlers.clear();\n\n    if (g.LogFile && g.LogFile != stdout)\n    {\n        fclose(g.LogFile);\n        g.LogFile = NULL;\n    }\n    g.LogBuffer.clear();\n\n    g.Initialized = false;\n}\n\n// FIXME: Add a more explicit sort order in the window structure.\nstatic int IMGUI_CDECL ChildWindowComparer(const void* lhs, const void* rhs)\n{\n    const ImGuiWindow* const a = *(const ImGuiWindow* const *)lhs;\n    const ImGuiWindow* const b = *(const ImGuiWindow* const *)rhs;\n    if (int d = (a->Flags & ImGuiWindowFlags_Popup) - (b->Flags & ImGuiWindowFlags_Popup))\n        return d;\n    if (int d = (a->Flags & ImGuiWindowFlags_Tooltip) - (b->Flags & ImGuiWindowFlags_Tooltip))\n        return d;\n    return (a->BeginOrderWithinParent - b->BeginOrderWithinParent);\n}\n\nstatic void AddWindowToSortBuffer(ImVector<ImGuiWindow*>* out_sorted_windows, ImGuiWindow* window)\n{\n    out_sorted_windows->push_back(window);\n    if (window->Active)\n    {\n        int count = window->DC.ChildWindows.Size;\n        if (count > 1)\n            ImQsort(window->DC.ChildWindows.begin(), (size_t)count, sizeof(ImGuiWindow*), ChildWindowComparer);\n        for (int i = 0; i < count; i++)\n        {\n            ImGuiWindow* child = window->DC.ChildWindows[i];\n            if (child->Active)\n                AddWindowToSortBuffer(out_sorted_windows, child);\n        }\n    }\n}\n\nstatic void AddDrawListToDrawData(ImVector<ImDrawList*>* out_list, ImDrawList* draw_list)\n{\n    if (draw_list->CmdBuffer.empty())\n        return;\n\n    // Remove trailing command if unused\n    ImDrawCmd& last_cmd = draw_list->CmdBuffer.back();\n    if (last_cmd.ElemCount == 0 && last_cmd.UserCallback == NULL)\n    {\n        draw_list->CmdBuffer.pop_back();\n        if (draw_list->CmdBuffer.empty())\n            return;\n    }\n\n    // Draw list sanity check. Detect mismatch between PrimReserve() calls and incrementing _VtxCurrentIdx, _VtxWritePtr etc. May trigger for you if you are using PrimXXX functions incorrectly.\n    IM_ASSERT(draw_list->VtxBuffer.Size == 0 || draw_list->_VtxWritePtr == draw_list->VtxBuffer.Data + draw_list->VtxBuffer.Size);\n    IM_ASSERT(draw_list->IdxBuffer.Size == 0 || draw_list->_IdxWritePtr == draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size);\n    IM_ASSERT((int)draw_list->_VtxCurrentIdx == draw_list->VtxBuffer.Size);\n\n    // Check that draw_list doesn't use more vertices than indexable (default ImDrawIdx = unsigned short = 2 bytes = 64K vertices per ImDrawList = per window)\n    // If this assert triggers because you are drawing lots of stuff manually:\n    // A) Make sure you are coarse clipping, because ImDrawList let all your vertices pass. You can use the Metrics window to inspect draw list contents.\n    // B) If you need/want meshes with more than 64K vertices, uncomment the '#define ImDrawIdx unsigned int' line in imconfig.h to set the index size to 4 bytes.\n    //    You'll need to handle the 4-bytes indices to your renderer. For example, the OpenGL example code detect index size at compile-time by doing:\n    //      glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset);\n    //    Your own engine or render API may use different parameters or function calls to specify index sizes. 2 and 4 bytes indices are generally supported by most API.\n    // C) If for some reason you cannot use 4 bytes indices or don't want to, a workaround is to call BeginChild()/EndChild() before reaching the 64K limit to split your draw commands in multiple draw lists.\n    if (sizeof(ImDrawIdx) == 2)\n        IM_ASSERT(draw_list->_VtxCurrentIdx < (1 << 16) && \"Too many vertices in ImDrawList using 16-bit indices. Read comment above\");\n\n    out_list->push_back(draw_list);\n}\n\nstatic void AddWindowToDrawData(ImGuiWindow* window, int layer)\n{\n    ImGuiContext& g = *GImGui;\n    g.IO.MetricsRenderWindows++;\n    AddDrawListToDrawData(&window->Viewport->DrawDataBuilder.Layers[layer], window->DrawList);\n    for (int i = 0; i < window->DC.ChildWindows.Size; i++)\n    {\n        ImGuiWindow* child = window->DC.ChildWindows[i];\n        if (IsWindowActiveAndVisible(child)) // Clipped children may have been marked not active\n            AddWindowToDrawData(child, layer);\n    }\n}\n\n// Layer is locked for the root window, however child windows may use a different viewport (e.g. extruding menu)\nstatic void AddRootWindowToDrawData(ImGuiWindow* window)\n{\n    int layer = (window->Flags & ImGuiWindowFlags_Tooltip) ? 1 : 0;\n    AddWindowToDrawData(window, layer);\n}\n\nvoid ImDrawDataBuilder::FlattenIntoSingleLayer()\n{\n    int n = Layers[0].Size;\n    int size = n;\n    for (int i = 1; i < IM_ARRAYSIZE(Layers); i++)\n        size += Layers[i].Size;\n    Layers[0].resize(size);\n    for (int layer_n = 1; layer_n < IM_ARRAYSIZE(Layers); layer_n++)\n    {\n        ImVector<ImDrawList*>& layer = Layers[layer_n];\n        if (layer.empty())\n            continue;\n        memcpy(&Layers[0][n], &layer[0], layer.Size * sizeof(ImDrawList*));\n        n += layer.Size;\n        layer.resize(0);\n    }\n}\n\nstatic void SetupViewportDrawData(ImGuiViewportP* viewport, ImVector<ImDrawList*>* draw_lists)\n{\n    ImDrawData* draw_data = &viewport->DrawDataP;\n    viewport->DrawData = draw_data; // Make publicly accessible\n    draw_data->Valid = true;\n    draw_data->CmdLists = (draw_lists->Size > 0) ? draw_lists->Data : NULL;\n    draw_data->CmdListsCount = draw_lists->Size;\n    draw_data->TotalVtxCount = draw_data->TotalIdxCount = 0;\n    draw_data->DisplayPos = viewport->Pos;\n    draw_data->DisplaySize = viewport->Size;\n    draw_data->FramebufferScale = ImGui::GetIO().DisplayFramebufferScale; // FIXME-VIEWPORT: This may vary on a per-monitor/viewport basis?\n    draw_data->OwnerViewport = viewport;\n    for (int n = 0; n < draw_lists->Size; n++)\n    {\n        draw_data->TotalVtxCount += draw_lists->Data[n]->VtxBuffer.Size;\n        draw_data->TotalIdxCount += draw_lists->Data[n]->IdxBuffer.Size;\n    }\n}\n\n// When using this function it is sane to ensure that float are perfectly rounded to integer values, to that e.g. (int)(max.x-min.x) in user's render produce correct result.\nvoid ImGui::PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DrawList->PushClipRect(clip_rect_min, clip_rect_max, intersect_with_current_clip_rect);\n    window->ClipRect = window->DrawList->_ClipRectStack.back();\n}\n\nvoid ImGui::PopClipRect()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DrawList->PopClipRect();\n    window->ClipRect = window->DrawList->_ClipRectStack.back();\n}\n\nstatic ImGuiWindow* FindFrontMostVisibleChildWindow(ImGuiWindow* window)\n{\n    for (int n = window->DC.ChildWindows.Size - 1; n >= 0; n--)\n        if (IsWindowActiveAndVisible(window->DC.ChildWindows[n]))\n            return FindFrontMostVisibleChildWindow(window->DC.ChildWindows[n]);\n    return window;\n}\n\nstatic void ImGui::EndFrameDrawDimmedBackgrounds()\n{\n    ImGuiContext& g = *GImGui;\n\n    // Draw modal whitening background on _other_ viewports than the one the modal is one\n    ImGuiWindow* modal_window = GetFrontMostPopupModal();\n    const bool dim_bg_for_modal = (modal_window != NULL);\n    const bool dim_bg_for_window_list = (g.NavWindowingTargetAnim != NULL);\n    if (dim_bg_for_modal || dim_bg_for_window_list)\n        for (int viewport_n = 0; viewport_n < g.Viewports.Size; viewport_n++)\n        {\n            ImGuiViewportP* viewport = g.Viewports[viewport_n];\n            if (modal_window && viewport == modal_window->Viewport)\n                continue;\n            if (g.NavWindowingList && viewport == g.NavWindowingList->Viewport)\n                continue;\n            if (g.NavWindowingTargetAnim && viewport == g.NavWindowingTargetAnim->Viewport)\n                continue;\n            ImDrawList* draw_list = GetForegroundDrawList(viewport);\n            const ImU32 dim_bg_col = GetColorU32(dim_bg_for_modal ? ImGuiCol_ModalWindowDimBg : ImGuiCol_NavWindowingDimBg, g.DimBgRatio);\n            draw_list->AddRectFilled(viewport->Pos, viewport->Pos + viewport->Size, dim_bg_col);\n        }\n\n    // Draw modal whitening background between CTRL-TAB list\n    if (dim_bg_for_window_list)\n    {\n        // Choose a draw list that will be front-most across all our children\n        ImGuiWindow* window = g.NavWindowingTargetAnim;\n        ImDrawList* draw_list = FindFrontMostVisibleChildWindow(window->RootWindow)->DrawList;\n        draw_list->PushClipRectFullScreen();\n\n        // Docking: draw modal whitening background on other nodes of a same dock tree\n        if (window->RootWindowDockStop->DockIsActive)\n            if (window->RootWindow != window->RootWindowDockStop)\n                RenderRectFilledWithHole(draw_list, window->RootWindow->Rect(), window->RootWindowDockStop->Rect(), GetColorU32(ImGuiCol_NavWindowingDimBg, g.DimBgRatio), g.Style.WindowRounding);\n\n        // Draw navigation selection/windowing rectangle border\n        float rounding = ImMax(window->WindowRounding, g.Style.WindowRounding);\n        ImRect bb = window->Rect();\n        bb.Expand(g.FontSize);\n        if (bb.Contains(window->Viewport->GetRect())) // If a window fits the entire viewport, adjust its highlight inward\n        {\n            bb.Expand(-g.FontSize - 1.0f);\n            rounding = window->WindowRounding;\n        }\n        draw_list->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha), rounding, ~0, 3.0f);\n        draw_list->PopClipRect();\n    }\n}\n\n// This is normally called by Render(). You may want to call it directly if you want to avoid calling Render() but the gain will be very minimal.\nvoid ImGui::EndFrame()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.Initialized);\n    if (g.FrameCountEnded == g.FrameCount)          // Don't process EndFrame() multiple times.\n        return;\n    IM_ASSERT(g.FrameScopeActive && \"Forgot to call ImGui::NewFrame()?\");\n\n    // Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME)\n    if (g.PlatformIO.Platform_SetImeInputPos && (g.PlatformImeLastPos.x == FLT_MAX || ImLengthSqr(g.PlatformImePos - g.PlatformImeLastPos) > 0.0001f))\n        if (g.PlatformImePosViewport && g.PlatformImePosViewport->PlatformWindowCreated)\n        {\n            g.PlatformIO.Platform_SetImeInputPos(g.PlatformImePosViewport, g.PlatformImePos);\n            g.PlatformImeLastPos = g.PlatformImePos;\n            g.PlatformImePosViewport = NULL;\n        }\n\n    // Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you\n    // to always call End/EndChild even if Begin/BeginChild returns false! (this is unfortunately inconsistent with most other Begin* API).\n    if (g.CurrentWindowStack.Size != 1)\n    {\n        if (g.CurrentWindowStack.Size > 1)\n        {\n            IM_ASSERT(g.CurrentWindowStack.Size == 1 && \"Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?\");\n            while (g.CurrentWindowStack.Size > 1) // FIXME-ERRORHANDLING\n                End();\n        }\n        else\n        {\n            IM_ASSERT(g.CurrentWindowStack.Size == 1 && \"Mismatched Begin/BeginChild vs End/EndChild calls: did you call End/EndChild too much?\");\n        }\n    }\n\n    // Hide implicit/fallback \"Debug\" window if it hasn't been used\n    g.FrameScopePushedImplicitWindow = false;\n    if (g.CurrentWindow && !g.CurrentWindow->WriteAccessed)\n        g.CurrentWindow->Active = false;\n    End();\n\n    // Draw modal whitening background on _other_ viewports than the one the modal is one\n    EndFrameDrawDimmedBackgrounds();\n\n    // Show CTRL+TAB list window\n    if (g.NavWindowingTarget)\n        NavUpdateWindowingList();\n\n    SetCurrentViewport(NULL, NULL);\n\n    // Drag and Drop: Elapse payload (if delivered, or if source stops being submitted)\n    if (g.DragDropActive)\n    {\n        bool is_delivered = g.DragDropPayload.Delivery;\n        bool is_elapsed = (g.DragDropPayload.DataFrameCount + 1 < g.FrameCount) && ((g.DragDropSourceFlags & ImGuiDragDropFlags_SourceAutoExpirePayload) || !IsMouseDown(g.DragDropMouseButton));\n        if (is_delivered || is_elapsed)\n            ClearDragDrop();\n    }\n\n    // Drag and Drop: Fallback for source tooltip. This is not ideal but better than nothing.\n    if (g.DragDropActive && g.DragDropSourceFrameCount < g.FrameCount)\n    {\n        g.DragDropWithinSourceOrTarget = true;\n        SetTooltip(\"...\");\n        g.DragDropWithinSourceOrTarget = false;\n    }\n\n    // End frame\n    g.FrameScopeActive = false;\n    g.FrameCountEnded = g.FrameCount;\n\n    // Initiate moving window + handle left-click and right-click focus\n    UpdateMouseMovingWindowEndFrame();\n\n    // Update user-facing viewport list (g.Viewports -> g.PlatformIO.Viewports after filtering out some)\n    UpdateViewportsEndFrame();\n\n    // Sort the window list so that all child windows are after their parent\n    // We cannot do that on FocusWindow() because childs may not exist yet\n    g.WindowsSortBuffer.resize(0);\n    g.WindowsSortBuffer.reserve(g.Windows.Size);\n    for (int i = 0; i != g.Windows.Size; i++)\n    {\n        ImGuiWindow* window = g.Windows[i];\n        if (window->Active && (window->Flags & ImGuiWindowFlags_ChildWindow))       // if a child is active its parent will add it\n            continue;\n        AddWindowToSortBuffer(&g.WindowsSortBuffer, window);\n    }\n\n    // This usually assert if there is a mismatch between the ImGuiWindowFlags_ChildWindow / ParentWindow values and DC.ChildWindows[] in parents, aka we've done something wrong.\n    IM_ASSERT(g.Windows.Size == g.WindowsSortBuffer.Size);\n    g.Windows.swap(g.WindowsSortBuffer);\n    g.IO.MetricsActiveWindows = g.WindowsActiveCount;\n\n    // Unlock font atlas\n    g.IO.Fonts->Locked = false;\n\n    // Clear Input data for next frame\n    g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f;\n    g.IO.InputQueueCharacters.resize(0);\n    memset(g.IO.NavInputs, 0, sizeof(g.IO.NavInputs));\n}\n\nvoid ImGui::Render()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.Initialized);\n\n    if (g.FrameCountEnded != g.FrameCount)\n        EndFrame();\n    g.FrameCountRendered = g.FrameCount;\n\n    // Gather ImDrawList to render (for each active window)\n    g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = g.IO.MetricsRenderWindows = 0;\n    for (int n = 0; n != g.Viewports.Size; n++)\n    {\n        ImGuiViewportP* viewport = g.Viewports[n];\n        viewport->DrawDataBuilder.Clear();\n        if (viewport->DrawLists[0] != NULL)\n            AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetBackgroundDrawList(viewport));\n    }\n\n    ImGuiWindow* windows_to_render_front_most[2];\n    windows_to_render_front_most[0] = (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) ? g.NavWindowingTarget->RootWindow : NULL;\n    windows_to_render_front_most[1] = g.NavWindowingTarget ? g.NavWindowingList : NULL;\n    for (int n = 0; n != g.Windows.Size; n++)\n    {\n        ImGuiWindow* window = g.Windows[n];\n        if (IsWindowActiveAndVisible(window) && (window->Flags & ImGuiWindowFlags_ChildWindow) == 0 && window != windows_to_render_front_most[0] && window != windows_to_render_front_most[1])\n            AddRootWindowToDrawData(window);\n    }\n    for (int n = 0; n < IM_ARRAYSIZE(windows_to_render_front_most); n++)\n        if (windows_to_render_front_most[n] && IsWindowActiveAndVisible(windows_to_render_front_most[n])) // NavWindowingTarget is always temporarily displayed as the front-most window\n            AddRootWindowToDrawData(windows_to_render_front_most[n]);\n\n    // Draw software mouse cursor if requested\n    if (g.IO.MouseDrawCursor)\n        RenderMouseCursor(g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor);\n\n    // Setup ImDrawData structures for end-user\n    g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = 0;\n    for (int n = 0; n < g.Viewports.Size; n++)\n    {\n        ImGuiViewportP* viewport = g.Viewports[n];\n        viewport->DrawDataBuilder.FlattenIntoSingleLayer();\n        if (viewport->DrawLists[1] != NULL)\n            AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetForegroundDrawList(viewport));\n        SetupViewportDrawData(viewport, &viewport->DrawDataBuilder.Layers[0]);\n        g.IO.MetricsRenderVertices += viewport->DrawData->TotalVtxCount;\n        g.IO.MetricsRenderIndices += viewport->DrawData->TotalIdxCount;\n    }\n\n    // (Legacy) Call the Render callback function. The current prefer way is to let the user retrieve GetDrawData() and call the render function themselves.\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    if (g.Viewports[0]->DrawData->CmdListsCount > 0 && g.IO.RenderDrawListsFn != NULL)\n        g.IO.RenderDrawListsFn(g.Viewports[0]->DrawData);\n#endif\n}\n\n// Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker.\n// CalcTextSize(\"\") should return ImVec2(0.0f, GImGui->FontSize)\nImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_text_after_double_hash, float wrap_width)\n{\n    ImGuiContext& g = *GImGui;\n\n    const char* text_display_end;\n    if (hide_text_after_double_hash)\n        text_display_end = FindRenderedTextEnd(text, text_end);      // Hide anything after a '##' string\n    else\n        text_display_end = text_end;\n\n    ImFont* font = g.Font;\n    const float font_size = g.FontSize;\n    if (text == text_display_end)\n        return ImVec2(0.0f, font_size);\n    ImVec2 text_size = font->CalcTextSizeA(font_size, FLT_MAX, wrap_width, text, text_display_end, NULL);\n\n    // Round\n    text_size.x = (float)(int)(text_size.x + 0.95f);\n\n    return text_size;\n}\n\n// Helper to calculate coarse clipping of large list of evenly sized items.\n// NB: Prefer using the ImGuiListClipper higher-level helper if you can! Read comments and instructions there on how those use this sort of pattern.\n// NB: 'items_count' is only used to clamp the result, if you don't know your count you can use INT_MAX\nvoid ImGui::CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (g.LogEnabled)\n    {\n        // If logging is active, do not perform any clipping\n        *out_items_display_start = 0;\n        *out_items_display_end = items_count;\n        return;\n    }\n    if (window->SkipItems)\n    {\n        *out_items_display_start = *out_items_display_end = 0;\n        return;\n    }\n\n    // We create the union of the ClipRect and the NavScoringRect which at worst should be 1 page away from ClipRect\n    ImRect unclipped_rect = window->ClipRect;\n    if (g.NavMoveRequest)\n        unclipped_rect.Add(g.NavScoringRectScreen);\n\n    const ImVec2 pos = window->DC.CursorPos;\n    int start = (int)((unclipped_rect.Min.y - pos.y) / items_height);\n    int end = (int)((unclipped_rect.Max.y - pos.y) / items_height);\n\n    // When performing a navigation request, ensure we have one item extra in the direction we are moving to\n    if (g.NavMoveRequest && g.NavMoveClipDir == ImGuiDir_Up)\n        start--;\n    if (g.NavMoveRequest && g.NavMoveClipDir == ImGuiDir_Down)\n        end++;\n\n    start = ImClamp(start, 0, items_count);\n    end = ImClamp(end + 1, start, items_count);\n    *out_items_display_start = start;\n    *out_items_display_end = end;\n}\n\n// Find window given position, search front-to-back\n// FIXME: Note that we have an inconsequential lag here: OuterRectClipped is updated in Begin(), so windows moved programatically\n// with SetWindowPos() and not SetNextWindowPos() will have that rectangle lagging by a frame at the time FindHoveredWindow() is\n// called, aka before the next Begin(). Moving window isn't affected.\nstatic void FindHoveredWindow()\n{\n    ImGuiContext& g = *GImGui;\n\n    // Special handling for the window being moved: Ignore the mouse viewport check (because it may reset/lose its viewport during the undocking frame)\n    ImGuiViewportP* moving_window_viewport = g.MovingWindow ? g.MovingWindow->Viewport : NULL;\n    if (g.MovingWindow)\n        g.MovingWindow->Viewport = g.MouseViewport;\n\n    ImGuiWindow* hovered_window = NULL;\n    ImGuiWindow* hovered_window_ignoring_moving_window = NULL;\n    if (g.MovingWindow && !(g.MovingWindow->Flags & ImGuiWindowFlags_NoMouseInputs))\n        hovered_window = g.MovingWindow;\n\n    ImVec2 padding_regular = g.Style.TouchExtraPadding;\n    ImVec2 padding_for_resize_from_edges = g.IO.ConfigWindowsResizeFromEdges ? ImMax(g.Style.TouchExtraPadding, ImVec2(WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS, WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS)) : padding_regular;\n    for (int i = g.Windows.Size - 1; i >= 0; i--)\n    {\n        ImGuiWindow* window = g.Windows[i];\n        if (!window->Active || window->Hidden)\n            continue;\n        if (window->Flags & ImGuiWindowFlags_NoMouseInputs)\n            continue;\n        IM_ASSERT(window->Viewport);\n        if (window->Viewport != g.MouseViewport)\n            continue;\n\n        // Using the clipped AABB, a child window will typically be clipped by its parent (not always)\n        ImRect bb(window->OuterRectClipped);\n        if (window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize))\n            bb.Expand(padding_regular);\n        else\n            bb.Expand(padding_for_resize_from_edges);\n        if (!bb.Contains(g.IO.MousePos))\n            continue;\n\n        if (window->HitTestHoleSize.x != 0)\n        {\n            // FIXME: Consider generalizing hit-testing override (with more generic data, callback, etc.) (#1512)\n            ImRect hole_bb((float)(window->HitTestHoleOffset.x), (float)(window->HitTestHoleOffset.y),\n                (float)(window->HitTestHoleOffset.x + window->HitTestHoleSize.x), (float)(window->HitTestHoleOffset.y + window->HitTestHoleSize.y));\n            if (hole_bb.Contains(g.IO.MousePos - window->Pos))\n                continue;\n        }\n        \n        if (hovered_window == NULL)\n            hovered_window = window;\n        if (hovered_window_ignoring_moving_window == NULL && (!g.MovingWindow || window->RootWindow != g.MovingWindow->RootWindow))\n            hovered_window_ignoring_moving_window = window;\n        if (hovered_window && hovered_window_ignoring_moving_window)\n            break;\n    }\n\n    g.HoveredWindow = hovered_window;\n    g.HoveredRootWindow = g.HoveredWindow ? g.HoveredWindow->RootWindow : NULL;\n    g.HoveredWindowUnderMovingWindow = hovered_window_ignoring_moving_window;\n\n    if (g.MovingWindow)\n        g.MovingWindow->Viewport = moving_window_viewport;\n}\n\n// Test if mouse cursor is hovering given rectangle\n// NB- Rectangle is clipped by our current clip setting\n// NB- Expand the rectangle to be generous on imprecise inputs systems (g.Style.TouchExtraPadding)\nbool ImGui::IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip)\n{\n    ImGuiContext& g = *GImGui;\n\n    // Clip\n    ImRect rect_clipped(r_min, r_max);\n    if (clip)\n        rect_clipped.ClipWith(g.CurrentWindow->ClipRect);\n\n    // Expand for touch input\n    const ImRect rect_for_touch(rect_clipped.Min - g.Style.TouchExtraPadding, rect_clipped.Max + g.Style.TouchExtraPadding);\n    if (!rect_for_touch.Contains(g.IO.MousePos))\n        return false;\n    if (!g.MouseViewport->GetRect().Overlaps(rect_clipped))\n        return false;\n    return true;\n}\n\nint ImGui::GetKeyIndex(ImGuiKey imgui_key)\n{\n    IM_ASSERT(imgui_key >= 0 && imgui_key < ImGuiKey_COUNT);\n    return GImGui->IO.KeyMap[imgui_key];\n}\n\n// Note that imgui doesn't know the semantic of each entry of io.KeysDown[]. Use your own indices/enums according to how your back-end/engine stored them into io.KeysDown[]!\nbool ImGui::IsKeyDown(int user_key_index)\n{\n    if (user_key_index < 0) return false;\n    IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(GImGui->IO.KeysDown));\n    return GImGui->IO.KeysDown[user_key_index];\n}\n\nint ImGui::CalcTypematicPressedRepeatAmount(float t, float t_prev, float repeat_delay, float repeat_rate)\n{\n    if (t == 0.0f)\n        return 1;\n    if (t <= repeat_delay || repeat_rate <= 0.0f)\n        return 0;\n    const int count = (int)((t - repeat_delay) / repeat_rate) - (int)((t_prev - repeat_delay) / repeat_rate);\n    return (count > 0) ? count : 0;\n}\n\nint ImGui::GetKeyPressedAmount(int key_index, float repeat_delay, float repeat_rate)\n{\n    ImGuiContext& g = *GImGui;\n    if (key_index < 0)\n        return 0;\n    IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown));\n    const float t = g.IO.KeysDownDuration[key_index];\n    return CalcTypematicPressedRepeatAmount(t, t - g.IO.DeltaTime, repeat_delay, repeat_rate);\n}\n\nbool ImGui::IsKeyPressed(int user_key_index, bool repeat)\n{\n    ImGuiContext& g = *GImGui;\n    if (user_key_index < 0)\n        return false;\n    IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown));\n    const float t = g.IO.KeysDownDuration[user_key_index];\n    if (t == 0.0f)\n        return true;\n    if (repeat && t > g.IO.KeyRepeatDelay)\n        return GetKeyPressedAmount(user_key_index, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0;\n    return false;\n}\n\nbool ImGui::IsKeyReleased(int user_key_index)\n{\n    ImGuiContext& g = *GImGui;\n    if (user_key_index < 0) return false;\n    IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown));\n    return g.IO.KeysDownDurationPrev[user_key_index] >= 0.0f && !g.IO.KeysDown[user_key_index];\n}\n\nbool ImGui::IsMouseDown(int button)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));\n    return g.IO.MouseDown[button];\n}\n\nbool ImGui::IsAnyMouseDown()\n{\n    ImGuiContext& g = *GImGui;\n    for (int n = 0; n < IM_ARRAYSIZE(g.IO.MouseDown); n++)\n        if (g.IO.MouseDown[n])\n            return true;\n    return false;\n}\n\nbool ImGui::IsMouseClicked(int button, bool repeat)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));\n    const float t = g.IO.MouseDownDuration[button];\n    if (t == 0.0f)\n        return true;\n\n    if (repeat && t > g.IO.KeyRepeatDelay)\n    {\n        float delay = g.IO.KeyRepeatDelay, rate = g.IO.KeyRepeatRate;\n        if ((ImFmod(t - delay, rate) > rate*0.5f) != (ImFmod(t - delay - g.IO.DeltaTime, rate) > rate*0.5f))\n            return true;\n    }\n\n    return false;\n}\n\nbool ImGui::IsMouseReleased(int button)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));\n    return g.IO.MouseReleased[button];\n}\n\nbool ImGui::IsMouseDoubleClicked(int button)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));\n    return g.IO.MouseDoubleClicked[button];\n}\n\nbool ImGui::IsMouseDragging(int button, float lock_threshold)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));\n    if (!g.IO.MouseDown[button])\n        return false;\n    if (lock_threshold < 0.0f)\n        lock_threshold = g.IO.MouseDragThreshold;\n    return g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold;\n}\n\nImVec2 ImGui::GetMousePos()\n{\n    return GImGui->IO.MousePos;\n}\n\n// NB: prefer to call right after BeginPopup(). At the time Selectable/MenuItem is activated, the popup is already closed!\nImVec2 ImGui::GetMousePosOnOpeningCurrentPopup()\n{\n    ImGuiContext& g = *GImGui;\n    if (g.BeginPopupStack.Size > 0)\n        return g.OpenPopupStack[g.BeginPopupStack.Size-1].OpenMousePos;\n    return g.IO.MousePos;\n}\n\n// We typically use ImVec2(-FLT_MAX,-FLT_MAX) to denote an invalid mouse position.\nbool ImGui::IsMousePosValid(const ImVec2* mouse_pos)\n{\n    // The assert is only to silence a false-positive in XCode Static Analysis.\n    // Because GImGui is not dereferenced in every code path, the static analyzer assume that it may be NULL (which it doesn't for other functions).\n    IM_ASSERT(GImGui != NULL);\n    const float MOUSE_INVALID = -256000.0f;\n    ImVec2 p = mouse_pos ? *mouse_pos : GImGui->IO.MousePos;\n    return p.x >= MOUSE_INVALID && p.y >= MOUSE_INVALID;\n}\n\n// Return the delta from the initial clicking position while the mouse button is clicked or was just released.\n// This is locked and return 0.0f until the mouse moves past a distance threshold at least once.\n// NB: This is only valid if IsMousePosValid(). Back-ends in theory should always keep mouse position valid when dragging even outside the client window.\nImVec2 ImGui::GetMouseDragDelta(int button, float lock_threshold)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));\n    if (lock_threshold < 0.0f)\n        lock_threshold = g.IO.MouseDragThreshold;\n    if (g.IO.MouseDown[button] || g.IO.MouseReleased[button])\n        if (g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold)\n            if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MouseClickedPos[button]))\n                return g.IO.MousePos - g.IO.MouseClickedPos[button];\n    return ImVec2(0.0f, 0.0f);\n}\n\nvoid ImGui::ResetMouseDragDelta(int button)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown));\n    // NB: We don't need to reset g.IO.MouseDragMaxDistanceSqr\n    g.IO.MouseClickedPos[button] = g.IO.MousePos;\n}\n\nImGuiMouseCursor ImGui::GetMouseCursor()\n{\n    return GImGui->MouseCursor;\n}\n\nvoid ImGui::SetMouseCursor(ImGuiMouseCursor cursor_type)\n{\n    GImGui->MouseCursor = cursor_type;\n}\n\nvoid ImGui::CaptureKeyboardFromApp(bool capture)\n{\n    GImGui->WantCaptureKeyboardNextFrame = capture ? 1 : 0;\n}\n\nvoid ImGui::CaptureMouseFromApp(bool capture)\n{\n    GImGui->WantCaptureMouseNextFrame = capture ? 1 : 0;\n}\n\nbool ImGui::IsItemActive()\n{\n    ImGuiContext& g = *GImGui;\n    if (g.ActiveId)\n    {\n        ImGuiWindow* window = g.CurrentWindow;\n        return g.ActiveId == window->DC.LastItemId;\n    }\n    return false;\n}\n\nbool ImGui::IsItemActivated()\n{\n    ImGuiContext& g = *GImGui;\n    if (g.ActiveId)\n    {\n        ImGuiWindow* window = g.CurrentWindow;\n        if (g.ActiveId == window->DC.LastItemId && g.ActiveIdPreviousFrame != window->DC.LastItemId)\n            return true;\n    }\n    return false;\n}\n\nbool ImGui::IsItemDeactivated()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    return (g.ActiveIdPreviousFrame == window->DC.LastItemId && g.ActiveIdPreviousFrame != 0 && g.ActiveId != window->DC.LastItemId);\n}\n\nbool ImGui::IsItemDeactivatedAfterEdit()\n{\n    ImGuiContext& g = *GImGui;\n    return IsItemDeactivated() && (g.ActiveIdPreviousFrameHasBeenEdited || (g.ActiveId == 0 && g.ActiveIdHasBeenEdited));\n}\n\nbool ImGui::IsItemFocused()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    if (g.NavId == 0 || g.NavDisableHighlight || g.NavId != window->DC.LastItemId)\n        return false;\n\n    // Special handling for the dummy item after Begin() which represent the title bar or tab. \n    // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case.\n    if (window->DC.LastItemId == window->ID && window->WriteAccessed)\n        return false;\n\n    return true;\n}\n\nbool ImGui::IsItemClicked(int mouse_button)\n{\n    return IsMouseClicked(mouse_button) && IsItemHovered(ImGuiHoveredFlags_None);\n}\n\nbool ImGui::IsItemToggledSelection()\n{\n    ImGuiContext& g = *GImGui;\n    return (g.CurrentWindow->DC.LastItemStatusFlags & ImGuiItemStatusFlags_ToggledSelection) ? true : false;\n}\n\nbool ImGui::IsAnyItemHovered()\n{\n    ImGuiContext& g = *GImGui;\n    return g.HoveredId != 0 || g.HoveredIdPreviousFrame != 0;\n}\n\nbool ImGui::IsAnyItemActive()\n{\n    ImGuiContext& g = *GImGui;\n    return g.ActiveId != 0;\n}\n\nbool ImGui::IsAnyItemFocused()\n{\n    ImGuiContext& g = *GImGui;\n    return g.NavId != 0 && !g.NavDisableHighlight;\n}\n\nbool ImGui::IsItemVisible()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->ClipRect.Overlaps(window->DC.LastItemRect);\n}\n\nbool ImGui::IsItemEdited()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_Edited) != 0;\n}\n\n// Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority.\nvoid ImGui::SetItemAllowOverlap()\n{\n    ImGuiContext& g = *GImGui;\n    if (g.HoveredId == g.CurrentWindow->DC.LastItemId)\n        g.HoveredIdAllowOverlap = true;\n    if (g.ActiveId == g.CurrentWindow->DC.LastItemId)\n        g.ActiveIdAllowOverlap = true;\n}\n\nImVec2 ImGui::GetItemRectMin()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.LastItemRect.Min;\n}\n\nImVec2 ImGui::GetItemRectMax()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.LastItemRect.Max;\n}\n\nImVec2 ImGui::GetItemRectSize()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.LastItemRect.GetSize();\n}\n\nstatic bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* parent_window = g.CurrentWindow;\n\n    flags |= ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_ChildWindow|ImGuiWindowFlags_NoDocking;\n    flags |= (parent_window->Flags & ImGuiWindowFlags_NoMove);  // Inherit the NoMove flag\n\n    // Size\n    const ImVec2 content_avail = GetContentRegionAvail();\n    ImVec2 size = ImFloor(size_arg);\n    const int auto_fit_axises = ((size.x == 0.0f) ? (1 << ImGuiAxis_X) : 0x00) | ((size.y == 0.0f) ? (1 << ImGuiAxis_Y) : 0x00);\n    if (size.x <= 0.0f)\n        size.x = ImMax(content_avail.x + size.x, 4.0f); // Arbitrary minimum child size (0.0f causing too much issues)\n    if (size.y <= 0.0f)\n        size.y = ImMax(content_avail.y + size.y, 4.0f);\n    SetNextWindowSize(size);\n\n    // Build up name. If you need to append to a same child from multiple location in the ID stack, use BeginChild(ImGuiID id) with a stable value.\n    char title[256];\n    if (name)\n        ImFormatString(title, IM_ARRAYSIZE(title), \"%s/%s_%08X\", parent_window->Name, name, id);\n    else\n        ImFormatString(title, IM_ARRAYSIZE(title), \"%s/%08X\", parent_window->Name, id);\n\n    const float backup_border_size = g.Style.ChildBorderSize;\n    if (!border)\n        g.Style.ChildBorderSize = 0.0f;\n    bool ret = Begin(title, NULL, flags);\n    g.Style.ChildBorderSize = backup_border_size;\n\n    ImGuiWindow* child_window = g.CurrentWindow;\n    child_window->ChildId = id;\n    child_window->AutoFitChildAxises = auto_fit_axises;\n\n    // Set the cursor to handle case where the user called SetNextWindowPos()+BeginChild() manually.\n    // While this is not really documented/defined, it seems that the expected thing to do.\n    if (child_window->BeginCount == 1)\n        parent_window->DC.CursorPos = child_window->Pos;\n\n    // Process navigation-in immediately so NavInit can run on first frame\n    if (g.NavActivateId == id && !(flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayerActiveMask != 0 || child_window->DC.NavHasScroll))\n    {\n        FocusWindow(child_window);\n        NavInitWindow(child_window, false);\n        SetActiveID(id+1, child_window); // Steal ActiveId with a dummy id so that key-press won't activate child item\n        g.ActiveIdSource = ImGuiInputSource_Nav;\n    }\n    return ret;\n}\n\nbool ImGui::BeginChild(const char* str_id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    return BeginChildEx(str_id, window->GetID(str_id), size_arg, border, extra_flags);\n}\n\nbool ImGui::BeginChild(ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags)\n{\n    IM_ASSERT(id != 0);\n    return BeginChildEx(NULL, id, size_arg, border, extra_flags);\n}\n\nvoid ImGui::EndChild()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    IM_ASSERT(window->Flags & ImGuiWindowFlags_ChildWindow);   // Mismatched BeginChild()/EndChild() callss\n    if (window->BeginCount > 1)\n    {\n        End();\n    }\n    else\n    {\n        ImVec2 sz = window->Size;\n        if (window->AutoFitChildAxises & (1 << ImGuiAxis_X)) // Arbitrary minimum zero-ish child size of 4.0f causes less trouble than a 0.0f\n            sz.x = ImMax(4.0f, sz.x);\n        if (window->AutoFitChildAxises & (1 << ImGuiAxis_Y))\n            sz.y = ImMax(4.0f, sz.y);\n        End();\n\n        ImGuiWindow* parent_window = g.CurrentWindow;\n        ImRect bb(parent_window->DC.CursorPos, parent_window->DC.CursorPos + sz);\n        ItemSize(sz);\n        if ((window->DC.NavLayerActiveMask != 0 || window->DC.NavHasScroll) && !(window->Flags & ImGuiWindowFlags_NavFlattened))\n        {\n            ItemAdd(bb, window->ChildId);\n            RenderNavHighlight(bb, window->ChildId);\n\n            // When browsing a window that has no activable items (scroll only) we keep a highlight on the child\n            if (window->DC.NavLayerActiveMask == 0 && window == g.NavWindow)\n                RenderNavHighlight(ImRect(bb.Min - ImVec2(2,2), bb.Max + ImVec2(2,2)), g.NavId, ImGuiNavHighlightFlags_TypeThin);\n        }\n        else\n        {\n            // Not navigable into\n            ItemAdd(bb, 0);\n        }\n    }\n}\n\n// Helper to create a child window / scrolling region that looks like a normal widget frame.\nbool ImGui::BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags extra_flags)\n{\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]);\n    PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding);\n    PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize);\n    PushStyleVar(ImGuiStyleVar_WindowPadding, style.FramePadding);\n    bool ret = BeginChild(id, size, true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysUseWindowPadding | extra_flags);\n    PopStyleVar(3);\n    PopStyleColor();\n    return ret;\n}\n\nvoid ImGui::EndChildFrame()\n{\n    EndChild();\n}\n\n// Save and compare stack sizes on Begin()/End() to detect usage errors\nstatic void CheckStacksSize(ImGuiWindow* window, bool write)\n{\n    // NOT checking: DC.ItemWidth, DC.AllowKeyboardFocus, DC.ButtonRepeat, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin)\n    ImGuiContext& g = *GImGui;\n    short* p_backup = &window->DC.StackSizesBackup[0];\n    { int current = window->IDStack.Size;       if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup == current && \"PushID/PopID or TreeNode/TreePop Mismatch!\");   p_backup++; }    // Too few or too many PopID()/TreePop()\n    { int current = window->DC.GroupStack.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup == current && \"BeginGroup/EndGroup Mismatch!\");                p_backup++; }    // Too few or too many EndGroup()\n    { int current = g.BeginPopupStack.Size;     if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup == current && \"BeginMenu/EndMenu or BeginPopup/EndPopup Mismatch\"); p_backup++;}// Too few or too many EndMenu()/EndPopup()\n    // For color, style and font stacks there is an incentive to use Push/Begin/Pop/.../End patterns, so we relax our checks a little to allow them.\n    { int current = g.ColorModifiers.Size;      if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup >= current && \"PushStyleColor/PopStyleColor Mismatch!\");       p_backup++; }    // Too few or too many PopStyleColor()\n    { int current = g.StyleModifiers.Size;      if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup >= current && \"PushStyleVar/PopStyleVar Mismatch!\");           p_backup++; }    // Too few or too many PopStyleVar()\n    { int current = g.FontStack.Size;           if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup >= current && \"PushFont/PopFont Mismatch!\");                   p_backup++; }    // Too few or too many PopFont()\n    IM_ASSERT(p_backup == window->DC.StackSizesBackup + IM_ARRAYSIZE(window->DC.StackSizesBackup));\n}\n\nstatic void SetWindowConditionAllowFlags(ImGuiWindow* window, ImGuiCond flags, bool enabled)\n{\n    window->SetWindowPosAllowFlags       = enabled ? (window->SetWindowPosAllowFlags       | flags) : (window->SetWindowPosAllowFlags       & ~flags);\n    window->SetWindowSizeAllowFlags      = enabled ? (window->SetWindowSizeAllowFlags      | flags) : (window->SetWindowSizeAllowFlags      & ~flags);\n    window->SetWindowCollapsedAllowFlags = enabled ? (window->SetWindowCollapsedAllowFlags | flags) : (window->SetWindowCollapsedAllowFlags & ~flags);\n    window->SetWindowDockAllowFlags      = enabled ? (window->SetWindowDockAllowFlags      | flags) : (window->SetWindowDockAllowFlags      & ~flags);\n}\n\nImGuiWindow* ImGui::FindWindowByID(ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    return (ImGuiWindow*)g.WindowsById.GetVoidPtr(id);\n}\n\nImGuiWindow* ImGui::FindWindowByName(const char* name)\n{\n    ImGuiID id = ImHashStr(name);\n    return FindWindowByID(id);\n}\n\nstatic ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n\n    // Create window the first time\n    ImGuiWindow* window = IM_NEW(ImGuiWindow)(&g, name);\n    window->Flags = flags;\n    g.WindowsById.SetVoidPtr(window->ID, window);\n\n    // Default/arbitrary window position. Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window.\n    ImGuiViewport* main_viewport = ImGui::GetMainViewport();\n    window->Pos = main_viewport->Pos + ImVec2(60, 60);\n\n    // User can disable loading and saving of settings. Tooltip and child windows also don't store settings.\n    if (!(flags & ImGuiWindowFlags_NoSavedSettings))\n        if (ImGuiWindowSettings* settings = ImGui::FindWindowSettings(window->ID))\n        {\n            // Retrieve settings from .ini file\n            window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings);\n            SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false);\n            if (settings->ViewportId)\n            {\n                window->ViewportId = settings->ViewportId;\n                window->ViewportPos = settings->ViewportPos;\n            }\n            else\n            {\n                window->ViewportPos = main_viewport->Pos;\n            }\n            window->Pos = ImFloor(settings->Pos + window->ViewportPos);\n            window->Collapsed = settings->Collapsed;\n            if (ImLengthSqr(settings->Size) > 0.00001f)\n                size = ImFloor(settings->Size);\n            window->DockId = settings->DockId;\n            window->DockOrder = settings->DockOrder;\n        }\n    window->Size = window->SizeFull = window->SizeFullAtLastBegin = ImFloor(size);\n    window->DC.CursorMaxPos = window->Pos; // So first call to CalcSizeContents() doesn't return crazy values\n\n    if ((flags & ImGuiWindowFlags_AlwaysAutoResize) != 0)\n    {\n        window->AutoFitFramesX = window->AutoFitFramesY = 2;\n        window->AutoFitOnlyGrows = false;\n    }\n    else\n    {\n        if (window->Size.x <= 0.0f)\n            window->AutoFitFramesX = 2;\n        if (window->Size.y <= 0.0f)\n            window->AutoFitFramesY = 2;\n        window->AutoFitOnlyGrows = (window->AutoFitFramesX > 0) || (window->AutoFitFramesY > 0);\n    }\n\n    g.WindowsFocusOrder.push_back(window);\n    if (flags & ImGuiWindowFlags_NoBringToFrontOnFocus)\n        g.Windows.push_front(window); // Quite slow but rare and only once\n    else\n        g.Windows.push_back(window);\n    return window;\n}\n\nstatic ImGuiWindow* GetWindowForTitleDisplay(ImGuiWindow* window)\n{\n    return window->DockNodeAsHost ? window->DockNodeAsHost->VisibleWindow : window;\n}\n\nstatic ImGuiWindow* GetWindowForTitleAndMenuHeight(ImGuiWindow* window)\n{\n    return (window->DockNodeAsHost && window->DockNodeAsHost->VisibleWindow) ? window->DockNodeAsHost->VisibleWindow : window;\n}\n\nstatic ImVec2 CalcSizeAfterConstraint(ImGuiWindow* window, ImVec2 new_size)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.NextWindowData.SizeConstraintCond != 0)\n    {\n        // Using -1,-1 on either X/Y axis to preserve the current size.\n        ImRect cr = g.NextWindowData.SizeConstraintRect;\n        new_size.x = (cr.Min.x >= 0 && cr.Max.x >= 0) ? ImClamp(new_size.x, cr.Min.x, cr.Max.x) : window->SizeFull.x;\n        new_size.y = (cr.Min.y >= 0 && cr.Max.y >= 0) ? ImClamp(new_size.y, cr.Min.y, cr.Max.y) : window->SizeFull.y;\n        if (g.NextWindowData.SizeCallback)\n        {\n            ImGuiSizeCallbackData data;\n            data.UserData = g.NextWindowData.SizeCallbackUserData;\n            data.Pos = window->Pos;\n            data.CurrentSize = window->SizeFull;\n            data.DesiredSize = new_size;\n            g.NextWindowData.SizeCallback(&data);\n            new_size = data.DesiredSize;\n        }\n    }\n\n    // Minimum size\n    if (!(window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysAutoResize)))\n    {\n        ImGuiWindow* window_for_height = GetWindowForTitleAndMenuHeight(window);\n        new_size = ImMax(new_size, g.Style.WindowMinSize);\n        new_size.y = ImMax(new_size.y, window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); // Reduce artifacts with very small windows\n    }\n    return new_size;\n}\n\nstatic ImVec2 CalcSizeContents(ImGuiWindow* window)\n{\n    if (window->Collapsed)\n        if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0)\n            return window->SizeContents;\n    if (window->Hidden && window->HiddenFramesCannotSkipItems == 0 && window->HiddenFramesCanSkipItems > 0)\n        return window->SizeContents;\n\n    ImVec2 sz;\n    sz.x = (float)(int)((window->SizeContentsExplicit.x != 0.0f) ? window->SizeContentsExplicit.x : (window->DC.CursorMaxPos.x - window->Pos.x + window->Scroll.x));\n    sz.y = (float)(int)((window->SizeContentsExplicit.y != 0.0f) ? window->SizeContentsExplicit.y : (window->DC.CursorMaxPos.y - window->Pos.y + window->Scroll.y));\n    return sz + window->WindowPadding;\n}\n\nstatic ImVec2 CalcSizeAutoFit(ImGuiWindow* window, const ImVec2& size_contents)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiStyle& style = g.Style;\n    if (window->Flags & ImGuiWindowFlags_Tooltip)\n    {\n        // Tooltip always resize\n        return size_contents;\n    }\n    else\n    {\n        // Maximum window size is determined by the viewport size or monitor size\n        const bool is_popup = (window->Flags & ImGuiWindowFlags_Popup) != 0;\n        const bool is_menu = (window->Flags & ImGuiWindowFlags_ChildMenu) != 0;\n        ImVec2 size_min = style.WindowMinSize;\n        if (is_popup || is_menu) // Popups and menus bypass style.WindowMinSize by default, but we give then a non-zero minimum size to facilitate understanding problematic cases (e.g. empty popups)\n            size_min = ImMin(size_min, ImVec2(4.0f, 4.0f));\n\n        ImVec2 avail_size = window->Viewport->Size;\n        if (window->ViewportOwned)\n            avail_size = ImVec2(FLT_MAX, FLT_MAX);\n        const int monitor_idx = window->ViewportAllowPlatformMonitorExtend;\n        if (monitor_idx >= 0 && monitor_idx < g.PlatformIO.Monitors.Size)\n            avail_size = g.PlatformIO.Monitors[monitor_idx].WorkSize;\n        ImVec2 size_auto_fit = ImClamp(size_contents, size_min, ImMax(size_min, avail_size - g.Style.DisplaySafeAreaPadding * 2.0f));\n\n        // When the window cannot fit all contents (either because of constraints, either because screen is too small),\n        // we are growing the size on the other axis to compensate for expected scrollbar. FIXME: Might turn bigger than ViewportSize-WindowPadding.\n        ImVec2 size_auto_fit_after_constraint = CalcSizeAfterConstraint(window, size_auto_fit);\n        if (size_auto_fit_after_constraint.x < size_contents.x && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && (window->Flags & ImGuiWindowFlags_HorizontalScrollbar))\n            size_auto_fit.y += style.ScrollbarSize;\n        if (size_auto_fit_after_constraint.y < size_contents.y && !(window->Flags & ImGuiWindowFlags_NoScrollbar))\n            size_auto_fit.x += style.ScrollbarSize;\n        return size_auto_fit;\n    }\n}\n\nImVec2 ImGui::CalcWindowExpectedSize(ImGuiWindow* window)\n{\n    ImVec2 size_contents = CalcSizeContents(window);\n    return CalcSizeAfterConstraint(window, CalcSizeAutoFit(window, size_contents));\n}\n\nfloat ImGui::GetWindowScrollMaxX(ImGuiWindow* window)\n{\n    return ImMax(0.0f, window->SizeContents.x - (window->SizeFull.x - window->ScrollbarSizes.x));\n}\n\nfloat ImGui::GetWindowScrollMaxY(ImGuiWindow* window)\n{\n    return ImMax(0.0f, window->SizeContents.y - (window->SizeFull.y - window->ScrollbarSizes.y));\n}\n\nstatic ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges)\n{\n    ImGuiContext& g = *GImGui;\n    ImVec2 scroll = window->Scroll;\n    if (window->ScrollTarget.x < FLT_MAX)\n    {\n        float cr_x = window->ScrollTargetCenterRatio.x;\n        scroll.x = window->ScrollTarget.x - cr_x * (window->SizeFull.x - window->ScrollbarSizes.x);\n    }\n    if (window->ScrollTarget.y < FLT_MAX)\n    {\n        // 'snap_on_edges' allows for a discontinuity at the edge of scrolling limits to take account of WindowPadding so that scrolling to make the last item visible scroll far enough to see the padding.\n        float cr_y = window->ScrollTargetCenterRatio.y;\n        float target_y = window->ScrollTarget.y;\n        if (snap_on_edges && cr_y <= 0.0f && target_y <= window->WindowPadding.y)\n            target_y = 0.0f;\n        if (snap_on_edges && cr_y >= 1.0f && target_y >= window->SizeContents.y - window->WindowPadding.y + g.Style.ItemSpacing.y)\n            target_y = window->SizeContents.y;\n        scroll.y = target_y - (1.0f - cr_y) * (window->TitleBarHeight() + window->MenuBarHeight()) - cr_y * (window->SizeFull.y - window->ScrollbarSizes.y);\n    }\n    scroll = ImMax(scroll, ImVec2(0.0f, 0.0f));\n    if (!window->Collapsed && !window->SkipItems)\n    {\n        scroll.x = ImMin(scroll.x, ImGui::GetWindowScrollMaxX(window));\n        scroll.y = ImMin(scroll.y, ImGui::GetWindowScrollMaxY(window));\n    }\n    return scroll;\n}\n\nstatic ImGuiCol GetWindowBgColorIdxFromFlags(ImGuiWindowFlags flags)\n{\n    if (flags & (ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup))\n        return ImGuiCol_PopupBg;\n    if (flags & ImGuiWindowFlags_ChildWindow)\n        return ImGuiCol_ChildBg;\n    return ImGuiCol_WindowBg;\n}\n\nstatic void CalcResizePosSizeFromAnyCorner(ImGuiWindow* window, const ImVec2& corner_target, const ImVec2& corner_norm, ImVec2* out_pos, ImVec2* out_size)\n{\n    ImVec2 pos_min = ImLerp(corner_target, window->Pos, corner_norm);                // Expected window upper-left\n    ImVec2 pos_max = ImLerp(window->Pos + window->Size, corner_target, corner_norm); // Expected window lower-right\n    ImVec2 size_expected = pos_max - pos_min;\n    ImVec2 size_constrained = CalcSizeAfterConstraint(window, size_expected);\n    *out_pos = pos_min;\n    if (corner_norm.x == 0.0f)\n        out_pos->x -= (size_constrained.x - size_expected.x);\n    if (corner_norm.y == 0.0f)\n        out_pos->y -= (size_constrained.y - size_expected.y);\n    *out_size = size_constrained;\n}\n\nstruct ImGuiResizeGripDef\n{\n    ImVec2  CornerPosN;\n    ImVec2  InnerDir;\n    int     AngleMin12, AngleMax12;\n};\n\nstatic const ImGuiResizeGripDef resize_grip_def[4] =\n{\n    { ImVec2(1,1), ImVec2(-1,-1), 0, 3 }, // Lower right\n    { ImVec2(0,1), ImVec2(+1,-1), 3, 6 }, // Lower left\n    { ImVec2(0,0), ImVec2(+1,+1), 6, 9 }, // Upper left\n    { ImVec2(1,0), ImVec2(-1,+1), 9,12 }, // Upper right\n};\n\nstatic ImRect GetResizeBorderRect(ImGuiWindow* window, int border_n, float perp_padding, float thickness)\n{\n    ImRect rect = window->Rect();\n    if (thickness == 0.0f) rect.Max -= ImVec2(1,1);\n    if (border_n == 0) return ImRect(rect.Min.x + perp_padding, rect.Min.y - thickness,    rect.Max.x - perp_padding, rect.Min.y + thickness);      // Top\n    if (border_n == 1) return ImRect(rect.Max.x - thickness,    rect.Min.y + perp_padding, rect.Max.x + thickness,    rect.Max.y - perp_padding);   // Right\n    if (border_n == 2) return ImRect(rect.Min.x + perp_padding, rect.Max.y - thickness,    rect.Max.x - perp_padding, rect.Max.y + thickness);      // Bottom\n    if (border_n == 3) return ImRect(rect.Min.x - thickness,    rect.Min.y + perp_padding, rect.Min.x + thickness,    rect.Max.y - perp_padding);   // Left\n    IM_ASSERT(0);\n    return ImRect();\n}\n\n// Handle resize for: Resize Grips, Borders, Gamepad\nstatic void ImGui::UpdateManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4])\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindowFlags flags = window->Flags;\n    if ((flags & ImGuiWindowFlags_NoResize) || (flags & ImGuiWindowFlags_AlwaysAutoResize) || window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0)\n        return;\n    if (window->WasActive == false) // Early out to avoid running this code for e.g. an hidden implicit/fallback Debug window.\n        return;\n\n    const int resize_border_count = g.IO.ConfigWindowsResizeFromEdges ? 4 : 0;\n    const float grip_draw_size = (float)(int)ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f);\n    const float grip_hover_inner_size = (float)(int)(grip_draw_size * 0.75f);\n    const float grip_hover_outer_size = g.IO.ConfigWindowsResizeFromEdges ? WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS : 0.0f;\n\n    ImVec2 pos_target(FLT_MAX, FLT_MAX);\n    ImVec2 size_target(FLT_MAX, FLT_MAX);\n\n    // Resize grips and borders are on layer 1\n    window->DC.NavLayerCurrent = ImGuiNavLayer_Menu;\n    window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu);\n\n    // Manual resize grips\n    PushID(\"#RESIZE\");\n    for (int resize_grip_n = 0; resize_grip_n < resize_grip_count; resize_grip_n++)\n    {\n        const ImGuiResizeGripDef& grip = resize_grip_def[resize_grip_n];\n        const ImVec2 corner = ImLerp(window->Pos, window->Pos + window->Size, grip.CornerPosN);\n\n        // Using the FlattenChilds button flag we make the resize button accessible even if we are hovering over a child window\n        ImRect resize_rect(corner - grip.InnerDir * grip_hover_outer_size, corner + grip.InnerDir * grip_hover_inner_size);\n        if (resize_rect.Min.x > resize_rect.Max.x) ImSwap(resize_rect.Min.x, resize_rect.Max.x);\n        if (resize_rect.Min.y > resize_rect.Max.y) ImSwap(resize_rect.Min.y, resize_rect.Max.y);\n        bool hovered, held;\n        ButtonBehavior(resize_rect, window->GetID((void*)(intptr_t)resize_grip_n), &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus);\n        //GetForegroundDrawList(window)->AddRect(resize_rect.Min, resize_rect.Max, IM_COL32(255, 255, 0, 255));\n        if (hovered || held)\n            g.MouseCursor = (resize_grip_n & 1) ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE;\n\n        if (held && g.IO.MouseDoubleClicked[0] && resize_grip_n == 0)\n        {\n            // Manual auto-fit when double-clicking\n            size_target = CalcSizeAfterConstraint(window, size_auto_fit);\n            ClearActiveID();\n        }\n        else if (held)\n        {\n            // Resize from any of the four corners\n            // We don't use an incremental MouseDelta but rather compute an absolute target size based on mouse position\n            ImVec2 corner_target = g.IO.MousePos - g.ActiveIdClickOffset + ImLerp(grip.InnerDir * grip_hover_outer_size, grip.InnerDir * -grip_hover_inner_size, grip.CornerPosN); // Corner of the window corresponding to our corner grip\n            CalcResizePosSizeFromAnyCorner(window, corner_target, grip.CornerPosN, &pos_target, &size_target);\n        }\n        if (resize_grip_n == 0 || held || hovered)\n            resize_grip_col[resize_grip_n] = GetColorU32(held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip);\n    }\n    for (int border_n = 0; border_n < resize_border_count; border_n++)\n    {\n        bool hovered, held;\n        ImRect border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS);\n        ButtonBehavior(border_rect, window->GetID((void*)(intptr_t)(border_n + 4)), &hovered, &held, ImGuiButtonFlags_FlattenChildren);\n        //GetForegroundDrawLists(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255));\n        if ((hovered && g.HoveredIdTimer > WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) || held)\n        {\n            g.MouseCursor = (border_n & 1) ? ImGuiMouseCursor_ResizeEW : ImGuiMouseCursor_ResizeNS;\n            if (held)\n                *border_held = border_n;\n        }\n        if (held)\n        {\n            ImVec2 border_target = window->Pos;\n            ImVec2 border_posn;\n            if (border_n == 0) { border_posn = ImVec2(0, 0); border_target.y = (g.IO.MousePos.y - g.ActiveIdClickOffset.y + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Top\n            if (border_n == 1) { border_posn = ImVec2(1, 0); border_target.x = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Right\n            if (border_n == 2) { border_posn = ImVec2(0, 1); border_target.y = (g.IO.MousePos.y - g.ActiveIdClickOffset.y + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Bottom\n            if (border_n == 3) { border_posn = ImVec2(0, 0); border_target.x = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Left\n            CalcResizePosSizeFromAnyCorner(window, border_target, border_posn, &pos_target, &size_target);\n        }\n    }\n    PopID();\n\n    // Navigation resize (keyboard/gamepad)\n    if (g.NavWindowingTarget && g.NavWindowingTarget->RootWindow == window)\n    {\n        ImVec2 nav_resize_delta;\n        if (g.NavInputSource == ImGuiInputSource_NavKeyboard && g.IO.KeyShift)\n            nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down);\n        if (g.NavInputSource == ImGuiInputSource_NavGamepad)\n            nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_Down);\n        if (nav_resize_delta.x != 0.0f || nav_resize_delta.y != 0.0f)\n        {\n            const float NAV_RESIZE_SPEED = 600.0f;\n            nav_resize_delta *= ImFloor(NAV_RESIZE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y));\n            g.NavWindowingToggleLayer = false;\n            g.NavDisableMouseHover = true;\n            resize_grip_col[0] = GetColorU32(ImGuiCol_ResizeGripActive);\n            // FIXME-NAV: Should store and accumulate into a separate size buffer to handle sizing constraints properly, right now a constraint will make us stuck.\n            size_target = CalcSizeAfterConstraint(window, window->SizeFull + nav_resize_delta);\n        }\n    }\n\n    // Apply back modified position/size to window\n    if (size_target.x != FLT_MAX)\n    {\n        window->SizeFull = size_target;\n        MarkIniSettingsDirty(window);\n    }\n    if (pos_target.x != FLT_MAX)\n    {\n        window->Pos = ImFloor(pos_target);\n        MarkIniSettingsDirty(window);\n    }\n\n    // Resize nav layer\n    window->DC.NavLayerCurrent = ImGuiNavLayer_Main;\n    window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main);\n\n    window->Size = window->SizeFull;\n}\n\nstatic inline void ClampWindowRect(ImGuiWindow* window, const ImRect& rect, const ImVec2& padding)\n{\n    ImGuiContext& g = *GImGui;\n    ImVec2 size_for_clamping = (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) ? ImVec2(window->Size.x, window->TitleBarHeight()) : window->Size;\n    window->Pos = ImMin(rect.Max - padding, ImMax(window->Pos + size_for_clamping, rect.Min + padding) - size_for_clamping);\n}\n\nstatic void ImGui::RenderOuterBorders(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    float rounding = window->WindowRounding;\n    float border_size = window->WindowBorderSize;\n    if (border_size > 0.0f && !(window->Flags & ImGuiWindowFlags_NoBackground))\n        window->DrawList->AddRect(window->Pos, window->Pos + window->Size, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size);\n\n    int border_held = window->ResizeBorderHeld;\n    if (border_held != -1)\n    {\n        struct ImGuiResizeBorderDef\n        {\n            ImVec2 InnerDir;\n            ImVec2 CornerPosN1, CornerPosN2;\n            float  OuterAngle;\n        };\n        static const ImGuiResizeBorderDef resize_border_def[4] =\n        {\n            { ImVec2(0,+1), ImVec2(0,0), ImVec2(1,0), IM_PI*1.50f }, // Top\n            { ImVec2(-1,0), ImVec2(1,0), ImVec2(1,1), IM_PI*0.00f }, // Right\n            { ImVec2(0,-1), ImVec2(1,1), ImVec2(0,1), IM_PI*0.50f }, // Bottom\n            { ImVec2(+1,0), ImVec2(0,1), ImVec2(0,0), IM_PI*1.00f }  // Left\n        };\n        const ImGuiResizeBorderDef& def = resize_border_def[border_held];\n        ImRect border_r = GetResizeBorderRect(window, border_held, rounding, 0.0f);\n        window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI*0.25f, def.OuterAngle);\n        window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN2) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle, def.OuterAngle + IM_PI*0.25f);\n        window->DrawList->PathStroke(GetColorU32(ImGuiCol_SeparatorActive), false, ImMax(2.0f, border_size)); // Thicker than usual\n    }\n    if (g.Style.FrameBorderSize > 0 && !(window->Flags & ImGuiWindowFlags_NoTitleBar) && !window->DockIsActive)\n    {\n        float y = window->Pos.y + window->TitleBarHeight() - 1;\n        window->DrawList->AddLine(ImVec2(window->Pos.x + border_size, y), ImVec2(window->Pos.x + window->Size.x - border_size, y), GetColorU32(ImGuiCol_Border), g.Style.FrameBorderSize);\n    }\n}\n\nvoid ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window)\n{\n    window->ParentWindow = parent_window;\n    window->RootWindow = window->RootWindowDockStop = window->RootWindowForTitleBarHighlight = window->RootWindowForNav = window;\n    if (parent_window && (flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Tooltip))\n    {\n        window->RootWindow = parent_window->RootWindow;\n        if (!window->DockIsActive && !(parent_window->Flags & ImGuiWindowFlags_DockNodeHost))\n            window->RootWindowDockStop = parent_window->RootWindowDockStop;\n    }\n    if (parent_window && !(flags & ImGuiWindowFlags_Modal) && (flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup)))\n        window->RootWindowForTitleBarHighlight = parent_window->RootWindowForTitleBarHighlight;\n    while (window->RootWindowForNav->Flags & ImGuiWindowFlags_NavFlattened)\n    {\n        IM_ASSERT(window->RootWindowForNav->ParentWindow != NULL);\n        window->RootWindowForNav = window->RootWindowForNav->ParentWindow;\n    }\n}\n\n// Push a new ImGui window to add widgets to.\n// - A default window called \"Debug\" is automatically stacked at the beginning of every frame so you can use widgets without explicitly calling a Begin/End pair.\n// - Begin/End can be called multiple times during the frame with the same window name to append content.\n// - The window name is used as a unique identifier to preserve window information across frames (and save rudimentary information to the .ini file).\n//   You can use the \"##\" or \"###\" markers to use the same label with different id, or same id with different label. See documentation at the top of this file.\n// - Return false when window is collapsed, so you can early out in your code. You always need to call ImGui::End() even if false is returned.\n// - Passing 'bool* p_open' displays a Close button on the upper-right corner of the window, the pointed value will be set to false when the button is pressed.\nbool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    IM_ASSERT(name != NULL && name[0] != '\\0');     // Window name required\n    IM_ASSERT(g.FrameScopeActive);                  // Forgot to call ImGui::NewFrame()\n    IM_ASSERT(g.FrameCountEnded != g.FrameCount);   // Called ImGui::Render() or ImGui::EndFrame() and haven't called ImGui::NewFrame() again yet\n\n    // Find or create\n    ImGuiWindow* window = FindWindowByName(name);\n    const bool window_just_created = (window == NULL);\n    const bool window_is_fallback = (g.CurrentWindowStack.Size == 0);\n    if (window_just_created)\n    {\n        ImVec2 size_on_first_use = (g.NextWindowData.SizeCond != 0) ? g.NextWindowData.SizeVal : ImVec2(0.0f, 0.0f); // Any condition flag will do since we are creating a new window here.\n        window = CreateNewWindow(name, size_on_first_use, flags);\n    }\n\n    // Automatically disable manual moving/resizing when NoInputs is set\n    if ((flags & ImGuiWindowFlags_NoInputs) == ImGuiWindowFlags_NoInputs)\n        flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;\n\n    if (flags & ImGuiWindowFlags_NavFlattened)\n        IM_ASSERT(flags & ImGuiWindowFlags_ChildWindow);\n\n    const int current_frame = g.FrameCount;\n    const bool first_begin_of_the_frame = (window->LastFrameActive != current_frame);\n\n    // Update the Appearing flag\n    bool window_just_activated_by_user = (window->LastFrameActive < current_frame - 1);   // Not using !WasActive because the implicit \"Debug\" window would always toggle off->on\n    const bool window_just_appearing_after_hidden_for_resize = (window->HiddenFramesCannotSkipItems > 0);\n    if (flags & ImGuiWindowFlags_Popup)\n    {\n        ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size];\n        window_just_activated_by_user |= (window->PopupId != popup_ref.PopupId); // We recycle popups so treat window as activated if popup id changed\n        window_just_activated_by_user |= (window != popup_ref.Window);\n    }\n    window->Appearing = (window_just_activated_by_user || window_just_appearing_after_hidden_for_resize);\n    if (window->Appearing)\n        SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, true);\n\n    // Update Flags, LastFrameActive, BeginOrderXXX fields\n    if (first_begin_of_the_frame)\n    {\n        window->FlagsPreviousFrame = window->Flags;\n        window->Flags = (ImGuiWindowFlags)flags;\n        window->LastFrameActive = current_frame;\n        window->BeginOrderWithinParent = 0;\n        window->BeginOrderWithinContext = (short)(g.WindowsActiveCount++);\n    }\n    else\n    {\n        flags = window->Flags;\n    }\n\n    // Docking\n    // (NB: during the frame dock nodes are created, it is possible that (window->DockIsActive == false) even though (window->DockNode->Windows.Size > 1)\n    IM_ASSERT(window->DockNode == NULL || window->DockNodeAsHost == NULL); // Cannot be both\n    if (g.NextWindowData.DockCond)\n        SetWindowDock(window, g.NextWindowData.DockId, g.NextWindowData.DockCond);\n    if (first_begin_of_the_frame)\n    {\n        bool has_dock_node = (window->DockId != 0 || window->DockNode != NULL);\n        bool new_auto_dock_node = !has_dock_node && g.IO.ConfigDockingTabBarOnSingleWindows && !(flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDocking)) && !window_is_fallback;\n        if (has_dock_node || new_auto_dock_node)\n        {\n            BeginDocked(window, p_open);\n            flags = window->Flags;\n        }\n    }\n\n    // Parent window is latched only on the first call to Begin() of the frame, so further append-calls can be done from a different window stack\n    ImGuiWindow* parent_window_in_stack = window->DockIsActive ? window->DockNode->HostWindow : g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back();\n    ImGuiWindow* parent_window = first_begin_of_the_frame ? ((flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup)) ? parent_window_in_stack : NULL) : window->ParentWindow;\n    IM_ASSERT(parent_window != NULL || !(flags & ImGuiWindowFlags_ChildWindow));\n\n    // Add to stack\n    // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow()\n    g.CurrentWindowStack.push_back(window);\n    g.CurrentWindow = NULL;\n    CheckStacksSize(window, true);\n    if (flags & ImGuiWindowFlags_Popup)\n    {\n        ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size];\n        popup_ref.Window = window;\n        g.BeginPopupStack.push_back(popup_ref);\n        window->PopupId = popup_ref.PopupId;\n    }\n\n    if (window_just_appearing_after_hidden_for_resize && !(flags & ImGuiWindowFlags_ChildWindow))\n        window->NavLastIds[0] = 0;\n\n    // Process SetNextWindow***() calls\n    bool window_pos_set_by_api = false;\n    bool window_size_x_set_by_api = false, window_size_y_set_by_api = false;\n    if (g.NextWindowData.PosCond)\n    {\n        window_pos_set_by_api = (window->SetWindowPosAllowFlags & g.NextWindowData.PosCond) != 0;\n        if (window_pos_set_by_api && ImLengthSqr(g.NextWindowData.PosPivotVal) > 0.00001f)\n        {\n            // May be processed on the next frame if this is our first frame and we are measuring size\n            // FIXME: Look into removing the branch so everything can go through this same code path for consistency.\n            window->SetWindowPosVal = g.NextWindowData.PosVal;\n            window->SetWindowPosPivot = g.NextWindowData.PosPivotVal;\n            window->SetWindowPosAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing);\n        }\n        else\n        {\n            SetWindowPos(window, g.NextWindowData.PosVal, g.NextWindowData.PosCond);\n        }\n    }\n    if (g.NextWindowData.SizeCond)\n    {\n        window_size_x_set_by_api = (window->SetWindowSizeAllowFlags & g.NextWindowData.SizeCond) != 0 && (g.NextWindowData.SizeVal.x > 0.0f);\n        window_size_y_set_by_api = (window->SetWindowSizeAllowFlags & g.NextWindowData.SizeCond) != 0 && (g.NextWindowData.SizeVal.y > 0.0f);\n        SetWindowSize(window, g.NextWindowData.SizeVal, g.NextWindowData.SizeCond);\n    }\n    if (g.NextWindowData.ContentSizeCond)\n    {\n        // Adjust passed \"client size\" to become a \"window size\"\n        window->SizeContentsExplicit = g.NextWindowData.ContentSizeVal;\n        if (window->SizeContentsExplicit.y != 0.0f)\n            window->SizeContentsExplicit.y += window->TitleBarHeight() + window->MenuBarHeight();\n    }\n    else if (first_begin_of_the_frame)\n    {\n        window->SizeContentsExplicit = ImVec2(0.0f, 0.0f);\n    }\n    window->WindowClass = g.NextWindowData.WindowClass;\n    if (g.NextWindowData.CollapsedCond)\n        SetWindowCollapsed(window, g.NextWindowData.CollapsedVal, g.NextWindowData.CollapsedCond);\n    if (g.NextWindowData.FocusCond)\n        FocusWindow(window);\n    if (window->Appearing)\n        SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, false);\n\n    // When reusing window again multiple times a frame, just append content (don't need to setup again)\n    if (first_begin_of_the_frame)\n    {\n        // Initialize\n        const bool window_is_child_tooltip = (flags & ImGuiWindowFlags_ChildWindow) && (flags & ImGuiWindowFlags_Tooltip); // FIXME-WIP: Undocumented behavior of Child+Tooltip for pinned tooltip (#1345)\n        UpdateWindowParentAndRootLinks(window, flags, parent_window);\n\n        window->Active = true;\n        window->HasCloseButton = (p_open != NULL);\n        window->ClipRect = ImVec4(-FLT_MAX,-FLT_MAX,+FLT_MAX,+FLT_MAX);\n        window->IDStack.resize(1);\n\n        // Update stored window name when it changes (which can _only_ happen with the \"###\" operator, so the ID would stay unchanged).\n        // The title bar always display the 'name' parameter, so we only update the string storage if it needs to be visible to the end-user elsewhere.\n        bool window_title_visible_elsewhere = false;\n        if ((window->Viewport && window->Viewport->Window == window) || (window->DockIsActive))\n            window_title_visible_elsewhere = true;\n        else if (g.NavWindowingList != NULL && (window->Flags & ImGuiWindowFlags_NoNavFocus) == 0)   // Window titles visible when using CTRL+TAB\n            window_title_visible_elsewhere = true;\n        if (window_title_visible_elsewhere && !window_just_created && strcmp(name, window->Name) != 0)\n        {\n            size_t buf_len = (size_t)window->NameBufLen;\n            window->Name = ImStrdupcpy(window->Name, &buf_len, name);\n            window->NameBufLen = (int)buf_len;\n        }\n\n        // UPDATE CONTENTS SIZE, UPDATE HIDDEN STATUS\n\n        // Update contents size from last frame for auto-fitting (or use explicit size)\n        window->SizeContents = CalcSizeContents(window);\n        if (window->HiddenFramesCanSkipItems > 0)\n            window->HiddenFramesCanSkipItems--;\n        if (window->HiddenFramesCannotSkipItems > 0)\n            window->HiddenFramesCannotSkipItems--;\n\n        // Hide new windows for one frame until they calculate their size\n        if (window_just_created && (!window_size_x_set_by_api || !window_size_y_set_by_api))\n            window->HiddenFramesCannotSkipItems = 1;\n\n        // Hide popup/tooltip window when re-opening while we measure size (because we recycle the windows)\n        // We reset Size/SizeContents for reappearing popups/tooltips early in this function, so further code won't be tempted to use the old size.\n        if (window_just_activated_by_user && (flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) != 0)\n        {\n            window->HiddenFramesCannotSkipItems = 1;\n            if (flags & ImGuiWindowFlags_AlwaysAutoResize)\n            {\n                if (!window_size_x_set_by_api)\n                    window->Size.x = window->SizeFull.x = 0.f;\n                if (!window_size_y_set_by_api)\n                    window->Size.y = window->SizeFull.y = 0.f;\n                window->SizeContents = ImVec2(0.f, 0.f);\n            }\n        }\n\n        // SELECT VIEWPORT\n        // We need to do this before using any style/font sizes, as viewport with a different DPI may affect font sizes.\n\n        UpdateSelectWindowViewport(window);\n        SetCurrentViewport(window, window->Viewport);\n        window->FontDpiScale = (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleFonts) ? window->Viewport->DpiScale : 1.0f;\n        SetCurrentWindow(window);\n        flags = window->Flags;\n\n        // Lock border size and padding for the frame (so that altering them doesn't cause inconsistencies)\n        if (flags & ImGuiWindowFlags_ChildWindow)\n            window->WindowBorderSize = style.ChildBorderSize;\n        else\n            window->WindowBorderSize = ((flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) && !(flags & ImGuiWindowFlags_Modal)) ? style.PopupBorderSize : style.WindowBorderSize;\n        if (!window->DockIsActive && (flags & ImGuiWindowFlags_ChildWindow) && !(flags & (ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_Popup)) && window->WindowBorderSize == 0.0f)\n            window->WindowPadding = ImVec2(0.0f, (flags & ImGuiWindowFlags_MenuBar) ? style.WindowPadding.y : 0.0f);\n        else\n            window->WindowPadding = style.WindowPadding;\n        window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x);\n        window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y;\n\n        // Collapse window by double-clicking on title bar\n        // At this point we don't have a clipping rectangle setup yet, so we can use the title bar area for hit detection and drawing\n        if (!(flags & ImGuiWindowFlags_NoTitleBar) && !(flags & ImGuiWindowFlags_NoCollapse) && !window->DockIsActive)\n        {\n            // We don't use a regular button+id to test for double-click on title bar (mostly due to legacy reason, could be fixed), so verify that we don't have items over the title bar.\n            ImRect title_bar_rect = window->TitleBarRect();\n            if (g.HoveredWindow == window && g.HoveredId == 0 && g.HoveredIdPreviousFrame == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max) && g.IO.MouseDoubleClicked[0])\n                window->WantCollapseToggle = true;\n            if (window->WantCollapseToggle)\n            {\n                window->Collapsed = !window->Collapsed;\n                MarkIniSettingsDirty(window);\n                FocusWindow(window);\n            }\n        }\n        else\n        {\n            window->Collapsed = false;\n        }\n        window->WantCollapseToggle = false;\n\n        // SIZE\n\n        // Calculate auto-fit size, handle automatic resize\n        const ImVec2 size_auto_fit = CalcSizeAutoFit(window, window->SizeContents);\n        ImVec2 size_full_modified(FLT_MAX, FLT_MAX);\n        if ((flags & ImGuiWindowFlags_AlwaysAutoResize) && !window->Collapsed)\n        {\n            // Using SetNextWindowSize() overrides ImGuiWindowFlags_AlwaysAutoResize, so it can be used on tooltips/popups, etc.\n            if (!window_size_x_set_by_api)\n                window->SizeFull.x = size_full_modified.x = size_auto_fit.x;\n            if (!window_size_y_set_by_api)\n                window->SizeFull.y = size_full_modified.y = size_auto_fit.y;\n        }\n        else if (window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0)\n        {\n            // Auto-fit may only grow window during the first few frames\n            // We still process initial auto-fit on collapsed windows to get a window width, but otherwise don't honor ImGuiWindowFlags_AlwaysAutoResize when collapsed.\n            if (!window_size_x_set_by_api && window->AutoFitFramesX > 0)\n                window->SizeFull.x = size_full_modified.x = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.x, size_auto_fit.x) : size_auto_fit.x;\n            if (!window_size_y_set_by_api && window->AutoFitFramesY > 0)\n                window->SizeFull.y = size_full_modified.y = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.y, size_auto_fit.y) : size_auto_fit.y;\n            if (!window->Collapsed)\n                MarkIniSettingsDirty(window);\n        }\n\n        // Apply minimum/maximum window size constraints and final size\n        window->SizeFull = CalcSizeAfterConstraint(window, window->SizeFull);\n        window->Size = window->Collapsed && !(flags & ImGuiWindowFlags_ChildWindow) ? window->TitleBarRect().GetSize() : window->SizeFull;\n\n        // SCROLLBAR STATUS\n\n        // Update scrollbar status (based on the Size that was effective during last frame or the auto-resized Size).\n        if (!window->Collapsed)\n        {\n            // When reading the current size we need to read it after size constraints have been applied\n            float size_x_for_scrollbars = size_full_modified.x != FLT_MAX ? window->SizeFull.x : window->SizeFullAtLastBegin.x;\n            float size_y_for_scrollbars = size_full_modified.y != FLT_MAX ? window->SizeFull.y : window->SizeFullAtLastBegin.y;\n            window->ScrollbarY = (flags & ImGuiWindowFlags_AlwaysVerticalScrollbar) || ((window->SizeContents.y > size_y_for_scrollbars) && !(flags & ImGuiWindowFlags_NoScrollbar));\n            window->ScrollbarX = (flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar) || ((window->SizeContents.x > size_x_for_scrollbars - (window->ScrollbarY ? style.ScrollbarSize : 0.0f)) && !(flags & ImGuiWindowFlags_NoScrollbar) && (flags & ImGuiWindowFlags_HorizontalScrollbar));\n            if (window->ScrollbarX && !window->ScrollbarY)\n                window->ScrollbarY = (window->SizeContents.y > size_y_for_scrollbars - style.ScrollbarSize) && !(flags & ImGuiWindowFlags_NoScrollbar);\n            window->ScrollbarSizes = ImVec2(window->ScrollbarY ? style.ScrollbarSize : 0.0f, window->ScrollbarX ? style.ScrollbarSize : 0.0f);\n        }\n\n        // POSITION\n\n        // Popup latch its initial position, will position itself when it appears next frame\n        if (window_just_activated_by_user)\n        {\n            window->AutoPosLastDirection = ImGuiDir_None;\n            if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api)\n                window->Pos = g.BeginPopupStack.back().OpenPopupPos;\n        }\n\n        // Position child window\n        if (flags & ImGuiWindowFlags_ChildWindow)\n        {\n            IM_ASSERT(parent_window && parent_window->Active);\n            window->BeginOrderWithinParent = (short)parent_window->DC.ChildWindows.Size;\n            parent_window->DC.ChildWindows.push_back(window);\n            if (!(flags & ImGuiWindowFlags_Popup) && !window_pos_set_by_api && !window_is_child_tooltip)\n                window->Pos = parent_window->DC.CursorPos;\n        }\n\n        const bool window_pos_with_pivot = (window->SetWindowPosVal.x != FLT_MAX && window->HiddenFramesCannotSkipItems == 0);\n        if (window_pos_with_pivot)\n            SetWindowPos(window, ImMax(style.DisplaySafeAreaPadding, window->SetWindowPosVal - window->SizeFull * window->SetWindowPosPivot), 0); // Position given a pivot (e.g. for centering)\n        else if ((flags & ImGuiWindowFlags_ChildMenu) != 0)\n            window->Pos = FindBestWindowPosForPopup(window);\n        else if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api && window_just_appearing_after_hidden_for_resize)\n            window->Pos = FindBestWindowPosForPopup(window);\n        else if ((flags & ImGuiWindowFlags_Tooltip) != 0 && !window_pos_set_by_api && !window_is_child_tooltip)\n            window->Pos = FindBestWindowPosForPopup(window);\n\n        // Late create viewport if we don't fit within our current host viewport.\n        if (window->ViewportAllowPlatformMonitorExtend >= 0 && !window->ViewportOwned && !(window->Viewport->Flags & ImGuiViewportFlags_Minimized))\n            if (!window->Viewport->GetRect().Contains(window->Rect()))\n            {\n                // This is based on the assumption that the DPI will be known ahead (same as the DPI of the selection done in UpdateSelectWindowViewport)\n                //ImGuiViewport* old_viewport = window->Viewport;\n                window->Viewport = AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_NoFocusOnAppearing);\n\n                // FIXME-DPI\n                //IM_ASSERT(old_viewport->DpiScale == window->Viewport->DpiScale); // FIXME-DPI: Something went wrong\n                SetCurrentViewport(window, window->Viewport);\n                window->FontDpiScale = (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleFonts) ? window->Viewport->DpiScale : 1.0f;\n                SetCurrentWindow(window);\n            }\n\n        bool viewport_rect_changed = false;\n        if (window->ViewportOwned)\n        {\n            // Synchronize window --> viewport in most situations\n            // Synchronize viewport -> window in case the platform window has been moved or resized from the OS/WM\n            if (window->Viewport->PlatformRequestMove)\n                window->Pos = window->Viewport->Pos;\n            else if (memcmp(&window->Viewport->Pos, &window->Pos, sizeof(window->Pos)) != 0)\n            {\n                viewport_rect_changed = true;\n                window->Viewport->Pos = window->Pos;\n            }\n\n            if (window->Viewport->PlatformRequestResize)\n                window->Size = window->SizeFull = window->Viewport->Size;\n            else if (memcmp(&window->Viewport->Size, &window->Size, sizeof(window->Size)) != 0)\n            {\n                viewport_rect_changed = true;\n                window->Viewport->Size = window->Size;\n            }\n\n            // The viewport may have changed monitor since the global update in UpdateViewportsNewFrame()\n            // Either a SetNextWindowPos() call in the current frame or a SetWindowPos() call in the previous frame may have this effect.\n            if (viewport_rect_changed)\n                UpdateViewportPlatformMonitor(window->Viewport);\n\n            // Update common viewport flags\n            ImGuiViewportFlags viewport_flags = (window->Viewport->Flags) & ~(ImGuiViewportFlags_TopMost | ImGuiViewportFlags_NoTaskBarIcon | ImGuiViewportFlags_NoDecoration);\n            const bool is_short_lived_floating_window = (flags & (ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) != 0;\n            if (flags & ImGuiWindowFlags_Tooltip)\n                viewport_flags |= ImGuiViewportFlags_TopMost;\n            if (g.IO.ConfigViewportsNoTaskBarIcon || is_short_lived_floating_window)\n                viewport_flags |= ImGuiViewportFlags_NoTaskBarIcon;\n            if (g.IO.ConfigViewportsNoDecoration || is_short_lived_floating_window)\n                viewport_flags |= ImGuiViewportFlags_NoDecoration;\n\n            // For popups and menus that may be protruding out of their parent viewport, we enable _NoFocusOnClick so that clicking on them\n            // won't steal the OS focus away from their parent window (which may be reflected in OS the title bar decoration).\n            // Setting _NoFocusOnClick would technically prevent us from bringing back to front in case they are being covered by an OS window from a different app, \n            // but it shouldn't be much of a problem considering those are already popups that are closed when clicking elsewhere.\n            if (is_short_lived_floating_window)\n                viewport_flags |= ImGuiViewportFlags_NoFocusOnAppearing | ImGuiViewportFlags_NoFocusOnClick;\n\n            // We can overwrite viewport flags using ImGuiWindowClass (advanced users)\n            // We don't default to the main viewport because.\n            if (window->WindowClass.ParentViewportId)\n                window->Viewport->ParentViewportId = window->WindowClass.ParentViewportId;\n            else if ((flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) && parent_window_in_stack)\n                window->Viewport->ParentViewportId = parent_window_in_stack->Viewport->ID;\n            else\n                window->Viewport->ParentViewportId = g.IO.ConfigViewportsNoDefaultParent ? 0 : IMGUI_VIEWPORT_DEFAULT_ID;\n            if (window->WindowClass.ViewportFlagsOverrideMask)\n                viewport_flags = (viewport_flags & ~window->WindowClass.ViewportFlagsOverrideMask) | (window->WindowClass.ViewportFlagsOverrideValue & window->WindowClass.ViewportFlagsOverrideMask);\n\n            // We also tell the back-end that clearing the platform window won't be necessary, as our window is filling the viewport and we have disabled BgAlpha\n            viewport_flags |= ImGuiViewportFlags_NoRendererClear;\n            window->Viewport->Flags = viewport_flags;\n        }\n\n        // Clamp position so window stays visible within its viewport or monitor\n        // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing.\n        ImRect viewport_rect = window->Viewport->GetRect();\n        if (!window_pos_set_by_api && !(flags & ImGuiWindowFlags_ChildWindow) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0)\n        {\n            ImVec2 clamp_padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding);\n            if (!window->ViewportOwned && viewport_rect.GetWidth() > 0 && viewport_rect.GetHeight() > 0.0f)\n                ClampWindowRect(window, viewport_rect, clamp_padding);\n            else if (window->ViewportOwned && g.PlatformIO.Monitors.Size > 0)\n            {\n                if (window->Viewport->PlatformMonitor == -1)\n                {\n                    // Fallback for \"lost\" window (e.g. a monitor disconnected): we move the window back over the main viewport\n                    SetWindowPos(window, g.Viewports[0]->Pos + style.DisplayWindowPadding, ImGuiCond_Always);\n                }\n                else\n                {\n                    ImGuiPlatformMonitor& monitor = g.PlatformIO.Monitors[window->Viewport->PlatformMonitor];\n                    ClampWindowRect(window, ImRect(monitor.WorkPos, monitor.WorkPos + monitor.WorkSize), clamp_padding);\n                }\n            }\n        }\n        window->Pos = ImFloor(window->Pos);\n\n        // Lock window rounding for the frame (so that altering them doesn't cause inconsistencies)\n        window->WindowRounding = (flags & ImGuiWindowFlags_ChildWindow) ? style.ChildRounding : ((flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiWindowFlags_Modal)) ? style.PopupRounding : style.WindowRounding;\n        if (window->ViewportOwned)\n            window->WindowRounding = 0.0f;\n\n        // Apply scrolling\n        window->Scroll = CalcNextScrollFromScrollTargetAndClamp(window, true);\n        window->ScrollTarget = ImVec2(FLT_MAX, FLT_MAX);\n\n        // Apply window focus (new and reactivated windows are moved to front)\n        bool want_focus = false;\n        if (window_just_activated_by_user && !(flags & ImGuiWindowFlags_NoFocusOnAppearing))\n        {\n            if (flags & ImGuiWindowFlags_Popup)\n                want_focus = true;\n            else if ((window->DockIsActive || (flags & ImGuiWindowFlags_ChildWindow) == 0) && !(flags & ImGuiWindowFlags_Tooltip))\n                want_focus = true;\n        }\n\n        // Decide if we are going to handle borders and resize grips\n        const bool handle_borders_and_resize_grips = (window->DockNodeAsHost || !window->DockIsActive);\n\n        // Handle manual resize: Resize Grips, Borders, Gamepad\n        int border_held = -1;\n        ImU32 resize_grip_col[4] = { 0 };\n        const int resize_grip_count = g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // 4\n        const float grip_draw_size = (float)(int)ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f);\n        if (handle_borders_and_resize_grips && !window->Collapsed)\n            UpdateManualResize(window, size_auto_fit, &border_held, resize_grip_count, &resize_grip_col[0]);\n        window->ResizeBorderHeld = (signed char)border_held;\n\n        // Synchronize window --> viewport again and one last time (clamping and manual resize may have affected either)\n        if (window->ViewportOwned)\n        {\n            if (!window->Viewport->PlatformRequestMove)\n                window->Viewport->Pos = window->Pos;\n            if (!window->Viewport->PlatformRequestResize)\n                window->Viewport->Size = window->Size;\n            viewport_rect = window->Viewport->GetRect();\n        }\n\n        // Save last known viewport position within the window itself (so it can be saved in .ini file and restored)\n        window->ViewportPos = window->Viewport->Pos;\n\n        // Default item width. Make it proportional to window size if window manually resizes\n        if (window->Size.x > 0.0f && !(flags & ImGuiWindowFlags_Tooltip) && !(flags & ImGuiWindowFlags_AlwaysAutoResize))\n            window->ItemWidthDefault = (float)(int)(window->Size.x * 0.65f);\n        else\n            window->ItemWidthDefault = (float)(int)(g.FontSize * 16.0f);\n\n        // DRAWING\n\n        // Setup draw list and outer clipping rectangle\n        window->DrawList->Clear();\n        window->DrawList->Flags = (g.Style.AntiAliasedLines ? ImDrawListFlags_AntiAliasedLines : 0) | (g.Style.AntiAliasedFill ? ImDrawListFlags_AntiAliasedFill : 0);\n        window->DrawList->PushTextureID(g.Font->ContainerAtlas->TexID);\n        if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup) && !window_is_child_tooltip)\n            PushClipRect(parent_window->ClipRect.Min, parent_window->ClipRect.Max, true);\n        else\n            PushClipRect(viewport_rect.Min, viewport_rect.Max, true);\n\n        // Draw modal or window list full viewport dimming background (for other viewports we'll render them in EndFrame)\n        const bool dim_bg_for_modal = (flags & ImGuiWindowFlags_Modal) && window == GetFrontMostPopupModal() && window->HiddenFramesCannotSkipItems <= 0;\n        const bool dim_bg_for_window_list = g.NavWindowingTargetAnim && ((window == g.NavWindowingTargetAnim->RootWindow) || (g.NavWindowingList && (window == g.NavWindowingList) && g.NavWindowingList->Viewport != g.NavWindowingTargetAnim->Viewport));\n        if (dim_bg_for_modal || dim_bg_for_window_list)\n        {\n            const ImU32 dim_bg_col = GetColorU32(dim_bg_for_modal ? ImGuiCol_ModalWindowDimBg : ImGuiCol_NavWindowingDimBg, g.DimBgRatio);\n            window->DrawList->AddRectFilled(viewport_rect.Min, viewport_rect.Max, dim_bg_col);\n        }\n\n        // Draw navigation selection/windowing rectangle background\n        if (dim_bg_for_window_list && window == g.NavWindowingTargetAnim)\n        {\n            ImRect bb = window->Rect();\n            bb.Expand(g.FontSize);\n            if (!bb.Contains(viewport_rect)) // Avoid drawing if the window covers all the viewport anyway\n                window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha * 0.25f), g.Style.WindowRounding);\n        }\n\n        // Draw window + handle manual resize\n        // As we highlight the title bar when want_focus is set, multiple reappearing windows will have have their title bar highlighted on their reappearing frame.\n        const float window_rounding = window->WindowRounding;\n        const float window_border_size = window->WindowBorderSize;\n        const ImGuiWindow* window_to_highlight = g.NavWindowingTarget ? g.NavWindowingTarget : g.NavWindow;\n        const bool title_bar_is_highlight = want_focus || (window_to_highlight && (window->RootWindowForTitleBarHighlight == window_to_highlight->RootWindowForTitleBarHighlight || (window->DockNode && window->DockNode == window_to_highlight->DockNode)));\n        const ImRect title_bar_rect = window->TitleBarRect();\n        if (window->Collapsed)\n        {\n            // Title bar only\n            float backup_border_size = style.FrameBorderSize;\n            g.Style.FrameBorderSize = window->WindowBorderSize;\n            ImU32 title_bar_col = GetColorU32((title_bar_is_highlight && !g.NavDisableHighlight) ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBgCollapsed);\n            RenderFrame(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, true, window_rounding);\n            g.Style.FrameBorderSize = backup_border_size;\n        }\n        else\n        {\n            // Window background\n            if (!(flags & ImGuiWindowFlags_NoBackground))\n            {\n                bool is_docking_transparent_payload = false;\n                if (g.DragDropActive && (g.FrameCount - g.DragDropAcceptFrameCount) <= 1 && g.IO.ConfigDockingTransparentPayload)\n                    if (g.DragDropPayload.IsDataType(IMGUI_PAYLOAD_TYPE_WINDOW) && *(ImGuiWindow**)g.DragDropPayload.Data == window)\n                        is_docking_transparent_payload = true;\n\n                ImU32 bg_col = GetColorU32(GetWindowBgColorIdxFromFlags(flags));\n                if (window->ViewportOwned)\n                {\n                    // No alpha\n                    bg_col = (bg_col | IM_COL32_A_MASK);\n                    if (is_docking_transparent_payload)\n                        window->Viewport->Alpha *= DOCKING_TRANSPARENT_PAYLOAD_ALPHA;\n                }\n                else\n                {\n                    // Adjust alpha. For docking\n                    float alpha = 1.0f;\n                    if (g.NextWindowData.BgAlphaCond != 0)\n                        alpha = g.NextWindowData.BgAlphaVal;\n                    if (is_docking_transparent_payload)\n                        alpha *= DOCKING_TRANSPARENT_PAYLOAD_ALPHA;\n                    if (alpha != 1.0f)\n                        bg_col = (bg_col & ~IM_COL32_A_MASK) | (IM_F32_TO_INT8_SAT(alpha) << IM_COL32_A_SHIFT);\n                }\n                window->DrawList->AddRectFilled(window->Pos + ImVec2(0, window->TitleBarHeight()), window->Pos + window->Size, bg_col, window_rounding, (flags & ImGuiWindowFlags_NoTitleBar) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Bot);\n            }\n            g.NextWindowData.BgAlphaCond = 0;\n\n            // Title bar\n            // (when docked, DockNode are drawing their own title bar. Individual windows however do NOT set the _NoTitleBar flag, \n            // in order for their pos/size to be matching their undocking state.)\n            if (!(flags & ImGuiWindowFlags_NoTitleBar) && !window->DockIsActive)\n            {\n                ImU32 title_bar_col = GetColorU32(title_bar_is_highlight ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg);\n                window->DrawList->AddRectFilled(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, window_rounding, ImDrawCornerFlags_Top);\n            }\n\n            // Menu bar\n            if (flags & ImGuiWindowFlags_MenuBar)\n            {\n                ImRect menu_bar_rect = window->MenuBarRect();\n                menu_bar_rect.ClipWith(window->Rect());  // Soft clipping, in particular child window don't have minimum size covering the menu bar so this is useful for them.\n                window->DrawList->AddRectFilled(menu_bar_rect.Min+ImVec2(window_border_size,0), menu_bar_rect.Max-ImVec2(window_border_size,0), GetColorU32(ImGuiCol_MenuBarBg), (flags & ImGuiWindowFlags_NoTitleBar) ? window_rounding : 0.0f, ImDrawCornerFlags_Top);\n                if (style.FrameBorderSize > 0.0f && menu_bar_rect.Max.y < window->Pos.y + window->Size.y)\n                    window->DrawList->AddLine(menu_bar_rect.GetBL(), menu_bar_rect.GetBR(), GetColorU32(ImGuiCol_Border), style.FrameBorderSize);\n            }\n\n            // Docking: Unhide tab bar (small triangle in the corner)\n            if (window->DockNode && window->DockNode->IsHiddenTabBar() && !window->DockNode->IsNoTabBar())\n            {\n                float unhide_sz_draw = ImFloor(g.FontSize * 0.70f);\n                float unhide_sz_hit = ImFloor(g.FontSize * 0.55f);\n                ImVec2 p = window->DockNode->Pos;\n                ImRect r(p, p + ImVec2(unhide_sz_hit, unhide_sz_hit));\n                bool hovered, held;\n                if (ButtonBehavior(r, window->GetID(\"#UNHIDE\"), &hovered, &held, ImGuiButtonFlags_FlattenChildren))\n                    window->DockNode->WantHiddenTabBarToggle = true;\n                // FIXME-DOCK: Ideally we'd use ImGuiCol_TitleBgActive/ImGuiCol_TitleBg here, but neither is guaranteed to be visible enough at this sort of size..\n                ImU32 col = GetColorU32(((held && hovered) || (window->DockNode->IsFocused && !hovered)) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);\n                window->DrawList->AddTriangleFilled(p, p + ImVec2(unhide_sz_draw, 0.0f), p + ImVec2(0.0f, unhide_sz_draw), col);\n            }\n\n            // Scrollbars\n            if (window->ScrollbarX)\n                Scrollbar(ImGuiAxis_X);\n            if (window->ScrollbarY)\n                Scrollbar(ImGuiAxis_Y);\n\n            // Render resize grips (after their input handling so we don't have a frame of latency)\n            if (handle_borders_and_resize_grips && !(flags & ImGuiWindowFlags_NoResize))\n            {\n                for (int resize_grip_n = 0; resize_grip_n < resize_grip_count; resize_grip_n++)\n                {\n                    const ImGuiResizeGripDef& grip = resize_grip_def[resize_grip_n];\n                    const ImVec2 corner = ImLerp(window->Pos, window->Pos + window->Size, grip.CornerPosN);\n                    window->DrawList->PathLineTo(corner + grip.InnerDir * ((resize_grip_n & 1) ? ImVec2(window_border_size, grip_draw_size) : ImVec2(grip_draw_size, window_border_size)));\n                    window->DrawList->PathLineTo(corner + grip.InnerDir * ((resize_grip_n & 1) ? ImVec2(grip_draw_size, window_border_size) : ImVec2(window_border_size, grip_draw_size)));\n                    window->DrawList->PathArcToFast(ImVec2(corner.x + grip.InnerDir.x * (window_rounding + window_border_size), corner.y + grip.InnerDir.y * (window_rounding + window_border_size)), window_rounding, grip.AngleMin12, grip.AngleMax12);\n                    window->DrawList->PathFillConvex(resize_grip_col[resize_grip_n]);\n                }\n            }\n\n            // Borders (for dock node host they will be rendered over after the tab bar)\n            if (handle_borders_and_resize_grips && !window->DockNodeAsHost)\n                RenderOuterBorders(window);\n        }\n\n        // Store a backup of SizeFull which we will use next frame to decide if we need scrollbars.\n        window->SizeFullAtLastBegin = window->SizeFull;\n\n        // Update various regions. Variables they depends on are set above in this function.\n        // FIXME: window->ContentsRegionRect.Max is currently very misleading / partly faulty, but some BeginChild() patterns relies on it.\n        // NB: WindowBorderSize is included in WindowPadding _and_ ScrollbarSizes so we need to cancel one out.\n        window->ContentsRegionRect.Min.x = window->Pos.x - window->Scroll.x + window->WindowPadding.x;\n        window->ContentsRegionRect.Min.y = window->Pos.y - window->Scroll.y + window->WindowPadding.y + window->TitleBarHeight() + window->MenuBarHeight();\n        window->ContentsRegionRect.Max.x = window->Pos.x - window->Scroll.x - window->WindowPadding.x + (window->SizeContentsExplicit.x != 0.0f ? window->SizeContentsExplicit.x : (window->Size.x - window->ScrollbarSizes.x + ImMin(window->ScrollbarSizes.x, window->WindowBorderSize)));\n        window->ContentsRegionRect.Max.y = window->Pos.y - window->Scroll.y - window->WindowPadding.y + (window->SizeContentsExplicit.y != 0.0f ? window->SizeContentsExplicit.y : (window->Size.y - window->ScrollbarSizes.y + ImMin(window->ScrollbarSizes.y, window->WindowBorderSize)));\n\n        // Save clipped aabb so we can access it in constant-time in FindHoveredWindow()\n        window->OuterRectClipped = window->Rect();\n        if (window->DockIsActive)\n            window->OuterRectClipped.Min.y += window->TitleBarHeight();\n        window->OuterRectClipped.ClipWith(window->ClipRect);\n\n        // Inner rectangle\n        // We set this up after processing the resize grip so that our clip rectangle doesn't lag by a frame\n        // Note that if our window is collapsed we will end up with an inverted (~null) clipping rectangle which is the correct behavior.\n        window->InnerMainRect.Min.x = title_bar_rect.Min.x + window->WindowBorderSize;\n        window->InnerMainRect.Min.y = title_bar_rect.Max.y + window->MenuBarHeight() + (((flags & ImGuiWindowFlags_MenuBar) || !(flags & ImGuiWindowFlags_NoTitleBar)) ? style.FrameBorderSize : window->WindowBorderSize);\n        window->InnerMainRect.Max.x = window->Pos.x + window->Size.x - ImMax(window->ScrollbarSizes.x, window->WindowBorderSize);\n        window->InnerMainRect.Max.y = window->Pos.y + window->Size.y - ImMax(window->ScrollbarSizes.y, window->WindowBorderSize);\n\n        // Inner clipping rectangle\n        // Force round operator last to ensure that e.g. (int)(max.x-min.x) in user's render code produce correct result.\n        window->InnerClipRect.Min.x = ImFloor(0.5f + window->InnerMainRect.Min.x + ImMax(0.0f, ImFloor(window->WindowPadding.x * 0.5f - window->WindowBorderSize)));\n        window->InnerClipRect.Min.y = ImFloor(0.5f + window->InnerMainRect.Min.y);\n        window->InnerClipRect.Max.x = ImFloor(0.5f + window->InnerMainRect.Max.x - ImMax(0.0f, ImFloor(window->WindowPadding.x * 0.5f - window->WindowBorderSize)));\n        window->InnerClipRect.Max.y = ImFloor(0.5f + window->InnerMainRect.Max.y);\n\n        // Setup drawing context\n        // (NB: That term \"drawing context / DC\" lost its meaning a long time ago. Initially was meant to hold transient data only. Nowadays difference between window-> and window->DC-> is dubious.)\n        window->DC.Indent.x = 0.0f + window->WindowPadding.x - window->Scroll.x;\n        window->DC.GroupOffset.x = 0.0f;\n        window->DC.ColumnsOffset.x = 0.0f;\n        window->DC.CursorStartPos = window->Pos + ImVec2(window->DC.Indent.x + window->DC.ColumnsOffset.x, window->TitleBarHeight() + window->MenuBarHeight() + window->WindowPadding.y - window->Scroll.y);\n        window->DC.CursorPos = window->DC.CursorStartPos;\n        window->DC.CursorPosPrevLine = window->DC.CursorPos;\n        window->DC.CursorMaxPos = window->DC.CursorStartPos;\n        window->DC.CurrentLineSize = window->DC.PrevLineSize = ImVec2(0.0f, 0.0f);\n        window->DC.CurrentLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f;\n        window->DC.NavHideHighlightOneFrame = false;\n        window->DC.NavHasScroll = (GetWindowScrollMaxY(window) > 0.0f);\n        window->DC.NavLayerActiveMask = window->DC.NavLayerActiveMaskNext;\n        window->DC.NavLayerActiveMaskNext = 0x00;\n        window->DC.MenuBarAppending = false;\n        window->DC.ChildWindows.resize(0);\n        window->DC.LayoutType = ImGuiLayoutType_Vertical;\n        window->DC.ParentLayoutType = parent_window ? parent_window->DC.LayoutType : ImGuiLayoutType_Vertical;\n        window->DC.FocusCounterAll = window->DC.FocusCounterTab = -1;\n        window->DC.ItemFlags = parent_window ? parent_window->DC.ItemFlags : ImGuiItemFlags_Default_;\n        window->DC.ItemWidth = window->ItemWidthDefault;\n        window->DC.TextWrapPos = -1.0f; // disabled\n        window->DC.ItemFlagsStack.resize(0);\n        window->DC.ItemWidthStack.resize(0);\n        window->DC.TextWrapPosStack.resize(0);\n        window->DC.CurrentColumns = NULL;\n        window->DC.TreeDepth = 0;\n        window->DC.TreeStoreMayJumpToParentOnPop = 0x00;\n        window->DC.StateStorage = &window->StateStorage;\n        window->DC.GroupStack.resize(0);\n        window->MenuColumns.Update(3, style.ItemSpacing.x, window_just_activated_by_user);\n\n        if ((flags & ImGuiWindowFlags_ChildWindow) && (window->DC.ItemFlags != parent_window->DC.ItemFlags))\n        {\n            window->DC.ItemFlags = parent_window->DC.ItemFlags;\n            window->DC.ItemFlagsStack.push_back(window->DC.ItemFlags);\n        }\n\n        if (window->AutoFitFramesX > 0)\n            window->AutoFitFramesX--;\n        if (window->AutoFitFramesY > 0)\n            window->AutoFitFramesY--;\n\n        // Apply focus (we need to call FocusWindow() AFTER setting DC.CursorStartPos so our initial navigation reference rectangle can start around there)\n        if (want_focus)\n        {\n            FocusWindow(window);\n            NavInitWindow(window, false);\n        }\n\n        // Close from platform window\n        if (p_open != NULL && window->Viewport->PlatformRequestClose && window->Viewport != GetMainViewport())\n        {\n            if (!window->DockIsActive || window->DockTabIsVisible)\n            {\n                window->Viewport->PlatformRequestClose = false;\n                g.NavWindowingToggleLayer = false; // Assume user mapped PlatformRequestClose on ALT-F4 so we disable ALT for menu toggle. False positive not an issue.\n                //IMGUI_DEBUG_LOG(\"Window '%s' PlatformRequestClose\\n\", window->Name);\n                *p_open = false;\n            }\n        }\n\n        // Title bar\n        if (!(flags & ImGuiWindowFlags_NoTitleBar) && !window->DockIsActive)\n        {\n            // Close & collapse button are on layer 1 (same as menus) and don't default focus\n            const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags;\n            window->DC.ItemFlags |= ImGuiItemFlags_NoNavDefaultFocus;\n            window->DC.NavLayerCurrent = ImGuiNavLayer_Menu;\n            window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu);\n\n            // Collapse button\n            if (!(flags & ImGuiWindowFlags_NoCollapse))\n                if (CollapseButton(window->GetID(\"#COLLAPSE\"), window->Pos, NULL))\n                    window->WantCollapseToggle = true; // Defer collapsing to next frame as we are too far in the Begin() function\n\n            // Close button\n            if (p_open != NULL)\n            {\n                const float rad = g.FontSize * 0.5f;\n                if (CloseButton(window->GetID(\"#CLOSE\"), ImVec2(window->Pos.x + window->Size.x - style.FramePadding.x - rad, window->Pos.y + style.FramePadding.y + rad), rad + 1))\n                    *p_open = false;\n            }\n\n            window->DC.NavLayerCurrent = ImGuiNavLayer_Main;\n            window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main);\n            window->DC.ItemFlags = item_flags_backup;\n\n            // Title bar text (with: horizontal alignment, avoiding collapse/close button, optional \"unsaved document\" marker)\n            // FIXME: Refactor text alignment facilities along with RenderText helpers, this is too much code..\n            const char* UNSAVED_DOCUMENT_MARKER = \"*\";\n            float marker_size_x = (flags & ImGuiWindowFlags_UnsavedDocument) ? CalcTextSize(UNSAVED_DOCUMENT_MARKER, NULL, false).x : 0.0f;\n            ImVec2 text_size = CalcTextSize(name, NULL, true) + ImVec2(marker_size_x, 0.0f);\n            ImRect text_r = title_bar_rect;\n            float pad_left = (flags & ImGuiWindowFlags_NoCollapse) ? style.FramePadding.x : (style.FramePadding.x + g.FontSize + style.ItemInnerSpacing.x);\n            float pad_right = (p_open == NULL)                     ? style.FramePadding.x : (style.FramePadding.x + g.FontSize + style.ItemInnerSpacing.x);\n            if (style.WindowTitleAlign.x > 0.0f)\n                pad_right = ImLerp(pad_right, pad_left, style.WindowTitleAlign.x);\n            text_r.Min.x += pad_left;\n            text_r.Max.x -= pad_right;\n            ImRect clip_rect = text_r;\n            clip_rect.Max.x = window->Pos.x + window->Size.x - (p_open ? title_bar_rect.GetHeight() - 3 : style.FramePadding.x); // Match the size of CloseButton()\n            RenderTextClipped(text_r.Min, text_r.Max, name, NULL, &text_size, style.WindowTitleAlign, &clip_rect);\n            if (flags & ImGuiWindowFlags_UnsavedDocument)\n            {\n                ImVec2 marker_pos = ImVec2(ImMax(text_r.Min.x, text_r.Min.x + (text_r.GetWidth() - text_size.x) * style.WindowTitleAlign.x) + text_size.x, text_r.Min.y) + ImVec2(2 - marker_size_x, 0.0f);\n                ImVec2 off = ImVec2(0.0f, (float)(int)(-g.FontSize * 0.25f));\n                RenderTextClipped(marker_pos + off, text_r.Max + off, UNSAVED_DOCUMENT_MARKER, NULL, NULL, ImVec2(0, style.WindowTitleAlign.y), &clip_rect);\n            }\n        }\n\n        // Clear hit test shape every frame\n        window->HitTestHoleSize.x = window->HitTestHoleSize.y = 0;\n\n        // Pressing CTRL+C while holding on a window copy its content to the clipboard\n        // This works but 1. doesn't handle multiple Begin/End pairs, 2. recursing into another Begin/End pair - so we need to work that out and add better logging scope.\n        // Maybe we can support CTRL+C on every element?\n        /*\n        if (g.ActiveId == move_id)\n            if (g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_C))\n                LogToClipboard();\n        */\n\n        if (g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable)\n        {\n            // Docking: Dragging a dockable window (or any of its child) turns it into a drag and drop source.\n            // We need to do this _before_ we overwrite window->DC.LastItemId below because BeginAsDockableDragDropSource() also overwrites it.\n            if ((g.ActiveId == window->MoveId) && (g.IO.ConfigDockingWithShift == g.IO.KeyShift))\n                if ((window->Flags & ImGuiWindowFlags_NoMove) == 0)\n                    if ((window->RootWindow->Flags & (ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDocking)) == 0)\n                        BeginAsDockableDragDropSource(window);\n\n            // Docking: Any dockable window can act as a target. For dock node hosts we call BeginAsDockableDragDropTarget() in DockNodeUpdate() instead.\n            if (g.DragDropActive && !(flags & ImGuiWindowFlags_NoDocking))\n                if (g.MovingWindow == NULL || g.MovingWindow->RootWindow != window)\n                    if ((window == window->RootWindow) && !(window->Flags & ImGuiWindowFlags_DockNodeHost))\n                        BeginAsDockableDragDropTarget(window);\n        }\n\n        // We fill last item data based on Title Bar/Tab, in order for IsItemHovered() and IsItemActive() to be usable after Begin().\n        // This is useful to allow creating context menus on title bar only, etc.\n        if (window->DockIsActive)\n        {\n            window->DC.LastItemId = window->ID;\n            window->DC.LastItemStatusFlags = window->DockTabItemStatusFlags;\n            window->DC.LastItemRect = window->DockTabItemRect;\n        }\n        else\n        {\n            window->DC.LastItemId = window->MoveId;\n            window->DC.LastItemStatusFlags = IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0;\n            window->DC.LastItemRect = title_bar_rect;\n        }\n\n#ifdef IMGUI_ENABLE_TEST_ENGINE\n        if (!(window->Flags & ImGuiWindowFlags_NoTitleBar))\n            IMGUI_TEST_ENGINE_ITEM_ADD(window->DC.LastItemRect, window->DC.LastItemId);\n#endif\n    }\n    else\n    {\n        // Append\n        SetCurrentViewport(window, window->Viewport);\n        SetCurrentWindow(window);\n    }\n\n    if (!(flags & ImGuiWindowFlags_DockNodeHost))\n        PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true);\n\n    // Clear 'accessed' flag last thing (After PushClipRect which will set the flag. We want the flag to stay false when the default \"Debug\" window is unused)\n    if (first_begin_of_the_frame)\n        window->WriteAccessed = false;\n\n    window->BeginCount++;\n    g.NextWindowData.Clear();\n\n    // When we are about to select this tab (which will only be visible on the _next frame_), flag it with a non-zero HiddenFramesCannotSkipItems.\n    // This will have the important effect of actually returning true in Begin() and not setting SkipItems, allowing an earlier submission of the window contents.\n    // This is analogous to regular windows being hidden from one frame. \n    // It is especially important as e.g. nested TabBars would otherwise generate flicker in the form of one empty frame, or focus requests won't be processed.\n    if (window->DockIsActive && !window->DockTabIsVisible)\n    {\n        if (window->LastFrameJustFocused == g.FrameCount)\n            window->HiddenFramesCannotSkipItems = 1;\n        else\n            window->HiddenFramesCanSkipItems = 1;\n    }\n\n    if (flags & ImGuiWindowFlags_ChildWindow)\n    {\n        // Child window can be out of sight and have \"negative\" clip windows.\n        // Mark them as collapsed so commands are skipped earlier (we can't manually collapse them because they have no title bar).\n        IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0 || (window->DockIsActive));\n        if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0)\n            if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y)\n                window->HiddenFramesCanSkipItems = 1;\n\n        // Completely hide along with parent or if parent is collapsed\n        if (parent_window && (parent_window->Collapsed || parent_window->Hidden))\n            window->HiddenFramesCanSkipItems = 1;\n    }\n\n    // Don't render if style alpha is 0.0 at the time of Begin(). This is arbitrary and inconsistent but has been there for a long while (may remove at some point)\n    if (style.Alpha <= 0.0f)\n        window->HiddenFramesCanSkipItems = 1;\n\n    // Update the Hidden flag\n    window->Hidden = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0);\n\n    // Update the SkipItems flag, used to early out of all items functions (no layout required)\n    bool skip_items = false;\n    if (window->Collapsed || !window->Active || window->Hidden)\n        if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0 && window->HiddenFramesCannotSkipItems <= 0)\n            skip_items = true;\n    window->SkipItems = skip_items;\n\n    return !skip_items;\n}\n\n// Old Begin() API with 5 parameters, avoid calling this version directly! Use SetNextWindowSize()/SetNextWindowBgAlpha() + Begin() instead.\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\nbool ImGui::Begin(const char* name, bool* p_open, const ImVec2& size_first_use, float bg_alpha_override, ImGuiWindowFlags flags)\n{\n    // Old API feature: we could pass the initial window size as a parameter. This was misleading because it only had an effect if the window didn't have data in the .ini file.\n    if (size_first_use.x != 0.0f || size_first_use.y != 0.0f)\n        SetNextWindowSize(size_first_use, ImGuiCond_FirstUseEver);\n\n    // Old API feature: override the window background alpha with a parameter.\n    if (bg_alpha_override >= 0.0f)\n        SetNextWindowBgAlpha(bg_alpha_override);\n\n    return Begin(name, p_open, flags);\n}\n#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n\nvoid ImGui::End()\n{\n    ImGuiContext& g = *GImGui;\n\n    if (g.CurrentWindowStack.Size <= 1 && g.FrameScopePushedImplicitWindow)\n    {\n        IM_ASSERT(g.CurrentWindowStack.Size > 1 && \"Calling End() too many times!\");\n        return; // FIXME-ERRORHANDLING\n    }\n    IM_ASSERT(g.CurrentWindowStack.Size > 0);\n\n    ImGuiWindow* window = g.CurrentWindow;\n\n    if (window->DC.CurrentColumns != NULL)\n        EndColumns();\n    if (!(window->Flags & ImGuiWindowFlags_DockNodeHost))   // Pop inner window clip rectangle\n        PopClipRect();\n\n    // Stop logging\n    if (!(window->Flags & ImGuiWindowFlags_ChildWindow))    // FIXME: add more options for scope of logging\n        LogFinish();\n\n    // Docking: report contents sizes to parent to allow for auto-resize\n    if (window->DockNode && window->DockTabIsVisible)\n        if (ImGuiWindow* host_window = window->DockNode->HostWindow)         // FIXME-DOCK\n            host_window->DC.CursorMaxPos = window->DC.CursorMaxPos + window->WindowPadding - host_window->WindowPadding;\n\n    // Pop from window stack\n    g.CurrentWindowStack.pop_back();\n    if (window->Flags & ImGuiWindowFlags_Popup)\n        g.BeginPopupStack.pop_back();\n    CheckStacksSize(window, false);\n    SetCurrentWindow(g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back());\n    if (g.CurrentWindow)\n        SetCurrentViewport(g.CurrentWindow, g.CurrentWindow->Viewport);\n}\n\nvoid ImGui::BringWindowToFocusFront(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.WindowsFocusOrder.back() == window)\n        return;\n    for (int i = g.WindowsFocusOrder.Size - 2; i >= 0; i--) // We can ignore the front most window\n        if (g.WindowsFocusOrder[i] == window)\n        {\n            memmove(&g.WindowsFocusOrder[i], &g.WindowsFocusOrder[i + 1], (size_t)(g.WindowsFocusOrder.Size - i - 1) * sizeof(ImGuiWindow*));\n            g.WindowsFocusOrder[g.WindowsFocusOrder.Size - 1] = window;\n            break;\n        }\n}\n\nvoid ImGui::BringWindowToDisplayFront(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* current_front_window = g.Windows.back();\n    if (current_front_window == window || current_front_window->RootWindow == window)\n        return;\n    for (int i = g.Windows.Size - 2; i >= 0; i--) // We can ignore the front most window\n        if (g.Windows[i] == window)\n        {\n            memmove(&g.Windows[i], &g.Windows[i + 1], (size_t)(g.Windows.Size - i - 1) * sizeof(ImGuiWindow*));\n            g.Windows[g.Windows.Size - 1] = window;\n            break;\n        }\n}\n\nvoid ImGui::BringWindowToDisplayBack(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.Windows[0] == window)\n        return;\n    for (int i = 0; i < g.Windows.Size; i++)\n        if (g.Windows[i] == window)\n        {\n            memmove(&g.Windows[1], &g.Windows[0], (size_t)i * sizeof(ImGuiWindow*));\n            g.Windows[0] = window;\n            break;\n        }\n}\n\n// Moving window to front of display and set focus (which happens to be back of our sorted list)\nvoid ImGui::FocusWindow(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n\n    if (g.NavWindow != window)\n    {\n        g.NavWindow = window;\n        if (window && g.NavDisableMouseHover)\n            g.NavMousePosDirty = true;\n        g.NavInitRequest = false;\n        g.NavId = window ? window->NavLastIds[0] : 0; // Restore NavId\n        g.NavIdIsAlive = false;\n        g.NavLayer = ImGuiNavLayer_Main;\n        //IMGUI_DEBUG_LOG(\"FocusWindow(\\\"%s\\\")\\n\", window ? window->Name : NULL);\n    }\n\n    // Close popups if any\n    ClosePopupsOverWindow(window, false);\n\n    // Passing NULL allow to disable keyboard focus\n    if (!window)\n        return;\n    window->LastFrameJustFocused = g.FrameCount;\n\n    // Select in dock node\n    if (window->DockNode && window->DockNode->TabBar)\n        window->DockNode->TabBar->SelectedTabId = window->DockNode->TabBar->NextSelectedTabId = window->ID;\n\n    // Move the root window to the top of the pile\n    if (window->RootWindow)\n        window = window->RootWindow;\n\n    // Steal focus on active widgets\n    if (window->Flags & ImGuiWindowFlags_Popup) // FIXME: This statement should be unnecessary. Need further testing before removing it..\n        if (g.ActiveId != 0 && g.ActiveIdWindow && g.ActiveIdWindow->RootWindow != window)\n            ClearActiveID();\n\n    // Bring to front\n    BringWindowToFocusFront(window);\n    if (!(window->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus))\n        BringWindowToDisplayFront(window);\n}\n\nvoid ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window)\n{\n    ImGuiContext& g = *GImGui;\n\n    int start_idx = g.WindowsFocusOrder.Size - 1;\n    if (under_this_window != NULL)\n    {\n        int under_this_window_idx = FindWindowFocusIndex(under_this_window);\n        if (under_this_window_idx != -1)\n            start_idx = under_this_window_idx - 1;\n    }\n    for (int i = start_idx; i >= 0; i--)\n    {\n        // We may later decide to test for different NoXXXInputs based on the active navigation input (mouse vs nav) but that may feel more confusing to the user.\n        ImGuiWindow* window = g.WindowsFocusOrder[i];\n        if (window != ignore_window && window->WasActive && !(window->Flags & ImGuiWindowFlags_ChildWindow))\n            if ((window->Flags & (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) != (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs))\n            {\n                // FIXME-DOCKING: This is failing (lagging by one frame) for docked windows. \n                // If A and B are docked into window and B disappear, at the NewFrame() call site window->NavLastChildNavWindow will still point to B.\n                // We might leverage the tab order implicitly stored in window->DockNodeAsHost->TabBar (essentially the 'most_recently_selected_tab' code in tab bar will do that but on next update)\n                // to tell which is the \"previous\" window. Or we may leverage 'LastFrameFocused/LastFrameJustFocused' and have this function handle child window itself?\n                ImGuiWindow* focus_window = NavRestoreLastChildNavWindow(window);\n                FocusWindow(focus_window);\n                return;\n            }\n    }\n    FocusWindow(NULL);\n}\n\nvoid ImGui::SetNextItemWidth(float item_width)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.NextItemWidth = item_width;\n}\n\nvoid ImGui::PushItemWidth(float item_width)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.ItemWidth = (item_width == 0.0f ? window->ItemWidthDefault : item_width);\n    window->DC.ItemWidthStack.push_back(window->DC.ItemWidth);\n}\n\nvoid ImGui::PushMultiItemsWidths(int components, float w_full)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    const ImGuiStyle& style = GImGui->Style;\n    const float w_item_one  = ImMax(1.0f, (float)(int)((w_full - (style.ItemInnerSpacing.x) * (components-1)) / (float)components));\n    const float w_item_last = ImMax(1.0f, (float)(int)(w_full - (w_item_one + style.ItemInnerSpacing.x) * (components-1)));\n    window->DC.ItemWidthStack.push_back(w_item_last);\n    for (int i = 0; i < components-1; i++)\n        window->DC.ItemWidthStack.push_back(w_item_one);\n    window->DC.ItemWidth = window->DC.ItemWidthStack.back();\n}\n\nvoid ImGui::PopItemWidth()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.ItemWidthStack.pop_back();\n    window->DC.ItemWidth = window->DC.ItemWidthStack.empty() ? window->ItemWidthDefault : window->DC.ItemWidthStack.back();\n}\n\n// Calculate default item width given value passed to PushItemWidth() or SetNextItemWidth(),\n// Then consume the \nfloat ImGui::GetNextItemWidth()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    float w;\n    if (window->DC.NextItemWidth != FLT_MAX)\n    {\n        w = window->DC.NextItemWidth;\n        window->DC.NextItemWidth = FLT_MAX;\n    }\n    else\n    {\n        w = window->DC.ItemWidth;\n    }\n    if (w < 0.0f)\n    {\n        float region_max_x = GetContentRegionMaxScreen().x;\n        w = ImMax(1.0f, region_max_x - window->DC.CursorPos.x + w);\n    }\n    w = (float)(int)w;\n    return w;\n}\n\n// Calculate item width *without* popping/consuming NextItemWidth if it was set.\n// (rarely used, which is why we avoid calling this from GetNextItemWidth() and instead do a backup/restore here)\nfloat ImGui::CalcItemWidth()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    float backup_next_item_width = window->DC.NextItemWidth;\n    float w = GetNextItemWidth();\n    window->DC.NextItemWidth = backup_next_item_width;\n    return w;\n}\n\n// [Internal] Calculate full item size given user provided 'size' parameter and default width/height. Default width is often == GetNextItemWidth().\n// Those two functions CalcItemWidth vs CalcItemSize are awkwardly named because they are not fully symmetrical.\n// Note that only CalcItemWidth() is publicly exposed.\n// The 4.0f here may be changed to match GetNextItemWidth() and/or BeginChild() (right now we have a mismatch which is harmless but undesirable)\nImVec2 ImGui::CalcItemSize(ImVec2 size, float default_w, float default_h)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n\n    ImVec2 region_max;\n    if (size.x < 0.0f || size.y < 0.0f)\n        region_max = GetContentRegionMaxScreen();\n\n    if (size.x == 0.0f)\n        size.x = default_w;\n    else if (size.x < 0.0f)\n        size.x = ImMax(4.0f, region_max.x - window->DC.CursorPos.x + size.x);\n\n    if (size.y == 0.0f)\n        size.y = default_h;\n    else if (size.y < 0.0f)\n        size.y = ImMax(4.0f, region_max.y - window->DC.CursorPos.y + size.y);\n\n    return size;\n}\n\nvoid ImGui::SetCurrentFont(ImFont* font)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(font && font->IsLoaded());    // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?\n    IM_ASSERT(font->Scale > 0.0f);\n    g.Font = font;\n    g.FontBaseSize = ImMax(1.0f, g.IO.FontGlobalScale * g.Font->FontSize * g.Font->Scale);\n    g.FontSize = g.CurrentWindow ? g.CurrentWindow->CalcFontSize() : 0.0f;\n\n    ImFontAtlas* atlas = g.Font->ContainerAtlas;\n    g.DrawListSharedData.TexUvWhitePixel = atlas->TexUvWhitePixel;\n    g.DrawListSharedData.Font = g.Font;\n    g.DrawListSharedData.FontSize = g.FontSize;\n}\n\nvoid ImGui::PushFont(ImFont* font)\n{\n    ImGuiContext& g = *GImGui;\n    if (!font)\n        font = GetDefaultFont();\n    SetCurrentFont(font);\n    g.FontStack.push_back(font);\n    g.CurrentWindow->DrawList->PushTextureID(font->ContainerAtlas->TexID);\n}\n\nvoid  ImGui::PopFont()\n{\n    ImGuiContext& g = *GImGui;\n    g.CurrentWindow->DrawList->PopTextureID();\n    g.FontStack.pop_back();\n    SetCurrentFont(g.FontStack.empty() ? GetDefaultFont() : g.FontStack.back());\n}\n\nvoid ImGui::PushItemFlag(ImGuiItemFlags option, bool enabled)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (enabled)\n        window->DC.ItemFlags |= option;\n    else\n        window->DC.ItemFlags &= ~option;\n    window->DC.ItemFlagsStack.push_back(window->DC.ItemFlags);\n}\n\nvoid ImGui::PopItemFlag()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.ItemFlagsStack.pop_back();\n    window->DC.ItemFlags = window->DC.ItemFlagsStack.empty() ? ImGuiItemFlags_Default_ : window->DC.ItemFlagsStack.back();\n}\n\n// FIXME: Look into renaming this once we have settled the new Focus/Activation/TabStop system.\nvoid ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus)\n{\n    PushItemFlag(ImGuiItemFlags_NoTabStop, !allow_keyboard_focus);\n}\n\nvoid ImGui::PopAllowKeyboardFocus()\n{\n    PopItemFlag();\n}\n\nvoid ImGui::PushButtonRepeat(bool repeat)\n{\n    PushItemFlag(ImGuiItemFlags_ButtonRepeat, repeat);\n}\n\nvoid ImGui::PopButtonRepeat()\n{\n    PopItemFlag();\n}\n\nvoid ImGui::PushTextWrapPos(float wrap_pos_x)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.TextWrapPos = wrap_pos_x;\n    window->DC.TextWrapPosStack.push_back(wrap_pos_x);\n}\n\nvoid ImGui::PopTextWrapPos()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.TextWrapPosStack.pop_back();\n    window->DC.TextWrapPos = window->DC.TextWrapPosStack.empty() ? -1.0f : window->DC.TextWrapPosStack.back();\n}\n\n// FIXME: This may incur a round-trip (if the end user got their data from a float4) but eventually we aim to store the in-flight colors as ImU32\nvoid ImGui::PushStyleColor(ImGuiCol idx, ImU32 col)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiColorMod backup;\n    backup.Col = idx;\n    backup.BackupValue = g.Style.Colors[idx];\n    g.ColorModifiers.push_back(backup);\n    g.Style.Colors[idx] = ColorConvertU32ToFloat4(col);\n}\n\nvoid ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiColorMod backup;\n    backup.Col = idx;\n    backup.BackupValue = g.Style.Colors[idx];\n    g.ColorModifiers.push_back(backup);\n    g.Style.Colors[idx] = col;\n}\n\nvoid ImGui::PopStyleColor(int count)\n{\n    ImGuiContext& g = *GImGui;\n    while (count > 0)\n    {\n        ImGuiColorMod& backup = g.ColorModifiers.back();\n        g.Style.Colors[backup.Col] = backup.BackupValue;\n        g.ColorModifiers.pop_back();\n        count--;\n    }\n}\n\nstruct ImGuiStyleVarInfo\n{\n    ImGuiDataType   Type;\n    ImU32           Count;\n    ImU32           Offset;\n    void*           GetVarPtr(ImGuiStyle* style) const { return (void*)((unsigned char*)style + Offset); }\n};\n\nstatic const ImGuiStyleVarInfo GStyleVarInfo[] =\n{\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) },               // ImGuiStyleVar_Alpha\n    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) },       // ImGuiStyleVar_WindowPadding\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) },      // ImGuiStyleVar_WindowRounding\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) },    // ImGuiStyleVar_WindowBorderSize\n    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowMinSize) },       // ImGuiStyleVar_WindowMinSize\n    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowTitleAlign) },    // ImGuiStyleVar_WindowTitleAlign\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildRounding) },       // ImGuiStyleVar_ChildRounding\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildBorderSize) },     // ImGuiStyleVar_ChildBorderSize\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupRounding) },       // ImGuiStyleVar_PopupRounding\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupBorderSize) },     // ImGuiStyleVar_PopupBorderSize\n    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, FramePadding) },        // ImGuiStyleVar_FramePadding\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameRounding) },       // ImGuiStyleVar_FrameRounding\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameBorderSize) },     // ImGuiStyleVar_FrameBorderSize\n    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemSpacing) },         // ImGuiStyleVar_ItemSpacing\n    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemInnerSpacing) },    // ImGuiStyleVar_ItemInnerSpacing\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, IndentSpacing) },       // ImGuiStyleVar_IndentSpacing\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarSize) },       // ImGuiStyleVar_ScrollbarSize\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarRounding) },   // ImGuiStyleVar_ScrollbarRounding\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabMinSize) },         // ImGuiStyleVar_GrabMinSize\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabRounding) },        // ImGuiStyleVar_GrabRounding\n    { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) },         // ImGuiStyleVar_TabRounding\n    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) },     // ImGuiStyleVar_ButtonTextAlign\n    { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign\n};\n\nstatic const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx)\n{\n    IM_ASSERT(idx >= 0 && idx < ImGuiStyleVar_COUNT);\n    IM_ASSERT(IM_ARRAYSIZE(GStyleVarInfo) == ImGuiStyleVar_COUNT);\n    return &GStyleVarInfo[idx];\n}\n\nvoid ImGui::PushStyleVar(ImGuiStyleVar idx, float val)\n{\n    const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx);\n    if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1)\n    {\n        ImGuiContext& g = *GImGui;\n        float* pvar = (float*)var_info->GetVarPtr(&g.Style);\n        g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar));\n        *pvar = val;\n        return;\n    }\n    IM_ASSERT(0 && \"Called PushStyleVar() float variant but variable is not a float!\");\n}\n\nvoid ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val)\n{\n    const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx);\n    if (var_info->Type == ImGuiDataType_Float && var_info->Count == 2)\n    {\n        ImGuiContext& g = *GImGui;\n        ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style);\n        g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar));\n        *pvar = val;\n        return;\n    }\n    IM_ASSERT(0 && \"Called PushStyleVar() ImVec2 variant but variable is not a ImVec2!\");\n}\n\nvoid ImGui::PopStyleVar(int count)\n{\n    ImGuiContext& g = *GImGui;\n    while (count > 0)\n    {\n        // We avoid a generic memcpy(data, &backup.Backup.., GDataTypeSize[info->Type] * info->Count), the overhead in Debug is not worth it.\n        ImGuiStyleMod& backup = g.StyleModifiers.back();\n        const ImGuiStyleVarInfo* info = GetStyleVarInfo(backup.VarIdx);\n        void* data = info->GetVarPtr(&g.Style);\n        if (info->Type == ImGuiDataType_Float && info->Count == 1)      { ((float*)data)[0] = backup.BackupFloat[0]; }\n        else if (info->Type == ImGuiDataType_Float && info->Count == 2) { ((float*)data)[0] = backup.BackupFloat[0]; ((float*)data)[1] = backup.BackupFloat[1]; }\n        g.StyleModifiers.pop_back();\n        count--;\n    }\n}\n\nconst char* ImGui::GetStyleColorName(ImGuiCol idx)\n{\n    // Create switch-case from enum with regexp: ImGuiCol_{.*}, --> case ImGuiCol_\\1: return \"\\1\";\n    switch (idx)\n    {\n    case ImGuiCol_Text: return \"Text\";\n    case ImGuiCol_TextDisabled: return \"TextDisabled\";\n    case ImGuiCol_WindowBg: return \"WindowBg\";\n    case ImGuiCol_ChildBg: return \"ChildBg\";\n    case ImGuiCol_PopupBg: return \"PopupBg\";\n    case ImGuiCol_Border: return \"Border\";\n    case ImGuiCol_BorderShadow: return \"BorderShadow\";\n    case ImGuiCol_FrameBg: return \"FrameBg\";\n    case ImGuiCol_FrameBgHovered: return \"FrameBgHovered\";\n    case ImGuiCol_FrameBgActive: return \"FrameBgActive\";\n    case ImGuiCol_TitleBg: return \"TitleBg\";\n    case ImGuiCol_TitleBgActive: return \"TitleBgActive\";\n    case ImGuiCol_TitleBgCollapsed: return \"TitleBgCollapsed\";\n    case ImGuiCol_MenuBarBg: return \"MenuBarBg\";\n    case ImGuiCol_ScrollbarBg: return \"ScrollbarBg\";\n    case ImGuiCol_ScrollbarGrab: return \"ScrollbarGrab\";\n    case ImGuiCol_ScrollbarGrabHovered: return \"ScrollbarGrabHovered\";\n    case ImGuiCol_ScrollbarGrabActive: return \"ScrollbarGrabActive\";\n    case ImGuiCol_CheckMark: return \"CheckMark\";\n    case ImGuiCol_SliderGrab: return \"SliderGrab\";\n    case ImGuiCol_SliderGrabActive: return \"SliderGrabActive\";\n    case ImGuiCol_Button: return \"Button\";\n    case ImGuiCol_ButtonHovered: return \"ButtonHovered\";\n    case ImGuiCol_ButtonActive: return \"ButtonActive\";\n    case ImGuiCol_Header: return \"Header\";\n    case ImGuiCol_HeaderHovered: return \"HeaderHovered\";\n    case ImGuiCol_HeaderActive: return \"HeaderActive\";\n    case ImGuiCol_Separator: return \"Separator\";\n    case ImGuiCol_SeparatorHovered: return \"SeparatorHovered\";\n    case ImGuiCol_SeparatorActive: return \"SeparatorActive\";\n    case ImGuiCol_ResizeGrip: return \"ResizeGrip\";\n    case ImGuiCol_ResizeGripHovered: return \"ResizeGripHovered\";\n    case ImGuiCol_ResizeGripActive: return \"ResizeGripActive\";\n    case ImGuiCol_Tab: return \"Tab\";\n    case ImGuiCol_TabHovered: return \"TabHovered\";\n    case ImGuiCol_TabActive: return \"TabActive\";\n    case ImGuiCol_TabUnfocused: return \"TabUnfocused\";\n    case ImGuiCol_TabUnfocusedActive: return \"TabUnfocusedActive\";\n    case ImGuiCol_DockingPreview: return \"DockingPreview\";\n    case ImGuiCol_DockingEmptyBg: return \"DockingEmptyBg\";\n    case ImGuiCol_PlotLines: return \"PlotLines\";\n    case ImGuiCol_PlotLinesHovered: return \"PlotLinesHovered\";\n    case ImGuiCol_PlotHistogram: return \"PlotHistogram\";\n    case ImGuiCol_PlotHistogramHovered: return \"PlotHistogramHovered\";\n    case ImGuiCol_TextSelectedBg: return \"TextSelectedBg\";\n    case ImGuiCol_DragDropTarget: return \"DragDropTarget\";\n    case ImGuiCol_NavHighlight: return \"NavHighlight\";\n    case ImGuiCol_NavWindowingHighlight: return \"NavWindowingHighlight\";\n    case ImGuiCol_NavWindowingDimBg: return \"NavWindowingDimBg\";\n    case ImGuiCol_ModalWindowDimBg: return \"ModalWindowDimBg\";\n    }\n    IM_ASSERT(0);\n    return \"Unknown\";\n}\n\nbool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent)\n{\n    if (window->RootWindow == potential_parent)\n        return true;\n    while (window != NULL)\n    {\n        if (window == potential_parent)\n            return true;\n        window = window->ParentWindow;\n    }\n    return false;\n}\n\nbool ImGui::IsWindowHovered(ImGuiHoveredFlags flags)\n{\n    IM_ASSERT((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0);   // Flags not supported by this function\n    ImGuiContext& g = *GImGui;\n\n    if (flags & ImGuiHoveredFlags_AnyWindow)\n    {\n        if (g.HoveredWindow == NULL)\n            return false;\n    }\n    else\n    {\n        switch (flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows))\n        {\n        case ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows:\n            if (g.HoveredWindow == NULL || g.HoveredWindow->RootWindowDockStop != g.CurrentWindow->RootWindowDockStop)\n                return false;\n            break;\n        case ImGuiHoveredFlags_RootWindow:\n            if (g.HoveredWindow != g.CurrentWindow->RootWindowDockStop)\n                return false;\n            break;\n        case ImGuiHoveredFlags_ChildWindows:\n            if (g.HoveredWindow == NULL || !IsWindowChildOf(g.HoveredWindow, g.CurrentWindow))\n                return false;\n            break;\n        default:\n            if (g.HoveredWindow != g.CurrentWindow)\n                return false;\n            break;\n        }\n    }\n\n    if (!IsWindowContentHoverable(g.HoveredWindow, flags))\n        return false;\n    if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))\n        if (g.ActiveId != 0 && !g.ActiveIdAllowOverlap && g.ActiveId != g.HoveredWindow->MoveId)\n            return false;\n    return true;\n}\n\nbool ImGui::IsWindowFocused(ImGuiFocusedFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n\n    if (flags & ImGuiFocusedFlags_AnyWindow)\n        return g.NavWindow != NULL;\n\n    IM_ASSERT(g.CurrentWindow);     // Not inside a Begin()/End()\n    switch (flags & (ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows))\n    {\n    case ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows:\n        return g.NavWindow && g.NavWindow->RootWindowDockStop == g.CurrentWindow->RootWindowDockStop;\n    case ImGuiFocusedFlags_RootWindow:\n        return g.NavWindow == g.CurrentWindow->RootWindowDockStop;\n    case ImGuiFocusedFlags_ChildWindows:\n        return g.NavWindow && IsWindowChildOf(g.NavWindow, g.CurrentWindow);\n    default:\n        return g.NavWindow == g.CurrentWindow;\n    }\n}\n\nImGuiID ImGui::GetWindowDockID()\n{\n    ImGuiContext& g = *GImGui;\n    return g.CurrentWindow->DockId;\n}\n\nbool ImGui::IsWindowDocked()\n{\n    ImGuiContext& g = *GImGui;\n    return g.CurrentWindow->DockIsActive;\n}\n\n// Can we focus this window with CTRL+TAB (or PadMenu + PadFocusPrev/PadFocusNext)\n// Note that NoNavFocus makes the window not reachable with CTRL+TAB but it can still be focused with mouse or programmaticaly.\n// If you want a window to never be focused, you may use the e.g. NoInputs flag.\nbool ImGui::IsWindowNavFocusable(ImGuiWindow* window)\n{\n    return window->Active && window == window->RootWindowDockStop && !(window->Flags & ImGuiWindowFlags_NoNavFocus);\n}\n\nfloat ImGui::GetWindowWidth()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return window->Size.x;\n}\n\nfloat ImGui::GetWindowHeight()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return window->Size.y;\n}\n\nImVec2 ImGui::GetWindowPos()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    return window->Pos;\n}\n\nvoid ImGui::SetWindowScrollX(ImGuiWindow* window, float new_scroll_x)\n{\n    window->DC.CursorMaxPos.x += window->Scroll.x; // SizeContents is generally computed based on CursorMaxPos which is affected by scroll position, so we need to apply our change to it.\n    window->Scroll.x = new_scroll_x;\n    window->DC.CursorMaxPos.x -= window->Scroll.x;\n}\n\nvoid ImGui::SetWindowScrollY(ImGuiWindow* window, float new_scroll_y)\n{\n    window->DC.CursorMaxPos.y += window->Scroll.y; // SizeContents is generally computed based on CursorMaxPos which is affected by scroll position, so we need to apply our change to it.\n    window->Scroll.y = new_scroll_y;\n    window->DC.CursorMaxPos.y -= window->Scroll.y;\n}\n\nvoid ImGui::SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond)\n{\n    // Test condition (NB: bit 0 is always true) and clear flags for next time\n    if (cond && (window->SetWindowPosAllowFlags & cond) == 0)\n        return;\n\n    IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags.\n    window->SetWindowPosAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing);\n    window->SetWindowPosVal = ImVec2(FLT_MAX, FLT_MAX);\n\n    // Set\n    const ImVec2 old_pos = window->Pos;\n    window->Pos = ImFloor(pos);\n    window->DC.CursorPos += (window->Pos - old_pos);    // As we happen to move the window while it is being appended to (which is a bad idea - will smear) let's at least offset the cursor\n    window->DC.CursorMaxPos += (window->Pos - old_pos); // And more importantly we need to adjust this so size calculation doesn't get affected.\n}\n\nvoid ImGui::SetWindowPos(const ImVec2& pos, ImGuiCond cond)\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    SetWindowPos(window, pos, cond);\n}\n\nvoid ImGui::SetWindowPos(const char* name, const ImVec2& pos, ImGuiCond cond)\n{\n    if (ImGuiWindow* window = FindWindowByName(name))\n        SetWindowPos(window, pos, cond);\n}\n\nImVec2 ImGui::GetWindowSize()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->Size;\n}\n\nvoid ImGui::SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond)\n{\n    // Test condition (NB: bit 0 is always true) and clear flags for next time\n    if (cond && (window->SetWindowSizeAllowFlags & cond) == 0)\n        return;\n\n    IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags.\n    window->SetWindowSizeAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing);\n\n    // Set\n    if (size.x > 0.0f)\n    {\n        window->AutoFitFramesX = 0;\n        window->SizeFull.x = ImFloor(size.x);\n    }\n    else\n    {\n        window->AutoFitFramesX = 2;\n        window->AutoFitOnlyGrows = false;\n    }\n    if (size.y > 0.0f)\n    {\n        window->AutoFitFramesY = 0;\n        window->SizeFull.y = ImFloor(size.y);\n    }\n    else\n    {\n        window->AutoFitFramesY = 2;\n        window->AutoFitOnlyGrows = false;\n    }\n}\n\nvoid ImGui::SetWindowSize(const ImVec2& size, ImGuiCond cond)\n{\n    SetWindowSize(GImGui->CurrentWindow, size, cond);\n}\n\nvoid ImGui::SetWindowSize(const char* name, const ImVec2& size, ImGuiCond cond)\n{\n    if (ImGuiWindow* window = FindWindowByName(name))\n        SetWindowSize(window, size, cond);\n}\n\nvoid ImGui::SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond)\n{\n    // Test condition (NB: bit 0 is always true) and clear flags for next time\n    if (cond && (window->SetWindowCollapsedAllowFlags & cond) == 0)\n        return;\n    window->SetWindowCollapsedAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing);\n\n    // Set\n    window->Collapsed = collapsed;\n}\n\nstatic void SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size)\n{\n    IM_ASSERT(window->HitTestHoleSize.x == 0);     // We don't support multiple holes/hit test filters\n    window->HitTestHoleSize = ImVec2ih((short)size.x, (short)size.y);\n    window->HitTestHoleOffset = ImVec2ih((short)(pos.x - window->Pos.x), (short)(pos.y - window->Pos.y));\n}\n\nvoid ImGui::SetWindowCollapsed(bool collapsed, ImGuiCond cond)\n{\n    SetWindowCollapsed(GImGui->CurrentWindow, collapsed, cond);\n}\n\nbool ImGui::IsWindowCollapsed()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->Collapsed;\n}\n\nbool ImGui::IsWindowAppearing()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->Appearing;\n}\n\nvoid ImGui::SetWindowCollapsed(const char* name, bool collapsed, ImGuiCond cond)\n{\n    if (ImGuiWindow* window = FindWindowByName(name))\n        SetWindowCollapsed(window, collapsed, cond);\n}\n\nvoid ImGui::SetWindowFocus()\n{\n    FocusWindow(GImGui->CurrentWindow);\n}\n\nvoid ImGui::SetWindowFocus(const char* name)\n{\n    if (name)\n    {\n        if (ImGuiWindow* window = FindWindowByName(name))\n            FocusWindow(window);\n    }\n    else\n    {\n        FocusWindow(NULL);\n    }\n}\n\nvoid ImGui::SetNextWindowPos(const ImVec2& pos, ImGuiCond cond, const ImVec2& pivot)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags.\n    g.NextWindowData.PosVal = pos;\n    g.NextWindowData.PosPivotVal = pivot;\n    g.NextWindowData.PosCond = cond ? cond : ImGuiCond_Always;\n    g.NextWindowData.PosUndock = true;\n}\n\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\nvoid ImGui::SetNextWindowPosCenter(ImGuiCond cond) \n{ \n    ImGuiViewport* viewport = ImGui::GetMainViewport(); \n    SetNextWindowPos(viewport->Pos + viewport->Size * 0.5f, cond, ImVec2(0.5f, 0.5f)); \n    SetNextWindowViewport(viewport->ID); \n}\n#endif\n\nvoid ImGui::SetNextWindowSize(const ImVec2& size, ImGuiCond cond)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags.\n    g.NextWindowData.SizeVal = size;\n    g.NextWindowData.SizeCond = cond ? cond : ImGuiCond_Always;\n}\n\nvoid ImGui::SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback, void* custom_callback_user_data)\n{\n    ImGuiContext& g = *GImGui;\n    g.NextWindowData.SizeConstraintCond = ImGuiCond_Always;\n    g.NextWindowData.SizeConstraintRect = ImRect(size_min, size_max);\n    g.NextWindowData.SizeCallback = custom_callback;\n    g.NextWindowData.SizeCallbackUserData = custom_callback_user_data;\n}\n\nvoid ImGui::SetNextWindowContentSize(const ImVec2& size)\n{\n    ImGuiContext& g = *GImGui;\n    g.NextWindowData.ContentSizeVal = size;  // In Begin() we will add the size of window decorations (title bar, menu etc.) to that to form a SizeContents value.\n    g.NextWindowData.ContentSizeCond = ImGuiCond_Always;\n}\n\nvoid ImGui::SetNextWindowCollapsed(bool collapsed, ImGuiCond cond)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags.\n    g.NextWindowData.CollapsedVal = collapsed;\n    g.NextWindowData.CollapsedCond = cond ? cond : ImGuiCond_Always;\n}\n\nvoid ImGui::SetNextWindowFocus()\n{\n    ImGuiContext& g = *GImGui;\n    g.NextWindowData.FocusCond = ImGuiCond_Always;   // Using a Cond member for consistency (may transition all of them to single flag set for fast Clear() op)\n}\n\nvoid ImGui::SetNextWindowBgAlpha(float alpha)\n{\n    ImGuiContext& g = *GImGui;\n    g.NextWindowData.BgAlphaVal = alpha;\n    g.NextWindowData.BgAlphaCond = ImGuiCond_Always; // Using a Cond member for consistency (may transition all of them to single flag set for fast Clear() op)\n}\n\nvoid ImGui::SetNextWindowViewport(ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    g.NextWindowData.ViewportCond = ImGuiCond_Always;\n    g.NextWindowData.ViewportId = id;\n}\n\nvoid ImGui::SetNextWindowDockID(ImGuiID id, ImGuiCond cond)\n{\n    ImGuiContext& g = *GImGui;\n    g.NextWindowData.DockCond = cond ? cond : ImGuiCond_Always;\n    g.NextWindowData.DockId = id;\n}\n\nvoid ImGui::SetNextWindowClass(const ImGuiWindowClass* window_class)\n{\n    ImGuiContext& g = *GImGui;\n    g.NextWindowData.WindowClass = *window_class;\n}\n\n// FIXME: This is in window space (not screen space!). We should try to obsolete all those functions.\nImVec2 ImGui::GetContentRegionMax()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    ImVec2 mx = window->ContentsRegionRect.Max - window->Pos;\n    if (window->DC.CurrentColumns)\n        mx.x = GetColumnOffset(window->DC.CurrentColumns->Current + 1) - window->WindowPadding.x;\n    return mx;\n}\n\n// [Internal] Absolute coordinate. Saner. This is not exposed until we finishing refactoring work rect features.\nImVec2 ImGui::GetContentRegionMaxScreen()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    ImVec2 mx = window->ContentsRegionRect.Max;\n    if (window->DC.CurrentColumns)\n        mx.x = window->Pos.x + GetColumnOffset(window->DC.CurrentColumns->Current + 1) - window->WindowPadding.x;\n    return mx;\n}\n\nImVec2 ImGui::GetContentRegionAvail()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return GetContentRegionMaxScreen() - window->DC.CursorPos;\n}\n\nfloat ImGui::GetContentRegionAvailWidth()\n{\n    return GetContentRegionAvail().x;\n}\n\n// In window space (not screen space!)\nImVec2 ImGui::GetWindowContentRegionMin()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->ContentsRegionRect.Min - window->Pos;\n}\n\nImVec2 ImGui::GetWindowContentRegionMax()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->ContentsRegionRect.Max - window->Pos;\n}\n\nfloat ImGui::GetWindowContentRegionWidth()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->ContentsRegionRect.GetWidth();\n}\n\nfloat ImGui::GetTextLineHeight()\n{\n    ImGuiContext& g = *GImGui;\n    return g.FontSize;\n}\n\nfloat ImGui::GetTextLineHeightWithSpacing()\n{\n    ImGuiContext& g = *GImGui;\n    return g.FontSize + g.Style.ItemSpacing.y;\n}\n\nfloat ImGui::GetFrameHeight()\n{\n    ImGuiContext& g = *GImGui;\n    return g.FontSize + g.Style.FramePadding.y * 2.0f;\n}\n\nfloat ImGui::GetFrameHeightWithSpacing()\n{\n    ImGuiContext& g = *GImGui;\n    return g.FontSize + g.Style.FramePadding.y * 2.0f + g.Style.ItemSpacing.y;\n}\n\nImDrawList* ImGui::GetWindowDrawList()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    return window->DrawList;\n}\n\nfloat ImGui::GetWindowDpiScale()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.CurrentViewport != NULL);\n    return g.CurrentViewport->DpiScale;\n}\n\nImGuiViewport* ImGui::GetWindowViewport()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.CurrentViewport != NULL && g.CurrentViewport == g.CurrentWindow->Viewport);\n    return g.CurrentViewport;\n}\n\nImFont* ImGui::GetFont()\n{\n    return GImGui->Font;\n}\n\nfloat ImGui::GetFontSize()\n{\n    return GImGui->FontSize;\n}\n\nImVec2 ImGui::GetFontTexUvWhitePixel()\n{\n    return GImGui->DrawListSharedData.TexUvWhitePixel;\n}\n\nvoid ImGui::SetWindowFontScale(float scale)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = GetCurrentWindow();\n    window->FontWindowScale = scale;\n    g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize();\n}\n\n// User generally sees positions in window coordinates. Internally we store CursorPos in absolute screen coordinates because it is more convenient.\n// Conversion happens as we pass the value to user, but it makes our naming convention confusing because GetCursorPos() == (DC.CursorPos - window.Pos). May want to rename 'DC.CursorPos'.\nImVec2 ImGui::GetCursorPos()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.CursorPos - window->Pos + window->Scroll;\n}\n\nfloat ImGui::GetCursorPosX()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.CursorPos.x - window->Pos.x + window->Scroll.x;\n}\n\nfloat ImGui::GetCursorPosY()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.CursorPos.y - window->Pos.y + window->Scroll.y;\n}\n\nvoid ImGui::SetCursorPos(const ImVec2& local_pos)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.CursorPos = window->Pos - window->Scroll + local_pos;\n    window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);\n}\n\nvoid ImGui::SetCursorPosX(float x)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + x;\n    window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x);\n}\n\nvoid ImGui::SetCursorPosY(float y)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.CursorPos.y = window->Pos.y - window->Scroll.y + y;\n    window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y);\n}\n\nImVec2 ImGui::GetCursorStartPos()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.CursorStartPos - window->Pos;\n}\n\nImVec2 ImGui::GetCursorScreenPos()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.CursorPos;\n}\n\nvoid ImGui::SetCursorScreenPos(const ImVec2& pos)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.CursorPos = pos;\n    window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos);\n}\n\nfloat ImGui::GetScrollX()\n{\n    return GImGui->CurrentWindow->Scroll.x;\n}\n\nfloat ImGui::GetScrollY()\n{\n    return GImGui->CurrentWindow->Scroll.y;\n}\n\nfloat ImGui::GetScrollMaxX()\n{\n    return GetWindowScrollMaxX(GImGui->CurrentWindow);\n}\n\nfloat ImGui::GetScrollMaxY()\n{\n    return GetWindowScrollMaxY(GImGui->CurrentWindow);\n}\n\nvoid ImGui::SetScrollX(float scroll_x)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->ScrollTarget.x = scroll_x;\n    window->ScrollTargetCenterRatio.x = 0.0f;\n}\n\nvoid ImGui::SetScrollY(float scroll_y)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    window->ScrollTarget.y = scroll_y + window->TitleBarHeight() + window->MenuBarHeight(); // title bar height canceled out when using ScrollTargetRelY\n    window->ScrollTargetCenterRatio.y = 0.0f;\n}\n\nvoid ImGui::SetScrollFromPosY(float local_y, float center_y_ratio)\n{\n    // We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size\n    ImGuiWindow* window = GetCurrentWindow();\n    IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f);\n    window->ScrollTarget.y = (float)(int)(local_y + window->Scroll.y);\n    window->ScrollTargetCenterRatio.y = center_y_ratio;\n}\n\n// center_y_ratio: 0.0f top of last item, 0.5f vertical center of last item, 1.0f bottom of last item.\nvoid ImGui::SetScrollHereY(float center_y_ratio)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    float target_y = window->DC.CursorPosPrevLine.y - window->Pos.y; // Top of last item, in window space\n    target_y += (window->DC.PrevLineSize.y * center_y_ratio) + (GImGui->Style.ItemSpacing.y * (center_y_ratio - 0.5f) * 2.0f); // Precisely aim above, in the middle or below the last line.\n    SetScrollFromPosY(target_y, center_y_ratio);\n}\n\nvoid ImGui::ActivateItem(ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    g.NavNextActivateId = id;\n}\n\nvoid ImGui::SetKeyboardFocusHere(int offset)\n{\n    IM_ASSERT(offset >= -1);    // -1 is allowed but not below\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    g.FocusRequestNextWindow = window;\n    g.FocusRequestNextCounterAll = window->DC.FocusCounterAll + 1 + offset;\n    g.FocusRequestNextCounterTab = INT_MAX;\n}\n\nvoid ImGui::SetItemDefaultFocus()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (!window->Appearing)\n        return;\n    if (g.NavWindow == window->RootWindowForNav && (g.NavInitRequest || g.NavInitResultId != 0) && g.NavLayer == g.NavWindow->DC.NavLayerCurrent)\n    {\n        g.NavInitRequest = false;\n        g.NavInitResultId = g.NavWindow->DC.LastItemId;\n        g.NavInitResultRectRel = ImRect(g.NavWindow->DC.LastItemRect.Min - g.NavWindow->Pos, g.NavWindow->DC.LastItemRect.Max - g.NavWindow->Pos);\n        NavUpdateAnyRequestFlag();\n        if (!IsItemVisible())\n            SetScrollHereY();\n    }\n}\n\nvoid ImGui::SetStateStorage(ImGuiStorage* tree)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    window->DC.StateStorage = tree ? tree : &window->StateStorage;\n}\n\nImGuiStorage* ImGui::GetStateStorage()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return window->DC.StateStorage;\n}\n\nvoid ImGui::PushID(const char* str_id)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    window->IDStack.push_back(window->GetIDNoKeepAlive(str_id));\n}\n\nvoid ImGui::PushID(const char* str_id_begin, const char* str_id_end)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    window->IDStack.push_back(window->GetIDNoKeepAlive(str_id_begin, str_id_end));\n}\n\nvoid ImGui::PushID(const void* ptr_id)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    window->IDStack.push_back(window->GetIDNoKeepAlive(ptr_id));\n}\n\nvoid ImGui::PushID(int int_id)\n{\n    const void* ptr_id = (void*)(intptr_t)int_id;\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    window->IDStack.push_back(window->GetIDNoKeepAlive(ptr_id));\n}\n\n// Push a given id value ignoring the ID stack as a seed.\nvoid ImGui::PushOverrideID(ImGuiID id)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    window->IDStack.push_back(id);\n}\n\nvoid ImGui::PopID()\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    window->IDStack.pop_back();\n}\n\nImGuiID ImGui::GetID(const char* str_id)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return window->GetID(str_id);\n}\n\nImGuiID ImGui::GetID(const char* str_id_begin, const char* str_id_end)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return window->GetID(str_id_begin, str_id_end);\n}\n\nImGuiID ImGui::GetID(const void* ptr_id)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return window->GetID(ptr_id);\n}\n\nbool ImGui::IsRectVisible(const ImVec2& size)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return window->ClipRect.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size));\n}\n\nbool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    return window->ClipRect.Overlaps(ImRect(rect_min, rect_max));\n}\n\n// Lock horizontal starting position + capture group bounding box into one \"item\" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.)\nvoid ImGui::BeginGroup()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = GetCurrentWindow();\n\n    window->DC.GroupStack.resize(window->DC.GroupStack.Size + 1);\n    ImGuiGroupData& group_data = window->DC.GroupStack.back();\n    group_data.BackupCursorPos = window->DC.CursorPos;\n    group_data.BackupCursorMaxPos = window->DC.CursorMaxPos;\n    group_data.BackupIndent = window->DC.Indent;\n    group_data.BackupGroupOffset = window->DC.GroupOffset;\n    group_data.BackupCurrentLineSize = window->DC.CurrentLineSize;\n    group_data.BackupCurrentLineTextBaseOffset = window->DC.CurrentLineTextBaseOffset;\n    group_data.BackupActiveIdIsAlive = g.ActiveIdIsAlive;\n    group_data.BackupActiveIdPreviousFrameIsAlive = g.ActiveIdPreviousFrameIsAlive;\n    group_data.AdvanceCursor = true;\n\n    window->DC.GroupOffset.x = window->DC.CursorPos.x - window->Pos.x - window->DC.ColumnsOffset.x;\n    window->DC.Indent = window->DC.GroupOffset;\n    window->DC.CursorMaxPos = window->DC.CursorPos;\n    window->DC.CurrentLineSize = ImVec2(0.0f, 0.0f);\n    if (g.LogEnabled)\n        g.LogLinePosY = -FLT_MAX; // To enforce Log carriage return\n}\n\nvoid ImGui::EndGroup()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = GetCurrentWindow();\n    IM_ASSERT(!window->DC.GroupStack.empty());  // Mismatched BeginGroup()/EndGroup() calls\n\n    ImGuiGroupData& group_data = window->DC.GroupStack.back();\n\n    ImRect group_bb(group_data.BackupCursorPos, window->DC.CursorMaxPos);\n    group_bb.Max = ImMax(group_bb.Min, group_bb.Max);\n\n    window->DC.CursorPos = group_data.BackupCursorPos;\n    window->DC.CursorMaxPos = ImMax(group_data.BackupCursorMaxPos, window->DC.CursorMaxPos);\n    window->DC.Indent = group_data.BackupIndent;\n    window->DC.GroupOffset = group_data.BackupGroupOffset;\n    window->DC.CurrentLineSize = group_data.BackupCurrentLineSize;\n    window->DC.CurrentLineTextBaseOffset = group_data.BackupCurrentLineTextBaseOffset;\n    if (g.LogEnabled)\n        g.LogLinePosY = -FLT_MAX; // To enforce Log carriage return\n\n    if (group_data.AdvanceCursor)\n    {\n        window->DC.CurrentLineTextBaseOffset = ImMax(window->DC.PrevLineTextBaseOffset, group_data.BackupCurrentLineTextBaseOffset);      // FIXME: Incorrect, we should grab the base offset from the *first line* of the group but it is hard to obtain now.\n        ItemSize(group_bb.GetSize(), 0.0f);\n        ItemAdd(group_bb, 0);\n    }\n\n    // If the current ActiveId was declared within the boundary of our group, we copy it to LastItemId so IsItemActive(), IsItemDeactivated() etc. will be functional on the entire group.\n    // It would be be neater if we replaced window.DC.LastItemId by e.g. 'bool LastItemIsActive', but would put a little more burden on individual widgets.\n    // (and if you grep for LastItemId you'll notice it is only used in that context.\n    if ((group_data.BackupActiveIdIsAlive != g.ActiveId) && (g.ActiveIdIsAlive == g.ActiveId) && g.ActiveId) // && g.ActiveIdWindow->RootWindow == window->RootWindow)\n        window->DC.LastItemId = g.ActiveId;\n    else if (!group_data.BackupActiveIdPreviousFrameIsAlive && g.ActiveIdPreviousFrameIsAlive) // && g.ActiveIdPreviousFrameWindow->RootWindow == window->RootWindow)\n        window->DC.LastItemId = g.ActiveIdPreviousFrame;\n    window->DC.LastItemRect = group_bb;\n\n    window->DC.GroupStack.pop_back();\n\n    //window->DrawList->AddRect(group_bb.Min, group_bb.Max, IM_COL32(255,0,255,255));   // [Debug]\n}\n\n// Gets back to previous line and continue with horizontal layout\n//      offset_from_start_x == 0 : follow right after previous item\n//      offset_from_start_x != 0 : align to specified x position (relative to window/group left)\n//      spacing_w < 0            : use default spacing if pos_x == 0, no spacing if pos_x != 0\n//      spacing_w >= 0           : enforce spacing amount\nvoid ImGui::SameLine(float offset_from_start_x, float spacing_w)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    if (offset_from_start_x != 0.0f)\n    {\n        if (spacing_w < 0.0f) spacing_w = 0.0f;\n        window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + offset_from_start_x + spacing_w + window->DC.GroupOffset.x + window->DC.ColumnsOffset.x;\n        window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y;\n    }\n    else\n    {\n        if (spacing_w < 0.0f) spacing_w = g.Style.ItemSpacing.x;\n        window->DC.CursorPos.x = window->DC.CursorPosPrevLine.x + spacing_w;\n        window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y;\n    }\n    window->DC.CurrentLineSize = window->DC.PrevLineSize;\n    window->DC.CurrentLineTextBaseOffset = window->DC.PrevLineTextBaseOffset;\n}\n\nvoid ImGui::Indent(float indent_w)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.Indent.x += (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing;\n    window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x;\n}\n\nvoid ImGui::Unindent(float indent_w)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = GetCurrentWindow();\n    window->DC.Indent.x -= (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing;\n    window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x;\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] TOOLTIPS\n//-----------------------------------------------------------------------------\n\nvoid ImGui::BeginTooltip()\n{\n    ImGuiContext& g = *GImGui;\n    if (g.DragDropWithinSourceOrTarget)\n    {\n        // The default tooltip position is a little offset to give space to see the context menu (it's also clamped within the current viewport/monitor)\n        // In the context of a dragging tooltip we try to reduce that offset and we enforce following the cursor.\n        // Whatever we do we want to call SetNextWindowPos() to enforce a tooltip position and disable clipping the tooltip without our display area, like regular tooltip do.\n        //ImVec2 tooltip_pos = g.IO.MousePos - g.ActiveIdClickOffset - g.Style.WindowPadding;\n        ImVec2 tooltip_pos = g.IO.MousePos + ImVec2(16 * g.Style.MouseCursorScale, 8 * g.Style.MouseCursorScale);\n        SetNextWindowPos(tooltip_pos);\n        SetNextWindowBgAlpha(g.Style.Colors[ImGuiCol_PopupBg].w * 0.60f);\n        //PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * 0.60f); // This would be nice but e.g ColorButton with checkboard has issue with transparent colors :(\n        BeginTooltipEx(0, true);\n    }\n    else\n    {\n        BeginTooltipEx(0, false);\n    }\n}\n\n// Not exposed publicly as BeginTooltip() because bool parameters are evil. Let's see if other needs arise first.\nvoid ImGui::BeginTooltipEx(ImGuiWindowFlags extra_flags, bool override_previous_tooltip)\n{\n    ImGuiContext& g = *GImGui;\n    char window_name[16];\n    ImFormatString(window_name, IM_ARRAYSIZE(window_name), \"##Tooltip_%02d\", g.TooltipOverrideCount);\n    if (override_previous_tooltip)\n        if (ImGuiWindow* window = FindWindowByName(window_name))\n            if (window->Active)\n            {\n                // Hide previous tooltip from being displayed. We can't easily \"reset\" the content of a window so we create a new one.\n                window->Hidden = true;\n                window->HiddenFramesCanSkipItems = 1;\n                ImFormatString(window_name, IM_ARRAYSIZE(window_name), \"##Tooltip_%02d\", ++g.TooltipOverrideCount);\n            }\n    ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip|ImGuiWindowFlags_NoInputs|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoDocking;\n    Begin(window_name, NULL, flags | extra_flags);\n}\n\nvoid ImGui::EndTooltip()\n{\n    IM_ASSERT(GetCurrentWindowRead()->Flags & ImGuiWindowFlags_Tooltip);   // Mismatched BeginTooltip()/EndTooltip() calls\n    End();\n}\n\nvoid ImGui::SetTooltipV(const char* fmt, va_list args)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.DragDropWithinSourceOrTarget)\n        BeginTooltip();\n    else\n        BeginTooltipEx(0, true);\n    TextV(fmt, args);\n    EndTooltip();\n}\n\nvoid ImGui::SetTooltip(const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    SetTooltipV(fmt, args);\n    va_end(args);\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] POPUPS\n//-----------------------------------------------------------------------------\n\nbool ImGui::IsPopupOpen(ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == id;\n}\n\nbool ImGui::IsPopupOpen(const char* str_id)\n{\n    ImGuiContext& g = *GImGui;\n    return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == g.CurrentWindow->GetID(str_id);\n}\n\nImGuiWindow* ImGui::GetFrontMostPopupModal()\n{\n    ImGuiContext& g = *GImGui;\n    for (int n = g.OpenPopupStack.Size-1; n >= 0; n--)\n        if (ImGuiWindow* popup = g.OpenPopupStack.Data[n].Window)\n            if (popup->Flags & ImGuiWindowFlags_Modal)\n                return popup;\n    return NULL;\n}\n\nvoid ImGui::OpenPopup(const char* str_id)\n{\n    ImGuiContext& g = *GImGui;\n    OpenPopupEx(g.CurrentWindow->GetID(str_id));\n}\n\n// Mark popup as open (toggle toward open state).\n// Popups are closed when user click outside, or activate a pressable item, or CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block.\n// Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level).\n// One open popup per level of the popup hierarchy (NB: when assigning we reset the Window member of ImGuiPopupRef to NULL)\nvoid ImGui::OpenPopupEx(ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* parent_window = g.CurrentWindow;\n    int current_stack_size = g.BeginPopupStack.Size;\n    ImGuiPopupData popup_ref; // Tagged as new ref as Window will be set back to NULL if we write this into OpenPopupStack.\n    popup_ref.PopupId = id;\n    popup_ref.Window = NULL;\n    popup_ref.SourceWindow = g.NavWindow;\n    popup_ref.OpenFrameCount = g.FrameCount;\n    popup_ref.OpenParentId = parent_window->IDStack.back();\n    popup_ref.OpenPopupPos = NavCalcPreferredRefPos();\n    popup_ref.OpenMousePos = IsMousePosValid(&g.IO.MousePos) ? g.IO.MousePos : popup_ref.OpenPopupPos;\n\n    //IMGUI_DEBUG_LOG(\"OpenPopupEx(0x%08X)\\n\", g.FrameCount, id);\n    if (g.OpenPopupStack.Size < current_stack_size + 1)\n    {\n        g.OpenPopupStack.push_back(popup_ref);\n    }\n    else\n    {\n        // Gently handle the user mistakenly calling OpenPopup() every frame. It is a programming mistake! However, if we were to run the regular code path, the ui\n        // would become completely unusable because the popup will always be in hidden-while-calculating-size state _while_ claiming focus. Which would be a very confusing\n        // situation for the programmer. Instead, we silently allow the popup to proceed, it will keep reappearing and the programming error will be more obvious to understand.\n        if (g.OpenPopupStack[current_stack_size].PopupId == id && g.OpenPopupStack[current_stack_size].OpenFrameCount == g.FrameCount - 1)\n        {\n            g.OpenPopupStack[current_stack_size].OpenFrameCount = popup_ref.OpenFrameCount;\n        }\n        else\n        {\n            // Close child popups if any, then flag popup for open/reopen\n            g.OpenPopupStack.resize(current_stack_size + 1);\n            g.OpenPopupStack[current_stack_size] = popup_ref;\n        }\n\n        // When reopening a popup we first refocus its parent, otherwise if its parent is itself a popup it would get closed by ClosePopupsOverWindow().\n        // This is equivalent to what ClosePopupToLevel() does.\n        //if (g.OpenPopupStack[current_stack_size].PopupId == id)\n        //    FocusWindow(parent_window);\n    }\n}\n\nbool ImGui::OpenPopupOnItemClick(const char* str_id, int mouse_button)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup))\n    {\n        ImGuiID id = str_id ? window->GetID(str_id) : window->DC.LastItemId; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict!\n        IM_ASSERT(id != 0);                                                  // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item)\n        OpenPopupEx(id);\n        return true;\n    }\n    return false;\n}\n\nvoid ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.OpenPopupStack.empty())\n        return;\n\n    // When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it.\n    // Don't close our own child popup windows.\n    int popup_count_to_keep = 0;\n    if (ref_window)\n    {\n        // Find the highest popup which is a descendant of the reference window (generally reference window = NavWindow)\n        for (; popup_count_to_keep < g.OpenPopupStack.Size; popup_count_to_keep++)\n        {\n            ImGuiPopupData& popup = g.OpenPopupStack[popup_count_to_keep];\n            if (!popup.Window)\n                continue;\n            IM_ASSERT((popup.Window->Flags & ImGuiWindowFlags_Popup) != 0);\n            if (popup.Window->Flags & ImGuiWindowFlags_ChildWindow)\n                continue;\n\n            // Trim the stack when popups are not direct descendant of the reference window (the reference window is often the NavWindow)\n            bool popup_or_descendent_is_ref_window = false;\n            for (int m = popup_count_to_keep; m < g.OpenPopupStack.Size && !popup_or_descendent_is_ref_window; m++)\n                if (ImGuiWindow* popup_window = g.OpenPopupStack[m].Window)\n                    if (popup_window->RootWindow == ref_window->RootWindow)\n                        popup_or_descendent_is_ref_window = true;\n            if (!popup_or_descendent_is_ref_window)\n                break;\n        }\n    }\n    if (popup_count_to_keep < g.OpenPopupStack.Size) // This test is not required but it allows to set a convenient breakpoint on the statement below\n    {\n        //IMGUI_DEBUG_LOG(\"ClosePopupsOverWindow(%s) -> ClosePopupToLevel(%d)\\n\", ref_window->Name, popup_count_to_keep);\n        ClosePopupToLevel(popup_count_to_keep, restore_focus_to_window_under_popup);\n    }\n}\n\nvoid ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size);\n    ImGuiWindow* focus_window = g.OpenPopupStack[remaining].SourceWindow;\n    ImGuiWindow* popup_window = g.OpenPopupStack[remaining].Window;\n    g.OpenPopupStack.resize(remaining);\n\n    if (restore_focus_to_window_under_popup)\n    {\n        if (focus_window && !focus_window->WasActive && popup_window)\n        {\n            // Fallback\n            FocusTopMostWindowUnderOne(popup_window, NULL);\n        }\n        else\n        {\n            if (g.NavLayer == 0 && focus_window)\n                focus_window = NavRestoreLastChildNavWindow(focus_window);\n            FocusWindow(focus_window);\n        }\n    }\n}\n\n// Close the popup we have begin-ed into.\nvoid ImGui::CloseCurrentPopup()\n{\n    ImGuiContext& g = *GImGui;\n    int popup_idx = g.BeginPopupStack.Size - 1;\n    if (popup_idx < 0 || popup_idx >= g.OpenPopupStack.Size || g.BeginPopupStack[popup_idx].PopupId != g.OpenPopupStack[popup_idx].PopupId)\n        return;\n\n    // Closing a menu closes its top-most parent popup (unless a modal)\n    while (popup_idx > 0)\n    {\n        ImGuiWindow* popup_window = g.OpenPopupStack[popup_idx].Window;\n        ImGuiWindow* parent_popup_window = g.OpenPopupStack[popup_idx - 1].Window;\n        bool close_parent = false;\n        if (popup_window && (popup_window->Flags & ImGuiWindowFlags_ChildMenu))\n            if (parent_popup_window == NULL || !(parent_popup_window->Flags & ImGuiWindowFlags_Modal))\n                close_parent = true;\n        if (!close_parent)\n            break;\n        popup_idx--;\n    }\n    //IMGUI_DEBUG_LOG(\"CloseCurrentPopup %d -> %d\\n\", g.BeginPopupStack.Size - 1, popup_idx);\n    ClosePopupToLevel(popup_idx, true);\n\n    // A common pattern is to close a popup when selecting a menu item/selectable that will open another window.\n    // To improve this usage pattern, we avoid nav highlight for a single frame in the parent window.\n    // Similarly, we could avoid mouse hover highlight in this window but it is less visually problematic.\n    if (ImGuiWindow* window = g.NavWindow)\n        window->DC.NavHideHighlightOneFrame = true;\n}\n\nbool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags)\n{\n    ImGuiContext& g = *GImGui;\n    if (!IsPopupOpen(id))\n    {\n        g.NextWindowData.Clear(); // We behave like Begin() and need to consume those values\n        return false;\n    }\n\n    char name[20];\n    if (extra_flags & ImGuiWindowFlags_ChildMenu)\n        ImFormatString(name, IM_ARRAYSIZE(name), \"##Menu_%02d\", g.BeginPopupStack.Size); // Recycle windows based on depth\n    else\n        ImFormatString(name, IM_ARRAYSIZE(name), \"##Popup_%08x\", id); // Not recycling, so we can close/open during the same frame\n\n    bool is_open = Begin(name, NULL, extra_flags | ImGuiWindowFlags_Popup);\n    if (!is_open) // NB: Begin can return false when the popup is completely clipped (e.g. zero size display)\n        EndPopup();\n\n    return is_open;\n}\n\nbool ImGui::BeginPopup(const char* str_id, ImGuiWindowFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.OpenPopupStack.Size <= g.BeginPopupStack.Size) // Early out for performance\n    {\n        g.NextWindowData.Clear(); // We behave like Begin() and need to consume those values\n        return false;\n    }\n    flags |= ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoDocking;\n    return BeginPopupEx(g.CurrentWindow->GetID(str_id), flags);\n}\n\n// If 'p_open' is specified for a modal popup window, the popup will have a regular close button which will close the popup.\n// Note that popup visibility status is owned by imgui (and manipulated with e.g. OpenPopup) so the actual value of *p_open is meaningless here.\nbool ImGui::BeginPopupModal(const char* name, bool* p_open, ImGuiWindowFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    const ImGuiID id = window->GetID(name);\n    if (!IsPopupOpen(id))\n    {\n        g.NextWindowData.Clear(); // We behave like Begin() and need to consume those values\n        return false;\n    }\n\n    // Center modal windows by default\n    // FIXME: Should test for (PosCond & window->SetWindowPosAllowFlags) with the upcoming window.\n    if (g.NextWindowData.PosCond == 0)\n        SetNextWindowPos(window->Viewport->GetCenter(), ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));\n\n    flags |= ImGuiWindowFlags_Popup | ImGuiWindowFlags_Modal | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoDocking;\n    const bool is_open = Begin(name, p_open, flags);\n    if (!is_open || (p_open && !*p_open)) // NB: is_open can be 'false' when the popup is completely clipped (e.g. zero size display)\n    {\n        EndPopup();\n        if (is_open)\n            ClosePopupToLevel(g.BeginPopupStack.Size, true);\n        return false;\n    }\n    return is_open;\n}\n\nvoid ImGui::EndPopup()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.CurrentWindow->Flags & ImGuiWindowFlags_Popup);  // Mismatched BeginPopup()/EndPopup() calls\n    IM_ASSERT(g.BeginPopupStack.Size > 0);\n\n    // Make all menus and popups wrap around for now, may need to expose that policy.\n    NavMoveRequestTryWrapping(g.CurrentWindow, ImGuiNavMoveFlags_LoopY);\n\n    End();\n}\n\n// This is a helper to handle the simplest case of associating one named popup to one given widget.\n// You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters).\n// You can pass a NULL str_id to use the identifier of the last item.\nbool ImGui::BeginPopupContextItem(const char* str_id, int mouse_button)\n{\n    ImGuiWindow* window = GImGui->CurrentWindow;\n    ImGuiID id = str_id ? window->GetID(str_id) : window->DC.LastItemId; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict!\n    IM_ASSERT(id != 0);                                                  // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item)\n    if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup))\n        OpenPopupEx(id);\n    return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings);\n}\n\nbool ImGui::BeginPopupContextWindow(const char* str_id, int mouse_button, bool also_over_items)\n{\n    if (!str_id)\n        str_id = \"window_context\";\n    ImGuiID id = GImGui->CurrentWindow->GetID(str_id);\n    if (IsMouseReleased(mouse_button) && IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup))\n        if (also_over_items || !IsAnyItemHovered())\n            OpenPopupEx(id);\n    return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings);\n}\n\nbool ImGui::BeginPopupContextVoid(const char* str_id, int mouse_button)\n{\n    if (!str_id)\n        str_id = \"void_context\";\n    ImGuiID id = GImGui->CurrentWindow->GetID(str_id);\n    if (IsMouseReleased(mouse_button) && !IsWindowHovered(ImGuiHoveredFlags_AnyWindow))\n        OpenPopupEx(id);\n    return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings);\n}\n\n// r_avoid = the rectangle to avoid (e.g. for tooltip it is a rectangle around the mouse cursor which we want to avoid. for popups it's a small point around the cursor.)\n// r_outer = the visible area rectangle, minus safe area padding. If our popup size won't fit because of safe area padding we ignore it.\n// (r_outer is usually equivalent to the viewport rectangle minus padding, but when multi-viewports are enabled and monitor\n//  information are available, it may represent the entire platform monitor from the frame of reference of the current viewport.\n//  this allows us to have tooltips/popups displayed out of the parent viewport.)\nImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy)\n{\n    ImVec2 base_pos_clamped = ImClamp(ref_pos, r_outer.Min, r_outer.Max - size);\n    //GetForegroundDrawList()->AddRect(r_avoid.Min, r_avoid.Max, IM_COL32(255,0,0,255));\n    //GetForegroundDrawList()->AddRect(r_outer.Min, r_outer.Max, IM_COL32(0,255,0,255));\n\n    // Combo Box policy (we want a connecting edge)\n    if (policy == ImGuiPopupPositionPolicy_ComboBox)\n    {\n        const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Down, ImGuiDir_Right, ImGuiDir_Left, ImGuiDir_Up };\n        for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++)\n        {\n            const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n];\n            if (n != -1 && dir == *last_dir) // Already tried this direction?\n                continue;\n            ImVec2 pos;\n            if (dir == ImGuiDir_Down)  pos = ImVec2(r_avoid.Min.x, r_avoid.Max.y);          // Below, Toward Right (default)\n            if (dir == ImGuiDir_Right) pos = ImVec2(r_avoid.Min.x, r_avoid.Min.y - size.y); // Above, Toward Right\n            if (dir == ImGuiDir_Left)  pos = ImVec2(r_avoid.Max.x - size.x, r_avoid.Max.y); // Below, Toward Left\n            if (dir == ImGuiDir_Up)    pos = ImVec2(r_avoid.Max.x - size.x, r_avoid.Min.y - size.y); // Above, Toward Left\n            if (!r_outer.Contains(ImRect(pos, pos + size)))\n                continue;\n            *last_dir = dir;\n            return pos;\n        }\n    }\n\n    // Default popup policy\n    const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Right, ImGuiDir_Down, ImGuiDir_Up, ImGuiDir_Left };\n    for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++)\n    {\n        const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n];\n        if (n != -1 && dir == *last_dir) // Already tried this direction?\n            continue;\n        float avail_w = (dir == ImGuiDir_Left ? r_avoid.Min.x : r_outer.Max.x) - (dir == ImGuiDir_Right ? r_avoid.Max.x : r_outer.Min.x);\n        float avail_h = (dir == ImGuiDir_Up ? r_avoid.Min.y : r_outer.Max.y) - (dir == ImGuiDir_Down ? r_avoid.Max.y : r_outer.Min.y);\n        if (avail_w < size.x || avail_h < size.y)\n            continue;\n        ImVec2 pos;\n        pos.x = (dir == ImGuiDir_Left) ? r_avoid.Min.x - size.x : (dir == ImGuiDir_Right) ? r_avoid.Max.x : base_pos_clamped.x;\n        pos.y = (dir == ImGuiDir_Up)   ? r_avoid.Min.y - size.y : (dir == ImGuiDir_Down)  ? r_avoid.Max.y : base_pos_clamped.y;\n        *last_dir = dir;\n        return pos;\n    }\n\n    // Fallback, try to keep within display\n    *last_dir = ImGuiDir_None;\n    ImVec2 pos = ref_pos;\n    pos.x = ImMax(ImMin(pos.x + size.x, r_outer.Max.x) - size.x, r_outer.Min.x);\n    pos.y = ImMax(ImMin(pos.y + size.y, r_outer.Max.y) - size.y, r_outer.Min.y);\n    return pos;\n}\n\nImRect ImGui::GetWindowAllowedExtentRect(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    ImRect r_screen;\n    if (window->ViewportAllowPlatformMonitorExtend >= 0)\n    {\n        // Extent with be in the frame of reference of the given viewport (so Min is likely to be negative here)\n        const ImGuiPlatformMonitor& monitor = g.PlatformIO.Monitors[window->ViewportAllowPlatformMonitorExtend];\n        r_screen.Min = monitor.WorkPos;\n        r_screen.Max = monitor.WorkPos + monitor.WorkSize;\n    }\n    else\n    {\n        r_screen.Min = window->Viewport->Pos;\n        r_screen.Max = window->Viewport->Pos + window->Viewport->Size;\n    }\n    ImVec2 padding = g.Style.DisplaySafeAreaPadding;\n    r_screen.Expand(ImVec2((r_screen.GetWidth() > padding.x * 2) ? -padding.x : 0.0f, (r_screen.GetHeight() > padding.y * 2) ? -padding.y : 0.0f));\n    return r_screen;\n}\n\nImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    if (window->Flags & ImGuiWindowFlags_ChildMenu)\n    {\n        // Child menus typically request _any_ position within the parent menu item, and then we move the new menu outside the parent bounds.\n        // This is how we end up with child menus appearing (most-commonly) on the right of the parent menu.\n        ImGuiWindow* parent_window = window->ParentWindow;\n        float horizontal_overlap = g.Style.ItemInnerSpacing.x; // We want some overlap to convey the relative depth of each menu (currently the amount of overlap is hard-coded to style.ItemSpacing.x).\n        ImRect r_outer = GetWindowAllowedExtentRect(window);\n        ImRect r_avoid;\n        if (parent_window->DC.MenuBarAppending)\n            r_avoid = ImRect(-FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight(), FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight() + parent_window->MenuBarHeight());\n        else\n            r_avoid = ImRect(parent_window->Pos.x + horizontal_overlap, -FLT_MAX, parent_window->Pos.x + parent_window->Size.x - horizontal_overlap - parent_window->ScrollbarSizes.x, FLT_MAX);\n        return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid);\n    }\n    if (window->Flags & ImGuiWindowFlags_Popup)\n    {\n        ImRect r_outer = GetWindowAllowedExtentRect(window);\n        ImRect r_avoid = ImRect(window->Pos.x - 1, window->Pos.y - 1, window->Pos.x + 1, window->Pos.y + 1);\n        return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid);\n    }\n    if (window->Flags & ImGuiWindowFlags_Tooltip)\n    {\n        // Position tooltip (always follows mouse)\n        float sc = g.Style.MouseCursorScale;\n        ImVec2 ref_pos = NavCalcPreferredRefPos();\n        ImRect r_outer = GetWindowAllowedExtentRect(window);\n        ImRect r_avoid;\n        if (!g.NavDisableHighlight && g.NavDisableMouseHover && !(g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos))\n            r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 16, ref_pos.y + 8);\n        else\n            r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 24 * sc, ref_pos.y + 24 * sc); // FIXME: Hard-coded based on mouse cursor shape expectation. Exact dimension not very important.\n        ImVec2 pos = FindBestWindowPosForPopupEx(ref_pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid);\n        if (window->AutoPosLastDirection == ImGuiDir_None)\n            pos = ref_pos + ImVec2(2, 2); // If there's not enough room, for tooltip we prefer avoiding the cursor at all cost even if it means that part of the tooltip won't be visible.\n        return pos;\n    }\n    IM_ASSERT(0);\n    return window->Pos;\n}\n\n\n//-----------------------------------------------------------------------------\n// [SECTION] KEYBOARD/GAMEPAD NAVIGATION\n//-----------------------------------------------------------------------------\n\nImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy)\n{\n    if (ImFabs(dx) > ImFabs(dy))\n        return (dx > 0.0f) ? ImGuiDir_Right : ImGuiDir_Left;\n    return (dy > 0.0f) ? ImGuiDir_Down : ImGuiDir_Up;\n}\n\nstatic float inline NavScoreItemDistInterval(float a0, float a1, float b0, float b1)\n{\n    if (a1 < b0)\n        return a1 - b0;\n    if (b1 < a0)\n        return a0 - b1;\n    return 0.0f;\n}\n\nstatic void inline NavClampRectToVisibleAreaForMoveDir(ImGuiDir move_dir, ImRect& r, const ImRect& clip_rect)\n{\n    if (move_dir == ImGuiDir_Left || move_dir == ImGuiDir_Right)\n    {\n        r.Min.y = ImClamp(r.Min.y, clip_rect.Min.y, clip_rect.Max.y);\n        r.Max.y = ImClamp(r.Max.y, clip_rect.Min.y, clip_rect.Max.y);\n    }\n    else\n    {\n        r.Min.x = ImClamp(r.Min.x, clip_rect.Min.x, clip_rect.Max.x);\n        r.Max.x = ImClamp(r.Max.x, clip_rect.Min.x, clip_rect.Max.x);\n    }\n}\n\n// Scoring function for directional navigation. Based on https://gist.github.com/rygorous/6981057\nstatic bool NavScoreItem(ImGuiNavMoveResult* result, ImRect cand)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (g.NavLayer != window->DC.NavLayerCurrent)\n        return false;\n\n    const ImRect& curr = g.NavScoringRectScreen; // Current modified source rect (NB: we've applied Max.x = Min.x in NavUpdate() to inhibit the effect of having varied item width)\n    g.NavScoringCount++;\n\n    // When entering through a NavFlattened border, we consider child window items as fully clipped for scoring\n    if (window->ParentWindow == g.NavWindow)\n    {\n        IM_ASSERT((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened);\n        if (!window->ClipRect.Contains(cand))\n            return false;\n        cand.ClipWithFull(window->ClipRect); // This allows the scored item to not overlap other candidates in the parent window\n    }\n\n    // We perform scoring on items bounding box clipped by the current clipping rectangle on the other axis (clipping on our movement axis would give us equal scores for all clipped items)\n    // For example, this ensure that items in one column are not reached when moving vertically from items in another column.\n    NavClampRectToVisibleAreaForMoveDir(g.NavMoveClipDir, cand, window->ClipRect);\n\n    // Compute distance between boxes\n    // FIXME-NAV: Introducing biases for vertical navigation, needs to be removed.\n    float dbx = NavScoreItemDistInterval(cand.Min.x, cand.Max.x, curr.Min.x, curr.Max.x);\n    float dby = NavScoreItemDistInterval(ImLerp(cand.Min.y, cand.Max.y, 0.2f), ImLerp(cand.Min.y, cand.Max.y, 0.8f), ImLerp(curr.Min.y, curr.Max.y, 0.2f), ImLerp(curr.Min.y, curr.Max.y, 0.8f)); // Scale down on Y to keep using box-distance for vertically touching items\n    if (dby != 0.0f && dbx != 0.0f)\n       dbx = (dbx/1000.0f) + ((dbx > 0.0f) ? +1.0f : -1.0f);\n    float dist_box = ImFabs(dbx) + ImFabs(dby);\n\n    // Compute distance between centers (this is off by a factor of 2, but we only compare center distances with each other so it doesn't matter)\n    float dcx = (cand.Min.x + cand.Max.x) - (curr.Min.x + curr.Max.x);\n    float dcy = (cand.Min.y + cand.Max.y) - (curr.Min.y + curr.Max.y);\n    float dist_center = ImFabs(dcx) + ImFabs(dcy); // L1 metric (need this for our connectedness guarantee)\n\n    // Determine which quadrant of 'curr' our candidate item 'cand' lies in based on distance\n    ImGuiDir quadrant;\n    float dax = 0.0f, day = 0.0f, dist_axial = 0.0f;\n    if (dbx != 0.0f || dby != 0.0f)\n    {\n        // For non-overlapping boxes, use distance between boxes\n        dax = dbx;\n        day = dby;\n        dist_axial = dist_box;\n        quadrant = ImGetDirQuadrantFromDelta(dbx, dby);\n    }\n    else if (dcx != 0.0f || dcy != 0.0f)\n    {\n        // For overlapping boxes with different centers, use distance between centers\n        dax = dcx;\n        day = dcy;\n        dist_axial = dist_center;\n        quadrant = ImGetDirQuadrantFromDelta(dcx, dcy);\n    }\n    else\n    {\n        // Degenerate case: two overlapping buttons with same center, break ties arbitrarily (note that LastItemId here is really the _previous_ item order, but it doesn't matter)\n        quadrant = (window->DC.LastItemId < g.NavId) ? ImGuiDir_Left : ImGuiDir_Right;\n    }\n\n#if IMGUI_DEBUG_NAV_SCORING\n    char buf[128];\n    if (ImGui::IsMouseHoveringRect(cand.Min, cand.Max))\n    {\n        ImFormatString(buf, IM_ARRAYSIZE(buf), \"dbox (%.2f,%.2f->%.4f)\\ndcen (%.2f,%.2f->%.4f)\\nd (%.2f,%.2f->%.4f)\\nnav %c, quadrant %c\", dbx, dby, dist_box, dcx, dcy, dist_center, dax, day, dist_axial, \"WENS\"[g.NavMoveDir], \"WENS\"[quadrant]);\n        ImDrawList* draw_list = ImGui::GetForegroundDrawList(window);\n        draw_list->AddRect(curr.Min, curr.Max, IM_COL32(255,200,0,100));\n        draw_list->AddRect(cand.Min, cand.Max, IM_COL32(255,255,0,200));\n        draw_list->AddRectFilled(cand.Max-ImVec2(4,4), cand.Max+ImGui::CalcTextSize(buf)+ImVec2(4,4), IM_COL32(40,0,0,150));\n        draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Max, ~0U, buf);\n    }\n    else if (g.IO.KeyCtrl) // Hold to preview score in matching quadrant. Press C to rotate.\n    {\n        if (ImGui::IsKeyPressedMap(ImGuiKey_C)) { g.NavMoveDirLast = (ImGuiDir)((g.NavMoveDirLast + 1) & 3); g.IO.KeysDownDuration[g.IO.KeyMap[ImGuiKey_C]] = 0.01f; }\n        if (quadrant == g.NavMoveDir)\n        {\n            ImFormatString(buf, IM_ARRAYSIZE(buf), \"%.0f/%.0f\", dist_box, dist_center);\n            ImDrawList* draw_list = ImGui::GetForegroundDrawList(window);\n            draw_list->AddRectFilled(cand.Min, cand.Max, IM_COL32(255, 0, 0, 200));\n            draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Min, IM_COL32(255, 255, 255, 255), buf);\n        }\n    }\n #endif\n\n    // Is it in the quadrant we're interesting in moving to?\n    bool new_best = false;\n    if (quadrant == g.NavMoveDir)\n    {\n        // Does it beat the current best candidate?\n        if (dist_box < result->DistBox)\n        {\n            result->DistBox = dist_box;\n            result->DistCenter = dist_center;\n            return true;\n        }\n        if (dist_box == result->DistBox)\n        {\n            // Try using distance between center points to break ties\n            if (dist_center < result->DistCenter)\n            {\n                result->DistCenter = dist_center;\n                new_best = true;\n            }\n            else if (dist_center == result->DistCenter)\n            {\n                // Still tied! we need to be extra-careful to make sure everything gets linked properly. We consistently break ties by symbolically moving \"later\" items\n                // (with higher index) to the right/downwards by an infinitesimal amount since we the current \"best\" button already (so it must have a lower index),\n                // this is fairly easy. This rule ensures that all buttons with dx==dy==0 will end up being linked in order of appearance along the x axis.\n                if (((g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) ? dby : dbx) < 0.0f) // moving bj to the right/down decreases distance\n                    new_best = true;\n            }\n        }\n    }\n\n    // Axial check: if 'curr' has no link at all in some direction and 'cand' lies roughly in that direction, add a tentative link. This will only be kept if no \"real\" matches\n    // are found, so it only augments the graph produced by the above method using extra links. (important, since it doesn't guarantee strong connectedness)\n    // This is just to avoid buttons having no links in a particular direction when there's a suitable neighbor. you get good graphs without this too.\n    // 2017/09/29: FIXME: This now currently only enabled inside menu bars, ideally we'd disable it everywhere. Menus in particular need to catch failure. For general navigation it feels awkward.\n    // Disabling it may lead to disconnected graphs when nodes are very spaced out on different axis. Perhaps consider offering this as an option?\n    if (result->DistBox == FLT_MAX && dist_axial < result->DistAxial)  // Check axial match\n        if (g.NavLayer == 1 && !(g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu))\n            if ((g.NavMoveDir == ImGuiDir_Left && dax < 0.0f) || (g.NavMoveDir == ImGuiDir_Right && dax > 0.0f) || (g.NavMoveDir == ImGuiDir_Up && day < 0.0f) || (g.NavMoveDir == ImGuiDir_Down && day > 0.0f))\n            {\n                result->DistAxial = dist_axial;\n                new_best = true;\n            }\n\n    return new_best;\n}\n\n// We get there when either NavId == id, or when g.NavAnyRequest is set (which is updated by NavUpdateAnyRequestFlag above)\nstatic void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, const ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    //if (!g.IO.NavActive)  // [2017/10/06] Removed this possibly redundant test but I am not sure of all the side-effects yet. Some of the feature here will need to work regardless of using a _NoNavInputs flag.\n    //    return;\n\n    const ImGuiItemFlags item_flags = window->DC.ItemFlags;\n    const ImRect nav_bb_rel(nav_bb.Min - window->Pos, nav_bb.Max - window->Pos);\n\n    // Process Init Request\n    if (g.NavInitRequest && g.NavLayer == window->DC.NavLayerCurrent)\n    {\n        // Even if 'ImGuiItemFlags_NoNavDefaultFocus' is on (typically collapse/close button) we record the first ResultId so they can be used as a fallback\n        if (!(item_flags & ImGuiItemFlags_NoNavDefaultFocus) || g.NavInitResultId == 0)\n        {\n            g.NavInitResultId = id;\n            g.NavInitResultRectRel = nav_bb_rel;\n        }\n        if (!(item_flags & ImGuiItemFlags_NoNavDefaultFocus))\n        {\n            g.NavInitRequest = false; // Found a match, clear request\n            NavUpdateAnyRequestFlag();\n        }\n    }\n\n    // Process Move Request (scoring for navigation)\n    // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRectScreen + scoring from a rect wrapped according to current wrapping policy)\n    if ((g.NavId != id || (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled|ImGuiItemFlags_NoNav)))\n    {\n        ImGuiNavMoveResult* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther;\n#if IMGUI_DEBUG_NAV_SCORING\n        // [DEBUG] Score all items in NavWindow at all times\n        if (!g.NavMoveRequest)\n            g.NavMoveDir = g.NavMoveDirLast;\n        bool new_best = NavScoreItem(result, nav_bb) && g.NavMoveRequest;\n#else\n        bool new_best = g.NavMoveRequest && NavScoreItem(result, nav_bb);\n#endif\n        if (new_best)\n        {\n            result->ID = id;\n            result->SelectScopeId = g.MultiSelectScopeId;\n            result->Window = window;\n            result->RectRel = nav_bb_rel;\n        }\n\n        const float VISIBLE_RATIO = 0.70f;\n        if ((g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb))\n            if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO)\n                if (NavScoreItem(&g.NavMoveResultLocalVisibleSet, nav_bb))\n                {\n                    result = &g.NavMoveResultLocalVisibleSet;\n                    result->ID = id;\n                    result->SelectScopeId = g.MultiSelectScopeId;\n                    result->Window = window;\n                    result->RectRel = nav_bb_rel;\n                }\n    }\n\n    // Update window-relative bounding box of navigated item\n    if (g.NavId == id)\n    {\n        g.NavWindow = window;                                           // Always refresh g.NavWindow, because some operations such as FocusItem() don't have a window.\n        g.NavLayer = window->DC.NavLayerCurrent;\n        g.NavIdIsAlive = true;\n        g.NavIdTabCounter = window->DC.FocusCounterTab;\n        window->NavRectRel[window->DC.NavLayerCurrent] = nav_bb_rel;    // Store item bounding box (relative to window position)\n    }\n}\n\nbool ImGui::NavMoveRequestButNoResultYet()\n{\n    ImGuiContext& g = *GImGui;\n    return g.NavMoveRequest && g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0;\n}\n\nvoid ImGui::NavMoveRequestCancel()\n{\n    ImGuiContext& g = *GImGui;\n    g.NavMoveRequest = false;\n    NavUpdateAnyRequestFlag();\n}\n\nvoid ImGui::NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const ImRect& bb_rel, ImGuiNavMoveFlags move_flags)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.NavMoveRequestForward == ImGuiNavForward_None);\n    ImGui::NavMoveRequestCancel();\n    g.NavMoveDir = move_dir;\n    g.NavMoveClipDir = clip_dir;\n    g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued;\n    g.NavMoveRequestFlags = move_flags;\n    g.NavWindow->NavRectRel[g.NavLayer] = bb_rel;\n}\n\nvoid ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.NavWindow != window || !NavMoveRequestButNoResultYet() || g.NavMoveRequestForward != ImGuiNavForward_None || g.NavLayer != 0)\n        return;\n    IM_ASSERT(move_flags != 0); // No points calling this with no wrapping\n    ImRect bb_rel = window->NavRectRel[0];\n\n    ImGuiDir clip_dir = g.NavMoveDir;\n    if (g.NavMoveDir == ImGuiDir_Left && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX)))\n    {\n        bb_rel.Min.x = bb_rel.Max.x = ImMax(window->SizeFull.x, window->SizeContents.x) - window->Scroll.x;\n        if (move_flags & ImGuiNavMoveFlags_WrapX) { bb_rel.TranslateY(-bb_rel.GetHeight()); clip_dir = ImGuiDir_Up; }\n        NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags);\n    }\n    if (g.NavMoveDir == ImGuiDir_Right && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX)))\n    {\n        bb_rel.Min.x = bb_rel.Max.x = -window->Scroll.x;\n        if (move_flags & ImGuiNavMoveFlags_WrapX) { bb_rel.TranslateY(+bb_rel.GetHeight()); clip_dir = ImGuiDir_Down; }\n        NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags);\n    }\n    if (g.NavMoveDir == ImGuiDir_Up && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY)))\n    {\n        bb_rel.Min.y = bb_rel.Max.y = ImMax(window->SizeFull.y, window->SizeContents.y) - window->Scroll.y;\n        if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(-bb_rel.GetWidth()); clip_dir = ImGuiDir_Left; }\n        NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags);\n    }\n    if (g.NavMoveDir == ImGuiDir_Down && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY)))\n    {\n        bb_rel.Min.y = bb_rel.Max.y = -window->Scroll.y;\n        if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(+bb_rel.GetWidth()); clip_dir = ImGuiDir_Right; }\n        NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags);\n    }\n}\n\n// FIXME: This could be replaced by updating a frame number in each window when (window == NavWindow) and (NavLayer == 0).\n// This way we could find the last focused window among our children. It would be much less confusing this way?\nstatic void ImGui::NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window)\n{\n    ImGuiWindow* parent_window = nav_window;\n    while (parent_window && (parent_window->Flags & ImGuiWindowFlags_ChildWindow) != 0 && (parent_window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0)\n        parent_window = parent_window->ParentWindow;\n    if (parent_window && parent_window != nav_window)\n        parent_window->NavLastChildNavWindow = nav_window;\n}\n\n// Restore the last focused child.\n// Call when we are expected to land on the Main Layer (0) after FocusWindow()\nstatic ImGuiWindow* ImGui::NavRestoreLastChildNavWindow(ImGuiWindow* window)\n{\n    if (window->NavLastChildNavWindow && window->NavLastChildNavWindow->WasActive)\n        return window->NavLastChildNavWindow;\n    if (window->DockNodeAsHost && window->DockNodeAsHost->TabBar)\n        if (ImGuiTabItem* tab = TabBarFindMostRecentlySelectedTabForActiveWindow(window->DockNodeAsHost->TabBar))\n            return tab->Window;\n    return window;\n}\n\nstatic void NavRestoreLayer(ImGuiNavLayer layer)\n{\n    ImGuiContext& g = *GImGui;\n    g.NavLayer = layer;\n    if (layer == 0)\n        g.NavWindow = ImGui::NavRestoreLastChildNavWindow(g.NavWindow);\n    if (g.NavWindow->NavLastIds[layer] != 0)\n        ImGui::SetNavIDWithRectRel(g.NavWindow->NavLastIds[layer], layer, g.NavWindow->NavRectRel[layer]);\n    else\n        ImGui::NavInitWindow(g.NavWindow, true);\n}\n\nstatic inline void ImGui::NavUpdateAnyRequestFlag()\n{\n    ImGuiContext& g = *GImGui;\n    g.NavAnyRequest = g.NavMoveRequest || g.NavInitRequest || (IMGUI_DEBUG_NAV_SCORING && g.NavWindow != NULL);\n    if (g.NavAnyRequest)\n        IM_ASSERT(g.NavWindow != NULL);\n}\n\n// This needs to be called before we submit any widget (aka in or before Begin)\nvoid ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(window == g.NavWindow);\n    bool init_for_nav = false;\n    if (!(window->Flags & ImGuiWindowFlags_NoNavInputs))\n        if (!(window->Flags & ImGuiWindowFlags_ChildWindow) || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastIds[0] == 0) || force_reinit)\n            init_for_nav = true;\n    if (init_for_nav)\n    {\n        SetNavID(0, g.NavLayer);\n        g.NavInitRequest = true;\n        g.NavInitRequestFromMove = false;\n        g.NavInitResultId = 0;\n        g.NavInitResultRectRel = ImRect();\n        NavUpdateAnyRequestFlag();\n    }\n    else\n    {\n        g.NavId = window->NavLastIds[0];\n    }\n}\n\nstatic ImVec2 ImGui::NavCalcPreferredRefPos()\n{\n    ImGuiContext& g = *GImGui;\n    if (g.NavDisableHighlight || !g.NavDisableMouseHover || !g.NavWindow)\n    {\n        // Mouse (we need a fallback in case the mouse becomes invalid after being used)\n        if (IsMousePosValid(&g.IO.MousePos))\n            return g.IO.MousePos;\n        return g.LastValidMousePos;\n    }\n    else\n    {\n        // When navigation is active and mouse is disabled, decide on an arbitrary position around the bottom left of the currently navigated item.\n        const ImRect& rect_rel = g.NavWindow->NavRectRel[g.NavLayer];\n        ImVec2 pos = g.NavWindow->Pos + ImVec2(rect_rel.Min.x + ImMin(g.Style.FramePadding.x * 4, rect_rel.GetWidth()), rect_rel.Max.y - ImMin(g.Style.FramePadding.y, rect_rel.GetHeight()));\n        ImRect visible_rect = g.NavWindow->Viewport->GetRect();\n        return ImFloor(ImClamp(pos, visible_rect.Min, visible_rect.Max));   // ImFloor() is important because non-integer mouse position application in back-end might be lossy and result in undesirable non-zero delta.\n    }\n}\n\nfloat ImGui::GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode)\n{\n    ImGuiContext& g = *GImGui;\n    if (mode == ImGuiInputReadMode_Down)\n        return g.IO.NavInputs[n];                         // Instant, read analog input (0.0f..1.0f, as provided by user)\n\n    const float t = g.IO.NavInputsDownDuration[n];\n    if (t < 0.0f && mode == ImGuiInputReadMode_Released)  // Return 1.0f when just released, no repeat, ignore analog input.\n        return (g.IO.NavInputsDownDurationPrev[n] >= 0.0f ? 1.0f : 0.0f);\n    if (t < 0.0f)\n        return 0.0f;\n    if (mode == ImGuiInputReadMode_Pressed)               // Return 1.0f when just pressed, no repeat, ignore analog input.\n        return (t == 0.0f) ? 1.0f : 0.0f;\n    if (mode == ImGuiInputReadMode_Repeat)\n        return (float)CalcTypematicPressedRepeatAmount(t, t - g.IO.DeltaTime, g.IO.KeyRepeatDelay * 0.80f, g.IO.KeyRepeatRate * 0.80f);\n    if (mode == ImGuiInputReadMode_RepeatSlow)\n        return (float)CalcTypematicPressedRepeatAmount(t, t - g.IO.DeltaTime, g.IO.KeyRepeatDelay * 1.00f, g.IO.KeyRepeatRate * 2.00f);\n    if (mode == ImGuiInputReadMode_RepeatFast)\n        return (float)CalcTypematicPressedRepeatAmount(t, t - g.IO.DeltaTime, g.IO.KeyRepeatDelay * 0.80f, g.IO.KeyRepeatRate * 0.30f);\n    return 0.0f;\n}\n\nImVec2 ImGui::GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor, float fast_factor)\n{\n    ImVec2 delta(0.0f, 0.0f);\n    if (dir_sources & ImGuiNavDirSourceFlags_Keyboard)\n        delta += ImVec2(GetNavInputAmount(ImGuiNavInput_KeyRight_, mode)   - GetNavInputAmount(ImGuiNavInput_KeyLeft_,   mode), GetNavInputAmount(ImGuiNavInput_KeyDown_,   mode) - GetNavInputAmount(ImGuiNavInput_KeyUp_,   mode));\n    if (dir_sources & ImGuiNavDirSourceFlags_PadDPad)\n        delta += ImVec2(GetNavInputAmount(ImGuiNavInput_DpadRight, mode)   - GetNavInputAmount(ImGuiNavInput_DpadLeft,   mode), GetNavInputAmount(ImGuiNavInput_DpadDown,   mode) - GetNavInputAmount(ImGuiNavInput_DpadUp,   mode));\n    if (dir_sources & ImGuiNavDirSourceFlags_PadLStick)\n        delta += ImVec2(GetNavInputAmount(ImGuiNavInput_LStickRight, mode) - GetNavInputAmount(ImGuiNavInput_LStickLeft, mode), GetNavInputAmount(ImGuiNavInput_LStickDown, mode) - GetNavInputAmount(ImGuiNavInput_LStickUp, mode));\n    if (slow_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakSlow))\n        delta *= slow_factor;\n    if (fast_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakFast))\n        delta *= fast_factor;\n    return delta;\n}\n\n// Scroll to keep newly navigated item fully into view\n// NB: We modify rect_rel by the amount we scrolled for, so it is immediately updated.\nstatic void NavScrollToBringItemIntoView(ImGuiWindow* window, const ImRect& item_rect)\n{\n    ImRect window_rect(window->InnerMainRect.Min - ImVec2(1, 1), window->InnerMainRect.Max + ImVec2(1, 1));\n    //GetForegroundDrawList(window)->AddRect(window_rect.Min, window_rect.Max, IM_COL32_WHITE); // [DEBUG]\n    if (window_rect.Contains(item_rect))\n        return;\n\n    ImGuiContext& g = *GImGui;\n    if (window->ScrollbarX && item_rect.Min.x < window_rect.Min.x)\n    {\n        window->ScrollTarget.x = item_rect.Min.x - window->Pos.x + window->Scroll.x - g.Style.ItemSpacing.x;\n        window->ScrollTargetCenterRatio.x = 0.0f;\n    }\n    else if (window->ScrollbarX && item_rect.Max.x >= window_rect.Max.x)\n    {\n        window->ScrollTarget.x = item_rect.Max.x - window->Pos.x + window->Scroll.x + g.Style.ItemSpacing.x;\n        window->ScrollTargetCenterRatio.x = 1.0f;\n    }\n    if (item_rect.Min.y < window_rect.Min.y)\n    {\n        window->ScrollTarget.y = item_rect.Min.y - window->Pos.y + window->Scroll.y - g.Style.ItemSpacing.y;\n        window->ScrollTargetCenterRatio.y = 0.0f;\n    }\n    else if (item_rect.Max.y >= window_rect.Max.y)\n    {\n        window->ScrollTarget.y = item_rect.Max.y - window->Pos.y + window->Scroll.y + g.Style.ItemSpacing.y;\n        window->ScrollTargetCenterRatio.y = 1.0f;\n    }\n}\n\nstatic void ImGui::NavUpdate()\n{\n    ImGuiContext& g = *GImGui;\n    g.IO.WantSetMousePos = false;\n#if 0\n    if (g.NavScoringCount > 0) IMGUI_DEBUG_LOG(\"NavScoringCount %d for '%s' layer %d (Init:%d, Move:%d)\\n\", g.FrameCount, g.NavScoringCount, g.NavWindow ? g.NavWindow->Name : \"NULL\", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest);\n#endif\n\n    // Set input source as Gamepad when buttons are pressed before we map Keyboard (some features differs when used with Gamepad vs Keyboard)\n    bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0;\n    bool nav_gamepad_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (g.IO.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0;\n    if (nav_gamepad_active)\n        if (g.IO.NavInputs[ImGuiNavInput_Activate] > 0.0f || g.IO.NavInputs[ImGuiNavInput_Input] > 0.0f || g.IO.NavInputs[ImGuiNavInput_Cancel] > 0.0f || g.IO.NavInputs[ImGuiNavInput_Menu] > 0.0f)\n            g.NavInputSource = ImGuiInputSource_NavGamepad;\n\n    // Update Keyboard->Nav inputs mapping\n    if (nav_keyboard_active)\n    {\n        #define NAV_MAP_KEY(_KEY, _NAV_INPUT) if (IsKeyDown(g.IO.KeyMap[_KEY])) { g.IO.NavInputs[_NAV_INPUT] = 1.0f; g.NavInputSource = ImGuiInputSource_NavKeyboard; }\n        NAV_MAP_KEY(ImGuiKey_Space,     ImGuiNavInput_Activate );\n        NAV_MAP_KEY(ImGuiKey_Enter,     ImGuiNavInput_Input    );\n        NAV_MAP_KEY(ImGuiKey_Escape,    ImGuiNavInput_Cancel   );\n        NAV_MAP_KEY(ImGuiKey_LeftArrow, ImGuiNavInput_KeyLeft_ );\n        NAV_MAP_KEY(ImGuiKey_RightArrow,ImGuiNavInput_KeyRight_);\n        NAV_MAP_KEY(ImGuiKey_UpArrow,   ImGuiNavInput_KeyUp_   );\n        NAV_MAP_KEY(ImGuiKey_DownArrow, ImGuiNavInput_KeyDown_ );\n        NAV_MAP_KEY(ImGuiKey_Tab,       ImGuiNavInput_KeyTab_  );\n        if (g.IO.KeyCtrl)\n            g.IO.NavInputs[ImGuiNavInput_TweakSlow] = 1.0f;\n        if (g.IO.KeyShift)\n            g.IO.NavInputs[ImGuiNavInput_TweakFast] = 1.0f;\n        if (g.IO.KeyAlt && !g.IO.KeyCtrl) // AltGR is Alt+Ctrl, also even on keyboards without AltGR we don't want Alt+Ctrl to open menu.\n            g.IO.NavInputs[ImGuiNavInput_KeyMenu_]  = 1.0f;\n        #undef NAV_MAP_KEY\n    }\n    memcpy(g.IO.NavInputsDownDurationPrev, g.IO.NavInputsDownDuration, sizeof(g.IO.NavInputsDownDuration));\n    for (int i = 0; i < IM_ARRAYSIZE(g.IO.NavInputs); i++)\n        g.IO.NavInputsDownDuration[i] = (g.IO.NavInputs[i] > 0.0f) ? (g.IO.NavInputsDownDuration[i] < 0.0f ? 0.0f : g.IO.NavInputsDownDuration[i] + g.IO.DeltaTime) : -1.0f;\n\n    // Process navigation init request (select first/default focus)\n    if (g.NavInitResultId != 0 && (!g.NavDisableHighlight || g.NavInitRequestFromMove))\n    {\n        // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called)\n        IM_ASSERT(g.NavWindow);\n        if (g.NavInitRequestFromMove)\n            SetNavIDWithRectRel(g.NavInitResultId, g.NavLayer, g.NavInitResultRectRel);\n        else\n            SetNavID(g.NavInitResultId, g.NavLayer);\n        g.NavWindow->NavRectRel[g.NavLayer] = g.NavInitResultRectRel;\n    }\n    g.NavInitRequest = false;\n    g.NavInitRequestFromMove = false;\n    g.NavInitResultId = 0;\n    g.NavJustMovedToId = 0;\n\n    // Process navigation move request\n    if (g.NavMoveRequest)\n        NavUpdateMoveResult();\n\n    // When a forwarded move request failed, we restore the highlight that we disabled during the forward frame\n    if (g.NavMoveRequestForward == ImGuiNavForward_ForwardActive)\n    {\n        IM_ASSERT(g.NavMoveRequest);\n        if (g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0)\n            g.NavDisableHighlight = false;\n        g.NavMoveRequestForward = ImGuiNavForward_None;\n    }\n\n    // Apply application mouse position movement, after we had a chance to process move request result.\n    if (g.NavMousePosDirty && g.NavIdIsAlive)\n    {\n        // Set mouse position given our knowledge of the navigated item position from last frame\n        if ((g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (g.IO.BackendFlags & ImGuiBackendFlags_HasSetMousePos))\n        {\n            if (!g.NavDisableHighlight && g.NavDisableMouseHover && g.NavWindow)\n            {\n                g.IO.MousePos = g.IO.MousePosPrev = NavCalcPreferredRefPos();\n                g.IO.WantSetMousePos = true;\n            }\n        }\n        g.NavMousePosDirty = false;\n    }\n    g.NavIdIsAlive = false;\n    g.NavJustTabbedId = 0;\n    IM_ASSERT(g.NavLayer == 0 || g.NavLayer == 1);\n\n    // Store our return window (for returning from Layer 1 to Layer 0) and clear it as soon as we step back in our own Layer 0\n    if (g.NavWindow)\n        NavSaveLastChildNavWindowIntoParent(g.NavWindow);\n    if (g.NavWindow && g.NavWindow->NavLastChildNavWindow != NULL && g.NavLayer == 0)\n        g.NavWindow->NavLastChildNavWindow = NULL;\n\n    // Update CTRL+TAB and Windowing features (hold Square to move/resize/etc.)\n    NavUpdateWindowing();\n\n    // Set output flags for user application\n    g.IO.NavActive = (nav_keyboard_active || nav_gamepad_active) && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs);\n    g.IO.NavVisible = (g.IO.NavActive && g.NavId != 0 && !g.NavDisableHighlight) || (g.NavWindowingTarget != NULL);\n\n    // Process NavCancel input (to close a popup, get back to parent, clear focus)\n    if (IsNavInputPressed(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed))\n    {\n        if (g.ActiveId != 0)\n        {\n            if (!(g.ActiveIdBlockNavInputFlags & (1 << ImGuiNavInput_Cancel)))\n                ClearActiveID();\n        }\n        else if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow) && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow && g.NavWindow != g.NavWindow->RootWindowDockStop)\n        {\n            // Exit child window\n            ImGuiWindow* child_window = g.NavWindow;\n            ImGuiWindow* parent_window = g.NavWindow->ParentWindow;\n            IM_ASSERT(child_window->ChildId != 0);\n            FocusWindow(parent_window);\n            SetNavID(child_window->ChildId, 0);\n            g.NavIdIsAlive = false;\n            if (g.NavDisableMouseHover)\n                g.NavMousePosDirty = true;\n        }\n        else if (g.OpenPopupStack.Size > 0)\n        {\n            // Close open popup/menu\n            if (!(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal))\n                ClosePopupToLevel(g.OpenPopupStack.Size - 1, true);\n        }\n        else if (g.NavLayer != 0)\n        {\n            // Leave the \"menu\" layer\n            NavRestoreLayer(ImGuiNavLayer_Main);\n        }\n        else\n        {\n            // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were\n            if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow)))\n                g.NavWindow->NavLastIds[0] = 0;\n            g.NavId = 0;\n        }\n    }\n\n    // Process manual activation request\n    g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavInputId = 0;\n    if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs))\n    {\n        bool activate_down = IsNavInputDown(ImGuiNavInput_Activate);\n        bool activate_pressed = activate_down && IsNavInputPressed(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed);\n        if (g.ActiveId == 0 && activate_pressed)\n            g.NavActivateId = g.NavId;\n        if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_down)\n            g.NavActivateDownId = g.NavId;\n        if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_pressed)\n            g.NavActivatePressedId = g.NavId;\n        if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && IsNavInputPressed(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed))\n            g.NavInputId = g.NavId;\n    }\n    if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs))\n        g.NavDisableHighlight = true;\n    if (g.NavActivateId != 0)\n        IM_ASSERT(g.NavActivateDownId == g.NavActivateId);\n    g.NavMoveRequest = false;\n\n    // Process programmatic activation request\n    if (g.NavNextActivateId != 0)\n        g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavInputId = g.NavNextActivateId;\n    g.NavNextActivateId = 0;\n\n    // Initiate directional inputs request\n    const int allowed_dir_flags = (g.ActiveId == 0) ? ~0 : g.ActiveIdAllowNavDirFlags;\n    if (g.NavMoveRequestForward == ImGuiNavForward_None)\n    {\n        g.NavMoveDir = ImGuiDir_None;\n        g.NavMoveRequestFlags = ImGuiNavMoveFlags_None;\n        if (g.NavWindow && !g.NavWindowingTarget && allowed_dir_flags && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs))\n        {\n            if ((allowed_dir_flags & (1<<ImGuiDir_Left))  && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadLeft, ImGuiNavInput_KeyLeft_, ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Left;\n            if ((allowed_dir_flags & (1<<ImGuiDir_Right)) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadRight,ImGuiNavInput_KeyRight_,ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Right;\n            if ((allowed_dir_flags & (1<<ImGuiDir_Up))    && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadUp,   ImGuiNavInput_KeyUp_,   ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Up;\n            if ((allowed_dir_flags & (1<<ImGuiDir_Down))  && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadDown, ImGuiNavInput_KeyDown_, ImGuiInputReadMode_Repeat)) g.NavMoveDir = ImGuiDir_Down;\n        }\n        g.NavMoveClipDir = g.NavMoveDir;\n    }\n    else\n    {\n        // Forwarding previous request (which has been modified, e.g. wrap around menus rewrite the requests with a starting rectangle at the other side of the window)\n        // (Preserve g.NavMoveRequestFlags, g.NavMoveClipDir which were set by the NavMoveRequestForward() function)\n        IM_ASSERT(g.NavMoveDir != ImGuiDir_None && g.NavMoveClipDir != ImGuiDir_None);\n        IM_ASSERT(g.NavMoveRequestForward == ImGuiNavForward_ForwardQueued);\n        g.NavMoveRequestForward = ImGuiNavForward_ForwardActive;\n    }\n\n    // Update PageUp/PageDown scroll\n    float nav_scoring_rect_offset_y = 0.0f;\n    if (nav_keyboard_active)\n        nav_scoring_rect_offset_y = NavUpdatePageUpPageDown(allowed_dir_flags);\n\n    // If we initiate a movement request and have no current NavId, we initiate a InitDefautRequest that will be used as a fallback if the direction fails to find a match\n    if (g.NavMoveDir != ImGuiDir_None)\n    {\n        g.NavMoveRequest = true;\n        g.NavMoveDirLast = g.NavMoveDir;\n    }\n    if (g.NavMoveRequest && g.NavId == 0)\n    {\n        g.NavInitRequest = g.NavInitRequestFromMove = true;\n        g.NavInitResultId = 0;\n        g.NavDisableHighlight = false;\n    }\n    NavUpdateAnyRequestFlag();\n\n    // Scrolling\n    if (g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.NavWindowingTarget)\n    {\n        // *Fallback* manual-scroll with Nav directional keys when window has no navigable item\n        ImGuiWindow* window = g.NavWindow;\n        const float scroll_speed = ImFloor(window->CalcFontSize() * 100 * g.IO.DeltaTime + 0.5f); // We need round the scrolling speed because sub-pixel scroll isn't reliably supported.\n        if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll && g.NavMoveRequest)\n        {\n            if (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right)\n                SetWindowScrollX(window, ImFloor(window->Scroll.x + ((g.NavMoveDir == ImGuiDir_Left) ? -1.0f : +1.0f) * scroll_speed));\n            if (g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down)\n                SetWindowScrollY(window, ImFloor(window->Scroll.y + ((g.NavMoveDir == ImGuiDir_Up) ? -1.0f : +1.0f) * scroll_speed));\n        }\n\n        // *Normal* Manual scroll with NavScrollXXX keys\n        // Next movement request will clamp the NavId reference rectangle to the visible area, so navigation will resume within those bounds.\n        ImVec2 scroll_dir = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down, 1.0f/10.0f, 10.0f);\n        if (scroll_dir.x != 0.0f && window->ScrollbarX)\n        {\n            SetWindowScrollX(window, ImFloor(window->Scroll.x + scroll_dir.x * scroll_speed));\n            g.NavMoveFromClampedRefRect = true;\n        }\n        if (scroll_dir.y != 0.0f)\n        {\n            SetWindowScrollY(window, ImFloor(window->Scroll.y + scroll_dir.y * scroll_speed));\n            g.NavMoveFromClampedRefRect = true;\n        }\n    }\n\n    // Reset search results\n    g.NavMoveResultLocal.Clear();\n    g.NavMoveResultLocalVisibleSet.Clear();\n    g.NavMoveResultOther.Clear();\n\n    // When we have manually scrolled (without using navigation) and NavId becomes out of bounds, we project its bounding box to the visible area to restart navigation within visible items\n    if (g.NavMoveRequest && g.NavMoveFromClampedRefRect && g.NavLayer == 0)\n    {\n        ImGuiWindow* window = g.NavWindow;\n        ImRect window_rect_rel(window->InnerMainRect.Min - window->Pos - ImVec2(1,1), window->InnerMainRect.Max - window->Pos + ImVec2(1,1));\n        if (!window_rect_rel.Contains(window->NavRectRel[g.NavLayer]))\n        {\n            float pad = window->CalcFontSize() * 0.5f;\n            window_rect_rel.Expand(ImVec2(-ImMin(window_rect_rel.GetWidth(), pad), -ImMin(window_rect_rel.GetHeight(), pad))); // Terrible approximation for the intent of starting navigation from first fully visible item\n            window->NavRectRel[g.NavLayer].ClipWith(window_rect_rel);\n            g.NavId = 0;\n        }\n        g.NavMoveFromClampedRefRect = false;\n    }\n\n    // For scoring we use a single segment on the left side our current item bounding box (not touching the edge to avoid box overlap with zero-spaced items)\n    ImRect nav_rect_rel = (g.NavWindow && !g.NavWindow->NavRectRel[g.NavLayer].IsInverted()) ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0,0,0,0);\n    g.NavScoringRectScreen = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : ImRect(0,0,0,0);\n    g.NavScoringRectScreen.TranslateY(nav_scoring_rect_offset_y);\n    g.NavScoringRectScreen.Min.x = ImMin(g.NavScoringRectScreen.Min.x + 1.0f, g.NavScoringRectScreen.Max.x);\n    g.NavScoringRectScreen.Max.x = g.NavScoringRectScreen.Min.x;\n    IM_ASSERT(!g.NavScoringRectScreen.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allows us to remove extraneous ImFabs() calls in NavScoreItem().\n    //GetForegroundDrawList()->AddRect(g.NavScoringRectScreen.Min, g.NavScoringRectScreen.Max, IM_COL32(255,200,0,255)); // [DEBUG]\n    g.NavScoringCount = 0;\n#if IMGUI_DEBUG_NAV_RECTS\n    if (g.NavWindow)\n    {\n        ImDrawList* draw_list = GetForegroundDrawList(g.NavWindow);\n        if (1) { for (int layer = 0; layer < 2; layer++) draw_list->AddRect(g.NavWindow->Pos + g.NavWindow->NavRectRel[layer].Min, g.NavWindow->Pos + g.NavWindow->NavRectRel[layer].Max, IM_COL32(255,200,0,255)); } // [DEBUG]\n        if (1) { ImU32 col = (!g.NavWindow->Hidden) ? IM_COL32(255,0,255,255) : IM_COL32(255,0,0,255); ImVec2 p = NavCalcPreferredRefPos(); char buf[32]; ImFormatString(buf, 32, \"%d\", g.NavLayer); draw_list->AddCircleFilled(p, 3.0f, col); draw_list->AddText(NULL, 13.0f, p + ImVec2(8,-4), col, buf); }\n    }\n#endif\n}\n\n// Apply result from previous frame navigation directional move request\nstatic void ImGui::NavUpdateMoveResult()\n{\n    ImGuiContext& g = *GImGui;\n    if (g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0)\n    {\n        // In a situation when there is no results but NavId != 0, re-enable the Navigation highlight (because g.NavId is not considered as a possible result)\n        if (g.NavId != 0)\n        {\n            g.NavDisableHighlight = false;\n            g.NavDisableMouseHover = true;\n        }\n        return;\n    }\n\n    // Select which result to use\n    ImGuiNavMoveResult* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : &g.NavMoveResultOther;\n\n    // PageUp/PageDown behavior first jumps to the bottom/top mostly visible item, _otherwise_ use the result from the previous/next page.\n    if (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet)\n        if (g.NavMoveResultLocalVisibleSet.ID != 0 && g.NavMoveResultLocalVisibleSet.ID != g.NavId)\n            result = &g.NavMoveResultLocalVisibleSet;\n\n    // Maybe entering a flattened child from the outside? In this case solve the tie using the regular scoring rules.\n    if (result != &g.NavMoveResultOther && g.NavMoveResultOther.ID != 0 && g.NavMoveResultOther.Window->ParentWindow == g.NavWindow)\n        if ((g.NavMoveResultOther.DistBox < result->DistBox) || (g.NavMoveResultOther.DistBox == result->DistBox && g.NavMoveResultOther.DistCenter < result->DistCenter))\n            result = &g.NavMoveResultOther;\n    IM_ASSERT(g.NavWindow && result->Window);\n\n    // Scroll to keep newly navigated item fully into view.\n    if (g.NavLayer == 0)\n    {\n        ImRect rect_abs = ImRect(result->RectRel.Min + result->Window->Pos, result->RectRel.Max + result->Window->Pos);\n        NavScrollToBringItemIntoView(result->Window, rect_abs);\n\n        // Estimate upcoming scroll so we can offset our result position so mouse position can be applied immediately after in NavUpdate()\n        ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(result->Window, false);\n        ImVec2 delta_scroll = result->Window->Scroll - next_scroll;\n        result->RectRel.Translate(delta_scroll);\n\n        // Also scroll parent window to keep us into view if necessary (we could/should technically recurse back the whole the parent hierarchy).\n        if (result->Window->Flags & ImGuiWindowFlags_ChildWindow)\n            NavScrollToBringItemIntoView(result->Window->ParentWindow, ImRect(rect_abs.Min + delta_scroll, rect_abs.Max + delta_scroll));\n    }\n\n    ClearActiveID();\n    g.NavWindow = result->Window;\n    if (g.NavId != result->ID)\n    {\n        // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId)\n        g.NavJustMovedToId = result->ID;\n        g.NavJustMovedToMultiSelectScopeId = result->SelectScopeId;\n    }\n    SetNavIDWithRectRel(result->ID, g.NavLayer, result->RectRel);\n    g.NavMoveFromClampedRefRect = false;\n}\n\nstatic float ImGui::NavUpdatePageUpPageDown(int allowed_dir_flags)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.NavMoveDir == ImGuiDir_None && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.NavWindowingTarget && g.NavLayer == 0)\n    {\n        ImGuiWindow* window = g.NavWindow;\n        bool page_up_held = IsKeyDown(g.IO.KeyMap[ImGuiKey_PageUp]) && (allowed_dir_flags & (1 << ImGuiDir_Up));\n        bool page_down_held = IsKeyDown(g.IO.KeyMap[ImGuiKey_PageDown]) && (allowed_dir_flags & (1 << ImGuiDir_Down));\n        if (page_up_held != page_down_held) // If either (not both) are pressed\n        {\n            if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll)\n            {\n                // Fallback manual-scroll when window has no navigable item\n                if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageUp], true))\n                    SetWindowScrollY(window, window->Scroll.y - window->InnerMainRect.GetHeight());\n                else if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageDown], true))\n                    SetWindowScrollY(window, window->Scroll.y + window->InnerMainRect.GetHeight());\n            }\n            else\n            {\n                const ImRect& nav_rect_rel = window->NavRectRel[g.NavLayer];\n                const float page_offset_y = ImMax(0.0f, window->InnerMainRect.GetHeight() - window->CalcFontSize() * 1.0f + nav_rect_rel.GetHeight());\n                float nav_scoring_rect_offset_y = 0.0f;\n                if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageUp], true))\n                {\n                    nav_scoring_rect_offset_y = -page_offset_y;\n                    g.NavMoveDir = ImGuiDir_Down; // Because our scoring rect is offset, we intentionally request the opposite direction (so we can always land on the last item)\n                    g.NavMoveClipDir = ImGuiDir_Up;\n                    g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet;\n                }\n                else if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageDown], true))\n                {\n                    nav_scoring_rect_offset_y = +page_offset_y;\n                    g.NavMoveDir = ImGuiDir_Up; // Because our scoring rect is offset, we intentionally request the opposite direction (so we can always land on the last item)\n                    g.NavMoveClipDir = ImGuiDir_Down;\n                    g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet;\n                }\n                return nav_scoring_rect_offset_y;\n            }\n        }\n    }\n    return 0.0f;\n}\n\nstatic int ImGui::FindWindowFocusIndex(ImGuiWindow* window) // FIXME-OPT O(N)\n{\n    ImGuiContext& g = *GImGui;\n    for (int i = g.WindowsFocusOrder.Size-1; i >= 0; i--)\n        if (g.WindowsFocusOrder[i] == window)\n            return i;\n    return -1;\n}\n\nstatic ImGuiWindow* FindWindowNavFocusable(int i_start, int i_stop, int dir) // FIXME-OPT O(N)\n{\n    ImGuiContext& g = *GImGui;\n    for (int i = i_start; i >= 0 && i < g.WindowsFocusOrder.Size && i != i_stop; i += dir)\n        if (ImGui::IsWindowNavFocusable(g.WindowsFocusOrder[i]))\n            return g.WindowsFocusOrder[i];\n    return NULL;\n}\n\nstatic void NavUpdateWindowingHighlightWindow(int focus_change_dir)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.NavWindowingTarget);\n    if (g.NavWindowingTarget->Flags & ImGuiWindowFlags_Modal)\n        return;\n\n    const int i_current = ImGui::FindWindowFocusIndex(g.NavWindowingTarget);\n    ImGuiWindow* window_target = FindWindowNavFocusable(i_current + focus_change_dir, -INT_MAX, focus_change_dir);\n    if (!window_target)\n        window_target = FindWindowNavFocusable((focus_change_dir < 0) ? (g.WindowsFocusOrder.Size - 1) : 0, i_current, focus_change_dir);\n    if (window_target) // Don't reset windowing target if there's a single window in the list\n        g.NavWindowingTarget = g.NavWindowingTargetAnim = window_target;\n    g.NavWindowingToggleLayer = false;\n}\n\n// Windowing management mode\n// Keyboard: CTRL+Tab (change focus/move/resize), Alt (toggle menu layer)\n// Gamepad:  Hold Menu/Square (change focus/move/resize), Tap Menu/Square (toggle menu layer)\nstatic void ImGui::NavUpdateWindowing()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* apply_focus_window = NULL;\n    bool apply_toggle_layer = false;\n\n    ImGuiWindow* modal_window = GetFrontMostPopupModal();\n    if (modal_window != NULL)\n    {\n        g.NavWindowingTarget = NULL;\n        return;\n    }\n\n    // Fade out\n    if (g.NavWindowingTargetAnim && g.NavWindowingTarget == NULL)\n    {\n        g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha - g.IO.DeltaTime * 10.0f, 0.0f);\n        if (g.DimBgRatio <= 0.0f && g.NavWindowingHighlightAlpha <= 0.0f)\n            g.NavWindowingTargetAnim = NULL;\n    }\n\n    // Start CTRL-TAB or Square+L/R window selection\n    bool start_windowing_with_gamepad = !g.NavWindowingTarget && IsNavInputPressed(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed);\n    bool start_windowing_with_keyboard = !g.NavWindowingTarget && g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard);\n    if (start_windowing_with_gamepad || start_windowing_with_keyboard)\n        if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1))\n        {\n            g.NavWindowingTarget = g.NavWindowingTargetAnim = window;\n            g.NavWindowingTimer = g.NavWindowingHighlightAlpha = 0.0f;\n            g.NavWindowingToggleLayer = start_windowing_with_keyboard ? false : true;\n            g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_NavKeyboard : ImGuiInputSource_NavGamepad;\n        }\n\n    // Gamepad update\n    g.NavWindowingTimer += g.IO.DeltaTime;\n    if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_NavGamepad)\n    {\n        // Highlight only appears after a brief time holding the button, so that a fast tap on PadMenu (to toggle NavLayer) doesn't add visual noise\n        g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f));\n\n        // Select window to focus\n        const int focus_change_dir = (int)IsNavInputPressed(ImGuiNavInput_FocusPrev, ImGuiInputReadMode_RepeatSlow) - (int)IsNavInputPressed(ImGuiNavInput_FocusNext, ImGuiInputReadMode_RepeatSlow);\n        if (focus_change_dir != 0)\n        {\n            NavUpdateWindowingHighlightWindow(focus_change_dir);\n            g.NavWindowingHighlightAlpha = 1.0f;\n        }\n\n        // Single press toggles NavLayer, long press with L/R apply actual focus on release (until then the window was merely rendered front-most)\n        if (!IsNavInputDown(ImGuiNavInput_Menu))\n        {\n            g.NavWindowingToggleLayer &= (g.NavWindowingHighlightAlpha < 1.0f); // Once button was held long enough we don't consider it a tap-to-toggle-layer press anymore.\n            if (g.NavWindowingToggleLayer && g.NavWindow)\n                apply_toggle_layer = true;\n            else if (!g.NavWindowingToggleLayer)\n                apply_focus_window = g.NavWindowingTarget;\n            g.NavWindowingTarget = NULL;\n        }\n    }\n\n    // Keyboard: Focus\n    if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_NavKeyboard)\n    {\n        // Visuals only appears after a brief time after pressing TAB the first time, so that a fast CTRL+TAB doesn't add visual noise\n        g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // 1.0f\n        if (IsKeyPressedMap(ImGuiKey_Tab, true))\n            NavUpdateWindowingHighlightWindow(g.IO.KeyShift ? +1 : -1);\n        if (!g.IO.KeyCtrl)\n            apply_focus_window = g.NavWindowingTarget;\n    }\n\n    // Keyboard: Press and Release ALT to toggle menu layer\n    // FIXME: We lack an explicit IO variable for \"is the imgui window focused\", so compare mouse validity to detect the common case of back-end clearing releases all keys on ALT-TAB\n    if (IsNavInputPressed(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Pressed))\n        g.NavWindowingToggleLayer = true;\n    if ((g.ActiveId == 0 || g.ActiveIdAllowOverlap) && g.NavWindowingToggleLayer && IsNavInputPressed(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Released))\n        if (IsMousePosValid(&g.IO.MousePos) == IsMousePosValid(&g.IO.MousePosPrev))\n            apply_toggle_layer = true;\n\n    // Move window\n    if (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoMove))\n    {\n        ImVec2 move_delta;\n        if (g.NavInputSource == ImGuiInputSource_NavKeyboard && !g.IO.KeyShift)\n            move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down);\n        if (g.NavInputSource == ImGuiInputSource_NavGamepad)\n            move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down);\n        if (move_delta.x != 0.0f || move_delta.y != 0.0f)\n        {\n            const float NAV_MOVE_SPEED = 800.0f;\n            const float move_speed = ImFloor(NAV_MOVE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y)); // FIXME: Doesn't code variable framerate very well\n            g.NavWindowingTarget->RootWindow->Pos += move_delta * move_speed;\n            g.NavDisableMouseHover = true;\n            MarkIniSettingsDirty(g.NavWindowingTarget);\n        }\n    }\n\n    // Apply final focus\n    if (apply_focus_window && (g.NavWindow == NULL || apply_focus_window != g.NavWindow->RootWindowDockStop))\n    {\n        ImGuiViewport* previous_viewport = g.NavWindow ? g.NavWindow->Viewport : NULL;\n        ClearActiveID();\n        g.NavDisableHighlight = false;\n        g.NavDisableMouseHover = true;\n        apply_focus_window = NavRestoreLastChildNavWindow(apply_focus_window);\n        ClosePopupsOverWindow(apply_focus_window, false);\n        FocusWindow(apply_focus_window);\n        if (apply_focus_window->NavLastIds[0] == 0)\n            NavInitWindow(apply_focus_window, false);\n\n        // If the window only has a menu layer, select it directly\n        if (apply_focus_window->DC.NavLayerActiveMask == (1 << ImGuiNavLayer_Menu))\n            g.NavLayer = ImGuiNavLayer_Menu;\n\n        // Request OS level focus\n        if (apply_focus_window->Viewport != previous_viewport && g.PlatformIO.Platform_SetWindowFocus)\n            g.PlatformIO.Platform_SetWindowFocus(apply_focus_window->Viewport);\n    }\n    if (apply_focus_window)\n        g.NavWindowingTarget = NULL;\n\n    // Apply menu/layer toggle\n    if (apply_toggle_layer && g.NavWindow)\n    {\n        // Move to parent menu if necessary\n        ImGuiWindow* new_nav_window = g.NavWindow;\n        while (new_nav_window->ParentWindow\n            && (new_nav_window->DC.NavLayerActiveMask & (1 << ImGuiNavLayer_Menu)) == 0\n            && (new_nav_window->Flags & ImGuiWindowFlags_ChildWindow) != 0\n            && (new_nav_window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0)\n            new_nav_window = new_nav_window->ParentWindow;\n        if (new_nav_window != g.NavWindow)\n        {\n            ImGuiWindow* old_nav_window = g.NavWindow;\n            FocusWindow(new_nav_window);\n            new_nav_window->NavLastChildNavWindow = old_nav_window;\n        }\n        g.NavDisableHighlight = false;\n        g.NavDisableMouseHover = true;\n\n        // When entering a regular menu bar with the Alt key, we always reinitialize the navigation ID. It however persist on docking tab tabs.\n        const ImGuiNavLayer new_nav_layer = (g.NavWindow->DC.NavLayerActiveMask & (1 << ImGuiNavLayer_Menu)) ? (ImGuiNavLayer)((int)g.NavLayer ^ 1) : ImGuiNavLayer_Main;\n        const bool preserve_layer_1_nav_id = (new_nav_window->DockNodeAsHost != NULL);\n        if (new_nav_layer == ImGuiNavLayer_Menu && !preserve_layer_1_nav_id)\n            g.NavWindow->NavLastIds[ImGuiNavLayer_Menu] = 0;\n        NavRestoreLayer(new_nav_layer);\n    }\n}\n\n// Window has already passed the IsWindowNavFocusable()\nstatic const char* GetFallbackWindowNameForWindowingList(ImGuiWindow* window)\n{\n    if (window->Flags & ImGuiWindowFlags_Popup)\n        return \"(Popup)\";\n    if ((window->Flags & ImGuiWindowFlags_MenuBar) && strcmp(window->Name, \"##MainMenuBar\") == 0)\n        return \"(Main menu bar)\";\n    if (window->DockNodeAsHost)\n        return \"(Dock node)\";\n    return \"(Untitled)\";\n}\n\n// Overlay displayed when using CTRL+TAB. Called by EndFrame().\nvoid ImGui::NavUpdateWindowingList()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.NavWindowingTarget != NULL);\n\n    if (g.NavWindowingTimer < NAV_WINDOWING_LIST_APPEAR_DELAY)\n        return;\n\n    if (g.NavWindowingList == NULL)\n        g.NavWindowingList = FindWindowByName(\"###NavWindowingList\");\n    ImGuiViewportP* viewport = /*g.NavWindow ? g.NavWindow->Viewport :*/ (ImGuiViewportP*)GetMainViewport();\n    SetNextWindowSizeConstraints(ImVec2(viewport->Size.x * 0.20f, viewport->Size.y * 0.20f), ImVec2(FLT_MAX, FLT_MAX));\n    SetNextWindowPos(viewport->Pos + viewport->Size * 0.5f, ImGuiCond_Always, ImVec2(0.5f, 0.5f));\n    PushStyleVar(ImGuiStyleVar_WindowPadding, g.Style.WindowPadding * 2.0f);\n    Begin(\"###NavWindowingList\", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings);\n    for (int n = g.WindowsFocusOrder.Size - 1; n >= 0; n--)\n    {\n        ImGuiWindow* window = g.WindowsFocusOrder[n];\n        if (!IsWindowNavFocusable(window))\n            continue;\n        const char* label = window->Name;\n        if (label == FindRenderedTextEnd(label))\n            label = GetFallbackWindowNameForWindowingList(window);\n        Selectable(label, g.NavWindowingTarget == window);\n    }\n    End();\n    PopStyleVar();\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] COLUMNS\n// In the current version, Columns are very weak. Needs to be replaced with a more full-featured system.\n//-----------------------------------------------------------------------------\n\nvoid ImGui::NextColumn()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems || window->DC.CurrentColumns == NULL)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    ImGuiColumns* columns = window->DC.CurrentColumns;\n\n    if (columns->Count == 1)\n    {\n        window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);\n        IM_ASSERT(columns->Current == 0);\n        return;\n    }\n\n    PopItemWidth();\n    PopClipRect();\n\n    columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y);\n    if (++columns->Current < columns->Count)\n    {\n        // New column (columns 1+ cancels out IndentX)\n        window->DC.ColumnsOffset.x = GetColumnOffset(columns->Current) - window->DC.Indent.x + g.Style.ItemSpacing.x;\n        window->DrawList->ChannelsSetCurrent(columns->Current);\n    }\n    else\n    {\n        // New row/line\n        window->DC.ColumnsOffset.x = 0.0f;\n        window->DrawList->ChannelsSetCurrent(0);\n        columns->Current = 0;\n        columns->LineMinY = columns->LineMaxY;\n    }\n    window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);\n    window->DC.CursorPos.y = columns->LineMinY;\n    window->DC.CurrentLineSize = ImVec2(0.0f, 0.0f);\n    window->DC.CurrentLineTextBaseOffset = 0.0f;\n\n    PushColumnClipRect();\n    PushItemWidth(GetColumnWidth() * 0.65f);  // FIXME-COLUMNS: Move on columns setup\n}\n\nint ImGui::GetColumnIndex()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.CurrentColumns ? window->DC.CurrentColumns->Current : 0;\n}\n\nint ImGui::GetColumnsCount()\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    return window->DC.CurrentColumns ? window->DC.CurrentColumns->Count : 1;\n}\n\nstatic float OffsetNormToPixels(const ImGuiColumns* columns, float offset_norm)\n{\n    return offset_norm * (columns->MaxX - columns->MinX);\n}\n\nstatic float PixelsToOffsetNorm(const ImGuiColumns* columns, float offset)\n{\n    return offset / (columns->MaxX - columns->MinX);\n}\n\nstatic const float COLUMNS_HIT_RECT_HALF_WIDTH = 4.0f;\n\nstatic float GetDraggedColumnOffset(ImGuiColumns* columns, int column_index)\n{\n    // Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing\n    // window creates a feedback loop because we store normalized positions. So while dragging we enforce absolute positioning.\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    IM_ASSERT(column_index > 0); // We are not supposed to drag column 0.\n    IM_ASSERT(g.ActiveId == columns->ID + ImGuiID(column_index));\n\n    float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window->Pos.x;\n    x = ImMax(x, ImGui::GetColumnOffset(column_index - 1) + g.Style.ColumnsMinSpacing);\n    if ((columns->Flags & ImGuiColumnsFlags_NoPreserveWidths))\n        x = ImMin(x, ImGui::GetColumnOffset(column_index + 1) - g.Style.ColumnsMinSpacing);\n\n    return x;\n}\n\nfloat ImGui::GetColumnOffset(int column_index)\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    ImGuiColumns* columns = window->DC.CurrentColumns;\n    IM_ASSERT(columns != NULL);\n\n    if (column_index < 0)\n        column_index = columns->Current;\n    IM_ASSERT(column_index < columns->Columns.Size);\n\n    const float t = columns->Columns[column_index].OffsetNorm;\n    const float x_offset = ImLerp(columns->MinX, columns->MaxX, t);\n    return x_offset;\n}\n\nstatic float GetColumnWidthEx(ImGuiColumns* columns, int column_index, bool before_resize = false)\n{\n    if (column_index < 0)\n        column_index = columns->Current;\n\n    float offset_norm;\n    if (before_resize)\n        offset_norm = columns->Columns[column_index + 1].OffsetNormBeforeResize - columns->Columns[column_index].OffsetNormBeforeResize;\n    else\n        offset_norm = columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm;\n    return OffsetNormToPixels(columns, offset_norm);\n}\n\nfloat ImGui::GetColumnWidth(int column_index)\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    ImGuiColumns* columns = window->DC.CurrentColumns;\n    IM_ASSERT(columns != NULL);\n\n    if (column_index < 0)\n        column_index = columns->Current;\n    return OffsetNormToPixels(columns, columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm);\n}\n\nvoid ImGui::SetColumnOffset(int column_index, float offset)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    ImGuiColumns* columns = window->DC.CurrentColumns;\n    IM_ASSERT(columns != NULL);\n\n    if (column_index < 0)\n        column_index = columns->Current;\n    IM_ASSERT(column_index < columns->Columns.Size);\n\n    const bool preserve_width = !(columns->Flags & ImGuiColumnsFlags_NoPreserveWidths) && (column_index < columns->Count-1);\n    const float width = preserve_width ? GetColumnWidthEx(columns, column_index, columns->IsBeingResized) : 0.0f;\n\n    if (!(columns->Flags & ImGuiColumnsFlags_NoForceWithinWindow))\n        offset = ImMin(offset, columns->MaxX - g.Style.ColumnsMinSpacing * (columns->Count - column_index));\n    columns->Columns[column_index].OffsetNorm = PixelsToOffsetNorm(columns, offset - columns->MinX);\n\n    if (preserve_width)\n        SetColumnOffset(column_index + 1, offset + ImMax(g.Style.ColumnsMinSpacing, width));\n}\n\nvoid ImGui::SetColumnWidth(int column_index, float width)\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    ImGuiColumns* columns = window->DC.CurrentColumns;\n    IM_ASSERT(columns != NULL);\n\n    if (column_index < 0)\n        column_index = columns->Current;\n    SetColumnOffset(column_index + 1, GetColumnOffset(column_index) + width);\n}\n\nvoid ImGui::PushColumnClipRect(int column_index)\n{\n    ImGuiWindow* window = GetCurrentWindowRead();\n    ImGuiColumns* columns = window->DC.CurrentColumns;\n    if (column_index < 0)\n        column_index = columns->Current;\n\n    ImGuiColumnData* column = &columns->Columns[column_index];\n    PushClipRect(column->ClipRect.Min, column->ClipRect.Max, false);\n}\n\nImGuiColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id)\n{\n    // We have few columns per window so for now we don't need bother much with turning this into a faster lookup.\n    for (int n = 0; n < window->ColumnsStorage.Size; n++)\n        if (window->ColumnsStorage[n].ID == id)\n            return &window->ColumnsStorage[n];\n\n    window->ColumnsStorage.push_back(ImGuiColumns());\n    ImGuiColumns* columns = &window->ColumnsStorage.back();\n    columns->ID = id;\n    return columns;\n}\n\nImGuiID ImGui::GetColumnsID(const char* str_id, int columns_count)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n\n    // Differentiate column ID with an arbitrary prefix for cases where users name their columns set the same as another widget.\n    // In addition, when an identifier isn't explicitly provided we include the number of columns in the hash to make it uniquer.\n    PushID(0x11223347 + (str_id ? 0 : columns_count));\n    ImGuiID id = window->GetID(str_id ? str_id : \"columns\");\n    PopID();\n\n    return id;\n}\n\nvoid ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = GetCurrentWindow();\n\n    IM_ASSERT(columns_count >= 1);\n    IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported\n\n    ImGuiID id = GetColumnsID(str_id, columns_count);\n\n    // Acquire storage for the columns set\n    ImGuiColumns* columns = FindOrCreateColumns(window, id);\n    IM_ASSERT(columns->ID == id);\n    columns->Current = 0;\n    columns->Count = columns_count;\n    columns->Flags = flags;\n    window->DC.CurrentColumns = columns;\n\n    // Set state for first column\n    const float content_region_width = (window->SizeContentsExplicit.x != 0.0f) ? (window->SizeContentsExplicit.x) : (window->InnerClipRect.Max.x - window->Pos.x);\n    columns->MinX = window->DC.Indent.x - g.Style.ItemSpacing.x; // Lock our horizontal range\n    columns->MaxX = ImMax(content_region_width - window->Scroll.x, columns->MinX + 1.0f);\n    columns->BackupCursorPosY = window->DC.CursorPos.y;\n    columns->BackupCursorMaxPosX = window->DC.CursorMaxPos.x;\n    columns->LineMinY = columns->LineMaxY = window->DC.CursorPos.y;\n    window->DC.ColumnsOffset.x = 0.0f;\n    window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);\n\n    // Clear data if columns count changed\n    if (columns->Columns.Size != 0 && columns->Columns.Size != columns_count + 1)\n        columns->Columns.resize(0);\n\n    // Initialize defaults\n    columns->IsFirstFrame = (columns->Columns.Size == 0);\n    if (columns->Columns.Size == 0)\n    {\n        columns->Columns.reserve(columns_count + 1);\n        for (int n = 0; n < columns_count + 1; n++)\n        {\n            ImGuiColumnData column;\n            column.OffsetNorm = n / (float)columns_count;\n            columns->Columns.push_back(column);\n        }\n    }\n\n    for (int n = 0; n < columns_count; n++)\n    {\n        // Compute clipping rectangle\n        ImGuiColumnData* column = &columns->Columns[n];\n        float clip_x1 = ImFloor(0.5f + window->Pos.x + GetColumnOffset(n));\n        float clip_x2 = ImFloor(0.5f + window->Pos.x + GetColumnOffset(n + 1) - 1.0f);\n        column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX);\n        column->ClipRect.ClipWith(window->ClipRect);\n    }\n\n    if (columns->Count > 1)\n    {\n        window->DrawList->ChannelsSplit(columns->Count);\n        PushColumnClipRect();\n    }\n    PushItemWidth(GetColumnWidth() * 0.65f);\n}\n\nvoid ImGui::EndColumns()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = GetCurrentWindow();\n    ImGuiColumns* columns = window->DC.CurrentColumns;\n    IM_ASSERT(columns != NULL);\n\n    PopItemWidth();\n    if (columns->Count > 1)\n    {\n        PopClipRect();\n        window->DrawList->ChannelsMerge();\n    }\n\n    columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y);\n    window->DC.CursorPos.y = columns->LineMaxY;\n    if (!(columns->Flags & ImGuiColumnsFlags_GrowParentContentsSize))\n        window->DC.CursorMaxPos.x = columns->BackupCursorMaxPosX;  // Restore cursor max pos, as columns don't grow parent\n\n    // Draw columns borders and handle resize\n    bool is_being_resized = false;\n    if (!(columns->Flags & ImGuiColumnsFlags_NoBorder) && !window->SkipItems)\n    {\n        // We clip Y boundaries CPU side because very long triangles are mishandled by some GPU drivers.\n        const float y1 = ImMax(columns->BackupCursorPosY, window->ClipRect.Min.y);\n        const float y2 = ImMin(window->DC.CursorPos.y, window->ClipRect.Max.y);\n        int dragging_column = -1;\n        for (int n = 1; n < columns->Count; n++)\n        {\n            ImGuiColumnData* column = &columns->Columns[n];\n            float x = window->Pos.x + GetColumnOffset(n);\n            const ImGuiID column_id = columns->ID + ImGuiID(n);\n            const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH;\n            const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2));\n            KeepAliveID(column_id);\n            if (IsClippedEx(column_hit_rect, column_id, false))\n                continue;\n\n            bool hovered = false, held = false;\n            if (!(columns->Flags & ImGuiColumnsFlags_NoResize))\n            {\n                ButtonBehavior(column_hit_rect, column_id, &hovered, &held);\n                if (hovered || held)\n                    g.MouseCursor = ImGuiMouseCursor_ResizeEW;\n                if (held && !(column->Flags & ImGuiColumnsFlags_NoResize))\n                    dragging_column = n;\n            }\n\n            // Draw column\n            const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : hovered ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator);\n            const float xi = (float)(int)x;\n            window->DrawList->AddLine(ImVec2(xi, y1 + 1.0f), ImVec2(xi, y2), col);\n        }\n\n        // Apply dragging after drawing the column lines, so our rendered lines are in sync with how items were displayed during the frame.\n        if (dragging_column != -1)\n        {\n            if (!columns->IsBeingResized)\n                for (int n = 0; n < columns->Count + 1; n++)\n                    columns->Columns[n].OffsetNormBeforeResize = columns->Columns[n].OffsetNorm;\n            columns->IsBeingResized = is_being_resized = true;\n            float x = GetDraggedColumnOffset(columns, dragging_column);\n            SetColumnOffset(dragging_column, x);\n        }\n    }\n    columns->IsBeingResized = is_being_resized;\n\n    window->DC.CurrentColumns = NULL;\n    window->DC.ColumnsOffset.x = 0.0f;\n    window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x);\n}\n\n// [2018-03: This is currently the only public API, while we are working on making BeginColumns/EndColumns user-facing]\nvoid ImGui::Columns(int columns_count, const char* id, bool border)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    IM_ASSERT(columns_count >= 1);\n\n    ImGuiColumnsFlags flags = (border ? 0 : ImGuiColumnsFlags_NoBorder);\n    //flags |= ImGuiColumnsFlags_NoPreserveWidths; // NB: Legacy behavior\n    ImGuiColumns* columns = window->DC.CurrentColumns;\n    if (columns != NULL && columns->Count == columns_count && columns->Flags == flags)\n        return;\n\n    if (columns != NULL)\n        EndColumns();\n\n    if (columns_count != 1)\n        BeginColumns(id, columns_count, flags);\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] DRAG AND DROP\n//-----------------------------------------------------------------------------\n\nvoid ImGui::ClearDragDrop()\n{\n    ImGuiContext& g = *GImGui;\n    g.DragDropActive = false;\n    g.DragDropPayload.Clear();\n    g.DragDropAcceptFlags = ImGuiDragDropFlags_None;\n    g.DragDropAcceptIdCurr = g.DragDropAcceptIdPrev = 0;\n    g.DragDropAcceptIdCurrRectSurface = FLT_MAX;\n    g.DragDropAcceptFrameCount = -1;\n\n    g.DragDropPayloadBufHeap.clear();\n    memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal));\n}\n\n// Call when current ID is active.\n// When this returns true you need to: a) call SetDragDropPayload() exactly once, b) you may render the payload visual/description, c) call EndDragDropSource()\nbool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    bool source_drag_active = false;\n    ImGuiID source_id = 0;\n    ImGuiID source_parent_id = 0;\n    int mouse_button = 0;\n    if (!(flags & ImGuiDragDropFlags_SourceExtern))\n    {\n        source_id = window->DC.LastItemId;\n        if (source_id != 0 && g.ActiveId != source_id) // Early out for most common case\n            return false;\n        if (g.IO.MouseDown[mouse_button] == false)\n            return false;\n\n        if (source_id == 0)\n        {\n            // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to:\n            // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag, C) Swallow your programmer pride.\n            if (!(flags & ImGuiDragDropFlags_SourceAllowNullID))\n            {\n                IM_ASSERT(0);\n                return false;\n            }\n\n            // Magic fallback (=somehow reprehensible) to handle items with no assigned ID, e.g. Text(), Image()\n            // We build a throwaway ID based on current ID stack + relative AABB of items in window.\n            // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING OF THE WIDGET, so if your widget moves your dragging operation will be canceled.\n            // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive.\n            bool is_hovered = (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect) != 0;\n            if (!is_hovered && (g.ActiveId == 0 || g.ActiveIdWindow != window))\n                return false;\n            source_id = window->DC.LastItemId = window->GetIDFromRectangle(window->DC.LastItemRect);\n            if (is_hovered)\n                SetHoveredID(source_id);\n            if (is_hovered && g.IO.MouseClicked[mouse_button])\n            {\n                SetActiveID(source_id, window);\n                FocusWindow(window);\n            }\n            if (g.ActiveId == source_id) // Allow the underlying widget to display/return hovered during the mouse release frame, else we would get a flicker.\n                g.ActiveIdAllowOverlap = is_hovered;\n        }\n        else\n        {\n            g.ActiveIdAllowOverlap = false;\n        }\n        if (g.ActiveId != source_id)\n            return false;\n        source_parent_id = window->IDStack.back();\n        source_drag_active = IsMouseDragging(mouse_button);\n    }\n    else\n    {\n        window = NULL;\n        source_id = ImHashStr(\"#SourceExtern\");\n        source_drag_active = true;\n    }\n\n    if (source_drag_active)\n    {\n        if (!g.DragDropActive)\n        {\n            IM_ASSERT(source_id != 0);\n            ClearDragDrop();\n            ImGuiPayload& payload = g.DragDropPayload;\n            payload.SourceId = source_id;\n            payload.SourceParentId = source_parent_id;\n            g.DragDropActive = true;\n            g.DragDropSourceFlags = flags;\n            g.DragDropMouseButton = mouse_button;\n        }\n        g.DragDropSourceFrameCount = g.FrameCount;\n        g.DragDropWithinSourceOrTarget = true;\n\n        if (!(flags & ImGuiDragDropFlags_SourceNoPreviewTooltip))\n        {\n            // Target can request the Source to not display its tooltip (we use a dedicated flag to make this request explicit)\n            // We unfortunately can't just modify the source flags and skip the call to BeginTooltip, as caller may be emitting contents.\n            BeginTooltip();\n            if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip))\n            {\n                ImGuiWindow* tooltip_window = g.CurrentWindow;\n                tooltip_window->SkipItems = true;\n                tooltip_window->HiddenFramesCanSkipItems = 1;\n            }\n        }\n\n        if (!(flags & ImGuiDragDropFlags_SourceNoDisableHover) && !(flags & ImGuiDragDropFlags_SourceExtern))\n            window->DC.LastItemStatusFlags &= ~ImGuiItemStatusFlags_HoveredRect;\n\n        return true;\n    }\n    return false;\n}\n\nvoid ImGui::EndDragDropSource()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.DragDropActive);\n    IM_ASSERT(g.DragDropWithinSourceOrTarget && \"Not after a BeginDragDropSource()?\");\n\n    if (!(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoPreviewTooltip))\n        EndTooltip();\n\n    // Discard the drag if have not called SetDragDropPayload()\n    if (g.DragDropPayload.DataFrameCount == -1)\n        ClearDragDrop();\n    g.DragDropWithinSourceOrTarget = false;\n}\n\n// Use 'cond' to choose to submit payload on drag start or every frame\nbool ImGui::SetDragDropPayload(const char* type, const void* data, size_t data_size, ImGuiCond cond)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiPayload& payload = g.DragDropPayload;\n    if (cond == 0)\n        cond = ImGuiCond_Always;\n\n    IM_ASSERT(type != NULL);\n    IM_ASSERT(strlen(type) < IM_ARRAYSIZE(payload.DataType) && \"Payload type can be at most 32 characters long\");\n    IM_ASSERT((data != NULL && data_size > 0) || (data == NULL && data_size == 0));\n    IM_ASSERT(cond == ImGuiCond_Always || cond == ImGuiCond_Once);\n    IM_ASSERT(payload.SourceId != 0);                               // Not called between BeginDragDropSource() and EndDragDropSource()\n\n    if (cond == ImGuiCond_Always || payload.DataFrameCount == -1)\n    {\n        // Copy payload\n        ImStrncpy(payload.DataType, type, IM_ARRAYSIZE(payload.DataType));\n        g.DragDropPayloadBufHeap.resize(0);\n        if (data_size > sizeof(g.DragDropPayloadBufLocal))\n        {\n            // Store in heap\n            g.DragDropPayloadBufHeap.resize((int)data_size);\n            payload.Data = g.DragDropPayloadBufHeap.Data;\n            memcpy(payload.Data, data, data_size);\n        }\n        else if (data_size > 0)\n        {\n            // Store locally\n            memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal));\n            payload.Data = g.DragDropPayloadBufLocal;\n            memcpy(payload.Data, data, data_size);\n        }\n        else\n        {\n            payload.Data = NULL;\n        }\n        payload.DataSize = (int)data_size;\n    }\n    payload.DataFrameCount = g.FrameCount;\n\n    return (g.DragDropAcceptFrameCount == g.FrameCount) || (g.DragDropAcceptFrameCount == g.FrameCount - 1);\n}\n\nbool ImGui::BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    if (!g.DragDropActive)\n        return false;\n\n    ImGuiWindow* window = g.CurrentWindow;\n    if (g.HoveredWindowUnderMovingWindow == NULL || window->RootWindow != g.HoveredWindowUnderMovingWindow->RootWindow)\n        return false;\n    IM_ASSERT(id != 0);\n    if (!IsMouseHoveringRect(bb.Min, bb.Max) || (id == g.DragDropPayload.SourceId))\n        return false;\n    if (window->SkipItems)\n        return false;\n\n    IM_ASSERT(g.DragDropWithinSourceOrTarget == false);\n    g.DragDropTargetRect = bb;\n    g.DragDropTargetId = id;\n    g.DragDropWithinSourceOrTarget = true;\n    return true;\n}\n\n// We don't use BeginDragDropTargetCustom() and duplicate its code because:\n// 1) we use LastItemRectHoveredRect which handles items that pushes a temporarily clip rectangle in their code. Calling BeginDragDropTargetCustom(LastItemRect) would not handle them.\n// 2) and it's faster. as this code may be very frequently called, we want to early out as fast as we can.\n// Also note how the HoveredWindow test is positioned differently in both functions (in both functions we optimize for the cheapest early out case)\nbool ImGui::BeginDragDropTarget()\n{\n    ImGuiContext& g = *GImGui;\n    if (!g.DragDropActive)\n        return false;\n\n    ImGuiWindow* window = g.CurrentWindow;\n    if (!(window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect))\n        return false;\n    if (g.HoveredWindowUnderMovingWindow == NULL || window->RootWindow != g.HoveredWindowUnderMovingWindow->RootWindow)\n        return false;\n\n    const ImRect& display_rect = (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect) ? window->DC.LastItemDisplayRect : window->DC.LastItemRect;\n    ImGuiID id = window->DC.LastItemId;\n    if (id == 0)\n        id = window->GetIDFromRectangle(display_rect);\n    if (g.DragDropPayload.SourceId == id)\n        return false;\n\n    IM_ASSERT(g.DragDropWithinSourceOrTarget == false);\n    g.DragDropTargetRect = display_rect;\n    g.DragDropTargetId = id;\n    g.DragDropWithinSourceOrTarget = true;\n    return true;\n}\n\nbool ImGui::IsDragDropPayloadBeingAccepted()\n{\n    ImGuiContext& g = *GImGui;\n    return g.DragDropActive && g.DragDropAcceptIdPrev != 0;\n}\n\nconst ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDropFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    ImGuiPayload& payload = g.DragDropPayload;\n    IM_ASSERT(g.DragDropActive);                        // Not called between BeginDragDropTarget() and EndDragDropTarget() ?\n    IM_ASSERT(payload.DataFrameCount != -1);            // Forgot to call EndDragDropTarget() ?\n    if (type != NULL && !payload.IsDataType(type))\n        return NULL;\n\n    // Accept smallest drag target bounding box, this allows us to nest drag targets conveniently without ordering constraints.\n    // NB: We currently accept NULL id as target. However, overlapping targets requires a unique ID to function!\n    const bool was_accepted_previously = (g.DragDropAcceptIdPrev == g.DragDropTargetId);\n    ImRect r = g.DragDropTargetRect;\n    float r_surface = r.GetWidth() * r.GetHeight();\n    if (r_surface < g.DragDropAcceptIdCurrRectSurface)\n    {\n        g.DragDropAcceptFlags = flags;\n        g.DragDropAcceptIdCurr = g.DragDropTargetId;\n        g.DragDropAcceptIdCurrRectSurface = r_surface;\n    }\n\n    // Render default drop visuals\n    payload.Preview = was_accepted_previously;\n    flags |= (g.DragDropSourceFlags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect); // Source can also inhibit the preview (useful for external sources that lives for 1 frame)\n    if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && payload.Preview)\n    {\n        // FIXME-DRAG: Settle on a proper default visuals for drop target.\n        r.Expand(3.5f);\n        bool push_clip_rect = !window->ClipRect.Contains(r);\n        if (push_clip_rect) window->DrawList->PushClipRect(r.Min-ImVec2(1,1), r.Max+ImVec2(1,1));\n        window->DrawList->AddRect(r.Min, r.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, ~0, 2.0f);\n        if (push_clip_rect) window->DrawList->PopClipRect();\n    }\n\n    g.DragDropAcceptFrameCount = g.FrameCount;\n    payload.Delivery = was_accepted_previously && !IsMouseDown(g.DragDropMouseButton); // For extern drag sources affecting os window focus, it's easier to just test !IsMouseDown() instead of IsMouseReleased()\n    if (!payload.Delivery && !(flags & ImGuiDragDropFlags_AcceptBeforeDelivery))\n        return NULL;\n\n    return &payload;\n}\n\nconst ImGuiPayload* ImGui::GetDragDropPayload()\n{\n    ImGuiContext& g = *GImGui;\n    return g.DragDropActive ? &g.DragDropPayload : NULL;\n}\n\n// We don't really use/need this now, but added it for the sake of consistency and because we might need it later.\nvoid ImGui::EndDragDropTarget()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.DragDropActive);\n    IM_ASSERT(g.DragDropWithinSourceOrTarget);\n    g.DragDropWithinSourceOrTarget = false;\n}\n\n\n//-----------------------------------------------------------------------------\n// [SECTION] LOGGING/CAPTURING\n//-----------------------------------------------------------------------------\n// All text output from the interface can be captured into tty/file/clipboard.\n// By default, tree nodes are automatically opened during logging.\n//-----------------------------------------------------------------------------\n\n// Pass text data straight to log (without being displayed)\nvoid ImGui::LogText(const char* fmt, ...)\n{\n    ImGuiContext& g = *GImGui;\n    if (!g.LogEnabled)\n        return;\n\n    va_list args;\n    va_start(args, fmt);\n    if (g.LogFile)\n        vfprintf(g.LogFile, fmt, args);\n    else\n        g.LogBuffer.appendfv(fmt, args);\n    va_end(args);\n}\n\n// Internal version that takes a position to decide on newline placement and pad items according to their depth.\n// We split text into individual lines to add current tree level padding\nvoid ImGui::LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    if (!text_end)\n        text_end = FindRenderedTextEnd(text, text_end);\n\n    const bool log_new_line = ref_pos && (ref_pos->y > g.LogLinePosY + 1);\n    if (ref_pos)\n        g.LogLinePosY = ref_pos->y;\n    if (log_new_line)\n        g.LogLineFirstItem = true;\n\n    const char* text_remaining = text;\n    if (g.LogDepthRef > window->DC.TreeDepth)  // Re-adjust padding if we have popped out of our starting depth\n        g.LogDepthRef = window->DC.TreeDepth;\n    const int tree_depth = (window->DC.TreeDepth - g.LogDepthRef);\n    for (;;)\n    {\n        // Split the string. Each new line (after a '\\n') is followed by spacing corresponding to the current depth of our log entry.\n        // We don't add a trailing \\n to allow a subsequent item on the same line to be captured.\n        const char* line_start = text_remaining;\n        const char* line_end = ImStreolRange(line_start, text_end);\n        const bool is_first_line = (line_start == text);\n        const bool is_last_line = (line_end == text_end);\n        if (!is_last_line || (line_start != line_end))\n        {\n            const int char_count = (int)(line_end - line_start);\n            if (log_new_line || !is_first_line)\n                LogText(IM_NEWLINE \"%*s%.*s\", tree_depth * 4, \"\", char_count, line_start);\n            else if (g.LogLineFirstItem)\n                LogText(\"%*s%.*s\", tree_depth * 4, \"\", char_count, line_start);\n            else\n                LogText(\" %.*s\", char_count, line_start);\n            g.LogLineFirstItem = false;\n        }\n        else if (log_new_line)\n        {\n            // An empty \"\" string at a different Y position should output a carriage return.\n            LogText(IM_NEWLINE);\n            break;\n        }\n\n        if (is_last_line)\n            break;\n        text_remaining = line_end + 1;\n    }\n}\n\n// Start logging/capturing text output\nvoid ImGui::LogBegin(ImGuiLogType type, int auto_open_depth)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    IM_ASSERT(g.LogEnabled == false);\n    IM_ASSERT(g.LogFile == NULL);\n    IM_ASSERT(g.LogBuffer.empty());\n    g.LogEnabled = true;\n    g.LogType = type;\n    g.LogDepthRef = window->DC.TreeDepth;\n    g.LogDepthToExpand = ((auto_open_depth >= 0) ? auto_open_depth : g.LogDepthToExpandDefault);\n    g.LogLinePosY = FLT_MAX;\n    g.LogLineFirstItem = true;\n}\n\nvoid ImGui::LogToTTY(int auto_open_depth)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.LogEnabled)\n        return;\n    LogBegin(ImGuiLogType_TTY, auto_open_depth);\n    g.LogFile = stdout;\n}\n\n// Start logging/capturing text output to given file\nvoid ImGui::LogToFile(int auto_open_depth, const char* filename)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.LogEnabled)\n        return;\n\n    // FIXME: We could probably open the file in text mode \"at\", however note that clipboard/buffer logging will still\n    // be subject to outputting OS-incompatible carriage return if within strings the user doesn't use IM_NEWLINE.\n    // By opening the file in binary mode \"ab\" we have consistent output everywhere.\n    if (!filename)\n        filename = g.IO.LogFilename;\n    if (!filename || !filename[0])\n        return;\n    FILE* f = ImFileOpen(filename, \"ab\");\n    if (f == NULL)\n    {\n        IM_ASSERT(0);\n        return;\n    }\n\n    LogBegin(ImGuiLogType_File, auto_open_depth);\n    g.LogFile = f;\n}\n\n// Start logging/capturing text output to clipboard\nvoid ImGui::LogToClipboard(int auto_open_depth)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.LogEnabled)\n        return;\n    LogBegin(ImGuiLogType_Clipboard, auto_open_depth);\n}\n\nvoid ImGui::LogToBuffer(int auto_open_depth)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.LogEnabled)\n        return;\n    LogBegin(ImGuiLogType_Buffer, auto_open_depth);\n}\n\nvoid ImGui::LogFinish()\n{\n    ImGuiContext& g = *GImGui;\n    if (!g.LogEnabled)\n        return;\n\n    LogText(IM_NEWLINE);\n    switch (g.LogType)\n    {\n    case ImGuiLogType_TTY:\n        fflush(g.LogFile);\n        break;\n    case ImGuiLogType_File:\n        fclose(g.LogFile);\n        break;\n    case ImGuiLogType_Buffer:\n        break;\n    case ImGuiLogType_Clipboard:\n        if (!g.LogBuffer.empty())\n            SetClipboardText(g.LogBuffer.begin());\n        break;\n    case ImGuiLogType_None:\n        IM_ASSERT(0);\n        break;\n    }\n\n    g.LogEnabled = false;\n    g.LogType = ImGuiLogType_None;\n    g.LogFile = NULL;\n    g.LogBuffer.clear();\n}\n\n// Helper to display logging buttons\n// FIXME-OBSOLETE: We should probably obsolete this and let the user have their own helper (this is one of the oldest function alive!)\nvoid ImGui::LogButtons()\n{\n    ImGuiContext& g = *GImGui;\n\n    PushID(\"LogButtons\");\n    const bool log_to_tty = Button(\"Log To TTY\"); SameLine();\n    const bool log_to_file = Button(\"Log To File\"); SameLine();\n    const bool log_to_clipboard = Button(\"Log To Clipboard\"); SameLine();\n    PushAllowKeyboardFocus(false);\n    SetNextItemWidth(80.0f);\n    SliderInt(\"Default Depth\", &g.LogDepthToExpandDefault, 0, 9, NULL);\n    PopAllowKeyboardFocus();\n    PopID();\n\n    // Start logging at the end of the function so that the buttons don't appear in the log\n    if (log_to_tty)\n        LogToTTY();\n    if (log_to_file)\n        LogToFile();\n    if (log_to_clipboard)\n        LogToClipboard();\n}\n\n\n//-----------------------------------------------------------------------------\n// [SECTION] SETTINGS\n//-----------------------------------------------------------------------------\n\nvoid ImGui::MarkIniSettingsDirty()\n{\n    ImGuiContext& g = *GImGui;\n    if (g.SettingsDirtyTimer <= 0.0f)\n        g.SettingsDirtyTimer = g.IO.IniSavingRate;\n}\n\nvoid ImGui::MarkIniSettingsDirty(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    if (!(window->Flags & ImGuiWindowFlags_NoSavedSettings))\n        if (g.SettingsDirtyTimer <= 0.0f)\n            g.SettingsDirtyTimer = g.IO.IniSavingRate;\n}\n\nImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name)\n{\n    ImGuiContext& g = *GImGui;\n    g.SettingsWindows.push_back(ImGuiWindowSettings());\n    ImGuiWindowSettings* settings = &g.SettingsWindows.back();\n    settings->Name = ImStrdup(name);\n    settings->ID = ImHashStr(name);\n    return settings;\n}\n\nImGuiWindowSettings* ImGui::FindWindowSettings(ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    for (int i = 0; i != g.SettingsWindows.Size; i++)\n        if (g.SettingsWindows[i].ID == id)\n            return &g.SettingsWindows[i];\n    return NULL;\n}\n\nImGuiWindowSettings* ImGui::FindOrCreateWindowSettings(const char* name)\n{\n    if (ImGuiWindowSettings* settings = FindWindowSettings(ImHashStr(name)))\n        return settings;\n    return CreateNewWindowSettings(name);\n}\n\nvoid ImGui::LoadIniSettingsFromDisk(const char* ini_filename)\n{\n    size_t file_data_size = 0;\n    char* file_data = (char*)ImFileLoadToMemory(ini_filename, \"rb\", &file_data_size);\n    if (!file_data)\n        return;\n    LoadIniSettingsFromMemory(file_data, (size_t)file_data_size);\n    IM_FREE(file_data);\n}\n\nImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name)\n{\n    ImGuiContext& g = *GImGui;\n    const ImGuiID type_hash = ImHashStr(type_name);\n    for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++)\n        if (g.SettingsHandlers[handler_n].TypeHash == type_hash)\n            return &g.SettingsHandlers[handler_n];\n    return NULL;\n}\n\n// Zero-tolerance, no error reporting, cheap .ini parsing\nvoid ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.Initialized);\n    IM_ASSERT(g.SettingsLoaded == false && g.FrameCount == 0);\n\n    // For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter).\n    // For our convenience and to make the code simpler, we'll also write zero-terminators within the buffer. So let's create a writable copy..\n    if (ini_size == 0)\n        ini_size = strlen(ini_data);\n    char* buf = (char*)IM_ALLOC(ini_size + 1);\n    char* buf_end = buf + ini_size;\n    memcpy(buf, ini_data, ini_size);\n    buf[ini_size] = 0;\n\n    void* entry_data = NULL;\n    ImGuiSettingsHandler* entry_handler = NULL;\n\n    char* line_end = NULL;\n    for (char* line = buf; line < buf_end; line = line_end + 1)\n    {\n        // Skip new lines markers, then find end of the line\n        while (*line == '\\n' || *line == '\\r')\n            line++;\n        line_end = line;\n        while (line_end < buf_end && *line_end != '\\n' && *line_end != '\\r')\n            line_end++;\n        line_end[0] = 0;\n        if (line[0] == ';')\n            continue;\n        if (line[0] == '[' && line_end > line && line_end[-1] == ']')\n        {\n            // Parse \"[Type][Name]\". Note that 'Name' can itself contains [] characters, which is acceptable with the current format and parsing code.\n            line_end[-1] = 0;\n            const char* name_end = line_end - 1;\n            const char* type_start = line + 1;\n            char* type_end = (char*)(intptr_t)ImStrchrRange(type_start, name_end, ']');\n            const char* name_start = type_end ? ImStrchrRange(type_end + 1, name_end, '[') : NULL;\n            if (!type_end || !name_start)\n            {\n                name_start = type_start; // Import legacy entries that have no type\n                type_start = \"Window\";\n            }\n            else\n            {\n                *type_end = 0; // Overwrite first ']'\n                name_start++;  // Skip second '['\n            }\n            entry_handler = FindSettingsHandler(type_start);\n            entry_data = entry_handler ? entry_handler->ReadOpenFn(&g, entry_handler, name_start) : NULL;\n        }\n        else if (entry_handler != NULL && entry_data != NULL)\n        {\n            // Let type handler parse the line\n            entry_handler->ReadLineFn(&g, entry_handler, entry_data, line);\n        }\n    }\n    IM_FREE(buf);\n    g.SettingsLoaded = true;\n    DockContextOnLoadSettings(&g);\n}\n\nvoid ImGui::SaveIniSettingsToDisk(const char* ini_filename)\n{\n    ImGuiContext& g = *GImGui;\n    g.SettingsDirtyTimer = 0.0f;\n    if (!ini_filename)\n        return;\n\n    size_t ini_data_size = 0;\n    const char* ini_data = SaveIniSettingsToMemory(&ini_data_size);\n    FILE* f = ImFileOpen(ini_filename, \"wt\");\n    if (!f)\n        return;\n    fwrite(ini_data, sizeof(char), ini_data_size, f);\n    fclose(f);\n}\n\n// Call registered handlers (e.g. SettingsHandlerWindow_WriteAll() + custom handlers) to write their stuff into a text buffer\nconst char* ImGui::SaveIniSettingsToMemory(size_t* out_size)\n{\n    ImGuiContext& g = *GImGui;\n    g.SettingsDirtyTimer = 0.0f;\n    g.SettingsIniData.Buf.resize(0);\n    g.SettingsIniData.Buf.push_back(0);\n    for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++)\n    {\n        ImGuiSettingsHandler* handler = &g.SettingsHandlers[handler_n];\n        handler->WriteAllFn(&g, handler, &g.SettingsIniData);\n    }\n    if (out_size)\n        *out_size = (size_t)g.SettingsIniData.size();\n    return g.SettingsIniData.c_str();\n}\n\nstatic void* SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name)\n{\n    ImGuiWindowSettings* settings = ImGui::FindWindowSettings(ImHashStr(name));\n    if (!settings)\n        settings = ImGui::CreateNewWindowSettings(name);\n    return (void*)settings;\n}\n\nstatic void SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line)\n{\n    ImGuiWindowSettings* settings = (ImGuiWindowSettings*)entry;\n    float x, y;\n    int i;\n    ImU32 u1;\n    if (sscanf(line, \"Pos=%f,%f\", &x, &y) == 2)                 { settings->Pos = ImVec2(x, y); }\n    else if (sscanf(line, \"Size=%f,%f\", &x, &y) == 2)           { settings->Size = ImMax(ImVec2(x, y), GImGui->Style.WindowMinSize); }\n    else if (sscanf(line, \"ViewportId=0x%08X\", &u1) == 1)       { settings->ViewportId = u1; }\n    else if (sscanf(line, \"ViewportPos=%f,%f\", &x, &y) == 2)    { settings->ViewportPos = ImVec2(x, y); }\n    else if (sscanf(line, \"Collapsed=%d\", &i) == 1)             { settings->Collapsed = (i != 0); }\n    else if (sscanf(line, \"DockId=0x%X,%d\", &u1, &i) == 2)      { settings->DockId = u1; settings->DockOrder = (short)i; }\n    else if (sscanf(line, \"DockId=0x%X\", &u1) == 1)             { settings->DockId = u1; settings->DockOrder = -1; }\n    else if (sscanf(line, \"ClassId=0x%X\", &u1) == 1)            { settings->ClassId = u1; }\n}\n\nstatic void SettingsHandlerWindow_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf)\n{\n    // Gather data from windows that were active during this session\n    // (if a window wasn't opened in this session we preserve its settings)\n    ImGuiContext& g = *imgui_ctx;\n    for (int i = 0; i != g.Windows.Size; i++)\n    {\n        ImGuiWindow* window = g.Windows[i];\n        if (window->Flags & ImGuiWindowFlags_NoSavedSettings)\n            continue;\n\n        ImGuiWindowSettings* settings = (window->SettingsIdx != -1) ? &g.SettingsWindows[window->SettingsIdx] : ImGui::FindWindowSettings(window->ID);\n        if (!settings)\n        {\n            settings = ImGui::CreateNewWindowSettings(window->Name);\n            window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings);\n        }\n        IM_ASSERT(settings->ID == window->ID);\n        settings->Pos = window->Pos - window->ViewportPos;\n        settings->Size = window->SizeFull;\n        settings->ViewportId = window->ViewportId;\n        settings->ViewportPos = window->ViewportPos;\n        IM_ASSERT(window->DockNode == NULL || window->DockNode->ID == window->DockId);\n        settings->DockId = window->DockId;\n        settings->ClassId = window->WindowClass.ClassId;\n        settings->DockOrder = window->DockOrder;\n        settings->Collapsed = window->Collapsed;\n    }\n\n    // Write to text buffer\n    buf->reserve(buf->size() + g.SettingsWindows.Size * 96); // ballpark reserve\n    for (int i = 0; i != g.SettingsWindows.Size; i++)\n    {\n        const ImGuiWindowSettings* settings = &g.SettingsWindows[i];\n        const char* name = settings->Name;\n        if (const char* p = strstr(name, \"###\"))  // Skip to the \"###\" marker if any. We don't skip past to match the behavior of GetID()\n            name = p;\n        buf->appendf(\"[%s][%s]\\n\", handler->TypeName, name);\n        if (settings->ViewportId != 0 && settings->ViewportId != ImGui::IMGUI_VIEWPORT_DEFAULT_ID)\n        {\n            buf->appendf(\"ViewportPos=%d,%d\\n\", (int)settings->ViewportPos.x, (int)settings->ViewportPos.y);\n            buf->appendf(\"ViewportId=0x%08X\\n\", settings->ViewportId);\n        }\n        if (settings->Pos.x != 0.0f || settings->Pos.y != 0.0f || settings->ViewportId == ImGui::IMGUI_VIEWPORT_DEFAULT_ID)\n            buf->appendf(\"Pos=%d,%d\\n\", (int)settings->Pos.x, (int)settings->Pos.y);\n        if (settings->Size.x != 0.0f || settings->Size.y != 0.0f)\n            buf->appendf(\"Size=%d,%d\\n\", (int)settings->Size.x, (int)settings->Size.y);\n        buf->appendf(\"Collapsed=%d\\n\", settings->Collapsed);\n        if (settings->DockId != 0)\n        {\n            // Write DockId as 4 digits if possible. Automatic DockId are small numbers, but full explicit DockSpace() are full ImGuiID range.\n            if (settings->DockOrder == -1)\n                buf->appendf(\"DockId=0x%08X\\n\", settings->DockId);\n            else\n                buf->appendf(\"DockId=0x%08X,%d\\n\", settings->DockId, settings->DockOrder);\n            if (settings->ClassId != 0)\n                buf->appendf(\"ClassId=0x%08X\\n\", settings->ClassId);\n        }\n        buf->appendf(\"\\n\");\n    }\n}\n\n\n//-----------------------------------------------------------------------------\n// [SECTION] VIEWPORTS, PLATFORM WINDOWS\n//-----------------------------------------------------------------------------\n\nImGuiViewport* ImGui::GetMainViewport()\n{\n    ImGuiContext& g = *GImGui;\n    return g.Viewports[0];\n}\n\nImGuiViewport* ImGui::FindViewportByID(ImGuiID id)\n{\n    ImGuiContext& g = *GImGui;\n    for (int n = 0; n < g.Viewports.Size; n++)\n        if (g.Viewports[n]->ID == id)\n            return g.Viewports[n];\n    return NULL;\n}\n\nImGuiViewport* ImGui::FindViewportByPlatformHandle(void* platform_handle)\n{\n    ImGuiContext& g = *GImGui;\n    for (int i = 0; i != g.Viewports.Size; i++)\n        if (g.Viewports[i]->PlatformHandle == platform_handle)\n            return g.Viewports[i];\n    return NULL;\n}\n\nvoid ImGui::SetCurrentViewport(ImGuiWindow* current_window, ImGuiViewportP* viewport)\n{\n    ImGuiContext& g = *GImGui;\n    (void)current_window;\n\n    if (viewport)\n        viewport->LastFrameActive = g.FrameCount;\n    if (g.CurrentViewport == viewport)\n        return;\n    g.CurrentViewport = viewport;\n    //IMGUI_DEBUG_LOG(\"SetCurrentViewport changed '%s' 0x%08X\\n\", current_window ? current_window->Name : NULL, viewport ? viewport->ID : 0);\n\n    // Notify platform layer of viewport changes\n    // FIXME-DPI: This is only currently used for experimenting with handling of multiple DPI\n    if (g.CurrentViewport && g.PlatformIO.Platform_OnChangedViewport)\n        g.PlatformIO.Platform_OnChangedViewport(g.CurrentViewport);\n}\n\nstatic void SetWindowViewport(ImGuiWindow* window, ImGuiViewportP* viewport)\n{\n    window->Viewport = viewport;\n    window->ViewportId = viewport->ID;\n    window->ViewportOwned = (viewport->Window == window);\n}\n\nstatic bool ImGui::GetWindowAlwaysWantOwnViewport(ImGuiWindow* window)\n{\n    // Tooltips and menus are not automatically forced into their own viewport when the NoMerge flag is set, however the multiplication of viewports makes them more likely to protude and create their own.\n    ImGuiContext& g = *GImGui;\n    if (g.IO.ConfigViewportsNoAutoMerge && (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))\n        if (!window->DockIsActive)\n            if ((window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) == 0)\n                return true;\n    return false;\n}\n\nstatic bool ImGui::UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImGuiViewportP* viewport)\n{\n    ImGuiContext& g = *GImGui;\n    if (!(viewport->Flags & (ImGuiViewportFlags_CanHostOtherWindows | ImGuiViewportFlags_Minimized)) || window->Viewport == viewport)\n        return false;\n    if (!viewport->GetRect().Contains(window->Rect()))\n        return false;\n    if (GetWindowAlwaysWantOwnViewport(window))\n        return false;\n\n    for (int n = 0; n < g.Windows.Size; n++)\n    {\n        ImGuiWindow* window_behind = g.Windows[n];\n        if (window_behind == window)\n            break;\n        if (window_behind->WasActive && window_behind->ViewportOwned && !(window_behind->Flags & ImGuiWindowFlags_ChildWindow))\n            if (window_behind->Viewport->GetRect().Overlaps(window->Rect()))\n                return false;\n    }\n\n    // Move to the existing viewport, Move child/hosted windows as well (FIXME-OPT: iterate child)\n    ImGuiViewportP* old_viewport = window->Viewport;\n    if (window->ViewportOwned)\n        for (int n = 0; n < g.Windows.Size; n++)\n            if (g.Windows[n]->Viewport == old_viewport)\n                SetWindowViewport(g.Windows[n], viewport);\n    SetWindowViewport(window, viewport);\n    BringWindowToDisplayFront(window);\n\n    return true;\n}\n\n// Scale all windows (position, size). Use when e.g. changing DPI. (This is a lossy operation!)\nvoid ImGui::ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale)\n{\n    ImGuiContext& g = *GImGui;\n    if (viewport->Window)\n    {\n        ScaleWindow(viewport->Window, scale);\n    }\n    else\n    {\n        for (int i = 0; i != g.Windows.Size; i++)\n            if (g.Windows[i]->Viewport == viewport)\n                ScaleWindow(g.Windows[i], scale);\n    }\n}\n\n// If the back-end doesn't set MouseLastHoveredViewport or doesn't honor ImGuiViewportFlags_NoInputs, we do a search ourselves.\n// A) It won't take account of the possibility that non-imgui windows may be in-between our dragged window and our target window. \n// B) It requires Platform_GetWindowFocus to be implemented by back-end.\nstatic ImGuiViewportP* FindViewportHoveredFromPlatformWindowStack(const ImVec2 mouse_platform_pos)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiViewportP* best_candidate = NULL;\n    for (int n = 0; n < g.Viewports.Size; n++)\n    {\n        ImGuiViewportP* viewport = g.Viewports[n];\n        if (!(viewport->Flags & (ImGuiViewportFlags_NoInputs | ImGuiViewportFlags_Minimized)) && viewport->GetRect().Contains(mouse_platform_pos))\n            if (best_candidate == NULL || best_candidate->LastFrontMostStampCount < viewport->LastFrontMostStampCount)\n                best_candidate = viewport;\n    }\n    return best_candidate;\n}\n\nstatic void ImGui::UpdateViewportsNewFrame()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.PlatformIO.Viewports.Size <= g.Viewports.Size);\n\n    // Update Minimized status (we need it first in order to decide if we'll apply Pos/Size of the main viewport)\n    for (int n = 0; n < g.Viewports.Size; n++)\n    {\n        ImGuiViewportP* viewport = g.Viewports[n];\n        const bool platform_funcs_available = viewport->PlatformWindowCreated;\n        if ((g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))\n            if (g.PlatformIO.Platform_GetWindowMinimized && platform_funcs_available)\n            {\n                bool minimized = g.PlatformIO.Platform_GetWindowMinimized(viewport);\n                if (minimized)\n                    viewport->Flags |= ImGuiViewportFlags_Minimized;\n                else\n                    viewport->Flags &= ~ImGuiViewportFlags_Minimized;\n            }\n    }\n\n    // Create/update main viewport with current platform position and size\n    ImGuiViewportP* main_viewport = g.Viewports[0];\n    IM_ASSERT(main_viewport->ID == IMGUI_VIEWPORT_DEFAULT_ID);\n    IM_ASSERT(main_viewport->Window == NULL);\n    ImVec2 main_viewport_platform_pos = ImVec2(0.0f, 0.0f);\n    ImVec2 main_viewport_platform_size = g.IO.DisplaySize;\n    if (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)\n        main_viewport_platform_pos = (main_viewport->Flags & ImGuiViewportFlags_Minimized) ? main_viewport->Pos : g.PlatformIO.Platform_GetWindowPos(main_viewport);\n    AddUpdateViewport(NULL, IMGUI_VIEWPORT_DEFAULT_ID, main_viewport_platform_pos, main_viewport_platform_size, ImGuiViewportFlags_CanHostOtherWindows);\n\n    g.CurrentViewport = NULL;\n    g.MouseViewport = NULL;\n    for (int n = 0; n < g.Viewports.Size; n++)\n    {\n        // Erase unused viewports\n        ImGuiViewportP* viewport = g.Viewports[n];\n        viewport->Idx = n;\n\n        if (n > 0 && viewport->LastFrameActive < g.FrameCount - 2)\n        {\n            // Clear references to this viewport in windows (window->ViewportId becomes the master data)\n            for (int window_n = 0; window_n < g.Windows.Size; window_n++)\n                if (g.Windows[window_n]->Viewport == viewport)\n                {\n                    g.Windows[window_n]->Viewport = NULL;\n                    g.Windows[window_n]->ViewportOwned = false;\n                }\n            if (viewport == g.MouseLastHoveredViewport) \n                g.MouseLastHoveredViewport = NULL;\n            g.Viewports.erase(g.Viewports.Data + n);\n\n            // Destroy\n            //IMGUI_DEBUG_LOG(\"Delete Viewport %08X (%s)\\n\", viewport->ID, viewport->Window ? viewport->Window->Name : \"n/a\");\n            DestroyPlatformWindow(viewport); // In most circumstances the platform window will already be destroyed here.\n            IM_ASSERT(g.PlatformIO.Viewports.contains(viewport) == false);\n            IM_DELETE(viewport);\n            n--;\n            continue;\n        }\n\n        const bool platform_funcs_available = viewport->PlatformWindowCreated;\n        if ((g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))\n        {\n            // Update Position and Size (from Platform Window to ImGui) if requested. \n            // We do it early in the frame instead of waiting for UpdatePlatformWindows() to avoid a frame of lag when moving/resizing using OS facilities.\n            if (!(viewport->Flags & ImGuiViewportFlags_Minimized) && platform_funcs_available)\n            {\n                if (viewport->PlatformRequestMove)\n                    viewport->Pos = viewport->LastPlatformPos = g.PlatformIO.Platform_GetWindowPos(viewport);\n                if (viewport->PlatformRequestResize)\n                    viewport->Size = viewport->LastPlatformSize = g.PlatformIO.Platform_GetWindowSize(viewport);\n            }\n\n            UpdateViewportPlatformMonitor(viewport);\n        }\n\n        // Reset alpha every frame. Users of transparency (docking) needs to request a lower alpha back.\n        viewport->Alpha = 1.0f;\n\n        // Translate imgui windows when a Host Viewport has been moved\n        // (This additionally keeps windows at the same place when ImGuiConfigFlags_ViewportsEnable is toggled!)\n        ImVec2 viewport_delta = viewport->Pos - viewport->LastPos;\n        if ((viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows) && (viewport_delta.x != 0.0f || viewport_delta.y != 0.0f))\n            for (int window_n = 0; window_n < g.Windows.Size; window_n++)\n                if (g.Windows[window_n]->Viewport == viewport || (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable) == 0)\n                    TranslateWindow(g.Windows[window_n], viewport_delta);\n\n        // Update DPI scale\n        float new_dpi_scale;\n        if (g.PlatformIO.Platform_GetWindowDpiScale && platform_funcs_available)\n            new_dpi_scale = g.PlatformIO.Platform_GetWindowDpiScale(viewport);\n        else if (viewport->PlatformMonitor != -1)\n            new_dpi_scale = g.PlatformIO.Monitors[viewport->PlatformMonitor].DpiScale;\n        else\n            new_dpi_scale = (viewport->DpiScale != 0.0f) ? viewport->DpiScale : 1.0f;\n        if (viewport->DpiScale != 0.0f && new_dpi_scale != viewport->DpiScale)\n        {\n            float scale_factor = new_dpi_scale / viewport->DpiScale;\n            if (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports)\n                ScaleWindowsInViewport(viewport, scale_factor);\n            //if (viewport == GetMainViewport())\n            //    g.PlatformInterface.SetWindowSize(viewport, viewport->Size * scale_factor);\n\n            // Scale our window moving pivot so that the window will rescale roughly around the mouse position.\n            // FIXME-VIEWPORT: This currently creates a resizing feedback loop when a window is straddling a DPI transition border.\n            // (Minor: since our sizes do not perfectly linearly scale, deferring the click offset scale until we know the actual window scale ratio may get us slightly more precise mouse positioning.)\n            //if (g.MovingWindow != NULL && g.MovingWindow->Viewport == viewport)\n            //    g.ActiveIdClickOffset = ImFloor(g.ActiveIdClickOffset * scale_factor);\n        }\n        viewport->DpiScale = new_dpi_scale;\n    }\n\n    if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))\n    {\n        g.MouseViewport = main_viewport;\n        return;\n    }\n\n    // Mouse handling: decide on the actual mouse viewport for this frame between the active/focused viewport and the hovered viewport.\n    // Note that 'viewport_hovered' should skip over any viewport that has the ImGuiViewportFlags_NoInputs flags set.\n    ImGuiViewportP* viewport_hovered = NULL;\n    if (g.IO.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)\n    {\n        viewport_hovered = g.IO.MouseHoveredViewport ? (ImGuiViewportP*)FindViewportByID(g.IO.MouseHoveredViewport) : NULL;\n        if (viewport_hovered && (viewport_hovered->Flags & ImGuiViewportFlags_NoInputs))\n        {\n            // Back-end failed at honoring its contract if it returned a viewport with the _NoInputs flag.\n            IM_ASSERT(0);\n            viewport_hovered = FindViewportHoveredFromPlatformWindowStack(g.IO.MousePos);\n        }\n    }\n    else\n    {\n        // If the back-end doesn't know how to honor ImGuiViewportFlags_NoInputs, we do a search ourselves. Note that this search:\n        // A) won't take account of the possibility that non-imgui windows may be in-between our dragged window and our target window. \n        // B) uses LastFrameAsRefViewport as a flawed replacement for the last time a window was focused (we could/should fix that by introducing Focus functions in PlatformIO)\n        viewport_hovered = FindViewportHoveredFromPlatformWindowStack(g.IO.MousePos);\n    }\n    if (viewport_hovered != NULL)\n        g.MouseLastHoveredViewport = viewport_hovered;\n    else if (g.MouseLastHoveredViewport == NULL)\n        g.MouseLastHoveredViewport = g.Viewports[0];\n\n    // Update mouse reference viewport\n    // (when moving a window we aim at its viewport, but this will be overwritten below if we go in drag and drop mode)\n    if (g.MovingWindow)\n        g.MouseViewport = g.MovingWindow->Viewport; \n    else\n        g.MouseViewport = g.MouseLastHoveredViewport;\n\n    // When dragging something, always refer to the last hovered viewport. \n    // - when releasing a moving window we will revert to aiming behind (at viewport_hovered)\n    // - when we are between viewports, our dragged preview will tend to show in the last viewport _even_ if we don't have tooltips in their viewports (when lacking monitor info)\n    // - consider the case of holding on a menu item to browse child menus: even thou a mouse button is held, there's no active id because menu items only react on mouse release.\n    const bool is_mouse_dragging_with_an_expected_destination = g.DragDropActive;\n    if (is_mouse_dragging_with_an_expected_destination && viewport_hovered == NULL)\n        viewport_hovered = g.MouseLastHoveredViewport;\n    if (is_mouse_dragging_with_an_expected_destination || g.ActiveId == 0 || !IsAnyMouseDown())\n        if (viewport_hovered != NULL && viewport_hovered != g.MouseViewport && !(viewport_hovered->Flags & ImGuiViewportFlags_NoInputs))\n            g.MouseViewport = viewport_hovered;\n\n    IM_ASSERT(g.MouseViewport != NULL);\n}\n\n// Update user-facing viewport list (g.Viewports -> g.PlatformIO.Viewports after filtering out some)\nstatic void ImGui::UpdateViewportsEndFrame()\n{\n    ImGuiContext& g = *GImGui;\n    g.PlatformIO.MainViewport = g.Viewports[0];\n    g.PlatformIO.Viewports.resize(0);\n    for (int i = 0; i < g.Viewports.Size; i++)\n    {\n        ImGuiViewportP* viewport = g.Viewports[i];\n        viewport->LastPos = viewport->Pos;\n        if (viewport->LastFrameActive < g.FrameCount || viewport->Size.x <= 0.0f || viewport->Size.y <= 0.0f)\n            if (i > 0) // Always include main viewport in the list\n                continue;\n        if (viewport->Window && !IsWindowActiveAndVisible(viewport->Window))\n            continue;\n        if (i > 0)\n            IM_ASSERT(viewport->Window != NULL);\n        g.PlatformIO.Viewports.push_back(viewport);\n    }\n    g.Viewports[0]->ClearRequestFlags(); // Clear main viewport flags because UpdatePlatformWindows() won't do it and may not even be called\n}\n\n// FIXME: We should ideally refactor the system to call this every frame (we currently don't)\nImGuiViewportP* ImGui::AddUpdateViewport(ImGuiWindow* window, ImGuiID id, const ImVec2& pos, const ImVec2& size, ImGuiViewportFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(id != 0);\n\n    if (window != NULL)\n    {\n        if (g.MovingWindow && g.MovingWindow->RootWindow == window)\n            flags |= ImGuiViewportFlags_NoInputs | ImGuiViewportFlags_NoFocusOnAppearing;\n        if ((window->Flags & ImGuiWindowFlags_NoMouseInputs) && (window->Flags & ImGuiWindowFlags_NoNavInputs))\n            flags |= ImGuiViewportFlags_NoInputs;\n        if (window->Flags & ImGuiWindowFlags_NoFocusOnAppearing)\n            flags |= ImGuiViewportFlags_NoFocusOnAppearing;\n    }\n\n    ImGuiViewportP* viewport = (ImGuiViewportP*)FindViewportByID(id);\n    if (viewport)\n    {\n        if (!viewport->PlatformRequestMove)\n            viewport->Pos = pos;\n        if (!viewport->PlatformRequestResize)\n            viewport->Size = size;\n        viewport->Flags = flags | (viewport->Flags & ImGuiViewportFlags_Minimized); // Preserve existing flags\n    }\n    else\n    {\n        // New viewport\n        viewport = IM_NEW(ImGuiViewportP)();\n        viewport->ID = id;\n        viewport->Idx = g.Viewports.Size;\n        viewport->Pos = viewport->LastPos = pos;\n        viewport->Size = size;\n        viewport->Flags = flags;\n        UpdateViewportPlatformMonitor(viewport);\n        g.Viewports.push_back(viewport);\n        //IMGUI_DEBUG_LOG(\"Add Viewport %08X (%s)\\n\", id, window->Name);\n\n        // We normally setup for all viewports in NewFrame() but here need to handle the mid-frame creation of a new viewport.\n        // We need to extend the fullscreen clip rect so the OverlayDrawList clip is correct for that the first frame\n        g.DrawListSharedData.ClipRectFullscreen.x = ImMin(g.DrawListSharedData.ClipRectFullscreen.x, viewport->Pos.x);\n        g.DrawListSharedData.ClipRectFullscreen.y = ImMin(g.DrawListSharedData.ClipRectFullscreen.y, viewport->Pos.y);\n        g.DrawListSharedData.ClipRectFullscreen.z = ImMax(g.DrawListSharedData.ClipRectFullscreen.z, viewport->Pos.x + viewport->Size.x);\n        g.DrawListSharedData.ClipRectFullscreen.w = ImMax(g.DrawListSharedData.ClipRectFullscreen.w, viewport->Pos.y + viewport->Size.y);\n\n        // Store initial DpiScale before the OS platform window creation, based on expected monitor data.\n        // This is so we can select an appropriate font size on the first frame of our window lifetime\n        if (viewport->PlatformMonitor != -1)\n            viewport->DpiScale = g.PlatformIO.Monitors[viewport->PlatformMonitor].DpiScale;\n    }\n\n    viewport->Window = window;\n    viewport->LastFrameActive = g.FrameCount;\n    IM_ASSERT(window == NULL || viewport->ID == window->ID);\n\n    if (window != NULL)\n        window->ViewportOwned = true;\n\n    return viewport;\n}\n\n// FIXME-VIEWPORT: This is all super messy and ought to be clarified or rewritten.\nstatic void ImGui::UpdateSelectWindowViewport(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindowFlags flags = window->Flags;\n    window->ViewportAllowPlatformMonitorExtend = -1;\n\n    // Restore main viewport if multi-viewport is not supported by the back-end\n    ImGuiViewportP* main_viewport = g.Viewports[0];\n    if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))\n    {\n        SetWindowViewport(window, main_viewport);\n        return;\n    }\n    window->ViewportOwned = false;\n\n    // Appearing popups reset their viewport so they can inherit again\n    if ((flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) && window->Appearing)\n    {\n        window->Viewport = NULL;\n        window->ViewportId = 0;\n    }\n\n    if (!g.NextWindowData.ViewportCond)\n    {\n        // By default inherit from parent window\n        if (window->Viewport == NULL && window->ParentWindow)\n            window->Viewport = window->ParentWindow->Viewport;\n\n        // Attempt to restore saved viewport id (= window that hasn't been activated yet), try to restore the viewport based on saved 'window->ViewportPos' restored from .ini file\n        if (window->Viewport == NULL && window->ViewportId != 0)\n        {\n            window->Viewport = (ImGuiViewportP*)FindViewportByID(window->ViewportId);\n            if (window->Viewport == NULL && window->ViewportPos.x != FLT_MAX && window->ViewportPos.y != FLT_MAX)\n                window->Viewport = AddUpdateViewport(window, window->ID, window->ViewportPos, window->Size, ImGuiViewportFlags_None);\n        }\n    }\n\n    if (g.NextWindowData.ViewportCond)\n    {\n        // Code explicitly request a viewport\n        window->Viewport = (ImGuiViewportP*)FindViewportByID(g.NextWindowData.ViewportId);\n        window->ViewportId = g.NextWindowData.ViewportId; // Store ID even if Viewport isn't resolved yet.\n    }\n    else if ((flags & ImGuiWindowFlags_ChildWindow) || (flags & ImGuiWindowFlags_ChildMenu))\n    {\n        // Always inherit viewport from parent window\n        window->Viewport = window->ParentWindow->Viewport;\n    }\n    else if (flags & ImGuiWindowFlags_Tooltip)\n    {\n        window->Viewport = g.MouseViewport;\n    }\n    else if (GetWindowAlwaysWantOwnViewport(window))\n    {\n        window->Viewport = AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_None);\n    }\n    else if (g.MovingWindow && g.MovingWindow->RootWindow == window && IsMousePosValid())\n    {\n        if (window->Viewport != NULL && window->Viewport->Window == window)\n            window->Viewport = AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_None);\n    }\n    else\n    {\n        // Merge into host viewport?\n        // We cannot test window->ViewportOwned as it set lower in the function.\n        bool try_to_merge_into_host_viewport = (window->Viewport && window == window->Viewport->Window && g.ActiveId == 0);\n        if (try_to_merge_into_host_viewport)\n            UpdateTryMergeWindowIntoHostViewport(window, g.Viewports[0]);\n    }\n\n    // Fallback to default viewport\n    if (window->Viewport == NULL)\n        window->Viewport = main_viewport;\n\n    // Mark window as allowed to protrude outside of its viewport and into the current monitor\n    if (flags & (ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup))\n    {\n        // We need to take account of the possibility that mouse may become invalid.\n        // Popups/Tooltip always set ViewportAllowPlatformMonitorExtend so GetWindowAllowedExtentRect() will return full monitor bounds.\n        ImVec2 mouse_ref = (flags & ImGuiWindowFlags_Tooltip) ? g.IO.MousePos : g.BeginPopupStack.back().OpenMousePos;\n        bool use_mouse_ref = (g.NavDisableHighlight || !g.NavDisableMouseHover || !g.NavWindow);\n        bool mouse_valid = IsMousePosValid(&mouse_ref);\n        if ((window->Appearing || (flags & ImGuiWindowFlags_Tooltip)) && (!use_mouse_ref || mouse_valid))\n            window->ViewportAllowPlatformMonitorExtend = FindPlatformMonitorForPos((use_mouse_ref && mouse_valid) ? mouse_ref : NavCalcPreferredRefPos());\n        else \n            window->ViewportAllowPlatformMonitorExtend = window->Viewport->PlatformMonitor;\n    }\n    else if (window->Viewport && window != window->Viewport->Window && window->Viewport->Window && !(flags & ImGuiWindowFlags_ChildWindow))\n    {\n        // When called from Begin() we don't have access to a proper version of the Hidden flag yet, so we replicate this code.\n        const bool will_be_visible = (window->DockIsActive && !window->DockTabIsVisible) ? false : true;\n        if ((window->Flags & ImGuiWindowFlags_DockNodeHost) && window->Viewport->LastFrameActive < g.FrameCount && will_be_visible)\n        {\n            // Steal/transfer ownership\n            //IMGUI_DEBUG_LOG(\"[%05d] Window '%s' steal Viewport %08X from Window '%s'\\n\", g.FrameCount, window->Name, window->Viewport->ID, window->Viewport->Window->Name);\n            window->Viewport->Window = window;\n            window->Viewport->ID = window->ID;\n            window->Viewport->LastNameHash = 0;\n        }\n        else if (!UpdateTryMergeWindowIntoHostViewport(window, g.Viewports[0])) // Merge?\n        {\n            // New viewport\n            window->Viewport = AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_NoFocusOnAppearing);\n        }\n    }\n    // Regular (non-child, non-popup) windows by default are also allowed to protrude\n    // Child windows are kept contained within their parent.\n    else if (window->ViewportAllowPlatformMonitorExtend < 0 && (flags & ImGuiWindowFlags_ChildWindow) == 0)\n        window->ViewportAllowPlatformMonitorExtend = window->Viewport->PlatformMonitor;\n\n    // Update flags\n    window->ViewportOwned = (window == window->Viewport->Window);\n    window->ViewportId = window->Viewport->ID;\n\n    // If the OS window has a title bar, hide our imgui title bar\n    //if (window->ViewportOwned && !(window->Viewport->Flags & ImGuiViewportFlags_NoDecoration))\n    //    window->Flags |= ImGuiWindowFlags_NoTitleBar;\n}\n\n// Called by user at the end of the main loop, after EndFrame()\n// This will handle the creation/update of all OS windows via function defined in the ImGuiPlatformIO api.\nvoid ImGui::UpdatePlatformWindows()\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.FrameCountEnded == g.FrameCount && \"Forgot to call Render() or EndFrame() before UpdatePlatformWindows()?\");\n    IM_ASSERT(g.FrameCountPlatformEnded < g.FrameCount);\n    g.FrameCountPlatformEnded = g.FrameCount;\n    if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable))\n        return;\n\n    // Create/resize/destroy platform windows to match each active viewport.\n    // Skip the main viewport (index 0), which is always fully handled by the application!\n    for (int i = 1; i < g.Viewports.Size; i++)\n    {\n        ImGuiViewportP* viewport = g.Viewports[i];\n\n        // Destroy platform window if the viewport hasn't been submitted or if it is hosting a hidden window \n        // (the implicit/fallback Debug##Default window will be registering its viewport then be disabled, causing a dummy DestroyPlatformWindow to be made each frame)\n        bool destroy_platform_window = false;\n        destroy_platform_window |= (viewport->LastFrameActive < g.FrameCount - 1);\n        destroy_platform_window |= (viewport->Window && !IsWindowActiveAndVisible(viewport->Window));\n        if (destroy_platform_window)\n        {\n            DestroyPlatformWindow(viewport);\n            continue;\n        }\n\n        // New windows that appears directly in a new viewport won't always have a size on their first frame\n        if (viewport->LastFrameActive < g.FrameCount || viewport->Size.x <= 0 || viewport->Size.y <= 0)\n            continue;\n\n        // Create window\n        bool is_new_platform_window = (viewport->PlatformWindowCreated == false);\n        if (is_new_platform_window)\n        {\n            //IMGUI_DEBUG_LOG(\"Create Platform Window %08X (%s)\\n\", viewport->ID, viewport->Window ? viewport->Window->Name : \"n/a\");\n            g.PlatformIO.Platform_CreateWindow(viewport);\n            if (g.PlatformIO.Renderer_CreateWindow != NULL)\n                g.PlatformIO.Renderer_CreateWindow(viewport);\n            viewport->LastNameHash = 0;\n            viewport->LastPlatformPos = viewport->LastPlatformSize = ImVec2(FLT_MAX, FLT_MAX); // By clearing those we'll enforce a call to Platform_SetWindowPos/Size below, before Platform_ShowWindow (FIXME: Is that necessary?)\n            viewport->LastRendererSize = viewport->Size;                                       // We don't need to call Renderer_SetWindowSize() as it is expected Renderer_CreateWindow() already did it.\n            viewport->PlatformWindowCreated = true;\n        }\n\n        // Apply Position and Size (from ImGui to Platform/Renderer back-ends)\n        if ((viewport->LastPlatformPos.x != viewport->Pos.x || viewport->LastPlatformPos.y != viewport->Pos.y) && !viewport->PlatformRequestMove)\n            g.PlatformIO.Platform_SetWindowPos(viewport, viewport->Pos);\n        if ((viewport->LastPlatformSize.x != viewport->Size.x || viewport->LastPlatformSize.y != viewport->Size.y) && !viewport->PlatformRequestResize)\n            g.PlatformIO.Platform_SetWindowSize(viewport, viewport->Size);\n        if ((viewport->LastRendererSize.x != viewport->Size.x || viewport->LastRendererSize.y != viewport->Size.y) && g.PlatformIO.Renderer_SetWindowSize)\n            g.PlatformIO.Renderer_SetWindowSize(viewport, viewport->Size);\n        viewport->LastPlatformPos = viewport->Pos;\n        viewport->LastPlatformSize = viewport->LastRendererSize = viewport->Size;\n\n        // Update title bar (if it changed)\n        if (ImGuiWindow* window_for_title = GetWindowForTitleDisplay(viewport->Window))\n        {\n            const char* title_begin = window_for_title->Name;\n            char* title_end = (char*)(intptr_t)FindRenderedTextEnd(title_begin);\n            const ImGuiID title_hash = ImHashStr(title_begin, title_end - title_begin);\n            if (viewport->LastNameHash != title_hash)\n            {\n                char title_end_backup_c = *title_end;\n                *title_end = 0; // Cut existing buffer short instead of doing an alloc/free, no small gain.\n                g.PlatformIO.Platform_SetWindowTitle(viewport, title_begin);\n                *title_end = title_end_backup_c;\n                viewport->LastNameHash = title_hash;\n            }\n        }\n\n        // Update alpha (if it changed)\n        if (viewport->LastAlpha != viewport->Alpha && g.PlatformIO.Platform_SetWindowAlpha)\n            g.PlatformIO.Platform_SetWindowAlpha(viewport, viewport->Alpha);\n        viewport->LastAlpha = viewport->Alpha;\n\n        // Optional, general purpose call to allow the back-end to perform general book-keeping even if things haven't changed.\n        if (g.PlatformIO.Platform_UpdateWindow)\n            g.PlatformIO.Platform_UpdateWindow(viewport);\n\n        if (is_new_platform_window)\n        {\n            // On startup ensure new platform window don't steal focus (give it a few frames, as nested contents may lead to viewport being created a few frames late)\n            if (g.FrameCount < 3)\n                viewport->Flags |= ImGuiViewportFlags_NoFocusOnAppearing;\n\n            // Show window\n            g.PlatformIO.Platform_ShowWindow(viewport);\n\n            // Even without focus, we assume the window becomes front-most. \n            // This is useful for our platform z-order heuristic when io.MouseHoveredViewport is not available.\n            if (viewport->LastFrontMostStampCount != g.ViewportFrontMostStampCount)\n                viewport->LastFrontMostStampCount = ++g.ViewportFrontMostStampCount;\n        }\n\n        // Clear request flags\n        viewport->ClearRequestFlags();\n    }\n\n    // Update our implicit z-order knowledge of platform windows, which is used when the back-end cannot provide io.MouseHoveredViewport.\n    // When setting Platform_GetWindowFocus, it is expected that the platform back-end can handle calls without crashing if it doesn't have data stored. \n    if (g.PlatformIO.Platform_GetWindowFocus != NULL)\n    {\n        ImGuiViewportP* focused_viewport = NULL;\n        for (int n = 0; n < g.Viewports.Size && focused_viewport == NULL; n++)\n        {\n            ImGuiViewportP* viewport = g.Viewports[n];\n            if (viewport->PlatformWindowCreated)\n                if (g.PlatformIO.Platform_GetWindowFocus(viewport))\n                    focused_viewport = viewport;\n        }\n        if (focused_viewport && g.PlatformLastFocusedViewport != focused_viewport->ID)\n        {\n            if (focused_viewport->LastFrontMostStampCount != g.ViewportFrontMostStampCount)\n                focused_viewport->LastFrontMostStampCount = ++g.ViewportFrontMostStampCount;\n            g.PlatformLastFocusedViewport = focused_viewport->ID;\n        }\n    }\n}\n\n// This is a default/basic function for performing the rendering/swap of multiple Platform Windows.\n// Custom renderers may prefer to not call this function at all, and instead iterate the publicly exposed platform data and handle rendering/sync themselves.\n// The Render/Swap functions stored in ImGuiPlatformIO are merely here to allow for this helper to exist, but you can do it yourself:\n//\n//    ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();\n//    for (int i = 1; i < platform_io.Viewports.Size; i++)\n//        if ((platform_io.Viewports[i]->Flags & ImGuiViewportFlags_Minimized) == 0)\n//            MyRenderFunction(platform_io.Viewports[i], my_args);\n//    for (int i = 1; i < platform_io.Viewports.Size; i++)\n//        if ((platform_io.Viewports[i]->Flags & ImGuiViewportFlags_Minimized) == 0)\n//            MySwapBufferFunction(platform_io.Viewports[i], my_args);\n//\nvoid ImGui::RenderPlatformWindowsDefault(void* platform_render_arg, void* renderer_render_arg)\n{\n    // Skip the main viewport (index 0), which is always fully handled by the application!\n    ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();\n    for (int i = 1; i < platform_io.Viewports.Size; i++)\n    {\n        ImGuiViewport* viewport = platform_io.Viewports[i];\n        if (viewport->Flags & ImGuiViewportFlags_Minimized)\n            continue;\n        if (platform_io.Platform_RenderWindow) platform_io.Platform_RenderWindow(viewport, platform_render_arg);\n        if (platform_io.Renderer_RenderWindow) platform_io.Renderer_RenderWindow(viewport, renderer_render_arg);\n    }\n    for (int i = 1; i < platform_io.Viewports.Size; i++)\n    {\n        ImGuiViewport* viewport = platform_io.Viewports[i];\n        if (viewport->Flags & ImGuiViewportFlags_Minimized)\n            continue;\n        if (platform_io.Platform_SwapBuffers) platform_io.Platform_SwapBuffers(viewport, platform_render_arg);\n        if (platform_io.Renderer_SwapBuffers) platform_io.Renderer_SwapBuffers(viewport, renderer_render_arg);\n    }\n}\n\nstatic int ImGui::FindPlatformMonitorForPos(const ImVec2& pos)\n{\n    ImGuiContext& g = *GImGui;\n    for (int monitor_n = 0; monitor_n < g.PlatformIO.Monitors.Size; monitor_n++)\n    {\n        const ImGuiPlatformMonitor& monitor = g.PlatformIO.Monitors[monitor_n];\n        if (ImRect(monitor.MainPos, monitor.MainPos + monitor.MainSize).Contains(pos))\n            return monitor_n;\n    }\n    return -1;\n}\n\n// Search for the monitor with the largest intersection area with the given rectangle\n// We generally try to avoid searching loops but the monitor count should be very small here\n// FIXME-OPT: We could test the last monitor used for that viewport first..\nstatic int ImGui::FindPlatformMonitorForRect(const ImRect& rect)\n{\n    ImGuiContext& g = *GImGui;\n\n    // Use a minimum threshold of 1.0f so a zero-sized rect won't false positive, and will still find the correct monitor given its position. \n    // This is necessary for tooltips which always resize down to zero at first.\n    const float surface_threshold = ImMax(rect.GetWidth() * rect.GetHeight() * 0.5f, 1.0f);\n    int best_monitor_n = -1;\n    float best_monitor_surface = 0.001f;\n\n    for (int monitor_n = 0; monitor_n < g.PlatformIO.Monitors.Size && best_monitor_surface < surface_threshold; monitor_n++)\n    {\n        const ImGuiPlatformMonitor& monitor = g.PlatformIO.Monitors[monitor_n];\n        const ImRect monitor_rect = ImRect(monitor.MainPos, monitor.MainPos + monitor.MainSize);\n        if (monitor_rect.Contains(rect))\n            return monitor_n;\n        ImRect overlapping_rect = rect;\n        overlapping_rect.ClipWithFull(monitor_rect);\n        float overlapping_surface = overlapping_rect.GetWidth() * overlapping_rect.GetHeight();\n        if (overlapping_surface < best_monitor_surface)\n            continue;\n        best_monitor_surface = overlapping_surface;\n        best_monitor_n = monitor_n;\n    }\n    return best_monitor_n;\n}\n\n// Update monitor from viewport rectangle (we'll use this info to clamp windows and save windows lost in a removed monitor)\nstatic void ImGui::UpdateViewportPlatformMonitor(ImGuiViewportP* viewport)\n{\n    viewport->PlatformMonitor = (short)FindPlatformMonitorForRect(viewport->GetRect());\n}\n\nvoid ImGui::DestroyPlatformWindow(ImGuiViewportP* viewport)\n{\n    ImGuiContext& g = *GImGui;\n    if (viewport->PlatformWindowCreated)\n    {\n        if (g.PlatformIO.Renderer_DestroyWindow)\n            g.PlatformIO.Renderer_DestroyWindow(viewport);\n        if (g.PlatformIO.Platform_DestroyWindow)\n            g.PlatformIO.Platform_DestroyWindow(viewport);\n        IM_ASSERT(viewport->RendererUserData == NULL && viewport->PlatformUserData == NULL);\n        viewport->PlatformWindowCreated = false;\n    }\n    else\n    {\n        IM_ASSERT(viewport->RendererUserData == NULL && viewport->PlatformUserData == NULL && viewport->PlatformHandle == NULL);\n    }\n    viewport->RendererUserData = viewport->PlatformUserData = viewport->PlatformHandle = NULL;\n    viewport->ClearRequestFlags();\n}\n\nvoid ImGui::DestroyPlatformWindows()\n{\n    // We call the destroy window on every viewport (including the main viewport, index 0) to give a chance to the back-end \n    // to clear any data they may have stored in e.g. PlatformUserData, RendererUserData. \n    // It is convenient for the platform back-end code to store something in the main viewport, in order for e.g. the mouse handling \n    // code to operator a consistent manner.\n    // It is expected that the back-end can handle calls to Renderer_DestroyWindow/Platform_DestroyWindow without\n    // crashing if it doesn't have data stored. \n    ImGuiContext& g = *GImGui;\n    for (int i = 0; i < g.Viewports.Size; i++)\n        DestroyPlatformWindow(g.Viewports[i]);\n}\n\n\n//-----------------------------------------------------------------------------\n// [SECTION] DOCKING\n//-----------------------------------------------------------------------------\n// Docking: Internal Types\n// Docking: Forward Declarations\n// Docking: ImGuiDockContext\n// Docking: ImGuiDockContext Docking/Undocking functions\n// Docking: ImGuiDockNode\n// Docking: ImGuiDockNode Tree manipulation functions\n// Docking: Public Functions (SetWindowDock, DockSpace)\n// Docking: Builder Functions\n// Docking: Begin/End Functions (called from Begin/End)\n// Docking: Settings\n//-----------------------------------------------------------------------------\n\n//-----------------------------------------------------------------------------\n// Docking: Internal Types\n//-----------------------------------------------------------------------------\n\nstatic float IMGUI_DOCK_SPLITTER_SIZE = 2.0f;\n\nenum ImGuiDockRequestType\n{\n    ImGuiDockRequestType_None = 0,\n    ImGuiDockRequestType_Dock,\n    ImGuiDockRequestType_Undock,\n    ImGuiDockRequestType_Split                  // Split is the same as Dock but without a DockPayload\n};\n\nstruct ImGuiDockRequest\n{\n    ImGuiDockRequestType    Type;\n    ImGuiWindow*            DockTargetWindow;   // Destination/Target Window to dock into (may be a loose window or a DockNode, might be NULL in which case DockTargetNode cannot be NULL)\n    ImGuiDockNode*          DockTargetNode;     // Destination/Target Node to dock into \n    ImGuiWindow*            DockPayload;        // Source/Payload window to dock (may be a loose window or a DockNode), [Optional]\n    ImGuiDir                DockSplitDir;\n    float                   DockSplitRatio;\n    bool                    DockSplitOuter;\n    ImGuiWindow*            UndockTargetWindow;\n    ImGuiDockNode*          UndockTargetNode;\n\n    ImGuiDockRequest()\n    {\n        Type = ImGuiDockRequestType_None;\n        DockTargetWindow = DockPayload = UndockTargetWindow = NULL;\n        DockTargetNode = UndockTargetNode = NULL;\n        DockSplitDir = ImGuiDir_None;\n        DockSplitRatio = 0.5f;\n        DockSplitOuter = false;\n    }\n};\n\nstruct ImGuiDockPreviewData\n{\n    ImGuiDockNode   FutureNode;\n    bool            IsDropAllowed;\n    bool            IsCenterAvailable;\n    bool            IsSidesAvailable;           // Hold your breath, grammar freaks..\n    bool            IsSplitDirExplicit;         // Set when hovered the drop rect (vs. implicit SplitDir==None when hovered the window)\n    ImGuiDockNode*  SplitNode;\n    ImGuiDir        SplitDir;\n    float           SplitRatio;\n    ImRect          DropRectsDraw[ImGuiDir_COUNT + 1];  // May be slightly different from hit-testing drop rects used in DockNodeCalcDropRects()\n\n    ImGuiDockPreviewData() : FutureNode(0) { IsDropAllowed = IsCenterAvailable = IsSidesAvailable = IsSplitDirExplicit = false; SplitNode = NULL; SplitDir = ImGuiDir_None; SplitRatio = 0.f; }\n};\n\n// Persistent Settings data, stored contiguously in SettingsNodes (sizeof() ~32 bytes)\nstruct ImGuiDockNodeSettings\n{\n    ImGuiID         ID;\n    ImGuiID         ParentID;\n    ImGuiID         SelectedTabID;\n    signed char     SplitAxis;\n    char            Depth;\n    char            IsDockSpace;\n    char            IsCentralNode;\n    char            IsHiddenTabBar;\n    ImVec2ih        Pos;\n    ImVec2ih        Size;\n    ImVec2ih        SizeRef;\n    ImGuiDockNodeSettings() { ID = ParentID = SelectedTabID = 0; SplitAxis = ImGuiAxis_None; Depth = 0; IsDockSpace = IsCentralNode = IsHiddenTabBar = 0; }\n};\n\nstruct ImGuiDockContext\n{\n    ImGuiStorage                    Nodes;                      // Map ID -> ImGuiDockNode*: Active nodes\n    ImVector<ImGuiDockRequest>      Requests;\n    ImVector<ImGuiDockNodeSettings> SettingsNodes;\n    bool                            WantFullRebuild;\n    ImGuiDockContext()              { WantFullRebuild = false; }\n};\n\n//-----------------------------------------------------------------------------\n// Docking: Forward Declarations\n//-----------------------------------------------------------------------------\n\nnamespace ImGui\n{\n    // ImGuiDockContext\n    static ImGuiDockNode*   DockContextAddNode(ImGuiContext* ctx, ImGuiID id);\n    static ImGuiID          DockContextGenNodeID(ImGuiContext* ctx);\n    static void             DockContextRemoveNode(ImGuiContext* ctx, ImGuiDockNode* node, bool merge_sibling_into_parent_node);\n    static void             DockContextQueueNotifyRemovedNode(ImGuiContext* ctx, ImGuiDockNode* node);\n    static void             DockContextProcessDock(ImGuiContext* ctx, ImGuiDockRequest* req);\n    static void             DockContextProcessUndockWindow(ImGuiContext* ctx, ImGuiWindow* window, bool clear_persistent_docking_ref = true);\n    static void             DockContextProcessUndockNode(ImGuiContext* ctx, ImGuiDockNode* node);\n    static void             DockContextPruneUnusedSettingsNodes(ImGuiContext* ctx);\n    static ImGuiDockNode*   DockContextFindNodeByID(ImGuiContext* ctx, ImGuiID id);\n    static void             DockContextClearNodes(ImGuiContext* ctx, ImGuiID root_id, bool clear_persistent_docking_refs);    // Use root_id==0 to clear all\n    static void             DockContextBuildNodesFromSettings(ImGuiContext* ctx, ImGuiDockNodeSettings* node_settings_array, int node_settings_count);\n    static void             DockContextBuildAddWindowsToNodes(ImGuiContext* ctx, ImGuiID root_id);                            // Use root_id==0 to add all\n\n    // ImGuiDockNode\n    static void             DockNodeAddWindow(ImGuiDockNode* node, ImGuiWindow* window, bool add_to_tab_bar);\n    static void             DockNodeMoveWindows(ImGuiDockNode* dst_node, ImGuiDockNode* src_node);\n    static void             DockNodeMoveChildNodes(ImGuiDockNode* dst_node, ImGuiDockNode* src_node);\n    static void             DockNodeApplyPosSizeToWindows(ImGuiDockNode* node);\n    static void             DockNodeRemoveWindow(ImGuiDockNode* node, ImGuiWindow* window, ImGuiID save_dock_id);\n    static void             DockNodeHideHostWindow(ImGuiDockNode* node);\n    static void             DockNodeUpdate(ImGuiDockNode* node);\n    static void             DockNodeUpdateVisibleFlagAndInactiveChilds(ImGuiDockNode* node);\n    static void             DockNodeUpdateTabBar(ImGuiDockNode* node, ImGuiWindow* host_window);\n    static void             DockNodeAddTabBar(ImGuiDockNode* node);\n    static void             DockNodeRemoveTabBar(ImGuiDockNode* node);\n    static ImGuiID          DockNodeUpdateTabListMenu(ImGuiDockNode* node, ImGuiTabBar* tab_bar);\n    static void             DockNodeUpdateVisibleFlag(ImGuiDockNode* node);\n    static void             DockNodeStartMouseMovingWindow(ImGuiDockNode* node, ImGuiWindow* window);\n    static bool             DockNodeIsDropAllowed(ImGuiWindow* host_window, ImGuiWindow* payload_window);\n    static void             DockNodePreviewDockCalc(ImGuiWindow* host_window, ImGuiDockNode* host_node, ImGuiWindow* payload_window, ImGuiDockPreviewData* preview_data, bool is_explicit_target, bool is_outer_docking);\n    static void             DockNodePreviewDockRender(ImGuiWindow* host_window, ImGuiDockNode* host_node, ImGuiWindow* payload_window, const ImGuiDockPreviewData* preview_data);\n    static ImRect           DockNodeCalcTabBarRect(const ImGuiDockNode* node);\n    static void             DockNodeCalcSplitRects(ImVec2& pos_old, ImVec2& size_old, ImVec2& pos_new, ImVec2& size_new, ImGuiDir dir, ImVec2 size_new_desired);\n    static bool             DockNodeCalcDropRectsAndTestMousePos(const ImRect& parent, ImGuiDir dir, ImRect& out_draw, bool outer_docking, ImVec2* test_mouse_pos);\n    static const char*      DockNodeGetHostWindowTitle(ImGuiDockNode* node, char* buf, int buf_size) { ImFormatString(buf, buf_size, \"##DockNode_%02X\", node->ID); return buf; }\n    static int              DockNodeGetDepth(const ImGuiDockNode* node) { int depth = 0; while (node->ParentNode) { node = node->ParentNode; depth++; } return depth; }\n    static int              DockNodeGetTabOrder(ImGuiWindow* window);\n\n    // ImGuiDockNode tree manipulations\n    static void             DockNodeTreeSplit(ImGuiContext* ctx, ImGuiDockNode* parent_node, ImGuiAxis split_axis, int split_first_child, float split_ratio, ImGuiDockNode* new_node);\n    static void             DockNodeTreeMerge(ImGuiContext* ctx, ImGuiDockNode* parent_node, ImGuiDockNode* merge_lead_child);\n    static void             DockNodeTreeUpdatePosSize(ImGuiDockNode* node, ImVec2 pos, ImVec2 size);\n    static void             DockNodeTreeUpdateSplitter(ImGuiDockNode* node);\n    static ImGuiDockNode*   DockNodeTreeFindNodeByPos(ImGuiDockNode* node, ImVec2 pos);\n    static ImGuiDockNode*   DockNodeTreeFindFallbackLeafNode(ImGuiDockNode* node);\n\n    // Settings\n    static void             DockSettingsRenameNodeReferences(ImGuiID old_node_id, ImGuiID new_node_id);\n    static void             DockSettingsRemoveNodeReferences(ImGuiID* node_ids, int node_ids_count);\n    static ImGuiDockNodeSettings*   DockSettingsFindNodeSettings(ImGuiContext* ctx, ImGuiID node_id);\n    static void*            DockSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name);\n    static void             DockSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line);\n    static void             DockSettingsHandler_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf);\n}\n\n//-----------------------------------------------------------------------------\n// Docking: ImGuiDockContext\n//-----------------------------------------------------------------------------\n// The lifetime model is different from the one of regular windows: we always create a ImGuiDockNode for each ImGuiDockNodeSettings,\n// or we always hold the entire docking node tree. Nodes are frequently hidden, e.g. if the window(s) or child nodes they host are not active.\n// At boot time only, we run a simple GC to remove nodes that have no references.\n// Because dock node settings (which are small, contiguous structures) are always mirrored by their corresponding dock nodes (more complete structures),\n// we can also very easily recreate the nodes from scratch given the settings data (this is what DockContextRebuild() does).\n// This is convenient as docking reconfiguration can be implemented by mostly poking at the simpler settings data.\n//-----------------------------------------------------------------------------\n\nvoid ImGui::DockContextInitialize(ImGuiContext* ctx)\n{\n    ImGuiContext& g = *ctx;\n    IM_ASSERT(g.DockContext == NULL);\n    g.DockContext = IM_NEW(ImGuiDockContext)();\n\n    // Add .ini handle for persistent docking data\n    ImGuiSettingsHandler ini_handler;\n    ini_handler.TypeName = \"Docking\";\n    ini_handler.TypeHash = ImHashStr(\"Docking\");\n    ini_handler.ReadOpenFn = DockSettingsHandler_ReadOpen;\n    ini_handler.ReadLineFn = DockSettingsHandler_ReadLine;\n    ini_handler.WriteAllFn = DockSettingsHandler_WriteAll;\n    g.SettingsHandlers.push_back(ini_handler);\n}\n\nvoid ImGui::DockContextShutdown(ImGuiContext* ctx)\n{\n    ImGuiContext& g = *ctx;\n    ImGuiDockContext* dc = ctx->DockContext;\n    for (int n = 0; n < dc->Nodes.Data.Size; n++)\n        if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p)\n            IM_DELETE(node);\n    IM_DELETE(g.DockContext);\n    g.DockContext = NULL;\n}\n\nvoid ImGui::DockContextOnLoadSettings(ImGuiContext* ctx)\n{\n    ImGuiDockContext* dc = ctx->DockContext;\n    DockContextPruneUnusedSettingsNodes(ctx);\n    DockContextBuildNodesFromSettings(ctx, dc->SettingsNodes.Data, dc->SettingsNodes.Size);\n}\n\nvoid ImGui::DockContextClearNodes(ImGuiContext* ctx, ImGuiID root_id, bool clear_persistent_docking_references)\n{\n    IM_ASSERT(ctx == GImGui);\n    DockBuilderRemoveNodeDockedWindows(root_id, clear_persistent_docking_references);\n    DockBuilderRemoveNodeChildNodes(root_id);\n}\n\n// This function also acts as a defacto test to make sure we can rebuild from scratch without a glitch\nvoid ImGui::DockContextRebuild(ImGuiContext* ctx)\n{\n    //IMGUI_DEBUG_LOG(\"[docking] full rebuild\\n\");\n    ImGuiDockContext* dc = ctx->DockContext;\n    SaveIniSettingsToMemory();\n    ImGuiID root_id = 0; // Rebuild all\n    DockContextClearNodes(ctx, root_id, false);\n    DockContextBuildNodesFromSettings(ctx, dc->SettingsNodes.Data, dc->SettingsNodes.Size);\n    DockContextBuildAddWindowsToNodes(ctx, root_id);\n}\n\n// Docking context update function, called by NewFrame()\nvoid ImGui::DockContextUpdateUndocking(ImGuiContext* ctx)\n{\n    ImGuiContext& g = *ctx;\n    ImGuiDockContext* dc = ctx->DockContext;\n    if (!(g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable))\n    {\n        if (dc->Nodes.Data.Size > 0 || dc->Requests.Size > 0)\n            DockContextClearNodes(ctx, 0, true);\n        return;\n    }\n\n    // Setting NoSplit at runtime merges all nodes\n    if (g.IO.ConfigDockingNoSplit)\n        for (int n = 0; n < dc->Nodes.Data.Size; n++)\n            if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p)\n                if (node->IsRootNode() && node->IsSplitNode())\n                {\n                    DockBuilderRemoveNodeChildNodes(node->ID);\n                    //dc->WantFullRebuild = true;\n                }\n    \n    // Process full rebuild\n#if 0\n    if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_C)))\n        dc->WantFullRebuild = true;\n#endif\n    if (dc->WantFullRebuild)\n    {\n        DockContextRebuild(ctx);\n        dc->WantFullRebuild = false;\n    }\n\n    // Process Undocking requests (we need to process them _before_ the UpdateMouseMovingWindowNewFrame call in NewFrame)\n    for (int n = 0; n < dc->Requests.Size; n++)\n    {\n        ImGuiDockRequest* req = &dc->Requests[n];\n        if (req->Type == ImGuiDockRequestType_Undock && req->UndockTargetWindow)\n            DockContextProcessUndockWindow(ctx, req->UndockTargetWindow);\n        else if (req->Type == ImGuiDockRequestType_Undock && req->UndockTargetNode)\n            DockContextProcessUndockNode(ctx, req->UndockTargetNode);\n    }\n}\n\n// Docking context update function, called by NewFrame()\nvoid ImGui::DockContextUpdateDocking(ImGuiContext* ctx)\n{\n    ImGuiContext& g = *ctx;\n    ImGuiDockContext* dc = ctx->DockContext;\n    if (!(g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable))\n        return;\n\n    // Process Docking requests\n    for (int n = 0; n < dc->Requests.Size; n++)\n        if (dc->Requests[n].Type == ImGuiDockRequestType_Dock)\n            DockContextProcessDock(ctx, &dc->Requests[n]);\n    dc->Requests.resize(0);\n\n    // Create windows for each automatic docking nodes\n    // We can have NULL pointers when we delete nodes, but because ID are recycled this should amortize nicely (and our node count will never be very high)\n    for (int n = 0; n < dc->Nodes.Data.Size; n++)\n        if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p)\n            if (node->IsRootNode() && !node->IsDockSpace())\n                DockNodeUpdate(node);\n}\n\nstatic ImGuiDockNode* ImGui::DockContextFindNodeByID(ImGuiContext* ctx, ImGuiID id)\n{\n    return (ImGuiDockNode*)ctx->DockContext->Nodes.GetVoidPtr(id);\n}\n\nstatic ImGuiID ImGui::DockContextGenNodeID(ImGuiContext* ctx)\n{\n    // Generate an ID for new node (the exact ID value doesn't matter as long as it is not already used)\n    // FIXME-OPT FIXME-DOCKING: This is suboptimal, even if the node count is small enough not to be a worry. We should poke in ctx->Nodes to find a suitable ID faster.\n    ImGuiID id = 0x0001;\n    while (DockContextFindNodeByID(ctx, id) != NULL)\n        id++;\n    return id;\n}\n\nstatic ImGuiDockNode* ImGui::DockContextAddNode(ImGuiContext* ctx, ImGuiID id)\n{\n    // Generate an ID for the new node (the exact ID value doesn't matter as long as it is not already used) and add the first window.\n    if (id == 0)\n        id = DockContextGenNodeID(ctx);\n    else\n        IM_ASSERT(DockContextFindNodeByID(ctx, id) == NULL);\n    ImGuiDockNode* node = IM_NEW(ImGuiDockNode)(id);\n    ctx->DockContext->Nodes.SetVoidPtr(node->ID, node);\n    return node;\n}\n\nstatic void ImGui::DockContextRemoveNode(ImGuiContext* ctx, ImGuiDockNode* node, bool merge_sibling_into_parent_node)\n{\n    ImGuiContext& g = *ctx;\n    ImGuiDockContext* dc = ctx->DockContext;\n\n    //printf(\"[%05d] RemoveNode 0x%04X\\n\", node->ID);\n    IM_ASSERT(DockContextFindNodeByID(ctx, node->ID) == node);\n    IM_ASSERT(node->ChildNodes[0] == NULL && node->ChildNodes[1] == NULL);\n    IM_ASSERT(node->Windows.Size == 0);\n\n    if (node->HostWindow)\n        node->HostWindow->DockNodeAsHost = NULL;\n\n    ImGuiDockNode* parent_node = node->ParentNode;\n    const bool merge = (merge_sibling_into_parent_node && parent_node != NULL);\n    if (merge)\n    {\n        IM_ASSERT(parent_node->ChildNodes[0] == node || parent_node->ChildNodes[1] == node);\n        ImGuiDockNode* sibling_node = (parent_node->ChildNodes[0] == node ? parent_node->ChildNodes[1] : parent_node->ChildNodes[0]);\n        DockNodeTreeMerge(&g, parent_node, sibling_node);\n    }\n    else\n    {\n        for (int n = 0; parent_node && n < IM_ARRAYSIZE(parent_node->ChildNodes); n++)\n            if (parent_node->ChildNodes[n] == node)\n                node->ParentNode->ChildNodes[n] = NULL;\n        dc->Nodes.SetVoidPtr(node->ID, NULL);\n        IM_DELETE(node);\n    }\n}\n\nstatic int IMGUI_CDECL DockNodeComparerDepthMostFirst(const void* lhs, const void* rhs)\n{\n    const ImGuiDockNode* a = *(const ImGuiDockNode* const*)lhs;\n    const ImGuiDockNode* b = *(const ImGuiDockNode* const*)rhs;\n    return ImGui::DockNodeGetDepth(b) - ImGui::DockNodeGetDepth(a);\n}\n\n// Pre C++0x doesn't allow us to use a function-local type (without linkage) as template parameter, so we moved this here.\nstruct ImGuiDockContextPruneNodeData\n{\n    int         CountWindows, CountChildWindows, CountChildNodes;\n    ImGuiID     RootID;\n    ImGuiDockContextPruneNodeData() { CountWindows = CountChildWindows = CountChildNodes = 0; RootID = 0; }\n};\n\n// Garbage collect unused nodes (run once at init time)\nstatic void ImGui::DockContextPruneUnusedSettingsNodes(ImGuiContext* ctx)\n{\n    ImGuiContext& g = *ctx;\n    ImGuiDockContext* dc = ctx->DockContext;\n    IM_ASSERT(g.Windows.Size == 0);\n\n    ImPool<ImGuiDockContextPruneNodeData> pool;\n    pool.Reserve(dc->SettingsNodes.Size);\n\n    // Count child nodes and compute RootID\n    for (int settings_n = 0; settings_n < dc->SettingsNodes.Size; settings_n++)\n    {\n        ImGuiDockNodeSettings* settings = &dc->SettingsNodes[settings_n];\n        ImGuiDockContextPruneNodeData* parent_data = settings->ParentID ? pool.GetByKey(settings->ParentID) : 0;\n        pool.GetOrAddByKey(settings->ID)->RootID = parent_data ? parent_data->RootID : settings->ID;\n        if (settings->ParentID)\n            pool.GetOrAddByKey(settings->ParentID)->CountChildNodes++;\n    }\n\n    // Count reference to dock ids from window settings\n    for (int settings_n = 0; settings_n < g.SettingsWindows.Size; settings_n++)\n        if (ImGuiID dock_id = g.SettingsWindows[settings_n].DockId)\n            if (ImGuiDockContextPruneNodeData* data = pool.GetByKey(dock_id))\n            {\n                ImGuiDockContextPruneNodeData* data_root = (data->RootID == dock_id) ? data : pool.GetByKey(data->RootID);\n                data->CountWindows++;\n                data_root->CountChildWindows++;\n            }\n\n    // Prune\n    for (int settings_n = 0; settings_n < dc->SettingsNodes.Size; settings_n++)\n    {\n        ImGuiDockNodeSettings* settings = &dc->SettingsNodes[settings_n];\n        ImGuiDockContextPruneNodeData* data = pool.GetByKey(settings->ID);\n        if (data->CountWindows > 1)\n            continue;\n        ImGuiDockContextPruneNodeData* data_root = (data->RootID == settings->ID) ? data : pool.GetByKey(data->RootID);\n\n        bool remove = false;\n        remove |= (data->CountWindows == 1 && settings->ParentID == 0 && data->CountChildNodes == 0 && !settings->IsCentralNode);  // Floating root node with only 1 window\n        remove |= (data->CountWindows == 0 && settings->ParentID == 0 && data->CountChildNodes == 0); // Leaf nodes with 0 window\n        remove |= (data_root->CountChildWindows == 0);\n        if (remove)\n        {\n            DockSettingsRemoveNodeReferences(&settings->ID, 1);\n            settings->ID = 0;\n        }\n    }\n}\n\nstatic void ImGui::DockContextBuildNodesFromSettings(ImGuiContext* ctx, ImGuiDockNodeSettings* node_settings_array, int node_settings_count)\n{\n    // Build nodes\n    for (int node_n = 0; node_n < node_settings_count; node_n++)\n    {\n        ImGuiDockNodeSettings* settings = &node_settings_array[node_n];\n        if (settings->ID == 0)\n            continue;\n        ImGuiDockNode* node = DockContextAddNode(ctx, settings->ID);\n        node->ParentNode = settings->ParentID ? DockContextFindNodeByID(ctx, settings->ParentID) : NULL;\n        node->Pos = ImVec2(settings->Pos.x, settings->Pos.y);\n        node->Size = ImVec2(settings->Size.x, settings->Size.y);\n        node->SizeRef = ImVec2(settings->SizeRef.x, settings->SizeRef.y);\n        node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_DockNode;\n        if (node->ParentNode && node->ParentNode->ChildNodes[0] == NULL)\n            node->ParentNode->ChildNodes[0] = node;\n        else if (node->ParentNode && node->ParentNode->ChildNodes[1] == NULL)\n            node->ParentNode->ChildNodes[1] = node;\n        node->SelectedTabID = settings->SelectedTabID;\n        node->SplitAxis = settings->SplitAxis;\n        if (settings->IsDockSpace)\n            node->LocalFlags |= ImGuiDockNodeFlags_DockSpace;\n        if (settings->IsCentralNode)\n            node->LocalFlags |= ImGuiDockNodeFlags_CentralNode;\n        if (settings->IsHiddenTabBar)\n            node->LocalFlags |= ImGuiDockNodeFlags_HiddenTabBar;\n\n        // Bind host window immediately if it already exist (in case of a rebuild)\n        // This is useful as the RootWindowForTitleBarHighlight links necessary to highlight the currently focused node requires node->HostWindow to be set.\n        char host_window_title[20];\n        ImGuiDockNode* root_node = DockNodeGetRootNode(node);\n        node->HostWindow = FindWindowByName(DockNodeGetHostWindowTitle(root_node, host_window_title, IM_ARRAYSIZE(host_window_title)));\n    }\n}\n\nvoid ImGui::DockContextBuildAddWindowsToNodes(ImGuiContext* ctx, ImGuiID root_id)\n{\n    // Rebind all windows to nodes (they can also lazily rebind but we'll have a visible glitch during the first frame)\n    ImGuiContext& g = *ctx;\n    for (int n = 0; n < g.Windows.Size; n++)\n    {\n        ImGuiWindow* window = g.Windows[n];\n        if (window->DockId == 0 || window->LastFrameActive < g.FrameCount - 1)\n            continue;\n        if (window->DockNode != NULL)\n            continue;\n\n        ImGuiDockNode* node = DockContextFindNodeByID(ctx, window->DockId);\n        IM_ASSERT(node != NULL);   // This should have been called after DockContextBuildNodesFromSettings()\n        if (root_id == 0 || DockNodeGetRootNode(node)->ID == root_id)\n            DockNodeAddWindow(node, window, true);\n    }\n}\n\n//-----------------------------------------------------------------------------\n// Docking: ImGuiDockContext Docking/Undocking functions\n//-----------------------------------------------------------------------------\n\nvoid ImGui::DockContextQueueDock(ImGuiContext* ctx, ImGuiWindow* target, ImGuiDockNode* target_node, ImGuiWindow* payload, ImGuiDir split_dir, float split_ratio, bool split_outer)\n{\n    IM_ASSERT(target != payload);\n    ImGuiDockRequest req;\n    req.Type = ImGuiDockRequestType_Dock;\n    req.DockTargetWindow = target;\n    req.DockTargetNode = target_node;\n    req.DockPayload = payload;\n    req.DockSplitDir = split_dir;\n    req.DockSplitRatio = split_ratio;\n    req.DockSplitOuter = split_outer;\n    ctx->DockContext->Requests.push_back(req);\n}\n\nvoid ImGui::DockContextQueueUndockWindow(ImGuiContext* ctx, ImGuiWindow* window)\n{\n    ImGuiDockRequest req;\n    req.Type = ImGuiDockRequestType_Undock;\n    req.UndockTargetWindow = window;\n    ctx->DockContext->Requests.push_back(req);\n}\n\nvoid ImGui::DockContextQueueUndockNode(ImGuiContext* ctx, ImGuiDockNode* node)\n{\n    ImGuiDockRequest req;\n    req.Type = ImGuiDockRequestType_Undock;\n    req.UndockTargetNode = node;\n    ctx->DockContext->Requests.push_back(req);\n}\n\nvoid ImGui::DockContextQueueNotifyRemovedNode(ImGuiContext* ctx, ImGuiDockNode* node)\n{\n    ImGuiDockContext* dc = ctx->DockContext;\n    for (int n = 0; n < dc->Requests.Size; n++)\n        if (dc->Requests[n].DockTargetNode == node)\n            dc->Requests[n].Type = ImGuiDockRequestType_None;\n}\n\nvoid ImGui::DockContextProcessDock(ImGuiContext* ctx, ImGuiDockRequest* req)\n{\n    IM_ASSERT((req->Type == ImGuiDockRequestType_Dock && req->DockPayload != NULL) || (req->Type == ImGuiDockRequestType_Split && req->DockPayload == NULL));\n    IM_ASSERT(req->DockTargetWindow != NULL || req->DockTargetNode != NULL);\n\n    ImGuiContext& g = *ctx;\n    ImGuiWindow* payload_window = req->DockPayload;     // Optional\n    ImGuiWindow* target_window = req->DockTargetWindow;\n    ImGuiDockNode* node = req->DockTargetNode;\n\n    // Decide which Tab will be selected at the end of the operation\n    ImGuiID next_selected_id = 0;\n    ImGuiDockNode* payload_node = NULL;\n    if (payload_window)\n    {\n        payload_node = payload_window->DockNodeAsHost;\n        payload_window->DockNodeAsHost = NULL; // Important to clear this as the node will have its life as a child which might be merged/deleted later.\n        if (payload_node && payload_node->IsLeafNode())\n            next_selected_id = payload_node->TabBar->NextSelectedTabId ? payload_node->TabBar->NextSelectedTabId : payload_node->TabBar->SelectedTabId;\n        if (payload_node == NULL)\n            next_selected_id = payload_window->ID;\n    }\n\n    // FIXME-DOCK: When we are trying to dock an existing single-window node into a loose window, transfer Node ID as well\n    // When processing an interactive split, usually LastFrameAlive will be < g.FrameCount. But DockBuilder operations can make it ==.\n    if (node)\n        IM_ASSERT(node->LastFrameAlive <= g.FrameCount);\n    if (node && target_window && node == target_window->DockNodeAsHost)\n        IM_ASSERT(node->Windows.Size > 0 || node->IsSplitNode() || node->IsCentralNode());\n\n    // Create new node and add existing window to it\n    if (node == NULL)\n    {\n        node = DockContextAddNode(ctx, 0);\n        node->Pos = target_window->Pos;\n        node->Size = target_window->Size;\n        if (target_window->DockNodeAsHost == NULL)\n        {\n            DockNodeAddWindow(node, target_window, true);\n            node->TabBar->Tabs[0].Flags &= ~ImGuiTabItemFlags_Unsorted;\n            target_window->DockIsActive = true;\n        }\n    }\n\n    ImGuiDir split_dir = req->DockSplitDir;\n    if (split_dir != ImGuiDir_None)\n    {\n        // Split into one, one side will be our payload node unless we are dropping a loose window\n        const ImGuiAxis split_axis = (split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Right) ? ImGuiAxis_X : ImGuiAxis_Y;\n        const int split_inheritor_child_idx = (split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Up) ? 1 : 0; // Current contents will be moved to the opposite side\n        const float split_ratio = req->DockSplitRatio;\n        DockNodeTreeSplit(ctx, node, split_axis, split_inheritor_child_idx, split_ratio, payload_node);  // payload_node may be NULL here!\n        ImGuiDockNode* new_node = node->ChildNodes[split_inheritor_child_idx ^ 1];\n        new_node->HostWindow = node->HostWindow;\n        node = new_node;\n    }\n    node->LocalFlags &= ~ImGuiDockNodeFlags_HiddenTabBar;\n\n    if (node != payload_node)\n    {\n        // Create tab bar before we call DockNodeMoveWindows (which would attempt to move the old tab-bar, which would lead us to payload tabs wrongly appearing before target tabs!)\n        if (node->Windows.Size > 0 && node->TabBar == NULL)\n        {\n            DockNodeAddTabBar(node);\n            for (int n = 0; n < node->Windows.Size; n++)\n                TabBarAddTab(node->TabBar, ImGuiTabItemFlags_None, node->Windows[n]);\n        }\n\n        if (payload_node != NULL)\n        {\n            // Transfer full payload node (with 1+ child windows or child nodes)\n            if (payload_node->IsSplitNode())\n            {\n                if (node->Windows.Size > 0)\n                {\n                    // We can dock a split payload into a node that already has windows _only_ if our payload is a node tree with a single visible node.\n                    // In this situation, we move the windows of the target node into the currently visible node of the payload.\n                    // This allows us to preserve some of the underlying dock tree settings nicely.\n                    IM_ASSERT(payload_node->OnlyNodeWithWindows != NULL); // The docking should have been blocked by DockNodePreviewDockCalc() early on and never submitted.\n                    ImGuiDockNode* visible_node = payload_node->OnlyNodeWithWindows;\n                    if (visible_node->TabBar)\n                        IM_ASSERT(visible_node->TabBar->Tabs.Size > 0);\n                    DockNodeMoveWindows(node, visible_node);\n                    DockNodeMoveWindows(visible_node, node);\n                    DockSettingsRenameNodeReferences(node->ID, visible_node->ID);\n                }\n                if (node->IsCentralNode())\n                {\n                    // Central node property needs to be moved to a leaf node, pick the last focused one.\n                    // FIXME-DOCKING: If we had to transfer other flags here, what would the policy be?\n                    ImGuiDockNode* last_focused_node = DockContextFindNodeByID(ctx, payload_node->LastFocusedNodeID);\n                    ImGuiDockNode* last_focused_root_node = DockNodeGetRootNode(last_focused_node);\n                    IM_ASSERT(last_focused_node != NULL && last_focused_root_node == DockNodeGetRootNode(payload_node));\n                    last_focused_node->LocalFlags |= ImGuiDockNodeFlags_CentralNode;\n                    node->LocalFlags &= ~ImGuiDockNodeFlags_CentralNode;\n                    last_focused_root_node->CentralNode = last_focused_node;\n                }\n\n                IM_ASSERT(node->Windows.Size == 0);\n                DockNodeMoveChildNodes(node, payload_node);\n            }\n            else\n            {\n                const ImGuiID payload_dock_id = payload_node->ID;\n                DockNodeMoveWindows(node, payload_node);\n                DockSettingsRenameNodeReferences(payload_dock_id, node->ID);\n            }\n            DockContextRemoveNode(ctx, payload_node, true);\n        }\n        else if (payload_window)\n        {\n            // Transfer single window\n            const ImGuiID payload_dock_id = payload_window->DockId;\n            node->VisibleWindow = payload_window;\n            DockNodeAddWindow(node, payload_window, true);\n            if (payload_dock_id != 0)\n                DockSettingsRenameNodeReferences(payload_dock_id, node->ID);\n        }\n    }\n\n    // Update selection immediately\n    if (ImGuiTabBar* tab_bar = node->TabBar)\n        tab_bar->NextSelectedTabId = next_selected_id;\n    MarkIniSettingsDirty();\n}\n\nvoid ImGui::DockContextProcessUndockWindow(ImGuiContext* ctx, ImGuiWindow* window, bool clear_persistent_docking_ref)\n{\n    (void)ctx;\n    if (window->DockNode)\n        DockNodeRemoveWindow(window->DockNode, window, clear_persistent_docking_ref ? 0 : window->DockId);\n    else\n        window->DockId = 0;\n    window->Collapsed = false;\n    window->DockIsActive = false;\n    window->DockTabIsVisible = false;\n    MarkIniSettingsDirty();\n}\n\nvoid ImGui::DockContextProcessUndockNode(ImGuiContext* ctx, ImGuiDockNode* node)\n{\n    IM_ASSERT(node->IsLeafNode());\n    IM_ASSERT(node->Windows.Size >= 1);\n\n    if (node->IsRootNode() || node->IsCentralNode())\n    {\n        // In the case of a root node or central node, the node will have to stay in place. Create a new node to receive the payload.\n        ImGuiDockNode* new_node = DockContextAddNode(ctx, 0);\n        DockNodeMoveWindows(new_node, node);\n        DockSettingsRenameNodeReferences(node->ID, new_node->ID);\n        for (int n = 0; n < new_node->Windows.Size; n++)\n            UpdateWindowParentAndRootLinks(new_node->Windows[n], new_node->Windows[n]->Flags, NULL);\n        new_node->WantMouseMove = true;\n    }\n    else\n    {\n        // Otherwise delete the previous node by merging the other sibling back into the parent node.\n        IM_ASSERT(node->ParentNode->ChildNodes[0] == node || node->ParentNode->ChildNodes[1] == node);\n        int index_in_parent = (node->ParentNode->ChildNodes[0] == node) ? 0 : 1;\n        node->ParentNode->ChildNodes[index_in_parent] = NULL;\n        DockNodeTreeMerge(ctx, node->ParentNode, node->ParentNode->ChildNodes[index_in_parent ^ 1]);\n        node->ParentNode->AuthorityForViewport = ImGuiDataAuthority_Window; // The node that stays in place keeps the viewport, so our newly dragged out node will create a new viewport\n        node->ParentNode = NULL;\n        node->AuthorityForPos = node->AuthorityForSize = ImGuiDataAuthority_Window;\n        node->WantMouseMove = true;\n    }\n    MarkIniSettingsDirty();\n}\n\n// This is mostly used for automation.\nbool ImGui::DockContextCalcDropPosForDocking(ImGuiWindow* target, ImGuiDockNode* target_node, ImGuiWindow* payload, ImGuiDir split_dir, bool split_outer, ImVec2* out_pos)\n{\n    if (split_outer)\n    {\n        IM_ASSERT(0);\n    }\n    else\n    {\n        ImGuiDockPreviewData split_data;\n        DockNodePreviewDockCalc(target, target_node, payload, &split_data, false, split_outer);\n        if (split_data.DropRectsDraw[split_dir+1].IsInverted())\n            return false;\n        *out_pos = split_data.DropRectsDraw[split_dir+1].GetCenter();\n        return true;\n    }\n    return false;\n}\n\n//-----------------------------------------------------------------------------\n// Docking: ImGuiDockNode\n//-----------------------------------------------------------------------------\n\nImGuiDockNode::ImGuiDockNode(ImGuiID id)\n{\n    ID = id;\n    SharedFlags = LocalFlags = ImGuiDockNodeFlags_None;\n    ParentNode = ChildNodes[0] = ChildNodes[1] = NULL;\n    TabBar = NULL;\n    SplitAxis = ImGuiAxis_None;\n\n    HostWindow = VisibleWindow = NULL;\n    CentralNode = OnlyNodeWithWindows = NULL;\n    LastFrameAlive = LastFrameActive = LastFrameFocused = -1;\n    LastFocusedNodeID = 0;\n    SelectedTabID = 0;\n    WantCloseTabID = 0;\n    AuthorityForPos = AuthorityForSize = ImGuiDataAuthority_DockNode;\n    AuthorityForViewport = ImGuiDataAuthority_Auto;\n    IsVisible = true;\n    IsFocused = HasCloseButton = HasCollapseButton = false;\n    WantCloseAll = WantLockSizeOnce = WantMouseMove = WantHiddenTabBarUpdate = WantHiddenTabBarToggle = false;\n}\n\nImGuiDockNode::~ImGuiDockNode()\n{\n    IM_DELETE(TabBar);\n    TabBar = NULL;\n    ChildNodes[0] = ChildNodes[1] = NULL;\n}\n\nint ImGui::DockNodeGetTabOrder(ImGuiWindow* window)\n{\n    ImGuiTabBar* tab_bar = window->DockNode->TabBar;\n    if (tab_bar == NULL)\n        return -1;\n    ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, window->ID);\n    return tab ? tab_bar->GetTabOrder(tab) : -1;\n}\n\nstatic void ImGui::DockNodeAddWindow(ImGuiDockNode* node, ImGuiWindow* window, bool add_to_tab_bar)\n{\n    ImGuiContext& g = *GImGui; (void)g;\n    if (window->DockNode)\n    {\n        // Can overwrite an existing window->DockNode (e.g. pointing to a disabled DockSpace node)\n        IM_ASSERT(window->DockNode->ID != node->ID);\n        DockNodeRemoveWindow(window->DockNode, window, 0);\n    }\n    IM_ASSERT(window->DockNode == NULL || window->DockNodeAsHost == NULL);\n\n    node->Windows.push_back(window);\n    node->WantHiddenTabBarUpdate = true;\n    window->DockNode = node;\n    window->DockId = node->ID;\n    window->DockIsActive = (node->Windows.Size > 1);\n    window->DockTabWantClose = false;\n\n    // If 2+ windows appeared on the same frame, creating a new DockNode+TabBar from the second window, \n    // then we need to hide the first one after the fact otherwise it would be visible as a standalone window for one frame.\n    if (node->HostWindow == NULL && node->Windows.Size == 2 && node->Windows[0]->WasActive == false)\n    {\n        node->Windows[0]->Hidden = true;\n        node->Windows[0]->HiddenFramesCanSkipItems = 1;\n    }\n\n    // When reactivating a node with one or two loose window, the window pos/size/viewport are authoritative over the node storage.\n    // In particular it is important we init the viewport from the first window so we don't create two viewports and drop one.\n    if (node->HostWindow == NULL && !node->IsDockSpace() && node->IsRootNode())\n    {\n        if (node->AuthorityForPos == ImGuiDataAuthority_Auto)\n            node->AuthorityForPos = ImGuiDataAuthority_Window;\n        if (node->AuthorityForSize == ImGuiDataAuthority_Auto)\n            node->AuthorityForSize = ImGuiDataAuthority_Window;\n        if (node->AuthorityForViewport == ImGuiDataAuthority_Auto)\n            node->AuthorityForViewport = ImGuiDataAuthority_Window;\n    }\n\n    // Add to tab bar if requested\n    if (add_to_tab_bar)\n    {\n        if (node->TabBar == NULL)\n        {\n            DockNodeAddTabBar(node);\n            node->TabBar->SelectedTabId = node->TabBar->NextSelectedTabId = node->SelectedTabID;\n            \n            // Add existing windows\n            for (int n = 0; n < node->Windows.Size - 1; n++)\n                TabBarAddTab(node->TabBar, ImGuiTabItemFlags_None, node->Windows[n]);\n        }\n        TabBarAddTab(node->TabBar, ImGuiTabItemFlags_Unsorted, window);\n    }\n\n    DockNodeUpdateVisibleFlag(node);\n\n    // Update this without waiting for the next time we Begin() in the window, so our host window will have the proper title bar color on its first frame.\n    if (node->HostWindow)\n        UpdateWindowParentAndRootLinks(window, window->Flags | ImGuiWindowFlags_ChildWindow, node->HostWindow);\n}\n\nstatic void ImGui::DockNodeRemoveWindow(ImGuiDockNode* node, ImGuiWindow* window, ImGuiID save_dock_id)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(window->DockNode == node);\n    //IM_ASSERT(window->RootWindow == node->HostWindow);\n    //IM_ASSERT(window->LastFrameActive < g.FrameCount);    // We may call this from Begin()\n    IM_ASSERT(save_dock_id == 0 || save_dock_id == node->ID);\n\n    window->DockNode = NULL;\n    window->DockIsActive = window->DockTabWantClose = false;\n    window->DockId = save_dock_id;\n    UpdateWindowParentAndRootLinks(window, window->Flags & ~ImGuiWindowFlags_ChildWindow, NULL); // Update immediately\n\n    // Remove window\n    bool erased = false;\n    for (int n = 0; n < node->Windows.Size; n++)\n        if (node->Windows[n] == window)\n        {\n            node->Windows.erase(node->Windows.Data + n);\n            erased = true;\n            break;\n        }\n    IM_ASSERT(erased);\n    if (node->VisibleWindow == window)\n        node->VisibleWindow = NULL;\n\n    // Remove tab and possibly tab bar\n    node->WantHiddenTabBarUpdate = true;\n    if (node->TabBar)\n    {\n        TabBarRemoveTab(node->TabBar, window->ID);\n        const int tab_count_threshold_for_tab_bar = node->IsCentralNode() ? 1 : 2;\n        if (node->Windows.Size < tab_count_threshold_for_tab_bar)\n            DockNodeRemoveTabBar(node);\n    }\n\n    if (node->Windows.Size == 0 && !node->IsCentralNode() && !node->IsDockSpace() && window->DockId != node->ID)\n    {\n        // Automatic dock node delete themselves if they are not holding at least one tab\n        DockContextRemoveNode(&g, node, true);\n        return;\n    }\n\n    if (node->Windows.Size == 1 && !node->IsCentralNode() && node->HostWindow)\n    {\n        ImGuiWindow* remaining_window = node->Windows[0];\n        if (node->HostWindow->ViewportOwned && node->IsRootNode())\n        {\n            // Transfer viewport back to the remaining loose window\n            IM_ASSERT(node->HostWindow->Viewport->Window == node->HostWindow);\n            node->HostWindow->Viewport->Window = remaining_window;\n            node->HostWindow->Viewport->ID = remaining_window->ID;\n        }\n        remaining_window->Collapsed = node->HostWindow->Collapsed;\n    }\n\n    // Update visibility immediately is required so the DockNodeUpdateRemoveInactiveChilds() processing can reflect changes up the tree\n    DockNodeUpdateVisibleFlag(node);\n}\n\nstatic void ImGui::DockNodeMoveChildNodes(ImGuiDockNode* dst_node, ImGuiDockNode* src_node)\n{\n    IM_ASSERT(dst_node->Windows.Size == 0);\n    dst_node->ChildNodes[0] = src_node->ChildNodes[0];\n    dst_node->ChildNodes[1] = src_node->ChildNodes[1];\n    if (dst_node->ChildNodes[0])\n        dst_node->ChildNodes[0]->ParentNode = dst_node;\n    if (dst_node->ChildNodes[1])\n        dst_node->ChildNodes[1]->ParentNode = dst_node;\n    dst_node->SplitAxis = src_node->SplitAxis;\n    dst_node->SizeRef = src_node->SizeRef;\n    src_node->ChildNodes[0] = src_node->ChildNodes[1] = NULL;\n}\n\nstatic void ImGui::DockNodeMoveWindows(ImGuiDockNode* dst_node, ImGuiDockNode* src_node)\n{\n    // Insert tabs in the same orders as currently ordered (node->Windows isn't ordered)\n    IM_ASSERT(src_node && dst_node && dst_node != src_node);\n    ImGuiTabBar* src_tab_bar = src_node->TabBar;\n    if (src_tab_bar != NULL)\n        IM_ASSERT(src_node->Windows.Size == src_node->TabBar->Tabs.Size);\n\n    // If the dst_node is empty we can just move the entire tab bar (to preserve selection, scrolling, etc.)\n    bool move_tab_bar = (src_tab_bar != NULL) && (dst_node->TabBar == NULL);\n    if (move_tab_bar)\n    {\n        dst_node->TabBar = src_node->TabBar;\n        src_node->TabBar = NULL;\n    }\n\n    for (int n = 0; n < src_node->Windows.Size; n++)\n    {\n        ImGuiWindow* window = src_tab_bar ? src_tab_bar->Tabs[n].Window : src_node->Windows[n];\n        window->DockNode = NULL;\n        window->DockIsActive = false;\n        DockNodeAddWindow(dst_node, window, move_tab_bar ? false : true);\n    }\n    src_node->Windows.clear();\n\n    if (!move_tab_bar && src_node->TabBar)\n    {\n        if (dst_node->TabBar)\n            dst_node->TabBar->SelectedTabId = src_node->TabBar->SelectedTabId;\n        DockNodeRemoveTabBar(src_node);\n    }\n}\n\nstatic void ImGui::DockNodeApplyPosSizeToWindows(ImGuiDockNode* node)\n{\n    for (int n = 0; n < node->Windows.Size; n++)\n    {\n        SetWindowPos(node->Windows[n], node->Pos, ImGuiCond_Always); // We don't assign directly to Pos because it can break the calculation of SizeContents on next frame\n        SetWindowSize(node->Windows[n], node->Size, ImGuiCond_Always);\n    }\n}\n\nstatic void ImGui::DockNodeHideHostWindow(ImGuiDockNode* node)\n{\n    if (node->HostWindow)\n    {\n        if (node->HostWindow->DockNodeAsHost == node)\n            node->HostWindow->DockNodeAsHost = NULL;\n        node->HostWindow = NULL;\n    }\n\n    if (node->Windows.Size == 1)\n    {\n        node->VisibleWindow = node->Windows[0];\n        node->Windows[0]->DockIsActive = false;\n    }\n\n    if (node->TabBar)\n        DockNodeRemoveTabBar(node);\n}\n\n// Search function called once by root node in DockNodeUpdate()\nstruct ImGuiDockNodeFindInfoResults\n{\n    ImGuiDockNode*      CentralNode;\n    ImGuiDockNode*      FirstNodeWithWindows;\n    int                 CountNodesWithWindows;\n    //ImGuiWindowClass  WindowClassForMerges;\n\n    ImGuiDockNodeFindInfoResults() { CentralNode = FirstNodeWithWindows = NULL; CountNodesWithWindows = 0; }\n};\n\nstatic void DockNodeFindInfo(ImGuiDockNode* node, ImGuiDockNodeFindInfoResults* results)\n{\n    if (node->Windows.Size > 0)\n    {\n        if (results->FirstNodeWithWindows == NULL)\n            results->FirstNodeWithWindows = node;\n        results->CountNodesWithWindows++;\n    }\n    if (node->IsCentralNode())\n    {\n        IM_ASSERT(results->CentralNode == NULL); // Should be only one\n        IM_ASSERT(node->IsLeafNode() && \"If you get this assert: please submit .ini file + repro of actions leading to this.\");\n        results->CentralNode = node;\n    }\n    if (results->CountNodesWithWindows > 1 && results->CentralNode != NULL)\n        return;\n    if (node->ChildNodes[0])\n        DockNodeFindInfo(node->ChildNodes[0], results);\n    if (node->ChildNodes[1])\n        DockNodeFindInfo(node->ChildNodes[1], results);\n}\n\n// - Remove inactive windows/nodes.\n// - Update visibility flag.\nstatic void ImGui::DockNodeUpdateVisibleFlagAndInactiveChilds(ImGuiDockNode* node)\n{\n    ImGuiContext& g = *GImGui;\n\n    IM_ASSERT(node->ParentNode == NULL || node->ParentNode->ChildNodes[0] == node || node->ParentNode->ChildNodes[1] == node);\n\n    // Inherit most flags\n    if (node->ParentNode)\n        node->SharedFlags = node->ParentNode->SharedFlags & ImGuiDockNodeFlags_SharedFlagsInheritMask_;\n\n    // Recurse into children\n    // There is the possibility that one of our child becoming empty will delete itself and moving its sibling contents into 'node'.\n    // If 'node->ChildNode[0]' delete itself, then 'node->ChildNode[1]->Windows' will be moved into 'node'\n    // If 'node->ChildNode[1]' delete itself, then 'node->ChildNode[0]->Windows' will be moved into 'node' and the \"remove inactive windows\" loop will have run twice on those windows (harmless)\n    if (node->ChildNodes[0])\n        DockNodeUpdateVisibleFlagAndInactiveChilds(node->ChildNodes[0]);\n    if (node->ChildNodes[1])\n        DockNodeUpdateVisibleFlagAndInactiveChilds(node->ChildNodes[1]);\n\n    // Remove inactive windows\n    for (int window_n = 0; window_n < node->Windows.Size; window_n++)\n    {\n        ImGuiWindow* window = node->Windows[window_n];\n        IM_ASSERT(window->DockNode == node);\n\n        bool node_was_active = (node->LastFrameActive + 1 == g.FrameCount);\n        bool remove = false;\n        remove |= node_was_active && (window->LastFrameActive + 1 < g.FrameCount);\n        remove |= node_was_active && (node->WantCloseAll || node->WantCloseTabID == window->ID) && window->HasCloseButton && !(window->Flags & ImGuiWindowFlags_UnsavedDocument);  // Submit all _expected_ closure from last frame\n        remove |= (window->DockTabWantClose);\n        if (!remove)\n            continue;\n        window->DockTabWantClose = false;\n        if (node->Windows.Size == 1 && !node->IsCentralNode())\n        {\n            DockNodeHideHostWindow(node);\n            DockNodeRemoveWindow(node, window, node->ID); // Will delete the node so it'll be invalid on return\n            return;\n        }\n        DockNodeRemoveWindow(node, window, node->ID);\n        window_n--;\n    }\n\n    // Auto-hide tab bar option\n    ImGuiDockNodeFlags node_flags = node->GetMergedFlags();\n    if (node->WantHiddenTabBarUpdate && node->Windows.Size == 1 && (node_flags & ImGuiDockNodeFlags_AutoHideTabBar) && !node->IsHiddenTabBar())\n        node->WantHiddenTabBarToggle = true;\n    node->WantHiddenTabBarUpdate = false;\n\n    // Apply toggles at a single point of the frame (here!)\n    if (node->Windows.Size > 1)\n        node->LocalFlags &= ~ImGuiDockNodeFlags_HiddenTabBar;\n    else if (node->WantHiddenTabBarToggle)\n        node->LocalFlags ^= ImGuiDockNodeFlags_HiddenTabBar;\n    node->WantHiddenTabBarToggle = false;\n\n    DockNodeUpdateVisibleFlag(node);\n}\n\nstatic void ImGui::DockNodeUpdateVisibleFlag(ImGuiDockNode* node)\n{\n    // Update visibility flag\n    bool is_visible = (node->ParentNode == NULL) ? node->IsDockSpace() : node->IsCentralNode();\n    is_visible |= (node->Windows.Size > 0);\n    is_visible |= (node->ChildNodes[0] && node->ChildNodes[0]->IsVisible);\n    is_visible |= (node->ChildNodes[1] && node->ChildNodes[1]->IsVisible);\n    node->IsVisible = is_visible;\n}\n\nstatic void ImGui::DockNodeStartMouseMovingWindow(ImGuiDockNode* node, ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(node->WantMouseMove == true);\n    ImVec2 backup_active_click_offset = g.ActiveIdClickOffset;\n    StartMouseMovingWindow(window);\n    g.MovingWindow = window; // If we are docked into a non moveable root window, StartMouseMovingWindow() won't set g.MovingWindow. Override that decision.\n    node->WantMouseMove = false;\n    g.ActiveIdClickOffset = backup_active_click_offset;\n}\n\nstatic void ImGui::DockNodeUpdate(ImGuiDockNode* node)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(node->LastFrameActive != g.FrameCount);\n    node->LastFrameAlive = g.FrameCount;\n\n    node->CentralNode = node->OnlyNodeWithWindows = NULL;\n    if (node->IsRootNode())\n    {\n        DockNodeUpdateVisibleFlagAndInactiveChilds(node);\n\n        // FIXME-DOCK: Merge this scan into the one above.\n        // - Setup central node pointers\n        // - Find if there's only a single visible window in the hierarchy (in which case we need to display a regular title bar -> FIXME-DOCK: that last part is not done yet!)\n        ImGuiDockNodeFindInfoResults results;\n        DockNodeFindInfo(node, &results);\n        node->CentralNode = results.CentralNode;\n        node->OnlyNodeWithWindows = (results.CountNodesWithWindows == 1) ? results.FirstNodeWithWindows : NULL;\n        if (node->LastFocusedNodeID == 0 && results.FirstNodeWithWindows != NULL)\n            node->LastFocusedNodeID = results.FirstNodeWithWindows->ID;\n\n        // Copy the window class from of our first window so it can be used for proper dock filtering.\n        // When node has mixed windows, prioritize the class with the most constraint (DockingAllowUnclassed = false) as the reference to copy.\n        // FIXME-DOCK: We don't recurse properly, this code could be reworked to work from DockNodeUpdateScanRec.\n        if (ImGuiDockNode* first_node_with_windows = results.FirstNodeWithWindows)\n        {\n            node->WindowClass = first_node_with_windows->Windows[0]->WindowClass;\n            for (int n = 1; n < first_node_with_windows->Windows.Size; n++)\n                if (first_node_with_windows->Windows[n]->WindowClass.DockingAllowUnclassed == false)\n                {\n                    node->WindowClass = first_node_with_windows->Windows[n]->WindowClass;\n                    break;\n                }\n        }\n    }\n\n    // Remove tab bar if not needed\n    if (node->TabBar && node->IsNoTabBar())\n        DockNodeRemoveTabBar(node);\n\n    // Early out for hidden root dock nodes (when all DockId references are in inactive windows, or there is only 1 floating window holding on the DockId)\n    if (node->Windows.Size <= 1 && node->IsRootNode() && node->IsLeafNode() && !node->IsDockSpace() && !g.IO.ConfigDockingTabBarOnSingleWindows)\n    {\n        if (node->Windows.Size == 1)\n        {\n            // Floating window pos/size is authoritative\n            ImGuiWindow* single_window = node->Windows[0];\n            node->Pos = single_window->Pos;\n            node->Size = single_window->SizeFull;\n            node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_Window;\n\n            // Transfer focus immediately so when we revert to a regular window it is immediately selected\n            if (node->HostWindow && g.NavWindow == node->HostWindow)\n                FocusWindow(single_window);\n            if (node->HostWindow)\n            {\n                single_window->Viewport = node->HostWindow->Viewport;\n                single_window->ViewportId = node->HostWindow->ViewportId;\n                if (node->HostWindow->ViewportOwned)\n                {\n                    single_window->Viewport->Window = single_window;\n                    single_window->ViewportOwned = true;\n                }\n            }\n        }\n\n        DockNodeHideHostWindow(node);\n        node->WantCloseAll = false;\n        node->WantCloseTabID = 0;\n        node->HasCloseButton = node->HasCollapseButton = false;\n        node->LastFrameActive = g.FrameCount;\n\n        if (node->WantMouseMove && node->Windows.Size == 1)\n            DockNodeStartMouseMovingWindow(node, node->Windows[0]);\n        return;\n    }\n\n    ImGuiWindow* host_window = NULL;\n    bool beginned_into_host_window = false;\n    if (node->IsDockSpace())\n    {\n        // [Explicit root dockspace node]\n        IM_ASSERT(node->HostWindow);\n        node->HasCloseButton = false;\n        node->HasCollapseButton = true;\n        host_window = node->HostWindow;\n    }\n    else\n    {\n        // [Automatic root or child nodes]\n        node->HasCloseButton = false;\n        node->HasCollapseButton = (node->Windows.Size > 0);\n        for (int window_n = 0; window_n < node->Windows.Size; window_n++)\n        {\n            // FIXME-DOCK: Setting DockIsActive here means that for single active window in a leaf node, DockIsActive will be cleared until the next Begin() call.\n            ImGuiWindow* window = node->Windows[window_n];\n            window->DockIsActive = (node->Windows.Size > 1);\n            node->HasCloseButton |= window->HasCloseButton;\n        }\n\n        if (node->IsRootNode() && node->IsVisible)\n        {\n            ImGuiWindow* ref_window = (node->Windows.Size > 0) ? node->Windows[0] : NULL;\n\n            // Sync Pos\n            if (node->AuthorityForPos == ImGuiDataAuthority_Window && ref_window)\n                SetNextWindowPos(ref_window->Pos);\n            else if (node->AuthorityForPos == ImGuiDataAuthority_DockNode)\n                SetNextWindowPos(node->Pos);\n\n            // Sync Size\n            if (node->AuthorityForSize == ImGuiDataAuthority_Window && ref_window)\n                SetNextWindowSize(ref_window->SizeFull);\n            else if (node->AuthorityForSize == ImGuiDataAuthority_DockNode)\n                SetNextWindowSize(node->Size);\n\n            // Sync Collapsed\n            if (node->AuthorityForSize == ImGuiDataAuthority_Window && ref_window)\n                SetNextWindowCollapsed(ref_window->Collapsed);\n\n            // Sync Viewport\n            if (node->AuthorityForViewport == ImGuiDataAuthority_Window && ref_window)\n                SetNextWindowViewport(ref_window->ViewportId);\n\n            SetNextWindowClass(&node->WindowClass);\n\n            // Begin into the host window\n            char window_label[20];\n            DockNodeGetHostWindowTitle(node, window_label, IM_ARRAYSIZE(window_label));\n            ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_DockNodeHost;\n            window_flags |= ImGuiWindowFlags_NoFocusOnAppearing;\n            window_flags |= ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoCollapse;\n            window_flags |= ImGuiWindowFlags_NoTitleBar;\n\n            PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));\n            Begin(window_label, NULL, window_flags);\n            PopStyleVar();\n            beginned_into_host_window = true;\n\n            node->HostWindow = host_window = g.CurrentWindow;\n            host_window->DockNodeAsHost = node;\n            host_window->DC.CursorPos = host_window->Pos;\n            node->Pos = host_window->Pos;\n            node->Size = host_window->Size;\n\n            // We set ImGuiWindowFlags_NoFocusOnAppearing because we don't want the host window to take full focus (e.g. steal NavWindow)\n            // But we still it bring it to the front of display. There's no way to choose this precise behavior via window flags.\n            // One simple case to ponder if: window A has a toggle to create windows B/C/D. Dock B/C/D together, clear the toggle and enable it again.\n            // When reappearing B/C/D will request focus and be moved to the top of the display pile, but they are not linked to the dock host window \n            // during the frame they appear. The dock host window would keep its old display order, and the sorting in EndFrame would move B/C/D back\n            // after the dock host window, losing their top-most status.\n            if (node->HostWindow->Appearing)\n                BringWindowToDisplayFront(node->HostWindow);\n\n            node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_Auto;\n        }\n        else if (node->ParentNode)\n        {\n            node->HostWindow = host_window = node->ParentNode->HostWindow;\n            node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_Auto;\n        }\n        if (node->WantMouseMove && node->HostWindow)\n            DockNodeStartMouseMovingWindow(node, node->HostWindow);\n    }\n\n    // Update focused node (the one whose title bar is highlight) within a node tree\n    if (node->IsSplitNode())\n        IM_ASSERT(node->TabBar == NULL);\n    if (node->IsRootNode())\n        if (g.NavWindow && g.NavWindow->RootWindowDockStop->DockNode && g.NavWindow->RootWindowDockStop->ParentWindow == host_window)\n            node->LastFocusedNodeID = g.NavWindow->RootWindowDockStop->DockNode->ID;\n\n    // We need to draw a background at the root level if requested by ImGuiDockNodeFlags_PassthruCentralNode, but we will only know the correct pos/size after\n    // processing the resizing splitters. So we are using the DrawList channel splitting facility to submit drawing primitives out of order!\n    const ImGuiDockNodeFlags node_flags = node->GetMergedFlags();\n    const bool render_dockspace_bg = node->IsRootNode() && host_window && (node_flags & ImGuiDockNodeFlags_PassthruCentralNode) != 0;\n    if (render_dockspace_bg)\n    {\n        host_window->DrawList->ChannelsSplit(2);\n        host_window->DrawList->ChannelsSetCurrent(1);\n    }\n\n    // Register a hit-test hole in the window unless we are currently dragging a window that is compatible with our dockspace\n    const ImGuiDockNode* central_node = node->CentralNode;\n    const bool central_node_hole = node->IsRootNode() && host_window && (node_flags & ImGuiDockNodeFlags_PassthruCentralNode) != 0 && central_node != NULL && central_node->IsEmpty();\n    bool central_node_hole_register_hit_test_hole = central_node_hole;\n    if (central_node_hole)\n        if (const ImGuiPayload* payload = ImGui::GetDragDropPayload())\n            if (payload->IsDataType(IMGUI_PAYLOAD_TYPE_WINDOW) && DockNodeIsDropAllowed(host_window, *(ImGuiWindow**)payload->Data))\n                central_node_hole_register_hit_test_hole = false;\n    if (central_node_hole_register_hit_test_hole)\n    {\n        // Add a little padding to match the \"resize from edges\" behavior and allow grabbing the splitter easily.\n        IM_ASSERT(node->IsDockSpace()); // We cannot pass this flag without the DockSpace() api. Testing this because we also setup the hole in host_window->ParentNode\n        ImRect central_hole(central_node->Pos, central_node->Pos + central_node->Size);\n        central_hole.Expand(ImVec2(-WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS, -WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS));\n        if (central_node_hole && !central_hole.IsInverted())\n        {\n            SetWindowHitTestHole(host_window, central_hole.Min, central_hole.Max - central_hole.Min);\n            SetWindowHitTestHole(host_window->ParentWindow, central_hole.Min, central_hole.Max - central_hole.Min);\n        }\n    }\n\n    // Update position/size, process and draw resizing splitters\n    if (node->IsRootNode() && host_window)\n    {\n        DockNodeTreeUpdatePosSize(node, host_window->Pos, host_window->Size);\n        DockNodeTreeUpdateSplitter(node);\n    }\n\n    // Draw empty node background (currently can only be the Central Node)\n    if (host_window && node->IsEmpty() && node->IsVisible && !(node_flags & ImGuiDockNodeFlags_PassthruCentralNode))\n        host_window->DrawList->AddRectFilled(node->Pos, node->Pos + node->Size, GetColorU32(ImGuiCol_DockingEmptyBg));\n\n    // Draw whole dockspace background if ImGuiDockNodeFlags_PassthruCentralNode if set.\n    if (render_dockspace_bg && node->IsVisible)\n    {\n        host_window->DrawList->ChannelsSetCurrent(0);\n        if (central_node_hole)\n            RenderRectFilledWithHole(host_window->DrawList, node->Rect(), central_node->Rect(), GetColorU32(ImGuiCol_WindowBg), 0.0f);\n        else\n            host_window->DrawList->AddRectFilled(node->Pos, node->Pos + node->Size, GetColorU32(ImGuiCol_WindowBg), 0.0f);\n        host_window->DrawList->ChannelsMerge();\n    }\n\n    // Draw and populate Tab Bar\n    if (host_window && node->Windows.Size > 0)\n    {\n        DockNodeUpdateTabBar(node, host_window);\n    }\n    else\n    {\n        node->WantCloseAll = false;\n        node->WantCloseTabID = 0;\n        node->IsFocused = false;\n    }\n    if (node->TabBar && node->TabBar->SelectedTabId)\n        node->SelectedTabID = node->TabBar->SelectedTabId;\n    else if (node->Windows.Size > 0)\n        node->SelectedTabID = node->Windows[0]->ID;\n\n    // Draw payload drop target\n    if (host_window && node->IsVisible)\n        if (node->IsRootNode() && (g.MovingWindow == NULL || g.MovingWindow->RootWindow != host_window))\n            BeginAsDockableDragDropTarget(host_window);\n\n    node->LastFrameActive = g.FrameCount;\n\n    // Recurse into children\n    if (host_window)\n    {\n        if (node->ChildNodes[0])\n            DockNodeUpdate(node->ChildNodes[0]);\n        if (node->ChildNodes[1])\n            DockNodeUpdate(node->ChildNodes[1]);\n\n        // Render outer borders last (after the tab bar)\n        if (node->IsRootNode())\n            RenderOuterBorders(host_window);\n    }\n\n    // End host window\n    if (beginned_into_host_window) //-V1020\n        End();\n}\n\n// Compare TabItem nodes given the last known DockOrder (will persist in .ini file as hint), used to sort tabs when multiple tabs are added on the same frame.\nstatic int IMGUI_CDECL TabItemComparerByDockOrder(const void* lhs, const void* rhs)\n{\n    ImGuiWindow* a = ((const ImGuiTabItem*)lhs)->Window;\n    ImGuiWindow* b = ((const ImGuiTabItem*)rhs)->Window;\n    if (int d = ((a->DockOrder == -1) ? INT_MAX : a->DockOrder) - ((b->DockOrder == -1) ? INT_MAX : b->DockOrder))\n        return d;\n    return (a->BeginOrderWithinContext - b->BeginOrderWithinContext);\n}\n\nstatic ImGuiID ImGui::DockNodeUpdateTabListMenu(ImGuiDockNode* node, ImGuiTabBar* tab_bar)\n{\n    // Try to position the menu so it is more likely to stays within the same viewport\n    ImGuiID ret_tab_id = 0;\n    SetNextWindowPos(ImVec2(node->Pos.x, node->Pos.y + GetFrameHeight()));\n    if (BeginPopup(\"#TabListMenu\"))\n    {\n        node->IsFocused = true;\n        if (tab_bar->Tabs.Size == 1)\n        {\n            if (MenuItem(\"Hide tab bar\", NULL, node->IsHiddenTabBar()))\n                node->WantHiddenTabBarToggle = true;\n        }\n        else\n        {\n            for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++)\n            {\n                ImGuiTabItem* tab = &tab_bar->Tabs[tab_n];\n                IM_ASSERT(tab->Window != NULL);\n                if (Selectable(tab->Window->Name, tab->ID == tab_bar->SelectedTabId))\n                    ret_tab_id = tab->ID;\n                SameLine();\n                Text(\"   \");\n            }\n        }\n        EndPopup();\n    }\n    return ret_tab_id;\n}\n\nstatic void ImGui::DockNodeUpdateTabBar(ImGuiDockNode* node, ImGuiWindow* host_window)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiStyle& style = g.Style;\n\n    const bool node_was_active = (node->LastFrameActive + 1 == g.FrameCount);\n    const bool closed_all = node->WantCloseAll && node_was_active;\n    const ImGuiID closed_one = node->WantCloseTabID && node_was_active;\n    node->WantCloseAll = false;\n    node->WantCloseTabID = 0;\n\n    // Decide if we should use a focused title bar color\n    bool is_focused = false;\n    ImGuiDockNode* root_node = DockNodeGetRootNode(node);\n    if (g.NavWindowingTarget)\n        is_focused = (g.NavWindowingTarget->DockNode == node);\n    else if (g.NavWindow && g.NavWindow->RootWindowForTitleBarHighlight == host_window->RootWindow && root_node->LastFocusedNodeID == node->ID)\n        is_focused = true;\n\n    // Hidden tab bar will show a triangle on the upper-left (in Begin)\n    if (node->IsHiddenTabBar() || node->IsNoTabBar())\n    {\n        node->VisibleWindow = (node->Windows.Size > 0) ? node->Windows[0] : NULL;\n        node->IsFocused = is_focused;\n        if (is_focused)\n            node->LastFrameFocused = g.FrameCount;\n        if (node->VisibleWindow)\n        {\n            // Notify root of visible window (used to display title in OS task bar)\n            if (is_focused || root_node->VisibleWindow == NULL)\n                root_node->VisibleWindow = node->VisibleWindow;\n            if (node->TabBar)\n                node->TabBar->VisibleTabId = node->VisibleWindow->ID;\n        }\n        return;\n    }\n\n    // Move ourselves to the Menu layer (so we can be accessed by tapping Alt) + undo SkipItems flag in order to draw over the title bar even if the window is collapsed\n    bool backup_skip_item = host_window->SkipItems;\n    if (!node->IsDockSpace())\n    {\n        host_window->SkipItems = false;\n        host_window->DC.NavLayerCurrent = ImGuiNavLayer_Menu;\n        host_window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu);\n    }\n\n    PushID(node->ID);\n    ImGuiTabBar* tab_bar = node->TabBar;\n    bool tab_bar_is_recreated = (tab_bar == NULL); // Tab bar are automatically destroyed when a node gets hidden\n    if (tab_bar == NULL)\n    {\n        DockNodeAddTabBar(node);\n        tab_bar = node->TabBar;\n    }\n\n    ImGuiID focus_tab_id = 0;\n    node->IsFocused = is_focused;\n\n    // Collapse button changes shape and display a list\n    if (IsPopupOpen(\"#TabListMenu\"))\n    {\n        if (ImGuiID tab_id = DockNodeUpdateTabListMenu(node, tab_bar))\n            focus_tab_id = tab_bar->NextSelectedTabId = tab_id;\n        is_focused |= node->IsFocused;\n    }\n\n    // Title bar\n    if (is_focused)\n        node->LastFrameFocused = g.FrameCount;\n    ImRect title_bar_rect = ImRect(node->Pos, node->Pos + ImVec2(node->Size.x, g.FontSize + style.FramePadding.y * 2.0f));\n    ImU32 title_bar_col = GetColorU32(host_window->Collapsed ? ImGuiCol_TitleBgCollapsed : is_focused ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg);\n    host_window->DrawList->AddRectFilled(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, host_window->WindowRounding, ImDrawCornerFlags_Top);\n\n    // Collapse button\n    if (CollapseButton(host_window->GetID(\"#COLLAPSE\"), title_bar_rect.Min, node))\n        OpenPopup(\"#TabListMenu\");\n    if (IsItemActive())\n        focus_tab_id = tab_bar->SelectedTabId;\n\n    // Submit new tabs and apply NavWindow focus back to the tab bar. They will be added as Unsorted and sorted below based on relative DockOrder value.\n    const int tabs_count_old = tab_bar->Tabs.Size;\n    for (int window_n = 0; window_n < node->Windows.Size; window_n++)\n    {\n        ImGuiWindow* window = node->Windows[window_n];\n        if (g.NavWindow && g.NavWindow->RootWindowDockStop == window)\n            tab_bar->SelectedTabId = window->ID;\n        if (TabBarFindTabByID(tab_bar, window->ID) == NULL)\n            TabBarAddTab(tab_bar, ImGuiTabItemFlags_Unsorted, window);\n    }\n\n    // If multiple tabs are appearing on the same frame, sort them based on their persistent DockOrder value\n    int tabs_unsorted_start = tab_bar->Tabs.Size;\n    for (int tab_n = tab_bar->Tabs.Size - 1; tab_n >= 0 && (tab_bar->Tabs[tab_n].Flags & ImGuiTabItemFlags_Unsorted); tab_n--)\n    {\n        tab_bar->Tabs[tab_n].Flags &= ~ImGuiTabItemFlags_Unsorted;\n        tabs_unsorted_start = tab_n;\n    }\n    //printf(\"[%05d] Sorting %d new appearing tabs\\n\", g.FrameCount, tab_bar->Tabs.Size - tabs_unsorted_start);\n    if (tab_bar->Tabs.Size > tabs_unsorted_start + 1)\n        ImQsort(tab_bar->Tabs.Data + tabs_unsorted_start, tab_bar->Tabs.Size - tabs_unsorted_start, sizeof(ImGuiTabItem), TabItemComparerByDockOrder);\n\n    // Selected newly added tabs, or persistent tab ID if the tab bar was just recreated\n    if (tab_bar_is_recreated && TabBarFindTabByID(tab_bar, node->SelectedTabID) != NULL)\n        tab_bar->SelectedTabId = tab_bar->NextSelectedTabId = node->SelectedTabID;\n    else if (tab_bar->Tabs.Size > tabs_count_old)\n        tab_bar->SelectedTabId = tab_bar->NextSelectedTabId = tab_bar->Tabs.back().Window->ID;\n\n    // Begin tab bar\n    const ImRect tab_bar_rect = DockNodeCalcTabBarRect(node);\n    ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_AutoSelectNewTabs; // | ImGuiTabBarFlags_NoTabListScrollingButtons);\n    tab_bar_flags |= ImGuiTabBarFlags_SaveSettings | ImGuiTabBarFlags_DockNode;\n    if (!host_window->Collapsed && is_focused)\n        tab_bar_flags |= ImGuiTabBarFlags_IsFocused;\n    BeginTabBarEx(tab_bar, tab_bar_rect, tab_bar_flags, node);\n    //host_window->DrawList->AddRect(tab_bar_rect.Min, tab_bar_rect.Max, IM_COL32(255,0,255,255));\n\n    // Submit actual tabs\n    node->VisibleWindow = NULL;\n    for (int window_n = 0; window_n < node->Windows.Size; window_n++)\n    {\n        ImGuiWindow* window = node->Windows[window_n];\n        if ((closed_all || closed_one == window->ID) && window->HasCloseButton && !(window->Flags & ImGuiWindowFlags_UnsavedDocument))\n            continue;\n        if (window->LastFrameActive + 1 >= g.FrameCount || !node_was_active)\n        {\n            ImGuiTabItemFlags tab_item_flags = 0;\n            if (window->Flags & ImGuiWindowFlags_UnsavedDocument)\n                tab_item_flags |= ImGuiTabItemFlags_UnsavedDocument;\n            if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton)\n                tab_item_flags |= ImGuiTabItemFlags_NoCloseWithMiddleMouseButton;\n\n            bool tab_open = true;\n            TabItemEx(tab_bar, window->Name, window->HasCloseButton ? &tab_open : NULL, tab_item_flags, window);\n            if (!tab_open)\n                node->WantCloseTabID = window->ID;\n            if (tab_bar->VisibleTabId == window->ID)\n                node->VisibleWindow = window;\n\n            // Store last item data so it can be queried with IsItemXXX functions after the user Begin() call\n            window->DockTabItemStatusFlags = host_window->DC.LastItemStatusFlags;\n            window->DockTabItemRect = host_window->DC.LastItemRect;\n\n            // Update navigation ID on menu layer\n            if (g.NavWindow && g.NavWindow->RootWindowDockStop == window && (window->DC.NavLayerActiveMask & (1 << 1)) == 0)\n                host_window->NavLastIds[1] = window->ID;\n        }\n    }\n\n    // Notify root of visible window (used to display title in OS task bar)\n    if (node->VisibleWindow)\n        if (is_focused || root_node->VisibleWindow == NULL)\n            root_node->VisibleWindow = node->VisibleWindow;\n\n    // Close button (after VisibleWindow was updated)\n    // Note that VisibleWindow may have been overrided by CTRL+Tabbing, so VisibleWindow->ID may be != from tab_bar->SelectedTabId\n    if (node->VisibleWindow)\n    {\n        if (!node->VisibleWindow->HasCloseButton)\n        {\n            PushItemFlag(ImGuiItemFlags_Disabled, true);\n            PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_Text] * ImVec4(1.0f,1.0f,1.0f,0.5f));\n        }\n        const float rad = g.FontSize * 0.5f;\n        if (CloseButton(host_window->GetID(\"#CLOSE\"), title_bar_rect.GetTR() + ImVec2(-style.FramePadding.x - rad, style.FramePadding.y + rad), rad + 1))\n            if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_bar->VisibleTabId))\n            {\n                node->WantCloseTabID = tab->ID;\n                TabBarCloseTab(tab_bar, tab);\n            }\n        //if (IsItemActive())\n        //    focus_tab_id = tab_bar->SelectedTabId;\n        if (!node->VisibleWindow->HasCloseButton)\n        {\n            PopStyleColor();\n            PopItemFlag();\n        }\n    }\n\n    // When clicking on the title bar outside of tabs, we still focus the selected tab for that node\n    if (g.HoveredWindow == host_window && g.HoveredId == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max))\n    {\n        if (IsMouseClicked(0))\n        {\n            focus_tab_id = tab_bar->SelectedTabId;\n\n            // Forward moving request to selected window\n            if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, focus_tab_id))\n                StartMouseMovingWindow(tab->Window);\n        }\n    }\n\n    // Forward focus from host node to selected window\n    //if (is_focused && g.NavWindow == host_window && !g.NavWindowingTarget)\n    //    focus_tab_id = tab_bar->SelectedTabId;\n\n    // When clicked on a tab we requested focus to the docked child\n    // This overrides the value set by \"forward focus from host node to selected window\".\n    if (tab_bar->NextSelectedTabId)\n        focus_tab_id = tab_bar->NextSelectedTabId;\n\n    // Apply navigation focus\n    if (focus_tab_id != 0)\n        if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, focus_tab_id))\n        {\n            FocusWindow(tab->Window);\n            NavInitWindow(tab->Window, false);\n        }\n\n    EndTabBar();\n    PopID();\n\n    // Restore SkipItems flag\n    if (!node->IsDockSpace())\n    {\n        host_window->DC.NavLayerCurrent = ImGuiNavLayer_Main;\n        host_window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main);\n        host_window->SkipItems = backup_skip_item;\n    }\n}\n\nstatic void ImGui::DockNodeAddTabBar(ImGuiDockNode* node)\n{\n    IM_ASSERT(node->TabBar == NULL);\n    node->TabBar = IM_NEW(ImGuiTabBar);\n}\n\nstatic void ImGui::DockNodeRemoveTabBar(ImGuiDockNode* node)\n{\n    if (node->TabBar == NULL)\n        return;\n    IM_DELETE(node->TabBar);\n    node->TabBar = NULL;\n}\n\nstatic bool DockNodeIsDropAllowedOne(ImGuiWindow* payload, ImGuiWindow* host_window)\n{\n    if (host_window->DockNodeAsHost && host_window->DockNodeAsHost->IsDockSpace() && payload->BeginOrderWithinContext < host_window->BeginOrderWithinContext)\n        return false;\n\n    ImGuiWindowClass* host_class = host_window->DockNodeAsHost ? &host_window->DockNodeAsHost->WindowClass : &host_window->WindowClass;\n    ImGuiWindowClass* payload_class = &payload->WindowClass;\n    if (host_class->ClassId != payload_class->ClassId)\n    {\n        if (host_class->ClassId != 0 && host_class->DockingAllowUnclassed && payload_class->ClassId == 0)\n            return true;\n        if (payload_class->ClassId != 0 && payload_class->DockingAllowUnclassed && host_class->ClassId == 0)\n            return true;\n        return false;\n    }\n\n    return true;\n}\n\nstatic bool ImGui::DockNodeIsDropAllowed(ImGuiWindow* host_window, ImGuiWindow* root_payload)\n{\n    if (root_payload->DockNodeAsHost && root_payload->DockNodeAsHost->IsSplitNode())\n        return true;\n\n    const int payload_count = root_payload->DockNodeAsHost ? root_payload->DockNodeAsHost->Windows.Size : 1;\n    for (int payload_n = 0; payload_n < payload_count; payload_n++)\n    {\n        ImGuiWindow* payload = root_payload->DockNodeAsHost ? root_payload->DockNodeAsHost->Windows[payload_n] : root_payload;\n        if (DockNodeIsDropAllowedOne(payload, host_window))\n            return true;\n    }\n    return false;\n}\n\nstatic ImRect ImGui::DockNodeCalcTabBarRect(const ImGuiDockNode* node)\n{\n    ImGuiContext& g = *GImGui;\n    ImRect r = ImRect(node->Pos.x, node->Pos.y, node->Pos.x + node->Size.x, node->Pos.y + (g.FontSize + g.Style.FramePadding.y * 2.0f));\n    if (node->HasCollapseButton)\n        r.Min.x += g.Style.FramePadding.x + g.FontSize; // + g.Style.ItemInnerSpacing.x; // <-- Adding ItemInnerSpacing makes the title text moves slightly when in a tab bar. Instead we adjusted RenderArrowDockMenu()\n    // In DockNodeUpdateTabBar() we currently display a disabled close button even if there is none.\n    r.Max.x -= g.Style.FramePadding.x + g.FontSize + 1.0f;\n    return r;\n}\n\nvoid ImGui::DockNodeCalcSplitRects(ImVec2& pos_old, ImVec2& size_old, ImVec2& pos_new, ImVec2& size_new, ImGuiDir dir, ImVec2 size_new_desired)\n{\n    ImGuiContext& g = *GImGui;\n    const float dock_spacing = g.Style.ItemInnerSpacing.x;\n    const ImGuiAxis axis = (dir == ImGuiDir_Left || dir == ImGuiDir_Right) ? ImGuiAxis_X : ImGuiAxis_Y;\n    pos_new[axis ^ 1] = pos_old[axis ^ 1];\n    size_new[axis ^ 1] = size_old[axis ^ 1];\n\n    // Distribute size on given axis (with a desired size or equally)\n    const float w_avail = size_old[axis] - dock_spacing;\n    if (size_new_desired[axis] > 0.0f && size_new_desired[axis] <= w_avail * 0.5f)\n    {\n        size_new[axis] = size_new_desired[axis];\n        size_old[axis] = (float)(int)(w_avail - size_new[axis]);\n    }\n    else\n    {\n        size_new[axis] = (float)(int)(w_avail * 0.5f);\n        size_old[axis] = (float)(int)(w_avail - size_new[axis]);\n    }\n\n    // Position each node\n    if (dir == ImGuiDir_Right || dir == ImGuiDir_Down)\n    {\n        pos_new[axis] = pos_old[axis] + size_old[axis] + dock_spacing;\n    }\n    else if (dir == ImGuiDir_Left || dir == ImGuiDir_Up)\n    {\n        pos_new[axis] = pos_old[axis];\n        pos_old[axis] = pos_new[axis] + size_new[axis] + dock_spacing;\n    }\n}\n\n// Retrieve the drop rectangles for a given direction or for the center + perform hit testing.\nbool ImGui::DockNodeCalcDropRectsAndTestMousePos(const ImRect& parent, ImGuiDir dir, ImRect& out_r, bool outer_docking, ImVec2* test_mouse_pos)\n{\n    ImGuiContext& g = *GImGui;\n\n    const float parent_smaller_axis = ImMin(parent.GetWidth(), parent.GetHeight());\n    const float hs_for_central_nodes = ImMin(g.FontSize * 1.5f, ImMax(g.FontSize * 0.5f, parent_smaller_axis / 8.0f));\n    float hs_w; // Half-size, longer axis\n    float hs_h; // Half-size, smaller axis\n    ImVec2 off; // Distance from edge or center\n    if (outer_docking)\n    {\n        //hs_w = ImFloor(ImClamp(parent_smaller_axis - hs_for_central_nodes * 4.0f, g.FontSize * 0.5f, g.FontSize * 8.0f));\n        //hs_h = ImFloor(hs_w * 0.15f);\n        //off = ImVec2(ImFloor(parent.GetWidth() * 0.5f - GetFrameHeightWithSpacing() * 1.4f - hs_h), ImFloor(parent.GetHeight() * 0.5f - GetFrameHeightWithSpacing() * 1.4f - hs_h));\n        hs_w = ImFloor(hs_for_central_nodes * 1.50f);\n        hs_h = ImFloor(hs_for_central_nodes * 0.80f);\n        off = ImVec2(ImFloor(parent.GetWidth() * 0.5f - GetFrameHeightWithSpacing() * 0.0f - hs_h), ImFloor(parent.GetHeight() * 0.5f - GetFrameHeightWithSpacing() * 0.0f - hs_h));\n    }\n    else\n    {\n        hs_w = ImFloor(hs_for_central_nodes);\n        hs_h = ImFloor(hs_for_central_nodes * 0.90f);\n        off = ImVec2(ImFloor(hs_w * 2.40f), ImFloor(hs_w * 2.40f));\n    }\n\n    ImVec2 c = ImFloor(parent.GetCenter());\n    if      (dir == ImGuiDir_None)  { out_r = ImRect(c.x - hs_w, c.y - hs_w,         c.x + hs_w, c.y + hs_w);         }\n    else if (dir == ImGuiDir_Up)    { out_r = ImRect(c.x - hs_w, c.y - off.y - hs_h, c.x + hs_w, c.y - off.y + hs_h); }\n    else if (dir == ImGuiDir_Down)  { out_r = ImRect(c.x - hs_w, c.y + off.y - hs_h, c.x + hs_w, c.y + off.y + hs_h); }\n    else if (dir == ImGuiDir_Left)  { out_r = ImRect(c.x - off.x - hs_h, c.y - hs_w, c.x - off.x + hs_h, c.y + hs_w); }\n    else if (dir == ImGuiDir_Right) { out_r = ImRect(c.x + off.x - hs_h, c.y - hs_w, c.x + off.x + hs_h, c.y + hs_w); }\n\n    if (test_mouse_pos == NULL)\n        return false;\n\n    ImRect hit_r = out_r;\n    if (!outer_docking)\n    {\n        // Custom hit testing for the 5-way selection, designed to reduce flickering when moving diagonally between sides\n        hit_r.Expand(ImFloor(hs_w * 0.30f));\n        ImVec2 mouse_delta = (*test_mouse_pos - c);\n        float mouse_delta_len2 = ImLengthSqr(mouse_delta);\n        float r_threshold_center = hs_w * 1.4f;\n        float r_threshold_sides = hs_w * (1.4f + 1.2f);\n        if (mouse_delta_len2 < r_threshold_center * r_threshold_center)\n            return (dir == ImGuiDir_None);\n        if (mouse_delta_len2 < r_threshold_sides * r_threshold_sides)\n            return (dir == ImGetDirQuadrantFromDelta(mouse_delta.x, mouse_delta.y));\n    }\n    return hit_r.Contains(*test_mouse_pos);\n}\n\n// host_node may be NULL if the window doesn't have a DockNode already.\nstatic void ImGui::DockNodePreviewDockCalc(ImGuiWindow* host_window, ImGuiDockNode* host_node, ImGuiWindow* root_payload, ImGuiDockPreviewData* data, bool is_explicit_target, bool is_outer_docking)\n{\n    ImGuiContext& g = *GImGui;\n\n    // There is an edge case when docking into a dockspace which only has inactive nodes.\n    // In this case DockNodeTreeFindNodeByPos() will have selected a leaf node which is inactive. \n    // Because the inactive leaf node doesn't have proper pos/size yet, we'll use the root node as reference.\n    ImGuiDockNode* ref_node_for_rect = (host_node && !host_node->IsVisible) ? DockNodeGetRootNode(host_node) : host_node;\n    if (ref_node_for_rect)\n        IM_ASSERT(ref_node_for_rect->IsVisible);\n\n    // Build a tentative future node (reuse same structure because it is practical)\n    data->FutureNode.HasCloseButton = (host_node ? host_node->HasCloseButton : host_window->HasCloseButton) || (root_payload->HasCloseButton);\n    data->FutureNode.HasCollapseButton = host_node ? true : ((host_window->Flags & ImGuiWindowFlags_NoCollapse) == 0);\n    data->FutureNode.Pos = host_node ? ref_node_for_rect->Pos : host_window->Pos;\n    data->FutureNode.Size = host_node ? ref_node_for_rect->Size : host_window->Size;\n\n    // Figure out here we are allowed to dock\n    ImGuiDockNodeFlags host_node_flags = host_node ? host_node->GetMergedFlags() : 0;\n    const bool src_is_visibly_splitted = root_payload->DockNodeAsHost && root_payload->DockNodeAsHost->IsSplitNode() && (root_payload->DockNodeAsHost->OnlyNodeWithWindows == NULL);\n    data->IsCenterAvailable = !is_outer_docking;\n    if (src_is_visibly_splitted && (!host_node || !host_node->IsEmpty()))\n        data->IsCenterAvailable = false;\n    if (host_node && (host_node_flags & ImGuiDockNodeFlags_NoDockingInCentralNode) && host_node->IsCentralNode())\n        data->IsCenterAvailable = false;\n\n    data->IsSidesAvailable = true;\n    if ((host_node && (host_node_flags & ImGuiDockNodeFlags_NoSplit)) || g.IO.ConfigDockingNoSplit)\n        data->IsSidesAvailable = false;\n    if (!is_outer_docking && host_node && host_node->ParentNode == NULL && host_node->IsCentralNode())\n        data->IsSidesAvailable = false;\n\n    // Calculate drop shapes geometry for allowed splitting directions\n    IM_ASSERT(ImGuiDir_None == -1);\n    data->SplitNode = host_node;\n    data->SplitDir = ImGuiDir_None;\n    data->IsSplitDirExplicit = false;\n    if (!host_window->Collapsed)\n        for (int dir = ImGuiDir_None; dir < ImGuiDir_COUNT; dir++)\n        {\n            if (dir == ImGuiDir_None && !data->IsCenterAvailable)\n                continue;\n            if (dir != ImGuiDir_None && !data->IsSidesAvailable)\n                continue;\n            if (DockNodeCalcDropRectsAndTestMousePos(data->FutureNode.Rect(), (ImGuiDir)dir, data->DropRectsDraw[dir+1], is_outer_docking, &g.IO.MousePos))\n            {\n                data->SplitDir = (ImGuiDir)dir;\n                data->IsSplitDirExplicit = true;\n            }\n        }\n\n    // When docking without holding Shift, we only allow and preview docking when hovering over a drop rect or over the title bar\n    data->IsDropAllowed = (data->SplitDir != ImGuiDir_None) || (data->IsCenterAvailable);\n    if (!is_explicit_target && !data->IsSplitDirExplicit && !g.IO.ConfigDockingWithShift)\n        data->IsDropAllowed = false;\n\n    // Calculate split area\n    data->SplitRatio = 0.0f;\n    if (data->SplitDir != ImGuiDir_None)\n    {\n        ImGuiDir split_dir = data->SplitDir;\n        ImGuiAxis split_axis = (split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Right) ? ImGuiAxis_X : ImGuiAxis_Y;\n        ImVec2 pos_new, pos_old = data->FutureNode.Pos;\n        ImVec2 size_new, size_old = data->FutureNode.Size;\n        DockNodeCalcSplitRects(pos_old, size_old, pos_new, size_new, split_dir, root_payload->Size);\n\n        // Calculate split ratio so we can pass it down the docking request\n        float split_ratio = ImSaturate(size_new[split_axis] / data->FutureNode.Size[split_axis]);\n        data->FutureNode.Pos = pos_new;\n        data->FutureNode.Size = size_new;\n        data->SplitRatio = (split_dir == ImGuiDir_Right || split_dir == ImGuiDir_Down) ? (1.0f - split_ratio) : (split_ratio);\n    }\n}\n\nstatic void ImGui::DockNodePreviewDockRender(ImGuiWindow* host_window, ImGuiDockNode* host_node, ImGuiWindow* root_payload, const ImGuiDockPreviewData* data)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.CurrentWindow == host_window);   // Because we rely on font size to calculate tab sizes\n\n    // With this option, we only display the preview on the target viewport, and the payload viewport is made transparent.\n    // To compensate for the single layer obstructed by the payload, we'll increase the alpha of the preview nodes.\n    const bool is_transparent_payload = g.IO.ConfigDockingTransparentPayload;\n\n    // In case the two windows involved are on different viewports, we will draw the overlay on each of them.\n    int overlay_draw_lists_count = 0;\n    ImDrawList* overlay_draw_lists[2];\n    overlay_draw_lists[overlay_draw_lists_count++] = GetForegroundDrawList(host_window->Viewport);\n    if (host_window->Viewport != root_payload->Viewport && !is_transparent_payload)\n        overlay_draw_lists[overlay_draw_lists_count++] = GetForegroundDrawList(root_payload->Viewport);\n\n    // Draw main preview rectangle\n    const ImU32 overlay_col_tabs = GetColorU32(ImGuiCol_TabActive);\n    const ImU32 overlay_col_main = GetColorU32(ImGuiCol_DockingPreview, is_transparent_payload ? 0.60f : 0.40f);\n    const ImU32 overlay_col_drop = GetColorU32(ImGuiCol_DockingPreview, is_transparent_payload ? 0.90f : 0.70f);\n    const ImU32 overlay_col_drop_hovered = GetColorU32(ImGuiCol_DockingPreview, is_transparent_payload ? 1.20f : 1.00f);\n    const ImU32 overlay_col_lines = GetColorU32(ImGuiCol_NavWindowingHighlight, is_transparent_payload ? 0.80f : 0.60f);\n\n    // Display area preview\n    const bool can_preview_tabs = (root_payload->DockNodeAsHost == NULL || root_payload->DockNodeAsHost->Windows.Size > 0);\n    if (data->IsDropAllowed)\n    {\n        ImRect overlay_rect = data->FutureNode.Rect();\n        if (data->SplitDir == ImGuiDir_None && can_preview_tabs)\n            overlay_rect.Min.y += GetFrameHeight();\n        if (data->SplitDir != ImGuiDir_None || data->IsCenterAvailable)\n            for (int overlay_n = 0; overlay_n < overlay_draw_lists_count; overlay_n++)\n                overlay_draw_lists[overlay_n]->AddRectFilled(overlay_rect.Min, overlay_rect.Max, overlay_col_main, host_window->WindowRounding);\n    }\n\n    // Display tab shape/label preview unless we are splitting node (it generally makes the situation harder to read)\n    if (data->IsDropAllowed && can_preview_tabs && data->SplitDir == ImGuiDir_None && data->IsCenterAvailable)\n    {\n        // Compute target tab bar geometry so we can locate our preview tabs\n        ImRect tab_bar_rect = DockNodeCalcTabBarRect(&data->FutureNode);\n        ImVec2 tab_pos = tab_bar_rect.Min;\n        if (host_node && host_node->TabBar)\n        {\n            if (!host_node->IsHiddenTabBar() && !host_node->IsNoTabBar())\n                tab_pos.x += host_node->TabBar->OffsetMax + g.Style.ItemInnerSpacing.x; // We don't use OffsetNewTab because when using non-persistent-order tab bar it is incremented with each Tab submission.\n            else\n                tab_pos.x += g.Style.ItemInnerSpacing.x + TabItemCalcSize(host_node->Windows[0]->Name, host_node->Windows[0]->HasCloseButton).x;\n        }\n        else if (!(host_window->Flags & ImGuiWindowFlags_DockNodeHost))\n        {\n            tab_pos.x += g.Style.ItemInnerSpacing.x + TabItemCalcSize(host_window->Name, host_window->HasCloseButton).x; // Account for slight offset which will be added when changing from title bar to tab bar\n        }\n\n        // Draw tab shape/label preview (payload may be a loose window or a host window carrying multiple tabbed windows)\n        if (root_payload->DockNodeAsHost)\n            IM_ASSERT(root_payload->DockNodeAsHost->Windows.Size == root_payload->DockNodeAsHost->TabBar->Tabs.Size);\n        const int payload_count = root_payload->DockNodeAsHost ? root_payload->DockNodeAsHost->TabBar->Tabs.Size : 1;\n        for (int payload_n = 0; payload_n < payload_count; payload_n++)\n        {\n            // Calculate the tab bounding box for each payload window\n            ImGuiWindow* payload = root_payload->DockNodeAsHost ? root_payload->DockNodeAsHost->TabBar->Tabs[payload_n].Window : root_payload;\n            if (!DockNodeIsDropAllowedOne(payload, host_window))\n                continue;\n\n            ImVec2 tab_size = TabItemCalcSize(payload->Name, payload->HasCloseButton);\n            ImRect tab_bb(tab_pos.x, tab_pos.y, tab_pos.x + tab_size.x, tab_pos.y + tab_size.y);\n            tab_pos.x += tab_size.x + g.Style.ItemInnerSpacing.x;\n            for (int overlay_n = 0; overlay_n < overlay_draw_lists_count; overlay_n++)\n            {\n                ImGuiTabItemFlags tab_flags = ImGuiTabItemFlags_Preview | ((payload->Flags & ImGuiWindowFlags_UnsavedDocument) ? ImGuiTabItemFlags_UnsavedDocument : 0);\n                if (!tab_bar_rect.Contains(tab_bb))\n                    overlay_draw_lists[overlay_n]->PushClipRect(tab_bar_rect.Min, tab_bar_rect.Max);\n                TabItemBackground(overlay_draw_lists[overlay_n], tab_bb, tab_flags, overlay_col_tabs);\n                TabItemLabelAndCloseButton(overlay_draw_lists[overlay_n], tab_bb, tab_flags, g.Style.FramePadding, payload->Name, 0, 0);\n                if (!tab_bar_rect.Contains(tab_bb))\n                    overlay_draw_lists[overlay_n]->PopClipRect();\n            }\n        }\n    }\n\n    // Display drop boxes\n    const float overlay_rounding = ImMax(3.0f, g.Style.FrameRounding);\n    for (int dir = ImGuiDir_None; dir < ImGuiDir_COUNT; dir++)\n    {\n        if (!data->DropRectsDraw[dir + 1].IsInverted())\n        {\n            ImRect draw_r = data->DropRectsDraw[dir + 1];\n            ImRect draw_r_in = draw_r;\n            draw_r_in.Expand(-2.0f);\n            ImU32 overlay_col = (data->SplitDir == (ImGuiDir)dir && data->IsSplitDirExplicit) ? overlay_col_drop_hovered : overlay_col_drop;\n            for (int overlay_n = 0; overlay_n < overlay_draw_lists_count; overlay_n++)\n            {\n                ImVec2 center = ImFloor(draw_r_in.GetCenter());\n                overlay_draw_lists[overlay_n]->AddRectFilled(draw_r.Min, draw_r.Max, overlay_col, overlay_rounding);\n                overlay_draw_lists[overlay_n]->AddRect(draw_r_in.Min, draw_r_in.Max, overlay_col_lines, overlay_rounding);\n                if (dir == ImGuiDir_Left || dir == ImGuiDir_Right)\n                    overlay_draw_lists[overlay_n]->AddLine(ImVec2(center.x, draw_r_in.Min.y), ImVec2(center.x, draw_r_in.Max.y), overlay_col_lines);\n                if (dir == ImGuiDir_Up || dir == ImGuiDir_Down)\n                    overlay_draw_lists[overlay_n]->AddLine(ImVec2(draw_r_in.Min.x, center.y), ImVec2(draw_r_in.Max.x, center.y), overlay_col_lines);\n            }\n        }\n        \n        // Stop after ImGuiDir_None\n        if ((host_node && (host_node->GetMergedFlags() & ImGuiDockNodeFlags_NoSplit)) || g.IO.ConfigDockingNoSplit)\n            return;\n    }\n}\n\n//-----------------------------------------------------------------------------\n// Docking: ImGuiDockNode Tree manipulation functions\n//-----------------------------------------------------------------------------\n\nvoid ImGui::DockNodeTreeSplit(ImGuiContext* ctx, ImGuiDockNode* parent_node, ImGuiAxis split_axis, int split_inheritor_child_idx, float split_ratio, ImGuiDockNode* new_node)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(split_axis != ImGuiAxis_None);\n\n    ImGuiDockNode* child_0 = (new_node && split_inheritor_child_idx != 0) ? new_node : DockContextAddNode(ctx, 0);\n    child_0->ParentNode = parent_node;\n\n    ImGuiDockNode* child_1 = (new_node && split_inheritor_child_idx != 1) ? new_node : DockContextAddNode(ctx, 0);\n    child_1->ParentNode = parent_node;\n\n    ImGuiDockNode* child_inheritor = (split_inheritor_child_idx == 0) ? child_0 : child_1;\n    DockNodeMoveChildNodes(child_inheritor, parent_node);\n    parent_node->ChildNodes[0] = child_0;\n    parent_node->ChildNodes[1] = child_1;\n    parent_node->ChildNodes[split_inheritor_child_idx]->VisibleWindow = parent_node->VisibleWindow;\n    parent_node->SplitAxis = split_axis;\n    parent_node->VisibleWindow = NULL;\n    parent_node->AuthorityForPos = parent_node->AuthorityForSize = ImGuiDataAuthority_DockNode;\n\n    float size_avail = (parent_node->Size[split_axis] - IMGUI_DOCK_SPLITTER_SIZE);\n    size_avail = ImMax(size_avail, g.Style.WindowMinSize[split_axis] * 2.0f);\n    IM_ASSERT(size_avail > 0.0f); // If you created a node manually with DockBuilderAddNode(), you need to also call DockBuilderSetNodeSize() before splitting.\n    child_0->SizeRef = child_1->SizeRef = parent_node->Size;\n    child_0->SizeRef[split_axis] = ImFloor(size_avail * split_ratio);\n    child_1->SizeRef[split_axis] = ImFloor(size_avail - child_0->SizeRef[split_axis]);\n\n    DockNodeMoveWindows(parent_node->ChildNodes[split_inheritor_child_idx], parent_node);\n    DockNodeTreeUpdatePosSize(parent_node, parent_node->Pos, parent_node->Size);\n\n    // Flags transfer (e.g. this is where we transfer the ImGuiDockNodeFlags_CentralNode property)\n    child_0->SharedFlags = parent_node->SharedFlags & ImGuiDockNodeFlags_SharedFlagsInheritMask_;\n    child_1->SharedFlags = parent_node->SharedFlags & ImGuiDockNodeFlags_SharedFlagsInheritMask_;\n    child_inheritor->LocalFlags = parent_node->LocalFlags & ImGuiDockNodeFlags_LocalFlagsTransferMask_;\n    parent_node->LocalFlags &= ~ImGuiDockNodeFlags_LocalFlagsTransferMask_;\n    if (child_inheritor->IsCentralNode())\n        DockNodeGetRootNode(parent_node)->CentralNode = child_inheritor;\n}\n\nvoid ImGui::DockNodeTreeMerge(ImGuiContext* ctx, ImGuiDockNode* parent_node, ImGuiDockNode* merge_lead_child)\n{\n    // When called from DockContextProcessUndockNode() it is possible that one of the child is NULL.\n    ImGuiDockNode* child_0 = parent_node->ChildNodes[0];\n    ImGuiDockNode* child_1 = parent_node->ChildNodes[1];\n    IM_ASSERT(child_0 || child_1);\n    IM_ASSERT(merge_lead_child == child_0 || merge_lead_child == child_1);\n    if ((child_0 && child_0->Windows.Size > 0) || (child_1 && child_1->Windows.Size > 0))\n    {\n        IM_ASSERT(parent_node->TabBar == NULL);\n        IM_ASSERT(parent_node->Windows.Size == 0);\n    }\n\n    ImVec2 backup_last_explicit_size = parent_node->SizeRef;\n    DockNodeMoveChildNodes(parent_node, merge_lead_child);\n    if (child_0)\n    {\n        DockNodeMoveWindows(parent_node, child_0); // Generally only 1 of the 2 child node will have windows\n        DockSettingsRenameNodeReferences(child_0->ID, parent_node->ID);\n    }\n    if (child_1)\n    {\n        DockNodeMoveWindows(parent_node, child_1);\n        DockSettingsRenameNodeReferences(child_1->ID, parent_node->ID);\n    }\n    DockNodeApplyPosSizeToWindows(parent_node);\n    parent_node->AuthorityForPos = parent_node->AuthorityForSize = parent_node->AuthorityForViewport = ImGuiDataAuthority_Auto;\n    parent_node->VisibleWindow = merge_lead_child->VisibleWindow;\n    parent_node->SizeRef = backup_last_explicit_size;\n\n    // Flags transfer\n    parent_node->LocalFlags = ((child_0 ? child_0->LocalFlags : 0) | (child_1 ? child_1->LocalFlags : 0)) & ImGuiDockNodeFlags_LocalFlagsTransferMask_;\n\n    if (child_0)\n    {\n        ctx->DockContext->Nodes.SetVoidPtr(child_0->ID, NULL);\n        IM_DELETE(child_0);\n    }\n    if (child_1)\n    {\n        ctx->DockContext->Nodes.SetVoidPtr(child_1->ID, NULL);\n        IM_DELETE(child_1);\n    }\n}\n\n// Update Pos/Size for a node hierarchy (don't affect child Windows yet)\nvoid ImGui::DockNodeTreeUpdatePosSize(ImGuiDockNode* node, ImVec2 pos, ImVec2 size)\n{\n    node->Pos = pos;\n    node->Size = size;\n    if (node->IsLeafNode())\n        return;\n\n    ImGuiDockNode* child_0 = node->ChildNodes[0];\n    ImGuiDockNode* child_1 = node->ChildNodes[1];\n    ImVec2 child_0_pos = pos, child_1_pos = pos;\n    ImVec2 child_0_size = size, child_1_size = size;\n    if (child_0->IsVisible && child_1->IsVisible)\n    {\n        const float spacing = IMGUI_DOCK_SPLITTER_SIZE;\n        const ImGuiAxis axis = (ImGuiAxis)node->SplitAxis;\n        const float size_avail = ImMax(size[axis] - spacing, 0.0f);\n\n        // Size allocation policy\n        // 1) The first 0..WindowMinSize[axis]*2 are allocated evenly to both windows.\n        ImGuiContext& g = *GImGui;\n        const float size_min_each = ImFloor(ImMin(size_avail, g.Style.WindowMinSize[axis] * 2.0f) * 0.5f);\n\n        // 2) Process locked absolute size (during a splitter resize we preserve the child of nodes not touching the splitter edge)\n        IM_ASSERT(!(child_0->WantLockSizeOnce && child_1->WantLockSizeOnce));\n        if (child_0->WantLockSizeOnce)\n        {\n            child_0->WantLockSizeOnce = false;\n            child_0_size[axis] = child_0->SizeRef[axis] = child_0->Size[axis];\n            child_1_size[axis] = child_1->SizeRef[axis] = (size_avail - child_0_size[axis]);\n\n        }\n        else if (child_1->WantLockSizeOnce)\n        {\n            child_1->WantLockSizeOnce = false;\n            child_1_size[axis] = child_1->SizeRef[axis] = child_1->Size[axis];\n            child_0_size[axis] = child_0->SizeRef[axis] = (size_avail - child_1_size[axis]);\n        }\n\n        // 3) If one window is the central node (~ use remaining space, should be made explicit!), use explicit size from the other, and remainder for the central node\n        else if (child_1->IsCentralNode() && child_0->SizeRef[axis] != 0.0f)\n        {\n            child_0_size[axis] = ImMin(size_avail - size_min_each, child_0->SizeRef[axis]);\n            child_1_size[axis] = (size_avail - child_0_size[axis]);\n        }\n        else if (child_0->IsCentralNode() && child_1->SizeRef[axis] != 0.0f)\n        {\n            child_1_size[axis] = ImMin(size_avail - size_min_each, child_1->SizeRef[axis]);\n            child_0_size[axis] = (size_avail - child_1_size[axis]);\n        }\n        else\n        {\n            // 4) Otherwise distribute according to the relative ratio of each SizeRef value\n            float split_ratio = child_0->SizeRef[axis] / (child_0->SizeRef[axis] + child_1->SizeRef[axis]);\n            child_0_size[axis] = ImMax(size_min_each, ImFloor(size_avail * split_ratio + 0.5F));\n            child_1_size[axis] = (size_avail - child_0_size[axis]);\n        }\n        child_1_pos[axis] += spacing + child_0_size[axis];\n    }\n    if (child_0->IsVisible)\n        DockNodeTreeUpdatePosSize(child_0, child_0_pos, child_0_size);\n    if (child_1->IsVisible)\n        DockNodeTreeUpdatePosSize(child_1, child_1_pos, child_1_size);\n}\n\nstatic void DockNodeTreeUpdateSplitterFindTouchingNode(ImGuiDockNode* node, ImGuiAxis axis, int side, ImVector<ImGuiDockNode*>* touching_nodes)\n{\n    if (node->IsLeafNode())\n    {\n        touching_nodes->push_back(node);\n        return;\n    }\n    if (node->ChildNodes[0]->IsVisible)\n        if (node->SplitAxis != axis || side == 0 || !node->ChildNodes[1]->IsVisible)\n            DockNodeTreeUpdateSplitterFindTouchingNode(node->ChildNodes[0], axis, side, touching_nodes);\n    if (node->ChildNodes[1]->IsVisible)\n        if (node->SplitAxis != axis || side == 1 || !node->ChildNodes[0]->IsVisible)\n            DockNodeTreeUpdateSplitterFindTouchingNode(node->ChildNodes[1], axis, side, touching_nodes);\n}\n\nvoid ImGui::DockNodeTreeUpdateSplitter(ImGuiDockNode* node)\n{\n    if (node->IsLeafNode())\n        return;\n\n    ImGuiContext& g = *GImGui;\n\n    ImGuiDockNode* child_0 = node->ChildNodes[0];\n    ImGuiDockNode* child_1 = node->ChildNodes[1];\n    if (child_0->IsVisible && child_1->IsVisible)\n    {\n        // Bounding box of the splitter cover the space between both nodes (w = Spacing, h = Size[xy^1] for when splitting horizontally)\n        const ImGuiAxis axis = (ImGuiAxis)node->SplitAxis;\n        IM_ASSERT(axis != ImGuiAxis_None);\n        ImRect bb;\n        bb.Min = child_0->Pos;\n        bb.Max = child_1->Pos;\n        bb.Min[axis] += child_0->Size[axis];\n        bb.Max[axis ^ 1] += child_1->Size[axis ^ 1];\n        //if (g.IO.KeyCtrl) GetForegroundDrawList(g.CurrentWindow->Viewport)->AddRect(bb.Min, bb.Max, IM_COL32(255,0,255,255));\n\n        if ((child_0->GetMergedFlags() | child_1->GetMergedFlags()) & ImGuiDockNodeFlags_NoResize)\n        {\n            ImGuiWindow* window = g.CurrentWindow;\n            window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_Separator), g.Style.FrameRounding);\n        }\n        else\n        {\n            //bb.Min[axis] += 1; // Display a little inward so highlight doesn't connect with nearby tabs on the neighbor node.\n            //bb.Max[axis] -= 1;\n            PushID(node->ID);\n\n            // Gather list of nodes that are touching the splitter line. Find resizing limits based on those nodes.\n            ImVector<ImGuiDockNode*> touching_nodes[2];\n            float min_size = g.Style.WindowMinSize[axis];\n            float resize_limits[2];\n            resize_limits[0] = node->ChildNodes[0]->Pos[axis] + min_size;\n            resize_limits[1] = node->ChildNodes[1]->Pos[axis] + node->ChildNodes[1]->Size[axis] - min_size;\n\n            ImGuiID splitter_id = GetID(\"##Splitter\");\n            if (g.ActiveId == splitter_id)\n            {\n                // Only process when splitter is active\n                DockNodeTreeUpdateSplitterFindTouchingNode(child_0, axis, 1, &touching_nodes[0]);\n                DockNodeTreeUpdateSplitterFindTouchingNode(child_1, axis, 0, &touching_nodes[1]);\n                for (int touching_node_n = 0; touching_node_n < touching_nodes[0].Size; touching_node_n++)\n                    resize_limits[0] = ImMax(resize_limits[0], touching_nodes[0][touching_node_n]->Rect().Min[axis] + min_size);\n                for (int touching_node_n = 0; touching_node_n < touching_nodes[1].Size; touching_node_n++)\n                    resize_limits[1] = ImMin(resize_limits[1], touching_nodes[1][touching_node_n]->Rect().Max[axis] - min_size);\n\n                /*\n                // [DEBUG] Render limits\n                ImDrawList* draw_list = node->HostWindow ? GetOverlayDrawList(node->HostWindow) : GetOverlayDrawList((ImGuiViewportP*)GetMainViewport());\n                for (int n = 0; n < 2; n++)\n                    if (axis == ImGuiAxis_X)\n                        draw_list->AddLine(ImVec2(resize_limits[n], node->ChildNodes[n]->Pos.y), ImVec2(resize_limits[n], node->ChildNodes[n]->Pos.y + node->ChildNodes[n]->Size.y), IM_COL32(255, 0, 255, 255), 3.0f);\n                    else\n                        draw_list->AddLine(ImVec2(node->ChildNodes[n]->Pos.x, resize_limits[n]), ImVec2(node->ChildNodes[n]->Pos.x + node->ChildNodes[n]->Size.x, resize_limits[n]), IM_COL32(255, 0, 255, 255), 3.0f);\n                */\n            }\n\n            // Use a short delay before highlighting the splitter (and changing the mouse cursor) in order for regular mouse movement to not highlight many splitters\n            float cur_size_0 = child_0->Size[axis];\n            float cur_size_1 = child_1->Size[axis];\n            float min_size_0 = resize_limits[0] - child_0->Pos[axis];\n            float min_size_1 = child_1->Pos[axis] + child_1->Size[axis] - resize_limits[1];\n            if (SplitterBehavior(bb, GetID(\"##Splitter\"), axis, &cur_size_0, &cur_size_1, min_size_0, min_size_1, WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS, WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER))\n            {\n                if (touching_nodes[0].Size > 0 && touching_nodes[1].Size > 0)\n                {\n                    child_0->Size[axis] = child_0->SizeRef[axis] = cur_size_0;\n                    child_1->Pos[axis] -= cur_size_1 - child_1->Size[axis];\n                    child_1->Size[axis] = child_1->SizeRef[axis] = cur_size_1;\n\n                    // Lock the size of every node that is a sibling of the node we are touching\n                    // This might be less desirable if we can merge sibling of a same axis into the same parental level.\n                    for (int side_n = 0; side_n < 2; side_n++)\n                        for (int touching_node_n = 0; touching_node_n < touching_nodes[side_n].Size; touching_node_n++)\n                        {\n                            ImGuiDockNode* touching_node = touching_nodes[side_n][touching_node_n];\n                            //ImDrawList* draw_list = node->HostWindow ? GetOverlayDrawList(node->HostWindow) : GetOverlayDrawList((ImGuiViewportP*)GetMainViewport());\n                            //draw_list->AddRect(touching_node->Pos, touching_node->Pos + touching_node->Size, IM_COL32(255, 128, 0, 255));\n                            while (touching_node->ParentNode != node)\n                            {\n                                if (touching_node->ParentNode->SplitAxis == axis)\n                                {\n                                    // Mark other node so its size will be preserved during the upcoming call to DockNodeTreeUpdatePosSize().\n                                    ImGuiDockNode* node_to_preserve = touching_node->ParentNode->ChildNodes[side_n];\n                                    node_to_preserve->WantLockSizeOnce = true;\n                                    //draw_list->AddRect(touching_node->Pos, touching_node->Rect().Max, IM_COL32(255, 0, 0, 255));\n                                    //draw_list->AddRectFilled(node_to_preserve->Pos, node_to_preserve->Rect().Max, IM_COL32(0, 255, 0, 100));\n                                }\n                                touching_node = touching_node->ParentNode;\n                            }\n                        }\n\n                    DockNodeTreeUpdatePosSize(child_0, child_0->Pos, child_0->Size);\n                    DockNodeTreeUpdatePosSize(child_1, child_1->Pos, child_1->Size);\n                    MarkIniSettingsDirty();\n                }\n            }\n            PopID();\n        }\n    }\n\n    if (child_0->IsVisible)\n        DockNodeTreeUpdateSplitter(child_0);\n    if (child_1->IsVisible)\n        DockNodeTreeUpdateSplitter(child_1);\n}\n\nImGuiDockNode* ImGui::DockNodeTreeFindFallbackLeafNode(ImGuiDockNode* node)\n{\n    if (node->IsLeafNode())\n        return node;\n    if (ImGuiDockNode* leaf_node = DockNodeTreeFindFallbackLeafNode(node->ChildNodes[0]))\n        return leaf_node;\n    if (ImGuiDockNode* leaf_node = DockNodeTreeFindFallbackLeafNode(node->ChildNodes[1]))\n        return leaf_node;\n    return NULL;\n}\n\nImGuiDockNode* ImGui::DockNodeTreeFindNodeByPos(ImGuiDockNode* node, ImVec2 pos)\n{\n    if (!node->IsVisible)\n        return NULL;\n\n    ImGuiContext& g = *GImGui;\n    const float dock_spacing = g.Style.ItemInnerSpacing.x;\n    ImRect r(node->Pos, node->Pos + node->Size);\n    r.Expand(dock_spacing * 0.5f);\n    bool inside = r.Contains(pos);\n    if (!inside)\n        return NULL;\n\n    if (node->IsLeafNode())\n        return node;\n    if (ImGuiDockNode* hovered_node = DockNodeTreeFindNodeByPos(node->ChildNodes[0], pos))\n        return hovered_node;\n    if (ImGuiDockNode* hovered_node = DockNodeTreeFindNodeByPos(node->ChildNodes[1], pos))\n        return hovered_node;\n\n    // There is an edge case when docking into a dockspace which only has inactive nodes (because none of the windows are active)\n    // In this case we need to fallback into any leaf mode, possibly the central node.\n    if (node->IsDockSpace() && node->IsRootNode())\n    {\n        if (node->CentralNode && node->IsLeafNode()) // FIXME-20181220: We should not have to test for IsLeafNode() here but we have another bug to fix first.\n            return node->CentralNode;\n        return DockNodeTreeFindFallbackLeafNode(node);\n    }\n    \n    return NULL;\n}\n\n//-----------------------------------------------------------------------------\n// Docking: Public Functions (SetWindowDock, DockSpace, DockSpaceOverViewport)\n//-----------------------------------------------------------------------------\n\nvoid ImGui::SetWindowDock(ImGuiWindow* window, ImGuiID dock_id, ImGuiCond cond)\n{\n    // Test condition (NB: bit 0 is always true) and clear flags for next time\n    if (cond && (window->SetWindowDockAllowFlags & cond) == 0)\n        return;\n    window->SetWindowDockAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing);\n\n    if (window->DockId == dock_id)\n        return;\n\n    // If the user attempt to set a dock id that is a split node, we'll dig within to find a suitable docking spot\n    ImGuiContext* ctx = GImGui;\n    if (ImGuiDockNode* new_node = DockContextFindNodeByID(ctx, dock_id))\n        if (new_node->IsSplitNode())\n        {\n            // Policy: Find central node or latest focused node. We first move back to our root node.\n            new_node = DockNodeGetRootNode(new_node);\n            if (new_node->CentralNode)\n            {\n                IM_ASSERT(new_node->CentralNode->IsCentralNode());\n                dock_id = new_node->CentralNode->ID;\n            }\n            else\n            {\n                dock_id = new_node->LastFocusedNodeID;\n            }\n        }\n\n    if (window->DockId == dock_id)\n        return;\n\n    if (window->DockNode)\n        DockNodeRemoveWindow(window->DockNode, window, 0);\n    window->DockId = dock_id;\n}\n\n// Create an explicit dockspace node within an existing window. Also expose dock node flags and creates a CentralNode by default.\n// The Central Node is always displayed even when empty and shrink/extend according to the requested size of its neighbors.\nvoid ImGui::DockSpace(ImGuiID id, const ImVec2& size_arg, ImGuiDockNodeFlags flags, const ImGuiWindowClass* window_class)\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiContext& g = *ctx;\n    ImGuiWindow* window = GetCurrentWindow();\n    if (!(g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable))\n        return;\n\n    IM_ASSERT((flags & ImGuiDockNodeFlags_DockSpace) == 0);\n    ImGuiDockNode* node = DockContextFindNodeByID(ctx, id);\n    if (!node)\n    {\n        node = DockContextAddNode(ctx, id);\n        node->LocalFlags |= ImGuiDockNodeFlags_CentralNode;\n    }\n    node->SharedFlags = flags;\n    node->WindowClass = window_class ? *window_class : ImGuiWindowClass();\n\n    // When a DockSpace transitioned form implicit to explicit this may be called a second time\n    // It is possible that the node has already been claimed by a docked window which appeared before the DockSpace() node, so we overwrite IsDockSpace again.\n    if (node->LastFrameActive == g.FrameCount && !(flags & ImGuiDockNodeFlags_KeepAliveOnly))\n    {\n        IM_ASSERT(node->IsDockSpace() == false && \"Cannot call DockSpace() twice a frame with the same ID\");\n        node->LocalFlags |= ImGuiDockNodeFlags_DockSpace;\n        return;\n    }\n    node->LocalFlags |= ImGuiDockNodeFlags_DockSpace;\n\n    // Keep alive mode, this is allow windows docked into this node so stay docked even if they are not visible\n    if (flags & ImGuiDockNodeFlags_KeepAliveOnly)\n    {\n        node->LastFrameAlive = g.FrameCount;\n        return;\n    }\n\n    const ImVec2 content_avail = GetContentRegionAvail();\n    ImVec2 size = ImFloor(size_arg);\n    if (size.x <= 0.0f)\n        size.x = ImMax(content_avail.x + size.x, 4.0f); // Arbitrary minimum child size (0.0f causing too much issues)\n    if (size.y <= 0.0f)\n        size.y = ImMax(content_avail.y + size.y, 4.0f);\n\n    node->Pos = window->DC.CursorPos;\n    node->Size = node->SizeRef = size;\n    SetNextWindowPos(node->Pos);\n    SetNextWindowSize(node->Size);\n    g.NextWindowData.PosUndock = false;\n\n    ImGuiWindowFlags window_flags = ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_DockNodeHost;\n    window_flags |= ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar;\n    window_flags |= ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse;\n\n    char title[256];\n    ImFormatString(title, IM_ARRAYSIZE(title), \"%s/DockSpace_%08X\", window->Name, id);\n\n    if (node->Windows.Size > 0 || node->IsSplitNode())\n        PushStyleColor(ImGuiCol_ChildBg, IM_COL32(0, 0, 0, 0));\n    PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0.0f);\n    Begin(title, NULL, window_flags);\n    PopStyleVar();\n    if (node->Windows.Size > 0 || node->IsSplitNode())\n        PopStyleColor();\n\n    ImGuiWindow* host_window = g.CurrentWindow;\n    host_window->DockNodeAsHost = node;\n    host_window->ChildId = window->GetID(title);\n    node->HostWindow = host_window;\n    node->OnlyNodeWithWindows = NULL;\n\n    IM_ASSERT(node->IsRootNode());\n    DockNodeUpdate(node);\n\n    End();\n}\n\n// Tips: Use with ImGuiDockNodeFlags_PassthruCentralNode!\n// The limitation with this call is that your window won't have a menu bar. \n// Even though we could pass window flags, it would also require the user to be able to call BeginMenuBar() somehow meaning we can't Begin/End in a single function.\n// So if you want a menu bar you need to repeat this code manually ourselves. As with advanced other Docking API, we may change this function signature.\nImGuiID ImGui::DockSpaceOverViewport(bool has_main_menu_bar, ImGuiViewport* viewport, ImGuiDockNodeFlags dockspace_flags, const ImGuiWindowClass* window_class)\n{\n    if (viewport == NULL)\n        viewport = GetMainViewport();\n\n\tauto pos = viewport->Pos;\n\tauto size = viewport->Size;\n\n\tif (has_main_menu_bar)\n\t{\n\t\tImGuiContext& g = *GImGui;\n\t\tg.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f));\n\n\t\tfloat height_offset = g.NextWindowData.MenuBarOffsetMinVal.y + g.FontBaseSize + (g.Style.FramePadding.y * 2);\n\t\tpos.y += height_offset;\n\t\tsize.y -= height_offset;\n\n\t}\n\n\tSetNextWindowPos(pos);\n\tSetNextWindowSize(size);\n    SetNextWindowViewport(viewport->ID);\n\n    ImGuiWindowFlags host_window_flags = 0;\n    host_window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDocking;\n    host_window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;\n    if (dockspace_flags & ImGuiDockNodeFlags_PassthruCentralNode)\n        host_window_flags |= ImGuiWindowFlags_NoBackground;\n\n    char label[32];\n    ImFormatString(label, IM_ARRAYSIZE(label), \"DockSpaceViewport_%08X\", viewport->ID);\n\n    PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);\n    PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);\n    PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));\n    Begin(label, NULL, host_window_flags);\n    PopStyleVar(3);\n\n    ImGuiID dockspace_id = GetID(\"DockSpace\");\n    DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags, window_class);\n    End();\n\n    return dockspace_id;\n}\n\n//-----------------------------------------------------------------------------\n// Docking: Builder Functions\n//-----------------------------------------------------------------------------\n// Very early end-user API to manipulate dock nodes.\n// It is expected that those functions are all called _before_ the dockspace node submission.\n//-----------------------------------------------------------------------------\n\nvoid ImGui::DockBuilderDockWindow(const char* window_name, ImGuiID node_id)\n{\n    // We don't preserve relative order of multiple docked windows (by clearing DockOrder back to -1)\n    ImGuiID window_id = ImHashStr(window_name);\n    if (ImGuiWindow* window = FindWindowByID(window_id))\n    {\n        // Apply to created window\n        SetWindowDock(window, node_id, ImGuiCond_Always);\n        window->DockOrder = -1;\n    }\n    else\n    {\n        // Apply to settings\n        ImGuiWindowSettings* settings = FindWindowSettings(window_id);\n        if (settings == NULL)\n            settings = CreateNewWindowSettings(window_name);\n        settings->DockId = node_id;\n        settings->DockOrder = -1;\n    }\n}\n\nImGuiDockNode* ImGui::DockBuilderGetNode(ImGuiID node_id)\n{\n    ImGuiContext* ctx = GImGui;\n    return DockContextFindNodeByID(ctx, node_id);\n}\n\nvoid ImGui::DockBuilderSetNodePos(ImGuiID node_id, ImVec2 pos)\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiDockNode* node = DockContextFindNodeByID(ctx, node_id);\n    if (node == NULL)\n        return;\n    node->Pos = pos;\n    node->AuthorityForPos = ImGuiDataAuthority_DockNode;\n}\n\nvoid ImGui::DockBuilderSetNodeSize(ImGuiID node_id, ImVec2 size)\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiDockNode* node = DockContextFindNodeByID(ctx, node_id);\n    if (node == NULL)\n        return;\n    node->Size = node->SizeRef = size;\n    node->AuthorityForSize = ImGuiDataAuthority_DockNode;\n}\n\n// If you create a regular node, both ref_pos/ref_size will position the window.\n// If you create a dockspace node: ref_pos won't be used, ref_size is useful on the first frame to...\nImGuiID ImGui::DockBuilderAddNode(ImGuiID id, ImGuiDockNodeFlags flags)\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiDockNode* node = NULL;\n    if (flags & ImGuiDockNodeFlags_DockSpace)\n    {\n        DockSpace(id, ImVec2(0, 0), (flags & ~ImGuiDockNodeFlags_DockSpace) | ImGuiDockNodeFlags_KeepAliveOnly);\n        node = DockContextFindNodeByID(ctx, id);\n    }\n    else\n    {\n        if (id != 0)\n            node = DockContextFindNodeByID(ctx, id);\n        if (!node)\n            node = DockContextAddNode(ctx, id);\n        node->LocalFlags = flags;\n    }\n    node->LastFrameAlive = ctx->FrameCount;   // Set this otherwise BeginDocked will undock during the same frame.\n    return node->ID;\n}\n\nvoid ImGui::DockBuilderRemoveNode(ImGuiID node_id)\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiDockNode* node = DockContextFindNodeByID(ctx, node_id);\n    if (node == NULL)\n        return;\n    DockBuilderRemoveNodeDockedWindows(node_id, true);\n    DockBuilderRemoveNodeChildNodes(node_id);\n    if (node->IsCentralNode() && node->ParentNode)\n        node->ParentNode->LocalFlags = ImGuiDockNodeFlags_CentralNode;\n    DockContextRemoveNode(ctx, node, true);\n}\n\nvoid ImGui::DockBuilderRemoveNodeChildNodes(ImGuiID root_id)\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiDockContext* dc = ctx->DockContext;\n\n    ImGuiDockNode* root_node = root_id ? DockContextFindNodeByID(ctx, root_id) : NULL;\n    if (root_id && root_node == NULL)\n        return;\n    bool has_central_node = false;\n\n    ImGuiDataAuthority backup_root_node_authority_for_pos = root_node ? root_node->AuthorityForPos : ImGuiDataAuthority_Auto;\n    ImGuiDataAuthority backup_root_node_authority_for_size = root_node ? root_node->AuthorityForSize : ImGuiDataAuthority_Auto;\n\n    // Process active windows\n    ImVector<ImGuiDockNode*> nodes_to_remove;\n    for (int n = 0; n < dc->Nodes.Data.Size; n++)\n        if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p)\n        {\n            bool want_removal = (root_id == 0) || (node->ID != root_id && DockNodeGetRootNode(node)->ID == root_id);\n            if (want_removal)\n            {\n                if (node->IsCentralNode())\n                    has_central_node = true;\n                if (root_id != 0)\n                    DockContextQueueNotifyRemovedNode(ctx, node);\n                if (root_node)\n                    DockNodeMoveWindows(root_node, node);\n                nodes_to_remove.push_back(node);\n            }\n        }\n\n    // DockNodeMoveWindows->DockNodeAddWindow will normally set those when reaching two windows (which is only adequate during interactive merge)\n    // Make sure we don't lose our current pos/size. (FIXME-DOCK: Consider tidying up that code in DockNodeAddWindow instead)\n    if (root_node)\n    {\n        root_node->AuthorityForPos = backup_root_node_authority_for_pos;\n        root_node->AuthorityForSize = backup_root_node_authority_for_size;\n    }\n\n    // Apply to settings\n    for (int settings_n = 0; settings_n < ctx->SettingsWindows.Size; settings_n++)\n        if (ImGuiID window_settings_dock_id = ctx->SettingsWindows[settings_n].DockId)\n            for (int n = 0; n < nodes_to_remove.Size; n++)\n                if (nodes_to_remove[n]->ID == window_settings_dock_id)\n                {\n                    ctx->SettingsWindows[settings_n].DockId = root_id;\n                    break;\n                }\n\n    // Not really efficient, but easier to destroy a whole hierarchy considering DockContextRemoveNode is attempting to merge nodes\n    if (nodes_to_remove.Size > 1)\n        ImQsort(nodes_to_remove.Data, nodes_to_remove.Size, sizeof(ImGuiDockNode*), DockNodeComparerDepthMostFirst);\n    for (int n = 0; n < nodes_to_remove.Size; n++)\n        DockContextRemoveNode(ctx, nodes_to_remove[n], false);\n\n    if (root_id == 0)\n    {\n        dc->Nodes.Clear();\n        dc->Requests.clear();\n    }\n    else if (has_central_node)\n    {\n        root_node->LocalFlags |= ImGuiDockNodeFlags_CentralNode;\n        root_node->CentralNode = root_node;\n    }\n}\n\nvoid ImGui::DockBuilderRemoveNodeDockedWindows(ImGuiID root_id, bool clear_persistent_docking_references)\n{\n    // Clear references in settings\n    ImGuiContext* ctx = GImGui;\n    ImGuiContext& g = *ctx;\n    if (clear_persistent_docking_references)\n    {\n        for (int n = 0; n < g.SettingsWindows.Size; n++)\n        {\n            ImGuiWindowSettings* settings = &g.SettingsWindows[n];\n            bool want_removal = (root_id == 0) || (settings->DockId == root_id);\n            if (!want_removal && settings->DockId != 0)\n                if (ImGuiDockNode* node = DockContextFindNodeByID(ctx, settings->DockId))\n                    if (DockNodeGetRootNode(node)->ID == root_id)\n                        want_removal = true;\n            if (want_removal)\n                settings->DockId = 0;\n        }\n    }\n\n    // Clear references in windows\n    for (int n = 0; n < g.Windows.Size; n++)\n    {\n        ImGuiWindow* window = g.Windows[n];\n        bool want_removal = (root_id == 0) || (window->DockNode && DockNodeGetRootNode(window->DockNode)->ID == root_id) || (window->DockNodeAsHost && window->DockNodeAsHost->ID == root_id);\n        if (want_removal)\n        {\n            const ImGuiID backup_dock_id = window->DockId;\n            DockContextProcessUndockWindow(ctx, window, clear_persistent_docking_references);\n            if (!clear_persistent_docking_references)\n                IM_ASSERT(window->DockId == backup_dock_id);\n        }\n    }\n}\n\n// FIXME-DOCK: We are not exposing nor using split_outer. \nImGuiID ImGui::DockBuilderSplitNode(ImGuiID id, ImGuiDir split_dir, float size_ratio_for_node_at_dir, ImGuiID* out_id_at_dir, ImGuiID* out_id_other)\n{\n    ImGuiContext* ctx = GImGui;\n    IM_ASSERT(split_dir != ImGuiDir_None);\n\n    ImGuiDockNode* node = DockContextFindNodeByID(ctx, id);\n    if (node == NULL)\n    {\n        IM_ASSERT(node != NULL);\n        return 0;\n    }\n\n    IM_ASSERT(!node->IsSplitNode()); // Assert if already Split\n\n    ImGuiDockRequest req;\n    req.Type = ImGuiDockRequestType_Split;\n    req.DockTargetWindow = NULL;\n    req.DockTargetNode = node;\n    req.DockPayload = NULL;\n    req.DockSplitDir = split_dir;\n    req.DockSplitRatio = ImSaturate((split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Up) ? size_ratio_for_node_at_dir : 1.0f - size_ratio_for_node_at_dir);\n    req.DockSplitOuter = false;\n    DockContextProcessDock(ctx, &req);\n\n    ImGuiID id_at_dir = node->ChildNodes[(split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Up) ? 0 : 1]->ID;\n    ImGuiID id_other = node->ChildNodes[(split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Up) ? 1 : 0]->ID;\n    if (out_id_at_dir)\n        *out_id_at_dir = id_at_dir;\n    if (out_id_other)\n        *out_id_other = id_other;\n    return id_at_dir;\n}\n\nstatic ImGuiDockNode* DockBuilderCopyNodeRec(ImGuiDockNode* src_node, ImGuiID dst_node_id_if_known, ImVector<ImGuiID>* out_node_remap_pairs)\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiDockNode* dst_node = ImGui::DockContextAddNode(ctx, dst_node_id_if_known);\n    dst_node->SharedFlags = src_node->SharedFlags;\n    dst_node->LocalFlags = src_node->LocalFlags;\n    dst_node->Pos = src_node->Pos;\n    dst_node->Size = src_node->Size;\n    dst_node->SizeRef = src_node->SizeRef;\n    dst_node->SplitAxis = src_node->SplitAxis;\n\n    out_node_remap_pairs->push_back(src_node->ID);\n    out_node_remap_pairs->push_back(dst_node->ID);\n\n    for (int child_n = 0; child_n < IM_ARRAYSIZE(src_node->ChildNodes); child_n++)\n        if (src_node->ChildNodes[child_n])\n        {\n            dst_node->ChildNodes[child_n] = DockBuilderCopyNodeRec(src_node->ChildNodes[child_n], 0, out_node_remap_pairs);\n            dst_node->ChildNodes[child_n]->ParentNode = dst_node;\n        }\n\n    //IMGUI_DEBUG_LOG(\"Fork node %08X -> %08X (%d childs)\\n\", src_node->ID, dst_node->ID, dst_node->IsSplitNode() ? 2 : 0);\n    return dst_node;\n}\n\nvoid ImGui::DockBuilderCopyNode(ImGuiID src_node_id, ImGuiID dst_node_id, ImVector<ImGuiID>* out_node_remap_pairs)\n{\n    ImGuiContext* ctx = GImGui;\n    IM_ASSERT(src_node_id != 0);\n    IM_ASSERT(dst_node_id != 0);\n    IM_ASSERT(out_node_remap_pairs != NULL);\n\n    ImGuiDockNode* src_node = DockContextFindNodeByID(ctx, src_node_id);\n    IM_ASSERT(src_node != NULL);\n\n    out_node_remap_pairs->clear();\n    DockBuilderRemoveNode(dst_node_id);\n    DockBuilderCopyNodeRec(src_node, dst_node_id, out_node_remap_pairs);\n\n    IM_ASSERT((out_node_remap_pairs->Size % 2) == 0);\n}\n\nvoid ImGui::DockBuilderCopyWindowSettings(const char* src_name, const char* dst_name)\n{\n    ImGuiWindow* src_window = FindWindowByName(src_name);\n    if (src_window == NULL)\n        return;\n    if (ImGuiWindow* dst_window = FindWindowByName(dst_name))\n    {\n        dst_window->Pos = src_window->Pos;\n        dst_window->Size = src_window->Size;\n        dst_window->SizeFull = src_window->SizeFull;\n        dst_window->Collapsed = src_window->Collapsed;\n    }\n    else if (ImGuiWindowSettings* dst_settings = FindOrCreateWindowSettings(dst_name))\n    {\n        if (src_window->ViewportId != 0 && src_window->ViewportId != IMGUI_VIEWPORT_DEFAULT_ID)\n        {\n            dst_settings->ViewportPos = src_window->Pos;\n            dst_settings->ViewportId = src_window->ViewportId;\n            dst_settings->Pos = ImVec2(0.0f, 0.0f);\n        }\n        else\n        {\n            dst_settings->Pos = src_window->Pos;\n        }\n        dst_settings->Size = src_window->SizeFull;\n        dst_settings->Collapsed = src_window->Collapsed;\n    }\n}\n\n// FIXME: Will probably want to change this signature, in particular how the window remapping pairs are passed.\nvoid ImGui::DockBuilderCopyDockSpace(ImGuiID src_dockspace_id, ImGuiID dst_dockspace_id, ImVector<const char*>* in_window_remap_pairs)\n{\n    IM_ASSERT(src_dockspace_id != 0);\n    IM_ASSERT(dst_dockspace_id != 0);\n    IM_ASSERT(in_window_remap_pairs != NULL);\n    IM_ASSERT((in_window_remap_pairs->Size % 2) == 0);\n\n    // Duplicate entire dock\n    // FIXME: When overwriting dst_dockspace_id, windows that aren't part of our dockspace window class but that are docked in a same node will be split apart,\n    // whereas we could attempt to at least keep them together in a new, same floating node.\n    ImVector<ImGuiID> node_remap_pairs;\n    DockBuilderCopyNode(src_dockspace_id, dst_dockspace_id, &node_remap_pairs);\n\n    // Attempt to transition all the upcoming windows associated to dst_dockspace_id into the newly created hierarchy of dock nodes\n    // (The windows associated to src_dockspace_id are staying in place)\n    ImVector<ImGuiID> src_windows;\n    for (int remap_window_n = 0; remap_window_n < in_window_remap_pairs->Size; remap_window_n += 2)\n    {\n        const char* src_window_name = (*in_window_remap_pairs)[remap_window_n];\n        const char* dst_window_name = (*in_window_remap_pairs)[remap_window_n + 1];\n        ImGuiID src_window_id = ImHashStr(src_window_name);\n        src_windows.push_back(src_window_id);\n\n        // Search in the remapping tables\n        ImGuiID src_dock_id = 0;\n        if (ImGuiWindow* src_window = FindWindowByID(src_window_id))\n            src_dock_id = src_window->DockId;\n        else if (ImGuiWindowSettings* src_window_settings = FindWindowSettings(src_window_id))\n            src_dock_id = src_window_settings->DockId;\n        ImGuiID dst_dock_id = 0;\n        for (int dock_remap_n = 0; dock_remap_n < node_remap_pairs.Size; dock_remap_n += 2)\n            if (node_remap_pairs[dock_remap_n] == src_dock_id)\n            {\n                dst_dock_id = node_remap_pairs[dock_remap_n + 1];\n                //node_remap_pairs[dock_remap_n] = node_remap_pairs[dock_remap_n + 1] = 0; // Clear\n                break;\n            }\n\n        if (dst_dock_id != 0)\n        {\n            // Docked windows gets redocked into the new node hierarchy. \n            //IMGUI_DEBUG_LOG(\"Remap window '%s' %08X -> %08X\\n\", dst_window_name, src_dock_id, dst_dock_id);\n            DockBuilderDockWindow(dst_window_name, dst_dock_id);\n        }\n        else\n        {\n            // Floating windows gets their settings transferred (regardless of whether the new window already exist or not)\n            // When this is leading to a Copy and not a Move, we would get two overlapping floating windows. Could we possibly dock them together?\n            DockBuilderCopyWindowSettings(src_window_name, dst_window_name);\n        }\n    }\n\n    // Anything else in the source nodes of 'node_remap_pairs' are windows that were docked in src_dockspace_id but are not owned by it (unaffiliated windows, e.g. \"ImGui Demo\")\n    // Find those windows and move to them to the cloned dock node. This may be optional?\n    for (int dock_remap_n = 0; dock_remap_n < node_remap_pairs.Size; dock_remap_n += 2)\n        if (ImGuiID src_dock_id = node_remap_pairs[dock_remap_n])\n        {\n            ImGuiID dst_dock_id = node_remap_pairs[dock_remap_n + 1];\n            ImGuiDockNode* node = DockBuilderGetNode(src_dock_id);\n            for (int window_n = 0; window_n < node->Windows.Size; window_n++)\n            {\n                ImGuiWindow* window = node->Windows[window_n];\n                if (src_windows.contains(window->ID))\n                    continue;\n\n                // Docked windows gets redocked into the new node hierarchy. \n                //IMGUI_DEBUG_LOG(\"Remap window '%s' %08X -> %08X\\n\", window->Name, src_dock_id, dst_dock_id);\n                DockBuilderDockWindow(window->Name, dst_dock_id);\n            }\n        }\n}\n\nvoid ImGui::DockBuilderFinish(ImGuiID root_id)\n{\n    ImGuiContext* ctx = GImGui;\n    //DockContextRebuild(ctx);\n    DockContextBuildAddWindowsToNodes(ctx, root_id);\n}\n\n//-----------------------------------------------------------------------------\n// Docking: Begin/End Functions (called from Begin/End)\n//-----------------------------------------------------------------------------\n\nvoid ImGui::BeginDocked(ImGuiWindow* window, bool* p_open)\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiContext& g = *ctx;\n\n    const bool auto_dock_node = (g.IO.ConfigDockingTabBarOnSingleWindows) && !(window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDocking));\n    if (auto_dock_node)\n    {\n        if (window->DockId == 0)\n        {\n            IM_ASSERT(window->DockNode == NULL);\n            window->DockId = DockContextGenNodeID(ctx);\n        }\n    }\n    else\n    {\n        // Calling SetNextWindowPos() undock windows by default (by setting PosUndock)\n        bool want_undock = false;\n        want_undock |= (window->Flags & ImGuiWindowFlags_NoDocking) != 0;\n        want_undock |= (g.NextWindowData.PosCond && (window->SetWindowPosAllowFlags & g.NextWindowData.PosCond) && g.NextWindowData.PosUndock);\n        g.NextWindowData.PosUndock = false;\n        if (want_undock)\n        {\n            DockContextProcessUndockWindow(ctx, window);\n            return;\n        }\n    }\n\n    // Bind to our dock node\n    ImGuiDockNode* node = window->DockNode;\n    if (node != NULL)\n        IM_ASSERT(window->DockId == node->ID);\n    if (window->DockId != 0 && node == NULL)\n    {\n        node = DockContextFindNodeByID(ctx, window->DockId);\n        \n        // We should not be docking into a split node (SetWindowDock should avoid this)\n        if (node && node->IsSplitNode())\n        {\n            DockContextProcessUndockWindow(ctx, window);\n            return;\n        }\n\n        // Create node\n        if (node == NULL)\n        {\n            node = DockContextAddNode(ctx, window->DockId);\n            node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_Window;\n            if (auto_dock_node)\n                node->LastFrameAlive = g.FrameCount;\n        }\n\n        DockNodeAddWindow(node, window, true);\n        IM_ASSERT(node == window->DockNode);\n    }\n\n#if 0\n    // Undock if the ImGuiDockNodeFlags_NoDockingInCentralNode got set\n    if (node->IsCentralNode && (node->Flags & ImGuiDockNodeFlags_NoDockingInCentralNode))\n    {\n        DockContextProcessUndockWindow(ctx, window);\n        return;\n    }\n#endif\n\n    // Undock if our dockspace node disappeared\n    // Note how we are testing for LastFrameAlive and NOT LastFrameActive. A DockSpace node can be maintained alive while being inactive with ImGuiDockNodeFlags_KeepAliveOnly.\n    if (node->LastFrameAlive < g.FrameCount)\n    {\n        // If the window has been orphaned, transition the docknode to an implicit node processed in DockContextUpdateDocking()\n        ImGuiDockNode* root_node = DockNodeGetRootNode(node);\n        if (root_node->LastFrameAlive < g.FrameCount)\n        {\n            DockContextProcessUndockWindow(ctx, window);\n        }\n        else\n        {\n            window->DockIsActive = true;\n            window->DockTabIsVisible = false;\n        }\n        return;\n    }\n\n    // Undock if we are submitted earlier than the host window\n    if (node->HostWindow && window->BeginOrderWithinContext < node->HostWindow->BeginOrderWithinContext)\n    {\n        DockContextProcessUndockWindow(ctx, window);\n        return;\n    }\n\n    // FIXME-DOCK: replace ->HostWindow NULL compare with something more explicit (~was initially intended as a first frame test)\n    if (node->HostWindow == NULL)\n    {\n        window->DockTabIsVisible = false;\n        return;\n    }\n    IM_ASSERT(node->HostWindow);\n    IM_ASSERT(node->IsLeafNode());\n\n    // Position window\n    SetNextWindowPos(node->Pos);\n    SetNextWindowSize(node->Size);\n    g.NextWindowData.PosUndock = false; // Cancel implicit undocking of SetNextWindowPos()\n    window->DockIsActive = true;\n    window->DockTabIsVisible = false;\n    if (node->SharedFlags & ImGuiDockNodeFlags_KeepAliveOnly)\n        return;\n\n    // When the window is selected we mark it as visible.\n    if (node->VisibleWindow == window)\n        window->DockTabIsVisible = true;\n\n    // Update window flag\n    IM_ASSERT((window->Flags & ImGuiWindowFlags_ChildWindow) == 0);\n    window->Flags |= ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_NoResize;\n    if (node->IsHiddenTabBar() || node->IsNoTabBar())\n        window->Flags |= ImGuiWindowFlags_NoTitleBar;\n    else\n        window->Flags &= ~ImGuiWindowFlags_NoTitleBar;      // Clear the NoTitleBar flag in case the user set it: confusingly enough we need a title bar height so we are correctly offset, but it won't be displayed!\n\n    // Save new dock order only if the tab bar has been visible once.\n    // This allows multiple windows to be created in the same frame and have their respective dock orders preserved.\n    if (node->TabBar && node->TabBar->CurrFrameVisible != -1)\n        window->DockOrder = (short)DockNodeGetTabOrder(window);\n\n    if ((node->WantCloseAll || node->WantCloseTabID == window->ID) && p_open != NULL)\n        *p_open = false;\n\n    // Update ChildId to allow returning from Child to Parent with Escape\n    ImGuiWindow* parent_window = window->DockNode->HostWindow;\n    window->ChildId = parent_window->GetID(window->Name);\n}\n\nvoid ImGui::BeginAsDockableDragDropSource(ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(g.ActiveId == window->MoveId);\n\n    window->DC.LastItemId = window->MoveId;\n    window = window->RootWindow;\n    IM_ASSERT((window->Flags & ImGuiWindowFlags_NoDocking) == 0);\n    bool is_drag_docking = (g.IO.ConfigDockingWithShift) || ImRect(0, 0, window->SizeFull.x, GetFrameHeight()).Contains(g.ActiveIdClickOffset);\n    if (is_drag_docking && BeginDragDropSource(ImGuiDragDropFlags_SourceNoPreviewTooltip | ImGuiDragDropFlags_SourceNoHoldToOpenOthers | ImGuiDragDropFlags_SourceAutoExpirePayload))\n    {\n        SetDragDropPayload(IMGUI_PAYLOAD_TYPE_WINDOW, &window, sizeof(window));\n        EndDragDropSource();\n    }\n}\n\nvoid ImGui::BeginAsDockableDragDropTarget(ImGuiWindow* window)\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiContext& g = *ctx;\n\n    //IM_ASSERT(window->RootWindow == window); // May also be a DockSpace\n    IM_ASSERT((window->Flags & ImGuiWindowFlags_NoDocking) == 0);\n    if (!g.DragDropActive)\n        return;\n    if (!BeginDragDropTargetCustom(window->Rect(), window->ID))\n        return;\n\n    // Peek into the payload before calling AcceptDragDropPayload() so we can handle overlapping dock nodes with filtering\n    // (this is a little unusual pattern, normally most code would call AcceptDragDropPayload directly)\n    const ImGuiPayload* payload = &g.DragDropPayload;\n    if (!payload->IsDataType(IMGUI_PAYLOAD_TYPE_WINDOW) || !DockNodeIsDropAllowed(window, *(ImGuiWindow**)payload->Data))\n    {\n        EndDragDropTarget();\n        return;\n    }\n\n    ImGuiWindow* payload_window = *(ImGuiWindow**)payload->Data;\n    if (AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_WINDOW, ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect))\n    {\n        // Select target node\n        ImGuiDockNode* node = NULL;\n        bool allow_null_target_node = false;\n        if (window->DockNodeAsHost)\n            node = DockNodeTreeFindNodeByPos(window->DockNodeAsHost, g.IO.MousePos);\n        else if (window->DockNode) // && window->DockIsActive)\n            node = window->DockNode;\n        else\n            allow_null_target_node = true; // Dock into a regular window\n\n        const ImRect explicit_target_rect = (node && node->TabBar && !node->IsHiddenTabBar() && !node->IsNoTabBar()) ? node->TabBar->BarRect : ImRect(window->Pos, window->Pos + ImVec2(window->Size.x, GetFrameHeight()));\n        const bool is_explicit_target = g.IO.ConfigDockingWithShift || IsMouseHoveringRect(explicit_target_rect.Min, explicit_target_rect.Max);\n\n        // Preview docking request and find out split direction/ratio\n        //const bool do_preview = true;     // Ignore testing for payload->IsPreview() which removes one frame of delay, but breaks overlapping drop targets within the same window.        \n        const bool do_preview = payload->IsPreview() || payload->IsDelivery();\n        if (do_preview && (node != NULL || allow_null_target_node))\n        {\n            ImGuiDockPreviewData split_inner, split_outer;\n            ImGuiDockPreviewData* split_data = &split_inner;\n            if (node && (node->ParentNode || node->IsCentralNode()))\n                if (ImGuiDockNode* root_node = DockNodeGetRootNode(node))\n                {\n                    DockNodePreviewDockCalc(window, root_node, payload_window, &split_outer, is_explicit_target, true);\n                    if (split_outer.IsSplitDirExplicit)\n                        split_data = &split_outer;\n                }\n            DockNodePreviewDockCalc(window, node, payload_window, &split_inner, is_explicit_target, false);\n            if (split_data == &split_outer)\n                split_inner.IsDropAllowed = false;\n\n            // Draw inner then outer, so that previewed tab (in inner data) will be behind the outer drop boxes\n            DockNodePreviewDockRender(window, node, payload_window, &split_inner);\n            DockNodePreviewDockRender(window, node, payload_window, &split_outer);\n\n            // Queue docking request\n            if (split_data->IsDropAllowed && payload->IsDelivery())\n                DockContextQueueDock(ctx, window, split_data->SplitNode, payload_window, split_data->SplitDir, split_data->SplitRatio, split_data == &split_outer);\n        }\n    }\n    EndDragDropTarget();\n}\n\n//-----------------------------------------------------------------------------\n// Docking: Settings\n//-----------------------------------------------------------------------------\n\nstatic void ImGui::DockSettingsRenameNodeReferences(ImGuiID old_node_id, ImGuiID new_node_id)\n{\n    ImGuiContext& g = *GImGui;\n    for (int window_n = 0; window_n < g.Windows.Size; window_n++)\n    {\n        ImGuiWindow* window = g.Windows[window_n];\n        if (window->DockId == old_node_id && window->DockNode == NULL)\n            window->DockId = new_node_id;\n    }\n    for (int settings_n = 0; settings_n < g.SettingsWindows.Size; settings_n++)     // FIXME-OPT: We could remove this loop by storing the index in the map\n    {\n        ImGuiWindowSettings* window_settings = &g.SettingsWindows[settings_n];\n        if (window_settings->DockId == old_node_id)\n            window_settings->DockId = new_node_id;\n    }\n}\n\n// Remove references stored in ImGuiWindowSettings to the given ImGuiDockNodeSettings\nstatic void ImGui::DockSettingsRemoveNodeReferences(ImGuiID* node_ids, int node_ids_count)\n{\n    ImGuiContext& g = *GImGui;\n    int found = 0;\n    for (int settings_n = 0; settings_n < g.SettingsWindows.Size; settings_n++)     // FIXME-OPT: We could remove this loop by storing the index in the map\n    {\n        ImGuiWindowSettings* window_settings = &g.SettingsWindows[settings_n];\n        for (int node_n = 0; node_n < node_ids_count; node_n++)\n            if (window_settings->DockId == node_ids[node_n])\n            {\n                window_settings->DockId = 0;\n                window_settings->DockOrder = -1;\n                if (++found < node_ids_count)\n                    break;\n                return;\n            }\n    }\n}\n\nstatic ImGuiDockNodeSettings* ImGui::DockSettingsFindNodeSettings(ImGuiContext* ctx, ImGuiID id)\n{\n    // FIXME-OPT\n    ImGuiDockContext* dc = ctx->DockContext;\n    for (int n = 0; n < dc->SettingsNodes.Size; n++)\n        if (dc->SettingsNodes[n].ID == id)\n            return &dc->SettingsNodes[n];\n    return NULL;\n}\n\nstatic void* ImGui::DockSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name)\n{\n    if (strcmp(name, \"Data\") != 0)\n        return NULL;\n    return (void*)1;\n}\n\nstatic void ImGui::DockSettingsHandler_ReadLine(ImGuiContext* ctx, ImGuiSettingsHandler*, void*, const char* line)\n{\n    char c = 0;\n    int x = 0, y = 0;\n    int r = 0;\n\n    // Parsing, e.g.\n    // \" DockNode   ID=0x00000001 Pos=383,193 Size=201,322 Split=Y,0.506 \"\n    // \"   DockNode ID=0x00000002 Parent=0x00000001 \"\n    ImGuiDockNodeSettings node;\n    line = ImStrSkipBlank(line);\n    if      (strncmp(line, \"DockNode\", 8) == 0)  { line = ImStrSkipBlank(line + strlen(\"DockNode\")); }\n    else if (strncmp(line, \"DockSpace\", 9) == 0) { line = ImStrSkipBlank(line + strlen(\"DockSpace\")); node.IsDockSpace = true; }\n    else return;\n    if (sscanf(line, \"ID=0x%08X%n\",      &node.ID, &r) == 1)        { line += r; } else return;\n    if (sscanf(line, \" Parent=0x%08X%n\", &node.ParentID, &r) == 1)  { line += r; if (node.ParentID == 0) return; }\n    if (node.ParentID == 0)\n    {\n        if (sscanf(line, \" Pos=%i,%i%n\",  &x, &y, &r) == 2)         { line += r; node.Pos = ImVec2ih((short)x, (short)y); } else return;\n        if (sscanf(line, \" Size=%i,%i%n\", &x, &y, &r) == 2)         { line += r; node.Size = ImVec2ih((short)x, (short)y); } else return;\n    }\n    else\n    {\n        if (sscanf(line, \" SizeRef=%i,%i%n\", &x, &y, &r) == 2)      { line += r; node.SizeRef = ImVec2ih((short)x, (short)y); }\n    }\n    if (sscanf(line, \" Split=%c%n\", &c, &r) == 1)                   { line += r; if (c == 'X') node.SplitAxis = ImGuiAxis_X; else if (c == 'Y') node.SplitAxis = ImGuiAxis_Y; }\n    if (sscanf(line, \" CentralNode=%d%n\", &x, &r) == 1)             { line += r; node.IsCentralNode = (x != 0); }\n    if (sscanf(line, \" HiddenTabBar=%d%n\", &x, &r) == 1)            { line += r; node.IsHiddenTabBar = (x != 0); }\n    if (sscanf(line, \" SelectedTab=0x%08X%n\", &node.SelectedTabID,&r) == 1) { line += r; }\n    ImGuiDockContext* dc = ctx->DockContext;\n    if (node.ParentID != 0)\n        if (ImGuiDockNodeSettings* parent_settings = DockSettingsFindNodeSettings(ctx, node.ParentID))\n            node.Depth = parent_settings->Depth + 1;\n    dc->SettingsNodes.push_back(node);\n}\n\nstatic void DockSettingsHandler_DockNodeToSettings(ImGuiDockContext* dc, ImGuiDockNode* node, int depth)\n{\n    ImGuiDockNodeSettings node_settings;\n    IM_ASSERT(depth < (1 << (sizeof(node_settings.Depth) << 3)));\n    node_settings.ID = node->ID;\n    node_settings.ParentID = node->ParentNode ? node->ParentNode->ID : 0;\n    node_settings.SelectedTabID = node->SelectedTabID;\n    node_settings.SplitAxis = node->IsSplitNode() ? (char)node->SplitAxis : ImGuiAxis_None;\n    node_settings.Depth = (char)depth;\n    node_settings.IsDockSpace = (char)node->IsDockSpace();\n    node_settings.IsCentralNode = (char)node->IsCentralNode();\n    node_settings.IsHiddenTabBar = (char)node->IsHiddenTabBar();\n    node_settings.Pos = ImVec2ih((short)node->Pos.x, (short)node->Pos.y);\n    node_settings.Size = ImVec2ih((short)node->Size.x, (short)node->Size.y);\n    node_settings.SizeRef = ImVec2ih((short)node->SizeRef.x, (short)node->SizeRef.y);\n    dc->SettingsNodes.push_back(node_settings);\n    if (node->ChildNodes[0])\n        DockSettingsHandler_DockNodeToSettings(dc, node->ChildNodes[0], depth + 1);\n    if (node->ChildNodes[1])\n        DockSettingsHandler_DockNodeToSettings(dc, node->ChildNodes[1], depth + 1);\n}\n\nstatic void ImGui::DockSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf)\n{\n    ImGuiContext& g = *ctx;\n    ImGuiDockContext* dc = g.DockContext;\n    if (!(g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable))\n        return;\n\n    // Gather settings data\n    // (unlike our windows settings, because nodes are always built we can do a full rewrite of the SettingsNode buffer)\n    dc->SettingsNodes.resize(0);\n    dc->SettingsNodes.reserve(dc->Nodes.Data.Size);\n    for (int n = 0; n < dc->Nodes.Data.Size; n++)\n        if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p)\n            if (node->IsRootNode())\n                DockSettingsHandler_DockNodeToSettings(dc, node, 0);\n\n    int max_depth = 0;\n    for (int node_n = 0; node_n < dc->SettingsNodes.Size; node_n++)\n        max_depth = ImMax((int)dc->SettingsNodes[node_n].Depth, max_depth);\n\n    // Write to text buffer\n    buf->appendf(\"[%s][Data]\\n\", handler->TypeName);\n    for (int node_n = 0; node_n < dc->SettingsNodes.Size; node_n++)\n    {\n        const int line_start_pos = buf->size(); (void)line_start_pos;\n        const ImGuiDockNodeSettings* node_settings = &dc->SettingsNodes[node_n];\n        buf->appendf(\"%*s%s%*s\", node_settings->Depth * 2, \"\", node_settings->IsDockSpace ? \"DockSpace\" : \"DockNode \", (max_depth - node_settings->Depth) * 2, \"\");  // Text align nodes to facilitate looking at .ini file\n        buf->appendf(\" ID=0x%08X\", node_settings->ID);\n        if (node_settings->ParentID)\n            buf->appendf(\" Parent=0x%08X SizeRef=%d,%d\", node_settings->ParentID, node_settings->SizeRef.x, node_settings->SizeRef.y);\n        else\n            buf->appendf(\" Pos=%d,%d Size=%d,%d\", node_settings->Pos.x, node_settings->Pos.y, node_settings->Size.x, node_settings->Size.y);\n        if (node_settings->SplitAxis != ImGuiAxis_None)\n            buf->appendf(\" Split=%c\", (node_settings->SplitAxis == ImGuiAxis_X) ? 'X' : 'Y');\n        if (node_settings->IsCentralNode)\n            buf->appendf(\" CentralNode=1\");\n        if (node_settings->IsHiddenTabBar)\n            buf->appendf(\" HiddenTabBar=1\");\n        if (node_settings->SelectedTabID)\n            buf->appendf(\" SelectedTab=0x%08X\", node_settings->SelectedTabID);\n\n#if IMGUI_DEBUG_DOCKING_INI\n        // [DEBUG] Include comments in the .ini file to ease debugging\n        if (ImGuiDockNode* node = DockContextFindNodeByID(ctx, node_settings->ID))\n        {\n            buf->appendf(\"%*s\", ImMax(2, (line_start_pos + 92) - buf->size()), \"\");        // Align everything\n            if (node->IsDockSpace && node->HostWindow && node->HostWindow->ParentWindow)\n                buf->appendf(\" ; in '%s'\", node->HostWindow->ParentWindow->Name);\n            int contains_window = 0;\n            for (int window_n = 0; window_n < ctx->SettingsWindows.Size; window_n++)\n                if (ctx->SettingsWindows[window_n].DockId == node_settings->ID)\n                {\n                    if (contains_window++ == 0)\n                        buf->appendf(\" ; contains \");\n                    buf->appendf(\"'%s' \", ctx->SettingsWindows[window_n].Name);\n                }\n        }\n#endif\n        buf->appendf(\"\\n\");\n    }\n    buf->appendf(\"\\n\");\n}\n\n\n//-----------------------------------------------------------------------------\n// [SECTION] PLATFORM DEPENDENT HELPERS\n//-----------------------------------------------------------------------------\n\n#if defined(_WIN32) && !defined(_WINDOWS_) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && (!defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) || !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS))\n#ifndef WIN32_LEAN_AND_MEAN\n#define WIN32_LEAN_AND_MEAN\n#endif\n#ifndef __MINGW32__\n#include <Windows.h>\n#else\n#include <windows.h>\n#endif\n#endif\n\n// Win32 API clipboard implementation\n#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS)\n\n#ifdef _MSC_VER\n#pragma comment(lib, \"user32\")\n#endif\n\nstatic const char* GetClipboardTextFn_DefaultImpl(void*)\n{\n    static ImVector<char> buf_local;\n    buf_local.clear();\n    if (!::OpenClipboard(NULL))\n        return NULL;\n    HANDLE wbuf_handle = ::GetClipboardData(CF_UNICODETEXT);\n    if (wbuf_handle == NULL)\n    {\n        ::CloseClipboard();\n        return NULL;\n    }\n    if (ImWchar* wbuf_global = (ImWchar*)::GlobalLock(wbuf_handle))\n    {\n        int buf_len = ImTextCountUtf8BytesFromStr(wbuf_global, NULL) + 1;\n        buf_local.resize(buf_len);\n        ImTextStrToUtf8(buf_local.Data, buf_len, wbuf_global, NULL);\n    }\n    ::GlobalUnlock(wbuf_handle);\n    ::CloseClipboard();\n    return buf_local.Data;\n}\n\nstatic void SetClipboardTextFn_DefaultImpl(void*, const char* text)\n{\n    if (!::OpenClipboard(NULL))\n        return;\n    const int wbuf_length = ImTextCountCharsFromUtf8(text, NULL) + 1;\n    HGLOBAL wbuf_handle = ::GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)wbuf_length * sizeof(ImWchar));\n    if (wbuf_handle == NULL)\n    {\n        ::CloseClipboard();\n        return;\n    }\n    ImWchar* wbuf_global = (ImWchar*)::GlobalLock(wbuf_handle);\n    ImTextStrFromUtf8(wbuf_global, wbuf_length, text, NULL);\n    ::GlobalUnlock(wbuf_handle);\n    ::EmptyClipboard();\n    if (::SetClipboardData(CF_UNICODETEXT, wbuf_handle) == NULL)\n        ::GlobalFree(wbuf_handle);\n    ::CloseClipboard();\n}\n\n#else\n\n// Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers\nstatic const char* GetClipboardTextFn_DefaultImpl(void*)\n{\n    ImGuiContext& g = *GImGui;\n    return g.PrivateClipboard.empty() ? NULL : g.PrivateClipboard.begin();\n}\n\n// Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers\nstatic void SetClipboardTextFn_DefaultImpl(void*, const char* text)\n{\n    ImGuiContext& g = *GImGui;\n    g.PrivateClipboard.clear();\n    const char* text_end = text + strlen(text);\n    g.PrivateClipboard.resize((int)(text_end - text) + 1);\n    memcpy(&g.PrivateClipboard[0], text, (size_t)(text_end - text));\n    g.PrivateClipboard[(int)(text_end - text)] = 0;\n}\n\n#endif\n\n//-----------------------------------------------------------------------------\n// [SECTION] METRICS/DEBUG WINDOW\n//-----------------------------------------------------------------------------\n\nstatic void RenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* viewport, const ImRect& bb)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    ImVec2 scale = bb.GetSize() / viewport->Size;\n    ImVec2 off = bb.Min - viewport->Pos * scale;\n    float alpha_mul = (viewport->Flags & ImGuiViewportFlags_Minimized) ? 0.30f : 1.00f;\n    window->DrawList->AddRectFilled(bb.Min, bb.Max, ImGui::GetColorU32(ImGuiCol_Border, alpha_mul * 0.40f));\n    for (int i = 0; i != g.Windows.Size; i++)\n    {\n        ImGuiWindow* thumb_window = g.Windows[i];\n        if (!thumb_window->WasActive || ((thumb_window->Flags & ImGuiWindowFlags_ChildWindow)))\n            continue;\n        if (thumb_window->SkipItems && (thumb_window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME-DOCK: Skip hidden docked windows. Identify those betters.\n            continue;\n        if (thumb_window->Viewport != viewport)\n            continue;\n\n        ImRect thumb_r = thumb_window->Rect();\n        ImRect title_r = thumb_window->TitleBarRect();\n        ImRect thumb_r_scaled = ImRect(ImFloor(off + thumb_r.Min * scale), ImFloor(off +  thumb_r.Max * scale));\n        ImRect title_r_scaled = ImRect(ImFloor(off + title_r.Min * scale), ImFloor(off +  ImVec2(title_r.Max.x, title_r.Min.y) * scale) + ImVec2(0,5)); // Exaggerate title bar height\n        thumb_r_scaled.ClipWithFull(bb);\n        title_r_scaled.ClipWithFull(bb);\n        const bool window_is_focused = (g.NavWindow && thumb_window->RootWindowForTitleBarHighlight == g.NavWindow->RootWindowForTitleBarHighlight);\n        window->DrawList->AddRectFilled(thumb_r_scaled.Min, thumb_r_scaled.Max, ImGui::GetColorU32(ImGuiCol_WindowBg, alpha_mul));\n        window->DrawList->AddRectFilled(title_r_scaled.Min, title_r_scaled.Max, ImGui::GetColorU32(window_is_focused ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg, alpha_mul));\n        window->DrawList->AddRect(thumb_r_scaled.Min, thumb_r_scaled.Max, ImGui::GetColorU32(ImGuiCol_Border, alpha_mul));\n        if (ImGuiWindow* window_for_title = GetWindowForTitleDisplay(thumb_window))\n            window->DrawList->AddText(g.Font, g.FontSize * 1.0f, title_r_scaled.Min, ImGui::GetColorU32(ImGuiCol_Text, alpha_mul), window_for_title->Name, ImGui::FindRenderedTextEnd(window_for_title->Name));\n    }\n    draw_list->AddRect(bb.Min, bb.Max, ImGui::GetColorU32(ImGuiCol_Border, alpha_mul));\n}\n\nvoid ImGui::ShowViewportThumbnails()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    // We don't display full monitor bounds (we could, but it often looks awkward), instead we display just enough to cover all of our viewports.\n    float SCALE = 1.0f / 8.0f;\n    ImRect bb_full;\n    //for (int n = 0; n < g.PlatformIO.Monitors.Size; n++)\n    //    bb_full.Add(GetPlatformMonitorMainRect(g.PlatformIO.Monitors[n]));\n    for (int n = 0; n < g.Viewports.Size; n++)\n        bb_full.Add(g.Viewports[n]->GetRect());\n    ImVec2 p = window->DC.CursorPos;\n    ImVec2 off = p - bb_full.Min * SCALE;\n    //for (int n = 0; n < g.PlatformIO.Monitors.Size; n++)\n    //    window->DrawList->AddRect(off + g.PlatformIO.Monitors[n].MainPos * SCALE, off + (g.PlatformIO.Monitors[n].MainPos + g.PlatformIO.Monitors[n].MainSize) * SCALE, ImGui::GetColorU32(ImGuiCol_Border));\n    for (int n = 0; n < g.Viewports.Size; n++)\n    {\n        ImGuiViewportP* viewport = g.Viewports[n];\n        ImRect viewport_draw_bb(off + (viewport->Pos) * SCALE, off + (viewport->Pos + viewport->Size) * SCALE);\n        RenderViewportThumbnail(window->DrawList, viewport, viewport_draw_bb);\n    }\n    ImGui::Dummy(bb_full.GetSize() * SCALE);\n}\n\nvoid ImGui::ShowMetricsWindow(bool* p_open)\n{\n    if (!ImGui::Begin(\"ImGui Metrics\", p_open))\n    {\n        ImGui::End();\n        return;\n    }\n\n    enum { RT_OuterRect, RT_OuterRectClipped, RT_InnerMainRect, RT_InnerClipRect, RT_ContentsRegionRect, RT_ContentsFullRect };\n    static bool show_windows_begin_order = false;\n    static bool show_windows_rects = false;\n    static int  show_windows_rect_type = RT_ContentsRegionRect;\n    static bool show_drawcmd_clip_rects = true;\n\n    ImGuiIO& io = ImGui::GetIO();\n    ImGui::Text(\"Dear ImGui %s\", ImGui::GetVersion());\n    ImGui::Text(\"Application average %.3f ms/frame (%.1f FPS)\", 1000.0f / io.Framerate, io.Framerate);\n    ImGui::Text(\"%d vertices, %d indices (%d triangles)\", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3);\n    ImGui::Text(\"%d active windows (%d visible)\", io.MetricsActiveWindows, io.MetricsRenderWindows);\n    ImGui::Text(\"%d active allocations\", io.MetricsActiveAllocations);\n    ImGui::Separator();\n\n    struct Funcs\n    {\n        static void NodeDrawList(ImGuiWindow* window, ImGuiViewportP* viewport, ImDrawList* draw_list, const char* label)\n        {\n            bool node_open = ImGui::TreeNode(draw_list, \"%s: '%s' %d vtx, %d indices, %d cmds\", label, draw_list->_OwnerName ? draw_list->_OwnerName : \"\", draw_list->VtxBuffer.Size, draw_list->IdxBuffer.Size, draw_list->CmdBuffer.Size);\n            if (draw_list == ImGui::GetWindowDrawList())\n            {\n                ImGui::SameLine();\n                ImGui::TextColored(ImVec4(1.0f,0.4f,0.4f,1.0f), \"CURRENTLY APPENDING\"); // Can't display stats for active draw list! (we don't have the data double-buffered)\n                if (node_open) ImGui::TreePop();\n                return;\n            }\n\n            ImDrawList* fg_draw_list = viewport ? GetForegroundDrawList(viewport) : NULL; // Render additional visuals into the top-most draw list\n            if (window && fg_draw_list && ImGui::IsItemHovered())\n                fg_draw_list->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255));\n            if (!node_open)\n                return;\n\n            int elem_offset = 0;\n            for (const ImDrawCmd* pcmd = draw_list->CmdBuffer.begin(); pcmd < draw_list->CmdBuffer.end(); elem_offset += pcmd->ElemCount, pcmd++)\n            {\n                if (pcmd->UserCallback == NULL && pcmd->ElemCount == 0)\n                    continue;\n                if (pcmd->UserCallback)\n                {\n                    ImGui::BulletText(\"Callback %p, user_data %p\", pcmd->UserCallback, pcmd->UserCallbackData);\n                    continue;\n                }\n                ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL;\n                bool pcmd_node_open = ImGui::TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), \"Draw %4d %s vtx, tex 0x%p, clip_rect (%4.0f,%4.0f)-(%4.0f,%4.0f)\", pcmd->ElemCount, draw_list->IdxBuffer.Size > 0 ? \"indexed\" : \"non-indexed\", pcmd->TextureId, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w);\n                if (show_drawcmd_clip_rects && fg_draw_list && ImGui::IsItemHovered())\n                {\n                    ImRect clip_rect = pcmd->ClipRect;\n                    ImRect vtxs_rect;\n                    for (int i = elem_offset; i < elem_offset + (int)pcmd->ElemCount; i++)\n                        vtxs_rect.Add(draw_list->VtxBuffer[idx_buffer ? idx_buffer[i] : i].pos);\n                    clip_rect.Floor(); fg_draw_list->AddRect(clip_rect.Min, clip_rect.Max, IM_COL32(255,255,0,255));\n                    vtxs_rect.Floor(); fg_draw_list->AddRect(vtxs_rect.Min, vtxs_rect.Max, IM_COL32(255,0,255,255));\n                }\n                if (!pcmd_node_open)\n                    continue;\n\n                // Display individual triangles/vertices. Hover on to get the corresponding triangle highlighted.\n                ImGuiListClipper clipper(pcmd->ElemCount/3); // Manually coarse clip our print out of individual vertices to save CPU, only items that may be visible.\n                while (clipper.Step())\n                    for (int prim = clipper.DisplayStart, idx_i = elem_offset + clipper.DisplayStart*3; prim < clipper.DisplayEnd; prim++)\n                    {\n                        char buf[300];\n                        char *buf_p = buf, *buf_end = buf + IM_ARRAYSIZE(buf);\n                        ImVec2 triangles_pos[3];\n                        for (int n = 0; n < 3; n++, idx_i++)\n                        {\n                            int vtx_i = idx_buffer ? idx_buffer[idx_i] : idx_i;\n                            ImDrawVert& v = draw_list->VtxBuffer[vtx_i];\n                            triangles_pos[n] = v.pos;\n                            buf_p += ImFormatString(buf_p, buf_end - buf_p, \"%s %04d: pos (%8.2f,%8.2f), uv (%.6f,%.6f), col %08X\\n\",\n                                (n == 0) ? \"idx\" : \"   \", idx_i, v.pos.x, v.pos.y, v.uv.x, v.uv.y, v.col);\n                        }\n                        ImGui::Selectable(buf, false);\n                        if (fg_draw_list && ImGui::IsItemHovered())\n                        {\n                            ImDrawListFlags backup_flags = fg_draw_list->Flags;\n                            fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines at is more readable for very large and thin triangles.\n                            fg_draw_list->AddPolyline(triangles_pos, 3, IM_COL32(255,255,0,255), true, 1.0f);\n                            fg_draw_list->Flags = backup_flags;\n                        }\n                    }\n                ImGui::TreePop();\n            }\n            ImGui::TreePop();\n        }\n\n        static void NodeColumns(const ImGuiColumns* columns)\n        {\n            if (!ImGui::TreeNode((void*)(uintptr_t)columns->ID, \"Columns Id: 0x%08X, Count: %d, Flags: 0x%04X\", columns->ID, columns->Count, columns->Flags))\n                return;\n            ImGui::BulletText(\"Width: %.1f (MinX: %.1f, MaxX: %.1f)\", columns->MaxX - columns->MinX, columns->MinX, columns->MaxX);\n            for (int column_n = 0; column_n < columns->Columns.Size; column_n++)\n                ImGui::BulletText(\"Column %02d: OffsetNorm %.3f (= %.1f px)\", column_n, columns->Columns[column_n].OffsetNorm, OffsetNormToPixels(columns, columns->Columns[column_n].OffsetNorm));\n            ImGui::TreePop();\n        }\n\n        static void NodeWindows(ImVector<ImGuiWindow*>& windows, const char* label)\n        {\n            if (!ImGui::TreeNode(label, \"%s (%d)\", label, windows.Size))\n                return;\n            for (int i = 0; i < windows.Size; i++)\n                Funcs::NodeWindow(windows[i], \"Window\");\n            ImGui::TreePop();\n        }\n\n        static void NodeWindow(ImGuiWindow* window, const char* label)\n        {\n            if (!ImGui::TreeNode(window, \"%s '%s', %d @ 0x%p\", label, window->Name, window->Active || window->WasActive, window))\n                return;\n            ImGuiWindowFlags flags = window->Flags;\n            NodeDrawList(window, window->Viewport, window->DrawList, \"DrawList\");\n            ImGui::BulletText(\"Pos: (%.1f,%.1f), Size: (%.1f,%.1f), SizeContents (%.1f,%.1f)\", window->Pos.x, window->Pos.y, window->Size.x, window->Size.y, window->SizeContents.x, window->SizeContents.y);\n            ImGui::BulletText(\"Flags: 0x%08X (%s%s%s%s%s%s%s%s%s..)\", flags,\n                (flags & ImGuiWindowFlags_ChildWindow)  ? \"Child \" : \"\",      (flags & ImGuiWindowFlags_Tooltip)     ? \"Tooltip \"   : \"\",  (flags & ImGuiWindowFlags_Popup) ? \"Popup \" : \"\",\n                (flags & ImGuiWindowFlags_Modal)        ? \"Modal \" : \"\",      (flags & ImGuiWindowFlags_ChildMenu)   ? \"ChildMenu \" : \"\",  (flags & ImGuiWindowFlags_NoSavedSettings) ? \"NoSavedSettings \" : \"\",\n                (flags & ImGuiWindowFlags_NoMouseInputs)? \"NoMouseInputs\":\"\", (flags & ImGuiWindowFlags_NoNavInputs) ? \"NoNavInputs\" : \"\", (flags & ImGuiWindowFlags_AlwaysAutoResize) ? \"AlwaysAutoResize\" : \"\");\n            ImGui::BulletText(\"Scroll: (%.2f/%.2f,%.2f/%.2f)\", window->Scroll.x, GetWindowScrollMaxX(window), window->Scroll.y, GetWindowScrollMaxY(window));\n            ImGui::BulletText(\"Active: %d/%d, WriteAccessed: %d, BeginOrderWithinContext: %d\", window->Active, window->WasActive, window->WriteAccessed, (window->Active || window->WasActive) ? window->BeginOrderWithinContext : -1);\n            ImGui::BulletText(\"Appearing: %d, Hidden: %d (CanSkip %d Cannot %d), SkipItems: %d\", window->Appearing, window->Hidden, window->HiddenFramesCanSkipItems, window->HiddenFramesCannotSkipItems, window->SkipItems);\n            ImGui::BulletText(\"NavLastIds: 0x%08X,0x%08X, NavLayerActiveMask: %X\", window->NavLastIds[0], window->NavLastIds[1], window->DC.NavLayerActiveMask);\n            ImGui::BulletText(\"NavLastChildNavWindow: %s\", window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : \"NULL\");\n            if (!window->NavRectRel[0].IsInverted())\n                ImGui::BulletText(\"NavRectRel[0]: (%.1f,%.1f)(%.1f,%.1f)\", window->NavRectRel[0].Min.x, window->NavRectRel[0].Min.y, window->NavRectRel[0].Max.x, window->NavRectRel[0].Max.y);\n            else\n                ImGui::BulletText(\"NavRectRel[0]: <None>\");\n            ImGui::BulletText(\"Viewport: %d%s, ViewportId: 0x%08X, ViewportPos: (%.1f,%.1f)\", window->Viewport ? window->Viewport->Idx : -1, window->ViewportOwned ? \" (Owned)\" : \"\", window->ViewportId, window->ViewportPos.x, window->ViewportPos.y);\n            ImGui::BulletText(\"ViewportMonitor: %d\", window->Viewport ? window->Viewport->PlatformMonitor : -1);\n            ImGui::BulletText(\"DockId: 0x%04X, DockOrder: %d, %s: 0x%p, Act: %d, Vis: %d\", window->DockId, window->DockOrder, window->DockNodeAsHost ? \"DockNodeAsHost\" : \"DockNode\", window->DockNodeAsHost ? window->DockNodeAsHost : window->DockNode, window->DockIsActive, window->DockTabIsVisible);\n            if (window->RootWindow != window) NodeWindow(window->RootWindow, \"RootWindow\");\n            if (window->RootWindowDockStop != window->RootWindow) NodeWindow(window->RootWindowDockStop, \"RootWindowDockStop\");\n            if (window->ParentWindow != NULL) NodeWindow(window->ParentWindow, \"ParentWindow\");\n            if (window->DC.ChildWindows.Size > 0) NodeWindows(window->DC.ChildWindows, \"ChildWindows\");\n            if (window->ColumnsStorage.Size > 0 && ImGui::TreeNode(\"Columns\", \"Columns sets (%d)\", window->ColumnsStorage.Size))\n            {\n                for (int n = 0; n < window->ColumnsStorage.Size; n++)\n                    NodeColumns(&window->ColumnsStorage[n]);\n                ImGui::TreePop();\n            }\n            ImGui::BulletText(\"Storage: %d bytes\", window->StateStorage.Data.Size * (int)sizeof(ImGuiStorage::Pair));\n            ImGui::TreePop();\n        }\n\n        static void NodeViewport(ImGuiViewportP* viewport)\n        {\n            ImGui::SetNextTreeNodeOpen(true, ImGuiCond_Once);\n            if (ImGui::TreeNode((void*)(intptr_t)viewport->ID, \"Viewport #%d, ID: 0x%08X, Parent: 0x%08X, Window: \\\"%s\\\"\", viewport->Idx, viewport->ID, viewport->ParentViewportId, viewport->Window ? viewport->Window->Name : \"N/A\"))\n            {\n                ImGuiWindowFlags flags = viewport->Flags;\n                ImGui::BulletText(\"Pos: (%.0f,%.0f), Size: (%.0f,%.0f), Monitor: %d, DpiScale: %.0f%%\", viewport->Pos.x, viewport->Pos.y, viewport->Size.x, viewport->Size.y, viewport->PlatformMonitor, viewport->DpiScale * 100.0f);\n                if (viewport->Idx > 0) { ImGui::SameLine(); if (ImGui::SmallButton(\"Reset Pos\")) { viewport->Pos = ImVec2(200,200); if (viewport->Window) viewport->Window->Pos = ImVec2(200,200); } }\n                ImGui::BulletText(\"Flags: 0x%04X =%s%s%s%s%s%s\", viewport->Flags,\n                    (flags & ImGuiViewportFlags_CanHostOtherWindows) ? \" CanHostOtherWindows\" : \"\", (flags & ImGuiViewportFlags_NoDecoration) ? \" NoDecoration\" : \"\",\n                    (flags & ImGuiViewportFlags_NoFocusOnAppearing)  ? \" NoFocusOnAppearing\"  : \"\", (flags & ImGuiViewportFlags_NoInputs)     ? \" NoInputs\"     : \"\",\n                    (flags & ImGuiViewportFlags_NoRendererClear)     ? \" NoRendererClear\"     : \"\", (flags & ImGuiViewportFlags_Minimized)    ? \" Minimized\"    : \"\");\n                for (int layer_i = 0; layer_i < IM_ARRAYSIZE(viewport->DrawDataBuilder.Layers); layer_i++)\n                    for (int draw_list_i = 0; draw_list_i < viewport->DrawDataBuilder.Layers[layer_i].Size; draw_list_i++)\n                        Funcs::NodeDrawList(NULL, viewport, viewport->DrawDataBuilder.Layers[layer_i][draw_list_i], \"DrawList\");\n                ImGui::TreePop();\n            }\n        }\n    };\n\n    // Access private state, we are going to display the draw lists from last frame\n    ImGuiContext& g = *GImGui;\n    Funcs::NodeWindows(g.Windows, \"Windows\");\n    if (ImGui::TreeNode(\"Viewport\", \"Viewports (%d)\", g.Viewports.Size))\n    {\n        ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing());\n        ImGui::ShowViewportThumbnails();\n        ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing());\n        if (g.PlatformIO.Monitors.Size > 0 && ImGui::TreeNode(\"Monitors\", \"Monitors (%d)\", g.PlatformIO.Monitors.Size))\n        {\n            ImGui::TextWrapped(\"(When viewports are enabled, imgui needs uses monitor data to position popup/tooltips so they don't straddle monitors.)\");\n            for (int i = 0; i < g.PlatformIO.Monitors.Size; i++)\n            {\n                const ImGuiPlatformMonitor& mon = g.PlatformIO.Monitors[i];\n                ImGui::BulletText(\"Monitor #%d: DPI %.0f%%\\n MainMin (%.0f,%.0f), MainMax (%.0f,%.0f), MainSize (%.0f,%.0f)\\n WorkMin (%.0f,%.0f), WorkMax (%.0f,%.0f), WorkSize (%.0f,%.0f)\", \n                    i, mon.DpiScale * 100.0f, \n                    mon.MainPos.x, mon.MainPos.y, mon.MainPos.x + mon.MainSize.x, mon.MainPos.y + mon.MainSize.y, mon.MainSize.x, mon.MainSize.y,\n                    mon.WorkPos.x, mon.WorkPos.y, mon.WorkPos.x + mon.WorkSize.x, mon.WorkPos.y + mon.WorkSize.y, mon.WorkSize.x, mon.WorkSize.y);\n            }\n            ImGui::TreePop();\n        }\n        for (int i = 0; i < g.Viewports.Size; i++)\n            Funcs::NodeViewport(g.Viewports[i]);\n        ImGui::TreePop();\n    }\n\n    if (ImGui::TreeNode(\"Popups\", \"Popups (%d)\", g.OpenPopupStack.Size))\n    {\n        for (int i = 0; i < g.OpenPopupStack.Size; i++)\n        {\n            ImGuiWindow* window = g.OpenPopupStack[i].Window;\n            ImGui::BulletText(\"PopupID: %08x, Window: '%s'%s%s\", g.OpenPopupStack[i].PopupId, window ? window->Name : \"NULL\", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? \" ChildWindow\" : \"\", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? \" ChildMenu\" : \"\");\n        }\n        ImGui::TreePop();\n    }\n\n    if (ImGui::TreeNode(\"Docking & Tab Bars\"))\n    {\n        ShowDockingDebug();\n        ImGui::TreePop();\n    }\n\n    if (ImGui::TreeNode(\"Internal state\"))\n    {\n        const char* input_source_names[] = { \"None\", \"Mouse\", \"Nav\", \"NavKeyboard\", \"NavGamepad\" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT);\n        ImGui::Text(\"HoveredWindow: '%s'\", g.HoveredWindow ? g.HoveredWindow->Name : \"NULL\");\n        ImGui::Text(\"HoveredRootWindow: '%s'\", g.HoveredRootWindow ? g.HoveredRootWindow->Name : \"NULL\");\n        ImGui::Text(\"HoveredWindowUnderMovingWindow: '%s'\", g.HoveredWindowUnderMovingWindow ? g.HoveredWindowUnderMovingWindow->Name : \"NULL\");\n        ImGui::Text(\"HoveredId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d\", g.HoveredId, g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Data is \"in-flight\" so depending on when the Metrics window is called we may see current frame information or not\n        ImGui::Text(\"ActiveId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d, Source: %s\", g.ActiveId, g.ActiveIdPreviousFrame, g.ActiveIdTimer, g.ActiveIdAllowOverlap, input_source_names[g.ActiveIdSource]);\n        ImGui::Text(\"ActiveIdWindow: '%s'\", g.ActiveIdWindow ? g.ActiveIdWindow->Name : \"NULL\");\n        ImGui::Text(\"MovingWindow: '%s'\", g.MovingWindow ? g.MovingWindow->Name : \"NULL\");\n        ImGui::Text(\"NavWindow: '%s'\", g.NavWindow ? g.NavWindow->Name : \"NULL\");\n        ImGui::Text(\"NavId: 0x%08X, NavLayer: %d\", g.NavId, g.NavLayer);\n        ImGui::Text(\"NavInputSource: %s\", input_source_names[g.NavInputSource]);\n        ImGui::Text(\"NavActive: %d, NavVisible: %d\", g.IO.NavActive, g.IO.NavVisible);\n        ImGui::Text(\"NavActivateId: 0x%08X, NavInputId: 0x%08X\", g.NavActivateId, g.NavInputId);\n        ImGui::Text(\"NavDisableHighlight: %d, NavDisableMouseHover: %d\", g.NavDisableHighlight, g.NavDisableMouseHover);\n        ImGui::Text(\"NavWindowingTarget: '%s'\", g.NavWindowingTarget ? g.NavWindowingTarget->Name : \"NULL\");\n        ImGui::Text(\"DragDrop: %d, SourceId = 0x%08X, Payload \\\"%s\\\" (%d bytes)\", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize);\n        ImGui::Text(\"MouseViewport: 0x%08X (UserHovered 0x%08X, LastHovered 0x%08X)\", g.MouseViewport->ID, g.IO.MouseHoveredViewport, g.MouseLastHoveredViewport ? g.MouseLastHoveredViewport->ID : 0);\n        ImGui::TreePop();\n    }\n\n    if (ImGui::TreeNode(\"Tools\"))\n    {\n        ImGui::Checkbox(\"Show windows begin order\", &show_windows_begin_order);\n        ImGui::Checkbox(\"Show windows rectangles\", &show_windows_rects);\n        ImGui::SameLine();\n        ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12);\n        show_windows_rects |= ImGui::Combo(\"##rects_type\", &show_windows_rect_type, \"OuterRect\\0\" \"OuterRectClipped\\0\" \"InnerMainRect\\0\" \"InnerClipRect\\0\" \"ContentsRegionRect\\0\");\n        ImGui::Checkbox(\"Show clipping rectangle when hovering ImDrawCmd node\", &show_drawcmd_clip_rects);\n        ImGui::TreePop();\n    }\n\n    if (show_windows_rects || show_windows_begin_order)\n    {\n        for (int n = 0; n < g.Windows.Size; n++)\n        {\n            ImGuiWindow* window = g.Windows[n];\n            if (!window->WasActive)\n                continue;\n            ImDrawList* draw_list = GetForegroundDrawList(window);\n            if (show_windows_rects)\n            {\n                ImRect r;\n                if (show_windows_rect_type == RT_OuterRect)                 { r = window->Rect(); }\n                else if (show_windows_rect_type == RT_OuterRectClipped)     { r = window->OuterRectClipped; }\n                else if (show_windows_rect_type == RT_InnerMainRect)        { r = window->InnerMainRect; }\n                else if (show_windows_rect_type == RT_InnerClipRect)        { r = window->InnerClipRect; }\n                else if (show_windows_rect_type == RT_ContentsRegionRect)   { r = window->ContentsRegionRect; }\n                draw_list->AddRect(r.Min, r.Max, IM_COL32(255, 0, 128, 255));\n            }\n            if (show_windows_begin_order && !(window->Flags & ImGuiWindowFlags_ChildWindow))\n            {\n                char buf[32];\n                ImFormatString(buf, IM_ARRAYSIZE(buf), \"%d\", window->BeginOrderWithinContext);\n                float font_size = ImGui::GetFontSize();\n                draw_list->AddRectFilled(window->Pos, window->Pos + ImVec2(font_size, font_size), IM_COL32(200, 100, 100, 255));\n                draw_list->AddText(window->Pos, IM_COL32(255, 255, 255, 255), buf);\n            }\n        }\n    }\n    ImGui::End();\n}\n\nvoid ImGui::ShowDockingDebug()\n{\n    ImGuiContext* ctx = GImGui;\n    ImGuiContext& g = *ctx;\n    ImGuiDockContext* dc = ctx->DockContext;\n\n    struct Funcs\n    {\n        static void NodeDockNode(ImGuiDockNode* node, const char* label)\n        {\n            ImGuiContext& g = *GImGui;\n            ImGui::SetNextTreeNodeOpen(true, ImGuiCond_Once);\n            bool open; \n            if (node->Windows.Size > 0)\n                open = ImGui::TreeNode((void*)(intptr_t)node->ID, \"%s 0x%04X%s: %d windows (vis: '%s')\", label, node->ID, node->IsVisible ? \"\" : \" (hidden)\", node->Windows.Size, node->VisibleWindow ? node->VisibleWindow->Name : \"NULL\");\n            else\n                open = ImGui::TreeNode((void*)(intptr_t)node->ID, \"%s 0x%04X%s: %s split (vis: '%s')\", label, node->ID, node->IsVisible ? \"\" : \" (hidden)\", (node->SplitAxis == ImGuiAxis_X) ? \"horizontal\" : (node->SplitAxis == ImGuiAxis_Y) ? \"vertical\" : \"n/a\", node->VisibleWindow ? node->VisibleWindow->Name : \"NULL\");\n            if (open)\n            {\n                IM_ASSERT(node->ChildNodes[0] == NULL || node->ChildNodes[0]->ParentNode == node);\n                IM_ASSERT(node->ChildNodes[1] == NULL || node->ChildNodes[1]->ParentNode == node);\n                ImGui::BulletText(\"Pos (%.0f,%.0f), Size (%.0f, %.0f) Ref (%.0f, %.0f)\",\n                    node->Pos.x, node->Pos.y, node->Size.x, node->Size.y, node->SizeRef.x, node->SizeRef.y);\n                ImGui::BulletText(\"VisibleWindow: 0x%08X %s\", node->VisibleWindow ? node->VisibleWindow->ID : 0, node->VisibleWindow ? node->VisibleWindow->Name : \"NULL\");\n                ImGui::BulletText(\"SelectedTabID: 0x%08X, LastFocusedNodeID: 0x%08X\", node->SelectedTabID, node->LastFocusedNodeID);\n                ImGui::BulletText(\"Misc:%s%s%s%s\", node->IsDockSpace() ? \" IsDockSpace\" : \"\", node->IsCentralNode() ? \" IsCentralNode\" : \"\", (g.FrameCount - node->LastFrameAlive < 2) ? \" IsAlive\" : \"\", (g.FrameCount - node->LastFrameActive < 2) ? \" IsActive\" : \"\");\n                if (ImGui::TreeNode(\"flags\", \"LocalFlags: 0x%04X SharedFlags: 0x%04X\", node->LocalFlags, node->SharedFlags))\n                {\n                    ImGui::CheckboxFlags(\"LocalFlags: NoSplit\",     (unsigned int*)&node->LocalFlags, ImGuiDockNodeFlags_NoSplit);\n                    ImGui::CheckboxFlags(\"LocalFlags: NoResize\",    (unsigned int*)&node->LocalFlags, ImGuiDockNodeFlags_NoResize);\n                    ImGui::CheckboxFlags(\"LocalFlags: NoTabBar\",    (unsigned int*)&node->LocalFlags, ImGuiDockNodeFlags_NoTabBar);\n                    ImGui::CheckboxFlags(\"LocalFlags: HiddenTabBar\",(unsigned int*)&node->LocalFlags, ImGuiDockNodeFlags_HiddenTabBar);\n                    ImGui::TreePop();\n                }\n                if (node->ChildNodes[0])\n                    NodeDockNode(node->ChildNodes[0], \"Child[0]\");\n                if (node->ChildNodes[1])\n                    NodeDockNode(node->ChildNodes[1], \"Child[1]\");\n                if (node->TabBar)\n                    NodeTabBar(node->TabBar);\n                ImGui::TreePop();\n            }\n        }\n\n        static void NodeTabBar(ImGuiTabBar* tab_bar)\n        {\n            // Standalone tab bars (not associated to docking/windows functionality) currently hold no discernible strings.\n            char buf[256];\n            char* p = buf;\n            const char* buf_end = buf + IM_ARRAYSIZE(buf);\n            p += ImFormatString(p, buf_end - p, \"TabBar (%d tabs)%s\", \n                tab_bar->Tabs.Size, (tab_bar->PrevFrameVisible < ImGui::GetFrameCount() - 2) ? \" *Inactive*\" : \"\");\n            if (tab_bar->Flags & ImGuiTabBarFlags_DockNode)\n            {\n                p += ImFormatString(p, buf_end - p, \"  { \");\n                for (int tab_n = 0; tab_n < ImMin(tab_bar->Tabs.Size, 3); tab_n++)\n                    p += ImFormatString(p, buf_end - p, \"%s'%s'\", tab_n > 0 ? \", \" : \"\", tab_bar->Tabs[tab_n].Window->Name);\n                p += ImFormatString(p, buf_end - p, (tab_bar->Tabs.Size > 3) ? \" ... }\" : \" } \");\n            }\n            if (ImGui::TreeNode(tab_bar, \"%s\", buf))\n            {\n                for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++)\n                {\n                    const ImGuiTabItem* tab = &tab_bar->Tabs[tab_n];\n                    ImGui::PushID(tab);\n                    if (ImGui::SmallButton(\"<\")) { TabBarQueueChangeTabOrder(tab_bar, tab, -1); } ImGui::SameLine(0, 2);\n                    if (ImGui::SmallButton(\">\")) { TabBarQueueChangeTabOrder(tab_bar, tab, +1); } ImGui::SameLine();\n                    ImGui::Text(\"%02d%c Tab 0x%08X '%s'\", tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, tab->Window ? tab->Window->Name : \"N/A\");\n                    ImGui::PopID();\n                }\n                ImGui::TreePop();\n            }\n        }\n    };\n\n    static bool show_window_dock_info = false;\n    ImGui::Checkbox(\"Ctrl shows window dock info\", &show_window_dock_info);\n\n    if (ImGui::TreeNode(\"Dock nodes\"))\n    {\n        if (ImGui::SmallButton(\"Clear settings\"))   { DockContextClearNodes(&g, 0, true); }\n        ImGui::SameLine();\n        if (ImGui::SmallButton(\"Rebuild all\"))      { dc->WantFullRebuild = true; }\n        for (int n = 0; n < dc->Nodes.Data.Size; n++)\n            if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p)\n                if (node->IsRootNode())\n                    Funcs::NodeDockNode(node, \"Node\");\n        ImGui::TreePop();\n    }\n\n    if (ImGui::TreeNode(\"TabBars\", \"Tab Bars (%d)\", g.TabBars.Data.Size))\n    {\n        for (int n = 0; n < g.TabBars.Data.Size; n++)\n            Funcs::NodeTabBar(g.TabBars.GetByIndex(n));\n        ImGui::TreePop();\n    }\n\n    if (ImGui::TreeNode(\"Settings\"))\n    {\n        if (ImGui::SmallButton(\"Refresh\"))\n            SaveIniSettingsToMemory();\n        ImGui::SameLine();\n        if (ImGui::SmallButton(\"Save to disk\"))\n            SaveIniSettingsToDisk(g.IO.IniFilename);\n        ImGui::Separator();\n        ImGui::Text(\"Docked Windows:\");\n        for (int n = 0; n < g.SettingsWindows.Size; n++)\n            if (g.SettingsWindows[n].DockId != 0)\n                ImGui::BulletText(\"Window '%s' -> DockId %08X\", g.SettingsWindows[n].Name, g.SettingsWindows[n].DockId);\n        ImGui::Separator();\n        ImGui::Text(\"Dock Nodes:\");\n        for (int n = 0; n < dc->SettingsNodes.Size; n++)\n        {\n            ImGuiDockNodeSettings* settings = &dc->SettingsNodes[n];\n            const char* selected_tab_name = NULL;\n            if (settings->SelectedTabID)\n            {\n                if (ImGuiWindow* window = FindWindowByID(settings->SelectedTabID))\n                    selected_tab_name = window->Name;\n                else if (ImGuiWindowSettings* window_settings = FindWindowSettings(settings->SelectedTabID))\n                    selected_tab_name = window_settings->Name;\n            }\n            ImGui::BulletText(\"Node %08X, Parent %08X, SelectedTab %08X ('%s')\", settings->ID, settings->ParentID, settings->SelectedTabID, selected_tab_name ? selected_tab_name : settings->SelectedTabID ? \"N/A\" : \"\");\n        }\n        ImGui::TreePop();\n    }\n\n    if (g.IO.KeyCtrl && show_window_dock_info)\n    {\n        for (int n = 0; n < dc->Nodes.Data.Size; n++)\n            if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p)\n            {\n                ImGuiDockNode* root_node = DockNodeGetRootNode(node);\n                if (ImGuiDockNode* hovered_node = DockNodeTreeFindNodeByPos(root_node, g.IO.MousePos))\n                    if (hovered_node != node)\n                        continue;\n                char buf[64] = \"\";\n                char* p = buf;\n                ImDrawList* overlay_draw_list = node->HostWindow ? GetForegroundDrawList(node->HostWindow) : GetForegroundDrawList((ImGuiViewportP*)GetMainViewport());\n                p += ImFormatString(p, buf + IM_ARRAYSIZE(buf) - p, \"DockId: %X%s\\n\", node->ID, node->IsCentralNode() ? \" *CentralNode*\" : \"\");\n                p += ImFormatString(p, buf + IM_ARRAYSIZE(buf) - p, \"Size: (%.0f, %.0f)\\n\", node->Size.x, node->Size.y);\n                p += ImFormatString(p, buf + IM_ARRAYSIZE(buf) - p, \"SizeRef: (%.0f, %.0f)\\n\", node->SizeRef.x, node->SizeRef.y);\n                int depth = DockNodeGetDepth(node);\n                overlay_draw_list->AddRect(node->Pos + ImVec2(3,3) * (float)depth, node->Pos + node->Size - ImVec2(3,3) * (float)depth, IM_COL32(200, 100, 100, 255));\n                ImVec2 pos = node->Pos + ImVec2(3,3) * (float)depth;\n                overlay_draw_list->AddRectFilled(pos - ImVec2(1, 1), pos + CalcTextSize(buf) + ImVec2(1, 1), IM_COL32(200, 100, 100, 255));\n                overlay_draw_list->AddText(NULL, 0.0f, pos, IM_COL32(255, 255, 255, 255), buf);\n            }\n    }\n}\n\n//-----------------------------------------------------------------------------\n\n// Include imgui_user.inl at the end of imgui.cpp to access private data/functions that aren't exposed.\n// Prefer just including imgui_internal.h from your code rather than using this define. If a declaration is missing from imgui_internal.h add it or request it on the github.\n#ifdef IMGUI_INCLUDE_IMGUI_USER_INL\n#include \"imgui_user.inl\"\n#endif\n\n//-----------------------------------------------------------------------------\n"
  },
  {
    "path": "src/imgui/imgui.hpp",
    "content": "// dear imgui, v1.70 WIP\n// (headers)\n\n// See imgui.cpp file for documentation.\n// Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code.\n// Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase.\n// Get latest version at https://github.com/ocornut/imgui\n\n/*\n\nIndex of this file:\n// Header mess\n// Forward declarations and basic types\n// ImGui API (Dear ImGui end-user API)\n// Flags & Enumerations\n// Memory allocations macros\n// ImVector<>\n// ImGuiStyle\n// ImGuiIO\n// Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload, ImGuiWindowClass)\n// Obsolete functions\n// Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor)\n// Draw List API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListFlags, ImDrawList, ImDrawData)\n// Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont)\n// Platform interface for multi-viewport support (ImGuiPlatformMonitor, ImGuiPlatformIO, ImGuiViewport)\n\n*/\n\n#pragma once\n\n#pragma warning(push, 0)\n\n// Configuration file with compile-time options (edit imconfig.h or define IMGUI_USER_CONFIG to your own filename)\n#ifdef IMGUI_USER_CONFIG\n#include IMGUI_USER_CONFIG\n#endif\n#if !defined(IMGUI_DISABLE_INCLUDE_IMCONFIG_H) || defined(IMGUI_INCLUDE_IMCONFIG_H)\n#include \"imconfig.hpp\"\n#endif\n\n//-----------------------------------------------------------------------------\n// Header mess\n//-----------------------------------------------------------------------------\n\n#include <float.h>                  // FLT_MAX\n#include <stdarg.h>                 // va_list\n#include <stddef.h>                 // ptrdiff_t, NULL\n#include <string.h>                 // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp\n\n// Version\n// (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens)\n#define IMGUI_VERSION               \"1.70 WIP\"\n#define IMGUI_VERSION_NUM           16991\n#define IMGUI_CHECKVERSION()        ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert))\n#define IMGUI_HAS_VIEWPORT          1 // Viewport WIP branch\n#define IMGUI_HAS_DOCK              1 // Docking WIP branch\n\n// Define attributes of all API symbols declarations (e.g. for DLL under Windows)\n// IMGUI_API is used for core imgui functions, IMGUI_IMPL_API is used for the default bindings files (imgui_impl_xxx.h)\n#ifndef IMGUI_API\n#define IMGUI_API\n#endif\n#ifndef IMGUI_IMPL_API\n#define IMGUI_IMPL_API              IMGUI_API\n#endif\n\n// Helper Macros\n#ifndef IM_ASSERT\n#include <assert.h>\n#define IM_ASSERT(_EXPR)            assert(_EXPR)                               // You can override the default assert handler by editing imconfig.h\n#endif\n#if defined(__clang__) || defined(__GNUC__)\n#define IM_FMTARGS(FMT)             __attribute__((format(printf, FMT, FMT+1))) // Apply printf-style warnings to user functions.\n#define IM_FMTLIST(FMT)             __attribute__((format(printf, FMT, 0)))\n#else\n#define IM_FMTARGS(FMT)\n#define IM_FMTLIST(FMT)\n#endif\n#define IM_ARRAYSIZE(_ARR)          ((int)(sizeof(_ARR)/sizeof(*_ARR)))         // Size of a static C-style array. Don't use on pointers!\n#define IM_OFFSETOF(_TYPE,_MEMBER)  ((size_t)&(((_TYPE*)0)->_MEMBER))           // Offset of _MEMBER within _TYPE. Standardized as offsetof() in modern C++.\n#define IM_UNUSED(_VAR)             ((void)_VAR)                                // Used to silence \"unused variable warnings\". Often useful as asserts may be stripped out from final builds.\n\n// Warnings\n#if defined(__clang__)\n#pragma clang diagnostic push\n#pragma clang diagnostic ignored \"-Wold-style-cast\"\n#if __has_warning(\"-Wzero-as-null-pointer-constant\")\n#pragma clang diagnostic ignored \"-Wzero-as-null-pointer-constant\"\n#endif\n#elif defined(__GNUC__) && __GNUC__ >= 8\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wclass-memaccess\"\n#endif\n\n//-----------------------------------------------------------------------------\n// Forward declarations and basic types\n//-----------------------------------------------------------------------------\n\nstruct ImDrawChannel;               // Temporary storage for ImDrawList ot output draw commands out of order, used by ImDrawList::ChannelsSplit()\nstruct ImDrawCmd;                   // A single draw command within a parent ImDrawList (generally maps to 1 GPU draw call, unless it is a callback)\nstruct ImDrawData;                  // All draw command lists required to render the frame + pos/size coordinates to use for the projection matrix.\nstruct ImDrawList;                  // A single draw command list (generally one per window, conceptually you may see this as a dynamic \"mesh\" builder)\nstruct ImDrawListSharedData;        // Data shared among multiple draw lists (typically owned by parent ImGui context, but you may create one yourself)\nstruct ImDrawVert;                  // A single vertex (pos + uv + col = 20 bytes by default. Override layout with IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT)\nstruct ImFont;                      // Runtime data for a single font within a parent ImFontAtlas\nstruct ImFontAtlas;                 // Runtime data for multiple fonts, bake multiple fonts into a single texture, TTF/OTF font loader\nstruct ImFontConfig;                // Configuration data when adding a font or merging fonts\nstruct ImFontGlyph;                 // A single font glyph (code point + coordinates within in ImFontAtlas + offset)\nstruct ImFontGlyphRangesBuilder;    // Helper to build glyph ranges from text/string data\nstruct ImColor;                     // Helper functions to create a color that can be converted to either u32 or float4 (*OBSOLETE* please avoid using)\nstruct ImGuiContext;                // Dear ImGui context (opaque structure, unless including imgui_internal.h)\nstruct ImGuiIO;                     // Main configuration and I/O between your application and ImGui\nstruct ImGuiInputTextCallbackData;  // Shared state of InputText() when using custom ImGuiInputTextCallback (rare/advanced use)\nstruct ImGuiListClipper;            // Helper to manually clip large list of items\nstruct ImGuiOnceUponAFrame;         // Helper for running a block of code not more than once a frame, used by IMGUI_ONCE_UPON_A_FRAME macro\nstruct ImGuiPayload;                // User data payload for drag and drop operations\nstruct ImGuiPlatformIO;             // Multi-viewport support: interface for Platform/Renderer back-ends + viewports to render\nstruct ImGuiPlatformMonitor;        // Multi-viewport support: user-provided bounds for each connected monitor/display. Used when positioning popups and tooltips to avoid them straddling monitors\nstruct ImGuiSizeCallbackData;       // Callback data when using SetNextWindowSizeConstraints() (rare/advanced use)\nstruct ImGuiStorage;                // Helper for key->value storage\nstruct ImGuiStyle;                  // Runtime data for styling/colors\nstruct ImGuiTextBuffer;             // Helper to hold and append into a text buffer (~string builder)\nstruct ImGuiTextFilter;             // Helper to parse and apply text filters (e.g. \"aaaaa[,bbbb][,ccccc]\")\nstruct ImGuiViewport;               // Viewport (generally ~1 per window to output to at the OS level. Need per-platform support to use multiple viewports)\nstruct ImGuiWindowClass;            // Window class (rare/advanced uses: provide hints to the platform back-end via altered viewport flags and parent/child info)\n\n// Typedefs and Enums/Flags (declared as int for compatibility with old C++, to allow using as flags and to not pollute the top of this file)\n// Use your programming IDE \"Go to definition\" facility on the names of the center columns to find the actual flags/enum lists.\n#ifndef ImTextureID\ntypedef void* ImTextureID;          // User data to identify a texture (this is whatever to you want it to be! read the FAQ about ImTextureID in imgui.cpp)\n#endif\ntypedef unsigned int ImGuiID;       // Unique ID used by widgets (typically hashed from a stack of string)\ntypedef unsigned short ImWchar;     // A single U16 character for keyboard input/display. We encode them as multi bytes UTF-8 when used in strings.\ntypedef int ImGuiCol;               // -> enum ImGuiCol_             // Enum: A color identifier for styling\ntypedef int ImGuiCond;              // -> enum ImGuiCond_            // Enum: A condition for Set*()\ntypedef int ImGuiDataType;          // -> enum ImGuiDataType_        // Enum: A primary data type\ntypedef int ImGuiDir;               // -> enum ImGuiDir_             // Enum: A cardinal direction\ntypedef int ImGuiKey;               // -> enum ImGuiKey_             // Enum: A key identifier (ImGui-side enum)\ntypedef int ImGuiNavInput;          // -> enum ImGuiNavInput_        // Enum: An input identifier for navigation\ntypedef int ImGuiMouseCursor;       // -> enum ImGuiMouseCursor_     // Enum: A mouse cursor identifier\ntypedef int ImGuiStyleVar;          // -> enum ImGuiStyleVar_        // Enum: A variable identifier for styling\ntypedef int ImDrawCornerFlags;      // -> enum ImDrawCornerFlags_    // Flags: for ImDrawList::AddRect*() etc.\ntypedef int ImDrawListFlags;        // -> enum ImDrawListFlags_      // Flags: for ImDrawList\ntypedef int ImFontAtlasFlags;       // -> enum ImFontAtlasFlags_     // Flags: for ImFontAtlas\ntypedef int ImGuiBackendFlags;      // -> enum ImGuiBackendFlags_    // Flags: for io.BackendFlags\ntypedef int ImGuiColorEditFlags;    // -> enum ImGuiColorEditFlags_  // Flags: for ColorEdit*(), ColorPicker*()\ntypedef int ImGuiColumnsFlags;      // -> enum ImGuiColumnsFlags_    // Flags: for Columns(), BeginColumns()\ntypedef int ImGuiConfigFlags;       // -> enum ImGuiConfigFlags_     // Flags: for io.ConfigFlags\ntypedef int ImGuiComboFlags;        // -> enum ImGuiComboFlags_      // Flags: for BeginCombo()\ntypedef int ImGuiDockNodeFlags;     // -> enum ImGuiDockNodeFlags_   // Flags: for DockSpace()                   \ntypedef int ImGuiDragDropFlags;     // -> enum ImGuiDragDropFlags_   // Flags: for *DragDrop*()\ntypedef int ImGuiFocusedFlags;      // -> enum ImGuiFocusedFlags_    // Flags: for IsWindowFocused()\ntypedef int ImGuiHoveredFlags;      // -> enum ImGuiHoveredFlags_    // Flags: for IsItemHovered(), IsWindowHovered() etc.\ntypedef int ImGuiInputTextFlags;    // -> enum ImGuiInputTextFlags_  // Flags: for InputText*()\ntypedef int ImGuiSelectableFlags;   // -> enum ImGuiSelectableFlags_ // Flags: for Selectable()\ntypedef int ImGuiTabBarFlags;       // -> enum ImGuiTabBarFlags_     // Flags: for BeginTabBar()\ntypedef int ImGuiTabItemFlags;      // -> enum ImGuiTabItemFlags_    // Flags: for BeginTabItem()\ntypedef int ImGuiTreeNodeFlags;     // -> enum ImGuiTreeNodeFlags_   // Flags: for TreeNode*(),CollapsingHeader()\ntypedef int ImGuiViewportFlags;     // -> enum ImGuiViewportFlags_   // Flags: for ImGuiViewport\ntypedef int ImGuiWindowFlags;       // -> enum ImGuiWindowFlags_     // Flags: for Begin*()\ntypedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData *data);\ntypedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data);\n\n// Scalar data types\ntypedef signed char         ImS8;   // 8-bit signed integer == char\ntypedef unsigned char       ImU8;   // 8-bit unsigned integer\ntypedef signed short        ImS16;  // 16-bit signed integer\ntypedef unsigned short      ImU16;  // 16-bit unsigned integer\ntypedef signed int          ImS32;  // 32-bit signed integer == int\ntypedef unsigned int        ImU32;  // 32-bit unsigned integer (often used to store packed colors)\n#if defined(_MSC_VER) && !defined(__clang__)\ntypedef signed   __int64    ImS64;  // 64-bit signed integer (pre and post C++11 with Visual Studio)\ntypedef unsigned __int64    ImU64;  // 64-bit unsigned integer (pre and post C++11 with Visual Studio)\n#elif (defined(__clang__) || defined(__GNUC__)) && (__cplusplus < 201100)\n#include <stdint.h>\ntypedef int64_t             ImS64;  // 64-bit signed integer (pre C++11)\ntypedef uint64_t            ImU64;  // 64-bit unsigned integer (pre C++11)\n#else\ntypedef signed   long long  ImS64;  // 64-bit signed integer (post C++11)\ntypedef unsigned long long  ImU64;  // 64-bit unsigned integer (post C++11)\n#endif\n\n// 2D vector (often used to store positions, sizes, etc.)\nstruct ImVec2\n{\n    float     x, y;\n    ImVec2()  { x = y = 0.0f; }\n    ImVec2(float _x, float _y) { x = _x; y = _y; }\n    float  operator[] (size_t idx) const { IM_ASSERT(idx <= 1); return (&x)[idx]; }    // We very rarely use this [] operator, the assert overhead is fine.\n    float& operator[] (size_t idx)       { IM_ASSERT(idx <= 1); return (&x)[idx]; }    // We very rarely use this [] operator, the assert overhead is fine.\n#ifdef IM_VEC2_CLASS_EXTRA\n    IM_VEC2_CLASS_EXTRA     // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec2.\n#endif\n};\n\n// 4D vector (often used to store floating-point colors)\nstruct ImVec4\n{\n    float     x, y, z, w;\n    ImVec4()  { x = y = z = w = 0.0f; }\n    ImVec4(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; }\n#ifdef IM_VEC4_CLASS_EXTRA\n    IM_VEC4_CLASS_EXTRA     // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec4.\n#endif\n};\n\n//-----------------------------------------------------------------------------\n// ImGui: Dear ImGui end-user API\n// (Inside a namespace so you can add extra functions in your own separate file. Please don't modify imgui.cpp/.h!)\n//-----------------------------------------------------------------------------\n\nnamespace ImGui\n{\n    // Context creation and access\n    // Each context create its own ImFontAtlas by default. You may instance one yourself and pass it to CreateContext() to share a font atlas between imgui contexts.\n    // All those functions are not reliant on the current context.\n    IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL);\n    IMGUI_API void          DestroyContext(ImGuiContext* ctx = NULL);   // NULL = destroy current context\n    IMGUI_API ImGuiContext* GetCurrentContext();\n    IMGUI_API void          SetCurrentContext(ImGuiContext* ctx);\n    IMGUI_API bool          DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert);\n\n    // Main\n    IMGUI_API ImGuiIO&      GetIO();                                    // access the IO structure (mouse/keyboard/gamepad inputs, time, various configuration options/flags)\n    IMGUI_API ImGuiStyle&   GetStyle();                                 // access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame.\n    IMGUI_API void          NewFrame();                                 // start a new Dear ImGui frame, you can submit any command from this point until Render()/EndFrame().\n    IMGUI_API void          EndFrame();                                 // ends the Dear ImGui frame. automatically called by Render(), you likely don't need to call that yourself directly. If you don't need to render data (skipping rendering) you may call EndFrame() but you'll have wasted CPU already! If you don't need to render, better to not create any imgui windows and not call NewFrame() at all!\n    IMGUI_API void          Render();                                   // ends the Dear ImGui frame, finalize the draw data. You can get call GetDrawData() to obtain it and run your rendering function. (Obsolete: this used to call io.RenderDrawListsFn(). Nowadays, we allow and prefer calling your render function yourself.)\n    IMGUI_API ImDrawData*   GetDrawData();                              // valid after Render() and until the next call to NewFrame(). this is what you have to render.\n\n    // Demo, Debug, Information\n    IMGUI_API void          ShowDemoWindow(bool* p_open = NULL);        // create demo/test window (previously called ShowTestWindow). demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application!\n    IMGUI_API void          ShowAboutWindow(bool* p_open = NULL);       // create about window. display Dear ImGui version, credits and build/system information.\n    IMGUI_API void          ShowMetricsWindow(bool* p_open = NULL);     // create metrics/debug window. display Dear ImGui internals: draw commands (with individual draw calls and vertices), window list, basic internal state, etc.\n    IMGUI_API void          ShowStyleEditor(ImGuiStyle* ref = NULL);    // add style editor block (not a window). you can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it uses the default style)\n    IMGUI_API bool          ShowStyleSelector(const char* label);       // add style selector block (not a window), essentially a combo listing the default styles.\n    IMGUI_API void          ShowFontSelector(const char* label);        // add font selector block (not a window), essentially a combo listing the loaded fonts.\n    IMGUI_API void          ShowUserGuide();                            // add basic help/info block (not a window): how to manipulate ImGui as a end-user (mouse/keyboard controls).\n    IMGUI_API const char*   GetVersion();                               // get the compiled version string e.g. \"1.23\" (essentially the compiled value for IMGUI_VERSION)\n\n    // Styles\n    IMGUI_API void          StyleColorsDark(ImGuiStyle* dst = NULL);    // new, recommended style (default)\n    IMGUI_API void          StyleColorsClassic(ImGuiStyle* dst = NULL); // classic imgui style\n\tIMGUI_API void          StyleColorsDarkCodz1(ImGuiStyle* dst = NULL); // https://github.com/codz01\n\tIMGUI_API void          StyleColorsCherry(ImGuiStyle* dst = NULL); // https://github.com/ocornut/imgui/issues/707\n\tIMGUI_API void          StyleColorsLightGreen(ImGuiStyle* dst = NULL); // https://github.com/ocornut/imgui/pull/1776\n\tIMGUI_API void          StyleColorsUE(ImGuiStyle* dst = NULL); // https://github.com/ocornut/imgui/issues/707\n\tIMGUI_API void          StyleCorporateGrey(ImGuiStyle* dst = NULL); // https://github.com/ocornut/imgui/issues/707\n    IMGUI_API void          StyleColorsLight(ImGuiStyle* dst = NULL);   // best used with borders and a custom, thicker font\n\n    // Windows\n    // - Begin() = push window to the stack and start appending to it. End() = pop window from the stack.\n    // - You may append multiple times to the same window during the same frame.\n    // - Passing 'bool* p_open != NULL' shows a window-closing widget in the upper-right corner of the window,\n    //   which clicking will set the boolean to false when clicked.\n    // - Begin() return false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting\n    //   anything to the window. Always call a matching End() for each Begin() call, regardless of its return value!\n    //   [this is due to legacy reason and is inconsistent with most other functions such as BeginMenu/EndMenu, BeginPopup/EndPopup, etc.\n    //    where the EndXXX call should only be called if the corresponding BeginXXX function returned true.]\n    // - Note that the bottom of window stack always contains a window called \"Debug\".\n    IMGUI_API bool          Begin(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0);\n    IMGUI_API void          End();\n\n    // Child Windows\n    // - Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window. Child windows can embed their own child.\n    // - For each independent axis of 'size': ==0.0f: use remaining host window size / >0.0f: fixed size / <0.0f: use remaining window size minus abs(size) / Each axis can use a different mode, e.g. ImVec2(0,400).\n    // - BeginChild() returns false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting anything to the window.\n    //   Always call a matching EndChild() for each BeginChild() call, regardless of its return value [this is due to legacy reason and is inconsistent with most other functions such as BeginMenu/EndMenu, BeginPopup/EndPopup, etc. where the EndXXX call should only be called if the corresponding BeginXXX function returned true.]\n    IMGUI_API bool          BeginChild(const char* str_id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0);\n    IMGUI_API bool          BeginChild(ImGuiID id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0);\n    IMGUI_API void          EndChild();\n\n    // Windows Utilities\n    // - \"current window\" = the window we are appending into while inside a Begin()/End() block. \"next window\" = next window we will Begin() into.\n    IMGUI_API bool          IsWindowAppearing();\n    IMGUI_API bool          IsWindowCollapsed();\n    IMGUI_API bool          IsWindowFocused(ImGuiFocusedFlags flags=0); // is current window focused? or its root/child, depending on flags. see flags for options.\n    IMGUI_API bool          IsWindowHovered(ImGuiHoveredFlags flags=0); // is current window hovered (and typically: not blocked by a popup/modal)? see flags for options. NB: If you are trying to check whether your mouse should be dispatched to imgui or to your app, you should use the 'io.WantCaptureMouse' boolean for that! Please read the FAQ!\n    IMGUI_API ImDrawList*   GetWindowDrawList();                        // get draw list associated to the current window, to append your own drawing primitives\n    IMGUI_API float         GetWindowDpiScale();                        // get DPI scale currently associated to the current window's viewport.\n    IMGUI_API ImGuiViewport*GetWindowViewport();                        // get viewport currently associated to the current window.\n    IMGUI_API ImVec2        GetWindowPos();                             // get current window position in screen space (useful if you want to do your own drawing via the DrawList API)\n    IMGUI_API ImVec2        GetWindowSize();                            // get current window size\n    IMGUI_API float         GetWindowWidth();                           // get current window width (shortcut for GetWindowSize().x)\n    IMGUI_API float         GetWindowHeight();                          // get current window height (shortcut for GetWindowSize().y)\n\n    // Prefer using SetNextXXX functions (before Begin) rather that SetXXX functions (after Begin).\n    IMGUI_API void          SetNextWindowPos(const ImVec2& pos, ImGuiCond cond = 0, const ImVec2& pivot = ImVec2(0,0)); // set next window position. call before Begin(). use pivot=(0.5f,0.5f) to center on given point, etc.\n    IMGUI_API void          SetNextWindowSize(const ImVec2& size, ImGuiCond cond = 0);                  // set next window size. set axis to 0.0f to force an auto-fit on this axis. call before Begin()\n    IMGUI_API void          SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback = NULL, void* custom_callback_data = NULL); // set next window size limits. use -1,-1 on either X/Y axis to preserve the current size. Use callback to apply non-trivial programmatic constraints.\n    IMGUI_API void          SetNextWindowContentSize(const ImVec2& size);                               // set next window content size (~ enforce the range of scrollbars). not including window decorations (title bar, menu bar, etc.). set an axis to 0.0f to leave it automatic. call before Begin()\n    IMGUI_API void          SetNextWindowCollapsed(bool collapsed, ImGuiCond cond = 0);                 // set next window collapsed state. call before Begin()\n    IMGUI_API void          SetNextWindowFocus();                                                       // set next window to be focused / front-most. call before Begin()\n    IMGUI_API void          SetNextWindowBgAlpha(float alpha);                                          // set next window background color alpha. helper to easily modify ImGuiCol_WindowBg/ChildBg/PopupBg. you may also use ImGuiWindowFlags_NoBackground.\n    IMGUI_API void          SetNextWindowViewport(ImGuiID viewport_id);                                 // set next window viewport\n    IMGUI_API void          SetWindowPos(const ImVec2& pos, ImGuiCond cond = 0);                        // (not recommended) set current window position - call within Begin()/End(). prefer using SetNextWindowPos(), as this may incur tearing and side-effects.\n    IMGUI_API void          SetWindowSize(const ImVec2& size, ImGuiCond cond = 0);                      // (not recommended) set current window size - call within Begin()/End(). set to ImVec2(0,0) to force an auto-fit. prefer using SetNextWindowSize(), as this may incur tearing and minor side-effects.\n    IMGUI_API void          SetWindowCollapsed(bool collapsed, ImGuiCond cond = 0);                     // (not recommended) set current window collapsed state. prefer using SetNextWindowCollapsed().\n    IMGUI_API void          SetWindowFocus();                                                           // (not recommended) set current window to be focused / front-most. prefer using SetNextWindowFocus().\n    IMGUI_API void          SetWindowFontScale(float scale);                                            // set font scale. Adjust IO.FontGlobalScale if you want to scale all windows\n    IMGUI_API void          SetWindowPos(const char* name, const ImVec2& pos, ImGuiCond cond = 0);      // set named window position.\n    IMGUI_API void          SetWindowSize(const char* name, const ImVec2& size, ImGuiCond cond = 0);    // set named window size. set axis to 0.0f to force an auto-fit on this axis.\n    IMGUI_API void          SetWindowCollapsed(const char* name, bool collapsed, ImGuiCond cond = 0);   // set named window collapsed state\n    IMGUI_API void          SetWindowFocus(const char* name);                                           // set named window to be focused / front-most. use NULL to remove focus.\n\n    // Content region\n    // - Those functions are bound to be redesigned soon (they are confusing, incomplete and return values in local window coordinates which increases confusion)\n    IMGUI_API ImVec2        GetContentRegionMax();                                          // current content boundaries (typically window boundaries including scrolling, or current column boundaries), in windows coordinates\n    IMGUI_API ImVec2        GetContentRegionAvail();                                        // == GetContentRegionMax() - GetCursorPos()\n    IMGUI_API float         GetContentRegionAvailWidth();                                   // == GetContentRegionAvail().x\n    IMGUI_API ImVec2        GetWindowContentRegionMin();                                    // content boundaries min (roughly (0,0)-Scroll), in window coordinates\n    IMGUI_API ImVec2        GetWindowContentRegionMax();                                    // content boundaries max (roughly (0,0)+Size-Scroll) where Size can be override with SetNextWindowContentSize(), in window coordinates\n    IMGUI_API float         GetWindowContentRegionWidth();                                  //\n\n    // Windows Scrolling\n    IMGUI_API float         GetScrollX();                                                   // get scrolling amount [0..GetScrollMaxX()]\n    IMGUI_API float         GetScrollY();                                                   // get scrolling amount [0..GetScrollMaxY()]\n    IMGUI_API float         GetScrollMaxX();                                                // get maximum scrolling amount ~~ ContentSize.X - WindowSize.X\n    IMGUI_API float         GetScrollMaxY();                                                // get maximum scrolling amount ~~ ContentSize.Y - WindowSize.Y\n    IMGUI_API void          SetScrollX(float scroll_x);                                     // set scrolling amount [0..GetScrollMaxX()]\n    IMGUI_API void          SetScrollY(float scroll_y);                                     // set scrolling amount [0..GetScrollMaxY()]\n    IMGUI_API void          SetScrollHereY(float center_y_ratio = 0.5f);                    // adjust scrolling amount to make current cursor position visible. center_y_ratio=0.0: top, 0.5: center, 1.0: bottom. When using to make a \"default/current item\" visible, consider using SetItemDefaultFocus() instead.\n    IMGUI_API void          SetScrollFromPosY(float local_y, float center_y_ratio = 0.5f);  // adjust scrolling amount to make given position visible. Generally GetCursorStartPos() + offset to compute a valid position.\n\n    // Parameters stacks (shared)\n    IMGUI_API void          PushFont(ImFont* font);                                         // use NULL as a shortcut to push default font\n    IMGUI_API void          PopFont();\n    IMGUI_API void          PushStyleColor(ImGuiCol idx, ImU32 col);\n    IMGUI_API void          PushStyleColor(ImGuiCol idx, const ImVec4& col);\n    IMGUI_API void          PopStyleColor(int count = 1);\n    IMGUI_API void          PushStyleVar(ImGuiStyleVar idx, float val);\n    IMGUI_API void          PushStyleVar(ImGuiStyleVar idx, const ImVec2& val);\n    IMGUI_API void          PopStyleVar(int count = 1);\n    IMGUI_API const ImVec4& GetStyleColorVec4(ImGuiCol idx);                                // retrieve style color as stored in ImGuiStyle structure. use to feed back into PushStyleColor(), otherwise use GetColorU32() to get style color with style alpha baked in.\n    IMGUI_API ImFont*       GetFont();                                                      // get current font\n    IMGUI_API float         GetFontSize();                                                  // get current font size (= height in pixels) of current font with current scale applied\n    IMGUI_API ImVec2        GetFontTexUvWhitePixel();                                       // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API\n    IMGUI_API ImU32         GetColorU32(ImGuiCol idx, float alpha_mul = 1.0f);              // retrieve given style color with style alpha applied and optional extra alpha multiplier\n    IMGUI_API ImU32         GetColorU32(const ImVec4& col);                                 // retrieve given color with style alpha applied\n    IMGUI_API ImU32         GetColorU32(ImU32 col);                                         // retrieve given color with style alpha applied\n\n    // Parameters stacks (current window)\n    IMGUI_API void          PushItemWidth(float item_width);                                // set width of items for common large \"item+label\" widgets. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side). 0.0f = default to ~2/3 of windows width, \n    IMGUI_API void          PopItemWidth();\n    IMGUI_API void          SetNextItemWidth(float item_width);                             // set width of the _next_ common large \"item+label\" widget. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side)\n    IMGUI_API float         CalcItemWidth();                                                // width of item given pushed settings and current cursor position\n    IMGUI_API void          PushTextWrapPos(float wrap_local_pos_x = 0.0f);                 // word-wrapping for Text*() commands. < 0.0f: no wrapping; 0.0f: wrap to end of window (or column); > 0.0f: wrap at 'wrap_pos_x' position in window local space\n    IMGUI_API void          PopTextWrapPos();\n    IMGUI_API void          PushAllowKeyboardFocus(bool allow_keyboard_focus);              // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets\n    IMGUI_API void          PopAllowKeyboardFocus();\n    IMGUI_API void          PushButtonRepeat(bool repeat);                                  // in 'repeat' mode, Button*() functions return repeated true in a typematic manner (using io.KeyRepeatDelay/io.KeyRepeatRate setting). Note that you can call IsItemActive() after any Button() to tell if the button is held in the current frame.\n    IMGUI_API void          PopButtonRepeat();\n\n    // Cursor / Layout\n    // - By \"cursor\" we mean the current output position.\n    // - The typical widget behavior is to output themselves at the current cursor position, then move the cursor one line down.\n    IMGUI_API void          Separator();                                                    // separator, generally horizontal. inside a menu bar or in horizontal layout mode, this becomes a vertical separator.\n    IMGUI_API void          SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f);  // call between widgets or groups to layout them horizontally. X position given in window coordinates.\n    IMGUI_API void          NewLine();                                                      // undo a SameLine() or force a new line when in an horizontal-layout context.\n    IMGUI_API void          Spacing();                                                      // add vertical spacing.\n    IMGUI_API void          Dummy(const ImVec2& size);                                      // add a dummy item of given size. unlike InvisibleButton(), Dummy() won't take the mouse click or be navigable into.\n    IMGUI_API void          Indent(float indent_w = 0.0f);                                  // move content position toward the right, by style.IndentSpacing or indent_w if != 0\n    IMGUI_API void          Unindent(float indent_w = 0.0f);                                // move content position back to the left, by style.IndentSpacing or indent_w if != 0\n    IMGUI_API void          BeginGroup();                                                   // lock horizontal starting position\n    IMGUI_API void          EndGroup();                                                     // unlock horizontal starting position + capture the whole group bounding box into one \"item\" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.)\n    IMGUI_API ImVec2        GetCursorPos();                                                 // cursor position in window coordinates (relative to window position)\n    IMGUI_API float         GetCursorPosX();                                                //   (some functions are using window-relative coordinates, such as: GetCursorPos, GetCursorStartPos, GetContentRegionMax, GetWindowContentRegion* etc.\n    IMGUI_API float         GetCursorPosY();                                                //    other functions such as GetCursorScreenPos or everything in ImDrawList::\n    IMGUI_API void          SetCursorPos(const ImVec2& local_pos);                          //    are using the main, absolute coordinate system.\n    IMGUI_API void          SetCursorPosX(float local_x);                                   //    GetWindowPos() + GetCursorPos() == GetCursorScreenPos() etc.)\n    IMGUI_API void          SetCursorPosY(float local_y);                                   //\n    IMGUI_API ImVec2        GetCursorStartPos();                                            // initial cursor position in window coordinates\n    IMGUI_API ImVec2        GetCursorScreenPos();                                           // cursor position in absolute screen coordinates (0..io.DisplaySize) or natural OS coordinates when using multiple viewport. Useful to work with ImDrawList API.\n    IMGUI_API void          SetCursorScreenPos(const ImVec2& pos);                          // cursor position in absolute screen coordinates (0..io.DisplaySize) or natural OS coordinates when using multiple viewport.\n    IMGUI_API void          AlignTextToFramePadding();                                      // vertically align upcoming text baseline to FramePadding.y so that it will align properly to regularly framed items (call if you have text on a line before a framed item)\n    IMGUI_API float         GetTextLineHeight();                                            // ~ FontSize\n    IMGUI_API float         GetTextLineHeightWithSpacing();                                 // ~ FontSize + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of text)\n    IMGUI_API float         GetFrameHeight();                                               // ~ FontSize + style.FramePadding.y * 2\n    IMGUI_API float         GetFrameHeightWithSpacing();                                    // ~ FontSize + style.FramePadding.y * 2 + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of framed widgets)\n\n    // ID stack/scopes\n    // - Read the FAQ for more details about how ID are handled in dear imgui. If you are creating widgets in a loop you most\n    //   likely want to push a unique identifier (e.g. object pointer, loop index) to uniquely differentiate them.\n    // - The resulting ID are hashes of the entire stack.\n    // - You can also use the \"Label##foobar\" syntax within widget label to distinguish them from each others.\n    // - In this header file we use the \"label\"/\"name\" terminology to denote a string that will be displayed and used as an ID,\n    //   whereas \"str_id\" denote a string that is only used as an ID and not normally displayed.\n    IMGUI_API void          PushID(const char* str_id);                                     // push string into the ID stack (will hash string).\n    IMGUI_API void          PushID(const char* str_id_begin, const char* str_id_end);       // push string into the ID stack (will hash string).\n    IMGUI_API void          PushID(const void* ptr_id);                                     // push pointer into the ID stack (will hash pointer).\n    IMGUI_API void          PushID(int int_id);                                             // push integer into the ID stack (will hash integer).\n    IMGUI_API void          PopID();                                                        // pop from the ID stack.\n    IMGUI_API ImGuiID       GetID(const char* str_id);                                      // calculate unique ID (hash of whole ID stack + given parameter). e.g. if you want to query into ImGuiStorage yourself\n    IMGUI_API ImGuiID       GetID(const char* str_id_begin, const char* str_id_end);\n    IMGUI_API ImGuiID       GetID(const void* ptr_id);\n\n    // Widgets: Text\n    IMGUI_API void          TextUnformatted(const char* text, const char* text_end = NULL);                // raw text without formatting. Roughly equivalent to Text(\"%s\", text) but: A) doesn't require null terminated string if 'text_end' is specified, B) it's faster, no memory copy is done, no buffer size limits, recommended for long chunks of text.\n    IMGUI_API void          Text(const char* fmt, ...)                                      IM_FMTARGS(1); // simple formatted text\n    IMGUI_API void          TextV(const char* fmt, va_list args)                            IM_FMTLIST(1);\n    IMGUI_API void          TextColored(const ImVec4& col, const char* fmt, ...)            IM_FMTARGS(2); // shortcut for PushStyleColor(ImGuiCol_Text, col); Text(fmt, ...); PopStyleColor();\n    IMGUI_API void          TextColoredV(const ImVec4& col, const char* fmt, va_list args)  IM_FMTLIST(2);\n    IMGUI_API void          TextDisabled(const char* fmt, ...)                              IM_FMTARGS(1); // shortcut for PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); Text(fmt, ...); PopStyleColor();\n    IMGUI_API void          TextDisabledV(const char* fmt, va_list args)                    IM_FMTLIST(1);\n    IMGUI_API void          TextWrapped(const char* fmt, ...)                               IM_FMTARGS(1); // shortcut for PushTextWrapPos(0.0f); Text(fmt, ...); PopTextWrapPos();. Note that this won't work on an auto-resizing window if there's no other widgets to extend the window width, yoy may need to set a size using SetNextWindowSize().\n    IMGUI_API void          TextWrappedV(const char* fmt, va_list args)                     IM_FMTLIST(1);\n    IMGUI_API void          LabelText(const char* label, const char* fmt, ...)              IM_FMTARGS(2); // display text+label aligned the same way as value+label widgets\n    IMGUI_API void          LabelTextV(const char* label, const char* fmt, va_list args)    IM_FMTLIST(2);\n    IMGUI_API void          BulletText(const char* fmt, ...)                                IM_FMTARGS(1); // shortcut for Bullet()+Text()\n    IMGUI_API void          BulletTextV(const char* fmt, va_list args)                      IM_FMTLIST(1);\n\n    // Widgets: Main\n    // - Most widgets return true when the value has been changed or when pressed/selected\n    IMGUI_API bool          Button(const char* label, const ImVec2& size = ImVec2(0,0));    // button\n    IMGUI_API bool          SmallButton(const char* label);                                 // button with FramePadding=(0,0) to easily embed within text\n    IMGUI_API bool          InvisibleButton(const char* str_id, const ImVec2& size);        // button behavior without the visuals, frequently useful to build custom behaviors using the public api (along with IsItemActive, IsItemHovered, etc.)\n    IMGUI_API bool          ArrowButton(const char* str_id, ImGuiDir dir);                  // square button with an arrow shape\n    IMGUI_API void          Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0));\n    IMGUI_API bool          ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0),  const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0,0,0,0), const ImVec4& tint_col = ImVec4(1,1,1,1));    // <0 frame_padding uses default frame padding settings. 0 for no padding\n    IMGUI_API bool          Checkbox(const char* label, bool* v);\n    IMGUI_API bool          CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value);\n    IMGUI_API bool          RadioButton(const char* label, bool active);                    // use with e.g. if (RadioButton(\"one\", my_value==1)) { my_value = 1; }\n    IMGUI_API bool          RadioButton(const char* label, int* v, int v_button);           // shortcut to handle the above pattern when value is an integer\n    IMGUI_API void          ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-1,0), const char* overlay = NULL);\n    IMGUI_API void          Bullet();                                                       // draw a small circle and keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses\n\n    // Widgets: Combo Box\n    // - The new BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items.\n    // - The old Combo() api are helpers over BeginCombo()/EndCombo() which are kept available for convenience purpose.\n    IMGUI_API bool          BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags = 0);\n    IMGUI_API void          EndCombo(); // only call EndCombo() if BeginCombo() returns true!\n    IMGUI_API bool          Combo(const char* label, int* current_item, const char* const items[], int items_count, int popup_max_height_in_items = -1);\n    IMGUI_API bool          Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int popup_max_height_in_items = -1);      // Separate items with \\0 within a string, end item-list with \\0\\0. e.g. \"One\\0Two\\0Three\\0\"\n    IMGUI_API bool          Combo(const char* label, int* current_item, bool(*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int popup_max_height_in_items = -1);\n\n    // Widgets: Drags\n    // - CTRL+Click on any drag box to turn them into an input box. Manually input values aren't clamped and can go off-bounds.\n    // - For all the Float2/Float3/Float4/Int2/Int3/Int4 versions of every functions, note that a 'float v[X]' function argument is the same as 'float* v', the array syntax is just a way to document the number of elements that are expected to be accessible. You can pass address of your first element out of a contiguous set, e.g. &myvector.x\n    // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. \"%.3f\" -> 1.234; \"%5.2f secs\" -> 01.23 secs; \"Biscuit: %.0f\" -> Biscuit: 1; etc.\n    // - Speed are per-pixel of mouse movement (v_speed=0.2f: mouse needs to move by 5 pixels to increase value by 1). For gamepad/keyboard navigation, minimum speed is Max(v_speed, minimum_step_at_given_precision).\n    IMGUI_API bool          DragFloat(const char* label, float* v, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = \"%.3f\", float power = 1.0f);     // If v_min >= v_max we have no bound\n    IMGUI_API bool          DragFloat2(const char* label, float v[2], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = \"%.3f\", float power = 1.0f);\n    IMGUI_API bool          DragFloat3(const char* label, float v[3], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = \"%.3f\", float power = 1.0f);\n    IMGUI_API bool          DragFloat4(const char* label, float v[4], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = \"%.3f\", float power = 1.0f);\n    IMGUI_API bool          DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = \"%.3f\", const char* format_max = NULL, float power = 1.0f);\n    IMGUI_API bool          DragInt(const char* label, int* v, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = \"%d\");                                       // If v_min >= v_max we have no bound\n    IMGUI_API bool          DragInt2(const char* label, int v[2], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = \"%d\");\n    IMGUI_API bool          DragInt3(const char* label, int v[3], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = \"%d\");\n    IMGUI_API bool          DragInt4(const char* label, int v[4], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = \"%d\");\n    IMGUI_API bool          DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = \"%d\", const char* format_max = NULL);\n    IMGUI_API bool          DragScalar(const char* label, ImGuiDataType data_type, void* v, float v_speed, const void* v_min = NULL, const void* v_max = NULL, const char* format = NULL, float power = 1.0f);\n    IMGUI_API bool          DragScalarN(const char* label, ImGuiDataType data_type, void* v, int components, float v_speed, const void* v_min = NULL, const void* v_max = NULL, const char* format = NULL, float power = 1.0f);\n\n    // Widgets: Sliders\n    // - CTRL+Click on any slider to turn them into an input box. Manually input values aren't clamped and can go off-bounds.\n    // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. \"%.3f\" -> 1.234; \"%5.2f secs\" -> 01.23 secs; \"Biscuit: %.0f\" -> Biscuit: 1; etc.\n    IMGUI_API bool          SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format = \"%.3f\", float power = 1.0f);     // adjust format to decorate the value with a prefix or a suffix for in-slider labels or unit display. Use power!=1.0 for power curve sliders\n    IMGUI_API bool          SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format = \"%.3f\", float power = 1.0f);\n    IMGUI_API bool          SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format = \"%.3f\", float power = 1.0f);\n    IMGUI_API bool          SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format = \"%.3f\", float power = 1.0f);\n    IMGUI_API bool          SliderAngle(const char* label, float* v_rad, float v_degrees_min = -360.0f, float v_degrees_max = +360.0f, const char* format = \"%.0f deg\");\n    IMGUI_API bool          SliderInt(const char* label, int* v, int v_min, int v_max, const char* format = \"%d\");\n    IMGUI_API bool          SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format = \"%d\");\n    IMGUI_API bool          SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format = \"%d\");\n    IMGUI_API bool          SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format = \"%d\");\n    IMGUI_API bool          SliderScalar(const char* label, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format = NULL, float power = 1.0f);\n    IMGUI_API bool          SliderScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format = NULL, float power = 1.0f);\n    IMGUI_API bool          VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format = \"%.3f\", float power = 1.0f);\n    IMGUI_API bool          VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format = \"%d\");\n    IMGUI_API bool          VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format = NULL, float power = 1.0f);\n\n    // Widgets: Input with Keyboard\n    // - If you want to use InputText() with a dynamic string type such as std::string or your own, see misc/cpp/imgui_stdlib.h\n    // - Most of the ImGuiInputTextFlags flags are only useful for InputText() and not for InputFloatX, InputIntX, InputDouble etc.\n    IMGUI_API bool          InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);\n    IMGUI_API bool          InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size = ImVec2(0,0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);\n    IMGUI_API bool          InputTextWithHint(const char* label, const char* hint, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);\n    IMGUI_API bool          InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, const char* format = \"%.3f\", ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputFloat2(const char* label, float v[2], const char* format = \"%.3f\", ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputFloat3(const char* label, float v[3], const char* format = \"%.3f\", ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputFloat4(const char* label, float v[4], const char* format = \"%.3f\", ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputInt(const char* label, int* v, int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputInt2(const char* label, int v[2], ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputInt3(const char* label, int v[3], ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputInt4(const char* label, int v[4], ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputDouble(const char* label, double* v, double step = 0.0, double step_fast = 0.0, const char* format = \"%.6f\", ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputScalar(const char* label, ImGuiDataType data_type, void* v, const void* step = NULL, const void* step_fast = NULL, const char* format = NULL, ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool          InputScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* step = NULL, const void* step_fast = NULL, const char* format = NULL, ImGuiInputTextFlags flags = 0);\n\n    // Widgets: Color Editor/Picker (tip: the ColorEdit* functions have a little colored preview square that can be left-clicked to open a picker, and right-clicked to open an option menu.)\n    // - Note that in C++ a 'float v[X]' function argument is the _same_ as 'float* v', the array syntax is just a way to document the number of elements that are expected to be accessible.\n    // - You can pass the address of a first float element out of a contiguous structure, e.g. &myvector.x\n    IMGUI_API bool          ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags = 0);\n    IMGUI_API bool          ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0);\n    IMGUI_API bool          ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags flags = 0);\n    IMGUI_API bool          ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags flags = 0, const float* ref_col = NULL);\n    IMGUI_API bool          ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0,0));  // display a colored square/button, hover for details, return true when pressed.\n    IMGUI_API void          SetColorEditOptions(ImGuiColorEditFlags flags);                     // initialize current options (generally on application startup) if you want to select a default format, picker type, etc. User will be able to change many settings, unless you pass the _NoOptions flag to your calls.\n\n    // Widgets: Trees\n    // - TreeNode functions return true when the node is open, in which case you need to also call TreePop() when you are finished displaying the tree node contents.\n    IMGUI_API bool          TreeNode(const char* label);\n    IMGUI_API bool          TreeNode(const char* str_id, const char* fmt, ...) IM_FMTARGS(2);   // helper variation to easily decorelate the id from the displayed string. Read the FAQ about why and how to use ID. to align arbitrary text at the same level as a TreeNode() you can use Bullet().\n    IMGUI_API bool          TreeNode(const void* ptr_id, const char* fmt, ...) IM_FMTARGS(2);   // \"\n    IMGUI_API bool          TreeNodeV(const char* str_id, const char* fmt, va_list args) IM_FMTLIST(2);\n    IMGUI_API bool          TreeNodeV(const void* ptr_id, const char* fmt, va_list args) IM_FMTLIST(2);\n    IMGUI_API bool          TreeNodeEx(const char* label, ImGuiTreeNodeFlags flags = 0);\n    IMGUI_API bool          TreeNodeEx(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) IM_FMTARGS(3);\n    IMGUI_API bool          TreeNodeEx(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) IM_FMTARGS(3);\n    IMGUI_API bool          TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3);\n    IMGUI_API bool          TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3);\n    IMGUI_API void          TreePush(const char* str_id);                                       // ~ Indent()+PushId(). Already called by TreeNode() when returning true, but you can call TreePush/TreePop yourself if desired.\n    IMGUI_API void          TreePush(const void* ptr_id = NULL);                                // \"\n    IMGUI_API void          TreePop();                                                          // ~ Unindent()+PopId()\n    IMGUI_API void          TreeAdvanceToLabelPos();                                            // advance cursor x position by GetTreeNodeToLabelSpacing()\n    IMGUI_API float         GetTreeNodeToLabelSpacing();                                        // horizontal distance preceding label when using TreeNode*() or Bullet() == (g.FontSize + style.FramePadding.x*2) for a regular unframed TreeNode\n    IMGUI_API void          SetNextTreeNodeOpen(bool is_open, ImGuiCond cond = 0);              // set next TreeNode/CollapsingHeader open state.\n    IMGUI_API bool          CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags = 0);  // if returning 'true' the header is open. doesn't indent nor push on ID stack. user doesn't have to call TreePop().\n    IMGUI_API bool          CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags flags = 0); // when 'p_open' isn't NULL, display an additional small close button on upper right of the header\n\n    // Widgets: Selectables\n    // - A selectable highlights when hovered, and can display another color when selected.\n    // - Neighbors selectable extend their highlight bounds in order to leave no gap between them.\n    IMGUI_API bool          Selectable(const char* label, bool selected = false, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0));  // \"bool selected\" carry the selection state (read-only). Selectable() is clicked is returns true so you can modify your selection state. size.x==0.0: use remaining width, size.x>0.0: specify width. size.y==0.0: use label height, size.y>0.0: specify height\n    IMGUI_API bool          Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0));       // \"bool* p_selected\" point to the selection state (read-write), as a convenient helper.\n\n    // Widgets: List Boxes\n    // - FIXME: To be consistent with all the newer API, ListBoxHeader/ListBoxFooter should in reality be called BeginListBox/EndListBox. Will rename them.\n    IMGUI_API bool          ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items = -1);\n    IMGUI_API bool          ListBox(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items = -1);\n    IMGUI_API bool          ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0,0)); // use if you want to reimplement ListBox() will custom data or interactions. if the function return true, you can output elements then call ListBoxFooter() afterwards.\n    IMGUI_API bool          ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // \"\n    IMGUI_API void          ListBoxFooter();                                                    // terminate the scrolling region. only call ListBoxFooter() if ListBoxHeader() returned true!\n\n    // Widgets: Data Plotting\n    IMGUI_API void          PlotLines(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float));\n    IMGUI_API void          PlotLines(const char* label, float(*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0));\n    IMGUI_API void          PlotHistogram(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float));\n    IMGUI_API void          PlotHistogram(const char* label, float(*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0));\n\n    // Widgets: Value() Helpers.\n    // - Those are merely shortcut to calling Text() with a format string. Output single value in \"name: value\" format (tip: freely declare more in your code to handle your types. you can add functions to the ImGui namespace)\n    IMGUI_API void          Value(const char* prefix, bool b);\n    IMGUI_API void          Value(const char* prefix, int v);\n    IMGUI_API void          Value(const char* prefix, unsigned int v);\n    IMGUI_API void          Value(const char* prefix, float v, const char* float_format = NULL);\n\n    // Widgets: Menus\n    IMGUI_API bool          BeginMainMenuBar();                                                 // create and append to a full screen menu-bar.\n    IMGUI_API void          EndMainMenuBar();                                                   // only call EndMainMenuBar() if BeginMainMenuBar() returns true!\n    IMGUI_API bool          BeginMenuBar();                                                     // append to menu-bar of current window (requires ImGuiWindowFlags_MenuBar flag set on parent window).\n    IMGUI_API void          EndMenuBar();                                                       // only call EndMenuBar() if BeginMenuBar() returns true!\n    IMGUI_API bool          BeginMenu(const char* label, bool enabled = true);                  // create a sub-menu entry. only call EndMenu() if this returns true!\n    IMGUI_API void          EndMenu();                                                          // only call EndMenu() if BeginMenu() returns true!\n    IMGUI_API bool          MenuItem(const char* label, const char* shortcut = NULL, bool selected = false, bool enabled = true);  // return true when activated. shortcuts are displayed for convenience but not processed by ImGui at the moment\n    IMGUI_API bool          MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled = true);              // return true when activated + toggle (*p_selected) if p_selected != NULL\n\n    // Tooltips\n    IMGUI_API void          BeginTooltip();                                                     // begin/append a tooltip window. to create full-featured tooltip (with any kind of items).\n    IMGUI_API void          EndTooltip();\n    IMGUI_API void          SetTooltip(const char* fmt, ...) IM_FMTARGS(1);                     // set a text-only tooltip, typically use with ImGui::IsItemHovered(). override any previous call to SetTooltip().\n    IMGUI_API void          SetTooltipV(const char* fmt, va_list args) IM_FMTLIST(1);\n\n    // Popups, Modals\n    // The properties of popups windows are:\n    // - They block normal mouse hovering detection outside them. (*)\n    // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE.\n    // - Their visibility state (~bool) is held internally by imgui instead of being held by the programmer as we are used to with regular Begin() calls.\n    //   User can manipulate the visibility state by calling OpenPopup().\n    // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even when normally blocked by a popup.\n    // Those three properties are connected. The library needs to hold their visibility state because it can close popups at any time.\n    IMGUI_API void          OpenPopup(const char* str_id);                                      // call to mark popup as open (don't call every frame!). popups are closed when user click outside, or if CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. By default, Selectable()/MenuItem() are calling CloseCurrentPopup(). Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level).\n    IMGUI_API bool          BeginPopup(const char* str_id, ImGuiWindowFlags flags = 0);                                             // return true if the popup is open, and you can start outputting to it. only call EndPopup() if BeginPopup() returns true!\n    IMGUI_API bool          BeginPopupContextItem(const char* str_id = NULL, int mouse_button = 1);                                 // helper to open and begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp!\n    IMGUI_API bool          BeginPopupContextWindow(const char* str_id = NULL, int mouse_button = 1, bool also_over_items = true);  // helper to open and begin popup when clicked on current window.\n    IMGUI_API bool          BeginPopupContextVoid(const char* str_id = NULL, int mouse_button = 1);                                 // helper to open and begin popup when clicked in void (where there are no imgui windows).\n    IMGUI_API bool          BeginPopupModal(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0);                     // modal dialog (regular window with title bar, block interactions behind the modal window, can't close the modal window by clicking outside)\n    IMGUI_API void          EndPopup();                                                                                             // only call EndPopup() if BeginPopupXXX() returns true!\n    IMGUI_API bool          OpenPopupOnItemClick(const char* str_id = NULL, int mouse_button = 1);                                  // helper to open popup when clicked on last item (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors). return true when just opened.\n    IMGUI_API bool          IsPopupOpen(const char* str_id);                                    // return true if the popup is open at the current begin-ed level of the popup stack.\n    IMGUI_API void          CloseCurrentPopup();                                                // close the popup we have begin-ed into. clicking on a MenuItem or Selectable automatically close the current popup.\n\n    // Columns\n    // - You can also use SameLine(pos_x) to mimic simplified columns.\n    // - The columns API is work-in-progress and rather lacking (columns are arguably the worst part of dear imgui at the moment!)\n    IMGUI_API void          Columns(int count = 1, const char* id = NULL, bool border = true);\n    IMGUI_API void          NextColumn();                                                       // next column, defaults to current row or next row if the current row is finished\n    IMGUI_API int           GetColumnIndex();                                                   // get current column index\n    IMGUI_API float         GetColumnWidth(int column_index = -1);                              // get column width (in pixels). pass -1 to use current column\n    IMGUI_API void          SetColumnWidth(int column_index, float width);                      // set column width (in pixels). pass -1 to use current column\n    IMGUI_API float         GetColumnOffset(int column_index = -1);                             // get position of column line (in pixels, from the left side of the contents region). pass -1 to use current column, otherwise 0..GetColumnsCount() inclusive. column 0 is typically 0.0f\n    IMGUI_API void          SetColumnOffset(int column_index, float offset_x);                  // set position of column line (in pixels, from the left side of the contents region). pass -1 to use current column\n    IMGUI_API int           GetColumnsCount();\n\n    // Tab Bars, Tabs\n    // [BETA API] API may evolve!\n    // Note: Tabs are automatically created by the docking system. Use this to create tab bars/tabs yourself without docking being involved.\n    IMGUI_API bool          BeginTabBar(const char* str_id, ImGuiTabBarFlags flags = 0);        // create and append into a TabBar\n    IMGUI_API void          EndTabBar();                                                        // only call EndTabBar() if BeginTabBar() returns true!\n    IMGUI_API bool          BeginTabItem(const char* label, bool* p_open = NULL, ImGuiTabItemFlags flags = 0);// create a Tab. Returns true if the Tab is selected.\n    IMGUI_API void          EndTabItem();                                                       // only call EndTabItem() if BeginTabItem() returns true!\n    IMGUI_API void          SetTabItemClosed(const char* tab_or_docked_window_label);           // notify TabBar or Docking system of a closed tab/window ahead (useful to reduce visual flicker on reorderable tab bars). For tab-bar: call after BeginTabBar() and before Tab submissions. Otherwise call with a window name.\n\n    // Docking \n    // [BETA API] Enable with io.ConfigFlags |= ImGuiConfigFlags_DockingEnable.\n    // Note: you DO NOT need to call DockSpace() to use most Docking facilities! \n    // To dock windows: if io.ConfigDockingWithShift == false: drag window from their title bar.\n    // To dock windows: if io.ConfigDockingWithShift == true: hold SHIFT anywhere while moving windows.\n    // Use DockSpace() to create an explicit dock node _within_ an existing window. See Docking demo for details.\n    IMGUI_API void          DockSpace(ImGuiID id, const ImVec2& size = ImVec2(0, 0), ImGuiDockNodeFlags flags = 0, const ImGuiWindowClass* window_class = NULL);\n    IMGUI_API ImGuiID       DockSpaceOverViewport(bool has_main_menu_bar, ImGuiViewport* viewport = NULL, ImGuiDockNodeFlags flags = 0, const ImGuiWindowClass* window_class = NULL);\n    IMGUI_API void          SetNextWindowDockID(ImGuiID dock_id, ImGuiCond cond = 0);           // set next window dock id (FIXME-DOCK)\n    IMGUI_API void          SetNextWindowClass(const ImGuiWindowClass* window_class);           // set next window class (rare/advanced uses: provide hints to the platform back-end via altered viewport flags and parent/child info)\n    IMGUI_API ImGuiID       GetWindowDockID();\n    IMGUI_API bool          IsWindowDocked();                                                   // is current window docked into another window? \n\n    // Logging/Capture\n    // - All text output from the interface can be captured into tty/file/clipboard. By default, tree nodes are automatically opened during logging.\n    IMGUI_API void          LogToTTY(int auto_open_depth = -1);                                 // start logging to tty (stdout)\n    IMGUI_API void          LogToFile(int auto_open_depth = -1, const char* filename = NULL);   // start logging to file\n    IMGUI_API void          LogToClipboard(int auto_open_depth = -1);                           // start logging to OS clipboard\n    IMGUI_API void          LogFinish();                                                        // stop logging (close file, etc.)\n    IMGUI_API void          LogButtons();                                                       // helper to display buttons for logging to tty/file/clipboard\n    IMGUI_API void          LogText(const char* fmt, ...) IM_FMTARGS(1);                        // pass text data straight to log (without being displayed)\n\n    // Drag and Drop\n    // [BETA API] API may evolve!\n    IMGUI_API bool          BeginDragDropSource(ImGuiDragDropFlags flags = 0);                                      // call when the current item is active. If this return true, you can call SetDragDropPayload() + EndDragDropSource()\n    IMGUI_API bool          SetDragDropPayload(const char* type, const void* data, size_t sz, ImGuiCond cond = 0);  // type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types. Data is copied and held by imgui.\n    IMGUI_API void          EndDragDropSource();                                                                    // only call EndDragDropSource() if BeginDragDropSource() returns true!\n    IMGUI_API bool                  BeginDragDropTarget();                                                          // call after submitting an item that may receive a payload. If this returns true, you can call AcceptDragDropPayload() + EndDragDropTarget()\n    IMGUI_API const ImGuiPayload*   AcceptDragDropPayload(const char* type, ImGuiDragDropFlags flags = 0);          // accept contents of a given type. If ImGuiDragDropFlags_AcceptBeforeDelivery is set you can peek into the payload before the mouse button is released.\n    IMGUI_API void                  EndDragDropTarget();                                                            // only call EndDragDropTarget() if BeginDragDropTarget() returns true!\n    IMGUI_API const ImGuiPayload*   GetDragDropPayload();                                                           // peek directly into the current payload from anywhere. may return NULL. use ImGuiPayload::IsDataType() to test for the payload type.\n\n    // Clipping\n    IMGUI_API void          PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect);\n    IMGUI_API void          PopClipRect();\n\n    // Focus, Activation\n    // - Prefer using \"SetItemDefaultFocus()\" over \"if (IsWindowAppearing()) SetScrollHereY()\" when applicable to signify \"this is the default item\"\n    IMGUI_API void          SetItemDefaultFocus();                                              // make last item the default focused item of a window.\n    IMGUI_API void          SetKeyboardFocusHere(int offset = 0);                               // focus keyboard on the next widget. Use positive 'offset' to access sub components of a multiple component widget. Use -1 to access previous widget.\n\n    // Item/Widgets Utilities\n    // - Most of the functions are referring to the last/previous item we submitted.\n    // - See Demo Window under \"Widgets->Querying Status\" for an interactive visualization of most of those functions.\n    IMGUI_API bool          IsItemHovered(ImGuiHoveredFlags flags = 0);                         // is the last item hovered? (and usable, aka not blocked by a popup, etc.). See ImGuiHoveredFlags for more options.\n    IMGUI_API bool          IsItemActive();                                                     // is the last item active? (e.g. button being held, text field being edited. This will continuously return true while holding mouse button on an item. Items that don't interact will always return false)\n    IMGUI_API bool          IsItemFocused();                                                    // is the last item focused for keyboard/gamepad navigation?\n    IMGUI_API bool          IsItemClicked(int mouse_button = 0);                                // is the last item clicked? (e.g. button/node just clicked on) == IsMouseClicked(mouse_button) && IsItemHovered()\n    IMGUI_API bool          IsItemVisible();                                                    // is the last item visible? (items may be out of sight because of clipping/scrolling)\n    IMGUI_API bool          IsItemEdited();                                                     // did the last item modify its underlying value this frame? or was pressed? This is generally the same as the \"bool\" return value of many widgets.\n    IMGUI_API bool          IsItemActivated();                                                  // was the last item just made active (item was previously inactive).\n    IMGUI_API bool          IsItemDeactivated();                                                // was the last item just made inactive (item was previously active). Useful for Undo/Redo patterns with widgets that requires continuous editing.\n    IMGUI_API bool          IsItemDeactivatedAfterEdit();                                       // was the last item just made inactive and made a value change when it was active? (e.g. Slider/Drag moved). Useful for Undo/Redo patterns with widgets that requires continuous editing. Note that you may get false positives (some widgets such as Combo()/ListBox()/Selectable() will return true even when clicking an already selected item).\n    IMGUI_API bool          IsAnyItemHovered();                                                 // is any item hovered?\n    IMGUI_API bool          IsAnyItemActive();                                                  // is any item active?\n    IMGUI_API bool          IsAnyItemFocused();                                                 // is any item focused?\n    IMGUI_API ImVec2        GetItemRectMin();                                                   // get upper-left bounding rectangle of the last item (screen space)\n    IMGUI_API ImVec2        GetItemRectMax();                                                   // get lower-right bounding rectangle of the last item (screen space)\n    IMGUI_API ImVec2        GetItemRectSize();                                                  // get size of last item\n    IMGUI_API void          SetItemAllowOverlap();                                              // allow last item to be overlapped by a subsequent item. sometimes useful with invisible buttons, selectables, etc. to catch unused area.\n\n    // Miscellaneous Utilities\n    IMGUI_API bool          IsRectVisible(const ImVec2& size);                                  // test if rectangle (of given size, starting from cursor position) is visible / not clipped.\n    IMGUI_API bool          IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max);      // test if rectangle (in screen space) is visible / not clipped. to perform coarse clipping on user's side.\n    IMGUI_API double        GetTime();                                                          // get global imgui time. incremented by io.DeltaTime every frame.\n    IMGUI_API int           GetFrameCount();                                                    // get global imgui frame count. incremented by 1 every frame.\n    IMGUI_API ImDrawList*   GetBackgroundDrawList();                                            // get background draw list for the viewport associated to the current window. this draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.\n    IMGUI_API ImDrawList*   GetForegroundDrawList();                                            // get foreground draw list for the viewport associated to the current window. this draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents.\n    IMGUI_API ImDrawList*   GetBackgroundDrawList(ImGuiViewport* viewport);                     // get background draw list for the given viewport. this draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.\n    IMGUI_API ImDrawList*   GetForegroundDrawList(ImGuiViewport* viewport);                     // get foreground draw list for the given viewport. this draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents.\n    IMGUI_API ImDrawListSharedData* GetDrawListSharedData();                                    // you may use this when creating your own ImDrawList instances.\n    IMGUI_API const char*   GetStyleColorName(ImGuiCol idx);                                    // get a string corresponding to the enum value (for display, saving, etc.).\n    IMGUI_API void          SetStateStorage(ImGuiStorage* storage);                             // replace current window storage with our own (if you want to manipulate it yourself, typically clear subsection of it)\n    IMGUI_API ImGuiStorage* GetStateStorage();\n    IMGUI_API ImVec2        CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f);\n    IMGUI_API void          CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end);    // calculate coarse clipping for large list of evenly sized items. Prefer using the ImGuiListClipper higher-level helper if you can.\n    IMGUI_API bool          BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags flags = 0); // helper to create a child window / scrolling region that looks like a normal widget frame\n    IMGUI_API void          EndChildFrame();                                                    // always call EndChildFrame() regardless of BeginChildFrame() return values (which indicates a collapsed/clipped window)\n\n    // Color Utilities\n    IMGUI_API ImVec4        ColorConvertU32ToFloat4(ImU32 in);\n    IMGUI_API ImU32         ColorConvertFloat4ToU32(const ImVec4& in);\n    IMGUI_API void          ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v);\n    IMGUI_API void          ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b);\n\n    // Inputs Utilities\n    IMGUI_API int           GetKeyIndex(ImGuiKey imgui_key);                                    // map ImGuiKey_* values into user's key index. == io.KeyMap[key]\n    IMGUI_API bool          IsKeyDown(int user_key_index);                                      // is key being held. == io.KeysDown[user_key_index]. note that imgui doesn't know the semantic of each entry of io.KeysDown[]. Use your own indices/enums according to how your backend/engine stored them into io.KeysDown[]!\n    IMGUI_API bool          IsKeyPressed(int user_key_index, bool repeat = true);               // was key pressed (went from !Down to Down). if repeat=true, uses io.KeyRepeatDelay / KeyRepeatRate\n    IMGUI_API bool          IsKeyReleased(int user_key_index);                                  // was key released (went from Down to !Down)..\n    IMGUI_API int           GetKeyPressedAmount(int key_index, float repeat_delay, float rate); // uses provided repeat rate/delay. return a count, most often 0 or 1 but might be >1 if RepeatRate is small enough that DeltaTime > RepeatRate\n    IMGUI_API bool          IsMouseDown(int button);                                            // is mouse button held (0=left, 1=right, 2=middle)\n    IMGUI_API bool          IsAnyMouseDown();                                                   // is any mouse button held\n    IMGUI_API bool          IsMouseClicked(int button, bool repeat = false);                    // did mouse button clicked (went from !Down to Down) (0=left, 1=right, 2=middle)\n    IMGUI_API bool          IsMouseDoubleClicked(int button);                                   // did mouse button double-clicked. a double-click returns false in IsMouseClicked(). uses io.MouseDoubleClickTime.\n    IMGUI_API bool          IsMouseReleased(int button);                                        // did mouse button released (went from Down to !Down)\n    IMGUI_API bool          IsMouseDragging(int button = 0, float lock_threshold = -1.0f);      // is mouse dragging. if lock_threshold < -1.0f uses io.MouseDraggingThreshold\n    IMGUI_API bool          IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip = true);  // is mouse hovering given bounding rect (in screen space). clipped by current clipping settings, but disregarding of other consideration of focus/window ordering/popup-block.\n    IMGUI_API bool          IsMousePosValid(const ImVec2* mouse_pos = NULL);                    // by convention we use (-FLT_MAX,-FLT_MAX) to denote that there is no mouse\n    IMGUI_API ImVec2        GetMousePos();                                                      // shortcut to ImGui::GetIO().MousePos provided by user, to be consistent with other calls\n    IMGUI_API ImVec2        GetMousePosOnOpeningCurrentPopup();                                 // retrieve backup of mouse position at the time of opening popup we have BeginPopup() into\n    IMGUI_API ImVec2        GetMouseDragDelta(int button = 0, float lock_threshold = -1.0f);    // return the delta from the initial clicking position while the mouse button is pressed or was just released. This is locked and return 0.0f until the mouse moves past a distance threshold at least once. If lock_threshold < -1.0f uses io.MouseDraggingThreshold.\n    IMGUI_API void          ResetMouseDragDelta(int button = 0);                                //\n    IMGUI_API ImGuiMouseCursor GetMouseCursor();                                                // get desired cursor type, reset in ImGui::NewFrame(), this is updated during the frame. valid before Render(). If you use software rendering by setting io.MouseDrawCursor ImGui will render those for you\n    IMGUI_API void          SetMouseCursor(ImGuiMouseCursor type);                              // set desired cursor type\n    IMGUI_API void          CaptureKeyboardFromApp(bool want_capture_keyboard_value = true);    // attention: misleading name! manually override io.WantCaptureKeyboard flag next frame (said flag is entirely left for your application to handle). e.g. force capture keyboard when your widget is being hovered. This is equivalent to setting \"io.WantCaptureKeyboard = want_capture_keyboard_value\"; after the next NewFrame() call.\n    IMGUI_API void          CaptureMouseFromApp(bool want_capture_mouse_value = true);          // attention: misleading name! manually override io.WantCaptureMouse flag next frame (said flag is entirely left for your application to handle). This is equivalent to setting \"io.WantCaptureMouse = want_capture_mouse_value;\" after the next NewFrame() call.\n\n    // Clipboard Utilities (also see the LogToClipboard() function to capture or output text data to the clipboard)\n    IMGUI_API const char*   GetClipboardText();\n    IMGUI_API void          SetClipboardText(const char* text);\n\n    // Settings/.Ini Utilities\n    // - The disk functions are automatically called if io.IniFilename != NULL (default is \"imgui.ini\").\n    // - Set io.IniFilename to NULL to load/save manually. Read io.WantSaveIniSettings description about handling .ini saving manually.\n    IMGUI_API void          LoadIniSettingsFromDisk(const char* ini_filename);                  // call after CreateContext() and before the first call to NewFrame(). NewFrame() automatically calls LoadIniSettingsFromDisk(io.IniFilename).\n    IMGUI_API void          LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size=0); // call after CreateContext() and before the first call to NewFrame() to provide .ini data from your own data source.\n    IMGUI_API void          SaveIniSettingsToDisk(const char* ini_filename);                    // this is automatically called (if io.IniFilename is not empty) a few seconds after any modification that should be reflected in the .ini file (and also by DestroyContext).\n    IMGUI_API const char*   SaveIniSettingsToMemory(size_t* out_ini_size = NULL);               // return a zero-terminated string with the .ini data which you can save by your own mean. call when io.WantSaveIniSettings is set, then save data by your own mean and clear io.WantSaveIniSettings.\n\n    // Memory Allocators\n    // - All those functions are not reliant on the current context.\n    // - If you reload the contents of imgui.cpp at runtime, you may need to call SetCurrentContext() + SetAllocatorFunctions() again because we use global storage for those.\n    IMGUI_API void          SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data = NULL);\n    IMGUI_API void*         MemAlloc(size_t size);\n    IMGUI_API void          MemFree(void* ptr);\n\n    // (Optional) Platform/OS interface for multi-viewport support\n    // Note: You may use GetWindowViewport() to get the current viewport of the current window.\n    IMGUI_API ImGuiPlatformIO&  GetPlatformIO();                                                // platform/renderer functions, for back-end to setup + viewports list.\n    IMGUI_API ImGuiViewport*    GetMainViewport();                                              // main viewport. same as GetPlatformIO().MainViewport == GetPlatformIO().Viewports[0].\n    IMGUI_API void              UpdatePlatformWindows();                                        // call in main loop. will call CreateWindow/ResizeWindow/etc. platform functions for each secondary viewport, and DestroyWindow for each inactive viewport.\n    IMGUI_API void              RenderPlatformWindowsDefault(void* platform_arg = NULL, void* renderer_arg = NULL); // call in main loop. will call RenderWindow/SwapBuffers platform functions for each secondary viewport which doesn't have the ImGuiViewportFlags_Minimized flag set. May be reimplemented by user for custom rendering needs.\n    IMGUI_API void              DestroyPlatformWindows();                                       // call DestroyWindow platform functions for all viewports. call from back-end Shutdown() if you need to close platform windows before imgui shutdown. otherwise will be called by DestroyContext().\n    IMGUI_API ImGuiViewport*    FindViewportByID(ImGuiID id);                                   // this is a helper for back-ends.\n    IMGUI_API ImGuiViewport*    FindViewportByPlatformHandle(void* platform_handle);            // this is a helper for back-ends. the type platform_handle is decided by the back-end (e.g. HWND, MyWindow*, GLFWwindow* etc.)\n\n} // namespace ImGui\n\n//-----------------------------------------------------------------------------\n// Flags & Enumerations\n//-----------------------------------------------------------------------------\n\n// Flags for ImGui::Begin()\nenum ImGuiWindowFlags_\n{\n    ImGuiWindowFlags_None                   = 0,\n    ImGuiWindowFlags_NoTitleBar             = 1 << 0,   // Disable title-bar\n    ImGuiWindowFlags_NoResize               = 1 << 1,   // Disable user resizing with the lower-right grip\n    ImGuiWindowFlags_NoMove                 = 1 << 2,   // Disable user moving the window\n    ImGuiWindowFlags_NoScrollbar            = 1 << 3,   // Disable scrollbars (window can still scroll with mouse or programmatically)\n    ImGuiWindowFlags_NoScrollWithMouse      = 1 << 4,   // Disable user vertically scrolling with mouse wheel. On child window, mouse wheel will be forwarded to the parent unless NoScrollbar is also set.\n    ImGuiWindowFlags_NoCollapse             = 1 << 5,   // Disable user collapsing window by double-clicking on it\n    ImGuiWindowFlags_AlwaysAutoResize       = 1 << 6,   // Resize every window to its content every frame\n    ImGuiWindowFlags_NoBackground           = 1 << 7,   // Disable drawing background color (WindowBg, etc.) and outside border. Similar as using SetNextWindowBgAlpha(0.0f).\n    ImGuiWindowFlags_NoSavedSettings        = 1 << 8,   // Never load/save settings in .ini file\n    ImGuiWindowFlags_NoMouseInputs          = 1 << 9,   // Disable catching mouse, hovering test with pass through.\n    ImGuiWindowFlags_MenuBar                = 1 << 10,  // Has a menu-bar\n    ImGuiWindowFlags_HorizontalScrollbar    = 1 << 11,  // Allow horizontal scrollbar to appear (off by default). You may use SetNextWindowContentSize(ImVec2(width,0.0f)); prior to calling Begin() to specify width. Read code in imgui_demo in the \"Horizontal Scrolling\" section.\n    ImGuiWindowFlags_NoFocusOnAppearing     = 1 << 12,  // Disable taking focus when transitioning from hidden to visible state\n    ImGuiWindowFlags_NoBringToFrontOnFocus  = 1 << 13,  // Disable bringing window to front when taking focus (e.g. clicking on it or programmatically giving it focus)\n    ImGuiWindowFlags_AlwaysVerticalScrollbar= 1 << 14,  // Always show vertical scrollbar (even if ContentSize.y < Size.y)\n    ImGuiWindowFlags_AlwaysHorizontalScrollbar=1<< 15,  // Always show horizontal scrollbar (even if ContentSize.x < Size.x)\n    ImGuiWindowFlags_AlwaysUseWindowPadding = 1 << 16,  // Ensure child windows without border uses style.WindowPadding (ignored by default for non-bordered child windows, because more convenient)\n    ImGuiWindowFlags_NoNavInputs            = 1 << 18,  // No gamepad/keyboard navigation within the window\n    ImGuiWindowFlags_NoNavFocus             = 1 << 19,  // No focusing toward this window with gamepad/keyboard navigation (e.g. skipped by CTRL+TAB)\n    ImGuiWindowFlags_UnsavedDocument        = 1 << 20,  // Append '*' to title without affecting the ID, as a convenience to avoid using the ### operator. When used in a tab/docking context, tab is selected on closure and closure is deferred by one frame to allow code to cancel the closure (with a confirmation popup, etc.) without flicker.\n    ImGuiWindowFlags_NoDocking              = 1 << 21,  // Disable docking of this window\n\n    ImGuiWindowFlags_NoNav                  = ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus,\n    ImGuiWindowFlags_NoDecoration           = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse,\n    ImGuiWindowFlags_NoInputs               = ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus,\n\n    // [Internal]\n    ImGuiWindowFlags_NavFlattened           = 1 << 23,  // [BETA] Allow gamepad/keyboard navigation to cross over parent border to this child (only use on child that have no scrolling!)\n    ImGuiWindowFlags_ChildWindow            = 1 << 24,  // Don't use! For internal use by BeginChild()\n    ImGuiWindowFlags_Tooltip                = 1 << 25,  // Don't use! For internal use by BeginTooltip()\n    ImGuiWindowFlags_Popup                  = 1 << 26,  // Don't use! For internal use by BeginPopup()\n    ImGuiWindowFlags_Modal                  = 1 << 27,  // Don't use! For internal use by BeginPopupModal()\n    ImGuiWindowFlags_ChildMenu              = 1 << 28,  // Don't use! For internal use by BeginMenu()\n    ImGuiWindowFlags_DockNodeHost           = 1 << 29   // Don't use! For internal use by Begin()/NewFrame()\n\n    // [Obsolete]\n    //ImGuiWindowFlags_ShowBorders          = 1 << 7,   // --> Set style.FrameBorderSize=1.0f / style.WindowBorderSize=1.0f to enable borders around windows and items\n    //ImGuiWindowFlags_ResizeFromAnySide    = 1 << 17,  // --> Set io.ConfigWindowsResizeFromEdges and make sure mouse cursors are supported by back-end (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors)\n};\n\n// Flags for ImGui::InputText()\nenum ImGuiInputTextFlags_\n{\n    ImGuiInputTextFlags_None                = 0,\n    ImGuiInputTextFlags_CharsDecimal        = 1 << 0,   // Allow 0123456789.+-*/\n    ImGuiInputTextFlags_CharsHexadecimal    = 1 << 1,   // Allow 0123456789ABCDEFabcdef\n    ImGuiInputTextFlags_CharsUppercase      = 1 << 2,   // Turn a..z into A..Z\n    ImGuiInputTextFlags_CharsNoBlank        = 1 << 3,   // Filter out spaces, tabs\n    ImGuiInputTextFlags_AutoSelectAll       = 1 << 4,   // Select entire text when first taking mouse focus\n    ImGuiInputTextFlags_EnterReturnsTrue    = 1 << 5,   // Return 'true' when Enter is pressed (as opposed to every time the value was modified). Consider looking at the IsItemDeactivatedAfterEdit() function.\n    ImGuiInputTextFlags_CallbackCompletion  = 1 << 6,   // Callback on pressing TAB (for completion handling)\n    ImGuiInputTextFlags_CallbackHistory     = 1 << 7,   // Callback on pressing Up/Down arrows (for history handling)\n    ImGuiInputTextFlags_CallbackAlways      = 1 << 8,   // Callback on each iteration. User code may query cursor position, modify text buffer.\n    ImGuiInputTextFlags_CallbackCharFilter  = 1 << 9,   // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard.\n    ImGuiInputTextFlags_AllowTabInput       = 1 << 10,  // Pressing TAB input a '\\t' character into the text field\n    ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 11,  // In multi-line mode, unfocus with Enter, add new line with Ctrl+Enter (default is opposite: unfocus with Ctrl+Enter, add line with Enter).\n    ImGuiInputTextFlags_NoHorizontalScroll  = 1 << 12,  // Disable following the cursor horizontally\n    ImGuiInputTextFlags_AlwaysInsertMode    = 1 << 13,  // Insert mode\n    ImGuiInputTextFlags_ReadOnly            = 1 << 14,  // Read-only mode\n    ImGuiInputTextFlags_Password            = 1 << 15,  // Password mode, display all characters as '*'\n    ImGuiInputTextFlags_NoUndoRedo          = 1 << 16,  // Disable undo/redo. Note that input text owns the text data while active, if you want to provide your own undo/redo stack you need e.g. to call ClearActiveID().\n    ImGuiInputTextFlags_CharsScientific     = 1 << 17,  // Allow 0123456789.+-*/eE (Scientific notation input)\n    ImGuiInputTextFlags_CallbackResize      = 1 << 18,  // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this)\n    // [Internal]\n    ImGuiInputTextFlags_Multiline           = 1 << 20   // For internal use by InputTextMultiline()\n};\n\n// Flags for ImGui::TreeNodeEx(), ImGui::CollapsingHeader*()\nenum ImGuiTreeNodeFlags_\n{\n    ImGuiTreeNodeFlags_None                 = 0,\n    ImGuiTreeNodeFlags_Selected             = 1 << 0,   // Draw as selected\n    ImGuiTreeNodeFlags_Framed               = 1 << 1,   // Full colored frame (e.g. for CollapsingHeader)\n    ImGuiTreeNodeFlags_AllowItemOverlap     = 1 << 2,   // Hit testing to allow subsequent widgets to overlap this one\n    ImGuiTreeNodeFlags_NoTreePushOnOpen     = 1 << 3,   // Don't do a TreePush() when open (e.g. for CollapsingHeader) = no extra indent nor pushing on ID stack\n    ImGuiTreeNodeFlags_NoAutoOpenOnLog      = 1 << 4,   // Don't automatically and temporarily open node when Logging is active (by default logging will automatically open tree nodes)\n    ImGuiTreeNodeFlags_DefaultOpen          = 1 << 5,   // Default node to be open\n    ImGuiTreeNodeFlags_OpenOnDoubleClick    = 1 << 6,   // Need double-click to open node\n    ImGuiTreeNodeFlags_OpenOnArrow          = 1 << 7,   // Only open when clicking on the arrow part. If ImGuiTreeNodeFlags_OpenOnDoubleClick is also set, single-click arrow or double-click all box to open.\n    ImGuiTreeNodeFlags_Leaf                 = 1 << 8,   // No collapsing, no arrow (use as a convenience for leaf nodes).\n    ImGuiTreeNodeFlags_Bullet               = 1 << 9,   // Display a bullet instead of arrow\n    ImGuiTreeNodeFlags_FramePadding         = 1 << 10,  // Use FramePadding (even for an unframed text node) to vertically align text baseline to regular widget height. Equivalent to calling AlignTextToFramePadding().\n    //ImGuiTreeNodeFlags_SpanAllAvailWidth  = 1 << 11,  // FIXME: TODO: Extend hit box horizontally even if not framed\n    //ImGuiTreeNodeFlags_NoScrollOnOpen     = 1 << 12,  // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible\n    ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 13,  // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop)\n    ImGuiTreeNodeFlags_CollapsingHeader     = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog\n\n    // Obsolete names (will be removed)\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    , ImGuiTreeNodeFlags_AllowOverlapMode = ImGuiTreeNodeFlags_AllowItemOverlap\n#endif\n};\n\n// Flags for ImGui::Selectable()\nenum ImGuiSelectableFlags_\n{\n    ImGuiSelectableFlags_None               = 0,\n    ImGuiSelectableFlags_DontClosePopups    = 1 << 0,   // Clicking this don't close parent popup window\n    ImGuiSelectableFlags_SpanAllColumns     = 1 << 1,   // Selectable frame can span all columns (text will still fit in current column)\n    ImGuiSelectableFlags_AllowDoubleClick   = 1 << 2,   // Generate press events on double clicks too\n    ImGuiSelectableFlags_Disabled           = 1 << 3    // Cannot be selected, display greyed out text\n};\n\n// Flags for ImGui::BeginCombo()\nenum ImGuiComboFlags_\n{\n    ImGuiComboFlags_None                    = 0,\n    ImGuiComboFlags_PopupAlignLeft          = 1 << 0,   // Align the popup toward the left by default\n    ImGuiComboFlags_HeightSmall             = 1 << 1,   // Max ~4 items visible. Tip: If you want your combo popup to be a specific size you can use SetNextWindowSizeConstraints() prior to calling BeginCombo()\n    ImGuiComboFlags_HeightRegular           = 1 << 2,   // Max ~8 items visible (default)\n    ImGuiComboFlags_HeightLarge             = 1 << 3,   // Max ~20 items visible\n    ImGuiComboFlags_HeightLargest           = 1 << 4,   // As many fitting items as possible\n    ImGuiComboFlags_NoArrowButton           = 1 << 5,   // Display on the preview box without the square arrow button\n    ImGuiComboFlags_NoPreview               = 1 << 6,   // Display only a square arrow button\n    ImGuiComboFlags_HeightMask_             = ImGuiComboFlags_HeightSmall | ImGuiComboFlags_HeightRegular | ImGuiComboFlags_HeightLarge | ImGuiComboFlags_HeightLargest\n};\n\n// Flags for ImGui::BeginTabBar()\nenum ImGuiTabBarFlags_\n{\n    ImGuiTabBarFlags_None                           = 0,\n    ImGuiTabBarFlags_Reorderable                    = 1 << 0,   // Allow manually dragging tabs to re-order them + New tabs are appended at the end of list\n    ImGuiTabBarFlags_AutoSelectNewTabs              = 1 << 1,   // Automatically select new tabs when they appear\n    ImGuiTabBarFlags_TabListPopupButton             = 1 << 2,\n    ImGuiTabBarFlags_NoCloseWithMiddleMouseButton   = 1 << 3,   // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false.\n    ImGuiTabBarFlags_NoTabListScrollingButtons      = 1 << 4,\n    ImGuiTabBarFlags_NoTooltip                      = 1 << 5,   // Disable tooltips when hovering a tab\n    ImGuiTabBarFlags_FittingPolicyResizeDown        = 1 << 6,   // Resize tabs when they don't fit\n    ImGuiTabBarFlags_FittingPolicyScroll            = 1 << 7,   // Add scroll buttons when tabs don't fit\n    ImGuiTabBarFlags_FittingPolicyMask_             = ImGuiTabBarFlags_FittingPolicyResizeDown | ImGuiTabBarFlags_FittingPolicyScroll,\n    ImGuiTabBarFlags_FittingPolicyDefault_          = ImGuiTabBarFlags_FittingPolicyResizeDown\n};\n\n// Flags for ImGui::BeginTabItem()\nenum ImGuiTabItemFlags_\n{\n    ImGuiTabItemFlags_None                          = 0,\n    ImGuiTabItemFlags_UnsavedDocument               = 1 << 0,   // Append '*' to title without affecting the ID, as a convenience to avoid using the ### operator. Also: tab is selected on closure and closure is deferred by one frame to allow code to undo it without flicker.\n    ImGuiTabItemFlags_SetSelected                   = 1 << 1,   // Trigger flag to programmatically make the tab selected when calling BeginTabItem()\n    ImGuiTabItemFlags_NoCloseWithMiddleMouseButton  = 1 << 2,   // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false.\n    ImGuiTabItemFlags_NoPushId                      = 1 << 3    // Don't call PushID(tab->ID)/PopID() on BeginTabItem()/EndTabItem()\n};\n\n// Flags for ImGui::DockSpace(), shared/inherited by child nodes.\n// (Some flags can be applied to individual nodes directly)\nenum ImGuiDockNodeFlags_\n{\n    ImGuiDockNodeFlags_None                         = 0,\n    ImGuiDockNodeFlags_KeepAliveOnly                = 1 << 0,   // Shared       // Don't display the dockspace node but keep it alive. Windows docked into this dockspace node won't be undocked.\n    //ImGuiDockNodeFlags_NoCentralNode              = 1 << 1,   // Shared       // Disable Central Node (the node which can stay empty)\n    ImGuiDockNodeFlags_NoDockingInCentralNode       = 1 << 2,   // Shared       // Disable docking inside the Central Node, which will be always kept empty.\n    ImGuiDockNodeFlags_PassthruCentralNode          = 1 << 3,   // Shared       // Enable passthru dockspace: 1) DockSpace() will render a ImGuiCol_WindowBg background covering everything excepted the Central Node when empty. Meaning the host window should probably use SetNextWindowBgAlpha(0.0f) prior to Begin() when using this. 2) When Central Node is empty: let inputs pass-through + won't display a DockingEmptyBg background. See demo for details.\n    ImGuiDockNodeFlags_NoSplit                      = 1 << 4,   // Shared/Local // Disable splitting the node into smaller nodes. Useful e.g. when embedding dockspaces into a main root one (the root one may have splitting disabled to reduce confusion). Note: when turned off, existing splits will be preserved.\n    ImGuiDockNodeFlags_NoResize                     = 1 << 5,   // Shared/Local // Disable resizing child nodes using the splitter/separators. Useful with programatically setup dockspaces. \n    ImGuiDockNodeFlags_AutoHideTabBar               = 1 << 6    // Shared/Local // Tab bar will automatically hide when there is a single window in the dock node.\n};\n\n// Flags for ImGui::IsWindowFocused()\nenum ImGuiFocusedFlags_\n{\n    ImGuiFocusedFlags_None                          = 0,\n    ImGuiFocusedFlags_ChildWindows                  = 1 << 0,   // IsWindowFocused(): Return true if any children of the window is focused\n    ImGuiFocusedFlags_RootWindow                    = 1 << 1,   // IsWindowFocused(): Test from root window (top most parent of the current hierarchy)\n    ImGuiFocusedFlags_AnyWindow                     = 1 << 2,   // IsWindowFocused(): Return true if any window is focused. Important: If you are trying to tell how to dispatch your low-level inputs, do NOT use this. Use ImGui::GetIO().WantCaptureMouse instead.\n    ImGuiFocusedFlags_RootAndChildWindows           = ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows\n};\n\n// Flags for ImGui::IsItemHovered(), ImGui::IsWindowHovered()\n// Note: if you are trying to check whether your mouse should be dispatched to imgui or to your app, you should use the 'io.WantCaptureMouse' boolean for that. Please read the FAQ!\n// Note: windows with the ImGuiWindowFlags_NoInputs flag are ignored by IsWindowHovered() calls.\nenum ImGuiHoveredFlags_\n{\n    ImGuiHoveredFlags_None                          = 0,        // Return true if directly over the item/window, not obstructed by another window, not obstructed by an active popup or modal blocking inputs under them.\n    ImGuiHoveredFlags_ChildWindows                  = 1 << 0,   // IsWindowHovered() only: Return true if any children of the window is hovered\n    ImGuiHoveredFlags_RootWindow                    = 1 << 1,   // IsWindowHovered() only: Test from root window (top most parent of the current hierarchy)\n    ImGuiHoveredFlags_AnyWindow                     = 1 << 2,   // IsWindowHovered() only: Return true if any window is hovered\n    ImGuiHoveredFlags_AllowWhenBlockedByPopup       = 1 << 3,   // Return true even if a popup window is normally blocking access to this item/window\n    //ImGuiHoveredFlags_AllowWhenBlockedByModal     = 1 << 4,   // Return true even if a modal popup window is normally blocking access to this item/window. FIXME-TODO: Unavailable yet.\n    ImGuiHoveredFlags_AllowWhenBlockedByActiveItem  = 1 << 5,   // Return true even if an active item is blocking access to this item/window. Useful for Drag and Drop patterns.\n    ImGuiHoveredFlags_AllowWhenOverlapped           = 1 << 6,   // Return true even if the position is overlapped by another window\n    ImGuiHoveredFlags_AllowWhenDisabled             = 1 << 7,   // Return true even if the item is disabled\n    ImGuiHoveredFlags_RectOnly                      = ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenOverlapped,\n    ImGuiHoveredFlags_RootAndChildWindows           = ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows\n};\n\n// Flags for ImGui::BeginDragDropSource(), ImGui::AcceptDragDropPayload()\nenum ImGuiDragDropFlags_\n{\n    ImGuiDragDropFlags_None                         = 0,\n    // BeginDragDropSource() flags\n    ImGuiDragDropFlags_SourceNoPreviewTooltip       = 1 << 0,   // By default, a successful call to BeginDragDropSource opens a tooltip so you can display a preview or description of the source contents. This flag disable this behavior.\n    ImGuiDragDropFlags_SourceNoDisableHover         = 1 << 1,   // By default, when dragging we clear data so that IsItemHovered() will return false, to avoid subsequent user code submitting tooltips. This flag disable this behavior so you can still call IsItemHovered() on the source item.\n    ImGuiDragDropFlags_SourceNoHoldToOpenOthers     = 1 << 2,   // Disable the behavior that allows to open tree nodes and collapsing header by holding over them while dragging a source item.\n    ImGuiDragDropFlags_SourceAllowNullID            = 1 << 3,   // Allow items such as Text(), Image() that have no unique identifier to be used as drag source, by manufacturing a temporary identifier based on their window-relative position. This is extremely unusual within the dear imgui ecosystem and so we made it explicit.\n    ImGuiDragDropFlags_SourceExtern                 = 1 << 4,   // External source (from outside of imgui), won't attempt to read current item/window info. Will always return true. Only one Extern source can be active simultaneously.\n    ImGuiDragDropFlags_SourceAutoExpirePayload      = 1 << 5,   // Automatically expire the payload if the source cease to be submitted (otherwise payloads are persisting while being dragged)\n    // AcceptDragDropPayload() flags\n    ImGuiDragDropFlags_AcceptBeforeDelivery         = 1 << 10,  // AcceptDragDropPayload() will returns true even before the mouse button is released. You can then call IsDelivery() to test if the payload needs to be delivered.\n    ImGuiDragDropFlags_AcceptNoDrawDefaultRect      = 1 << 11,  // Do not draw the default highlight rectangle when hovering over target.\n    ImGuiDragDropFlags_AcceptNoPreviewTooltip       = 1 << 12,  // Request hiding the BeginDragDropSource tooltip from the BeginDragDropTarget site.\n    ImGuiDragDropFlags_AcceptPeekOnly               = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect  // For peeking ahead and inspecting the payload before delivery.\n};\n\n// Standard Drag and Drop payload types. You can define you own payload types using short strings. Types starting with '_' are defined by Dear ImGui.\n#define IMGUI_PAYLOAD_TYPE_COLOR_3F     \"_COL3F\"    // float[3]: Standard type for colors, without alpha. User code may use this type.\n#define IMGUI_PAYLOAD_TYPE_COLOR_4F     \"_COL4F\"    // float[4]: Standard type for colors. User code may use this type.\n\n// A primary data type\nenum ImGuiDataType_\n{\n    ImGuiDataType_S8,       // char\n    ImGuiDataType_U8,       // unsigned char\n    ImGuiDataType_S16,      // short\n    ImGuiDataType_U16,      // unsigned short\n    ImGuiDataType_S32,      // int\n    ImGuiDataType_U32,      // unsigned int\n    ImGuiDataType_S64,      // long long / __int64\n    ImGuiDataType_U64,      // unsigned long long / unsigned __int64\n    ImGuiDataType_Float,    // float\n    ImGuiDataType_Double,   // double\n    ImGuiDataType_COUNT\n};\n\n// A cardinal direction\nenum ImGuiDir_\n{\n    ImGuiDir_None    = -1,\n    ImGuiDir_Left    = 0,\n    ImGuiDir_Right   = 1,\n    ImGuiDir_Up      = 2,\n    ImGuiDir_Down    = 3,\n    ImGuiDir_COUNT\n};\n\n// User fill ImGuiIO.KeyMap[] array with indices into the ImGuiIO.KeysDown[512] array\nenum ImGuiKey_\n{\n    ImGuiKey_Tab,\n    ImGuiKey_LeftArrow,\n    ImGuiKey_RightArrow,\n    ImGuiKey_UpArrow,\n    ImGuiKey_DownArrow,\n    ImGuiKey_PageUp,\n    ImGuiKey_PageDown,\n    ImGuiKey_Home,\n    ImGuiKey_End,\n    ImGuiKey_Insert,\n    ImGuiKey_Delete,\n    ImGuiKey_Backspace,\n    ImGuiKey_Space,\n    ImGuiKey_Enter,\n    ImGuiKey_Escape,\n    ImGuiKey_A,         // for text edit CTRL+A: select all\n    ImGuiKey_C,         // for text edit CTRL+C: copy\n    ImGuiKey_V,         // for text edit CTRL+V: paste\n    ImGuiKey_X,         // for text edit CTRL+X: cut\n    ImGuiKey_Y,         // for text edit CTRL+Y: redo\n    ImGuiKey_Z,         // for text edit CTRL+Z: undo\n    ImGuiKey_COUNT\n};\n\n// Gamepad/Keyboard directional navigation\n// Keyboard: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays.\n// Gamepad:  Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. Back-end: set ImGuiBackendFlags_HasGamepad and fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame().\n// Read instructions in imgui.cpp for more details. Download PNG/PSD at http://goo.gl/9LgVZW.\nenum ImGuiNavInput_\n{\n    // Gamepad Mapping\n    ImGuiNavInput_Activate,      // activate / open / toggle / tweak value       // e.g. Cross  (PS4), A (Xbox), A (Switch), Space (Keyboard)\n    ImGuiNavInput_Cancel,        // cancel / close / exit                        // e.g. Circle (PS4), B (Xbox), B (Switch), Escape (Keyboard)\n    ImGuiNavInput_Input,         // text input / on-screen keyboard              // e.g. Triang.(PS4), Y (Xbox), X (Switch), Return (Keyboard)\n    ImGuiNavInput_Menu,          // tap: toggle menu / hold: focus, move, resize // e.g. Square (PS4), X (Xbox), Y (Switch), Alt (Keyboard)\n    ImGuiNavInput_DpadLeft,      // move / tweak / resize window (w/ PadMenu)    // e.g. D-pad Left/Right/Up/Down (Gamepads), Arrow keys (Keyboard)\n    ImGuiNavInput_DpadRight,     //\n    ImGuiNavInput_DpadUp,        //\n    ImGuiNavInput_DpadDown,      //\n    ImGuiNavInput_LStickLeft,    // scroll / move window (w/ PadMenu)            // e.g. Left Analog Stick Left/Right/Up/Down\n    ImGuiNavInput_LStickRight,   //\n    ImGuiNavInput_LStickUp,      //\n    ImGuiNavInput_LStickDown,    //\n    ImGuiNavInput_FocusPrev,     // next window (w/ PadMenu)                     // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch)\n    ImGuiNavInput_FocusNext,     // prev window (w/ PadMenu)                     // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch)\n    ImGuiNavInput_TweakSlow,     // slower tweaks                                // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch)\n    ImGuiNavInput_TweakFast,     // faster tweaks                                // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch)\n\n    // [Internal] Don't use directly! This is used internally to differentiate keyboard from gamepad inputs for behaviors that require to differentiate them.\n    // Keyboard behavior that have no corresponding gamepad mapping (e.g. CTRL+TAB) will be directly reading from io.KeysDown[] instead of io.NavInputs[].\n    ImGuiNavInput_KeyMenu_,      // toggle menu                                  // = io.KeyAlt\n    ImGuiNavInput_KeyTab_,       // tab                                          // = Tab key\n    ImGuiNavInput_KeyLeft_,      // move left                                    // = Arrow keys\n    ImGuiNavInput_KeyRight_,     // move right\n    ImGuiNavInput_KeyUp_,        // move up\n    ImGuiNavInput_KeyDown_,      // move down\n    ImGuiNavInput_COUNT,\n    ImGuiNavInput_InternalStart_ = ImGuiNavInput_KeyMenu_\n};\n\n// Configuration flags stored in io.ConfigFlags. Set by user/application.\nenum ImGuiConfigFlags_\n{\n    ImGuiConfigFlags_None                   = 0,\n    ImGuiConfigFlags_NavEnableKeyboard      = 1 << 0,   // Master keyboard navigation enable flag. NewFrame() will automatically fill io.NavInputs[] based on io.KeysDown[].\n    ImGuiConfigFlags_NavEnableGamepad       = 1 << 1,   // Master gamepad navigation enable flag. This is mostly to instruct your imgui back-end to fill io.NavInputs[]. Back-end also needs to set ImGuiBackendFlags_HasGamepad.\n    ImGuiConfigFlags_NavEnableSetMousePos   = 1 << 2,   // Instruct navigation to move the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is awkward. Will update io.MousePos and set io.WantSetMousePos=true. If enabled you MUST honor io.WantSetMousePos requests in your binding, otherwise ImGui will react as if the mouse is jumping around back and forth.\n    ImGuiConfigFlags_NavNoCaptureKeyboard   = 1 << 3,   // Instruct navigation to not set the io.WantCaptureKeyboard flag when io.NavActive is set.\n    ImGuiConfigFlags_NoMouse                = 1 << 4,   // Instruct imgui to clear mouse position/buttons in NewFrame(). This allows ignoring the mouse information set by the back-end.\n    ImGuiConfigFlags_NoMouseCursorChange    = 1 << 5,   // Instruct back-end to not alter mouse cursor shape and visibility. Use if the back-end cursor changes are interfering with yours and you don't want to use SetMouseCursor() to change mouse cursor. You may want to honor requests from imgui by reading GetMouseCursor() yourself instead.\n\n    // [BETA] Docking\n    ImGuiConfigFlags_DockingEnable          = 1 << 6,   // Docking enable flags. Use SHIFT to dock window into another (or without SHIFT if io.ConfigDockingWithShift = false).\n\n    // [BETA] Viewports\n    // When using viewports it is recommended that your default value for ImGuiCol_WindowBg is opaque (Alpha=1.0) so transition to a viewport won't be noticeable.\n    ImGuiConfigFlags_ViewportsEnable        = 1 << 10,  // Viewport enable flags (require both ImGuiConfigFlags_PlatformHasViewports + ImGuiConfigFlags_RendererHasViewports set by the respective back-ends)\n    ImGuiConfigFlags_DpiEnableScaleViewports= 1 << 14,  // [BETA: Don't use] FIXME-DPI: Reposition and resize imgui windows when the DpiScale of a viewport changed (mostly useful for the main viewport hosting other window). Note that resizing the main window itself is up to your application.\n    ImGuiConfigFlags_DpiEnableScaleFonts    = 1 << 15,  // [BETA: Don't use] FIXME-DPI: Request bitmap-scaled fonts to match DpiScale. This is a very low-quality workaround. The correct way to handle DPI is _currently_ to replace the atlas and/or fonts in the Platform_OnChangedViewport callback, but this is all early work in progress.\n\n    // User storage (to allow your back-end/engine to communicate to code that may be shared between multiple projects. Those flags are not used by core ImGui)\n    ImGuiConfigFlags_IsSRGB                 = 1 << 20,  // Application is SRGB-aware.\n    ImGuiConfigFlags_IsTouchScreen          = 1 << 21   // Application is using a touch screen instead of a mouse.\n};\n\n// Back-end capabilities flags stored in io.BackendFlags. Set by imgui_impl_xxx or custom back-end.\nenum ImGuiBackendFlags_\n{\n    ImGuiBackendFlags_None                  = 0,\n    ImGuiBackendFlags_HasGamepad            = 1 << 0,   // Back-end supports gamepad and currently has one connected.\n    ImGuiBackendFlags_HasMouseCursors       = 1 << 1,   // Back-end supports honoring GetMouseCursor() value to change the OS cursor shape.\n    ImGuiBackendFlags_HasSetMousePos        = 1 << 2,   // Back-end supports io.WantSetMousePos requests to reposition the OS mouse position (only used if ImGuiConfigFlags_NavEnableSetMousePos is set).\n\n    // [BETA] Viewports\n    ImGuiBackendFlags_PlatformHasViewports  = 1 << 10,  // Back-end Platform supports multiple viewports.\n    ImGuiBackendFlags_HasMouseHoveredViewport=1 << 11,  // Back-end Platform supports setting io.MouseHoveredViewport to the viewport directly under the mouse _IGNORING_ viewports with the ImGuiViewportFlags_NoInputs flag and _REGARDLESS_ of whether another viewport is focused and may be capturing the mouse. This information is _NOT EASY_ to provide correctly with most high-level engines! Don't set this without studying how the examples/ back-end handle it!\n    ImGuiBackendFlags_RendererHasViewports  = 1 << 12   // Back-end Renderer supports multiple viewports.\n};\n\n// Enumeration for PushStyleColor() / PopStyleColor()\nenum ImGuiCol_\n{\n    ImGuiCol_Text,\n    ImGuiCol_TextDisabled,\n    ImGuiCol_WindowBg,              // Background of normal windows\n    ImGuiCol_ChildBg,               // Background of child windows\n    ImGuiCol_PopupBg,               // Background of popups, menus, tooltips windows\n    ImGuiCol_Border,\n    ImGuiCol_BorderShadow,\n    ImGuiCol_FrameBg,               // Background of checkbox, radio button, plot, slider, text input\n    ImGuiCol_FrameBgHovered,\n    ImGuiCol_FrameBgActive,\n    ImGuiCol_TitleBg,\n    ImGuiCol_TitleBgActive,\n    ImGuiCol_TitleBgCollapsed,\n    ImGuiCol_MenuBarBg,\n    ImGuiCol_ScrollbarBg,\n    ImGuiCol_ScrollbarGrab,\n    ImGuiCol_ScrollbarGrabHovered,\n    ImGuiCol_ScrollbarGrabActive,\n    ImGuiCol_CheckMark,\n    ImGuiCol_SliderGrab,\n    ImGuiCol_SliderGrabActive,\n    ImGuiCol_Button,\n    ImGuiCol_ButtonHovered,\n    ImGuiCol_ButtonActive,\n    ImGuiCol_Header,\n    ImGuiCol_HeaderHovered,\n    ImGuiCol_HeaderActive,\n    ImGuiCol_Separator,\n    ImGuiCol_SeparatorHovered,\n    ImGuiCol_SeparatorActive,\n    ImGuiCol_ResizeGrip,\n    ImGuiCol_ResizeGripHovered,\n    ImGuiCol_ResizeGripActive,\n    ImGuiCol_Tab,\n    ImGuiCol_TabHovered,\n    ImGuiCol_TabActive,\n    ImGuiCol_TabUnfocused,\n    ImGuiCol_TabUnfocusedActive,\n    ImGuiCol_DockingPreview,\n    ImGuiCol_DockingEmptyBg,        // Background color for empty node (e.g. CentralNode with no window docked into it)\n    ImGuiCol_PlotLines,\n    ImGuiCol_PlotLinesHovered,\n    ImGuiCol_PlotHistogram,\n    ImGuiCol_PlotHistogramHovered,\n    ImGuiCol_TextSelectedBg,\n    ImGuiCol_DragDropTarget,\n    ImGuiCol_NavHighlight,          // Gamepad/keyboard: current highlighted item\n    ImGuiCol_NavWindowingHighlight, // Highlight window when using CTRL+TAB\n    ImGuiCol_NavWindowingDimBg,     // Darken/colorize entire screen behind the CTRL+TAB window list, when active\n    ImGuiCol_ModalWindowDimBg,      // Darken/colorize entire screen behind a modal window, when one is active\n    ImGuiCol_COUNT\n\n    // Obsolete names (will be removed)\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    , ImGuiCol_ModalWindowDarkening = ImGuiCol_ModalWindowDimBg                      // [renamed in 1.63]\n    , ImGuiCol_ChildWindowBg = ImGuiCol_ChildBg                                      // [renamed in 1.53]\n    , ImGuiCol_Column = ImGuiCol_Separator, ImGuiCol_ColumnHovered = ImGuiCol_SeparatorHovered, ImGuiCol_ColumnActive = ImGuiCol_SeparatorActive  // [renamed in 1.51]\n    //ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered, // [unused since 1.60+] the close button now uses regular button colors.\n    //ImGuiCol_ComboBg,                                                              // [unused since 1.53+] ComboBg has been merged with PopupBg, so a redirect isn't accurate.\n#endif\n};\n\n// Enumeration for PushStyleVar() / PopStyleVar() to temporarily modify the ImGuiStyle structure.\n// NB: the enum only refers to fields of ImGuiStyle which makes sense to be pushed/popped inside UI code. During initialization, feel free to just poke into ImGuiStyle directly.\n// NB: if changing this enum, you need to update the associated internal table GStyleVarInfo[] accordingly. This is where we link enum values to members offset/type.\nenum ImGuiStyleVar_\n{\n    // Enum name --------------------- // Member in ImGuiStyle structure (see ImGuiStyle for descriptions)\n    ImGuiStyleVar_Alpha,               // float     Alpha\n    ImGuiStyleVar_WindowPadding,       // ImVec2    WindowPadding\n    ImGuiStyleVar_WindowRounding,      // float     WindowRounding\n    ImGuiStyleVar_WindowBorderSize,    // float     WindowBorderSize\n    ImGuiStyleVar_WindowMinSize,       // ImVec2    WindowMinSize\n    ImGuiStyleVar_WindowTitleAlign,    // ImVec2    WindowTitleAlign\n    ImGuiStyleVar_ChildRounding,       // float     ChildRounding\n    ImGuiStyleVar_ChildBorderSize,     // float     ChildBorderSize\n    ImGuiStyleVar_PopupRounding,       // float     PopupRounding\n    ImGuiStyleVar_PopupBorderSize,     // float     PopupBorderSize\n    ImGuiStyleVar_FramePadding,        // ImVec2    FramePadding\n    ImGuiStyleVar_FrameRounding,       // float     FrameRounding\n    ImGuiStyleVar_FrameBorderSize,     // float     FrameBorderSize\n    ImGuiStyleVar_ItemSpacing,         // ImVec2    ItemSpacing\n    ImGuiStyleVar_ItemInnerSpacing,    // ImVec2    ItemInnerSpacing\n    ImGuiStyleVar_IndentSpacing,       // float     IndentSpacing\n    ImGuiStyleVar_ScrollbarSize,       // float     ScrollbarSize\n    ImGuiStyleVar_ScrollbarRounding,   // float     ScrollbarRounding\n    ImGuiStyleVar_GrabMinSize,         // float     GrabMinSize\n    ImGuiStyleVar_GrabRounding,        // float     GrabRounding\n    ImGuiStyleVar_TabRounding,         // float     TabRounding\n    ImGuiStyleVar_ButtonTextAlign,     // ImVec2    ButtonTextAlign\n    ImGuiStyleVar_SelectableTextAlign, // ImVec2    SelectableTextAlign\n    ImGuiStyleVar_COUNT\n\n    // Obsolete names (will be removed)\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    , ImGuiStyleVar_Count_ = ImGuiStyleVar_COUNT, ImGuiStyleVar_ChildWindowRounding = ImGuiStyleVar_ChildRounding\n#endif\n};\n\n// Flags for ColorEdit3() / ColorEdit4() / ColorPicker3() / ColorPicker4() / ColorButton()\nenum ImGuiColorEditFlags_\n{\n    ImGuiColorEditFlags_None            = 0,\n    ImGuiColorEditFlags_NoAlpha         = 1 << 1,   //              // ColorEdit, ColorPicker, ColorButton: ignore Alpha component (will only read 3 components from the input pointer).\n    ImGuiColorEditFlags_NoPicker        = 1 << 2,   //              // ColorEdit: disable picker when clicking on colored square.\n    ImGuiColorEditFlags_NoOptions       = 1 << 3,   //              // ColorEdit: disable toggling options menu when right-clicking on inputs/small preview.\n    ImGuiColorEditFlags_NoSmallPreview  = 1 << 4,   //              // ColorEdit, ColorPicker: disable colored square preview next to the inputs. (e.g. to show only the inputs)\n    ImGuiColorEditFlags_NoInputs        = 1 << 5,   //              // ColorEdit, ColorPicker: disable inputs sliders/text widgets (e.g. to show only the small preview colored square).\n    ImGuiColorEditFlags_NoTooltip       = 1 << 6,   //              // ColorEdit, ColorPicker, ColorButton: disable tooltip when hovering the preview.\n    ImGuiColorEditFlags_NoLabel         = 1 << 7,   //              // ColorEdit, ColorPicker: disable display of inline text label (the label is still forwarded to the tooltip and picker).\n    ImGuiColorEditFlags_NoSidePreview   = 1 << 8,   //              // ColorPicker: disable bigger color preview on right side of the picker, use small colored square preview instead.\n    ImGuiColorEditFlags_NoDragDrop      = 1 << 9,   //              // ColorEdit: disable drag and drop target. ColorButton: disable drag and drop source.\n\n    // User Options (right-click on widget to change some of them).\n    ImGuiColorEditFlags_AlphaBar        = 1 << 16,  //              // ColorEdit, ColorPicker: show vertical alpha bar/gradient in picker.\n    ImGuiColorEditFlags_AlphaPreview    = 1 << 17,  //              // ColorEdit, ColorPicker, ColorButton: display preview as a transparent color over a checkerboard, instead of opaque.\n    ImGuiColorEditFlags_AlphaPreviewHalf= 1 << 18,  //              // ColorEdit, ColorPicker, ColorButton: display half opaque / half checkerboard, instead of opaque.\n    ImGuiColorEditFlags_HDR             = 1 << 19,  //              // (WIP) ColorEdit: Currently only disable 0.0f..1.0f limits in RGBA edition (note: you probably want to use ImGuiColorEditFlags_Float flag as well).\n    ImGuiColorEditFlags_DisplayRGB      = 1 << 20,  // [Display]    // ColorEdit: override _display_ type among RGB/HSV/Hex. ColorPicker: select any combination using one or more of RGB/HSV/Hex.\n    ImGuiColorEditFlags_DisplayHSV      = 1 << 21,  // [Display]    // \"\n    ImGuiColorEditFlags_DisplayHex      = 1 << 22,  // [Display]    // \"\n    ImGuiColorEditFlags_Uint8           = 1 << 23,  // [DataType]   // ColorEdit, ColorPicker, ColorButton: _display_ values formatted as 0..255.\n    ImGuiColorEditFlags_Float           = 1 << 24,  // [DataType]   // ColorEdit, ColorPicker, ColorButton: _display_ values formatted as 0.0f..1.0f floats instead of 0..255 integers. No round-trip of value via integers.\n    ImGuiColorEditFlags_PickerHueBar    = 1 << 25,  // [Picker]     // ColorPicker: bar for Hue, rectangle for Sat/Value.\n    ImGuiColorEditFlags_PickerHueWheel  = 1 << 26,  // [Picker]     // ColorPicker: wheel for Hue, triangle for Sat/Value.\n    ImGuiColorEditFlags_InputRGB        = 1 << 27,  // [Input]      // ColorEdit, ColorPicker: input and output data in RGB format.\n    ImGuiColorEditFlags_InputHSV        = 1 << 28,  // [Input]      // ColorEdit, ColorPicker: input and output data in HSV format.\n\n    // Defaults Options. You can set application defaults using SetColorEditOptions(). The intent is that you probably don't want to\n    // override them in most of your calls. Let the user choose via the option menu and/or call SetColorEditOptions() once during startup.\n    ImGuiColorEditFlags__OptionsDefault = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_DisplayRGB|ImGuiColorEditFlags_InputRGB|ImGuiColorEditFlags_PickerHueBar,\n\n    // [Internal] Masks\n    ImGuiColorEditFlags__DisplayMask    = ImGuiColorEditFlags_DisplayRGB|ImGuiColorEditFlags_DisplayHSV|ImGuiColorEditFlags_DisplayHex,\n    ImGuiColorEditFlags__DataTypeMask   = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_Float,\n    ImGuiColorEditFlags__PickerMask     = ImGuiColorEditFlags_PickerHueWheel|ImGuiColorEditFlags_PickerHueBar,\n    ImGuiColorEditFlags__InputMask      = ImGuiColorEditFlags_InputRGB|ImGuiColorEditFlags_InputHSV\n\n    // Obsolete names (will be removed)\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    , ImGuiColorEditFlags_RGB = ImGuiColorEditFlags_DisplayRGB, ImGuiColorEditFlags_HSV = ImGuiColorEditFlags_DisplayHSV, ImGuiColorEditFlags_HEX = ImGuiColorEditFlags_DisplayHex\n#endif\n};\n\n// Enumeration for GetMouseCursor()\n// User code may request binding to display given cursor by calling SetMouseCursor(), which is why we have some cursors that are marked unused here\nenum ImGuiMouseCursor_\n{\n    ImGuiMouseCursor_None = -1,\n    ImGuiMouseCursor_Arrow = 0,\n    ImGuiMouseCursor_TextInput,         // When hovering over InputText, etc.\n    ImGuiMouseCursor_ResizeAll,         // (Unused by imgui functions)\n    ImGuiMouseCursor_ResizeNS,          // When hovering over an horizontal border\n    ImGuiMouseCursor_ResizeEW,          // When hovering over a vertical border or a column\n    ImGuiMouseCursor_ResizeNESW,        // When hovering over the bottom-left corner of a window\n    ImGuiMouseCursor_ResizeNWSE,        // When hovering over the bottom-right corner of a window\n    ImGuiMouseCursor_Hand,              // (Unused by imgui functions. Use for e.g. hyperlinks)\n    ImGuiMouseCursor_COUNT\n\n    // Obsolete names (will be removed)\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    , ImGuiMouseCursor_Count_ = ImGuiMouseCursor_COUNT\n#endif\n};\n\n// Enumateration for ImGui::SetWindow***(), SetNextWindow***(), SetNextTreeNode***() functions\n// Represent a condition.\n// Important: Treat as a regular enum! Do NOT combine multiple values using binary operators! All the functions above treat 0 as a shortcut to ImGuiCond_Always.\nenum ImGuiCond_\n{\n    ImGuiCond_Always        = 1 << 0,   // Set the variable\n    ImGuiCond_Once          = 1 << 1,   // Set the variable once per runtime session (only the first call with succeed)\n    ImGuiCond_FirstUseEver  = 1 << 2,   // Set the variable if the object/window has no persistently saved data (no entry in .ini file)\n    ImGuiCond_Appearing     = 1 << 3    // Set the variable if the object/window is appearing after being hidden/inactive (or the first time)\n\n    // Obsolete names (will be removed)\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    , ImGuiSetCond_Always = ImGuiCond_Always, ImGuiSetCond_Once = ImGuiCond_Once, ImGuiSetCond_FirstUseEver = ImGuiCond_FirstUseEver, ImGuiSetCond_Appearing = ImGuiCond_Appearing\n#endif\n};\n\n//-----------------------------------------------------------------------------\n// Helpers: Memory allocations macros\n// IM_MALLOC(), IM_FREE(), IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE()\n// We call C++ constructor on own allocated memory via the placement \"new(ptr) Type()\" syntax.\n// Defining a custom placement new() with a dummy parameter allows us to bypass including <new> which on some platforms complains when user has disabled exceptions.\n//-----------------------------------------------------------------------------\n\nstruct ImNewDummy {};\ninline void* operator new(size_t, ImNewDummy, void* ptr) { return ptr; }\ninline void  operator delete(void*, ImNewDummy, void*)   {} // This is only required so we can use the symmetrical new()\n#define IM_ALLOC(_SIZE)                     ImGui::MemAlloc(_SIZE)\n#define IM_FREE(_PTR)                       ImGui::MemFree(_PTR)\n#define IM_PLACEMENT_NEW(_PTR)              new(ImNewDummy(), _PTR)\n#define IM_NEW(_TYPE)                       new(ImNewDummy(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE\ntemplate<typename T> void IM_DELETE(T* p)   { if (p) { p->~T(); ImGui::MemFree(p); } }\n\n//-----------------------------------------------------------------------------\n// Helper: ImVector<>\n// Lightweight std::vector<>-like class to avoid dragging dependencies (also, some implementations of STL with debug enabled are absurdly slow, we bypass it so our code runs fast in debug).\n// You generally do NOT need to care or use this ever. But we need to make it available in imgui.h because some of our data structures are relying on it.\n// Important: clear() frees memory, resize(0) keep the allocated buffer. We use resize(0) a lot to intentionally recycle allocated buffers across frames and amortize our costs.\n// Important: our implementation does NOT call C++ constructors/destructors, we treat everything as raw data! This is intentional but be extra mindful of that,\n// do NOT use this class as a std::vector replacement in your own code! Many of the structures used by dear imgui can be safely initialized by a zero-memset.\n//-----------------------------------------------------------------------------\n\ntemplate<typename T>\nstruct ImVector\n{\n    int                 Size;\n    int                 Capacity;\n    T*                  Data;\n\n    // Provide standard typedefs but we don't use them ourselves.\n    typedef T                   value_type;\n    typedef value_type*         iterator;\n    typedef const value_type*   const_iterator;\n\n    // Constructors, destructor\n    inline ImVector()                                       { Size = Capacity = 0; Data = NULL; }\n    inline ImVector(const ImVector<T>& src)                 { Size = Capacity = 0; Data = NULL; operator=(src); }\n    inline ImVector<T>& operator=(const ImVector<T>& src)   { clear(); resize(src.Size); memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; }\n    inline ~ImVector()                                      { if (Data) IM_FREE(Data); }\n\n    inline bool         empty() const                       { return Size == 0; }\n    inline int          size() const                        { return Size; }\n    inline int          size_in_bytes() const               { return Size * (int)sizeof(T); }\n    inline int          capacity() const                    { return Capacity; }\n    inline T&           operator[](int i)                   { IM_ASSERT(i < Size); return Data[i]; }\n    inline const T&     operator[](int i) const             { IM_ASSERT(i < Size); return Data[i]; }\n\n    inline void         clear()                             { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } }\n    inline T*           begin()                             { return Data; }\n    inline const T*     begin() const                       { return Data; }\n    inline T*           end()                               { return Data + Size; }\n    inline const T*     end() const                         { return Data + Size; }\n    inline T&           front()                             { IM_ASSERT(Size > 0); return Data[0]; }\n    inline const T&     front() const                       { IM_ASSERT(Size > 0); return Data[0]; }\n    inline T&           back()                              { IM_ASSERT(Size > 0); return Data[Size - 1]; }\n    inline const T&     back() const                        { IM_ASSERT(Size > 0); return Data[Size - 1]; }\n    inline void         swap(ImVector<T>& rhs)              { int rhs_size = rhs.Size; rhs.Size = Size; Size = rhs_size; int rhs_cap = rhs.Capacity; rhs.Capacity = Capacity; Capacity = rhs_cap; T* rhs_data = rhs.Data; rhs.Data = Data; Data = rhs_data; }\n\n    inline int          _grow_capacity(int sz) const        { int new_capacity = Capacity ? (Capacity + Capacity/2) : 8; return new_capacity > sz ? new_capacity : sz; }\n    inline void         resize(int new_size)                { if (new_size > Capacity) reserve(_grow_capacity(new_size)); Size = new_size; }\n    inline void         resize(int new_size, const T& v)    { if (new_size > Capacity) reserve(_grow_capacity(new_size)); if (new_size > Size) for (int n = Size; n < new_size; n++) memcpy(&Data[n], &v, sizeof(v)); Size = new_size; }\n    inline void         reserve(int new_capacity)           { if (new_capacity <= Capacity) return; T* new_data = (T*)IM_ALLOC((size_t)new_capacity * sizeof(T)); if (Data) { memcpy(new_data, Data, (size_t)Size * sizeof(T)); IM_FREE(Data); } Data = new_data; Capacity = new_capacity; }\n\n    // NB: It is illegal to call push_back/push_front/insert with a reference pointing inside the ImVector data itself! e.g. v.push_back(v[10]) is forbidden.\n    inline void         push_back(const T& v)               { if (Size == Capacity) reserve(_grow_capacity(Size + 1)); memcpy(&Data[Size], &v, sizeof(v)); Size++; }\n    inline void         pop_back()                          { IM_ASSERT(Size > 0); Size--; }\n    inline void         push_front(const T& v)              { if (Size == 0) push_back(v); else insert(Data, v); }\n    inline T*           erase(const T* it)                  { IM_ASSERT(it >= Data && it < Data+Size); const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + 1, ((size_t)Size - (size_t)off - 1) * sizeof(T)); Size--; return Data + off; }\n    inline T*           erase(const T* it, const T* it_last){ IM_ASSERT(it >= Data && it < Data+Size && it_last > it && it_last <= Data+Size); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + count, ((size_t)Size - (size_t)off - count) * sizeof(T)); Size -= (int)count; return Data + off; }\n    inline T*           erase_unsorted(const T* it)         { IM_ASSERT(it >= Data && it < Data+Size);  const ptrdiff_t off = it - Data; if (it < Data+Size-1) memcpy(Data + off, Data + Size - 1, sizeof(T)); Size--; return Data + off; }\n    inline T*           insert(const T* it, const T& v)     { IM_ASSERT(it >= Data && it <= Data+Size); const ptrdiff_t off = it - Data; if (Size == Capacity) reserve(_grow_capacity(Size + 1)); if (off < (int)Size) memmove(Data + off + 1, Data + off, ((size_t)Size - (size_t)off) * sizeof(T)); memcpy(&Data[off], &v, sizeof(v)); Size++; return Data + off; }\n    inline bool         contains(const T& v) const          { const T* data = Data;  const T* data_end = Data + Size; while (data < data_end) if (*data++ == v) return true; return false; }\n    inline int          index_from_ptr(const T* it) const   { IM_ASSERT(it >= Data && it <= Data+Size); const ptrdiff_t off = it - Data; return (int)off; }\n};\n\n//-----------------------------------------------------------------------------\n// ImGuiStyle\n// You may modify the ImGui::GetStyle() main instance during initialization and before NewFrame().\n// During the frame, use ImGui::PushStyleVar(ImGuiStyleVar_XXXX)/PopStyleVar() to alter the main style values,\n// and ImGui::PushStyleColor(ImGuiCol_XXX)/PopStyleColor() for colors.\n//-----------------------------------------------------------------------------\n\nstruct ImGuiStyle\n{\n    float       Alpha;                      // Global alpha applies to everything in ImGui.\n    ImVec2      WindowPadding;              // Padding within a window.\n    float       WindowRounding;             // Radius of window corners rounding. Set to 0.0f to have rectangular windows.\n    float       WindowBorderSize;           // Thickness of border around windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly).\n    ImVec2      WindowMinSize;              // Minimum window size. This is a global setting. If you want to constraint individual windows, use SetNextWindowSizeConstraints().\n    ImVec2      WindowTitleAlign;           // Alignment for title bar text. Defaults to (0.0f,0.5f) for left-aligned,vertically centered.\n    float       ChildRounding;              // Radius of child window corners rounding. Set to 0.0f to have rectangular windows.\n    float       ChildBorderSize;            // Thickness of border around child windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly).\n    float       PopupRounding;              // Radius of popup window corners rounding. (Note that tooltip windows use WindowRounding)\n    float       PopupBorderSize;            // Thickness of border around popup/tooltip windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly).\n    ImVec2      FramePadding;               // Padding within a framed rectangle (used by most widgets).\n    float       FrameRounding;              // Radius of frame corners rounding. Set to 0.0f to have rectangular frame (used by most widgets).\n    float       FrameBorderSize;            // Thickness of border around frames. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly).\n    ImVec2      ItemSpacing;                // Horizontal and vertical spacing between widgets/lines.\n    ImVec2      ItemInnerSpacing;           // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label).\n    ImVec2      TouchExtraPadding;          // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much!\n    float       IndentSpacing;              // Horizontal indentation when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2).\n    float       ColumnsMinSpacing;          // Minimum horizontal spacing between two columns.\n    float       ScrollbarSize;              // Width of the vertical scrollbar, Height of the horizontal scrollbar.\n    float       ScrollbarRounding;          // Radius of grab corners for scrollbar.\n    float       GrabMinSize;                // Minimum width/height of a grab box for slider/scrollbar.\n    float       GrabRounding;               // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.\n    float       TabRounding;                // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.\n    float       TabBorderSize;              // Thickness of border around tabs.\n    ImVec2      ButtonTextAlign;            // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered).\n    ImVec2      SelectableTextAlign;        // Alignment of selectable text when selectable is larger than text. Defaults to (0.0f, 0.0f) (top-left aligned).\n    ImVec2      DisplayWindowPadding;       // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows.\n    ImVec2      DisplaySafeAreaPadding;     // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly!\n    float       MouseCursorScale;           // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later.\n    bool        AntiAliasedLines;           // Enable anti-aliasing on lines/borders. Disable if you are really tight on CPU/GPU.\n    bool        AntiAliasedFill;            // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.)\n    float       CurveTessellationTol;       // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.\n    ImVec4      Colors[ImGuiCol_COUNT];\n\n    IMGUI_API ImGuiStyle();\n    IMGUI_API void ScaleAllSizes(float scale_factor);\n};\n\n//-----------------------------------------------------------------------------\n// ImGuiIO\n// Communicate most settings and inputs/outputs to Dear ImGui using this structure.\n// Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage.\n//-----------------------------------------------------------------------------\n\nstruct ImGuiIO\n{\n    //------------------------------------------------------------------\n    // Configuration (fill once)                // Default value\n    //------------------------------------------------------------------\n\n    ImGuiConfigFlags   ConfigFlags;             // = 0              // See ImGuiConfigFlags_ enum. Set by user/application. Gamepad/keyboard navigation options, etc.\n    ImGuiBackendFlags  BackendFlags;            // = 0              // See ImGuiBackendFlags_ enum. Set by back-end (imgui_impl_xxx files or custom back-end) to communicate features supported by the back-end.\n    ImVec2      DisplaySize;                    // <unset>          // Main display size, in pixels. This is for the default viewport. Use BeginViewport() for other viewports.\n    float       DeltaTime;                      // = 1.0f/60.0f     // Time elapsed since last frame, in seconds.\n    float       IniSavingRate;                  // = 5.0f           // Minimum time between saving positions/sizes to .ini file, in seconds.\n    const char* IniFilename;                    // = \"imgui.ini\"    // Path to .ini file. Set NULL to disable automatic .ini loading/saving, if e.g. you want to manually load/save from memory.\n    const char* LogFilename;                    // = \"imgui_log.txt\"// Path to .log file (default parameter to ImGui::LogToFile when no file is specified).\n    float       MouseDoubleClickTime;           // = 0.30f          // Time for a double-click, in seconds.\n    float       MouseDoubleClickMaxDist;        // = 6.0f           // Distance threshold to stay in to validate a double-click, in pixels.\n    float       MouseDragThreshold;             // = 6.0f           // Distance threshold before considering we are dragging.\n    int         KeyMap[ImGuiKey_COUNT];         // <unset>          // Map of indices into the KeysDown[512] entries array which represent your \"native\" keyboard state.\n    float       KeyRepeatDelay;                 // = 0.250f         // When holding a key/button, time before it starts repeating, in seconds (for buttons in Repeat mode, etc.).\n    float       KeyRepeatRate;                  // = 0.050f         // When holding a key/button, rate at which it repeats, in seconds.\n    void*       UserData;                       // = NULL           // Store your own data for retrieval by callbacks.\n\n    ImFontAtlas*Fonts;                          // <auto>           // Load, rasterize and pack one or more fonts into a single texture.\n    float       FontGlobalScale;                // = 1.0f           // Global scale all fonts\n    bool        FontAllowUserScaling;           // = false          // Allow user scaling text of individual window with CTRL+Wheel.\n    ImFont*     FontDefault;                    // = NULL           // Font to use on NewFrame(). Use NULL to uses Fonts->Fonts[0].\n    ImVec2      DisplayFramebufferScale;        // = (1, 1)         // For retina display or other situations where window coordinates are different from framebuffer coordinates. This generally ends up in ImDrawData::FramebufferScale.\n\n    // Docking options (when ImGuiConfigFlags_DockingEnable is set)\n    bool        ConfigDockingNoSplit;           // = false          // Simplified docking mode: disable window splitting, so docking is limited to merging multiple windows together into tab-bars.\n    bool        ConfigDockingWithShift;         // = false          // Enable docking with holding Shift key (reduce visual noise, allows dropping in wider space)\n    bool        ConfigDockingTabBarOnSingleWindows; // = false      // [BETA] Make every single floating window display within a docking node.\n    bool        ConfigDockingTransparentPayload;// = false          // [BETA] Make window or viewport transparent when docking and only display docking boxes on the target viewport. Useful if rendering of multiple viewport cannot be synced. Best used with ConfigViewportsNoAutoMerge.\n    \n    // Viewport options (when ImGuiConfigFlags_ViewportsEnable is set)\n    bool        ConfigViewportsNoAutoMerge;     // = false;         // Set to make all floating imgui windows always create their own viewport. Otherwise, they are merged into the main host viewports when overlapping it.\n    bool        ConfigViewportsNoTaskBarIcon;   // = false          // Disable default OS task bar icon flag for secondary viewports. When a viewport doesn't want a task bar icon, ImGuiViewportFlags_NoTaskBarIcon will be set on it.\n    bool        ConfigViewportsNoDecoration;    // = true           // [BETA] Disable default OS window decoration flag for secondary viewports. When a viewport doesn't want window decorations, ImGuiViewportFlags_NoDecoration will be set on it. Enabling decoration can create subsequent issues at OS levels (e.g. minimum window size).\n    bool        ConfigViewportsNoDefaultParent; // = false          // Disable default OS parenting to main viewport for secondary viewports. By default, viewports are marked with ParentViewportId = <main_viewport>, expecting the platform back-end to setup a parent/child relationship between the OS windows (some back-end may ignore this). Set to true if you want the default to be 0, then all viewports will be top-level OS windows.\n\n    // Miscellaneous options\n    bool        MouseDrawCursor;                // = false          // Request ImGui to draw a mouse cursor for you (if you are on a platform without a mouse cursor). Cannot be easily renamed to 'io.ConfigXXX' because this is frequently used by back-end implementations.\n    bool        ConfigMacOSXBehaviors;          // = defined(__APPLE__) // OS X style: Text editing cursor movement using Alt instead of Ctrl, Shortcuts using Cmd/Super instead of Ctrl, Line/Text Start and End using Cmd+Arrows instead of Home/End, Double click selects by word instead of selecting whole text, Multi-selection in lists uses Cmd/Super instead of Ctrl (was called io.OptMacOSXBehaviors prior to 1.63)\n    bool        ConfigInputTextCursorBlink;     // = true           // Set to false to disable blinking cursor, for users who consider it distracting. (was called: io.OptCursorBlink prior to 1.63)\n    bool        ConfigWindowsResizeFromEdges;   // = true           // Enable resizing of windows from their edges and from the lower-left corner. This requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback. (This used to be a per-window ImGuiWindowFlags_ResizeFromAnySide flag)\n    bool        ConfigWindowsMoveFromTitleBarOnly; // = false       // [BETA] Set to true to only allow moving windows when clicked+dragged from the title bar. Windows without a title bar are not affected.\n\n    //------------------------------------------------------------------\n    // Platform Functions\n    // (the imgui_impl_xxxx back-end files are setting those up for you)\n    //------------------------------------------------------------------\n\n    // Optional: Platform/Renderer back-end name (informational only! will be displayed in About Window) + User data for back-end/wrappers to store their own stuff.\n    const char* BackendPlatformName;            // = NULL\n    const char* BackendRendererName;            // = NULL\n    void*       BackendPlatformUserData;        // = NULL\n    void*       BackendRendererUserData;        // = NULL\n    void*       BackendLanguageUserData;        // = NULL\n\n    // Optional: Access OS clipboard\n    // (default to use native Win32 clipboard on Windows, otherwise uses a private clipboard. Override to access OS clipboard on other architectures)\n    const char* (*GetClipboardTextFn)(void* user_data);\n    void        (*SetClipboardTextFn)(void* user_data, const char* text);\n    void*       ClipboardUserData;\n\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    // [OBSOLETE since 1.60+] Rendering function, will be automatically called in Render(). Please call your rendering function yourself now!\n    // You can obtain the ImDrawData* by calling ImGui::GetDrawData() after Render(). See example applications if you are unsure of how to implement this.\n    void        (*RenderDrawListsFn)(ImDrawData* data);\n#else\n    // This is only here to keep ImGuiIO the same size/layout, so that IMGUI_DISABLE_OBSOLETE_FUNCTIONS can exceptionally be used outside of imconfig.h.\n    void*       RenderDrawListsFnUnused;\n#endif\n\n    //------------------------------------------------------------------\n    // Input - Fill before calling NewFrame()\n    //------------------------------------------------------------------\n\n    ImVec2      MousePos;                       // Mouse position, in pixels. Set to ImVec2(-FLT_MAX,-FLT_MAX) if mouse is unavailable (on another screen, etc.)\n    bool        MouseDown[5];                   // Mouse buttons: 0=left, 1=right, 2=middle + extras. ImGui itself mostly only uses left button (BeginPopupContext** are using right button). Others buttons allows us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API.\n    float       MouseWheel;                     // Mouse wheel Vertical: 1 unit scrolls about 5 lines text.\n    float       MouseWheelH;                    // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all back-ends.\n    ImGuiID     MouseHoveredViewport;           // (Optional) When using multiple viewports: viewport the OS mouse cursor is hovering _IGNORING_ viewports with the ImGuiViewportFlags_NoInputs flag, and _REGARDLESS_ of whether another viewport is focused. Set io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport if you can provide this info. If you don't imgui will infer the value using the rectangles and last focused time of the viewports it knows about (ignoring other OS windows).\n    bool        KeyCtrl;                        // Keyboard modifier pressed: Control\n    bool        KeyShift;                       // Keyboard modifier pressed: Shift\n    bool        KeyAlt;                         // Keyboard modifier pressed: Alt\n    bool        KeySuper;                       // Keyboard modifier pressed: Cmd/Super/Windows\n    bool        KeysDown[512];                  // Keyboard keys that are pressed (ideally left in the \"native\" order your engine has access to keyboard keys, so you can use your own defines/enums for keys).\n    float       NavInputs[ImGuiNavInput_COUNT]; // Gamepad inputs. Cleared back to zero by EndFrame(). Keyboard keys will be auto-mapped and be written here by NewFrame().\n\n    // Functions\n    IMGUI_API void  AddInputCharacter(ImWchar c);               // Queue new character input\n    IMGUI_API void  AddInputCharactersUTF8(const char* str);    // Queue new characters input from an UTF-8 string\n    IMGUI_API void  ClearInputCharacters();                     // Clear the text input buffer manually\n\n    //------------------------------------------------------------------\n    // Output - Retrieve after calling NewFrame()\n    //------------------------------------------------------------------\n\n    bool        WantCaptureMouse;               // When io.WantCaptureMouse is true, imgui will use the mouse inputs, do not dispatch them to your main game/application (in both cases, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.).\n    bool        WantCaptureKeyboard;            // When io.WantCaptureKeyboard is true, imgui will use the keyboard inputs, do not dispatch them to your main game/application (in both cases, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.).\n    bool        WantTextInput;                  // Mobile/console: when io.WantTextInput is true, you may display an on-screen keyboard. This is set by ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active).\n    bool        WantSetMousePos;                // MousePos has been altered, back-end should reposition mouse on next frame. Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled.\n    bool        WantSaveIniSettings;            // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. IMPORTANT: You need to clear io.WantSaveIniSettings yourself.\n    bool        NavActive;                      // Directional navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag.\n    bool        NavVisible;                     // Directional navigation is visible and allowed (will handle ImGuiKey_NavXXX events).\n    float       Framerate;                      // Application framerate estimation, in frame per second. Solely for convenience. Rolling average estimation based on IO.DeltaTime over 120 frames\n    int         MetricsRenderVertices;          // Vertices output during last call to Render()\n    int         MetricsRenderIndices;           // Indices output during last call to Render() = number of triangles * 3\n    int         MetricsRenderWindows;           // Number of visible windows\n    int         MetricsActiveWindows;           // Number of active windows\n    int         MetricsActiveAllocations;       // Number of active allocations, updated by MemAlloc/MemFree based on current context. May be off if you have multiple imgui contexts.\n    ImVec2      MouseDelta;                     // Mouse delta. Note that this is zero if either current or previous position are invalid (-FLT_MAX,-FLT_MAX), so a disappearing/reappearing mouse won't have a huge delta.\n\n    //------------------------------------------------------------------\n    // [Internal] ImGui will maintain those fields. Forward compatibility not guaranteed!\n    //------------------------------------------------------------------\n\n    ImVec2      MousePosPrev;                   // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid)\n    ImVec2      MouseClickedPos[5];             // Position at time of clicking\n    double      MouseClickedTime[5];            // Time of last click (used to figure out double-click)\n    bool        MouseClicked[5];                // Mouse button went from !Down to Down\n    bool        MouseDoubleClicked[5];          // Has mouse button been double-clicked?\n    bool        MouseReleased[5];               // Mouse button went from Down to !Down\n    bool        MouseDownOwned[5];              // Track if button was clicked inside a window. We don't request mouse capture from the application if click started outside ImGui bounds.\n    float       MouseDownDuration[5];           // Duration the mouse button has been down (0.0f == just clicked)\n    float       MouseDownDurationPrev[5];       // Previous time the mouse button has been down\n    ImVec2      MouseDragMaxDistanceAbs[5];     // Maximum distance, absolute, on each axis, of how much mouse has traveled from the clicking point\n    float       MouseDragMaxDistanceSqr[5];     // Squared maximum distance of how much mouse has traveled from the clicking point\n    float       KeysDownDuration[512];          // Duration the keyboard key has been down (0.0f == just pressed)\n    float       KeysDownDurationPrev[512];      // Previous duration the key has been down\n    float       NavInputsDownDuration[ImGuiNavInput_COUNT];\n    float       NavInputsDownDurationPrev[ImGuiNavInput_COUNT];\n    ImVector<ImWchar> InputQueueCharacters;     // Queue of _characters_ input (obtained by platform back-end). Fill using AddInputCharacter() helper.\n\n    IMGUI_API   ImGuiIO();\n};\n\n//-----------------------------------------------------------------------------\n// Misc data structures\n//-----------------------------------------------------------------------------\n\n// Shared state of InputText(), passed as an argument to your callback when a ImGuiInputTextFlags_Callback* flag is used.\n// The callback function should return 0 by default.\n// Callbacks (follow a flag name and see comments in ImGuiInputTextFlags_ declarations for more details)\n// - ImGuiInputTextFlags_CallbackCompletion:  Callback on pressing TAB\n// - ImGuiInputTextFlags_CallbackHistory:     Callback on pressing Up/Down arrows\n// - ImGuiInputTextFlags_CallbackAlways:      Callback on each iteration\n// - ImGuiInputTextFlags_CallbackCharFilter:  Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard.\n// - ImGuiInputTextFlags_CallbackResize:      Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow.\nstruct ImGuiInputTextCallbackData\n{\n    ImGuiInputTextFlags EventFlag;      // One ImGuiInputTextFlags_Callback*    // Read-only\n    ImGuiInputTextFlags Flags;          // What user passed to InputText()      // Read-only\n    void*               UserData;       // What user passed to InputText()      // Read-only\n\n    // Arguments for the different callback events\n    // - To modify the text buffer in a callback, prefer using the InsertChars() / DeleteChars() function. InsertChars() will take care of calling the resize callback if necessary.\n    // - If you know your edits are not going to resize the underlying buffer allocation, you may modify the contents of 'Buf[]' directly. You need to update 'BufTextLen' accordingly (0 <= BufTextLen < BufSize) and set 'BufDirty'' to true so InputText can update its internal state.\n    ImWchar             EventChar;      // Character input                      // Read-write   // [CharFilter] Replace character with another one, or set to zero to drop. return 1 is equivalent to setting EventChar=0;\n    ImGuiKey            EventKey;       // Key pressed (Up/Down/TAB)            // Read-only    // [Completion,History]\n    char*               Buf;            // Text buffer                          // Read-write   // [Resize] Can replace pointer / [Completion,History,Always] Only write to pointed data, don't replace the actual pointer!\n    int                 BufTextLen;     // Text length (in bytes)               // Read-write   // [Resize,Completion,History,Always] Exclude zero-terminator storage. In C land: == strlen(some_text), in C++ land: string.length()\n    int                 BufSize;        // Buffer size (in bytes) = capacity+1  // Read-only    // [Resize,Completion,History,Always] Include zero-terminator storage. In C land == ARRAYSIZE(my_char_array), in C++ land: string.capacity()+1\n    bool                BufDirty;       // Set if you modify Buf/BufTextLen!    // Write        // [Completion,History,Always]\n    int                 CursorPos;      //                                      // Read-write   // [Completion,History,Always]\n    int                 SelectionStart; //                                      // Read-write   // [Completion,History,Always] == to SelectionEnd when no selection)\n    int                 SelectionEnd;   //                                      // Read-write   // [Completion,History,Always]\n\n    // Helper functions for text manipulation.\n    // Use those function to benefit from the CallbackResize behaviors. Calling those function reset the selection.\n    IMGUI_API ImGuiInputTextCallbackData();\n    IMGUI_API void      DeleteChars(int pos, int bytes_count);\n    IMGUI_API void      InsertChars(int pos, const char* text, const char* text_end = NULL);\n    bool                HasSelection() const { return SelectionStart != SelectionEnd; }\n};\n\n// Resizing callback data to apply custom constraint. As enabled by SetNextWindowSizeConstraints(). Callback is called during the next Begin().\n// NB: For basic min/max size constraint on each axis you don't need to use the callback! The SetNextWindowSizeConstraints() parameters are enough.\nstruct ImGuiSizeCallbackData\n{\n    void*   UserData;       // Read-only.   What user passed to SetNextWindowSizeConstraints()\n    ImVec2  Pos;            // Read-only.   Window position, for reference.\n    ImVec2  CurrentSize;    // Read-only.   Current window size.\n    ImVec2  DesiredSize;    // Read-write.  Desired size, based on user's mouse position. Write to this field to restrain resizing.\n};\n\n// Data payload for Drag and Drop operations: AcceptDragDropPayload(), GetDragDropPayload()\nstruct ImGuiPayload\n{\n    // Members\n    void*           Data;               // Data (copied and owned by dear imgui)\n    int             DataSize;           // Data size\n\n    // [Internal]\n    ImGuiID         SourceId;           // Source item id\n    ImGuiID         SourceParentId;     // Source parent id (if available)\n    int             DataFrameCount;     // Data timestamp\n    char            DataType[32+1];     // Data type tag (short user-supplied string, 32 characters max)\n    bool            Preview;            // Set when AcceptDragDropPayload() was called and mouse has been hovering the target item (nb: handle overlapping drag targets)\n    bool            Delivery;           // Set when AcceptDragDropPayload() was called and mouse button is released over the target item.\n\n    ImGuiPayload()  { Clear(); }\n    void Clear()    { SourceId = SourceParentId = 0; Data = NULL; DataSize = 0; memset(DataType, 0, sizeof(DataType)); DataFrameCount = -1; Preview = Delivery = false; }\n    bool IsDataType(const char* type) const { return DataFrameCount != -1 && strcmp(type, DataType) == 0; }\n    bool IsPreview() const                  { return Preview; }\n    bool IsDelivery() const                 { return Delivery; }\n};\n\n// [BETA] Rarely used / very advanced uses only. Use with SetNextWindowClass() and DockSpace() functions.\n// Provide hints to the platform back-end via altered viewport flags (enable/disable OS decoration, OS task bar icons, etc.) and OS level parent/child relationships.\nstruct ImGuiWindowClass\n{\n    ImGuiID             ClassId;                    // User data. 0 = Default class (unclassed)\n    ImGuiID             ParentViewportId;           // Hint for the platform back-end. If non-zero, the platform back-end can create a parent<>child relationship between the platform windows. Not conforming back-ends are free to e.g. parent every viewport to the main viewport or not.\n    ImGuiViewportFlags  ViewportFlagsOverrideMask;  // Viewport flags to override when a window of this class owns a viewport. This allows you to enforce OS decoration or task bar icon, override the defaults on a per-window basis.\n    ImGuiViewportFlags  ViewportFlagsOverrideValue; // Viewport flags values to override when a window of this class owns a viewport.\n    bool                DockingAllowUnclassed;      // true = can be docked/merged with an unclassed window\n\n    ImGuiWindowClass()  { ClassId = 0; ParentViewportId = 0; ViewportFlagsOverrideMask = ViewportFlagsOverrideValue = 0x00; DockingAllowUnclassed = true; }\n};\n\n//-----------------------------------------------------------------------------\n// Obsolete functions (Will be removed! Read 'API BREAKING CHANGES' section in imgui.cpp for details)\n// Please keep your copy of dear imgui up to date! Occasionally set '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in imconfig.h to stay ahead.\n//-----------------------------------------------------------------------------\n\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\nnamespace ImGui\n{\n    // OBSOLETED in 1.69 (from Mar 2019)\n    static inline ImDrawList* GetOverlayDrawList()            { return GetForegroundDrawList(); }\n    // OBSOLETED in 1.66 (from Sep 2018)\n    static inline void  SetScrollHere(float center_ratio=0.5f){ SetScrollHereY(center_ratio); }\n    // OBSOLETED in 1.63 (between Aug 2018 and Sept 2018)\n    static inline bool  IsItemDeactivatedAfterChange()        { return IsItemDeactivatedAfterEdit(); }\n    // OBSOLETED in 1.61 (between Apr 2018 and Aug 2018)\n    IMGUI_API bool      InputFloat(const char* label, float* v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags flags = 0); // Use the 'const char* format' version instead of 'decimal_precision'!\n    IMGUI_API bool      InputFloat2(const char* label, float v[2], int decimal_precision, ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool      InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags flags = 0);\n    IMGUI_API bool      InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags flags = 0);\n    // OBSOLETED in 1.60 (between Dec 2017 and Apr 2018)\n    static inline bool  IsAnyWindowFocused()                  { return IsWindowFocused(ImGuiFocusedFlags_AnyWindow); }\n    static inline bool  IsAnyWindowHovered()                  { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); }\n    static inline ImVec2 CalcItemRectClosestPoint(const ImVec2& pos, bool on_edge = false, float outward = 0.f) { IM_UNUSED(on_edge); IM_UNUSED(outward); IM_ASSERT(0); return pos; }\n    // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017)\n    static inline void  ShowTestWindow()                      { return ShowDemoWindow(); }\n    static inline bool  IsRootWindowFocused()                 { return IsWindowFocused(ImGuiFocusedFlags_RootWindow); }\n    static inline bool  IsRootWindowOrAnyChildFocused()       { return IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows); }\n    static inline void  SetNextWindowContentWidth(float w)    { SetNextWindowContentSize(ImVec2(w, 0.0f)); }\n    static inline float GetItemsLineHeightWithSpacing()       { return GetFrameHeightWithSpacing(); }\n    // OBSOLETED in 1.52 (between Aug 2017 and Oct 2017)\n    IMGUI_API bool      Begin(const char* name, bool* p_open, const ImVec2& size_on_first_use, float bg_alpha_override = -1.0f, ImGuiWindowFlags flags = 0); // Use SetNextWindowSize(size, ImGuiCond_FirstUseEver) + SetNextWindowBgAlpha() instead.\n    static inline bool  IsRootWindowOrAnyChildHovered()       { return IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows); }\n    static inline void  AlignFirstTextHeightToWidgets()       { AlignTextToFramePadding(); }\n    void                SetNextWindowPosCenter(ImGuiCond cond);\n    // OBSOLETED in 1.51 (between Jun 2017 and Aug 2017)\n    static inline bool  IsItemHoveredRect()                   { return IsItemHovered(ImGuiHoveredFlags_RectOnly); }\n    static inline bool  IsPosHoveringAnyWindow(const ImVec2&) { IM_ASSERT(0); return false; } // This was misleading and partly broken. You probably want to use the ImGui::GetIO().WantCaptureMouse flag instead.\n    static inline bool  IsMouseHoveringAnyWindow()            { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); }\n    static inline bool  IsMouseHoveringWindow()               { return IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem); }\n}\ntypedef ImGuiInputTextCallback      ImGuiTextEditCallback;    // OBSOLETE in 1.63 (from Aug 2018): made the names consistent\ntypedef ImGuiInputTextCallbackData  ImGuiTextEditCallbackData;\n#endif\n\n//-----------------------------------------------------------------------------\n// Helpers\n//-----------------------------------------------------------------------------\n\n// Helper: Execute a block of code at maximum once a frame. Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame.\n// Usage: static ImGuiOnceUponAFrame oaf; if (oaf) ImGui::Text(\"This will be called only once per frame\");\nstruct ImGuiOnceUponAFrame\n{\n    ImGuiOnceUponAFrame() { RefFrame = -1; }\n    mutable int RefFrame;\n    operator bool() const { int current_frame = ImGui::GetFrameCount(); if (RefFrame == current_frame) return false; RefFrame = current_frame; return true; }\n};\n\n// Helper: Macro for ImGuiOnceUponAFrame. Attention: The macro expands into 2 statement so make sure you don't use it within e.g. an if() statement without curly braces.\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n#define IMGUI_ONCE_UPON_A_FRAME     static ImGuiOnceUponAFrame imgui_oaf; if (imgui_oaf)    // OBSOLETED in 1.51, will remove!\n#endif\n\n// Helper: Parse and apply text filters. In format \"aaaaa[,bbbb][,ccccc]\"\nstruct ImGuiTextFilter\n{\n    IMGUI_API           ImGuiTextFilter(const char* default_filter = \"\");\n    IMGUI_API bool      Draw(const char* label = \"Filter (inc,-exc)\", float width = 0.0f);  // Helper calling InputText+Build\n    IMGUI_API bool      PassFilter(const char* text, const char* text_end = NULL) const;\n    IMGUI_API void      Build();\n    void                Clear()          { InputBuf[0] = 0; Build(); }\n    bool                IsActive() const { return !Filters.empty(); }\n\n    // [Internal]\n    struct TextRange\n    {\n        const char* b;\n        const char* e;\n\n        TextRange() { b = e = NULL; }\n        TextRange(const char* _b, const char* _e) { b = _b; e = _e; }\n        const char*     begin() const   { return b; }\n        const char*     end () const    { return e; }\n        bool            empty() const   { return b == e; }\n        IMGUI_API void  split(char separator, ImVector<TextRange>* out) const;\n    };\n    char                InputBuf[256];\n    ImVector<TextRange> Filters;\n    int                 CountGrep;\n};\n\n// Helper: Growable text buffer for logging/accumulating text\n// (this could be called 'ImGuiTextBuilder' / 'ImGuiStringBuilder')\nstruct ImGuiTextBuffer\n{\n    ImVector<char>      Buf;\n    static char         EmptyString[1];\n\n    ImGuiTextBuffer()   { }\n    inline char         operator[](int i)       { IM_ASSERT(Buf.Data != NULL); return Buf.Data[i]; }\n    const char*         begin() const           { return Buf.Data ? &Buf.front() : EmptyString; }\n    const char*         end() const             { return Buf.Data ? &Buf.back() : EmptyString; }   // Buf is zero-terminated, so end() will point on the zero-terminator\n    int                 size() const            { return Buf.Size ? Buf.Size - 1 : 0; }\n    bool                empty()                 { return Buf.Size <= 1; }\n    void                clear()                 { Buf.clear(); }\n    void                reserve(int capacity)   { Buf.reserve(capacity); }\n    const char*         c_str() const           { return Buf.Data ? Buf.Data : EmptyString; }\n    IMGUI_API void      append(const char* str, const char* str_end = NULL);\n    IMGUI_API void      appendf(const char* fmt, ...) IM_FMTARGS(2);\n    IMGUI_API void      appendfv(const char* fmt, va_list args) IM_FMTLIST(2);\n};\n\n// Helper: Key->Value storage\n// Typically you don't have to worry about this since a storage is held within each Window.\n// We use it to e.g. store collapse state for a tree (Int 0/1)\n// This is optimized for efficient lookup (dichotomy into a contiguous buffer) and rare insertion (typically tied to user interactions aka max once a frame)\n// You can use it as custom user storage for temporary values. Declare your own storage if, for example:\n// - You want to manipulate the open/close state of a particular sub-tree in your interface (tree node uses Int 0/1 to store their state).\n// - You want to store custom debug data easily without adding or editing structures in your code (probably not efficient, but convenient)\n// Types are NOT stored, so it is up to you to make sure your Key don't collide with different types.\nstruct ImGuiStorage\n{\n    struct Pair\n    {\n        ImGuiID key;\n        union { int val_i; float val_f; void* val_p; };\n        Pair(ImGuiID _key, int _val_i)   { key = _key; val_i = _val_i; }\n        Pair(ImGuiID _key, float _val_f) { key = _key; val_f = _val_f; }\n        Pair(ImGuiID _key, void* _val_p) { key = _key; val_p = _val_p; }\n    };\n    ImVector<Pair>      Data;\n\n    // - Get***() functions find pair, never add/allocate. Pairs are sorted so a query is O(log N)\n    // - Set***() functions find pair, insertion on demand if missing.\n    // - Sorted insertion is costly, paid once. A typical frame shouldn't need to insert any new pair.\n    void                Clear() { Data.clear(); }\n    IMGUI_API int       GetInt(ImGuiID key, int default_val = 0) const;\n    IMGUI_API void      SetInt(ImGuiID key, int val);\n    IMGUI_API bool      GetBool(ImGuiID key, bool default_val = false) const;\n    IMGUI_API void      SetBool(ImGuiID key, bool val);\n    IMGUI_API float     GetFloat(ImGuiID key, float default_val = 0.0f) const;\n    IMGUI_API void      SetFloat(ImGuiID key, float val);\n    IMGUI_API void*     GetVoidPtr(ImGuiID key) const; // default_val is NULL\n    IMGUI_API void      SetVoidPtr(ImGuiID key, void* val);\n\n    // - Get***Ref() functions finds pair, insert on demand if missing, return pointer. Useful if you intend to do Get+Set.\n    // - References are only valid until a new value is added to the storage. Calling a Set***() function or a Get***Ref() function invalidates the pointer.\n    // - A typical use case where this is convenient for quick hacking (e.g. add storage during a live Edit&Continue session if you can't modify existing struct)\n    //      float* pvar = ImGui::GetFloatRef(key); ImGui::SliderFloat(\"var\", pvar, 0, 100.0f); some_var += *pvar;\n    IMGUI_API int*      GetIntRef(ImGuiID key, int default_val = 0);\n    IMGUI_API bool*     GetBoolRef(ImGuiID key, bool default_val = false);\n    IMGUI_API float*    GetFloatRef(ImGuiID key, float default_val = 0.0f);\n    IMGUI_API void**    GetVoidPtrRef(ImGuiID key, void* default_val = NULL);\n\n    // Use on your own storage if you know only integer are being stored (open/close all tree nodes)\n    IMGUI_API void      SetAllInt(int val);\n\n    // For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once.\n    IMGUI_API void      BuildSortByKey();\n};\n\n// Helper: Manually clip large list of items.\n// If you are submitting lots of evenly spaced items and you have a random access to the list, you can perform coarse clipping based on visibility to save yourself from processing those items at all.\n// The clipper calculates the range of visible items and advance the cursor to compensate for the non-visible items we have skipped.\n// ImGui already clip items based on their bounds but it needs to measure text size to do so. Coarse clipping before submission makes this cost and your own data fetching/submission cost null.\n// Usage:\n//     ImGuiListClipper clipper(1000);  // we have 1000 elements, evenly spaced.\n//     while (clipper.Step())\n//         for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)\n//             ImGui::Text(\"line number %d\", i);\n// - Step 0: the clipper let you process the first element, regardless of it being visible or not, so we can measure the element height (step skipped if we passed a known height as second arg to constructor).\n// - Step 1: the clipper infer height from first element, calculate the actual range of elements to display, and position the cursor before the first element.\n// - (Step 2: dummy step only required if an explicit items_height was passed to constructor or Begin() and user call Step(). Does nothing and switch to Step 3.)\n// - Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), advance the cursor to the end of the list and then returns 'false' to end the loop.\nstruct ImGuiListClipper\n{\n    float   StartPosY;\n    float   ItemsHeight;\n    int     ItemsCount, StepNo, DisplayStart, DisplayEnd;\n\n    // items_count:  Use -1 to ignore (you can call Begin later). Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step).\n    // items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing().\n    // If you don't specify an items_height, you NEED to call Step(). If you specify items_height you may call the old Begin()/End() api directly, but prefer calling Step().\n    ImGuiListClipper(int items_count = -1, float items_height = -1.0f)  { Begin(items_count, items_height); } // NB: Begin() initialize every fields (as we allow user to call Begin/End multiple times on a same instance if they want).\n    ~ImGuiListClipper()                                                 { IM_ASSERT(ItemsCount == -1); }      // Assert if user forgot to call End() or Step() until false.\n\n    IMGUI_API bool Step();                                              // Call until it returns false. The DisplayStart/DisplayEnd fields will be set and you can process/draw those items.\n    IMGUI_API void Begin(int items_count, float items_height = -1.0f);  // Automatically called by constructor if you passed 'items_count' or by Step() in Step 1.\n    IMGUI_API void End();                                               // Automatically called on the last call of Step() that returns false.\n};\n\n// Helpers macros to generate 32-bits encoded colors\n#ifdef IMGUI_USE_BGRA_PACKED_COLOR\n#define IM_COL32_R_SHIFT    16\n#define IM_COL32_G_SHIFT    8\n#define IM_COL32_B_SHIFT    0\n#define IM_COL32_A_SHIFT    24\n#define IM_COL32_A_MASK     0xFF000000\n#else\n#define IM_COL32_R_SHIFT    0\n#define IM_COL32_G_SHIFT    8\n#define IM_COL32_B_SHIFT    16\n#define IM_COL32_A_SHIFT    24\n#define IM_COL32_A_MASK     0xFF000000\n#endif\n#define IM_COL32(R,G,B,A)    (((ImU32)(A)<<IM_COL32_A_SHIFT) | ((ImU32)(B)<<IM_COL32_B_SHIFT) | ((ImU32)(G)<<IM_COL32_G_SHIFT) | ((ImU32)(R)<<IM_COL32_R_SHIFT))\n#define IM_COL32_WHITE       IM_COL32(255,255,255,255)  // Opaque white = 0xFFFFFFFF\n#define IM_COL32_BLACK       IM_COL32(0,0,0,255)        // Opaque black\n#define IM_COL32_BLACK_TRANS IM_COL32(0,0,0,0)          // Transparent black = 0x00000000\n\n// Helper: ImColor() implicitly converts colors to either ImU32 (packed 4x1 byte) or ImVec4 (4x1 float)\n// Prefer using IM_COL32() macros if you want a guaranteed compile-time ImU32 for usage with ImDrawList API.\n// **Avoid storing ImColor! Store either u32 of ImVec4. This is not a full-featured color class. MAY OBSOLETE.\n// **None of the ImGui API are using ImColor directly but you can use it as a convenience to pass colors in either ImU32 or ImVec4 formats. Explicitly cast to ImU32 or ImVec4 if needed.\nstruct ImColor\n{\n    ImVec4              Value;\n\n    ImColor()                                                       { Value.x = Value.y = Value.z = Value.w = 0.0f; }\n    ImColor(int r, int g, int b, int a = 255)                       { float sc = 1.0f/255.0f; Value.x = (float)r * sc; Value.y = (float)g * sc; Value.z = (float)b * sc; Value.w = (float)a * sc; }\n    ImColor(ImU32 rgba)                                             { float sc = 1.0f/255.0f; Value.x = (float)((rgba>>IM_COL32_R_SHIFT)&0xFF) * sc; Value.y = (float)((rgba>>IM_COL32_G_SHIFT)&0xFF) * sc; Value.z = (float)((rgba>>IM_COL32_B_SHIFT)&0xFF) * sc; Value.w = (float)((rgba>>IM_COL32_A_SHIFT)&0xFF) * sc; }\n    ImColor(float r, float g, float b, float a = 1.0f)              { Value.x = r; Value.y = g; Value.z = b; Value.w = a; }\n    ImColor(const ImVec4& col)                                      { Value = col; }\n    inline operator ImU32() const                                   { return ImGui::ColorConvertFloat4ToU32(Value); }\n    inline operator ImVec4() const                                  { return Value; }\n\n    // FIXME-OBSOLETE: May need to obsolete/cleanup those helpers.\n    inline void    SetHSV(float h, float s, float v, float a = 1.0f){ ImGui::ColorConvertHSVtoRGB(h, s, v, Value.x, Value.y, Value.z); Value.w = a; }\n    static ImColor HSV(float h, float s, float v, float a = 1.0f)   { float r,g,b; ImGui::ColorConvertHSVtoRGB(h, s, v, r, g, b); return ImColor(r,g,b,a); }\n};\n\n//-----------------------------------------------------------------------------\n// Draw List API (ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListFlags, ImDrawList, ImDrawData)\n// Hold a series of drawing commands. The user provides a renderer for ImDrawData which essentially contains an array of ImDrawList.\n//-----------------------------------------------------------------------------\n\n// Draw callbacks for advanced uses.\n// NB: You most likely do NOT need to use draw callbacks just to create your own widget or customized UI rendering,\n// you can poke into the draw list for that! Draw callback may be useful for example to: A) Change your GPU render state,\n// B) render a complex 3D scene inside a UI element without an intermediate texture/render target, etc.\n// The expected behavior from your rendering function is 'if (cmd.UserCallback != NULL) { cmd.UserCallback(parent_list, cmd); } else { RenderTriangles() }'\ntypedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* cmd);\n\n// Typically, 1 command = 1 GPU draw call (unless command is a callback)\nstruct ImDrawCmd\n{\n    unsigned int    ElemCount;              // Number of indices (multiple of 3) to be rendered as triangles. Vertices are stored in the callee ImDrawList's vtx_buffer[] array, indices in idx_buffer[].\n    ImVec4          ClipRect;               // Clipping rectangle (x1, y1, x2, y2). Subtract ImDrawData->DisplayPos to get clipping rectangle in \"viewport\" coordinates\n    ImTextureID     TextureId;              // User-provided texture ID. Set by user in ImfontAtlas::SetTexID() for fonts or passed to Image*() functions. Ignore if never using images or multiple fonts atlas.\n    ImDrawCallback  UserCallback;           // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally.\n    void*           UserCallbackData;       // The draw callback code can access this.\n\n    ImDrawCmd() { ElemCount = 0; ClipRect.x = ClipRect.y = ClipRect.z = ClipRect.w = 0.0f; TextureId = (ImTextureID)NULL; UserCallback = NULL; UserCallbackData = NULL; }\n};\n\n// Vertex index (override with '#define ImDrawIdx unsigned int' in imconfig.h)\n#ifndef ImDrawIdx\ntypedef unsigned short ImDrawIdx;\n#endif\n\n// Vertex layout\n#ifndef IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT\nstruct ImDrawVert\n{\n    ImVec2  pos;\n    ImVec2  uv;\n    ImU32   col;\n};\n#else\n// You can override the vertex format layout by defining IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT in imconfig.h\n// The code expect ImVec2 pos (8 bytes), ImVec2 uv (8 bytes), ImU32 col (4 bytes), but you can re-order them or add other fields as needed to simplify integration in your engine.\n// The type has to be described within the macro (you can either declare the struct or use a typedef)\n// NOTE: IMGUI DOESN'T CLEAR THE STRUCTURE AND DOESN'T CALL A CONSTRUCTOR SO ANY CUSTOM FIELD WILL BE UNINITIALIZED. IF YOU ADD EXTRA FIELDS (SUCH AS A 'Z' COORDINATES) YOU WILL NEED TO CLEAR THEM DURING RENDER OR TO IGNORE THEM.\nIMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT;\n#endif\n\n// Draw channels are used by the Columns API to \"split\" the render list into different channels while building, so items of each column can be batched together.\n// You can also use them to simulate drawing layers and submit primitives in a different order than how they will be rendered.\nstruct ImDrawChannel\n{\n    ImVector<ImDrawCmd>     CmdBuffer;\n    ImVector<ImDrawIdx>     IdxBuffer;\n};\n\nenum ImDrawCornerFlags_\n{\n    ImDrawCornerFlags_TopLeft   = 1 << 0, // 0x1\n    ImDrawCornerFlags_TopRight  = 1 << 1, // 0x2\n    ImDrawCornerFlags_BotLeft   = 1 << 2, // 0x4\n    ImDrawCornerFlags_BotRight  = 1 << 3, // 0x8\n    ImDrawCornerFlags_Top       = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_TopRight,   // 0x3\n    ImDrawCornerFlags_Bot       = ImDrawCornerFlags_BotLeft | ImDrawCornerFlags_BotRight,   // 0xC\n    ImDrawCornerFlags_Left      = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft,    // 0x5\n    ImDrawCornerFlags_Right     = ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight,  // 0xA\n    ImDrawCornerFlags_All       = 0xF     // In your function calls you may use ~0 (= all bits sets) instead of ImDrawCornerFlags_All, as a convenience\n};\n\nenum ImDrawListFlags_\n{\n    ImDrawListFlags_None             = 0,\n    ImDrawListFlags_AntiAliasedLines = 1 << 0,  // Lines are anti-aliased (*2 the number of triangles for 1.0f wide line, otherwise *3 the number of triangles)\n    ImDrawListFlags_AntiAliasedFill  = 1 << 1   // Filled shapes have anti-aliased edges (*2 the number of vertices)\n};\n\n// Draw command list\n// This is the low-level list of polygons that ImGui functions are filling. At the end of the frame, all command lists are passed to your ImGuiIO::RenderDrawListFn function for rendering.\n// Each ImGui window contains its own ImDrawList. You can use ImGui::GetWindowDrawList() to access the current window draw list and draw custom primitives.\n// You can interleave normal ImGui:: calls and adding primitives to the current draw list.\n// All positions are generally in pixel coordinates (generally top-left at 0,0, bottom-right at io.DisplaySize, unless multiple viewports are used), but you are totally free to apply whatever transformation matrix to want to the data (if you apply such transformation you'll want to apply it to ClipRect as well)\n// Important: Primitives are always added to the list and not culled (culling is done at higher-level by ImGui:: functions), if you use this API a lot consider coarse culling your drawn objects.\nstruct ImDrawList\n{\n    // This is what you have to render\n    ImVector<ImDrawCmd>     CmdBuffer;          // Draw commands. Typically 1 command = 1 GPU draw call, unless the command is a callback.\n    ImVector<ImDrawIdx>     IdxBuffer;          // Index buffer. Each command consume ImDrawCmd::ElemCount of those\n    ImVector<ImDrawVert>    VtxBuffer;          // Vertex buffer.\n    ImDrawListFlags         Flags;              // Flags, you may poke into these to adjust anti-aliasing settings per-primitive.\n\n    // [Internal, used while building lists]\n    const ImDrawListSharedData* _Data;          // Pointer to shared draw data (you can use ImGui::GetDrawListSharedData() to get the one from current ImGui context)\n    const char*             _OwnerName;         // Pointer to owner window's name for debugging\n    unsigned int            _VtxCurrentIdx;     // [Internal] == VtxBuffer.Size\n    ImDrawVert*             _VtxWritePtr;       // [Internal] point within VtxBuffer.Data after each add command (to avoid using the ImVector<> operators too much)\n    ImDrawIdx*              _IdxWritePtr;       // [Internal] point within IdxBuffer.Data after each add command (to avoid using the ImVector<> operators too much)\n    ImVector<ImVec4>        _ClipRectStack;     // [Internal]\n    ImVector<ImTextureID>   _TextureIdStack;    // [Internal]\n    ImVector<ImVec2>        _Path;              // [Internal] current path building\n    int                     _ChannelsCurrent;   // [Internal] current channel number (0)\n    int                     _ChannelsCount;     // [Internal] number of active channels (1+)\n    ImVector<ImDrawChannel> _Channels;          // [Internal] draw channels for columns API (not resized down so _ChannelsCount may be smaller than _Channels.Size)\n\n    // If you want to create ImDrawList instances, pass them ImGui::GetDrawListSharedData() or create and use your own ImDrawListSharedData (so you can use ImDrawList without ImGui)\n    ImDrawList(const ImDrawListSharedData* shared_data) { _Data = shared_data; _OwnerName = NULL; Clear(); }\n    ~ImDrawList() { ClearFreeMemory(); }\n    IMGUI_API void  PushClipRect(ImVec2 clip_rect_min, ImVec2 clip_rect_max, bool intersect_with_current_clip_rect = false);  // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling)\n    IMGUI_API void  PushClipRectFullScreen();\n    IMGUI_API void  PopClipRect();\n    IMGUI_API void  PushTextureID(ImTextureID texture_id);\n    IMGUI_API void  PopTextureID();\n    inline ImVec2   GetClipRectMin() const { const ImVec4& cr = _ClipRectStack.back(); return ImVec2(cr.x, cr.y); }\n    inline ImVec2   GetClipRectMax() const { const ImVec4& cr = _ClipRectStack.back(); return ImVec2(cr.z, cr.w); }\n\n    // Primitives\n    IMGUI_API void  AddLine(const ImVec2& a, const ImVec2& b, ImU32 col, float thickness = 1.0f);\n    IMGUI_API void  AddRect(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding = 0.0f, int rounding_corners_flags = ImDrawCornerFlags_All, float thickness = 1.0f);   // a: upper-left, b: lower-right (== upper-left + size), rounding_corners_flags: 4-bits corresponding to which corner to round\n    IMGUI_API void  AddRectFilled(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding = 0.0f, int rounding_corners_flags = ImDrawCornerFlags_All);                     // a: upper-left, b: lower-right (== upper-left + size)\n    IMGUI_API void  AddRectFilledMultiColor(const ImVec2& a, const ImVec2& b, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left);\n    IMGUI_API void  AddQuad(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, ImU32 col, float thickness = 1.0f);\n    IMGUI_API void  AddQuadFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, ImU32 col);\n    IMGUI_API void  AddTriangle(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col, float thickness = 1.0f);\n    IMGUI_API void  AddTriangleFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col);\n    IMGUI_API void  AddCircle(const ImVec2& centre, float radius, ImU32 col, int num_segments = 12, float thickness = 1.0f);\n    IMGUI_API void  AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, int num_segments = 12);\n    IMGUI_API void  AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL);\n    IMGUI_API void  AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL);\n    IMGUI_API void  AddImage(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a = ImVec2(0,0), const ImVec2& uv_b = ImVec2(1,1), ImU32 col = IM_COL32_WHITE);\n    IMGUI_API void  AddImageQuad(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a = ImVec2(0,0), const ImVec2& uv_b = ImVec2(1,0), const ImVec2& uv_c = ImVec2(1,1), const ImVec2& uv_d = ImVec2(0,1), ImU32 col = IM_COL32_WHITE);\n    IMGUI_API void  AddImageRounded(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, ImU32 col, float rounding, int rounding_corners = ImDrawCornerFlags_All);\n    IMGUI_API void  AddPolyline(const ImVec2* points, int num_points, ImU32 col, bool closed, float thickness);\n    IMGUI_API void  AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); // Note: Anti-aliased filling requires points to be in clockwise order.\n    IMGUI_API void  AddBezierCurve(const ImVec2& pos0, const ImVec2& cp0, const ImVec2& cp1, const ImVec2& pos1, ImU32 col, float thickness, int num_segments = 0);\n\n    // Stateful path API, add points then finish with PathFillConvex() or PathStroke()\n    inline    void  PathClear()                                                 { _Path.Size = 0; }\n    inline    void  PathLineTo(const ImVec2& pos)                               { _Path.push_back(pos); }\n    inline    void  PathLineToMergeDuplicate(const ImVec2& pos)                 { if (_Path.Size == 0 || memcmp(&_Path.Data[_Path.Size-1], &pos, 8) != 0) _Path.push_back(pos); }\n    inline    void  PathFillConvex(ImU32 col)                                   { AddConvexPolyFilled(_Path.Data, _Path.Size, col); _Path.Size = 0; }  // Note: Anti-aliased filling requires points to be in clockwise order.\n    inline    void  PathStroke(ImU32 col, bool closed, float thickness = 1.0f)  { AddPolyline(_Path.Data, _Path.Size, col, closed, thickness); _Path.Size = 0; }\n    IMGUI_API void  PathArcTo(const ImVec2& centre, float radius, float a_min, float a_max, int num_segments = 10);\n    IMGUI_API void  PathArcToFast(const ImVec2& centre, float radius, int a_min_of_12, int a_max_of_12);                                            // Use precomputed angles for a 12 steps circle\n    IMGUI_API void  PathBezierCurveTo(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, int num_segments = 0);\n    IMGUI_API void  PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, int rounding_corners_flags = ImDrawCornerFlags_All);\n\n    // Channels\n    // - Use to simulate layers. By switching channels to can render out-of-order (e.g. submit foreground primitives before background primitives)\n    // - Use to minimize draw calls (e.g. if going back-and-forth between multiple non-overlapping clipping rectangles, prefer to append into separate channels then merge at the end)\n    IMGUI_API void  ChannelsSplit(int channels_count);\n    IMGUI_API void  ChannelsMerge();\n    IMGUI_API void  ChannelsSetCurrent(int channel_index);\n\n    // Advanced\n    IMGUI_API void  AddCallback(ImDrawCallback callback, void* callback_data);  // Your rendering function must check for 'UserCallback' in ImDrawCmd and call the function instead of rendering triangles.\n    IMGUI_API void  AddDrawCmd();                                               // This is useful if you need to forcefully create a new draw call (to allow for dependent rendering / blending). Otherwise primitives are merged into the same draw-call as much as possible\n    IMGUI_API ImDrawList* CloneOutput() const;                                  // Create a clone of the CmdBuffer/IdxBuffer/VtxBuffer.\n\n    // Internal helpers\n    // NB: all primitives needs to be reserved via PrimReserve() beforehand!\n    IMGUI_API void  Clear();\n    IMGUI_API void  ClearFreeMemory();\n    IMGUI_API void  PrimReserve(int idx_count, int vtx_count);\n    IMGUI_API void  PrimRect(const ImVec2& a, const ImVec2& b, ImU32 col);      // Axis aligned rectangle (composed of two triangles)\n    IMGUI_API void  PrimRectUV(const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, ImU32 col);\n    IMGUI_API void  PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a, const ImVec2& uv_b, const ImVec2& uv_c, const ImVec2& uv_d, ImU32 col);\n    inline    void  PrimWriteVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col){ _VtxWritePtr->pos = pos; _VtxWritePtr->uv = uv; _VtxWritePtr->col = col; _VtxWritePtr++; _VtxCurrentIdx++; }\n    inline    void  PrimWriteIdx(ImDrawIdx idx)                                 { *_IdxWritePtr = idx; _IdxWritePtr++; }\n    inline    void  PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col)     { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); }\n    IMGUI_API void  UpdateClipRect();\n    IMGUI_API void  UpdateTextureID();\n};\n\n// All draw data to render an ImGui frame\n// (NB: the style and the naming convention here is a little inconsistent, we currently preserve them for backward compatibility purpose,\n// as this is one of the oldest structure exposed by the library! Basically, ImDrawList == CmdList)\nstruct ImDrawData\n{\n    bool            Valid;                  // Only valid after Render() is called and before the next NewFrame() is called.\n    ImDrawList**    CmdLists;               // Array of ImDrawList* to render. The ImDrawList are owned by ImGuiContext and only pointed to from here.\n    int             CmdListsCount;          // Number of ImDrawList* to render\n    int             TotalIdxCount;          // For convenience, sum of all ImDrawList's IdxBuffer.Size\n    int             TotalVtxCount;          // For convenience, sum of all ImDrawList's VtxBuffer.Size\n    ImVec2          DisplayPos;             // Upper-left position of the viewport to render (== upper-left of the orthogonal projection matrix to use)\n    ImVec2          DisplaySize;            // Size of the viewport to render (== io.DisplaySize for the main viewport) (DisplayPos + DisplaySize == lower-right of the orthogonal projection matrix to use)\n    ImVec2          FramebufferScale;       // Amount of pixels for each unit of DisplaySize. Based on io.DisplayFramebufferScale. Generally (1,1) on normal display, (2,2) on OSX with Retina display.\n    ImGuiViewport*  OwnerViewport;          // Viewport carrying the ImDrawData instance, might be of use to the renderer (generally not).\n\n    // Functions\n    ImDrawData()    { Valid = false; Clear(); }\n    ~ImDrawData()   { Clear(); }\n    void Clear()    { Valid = false; CmdLists = NULL; CmdListsCount = TotalVtxCount = TotalIdxCount = 0; DisplayPos = DisplaySize = FramebufferScale = ImVec2(0.f, 0.f); OwnerViewport = NULL; } // The ImDrawList are owned by ImGuiContext!\n    IMGUI_API void  DeIndexAllBuffers();                    // Helper to convert all buffers from indexed to non-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering!\n    IMGUI_API void  ScaleClipRects(const ImVec2& fb_scale); // Helper to scale the ClipRect field of each ImDrawCmd. Use if your final output buffer is at a different scale than ImGui expects, or if there is a difference between your window resolution and framebuffer resolution.\n};\n\n//-----------------------------------------------------------------------------\n// Font API (ImFontConfig, ImFontGlyph, ImFontAtlasFlags, ImFontAtlas, ImFontGlyphRangesBuilder, ImFont)\n//-----------------------------------------------------------------------------\n\nstruct ImFontConfig\n{\n    void*           FontData;               //          // TTF/OTF data\n    int             FontDataSize;           //          // TTF/OTF data size\n    bool            FontDataOwnedByAtlas;   // true     // TTF/OTF data ownership taken by the container ImFontAtlas (will delete memory itself).\n    int             FontNo;                 // 0        // Index of font within TTF/OTF file\n    float           SizePixels;             //          // Size in pixels for rasterizer (more or less maps to the resulting font height).\n    int             OversampleH;            // 3        // Rasterize at higher quality for sub-pixel positioning. Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details.\n    int             OversampleV;            // 1        // Rasterize at higher quality for sub-pixel positioning. We don't use sub-pixel positions on the Y axis.\n    bool            PixelSnapH;             // false    // Align every glyph to pixel boundary. Useful e.g. if you are merging a non-pixel aligned font with the default font. If enabled, you can set OversampleH/V to 1.\n    ImVec2          GlyphExtraSpacing;      // 0, 0     // Extra spacing (in pixels) between glyphs. Only X axis is supported for now.\n    ImVec2          GlyphOffset;            // 0, 0     // Offset all glyphs from this font input.\n    const ImWchar*  GlyphRanges;            // NULL     // Pointer to a user-provided list of Unicode range (2 value per range, values are inclusive, zero-terminated list). THE ARRAY DATA NEEDS TO PERSIST AS LONG AS THE FONT IS ALIVE.\n    float           GlyphMinAdvanceX;       // 0        // Minimum AdvanceX for glyphs, set Min to align font icons, set both Min/Max to enforce mono-space font\n    float           GlyphMaxAdvanceX;       // FLT_MAX  // Maximum AdvanceX for glyphs\n    bool            MergeMode;              // false    // Merge into previous ImFont, so you can combine multiple inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). You may want to use GlyphOffset.y when merge font of different heights.\n    unsigned int    RasterizerFlags;        // 0x00     // Settings for custom font rasterizer (e.g. ImGuiFreeType). Leave as zero if you aren't using one.\n    float           RasterizerMultiply;     // 1.0f     // Brighten (>1.0f) or darken (<1.0f) font output. Brightening small fonts may be a good workaround to make them more readable.\n\n    // [Internal]\n    char            Name[40];               // Name (strictly to ease debugging)\n    ImFont*         DstFont;\n\n    IMGUI_API ImFontConfig();\n};\n\nstruct ImFontGlyph\n{\n    ImWchar         Codepoint;          // 0x0000..0xFFFF\n    float           AdvanceX;           // Distance to next character (= data from font + ImFontConfig::GlyphExtraSpacing.x baked in)\n    float           X0, Y0, X1, Y1;     // Glyph corners\n    float           U0, V0, U1, V1;     // Texture coordinates\n};\n\n// Helper to build glyph ranges from text/string data. Feed your application strings/characters to it then call BuildRanges().\n// This is essentially a tightly packed of vector of 64k booleans = 8KB storage.\nstruct ImFontGlyphRangesBuilder\n{\n    ImVector<int> UsedChars;            // Store 1-bit per Unicode code point (0=unused, 1=used)\n\n    ImFontGlyphRangesBuilder()          { UsedChars.resize(0x10000 / sizeof(int)); memset(UsedChars.Data, 0, 0x10000 / sizeof(int)); }\n    bool            GetBit(int n) const { int off = (n >> 5); int mask = 1 << (n & 31); return (UsedChars[off] & mask) != 0; }  // Get bit n in the array\n    void            SetBit(int n)       { int off = (n >> 5); int mask = 1 << (n & 31); UsedChars[off] |= mask; }               // Set bit n in the array\n    void            AddChar(ImWchar c)  { SetBit(c); }                          // Add character\n    IMGUI_API void  AddText(const char* text, const char* text_end = NULL);     // Add string (each character of the UTF-8 string are added)\n    IMGUI_API void  AddRanges(const ImWchar* ranges);                           // Add ranges, e.g. builder.AddRanges(ImFontAtlas::GetGlyphRangesDefault()) to force add all of ASCII/Latin+Ext\n    IMGUI_API void  BuildRanges(ImVector<ImWchar>* out_ranges);                 // Output new ranges\n};\n\nenum ImFontAtlasFlags_\n{\n    ImFontAtlasFlags_None               = 0,\n    ImFontAtlasFlags_NoPowerOfTwoHeight = 1 << 0,   // Don't round the height to next power of two\n    ImFontAtlasFlags_NoMouseCursors     = 1 << 1    // Don't build software mouse cursors into the atlas\n};\n\n// Load and rasterize multiple TTF/OTF fonts into a same texture. The font atlas will build a single texture holding:\n//  - One or more fonts.\n//  - Custom graphics data needed to render the shapes needed by Dear ImGui.\n//  - Mouse cursor shapes for software cursor rendering (unless setting 'Flags |= ImFontAtlasFlags_NoMouseCursors' in the font atlas).\n// It is the user-code responsibility to setup/build the atlas, then upload the pixel data into a texture accessible by your graphics api.\n//  - Optionally, call any of the AddFont*** functions. If you don't call any, the default font embedded in the code will be loaded for you.\n//  - Call GetTexDataAsAlpha8() or GetTexDataAsRGBA32() to build and retrieve pixels data.\n//  - Upload the pixels data into a texture within your graphics system (see imgui_impl_xxxx.cpp examples)\n//  - Call SetTexID(my_tex_id); and pass the pointer/identifier to your texture in a format natural to your graphics API.\n//    This value will be passed back to you during rendering to identify the texture. Read FAQ entry about ImTextureID for more details.\n// Common pitfalls:\n// - If you pass a 'glyph_ranges' array to AddFont*** functions, you need to make sure that your array persist up until the\n//   atlas is build (when calling GetTexData*** or Build()). We only copy the pointer, not the data.\n// - Important: By default, AddFontFromMemoryTTF() takes ownership of the data. Even though we are not writing to it, we will free the pointer on destruction.\n//   You can set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed,\n// - Even though many functions are suffixed with \"TTF\", OTF data is supported just as well.\n// - This is an old API and it is currently awkward for those and and various other reasons! We will address them in the future!\nstruct ImFontAtlas\n{\n    IMGUI_API ImFontAtlas();\n    IMGUI_API ~ImFontAtlas();\n    IMGUI_API ImFont*           AddFont(const ImFontConfig* font_cfg);\n    IMGUI_API ImFont*           AddFontDefault(const ImFontConfig* font_cfg = NULL);\n    IMGUI_API ImFont*           AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL);\n    IMGUI_API ImFont*           AddFontFromMemoryTTF(void* font_data, int font_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // Note: Transfer ownership of 'ttf_data' to ImFontAtlas! Will be deleted after destruction of the atlas. Set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed.\n    IMGUI_API ImFont*           AddFontFromMemoryCompressedTTF(const void* compressed_font_data, int compressed_font_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data' still owned by caller. Compress with binary_to_compressed_c.cpp.\n    IMGUI_API ImFont*           AddFontFromMemoryCompressedBase85TTF(const char* compressed_font_data_base85, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL);              // 'compressed_font_data_base85' still owned by caller. Compress with binary_to_compressed_c.cpp with -base85 parameter.\n    IMGUI_API void              ClearInputData();           // Clear input data (all ImFontConfig structures including sizes, TTF data, glyph ranges, etc.) = all the data used to build the texture and fonts.\n    IMGUI_API void              ClearTexData();             // Clear output texture data (CPU side). Saves RAM once the texture has been copied to graphics memory.\n    IMGUI_API void              ClearFonts();               // Clear output font data (glyphs storage, UV coordinates).\n    IMGUI_API void              Clear();                    // Clear all input and output.\n\n    // Build atlas, retrieve pixel data.\n    // User is in charge of copying the pixels into graphics memory (e.g. create a texture with your engine). Then store your texture handle with SetTexID().\n    // The pitch is always = Width * BytesPerPixels (1 or 4)\n    // Building in RGBA32 format is provided for convenience and compatibility, but note that unless you manually manipulate or copy color data into\n    // the texture (e.g. when using the AddCustomRect*** api), then the RGB pixels emitted will always be white (~75% of memory/bandwidth waste.\n    IMGUI_API bool              Build();                    // Build pixels data. This is called automatically for you by the GetTexData*** functions.\n    IMGUI_API void              GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL);  // 1 byte per-pixel\n    IMGUI_API void              GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL);  // 4 bytes-per-pixel\n    bool                        IsBuilt()                   { return Fonts.Size > 0 && (TexPixelsAlpha8 != NULL || TexPixelsRGBA32 != NULL); }\n    void                        SetTexID(ImTextureID id)    { TexID = id; }\n\n    //-------------------------------------------\n    // Glyph Ranges\n    //-------------------------------------------\n\n    // Helpers to retrieve list of common Unicode ranges (2 value per range, values are inclusive, zero-terminated list)\n    // NB: Make sure that your string are UTF-8 and NOT in your local code page. In C++11, you can create UTF-8 string literal using the u8\"Hello world\" syntax. See FAQ for details.\n    // NB: Consider using ImFontGlyphRangesBuilder to build glyph ranges from textual data.\n    IMGUI_API const ImWchar*    GetGlyphRangesDefault();                // Basic Latin, Extended Latin\n    IMGUI_API const ImWchar*    GetGlyphRangesKorean();                 // Default + Korean characters\n    IMGUI_API const ImWchar*    GetGlyphRangesJapanese();               // Default + Hiragana, Katakana, Half-Width, Selection of 1946 Ideographs\n    IMGUI_API const ImWchar*    GetGlyphRangesChineseFull();            // Default + Half-Width + Japanese Hiragana/Katakana + full set of about 21000 CJK Unified Ideographs\n    IMGUI_API const ImWchar*    GetGlyphRangesChineseSimplifiedCommon();// Default + Half-Width + Japanese Hiragana/Katakana + set of 2500 CJK Unified Ideographs for common simplified Chinese\n    IMGUI_API const ImWchar*    GetGlyphRangesCyrillic();               // Default + about 400 Cyrillic characters\n    IMGUI_API const ImWchar*    GetGlyphRangesThai();                   // Default + Thai characters\n    IMGUI_API const ImWchar*    GetGlyphRangesVietnamese();             // Default + Vietname characters\n\n    //-------------------------------------------\n    // Custom Rectangles/Glyphs API\n    //-------------------------------------------\n\n    // You can request arbitrary rectangles to be packed into the atlas, for your own purposes. After calling Build(), you can query the rectangle position and render your pixels.\n    // You can also request your rectangles to be mapped as font glyph (given a font + Unicode point), so you can render e.g. custom colorful icons and use them as regular glyphs.\n    struct CustomRect\n    {\n        unsigned int    ID;             // Input    // User ID. Use <0x10000 to map into a font glyph, >=0x10000 for other/internal/custom texture data.\n        unsigned short  Width, Height;  // Input    // Desired rectangle dimension\n        unsigned short  X, Y;           // Output   // Packed position in Atlas\n        float           GlyphAdvanceX;  // Input    // For custom font glyphs only (ID<0x10000): glyph xadvance\n        ImVec2          GlyphOffset;    // Input    // For custom font glyphs only (ID<0x10000): glyph display offset\n        ImFont*         Font;           // Input    // For custom font glyphs only (ID<0x10000): target font\n        CustomRect()            { ID = 0xFFFFFFFF; Width = Height = 0; X = Y = 0xFFFF; GlyphAdvanceX = 0.0f; GlyphOffset = ImVec2(0,0); Font = NULL; }\n        bool IsPacked() const   { return X != 0xFFFF; }\n    };\n\n    IMGUI_API int       AddCustomRectRegular(unsigned int id, int width, int height);                                                                   // Id needs to be >= 0x10000. Id >= 0x80000000 are reserved for ImGui and ImDrawList\n    IMGUI_API int       AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0,0));   // Id needs to be < 0x10000 to register a rectangle to map into a specific font.\n    const CustomRect*   GetCustomRectByIndex(int index) const { if (index < 0) return NULL; return &CustomRects[index]; }\n\n    // [Internal]\n    IMGUI_API void      CalcCustomRectUV(const CustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max);\n    IMGUI_API bool      GetMouseCursorTexData(ImGuiMouseCursor cursor, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2]);\n\n    //-------------------------------------------\n    // Members\n    //-------------------------------------------\n\n    bool                        Locked;             // Marked as Locked by ImGui::NewFrame() so attempt to modify the atlas will assert.\n    ImFontAtlasFlags            Flags;              // Build flags (see ImFontAtlasFlags_)\n    ImTextureID                 TexID;              // User data to refer to the texture once it has been uploaded to user's graphic systems. It is passed back to you during rendering via the ImDrawCmd structure.\n    int                         TexDesiredWidth;    // Texture width desired by user before Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height.\n    int                         TexGlyphPadding;    // Padding between glyphs within texture in pixels. Defaults to 1. If your rendering method doesn't rely on bilinear filtering you may set this to 0.\n\n    // [Internal]\n    // NB: Access texture data via GetTexData*() calls! Which will setup a default font for you.\n    unsigned char*              TexPixelsAlpha8;    // 1 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight\n    unsigned int*               TexPixelsRGBA32;    // 4 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight * 4\n    int                         TexWidth;           // Texture width calculated during Build().\n    int                         TexHeight;          // Texture height calculated during Build().\n    ImVec2                      TexUvScale;         // = (1.0f/TexWidth, 1.0f/TexHeight)\n    ImVec2                      TexUvWhitePixel;    // Texture coordinates to a white pixel\n    ImVector<ImFont*>           Fonts;              // Hold all the fonts returned by AddFont*. Fonts[0] is the default font upon calling ImGui::NewFrame(), use ImGui::PushFont()/PopFont() to change the current font.\n    ImVector<CustomRect>        CustomRects;        // Rectangles for packing custom texture data into the atlas.\n    ImVector<ImFontConfig>      ConfigData;         // Internal data\n    int                         CustomRectIds[1];   // Identifiers of custom texture rectangle used by ImFontAtlas/ImDrawList\n\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    typedef ImFontGlyphRangesBuilder GlyphRangesBuilder; // OBSOLETE 1.67+\n#endif\n};\n\n// Font runtime data and rendering\n// ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32().\nstruct ImFont\n{\n    // Members: Hot ~20/24 bytes (for CalcTextSize)\n    ImVector<float>             IndexAdvanceX;      // 12-16 // out //            // Sparse. Glyphs->AdvanceX in a directly indexable way (cache-friendly for CalcTextSize functions which only this this info, and are often bottleneck in large UI).\n    float                       FallbackAdvanceX;   // 4     // out // = FallbackGlyph->AdvanceX\n    float                       FontSize;           // 4     // in  //            // Height of characters/line, set during loading (don't change after loading)\n\n    // Members: Hot ~36/48 bytes (for CalcTextSize + render loop)\n    ImVector<ImWchar>           IndexLookup;        // 12-16 // out //            // Sparse. Index glyphs by Unicode code-point.\n    ImVector<ImFontGlyph>       Glyphs;             // 12-16 // out //            // All glyphs.\n    const ImFontGlyph*          FallbackGlyph;      // 4-8   // out // = FindGlyph(FontFallbackChar)\n    ImVec2                      DisplayOffset;      // 8     // in  // = (0,0)    // Offset font rendering by xx pixels\n\n    // Members: Cold ~32/40 bytes\n    ImFontAtlas*                ContainerAtlas;     // 4-8   // out //            // What we has been loaded into\n    const ImFontConfig*         ConfigData;         // 4-8   // in  //            // Pointer within ContainerAtlas->ConfigData\n    short                       ConfigDataCount;    // 2     // in  // ~ 1        // Number of ImFontConfig involved in creating this font. Bigger than 1 when merging multiple font sources into one ImFont.\n    ImWchar                     FallbackChar;       // 2     // in  // = '?'      // Replacement glyph if one isn't found. Only set via SetFallbackChar()\n    float                       Scale;              // 4     // in  // = 1.f      // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale()\n    float                       Ascent, Descent;    // 4+4   // out //            // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize]\n    int                         MetricsTotalSurface;// 4     // out //            // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs)\n    bool                        DirtyLookupTables;  // 1     // out //\n\n    // Methods\n    IMGUI_API ImFont();\n    IMGUI_API ~ImFont();\n    IMGUI_API const ImFontGlyph*FindGlyph(ImWchar c) const;\n    IMGUI_API const ImFontGlyph*FindGlyphNoFallback(ImWchar c) const;\n    float                       GetCharAdvance(ImWchar c) const     { return ((int)c < IndexAdvanceX.Size) ? IndexAdvanceX[(int)c] : FallbackAdvanceX; }\n    bool                        IsLoaded() const                    { return ContainerAtlas != NULL; }\n    const char*                 GetDebugName() const                { return ConfigData ? ConfigData->Name : \"<unknown>\"; }\n\n    // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.\n    // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.\n    IMGUI_API ImVec2            CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8\n    IMGUI_API const char*       CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const;\n    IMGUI_API void              RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const;\n    IMGUI_API void              RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false) const;\n\n    // [Internal] Don't use!\n    IMGUI_API void              BuildLookupTable();\n    IMGUI_API void              ClearOutputData();\n    IMGUI_API void              GrowIndex(int new_size);\n    IMGUI_API void              AddGlyph(ImWchar c, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x);\n    IMGUI_API void              AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst = true); // Makes 'dst' character/glyph points to 'src' character/glyph. Currently needs to be called AFTER fonts have been built.\n    IMGUI_API void              SetFallbackChar(ImWchar c);\n\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n    typedef ImFontGlyph Glyph; // OBSOLETE 1.52+\n#endif\n};\n\n//-----------------------------------------------------------------------------\n// [BETA] Platform interface for multi-viewport support\n// - completely optional, for advanced users!\n// - this is used for back-ends aiming to support the seamless creation of multiple viewport (= multiple Platform/OS windows)\n//   dear imgui manages the viewports, and the back-end create one Platform/OS windows for each secondary viewport.\n// - if you are new to dear imgui and trying to integrate it into your engine, you should probably ignore this for now.\n//-----------------------------------------------------------------------------\n\n// (Optional) This is required when enabling multi-viewport. Represent the bounds of each connected monitor/display and their DPI. \n// We use this information for multiple DPI support + clamping the position of popups and tooltips so they don't straddle multiple monitors.\nstruct ImGuiPlatformMonitor\n{\n    ImVec2  MainPos, MainSize;  // Coordinates of the area displayed on this monitor (Min = upper left, Max = bottom right)\n    ImVec2  WorkPos, WorkSize;  // (Optional) Coordinates without task bars / side bars / menu bars. imgui uses this to avoid positioning popups/tooltips inside this region.\n    float   DpiScale;           // 1.0f = 96 DPI\n    ImGuiPlatformMonitor() { MainPos = MainSize = WorkPos = WorkSize = ImVec2(0,0); DpiScale = 1.0f; }\n};\n\n// (Optional) Setup required only if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) is enabled. \n// Access via ImGui::GetPlatformIO(). This is designed so we can mix and match two imgui_impl_xxxx files,\n// one for the Platform (~window handling), one for Renderer. Custom engine back-ends will often provide \n// both Platform and Renderer interfaces and so may not need to use all functions.\n// Platform functions are typically called before their Renderer counterpart, \n// apart from Destroy which are called the other way. \n// RenderPlatformWindowsDefault() is that helper that iterate secondary viewports and call, in this order:\n//   Platform_RenderWindow(), Renderer_RenderWindow(), Platform_SwapBuffers(), Renderer_SwapBuffers()\n// You may skip using RenderPlatformWindowsDefault() and call your draw/swap functions yourself if you need\n// specific behavior for your multi-window rendering.\nstruct ImGuiPlatformIO\n{\n    //------------------------------------------------------------------\n    // Input - Back-end interface/functions + Monitor List\n    //------------------------------------------------------------------\n\n    // (Optional) Platform functions (e.g. Win32, GLFW, SDL2)\n    // Most of them are called by ImGui::UpdatePlatformWindows() and ImGui::RenderPlatformWindowsDefault().\n    void    (*Platform_CreateWindow)(ImGuiViewport* vp);                    // Create a new platform window for the given viewport\n    void    (*Platform_DestroyWindow)(ImGuiViewport* vp);\n    void    (*Platform_ShowWindow)(ImGuiViewport* vp);                      // Newly created windows are initially hidden so SetWindowPos/Size/Title can be called on them first\n    void    (*Platform_SetWindowPos)(ImGuiViewport* vp, ImVec2 pos);\n    ImVec2  (*Platform_GetWindowPos)(ImGuiViewport* vp);\n    void    (*Platform_SetWindowSize)(ImGuiViewport* vp, ImVec2 size);\n    ImVec2  (*Platform_GetWindowSize)(ImGuiViewport* vp);\n    void    (*Platform_SetWindowFocus)(ImGuiViewport* vp);                  // Move window to front and set input focus\n    bool    (*Platform_GetWindowFocus)(ImGuiViewport* vp);\n    bool    (*Platform_GetWindowMinimized)(ImGuiViewport* vp);\n    void    (*Platform_SetWindowTitle)(ImGuiViewport* vp, const char* title);\n    void    (*Platform_SetWindowAlpha)(ImGuiViewport* vp, float alpha);     // (Optional) Setup window transparency\n    void    (*Platform_UpdateWindow)(ImGuiViewport* vp);                    // (Optional) Called in UpdatePlatforms(). Optional hook to allow the platform back-end from doing general book-keeping every frame.\n    void    (*Platform_RenderWindow)(ImGuiViewport* vp, void* render_arg);  // (Optional) Setup for render\n    void    (*Platform_SwapBuffers)(ImGuiViewport* vp, void* render_arg);   // (Optional) Call Present/SwapBuffers (platform side)\n    float   (*Platform_GetWindowDpiScale)(ImGuiViewport* vp);               // (Optional) [BETA] (FIXME-DPI) DPI handling: Return DPI scale for this viewport. 1.0f = 96 DPI.\n    void    (*Platform_OnChangedViewport)(ImGuiViewport* vp);               // (Optional) [BETA] (FIXME-DPI) DPI handling: Called during Begin() every time the viewport we are outputting into changes, so back-end has a chance to swap fonts to adjust style.\n    void    (*Platform_SetImeInputPos)(ImGuiViewport* vp, ImVec2 pos);      // (Optional) Set IME (Input Method Editor, e.g. for Asian languages) input position, so text preview appears over the imgui input box.\n    int     (*Platform_CreateVkSurface)(ImGuiViewport* vp, ImU64 vk_inst, const void* vk_allocators, ImU64* out_vk_surface); // (Optional) For Renderer to call into Platform code\n\n    // (Optional) Renderer functions (e.g. DirectX, OpenGL3, Vulkan)\n    void    (*Renderer_CreateWindow)(ImGuiViewport* vp);                    // Create swap chains, frame buffers etc.\n    void    (*Renderer_DestroyWindow)(ImGuiViewport* vp);\n    void    (*Renderer_SetWindowSize)(ImGuiViewport* vp, ImVec2 size);      // Resize swap chain, frame buffers etc.\n    void    (*Renderer_RenderWindow)(ImGuiViewport* vp, void* render_arg);  // (Optional) Clear targets, Render viewport->DrawData\n    void    (*Renderer_SwapBuffers)(ImGuiViewport* vp, void* render_arg);   // (Optional) Call Present/SwapBuffers (renderer side)\n\n    // (Optional) List of monitors (updated by: app/back-end, used by: imgui to clamp popups/tooltips within same monitor and not have them straddle monitors)\n    ImVector<ImGuiPlatformMonitor>  Monitors;\n\n    //------------------------------------------------------------------\n    // Output - List of viewports to render into platform windows\n    //------------------------------------------------------------------\n\n    // List of viewports (the list is updated by calling ImGui::EndFrame or ImGui::Render)\n    ImGuiViewport*                  MainViewport;                           // Guaranteed to be == Viewports[0]\n    ImVector<ImGuiViewport*>        Viewports;                              // Main viewports, followed by all secondary viewports. \n    ImGuiPlatformIO()               { memset(this, 0, sizeof(*this)); }     // Zero clear\n};\n\n// Flags stored in ImGuiViewport::Flags, giving indications to the platform back-ends.\nenum ImGuiViewportFlags_\n{\n    ImGuiViewportFlags_None                     = 0,\n    ImGuiViewportFlags_NoDecoration             = 1 << 0,   // Platform Window: Disable platform decorations: title bar, borders, etc. (generally set all windows, but if ImGuiConfigFlags_ViewportsDecoration is set we only set this on popups/tooltips)\n    ImGuiViewportFlags_NoTaskBarIcon            = 1 << 1,   // Platform Window: Disable platform task bar icon (generally set on popups/tooltips, or all windows if ImGuiConfigFlags_ViewportsNoTaskBarIcon is set)\n    ImGuiViewportFlags_NoFocusOnAppearing       = 1 << 2,   // Platform Window: Don't take focus when created.\n    ImGuiViewportFlags_NoFocusOnClick           = 1 << 3,   // Platform Window: Don't take focus when clicked on.\n    ImGuiViewportFlags_NoInputs                 = 1 << 4,   // Platform Window: Make mouse pass through so we can drag this window while peaking behind it.\n    ImGuiViewportFlags_NoRendererClear          = 1 << 5,   // Platform Window: Renderer doesn't need to clear the framebuffer ahead (because we will fill it entirely).\n    ImGuiViewportFlags_TopMost                  = 1 << 6,   // Platform Window: Display on top (for tooltips only)\n    ImGuiViewportFlags_Minimized                = 1 << 7    // Platform Window: Window is minimized, can skip render. When minimized we tend to avoid using the viewport pos/size for clipping window or testing if they are contained in the viewport.\n};\n\n// The viewports created and managed by imgui. The role of the platform back-end is to create the platform/OS windows corresponding to each viewport.\nstruct ImGuiViewport\n{\n    ImGuiID             ID;                     // Unique identifier for the viewport\n    ImGuiViewportFlags  Flags;                  // See ImGuiViewportFlags_\n    ImVec2              Pos;                    // Position of viewport both in imgui space and in OS desktop/native space\n    ImVec2              Size;                   // Size of viewport in pixel\n    float               DpiScale;               // 1.0f = 96 DPI = No extra scale\n    ImDrawData*         DrawData;               // The ImDrawData corresponding to this viewport. Valid after Render() and until the next call to NewFrame().\n    ImGuiID             ParentViewportId;       // (Advanced) 0: no parent. Instruct the platform back-end to setup a parent/child relationship between platform windows.\n\n    void*               RendererUserData;       // void* to hold custom data structure for the renderer (e.g. swap chain, frame-buffers etc.)\n    void*               PlatformUserData;       // void* to hold custom data structure for the OS / platform (e.g. windowing info, render context)\n    void*               PlatformHandle;         // void* for FindViewportByPlatformHandle(). (e.g. suggested to use natural platform handle such as HWND, GlfwWindow*, SDL_Window*)\n    bool                PlatformRequestClose;   // Platform window requested closure (e.g. window was moved by the OS / host window manager, e.g. pressing ALT-F4)\n    bool                PlatformRequestMove;    // Platform window requested move (e.g. window was moved by the OS / host window manager, authoritative position will be OS window position)\n    bool                PlatformRequestResize;  // Platform window requested resize (e.g. window was resized by the OS / host window manager, authoritative size will be OS window size)\n\n    ImGuiViewport()     { ID = 0; Flags = 0; DpiScale = 0.0f; DrawData = NULL; ParentViewportId = 0; RendererUserData = PlatformUserData = PlatformHandle = NULL; PlatformRequestClose = PlatformRequestMove = PlatformRequestResize = false; }\n    ~ImGuiViewport()    { IM_ASSERT(PlatformUserData == NULL && RendererUserData == NULL); }\n};\n\n#if defined(__clang__)\n#pragma clang diagnostic pop\n#elif defined(__GNUC__) && __GNUC__ >= 8\n#pragma GCC diagnostic pop\n#endif\n\n// Include imgui_user.h at the end of imgui.h (convenient for user to only explicitly include vanilla imgui.h)\n#ifdef IMGUI_INCLUDE_IMGUI_USER_H\n#include \"imgui_user.hpp\"\n#endif\n\n#pragma warning(pop)"
  },
  {
    "path": "src/imgui/imgui_draw.cpp",
    "content": "// dear imgui, v1.70 WIP\n// (drawing and font code)\n\n/*\n\nIndex of this file:\n\n// [SECTION] STB libraries implementation\n// [SECTION] Style functions\n// [SECTION] ImDrawList\n// [SECTION] ImDrawData\n// [SECTION] Helpers ShadeVertsXXX functions\n// [SECTION] ImFontConfig\n// [SECTION] ImFontAtlas\n// [SECTION] ImFontAtlas glyph ranges helpers\n// [SECTION] ImFontGlyphRangesBuilder\n// [SECTION] ImFont\n// [SECTION] Internal Render Helpers\n// [SECTION] Decompression code\n// [SECTION] Default font data (ProggyClean.ttf)\n\n*/\n\n#pragma warning(push, 0)\n#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)\n#define _CRT_SECURE_NO_WARNINGS\n#endif\n\n#include \"imgui.hpp\"\n#ifndef IMGUI_DEFINE_MATH_OPERATORS\n#define IMGUI_DEFINE_MATH_OPERATORS\n#endif\n#include \"imgui_internal.hpp\"\n\n#include <stdio.h>      // vsnprintf, sscanf, printf\n#if !defined(alloca)\n#if defined(__GLIBC__) || defined(__sun) || defined(__CYGWIN__) || defined(__APPLE__)\n#include <alloca.h>     // alloca (glibc uses <alloca.h>. Note that Cygwin may have _WIN32 defined, so the order matters here)\n#elif defined(_WIN32)\n#include <malloc.h>     // alloca\n#if !defined(alloca)\n#define alloca _alloca  // for clang with MS Codegen\n#endif\n#else\n#include <stdlib.h>     // alloca\n#endif\n#endif\n\n// Visual Studio warnings\n#ifdef _MSC_VER\n#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)\n#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen\n#endif\n\n// Clang/GCC warnings with -Weverything\n#ifdef __clang__\n#pragma clang diagnostic ignored \"-Wold-style-cast\"         // warning : use of old-style cast                              // yes, they are more terse.\n#pragma clang diagnostic ignored \"-Wfloat-equal\"            // warning : comparing floating point with == or != is unsafe   // storing and comparing against same constants ok.\n#pragma clang diagnostic ignored \"-Wglobal-constructors\"    // warning : declaration requires a global destructor           // similar to above, not sure what the exact difference is.\n#pragma clang diagnostic ignored \"-Wsign-conversion\"        // warning : implicit conversion changes signedness             //\n#if __has_warning(\"-Wzero-as-null-pointer-constant\")\n#pragma clang diagnostic ignored \"-Wzero-as-null-pointer-constant\"  // warning : zero as null pointer constant              // some standard header variations use #define NULL 0\n#endif\n#if __has_warning(\"-Wcomma\")\n#pragma clang diagnostic ignored \"-Wcomma\"                  // warning : possible misuse of comma operator here             //\n#endif\n#if __has_warning(\"-Wreserved-id-macro\")\n#pragma clang diagnostic ignored \"-Wreserved-id-macro\"      // warning : macro name is a reserved identifier                //\n#endif\n#if __has_warning(\"-Wdouble-promotion\")\n#pragma clang diagnostic ignored \"-Wdouble-promotion\"       // warning: implicit conversion from 'float' to 'double' when passing argument to function  // using printf() is a misery with this as C++ va_arg ellipsis changes float to double.\n#endif\n#elif defined(__GNUC__)\n#pragma GCC diagnostic ignored \"-Wunused-function\"          // warning: 'xxxx' defined but not used\n#pragma GCC diagnostic ignored \"-Wdouble-promotion\"         // warning: implicit conversion from 'float' to 'double' when passing argument to function\n#pragma GCC diagnostic ignored \"-Wconversion\"               // warning: conversion to 'xxxx' from 'xxxx' may alter its value\n#pragma GCC diagnostic ignored \"-Wstack-protector\"          // warning: stack protector not protecting local variables: variable length buffer\n#if __GNUC__ >= 8\n#pragma GCC diagnostic ignored \"-Wclass-memaccess\"          // warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead\n#endif\n#endif\n\n//-------------------------------------------------------------------------\n// [SECTION] STB libraries implementation\n//-------------------------------------------------------------------------\n\n// Compile time options:\n//#define IMGUI_STB_NAMESPACE           ImStb\n//#define IMGUI_STB_TRUETYPE_FILENAME   \"my_folder/stb_truetype.h\"\n//#define IMGUI_STB_RECT_PACK_FILENAME  \"my_folder/stb_rect_pack.h\"\n//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION\n//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION\n\n#ifdef IMGUI_STB_NAMESPACE\nnamespace IMGUI_STB_NAMESPACE\n{\n#endif\n\n#ifdef _MSC_VER\n#pragma warning (push)\n#pragma warning (disable: 4456)                             // declaration of 'xx' hides previous local declaration\n#endif\n\n#ifdef __clang__\n#pragma clang diagnostic push\n#pragma clang diagnostic ignored \"-Wunused-function\"\n#pragma clang diagnostic ignored \"-Wmissing-prototypes\"\n#pragma clang diagnostic ignored \"-Wimplicit-fallthrough\"\n#pragma clang diagnostic ignored \"-Wcast-qual\"              // warning : cast from 'const xxxx *' to 'xxx *' drops const qualifier //\n#endif\n\n#ifdef __GNUC__\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wtype-limits\"              // warning: comparison is always true due to limited range of data type [-Wtype-limits]\n#pragma GCC diagnostic ignored \"-Wcast-qual\"                // warning: cast from type 'const xxxx *' to type 'xxxx *' casts away qualifiers\n#endif\n\n#ifndef STB_RECT_PACK_IMPLEMENTATION                        // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds)\n#ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION\n#define STBRP_STATIC\n#define STBRP_ASSERT(x)     IM_ASSERT(x)\n#define STBRP_SORT          ImQsort\n#define STB_RECT_PACK_IMPLEMENTATION\n#endif\n#ifdef IMGUI_STB_RECT_PACK_FILENAME\n#include IMGUI_STB_RECT_PACK_FILENAME\n#else\n#include \"imstb_rectpack.hpp\"\n#endif\n#endif\n\n#ifndef STB_TRUETYPE_IMPLEMENTATION                         // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds)\n#ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION\n#define STBTT_malloc(x,u)   ((void)(u), IM_ALLOC(x))\n#define STBTT_free(x,u)     ((void)(u), IM_FREE(x))\n#define STBTT_assert(x)     IM_ASSERT(x)\n#define STBTT_fmod(x,y)     ImFmod(x,y)\n#define STBTT_sqrt(x)       ImSqrt(x)\n#define STBTT_pow(x,y)      ImPow(x,y)\n#define STBTT_fabs(x)       ImFabs(x)\n#define STBTT_ifloor(x)     ((int)ImFloorStd(x))\n#define STBTT_iceil(x)      ((int)ImCeil(x))\n#define STBTT_STATIC\n#define STB_TRUETYPE_IMPLEMENTATION\n#else\n#define STBTT_DEF extern\n#endif\n#ifdef IMGUI_STB_TRUETYPE_FILENAME\n#include IMGUI_STB_TRUETYPE_FILENAME\n#else\n#include \"imstb_truetype.hpp\"\n#endif\n#endif\n\n#ifdef __GNUC__\n#pragma GCC diagnostic pop\n#endif\n\n#ifdef __clang__\n#pragma clang diagnostic pop\n#endif\n\n#ifdef _MSC_VER\n#pragma warning (pop)\n#endif\n\n#ifdef IMGUI_STB_NAMESPACE\n} // namespace ImStb\nusing namespace IMGUI_STB_NAMESPACE;\n#endif\n\n//-----------------------------------------------------------------------------\n// [SECTION] Style functions\n//-----------------------------------------------------------------------------\n\nvoid ImGui::StyleColorsDark(ImGuiStyle* dst)\n{\n    ImGuiStyle* style = dst ? dst : &ImGui::GetStyle();\n    ImVec4* colors = style->Colors;\n\n    colors[ImGuiCol_Text]                   = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);\n    colors[ImGuiCol_TextDisabled]           = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);\n    colors[ImGuiCol_WindowBg]               = ImVec4(0.06f, 0.06f, 0.06f, 0.94f);\n    colors[ImGuiCol_ChildBg]                = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n    colors[ImGuiCol_PopupBg]                = ImVec4(0.08f, 0.08f, 0.08f, 0.94f);\n    colors[ImGuiCol_Border]                 = ImVec4(0.43f, 0.43f, 0.50f, 0.50f);\n    colors[ImGuiCol_BorderShadow]           = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n    colors[ImGuiCol_FrameBg]                = ImVec4(0.16f, 0.29f, 0.48f, 0.54f);\n    colors[ImGuiCol_FrameBgHovered]         = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);\n    colors[ImGuiCol_FrameBgActive]          = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);\n    colors[ImGuiCol_TitleBg]                = ImVec4(0.04f, 0.04f, 0.04f, 1.00f);\n    colors[ImGuiCol_TitleBgActive]          = ImVec4(0.16f, 0.29f, 0.48f, 1.00f);\n    colors[ImGuiCol_TitleBgCollapsed]       = ImVec4(0.00f, 0.00f, 0.00f, 0.51f);\n    colors[ImGuiCol_MenuBarBg]              = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);\n    colors[ImGuiCol_ScrollbarBg]            = ImVec4(0.02f, 0.02f, 0.02f, 0.53f);\n    colors[ImGuiCol_ScrollbarGrab]          = ImVec4(0.31f, 0.31f, 0.31f, 1.00f);\n    colors[ImGuiCol_ScrollbarGrabHovered]   = ImVec4(0.41f, 0.41f, 0.41f, 1.00f);\n    colors[ImGuiCol_ScrollbarGrabActive]    = ImVec4(0.51f, 0.51f, 0.51f, 1.00f);\n    colors[ImGuiCol_CheckMark]              = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n    colors[ImGuiCol_SliderGrab]             = ImVec4(0.24f, 0.52f, 0.88f, 1.00f);\n    colors[ImGuiCol_SliderGrabActive]       = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n    colors[ImGuiCol_Button]                 = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);\n    colors[ImGuiCol_ButtonHovered]          = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n    colors[ImGuiCol_ButtonActive]           = ImVec4(0.06f, 0.53f, 0.98f, 1.00f);\n    colors[ImGuiCol_Header]                 = ImVec4(0.26f, 0.59f, 0.98f, 0.31f);\n    colors[ImGuiCol_HeaderHovered]          = ImVec4(0.26f, 0.59f, 0.98f, 0.80f);\n    colors[ImGuiCol_HeaderActive]           = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n    colors[ImGuiCol_Separator]              = colors[ImGuiCol_Border];\n    colors[ImGuiCol_SeparatorHovered]       = ImVec4(0.10f, 0.40f, 0.75f, 0.78f);\n    colors[ImGuiCol_SeparatorActive]        = ImVec4(0.10f, 0.40f, 0.75f, 1.00f);\n    colors[ImGuiCol_ResizeGrip]             = ImVec4(0.26f, 0.59f, 0.98f, 0.25f);\n    colors[ImGuiCol_ResizeGripHovered]      = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);\n    colors[ImGuiCol_ResizeGripActive]       = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);\n    colors[ImGuiCol_Tab]                    = ImLerp(colors[ImGuiCol_Header],       colors[ImGuiCol_TitleBgActive], 0.80f);\n    colors[ImGuiCol_TabHovered]             = colors[ImGuiCol_HeaderHovered];\n    colors[ImGuiCol_TabActive]              = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f);\n    colors[ImGuiCol_TabUnfocused]           = ImLerp(colors[ImGuiCol_Tab],          colors[ImGuiCol_TitleBg], 0.80f);\n    colors[ImGuiCol_TabUnfocusedActive]     = ImLerp(colors[ImGuiCol_TabActive],    colors[ImGuiCol_TitleBg], 0.40f);\n    colors[ImGuiCol_DockingPreview]         = colors[ImGuiCol_HeaderActive] * ImVec4(1.0f, 1.0f, 1.0f, 0.7f);\n    colors[ImGuiCol_DockingEmptyBg]         = ImVec4(0.20f, 0.20f, 0.20f, 1.00f);\n    colors[ImGuiCol_PlotLines]              = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);\n    colors[ImGuiCol_PlotLinesHovered]       = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);\n    colors[ImGuiCol_PlotHistogram]          = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);\n    colors[ImGuiCol_PlotHistogramHovered]   = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);\n    colors[ImGuiCol_TextSelectedBg]         = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);\n    colors[ImGuiCol_DragDropTarget]         = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);\n    colors[ImGuiCol_NavHighlight]           = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n    colors[ImGuiCol_NavWindowingHighlight]  = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);\n    colors[ImGuiCol_NavWindowingDimBg]      = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);\n    colors[ImGuiCol_ModalWindowDimBg]       = ImVec4(0.80f, 0.80f, 0.80f, 0.35f);\n}\n\nvoid ImGui::StyleColorsCherry(ImGuiStyle* dst)\n{\n\tImGuiStyle* style = dst ? dst : &ImGui::GetStyle();\n\tImVec4* colors = style->Colors;\n\n\t// cherry colors, 3 intensities\n#define HI(v)   ImVec4(0.502f, 0.075f, 0.256f, v)\n#define XHI(v)   ImVec4(0.802f, 0.075f, 0.256f, v)\n#define MED(v)  ImVec4(0.455f, 0.198f, 0.301f, v)\n#define LOW(v)  ImVec4(0.232f, 0.201f, 0.271f, v)\n// backgrounds (@todo: complete with BG_MED, BG_LOW)\n#define BG(v)   ImVec4(0.200f, 0.220f, 0.270f, v)\n// text\n#define TEXT(v) ImVec4(0.860f, 0.930f, 0.890f, v)\n\n\tcolors[ImGuiCol_Tab] = HI(1.0f);\n\tcolors[ImGuiCol_TabActive] = XHI(0.50f);\n\tcolors[ImGuiCol_TabHovered] = XHI(1.0f);\n\tcolors[ImGuiCol_TabUnfocused] = LOW(1.00f);\n\tcolors[ImGuiCol_TabUnfocusedActive] = MED(0.60f);\n\n\tcolors[ImGuiCol_DockingPreview] = HI(1.00f);\n\tcolors[ImGuiCol_DragDropTarget] = BG(1.00f);\n\tcolors[ImGuiCol_DockingEmptyBg] = BG(1.00f);\n\n\tcolors[ImGuiCol_Text] = TEXT(0.78f);\n\tcolors[ImGuiCol_TextDisabled] = TEXT(0.28f);\n\tcolors[ImGuiCol_WindowBg] = ImVec4(0.13f, 0.14f, 0.17f, 1.00f);\n\tcolors[ImGuiCol_ChildWindowBg] = BG(0.58f);\n\tcolors[ImGuiCol_ChildBg] = ImVec4(0, 0, 0, 0);\n\tcolors[ImGuiCol_PopupBg] = BG(0.9f);\n\tcolors[ImGuiCol_Border] = ImVec4(0.31f, 0.31f, 1.00f, 0.00f);\n\tcolors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n\n\tcolors[ImGuiCol_FrameBg] = BG(1.00f);\n\tcolors[ImGuiCol_FrameBgHovered] = MED(0.78f);\n\tcolors[ImGuiCol_FrameBgActive] = MED(1.00f);\n\tcolors[ImGuiCol_TitleBg] = LOW(1.00f);\n\tcolors[ImGuiCol_TitleBgActive] = HI(1.00f);\n\tcolors[ImGuiCol_TitleBgCollapsed] = BG(0.75f);\n\tcolors[ImGuiCol_MenuBarBg] = BG(0.47f);\n\tcolors[ImGuiCol_ScrollbarBg] = BG(1.00f);\n\tcolors[ImGuiCol_ScrollbarGrab] = ImVec4(0.09f, 0.15f, 0.16f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarGrabHovered] = MED(0.78f);\n\tcolors[ImGuiCol_ScrollbarGrabActive] = MED(1.00f);\n\tcolors[ImGuiCol_CheckMark] = ImVec4(0.71f, 0.22f, 0.27f, 1.00f);\n\tcolors[ImGuiCol_SliderGrab] = ImVec4(0.47f, 0.77f, 0.83f, 0.14f);\n\tcolors[ImGuiCol_SliderGrabActive] = ImVec4(0.71f, 0.22f, 0.27f, 1.00f);\n\tcolors[ImGuiCol_Button] = ImVec4(0.47f, 0.77f, 0.83f, 0.14f);\n\tcolors[ImGuiCol_ButtonHovered] = MED(0.86f);\n\tcolors[ImGuiCol_ButtonActive] = MED(1.00f);\n\tcolors[ImGuiCol_Header] = MED(0.76f);\n\tcolors[ImGuiCol_HeaderHovered] = MED(0.86f);\n\tcolors[ImGuiCol_HeaderActive] = HI(1.00f);\n\tcolors[ImGuiCol_Column] = ImVec4(0.14f, 0.16f, 0.19f, 1.00f);\n\tcolors[ImGuiCol_ColumnHovered] = MED(0.78f);\n\tcolors[ImGuiCol_ColumnActive] = MED(1.00f);\n\tcolors[ImGuiCol_ResizeGrip] = ImVec4(0.47f, 0.77f, 0.83f, 0.04f);\n\tcolors[ImGuiCol_ResizeGripHovered] = MED(0.78f);\n\tcolors[ImGuiCol_ResizeGripActive] = MED(1.00f);\n\tcolors[ImGuiCol_PlotLines] = TEXT(0.63f);\n\tcolors[ImGuiCol_PlotLinesHovered] = MED(1.00f);\n\tcolors[ImGuiCol_PlotHistogram] = TEXT(0.63f);\n\tcolors[ImGuiCol_PlotHistogramHovered] = MED(1.00f);\n\tcolors[ImGuiCol_TextSelectedBg] = MED(0.43f);\n\t// [...]\n\tcolors[ImGuiCol_ModalWindowDarkening] = BG(1.73f);\n\tcolors[ImGuiCol_Border] = ImVec4(0.539f, 0.479f, 0.255f, 0.162f);\n\n\tstyle->TabRounding = 0.0f;\n\tstyle->TabBorderSize = 0.0f;\n\n\tstyle->WindowPadding = ImVec2(6, 4);\n\tstyle->WindowRounding = 0.0f;\n\tstyle->FramePadding = ImVec2(10, 4);\n\tstyle->FrameRounding = 3.0f;\n\tstyle->TouchExtraPadding = ImVec2(0, 0);\n\tstyle->IndentSpacing = 6.0f;\n\tstyle->ScrollbarSize = 12.0f;\n\tstyle->ScrollbarRounding = 16.0f;\n\tstyle->GrabMinSize = 20.0f;\n\tstyle->GrabRounding = 2.0f;\n\n\tstyle->ItemInnerSpacing = ImVec2(10, 1);\n\tstyle->ItemSpacing.x = 10;\n\tstyle->ItemSpacing.y = 4;\n\tstyle->IndentSpacing = 22;\n\n\tstyle->WindowTitleAlign.x = 0.50f;\n\n\tstyle->FrameBorderSize = 0.0f;\n\tstyle->WindowBorderSize = 1.0f;\n}\n\nvoid ImGui::StyleColorsUE(ImGuiStyle* dst)\n{\n\tImGuiStyle* style = dst ? dst : &ImGui::GetStyle();\n\tImVec4* colors = style->Colors;\n\n\tcolors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);\n\tcolors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);\n\tcolors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f);\n\tcolors[ImGuiCol_ChildBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.00f);\n\tcolors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f);\n\tcolors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f);\n\tcolors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n\tcolors[ImGuiCol_FrameBg] = ImVec4(0.20f, 0.21f, 0.22f, 0.54f);\n\tcolors[ImGuiCol_FrameBgHovered] = ImVec4(0.40f, 0.40f, 0.40f, 0.40f);\n\tcolors[ImGuiCol_FrameBgActive] = ImVec4(0.18f, 0.18f, 0.18f, 0.67f);\n\tcolors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f);\n\tcolors[ImGuiCol_TitleBgActive] = ImVec4(0.29f, 0.29f, 0.29f, 1.00f);\n\tcolors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f);\n\tcolors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f);\n\tcolors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f);\n\tcolors[ImGuiCol_CheckMark] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f);\n\tcolors[ImGuiCol_SliderGrab] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f);\n\tcolors[ImGuiCol_SliderGrabActive] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);\n\tcolors[ImGuiCol_Button] = ImVec4(0.44f, 0.44f, 0.44f, 0.40f);\n\tcolors[ImGuiCol_ButtonHovered] = ImVec4(0.46f, 0.47f, 0.48f, 1.00f);\n\tcolors[ImGuiCol_ButtonActive] = ImVec4(0.42f, 0.42f, 0.42f, 1.00f);\n\tcolors[ImGuiCol_Header] = ImVec4(0.70f, 0.70f, 0.70f, 0.31f);\n\tcolors[ImGuiCol_HeaderHovered] = ImVec4(0.70f, 0.70f, 0.70f, 0.80f);\n\tcolors[ImGuiCol_HeaderActive] = ImVec4(0.48f, 0.50f, 0.52f, 1.00f);\n\tcolors[ImGuiCol_Separator] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f);\n\tcolors[ImGuiCol_SeparatorHovered] = ImVec4(0.72f, 0.72f, 0.72f, 0.78f);\n\tcolors[ImGuiCol_SeparatorActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f);\n\tcolors[ImGuiCol_ResizeGrip] = ImVec4(0.91f, 0.91f, 0.91f, 0.25f);\n\tcolors[ImGuiCol_ResizeGripHovered] = ImVec4(0.81f, 0.81f, 0.81f, 0.67f);\n\tcolors[ImGuiCol_ResizeGripActive] = ImVec4(0.46f, 0.46f, 0.46f, 0.95f);\n\tcolors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);\n\tcolors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);\n\tcolors[ImGuiCol_PlotHistogram] = ImVec4(0.73f, 0.60f, 0.15f, 1.00f);\n\tcolors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);\n\tcolors[ImGuiCol_TextSelectedBg] = ImVec4(0.87f, 0.87f, 0.87f, 0.35f);\n\tcolors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f);\n\tcolors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);\n\tcolors[ImGuiCol_NavHighlight] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);\n\tcolors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);\n}\n\nIMGUI_API void ImGui::StyleCorporateGrey(ImGuiStyle* dst)\n{\n\tImGuiStyle& style = ImGui::GetStyle();\n\tImVec4* colors = style.Colors;\n\n\t/// 0 = FLAT APPEARENCE\n\t/// 1 = MORE \"3D\" LOOK\n\tint is3D = 1;\n\n\tcolors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);\n\tcolors[ImGuiCol_TextDisabled] = ImVec4(0.40f, 0.40f, 0.40f, 1.00f);\n\tcolors[ImGuiCol_ChildBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);\n\tcolors[ImGuiCol_WindowBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);\n\tcolors[ImGuiCol_PopupBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);\n\tcolors[ImGuiCol_Border] = ImVec4(0.12f, 0.12f, 0.12f, 0.71f);\n\tcolors[ImGuiCol_BorderShadow] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f);\n\tcolors[ImGuiCol_FrameBg] = ImVec4(0.42f, 0.42f, 0.42f, 0.54f);\n\tcolors[ImGuiCol_FrameBgHovered] = ImVec4(0.42f, 0.42f, 0.42f, 0.40f);\n\tcolors[ImGuiCol_FrameBgActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.67f);\n\tcolors[ImGuiCol_TitleBg] = ImVec4(0.19f, 0.19f, 0.19f, 1.00f);\n\tcolors[ImGuiCol_TitleBgActive] = ImVec4(0.22f, 0.22f, 0.22f, 1.00f);\n\tcolors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.17f, 0.17f, 0.17f, 0.90f);\n\tcolors[ImGuiCol_MenuBarBg] = ImVec4(0.335f, 0.335f, 0.335f, 1.000f);\n\tcolors[ImGuiCol_ScrollbarBg] = ImVec4(0.24f, 0.24f, 0.24f, 0.53f);\n\tcolors[ImGuiCol_ScrollbarGrab] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.52f, 0.52f, 0.52f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.76f, 0.76f, 0.76f, 1.00f);\n\tcolors[ImGuiCol_CheckMark] = ImVec4(0.65f, 0.65f, 0.65f, 1.00f);\n\tcolors[ImGuiCol_SliderGrab] = ImVec4(0.52f, 0.52f, 0.52f, 1.00f);\n\tcolors[ImGuiCol_SliderGrabActive] = ImVec4(0.64f, 0.64f, 0.64f, 1.00f);\n\tcolors[ImGuiCol_Button] = ImVec4(0.54f, 0.54f, 0.54f, 0.35f);\n\tcolors[ImGuiCol_ButtonHovered] = ImVec4(0.52f, 0.52f, 0.52f, 0.59f);\n\tcolors[ImGuiCol_ButtonActive] = ImVec4(0.76f, 0.76f, 0.76f, 1.00f);\n\tcolors[ImGuiCol_Header] = ImVec4(0.38f, 0.38f, 0.38f, 1.00f);\n\tcolors[ImGuiCol_HeaderHovered] = ImVec4(0.47f, 0.47f, 0.47f, 1.00f);\n\tcolors[ImGuiCol_HeaderActive] = ImVec4(0.76f, 0.76f, 0.76f, 0.77f);\n\tcolors[ImGuiCol_Separator] = ImVec4(0.000f, 0.000f, 0.000f, 0.137f);\n\tcolors[ImGuiCol_SeparatorHovered] = ImVec4(0.700f, 0.671f, 0.600f, 0.290f);\n\tcolors[ImGuiCol_SeparatorActive] = ImVec4(0.702f, 0.671f, 0.600f, 0.674f);\n\tcolors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.25f);\n\tcolors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);\n\tcolors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);\n\tcolors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);\n\tcolors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);\n\tcolors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);\n\tcolors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);\n\tcolors[ImGuiCol_TextSelectedBg] = ImVec4(0.73f, 0.73f, 0.73f, 0.35f);\n\tcolors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f);\n\tcolors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);\n\tcolors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n\tcolors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);\n\tcolors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);\n\n\tstyle.PopupRounding = 3;\n\n\tstyle.WindowPadding = ImVec2(4, 4);\n\tstyle.FramePadding = ImVec2(6, 4);\n\tstyle.ItemSpacing = ImVec2(6, 2);\n\n\tstyle.ScrollbarSize = 18;\n\n\tstyle.WindowBorderSize = 1;\n\tstyle.ChildBorderSize = 1;\n\tstyle.PopupBorderSize = 1;\n\tstyle.FrameBorderSize = is3D;\n\n\tstyle.WindowRounding = 3;\n\tstyle.ChildRounding = 3;\n\tstyle.FrameRounding = 3;\n\tstyle.ScrollbarRounding = 2;\n\tstyle.GrabRounding = 3;\n\n#ifdef IMGUI_HAS_DOCK \n\tstyle.TabBorderSize = is3D;\n\tstyle.TabRounding = 3;\n\n\tcolors[ImGuiCol_DockingEmptyBg] = ImVec4(0.38f, 0.38f, 0.38f, 1.00f);\n\tcolors[ImGuiCol_Tab] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);\n\tcolors[ImGuiCol_TabHovered] = ImVec4(0.40f, 0.40f, 0.40f, 1.00f);\n\tcolors[ImGuiCol_TabActive] = ImVec4(0.33f, 0.33f, 0.33f, 1.00f);\n\tcolors[ImGuiCol_TabUnfocused] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);\n\tcolors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.33f, 0.33f, 0.33f, 1.00f);\n\tcolors[ImGuiCol_DockingPreview] = ImVec4(0.85f, 0.85f, 0.85f, 0.28f);\n\n\tif (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)\n\t{\n\t\tstyle.WindowRounding = 0.0f;\n\t\tstyle.Colors[ImGuiCol_WindowBg].w = 1.0f;\n\t}\n#endif\n}\n\nvoid ImGui::StyleColorsDarkCodz1(ImGuiStyle* dst)\n{\n\tImGuiStyle* style = dst ? dst : &ImGui::GetStyle();\n\tImVec4* colors = style->Colors;\n\n\tstyle->FrameBorderSize = 1.0f;\n\tstyle->FramePadding = ImVec2(4.0f, 2.0f);\n\tstyle->ItemSpacing = ImVec2(8.0f, 2.0f);\n\tstyle->WindowBorderSize = 1.0f;\n\tstyle->TabBorderSize = 1.0f;\n\tstyle->WindowRounding = 1.0f;\n\tstyle->ChildRounding = 1.0f;\n\tstyle->FrameRounding = 1.0f;\n\tstyle->ScrollbarRounding = 1.0f;\n\tstyle->GrabRounding = 1.0f;\n\tstyle->TabRounding = 1.0f;\n\n\t// Setup style\n\tcolors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 0.95f);\n\tcolors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);\n\tcolors[ImGuiCol_WindowBg] = ImVec4(0.13f, 0.12f, 0.12f, 1.00f);\n\tcolors[ImGuiCol_ChildBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.00f);\n\tcolors[ImGuiCol_PopupBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.94f);\n\tcolors[ImGuiCol_Border] = ImVec4(0.53f, 0.53f, 0.53f, 0.46f);\n\tcolors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n\tcolors[ImGuiCol_FrameBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.85f);\n\tcolors[ImGuiCol_FrameBgHovered] = ImVec4(0.22f, 0.22f, 0.22f, 0.40f);\n\tcolors[ImGuiCol_FrameBgActive] = ImVec4(0.16f, 0.16f, 0.16f, 0.53f);\n\tcolors[ImGuiCol_TitleBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);\n\tcolors[ImGuiCol_TitleBgActive] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);\n\tcolors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f);\n\tcolors[ImGuiCol_MenuBarBg] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f);\n\tcolors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.48f, 0.48f, 0.48f, 1.00f);\n\tcolors[ImGuiCol_CheckMark] = ImVec4(0.79f, 0.79f, 0.79f, 1.00f);\n\tcolors[ImGuiCol_SliderGrab] = ImVec4(0.48f, 0.47f, 0.47f, 0.91f);\n\tcolors[ImGuiCol_SliderGrabActive] = ImVec4(0.56f, 0.55f, 0.55f, 0.62f);\n\tcolors[ImGuiCol_Button] = ImVec4(0.50f, 0.50f, 0.50f, 0.63f);\n\tcolors[ImGuiCol_ButtonHovered] = ImVec4(0.67f, 0.67f, 0.68f, 0.63f);\n\tcolors[ImGuiCol_ButtonActive] = ImVec4(0.26f, 0.26f, 0.26f, 0.63f);\n\tcolors[ImGuiCol_Header] = ImVec4(0.54f, 0.54f, 0.54f, 0.58f);\n\tcolors[ImGuiCol_HeaderHovered] = ImVec4(0.64f, 0.65f, 0.65f, 0.80f);\n\tcolors[ImGuiCol_HeaderActive] = ImVec4(0.25f, 0.25f, 0.25f, 0.80f);\n\tcolors[ImGuiCol_Separator] = ImVec4(0.58f, 0.58f, 0.58f, 0.50f);\n\tcolors[ImGuiCol_SeparatorHovered] = ImVec4(0.81f, 0.81f, 0.81f, 0.64f);\n\tcolors[ImGuiCol_SeparatorActive] = ImVec4(0.81f, 0.81f, 0.81f, 0.64f);\n\tcolors[ImGuiCol_ResizeGrip] = ImVec4(0.87f, 0.87f, 0.87f, 0.53f);\n\tcolors[ImGuiCol_ResizeGripHovered] = ImVec4(0.87f, 0.87f, 0.87f, 0.74f);\n\tcolors[ImGuiCol_ResizeGripActive] = ImVec4(0.87f, 0.87f, 0.87f, 0.74f);\n\tcolors[ImGuiCol_Tab] = ImVec4(0.01f, 0.01f, 0.01f, 0.86f);\n\tcolors[ImGuiCol_TabHovered] = ImVec4(0.29f, 0.29f, 0.29f, 1.00f);\n\tcolors[ImGuiCol_TabActive] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f);\n\tcolors[ImGuiCol_TabUnfocused] = ImVec4(0.02f, 0.02f, 0.02f, 1.00f);\n\tcolors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.19f, 0.19f, 0.19f, 1.00f);\n\tcolors[ImGuiCol_DockingPreview] = ImVec4(0.38f, 0.48f, 0.60f, 1.00f);\n\tcolors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f);\n\tcolors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);\n\tcolors[ImGuiCol_PlotLinesHovered] = ImVec4(0.68f, 0.68f, 0.68f, 1.00f);\n\tcolors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.77f, 0.33f, 1.00f);\n\tcolors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.87f, 0.55f, 0.08f, 1.00f);\n\tcolors[ImGuiCol_TextSelectedBg] = ImVec4(0.47f, 0.60f, 0.76f, 0.47f);\n\tcolors[ImGuiCol_DragDropTarget] = ImVec4(0.58f, 0.58f, 0.58f, 0.90f);\n\tcolors[ImGuiCol_NavHighlight] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);\n\tcolors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);\n\tcolors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);\n\tcolors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f);\n}\n\nvoid ImGui::StyleColorsLightGreen(ImGuiStyle* dst)\n{\n\tImGuiStyle* style = dst ? dst : &ImGui::GetStyle();\n\tImVec4* colors = style->Colors;\n\n\tstyle->WindowRounding = 2.0f;             // Radius of window corners rounding. Set to 0.0f to have rectangular windows\n\tstyle->ScrollbarRounding = 3.0f;             // Radius of grab corners rounding for scrollbar\n\tstyle->GrabRounding = 2.0f;             // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.\n\tstyle->AntiAliasedLines = true;\n\tstyle->AntiAliasedFill = true;\n\tstyle->WindowRounding = 2;\n\tstyle->ChildRounding = 2;\n\tstyle->ScrollbarSize = 16;\n\tstyle->ScrollbarRounding = 3;\n\tstyle->GrabRounding = 2;\n\tstyle->ItemSpacing.x = 10;\n\tstyle->ItemSpacing.y = 4;\n\tstyle->IndentSpacing = 22;\n\tstyle->FramePadding.x = 6;\n\tstyle->FramePadding.y = 4;\n\tstyle->Alpha = 1.0f;\n\tstyle->FrameRounding = 3.0f;\n\n\tcolors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);\n\tcolors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);\n\tcolors[ImGuiCol_WindowBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);\n\t//colors[ImGuiCol_ChildWindowBg]         = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n\tcolors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n\tcolors[ImGuiCol_PopupBg] = ImVec4(0.93f, 0.93f, 0.93f, 0.98f);\n\tcolors[ImGuiCol_Border] = ImVec4(0.71f, 0.71f, 0.71f, 0.08f);\n\tcolors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.04f);\n\tcolors[ImGuiCol_FrameBg] = ImVec4(0.71f, 0.71f, 0.71f, 0.55f);\n\tcolors[ImGuiCol_FrameBgHovered] = ImVec4(0.94f, 0.94f, 0.94f, 0.55f);\n\tcolors[ImGuiCol_FrameBgActive] = ImVec4(0.71f, 0.78f, 0.69f, 0.98f);\n\tcolors[ImGuiCol_TitleBg] = ImVec4(0.85f, 0.85f, 0.85f, 1.00f);\n\tcolors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.82f, 0.78f, 0.78f, 0.51f);\n\tcolors[ImGuiCol_TitleBgActive] = ImVec4(0.78f, 0.78f, 0.78f, 1.00f);\n\tcolors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);\n\tcolors[ImGuiCol_ScrollbarBg] = ImVec4(0.20f, 0.25f, 0.30f, 0.61f);\n\tcolors[ImGuiCol_ScrollbarGrab] = ImVec4(0.90f, 0.90f, 0.90f, 0.30f);\n\tcolors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.92f, 0.92f, 0.92f, 0.78f);\n\tcolors[ImGuiCol_ScrollbarGrabActive] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);\n\tcolors[ImGuiCol_CheckMark] = ImVec4(0.184f, 0.407f, 0.193f, 1.00f);\n\tcolors[ImGuiCol_SliderGrab] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);\n\tcolors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n\tcolors[ImGuiCol_Button] = ImVec4(0.71f, 0.78f, 0.69f, 0.40f);\n\tcolors[ImGuiCol_ButtonHovered] = ImVec4(0.725f, 0.805f, 0.702f, 1.00f);\n\tcolors[ImGuiCol_ButtonActive] = ImVec4(0.793f, 0.900f, 0.836f, 1.00f);\n\tcolors[ImGuiCol_Header] = ImVec4(0.71f, 0.78f, 0.69f, 0.31f);\n\tcolors[ImGuiCol_HeaderHovered] = ImVec4(0.71f, 0.78f, 0.69f, 0.80f);\n\tcolors[ImGuiCol_HeaderActive] = ImVec4(0.71f, 0.78f, 0.69f, 1.00f);\n\tcolors[ImGuiCol_Column] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);\n\tcolors[ImGuiCol_ColumnHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);\n\tcolors[ImGuiCol_ColumnActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n\tcolors[ImGuiCol_Separator] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);\n\tcolors[ImGuiCol_SeparatorHovered] = ImVec4(0.14f, 0.44f, 0.80f, 0.78f);\n\tcolors[ImGuiCol_SeparatorActive] = ImVec4(0.14f, 0.44f, 0.80f, 1.00f);\n\tcolors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.00f);\n\tcolors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.45f);\n\tcolors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);\n\tcolors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);\n\tcolors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);\n\tcolors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);\n\tcolors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);\n\tcolors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);\n\tcolors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);\n\tcolors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);\n\tcolors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered];\n\tcolors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.70f, 0.70f, 0.70f, 0.70f);\n}\n\nvoid ImGui::StyleColorsClassic(ImGuiStyle* dst)\n{\n    ImGuiStyle* style = dst ? dst : &ImGui::GetStyle();\n    ImVec4* colors = style->Colors;\n\n    colors[ImGuiCol_Text]                   = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);\n    colors[ImGuiCol_TextDisabled]           = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);\n    colors[ImGuiCol_WindowBg]               = ImVec4(0.00f, 0.00f, 0.00f, 0.70f);\n    colors[ImGuiCol_ChildBg]                = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n    colors[ImGuiCol_PopupBg]                = ImVec4(0.11f, 0.11f, 0.14f, 0.92f);\n    colors[ImGuiCol_Border]                 = ImVec4(0.50f, 0.50f, 0.50f, 0.50f);\n    colors[ImGuiCol_BorderShadow]           = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n    colors[ImGuiCol_FrameBg]                = ImVec4(0.43f, 0.43f, 0.43f, 0.39f);\n    colors[ImGuiCol_FrameBgHovered]         = ImVec4(0.47f, 0.47f, 0.69f, 0.40f);\n    colors[ImGuiCol_FrameBgActive]          = ImVec4(0.42f, 0.41f, 0.64f, 0.69f);\n    colors[ImGuiCol_TitleBg]                = ImVec4(0.27f, 0.27f, 0.54f, 0.83f);\n    colors[ImGuiCol_TitleBgActive]          = ImVec4(0.32f, 0.32f, 0.63f, 0.87f);\n    colors[ImGuiCol_TitleBgCollapsed]       = ImVec4(0.40f, 0.40f, 0.80f, 0.20f);\n    colors[ImGuiCol_MenuBarBg]              = ImVec4(0.40f, 0.40f, 0.55f, 0.80f);\n    colors[ImGuiCol_ScrollbarBg]            = ImVec4(0.20f, 0.25f, 0.30f, 0.60f);\n    colors[ImGuiCol_ScrollbarGrab]          = ImVec4(0.40f, 0.40f, 0.80f, 0.30f);\n    colors[ImGuiCol_ScrollbarGrabHovered]   = ImVec4(0.40f, 0.40f, 0.80f, 0.40f);\n    colors[ImGuiCol_ScrollbarGrabActive]    = ImVec4(0.41f, 0.39f, 0.80f, 0.60f);\n    colors[ImGuiCol_CheckMark]              = ImVec4(0.90f, 0.90f, 0.90f, 0.50f);\n    colors[ImGuiCol_SliderGrab]             = ImVec4(1.00f, 1.00f, 1.00f, 0.30f);\n    colors[ImGuiCol_SliderGrabActive]       = ImVec4(0.41f, 0.39f, 0.80f, 0.60f);\n    colors[ImGuiCol_Button]                 = ImVec4(0.35f, 0.40f, 0.61f, 0.62f);\n    colors[ImGuiCol_ButtonHovered]          = ImVec4(0.40f, 0.48f, 0.71f, 0.79f);\n    colors[ImGuiCol_ButtonActive]           = ImVec4(0.46f, 0.54f, 0.80f, 1.00f);\n    colors[ImGuiCol_Header]                 = ImVec4(0.40f, 0.40f, 0.90f, 0.45f);\n    colors[ImGuiCol_HeaderHovered]          = ImVec4(0.45f, 0.45f, 0.90f, 0.80f);\n    colors[ImGuiCol_HeaderActive]           = ImVec4(0.53f, 0.53f, 0.87f, 0.80f);\n    colors[ImGuiCol_Separator]              = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);\n    colors[ImGuiCol_SeparatorHovered]       = ImVec4(0.60f, 0.60f, 0.70f, 1.00f);\n    colors[ImGuiCol_SeparatorActive]        = ImVec4(0.70f, 0.70f, 0.90f, 1.00f);\n    colors[ImGuiCol_ResizeGrip]             = ImVec4(1.00f, 1.00f, 1.00f, 0.16f);\n    colors[ImGuiCol_ResizeGripHovered]      = ImVec4(0.78f, 0.82f, 1.00f, 0.60f);\n    colors[ImGuiCol_ResizeGripActive]       = ImVec4(0.78f, 0.82f, 1.00f, 0.90f);\n    colors[ImGuiCol_Tab]                    = ImLerp(colors[ImGuiCol_Header],       colors[ImGuiCol_TitleBgActive], 0.80f);\n    colors[ImGuiCol_TabHovered]             = colors[ImGuiCol_HeaderHovered];\n    colors[ImGuiCol_TabActive]              = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f);\n    colors[ImGuiCol_TabUnfocused]           = ImLerp(colors[ImGuiCol_Tab],          colors[ImGuiCol_TitleBg], 0.80f);\n    colors[ImGuiCol_TabUnfocusedActive]     = ImLerp(colors[ImGuiCol_TabActive],    colors[ImGuiCol_TitleBg], 0.40f);\n    colors[ImGuiCol_DockingPreview]         = colors[ImGuiCol_Header] * ImVec4(1.0f, 1.0f, 1.0f, 0.7f);\n    colors[ImGuiCol_DockingEmptyBg]         = ImVec4(0.20f, 0.20f, 0.20f, 1.00f);\n    colors[ImGuiCol_PlotLines]              = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);\n    colors[ImGuiCol_PlotLinesHovered]       = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);\n    colors[ImGuiCol_PlotHistogram]          = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);\n    colors[ImGuiCol_PlotHistogramHovered]   = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);\n    colors[ImGuiCol_TextSelectedBg]         = ImVec4(0.00f, 0.00f, 1.00f, 0.35f);\n    colors[ImGuiCol_DragDropTarget]         = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);\n    colors[ImGuiCol_NavHighlight]           = colors[ImGuiCol_HeaderHovered];\n    colors[ImGuiCol_NavWindowingHighlight]  = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);\n    colors[ImGuiCol_NavWindowingDimBg]      = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);\n    colors[ImGuiCol_ModalWindowDimBg]       = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);\n}\n\n// Those light colors are better suited with a thicker font than the default one + FrameBorder\nvoid ImGui::StyleColorsLight(ImGuiStyle* dst)\n{\n    ImGuiStyle* style = dst ? dst : &ImGui::GetStyle();\n    ImVec4* colors = style->Colors;\n\n    colors[ImGuiCol_Text]                   = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);\n    colors[ImGuiCol_TextDisabled]           = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);\n    colors[ImGuiCol_WindowBg]               = ImVec4(0.94f, 0.94f, 0.94f, 1.00f);\n    colors[ImGuiCol_ChildBg]                = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n    colors[ImGuiCol_PopupBg]                = ImVec4(1.00f, 1.00f, 1.00f, 0.98f);\n    colors[ImGuiCol_Border]                 = ImVec4(0.00f, 0.00f, 0.00f, 0.30f);\n    colors[ImGuiCol_BorderShadow]           = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);\n    colors[ImGuiCol_FrameBg]                = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);\n    colors[ImGuiCol_FrameBgHovered]         = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);\n    colors[ImGuiCol_FrameBgActive]          = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);\n    colors[ImGuiCol_TitleBg]                = ImVec4(0.96f, 0.96f, 0.96f, 1.00f);\n    colors[ImGuiCol_TitleBgActive]          = ImVec4(0.82f, 0.82f, 0.82f, 1.00f);\n    colors[ImGuiCol_TitleBgCollapsed]       = ImVec4(1.00f, 1.00f, 1.00f, 0.51f);\n    colors[ImGuiCol_MenuBarBg]              = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);\n    colors[ImGuiCol_ScrollbarBg]            = ImVec4(0.98f, 0.98f, 0.98f, 0.53f);\n    colors[ImGuiCol_ScrollbarGrab]          = ImVec4(0.69f, 0.69f, 0.69f, 0.80f);\n    colors[ImGuiCol_ScrollbarGrabHovered]   = ImVec4(0.49f, 0.49f, 0.49f, 0.80f);\n    colors[ImGuiCol_ScrollbarGrabActive]    = ImVec4(0.49f, 0.49f, 0.49f, 1.00f);\n    colors[ImGuiCol_CheckMark]              = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n    colors[ImGuiCol_SliderGrab]             = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);\n    colors[ImGuiCol_SliderGrabActive]       = ImVec4(0.46f, 0.54f, 0.80f, 0.60f);\n    colors[ImGuiCol_Button]                 = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);\n    colors[ImGuiCol_ButtonHovered]          = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n    colors[ImGuiCol_ButtonActive]           = ImVec4(0.06f, 0.53f, 0.98f, 1.00f);\n    colors[ImGuiCol_Header]                 = ImVec4(0.26f, 0.59f, 0.98f, 0.31f);\n    colors[ImGuiCol_HeaderHovered]          = ImVec4(0.26f, 0.59f, 0.98f, 0.80f);\n    colors[ImGuiCol_HeaderActive]           = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);\n    colors[ImGuiCol_Separator]              = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);\n    colors[ImGuiCol_SeparatorHovered]       = ImVec4(0.14f, 0.44f, 0.80f, 0.78f);\n    colors[ImGuiCol_SeparatorActive]        = ImVec4(0.14f, 0.44f, 0.80f, 1.00f);\n    colors[ImGuiCol_ResizeGrip]             = ImVec4(0.80f, 0.80f, 0.80f, 0.56f);\n    colors[ImGuiCol_ResizeGripHovered]      = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);\n    colors[ImGuiCol_ResizeGripActive]       = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);\n    colors[ImGuiCol_Tab]                    = ImLerp(colors[ImGuiCol_Header],       colors[ImGuiCol_TitleBgActive], 0.90f);\n    colors[ImGuiCol_TabHovered]             = colors[ImGuiCol_HeaderHovered];\n    colors[ImGuiCol_TabActive]              = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f);\n    colors[ImGuiCol_TabUnfocused]           = ImLerp(colors[ImGuiCol_Tab],          colors[ImGuiCol_TitleBg], 0.80f);\n    colors[ImGuiCol_TabUnfocusedActive]     = ImLerp(colors[ImGuiCol_TabActive],    colors[ImGuiCol_TitleBg], 0.40f);\n    colors[ImGuiCol_DockingPreview]         = colors[ImGuiCol_Header] * ImVec4(1.0f, 1.0f, 1.0f, 0.7f);\n    colors[ImGuiCol_DockingEmptyBg]         = ImVec4(0.20f, 0.20f, 0.20f, 1.00f);\n    colors[ImGuiCol_PlotLines]              = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);\n    colors[ImGuiCol_PlotLinesHovered]       = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);\n    colors[ImGuiCol_PlotHistogram]          = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);\n    colors[ImGuiCol_PlotHistogramHovered]   = ImVec4(1.00f, 0.45f, 0.00f, 1.00f);\n    colors[ImGuiCol_TextSelectedBg]         = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);\n    colors[ImGuiCol_DragDropTarget]         = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);\n    colors[ImGuiCol_NavHighlight]           = colors[ImGuiCol_HeaderHovered];\n    colors[ImGuiCol_NavWindowingHighlight]  = ImVec4(0.70f, 0.70f, 0.70f, 0.70f);\n    colors[ImGuiCol_NavWindowingDimBg]      = ImVec4(0.20f, 0.20f, 0.20f, 0.20f);\n    colors[ImGuiCol_ModalWindowDimBg]       = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);\n}\n\n//-----------------------------------------------------------------------------\n// ImDrawList\n//-----------------------------------------------------------------------------\n\nImDrawListSharedData::ImDrawListSharedData()\n{\n    Font = NULL;\n    FontSize = 0.0f;\n    CurveTessellationTol = 0.0f;\n    ClipRectFullscreen = ImVec4(-8192.0f, -8192.0f, +8192.0f, +8192.0f);\n\n    // Const data\n    for (int i = 0; i < IM_ARRAYSIZE(CircleVtx12); i++)\n    {\n        const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(CircleVtx12);\n        CircleVtx12[i] = ImVec2(ImCos(a), ImSin(a));\n    }\n}\n\nvoid ImDrawList::Clear()\n{\n    CmdBuffer.resize(0);\n    IdxBuffer.resize(0);\n    VtxBuffer.resize(0);\n    Flags = ImDrawListFlags_AntiAliasedLines | ImDrawListFlags_AntiAliasedFill;\n    _VtxCurrentIdx = 0;\n    _VtxWritePtr = NULL;\n    _IdxWritePtr = NULL;\n    _ClipRectStack.resize(0);\n    _TextureIdStack.resize(0);\n    _Path.resize(0);\n    _ChannelsCurrent = 0;\n    _ChannelsCount = 1;\n    // NB: Do not clear channels so our allocations are re-used after the first frame.\n}\n\nvoid ImDrawList::ClearFreeMemory()\n{\n    CmdBuffer.clear();\n    IdxBuffer.clear();\n    VtxBuffer.clear();\n    _VtxCurrentIdx = 0;\n    _VtxWritePtr = NULL;\n    _IdxWritePtr = NULL;\n    _ClipRectStack.clear();\n    _TextureIdStack.clear();\n    _Path.clear();\n    _ChannelsCurrent = 0;\n    _ChannelsCount = 1;\n    for (int i = 0; i < _Channels.Size; i++)\n    {\n        if (i == 0) memset(&_Channels[0], 0, sizeof(_Channels[0]));  // channel 0 is a copy of CmdBuffer/IdxBuffer, don't destruct again\n        _Channels[i].CmdBuffer.clear();\n        _Channels[i].IdxBuffer.clear();\n    }\n    _Channels.clear();\n}\n\nImDrawList* ImDrawList::CloneOutput() const\n{\n    ImDrawList* dst = IM_NEW(ImDrawList(NULL));\n    dst->CmdBuffer = CmdBuffer;\n    dst->IdxBuffer = IdxBuffer;\n    dst->VtxBuffer = VtxBuffer;\n    dst->Flags = Flags;\n    return dst;\n}\n\n// Using macros because C++ is a terrible language, we want guaranteed inline, no code in header, and no overhead in Debug builds\n#define GetCurrentClipRect()    (_ClipRectStack.Size ? _ClipRectStack.Data[_ClipRectStack.Size-1]  : _Data->ClipRectFullscreen)\n#define GetCurrentTextureId()   (_TextureIdStack.Size ? _TextureIdStack.Data[_TextureIdStack.Size-1] : (ImTextureID)NULL)\n\nvoid ImDrawList::AddDrawCmd()\n{\n    ImDrawCmd draw_cmd;\n    draw_cmd.ClipRect = GetCurrentClipRect();\n    draw_cmd.TextureId = GetCurrentTextureId();\n\n    IM_ASSERT(draw_cmd.ClipRect.x <= draw_cmd.ClipRect.z && draw_cmd.ClipRect.y <= draw_cmd.ClipRect.w);\n    CmdBuffer.push_back(draw_cmd);\n}\n\nvoid ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data)\n{\n    ImDrawCmd* current_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL;\n    if (!current_cmd || current_cmd->ElemCount != 0 || current_cmd->UserCallback != NULL)\n    {\n        AddDrawCmd();\n        current_cmd = &CmdBuffer.back();\n    }\n    current_cmd->UserCallback = callback;\n    current_cmd->UserCallbackData = callback_data;\n\n    AddDrawCmd(); // Force a new command after us (see comment below)\n}\n\n// Our scheme may appears a bit unusual, basically we want the most-common calls AddLine AddRect etc. to not have to perform any check so we always have a command ready in the stack.\n// The cost of figuring out if a new command has to be added or if we can merge is paid in those Update** functions only.\nvoid ImDrawList::UpdateClipRect()\n{\n    // If current command is used with different settings we need to add a new command\n    const ImVec4 curr_clip_rect = GetCurrentClipRect();\n    ImDrawCmd* curr_cmd = CmdBuffer.Size > 0 ? &CmdBuffer.Data[CmdBuffer.Size-1] : NULL;\n    if (!curr_cmd || (curr_cmd->ElemCount != 0 && memcmp(&curr_cmd->ClipRect, &curr_clip_rect, sizeof(ImVec4)) != 0) || curr_cmd->UserCallback != NULL)\n    {\n        AddDrawCmd();\n        return;\n    }\n\n    // Try to merge with previous command if it matches, else use current command\n    ImDrawCmd* prev_cmd = CmdBuffer.Size > 1 ? curr_cmd - 1 : NULL;\n    if (curr_cmd->ElemCount == 0 && prev_cmd && memcmp(&prev_cmd->ClipRect, &curr_clip_rect, sizeof(ImVec4)) == 0 && prev_cmd->TextureId == GetCurrentTextureId() && prev_cmd->UserCallback == NULL)\n        CmdBuffer.pop_back();\n    else\n        curr_cmd->ClipRect = curr_clip_rect;\n}\n\nvoid ImDrawList::UpdateTextureID()\n{\n    // If current command is used with different settings we need to add a new command\n    const ImTextureID curr_texture_id = GetCurrentTextureId();\n    ImDrawCmd* curr_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL;\n    if (!curr_cmd || (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != curr_texture_id) || curr_cmd->UserCallback != NULL)\n    {\n        AddDrawCmd();\n        return;\n    }\n\n    // Try to merge with previous command if it matches, else use current command\n    ImDrawCmd* prev_cmd = CmdBuffer.Size > 1 ? curr_cmd - 1 : NULL;\n    if (curr_cmd->ElemCount == 0 && prev_cmd && prev_cmd->TextureId == curr_texture_id && memcmp(&prev_cmd->ClipRect, &GetCurrentClipRect(), sizeof(ImVec4)) == 0 && prev_cmd->UserCallback == NULL)\n        CmdBuffer.pop_back();\n    else\n        curr_cmd->TextureId = curr_texture_id;\n}\n\n#undef GetCurrentClipRect\n#undef GetCurrentTextureId\n\n// Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling)\nvoid ImDrawList::PushClipRect(ImVec2 cr_min, ImVec2 cr_max, bool intersect_with_current_clip_rect)\n{\n    ImVec4 cr(cr_min.x, cr_min.y, cr_max.x, cr_max.y);\n    if (intersect_with_current_clip_rect && _ClipRectStack.Size)\n    {\n        ImVec4 current = _ClipRectStack.Data[_ClipRectStack.Size-1];\n        if (cr.x < current.x) cr.x = current.x;\n        if (cr.y < current.y) cr.y = current.y;\n        if (cr.z > current.z) cr.z = current.z;\n        if (cr.w > current.w) cr.w = current.w;\n    }\n    cr.z = ImMax(cr.x, cr.z);\n    cr.w = ImMax(cr.y, cr.w);\n\n    _ClipRectStack.push_back(cr);\n    UpdateClipRect();\n}\n\nvoid ImDrawList::PushClipRectFullScreen()\n{\n    PushClipRect(ImVec2(_Data->ClipRectFullscreen.x, _Data->ClipRectFullscreen.y), ImVec2(_Data->ClipRectFullscreen.z, _Data->ClipRectFullscreen.w));\n}\n\nvoid ImDrawList::PopClipRect()\n{\n    IM_ASSERT(_ClipRectStack.Size > 0);\n    _ClipRectStack.pop_back();\n    UpdateClipRect();\n}\n\nvoid ImDrawList::PushTextureID(ImTextureID texture_id)\n{\n    _TextureIdStack.push_back(texture_id);\n    UpdateTextureID();\n}\n\nvoid ImDrawList::PopTextureID()\n{\n    IM_ASSERT(_TextureIdStack.Size > 0);\n    _TextureIdStack.pop_back();\n    UpdateTextureID();\n}\n\nvoid ImDrawList::ChannelsSplit(int channels_count)\n{\n    IM_ASSERT(_ChannelsCurrent == 0 && _ChannelsCount == 1);\n    int old_channels_count = _Channels.Size;\n    if (old_channels_count < channels_count)\n        _Channels.resize(channels_count);\n    _ChannelsCount = channels_count;\n\n    // _Channels[] (24/32 bytes each) hold storage that we'll swap with this->_CmdBuffer/_IdxBuffer\n    // The content of _Channels[0] at this point doesn't matter. We clear it to make state tidy in a debugger but we don't strictly need to.\n    // When we switch to the next channel, we'll copy _CmdBuffer/_IdxBuffer into _Channels[0] and then _Channels[1] into _CmdBuffer/_IdxBuffer\n    memset(&_Channels[0], 0, sizeof(ImDrawChannel));\n    for (int i = 1; i < channels_count; i++)\n    {\n        if (i >= old_channels_count)\n        {\n            IM_PLACEMENT_NEW(&_Channels[i]) ImDrawChannel();\n        }\n        else\n        {\n            _Channels[i].CmdBuffer.resize(0);\n            _Channels[i].IdxBuffer.resize(0);\n        }\n        if (_Channels[i].CmdBuffer.Size == 0)\n        {\n            ImDrawCmd draw_cmd;\n            draw_cmd.ClipRect = _ClipRectStack.back();\n            draw_cmd.TextureId = _TextureIdStack.back();\n            _Channels[i].CmdBuffer.push_back(draw_cmd);\n        }\n    }\n}\n\nvoid ImDrawList::ChannelsMerge()\n{\n    // Note that we never use or rely on channels.Size because it is merely a buffer that we never shrink back to 0 to keep all sub-buffers ready for use.\n    if (_ChannelsCount <= 1)\n        return;\n\n    ChannelsSetCurrent(0);\n    if (CmdBuffer.Size && CmdBuffer.back().ElemCount == 0)\n        CmdBuffer.pop_back();\n\n    int new_cmd_buffer_count = 0, new_idx_buffer_count = 0;\n    for (int i = 1; i < _ChannelsCount; i++)\n    {\n        ImDrawChannel& ch = _Channels[i];\n        if (ch.CmdBuffer.Size && ch.CmdBuffer.back().ElemCount == 0)\n            ch.CmdBuffer.pop_back();\n        new_cmd_buffer_count += ch.CmdBuffer.Size;\n        new_idx_buffer_count += ch.IdxBuffer.Size;\n    }\n    CmdBuffer.resize(CmdBuffer.Size + new_cmd_buffer_count);\n    IdxBuffer.resize(IdxBuffer.Size + new_idx_buffer_count);\n\n    ImDrawCmd* cmd_write = CmdBuffer.Data + CmdBuffer.Size - new_cmd_buffer_count;\n    _IdxWritePtr = IdxBuffer.Data + IdxBuffer.Size - new_idx_buffer_count;\n    for (int i = 1; i < _ChannelsCount; i++)\n    {\n        ImDrawChannel& ch = _Channels[i];\n        if (int sz = ch.CmdBuffer.Size) { memcpy(cmd_write, ch.CmdBuffer.Data, sz * sizeof(ImDrawCmd)); cmd_write += sz; }\n        if (int sz = ch.IdxBuffer.Size) { memcpy(_IdxWritePtr, ch.IdxBuffer.Data, sz * sizeof(ImDrawIdx)); _IdxWritePtr += sz; }\n    }\n    UpdateClipRect(); // We call this instead of AddDrawCmd(), so that empty channels won't produce an extra draw call.\n    _ChannelsCount = 1;\n}\n\nvoid ImDrawList::ChannelsSetCurrent(int idx)\n{\n    IM_ASSERT(idx < _ChannelsCount);\n    if (_ChannelsCurrent == idx) return;\n    memcpy(&_Channels.Data[_ChannelsCurrent].CmdBuffer, &CmdBuffer, sizeof(CmdBuffer)); // copy 12 bytes, four times\n    memcpy(&_Channels.Data[_ChannelsCurrent].IdxBuffer, &IdxBuffer, sizeof(IdxBuffer));\n    _ChannelsCurrent = idx;\n    memcpy(&CmdBuffer, &_Channels.Data[_ChannelsCurrent].CmdBuffer, sizeof(CmdBuffer));\n    memcpy(&IdxBuffer, &_Channels.Data[_ChannelsCurrent].IdxBuffer, sizeof(IdxBuffer));\n    _IdxWritePtr = IdxBuffer.Data + IdxBuffer.Size;\n}\n\n// NB: this can be called with negative count for removing primitives (as long as the result does not underflow)\nvoid ImDrawList::PrimReserve(int idx_count, int vtx_count)\n{\n    ImDrawCmd& draw_cmd = CmdBuffer.Data[CmdBuffer.Size-1];\n    draw_cmd.ElemCount += idx_count;\n\n    int vtx_buffer_old_size = VtxBuffer.Size;\n    VtxBuffer.resize(vtx_buffer_old_size + vtx_count);\n    _VtxWritePtr = VtxBuffer.Data + vtx_buffer_old_size;\n\n    int idx_buffer_old_size = IdxBuffer.Size;\n    IdxBuffer.resize(idx_buffer_old_size + idx_count);\n    _IdxWritePtr = IdxBuffer.Data + idx_buffer_old_size;\n}\n\n// Fully unrolled with inline call to keep our debug builds decently fast.\nvoid ImDrawList::PrimRect(const ImVec2& a, const ImVec2& c, ImU32 col)\n{\n    ImVec2 b(c.x, a.y), d(a.x, c.y), uv(_Data->TexUvWhitePixel);\n    ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx;\n    _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2);\n    _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3);\n    _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;\n    _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col;\n    _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col;\n    _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col;\n    _VtxWritePtr += 4;\n    _VtxCurrentIdx += 4;\n    _IdxWritePtr += 6;\n}\n\nvoid ImDrawList::PrimRectUV(const ImVec2& a, const ImVec2& c, const ImVec2& uv_a, const ImVec2& uv_c, ImU32 col)\n{\n    ImVec2 b(c.x, a.y), d(a.x, c.y), uv_b(uv_c.x, uv_a.y), uv_d(uv_a.x, uv_c.y);\n    ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx;\n    _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2);\n    _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3);\n    _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col;\n    _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col;\n    _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col;\n    _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col;\n    _VtxWritePtr += 4;\n    _VtxCurrentIdx += 4;\n    _IdxWritePtr += 6;\n}\n\nvoid ImDrawList::PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a, const ImVec2& uv_b, const ImVec2& uv_c, const ImVec2& uv_d, ImU32 col)\n{\n    ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx;\n    _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2);\n    _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3);\n    _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col;\n    _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col;\n    _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col;\n    _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col;\n    _VtxWritePtr += 4;\n    _VtxCurrentIdx += 4;\n    _IdxWritePtr += 6;\n}\n\n// On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superflous function calls to optimize debug/non-inlined builds.\n// Those macros expects l-values.\n#define IM_NORMALIZE2F_OVER_ZERO(VX,VY)     { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = 1.0f / ImSqrt(d2); VX *= inv_len; VY *= inv_len; } }\n#define IM_FIXNORMAL2F(VX,VY)               { float d2 = VX*VX + VY*VY; if (d2 < 0.5f) d2 = 0.5f; float inv_lensq = 1.0f / d2; VX *= inv_lensq; VY *= inv_lensq; }\n\n// TODO: Thickness anti-aliased lines cap are missing their AA fringe.\n// We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds.\nvoid ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, bool closed, float thickness)\n{\n    if (points_count < 2)\n        return;\n\n    const ImVec2 uv = _Data->TexUvWhitePixel;\n\n    int count = points_count;\n    if (!closed)\n        count = points_count-1;\n\n    const bool thick_line = thickness > 1.0f;\n    if (Flags & ImDrawListFlags_AntiAliasedLines)\n    {\n        // Anti-aliased stroke\n        const float AA_SIZE = 1.0f;\n        const ImU32 col_trans = col & ~IM_COL32_A_MASK;\n\n        const int idx_count = thick_line ? count*18 : count*12;\n        const int vtx_count = thick_line ? points_count*4 : points_count*3;\n        PrimReserve(idx_count, vtx_count);\n\n        // Temporary buffer\n        ImVec2* temp_normals = (ImVec2*)alloca(points_count * (thick_line ? 5 : 3) * sizeof(ImVec2)); //-V630\n        ImVec2* temp_points = temp_normals + points_count;\n\n        for (int i1 = 0; i1 < count; i1++)\n        {\n            const int i2 = (i1+1) == points_count ? 0 : i1+1;\n            float dx = points[i2].x - points[i1].x;\n            float dy = points[i2].y - points[i1].y;\n            IM_NORMALIZE2F_OVER_ZERO(dx, dy);\n            temp_normals[i1].x = dy;\n            temp_normals[i1].y = -dx;\n        }\n        if (!closed)\n            temp_normals[points_count-1] = temp_normals[points_count-2];\n\n        if (!thick_line)\n        {\n            if (!closed)\n            {\n                temp_points[0] = points[0] + temp_normals[0] * AA_SIZE;\n                temp_points[1] = points[0] - temp_normals[0] * AA_SIZE;\n                temp_points[(points_count-1)*2+0] = points[points_count-1] + temp_normals[points_count-1] * AA_SIZE;\n                temp_points[(points_count-1)*2+1] = points[points_count-1] - temp_normals[points_count-1] * AA_SIZE;\n            }\n\n            // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer.\n            unsigned int idx1 = _VtxCurrentIdx;\n            for (int i1 = 0; i1 < count; i1++)\n            {\n                const int i2 = (i1+1) == points_count ? 0 : i1+1;\n                unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+3;\n\n                // Average normals\n                float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f;\n                float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f;\n                IM_FIXNORMAL2F(dm_x, dm_y)\n                dm_x *= AA_SIZE;\n                dm_y *= AA_SIZE;\n\n                // Add temporary vertexes\n                ImVec2* out_vtx = &temp_points[i2*2];\n                out_vtx[0].x = points[i2].x + dm_x;\n                out_vtx[0].y = points[i2].y + dm_y;\n                out_vtx[1].x = points[i2].x - dm_x;\n                out_vtx[1].y = points[i2].y - dm_y;\n\n                // Add indexes\n                _IdxWritePtr[0] = (ImDrawIdx)(idx2+0); _IdxWritePtr[1] = (ImDrawIdx)(idx1+0); _IdxWritePtr[2] = (ImDrawIdx)(idx1+2);\n                _IdxWritePtr[3] = (ImDrawIdx)(idx1+2); _IdxWritePtr[4] = (ImDrawIdx)(idx2+2); _IdxWritePtr[5] = (ImDrawIdx)(idx2+0);\n                _IdxWritePtr[6] = (ImDrawIdx)(idx2+1); _IdxWritePtr[7] = (ImDrawIdx)(idx1+1); _IdxWritePtr[8] = (ImDrawIdx)(idx1+0);\n                _IdxWritePtr[9] = (ImDrawIdx)(idx1+0); _IdxWritePtr[10]= (ImDrawIdx)(idx2+0); _IdxWritePtr[11]= (ImDrawIdx)(idx2+1);\n                _IdxWritePtr += 12;\n\n                idx1 = idx2;\n            }\n\n            // Add vertexes\n            for (int i = 0; i < points_count; i++)\n            {\n                _VtxWritePtr[0].pos = points[i];          _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;\n                _VtxWritePtr[1].pos = temp_points[i*2+0]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans;\n                _VtxWritePtr[2].pos = temp_points[i*2+1]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col_trans;\n                _VtxWritePtr += 3;\n            }\n        }\n        else\n        {\n            const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;\n            if (!closed)\n            {\n                temp_points[0] = points[0] + temp_normals[0] * (half_inner_thickness + AA_SIZE);\n                temp_points[1] = points[0] + temp_normals[0] * (half_inner_thickness);\n                temp_points[2] = points[0] - temp_normals[0] * (half_inner_thickness);\n                temp_points[3] = points[0] - temp_normals[0] * (half_inner_thickness + AA_SIZE);\n                temp_points[(points_count-1)*4+0] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE);\n                temp_points[(points_count-1)*4+1] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness);\n                temp_points[(points_count-1)*4+2] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness);\n                temp_points[(points_count-1)*4+3] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE);\n            }\n\n            // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer.\n            unsigned int idx1 = _VtxCurrentIdx;\n            for (int i1 = 0; i1 < count; i1++)\n            {\n                const int i2 = (i1+1) == points_count ? 0 : i1+1;\n                unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+4;\n\n                // Average normals\n                float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f;\n                float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f;\n                IM_FIXNORMAL2F(dm_x, dm_y);\n                float dm_out_x = dm_x * (half_inner_thickness + AA_SIZE);\n                float dm_out_y = dm_y * (half_inner_thickness + AA_SIZE);\n                float dm_in_x = dm_x * half_inner_thickness;\n                float dm_in_y = dm_y * half_inner_thickness;\n\n                // Add temporary vertexes\n                ImVec2* out_vtx = &temp_points[i2*4];\n                out_vtx[0].x = points[i2].x + dm_out_x;\n                out_vtx[0].y = points[i2].y + dm_out_y;\n                out_vtx[1].x = points[i2].x + dm_in_x;\n                out_vtx[1].y = points[i2].y + dm_in_y;\n                out_vtx[2].x = points[i2].x - dm_in_x;\n                out_vtx[2].y = points[i2].y - dm_in_y;\n                out_vtx[3].x = points[i2].x - dm_out_x;\n                out_vtx[3].y = points[i2].y - dm_out_y;\n\n                // Add indexes\n                _IdxWritePtr[0]  = (ImDrawIdx)(idx2+1); _IdxWritePtr[1]  = (ImDrawIdx)(idx1+1); _IdxWritePtr[2]  = (ImDrawIdx)(idx1+2);\n                _IdxWritePtr[3]  = (ImDrawIdx)(idx1+2); _IdxWritePtr[4]  = (ImDrawIdx)(idx2+2); _IdxWritePtr[5]  = (ImDrawIdx)(idx2+1);\n                _IdxWritePtr[6]  = (ImDrawIdx)(idx2+1); _IdxWritePtr[7]  = (ImDrawIdx)(idx1+1); _IdxWritePtr[8]  = (ImDrawIdx)(idx1+0);\n                _IdxWritePtr[9]  = (ImDrawIdx)(idx1+0); _IdxWritePtr[10] = (ImDrawIdx)(idx2+0); _IdxWritePtr[11] = (ImDrawIdx)(idx2+1);\n                _IdxWritePtr[12] = (ImDrawIdx)(idx2+2); _IdxWritePtr[13] = (ImDrawIdx)(idx1+2); _IdxWritePtr[14] = (ImDrawIdx)(idx1+3);\n                _IdxWritePtr[15] = (ImDrawIdx)(idx1+3); _IdxWritePtr[16] = (ImDrawIdx)(idx2+3); _IdxWritePtr[17] = (ImDrawIdx)(idx2+2);\n                _IdxWritePtr += 18;\n\n                idx1 = idx2;\n            }\n\n            // Add vertexes\n            for (int i = 0; i < points_count; i++)\n            {\n                _VtxWritePtr[0].pos = temp_points[i*4+0]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col_trans;\n                _VtxWritePtr[1].pos = temp_points[i*4+1]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col;\n                _VtxWritePtr[2].pos = temp_points[i*4+2]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col;\n                _VtxWritePtr[3].pos = temp_points[i*4+3]; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col_trans;\n                _VtxWritePtr += 4;\n            }\n        }\n        _VtxCurrentIdx += (ImDrawIdx)vtx_count;\n    }\n    else\n    {\n        // Non Anti-aliased Stroke\n        const int idx_count = count*6;\n        const int vtx_count = count*4;      // FIXME-OPT: Not sharing edges\n        PrimReserve(idx_count, vtx_count);\n\n        for (int i1 = 0; i1 < count; i1++)\n        {\n            const int i2 = (i1+1) == points_count ? 0 : i1+1;\n            const ImVec2& p1 = points[i1];\n            const ImVec2& p2 = points[i2];\n\n            float dx = p2.x - p1.x;\n            float dy = p2.y - p1.y;\n            IM_NORMALIZE2F_OVER_ZERO(dx, dy);\n            dx *= (thickness * 0.5f);\n            dy *= (thickness * 0.5f);\n\n            _VtxWritePtr[0].pos.x = p1.x + dy; _VtxWritePtr[0].pos.y = p1.y - dx; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;\n            _VtxWritePtr[1].pos.x = p2.x + dy; _VtxWritePtr[1].pos.y = p2.y - dx; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col;\n            _VtxWritePtr[2].pos.x = p2.x - dy; _VtxWritePtr[2].pos.y = p2.y + dx; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col;\n            _VtxWritePtr[3].pos.x = p1.x - dy; _VtxWritePtr[3].pos.y = p1.y + dx; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col;\n            _VtxWritePtr += 4;\n\n            _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+2);\n            _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx+2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx+3);\n            _IdxWritePtr += 6;\n            _VtxCurrentIdx += 4;\n        }\n    }\n}\n\n// We intentionally avoid using ImVec2 and its math operators here to reduce cost to a minimum for debug/non-inlined builds.\nvoid ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_count, ImU32 col)\n{\n    if (points_count < 3)\n        return;\n\n    const ImVec2 uv = _Data->TexUvWhitePixel;\n\n    if (Flags & ImDrawListFlags_AntiAliasedFill)\n    {\n        // Anti-aliased Fill\n        const float AA_SIZE = 1.0f;\n        const ImU32 col_trans = col & ~IM_COL32_A_MASK;\n        const int idx_count = (points_count-2)*3 + points_count*6;\n        const int vtx_count = (points_count*2);\n        PrimReserve(idx_count, vtx_count);\n\n        // Add indexes for fill\n        unsigned int vtx_inner_idx = _VtxCurrentIdx;\n        unsigned int vtx_outer_idx = _VtxCurrentIdx+1;\n        for (int i = 2; i < points_count; i++)\n        {\n            _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+((i-1)<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx+(i<<1));\n            _IdxWritePtr += 3;\n        }\n\n        // Compute normals\n        ImVec2* temp_normals = (ImVec2*)alloca(points_count * sizeof(ImVec2)); //-V630\n        for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++)\n        {\n            const ImVec2& p0 = points[i0];\n            const ImVec2& p1 = points[i1];\n            float dx = p1.x - p0.x;\n            float dy = p1.y - p0.y;\n            IM_NORMALIZE2F_OVER_ZERO(dx, dy);\n            temp_normals[i0].x = dy;\n            temp_normals[i0].y = -dx;\n        }\n\n        for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++)\n        {\n            // Average normals\n            const ImVec2& n0 = temp_normals[i0];\n            const ImVec2& n1 = temp_normals[i1];\n            float dm_x = (n0.x + n1.x) * 0.5f;\n            float dm_y = (n0.y + n1.y) * 0.5f;\n            IM_FIXNORMAL2F(dm_x, dm_y);\n            dm_x *= AA_SIZE * 0.5f;\n            dm_y *= AA_SIZE * 0.5f;\n\n            // Add vertices\n            _VtxWritePtr[0].pos.x = (points[i1].x - dm_x); _VtxWritePtr[0].pos.y = (points[i1].y - dm_y); _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;        // Inner\n            _VtxWritePtr[1].pos.x = (points[i1].x + dm_x); _VtxWritePtr[1].pos.y = (points[i1].y + dm_y); _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans;  // Outer\n            _VtxWritePtr += 2;\n\n            // Add indexes for fringes\n            _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx+(i1<<1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+(i0<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx+(i0<<1));\n            _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx+(i0<<1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx+(i1<<1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx+(i1<<1));\n            _IdxWritePtr += 6;\n        }\n        _VtxCurrentIdx += (ImDrawIdx)vtx_count;\n    }\n    else\n    {\n        // Non Anti-aliased Fill\n        const int idx_count = (points_count-2)*3;\n        const int vtx_count = points_count;\n        PrimReserve(idx_count, vtx_count);\n        for (int i = 0; i < vtx_count; i++)\n        {\n            _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col;\n            _VtxWritePtr++;\n        }\n        for (int i = 2; i < points_count; i++)\n        {\n            _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+i-1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+i);\n            _IdxWritePtr += 3;\n        }\n        _VtxCurrentIdx += (ImDrawIdx)vtx_count;\n    }\n}\n\nvoid ImDrawList::PathArcToFast(const ImVec2& centre, float radius, int a_min_of_12, int a_max_of_12)\n{\n    if (radius == 0.0f || a_min_of_12 > a_max_of_12)\n    {\n        _Path.push_back(centre);\n        return;\n    }\n    _Path.reserve(_Path.Size + (a_max_of_12 - a_min_of_12 + 1));\n    for (int a = a_min_of_12; a <= a_max_of_12; a++)\n    {\n        const ImVec2& c = _Data->CircleVtx12[a % IM_ARRAYSIZE(_Data->CircleVtx12)];\n        _Path.push_back(ImVec2(centre.x + c.x * radius, centre.y + c.y * radius));\n    }\n}\n\nvoid ImDrawList::PathArcTo(const ImVec2& centre, float radius, float a_min, float a_max, int num_segments)\n{\n    if (radius == 0.0f)\n    {\n        _Path.push_back(centre);\n        return;\n    }\n\n    // Note that we are adding a point at both a_min and a_max.\n    // If you are trying to draw a full closed circle you don't want the overlapping points!\n    _Path.reserve(_Path.Size + (num_segments + 1));\n    for (int i = 0; i <= num_segments; i++)\n    {\n        const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min);\n        _Path.push_back(ImVec2(centre.x + ImCos(a) * radius, centre.y + ImSin(a) * radius));\n    }\n}\n\nstatic void PathBezierToCasteljau(ImVector<ImVec2>* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level)\n{\n    float dx = x4 - x1;\n    float dy = y4 - y1;\n    float d2 = ((x2 - x4) * dy - (y2 - y4) * dx);\n    float d3 = ((x3 - x4) * dy - (y3 - y4) * dx);\n    d2 = (d2 >= 0) ? d2 : -d2;\n    d3 = (d3 >= 0) ? d3 : -d3;\n    if ((d2+d3) * (d2+d3) < tess_tol * (dx*dx + dy*dy))\n    {\n        path->push_back(ImVec2(x4, y4));\n    }\n    else if (level < 10)\n    {\n        float x12 = (x1+x2)*0.5f,       y12 = (y1+y2)*0.5f;\n        float x23 = (x2+x3)*0.5f,       y23 = (y2+y3)*0.5f;\n        float x34 = (x3+x4)*0.5f,       y34 = (y3+y4)*0.5f;\n        float x123 = (x12+x23)*0.5f,    y123 = (y12+y23)*0.5f;\n        float x234 = (x23+x34)*0.5f,    y234 = (y23+y34)*0.5f;\n        float x1234 = (x123+x234)*0.5f, y1234 = (y123+y234)*0.5f;\n\n        PathBezierToCasteljau(path, x1,y1,        x12,y12,    x123,y123,  x1234,y1234, tess_tol, level+1);\n        PathBezierToCasteljau(path, x1234,y1234,  x234,y234,  x34,y34,    x4,y4,       tess_tol, level+1);\n    }\n}\n\nvoid ImDrawList::PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments)\n{\n    ImVec2 p1 = _Path.back();\n    if (num_segments == 0)\n    {\n        // Auto-tessellated\n        PathBezierToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, _Data->CurveTessellationTol, 0);\n    }\n    else\n    {\n        float t_step = 1.0f / (float)num_segments;\n        for (int i_step = 1; i_step <= num_segments; i_step++)\n        {\n            float t = t_step * i_step;\n            float u = 1.0f - t;\n            float w1 = u*u*u;\n            float w2 = 3*u*u*t;\n            float w3 = 3*u*t*t;\n            float w4 = t*t*t;\n            _Path.push_back(ImVec2(w1*p1.x + w2*p2.x + w3*p3.x + w4*p4.x, w1*p1.y + w2*p2.y + w3*p3.y + w4*p4.y));\n        }\n    }\n}\n\nvoid ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, int rounding_corners)\n{\n    rounding = ImMin(rounding, ImFabs(b.x - a.x) * ( ((rounding_corners & ImDrawCornerFlags_Top)  == ImDrawCornerFlags_Top)  || ((rounding_corners & ImDrawCornerFlags_Bot)   == ImDrawCornerFlags_Bot)   ? 0.5f : 1.0f ) - 1.0f);\n    rounding = ImMin(rounding, ImFabs(b.y - a.y) * ( ((rounding_corners & ImDrawCornerFlags_Left) == ImDrawCornerFlags_Left) || ((rounding_corners & ImDrawCornerFlags_Right) == ImDrawCornerFlags_Right) ? 0.5f : 1.0f ) - 1.0f);\n\n    if (rounding <= 0.0f || rounding_corners == 0)\n    {\n        PathLineTo(a);\n        PathLineTo(ImVec2(b.x, a.y));\n        PathLineTo(b);\n        PathLineTo(ImVec2(a.x, b.y));\n    }\n    else\n    {\n        const float rounding_tl = (rounding_corners & ImDrawCornerFlags_TopLeft) ? rounding : 0.0f;\n        const float rounding_tr = (rounding_corners & ImDrawCornerFlags_TopRight) ? rounding : 0.0f;\n        const float rounding_br = (rounding_corners & ImDrawCornerFlags_BotRight) ? rounding : 0.0f;\n        const float rounding_bl = (rounding_corners & ImDrawCornerFlags_BotLeft) ? rounding : 0.0f;\n        PathArcToFast(ImVec2(a.x + rounding_tl, a.y + rounding_tl), rounding_tl, 6, 9);\n        PathArcToFast(ImVec2(b.x - rounding_tr, a.y + rounding_tr), rounding_tr, 9, 12);\n        PathArcToFast(ImVec2(b.x - rounding_br, b.y - rounding_br), rounding_br, 0, 3);\n        PathArcToFast(ImVec2(a.x + rounding_bl, b.y - rounding_bl), rounding_bl, 3, 6);\n    }\n}\n\nvoid ImDrawList::AddLine(const ImVec2& a, const ImVec2& b, ImU32 col, float thickness)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n    PathLineTo(a + ImVec2(0.5f,0.5f));\n    PathLineTo(b + ImVec2(0.5f,0.5f));\n    PathStroke(col, false, thickness);\n}\n\n// a: upper-left, b: lower-right. we don't render 1 px sized rectangles properly.\nvoid ImDrawList::AddRect(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding, int rounding_corners_flags, float thickness)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n    if (Flags & ImDrawListFlags_AntiAliasedLines)\n        PathRect(a + ImVec2(0.5f,0.5f), b - ImVec2(0.50f,0.50f), rounding, rounding_corners_flags);\n    else\n        PathRect(a + ImVec2(0.5f,0.5f), b - ImVec2(0.49f,0.49f), rounding, rounding_corners_flags); // Better looking lower-right corner and rounded non-AA shapes.\n    PathStroke(col, true, thickness);\n}\n\nvoid ImDrawList::AddRectFilled(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding, int rounding_corners_flags)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n    if (rounding > 0.0f)\n    {\n        PathRect(a, b, rounding, rounding_corners_flags);\n        PathFillConvex(col);\n    }\n    else\n    {\n        PrimReserve(6, 4);\n        PrimRect(a, b, col);\n    }\n}\n\nvoid ImDrawList::AddRectFilledMultiColor(const ImVec2& a, const ImVec2& c, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left)\n{\n    if (((col_upr_left | col_upr_right | col_bot_right | col_bot_left) & IM_COL32_A_MASK) == 0)\n        return;\n\n    const ImVec2 uv = _Data->TexUvWhitePixel;\n    PrimReserve(6, 4);\n    PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+1)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2));\n    PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+3));\n    PrimWriteVtx(a, uv, col_upr_left);\n    PrimWriteVtx(ImVec2(c.x, a.y), uv, col_upr_right);\n    PrimWriteVtx(c, uv, col_bot_right);\n    PrimWriteVtx(ImVec2(a.x, c.y), uv, col_bot_left);\n}\n\nvoid ImDrawList::AddQuad(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, ImU32 col, float thickness)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n\n    PathLineTo(a);\n    PathLineTo(b);\n    PathLineTo(c);\n    PathLineTo(d);\n    PathStroke(col, true, thickness);\n}\n\nvoid ImDrawList::AddQuadFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, ImU32 col)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n\n    PathLineTo(a);\n    PathLineTo(b);\n    PathLineTo(c);\n    PathLineTo(d);\n    PathFillConvex(col);\n}\n\nvoid ImDrawList::AddTriangle(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col, float thickness)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n\n    PathLineTo(a);\n    PathLineTo(b);\n    PathLineTo(c);\n    PathStroke(col, true, thickness);\n}\n\nvoid ImDrawList::AddTriangleFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n\n    PathLineTo(a);\n    PathLineTo(b);\n    PathLineTo(c);\n    PathFillConvex(col);\n}\n\nvoid ImDrawList::AddCircle(const ImVec2& centre, float radius, ImU32 col, int num_segments, float thickness)\n{\n    if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2)\n        return;\n\n    // Because we are filling a closed shape we remove 1 from the count of segments/points\n    const float a_max = IM_PI*2.0f * ((float)num_segments - 1.0f) / (float)num_segments;\n    PathArcTo(centre, radius-0.5f, 0.0f, a_max, num_segments - 1);\n    PathStroke(col, true, thickness);\n}\n\nvoid ImDrawList::AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, int num_segments)\n{\n    if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2)\n        return;\n\n    // Because we are filling a closed shape we remove 1 from the count of segments/points\n    const float a_max = IM_PI*2.0f * ((float)num_segments - 1.0f) / (float)num_segments;\n    PathArcTo(centre, radius, 0.0f, a_max, num_segments - 1);\n    PathFillConvex(col);\n}\n\nvoid ImDrawList::AddBezierCurve(const ImVec2& pos0, const ImVec2& cp0, const ImVec2& cp1, const ImVec2& pos1, ImU32 col, float thickness, int num_segments)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n\n    PathLineTo(pos0);\n    PathBezierCurveTo(cp0, cp1, pos1, num_segments);\n    PathStroke(col, false, thickness);\n}\n\nvoid ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end, float wrap_width, const ImVec4* cpu_fine_clip_rect)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n\n    if (text_end == NULL)\n        text_end = text_begin + strlen(text_begin);\n    if (text_begin == text_end)\n        return;\n\n    // Pull default font/size from the shared ImDrawListSharedData instance\n    if (font == NULL)\n        font = _Data->Font;\n    if (font_size == 0.0f)\n        font_size = _Data->FontSize;\n\n    IM_ASSERT(font->ContainerAtlas->TexID == _TextureIdStack.back());  // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font.\n\n    ImVec4 clip_rect = _ClipRectStack.back();\n    if (cpu_fine_clip_rect)\n    {\n        clip_rect.x = ImMax(clip_rect.x, cpu_fine_clip_rect->x);\n        clip_rect.y = ImMax(clip_rect.y, cpu_fine_clip_rect->y);\n        clip_rect.z = ImMin(clip_rect.z, cpu_fine_clip_rect->z);\n        clip_rect.w = ImMin(clip_rect.w, cpu_fine_clip_rect->w);\n    }\n    font->RenderText(this, font_size, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip_rect != NULL);\n}\n\nvoid ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end)\n{\n    AddText(NULL, 0.0f, pos, col, text_begin, text_end);\n}\n\nvoid ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, ImU32 col)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n\n    const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back();\n    if (push_texture_id)\n        PushTextureID(user_texture_id);\n\n    PrimReserve(6, 4);\n    PrimRectUV(a, b, uv_a, uv_b, col);\n\n    if (push_texture_id)\n        PopTextureID();\n}\n\nvoid ImDrawList::AddImageQuad(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a, const ImVec2& uv_b, const ImVec2& uv_c, const ImVec2& uv_d, ImU32 col)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n\n    const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back();\n    if (push_texture_id)\n        PushTextureID(user_texture_id);\n\n    PrimReserve(6, 4);\n    PrimQuadUV(a, b, c, d, uv_a, uv_b, uv_c, uv_d, col);\n\n    if (push_texture_id)\n        PopTextureID();\n}\n\nvoid ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, ImU32 col, float rounding, int rounding_corners)\n{\n    if ((col & IM_COL32_A_MASK) == 0)\n        return;\n\n    if (rounding <= 0.0f || (rounding_corners & ImDrawCornerFlags_All) == 0)\n    {\n        AddImage(user_texture_id, a, b, uv_a, uv_b, col);\n        return;\n    }\n\n    const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back();\n    if (push_texture_id)\n        PushTextureID(user_texture_id);\n\n    int vert_start_idx = VtxBuffer.Size;\n    PathRect(a, b, rounding, rounding_corners);\n    PathFillConvex(col);\n    int vert_end_idx = VtxBuffer.Size;\n    ImGui::ShadeVertsLinearUV(this, vert_start_idx, vert_end_idx, a, b, uv_a, uv_b, true);\n\n    if (push_texture_id)\n        PopTextureID();\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] ImDrawData\n//-----------------------------------------------------------------------------\n\n// For backward compatibility: convert all buffers from indexed to de-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering!\nvoid ImDrawData::DeIndexAllBuffers()\n{\n    ImVector<ImDrawVert> new_vtx_buffer;\n    TotalVtxCount = TotalIdxCount = 0;\n    for (int i = 0; i < CmdListsCount; i++)\n    {\n        ImDrawList* cmd_list = CmdLists[i];\n        if (cmd_list->IdxBuffer.empty())\n            continue;\n        new_vtx_buffer.resize(cmd_list->IdxBuffer.Size);\n        for (int j = 0; j < cmd_list->IdxBuffer.Size; j++)\n            new_vtx_buffer[j] = cmd_list->VtxBuffer[cmd_list->IdxBuffer[j]];\n        cmd_list->VtxBuffer.swap(new_vtx_buffer);\n        cmd_list->IdxBuffer.resize(0);\n        TotalVtxCount += cmd_list->VtxBuffer.Size;\n    }\n}\n\n// Helper to scale the ClipRect field of each ImDrawCmd.\n// Use if your final output buffer is at a different scale than draw_data->DisplaySize,\n// or if there is a difference between your window resolution and framebuffer resolution.\nvoid ImDrawData::ScaleClipRects(const ImVec2& fb_scale)\n{\n    for (int i = 0; i < CmdListsCount; i++)\n    {\n        ImDrawList* cmd_list = CmdLists[i];\n        for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)\n        {\n            ImDrawCmd* cmd = &cmd_list->CmdBuffer[cmd_i];\n            cmd->ClipRect = ImVec4(cmd->ClipRect.x * fb_scale.x, cmd->ClipRect.y * fb_scale.y, cmd->ClipRect.z * fb_scale.x, cmd->ClipRect.w * fb_scale.y);\n        }\n    }\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] Helpers ShadeVertsXXX functions\n//-----------------------------------------------------------------------------\n\n// Generic linear color gradient, write to RGB fields, leave A untouched.\nvoid ImGui::ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1)\n{\n    ImVec2 gradient_extent = gradient_p1 - gradient_p0;\n    float gradient_inv_length2 = 1.0f / ImLengthSqr(gradient_extent);\n    ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx;\n    ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx;\n    for (ImDrawVert* vert = vert_start; vert < vert_end; vert++)\n    {\n        float d = ImDot(vert->pos - gradient_p0, gradient_extent);\n        float t = ImClamp(d * gradient_inv_length2, 0.0f, 1.0f);\n        int r = ImLerp((int)(col0 >> IM_COL32_R_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_R_SHIFT) & 0xFF, t);\n        int g = ImLerp((int)(col0 >> IM_COL32_G_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_G_SHIFT) & 0xFF, t);\n        int b = ImLerp((int)(col0 >> IM_COL32_B_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_B_SHIFT) & 0xFF, t);\n        vert->col = (r << IM_COL32_R_SHIFT) | (g << IM_COL32_G_SHIFT) | (b << IM_COL32_B_SHIFT) | (vert->col & IM_COL32_A_MASK);\n    }\n}\n\n// Distribute UV over (a, b) rectangle\nvoid ImGui::ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, bool clamp)\n{\n    const ImVec2 size = b - a;\n    const ImVec2 uv_size = uv_b - uv_a;\n    const ImVec2 scale = ImVec2(\n        size.x != 0.0f ? (uv_size.x / size.x) : 0.0f,\n        size.y != 0.0f ? (uv_size.y / size.y) : 0.0f);\n\n    ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx;\n    ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx;\n    if (clamp)\n    {\n        const ImVec2 min = ImMin(uv_a, uv_b);\n        const ImVec2 max = ImMax(uv_a, uv_b);\n        for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex)\n            vertex->uv = ImClamp(uv_a + ImMul(ImVec2(vertex->pos.x, vertex->pos.y) - a, scale), min, max);\n    }\n    else\n    {\n        for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex)\n            vertex->uv = uv_a + ImMul(ImVec2(vertex->pos.x, vertex->pos.y) - a, scale);\n    }\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] ImFontConfig\n//-----------------------------------------------------------------------------\n\nImFontConfig::ImFontConfig()\n{\n    FontData = NULL;\n    FontDataSize = 0;\n    FontDataOwnedByAtlas = true;\n    FontNo = 0;\n    SizePixels = 0.0f;\n    OversampleH = 3; // FIXME: 2 may be a better default?\n    OversampleV = 1;\n    PixelSnapH = false;\n    GlyphExtraSpacing = ImVec2(0.0f, 0.0f);\n    GlyphOffset = ImVec2(0.0f, 0.0f);\n    GlyphRanges = NULL;\n    GlyphMinAdvanceX = 0.0f;\n    GlyphMaxAdvanceX = FLT_MAX;\n    MergeMode = false;\n    RasterizerFlags = 0x00;\n    RasterizerMultiply = 1.0f;\n    memset(Name, 0, sizeof(Name));\n    DstFont = NULL;\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] ImFontAtlas\n//-----------------------------------------------------------------------------\n\n// A work of art lies ahead! (. = white layer, X = black layer, others are blank)\n// The white texels on the top left are the ones we'll use everywhere in ImGui to render filled shapes.\nconst int FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF = 108;\nconst int FONT_ATLAS_DEFAULT_TEX_DATA_H      = 27;\nconst unsigned int FONT_ATLAS_DEFAULT_TEX_DATA_ID = 0x80000000;\nstatic const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] =\n{\n    \"..-         -XXXXXXX-    X    -           X           -XXXXXXX          -          XXXXXXX-     XX          \"\n    \"..-         -X.....X-   X.X   -          X.X          -X.....X          -          X.....X-    X..X         \"\n    \"---         -XXX.XXX-  X...X  -         X...X         -X....X           -           X....X-    X..X         \"\n    \"X           -  X.X  - X.....X -        X.....X        -X...X            -            X...X-    X..X         \"\n    \"XX          -  X.X  -X.......X-       X.......X       -X..X.X           -           X.X..X-    X..X         \"\n    \"X.X         -  X.X  -XXXX.XXXX-       XXXX.XXXX       -X.X X.X          -          X.X X.X-    X..XXX       \"\n    \"X..X        -  X.X  -   X.X   -          X.X          -XX   X.X         -         X.X   XX-    X..X..XXX    \"\n    \"X...X       -  X.X  -   X.X   -    XX    X.X    XX    -      X.X        -        X.X      -    X..X..X..XX  \"\n    \"X....X      -  X.X  -   X.X   -   X.X    X.X    X.X   -       X.X       -       X.X       -    X..X..X..X.X \"\n    \"X.....X     -  X.X  -   X.X   -  X..X    X.X    X..X  -        X.X      -      X.X        -XXX X..X..X..X..X\"\n    \"X......X    -  X.X  -   X.X   - X...XXXXXX.XXXXXX...X -         X.X   XX-XX   X.X         -X..XX........X..X\"\n    \"X.......X   -  X.X  -   X.X   -X.....................X-          X.X X.X-X.X X.X          -X...X...........X\"\n    \"X........X  -  X.X  -   X.X   - X...XXXXXX.XXXXXX...X -           X.X..X-X..X.X           - X..............X\"\n    \"X.........X -XXX.XXX-   X.X   -  X..X    X.X    X..X  -            X...X-X...X            -  X.............X\"\n    \"X..........X-X.....X-   X.X   -   X.X    X.X    X.X   -           X....X-X....X           -  X.............X\"\n    \"X......XXXXX-XXXXXXX-   X.X   -    XX    X.X    XX    -          X.....X-X.....X          -   X............X\"\n    \"X...X..X    ---------   X.X   -          X.X          -          XXXXXXX-XXXXXXX          -   X...........X \"\n    \"X..X X..X   -       -XXXX.XXXX-       XXXX.XXXX       -------------------------------------    X..........X \"\n    \"X.X  X..X   -       -X.......X-       X.......X       -    XX           XX    -           -    X..........X \"\n    \"XX    X..X  -       - X.....X -        X.....X        -   X.X           X.X   -           -     X........X  \"\n    \"      X..X          -  X...X  -         X...X         -  X..X           X..X  -           -     X........X  \"\n    \"       XX           -   X.X   -          X.X          - X...XXXXXXXXXXXXX...X -           -     XXXXXXXXXX  \"\n    \"------------        -    X    -           X           -X.....................X-           ------------------\"\n    \"                    ----------------------------------- X...XXXXXXXXXXXXX...X -                             \"\n    \"                                                      -  X..X           X..X  -                             \"\n    \"                                                      -   X.X           X.X   -                             \"\n    \"                                                      -    XX           XX    -                             \"\n};\n\nstatic const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3] =\n{\n    // Pos ........ Size ......... Offset ......\n    { ImVec2( 0,3), ImVec2(12,19), ImVec2( 0, 0) }, // ImGuiMouseCursor_Arrow\n    { ImVec2(13,0), ImVec2( 7,16), ImVec2( 1, 8) }, // ImGuiMouseCursor_TextInput\n    { ImVec2(31,0), ImVec2(23,23), ImVec2(11,11) }, // ImGuiMouseCursor_ResizeAll\n    { ImVec2(21,0), ImVec2( 9,23), ImVec2( 4,11) }, // ImGuiMouseCursor_ResizeNS\n    { ImVec2(55,18),ImVec2(23, 9), ImVec2(11, 4) }, // ImGuiMouseCursor_ResizeEW\n    { ImVec2(73,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNESW\n    { ImVec2(55,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNWSE\n    { ImVec2(91,0), ImVec2(17,22), ImVec2( 5, 0) }, // ImGuiMouseCursor_Hand\n};\n\nImFontAtlas::ImFontAtlas()\n{\n    Locked = false;\n    Flags = ImFontAtlasFlags_None;\n    TexID = (ImTextureID)NULL;\n    TexDesiredWidth = 0;\n    TexGlyphPadding = 1;\n\n    TexPixelsAlpha8 = NULL;\n    TexPixelsRGBA32 = NULL;\n    TexWidth = TexHeight = 0;\n    TexUvScale = ImVec2(0.0f, 0.0f);\n    TexUvWhitePixel = ImVec2(0.0f, 0.0f);\n    for (int n = 0; n < IM_ARRAYSIZE(CustomRectIds); n++)\n        CustomRectIds[n] = -1;\n}\n\nImFontAtlas::~ImFontAtlas()\n{\n    IM_ASSERT(!Locked && \"Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!\");\n    Clear();\n}\n\nvoid    ImFontAtlas::ClearInputData()\n{\n    IM_ASSERT(!Locked && \"Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!\");\n    for (int i = 0; i < ConfigData.Size; i++)\n        if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas)\n        {\n            IM_FREE(ConfigData[i].FontData);\n            ConfigData[i].FontData = NULL;\n        }\n\n    // When clearing this we lose access to the font name and other information used to build the font.\n    for (int i = 0; i < Fonts.Size; i++)\n        if (Fonts[i]->ConfigData >= ConfigData.Data && Fonts[i]->ConfigData < ConfigData.Data + ConfigData.Size)\n        {\n            Fonts[i]->ConfigData = NULL;\n            Fonts[i]->ConfigDataCount = 0;\n        }\n    ConfigData.clear();\n    CustomRects.clear();\n    for (int n = 0; n < IM_ARRAYSIZE(CustomRectIds); n++)\n        CustomRectIds[n] = -1;\n}\n\nvoid    ImFontAtlas::ClearTexData()\n{\n    IM_ASSERT(!Locked && \"Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!\");\n    if (TexPixelsAlpha8)\n        IM_FREE(TexPixelsAlpha8);\n    if (TexPixelsRGBA32)\n        IM_FREE(TexPixelsRGBA32);\n    TexPixelsAlpha8 = NULL;\n    TexPixelsRGBA32 = NULL;\n}\n\nvoid    ImFontAtlas::ClearFonts()\n{\n    IM_ASSERT(!Locked && \"Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!\");\n    for (int i = 0; i < Fonts.Size; i++)\n        IM_DELETE(Fonts[i]);\n    Fonts.clear();\n}\n\nvoid    ImFontAtlas::Clear()\n{\n    ClearInputData();\n    ClearTexData();\n    ClearFonts();\n}\n\nvoid    ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)\n{\n    // Build atlas on demand\n    if (TexPixelsAlpha8 == NULL)\n    {\n        if (ConfigData.empty())\n            AddFontDefault();\n        Build();\n    }\n\n    *out_pixels = TexPixelsAlpha8;\n    if (out_width) *out_width = TexWidth;\n    if (out_height) *out_height = TexHeight;\n    if (out_bytes_per_pixel) *out_bytes_per_pixel = 1;\n}\n\nvoid    ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel)\n{\n    // Convert to RGBA32 format on demand\n    // Although it is likely to be the most commonly used format, our font rendering is 1 channel / 8 bpp\n    if (!TexPixelsRGBA32)\n    {\n        unsigned char* pixels = NULL;\n        GetTexDataAsAlpha8(&pixels, NULL, NULL);\n        if (pixels)\n        {\n            TexPixelsRGBA32 = (unsigned int*)IM_ALLOC((size_t)TexWidth * (size_t)TexHeight * 4);\n            const unsigned char* src = pixels;\n            unsigned int* dst = TexPixelsRGBA32;\n            for (int n = TexWidth * TexHeight; n > 0; n--)\n                *dst++ = IM_COL32(255, 255, 255, (unsigned int)(*src++));\n        }\n    }\n\n    *out_pixels = (unsigned char*)TexPixelsRGBA32;\n    if (out_width) *out_width = TexWidth;\n    if (out_height) *out_height = TexHeight;\n    if (out_bytes_per_pixel) *out_bytes_per_pixel = 4;\n}\n\nImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg)\n{\n    IM_ASSERT(!Locked && \"Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!\");\n    IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0);\n    IM_ASSERT(font_cfg->SizePixels > 0.0f);\n\n    // Create new font\n    if (!font_cfg->MergeMode)\n        Fonts.push_back(IM_NEW(ImFont));\n    else\n        IM_ASSERT(!Fonts.empty() && \"Cannot use MergeMode for the first font\"); // When using MergeMode make sure that a font has already been added before. You can use ImGui::GetIO().Fonts->AddFontDefault() to add the default imgui font.\n\n    ConfigData.push_back(*font_cfg);\n    ImFontConfig& new_font_cfg = ConfigData.back();\n    if (new_font_cfg.DstFont == NULL)\n        new_font_cfg.DstFont = Fonts.back();\n    if (!new_font_cfg.FontDataOwnedByAtlas)\n    {\n        new_font_cfg.FontData = IM_ALLOC(new_font_cfg.FontDataSize);\n        new_font_cfg.FontDataOwnedByAtlas = true;\n        memcpy(new_font_cfg.FontData, font_cfg->FontData, (size_t)new_font_cfg.FontDataSize);\n    }\n\n    // Invalidate texture\n    ClearTexData();\n    return new_font_cfg.DstFont;\n}\n\n// Default font TTF is compressed with stb_compress then base85 encoded (see misc/fonts/binary_to_compressed_c.cpp for encoder)\nstatic unsigned int stb_decompress_length(const unsigned char *input);\nstatic unsigned int stb_decompress(unsigned char *output, const unsigned char *input, unsigned int length);\nstatic const char*  GetDefaultCompressedFontDataTTFBase85();\nstatic unsigned int Decode85Byte(char c)                                    { return c >= '\\\\' ? c-36 : c-35; }\nstatic void         Decode85(const unsigned char* src, unsigned char* dst)\n{\n    while (*src)\n    {\n        unsigned int tmp = Decode85Byte(src[0]) + 85*(Decode85Byte(src[1]) + 85*(Decode85Byte(src[2]) + 85*(Decode85Byte(src[3]) + 85*Decode85Byte(src[4]))));\n        dst[0] = ((tmp >> 0) & 0xFF); dst[1] = ((tmp >> 8) & 0xFF); dst[2] = ((tmp >> 16) & 0xFF); dst[3] = ((tmp >> 24) & 0xFF);   // We can't assume little-endianness.\n        src += 5;\n        dst += 4;\n    }\n}\n\n// Load embedded ProggyClean.ttf at size 13, disable oversampling\nImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template)\n{\n    ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();\n    if (!font_cfg_template)\n    {\n        font_cfg.OversampleH = font_cfg.OversampleV = 1;\n        font_cfg.PixelSnapH = true;\n    }\n    if (font_cfg.SizePixels <= 0.0f)\n        font_cfg.SizePixels = 13.0f * 1.0f;\n    if (font_cfg.Name[0] == '\\0')\n        ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), \"ProggyClean.ttf, %dpx\", (int)font_cfg.SizePixels);\n\n    const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85();\n    const ImWchar* glyph_ranges = font_cfg.GlyphRanges != NULL ? font_cfg.GlyphRanges : GetGlyphRangesDefault();\n    ImFont* font = AddFontFromMemoryCompressedBase85TTF(ttf_compressed_base85, font_cfg.SizePixels, &font_cfg, glyph_ranges);\n    font->DisplayOffset.y = 1.0f;\n    return font;\n}\n\nImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)\n{\n    IM_ASSERT(!Locked && \"Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!\");\n    size_t data_size = 0;\n    void* data = ImFileLoadToMemory(filename, \"rb\", &data_size, 0);\n    if (!data)\n    {\n        IM_ASSERT(0); // Could not load file.\n        return NULL;\n    }\n    ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();\n    if (font_cfg.Name[0] == '\\0')\n    {\n        // Store a short copy of filename into into the font name for convenience\n        const char* p;\n        for (p = filename + strlen(filename); p > filename && p[-1] != '/' && p[-1] != '\\\\'; p--) {}\n        ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), \"%s, %.0fpx\", p, size_pixels);\n    }\n    return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges);\n}\n\n// NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build().\nImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)\n{\n    IM_ASSERT(!Locked && \"Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!\");\n    ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();\n    IM_ASSERT(font_cfg.FontData == NULL);\n    font_cfg.FontData = ttf_data;\n    font_cfg.FontDataSize = ttf_size;\n    font_cfg.SizePixels = size_pixels;\n    if (glyph_ranges)\n        font_cfg.GlyphRanges = glyph_ranges;\n    return AddFont(&font_cfg);\n}\n\nImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges)\n{\n    const unsigned int buf_decompressed_size = stb_decompress_length((const unsigned char*)compressed_ttf_data);\n    unsigned char* buf_decompressed_data = (unsigned char *)IM_ALLOC(buf_decompressed_size);\n    stb_decompress(buf_decompressed_data, (const unsigned char*)compressed_ttf_data, (unsigned int)compressed_ttf_size);\n\n    ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig();\n    IM_ASSERT(font_cfg.FontData == NULL);\n    font_cfg.FontDataOwnedByAtlas = true;\n    return AddFontFromMemoryTTF(buf_decompressed_data, (int)buf_decompressed_size, size_pixels, &font_cfg, glyph_ranges);\n}\n\nImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges)\n{\n    int compressed_ttf_size = (((int)strlen(compressed_ttf_data_base85) + 4) / 5) * 4;\n    void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size);\n    Decode85((const unsigned char*)compressed_ttf_data_base85, (unsigned char*)compressed_ttf);\n    ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges);\n    IM_FREE(compressed_ttf);\n    return font;\n}\n\nint ImFontAtlas::AddCustomRectRegular(unsigned int id, int width, int height)\n{\n    IM_ASSERT(id >= 0x10000);\n    IM_ASSERT(width > 0 && width <= 0xFFFF);\n    IM_ASSERT(height > 0 && height <= 0xFFFF);\n    CustomRect r;\n    r.ID = id;\n    r.Width = (unsigned short)width;\n    r.Height = (unsigned short)height;\n    CustomRects.push_back(r);\n    return CustomRects.Size - 1; // Return index\n}\n\nint ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset)\n{\n    IM_ASSERT(font != NULL);\n    IM_ASSERT(width > 0 && width <= 0xFFFF);\n    IM_ASSERT(height > 0 && height <= 0xFFFF);\n    CustomRect r;\n    r.ID = id;\n    r.Width = (unsigned short)width;\n    r.Height = (unsigned short)height;\n    r.GlyphAdvanceX = advance_x;\n    r.GlyphOffset = offset;\n    r.Font = font;\n    CustomRects.push_back(r);\n    return CustomRects.Size - 1; // Return index\n}\n\nvoid ImFontAtlas::CalcCustomRectUV(const CustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max)\n{\n    IM_ASSERT(TexWidth > 0 && TexHeight > 0);   // Font atlas needs to be built before we can calculate UV coordinates\n    IM_ASSERT(rect->IsPacked());                // Make sure the rectangle has been packed\n    *out_uv_min = ImVec2((float)rect->X * TexUvScale.x, (float)rect->Y * TexUvScale.y);\n    *out_uv_max = ImVec2((float)(rect->X + rect->Width) * TexUvScale.x, (float)(rect->Y + rect->Height) * TexUvScale.y);\n}\n\nbool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2])\n{\n    if (cursor_type <= ImGuiMouseCursor_None || cursor_type >= ImGuiMouseCursor_COUNT)\n        return false;\n    if (Flags & ImFontAtlasFlags_NoMouseCursors)\n        return false;\n\n    IM_ASSERT(CustomRectIds[0] != -1);\n    ImFontAtlas::CustomRect& r = CustomRects[CustomRectIds[0]];\n    IM_ASSERT(r.ID == FONT_ATLAS_DEFAULT_TEX_DATA_ID);\n    ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r.X, (float)r.Y);\n    ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][1];\n    *out_size = size;\n    *out_offset = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][2];\n    out_uv_border[0] = (pos) * TexUvScale;\n    out_uv_border[1] = (pos + size) * TexUvScale;\n    pos.x += FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1;\n    out_uv_fill[0] = (pos) * TexUvScale;\n    out_uv_fill[1] = (pos + size) * TexUvScale;\n    return true;\n}\n\nbool    ImFontAtlas::Build()\n{\n    IM_ASSERT(!Locked && \"Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!\");\n    return ImFontAtlasBuildWithStbTruetype(this);\n}\n\nvoid    ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_brighten_factor)\n{\n    for (unsigned int i = 0; i < 256; i++)\n    {\n        unsigned int value = (unsigned int)(i * in_brighten_factor);\n        out_table[i] = value > 255 ? 255 : (value & 0xFF);\n    }\n}\n\nvoid    ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride)\n{\n    unsigned char* data = pixels + x + y * stride;\n    for (int j = h; j > 0; j--, data += stride)\n        for (int i = 0; i < w; i++)\n            data[i] = table[data[i]];\n}\n\n// Temporary data for one source font (multiple source fonts can be merged into one destination ImFont)\n// (C++03 doesn't allow instancing ImVector<> with function-local types so we declare the type here.)\nstruct ImFontBuildSrcData\n{\n    stbtt_fontinfo      FontInfo;\n    stbtt_pack_range    PackRange;          // Hold the list of codepoints to pack (essentially points to Codepoints.Data)\n    stbrp_rect*         Rects;              // Rectangle to pack. We first fill in their size and the packer will give us their position.\n    stbtt_packedchar*   PackedChars;        // Output glyphs\n    const ImWchar*      SrcRanges;          // Ranges as requested by user (user is allowed to request too much, e.g. 0x0020..0xFFFF)\n    int                 DstIndex;           // Index into atlas->Fonts[] and dst_tmp_array[]\n    int                 GlyphsHighest;      // Highest requested codepoint\n    int                 GlyphsCount;        // Glyph count (excluding missing glyphs and glyphs already set by an earlier source font)\n    ImBoolVector        GlyphsSet;          // Glyph bit map (random access, 1-bit per codepoint. This will be a maximum of 8KB)\n    ImVector<int>       GlyphsList;         // Glyph codepoints list (flattened version of GlyphsMap)\n};\n\n// Temporary data for one destination ImFont* (multiple source fonts can be merged into one destination ImFont)\nstruct ImFontBuildDstData\n{\n    int                 SrcCount;           // Number of source fonts targeting this destination font.\n    int                 GlyphsHighest;\n    int                 GlyphsCount;\n    ImBoolVector        GlyphsSet;          // This is used to resolve collision when multiple sources are merged into a same destination font.\n};\n\nstatic void UnpackBoolVectorToFlatIndexList(const ImBoolVector* in, ImVector<int>* out)\n{\n    IM_ASSERT(sizeof(in->Storage.Data[0]) == sizeof(int));\n    const int* it_begin = in->Storage.begin();\n    const int* it_end = in->Storage.end();\n    for (const int* it = it_begin; it < it_end; it++)\n        if (int entries_32 = *it)\n            for (int bit_n = 0; bit_n < 32; bit_n++)\n                if (entries_32 & (1 << bit_n))\n                    out->push_back((int)((it - it_begin) << 5) + bit_n);\n}\n\nbool    ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas)\n{\n    IM_ASSERT(atlas->ConfigData.Size > 0);\n\n    ImFontAtlasBuildRegisterDefaultCustomRects(atlas);\n\n    // Clear atlas\n    atlas->TexID = (ImTextureID)NULL;\n    atlas->TexWidth = atlas->TexHeight = 0;\n    atlas->TexUvScale = ImVec2(0.0f, 0.0f);\n    atlas->TexUvWhitePixel = ImVec2(0.0f, 0.0f);\n    atlas->ClearTexData();\n\n    // Temporary storage for building\n    ImVector<ImFontBuildSrcData> src_tmp_array;\n    ImVector<ImFontBuildDstData> dst_tmp_array;\n    src_tmp_array.resize(atlas->ConfigData.Size);\n    dst_tmp_array.resize(atlas->Fonts.Size);\n    memset(src_tmp_array.Data, 0, (size_t)src_tmp_array.size_in_bytes());\n    memset(dst_tmp_array.Data, 0, (size_t)dst_tmp_array.size_in_bytes());\n\n    // 1. Initialize font loading structure, check font data validity\n    for (int src_i = 0; src_i < atlas->ConfigData.Size; src_i++)\n    {\n        ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];\n        ImFontConfig& cfg = atlas->ConfigData[src_i];\n        IM_ASSERT(cfg.DstFont && (!cfg.DstFont->IsLoaded() || cfg.DstFont->ContainerAtlas == atlas));\n\n        // Find index from cfg.DstFont (we allow the user to set cfg.DstFont. Also it makes casual debugging nicer than when storing indices)\n        src_tmp.DstIndex = -1;\n        for (int output_i = 0; output_i < atlas->Fonts.Size && src_tmp.DstIndex == -1; output_i++)\n            if (cfg.DstFont == atlas->Fonts[output_i])\n                src_tmp.DstIndex = output_i;\n        IM_ASSERT(src_tmp.DstIndex != -1); // cfg.DstFont not pointing within atlas->Fonts[] array?\n        if (src_tmp.DstIndex == -1)\n            return false;\n\n        // Initialize helper structure for font loading and verify that the TTF/OTF data is correct\n        const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)cfg.FontData, cfg.FontNo);\n        IM_ASSERT(font_offset >= 0 && \"FontData is incorrect, or FontNo cannot be found.\");\n        if (!stbtt_InitFont(&src_tmp.FontInfo, (unsigned char*)cfg.FontData, font_offset))\n            return false;\n\n        // Measure highest codepoints\n        ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex];\n        src_tmp.SrcRanges = cfg.GlyphRanges ? cfg.GlyphRanges : atlas->GetGlyphRangesDefault();\n        for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2)\n            src_tmp.GlyphsHighest = ImMax(src_tmp.GlyphsHighest, (int)src_range[1]);\n        dst_tmp.SrcCount++;\n        dst_tmp.GlyphsHighest = ImMax(dst_tmp.GlyphsHighest, src_tmp.GlyphsHighest);\n    }\n\n    // 2. For every requested codepoint, check for their presence in the font data, and handle redundancy or overlaps between source fonts to avoid unused glyphs.\n    int total_glyphs_count = 0;\n    for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)\n    {\n        ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];\n        ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex];\n        src_tmp.GlyphsSet.Resize(src_tmp.GlyphsHighest + 1);\n        if (dst_tmp.GlyphsSet.Storage.empty())\n            dst_tmp.GlyphsSet.Resize(dst_tmp.GlyphsHighest + 1);\n\n        for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2)\n            for (int codepoint = src_range[0]; codepoint <= src_range[1]; codepoint++)\n            {\n                if (dst_tmp.GlyphsSet.GetBit(codepoint))    // Don't overwrite existing glyphs. We could make this an option for MergeMode (e.g. MergeOverwrite==true)\n                    continue;\n                if (!stbtt_FindGlyphIndex(&src_tmp.FontInfo, codepoint))    // It is actually in the font?\n                    continue;\n\n                // Add to avail set/counters\n                src_tmp.GlyphsCount++;\n                dst_tmp.GlyphsCount++;\n                src_tmp.GlyphsSet.SetBit(codepoint, true);\n                dst_tmp.GlyphsSet.SetBit(codepoint, true);\n                total_glyphs_count++;\n            }\n    }\n\n    // 3. Unpack our bit map into a flat list (we now have all the Unicode points that we know are requested _and_ available _and_ not overlapping another)\n    for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)\n    {\n        ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];\n        src_tmp.GlyphsList.reserve(src_tmp.GlyphsCount);\n        UnpackBoolVectorToFlatIndexList(&src_tmp.GlyphsSet, &src_tmp.GlyphsList);\n        src_tmp.GlyphsSet.Clear();\n        IM_ASSERT(src_tmp.GlyphsList.Size == src_tmp.GlyphsCount);\n    }\n    for (int dst_i = 0; dst_i < dst_tmp_array.Size; dst_i++)\n        dst_tmp_array[dst_i].GlyphsSet.Clear();\n    dst_tmp_array.clear();\n\n    // Allocate packing character data and flag packed characters buffer as non-packed (x0=y0=x1=y1=0)\n    // (We technically don't need to zero-clear buf_rects, but let's do it for the sake of sanity)\n    ImVector<stbrp_rect> buf_rects;\n    ImVector<stbtt_packedchar> buf_packedchars;\n    buf_rects.resize(total_glyphs_count);\n    buf_packedchars.resize(total_glyphs_count);\n    memset(buf_rects.Data, 0, (size_t)buf_rects.size_in_bytes());\n    memset(buf_packedchars.Data, 0, (size_t)buf_packedchars.size_in_bytes());\n\n    // 4. Gather glyphs sizes so we can pack them in our virtual canvas.\n    int total_surface = 0;\n    int buf_rects_out_n = 0;\n    int buf_packedchars_out_n = 0;\n    for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)\n    {\n        ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];\n        if (src_tmp.GlyphsCount == 0)\n            continue;\n\n        src_tmp.Rects = &buf_rects[buf_rects_out_n];\n        src_tmp.PackedChars = &buf_packedchars[buf_packedchars_out_n];\n        buf_rects_out_n += src_tmp.GlyphsCount;\n        buf_packedchars_out_n += src_tmp.GlyphsCount;\n\n        // Convert our ranges in the format stb_truetype wants\n        ImFontConfig& cfg = atlas->ConfigData[src_i];\n        src_tmp.PackRange.font_size = cfg.SizePixels;\n        src_tmp.PackRange.first_unicode_codepoint_in_range = 0;\n        src_tmp.PackRange.array_of_unicode_codepoints = src_tmp.GlyphsList.Data;\n        src_tmp.PackRange.num_chars = src_tmp.GlyphsList.Size;\n        src_tmp.PackRange.chardata_for_range = src_tmp.PackedChars;\n        src_tmp.PackRange.h_oversample = (unsigned char)cfg.OversampleH;\n        src_tmp.PackRange.v_oversample = (unsigned char)cfg.OversampleV;\n\n        // Gather the sizes of all rectangles we will need to pack (this loop is based on stbtt_PackFontRangesGatherRects)\n        const float scale = (cfg.SizePixels > 0) ? stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, cfg.SizePixels) : stbtt_ScaleForMappingEmToPixels(&src_tmp.FontInfo, -cfg.SizePixels);\n        const int padding = atlas->TexGlyphPadding;\n        for (int glyph_i = 0; glyph_i < src_tmp.GlyphsList.Size; glyph_i++)\n        {\n            int x0, y0, x1, y1;\n            const int glyph_index_in_font = stbtt_FindGlyphIndex(&src_tmp.FontInfo, src_tmp.GlyphsList[glyph_i]);\n            IM_ASSERT(glyph_index_in_font != 0);\n            stbtt_GetGlyphBitmapBoxSubpixel(&src_tmp.FontInfo, glyph_index_in_font, scale * cfg.OversampleH, scale * cfg.OversampleV, 0, 0, &x0, &y0, &x1, &y1);\n            src_tmp.Rects[glyph_i].w = (stbrp_coord)(x1 - x0 + padding + cfg.OversampleH - 1);\n            src_tmp.Rects[glyph_i].h = (stbrp_coord)(y1 - y0 + padding + cfg.OversampleV - 1);\n            total_surface += src_tmp.Rects[glyph_i].w * src_tmp.Rects[glyph_i].h;\n        }\n    }\n\n    // We need a width for the skyline algorithm, any width!\n    // The exact width doesn't really matter much, but some API/GPU have texture size limitations and increasing width can decrease height.\n    // User can override TexDesiredWidth and TexGlyphPadding if they wish, otherwise we use a simple heuristic to select the width based on expected surface.\n    const int surface_sqrt = (int)ImSqrt((float)total_surface) + 1;\n    atlas->TexHeight = 0;\n    if (atlas->TexDesiredWidth > 0)\n        atlas->TexWidth = atlas->TexDesiredWidth;\n    else\n        atlas->TexWidth = (surface_sqrt >= 4096*0.7f) ? 4096 : (surface_sqrt >= 2048*0.7f) ? 2048 : (surface_sqrt >= 1024*0.7f) ? 1024 : 512;\n\n    // 5. Start packing\n    // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values).\n    const int TEX_HEIGHT_MAX = 1024 * 32;\n    stbtt_pack_context spc = {};\n    stbtt_PackBegin(&spc, NULL, atlas->TexWidth, TEX_HEIGHT_MAX, 0, atlas->TexGlyphPadding, NULL);\n    ImFontAtlasBuildPackCustomRects(atlas, spc.pack_info);\n\n    // 6. Pack each source font. No rendering yet, we are working with rectangles in an infinitely tall texture at this point.\n    for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)\n    {\n        ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];\n        if (src_tmp.GlyphsCount == 0)\n            continue;\n\n        stbrp_pack_rects((stbrp_context*)spc.pack_info, src_tmp.Rects, src_tmp.GlyphsCount);\n\n        // Extend texture height and mark missing glyphs as non-packed so we won't render them.\n        // FIXME: We are not handling packing failure here (would happen if we got off TEX_HEIGHT_MAX or if a single if larger than TexWidth?)\n        for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++)\n            if (src_tmp.Rects[glyph_i].was_packed)\n                atlas->TexHeight = ImMax(atlas->TexHeight, src_tmp.Rects[glyph_i].y + src_tmp.Rects[glyph_i].h);\n    }\n\n    // 7. Allocate texture\n    atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(atlas->TexHeight);\n    atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight);\n    atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(atlas->TexWidth * atlas->TexHeight);\n    memset(atlas->TexPixelsAlpha8, 0, atlas->TexWidth * atlas->TexHeight);\n    spc.pixels = atlas->TexPixelsAlpha8;\n    spc.height = atlas->TexHeight;\n\n    // 8. Render/rasterize font characters into the texture\n    for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)\n    {\n        ImFontConfig& cfg = atlas->ConfigData[src_i];\n        ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];\n        if (src_tmp.GlyphsCount == 0)\n            continue;\n\n        stbtt_PackFontRangesRenderIntoRects(&spc, &src_tmp.FontInfo, &src_tmp.PackRange, 1, src_tmp.Rects);\n\n        // Apply multiply operator\n        if (cfg.RasterizerMultiply != 1.0f)\n        {\n            unsigned char multiply_table[256];\n            ImFontAtlasBuildMultiplyCalcLookupTable(multiply_table, cfg.RasterizerMultiply);\n            stbrp_rect* r = &src_tmp.Rects[0];\n            for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++, r++)\n                if (r->was_packed)\n                    ImFontAtlasBuildMultiplyRectAlpha8(multiply_table, atlas->TexPixelsAlpha8, r->x, r->y, r->w, r->h, atlas->TexWidth * 1);\n        }\n        src_tmp.Rects = NULL;\n    }\n\n    // End packing\n    stbtt_PackEnd(&spc);\n    buf_rects.clear();\n\n    // 9. Setup ImFont and glyphs for runtime\n    for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)\n    {\n        ImFontBuildSrcData& src_tmp = src_tmp_array[src_i];\n        if (src_tmp.GlyphsCount == 0)\n            continue;\n\n        ImFontConfig& cfg = atlas->ConfigData[src_i];\n        ImFont* dst_font = cfg.DstFont; // We can have multiple input fonts writing into a same destination font (when using MergeMode=true)\n\n        const float font_scale = stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, cfg.SizePixels);\n        int unscaled_ascent, unscaled_descent, unscaled_line_gap;\n        stbtt_GetFontVMetrics(&src_tmp.FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap);\n\n        const float ascent = ImFloor(unscaled_ascent * font_scale + ((unscaled_ascent > 0.0f) ? +1 : -1));\n        const float descent = ImFloor(unscaled_descent * font_scale + ((unscaled_descent > 0.0f) ? +1 : -1));\n        ImFontAtlasBuildSetupFont(atlas, dst_font, &cfg, ascent, descent);\n        const float font_off_x = cfg.GlyphOffset.x;\n        const float font_off_y = cfg.GlyphOffset.y + (float)(int)(dst_font->Ascent + 0.5f);\n\n        for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++)\n        {\n            const int codepoint = src_tmp.GlyphsList[glyph_i];\n            const stbtt_packedchar& pc = src_tmp.PackedChars[glyph_i];\n\n            const float char_advance_x_org = pc.xadvance;\n            const float char_advance_x_mod = ImClamp(char_advance_x_org, cfg.GlyphMinAdvanceX, cfg.GlyphMaxAdvanceX);\n            float char_off_x = font_off_x;\n            if (char_advance_x_org != char_advance_x_mod)\n                char_off_x += cfg.PixelSnapH ? (float)(int)((char_advance_x_mod - char_advance_x_org) * 0.5f) : (char_advance_x_mod - char_advance_x_org) * 0.5f;\n\n            // Register glyph\n            stbtt_aligned_quad q;\n            float dummy_x = 0.0f, dummy_y = 0.0f;\n            stbtt_GetPackedQuad(src_tmp.PackedChars, atlas->TexWidth, atlas->TexHeight, glyph_i, &dummy_x, &dummy_y, &q, 0);\n            dst_font->AddGlyph((ImWchar)codepoint, q.x0 + char_off_x, q.y0 + font_off_y, q.x1 + char_off_x, q.y1 + font_off_y, q.s0, q.t0, q.s1, q.t1, char_advance_x_mod);\n        }\n    }\n\n    // Cleanup temporary (ImVector doesn't honor destructor)\n    for (int src_i = 0; src_i < src_tmp_array.Size; src_i++)\n        src_tmp_array[src_i].~ImFontBuildSrcData();\n\n    ImFontAtlasBuildFinish(atlas);\n    return true;\n}\n\nvoid ImFontAtlasBuildRegisterDefaultCustomRects(ImFontAtlas* atlas)\n{\n    if (atlas->CustomRectIds[0] >= 0)\n        return;\n    if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors))\n        atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_ID, FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF*2+1, FONT_ATLAS_DEFAULT_TEX_DATA_H);\n    else\n        atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_ID, 2, 2);\n}\n\nvoid ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent)\n{\n    if (!font_config->MergeMode)\n    {\n        font->ClearOutputData();\n        font->FontSize = font_config->SizePixels;\n        font->ConfigData = font_config;\n        font->ContainerAtlas = atlas;\n        font->Ascent = ascent;\n        font->Descent = descent;\n    }\n    font->ConfigDataCount++;\n}\n\nvoid ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque)\n{\n    stbrp_context* pack_context = (stbrp_context*)stbrp_context_opaque;\n    IM_ASSERT(pack_context != NULL);\n\n    ImVector<ImFontAtlas::CustomRect>& user_rects = atlas->CustomRects;\n    IM_ASSERT(user_rects.Size >= 1); // We expect at least the default custom rects to be registered, else something went wrong.\n\n    ImVector<stbrp_rect> pack_rects;\n    pack_rects.resize(user_rects.Size);\n    memset(pack_rects.Data, 0, (size_t)pack_rects.size_in_bytes());\n    for (int i = 0; i < user_rects.Size; i++)\n    {\n        pack_rects[i].w = user_rects[i].Width;\n        pack_rects[i].h = user_rects[i].Height;\n    }\n    stbrp_pack_rects(pack_context, &pack_rects[0], pack_rects.Size);\n    for (int i = 0; i < pack_rects.Size; i++)\n        if (pack_rects[i].was_packed)\n        {\n            user_rects[i].X = pack_rects[i].x;\n            user_rects[i].Y = pack_rects[i].y;\n            IM_ASSERT(pack_rects[i].w == user_rects[i].Width && pack_rects[i].h == user_rects[i].Height);\n            atlas->TexHeight = ImMax(atlas->TexHeight, pack_rects[i].y + pack_rects[i].h);\n        }\n}\n\nstatic void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas)\n{\n    IM_ASSERT(atlas->CustomRectIds[0] >= 0);\n    IM_ASSERT(atlas->TexPixelsAlpha8 != NULL);\n    ImFontAtlas::CustomRect& r = atlas->CustomRects[atlas->CustomRectIds[0]];\n    IM_ASSERT(r.ID == FONT_ATLAS_DEFAULT_TEX_DATA_ID);\n    IM_ASSERT(r.IsPacked());\n\n    const int w = atlas->TexWidth;\n    if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors))\n    {\n        // Render/copy pixels\n        IM_ASSERT(r.Width == FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF * 2 + 1 && r.Height == FONT_ATLAS_DEFAULT_TEX_DATA_H);\n        for (int y = 0, n = 0; y < FONT_ATLAS_DEFAULT_TEX_DATA_H; y++)\n            for (int x = 0; x < FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF; x++, n++)\n            {\n                const int offset0 = (int)(r.X + x) + (int)(r.Y + y) * w;\n                const int offset1 = offset0 + FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1;\n                atlas->TexPixelsAlpha8[offset0] = FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[n] == '.' ? 0xFF : 0x00;\n                atlas->TexPixelsAlpha8[offset1] = FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[n] == 'X' ? 0xFF : 0x00;\n            }\n    }\n    else\n    {\n        IM_ASSERT(r.Width == 2 && r.Height == 2);\n        const int offset = (int)(r.X) + (int)(r.Y) * w;\n        atlas->TexPixelsAlpha8[offset] = atlas->TexPixelsAlpha8[offset + 1] = atlas->TexPixelsAlpha8[offset + w] = atlas->TexPixelsAlpha8[offset + w + 1] = 0xFF;\n    }\n    atlas->TexUvWhitePixel = ImVec2((r.X + 0.5f) * atlas->TexUvScale.x, (r.Y + 0.5f) * atlas->TexUvScale.y);\n}\n\nvoid ImFontAtlasBuildFinish(ImFontAtlas* atlas)\n{\n    // Render into our custom data block\n    ImFontAtlasBuildRenderDefaultTexData(atlas);\n\n    // Register custom rectangle glyphs\n    for (int i = 0; i < atlas->CustomRects.Size; i++)\n    {\n        const ImFontAtlas::CustomRect& r = atlas->CustomRects[i];\n        if (r.Font == NULL || r.ID > 0x10000)\n            continue;\n\n        IM_ASSERT(r.Font->ContainerAtlas == atlas);\n        ImVec2 uv0, uv1;\n        atlas->CalcCustomRectUV(&r, &uv0, &uv1);\n        r.Font->AddGlyph((ImWchar)r.ID, r.GlyphOffset.x, r.GlyphOffset.y, r.GlyphOffset.x + r.Width, r.GlyphOffset.y + r.Height, uv0.x, uv0.y, uv1.x, uv1.y, r.GlyphAdvanceX);\n    }\n\n    // Build all fonts lookup tables\n    for (int i = 0; i < atlas->Fonts.Size; i++)\n        if (atlas->Fonts[i]->DirtyLookupTables)\n            atlas->Fonts[i]->BuildLookupTable();\n}\n\n// Retrieve list of range (2 int per range, values are inclusive)\nconst ImWchar*   ImFontAtlas::GetGlyphRangesDefault()\n{\n    static const ImWchar ranges[] =\n    {\n        0x0020, 0x00FF, // Basic Latin + Latin Supplement\n        0,\n    };\n    return &ranges[0];\n}\n\nconst ImWchar*  ImFontAtlas::GetGlyphRangesKorean()\n{\n    static const ImWchar ranges[] =\n    {\n        0x0020, 0x00FF, // Basic Latin + Latin Supplement\n        0x3131, 0x3163, // Korean alphabets\n        0xAC00, 0xD79D, // Korean characters\n        0,\n    };\n    return &ranges[0];\n}\n\nconst ImWchar*  ImFontAtlas::GetGlyphRangesChineseFull()\n{\n    static const ImWchar ranges[] =\n    {\n        0x0020, 0x00FF, // Basic Latin + Latin Supplement\n        0x2000, 0x206F, // General Punctuation\n        0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana\n        0x31F0, 0x31FF, // Katakana Phonetic Extensions\n        0xFF00, 0xFFEF, // Half-width characters\n        0x4e00, 0x9FAF, // CJK Ideograms\n        0,\n    };\n    return &ranges[0];\n}\n\nstatic void UnpackAccumulativeOffsetsIntoRanges(int base_codepoint, const short* accumulative_offsets, int accumulative_offsets_count, ImWchar* out_ranges)\n{\n    for (int n = 0; n < accumulative_offsets_count; n++, out_ranges += 2)\n    {\n        out_ranges[0] = out_ranges[1] = (ImWchar)(base_codepoint + accumulative_offsets[n]);\n        base_codepoint += accumulative_offsets[n];\n    }\n    out_ranges[0] = 0;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] ImFontAtlas glyph ranges helpers\n//-------------------------------------------------------------------------\n\nconst ImWchar*  ImFontAtlas::GetGlyphRangesChineseSimplifiedCommon()\n{\n    // Store 2500 regularly used characters for Simplified Chinese.\n    // Sourced from https://zh.wiktionary.org/wiki/%E9%99%84%E5%BD%95:%E7%8E%B0%E4%BB%A3%E6%B1%89%E8%AF%AD%E5%B8%B8%E7%94%A8%E5%AD%97%E8%A1%A8\n    // This table covers 97.97% of all characters used during the month in July, 1987.\n    // You can use ImFontGlyphRangesBuilder to create your own ranges derived from this, by merging existing ranges or adding new characters.\n    // (Stored as accumulative offsets from the initial unicode codepoint 0x4E00. This encoding is designed to helps us compact the source code size.)\n    static const short accumulative_offsets_from_0x4E00[] =\n    {\n        0,1,2,4,1,1,1,1,2,1,3,2,1,2,2,1,1,1,1,1,5,2,1,2,3,3,3,2,2,4,1,1,1,2,1,5,2,3,1,2,1,2,1,1,2,1,1,2,2,1,4,1,1,1,1,5,10,1,2,19,2,1,2,1,2,1,2,1,2,\n        1,5,1,6,3,2,1,2,2,1,1,1,4,8,5,1,1,4,1,1,3,1,2,1,5,1,2,1,1,1,10,1,1,5,2,4,6,1,4,2,2,2,12,2,1,1,6,1,1,1,4,1,1,4,6,5,1,4,2,2,4,10,7,1,1,4,2,4,\n        2,1,4,3,6,10,12,5,7,2,14,2,9,1,1,6,7,10,4,7,13,1,5,4,8,4,1,1,2,28,5,6,1,1,5,2,5,20,2,2,9,8,11,2,9,17,1,8,6,8,27,4,6,9,20,11,27,6,68,2,2,1,1,\n        1,2,1,2,2,7,6,11,3,3,1,1,3,1,2,1,1,1,1,1,3,1,1,8,3,4,1,5,7,2,1,4,4,8,4,2,1,2,1,1,4,5,6,3,6,2,12,3,1,3,9,2,4,3,4,1,5,3,3,1,3,7,1,5,1,1,1,1,2,\n        3,4,5,2,3,2,6,1,1,2,1,7,1,7,3,4,5,15,2,2,1,5,3,22,19,2,1,1,1,1,2,5,1,1,1,6,1,1,12,8,2,9,18,22,4,1,1,5,1,16,1,2,7,10,15,1,1,6,2,4,1,2,4,1,6,\n        1,1,3,2,4,1,6,4,5,1,2,1,1,2,1,10,3,1,3,2,1,9,3,2,5,7,2,19,4,3,6,1,1,1,1,1,4,3,2,1,1,1,2,5,3,1,1,1,2,2,1,1,2,1,1,2,1,3,1,1,1,3,7,1,4,1,1,2,1,\n        1,2,1,2,4,4,3,8,1,1,1,2,1,3,5,1,3,1,3,4,6,2,2,14,4,6,6,11,9,1,15,3,1,28,5,2,5,5,3,1,3,4,5,4,6,14,3,2,3,5,21,2,7,20,10,1,2,19,2,4,28,28,2,3,\n        2,1,14,4,1,26,28,42,12,40,3,52,79,5,14,17,3,2,2,11,3,4,6,3,1,8,2,23,4,5,8,10,4,2,7,3,5,1,1,6,3,1,2,2,2,5,28,1,1,7,7,20,5,3,29,3,17,26,1,8,4,\n        27,3,6,11,23,5,3,4,6,13,24,16,6,5,10,25,35,7,3,2,3,3,14,3,6,2,6,1,4,2,3,8,2,1,1,3,3,3,4,1,1,13,2,2,4,5,2,1,14,14,1,2,2,1,4,5,2,3,1,14,3,12,\n        3,17,2,16,5,1,2,1,8,9,3,19,4,2,2,4,17,25,21,20,28,75,1,10,29,103,4,1,2,1,1,4,2,4,1,2,3,24,2,2,2,1,1,2,1,3,8,1,1,1,2,1,1,3,1,1,1,6,1,5,3,1,1,\n        1,3,4,1,1,5,2,1,5,6,13,9,16,1,1,1,1,3,2,3,2,4,5,2,5,2,2,3,7,13,7,2,2,1,1,1,1,2,3,3,2,1,6,4,9,2,1,14,2,14,2,1,18,3,4,14,4,11,41,15,23,15,23,\n        176,1,3,4,1,1,1,1,5,3,1,2,3,7,3,1,1,2,1,2,4,4,6,2,4,1,9,7,1,10,5,8,16,29,1,1,2,2,3,1,3,5,2,4,5,4,1,1,2,2,3,3,7,1,6,10,1,17,1,44,4,6,2,1,1,6,\n        5,4,2,10,1,6,9,2,8,1,24,1,2,13,7,8,8,2,1,4,1,3,1,3,3,5,2,5,10,9,4,9,12,2,1,6,1,10,1,1,7,7,4,10,8,3,1,13,4,3,1,6,1,3,5,2,1,2,17,16,5,2,16,6,\n        1,4,2,1,3,3,6,8,5,11,11,1,3,3,2,4,6,10,9,5,7,4,7,4,7,1,1,4,2,1,3,6,8,7,1,6,11,5,5,3,24,9,4,2,7,13,5,1,8,82,16,61,1,1,1,4,2,2,16,10,3,8,1,1,\n        6,4,2,1,3,1,1,1,4,3,8,4,2,2,1,1,1,1,1,6,3,5,1,1,4,6,9,2,1,1,1,2,1,7,2,1,6,1,5,4,4,3,1,8,1,3,3,1,3,2,2,2,2,3,1,6,1,2,1,2,1,3,7,1,8,2,1,2,1,5,\n        2,5,3,5,10,1,2,1,1,3,2,5,11,3,9,3,5,1,1,5,9,1,2,1,5,7,9,9,8,1,3,3,3,6,8,2,3,2,1,1,32,6,1,2,15,9,3,7,13,1,3,10,13,2,14,1,13,10,2,1,3,10,4,15,\n        2,15,15,10,1,3,9,6,9,32,25,26,47,7,3,2,3,1,6,3,4,3,2,8,5,4,1,9,4,2,2,19,10,6,2,3,8,1,2,2,4,2,1,9,4,4,4,6,4,8,9,2,3,1,1,1,1,3,5,5,1,3,8,4,6,\n        2,1,4,12,1,5,3,7,13,2,5,8,1,6,1,2,5,14,6,1,5,2,4,8,15,5,1,23,6,62,2,10,1,1,8,1,2,2,10,4,2,2,9,2,1,1,3,2,3,1,5,3,3,2,1,3,8,1,1,1,11,3,1,1,4,\n        3,7,1,14,1,2,3,12,5,2,5,1,6,7,5,7,14,11,1,3,1,8,9,12,2,1,11,8,4,4,2,6,10,9,13,1,1,3,1,5,1,3,2,4,4,1,18,2,3,14,11,4,29,4,2,7,1,3,13,9,2,2,5,\n        3,5,20,7,16,8,5,72,34,6,4,22,12,12,28,45,36,9,7,39,9,191,1,1,1,4,11,8,4,9,2,3,22,1,1,1,1,4,17,1,7,7,1,11,31,10,2,4,8,2,3,2,1,4,2,16,4,32,2,\n        3,19,13,4,9,1,5,2,14,8,1,1,3,6,19,6,5,1,16,6,2,10,8,5,1,2,3,1,5,5,1,11,6,6,1,3,3,2,6,3,8,1,1,4,10,7,5,7,7,5,8,9,2,1,3,4,1,1,3,1,3,3,2,6,16,\n        1,4,6,3,1,10,6,1,3,15,2,9,2,10,25,13,9,16,6,2,2,10,11,4,3,9,1,2,6,6,5,4,30,40,1,10,7,12,14,33,6,3,6,7,3,1,3,1,11,14,4,9,5,12,11,49,18,51,31,\n        140,31,2,2,1,5,1,8,1,10,1,4,4,3,24,1,10,1,3,6,6,16,3,4,5,2,1,4,2,57,10,6,22,2,22,3,7,22,6,10,11,36,18,16,33,36,2,5,5,1,1,1,4,10,1,4,13,2,7,\n        5,2,9,3,4,1,7,43,3,7,3,9,14,7,9,1,11,1,1,3,7,4,18,13,1,14,1,3,6,10,73,2,2,30,6,1,11,18,19,13,22,3,46,42,37,89,7,3,16,34,2,2,3,9,1,7,1,1,1,2,\n        2,4,10,7,3,10,3,9,5,28,9,2,6,13,7,3,1,3,10,2,7,2,11,3,6,21,54,85,2,1,4,2,2,1,39,3,21,2,2,5,1,1,1,4,1,1,3,4,15,1,3,2,4,4,2,3,8,2,20,1,8,7,13,\n        4,1,26,6,2,9,34,4,21,52,10,4,4,1,5,12,2,11,1,7,2,30,12,44,2,30,1,1,3,6,16,9,17,39,82,2,2,24,7,1,7,3,16,9,14,44,2,1,2,1,2,3,5,2,4,1,6,7,5,3,\n        2,6,1,11,5,11,2,1,18,19,8,1,3,24,29,2,1,3,5,2,2,1,13,6,5,1,46,11,3,5,1,1,5,8,2,10,6,12,6,3,7,11,2,4,16,13,2,5,1,1,2,2,5,2,28,5,2,23,10,8,4,\n        4,22,39,95,38,8,14,9,5,1,13,5,4,3,13,12,11,1,9,1,27,37,2,5,4,4,63,211,95,2,2,2,1,3,5,2,1,1,2,2,1,1,1,3,2,4,1,2,1,1,5,2,2,1,1,2,3,1,3,1,1,1,\n        3,1,4,2,1,3,6,1,1,3,7,15,5,3,2,5,3,9,11,4,2,22,1,6,3,8,7,1,4,28,4,16,3,3,25,4,4,27,27,1,4,1,2,2,7,1,3,5,2,28,8,2,14,1,8,6,16,25,3,3,3,14,3,\n        3,1,1,2,1,4,6,3,8,4,1,1,1,2,3,6,10,6,2,3,18,3,2,5,5,4,3,1,5,2,5,4,23,7,6,12,6,4,17,11,9,5,1,1,10,5,12,1,1,11,26,33,7,3,6,1,17,7,1,5,12,1,11,\n        2,4,1,8,14,17,23,1,2,1,7,8,16,11,9,6,5,2,6,4,16,2,8,14,1,11,8,9,1,1,1,9,25,4,11,19,7,2,15,2,12,8,52,7,5,19,2,16,4,36,8,1,16,8,24,26,4,6,2,9,\n        5,4,36,3,28,12,25,15,37,27,17,12,59,38,5,32,127,1,2,9,17,14,4,1,2,1,1,8,11,50,4,14,2,19,16,4,17,5,4,5,26,12,45,2,23,45,104,30,12,8,3,10,2,2,\n        3,3,1,4,20,7,2,9,6,15,2,20,1,3,16,4,11,15,6,134,2,5,59,1,2,2,2,1,9,17,3,26,137,10,211,59,1,2,4,1,4,1,1,1,2,6,2,3,1,1,2,3,2,3,1,3,4,4,2,3,3,\n        1,4,3,1,7,2,2,3,1,2,1,3,3,3,2,2,3,2,1,3,14,6,1,3,2,9,6,15,27,9,34,145,1,1,2,1,1,1,1,2,1,1,1,1,2,2,2,3,1,2,1,1,1,2,3,5,8,3,5,2,4,1,3,2,2,2,12,\n        4,1,1,1,10,4,5,1,20,4,16,1,15,9,5,12,2,9,2,5,4,2,26,19,7,1,26,4,30,12,15,42,1,6,8,172,1,1,4,2,1,1,11,2,2,4,2,1,2,1,10,8,1,2,1,4,5,1,2,5,1,8,\n        4,1,3,4,2,1,6,2,1,3,4,1,2,1,1,1,1,12,5,7,2,4,3,1,1,1,3,3,6,1,2,2,3,3,3,2,1,2,12,14,11,6,6,4,12,2,8,1,7,10,1,35,7,4,13,15,4,3,23,21,28,52,5,\n        26,5,6,1,7,10,2,7,53,3,2,1,1,1,2,163,532,1,10,11,1,3,3,4,8,2,8,6,2,2,23,22,4,2,2,4,2,1,3,1,3,3,5,9,8,2,1,2,8,1,10,2,12,21,20,15,105,2,3,1,1,\n        3,2,3,1,1,2,5,1,4,15,11,19,1,1,1,1,5,4,5,1,1,2,5,3,5,12,1,2,5,1,11,1,1,15,9,1,4,5,3,26,8,2,1,3,1,1,15,19,2,12,1,2,5,2,7,2,19,2,20,6,26,7,5,\n        2,2,7,34,21,13,70,2,128,1,1,2,1,1,2,1,1,3,2,2,2,15,1,4,1,3,4,42,10,6,1,49,85,8,1,2,1,1,4,4,2,3,6,1,5,7,4,3,211,4,1,2,1,2,5,1,2,4,2,2,6,5,6,\n        10,3,4,48,100,6,2,16,296,5,27,387,2,2,3,7,16,8,5,38,15,39,21,9,10,3,7,59,13,27,21,47,5,21,6\n    };\n    static ImWchar base_ranges[] = // not zero-terminated\n    {\n        0x0020, 0x00FF, // Basic Latin + Latin Supplement\n        0x2000, 0x206F, // General Punctuation\n        0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana\n        0x31F0, 0x31FF, // Katakana Phonetic Extensions\n        0xFF00, 0xFFEF  // Half-width characters\n    };\n    static ImWchar full_ranges[IM_ARRAYSIZE(base_ranges) + IM_ARRAYSIZE(accumulative_offsets_from_0x4E00) * 2 + 1] = { 0 };\n    if (!full_ranges[0])\n    {\n        memcpy(full_ranges, base_ranges, sizeof(base_ranges));\n        UnpackAccumulativeOffsetsIntoRanges(0x4E00, accumulative_offsets_from_0x4E00, IM_ARRAYSIZE(accumulative_offsets_from_0x4E00), full_ranges + IM_ARRAYSIZE(base_ranges));\n    }\n    return &full_ranges[0];\n}\n\nconst ImWchar*  ImFontAtlas::GetGlyphRangesJapanese()\n{\n    // 1946 common ideograms code points for Japanese\n    // Sourced from http://theinstructionlimit.com/common-kanji-character-ranges-for-xna-spritefont-rendering\n    // FIXME: Source a list of the revised 2136 Joyo Kanji list from 2010 and rebuild this.\n    // You can use ImFontGlyphRangesBuilder to create your own ranges derived from this, by merging existing ranges or adding new characters.\n    // (Stored as accumulative offsets from the initial unicode codepoint 0x4E00. This encoding is designed to helps us compact the source code size.)\n    static const short accumulative_offsets_from_0x4E00[] =\n    {\n        0,1,2,4,1,1,1,1,2,1,6,2,2,1,8,5,7,11,1,2,10,10,8,2,4,20,2,11,8,2,1,2,1,6,2,1,7,5,3,7,1,1,13,7,9,1,4,6,1,2,1,10,1,1,9,2,2,4,5,6,14,1,1,9,3,18,\n        5,4,2,2,10,7,1,1,1,3,2,4,3,23,2,10,12,2,14,2,4,13,1,6,10,3,1,7,13,6,4,13,5,2,3,17,2,2,5,7,6,4,1,7,14,16,6,13,9,15,1,1,7,16,4,7,1,19,9,2,7,15,\n        2,6,5,13,25,4,14,13,11,25,1,1,1,2,1,2,2,3,10,11,3,3,1,1,4,4,2,1,4,9,1,4,3,5,5,2,7,12,11,15,7,16,4,5,16,2,1,1,6,3,3,1,1,2,7,6,6,7,1,4,7,6,1,1,\n        2,1,12,3,3,9,5,8,1,11,1,2,3,18,20,4,1,3,6,1,7,3,5,5,7,2,2,12,3,1,4,2,3,2,3,11,8,7,4,17,1,9,25,1,1,4,2,2,4,1,2,7,1,1,1,3,1,2,6,16,1,2,1,1,3,12,\n        20,2,5,20,8,7,6,2,1,1,1,1,6,2,1,2,10,1,1,6,1,3,1,2,1,4,1,12,4,1,3,1,1,1,1,1,10,4,7,5,13,1,15,1,1,30,11,9,1,15,38,14,1,32,17,20,1,9,31,2,21,9,\n        4,49,22,2,1,13,1,11,45,35,43,55,12,19,83,1,3,2,3,13,2,1,7,3,18,3,13,8,1,8,18,5,3,7,25,24,9,24,40,3,17,24,2,1,6,2,3,16,15,6,7,3,12,1,9,7,3,3,\n        3,15,21,5,16,4,5,12,11,11,3,6,3,2,31,3,2,1,1,23,6,6,1,4,2,6,5,2,1,1,3,3,22,2,6,2,3,17,3,2,4,5,1,9,5,1,1,6,15,12,3,17,2,14,2,8,1,23,16,4,2,23,\n        8,15,23,20,12,25,19,47,11,21,65,46,4,3,1,5,6,1,2,5,26,2,1,1,3,11,1,1,1,2,1,2,3,1,1,10,2,3,1,1,1,3,6,3,2,2,6,6,9,2,2,2,6,2,5,10,2,4,1,2,1,2,2,\n        3,1,1,3,1,2,9,23,9,2,1,1,1,1,5,3,2,1,10,9,6,1,10,2,31,25,3,7,5,40,1,15,6,17,7,27,180,1,3,2,2,1,1,1,6,3,10,7,1,3,6,17,8,6,2,2,1,3,5,5,8,16,14,\n        15,1,1,4,1,2,1,1,1,3,2,7,5,6,2,5,10,1,4,2,9,1,1,11,6,1,44,1,3,7,9,5,1,3,1,1,10,7,1,10,4,2,7,21,15,7,2,5,1,8,3,4,1,3,1,6,1,4,2,1,4,10,8,1,4,5,\n        1,5,10,2,7,1,10,1,1,3,4,11,10,29,4,7,3,5,2,3,33,5,2,19,3,1,4,2,6,31,11,1,3,3,3,1,8,10,9,12,11,12,8,3,14,8,6,11,1,4,41,3,1,2,7,13,1,5,6,2,6,12,\n        12,22,5,9,4,8,9,9,34,6,24,1,1,20,9,9,3,4,1,7,2,2,2,6,2,28,5,3,6,1,4,6,7,4,2,1,4,2,13,6,4,4,3,1,8,8,3,2,1,5,1,2,2,3,1,11,11,7,3,6,10,8,6,16,16,\n        22,7,12,6,21,5,4,6,6,3,6,1,3,2,1,2,8,29,1,10,1,6,13,6,6,19,31,1,13,4,4,22,17,26,33,10,4,15,12,25,6,67,10,2,3,1,6,10,2,6,2,9,1,9,4,4,1,2,16,2,\n        5,9,2,3,8,1,8,3,9,4,8,6,4,8,11,3,2,1,1,3,26,1,7,5,1,11,1,5,3,5,2,13,6,39,5,1,5,2,11,6,10,5,1,15,5,3,6,19,21,22,2,4,1,6,1,8,1,4,8,2,4,2,2,9,2,\n        1,1,1,4,3,6,3,12,7,1,14,2,4,10,2,13,1,17,7,3,2,1,3,2,13,7,14,12,3,1,29,2,8,9,15,14,9,14,1,3,1,6,5,9,11,3,38,43,20,7,7,8,5,15,12,19,15,81,8,7,\n        1,5,73,13,37,28,8,8,1,15,18,20,165,28,1,6,11,8,4,14,7,15,1,3,3,6,4,1,7,14,1,1,11,30,1,5,1,4,14,1,4,2,7,52,2,6,29,3,1,9,1,21,3,5,1,26,3,11,14,\n        11,1,17,5,1,2,1,3,2,8,1,2,9,12,1,1,2,3,8,3,24,12,7,7,5,17,3,3,3,1,23,10,4,4,6,3,1,16,17,22,3,10,21,16,16,6,4,10,2,1,1,2,8,8,6,5,3,3,3,39,25,\n        15,1,1,16,6,7,25,15,6,6,12,1,22,13,1,4,9,5,12,2,9,1,12,28,8,3,5,10,22,60,1,2,40,4,61,63,4,1,13,12,1,4,31,12,1,14,89,5,16,6,29,14,2,5,49,18,18,\n        5,29,33,47,1,17,1,19,12,2,9,7,39,12,3,7,12,39,3,1,46,4,12,3,8,9,5,31,15,18,3,2,2,66,19,13,17,5,3,46,124,13,57,34,2,5,4,5,8,1,1,1,4,3,1,17,5,\n        3,5,3,1,8,5,6,3,27,3,26,7,12,7,2,17,3,7,18,78,16,4,36,1,2,1,6,2,1,39,17,7,4,13,4,4,4,1,10,4,2,4,6,3,10,1,19,1,26,2,4,33,2,73,47,7,3,8,2,4,15,\n        18,1,29,2,41,14,1,21,16,41,7,39,25,13,44,2,2,10,1,13,7,1,7,3,5,20,4,8,2,49,1,10,6,1,6,7,10,7,11,16,3,12,20,4,10,3,1,2,11,2,28,9,2,4,7,2,15,1,\n        27,1,28,17,4,5,10,7,3,24,10,11,6,26,3,2,7,2,2,49,16,10,16,15,4,5,27,61,30,14,38,22,2,7,5,1,3,12,23,24,17,17,3,3,2,4,1,6,2,7,5,1,1,5,1,1,9,4,\n        1,3,6,1,8,2,8,4,14,3,5,11,4,1,3,32,1,19,4,1,13,11,5,2,1,8,6,8,1,6,5,13,3,23,11,5,3,16,3,9,10,1,24,3,198,52,4,2,2,5,14,5,4,22,5,20,4,11,6,41,\n        1,5,2,2,11,5,2,28,35,8,22,3,18,3,10,7,5,3,4,1,5,3,8,9,3,6,2,16,22,4,5,5,3,3,18,23,2,6,23,5,27,8,1,33,2,12,43,16,5,2,3,6,1,20,4,2,9,7,1,11,2,\n        10,3,14,31,9,3,25,18,20,2,5,5,26,14,1,11,17,12,40,19,9,6,31,83,2,7,9,19,78,12,14,21,76,12,113,79,34,4,1,1,61,18,85,10,2,2,13,31,11,50,6,33,159,\n        179,6,6,7,4,4,2,4,2,5,8,7,20,32,22,1,3,10,6,7,28,5,10,9,2,77,19,13,2,5,1,4,4,7,4,13,3,9,31,17,3,26,2,6,6,5,4,1,7,11,3,4,2,1,6,2,20,4,1,9,2,6,\n        3,7,1,1,1,20,2,3,1,6,2,3,6,2,4,8,1,5,13,8,4,11,23,1,10,6,2,1,3,21,2,2,4,24,31,4,10,10,2,5,192,15,4,16,7,9,51,1,2,1,1,5,1,1,2,1,3,5,3,1,3,4,1,\n        3,1,3,3,9,8,1,2,2,2,4,4,18,12,92,2,10,4,3,14,5,25,16,42,4,14,4,2,21,5,126,30,31,2,1,5,13,3,22,5,6,6,20,12,1,14,12,87,3,19,1,8,2,9,9,3,3,23,2,\n        3,7,6,3,1,2,3,9,1,3,1,6,3,2,1,3,11,3,1,6,10,3,2,3,1,2,1,5,1,1,11,3,6,4,1,7,2,1,2,5,5,34,4,14,18,4,19,7,5,8,2,6,79,1,5,2,14,8,2,9,2,1,36,28,16,\n        4,1,1,1,2,12,6,42,39,16,23,7,15,15,3,2,12,7,21,64,6,9,28,8,12,3,3,41,59,24,51,55,57,294,9,9,2,6,2,15,1,2,13,38,90,9,9,9,3,11,7,1,1,1,5,6,3,2,\n        1,2,2,3,8,1,4,4,1,5,7,1,4,3,20,4,9,1,1,1,5,5,17,1,5,2,6,2,4,1,4,5,7,3,18,11,11,32,7,5,4,7,11,127,8,4,3,3,1,10,1,1,6,21,14,1,16,1,7,1,3,6,9,65,\n        51,4,3,13,3,10,1,1,12,9,21,110,3,19,24,1,1,10,62,4,1,29,42,78,28,20,18,82,6,3,15,6,84,58,253,15,155,264,15,21,9,14,7,58,40,39,\n    };\n    static ImWchar base_ranges[] = // not zero-terminated\n    {\n        0x0020, 0x00FF, // Basic Latin + Latin Supplement\n        0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana\n        0x31F0, 0x31FF, // Katakana Phonetic Extensions\n        0xFF00, 0xFFEF  // Half-width characters\n    };\n    static ImWchar full_ranges[IM_ARRAYSIZE(base_ranges) + IM_ARRAYSIZE(accumulative_offsets_from_0x4E00)*2 + 1] = { 0 };\n    if (!full_ranges[0])\n    {\n        memcpy(full_ranges, base_ranges, sizeof(base_ranges));\n        UnpackAccumulativeOffsetsIntoRanges(0x4E00, accumulative_offsets_from_0x4E00, IM_ARRAYSIZE(accumulative_offsets_from_0x4E00), full_ranges + IM_ARRAYSIZE(base_ranges));\n    }\n    return &full_ranges[0];\n}\n\nconst ImWchar*  ImFontAtlas::GetGlyphRangesCyrillic()\n{\n    static const ImWchar ranges[] =\n    {\n        0x0020, 0x00FF, // Basic Latin + Latin Supplement\n        0x0400, 0x052F, // Cyrillic + Cyrillic Supplement\n        0x2DE0, 0x2DFF, // Cyrillic Extended-A\n        0xA640, 0xA69F, // Cyrillic Extended-B\n        0,\n    };\n    return &ranges[0];\n}\n\nconst ImWchar*  ImFontAtlas::GetGlyphRangesThai()\n{\n    static const ImWchar ranges[] =\n    {\n        0x0020, 0x00FF, // Basic Latin\n        0x2010, 0x205E, // Punctuations\n        0x0E00, 0x0E7F, // Thai\n        0,\n    };\n    return &ranges[0];\n}\n\nconst ImWchar*  ImFontAtlas::GetGlyphRangesVietnamese()\n{\n    static const ImWchar ranges[] =\n    {\n        0x0020, 0x00FF, // Basic Latin\n        0x0102, 0x0103,\n        0x0110, 0x0111,\n        0x0128, 0x0129,\n        0x0168, 0x0169,\n        0x01A0, 0x01A1,\n        0x01AF, 0x01B0,\n        0x1EA0, 0x1EF9,\n        0,\n    };\n    return &ranges[0];\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] ImFontGlyphRangesBuilder\n//-----------------------------------------------------------------------------\n\nvoid ImFontGlyphRangesBuilder::AddText(const char* text, const char* text_end)\n{\n    while (text_end ? (text < text_end) : *text)\n    {\n        unsigned int c = 0;\n        int c_len = ImTextCharFromUtf8(&c, text, text_end);\n        text += c_len;\n        if (c_len == 0)\n            break;\n        if (c < 0x10000)\n            AddChar((ImWchar)c);\n    }\n}\n\nvoid ImFontGlyphRangesBuilder::AddRanges(const ImWchar* ranges)\n{\n    for (; ranges[0]; ranges += 2)\n        for (ImWchar c = ranges[0]; c <= ranges[1]; c++)\n            AddChar(c);\n}\n\nvoid ImFontGlyphRangesBuilder::BuildRanges(ImVector<ImWchar>* out_ranges)\n{\n    for (int n = 0; n < 0x10000; n++)\n        if (GetBit(n))\n        {\n            out_ranges->push_back((ImWchar)n);\n            while (n < 0x10000 && GetBit(n + 1))\n                n++;\n            out_ranges->push_back((ImWchar)n);\n        }\n    out_ranges->push_back(0);\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] ImFont\n//-----------------------------------------------------------------------------\n\nImFont::ImFont()\n{\n    FontSize = 0.0f;\n    FallbackAdvanceX = 0.0f;\n    FallbackChar = (ImWchar)'?';\n    DisplayOffset = ImVec2(0.0f, 0.0f);\n    FallbackGlyph = NULL;\n    ContainerAtlas = NULL;\n    ConfigData = NULL;\n    ConfigDataCount = 0;\n    DirtyLookupTables = false;\n    Scale = 1.0f;\n    Ascent = Descent = 0.0f;\n    MetricsTotalSurface = 0;\n}\n\nImFont::~ImFont()\n{\n    ClearOutputData();\n}\n\nvoid    ImFont::ClearOutputData()\n{\n    FontSize = 0.0f;\n    FallbackAdvanceX = 0.0f;\n    Glyphs.clear();\n    IndexAdvanceX.clear();\n    IndexLookup.clear();\n    FallbackGlyph = NULL;\n    ContainerAtlas = NULL;\n    DirtyLookupTables = true;\n    Ascent = Descent = 0.0f;\n    MetricsTotalSurface = 0;\n}\n\nvoid ImFont::BuildLookupTable()\n{\n    int max_codepoint = 0;\n    for (int i = 0; i != Glyphs.Size; i++)\n        max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint);\n\n    IM_ASSERT(Glyphs.Size < 0xFFFF); // -1 is reserved\n    IndexAdvanceX.clear();\n    IndexLookup.clear();\n    DirtyLookupTables = false;\n    GrowIndex(max_codepoint + 1);\n    for (int i = 0; i < Glyphs.Size; i++)\n    {\n        int codepoint = (int)Glyphs[i].Codepoint;\n        IndexAdvanceX[codepoint] = Glyphs[i].AdvanceX;\n        IndexLookup[codepoint] = (ImWchar)i;\n    }\n\n    // Create a glyph to handle TAB\n    // FIXME: Needs proper TAB handling but it needs to be contextualized (or we could arbitrary say that each string starts at \"column 0\" ?)\n    if (FindGlyph((ImWchar)' '))\n    {\n        if (Glyphs.back().Codepoint != '\\t')   // So we can call this function multiple times\n            Glyphs.resize(Glyphs.Size + 1);\n        ImFontGlyph& tab_glyph = Glyphs.back();\n        tab_glyph = *FindGlyph((ImWchar)' ');\n        tab_glyph.Codepoint = '\\t';\n        tab_glyph.AdvanceX *= IM_TABSIZE;\n        IndexAdvanceX[(int)tab_glyph.Codepoint] = (float)tab_glyph.AdvanceX;\n        IndexLookup[(int)tab_glyph.Codepoint] = (ImWchar)(Glyphs.Size-1);\n    }\n\n    FallbackGlyph = FindGlyphNoFallback(FallbackChar);\n    FallbackAdvanceX = FallbackGlyph ? FallbackGlyph->AdvanceX : 0.0f;\n    for (int i = 0; i < max_codepoint + 1; i++)\n        if (IndexAdvanceX[i] < 0.0f)\n            IndexAdvanceX[i] = FallbackAdvanceX;\n}\n\nvoid ImFont::SetFallbackChar(ImWchar c)\n{\n    FallbackChar = c;\n    BuildLookupTable();\n}\n\nvoid ImFont::GrowIndex(int new_size)\n{\n    IM_ASSERT(IndexAdvanceX.Size == IndexLookup.Size);\n    if (new_size <= IndexLookup.Size)\n        return;\n    IndexAdvanceX.resize(new_size, -1.0f);\n    IndexLookup.resize(new_size, (ImWchar)-1);\n}\n\n// x0/y0/x1/y1 are offset from the character upper-left layout position, in pixels. Therefore x0/y0 are often fairly close to zero.\n// Not to be mistaken with texture coordinates, which are held by u0/v0/u1/v1 in normalized format (0.0..1.0 on each texture axis).\nvoid ImFont::AddGlyph(ImWchar codepoint, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x)\n{\n    Glyphs.resize(Glyphs.Size + 1);\n    ImFontGlyph& glyph = Glyphs.back();\n    glyph.Codepoint = (ImWchar)codepoint;\n    glyph.X0 = x0;\n    glyph.Y0 = y0;\n    glyph.X1 = x1;\n    glyph.Y1 = y1;\n    glyph.U0 = u0;\n    glyph.V0 = v0;\n    glyph.U1 = u1;\n    glyph.V1 = v1;\n    glyph.AdvanceX = advance_x + ConfigData->GlyphExtraSpacing.x;  // Bake spacing into AdvanceX\n\n    if (ConfigData->PixelSnapH)\n        glyph.AdvanceX = (float)(int)(glyph.AdvanceX + 0.5f);\n\n    // Compute rough surface usage metrics (+1 to account for average padding, +0.99 to round)\n    DirtyLookupTables = true;\n    MetricsTotalSurface += (int)((glyph.U1 - glyph.U0) * ContainerAtlas->TexWidth + 1.99f) * (int)((glyph.V1 - glyph.V0) * ContainerAtlas->TexHeight + 1.99f);\n}\n\nvoid ImFont::AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst)\n{\n    IM_ASSERT(IndexLookup.Size > 0);    // Currently this can only be called AFTER the font has been built, aka after calling ImFontAtlas::GetTexDataAs*() function.\n    int index_size = IndexLookup.Size;\n\n    if (dst < index_size && IndexLookup.Data[dst] == (ImWchar)-1 && !overwrite_dst) // 'dst' already exists\n        return;\n    if (src >= index_size && dst >= index_size) // both 'dst' and 'src' don't exist -> no-op\n        return;\n\n    GrowIndex(dst + 1);\n    IndexLookup[dst] = (src < index_size) ? IndexLookup.Data[src] : (ImWchar)-1;\n    IndexAdvanceX[dst] = (src < index_size) ? IndexAdvanceX.Data[src] : 1.0f;\n}\n\nconst ImFontGlyph* ImFont::FindGlyph(ImWchar c) const\n{\n    if (c >= IndexLookup.Size)\n        return FallbackGlyph;\n    const ImWchar i = IndexLookup.Data[c];\n    if (i == (ImWchar)-1)\n        return FallbackGlyph;\n    return &Glyphs.Data[i];\n}\n\nconst ImFontGlyph* ImFont::FindGlyphNoFallback(ImWchar c) const\n{\n    if (c >= IndexLookup.Size)\n        return NULL;\n    const ImWchar i = IndexLookup.Data[c];\n    if (i == (ImWchar)-1)\n        return NULL;\n    return &Glyphs.Data[i];\n}\n\nconst char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const\n{\n    // Simple word-wrapping for English, not full-featured. Please submit failing cases!\n    // FIXME: Much possible improvements (don't cut things like \"word !\", \"word!!!\" but cut within \"word,,,,\", more sensible support for punctuations, support for Unicode punctuations, etc.)\n\n    // For references, possible wrap point marked with ^\n    //  \"aaa bbb, ccc,ddd. eee   fff. ggg!\"\n    //      ^    ^    ^   ^   ^__    ^    ^\n\n    // List of hardcoded separators: .,;!?'\"\n\n    // Skip extra blanks after a line returns (that includes not counting them in width computation)\n    // e.g. \"Hello    world\" --> \"Hello\" \"World\"\n\n    // Cut words that cannot possibly fit within one line.\n    // e.g.: \"The tropical fish\" with ~5 characters worth of width --> \"The tr\" \"opical\" \"fish\"\n\n    float line_width = 0.0f;\n    float word_width = 0.0f;\n    float blank_width = 0.0f;\n    wrap_width /= scale; // We work with unscaled widths to avoid scaling every characters\n\n    const char* word_end = text;\n    const char* prev_word_end = NULL;\n    bool inside_word = true;\n\n    const char* s = text;\n    while (s < text_end)\n    {\n        unsigned int c = (unsigned int)*s;\n        const char* next_s;\n        if (c < 0x80)\n            next_s = s + 1;\n        else\n            next_s = s + ImTextCharFromUtf8(&c, s, text_end);\n        if (c == 0)\n            break;\n\n        if (c < 32)\n        {\n            if (c == '\\n')\n            {\n                line_width = word_width = blank_width = 0.0f;\n                inside_word = true;\n                s = next_s;\n                continue;\n            }\n            if (c == '\\r')\n            {\n                s = next_s;\n                continue;\n            }\n        }\n\n        const float char_width = ((int)c < IndexAdvanceX.Size ? IndexAdvanceX.Data[c] : FallbackAdvanceX);\n        if (ImCharIsBlankW(c))\n        {\n            if (inside_word)\n            {\n                line_width += blank_width;\n                blank_width = 0.0f;\n                word_end = s;\n            }\n            blank_width += char_width;\n            inside_word = false;\n        }\n        else\n        {\n            word_width += char_width;\n            if (inside_word)\n            {\n                word_end = next_s;\n            }\n            else\n            {\n                prev_word_end = word_end;\n                line_width += word_width + blank_width;\n                word_width = blank_width = 0.0f;\n            }\n\n            // Allow wrapping after punctuation.\n            inside_word = !(c == '.' || c == ',' || c == ';' || c == '!' || c == '?' || c == '\\\"');\n        }\n\n        // We ignore blank width at the end of the line (they can be skipped)\n        if (line_width + word_width >= wrap_width)\n        {\n            // Words that cannot possibly fit within an entire line will be cut anywhere.\n            if (word_width < wrap_width)\n                s = prev_word_end ? prev_word_end : word_end;\n            break;\n        }\n\n        s = next_s;\n    }\n\n    return s;\n}\n\nImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const\n{\n    if (!text_end)\n        text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this.\n\n    const float line_height = size;\n    const float scale = size / FontSize;\n\n    ImVec2 text_size = ImVec2(0,0);\n    float line_width = 0.0f;\n\n    const bool word_wrap_enabled = (wrap_width > 0.0f);\n    const char* word_wrap_eol = NULL;\n\n    const char* s = text_begin;\n    while (s < text_end)\n    {\n        if (word_wrap_enabled)\n        {\n            // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature.\n            if (!word_wrap_eol)\n            {\n                word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width);\n                if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity.\n                    word_wrap_eol++;    // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below\n            }\n\n            if (s >= word_wrap_eol)\n            {\n                if (text_size.x < line_width)\n                    text_size.x = line_width;\n                text_size.y += line_height;\n                line_width = 0.0f;\n                word_wrap_eol = NULL;\n\n                // Wrapping skips upcoming blanks\n                while (s < text_end)\n                {\n                    const char c = *s;\n                    if (ImCharIsBlankA(c)) { s++; } else if (c == '\\n') { s++; break; } else { break; }\n                }\n                continue;\n            }\n        }\n\n        // Decode and advance source\n        const char* prev_s = s;\n        unsigned int c = (unsigned int)*s;\n        if (c < 0x80)\n        {\n            s += 1;\n        }\n        else\n        {\n            s += ImTextCharFromUtf8(&c, s, text_end);\n            if (c == 0) // Malformed UTF-8?\n                break;\n        }\n\n        if (c < 32)\n        {\n            if (c == '\\n')\n            {\n                text_size.x = ImMax(text_size.x, line_width);\n                text_size.y += line_height;\n                line_width = 0.0f;\n                continue;\n            }\n            if (c == '\\r')\n                continue;\n        }\n\n        const float char_width = ((int)c < IndexAdvanceX.Size ? IndexAdvanceX.Data[c] : FallbackAdvanceX) * scale;\n        if (line_width + char_width >= max_width)\n        {\n            s = prev_s;\n            break;\n        }\n\n        line_width += char_width;\n    }\n\n    if (text_size.x < line_width)\n        text_size.x = line_width;\n\n    if (line_width > 0 || text_size.y == 0.0f)\n        text_size.y += line_height;\n\n    if (remaining)\n        *remaining = s;\n\n    return text_size;\n}\n\nvoid ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const\n{\n    if (c == ' ' || c == '\\t' || c == '\\n' || c == '\\r') // Match behavior of RenderText(), those 4 codepoints are hard-coded.\n        return;\n    if (const ImFontGlyph* glyph = FindGlyph(c))\n    {\n        float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f;\n        pos.x = (float)(int)pos.x + DisplayOffset.x;\n        pos.y = (float)(int)pos.y + DisplayOffset.y;\n        draw_list->PrimReserve(6, 4);\n        draw_list->PrimRectUV(ImVec2(pos.x + glyph->X0 * scale, pos.y + glyph->Y0 * scale), ImVec2(pos.x + glyph->X1 * scale, pos.y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col);\n    }\n}\n\nvoid ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const\n{\n    if (!text_end)\n        text_end = text_begin + strlen(text_begin); // ImGui functions generally already provides a valid text_end, so this is merely to handle direct calls.\n\n    // Align to be pixel perfect\n    pos.x = (float)(int)pos.x + DisplayOffset.x;\n    pos.y = (float)(int)pos.y + DisplayOffset.y;\n    float x = pos.x;\n    float y = pos.y;\n    if (y > clip_rect.w)\n        return;\n\n    const float scale = size / FontSize;\n    const float line_height = FontSize * scale;\n    const bool word_wrap_enabled = (wrap_width > 0.0f);\n    const char* word_wrap_eol = NULL;\n\n    // Fast-forward to first visible line\n    const char* s = text_begin;\n    if (y + line_height < clip_rect.y && !word_wrap_enabled)\n        while (y + line_height < clip_rect.y && s < text_end)\n        {\n            s = (const char*)memchr(s, '\\n', text_end - s);\n            s = s ? s + 1 : text_end;\n            y += line_height;\n        }\n\n    // For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve()\n    // Note that very large horizontal line will still be affected by the issue (e.g. a one megabyte string buffer without a newline will likely crash atm)\n    if (text_end - s > 10000 && !word_wrap_enabled)\n    {\n        const char* s_end = s;\n        float y_end = y;\n        while (y_end < clip_rect.w && s_end < text_end)\n        {\n            s_end = (const char*)memchr(s_end, '\\n', text_end - s_end);\n            s_end = s_end ? s_end + 1 : text_end;\n            y_end += line_height;\n        }\n        text_end = s_end;\n    }\n    if (s == text_end)\n        return;\n\n    // Reserve vertices for remaining worse case (over-reserving is useful and easily amortized)\n    const int vtx_count_max = (int)(text_end - s) * 4;\n    const int idx_count_max = (int)(text_end - s) * 6;\n    const int idx_expected_size = draw_list->IdxBuffer.Size + idx_count_max;\n    draw_list->PrimReserve(idx_count_max, vtx_count_max);\n\n    ImDrawVert* vtx_write = draw_list->_VtxWritePtr;\n    ImDrawIdx* idx_write = draw_list->_IdxWritePtr;\n    unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx;\n\n    while (s < text_end)\n    {\n        if (word_wrap_enabled)\n        {\n            // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature.\n            if (!word_wrap_eol)\n            {\n                word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - (x - pos.x));\n                if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity.\n                    word_wrap_eol++;    // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below\n            }\n\n            if (s >= word_wrap_eol)\n            {\n                x = pos.x;\n                y += line_height;\n                word_wrap_eol = NULL;\n\n                // Wrapping skips upcoming blanks\n                while (s < text_end)\n                {\n                    const char c = *s;\n                    if (ImCharIsBlankA(c)) { s++; } else if (c == '\\n') { s++; break; } else { break; }\n                }\n                continue;\n            }\n        }\n\n        // Decode and advance source\n        unsigned int c = (unsigned int)*s;\n        if (c < 0x80)\n        {\n            s += 1;\n        }\n        else\n        {\n            s += ImTextCharFromUtf8(&c, s, text_end);\n            if (c == 0) // Malformed UTF-8?\n                break;\n        }\n\n        if (c < 32)\n        {\n            if (c == '\\n')\n            {\n                x = pos.x;\n                y += line_height;\n                if (y > clip_rect.w)\n                    break; // break out of main loop\n                continue;\n            }\n            if (c == '\\r')\n                continue;\n        }\n\n        float char_width = 0.0f;\n        if (const ImFontGlyph* glyph = FindGlyph((ImWchar)c))\n        {\n            char_width = glyph->AdvanceX * scale;\n\n            // Arbitrarily assume that both space and tabs are empty glyphs as an optimization\n            if (c != ' ' && c != '\\t')\n            {\n                // We don't do a second finer clipping test on the Y axis as we've already skipped anything before clip_rect.y and exit once we pass clip_rect.w\n                float x1 = x + glyph->X0 * scale;\n                float x2 = x + glyph->X1 * scale;\n                float y1 = y + glyph->Y0 * scale;\n                float y2 = y + glyph->Y1 * scale;\n                if (x1 <= clip_rect.z && x2 >= clip_rect.x)\n                {\n                    // Render a character\n                    float u1 = glyph->U0;\n                    float v1 = glyph->V0;\n                    float u2 = glyph->U1;\n                    float v2 = glyph->V1;\n\n                    // CPU side clipping used to fit text in their frame when the frame is too small. Only does clipping for axis aligned quads.\n                    if (cpu_fine_clip)\n                    {\n                        if (x1 < clip_rect.x)\n                        {\n                            u1 = u1 + (1.0f - (x2 - clip_rect.x) / (x2 - x1)) * (u2 - u1);\n                            x1 = clip_rect.x;\n                        }\n                        if (y1 < clip_rect.y)\n                        {\n                            v1 = v1 + (1.0f - (y2 - clip_rect.y) / (y2 - y1)) * (v2 - v1);\n                            y1 = clip_rect.y;\n                        }\n                        if (x2 > clip_rect.z)\n                        {\n                            u2 = u1 + ((clip_rect.z - x1) / (x2 - x1)) * (u2 - u1);\n                            x2 = clip_rect.z;\n                        }\n                        if (y2 > clip_rect.w)\n                        {\n                            v2 = v1 + ((clip_rect.w - y1) / (y2 - y1)) * (v2 - v1);\n                            y2 = clip_rect.w;\n                        }\n                        if (y1 >= y2)\n                        {\n                            x += char_width;\n                            continue;\n                        }\n                    }\n\n                    // We are NOT calling PrimRectUV() here because non-inlined causes too much overhead in a debug builds. Inlined here:\n                    {\n                        idx_write[0] = (ImDrawIdx)(vtx_current_idx); idx_write[1] = (ImDrawIdx)(vtx_current_idx+1); idx_write[2] = (ImDrawIdx)(vtx_current_idx+2);\n                        idx_write[3] = (ImDrawIdx)(vtx_current_idx); idx_write[4] = (ImDrawIdx)(vtx_current_idx+2); idx_write[5] = (ImDrawIdx)(vtx_current_idx+3);\n                        vtx_write[0].pos.x = x1; vtx_write[0].pos.y = y1; vtx_write[0].col = col; vtx_write[0].uv.x = u1; vtx_write[0].uv.y = v1;\n                        vtx_write[1].pos.x = x2; vtx_write[1].pos.y = y1; vtx_write[1].col = col; vtx_write[1].uv.x = u2; vtx_write[1].uv.y = v1;\n                        vtx_write[2].pos.x = x2; vtx_write[2].pos.y = y2; vtx_write[2].col = col; vtx_write[2].uv.x = u2; vtx_write[2].uv.y = v2;\n                        vtx_write[3].pos.x = x1; vtx_write[3].pos.y = y2; vtx_write[3].col = col; vtx_write[3].uv.x = u1; vtx_write[3].uv.y = v2;\n                        vtx_write += 4;\n                        vtx_current_idx += 4;\n                        idx_write += 6;\n                    }\n                }\n            }\n        }\n\n        x += char_width;\n    }\n\n    // Give back unused vertices\n    draw_list->VtxBuffer.resize((int)(vtx_write - draw_list->VtxBuffer.Data));\n    draw_list->IdxBuffer.resize((int)(idx_write - draw_list->IdxBuffer.Data));\n    draw_list->CmdBuffer[draw_list->CmdBuffer.Size-1].ElemCount -= (idx_expected_size - draw_list->IdxBuffer.Size);\n    draw_list->_VtxWritePtr = vtx_write;\n    draw_list->_IdxWritePtr = idx_write;\n    draw_list->_VtxCurrentIdx = (unsigned int)draw_list->VtxBuffer.Size;\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] Internal Render Helpers\n// (progressively moved from imgui.cpp to here when they are redesigned to stop accessing ImGui global state)\n//-----------------------------------------------------------------------------\n// - RenderMouseCursor()\n// - RenderArrowDockMenu()\n// - RenderArrowPointingAt()\n// - RenderRectFilledRangeH()\n// - RenderRectFilledWithHole()\n// - RenderPixelEllipsis()\n//-----------------------------------------------------------------------------\n\nvoid ImGui::RenderMouseCursor(ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor)\n{\n    if (mouse_cursor == ImGuiMouseCursor_None)\n        return;\n    IM_ASSERT(mouse_cursor > ImGuiMouseCursor_None && mouse_cursor < ImGuiMouseCursor_COUNT);\n\n    const ImU32 col_shadow = IM_COL32(0, 0, 0, 48);\n    const ImU32 col_border = IM_COL32(0, 0, 0, 255);          // Black\n    const ImU32 col_fill   = IM_COL32(255, 255, 255, 255);    // White\n\n    ImGuiContext& g = *GImGui;\n    ImFontAtlas* font_atlas = g.IO.Fonts;\n    ImVec2 offset, size, uv[4];\n    if (font_atlas->GetMouseCursorTexData(mouse_cursor, &offset, &size, &uv[0], &uv[2]))\n    {\n        pos -= offset;\n        const ImTextureID tex_id = font_atlas->TexID;\n\n        // We need to account for the possibility of the mouse cursor straddling multiple viewports...\n        for (int viewport_n = 0; viewport_n < g.Viewports.Size; viewport_n++)\n        {\n            ImGuiViewportP* viewport = g.Viewports[viewport_n];\n            if (!viewport->GetRect().Overlaps(ImRect(pos, pos + ImVec2(size.x + 2, size.y + 2) * scale)))\n                continue;\n\n            ImDrawList* draw_list = GetForegroundDrawList(viewport);\n            draw_list->PushTextureID(tex_id);\n            draw_list->AddImage(tex_id, pos + ImVec2(1,0)*scale, pos + ImVec2(1,0)*scale + size*scale, uv[2], uv[3], col_shadow);\n            draw_list->AddImage(tex_id, pos + ImVec2(2,0)*scale, pos + ImVec2(2,0)*scale + size*scale, uv[2], uv[3], col_shadow);\n            draw_list->AddImage(tex_id, pos,                     pos + size*scale,                     uv[2], uv[3], col_border);\n            draw_list->AddImage(tex_id, pos,                     pos + size*scale,                     uv[0], uv[1], col_fill);\n            draw_list->PopTextureID();\n        }\n    }\n}\n\n// Render an arrow. 'pos' is position of the arrow tip. half_sz.x is length from base to tip. half_sz.y is length on each side.\nvoid ImGui::RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col)\n{\n    switch (direction)\n    {\n    case ImGuiDir_Left:  draw_list->AddTriangleFilled(ImVec2(pos.x + half_sz.x, pos.y - half_sz.y), ImVec2(pos.x + half_sz.x, pos.y + half_sz.y), pos, col); return;\n    case ImGuiDir_Right: draw_list->AddTriangleFilled(ImVec2(pos.x - half_sz.x, pos.y + half_sz.y), ImVec2(pos.x - half_sz.x, pos.y - half_sz.y), pos, col); return;\n    case ImGuiDir_Up:    draw_list->AddTriangleFilled(ImVec2(pos.x + half_sz.x, pos.y + half_sz.y), ImVec2(pos.x - half_sz.x, pos.y + half_sz.y), pos, col); return;\n    case ImGuiDir_Down:  draw_list->AddTriangleFilled(ImVec2(pos.x - half_sz.x, pos.y - half_sz.y), ImVec2(pos.x + half_sz.x, pos.y - half_sz.y), pos, col); return;\n    case ImGuiDir_None: case ImGuiDir_COUNT: break; // Fix warnings\n    }\n}\n\n// This is less wide than RenderArrow() and we use in dock nodes instead of the regular RenderArrow() to denote a change of functionality,\n// and because the saved space means that the left-most tab label can stay at exactly the same position as the label of a loose window.\nvoid ImGui::RenderArrowDockMenu(ImDrawList* draw_list, ImVec2 p_min, float sz, ImU32 col)\n{\n    draw_list->AddRectFilled(p_min + ImVec2(sz * 0.10f, sz * 0.15f), p_min + ImVec2(sz * 0.70f, sz * 0.30f), col);\n    RenderArrowPointingAt(draw_list, p_min + ImVec2(sz * 0.40f, sz * 0.85f), ImVec2(sz * 0.30f, sz * 0.40f), ImGuiDir_Down, col);\n}\n\nstatic inline float ImAcos01(float x)\n{\n    if (x <= 0.0f) return IM_PI * 0.5f;\n    if (x >= 1.0f) return 0.0f;\n    return ImAcos(x);\n    //return (-0.69813170079773212f * x * x - 0.87266462599716477f) * x + 1.5707963267948966f; // Cheap approximation, may be enough for what we do.\n}\n\n// FIXME: Cleanup and move code to ImDrawList.\nvoid ImGui::RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding)\n{\n    if (x_end_norm == x_start_norm)\n        return;\n    if (x_start_norm > x_end_norm)\n        ImSwap(x_start_norm, x_end_norm);\n\n    ImVec2 p0 = ImVec2(ImLerp(rect.Min.x, rect.Max.x, x_start_norm), rect.Min.y);\n    ImVec2 p1 = ImVec2(ImLerp(rect.Min.x, rect.Max.x, x_end_norm), rect.Max.y);\n    if (rounding == 0.0f)\n    {\n        draw_list->AddRectFilled(p0, p1, col, 0.0f);\n        return;\n    }\n\n    rounding = ImClamp(ImMin((rect.Max.x - rect.Min.x) * 0.5f, (rect.Max.y - rect.Min.y) * 0.5f) - 1.0f, 0.0f, rounding);\n    const float inv_rounding = 1.0f / rounding;\n    const float arc0_b = ImAcos01(1.0f - (p0.x - rect.Min.x) * inv_rounding);\n    const float arc0_e = ImAcos01(1.0f - (p1.x - rect.Min.x) * inv_rounding);\n    const float half_pi = IM_PI * 0.5f; // We will == compare to this because we know this is the exact value ImAcos01 can return.\n    const float x0 = ImMax(p0.x, rect.Min.x + rounding);\n    if (arc0_b == arc0_e)\n    {\n        draw_list->PathLineTo(ImVec2(x0, p1.y));\n        draw_list->PathLineTo(ImVec2(x0, p0.y));\n    }\n    else if (arc0_b == 0.0f && arc0_e == half_pi)\n    {\n        draw_list->PathArcToFast(ImVec2(x0, p1.y - rounding), rounding, 3, 6); // BL\n        draw_list->PathArcToFast(ImVec2(x0, p0.y + rounding), rounding, 6, 9); // TR\n    }\n    else\n    {\n        draw_list->PathArcTo(ImVec2(x0, p1.y - rounding), rounding, IM_PI - arc0_e, IM_PI - arc0_b, 3); // BL\n        draw_list->PathArcTo(ImVec2(x0, p0.y + rounding), rounding, IM_PI + arc0_b, IM_PI + arc0_e, 3); // TR\n    }\n    if (p1.x > rect.Min.x + rounding)\n    {\n        const float arc1_b = ImAcos01(1.0f - (rect.Max.x - p1.x) * inv_rounding);\n        const float arc1_e = ImAcos01(1.0f - (rect.Max.x - p0.x) * inv_rounding);\n        const float x1 = ImMin(p1.x, rect.Max.x - rounding);\n        if (arc1_b == arc1_e)\n        {\n            draw_list->PathLineTo(ImVec2(x1, p0.y));\n            draw_list->PathLineTo(ImVec2(x1, p1.y));\n        }\n        else if (arc1_b == 0.0f && arc1_e == half_pi)\n        {\n            draw_list->PathArcToFast(ImVec2(x1, p0.y + rounding), rounding, 9, 12); // TR\n            draw_list->PathArcToFast(ImVec2(x1, p1.y - rounding), rounding, 0, 3);  // BR\n        }\n        else\n        {\n            draw_list->PathArcTo(ImVec2(x1, p0.y + rounding), rounding, -arc1_e, -arc1_b, 3); // TR\n            draw_list->PathArcTo(ImVec2(x1, p1.y - rounding), rounding, +arc1_b, +arc1_e, 3); // BR\n        }\n    }\n    draw_list->PathFillConvex(col);\n}\n\n// For CTRL+TAB within a docking node we need to render the dimming background in 8 steps\n// (Because the root node renders the background in one shot, in order to avoid flickering when a child dock node is not submitted)\nvoid ImGui::RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect inner, ImU32 col, float rounding)\n{\n    const bool fill_L = (inner.Min.x > outer.Min.x);\n    const bool fill_R = (inner.Max.x < outer.Max.x);\n    const bool fill_U = (inner.Min.y > outer.Min.y);\n    const bool fill_D = (inner.Max.y < outer.Max.y);\n    if (fill_L) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Min.y), ImVec2(inner.Min.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawCornerFlags_TopLeft) | (fill_D ? 0 : ImDrawCornerFlags_BotLeft));\n    if (fill_R) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Min.y), ImVec2(outer.Max.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawCornerFlags_TopRight) | (fill_D ? 0 : ImDrawCornerFlags_BotRight));\n    if (fill_U) draw_list->AddRectFilled(ImVec2(inner.Min.x, outer.Min.y), ImVec2(inner.Max.x, inner.Min.y), col, rounding, (fill_L ? 0 : ImDrawCornerFlags_TopLeft) | (fill_R ? 0 : ImDrawCornerFlags_TopRight));\n    if (fill_D) draw_list->AddRectFilled(ImVec2(inner.Min.x, inner.Max.y), ImVec2(inner.Max.x, outer.Max.y), col, rounding, (fill_L ? 0 : ImDrawCornerFlags_BotLeft) | (fill_R ? 0 : ImDrawCornerFlags_BotRight));\n    if (fill_L && fill_U) draw_list->AddRectFilled(ImVec2(outer.Min.x, outer.Min.y), ImVec2(inner.Min.x, inner.Min.y), col, rounding, ImDrawCornerFlags_TopLeft);\n    if (fill_R && fill_U) draw_list->AddRectFilled(ImVec2(inner.Max.x, outer.Min.y), ImVec2(outer.Max.x, inner.Min.y), col, rounding, ImDrawCornerFlags_TopRight);\n    if (fill_L && fill_D) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Max.y), ImVec2(inner.Min.x, outer.Max.y), col, rounding, ImDrawCornerFlags_BotLeft);\n    if (fill_R && fill_D) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Max.y), ImVec2(outer.Max.x, outer.Max.y), col, rounding, ImDrawCornerFlags_BotRight);\n}\n\n// FIXME: Rendering an ellipsis \"...\" is a surprisingly tricky problem for us... we cannot rely on font glyph having it,\n// and regular dot are typically too wide. If we render a dot/shape ourselves it comes with the risk that it wouldn't match\n// the boldness or positioning of what the font uses...\nvoid ImGui::RenderPixelEllipsis(ImDrawList* draw_list, ImVec2 pos, int count, ImU32 col)\n{\n    ImFont* font = draw_list->_Data->Font;\n    const float font_scale = draw_list->_Data->FontSize / font->FontSize;\n    pos.y += (float)(int)(font->DisplayOffset.y + font->Ascent * font_scale + 0.5f - 1.0f);\n    for (int dot_n = 0; dot_n < count; dot_n++)\n        draw_list->AddRectFilled(ImVec2(pos.x + dot_n * 2.0f, pos.y), ImVec2(pos.x + dot_n * 2.0f + 1.0f, pos.y + 1.0f), col);\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] Decompression code\n//-----------------------------------------------------------------------------\n// Compressed with stb_compress() then converted to a C array and encoded as base85.\n// Use the program in misc/fonts/binary_to_compressed_c.cpp to create the array from a TTF file.\n// The purpose of encoding as base85 instead of \"0x00,0x01,...\" style is only save on _source code_ size.\n// Decompression from stb.h (public domain) by Sean Barrett https://github.com/nothings/stb/blob/master/stb.h\n//-----------------------------------------------------------------------------\n\nstatic unsigned int stb_decompress_length(const unsigned char *input)\n{\n    return (input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11];\n}\n\nstatic unsigned char *stb__barrier_out_e, *stb__barrier_out_b;\nstatic const unsigned char *stb__barrier_in_b;\nstatic unsigned char *stb__dout;\nstatic void stb__match(const unsigned char *data, unsigned int length)\n{\n    // INVERSE of memmove... write each byte before copying the next...\n    IM_ASSERT(stb__dout + length <= stb__barrier_out_e);\n    if (stb__dout + length > stb__barrier_out_e) { stb__dout += length; return; }\n    if (data < stb__barrier_out_b) { stb__dout = stb__barrier_out_e+1; return; }\n    while (length--) *stb__dout++ = *data++;\n}\n\nstatic void stb__lit(const unsigned char *data, unsigned int length)\n{\n    IM_ASSERT(stb__dout + length <= stb__barrier_out_e);\n    if (stb__dout + length > stb__barrier_out_e) { stb__dout += length; return; }\n    if (data < stb__barrier_in_b) { stb__dout = stb__barrier_out_e+1; return; }\n    memcpy(stb__dout, data, length);\n    stb__dout += length;\n}\n\n#define stb__in2(x)   ((i[x] << 8) + i[(x)+1])\n#define stb__in3(x)   ((i[x] << 16) + stb__in2((x)+1))\n#define stb__in4(x)   ((i[x] << 24) + stb__in3((x)+1))\n\nstatic const unsigned char *stb_decompress_token(const unsigned char *i)\n{\n    if (*i >= 0x20) { // use fewer if's for cases that expand small\n        if (*i >= 0x80)       stb__match(stb__dout-i[1]-1, i[0] - 0x80 + 1), i += 2;\n        else if (*i >= 0x40)  stb__match(stb__dout-(stb__in2(0) - 0x4000 + 1), i[2]+1), i += 3;\n        else /* *i >= 0x20 */ stb__lit(i+1, i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);\n    } else { // more ifs for cases that expand large, since overhead is amortized\n        if (*i >= 0x18)       stb__match(stb__dout-(stb__in3(0) - 0x180000 + 1), i[3]+1), i += 4;\n        else if (*i >= 0x10)  stb__match(stb__dout-(stb__in3(0) - 0x100000 + 1), stb__in2(3)+1), i += 5;\n        else if (*i >= 0x08)  stb__lit(i+2, stb__in2(0) - 0x0800 + 1), i += 2 + (stb__in2(0) - 0x0800 + 1);\n        else if (*i == 0x07)  stb__lit(i+3, stb__in2(1) + 1), i += 3 + (stb__in2(1) + 1);\n        else if (*i == 0x06)  stb__match(stb__dout-(stb__in3(1)+1), i[4]+1), i += 5;\n        else if (*i == 0x04)  stb__match(stb__dout-(stb__in3(1)+1), stb__in2(4)+1), i += 6;\n    }\n    return i;\n}\n\nstatic unsigned int stb_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen)\n{\n    const unsigned long ADLER_MOD = 65521;\n    unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;\n    unsigned long blocklen, i;\n\n    blocklen = buflen % 5552;\n    while (buflen) {\n        for (i=0; i + 7 < blocklen; i += 8) {\n            s1 += buffer[0], s2 += s1;\n            s1 += buffer[1], s2 += s1;\n            s1 += buffer[2], s2 += s1;\n            s1 += buffer[3], s2 += s1;\n            s1 += buffer[4], s2 += s1;\n            s1 += buffer[5], s2 += s1;\n            s1 += buffer[6], s2 += s1;\n            s1 += buffer[7], s2 += s1;\n\n            buffer += 8;\n        }\n\n        for (; i < blocklen; ++i)\n            s1 += *buffer++, s2 += s1;\n\n        s1 %= ADLER_MOD, s2 %= ADLER_MOD;\n        buflen -= blocklen;\n        blocklen = 5552;\n    }\n    return (unsigned int)(s2 << 16) + (unsigned int)s1;\n}\n\nstatic unsigned int stb_decompress(unsigned char *output, const unsigned char *i, unsigned int /*length*/)\n{\n    unsigned int olen;\n    if (stb__in4(0) != 0x57bC0000) return 0;\n    if (stb__in4(4) != 0)          return 0; // error! stream is > 4GB\n    olen = stb_decompress_length(i);\n    stb__barrier_in_b = i;\n    stb__barrier_out_e = output + olen;\n    stb__barrier_out_b = output;\n    i += 16;\n\n    stb__dout = output;\n    for (;;) {\n        const unsigned char *old_i = i;\n        i = stb_decompress_token(i);\n        if (i == old_i) {\n            if (*i == 0x05 && i[1] == 0xfa) {\n                IM_ASSERT(stb__dout == output + olen);\n                if (stb__dout != output + olen) return 0;\n                if (stb_adler32(1, output, olen) != (unsigned int) stb__in4(2))\n                    return 0;\n                return olen;\n            } else {\n                IM_ASSERT(0); /* NOTREACHED */\n                return 0;\n            }\n        }\n        IM_ASSERT(stb__dout <= output + olen);\n        if (stb__dout > output + olen)\n            return 0;\n    }\n}\n\n//-----------------------------------------------------------------------------\n// [SECTION] Default font data (ProggyClean.ttf)\n//-----------------------------------------------------------------------------\n// ProggyClean.ttf\n// Copyright (c) 2004, 2005 Tristan Grimmer\n// MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip)\n// Download and more information at http://upperbounds.net\n//-----------------------------------------------------------------------------\n// File: 'ProggyClean.ttf' (41208 bytes)\n// Exported using misc/fonts/binary_to_compressed_c.cpp (with compression + base85 string encoding).\n// The purpose of encoding as base85 instead of \"0x00,0x01,...\" style is only save on _source code_ size.\n//-----------------------------------------------------------------------------\nstatic const char proggy_clean_ttf_compressed_data_base85[11980+1] =\n    \"7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/\"\n    \"2*>]b(MC;$jPfY.;h^`IWM9<Lh2TlS+f-s$o6Q<BWH`YiU.xfLq$N;$0iR/GX:U(jcW2p/W*q?-qmnUCI;jHSAiFWM.R*kU@C=GH?a9wp8f$e.-4^Qg1)Q-GL(lf(r/7GrRgwV%MS=C#\"\n    \"`8ND>Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1<q-UE31#^-V'8IRUo7Qf./L>=Ke$$'5F%)]0^#0X@U.a<r:QLtFsLcL6##lOj)#.Y5<-R&KgLwqJfLgN&;Q?gI^#DY2uL\"\n    \"i@^rMl9t=cWq6##weg>$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#W@lK.N'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;-<nLENhvx>-VsM.M0rJfLH2eTM`*oJMHRC`N\"\n    \"kfimM2J,W-jXS:)r0wK#@Fge$U>`w'N7G#$#fB#$E^$#:9:hk+eOe--6x)F7*E%?76%^GMHePW-Z5l'&GiF#$956:rS?dA#fiK:)Yr+`&#0j@'DbG&#^$PG.Ll+DNa<XCMKEV*N)LN/N\"\n    \"*b=%Q6pia-Xg8I$<MR&,VdJe$<(7G;Ckl'&hF;;$<_=X(b.RS%%)###MPBuuE1V:v&cX&#2m#(&cV]`k9OhLMbn%s$G2,B$BfD3X*sp5#l,$R#]x_X1xKX%b5U*[r5iMfUo9U`N99hG)\"\n    \"tm+/Us9pG)XPu`<0s-)WTt(gCRxIg(%6sfh=ktMKn3j)<6<b5Sk_/0(^]AaN#(p/L>&VZ>1i%h1S9u5o@YaaW$e+b<TWFn/Z:Oh(Cx2$lNEoN^e)#CFY@@I;BOQ*sRwZtZxRcU7uW6CX\"\n    \"ow0i(?$Q[cjOd[P4d)]>ROPOpxTO7Stwi1::iB1q)C_=dV26J;2,]7op$]uQr@_V7$q^%lQwtuHY]=DX,n3L#0PHDO4f9>dC@O>HBuKPpP*E,N+b3L#lpR/MrTEH.IAQk.a>D[.e;mc.\"\n    \"x]Ip.PH^'/aqUO/$1WxLoW0[iLA<QT;5HKD+@qQ'NQ(3_PLhE48R.qAPSwQ0/WK?Z,[x?-J;jQTWA0X@KJ(_Y8N-:/M74:/-ZpKrUss?d#dZq]DAbkU*JqkL+nwX@@47`5>w=4h(9.`G\"\n    \"CRUxHPeR`5Mjol(dUWxZa(>STrPkrJiWx`5U7F#.g*jrohGg`cg:lSTvEY/EV_7H4Q9[Z%cnv;JQYZ5q.l7Zeas:HOIZOB?G<Nald$qs]@]L<J7bR*>gv:[7MI2k).'2($5FNP&EQ(,)\"\n    \"U]W]+fh18.vsai00);D3@4ku5P?DP8aJt+;qUM]=+b'8@;mViBKx0DE[-auGl8:PJ&Dj+M6OC]O^((##]`0i)drT;-7X`=-H3[igUnPG-NZlo.#k@h#=Ork$m>a>$-?Tm$UV(?#P6YY#\"\n    \"'/###xe7q.73rI3*pP/$1>s9)W,JrM7SN]'/4C#v$U`0#V.[0>xQsH$fEmPMgY2u7Kh(G%siIfLSoS+MK2eTM$=5,M8p`A.;_R%#u[K#$x4AG8.kK/HSB==-'Ie/QTtG?-.*^N-4B/ZM\"\n    \"_3YlQC7(p7q)&](`6_c)$/*JL(L-^(]$wIM`dPtOdGA,U3:w2M-0<q-]L_?^)1vw'.,MRsqVr.L;aN&#/EgJ)PBc[-f>+WomX2u7lqM2iEumMTcsF?-aT=Z-97UEnXglEn1K-bnEO`gu\"\n    \"Ft(c%=;Am_Qs@jLooI&NX;]0#j4#F14;gl8-GQpgwhrq8'=l_f-b49'UOqkLu7-##oDY2L(te+Mch&gLYtJ,MEtJfLh'x'M=$CS-ZZ%P]8bZ>#S?YY#%Q&q'3^Fw&?D)UDNrocM3A76/\"\n    \"/oL?#h7gl85[qW/NDOk%16ij;+:1a'iNIdb-ou8.P*w,v5#EI$TWS>Pot-R*H'-SEpA:g)f+O$%%`kA#G=8RMmG1&O`>to8bC]T&$,n.LoO>29sp3dt-52U%VM#q7'DHpg+#Z9%H[K<L\"\n    \"%a2E-grWVM3@2=-k22tL]4$##6We'8UJCKE[d_=%wI;'6X-GsLX4j^SgJ$##R*w,vP3wK#iiW&#*h^D&R?jp7+/u&#(AP##XU8c$fSYW-J95_-Dp[g9wcO&#M-h1OcJlc-*vpw0xUX&#\"\n    \"OQFKNX@QI'IoPp7nb,QU//MQ&ZDkKP)X<WSVL(68uVl&#c'[0#(s1X&xm$Y%B7*K:eDA323j998GXbA#pwMs-jgD$9QISB-A_(aN4xoFM^@C58D0+Q+q3n0#3U1InDjF682-SjMXJK)(\"\n    \"h$hxua_K]ul92%'BOU&#BRRh-slg8KDlr:%L71Ka:.A;%YULjDPmL<LYs8i#XwJOYaKPKc1h:'9Ke,g)b),78=I39B;xiY$bgGw-&.Zi9InXDuYa%G*f2Bq7mn9^#p1vv%#(Wi-;/Z5h\"\n    \"o;#2:;%d&#x9v68C5g?ntX0X)pT`;%pB3q7mgGN)3%(P8nTd5L7GeA-GL@+%J3u2:(Yf>et`e;)f#Km8&+DC$I46>#Kr]]u-[=99tts1.qb#q72g1WJO81q+eN'03'eM>&1XxY-caEnO\"\n    \"j%2n8)),?ILR5^.Ibn<-X-Mq7[a82Lq:F&#ce+S9wsCK*x`569E8ew'He]h:sI[2LM$[guka3ZRd6:t%IG:;$%YiJ:Nq=?eAw;/:nnDq0(CYcMpG)qLN4$##&J<j$UpK<Q4a1]MupW^-\"\n    \"sj_$%[HK%'F####QRZJ::Y3EGl4'@%FkiAOg#p[##O`gukTfBHagL<LHw%q&OV0##F=6/:chIm0@eCP8X]:kFI%hl8hgO@RcBhS-@Qb$%+m=hPDLg*%K8ln(wcf3/'DW-$.lR?n[nCH-\"\n    \"eXOONTJlh:.RYF%3'p6sq:UIMA945&^HFS87@$EP2iG<-lCO$%c`uKGD3rC$x0BL8aFn--`ke%#HMP'vh1/R&O_J9'um,.<tx[@%wsJk&bUT2`0uMv7gg#qp/ij.L56'hl;.s5CUrxjO\"\n    \"M7-##.l+Au'A&O:-T72L]P`&=;ctp'XScX*rU.>-XTt,%OVU4)S1+R-#dg0/Nn?Ku1^0f$B*P:Rowwm-`0PKjYDDM'3]d39VZHEl4,.j']Pk-M.h^&:0FACm$maq-&sgw0t7/6(^xtk%\"\n    \"LuH88Fj-ekm>GA#_>568x6(OFRl-IZp`&b,_P'$M<Jnq79VsJW/mWS*PUiq76;]/NM_>hLbxfc$mj`,O;&%W2m`Zh:/)Uetw:aJ%]K9h:TcF]u_-Sj9,VK3M.*'&0D[Ca]J9gp8,kAW]\"\n    \"%(?A%R$f<->Zts'^kn=-^@c4%-pY6qI%J%1IGxfLU9CP8cbPlXv);C=b),<2mOvP8up,UVf3839acAWAW-W?#ao/^#%KYo8fRULNd2.>%m]UK:n%r$'sw]J;5pAoO_#2mO3n,'=H5(et\"\n    \"Hg*`+RLgv>=4U8guD$I%D:W>-r5V*%j*W:Kvej.Lp$<M-SGZ':+Q_k+uvOSLiEo(<aD/K<CCc`'Lx>'?;++O'>()jLR-^u68PHm8ZFWe+ej8h:9r6L*0//c&iH&R8pRbA#Kjm%upV1g:\"\n    \"a_#Ur7FuA#(tRh#.Y5K+@?3<-8m0$PEn;J:rh6?I6uG<-`wMU'ircp0LaE_OtlMb&1#6T.#FDKu#1Lw%u%+GM+X'e?YLfjM[VO0MbuFp7;>Q&#WIo)0@F%q7c#4XAXN-U&VB<HFF*qL(\"\n    \"$/V,;(kXZejWO`<[5?\\?ewY(*9=%wDc;,u<'9t3W-(H1th3+G]ucQ]kLs7df($/*JL]@*t7Bu_G3_7mp7<iaQjO@.kLg;x3B0lqp7Hf,^Ze7-##@/c58Mo(3;knp0%)A7?-W+eI'o8)b<\"\n    \"nKnw'Ho8C=Y>pqB>0ie&jhZ[?iLR@@_AvA-iQC(=ksRZRVp7`.=+NpBC%rh&3]R:8XDmE5^V8O(x<<aG/1N$#FX$0V5Y6x'aErI3I$7x%E`v<-BY,)%-?Psf*l?%C3.mM(=/M0:JxG'?\"\n    \"7WhH%o'a<-80g0NBxoO(GH<dM]n.+%q@jH?f.UsJ2Ggs&4<-e47&Kl+f//9@`b+?.TeN_&B8Ss?v;^Trk;f#YvJkl&w$]>-+k?'(<S:68tq*WoDfZu';mM?8X[ma8W%*`-=;D.(nc7/;\"\n    \")g:T1=^J$&BRV(-lTmNB6xqB[@0*o.erM*<SWF]u2=st-*(6v>^](H.aREZSi,#1:[IXaZFOm<-ui#qUq2$##Ri;u75OK#(RtaW-K-F`S+cF]uN`-KMQ%rP/Xri.LRcB##=YL3BgM/3M\"\n    \"D?@f&1'BW-)Ju<L25gl8uhVm1hL$##*8###'A3/LkKW+(^rWX?5W_8g)a(m&K8P>#bmmWCMkk&#TR`C,5d>g)F;t,4:@_l8G/5h4vUd%&%950:VXD'QdWoY-F$BtUwmfe$YqL'8(PWX(\"\n    \"P?^@Po3$##`MSs?DWBZ/S>+4%>fX,VWv/w'KD`LP5IbH;rTV>n3cEK8U#bX]l-/V+^lj3;vlMb&[5YQ8#pekX9JP3XUC72L,,?+Ni&co7ApnO*5NK,((W-i:$,kp'UDAO(G0Sq7MVjJs\"\n    \"bIu)'Z,*[>br5fX^:FPAWr-m2KgL<LUN098kTF&#lvo58=/vjDo;.;)Ka*hLR#/k=rKbxuV`>Q_nN6'8uTG&#1T5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q\"\n    \"h4CB/5OvmA&,Q&QbUoi$a_%3M01H)4x7I^&KQVgtFnV+;[Pc>[m4k//,]1?#`VY[Jr*3&&slRfLiVZJ:]?=K3Sw=[$=uRB?3xk48@aeg<Z'<$#4H)6,>e0jT6'N#(q%.O=?2S]u*(m<-\"\n    \"V8J'(1)G][68hW$5'q[GC&5j`TE?m'esFGNRM)j,ffZ?-qx8;->g4t*:CIP/[Qap7/9'#(1sao7w-.qNUdkJ)tCF&#B^;xGvn2r9FEPFFFcL@.iFNkTve$m%#QvQS8U@)2Z+3K:AKM5i\"\n    \"sZ88+dKQ)W6>J%CL<KE>`.d*(B`-n8D9oK<Up]c$X$(,)M8Zt7/[rdkqTgl-0cuGMv'?>-XV1q['-5k'cAZ69e;D_?$ZPP&s^+7])$*$#@QYi9,5P&#9r+$%CE=68>K8r0=dSC%%(@p7\"\n    \".m7jilQ02'0-VWAg<a/''3u.=4L$Y)6k/K:_[3=&jvL<L0C/2'v:^;-DIBW,B4E68:kZ;%?8(Q8BH=kO65BW?xSG&#@uU,DS*,?.+(o(#1vCS8#CHF>TlGW'b)Tq7VT9q^*^$$.:&N@@\"\n    \"$&)WHtPm*5_rO0&e%K&#-30j(E4#'Zb.o/(Tpm$>K'f@[PvFl,hfINTNU6u'0pao7%XUp9]5.>%h`8_=VYbxuel.NTSsJfLacFu3B'lQSu/m6-Oqem8T+oE--$0a/k]uj9EwsG>%veR*\"\n    \"hv^BFpQj:K'#SJ,sB-'#](j.Lg92rTw-*n%@/;39rrJF,l#qV%OrtBeC6/,;qB3ebNW[?,Hqj2L.1NP&GjUR=1D8QaS3Up&@*9wP?+lo7b?@%'k4`p0Z$22%K3+iCZj?XJN4Nm&+YF]u\"\n    \"@-W$U%VEQ/,,>>#)D<h#`)h0:<Q6909ua+&VU%n2:cG3FJ-%@Bj-DgLr`Hw&HAKjKjseK</xKT*)B,N9X3]krc12t'pgTV(Lv-tL[xg_%=M_q7a^x?7Ubd>#%8cY#YZ?=,`Wdxu/ae&#\"\n    \"w6)R89tI#6@s'(6Bf7a&?S=^ZI_kS&ai`&=tE72L_D,;^R)7[$s<Eh#c&)q.MXI%#v9ROa5FZO%sF7q7Nwb&#ptUJ:aqJe$Sl68%.D###EC><?-aF&#RNQv>o8lKN%5/$(vdfq7+ebA#\"\n    \"u1p]ovUKW&Y%q]'>$1@-[xfn$7ZTp7mM,G,Ko7a&Gu%G[RMxJs[0MM%wci.LFDK)(<c`Q8N)jEIF*+?P2a8g%)$q]o2aH8C&<SibC/q,(e:v;-b#6[$NtDZ84Je2KNvB#$P5?tQ3nt(0\"\n    \"d=j.LQf./Ll33+(;q3L-w=8dX$#WF&uIJ@-bfI>%:_i2B5CsR8&9Z&#=mPEnm0f`<&c)QL5uJ#%u%lJj+D-r;BoF&#4DoS97h5g)E#o:&S4weDF,9^Hoe`h*L+_a*NrLW-1pG_&2UdB8\"\n    \"6e%B/:=>)N4xeW.*wft-;$'58-ESqr<b?UI(_%@[P46>#U`'6AQ]m&6/`Z>#S?YY#Vc;r7U2&326d=w&H####?TZ`*4?&.MK?LP8Vxg>$[QXc%QJv92.(Db*B)gb*BM9dM*hJMAo*c&#\"\n    \"b0v=Pjer]$gG&JXDf->'StvU7505l9$AFvgYRI^&<^b68?j#q9QX4SM'RO#&sL1IM.rJfLUAj221]d##DW=m83u5;'bYx,*Sl0hL(W;;$doB&O/TQ:(Z^xBdLjL<Lni;''X.`$#8+1GD\"\n    \":k$YUWsbn8ogh6rxZ2Z9]%nd+>V#*8U_72Lh+2Q8Cj0i:6hp&$C/:p(HK>T8Y[gHQ4`4)'$Ab(Nof%V'8hL&#<NEdtg(n'=S1A(Q1/I&4([%dM`,Iu'1:_hL>SfD07&6D<fp8dHM7/g+\"\n    \"tlPN9J*rKaPct&?'uBCem^jn%9_K)<,C5K3s=5g&GmJb*[SYq7K;TRLGCsM-$$;S%:Y@r7AK0pprpL<Lrh,q7e/%KWK:50I^+m'vi`3?%Zp+<-d+$L-Sv:@.o19n$s0&39;kn;S%BSq*\"\n    \"$3WoJSCLweV[aZ'MQIjO<7;X-X;&+dMLvu#^UsGEC9WEc[X(wI7#2.(F0jV*eZf<-Qv3J-c+J5AlrB#$p(H68LvEA'q3n0#m,[`*8Ft)FcYgEud]CWfm68,(aLA$@EFTgLXoBq/UPlp7\"\n    \":d[/;r_ix=:TF`S5H-b<LI&HY(K=h#)]Lk$K14lVfm:x$H<3^Ql<M`$OhapBnkup'D#L$Pb_`N*g]2e;X/Dtg,bsj&K#2[-:iYr'_wgH)NUIR8a1n#S?Yej'h8^58UbZd+^FKD*T@;6A\"\n    \"7aQC[K8d-(v6GI$x:T<&'Gp5Uf>@M.*J:;$-rv29'M]8qMv-tLp,'886iaC=Hb*YJoKJ,(j%K=H`K.v9HggqBIiZu'QvBT.#=)0ukruV&.)3=(^1`o*Pj4<-<aN((^7('#Z0wK#5GX@7\"\n    \"u][`*S^43933A4rl][`*O4CgLEl]v$1Q3AeF37dbXk,.)vj#x'd`;qgbQR%FW,2(?LO=s%Sc68%NP'##Aotl8x=BE#j1UD([3$M(]UI2LX3RpKN@;/#f'f/&_mt&F)XdF<9t4)Qa.*kT\"\n    \"LwQ'(TTB9.xH'>#MJ+gLq9-##@HuZPN0]u:h7.T..G:;$/Usj(T7`Q8tT72LnYl<-qx8;-HV7Q-&Xdx%1a,hC=0u+HlsV>nuIQL-5<N?)NBS)QN*_I,?&)2'IM%L3I)X((e/dl2&8'<M\"\n    \":^#M*Q+[T.Xri.LYS3v%fF`68h;b-X[/En'CR.q7E)p'/kle2HM,u;^%OKC-N+Ll%F9CF<Nf'^#t2L,;27W:0O@6##U6W7:$rJfLWHj$#)woqBefIZ.PK<b*t7ed;p*_m;4ExK#h@&]>\"\n    \"_>@kXQtMacfD.m-VAb8;IReM3$wf0''hra*so568'Ip&vRs849'MRYSp%:t:h5qSgwpEr$B>Q,;s(C#$)`svQuF$##-D,##,g68@2[T;.XSdN9Qe)rpt._K-#5wF)sP'##p#C0c%-Gb%\"\n    \"hd+<-j'Ai*x&&HMkT]C'OSl##5RG[JXaHN;d'uA#x._U;.`PU@(Z3dt4r152@:v,'R.Sj'w#0<-;kPI)FfJ&#AYJ&#//)>-k=m=*XnK$>=)72L]0I%>.G690a:$##<,);?;72#?x9+d;\"\n    \"^V'9;jY@;)br#q^YQpx:X#Te$Z^'=-=bGhLf:D6&bNwZ9-ZD#n^9HhLMr5G;']d&6'wYmTFmL<LD)F^%[tC'8;+9E#C$g%#5Y>q9wI>P(9mI[>kC-ekLC/R&CH+s'B;K-M6$EB%is00:\"\n    \"+A4[7xks.LrNk0&E)wILYF@2L'0Nb$+pv<(2.768/FrY&h$^3i&@+G%JT'<-,v`3;_)I9M^AE]CN?Cl2AZg+%4iTpT3<n-&%H%b<FDj2M<hH=&Eh<2Len$b*aTX=-8QxN)k11IM1c^j%\"\n    \"9s<L<NFSo)B?+<-(GxsF,^-Eh@$4dXhN$+#rxK8'je'D7k`e;)2pYwPA'_p9&@^18ml1^[@g4t*[JOa*[=Qp7(qJ_oOL^('7fB&Hq-:sf,sNj8xq^>$U4O]GKx'm9)b@p7YsvK3w^YR-\"\n    \"CdQ*:Ir<($u&)#(&?L9Rg3H)4fiEp^iI9O8KnTj,]H?D*r7'M;PwZ9K0E^k&-cpI;.p/6_vwoFMV<->#%Xi.LxVnrU(4&8/P+:hLSKj$#U%]49t'I:rgMi'FL@a:0Y-uA[39',(vbma*\"\n    \"hU%<-SRF`Tt:542R_VV$p@[p8DV[A,?1839FWdF<TddF<9Ah-6&9tWoDlh]&1SpGMq>Ti1O*H&#(AL8[_P%.M>v^-))qOT*F5Cq0`Ye%+$B6i:7@0IX<N+T+0MlMBPQ*Vj>SsD<U4JHY\"\n    \"8kD2)2fU/M#$e.)T4,_=8hLim[&);?UkK'-x?'(:siIfL<$pFM`i<?%W(mGDHM%>iWP,##P`%/L<eXi:@Z9C.7o=@(pXdAO/NLQ8lPl+HPOQa8wD8=^GlPa8TKI1CjhsCTSLJM'/Wl>-\"\n    \"S(qw%sf/@%#B6;/U7K]uZbi^Oc^2n<bhPmUkMw>%t<)'mEVE''n`WnJra$^TKvX5B>;_aSEK',(hwa0:i4G?.Bci.(X[?b*($,=-n<.Q%`(X=?+@Am*Js0&=3bh8K]mL<LoNs'6,'85`\"\n    \"0?t/'_U59@]ddF<#LdF<eWdF<OuN/45rY<-L@&#+fm>69=Lb,OcZV/);TTm8VI;?%OtJ<(b4mq7M6:u?KRdF<gR@2L=FNU-<b[(9c/ML3m;Z[$oF3g)GAWqpARc=<ROu7cL5l;-[A]%/\"\n    \"+fsd;l#SafT/f*W]0=O'$(Tb<[)*@e775R-:Yob%g*>l*:xP?Yb.5)%w_I?7uk5JC+FS(m#i'k.'a0i)9<7b'fs'59hq$*5Uhv##pi^8+hIEBF`nvo`;'l0.^S1<-wUK2/Coh58KKhLj\"\n    \"M=SO*rfO`+qC`W-On.=AJ56>>i2@2LH6A:&5q`?9I3@@'04&p2/LVa*T-4<-i3;M9UvZd+N7>b*eIwg:CC)c<>nO&#<IGe;__.thjZl<%w(Wk2xmp4Q@I#I9,DF]u7-P=.-_:YJ]aS@V\"\n    \"?6*C()dOp7:WL,b&3Rg/.cmM9&r^>$(>.Z-I&J(Q0Hd5Q%7Co-b`-c<N(6r@ip+AurK<m86QIth*#v;-OBqi+L7wDE-Ir8K['m+DDSLwK&/.?-V%U_%3:qKNu$_b*B-kp7NaD'QdWQPK\"\n    \"Yq[@>P)hI;*_F]u`Rb[.j8_Q/<&>uu+VsH$sM9TA%?)(vmJ80),P7E>)tjD%2L=-t#fK[%`v=Q8<FfNkgg^oIbah*#8/Qt$F&:K*-(N/'+1vMB,u()-a.VUU*#[e%gAAO(S>WlA2);Sa\"\n    \">gXm8YB`1d@K#n]76-a$U,mF<fX]idqd)<3,]J7JmW4`6]uks=4-72L(jEk+:bJ0M^q-8Dm_Z?0olP1C9Sa&H[d&c$ooQUj]Exd*3ZM@-WGW2%s',B-_M%>%Ul:#/'xoFM9QX-$.QN'>\"\n    \"[%$Z$uF6pA6Ki2O5:8w*vP1<-1`[G,)-m#>0`P&#eb#.3i)rtB61(o'$?X3B</R90;eZ]%Ncq;-Tl]#F>2Qft^ae_5tKL9MUe9b*sLEQ95C&`=G?@Mj=wh*'3E>=-<)Gt*Iw)'QG:`@I\"\n    \"wOf7&]1i'S01B+Ev/Nac#9S;=;YQpg_6U`*kVY39xK,[/6Aj7:'1Bm-_1EYfa1+o&o4hp7KN_Q(OlIo@S%;jVdn0'1<Vc52=u`3^o-n1'g4v58Hj&6_t7$##?M)c<$bgQ_'SY((-xkA#\"\n    \"Y(,p'H9rIVY-b,'%bCPF7.J<Up^,(dU1VY*5#WkTU>h19w,WQhLI)3S#f$2(eb,jr*b;3Vw]*7NH%$c4Vs,eD9>XW8?N]o+(*pgC%/72LV-u<Hp,3@e^9UB1J+ak9-TN/mhKPg+AJYd$\"\n    \"MlvAF_jCK*.O-^(63adMT->W%iewS8W6m2rtCpo'RS1R84=@paTKt)>=%&1[)*vp'u+x,VrwN;&]kuO9JDbg=pO$J*.jVe;u'm0dr9l,<*wMK*Oe=g8lV_KEBFkO'oU]^=[-792#ok,)\"\n    \"i]lR8qQ2oA8wcRCZ^7w/Njh;?.stX?Q1>S1q4Bn$)K1<-rGdO'$Wr.Lc.CG)$/*JL4tNR/,SVO3,aUw'DJN:)Ss;wGn9A32ijw%FL+Z0Fn.U9;reSq)bmI32U==5ALuG&#Vf1398/pVo\"\n    \"1*c-(aY168o<`JsSbk-,1N;$>0:OUas(3:8Z972LSfF8eb=c-;>SPw7.6hn3m`9^Xkn(r.qS[0;T%&Qc=+STRxX'q1BNk3&*eu2;&8q$&x>Q#Q7^Tf+6<(d%ZVmj2bDi%.3L2n+4W'$P\"\n    \"iDDG)g,r%+?,$@?uou5tSe2aN_AQU*<h`e-GI7)?OK2A.d7_c)?wQ5AS@DL3r#7fSkgl6-++D:'A,uq7SvlB$pcpH'q3n0#_%dY#xCpr-l<F0NR@-##FEV6NTF6##$l84N1w?AO>'IAO\"\n    \"URQ##V^Fv-XFbGM7Fl(N<3DhLGF%q.1rC$#:T__&Pi68%0xi_&[qFJ(77j_&JWoF.V735&T,[R*:xFR*K5>>#`bW-?4Ne_&6Ne_&6Ne_&n`kr-#GJcM6X;uM6X;uM(.a..^2TkL%oR(#\"\n    \";u.T%fAr%4tJ8&><1=GHZ_+m9/#H1F^R#SC#*N=BA9(D?v[UiFY>>^8p,KKF.W]L29uLkLlu/+4T<XoIB&hx=T1PcDaB&;HH+-AFr?(m9HZV)FKS8JCw;SD=6[^/DZUL`EUDf]GGlG&>\"\n    \"w$)F./^n3+rlo+DB;5sIYGNk+i1t-69Jg--0pao7Sm#K)pdHW&;LuDNH@H>#/X-TI(;P>#,Gc>#0Su>#4`1?#8lC?#<xU?#@.i?#D:%@#HF7@#LRI@#P_[@#Tkn@#Xw*A#]-=A#a9OA#\"\n    \"d<F&#*;G##.GY##2Sl##6`($#:l:$#>xL$#B.`$#F:r$#JF.%#NR@%#R_R%#Vke%#Zww%#_-4&#3^Rh%Sflr-k'MS.o?.5/sWel/wpEM0%3'/1)K^f1-d>G21&v(35>V`39V7A4=onx4\"\n    \"A1OY5EI0;6Ibgr6M$HS7Q<)58C5w,;WoA*#[%T*#`1g*#d=#+#hI5+#lUG+#pbY+#tnl+#x$),#&1;,#*=M,#.I`,#2Ur,#6b.-#;w[H#iQtA#m^0B#qjBB#uvTB##-hB#'9$C#+E6C#\"\n    \"/QHC#3^ZC#7jmC#;v)D#?,<D#C8ND#GDaD#KPsD#O]/E#g1A5#KA*1#gC17#MGd;#8(02#L-d3#rWM4#Hga1#,<w0#T.j<#O#'2#CYN1#qa^:#_4m3#o@/=#eG8=#t8J5#`+78#4uI-#\"\n    \"m3B2#SB[8#Q0@8#i[*9#iOn8#1Nm;#^sN9#qh<9#:=x-#P;K2#$%X9#bC+.#Rg;<#mN=.#MTF.#RZO.#2?)4#Y#(/#[)1/#b;L/#dAU/#0Sv;#lY$0#n`-0#sf60#(F24#wrH0#%/e0#\"\n    \"TmD<#%JSMFove:CTBEXI:<eh2g)B,3h2^G3i;#d3jD>)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP\"\n    \"GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp\"\n    \"O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#\";\n\nstatic const char* GetDefaultCompressedFontDataTTFBase85()\n{\n    return proggy_clean_ttf_compressed_data_base85;\n}\n\n#pragma warning(pop)"
  },
  {
    "path": "src/imgui/imgui_impl_dx12.cpp",
    "content": "// dear imgui: Renderer for DirectX12\n// This needs to be used along with a Platform Binding (e.g. Win32)\n\n// Implemented features:\n//  [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.\n// Issues:\n//  [ ] 64-bit only for now! (Because sizeof(ImTextureId) == sizeof(void*)). See github.com/ocornut/imgui/pull/301\n\n// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.\n// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.\n// https://github.com/ocornut/imgui\n\n// CHANGELOG\n// (minor and older changes stripped away, please see git history for details)\n//  2019-03-29: Misc: Various minor tidying up.\n//  2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile().\n//  2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.\n//  2018-06-12: DirectX12: Moved the ID3D12GraphicsCommandList* parameter from NewFrame() to RenderDrawData().\n//  2018-06-08: Misc: Extracted imgui_impl_dx12.cpp/.h away from the old combined DX12+Win32 example.\n//  2018-06-08: DirectX12: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle (to ease support for future multi-viewport).\n//  2018-02-22: Merged into master with all Win32 code synchronized to other examples.\n\n#include \"imgui.hpp\"\n#include \"imgui_impl_dx12.hpp\"\n\n// DirectX\n#include <d3d12.h>\n#include <dxgi1_4.h>\n#include <d3dcompiler.h>\n#ifdef _MSC_VER\n#pragma comment(lib, \"d3dcompiler\") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.\n#endif\n\n// DirectX data\nstatic ID3D12Device*                g_pd3dDevice = NULL;\nstatic ID3D10Blob*                  g_pVertexShaderBlob = NULL;\nstatic ID3D10Blob*                  g_pPixelShaderBlob = NULL;\nstatic ID3D12RootSignature*         g_pRootSignature = NULL;\nstatic ID3D12PipelineState*         g_pPipelineState = NULL;\nstatic DXGI_FORMAT                  g_RTVFormat = DXGI_FORMAT_UNKNOWN;\nstatic ID3D12Resource*              g_pFontTextureResource = NULL;\nstatic D3D12_CPU_DESCRIPTOR_HANDLE  g_hFontSrvCpuDescHandle = {};\nstatic D3D12_GPU_DESCRIPTOR_HANDLE  g_hFontSrvGpuDescHandle = {};\n\nstruct FrameResources\n{\n    ID3D12Resource*     IndexBuffer;\n    ID3D12Resource*     VertexBuffer;\n    int                 IndexBufferSize;\n    int                 VertexBufferSize;\n};\nstatic FrameResources*  g_pFrameResources = NULL;\nstatic UINT             g_numFramesInFlight = 0;\nstatic UINT             g_frameIndex = UINT_MAX;\nstatic bool\t\t\t\tg_initialized = false;\n\nstruct VERTEX_CONSTANT_BUFFER\n{\n    float   mvp[4][4];\n};\n\n// Forward Declarations\nstatic void ImGui_ImplDX12_InitPlatformInterface();\nstatic void ImGui_ImplDX12_ShutdownPlatformInterface();\n\n// Render function\n// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)\nvoid ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* ctx)\n{\n    // Avoid rendering when minimized\n    if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)\n        return;\n\n    // FIXME: I'm assuming that this only gets called once per frame!\n    // If not, we can't just re-allocate the IB or VB, we'll have to do a proper allocator.\n    g_frameIndex = g_frameIndex + 1;\n    FrameResources* fr = &g_pFrameResources[g_frameIndex % g_numFramesInFlight];\n\n    // Create and grow vertex/index buffers if needed\n    if (fr->VertexBuffer == NULL || fr->VertexBufferSize < draw_data->TotalVtxCount)\n    {\n        if (fr->VertexBuffer != NULL) { fr->VertexBuffer->Release(); fr->VertexBuffer = NULL; }\n        fr->VertexBufferSize = draw_data->TotalVtxCount + 5000;\n        D3D12_HEAP_PROPERTIES props;\n        memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));\n        props.Type = D3D12_HEAP_TYPE_UPLOAD;\n        props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;\n        props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;\n        D3D12_RESOURCE_DESC desc;\n        memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC));\n        desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;\n        desc.Width = fr->VertexBufferSize * sizeof(ImDrawVert);\n        desc.Height = 1;\n        desc.DepthOrArraySize = 1;\n        desc.MipLevels = 1;\n        desc.Format = DXGI_FORMAT_UNKNOWN;\n        desc.SampleDesc.Count = 1;\n        desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;\n        desc.Flags = D3D12_RESOURCE_FLAG_NONE;\n        if (g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&fr->VertexBuffer)) < 0)\n            return;\n    }\n    if (fr->IndexBuffer == NULL || fr->IndexBufferSize < draw_data->TotalIdxCount)\n    {\n        if (fr->IndexBuffer != NULL) { fr->IndexBuffer->Release(); fr->IndexBuffer = NULL; }\n        fr->IndexBufferSize = draw_data->TotalIdxCount + 10000;\n        D3D12_HEAP_PROPERTIES props;\n        memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));\n        props.Type = D3D12_HEAP_TYPE_UPLOAD;\n        props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;\n        props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;\n        D3D12_RESOURCE_DESC desc;\n        memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC));\n        desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;\n        desc.Width = fr->IndexBufferSize * sizeof(ImDrawIdx);\n        desc.Height = 1;\n        desc.DepthOrArraySize = 1;\n        desc.MipLevels = 1;\n        desc.Format = DXGI_FORMAT_UNKNOWN;\n        desc.SampleDesc.Count = 1;\n        desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;\n        desc.Flags = D3D12_RESOURCE_FLAG_NONE;\n        if (g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&fr->IndexBuffer)) < 0)\n            return;\n    }\n\n    // Upload vertex/index data into a single contiguous GPU buffer\n    void* vtx_resource, *idx_resource;\n    D3D12_RANGE range;\n    memset(&range, 0, sizeof(D3D12_RANGE));\n    if (fr->VertexBuffer->Map(0, &range, &vtx_resource) != S_OK)\n        return;\n    if (fr->IndexBuffer->Map(0, &range, &idx_resource) != S_OK)\n        return;\n    ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource;\n    ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource;\n    for (int n = 0; n < draw_data->CmdListsCount; n++)\n    {\n        const ImDrawList* cmd_list = draw_data->CmdLists[n];\n        memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));\n        memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));\n        vtx_dst += cmd_list->VtxBuffer.Size;\n        idx_dst += cmd_list->IdxBuffer.Size;\n    }\n    fr->VertexBuffer->Unmap(0, &range);\n    fr->IndexBuffer->Unmap(0, &range);\n\n    // Setup orthographic projection matrix into our constant buffer\n    // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is (0,0) for single viewport apps.\n    VERTEX_CONSTANT_BUFFER vertex_constant_buffer;\n    {\n        float L = draw_data->DisplayPos.x;\n        float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;\n        float T = draw_data->DisplayPos.y;\n        float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;\n        float mvp[4][4] =\n        {\n            { 2.0f/(R-L),   0.0f,           0.0f,       0.0f },\n            { 0.0f,         2.0f/(T-B),     0.0f,       0.0f },\n            { 0.0f,         0.0f,           0.5f,       0.0f },\n            { (R+L)/(L-R),  (T+B)/(B-T),    0.5f,       1.0f },\n        };\n        memcpy(&vertex_constant_buffer.mvp, mvp, sizeof(mvp));\n    }\n\n    // Setup viewport\n    D3D12_VIEWPORT vp;\n    memset(&vp, 0, sizeof(D3D12_VIEWPORT));\n    vp.Width = draw_data->DisplaySize.x;\n    vp.Height = draw_data->DisplaySize.y;\n    vp.MinDepth = 0.0f;\n    vp.MaxDepth = 1.0f;\n    vp.TopLeftX = vp.TopLeftY = 0.0f;\n    ctx->RSSetViewports(1, &vp);\n\n    // Bind shader and vertex buffers\n    unsigned int stride = sizeof(ImDrawVert);\n    unsigned int offset = 0;\n    D3D12_VERTEX_BUFFER_VIEW vbv;\n    memset(&vbv, 0, sizeof(D3D12_VERTEX_BUFFER_VIEW));\n    vbv.BufferLocation = fr->VertexBuffer->GetGPUVirtualAddress() + offset;\n    vbv.SizeInBytes = fr->VertexBufferSize * stride;\n    vbv.StrideInBytes = stride;\n    ctx->IASetVertexBuffers(0, 1, &vbv);\n    D3D12_INDEX_BUFFER_VIEW ibv;\n    memset(&ibv, 0, sizeof(D3D12_INDEX_BUFFER_VIEW));\n    ibv.BufferLocation = fr->IndexBuffer->GetGPUVirtualAddress();\n    ibv.SizeInBytes = fr->IndexBufferSize * sizeof(ImDrawIdx);\n    ibv.Format = sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;\n    ctx->IASetIndexBuffer(&ibv);\n    ctx->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);\n    ctx->SetPipelineState(g_pPipelineState);\n    ctx->SetGraphicsRootSignature(g_pRootSignature);\n    ctx->SetGraphicsRoot32BitConstants(0, 16, &vertex_constant_buffer, 0);\n\n    // Setup blend factor\n    const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };\n    ctx->OMSetBlendFactor(blend_factor);\n\n    // Render command lists\n    int vtx_offset = 0;\n    int idx_offset = 0;\n    ImVec2 clip_off = draw_data->DisplayPos;\n    for (int n = 0; n < draw_data->CmdListsCount; n++)\n    {\n        const ImDrawList* cmd_list = draw_data->CmdLists[n];\n        for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)\n        {\n            const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];\n            if (pcmd->UserCallback)\n            {\n                // User callback (registered via ImDrawList::AddCallback)\n                pcmd->UserCallback(cmd_list, pcmd);\n            }\n            else\n            {\n                // Apply Scissor, Bind texture, Draw\n                const D3D12_RECT r = { (LONG)(pcmd->ClipRect.x - clip_off.x), (LONG)(pcmd->ClipRect.y - clip_off.y), (LONG)(pcmd->ClipRect.z - clip_off.x), (LONG)(pcmd->ClipRect.w - clip_off.y) };\n                ctx->SetGraphicsRootDescriptorTable(1, *(D3D12_GPU_DESCRIPTOR_HANDLE*)&pcmd->TextureId);\n                ctx->RSSetScissorRects(1, &r);\n                ctx->DrawIndexedInstanced(pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);\n            }\n            idx_offset += pcmd->ElemCount;\n        }\n        vtx_offset += cmd_list->VtxBuffer.Size;\n    }\n}\n\nstatic void ImGui_ImplDX12_CreateFontsTexture()\n{\n    // Build texture atlas\n    ImGuiIO& io = ImGui::GetIO();\n    unsigned char* pixels;\n    int width, height;\n    io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);\n\n    // Upload texture to graphics system\n    {\n        D3D12_HEAP_PROPERTIES props;\n        memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES));\n        props.Type = D3D12_HEAP_TYPE_DEFAULT;\n        props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;\n        props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;\n\n        D3D12_RESOURCE_DESC desc;\n        ZeroMemory(&desc, sizeof(desc));\n        desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;\n        desc.Alignment = 0;\n        desc.Width = width;\n        desc.Height = height;\n        desc.DepthOrArraySize = 1;\n        desc.MipLevels = 1;\n        desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;\n        desc.SampleDesc.Count = 1;\n        desc.SampleDesc.Quality = 0;\n        desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;\n        desc.Flags = D3D12_RESOURCE_FLAG_NONE;\n\n        ID3D12Resource* pTexture = NULL;\n        g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,\n            D3D12_RESOURCE_STATE_COPY_DEST, NULL, IID_PPV_ARGS(&pTexture));\n\n        UINT uploadPitch = (width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u);\n        UINT uploadSize = height * uploadPitch;\n        desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;\n        desc.Alignment = 0;\n        desc.Width = uploadSize;\n        desc.Height = 1;\n        desc.DepthOrArraySize = 1;\n        desc.MipLevels = 1;\n        desc.Format = DXGI_FORMAT_UNKNOWN;\n        desc.SampleDesc.Count = 1;\n        desc.SampleDesc.Quality = 0;\n        desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;\n        desc.Flags = D3D12_RESOURCE_FLAG_NONE;\n\n        props.Type = D3D12_HEAP_TYPE_UPLOAD;\n        props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;\n        props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;\n\n        ID3D12Resource* uploadBuffer = NULL;\n        HRESULT hr = g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc,\n            D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&uploadBuffer));\n        IM_ASSERT(SUCCEEDED(hr));\n\n        void* mapped = NULL;\n        D3D12_RANGE range = { 0, uploadSize };\n        hr = uploadBuffer->Map(0, &range, &mapped);\n        IM_ASSERT(SUCCEEDED(hr));\n        for (int y = 0; y < height; y++)\n            memcpy((void*) ((uintptr_t) mapped + y * uploadPitch), pixels + y * width * 4, width * 4);\n        uploadBuffer->Unmap(0, &range);\n\n        D3D12_TEXTURE_COPY_LOCATION srcLocation = {};\n        srcLocation.pResource = uploadBuffer;\n        srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;\n        srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;\n        srcLocation.PlacedFootprint.Footprint.Width = width;\n        srcLocation.PlacedFootprint.Footprint.Height = height;\n        srcLocation.PlacedFootprint.Footprint.Depth = 1;\n        srcLocation.PlacedFootprint.Footprint.RowPitch = uploadPitch;\n\n        D3D12_TEXTURE_COPY_LOCATION dstLocation = {};\n        dstLocation.pResource = pTexture;\n        dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;\n        dstLocation.SubresourceIndex = 0;\n\n        D3D12_RESOURCE_BARRIER barrier = {};\n        barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;\n        barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;\n        barrier.Transition.pResource   = pTexture;\n        barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;\n        barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;\n        barrier.Transition.StateAfter  = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;\n\n        ID3D12Fence* fence = NULL;\n        hr = g_pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));\n        IM_ASSERT(SUCCEEDED(hr));\n\n        HANDLE event = CreateEvent(0, 0, 0, 0);\n        IM_ASSERT(event != NULL);\n\n        D3D12_COMMAND_QUEUE_DESC queueDesc = {};\n        queueDesc.Type     = D3D12_COMMAND_LIST_TYPE_DIRECT;\n        queueDesc.Flags    = D3D12_COMMAND_QUEUE_FLAG_NONE;\n        queueDesc.NodeMask = 1;\n\n        ID3D12CommandQueue* cmdQueue = NULL;\n        hr = g_pd3dDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&cmdQueue));\n        IM_ASSERT(SUCCEEDED(hr));\n\n        ID3D12CommandAllocator* cmdAlloc = NULL;\n        hr = g_pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc));\n        IM_ASSERT(SUCCEEDED(hr));\n\n        ID3D12GraphicsCommandList* cmdList = NULL;\n        hr = g_pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, cmdAlloc, NULL, IID_PPV_ARGS(&cmdList));\n        IM_ASSERT(SUCCEEDED(hr));\n\n        cmdList->CopyTextureRegion(&dstLocation, 0, 0, 0, &srcLocation, NULL);\n        cmdList->ResourceBarrier(1, &barrier);\n\n        hr = cmdList->Close();\n        IM_ASSERT(SUCCEEDED(hr));\n\n        cmdQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*) &cmdList);\n        hr = cmdQueue->Signal(fence, 1);\n        IM_ASSERT(SUCCEEDED(hr));\n\n        fence->SetEventOnCompletion(1, event);\n        WaitForSingleObject(event, INFINITE);\n\n        cmdList->Release();\n        cmdAlloc->Release();\n        cmdQueue->Release();\n        CloseHandle(event);\n        fence->Release();\n        uploadBuffer->Release();\n\n        // Create texture view\n        D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;\n        ZeroMemory(&srvDesc, sizeof(srvDesc));\n        srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;\n        srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;\n        srvDesc.Texture2D.MipLevels = desc.MipLevels;\n        srvDesc.Texture2D.MostDetailedMip = 0;\n        srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;\n        g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, g_hFontSrvCpuDescHandle);\n        if (g_pFontTextureResource != NULL)\n            g_pFontTextureResource->Release();\n        g_pFontTextureResource = pTexture;\n    }\n\n    // Store our identifier\n    static_assert(sizeof(ImTextureID) >= sizeof(g_hFontSrvGpuDescHandle.ptr), \"Can't pack descriptor handle into TexID, 32-bit not supported yet.\");\n    io.Fonts->TexID = (ImTextureID)g_hFontSrvGpuDescHandle.ptr;\n}\n\nbool    ImGui_ImplDX12_CreateDeviceObjects()\n{\n    if (!g_pd3dDevice)\n        return false;\n    if (g_pPipelineState)\n        ImGui_ImplDX12_InvalidateDeviceObjects();\n\n    // Create the root signature\n    {\n        D3D12_DESCRIPTOR_RANGE descRange = {};\n        descRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;\n        descRange.NumDescriptors = 1;\n        descRange.BaseShaderRegister = 0;\n        descRange.RegisterSpace = 0;\n        descRange.OffsetInDescriptorsFromTableStart = 0;\n\n        D3D12_ROOT_PARAMETER param[2] = {};\n\n        param[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;\n        param[0].Constants.ShaderRegister = 0;\n        param[0].Constants.RegisterSpace = 0;\n        param[0].Constants.Num32BitValues = 16;\n        param[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;\n\n        param[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;\n        param[1].DescriptorTable.NumDescriptorRanges = 1;\n        param[1].DescriptorTable.pDescriptorRanges = &descRange;\n        param[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;\n\n        D3D12_STATIC_SAMPLER_DESC staticSampler = {};\n        staticSampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;\n        staticSampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP;\n        staticSampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP;\n        staticSampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP;\n        staticSampler.MipLODBias = 0.f;\n        staticSampler.MaxAnisotropy = 0;\n        staticSampler.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;\n        staticSampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;\n        staticSampler.MinLOD = 0.f;\n        staticSampler.MaxLOD = 0.f;\n        staticSampler.ShaderRegister = 0;\n        staticSampler.RegisterSpace = 0;\n        staticSampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;\n\n        D3D12_ROOT_SIGNATURE_DESC desc = {};\n        desc.NumParameters = _countof(param);\n        desc.pParameters = param;\n        desc.NumStaticSamplers = 1;\n        desc.pStaticSamplers = &staticSampler;\n        desc.Flags =\n            D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT |\n            D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS |\n            D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |\n            D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS;\n\n        ID3DBlob* blob = NULL;\n        if (D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, NULL) != S_OK)\n            return false;\n\n        g_pd3dDevice->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&g_pRootSignature));\n        blob->Release();\n    }\n\n    // By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)\n    // If you would like to use this DX12 sample code but remove this dependency you can:\n    //  1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]\n    //  2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.\n    // See https://github.com/ocornut/imgui/pull/638 for sources and details.\n\n    D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc;\n    memset(&psoDesc, 0, sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC));\n    psoDesc.NodeMask = 1;\n    psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;\n    psoDesc.pRootSignature = g_pRootSignature;\n    psoDesc.SampleMask = UINT_MAX;\n    psoDesc.NumRenderTargets = 1;\n    psoDesc.RTVFormats[0] = g_RTVFormat;\n    psoDesc.SampleDesc.Count = 1;\n    psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;\n\n    // Create the vertex shader\n    {\n        static const char* vertexShader =\n            \"cbuffer vertexBuffer : register(b0) \\\n            {\\\n              float4x4 ProjectionMatrix; \\\n            };\\\n            struct VS_INPUT\\\n            {\\\n              float2 pos : POSITION;\\\n              float4 col : COLOR0;\\\n              float2 uv  : TEXCOORD0;\\\n            };\\\n            \\\n            struct PS_INPUT\\\n            {\\\n              float4 pos : SV_POSITION;\\\n              float4 col : COLOR0;\\\n              float2 uv  : TEXCOORD0;\\\n            };\\\n            \\\n            PS_INPUT main(VS_INPUT input)\\\n            {\\\n              PS_INPUT output;\\\n              output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\\\n              output.col = input.col;\\\n              output.uv  = input.uv;\\\n              return output;\\\n            }\";\n\n        D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, \"main\", \"vs_5_0\", 0, 0, &g_pVertexShaderBlob, NULL);\n        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!\n            return false;\n        psoDesc.VS = { g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize() };\n\n        // Create the input layout\n        static D3D12_INPUT_ELEMENT_DESC local_layout[] = {\n            { \"POSITION\", 0, DXGI_FORMAT_R32G32_FLOAT,   0, IM_OFFSETOF(ImDrawVert, pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n            { \"TEXCOORD\", 0, DXGI_FORMAT_R32G32_FLOAT,   0, IM_OFFSETOF(ImDrawVert, uv),  D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n            { \"COLOR\",    0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, IM_OFFSETOF(ImDrawVert, col), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n        };\n        psoDesc.InputLayout = { local_layout, 3 };\n    }\n\n    // Create the pixel shader\n    {\n        static const char* pixelShader =\n            \"struct PS_INPUT\\\n            {\\\n              float4 pos : SV_POSITION;\\\n              float4 col : COLOR0;\\\n              float2 uv  : TEXCOORD0;\\\n            };\\\n            SamplerState sampler0 : register(s0);\\\n            Texture2D texture0 : register(t0);\\\n            \\\n            float4 main(PS_INPUT input) : SV_Target\\\n            {\\\n              float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \\\n              return out_col; \\\n            }\";\n\n        D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, \"main\", \"ps_5_0\", 0, 0, &g_pPixelShaderBlob, NULL);\n        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!\n            return false;\n        psoDesc.PS = { g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize() };\n    }\n\n    // Create the blending setup\n    {\n        D3D12_BLEND_DESC& desc = psoDesc.BlendState;\n        desc.AlphaToCoverageEnable = false;\n        desc.RenderTarget[0].BlendEnable = true;\n        desc.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA;\n        desc.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA;\n        desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;\n        desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA;\n        desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO;\n        desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;\n        desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;\n    }\n\n    // Create the rasterizer state\n    {\n        D3D12_RASTERIZER_DESC& desc = psoDesc.RasterizerState;\n        desc.FillMode = D3D12_FILL_MODE_SOLID;\n        desc.CullMode = D3D12_CULL_MODE_NONE;\n        desc.FrontCounterClockwise = FALSE;\n        desc.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;\n        desc.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;\n        desc.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;\n        desc.DepthClipEnable = true;\n        desc.MultisampleEnable = FALSE;\n        desc.AntialiasedLineEnable = FALSE;\n        desc.ForcedSampleCount = 0;\n        desc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;\n    }\n\n    // Create depth-stencil State\n    {\n        D3D12_DEPTH_STENCIL_DESC& desc = psoDesc.DepthStencilState;\n        desc.DepthEnable = false;\n        desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;\n        desc.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;\n        desc.StencilEnable = false;\n        desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;\n        desc.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;\n        desc.BackFace = desc.FrontFace;\n    }\n\n    if (g_pd3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&g_pPipelineState)) != S_OK)\n        return false;\n\n    ImGui_ImplDX12_CreateFontsTexture();\n\n    return true;\n}\n\nIMGUI_IMPL_API bool ImGui_ImplDX12_IsInitialized()\n{\n\treturn g_initialized;\n}\n\nvoid    ImGui_ImplDX12_InvalidateDeviceObjects()\n{\n    if (!g_pd3dDevice)\n        return;\n\n    ImGuiIO& io = ImGui::GetIO();\n    if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }\n    if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }\n    if (g_pRootSignature) { g_pRootSignature->Release(); g_pRootSignature = NULL; }\n    if (g_pPipelineState) { g_pPipelineState->Release(); g_pPipelineState = NULL; }\n    if (g_pFontTextureResource) { g_pFontTextureResource->Release(); g_pFontTextureResource = NULL; io.Fonts->TexID = NULL; } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.\n    for (UINT i = 0; i < g_numFramesInFlight; i++)\n    {\n        FrameResources* fr = &g_pFrameResources[i];\n        if (fr->IndexBuffer)  { fr->IndexBuffer->Release();  fr->IndexBuffer = NULL; }\n        if (fr->VertexBuffer) { fr->VertexBuffer->Release(); fr->VertexBuffer = NULL; }\n    }\n}\n\nbool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format,\n                         D3D12_CPU_DESCRIPTOR_HANDLE font_srv_cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE font_srv_gpu_desc_handle)\n{\n    // Setup back-end capabilities flags\n    ImGuiIO& io = ImGui::GetIO();\n    io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports;    // We can create multi-viewports on the Renderer side (optional) // FIXME-VIEWPORT: Actually unfinished..\n    io.BackendRendererName = \"imgui_impl_dx12\";\n\n    g_pd3dDevice = device;\n    g_RTVFormat = rtv_format;\n    g_hFontSrvCpuDescHandle = font_srv_cpu_desc_handle;\n    g_hFontSrvGpuDescHandle = font_srv_gpu_desc_handle;\n    g_pFrameResources = new FrameResources[num_frames_in_flight];\n    g_numFramesInFlight = num_frames_in_flight;\n    g_frameIndex = UINT_MAX;\n\n    // Create buffers with a default size (they will later be grown as needed)\n    for (int i = 0; i < num_frames_in_flight; i++)\n    {\n        FrameResources* fr = &g_pFrameResources[i];\n        fr->IndexBuffer = NULL;\n        fr->VertexBuffer = NULL;\n        fr->IndexBufferSize = 10000;\n        fr->VertexBufferSize = 5000;\n    }\n\n    if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)\n        ImGui_ImplDX12_InitPlatformInterface();\n\n\tg_initialized = true;\n\n    return true;\n}\n\nvoid ImGui_ImplDX12_Shutdown()\n{\n    ImGui_ImplDX12_ShutdownPlatformInterface();\n    ImGui_ImplDX12_InvalidateDeviceObjects();\n    delete[] g_pFrameResources;\n    g_pFrameResources = NULL;\n    g_pd3dDevice = NULL;\n    g_hFontSrvCpuDescHandle.ptr = 0;\n    g_hFontSrvGpuDescHandle.ptr = 0;\n    g_numFramesInFlight = 0;\n    g_frameIndex = UINT_MAX;\n\n\tg_initialized = false;\n}\n\nvoid ImGui_ImplDX12_NewFrame()\n{\n    if (!g_pPipelineState)\n        ImGui_ImplDX12_CreateDeviceObjects();\n}\n\n//--------------------------------------------------------------------------------------------------------\n// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT\n// This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously.\n// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..\n//--------------------------------------------------------------------------------------------------------\n\nstruct ImGuiViewportDataDx12\n{\n    IDXGISwapChain3*            SwapChain;\n\n    ImGuiViewportDataDx12() { SwapChain = NULL; }\n    ~ImGuiViewportDataDx12() { IM_ASSERT(SwapChain == NULL); }\n};\n\nstatic void ImGui_ImplDX12_CreateWindow(ImGuiViewport* viewport)\n{\n    ImGuiViewportDataDx12* data = IM_NEW(ImGuiViewportDataDx12)();\n    viewport->RendererUserData = data;\n    IM_ASSERT(0);\n\n    /*\n    // FIXME-PLATFORM\n    HWND hwnd = (HWND)viewport->PlatformHandle;\n    IM_ASSERT(hwnd != 0);\n\n    // Create swap chain\n    DXGI_SWAP_CHAIN_DESC sd;\n    ZeroMemory(&sd, sizeof(sd));\n    sd.BufferDesc.Width = (UINT)viewport->Size.x;\n    sd.BufferDesc.Height = (UINT)viewport->Size.y;\n    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;\n    sd.SampleDesc.Count = 1;\n    sd.SampleDesc.Quality = 0;\n    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;\n    sd.BufferCount = 1;\n    sd.OutputWindow = hwnd;\n    sd.Windowed = TRUE;\n    sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;\n    sd.Flags = 0;\n\n    IM_ASSERT(data->SwapChain == NULL && data->RTView == NULL);\n    g_pFactory->CreateSwapChain(g_pd3dDevice, &sd, &data->SwapChain);\n\n    // Create the render target\n    if (data->SwapChain)\n    {\n        ID3D11Texture2D* pBackBuffer;\n        data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));\n        g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView);\n        pBackBuffer->Release();\n    }\n    */\n}\n\nstatic void ImGui_ImplDX12_DestroyWindow(ImGuiViewport* viewport)\n{\n    // The main viewport (owned by the application) will always have RendererUserData == NULL since we didn't create the data for it.\n    if (ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData)\n    {\n        IM_ASSERT(0);\n        /*\n        if (data->SwapChain)\n            data->SwapChain->Release();\n        data->SwapChain = NULL;\n        if (data->RTView)\n            data->RTView->Release();\n        data->RTView = NULL;\n        IM_DELETE(data);\n        */\n    }\n    viewport->RendererUserData = NULL;\n}\n\nstatic void ImGui_ImplDX12_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)\n{\n    ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData;\n    IM_ASSERT(0);\n    (void)data; (void)size;\n    /*\n    if (data->RTView)\n    {\n        data->RTView->Release();\n        data->RTView = NULL;\n    }\n    if (data->SwapChain)\n    {\n        ID3D11Texture2D* pBackBuffer = NULL;\n        data->SwapChain->ResizeBuffers(0, (UINT)size.x, (UINT)size.y, DXGI_FORMAT_UNKNOWN, 0);\n        data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));\n        g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView);\n        pBackBuffer->Release();\n    }\n    */\n}\n\n// arg = ID3D12GraphicsCommandList*\nstatic void ImGui_ImplDX12_RenderWindow(ImGuiViewport* viewport, void* renderer_arg)\n{\n    ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData;\n    IM_ASSERT(0);\n    (void)data;\n\n    ID3D12GraphicsCommandList* command_list = (ID3D12GraphicsCommandList*)renderer_arg;\n\n    /*\n    ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);\n    g_pd3dDeviceContext->OMSetRenderTargets(1, &data->RTView, NULL);\n    if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear))\n        g_pd3dDeviceContext->ClearRenderTargetView(data->RTView, (float*)&clear_color);\n    */\n    ImGui_ImplDX12_RenderDrawData(viewport->DrawData, command_list);\n}\n\nstatic void ImGui_ImplDX12_SwapBuffers(ImGuiViewport* viewport, void*)\n{\n    ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData;\n    IM_ASSERT(0);\n    (void)data;\n    /*\n    data->SwapChain->Present(0, 0); // Present without vsync\n    */\n}\n\nvoid ImGui_ImplDX12_InitPlatformInterface()\n{\n    ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();\n    platform_io.Renderer_CreateWindow = ImGui_ImplDX12_CreateWindow;\n    platform_io.Renderer_DestroyWindow = ImGui_ImplDX12_DestroyWindow;\n    platform_io.Renderer_SetWindowSize = ImGui_ImplDX12_SetWindowSize;\n    platform_io.Renderer_RenderWindow = ImGui_ImplDX12_RenderWindow;\n    platform_io.Renderer_SwapBuffers = ImGui_ImplDX12_SwapBuffers;\n}\n\nvoid ImGui_ImplDX12_ShutdownPlatformInterface()\n{\n    ImGui::DestroyPlatformWindows();\n}\n"
  },
  {
    "path": "src/imgui/imgui_impl_dx12.hpp",
    "content": "// dear imgui: Renderer for DirectX12\n// This needs to be used along with a Platform Binding (e.g. Win32)\n\n// Implemented features:\n//  [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.\n// Issues:\n//  [ ] 64-bit only for now! (Because sizeof(ImTextureId) == sizeof(void*)). See github.com/ocornut/imgui/pull/301\n\n// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.\n// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.\n// https://github.com/ocornut/imgui\n\n#pragma once\n\n#include <cstdint>\n\nenum DXGI_FORMAT : std::int32_t;\nstruct ID3D12Device;\nstruct ID3D12GraphicsCommandList;\nstruct D3D12_CPU_DESCRIPTOR_HANDLE;\nstruct D3D12_GPU_DESCRIPTOR_HANDLE;\n\n// cmd_list is the command list that the implementation will use to render imgui draw lists.\n// Before calling the render function, caller must prepare cmd_list by resetting it and setting the appropriate\n// render target and descriptor heap that contains font_srv_cpu_desc_handle/font_srv_gpu_desc_handle.\n// font_srv_cpu_desc_handle and font_srv_gpu_desc_handle are handles to a single SRV descriptor to use for the internal font texture.\nIMGUI_IMPL_API bool     ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format,\n                                            D3D12_CPU_DESCRIPTOR_HANDLE font_srv_cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE font_srv_gpu_desc_handle);\nIMGUI_IMPL_API void     ImGui_ImplDX12_Shutdown();\nIMGUI_IMPL_API void     ImGui_ImplDX12_NewFrame();\nIMGUI_IMPL_API void     ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* graphics_command_list);\n\n// Use if you want to reset your rendering device without losing ImGui state.\nIMGUI_IMPL_API void     ImGui_ImplDX12_InvalidateDeviceObjects();\nIMGUI_IMPL_API bool     ImGui_ImplDX12_CreateDeviceObjects();\n\n// Wisp Extension\nIMGUI_IMPL_API bool\t\tImGui_ImplDX12_IsInitialized();"
  },
  {
    "path": "src/imgui/imgui_impl_win32.cpp",
    "content": "// dear imgui: Platform Binding for Windows (standard windows API for 32 and 64 bits applications)\n// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)\n\n// Implemented features:\n//  [X] Platform: Clipboard support (for Win32 this is actually part of core imgui)\n//  [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.\n//  [X] Platform: Keyboard arrays indexed using VK_* Virtual Key Codes, e.g. ImGui::IsKeyPressed(VK_SPACE).\n//  [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.\n//  [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.\n\n#include \"imgui.hpp\"\n#include \"imgui_impl_win32.hpp\"\n#ifndef WIN32_LEAN_AND_MEAN\n#define WIN32_LEAN_AND_MEAN\n#endif\n#include <windows.h>\n#include <XInput.h>\n#include <tchar.h>\n\n// CHANGELOG\n// (minor and older changes stripped away, please see git history for details)\n//  2018-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.\n//  2019-01-17: Misc: Using GetForegroundWindow()+IsChild() instead of GetActiveWindow() to be compatible with windows created in a different thread or parent.\n//  2019-01-17: Inputs: Added support for mouse buttons 4 and 5 via WM_XBUTTON* messages.\n//  2019-01-15: Inputs: Added support for XInput gamepads (if ImGuiConfigFlags_NavEnableGamepad is set by user application).\n//  2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.\n//  2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.\n//  2018-06-10: Inputs: Fixed handling of mouse wheel messages to support fine position messages (typically sent by track-pads).\n//  2018-06-08: Misc: Extracted imgui_impl_win32.cpp/.h away from the old combined DX9/DX10/DX11/DX12 examples.\n//  2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.\n//  2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).\n//  2018-02-06: Inputs: Added mapping for ImGuiKey_Space.\n//  2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).\n//  2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.\n//  2018-01-20: Inputs: Added Horizontal Mouse Wheel support.\n//  2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.\n//  2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.\n//  2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.\n//  2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.\n//  2016-11-12: Inputs: Only call Win32 ::SetCursor(NULL) when io.MouseDrawCursor is set.\n\n// Win32 Data\nstatic HWND                 g_hWnd = 0;\nstatic INT64                g_Time = 0;\nstatic INT64                g_TicksPerSecond = 0;\nstatic ImGuiMouseCursor     g_LastMouseCursor = ImGuiMouseCursor_COUNT;\nstatic bool                 g_HasGamepad = false;\nstatic bool                 g_WantUpdateHasGamepad = true;\nstatic bool                 g_WantUpdateMonitors = true;\n\n// Forward Declarations\nstatic void ImGui_ImplWin32_InitPlatformInterface();\nstatic void ImGui_ImplWin32_ShutdownPlatformInterface();\nstatic void ImGui_ImplWin32_UpdateMonitors();\n\n// Functions\nbool    ImGui_ImplWin32_Init(void* hwnd)\n{\n    if (!::QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))\n        return false;\n    if (!::QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))\n        return false;\n\n    // Setup back-end capabilities flags\n    ImGuiIO& io = ImGui::GetIO();\n    io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;         // We can honor GetMouseCursor() values (optional)\n    io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos;          // We can honor io.WantSetMousePos requests (optional, rarely used)\n    io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports;    // We can create multi-viewports on the Platform side (optional)\n    io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)\n    io.BackendPlatformName = \"imgui_impl_win32\";\n\n    // Our mouse update function expect PlatformHandle to be filled for the main viewport\n    g_hWnd = (HWND)hwnd;\n    ImGuiViewport* main_viewport = ImGui::GetMainViewport();\n    main_viewport->PlatformHandle = (void*)g_hWnd;\n    if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)\n        ImGui_ImplWin32_InitPlatformInterface();\n\n    // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime.\n    io.KeyMap[ImGuiKey_Tab] = VK_TAB;\n    io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;\n    io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;\n    io.KeyMap[ImGuiKey_UpArrow] = VK_UP;\n    io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;\n    io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;\n    io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;\n    io.KeyMap[ImGuiKey_Home] = VK_HOME;\n    io.KeyMap[ImGuiKey_End] = VK_END;\n    io.KeyMap[ImGuiKey_Insert] = VK_INSERT;\n    io.KeyMap[ImGuiKey_Delete] = VK_DELETE;\n    io.KeyMap[ImGuiKey_Backspace] = VK_BACK;\n    io.KeyMap[ImGuiKey_Space] = VK_SPACE;\n    io.KeyMap[ImGuiKey_Enter] = VK_RETURN;\n    io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;\n    io.KeyMap[ImGuiKey_A] = 'A';\n    io.KeyMap[ImGuiKey_C] = 'C';\n    io.KeyMap[ImGuiKey_V] = 'V';\n    io.KeyMap[ImGuiKey_X] = 'X';\n    io.KeyMap[ImGuiKey_Y] = 'Y';\n    io.KeyMap[ImGuiKey_Z] = 'Z';\n\n    return true;\n}\n\nvoid    ImGui_ImplWin32_Shutdown()\n{\n    ImGui_ImplWin32_ShutdownPlatformInterface();\n    g_hWnd = (HWND)0;\n}\n\nstatic bool ImGui_ImplWin32_UpdateMouseCursor()\n{\n    ImGuiIO& io = ImGui::GetIO();\n    if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)\n        return false;\n\n    ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();\n    if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)\n    {\n        // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor\n        ::SetCursor(NULL);\n    }\n    else\n    {\n        // Show OS mouse cursor\n        LPTSTR win32_cursor = IDC_ARROW;\n        switch (imgui_cursor)\n        {\n        case ImGuiMouseCursor_Arrow:        win32_cursor = IDC_ARROW; break;\n        case ImGuiMouseCursor_TextInput:    win32_cursor = IDC_IBEAM; break;\n        case ImGuiMouseCursor_ResizeAll:    win32_cursor = IDC_SIZEALL; break;\n        case ImGuiMouseCursor_ResizeEW:     win32_cursor = IDC_SIZEWE; break;\n        case ImGuiMouseCursor_ResizeNS:     win32_cursor = IDC_SIZENS; break;\n        case ImGuiMouseCursor_ResizeNESW:   win32_cursor = IDC_SIZENESW; break;\n        case ImGuiMouseCursor_ResizeNWSE:   win32_cursor = IDC_SIZENWSE; break;\n        case ImGuiMouseCursor_Hand:         win32_cursor = IDC_HAND; break;\n        }\n        ::SetCursor(::LoadCursor(NULL, win32_cursor));\n    }\n    return true;\n}\n\n// This code supports multi-viewports (multiple OS Windows mapped into different Dear ImGui viewports)\n// Because of that, it is a little more complicated than your typical single-viewport binding code!\nstatic void ImGui_ImplWin32_UpdateMousePos()\n{\n    ImGuiIO& io = ImGui::GetIO();\n\n    // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)\n    // (When multi-viewports are enabled, all imgui positions are same as OS positions)\n    if (io.WantSetMousePos)\n    {\n        POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };\n        if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0)\n            ::ClientToScreen(g_hWnd, &pos);\n        ::SetCursorPos(pos.x, pos.y);\n    }\n\n    io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);\n    io.MouseHoveredViewport = 0;\n\n    // Set imgui mouse position\n    POINT mouse_screen_pos;\n    if (!::GetCursorPos(&mouse_screen_pos))\n        return;\n    if (HWND focused_hwnd = ::GetForegroundWindow())\n    {\n        if (::IsChild(focused_hwnd, g_hWnd))\n            focused_hwnd = g_hWnd;\n        if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)\n        {\n            // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)\n            // This is the position you can get with GetCursorPos(). In theory adding viewport->Pos is also the reverse operation of doing ScreenToClient().\n            if (ImGui::FindViewportByPlatformHandle((void*)focused_hwnd) != NULL)\n                io.MousePos = ImVec2((float)mouse_screen_pos.x, (float)mouse_screen_pos.y);\n        }\n        else\n        {\n            // Single viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window.)\n            // This is the position you can get with GetCursorPos() + ScreenToClient() or from WM_MOUSEMOVE.\n            if (focused_hwnd == g_hWnd)\n            {\n                POINT mouse_client_pos = mouse_screen_pos;\n                ::ScreenToClient(focused_hwnd, &mouse_client_pos);\n                io.MousePos = ImVec2((float)mouse_client_pos.x, (float)mouse_client_pos.y);\n            }\n        }\n    }\n\n    // (Optional) When using multiple viewports: set io.MouseHoveredViewport to the viewport the OS mouse cursor is hovering.\n    // Important: this information is not easy to provide and many high-level windowing library won't be able to provide it correctly, because\n    // - This is _ignoring_ viewports with the ImGuiViewportFlags_NoInputs flag (pass-through windows).\n    // - This is _regardless_ of whether another viewport is focused or being dragged from.\n    // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the back-end, imgui will ignore this field and infer the information by relying on the\n    // rectangles and last focused time of every viewports it knows about. It will be unaware of foreign windows that may be sitting between or over your windows.\n    if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos))\n        if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hovered_hwnd))\n            if ((viewport->Flags & ImGuiViewportFlags_NoInputs) == 0) // FIXME: We still get our NoInputs window with WM_NCHITTEST/HTTRANSPARENT code when decorated?\n                io.MouseHoveredViewport = viewport->ID;\n}\n\n#ifdef _MSC_VER\n#pragma comment(lib, \"xinput\")\n#endif\n\n// Gamepad navigation mapping\nstatic void ImGui_ImplWin32_UpdateGamepads()\n{\n    ImGuiIO& io = ImGui::GetIO();\n    memset(io.NavInputs, 0, sizeof(io.NavInputs));\n    if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)\n        return;\n\n    // Calling XInputGetState() every frame on disconnected gamepads is unfortunately too slow.\n    // Instead we refresh gamepad availability by calling XInputGetCapabilities() _only_ after receiving WM_DEVICECHANGE.\n    if (g_WantUpdateHasGamepad)\n    {\n        XINPUT_CAPABILITIES caps;\n        g_HasGamepad = (XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS);\n        g_WantUpdateHasGamepad = false;\n    }\n\n    XINPUT_STATE xinput_state;\n    io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;\n    if (g_HasGamepad && XInputGetState(0, &xinput_state) == ERROR_SUCCESS)\n    {\n        const XINPUT_GAMEPAD& gamepad = xinput_state.Gamepad;\n        io.BackendFlags |= ImGuiBackendFlags_HasGamepad;\n\n        #define MAP_BUTTON(NAV_NO, BUTTON_ENUM)     { io.NavInputs[NAV_NO] = (gamepad.wButtons & BUTTON_ENUM) ? 1.0f : 0.0f; }\n        #define MAP_ANALOG(NAV_NO, VALUE, V0, V1)   { float vn = (float)(VALUE - V0) / (float)(V1 - V0); if (vn > 1.0f) vn = 1.0f; if (vn > 0.0f && io.NavInputs[NAV_NO] < vn) io.NavInputs[NAV_NO] = vn; }\n        MAP_BUTTON(ImGuiNavInput_Activate,      XINPUT_GAMEPAD_A);              // Cross / A\n        MAP_BUTTON(ImGuiNavInput_Cancel,        XINPUT_GAMEPAD_B);              // Circle / B\n        MAP_BUTTON(ImGuiNavInput_Menu,          XINPUT_GAMEPAD_X);              // Square / X\n        MAP_BUTTON(ImGuiNavInput_Input,         XINPUT_GAMEPAD_Y);              // Triangle / Y\n        MAP_BUTTON(ImGuiNavInput_DpadLeft,      XINPUT_GAMEPAD_DPAD_LEFT);      // D-Pad Left\n        MAP_BUTTON(ImGuiNavInput_DpadRight,     XINPUT_GAMEPAD_DPAD_RIGHT);     // D-Pad Right\n        MAP_BUTTON(ImGuiNavInput_DpadUp,        XINPUT_GAMEPAD_DPAD_UP);        // D-Pad Up\n        MAP_BUTTON(ImGuiNavInput_DpadDown,      XINPUT_GAMEPAD_DPAD_DOWN);      // D-Pad Down\n        MAP_BUTTON(ImGuiNavInput_FocusPrev,     XINPUT_GAMEPAD_LEFT_SHOULDER);  // L1 / LB\n        MAP_BUTTON(ImGuiNavInput_FocusNext,     XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB\n        MAP_BUTTON(ImGuiNavInput_TweakSlow,     XINPUT_GAMEPAD_LEFT_SHOULDER);  // L1 / LB\n        MAP_BUTTON(ImGuiNavInput_TweakFast,     XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB\n        MAP_ANALOG(ImGuiNavInput_LStickLeft,    gamepad.sThumbLX,  -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);\n        MAP_ANALOG(ImGuiNavInput_LStickRight,   gamepad.sThumbLX,  +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);\n        MAP_ANALOG(ImGuiNavInput_LStickUp,      gamepad.sThumbLY,  +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);\n        MAP_ANALOG(ImGuiNavInput_LStickDown,    gamepad.sThumbLY,  -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32767);\n        #undef MAP_BUTTON\n        #undef MAP_ANALOG\n    }\n}\n\nvoid    ImGui_ImplWin32_NewFrame()\n{\n    ImGuiIO& io = ImGui::GetIO();\n    IM_ASSERT(io.Fonts->IsBuilt() && \"Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().\");\n\n    // Setup display size (every frame to accommodate for window resizing)\n    RECT rect;\n    ::GetClientRect(g_hWnd, &rect);\n    io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));\n    if (g_WantUpdateMonitors)\n        ImGui_ImplWin32_UpdateMonitors();\n\n    // Setup time step\n    INT64 current_time;\n    ::QueryPerformanceCounter((LARGE_INTEGER *)&current_time);\n    io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;\n    g_Time = current_time;\n\n    // Read keyboard modifiers inputs\n    io.KeyCtrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0;\n    io.KeyShift = (::GetKeyState(VK_SHIFT) & 0x8000) != 0;\n    io.KeyAlt = (::GetKeyState(VK_MENU) & 0x8000) != 0;\n    io.KeySuper = false;\n    // io.KeysDown[], io.MousePos, io.MouseDown[], io.MouseWheel: filled by the WndProc handler below.\n\n    // Update OS mouse position\n    ImGui_ImplWin32_UpdateMousePos();\n\n    // Update OS mouse cursor with the cursor requested by imgui\n    ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();\n    if (g_LastMouseCursor != mouse_cursor)\n    {\n        g_LastMouseCursor = mouse_cursor;\n        ImGui_ImplWin32_UpdateMouseCursor();\n    }\n\n    // Update game controllers (if enabled and available)\n    ImGui_ImplWin32_UpdateGamepads();\n}\n\n// Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.\n#ifndef WM_MOUSEHWHEEL\n#define WM_MOUSEHWHEEL 0x020E\n#endif\n#ifndef DBT_DEVNODES_CHANGED\n#define DBT_DEVNODES_CHANGED 0x0007\n#endif\n\n// Process Win32 mouse/keyboard inputs.\n// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.\n// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.\n// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.\n// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.\n// PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinates when dragging mouse outside of our window bounds.\n// PS: We treat DBLCLK messages as regular mouse down messages, so this code will work on windows classes that have the CS_DBLCLKS flag set. Our own example app code doesn't set this flag.\nIMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)\n{\n    if (ImGui::GetCurrentContext() == NULL)\n        return 0;\n\n    ImGuiIO& io = ImGui::GetIO();\n    switch (msg)\n    {\n    case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:\n    case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:\n    case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:\n    case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK:\n    {\n        int button = 0;\n        if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; }\n        if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; }\n        if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; }\n        if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }\n        if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)\n            ::SetCapture(hwnd);\n        io.MouseDown[button] = true;\n        return 0;\n    }\n    case WM_LBUTTONUP:\n    case WM_RBUTTONUP:\n    case WM_MBUTTONUP:\n    case WM_XBUTTONUP:\n    {\n        int button = 0;\n        if (msg == WM_LBUTTONUP) { button = 0; }\n        if (msg == WM_RBUTTONUP) { button = 1; }\n        if (msg == WM_MBUTTONUP) { button = 2; }\n        if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }\n        io.MouseDown[button] = false;\n        if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)\n            ::ReleaseCapture();\n        return 0;\n    }\n    case WM_MOUSEWHEEL:\n        io.MouseWheel += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;\n        return 0;\n    case WM_MOUSEHWHEEL:\n        io.MouseWheelH += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;\n        return 0;\n    case WM_KEYDOWN:\n    case WM_SYSKEYDOWN:\n        if (wParam < 256)\n            io.KeysDown[wParam] = 1;\n        return 0;\n    case WM_KEYUP:\n    case WM_SYSKEYUP:\n        if (wParam < 256)\n            io.KeysDown[wParam] = 0;\n        return 0;\n    case WM_CHAR:\n        // You can also use ToAscii()+GetKeyboardState() to retrieve characters.\n        if (wParam > 0 && wParam < 0x10000)\n            io.AddInputCharacter((unsigned short)wParam);\n        return 0;\n    case WM_SETCURSOR:\n        if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor())\n            return 1;\n        return 0;\n    case WM_DEVICECHANGE:\n        if ((UINT)wParam == DBT_DEVNODES_CHANGED)\n            g_WantUpdateHasGamepad = true;\n        return 0;\n    case WM_DISPLAYCHANGE:\n        g_WantUpdateMonitors = true;\n        return 0;\n    }\n    return 0;\n}\n\n//--------------------------------------------------------------------------------------------------------\n// DPI handling\n// Those in theory should be simple calls but Windows has multiple ways to handle DPI, and most of them\n// require recent Windows versions at runtime or recent Windows SDK at compile-time. Neither we want to depend on.\n// So we dynamically select and load those functions to avoid dependencies. This is the scheme successfully \n// used by GLFW (from which we borrowed some of the code here) and other applications aiming to be portable.\n//---------------------------------------------------------------------------------------------------------\n// At this point ImGui_ImplWin32_EnableDpiAwareness() is just a helper called by main.cpp, we don't call it automatically.\n//---------------------------------------------------------------------------------------------------------\n\nstatic BOOL IsWindowsVersionOrGreater(WORD major, WORD minor, WORD sp)\n{\n    OSVERSIONINFOEXW osvi = { sizeof(osvi), major, minor, 0, 0,{ 0 }, sp };\n    DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR;\n    ULONGLONG cond = VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);\n    cond = VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);\n    cond = VerSetConditionMask(cond, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);\n    return VerifyVersionInfoW(&osvi, mask, cond);\n}\n#define IsWindows8Point1OrGreater()  IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) // _WIN32_WINNT_WINBLUE\n#define IsWindows10OrGreater()       IsWindowsVersionOrGreater(HIBYTE(0x0A00), LOBYTE(0x0A00), 0) // _WIN32_WINNT_WIN10\n\n#ifndef DPI_ENUMS_DECLARED\ntypedef enum { PROCESS_DPI_UNAWARE = 0, PROCESS_SYSTEM_DPI_AWARE = 1, PROCESS_PER_MONITOR_DPI_AWARE = 2 } PROCESS_DPI_AWARENESS;\ntypedef enum { MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2, MDT_DEFAULT = MDT_EFFECTIVE_DPI } MONITOR_DPI_TYPE;\n#endif\n#ifndef _DPI_AWARENESS_CONTEXTS_\nDECLARE_HANDLE(DPI_AWARENESS_CONTEXT);\n#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE    (DPI_AWARENESS_CONTEXT)-3\n#endif\n#ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2\n#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (DPI_AWARENESS_CONTEXT)-4\n#endif\ntypedef HRESULT(WINAPI * PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS);                     // Shcore.lib+dll, Windows 8.1\ntypedef HRESULT(WINAPI * PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*);        // Shcore.lib+dll, Windows 8.1\ntypedef DPI_AWARENESS_CONTEXT(WINAPI * PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib+dll, Windows 10 v1607 (Creators Update)\n\nvoid ImGui_ImplWin32_EnableDpiAwareness()\n{\n    // if (IsWindows10OrGreater()) // FIXME-DPI: This needs a manifest to succeed. Instead we try to grab the function pointer.\n    {\n        static HINSTANCE user32_dll = ::LoadLibraryA(\"user32.dll\"); // Reference counted per-process\n        if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, \"SetThreadDpiAwarenessContext\"))\n        {\n            SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);\n            return;\n        }\n    }\n    if (IsWindows8Point1OrGreater())\n    {\n        static HINSTANCE shcore_dll = ::LoadLibraryA(\"shcore.dll\"); // Reference counted per-process\n        if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, \"SetProcessDpiAwareness\"))\n            SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE);\n    }\n    else\n    {\n        SetProcessDPIAware();\n    }\n}\n\n#ifdef _MSC_VER\n#pragma comment(lib, \"gdi32\")   // GetDeviceCaps()\n#endif\n\nfloat ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor)\n{\n    UINT xdpi = 96, ydpi = 96;\n    if (::IsWindows8Point1OrGreater())\n    {\n        static HINSTANCE shcore_dll = ::LoadLibraryA(\"shcore.dll\"); // Reference counted per-process\n        if (PFN_GetDpiForMonitor GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, \"GetDpiForMonitor\"))\n            GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);\n    }\n    else\n    {\n        const HDC dc = ::GetDC(NULL);\n        xdpi = ::GetDeviceCaps(dc, LOGPIXELSX);\n        ydpi = ::GetDeviceCaps(dc, LOGPIXELSY);\n        ::ReleaseDC(NULL, dc);\n    }\n    IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!\n    return xdpi / 96.0f;\n}\n\nfloat ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd)\n{\n    HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST);\n    return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);\n}\n\n//--------------------------------------------------------------------------------------------------------\n// IME (Input Method Editor) basic support for e.g. Asian language users\n//--------------------------------------------------------------------------------------------------------\n\n#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) && !defined(__GNUC__)\n#define HAS_WIN32_IME   1\n#include <imm.h>\n#ifdef _MSC_VER\n#pragma comment(lib, \"imm32\")\n#endif\nstatic void ImGui_ImplWin32_SetImeInputPos(ImGuiViewport* viewport, ImVec2 pos)\n{\n    COMPOSITIONFORM cf = { CFS_FORCE_POSITION,{ (LONG)(pos.x - viewport->Pos.x), (LONG)(pos.y - viewport->Pos.y) },{ 0, 0, 0, 0 } };\n    if (HWND hwnd = (HWND)viewport->PlatformHandle)\n        if (HIMC himc = ::ImmGetContext(hwnd))\n        {\n            ::ImmSetCompositionWindow(himc, &cf);\n            ::ImmReleaseContext(hwnd, himc);\n        }\n}\n#else\n#define HAS_WIN32_IME   0\n#endif\n\n//--------------------------------------------------------------------------------------------------------\n// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT\n// This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously.\n// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..\n//--------------------------------------------------------------------------------------------------------\n\nstruct ImGuiViewportDataWin32\n{\n    HWND    Hwnd;\n    bool    HwndOwned;\n    DWORD   DwStyle;\n    DWORD   DwExStyle;\n\n    ImGuiViewportDataWin32() { Hwnd = NULL; HwndOwned = false;  DwStyle = DwExStyle = 0; }\n    ~ImGuiViewportDataWin32() { IM_ASSERT(Hwnd == NULL); }\n};\n\nstatic void ImGui_ImplWin32_GetWin32StyleFromViewportFlags(ImGuiViewportFlags flags, DWORD* out_style, DWORD* out_ex_style)\n{\n    if (flags & ImGuiViewportFlags_NoDecoration)\n        *out_style = WS_POPUP;\n    else\n        *out_style = WS_OVERLAPPEDWINDOW;\n\n    if (flags & ImGuiViewportFlags_NoTaskBarIcon)\n        *out_ex_style = WS_EX_TOOLWINDOW;\n    else\n        *out_ex_style = WS_EX_APPWINDOW;\n\n    if (flags & ImGuiViewportFlags_TopMost)\n        *out_ex_style |= WS_EX_TOPMOST;\n}\n\nstatic void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport)\n{\n    ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)();\n    viewport->PlatformUserData = data;\n\n    // Select style and parent window\n    ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &data->DwStyle, &data->DwExStyle);\n    HWND parent_window = NULL;\n    if (viewport->ParentViewportId != 0)\n        if (ImGuiViewport* parent_viewport = ImGui::FindViewportByID(viewport->ParentViewportId))\n            parent_window = (HWND)parent_viewport->PlatformHandle;\n\n    // Create window\n    RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) };\n    ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);\n    data->Hwnd = ::CreateWindowEx(\n        data->DwExStyle, _T(\"ImGui Platform\"), _T(\"Untitled\"), data->DwStyle,   // Style, class name, window name\n        rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,    // Window area\n        parent_window, NULL, ::GetModuleHandle(NULL), NULL);                    // Parent window, Menu, Instance, Param\n    data->HwndOwned = true;\n    viewport->PlatformRequestResize = false;\n    viewport->PlatformHandle = data->Hwnd;\n}\n\nstatic void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport)\n{\n    if (ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData)\n    {\n        if (::GetCapture() == data->Hwnd)\n        {\n            // Transfer capture so if we started dragging from a window that later disappears, we'll still receive the MOUSEUP event.\n            ::ReleaseCapture();\n            ::SetCapture(g_hWnd);\n        }\n        if (data->Hwnd && data->HwndOwned)\n            ::DestroyWindow(data->Hwnd);\n        data->Hwnd = NULL;\n        IM_DELETE(data);\n    }\n    viewport->PlatformUserData = viewport->PlatformHandle = NULL;\n}\n\nstatic void ImGui_ImplWin32_ShowWindow(ImGuiViewport* viewport)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)\n        ::ShowWindow(data->Hwnd, SW_SHOWNA);\n    else\n        ::ShowWindow(data->Hwnd, SW_SHOW);\n}\n\nstatic void ImGui_ImplWin32_UpdateWindow(ImGuiViewport* viewport)\n{\n    // (Optional) Update Win32 style if it changed _after_ creation. \n    // Generally they won't change unless configuration flags are changed, but advanced uses (such as manually rewriting viewport flags) make this useful.\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    DWORD new_style;\n    DWORD new_ex_style;\n    ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &new_style, &new_ex_style);\n\n    // Only reapply the flags that have been changed from our point of view (as other flags are being modified by Windows)\n    if (data->DwStyle != new_style || data->DwExStyle != new_ex_style)\n    {\n        data->DwStyle = new_style;\n        data->DwExStyle = new_ex_style;\n        ::SetWindowLong(data->Hwnd, GWL_STYLE, data->DwStyle);\n        ::SetWindowLong(data->Hwnd, GWL_EXSTYLE, data->DwExStyle);\n        RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) };\n        ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen\n        ::SetWindowPos(data->Hwnd, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);\n        ::ShowWindow(data->Hwnd, SW_SHOWNA); // This is necessary when we alter the style\n        viewport->PlatformRequestMove = viewport->PlatformRequestResize = true;\n    }\n}\n\nstatic ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    POINT pos = { 0, 0 };\n    ::ClientToScreen(data->Hwnd, &pos);\n    return ImVec2((float)pos.x, (float)pos.y);\n}\n\nstatic void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y };\n    ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);\n    ::SetWindowPos(data->Hwnd, NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);\n}\n\nstatic ImVec2 ImGui_ImplWin32_GetWindowSize(ImGuiViewport* viewport)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    RECT rect;\n    ::GetClientRect(data->Hwnd, &rect);\n    return ImVec2(float(rect.right - rect.left), float(rect.bottom - rect.top));\n}\n\nstatic void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y };\n    ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen\n    ::SetWindowPos(data->Hwnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);\n}\n\nstatic void ImGui_ImplWin32_SetWindowFocus(ImGuiViewport* viewport)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    ::BringWindowToTop(data->Hwnd);\n    ::SetForegroundWindow(data->Hwnd);\n    ::SetFocus(data->Hwnd);\n}\n\nstatic bool ImGui_ImplWin32_GetWindowFocus(ImGuiViewport* viewport)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    return ::GetForegroundWindow() == data->Hwnd;\n}\n\nstatic bool ImGui_ImplWin32_GetWindowMinimized(ImGuiViewport* viewport)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    return ::IsIconic(data->Hwnd) != 0;\n}\n\nstatic void ImGui_ImplWin32_SetWindowTitle(ImGuiViewport* viewport, const char* title)\n{\n    // ::SetWindowTextA() doesn't properly handle UTF-8 so we explicitely convert our string.\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    int n = ::MultiByteToWideChar(CP_UTF8, 0, title, -1, NULL, 0);\n    ImVector<wchar_t> title_w;\n    title_w.resize(n);\n    ::MultiByteToWideChar(CP_UTF8, 0, title, -1, title_w.Data, n);\n    ::SetWindowTextW(data->Hwnd, title_w.Data);\n}\n\nstatic void ImGui_ImplWin32_SetWindowAlpha(ImGuiViewport* viewport, float alpha)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f);\n    if (alpha < 1.0f)\n    {\n        DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED;\n        ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style);\n        ::SetLayeredWindowAttributes(data->Hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA);\n    }\n    else\n    {\n        DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED;\n        ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style);\n    }\n}\n\nstatic float ImGui_ImplWin32_GetWindowDpiScale(ImGuiViewport* viewport)\n{\n    ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;\n    IM_ASSERT(data->Hwnd != 0);\n    return ImGui_ImplWin32_GetDpiScaleForHwnd(data->Hwnd);\n}\n\n// FIXME-DPI: Testing DPI related ideas\nstatic void ImGui_ImplWin32_OnChangedViewport(ImGuiViewport* viewport)\n{\n    (void)viewport;\n#if 0\n    ImGuiStyle default_style;\n    //default_style.WindowPadding = ImVec2(0, 0);\n    //default_style.WindowBorderSize = 0.0f;\n    //default_style.ItemSpacing.y = 3.0f;\n    //default_style.FramePadding = ImVec2(0, 0);\n    default_style.ScaleAllSizes(viewport->DpiScale);\n    ImGuiStyle& style = ImGui::GetStyle();\n    style = default_style;\n#endif\n}\n\nstatic LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)\n{\n    if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))\n        return true;\n\n    if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd))\n    {\n        switch (msg)\n        {\n        case WM_CLOSE:\n            viewport->PlatformRequestClose = true;\n            return 0;\n        case WM_MOVE:\n            viewport->PlatformRequestMove = true;\n            break;\n        case WM_SIZE:\n            viewport->PlatformRequestResize = true;\n            break;\n        case WM_MOUSEACTIVATE:\n            if (viewport->Flags & ImGuiViewportFlags_NoFocusOnClick)\n                return MA_NOACTIVATE;\n            break;\n        case WM_NCHITTEST:\n            // Let mouse pass-through the window. This will allow the back-end to set io.MouseHoveredViewport properly (which is OPTIONAL).\n            // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging.\n            // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in\n            // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system.\n            if (viewport->Flags & ImGuiViewportFlags_NoInputs)\n                return HTTRANSPARENT;\n            break;\n        }\n    }\n\n    return DefWindowProc(hWnd, msg, wParam, lParam);\n}\n\nstatic BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, HDC, LPRECT, LPARAM)\n{\n    MONITORINFO info = { 0 };\n    info.cbSize = sizeof(MONITORINFO);\n    if (!::GetMonitorInfo(monitor, &info))\n        return TRUE;\n    ImGuiPlatformMonitor imgui_monitor;\n    imgui_monitor.MainPos = ImVec2((float)info.rcMonitor.left, (float)info.rcMonitor.top);\n    imgui_monitor.MainSize = ImVec2((float)(info.rcMonitor.right - info.rcMonitor.left), (float)(info.rcMonitor.bottom - info.rcMonitor.top));\n    imgui_monitor.WorkPos = ImVec2((float)info.rcWork.left, (float)info.rcWork.top);\n    imgui_monitor.WorkSize = ImVec2((float)(info.rcWork.right - info.rcWork.left), (float)(info.rcWork.bottom - info.rcWork.top));\n    imgui_monitor.DpiScale = ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);\n    ImGuiPlatformIO& io = ImGui::GetPlatformIO();\n    if (info.dwFlags & MONITORINFOF_PRIMARY)\n        io.Monitors.push_front(imgui_monitor);\n    else\n        io.Monitors.push_back(imgui_monitor);\n    return TRUE;\n}\n\nstatic void ImGui_ImplWin32_UpdateMonitors()\n{\n    ImGui::GetPlatformIO().Monitors.resize(0);\n    ::EnumDisplayMonitors(NULL, NULL, ImGui_ImplWin32_UpdateMonitors_EnumFunc, NULL);\n    g_WantUpdateMonitors = false;\n}\n\nstatic void ImGui_ImplWin32_InitPlatformInterface()\n{\n    WNDCLASSEX wcex;\n    wcex.cbSize = sizeof(WNDCLASSEX);\n    wcex.style = CS_HREDRAW | CS_VREDRAW;\n    wcex.lpfnWndProc = ImGui_ImplWin32_WndProcHandler_PlatformWindow;\n    wcex.cbClsExtra = 0;\n    wcex.cbWndExtra = 0;\n    wcex.hInstance = ::GetModuleHandle(NULL);\n    wcex.hIcon = NULL;\n    wcex.hCursor = NULL;\n    wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);\n    wcex.lpszMenuName = NULL;\n    wcex.lpszClassName = _T(\"ImGui Platform\");\n    wcex.hIconSm = NULL;\n    ::RegisterClassEx(&wcex);\n\n    ImGui_ImplWin32_UpdateMonitors();\n\n    // Register platform interface (will be coupled with a renderer interface)\n    ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();\n    platform_io.Platform_CreateWindow = ImGui_ImplWin32_CreateWindow;\n    platform_io.Platform_DestroyWindow = ImGui_ImplWin32_DestroyWindow;\n    platform_io.Platform_ShowWindow = ImGui_ImplWin32_ShowWindow;\n    platform_io.Platform_SetWindowPos = ImGui_ImplWin32_SetWindowPos;\n    platform_io.Platform_GetWindowPos = ImGui_ImplWin32_GetWindowPos;\n    platform_io.Platform_SetWindowSize = ImGui_ImplWin32_SetWindowSize;\n    platform_io.Platform_GetWindowSize = ImGui_ImplWin32_GetWindowSize;\n    platform_io.Platform_SetWindowFocus = ImGui_ImplWin32_SetWindowFocus;\n    platform_io.Platform_GetWindowFocus = ImGui_ImplWin32_GetWindowFocus;\n    platform_io.Platform_GetWindowMinimized = ImGui_ImplWin32_GetWindowMinimized;\n    platform_io.Platform_SetWindowTitle = ImGui_ImplWin32_SetWindowTitle;\n    platform_io.Platform_SetWindowAlpha = ImGui_ImplWin32_SetWindowAlpha;\n    platform_io.Platform_UpdateWindow = ImGui_ImplWin32_UpdateWindow;\n    platform_io.Platform_GetWindowDpiScale = ImGui_ImplWin32_GetWindowDpiScale; // FIXME-DPI\n    platform_io.Platform_OnChangedViewport = ImGui_ImplWin32_OnChangedViewport; // FIXME-DPI\n#if HAS_WIN32_IME\n    platform_io.Platform_SetImeInputPos = ImGui_ImplWin32_SetImeInputPos;\n#endif\n\n    // Register main window handle (which is owned by the main application, not by us)\n    ImGuiViewport* main_viewport = ImGui::GetMainViewport();\n    ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)();\n    data->Hwnd = g_hWnd;\n    data->HwndOwned = false;\n    main_viewport->PlatformUserData = data;\n    main_viewport->PlatformHandle = (void*)g_hWnd;\n}\n\nstatic void ImGui_ImplWin32_ShutdownPlatformInterface()\n{\n    ::UnregisterClass(_T(\"ImGui Platform\"), ::GetModuleHandle(NULL));\n}\n"
  },
  {
    "path": "src/imgui/imgui_impl_win32.hpp",
    "content": "// dear imgui: Platform Binding for Windows (standard windows API for 32 and 64 bits applications)\n// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)\n\n// Implemented features:\n//  [X] Platform: Clipboard support (for Win32 this is actually part of core imgui)\n//  [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.\n//  [X] Platform: Keyboard arrays indexed using VK_* Virtual Key Codes, e.g. ImGui::IsKeyPressed(VK_SPACE).\n//  [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.\n//  [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.\n\n#pragma once\n\nIMGUI_IMPL_API bool     ImGui_ImplWin32_Init(void* hwnd);\nIMGUI_IMPL_API void     ImGui_ImplWin32_Shutdown();\nIMGUI_IMPL_API void     ImGui_ImplWin32_NewFrame();\n\n// DPI-related helpers (which run and compile without requiring 8.1 or 10, neither Windows version, neither associated SDK)\nIMGUI_IMPL_API void     ImGui_ImplWin32_EnableDpiAwareness();\nIMGUI_IMPL_API float    ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd);       // HWND hwnd\nIMGUI_IMPL_API float    ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor); // HMONITOR monitor\n\n// Handler for Win32 messages, update mouse/keyboard data.\n// You may or not need this for your implementation, but it can serve as reference for handling inputs.\n// Intentionally commented out to avoid dragging dependencies on <windows.h> types. You can COPY this line into your .cpp code instead.\n/*\nIMGUI_IMPL_API LRESULT  ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);\n*/\n"
  },
  {
    "path": "src/imgui/imgui_internal.hpp",
    "content": "// dear imgui, v1.70 WIP\n// (internal structures/api)\n\n// You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility!\n// Set:\n//   #define IMGUI_DEFINE_MATH_OPERATORS\n// To implement maths operators for ImVec2 (disabled by default to not collide with using IM_VEC2_CLASS_EXTRA along with your own math types+operators)\n\n/*\n\nIndex of this file:\n// Header mess\n// Forward declarations\n// STB libraries includes\n// Context pointer\n// Generic helpers\n// Misc data structures\n// Main imgui context\n// Tab bar, tab item\n// Internal API\n\n*/\n\n#pragma once\n\n//-----------------------------------------------------------------------------\n// Header mess\n//-----------------------------------------------------------------------------\n\n#ifndef IMGUI_VERSION\n#error Must include imgui.h before imgui_internal.h\n#endif\n\n#include <stdio.h>      // FILE*\n#include <stdlib.h>     // NULL, malloc, free, qsort, atoi, atof\n#include <math.h>       // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf\n#include <limits.h>     // INT_MIN, INT_MAX\n\n#ifdef _MSC_VER\n#pragma warning (push)\n#pragma warning (disable: 4251) // class 'xxx' needs to have dll-interface to be used by clients of struct 'xxx' // when IMGUI_API is set to__declspec(dllexport)\n#endif\n\n#ifdef __clang__\n#pragma clang diagnostic push\n#pragma clang diagnostic ignored \"-Wunused-function\"                // for stb_textedit.h\n#pragma clang diagnostic ignored \"-Wmissing-prototypes\"             // for stb_textedit.h\n#pragma clang diagnostic ignored \"-Wold-style-cast\"\n#if __has_warning(\"-Wzero-as-null-pointer-constant\")\n#pragma clang diagnostic ignored \"-Wzero-as-null-pointer-constant\"\n#endif\n#if __has_warning(\"-Wdouble-promotion\")\n#pragma clang diagnostic ignored \"-Wdouble-promotion\"\n#endif\n#endif\n\n//-----------------------------------------------------------------------------\n// Forward declarations\n//-----------------------------------------------------------------------------\n\nstruct ImRect;                      // An axis-aligned rectangle (2 points)\nstruct ImDrawDataBuilder;           // Helper to build a ImDrawData instance\nstruct ImDrawListSharedData;        // Data shared between all ImDrawList instances\nstruct ImGuiColorMod;               // Stacked color modifier, backup of modified data so we can restore it\nstruct ImGuiColumnData;             // Storage data for a single column\nstruct ImGuiColumns;                // Storage data for a columns set\nstruct ImGuiContext;                // Main imgui context\nstruct ImGuiDataTypeInfo;           // Type information associated to a ImGuiDataType enum\nstruct ImGuiDockContext;            // Docking system context\nstruct ImGuiDockNode;               // Docking system node (hold a list of Windows OR two child dock nodes)\nstruct ImGuiDockNodeSettings;       // Storage for a dock node in .ini file (we preserve those even if the associated dock node isn't active during the session)\nstruct ImGuiGroupData;              // Stacked storage data for BeginGroup()/EndGroup()\nstruct ImGuiInputTextState;         // Internal state of the currently focused/edited text input box\nstruct ImGuiItemHoveredDataBackup;  // Backup and restore IsItemHovered() internal data\nstruct ImGuiMenuColumns;            // Simple column measurement, currently used for MenuItem() only\nstruct ImGuiNavMoveResult;          // Result of a directional navigation move query result\nstruct ImGuiNextWindowData;         // Storage for SetNexWindow** functions\nstruct ImGuiPopupData;              // Storage for current popup stack\nstruct ImGuiSettingsHandler;        // Storage for one type registered in the .ini file\nstruct ImGuiStyleMod;               // Stacked style modifier, backup of modified data so we can restore it\nstruct ImGuiTabBar;                 // Storage for a tab bar\nstruct ImGuiTabItem;                // Storage for a tab item (within a tab bar)\nstruct ImGuiWindow;                 // Storage for one window\nstruct ImGuiWindowTempData;         // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame)\nstruct ImGuiWindowSettings;         // Storage for window settings stored in .ini file (we keep one of those even if the actual window wasn't instanced during this session)\n\n// Use your programming IDE \"Go to definition\" facility on the names of the center columns to find the actual flags/enum lists.\ntypedef int ImGuiDataAuthority;     // -> enum ImGuiDataAuthority_     // Enum: for storing the source authority (dock node vs window) of a field\ntypedef int ImGuiLayoutType;        // -> enum ImGuiLayoutType_        // Enum: Horizontal or vertical\ntypedef int ImGuiButtonFlags;       // -> enum ImGuiButtonFlags_       // Flags: for ButtonEx(), ButtonBehavior()\ntypedef int ImGuiDragFlags;         // -> enum ImGuiDragFlags_         // Flags: for DragBehavior()\ntypedef int ImGuiItemFlags;         // -> enum ImGuiItemFlags_         // Flags: for PushItemFlag()\ntypedef int ImGuiItemStatusFlags;   // -> enum ImGuiItemStatusFlags_   // Flags: for DC.LastItemStatusFlags\ntypedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight()\ntypedef int ImGuiNavDirSourceFlags; // -> enum ImGuiNavDirSourceFlags_ // Flags: for GetNavInputAmount2d()\ntypedef int ImGuiNavMoveFlags;      // -> enum ImGuiNavMoveFlags_      // Flags: for navigation requests\ntypedef int ImGuiSeparatorFlags;    // -> enum ImGuiSeparatorFlags_    // Flags: for Separator() - internal\ntypedef int ImGuiSliderFlags;       // -> enum ImGuiSliderFlags_       // Flags: for SliderBehavior()\ntypedef int ImGuiTextFlags;         // -> enum ImGuiTextFlags_         // Flags: for TextEx()\n\n//-------------------------------------------------------------------------\n// STB libraries includes\n//-------------------------------------------------------------------------\n\nnamespace ImStb\n{\n\n#undef STB_TEXTEDIT_STRING\n#undef STB_TEXTEDIT_CHARTYPE\n#define STB_TEXTEDIT_STRING             ImGuiInputTextState\n#define STB_TEXTEDIT_CHARTYPE           ImWchar\n#define STB_TEXTEDIT_GETWIDTH_NEWLINE   -1.0f\n#define STB_TEXTEDIT_UNDOSTATECOUNT     99\n#define STB_TEXTEDIT_UNDOCHARCOUNT      999\n#include \"imstb_textedit.hpp\"\n\n} // namespace ImStb\n\n//-----------------------------------------------------------------------------\n// Context pointer\n//-----------------------------------------------------------------------------\n\n#ifndef GImGui\nextern IMGUI_API ImGuiContext* GImGui;  // Current implicit ImGui context pointer\n#endif\n\n// Internal Drag and Drop payload types. String starting with '_' are reserved for Dear ImGui.\n#define IMGUI_PAYLOAD_TYPE_WINDOW       \"_IMWINDOW\"     // Payload == ImGuiWindow*\n\n//-----------------------------------------------------------------------------\n// Generic helpers\n//-----------------------------------------------------------------------------\n\n#define IM_PI           3.14159265358979323846f\n#ifdef _WIN32\n#define IM_NEWLINE      \"\\r\\n\"   // Play it nice with Windows users (2018/05 news: Microsoft announced that Notepad will finally display Unix-style carriage returns!)\n#else\n#define IM_NEWLINE      \"\\n\"\n#endif\n#define IM_TABSIZE      (4)\n\n#define IMGUI_DEBUG_LOG(_FMT,...)       printf(\"[%05d] \" _FMT, GImGui->FrameCount, __VA_ARGS__)\n#define IM_STATIC_ASSERT(_COND)         typedef char static_assertion_##__line__[(_COND)?1:-1]\n#define IM_F32_TO_INT8_UNBOUND(_VAL)    ((int)((_VAL) * 255.0f + ((_VAL)>=0 ? 0.5f : -0.5f)))   // Unsaturated, for display purpose\n#define IM_F32_TO_INT8_SAT(_VAL)        ((int)(ImSaturate(_VAL) * 255.0f + 0.5f))               // Saturated, always output 0..255\n\n// Enforce cdecl calling convention for functions called by the standard library, in case compilation settings changed the default to e.g. __vectorcall\n#ifdef _MSC_VER\n#define IMGUI_CDECL __cdecl\n#else\n#define IMGUI_CDECL\n#endif\n\n// Helpers: UTF-8 <> wchar\nIMGUI_API int           ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end);      // return output UTF-8 bytes count\nIMGUI_API int           ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end);          // read one character. return input UTF-8 bytes count\nIMGUI_API int           ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL);   // return input UTF-8 bytes count\nIMGUI_API int           ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end);                            // return number of UTF-8 code-points (NOT bytes count)\nIMGUI_API int           ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end);                        // return number of bytes to express one char in UTF-8\nIMGUI_API int           ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end);                   // return number of bytes to express string in UTF-8\n\n// Helpers: Misc\nIMGUI_API ImU32         ImHashData(const void* data, size_t data_size, ImU32 seed = 0);\nIMGUI_API ImU32         ImHashStr(const char* data, size_t data_size = 0, ImU32 seed = 0);\nIMGUI_API void*         ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size = NULL, int padding_bytes = 0);\nIMGUI_API FILE*         ImFileOpen(const char* filename, const char* file_open_mode);\nstatic inline bool      ImCharIsBlankA(char c)          { return c == ' ' || c == '\\t'; }\nstatic inline bool      ImCharIsBlankW(unsigned int c)  { return c == ' ' || c == '\\t' || c == 0x3000; }\nstatic inline bool      ImIsPowerOfTwo(int v)           { return v != 0 && (v & (v - 1)) == 0; }\nstatic inline int       ImUpperPowerOfTwo(int v)        { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }\n#define ImQsort         qsort\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\nstatic inline ImU32     ImHash(const void* data, int size, ImU32 seed = 0) { return size ? ImHashData(data, (size_t)size, seed) : ImHashStr((const char*)data, 0, seed); } // [moved to ImHashStr/ImHashData in 1.68]\n#endif\n\n// Helpers: Geometry\nIMGUI_API ImVec2        ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p);\nIMGUI_API bool          ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p);\nIMGUI_API ImVec2        ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p);\nIMGUI_API void          ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w);\nIMGUI_API ImGuiDir      ImGetDirQuadrantFromDelta(float dx, float dy);\n\n// Helpers: String\nIMGUI_API int           ImStricmp(const char* str1, const char* str2);\nIMGUI_API int           ImStrnicmp(const char* str1, const char* str2, size_t count);\nIMGUI_API void          ImStrncpy(char* dst, const char* src, size_t count);\nIMGUI_API char*         ImStrdup(const char* str);\nIMGUI_API char*         ImStrdupcpy(char* dst, size_t* p_dst_size, const char* str);\nIMGUI_API const char*   ImStrchrRange(const char* str_begin, const char* str_end, char c);\nIMGUI_API int           ImStrlenW(const ImWchar* str);\nIMGUI_API const char*   ImStreolRange(const char* str, const char* str_end);                // End end-of-line\nIMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin);   // Find beginning-of-line\nIMGUI_API const char*   ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end);\nIMGUI_API void          ImStrTrimBlanks(char* str);\nIMGUI_API const char*   ImStrSkipBlank(const char* str);\nIMGUI_API int           ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) IM_FMTARGS(3);\nIMGUI_API int           ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) IM_FMTLIST(3);\nIMGUI_API const char*   ImParseFormatFindStart(const char* format);\nIMGUI_API const char*   ImParseFormatFindEnd(const char* format);\nIMGUI_API const char*   ImParseFormatTrimDecorations(const char* format, char* buf, size_t buf_size);\nIMGUI_API int           ImParseFormatPrecision(const char* format, int default_value);\n\n// Helpers: ImVec2/ImVec4 operators\n// We are keeping those disabled by default so they don't leak in user space, to allow user enabling implicit cast operators between ImVec2 and their own types (using IM_VEC2_CLASS_EXTRA etc.)\n// We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself.\n#ifdef IMGUI_DEFINE_MATH_OPERATORS\nstatic inline ImVec2 operator*(const ImVec2& lhs, const float rhs)              { return ImVec2(lhs.x*rhs, lhs.y*rhs); }\nstatic inline ImVec2 operator/(const ImVec2& lhs, const float rhs)              { return ImVec2(lhs.x/rhs, lhs.y/rhs); }\nstatic inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs)            { return ImVec2(lhs.x+rhs.x, lhs.y+rhs.y); }\nstatic inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs)            { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); }\nstatic inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs)            { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); }\nstatic inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs)            { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); }\nstatic inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs)                { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; }\nstatic inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs)                { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; }\nstatic inline ImVec2& operator*=(ImVec2& lhs, const float rhs)                  { lhs.x *= rhs; lhs.y *= rhs; return lhs; }\nstatic inline ImVec2& operator/=(ImVec2& lhs, const float rhs)                  { lhs.x /= rhs; lhs.y /= rhs; return lhs; }\nstatic inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs)            { return ImVec4(lhs.x+rhs.x, lhs.y+rhs.y, lhs.z+rhs.z, lhs.w+rhs.w); }\nstatic inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs)            { return ImVec4(lhs.x-rhs.x, lhs.y-rhs.y, lhs.z-rhs.z, lhs.w-rhs.w); }\nstatic inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs)            { return ImVec4(lhs.x*rhs.x, lhs.y*rhs.y, lhs.z*rhs.z, lhs.w*rhs.w); }\n#endif\n\n// Helpers: Maths\n// - Wrapper for standard libs functions. (Note that imgui_demo.cpp does _not_ use them to keep the code easy to copy)\n#ifndef IMGUI_DISABLE_MATH_FUNCTIONS\nstatic inline float  ImFabs(float x)                                            { return fabsf(x); }\nstatic inline float  ImSqrt(float x)                                            { return sqrtf(x); }\nstatic inline float  ImPow(float x, float y)                                    { return powf(x, y); }\nstatic inline double ImPow(double x, double y)                                  { return pow(x, y); }\nstatic inline float  ImFmod(float x, float y)                                   { return fmodf(x, y); }\nstatic inline double ImFmod(double x, double y)                                 { return fmod(x, y); }\nstatic inline float  ImCos(float x)                                             { return cosf(x); }\nstatic inline float  ImSin(float x)                                             { return sinf(x); }\nstatic inline float  ImAcos(float x)                                            { return acosf(x); }\nstatic inline float  ImAtan2(float y, float x)                                  { return atan2f(y, x); }\nstatic inline double ImAtof(const char* s)                                      { return atof(s); }\nstatic inline float  ImFloorStd(float x)                                        { return floorf(x); }   // we already uses our own ImFloor() { return (float)(int)v } internally so the standard one wrapper is named differently (it's used by stb_truetype)\nstatic inline float  ImCeil(float x)                                            { return ceilf(x); }\n#endif\n// - ImMin/ImMax/ImClamp/ImLerp/ImSwap are used by widgets which support for variety of types: signed/unsigned int/long long float/double\n// (Exceptionally using templates here but we could also redefine them for variety of types)\ntemplate<typename T> static inline T ImMin(T lhs, T rhs)                        { return lhs < rhs ? lhs : rhs; }\ntemplate<typename T> static inline T ImMax(T lhs, T rhs)                        { return lhs >= rhs ? lhs : rhs; }\ntemplate<typename T> static inline T ImClamp(T v, T mn, T mx)                   { return (v < mn) ? mn : (v > mx) ? mx : v; }\ntemplate<typename T> static inline T ImLerp(T a, T b, float t)                  { return (T)(a + (b - a) * t); }\ntemplate<typename T> static inline void ImSwap(T& a, T& b)                      { T tmp = a; a = b; b = tmp; }\ntemplate<typename T> static inline T ImAddClampOverflow(T a, T b, T mn, T mx)   { if (b < 0 && (a < mn - b)) return mn; if (b > 0 && (a > mx - b)) return mx; return a + b; }\ntemplate<typename T> static inline T ImSubClampOverflow(T a, T b, T mn, T mx)   { if (b > 0 && (a < mn + b)) return mn; if (b < 0 && (a > mx + b)) return mx; return a - b; }\n// - Misc maths helpers\nstatic inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs)                { return ImVec2(lhs.x < rhs.x ? lhs.x : rhs.x, lhs.y < rhs.y ? lhs.y : rhs.y); }\nstatic inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs)                { return ImVec2(lhs.x >= rhs.x ? lhs.x : rhs.x, lhs.y >= rhs.y ? lhs.y : rhs.y); }\nstatic inline ImVec2 ImClamp(const ImVec2& v, const ImVec2& mn, ImVec2 mx)      { return ImVec2((v.x < mn.x) ? mn.x : (v.x > mx.x) ? mx.x : v.x, (v.y < mn.y) ? mn.y : (v.y > mx.y) ? mx.y : v.y); }\nstatic inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t)          { return ImVec2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t); }\nstatic inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t)  { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); }\nstatic inline ImVec4 ImLerp(const ImVec4& a, const ImVec4& b, float t)          { return ImVec4(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t); }\nstatic inline float  ImSaturate(float f)                                        { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; }\nstatic inline float  ImLengthSqr(const ImVec2& lhs)                             { return lhs.x*lhs.x + lhs.y*lhs.y; }\nstatic inline float  ImLengthSqr(const ImVec4& lhs)                             { return lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z + lhs.w*lhs.w; }\nstatic inline float  ImInvLength(const ImVec2& lhs, float fail_value)           { float d = lhs.x*lhs.x + lhs.y*lhs.y; if (d > 0.0f) return 1.0f / ImSqrt(d); return fail_value; }\nstatic inline float  ImFloor(float f)                                           { return (float)(int)f; }\nstatic inline ImVec2 ImFloor(const ImVec2& v)                                   { return ImVec2((float)(int)v.x, (float)(int)v.y); }\nstatic inline int    ImModPositive(int a, int b)                                { return (a + b) % b; }\nstatic inline float  ImDot(const ImVec2& a, const ImVec2& b)                    { return a.x * b.x + a.y * b.y; }\nstatic inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a)        { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); }\nstatic inline float  ImLinearSweep(float current, float target, float speed)    { if (current < target) return ImMin(current + speed, target); if (current > target) return ImMax(current - speed, target); return current; }\nstatic inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs)                { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); }\n\n// Helper: ImBoolVector. Store 1-bit per value.\n// Note that Resize() currently clears the whole vector.\nstruct ImBoolVector\n{\n    ImVector<int>   Storage;\n    ImBoolVector()  { }\n    void            Resize(int sz)          { Storage.resize((sz + 31) >> 5); memset(Storage.Data, 0, (size_t)Storage.Size * sizeof(Storage.Data[0])); }\n    void            Clear()                 { Storage.clear(); }\n    bool            GetBit(int n) const     { int off = (n >> 5); int mask = 1 << (n & 31); return (Storage[off] & mask) != 0; }\n    void            SetBit(int n, bool v)   { int off = (n >> 5); int mask = 1 << (n & 31); if (v) Storage[off] |= mask; else Storage[off] &= ~mask; }\n};\n\n// Helper: ImPool<>. Basic keyed storage for contiguous instances, slow/amortized insertion, O(1) indexable, O(Log N) queries by ID over a dense/hot buffer,\n// Honor constructor/destructor. Add/remove invalidate all pointers. Indexes have the same lifetime as the associated object.\ntypedef int ImPoolIdx;\ntemplate<typename T>\nstruct IMGUI_API ImPool\n{\n    ImVector<T>     Data;       // Contiguous data\n    ImGuiStorage    Map;        // ID->Index\n    ImPoolIdx       FreeIdx;    // Next free idx to use\n\n    ImPool()    { FreeIdx = 0; }\n    ~ImPool()   { Clear(); }\n    T*          GetByKey(ImGuiID key)               { int idx = Map.GetInt(key, -1); return (idx != -1) ? &Data[idx] : NULL; }\n    T*          GetByIndex(ImPoolIdx n)             { return &Data[n]; }\n    ImPoolIdx   GetIndex(const T* p) const          { IM_ASSERT(p >= Data.Data && p < Data.Data + Data.Size); return (ImPoolIdx)(p - Data.Data); }\n    T*          GetOrAddByKey(ImGuiID key)          { int* p_idx = Map.GetIntRef(key, -1); if (*p_idx != -1) return &Data[*p_idx]; *p_idx = FreeIdx; return Add(); }\n    bool        Contains(const T* p) const          { return (p >= Data.Data && p < Data.Data + Data.Size); }\n    void        Clear()                             { for (int n = 0; n < Map.Data.Size; n++) { int idx = Map.Data[n].val_i; if (idx != -1) Data[idx].~T(); } Map.Clear(); Data.clear(); FreeIdx = 0; }\n    T*          Add()                               { int idx = FreeIdx; if (idx == Data.Size) { Data.resize(Data.Size + 1); FreeIdx++; } else { FreeIdx = *(int*)&Data[idx]; } IM_PLACEMENT_NEW(&Data[idx]) T(); return &Data[idx]; }\n    void        Remove(ImGuiID key, const T* p)     { Remove(key, GetIndex(p)); }\n    void        Remove(ImGuiID key, ImPoolIdx idx)  { Data[idx].~T(); *(int*)&Data[idx] = FreeIdx; FreeIdx = idx; Map.SetInt(key, -1); }\n    void        Reserve(int capacity)               { Data.reserve(capacity); Map.Data.reserve(capacity); }\n    int         GetSize() const                     { return Data.Size; }\n};\n\n//-----------------------------------------------------------------------------\n// Misc data structures\n//-----------------------------------------------------------------------------\n\nenum ImGuiButtonFlags_\n{\n    ImGuiButtonFlags_None                   = 0,\n    ImGuiButtonFlags_Repeat                 = 1 << 0,   // hold to repeat\n    ImGuiButtonFlags_PressedOnClickRelease  = 1 << 1,   // [Default] return true on click + release on same item\n    ImGuiButtonFlags_PressedOnClick         = 1 << 2,   // return true on click (default requires click+release)\n    ImGuiButtonFlags_PressedOnRelease       = 1 << 3,   // return true on release (default requires click+release)\n    ImGuiButtonFlags_PressedOnDoubleClick   = 1 << 4,   // return true on double-click (default requires click+release)\n    ImGuiButtonFlags_FlattenChildren        = 1 << 5,   // allow interactions even if a child window is overlapping\n    ImGuiButtonFlags_AllowItemOverlap       = 1 << 6,   // require previous frame HoveredId to either match id or be null before being usable, use along with SetItemAllowOverlap()\n    ImGuiButtonFlags_DontClosePopups        = 1 << 7,   // disable automatically closing parent popup on press // [UNUSED]\n    ImGuiButtonFlags_Disabled               = 1 << 8,   // disable interactions\n    ImGuiButtonFlags_AlignTextBaseLine      = 1 << 9,   // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine\n    ImGuiButtonFlags_NoKeyModifiers         = 1 << 10,  // disable interaction if a key modifier is held\n    ImGuiButtonFlags_NoHoldingActiveID      = 1 << 11,  // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only)\n    ImGuiButtonFlags_PressedOnDragDropHold  = 1 << 12,  // press when held into while we are drag and dropping another item (used by e.g. tree nodes, collapsing headers)\n    ImGuiButtonFlags_NoNavFocus             = 1 << 13,  // don't override navigation focus when activated\n    ImGuiButtonFlags_NoHoveredOnNav         = 1 << 14   // don't report as hovered when navigated on\n};\n\nenum ImGuiSliderFlags_\n{\n    ImGuiSliderFlags_None                   = 0,\n    ImGuiSliderFlags_Vertical               = 1 << 0\n};\n\nenum ImGuiDragFlags_\n{\n    ImGuiDragFlags_None                     = 0,\n    ImGuiDragFlags_Vertical                 = 1 << 0\n};\n\nenum ImGuiColumnsFlags_\n{\n    // Default: 0\n    ImGuiColumnsFlags_None                  = 0,\n    ImGuiColumnsFlags_NoBorder              = 1 << 0,   // Disable column dividers\n    ImGuiColumnsFlags_NoResize              = 1 << 1,   // Disable resizing columns when clicking on the dividers\n    ImGuiColumnsFlags_NoPreserveWidths      = 1 << 2,   // Disable column width preservation when adjusting columns\n    ImGuiColumnsFlags_NoForceWithinWindow   = 1 << 3,   // Disable forcing columns to fit within window\n    ImGuiColumnsFlags_GrowParentContentsSize= 1 << 4    // (WIP) Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove.\n};\n\nenum ImGuiSelectableFlagsPrivate_\n{\n    // NB: need to be in sync with last value of ImGuiSelectableFlags_\n    ImGuiSelectableFlags_NoHoldingActiveID  = 1 << 10,\n    ImGuiSelectableFlags_PressedOnClick     = 1 << 11,\n    ImGuiSelectableFlags_PressedOnRelease   = 1 << 12,\n    ImGuiSelectableFlags_DrawFillAvailWidth = 1 << 13,\n    ImGuiSelectableFlags_AllowItemOverlap   = 1 << 14\n};\n\nenum ImGuiSeparatorFlags_\n{\n    ImGuiSeparatorFlags_None                = 0,\n    ImGuiSeparatorFlags_Horizontal          = 1 << 0,   // Axis default to current layout type, so generally Horizontal unless e.g. in a menu bar\n    ImGuiSeparatorFlags_Vertical            = 1 << 1\n};\n\n// Transient per-window flags, reset at the beginning of the frame. For child window, inherited from parent on first Begin().\n// This is going to be exposed in imgui.h when stabilized enough.\nenum ImGuiItemFlags_\n{\n    ImGuiItemFlags_NoTabStop                = 1 << 0,  // false\n    ImGuiItemFlags_ButtonRepeat             = 1 << 1,  // false    // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings.\n    ImGuiItemFlags_Disabled                 = 1 << 2,  // false    // [BETA] Disable interactions but doesn't affect visuals yet. See github.com/ocornut/imgui/issues/211\n    ImGuiItemFlags_NoNav                    = 1 << 3,  // false\n    ImGuiItemFlags_NoNavDefaultFocus        = 1 << 4,  // false\n    ImGuiItemFlags_SelectableDontClosePopup = 1 << 5,  // false    // MenuItem/Selectable() automatically closes current Popup window\n    ImGuiItemFlags_Default_                 = 0\n};\n\n// Storage for LastItem data\nenum ImGuiItemStatusFlags_\n{\n    ImGuiItemStatusFlags_None               = 0,\n    ImGuiItemStatusFlags_HoveredRect        = 1 << 0,\n    ImGuiItemStatusFlags_HasDisplayRect     = 1 << 1,\n    ImGuiItemStatusFlags_Edited             = 1 << 2,   // Value exposed by item was edited in the current frame (should match the bool return value of most widgets)\n    ImGuiItemStatusFlags_ToggledSelection   = 1 << 3    // Set when Selectable(), TreeNode() reports toggling a selection. We can't report \"Selected\" because reporting the change allows us to handle clipping with less issues.\n\n#ifdef IMGUI_ENABLE_TEST_ENGINE\n    , // [imgui-test only]\n    ImGuiItemStatusFlags_Openable           = 1 << 10,  //\n    ImGuiItemStatusFlags_Opened             = 1 << 11,  //\n    ImGuiItemStatusFlags_Checkable          = 1 << 12,  //\n    ImGuiItemStatusFlags_Checked            = 1 << 13   //\n#endif\n};\n\nenum ImGuiTextFlags_\n{\n    ImGuiTextFlags_None = 0,\n    ImGuiTextFlags_NoWidthForLargeClippedText = 1 << 0\n};\n\n// FIXME: this is in development, not exposed/functional as a generic feature yet.\n// Horizontal/Vertical enums are fixed to 0/1 so they may be used to index ImVec2\nenum ImGuiLayoutType_\n{\n    ImGuiLayoutType_Horizontal = 0,\n    ImGuiLayoutType_Vertical = 1\n};\n\nenum ImGuiLogType\n{\n    ImGuiLogType_None = 0,\n    ImGuiLogType_TTY,\n    ImGuiLogType_File,\n    ImGuiLogType_Buffer,\n    ImGuiLogType_Clipboard\n};\n\n// X/Y enums are fixed to 0/1 so they may be used to index ImVec2\nenum ImGuiAxis\n{\n    ImGuiAxis_None = -1,\n    ImGuiAxis_X = 0,\n    ImGuiAxis_Y = 1\n};\n\nenum ImGuiPlotType\n{\n    ImGuiPlotType_Lines,\n    ImGuiPlotType_Histogram\n};\n\nenum ImGuiInputSource\n{\n    ImGuiInputSource_None = 0,\n    ImGuiInputSource_Mouse,\n    ImGuiInputSource_Nav,\n    ImGuiInputSource_NavKeyboard,   // Only used occasionally for storage, not tested/handled by most code\n    ImGuiInputSource_NavGamepad,    // \"\n    ImGuiInputSource_COUNT\n};\n\n// FIXME-NAV: Clarify/expose various repeat delay/rate\nenum ImGuiInputReadMode\n{\n    ImGuiInputReadMode_Down,\n    ImGuiInputReadMode_Pressed,\n    ImGuiInputReadMode_Released,\n    ImGuiInputReadMode_Repeat,\n    ImGuiInputReadMode_RepeatSlow,\n    ImGuiInputReadMode_RepeatFast\n};\n\nenum ImGuiNavHighlightFlags_\n{\n    ImGuiNavHighlightFlags_None         = 0,\n    ImGuiNavHighlightFlags_TypeDefault  = 1 << 0,\n    ImGuiNavHighlightFlags_TypeThin     = 1 << 1,\n    ImGuiNavHighlightFlags_AlwaysDraw   = 1 << 2,       // Draw rectangular highlight if (g.NavId == id) _even_ when using the mouse.\n    ImGuiNavHighlightFlags_NoRounding   = 1 << 3\n};\n\nenum ImGuiNavDirSourceFlags_\n{\n    ImGuiNavDirSourceFlags_None         = 0,\n    ImGuiNavDirSourceFlags_Keyboard     = 1 << 0,\n    ImGuiNavDirSourceFlags_PadDPad      = 1 << 1,\n    ImGuiNavDirSourceFlags_PadLStick    = 1 << 2\n};\n\nenum ImGuiNavMoveFlags_\n{\n    ImGuiNavMoveFlags_None                  = 0,\n    ImGuiNavMoveFlags_LoopX                 = 1 << 0,   // On failed request, restart from opposite side\n    ImGuiNavMoveFlags_LoopY                 = 1 << 1,\n    ImGuiNavMoveFlags_WrapX                 = 1 << 2,   // On failed request, request from opposite side one line down (when NavDir==right) or one line up (when NavDir==left)\n    ImGuiNavMoveFlags_WrapY                 = 1 << 3,   // This is not super useful for provided for completeness\n    ImGuiNavMoveFlags_AllowCurrentNavId     = 1 << 4,   // Allow scoring and considering the current NavId as a move target candidate. This is used when the move source is offset (e.g. pressing PageDown actually needs to send a Up move request, if we are pressing PageDown from the bottom-most item we need to stay in place)\n    ImGuiNavMoveFlags_AlsoScoreVisibleSet   = 1 << 5    // Store alternate result in NavMoveResultLocalVisibleSet that only comprise elements that are already fully visible.\n};\n\nenum ImGuiNavForward\n{\n    ImGuiNavForward_None,\n    ImGuiNavForward_ForwardQueued,\n    ImGuiNavForward_ForwardActive\n};\n\nenum ImGuiNavLayer\n{\n    ImGuiNavLayer_Main  = 0,    // Main scrolling layer\n    ImGuiNavLayer_Menu  = 1,    // Menu layer (access with Alt/ImGuiNavInput_Menu)\n    ImGuiNavLayer_COUNT\n};\n\nenum ImGuiPopupPositionPolicy\n{\n    ImGuiPopupPositionPolicy_Default,\n    ImGuiPopupPositionPolicy_ComboBox\n};\n\n// 1D vector (this odd construct is used to facilitate the transition between 1D and 2D, and the maintenance of some branches/patches)\nstruct ImVec1\n{\n    float   x;\n    ImVec1()         { x = 0.0f; }\n    ImVec1(float _x) { x = _x; }\n};\n\n// 2D vector (half-size integer)\nstruct ImVec2ih\n{\n    short   x, y;\n    ImVec2ih()                   { x = y = 0; }\n    ImVec2ih(short _x, short _y) { x = _x; y = _y; }\n};\n\n// 2D axis aligned bounding-box\n// NB: we can't rely on ImVec2 math operators being available here\nstruct IMGUI_API ImRect\n{\n    ImVec2      Min;    // Upper-left\n    ImVec2      Max;    // Lower-right\n\n    ImRect()                                        : Min(FLT_MAX,FLT_MAX), Max(-FLT_MAX,-FLT_MAX)  {}\n    ImRect(const ImVec2& min, const ImVec2& max)    : Min(min), Max(max)                            {}\n    ImRect(const ImVec4& v)                         : Min(v.x, v.y), Max(v.z, v.w)                  {}\n    ImRect(float x1, float y1, float x2, float y2)  : Min(x1, y1), Max(x2, y2)                      {}\n\n    ImVec2      GetCenter() const                   { return ImVec2((Min.x + Max.x) * 0.5f, (Min.y + Max.y) * 0.5f); }\n    ImVec2      GetSize() const                     { return ImVec2(Max.x - Min.x, Max.y - Min.y); }\n    float       GetWidth() const                    { return Max.x - Min.x; }\n    float       GetHeight() const                   { return Max.y - Min.y; }\n    ImVec2      GetTL() const                       { return Min; }                   // Top-left\n    ImVec2      GetTR() const                       { return ImVec2(Max.x, Min.y); }  // Top-right\n    ImVec2      GetBL() const                       { return ImVec2(Min.x, Max.y); }  // Bottom-left\n    ImVec2      GetBR() const                       { return Max; }                   // Bottom-right\n    bool        Contains(const ImVec2& p) const     { return p.x     >= Min.x && p.y     >= Min.y && p.x     <  Max.x && p.y     <  Max.y; }\n    bool        Contains(const ImRect& r) const     { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x <= Max.x && r.Max.y <= Max.y; }\n    bool        Overlaps(const ImRect& r) const     { return r.Min.y <  Max.y && r.Max.y >  Min.y && r.Min.x <  Max.x && r.Max.x >  Min.x; }\n    void        Add(const ImVec2& p)                { if (Min.x > p.x)     Min.x = p.x;     if (Min.y > p.y)     Min.y = p.y;     if (Max.x < p.x)     Max.x = p.x;     if (Max.y < p.y)     Max.y = p.y; }\n    void        Add(const ImRect& r)                { if (Min.x > r.Min.x) Min.x = r.Min.x; if (Min.y > r.Min.y) Min.y = r.Min.y; if (Max.x < r.Max.x) Max.x = r.Max.x; if (Max.y < r.Max.y) Max.y = r.Max.y; }\n    void        Expand(const float amount)          { Min.x -= amount;   Min.y -= amount;   Max.x += amount;   Max.y += amount; }\n    void        Expand(const ImVec2& amount)        { Min.x -= amount.x; Min.y -= amount.y; Max.x += amount.x; Max.y += amount.y; }\n    void        Translate(const ImVec2& d)          { Min.x += d.x; Min.y += d.y; Max.x += d.x; Max.y += d.y; }\n    void        TranslateX(float dx)                { Min.x += dx; Max.x += dx; }\n    void        TranslateY(float dy)                { Min.y += dy; Max.y += dy; }\n    void        ClipWith(const ImRect& r)           { Min = ImMax(Min, r.Min); Max = ImMin(Max, r.Max); }                   // Simple version, may lead to an inverted rectangle, which is fine for Contains/Overlaps test but not for display.\n    void        ClipWithFull(const ImRect& r)       { Min = ImClamp(Min, r.Min, r.Max); Max = ImClamp(Max, r.Min, r.Max); } // Full version, ensure both points are fully clipped.\n    void        Floor()                             { Min.x = (float)(int)Min.x; Min.y = (float)(int)Min.y; Max.x = (float)(int)Max.x; Max.y = (float)(int)Max.y; }\n    bool        IsInverted() const                  { return Min.x > Max.x || Min.y > Max.y; }\n};\n\n// Type information associated to one ImGuiDataType. Retrieve with DataTypeGetInfo().\nstruct ImGuiDataTypeInfo\n{\n    size_t      Size;           // Size in byte\n    const char* PrintFmt;       // Default printf format for the type\n    const char* ScanFmt;        // Default scanf format for the type\n};\n\n// Stacked color modifier, backup of modified data so we can restore it\nstruct ImGuiColorMod\n{\n    ImGuiCol    Col;\n    ImVec4      BackupValue;\n};\n\n// Stacked style modifier, backup of modified data so we can restore it. Data type inferred from the variable.\nstruct ImGuiStyleMod\n{\n    ImGuiStyleVar   VarIdx;\n    union           { int BackupInt[2]; float BackupFloat[2]; };\n    ImGuiStyleMod(ImGuiStyleVar idx, int v)     { VarIdx = idx; BackupInt[0] = v; }\n    ImGuiStyleMod(ImGuiStyleVar idx, float v)   { VarIdx = idx; BackupFloat[0] = v; }\n    ImGuiStyleMod(ImGuiStyleVar idx, ImVec2 v)  { VarIdx = idx; BackupFloat[0] = v.x; BackupFloat[1] = v.y; }\n};\n\n// Stacked storage data for BeginGroup()/EndGroup()\nstruct ImGuiGroupData\n{\n    ImVec2      BackupCursorPos;\n    ImVec2      BackupCursorMaxPos;\n    ImVec1      BackupIndent;\n    ImVec1      BackupGroupOffset;\n    ImVec2      BackupCurrentLineSize;\n    float       BackupCurrentLineTextBaseOffset;\n    ImGuiID     BackupActiveIdIsAlive;\n    bool        BackupActiveIdPreviousFrameIsAlive;\n    bool        AdvanceCursor;\n};\n\n// Simple column measurement, currently used for MenuItem() only.. This is very short-sighted/throw-away code and NOT a generic helper.\nstruct IMGUI_API ImGuiMenuColumns\n{\n    float       Spacing;\n    float       Width, NextWidth;\n    float       Pos[3], NextWidths[3];\n\n    ImGuiMenuColumns();\n    void        Update(int count, float spacing, bool clear);\n    float       DeclColumns(float w0, float w1, float w2);\n    float       CalcExtraSpace(float avail_w);\n};\n\n// Internal state of the currently focused/edited text input box\nstruct IMGUI_API ImGuiInputTextState\n{\n    ImGuiID                 ID;                     // widget id owning the text state\n    int                     CurLenW, CurLenA;       // we need to maintain our buffer length in both UTF-8 and wchar format. UTF-8 len is valid even if TextA is not.\n    ImVector<ImWchar>       TextW;                  // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer.\n    ImVector<char>          TextA;                  // temporary UTF8 buffer for callbacks and other operations. this is not updated in every code-path! size=capacity.\n    ImVector<char>          InitialTextA;           // backup of end-user buffer at the time of focus (in UTF-8, unaltered)\n    bool                    TextAIsValid;           // temporary UTF8 buffer is not initially valid before we make the widget active (until then we pull the data from user argument)\n    int                     BufCapacityA;           // end-user buffer capacity\n    float                   ScrollX;                // horizontal scrolling/offset\n    ImStb::STB_TexteditState Stb;                   // state for stb_textedit.h\n    float                   CursorAnim;             // timer for cursor blink, reset on every user action so the cursor reappears immediately\n    bool                    CursorFollow;           // set when we want scrolling to follow the current cursor position (not always!)\n    bool                    SelectedAllMouseLock;   // after a double-click to select all, we ignore further mouse drags to update selection\n\n    // Temporarily set when active\n    ImGuiInputTextFlags     UserFlags;\n    ImGuiInputTextCallback  UserCallback;\n    void*                   UserCallbackData;\n\n    ImGuiInputTextState()                           { memset(this, 0, sizeof(*this)); }\n    void                ClearFreeMemory()           { TextW.clear(); TextA.clear(); InitialTextA.clear(); }\n    void                CursorAnimReset()           { CursorAnim = -0.30f; }                                   // After a user-input the cursor stays on for a while without blinking\n    void                CursorClamp()               { Stb.cursor = ImMin(Stb.cursor, CurLenW); Stb.select_start = ImMin(Stb.select_start, CurLenW); Stb.select_end = ImMin(Stb.select_end, CurLenW); }\n    bool                HasSelection() const        { return Stb.select_start != Stb.select_end; }\n    void                ClearSelection()            { Stb.select_start = Stb.select_end = Stb.cursor; }\n    void                SelectAll()                 { Stb.select_start = 0; Stb.cursor = Stb.select_end = CurLenW; Stb.has_preferred_x = 0; }\n    int                 GetUndoAvailCount() const   { return Stb.undostate.undo_point; }\n    int                 GetRedoAvailCount() const   { return STB_TEXTEDIT_UNDOSTATECOUNT - Stb.undostate.redo_point; }\n    void                OnKeyPressed(int key);      // Cannot be inline because we call in code in stb_textedit.h implementation\n};\n\n// Windows data saved in imgui.ini file\nstruct ImGuiWindowSettings\n{\n    char*       Name;\n    ImGuiID     ID;\n    ImVec2      Pos;            // NB: Settings position are stored RELATIVE to the viewport! Whereas runtime ones are absolute positions.\n    ImVec2      Size;\n    ImVec2      ViewportPos;\n    ImGuiID     ViewportId;\n    ImGuiID     DockId;         // ID of last known DockNode (even if the DockNode is invisible because it has only 1 active window), or 0 if none. \n    ImGuiID     ClassId;        // ID of window class if specified\n    short       DockOrder;      // Order of the last time the window was visible within its DockNode. This is used to reorder windows that are reappearing on the same frame. Same value between windows that were active and windows that were none are possible.\n    bool        Collapsed;\n\n    ImGuiWindowSettings() { Name = NULL; ID = 0; Pos = Size = ViewportPos = ImVec2(0, 0); ViewportId = DockId = ClassId = 0; DockOrder = -1; Collapsed = false; }\n};\n\nstruct ImGuiSettingsHandler\n{\n    const char* TypeName;       // Short description stored in .ini file. Disallowed characters: '[' ']'\n    ImGuiID     TypeHash;       // == ImHashStr(TypeName)\n    void*       (*ReadOpenFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name);              // Read: Called when entering into a new ini entry e.g. \"[Window][Name]\"\n    void        (*ReadLineFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line); // Read: Called for every line of text within an ini entry\n    void        (*WriteAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out_buf);      // Write: Output every entries into 'out_buf'\n    void*       UserData;\n\n    ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); }\n};\n\n// Storage for current popup stack\nstruct ImGuiPopupData\n{\n    ImGuiID             PopupId;        // Set on OpenPopup()\n    ImGuiWindow*        Window;         // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup()\n    ImGuiWindow*        SourceWindow;   // Set on OpenPopup() copy of NavWindow at the time of opening the popup\n    int                 OpenFrameCount; // Set on OpenPopup()\n    ImGuiID             OpenParentId;   // Set on OpenPopup(), we need this to differentiate multiple menu sets from each others (e.g. inside menu bar vs loose menu items)\n    ImVec2              OpenPopupPos;   // Set on OpenPopup(), preferred popup position (typically == OpenMousePos when using mouse)\n    ImVec2              OpenMousePos;   // Set on OpenPopup(), copy of mouse position at the time of opening popup\n\n    ImGuiPopupData() { PopupId = 0; Window = SourceWindow = NULL; OpenFrameCount = -1; OpenParentId = 0; }\n};\n\nstruct ImGuiColumnData\n{\n    float               OffsetNorm;         // Column start offset, normalized 0.0 (far left) -> 1.0 (far right)\n    float               OffsetNormBeforeResize;\n    ImGuiColumnsFlags   Flags;              // Not exposed\n    ImRect              ClipRect;\n\n    ImGuiColumnData()   { OffsetNorm = OffsetNormBeforeResize = 0.0f; Flags = ImGuiColumnsFlags_None; }\n};\n\nstruct ImGuiColumns\n{\n    ImGuiID             ID;\n    ImGuiColumnsFlags   Flags;\n    bool                IsFirstFrame;\n    bool                IsBeingResized;\n    int                 Current;\n    int                 Count;\n    float               MinX, MaxX;\n    float               LineMinY, LineMaxY;\n    float               BackupCursorPosY;       // Backup of CursorPos at the time of BeginColumns()\n    float               BackupCursorMaxPosX;    // Backup of CursorMaxPos at the time of BeginColumns()\n    ImVector<ImGuiColumnData> Columns;\n\n    ImGuiColumns()      { Clear(); }\n    void Clear()\n    {\n        ID = 0;\n        Flags = ImGuiColumnsFlags_None;\n        IsFirstFrame = false;\n        IsBeingResized = false;\n        Current = 0;\n        Count = 1;\n        MinX = MaxX = 0.0f;\n        LineMinY = LineMaxY = 0.0f;\n        BackupCursorPosY = 0.0f;\n        BackupCursorMaxPosX = 0.0f;\n        Columns.clear();\n    }\n};\n\n// Data shared between all ImDrawList instances\nstruct IMGUI_API ImDrawListSharedData\n{\n    ImVec2          TexUvWhitePixel;            // UV of white pixel in the atlas\n    ImFont*         Font;                       // Current/default font (optional, for simplified AddText overload)\n    float           FontSize;                   // Current/default font size (optional, for simplified AddText overload)\n    float           CurveTessellationTol;\n    ImVec4          ClipRectFullscreen;         // Value for PushClipRectFullscreen()\n\n    // Const data\n    // FIXME: Bake rounded corners fill/borders in atlas\n    ImVec2          CircleVtx12[12];\n\n    ImDrawListSharedData();\n};\n\nstruct ImDrawDataBuilder\n{\n    ImVector<ImDrawList*>   Layers[2];           // Global layers for: regular, tooltip\n\n    void Clear()            { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].resize(0); }\n    void ClearFreeMemory()  { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].clear(); }\n    IMGUI_API void FlattenIntoSingleLayer();\n};\n\nenum ImGuiViewportFlagsPrivate_\n{\n    ImGuiViewportFlags_CanHostOtherWindows  = 1 << 10   // Normal viewports are associated to a single window. The main viewport can host multiple windows.\n};\n\n// ImGuiViewport Private/Internals fields (cardinal sin: we are using inheritance!)\n// Note that every instance of ImGuiViewport is in fact a ImGuiViewportP.\nstruct ImGuiViewportP : public ImGuiViewport\n{\n    int                 Idx;\n    int                 LastFrameActive;          // Last frame number this viewport was activated by a window\n    int                 LastFrameDrawLists[2];    // Last frame number the background (0) and foreground (1) draw lists were used\n    int                 LastFrontMostStampCount;  // Last stamp number from when a window hosted by this viewport was made front-most (by comparing this value between two viewport we have an implicit viewport z-order\n    ImGuiID             LastNameHash;\n    ImVec2              LastPos;\n    float               Alpha;                    // Window opacity (when dragging dockable windows/viewports we make them transparent)\n    float               LastAlpha;\n    short               PlatformMonitor;\n    bool                PlatformWindowCreated;\n    ImGuiWindow*        Window;                   // Set when the viewport is owned by a window (and ImGuiViewportFlags_CanHostOtherWindows is NOT set)\n    ImDrawList*         DrawLists[2];             // Convenience background (0) and foreground (1) draw lists. We use them to draw software mouser cursor when io.MouseDrawCursor is set and to draw most debug overlays.\n    ImDrawData          DrawDataP;\n    ImDrawDataBuilder   DrawDataBuilder;\n    ImVec2              LastPlatformPos;\n    ImVec2              LastPlatformSize;\n    ImVec2              LastRendererSize;\n\n    ImGuiViewportP()            { Idx = -1; LastFrameActive = LastFrameDrawLists[0] = LastFrameDrawLists[1] = LastFrontMostStampCount = -1; LastNameHash = 0; Alpha = LastAlpha = 1.0f; PlatformMonitor = -1; PlatformWindowCreated = false; Window = NULL; DrawLists[0] = DrawLists[1] = NULL; LastPlatformPos = LastPlatformSize = LastRendererSize = ImVec2(FLT_MAX, FLT_MAX); }\n    ~ImGuiViewportP()           { if (DrawLists[0]) IM_DELETE(DrawLists[0]); if (DrawLists[1]) IM_DELETE(DrawLists[1]); }\n    ImRect  GetRect() const     { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); }\n    ImVec2  GetCenter() const   { return ImVec2(Pos.x + Size.x * 0.5f, Pos.y + Size.y * 0.5f); }\n    void    ClearRequestFlags() { PlatformRequestClose = PlatformRequestMove = PlatformRequestResize = false; }\n};\n\nstruct ImGuiNavMoveResult\n{\n    ImGuiID       ID;           // Best candidate\n    ImGuiID       SelectScopeId;// Best candidate window current selectable group ID\n    ImGuiWindow*  Window;       // Best candidate window\n    float         DistBox;      // Best candidate box distance to current NavId\n    float         DistCenter;   // Best candidate center distance to current NavId\n    float         DistAxial;\n    ImRect        RectRel;      // Best candidate bounding box in window relative space\n\n    ImGuiNavMoveResult() { Clear(); }\n    void Clear()         { ID = SelectScopeId = 0; Window = NULL; DistBox = DistCenter = DistAxial = FLT_MAX; RectRel = ImRect(); }\n};\n\n// Storage for SetNexWindow** functions\nstruct ImGuiNextWindowData\n{\n    ImGuiCond               PosCond;\n    ImGuiCond               SizeCond;\n    ImGuiCond               ContentSizeCond;\n    ImGuiCond               CollapsedCond;\n    ImGuiCond               SizeConstraintCond;\n    ImGuiCond               FocusCond;\n    ImGuiCond               BgAlphaCond;\n    ImGuiCond               ViewportCond;\n    ImGuiCond               DockCond;\n    ImVec2                  PosVal;\n    ImVec2                  PosPivotVal;\n    ImVec2                  SizeVal;\n    ImVec2                  ContentSizeVal;\n    bool                    PosUndock;\n    bool                    CollapsedVal;\n    ImRect                  SizeConstraintRect;\n    ImGuiSizeCallback       SizeCallback;\n    void*                   SizeCallbackUserData;\n    float                   BgAlphaVal;\n    ImGuiID                 ViewportId;\n    ImGuiID                 DockId;\n    ImGuiWindowClass        WindowClass;\n    ImVec2                  MenuBarOffsetMinVal;                // This is not exposed publicly, so we don't clear it.\n\n    ImGuiNextWindowData()\n    {\n        PosCond = SizeCond = ContentSizeCond = CollapsedCond = SizeConstraintCond = FocusCond = BgAlphaCond = ViewportCond = DockCond = 0;\n        PosVal = PosPivotVal = SizeVal = ImVec2(0.0f, 0.0f);\n        ContentSizeVal = ImVec2(0.0f, 0.0f);\n        PosUndock = CollapsedVal = false;\n        SizeConstraintRect = ImRect();\n        SizeCallback = NULL;\n        SizeCallbackUserData = NULL;\n        BgAlphaVal = FLT_MAX;\n        ViewportId = DockId = 0;\n        MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f);\n    }\n\n    void    Clear()\n    {\n        PosCond = SizeCond = ContentSizeCond = CollapsedCond = SizeConstraintCond = FocusCond = BgAlphaCond = ViewportCond = DockCond = 0;\n        WindowClass = ImGuiWindowClass();\n    }\n};\n\n//-----------------------------------------------------------------------------\n// Docking, Tabs\n//-----------------------------------------------------------------------------\n\nstruct ImGuiTabBarSortItem\n{\n    int             Index;\n    float           Width;\n};\n\nstruct ImGuiTabBarRef\n{\n    ImGuiTabBar*    Ptr;                    // Either field can be set, not both. Dock node tab bars are loose while BeginTabBar() ones are in a pool.\n    int             IndexInMainPool;\n\n    ImGuiTabBarRef(ImGuiTabBar* ptr)        { Ptr = ptr; IndexInMainPool = -1; }\n    ImGuiTabBarRef(int index_in_main_pool)  { Ptr = NULL; IndexInMainPool = index_in_main_pool; }\n};\n\nenum ImGuiDockNodeFlagsPrivate_\n{\n    // [Internal]\n    ImGuiDockNodeFlags_DockSpace                = 1 << 10,  // Local  // A dockspace is a node that occupy space within an existing user window. Otherwise the node is floating and create its own window.\n    ImGuiDockNodeFlags_CentralNode              = 1 << 11,  // Local\n    ImGuiDockNodeFlags_NoTabBar                 = 1 << 12,  // Local  // Tab bar is completely unavailable. No triangle in the corner to enable it back.\n    ImGuiDockNodeFlags_HiddenTabBar             = 1 << 13,  // Local  // Tab bar is hidden, with a triangle in the corner to show it again (NB: actual tab-bar instance may be destroyed as this is only used for single-window tab bar)\n    ImGuiDockNodeFlags_SharedFlagsInheritMask_  = ~0,\n    ImGuiDockNodeFlags_LocalFlagsMask_          = ImGuiDockNodeFlags_NoSplit | ImGuiDockNodeFlags_NoResize | ImGuiDockNodeFlags_AutoHideTabBar | ImGuiDockNodeFlags_DockSpace | ImGuiDockNodeFlags_CentralNode | ImGuiDockNodeFlags_NoTabBar | ImGuiDockNodeFlags_HiddenTabBar,\n    ImGuiDockNodeFlags_LocalFlagsTransferMask_  = ImGuiDockNodeFlags_LocalFlagsMask_ & ~ImGuiDockNodeFlags_DockSpace  // When splitting those flags are moved to the inheriting child, never duplicated\n};\n\n// Store the source authority (dock node vs window) of a field\nenum ImGuiDataAuthority_\n{\n    ImGuiDataAuthority_Auto,\n    ImGuiDataAuthority_DockNode,\n    ImGuiDataAuthority_Window\n};\n\n// sizeof() 116~160\nstruct ImGuiDockNode\n{\n    ImGuiID                 ID;\n    ImGuiDockNodeFlags      SharedFlags;                // Flags shared by all nodes of a same dockspace hierarchy (inherited from the root node)\n    ImGuiDockNodeFlags      LocalFlags;                 // Flags specific to this node\n    ImGuiDockNode*          ParentNode;\n    ImGuiDockNode*          ChildNodes[2];              // [Split node only] Child nodes (left/right or top/bottom). Consider switching to an array.\n    ImVector<ImGuiWindow*>  Windows;                    // Note: unordered list! Iterate TabBar->Tabs for user-order.\n    ImGuiTabBar*            TabBar;\n    ImVec2                  Pos;                        // Current position\n    ImVec2                  Size;                       // Current size\n    ImVec2                  SizeRef;                    // [Split node only] Last explicitly written-to size (overridden when using a splitter affecting the node), used to calculate Size.\n    int                     SplitAxis;                  // [Split node only] Split axis (X or Y)\n    ImGuiWindowClass        WindowClass;\n\n    ImGuiWindow*            HostWindow;\n    ImGuiWindow*            VisibleWindow;              // Generally point to window which is ID is == SelectedTabID, but when CTRL+Tabbing this can be a different window.\n    ImGuiDockNode*          CentralNode;                // [Root node only] Pointer to central node.\n    ImGuiDockNode*          OnlyNodeWithWindows;        // [Root node only] Set when there is a single visible node within the hierarchy.\n    int                     LastFrameAlive;             // Last frame number the node was updated or kept alive explicitly with DockSpace() + ImGuiDockNodeFlags_KeepAliveOnly\n    int                     LastFrameActive;            // Last frame number the node was updated.\n    int                     LastFrameFocused;           // Last frame number the node was focused.\n    ImGuiID                 LastFocusedNodeID;          // [Root node only] Which of our child docking node (any ancestor in the hierarchy) was last focused.\n    ImGuiID                 SelectedTabID;              // [Tab node only] Which of our tab is selected.\n    ImGuiID                 WantCloseTabID;             // [Tab node only] Set when closing a specific tab.\n    ImGuiDataAuthority      AuthorityForPos         :3;\n    ImGuiDataAuthority      AuthorityForSize        :3;\n    ImGuiDataAuthority      AuthorityForViewport    :3;\n    bool                    IsVisible               :1; // Set to false when the node is hidden (usually disabled as it has no active window)\n    bool                    IsFocused               :1;\n    bool                    HasCloseButton          :1;\n    bool                    HasCollapseButton       :1;\n    bool                    WantCloseAll            :1; // Set when closing all tabs at once.\n    bool                    WantLockSizeOnce        :1;\n    bool                    WantMouseMove           :1; // After a node extraction we need to transition toward moving the newly created host window\n    bool                    WantHiddenTabBarUpdate  :1;\n    bool                    WantHiddenTabBarToggle  :1;\n\n    ImGuiDockNode(ImGuiID id);\n    ~ImGuiDockNode();\n    bool                    IsRootNode() const      { return ParentNode == NULL; }\n    bool                    IsDockSpace() const     { return (LocalFlags & ImGuiDockNodeFlags_DockSpace) != 0; }\n    bool                    IsCentralNode() const   { return (LocalFlags & ImGuiDockNodeFlags_CentralNode) != 0; }\n    bool                    IsHiddenTabBar() const  { return (LocalFlags & ImGuiDockNodeFlags_HiddenTabBar) != 0; } // Hidden tab bar can be shown back by clicking the small triangle\n    bool                    IsNoTabBar() const      { return (LocalFlags & ImGuiDockNodeFlags_NoTabBar) != 0; }     // Never show a tab bar\n    bool                    IsSplitNode() const     { return ChildNodes[0] != NULL; }\n    bool                    IsLeafNode() const      { return ChildNodes[0] == NULL; }\n    bool                    IsEmpty() const         { return ChildNodes[0] == NULL && Windows.Size == 0; }\n    ImGuiDockNodeFlags      GetMergedFlags() const  { return SharedFlags | LocalFlags; }\n    ImRect                  Rect() const            { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); }\n};\n\n//-----------------------------------------------------------------------------\n// Main imgui context\n//-----------------------------------------------------------------------------\n\nstruct ImGuiContext\n{\n    bool                    Initialized;\n    bool                    FrameScopeActive;                   // Set by NewFrame(), cleared by EndFrame()\n    bool                    FrameScopePushedImplicitWindow;     // Set by NewFrame(), cleared by EndFrame()\n    bool                    FontAtlasOwnedByContext;            // Io.Fonts-> is owned by the ImGuiContext and will be destructed along with it.\n    ImGuiIO                 IO;\n    ImGuiPlatformIO         PlatformIO;\n    ImGuiStyle              Style;\n    ImGuiConfigFlags        ConfigFlagsForFrame;                // = g.IO.ConfigFlags at the time of NewFrame()\n    ImFont*                 Font;                               // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back()\n    float                   FontSize;                           // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window.\n    float                   FontBaseSize;                       // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Base text height.\n    ImDrawListSharedData    DrawListSharedData;\n\n    double                  Time;\n    int                     FrameCount;\n    int                     FrameCountEnded;\n    int                     FrameCountPlatformEnded;\n    int                     FrameCountRendered;\n    ImVector<ImGuiWindow*>  Windows;                            // Windows, sorted in display order, back to front\n    ImVector<ImGuiWindow*>  WindowsFocusOrder;                  // Windows, sorted in focus order, back to front\n    ImVector<ImGuiWindow*>  WindowsSortBuffer;\n    ImVector<ImGuiWindow*>  CurrentWindowStack;\n    ImGuiStorage            WindowsById;\n    int                     WindowsActiveCount;\n    ImGuiWindow*            CurrentWindow;                      // Being drawn into\n    ImGuiWindow*            HoveredWindow;                      // Will catch mouse inputs\n    ImGuiWindow*            HoveredRootWindow;                  // Will catch mouse inputs (for focus/move only)\n    ImGuiWindow*            HoveredWindowUnderMovingWindow;     // Hovered window ignoring MovingWindow. Only set if MovingWindow is set.\n    ImGuiID                 HoveredId;                          // Hovered widget\n    bool                    HoveredIdAllowOverlap;\n    ImGuiID                 HoveredIdPreviousFrame;\n    float                   HoveredIdTimer;                     // Measure contiguous hovering time\n    float                   HoveredIdNotActiveTimer;            // Measure contiguous hovering time where the item has not been active\n    ImGuiID                 ActiveId;                           // Active widget\n    ImGuiID                 ActiveIdPreviousFrame;\n    ImGuiID                 ActiveIdIsAlive;                    // Active widget has been seen this frame (we can't use a bool as the ActiveId may change within the frame)\n    float                   ActiveIdTimer;\n    bool                    ActiveIdIsJustActivated;            // Set at the time of activation for one frame\n    bool                    ActiveIdAllowOverlap;               // Active widget allows another widget to steal active id (generally for overlapping widgets, but not always)\n    bool                    ActiveIdHasBeenPressed;             // Track whether the active id led to a press (this is to allow changing between PressOnClick and PressOnRelease without pressing twice). Used by range_select branch.\n    bool                    ActiveIdHasBeenEdited;              // Was the value associated to the widget Edited over the course of the Active state.\n    bool                    ActiveIdPreviousFrameIsAlive;\n    bool                    ActiveIdPreviousFrameHasBeenEdited;\n    int                     ActiveIdAllowNavDirFlags;           // Active widget allows using directional navigation (e.g. can activate a button and move away from it)\n    int                     ActiveIdBlockNavInputFlags;\n    ImVec2                  ActiveIdClickOffset;                // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior)\n    ImGuiWindow*            ActiveIdWindow;\n    ImGuiWindow*            ActiveIdPreviousFrameWindow;\n    ImGuiInputSource        ActiveIdSource;                     // Activating with mouse or nav (gamepad/keyboard)\n    ImGuiID                 LastActiveId;                       // Store the last non-zero ActiveId, useful for animation.\n    float                   LastActiveIdTimer;                  // Store the last non-zero ActiveId timer since the beginning of activation, useful for animation.\n    ImVec2                  LastValidMousePos;\n    ImGuiWindow*            MovingWindow;                       // Track the window we clicked on (in order to preserve focus). The actually window that is moved is generally MovingWindow->RootWindow.\n    ImVector<ImGuiColorMod> ColorModifiers;                     // Stack for PushStyleColor()/PopStyleColor()\n    ImVector<ImGuiStyleMod> StyleModifiers;                     // Stack for PushStyleVar()/PopStyleVar()\n    ImVector<ImFont*>       FontStack;                          // Stack for PushFont()/PopFont()\n    ImVector<ImGuiPopupData>OpenPopupStack;                     // Which popups are open (persistent)\n    ImVector<ImGuiPopupData>BeginPopupStack;                    // Which level of BeginPopup() we are in (reset every frame)\n    ImGuiNextWindowData     NextWindowData;                     // Storage for SetNextWindow** functions\n    bool                    NextTreeNodeOpenVal;                // Storage for SetNextTreeNode** functions\n    ImGuiCond               NextTreeNodeOpenCond;\n\n    // Viewports\n    ImVector<ImGuiViewportP*> Viewports;                        // Active viewports (always 1+, and generally 1 unless multi-viewports are enabled). Each viewports hold their copy of ImDrawData. \n    ImGuiViewportP*         CurrentViewport;                    // We track changes of viewport (happening in Begin) so we can call Platform_OnChangedViewport()\n    ImGuiViewportP*         MouseViewport;\n    ImGuiViewportP*         MouseLastHoveredViewport;           // Last known viewport that was hovered by mouse (even if we are not hovering any viewport any more) + honoring the _NoInputs flag.\n    ImGuiID                 PlatformLastFocusedViewport;        // Record of last focused platform window/viewport, when this changes we stamp the viewport as front-most\n    int                     ViewportFrontMostStampCount;        // Every time the front-most window changes, we stamp its viewport with an incrementing counter\n\n    // Navigation data (for gamepad/keyboard)\n    ImGuiWindow*            NavWindow;                          // Focused window for navigation. Could be called 'FocusWindow'\n    ImGuiID                 NavId;                              // Focused item for navigation\n    ImGuiID                 NavActivateId;                      // ~~ (g.ActiveId == 0) && IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0, also set when calling ActivateItem()\n    ImGuiID                 NavActivateDownId;                  // ~~ IsNavInputDown(ImGuiNavInput_Activate) ? NavId : 0\n    ImGuiID                 NavActivatePressedId;               // ~~ IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0\n    ImGuiID                 NavInputId;                         // ~~ IsNavInputPressed(ImGuiNavInput_Input) ? NavId : 0\n    ImGuiID                 NavJustTabbedId;                    // Just tabbed to this id.\n    ImGuiID                 NavJustMovedToId;                   // Just navigated to this id (result of a successfully MoveRequest).\n    ImGuiID                 NavJustMovedToMultiSelectScopeId;   // Just navigated to this select scope id (result of a successfully MoveRequest).\n    ImGuiID                 NavNextActivateId;                  // Set by ActivateItem(), queued until next frame.\n    ImGuiInputSource        NavInputSource;                     // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard.\n    ImRect                  NavScoringRectScreen;               // Rectangle used for scoring, in screen space. Based of window->DC.NavRefRectRel[], modified for directional navigation scoring.\n    int                     NavScoringCount;                    // Metrics for debugging\n    ImGuiWindow*            NavWindowingTarget;                 // When selecting a window (holding Menu+FocusPrev/Next, or equivalent of CTRL-TAB) this window is temporarily displayed front-most.\n    ImGuiWindow*            NavWindowingTargetAnim;             // Record of last valid NavWindowingTarget until DimBgRatio and NavWindowingHighlightAlpha becomes 0.0f\n    ImGuiWindow*            NavWindowingList;\n    float                   NavWindowingTimer;\n    float                   NavWindowingHighlightAlpha;\n    bool                    NavWindowingToggleLayer;\n    ImGuiNavLayer           NavLayer;                           // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later.\n    int                     NavIdTabCounter;                    // == NavWindow->DC.FocusIdxTabCounter at time of NavId processing\n    bool                    NavIdIsAlive;                       // Nav widget has been seen this frame ~~ NavRefRectRel is valid\n    bool                    NavMousePosDirty;                   // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default)\n    bool                    NavDisableHighlight;                // When user starts using mouse, we hide gamepad/keyboard highlight (NB: but they are still available, which is why NavDisableHighlight isn't always != NavDisableMouseHover)\n    bool                    NavDisableMouseHover;               // When user starts using gamepad/keyboard, we hide mouse hovering highlight until mouse is touched again.\n    bool                    NavAnyRequest;                      // ~~ NavMoveRequest || NavInitRequest\n    bool                    NavInitRequest;                     // Init request for appearing window to select first item\n    bool                    NavInitRequestFromMove;\n    ImGuiID                 NavInitResultId;\n    ImRect                  NavInitResultRectRel;\n    bool                    NavMoveFromClampedRefRect;          // Set by manual scrolling, if we scroll to a point where NavId isn't visible we reset navigation from visible items\n    bool                    NavMoveRequest;                     // Move request for this frame\n    ImGuiNavMoveFlags       NavMoveRequestFlags;\n    ImGuiNavForward         NavMoveRequestForward;              // None / ForwardQueued / ForwardActive (this is used to navigate sibling parent menus from a child menu)\n    ImGuiDir                NavMoveDir, NavMoveDirLast;         // Direction of the move request (left/right/up/down), direction of the previous move request\n    ImGuiDir                NavMoveClipDir;\n    ImGuiNavMoveResult      NavMoveResultLocal;                 // Best move request candidate within NavWindow\n    ImGuiNavMoveResult      NavMoveResultLocalVisibleSet;       // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag)\n    ImGuiNavMoveResult      NavMoveResultOther;                 // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag)\n\n    // Tabbing system (older than Nav, active even if Nav is disabled. FIXME-NAV: This needs a redesign!)\n    ImGuiWindow*            FocusRequestCurrWindow;             //\n    ImGuiWindow*            FocusRequestNextWindow;             //\n    int                     FocusRequestCurrCounterAll;         // Any item being requested for focus, stored as an index (we on layout to be stable between the frame pressing TAB and the next frame, semi-ouch)\n    int                     FocusRequestCurrCounterTab;         // Tab item being requested for focus, stored as an index\n    int                     FocusRequestNextCounterAll;         // Stored for next frame\n    int                     FocusRequestNextCounterTab;         // \"\n    bool                    FocusTabPressed;                    //\n\n    // Render\n    float                   DimBgRatio;                         // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list)\n    ImGuiMouseCursor        MouseCursor;\n\n    // Drag and Drop\n    bool                    DragDropActive;\n    bool                    DragDropWithinSourceOrTarget;\n    ImGuiDragDropFlags      DragDropSourceFlags;\n    int                     DragDropSourceFrameCount;\n    int                     DragDropMouseButton;\n    ImGuiPayload            DragDropPayload;\n    ImRect                  DragDropTargetRect;\n    ImGuiID                 DragDropTargetId;\n    ImGuiDragDropFlags      DragDropAcceptFlags;\n    float                   DragDropAcceptIdCurrRectSurface;    // Target item surface (we resolve overlapping targets by prioritizing the smaller surface)\n    ImGuiID                 DragDropAcceptIdCurr;               // Target item id (set at the time of accepting the payload)\n    ImGuiID                 DragDropAcceptIdPrev;               // Target item id from previous frame (we need to store this to allow for overlapping drag and drop targets)\n    int                     DragDropAcceptFrameCount;           // Last time a target expressed a desire to accept the source\n    ImVector<unsigned char> DragDropPayloadBufHeap;             // We don't expose the ImVector<> directly\n    unsigned char           DragDropPayloadBufLocal[8];         // Local buffer for small payloads\n\n    // Tab bars\n    ImPool<ImGuiTabBar>             TabBars;\n    ImGuiTabBar*                    CurrentTabBar;\n    ImVector<ImGuiTabBarRef>        CurrentTabBarStack;\n    ImVector<ImGuiTabBarSortItem>   TabSortByWidthBuffer;\n\n    // Widget state\n    ImGuiInputTextState     InputTextState;\n    ImFont                  InputTextPasswordFont;\n    ImGuiID                 TempInputTextId;                    // Temporary text input when CTRL+clicking on a slider, etc.\n    ImGuiColorEditFlags     ColorEditOptions;                   // Store user options for color edit widgets\n    ImVec4                  ColorPickerRef;\n    bool                    DragCurrentAccumDirty;\n    float                   DragCurrentAccum;                   // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings\n    float                   DragSpeedDefaultRatio;              // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio\n    ImVec2                  ScrollbarClickDeltaToGrabCenter;    // Distance between mouse and center of grab box, normalized in parent space. Use storage?\n    int                     TooltipOverrideCount;\n    ImVector<char>          PrivateClipboard;                   // If no custom clipboard handler is defined\n\n    // Range-Select/Multi-Select\n    // [This is unused in this branch, but left here to facilitate merging/syncing multiple branches]\n    ImGuiID                 MultiSelectScopeId;\n\n    // Platform support\n    ImVec2                  PlatformImePos;                     // Cursor position request & last passed to the OS Input Method Editor\n    ImVec2                  PlatformImeLastPos;\n    ImGuiViewportP*         PlatformImePosViewport;\n\n    // Extensions\n    // FIXME: We could provide an API to register one slot in an array held in ImGuiContext?\n    ImGuiDockContext*       DockContext;\n\n    // Settings\n    bool                           SettingsLoaded;\n    float                          SettingsDirtyTimer;          // Save .ini Settings to memory when time reaches zero\n    ImGuiTextBuffer                SettingsIniData;             // In memory .ini settings\n    ImVector<ImGuiSettingsHandler> SettingsHandlers;            // List of .ini settings handlers\n    ImVector<ImGuiWindowSettings>  SettingsWindows;             // ImGuiWindow .ini settings entries (parsed from the last loaded .ini file and maintained on saving)\n\n    // Logging\n    bool                    LogEnabled;\n    ImGuiLogType            LogType;\n    FILE*                   LogFile;                            // If != NULL log to stdout/ file\n    ImGuiTextBuffer         LogBuffer;                          // Accumulation buffer when log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators.\n    float                   LogLinePosY;\n    bool                    LogLineFirstItem;\n    int                     LogDepthRef;\n    int                     LogDepthToExpand;\n    int                     LogDepthToExpandDefault;            // Default/stored value for LogDepthMaxExpand if not specified in the LogXXX function call.\n\n    // Misc\n    float                   FramerateSecPerFrame[120];          // Calculate estimate of framerate for user over the last 2 seconds.\n    int                     FramerateSecPerFrameIdx;\n    float                   FramerateSecPerFrameAccum;\n    int                     WantCaptureMouseNextFrame;          // Explicit capture via CaptureKeyboardFromApp()/CaptureMouseFromApp() sets those flags\n    int                     WantCaptureKeyboardNextFrame;\n    int                     WantTextInputNextFrame;\n    char                    TempBuffer[1024*3+1];               // Temporary text buffer\n\n    ImGuiContext(ImFontAtlas* shared_font_atlas)\n    {\n        Initialized = false;\n        FrameScopeActive = FrameScopePushedImplicitWindow = false;\n        ConfigFlagsForFrame = ImGuiConfigFlags_None;\n        Font = NULL;\n        FontSize = FontBaseSize = 0.0f;\n        FontAtlasOwnedByContext = shared_font_atlas ? false : true;\n        IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)();\n\n        Time = 0.0f;\n        FrameCount = 0;\n        FrameCountEnded = FrameCountPlatformEnded = FrameCountRendered = -1;\n        WindowsActiveCount = 0;\n        CurrentWindow = NULL;\n        HoveredWindow = NULL;\n        HoveredRootWindow = NULL;\n        HoveredWindowUnderMovingWindow = NULL;\n        HoveredId = 0;\n        HoveredIdAllowOverlap = false;\n        HoveredIdPreviousFrame = 0;\n        HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f;\n        ActiveId = 0;\n        ActiveIdPreviousFrame = 0;\n        ActiveIdIsAlive = 0;\n        ActiveIdTimer = 0.0f;\n        ActiveIdIsJustActivated = false;\n        ActiveIdAllowOverlap = false;\n        ActiveIdHasBeenPressed = false;\n        ActiveIdHasBeenEdited = false;\n        ActiveIdPreviousFrameIsAlive = false;\n        ActiveIdPreviousFrameHasBeenEdited = false;\n        ActiveIdAllowNavDirFlags = 0x00;\n        ActiveIdBlockNavInputFlags = 0x00;\n        ActiveIdClickOffset = ImVec2(-1,-1);\n        ActiveIdWindow = ActiveIdPreviousFrameWindow = NULL;\n        ActiveIdSource = ImGuiInputSource_None;\n        LastActiveId = 0;\n        LastActiveIdTimer = 0.0f;\n        LastValidMousePos = ImVec2(0.0f, 0.0f);\n        MovingWindow = NULL;\n        NextTreeNodeOpenVal = false;\n        NextTreeNodeOpenCond = 0;\n\n        CurrentViewport = NULL;\n        MouseViewport = MouseLastHoveredViewport = NULL;\n        PlatformLastFocusedViewport = 0;\n        ViewportFrontMostStampCount = 0;\n\n        NavWindow = NULL;\n        NavId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0;\n        NavJustTabbedId = NavJustMovedToId = NavJustMovedToMultiSelectScopeId = NavNextActivateId = 0;\n        NavInputSource = ImGuiInputSource_None;\n        NavScoringRectScreen = ImRect();\n        NavScoringCount = 0;\n        NavWindowingTarget = NavWindowingTargetAnim = NavWindowingList = NULL;\n        NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f;\n        NavWindowingToggleLayer = false;\n        NavLayer = ImGuiNavLayer_Main;\n        NavIdTabCounter = INT_MAX;\n        NavIdIsAlive = false;\n        NavMousePosDirty = false;\n        NavDisableHighlight = true;\n        NavDisableMouseHover = false;\n        NavAnyRequest = false;\n        NavInitRequest = false;\n        NavInitRequestFromMove = false;\n        NavInitResultId = 0;\n        NavMoveFromClampedRefRect = false;\n        NavMoveRequest = false;\n        NavMoveRequestFlags = 0;\n        NavMoveRequestForward = ImGuiNavForward_None;\n        NavMoveDir = NavMoveDirLast = NavMoveClipDir = ImGuiDir_None;\n\n        FocusRequestCurrWindow = FocusRequestNextWindow = NULL;\n        FocusRequestCurrCounterAll = FocusRequestCurrCounterTab = INT_MAX;\n        FocusRequestNextCounterAll = FocusRequestNextCounterTab = INT_MAX;\n        FocusTabPressed = false;\n\n        DimBgRatio = 0.0f;\n        MouseCursor = ImGuiMouseCursor_Arrow;\n\n        DragDropActive = DragDropWithinSourceOrTarget = false;\n        DragDropSourceFlags = 0;\n        DragDropSourceFrameCount = -1;\n        DragDropMouseButton = -1;\n        DragDropTargetId = 0;\n        DragDropAcceptFlags = 0;\n        DragDropAcceptIdCurrRectSurface = 0.0f;\n        DragDropAcceptIdPrev = DragDropAcceptIdCurr = 0;\n        DragDropAcceptFrameCount = -1;\n        memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal));\n\n        CurrentTabBar = NULL;\n\n        TempInputTextId = 0;\n        ColorEditOptions = ImGuiColorEditFlags__OptionsDefault;\n        DragCurrentAccumDirty = false;\n        DragCurrentAccum = 0.0f;\n        DragSpeedDefaultRatio = 1.0f / 100.0f;\n        ScrollbarClickDeltaToGrabCenter = ImVec2(0.0f, 0.0f);\n        TooltipOverrideCount = 0;\n\n        MultiSelectScopeId = 0;\n\n        PlatformImePos = PlatformImeLastPos = ImVec2(FLT_MAX, FLT_MAX);\n        PlatformImePosViewport = 0;\n\n        DockContext = NULL;\n\n        SettingsLoaded = false;\n        SettingsDirtyTimer = 0.0f;\n\n        LogEnabled = false;\n        LogType = ImGuiLogType_None;\n        LogFile = NULL;\n        LogLinePosY = FLT_MAX;\n        LogLineFirstItem = false;\n        LogDepthRef = 0;\n        LogDepthToExpand = LogDepthToExpandDefault = 2;\n\n        memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame));\n        FramerateSecPerFrameIdx = 0;\n        FramerateSecPerFrameAccum = 0.0f;\n        WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1;\n        memset(TempBuffer, 0, sizeof(TempBuffer));\n    }\n};\n\n//-----------------------------------------------------------------------------\n// ImGuiWindow\n//-----------------------------------------------------------------------------\n\n// Transient per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the DC variable name in ImGuiWindow.\n// FIXME: That's theory, in practice the delimitation between ImGuiWindow and ImGuiWindowTempData is quite tenuous and could be reconsidered.\nstruct IMGUI_API ImGuiWindowTempData\n{\n    ImVec2                  CursorPos;\n    ImVec2                  CursorPosPrevLine;\n    ImVec2                  CursorStartPos;         // Initial position in client area with padding\n    ImVec2                  CursorMaxPos;           // Used to implicitly calculate the size of our contents, always growing during the frame. Turned into window->SizeContents at the beginning of next frame\n    ImVec2                  CurrentLineSize;\n    float                   CurrentLineTextBaseOffset;\n    ImVec2                  PrevLineSize;\n    float                   PrevLineTextBaseOffset;\n    int                     TreeDepth;\n    ImU32                   TreeStoreMayJumpToParentOnPop; // Store a copy of !g.NavIdIsAlive for TreeDepth 0..31.. Could be turned into a ImU64 if necessary.\n    ImGuiID                 LastItemId;\n    ImGuiItemStatusFlags    LastItemStatusFlags;\n    ImRect                  LastItemRect;           // Interaction rect\n    ImRect                  LastItemDisplayRect;    // End-user display rect (only valid if LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect)\n    ImGuiNavLayer           NavLayerCurrent;        // Current layer, 0..31 (we currently only use 0..1)\n    int                     NavLayerCurrentMask;    // = (1 << NavLayerCurrent) used by ItemAdd prior to clipping.\n    int                     NavLayerActiveMask;     // Which layer have been written to (result from previous frame)\n    int                     NavLayerActiveMaskNext; // Which layer have been written to (buffer for current frame)\n    bool                    NavHideHighlightOneFrame;\n    bool                    NavHasScroll;           // Set when scrolling can be used (ScrollMax > 0.0f)\n    bool                    MenuBarAppending;       // FIXME: Remove this\n    ImVec2                  MenuBarOffset;          // MenuBarOffset.x is sort of equivalent of a per-layer CursorPos.x, saved/restored as we switch to the menu bar. The only situation when MenuBarOffset.y is > 0 if when (SafeAreaPadding.y > FramePadding.y), often used on TVs.\n    ImVector<ImGuiWindow*>  ChildWindows;\n    ImGuiStorage*           StateStorage;\n    ImGuiLayoutType         LayoutType;\n    ImGuiLayoutType         ParentLayoutType;       // Layout type of parent window at the time of Begin()\n    int                     FocusCounterAll;        // Counter for focus/tabbing system. Start at -1 and increase as assigned via FocusableItemRegister() (FIXME-NAV: Needs redesign)\n    int                     FocusCounterTab;        // (same, but only count widgets which you can Tab through)\n\n    // We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings.\n    ImGuiItemFlags          ItemFlags;              // == ItemFlagsStack.back() [empty == ImGuiItemFlags_Default]\n    float                   ItemWidth;              // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window\n    float                   NextItemWidth;\n    float                   TextWrapPos;            // == TextWrapPosStack.back() [empty == -1.0f]\n    ImVector<ImGuiItemFlags>ItemFlagsStack;\n    ImVector<float>         ItemWidthStack;\n    ImVector<float>         TextWrapPosStack;\n    ImVector<ImGuiGroupData>GroupStack;\n    short                   StackSizesBackup[6];    // Store size of various stacks for asserting\n\n    ImVec1                  Indent;                 // Indentation / start position from left of window (increased by TreePush/TreePop, etc.)\n    ImVec1                  GroupOffset;\n    ImVec1                  ColumnsOffset;          // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API.\n    ImGuiColumns*           CurrentColumns;         // Current columns set\n\n    ImGuiWindowTempData()\n    {\n        CursorPos = CursorPosPrevLine = CursorStartPos = CursorMaxPos = ImVec2(0.0f, 0.0f);\n        CurrentLineSize = PrevLineSize = ImVec2(0.0f, 0.0f);\n        CurrentLineTextBaseOffset = PrevLineTextBaseOffset = 0.0f;\n        TreeDepth = 0;\n        TreeStoreMayJumpToParentOnPop = 0x00;\n        LastItemId = 0;\n        LastItemStatusFlags = 0;\n        LastItemRect = LastItemDisplayRect = ImRect();\n        NavLayerActiveMask = NavLayerActiveMaskNext = 0x00;\n        NavLayerCurrent = ImGuiNavLayer_Main;\n        NavLayerCurrentMask = (1 << ImGuiNavLayer_Main);\n        NavHideHighlightOneFrame = false;\n        NavHasScroll = false;\n        MenuBarAppending = false;\n        MenuBarOffset = ImVec2(0.0f, 0.0f);\n        StateStorage = NULL;\n        LayoutType = ParentLayoutType = ImGuiLayoutType_Vertical;\n        FocusCounterAll = FocusCounterTab = -1;\n\n        ItemFlags = ImGuiItemFlags_Default_;\n        ItemWidth = 0.0f;\n        NextItemWidth = +FLT_MAX;\n        TextWrapPos = -1.0f;\n        memset(StackSizesBackup, 0, sizeof(StackSizesBackup));\n\n        Indent = ImVec1(0.0f);\n        GroupOffset = ImVec1(0.0f);\n        ColumnsOffset = ImVec1(0.0f);\n        CurrentColumns = NULL;\n    }\n};\n\n// Storage for one window\nstruct IMGUI_API ImGuiWindow\n{\n    char*                   Name;\n    ImGuiID                 ID;                                 // == ImHashStr(Name)\n    ImGuiWindowFlags        Flags, FlagsPreviousFrame;          // See enum ImGuiWindowFlags_\n    ImGuiWindowClass        WindowClass;                        // Advanced users only. Set with SetNextWindowClass()\n    ImGuiViewportP*         Viewport;                           // Always set in Begin(), only inactive windows may have a NULL value here\n    ImGuiID                 ViewportId;                         // We backup the viewport id (since the viewport may disappear or never be created if the window is inactive)\n    ImVec2                  ViewportPos;                        // We backup the viewport position (since the viewport may disappear or never be created if the window is inactive)\n    int                     ViewportAllowPlatformMonitorExtend; // Reset to -1 every frame (index is guaranteed to be valid between NewFrame..EndFrame), only used in the Appearing frame of a tooltip/popup to enforce clamping to a given monitor\n    ImVec2                  Pos;                                // Position (always rounded-up to nearest pixel)\n    ImVec2                  Size;                               // Current size (==SizeFull or collapsed title bar size)\n    ImVec2                  SizeFull;                           // Size when non collapsed\n    ImVec2                  SizeFullAtLastBegin;                // Copy of SizeFull at the end of Begin. This is the reference value we'll use on the next frame to decide if we need scrollbars.\n    ImVec2                  SizeContents;                       // Size of contents (== extents reach of the drawing cursor) from previous frame. Include decoration, window title, border, menu, etc.\n    ImVec2                  SizeContentsExplicit;               // Size of contents explicitly set by the user via SetNextWindowContentSize()\n    ImVec2                  WindowPadding;                      // Window padding at the time of begin.\n    float                   WindowRounding;                     // Window rounding at the time of begin.\n    float                   WindowBorderSize;                   // Window border size at the time of begin.\n    int                     NameBufLen;                         // Size of buffer storing Name. May be larger than strlen(Name)!\n    ImGuiID                 MoveId;                             // == window->GetID(\"#MOVE\")\n    ImGuiID                 ChildId;                            // ID of corresponding item in parent window (for navigation to return from child window to parent window)\n    ImVec2                  Scroll;\n    ImVec2                  ScrollTarget;                       // target scroll position. stored as cursor position with scrolling canceled out, so the highest point is always 0.0f. (FLT_MAX for no change)\n    ImVec2                  ScrollTargetCenterRatio;            // 0.0f = scroll so that target position is at top, 0.5f = scroll so that target position is centered\n    ImVec2                  ScrollbarSizes;                     // Size taken by scrollbars on each axis\n    bool                    ScrollbarX, ScrollbarY;\n    bool                    ViewportOwned;\n    bool                    Active;                             // Set to true on Begin(), unless Collapsed\n    bool                    WasActive;\n    bool                    WriteAccessed;                      // Set to true when any widget access the current window\n    bool                    Collapsed;                          // Set when collapsing window to become only title-bar\n    bool                    WantCollapseToggle;\n    bool                    SkipItems;                          // Set when items can safely be all clipped (e.g. window not visible or collapsed)\n    bool                    Appearing;                          // Set during the frame where the window is appearing (or re-appearing)\n    bool                    Hidden;                             // Do not display (== (HiddenFrames*** > 0))\n    bool                    HasCloseButton;                     // Set when the window has a close button (p_open != NULL)\n    signed char             ResizeBorderHeld;                   // Current border being held for resize (-1: none, otherwise 0-3)\n    short                   BeginCount;                         // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs)\n    short                   BeginOrderWithinParent;             // Order within immediate parent window, if we are a child window. Otherwise 0.\n    short                   BeginOrderWithinContext;            // Order within entire imgui context. This is mostly used for debugging submission order related issues.\n    ImGuiID                 PopupId;                            // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling)\n    int                     AutoFitFramesX, AutoFitFramesY;\n    bool                    AutoFitOnlyGrows;\n    int                     AutoFitChildAxises;\n    ImGuiDir                AutoPosLastDirection;\n    int                     HiddenFramesCanSkipItems;           // Hide the window for N frames\n    int                     HiddenFramesCannotSkipItems;        // Hide the window for N frames while allowing items to be submitted so we can measure their size\n    ImGuiCond               SetWindowPosAllowFlags;             // store acceptable condition flags for SetNextWindowPos() use.\n    ImGuiCond               SetWindowSizeAllowFlags;            // store acceptable condition flags for SetNextWindowSize() use.\n    ImGuiCond               SetWindowCollapsedAllowFlags;       // store acceptable condition flags for SetNextWindowCollapsed() use.\n    ImGuiCond               SetWindowDockAllowFlags;            // store acceptable condition flags for SetNextWindowDock() use.\n    ImVec2                  SetWindowPosVal;                    // store window position when using a non-zero Pivot (position set needs to be processed when we know the window size)\n    ImVec2                  SetWindowPosPivot;                  // store window pivot for positioning. ImVec2(0,0) when positioning from top-left corner; ImVec2(0.5f,0.5f) for centering; ImVec2(1,1) for bottom right.\n\n    ImGuiWindowTempData     DC;                                 // Temporary per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the \"DC\" variable name.\n    ImVector<ImGuiID>       IDStack;                            // ID stack. ID are hashes seeded with the value at the top of the stack\n    ImRect                  ClipRect;                           // Current clipping rectangle. = DrawList->clip_rect_stack.back(). Scissoring / clipping rectangle. x1, y1, x2, y2.\n    ImRect                  OuterRectClipped;                   // = WindowRect just after setup in Begin(). == window->Rect() for root window.\n    ImRect                  InnerMainRect, InnerClipRect;\n    ImVec2ih                HitTestHoleSize, HitTestHoleOffset;\n    ImRect                  ContentsRegionRect;                 // FIXME: This is currently confusing/misleading. Maximum visible content position ~~ Pos + (SizeContentsExplicit ? SizeContentsExplicit : Size - ScrollbarSizes) - CursorStartPos, per axis\n    int                     LastFrameActive;                    // Last frame number the window was Active.\n    int                     LastFrameJustFocused;               // Last frame number the window was made Focused.\n    float                   ItemWidthDefault;\n    ImGuiMenuColumns        MenuColumns;                        // Simplified columns storage for menu items\n    ImGuiStorage            StateStorage;\n    ImVector<ImGuiColumns>  ColumnsStorage;\n    float                   FontWindowScale;                    // User scale multiplier per-window\n    float                   FontDpiScale;\n    int                     SettingsIdx;                        // Index into SettingsWindow[] (indices are always valid as we only grow the array from the back)\n\n    ImDrawList*             DrawList;                           // == &DrawListInst (for backward compatibility reason with code using imgui_internal.h we keep this a pointer)\n    ImDrawList              DrawListInst;\n    ImGuiWindow*            ParentWindow;                       // If we are a child _or_ popup window, this is pointing to our parent. Otherwise NULL.\n    ImGuiWindow*            RootWindow;                         // Point to ourself or first ancestor that is not a child window.\n    ImGuiWindow*            RootWindowDockStop;                 // Point to ourself or first ancestor that is not a child window. Doesn't cross through dock nodes. We use this so IsWindowFocused() can behave consistently regardless of docking state.\n    ImGuiWindow*            RootWindowForTitleBarHighlight;     // Point to ourself or first ancestor which will display TitleBgActive color when this window is active.\n    ImGuiWindow*            RootWindowForNav;                   // Point to ourself or first ancestor which doesn't have the NavFlattened flag.\n\n    ImGuiWindow*            NavLastChildNavWindow;              // When going to the menu bar, we remember the child window we came from. (This could probably be made implicit if we kept g.Windows sorted by last focused including child window.)\n    ImGuiID                 NavLastIds[ImGuiNavLayer_COUNT];    // Last known NavId for this window, per layer (0/1)\n    ImRect                  NavRectRel[ImGuiNavLayer_COUNT];    // Reference rectangle, in window relative space\n\n    // Docking\n    ImGuiDockNode*          DockNode;                           // Which node are we docked into\n    ImGuiDockNode*          DockNodeAsHost;                     // Which node are we owning (for parent windows)\n    ImGuiID                 DockId;                             // Backup of last valid DockNode->Id, so single value remember their dock node id\n    ImGuiItemStatusFlags    DockTabItemStatusFlags;\n    ImRect                  DockTabItemRect;\n    short                   DockOrder;                          // Order of the last time the window was visible within its DockNode. This is used to reorder windows that are reappearing on the same frame. Same value between windows that were active and windows that were none are possible.\n    bool                    DockIsActive        :1;             // =~ (DockNode != NULL) && (DockNode->Windows.Size > 1)\n    bool                    DockTabIsVisible    :1;             // Is the window visible this frame? =~ is the corresponding tab selected?\n    bool                    DockTabWantClose    :1;\n\npublic:\n    ImGuiWindow(ImGuiContext* context, const char* name);\n    ~ImGuiWindow();\n\n    ImGuiID     GetID(const char* str, const char* str_end = NULL);\n    ImGuiID     GetID(const void* ptr);\n    ImGuiID     GetIDNoKeepAlive(const char* str, const char* str_end = NULL);\n    ImGuiID     GetIDNoKeepAlive(const void* ptr);\n    ImGuiID     GetIDFromRectangle(const ImRect& r_abs);\n\n    // We don't use g.FontSize because the window may be != g.CurrentWidow.\n    ImRect      Rect() const                            { return ImRect(Pos.x, Pos.y, Pos.x+Size.x, Pos.y+Size.y); }\n    float       CalcFontSize() const                    { return GImGui->FontBaseSize * FontWindowScale * FontDpiScale; }\n    float       TitleBarHeight() const                  { return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f; }\n    ImRect      TitleBarRect() const                    { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); }\n    float       MenuBarHeight() const                   { return (Flags & ImGuiWindowFlags_MenuBar) ? DC.MenuBarOffset.y + CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f : 0.0f; }\n    ImRect      MenuBarRect() const                     { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); }\n};\n\n// Backup and restore just enough data to be able to use IsItemHovered() on item A after another B in the same window has overwritten the data.\nstruct ImGuiItemHoveredDataBackup\n{\n    ImGuiID                 LastItemId;\n    ImGuiItemStatusFlags    LastItemStatusFlags;\n    ImRect                  LastItemRect;\n    ImRect                  LastItemDisplayRect;\n\n    ImGuiItemHoveredDataBackup() { Backup(); }\n    void Backup()           { ImGuiWindow* window = GImGui->CurrentWindow; LastItemId = window->DC.LastItemId; LastItemStatusFlags = window->DC.LastItemStatusFlags; LastItemRect = window->DC.LastItemRect; LastItemDisplayRect = window->DC.LastItemDisplayRect; }\n    void Restore() const    { ImGuiWindow* window = GImGui->CurrentWindow; window->DC.LastItemId = LastItemId; window->DC.LastItemStatusFlags = LastItemStatusFlags; window->DC.LastItemRect = LastItemRect; window->DC.LastItemDisplayRect = LastItemDisplayRect; }\n};\n\n//-----------------------------------------------------------------------------\n// Tab bar, tab item\n//-----------------------------------------------------------------------------\n\nenum ImGuiTabBarFlagsPrivate_\n{\n    ImGuiTabBarFlags_DockNode                   = 1 << 20,  // Part of a dock node [we don't use this in the master branch but it facilitate branch syncing to keep this around]\n    ImGuiTabBarFlags_IsFocused                  = 1 << 21,\n    ImGuiTabBarFlags_SaveSettings               = 1 << 22   // FIXME: Settings are handled by the docking system, this only request the tab bar to mark settings dirty when reordering tabs\n};\n\nenum ImGuiTabItemFlagsPrivate_\n{\n    ImGuiTabItemFlags_NoCloseButton             = 1 << 20,  // Store whether p_open is set or not, which we need to recompute WidthContents during layout.\n    ImGuiTabItemFlags_Unsorted                  = 1 << 21,  // [Docking] Trailing tabs with the _Unsorted flag will be sorted based on the DockOrder of their Window.\n    ImGuiTabItemFlags_Preview                   = 1 << 22   // [Docking] Display tab shape for docking preview (height is adjusted slightly to compensate for the yet missing tab bar)\n};\n\n// Storage for one active tab item (sizeof() 32~40 bytes)\nstruct ImGuiTabItem\n{\n    ImGuiID             ID;\n    ImGuiTabItemFlags   Flags;\n    ImGuiWindow*        Window;                 // When TabItem is part of a DockNode's TabBar, we hold on to a window.\n    int                 LastFrameVisible;\n    int                 LastFrameSelected;      // This allows us to infer an ordered list of the last activated tabs with little maintenance\n    int                 NameOffset;             // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames\n    float               Offset;                 // Position relative to beginning of tab\n    float               Width;                  // Width currently displayed\n    float               WidthContents;          // Width of actual contents, stored during BeginTabItem() call\n\n    ImGuiTabItem()      { ID = Flags = 0; Window = NULL; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = WidthContents = 0.0f; }\n};\n\n// Storage for a tab bar (sizeof() 92~96 bytes)\nstruct ImGuiTabBar\n{\n    ImVector<ImGuiTabItem> Tabs;\n    ImGuiID             ID;                     // Zero for tab-bars used by docking\n    ImGuiID             SelectedTabId;          // Selected tab\n    ImGuiID             NextSelectedTabId;\n    ImGuiID             VisibleTabId;           // Can occasionally be != SelectedTabId (e.g. when previewing contents for CTRL+TAB preview)\n    int                 CurrFrameVisible;\n    int                 PrevFrameVisible;\n    ImRect              BarRect;\n    float               ContentsHeight;\n    float               OffsetMax;              // Distance from BarRect.Min.x, locked during layout\n    float               OffsetNextTab;          // Distance from BarRect.Min.x, incremented with each BeginTabItem() call, not used if ImGuiTabBarFlags_Reorderable if set.\n    float               ScrollingAnim;\n    float               ScrollingTarget;\n    float               ScrollingTargetDistToVisibility;\n    float               ScrollingSpeed;\n    ImGuiTabBarFlags    Flags;\n    ImGuiID             ReorderRequestTabId;\n    int                 ReorderRequestDir;\n    bool                WantLayout;\n    bool                VisibleTabWasSubmitted;\n    short               LastTabItemIdx;         // For BeginTabItem()/EndTabItem()\n    ImVec2              FramePadding;           // style.FramePadding locked at the time of BeginTabBar()\n    ImGuiTextBuffer     TabsNames;              // For non-docking tab bar we re-append names in a contiguous buffer.\n\n    ImGuiTabBar();\n    int                 GetTabOrder(const ImGuiTabItem* tab) const  { return Tabs.index_from_ptr(tab); }\n    const char*         GetTabName(const ImGuiTabItem* tab) const\n    {\n        if (tab->Window)\n            return tab->Window->Name;\n        IM_ASSERT(tab->NameOffset != -1 && tab->NameOffset < TabsNames.Buf.Size);\n        return TabsNames.Buf.Data + tab->NameOffset;\n    }\n};\n\n//-----------------------------------------------------------------------------\n// Internal API\n// No guarantee of forward compatibility here.\n//-----------------------------------------------------------------------------\n\nnamespace ImGui\n{\n    // We should always have a CurrentWindow in the stack (there is an implicit \"Debug\" window)\n    // If this ever crash because g.CurrentWindow is NULL it means that either\n    // - ImGui::NewFrame() has never been called, which is illegal.\n    // - You are calling ImGui functions after ImGui::EndFrame()/ImGui::Render() and before the next ImGui::NewFrame(), which is also illegal.\n    inline    ImGuiWindow*  GetCurrentWindowRead()      { ImGuiContext& g = *GImGui; return g.CurrentWindow; }\n    inline    ImGuiWindow*  GetCurrentWindow()          { ImGuiContext& g = *GImGui; g.CurrentWindow->WriteAccessed = true; return g.CurrentWindow; }\n    IMGUI_API ImGuiWindow*  FindWindowByID(ImGuiID id);\n    IMGUI_API ImGuiWindow*  FindWindowByName(const char* name);\n    IMGUI_API void          FocusWindow(ImGuiWindow* window);\n    IMGUI_API void          FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window);\n    IMGUI_API void          BringWindowToFocusFront(ImGuiWindow* window);\n    IMGUI_API void          BringWindowToDisplayFront(ImGuiWindow* window);\n    IMGUI_API void          BringWindowToDisplayBack(ImGuiWindow* window);\n    IMGUI_API void          UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window);\n    IMGUI_API ImVec2        CalcWindowExpectedSize(ImGuiWindow* window);\n    IMGUI_API bool          IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent);\n    IMGUI_API bool          IsWindowNavFocusable(ImGuiWindow* window);\n    IMGUI_API void          SetWindowScrollX(ImGuiWindow* window, float new_scroll_x);\n    IMGUI_API void          SetWindowScrollY(ImGuiWindow* window, float new_scroll_y);\n    IMGUI_API float         GetWindowScrollMaxX(ImGuiWindow* window);\n    IMGUI_API float         GetWindowScrollMaxY(ImGuiWindow* window);\n    IMGUI_API ImRect        GetWindowAllowedExtentRect(ImGuiWindow* window);\n    IMGUI_API void          SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond);\n    IMGUI_API void          SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond);\n    IMGUI_API void          SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond);\n\n    IMGUI_API void          SetCurrentFont(ImFont* font);\n    inline ImFont*          GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; }\n    inline ImDrawList*      GetForegroundDrawList(ImGuiWindow* window) { return GetForegroundDrawList(window->Viewport); }\n\n    // Init\n    IMGUI_API void          Initialize(ImGuiContext* context);\n    IMGUI_API void          Shutdown(ImGuiContext* context);    // Since 1.60 this is a _private_ function. You can call DestroyContext() to destroy the context created by CreateContext().\n\n    // NewFrame\n    IMGUI_API void          UpdateHoveredWindowAndCaptureFlags();\n    IMGUI_API void          StartMouseMovingWindow(ImGuiWindow* window);\n    IMGUI_API void          UpdateMouseMovingWindowNewFrame();\n    IMGUI_API void          UpdateMouseMovingWindowEndFrame();\n\n    // Viewports\n    IMGUI_API void                  ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale);\n    IMGUI_API void                  DestroyPlatformWindow(ImGuiViewportP* viewport);\n    IMGUI_API void                  ShowViewportThumbnails();\n\n    // Settings\n    IMGUI_API void                  MarkIniSettingsDirty();\n    IMGUI_API void                  MarkIniSettingsDirty(ImGuiWindow* window);\n    IMGUI_API ImGuiWindowSettings*  CreateNewWindowSettings(const char* name);\n    IMGUI_API ImGuiWindowSettings*  FindWindowSettings(ImGuiID id);\n    IMGUI_API ImGuiWindowSettings*  FindOrCreateWindowSettings(const char* name);\n    IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name);\n\n    // Basic Accessors\n    inline ImGuiID          GetItemID()     { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemId; }\n    inline ImGuiID          GetActiveID()   { ImGuiContext& g = *GImGui; return g.ActiveId; }\n    inline ImGuiID          GetFocusID()    { ImGuiContext& g = *GImGui; return g.NavId; }\n    IMGUI_API void          SetActiveID(ImGuiID id, ImGuiWindow* window);\n    IMGUI_API void          SetFocusID(ImGuiID id, ImGuiWindow* window);\n    IMGUI_API void          ClearActiveID();\n    IMGUI_API ImGuiID       GetHoveredID();\n    IMGUI_API void          SetHoveredID(ImGuiID id);\n    IMGUI_API void          KeepAliveID(ImGuiID id);\n    IMGUI_API void          MarkItemEdited(ImGuiID id);\n    IMGUI_API void          PushOverrideID(ImGuiID id);\n\n    // Basic Helpers for widget code\n    IMGUI_API void          ItemSize(const ImVec2& size, float text_offset_y = 0.0f);\n    IMGUI_API void          ItemSize(const ImRect& bb, float text_offset_y = 0.0f);\n    IMGUI_API bool          ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL);\n    IMGUI_API bool          ItemHoverable(const ImRect& bb, ImGuiID id);\n    IMGUI_API bool          IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged);\n    IMGUI_API bool          FocusableItemRegister(ImGuiWindow* window, ImGuiID id);   // Return true if focus is requested\n    IMGUI_API void          FocusableItemUnregister(ImGuiWindow* window);\n    IMGUI_API float         GetNextItemWidth();\n    IMGUI_API ImVec2        CalcItemSize(ImVec2 size, float default_w, float default_h);\n    IMGUI_API float         CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x);\n    IMGUI_API void          PushMultiItemsWidths(int components, float width_full);\n    IMGUI_API void          PushItemFlag(ImGuiItemFlags option, bool enabled);\n    IMGUI_API void          PopItemFlag();\n    IMGUI_API bool          IsItemToggledSelection();                                           // was the last item selection toggled? (after Selectable(), TreeNode() etc. We only returns toggle _event_ in order to handle clipping correctly)\n    IMGUI_API ImVec2        GetContentRegionMaxScreen();\n\n    // Logging/Capture\n    IMGUI_API void          LogBegin(ImGuiLogType type, int auto_open_depth);   // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name.\n    IMGUI_API void          LogToBuffer(int auto_open_depth = -1);              // Start logging/capturing to internal buffer\n\n    // Popups, Modals, Tooltips\n    IMGUI_API void          OpenPopupEx(ImGuiID id);\n    IMGUI_API void          ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup);\n    IMGUI_API void          ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup);\n    IMGUI_API bool          IsPopupOpen(ImGuiID id); // Test for id within current popup stack level (currently begin-ed into); this doesn't scan the whole popup stack!\n    IMGUI_API bool          BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags);\n    IMGUI_API void          BeginTooltipEx(ImGuiWindowFlags extra_flags, bool override_previous_tooltip = true);\n    IMGUI_API ImGuiWindow*  GetFrontMostPopupModal();\n    IMGUI_API ImVec2        FindBestWindowPosForPopup(ImGuiWindow* window);\n    IMGUI_API ImVec2        FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy = ImGuiPopupPositionPolicy_Default);\n\n    // Navigation\n    IMGUI_API void          NavInitWindow(ImGuiWindow* window, bool force_reinit);\n    IMGUI_API bool          NavMoveRequestButNoResultYet();\n    IMGUI_API void          NavMoveRequestCancel();\n    IMGUI_API void          NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const ImRect& bb_rel, ImGuiNavMoveFlags move_flags);\n    IMGUI_API void          NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags);\n    IMGUI_API float         GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode);\n    IMGUI_API ImVec2        GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor = 0.0f, float fast_factor = 0.0f);\n    IMGUI_API int           CalcTypematicPressedRepeatAmount(float t, float t_prev, float repeat_delay, float repeat_rate);\n    IMGUI_API void          ActivateItem(ImGuiID id);   // Remotely activate a button, checkbox, tree node etc. given its unique ID. activation is queued and processed on the next frame when the item is encountered again.\n    IMGUI_API void          SetNavID(ImGuiID id, int nav_layer);\n    IMGUI_API void          SetNavIDWithRectRel(ImGuiID id, int nav_layer, const ImRect& rect_rel);\n\n    // Inputs\n    inline bool             IsKeyPressedMap(ImGuiKey key, bool repeat = true)           { const int key_index = GImGui->IO.KeyMap[key]; return (key_index >= 0) ? IsKeyPressed(key_index, repeat) : false; }\n    inline bool             IsNavInputDown(ImGuiNavInput n)                             { return GImGui->IO.NavInputs[n] > 0.0f; }\n    inline bool             IsNavInputPressed(ImGuiNavInput n, ImGuiInputReadMode mode) { return GetNavInputAmount(n, mode) > 0.0f; }\n    inline bool             IsNavInputPressedAnyOfTwo(ImGuiNavInput n1, ImGuiNavInput n2, ImGuiInputReadMode mode) { return (GetNavInputAmount(n1, mode) + GetNavInputAmount(n2, mode)) > 0.0f; }\n    \n    // Docking\n    // (some functions are only declared in imgui.cpp, see Docking section)\n    IMGUI_API void          DockContextInitialize(ImGuiContext* ctx);\n    IMGUI_API void          DockContextShutdown(ImGuiContext* ctx);\n    IMGUI_API void          DockContextOnLoadSettings(ImGuiContext* ctx);\n    IMGUI_API void          DockContextRebuild(ImGuiContext* ctx);\n    IMGUI_API void          DockContextUpdateUndocking(ImGuiContext* ctx);\n    IMGUI_API void          DockContextUpdateDocking(ImGuiContext* ctx);\n    IMGUI_API void          DockContextQueueDock(ImGuiContext* ctx, ImGuiWindow* target, ImGuiDockNode* target_node, ImGuiWindow* payload, ImGuiDir split_dir, float split_ratio, bool split_outer);\n    IMGUI_API void          DockContextQueueUndockWindow(ImGuiContext* ctx, ImGuiWindow* window);\n    IMGUI_API void          DockContextQueueUndockNode(ImGuiContext* ctx, ImGuiDockNode* node);\n    IMGUI_API bool          DockContextCalcDropPosForDocking(ImGuiWindow* target, ImGuiDockNode* target_node, ImGuiWindow* payload, ImGuiDir split_dir, bool split_outer, ImVec2* out_pos);\n    inline ImGuiDockNode*   DockNodeGetRootNode(ImGuiDockNode* node) { while (node->ParentNode) node = node->ParentNode; return node; }\n    IMGUI_API void          BeginDocked(ImGuiWindow* window, bool* p_open);\n    IMGUI_API void          BeginAsDockableDragDropSource(ImGuiWindow* window);\n    IMGUI_API void          BeginAsDockableDragDropTarget(ImGuiWindow* window);\n    IMGUI_API void          SetWindowDock(ImGuiWindow* window, ImGuiID dock_id, ImGuiCond cond);\n    IMGUI_API void          ShowDockingDebug();\n\n    // Docking - Builder function needs to be generally called before the DockSpace() node is submitted.\n    IMGUI_API void          DockBuilderDockWindow(const char* window_name, ImGuiID node_id);\n    IMGUI_API ImGuiDockNode*DockBuilderGetNode(ImGuiID node_id);                    // Warning: DO NOT HOLD ON ImGuiDockNode* pointer, will be invalided by any split/merge/remove operation.\n    inline ImGuiDockNode*   DockBuilderGetCentralNode(ImGuiID node_id)              { ImGuiDockNode* node = DockBuilderGetNode(node_id); if (!node) return NULL; return DockNodeGetRootNode(node)->CentralNode; }\n    IMGUI_API ImGuiID       DockBuilderAddNode(ImGuiID node_id, ImGuiDockNodeFlags flags = 0);  // Use (flags == ImGuiDockNodeFlags_DockSpace) to create a dockspace, otherwise it'll create a floating node.\n    IMGUI_API void          DockBuilderRemoveNode(ImGuiID node_id);                 // Remove node and all its child, undock all windows\n    IMGUI_API void          DockBuilderRemoveNodeDockedWindows(ImGuiID node_id, bool clear_persistent_docking_references = true);\n    IMGUI_API void          DockBuilderRemoveNodeChildNodes(ImGuiID node_id);       // Remove all split/hierarchy. All remaining docked windows will be re-docked to the root.\n    IMGUI_API void          DockBuilderSetNodePos(ImGuiID node_id, ImVec2 pos);\n    IMGUI_API void          DockBuilderSetNodeSize(ImGuiID node_id, ImVec2 size);\n    IMGUI_API ImGuiID       DockBuilderSplitNode(ImGuiID node_id, ImGuiDir split_dir, float size_ratio_for_node_at_dir, ImGuiID* out_id_dir, ImGuiID* out_id_other);\n    IMGUI_API void          DockBuilderCopyDockSpace(ImGuiID src_dockspace_id, ImGuiID dst_dockspace_id, ImVector<const char*>* in_window_remap_pairs);\n    IMGUI_API void          DockBuilderCopyNode(ImGuiID src_node_id, ImGuiID dst_node_id, ImVector<ImGuiID>* out_node_remap_pairs);\n    IMGUI_API void          DockBuilderCopyWindowSettings(const char* src_name, const char* dst_name);\n    IMGUI_API void          DockBuilderFinish(ImGuiID node_id);\n\n    // Drag and Drop\n    IMGUI_API bool          BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id);\n    IMGUI_API void          ClearDragDrop();\n    IMGUI_API bool          IsDragDropPayloadBeingAccepted();\n\n    // New Columns API (FIXME-WIP)\n    IMGUI_API void          BeginColumns(const char* str_id, int count, ImGuiColumnsFlags flags = 0); // setup number of columns. use an identifier to distinguish multiple column sets. close with EndColumns().\n    IMGUI_API void          EndColumns();                                                             // close columns\n    IMGUI_API void          PushColumnClipRect(int column_index = -1);\n    IMGUI_API ImGuiID       GetColumnsID(const char* str_id, int count);\n    IMGUI_API ImGuiColumns* FindOrCreateColumns(ImGuiWindow* window, ImGuiID id);\n\n    // Tab Bars\n    IMGUI_API bool          BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& bb, ImGuiTabBarFlags flags, ImGuiDockNode* dock_node);\n    IMGUI_API ImGuiTabItem* TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id);\n    IMGUI_API ImGuiTabItem* TabBarFindMostRecentlySelectedTabForActiveWindow(ImGuiTabBar* tab_bar);\n    IMGUI_API void          TabBarAddTab(ImGuiTabBar* tab_bar, ImGuiTabItemFlags tab_flags, ImGuiWindow* window);\n    IMGUI_API void          TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id);\n    IMGUI_API void          TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab);\n    IMGUI_API void          TabBarQueueChangeTabOrder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir);\n    IMGUI_API bool          TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags, ImGuiWindow* docked_window);\n    IMGUI_API ImVec2        TabItemCalcSize(const char* label, bool has_close_button);\n    IMGUI_API void          TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col);\n    IMGUI_API bool          TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id);\n\n    // Render helpers\n    // AVOID USING OUTSIDE OF IMGUI.CPP! NOT FOR PUBLIC CONSUMPTION. THOSE FUNCTIONS ARE A MESS. THEIR SIGNATURE AND BEHAVIOR WILL CHANGE, THEY NEED TO BE REFACTORED INTO SOMETHING DECENT.\n    // NB: All position are in absolute pixels coordinates (we are never using window coordinates internally)\n    IMGUI_API void          RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, bool hide_text_after_hash = true);\n    IMGUI_API void          RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width);\n    IMGUI_API void          RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0,0), const ImRect* clip_rect = NULL);\n    IMGUI_API void          RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0, 0), const ImRect* clip_rect = NULL);\n    IMGUI_API void          RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f);\n    IMGUI_API void          RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f);\n    IMGUI_API void          RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, int rounding_corners_flags = ~0);\n    IMGUI_API void          RenderArrow(ImVec2 pos, ImGuiDir dir, float scale = 1.0f);\n    IMGUI_API void          RenderBullet(ImVec2 pos);\n    IMGUI_API void          RenderCheckMark(ImVec2 pos, ImU32 col, float sz);\n    IMGUI_API void          RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight\n    IMGUI_API void          RenderMouseCursor(ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor = ImGuiMouseCursor_Arrow);\n    IMGUI_API const char*   FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text.\n    IMGUI_API void          LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end = NULL);\n\n    // Render helpers (those functions don't access any ImGui state!)\n    IMGUI_API void          RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col);\n    IMGUI_API void          RenderArrowDockMenu(ImDrawList* draw_list, ImVec2 p_min, float sz, ImU32 col);\n    IMGUI_API void          RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding);\n    IMGUI_API void          RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect inner, ImU32 col, float rounding);\n    IMGUI_API void          RenderPixelEllipsis(ImDrawList* draw_list, ImVec2 pos, int count, ImU32 col);\n\n    // Widgets\n    IMGUI_API void          TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0);\n    IMGUI_API bool          ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0,0), ImGuiButtonFlags flags = 0);\n    IMGUI_API bool          CloseButton(ImGuiID id, const ImVec2& pos, float radius);\n    IMGUI_API bool          CollapseButton(ImGuiID id, const ImVec2& pos, ImGuiDockNode* dock_node);\n    IMGUI_API bool          ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags);\n    IMGUI_API void          Scrollbar(ImGuiAxis axis);\n    IMGUI_API ImGuiID       GetScrollbarID(ImGuiWindow* window, ImGuiAxis axis);\n    IMGUI_API void          VerticalSeparator();        // Vertical separator, for menu bars (use current line height). Not exposed because it is misleading and it doesn't have an effect on regular layout.\n\n    // Widgets low-level behaviors\n    IMGUI_API bool          ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags = 0);\n    IMGUI_API bool          DragBehavior(ImGuiID id, ImGuiDataType data_type, void* v, float v_speed, const void* v_min, const void* v_max, const char* format, float power, ImGuiDragFlags flags);\n    IMGUI_API bool          SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb);\n    IMGUI_API bool          SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend = 0.0f, float hover_visibility_delay = 0.0f);\n    IMGUI_API bool          TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end = NULL);\n    IMGUI_API bool          TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0);                     // Consume previous SetNextTreeNodeOpened() data, if any. May return true when logging\n    IMGUI_API void          TreePushOverrideID(ImGuiID id);\n\n    // Template functions are instantiated in imgui_widgets.cpp for a finite number of types.\n    // To use them externally (for custom widget) you may need an \"extern template\" statement in your code in order to link to existing instances and silence Clang warnings (see #2036).\n    // e.g. \" extern template IMGUI_API float RoundScalarWithFormatT<float, float>(const char* format, ImGuiDataType data_type, float v); \"\n    template<typename T, typename SIGNED_T, typename FLOAT_T>   IMGUI_API bool  DragBehaviorT(ImGuiDataType data_type, T* v, float v_speed, T v_min, T v_max, const char* format, float power, ImGuiDragFlags flags);\n    template<typename T, typename SIGNED_T, typename FLOAT_T>   IMGUI_API bool  SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, T* v, T v_min, T v_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb);\n    template<typename T, typename FLOAT_T>                      IMGUI_API float SliderCalcRatioFromValueT(ImGuiDataType data_type, T v, T v_min, T v_max, float power, float linear_zero_pos);\n    template<typename T, typename SIGNED_T>                     IMGUI_API T     RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, T v);\n\n    // Data type helpers\n    IMGUI_API const ImGuiDataTypeInfo*  DataTypeGetInfo(ImGuiDataType data_type);\n    IMGUI_API int           DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* data_ptr, const char* format);\n    IMGUI_API void          DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg_1, const void* arg_2);\n    IMGUI_API bool          DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* data_ptr, const char* format);\n\n    // InputText\n    IMGUI_API bool          InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);\n    IMGUI_API bool          TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* data_ptr, const char* format);\n    inline bool             TempInputTextIsActive(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.ActiveId == id && g.TempInputTextId == id); }\n\n    // Color\n    IMGUI_API void          ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags);\n    IMGUI_API void          ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags);\n    IMGUI_API void          ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags);\n\n    // Plot\n    IMGUI_API void          PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size);\n\n    // Shade functions (write over already created vertices)\n    IMGUI_API void          ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1);\n    IMGUI_API void          ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, bool clamp);\n\n} // namespace ImGui\n\n// ImFontAtlas internals\nIMGUI_API bool              ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas);\nIMGUI_API void              ImFontAtlasBuildRegisterDefaultCustomRects(ImFontAtlas* atlas);\nIMGUI_API void              ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent);\nIMGUI_API void              ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque);\nIMGUI_API void              ImFontAtlasBuildFinish(ImFontAtlas* atlas);\nIMGUI_API void              ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor);\nIMGUI_API void              ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride);\n\n// Test engine hooks (imgui-test)\n//#define IMGUI_ENABLE_TEST_ENGINE\n#ifdef IMGUI_ENABLE_TEST_ENGINE\nextern void                 ImGuiTestEngineHook_PreNewFrame(ImGuiContext* ctx);\nextern void                 ImGuiTestEngineHook_PostNewFrame(ImGuiContext* ctx);\nextern void                 ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id);\nextern void                 ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags);\n#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB, _ID)                ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID)               // Register status flags\n#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS)    ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS)   // Register status flags\n#else\n#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB, _ID)                do { } while (0)\n#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS)    do { } while (0)\n#endif\n\n#ifdef __clang__\n#pragma clang diagnostic pop\n#endif\n\n#ifdef _MSC_VER\n#pragma warning (pop)\n#endif\n"
  },
  {
    "path": "src/imgui/imgui_widgets.cpp",
    "content": "// dear imgui, v1.70 WIP\n// (widgets code)\n\n/*\n\nIndex of this file:\n\n// [SECTION] Forward Declarations\n// [SECTION] Widgets: Text, etc.\n// [SECTION] Widgets: Main (Button, Image, Checkbox, RadioButton, ProgressBar, Bullet, etc.)\n// [SECTION] Widgets: Low-level Layout helpers (Spacing, Dummy, NewLine, Separator, etc.)\n// [SECTION] Widgets: ComboBox\n// [SECTION] Data Type and Data Formatting Helpers\n// [SECTION] Widgets: DragScalar, DragFloat, DragInt, etc.\n// [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc.\n// [SECTION] Widgets: InputScalar, InputFloat, InputInt, etc.\n// [SECTION] Widgets: InputText, InputTextMultiline\n// [SECTION] Widgets: ColorEdit, ColorPicker, ColorButton, etc.\n// [SECTION] Widgets: TreeNode, CollapsingHeader, etc.\n// [SECTION] Widgets: Selectable\n// [SECTION] Widgets: ListBox\n// [SECTION] Widgets: PlotLines, PlotHistogram\n// [SECTION] Widgets: Value helpers\n// [SECTION] Widgets: MenuItem, BeginMenu, EndMenu, etc.\n// [SECTION] Widgets: BeginTabBar, EndTabBar, etc.\n// [SECTION] Widgets: BeginTabItem, EndTabItem, etc.\n\n*/\n\n#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)\n#define _CRT_SECURE_NO_WARNINGS\n#endif\n\n#include \"imgui.hpp\"\n#ifndef IMGUI_DEFINE_MATH_OPERATORS\n#define IMGUI_DEFINE_MATH_OPERATORS\n#endif\n#include \"imgui_internal.hpp\"\n\n#include <ctype.h>      // toupper\n#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier\n#include <stddef.h>     // intptr_t\n#else\n#include <stdint.h>     // intptr_t\n#endif\n\n// Visual Studio warnings\n#ifdef _MSC_VER\n#pragma warning (disable: 4127) // condition expression is constant\n#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen\n#endif\n\n// Clang/GCC warnings with -Weverything\n#ifdef __clang__\n#pragma clang diagnostic ignored \"-Wold-style-cast\"         // warning : use of old-style cast                              // yes, they are more terse.\n#pragma clang diagnostic ignored \"-Wfloat-equal\"            // warning : comparing floating point with == or != is unsafe   // storing and comparing against same constants (typically 0.0f) is ok.\n#pragma clang diagnostic ignored \"-Wformat-nonliteral\"      // warning : format string is not a string literal              // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code.\n#pragma clang diagnostic ignored \"-Wsign-conversion\"        // warning : implicit conversion changes signedness             //\n#if __has_warning(\"-Wzero-as-null-pointer-constant\")\n#pragma clang diagnostic ignored \"-Wzero-as-null-pointer-constant\"  // warning : zero as null pointer constant              // some standard header variations use #define NULL 0\n#endif\n#if __has_warning(\"-Wdouble-promotion\")\n#pragma clang diagnostic ignored \"-Wdouble-promotion\"       // warning: implicit conversion from 'float' to 'double' when passing argument to function  // using printf() is a misery with this as C++ va_arg ellipsis changes float to double.\n#endif\n#elif defined(__GNUC__)\n#pragma GCC diagnostic ignored \"-Wformat-nonliteral\"        // warning: format not a string literal, format string not checked\n#if __GNUC__ >= 8\n#pragma GCC diagnostic ignored \"-Wclass-memaccess\"          // warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead\n#endif\n#endif\n\n//-------------------------------------------------------------------------\n// Data\n//-------------------------------------------------------------------------\n\n// Those MIN/MAX values are not define because we need to point to them\nstatic const signed char    IM_S8_MIN  = -128;\nstatic const signed char    IM_S8_MAX  = 127;\nstatic const unsigned char  IM_U8_MIN  = 0;\nstatic const unsigned char  IM_U8_MAX  = 0xFF;\nstatic const signed short   IM_S16_MIN = -32768;\nstatic const signed short   IM_S16_MAX = 32767;\nstatic const unsigned short IM_U16_MIN = 0;\nstatic const unsigned short IM_U16_MAX = 0xFFFF;\nstatic const ImS32          IM_S32_MIN = INT_MIN;    // (-2147483647 - 1), (0x80000000);\nstatic const ImS32          IM_S32_MAX = INT_MAX;    // (2147483647), (0x7FFFFFFF)\nstatic const ImU32          IM_U32_MIN = 0;\nstatic const ImU32          IM_U32_MAX = UINT_MAX;   // (0xFFFFFFFF)\n#ifdef LLONG_MIN\nstatic const ImS64          IM_S64_MIN = LLONG_MIN;  // (-9223372036854775807ll - 1ll);\nstatic const ImS64          IM_S64_MAX = LLONG_MAX;  // (9223372036854775807ll);\n#else\nstatic const ImS64          IM_S64_MIN = -9223372036854775807LL - 1;\nstatic const ImS64          IM_S64_MAX = 9223372036854775807LL;\n#endif\nstatic const ImU64          IM_U64_MIN = 0;\n#ifdef ULLONG_MAX\nstatic const ImU64          IM_U64_MAX = ULLONG_MAX; // (0xFFFFFFFFFFFFFFFFull);\n#else\nstatic const ImU64          IM_U64_MAX = (2ULL * 9223372036854775807LL + 1);\n#endif\n\n//-------------------------------------------------------------------------\n// [SECTION] Forward Declarations\n//-------------------------------------------------------------------------\n\n// For InputTextEx()\nstatic bool             InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data);\nstatic int              InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end);\nstatic ImVec2           InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false);\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: Text, etc.\n//-------------------------------------------------------------------------\n// - TextUnformatted()\n// - Text()\n// - TextV()\n// - TextColored()\n// - TextColoredV()\n// - TextDisabled()\n// - TextDisabledV()\n// - TextWrapped()\n// - TextWrappedV()\n// - LabelText()\n// - LabelTextV()\n// - BulletText()\n// - BulletTextV()\n//-------------------------------------------------------------------------\n\nvoid ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(text != NULL);\n    const char* text_begin = text;\n    if (text_end == NULL)\n        text_end = text + strlen(text); // FIXME-OPT\n\n    const ImVec2 text_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrentLineTextBaseOffset);\n    const float wrap_pos_x = window->DC.TextWrapPos;\n    const bool wrap_enabled = (wrap_pos_x >= 0.0f);\n    if (text_end - text > 2000 && !wrap_enabled)\n    {\n        // Long text!\n        // Perform manual coarse clipping to optimize for long multi-line text\n        // - From this point we will only compute the width of lines that are visible. Optimization only available when word-wrapping is disabled.\n        // - We also don't vertically center the text within the line full height, which is unlikely to matter because we are likely the biggest and only item on the line.\n        // - We use memchr(), pay attention that well optimized versions of those str/mem functions are much faster than a casually written loop.\n        const char* line = text;\n        const float line_height = GetTextLineHeight();\n        ImVec2 text_size(0,0);\n\n        // Lines to skip (can't skip when logging text)\n        ImVec2 pos = text_pos;\n        if (!g.LogEnabled)\n        {\n            int lines_skippable = (int)((window->ClipRect.Min.y - text_pos.y) / line_height);\n            if (lines_skippable > 0)\n            {\n                int lines_skipped = 0;\n                while (line < text_end && lines_skipped < lines_skippable)\n                {\n                    const char* line_end = (const char*)memchr(line, '\\n', text_end - line);\n                    if (!line_end)\n                        line_end = text_end;\n                    if ((flags & ImGuiTextFlags_NoWidthForLargeClippedText) == 0)\n                        text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x);\n                    line = line_end + 1;\n                    lines_skipped++;\n                }\n                pos.y += lines_skipped * line_height;\n            }\n        }\n\n        // Lines to render\n        if (line < text_end)\n        {\n            ImRect line_rect(pos, pos + ImVec2(FLT_MAX, line_height));\n            while (line < text_end)\n            {\n                if (IsClippedEx(line_rect, 0, false))\n                    break;\n\n                const char* line_end = (const char*)memchr(line, '\\n', text_end - line);\n                if (!line_end)\n                    line_end = text_end;\n                text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x);\n                RenderText(pos, line, line_end, false);\n                line = line_end + 1;\n                line_rect.Min.y += line_height;\n                line_rect.Max.y += line_height;\n                pos.y += line_height;\n            }\n\n            // Count remaining lines\n            int lines_skipped = 0;\n            while (line < text_end)\n            {\n                const char* line_end = (const char*)memchr(line, '\\n', text_end - line);\n                if (!line_end)\n                    line_end = text_end;\n                if ((flags & ImGuiTextFlags_NoWidthForLargeClippedText) == 0)\n                    text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x);\n                line = line_end + 1;\n                lines_skipped++;\n            }\n            pos.y += lines_skipped * line_height;\n        }\n        text_size.y = (pos - text_pos).y;\n\n        ImRect bb(text_pos, text_pos + text_size);\n        ItemSize(text_size);\n        ItemAdd(bb, 0);\n    }\n    else\n    {\n        const float wrap_width = wrap_enabled ? CalcWrapWidthForPos(window->DC.CursorPos, wrap_pos_x) : 0.0f;\n        const ImVec2 text_size = CalcTextSize(text_begin, text_end, false, wrap_width);\n\n        ImRect bb(text_pos, text_pos + text_size);\n        ItemSize(text_size);\n        if (!ItemAdd(bb, 0))\n            return;\n\n        // Render (we don't hide text after ## in this end-user function)\n        RenderTextWrapped(bb.Min, text_begin, text_end, wrap_width);\n    }\n}\n\nvoid ImGui::TextUnformatted(const char* text, const char* text_end)\n{\n    TextEx(text, text_end, ImGuiTextFlags_NoWidthForLargeClippedText);\n}\n\nvoid ImGui::Text(const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    TextV(fmt, args);\n    va_end(args);\n}\n\nvoid ImGui::TextV(const char* fmt, va_list args)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    const char* text_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);\n    TextEx(g.TempBuffer, text_end, ImGuiTextFlags_NoWidthForLargeClippedText);\n}\n\nvoid ImGui::TextColored(const ImVec4& col, const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    TextColoredV(col, fmt, args);\n    va_end(args);\n}\n\nvoid ImGui::TextColoredV(const ImVec4& col, const char* fmt, va_list args)\n{\n    PushStyleColor(ImGuiCol_Text, col);\n    TextV(fmt, args);\n    PopStyleColor();\n}\n\nvoid ImGui::TextDisabled(const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    TextDisabledV(fmt, args);\n    va_end(args);\n}\n\nvoid ImGui::TextDisabledV(const char* fmt, va_list args)\n{\n    PushStyleColor(ImGuiCol_Text, GImGui->Style.Colors[ImGuiCol_TextDisabled]);\n    TextV(fmt, args);\n    PopStyleColor();\n}\n\nvoid ImGui::TextWrapped(const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    TextWrappedV(fmt, args);\n    va_end(args);\n}\n\nvoid ImGui::TextWrappedV(const char* fmt, va_list args)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    bool need_backup = (window->DC.TextWrapPos < 0.0f);  // Keep existing wrap position if one is already set\n    if (need_backup)\n        PushTextWrapPos(0.0f);\n    TextV(fmt, args);\n    if (need_backup)\n        PopTextWrapPos();\n}\n\nvoid ImGui::LabelText(const char* label, const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    LabelTextV(label, fmt, args);\n    va_end(args);\n}\n\n// Add a label+text combo aligned to other label+value widgets\nvoid ImGui::LabelTextV(const char* label, const char* fmt, va_list args)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const float w = GetNextItemWidth();\n\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n    const ImRect value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2));\n    const ImRect total_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + (label_size.x > 0.0f ? style.ItemInnerSpacing.x : 0.0f), style.FramePadding.y*2) + label_size);\n    ItemSize(total_bb, style.FramePadding.y);\n    if (!ItemAdd(total_bb, 0))\n        return;\n\n    // Render\n    const char* value_text_begin = &g.TempBuffer[0];\n    const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);\n    RenderTextClipped(value_bb.Min, value_bb.Max, value_text_begin, value_text_end, NULL, ImVec2(0.0f,0.5f));\n    if (label_size.x > 0.0f)\n        RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y + style.FramePadding.y), label);\n}\n\nvoid ImGui::BulletText(const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    BulletTextV(fmt, args);\n    va_end(args);\n}\n\n// Text with a little bullet aligned to the typical tree node.\nvoid ImGui::BulletTextV(const char* fmt, va_list args)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n\n    const char* text_begin = g.TempBuffer;\n    const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);\n    const ImVec2 label_size = CalcTextSize(text_begin, text_end, false);\n    const float text_base_offset_y = ImMax(0.0f, window->DC.CurrentLineTextBaseOffset); // Latch before ItemSize changes it\n    const float line_height = ImMax(ImMin(window->DC.CurrentLineSize.y, g.FontSize + g.Style.FramePadding.y*2), g.FontSize);\n    const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize + (label_size.x > 0.0f ? (label_size.x + style.FramePadding.x*2) : 0.0f), ImMax(line_height, label_size.y)));  // Empty text doesn't add padding\n    ItemSize(bb);\n    if (!ItemAdd(bb, 0))\n        return;\n\n    // Render\n    RenderBullet(bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f));\n    RenderText(bb.Min+ImVec2(g.FontSize + style.FramePadding.x*2, text_base_offset_y), text_begin, text_end, false);\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: Main\n//-------------------------------------------------------------------------\n// - ButtonBehavior() [Internal]\n// - Button()\n// - SmallButton()\n// - InvisibleButton()\n// - ArrowButton()\n// - CloseButton() [Internal]\n// - CollapseButton() [Internal]\n// - Scrollbar() [Internal]\n// - Image()\n// - ImageButton()\n// - Checkbox()\n// - CheckboxFlags()\n// - RadioButton()\n// - ProgressBar()\n// - Bullet()\n//-------------------------------------------------------------------------\n\n// The ButtonBehavior() function is key to many interactions and used by many/most widgets.\n// Because we handle so many cases (keyboard/gamepad navigation, drag and drop) and many specific behavior (via ImGuiButtonFlags_),\n// this code is a little complex.\n// By far the most common path is interacting with the Mouse using the default ImGuiButtonFlags_PressedOnClickRelease button behavior.\n// See the series of events below and the corresponding state reported by dear imgui:\n//------------------------------------------------------------------------------------------------------------------------------------------------\n// with PressedOnClickRelease:             return-value  IsItemHovered()  IsItemActive()  IsItemActivated()  IsItemDeactivated()  IsItemClicked()\n//   Frame N+0 (mouse is outside bb)        -             -                -               -                  -                    -    \n//   Frame N+1 (mouse moves inside bb)      -             true             -               -                  -                    -    \n//   Frame N+2 (mouse button is down)       -             true             true            true               -                    true\n//   Frame N+3 (mouse button is down)       -             true             true            -                  -                    -    \n//   Frame N+4 (mouse moves outside bb)     -             -                true            -                  -                    -\n//   Frame N+5 (mouse moves inside bb)      -             true             true            -                  -                    -\n//   Frame N+6 (mouse button is released)   true          true             -               -                  true                 -    \n//   Frame N+7 (mouse button is released)   -             true             -               -                  -                    -    \n//   Frame N+8 (mouse moves outside bb)     -             -                -               -                  -                    -    \n//------------------------------------------------------------------------------------------------------------------------------------------------\n// with PressedOnClick:                    return-value  IsItemHovered()  IsItemActive()  IsItemActivated()  IsItemDeactivated()  IsItemClicked()\n//   Frame N+2 (mouse button is down)       true          true             true            true               -                    true\n//   Frame N+3 (mouse button is down)       -             true             true            -                  -                    -    \n//   Frame N+6 (mouse button is released)   -             true             -               -                  true                 -    \n//   Frame N+7 (mouse button is released)   -             true             -               -                  -                    -    \n//------------------------------------------------------------------------------------------------------------------------------------------------\n// with PressedOnRelease:                  return-value  IsItemHovered()  IsItemActive()  IsItemActivated()  IsItemDeactivated()  IsItemClicked()\n//   Frame N+2 (mouse button is down)       -             true             -               -                  -                    true\n//   Frame N+3 (mouse button is down)       -             true             -               -                  -                    -    \n//   Frame N+6 (mouse button is released)   true          true             -               -                  -                    -\n//   Frame N+7 (mouse button is released)   -             true             -               -                  -                    -    \n//------------------------------------------------------------------------------------------------------------------------------------------------\n// with PressedOnDoubleClick:              return-value  IsItemHovered()  IsItemActive()  IsItemActivated()  IsItemDeactivated()  IsItemClicked()\n//   Frame N+0 (mouse button is down)       -             true             -               -                  -                    true\n//   Frame N+1 (mouse button is down)       -             true             -               -                  -                    -    \n//   Frame N+2 (mouse button is released)   -             true             -               -                  -                    -\n//   Frame N+3 (mouse button is released)   -             true             -               -                  -                    -    \n//   Frame N+4 (mouse button is down)       true          true             true            true               -                    true\n//   Frame N+5 (mouse button is down)       -             true             true            -                  -                    -    \n//   Frame N+6 (mouse button is released)   -             true             -               -                  true                 -\n//   Frame N+7 (mouse button is released)   -             true             -               -                  -                    -    \n//------------------------------------------------------------------------------------------------------------------------------------------------\n// The behavior of the return-value changes when ImGuiButtonFlags_Repeat is set:\n//                                         Repeat+                  Repeat+           Repeat+             Repeat+\n//                                         PressedOnClickRelease    PressedOnClick    PressedOnRelease    PressedOnDoubleClick\n//-------------------------------------------------------------------------------------------------------------------------------------------------\n//   Frame N+0 (mouse button is down)       -                        true              -                   true \n//   ...                                    -                        -                 -                   -\n//   Frame N + RepeatDelay                  true                     true              -                   true\n//   ...                                    -                        -                 -                   -\n//   Frame N + RepeatDelay + RepeatRate*N   true                     true              -                   true\n//-------------------------------------------------------------------------------------------------------------------------------------------------\n\nbool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = GetCurrentWindow();\n\n    if (flags & ImGuiButtonFlags_Disabled)\n    {\n        if (out_hovered) *out_hovered = false;\n        if (out_held) *out_held = false;\n        if (g.ActiveId == id) ClearActiveID();\n        return false;\n    }\n\n    // Default behavior requires click+release on same spot\n    if ((flags & (ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnRelease | ImGuiButtonFlags_PressedOnDoubleClick)) == 0)\n        flags |= ImGuiButtonFlags_PressedOnClickRelease;\n\n    ImGuiWindow* backup_hovered_window = g.HoveredWindow;\n    const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredRootWindow == window->RootWindow;\n    if (flatten_hovered_children)\n        g.HoveredWindow = window;\n\n#ifdef IMGUI_ENABLE_TEST_ENGINE\n    if (id != 0 && window->DC.LastItemId != id)\n        ImGuiTestEngineHook_ItemAdd(&g, bb, id);\n#endif\n\n    bool pressed = false;\n    bool hovered = ItemHoverable(bb, id);\n\n    // Drag source doesn't report as hovered\n    if (hovered && g.DragDropActive && g.DragDropPayload.SourceId == id && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoDisableHover))\n        hovered = false;\n\n    // Special mode for Drag and Drop where holding button pressed for a long time while dragging another item triggers the button\n    if (g.DragDropActive && (flags & ImGuiButtonFlags_PressedOnDragDropHold) && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoHoldToOpenOthers))\n        if (IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))\n        {\n            hovered = true;\n            SetHoveredID(id);\n            if (CalcTypematicPressedRepeatAmount(g.HoveredIdTimer + 0.0001f, g.HoveredIdTimer + 0.0001f - g.IO.DeltaTime, 0.01f, 0.70f)) // FIXME: Our formula for CalcTypematicPressedRepeatAmount() is fishy\n            {\n                pressed = true;\n                FocusWindow(window);\n            }\n        }\n\n    if (flatten_hovered_children)\n        g.HoveredWindow = backup_hovered_window;\n\n    // AllowOverlap mode (rarely used) requires previous frame HoveredId to be null or to match. This allows using patterns where a later submitted widget overlaps a previous one.\n    if (hovered && (flags & ImGuiButtonFlags_AllowItemOverlap) && (g.HoveredIdPreviousFrame != id && g.HoveredIdPreviousFrame != 0))\n        hovered = false;\n\n    // Mouse\n    if (hovered)\n    {\n        if (!(flags & ImGuiButtonFlags_NoKeyModifiers) || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt))\n        {\n            if ((flags & ImGuiButtonFlags_PressedOnClickRelease) && g.IO.MouseClicked[0])\n            {\n                SetActiveID(id, window);\n                if (!(flags & ImGuiButtonFlags_NoNavFocus))\n                    SetFocusID(id, window);\n                FocusWindow(window);\n            }\n            if (((flags & ImGuiButtonFlags_PressedOnClick) && g.IO.MouseClicked[0]) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDoubleClicked[0]))\n            {\n                pressed = true;\n                if (flags & ImGuiButtonFlags_NoHoldingActiveID)\n                    ClearActiveID();\n                else\n                    SetActiveID(id, window); // Hold on ID\n                FocusWindow(window);\n            }\n            if ((flags & ImGuiButtonFlags_PressedOnRelease) && g.IO.MouseReleased[0])\n            {\n                if (!((flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[0] >= g.IO.KeyRepeatDelay))  // Repeat mode trumps <on release>\n                    pressed = true;\n                ClearActiveID();\n            }\n\n            // 'Repeat' mode acts when held regardless of _PressedOn flags (see table above).\n            // Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings.\n            if ((flags & ImGuiButtonFlags_Repeat) && g.ActiveId == id && g.IO.MouseDownDuration[0] > 0.0f && IsMouseClicked(0, true))\n                pressed = true;\n        }\n\n        if (pressed)\n            g.NavDisableHighlight = true;\n    }\n\n    // Gamepad/Keyboard navigation\n    // We report navigated item as hovered but we don't set g.HoveredId to not interfere with mouse.\n    if (g.NavId == id && !g.NavDisableHighlight && g.NavDisableMouseHover && (g.ActiveId == 0 || g.ActiveId == id || g.ActiveId == window->MoveId))\n        if (!(flags & ImGuiButtonFlags_NoHoveredOnNav))\n            hovered = true;\n\n    if (g.NavActivateDownId == id)\n    {\n        bool nav_activated_by_code = (g.NavActivateId == id);\n        bool nav_activated_by_inputs = IsNavInputPressed(ImGuiNavInput_Activate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed);\n        if (nav_activated_by_code || nav_activated_by_inputs)\n            pressed = true;\n        if (nav_activated_by_code || nav_activated_by_inputs || g.ActiveId == id)\n        {\n            // Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button.\n            g.NavActivateId = id; // This is so SetActiveId assign a Nav source\n            SetActiveID(id, window);\n            if ((nav_activated_by_code || nav_activated_by_inputs) && !(flags & ImGuiButtonFlags_NoNavFocus))\n                SetFocusID(id, window);\n            g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right) | (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down);\n        }\n    }\n\n    bool held = false;\n    if (g.ActiveId == id)\n    {\n        if (pressed)\n            g.ActiveIdHasBeenPressed = true;\n        if (g.ActiveIdSource == ImGuiInputSource_Mouse)\n        {\n            if (g.ActiveIdIsJustActivated)\n                g.ActiveIdClickOffset = g.IO.MousePos - bb.Min;\n            if (g.IO.MouseDown[0])\n            {\n                held = true;\n            }\n            else\n            {\n                if (hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease))\n                    if (!((flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[0] >= g.IO.KeyRepeatDelay))  // Repeat mode trumps <on release>\n                        if (!g.DragDropActive)\n                            pressed = true;\n                ClearActiveID();\n            }\n            if (!(flags & ImGuiButtonFlags_NoNavFocus))\n                g.NavDisableHighlight = true;\n        }\n        else if (g.ActiveIdSource == ImGuiInputSource_Nav)\n        {\n            if (g.NavActivateDownId != id)\n                ClearActiveID();\n        }\n    }\n\n    if (out_hovered) *out_hovered = hovered;\n    if (out_held) *out_held = held;\n\n    return pressed;\n}\n\nbool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = window->GetID(label);\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n\n    ImVec2 pos = window->DC.CursorPos;\n    if ((flags & ImGuiButtonFlags_AlignTextBaseLine) && style.FramePadding.y < window->DC.CurrentLineTextBaseOffset) // Try to vertically align buttons that are smaller/have no padding so that text baseline matches (bit hacky, since it shouldn't be a flag)\n        pos.y += window->DC.CurrentLineTextBaseOffset - style.FramePadding.y;\n    ImVec2 size = CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f);\n\n    const ImRect bb(pos, pos + size);\n    ItemSize(size, style.FramePadding.y);\n    if (!ItemAdd(bb, id))\n        return false;\n\n    if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat)\n        flags |= ImGuiButtonFlags_Repeat;\n    bool hovered, held;\n    bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags);\n    if (pressed)\n        MarkItemEdited(id);\n\n    // Render\n    const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);\n    RenderNavHighlight(bb, id);\n    RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding);\n    RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, label, NULL, &label_size, style.ButtonTextAlign, &bb);\n\n    // Automatically close popups\n    //if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))\n    //    CloseCurrentPopup();\n\n    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags);\n    return pressed;\n}\n\nbool ImGui::Button(const char* label, const ImVec2& size_arg)\n{\n    return ButtonEx(label, size_arg, 0);\n}\n\n// Small buttons fits within text without additional vertical spacing.\nbool ImGui::SmallButton(const char* label)\n{\n    ImGuiContext& g = *GImGui;\n    float backup_padding_y = g.Style.FramePadding.y;\n    g.Style.FramePadding.y = 0.0f;\n    bool pressed = ButtonEx(label, ImVec2(0, 0), ImGuiButtonFlags_AlignTextBaseLine);\n    g.Style.FramePadding.y = backup_padding_y;\n    return pressed;\n}\n\n// Tip: use ImGui::PushID()/PopID() to push indices or pointers in the ID stack.\n// Then you can keep 'str_id' empty or the same for all your buttons (instead of creating a string based on a non-string id)\nbool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    // Cannot use zero-size for InvisibleButton(). Unlike Button() there is not way to fallback using the label size.\n    IM_ASSERT(size_arg.x != 0.0f && size_arg.y != 0.0f);\n\n    const ImGuiID id = window->GetID(str_id);\n    ImVec2 size = CalcItemSize(size_arg, 0.0f, 0.0f);\n    const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);\n    ItemSize(size);\n    if (!ItemAdd(bb, id))\n        return false;\n\n    bool hovered, held;\n    bool pressed = ButtonBehavior(bb, id, &hovered, &held);\n\n    return pressed;\n}\n\nbool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiButtonFlags flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiID id = window->GetID(str_id);\n    const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);\n    const float default_size = GetFrameHeight();\n    ItemSize(size, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f);\n    if (!ItemAdd(bb, id))\n        return false;\n\n    if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat)\n        flags |= ImGuiButtonFlags_Repeat;\n\n    bool hovered, held;\n    bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags);\n\n    // Render\n    const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);\n    RenderNavHighlight(bb, id);\n    RenderFrame(bb.Min, bb.Max, col, true, g.Style.FrameRounding);\n    RenderArrow(bb.Min + ImVec2(ImMax(0.0f, (size.x - g.FontSize) * 0.5f), ImMax(0.0f, (size.y - g.FontSize) * 0.5f)), dir);\n\n    return pressed;\n}\n\nbool ImGui::ArrowButton(const char* str_id, ImGuiDir dir)\n{\n    float sz = GetFrameHeight();\n    return ArrowButtonEx(str_id, dir, ImVec2(sz, sz), 0);\n}\n\n// Button to close a window\nbool ImGui::CloseButton(ImGuiID id, const ImVec2& pos, float radius)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    // We intentionally allow interaction when clipped so that a mechanical Alt,Right,Validate sequence close a window.\n    // (this isn't the regular behavior of buttons, but it doesn't affect the user much because navigation tends to keep items visible).\n    const ImRect bb(pos - ImVec2(radius,radius), pos + ImVec2(radius,radius));\n    bool is_clipped = !ItemAdd(bb, id);\n\n    bool hovered, held;\n    bool pressed = ButtonBehavior(bb, id, &hovered, &held);\n    if (is_clipped)\n        return pressed;\n\n    // Render\n    ImVec2 center = bb.GetCenter();\n    if (hovered)\n        window->DrawList->AddCircleFilled(center, ImMax(2.0f, radius), GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered), 9);\n\n    float cross_extent = (radius * 0.7071f) - 1.0f;\n    ImU32 cross_col = GetColorU32(ImGuiCol_Text);\n    center -= ImVec2(0.5f, 0.5f);\n    window->DrawList->AddLine(center + ImVec2(+cross_extent,+cross_extent), center + ImVec2(-cross_extent,-cross_extent), cross_col, 1.0f);\n    window->DrawList->AddLine(center + ImVec2(+cross_extent,-cross_extent), center + ImVec2(-cross_extent,+cross_extent), cross_col, 1.0f);\n\n    return pressed;\n}\n\n// The Collapse button also functions as a Dock Menu button.\nbool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos, ImGuiDockNode* dock_node)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f);\n    ItemAdd(bb, id);\n    bool hovered, held;\n    bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_None);\n\n    //bool is_dock_menu = (window->DockNodeAsHost && !window->Collapsed);\n    ImVec2 off = dock_node ? ImVec2((float)(int)(-g.Style.ItemInnerSpacing.x * 0.5f) + 0.5f, 0.0f) : ImVec2(0.0f, 0.0f);\n    ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);\n    if (hovered || held)\n        window->DrawList->AddCircleFilled(bb.GetCenter() + off + ImVec2(0,-0.5f), g.FontSize * 0.5f + 1.0f, col, 9);\n\n    if (dock_node)\n        RenderArrowDockMenu(window->DrawList, bb.Min + g.Style.FramePadding, g.FontSize, GetColorU32(ImGuiCol_Text));\n    else\n        RenderArrow(bb.Min + g.Style.FramePadding, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f);\n\n    // Switch to moving the window after mouse is moved beyond the initial drag threshold\n    if (IsItemActive() && IsMouseDragging(0))\n    {\n        bool can_extract_dock_node = false;\n        if (dock_node != NULL && dock_node->VisibleWindow && !(dock_node->VisibleWindow->Flags & ImGuiWindowFlags_NoMove))\n        {\n            ImGuiDockNode* root_node = DockNodeGetRootNode(dock_node);\n            if (root_node->OnlyNodeWithWindows != dock_node || (root_node->CentralNode != NULL))\n                can_extract_dock_node = true;\n        }\n        if (can_extract_dock_node)\n        {\n            float threshold_base = g.FontSize;\n            float threshold_x = (threshold_base * 2.2f);\n            float threshold_y = (threshold_base * 1.5f);\n            IM_ASSERT(window->DockNodeAsHost != NULL);\n            if (g.IO.MouseDragMaxDistanceAbs[0].x > threshold_x || g.IO.MouseDragMaxDistanceAbs[0].y > threshold_y)\n                DockContextQueueUndockNode(&g, dock_node);\n        }\n        else\n        {\n            ImVec2 backup_active_click_offset = g.ActiveIdClickOffset;\n            StartMouseMovingWindow(window);\n            g.ActiveIdClickOffset = backup_active_click_offset;\n        }\n    }\n\n    return pressed;\n}\n\nImGuiID ImGui::GetScrollbarID(ImGuiWindow* window, ImGuiAxis axis)\n{\n    return window->GetIDNoKeepAlive(axis == ImGuiAxis_X ? \"#SCROLLX\" : \"#SCROLLY\");\n}\n\n// Vertical/Horizontal scrollbar\n// The entire piece of code below is rather confusing because:\n// - We handle absolute seeking (when first clicking outside the grab) and relative manipulation (afterward or when clicking inside the grab)\n// - We store values as normalized ratio and in a form that allows the window content to change while we are holding on a scrollbar\n// - We handle both horizontal and vertical scrollbars, which makes the terminology not ideal.\nvoid ImGui::Scrollbar(ImGuiAxis axis)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    const bool horizontal = (axis == ImGuiAxis_X);\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = GetScrollbarID(window, axis);\n    KeepAliveID(id);\n\n    // Render background\n    bool other_scrollbar = (horizontal ? window->ScrollbarY : window->ScrollbarX);\n    float other_scrollbar_size_w = other_scrollbar ? style.ScrollbarSize : 0.0f;\n    const ImRect host_rect = window->Rect();\n    const float border_size = window->WindowBorderSize;\n    ImRect bb = horizontal\n        ? ImRect(host_rect.Min.x + border_size, host_rect.Max.y - style.ScrollbarSize, host_rect.Max.x - other_scrollbar_size_w - border_size, host_rect.Max.y - border_size)\n        : ImRect(host_rect.Max.x - style.ScrollbarSize, host_rect.Min.y + border_size, host_rect.Max.x - border_size, host_rect.Max.y - other_scrollbar_size_w - border_size);\n    bb.Min.x = ImMax(host_rect.Min.x, bb.Min.x); // Handle case where the host rectangle is smaller than the scrollbar\n    bb.Min.y = ImMax(host_rect.Min.y, bb.Min.y);\n    if (!horizontal)\n        bb.Min.y += window->TitleBarHeight() + ((window->Flags & ImGuiWindowFlags_MenuBar) ? window->MenuBarHeight() : 0.0f); // FIXME: InnerRect?\n\n    const float bb_width = bb.GetWidth();\n    const float bb_height = bb.GetHeight();\n    if (bb_width <= 0.0f || bb_height <= 0.0f)\n        return;\n\n    // When we are too small, start hiding and disabling the grab (this reduce visual noise on very small window and facilitate using the resize grab)\n    float alpha = 1.0f;\n    if ((axis == ImGuiAxis_Y) && bb_height < g.FontSize + g.Style.FramePadding.y * 2.0f)\n    {\n        alpha = ImSaturate((bb_height - g.FontSize) / (g.Style.FramePadding.y * 2.0f));\n        if (alpha <= 0.0f)\n            return;\n    }\n    const bool allow_interaction = (alpha >= 1.0f);\n\n    int window_rounding_corners;\n    if (horizontal)\n        window_rounding_corners = ImDrawCornerFlags_BotLeft | (other_scrollbar ? 0 : ImDrawCornerFlags_BotRight);\n    else\n        window_rounding_corners = (((window->Flags & ImGuiWindowFlags_NoTitleBar) && !(window->Flags & ImGuiWindowFlags_MenuBar)) ? ImDrawCornerFlags_TopRight : 0) | (other_scrollbar ? 0 : ImDrawCornerFlags_BotRight);\n    window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_ScrollbarBg), window->WindowRounding, window_rounding_corners);\n    bb.Expand(ImVec2(-ImClamp((float)(int)((bb_width - 2.0f) * 0.5f), 0.0f, 3.0f), -ImClamp((float)(int)((bb_height - 2.0f) * 0.5f), 0.0f, 3.0f)));\n\n    // V denote the main, longer axis of the scrollbar (= height for a vertical scrollbar)\n    float scrollbar_size_v = horizontal ? bb.GetWidth() : bb.GetHeight();\n    float scroll_v = horizontal ? window->Scroll.x : window->Scroll.y;\n    float win_size_avail_v = (horizontal ? window->SizeFull.x : window->SizeFull.y) - other_scrollbar_size_w;\n    float win_size_contents_v = horizontal ? window->SizeContents.x : window->SizeContents.y;\n\n    // Calculate the height of our grabbable box. It generally represent the amount visible (vs the total scrollable amount)\n    // But we maintain a minimum size in pixel to allow for the user to still aim inside.\n    IM_ASSERT(ImMax(win_size_contents_v, win_size_avail_v) > 0.0f); // Adding this assert to check if the ImMax(XXX,1.0f) is still needed. PLEASE CONTACT ME if this triggers.\n    const float win_size_v = ImMax(ImMax(win_size_contents_v, win_size_avail_v), 1.0f);\n    const float grab_h_pixels = ImClamp(scrollbar_size_v * (win_size_avail_v / win_size_v), style.GrabMinSize, scrollbar_size_v);\n    const float grab_h_norm = grab_h_pixels / scrollbar_size_v;\n\n    // Handle input right away. None of the code of Begin() is relying on scrolling position before calling Scrollbar().\n    bool held = false;\n    bool hovered = false;\n    const bool previously_held = (g.ActiveId == id);\n    ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_NoNavFocus);\n\n    float scroll_max = ImMax(1.0f, win_size_contents_v - win_size_avail_v);\n    float scroll_ratio = ImSaturate(scroll_v / scroll_max);\n    float grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v;\n    if (held && allow_interaction && grab_h_norm < 1.0f)\n    {\n        float scrollbar_pos_v = horizontal ? bb.Min.x : bb.Min.y;\n        float mouse_pos_v = horizontal ? g.IO.MousePos.x : g.IO.MousePos.y;\n        float* click_delta_to_grab_center_v = horizontal ? &g.ScrollbarClickDeltaToGrabCenter.x : &g.ScrollbarClickDeltaToGrabCenter.y;\n\n        // Click position in scrollbar normalized space (0.0f->1.0f)\n        const float clicked_v_norm = ImSaturate((mouse_pos_v - scrollbar_pos_v) / scrollbar_size_v);\n        SetHoveredID(id);\n\n        bool seek_absolute = false;\n        if (!previously_held)\n        {\n            // On initial click calculate the distance between mouse and the center of the grab\n            if (clicked_v_norm >= grab_v_norm && clicked_v_norm <= grab_v_norm + grab_h_norm)\n            {\n                *click_delta_to_grab_center_v = clicked_v_norm - grab_v_norm - grab_h_norm*0.5f;\n            }\n            else\n            {\n                seek_absolute = true;\n                *click_delta_to_grab_center_v = 0.0f;\n            }\n        }\n\n        // Apply scroll\n        // It is ok to modify Scroll here because we are being called in Begin() after the calculation of SizeContents and before setting up our starting position\n        const float scroll_v_norm = ImSaturate((clicked_v_norm - *click_delta_to_grab_center_v - grab_h_norm*0.5f) / (1.0f - grab_h_norm));\n        scroll_v = (float)(int)(0.5f + scroll_v_norm * scroll_max);//(win_size_contents_v - win_size_v));\n        if (horizontal)\n            window->Scroll.x = scroll_v;\n        else\n            window->Scroll.y = scroll_v;\n\n        // Update values for rendering\n        scroll_ratio = ImSaturate(scroll_v / scroll_max);\n        grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v;\n\n        // Update distance to grab now that we have seeked and saturated\n        if (seek_absolute)\n            *click_delta_to_grab_center_v = clicked_v_norm - grab_v_norm - grab_h_norm*0.5f;\n    }\n\n    // Render grab\n    const ImU32 grab_col = GetColorU32(held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab, alpha);\n    ImRect grab_rect;\n    if (horizontal)\n        grab_rect = ImRect(ImLerp(bb.Min.x, bb.Max.x, grab_v_norm), bb.Min.y, ImMin(ImLerp(bb.Min.x, bb.Max.x, grab_v_norm) + grab_h_pixels, host_rect.Max.x), bb.Max.y);\n    else\n        grab_rect = ImRect(bb.Min.x, ImLerp(bb.Min.y, bb.Max.y, grab_v_norm), bb.Max.x, ImMin(ImLerp(bb.Min.y, bb.Max.y, grab_v_norm) + grab_h_pixels, host_rect.Max.y));\n    window->DrawList->AddRectFilled(grab_rect.Min, grab_rect.Max, grab_col, style.ScrollbarRounding);\n}\n\nvoid ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);\n    if (border_col.w > 0.0f)\n        bb.Max += ImVec2(2, 2);\n    ItemSize(bb);\n    if (!ItemAdd(bb, 0))\n        return;\n\n    if (border_col.w > 0.0f)\n    {\n        window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(border_col), 0.0f);\n        window->DrawList->AddImage(user_texture_id, bb.Min + ImVec2(1, 1), bb.Max - ImVec2(1, 1), uv0, uv1, GetColorU32(tint_col));\n    }\n    else\n    {\n        window->DrawList->AddImage(user_texture_id, bb.Min, bb.Max, uv0, uv1, GetColorU32(tint_col));\n    }\n}\n\n// frame_padding < 0: uses FramePadding from style (default)\n// frame_padding = 0: no framing\n// frame_padding > 0: set framing size\n// The color used are the button colors.\nbool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n\n    // Default to using texture ID as ID. User can still push string/integer prefixes.\n    // We could hash the size/uv to create a unique ID but that would prevent the user from animating UV.\n    PushID((void*)(intptr_t)user_texture_id);\n    const ImGuiID id = window->GetID(\"#image\");\n    PopID();\n\n    const ImVec2 padding = (frame_padding >= 0) ? ImVec2((float)frame_padding, (float)frame_padding) : style.FramePadding;\n    const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding * 2);\n    const ImRect image_bb(window->DC.CursorPos + padding, window->DC.CursorPos + padding + size);\n    ItemSize(bb);\n    if (!ItemAdd(bb, id))\n        return false;\n\n    bool hovered, held;\n    bool pressed = ButtonBehavior(bb, id, &hovered, &held);\n\n    // Render\n    const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);\n    RenderNavHighlight(bb, id);\n    RenderFrame(bb.Min, bb.Max, col, true, ImClamp((float)ImMin(padding.x, padding.y), 0.0f, style.FrameRounding));\n    if (bg_col.w > 0.0f)\n        window->DrawList->AddRectFilled(image_bb.Min, image_bb.Max, GetColorU32(bg_col));\n    window->DrawList->AddImage(user_texture_id, image_bb.Min, image_bb.Max, uv0, uv1, GetColorU32(tint_col));\n\n    return pressed;\n}\n\nbool ImGui::Checkbox(const char* label, bool* v)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = window->GetID(label);\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n\n    const float square_sz = GetFrameHeight();\n    const ImVec2 pos = window->DC.CursorPos;\n    const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f));\n    ItemSize(total_bb, style.FramePadding.y);\n    if (!ItemAdd(total_bb, id))\n        return false;\n\n    bool hovered, held;\n    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);\n    if (pressed)\n    {\n        *v = !(*v);\n        MarkItemEdited(id);\n    }\n\n    const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz));\n    RenderNavHighlight(total_bb, id);\n    RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding);\n    if (*v)\n    {\n        const float pad = ImMax(1.0f, (float)(int)(square_sz / 6.0f));\n        RenderCheckMark(check_bb.Min + ImVec2(pad, pad), GetColorU32(ImGuiCol_CheckMark), square_sz - pad*2.0f);\n    }\n\n    if (g.LogEnabled)\n        LogRenderedText(&total_bb.Min, *v ? \"[x]\" : \"[ ]\");\n    if (label_size.x > 0.0f)\n        RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label);\n\n    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));\n    return pressed;\n}\n\nbool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value)\n{\n    bool v = ((*flags & flags_value) == flags_value);\n    bool pressed = Checkbox(label, &v);\n    if (pressed)\n    {\n        if (v)\n            *flags |= flags_value;\n        else\n            *flags &= ~flags_value;\n    }\n\n    return pressed;\n}\n\nbool ImGui::RadioButton(const char* label, bool active)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = window->GetID(label);\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n\n    const float square_sz = GetFrameHeight();\n    const ImVec2 pos = window->DC.CursorPos;\n    const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz));\n    const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f));\n    ItemSize(total_bb, style.FramePadding.y);\n    if (!ItemAdd(total_bb, id))\n        return false;\n\n    ImVec2 center = check_bb.GetCenter();\n    center.x = (float)(int)center.x + 0.5f;\n    center.y = (float)(int)center.y + 0.5f;\n    const float radius = (square_sz - 1.0f) * 0.5f;\n\n    bool hovered, held;\n    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);\n    if (pressed)\n        MarkItemEdited(id);\n\n    RenderNavHighlight(total_bb, id);\n    window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), 16);\n    if (active)\n    {\n        const float pad = ImMax(1.0f, (float)(int)(square_sz / 6.0f));\n        window->DrawList->AddCircleFilled(center, radius - pad, GetColorU32(ImGuiCol_CheckMark), 16);\n    }\n\n    if (style.FrameBorderSize > 0.0f)\n    {\n        window->DrawList->AddCircle(center + ImVec2(1,1), radius, GetColorU32(ImGuiCol_BorderShadow), 16, style.FrameBorderSize);\n        window->DrawList->AddCircle(center, radius, GetColorU32(ImGuiCol_Border), 16, style.FrameBorderSize);\n    }\n\n    if (g.LogEnabled)\n        LogRenderedText(&total_bb.Min, active ? \"(x)\" : \"( )\");\n    if (label_size.x > 0.0f)\n        RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label);\n\n    return pressed;\n}\n\nbool ImGui::RadioButton(const char* label, int* v, int v_button)\n{\n    const bool pressed = RadioButton(label, *v == v_button);\n    if (pressed)\n        *v = v_button;\n    return pressed;\n}\n\n// size_arg (for each axis) < 0.0f: align to end, 0.0f: auto, > 0.0f: specified size\nvoid ImGui::ProgressBar(float fraction, const ImVec2& size_arg, const char* overlay)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n\n    ImVec2 pos = window->DC.CursorPos;\n    ImVec2 size = CalcItemSize(size_arg, GetNextItemWidth(), g.FontSize + style.FramePadding.y*2.0f);\n    ImRect bb(pos, pos + size);\n    ItemSize(size, style.FramePadding.y);\n    if (!ItemAdd(bb, 0))\n        return;\n\n    // Render\n    fraction = ImSaturate(fraction);\n    RenderFrame(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding);\n    bb.Expand(ImVec2(-style.FrameBorderSize, -style.FrameBorderSize));\n    const ImVec2 fill_br = ImVec2(ImLerp(bb.Min.x, bb.Max.x, fraction), bb.Max.y);\n    RenderRectFilledRangeH(window->DrawList, bb, GetColorU32(ImGuiCol_PlotHistogram), 0.0f, fraction, style.FrameRounding);\n\n    // Default displaying the fraction as percentage string, but user can override it\n    char overlay_buf[32];\n    if (!overlay)\n    {\n        ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), \"%.0f%%\", fraction*100+0.01f);\n        overlay = overlay_buf;\n    }\n\n    ImVec2 overlay_size = CalcTextSize(overlay, NULL);\n    if (overlay_size.x > 0.0f)\n        RenderTextClipped(ImVec2(ImClamp(fill_br.x + style.ItemSpacing.x, bb.Min.x, bb.Max.x - overlay_size.x - style.ItemInnerSpacing.x), bb.Min.y), bb.Max, overlay, NULL, &overlay_size, ImVec2(0.0f,0.5f), &bb);\n}\n\nvoid ImGui::Bullet()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const float line_height = ImMax(ImMin(window->DC.CurrentLineSize.y, g.FontSize + g.Style.FramePadding.y*2), g.FontSize);\n    const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize, line_height));\n    ItemSize(bb);\n    if (!ItemAdd(bb, 0))\n    {\n        SameLine(0, style.FramePadding.x*2);\n        return;\n    }\n\n    // Render and stay on same line\n    RenderBullet(bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f));\n    SameLine(0, style.FramePadding.x*2);\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: Low-level Layout helpers\n//-------------------------------------------------------------------------\n// - Spacing()\n// - Dummy()\n// - NewLine()\n// - AlignTextToFramePadding()\n// - Separator()\n// - VerticalSeparator() [Internal]\n// - SplitterBehavior() [Internal]\n//-------------------------------------------------------------------------\n\nvoid ImGui::Spacing()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n    ItemSize(ImVec2(0,0));\n}\n\nvoid ImGui::Dummy(const ImVec2& size)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);\n    ItemSize(size);\n    ItemAdd(bb, 0);\n}\n\nvoid ImGui::NewLine()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiLayoutType backup_layout_type = window->DC.LayoutType;\n    window->DC.LayoutType = ImGuiLayoutType_Vertical;\n    if (window->DC.CurrentLineSize.y > 0.0f)     // In the event that we are on a line with items that is smaller that FontSize high, we will preserve its height.\n        ItemSize(ImVec2(0,0));\n    else\n        ItemSize(ImVec2(0.0f, g.FontSize));\n    window->DC.LayoutType = backup_layout_type;\n}\n\nvoid ImGui::AlignTextToFramePadding()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    window->DC.CurrentLineSize.y = ImMax(window->DC.CurrentLineSize.y, g.FontSize + g.Style.FramePadding.y * 2);\n    window->DC.CurrentLineTextBaseOffset = ImMax(window->DC.CurrentLineTextBaseOffset, g.Style.FramePadding.y);\n}\n\n// Horizontal/vertical separating line\nvoid ImGui::Separator()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n    ImGuiContext& g = *GImGui;\n\n    // Those flags should eventually be overrideable by the user\n    ImGuiSeparatorFlags flags = (window->DC.LayoutType == ImGuiLayoutType_Horizontal) ? ImGuiSeparatorFlags_Vertical : ImGuiSeparatorFlags_Horizontal;\n    IM_ASSERT(ImIsPowerOfTwo(flags & (ImGuiSeparatorFlags_Horizontal | ImGuiSeparatorFlags_Vertical)));   // Check that only 1 option is selected\n    if (flags & ImGuiSeparatorFlags_Vertical)\n    {\n        VerticalSeparator();\n        return;\n    }\n\n    // Horizontal Separator\n    if (window->DC.CurrentColumns)\n        PopClipRect();\n\n    float x1 = window->Pos.x;\n    float x2 = window->Pos.x + window->Size.x;\n    if (!window->DC.GroupStack.empty())\n        x1 += window->DC.Indent.x;\n\n    const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y+1.0f));\n    ItemSize(ImVec2(0.0f, 1.0f)); // NB: we don't provide our width so that it doesn't get feed back into AutoFit\n    if (!ItemAdd(bb, 0))\n    {\n        if (window->DC.CurrentColumns)\n            PushColumnClipRect();\n        return;\n    }\n\n    window->DrawList->AddLine(bb.Min, ImVec2(bb.Max.x,bb.Min.y), GetColorU32(ImGuiCol_Separator));\n\n    if (g.LogEnabled)\n        LogRenderedText(&bb.Min, \"--------------------------------\");\n\n    if (window->DC.CurrentColumns)\n    {\n        PushColumnClipRect();\n        window->DC.CurrentColumns->LineMinY = window->DC.CursorPos.y;\n    }\n}\n\nvoid ImGui::VerticalSeparator()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n    ImGuiContext& g = *GImGui;\n\n    float y1 = window->DC.CursorPos.y;\n    float y2 = window->DC.CursorPos.y + window->DC.CurrentLineSize.y;\n    const ImRect bb(ImVec2(window->DC.CursorPos.x, y1), ImVec2(window->DC.CursorPos.x + 1.0f, y2));\n    ItemSize(ImVec2(1.0f, 0.0f));\n    if (!ItemAdd(bb, 0))\n        return;\n\n    window->DrawList->AddLine(ImVec2(bb.Min.x, bb.Min.y), ImVec2(bb.Min.x, bb.Max.y), GetColorU32(ImGuiCol_Separator));\n    if (g.LogEnabled)\n        LogText(\" |\");\n}\n\n// Using 'hover_visibility_delay' allows us to hide the highlight and mouse cursor for a short time, which can be convenient to reduce visual noise.\nbool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend, float hover_visibility_delay)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags;\n    window->DC.ItemFlags |= ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus;\n    bool item_add = ItemAdd(bb, id);\n    window->DC.ItemFlags = item_flags_backup;\n    if (!item_add)\n        return false;\n\n    bool hovered, held;\n    ImRect bb_interact = bb;\n    bb_interact.Expand(axis == ImGuiAxis_Y ? ImVec2(0.0f, hover_extend) : ImVec2(hover_extend, 0.0f));\n    ButtonBehavior(bb_interact, id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap);\n    if (g.ActiveId != id)\n        SetItemAllowOverlap();\n\n    if (held || (g.HoveredId == id && g.HoveredIdPreviousFrame == id && g.HoveredIdTimer >= hover_visibility_delay))\n        SetMouseCursor(axis == ImGuiAxis_Y ? ImGuiMouseCursor_ResizeNS : ImGuiMouseCursor_ResizeEW);\n\n    ImRect bb_render = bb;\n    if (held)\n    {\n        ImVec2 mouse_delta_2d = g.IO.MousePos - g.ActiveIdClickOffset - bb_interact.Min;\n        float mouse_delta = (axis == ImGuiAxis_Y) ? mouse_delta_2d.y : mouse_delta_2d.x;\n\n        // Minimum pane size\n        float size_1_maximum_delta = ImMax(0.0f, *size1 - min_size1);\n        float size_2_maximum_delta = ImMax(0.0f, *size2 - min_size2);\n        if (mouse_delta < -size_1_maximum_delta)\n            mouse_delta = -size_1_maximum_delta;\n        if (mouse_delta > size_2_maximum_delta)\n            mouse_delta = size_2_maximum_delta;\n\n        // Apply resize\n        if (mouse_delta != 0.0f)\n        {\n            if (mouse_delta < 0.0f)\n                IM_ASSERT(*size1 + mouse_delta >= min_size1);\n            if (mouse_delta > 0.0f)\n                IM_ASSERT(*size2 - mouse_delta >= min_size2);\n            *size1 += mouse_delta;\n            *size2 -= mouse_delta;\n            bb_render.Translate((axis == ImGuiAxis_X) ? ImVec2(mouse_delta, 0.0f) : ImVec2(0.0f, mouse_delta));\n            MarkItemEdited(id);\n        }\n    }\n\n    // Render\n    const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : (hovered && g.HoveredIdTimer >= hover_visibility_delay) ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator);\n    window->DrawList->AddRectFilled(bb_render.Min, bb_render.Max, col, g.Style.FrameRounding);\n\n    return held;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: ComboBox\n//-------------------------------------------------------------------------\n// - BeginCombo()\n// - EndCombo()\n// - Combo()\n//-------------------------------------------------------------------------\n\nstatic float CalcMaxPopupHeightFromItemCount(int items_count)\n{\n    ImGuiContext& g = *GImGui;\n    if (items_count <= 0)\n        return FLT_MAX;\n    return (g.FontSize + g.Style.ItemSpacing.y) * items_count - g.Style.ItemSpacing.y + (g.Style.WindowPadding.y * 2);\n}\n\nbool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags)\n{\n    // Always consume the SetNextWindowSizeConstraint() call in our early return paths\n    ImGuiContext& g = *GImGui;\n    ImGuiCond backup_next_window_size_constraint = g.NextWindowData.SizeConstraintCond;\n    g.NextWindowData.SizeConstraintCond = 0;\n\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    IM_ASSERT((flags & (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) != (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)); // Can't use both flags together\n\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = window->GetID(label);\n\n    const float arrow_size = (flags & ImGuiComboFlags_NoArrowButton) ? 0.0f : GetFrameHeight();\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n    const float expected_w = GetNextItemWidth();\n    const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : expected_w;\n    const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f));\n    const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));\n    ItemSize(total_bb, style.FramePadding.y);\n    if (!ItemAdd(total_bb, id, &frame_bb))\n        return false;\n\n    bool hovered, held;\n    bool pressed = ButtonBehavior(frame_bb, id, &hovered, &held);\n    bool popup_open = IsPopupOpen(id);\n\n    const ImU32 frame_col = GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);\n    const float value_x2 = ImMax(frame_bb.Min.x, frame_bb.Max.x - arrow_size);\n    RenderNavHighlight(frame_bb, id);\n    if (!(flags & ImGuiComboFlags_NoPreview))\n        window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(value_x2, frame_bb.Max.y), frame_col, style.FrameRounding, ImDrawCornerFlags_Left);\n    if (!(flags & ImGuiComboFlags_NoArrowButton))\n    {\n        window->DrawList->AddRectFilled(ImVec2(value_x2, frame_bb.Min.y), frame_bb.Max, GetColorU32((popup_open || hovered) ? ImGuiCol_ButtonHovered : ImGuiCol_Button), style.FrameRounding, (w <= arrow_size) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Right);\n        RenderArrow(ImVec2(value_x2 + style.FramePadding.y, frame_bb.Min.y + style.FramePadding.y), ImGuiDir_Down);\n    }\n    RenderFrameBorder(frame_bb.Min, frame_bb.Max, style.FrameRounding);\n    if (preview_value != NULL && !(flags & ImGuiComboFlags_NoPreview))\n        RenderTextClipped(frame_bb.Min + style.FramePadding, ImVec2(value_x2, frame_bb.Max.y), preview_value, NULL, NULL, ImVec2(0.0f,0.0f));\n    if (label_size.x > 0)\n        RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);\n\n    if ((pressed || g.NavActivateId == id) && !popup_open)\n    {\n        if (window->DC.NavLayerCurrent == 0)\n            window->NavLastIds[0] = id;\n        OpenPopupEx(id);\n        popup_open = true;\n    }\n\n    if (!popup_open)\n        return false;\n\n    if (backup_next_window_size_constraint)\n    {\n        g.NextWindowData.SizeConstraintCond = backup_next_window_size_constraint;\n        g.NextWindowData.SizeConstraintRect.Min.x = ImMax(g.NextWindowData.SizeConstraintRect.Min.x, w);\n    }\n    else\n    {\n        if ((flags & ImGuiComboFlags_HeightMask_) == 0)\n            flags |= ImGuiComboFlags_HeightRegular;\n        IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiComboFlags_HeightMask_));    // Only one\n        int popup_max_height_in_items = -1;\n        if (flags & ImGuiComboFlags_HeightRegular)     popup_max_height_in_items = 8;\n        else if (flags & ImGuiComboFlags_HeightSmall)  popup_max_height_in_items = 4;\n        else if (flags & ImGuiComboFlags_HeightLarge)  popup_max_height_in_items = 20;\n        SetNextWindowSizeConstraints(ImVec2(w, 0.0f), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items)));\n    }\n\n    char name[16];\n    ImFormatString(name, IM_ARRAYSIZE(name), \"##Combo_%02d\", g.BeginPopupStack.Size); // Recycle windows based on depth\n\n    // Peak into expected window size so we can position it\n    if (ImGuiWindow* popup_window = FindWindowByName(name))\n        if (popup_window->WasActive)\n        {\n            ImVec2 size_expected = CalcWindowExpectedSize(popup_window);\n            if (flags & ImGuiComboFlags_PopupAlignLeft)\n                popup_window->AutoPosLastDirection = ImGuiDir_Left;\n            ImRect r_outer = GetWindowAllowedExtentRect(popup_window);\n            ImVec2 pos = FindBestWindowPosForPopupEx(frame_bb.GetBL(), size_expected, &popup_window->AutoPosLastDirection, r_outer, frame_bb, ImGuiPopupPositionPolicy_ComboBox);\n            SetNextWindowPos(pos);\n        }\n\n    // Horizontally align ourselves with the framed text\n    ImGuiWindowFlags window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings;\n    PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(style.FramePadding.x, style.WindowPadding.y));\n    bool ret = Begin(name, NULL, window_flags);\n    PopStyleVar();\n    if (!ret)\n    {\n        EndPopup();\n        IM_ASSERT(0);   // This should never happen as we tested for IsPopupOpen() above\n        return false;\n    }\n    return true;\n}\n\nvoid ImGui::EndCombo()\n{\n    EndPopup();\n}\n\n// Getter for the old Combo() API: const char*[]\nstatic bool Items_ArrayGetter(void* data, int idx, const char** out_text)\n{\n    const char* const* items = (const char* const*)data;\n    if (out_text)\n        *out_text = items[idx];\n    return true;\n}\n\n// Getter for the old Combo() API: \"item1\\0item2\\0item3\\0\"\nstatic bool Items_SingleStringGetter(void* data, int idx, const char** out_text)\n{\n    // FIXME-OPT: we could pre-compute the indices to fasten this. But only 1 active combo means the waste is limited.\n    const char* items_separated_by_zeros = (const char*)data;\n    int items_count = 0;\n    const char* p = items_separated_by_zeros;\n    while (*p)\n    {\n        if (idx == items_count)\n            break;\n        p += strlen(p) + 1;\n        items_count++;\n    }\n    if (!*p)\n        return false;\n    if (out_text)\n        *out_text = p;\n    return true;\n}\n\n// Old API, prefer using BeginCombo() nowadays if you can.\nbool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int popup_max_height_in_items)\n{\n    ImGuiContext& g = *GImGui;\n\n    // Call the getter to obtain the preview string which is a parameter to BeginCombo()\n    const char* preview_value = NULL;\n    if (*current_item >= 0 && *current_item < items_count)\n        items_getter(data, *current_item, &preview_value);\n\n    // The old Combo() API exposed \"popup_max_height_in_items\". The new more general BeginCombo() API doesn't have/need it, but we emulate it here.\n    if (popup_max_height_in_items != -1 && !g.NextWindowData.SizeConstraintCond)\n        SetNextWindowSizeConstraints(ImVec2(0,0), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items)));\n\n    if (!BeginCombo(label, preview_value, ImGuiComboFlags_None))\n        return false;\n\n    // Display items\n    // FIXME-OPT: Use clipper (but we need to disable it on the appearing frame to make sure our call to SetItemDefaultFocus() is processed)\n    bool value_changed = false;\n    for (int i = 0; i < items_count; i++)\n    {\n        PushID((void*)(intptr_t)i);\n        const bool item_selected = (i == *current_item);\n        const char* item_text;\n        if (!items_getter(data, i, &item_text))\n            item_text = \"*Unknown item*\";\n        if (Selectable(item_text, item_selected))\n        {\n            value_changed = true;\n            *current_item = i;\n        }\n        if (item_selected)\n            SetItemDefaultFocus();\n        PopID();\n    }\n\n    EndCombo();\n    return value_changed;\n}\n\n// Combo box helper allowing to pass an array of strings.\nbool ImGui::Combo(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items)\n{\n    const bool value_changed = Combo(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_in_items);\n    return value_changed;\n}\n\n// Combo box helper allowing to pass all items in a single string literal holding multiple zero-terminated items \"item1\\0item2\\0\"\nbool ImGui::Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items)\n{\n    int items_count = 0;\n    const char* p = items_separated_by_zeros;       // FIXME-OPT: Avoid computing this, or at least only when combo is open\n    while (*p)\n    {\n        p += strlen(p) + 1;\n        items_count++;\n    }\n    bool value_changed = Combo(label, current_item, Items_SingleStringGetter, (void*)items_separated_by_zeros, items_count, height_in_items);\n    return value_changed;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Data Type and Data Formatting Helpers [Internal]\n//-------------------------------------------------------------------------\n// - PatchFormatStringFloatToInt()\n// - DataTypeGetInfo()\n// - DataTypeFormatString()\n// - DataTypeApplyOp()\n// - DataTypeApplyOpFromText()\n// - GetMinimumStepAtDecimalPrecision\n// - RoundScalarWithFormat<>()\n//-------------------------------------------------------------------------\n\nstatic const ImGuiDataTypeInfo GDataTypeInfo[] =\n{\n    { sizeof(char),             \"%d\",   \"%d\"    },  // ImGuiDataType_S8\n    { sizeof(unsigned char),    \"%u\",   \"%u\"    },\n    { sizeof(short),            \"%d\",   \"%d\"    },  // ImGuiDataType_S16\n    { sizeof(unsigned short),   \"%u\",   \"%u\"    },\n    { sizeof(int),              \"%d\",   \"%d\"    },  // ImGuiDataType_S32\n    { sizeof(unsigned int),     \"%u\",   \"%u\"    },\n#ifdef _MSC_VER\n    { sizeof(ImS64),            \"%I64d\",\"%I64d\" },  // ImGuiDataType_S64\n    { sizeof(ImU64),            \"%I64u\",\"%I64u\" },\n#else\n    { sizeof(ImS64),            \"%lld\", \"%lld\"  },  // ImGuiDataType_S64\n    { sizeof(ImU64),            \"%llu\", \"%llu\"  },\n#endif\n    { sizeof(float),            \"%f\",   \"%f\"    },  // ImGuiDataType_Float (float are promoted to double in va_arg)\n    { sizeof(double),           \"%f\",   \"%lf\"   },  // ImGuiDataType_Double\n};\nIM_STATIC_ASSERT(IM_ARRAYSIZE(GDataTypeInfo) == ImGuiDataType_COUNT);\n\n// FIXME-LEGACY: Prior to 1.61 our DragInt() function internally used floats and because of this the compile-time default value for format was \"%.0f\".\n// Even though we changed the compile-time default, we expect users to have carried %f around, which would break the display of DragInt() calls.\n// To honor backward compatibility we are rewriting the format string, unless IMGUI_DISABLE_OBSOLETE_FUNCTIONS is enabled. What could possibly go wrong?!\nstatic const char* PatchFormatStringFloatToInt(const char* fmt)\n{\n    if (fmt[0] == '%' && fmt[1] == '.' && fmt[2] == '0' && fmt[3] == 'f' && fmt[4] == 0) // Fast legacy path for \"%.0f\" which is expected to be the most common case.\n        return \"%d\";\n    const char* fmt_start = ImParseFormatFindStart(fmt);    // Find % (if any, and ignore %%)\n    const char* fmt_end = ImParseFormatFindEnd(fmt_start);  // Find end of format specifier, which itself is an exercise of confidence/recklessness (because snprintf is dependent on libc or user).\n    if (fmt_end > fmt_start && fmt_end[-1] == 'f')\n    {\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n        if (fmt_start == fmt && fmt_end[0] == 0)\n            return \"%d\";\n        ImGuiContext& g = *GImGui;\n        ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), \"%.*s%%d%s\", (int)(fmt_start - fmt), fmt, fmt_end); // Honor leading and trailing decorations, but lose alignment/precision.\n        return g.TempBuffer;\n#else\n        IM_ASSERT(0 && \"DragInt(): Invalid format string!\"); // Old versions used a default parameter of \"%.0f\", please replace with e.g. \"%d\"\n#endif\n    }\n    return fmt;\n}\n\nconst ImGuiDataTypeInfo* ImGui::DataTypeGetInfo(ImGuiDataType data_type)\n{\n    IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT);\n    return &GDataTypeInfo[data_type];\n}\n\nint ImGui::DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* data_ptr, const char* format)\n{\n    // Signedness doesn't matter when pushing integer arguments\n    if (data_type == ImGuiDataType_S32 || data_type == ImGuiDataType_U32)\n        return ImFormatString(buf, buf_size, format, *(const ImU32*)data_ptr);\n    if (data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64)\n        return ImFormatString(buf, buf_size, format, *(const ImU64*)data_ptr);\n    if (data_type == ImGuiDataType_Float)\n        return ImFormatString(buf, buf_size, format, *(const float*)data_ptr);\n    if (data_type == ImGuiDataType_Double)\n        return ImFormatString(buf, buf_size, format, *(const double*)data_ptr);\n    if (data_type == ImGuiDataType_S8)\n        return ImFormatString(buf, buf_size, format, *(const ImS8*)data_ptr);\n    if (data_type == ImGuiDataType_U8)\n        return ImFormatString(buf, buf_size, format, *(const ImU8*)data_ptr);\n    if (data_type == ImGuiDataType_S16)\n        return ImFormatString(buf, buf_size, format, *(const ImS16*)data_ptr);\n    if (data_type == ImGuiDataType_U16)\n        return ImFormatString(buf, buf_size, format, *(const ImU16*)data_ptr);\n    IM_ASSERT(0);\n    return 0;\n}\n\nvoid ImGui::DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg1, const void* arg2)\n{\n    IM_ASSERT(op == '+' || op == '-');\n    switch (data_type)\n    {\n        case ImGuiDataType_S8:\n            if (op == '+') { *(ImS8*)output  = ImAddClampOverflow(*(const ImS8*)arg1,  *(const ImS8*)arg2,  IM_S8_MIN,  IM_S8_MAX); }\n            if (op == '-') { *(ImS8*)output  = ImSubClampOverflow(*(const ImS8*)arg1,  *(const ImS8*)arg2,  IM_S8_MIN,  IM_S8_MAX); }\n            return;\n        case ImGuiDataType_U8:\n            if (op == '+') { *(ImU8*)output  = ImAddClampOverflow(*(const ImU8*)arg1,  *(const ImU8*)arg2,  IM_U8_MIN,  IM_U8_MAX); }\n            if (op == '-') { *(ImU8*)output  = ImSubClampOverflow(*(const ImU8*)arg1,  *(const ImU8*)arg2,  IM_U8_MIN,  IM_U8_MAX); }\n            return;\n        case ImGuiDataType_S16:\n            if (op == '+') { *(ImS16*)output = ImAddClampOverflow(*(const ImS16*)arg1, *(const ImS16*)arg2, IM_S16_MIN, IM_S16_MAX); }\n            if (op == '-') { *(ImS16*)output = ImSubClampOverflow(*(const ImS16*)arg1, *(const ImS16*)arg2, IM_S16_MIN, IM_S16_MAX); }\n            return;\n        case ImGuiDataType_U16:\n            if (op == '+') { *(ImU16*)output = ImAddClampOverflow(*(const ImU16*)arg1, *(const ImU16*)arg2, IM_U16_MIN, IM_U16_MAX); }\n            if (op == '-') { *(ImU16*)output = ImSubClampOverflow(*(const ImU16*)arg1, *(const ImU16*)arg2, IM_U16_MIN, IM_U16_MAX); }\n            return;\n        case ImGuiDataType_S32:\n            if (op == '+') { *(ImS32*)output = ImAddClampOverflow(*(const ImS32*)arg1, *(const ImS32*)arg2, IM_S32_MIN, IM_S32_MAX); }\n            if (op == '-') { *(ImS32*)output = ImSubClampOverflow(*(const ImS32*)arg1, *(const ImS32*)arg2, IM_S32_MIN, IM_S32_MAX); }\n            return;\n        case ImGuiDataType_U32:\n            if (op == '+') { *(ImU32*)output = ImAddClampOverflow(*(const ImU32*)arg1, *(const ImU32*)arg2, IM_U32_MIN, IM_U32_MAX); }\n            if (op == '-') { *(ImU32*)output = ImSubClampOverflow(*(const ImU32*)arg1, *(const ImU32*)arg2, IM_U32_MIN, IM_U32_MAX); }\n            return;\n        case ImGuiDataType_S64:\n            if (op == '+') { *(ImS64*)output = ImAddClampOverflow(*(const ImS64*)arg1, *(const ImS64*)arg2, IM_S64_MIN, IM_S64_MAX); }\n            if (op == '-') { *(ImS64*)output = ImSubClampOverflow(*(const ImS64*)arg1, *(const ImS64*)arg2, IM_S64_MIN, IM_S64_MAX); }\n            return;\n        case ImGuiDataType_U64:\n            if (op == '+') { *(ImU64*)output = ImAddClampOverflow(*(const ImU64*)arg1, *(const ImU64*)arg2, IM_U64_MIN, IM_U64_MAX); }\n            if (op == '-') { *(ImU64*)output = ImSubClampOverflow(*(const ImU64*)arg1, *(const ImU64*)arg2, IM_U64_MIN, IM_U64_MAX); }\n            return;\n        case ImGuiDataType_Float:\n            if (op == '+') { *(float*)output = *(const float*)arg1 + *(const float*)arg2; }\n            if (op == '-') { *(float*)output = *(const float*)arg1 - *(const float*)arg2; }\n            return;\n        case ImGuiDataType_Double:\n            if (op == '+') { *(double*)output = *(const double*)arg1 + *(const double*)arg2; }\n            if (op == '-') { *(double*)output = *(const double*)arg1 - *(const double*)arg2; }\n            return;\n        case ImGuiDataType_COUNT: break;\n    }\n    IM_ASSERT(0);\n}\n\n// User can input math operators (e.g. +100) to edit a numerical values.\n// NB: This is _not_ a full expression evaluator. We should probably add one and replace this dumb mess..\nbool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* data_ptr, const char* format)\n{\n    while (ImCharIsBlankA(*buf))\n        buf++;\n\n    // We don't support '-' op because it would conflict with inputing negative value.\n    // Instead you can use +-100 to subtract from an existing value\n    char op = buf[0];\n    if (op == '+' || op == '*' || op == '/')\n    {\n        buf++;\n        while (ImCharIsBlankA(*buf))\n            buf++;\n    }\n    else\n    {\n        op = 0;\n    }\n    if (!buf[0])\n        return false;\n\n    // Copy the value in an opaque buffer so we can compare at the end of the function if it changed at all.\n    IM_ASSERT(data_type < ImGuiDataType_COUNT);\n    int data_backup[2];\n    const ImGuiDataTypeInfo* type_info = ImGui::DataTypeGetInfo(data_type);\n    IM_ASSERT(type_info->Size <= sizeof(data_backup));\n    memcpy(data_backup, data_ptr, type_info->Size);\n\n    if (format == NULL)\n        format = type_info->ScanFmt;\n\n    // FIXME-LEGACY: The aim is to remove those operators and write a proper expression evaluator at some point..\n    int arg1i = 0;\n    if (data_type == ImGuiDataType_S32)\n    {\n        int* v = (int*)data_ptr;\n        int arg0i = *v;\n        float arg1f = 0.0f;\n        if (op && sscanf(initial_value_buf, format, &arg0i) < 1)\n            return false;\n        // Store operand in a float so we can use fractional value for multipliers (*1.1), but constant always parsed as integer so we can fit big integers (e.g. 2000000003) past float precision\n        if (op == '+')      { if (sscanf(buf, \"%d\", &arg1i)) *v = (int)(arg0i + arg1i); }                   // Add (use \"+-\" to subtract)\n        else if (op == '*') { if (sscanf(buf, \"%f\", &arg1f)) *v = (int)(arg0i * arg1f); }                   // Multiply\n        else if (op == '/') { if (sscanf(buf, \"%f\", &arg1f) && arg1f != 0.0f) *v = (int)(arg0i / arg1f); }  // Divide\n        else                { if (sscanf(buf, format, &arg1i) == 1) *v = arg1i; }                           // Assign constant\n    }\n    else if (data_type == ImGuiDataType_Float)\n    {\n        // For floats we have to ignore format with precision (e.g. \"%.2f\") because sscanf doesn't take them in\n        format = \"%f\";\n        float* v = (float*)data_ptr;\n        float arg0f = *v, arg1f = 0.0f;\n        if (op && sscanf(initial_value_buf, format, &arg0f) < 1)\n            return false;\n        if (sscanf(buf, format, &arg1f) < 1)\n            return false;\n        if (op == '+')      { *v = arg0f + arg1f; }                    // Add (use \"+-\" to subtract)\n        else if (op == '*') { *v = arg0f * arg1f; }                    // Multiply\n        else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide\n        else                { *v = arg1f; }                            // Assign constant\n    }\n    else if (data_type == ImGuiDataType_Double)\n    {\n        format = \"%lf\"; // scanf differentiate float/double unlike printf which forces everything to double because of ellipsis\n        double* v = (double*)data_ptr;\n        double arg0f = *v, arg1f = 0.0;\n        if (op && sscanf(initial_value_buf, format, &arg0f) < 1)\n            return false;\n        if (sscanf(buf, format, &arg1f) < 1)\n            return false;\n        if (op == '+')      { *v = arg0f + arg1f; }                    // Add (use \"+-\" to subtract)\n        else if (op == '*') { *v = arg0f * arg1f; }                    // Multiply\n        else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide\n        else                { *v = arg1f; }                            // Assign constant\n    }\n    else if (data_type == ImGuiDataType_U32 || data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64)\n    {\n        // All other types assign constant\n        // We don't bother handling support for legacy operators since they are a little too crappy. Instead we will later implement a proper expression evaluator in the future.\n        sscanf(buf, format, data_ptr);\n    }\n    else\n    {\n        // Small types need a 32-bit buffer to receive the result from scanf()\n        int v32;\n        sscanf(buf, format, &v32);\n        if (data_type == ImGuiDataType_S8)\n            *(ImS8*)data_ptr = (ImS8)ImClamp(v32, (int)IM_S8_MIN, (int)IM_S8_MAX);\n        else if (data_type == ImGuiDataType_U8)\n            *(ImU8*)data_ptr = (ImU8)ImClamp(v32, (int)IM_U8_MIN, (int)IM_U8_MAX);\n        else if (data_type == ImGuiDataType_S16)\n            *(ImS16*)data_ptr = (ImS16)ImClamp(v32, (int)IM_S16_MIN, (int)IM_S16_MAX);\n        else if (data_type == ImGuiDataType_U16)\n            *(ImU16*)data_ptr = (ImU16)ImClamp(v32, (int)IM_U16_MIN, (int)IM_U16_MAX);\n        else\n            IM_ASSERT(0);\n    }\n\n    return memcmp(data_backup, data_ptr, type_info->Size) != 0;\n}\n\nstatic float GetMinimumStepAtDecimalPrecision(int decimal_precision)\n{\n    static const float min_steps[10] = { 1.0f, 0.1f, 0.01f, 0.001f, 0.0001f, 0.00001f, 0.000001f, 0.0000001f, 0.00000001f, 0.000000001f };\n    if (decimal_precision < 0)\n        return FLT_MIN;\n    return (decimal_precision < IM_ARRAYSIZE(min_steps)) ? min_steps[decimal_precision] : ImPow(10.0f, (float)-decimal_precision);\n}\n\ntemplate<typename TYPE>\nstatic const char* ImAtoi(const char* src, TYPE* output)\n{\n    int negative = 0;\n    if (*src == '-') { negative = 1; src++; }\n    if (*src == '+') { src++; }\n    TYPE v = 0;\n    while (*src >= '0' && *src <= '9')\n        v = (v * 10) + (*src++ - '0');\n    *output = negative ? -v : v;\n    return src;\n}\n\ntemplate<typename TYPE, typename SIGNEDTYPE>\nTYPE ImGui::RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, TYPE v)\n{\n    const char* fmt_start = ImParseFormatFindStart(format);\n    if (fmt_start[0] != '%' || fmt_start[1] == '%') // Don't apply if the value is not visible in the format string\n        return v;\n    char v_str[64];\n    ImFormatString(v_str, IM_ARRAYSIZE(v_str), fmt_start, v);\n    const char* p = v_str;\n    while (*p == ' ')\n        p++;\n    if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double)\n        v = (TYPE)ImAtof(p);\n    else\n        ImAtoi(p, (SIGNEDTYPE*)&v);\n    return v;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: DragScalar, DragFloat, DragInt, etc.\n//-------------------------------------------------------------------------\n// - DragBehaviorT<>() [Internal]\n// - DragBehavior() [Internal]\n// - DragScalar()\n// - DragScalarN()\n// - DragFloat()\n// - DragFloat2()\n// - DragFloat3()\n// - DragFloat4()\n// - DragFloatRange2()\n// - DragInt()\n// - DragInt2()\n// - DragInt3()\n// - DragInt4()\n// - DragIntRange2()\n//-------------------------------------------------------------------------\n\n// This is called by DragBehavior() when the widget is active (held by mouse or being manipulated with Nav controls)\ntemplate<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE>\nbool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const TYPE v_min, const TYPE v_max, const char* format, float power, ImGuiDragFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    const ImGuiAxis axis = (flags & ImGuiDragFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X;\n    const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double);\n    const bool has_min_max = (v_min != v_max);\n    const bool is_power = (power != 1.0f && is_decimal && has_min_max && (v_max - v_min < FLT_MAX));\n\n    // Default tweak speed\n    if (v_speed == 0.0f && has_min_max && (v_max - v_min < FLT_MAX))\n        v_speed = (float)((v_max - v_min) * g.DragSpeedDefaultRatio);\n\n    // Inputs accumulates into g.DragCurrentAccum, which is flushed into the current value as soon as it makes a difference with our precision settings\n    float adjust_delta = 0.0f;\n    if (g.ActiveIdSource == ImGuiInputSource_Mouse && IsMousePosValid() && g.IO.MouseDragMaxDistanceSqr[0] > 1.0f*1.0f)\n    {\n        adjust_delta = g.IO.MouseDelta[axis];\n        if (g.IO.KeyAlt)\n            adjust_delta *= 1.0f / 100.0f;\n        if (g.IO.KeyShift)\n            adjust_delta *= 10.0f;\n    }\n    else if (g.ActiveIdSource == ImGuiInputSource_Nav)\n    {\n        int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0;\n        adjust_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 1.0f / 10.0f, 10.0f)[axis];\n        v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision));\n    }\n    adjust_delta *= v_speed;\n\n    // For vertical drag we currently assume that Up=higher value (like we do with vertical sliders). This may become a parameter.\n    if (axis == ImGuiAxis_Y)\n        adjust_delta = -adjust_delta;\n\n    // Clear current value on activation\n    // Avoid altering values and clamping when we are _already_ past the limits and heading in the same direction, so e.g. if range is 0..255, current value is 300 and we are pushing to the right side, keep the 300.\n    bool is_just_activated = g.ActiveIdIsJustActivated;\n    bool is_already_past_limits_and_pushing_outward = has_min_max && ((*v >= v_max && adjust_delta > 0.0f) || (*v <= v_min && adjust_delta < 0.0f));\n    bool is_drag_direction_change_with_power = is_power && ((adjust_delta < 0 && g.DragCurrentAccum > 0) || (adjust_delta > 0 && g.DragCurrentAccum < 0));\n    if (is_just_activated || is_already_past_limits_and_pushing_outward || is_drag_direction_change_with_power)\n    {\n        g.DragCurrentAccum = 0.0f;\n        g.DragCurrentAccumDirty = false;\n    }\n    else if (adjust_delta != 0.0f)\n    {\n        g.DragCurrentAccum += adjust_delta;\n        g.DragCurrentAccumDirty = true;\n    }\n\n    if (!g.DragCurrentAccumDirty)\n        return false;\n\n    TYPE v_cur = *v;\n    FLOATTYPE v_old_ref_for_accum_remainder = (FLOATTYPE)0.0f;\n\n    if (is_power)\n    {\n        // Offset + round to user desired precision, with a curve on the v_min..v_max range to get more precision on one side of the range\n        FLOATTYPE v_old_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power);\n        FLOATTYPE v_new_norm_curved = v_old_norm_curved + (g.DragCurrentAccum / (v_max - v_min));\n        v_cur = v_min + (TYPE)ImPow(ImSaturate((float)v_new_norm_curved), power) * (v_max - v_min);\n        v_old_ref_for_accum_remainder = v_old_norm_curved;\n    }\n    else\n    {\n        v_cur += (TYPE)g.DragCurrentAccum;\n    }\n\n    // Round to user desired precision based on format string\n    v_cur = RoundScalarWithFormatT<TYPE, SIGNEDTYPE>(format, data_type, v_cur);\n\n    // Preserve remainder after rounding has been applied. This also allow slow tweaking of values.\n    g.DragCurrentAccumDirty = false;\n    if (is_power)\n    {\n        FLOATTYPE v_cur_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power);\n        g.DragCurrentAccum -= (float)(v_cur_norm_curved - v_old_ref_for_accum_remainder);\n    }\n    else\n    {\n        g.DragCurrentAccum -= (float)((SIGNEDTYPE)v_cur - (SIGNEDTYPE)*v);\n    }\n\n    // Lose zero sign for float/double\n    if (v_cur == (TYPE)-0)\n        v_cur = (TYPE)0;\n\n    // Clamp values (+ handle overflow/wrap-around for integer types)\n    if (*v != v_cur && has_min_max)\n    {\n        if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_decimal))\n            v_cur = v_min;\n        if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_decimal))\n            v_cur = v_max;\n    }\n\n    // Apply result\n    if (*v == v_cur)\n        return false;\n    *v = v_cur;\n    return true;\n}\n\nbool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* v, float v_speed, const void* v_min, const void* v_max, const char* format, float power, ImGuiDragFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.ActiveId == id)\n    {\n        if (g.ActiveIdSource == ImGuiInputSource_Mouse && !g.IO.MouseDown[0])\n            ClearActiveID();\n        else if (g.ActiveIdSource == ImGuiInputSource_Nav && g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated)\n            ClearActiveID();\n    }\n    if (g.ActiveId != id)\n        return false;\n\n    switch (data_type)\n    {\n    case ImGuiDataType_S8:     { ImS32 v32 = (ImS32)*(ImS8*)v;  bool r = DragBehaviorT<ImS32, ImS32, float >(ImGuiDataType_S32, &v32, v_speed, v_min ? *(const ImS8*) v_min : IM_S8_MIN,  v_max ? *(const ImS8*)v_max  : IM_S8_MAX,  format, power, flags); if (r) *(ImS8*)v = (ImS8)v32; return r; }\n    case ImGuiDataType_U8:     { ImU32 v32 = (ImU32)*(ImU8*)v;  bool r = DragBehaviorT<ImU32, ImS32, float >(ImGuiDataType_U32, &v32, v_speed, v_min ? *(const ImU8*) v_min : IM_U8_MIN,  v_max ? *(const ImU8*)v_max  : IM_U8_MAX,  format, power, flags); if (r) *(ImU8*)v = (ImU8)v32; return r; }\n    case ImGuiDataType_S16:    { ImS32 v32 = (ImS32)*(ImS16*)v; bool r = DragBehaviorT<ImS32, ImS32, float >(ImGuiDataType_S32, &v32, v_speed, v_min ? *(const ImS16*)v_min : IM_S16_MIN, v_max ? *(const ImS16*)v_max : IM_S16_MAX, format, power, flags); if (r) *(ImS16*)v = (ImS16)v32; return r; }\n    case ImGuiDataType_U16:    { ImU32 v32 = (ImU32)*(ImU16*)v; bool r = DragBehaviorT<ImU32, ImS32, float >(ImGuiDataType_U32, &v32, v_speed, v_min ? *(const ImU16*)v_min : IM_U16_MIN, v_max ? *(const ImU16*)v_max : IM_U16_MAX, format, power, flags); if (r) *(ImU16*)v = (ImU16)v32; return r; }\n    case ImGuiDataType_S32:    return DragBehaviorT<ImS32, ImS32, float >(data_type, (ImS32*)v,  v_speed, v_min ? *(const ImS32* )v_min : IM_S32_MIN, v_max ? *(const ImS32* )v_max : IM_S32_MAX, format, power, flags);\n    case ImGuiDataType_U32:    return DragBehaviorT<ImU32, ImS32, float >(data_type, (ImU32*)v,  v_speed, v_min ? *(const ImU32* )v_min : IM_U32_MIN, v_max ? *(const ImU32* )v_max : IM_U32_MAX, format, power, flags);\n    case ImGuiDataType_S64:    return DragBehaviorT<ImS64, ImS64, double>(data_type, (ImS64*)v,  v_speed, v_min ? *(const ImS64* )v_min : IM_S64_MIN, v_max ? *(const ImS64* )v_max : IM_S64_MAX, format, power, flags);\n    case ImGuiDataType_U64:    return DragBehaviorT<ImU64, ImS64, double>(data_type, (ImU64*)v,  v_speed, v_min ? *(const ImU64* )v_min : IM_U64_MIN, v_max ? *(const ImU64* )v_max : IM_U64_MAX, format, power, flags);\n    case ImGuiDataType_Float:  return DragBehaviorT<float, float, float >(data_type, (float*)v,  v_speed, v_min ? *(const float* )v_min : -FLT_MAX,   v_max ? *(const float* )v_max : FLT_MAX,    format, power, flags);\n    case ImGuiDataType_Double: return DragBehaviorT<double,double,double>(data_type, (double*)v, v_speed, v_min ? *(const double*)v_min : -DBL_MAX,   v_max ? *(const double*)v_max : DBL_MAX,    format, power, flags);\n    case ImGuiDataType_COUNT:  break;\n    }\n    IM_ASSERT(0);\n    return false;\n}\n\nbool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* v, float v_speed, const void* v_min, const void* v_max, const char* format, float power)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    if (power != 1.0f)\n        IM_ASSERT(v_min != NULL && v_max != NULL); // When using a power curve the drag needs to have known bounds\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = window->GetID(label);\n    const float w = GetNextItemWidth();\n\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n    const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f));\n    const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));\n\n    ItemSize(total_bb, style.FramePadding.y);\n    if (!ItemAdd(total_bb, id, &frame_bb))\n        return false;\n\n    // Default format string when passing NULL\n    if (format == NULL)\n        format = DataTypeGetInfo(data_type)->PrintFmt;\n    else if (data_type == ImGuiDataType_S32 && strcmp(format, \"%d\") != 0) // (FIXME-LEGACY: Patch old \"%.0f\" format string to use \"%d\", read function more details.)\n        format = PatchFormatStringFloatToInt(format);\n\n    // Tabbing or CTRL-clicking on Drag turns it into an input box\n    const bool hovered = ItemHoverable(frame_bb, id);\n    bool temp_input_is_active = TempInputTextIsActive(id);\n    bool temp_input_start = false;\n    if (!temp_input_is_active)\n    {\n        const bool focus_requested = FocusableItemRegister(window, id);\n        const bool clicked = (hovered && g.IO.MouseClicked[0]);\n        const bool double_clicked = (hovered && g.IO.MouseDoubleClicked[0]);\n        if (focus_requested || clicked || double_clicked || g.NavActivateId == id || g.NavInputId == id)\n        {\n            SetActiveID(id, window);\n            SetFocusID(id, window);\n            FocusWindow(window);\n            g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down);\n            if (focus_requested || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavInputId == id)\n            {\n                temp_input_start = true;\n                FocusableItemUnregister(window);\n            }\n        }\n    }\n    if (temp_input_is_active || temp_input_start)\n        return TempInputTextScalar(frame_bb, id, label, data_type, v, format);\n\n    // Draw frame\n    const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);\n    RenderNavHighlight(frame_bb, id);\n    RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding);\n\n    // Drag behavior\n    const bool value_changed = DragBehavior(id, data_type, v, v_speed, v_min, v_max, format, power, ImGuiDragFlags_None);\n    if (value_changed)\n        MarkItemEdited(id);\n\n    // Display value using user-provided display format so user can add prefix/suffix/decorations to the value.\n    char value_buf[64];\n    const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, v, format);\n    RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f));\n\n    if (label_size.x > 0.0f)\n        RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);\n\n    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags);\n    return value_changed;\n}\n\nbool ImGui::DragScalarN(const char* label, ImGuiDataType data_type, void* v, int components, float v_speed, const void* v_min, const void* v_max, const char* format, float power)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    bool value_changed = false;\n    BeginGroup();\n    PushID(label);\n    PushMultiItemsWidths(components, GetNextItemWidth());\n    size_t type_size = GDataTypeInfo[data_type].Size;\n    for (int i = 0; i < components; i++)\n    {\n        PushID(i);\n        value_changed |= DragScalar(\"\", data_type, v, v_speed, v_min, v_max, format, power);\n        SameLine(0, g.Style.ItemInnerSpacing.x);\n        PopID();\n        PopItemWidth();\n        v = (void*)((char*)v + type_size);\n    }\n    PopID();\n\n    TextEx(label, FindRenderedTextEnd(label));\n    EndGroup();\n    return value_changed;\n}\n\nbool ImGui::DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power)\n{\n    return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power);\n}\n\nbool ImGui::DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power)\n{\n    return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power);\n}\n\nbool ImGui::DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power)\n{\n    return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power);\n}\n\nbool ImGui::DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power)\n{\n    return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power);\n}\n\nbool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed, float v_min, float v_max, const char* format, const char* format_max, float power)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    PushID(label);\n    BeginGroup();\n    PushMultiItemsWidths(2, GetNextItemWidth());\n\n    bool value_changed = DragFloat(\"##min\", v_current_min, v_speed, (v_min >= v_max) ? -FLT_MAX : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), format, power);\n    PopItemWidth();\n    SameLine(0, g.Style.ItemInnerSpacing.x);\n    value_changed |= DragFloat(\"##max\", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? FLT_MAX : v_max, format_max ? format_max : format, power);\n    PopItemWidth();\n    SameLine(0, g.Style.ItemInnerSpacing.x);\n\n    TextEx(label, FindRenderedTextEnd(label));\n    EndGroup();\n    PopID();\n    return value_changed;\n}\n\n// NB: v_speed is float to allow adjusting the drag speed with more precision\nbool ImGui::DragInt(const char* label, int* v, float v_speed, int v_min, int v_max, const char* format)\n{\n    return DragScalar(label, ImGuiDataType_S32, v, v_speed, &v_min, &v_max, format);\n}\n\nbool ImGui::DragInt2(const char* label, int v[2], float v_speed, int v_min, int v_max, const char* format)\n{\n    return DragScalarN(label, ImGuiDataType_S32, v, 2, v_speed, &v_min, &v_max, format);\n}\n\nbool ImGui::DragInt3(const char* label, int v[3], float v_speed, int v_min, int v_max, const char* format)\n{\n    return DragScalarN(label, ImGuiDataType_S32, v, 3, v_speed, &v_min, &v_max, format);\n}\n\nbool ImGui::DragInt4(const char* label, int v[4], float v_speed, int v_min, int v_max, const char* format)\n{\n    return DragScalarN(label, ImGuiDataType_S32, v, 4, v_speed, &v_min, &v_max, format);\n}\n\nbool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed, int v_min, int v_max, const char* format, const char* format_max)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    PushID(label);\n    BeginGroup();\n    PushMultiItemsWidths(2, GetNextItemWidth());\n\n    bool value_changed = DragInt(\"##min\", v_current_min, v_speed, (v_min >= v_max) ? INT_MIN : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), format);\n    PopItemWidth();\n    SameLine(0, g.Style.ItemInnerSpacing.x);\n    value_changed |= DragInt(\"##max\", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? INT_MAX : v_max, format_max ? format_max : format);\n    PopItemWidth();\n    SameLine(0, g.Style.ItemInnerSpacing.x);\n\n    TextEx(label, FindRenderedTextEnd(label));\n    EndGroup();\n    PopID();\n\n    return value_changed;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc.\n//-------------------------------------------------------------------------\n// - SliderBehaviorT<>() [Internal]\n// - SliderBehavior() [Internal]\n// - SliderScalar()\n// - SliderScalarN()\n// - SliderFloat()\n// - SliderFloat2()\n// - SliderFloat3()\n// - SliderFloat4()\n// - SliderAngle()\n// - SliderInt()\n// - SliderInt2()\n// - SliderInt3()\n// - SliderInt4()\n// - VSliderScalar()\n// - VSliderFloat()\n// - VSliderInt()\n//-------------------------------------------------------------------------\n\ntemplate<typename TYPE, typename FLOATTYPE>\nfloat ImGui::SliderCalcRatioFromValueT(ImGuiDataType data_type, TYPE v, TYPE v_min, TYPE v_max, float power, float linear_zero_pos)\n{\n    if (v_min == v_max)\n        return 0.0f;\n\n    const bool is_power = (power != 1.0f) && (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double);\n    const TYPE v_clamped = (v_min < v_max) ? ImClamp(v, v_min, v_max) : ImClamp(v, v_max, v_min);\n    if (is_power)\n    {\n        if (v_clamped < 0.0f)\n        {\n            const float f = 1.0f - (float)((v_clamped - v_min) / (ImMin((TYPE)0, v_max) - v_min));\n            return (1.0f - ImPow(f, 1.0f/power)) * linear_zero_pos;\n        }\n        else\n        {\n            const float f = (float)((v_clamped - ImMax((TYPE)0, v_min)) / (v_max - ImMax((TYPE)0, v_min)));\n            return linear_zero_pos + ImPow(f, 1.0f/power) * (1.0f - linear_zero_pos);\n        }\n    }\n\n    // Linear slider\n    return (float)((FLOATTYPE)(v_clamped - v_min) / (FLOATTYPE)(v_max - v_min));\n}\n\n// FIXME: Move some of the code into SliderBehavior(). Current responsability is larger than what the equivalent DragBehaviorT<> does, we also do some rendering, etc.\ntemplate<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE>\nbool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, TYPE* v, const TYPE v_min, const TYPE v_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb)\n{\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n\n    const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X;\n    const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double);\n    const bool is_power = (power != 1.0f) && is_decimal;\n\n    const float grab_padding = 2.0f;\n    const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f;\n    float grab_sz = style.GrabMinSize;\n    SIGNEDTYPE v_range = (v_min < v_max ? v_max - v_min : v_min - v_max);\n    if (!is_decimal && v_range >= 0)                                             // v_range < 0 may happen on integer overflows\n        grab_sz = ImMax((float)(slider_sz / (v_range + 1)), style.GrabMinSize);  // For integer sliders: if possible have the grab size represent 1 unit\n    grab_sz = ImMin(grab_sz, slider_sz);\n    const float slider_usable_sz = slider_sz - grab_sz;\n    const float slider_usable_pos_min = bb.Min[axis] + grab_padding + grab_sz * 0.5f;\n    const float slider_usable_pos_max = bb.Max[axis] - grab_padding - grab_sz * 0.5f;\n\n    // For power curve sliders that cross over sign boundary we want the curve to be symmetric around 0.0f\n    float linear_zero_pos;   // 0.0->1.0f\n    if (is_power && v_min * v_max < 0.0f)\n    {\n        // Different sign\n        const FLOATTYPE linear_dist_min_to_0 = ImPow(v_min >= 0 ? (FLOATTYPE)v_min : -(FLOATTYPE)v_min, (FLOATTYPE)1.0f / power);\n        const FLOATTYPE linear_dist_max_to_0 = ImPow(v_max >= 0 ? (FLOATTYPE)v_max : -(FLOATTYPE)v_max, (FLOATTYPE)1.0f / power);\n        linear_zero_pos = (float)(linear_dist_min_to_0 / (linear_dist_min_to_0 + linear_dist_max_to_0));\n    }\n    else\n    {\n        // Same sign\n        linear_zero_pos = v_min < 0.0f ? 1.0f : 0.0f;\n    }\n\n    // Process interacting with the slider\n    bool value_changed = false;\n    if (g.ActiveId == id)\n    {\n        bool set_new_value = false;\n        float clicked_t = 0.0f;\n        if (g.ActiveIdSource == ImGuiInputSource_Mouse)\n        {\n            if (!g.IO.MouseDown[0])\n            {\n                ClearActiveID();\n            }\n            else\n            {\n                const float mouse_abs_pos = g.IO.MousePos[axis];\n                clicked_t = (slider_usable_sz > 0.0f) ? ImClamp((mouse_abs_pos - slider_usable_pos_min) / slider_usable_sz, 0.0f, 1.0f) : 0.0f;\n                if (axis == ImGuiAxis_Y)\n                    clicked_t = 1.0f - clicked_t;\n                set_new_value = true;\n            }\n        }\n        else if (g.ActiveIdSource == ImGuiInputSource_Nav)\n        {\n            const ImVec2 delta2 = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 0.0f, 0.0f);\n            float delta = (axis == ImGuiAxis_X) ? delta2.x : -delta2.y;\n            if (g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated)\n            {\n                ClearActiveID();\n            }\n            else if (delta != 0.0f)\n            {\n                clicked_t = SliderCalcRatioFromValueT<TYPE,FLOATTYPE>(data_type, *v, v_min, v_max, power, linear_zero_pos);\n                const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0;\n                if ((decimal_precision > 0) || is_power)\n                {\n                    delta /= 100.0f;    // Gamepad/keyboard tweak speeds in % of slider bounds\n                    if (IsNavInputDown(ImGuiNavInput_TweakSlow))\n                        delta /= 10.0f;\n                }\n                else\n                {\n                    if ((v_range >= -100.0f && v_range <= 100.0f) || IsNavInputDown(ImGuiNavInput_TweakSlow))\n                        delta = ((delta < 0.0f) ? -1.0f : +1.0f) / (float)v_range; // Gamepad/keyboard tweak speeds in integer steps\n                    else\n                        delta /= 100.0f;\n                }\n                if (IsNavInputDown(ImGuiNavInput_TweakFast))\n                    delta *= 10.0f;\n                set_new_value = true;\n                if ((clicked_t >= 1.0f && delta > 0.0f) || (clicked_t <= 0.0f && delta < 0.0f)) // This is to avoid applying the saturation when already past the limits\n                    set_new_value = false;\n                else\n                    clicked_t = ImSaturate(clicked_t + delta);\n            }\n        }\n\n        if (set_new_value)\n        {\n            TYPE v_new;\n            if (is_power)\n            {\n                // Account for power curve scale on both sides of the zero\n                if (clicked_t < linear_zero_pos)\n                {\n                    // Negative: rescale to the negative range before powering\n                    float a = 1.0f - (clicked_t / linear_zero_pos);\n                    a = ImPow(a, power);\n                    v_new = ImLerp(ImMin(v_max, (TYPE)0), v_min, a);\n                }\n                else\n                {\n                    // Positive: rescale to the positive range before powering\n                    float a;\n                    if (ImFabs(linear_zero_pos - 1.0f) > 1.e-6f)\n                        a = (clicked_t - linear_zero_pos) / (1.0f - linear_zero_pos);\n                    else\n                        a = clicked_t;\n                    a = ImPow(a, power);\n                    v_new = ImLerp(ImMax(v_min, (TYPE)0), v_max, a);\n                }\n            }\n            else\n            {\n                // Linear slider\n                if (is_decimal)\n                {\n                    v_new = ImLerp(v_min, v_max, clicked_t);\n                }\n                else\n                {\n                    // For integer values we want the clicking position to match the grab box so we round above\n                    // This code is carefully tuned to work with large values (e.g. high ranges of U64) while preserving this property..\n                    FLOATTYPE v_new_off_f = (v_max - v_min) * clicked_t;\n                    TYPE v_new_off_floor = (TYPE)(v_new_off_f);\n                    TYPE v_new_off_round = (TYPE)(v_new_off_f + (FLOATTYPE)0.5);\n                    if (!is_decimal && v_new_off_floor < v_new_off_round)\n                        v_new = v_min + v_new_off_round;\n                    else\n                        v_new = v_min + v_new_off_floor;\n                }\n            }\n\n            // Round to user desired precision based on format string\n            v_new = RoundScalarWithFormatT<TYPE,SIGNEDTYPE>(format, data_type, v_new);\n\n            // Apply result\n            if (*v != v_new)\n            {\n                *v = v_new;\n                value_changed = true;\n            }\n        }\n    }\n\n    if (slider_sz < 1.0f)\n    {\n        *out_grab_bb = ImRect(bb.Min, bb.Min);\n    }\n    else\n    {\n        // Output grab position so it can be displayed by the caller\n        float grab_t = SliderCalcRatioFromValueT<TYPE, FLOATTYPE>(data_type, *v, v_min, v_max, power, linear_zero_pos);\n        if (axis == ImGuiAxis_Y)\n            grab_t = 1.0f - grab_t;\n        const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t);\n        if (axis == ImGuiAxis_X)\n            *out_grab_bb = ImRect(grab_pos - grab_sz * 0.5f, bb.Min.y + grab_padding, grab_pos + grab_sz * 0.5f, bb.Max.y - grab_padding);\n        else\n            *out_grab_bb = ImRect(bb.Min.x + grab_padding, grab_pos - grab_sz * 0.5f, bb.Max.x - grab_padding, grab_pos + grab_sz * 0.5f);\n    }\n\n    return value_changed;\n}\n\n// For 32-bits and larger types, slider bounds are limited to half the natural type range.\n// So e.g. an integer Slider between INT_MAX-10 and INT_MAX will fail, but an integer Slider between INT_MAX/2-10 and INT_MAX/2 will be ok.\n// It would be possible to lift that limitation with some work but it doesn't seem to be worth it for sliders.\nbool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb)\n{\n    switch (data_type)\n    {\n    case ImGuiDataType_S8:  { ImS32 v32 = (ImS32)*(ImS8*)v;  bool r = SliderBehaviorT<ImS32, ImS32, float >(bb, id, ImGuiDataType_S32, &v32, *(const ImS8*)v_min,  *(const ImS8*)v_max,  format, power, flags, out_grab_bb); if (r) *(ImS8*)v  = (ImS8)v32;  return r; }\n    case ImGuiDataType_U8:  { ImU32 v32 = (ImU32)*(ImU8*)v;  bool r = SliderBehaviorT<ImU32, ImS32, float >(bb, id, ImGuiDataType_U32, &v32, *(const ImU8*)v_min,  *(const ImU8*)v_max,  format, power, flags, out_grab_bb); if (r) *(ImU8*)v  = (ImU8)v32;  return r; }\n    case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)v; bool r = SliderBehaviorT<ImS32, ImS32, float >(bb, id, ImGuiDataType_S32, &v32, *(const ImS16*)v_min, *(const ImS16*)v_max, format, power, flags, out_grab_bb); if (r) *(ImS16*)v = (ImS16)v32; return r; }\n    case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)v; bool r = SliderBehaviorT<ImU32, ImS32, float >(bb, id, ImGuiDataType_U32, &v32, *(const ImU16*)v_min, *(const ImU16*)v_max, format, power, flags, out_grab_bb); if (r) *(ImU16*)v = (ImU16)v32; return r; }\n    case ImGuiDataType_S32:\n        IM_ASSERT(*(const ImS32*)v_min >= IM_S32_MIN/2 && *(const ImS32*)v_max <= IM_S32_MAX/2);\n        return SliderBehaviorT<ImS32, ImS32, float >(bb, id, data_type, (ImS32*)v,  *(const ImS32*)v_min,  *(const ImS32*)v_max,  format, power, flags, out_grab_bb);\n    case ImGuiDataType_U32:\n        IM_ASSERT(*(const ImU32*)v_min <= IM_U32_MAX/2);\n        return SliderBehaviorT<ImU32, ImS32, float >(bb, id, data_type, (ImU32*)v,  *(const ImU32*)v_min,  *(const ImU32*)v_max,  format, power, flags, out_grab_bb);\n    case ImGuiDataType_S64:\n        IM_ASSERT(*(const ImS64*)v_min >= IM_S64_MIN/2 && *(const ImS64*)v_max <= IM_S64_MAX/2);\n        return SliderBehaviorT<ImS64, ImS64, double>(bb, id, data_type, (ImS64*)v,  *(const ImS64*)v_min,  *(const ImS64*)v_max,  format, power, flags, out_grab_bb);\n    case ImGuiDataType_U64:\n        IM_ASSERT(*(const ImU64*)v_min <= IM_U64_MAX/2);\n        return SliderBehaviorT<ImU64, ImS64, double>(bb, id, data_type, (ImU64*)v,  *(const ImU64*)v_min,  *(const ImU64*)v_max,  format, power, flags, out_grab_bb);\n    case ImGuiDataType_Float:\n        IM_ASSERT(*(const float*)v_min >= -FLT_MAX/2.0f && *(const float*)v_max <= FLT_MAX/2.0f);\n        return SliderBehaviorT<float, float, float >(bb, id, data_type, (float*)v,  *(const float*)v_min,  *(const float*)v_max,  format, power, flags, out_grab_bb);\n    case ImGuiDataType_Double:\n        IM_ASSERT(*(const double*)v_min >= -DBL_MAX/2.0f && *(const double*)v_max <= DBL_MAX/2.0f);\n        return SliderBehaviorT<double,double,double>(bb, id, data_type, (double*)v, *(const double*)v_min, *(const double*)v_max, format, power, flags, out_grab_bb);\n    case ImGuiDataType_COUNT: break;\n    }\n    IM_ASSERT(0);\n    return false;\n}\n\nbool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format, float power)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = window->GetID(label);\n    const float w = GetNextItemWidth();\n\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n    const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f));\n    const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));\n\n    ItemSize(total_bb, style.FramePadding.y);\n    if (!ItemAdd(total_bb, id, &frame_bb))\n        return false;\n\n    // Default format string when passing NULL\n    if (format == NULL)\n        format = DataTypeGetInfo(data_type)->PrintFmt;\n    else if (data_type == ImGuiDataType_S32 && strcmp(format, \"%d\") != 0) // (FIXME-LEGACY: Patch old \"%.0f\" format string to use \"%d\", read function more details.)\n        format = PatchFormatStringFloatToInt(format);\n\n    // Tabbing or CTRL-clicking on Slider turns it into an input box\n    const bool hovered = ItemHoverable(frame_bb, id);\n    bool temp_input_is_active = TempInputTextIsActive(id);\n    bool temp_input_start = false;\n    if (!temp_input_is_active)\n    {\n        const bool focus_requested = FocusableItemRegister(window, id);\n        const bool clicked = (hovered && g.IO.MouseClicked[0]);\n        if (focus_requested || clicked || g.NavActivateId == id || g.NavInputId == id)\n        {\n            SetActiveID(id, window);\n            SetFocusID(id, window);\n            FocusWindow(window);\n            g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down);\n            if (focus_requested || (clicked && g.IO.KeyCtrl) || g.NavInputId == id)\n            {\n                temp_input_start = true;\n                FocusableItemUnregister(window);\n            }\n        }\n    }\n    if (temp_input_is_active || temp_input_start)\n        return TempInputTextScalar(frame_bb, id, label, data_type, v, format);\n\n    // Draw frame\n    const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);\n    RenderNavHighlight(frame_bb, id);\n    RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding);\n\n    // Slider behavior\n    ImRect grab_bb;\n    const bool value_changed = SliderBehavior(frame_bb, id, data_type, v, v_min, v_max, format, power, ImGuiSliderFlags_None, &grab_bb);\n    if (value_changed)\n        MarkItemEdited(id);\n\n    // Render grab\n    if (grab_bb.Max.x > grab_bb.Min.x)\n        window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding);\n\n    // Display value using user-provided display format so user can add prefix/suffix/decorations to the value.\n    char value_buf[64];\n    const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, v, format);\n    RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f,0.5f));\n\n    if (label_size.x > 0.0f)\n        RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);\n\n    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags);\n    return value_changed;\n}\n\n// Add multiple sliders on 1 line for compact edition of multiple components\nbool ImGui::SliderScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format, float power)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    bool value_changed = false;\n    BeginGroup();\n    PushID(label);\n    PushMultiItemsWidths(components, GetNextItemWidth());\n    size_t type_size = GDataTypeInfo[data_type].Size;\n    for (int i = 0; i < components; i++)\n    {\n        PushID(i);\n        value_changed |= SliderScalar(\"\", data_type, v, v_min, v_max, format, power);\n        SameLine(0, g.Style.ItemInnerSpacing.x);\n        PopID();\n        PopItemWidth();\n        v = (void*)((char*)v + type_size);\n    }\n    PopID();\n\n    TextEx(label, FindRenderedTextEnd(label));\n    EndGroup();\n    return value_changed;\n}\n\nbool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power)\n{\n    return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power);\n}\n\nbool ImGui::SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power)\n{\n    return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power);\n}\n\nbool ImGui::SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power)\n{\n    return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power);\n}\n\nbool ImGui::SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power)\n{\n    return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power);\n}\n\nbool ImGui::SliderAngle(const char* label, float* v_rad, float v_degrees_min, float v_degrees_max, const char* format)\n{\n    if (format == NULL)\n        format = \"%.0f deg\";\n    float v_deg = (*v_rad) * 360.0f / (2*IM_PI);\n    bool value_changed = SliderFloat(label, &v_deg, v_degrees_min, v_degrees_max, format, 1.0f);\n    *v_rad = v_deg * (2*IM_PI) / 360.0f;\n    return value_changed;\n}\n\nbool ImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const char* format)\n{\n    return SliderScalar(label, ImGuiDataType_S32, v, &v_min, &v_max, format);\n}\n\nbool ImGui::SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format)\n{\n    return SliderScalarN(label, ImGuiDataType_S32, v, 2, &v_min, &v_max, format);\n}\n\nbool ImGui::SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format)\n{\n    return SliderScalarN(label, ImGuiDataType_S32, v, 3, &v_min, &v_max, format);\n}\n\nbool ImGui::SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format)\n{\n    return SliderScalarN(label, ImGuiDataType_S32, v, 4, &v_min, &v_max, format);\n}\n\nbool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format, float power)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = window->GetID(label);\n\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n    const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size);\n    const ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));\n\n    ItemSize(bb, style.FramePadding.y);\n    if (!ItemAdd(frame_bb, id))\n        return false;\n\n    // Default format string when passing NULL\n    if (format == NULL)\n        format = DataTypeGetInfo(data_type)->PrintFmt;\n    else if (data_type == ImGuiDataType_S32 && strcmp(format, \"%d\") != 0) // (FIXME-LEGACY: Patch old \"%.0f\" format string to use \"%d\", read function more details.)\n        format = PatchFormatStringFloatToInt(format);\n\n    const bool hovered = ItemHoverable(frame_bb, id);\n    if ((hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || g.NavInputId == id)\n    {\n        SetActiveID(id, window);\n        SetFocusID(id, window);\n        FocusWindow(window);\n        g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right);\n    }\n\n    // Draw frame\n    const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);\n    RenderNavHighlight(frame_bb, id);\n    RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding);\n\n    // Slider behavior\n    ImRect grab_bb;\n    const bool value_changed = SliderBehavior(frame_bb, id, data_type, v, v_min, v_max, format, power, ImGuiSliderFlags_Vertical, &grab_bb);\n    if (value_changed)\n        MarkItemEdited(id);\n\n    // Render grab\n    if (grab_bb.Max.y > grab_bb.Min.y)\n        window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding);\n\n    // Display value using user-provided display format so user can add prefix/suffix/decorations to the value.\n    // For the vertical slider we allow centered text to overlap the frame padding\n    char value_buf[64];\n    const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, v, format);\n    RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f,0.0f));\n    if (label_size.x > 0.0f)\n        RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);\n\n    return value_changed;\n}\n\nbool ImGui::VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format, float power)\n{\n    return VSliderScalar(label, size, ImGuiDataType_Float, v, &v_min, &v_max, format, power);\n}\n\nbool ImGui::VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format)\n{\n    return VSliderScalar(label, size, ImGuiDataType_S32, v, &v_min, &v_max, format);\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: InputScalar, InputFloat, InputInt, etc.\n//-------------------------------------------------------------------------\n// - ImParseFormatFindStart() [Internal]\n// - ImParseFormatFindEnd() [Internal]\n// - ImParseFormatTrimDecorations() [Internal]\n// - ImParseFormatPrecision() [Internal]\n// - TempInputTextScalar() [Internal]\n// - InputScalar()\n// - InputScalarN()\n// - InputFloat()\n// - InputFloat2()\n// - InputFloat3()\n// - InputFloat4()\n// - InputInt()\n// - InputInt2()\n// - InputInt3()\n// - InputInt4()\n// - InputDouble()\n//-------------------------------------------------------------------------\n\n// We don't use strchr() because our strings are usually very short and often start with '%'\nconst char* ImParseFormatFindStart(const char* fmt)\n{\n    while (char c = fmt[0])\n    {\n        if (c == '%' && fmt[1] != '%')\n            return fmt;\n        else if (c == '%')\n            fmt++;\n        fmt++;\n    }\n    return fmt;\n}\n\nconst char* ImParseFormatFindEnd(const char* fmt)\n{\n    // Printf/scanf types modifiers: I/L/h/j/l/t/w/z. Other uppercase letters qualify as types aka end of the format.\n    if (fmt[0] != '%')\n        return fmt;\n    const unsigned int ignored_uppercase_mask = (1 << ('I'-'A')) | (1 << ('L'-'A'));\n    const unsigned int ignored_lowercase_mask = (1 << ('h'-'a')) | (1 << ('j'-'a')) | (1 << ('l'-'a')) | (1 << ('t'-'a')) | (1 << ('w'-'a')) | (1 << ('z'-'a'));\n    for (char c; (c = *fmt) != 0; fmt++)\n    {\n        if (c >= 'A' && c <= 'Z' && ((1 << (c - 'A')) & ignored_uppercase_mask) == 0)\n            return fmt + 1;\n        if (c >= 'a' && c <= 'z' && ((1 << (c - 'a')) & ignored_lowercase_mask) == 0)\n            return fmt + 1;\n    }\n    return fmt;\n}\n\n// Extract the format out of a format string with leading or trailing decorations\n//  fmt = \"blah blah\"  -> return fmt\n//  fmt = \"%.3f\"       -> return fmt\n//  fmt = \"hello %.3f\" -> return fmt + 6\n//  fmt = \"%.3f hello\" -> return buf written with \"%.3f\"\nconst char* ImParseFormatTrimDecorations(const char* fmt, char* buf, size_t buf_size)\n{\n    const char* fmt_start = ImParseFormatFindStart(fmt);\n    if (fmt_start[0] != '%')\n        return fmt;\n    const char* fmt_end = ImParseFormatFindEnd(fmt_start);\n    if (fmt_end[0] == 0) // If we only have leading decoration, we don't need to copy the data.\n        return fmt_start;\n    ImStrncpy(buf, fmt_start, ImMin((size_t)(fmt_end - fmt_start) + 1, buf_size));\n    return buf;\n}\n\n// Parse display precision back from the display format string\n// FIXME: This is still used by some navigation code path to infer a minimum tweak step, but we should aim to rework widgets so it isn't needed.\nint ImParseFormatPrecision(const char* fmt, int default_precision)\n{\n    fmt = ImParseFormatFindStart(fmt);\n    if (fmt[0] != '%')\n        return default_precision;\n    fmt++;\n    while (*fmt >= '0' && *fmt <= '9')\n        fmt++;\n    int precision = INT_MAX;\n    if (*fmt == '.')\n    {\n        fmt = ImAtoi<int>(fmt + 1, &precision);\n        if (precision < 0 || precision > 99)\n            precision = default_precision;\n    }\n    if (*fmt == 'e' || *fmt == 'E') // Maximum precision with scientific notation\n        precision = -1;\n    if ((*fmt == 'g' || *fmt == 'G') && precision == INT_MAX)\n        precision = -1;\n    return (precision == INT_MAX) ? default_precision : precision;\n}\n\n// Create text input in place of another active widget (e.g. used when doing a CTRL+Click on drag/slider widgets)\n// FIXME: Facilitate using this in variety of other situations.\nbool ImGui::TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* data_ptr, const char* format)\n{\n    ImGuiContext& g = *GImGui;\n\n    // On the first frame, g.TempInputTextId == 0, then on subsequent frames it becomes == id.\n    // We clear ActiveID on the first frame to allow the InputText() taking it back.\n    const bool init = (g.TempInputTextId != id);\n    if (init)\n        ClearActiveID();\n\n    char fmt_buf[32];\n    char data_buf[32];\n    format = ImParseFormatTrimDecorations(format, fmt_buf, IM_ARRAYSIZE(fmt_buf));\n    DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, data_ptr, format);\n    ImStrTrimBlanks(data_buf);\n\n    g.CurrentWindow->DC.CursorPos = bb.Min;\n    ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ((data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) ? ImGuiInputTextFlags_CharsScientific : ImGuiInputTextFlags_CharsDecimal);\n    bool value_changed = InputTextEx(label, NULL, data_buf, IM_ARRAYSIZE(data_buf), bb.GetSize(), flags);\n    if (init)\n    {\n        // First frame we started displaying the InputText widget, we expect it to take the active id.\n        IM_ASSERT(g.ActiveId == id);\n        g.TempInputTextId = g.ActiveId;\n    }\n    if (value_changed)\n        return DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialTextA.Data, data_type, data_ptr, NULL);\n    return false;\n}\n\nbool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* data_ptr, const void* step, const void* step_fast, const char* format, ImGuiInputTextFlags flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    ImGuiStyle& style = g.Style;\n\n    if (format == NULL)\n        format = DataTypeGetInfo(data_type)->PrintFmt;\n\n    char buf[64];\n    DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, data_ptr, format);\n\n    bool value_changed = false;\n    if ((flags & (ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0)\n        flags |= ImGuiInputTextFlags_CharsDecimal;\n    flags |= ImGuiInputTextFlags_AutoSelectAll;\n\n    if (step != NULL)\n    {\n        const float button_size = GetFrameHeight();\n\n        BeginGroup(); // The only purpose of the group here is to allow the caller to query item data e.g. IsItemActive()\n        PushID(label);\n        SetNextItemWidth(ImMax(1.0f, GetNextItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));\n        if (InputText(\"\", buf, IM_ARRAYSIZE(buf), flags)) // PushId(label) + \"\" gives us the expected ID from outside point of view\n            value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, data_ptr, format);\n\n        // Step buttons\n        const ImVec2 backup_frame_padding = style.FramePadding;\n        style.FramePadding.x = style.FramePadding.y;\n        ImGuiButtonFlags button_flags = ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups;\n        if (flags & ImGuiInputTextFlags_ReadOnly)\n            button_flags |= ImGuiButtonFlags_Disabled;\n        SameLine(0, style.ItemInnerSpacing.x);\n        if (ButtonEx(\"-\", ImVec2(button_size, button_size), button_flags))\n        {\n            DataTypeApplyOp(data_type, '-', data_ptr, data_ptr, g.IO.KeyCtrl && step_fast ? step_fast : step);\n            value_changed = true;\n        }\n        SameLine(0, style.ItemInnerSpacing.x);\n        if (ButtonEx(\"+\", ImVec2(button_size, button_size), button_flags))\n        {\n            DataTypeApplyOp(data_type, '+', data_ptr, data_ptr, g.IO.KeyCtrl && step_fast ? step_fast : step);\n            value_changed = true;\n        }\n        SameLine(0, style.ItemInnerSpacing.x);\n        TextEx(label, FindRenderedTextEnd(label));\n        style.FramePadding = backup_frame_padding;\n\n        PopID();\n        EndGroup();\n    }\n    else\n    {\n        if (InputText(label, buf, IM_ARRAYSIZE(buf), flags))\n            value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, data_ptr, format);\n    }\n\n    return value_changed;\n}\n\nbool ImGui::InputScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* step, const void* step_fast, const char* format, ImGuiInputTextFlags flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    bool value_changed = false;\n    BeginGroup();\n    PushID(label);\n    PushMultiItemsWidths(components, GetNextItemWidth());\n    size_t type_size = GDataTypeInfo[data_type].Size;\n    for (int i = 0; i < components; i++)\n    {\n        PushID(i);\n        value_changed |= InputScalar(\"\", data_type, v, step, step_fast, format, flags);\n        SameLine(0, g.Style.ItemInnerSpacing.x);\n        PopID();\n        PopItemWidth();\n        v = (void*)((char*)v + type_size);\n    }\n    PopID();\n\n    TextEx(label, FindRenderedTextEnd(label));\n    EndGroup();\n    return value_changed;\n}\n\nbool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, const char* format, ImGuiInputTextFlags flags)\n{\n    flags |= ImGuiInputTextFlags_CharsScientific;\n    return InputScalar(label, ImGuiDataType_Float, (void*)v, (void*)(step>0.0f ? &step : NULL), (void*)(step_fast>0.0f ? &step_fast : NULL), format, flags);\n}\n\nbool ImGui::InputFloat2(const char* label, float v[2], const char* format, ImGuiInputTextFlags flags)\n{\n    return InputScalarN(label, ImGuiDataType_Float, v, 2, NULL, NULL, format, flags);\n}\n\nbool ImGui::InputFloat3(const char* label, float v[3], const char* format, ImGuiInputTextFlags flags)\n{\n    return InputScalarN(label, ImGuiDataType_Float, v, 3, NULL, NULL, format, flags);\n}\n\nbool ImGui::InputFloat4(const char* label, float v[4], const char* format, ImGuiInputTextFlags flags)\n{\n    return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, flags);\n}\n\n// Prefer using \"const char* format\" directly, which is more flexible and consistent with other API.\n#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS\nbool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags flags)\n{\n    char format[16] = \"%f\";\n    if (decimal_precision >= 0)\n        ImFormatString(format, IM_ARRAYSIZE(format), \"%%.%df\", decimal_precision);\n    return InputFloat(label, v, step, step_fast, format, flags);\n}\n\nbool ImGui::InputFloat2(const char* label, float v[2], int decimal_precision, ImGuiInputTextFlags flags)\n{\n    char format[16] = \"%f\";\n    if (decimal_precision >= 0)\n        ImFormatString(format, IM_ARRAYSIZE(format), \"%%.%df\", decimal_precision);\n    return InputScalarN(label, ImGuiDataType_Float, v, 2, NULL, NULL, format, flags);\n}\n\nbool ImGui::InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags flags)\n{\n    char format[16] = \"%f\";\n    if (decimal_precision >= 0)\n        ImFormatString(format, IM_ARRAYSIZE(format), \"%%.%df\", decimal_precision);\n    return InputScalarN(label, ImGuiDataType_Float, v, 3, NULL, NULL, format, flags);\n}\n\nbool ImGui::InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags flags)\n{\n    char format[16] = \"%f\";\n    if (decimal_precision >= 0)\n        ImFormatString(format, IM_ARRAYSIZE(format), \"%%.%df\", decimal_precision);\n    return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, flags);\n}\n#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS\n\nbool ImGui::InputInt(const char* label, int* v, int step, int step_fast, ImGuiInputTextFlags flags)\n{\n    // Hexadecimal input provided as a convenience but the flag name is awkward. Typically you'd use InputText() to parse your own data, if you want to handle prefixes.\n    const char* format = (flags & ImGuiInputTextFlags_CharsHexadecimal) ? \"%08X\" : \"%d\";\n    return InputScalar(label, ImGuiDataType_S32, (void*)v, (void*)(step>0 ? &step : NULL), (void*)(step_fast>0 ? &step_fast : NULL), format, flags);\n}\n\nbool ImGui::InputInt2(const char* label, int v[2], ImGuiInputTextFlags flags)\n{\n    return InputScalarN(label, ImGuiDataType_S32, v, 2, NULL, NULL, \"%d\", flags);\n}\n\nbool ImGui::InputInt3(const char* label, int v[3], ImGuiInputTextFlags flags)\n{\n    return InputScalarN(label, ImGuiDataType_S32, v, 3, NULL, NULL, \"%d\", flags);\n}\n\nbool ImGui::InputInt4(const char* label, int v[4], ImGuiInputTextFlags flags)\n{\n    return InputScalarN(label, ImGuiDataType_S32, v, 4, NULL, NULL, \"%d\", flags);\n}\n\nbool ImGui::InputDouble(const char* label, double* v, double step, double step_fast, const char* format, ImGuiInputTextFlags flags)\n{\n    flags |= ImGuiInputTextFlags_CharsScientific;\n    return InputScalar(label, ImGuiDataType_Double, (void*)v, (void*)(step>0.0 ? &step : NULL), (void*)(step_fast>0.0 ? &step_fast : NULL), format, flags);\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: InputText, InputTextMultiline, InputTextWithHint\n//-------------------------------------------------------------------------\n// - InputText()\n// - InputTextWithHint()\n// - InputTextMultiline()\n// - InputTextEx() [Internal]\n//-------------------------------------------------------------------------\n\nbool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)\n{\n    IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline()\n    return InputTextEx(label, NULL, buf, (int)buf_size, ImVec2(0,0), flags, callback, user_data);\n}\n\nbool ImGui::InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)\n{\n    return InputTextEx(label, NULL, buf, (int)buf_size, size, flags | ImGuiInputTextFlags_Multiline, callback, user_data);\n}\n\nbool ImGui::InputTextWithHint(const char* label, const char* hint, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)\n{\n    IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline()\n    return InputTextEx(label, hint, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data);\n}\n\nstatic int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end)\n{\n    int line_count = 0;\n    const char* s = text_begin;\n    while (char c = *s++) // We are only matching for \\n so we can ignore UTF-8 decoding\n        if (c == '\\n')\n            line_count++;\n    s--;\n    if (s[0] != '\\n' && s[0] != '\\r')\n        line_count++;\n    *out_text_end = s;\n    return line_count;\n}\n\nstatic ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining, ImVec2* out_offset, bool stop_on_new_line)\n{\n    ImGuiContext& g = *GImGui;\n    ImFont* font = g.Font;\n    const float line_height = g.FontSize;\n    const float scale = line_height / font->FontSize;\n\n    ImVec2 text_size = ImVec2(0,0);\n    float line_width = 0.0f;\n\n    const ImWchar* s = text_begin;\n    while (s < text_end)\n    {\n        unsigned int c = (unsigned int)(*s++);\n        if (c == '\\n')\n        {\n            text_size.x = ImMax(text_size.x, line_width);\n            text_size.y += line_height;\n            line_width = 0.0f;\n            if (stop_on_new_line)\n                break;\n            continue;\n        }\n        if (c == '\\r')\n            continue;\n\n        const float char_width = font->GetCharAdvance((ImWchar)c) * scale;\n        line_width += char_width;\n    }\n\n    if (text_size.x < line_width)\n        text_size.x = line_width;\n\n    if (out_offset)\n        *out_offset = ImVec2(line_width, text_size.y + line_height);  // offset allow for the possibility of sitting after a trailing \\n\n\n    if (line_width > 0 || text_size.y == 0.0f)                        // whereas size.y will ignore the trailing \\n\n        text_size.y += line_height;\n\n    if (remaining)\n        *remaining = s;\n\n    return text_size;\n}\n\n// Wrapper for stb_textedit.h to edit text (our wrapper is for: statically sized buffer, single-line, wchar characters. InputText converts between UTF-8 and wchar)\nnamespace ImStb\n{\n\nstatic int     STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING* obj)                             { return obj->CurLenW; }\nstatic ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING* obj, int idx)                      { return obj->TextW[idx]; }\nstatic float   STB_TEXTEDIT_GETWIDTH(STB_TEXTEDIT_STRING* obj, int line_start_idx, int char_idx)  { ImWchar c = obj->TextW[line_start_idx+char_idx]; if (c == '\\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; return GImGui->Font->GetCharAdvance(c) * (GImGui->FontSize / GImGui->Font->FontSize); }\nstatic int     STB_TEXTEDIT_KEYTOTEXT(int key)                                                    { return key >= 0x10000 ? 0 : key; }\nstatic ImWchar STB_TEXTEDIT_NEWLINE = '\\n';\nstatic void    STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx)\n{\n    const ImWchar* text = obj->TextW.Data;\n    const ImWchar* text_remaining = NULL;\n    const ImVec2 size = InputTextCalcTextSizeW(text + line_start_idx, text + obj->CurLenW, &text_remaining, NULL, true);\n    r->x0 = 0.0f;\n    r->x1 = size.x;\n    r->baseline_y_delta = size.y;\n    r->ymin = 0.0f;\n    r->ymax = size.y;\n    r->num_chars = (int)(text_remaining - (text + line_start_idx));\n}\n\nstatic bool is_separator(unsigned int c)                                        { return ImCharIsBlankW(c) || c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; }\nstatic int  is_word_boundary_from_right(STB_TEXTEDIT_STRING* obj, int idx)      { return idx > 0 ? (is_separator( obj->TextW[idx-1] ) && !is_separator( obj->TextW[idx] ) ) : 1; }\nstatic int  STB_TEXTEDIT_MOVEWORDLEFT_IMPL(STB_TEXTEDIT_STRING* obj, int idx)   { idx--; while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; }\n#ifdef __APPLE__    // FIXME: Move setting to IO structure\nstatic int  is_word_boundary_from_left(STB_TEXTEDIT_STRING* obj, int idx)       { return idx > 0 ? (!is_separator( obj->TextW[idx-1] ) && is_separator( obj->TextW[idx] ) ) : 1; }\nstatic int  STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(STB_TEXTEDIT_STRING* obj, int idx)  { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; }\n#else\nstatic int  STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(STB_TEXTEDIT_STRING* obj, int idx)  { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; }\n#endif\n#define STB_TEXTEDIT_MOVEWORDLEFT   STB_TEXTEDIT_MOVEWORDLEFT_IMPL    // They need to be #define for stb_textedit.h\n#define STB_TEXTEDIT_MOVEWORDRIGHT  STB_TEXTEDIT_MOVEWORDRIGHT_IMPL\n\nstatic void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int pos, int n)\n{\n    ImWchar* dst = obj->TextW.Data + pos;\n\n    // We maintain our buffer length in both UTF-8 and wchar formats\n    obj->CurLenA -= ImTextCountUtf8BytesFromStr(dst, dst + n);\n    obj->CurLenW -= n;\n\n    // Offset remaining text (FIXME-OPT: Use memmove)\n    const ImWchar* src = obj->TextW.Data + pos + n;\n    while (ImWchar c = *src++)\n        *dst++ = c;\n    *dst = '\\0';\n}\n\nstatic bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const ImWchar* new_text, int new_text_len)\n{\n    const bool is_resizable = (obj->UserFlags & ImGuiInputTextFlags_CallbackResize) != 0;\n    const int text_len = obj->CurLenW;\n    IM_ASSERT(pos <= text_len);\n\n    const int new_text_len_utf8 = ImTextCountUtf8BytesFromStr(new_text, new_text + new_text_len);\n    if (!is_resizable && (new_text_len_utf8 + obj->CurLenA + 1 > obj->BufCapacityA))\n        return false;\n\n    // Grow internal buffer if needed\n    if (new_text_len + text_len + 1 > obj->TextW.Size)\n    {\n        if (!is_resizable)\n            return false;\n        IM_ASSERT(text_len < obj->TextW.Size);\n        obj->TextW.resize(text_len + ImClamp(new_text_len * 4, 32, ImMax(256, new_text_len)) + 1);\n    }\n\n    ImWchar* text = obj->TextW.Data;\n    if (pos != text_len)\n        memmove(text + pos + new_text_len, text + pos, (size_t)(text_len - pos) * sizeof(ImWchar));\n    memcpy(text + pos, new_text, (size_t)new_text_len * sizeof(ImWchar));\n\n    obj->CurLenW += new_text_len;\n    obj->CurLenA += new_text_len_utf8;\n    obj->TextW[obj->CurLenW] = '\\0';\n\n    return true;\n}\n\n// We don't use an enum so we can build even with conflicting symbols (if another user of stb_textedit.h leak their STB_TEXTEDIT_K_* symbols)\n#define STB_TEXTEDIT_K_LEFT         0x10000 // keyboard input to move cursor left\n#define STB_TEXTEDIT_K_RIGHT        0x10001 // keyboard input to move cursor right\n#define STB_TEXTEDIT_K_UP           0x10002 // keyboard input to move cursor up\n#define STB_TEXTEDIT_K_DOWN         0x10003 // keyboard input to move cursor down\n#define STB_TEXTEDIT_K_LINESTART    0x10004 // keyboard input to move cursor to start of line\n#define STB_TEXTEDIT_K_LINEEND      0x10005 // keyboard input to move cursor to end of line\n#define STB_TEXTEDIT_K_TEXTSTART    0x10006 // keyboard input to move cursor to start of text\n#define STB_TEXTEDIT_K_TEXTEND      0x10007 // keyboard input to move cursor to end of text\n#define STB_TEXTEDIT_K_DELETE       0x10008 // keyboard input to delete selection or character under cursor\n#define STB_TEXTEDIT_K_BACKSPACE    0x10009 // keyboard input to delete selection or character left of cursor\n#define STB_TEXTEDIT_K_UNDO         0x1000A // keyboard input to perform undo\n#define STB_TEXTEDIT_K_REDO         0x1000B // keyboard input to perform redo\n#define STB_TEXTEDIT_K_WORDLEFT     0x1000C // keyboard input to move cursor left one word\n#define STB_TEXTEDIT_K_WORDRIGHT    0x1000D // keyboard input to move cursor right one word\n#define STB_TEXTEDIT_K_SHIFT        0x20000\n\n#define STB_TEXTEDIT_IMPLEMENTATION\n#include \"imstb_textedit.hpp\"\n\n}\n\nvoid ImGuiInputTextState::OnKeyPressed(int key)\n{\n    stb_textedit_key(this, &Stb, key);\n    CursorFollow = true;\n    CursorAnimReset();\n}\n\nImGuiInputTextCallbackData::ImGuiInputTextCallbackData()\n{\n    memset(this, 0, sizeof(*this));\n}\n\n// Public API to manipulate UTF-8 text\n// We expose UTF-8 to the user (unlike the STB_TEXTEDIT_* functions which are manipulating wchar)\n// FIXME: The existence of this rarely exercised code path is a bit of a nuisance.\nvoid ImGuiInputTextCallbackData::DeleteChars(int pos, int bytes_count)\n{\n    IM_ASSERT(pos + bytes_count <= BufTextLen);\n    char* dst = Buf + pos;\n    const char* src = Buf + pos + bytes_count;\n    while (char c = *src++)\n        *dst++ = c;\n    *dst = '\\0';\n\n    if (CursorPos + bytes_count >= pos)\n        CursorPos -= bytes_count;\n    else if (CursorPos >= pos)\n        CursorPos = pos;\n    SelectionStart = SelectionEnd = CursorPos;\n    BufDirty = true;\n    BufTextLen -= bytes_count;\n}\n\nvoid ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, const char* new_text_end)\n{\n    const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0;\n    const int new_text_len = new_text_end ? (int)(new_text_end - new_text) : (int)strlen(new_text);\n    if (new_text_len + BufTextLen >= BufSize)\n    {\n        if (!is_resizable)\n            return;\n\n        // Contrary to STB_TEXTEDIT_INSERTCHARS() this is working in the UTF8 buffer, hence the midly similar code (until we remove the U16 buffer alltogether!)\n        ImGuiContext& g = *GImGui;\n        ImGuiInputTextState* edit_state = &g.InputTextState;\n        IM_ASSERT(edit_state->ID != 0 && g.ActiveId == edit_state->ID);\n        IM_ASSERT(Buf == edit_state->TextA.Data);\n        int new_buf_size = BufTextLen + ImClamp(new_text_len * 4, 32, ImMax(256, new_text_len)) + 1;\n        edit_state->TextA.reserve(new_buf_size + 1);\n        Buf = edit_state->TextA.Data;\n        BufSize = edit_state->BufCapacityA = new_buf_size;\n    }\n\n    if (BufTextLen != pos)\n        memmove(Buf + pos + new_text_len, Buf + pos, (size_t)(BufTextLen - pos));\n    memcpy(Buf + pos, new_text, (size_t)new_text_len * sizeof(char));\n    Buf[BufTextLen + new_text_len] = '\\0';\n\n    if (CursorPos >= pos)\n        CursorPos += new_text_len;\n    SelectionStart = SelectionEnd = CursorPos;\n    BufDirty = true;\n    BufTextLen += new_text_len;\n}\n\n// Return false to discard a character.\nstatic bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)\n{\n    unsigned int c = *p_char;\n\n    // Filter non-printable (NB: isprint is unreliable! see #2467)\n    if (c < 0x20)\n    {\n        bool pass = false;\n        pass |= (c == '\\n' && (flags & ImGuiInputTextFlags_Multiline));\n        pass |= (c == '\\t' && (flags & ImGuiInputTextFlags_AllowTabInput));\n        if (!pass)\n            return false;\n    }\n\n    // Filter private Unicode range. GLFW on OSX seems to send private characters for special keys like arrow keys (FIXME)\n    if (c >= 0xE000 && c <= 0xF8FF)\n        return false;\n\n    // Generic named filters\n    if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific))\n    {\n        if (flags & ImGuiInputTextFlags_CharsDecimal)\n            if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/'))\n                return false;\n\n        if (flags & ImGuiInputTextFlags_CharsScientific)\n            if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/') && (c != 'e') && (c != 'E'))\n                return false;\n\n        if (flags & ImGuiInputTextFlags_CharsHexadecimal)\n            if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F'))\n                return false;\n\n        if (flags & ImGuiInputTextFlags_CharsUppercase)\n            if (c >= 'a' && c <= 'z')\n                *p_char = (c += (unsigned int)('A'-'a'));\n\n        if (flags & ImGuiInputTextFlags_CharsNoBlank)\n            if (ImCharIsBlankW(c))\n                return false;\n    }\n\n    // Custom callback filter\n    if (flags & ImGuiInputTextFlags_CallbackCharFilter)\n    {\n        ImGuiInputTextCallbackData callback_data;\n        memset(&callback_data, 0, sizeof(ImGuiInputTextCallbackData));\n        callback_data.EventFlag = ImGuiInputTextFlags_CallbackCharFilter;\n        callback_data.EventChar = (ImWchar)c;\n        callback_data.Flags = flags;\n        callback_data.UserData = user_data;\n        if (callback(&callback_data) != 0)\n            return false;\n        *p_char = callback_data.EventChar;\n        if (!callback_data.EventChar)\n            return false;\n    }\n\n    return true;\n}\n\n// Edit a string of text\n// - buf_size account for the zero-terminator, so a buf_size of 6 can hold \"Hello\" but not \"Hello!\".\n//   This is so we can easily call InputText() on static arrays using ARRAYSIZE() and to match\n//   Note that in std::string world, capacity() would omit 1 byte used by the zero-terminator.\n// - When active, hold on a privately held copy of the text (and apply back to 'buf'). So changing 'buf' while the InputText is active has no effect.\n// - If you want to use ImGui::InputText() with std::string, see misc/cpp/imgui_stdlib.h\n// (FIXME: Rather confusing and messy function, among the worse part of our codebase, expecting to rewrite a V2 at some point.. Partly because we are\n//  doing UTF8 > U16 > UTF8 conversions on the go to easily interface with stb_textedit. Ideally should stay in UTF-8 all the time. See https://github.com/nothings/stb/issues/188)\nbool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* callback_user_data)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline)));        // Can't use both together (they both use up/down keys)\n    IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key)\n\n    ImGuiContext& g = *GImGui;\n    ImGuiIO& io = g.IO;\n    const ImGuiStyle& style = g.Style;\n\n    const bool RENDER_SELECTION_WHEN_INACTIVE = false;\n    const bool is_multiline = (flags & ImGuiInputTextFlags_Multiline) != 0;\n    const bool is_readonly = (flags & ImGuiInputTextFlags_ReadOnly) != 0;\n    const bool is_password = (flags & ImGuiInputTextFlags_Password) != 0;\n    const bool is_undoable = (flags & ImGuiInputTextFlags_NoUndoRedo) == 0;\n    const bool is_resizable = (flags & ImGuiInputTextFlags_CallbackResize) != 0;\n    if (is_resizable)\n        IM_ASSERT(callback != NULL); // Must provide a callback if you set the ImGuiInputTextFlags_CallbackResize flag!\n\n    if (is_multiline) // Open group before calling GetID() because groups tracks id created within their scope,\n        BeginGroup();\n    const ImGuiID id = window->GetID(label);\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n    ImVec2 size = CalcItemSize(size_arg, GetNextItemWidth(), (is_multiline ? GetTextLineHeight() * 8.0f : label_size.y) + style.FramePadding.y*2.0f); // Arbitrary default of 8 lines high for multi-line\n    const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size);\n    const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? (style.ItemInnerSpacing.x + label_size.x) : 0.0f, 0.0f));\n\n    ImGuiWindow* draw_window = window;\n    if (is_multiline)\n    {\n        if (!ItemAdd(total_bb, id, &frame_bb))\n        {\n            ItemSize(total_bb, style.FramePadding.y);\n            EndGroup();\n            return false;\n        }\n        if (!BeginChildFrame(id, frame_bb.GetSize()))\n        {\n            EndChildFrame();\n            EndGroup();\n            return false;\n        }\n        draw_window = GetCurrentWindow();\n        draw_window->DC.NavLayerActiveMaskNext |= draw_window->DC.NavLayerCurrentMask; // This is to ensure that EndChild() will display a navigation highlight\n        size.x -= draw_window->ScrollbarSizes.x;\n    }\n    else\n    {\n        ItemSize(total_bb, style.FramePadding.y);\n        if (!ItemAdd(total_bb, id, &frame_bb))\n            return false;\n    }\n    const bool hovered = ItemHoverable(frame_bb, id);\n    if (hovered)\n        g.MouseCursor = ImGuiMouseCursor_TextInput;\n\n    // NB: we are only allowed to access 'edit_state' if we are the active widget.\n    ImGuiInputTextState* state = NULL;\n    if (g.InputTextState.ID == id)\n        state = &g.InputTextState;\n\n    const bool focus_requested = FocusableItemRegister(window, id);\n    const bool focus_requested_by_code = focus_requested && (g.FocusRequestCurrWindow == window && g.FocusRequestCurrCounterAll == window->DC.FocusCounterAll);\n    const bool focus_requested_by_tab = focus_requested && !focus_requested_by_code;\n\n    const bool user_clicked = hovered && io.MouseClicked[0];\n    const bool user_nav_input_start = (g.ActiveId != id) && ((g.NavInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_NavKeyboard));\n    const bool user_scroll_finish = is_multiline && state != NULL && g.ActiveId == 0 && g.ActiveIdPreviousFrame == GetScrollbarID(draw_window, ImGuiAxis_Y);\n    const bool user_scroll_active = is_multiline && state != NULL && g.ActiveId == GetScrollbarID(draw_window, ImGuiAxis_Y);\n\n    bool clear_active_id = false;\n    bool select_all = (g.ActiveId != id) && ((flags & ImGuiInputTextFlags_AutoSelectAll) != 0 || user_nav_input_start) && (!is_multiline);\n\n    const bool init_make_active = (focus_requested || user_clicked || user_scroll_finish || user_nav_input_start);\n    const bool init_state = (init_make_active || user_scroll_active);\n    if (init_state && g.ActiveId != id)\n    {\n        // Access state even if we don't own it yet.\n        state = &g.InputTextState;\n        state->CursorAnimReset();\n\n        // Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar)\n        // From the moment we focused we are ignoring the content of 'buf' (unless we are in read-only mode)\n        const int buf_len = (int)strlen(buf);\n        state->InitialTextA.resize(buf_len + 1);    // UTF-8. we use +1 to make sure that .Data is always pointing to at least an empty string.\n        memcpy(state->InitialTextA.Data, buf, buf_len + 1);\n\n        // Start edition\n        const char* buf_end = NULL;\n        state->TextW.resize(buf_size + 1);          // wchar count <= UTF-8 count. we use +1 to make sure that .Data is always pointing to at least an empty string.\n        state->TextA.resize(0);\n        state->TextAIsValid = false;                // TextA is not valid yet (we will display buf until then)\n        state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, buf_size, buf, NULL, &buf_end);\n        state->CurLenA = (int)(buf_end - buf);      // We can't get the result from ImStrncpy() above because it is not UTF-8 aware. Here we'll cut off malformed UTF-8.\n\n        // Preserve cursor position and undo/redo stack if we come back to same widget\n        // FIXME: For non-readonly widgets we might be able to require that TextAIsValid && TextA == buf ? (untested) and discard undo stack if user buffer has changed.\n        const bool recycle_state = (state->ID == id);\n        if (recycle_state)\n        {\n            // Recycle existing cursor/selection/undo stack but clamp position\n            // Note a single mouse click will override the cursor/position immediately by calling stb_textedit_click handler.\n            state->CursorClamp();\n        }\n        else\n        {\n            state->ID = id;\n            state->ScrollX = 0.0f;\n            stb_textedit_initialize_state(&state->Stb, !is_multiline);\n            if (!is_multiline && focus_requested_by_code)\n                select_all = true;\n        }\n        if (flags & ImGuiInputTextFlags_AlwaysInsertMode)\n            state->Stb.insert_mode = 1;\n        if (!is_multiline && (focus_requested_by_tab || (user_clicked && io.KeyCtrl)))\n            select_all = true;\n    }\n\n    if (g.ActiveId != id && init_make_active)\n    {\n        IM_ASSERT(state && state->ID == id);\n        SetActiveID(id, window);\n        SetFocusID(id, window);\n        FocusWindow(window);\n        IM_ASSERT(ImGuiNavInput_COUNT < 32);\n        g.ActiveIdBlockNavInputFlags = (1 << ImGuiNavInput_Cancel);\n        if (flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_AllowTabInput))  // Disable keyboard tabbing out as we will use the \\t character.\n            g.ActiveIdBlockNavInputFlags |= (1 << ImGuiNavInput_KeyTab_);\n        if (!is_multiline && !(flags & ImGuiInputTextFlags_CallbackHistory))\n            g.ActiveIdAllowNavDirFlags = ((1 << ImGuiDir_Up) | (1 << ImGuiDir_Down));\n    }\n\n    // We have an edge case if ActiveId was set through another widget (e.g. widget being swapped), clear id immediately (don't wait until the end of the function)\n    if (g.ActiveId == id && state == NULL)\n        ClearActiveID();\n\n    // Release focus when we click outside\n    if (g.ActiveId == id && io.MouseClicked[0] && !init_state && !init_make_active) //-V560\n        clear_active_id = true;\n\n    // Lock the decision of whether we are going to take the path displaying the cursor or selection\n    const bool render_cursor = (g.ActiveId == id) || (state && user_scroll_active);\n    bool render_selection = state && state->HasSelection() && (RENDER_SELECTION_WHEN_INACTIVE || render_cursor);\n    bool value_changed = false;\n    bool enter_pressed = false;\n\n    // When read-only we always use the live data passed to the function\n    // FIXME-OPT: Because our selection/cursor code currently needs the wide text we need to convert it when active, which is not ideal :(\n    if (is_readonly && state != NULL && (render_cursor || render_selection))\n    {\n        const char* buf_end = NULL;\n        state->TextW.resize(buf_size + 1);\n        state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, state->TextW.Size, buf, NULL, &buf_end);\n        state->CurLenA = (int)(buf_end - buf);\n        state->CursorClamp();\n        render_selection &= state->HasSelection();\n    }\n\n    // Select the buffer to render.\n    const bool buf_display_from_state = (render_cursor || render_selection || g.ActiveId == id) && !is_readonly && state && state->TextAIsValid;\n    const bool is_displaying_hint = (hint != NULL && (buf_display_from_state ? state->TextA.Data : buf)[0] == 0);\n\n    // Password pushes a temporary font with only a fallback glyph\n    if (is_password && !is_displaying_hint)\n    {\n        const ImFontGlyph* glyph = g.Font->FindGlyph('*');\n        ImFont* password_font = &g.InputTextPasswordFont;\n        password_font->FontSize = g.Font->FontSize;\n        password_font->Scale = g.Font->Scale;\n        password_font->DisplayOffset = g.Font->DisplayOffset;\n        password_font->Ascent = g.Font->Ascent;\n        password_font->Descent = g.Font->Descent;\n        password_font->ContainerAtlas = g.Font->ContainerAtlas;\n        password_font->FallbackGlyph = glyph;\n        password_font->FallbackAdvanceX = glyph->AdvanceX;\n        IM_ASSERT(password_font->Glyphs.empty() && password_font->IndexAdvanceX.empty() && password_font->IndexLookup.empty());\n        PushFont(password_font);\n    }\n\n    // Process mouse inputs and character inputs\n    int backup_current_text_length = 0;\n    if (g.ActiveId == id)\n    {\n        IM_ASSERT(state != NULL);\n        backup_current_text_length = state->CurLenA;\n        state->BufCapacityA = buf_size;\n        state->UserFlags = flags;\n        state->UserCallback = callback;\n        state->UserCallbackData = callback_user_data;\n\n        // Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget.\n        // Down the line we should have a cleaner library-wide concept of Selected vs Active.\n        g.ActiveIdAllowOverlap = !io.MouseDown[0];\n        g.WantTextInputNextFrame = 1;\n\n        // Edit in progress\n        const float mouse_x = (io.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + state->ScrollX;\n        const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y - style.FramePadding.y) : (g.FontSize*0.5f));\n\n        const bool is_osx = io.ConfigMacOSXBehaviors;\n        if (select_all || (hovered && !is_osx && io.MouseDoubleClicked[0]))\n        {\n            state->SelectAll();\n            state->SelectedAllMouseLock = true;\n        }\n        else if (hovered && is_osx && io.MouseDoubleClicked[0])\n        {\n            // Double-click select a word only, OS X style (by simulating keystrokes)\n            state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT);\n            state->OnKeyPressed(STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT);\n        }\n        else if (io.MouseClicked[0] && !state->SelectedAllMouseLock)\n        {\n            if (hovered)\n            {\n                stb_textedit_click(state, &state->Stb, mouse_x, mouse_y);\n                state->CursorAnimReset();\n            }\n        }\n        else if (io.MouseDown[0] && !state->SelectedAllMouseLock && (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f))\n        {\n            stb_textedit_drag(state, &state->Stb, mouse_x, mouse_y);\n            state->CursorAnimReset();\n            state->CursorFollow = true;\n        }\n        if (state->SelectedAllMouseLock && !io.MouseDown[0])\n            state->SelectedAllMouseLock = false;\n\n        // It is ill-defined whether the back-end needs to send a \\t character when pressing the TAB keys.\n        // Win32 and GLFW naturally do it but not SDL.\n        const bool ignore_char_inputs = (io.KeyCtrl && !io.KeyAlt) || (is_osx && io.KeySuper);\n        if ((flags & ImGuiInputTextFlags_AllowTabInput) && IsKeyPressedMap(ImGuiKey_Tab) && !ignore_char_inputs && !io.KeyShift && !is_readonly)\n            if (!io.InputQueueCharacters.contains('\\t'))\n            {\n                unsigned int c = '\\t'; // Insert TAB\n                if (InputTextFilterCharacter(&c, flags, callback, callback_user_data))\n                    state->OnKeyPressed((int)c);\n            }\n\n        // Process regular text input (before we check for Return because using some IME will effectively send a Return?)\n        // We ignore CTRL inputs, but need to allow ALT+CTRL as some keyboards (e.g. German) use AltGR (which _is_ Alt+Ctrl) to input certain characters.\n        if (io.InputQueueCharacters.Size > 0)\n        {\n            if (!ignore_char_inputs && !is_readonly && !user_nav_input_start)\n                for (int n = 0; n < io.InputQueueCharacters.Size; n++)\n                {\n                    // Insert character if they pass filtering\n                    unsigned int c = (unsigned int)io.InputQueueCharacters[n];\n                    if (c == '\\t' && io.KeyShift)\n                        continue;\n                    if (InputTextFilterCharacter(&c, flags, callback, callback_user_data))\n                        state->OnKeyPressed((int)c);\n                }\n\n            // Consume characters\n            io.InputQueueCharacters.resize(0);\n        }\n    }\n\n    // Process other shortcuts/key-presses\n    bool cancel_edit = false;\n    if (g.ActiveId == id && !g.ActiveIdIsJustActivated && !clear_active_id)\n    {\n        IM_ASSERT(state != NULL);\n        const int k_mask = (io.KeyShift ? STB_TEXTEDIT_K_SHIFT : 0);\n        const bool is_osx = io.ConfigMacOSXBehaviors;\n        const bool is_shortcut_key = (is_osx ? (io.KeySuper && !io.KeyCtrl) : (io.KeyCtrl && !io.KeySuper)) && !io.KeyAlt && !io.KeyShift; // OS X style: Shortcuts using Cmd/Super instead of Ctrl\n        const bool is_osx_shift_shortcut = is_osx && io.KeySuper && io.KeyShift && !io.KeyCtrl && !io.KeyAlt;\n        const bool is_wordmove_key_down = is_osx ? io.KeyAlt : io.KeyCtrl;                     // OS X style: Text editing cursor movement using Alt instead of Ctrl\n        const bool is_startend_key_down = is_osx && io.KeySuper && !io.KeyCtrl && !io.KeyAlt;  // OS X style: Line/Text Start and End using Cmd+Arrows instead of Home/End\n        const bool is_ctrl_key_only = io.KeyCtrl && !io.KeyShift && !io.KeyAlt && !io.KeySuper;\n        const bool is_shift_key_only = io.KeyShift && !io.KeyCtrl && !io.KeyAlt && !io.KeySuper;\n\n        const bool is_cut   = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_X)) || (is_shift_key_only && IsKeyPressedMap(ImGuiKey_Delete))) && !is_readonly && !is_password && (!is_multiline || state->HasSelection());\n        const bool is_copy  = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_C)) || (is_ctrl_key_only  && IsKeyPressedMap(ImGuiKey_Insert))) && !is_password && (!is_multiline || state->HasSelection());\n        const bool is_paste = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_V)) || (is_shift_key_only && IsKeyPressedMap(ImGuiKey_Insert))) && !is_readonly;\n        const bool is_undo  = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Z)) && !is_readonly && is_undoable);\n        const bool is_redo  = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Y)) || (is_osx_shift_shortcut && IsKeyPressedMap(ImGuiKey_Z))) && !is_readonly && is_undoable;\n\n        if (IsKeyPressedMap(ImGuiKey_LeftArrow))                        { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINESTART : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDLEFT : STB_TEXTEDIT_K_LEFT) | k_mask); }\n        else if (IsKeyPressedMap(ImGuiKey_RightArrow))                  { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINEEND : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDRIGHT : STB_TEXTEDIT_K_RIGHT) | k_mask); }\n        else if (IsKeyPressedMap(ImGuiKey_UpArrow) && is_multiline)     { if (io.KeyCtrl) SetWindowScrollY(draw_window, ImMax(draw_window->Scroll.y - g.FontSize, 0.0f)); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTSTART : STB_TEXTEDIT_K_UP) | k_mask); }\n        else if (IsKeyPressedMap(ImGuiKey_DownArrow) && is_multiline)   { if (io.KeyCtrl) SetWindowScrollY(draw_window, ImMin(draw_window->Scroll.y + g.FontSize, GetScrollMaxY())); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTEND : STB_TEXTEDIT_K_DOWN) | k_mask); }\n        else if (IsKeyPressedMap(ImGuiKey_Home))                        { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); }\n        else if (IsKeyPressedMap(ImGuiKey_End))                         { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); }\n        else if (IsKeyPressedMap(ImGuiKey_Delete) && !is_readonly)      { state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); }\n        else if (IsKeyPressedMap(ImGuiKey_Backspace) && !is_readonly)\n        {\n            if (!state->HasSelection())\n            {\n                if (is_wordmove_key_down)\n                    state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT|STB_TEXTEDIT_K_SHIFT);\n                else if (is_osx && io.KeySuper && !io.KeyAlt && !io.KeyCtrl)\n                    state->OnKeyPressed(STB_TEXTEDIT_K_LINESTART|STB_TEXTEDIT_K_SHIFT);\n            }\n            state->OnKeyPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask);\n        }\n        else if (IsKeyPressedMap(ImGuiKey_Enter))\n        {\n            bool ctrl_enter_for_new_line = (flags & ImGuiInputTextFlags_CtrlEnterForNewLine) != 0;\n            if (!is_multiline || (ctrl_enter_for_new_line && !io.KeyCtrl) || (!ctrl_enter_for_new_line && io.KeyCtrl))\n            {\n                enter_pressed = clear_active_id = true;\n            }\n            else if (!is_readonly)\n            {\n                unsigned int c = '\\n'; // Insert new line\n                if (InputTextFilterCharacter(&c, flags, callback, callback_user_data))\n                    state->OnKeyPressed((int)c);\n            }\n        }\n        else if (IsKeyPressedMap(ImGuiKey_Escape))\n        {\n            clear_active_id = cancel_edit = true;\n        }\n        else if (is_undo || is_redo)\n        {\n            state->OnKeyPressed(is_undo ? STB_TEXTEDIT_K_UNDO : STB_TEXTEDIT_K_REDO);\n            state->ClearSelection();\n        }\n        else if (is_shortcut_key && IsKeyPressedMap(ImGuiKey_A))\n        {\n            state->SelectAll();\n            state->CursorFollow = true;\n        }\n        else if (is_cut || is_copy)\n        {\n            // Cut, Copy\n            if (io.SetClipboardTextFn)\n            {\n                const int ib = state->HasSelection() ? ImMin(state->Stb.select_start, state->Stb.select_end) : 0;\n                const int ie = state->HasSelection() ? ImMax(state->Stb.select_start, state->Stb.select_end) : state->CurLenW;\n                const int clipboard_data_len = ImTextCountUtf8BytesFromStr(state->TextW.Data + ib, state->TextW.Data + ie) + 1;\n                char* clipboard_data = (char*)IM_ALLOC(clipboard_data_len * sizeof(char));\n                ImTextStrToUtf8(clipboard_data, clipboard_data_len, state->TextW.Data + ib, state->TextW.Data + ie);\n                SetClipboardText(clipboard_data);\n                MemFree(clipboard_data);\n            }\n            if (is_cut)\n            {\n                if (!state->HasSelection())\n                    state->SelectAll();\n                state->CursorFollow = true;\n                stb_textedit_cut(state, &state->Stb);\n            }\n        }\n        else if (is_paste)\n        {\n            if (const char* clipboard = GetClipboardText())\n            {\n                // Filter pasted buffer\n                const int clipboard_len = (int)strlen(clipboard);\n                ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len+1) * sizeof(ImWchar));\n                int clipboard_filtered_len = 0;\n                for (const char* s = clipboard; *s; )\n                {\n                    unsigned int c;\n                    s += ImTextCharFromUtf8(&c, s, NULL);\n                    if (c == 0)\n                        break;\n                    if (c >= 0x10000 || !InputTextFilterCharacter(&c, flags, callback, callback_user_data))\n                        continue;\n                    clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c;\n                }\n                clipboard_filtered[clipboard_filtered_len] = 0;\n                if (clipboard_filtered_len > 0) // If everything was filtered, ignore the pasting operation\n                {\n                    stb_textedit_paste(state, &state->Stb, clipboard_filtered, clipboard_filtered_len);\n                    state->CursorFollow = true;\n                }\n                MemFree(clipboard_filtered);\n            }\n        }\n\n        // Update render selection flag after events have been handled, so selection highlight can be displayed during the same frame.\n        render_selection |= state->HasSelection() && (RENDER_SELECTION_WHEN_INACTIVE || render_cursor);\n    }\n\n    // Process callbacks and apply result back to user's buffer.\n    if (g.ActiveId == id)\n    {\n        IM_ASSERT(state != NULL);\n        const char* apply_new_text = NULL;\n        int apply_new_text_length = 0;\n        if (cancel_edit)\n        {\n            // Restore initial value. Only return true if restoring to the initial value changes the current buffer contents.\n            if (!is_readonly && strcmp(buf, state->InitialTextA.Data) != 0)\n            {\n                apply_new_text = state->InitialTextA.Data;\n                apply_new_text_length = state->InitialTextA.Size - 1;\n            }\n        }\n\n        // When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer before clearing ActiveId, even though strictly speaking it wasn't modified on this frame.\n        // If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. Also this allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage.\n        bool apply_edit_back_to_user_buffer = !cancel_edit || (enter_pressed && (flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0);\n        if (apply_edit_back_to_user_buffer)\n        {\n            // Apply new value immediately - copy modified buffer back\n            // Note that as soon as the input box is active, the in-widget value gets priority over any underlying modification of the input buffer\n            // FIXME: We actually always render 'buf' when calling DrawList->AddText, making the comment above incorrect.\n            // FIXME-OPT: CPU waste to do this every time the widget is active, should mark dirty state from the stb_textedit callbacks.\n            if (!is_readonly)\n            {\n                state->TextAIsValid = true;\n                state->TextA.resize(state->TextW.Size * 4 + 1);\n                ImTextStrToUtf8(state->TextA.Data, state->TextA.Size, state->TextW.Data, NULL);\n            }\n\n            // User callback\n            if ((flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory | ImGuiInputTextFlags_CallbackAlways)) != 0)\n            {\n                IM_ASSERT(callback != NULL);\n\n                // The reason we specify the usage semantic (Completion/History) is that Completion needs to disable keyboard TABBING at the moment.\n                ImGuiInputTextFlags event_flag = 0;\n                ImGuiKey event_key = ImGuiKey_COUNT;\n                if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && IsKeyPressedMap(ImGuiKey_Tab))\n                {\n                    event_flag = ImGuiInputTextFlags_CallbackCompletion;\n                    event_key = ImGuiKey_Tab;\n                }\n                else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_UpArrow))\n                {\n                    event_flag = ImGuiInputTextFlags_CallbackHistory;\n                    event_key = ImGuiKey_UpArrow;\n                }\n                else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_DownArrow))\n                {\n                    event_flag = ImGuiInputTextFlags_CallbackHistory;\n                    event_key = ImGuiKey_DownArrow;\n                }\n                else if (flags & ImGuiInputTextFlags_CallbackAlways)\n                    event_flag = ImGuiInputTextFlags_CallbackAlways;\n\n                if (event_flag)\n                {\n                    ImGuiInputTextCallbackData callback_data;\n                    memset(&callback_data, 0, sizeof(ImGuiInputTextCallbackData));\n                    callback_data.EventFlag = event_flag;\n                    callback_data.Flags = flags;\n                    callback_data.UserData = callback_user_data;\n\n                    callback_data.EventKey = event_key;\n                    callback_data.Buf = state->TextA.Data;\n                    callback_data.BufTextLen = state->CurLenA;\n                    callback_data.BufSize = state->BufCapacityA;\n                    callback_data.BufDirty = false;\n\n                    // We have to convert from wchar-positions to UTF-8-positions, which can be pretty slow (an incentive to ditch the ImWchar buffer, see https://github.com/nothings/stb/issues/188)\n                    ImWchar* text = state->TextW.Data;\n                    const int utf8_cursor_pos = callback_data.CursorPos = ImTextCountUtf8BytesFromStr(text, text + state->Stb.cursor);\n                    const int utf8_selection_start = callback_data.SelectionStart = ImTextCountUtf8BytesFromStr(text, text + state->Stb.select_start);\n                    const int utf8_selection_end = callback_data.SelectionEnd = ImTextCountUtf8BytesFromStr(text, text + state->Stb.select_end);\n\n                    // Call user code\n                    callback(&callback_data);\n\n                    // Read back what user may have modified\n                    IM_ASSERT(callback_data.Buf == state->TextA.Data);  // Invalid to modify those fields\n                    IM_ASSERT(callback_data.BufSize == state->BufCapacityA);\n                    IM_ASSERT(callback_data.Flags == flags);\n                    if (callback_data.CursorPos != utf8_cursor_pos)            { state->Stb.cursor = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.CursorPos); state->CursorFollow = true; }\n                    if (callback_data.SelectionStart != utf8_selection_start)  { state->Stb.select_start = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionStart); }\n                    if (callback_data.SelectionEnd != utf8_selection_end)      { state->Stb.select_end = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd); }\n                    if (callback_data.BufDirty)\n                    {\n                        IM_ASSERT(callback_data.BufTextLen == (int)strlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text!\n                        if (callback_data.BufTextLen > backup_current_text_length && is_resizable)\n                            state->TextW.resize(state->TextW.Size + (callback_data.BufTextLen - backup_current_text_length));\n                        state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, state->TextW.Size, callback_data.Buf, NULL);\n                        state->CurLenA = callback_data.BufTextLen;  // Assume correct length and valid UTF-8 from user, saves us an extra strlen()\n                        state->CursorAnimReset();\n                    }\n                }\n            }\n\n            // Will copy result string if modified\n            if (!is_readonly && strcmp(state->TextA.Data, buf) != 0)\n            {\n                apply_new_text = state->TextA.Data;\n                apply_new_text_length = state->CurLenA;\n            }\n        }\n\n        // Copy result to user buffer\n        if (apply_new_text)\n        {\n            IM_ASSERT(apply_new_text_length >= 0);\n            if (backup_current_text_length != apply_new_text_length && is_resizable)\n            {\n                ImGuiInputTextCallbackData callback_data;\n                callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize;\n                callback_data.Flags = flags;\n                callback_data.Buf = buf;\n                callback_data.BufTextLen = apply_new_text_length;\n                callback_data.BufSize = ImMax(buf_size, apply_new_text_length + 1);\n                callback_data.UserData = callback_user_data;\n                callback(&callback_data);\n                buf = callback_data.Buf;\n                buf_size = callback_data.BufSize;\n                apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1);\n                IM_ASSERT(apply_new_text_length <= buf_size);\n            }\n\n            // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size.\n            ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size));\n            value_changed = true;\n        }\n\n        // Clear temporary user storage\n        state->UserFlags = 0;\n        state->UserCallback = NULL;\n        state->UserCallbackData = NULL;\n    }\n\n    // Release active ID at the end of the function (so e.g. pressing Return still does a final application of the value)\n    if (clear_active_id && g.ActiveId == id)\n        ClearActiveID();\n\n    // Render frame\n    if (!is_multiline)\n    {\n        RenderNavHighlight(frame_bb, id);\n        RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding);\n    }\n\n    const ImVec4 clip_rect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Min.x + size.x, frame_bb.Min.y + size.y); // Not using frame_bb.Max because we have adjusted size\n    ImVec2 draw_pos = is_multiline ? draw_window->DC.CursorPos : frame_bb.Min + style.FramePadding;\n    ImVec2 text_size(0.0f, 0.0f);\n\n    // Set upper limit of single-line InputTextEx() at 2 million characters strings. The current pathological worst case is a long line\n    // without any carriage return, which would makes ImFont::RenderText() reserve too many vertices and probably crash. Avoid it altogether.\n    // Note that we only use this limit on single-line InputText(), so a pathologically large line on a InputTextMultiline() would still crash.\n    const int buf_display_max_length = 2 * 1024 * 1024;\n    const char* buf_display = buf_display_from_state ? state->TextA.Data : buf; //-V595\n    const char* buf_display_end = NULL; // We have specialized paths below for setting the length\n    if (is_displaying_hint)\n    {\n        buf_display = hint;\n        buf_display_end = hint + strlen(hint);\n    }\n\n    // Render text. We currently only render selection when the widget is active or while scrolling.\n    // FIXME: We could remove the '&& render_cursor' to keep rendering selection when inactive.\n    if (render_cursor || render_selection)\n    {\n        IM_ASSERT(state != NULL);\n        if (!is_displaying_hint)\n            buf_display_end = buf_display + state->CurLenA;\n\n        // Render text (with cursor and selection)\n        // This is going to be messy. We need to:\n        // - Display the text (this alone can be more easily clipped)\n        // - Handle scrolling, highlight selection, display cursor (those all requires some form of 1d->2d cursor position calculation)\n        // - Measure text height (for scrollbar)\n        // We are attempting to do most of that in **one main pass** to minimize the computation cost (non-negligible for large amount of text) + 2nd pass for selection rendering (we could merge them by an extra refactoring effort)\n        // FIXME: This should occur on buf_display but we'd need to maintain cursor/select_start/select_end for UTF-8.\n        const ImWchar* text_begin = state->TextW.Data;\n        ImVec2 cursor_offset, select_start_offset;\n\n        {\n            // Find lines numbers straddling 'cursor' (slot 0) and 'select_start' (slot 1) positions.\n            const ImWchar* searches_input_ptr[2] = { NULL, NULL };\n            int searches_result_line_no[2] = { -1000, -1000 };\n            int searches_remaining = 0;\n            if (render_cursor)\n            {\n                searches_input_ptr[0] = text_begin + state->Stb.cursor;\n                searches_result_line_no[0] = -1;\n                searches_remaining++;\n            }\n            if (render_selection)\n            {\n                searches_input_ptr[1] = text_begin + ImMin(state->Stb.select_start, state->Stb.select_end);\n                searches_result_line_no[1] = -1;\n                searches_remaining++;\n            }\n\n            // Iterate all lines to find our line numbers\n            // In multi-line mode, we never exit the loop until all lines are counted, so add one extra to the searches_remaining counter.\n            searches_remaining += is_multiline ? 1 : 0;\n            int line_count = 0;\n            //for (const ImWchar* s = text_begin; (s = (const ImWchar*)wcschr((const wchar_t*)s, (wchar_t)'\\n')) != NULL; s++)  // FIXME-OPT: Could use this when wchar_t are 16-bits\n            for (const ImWchar* s = text_begin; *s != 0; s++)\n                if (*s == '\\n')\n                {\n                    line_count++;\n                    if (searches_result_line_no[0] == -1 && s >= searches_input_ptr[0]) { searches_result_line_no[0] = line_count; if (--searches_remaining <= 0) break; }\n                    if (searches_result_line_no[1] == -1 && s >= searches_input_ptr[1]) { searches_result_line_no[1] = line_count; if (--searches_remaining <= 0) break; }\n                }\n            line_count++;\n            if (searches_result_line_no[0] == -1)\n                searches_result_line_no[0] = line_count;\n            if (searches_result_line_no[1] == -1)\n                searches_result_line_no[1] = line_count;\n\n            // Calculate 2d position by finding the beginning of the line and measuring distance\n            cursor_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_ptr[0], text_begin), searches_input_ptr[0]).x;\n            cursor_offset.y = searches_result_line_no[0] * g.FontSize;\n            if (searches_result_line_no[1] >= 0)\n            {\n                select_start_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_ptr[1], text_begin), searches_input_ptr[1]).x;\n                select_start_offset.y = searches_result_line_no[1] * g.FontSize;\n            }\n\n            // Store text height (note that we haven't calculated text width at all, see GitHub issues #383, #1224)\n            if (is_multiline)\n                text_size = ImVec2(size.x, line_count * g.FontSize);\n        }\n\n        // Scroll\n        if (render_cursor && state->CursorFollow)\n        {\n            // Horizontal scroll in chunks of quarter width\n            if (!(flags & ImGuiInputTextFlags_NoHorizontalScroll))\n            {\n                const float scroll_increment_x = size.x * 0.25f;\n                if (cursor_offset.x < state->ScrollX)\n                    state->ScrollX = (float)(int)ImMax(0.0f, cursor_offset.x - scroll_increment_x);\n                else if (cursor_offset.x - size.x >= state->ScrollX)\n                    state->ScrollX = (float)(int)(cursor_offset.x - size.x + scroll_increment_x);\n            }\n            else\n            {\n                state->ScrollX = 0.0f;\n            }\n\n            // Vertical scroll\n            if (is_multiline)\n            {\n                float scroll_y = draw_window->Scroll.y;\n                if (cursor_offset.y - g.FontSize < scroll_y)\n                    scroll_y = ImMax(0.0f, cursor_offset.y - g.FontSize);\n                else if (cursor_offset.y - size.y >= scroll_y)\n                    scroll_y = cursor_offset.y - size.y;\n                draw_window->DC.CursorPos.y += (draw_window->Scroll.y - scroll_y);   // Manipulate cursor pos immediately avoid a frame of lag\n                draw_window->Scroll.y = scroll_y;\n                draw_pos.y = draw_window->DC.CursorPos.y;\n            }\n\n            state->CursorFollow = false;\n        }\n\n        // Draw selection\n        const ImVec2 draw_scroll = ImVec2(state->ScrollX, 0.0f);\n        if (render_selection)\n        {\n            const ImWchar* text_selected_begin = text_begin + ImMin(state->Stb.select_start, state->Stb.select_end);\n            const ImWchar* text_selected_end = text_begin + ImMax(state->Stb.select_start, state->Stb.select_end);\n\n            ImU32 bg_color = GetColorU32(ImGuiCol_TextSelectedBg, render_cursor ? 1.0f : 0.6f); // FIXME: current code flow mandate that render_cursor is always true here, we are leaving the transparent one for tests.\n            float bg_offy_up = is_multiline ? 0.0f : -1.0f;    // FIXME: those offsets should be part of the style? they don't play so well with multi-line selection.\n            float bg_offy_dn = is_multiline ? 0.0f : 2.0f;\n            ImVec2 rect_pos = draw_pos + select_start_offset - draw_scroll;\n            for (const ImWchar* p = text_selected_begin; p < text_selected_end; )\n            {\n                if (rect_pos.y > clip_rect.w + g.FontSize)\n                    break;\n                if (rect_pos.y < clip_rect.y)\n                {\n                    //p = (const ImWchar*)wmemchr((const wchar_t*)p, '\\n', text_selected_end - p);  // FIXME-OPT: Could use this when wchar_t are 16-bits\n                    //p = p ? p + 1 : text_selected_end;\n                    while (p < text_selected_end)\n                        if (*p++ == '\\n')\n                            break;\n                }\n                else\n                {\n                    ImVec2 rect_size = InputTextCalcTextSizeW(p, text_selected_end, &p, NULL, true);\n                    if (rect_size.x <= 0.0f) rect_size.x = (float)(int)(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines\n                    ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos +ImVec2(rect_size.x, bg_offy_dn));\n                    rect.ClipWith(clip_rect);\n                    if (rect.Overlaps(clip_rect))\n                        draw_window->DrawList->AddRectFilled(rect.Min, rect.Max, bg_color);\n                }\n                rect_pos.x = draw_pos.x - draw_scroll.x;\n                rect_pos.y += g.FontSize;\n            }\n        }\n\n        // We test for 'buf_display_max_length' as a way to avoid some pathological cases (e.g. single-line 1 MB string) which would make ImDrawList crash.\n        if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length)\n        {\n            ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text);\n            draw_window->DrawList->AddText(g.Font, g.FontSize, draw_pos - draw_scroll, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect);\n        }\n\n        // Draw blinking cursor\n        if (render_cursor)\n        {\n            state->CursorAnim += io.DeltaTime;\n            bool cursor_is_visible = (!g.IO.ConfigInputTextCursorBlink) || (state->CursorAnim <= 0.0f) || ImFmod(state->CursorAnim, 1.20f) <= 0.80f;\n            ImVec2 cursor_screen_pos = draw_pos + cursor_offset - draw_scroll;\n            ImRect cursor_screen_rect(cursor_screen_pos.x, cursor_screen_pos.y - g.FontSize + 0.5f, cursor_screen_pos.x + 1.0f, cursor_screen_pos.y - 1.5f);\n            if (cursor_is_visible && cursor_screen_rect.Overlaps(clip_rect))\n                draw_window->DrawList->AddLine(cursor_screen_rect.Min, cursor_screen_rect.GetBL(), GetColorU32(ImGuiCol_Text));\n\n            // Notify OS of text input position for advanced IME (-1 x offset so that Windows IME can cover our cursor. Bit of an extra nicety.)\n            if (!is_readonly)\n            {\n                g.PlatformImePos = ImVec2(cursor_screen_pos.x - 1, cursor_screen_pos.y - g.FontSize);\n                g.PlatformImePosViewport = window->Viewport;\n            }\n        }\n    }\n    else\n    {\n        // Render text only (no selection, no cursor)\n        if (is_multiline)\n            text_size = ImVec2(size.x, InputTextCalcTextLenAndLineCount(buf_display, &buf_display_end) * g.FontSize); // We don't need width\n        else if (!is_displaying_hint && g.ActiveId == id)\n            buf_display_end = buf_display + state->CurLenA;\n        else if (!is_displaying_hint)\n            buf_display_end = buf_display + strlen(buf_display);\n\n        if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length)\n        {\n            ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text);\n            draw_window->DrawList->AddText(g.Font, g.FontSize, draw_pos, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect);\n        }\n    }\n\n    if (is_multiline)\n    {\n        Dummy(text_size + ImVec2(0.0f, g.FontSize)); // Always add room to scroll an extra line\n        EndChildFrame();\n        EndGroup();\n    }\n\n    if (is_password && !is_displaying_hint)\n        PopFont();\n\n    // Log as text\n    if (g.LogEnabled && !(is_password && !is_displaying_hint))\n        LogRenderedText(&draw_pos, buf_display, buf_display_end);\n\n    if (label_size.x > 0)\n        RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);\n\n    if (value_changed)\n        MarkItemEdited(id);\n\n    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags);\n    if ((flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0)\n        return enter_pressed;\n    else\n        return value_changed;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: ColorEdit, ColorPicker, ColorButton, etc.\n//-------------------------------------------------------------------------\n// - ColorEdit3()\n// - ColorEdit4()\n// - ColorPicker3()\n// - RenderColorRectWithAlphaCheckerboard() [Internal]\n// - ColorPicker4()\n// - ColorButton()\n// - SetColorEditOptions()\n// - ColorTooltip() [Internal]\n// - ColorEditOptionsPopup() [Internal]\n// - ColorPickerOptionsPopup() [Internal]\n//-------------------------------------------------------------------------\n\nbool ImGui::ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags)\n{\n    return ColorEdit4(label, col, flags | ImGuiColorEditFlags_NoAlpha);\n}\n\n// Edit colors components (each component in 0.0f..1.0f range).\n// See enum ImGuiColorEditFlags_ for available options. e.g. Only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set.\n// With typical options: Left-click on colored square to open color picker. Right-click to open option menu. CTRL-Click over input fields to edit them and TAB to go to next item.\nbool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const float square_sz = GetFrameHeight();\n    const float w_extra = (flags & ImGuiColorEditFlags_NoSmallPreview) ? 0.0f : (square_sz + style.ItemInnerSpacing.x);\n    const float w_items_all = GetNextItemWidth() - w_extra;\n    const char* label_display_end = FindRenderedTextEnd(label);\n\n    BeginGroup();\n    PushID(label);\n\n    // If we're not showing any slider there's no point in doing any HSV conversions\n    const ImGuiColorEditFlags flags_untouched = flags;\n    if (flags & ImGuiColorEditFlags_NoInputs)\n        flags = (flags & (~ImGuiColorEditFlags__DisplayMask)) | ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_NoOptions;\n\n    // Context menu: display and modify options (before defaults are applied)\n    if (!(flags & ImGuiColorEditFlags_NoOptions))\n        ColorEditOptionsPopup(col, flags);\n\n    // Read stored options\n    if (!(flags & ImGuiColorEditFlags__DisplayMask))\n        flags |= (g.ColorEditOptions & ImGuiColorEditFlags__DisplayMask);\n    if (!(flags & ImGuiColorEditFlags__DataTypeMask))\n        flags |= (g.ColorEditOptions & ImGuiColorEditFlags__DataTypeMask);\n    if (!(flags & ImGuiColorEditFlags__PickerMask))\n        flags |= (g.ColorEditOptions & ImGuiColorEditFlags__PickerMask);\n    if (!(flags & ImGuiColorEditFlags__InputMask))\n        flags |= (g.ColorEditOptions & ImGuiColorEditFlags__InputMask);\n    flags |= (g.ColorEditOptions & ~(ImGuiColorEditFlags__DisplayMask | ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__PickerMask | ImGuiColorEditFlags__InputMask));\n    IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DisplayMask)); // Check that only 1 is selected\n    IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask));   // Check that only 1 is selected\n\n    const bool alpha = (flags & ImGuiColorEditFlags_NoAlpha) == 0;\n    const bool hdr = (flags & ImGuiColorEditFlags_HDR) != 0;\n    const int components = alpha ? 4 : 3;\n\n    // Convert to the formats we need\n    float f[4] = { col[0], col[1], col[2], alpha ? col[3] : 1.0f };\n    if ((flags & ImGuiColorEditFlags_InputHSV) && (flags & ImGuiColorEditFlags_DisplayRGB))\n        ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);\n    else if ((flags & ImGuiColorEditFlags_InputRGB) && (flags & ImGuiColorEditFlags_DisplayHSV))\n        ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);\n    int i[4] = { IM_F32_TO_INT8_UNBOUND(f[0]), IM_F32_TO_INT8_UNBOUND(f[1]), IM_F32_TO_INT8_UNBOUND(f[2]), IM_F32_TO_INT8_UNBOUND(f[3]) };\n\n    bool value_changed = false;\n    bool value_changed_as_float = false;\n\n    if ((flags & (ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV)) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0)\n    {\n        // RGB/HSV 0..255 Sliders\n        const float w_item_one  = ImMax(1.0f, (float)(int)((w_items_all - (style.ItemInnerSpacing.x) * (components-1)) / (float)components));\n        const float w_item_last = ImMax(1.0f, (float)(int)(w_items_all - (w_item_one + style.ItemInnerSpacing.x) * (components-1)));\n\n        const bool hide_prefix = (w_item_one <= CalcTextSize((flags & ImGuiColorEditFlags_Float) ? \"M:0.000\" : \"M:000\").x);\n        static const char* ids[4] = { \"##X\", \"##Y\", \"##Z\", \"##W\" };\n        static const char* fmt_table_int[3][4] =\n        {\n            {   \"%3d\",   \"%3d\",   \"%3d\",   \"%3d\" }, // Short display\n            { \"R:%3d\", \"G:%3d\", \"B:%3d\", \"A:%3d\" }, // Long display for RGBA\n            { \"H:%3d\", \"S:%3d\", \"V:%3d\", \"A:%3d\" }  // Long display for HSVA\n        };\n        static const char* fmt_table_float[3][4] =\n        {\n            {   \"%0.3f\",   \"%0.3f\",   \"%0.3f\",   \"%0.3f\" }, // Short display\n            { \"R:%0.3f\", \"G:%0.3f\", \"B:%0.3f\", \"A:%0.3f\" }, // Long display for RGBA\n            { \"H:%0.3f\", \"S:%0.3f\", \"V:%0.3f\", \"A:%0.3f\" }  // Long display for HSVA\n        };\n        const int fmt_idx = hide_prefix ? 0 : (flags & ImGuiColorEditFlags_DisplayHSV) ? 2 : 1;\n\n        for (int n = 0; n < components; n++)\n        {\n            if (n > 0)\n                SameLine(0, style.ItemInnerSpacing.x);\n            SetNextItemWidth((n + 1 < components) ? w_item_one : w_item_last);\n            if (flags & ImGuiColorEditFlags_Float)\n            {\n                value_changed |= DragFloat(ids[n], &f[n], 1.0f/255.0f, 0.0f, hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]);\n                value_changed_as_float |= value_changed;\n            }\n            else\n            {\n                value_changed |= DragInt(ids[n], &i[n], 1.0f, 0, hdr ? 0 : 255, fmt_table_int[fmt_idx][n]);\n            }\n            if (!(flags & ImGuiColorEditFlags_NoOptions))\n                OpenPopupOnItemClick(\"context\");\n        }\n    }\n    else if ((flags & ImGuiColorEditFlags_DisplayHex) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0)\n    {\n        // RGB Hexadecimal Input\n        char buf[64];\n        if (alpha)\n            ImFormatString(buf, IM_ARRAYSIZE(buf), \"#%02X%02X%02X%02X\", ImClamp(i[0],0,255), ImClamp(i[1],0,255), ImClamp(i[2],0,255), ImClamp(i[3],0,255));\n        else\n            ImFormatString(buf, IM_ARRAYSIZE(buf), \"#%02X%02X%02X\", ImClamp(i[0],0,255), ImClamp(i[1],0,255), ImClamp(i[2],0,255));\n        SetNextItemWidth(w_items_all);\n        if (InputText(\"##Text\", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase))\n        {\n            value_changed = true;\n            char* p = buf;\n            while (*p == '#' || ImCharIsBlankA(*p))\n                p++;\n            i[0] = i[1] = i[2] = i[3] = 0;\n            if (alpha)\n                sscanf(p, \"%02X%02X%02X%02X\", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2], (unsigned int*)&i[3]); // Treat at unsigned (%X is unsigned)\n            else\n                sscanf(p, \"%02X%02X%02X\", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]);\n        }\n        if (!(flags & ImGuiColorEditFlags_NoOptions))\n            OpenPopupOnItemClick(\"context\");\n    }\n\n    ImGuiWindow* picker_active_window = NULL;\n    if (!(flags & ImGuiColorEditFlags_NoSmallPreview))\n    {\n        if (!(flags & ImGuiColorEditFlags_NoInputs))\n            SameLine(0, style.ItemInnerSpacing.x);\n\n        const ImVec4 col_v4(col[0], col[1], col[2], alpha ? col[3] : 1.0f);\n        if (ColorButton(\"##ColorButton\", col_v4, flags))\n        {\n            if (!(flags & ImGuiColorEditFlags_NoPicker))\n            {\n                // Store current color and open a picker\n                g.ColorPickerRef = col_v4;\n                OpenPopup(\"picker\");\n                SetNextWindowPos(window->DC.LastItemRect.GetBL() + ImVec2(-1,style.ItemSpacing.y));\n            }\n        }\n        if (!(flags & ImGuiColorEditFlags_NoOptions))\n            OpenPopupOnItemClick(\"context\");\n\n        if (BeginPopup(\"picker\"))\n        {\n            picker_active_window = g.CurrentWindow;\n            if (label != label_display_end)\n            {\n                TextEx(label, label_display_end);\n                Spacing();\n            }\n            ImGuiColorEditFlags picker_flags_to_forward = ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__PickerMask | ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaBar;\n            ImGuiColorEditFlags picker_flags = (flags_untouched & picker_flags_to_forward) | ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaPreviewHalf;\n            SetNextItemWidth(square_sz * 12.0f); // Use 256 + bar sizes?\n            value_changed |= ColorPicker4(\"##picker\", col, picker_flags, &g.ColorPickerRef.x);\n            EndPopup();\n        }\n    }\n\n    if (label != label_display_end && !(flags & ImGuiColorEditFlags_NoLabel))\n    {\n        SameLine(0, style.ItemInnerSpacing.x);\n        TextEx(label, label_display_end);\n    }\n\n    // Convert back\n    if (value_changed && picker_active_window == NULL)\n    {\n        if (!value_changed_as_float)\n            for (int n = 0; n < 4; n++)\n                f[n] = i[n] / 255.0f;\n        if ((flags & ImGuiColorEditFlags_DisplayHSV) && (flags & ImGuiColorEditFlags_InputRGB))\n            ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);\n        if ((flags & ImGuiColorEditFlags_DisplayRGB) && (flags & ImGuiColorEditFlags_InputHSV))\n            ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);\n\n        col[0] = f[0];\n        col[1] = f[1];\n        col[2] = f[2];\n        if (alpha)\n            col[3] = f[3];\n    }\n\n    PopID();\n    EndGroup();\n\n    // Drag and Drop Target\n    // NB: The flag test is merely an optional micro-optimization, BeginDragDropTarget() does the same test.\n    if ((window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect) && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropTarget())\n    {\n        bool accepted_drag_drop = false;\n        if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F))\n        {\n            memcpy((float*)col, payload->Data, sizeof(float) * 3); // Preserve alpha if any //-V512\n            value_changed = accepted_drag_drop = true;\n        }\n        if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F))\n        {\n            memcpy((float*)col, payload->Data, sizeof(float) * components);\n            value_changed = accepted_drag_drop = true;\n        }\n\n        // Drag-drop payloads are always RGB\n        if (accepted_drag_drop && (flags & ImGuiColorEditFlags_InputHSV))\n            ColorConvertRGBtoHSV(col[0], col[1], col[2], col[0], col[1], col[2]);\n        EndDragDropTarget();\n    }\n\n    // When picker is being actively used, use its active id so IsItemActive() will function on ColorEdit4().\n    if (picker_active_window && g.ActiveId != 0 && g.ActiveIdWindow == picker_active_window)\n        window->DC.LastItemId = g.ActiveId;\n\n    if (value_changed)\n        MarkItemEdited(window->DC.LastItemId);\n\n    return value_changed;\n}\n\nbool ImGui::ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags flags)\n{\n    float col4[4] = { col[0], col[1], col[2], 1.0f };\n    if (!ColorPicker4(label, col4, flags | ImGuiColorEditFlags_NoAlpha))\n        return false;\n    col[0] = col4[0]; col[1] = col4[1]; col[2] = col4[2];\n    return true;\n}\n\nstatic inline ImU32 ImAlphaBlendColor(ImU32 col_a, ImU32 col_b)\n{\n    float t = ((col_b >> IM_COL32_A_SHIFT) & 0xFF) / 255.f;\n    int r = ImLerp((int)(col_a >> IM_COL32_R_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_R_SHIFT) & 0xFF, t);\n    int g = ImLerp((int)(col_a >> IM_COL32_G_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_G_SHIFT) & 0xFF, t);\n    int b = ImLerp((int)(col_a >> IM_COL32_B_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_B_SHIFT) & 0xFF, t);\n    return IM_COL32(r, g, b, 0xFF);\n}\n\n// Helper for ColorPicker4()\n// NB: This is rather brittle and will show artifact when rounding this enabled if rounded corners overlap multiple cells. Caller currently responsible for avoiding that.\n// I spent a non reasonable amount of time trying to getting this right for ColorButton with rounding+anti-aliasing+ImGuiColorEditFlags_HalfAlphaPreview flag + various grid sizes and offsets, and eventually gave up... probably more reasonable to disable rounding alltogether.\nvoid ImGui::RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 col, float grid_step, ImVec2 grid_off, float rounding, int rounding_corners_flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (((col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT) < 0xFF)\n    {\n        ImU32 col_bg1 = GetColorU32(ImAlphaBlendColor(IM_COL32(204,204,204,255), col));\n        ImU32 col_bg2 = GetColorU32(ImAlphaBlendColor(IM_COL32(128,128,128,255), col));\n        window->DrawList->AddRectFilled(p_min, p_max, col_bg1, rounding, rounding_corners_flags);\n\n        int yi = 0;\n        for (float y = p_min.y + grid_off.y; y < p_max.y; y += grid_step, yi++)\n        {\n            float y1 = ImClamp(y, p_min.y, p_max.y), y2 = ImMin(y + grid_step, p_max.y);\n            if (y2 <= y1)\n                continue;\n            for (float x = p_min.x + grid_off.x + (yi & 1) * grid_step; x < p_max.x; x += grid_step * 2.0f)\n            {\n                float x1 = ImClamp(x, p_min.x, p_max.x), x2 = ImMin(x + grid_step, p_max.x);\n                if (x2 <= x1)\n                    continue;\n                int rounding_corners_flags_cell = 0;\n                if (y1 <= p_min.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopRight; }\n                if (y2 >= p_max.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotRight; }\n                rounding_corners_flags_cell &= rounding_corners_flags;\n                window->DrawList->AddRectFilled(ImVec2(x1,y1), ImVec2(x2,y2), col_bg2, rounding_corners_flags_cell ? rounding : 0.0f, rounding_corners_flags_cell);\n            }\n        }\n    }\n    else\n    {\n        window->DrawList->AddRectFilled(p_min, p_max, col, rounding, rounding_corners_flags);\n    }\n}\n\n// Helper for ColorPicker4()\nstatic void RenderArrowsForVerticalBar(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, float bar_w)\n{\n    ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + half_sz.x + 1,         pos.y), ImVec2(half_sz.x + 2, half_sz.y + 1), ImGuiDir_Right, IM_COL32_BLACK);\n    ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + half_sz.x,             pos.y), half_sz,                              ImGuiDir_Right, IM_COL32_WHITE);\n    ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + bar_w - half_sz.x - 1, pos.y), ImVec2(half_sz.x + 2, half_sz.y + 1), ImGuiDir_Left,  IM_COL32_BLACK);\n    ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + bar_w - half_sz.x,     pos.y), half_sz,                              ImGuiDir_Left,  IM_COL32_WHITE);\n}\n\n// Note: ColorPicker4() only accesses 3 floats if ImGuiColorEditFlags_NoAlpha flag is set.\n// (In C++ the 'float col[4]' notation for a function argument is equivalent to 'float* col', we only specify a size to facilitate understanding of the code.)\n// FIXME: we adjust the big color square height based on item width, which may cause a flickering feedback loop (if automatic height makes a vertical scrollbar appears, affecting automatic width..)\nbool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags flags, const float* ref_col)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImDrawList* draw_list = window->DrawList;\n    ImGuiStyle& style = g.Style;\n    ImGuiIO& io = g.IO;\n\n    PushID(label);\n    BeginGroup();\n\n    if (!(flags & ImGuiColorEditFlags_NoSidePreview))\n        flags |= ImGuiColorEditFlags_NoSmallPreview;\n\n    // Context menu: display and store options.\n    if (!(flags & ImGuiColorEditFlags_NoOptions))\n        ColorPickerOptionsPopup(col, flags);\n\n    // Read stored options\n    if (!(flags & ImGuiColorEditFlags__PickerMask))\n        flags |= ((g.ColorEditOptions & ImGuiColorEditFlags__PickerMask) ? g.ColorEditOptions : ImGuiColorEditFlags__OptionsDefault) & ImGuiColorEditFlags__PickerMask;\n    if (!(flags & ImGuiColorEditFlags__InputMask))\n        flags |= ((g.ColorEditOptions & ImGuiColorEditFlags__InputMask) ? g.ColorEditOptions : ImGuiColorEditFlags__OptionsDefault) & ImGuiColorEditFlags__InputMask;\n    IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__PickerMask)); // Check that only 1 is selected\n    IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask));  // Check that only 1 is selected\n    if (!(flags & ImGuiColorEditFlags_NoOptions))\n        flags |= (g.ColorEditOptions & ImGuiColorEditFlags_AlphaBar);\n\n    // Setup\n    int components = (flags & ImGuiColorEditFlags_NoAlpha) ? 3 : 4;\n    bool alpha_bar = (flags & ImGuiColorEditFlags_AlphaBar) && !(flags & ImGuiColorEditFlags_NoAlpha);\n    ImVec2 picker_pos = window->DC.CursorPos;\n    float square_sz = GetFrameHeight();\n    float bars_width = square_sz; // Arbitrary smallish width of Hue/Alpha picking bars\n    float sv_picker_size = ImMax(bars_width * 1, GetNextItemWidth() - (alpha_bar ? 2 : 1) * (bars_width + style.ItemInnerSpacing.x)); // Saturation/Value picking box\n    float bar0_pos_x = picker_pos.x + sv_picker_size + style.ItemInnerSpacing.x;\n    float bar1_pos_x = bar0_pos_x + bars_width + style.ItemInnerSpacing.x;\n    float bars_triangles_half_sz = (float)(int)(bars_width * 0.20f);\n\n    float backup_initial_col[4];\n    memcpy(backup_initial_col, col, components * sizeof(float));\n\n    float wheel_thickness = sv_picker_size * 0.08f;\n    float wheel_r_outer = sv_picker_size * 0.50f;\n    float wheel_r_inner = wheel_r_outer - wheel_thickness;\n    ImVec2 wheel_center(picker_pos.x + (sv_picker_size + bars_width)*0.5f, picker_pos.y + sv_picker_size*0.5f);\n\n    // Note: the triangle is displayed rotated with triangle_pa pointing to Hue, but most coordinates stays unrotated for logic.\n    float triangle_r = wheel_r_inner - (int)(sv_picker_size * 0.027f);\n    ImVec2 triangle_pa = ImVec2(triangle_r, 0.0f); // Hue point.\n    ImVec2 triangle_pb = ImVec2(triangle_r * -0.5f, triangle_r * -0.866025f); // Black point.\n    ImVec2 triangle_pc = ImVec2(triangle_r * -0.5f, triangle_r * +0.866025f); // White point.\n\n    float H = col[0], S = col[1], V = col[2];\n    float R = col[0], G = col[1], B = col[2];\n    if (flags & ImGuiColorEditFlags_InputRGB)\n        ColorConvertRGBtoHSV(R, G, B, H, S, V);\n    else if (flags & ImGuiColorEditFlags_InputHSV)\n        ColorConvertHSVtoRGB(H, S, V, R, G, B);\n\n    bool value_changed = false, value_changed_h = false, value_changed_sv = false;\n\n    PushItemFlag(ImGuiItemFlags_NoNav, true);\n    if (flags & ImGuiColorEditFlags_PickerHueWheel)\n    {\n        // Hue wheel + SV triangle logic\n        InvisibleButton(\"hsv\", ImVec2(sv_picker_size + style.ItemInnerSpacing.x + bars_width, sv_picker_size));\n        if (IsItemActive())\n        {\n            ImVec2 initial_off = g.IO.MouseClickedPos[0] - wheel_center;\n            ImVec2 current_off = g.IO.MousePos - wheel_center;\n            float initial_dist2 = ImLengthSqr(initial_off);\n            if (initial_dist2 >= (wheel_r_inner-1)*(wheel_r_inner-1) && initial_dist2 <= (wheel_r_outer+1)*(wheel_r_outer+1))\n            {\n                // Interactive with Hue wheel\n                H = ImAtan2(current_off.y, current_off.x) / IM_PI*0.5f;\n                if (H < 0.0f)\n                    H += 1.0f;\n                value_changed = value_changed_h = true;\n            }\n            float cos_hue_angle = ImCos(-H * 2.0f * IM_PI);\n            float sin_hue_angle = ImSin(-H * 2.0f * IM_PI);\n            if (ImTriangleContainsPoint(triangle_pa, triangle_pb, triangle_pc, ImRotate(initial_off, cos_hue_angle, sin_hue_angle)))\n            {\n                // Interacting with SV triangle\n                ImVec2 current_off_unrotated = ImRotate(current_off, cos_hue_angle, sin_hue_angle);\n                if (!ImTriangleContainsPoint(triangle_pa, triangle_pb, triangle_pc, current_off_unrotated))\n                    current_off_unrotated = ImTriangleClosestPoint(triangle_pa, triangle_pb, triangle_pc, current_off_unrotated);\n                float uu, vv, ww;\n                ImTriangleBarycentricCoords(triangle_pa, triangle_pb, triangle_pc, current_off_unrotated, uu, vv, ww);\n                V = ImClamp(1.0f - vv, 0.0001f, 1.0f);\n                S = ImClamp(uu / V, 0.0001f, 1.0f);\n                value_changed = value_changed_sv = true;\n            }\n        }\n        if (!(flags & ImGuiColorEditFlags_NoOptions))\n            OpenPopupOnItemClick(\"context\");\n    }\n    else if (flags & ImGuiColorEditFlags_PickerHueBar)\n    {\n        // SV rectangle logic\n        InvisibleButton(\"sv\", ImVec2(sv_picker_size, sv_picker_size));\n        if (IsItemActive())\n        {\n            S = ImSaturate((io.MousePos.x - picker_pos.x) / (sv_picker_size-1));\n            V = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1));\n            value_changed = value_changed_sv = true;\n        }\n        if (!(flags & ImGuiColorEditFlags_NoOptions))\n            OpenPopupOnItemClick(\"context\");\n\n        // Hue bar logic\n        SetCursorScreenPos(ImVec2(bar0_pos_x, picker_pos.y));\n        InvisibleButton(\"hue\", ImVec2(bars_width, sv_picker_size));\n        if (IsItemActive())\n        {\n            H = ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1));\n            value_changed = value_changed_h = true;\n        }\n    }\n\n    // Alpha bar logic\n    if (alpha_bar)\n    {\n        SetCursorScreenPos(ImVec2(bar1_pos_x, picker_pos.y));\n        InvisibleButton(\"alpha\", ImVec2(bars_width, sv_picker_size));\n        if (IsItemActive())\n        {\n            col[3] = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1));\n            value_changed = true;\n        }\n    }\n    PopItemFlag(); // ImGuiItemFlags_NoNav\n\n    if (!(flags & ImGuiColorEditFlags_NoSidePreview))\n    {\n        SameLine(0, style.ItemInnerSpacing.x);\n        BeginGroup();\n    }\n\n    if (!(flags & ImGuiColorEditFlags_NoLabel))\n    {\n        const char* label_display_end = FindRenderedTextEnd(label);\n        if (label != label_display_end)\n        {\n            if ((flags & ImGuiColorEditFlags_NoSidePreview))\n                SameLine(0, style.ItemInnerSpacing.x);\n            TextEx(label, label_display_end);\n        }\n    }\n\n    if (!(flags & ImGuiColorEditFlags_NoSidePreview))\n    {\n        PushItemFlag(ImGuiItemFlags_NoNavDefaultFocus, true);\n        ImVec4 col_v4(col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]);\n        if ((flags & ImGuiColorEditFlags_NoLabel))\n            Text(\"Current\");\n\n        ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf | ImGuiColorEditFlags_NoTooltip;\n        ColorButton(\"##current\", col_v4, (flags & sub_flags_to_forward), ImVec2(square_sz * 3, square_sz * 2));\n        if (ref_col != NULL)\n        {\n            Text(\"Original\");\n            ImVec4 ref_col_v4(ref_col[0], ref_col[1], ref_col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : ref_col[3]);\n            if (ColorButton(\"##original\", ref_col_v4, (flags & sub_flags_to_forward), ImVec2(square_sz * 3, square_sz * 2)))\n            {\n                memcpy(col, ref_col, components * sizeof(float));\n                value_changed = true;\n            }\n        }\n        PopItemFlag();\n        EndGroup();\n    }\n\n    // Convert back color to RGB\n    if (value_changed_h || value_changed_sv)\n    {\n        if (flags & ImGuiColorEditFlags_InputRGB)\n        {\n            ColorConvertHSVtoRGB(H >= 1.0f ? H - 10 * 1e-6f : H, S > 0.0f ? S : 10*1e-6f, V > 0.0f ? V : 1e-6f, col[0], col[1], col[2]);\n        }\n        else if (flags & ImGuiColorEditFlags_InputHSV)\n        {\n            col[0] = H;\n            col[1] = S;\n            col[2] = V;\n        }\n    }\n\n    // R,G,B and H,S,V slider color editor\n    bool value_changed_fix_hue_wrap = false;\n    if ((flags & ImGuiColorEditFlags_NoInputs) == 0)\n    {\n        PushItemWidth((alpha_bar ? bar1_pos_x : bar0_pos_x) + bars_width - picker_pos.x);\n        ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoSmallPreview | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf;\n        ImGuiColorEditFlags sub_flags = (flags & sub_flags_to_forward) | ImGuiColorEditFlags_NoPicker;\n        if (flags & ImGuiColorEditFlags_DisplayRGB || (flags & ImGuiColorEditFlags__DisplayMask) == 0)\n            if (ColorEdit4(\"##rgb\", col, sub_flags | ImGuiColorEditFlags_DisplayRGB))\n            {\n                // FIXME: Hackily differenciating using the DragInt (ActiveId != 0 && !ActiveIdAllowOverlap) vs. using the InputText or DropTarget.\n                // For the later we don't want to run the hue-wrap canceling code. If you are well versed in HSV picker please provide your input! (See #2050)\n                value_changed_fix_hue_wrap = (g.ActiveId != 0 && !g.ActiveIdAllowOverlap);\n                value_changed = true;\n            }\n        if (flags & ImGuiColorEditFlags_DisplayHSV || (flags & ImGuiColorEditFlags__DisplayMask) == 0)\n            value_changed |= ColorEdit4(\"##hsv\", col, sub_flags | ImGuiColorEditFlags_DisplayHSV);\n        if (flags & ImGuiColorEditFlags_DisplayHex || (flags & ImGuiColorEditFlags__DisplayMask) == 0)\n            value_changed |= ColorEdit4(\"##hex\", col, sub_flags | ImGuiColorEditFlags_DisplayHex);\n        PopItemWidth();\n    }\n\n    // Try to cancel hue wrap (after ColorEdit4 call), if any\n    if (value_changed_fix_hue_wrap && (flags & ImGuiColorEditFlags_InputRGB))\n    {\n        float new_H, new_S, new_V;\n        ColorConvertRGBtoHSV(col[0], col[1], col[2], new_H, new_S, new_V);\n        if (new_H <= 0 && H > 0)\n        {\n            if (new_V <= 0 && V != new_V)\n                ColorConvertHSVtoRGB(H, S, new_V <= 0 ? V * 0.5f : new_V, col[0], col[1], col[2]);\n            else if (new_S <= 0)\n                ColorConvertHSVtoRGB(H, new_S <= 0 ? S * 0.5f : new_S, new_V, col[0], col[1], col[2]);\n        }\n    }\n\n    if (value_changed)\n    {\n        if (flags & ImGuiColorEditFlags_InputRGB)\n        {\n            R = col[0];\n            G = col[1];\n            B = col[2];\n            ColorConvertRGBtoHSV(R, G, B, H, S, V);\n        }\n        else if (flags & ImGuiColorEditFlags_InputHSV)\n        {\n            H = col[0];\n            S = col[1];\n            V = col[2];\n            ColorConvertHSVtoRGB(H, S, V, R, G, B);\n        }\n    }\n\n    ImVec4 hue_color_f(1, 1, 1, 1); ColorConvertHSVtoRGB(H, 1, 1, hue_color_f.x, hue_color_f.y, hue_color_f.z);\n    ImU32 hue_color32 = ColorConvertFloat4ToU32(hue_color_f);\n    ImU32 col32_no_alpha = ColorConvertFloat4ToU32(ImVec4(R, G, B, 1.0f));\n\n    const ImU32 hue_colors[6+1] = { IM_COL32(255,0,0,255), IM_COL32(255,255,0,255), IM_COL32(0,255,0,255), IM_COL32(0,255,255,255), IM_COL32(0,0,255,255), IM_COL32(255,0,255,255), IM_COL32(255,0,0,255) };\n    ImVec2 sv_cursor_pos;\n\n    if (flags & ImGuiColorEditFlags_PickerHueWheel)\n    {\n        // Render Hue Wheel\n        const float aeps = 1.5f / wheel_r_outer; // Half a pixel arc length in radians (2pi cancels out).\n        const int segment_per_arc = ImMax(4, (int)wheel_r_outer / 12);\n        for (int n = 0; n < 6; n++)\n        {\n            const float a0 = (n)     /6.0f * 2.0f * IM_PI - aeps;\n            const float a1 = (n+1.0f)/6.0f * 2.0f * IM_PI + aeps;\n            const int vert_start_idx = draw_list->VtxBuffer.Size;\n            draw_list->PathArcTo(wheel_center, (wheel_r_inner + wheel_r_outer)*0.5f, a0, a1, segment_per_arc);\n            draw_list->PathStroke(IM_COL32_WHITE, false, wheel_thickness);\n            const int vert_end_idx = draw_list->VtxBuffer.Size;\n\n            // Paint colors over existing vertices\n            ImVec2 gradient_p0(wheel_center.x + ImCos(a0) * wheel_r_inner, wheel_center.y + ImSin(a0) * wheel_r_inner);\n            ImVec2 gradient_p1(wheel_center.x + ImCos(a1) * wheel_r_inner, wheel_center.y + ImSin(a1) * wheel_r_inner);\n            ShadeVertsLinearColorGradientKeepAlpha(draw_list, vert_start_idx, vert_end_idx, gradient_p0, gradient_p1, hue_colors[n], hue_colors[n+1]);\n        }\n\n        // Render Cursor + preview on Hue Wheel\n        float cos_hue_angle = ImCos(H * 2.0f * IM_PI);\n        float sin_hue_angle = ImSin(H * 2.0f * IM_PI);\n        ImVec2 hue_cursor_pos(wheel_center.x + cos_hue_angle * (wheel_r_inner+wheel_r_outer)*0.5f, wheel_center.y + sin_hue_angle * (wheel_r_inner+wheel_r_outer)*0.5f);\n        float hue_cursor_rad = value_changed_h ? wheel_thickness * 0.65f : wheel_thickness * 0.55f;\n        int hue_cursor_segments = ImClamp((int)(hue_cursor_rad / 1.4f), 9, 32);\n        draw_list->AddCircleFilled(hue_cursor_pos, hue_cursor_rad, hue_color32, hue_cursor_segments);\n        draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad+1, IM_COL32(128,128,128,255), hue_cursor_segments);\n        draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad, IM_COL32_WHITE, hue_cursor_segments);\n\n        // Render SV triangle (rotated according to hue)\n        ImVec2 tra = wheel_center + ImRotate(triangle_pa, cos_hue_angle, sin_hue_angle);\n        ImVec2 trb = wheel_center + ImRotate(triangle_pb, cos_hue_angle, sin_hue_angle);\n        ImVec2 trc = wheel_center + ImRotate(triangle_pc, cos_hue_angle, sin_hue_angle);\n        ImVec2 uv_white = GetFontTexUvWhitePixel();\n        draw_list->PrimReserve(6, 6);\n        draw_list->PrimVtx(tra, uv_white, hue_color32);\n        draw_list->PrimVtx(trb, uv_white, hue_color32);\n        draw_list->PrimVtx(trc, uv_white, IM_COL32_WHITE);\n        draw_list->PrimVtx(tra, uv_white, IM_COL32_BLACK_TRANS);\n        draw_list->PrimVtx(trb, uv_white, IM_COL32_BLACK);\n        draw_list->PrimVtx(trc, uv_white, IM_COL32_BLACK_TRANS);\n        draw_list->AddTriangle(tra, trb, trc, IM_COL32(128,128,128,255), 1.5f);\n        sv_cursor_pos = ImLerp(ImLerp(trc, tra, ImSaturate(S)), trb, ImSaturate(1 - V));\n    }\n    else if (flags & ImGuiColorEditFlags_PickerHueBar)\n    {\n        // Render SV Square\n        draw_list->AddRectFilledMultiColor(picker_pos, picker_pos + ImVec2(sv_picker_size,sv_picker_size), IM_COL32_WHITE, hue_color32, hue_color32, IM_COL32_WHITE);\n        draw_list->AddRectFilledMultiColor(picker_pos, picker_pos + ImVec2(sv_picker_size,sv_picker_size), IM_COL32_BLACK_TRANS, IM_COL32_BLACK_TRANS, IM_COL32_BLACK, IM_COL32_BLACK);\n        RenderFrameBorder(picker_pos, picker_pos + ImVec2(sv_picker_size,sv_picker_size), 0.0f);\n        sv_cursor_pos.x = ImClamp((float)(int)(picker_pos.x + ImSaturate(S)     * sv_picker_size + 0.5f), picker_pos.x + 2, picker_pos.x + sv_picker_size - 2); // Sneakily prevent the circle to stick out too much\n        sv_cursor_pos.y = ImClamp((float)(int)(picker_pos.y + ImSaturate(1 - V) * sv_picker_size + 0.5f), picker_pos.y + 2, picker_pos.y + sv_picker_size - 2);\n\n        // Render Hue Bar\n        for (int i = 0; i < 6; ++i)\n            draw_list->AddRectFilledMultiColor(ImVec2(bar0_pos_x, picker_pos.y + i * (sv_picker_size / 6)), ImVec2(bar0_pos_x + bars_width, picker_pos.y + (i + 1) * (sv_picker_size / 6)), hue_colors[i], hue_colors[i], hue_colors[i + 1], hue_colors[i + 1]);\n        float bar0_line_y = (float)(int)(picker_pos.y + H * sv_picker_size + 0.5f);\n        RenderFrameBorder(ImVec2(bar0_pos_x, picker_pos.y), ImVec2(bar0_pos_x + bars_width, picker_pos.y + sv_picker_size), 0.0f);\n        RenderArrowsForVerticalBar(draw_list, ImVec2(bar0_pos_x - 1, bar0_line_y), ImVec2(bars_triangles_half_sz + 1, bars_triangles_half_sz), bars_width + 2.0f);\n    }\n\n    // Render cursor/preview circle (clamp S/V within 0..1 range because floating points colors may lead HSV values to be out of range)\n    float sv_cursor_rad = value_changed_sv ? 10.0f : 6.0f;\n    draw_list->AddCircleFilled(sv_cursor_pos, sv_cursor_rad, col32_no_alpha, 12);\n    draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad+1, IM_COL32(128,128,128,255), 12);\n    draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad, IM_COL32_WHITE, 12);\n\n    // Render alpha bar\n    if (alpha_bar)\n    {\n        float alpha = ImSaturate(col[3]);\n        ImRect bar1_bb(bar1_pos_x, picker_pos.y, bar1_pos_x + bars_width, picker_pos.y + sv_picker_size);\n        RenderColorRectWithAlphaCheckerboard(bar1_bb.Min, bar1_bb.Max, IM_COL32(0,0,0,0), bar1_bb.GetWidth() / 2.0f, ImVec2(0.0f, 0.0f));\n        draw_list->AddRectFilledMultiColor(bar1_bb.Min, bar1_bb.Max, col32_no_alpha, col32_no_alpha, col32_no_alpha & ~IM_COL32_A_MASK, col32_no_alpha & ~IM_COL32_A_MASK);\n        float bar1_line_y = (float)(int)(picker_pos.y + (1.0f - alpha) * sv_picker_size + 0.5f);\n        RenderFrameBorder(bar1_bb.Min, bar1_bb.Max, 0.0f);\n        RenderArrowsForVerticalBar(draw_list, ImVec2(bar1_pos_x - 1, bar1_line_y), ImVec2(bars_triangles_half_sz + 1, bars_triangles_half_sz), bars_width + 2.0f);\n    }\n\n    EndGroup();\n\n    if (value_changed && memcmp(backup_initial_col, col, components * sizeof(float)) == 0)\n        value_changed = false;\n    if (value_changed)\n        MarkItemEdited(window->DC.LastItemId);\n\n    PopID();\n\n    return value_changed;\n}\n\n// A little colored square. Return true when clicked.\n// FIXME: May want to display/ignore the alpha component in the color display? Yet show it in the tooltip.\n// 'desc_id' is not called 'label' because we don't display it next to the button, but only in the tooltip.\n// Note that 'col' may be encoded in HSV if ImGuiColorEditFlags_InputHSV is set.\nbool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags, ImVec2 size)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiID id = window->GetID(desc_id);\n    float default_size = GetFrameHeight();\n    if (size.x == 0.0f)\n        size.x = default_size;\n    if (size.y == 0.0f)\n        size.y = default_size;\n    const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);\n    ItemSize(bb, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f);\n    if (!ItemAdd(bb, id))\n        return false;\n\n    bool hovered, held;\n    bool pressed = ButtonBehavior(bb, id, &hovered, &held);\n\n    if (flags & ImGuiColorEditFlags_NoAlpha)\n        flags &= ~(ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf);\n\n    ImVec4 col_rgb = col;\n    if (flags & ImGuiColorEditFlags_InputHSV)\n        ColorConvertHSVtoRGB(col_rgb.x, col_rgb.y, col_rgb.z, col_rgb.x, col_rgb.y, col_rgb.z);\n\n    ImVec4 col_rgb_without_alpha(col_rgb.x, col_rgb.y, col_rgb.z, 1.0f);\n    float grid_step = ImMin(size.x, size.y) / 2.99f;\n    float rounding = ImMin(g.Style.FrameRounding, grid_step * 0.5f);\n    ImRect bb_inner = bb;\n    float off = -0.75f; // The border (using Col_FrameBg) tends to look off when color is near-opaque and rounding is enabled. This offset seemed like a good middle ground to reduce those artifacts.\n    bb_inner.Expand(off);\n    if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f)\n    {\n        float mid_x = (float)(int)((bb_inner.Min.x + bb_inner.Max.x) * 0.5f + 0.5f);\n        RenderColorRectWithAlphaCheckerboard(ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawCornerFlags_TopRight| ImDrawCornerFlags_BotRight);\n        window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawCornerFlags_TopLeft|ImDrawCornerFlags_BotLeft);\n    }\n    else\n    {\n        // Because GetColorU32() multiplies by the global style Alpha and we don't want to display a checkerboard if the source code had no alpha\n        ImVec4 col_source = (flags & ImGuiColorEditFlags_AlphaPreview) ? col_rgb : col_rgb_without_alpha;\n        if (col_source.w < 1.0f)\n            RenderColorRectWithAlphaCheckerboard(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding);\n        else\n            window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding, ImDrawCornerFlags_All);\n    }\n    RenderNavHighlight(bb, id);\n    if (g.Style.FrameBorderSize > 0.0f)\n        RenderFrameBorder(bb.Min, bb.Max, rounding);\n    else\n        window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), rounding); // Color button are often in need of some sort of border\n\n    // Drag and Drop Source\n    // NB: The ActiveId test is merely an optional micro-optimization, BeginDragDropSource() does the same test.\n    if (g.ActiveId == id && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropSource())\n    {\n        if (flags & ImGuiColorEditFlags_NoAlpha)\n            SetDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F, &col_rgb, sizeof(float) * 3, ImGuiCond_Once);\n        else\n            SetDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F, &col_rgb, sizeof(float) * 4, ImGuiCond_Once);\n        ColorButton(desc_id, col, flags);\n        SameLine();\n        TextEx(\"Color\");\n        EndDragDropSource();\n    }\n\n    // Tooltip\n    if (!(flags & ImGuiColorEditFlags_NoTooltip) && hovered)\n        ColorTooltip(desc_id, &col.x, flags & (ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf));\n\n    if (pressed)\n        MarkItemEdited(id);\n\n    return pressed;\n}\n\n// Initialize/override default color options\nvoid ImGui::SetColorEditOptions(ImGuiColorEditFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    if ((flags & ImGuiColorEditFlags__DisplayMask) == 0)\n        flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__DisplayMask;\n    if ((flags & ImGuiColorEditFlags__DataTypeMask) == 0)\n        flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__DataTypeMask;\n    if ((flags & ImGuiColorEditFlags__PickerMask) == 0)\n        flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__PickerMask;\n    if ((flags & ImGuiColorEditFlags__InputMask) == 0)\n        flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__InputMask;\n    IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DisplayMask));    // Check only 1 option is selected\n    IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DataTypeMask));   // Check only 1 option is selected\n    IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__PickerMask));     // Check only 1 option is selected\n    IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask));      // Check only 1 option is selected\n    g.ColorEditOptions = flags;\n}\n\n// Note: only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set.\nvoid ImGui::ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n\n    BeginTooltipEx(0, true);\n    const char* text_end = text ? FindRenderedTextEnd(text, NULL) : text;\n    if (text_end > text)\n    {\n        TextEx(text, text_end);\n        Separator();\n    }\n\n    ImVec2 sz(g.FontSize * 3 + g.Style.FramePadding.y * 2, g.FontSize * 3 + g.Style.FramePadding.y * 2);\n    ImVec4 cf(col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]);\n    int cr = IM_F32_TO_INT8_SAT(col[0]), cg = IM_F32_TO_INT8_SAT(col[1]), cb = IM_F32_TO_INT8_SAT(col[2]), ca = (flags & ImGuiColorEditFlags_NoAlpha) ? 255 : IM_F32_TO_INT8_SAT(col[3]);\n    ColorButton(\"##preview\", cf, (flags & (ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)) | ImGuiColorEditFlags_NoTooltip, sz);\n    SameLine();\n    if ((flags & ImGuiColorEditFlags_InputRGB) || !(flags & ImGuiColorEditFlags__InputMask))\n    {\n        if (flags & ImGuiColorEditFlags_NoAlpha)\n            Text(\"#%02X%02X%02X\\nR: %d, G: %d, B: %d\\n(%.3f, %.3f, %.3f)\", cr, cg, cb, cr, cg, cb, col[0], col[1], col[2]);\n        else\n            Text(\"#%02X%02X%02X%02X\\nR:%d, G:%d, B:%d, A:%d\\n(%.3f, %.3f, %.3f, %.3f)\", cr, cg, cb, ca, cr, cg, cb, ca, col[0], col[1], col[2], col[3]);\n    }\n    else if (flags & ImGuiColorEditFlags_InputHSV)\n    {\n        if (flags & ImGuiColorEditFlags_NoAlpha)\n            Text(\"H: %.3f, S: %.3f, V: %.3f\", col[0], col[1], col[2]);\n        else\n            Text(\"H: %.3f, S: %.3f, V: %.3f, A: %.3f\", col[0], col[1], col[2], col[3]);\n    }\n    EndTooltip();\n}\n\nvoid ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags)\n{\n    bool allow_opt_inputs = !(flags & ImGuiColorEditFlags__DisplayMask);\n    bool allow_opt_datatype = !(flags & ImGuiColorEditFlags__DataTypeMask);\n    if ((!allow_opt_inputs && !allow_opt_datatype) || !BeginPopup(\"context\"))\n        return;\n    ImGuiContext& g = *GImGui;\n    ImGuiColorEditFlags opts = g.ColorEditOptions;\n    if (allow_opt_inputs)\n    {\n        if (RadioButton(\"RGB\", (opts & ImGuiColorEditFlags_DisplayRGB) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayRGB;\n        if (RadioButton(\"HSV\", (opts & ImGuiColorEditFlags_DisplayHSV) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayHSV;\n        if (RadioButton(\"Hex\", (opts & ImGuiColorEditFlags_DisplayHex) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayHex;\n    }\n    if (allow_opt_datatype)\n    {\n        if (allow_opt_inputs) Separator();\n        if (RadioButton(\"0..255\",     (opts & ImGuiColorEditFlags_Uint8) != 0)) opts = (opts & ~ImGuiColorEditFlags__DataTypeMask) | ImGuiColorEditFlags_Uint8;\n        if (RadioButton(\"0.00..1.00\", (opts & ImGuiColorEditFlags_Float) != 0)) opts = (opts & ~ImGuiColorEditFlags__DataTypeMask) | ImGuiColorEditFlags_Float;\n    }\n\n    if (allow_opt_inputs || allow_opt_datatype)\n        Separator();\n    if (Button(\"Copy as..\", ImVec2(-1,0)))\n        OpenPopup(\"Copy\");\n    if (BeginPopup(\"Copy\"))\n    {\n        int cr = IM_F32_TO_INT8_SAT(col[0]), cg = IM_F32_TO_INT8_SAT(col[1]), cb = IM_F32_TO_INT8_SAT(col[2]), ca = (flags & ImGuiColorEditFlags_NoAlpha) ? 255 : IM_F32_TO_INT8_SAT(col[3]);\n        char buf[64];\n        ImFormatString(buf, IM_ARRAYSIZE(buf), \"(%.3ff, %.3ff, %.3ff, %.3ff)\", col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]);\n        if (Selectable(buf))\n            SetClipboardText(buf);\n        ImFormatString(buf, IM_ARRAYSIZE(buf), \"(%d,%d,%d,%d)\", cr, cg, cb, ca);\n        if (Selectable(buf))\n            SetClipboardText(buf);\n        if (flags & ImGuiColorEditFlags_NoAlpha)\n            ImFormatString(buf, IM_ARRAYSIZE(buf), \"0x%02X%02X%02X\", cr, cg, cb);\n        else\n            ImFormatString(buf, IM_ARRAYSIZE(buf), \"0x%02X%02X%02X%02X\", cr, cg, cb, ca);\n        if (Selectable(buf))\n            SetClipboardText(buf);\n        EndPopup();\n    }\n\n    g.ColorEditOptions = opts;\n    EndPopup();\n}\n\nvoid ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags)\n{\n    bool allow_opt_picker = !(flags & ImGuiColorEditFlags__PickerMask);\n    bool allow_opt_alpha_bar = !(flags & ImGuiColorEditFlags_NoAlpha) && !(flags & ImGuiColorEditFlags_AlphaBar);\n    if ((!allow_opt_picker && !allow_opt_alpha_bar) || !BeginPopup(\"context\"))\n        return;\n    ImGuiContext& g = *GImGui;\n    if (allow_opt_picker)\n    {\n        ImVec2 picker_size(g.FontSize * 8, ImMax(g.FontSize * 8 - (GetFrameHeight() + g.Style.ItemInnerSpacing.x), 1.0f)); // FIXME: Picker size copied from main picker function\n        PushItemWidth(picker_size.x);\n        for (int picker_type = 0; picker_type < 2; picker_type++)\n        {\n            // Draw small/thumbnail version of each picker type (over an invisible button for selection)\n            if (picker_type > 0) Separator();\n            PushID(picker_type);\n            ImGuiColorEditFlags picker_flags = ImGuiColorEditFlags_NoInputs|ImGuiColorEditFlags_NoOptions|ImGuiColorEditFlags_NoLabel|ImGuiColorEditFlags_NoSidePreview|(flags & ImGuiColorEditFlags_NoAlpha);\n            if (picker_type == 0) picker_flags |= ImGuiColorEditFlags_PickerHueBar;\n            if (picker_type == 1) picker_flags |= ImGuiColorEditFlags_PickerHueWheel;\n            ImVec2 backup_pos = GetCursorScreenPos();\n            if (Selectable(\"##selectable\", false, 0, picker_size)) // By default, Selectable() is closing popup\n                g.ColorEditOptions = (g.ColorEditOptions & ~ImGuiColorEditFlags__PickerMask) | (picker_flags & ImGuiColorEditFlags__PickerMask);\n            SetCursorScreenPos(backup_pos);\n            ImVec4 dummy_ref_col;\n            memcpy(&dummy_ref_col, ref_col, sizeof(float) * ((picker_flags & ImGuiColorEditFlags_NoAlpha) ? 3 : 4));\n            ColorPicker4(\"##dummypicker\", &dummy_ref_col.x, picker_flags);\n            PopID();\n        }\n        PopItemWidth();\n    }\n    if (allow_opt_alpha_bar)\n    {\n        if (allow_opt_picker) Separator();\n        CheckboxFlags(\"Alpha Bar\", (unsigned int*)&g.ColorEditOptions, ImGuiColorEditFlags_AlphaBar);\n    }\n    EndPopup();\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: TreeNode, CollapsingHeader, etc.\n//-------------------------------------------------------------------------\n// - TreeNode()\n// - TreeNodeV()\n// - TreeNodeEx()\n// - TreeNodeExV()\n// - TreeNodeBehavior() [Internal]\n// - TreePush()\n// - TreePop()\n// - TreeAdvanceToLabelPos()\n// - GetTreeNodeToLabelSpacing()\n// - SetNextTreeNodeOpen()\n// - CollapsingHeader()\n//-------------------------------------------------------------------------\n\nbool ImGui::TreeNode(const char* str_id, const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    bool is_open = TreeNodeExV(str_id, 0, fmt, args);\n    va_end(args);\n    return is_open;\n}\n\nbool ImGui::TreeNode(const void* ptr_id, const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    bool is_open = TreeNodeExV(ptr_id, 0, fmt, args);\n    va_end(args);\n    return is_open;\n}\n\nbool ImGui::TreeNode(const char* label)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n    return TreeNodeBehavior(window->GetID(label), 0, label, NULL);\n}\n\nbool ImGui::TreeNodeV(const char* str_id, const char* fmt, va_list args)\n{\n    return TreeNodeExV(str_id, 0, fmt, args);\n}\n\nbool ImGui::TreeNodeV(const void* ptr_id, const char* fmt, va_list args)\n{\n    return TreeNodeExV(ptr_id, 0, fmt, args);\n}\n\nbool ImGui::TreeNodeEx(const char* label, ImGuiTreeNodeFlags flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    return TreeNodeBehavior(window->GetID(label), flags, label, NULL);\n}\n\nbool ImGui::TreeNodeEx(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    bool is_open = TreeNodeExV(str_id, flags, fmt, args);\n    va_end(args);\n    return is_open;\n}\n\nbool ImGui::TreeNodeEx(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    bool is_open = TreeNodeExV(ptr_id, flags, fmt, args);\n    va_end(args);\n    return is_open;\n}\n\nbool ImGui::TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);\n    return TreeNodeBehavior(window->GetID(str_id), flags, g.TempBuffer, label_end);\n}\n\nbool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args);\n    return TreeNodeBehavior(window->GetID(ptr_id), flags, g.TempBuffer, label_end);\n}\n\nbool ImGui::TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags)\n{\n    if (flags & ImGuiTreeNodeFlags_Leaf)\n        return true;\n\n    // We only write to the tree storage if the user clicks (or explicitly use SetNextTreeNode*** functions)\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    ImGuiStorage* storage = window->DC.StateStorage;\n\n    bool is_open;\n    if (g.NextTreeNodeOpenCond != 0)\n    {\n        if (g.NextTreeNodeOpenCond & ImGuiCond_Always)\n        {\n            is_open = g.NextTreeNodeOpenVal;\n            storage->SetInt(id, is_open);\n        }\n        else\n        {\n            // We treat ImGuiCond_Once and ImGuiCond_FirstUseEver the same because tree node state are not saved persistently.\n            const int stored_value = storage->GetInt(id, -1);\n            if (stored_value == -1)\n            {\n                is_open = g.NextTreeNodeOpenVal;\n                storage->SetInt(id, is_open);\n            }\n            else\n            {\n                is_open = stored_value != 0;\n            }\n        }\n        g.NextTreeNodeOpenCond = 0;\n    }\n    else\n    {\n        is_open = storage->GetInt(id, (flags & ImGuiTreeNodeFlags_DefaultOpen) ? 1 : 0) != 0;\n    }\n\n    // When logging is enabled, we automatically expand tree nodes (but *NOT* collapsing headers.. seems like sensible behavior).\n    // NB- If we are above max depth we still allow manually opened nodes to be logged.\n    if (g.LogEnabled && !(flags & ImGuiTreeNodeFlags_NoAutoOpenOnLog) && (window->DC.TreeDepth - g.LogDepthRef) < g.LogDepthToExpand)\n        is_open = true;\n\n    return is_open;\n}\n\nbool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const bool display_frame = (flags & ImGuiTreeNodeFlags_Framed) != 0;\n    const ImVec2 padding = (display_frame || (flags & ImGuiTreeNodeFlags_FramePadding)) ? style.FramePadding : ImVec2(style.FramePadding.x, 0.0f);\n\n    if (!label_end)\n        label_end = FindRenderedTextEnd(label);\n    const ImVec2 label_size = CalcTextSize(label, label_end, false);\n\n    // We vertically grow up to current line height up the typical widget height.\n    const float text_base_offset_y = ImMax(padding.y, window->DC.CurrentLineTextBaseOffset); // Latch before ItemSize changes it\n    const float frame_height = ImMax(ImMin(window->DC.CurrentLineSize.y, g.FontSize + style.FramePadding.y*2), label_size.y + padding.y*2);\n    ImRect frame_bb = ImRect(window->DC.CursorPos, ImVec2(GetContentRegionMaxScreen().x, window->DC.CursorPos.y + frame_height));\n    if (display_frame)\n    {\n        // Framed header expand a little outside the default padding\n        frame_bb.Min.x -= (float)(int)(window->WindowPadding.x*0.5f) - 1;\n        frame_bb.Max.x += (float)(int)(window->WindowPadding.x*0.5f) - 1;\n    }\n\n    const float text_offset_x = (g.FontSize + (display_frame ? padding.x*3 : padding.x*2));   // Collapser arrow width + Spacing\n    const float text_width = g.FontSize + (label_size.x > 0.0f ? label_size.x + padding.x*2 : 0.0f);   // Include collapser\n    ItemSize(ImVec2(text_width, frame_height), text_base_offset_y);\n\n    // For regular tree nodes, we arbitrary allow to click past 2 worth of ItemSpacing\n    // (Ideally we'd want to add a flag for the user to specify if we want the hit test to be done up to the right side of the content or not)\n    const ImRect interact_bb = display_frame ? frame_bb : ImRect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Min.x + text_width + style.ItemSpacing.x*2, frame_bb.Max.y);\n    bool is_open = TreeNodeBehaviorIsOpen(id, flags);\n    bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0;\n\n    // Store a flag for the current depth to tell if we will allow closing this node when navigating one of its child.\n    // For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop().\n    // This is currently only support 32 level deep and we are fine with (1 << Depth) overflowing into a zero.\n    if (is_open && !g.NavIdIsAlive && (flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen))\n        window->DC.TreeStoreMayJumpToParentOnPop |= (1 << window->DC.TreeDepth);\n\n    bool item_add = ItemAdd(interact_bb, id);\n    window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HasDisplayRect;\n    window->DC.LastItemDisplayRect = frame_bb;\n\n    if (!item_add)\n    {\n        if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen))\n            TreePushOverrideID(id);\n        IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0));\n        return is_open;\n    }\n\n    // Flags that affects opening behavior:\n    // - 0 (default) .................... single-click anywhere to open\n    // - OpenOnDoubleClick .............. double-click anywhere to open\n    // - OpenOnArrow .................... single-click on arrow to open\n    // - OpenOnDoubleClick|OpenOnArrow .. single-click on arrow or double-click anywhere to open\n    ImGuiButtonFlags button_flags = ImGuiButtonFlags_NoKeyModifiers;\n    if (flags & ImGuiTreeNodeFlags_AllowItemOverlap)\n        button_flags |= ImGuiButtonFlags_AllowItemOverlap;\n    if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick)\n        button_flags |= ImGuiButtonFlags_PressedOnDoubleClick | ((flags & ImGuiTreeNodeFlags_OpenOnArrow) ? ImGuiButtonFlags_PressedOnClickRelease : 0);\n    if (!is_leaf)\n        button_flags |= ImGuiButtonFlags_PressedOnDragDropHold;\n\n    bool selected = (flags & ImGuiTreeNodeFlags_Selected) != 0;\n    const bool was_selected = selected;\n\n    bool hovered, held;\n    bool pressed = ButtonBehavior(interact_bb, id, &hovered, &held, button_flags);\n    bool toggled = false;\n    if (!is_leaf)\n    {\n        if (pressed)\n        {\n            toggled = !(flags & (ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) || (g.NavActivateId == id);\n            if (flags & ImGuiTreeNodeFlags_OpenOnArrow)\n                toggled |= IsMouseHoveringRect(interact_bb.Min, ImVec2(interact_bb.Min.x + text_offset_x, interact_bb.Max.y)) && (!g.NavDisableMouseHover);\n            if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick)\n                toggled |= g.IO.MouseDoubleClicked[0];\n            if (g.DragDropActive && is_open) // When using Drag and Drop \"hold to open\" we keep the node highlighted after opening, but never close it again.\n                toggled = false;\n        }\n\n        if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Left && is_open)\n        {\n            toggled = true;\n            NavMoveRequestCancel();\n        }\n        if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Right && !is_open) // If there's something upcoming on the line we may want to give it the priority?\n        {\n            toggled = true;\n            NavMoveRequestCancel();\n        }\n\n        if (toggled)\n        {\n            is_open = !is_open;\n            window->DC.StateStorage->SetInt(id, is_open);\n        }\n    }\n    if (flags & ImGuiTreeNodeFlags_AllowItemOverlap)\n        SetItemAllowOverlap();\n\n    // In this branch, TreeNodeBehavior() cannot toggle the selection so this will never trigger.\n    if (selected != was_selected) //-V547\n        window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledSelection;\n\n    // Render\n    const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);\n    const ImVec2 text_pos = frame_bb.Min + ImVec2(text_offset_x, text_base_offset_y);\n    ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_TypeThin;\n    if (display_frame)\n    {\n        // Framed type\n        RenderFrame(frame_bb.Min, frame_bb.Max, col, true, style.FrameRounding);\n        RenderNavHighlight(frame_bb, id, nav_highlight_flags);\n        RenderArrow(frame_bb.Min + ImVec2(padding.x, text_base_offset_y), is_open ? ImGuiDir_Down : ImGuiDir_Right, 1.0f);\n        if (g.LogEnabled)\n        {\n            // NB: '##' is normally used to hide text (as a library-wide feature), so we need to specify the text range to make sure the ## aren't stripped out here.\n            const char log_prefix[] = \"\\n##\";\n            const char log_suffix[] = \"##\";\n            LogRenderedText(&text_pos, log_prefix, log_prefix+3);\n            RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size);\n            LogRenderedText(&text_pos, log_suffix, log_suffix+2);\n        }\n        else\n        {\n            RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size);\n        }\n    }\n    else\n    {\n        // Unframed typed for tree nodes\n        if (hovered || selected)\n        {\n            RenderFrame(frame_bb.Min, frame_bb.Max, col, false);\n            RenderNavHighlight(frame_bb, id, nav_highlight_flags);\n        }\n\n        if (flags & ImGuiTreeNodeFlags_Bullet)\n            RenderBullet(frame_bb.Min + ImVec2(text_offset_x * 0.5f, g.FontSize*0.50f + text_base_offset_y));\n        else if (!is_leaf)\n            RenderArrow(frame_bb.Min + ImVec2(padding.x, g.FontSize*0.15f + text_base_offset_y), is_open ? ImGuiDir_Down : ImGuiDir_Right, 0.70f);\n        if (g.LogEnabled)\n            LogRenderedText(&text_pos, \">\");\n        RenderText(text_pos, label, label_end, false);\n    }\n\n    if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen))\n        TreePushOverrideID(id);\n    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0));\n    return is_open;\n}\n\nvoid ImGui::TreePush(const char* str_id)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    Indent();\n    window->DC.TreeDepth++;\n    PushID(str_id ? str_id : \"#TreePush\");\n}\n\nvoid ImGui::TreePush(const void* ptr_id)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    Indent();\n    window->DC.TreeDepth++;\n    PushID(ptr_id ? ptr_id : (const void*)\"#TreePush\");\n}\n\nvoid ImGui::TreePushOverrideID(ImGuiID id)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    Indent();\n    window->DC.TreeDepth++;\n    window->IDStack.push_back(id);\n}\n\nvoid ImGui::TreePop()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    Unindent();\n\n    window->DC.TreeDepth--;\n    if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet())\n        if (g.NavIdIsAlive && (window->DC.TreeStoreMayJumpToParentOnPop & (1 << window->DC.TreeDepth)))\n        {\n            SetNavID(window->IDStack.back(), g.NavLayer);\n            NavMoveRequestCancel();\n        }\n    window->DC.TreeStoreMayJumpToParentOnPop &= (1 << window->DC.TreeDepth) - 1;\n\n    IM_ASSERT(window->IDStack.Size > 1); // There should always be 1 element in the IDStack (pushed during window creation). If this triggers you called TreePop/PopID too much.\n    PopID();\n}\n\nvoid ImGui::TreeAdvanceToLabelPos()\n{\n    ImGuiContext& g = *GImGui;\n    g.CurrentWindow->DC.CursorPos.x += GetTreeNodeToLabelSpacing();\n}\n\n// Horizontal distance preceding label when using TreeNode() or Bullet()\nfloat ImGui::GetTreeNodeToLabelSpacing()\n{\n    ImGuiContext& g = *GImGui;\n    return g.FontSize + (g.Style.FramePadding.x * 2.0f);\n}\n\nvoid ImGui::SetNextTreeNodeOpen(bool is_open, ImGuiCond cond)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.CurrentWindow->SkipItems)\n        return;\n    g.NextTreeNodeOpenVal = is_open;\n    g.NextTreeNodeOpenCond = cond ? cond : ImGuiCond_Always;\n}\n\n// CollapsingHeader returns true when opened but do not indent nor push into the ID stack (because of the ImGuiTreeNodeFlags_NoTreePushOnOpen flag).\n// This is basically the same as calling TreeNodeEx(label, ImGuiTreeNodeFlags_CollapsingHeader). You can remove the _NoTreePushOnOpen flag if you want behavior closer to normal TreeNode().\nbool ImGui::CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    return TreeNodeBehavior(window->GetID(label), flags | ImGuiTreeNodeFlags_CollapsingHeader, label);\n}\n\nbool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags flags)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    if (p_open && !*p_open)\n        return false;\n\n    ImGuiID id = window->GetID(label);\n    bool is_open = TreeNodeBehavior(id, flags | ImGuiTreeNodeFlags_CollapsingHeader | (p_open ? ImGuiTreeNodeFlags_AllowItemOverlap : 0), label);\n    if (p_open)\n    {\n        // Create a small overlapping close button // FIXME: We can evolve this into user accessible helpers to add extra buttons on title bars, headers, etc.\n        ImGuiContext& g = *GImGui;\n        ImGuiItemHoveredDataBackup last_item_backup;\n        float button_radius = g.FontSize * 0.5f;\n        ImVec2 button_center = ImVec2(ImMin(window->DC.LastItemRect.Max.x, window->ClipRect.Max.x) - g.Style.FramePadding.x - button_radius, window->DC.LastItemRect.GetCenter().y);\n        if (CloseButton(window->GetID((void*)((intptr_t)id+1)), button_center, button_radius))\n            *p_open = false;\n        last_item_backup.Restore();\n    }\n\n    return is_open;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: Selectable\n//-------------------------------------------------------------------------\n// - Selectable()\n//-------------------------------------------------------------------------\n\n// Tip: pass a non-visible label (e.g. \"##dummy\") then you can use the space to draw other text or image.\n// But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID or use ##unique_id.\nbool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags flags, const ImVec2& size_arg)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n\n    if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) // FIXME-OPT: Avoid if vertically clipped.\n        PopClipRect();\n\n    ImGuiID id = window->GetID(label);\n    ImVec2 label_size = CalcTextSize(label, NULL, true);\n    ImVec2 size(size_arg.x != 0.0f ? size_arg.x : label_size.x, size_arg.y != 0.0f ? size_arg.y : label_size.y);\n    ImVec2 pos = window->DC.CursorPos;\n    pos.y += window->DC.CurrentLineTextBaseOffset;\n    ImRect bb_inner(pos, pos + size);\n    ItemSize(size);\n\n    // Fill horizontal space.\n    ImVec2 window_padding = window->WindowPadding;\n    float max_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? GetWindowContentRegionMax().x : GetContentRegionMax().x;\n    float w_draw = ImMax(label_size.x, window->Pos.x + max_x - window_padding.x - pos.x);\n    ImVec2 size_draw((size_arg.x != 0 && !(flags & ImGuiSelectableFlags_DrawFillAvailWidth)) ? size_arg.x : w_draw, size_arg.y != 0.0f ? size_arg.y : size.y);\n    ImRect bb(pos, pos + size_draw);\n    if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_DrawFillAvailWidth))\n        bb.Max.x += window_padding.x;\n\n    // Selectables are tightly packed together so we extend the box to cover spacing between selectable.\n    const float spacing_x = style.ItemSpacing.x;\n    const float spacing_y = style.ItemSpacing.y;\n    const float spacing_L = (float)(int)(spacing_x * 0.50f);\n    const float spacing_U = (float)(int)(spacing_y * 0.50f);\n    bb.Min.x -= spacing_L;\n    bb.Min.y -= spacing_U;\n    bb.Max.x += (spacing_x - spacing_L);\n    bb.Max.y += (spacing_y - spacing_U);\n\n    bool item_add;\n    if (flags & ImGuiSelectableFlags_Disabled)\n    {\n        ImGuiItemFlags backup_item_flags = window->DC.ItemFlags;\n        window->DC.ItemFlags |= ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNavDefaultFocus;\n        item_add = ItemAdd(bb, id);\n        window->DC.ItemFlags = backup_item_flags;\n    }\n    else\n    {\n        item_add = ItemAdd(bb, id);\n    }\n    if (!item_add)\n    {\n        if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns)\n            PushColumnClipRect();\n        return false;\n    }\n\n    // We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries\n    ImGuiButtonFlags button_flags = 0;\n    if (flags & ImGuiSelectableFlags_NoHoldingActiveID) button_flags |= ImGuiButtonFlags_NoHoldingActiveID;\n    if (flags & ImGuiSelectableFlags_PressedOnClick) button_flags |= ImGuiButtonFlags_PressedOnClick;\n    if (flags & ImGuiSelectableFlags_PressedOnRelease) button_flags |= ImGuiButtonFlags_PressedOnRelease;\n    if (flags & ImGuiSelectableFlags_Disabled) button_flags |= ImGuiButtonFlags_Disabled;\n    if (flags & ImGuiSelectableFlags_AllowDoubleClick) button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick;\n    if (flags & ImGuiSelectableFlags_AllowItemOverlap) button_flags |= ImGuiButtonFlags_AllowItemOverlap;\n\n    if (flags & ImGuiSelectableFlags_Disabled)\n        selected = false;\n\n    const bool was_selected = selected;\n    bool hovered, held;\n    bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags);\n    // Hovering selectable with mouse updates NavId accordingly so navigation can be resumed with gamepad/keyboard (this doesn't happen on most widgets)\n    if (pressed || hovered)\n        if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent)\n        {\n            g.NavDisableHighlight = true;\n            SetNavID(id, window->DC.NavLayerCurrent);\n        }\n    if (pressed)\n        MarkItemEdited(id);\n\n    if (flags & ImGuiSelectableFlags_AllowItemOverlap)\n        SetItemAllowOverlap();\n\n    // In this branch, Selectable() cannot toggle the selection so this will never trigger.\n    if (selected != was_selected) //-V547\n        window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledSelection;\n\n    // Render\n    if (hovered || selected)\n    {\n        const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);\n        RenderFrame(bb.Min, bb.Max, col, false, 0.0f);\n        RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding);\n    }\n\n    if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns)\n    {\n        PushColumnClipRect();\n        bb.Max.x -= (GetContentRegionMax().x - max_x);\n    }\n\n    if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]);\n    RenderTextClipped(bb_inner.Min, bb_inner.Max, label, NULL, &label_size, style.SelectableTextAlign, &bb);\n    if (flags & ImGuiSelectableFlags_Disabled) PopStyleColor();\n\n    // Automatically close popups\n    if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(window->DC.ItemFlags & ImGuiItemFlags_SelectableDontClosePopup))\n        CloseCurrentPopup();\n\n    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags);\n    return pressed;\n}\n\nbool ImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags, const ImVec2& size_arg)\n{\n    if (Selectable(label, *p_selected, flags, size_arg))\n    {\n        *p_selected = !*p_selected;\n        return true;\n    }\n    return false;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: ListBox\n//-------------------------------------------------------------------------\n// - ListBox()\n// - ListBoxHeader()\n// - ListBoxFooter()\n//-------------------------------------------------------------------------\n// FIXME: This is an old API. We should redesign some of it, rename ListBoxHeader->BeginListBox, ListBoxFooter->EndListBox\n// and promote using them over existing ListBox() functions, similarly to change with combo boxes.\n//-------------------------------------------------------------------------\n\n// FIXME: In principle this function should be called BeginListBox(). We should rename it after re-evaluating if we want to keep the same signature.\n// Helper to calculate the size of a listbox and display a label on the right.\n// Tip: To have a list filling the entire window width, PushItemWidth(-1) and pass an non-visible label e.g. \"##empty\"\nbool ImGui::ListBoxHeader(const char* label, const ImVec2& size_arg)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    const ImGuiStyle& style = GetStyle();\n    const ImGuiID id = GetID(label);\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n\n    // Size default to hold ~7 items. Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar.\n    ImVec2 size = CalcItemSize(size_arg, GetNextItemWidth(), GetTextLineHeightWithSpacing() * 7.4f + style.ItemSpacing.y);\n    ImVec2 frame_size = ImVec2(size.x, ImMax(size.y, label_size.y));\n    ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size);\n    ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));\n    window->DC.LastItemRect = bb; // Forward storage for ListBoxFooter.. dodgy.\n\n    if (!IsRectVisible(bb.Min, bb.Max))\n    {\n        ItemSize(bb.GetSize(), style.FramePadding.y);\n        ItemAdd(bb, 0, &frame_bb);\n        return false;\n    }\n\n    BeginGroup();\n    if (label_size.x > 0)\n        RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);\n\n    BeginChildFrame(id, frame_bb.GetSize());\n    return true;\n}\n\n// FIXME: In principle this function should be called EndListBox(). We should rename it after re-evaluating if we want to keep the same signature.\nbool ImGui::ListBoxHeader(const char* label, int items_count, int height_in_items)\n{\n    // Size default to hold ~7.25 items.\n    // We add +25% worth of item height to allow the user to see at a glance if there are more items up/down, without looking at the scrollbar.\n    // We don't add this extra bit if items_count <= height_in_items. It is slightly dodgy, because it means a dynamic list of items will make the widget resize occasionally when it crosses that size.\n    // I am expecting that someone will come and complain about this behavior in a remote future, then we can advise on a better solution.\n    if (height_in_items < 0)\n        height_in_items = ImMin(items_count, 7);\n    const ImGuiStyle& style = GetStyle();\n    float height_in_items_f = (height_in_items < items_count) ? (height_in_items + 0.25f) : (height_in_items + 0.00f);\n\n    // We include ItemSpacing.y so that a list sized for the exact number of items doesn't make a scrollbar appears. We could also enforce that by passing a flag to BeginChild().\n    ImVec2 size;\n    size.x = 0.0f;\n    size.y = GetTextLineHeightWithSpacing() * height_in_items_f + style.FramePadding.y * 2.0f;\n    return ListBoxHeader(label, size);\n}\n\n// FIXME: In principle this function should be called EndListBox(). We should rename it after re-evaluating if we want to keep the same signature.\nvoid ImGui::ListBoxFooter()\n{\n    ImGuiWindow* parent_window = GetCurrentWindow()->ParentWindow;\n    const ImRect bb = parent_window->DC.LastItemRect;\n    const ImGuiStyle& style = GetStyle();\n\n    EndChildFrame();\n\n    // Redeclare item size so that it includes the label (we have stored the full size in LastItemRect)\n    // We call SameLine() to restore DC.CurrentLine* data\n    SameLine();\n    parent_window->DC.CursorPos = bb.Min;\n    ItemSize(bb, style.FramePadding.y);\n    EndGroup();\n}\n\nbool ImGui::ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_items)\n{\n    const bool value_changed = ListBox(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_items);\n    return value_changed;\n}\n\nbool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items)\n{\n    if (!ListBoxHeader(label, items_count, height_in_items))\n        return false;\n\n    // Assume all items have even height (= 1 line of text). If you need items of different or variable sizes you can create a custom version of ListBox() in your code without using the clipper.\n    ImGuiContext& g = *GImGui;\n    bool value_changed = false;\n    ImGuiListClipper clipper(items_count, GetTextLineHeightWithSpacing()); // We know exactly our line height here so we pass it as a minor optimization, but generally you don't need to.\n    while (clipper.Step())\n        for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)\n        {\n            const bool item_selected = (i == *current_item);\n            const char* item_text;\n            if (!items_getter(data, i, &item_text))\n                item_text = \"*Unknown item*\";\n\n            PushID(i);\n            if (Selectable(item_text, item_selected))\n            {\n                *current_item = i;\n                value_changed = true;\n            }\n            if (item_selected)\n                SetItemDefaultFocus();\n            PopID();\n        }\n    ListBoxFooter();\n    if (value_changed)\n        MarkItemEdited(g.CurrentWindow->DC.LastItemId);\n\n    return value_changed;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: PlotLines, PlotHistogram\n//-------------------------------------------------------------------------\n// - PlotEx() [Internal]\n// - PlotLines()\n// - PlotHistogram()\n//-------------------------------------------------------------------------\n\nvoid ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = window->GetID(label);\n\n    const ImVec2 label_size = CalcTextSize(label, NULL, true);\n    if (frame_size.x == 0.0f)\n        frame_size.x = GetNextItemWidth();\n    if (frame_size.y == 0.0f)\n        frame_size.y = label_size.y + (style.FramePadding.y * 2);\n\n    const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size);\n    const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);\n    const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0));\n    ItemSize(total_bb, style.FramePadding.y);\n    if (!ItemAdd(total_bb, 0, &frame_bb))\n        return;\n    const bool hovered = ItemHoverable(frame_bb, id);\n\n    // Determine scale from values if not specified\n    if (scale_min == FLT_MAX || scale_max == FLT_MAX)\n    {\n        float v_min = FLT_MAX;\n        float v_max = -FLT_MAX;\n        for (int i = 0; i < values_count; i++)\n        {\n            const float v = values_getter(data, i);\n            if (v != v) // Ignore NaN values\n                continue;\n            v_min = ImMin(v_min, v);\n            v_max = ImMax(v_max, v);\n        }\n        if (scale_min == FLT_MAX)\n            scale_min = v_min;\n        if (scale_max == FLT_MAX)\n            scale_max = v_max;\n    }\n\n    RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding);\n\n    const int values_count_min = (plot_type == ImGuiPlotType_Lines) ? 2 : 1;\n    if (values_count >= values_count_min)\n    {\n        int res_w = ImMin((int)frame_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0);\n        int item_count = values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0);\n\n        // Tooltip on hover\n        int v_hovered = -1;\n        if (hovered && inner_bb.Contains(g.IO.MousePos))\n        {\n            const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f);\n            const int v_idx = (int)(t * item_count);\n            IM_ASSERT(v_idx >= 0 && v_idx < values_count);\n\n            const float v0 = values_getter(data, (v_idx + values_offset) % values_count);\n            const float v1 = values_getter(data, (v_idx + 1 + values_offset) % values_count);\n            if (plot_type == ImGuiPlotType_Lines)\n                SetTooltip(\"%d: %8.4g\\n%d: %8.4g\", v_idx, v0, v_idx+1, v1);\n            else if (plot_type == ImGuiPlotType_Histogram)\n                SetTooltip(\"%d: %8.4g\", v_idx, v0);\n            v_hovered = v_idx;\n        }\n\n        const float t_step = 1.0f / (float)res_w;\n        const float inv_scale = (scale_min == scale_max) ? 0.0f : (1.0f / (scale_max - scale_min));\n\n        float v0 = values_getter(data, (0 + values_offset) % values_count);\n        float t0 = 0.0f;\n        ImVec2 tp0 = ImVec2( t0, 1.0f - ImSaturate((v0 - scale_min) * inv_scale) );                       // Point in the normalized space of our target rectangle\n        float histogram_zero_line_t = (scale_min * scale_max < 0.0f) ? (-scale_min * inv_scale) : (scale_min < 0.0f ? 0.0f : 1.0f);   // Where does the zero line stands\n\n        const ImU32 col_base = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLines : ImGuiCol_PlotHistogram);\n        const ImU32 col_hovered = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLinesHovered : ImGuiCol_PlotHistogramHovered);\n\n        for (int n = 0; n < res_w; n++)\n        {\n            const float t1 = t0 + t_step;\n            const int v1_idx = (int)(t0 * item_count + 0.5f);\n            IM_ASSERT(v1_idx >= 0 && v1_idx < values_count);\n            const float v1 = values_getter(data, (v1_idx + values_offset + 1) % values_count);\n            const ImVec2 tp1 = ImVec2( t1, 1.0f - ImSaturate((v1 - scale_min) * inv_scale) );\n\n            // NB: Draw calls are merged together by the DrawList system. Still, we should render our batch are lower level to save a bit of CPU.\n            ImVec2 pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0);\n            ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, (plot_type == ImGuiPlotType_Lines) ? tp1 : ImVec2(tp1.x, histogram_zero_line_t));\n            if (plot_type == ImGuiPlotType_Lines)\n            {\n                window->DrawList->AddLine(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base);\n            }\n            else if (plot_type == ImGuiPlotType_Histogram)\n            {\n                if (pos1.x >= pos0.x + 2.0f)\n                    pos1.x -= 1.0f;\n                window->DrawList->AddRectFilled(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base);\n            }\n\n            t0 = t1;\n            tp0 = tp1;\n        }\n    }\n\n    // Text overlay\n    if (overlay_text)\n        RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, overlay_text, NULL, NULL, ImVec2(0.5f,0.0f));\n\n    if (label_size.x > 0.0f)\n        RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label);\n}\n\nstruct ImGuiPlotArrayGetterData\n{\n    const float* Values;\n    int Stride;\n\n    ImGuiPlotArrayGetterData(const float* values, int stride) { Values = values; Stride = stride; }\n};\n\nstatic float Plot_ArrayGetter(void* data, int idx)\n{\n    ImGuiPlotArrayGetterData* plot_data = (ImGuiPlotArrayGetterData*)data;\n    const float v = *(const float*)(const void*)((const unsigned char*)plot_data->Values + (size_t)idx * plot_data->Stride);\n    return v;\n}\n\nvoid ImGui::PlotLines(const char* label, const float* values, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size, int stride)\n{\n    ImGuiPlotArrayGetterData data(values, stride);\n    PlotEx(ImGuiPlotType_Lines, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);\n}\n\nvoid ImGui::PlotLines(const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size)\n{\n    PlotEx(ImGuiPlotType_Lines, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);\n}\n\nvoid ImGui::PlotHistogram(const char* label, const float* values, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size, int stride)\n{\n    ImGuiPlotArrayGetterData data(values, stride);\n    PlotEx(ImGuiPlotType_Histogram, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);\n}\n\nvoid ImGui::PlotHistogram(const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size)\n{\n    PlotEx(ImGuiPlotType_Histogram, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size);\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: Value helpers\n// Those is not very useful, legacy API.\n//-------------------------------------------------------------------------\n// - Value()\n//-------------------------------------------------------------------------\n\nvoid ImGui::Value(const char* prefix, bool b)\n{\n    Text(\"%s: %s\", prefix, (b ? \"true\" : \"false\"));\n}\n\nvoid ImGui::Value(const char* prefix, int v)\n{\n    Text(\"%s: %d\", prefix, v);\n}\n\nvoid ImGui::Value(const char* prefix, unsigned int v)\n{\n    Text(\"%s: %d\", prefix, v);\n}\n\nvoid ImGui::Value(const char* prefix, float v, const char* float_format)\n{\n    if (float_format)\n    {\n        char fmt[64];\n        ImFormatString(fmt, IM_ARRAYSIZE(fmt), \"%%s: %s\", float_format);\n        Text(fmt, prefix, v);\n    }\n    else\n    {\n        Text(\"%s: %.3f\", prefix, v);\n    }\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] MenuItem, BeginMenu, EndMenu, etc.\n//-------------------------------------------------------------------------\n// - ImGuiMenuColumns [Internal]\n// - BeginMainMenuBar()\n// - EndMainMenuBar()\n// - BeginMenuBar()\n// - EndMenuBar()\n// - BeginMenu()\n// - EndMenu()\n// - MenuItem()\n//-------------------------------------------------------------------------\n\n// Helpers for internal use\nImGuiMenuColumns::ImGuiMenuColumns()\n{\n    Spacing = Width = NextWidth = 0.0f;\n    memset(Pos, 0, sizeof(Pos));\n    memset(NextWidths, 0, sizeof(NextWidths));\n}\n\nvoid ImGuiMenuColumns::Update(int count, float spacing, bool clear)\n{\n    IM_ASSERT(count == IM_ARRAYSIZE(Pos));\n    IM_UNUSED(count);\n    Width = NextWidth = 0.0f;\n    Spacing = spacing;\n    if (clear)\n        memset(NextWidths, 0, sizeof(NextWidths));\n    for (int i = 0; i < IM_ARRAYSIZE(Pos); i++)\n    {\n        if (i > 0 && NextWidths[i] > 0.0f)\n            Width += Spacing;\n        Pos[i] = (float)(int)Width;\n        Width += NextWidths[i];\n        NextWidths[i] = 0.0f;\n    }\n}\n\nfloat ImGuiMenuColumns::DeclColumns(float w0, float w1, float w2) // not using va_arg because they promote float to double\n{\n    NextWidth = 0.0f;\n    NextWidths[0] = ImMax(NextWidths[0], w0);\n    NextWidths[1] = ImMax(NextWidths[1], w1);\n    NextWidths[2] = ImMax(NextWidths[2], w2);\n    for (int i = 0; i < IM_ARRAYSIZE(Pos); i++)\n        NextWidth += NextWidths[i] + ((i > 0 && NextWidths[i] > 0.0f) ? Spacing : 0.0f);\n    return ImMax(Width, NextWidth);\n}\n\nfloat ImGuiMenuColumns::CalcExtraSpace(float avail_w)\n{\n    return ImMax(0.0f, avail_w - Width);\n}\n\n// For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set.\nbool ImGui::BeginMainMenuBar()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiViewport* viewport = g.Viewports[0];\n    g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f));\n    SetNextWindowPos(viewport->Pos);\n    SetNextWindowSize(ImVec2(viewport->Size.x, g.NextWindowData.MenuBarOffsetMinVal.y + g.FontBaseSize + g.Style.FramePadding.y));\n    SetNextWindowViewport(viewport->ID); // Enforce viewport so we don't create our onw viewport when ImGuiConfigFlags_ViewportsNoMerge is set.\n    PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);\n    PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0,0));\n    ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar;\n    bool is_open = Begin(\"##MainMenuBar\", NULL, window_flags) && BeginMenuBar();\n    PopStyleVar(2);\n    g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f);\n    if (!is_open)\n    {\n        End();\n        return false;\n    }\n    return true; //-V1020\n}\n\nvoid ImGui::EndMainMenuBar()\n{\n    EndMenuBar();\n\n    // When the user has left the menu layer (typically: closed menus through activation of an item), we restore focus to the previous window\n    // FIXME: With this strategy we won't be able to restore a NULL focus.\n    ImGuiContext& g = *GImGui;\n    if (g.CurrentWindow == g.NavWindow && g.NavLayer == 0)\n        FocusTopMostWindowUnderOne(g.NavWindow, NULL);\n\n    End();\n}\n\nbool ImGui::BeginMenuBar()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n    if (!(window->Flags & ImGuiWindowFlags_MenuBar))\n        return false;\n\n    IM_ASSERT(!window->DC.MenuBarAppending);\n    BeginGroup(); // Backup position on layer 0\n    PushID(\"##menubar\");\n\n    // We don't clip with current window clipping rectangle as it is already set to the area below. However we clip with window full rect.\n    // We remove 1 worth of rounding to Max.x to that text in long menus and small windows don't tend to display over the lower-right rounded area, which looks particularly glitchy.\n    ImRect bar_rect = window->MenuBarRect();\n    ImRect clip_rect(ImFloor(bar_rect.Min.x + window->WindowBorderSize + 0.5f), ImFloor(bar_rect.Min.y + window->WindowBorderSize + 0.5f), ImFloor(ImMax(bar_rect.Min.x, bar_rect.Max.x - ImMax(window->WindowRounding, window->WindowBorderSize)) + 0.5f), ImFloor(bar_rect.Max.y + 0.5f));\n    clip_rect.ClipWith(window->OuterRectClipped);\n    PushClipRect(clip_rect.Min, clip_rect.Max, false);\n\n    window->DC.CursorPos = ImVec2(bar_rect.Min.x + window->DC.MenuBarOffset.x, bar_rect.Min.y + window->DC.MenuBarOffset.y);\n    window->DC.LayoutType = ImGuiLayoutType_Horizontal;\n    window->DC.NavLayerCurrent = ImGuiNavLayer_Menu;\n    window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu);\n    window->DC.MenuBarAppending = true;\n    AlignTextToFramePadding();\n    return true;\n}\n\nvoid ImGui::EndMenuBar()\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return;\n    ImGuiContext& g = *GImGui;\n\n    // Nav: When a move request within one of our child menu failed, capture the request to navigate among our siblings.\n    if (NavMoveRequestButNoResultYet() && (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) && (g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu))\n    {\n        ImGuiWindow* nav_earliest_child = g.NavWindow;\n        while (nav_earliest_child->ParentWindow && (nav_earliest_child->ParentWindow->Flags & ImGuiWindowFlags_ChildMenu))\n            nav_earliest_child = nav_earliest_child->ParentWindow;\n        if (nav_earliest_child->ParentWindow == window && nav_earliest_child->DC.ParentLayoutType == ImGuiLayoutType_Horizontal && g.NavMoveRequestForward == ImGuiNavForward_None)\n        {\n            // To do so we claim focus back, restore NavId and then process the movement request for yet another frame.\n            // This involve a one-frame delay which isn't very problematic in this situation. We could remove it by scoring in advance for multiple window (probably not worth the hassle/cost)\n            const ImGuiNavLayer layer = ImGuiNavLayer_Menu;\n            IM_ASSERT(window->DC.NavLayerActiveMaskNext & (1 << layer)); // Sanity check\n            FocusWindow(window);\n            SetNavIDWithRectRel(window->NavLastIds[layer], layer, window->NavRectRel[layer]);\n            g.NavLayer = layer;\n            g.NavDisableHighlight = true; // Hide highlight for the current frame so we don't see the intermediary selection.\n            g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued;\n            NavMoveRequestCancel();\n        }\n    }\n\n    IM_ASSERT(window->Flags & ImGuiWindowFlags_MenuBar);\n    IM_ASSERT(window->DC.MenuBarAppending);\n    PopClipRect();\n    PopID();\n    window->DC.MenuBarOffset.x = window->DC.CursorPos.x - window->MenuBarRect().Min.x; // Save horizontal position so next append can reuse it. This is kinda equivalent to a per-layer CursorPos.\n    window->DC.GroupStack.back().AdvanceCursor = false;\n    EndGroup(); // Restore position on layer 0\n    window->DC.LayoutType = ImGuiLayoutType_Vertical;\n    window->DC.NavLayerCurrent = ImGuiNavLayer_Main;\n    window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main);\n    window->DC.MenuBarAppending = false;\n}\n\nbool ImGui::BeginMenu(const char* label, bool enabled)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = window->GetID(label);\n\n    ImVec2 label_size = CalcTextSize(label, NULL, true);\n\n    bool pressed;\n    bool menu_is_open = IsPopupOpen(id);\n    bool menuset_is_open = !(window->Flags & ImGuiWindowFlags_Popup) && (g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].OpenParentId == window->IDStack.back());\n    ImGuiWindow* backed_nav_window = g.NavWindow;\n    if (menuset_is_open)\n        g.NavWindow = window;  // Odd hack to allow hovering across menus of a same menu-set (otherwise we wouldn't be able to hover parent)\n\n    // The reference position stored in popup_pos will be used by Begin() to find a suitable position for the child menu,\n    // However the final position is going to be different! It is choosen by FindBestWindowPosForPopup().\n    // e.g. Menus tend to overlap each other horizontally to amplify relative Z-ordering.\n    ImVec2 popup_pos, pos = window->DC.CursorPos;\n    if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)\n    {\n        // Menu inside an horizontal menu bar\n        // Selectable extend their highlight by half ItemSpacing in each direction.\n        // For ChildMenu, the popup position will be overwritten by the call to FindBestWindowPosForPopup() in Begin()\n        popup_pos = ImVec2(pos.x - 1.0f - (float)(int)(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight());\n        window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f);\n        PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y));\n        float w = label_size.x;\n        pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_PressedOnClick | ImGuiSelectableFlags_DontClosePopups | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f));\n        PopStyleVar();\n        window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar().\n    }\n    else\n    {\n        // Menu inside a menu\n        popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y);\n        float w = window->MenuColumns.DeclColumns(label_size.x, 0.0f, (float)(int)(g.FontSize * 1.20f)); // Feedback to next frame\n        float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w);\n        pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_PressedOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_DrawFillAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f));\n        if (!enabled) PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);\n        RenderArrow(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.30f, 0.0f), ImGuiDir_Right);\n        if (!enabled) PopStyleColor();\n    }\n\n    const bool hovered = enabled && ItemHoverable(window->DC.LastItemRect, id);\n    if (menuset_is_open)\n        g.NavWindow = backed_nav_window;\n\n    bool want_open = false;\n    bool want_close = false;\n    if (window->DC.LayoutType == ImGuiLayoutType_Vertical) // (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu))\n    {\n        // Close menu when not hovering it anymore unless we are moving roughly in the direction of the menu\n        // Implement http://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown to avoid using timers, so menus feels more reactive.\n        bool moving_toward_other_child_menu = false;\n\n        ImGuiWindow* child_menu_window = (g.BeginPopupStack.Size < g.OpenPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].SourceWindow == window) ? g.OpenPopupStack[g.BeginPopupStack.Size].Window : NULL;\n        if (g.HoveredWindow == window && child_menu_window != NULL && !(window->Flags & ImGuiWindowFlags_MenuBar))\n        {\n            // FIXME-DPI: Values should be derived from a master \"scale\" factor.\n            ImRect next_window_rect = child_menu_window->Rect();\n            ImVec2 ta = g.IO.MousePos - g.IO.MouseDelta;\n            ImVec2 tb = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetTL() : next_window_rect.GetTR();\n            ImVec2 tc = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetBL() : next_window_rect.GetBR();\n            float extra = ImClamp(ImFabs(ta.x - tb.x) * 0.30f, 5.0f, 30.0f);    // add a bit of extra slack.\n            ta.x += (window->Pos.x < child_menu_window->Pos.x) ? -0.5f : +0.5f; // to avoid numerical issues\n            tb.y = ta.y + ImMax((tb.y - extra) - ta.y, -100.0f);                // triangle is maximum 200 high to limit the slope and the bias toward large sub-menus // FIXME: Multiply by fb_scale?\n            tc.y = ta.y + ImMin((tc.y + extra) - ta.y, +100.0f);\n            moving_toward_other_child_menu = ImTriangleContainsPoint(ta, tb, tc, g.IO.MousePos);\n            //GetForegroundDrawList()->AddTriangleFilled(ta, tb, tc, moving_within_opened_triangle ? IM_COL32(0,128,0,128) : IM_COL32(128,0,0,128)); // [DEBUG]\n        }\n        if (menu_is_open && !hovered && g.HoveredWindow == window && g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrame != id && !moving_toward_other_child_menu)\n            want_close = true;\n\n        if (!menu_is_open && hovered && pressed) // Click to open\n            want_open = true;\n        else if (!menu_is_open && hovered && !moving_toward_other_child_menu) // Hover to open\n            want_open = true;\n\n        if (g.NavActivateId == id)\n        {\n            want_close = menu_is_open;\n            want_open = !menu_is_open;\n        }\n        if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Right) // Nav-Right to open\n        {\n            want_open = true;\n            NavMoveRequestCancel();\n        }\n    }\n    else\n    {\n        // Menu bar\n        if (menu_is_open && pressed && menuset_is_open) // Click an open menu again to close it\n        {\n            want_close = true;\n            want_open = menu_is_open = false;\n        }\n        else if (pressed || (hovered && menuset_is_open && !menu_is_open)) // First click to open, then hover to open others\n        {\n            want_open = true;\n        }\n        else if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Down) // Nav-Down to open\n        {\n            want_open = true;\n            NavMoveRequestCancel();\n        }\n    }\n\n    if (!enabled) // explicitly close if an open menu becomes disabled, facilitate users code a lot in pattern such as 'if (BeginMenu(\"options\", has_object)) { ..use object.. }'\n        want_close = true;\n    if (want_close && IsPopupOpen(id))\n        ClosePopupToLevel(g.BeginPopupStack.Size, true);\n\n    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Openable | (menu_is_open ? ImGuiItemStatusFlags_Opened : 0));\n\n    if (!menu_is_open && want_open && g.OpenPopupStack.Size > g.BeginPopupStack.Size)\n    {\n        // Don't recycle same menu level in the same frame, first close the other menu and yield for a frame.\n        OpenPopup(label);\n        return false;\n    }\n\n    menu_is_open |= want_open;\n    if (want_open)\n        OpenPopup(label);\n\n    if (menu_is_open)\n    {\n        // Sub-menus are ChildWindow so that mouse can be hovering across them (otherwise top-most popup menu would steal focus and not allow hovering on parent menu)\n        SetNextWindowPos(popup_pos, ImGuiCond_Always);\n        ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus;\n        if (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu))\n            flags |= ImGuiWindowFlags_ChildWindow;\n        menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display)\n    }\n\n    return menu_is_open;\n}\n\nvoid ImGui::EndMenu()\n{\n    // Nav: When a left move request _within our child menu_ failed, close ourselves (the _parent_ menu).\n    // A menu doesn't close itself because EndMenuBar() wants the catch the last Left<>Right inputs.\n    // However, it means that with the current code, a BeginMenu() from outside another menu or a menu-bar won't be closable with the Left direction.\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (g.NavWindow && g.NavWindow->ParentWindow == window && g.NavMoveDir == ImGuiDir_Left && NavMoveRequestButNoResultYet() && window->DC.LayoutType == ImGuiLayoutType_Vertical)\n    {\n        ClosePopupToLevel(g.BeginPopupStack.Size, true);\n        NavMoveRequestCancel();\n    }\n\n    EndPopup();\n}\n\nbool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, bool enabled)\n{\n    ImGuiWindow* window = GetCurrentWindow();\n    if (window->SkipItems)\n        return false;\n\n    ImGuiContext& g = *GImGui;\n    ImGuiStyle& style = g.Style;\n    ImVec2 pos = window->DC.CursorPos;\n    ImVec2 label_size = CalcTextSize(label, NULL, true);\n\n    ImGuiSelectableFlags flags = ImGuiSelectableFlags_PressedOnRelease | (enabled ? 0 : ImGuiSelectableFlags_Disabled);\n    bool pressed;\n    if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)\n    {\n        // Mimic the exact layout spacing of BeginMenu() to allow MenuItem() inside a menu bar, which is a little misleading but may be useful\n        // Note that in this situation we render neither the shortcut neither the selected tick mark\n        float w = label_size.x;\n        window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f);\n        PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y));\n        pressed = Selectable(label, false, flags, ImVec2(w, 0.0f));\n        PopStyleVar();\n        window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar().\n    }\n    else\n    {\n        ImVec2 shortcut_size = shortcut ? CalcTextSize(shortcut, NULL) : ImVec2(0.0f, 0.0f);\n        float w = window->MenuColumns.DeclColumns(label_size.x, shortcut_size.x, (float)(int)(g.FontSize * 1.20f)); // Feedback for next frame\n        float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w);\n        pressed = Selectable(label, false, flags | ImGuiSelectableFlags_DrawFillAvailWidth, ImVec2(w, 0.0f));\n        if (shortcut_size.x > 0.0f)\n        {\n            PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);\n            RenderText(pos + ImVec2(window->MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false);\n            PopStyleColor();\n        }\n        if (selected)\n            RenderCheckMark(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize  * 0.866f);\n    }\n\n    IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0));\n    return pressed;\n}\n\nbool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled)\n{\n    if (MenuItem(label, shortcut, p_selected ? *p_selected : false, enabled))\n    {\n        if (p_selected)\n            *p_selected = !*p_selected;\n        return true;\n    }\n    return false;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: BeginTabBar, EndTabBar, etc.\n//-------------------------------------------------------------------------\n// [BETA API] API may evolve!\n//-------------------------------------------------------------------------\n// - BeginTabBar()\n// - BeginTabBarEx() [Internal]\n// - EndTabBar()\n// - TabBarLayout() [Internal]\n// - TabBarCalcTabID() [Internal]\n// - TabBarCalcMaxTabWidth() [Internal]\n// - TabBarFindTabById() [Internal]\n// - TabBarAddTab() [Internal]\n// - TabBarRemoveTab() [Internal]\n// - TabBarCloseTab() [Internal]\n// - TabBarScrollClamp()v\n// - TabBarScrollToTab() [Internal]\n// - TabBarQueueChangeTabOrder() [Internal]\n// - TabBarScrollingButtons() [Internal]\n// - TabBarTabListPopupButton() [Internal]\n//-------------------------------------------------------------------------\n\nnamespace ImGui\n{\n    static void             TabBarLayout(ImGuiTabBar* tab_bar);\n    static ImU32            TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label);\n    static float            TabBarCalcMaxTabWidth();\n    static float            TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling);\n    static void             TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab);\n    static ImGuiTabItem*    TabBarScrollingButtons(ImGuiTabBar* tab_bar);\n    static ImGuiTabItem*    TabBarTabListPopupButton(ImGuiTabBar* tab_bar);\n}\n\nImGuiTabBar::ImGuiTabBar()\n{\n    ID = 0;\n    SelectedTabId = NextSelectedTabId = VisibleTabId = 0;\n    CurrFrameVisible = PrevFrameVisible = -1;\n    ContentsHeight = 0.0f;\n    OffsetMax = OffsetNextTab = 0.0f;\n    ScrollingAnim = ScrollingTarget = ScrollingTargetDistToVisibility = ScrollingSpeed = 0.0f;\n    Flags = ImGuiTabBarFlags_None;\n    ReorderRequestTabId = 0;\n    ReorderRequestDir = 0;\n    WantLayout = VisibleTabWasSubmitted = false;\n    LastTabItemIdx = -1;\n}\n\nstatic int IMGUI_CDECL TabItemComparerByVisibleOffset(const void* lhs, const void* rhs)\n{\n    const ImGuiTabItem* a = (const ImGuiTabItem*)lhs;\n    const ImGuiTabItem* b = (const ImGuiTabItem*)rhs;\n    return (int)(a->Offset - b->Offset);\n}\n\nstatic int IMGUI_CDECL TabBarSortItemComparer(const void* lhs, const void* rhs)\n{\n    const ImGuiTabBarSortItem* a = (const ImGuiTabBarSortItem*)lhs;\n    const ImGuiTabBarSortItem* b = (const ImGuiTabBarSortItem*)rhs;\n    if (int d = (int)(b->Width - a->Width))\n        return d;\n    return (b->Index - a->Index);\n}\n\nstatic ImGuiTabBar* GetTabBarFromTabBarRef(const ImGuiTabBarRef& ref)\n{\n    ImGuiContext& g = *GImGui;\n    return ref.Ptr ? ref.Ptr : g.TabBars.GetByIndex(ref.IndexInMainPool);\n}\n\nstatic ImGuiTabBarRef GetTabBarRefFromTabBar(ImGuiTabBar* tab_bar)\n{\n    ImGuiContext& g = *GImGui;\n    if (g.TabBars.Contains(tab_bar))\n        return ImGuiTabBarRef(g.TabBars.GetIndex(tab_bar));\n    return ImGuiTabBarRef(tab_bar);\n}\n\nbool    ImGui::BeginTabBar(const char* str_id, ImGuiTabBarFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (window->SkipItems)\n        return false;\n\n    ImGuiID id = window->GetID(str_id);\n    ImGuiTabBar* tab_bar = g.TabBars.GetOrAddByKey(id);\n    ImRect tab_bar_bb = ImRect(window->DC.CursorPos.x, window->DC.CursorPos.y, window->InnerClipRect.Max.x, window->DC.CursorPos.y + g.FontSize + g.Style.FramePadding.y * 2);\n    tab_bar->ID = id;\n    return BeginTabBarEx(tab_bar, tab_bar_bb, flags | ImGuiTabBarFlags_IsFocused, NULL);\n}\n\nbool    ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImGuiTabBarFlags flags, ImGuiDockNode* dock_node)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (window->SkipItems)\n        return false;\n\n    if ((flags & ImGuiTabBarFlags_DockNode) == 0)\n        PushOverrideID(tab_bar->ID);\n\n    // Add to stack\n    g.CurrentTabBarStack.push_back(GetTabBarRefFromTabBar(tab_bar));\n    g.CurrentTabBar = tab_bar;\n\n    if (tab_bar->CurrFrameVisible == g.FrameCount)\n    {\n        //IMGUI_DEBUG_LOG(\"BeginTabBarEx already called this frame\\n\", g.FrameCount);\n        //IM_ASSERT(0);\n        return true;\n    }\n\n    // When toggling back from ordered to manually-reorderable, shuffle tabs to enforce the last visible order.\n    // Otherwise, the most recently inserted tabs would move at the end of visible list which can be a little too confusing or magic for the user.\n    if ((flags & ImGuiTabBarFlags_Reorderable) && !(tab_bar->Flags & ImGuiTabBarFlags_Reorderable) && tab_bar->Tabs.Size > 1 && tab_bar->PrevFrameVisible != -1)\n        ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerByVisibleOffset);\n\n    // Flags\n    if ((flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0)\n        flags |= ImGuiTabBarFlags_FittingPolicyDefault_;\n\n    tab_bar->Flags = flags;\n    tab_bar->BarRect = tab_bar_bb;\n    tab_bar->WantLayout = true; // Layout will be done on the first call to ItemTab()\n    tab_bar->PrevFrameVisible = tab_bar->CurrFrameVisible;\n    tab_bar->CurrFrameVisible = g.FrameCount;\n    tab_bar->FramePadding = g.Style.FramePadding;\n\n    // Layout\n    ItemSize(ImVec2(tab_bar->OffsetMax, tab_bar->BarRect.GetHeight()));\n    window->DC.CursorPos.x = tab_bar->BarRect.Min.x;\n\n    // Draw separator\n    const ImU32 col = GetColorU32((flags & ImGuiTabBarFlags_IsFocused) ? ImGuiCol_TabActive : ImGuiCol_Tab);\n    const float y = tab_bar->BarRect.Max.y - 1.0f;\n    if (dock_node != NULL)\n    {\n        const float separator_min_x = dock_node->Pos.x + window->WindowBorderSize;\n        const float separator_max_x = dock_node->Pos.x + dock_node->Size.x - window->WindowBorderSize;\n        window->DrawList->AddLine(ImVec2(separator_min_x, y), ImVec2(separator_max_x, y), col, 1.0f);\n    }\n    else\n    {\n        const float separator_min_x = tab_bar->BarRect.Min.x - window->WindowPadding.x;\n        const float separator_max_x = tab_bar->BarRect.Max.x + window->WindowPadding.x;\n        window->DrawList->AddLine(ImVec2(separator_min_x, y), ImVec2(separator_max_x, y), col, 1.0f);\n    }\n    return true;\n}\n\nvoid    ImGui::EndTabBar()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (window->SkipItems)\n        return;\n\n    ImGuiTabBar* tab_bar = g.CurrentTabBar;\n    if (tab_bar == NULL)\n    {\n        IM_ASSERT(tab_bar != NULL && \"Mismatched BeginTabBar()/EndTabBar()!\");\n        return; // FIXME-ERRORHANDLING\n    }\n    if (tab_bar->WantLayout)\n        TabBarLayout(tab_bar);\n\n    // Restore the last visible height if no tab is visible, this reduce vertical flicker/movement when a tabs gets removed without calling SetTabItemClosed().\n    const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount);\n    if (tab_bar->VisibleTabWasSubmitted || tab_bar->VisibleTabId == 0 || tab_bar_appearing)\n        tab_bar->ContentsHeight = ImMax(window->DC.CursorPos.y - tab_bar->BarRect.Max.y, 0.0f);\n    else\n        window->DC.CursorPos.y = tab_bar->BarRect.Max.y + tab_bar->ContentsHeight;\n\n    if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0)\n        PopID();\n\n    g.CurrentTabBarStack.pop_back();\n    g.CurrentTabBar = g.CurrentTabBarStack.empty() ? NULL : GetTabBarFromTabBarRef(g.CurrentTabBarStack.back());\n}\n\n// This is called only once a frame before by the first call to ItemTab()\n// The reason we're not calling it in BeginTabBar() is to leave a chance to the user to call the SetTabItemClosed() functions.\nstatic void ImGui::TabBarLayout(ImGuiTabBar* tab_bar)\n{\n    ImGuiContext& g = *GImGui;\n    tab_bar->WantLayout = false;\n\n    // Garbage collect\n    int tab_dst_n = 0;\n    for (int tab_src_n = 0; tab_src_n < tab_bar->Tabs.Size; tab_src_n++)\n    {\n        ImGuiTabItem* tab = &tab_bar->Tabs[tab_src_n];\n        if (tab->LastFrameVisible < tab_bar->PrevFrameVisible)\n        {\n            if (tab->ID == tab_bar->SelectedTabId)\n                tab_bar->SelectedTabId = 0;\n            continue;\n        }\n        if (tab_dst_n != tab_src_n)\n            tab_bar->Tabs[tab_dst_n] = tab_bar->Tabs[tab_src_n];\n        tab_dst_n++;\n    }\n    if (tab_bar->Tabs.Size != tab_dst_n)\n        tab_bar->Tabs.resize(tab_dst_n);\n\n    // Setup next selected tab\n    ImGuiID scroll_track_selected_tab_id = 0;\n    if (tab_bar->NextSelectedTabId)\n    {\n        tab_bar->SelectedTabId = tab_bar->NextSelectedTabId;\n        tab_bar->NextSelectedTabId = 0;\n        scroll_track_selected_tab_id = tab_bar->SelectedTabId;\n    }\n\n    // Process order change request (we could probably process it when requested but it's just saner to do it in a single spot).\n    if (tab_bar->ReorderRequestTabId != 0)\n    {\n        if (ImGuiTabItem* tab1 = TabBarFindTabByID(tab_bar, tab_bar->ReorderRequestTabId))\n        {\n            //IM_ASSERT(tab_bar->Flags & ImGuiTabBarFlags_Reorderable); // <- this may happen when using debug tools\n            int tab2_order = tab_bar->GetTabOrder(tab1) + tab_bar->ReorderRequestDir;\n            if (tab2_order >= 0 && tab2_order < tab_bar->Tabs.Size)\n            {\n                ImGuiTabItem* tab2 = &tab_bar->Tabs[tab2_order];\n                ImGuiTabItem item_tmp = *tab1;\n                *tab1 = *tab2;\n                *tab2 = item_tmp;\n                if (tab2->ID == tab_bar->SelectedTabId)\n                    scroll_track_selected_tab_id = tab2->ID;\n                tab1 = tab2 = NULL;\n            }\n            if (tab_bar->Flags & ImGuiTabBarFlags_SaveSettings)\n                MarkIniSettingsDirty();\n        }\n        tab_bar->ReorderRequestTabId = 0;\n    }\n\n    // Tab List Popup (will alter tab_bar->BarRect and therefore the available width!)\n    const bool tab_list_popup_button = (tab_bar->Flags & ImGuiTabBarFlags_TabListPopupButton) != 0;\n    if (tab_list_popup_button)\n        if (ImGuiTabItem* tab_to_select = TabBarTabListPopupButton(tab_bar)) // NB: Will alter BarRect.Max.x!\n            scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID;\n\n    ImVector<ImGuiTabBarSortItem>& width_sort_buffer = g.TabSortByWidthBuffer;\n    width_sort_buffer.resize(tab_bar->Tabs.Size);\n\n    // Compute ideal widths\n    float width_total_contents = 0.0f;\n    ImGuiTabItem* most_recently_selected_tab = NULL;\n    bool found_selected_tab_id = false;\n    for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++)\n    {\n        ImGuiTabItem* tab = &tab_bar->Tabs[tab_n];\n        IM_ASSERT(tab->LastFrameVisible >= tab_bar->PrevFrameVisible);\n\n        if (most_recently_selected_tab == NULL || most_recently_selected_tab->LastFrameSelected < tab->LastFrameSelected)\n            most_recently_selected_tab = tab;\n        if (tab->ID == tab_bar->SelectedTabId)\n            found_selected_tab_id = true;\n\n        // Refresh tab width immediately, otherwise changes of style e.g. style.FramePadding.x would noticeably lag in the tab bar.\n        // Additionally, when using TabBarAddTab() to manipulate tab bar order we occasionally insert new tabs that don't have a width yet,\n        // and we cannot wait for the next BeginTabItem() call. We cannot compute this width within TabBarAddTab() because font size depends on the active window.\n        const char* tab_name = tab_bar->GetTabName(tab);\n        const bool has_close_button = tab->Window ? tab->Window->HasCloseButton : ((tab->Flags & ImGuiTabItemFlags_NoCloseButton) == 0);\n        tab->WidthContents = TabItemCalcSize(tab_name, has_close_button).x;\n\n        width_total_contents += (tab_n > 0 ? g.Style.ItemInnerSpacing.x : 0.0f) + tab->WidthContents;\n\n        // Store data so we can build an array sorted by width if we need to shrink tabs down\n        width_sort_buffer[tab_n].Index = tab_n;\n        width_sort_buffer[tab_n].Width = tab->WidthContents;\n    }\n\n    // Compute width\n    const float width_avail = tab_bar->BarRect.GetWidth();\n    float width_excess = (width_avail < width_total_contents) ? (width_total_contents - width_avail) : 0.0f;\n    if (width_excess > 0.0f && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyResizeDown))\n    {\n        // If we don't have enough room, resize down the largest tabs first\n        if (tab_bar->Tabs.Size > 1)\n            ImQsort(width_sort_buffer.Data, (size_t)width_sort_buffer.Size, sizeof(ImGuiTabBarSortItem), TabBarSortItemComparer);\n        int tab_count_same_width = 1;\n        while (width_excess > 0.0f && tab_count_same_width < tab_bar->Tabs.Size)\n        {\n            while (tab_count_same_width < tab_bar->Tabs.Size && width_sort_buffer[0].Width == width_sort_buffer[tab_count_same_width].Width)\n                tab_count_same_width++;\n            float width_to_remove_per_tab_max = (tab_count_same_width < tab_bar->Tabs.Size) ? (width_sort_buffer[0].Width - width_sort_buffer[tab_count_same_width].Width) : (width_sort_buffer[0].Width - 1.0f);\n            float width_to_remove_per_tab = ImMin(width_excess / tab_count_same_width, width_to_remove_per_tab_max);\n            for (int tab_n = 0; tab_n < tab_count_same_width; tab_n++)\n                width_sort_buffer[tab_n].Width -= width_to_remove_per_tab;\n            width_excess -= width_to_remove_per_tab * tab_count_same_width;\n        }\n        for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++)\n            tab_bar->Tabs[width_sort_buffer[tab_n].Index].Width = (float)(int)width_sort_buffer[tab_n].Width;\n    }\n    else\n    {\n        const float tab_max_width = TabBarCalcMaxTabWidth();\n        for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++)\n        {\n            ImGuiTabItem* tab = &tab_bar->Tabs[tab_n];\n            tab->Width = ImMin(tab->WidthContents, tab_max_width);\n        }\n    }\n\n    // Layout all active tabs\n    float offset_x = 0.0f;\n    for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++)\n    {\n        ImGuiTabItem* tab = &tab_bar->Tabs[tab_n];\n        tab->Offset = offset_x;\n        if (scroll_track_selected_tab_id == 0 && g.NavJustMovedToId == tab->ID)\n            scroll_track_selected_tab_id = tab->ID;\n        offset_x += tab->Width + g.Style.ItemInnerSpacing.x;\n    }\n    tab_bar->OffsetMax = ImMax(offset_x - g.Style.ItemInnerSpacing.x, 0.0f);\n    tab_bar->OffsetNextTab = 0.0f;\n\n    // Horizontal scrolling buttons\n    const bool scrolling_buttons = (tab_bar->OffsetMax > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll);\n    if (scrolling_buttons)\n        if (ImGuiTabItem* tab_to_select = TabBarScrollingButtons(tab_bar)) // NB: Will alter BarRect.Max.x!\n            scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID;\n\n    // If we have lost the selected tab, select the next most recently active one\n    if (found_selected_tab_id == false)\n        tab_bar->SelectedTabId = 0;\n    if (tab_bar->SelectedTabId == 0 && tab_bar->NextSelectedTabId == 0 && most_recently_selected_tab != NULL)\n        scroll_track_selected_tab_id = tab_bar->SelectedTabId = most_recently_selected_tab->ID;\n\n    // Lock in visible tab\n    tab_bar->VisibleTabId = tab_bar->SelectedTabId;\n    tab_bar->VisibleTabWasSubmitted = false;\n\n    // CTRL+TAB can override visible tab temporarily\n    if (g.NavWindowingTarget != NULL && g.NavWindowingTarget->DockNode && g.NavWindowingTarget->DockNode->TabBar == tab_bar)\n        tab_bar->VisibleTabId = scroll_track_selected_tab_id = g.NavWindowingTarget->ID;\n\n    // Update scrolling\n    if (scroll_track_selected_tab_id)\n        if (ImGuiTabItem* scroll_track_selected_tab = TabBarFindTabByID(tab_bar, scroll_track_selected_tab_id))\n            TabBarScrollToTab(tab_bar, scroll_track_selected_tab);\n    tab_bar->ScrollingAnim = TabBarScrollClamp(tab_bar, tab_bar->ScrollingAnim);\n    tab_bar->ScrollingTarget = TabBarScrollClamp(tab_bar, tab_bar->ScrollingTarget);\n    if (tab_bar->ScrollingAnim != tab_bar->ScrollingTarget)\n    {\n        // Scrolling speed adjust itself so we can always reach our target in 1/3 seconds.\n        // Teleport if we are aiming far off the visible line\n        tab_bar->ScrollingSpeed = ImMax(tab_bar->ScrollingSpeed, 70.0f * g.FontSize);\n        tab_bar->ScrollingSpeed = ImMax(tab_bar->ScrollingSpeed, ImFabs(tab_bar->ScrollingTarget - tab_bar->ScrollingAnim) / 0.3f);\n        const bool teleport = (tab_bar->PrevFrameVisible + 1 < g.FrameCount) || (tab_bar->ScrollingTargetDistToVisibility > 10.0f * g.FontSize);\n        tab_bar->ScrollingAnim = teleport ? tab_bar->ScrollingTarget : ImLinearSweep(tab_bar->ScrollingAnim, tab_bar->ScrollingTarget, g.IO.DeltaTime * tab_bar->ScrollingSpeed);\n    }\n    else\n    {\n        tab_bar->ScrollingSpeed = 0.0f;\n    }\n\n    // Clear name buffers\n    if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0)\n        tab_bar->TabsNames.Buf.resize(0);\n}\n\n// Dockables uses Name/ID in the global namespace. Non-dockable items use the ID stack.\nstatic ImU32   ImGui::TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label)\n{\n    if (tab_bar->Flags & ImGuiTabBarFlags_DockNode)\n    {\n        ImGuiID id = ImHashStr(label);\n        KeepAliveID(id);\n        return id;\n    }\n    else\n    {\n        ImGuiWindow* window = GImGui->CurrentWindow;\n        return window->GetID(label);\n    }\n}\n\nstatic float ImGui::TabBarCalcMaxTabWidth()\n{\n    ImGuiContext& g = *GImGui;\n    return g.FontSize * 20.0f;\n}\n\nImGuiTabItem* ImGui::TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id)\n{\n    if (tab_id != 0)\n        for (int n = 0; n < tab_bar->Tabs.Size; n++)\n            if (tab_bar->Tabs[n].ID == tab_id)\n                return &tab_bar->Tabs[n];\n    return NULL;\n}\n\n// FIXME: See references to #2304 in TODO.txt\nImGuiTabItem* ImGui::TabBarFindMostRecentlySelectedTabForActiveWindow(ImGuiTabBar* tab_bar)\n{\n    ImGuiTabItem* most_recently_selected_tab = NULL;\n    for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++)\n    {\n        ImGuiTabItem* tab = &tab_bar->Tabs[tab_n];\n        if (most_recently_selected_tab == NULL || most_recently_selected_tab->LastFrameSelected < tab->LastFrameSelected)\n            if (tab->Window && tab->Window->WasActive)\n                most_recently_selected_tab = tab;\n    }\n    return most_recently_selected_tab;\n}\n\n// The purpose of this call is to register tab in advance so we can control their order at the time they appear. \n// Otherwise calling this is unnecessary as tabs are appending as needed by the BeginTabItem() function.\nvoid ImGui::TabBarAddTab(ImGuiTabBar* tab_bar, ImGuiTabItemFlags tab_flags, ImGuiWindow* window)\n{\n    ImGuiContext& g = *GImGui;\n    IM_ASSERT(TabBarFindTabByID(tab_bar, window->ID) == NULL);\n    IM_ASSERT(g.CurrentTabBar == NULL);                     // Can't work while the tab bar is active as our tab doesn't have an X offset yet\n\n    ImGuiTabItem new_tab;\n    new_tab.ID = window->ID;\n    new_tab.Flags = tab_flags;\n    new_tab.LastFrameVisible = tab_bar->CurrFrameVisible;   // Required so BeginTabBar() doesn't ditch the tab\n    if (new_tab.LastFrameVisible == -1)\n        new_tab.LastFrameVisible = g.FrameCount - 1;\n    new_tab.Window = window;                                // Required so tab bar layout can compute the tab width before tab submission\n    tab_bar->Tabs.push_back(new_tab);\n}\n\n// The *TabId fields be already set by the docking system _before_ the actual TabItem was created, so we clear them regardless.\nvoid ImGui::TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id)\n{\n    if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_id))\n        tab_bar->Tabs.erase(tab);\n    if (tab_bar->VisibleTabId == tab_id)      { tab_bar->VisibleTabId = 0; }\n    if (tab_bar->SelectedTabId == tab_id)     { tab_bar->SelectedTabId = 0; }\n    if (tab_bar->NextSelectedTabId == tab_id) { tab_bar->NextSelectedTabId = 0; }\n}\n\n// Called on manual closure attempt\nvoid ImGui::TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab)\n{\n    if ((tab_bar->VisibleTabId == tab->ID) && !(tab->Flags & ImGuiTabItemFlags_UnsavedDocument))\n    {\n        // This will remove a frame of lag for selecting another tab on closure.\n        // However we don't run it in the case where the 'Unsaved' flag is set, so user gets a chance to fully undo the closure\n        tab->LastFrameVisible = -1;\n        tab_bar->SelectedTabId = tab_bar->NextSelectedTabId = 0;\n    }\n    else if ((tab_bar->VisibleTabId != tab->ID) && (tab->Flags & ImGuiTabItemFlags_UnsavedDocument))\n    {\n        // Actually select before expecting closure\n        tab_bar->NextSelectedTabId = tab->ID;\n    }\n}\n\nstatic float ImGui::TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling)\n{\n    scrolling = ImMin(scrolling, tab_bar->OffsetMax - tab_bar->BarRect.GetWidth());\n    return ImMax(scrolling, 0.0f);\n}\n\nstatic void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab)\n{\n    ImGuiContext& g = *GImGui;\n    float margin = g.FontSize * 1.0f; // When to scroll to make Tab N+1 visible always make a bit of N visible to suggest more scrolling area (since we don't have a scrollbar)\n    int order = tab_bar->GetTabOrder(tab);\n    float tab_x1 = tab->Offset + (order > 0 ? -margin : 0.0f);\n    float tab_x2 = tab->Offset + tab->Width + (order + 1 < tab_bar->Tabs.Size ? margin : 1.0f);\n    tab_bar->ScrollingTargetDistToVisibility = 0.0f;\n    if (tab_bar->ScrollingTarget > tab_x1)\n    {\n        tab_bar->ScrollingTargetDistToVisibility = ImMax(tab_bar->ScrollingAnim - tab_x2, 0.0f);\n        tab_bar->ScrollingTarget = tab_x1;\n    }\n    else if (tab_bar->ScrollingTarget < tab_x2 - tab_bar->BarRect.GetWidth())\n    {\n        tab_bar->ScrollingTargetDistToVisibility = ImMax((tab_x1 - tab_bar->BarRect.GetWidth()) - tab_bar->ScrollingAnim, 0.0f);\n        tab_bar->ScrollingTarget = tab_x2 - tab_bar->BarRect.GetWidth();\n    }\n}\n\nvoid ImGui::TabBarQueueChangeTabOrder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir)\n{\n    IM_ASSERT(dir == -1 || dir == +1);\n    IM_ASSERT(tab_bar->ReorderRequestTabId == 0);\n    tab_bar->ReorderRequestTabId = tab->ID;\n    tab_bar->ReorderRequestDir = dir;\n}\n\nstatic ImGuiTabItem* ImGui::TabBarScrollingButtons(ImGuiTabBar* tab_bar)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    const ImVec2 arrow_button_size(g.FontSize - 2.0f, g.FontSize + g.Style.FramePadding.y * 2.0f);\n    const float scrolling_buttons_width = arrow_button_size.x * 2.0f;\n\n    const ImVec2 backup_cursor_pos = window->DC.CursorPos;\n    //window->DrawList->AddRect(ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width, tab_bar->BarRect.Min.y), ImVec2(tab_bar->BarRect.Max.x, tab_bar->BarRect.Max.y), IM_COL32(255,0,0,255));\n\n    const ImRect avail_bar_rect = tab_bar->BarRect;\n    bool want_clip_rect = !avail_bar_rect.Contains(ImRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(scrolling_buttons_width, 0.0f)));\n    if (want_clip_rect)\n        PushClipRect(tab_bar->BarRect.Min, tab_bar->BarRect.Max + ImVec2(g.Style.ItemInnerSpacing.x, 0.0f), true);\n\n    ImGuiTabItem* tab_to_select = NULL;\n\n    int select_dir = 0;\n    ImVec4 arrow_col = g.Style.Colors[ImGuiCol_Text];\n    arrow_col.w *= 0.5f;\n\n    PushStyleColor(ImGuiCol_Text, arrow_col);\n    PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));\n    const float backup_repeat_delay = g.IO.KeyRepeatDelay;\n    const float backup_repeat_rate = g.IO.KeyRepeatRate;\n    g.IO.KeyRepeatDelay = 0.250f;\n    g.IO.KeyRepeatRate = 0.200f;\n    window->DC.CursorPos = ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width, tab_bar->BarRect.Min.y);\n    if (ArrowButtonEx(\"##<\", ImGuiDir_Left, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat))\n        select_dir = -1;\n    window->DC.CursorPos = ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width + arrow_button_size.x, tab_bar->BarRect.Min.y);\n    if (ArrowButtonEx(\"##>\", ImGuiDir_Right, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat))\n        select_dir = +1;\n    PopStyleColor(2);\n    g.IO.KeyRepeatRate = backup_repeat_rate;\n    g.IO.KeyRepeatDelay = backup_repeat_delay;\n\n    if (want_clip_rect)\n        PopClipRect();\n\n    if (select_dir != 0)\n        if (ImGuiTabItem* tab_item = TabBarFindTabByID(tab_bar, tab_bar->SelectedTabId))\n        {\n            int selected_order = tab_bar->GetTabOrder(tab_item);\n            int target_order = selected_order + select_dir;\n            tab_to_select = &tab_bar->Tabs[(target_order >= 0 && target_order < tab_bar->Tabs.Size) ? target_order : selected_order]; // If we are at the end of the list, still scroll to make our tab visible\n        }\n    window->DC.CursorPos = backup_cursor_pos;\n    tab_bar->BarRect.Max.x -= scrolling_buttons_width + 1.0f;\n\n    return tab_to_select;\n}\n\nstatic ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n\n    // We use g.Style.FramePadding.y to match the square ArrowButton size\n    const float tab_list_popup_button_width = g.FontSize + g.Style.FramePadding.y;\n    const ImVec2 backup_cursor_pos = window->DC.CursorPos;\n    window->DC.CursorPos = ImVec2(tab_bar->BarRect.Min.x - g.Style.FramePadding.y, tab_bar->BarRect.Min.y);\n    tab_bar->BarRect.Min.x += tab_list_popup_button_width;\n\n    ImVec4 arrow_col = g.Style.Colors[ImGuiCol_Text];\n    arrow_col.w *= 0.5f;\n    PushStyleColor(ImGuiCol_Text, arrow_col);\n    PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));\n    bool open = BeginCombo(\"##v\", NULL, ImGuiComboFlags_NoPreview | ImGuiComboFlags_PopupAlignLeft);\n    PopStyleColor(2);\n\n    ImGuiTabItem* tab_to_select = NULL;\n    if (open)\n    {\n        for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++)\n        {\n            ImGuiTabItem* tab = &tab_bar->Tabs[tab_n];\n            const char* tab_name = tab_bar->GetTabName(tab);\n            if (Selectable(tab_name, tab_bar->SelectedTabId == tab->ID))\n                tab_to_select = tab;\n        }\n        EndCombo();\n    }\n\n    window->DC.CursorPos = backup_cursor_pos;\n    return tab_to_select;\n}\n\n//-------------------------------------------------------------------------\n// [SECTION] Widgets: BeginTabItem, EndTabItem, etc.\n//-------------------------------------------------------------------------\n// [BETA API] API may evolve!\n//-------------------------------------------------------------------------\n// - BeginTabItem()\n// - EndTabItem()\n// - TabItemEx() [Internal]\n// - SetTabItemClosed()\n// - TabItemCalcSize() [Internal]\n// - TabItemBackground() [Internal]\n// - TabItemLabelAndCloseButton() [Internal]\n//-------------------------------------------------------------------------\n\nbool    ImGui::BeginTabItem(const char* label, bool* p_open, ImGuiTabItemFlags flags)\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (window->SkipItems)\n        return false;\n\n    ImGuiTabBar* tab_bar = g.CurrentTabBar;\n    if (tab_bar == NULL)\n    {\n        IM_ASSERT(tab_bar && \"Needs to be called between BeginTabBar() and EndTabBar()!\");\n        return false; // FIXME-ERRORHANDLING\n    }\n    bool ret = TabItemEx(tab_bar, label, p_open, flags, NULL);\n    if (ret && !(flags & ImGuiTabItemFlags_NoPushId))\n    {\n        ImGuiTabItem* tab = &tab_bar->Tabs[tab_bar->LastTabItemIdx];\n        PushOverrideID(tab->ID); // We already hashed 'label' so push into the ID stack directly instead of doing another hash through PushID(label)\n    }\n    return ret;\n}\n\nvoid    ImGui::EndTabItem()\n{\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (window->SkipItems)\n        return;\n\n    ImGuiTabBar* tab_bar = g.CurrentTabBar;\n    if (tab_bar == NULL)\n    {\n        IM_ASSERT(tab_bar != NULL && \"Needs to be called between BeginTabBar() and EndTabBar()!\");\n        return;\n    }\n    IM_ASSERT(tab_bar->LastTabItemIdx >= 0);\n    ImGuiTabItem* tab = &tab_bar->Tabs[tab_bar->LastTabItemIdx];\n    if (!(tab->Flags & ImGuiTabItemFlags_NoPushId))\n        window->IDStack.pop_back();\n}\n\nbool    ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags, ImGuiWindow* docked_window)\n{\n    // Layout whole tab bar if not already done\n    if (tab_bar->WantLayout)\n        TabBarLayout(tab_bar);\n\n    ImGuiContext& g = *GImGui;\n    ImGuiWindow* window = g.CurrentWindow;\n    if (window->SkipItems)\n        return false;\n\n    const ImGuiStyle& style = g.Style;\n    const ImGuiID id = TabBarCalcTabID(tab_bar, label);\n\n    // If the user called us with *p_open == false, we early out and don't render. We make a dummy call to ItemAdd() so that attempts to use a contextual popup menu with an implicit ID won't use an older ID.\n    if (p_open && !*p_open)\n    {\n        PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true);\n        ItemAdd(ImRect(), id);\n        PopItemFlag();\n        return false;\n    }\n\n    // Calculate tab contents size\n    ImVec2 size = TabItemCalcSize(label, p_open != NULL);\n\n    // Acquire tab data\n    ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, id);\n    bool tab_is_new = false;\n    if (tab == NULL)\n    {\n        tab_bar->Tabs.push_back(ImGuiTabItem());\n        tab = &tab_bar->Tabs.back();\n        tab->ID = id;\n        tab->Width = size.x;\n        tab_is_new = true;\n    }\n    tab_bar->LastTabItemIdx = (short)tab_bar->Tabs.index_from_ptr(tab);\n    tab->WidthContents = size.x;\n\n    if (p_open == NULL)\n        flags |= ImGuiTabItemFlags_NoCloseButton;\n\n    const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount);\n    const bool tab_bar_focused = (tab_bar->Flags & ImGuiTabBarFlags_IsFocused) != 0;\n    const bool tab_appearing = (tab->LastFrameVisible + 1 < g.FrameCount);\n    tab->LastFrameVisible = g.FrameCount;\n    tab->Flags = flags;\n    tab->Window = docked_window;\n\n    // Append name with zero-terminator\n    if (tab_bar->Flags & ImGuiTabBarFlags_DockNode)\n    {\n        IM_ASSERT(tab->Window != NULL);\n        tab->NameOffset = -1;\n    }\n    else\n    {\n        IM_ASSERT(tab->Window == NULL);\n        tab->NameOffset = tab_bar->TabsNames.size();\n        tab_bar->TabsNames.append(label, label + strlen(label) + 1); // Append name _with_ the zero-terminator.\n    }\n\n    // If we are not reorderable, always reset offset based on submission order.\n    // (We already handled layout and sizing using the previous known order, but sizing is not affected by order!)\n    if (!tab_appearing && !(tab_bar->Flags & ImGuiTabBarFlags_Reorderable))\n    {\n        tab->Offset = tab_bar->OffsetNextTab;\n        tab_bar->OffsetNextTab += tab->Width + g.Style.ItemInnerSpacing.x;\n    }\n\n    // Update selected tab\n    if (tab_appearing && (tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs) && tab_bar->NextSelectedTabId == 0)\n        if (!tab_bar_appearing || tab_bar->SelectedTabId == 0)\n            tab_bar->NextSelectedTabId = id;  // New tabs gets activated\n    if ((flags & ImGuiTabItemFlags_SetSelected) && (tab_bar->SelectedTabId != id)) // SetSelected can only be passed on explicit tab bar\n        tab_bar->NextSelectedTabId = id;\n\n    // Lock visibility\n    bool tab_contents_visible = (tab_bar->VisibleTabId == id);\n    if (tab_contents_visible)\n        tab_bar->VisibleTabWasSubmitted = true;\n\n    // On the very first frame of a tab bar we let first tab contents be visible to minimize appearing glitches\n    if (!tab_contents_visible && tab_bar->SelectedTabId == 0 && tab_bar_appearing && docked_window == NULL)\n        if (tab_bar->Tabs.Size == 1 && !(tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs))\n            tab_contents_visible = true;\n\n    if (tab_appearing && !(tab_bar_appearing && !tab_is_new))\n    {\n        PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true);\n        ItemAdd(ImRect(), id);\n        PopItemFlag();\n        return tab_contents_visible;\n    }\n\n    if (tab_bar->SelectedTabId == id)\n        tab->LastFrameSelected = g.FrameCount;\n\n    // Backup current layout position\n    const ImVec2 backup_main_cursor_pos = window->DC.CursorPos;\n\n    // Layout\n    size.x = tab->Width;\n    window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2((float)(int)tab->Offset - tab_bar->ScrollingAnim, 0.0f);\n    ImVec2 pos = window->DC.CursorPos;\n    ImRect bb(pos, pos + size);\n\n    // We don't have CPU clipping primitives to clip the CloseButton (until it becomes a texture), so need to add an extra draw call (temporary in the case of vertical animation)\n    bool want_clip_rect = (bb.Min.x < tab_bar->BarRect.Min.x) || (bb.Max.x >= tab_bar->BarRect.Max.x);\n    if (want_clip_rect)\n        PushClipRect(ImVec2(ImMax(bb.Min.x, tab_bar->BarRect.Min.x), bb.Min.y - 1), ImVec2(tab_bar->BarRect.Max.x, bb.Max.y), true);\n\n    ItemSize(bb, style.FramePadding.y);\n    if (!ItemAdd(bb, id))\n    {\n        if (want_clip_rect)\n            PopClipRect();\n        window->DC.CursorPos = backup_main_cursor_pos;\n        return tab_contents_visible;\n    }\n\n    // Click to Select a tab\n    ImGuiButtonFlags button_flags = (ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_AllowItemOverlap);\n    if (g.DragDropActive && !g.DragDropPayload.IsDataType(IMGUI_PAYLOAD_TYPE_WINDOW))\n        button_flags |= ImGuiButtonFlags_PressedOnDragDropHold;\n    bool hovered, held;\n    bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags);\n    if (pressed)\n        tab_bar->NextSelectedTabId = id;\n    hovered |= (g.HoveredId == id);\n\n    // Allow the close button to overlap unless we are dragging (in which case we don't want any overlapping tabs to be hovered)\n    if (!held)\n        SetItemAllowOverlap();\n\n    // Drag and drop a single floating window node moves it\n    // FIXME-DOCK: In theory we shouldn't test for the ConfigDockingNodifySingleWindows flag here.\n    // When our single window node and OnlyNodeWithWindows are working properly we may remove this check here.\n    ImGuiDockNode* node = docked_window ? docked_window->DockNode : NULL;\n    const bool single_floating_window_node = node && node->IsRootNode() && !node->IsDockSpace() && node->Windows.Size == 1 && g.IO.ConfigDockingTabBarOnSingleWindows;\n    if (held && single_floating_window_node && IsMouseDragging(0, 0.0f))\n    {\n        // Move\n        StartMouseMovingWindow(docked_window);\n    }\n    else if (held && !tab_appearing && IsMouseDragging(0))\n    {\n        // Drag and drop: re-order tabs\n        float drag_distance_from_edge_x = 0.0f;\n        if (!g.DragDropActive && ((tab_bar->Flags & ImGuiTabBarFlags_Reorderable) || (docked_window != NULL)))\n        {\n            // While moving a tab it will jump on the other side of the mouse, so we also test for MouseDelta.x\n            if (g.IO.MouseDelta.x < 0.0f && g.IO.MousePos.x < bb.Min.x)\n            {\n                drag_distance_from_edge_x = bb.Min.x - g.IO.MousePos.x;\n                if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable)\n                    TabBarQueueChangeTabOrder(tab_bar, tab, -1);\n            }\n            else if (g.IO.MouseDelta.x > 0.0f && g.IO.MousePos.x > bb.Max.x)\n            {\n                drag_distance_from_edge_x = g.IO.MousePos.x - bb.Max.x;\n                if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable)\n                    TabBarQueueChangeTabOrder(tab_bar, tab, +1);\n            }\n        }\n\n        // Extract a Dockable window out of it's tab bar\n        if (docked_window != NULL && !(docked_window->Flags & ImGuiWindowFlags_NoMove))\n        {\n            // We use a variable threshold to distinguish dragging tabs within a tab bar and extracting them out of the tab bar\n            bool undocking_tab = (g.DragDropActive && g.DragDropPayload.SourceId == id);\n\n            if (!undocking_tab) //&& (!g.IO.ConfigDockingWithShift || g.IO.KeyShift)\n            {\n                float threshold_base = g.FontSize;\n                //float threshold_base = g.IO.ConfigDockingWithShift ? g.FontSize * 0.5f : g.FontSize;\n                float threshold_x = (threshold_base * 2.2f);\n                float threshold_y = (threshold_base * 1.5f) + ImClamp((ImFabs(g.IO.MouseDragMaxDistanceAbs[0].x) - threshold_base * 2.0f) * 0.20f, 0.0f, threshold_base * 4.0f);\n                //GetOverlayDrawList(window)->AddRect(ImVec2(bb.Min.x - threshold_x, bb.Min.y - threshold_y), ImVec2(bb.Max.x + threshold_x, bb.Max.y + threshold_y), IM_COL32_WHITE); // [DEBUG]\n\n                float distance_from_edge_y = ImMax(bb.Min.y - g.IO.MousePos.y, g.IO.MousePos.y - bb.Max.y);\n                if (distance_from_edge_y >= threshold_y)\n                    undocking_tab = true;\n                else if (drag_distance_from_edge_x > threshold_x)\n                    if ((tab_bar->ReorderRequestDir < 0 && tab_bar->GetTabOrder(tab) == 0) || (tab_bar->ReorderRequestDir > 0 && tab_bar->GetTabOrder(tab) == tab_bar->Tabs.Size - 1))\n                        undocking_tab = true;\n            }\n\n            if (undocking_tab)\n            {\n                // Undock\n                DockContextQueueUndockWindow(&g, docked_window);\n                g.MovingWindow = docked_window;\n                g.ActiveId = g.MovingWindow->MoveId;\n                g.ActiveIdClickOffset -= g.MovingWindow->Pos - bb.Min;\n            }\n        }\n    }\n\n#if 0\n    if (hovered && g.HoveredIdNotActiveTimer > 0.50f && bb.GetWidth() < tab->WidthContents)\n    {\n        // Enlarge tab display when hovering\n        bb.Max.x = bb.Min.x + (float)(int)ImLerp(bb.GetWidth(), tab->WidthContents, ImSaturate((g.HoveredIdNotActiveTimer - 0.40f) * 6.0f));\n        display_draw_list = GetForegroundDrawList(window);\n        TabItemBackground(display_draw_list, bb, flags, GetColorU32(ImGuiCol_TitleBgActive));\n    }\n#endif\n\n    // Render tab shape\n    ImDrawList* display_draw_list = window->DrawList;\n    const ImU32 tab_col = GetColorU32((held || hovered) ? ImGuiCol_TabHovered : tab_contents_visible ? (tab_bar_focused ? ImGuiCol_TabActive : ImGuiCol_TabUnfocusedActive) : (tab_bar_focused ? ImGuiCol_Tab : ImGuiCol_TabUnfocused));\n    TabItemBackground(display_draw_list, bb, flags, tab_col);\n    RenderNavHighlight(bb, id);\n\n    // Select with right mouse button. This is so the common idiom for context menu automatically highlight the current widget.\n    const bool hovered_unblocked = IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup);\n    if (hovered_unblocked && (IsMouseClicked(1) || IsMouseReleased(1)))\n        tab_bar->NextSelectedTabId = id;\n\n    if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton)\n        flags |= ImGuiTabItemFlags_NoCloseWithMiddleMouseButton;\n\n    // Render tab label, process close button\n    const ImGuiID close_button_id = p_open ? window->GetID((void*)((intptr_t)id + 1)) : 0;\n    bool just_closed = TabItemLabelAndCloseButton(display_draw_list, bb, flags, tab_bar->FramePadding, label, id, close_button_id);\n    if (just_closed && p_open != NULL)\n    {\n        *p_open = false;\n        TabBarCloseTab(tab_bar, tab);\n    }\n\n    // Restore main window position so user can draw there\n    if (want_clip_rect)\n        PopClipRect();\n    window->DC.CursorPos = backup_main_cursor_pos;\n\n    // Tooltip (FIXME: Won't work over the close button because ItemOverlap systems messes up with HoveredIdTimer)\n    // We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar (which g.HoveredId ignores)\n    if (g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > 0.50f && IsItemHovered())\n        if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip))\n            SetTooltip(\"%.*s\", (int)(FindRenderedTextEnd(label) - label), label);\n\n    return tab_contents_visible;\n}\n\n// [Public] This is call is 100% optional but it allows to remove some one-frame glitches when a tab has been unexpectedly removed.\n// To use it to need to call the function SetTabItemClosed() after BeginTabBar() and before any call to BeginTabItem()\nvoid    ImGui::SetTabItemClosed(const char* label)\n{\n    ImGuiContext& g = *GImGui;\n    bool is_within_manual_tab_bar = g.CurrentTabBar && !(g.CurrentTabBar->Flags & ImGuiTabBarFlags_DockNode);\n    if (is_within_manual_tab_bar)\n    {\n        ImGuiTabBar* tab_bar = g.CurrentTabBar;\n        IM_ASSERT(tab_bar->WantLayout);         // Needs to be called AFTER BeginTabBar() and BEFORE the first call to BeginTabItem()\n        ImGuiID tab_id = TabBarCalcTabID(tab_bar, label);\n        TabBarRemoveTab(tab_bar, tab_id);\n    }\n    else if (ImGuiWindow* window = FindWindowByName(label))\n    {\n        if (window->DockIsActive)\n            if (ImGuiDockNode* node = window->DockNode)\n            {\n                ImGuiID tab_id = TabBarCalcTabID(node->TabBar, label);\n                TabBarRemoveTab(node->TabBar, tab_id);\n                window->DockTabWantClose = true;\n            }\n    }\n}\n\nImVec2 ImGui::TabItemCalcSize(const char* label, bool has_close_button)\n{\n    ImGuiContext& g = *GImGui;\n    ImVec2 label_size = CalcTextSize(label, NULL, true);\n    ImVec2 size = ImVec2(label_size.x + g.Style.FramePadding.x, label_size.y + g.Style.FramePadding.y * 2.0f);\n    if (has_close_button)\n        size.x += g.Style.FramePadding.x + (g.Style.ItemInnerSpacing.x + g.FontSize); // We use Y intentionally to fit the close button circle.\n    else\n        size.x += g.Style.FramePadding.x + 1.0f;\n    return ImVec2(ImMin(size.x, TabBarCalcMaxTabWidth()), size.y);\n}\n\nvoid ImGui::TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col)\n{\n    // While rendering tabs, we trim 1 pixel off the top of our bounding box so they can fit within a regular frame height while looking \"detached\" from it.\n    ImGuiContext& g = *GImGui;\n    const float width = bb.GetWidth();\n    IM_UNUSED(flags);\n    IM_ASSERT(width > 0.0f);\n    const float rounding = ImMax(0.0f, ImMin(g.Style.TabRounding, width * 0.5f - 1.0f));\n    const float y1 = bb.Min.y + 1.0f;\n    const float y2 = bb.Max.y + ((flags & ImGuiTabItemFlags_Preview) ? 0.0f : -1.0f);\n    draw_list->PathLineTo(ImVec2(bb.Min.x, y2));\n    draw_list->PathArcToFast(ImVec2(bb.Min.x + rounding, y1 + rounding), rounding, 6, 9);\n    draw_list->PathArcToFast(ImVec2(bb.Max.x - rounding, y1 + rounding), rounding, 9, 12);\n    draw_list->PathLineTo(ImVec2(bb.Max.x, y2));\n    draw_list->PathFillConvex(col);\n    if (g.Style.TabBorderSize > 0.0f)\n    {\n        draw_list->PathLineTo(ImVec2(bb.Min.x + 0.5f, y2));\n        draw_list->PathArcToFast(ImVec2(bb.Min.x + rounding + 0.5f, y1 + rounding + 0.5f), rounding, 6, 9);\n        draw_list->PathArcToFast(ImVec2(bb.Max.x - rounding - 0.5f, y1 + rounding + 0.5f), rounding, 9, 12);\n        draw_list->PathLineTo(ImVec2(bb.Max.x - 0.5f, y2));\n        draw_list->PathStroke(GetColorU32(ImGuiCol_Border), false, g.Style.TabBorderSize);\n    }\n}\n\n// Render text label (with custom clipping) + Unsaved Document marker + Close Button logic\n// We tend to lock style.FramePadding for a given tab-bar, hence the 'frame_padding' parameter.\nbool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id)\n{\n    ImGuiContext& g = *GImGui;\n    ImVec2 label_size = CalcTextSize(label, NULL, true);\n    if (bb.GetWidth() <= 1.0f)\n        return false;\n\n    // Render text label (with clipping + alpha gradient) + unsaved marker\n    const char* TAB_UNSAVED_MARKER = \"*\";\n    ImRect text_pixel_clip_bb(bb.Min.x + frame_padding.x, bb.Min.y + frame_padding.y, bb.Max.x - frame_padding.x, bb.Max.y);\n    if (flags & ImGuiTabItemFlags_UnsavedDocument)\n    {\n        text_pixel_clip_bb.Max.x -= CalcTextSize(TAB_UNSAVED_MARKER, NULL, false).x;\n        ImVec2 unsaved_marker_pos(ImMin(bb.Min.x + frame_padding.x + label_size.x + 2, text_pixel_clip_bb.Max.x), bb.Min.y + frame_padding.y + (float)(int)(-g.FontSize * 0.25f));\n        RenderTextClippedEx(draw_list, unsaved_marker_pos, bb.Max - frame_padding, TAB_UNSAVED_MARKER, NULL, NULL);\n    }\n    ImRect text_ellipsis_clip_bb = text_pixel_clip_bb;\n\n    // Close Button\n    // We are relying on a subtle and confusing distinction between 'hovered' and 'g.HoveredId' which happens because we are using ImGuiButtonFlags_AllowOverlapMode + SetItemAllowOverlap()\n    //  'hovered' will be true when hovering the Tab but NOT when hovering the close button\n    //  'g.HoveredId==id' will be true when hovering the Tab including when hovering the close button\n    //  'g.ActiveId==close_button_id' will be true when we are holding on the close button, in which case both hovered booleans are false\n    bool close_button_pressed = false;\n    bool close_button_visible = false;\n    if (close_button_id != 0)\n        if (g.HoveredId == tab_id || g.HoveredId == close_button_id || g.ActiveId == close_button_id)\n            close_button_visible = true;\n    if (close_button_visible)\n    {\n        ImGuiItemHoveredDataBackup last_item_backup;\n        const float close_button_sz = g.FontSize * 0.5f;\n        if (CloseButton(close_button_id, ImVec2(bb.Max.x - frame_padding.x - close_button_sz, bb.Min.y + frame_padding.y + close_button_sz), close_button_sz))\n            close_button_pressed = true;\n        last_item_backup.Restore();\n\n        // Close with middle mouse button\n        if (!(flags & ImGuiTabItemFlags_NoCloseWithMiddleMouseButton) && IsMouseClicked(2))\n            close_button_pressed = true;\n\n        text_pixel_clip_bb.Max.x -= close_button_sz * 2.0f;\n    }\n\n    // Label with ellipsis\n    // FIXME: This should be extracted into a helper but the use of text_pixel_clip_bb and !close_button_visible makes it tricky to abstract at the moment\n    const char* label_display_end = FindRenderedTextEnd(label);\n    if (label_size.x > text_ellipsis_clip_bb.GetWidth())\n    {\n        const int ellipsis_dot_count = 3;\n        const float ellipsis_width = (1.0f + 1.0f) * ellipsis_dot_count - 1.0f;\n        const char* label_end = NULL;\n        float label_size_clipped_x = g.Font->CalcTextSizeA(g.FontSize, text_ellipsis_clip_bb.GetWidth() - ellipsis_width + 1.0f, 0.0f, label, label_display_end, &label_end).x;\n        if (label_end == label && label_end < label_display_end)    // Always display at least 1 character if there's no room for character + ellipsis\n        {\n            label_end = label + ImTextCountUtf8BytesFromChar(label, label_display_end);\n            label_size_clipped_x = g.Font->CalcTextSizeA(g.FontSize, FLT_MAX, 0.0f, label, label_end).x;\n        }\n        while (label_end > label && ImCharIsBlankA(label_end[-1])) // Trim trailing space\n        {\n            label_end--;\n            label_size_clipped_x -= g.Font->CalcTextSizeA(g.FontSize, FLT_MAX, 0.0f, label_end, label_end + 1).x; // Ascii blanks are always 1 byte\n        }\n        RenderTextClippedEx(draw_list, text_pixel_clip_bb.Min, text_pixel_clip_bb.Max, label, label_end, &label_size, ImVec2(0.0f, 0.0f));\n\n        const float ellipsis_x = text_pixel_clip_bb.Min.x + label_size_clipped_x + 1.0f;\n        if (!close_button_visible && ellipsis_x + ellipsis_width <= bb.Max.x)\n            RenderPixelEllipsis(draw_list, ImVec2(ellipsis_x, text_pixel_clip_bb.Min.y), ellipsis_dot_count, GetColorU32(ImGuiCol_Text));\n    }\n    else\n    {\n        RenderTextClippedEx(draw_list, text_pixel_clip_bb.Min, text_pixel_clip_bb.Max, label, label_display_end, &label_size, ImVec2(0.0f, 0.0f));\n    }\n\n    return close_button_pressed;\n}\n"
  },
  {
    "path": "src/imgui/imstb_rectpack.hpp",
    "content": "// [DEAR IMGUI] \n// This is a slightly modified version of stb_rect_pack.h 0.99. \n// Those changes would need to be pushed into nothings/stb:\n// - Added STBRP__CDECL\n// Grep for [DEAR IMGUI] to find the changes.\n\n// stb_rect_pack.h - v0.99 - public domain - rectangle packing\n// Sean Barrett 2014\n//\n// Useful for e.g. packing rectangular textures into an atlas.\n// Does not do rotation.\n//\n// Not necessarily the awesomest packing method, but better than\n// the totally naive one in stb_truetype (which is primarily what\n// this is meant to replace).\n//\n// Has only had a few tests run, may have issues.\n//\n// More docs to come.\n//\n// No memory allocations; uses qsort() and assert() from stdlib.\n// Can override those by defining STBRP_SORT and STBRP_ASSERT.\n//\n// This library currently uses the Skyline Bottom-Left algorithm.\n//\n// Please note: better rectangle packers are welcome! Please\n// implement them to the same API, but with a different init\n// function.\n//\n// Credits\n//\n//  Library\n//    Sean Barrett\n//  Minor features\n//    Martins Mozeiko\n//    github:IntellectualKitty\n//    \n//  Bugfixes / warning fixes\n//    Jeremy Jaussaud\n//\n// Version history:\n//\n//     0.99  (2019-02-07)  warning fixes\n//     0.11  (2017-03-03)  return packing success/fail result\n//     0.10  (2016-10-25)  remove cast-away-const to avoid warnings\n//     0.09  (2016-08-27)  fix compiler warnings\n//     0.08  (2015-09-13)  really fix bug with empty rects (w=0 or h=0)\n//     0.07  (2015-09-13)  fix bug with empty rects (w=0 or h=0)\n//     0.06  (2015-04-15)  added STBRP_SORT to allow replacing qsort\n//     0.05:  added STBRP_ASSERT to allow replacing assert\n//     0.04:  fixed minor bug in STBRP_LARGE_RECTS support\n//     0.01:  initial release\n//\n// LICENSE\n//\n//   See end of file for license information.\n\n//////////////////////////////////////////////////////////////////////////////\n//\n//       INCLUDE SECTION\n//\n\n#ifndef STB_INCLUDE_STB_RECT_PACK_H\n#define STB_INCLUDE_STB_RECT_PACK_H\n\n#define STB_RECT_PACK_VERSION  1\n\n#ifdef STBRP_STATIC\n#define STBRP_DEF static\n#else\n#define STBRP_DEF extern\n#endif\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef struct stbrp_context stbrp_context;\ntypedef struct stbrp_node    stbrp_node;\ntypedef struct stbrp_rect    stbrp_rect;\n\n#ifdef STBRP_LARGE_RECTS\ntypedef int            stbrp_coord;\n#else\ntypedef unsigned short stbrp_coord;\n#endif\n\nSTBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);\n// Assign packed locations to rectangles. The rectangles are of type\n// 'stbrp_rect' defined below, stored in the array 'rects', and there\n// are 'num_rects' many of them.\n//\n// Rectangles which are successfully packed have the 'was_packed' flag\n// set to a non-zero value and 'x' and 'y' store the minimum location\n// on each axis (i.e. bottom-left in cartesian coordinates, top-left\n// if you imagine y increasing downwards). Rectangles which do not fit\n// have the 'was_packed' flag set to 0.\n//\n// You should not try to access the 'rects' array from another thread\n// while this function is running, as the function temporarily reorders\n// the array while it executes.\n//\n// To pack into another rectangle, you need to call stbrp_init_target\n// again. To continue packing into the same rectangle, you can call\n// this function again. Calling this multiple times with multiple rect\n// arrays will probably produce worse packing results than calling it\n// a single time with the full rectangle array, but the option is\n// available.\n//\n// The function returns 1 if all of the rectangles were successfully\n// packed and 0 otherwise.\n\nstruct stbrp_rect\n{\n   // reserved for your use:\n   int            id;\n\n   // input:\n   stbrp_coord    w, h;\n\n   // output:\n   stbrp_coord    x, y;\n   int            was_packed;  // non-zero if valid packing\n\n}; // 16 bytes, nominally\n\n\nSTBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);\n// Initialize a rectangle packer to:\n//    pack a rectangle that is 'width' by 'height' in dimensions\n//    using temporary storage provided by the array 'nodes', which is 'num_nodes' long\n//\n// You must call this function every time you start packing into a new target.\n//\n// There is no \"shutdown\" function. The 'nodes' memory must stay valid for\n// the following stbrp_pack_rects() call (or calls), but can be freed after\n// the call (or calls) finish.\n//\n// Note: to guarantee best results, either:\n//       1. make sure 'num_nodes' >= 'width'\n//   or  2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'\n//\n// If you don't do either of the above things, widths will be quantized to multiples\n// of small integers to guarantee the algorithm doesn't run out of temporary storage.\n//\n// If you do #2, then the non-quantized algorithm will be used, but the algorithm\n// may run out of temporary storage and be unable to pack some rectangles.\n\nSTBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);\n// Optionally call this function after init but before doing any packing to\n// change the handling of the out-of-temp-memory scenario, described above.\n// If you call init again, this will be reset to the default (false).\n\n\nSTBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);\n// Optionally select which packing heuristic the library should use. Different\n// heuristics will produce better/worse results for different data sets.\n// If you call init again, this will be reset to the default.\n\nenum\n{\n   STBRP_HEURISTIC_Skyline_default=0,\n   STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,\n   STBRP_HEURISTIC_Skyline_BF_sortHeight\n};\n\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// the details of the following structures don't matter to you, but they must\n// be visible so you can handle the memory allocations for them\n\nstruct stbrp_node\n{\n   stbrp_coord  x,y;\n   stbrp_node  *next;\n};\n\nstruct stbrp_context\n{\n   int width;\n   int height;\n   int align;\n   int init_mode;\n   int heuristic;\n   int num_nodes;\n   stbrp_node *active_head;\n   stbrp_node *free_head;\n   stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'\n};\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n\n//////////////////////////////////////////////////////////////////////////////\n//\n//     IMPLEMENTATION SECTION\n//\n\n#ifdef STB_RECT_PACK_IMPLEMENTATION\n#ifndef STBRP_SORT\n#include <stdlib.h>\n#define STBRP_SORT qsort\n#endif\n\n#ifndef STBRP_ASSERT\n#include <assert.h>\n#define STBRP_ASSERT assert\n#endif\n\n// [DEAR IMGUI] Added STBRP__CDECL\n#ifdef _MSC_VER\n#define STBRP__NOTUSED(v)  (void)(v)\n#define STBRP__CDECL __cdecl\n#else\n#define STBRP__NOTUSED(v)  (void)sizeof(v)\n#define STBRP__CDECL\n#endif\n\nenum\n{\n   STBRP__INIT_skyline = 1\n};\n\nSTBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)\n{\n   switch (context->init_mode) {\n      case STBRP__INIT_skyline:\n         STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);\n         context->heuristic = heuristic;\n         break;\n      default:\n         STBRP_ASSERT(0);\n   }\n}\n\nSTBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)\n{\n   if (allow_out_of_mem)\n      // if it's ok to run out of memory, then don't bother aligning them;\n      // this gives better packing, but may fail due to OOM (even though\n      // the rectangles easily fit). @TODO a smarter approach would be to only\n      // quantize once we've hit OOM, then we could get rid of this parameter.\n      context->align = 1;\n   else {\n      // if it's not ok to run out of memory, then quantize the widths\n      // so that num_nodes is always enough nodes.\n      //\n      // I.e. num_nodes * align >= width\n      //                  align >= width / num_nodes\n      //                  align = ceil(width/num_nodes)\n\n      context->align = (context->width + context->num_nodes-1) / context->num_nodes;\n   }\n}\n\nSTBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)\n{\n   int i;\n#ifndef STBRP_LARGE_RECTS\n   STBRP_ASSERT(width <= 0xffff && height <= 0xffff);\n#endif\n\n   for (i=0; i < num_nodes-1; ++i)\n      nodes[i].next = &nodes[i+1];\n   nodes[i].next = NULL;\n   context->init_mode = STBRP__INIT_skyline;\n   context->heuristic = STBRP_HEURISTIC_Skyline_default;\n   context->free_head = &nodes[0];\n   context->active_head = &context->extra[0];\n   context->width = width;\n   context->height = height;\n   context->num_nodes = num_nodes;\n   stbrp_setup_allow_out_of_mem(context, 0);\n\n   // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)\n   context->extra[0].x = 0;\n   context->extra[0].y = 0;\n   context->extra[0].next = &context->extra[1];\n   context->extra[1].x = (stbrp_coord) width;\n#ifdef STBRP_LARGE_RECTS\n   context->extra[1].y = (1<<30);\n#else\n   context->extra[1].y = 65535;\n#endif\n   context->extra[1].next = NULL;\n}\n\n// find minimum y position if it starts at x1\nstatic int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)\n{\n   stbrp_node *node = first;\n   int x1 = x0 + width;\n   int min_y, visited_width, waste_area;\n\n   STBRP__NOTUSED(c);\n\n   STBRP_ASSERT(first->x <= x0);\n\n   #if 0\n   // skip in case we're past the node\n   while (node->next->x <= x0)\n      ++node;\n   #else\n   STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency\n   #endif\n\n   STBRP_ASSERT(node->x <= x0);\n\n   min_y = 0;\n   waste_area = 0;\n   visited_width = 0;\n   while (node->x < x1) {\n      if (node->y > min_y) {\n         // raise min_y higher.\n         // we've accounted for all waste up to min_y,\n         // but we'll now add more waste for everything we've visted\n         waste_area += visited_width * (node->y - min_y);\n         min_y = node->y;\n         // the first time through, visited_width might be reduced\n         if (node->x < x0)\n            visited_width += node->next->x - x0;\n         else\n            visited_width += node->next->x - node->x;\n      } else {\n         // add waste area\n         int under_width = node->next->x - node->x;\n         if (under_width + visited_width > width)\n            under_width = width - visited_width;\n         waste_area += under_width * (min_y - node->y);\n         visited_width += under_width;\n      }\n      node = node->next;\n   }\n\n   *pwaste = waste_area;\n   return min_y;\n}\n\ntypedef struct\n{\n   int x,y;\n   stbrp_node **prev_link;\n} stbrp__findresult;\n\nstatic stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)\n{\n   int best_waste = (1<<30), best_x, best_y = (1 << 30);\n   stbrp__findresult fr;\n   stbrp_node **prev, *node, *tail, **best = NULL;\n\n   // align to multiple of c->align\n   width = (width + c->align - 1);\n   width -= width % c->align;\n   STBRP_ASSERT(width % c->align == 0);\n\n   node = c->active_head;\n   prev = &c->active_head;\n   while (node->x + width <= c->width) {\n      int y,waste;\n      y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);\n      if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL\n         // bottom left\n         if (y < best_y) {\n            best_y = y;\n            best = prev;\n         }\n      } else {\n         // best-fit\n         if (y + height <= c->height) {\n            // can only use it if it first vertically\n            if (y < best_y || (y == best_y && waste < best_waste)) {\n               best_y = y;\n               best_waste = waste;\n               best = prev;\n            }\n         }\n      }\n      prev = &node->next;\n      node = node->next;\n   }\n\n   best_x = (best == NULL) ? 0 : (*best)->x;\n\n   // if doing best-fit (BF), we also have to try aligning right edge to each node position\n   //\n   // e.g, if fitting\n   //\n   //     ____________________\n   //    |____________________|\n   //\n   //            into\n   //\n   //   |                         |\n   //   |             ____________|\n   //   |____________|\n   //\n   // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned\n   //\n   // This makes BF take about 2x the time\n\n   if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {\n      tail = c->active_head;\n      node = c->active_head;\n      prev = &c->active_head;\n      // find first node that's admissible\n      while (tail->x < width)\n         tail = tail->next;\n      while (tail) {\n         int xpos = tail->x - width;\n         int y,waste;\n         STBRP_ASSERT(xpos >= 0);\n         // find the left position that matches this\n         while (node->next->x <= xpos) {\n            prev = &node->next;\n            node = node->next;\n         }\n         STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);\n         y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);\n         if (y + height < c->height) {\n            if (y <= best_y) {\n               if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {\n                  best_x = xpos;\n                  STBRP_ASSERT(y <= best_y);\n                  best_y = y;\n                  best_waste = waste;\n                  best = prev;\n               }\n            }\n         }\n         tail = tail->next;\n      }         \n   }\n\n   fr.prev_link = best;\n   fr.x = best_x;\n   fr.y = best_y;\n   return fr;\n}\n\nstatic stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)\n{\n   // find best position according to heuristic\n   stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);\n   stbrp_node *node, *cur;\n\n   // bail if:\n   //    1. it failed\n   //    2. the best node doesn't fit (we don't always check this)\n   //    3. we're out of memory\n   if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {\n      res.prev_link = NULL;\n      return res;\n   }\n\n   // on success, create new node\n   node = context->free_head;\n   node->x = (stbrp_coord) res.x;\n   node->y = (stbrp_coord) (res.y + height);\n\n   context->free_head = node->next;\n\n   // insert the new node into the right starting point, and\n   // let 'cur' point to the remaining nodes needing to be\n   // stiched back in\n\n   cur = *res.prev_link;\n   if (cur->x < res.x) {\n      // preserve the existing one, so start testing with the next one\n      stbrp_node *next = cur->next;\n      cur->next = node;\n      cur = next;\n   } else {\n      *res.prev_link = node;\n   }\n\n   // from here, traverse cur and free the nodes, until we get to one\n   // that shouldn't be freed\n   while (cur->next && cur->next->x <= res.x + width) {\n      stbrp_node *next = cur->next;\n      // move the current node to the free list\n      cur->next = context->free_head;\n      context->free_head = cur;\n      cur = next;\n   }\n\n   // stitch the list back in\n   node->next = cur;\n\n   if (cur->x < res.x + width)\n      cur->x = (stbrp_coord) (res.x + width);\n\n#ifdef _DEBUG\n   cur = context->active_head;\n   while (cur->x < context->width) {\n      STBRP_ASSERT(cur->x < cur->next->x);\n      cur = cur->next;\n   }\n   STBRP_ASSERT(cur->next == NULL);\n\n   {\n      int count=0;\n      cur = context->active_head;\n      while (cur) {\n         cur = cur->next;\n         ++count;\n      }\n      cur = context->free_head;\n      while (cur) {\n         cur = cur->next;\n         ++count;\n      }\n      STBRP_ASSERT(count == context->num_nodes+2);\n   }\n#endif\n\n   return res;\n}\n\n// [DEAR IMGUI] Added STBRP__CDECL\nstatic int STBRP__CDECL rect_height_compare(const void *a, const void *b)\n{\n   const stbrp_rect *p = (const stbrp_rect *) a;\n   const stbrp_rect *q = (const stbrp_rect *) b;\n   if (p->h > q->h)\n      return -1;\n   if (p->h < q->h)\n      return  1;\n   return (p->w > q->w) ? -1 : (p->w < q->w);\n}\n\n// [DEAR IMGUI] Added STBRP__CDECL\nstatic int STBRP__CDECL rect_original_order(const void *a, const void *b)\n{\n   const stbrp_rect *p = (const stbrp_rect *) a;\n   const stbrp_rect *q = (const stbrp_rect *) b;\n   return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);\n}\n\n#ifdef STBRP_LARGE_RECTS\n#define STBRP__MAXVAL  0xffffffff\n#else\n#define STBRP__MAXVAL  0xffff\n#endif\n\nSTBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)\n{\n   int i, all_rects_packed = 1;\n\n   // we use the 'was_packed' field internally to allow sorting/unsorting\n   for (i=0; i < num_rects; ++i) {\n      rects[i].was_packed = i;\n   }\n\n   // sort according to heuristic\n   STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);\n\n   for (i=0; i < num_rects; ++i) {\n      if (rects[i].w == 0 || rects[i].h == 0) {\n         rects[i].x = rects[i].y = 0;  // empty rect needs no space\n      } else {\n         stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);\n         if (fr.prev_link) {\n            rects[i].x = (stbrp_coord) fr.x;\n            rects[i].y = (stbrp_coord) fr.y;\n         } else {\n            rects[i].x = rects[i].y = STBRP__MAXVAL;\n         }\n      }\n   }\n\n   // unsort\n   STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);\n\n   // set was_packed flags and all_rects_packed status\n   for (i=0; i < num_rects; ++i) {\n      rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);\n      if (!rects[i].was_packed)\n         all_rects_packed = 0;\n   }\n\n   // return the all_rects_packed status\n   return all_rects_packed;\n}\n#endif\n\n/*\n------------------------------------------------------------------------------\nThis software is available under 2 licenses -- choose whichever you prefer.\n------------------------------------------------------------------------------\nALTERNATIVE A - MIT License\nCopyright (c) 2017 Sean Barrett\nPermission is hereby granted, free of charge, to any person obtaining a copy of \nthis software and associated documentation files (the \"Software\"), to deal in \nthe Software without restriction, including without limitation the rights to \nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies \nof the Software, and to permit persons to whom the Software is furnished to do \nso, subject to the following conditions:\nThe above copyright notice and this permission notice shall be included in all \ncopies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR \nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, \nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE \nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER \nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, \nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE \nSOFTWARE.\n------------------------------------------------------------------------------\nALTERNATIVE B - Public Domain (www.unlicense.org)\nThis is free and unencumbered software released into the public domain.\nAnyone is free to copy, modify, publish, use, compile, sell, or distribute this \nsoftware, either in source code form or as a compiled binary, for any purpose, \ncommercial or non-commercial, and by any means.\nIn jurisdictions that recognize copyright laws, the author or authors of this \nsoftware dedicate any and all copyright interest in the software to the public \ndomain. We make this dedication for the benefit of the public at large and to \nthe detriment of our heirs and successors. We intend this dedication to be an \novert act of relinquishment in perpetuity of all present and future rights to \nthis software under copyright law.\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR \nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, \nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE \nAUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN \nACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION \nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n------------------------------------------------------------------------------\n*/\n"
  },
  {
    "path": "src/imgui/imstb_textedit.hpp",
    "content": "// [DEAR IMGUI] \n// This is a slightly modified version of stb_textedit.h 1.13. \n// Those changes would need to be pushed into nothings/stb:\n// - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321)\n// Grep for [DEAR IMGUI] to find the changes.\n\n// stb_textedit.h - v1.13  - public domain - Sean Barrett\n// Development of this library was sponsored by RAD Game Tools\n//\n// This C header file implements the guts of a multi-line text-editing\n// widget; you implement display, word-wrapping, and low-level string\n// insertion/deletion, and stb_textedit will map user inputs into\n// insertions & deletions, plus updates to the cursor position,\n// selection state, and undo state.\n//\n// It is intended for use in games and other systems that need to build\n// their own custom widgets and which do not have heavy text-editing\n// requirements (this library is not recommended for use for editing large\n// texts, as its performance does not scale and it has limited undo).\n//\n// Non-trivial behaviors are modelled after Windows text controls.\n// \n//\n// LICENSE\n//\n// See end of file for license information.\n//\n//\n// DEPENDENCIES\n//\n// Uses the C runtime function 'memmove', which you can override\n// by defining STB_TEXTEDIT_memmove before the implementation.\n// Uses no other functions. Performs no runtime allocations.\n//\n//\n// VERSION HISTORY\n//\n//   1.13 (2019-02-07) fix bug in undo size management\n//   1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash\n//   1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield\n//   1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual\n//   1.9  (2016-08-27) customizable move-by-word\n//   1.8  (2016-04-02) better keyboard handling when mouse button is down\n//   1.7  (2015-09-13) change y range handling in case baseline is non-0\n//   1.6  (2015-04-15) allow STB_TEXTEDIT_memmove\n//   1.5  (2014-09-10) add support for secondary keys for OS X\n//   1.4  (2014-08-17) fix signed/unsigned warnings\n//   1.3  (2014-06-19) fix mouse clicking to round to nearest char boundary\n//   1.2  (2014-05-27) fix some RAD types that had crept into the new code\n//   1.1  (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE )\n//   1.0  (2012-07-26) improve documentation, initial public release\n//   0.3  (2012-02-24) bugfixes, single-line mode; insert mode\n//   0.2  (2011-11-28) fixes to undo/redo\n//   0.1  (2010-07-08) initial version\n//\n// ADDITIONAL CONTRIBUTORS\n//\n//   Ulf Winklemann: move-by-word in 1.1\n//   Fabian Giesen: secondary key inputs in 1.5\n//   Martins Mozeiko: STB_TEXTEDIT_memmove in 1.6\n//\n//   Bugfixes:\n//      Scott Graham\n//      Daniel Keller\n//      Omar Cornut\n//      Dan Thompson\n//\n// USAGE\n//\n// This file behaves differently depending on what symbols you define\n// before including it.\n//\n//\n// Header-file mode:\n//\n//   If you do not define STB_TEXTEDIT_IMPLEMENTATION before including this,\n//   it will operate in \"header file\" mode. In this mode, it declares a\n//   single public symbol, STB_TexteditState, which encapsulates the current\n//   state of a text widget (except for the string, which you will store\n//   separately).\n//\n//   To compile in this mode, you must define STB_TEXTEDIT_CHARTYPE to a\n//   primitive type that defines a single character (e.g. char, wchar_t, etc).\n//\n//   To save space or increase undo-ability, you can optionally define the\n//   following things that are used by the undo system:\n//\n//      STB_TEXTEDIT_POSITIONTYPE         small int type encoding a valid cursor position\n//      STB_TEXTEDIT_UNDOSTATECOUNT       the number of undo states to allow\n//      STB_TEXTEDIT_UNDOCHARCOUNT        the number of characters to store in the undo buffer\n//\n//   If you don't define these, they are set to permissive types and\n//   moderate sizes. The undo system does no memory allocations, so\n//   it grows STB_TexteditState by the worst-case storage which is (in bytes):\n//\n//        [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT\n//      +          sizeof(STB_TEXTEDIT_CHARTYPE)      * STB_TEXTEDIT_UNDOCHAR_COUNT\n//\n//\n// Implementation mode:\n//\n//   If you define STB_TEXTEDIT_IMPLEMENTATION before including this, it\n//   will compile the implementation of the text edit widget, depending\n//   on a large number of symbols which must be defined before the include.\n//\n//   The implementation is defined only as static functions. You will then\n//   need to provide your own APIs in the same file which will access the\n//   static functions.\n//\n//   The basic concept is that you provide a \"string\" object which\n//   behaves like an array of characters. stb_textedit uses indices to\n//   refer to positions in the string, implicitly representing positions\n//   in the displayed textedit. This is true for both plain text and\n//   rich text; even with rich text stb_truetype interacts with your\n//   code as if there was an array of all the displayed characters.\n//\n// Symbols that must be the same in header-file and implementation mode:\n//\n//     STB_TEXTEDIT_CHARTYPE             the character type\n//     STB_TEXTEDIT_POSITIONTYPE         small type that is a valid cursor position\n//     STB_TEXTEDIT_UNDOSTATECOUNT       the number of undo states to allow\n//     STB_TEXTEDIT_UNDOCHARCOUNT        the number of characters to store in the undo buffer\n//\n// Symbols you must define for implementation mode:\n//\n//    STB_TEXTEDIT_STRING               the type of object representing a string being edited,\n//                                      typically this is a wrapper object with other data you need\n//\n//    STB_TEXTEDIT_STRINGLEN(obj)       the length of the string (ideally O(1))\n//    STB_TEXTEDIT_LAYOUTROW(&r,obj,n)  returns the results of laying out a line of characters\n//                                        starting from character #n (see discussion below)\n//    STB_TEXTEDIT_GETWIDTH(obj,n,i)    returns the pixel delta from the xpos of the i'th character\n//                                        to the xpos of the i+1'th char for a line of characters\n//                                        starting at character #n (i.e. accounts for kerning\n//                                        with previous char)\n//    STB_TEXTEDIT_KEYTOTEXT(k)         maps a keyboard input to an insertable character\n//                                        (return type is int, -1 means not valid to insert)\n//    STB_TEXTEDIT_GETCHAR(obj,i)       returns the i'th character of obj, 0-based\n//    STB_TEXTEDIT_NEWLINE              the character returned by _GETCHAR() we recognize\n//                                        as manually wordwrapping for end-of-line positioning\n//\n//    STB_TEXTEDIT_DELETECHARS(obj,i,n)      delete n characters starting at i\n//    STB_TEXTEDIT_INSERTCHARS(obj,i,c*,n)   insert n characters at i (pointed to by STB_TEXTEDIT_CHARTYPE*)\n//\n//    STB_TEXTEDIT_K_SHIFT       a power of two that is or'd in to a keyboard input to represent the shift key\n//\n//    STB_TEXTEDIT_K_LEFT        keyboard input to move cursor left\n//    STB_TEXTEDIT_K_RIGHT       keyboard input to move cursor right\n//    STB_TEXTEDIT_K_UP          keyboard input to move cursor up\n//    STB_TEXTEDIT_K_DOWN        keyboard input to move cursor down\n//    STB_TEXTEDIT_K_LINESTART   keyboard input to move cursor to start of line  // e.g. HOME\n//    STB_TEXTEDIT_K_LINEEND     keyboard input to move cursor to end of line    // e.g. END\n//    STB_TEXTEDIT_K_TEXTSTART   keyboard input to move cursor to start of text  // e.g. ctrl-HOME\n//    STB_TEXTEDIT_K_TEXTEND     keyboard input to move cursor to end of text    // e.g. ctrl-END\n//    STB_TEXTEDIT_K_DELETE      keyboard input to delete selection or character under cursor\n//    STB_TEXTEDIT_K_BACKSPACE   keyboard input to delete selection or character left of cursor\n//    STB_TEXTEDIT_K_UNDO        keyboard input to perform undo\n//    STB_TEXTEDIT_K_REDO        keyboard input to perform redo\n//\n// Optional:\n//    STB_TEXTEDIT_K_INSERT              keyboard input to toggle insert mode\n//    STB_TEXTEDIT_IS_SPACE(ch)          true if character is whitespace (e.g. 'isspace'),\n//                                          required for default WORDLEFT/WORDRIGHT handlers\n//    STB_TEXTEDIT_MOVEWORDLEFT(obj,i)   custom handler for WORDLEFT, returns index to move cursor to\n//    STB_TEXTEDIT_MOVEWORDRIGHT(obj,i)  custom handler for WORDRIGHT, returns index to move cursor to\n//    STB_TEXTEDIT_K_WORDLEFT            keyboard input to move cursor left one word // e.g. ctrl-LEFT\n//    STB_TEXTEDIT_K_WORDRIGHT           keyboard input to move cursor right one word // e.g. ctrl-RIGHT\n//    STB_TEXTEDIT_K_LINESTART2          secondary keyboard input to move cursor to start of line\n//    STB_TEXTEDIT_K_LINEEND2            secondary keyboard input to move cursor to end of line\n//    STB_TEXTEDIT_K_TEXTSTART2          secondary keyboard input to move cursor to start of text\n//    STB_TEXTEDIT_K_TEXTEND2            secondary keyboard input to move cursor to end of text\n//\n// Todo:\n//    STB_TEXTEDIT_K_PGUP        keyboard input to move cursor up a page\n//    STB_TEXTEDIT_K_PGDOWN      keyboard input to move cursor down a page\n//\n// Keyboard input must be encoded as a single integer value; e.g. a character code\n// and some bitflags that represent shift states. to simplify the interface, SHIFT must\n// be a bitflag, so we can test the shifted state of cursor movements to allow selection,\n// i.e. (STB_TEXTED_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow.\n//\n// You can encode other things, such as CONTROL or ALT, in additional bits, and\n// then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example,\n// my Windows implementations add an additional CONTROL bit, and an additional KEYDOWN\n// bit. Then all of the STB_TEXTEDIT_K_ values bitwise-or in the KEYDOWN bit,\n// and I pass both WM_KEYDOWN and WM_CHAR events to the \"key\" function in the\n// API below. The control keys will only match WM_KEYDOWN events because of the\n// keydown bit I add, and STB_TEXTEDIT_KEYTOTEXT only tests for the KEYDOWN\n// bit so it only decodes WM_CHAR events.\n//\n// STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed\n// row of characters assuming they start on the i'th character--the width and\n// the height and the number of characters consumed. This allows this library\n// to traverse the entire layout incrementally. You need to compute word-wrapping\n// here.\n//\n// Each textfield keeps its own insert mode state, which is not how normal\n// applications work. To keep an app-wide insert mode, update/copy the\n// \"insert_mode\" field of STB_TexteditState before/after calling API functions.\n//\n// API\n//\n//    void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)\n//\n//    void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)\n//    void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)\n//    int  stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)\n//    int  stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)\n//    void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key)\n//\n//    Each of these functions potentially updates the string and updates the\n//    state.\n//\n//      initialize_state:\n//          set the textedit state to a known good default state when initially\n//          constructing the textedit.\n//\n//      click:\n//          call this with the mouse x,y on a mouse down; it will update the cursor\n//          and reset the selection start/end to the cursor point. the x,y must\n//          be relative to the text widget, with (0,0) being the top left.\n//     \n//      drag:\n//          call this with the mouse x,y on a mouse drag/up; it will update the\n//          cursor and the selection end point\n//     \n//      cut:\n//          call this to delete the current selection; returns true if there was\n//          one. you should FIRST copy the current selection to the system paste buffer.\n//          (To copy, just copy the current selection out of the string yourself.)\n//     \n//      paste:\n//          call this to paste text at the current cursor point or over the current\n//          selection if there is one.\n//     \n//      key:\n//          call this for keyboard inputs sent to the textfield. you can use it\n//          for \"key down\" events or for \"translated\" key events. if you need to\n//          do both (as in Win32), or distinguish Unicode characters from control\n//          inputs, set a high bit to distinguish the two; then you can define the\n//          various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit\n//          set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is\n//          clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to\n//          anything other type you wante before including.\n//\n//     \n//   When rendering, you can read the cursor position and selection state from\n//   the STB_TexteditState.\n//\n//\n// Notes:\n//\n// This is designed to be usable in IMGUI, so it allows for the possibility of\n// running in an IMGUI that has NOT cached the multi-line layout. For this\n// reason, it provides an interface that is compatible with computing the\n// layout incrementally--we try to make sure we make as few passes through\n// as possible. (For example, to locate the mouse pointer in the text, we\n// could define functions that return the X and Y positions of characters\n// and binary search Y and then X, but if we're doing dynamic layout this\n// will run the layout algorithm many times, so instead we manually search\n// forward in one pass. Similar logic applies to e.g. up-arrow and\n// down-arrow movement.)\n//\n// If it's run in a widget that *has* cached the layout, then this is less\n// efficient, but it's not horrible on modern computers. But you wouldn't\n// want to edit million-line files with it.\n\n\n////////////////////////////////////////////////////////////////////////////\n////////////////////////////////////////////////////////////////////////////\n////\n////   Header-file mode\n////\n////\n\n#ifndef INCLUDE_STB_TEXTEDIT_H\n#define INCLUDE_STB_TEXTEDIT_H\n\n////////////////////////////////////////////////////////////////////////\n//\n//     STB_TexteditState\n//\n// Definition of STB_TexteditState which you should store\n// per-textfield; it includes cursor position, selection state,\n// and undo state.\n//\n\n#ifndef STB_TEXTEDIT_UNDOSTATECOUNT\n#define STB_TEXTEDIT_UNDOSTATECOUNT   99\n#endif\n#ifndef STB_TEXTEDIT_UNDOCHARCOUNT\n#define STB_TEXTEDIT_UNDOCHARCOUNT   999\n#endif\n#ifndef STB_TEXTEDIT_CHARTYPE\n#define STB_TEXTEDIT_CHARTYPE        int\n#endif\n#ifndef STB_TEXTEDIT_POSITIONTYPE\n#define STB_TEXTEDIT_POSITIONTYPE    int\n#endif\n\ntypedef struct\n{\n   // private data\n   STB_TEXTEDIT_POSITIONTYPE  where;\n   STB_TEXTEDIT_POSITIONTYPE  insert_length;\n   STB_TEXTEDIT_POSITIONTYPE  delete_length;\n   int                        char_storage;\n} StbUndoRecord;\n\ntypedef struct\n{\n   // private data\n   StbUndoRecord          undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT];\n   STB_TEXTEDIT_CHARTYPE  undo_char[STB_TEXTEDIT_UNDOCHARCOUNT];\n   short undo_point, redo_point;\n   int undo_char_point, redo_char_point;\n} StbUndoState;\n\ntypedef struct\n{\n   /////////////////////\n   //\n   // public data\n   //\n\n   int cursor;\n   // position of the text cursor within the string\n\n   int select_start;          // selection start point\n   int select_end;\n   // selection start and end point in characters; if equal, no selection.\n   // note that start may be less than or greater than end (e.g. when\n   // dragging the mouse, start is where the initial click was, and you\n   // can drag in either direction)\n\n   unsigned char insert_mode;\n   // each textfield keeps its own insert mode state. to keep an app-wide\n   // insert mode, copy this value in/out of the app state\n\n   /////////////////////\n   //\n   // private data\n   //\n   unsigned char cursor_at_end_of_line; // not implemented yet\n   unsigned char initialized;\n   unsigned char has_preferred_x;\n   unsigned char single_line;\n   unsigned char padding1, padding2, padding3;\n   float preferred_x; // this determines where the cursor up/down tries to seek to along x\n   StbUndoState undostate;\n} STB_TexteditState;\n\n\n////////////////////////////////////////////////////////////////////////\n//\n//     StbTexteditRow\n//\n// Result of layout query, used by stb_textedit to determine where\n// the text in each row is.\n\n// result of layout query\ntypedef struct\n{\n   float x0,x1;             // starting x location, end x location (allows for align=right, etc)\n   float baseline_y_delta;  // position of baseline relative to previous row's baseline\n   float ymin,ymax;         // height of row above and below baseline\n   int num_chars;\n} StbTexteditRow;\n#endif //INCLUDE_STB_TEXTEDIT_H\n\n\n////////////////////////////////////////////////////////////////////////////\n////////////////////////////////////////////////////////////////////////////\n////\n////   Implementation mode\n////\n////\n\n\n// implementation isn't include-guarded, since it might have indirectly\n// included just the \"header\" portion\n#ifdef STB_TEXTEDIT_IMPLEMENTATION\n\n#ifndef STB_TEXTEDIT_memmove\n#include <string.h>\n#define STB_TEXTEDIT_memmove memmove\n#endif\n\n\n/////////////////////////////////////////////////////////////////////////////\n//\n//      Mouse input handling\n//\n\n// traverse the layout to locate the nearest character to a display position\nstatic int stb_text_locate_coord(STB_TEXTEDIT_STRING *str, float x, float y)\n{\n   StbTexteditRow r;\n   int n = STB_TEXTEDIT_STRINGLEN(str);\n   float base_y = 0, prev_x;\n   int i=0, k;\n\n   r.x0 = r.x1 = 0;\n   r.ymin = r.ymax = 0;\n   r.num_chars = 0;\n\n   // search rows to find one that straddles 'y'\n   while (i < n) {\n      STB_TEXTEDIT_LAYOUTROW(&r, str, i);\n      if (r.num_chars <= 0)\n         return n;\n\n      if (i==0 && y < base_y + r.ymin)\n         return 0;\n\n      if (y < base_y + r.ymax)\n         break;\n\n      i += r.num_chars;\n      base_y += r.baseline_y_delta;\n   }\n\n   // below all text, return 'after' last character\n   if (i >= n)\n      return n;\n\n   // check if it's before the beginning of the line\n   if (x < r.x0)\n      return i;\n\n   // check if it's before the end of the line\n   if (x < r.x1) {\n      // search characters in row for one that straddles 'x'\n      prev_x = r.x0;\n      for (k=0; k < r.num_chars; ++k) {\n         float w = STB_TEXTEDIT_GETWIDTH(str, i, k);\n         if (x < prev_x+w) {\n            if (x < prev_x+w/2)\n               return k+i;\n            else\n               return k+i+1;\n         }\n         prev_x += w;\n      }\n      // shouldn't happen, but if it does, fall through to end-of-line case\n   }\n\n   // if the last character is a newline, return that. otherwise return 'after' the last character\n   if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE)\n      return i+r.num_chars-1;\n   else\n      return i+r.num_chars;\n}\n\n// API click: on mouse down, move the cursor to the clicked location, and reset the selection\nstatic void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)\n{\n   // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse\n   // goes off the top or bottom of the text\n   if( state->single_line )\n   {\n      StbTexteditRow r;\n      STB_TEXTEDIT_LAYOUTROW(&r, str, 0);\n      y = r.ymin;\n   }\n\n   state->cursor = stb_text_locate_coord(str, x, y);\n   state->select_start = state->cursor;\n   state->select_end = state->cursor;\n   state->has_preferred_x = 0;\n}\n\n// API drag: on mouse drag, move the cursor and selection endpoint to the clicked location\nstatic void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)\n{\n   int p = 0;\n\n   // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse\n   // goes off the top or bottom of the text\n   if( state->single_line )\n   {\n      StbTexteditRow r;\n      STB_TEXTEDIT_LAYOUTROW(&r, str, 0);\n      y = r.ymin;\n   }\n\n   if (state->select_start == state->select_end)\n      state->select_start = state->cursor;\n\n   p = stb_text_locate_coord(str, x, y);\n   state->cursor = state->select_end = p;\n}\n\n/////////////////////////////////////////////////////////////////////////////\n//\n//      Keyboard input handling\n//\n\n// forward declarations\nstatic void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);\nstatic void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);\nstatic void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length);\nstatic void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length);\nstatic void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length);\n\ntypedef struct\n{\n   float x,y;    // position of n'th character\n   float height; // height of line\n   int first_char, length; // first char of row, and length\n   int prev_first;  // first char of previous row\n} StbFindState;\n\n// find the x/y location of a character, and remember info about the previous row in\n// case we get a move-up event (for page up, we'll have to rescan)\nstatic void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *str, int n, int single_line)\n{\n   StbTexteditRow r;\n   int prev_start = 0;\n   int z = STB_TEXTEDIT_STRINGLEN(str);\n   int i=0, first;\n\n   if (n == z) {\n      // if it's at the end, then find the last line -- simpler than trying to\n      // explicitly handle this case in the regular code\n      if (single_line) {\n         STB_TEXTEDIT_LAYOUTROW(&r, str, 0);\n         find->y = 0;\n         find->first_char = 0;\n         find->length = z;\n         find->height = r.ymax - r.ymin;\n         find->x = r.x1;\n      } else {\n         find->y = 0;\n         find->x = 0;\n         find->height = 1;\n         while (i < z) {\n            STB_TEXTEDIT_LAYOUTROW(&r, str, i);\n            prev_start = i;\n            i += r.num_chars;\n         }\n         find->first_char = i;\n         find->length = 0;\n         find->prev_first = prev_start;\n      }\n      return;\n   }\n\n   // search rows to find the one that straddles character n\n   find->y = 0;\n\n   for(;;) {\n      STB_TEXTEDIT_LAYOUTROW(&r, str, i);\n      if (n < i + r.num_chars)\n         break;\n      prev_start = i;\n      i += r.num_chars;\n      find->y += r.baseline_y_delta;\n   }\n\n   find->first_char = first = i;\n   find->length = r.num_chars;\n   find->height = r.ymax - r.ymin;\n   find->prev_first = prev_start;\n\n   // now scan to find xpos\n   find->x = r.x0;\n   for (i=0; first+i < n; ++i)\n      find->x += STB_TEXTEDIT_GETWIDTH(str, first, i);\n}\n\n#define STB_TEXT_HAS_SELECTION(s)   ((s)->select_start != (s)->select_end)\n\n// make the selection/cursor state valid if client altered the string\nstatic void stb_textedit_clamp(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)\n{\n   int n = STB_TEXTEDIT_STRINGLEN(str);\n   if (STB_TEXT_HAS_SELECTION(state)) {\n      if (state->select_start > n) state->select_start = n;\n      if (state->select_end   > n) state->select_end = n;\n      // if clamping forced them to be equal, move the cursor to match\n      if (state->select_start == state->select_end)\n         state->cursor = state->select_start;\n   }\n   if (state->cursor > n) state->cursor = n;\n}\n\n// delete characters while updating undo\nstatic void stb_textedit_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len)\n{\n   stb_text_makeundo_delete(str, state, where, len);\n   STB_TEXTEDIT_DELETECHARS(str, where, len);\n   state->has_preferred_x = 0;\n}\n\n// delete the section\nstatic void stb_textedit_delete_selection(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)\n{\n   stb_textedit_clamp(str, state);\n   if (STB_TEXT_HAS_SELECTION(state)) {\n      if (state->select_start < state->select_end) {\n         stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start);\n         state->select_end = state->cursor = state->select_start;\n      } else {\n         stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end);\n         state->select_start = state->cursor = state->select_end;\n      }\n      state->has_preferred_x = 0;\n   }\n}\n\n// canoncialize the selection so start <= end\nstatic void stb_textedit_sortselection(STB_TexteditState *state)\n{\n   if (state->select_end < state->select_start) {\n      int temp = state->select_end;\n      state->select_end = state->select_start;\n      state->select_start = temp;\n   }\n}\n\n// move cursor to first character of selection\nstatic void stb_textedit_move_to_first(STB_TexteditState *state)\n{\n   if (STB_TEXT_HAS_SELECTION(state)) {\n      stb_textedit_sortselection(state);\n      state->cursor = state->select_start;\n      state->select_end = state->select_start;\n      state->has_preferred_x = 0;\n   }\n}\n\n// move cursor to last character of selection\nstatic void stb_textedit_move_to_last(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)\n{\n   if (STB_TEXT_HAS_SELECTION(state)) {\n      stb_textedit_sortselection(state);\n      stb_textedit_clamp(str, state);\n      state->cursor = state->select_end;\n      state->select_start = state->select_end;\n      state->has_preferred_x = 0;\n   }\n}\n\n#ifdef STB_TEXTEDIT_IS_SPACE\nstatic int is_word_boundary( STB_TEXTEDIT_STRING *str, int idx )\n{\n   return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1;\n}\n\n#ifndef STB_TEXTEDIT_MOVEWORDLEFT\nstatic int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING *str, int c )\n{\n   --c; // always move at least one character\n   while( c >= 0 && !is_word_boundary( str, c ) )\n      --c;\n\n   if( c < 0 )\n      c = 0;\n\n   return c;\n}\n#define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous\n#endif\n\n#ifndef STB_TEXTEDIT_MOVEWORDRIGHT\nstatic int stb_textedit_move_to_word_next( STB_TEXTEDIT_STRING *str, int c )\n{\n   const int len = STB_TEXTEDIT_STRINGLEN(str);\n   ++c; // always move at least one character\n   while( c < len && !is_word_boundary( str, c ) )\n      ++c;\n\n   if( c > len )\n      c = len;\n\n   return c;\n}\n#define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next\n#endif\n\n#endif\n\n// update selection and cursor to match each other\nstatic void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state)\n{\n   if (!STB_TEXT_HAS_SELECTION(state))\n      state->select_start = state->select_end = state->cursor;\n   else\n      state->cursor = state->select_end;\n}\n\n// API cut: delete selection\nstatic int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)\n{\n   if (STB_TEXT_HAS_SELECTION(state)) {\n      stb_textedit_delete_selection(str,state); // implicitly clamps\n      state->has_preferred_x = 0;\n      return 1;\n   }\n   return 0;\n}\n\n// API paste: replace existing selection with passed-in text\nstatic int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)\n{\n   // if there's a selection, the paste should delete it\n   stb_textedit_clamp(str, state);\n   stb_textedit_delete_selection(str,state);\n   // try to insert the characters\n   if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) {\n      stb_text_makeundo_insert(state, state->cursor, len);\n      state->cursor += len;\n      state->has_preferred_x = 0;\n      return 1;\n   }\n   // remove the undo since we didn't actually insert the characters\n   if (state->undostate.undo_point)\n      --state->undostate.undo_point;\n   return 0;\n}\n\n#ifndef STB_TEXTEDIT_KEYTYPE\n#define STB_TEXTEDIT_KEYTYPE int\n#endif\n\n// API key: process a keyboard input\nstatic void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key)\n{\nretry:\n   switch (key) {\n      default: {\n         int c = STB_TEXTEDIT_KEYTOTEXT(key);\n         if (c > 0) {\n            STB_TEXTEDIT_CHARTYPE ch = (STB_TEXTEDIT_CHARTYPE) c;\n\n            // can't add newline in single-line mode\n            if (c == '\\n' && state->single_line)\n               break;\n\n            if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) {\n               stb_text_makeundo_replace(str, state, state->cursor, 1, 1);\n               STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1);\n               if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {\n                  ++state->cursor;\n                  state->has_preferred_x = 0;\n               }\n            } else {\n               stb_textedit_delete_selection(str,state); // implicitly clamps\n               if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {\n                  stb_text_makeundo_insert(state, state->cursor, 1);\n                  ++state->cursor;\n                  state->has_preferred_x = 0;\n               }\n            }\n         }\n         break;\n      }\n\n#ifdef STB_TEXTEDIT_K_INSERT\n      case STB_TEXTEDIT_K_INSERT:\n         state->insert_mode = !state->insert_mode;\n         break;\n#endif\n         \n      case STB_TEXTEDIT_K_UNDO:\n         stb_text_undo(str, state);\n         state->has_preferred_x = 0;\n         break;\n\n      case STB_TEXTEDIT_K_REDO:\n         stb_text_redo(str, state);\n         state->has_preferred_x = 0;\n         break;\n\n      case STB_TEXTEDIT_K_LEFT:\n         // if currently there's a selection, move cursor to start of selection\n         if (STB_TEXT_HAS_SELECTION(state))\n            stb_textedit_move_to_first(state);\n         else \n            if (state->cursor > 0)\n               --state->cursor;\n         state->has_preferred_x = 0;\n         break;\n\n      case STB_TEXTEDIT_K_RIGHT:\n         // if currently there's a selection, move cursor to end of selection\n         if (STB_TEXT_HAS_SELECTION(state))\n            stb_textedit_move_to_last(str, state);\n         else\n            ++state->cursor;\n         stb_textedit_clamp(str, state);\n         state->has_preferred_x = 0;\n         break;\n\n      case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT:\n         stb_textedit_clamp(str, state);\n         stb_textedit_prep_selection_at_cursor(state);\n         // move selection left\n         if (state->select_end > 0)\n            --state->select_end;\n         state->cursor = state->select_end;\n         state->has_preferred_x = 0;\n         break;\n\n#ifdef STB_TEXTEDIT_MOVEWORDLEFT\n      case STB_TEXTEDIT_K_WORDLEFT:\n         if (STB_TEXT_HAS_SELECTION(state))\n            stb_textedit_move_to_first(state);\n         else {\n            state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);\n            stb_textedit_clamp( str, state );\n         }\n         break;\n\n      case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT:\n         if( !STB_TEXT_HAS_SELECTION( state ) )\n            stb_textedit_prep_selection_at_cursor(state);\n\n         state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);\n         state->select_end = state->cursor;\n\n         stb_textedit_clamp( str, state );\n         break;\n#endif\n\n#ifdef STB_TEXTEDIT_MOVEWORDRIGHT\n      case STB_TEXTEDIT_K_WORDRIGHT:\n         if (STB_TEXT_HAS_SELECTION(state)) \n            stb_textedit_move_to_last(str, state);\n         else {\n            state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);\n            stb_textedit_clamp( str, state );\n         }\n         break;\n\n      case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT:\n         if( !STB_TEXT_HAS_SELECTION( state ) )\n            stb_textedit_prep_selection_at_cursor(state);\n\n         state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);\n         state->select_end = state->cursor;\n\n         stb_textedit_clamp( str, state );\n         break;\n#endif\n\n      case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT:\n         stb_textedit_prep_selection_at_cursor(state);\n         // move selection right\n         ++state->select_end;\n         stb_textedit_clamp(str, state);\n         state->cursor = state->select_end;\n         state->has_preferred_x = 0;\n         break;\n\n      case STB_TEXTEDIT_K_DOWN:\n      case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT: {\n         StbFindState find;\n         StbTexteditRow row;\n         int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;\n\n         if (state->single_line) {\n            // on windows, up&down in single-line behave like left&right\n            key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT);\n            goto retry;\n         }\n\n         if (sel)\n            stb_textedit_prep_selection_at_cursor(state);\n         else if (STB_TEXT_HAS_SELECTION(state))\n            stb_textedit_move_to_last(str,state);\n\n         // compute current position of cursor point\n         stb_textedit_clamp(str, state);\n         stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);\n\n         // now find character position down a row\n         if (find.length) {\n            float goal_x = state->has_preferred_x ? state->preferred_x : find.x;\n            float x;\n            int start = find.first_char + find.length;\n            state->cursor = start;\n            STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);\n            x = row.x0;\n            for (i=0; i < row.num_chars; ++i) {\n               float dx = STB_TEXTEDIT_GETWIDTH(str, start, i);\n               #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE\n               if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)\n                  break;\n               #endif\n               x += dx;\n               if (x > goal_x)\n                  break;\n               ++state->cursor;\n            }\n            stb_textedit_clamp(str, state);\n\n            state->has_preferred_x = 1;\n            state->preferred_x = goal_x;\n\n            if (sel)\n               state->select_end = state->cursor;\n         }\n         break;\n      }\n         \n      case STB_TEXTEDIT_K_UP:\n      case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: {\n         StbFindState find;\n         StbTexteditRow row;\n         int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;\n\n         if (state->single_line) {\n            // on windows, up&down become left&right\n            key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT);\n            goto retry;\n         }\n\n         if (sel)\n            stb_textedit_prep_selection_at_cursor(state);\n         else if (STB_TEXT_HAS_SELECTION(state))\n            stb_textedit_move_to_first(state);\n\n         // compute current position of cursor point\n         stb_textedit_clamp(str, state);\n         stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);\n\n         // can only go up if there's a previous row\n         if (find.prev_first != find.first_char) {\n            // now find character position up a row\n            float goal_x = state->has_preferred_x ? state->preferred_x : find.x;\n            float x;\n            state->cursor = find.prev_first;\n            STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);\n            x = row.x0;\n            for (i=0; i < row.num_chars; ++i) {\n               float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i);\n               #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE\n               if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)\n                  break;\n               #endif\n               x += dx;\n               if (x > goal_x)\n                  break;\n               ++state->cursor;\n            }\n            stb_textedit_clamp(str, state);\n\n            state->has_preferred_x = 1;\n            state->preferred_x = goal_x;\n\n            if (sel)\n               state->select_end = state->cursor;\n         }\n         break;\n      }\n\n      case STB_TEXTEDIT_K_DELETE:\n      case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT:\n         if (STB_TEXT_HAS_SELECTION(state))\n            stb_textedit_delete_selection(str, state);\n         else {\n            int n = STB_TEXTEDIT_STRINGLEN(str);\n            if (state->cursor < n)\n               stb_textedit_delete(str, state, state->cursor, 1);\n         }\n         state->has_preferred_x = 0;\n         break;\n\n      case STB_TEXTEDIT_K_BACKSPACE:\n      case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT:\n         if (STB_TEXT_HAS_SELECTION(state))\n            stb_textedit_delete_selection(str, state);\n         else {\n            stb_textedit_clamp(str, state);\n            if (state->cursor > 0) {\n               stb_textedit_delete(str, state, state->cursor-1, 1);\n               --state->cursor;\n            }\n         }\n         state->has_preferred_x = 0;\n         break;\n         \n#ifdef STB_TEXTEDIT_K_TEXTSTART2\n      case STB_TEXTEDIT_K_TEXTSTART2:\n#endif\n      case STB_TEXTEDIT_K_TEXTSTART:\n         state->cursor = state->select_start = state->select_end = 0;\n         state->has_preferred_x = 0;\n         break;\n\n#ifdef STB_TEXTEDIT_K_TEXTEND2\n      case STB_TEXTEDIT_K_TEXTEND2:\n#endif\n      case STB_TEXTEDIT_K_TEXTEND:\n         state->cursor = STB_TEXTEDIT_STRINGLEN(str);\n         state->select_start = state->select_end = 0;\n         state->has_preferred_x = 0;\n         break;\n        \n#ifdef STB_TEXTEDIT_K_TEXTSTART2\n      case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT:\n#endif\n      case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT:\n         stb_textedit_prep_selection_at_cursor(state);\n         state->cursor = state->select_end = 0;\n         state->has_preferred_x = 0;\n         break;\n\n#ifdef STB_TEXTEDIT_K_TEXTEND2\n      case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT:\n#endif\n      case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT:\n         stb_textedit_prep_selection_at_cursor(state);\n         state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str);\n         state->has_preferred_x = 0;\n         break;\n\n\n#ifdef STB_TEXTEDIT_K_LINESTART2\n      case STB_TEXTEDIT_K_LINESTART2:\n#endif\n      case STB_TEXTEDIT_K_LINESTART:\n         stb_textedit_clamp(str, state);\n         stb_textedit_move_to_first(state);\n         if (state->single_line)\n            state->cursor = 0;\n         else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)\n            --state->cursor;\n         state->has_preferred_x = 0;\n         break;\n\n#ifdef STB_TEXTEDIT_K_LINEEND2\n      case STB_TEXTEDIT_K_LINEEND2:\n#endif\n      case STB_TEXTEDIT_K_LINEEND: {\n         int n = STB_TEXTEDIT_STRINGLEN(str);\n         stb_textedit_clamp(str, state);\n         stb_textedit_move_to_first(state);\n         if (state->single_line)\n             state->cursor = n;\n         else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)\n             ++state->cursor;\n         state->has_preferred_x = 0;\n         break;\n      }\n\n#ifdef STB_TEXTEDIT_K_LINESTART2\n      case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT:\n#endif\n      case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT:\n         stb_textedit_clamp(str, state);\n         stb_textedit_prep_selection_at_cursor(state);\n         if (state->single_line)\n            state->cursor = 0;\n         else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)\n            --state->cursor;\n         state->select_end = state->cursor;\n         state->has_preferred_x = 0;\n         break;\n\n#ifdef STB_TEXTEDIT_K_LINEEND2\n      case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:\n#endif\n      case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {\n         int n = STB_TEXTEDIT_STRINGLEN(str);\n         stb_textedit_clamp(str, state);\n         stb_textedit_prep_selection_at_cursor(state);\n         if (state->single_line)\n             state->cursor = n;\n         else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)\n            ++state->cursor;\n         state->select_end = state->cursor;\n         state->has_preferred_x = 0;\n         break;\n      }\n\n// @TODO:\n//    STB_TEXTEDIT_K_PGUP      - move cursor up a page\n//    STB_TEXTEDIT_K_PGDOWN    - move cursor down a page\n   }\n}\n\n/////////////////////////////////////////////////////////////////////////////\n//\n//      Undo processing\n//\n// @OPTIMIZE: the undo/redo buffer should be circular\n\nstatic void stb_textedit_flush_redo(StbUndoState *state)\n{\n   state->redo_point = STB_TEXTEDIT_UNDOSTATECOUNT;\n   state->redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT;\n}\n\n// discard the oldest entry in the undo list\nstatic void stb_textedit_discard_undo(StbUndoState *state)\n{\n   if (state->undo_point > 0) {\n      // if the 0th undo state has characters, clean those up\n      if (state->undo_rec[0].char_storage >= 0) {\n         int n = state->undo_rec[0].insert_length, i;\n         // delete n characters from all other records\n         state->undo_char_point -= n;\n         STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE)));\n         for (i=0; i < state->undo_point; ++i)\n            if (state->undo_rec[i].char_storage >= 0)\n               state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it\n      }\n      --state->undo_point;\n      STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));\n   }\n}\n\n// discard the oldest entry in the redo list--it's bad if this\n// ever happens, but because undo & redo have to store the actual\n// characters in different cases, the redo character buffer can\n// fill up even though the undo buffer didn't\nstatic void stb_textedit_discard_redo(StbUndoState *state)\n{\n   int k = STB_TEXTEDIT_UNDOSTATECOUNT-1;\n\n   if (state->redo_point <= k) {\n      // if the k'th undo state has characters, clean those up\n      if (state->undo_rec[k].char_storage >= 0) {\n         int n = state->undo_rec[k].insert_length, i;\n         // move the remaining redo character data to the end of the buffer\n         state->redo_char_point += n;\n         STB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((STB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(STB_TEXTEDIT_CHARTYPE)));\n         // adjust the position of all the other records to account for above memmove\n         for (i=state->redo_point; i < k; ++i)\n            if (state->undo_rec[i].char_storage >= 0)\n               state->undo_rec[i].char_storage += n;\n      }\n      // now move all the redo records towards the end of the buffer; the first one is at 'redo_point'\n      // {DEAR IMGUI]\n      size_t move_size = (size_t)((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0]));\n      const char* buf_begin = (char*)state->undo_rec; (void)buf_begin;\n      const char* buf_end   = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end;\n      IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin);\n      IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end);\n      STB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size);\n\n      // now move redo_point to point to the new one\n      ++state->redo_point;\n   }\n}\n\nstatic StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars)\n{\n   // any time we create a new undo record, we discard redo\n   stb_textedit_flush_redo(state);\n\n   // if we have no free records, we have to make room, by sliding the\n   // existing records down\n   if (state->undo_point == STB_TEXTEDIT_UNDOSTATECOUNT)\n      stb_textedit_discard_undo(state);\n\n   // if the characters to store won't possibly fit in the buffer, we can't undo\n   if (numchars > STB_TEXTEDIT_UNDOCHARCOUNT) {\n      state->undo_point = 0;\n      state->undo_char_point = 0;\n      return NULL;\n   }\n\n   // if we don't have enough free characters in the buffer, we have to make room\n   while (state->undo_char_point + numchars > STB_TEXTEDIT_UNDOCHARCOUNT)\n      stb_textedit_discard_undo(state);\n\n   return &state->undo_rec[state->undo_point++];\n}\n\nstatic STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len)\n{\n   StbUndoRecord *r = stb_text_create_undo_record(state, insert_len);\n   if (r == NULL)\n      return NULL;\n\n   r->where = pos;\n   r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len;\n   r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len;\n\n   if (insert_len == 0) {\n      r->char_storage = -1;\n      return NULL;\n   } else {\n      r->char_storage = state->undo_char_point;\n      state->undo_char_point += insert_len;\n      return &state->undo_char[r->char_storage];\n   }\n}\n\nstatic void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)\n{\n   StbUndoState *s = &state->undostate;\n   StbUndoRecord u, *r;\n   if (s->undo_point == 0)\n      return;\n\n   // we need to do two things: apply the undo record, and create a redo record\n   u = s->undo_rec[s->undo_point-1];\n   r = &s->undo_rec[s->redo_point-1];\n   r->char_storage = -1;\n\n   r->insert_length = u.delete_length;\n   r->delete_length = u.insert_length;\n   r->where = u.where;\n\n   if (u.delete_length) {\n      // if the undo record says to delete characters, then the redo record will\n      // need to re-insert the characters that get deleted, so we need to store\n      // them.\n\n      // there are three cases:\n      //    there's enough room to store the characters\n      //    characters stored for *redoing* don't leave room for redo\n      //    characters stored for *undoing* don't leave room for redo\n      // if the last is true, we have to bail\n\n      if (s->undo_char_point + u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT) {\n         // the undo records take up too much character space; there's no space to store the redo characters\n         r->insert_length = 0;\n      } else {\n         int i;\n\n         // there's definitely room to store the characters eventually\n         while (s->undo_char_point + u.delete_length > s->redo_char_point) {\n            // should never happen:\n            if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT)\n               return;\n            // there's currently not enough room, so discard a redo record\n            stb_textedit_discard_redo(s);\n         }\n         r = &s->undo_rec[s->redo_point-1];\n\n         r->char_storage = s->redo_char_point - u.delete_length;\n         s->redo_char_point = s->redo_char_point - u.delete_length;\n\n         // now save the characters\n         for (i=0; i < u.delete_length; ++i)\n            s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i);\n      }\n\n      // now we can carry out the deletion\n      STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length);\n   }\n\n   // check type of recorded action:\n   if (u.insert_length) {\n      // easy case: was a deletion, so we need to insert n characters\n      STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length);\n      s->undo_char_point -= u.insert_length;\n   }\n\n   state->cursor = u.where + u.insert_length;\n\n   s->undo_point--;\n   s->redo_point--;\n}\n\nstatic void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)\n{\n   StbUndoState *s = &state->undostate;\n   StbUndoRecord *u, r;\n   if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT)\n      return;\n\n   // we need to do two things: apply the redo record, and create an undo record\n   u = &s->undo_rec[s->undo_point];\n   r = s->undo_rec[s->redo_point];\n\n   // we KNOW there must be room for the undo record, because the redo record\n   // was derived from an undo record\n\n   u->delete_length = r.insert_length;\n   u->insert_length = r.delete_length;\n   u->where = r.where;\n   u->char_storage = -1;\n\n   if (r.delete_length) {\n      // the redo record requires us to delete characters, so the undo record\n      // needs to store the characters\n\n      if (s->undo_char_point + u->insert_length > s->redo_char_point) {\n         u->insert_length = 0;\n         u->delete_length = 0;\n      } else {\n         int i;\n         u->char_storage = s->undo_char_point;\n         s->undo_char_point = s->undo_char_point + u->insert_length;\n\n         // now save the characters\n         for (i=0; i < u->insert_length; ++i)\n            s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i);\n      }\n\n      STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length);\n   }\n\n   if (r.insert_length) {\n      // easy case: need to insert n characters\n      STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length);\n      s->redo_char_point += r.insert_length;\n   }\n\n   state->cursor = r.where + r.insert_length;\n\n   s->undo_point++;\n   s->redo_point++;\n}\n\nstatic void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length)\n{\n   stb_text_createundo(&state->undostate, where, 0, length);\n}\n\nstatic void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length)\n{\n   int i;\n   STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0);\n   if (p) {\n      for (i=0; i < length; ++i)\n         p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);\n   }\n}\n\nstatic void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length)\n{\n   int i;\n   STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length);\n   if (p) {\n      for (i=0; i < old_length; ++i)\n         p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);\n   }\n}\n\n// reset the state to default\nstatic void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line)\n{\n   state->undostate.undo_point = 0;\n   state->undostate.undo_char_point = 0;\n   state->undostate.redo_point = STB_TEXTEDIT_UNDOSTATECOUNT;\n   state->undostate.redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT;\n   state->select_end = state->select_start = 0;\n   state->cursor = 0;\n   state->has_preferred_x = 0;\n   state->preferred_x = 0;\n   state->cursor_at_end_of_line = 0;\n   state->initialized = 1;\n   state->single_line = (unsigned char) is_single_line;\n   state->insert_mode = 0;\n}\n\n// API initialize\nstatic void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)\n{\n   stb_textedit_clear_state(state, is_single_line);\n}\n\n#if defined(__GNUC__) || defined(__clang__)\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wcast-qual\"\n#endif\n\nstatic int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE const *ctext, int len)\n{\n   return stb_textedit_paste_internal(str, state, (STB_TEXTEDIT_CHARTYPE *) ctext, len);\n}\n\n#if defined(__GNUC__) || defined(__clang__)\n#pragma GCC diagnostic pop\n#endif\n\n#endif//STB_TEXTEDIT_IMPLEMENTATION\n\n/*\n------------------------------------------------------------------------------\nThis software is available under 2 licenses -- choose whichever you prefer.\n------------------------------------------------------------------------------\nALTERNATIVE A - MIT License\nCopyright (c) 2017 Sean Barrett\nPermission is hereby granted, free of charge, to any person obtaining a copy of \nthis software and associated documentation files (the \"Software\"), to deal in \nthe Software without restriction, including without limitation the rights to \nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies \nof the Software, and to permit persons to whom the Software is furnished to do \nso, subject to the following conditions:\nThe above copyright notice and this permission notice shall be included in all \ncopies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR \nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, \nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE \nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER \nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, \nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE \nSOFTWARE.\n------------------------------------------------------------------------------\nALTERNATIVE B - Public Domain (www.unlicense.org)\nThis is free and unencumbered software released into the public domain.\nAnyone is free to copy, modify, publish, use, compile, sell, or distribute this \nsoftware, either in source code form or as a compiled binary, for any purpose, \ncommercial or non-commercial, and by any means.\nIn jurisdictions that recognize copyright laws, the author or authors of this \nsoftware dedicate any and all copyright interest in the software to the public \ndomain. We make this dedication for the benefit of the public at large and to \nthe detriment of our heirs and successors. We intend this dedication to be an \novert act of relinquishment in perpetuity of all present and future rights to \nthis software under copyright law.\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR \nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, \nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE \nAUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN \nACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION \nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n------------------------------------------------------------------------------\n*/\n"
  },
  {
    "path": "src/imgui/imstb_truetype.hpp",
    "content": "// [DEAR IMGUI] \n// This is a slightly modified version of stb_truetype.h 1.20.\n// Mostly fixing for compiler and static analyzer warnings.\n// Grep for [DEAR IMGUI] to find the changes.\n\n// stb_truetype.h - v1.20 - public domain\n// authored from 2009-2016 by Sean Barrett / RAD Game Tools\n//\n//   This library processes TrueType files:\n//        parse files\n//        extract glyph metrics\n//        extract glyph shapes\n//        render glyphs to one-channel bitmaps with antialiasing (box filter)\n//        render glyphs to one-channel SDF bitmaps (signed-distance field/function)\n//\n//   Todo:\n//        non-MS cmaps\n//        crashproof on bad data\n//        hinting? (no longer patented)\n//        cleartype-style AA?\n//        optimize: use simple memory allocator for intermediates\n//        optimize: build edge-list directly from curves\n//        optimize: rasterize directly from curves?\n//\n// ADDITIONAL CONTRIBUTORS\n//\n//   Mikko Mononen: compound shape support, more cmap formats\n//   Tor Andersson: kerning, subpixel rendering\n//   Dougall Johnson: OpenType / Type 2 font handling\n//   Daniel Ribeiro Maciel: basic GPOS-based kerning\n//\n//   Misc other:\n//       Ryan Gordon\n//       Simon Glass\n//       github:IntellectualKitty\n//       Imanol Celaya\n//       Daniel Ribeiro Maciel\n//\n//   Bug/warning reports/fixes:\n//       \"Zer\" on mollyrocket       Fabian \"ryg\" Giesen\n//       Cass Everitt               Martins Mozeiko\n//       stoiko (Haemimont Games)   Cap Petschulat\n//       Brian Hook                 Omar Cornut\n//       Walter van Niftrik         github:aloucks\n//       David Gow                  Peter LaValle\n//       David Given                Sergey Popov\n//       Ivan-Assen Ivanov          Giumo X. Clanjor\n//       Anthony Pesch              Higor Euripedes\n//       Johan Duparc               Thomas Fields\n//       Hou Qiming                 Derek Vinyard\n//       Rob Loach                  Cort Stratton\n//       Kenney Phillis Jr.         github:oyvindjam\n//       Brian Costabile            github:vassvik\n//       \n// VERSION HISTORY\n//\n//   1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics()\n//   1.19 (2018-02-11) GPOS kerning, STBTT_fmod\n//   1.18 (2018-01-29) add missing function\n//   1.17 (2017-07-23) make more arguments const; doc fix\n//   1.16 (2017-07-12) SDF support\n//   1.15 (2017-03-03) make more arguments const\n//   1.14 (2017-01-16) num-fonts-in-TTC function\n//   1.13 (2017-01-02) support OpenType fonts, certain Apple fonts\n//   1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual\n//   1.11 (2016-04-02) fix unused-variable warning\n//   1.10 (2016-04-02) user-defined fabs(); rare memory leak; remove duplicate typedef\n//   1.09 (2016-01-16) warning fix; avoid crash on outofmem; use allocation userdata properly\n//   1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges\n//   1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints;\n//                     variant PackFontRanges to pack and render in separate phases;\n//                     fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?);\n//                     fixed an assert() bug in the new rasterizer\n//                     replace assert() with STBTT_assert() in new rasterizer\n//\n//   Full history can be found at the end of this file.\n//\n// LICENSE\n//\n//   See end of file for license information.\n//\n// USAGE\n//\n//   Include this file in whatever places need to refer to it. In ONE C/C++\n//   file, write:\n//      #define STB_TRUETYPE_IMPLEMENTATION\n//   before the #include of this file. This expands out the actual\n//   implementation into that C/C++ file.\n//\n//   To make the implementation private to the file that generates the implementation,\n//      #define STBTT_STATIC\n//\n//   Simple 3D API (don't ship this, but it's fine for tools and quick start)\n//           stbtt_BakeFontBitmap()               -- bake a font to a bitmap for use as texture\n//           stbtt_GetBakedQuad()                 -- compute quad to draw for a given char\n//\n//   Improved 3D API (more shippable):\n//           #include \"stb_rect_pack.h\"           -- optional, but you really want it\n//           stbtt_PackBegin()\n//           stbtt_PackSetOversampling()          -- for improved quality on small fonts\n//           stbtt_PackFontRanges()               -- pack and renders\n//           stbtt_PackEnd()\n//           stbtt_GetPackedQuad()\n//\n//   \"Load\" a font file from a memory buffer (you have to keep the buffer loaded)\n//           stbtt_InitFont()\n//           stbtt_GetFontOffsetForIndex()        -- indexing for TTC font collections\n//           stbtt_GetNumberOfFonts()             -- number of fonts for TTC font collections\n//\n//   Render a unicode codepoint to a bitmap\n//           stbtt_GetCodepointBitmap()           -- allocates and returns a bitmap\n//           stbtt_MakeCodepointBitmap()          -- renders into bitmap you provide\n//           stbtt_GetCodepointBitmapBox()        -- how big the bitmap must be\n//\n//   Character advance/positioning\n//           stbtt_GetCodepointHMetrics()\n//           stbtt_GetFontVMetrics()\n//           stbtt_GetFontVMetricsOS2()\n//           stbtt_GetCodepointKernAdvance()\n//\n//   Starting with version 1.06, the rasterizer was replaced with a new,\n//   faster and generally-more-precise rasterizer. The new rasterizer more\n//   accurately measures pixel coverage for anti-aliasing, except in the case\n//   where multiple shapes overlap, in which case it overestimates the AA pixel\n//   coverage. Thus, anti-aliasing of intersecting shapes may look wrong. If\n//   this turns out to be a problem, you can re-enable the old rasterizer with\n//        #define STBTT_RASTERIZER_VERSION 1\n//   which will incur about a 15% speed hit.\n//\n// ADDITIONAL DOCUMENTATION\n//\n//   Immediately after this block comment are a series of sample programs.\n//\n//   After the sample programs is the \"header file\" section. This section\n//   includes documentation for each API function.\n//\n//   Some important concepts to understand to use this library:\n//\n//      Codepoint\n//         Characters are defined by unicode codepoints, e.g. 65 is\n//         uppercase A, 231 is lowercase c with a cedilla, 0x7e30 is\n//         the hiragana for \"ma\".\n//\n//      Glyph\n//         A visual character shape (every codepoint is rendered as\n//         some glyph)\n//\n//      Glyph index\n//         A font-specific integer ID representing a glyph\n//\n//      Baseline\n//         Glyph shapes are defined relative to a baseline, which is the\n//         bottom of uppercase characters. Characters extend both above\n//         and below the baseline.\n//\n//      Current Point\n//         As you draw text to the screen, you keep track of a \"current point\"\n//         which is the origin of each character. The current point's vertical\n//         position is the baseline. Even \"baked fonts\" use this model.\n//\n//      Vertical Font Metrics\n//         The vertical qualities of the font, used to vertically position\n//         and space the characters. See docs for stbtt_GetFontVMetrics.\n//\n//      Font Size in Pixels or Points\n//         The preferred interface for specifying font sizes in stb_truetype\n//         is to specify how tall the font's vertical extent should be in pixels.\n//         If that sounds good enough, skip the next paragraph.\n//\n//         Most font APIs instead use \"points\", which are a common typographic\n//         measurement for describing font size, defined as 72 points per inch.\n//         stb_truetype provides a point API for compatibility. However, true\n//         \"per inch\" conventions don't make much sense on computer displays\n//         since different monitors have different number of pixels per\n//         inch. For example, Windows traditionally uses a convention that\n//         there are 96 pixels per inch, thus making 'inch' measurements have\n//         nothing to do with inches, and thus effectively defining a point to\n//         be 1.333 pixels. Additionally, the TrueType font data provides\n//         an explicit scale factor to scale a given font's glyphs to points,\n//         but the author has observed that this scale factor is often wrong\n//         for non-commercial fonts, thus making fonts scaled in points\n//         according to the TrueType spec incoherently sized in practice.\n//\n// DETAILED USAGE:\n//\n//  Scale:\n//    Select how high you want the font to be, in points or pixels.\n//    Call ScaleForPixelHeight or ScaleForMappingEmToPixels to compute\n//    a scale factor SF that will be used by all other functions.\n//\n//  Baseline:\n//    You need to select a y-coordinate that is the baseline of where\n//    your text will appear. Call GetFontBoundingBox to get the baseline-relative\n//    bounding box for all characters. SF*-y0 will be the distance in pixels\n//    that the worst-case character could extend above the baseline, so if\n//    you want the top edge of characters to appear at the top of the\n//    screen where y=0, then you would set the baseline to SF*-y0.\n//\n//  Current point:\n//    Set the current point where the first character will appear. The\n//    first character could extend left of the current point; this is font\n//    dependent. You can either choose a current point that is the leftmost\n//    point and hope, or add some padding, or check the bounding box or\n//    left-side-bearing of the first character to be displayed and set\n//    the current point based on that.\n//\n//  Displaying a character:\n//    Compute the bounding box of the character. It will contain signed values\n//    relative to <current_point, baseline>. I.e. if it returns x0,y0,x1,y1,\n//    then the character should be displayed in the rectangle from\n//    <current_point+SF*x0, baseline+SF*y0> to <current_point+SF*x1,baseline+SF*y1).\n//\n//  Advancing for the next character:\n//    Call GlyphHMetrics, and compute 'current_point += SF * advance'.\n// \n//\n// ADVANCED USAGE\n//\n//   Quality:\n//\n//    - Use the functions with Subpixel at the end to allow your characters\n//      to have subpixel positioning. Since the font is anti-aliased, not\n//      hinted, this is very import for quality. (This is not possible with\n//      baked fonts.)\n//\n//    - Kerning is now supported, and if you're supporting subpixel rendering\n//      then kerning is worth using to give your text a polished look.\n//\n//   Performance:\n//\n//    - Convert Unicode codepoints to glyph indexes and operate on the glyphs;\n//      if you don't do this, stb_truetype is forced to do the conversion on\n//      every call.\n//\n//    - There are a lot of memory allocations. We should modify it to take\n//      a temp buffer and allocate from the temp buffer (without freeing),\n//      should help performance a lot.\n//\n// NOTES\n//\n//   The system uses the raw data found in the .ttf file without changing it\n//   and without building auxiliary data structures. This is a bit inefficient\n//   on little-endian systems (the data is big-endian), but assuming you're\n//   caching the bitmaps or glyph shapes this shouldn't be a big deal.\n//\n//   It appears to be very hard to programmatically determine what font a\n//   given file is in a general way. I provide an API for this, but I don't\n//   recommend it.\n//\n//\n// SOURCE STATISTICS (based on v0.6c, 2050 LOC)\n//\n//   Documentation & header file        520 LOC  \\___ 660 LOC documentation\n//   Sample code                        140 LOC  /\n//   Truetype parsing                   620 LOC  ---- 620 LOC TrueType\n//   Software rasterization             240 LOC  \\.\n//   Curve tessellation                 120 LOC   \\__ 550 LOC Bitmap creation\n//   Bitmap management                  100 LOC   /\n//   Baked bitmap interface              70 LOC  /\n//   Font name matching & access        150 LOC  ---- 150 \n//   C runtime library abstraction       60 LOC  ----  60\n//\n//\n// PERFORMANCE MEASUREMENTS FOR 1.06:\n//\n//                      32-bit     64-bit\n//   Previous release:  8.83 s     7.68 s\n//   Pool allocations:  7.72 s     6.34 s\n//   Inline sort     :  6.54 s     5.65 s\n//   New rasterizer  :  5.63 s     5.00 s\n\n//////////////////////////////////////////////////////////////////////////////\n//////////////////////////////////////////////////////////////////////////////\n////\n////  SAMPLE PROGRAMS\n////\n//\n//  Incomplete text-in-3d-api example, which draws quads properly aligned to be lossless\n//\n#if 0\n#define STB_TRUETYPE_IMPLEMENTATION  // force following include to generate implementation\n#include \"stb_truetype.h\"\n\nunsigned char ttf_buffer[1<<20];\nunsigned char temp_bitmap[512*512];\n\nstbtt_bakedchar cdata[96]; // ASCII 32..126 is 95 glyphs\nGLuint ftex;\n\nvoid my_stbtt_initfont(void)\n{\n   fread(ttf_buffer, 1, 1<<20, fopen(\"c:/windows/fonts/times.ttf\", \"rb\"));\n   stbtt_BakeFontBitmap(ttf_buffer,0, 32.0, temp_bitmap,512,512, 32,96, cdata); // no guarantee this fits!\n   // can free ttf_buffer at this point\n   glGenTextures(1, &ftex);\n   glBindTexture(GL_TEXTURE_2D, ftex);\n   glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);\n   // can free temp_bitmap at this point\n   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\n}\n\nvoid my_stbtt_print(float x, float y, char *text)\n{\n   // assume orthographic projection with units = screen pixels, origin at top left\n   glEnable(GL_TEXTURE_2D);\n   glBindTexture(GL_TEXTURE_2D, ftex);\n   glBegin(GL_QUADS);\n   while (*text) {\n      if (*text >= 32 && *text < 128) {\n         stbtt_aligned_quad q;\n         stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);//1=opengl & d3d10+,0=d3d9\n         glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0);\n         glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0);\n         glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1);\n         glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1);\n      }\n      ++text;\n   }\n   glEnd();\n}\n#endif\n//\n//\n//////////////////////////////////////////////////////////////////////////////\n//\n// Complete program (this compiles): get a single bitmap, print as ASCII art\n//\n#if 0\n#include <stdio.h>\n#define STB_TRUETYPE_IMPLEMENTATION  // force following include to generate implementation\n#include \"stb_truetype.h\"\n\nchar ttf_buffer[1<<25];\n\nint main(int argc, char **argv)\n{\n   stbtt_fontinfo font;\n   unsigned char *bitmap;\n   int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20);\n\n   fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : \"c:/windows/fonts/arialbd.ttf\", \"rb\"));\n\n   stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));\n   bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0);\n\n   for (j=0; j < h; ++j) {\n      for (i=0; i < w; ++i)\n         putchar(\" .:ioVM@\"[bitmap[j*w+i]>>5]);\n      putchar('\\n');\n   }\n   return 0;\n}\n#endif \n//\n// Output:\n//\n//     .ii.\n//    @@@@@@.\n//   V@Mio@@o\n//   :i.  V@V\n//     :oM@@M\n//   :@@@MM@M\n//   @@o  o@M\n//  :@@.  M@M\n//   @@@o@@@@\n//   :M@@V:@@.\n//  \n//////////////////////////////////////////////////////////////////////////////\n// \n// Complete program: print \"Hello World!\" banner, with bugs\n//\n#if 0\nchar buffer[24<<20];\nunsigned char screen[20][79];\n\nint main(int arg, char **argv)\n{\n   stbtt_fontinfo font;\n   int i,j,ascent,baseline,ch=0;\n   float scale, xpos=2; // leave a little padding in case the character extends left\n   char *text = \"Heljo World!\"; // intentionally misspelled to show 'lj' brokenness\n\n   fread(buffer, 1, 1000000, fopen(\"c:/windows/fonts/arialbd.ttf\", \"rb\"));\n   stbtt_InitFont(&font, buffer, 0);\n\n   scale = stbtt_ScaleForPixelHeight(&font, 15);\n   stbtt_GetFontVMetrics(&font, &ascent,0,0);\n   baseline = (int) (ascent*scale);\n\n   while (text[ch]) {\n      int advance,lsb,x0,y0,x1,y1;\n      float x_shift = xpos - (float) floor(xpos);\n      stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb);\n      stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale,scale,x_shift,0, &x0,&y0,&x1,&y1);\n      stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(int) xpos + x0], x1-x0,y1-y0, 79, scale,scale,x_shift,0, text[ch]);\n      // note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong\n      // because this API is really for baking character bitmaps into textures. if you want to render\n      // a sequence of characters, you really need to render each bitmap to a temp buffer, then\n      // \"alpha blend\" that into the working buffer\n      xpos += (advance * scale);\n      if (text[ch+1])\n         xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch],text[ch+1]);\n      ++ch;\n   }\n\n   for (j=0; j < 20; ++j) {\n      for (i=0; i < 78; ++i)\n         putchar(\" .:ioVM@\"[screen[j][i]>>5]);\n      putchar('\\n');\n   }\n\n   return 0;\n}\n#endif\n\n\n//////////////////////////////////////////////////////////////////////////////\n//////////////////////////////////////////////////////////////////////////////\n////\n////   INTEGRATION WITH YOUR CODEBASE\n////\n////   The following sections allow you to supply alternate definitions\n////   of C library functions used by stb_truetype, e.g. if you don't\n////   link with the C runtime library.\n\n#ifdef STB_TRUETYPE_IMPLEMENTATION\n   // #define your own (u)stbtt_int8/16/32 before including to override this\n   #ifndef stbtt_uint8\n   typedef unsigned char   stbtt_uint8;\n   typedef signed   char   stbtt_int8;\n   typedef unsigned short  stbtt_uint16;\n   typedef signed   short  stbtt_int16;\n   typedef unsigned int    stbtt_uint32;\n   typedef signed   int    stbtt_int32;\n   #endif\n\n   typedef char stbtt__check_size32[sizeof(stbtt_int32)==4 ? 1 : -1];\n   typedef char stbtt__check_size16[sizeof(stbtt_int16)==2 ? 1 : -1];\n\n   // e.g. #define your own STBTT_ifloor/STBTT_iceil() to avoid math.h\n   #ifndef STBTT_ifloor\n   #include <math.h>\n   #define STBTT_ifloor(x)   ((int) floor(x))\n   #define STBTT_iceil(x)    ((int) ceil(x))\n   #endif\n\n   #ifndef STBTT_sqrt\n   #include <math.h>\n   #define STBTT_sqrt(x)      sqrt(x)\n   #define STBTT_pow(x,y)     pow(x,y)\n   #endif\n\n   #ifndef STBTT_fmod\n   #include <math.h>\n   #define STBTT_fmod(x,y)    fmod(x,y)\n   #endif\n\n   #ifndef STBTT_cos\n   #include <math.h>\n   #define STBTT_cos(x)       cos(x)\n   #define STBTT_acos(x)      acos(x)\n   #endif\n\n   #ifndef STBTT_fabs\n   #include <math.h>\n   #define STBTT_fabs(x)      fabs(x)\n   #endif\n\n   // #define your own functions \"STBTT_malloc\" / \"STBTT_free\" to avoid malloc.h\n   #ifndef STBTT_malloc\n   #include <stdlib.h>\n   #define STBTT_malloc(x,u)  ((void)(u),malloc(x))\n   #define STBTT_free(x,u)    ((void)(u),free(x))\n   #endif\n\n   #ifndef STBTT_assert\n   #include <assert.h>\n   #define STBTT_assert(x)    assert(x)\n   #endif\n\n   #ifndef STBTT_strlen\n   #include <string.h>\n   #define STBTT_strlen(x)    strlen(x)\n   #endif\n\n   #ifndef STBTT_memcpy\n   #include <string.h>\n   #define STBTT_memcpy       memcpy\n   #define STBTT_memset       memset\n   #endif\n#endif\n\n///////////////////////////////////////////////////////////////////////////////\n///////////////////////////////////////////////////////////////////////////////\n////\n////   INTERFACE\n////\n////\n\n#ifndef __STB_INCLUDE_STB_TRUETYPE_H__\n#define __STB_INCLUDE_STB_TRUETYPE_H__\n\n#ifdef STBTT_STATIC\n#define STBTT_DEF static\n#else\n#define STBTT_DEF extern\n#endif\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// private structure\ntypedef struct\n{\n   unsigned char *data;\n   int cursor;\n   int size;\n} stbtt__buf;\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// TEXTURE BAKING API\n//\n// If you use this API, you only have to call two functions ever.\n//\n\ntypedef struct\n{\n   unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap\n   float xoff,yoff,xadvance;\n} stbtt_bakedchar;\n\nSTBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset,  // font location (use offset=0 for plain .ttf)\n                                float pixel_height,                     // height of font in pixels\n                                unsigned char *pixels, int pw, int ph,  // bitmap to be filled in\n                                int first_char, int num_chars,          // characters to bake\n                                stbtt_bakedchar *chardata);             // you allocate this, it's num_chars long\n// if return is positive, the first unused row of the bitmap\n// if return is negative, returns the negative of the number of characters that fit\n// if return is 0, no characters fit and no rows were used\n// This uses a very crappy packing.\n\ntypedef struct\n{\n   float x0,y0,s0,t0; // top-left\n   float x1,y1,s1,t1; // bottom-right\n} stbtt_aligned_quad;\n\nSTBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar *chardata, int pw, int ph,  // same data as above\n                               int char_index,             // character to display\n                               float *xpos, float *ypos,   // pointers to current position in screen pixel space\n                               stbtt_aligned_quad *q,      // output: quad to draw\n                               int opengl_fillrule);       // true if opengl fill rule; false if DX9 or earlier\n// Call GetBakedQuad with char_index = 'character - first_char', and it\n// creates the quad you need to draw and advances the current position.\n//\n// The coordinate system used assumes y increases downwards.\n//\n// Characters will extend both above and below the current position;\n// see discussion of \"BASELINE\" above.\n//\n// It's inefficient; you might want to c&p it and optimize it.\n\nSTBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char *fontdata, int index, float size, float *ascent, float *descent, float *lineGap);\n// Query the font vertical metrics without having to create a font first.\n\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// NEW TEXTURE BAKING API\n//\n// This provides options for packing multiple fonts into one atlas, not\n// perfectly but better than nothing.\n\ntypedef struct\n{\n   unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap\n   float xoff,yoff,xadvance;\n   float xoff2,yoff2;\n} stbtt_packedchar;\n\ntypedef struct stbtt_pack_context stbtt_pack_context;\ntypedef struct stbtt_fontinfo stbtt_fontinfo;\n#ifndef STB_RECT_PACK_VERSION\ntypedef struct stbrp_rect stbrp_rect;\n#endif\n\nSTBTT_DEF int  stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context);\n// Initializes a packing context stored in the passed-in stbtt_pack_context.\n// Future calls using this context will pack characters into the bitmap passed\n// in here: a 1-channel bitmap that is width * height. stride_in_bytes is\n// the distance from one row to the next (or 0 to mean they are packed tightly\n// together). \"padding\" is the amount of padding to leave between each\n// character (normally you want '1' for bitmaps you'll use as textures with\n// bilinear filtering).\n//\n// Returns 0 on failure, 1 on success.\n\nSTBTT_DEF void stbtt_PackEnd  (stbtt_pack_context *spc);\n// Cleans up the packing context and frees all memory.\n\n#define STBTT_POINT_SIZE(x)   (-(x))\n\nSTBTT_DEF int  stbtt_PackFontRange(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, float font_size,\n                                int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range);\n// Creates character bitmaps from the font_index'th font found in fontdata (use\n// font_index=0 if you don't know what that is). It creates num_chars_in_range\n// bitmaps for characters with unicode values starting at first_unicode_char_in_range\n// and increasing. Data for how to render them is stored in chardata_for_range;\n// pass these to stbtt_GetPackedQuad to get back renderable quads.\n//\n// font_size is the full height of the character from ascender to descender,\n// as computed by stbtt_ScaleForPixelHeight. To use a point size as computed\n// by stbtt_ScaleForMappingEmToPixels, wrap the point size in STBTT_POINT_SIZE()\n// and pass that result as 'font_size':\n//       ...,                  20 , ... // font max minus min y is 20 pixels tall\n//       ..., STBTT_POINT_SIZE(20), ... // 'M' is 20 pixels tall\n\ntypedef struct\n{\n   float font_size;\n   int first_unicode_codepoint_in_range;  // if non-zero, then the chars are continuous, and this is the first codepoint\n   int *array_of_unicode_codepoints;       // if non-zero, then this is an array of unicode codepoints\n   int num_chars;\n   stbtt_packedchar *chardata_for_range; // output\n   unsigned char h_oversample, v_oversample; // don't set these, they're used internally\n} stbtt_pack_range;\n\nSTBTT_DEF int  stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges);\n// Creates character bitmaps from multiple ranges of characters stored in\n// ranges. This will usually create a better-packed bitmap than multiple\n// calls to stbtt_PackFontRange. Note that you can call this multiple\n// times within a single PackBegin/PackEnd.\n\nSTBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample);\n// Oversampling a font increases the quality by allowing higher-quality subpixel\n// positioning, and is especially valuable at smaller text sizes.\n//\n// This function sets the amount of oversampling for all following calls to\n// stbtt_PackFontRange(s) or stbtt_PackFontRangesGatherRects for a given\n// pack context. The default (no oversampling) is achieved by h_oversample=1\n// and v_oversample=1. The total number of pixels required is\n// h_oversample*v_oversample larger than the default; for example, 2x2\n// oversampling requires 4x the storage of 1x1. For best results, render\n// oversampled textures with bilinear filtering. Look at the readme in\n// stb/tests/oversample for information about oversampled fonts\n//\n// To use with PackFontRangesGather etc., you must set it before calls\n// call to PackFontRangesGatherRects.\n\nSTBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip);\n// If skip != 0, this tells stb_truetype to skip any codepoints for which\n// there is no corresponding glyph. If skip=0, which is the default, then\n// codepoints without a glyph recived the font's \"missing character\" glyph,\n// typically an empty box by convention.\n\nSTBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar *chardata, int pw, int ph,  // same data as above\n                               int char_index,             // character to display\n                               float *xpos, float *ypos,   // pointers to current position in screen pixel space\n                               stbtt_aligned_quad *q,      // output: quad to draw\n                               int align_to_integer);\n\nSTBTT_DEF int  stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects);\nSTBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects);\nSTBTT_DEF int  stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects);\n// Calling these functions in sequence is roughly equivalent to calling\n// stbtt_PackFontRanges(). If you more control over the packing of multiple\n// fonts, or if you want to pack custom data into a font texture, take a look\n// at the source to of stbtt_PackFontRanges() and create a custom version \n// using these functions, e.g. call GatherRects multiple times,\n// building up a single array of rects, then call PackRects once,\n// then call RenderIntoRects repeatedly. This may result in a\n// better packing than calling PackFontRanges multiple times\n// (or it may not).\n\n// this is an opaque structure that you shouldn't mess with which holds\n// all the context needed from PackBegin to PackEnd.\nstruct stbtt_pack_context {\n   void *user_allocator_context;\n   void *pack_info;\n   int   width;\n   int   height;\n   int   stride_in_bytes;\n   int   padding;\n   int   skip_missing;\n   unsigned int   h_oversample, v_oversample;\n   unsigned char *pixels;\n   void  *nodes;\n};\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// FONT LOADING\n//\n//\n\nSTBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data);\n// This function will determine the number of fonts in a font file.  TrueType\n// collection (.ttc) files may contain multiple fonts, while TrueType font\n// (.ttf) files only contain one font. The number of fonts can be used for\n// indexing with the previous function where the index is between zero and one\n// less than the total fonts. If an error occurs, -1 is returned.\n\nSTBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index);\n// Each .ttf/.ttc file may have more than one font. Each font has a sequential\n// index number starting from 0. Call this function to get the font offset for\n// a given index; it returns -1 if the index is out of range. A regular .ttf\n// file will only define one font and it always be at offset 0, so it will\n// return '0' for index 0, and -1 for all other indices.\n\n// The following structure is defined publicly so you can declare one on\n// the stack or as a global or etc, but you should treat it as opaque.\nstruct stbtt_fontinfo\n{\n   void           * userdata;\n   unsigned char  * data;              // pointer to .ttf file\n   int              fontstart;         // offset of start of font\n\n   int numGlyphs;                     // number of glyphs, needed for range checking\n\n   int loca,head,glyf,hhea,hmtx,kern,gpos; // table locations as offset from start of .ttf\n   int index_map;                     // a cmap mapping for our chosen character encoding\n   int indexToLocFormat;              // format needed to map from glyph index to glyph\n\n   stbtt__buf cff;                    // cff font data\n   stbtt__buf charstrings;            // the charstring index\n   stbtt__buf gsubrs;                 // global charstring subroutines index\n   stbtt__buf subrs;                  // private charstring subroutines index\n   stbtt__buf fontdicts;              // array of font dicts\n   stbtt__buf fdselect;               // map from glyph to fontdict\n};\n\nSTBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset);\n// Given an offset into the file that defines a font, this function builds\n// the necessary cached info for the rest of the system. You must allocate\n// the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't\n// need to do anything special to free it, because the contents are pure\n// value data with no additional data structures. Returns 0 on failure.\n\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// CHARACTER TO GLYPH-INDEX CONVERSIOn\n\nSTBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint);\n// If you're going to perform multiple operations on the same character\n// and you want a speed-up, call this function with the character you're\n// going to process, then use glyph-based functions instead of the\n// codepoint-based functions.\n// Returns 0 if the character codepoint is not defined in the font.\n\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// CHARACTER PROPERTIES\n//\n\nSTBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels);\n// computes a scale factor to produce a font whose \"height\" is 'pixels' tall.\n// Height is measured as the distance from the highest ascender to the lowest\n// descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics\n// and computing:\n//       scale = pixels / (ascent - descent)\n// so if you prefer to measure height by the ascent only, use a similar calculation.\n\nSTBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels);\n// computes a scale factor to produce a font whose EM size is mapped to\n// 'pixels' tall. This is probably what traditional APIs compute, but\n// I'm not positive.\n\nSTBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap);\n// ascent is the coordinate above the baseline the font extends; descent\n// is the coordinate below the baseline the font extends (i.e. it is typically negative)\n// lineGap is the spacing between one row's descent and the next row's ascent...\n// so you should advance the vertical position by \"*ascent - *descent + *lineGap\"\n//   these are expressed in unscaled coordinates, so you must multiply by\n//   the scale factor for a given size\n\nSTBTT_DEF int  stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap);\n// analogous to GetFontVMetrics, but returns the \"typographic\" values from the OS/2\n// table (specific to MS/Windows TTF files).\n//\n// Returns 1 on success (table present), 0 on failure.\n\nSTBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1);\n// the bounding box around all possible characters\n\nSTBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing);\n// leftSideBearing is the offset from the current horizontal position to the left edge of the character\n// advanceWidth is the offset from the current horizontal position to the next horizontal position\n//   these are expressed in unscaled coordinates\n\nSTBTT_DEF int  stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2);\n// an additional amount to add to the 'advance' value between ch1 and ch2\n\nSTBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1);\n// Gets the bounding box of the visible part of the glyph, in unscaled coordinates\n\nSTBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing);\nSTBTT_DEF int  stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2);\nSTBTT_DEF int  stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);\n// as above, but takes one or more glyph indices for greater efficiency\n\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// GLYPH SHAPES (you probably don't need these, but they have to go before\n// the bitmaps for C declaration-order reasons)\n//\n\n#ifndef STBTT_vmove // you can predefine these to use different values (but why?)\n   enum {\n      STBTT_vmove=1,\n      STBTT_vline,\n      STBTT_vcurve,\n      STBTT_vcubic\n   };\n#endif\n\n#ifndef stbtt_vertex // you can predefine this to use different values\n                   // (we share this with other code at RAD)\n   #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file\n   typedef struct\n   {\n      stbtt_vertex_type x,y,cx,cy,cx1,cy1;\n      unsigned char type,padding;\n   } stbtt_vertex;\n#endif\n\nSTBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index);\n// returns non-zero if nothing is drawn for this glyph\n\nSTBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices);\nSTBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices);\n// returns # of vertices and fills *vertices with the pointer to them\n//   these are expressed in \"unscaled\" coordinates\n//\n// The shape is a series of contours. Each one starts with\n// a STBTT_moveto, then consists of a series of mixed\n// STBTT_lineto and STBTT_curveto segments. A lineto\n// draws a line from previous endpoint to its x,y; a curveto\n// draws a quadratic bezier from previous endpoint to\n// its x,y, using cx,cy as the bezier control point.\n\nSTBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices);\n// frees the data allocated above\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// BITMAP RENDERING\n//\n\nSTBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata);\n// frees the bitmap allocated below\n\nSTBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff);\n// allocates a large-enough single-channel 8bpp bitmap and renders the\n// specified character/glyph at the specified scale into it, with\n// antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque).\n// *width & *height are filled out with the width & height of the bitmap,\n// which is stored left-to-right, top-to-bottom.\n//\n// xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap\n\nSTBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff);\n// the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel\n// shift for the character\n\nSTBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint);\n// the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap\n// in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap\n// is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the\n// width and height and positioning info for it first.\n\nSTBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint);\n// same as stbtt_MakeCodepointBitmap, but you can specify a subpixel\n// shift for the character\n\nSTBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int codepoint);\n// same as stbtt_MakeCodepointBitmapSubpixel, but prefiltering\n// is performed (see stbtt_PackSetOversampling)\n\nSTBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);\n// get the bbox of the bitmap centered around the glyph origin; so the\n// bitmap width is ix1-ix0, height is iy1-iy0, and location to place\n// the bitmap top left is (leftSideBearing*scale,iy0).\n// (Note that the bitmap uses y-increases-down, but the shape uses\n// y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.)\n\nSTBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1);\n// same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel\n// shift for the character\n\n// the following functions are equivalent to the above functions, but operate\n// on glyph indices instead of Unicode codepoints (for efficiency)\nSTBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff);\nSTBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff);\nSTBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph);\nSTBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph);\nSTBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int glyph);\nSTBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1);\nSTBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1);\n\n\n// @TODO: don't expose this structure\ntypedef struct\n{\n   int w,h,stride;\n   unsigned char *pixels;\n} stbtt__bitmap;\n\n// rasterize a shape with quadratic beziers into a bitmap\nSTBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result,        // 1-channel bitmap to draw into\n                               float flatness_in_pixels,     // allowable error of curve in pixels\n                               stbtt_vertex *vertices,       // array of vertices defining shape\n                               int num_verts,                // number of vertices in above array\n                               float scale_x, float scale_y, // scale applied to input vertices\n                               float shift_x, float shift_y, // translation applied to input vertices\n                               int x_off, int y_off,         // another translation applied to input\n                               int invert,                   // if non-zero, vertically flip shape\n                               void *userdata);              // context for to STBTT_MALLOC\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// Signed Distance Function (or Field) rendering\n\nSTBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata);\n// frees the SDF bitmap allocated below\n\nSTBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float scale, int glyph, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff);\nSTBTT_DEF unsigned char * stbtt_GetCodepointSDF(const stbtt_fontinfo *info, float scale, int codepoint, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff);\n// These functions compute a discretized SDF field for a single character, suitable for storing\n// in a single-channel texture, sampling with bilinear filtering, and testing against\n// larger than some threshold to produce scalable fonts.\n//        info              --  the font\n//        scale             --  controls the size of the resulting SDF bitmap, same as it would be creating a regular bitmap\n//        glyph/codepoint   --  the character to generate the SDF for\n//        padding           --  extra \"pixels\" around the character which are filled with the distance to the character (not 0),\n//                                 which allows effects like bit outlines\n//        onedge_value      --  value 0-255 to test the SDF against to reconstruct the character (i.e. the isocontour of the character)\n//        pixel_dist_scale  --  what value the SDF should increase by when moving one SDF \"pixel\" away from the edge (on the 0..255 scale)\n//                                 if positive, > onedge_value is inside; if negative, < onedge_value is inside\n//        width,height      --  output height & width of the SDF bitmap (including padding)\n//        xoff,yoff         --  output origin of the character\n//        return value      --  a 2D array of bytes 0..255, width*height in size\n//\n// pixel_dist_scale & onedge_value are a scale & bias that allows you to make\n// optimal use of the limited 0..255 for your application, trading off precision\n// and special effects. SDF values outside the range 0..255 are clamped to 0..255.\n//\n// Example:\n//      scale = stbtt_ScaleForPixelHeight(22)\n//      padding = 5\n//      onedge_value = 180\n//      pixel_dist_scale = 180/5.0 = 36.0\n//\n//      This will create an SDF bitmap in which the character is about 22 pixels\n//      high but the whole bitmap is about 22+5+5=32 pixels high. To produce a filled\n//      shape, sample the SDF at each pixel and fill the pixel if the SDF value\n//      is greater than or equal to 180/255. (You'll actually want to antialias,\n//      which is beyond the scope of this example.) Additionally, you can compute\n//      offset outlines (e.g. to stroke the character border inside & outside,\n//      or only outside). For example, to fill outside the character up to 3 SDF\n//      pixels, you would compare against (180-36.0*3)/255 = 72/255. The above\n//      choice of variables maps a range from 5 pixels outside the shape to\n//      2 pixels inside the shape to 0..255; this is intended primarily for apply\n//      outside effects only (the interior range is needed to allow proper\n//      antialiasing of the font at *smaller* sizes)\n//\n// The function computes the SDF analytically at each SDF pixel, not by e.g.\n// building a higher-res bitmap and approximating it. In theory the quality\n// should be as high as possible for an SDF of this size & representation, but\n// unclear if this is true in practice (perhaps building a higher-res bitmap\n// and computing from that can allow drop-out prevention).\n//\n// The algorithm has not been optimized at all, so expect it to be slow\n// if computing lots of characters or very large sizes. \n\n\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// Finding the right font...\n//\n// You should really just solve this offline, keep your own tables\n// of what font is what, and don't try to get it out of the .ttf file.\n// That's because getting it out of the .ttf file is really hard, because\n// the names in the file can appear in many possible encodings, in many\n// possible languages, and e.g. if you need a case-insensitive comparison,\n// the details of that depend on the encoding & language in a complex way\n// (actually underspecified in truetype, but also gigantic).\n//\n// But you can use the provided functions in two possible ways:\n//     stbtt_FindMatchingFont() will use *case-sensitive* comparisons on\n//             unicode-encoded names to try to find the font you want;\n//             you can run this before calling stbtt_InitFont()\n//\n//     stbtt_GetFontNameString() lets you get any of the various strings\n//             from the file yourself and do your own comparisons on them.\n//             You have to have called stbtt_InitFont() first.\n\n\nSTBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags);\n// returns the offset (not index) of the font that matches, or -1 if none\n//   if you use STBTT_MACSTYLE_DONTCARE, use a font name like \"Arial Bold\".\n//   if you use any other flag, use a font name like \"Arial\"; this checks\n//     the 'macStyle' header field; i don't know if fonts set this consistently\n#define STBTT_MACSTYLE_DONTCARE     0\n#define STBTT_MACSTYLE_BOLD         1\n#define STBTT_MACSTYLE_ITALIC       2\n#define STBTT_MACSTYLE_UNDERSCORE   4\n#define STBTT_MACSTYLE_NONE         8   // <= not same as 0, this makes us check the bitfield is 0\n\nSTBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2);\n// returns 1/0 whether the first string interpreted as utf8 is identical to\n// the second string interpreted as big-endian utf16... useful for strings from next func\n\nSTBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID);\n// returns the string (which may be big-endian double byte, e.g. for unicode)\n// and puts the length in bytes in *length.\n//\n// some of the values for the IDs are below; for more see the truetype spec:\n//     http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html\n//     http://www.microsoft.com/typography/otspec/name.htm\n\nenum { // platformID\n   STBTT_PLATFORM_ID_UNICODE   =0,\n   STBTT_PLATFORM_ID_MAC       =1,\n   STBTT_PLATFORM_ID_ISO       =2,\n   STBTT_PLATFORM_ID_MICROSOFT =3\n};\n\nenum { // encodingID for STBTT_PLATFORM_ID_UNICODE\n   STBTT_UNICODE_EID_UNICODE_1_0    =0,\n   STBTT_UNICODE_EID_UNICODE_1_1    =1,\n   STBTT_UNICODE_EID_ISO_10646      =2,\n   STBTT_UNICODE_EID_UNICODE_2_0_BMP=3,\n   STBTT_UNICODE_EID_UNICODE_2_0_FULL=4\n};\n\nenum { // encodingID for STBTT_PLATFORM_ID_MICROSOFT\n   STBTT_MS_EID_SYMBOL        =0,\n   STBTT_MS_EID_UNICODE_BMP   =1,\n   STBTT_MS_EID_SHIFTJIS      =2,\n   STBTT_MS_EID_UNICODE_FULL  =10\n};\n\nenum { // encodingID for STBTT_PLATFORM_ID_MAC; same as Script Manager codes\n   STBTT_MAC_EID_ROMAN        =0,   STBTT_MAC_EID_ARABIC       =4,\n   STBTT_MAC_EID_JAPANESE     =1,   STBTT_MAC_EID_HEBREW       =5,\n   STBTT_MAC_EID_CHINESE_TRAD =2,   STBTT_MAC_EID_GREEK        =6,\n   STBTT_MAC_EID_KOREAN       =3,   STBTT_MAC_EID_RUSSIAN      =7\n};\n\nenum { // languageID for STBTT_PLATFORM_ID_MICROSOFT; same as LCID...\n       // problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs\n   STBTT_MS_LANG_ENGLISH     =0x0409,   STBTT_MS_LANG_ITALIAN     =0x0410,\n   STBTT_MS_LANG_CHINESE     =0x0804,   STBTT_MS_LANG_JAPANESE    =0x0411,\n   STBTT_MS_LANG_DUTCH       =0x0413,   STBTT_MS_LANG_KOREAN      =0x0412,\n   STBTT_MS_LANG_FRENCH      =0x040c,   STBTT_MS_LANG_RUSSIAN     =0x0419,\n   STBTT_MS_LANG_GERMAN      =0x0407,   STBTT_MS_LANG_SPANISH     =0x0409,\n   STBTT_MS_LANG_HEBREW      =0x040d,   STBTT_MS_LANG_SWEDISH     =0x041D\n};\n\nenum { // languageID for STBTT_PLATFORM_ID_MAC\n   STBTT_MAC_LANG_ENGLISH      =0 ,   STBTT_MAC_LANG_JAPANESE     =11,\n   STBTT_MAC_LANG_ARABIC       =12,   STBTT_MAC_LANG_KOREAN       =23,\n   STBTT_MAC_LANG_DUTCH        =4 ,   STBTT_MAC_LANG_RUSSIAN      =32,\n   STBTT_MAC_LANG_FRENCH       =1 ,   STBTT_MAC_LANG_SPANISH      =6 ,\n   STBTT_MAC_LANG_GERMAN       =2 ,   STBTT_MAC_LANG_SWEDISH      =5 ,\n   STBTT_MAC_LANG_HEBREW       =10,   STBTT_MAC_LANG_CHINESE_SIMPLIFIED =33,\n   STBTT_MAC_LANG_ITALIAN      =3 ,   STBTT_MAC_LANG_CHINESE_TRAD =19\n};\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif // __STB_INCLUDE_STB_TRUETYPE_H__\n\n///////////////////////////////////////////////////////////////////////////////\n///////////////////////////////////////////////////////////////////////////////\n////\n////   IMPLEMENTATION\n////\n////\n\n#ifdef STB_TRUETYPE_IMPLEMENTATION\n\n#ifndef STBTT_MAX_OVERSAMPLE\n#define STBTT_MAX_OVERSAMPLE   8\n#endif\n\n#if STBTT_MAX_OVERSAMPLE > 255\n#error \"STBTT_MAX_OVERSAMPLE cannot be > 255\"\n#endif\n\ntypedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1];\n\n#ifndef STBTT_RASTERIZER_VERSION\n#define STBTT_RASTERIZER_VERSION 2\n#endif\n\n#ifdef _MSC_VER\n#define STBTT__NOTUSED(v)  (void)(v)\n#else\n#define STBTT__NOTUSED(v)  (void)sizeof(v)\n#endif\n\n//////////////////////////////////////////////////////////////////////////\n//\n// stbtt__buf helpers to parse data from file\n//\n\nstatic stbtt_uint8 stbtt__buf_get8(stbtt__buf *b)\n{\n   if (b->cursor >= b->size)\n      return 0;\n   return b->data[b->cursor++];\n}\n\nstatic stbtt_uint8 stbtt__buf_peek8(stbtt__buf *b)\n{\n   if (b->cursor >= b->size)\n      return 0;\n   return b->data[b->cursor];\n}\n\nstatic void stbtt__buf_seek(stbtt__buf *b, int o)\n{\n   STBTT_assert(!(o > b->size || o < 0));\n   b->cursor = (o > b->size || o < 0) ? b->size : o;\n}\n\nstatic void stbtt__buf_skip(stbtt__buf *b, int o)\n{\n   stbtt__buf_seek(b, b->cursor + o);\n}\n\nstatic stbtt_uint32 stbtt__buf_get(stbtt__buf *b, int n)\n{\n   stbtt_uint32 v = 0;\n   int i;\n   STBTT_assert(n >= 1 && n <= 4);\n   for (i = 0; i < n; i++)\n      v = (v << 8) | stbtt__buf_get8(b);\n   return v;\n}\n\nstatic stbtt__buf stbtt__new_buf(const void *p, size_t size)\n{\n   stbtt__buf r;\n   STBTT_assert(size < 0x40000000);\n   r.data = (stbtt_uint8*) p;\n   r.size = (int) size;\n   r.cursor = 0;\n   return r;\n}\n\n#define stbtt__buf_get16(b)  stbtt__buf_get((b), 2)\n#define stbtt__buf_get32(b)  stbtt__buf_get((b), 4)\n\nstatic stbtt__buf stbtt__buf_range(const stbtt__buf *b, int o, int s)\n{\n   stbtt__buf r = stbtt__new_buf(NULL, 0);\n   if (o < 0 || s < 0 || o > b->size || s > b->size - o) return r;\n   r.data = b->data + o;\n   r.size = s;\n   return r;\n}\n\nstatic stbtt__buf stbtt__cff_get_index(stbtt__buf *b)\n{\n   int count, start, offsize;\n   start = b->cursor;\n   count = stbtt__buf_get16(b);\n   if (count) {\n      offsize = stbtt__buf_get8(b);\n      STBTT_assert(offsize >= 1 && offsize <= 4);\n      stbtt__buf_skip(b, offsize * count);\n      stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1);\n   }\n   return stbtt__buf_range(b, start, b->cursor - start);\n}\n\nstatic stbtt_uint32 stbtt__cff_int(stbtt__buf *b)\n{\n   int b0 = stbtt__buf_get8(b);\n   if (b0 >= 32 && b0 <= 246)       return b0 - 139;\n   else if (b0 >= 247 && b0 <= 250) return (b0 - 247)*256 + stbtt__buf_get8(b) + 108;\n   else if (b0 >= 251 && b0 <= 254) return -(b0 - 251)*256 - stbtt__buf_get8(b) - 108;\n   else if (b0 == 28)               return stbtt__buf_get16(b);\n   else if (b0 == 29)               return stbtt__buf_get32(b);\n   STBTT_assert(0);\n   return 0;\n}\n\nstatic void stbtt__cff_skip_operand(stbtt__buf *b) {\n   int v, b0 = stbtt__buf_peek8(b);\n   STBTT_assert(b0 >= 28);\n   if (b0 == 30) {\n      stbtt__buf_skip(b, 1);\n      while (b->cursor < b->size) {\n         v = stbtt__buf_get8(b);\n         if ((v & 0xF) == 0xF || (v >> 4) == 0xF)\n            break;\n      }\n   } else {\n      stbtt__cff_int(b);\n   }\n}\n\nstatic stbtt__buf stbtt__dict_get(stbtt__buf *b, int key)\n{\n   stbtt__buf_seek(b, 0);\n   while (b->cursor < b->size) {\n      int start = b->cursor, end, op;\n      while (stbtt__buf_peek8(b) >= 28)\n         stbtt__cff_skip_operand(b);\n      end = b->cursor;\n      op = stbtt__buf_get8(b);\n      if (op == 12)  op = stbtt__buf_get8(b) | 0x100;\n      if (op == key) return stbtt__buf_range(b, start, end-start);\n   }\n   return stbtt__buf_range(b, 0, 0);\n}\n\nstatic void stbtt__dict_get_ints(stbtt__buf *b, int key, int outcount, stbtt_uint32 *out)\n{\n   int i;\n   stbtt__buf operands = stbtt__dict_get(b, key);\n   for (i = 0; i < outcount && operands.cursor < operands.size; i++)\n      out[i] = stbtt__cff_int(&operands);\n}\n\nstatic int stbtt__cff_index_count(stbtt__buf *b)\n{\n   stbtt__buf_seek(b, 0);\n   return stbtt__buf_get16(b);\n}\n\nstatic stbtt__buf stbtt__cff_index_get(stbtt__buf b, int i)\n{\n   int count, offsize, start, end;\n   stbtt__buf_seek(&b, 0);\n   count = stbtt__buf_get16(&b);\n   offsize = stbtt__buf_get8(&b);\n   STBTT_assert(i >= 0 && i < count);\n   STBTT_assert(offsize >= 1 && offsize <= 4);\n   stbtt__buf_skip(&b, i*offsize);\n   start = stbtt__buf_get(&b, offsize);\n   end = stbtt__buf_get(&b, offsize);\n   return stbtt__buf_range(&b, 2+(count+1)*offsize+start, end - start);\n}\n\n//////////////////////////////////////////////////////////////////////////\n//\n// accessors to parse data from file\n//\n\n// on platforms that don't allow misaligned reads, if we want to allow\n// truetype fonts that aren't padded to alignment, define ALLOW_UNALIGNED_TRUETYPE\n\n#define ttBYTE(p)     (* (stbtt_uint8 *) (p))\n#define ttCHAR(p)     (* (stbtt_int8 *) (p))\n#define ttFixed(p)    ttLONG(p)\n\nstatic stbtt_uint16 ttUSHORT(stbtt_uint8 *p) { return p[0]*256 + p[1]; }\nstatic stbtt_int16 ttSHORT(stbtt_uint8 *p)   { return p[0]*256 + p[1]; }\nstatic stbtt_uint32 ttULONG(stbtt_uint8 *p)  { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }\nstatic stbtt_int32 ttLONG(stbtt_uint8 *p)    { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }\n\n#define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))\n#define stbtt_tag(p,str)           stbtt_tag4(p,str[0],str[1],str[2],str[3])\n\nstatic int stbtt__isfont(stbtt_uint8 *font)\n{\n   // check the version number\n   if (stbtt_tag4(font, '1',0,0,0))  return 1; // TrueType 1\n   if (stbtt_tag(font, \"typ1\"))   return 1; // TrueType with type 1 font -- we don't support this!\n   if (stbtt_tag(font, \"OTTO\"))   return 1; // OpenType with CFF\n   if (stbtt_tag4(font, 0,1,0,0)) return 1; // OpenType 1.0\n   if (stbtt_tag(font, \"true\"))   return 1; // Apple specification for TrueType fonts\n   return 0;\n}\n\n// @OPTIMIZE: binary search\nstatic stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart, const char *tag)\n{\n   stbtt_int32 num_tables = ttUSHORT(data+fontstart+4);\n   stbtt_uint32 tabledir = fontstart + 12;\n   stbtt_int32 i;\n   for (i=0; i < num_tables; ++i) {\n      stbtt_uint32 loc = tabledir + 16*i;\n      if (stbtt_tag(data+loc+0, tag))\n         return ttULONG(data+loc+8);\n   }\n   return 0;\n}\n\nstatic int stbtt_GetFontOffsetForIndex_internal(unsigned char *font_collection, int index)\n{\n   // if it's just a font, there's only one valid index\n   if (stbtt__isfont(font_collection))\n      return index == 0 ? 0 : -1;\n\n   // check if it's a TTC\n   if (stbtt_tag(font_collection, \"ttcf\")) {\n      // version 1?\n      if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {\n         stbtt_int32 n = ttLONG(font_collection+8);\n         if (index >= n)\n            return -1;\n         return ttULONG(font_collection+12+index*4);\n      }\n   }\n   return -1;\n}\n\nstatic int stbtt_GetNumberOfFonts_internal(unsigned char *font_collection)\n{\n   // if it's just a font, there's only one valid font\n   if (stbtt__isfont(font_collection))\n      return 1;\n\n   // check if it's a TTC\n   if (stbtt_tag(font_collection, \"ttcf\")) {\n      // version 1?\n      if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {\n         return ttLONG(font_collection+8);\n      }\n   }\n   return 0;\n}\n\nstatic stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict)\n{\n   stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 };\n   stbtt__buf pdict;\n   stbtt__dict_get_ints(&fontdict, 18, 2, private_loc);\n   if (!private_loc[1] || !private_loc[0]) return stbtt__new_buf(NULL, 0);\n   pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]);\n   stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff);\n   if (!subrsoff) return stbtt__new_buf(NULL, 0);\n   stbtt__buf_seek(&cff, private_loc[1]+subrsoff);\n   return stbtt__cff_get_index(&cff);\n}\n\nstatic int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart)\n{\n   stbtt_uint32 cmap, t;\n   stbtt_int32 i,numTables;\n\n   info->data = data;\n   info->fontstart = fontstart;\n   info->cff = stbtt__new_buf(NULL, 0);\n\n   cmap = stbtt__find_table(data, fontstart, \"cmap\");       // required\n   info->loca = stbtt__find_table(data, fontstart, \"loca\"); // required\n   info->head = stbtt__find_table(data, fontstart, \"head\"); // required\n   info->glyf = stbtt__find_table(data, fontstart, \"glyf\"); // required\n   info->hhea = stbtt__find_table(data, fontstart, \"hhea\"); // required\n   info->hmtx = stbtt__find_table(data, fontstart, \"hmtx\"); // required\n   info->kern = stbtt__find_table(data, fontstart, \"kern\"); // not required\n   info->gpos = stbtt__find_table(data, fontstart, \"GPOS\"); // not required\n\n   if (!cmap || !info->head || !info->hhea || !info->hmtx)\n      return 0;\n   if (info->glyf) {\n      // required for truetype\n      if (!info->loca) return 0;\n   } else {\n      // initialization for CFF / Type2 fonts (OTF)\n      stbtt__buf b, topdict, topdictidx;\n      stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;\n      stbtt_uint32 cff;\n\n      cff = stbtt__find_table(data, fontstart, \"CFF \");\n      if (!cff) return 0;\n\n      info->fontdicts = stbtt__new_buf(NULL, 0);\n      info->fdselect = stbtt__new_buf(NULL, 0);\n\n      // @TODO this should use size from table (not 512MB)\n      info->cff = stbtt__new_buf(data+cff, 512*1024*1024);\n      b = info->cff;\n\n      // read the header\n      stbtt__buf_skip(&b, 2);\n      stbtt__buf_seek(&b, stbtt__buf_get8(&b)); // hdrsize\n\n      // @TODO the name INDEX could list multiple fonts,\n      // but we just use the first one.\n      stbtt__cff_get_index(&b);  // name INDEX\n      topdictidx = stbtt__cff_get_index(&b);\n      topdict = stbtt__cff_index_get(topdictidx, 0);\n      stbtt__cff_get_index(&b);  // string INDEX\n      info->gsubrs = stbtt__cff_get_index(&b);\n\n      stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);\n      stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);\n      stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);\n      stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);\n      info->subrs = stbtt__get_subrs(b, topdict);\n\n      // we only support Type 2 charstrings\n      if (cstype != 2) return 0;\n      if (charstrings == 0) return 0;\n\n      if (fdarrayoff) {\n         // looks like a CID font\n         if (!fdselectoff) return 0;\n         stbtt__buf_seek(&b, fdarrayoff);\n         info->fontdicts = stbtt__cff_get_index(&b);\n         info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size-fdselectoff);\n      }\n\n      stbtt__buf_seek(&b, charstrings);\n      info->charstrings = stbtt__cff_get_index(&b);\n   }\n\n   t = stbtt__find_table(data, fontstart, \"maxp\");\n   if (t)\n      info->numGlyphs = ttUSHORT(data+t+4);\n   else\n      info->numGlyphs = 0xffff;\n\n   // find a cmap encoding table we understand *now* to avoid searching\n   // later. (todo: could make this installable)\n   // the same regardless of glyph.\n   numTables = ttUSHORT(data + cmap + 2);\n   info->index_map = 0;\n   for (i=0; i < numTables; ++i) {\n      stbtt_uint32 encoding_record = cmap + 4 + 8 * i;\n      // find an encoding we understand:\n      switch(ttUSHORT(data+encoding_record)) {\n         case STBTT_PLATFORM_ID_MICROSOFT:\n            switch (ttUSHORT(data+encoding_record+2)) {\n               case STBTT_MS_EID_UNICODE_BMP:\n               case STBTT_MS_EID_UNICODE_FULL:\n                  // MS/Unicode\n                  info->index_map = cmap + ttULONG(data+encoding_record+4);\n                  break;\n            }\n            break;\n        case STBTT_PLATFORM_ID_UNICODE:\n            // Mac/iOS has these\n            // all the encodingIDs are unicode, so we don't bother to check it\n            info->index_map = cmap + ttULONG(data+encoding_record+4);\n            break;\n      }\n   }\n   if (info->index_map == 0)\n      return 0;\n\n   info->indexToLocFormat = ttUSHORT(data+info->head + 50);\n   return 1;\n}\n\nSTBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint)\n{\n   stbtt_uint8 *data = info->data;\n   stbtt_uint32 index_map = info->index_map;\n\n   stbtt_uint16 format = ttUSHORT(data + index_map + 0);\n   if (format == 0) { // apple byte encoding\n      stbtt_int32 bytes = ttUSHORT(data + index_map + 2);\n      if (unicode_codepoint < bytes-6)\n         return ttBYTE(data + index_map + 6 + unicode_codepoint);\n      return 0;\n   } else if (format == 6) {\n      stbtt_uint32 first = ttUSHORT(data + index_map + 6);\n      stbtt_uint32 count = ttUSHORT(data + index_map + 8);\n      if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count)\n         return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);\n      return 0;\n   } else if (format == 2) {\n      STBTT_assert(0); // @TODO: high-byte mapping for japanese/chinese/korean\n      return 0;\n   } else if (format == 4) { // standard mapping for windows fonts: binary search collection of ranges\n      stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1;\n      stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1;\n      stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10);\n      stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1;\n\n      // do a binary search of the segments\n      stbtt_uint32 endCount = index_map + 14;\n      stbtt_uint32 search = endCount;\n\n      if (unicode_codepoint > 0xffff)\n         return 0;\n\n      // they lie from endCount .. endCount + segCount\n      // but searchRange is the nearest power of two, so...\n      if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2))\n         search += rangeShift*2;\n\n      // now decrement to bias correctly to find smallest\n      search -= 2;\n      while (entrySelector) {\n         stbtt_uint16 end;\n         searchRange >>= 1;\n         end = ttUSHORT(data + search + searchRange*2);\n         if (unicode_codepoint > end)\n            search += searchRange*2;\n         --entrySelector;\n      }\n      search += 2;\n\n      {\n         stbtt_uint16 offset, start;\n         stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1);\n\n         STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item));\n         start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);\n         if (unicode_codepoint < start)\n            return 0;\n\n         offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);\n         if (offset == 0)\n            return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));\n\n         return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);\n      }\n   } else if (format == 12 || format == 13) {\n      stbtt_uint32 ngroups = ttULONG(data+index_map+12);\n      stbtt_int32 low,high;\n      low = 0; high = (stbtt_int32)ngroups;\n      // Binary search the right group.\n      while (low < high) {\n         stbtt_int32 mid = low + ((high-low) >> 1); // rounds down, so low <= mid < high\n         stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12);\n         stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4);\n         if ((stbtt_uint32) unicode_codepoint < start_char)\n            high = mid;\n         else if ((stbtt_uint32) unicode_codepoint > end_char)\n            low = mid+1;\n         else {\n            stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8);\n            if (format == 12)\n               return start_glyph + unicode_codepoint-start_char;\n            else // format == 13\n               return start_glyph;\n         }\n      }\n      return 0; // not found\n   }\n   // @TODO\n   STBTT_assert(0);\n   return 0;\n}\n\nSTBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices)\n{\n   return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);\n}\n\nstatic void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy)\n{\n   v->type = type;\n   v->x = (stbtt_int16) x;\n   v->y = (stbtt_int16) y;\n   v->cx = (stbtt_int16) cx;\n   v->cy = (stbtt_int16) cy;\n}\n\nstatic int stbtt__GetGlyfOffset(const stbtt_fontinfo *info, int glyph_index)\n{\n   int g1,g2;\n\n   STBTT_assert(!info->cff.size);\n\n   if (glyph_index >= info->numGlyphs) return -1; // glyph index out of range\n   if (info->indexToLocFormat >= 2)    return -1; // unknown index->glyph map format\n\n   if (info->indexToLocFormat == 0) {\n      g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;\n      g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;\n   } else {\n      g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4);\n      g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4);\n   }\n\n   return g1==g2 ? -1 : g1; // if length is 0, return -1\n}\n\nstatic int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);\n\nSTBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)\n{\n   if (info->cff.size) {\n      stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);\n   } else {\n      int g = stbtt__GetGlyfOffset(info, glyph_index);\n      if (g < 0) return 0;\n\n      if (x0) *x0 = ttSHORT(info->data + g + 2);\n      if (y0) *y0 = ttSHORT(info->data + g + 4);\n      if (x1) *x1 = ttSHORT(info->data + g + 6);\n      if (y1) *y1 = ttSHORT(info->data + g + 8);\n   }\n   return 1;\n}\n\nSTBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1)\n{\n   return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1);\n}\n\nSTBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index)\n{\n   stbtt_int16 numberOfContours;\n   int g;\n   if (info->cff.size)\n      return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0;\n   g = stbtt__GetGlyfOffset(info, glyph_index);\n   if (g < 0) return 1;\n   numberOfContours = ttSHORT(info->data + g);\n   return numberOfContours == 0;\n}\n\nstatic int stbtt__close_shape(stbtt_vertex *vertices, int num_vertices, int was_off, int start_off,\n    stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)\n{\n   if (start_off) {\n      if (was_off)\n         stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);\n      stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx,sy,scx,scy);\n   } else {\n      if (was_off)\n         stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve,sx,sy,cx,cy);\n      else\n         stbtt_setvertex(&vertices[num_vertices++], STBTT_vline,sx,sy,0,0);\n   }\n   return num_vertices;\n}\n\nstatic int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)\n{\n   stbtt_int16 numberOfContours;\n   stbtt_uint8 *endPtsOfContours;\n   stbtt_uint8 *data = info->data;\n   stbtt_vertex *vertices=0;\n   int num_vertices=0;\n   int g = stbtt__GetGlyfOffset(info, glyph_index);\n\n   *pvertices = NULL;\n\n   if (g < 0) return 0;\n\n   numberOfContours = ttSHORT(data + g);\n\n   if (numberOfContours > 0) {\n      stbtt_uint8 flags=0,flagcount;\n      stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;\n      stbtt_int32 x,y,cx,cy,sx,sy, scx,scy;\n      stbtt_uint8 *points;\n      endPtsOfContours = (data + g + 10);\n      ins = ttUSHORT(data + g + 10 + numberOfContours * 2);\n      points = data + g + 10 + numberOfContours * 2 + 2 + ins;\n\n      n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2);\n\n      m = n + 2*numberOfContours;  // a loose bound on how many vertices we might need\n      vertices = (stbtt_vertex *) STBTT_malloc(m * sizeof(vertices[0]), info->userdata);\n      if (vertices == 0)\n         return 0;\n\n      next_move = 0;\n      flagcount=0;\n\n      // in first pass, we load uninterpreted data into the allocated array\n      // above, shifted to the end of the array so we won't overwrite it when\n      // we create our final data starting from the front\n\n      off = m - n; // starting offset for uninterpreted data, regardless of how m ends up being calculated\n\n      // first load flags\n\n      for (i=0; i < n; ++i) {\n         if (flagcount == 0) {\n            flags = *points++;\n            if (flags & 8)\n               flagcount = *points++;\n         } else\n            --flagcount;\n         vertices[off+i].type = flags;\n      }\n\n      // now load x coordinates\n      x=0;\n      for (i=0; i < n; ++i) {\n         flags = vertices[off+i].type;\n         if (flags & 2) {\n            stbtt_int16 dx = *points++;\n            x += (flags & 16) ? dx : -dx; // ???\n         } else {\n            if (!(flags & 16)) {\n               x = x + (stbtt_int16) (points[0]*256 + points[1]);\n               points += 2;\n            }\n         }\n         vertices[off+i].x = (stbtt_int16) x;\n      }\n\n      // now load y coordinates\n      y=0;\n      for (i=0; i < n; ++i) {\n         flags = vertices[off+i].type;\n         if (flags & 4) {\n            stbtt_int16 dy = *points++;\n            y += (flags & 32) ? dy : -dy; // ???\n         } else {\n            if (!(flags & 32)) {\n               y = y + (stbtt_int16) (points[0]*256 + points[1]);\n               points += 2;\n            }\n         }\n         vertices[off+i].y = (stbtt_int16) y;\n      }\n\n      // now convert them to our format\n      num_vertices=0;\n      sx = sy = cx = cy = scx = scy = 0;\n      for (i=0; i < n; ++i) {\n         flags = vertices[off+i].type;\n         x     = (stbtt_int16) vertices[off+i].x;\n         y     = (stbtt_int16) vertices[off+i].y;\n\n         if (next_move == i) {\n            if (i != 0)\n               num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);\n\n            // now start the new one               \n            start_off = !(flags & 1);\n            if (start_off) {\n               // if we start off with an off-curve point, then when we need to find a point on the curve\n               // where we can start, and we need to save some state for when we wraparound.\n               scx = x;\n               scy = y;\n               if (!(vertices[off+i+1].type & 1)) {\n                  // next point is also a curve point, so interpolate an on-point curve\n                  sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1;\n                  sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1;\n               } else {\n                  // otherwise just use the next point as our start point\n                  sx = (stbtt_int32) vertices[off+i+1].x;\n                  sy = (stbtt_int32) vertices[off+i+1].y;\n                  ++i; // we're using point i+1 as the starting point, so skip it\n               }\n            } else {\n               sx = x;\n               sy = y;\n            }\n            stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove,sx,sy,0,0);\n            was_off = 0;\n            next_move = 1 + ttUSHORT(endPtsOfContours+j*2);\n            ++j;\n         } else {\n            if (!(flags & 1)) { // if it's a curve\n               if (was_off) // two off-curve control points in a row means interpolate an on-curve midpoint\n                  stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);\n               cx = x;\n               cy = y;\n               was_off = 1;\n            } else {\n               if (was_off)\n                  stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x,y, cx, cy);\n               else\n                  stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x,y,0,0);\n               was_off = 0;\n            }\n         }\n      }\n      num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);\n   } else if (numberOfContours == -1) {\n      // Compound shapes.\n      int more = 1;\n      stbtt_uint8 *comp = data + g + 10;\n      num_vertices = 0;\n      vertices = 0;\n      while (more) {\n         stbtt_uint16 flags, gidx;\n         int comp_num_verts = 0, i;\n         stbtt_vertex *comp_verts = 0, *tmp = 0;\n         float mtx[6] = {1,0,0,1,0,0}, m, n;\n         \n         flags = ttSHORT(comp); comp+=2;\n         gidx = ttSHORT(comp); comp+=2;\n\n         if (flags & 2) { // XY values\n            if (flags & 1) { // shorts\n               mtx[4] = ttSHORT(comp); comp+=2;\n               mtx[5] = ttSHORT(comp); comp+=2;\n            } else {\n               mtx[4] = ttCHAR(comp); comp+=1;\n               mtx[5] = ttCHAR(comp); comp+=1;\n            }\n         }\n         else {\n            // @TODO handle matching point\n            STBTT_assert(0);\n         }\n         if (flags & (1<<3)) { // WE_HAVE_A_SCALE\n            mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;\n            mtx[1] = mtx[2] = 0;\n         } else if (flags & (1<<6)) { // WE_HAVE_AN_X_AND_YSCALE\n            mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;\n            mtx[1] = mtx[2] = 0;\n            mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;\n         } else if (flags & (1<<7)) { // WE_HAVE_A_TWO_BY_TWO\n            mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;\n            mtx[1] = ttSHORT(comp)/16384.0f; comp+=2;\n            mtx[2] = ttSHORT(comp)/16384.0f; comp+=2;\n            mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;\n         }\n         \n         // Find transformation scales.\n         m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);\n         n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);\n\n         // Get indexed glyph.\n         comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);\n         if (comp_num_verts > 0) {\n            // Transform vertices.\n            for (i = 0; i < comp_num_verts; ++i) {\n               stbtt_vertex* v = &comp_verts[i];\n               stbtt_vertex_type x,y;\n               x=v->x; y=v->y;\n               v->x = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));\n               v->y = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));\n               x=v->cx; y=v->cy;\n               v->cx = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));\n               v->cy = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));\n            }\n            // Append vertices.\n            tmp = (stbtt_vertex*)STBTT_malloc((num_vertices+comp_num_verts)*sizeof(stbtt_vertex), info->userdata);\n            if (!tmp) {\n               if (vertices) STBTT_free(vertices, info->userdata);\n               if (comp_verts) STBTT_free(comp_verts, info->userdata);\n               return 0;\n            }\n            if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex)); //-V595\n            STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex));\n            if (vertices) STBTT_free(vertices, info->userdata);\n            vertices = tmp;\n            STBTT_free(comp_verts, info->userdata);\n            num_vertices += comp_num_verts;\n         }\n         // More components ?\n         more = flags & (1<<5);\n      }\n   } else if (numberOfContours < 0) {\n      // @TODO other compound variations?\n      STBTT_assert(0);\n   } else {\n      // numberOfCounters == 0, do nothing\n   }\n\n   *pvertices = vertices;\n   return num_vertices;\n}\n\ntypedef struct\n{\n   int bounds;\n   int started;\n   float first_x, first_y;\n   float x, y;\n   stbtt_int32 min_x, max_x, min_y, max_y;\n\n   stbtt_vertex *pvertices;\n   int num_vertices;\n} stbtt__csctx;\n\n#define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0}\n\nstatic void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y)\n{\n   if (x > c->max_x || !c->started) c->max_x = x;\n   if (y > c->max_y || !c->started) c->max_y = y;\n   if (x < c->min_x || !c->started) c->min_x = x;\n   if (y < c->min_y || !c->started) c->min_y = y;\n   c->started = 1;\n}\n\nstatic void stbtt__csctx_v(stbtt__csctx *c, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy, stbtt_int32 cx1, stbtt_int32 cy1)\n{\n   if (c->bounds) {\n      stbtt__track_vertex(c, x, y);\n      if (type == STBTT_vcubic) {\n         stbtt__track_vertex(c, cx, cy);\n         stbtt__track_vertex(c, cx1, cy1);\n      }\n   } else {\n      stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy);\n      c->pvertices[c->num_vertices].cx1 = (stbtt_int16) cx1;\n      c->pvertices[c->num_vertices].cy1 = (stbtt_int16) cy1;\n   }\n   c->num_vertices++;\n}\n\nstatic void stbtt__csctx_close_shape(stbtt__csctx *ctx)\n{\n   if (ctx->first_x != ctx->x || ctx->first_y != ctx->y)\n      stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->first_x, (int)ctx->first_y, 0, 0, 0, 0);\n}\n\nstatic void stbtt__csctx_rmove_to(stbtt__csctx *ctx, float dx, float dy)\n{\n   stbtt__csctx_close_shape(ctx);\n   ctx->first_x = ctx->x = ctx->x + dx;\n   ctx->first_y = ctx->y = ctx->y + dy;\n   stbtt__csctx_v(ctx, STBTT_vmove, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);\n}\n\nstatic void stbtt__csctx_rline_to(stbtt__csctx *ctx, float dx, float dy)\n{\n   ctx->x += dx;\n   ctx->y += dy;\n   stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);\n}\n\nstatic void stbtt__csctx_rccurve_to(stbtt__csctx *ctx, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3)\n{\n   float cx1 = ctx->x + dx1;\n   float cy1 = ctx->y + dy1;\n   float cx2 = cx1 + dx2;\n   float cy2 = cy1 + dy2;\n   ctx->x = cx2 + dx3;\n   ctx->y = cy2 + dy3;\n   stbtt__csctx_v(ctx, STBTT_vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2);\n}\n\nstatic stbtt__buf stbtt__get_subr(stbtt__buf idx, int n)\n{\n   int count = stbtt__cff_index_count(&idx);\n   int bias = 107;\n   if (count >= 33900)\n      bias = 32768;\n   else if (count >= 1240)\n      bias = 1131;\n   n += bias;\n   if (n < 0 || n >= count)\n      return stbtt__new_buf(NULL, 0);\n   return stbtt__cff_index_get(idx, n);\n}\n\nstatic stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo *info, int glyph_index)\n{\n   stbtt__buf fdselect = info->fdselect;\n   int nranges, start, end, v, fmt, fdselector = -1, i;\n\n   stbtt__buf_seek(&fdselect, 0);\n   fmt = stbtt__buf_get8(&fdselect);\n   if (fmt == 0) {\n      // untested\n      stbtt__buf_skip(&fdselect, glyph_index);\n      fdselector = stbtt__buf_get8(&fdselect);\n   } else if (fmt == 3) {\n      nranges = stbtt__buf_get16(&fdselect);\n      start = stbtt__buf_get16(&fdselect);\n      for (i = 0; i < nranges; i++) {\n         v = stbtt__buf_get8(&fdselect);\n         end = stbtt__buf_get16(&fdselect);\n         if (glyph_index >= start && glyph_index < end) {\n            fdselector = v;\n            break;\n         }\n         start = end;\n      }\n   }\n   if (fdselector == -1) stbtt__new_buf(NULL, 0);\n   return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector));\n}\n\nstatic int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, stbtt__csctx *c)\n{\n   int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;\n   int has_subrs = 0, clear_stack;\n   float s[48];\n   stbtt__buf subr_stack[10], subrs = info->subrs, b;\n   float f;\n\n#define STBTT__CSERR(s) (0)\n\n   // this currently ignores the initial width value, which isn't needed if we have hmtx\n   b = stbtt__cff_index_get(info->charstrings, glyph_index);\n   while (b.cursor < b.size) {\n      i = 0;\n      clear_stack = 1;\n      b0 = stbtt__buf_get8(&b);\n      switch (b0) {\n      // @TODO implement hinting\n      case 0x13: // hintmask\n      case 0x14: // cntrmask\n         if (in_header)\n            maskbits += (sp / 2); // implicit \"vstem\"\n         in_header = 0;\n         stbtt__buf_skip(&b, (maskbits + 7) / 8);\n         break;\n\n      case 0x01: // hstem\n      case 0x03: // vstem\n      case 0x12: // hstemhm\n      case 0x17: // vstemhm\n         maskbits += (sp / 2);\n         break;\n\n      case 0x15: // rmoveto\n         in_header = 0;\n         if (sp < 2) return STBTT__CSERR(\"rmoveto stack\");\n         stbtt__csctx_rmove_to(c, s[sp-2], s[sp-1]);\n         break;\n      case 0x04: // vmoveto\n         in_header = 0;\n         if (sp < 1) return STBTT__CSERR(\"vmoveto stack\");\n         stbtt__csctx_rmove_to(c, 0, s[sp-1]);\n         break;\n      case 0x16: // hmoveto\n         in_header = 0;\n         if (sp < 1) return STBTT__CSERR(\"hmoveto stack\");\n         stbtt__csctx_rmove_to(c, s[sp-1], 0);\n         break;\n\n      case 0x05: // rlineto\n         if (sp < 2) return STBTT__CSERR(\"rlineto stack\");\n         for (; i + 1 < sp; i += 2)\n            stbtt__csctx_rline_to(c, s[i], s[i+1]);\n         break;\n\n      // hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical\n      // starting from a different place.\n\n      case 0x07: // vlineto\n         if (sp < 1) return STBTT__CSERR(\"vlineto stack\");\n         goto vlineto;\n      case 0x06: // hlineto\n         if (sp < 1) return STBTT__CSERR(\"hlineto stack\");\n         for (;;) {\n            if (i >= sp) break;\n            stbtt__csctx_rline_to(c, s[i], 0);\n            i++;\n      vlineto:\n            if (i >= sp) break;\n            stbtt__csctx_rline_to(c, 0, s[i]);\n            i++;\n         }\n         break;\n\n      case 0x1F: // hvcurveto\n         if (sp < 4) return STBTT__CSERR(\"hvcurveto stack\");\n         goto hvcurveto;\n      case 0x1E: // vhcurveto\n         if (sp < 4) return STBTT__CSERR(\"vhcurveto stack\");\n         for (;;) {\n            if (i + 3 >= sp) break;\n            stbtt__csctx_rccurve_to(c, 0, s[i], s[i+1], s[i+2], s[i+3], (sp - i == 5) ? s[i + 4] : 0.0f);\n            i += 4;\n      hvcurveto:\n            if (i + 3 >= sp) break;\n            stbtt__csctx_rccurve_to(c, s[i], 0, s[i+1], s[i+2], (sp - i == 5) ? s[i+4] : 0.0f, s[i+3]);\n            i += 4;\n         }\n         break;\n\n      case 0x08: // rrcurveto\n         if (sp < 6) return STBTT__CSERR(\"rcurveline stack\");\n         for (; i + 5 < sp; i += 6)\n            stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);\n         break;\n\n      case 0x18: // rcurveline\n         if (sp < 8) return STBTT__CSERR(\"rcurveline stack\");\n         for (; i + 5 < sp - 2; i += 6)\n            stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);\n         if (i + 1 >= sp) return STBTT__CSERR(\"rcurveline stack\");\n         stbtt__csctx_rline_to(c, s[i], s[i+1]);\n         break;\n\n      case 0x19: // rlinecurve\n         if (sp < 8) return STBTT__CSERR(\"rlinecurve stack\");\n         for (; i + 1 < sp - 6; i += 2)\n            stbtt__csctx_rline_to(c, s[i], s[i+1]);\n         if (i + 5 >= sp) return STBTT__CSERR(\"rlinecurve stack\");\n         stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);\n         break;\n\n      case 0x1A: // vvcurveto\n      case 0x1B: // hhcurveto\n         if (sp < 4) return STBTT__CSERR(\"(vv|hh)curveto stack\");\n         f = 0.0;\n         if (sp & 1) { f = s[i]; i++; }\n         for (; i + 3 < sp; i += 4) {\n            if (b0 == 0x1B)\n               stbtt__csctx_rccurve_to(c, s[i], f, s[i+1], s[i+2], s[i+3], 0.0);\n            else\n               stbtt__csctx_rccurve_to(c, f, s[i], s[i+1], s[i+2], 0.0, s[i+3]);\n            f = 0.0;\n         }\n         break;\n\n      case 0x0A: // callsubr\n         if (!has_subrs) {\n            if (info->fdselect.size)\n               subrs = stbtt__cid_get_glyph_subrs(info, glyph_index);\n            has_subrs = 1;\n         }\n         // fallthrough\n      case 0x1D: // callgsubr\n         if (sp < 1) return STBTT__CSERR(\"call(g|)subr stack\");\n         v = (int) s[--sp];\n         if (subr_stack_height >= 10) return STBTT__CSERR(\"recursion limit\");\n         subr_stack[subr_stack_height++] = b;\n         b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v);\n         if (b.size == 0) return STBTT__CSERR(\"subr not found\");\n         b.cursor = 0;\n         clear_stack = 0;\n         break;\n\n      case 0x0B: // return\n         if (subr_stack_height <= 0) return STBTT__CSERR(\"return outside subr\");\n         b = subr_stack[--subr_stack_height];\n         clear_stack = 0;\n         break;\n\n      case 0x0E: // endchar\n         stbtt__csctx_close_shape(c);\n         return 1;\n\n      case 0x0C: { // two-byte escape\n         float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6;\n         float dx, dy;\n         int b1 = stbtt__buf_get8(&b);\n         switch (b1) {\n         // @TODO These \"flex\" implementations ignore the flex-depth and resolution,\n         // and always draw beziers.\n         case 0x22: // hflex\n            if (sp < 7) return STBTT__CSERR(\"hflex stack\");\n            dx1 = s[0];\n            dx2 = s[1];\n            dy2 = s[2];\n            dx3 = s[3];\n            dx4 = s[4];\n            dx5 = s[5];\n            dx6 = s[6];\n            stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0);\n            stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0);\n            break;\n\n         case 0x23: // flex\n            if (sp < 13) return STBTT__CSERR(\"flex stack\");\n            dx1 = s[0];\n            dy1 = s[1];\n            dx2 = s[2];\n            dy2 = s[3];\n            dx3 = s[4];\n            dy3 = s[5];\n            dx4 = s[6];\n            dy4 = s[7];\n            dx5 = s[8];\n            dy5 = s[9];\n            dx6 = s[10];\n            dy6 = s[11];\n            //fd is s[12]\n            stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);\n            stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);\n            break;\n\n         case 0x24: // hflex1\n            if (sp < 9) return STBTT__CSERR(\"hflex1 stack\");\n            dx1 = s[0];\n            dy1 = s[1];\n            dx2 = s[2];\n            dy2 = s[3];\n            dx3 = s[4];\n            dx4 = s[5];\n            dx5 = s[6];\n            dy5 = s[7];\n            dx6 = s[8];\n            stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0);\n            stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1+dy2+dy5));\n            break;\n\n         case 0x25: // flex1\n            if (sp < 11) return STBTT__CSERR(\"flex1 stack\");\n            dx1 = s[0];\n            dy1 = s[1];\n            dx2 = s[2];\n            dy2 = s[3];\n            dx3 = s[4];\n            dy3 = s[5];\n            dx4 = s[6];\n            dy4 = s[7];\n            dx5 = s[8];\n            dy5 = s[9];\n            dx6 = dy6 = s[10];\n            dx = dx1+dx2+dx3+dx4+dx5;\n            dy = dy1+dy2+dy3+dy4+dy5;\n            if (STBTT_fabs(dx) > STBTT_fabs(dy))\n               dy6 = -dy;\n            else\n               dx6 = -dx;\n            stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);\n            stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);\n            break;\n\n         default:\n            return STBTT__CSERR(\"unimplemented\");\n         }\n      } break;\n\n      default:\n         if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254)) //-V560\n            return STBTT__CSERR(\"reserved operator\");\n\n         // push immediate\n         if (b0 == 255) {\n            f = (float)(stbtt_int32)stbtt__buf_get32(&b) / 0x10000;\n         } else {\n            stbtt__buf_skip(&b, -1);\n            f = (float)(stbtt_int16)stbtt__cff_int(&b);\n         }\n         if (sp >= 48) return STBTT__CSERR(\"push stack overflow\");\n         s[sp++] = f;\n         clear_stack = 0;\n         break;\n      }\n      if (clear_stack) sp = 0;\n   }\n   return STBTT__CSERR(\"no endchar\");\n\n#undef STBTT__CSERR\n}\n\nstatic int stbtt__GetGlyphShapeT2(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)\n{\n   // runs the charstring twice, once to count and once to output (to avoid realloc)\n   stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1);\n   stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0);\n   if (stbtt__run_charstring(info, glyph_index, &count_ctx)) {\n      *pvertices = (stbtt_vertex*)STBTT_malloc(count_ctx.num_vertices*sizeof(stbtt_vertex), info->userdata);\n      output_ctx.pvertices = *pvertices;\n      if (stbtt__run_charstring(info, glyph_index, &output_ctx)) {\n         STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices);\n         return output_ctx.num_vertices;\n      }\n   }\n   *pvertices = NULL;\n   return 0;\n}\n\nstatic int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)\n{\n   stbtt__csctx c = STBTT__CSCTX_INIT(1);\n   int r = stbtt__run_charstring(info, glyph_index, &c);\n   if (x0)  *x0 = r ? c.min_x : 0;\n   if (y0)  *y0 = r ? c.min_y : 0;\n   if (x1)  *x1 = r ? c.max_x : 0;\n   if (y1)  *y1 = r ? c.max_y : 0;\n   return r ? c.num_vertices : 0;\n}\n\nSTBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)\n{\n   if (!info->cff.size)\n      return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices);\n   else\n      return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices);\n}\n\nSTBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing)\n{\n   stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);\n   if (glyph_index < numOfLongHorMetrics) {\n      if (advanceWidth)     *advanceWidth    = ttSHORT(info->data + info->hmtx + 4*glyph_index);\n      if (leftSideBearing)  *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);\n   } else {\n      if (advanceWidth)     *advanceWidth    = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));\n      if (leftSideBearing)  *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));\n   }\n}\n\nstatic int  stbtt__GetGlyphKernInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)\n{\n   stbtt_uint8 *data = info->data + info->kern;\n   stbtt_uint32 needle, straw;\n   int l, r, m;\n\n   // we only look at the first table. it must be 'horizontal' and format 0.\n   if (!info->kern)\n      return 0;\n   if (ttUSHORT(data+2) < 1) // number of tables, need at least 1\n      return 0;\n   if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format\n      return 0;\n\n   l = 0;\n   r = ttUSHORT(data+10) - 1;\n   needle = glyph1 << 16 | glyph2;\n   while (l <= r) {\n      m = (l + r) >> 1;\n      straw = ttULONG(data+18+(m*6)); // note: unaligned read\n      if (needle < straw)\n         r = m - 1;\n      else if (needle > straw)\n         l = m + 1;\n      else\n         return ttSHORT(data+22+(m*6));\n   }\n   return 0;\n}\n\nstatic stbtt_int32  stbtt__GetCoverageIndex(stbtt_uint8 *coverageTable, int glyph)\n{\n    stbtt_uint16 coverageFormat = ttUSHORT(coverageTable);\n    switch(coverageFormat) {\n        case 1: {\n            stbtt_uint16 glyphCount = ttUSHORT(coverageTable + 2);\n\n            // Binary search.\n            stbtt_int32 l=0, r=glyphCount-1, m;\n            int straw, needle=glyph;\n            while (l <= r) {\n                stbtt_uint8 *glyphArray = coverageTable + 4;\n                stbtt_uint16 glyphID;\n                m = (l + r) >> 1;\n                glyphID = ttUSHORT(glyphArray + 2 * m);\n                straw = glyphID;\n                if (needle < straw)\n                    r = m - 1;\n                else if (needle > straw)\n                    l = m + 1;\n                else {\n                     return m;\n                }\n            }\n        } break;\n\n        case 2: {\n            stbtt_uint16 rangeCount = ttUSHORT(coverageTable + 2);\n            stbtt_uint8 *rangeArray = coverageTable + 4;\n\n            // Binary search.\n            stbtt_int32 l=0, r=rangeCount-1, m;\n            int strawStart, strawEnd, needle=glyph;\n            while (l <= r) {\n                stbtt_uint8 *rangeRecord;\n                m = (l + r) >> 1;\n                rangeRecord = rangeArray + 6 * m;\n                strawStart = ttUSHORT(rangeRecord);\n                strawEnd = ttUSHORT(rangeRecord + 2);\n                if (needle < strawStart)\n                    r = m - 1;\n                else if (needle > strawEnd)\n                    l = m + 1;\n                else {\n                    stbtt_uint16 startCoverageIndex = ttUSHORT(rangeRecord + 4);\n                    return startCoverageIndex + glyph - strawStart;\n                }\n            }\n        } break;\n\n        default: {\n            // There are no other cases.\n            STBTT_assert(0);\n        } break;\n    }\n\n    return -1;\n}\n\nstatic stbtt_int32  stbtt__GetGlyphClass(stbtt_uint8 *classDefTable, int glyph)\n{\n    stbtt_uint16 classDefFormat = ttUSHORT(classDefTable);\n    switch(classDefFormat)\n    {\n        case 1: {\n            stbtt_uint16 startGlyphID = ttUSHORT(classDefTable + 2);\n            stbtt_uint16 glyphCount = ttUSHORT(classDefTable + 4);\n            stbtt_uint8 *classDef1ValueArray = classDefTable + 6;\n\n            if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount)\n                return (stbtt_int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID));\n\n            // [DEAR IMGUI] Commented to fix static analyzer warning\n            //classDefTable = classDef1ValueArray + 2 * glyphCount;\n        } break;\n\n        case 2: {\n            stbtt_uint16 classRangeCount = ttUSHORT(classDefTable + 2);\n            stbtt_uint8 *classRangeRecords = classDefTable + 4;\n\n            // Binary search.\n            stbtt_int32 l=0, r=classRangeCount-1, m;\n            int strawStart, strawEnd, needle=glyph;\n            while (l <= r) {\n                stbtt_uint8 *classRangeRecord;\n                m = (l + r) >> 1;\n                classRangeRecord = classRangeRecords + 6 * m;\n                strawStart = ttUSHORT(classRangeRecord);\n                strawEnd = ttUSHORT(classRangeRecord + 2);\n                if (needle < strawStart)\n                    r = m - 1;\n                else if (needle > strawEnd)\n                    l = m + 1;\n                else\n                    return (stbtt_int32)ttUSHORT(classRangeRecord + 4);\n            }\n\n            // [DEAR IMGUI] Commented to fix static analyzer warning\n            //classDefTable = classRangeRecords + 6 * classRangeCount;\n        } break;\n\n        default: {\n            // There are no other cases.\n            STBTT_assert(0);\n        } break;\n    }\n\n    return -1;\n}\n\n// Define to STBTT_assert(x) if you want to break on unimplemented formats.\n#define STBTT_GPOS_TODO_assert(x)\n\nstatic stbtt_int32  stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)\n{\n    stbtt_uint16 lookupListOffset;\n    stbtt_uint8 *lookupList;\n    stbtt_uint16 lookupCount;\n    stbtt_uint8 *data;\n    stbtt_int32 i;\n\n    if (!info->gpos) return 0;\n\n    data = info->data + info->gpos;\n\n    if (ttUSHORT(data+0) != 1) return 0; // Major version 1\n    if (ttUSHORT(data+2) != 0) return 0; // Minor version 0\n\n    lookupListOffset = ttUSHORT(data+8);\n    lookupList = data + lookupListOffset;\n    lookupCount = ttUSHORT(lookupList);\n\n    for (i=0; i<lookupCount; ++i) {\n        stbtt_uint16 lookupOffset = ttUSHORT(lookupList + 2 + 2 * i);\n        stbtt_uint8 *lookupTable = lookupList + lookupOffset;\n\n        stbtt_uint16 lookupType = ttUSHORT(lookupTable);\n        stbtt_uint16 subTableCount = ttUSHORT(lookupTable + 4);\n        stbtt_uint8 *subTableOffsets = lookupTable + 6;\n        switch(lookupType) {\n            case 2: { // Pair Adjustment Positioning Subtable\n                stbtt_int32 sti;\n                for (sti=0; sti<subTableCount; sti++) {\n                    stbtt_uint16 subtableOffset = ttUSHORT(subTableOffsets + 2 * sti);\n                    stbtt_uint8 *table = lookupTable + subtableOffset;\n                    stbtt_uint16 posFormat = ttUSHORT(table);\n                    stbtt_uint16 coverageOffset = ttUSHORT(table + 2);\n                    stbtt_int32 coverageIndex = stbtt__GetCoverageIndex(table + coverageOffset, glyph1);\n                    if (coverageIndex == -1) continue;\n\n                    switch (posFormat) {\n                        case 1: {\n                            stbtt_int32 l, r, m;\n                            int straw, needle;\n                            stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);\n                            stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);\n                            stbtt_int32 valueRecordPairSizeInBytes = 2;\n                            stbtt_uint16 pairSetCount = ttUSHORT(table + 8);\n                            stbtt_uint16 pairPosOffset = ttUSHORT(table + 10 + 2 * coverageIndex);\n                            stbtt_uint8 *pairValueTable = table + pairPosOffset;\n                            stbtt_uint16 pairValueCount = ttUSHORT(pairValueTable);\n                            stbtt_uint8 *pairValueArray = pairValueTable + 2;\n                            // TODO: Support more formats.\n                            STBTT_GPOS_TODO_assert(valueFormat1 == 4);\n                            if (valueFormat1 != 4) return 0;\n                            STBTT_GPOS_TODO_assert(valueFormat2 == 0);\n                            if (valueFormat2 != 0) return 0;\n\n                            STBTT_assert(coverageIndex < pairSetCount);\n                            STBTT__NOTUSED(pairSetCount);\n\n                            needle=glyph2;\n                            r=pairValueCount-1;\n                            l=0;\n\n                            // Binary search.\n                            while (l <= r) {\n                                stbtt_uint16 secondGlyph;\n                                stbtt_uint8 *pairValue;\n                                m = (l + r) >> 1;\n                                pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m;\n                                secondGlyph = ttUSHORT(pairValue);\n                                straw = secondGlyph;\n                                if (needle < straw)\n                                    r = m - 1;\n                                else if (needle > straw)\n                                    l = m + 1;\n                                else {\n                                    stbtt_int16 xAdvance = ttSHORT(pairValue + 2);\n                                    return xAdvance;\n                                }\n                            }\n                        } break;\n\n                        case 2: {\n                            stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);\n                            stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);\n\n                            stbtt_uint16 classDef1Offset = ttUSHORT(table + 8);\n                            stbtt_uint16 classDef2Offset = ttUSHORT(table + 10);\n                            int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1);\n                            int glyph2class = stbtt__GetGlyphClass(table + classDef2Offset, glyph2);\n\n                            stbtt_uint16 class1Count = ttUSHORT(table + 12);\n                            stbtt_uint16 class2Count = ttUSHORT(table + 14);\n                            STBTT_assert(glyph1class < class1Count);\n                            STBTT_assert(glyph2class < class2Count);\n\n                            // TODO: Support more formats.\n                            STBTT_GPOS_TODO_assert(valueFormat1 == 4);\n                            if (valueFormat1 != 4) return 0;\n                            STBTT_GPOS_TODO_assert(valueFormat2 == 0);\n                            if (valueFormat2 != 0) return 0;\n\n                            if (glyph1class >= 0 && glyph1class < class1Count && glyph2class >= 0 && glyph2class < class2Count) {\n                                stbtt_uint8 *class1Records = table + 16;\n                                stbtt_uint8 *class2Records = class1Records + 2 * (glyph1class * class2Count);\n                                stbtt_int16 xAdvance = ttSHORT(class2Records + 2 * glyph2class);\n                                return xAdvance;\n                            }\n                        } break;\n\n                        default: {\n                            // There are no other cases.\n                            STBTT_assert(0);\n                            break;\n                        };\n                    }\n                }\n                break;\n            };\n\n            default:\n                // TODO: Implement other stuff.\n                break;\n        }\n    }\n\n    return 0;\n}\n\nSTBTT_DEF int  stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int g1, int g2)\n{\n   int xAdvance = 0;\n\n   if (info->gpos)\n      xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2);\n\n   if (info->kern)\n      xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2);\n\n   return xAdvance;\n}\n\nSTBTT_DEF int  stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2)\n{\n   if (!info->kern && !info->gpos) // if no kerning table, don't waste time looking up both codepoint->glyphs\n      return 0;\n   return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2));\n}\n\nSTBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing)\n{\n   stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing);\n}\n\nSTBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap)\n{\n   if (ascent ) *ascent  = ttSHORT(info->data+info->hhea + 4);\n   if (descent) *descent = ttSHORT(info->data+info->hhea + 6);\n   if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);\n}\n\nSTBTT_DEF int  stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap)\n{\n   int tab = stbtt__find_table(info->data, info->fontstart, \"OS/2\");\n   if (!tab)\n      return 0;\n   if (typoAscent ) *typoAscent  = ttSHORT(info->data+tab + 68);\n   if (typoDescent) *typoDescent = ttSHORT(info->data+tab + 70);\n   if (typoLineGap) *typoLineGap = ttSHORT(info->data+tab + 72);\n   return 1;\n}\n\nSTBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1)\n{\n   *x0 = ttSHORT(info->data + info->head + 36);\n   *y0 = ttSHORT(info->data + info->head + 38);\n   *x1 = ttSHORT(info->data + info->head + 40);\n   *y1 = ttSHORT(info->data + info->head + 42);\n}\n\nSTBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height)\n{\n   int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);\n   return (float) height / fheight;\n}\n\nSTBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels)\n{\n   int unitsPerEm = ttUSHORT(info->data + info->head + 18);\n   return pixels / unitsPerEm;\n}\n\nSTBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v)\n{\n   STBTT_free(v, info->userdata);\n}\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// antialiasing software rasterizer\n//\n\nSTBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1)\n{\n   int x0=0,y0=0,x1,y1; // =0 suppresses compiler warning\n   if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {\n      // e.g. space character\n      if (ix0) *ix0 = 0;\n      if (iy0) *iy0 = 0;\n      if (ix1) *ix1 = 0;\n      if (iy1) *iy1 = 0;\n   } else {\n      // move to integral bboxes (treating pixels as little squares, what pixels get touched)?\n      if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x);\n      if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);\n      if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x);\n      if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y);\n   }\n}\n\nSTBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)\n{\n   stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);\n}\n\nSTBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1)\n{\n   stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1);\n}\n\nSTBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)\n{\n   stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1);\n}\n\n//////////////////////////////////////////////////////////////////////////////\n//\n//  Rasterizer\n\ntypedef struct stbtt__hheap_chunk\n{\n   struct stbtt__hheap_chunk *next;\n} stbtt__hheap_chunk;\n\ntypedef struct stbtt__hheap\n{\n   struct stbtt__hheap_chunk *head;\n   void   *first_free;\n   int    num_remaining_in_head_chunk;\n} stbtt__hheap;\n\nstatic void *stbtt__hheap_alloc(stbtt__hheap *hh, size_t size, void *userdata)\n{\n   if (hh->first_free) {\n      void *p = hh->first_free;\n      hh->first_free = * (void **) p;\n      return p;\n   } else {\n      if (hh->num_remaining_in_head_chunk == 0) {\n         int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);\n         stbtt__hheap_chunk *c = (stbtt__hheap_chunk *) STBTT_malloc(sizeof(stbtt__hheap_chunk) + size * count, userdata);\n         if (c == NULL)\n            return NULL;\n         c->next = hh->head;\n         hh->head = c;\n         hh->num_remaining_in_head_chunk = count;\n      }\n      --hh->num_remaining_in_head_chunk;\n      return (char *) (hh->head) + sizeof(stbtt__hheap_chunk) + size * hh->num_remaining_in_head_chunk;\n   }\n}\n\nstatic void stbtt__hheap_free(stbtt__hheap *hh, void *p)\n{\n   *(void **) p = hh->first_free;\n   hh->first_free = p;\n}\n\nstatic void stbtt__hheap_cleanup(stbtt__hheap *hh, void *userdata)\n{\n   stbtt__hheap_chunk *c = hh->head;\n   while (c) {\n      stbtt__hheap_chunk *n = c->next;\n      STBTT_free(c, userdata);\n      c = n;\n   }\n}\n\ntypedef struct stbtt__edge {\n   float x0,y0, x1,y1;\n   int invert;\n} stbtt__edge;\n\n\ntypedef struct stbtt__active_edge\n{\n   struct stbtt__active_edge *next;\n   #if STBTT_RASTERIZER_VERSION==1\n   int x,dx;\n   float ey;\n   int direction;\n   #elif STBTT_RASTERIZER_VERSION==2\n   float fx,fdx,fdy;\n   float direction;\n   float sy;\n   float ey;\n   #else\n   #error \"Unrecognized value of STBTT_RASTERIZER_VERSION\"\n   #endif\n} stbtt__active_edge;\n\n#if STBTT_RASTERIZER_VERSION == 1\n#define STBTT_FIXSHIFT   10\n#define STBTT_FIX        (1 << STBTT_FIXSHIFT)\n#define STBTT_FIXMASK    (STBTT_FIX-1)\n\nstatic stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata)\n{\n   stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata);\n   float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);\n   STBTT_assert(z != NULL);\n   if (!z) return z;\n   \n   // round dx down to avoid overshooting\n   if (dxdy < 0)\n      z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy);\n   else\n      z->dx = STBTT_ifloor(STBTT_FIX * dxdy);\n\n   z->x = STBTT_ifloor(STBTT_FIX * e->x0 + z->dx * (start_point - e->y0)); // use z->dx so when we offset later it's by the same amount\n   z->x -= off_x * STBTT_FIX;\n\n   z->ey = e->y1;\n   z->next = 0;\n   z->direction = e->invert ? 1 : -1;\n   return z;\n}\n#elif STBTT_RASTERIZER_VERSION == 2\nstatic stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata)\n{\n   stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata);\n   float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);\n   STBTT_assert(z != NULL);\n   //STBTT_assert(e->y0 <= start_point);\n   if (!z) return z;\n   z->fdx = dxdy;\n   z->fdy = dxdy != 0.0f ? (1.0f/dxdy) : 0.0f;\n   z->fx = e->x0 + dxdy * (start_point - e->y0);\n   z->fx -= off_x;\n   z->direction = e->invert ? 1.0f : -1.0f;\n   z->sy = e->y0;\n   z->ey = e->y1;\n   z->next = 0;\n   return z;\n}\n#else\n#error \"Unrecognized value of STBTT_RASTERIZER_VERSION\"\n#endif\n\n#if STBTT_RASTERIZER_VERSION == 1\n// note: this routine clips fills that extend off the edges... ideally this\n// wouldn't happen, but it could happen if the truetype glyph bounding boxes\n// are wrong, or if the user supplies a too-small bitmap\nstatic void stbtt__fill_active_edges(unsigned char *scanline, int len, stbtt__active_edge *e, int max_weight)\n{\n   // non-zero winding fill\n   int x0=0, w=0;\n\n   while (e) {\n      if (w == 0) {\n         // if we're currently at zero, we need to record the edge start point\n         x0 = e->x; w += e->direction;\n      } else {\n         int x1 = e->x; w += e->direction;\n         // if we went to zero, we need to draw\n         if (w == 0) {\n            int i = x0 >> STBTT_FIXSHIFT;\n            int j = x1 >> STBTT_FIXSHIFT;\n\n            if (i < len && j >= 0) {\n               if (i == j) {\n                  // x0,x1 are the same pixel, so compute combined coverage\n                  scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> STBTT_FIXSHIFT);\n               } else {\n                  if (i >= 0) // add antialiasing for x0\n                     scanline[i] = scanline[i] + (stbtt_uint8) (((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT);\n                  else\n                     i = -1; // clip\n\n                  if (j < len) // add antialiasing for x1\n                     scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT);\n                  else\n                     j = len; // clip\n\n                  for (++i; i < j; ++i) // fill pixels between x0 and x1\n                     scanline[i] = scanline[i] + (stbtt_uint8) max_weight;\n               }\n            }\n         }\n      }\n      \n      e = e->next;\n   }\n}\n\nstatic void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata)\n{\n   stbtt__hheap hh = { 0, 0, 0 };\n   stbtt__active_edge *active = NULL;\n   int y,j=0;\n   int max_weight = (255 / vsubsample);  // weight per vertical scanline\n   int s; // vertical subsample index\n   unsigned char scanline_data[512], *scanline;\n\n   if (result->w > 512)\n      scanline = (unsigned char *) STBTT_malloc(result->w, userdata);\n   else\n      scanline = scanline_data;\n\n   y = off_y * vsubsample;\n   e[n].y0 = (off_y + result->h) * (float) vsubsample + 1;\n\n   while (j < result->h) {\n      STBTT_memset(scanline, 0, result->w);\n      for (s=0; s < vsubsample; ++s) {\n         // find center of pixel for this scanline\n         float scan_y = y + 0.5f;\n         stbtt__active_edge **step = &active;\n\n         // update all active edges;\n         // remove all active edges that terminate before the center of this scanline\n         while (*step) {\n            stbtt__active_edge * z = *step;\n            if (z->ey <= scan_y) {\n               *step = z->next; // delete from list\n               STBTT_assert(z->direction);\n               z->direction = 0;\n               stbtt__hheap_free(&hh, z);\n            } else {\n               z->x += z->dx; // advance to position for current scanline\n               step = &((*step)->next); // advance through list\n            }\n         }\n\n         // resort the list if needed\n         for(;;) {\n            int changed=0;\n            step = &active;\n            while (*step && (*step)->next) {\n               if ((*step)->x > (*step)->next->x) {\n                  stbtt__active_edge *t = *step;\n                  stbtt__active_edge *q = t->next;\n\n                  t->next = q->next;\n                  q->next = t;\n                  *step = q;\n                  changed = 1;\n               }\n               step = &(*step)->next;\n            }\n            if (!changed) break;\n         }\n\n         // insert all edges that start before the center of this scanline -- omit ones that also end on this scanline\n         while (e->y0 <= scan_y) {\n            if (e->y1 > scan_y) {\n               stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata);\n               if (z != NULL) {\n                  // find insertion point\n                  if (active == NULL)\n                     active = z;\n                  else if (z->x < active->x) {\n                     // insert at front\n                     z->next = active;\n                     active = z;\n                  } else {\n                     // find thing to insert AFTER\n                     stbtt__active_edge *p = active;\n                     while (p->next && p->next->x < z->x)\n                        p = p->next;\n                     // at this point, p->next->x is NOT < z->x\n                     z->next = p->next;\n                     p->next = z;\n                  }\n               }\n            }\n            ++e;\n         }\n\n         // now process all active edges in XOR fashion\n         if (active)\n            stbtt__fill_active_edges(scanline, result->w, active, max_weight);\n\n         ++y;\n      }\n      STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w);\n      ++j;\n   }\n\n   stbtt__hheap_cleanup(&hh, userdata);\n\n   if (scanline != scanline_data)\n      STBTT_free(scanline, userdata);\n}\n\n#elif STBTT_RASTERIZER_VERSION == 2\n\n// the edge passed in here does not cross the vertical line at x or the vertical line at x+1\n// (i.e. it has already been clipped to those)\nstatic void stbtt__handle_clipped_edge(float *scanline, int x, stbtt__active_edge *e, float x0, float y0, float x1, float y1)\n{\n   if (y0 == y1) return;\n   STBTT_assert(y0 < y1);\n   STBTT_assert(e->sy <= e->ey);\n   if (y0 > e->ey) return;\n   if (y1 < e->sy) return;\n   if (y0 < e->sy) {\n      x0 += (x1-x0) * (e->sy - y0) / (y1-y0);\n      y0 = e->sy;\n   }\n   if (y1 > e->ey) {\n      x1 += (x1-x0) * (e->ey - y1) / (y1-y0);\n      y1 = e->ey;\n   }\n\n   if (x0 == x)\n      STBTT_assert(x1 <= x+1);\n   else if (x0 == x+1)\n      STBTT_assert(x1 >= x);\n   else if (x0 <= x)\n      STBTT_assert(x1 <= x);\n   else if (x0 >= x+1)\n      STBTT_assert(x1 >= x+1);\n   else\n      STBTT_assert(x1 >= x && x1 <= x+1);\n\n   if (x0 <= x && x1 <= x)\n      scanline[x] += e->direction * (y1-y0);\n   else if (x0 >= x+1 && x1 >= x+1)\n      ;\n   else {\n      STBTT_assert(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);\n      scanline[x] += e->direction * (y1-y0) * (1-((x0-x)+(x1-x))/2); // coverage = 1 - average x position\n   }\n}\n\nstatic void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, int len, stbtt__active_edge *e, float y_top)\n{\n   float y_bottom = y_top+1;\n\n   while (e) {\n      // brute force every pixel\n\n      // compute intersection points with top & bottom\n      STBTT_assert(e->ey >= y_top);\n\n      if (e->fdx == 0) {\n         float x0 = e->fx;\n         if (x0 < len) {\n            if (x0 >= 0) {\n               stbtt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom);\n               stbtt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom);\n            } else {\n               stbtt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);\n            }\n         }\n      } else {\n         float x0 = e->fx;\n         float dx = e->fdx;\n         float xb = x0 + dx;\n         float x_top, x_bottom;\n         float sy0,sy1;\n         float dy = e->fdy;\n         STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);\n\n         // compute endpoints of line segment clipped to this scanline (if the\n         // line segment starts on this scanline. x0 is the intersection of the\n         // line with y_top, but that may be off the line segment.\n         if (e->sy > y_top) {\n            x_top = x0 + dx * (e->sy - y_top);\n            sy0 = e->sy;\n         } else {\n            x_top = x0;\n            sy0 = y_top;\n         }\n         if (e->ey < y_bottom) {\n            x_bottom = x0 + dx * (e->ey - y_top);\n            sy1 = e->ey;\n         } else {\n            x_bottom = xb;\n            sy1 = y_bottom;\n         }\n\n         if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {\n            // from here on, we don't have to range check x values\n\n            if ((int) x_top == (int) x_bottom) {\n               float height;\n               // simple case, only spans one pixel\n               int x = (int) x_top;\n               height = sy1 - sy0;\n               STBTT_assert(x >= 0 && x < len);\n               scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2)  * height;\n               scanline_fill[x] += e->direction * height; // everything right of this pixel is filled\n            } else {\n               int x,x1,x2;\n               float y_crossing, step, sign, area;\n               // covers 2+ pixels\n               if (x_top > x_bottom) {\n                  // flip scanline vertically; signed area is the same\n                  float t;\n                  sy0 = y_bottom - (sy0 - y_top);\n                  sy1 = y_bottom - (sy1 - y_top);\n                  t = sy0, sy0 = sy1, sy1 = t;\n                  t = x_bottom, x_bottom = x_top, x_top = t;\n                  dx = -dx;\n                  dy = -dy;\n                  t = x0, x0 = xb, xb = t;\n                  // [DEAR IMGUI] Fix static analyzer warning\n                  (void)dx; // [ImGui: fix static analyzer warning]\n               }\n\n               x1 = (int) x_top;\n               x2 = (int) x_bottom;\n               // compute intersection with y axis at x1+1\n               y_crossing = (x1+1 - x0) * dy + y_top;\n\n               sign = e->direction;\n               // area of the rectangle covered from y0..y_crossing\n               area = sign * (y_crossing-sy0);\n               // area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing)\n               scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2);\n\n               step = sign * dy;\n               for (x = x1+1; x < x2; ++x) {\n                  scanline[x] += area + step/2;\n                  area += step;\n               }\n               y_crossing += dy * (x2 - (x1+1));\n\n               STBTT_assert(STBTT_fabs(area) <= 1.01f);\n\n               scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing);\n\n               scanline_fill[x2] += sign * (sy1-sy0);\n            }\n         } else {\n            // if edge goes outside of box we're drawing, we require\n            // clipping logic. since this does not match the intended use\n            // of this library, we use a different, very slow brute\n            // force implementation\n            int x;\n            for (x=0; x < len; ++x) {\n               // cases:\n               //\n               // there can be up to two intersections with the pixel. any intersection\n               // with left or right edges can be handled by splitting into two (or three)\n               // regions. intersections with top & bottom do not necessitate case-wise logic.\n               //\n               // the old way of doing this found the intersections with the left & right edges,\n               // then used some simple logic to produce up to three segments in sorted order\n               // from top-to-bottom. however, this had a problem: if an x edge was epsilon\n               // across the x border, then the corresponding y position might not be distinct\n               // from the other y segment, and it might ignored as an empty segment. to avoid\n               // that, we need to explicitly produce segments based on x positions.\n\n               // rename variables to clearly-defined pairs\n               float y0 = y_top;\n               float x1 = (float) (x);\n               float x2 = (float) (x+1);\n               float x3 = xb;\n               float y3 = y_bottom;\n\n               // x = e->x + e->dx * (y-y_top)\n               // (y-y_top) = (x - e->x) / e->dx\n               // y = (x - e->x) / e->dx + y_top\n               float y1 = (x - x0) / dx + y_top;\n               float y2 = (x+1 - x0) / dx + y_top;\n\n               if (x0 < x1 && x3 > x2) {         // three segments descending down-right\n                  stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);\n                  stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x2,y2);\n                  stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);\n               } else if (x3 < x1 && x0 > x2) {  // three segments descending down-left\n                  stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);\n                  stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x1,y1);\n                  stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);\n               } else if (x0 < x1 && x3 > x1) {  // two segments across x, down-right\n                  stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);\n                  stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);\n               } else if (x3 < x1 && x0 > x1) {  // two segments across x, down-left\n                  stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);\n                  stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);\n               } else if (x0 < x2 && x3 > x2) {  // two segments across x+1, down-right\n                  stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);\n                  stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);\n               } else if (x3 < x2 && x0 > x2) {  // two segments across x+1, down-left\n                  stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);\n                  stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);\n               } else {  // one segment\n                  stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x3,y3);\n               }\n            }\n         }\n      }\n      e = e->next;\n   }\n}\n\n// directly AA rasterize edges w/o supersampling\nstatic void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata)\n{\n   stbtt__hheap hh = { 0, 0, 0 };\n   stbtt__active_edge *active = NULL;\n   int y,j=0, i;\n   float scanline_data[129], *scanline, *scanline2;\n\n   STBTT__NOTUSED(vsubsample);\n\n   if (result->w > 64)\n      scanline = (float *) STBTT_malloc((result->w*2+1) * sizeof(float), userdata);\n   else\n      scanline = scanline_data;\n\n   scanline2 = scanline + result->w;\n\n   y = off_y;\n   e[n].y0 = (float) (off_y + result->h) + 1;\n\n   while (j < result->h) {\n      // find center of pixel for this scanline\n      float scan_y_top    = y + 0.0f;\n      float scan_y_bottom = y + 1.0f;\n      stbtt__active_edge **step = &active;\n\n      STBTT_memset(scanline , 0, result->w*sizeof(scanline[0]));\n      STBTT_memset(scanline2, 0, (result->w+1)*sizeof(scanline[0]));\n\n      // update all active edges;\n      // remove all active edges that terminate before the top of this scanline\n      while (*step) {\n         stbtt__active_edge * z = *step;\n         if (z->ey <= scan_y_top) {\n            *step = z->next; // delete from list\n            STBTT_assert(z->direction);\n            z->direction = 0;\n            stbtt__hheap_free(&hh, z);\n         } else {\n            step = &((*step)->next); // advance through list\n         }\n      }\n\n      // insert all edges that start before the bottom of this scanline\n      while (e->y0 <= scan_y_bottom) {\n         if (e->y0 != e->y1) {\n            stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);\n            if (z != NULL) {\n               if (j == 0 && off_y != 0) {\n                  if (z->ey < scan_y_top) {\n                     // this can happen due to subpixel positioning and some kind of fp rounding error i think\n                     z->ey = scan_y_top;\n                  }\n               }\n               STBTT_assert(z->ey >= scan_y_top); // if we get really unlucky a tiny bit of an edge can be out of bounds\n               // insert at front\n               z->next = active;\n               active = z;\n            }\n         }\n         ++e;\n      }\n\n      // now process all active edges\n      if (active)\n         stbtt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);\n\n      {\n         float sum = 0;\n         for (i=0; i < result->w; ++i) {\n            float k;\n            int m;\n            sum += scanline2[i];\n            k = scanline[i] + sum;\n            k = (float) STBTT_fabs(k)*255 + 0.5f;\n            m = (int) k;\n            if (m > 255) m = 255;\n            result->pixels[j*result->stride + i] = (unsigned char) m;\n         }\n      }\n      // advance all the edges\n      step = &active;\n      while (*step) {\n         stbtt__active_edge *z = *step;\n         z->fx += z->fdx; // advance to position for current scanline\n         step = &((*step)->next); // advance through list\n      }\n\n      ++y;\n      ++j;\n   }\n\n   stbtt__hheap_cleanup(&hh, userdata);\n\n   if (scanline != scanline_data)\n      STBTT_free(scanline, userdata);\n}\n#else\n#error \"Unrecognized value of STBTT_RASTERIZER_VERSION\"\n#endif\n\n#define STBTT__COMPARE(a,b)  ((a)->y0 < (b)->y0)\n\nstatic void stbtt__sort_edges_ins_sort(stbtt__edge *p, int n)\n{\n   int i,j;\n   for (i=1; i < n; ++i) {\n      stbtt__edge t = p[i], *a = &t;\n      j = i;\n      while (j > 0) {\n         stbtt__edge *b = &p[j-1];\n         int c = STBTT__COMPARE(a,b);\n         if (!c) break;\n         p[j] = p[j-1];\n         --j;\n      }\n      if (i != j)\n         p[j] = t;\n   }\n}\n\nstatic void stbtt__sort_edges_quicksort(stbtt__edge *p, int n)\n{\n   /* threshold for transitioning to insertion sort */\n   while (n > 12) {\n      stbtt__edge t;\n      int c01,c12,c,m,i,j;\n\n      /* compute median of three */\n      m = n >> 1;\n      c01 = STBTT__COMPARE(&p[0],&p[m]);\n      c12 = STBTT__COMPARE(&p[m],&p[n-1]);\n      /* if 0 >= mid >= end, or 0 < mid < end, then use mid */\n      if (c01 != c12) {\n         /* otherwise, we'll need to swap something else to middle */\n         int z;\n         c = STBTT__COMPARE(&p[0],&p[n-1]);\n         /* 0>mid && mid<n:  0>n => n; 0<n => 0 */\n         /* 0<mid && mid>n:  0>n => 0; 0<n => n */\n         z = (c == c12) ? 0 : n-1;\n         t = p[z];\n         p[z] = p[m];\n         p[m] = t;\n      }\n      /* now p[m] is the median-of-three */\n      /* swap it to the beginning so it won't move around */\n      t = p[0];\n      p[0] = p[m];\n      p[m] = t;\n\n      /* partition loop */\n      i=1;\n      j=n-1;\n      for(;;) {\n         /* handling of equality is crucial here */\n         /* for sentinels & efficiency with duplicates */\n         for (;;++i) {\n            if (!STBTT__COMPARE(&p[i], &p[0])) break;\n         }\n         for (;;--j) {\n            if (!STBTT__COMPARE(&p[0], &p[j])) break;\n         }\n         /* make sure we haven't crossed */\n         if (i >= j) break;\n         t = p[i];\n         p[i] = p[j];\n         p[j] = t;\n\n         ++i;\n         --j;\n      }\n      /* recurse on smaller side, iterate on larger */\n      if (j < (n-i)) {\n         stbtt__sort_edges_quicksort(p,j);\n         p = p+i;\n         n = n-i;\n      } else {\n         stbtt__sort_edges_quicksort(p+i, n-i);\n         n = j;\n      }\n   }\n}\n\nstatic void stbtt__sort_edges(stbtt__edge *p, int n)\n{\n   stbtt__sort_edges_quicksort(p, n);\n   stbtt__sort_edges_ins_sort(p, n);\n}\n\ntypedef struct\n{\n   float x,y;\n} stbtt__point;\n\nstatic void stbtt__rasterize(stbtt__bitmap *result, stbtt__point *pts, int *wcount, int windings, float scale_x, float scale_y, float shift_x, float shift_y, int off_x, int off_y, int invert, void *userdata)\n{\n   float y_scale_inv = invert ? -scale_y : scale_y;\n   stbtt__edge *e;\n   int n,i,j,k,m;\n#if STBTT_RASTERIZER_VERSION == 1\n   int vsubsample = result->h < 8 ? 15 : 5;\n#elif STBTT_RASTERIZER_VERSION == 2\n   int vsubsample = 1;\n#else\n   #error \"Unrecognized value of STBTT_RASTERIZER_VERSION\"\n#endif\n   // vsubsample should divide 255 evenly; otherwise we won't reach full opacity\n\n   // now we have to blow out the windings into explicit edge lists\n   n = 0;\n   for (i=0; i < windings; ++i)\n      n += wcount[i];\n\n   e = (stbtt__edge *) STBTT_malloc(sizeof(*e) * (n+1), userdata); // add an extra one as a sentinel\n   if (e == 0) return;\n   n = 0;\n\n   m=0;\n   for (i=0; i < windings; ++i) {\n      stbtt__point *p = pts + m;\n      m += wcount[i];\n      j = wcount[i]-1;\n      for (k=0; k < wcount[i]; j=k++) {\n         int a=k,b=j;\n         // skip the edge if horizontal\n         if (p[j].y == p[k].y)\n            continue;\n         // add edge from j to k to the list\n         e[n].invert = 0;\n         if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {\n            e[n].invert = 1;\n            a=j,b=k;\n         }\n         e[n].x0 = p[a].x * scale_x + shift_x;\n         e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;\n         e[n].x1 = p[b].x * scale_x + shift_x;\n         e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;\n         ++n;\n      }\n   }\n\n   // now sort the edges by their highest point (should snap to integer, and then by x)\n   //STBTT_sort(e, n, sizeof(e[0]), stbtt__edge_compare);\n   stbtt__sort_edges(e, n);\n\n   // now, traverse the scanlines and find the intersections on each scanline, use xor winding rule\n   stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);\n\n   STBTT_free(e, userdata);\n}\n\nstatic void stbtt__add_point(stbtt__point *points, int n, float x, float y)\n{\n   if (!points) return; // during first pass, it's unallocated\n   points[n].x = x;\n   points[n].y = y;\n}\n\n// tessellate until threshold p is happy... @TODO warped to compensate for non-linear stretching\nstatic int stbtt__tesselate_curve(stbtt__point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float objspace_flatness_squared, int n)\n{\n   // midpoint\n   float mx = (x0 + 2*x1 + x2)/4;\n   float my = (y0 + 2*y1 + y2)/4;\n   // versus directly drawn line\n   float dx = (x0+x2)/2 - mx;\n   float dy = (y0+y2)/2 - my;\n   if (n > 16) // 65536 segments on one curve better be enough!\n      return 1;\n   if (dx*dx+dy*dy > objspace_flatness_squared) { // half-pixel error allowed... need to be smaller if AA\n      stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);\n      stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);\n   } else {\n      stbtt__add_point(points, *num_points,x2,y2);\n      *num_points = *num_points+1;\n   }\n   return 1;\n}\n\nstatic void stbtt__tesselate_cubic(stbtt__point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, float objspace_flatness_squared, int n)\n{\n   // @TODO this \"flatness\" calculation is just made-up nonsense that seems to work well enough\n   float dx0 = x1-x0;\n   float dy0 = y1-y0;\n   float dx1 = x2-x1;\n   float dy1 = y2-y1;\n   float dx2 = x3-x2;\n   float dy2 = y3-y2;\n   float dx = x3-x0;\n   float dy = y3-y0;\n   float longlen = (float) (STBTT_sqrt(dx0*dx0+dy0*dy0)+STBTT_sqrt(dx1*dx1+dy1*dy1)+STBTT_sqrt(dx2*dx2+dy2*dy2));\n   float shortlen = (float) STBTT_sqrt(dx*dx+dy*dy);\n   float flatness_squared = longlen*longlen-shortlen*shortlen;\n\n   if (n > 16) // 65536 segments on one curve better be enough!\n      return;\n\n   if (flatness_squared > objspace_flatness_squared) {\n      float x01 = (x0+x1)/2;\n      float y01 = (y0+y1)/2;\n      float x12 = (x1+x2)/2;\n      float y12 = (y1+y2)/2;\n      float x23 = (x2+x3)/2;\n      float y23 = (y2+y3)/2;\n\n      float xa = (x01+x12)/2;\n      float ya = (y01+y12)/2;\n      float xb = (x12+x23)/2;\n      float yb = (y12+y23)/2;\n\n      float mx = (xa+xb)/2;\n      float my = (ya+yb)/2;\n\n      stbtt__tesselate_cubic(points, num_points, x0,y0, x01,y01, xa,ya, mx,my, objspace_flatness_squared,n+1);\n      stbtt__tesselate_cubic(points, num_points, mx,my, xb,yb, x23,y23, x3,y3, objspace_flatness_squared,n+1);\n   } else {\n      stbtt__add_point(points, *num_points,x3,y3);\n      *num_points = *num_points+1;\n   }\n}\n\n// returns number of contours\nstatic stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata)\n{\n   stbtt__point *points=0;\n   int num_points=0;\n\n   float objspace_flatness_squared = objspace_flatness * objspace_flatness;\n   int i,n=0,start=0, pass;\n\n   // count how many \"moves\" there are to get the contour count\n   for (i=0; i < num_verts; ++i)\n      if (vertices[i].type == STBTT_vmove)\n         ++n;\n\n   *num_contours = n;\n   if (n == 0) return 0;\n\n   *contour_lengths = (int *) STBTT_malloc(sizeof(**contour_lengths) * n, userdata);\n\n   if (*contour_lengths == 0) {\n      *num_contours = 0;\n      return 0;\n   }\n\n   // make two passes through the points so we don't need to realloc\n   for (pass=0; pass < 2; ++pass) {\n      float x=0,y=0;\n      if (pass == 1) {\n         points = (stbtt__point *) STBTT_malloc(num_points * sizeof(points[0]), userdata);\n         if (points == NULL) goto error;\n      }\n      num_points = 0;\n      n= -1;\n      for (i=0; i < num_verts; ++i) {\n         switch (vertices[i].type) {\n            case STBTT_vmove:\n               // start the next contour\n               if (n >= 0)\n                  (*contour_lengths)[n] = num_points - start;\n               ++n;\n               start = num_points;\n\n               x = vertices[i].x, y = vertices[i].y;\n               stbtt__add_point(points, num_points++, x,y);\n               break;\n            case STBTT_vline:\n               x = vertices[i].x, y = vertices[i].y;\n               stbtt__add_point(points, num_points++, x, y);\n               break;\n            case STBTT_vcurve:\n               stbtt__tesselate_curve(points, &num_points, x,y,\n                                        vertices[i].cx, vertices[i].cy,\n                                        vertices[i].x,  vertices[i].y,\n                                        objspace_flatness_squared, 0);\n               x = vertices[i].x, y = vertices[i].y;\n               break;\n            case STBTT_vcubic:\n               stbtt__tesselate_cubic(points, &num_points, x,y,\n                                        vertices[i].cx, vertices[i].cy,\n                                        vertices[i].cx1, vertices[i].cy1,\n                                        vertices[i].x,  vertices[i].y,\n                                        objspace_flatness_squared, 0);\n               x = vertices[i].x, y = vertices[i].y;\n               break;\n         }\n      }\n      (*contour_lengths)[n] = num_points - start;\n   }\n\n   return points;\nerror:\n   STBTT_free(points, userdata);\n   STBTT_free(*contour_lengths, userdata);\n   *contour_lengths = 0;\n   *num_contours = 0;\n   return NULL;\n}\n\nSTBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata)\n{\n   float scale            = scale_x > scale_y ? scale_y : scale_x;\n   int winding_count      = 0;\n   int *winding_lengths   = NULL;\n   stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);\n   if (windings) {\n      stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata);\n      STBTT_free(winding_lengths, userdata);\n      STBTT_free(windings, userdata);\n   }\n}\n\nSTBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata)\n{\n   STBTT_free(bitmap, userdata);\n}\n\nSTBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff)\n{\n   int ix0,iy0,ix1,iy1;\n   stbtt__bitmap gbm;\n   stbtt_vertex *vertices;   \n   int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);\n\n   if (scale_x == 0) scale_x = scale_y;\n   if (scale_y == 0) {\n      if (scale_x == 0) {\n         STBTT_free(vertices, info->userdata);\n         return NULL;\n      }\n      scale_y = scale_x;\n   }\n\n   stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1);\n\n   // now we get the size\n   gbm.w = (ix1 - ix0);\n   gbm.h = (iy1 - iy0);\n   gbm.pixels = NULL; // in case we error\n\n   if (width ) *width  = gbm.w;\n   if (height) *height = gbm.h;\n   if (xoff  ) *xoff   = ix0;\n   if (yoff  ) *yoff   = iy0;\n   \n   if (gbm.w && gbm.h) {\n      gbm.pixels = (unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata);\n      if (gbm.pixels) {\n         gbm.stride = gbm.w;\n\n         stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);\n      }\n   }\n   STBTT_free(vertices, info->userdata);\n   return gbm.pixels;\n}   \n\nSTBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff)\n{\n   return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);\n}\n\nSTBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph)\n{\n   int ix0,iy0;\n   stbtt_vertex *vertices;\n   int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);\n   stbtt__bitmap gbm;   \n\n   stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0);\n   gbm.pixels = output;\n   gbm.w = out_w;\n   gbm.h = out_h;\n   gbm.stride = out_stride;\n\n   if (gbm.w && gbm.h)\n      stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->userdata);\n\n   STBTT_free(vertices, info->userdata);\n}\n\nSTBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph)\n{\n   stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph);\n}\n\nSTBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff)\n{\n   return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff);\n}   \n\nSTBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int codepoint)\n{\n   stbtt_MakeGlyphBitmapSubpixelPrefilter(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, oversample_x, oversample_y, sub_x, sub_y, stbtt_FindGlyphIndex(info,codepoint));\n}\n\nSTBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint)\n{\n   stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint));\n}\n\nSTBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff)\n{\n   return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff);\n}   \n\nSTBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint)\n{\n   stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint);\n}\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// bitmap baking\n//\n// This is SUPER-CRAPPY packing to keep source code small\n\nstatic int stbtt_BakeFontBitmap_internal(unsigned char *data, int offset,  // font location (use offset=0 for plain .ttf)\n                                float pixel_height,                     // height of font in pixels\n                                unsigned char *pixels, int pw, int ph,  // bitmap to be filled in\n                                int first_char, int num_chars,          // characters to bake\n                                stbtt_bakedchar *chardata)\n{\n   float scale;\n   int x,y,bottom_y, i;\n   stbtt_fontinfo f;\n   f.userdata = NULL;\n   if (!stbtt_InitFont(&f, data, offset))\n      return -1;\n   STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels\n   x=y=1;\n   bottom_y = 1;\n\n   scale = stbtt_ScaleForPixelHeight(&f, pixel_height);\n\n   for (i=0; i < num_chars; ++i) {\n      int advance, lsb, x0,y0,x1,y1,gw,gh;\n      int g = stbtt_FindGlyphIndex(&f, first_char + i);\n      stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);\n      stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1);\n      gw = x1-x0;\n      gh = y1-y0;\n      if (x + gw + 1 >= pw)\n         y = bottom_y, x = 1; // advance to next row\n      if (y + gh + 1 >= ph) // check if it fits vertically AFTER potentially moving to next row\n         return -i;\n      STBTT_assert(x+gw < pw);\n      STBTT_assert(y+gh < ph);\n      stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g);\n      chardata[i].x0 = (stbtt_int16) x;\n      chardata[i].y0 = (stbtt_int16) y;\n      chardata[i].x1 = (stbtt_int16) (x + gw);\n      chardata[i].y1 = (stbtt_int16) (y + gh);\n      chardata[i].xadvance = scale * advance;\n      chardata[i].xoff     = (float) x0;\n      chardata[i].yoff     = (float) y0;\n      x = x + gw + 1;\n      if (y+gh+1 > bottom_y)\n         bottom_y = y+gh+1;\n   }\n   return bottom_y;\n}\n\nSTBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int opengl_fillrule)\n{\n   float d3d_bias = opengl_fillrule ? 0 : -0.5f;\n   float ipw = 1.0f / pw, iph = 1.0f / ph;\n   const stbtt_bakedchar *b = chardata + char_index;\n   int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f);\n   int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f);\n\n   q->x0 = round_x + d3d_bias;\n   q->y0 = round_y + d3d_bias;\n   q->x1 = round_x + b->x1 - b->x0 + d3d_bias;\n   q->y1 = round_y + b->y1 - b->y0 + d3d_bias;\n\n   q->s0 = b->x0 * ipw;\n   q->t0 = b->y0 * iph;\n   q->s1 = b->x1 * ipw;\n   q->t1 = b->y1 * iph;\n\n   *xpos += b->xadvance;\n}\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// rectangle packing replacement routines if you don't have stb_rect_pack.h\n//\n\n#ifndef STB_RECT_PACK_VERSION\n\ntypedef int stbrp_coord;\n\n////////////////////////////////////////////////////////////////////////////////////\n//                                                                                //\n//                                                                                //\n// COMPILER WARNING ?!?!?                                                         //\n//                                                                                //\n//                                                                                //\n// if you get a compile warning due to these symbols being defined more than      //\n// once, move #include \"stb_rect_pack.h\" before #include \"stb_truetype.h\"         //\n//                                                                                //\n////////////////////////////////////////////////////////////////////////////////////\n\ntypedef struct\n{\n   int width,height;\n   int x,y,bottom_y;\n} stbrp_context;\n\ntypedef struct\n{\n   unsigned char x;\n} stbrp_node;\n\nstruct stbrp_rect\n{\n   stbrp_coord x,y;\n   int id,w,h,was_packed;\n};\n\nstatic void stbrp_init_target(stbrp_context *con, int pw, int ph, stbrp_node *nodes, int num_nodes)\n{\n   con->width  = pw;\n   con->height = ph;\n   con->x = 0;\n   con->y = 0;\n   con->bottom_y = 0;\n   STBTT__NOTUSED(nodes);\n   STBTT__NOTUSED(num_nodes);   \n}\n\nstatic void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects, int num_rects)\n{\n   int i;\n   for (i=0; i < num_rects; ++i) {\n      if (con->x + rects[i].w > con->width) {\n         con->x = 0;\n         con->y = con->bottom_y;\n      }\n      if (con->y + rects[i].h > con->height)\n         break;\n      rects[i].x = con->x;\n      rects[i].y = con->y;\n      rects[i].was_packed = 1;\n      con->x += rects[i].w;\n      if (con->y + rects[i].h > con->bottom_y)\n         con->bottom_y = con->y + rects[i].h;\n   }\n   for (   ; i < num_rects; ++i)\n      rects[i].was_packed = 0;\n}\n#endif\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// bitmap baking\n//\n// This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If\n// stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy.\n\nSTBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context)\n{\n   stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context)            ,alloc_context);\n   int            num_nodes = pw - padding;\n   stbrp_node    *nodes   = (stbrp_node    *) STBTT_malloc(sizeof(*nodes  ) * num_nodes,alloc_context);\n\n   if (context == NULL || nodes == NULL) {\n      if (context != NULL) STBTT_free(context, alloc_context);\n      if (nodes   != NULL) STBTT_free(nodes  , alloc_context);\n      return 0;\n   }\n\n   spc->user_allocator_context = alloc_context;\n   spc->width = pw;\n   spc->height = ph;\n   spc->pixels = pixels;\n   spc->pack_info = context;\n   spc->nodes = nodes;\n   spc->padding = padding;\n   spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;\n   spc->h_oversample = 1;\n   spc->v_oversample = 1;\n   spc->skip_missing = 0;\n\n   stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);\n\n   if (pixels)\n      STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels\n\n   return 1;\n}\n\nSTBTT_DEF void stbtt_PackEnd  (stbtt_pack_context *spc)\n{\n   STBTT_free(spc->nodes    , spc->user_allocator_context);\n   STBTT_free(spc->pack_info, spc->user_allocator_context);\n}\n\nSTBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample)\n{\n   STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);\n   STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);\n   if (h_oversample <= STBTT_MAX_OVERSAMPLE)\n      spc->h_oversample = h_oversample;\n   if (v_oversample <= STBTT_MAX_OVERSAMPLE)\n      spc->v_oversample = v_oversample;\n}\n\nSTBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip)\n{\n   spc->skip_missing = skip;\n}\n\n#define STBTT__OVER_MASK  (STBTT_MAX_OVERSAMPLE-1)\n\nstatic void stbtt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)\n{\n   unsigned char buffer[STBTT_MAX_OVERSAMPLE];\n   int safe_w = w - kernel_width;\n   int j;\n   STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); // suppress bogus warning from VS2013 -analyze\n   for (j=0; j < h; ++j) {\n      int i;\n      unsigned int total;\n      STBTT_memset(buffer, 0, kernel_width);\n\n      total = 0;\n\n      // make kernel_width a constant in common cases so compiler can optimize out the divide\n      switch (kernel_width) {\n         case 2:\n            for (i=0; i <= safe_w; ++i) {\n               total += pixels[i] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];\n               pixels[i] = (unsigned char) (total / 2);\n            }\n            break;\n         case 3:\n            for (i=0; i <= safe_w; ++i) {\n               total += pixels[i] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];\n               pixels[i] = (unsigned char) (total / 3);\n            }\n            break;\n         case 4:\n            for (i=0; i <= safe_w; ++i) {\n               total += pixels[i] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];\n               pixels[i] = (unsigned char) (total / 4);\n            }\n            break;\n         case 5:\n            for (i=0; i <= safe_w; ++i) {\n               total += pixels[i] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];\n               pixels[i] = (unsigned char) (total / 5);\n            }\n            break;\n         default:\n            for (i=0; i <= safe_w; ++i) {\n               total += pixels[i] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];\n               pixels[i] = (unsigned char) (total / kernel_width);\n            }\n            break;\n      }\n\n      for (; i < w; ++i) {\n         STBTT_assert(pixels[i] == 0);\n         total -= buffer[i & STBTT__OVER_MASK];\n         pixels[i] = (unsigned char) (total / kernel_width);\n      }\n\n      pixels += stride_in_bytes;\n   }\n}\n\nstatic void stbtt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)\n{\n   unsigned char buffer[STBTT_MAX_OVERSAMPLE];\n   int safe_h = h - kernel_width;\n   int j;\n   STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); // suppress bogus warning from VS2013 -analyze\n   for (j=0; j < w; ++j) {\n      int i;\n      unsigned int total;\n      STBTT_memset(buffer, 0, kernel_width);\n\n      total = 0;\n\n      // make kernel_width a constant in common cases so compiler can optimize out the divide\n      switch (kernel_width) {\n         case 2:\n            for (i=0; i <= safe_h; ++i) {\n               total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];\n               pixels[i*stride_in_bytes] = (unsigned char) (total / 2);\n            }\n            break;\n         case 3:\n            for (i=0; i <= safe_h; ++i) {\n               total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];\n               pixels[i*stride_in_bytes] = (unsigned char) (total / 3);\n            }\n            break;\n         case 4:\n            for (i=0; i <= safe_h; ++i) {\n               total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];\n               pixels[i*stride_in_bytes] = (unsigned char) (total / 4);\n            }\n            break;\n         case 5:\n            for (i=0; i <= safe_h; ++i) {\n               total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];\n               pixels[i*stride_in_bytes] = (unsigned char) (total / 5);\n            }\n            break;\n         default:\n            for (i=0; i <= safe_h; ++i) {\n               total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];\n               buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];\n               pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width);\n            }\n            break;\n      }\n\n      for (; i < h; ++i) {\n         STBTT_assert(pixels[i*stride_in_bytes] == 0);\n         total -= buffer[i & STBTT__OVER_MASK];\n         pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width);\n      }\n\n      pixels += 1;\n   }\n}\n\nstatic float stbtt__oversample_shift(int oversample)\n{\n   if (!oversample)\n      return 0.0f;\n\n   // The prefilter is a box filter of width \"oversample\",\n   // which shifts phase by (oversample - 1)/2 pixels in\n   // oversampled space. We want to shift in the opposite\n   // direction to counter this.\n   return (float)-(oversample - 1) / (2.0f * (float)oversample);\n}\n\n// rects array must be big enough to accommodate all characters in the given ranges\nSTBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)\n{\n   int i,j,k;\n\n   k=0;\n   for (i=0; i < num_ranges; ++i) {\n      float fh = ranges[i].font_size;\n      float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);\n      ranges[i].h_oversample = (unsigned char) spc->h_oversample;\n      ranges[i].v_oversample = (unsigned char) spc->v_oversample;\n      for (j=0; j < ranges[i].num_chars; ++j) {\n         int x0,y0,x1,y1;\n         int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];\n         int glyph = stbtt_FindGlyphIndex(info, codepoint);\n         if (glyph == 0 && spc->skip_missing) {\n            rects[k].w = rects[k].h = 0;\n         } else {\n            stbtt_GetGlyphBitmapBoxSubpixel(info,glyph,\n                                            scale * spc->h_oversample,\n                                            scale * spc->v_oversample,\n                                            0,0,\n                                            &x0,&y0,&x1,&y1);\n            rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1);\n            rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1);\n         }\n         ++k;\n      }\n   }\n\n   return k;\n}\n\nSTBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int prefilter_x, int prefilter_y, float *sub_x, float *sub_y, int glyph)\n{\n   stbtt_MakeGlyphBitmapSubpixel(info,\n                                 output,\n                                 out_w - (prefilter_x - 1),\n                                 out_h - (prefilter_y - 1),\n                                 out_stride,\n                                 scale_x,\n                                 scale_y,\n                                 shift_x,\n                                 shift_y,\n                                 glyph);\n\n   if (prefilter_x > 1)\n      stbtt__h_prefilter(output, out_w, out_h, out_stride, prefilter_x);\n\n   if (prefilter_y > 1)\n      stbtt__v_prefilter(output, out_w, out_h, out_stride, prefilter_y);\n\n   *sub_x = stbtt__oversample_shift(prefilter_x);\n   *sub_y = stbtt__oversample_shift(prefilter_y);\n}\n\n// rects array must be big enough to accommodate all characters in the given ranges\nSTBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)\n{\n   int i,j,k, return_value = 1;\n\n   // save current values\n   int old_h_over = spc->h_oversample;\n   int old_v_over = spc->v_oversample;\n\n   k = 0;\n   for (i=0; i < num_ranges; ++i) {\n      float fh = ranges[i].font_size;\n      float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);\n      float recip_h,recip_v,sub_x,sub_y;\n      spc->h_oversample = ranges[i].h_oversample;\n      spc->v_oversample = ranges[i].v_oversample;\n      recip_h = 1.0f / spc->h_oversample;\n      recip_v = 1.0f / spc->v_oversample;\n      sub_x = stbtt__oversample_shift(spc->h_oversample);\n      sub_y = stbtt__oversample_shift(spc->v_oversample);\n      for (j=0; j < ranges[i].num_chars; ++j) {\n         stbrp_rect *r = &rects[k];\n         if (r->was_packed && r->w != 0 && r->h != 0) {\n            stbtt_packedchar *bc = &ranges[i].chardata_for_range[j];\n            int advance, lsb, x0,y0,x1,y1;\n            int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];\n            int glyph = stbtt_FindGlyphIndex(info, codepoint);\n            stbrp_coord pad = (stbrp_coord) spc->padding;\n\n            // pad on left and top\n            r->x += pad;\n            r->y += pad;\n            r->w -= pad;\n            r->h -= pad;\n            stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb);\n            stbtt_GetGlyphBitmapBox(info, glyph,\n                                    scale * spc->h_oversample,\n                                    scale * spc->v_oversample,\n                                    &x0,&y0,&x1,&y1);\n            stbtt_MakeGlyphBitmapSubpixel(info,\n                                          spc->pixels + r->x + r->y*spc->stride_in_bytes,\n                                          r->w - spc->h_oversample+1,\n                                          r->h - spc->v_oversample+1,\n                                          spc->stride_in_bytes,\n                                          scale * spc->h_oversample,\n                                          scale * spc->v_oversample,\n                                          0,0,\n                                          glyph);\n\n            if (spc->h_oversample > 1)\n               stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,\n                                  r->w, r->h, spc->stride_in_bytes,\n                                  spc->h_oversample);\n\n            if (spc->v_oversample > 1)\n               stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,\n                                  r->w, r->h, spc->stride_in_bytes,\n                                  spc->v_oversample);\n\n            bc->x0       = (stbtt_int16)  r->x;\n            bc->y0       = (stbtt_int16)  r->y;\n            bc->x1       = (stbtt_int16) (r->x + r->w);\n            bc->y1       = (stbtt_int16) (r->y + r->h);\n            bc->xadvance =                scale * advance;\n            bc->xoff     =       (float)  x0 * recip_h + sub_x;\n            bc->yoff     =       (float)  y0 * recip_v + sub_y;\n            bc->xoff2    =                (x0 + r->w) * recip_h + sub_x;\n            bc->yoff2    =                (y0 + r->h) * recip_v + sub_y;\n         } else {\n            return_value = 0; // if any fail, report failure\n         }\n\n         ++k;\n      }\n   }\n\n   // restore original values\n   spc->h_oversample = old_h_over;\n   spc->v_oversample = old_v_over;\n\n   return return_value;\n}\n\nSTBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects)\n{\n   stbrp_pack_rects((stbrp_context *) spc->pack_info, rects, num_rects);\n}\n\nSTBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)\n{\n   stbtt_fontinfo info;\n   int i,j,n, return_value = 1;\n   //stbrp_context *context = (stbrp_context *) spc->pack_info;\n   stbrp_rect    *rects;\n\n   // flag all characters as NOT packed\n   for (i=0; i < num_ranges; ++i)\n      for (j=0; j < ranges[i].num_chars; ++j)\n         ranges[i].chardata_for_range[j].x0 =\n         ranges[i].chardata_for_range[j].y0 =\n         ranges[i].chardata_for_range[j].x1 =\n         ranges[i].chardata_for_range[j].y1 = 0;\n\n   n = 0;\n   for (i=0; i < num_ranges; ++i)\n      n += ranges[i].num_chars;\n         \n   rects = (stbrp_rect *) STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context);\n   if (rects == NULL)\n      return 0;\n\n   info.userdata = spc->user_allocator_context;\n   stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index));\n\n   n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects);\n\n   stbtt_PackFontRangesPackRects(spc, rects, n);\n  \n   return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects);\n\n   STBTT_free(rects, spc->user_allocator_context);\n   return return_value;\n}\n\nSTBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, float font_size,\n            int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range)\n{\n   stbtt_pack_range range;\n   range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range;\n   range.array_of_unicode_codepoints = NULL;\n   range.num_chars                   = num_chars_in_range;\n   range.chardata_for_range          = chardata_for_range;\n   range.font_size                   = font_size;\n   return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);\n}\n\nSTBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char *fontdata, int index, float size, float *ascent, float *descent, float *lineGap)\n{\n   int i_ascent, i_descent, i_lineGap;\n   float scale;\n   stbtt_fontinfo info;\n   stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, index));\n   scale = size > 0 ? stbtt_ScaleForPixelHeight(&info, size) : stbtt_ScaleForMappingEmToPixels(&info, -size);\n   stbtt_GetFontVMetrics(&info, &i_ascent, &i_descent, &i_lineGap);\n   *ascent  = (float) i_ascent  * scale;\n   *descent = (float) i_descent * scale;\n   *lineGap = (float) i_lineGap * scale;\n}\n\nSTBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int align_to_integer)\n{\n   float ipw = 1.0f / pw, iph = 1.0f / ph;\n   const stbtt_packedchar *b = chardata + char_index;\n\n   if (align_to_integer) {\n      float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5f);\n      float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5f);\n      q->x0 = x;\n      q->y0 = y;\n      q->x1 = x + b->xoff2 - b->xoff;\n      q->y1 = y + b->yoff2 - b->yoff;\n   } else {\n      q->x0 = *xpos + b->xoff;\n      q->y0 = *ypos + b->yoff;\n      q->x1 = *xpos + b->xoff2;\n      q->y1 = *ypos + b->yoff2;\n   }\n\n   q->s0 = b->x0 * ipw;\n   q->t0 = b->y0 * iph;\n   q->s1 = b->x1 * ipw;\n   q->t1 = b->y1 * iph;\n\n   *xpos += b->xadvance;\n}\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// sdf computation\n//\n\n#define STBTT_min(a,b)  ((a) < (b) ? (a) : (b))\n#define STBTT_max(a,b)  ((a) < (b) ? (b) : (a))\n\nstatic int stbtt__ray_intersect_bezier(float orig[2], float ray[2], float q0[2], float q1[2], float q2[2], float hits[2][2])\n{\n   float q0perp = q0[1]*ray[0] - q0[0]*ray[1];\n   float q1perp = q1[1]*ray[0] - q1[0]*ray[1];\n   float q2perp = q2[1]*ray[0] - q2[0]*ray[1];\n   float roperp = orig[1]*ray[0] - orig[0]*ray[1];\n\n   float a = q0perp - 2*q1perp + q2perp;\n   float b = q1perp - q0perp;\n   float c = q0perp - roperp;\n\n   float s0 = 0., s1 = 0.;\n   int num_s = 0;\n\n   if (a != 0.0) {\n      float discr = b*b - a*c;\n      if (discr > 0.0) {\n         float rcpna = -1 / a;\n         float d = (float) STBTT_sqrt(discr);\n         s0 = (b+d) * rcpna;\n         s1 = (b-d) * rcpna;\n         if (s0 >= 0.0 && s0 <= 1.0)\n            num_s = 1;\n         if (d > 0.0 && s1 >= 0.0 && s1 <= 1.0) {\n            if (num_s == 0) s0 = s1;\n            ++num_s;\n         }\n      }\n   } else {\n      // 2*b*s + c = 0\n      // s = -c / (2*b)\n      s0 = c / (-2 * b);\n      if (s0 >= 0.0 && s0 <= 1.0)\n         num_s = 1;\n   }\n\n   if (num_s == 0)\n      return 0;\n   else {\n      float rcp_len2 = 1 / (ray[0]*ray[0] + ray[1]*ray[1]);\n      float rayn_x = ray[0] * rcp_len2, rayn_y = ray[1] * rcp_len2;\n\n      float q0d =   q0[0]*rayn_x +   q0[1]*rayn_y;\n      float q1d =   q1[0]*rayn_x +   q1[1]*rayn_y;\n      float q2d =   q2[0]*rayn_x +   q2[1]*rayn_y;\n      float rod = orig[0]*rayn_x + orig[1]*rayn_y;\n\n      float q10d = q1d - q0d;\n      float q20d = q2d - q0d;\n      float q0rd = q0d - rod;\n\n      hits[0][0] = q0rd + s0*(2.0f - 2.0f*s0)*q10d + s0*s0*q20d;\n      hits[0][1] = a*s0+b;\n\n      if (num_s > 1) {\n         hits[1][0] = q0rd + s1*(2.0f - 2.0f*s1)*q10d + s1*s1*q20d;\n         hits[1][1] = a*s1+b;\n         return 2;\n      } else {\n         return 1;\n      }\n   }\n}\n\nstatic int equal(float *a, float *b)\n{\n   return (a[0] == b[0] && a[1] == b[1]);\n}\n\nstatic int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex *verts)\n{\n   int i;\n   float orig[2], ray[2] = { 1, 0 };\n   float y_frac;\n   int winding = 0;\n\n   orig[0] = x;\n   //orig[1] = y; // [DEAR IMGUI] commmented double assignment\n\n   // make sure y never passes through a vertex of the shape\n   y_frac = (float) STBTT_fmod(y, 1.0f);\n   if (y_frac < 0.01f)\n      y += 0.01f;\n   else if (y_frac > 0.99f)\n      y -= 0.01f;\n   orig[1] = y;\n\n   // test a ray from (-infinity,y) to (x,y)\n   for (i=0; i < nverts; ++i) {\n      if (verts[i].type == STBTT_vline) {\n         int x0 = (int) verts[i-1].x, y0 = (int) verts[i-1].y;\n         int x1 = (int) verts[i  ].x, y1 = (int) verts[i  ].y;\n         if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) {\n            float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0;\n            if (x_inter < x)  \n               winding += (y0 < y1) ? 1 : -1;\n         }\n      }\n      if (verts[i].type == STBTT_vcurve) {\n         int x0 = (int) verts[i-1].x , y0 = (int) verts[i-1].y ;\n         int x1 = (int) verts[i  ].cx, y1 = (int) verts[i  ].cy;\n         int x2 = (int) verts[i  ].x , y2 = (int) verts[i  ].y ;\n         int ax = STBTT_min(x0,STBTT_min(x1,x2)), ay = STBTT_min(y0,STBTT_min(y1,y2));\n         int by = STBTT_max(y0,STBTT_max(y1,y2));\n         if (y > ay && y < by && x > ax) {\n            float q0[2],q1[2],q2[2];\n            float hits[2][2];\n            q0[0] = (float)x0;\n            q0[1] = (float)y0;\n            q1[0] = (float)x1;\n            q1[1] = (float)y1;\n            q2[0] = (float)x2;\n            q2[1] = (float)y2;\n            if (equal(q0,q1) || equal(q1,q2)) {\n               x0 = (int)verts[i-1].x;\n               y0 = (int)verts[i-1].y;\n               x1 = (int)verts[i  ].x;\n               y1 = (int)verts[i  ].y;\n               if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) {\n                  float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0;\n                  if (x_inter < x)  \n                     winding += (y0 < y1) ? 1 : -1;\n               }\n            } else {\n               int num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits);\n               if (num_hits >= 1)\n                  if (hits[0][0] < 0)\n                     winding += (hits[0][1] < 0 ? -1 : 1);\n               if (num_hits >= 2)\n                  if (hits[1][0] < 0)\n                     winding += (hits[1][1] < 0 ? -1 : 1);\n            }\n         } \n      }\n   }\n   return winding;\n}\n\nstatic float stbtt__cuberoot( float x )\n{\n   if (x<0)\n      return -(float) STBTT_pow(-x,1.0f/3.0f);\n   else\n      return  (float) STBTT_pow( x,1.0f/3.0f);\n}\n\n// x^3 + c*x^2 + b*x + a = 0\nstatic int stbtt__solve_cubic(float a, float b, float c, float* r)\n{\n\tfloat s = -a / 3;\n\tfloat p = b - a*a / 3;\n\tfloat q = a * (2*a*a - 9*b) / 27 + c;\n   float p3 = p*p*p;\n\tfloat d = q*q + 4*p3 / 27;\n\tif (d >= 0) {\n\t\tfloat z = (float) STBTT_sqrt(d);\n\t\tfloat u = (-q + z) / 2;\n\t\tfloat v = (-q - z) / 2;\n\t\tu = stbtt__cuberoot(u);\n\t\tv = stbtt__cuberoot(v);\n\t\tr[0] = s + u + v;\n\t\treturn 1;\n\t} else {\n\t   float u = (float) STBTT_sqrt(-p/3);\n\t   float v = (float) STBTT_acos(-STBTT_sqrt(-27/p3) * q / 2) / 3; // p3 must be negative, since d is negative\n\t   float m = (float) STBTT_cos(v);\n      float n = (float) STBTT_cos(v-3.141592/2)*1.732050808f;\n\t   r[0] = s + u * 2 * m;\n\t   r[1] = s - u * (m + n);\n\t   r[2] = s - u * (m - n);\n\n      //STBTT_assert( STBTT_fabs(((r[0]+a)*r[0]+b)*r[0]+c) < 0.05f);  // these asserts may not be safe at all scales, though they're in bezier t parameter units so maybe?\n      //STBTT_assert( STBTT_fabs(((r[1]+a)*r[1]+b)*r[1]+c) < 0.05f);\n      //STBTT_assert( STBTT_fabs(((r[2]+a)*r[2]+b)*r[2]+c) < 0.05f);\n   \treturn 3;\n   }\n}\n\nSTBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float scale, int glyph, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff)\n{\n   float scale_x = scale, scale_y = scale;\n   int ix0,iy0,ix1,iy1;\n   int w,h;\n   unsigned char *data;\n\n   // if one scale is 0, use same scale for both\n   if (scale_x == 0) scale_x = scale_y;\n   if (scale_y == 0) {\n      if (scale_x == 0) return NULL;  // if both scales are 0, return NULL\n      scale_y = scale_x;\n   }\n\n   stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f,0.0f, &ix0,&iy0,&ix1,&iy1);\n\n   // if empty, return NULL\n   if (ix0 == ix1 || iy0 == iy1)\n      return NULL;\n\n   ix0 -= padding;\n   iy0 -= padding;\n   ix1 += padding;\n   iy1 += padding;\n\n   w = (ix1 - ix0);\n   h = (iy1 - iy0);\n\n   if (width ) *width  = w;\n   if (height) *height = h;\n   if (xoff  ) *xoff   = ix0;\n   if (yoff  ) *yoff   = iy0;\n\n   // invert for y-downwards bitmaps\n   scale_y = -scale_y;\n      \n   {\n      int x,y,i,j;\n      float *precompute;\n      stbtt_vertex *verts;\n      int num_verts = stbtt_GetGlyphShape(info, glyph, &verts);\n      data = (unsigned char *) STBTT_malloc(w * h, info->userdata);\n      precompute = (float *) STBTT_malloc(num_verts * sizeof(float), info->userdata);\n\n      for (i=0,j=num_verts-1; i < num_verts; j=i++) {\n         if (verts[i].type == STBTT_vline) {\n            float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y;\n            float x1 = verts[j].x*scale_x, y1 = verts[j].y*scale_y;\n            float dist = (float) STBTT_sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));\n            precompute[i] = (dist == 0) ? 0.0f : 1.0f / dist;\n         } else if (verts[i].type == STBTT_vcurve) {\n            float x2 = verts[j].x *scale_x, y2 = verts[j].y *scale_y;\n            float x1 = verts[i].cx*scale_x, y1 = verts[i].cy*scale_y;\n            float x0 = verts[i].x *scale_x, y0 = verts[i].y *scale_y;\n            float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2;\n            float len2 = bx*bx + by*by;\n            if (len2 != 0.0f)\n               precompute[i] = 1.0f / (bx*bx + by*by);\n            else\n               precompute[i] = 0.0f;\n         } else\n            precompute[i] = 0.0f;\n      }\n\n      for (y=iy0; y < iy1; ++y) {\n         for (x=ix0; x < ix1; ++x) {\n            float val;\n            float min_dist = 999999.0f;\n            float sx = (float) x + 0.5f;\n            float sy = (float) y + 0.5f;\n            float x_gspace = (sx / scale_x);\n            float y_gspace = (sy / scale_y);\n\n            int winding = stbtt__compute_crossings_x(x_gspace, y_gspace, num_verts, verts); // @OPTIMIZE: this could just be a rasterization, but needs to be line vs. non-tesselated curves so a new path\n\n            for (i=0; i < num_verts; ++i) {\n               float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y;\n\n               // check against every point here rather than inside line/curve primitives -- @TODO: wrong if multiple 'moves' in a row produce a garbage point, and given culling, probably more efficient to do within line/curve\n               float dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy);\n               if (dist2 < min_dist*min_dist)\n                  min_dist = (float) STBTT_sqrt(dist2);\n\n               if (verts[i].type == STBTT_vline) {\n                  float x1 = verts[i-1].x*scale_x, y1 = verts[i-1].y*scale_y;\n\n                  // coarse culling against bbox\n                  //if (sx > STBTT_min(x0,x1)-min_dist && sx < STBTT_max(x0,x1)+min_dist &&\n                  //    sy > STBTT_min(y0,y1)-min_dist && sy < STBTT_max(y0,y1)+min_dist)\n                  float dist = (float) STBTT_fabs((x1-x0)*(y0-sy) - (y1-y0)*(x0-sx)) * precompute[i];\n                  STBTT_assert(i != 0);\n                  if (dist < min_dist) {\n                     // check position along line\n                     // x' = x0 + t*(x1-x0), y' = y0 + t*(y1-y0)\n                     // minimize (x'-sx)*(x'-sx)+(y'-sy)*(y'-sy)\n                     float dx = x1-x0, dy = y1-y0;\n                     float px = x0-sx, py = y0-sy;\n                     // minimize (px+t*dx)^2 + (py+t*dy)^2 = px*px + 2*px*dx*t + t^2*dx*dx + py*py + 2*py*dy*t + t^2*dy*dy\n                     // derivative: 2*px*dx + 2*py*dy + (2*dx*dx+2*dy*dy)*t, set to 0 and solve\n                     float t = -(px*dx + py*dy) / (dx*dx + dy*dy);\n                     if (t >= 0.0f && t <= 1.0f)\n                        min_dist = dist;\n                  }\n               } else if (verts[i].type == STBTT_vcurve) {\n                  float x2 = verts[i-1].x *scale_x, y2 = verts[i-1].y *scale_y;\n                  float x1 = verts[i  ].cx*scale_x, y1 = verts[i  ].cy*scale_y;\n                  float box_x0 = STBTT_min(STBTT_min(x0,x1),x2);\n                  float box_y0 = STBTT_min(STBTT_min(y0,y1),y2);\n                  float box_x1 = STBTT_max(STBTT_max(x0,x1),x2);\n                  float box_y1 = STBTT_max(STBTT_max(y0,y1),y2);\n                  // coarse culling against bbox to avoid computing cubic unnecessarily\n                  if (sx > box_x0-min_dist && sx < box_x1+min_dist && sy > box_y0-min_dist && sy < box_y1+min_dist) {\n                     int num=0;\n                     float ax = x1-x0, ay = y1-y0;\n                     float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2;\n                     float mx = x0 - sx, my = y0 - sy;\n                     float res[3],px,py,t,it;\n                     float a_inv = precompute[i];\n                     if (a_inv == 0.0) { // if a_inv is 0, it's 2nd degree so use quadratic formula\n                        float a = 3*(ax*bx + ay*by);\n                        float b = 2*(ax*ax + ay*ay) + (mx*bx+my*by);\n                        float c = mx*ax+my*ay;\n                        if (a == 0.0) { // if a is 0, it's linear\n                           if (b != 0.0) {\n                              res[num++] = -c/b;\n                           }\n                        } else {\n                           float discriminant = b*b - 4*a*c;\n                           if (discriminant < 0)\n                              num = 0;\n                           else {\n                              float root = (float) STBTT_sqrt(discriminant);\n                              res[0] = (-b - root)/(2*a);\n                              res[1] = (-b + root)/(2*a);\n                              num = 2; // don't bother distinguishing 1-solution case, as code below will still work\n                           }\n                        }\n                     } else {\n                        float b = 3*(ax*bx + ay*by) * a_inv; // could precompute this as it doesn't depend on sample point\n                        float c = (2*(ax*ax + ay*ay) + (mx*bx+my*by)) * a_inv;\n                        float d = (mx*ax+my*ay) * a_inv;\n                        num = stbtt__solve_cubic(b, c, d, res);\n                     }\n                     if (num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) {\n                        t = res[0], it = 1.0f - t;\n                        px = it*it*x0 + 2*t*it*x1 + t*t*x2;\n                        py = it*it*y0 + 2*t*it*y1 + t*t*y2;\n                        dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy);\n                        if (dist2 < min_dist * min_dist)\n                           min_dist = (float) STBTT_sqrt(dist2);\n                     }\n                     if (num >= 2 && res[1] >= 0.0f && res[1] <= 1.0f) {\n                        t = res[1], it = 1.0f - t;\n                        px = it*it*x0 + 2*t*it*x1 + t*t*x2;\n                        py = it*it*y0 + 2*t*it*y1 + t*t*y2;\n                        dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy);\n                        if (dist2 < min_dist * min_dist)\n                           min_dist = (float) STBTT_sqrt(dist2);\n                     }\n                     if (num >= 3 && res[2] >= 0.0f && res[2] <= 1.0f) {\n                        t = res[2], it = 1.0f - t;\n                        px = it*it*x0 + 2*t*it*x1 + t*t*x2;\n                        py = it*it*y0 + 2*t*it*y1 + t*t*y2;\n                        dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy);\n                        if (dist2 < min_dist * min_dist)\n                           min_dist = (float) STBTT_sqrt(dist2);\n                     }\n                  }\n               }\n            }\n            if (winding == 0)\n               min_dist = -min_dist;  // if outside the shape, value is negative\n            val = onedge_value + pixel_dist_scale * min_dist;\n            if (val < 0)\n               val = 0;\n            else if (val > 255)\n               val = 255;\n            data[(y-iy0)*w+(x-ix0)] = (unsigned char) val;\n         }\n      }\n      STBTT_free(precompute, info->userdata);\n      STBTT_free(verts, info->userdata);\n   }\n   return data;\n}   \n\nSTBTT_DEF unsigned char * stbtt_GetCodepointSDF(const stbtt_fontinfo *info, float scale, int codepoint, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff)\n{\n   return stbtt_GetGlyphSDF(info, scale, stbtt_FindGlyphIndex(info, codepoint), padding, onedge_value, pixel_dist_scale, width, height, xoff, yoff);\n}\n\nSTBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata)\n{\n   STBTT_free(bitmap, userdata);\n}\n\n//////////////////////////////////////////////////////////////////////////////\n//\n// font name matching -- recommended not to use this\n//\n\n// check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string\nstatic stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2, stbtt_int32 len2) \n{\n   stbtt_int32 i=0;\n\n   // convert utf16 to utf8 and compare the results while converting\n   while (len2) {\n      stbtt_uint16 ch = s2[0]*256 + s2[1];\n      if (ch < 0x80) {\n         if (i >= len1) return -1;\n         if (s1[i++] != ch) return -1;\n      } else if (ch < 0x800) {\n         if (i+1 >= len1) return -1;\n         if (s1[i++] != 0xc0 + (ch >> 6)) return -1;\n         if (s1[i++] != 0x80 + (ch & 0x3f)) return -1;\n      } else if (ch >= 0xd800 && ch < 0xdc00) {\n         stbtt_uint32 c;\n         stbtt_uint16 ch2 = s2[2]*256 + s2[3];\n         if (i+3 >= len1) return -1;\n         c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;\n         if (s1[i++] != 0xf0 + (c >> 18)) return -1;\n         if (s1[i++] != 0x80 + ((c >> 12) & 0x3f)) return -1;\n         if (s1[i++] != 0x80 + ((c >>  6) & 0x3f)) return -1;\n         if (s1[i++] != 0x80 + ((c      ) & 0x3f)) return -1;\n         s2 += 2; // plus another 2 below\n         len2 -= 2;\n      } else if (ch >= 0xdc00 && ch < 0xe000) {\n         return -1;\n      } else {\n         if (i+2 >= len1) return -1;\n         if (s1[i++] != 0xe0 + (ch >> 12)) return -1;\n         if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f)) return -1;\n         if (s1[i++] != 0x80 + ((ch     ) & 0x3f)) return -1;\n      }\n      s2 += 2;\n      len2 -= 2;\n   }\n   return i;\n}\n\nstatic int stbtt_CompareUTF8toUTF16_bigendian_internal(char *s1, int len1, char *s2, int len2) \n{\n   return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8*) s1, len1, (stbtt_uint8*) s2, len2);\n}\n\n// returns results in whatever encoding you request... but note that 2-byte encodings\n// will be BIG-ENDIAN... use stbtt_CompareUTF8toUTF16_bigendian() to compare\nSTBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID)\n{\n   stbtt_int32 i,count,stringOffset;\n   stbtt_uint8 *fc = font->data;\n   stbtt_uint32 offset = font->fontstart;\n   stbtt_uint32 nm = stbtt__find_table(fc, offset, \"name\");\n   if (!nm) return NULL;\n\n   count = ttUSHORT(fc+nm+2);\n   stringOffset = nm + ttUSHORT(fc+nm+4);\n   for (i=0; i < count; ++i) {\n      stbtt_uint32 loc = nm + 6 + 12 * i;\n      if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2)\n          && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) {\n         *length = ttUSHORT(fc+loc+8);\n         return (const char *) (fc+stringOffset+ttUSHORT(fc+loc+10));\n      }\n   }\n   return NULL;\n}\n\nstatic int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id)\n{\n   stbtt_int32 i;\n   stbtt_int32 count = ttUSHORT(fc+nm+2);\n   stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4);\n\n   for (i=0; i < count; ++i) {\n      stbtt_uint32 loc = nm + 6 + 12 * i;\n      stbtt_int32 id = ttUSHORT(fc+loc+6);\n      if (id == target_id) {\n         // find the encoding\n         stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4);\n\n         // is this a Unicode encoding?\n         if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {\n            stbtt_int32 slen = ttUSHORT(fc+loc+8);\n            stbtt_int32 off = ttUSHORT(fc+loc+10);\n\n            // check if there's a prefix match\n            stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen);\n            if (matchlen >= 0) {\n               // check for target_id+1 immediately following, with same encoding & language\n               if (i+1 < count && ttUSHORT(fc+loc+12+6) == next_id && ttUSHORT(fc+loc+12) == platform && ttUSHORT(fc+loc+12+2) == encoding && ttUSHORT(fc+loc+12+4) == language) {\n                  slen = ttUSHORT(fc+loc+12+8);\n                  off = ttUSHORT(fc+loc+12+10);\n                  if (slen == 0) {\n                     if (matchlen == nlen)\n                        return 1;\n                  } else if (matchlen < nlen && name[matchlen] == ' ') {\n                     ++matchlen;\n                     if (stbtt_CompareUTF8toUTF16_bigendian_internal((char*) (name+matchlen), nlen-matchlen, (char*)(fc+stringOffset+off),slen))\n                        return 1;\n                  }\n               } else {\n                  // if nothing immediately following\n                  if (matchlen == nlen)\n                     return 1;\n               }\n            }\n         }\n\n         // @TODO handle other encodings\n      }\n   }\n   return 0;\n}\n\nstatic int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags)\n{\n   stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((char *) name);\n   stbtt_uint32 nm,hd;\n   if (!stbtt__isfont(fc+offset)) return 0;\n\n   // check italics/bold/underline flags in macStyle...\n   if (flags) {\n      hd = stbtt__find_table(fc, offset, \"head\");\n      if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7)) return 0;\n   }\n\n   nm = stbtt__find_table(fc, offset, \"name\");\n   if (!nm) return 0;\n\n   if (flags) {\n      // if we checked the macStyle flags, then just check the family and ignore the subfamily\n      if (stbtt__matchpair(fc, nm, name, nlen, 16, -1))  return 1;\n      if (stbtt__matchpair(fc, nm, name, nlen,  1, -1))  return 1;\n      if (stbtt__matchpair(fc, nm, name, nlen,  3, -1))  return 1;\n   } else {\n      if (stbtt__matchpair(fc, nm, name, nlen, 16, 17))  return 1;\n      if (stbtt__matchpair(fc, nm, name, nlen,  1,  2))  return 1;\n      if (stbtt__matchpair(fc, nm, name, nlen,  3, -1))  return 1;\n   }\n\n   return 0;\n}\n\nstatic int stbtt_FindMatchingFont_internal(unsigned char *font_collection, char *name_utf8, stbtt_int32 flags)\n{\n   stbtt_int32 i;\n   for (i=0;;++i) {\n      stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);\n      if (off < 0) return off;\n      if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags))\n         return off;\n   }\n}\n\n#if defined(__GNUC__) || defined(__clang__)\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wcast-qual\"\n#endif\n\nSTBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset,\n                                float pixel_height, unsigned char *pixels, int pw, int ph,\n                                int first_char, int num_chars, stbtt_bakedchar *chardata)\n{\n   return stbtt_BakeFontBitmap_internal((unsigned char *) data, offset, pixel_height, pixels, pw, ph, first_char, num_chars, chardata);\n}\n\nSTBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index)\n{\n   return stbtt_GetFontOffsetForIndex_internal((unsigned char *) data, index);   \n}\n\nSTBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data)\n{\n   return stbtt_GetNumberOfFonts_internal((unsigned char *) data);\n}\n\nSTBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset)\n{\n   return stbtt_InitFont_internal(info, (unsigned char *) data, offset);\n}\n\nSTBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags)\n{\n   return stbtt_FindMatchingFont_internal((unsigned char *) fontdata, (char *) name, flags);\n}\n\nSTBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2)\n{\n   return stbtt_CompareUTF8toUTF16_bigendian_internal((char *) s1, len1, (char *) s2, len2);\n}\n\n#if defined(__GNUC__) || defined(__clang__)\n#pragma GCC diagnostic pop\n#endif\n\n#endif // STB_TRUETYPE_IMPLEMENTATION\n\n\n// FULL VERSION HISTORY\n//\n//   1.19 (2018-02-11) OpenType GPOS kerning (horizontal only), STBTT_fmod\n//   1.18 (2018-01-29) add missing function\n//   1.17 (2017-07-23) make more arguments const; doc fix\n//   1.16 (2017-07-12) SDF support\n//   1.15 (2017-03-03) make more arguments const\n//   1.14 (2017-01-16) num-fonts-in-TTC function\n//   1.13 (2017-01-02) support OpenType fonts, certain Apple fonts\n//   1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual\n//   1.11 (2016-04-02) fix unused-variable warning\n//   1.10 (2016-04-02) allow user-defined fabs() replacement\n//                     fix memory leak if fontsize=0.0\n//                     fix warning from duplicate typedef\n//   1.09 (2016-01-16) warning fix; avoid crash on outofmem; use alloc userdata for PackFontRanges\n//   1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges\n//   1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints;\n//                     allow PackFontRanges to pack and render in separate phases;\n//                     fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?);\n//                     fixed an assert() bug in the new rasterizer\n//                     replace assert() with STBTT_assert() in new rasterizer\n//   1.06 (2015-07-14) performance improvements (~35% faster on x86 and x64 on test machine)\n//                     also more precise AA rasterizer, except if shapes overlap\n//                     remove need for STBTT_sort\n//   1.05 (2015-04-15) fix misplaced definitions for STBTT_STATIC\n//   1.04 (2015-04-15) typo in example\n//   1.03 (2015-04-12) STBTT_STATIC, fix memory leak in new packing, various fixes\n//   1.02 (2014-12-10) fix various warnings & compile issues w/ stb_rect_pack, C++\n//   1.01 (2014-12-08) fix subpixel position when oversampling to exactly match\n//                        non-oversampled; STBTT_POINT_SIZE for packed case only\n//   1.00 (2014-12-06) add new PackBegin etc. API, w/ support for oversampling\n//   0.99 (2014-09-18) fix multiple bugs with subpixel rendering (ryg)\n//   0.9  (2014-08-07) support certain mac/iOS fonts without an MS platformID\n//   0.8b (2014-07-07) fix a warning\n//   0.8  (2014-05-25) fix a few more warnings\n//   0.7  (2013-09-25) bugfix: subpixel glyph bug fixed in 0.5 had come back\n//   0.6c (2012-07-24) improve documentation\n//   0.6b (2012-07-20) fix a few more warnings\n//   0.6  (2012-07-17) fix warnings; added stbtt_ScaleForMappingEmToPixels,\n//                        stbtt_GetFontBoundingBox, stbtt_IsGlyphEmpty\n//   0.5  (2011-12-09) bugfixes:\n//                        subpixel glyph renderer computed wrong bounding box\n//                        first vertex of shape can be off-curve (FreeSans)\n//   0.4b (2011-12-03) fixed an error in the font baking example\n//   0.4  (2011-12-01) kerning, subpixel rendering (tor)\n//                    bugfixes for:\n//                        codepoint-to-glyph conversion using table fmt=12\n//                        codepoint-to-glyph conversion using table fmt=4\n//                        stbtt_GetBakedQuad with non-square texture (Zer)\n//                    updated Hello World! sample to use kerning and subpixel\n//                    fixed some warnings\n//   0.3  (2009-06-24) cmap fmt=12, compound shapes (MM)\n//                    userdata, malloc-from-userdata, non-zero fill (stb)\n//   0.2  (2009-03-11) Fix unsigned/signed char warnings\n//   0.1  (2009-03-09) First public release\n//\n\n/*\n------------------------------------------------------------------------------\nThis software is available under 2 licenses -- choose whichever you prefer.\n------------------------------------------------------------------------------\nALTERNATIVE A - MIT License\nCopyright (c) 2017 Sean Barrett\nPermission is hereby granted, free of charge, to any person obtaining a copy of \nthis software and associated documentation files (the \"Software\"), to deal in \nthe Software without restriction, including without limitation the rights to \nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies \nof the Software, and to permit persons to whom the Software is furnished to do \nso, subject to the following conditions:\nThe above copyright notice and this permission notice shall be included in all \ncopies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR \nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, \nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE \nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER \nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, \nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE \nSOFTWARE.\n------------------------------------------------------------------------------\nALTERNATIVE B - Public Domain (www.unlicense.org)\nThis is free and unencumbered software released into the public domain.\nAnyone is free to copy, modify, publish, use, compile, sell, or distribute this \nsoftware, either in source code form or as a compiled binary, for any purpose, \ncommercial or non-commercial, and by any means.\nIn jurisdictions that recognize copyright laws, the author or authors of this \nsoftware dedicate any and all copyright interest in the software to the public \ndomain. We make this dedication for the benefit of the public at large and to \nthe detriment of our heirs and successors. We intend this dedication to be an \novert act of relinquishment in perpetuity of all present and future rights to \nthis software under copyright law.\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR \nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, \nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE \nAUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN \nACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION \nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n------------------------------------------------------------------------------\n*/\n"
  },
  {
    "path": "src/imgui_graphics_settings.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"render_tasks/d3d12_rtao_task.hpp\"\n#include \"render_tasks/d3d12_hbao.hpp\"\n#include \"render_tasks/d3d12_ansel.hpp\"\n#include \"render_tasks/d3d12_build_acceleration_structures.hpp\"\n#include \"render_tasks/d3d12_rt_shadow_task.hpp\"\n#include \"render_tasks/d3d12_shadow_denoiser_task.hpp\"\n\nnamespace wr::imgui::window\n{\n\tstatic bool rtao_settings_open = true;\n\tstatic bool hbao_settings_open = true;\n\tstatic bool ansel_settings_open = true;\n\tstatic bool asbuild_settings_open = true;\n\tstatic bool shadow_settings_open = true;\n\tstatic bool shadow_denoiser_settings_open = true;\n\n\tvoid GraphicsSettings(FrameGraph* frame_graph)\n\t{\n\t\tif (frame_graph->HasTask<wr::RTAOData>() && rtao_settings_open)\n\t\t{\n\t\t\tauto rtao_user_settings = frame_graph->GetSettings<RTAOData, RTAOSettings>();\n\t\t\tImGui::Begin(\"RTAO Settings\", &rtao_settings_open);\n\n\t\t\tImGui::DragFloat(\"Bias\", &rtao_user_settings.m_runtime.bias, 0.01f, 0.0f, 100.f);\n\t\t\tImGui::DragFloat(\"Radius\", &rtao_user_settings.m_runtime.radius, 0.1f, 0.0f, 1000.f);\n\t\t\tImGui::DragFloat(\"Power\", &rtao_user_settings.m_runtime.power, 0.1f, 0.0f, 10.f);\n\t\t\tImGui::DragInt(\"SPP\", &rtao_user_settings.m_runtime.sample_count, 1, 0, 1073741824);\n\n\t\t\tImGui::End();\n\n\t\t\tframe_graph->UpdateSettings<RTAOData>(rtao_user_settings);\n\t\t}\n\n\n\t\tif (frame_graph->HasTask<HBAOData>() && hbao_settings_open)\n\t\t{\n\t\t\tauto hbao_user_settings = frame_graph->GetSettings<HBAOData, HBAOSettings>();\n\t\t\tImGui::Begin(\"HBAO+ Settings\", &hbao_settings_open);\n\n\t\t\tImGui::DragFloat(\"Meters to units\", &hbao_user_settings.m_runtime.m_meters_to_view_space_units, 0.1f, 0.1f, 100.f);\n\t\t\tImGui::DragFloat(\"Radius\", &hbao_user_settings.m_runtime.m_radius, 0.1f, 0.0f, 100.f);\n\t\t\tImGui::DragFloat(\"Bias\", &hbao_user_settings.m_runtime.m_bias, 0.1f, 0.0f, 0.5f);\n\t\t\tImGui::DragFloat(\"Power\", &hbao_user_settings.m_runtime.m_power_exp, 0.1f, 1.f, 4.f);\n\t\t\tImGui::Checkbox(\"Blur\", &hbao_user_settings.m_runtime.m_enable_blur);\n\t\t\tImGui::DragFloat(\"Blur Sharpness\", &hbao_user_settings.m_runtime.m_blur_sharpness, 0.5f, 0.0f, 16.0f);\n\n\t\t\tImGui::End();\n\n\t\t\tframe_graph->UpdateSettings<HBAOData>(hbao_user_settings);\n\t\t}\n\n\n\t\tif (frame_graph->HasTask<AnselData>() && ansel_settings_open)\n\t\t{\n\t\t\tauto ansel_user_settings = frame_graph->GetSettings<AnselData, AnselSettings>();\n\n\t\t\tImGui::Begin(\"NVIDIA Ansel Settings\", &ansel_settings_open);\n\n\t\t\tImGui::Checkbox(\"Translation\", &ansel_user_settings.m_runtime.m_allow_translation); ImGui::SameLine();\n\t\t\tImGui::Checkbox(\"Rotation\", &ansel_user_settings.m_runtime.m_allow_rotation); ImGui::SameLine();\n\t\t\tImGui::Checkbox(\"FOV\", &ansel_user_settings.m_runtime.m_allow_fov);\n\t\t\tImGui::Checkbox(\"Mono 360\", &ansel_user_settings.m_runtime.m_allow_mono_360); ImGui::SameLine();\n\n\t\t\tImGui::Checkbox(\"Stereo 360\", &ansel_user_settings.m_runtime.m_allow_stero_360); ImGui::SameLine();\n\t\t\tImGui::Checkbox(\"Raw HDR\", &ansel_user_settings.m_runtime.m_allow_raw);\n\t\t\tImGui::Checkbox(\"Pause\", &ansel_user_settings.m_runtime.m_allow_pause); ImGui::SameLine();\n\t\t\tImGui::Checkbox(\"High res\", &ansel_user_settings.m_runtime.m_allow_highres);\n\n\t\t\tImGui::DragFloat(\"Camera Speed\", &ansel_user_settings.m_runtime.m_translation_speed_in_world_units_per_sec, 0.1f, 0.1f, 100.f);\n\t\t\tImGui::DragFloat(\"Rotation Speed\", &ansel_user_settings.m_runtime.m_rotation_speed_in_deg_per_second, 5.f, 5.f, 920.f);\n\t\t\tImGui::DragFloat(\"Max FOV\", &ansel_user_settings.m_runtime.m_maximum_fov_in_deg, 1.f, 140.f, 179.f);\n\n\t\t\tImGui::End();\n\n\t\t\tframe_graph->UpdateSettings<AnselData>(ansel_user_settings);\n\t\t}\n\n\n\t\tif (frame_graph->HasTask<ASBuildData>() && asbuild_settings_open)\n\t\t{\n\t\t\tauto as_build_user_settings = frame_graph->GetSettings<ASBuildData, ASBuildSettings>();\n\n\t\t\tImGui::Begin(\"Acceleration Structure Settings\", &asbuild_settings_open);\n\n\t\t\tImGui::Checkbox(\"Disable rebuilding\", &as_build_user_settings.m_runtime.m_rebuild_as);\n\n\t\t\tImGui::End();\n\t\t\tframe_graph->UpdateSettings<ASBuildData>(as_build_user_settings);\n\t\t}\n\n\t\tif (frame_graph->HasTask<RTShadowData>() && shadow_settings_open)\n\t\t{\n\t\t\tauto shadow_user_settings = frame_graph->GetSettings<RTShadowData, RTShadowSettings>();\n\n\t\t\tImGui::Begin(\"Shadow Settings\", &rtao_settings_open);\n\n\t\t\tImGui::DragFloat(\"Epsilon\", &shadow_user_settings.m_runtime.m_epsilon, 0.01f, 0.0f, 15.f);\n\t\t\tImGui::DragInt(\"Sample Count\", &shadow_user_settings.m_runtime.m_sample_count, 1, 1, 64);\n\t\t\t\n\t\t\tframe_graph->UpdateSettings<RTShadowData>(shadow_user_settings);\n\t\t\t\n\t\t\tif (frame_graph->HasTask<ShadowDenoiserData>())\n\t\t\t{\n\t\t\t\tauto shadow_denoiser_user_settings = frame_graph->GetSettings<ShadowDenoiserData, ShadowDenoiserSettings>();\n\n\t\t\t\tImGui::Dummy(ImVec2(0.0f, 10.0f));\n\t\t\t\tImGui::LabelText(\"\", \"Denoising\");\n\t\t\t\tImGui::Separator();\n\n\t\t\t\tImGui::DragFloat(\"Alpha\", &shadow_denoiser_user_settings.m_runtime.m_alpha, 0.01f, 0.001f, 1.f);\n\t\t\t\tImGui::DragFloat(\"Moments Alpha\", &shadow_denoiser_user_settings.m_runtime.m_moments_alpha, 0.01f, 0.001f, 1.f);\n\t\t\t\tImGui::DragFloat(\"L Phi\", &shadow_denoiser_user_settings.m_runtime.m_l_phi, 0.1f, 0.1f, 16.f);\n\t\t\t\tImGui::DragFloat(\"N Phi\", &shadow_denoiser_user_settings.m_runtime.m_n_phi, 1.f, 1.f, 360.f);\n\t\t\t\tImGui::DragFloat(\"Z Phi\", &shadow_denoiser_user_settings.m_runtime.m_z_phi, 0.1f, 0.1f, 16.f);\n\n\t\t\t\tframe_graph->UpdateSettings<ShadowDenoiserData>(shadow_denoiser_user_settings);\n\t\t\t}\n\t\t\tImGui::End();\n\n\n\t\t}\t\n\t}\n\n}// namepace imgui::window\n\nnamespace wr::imgui::menu\n{\n\tvoid GraphicsSettingsMenu(wr::FrameGraph* frame_graph)\n\t{\n\t\tif (ImGui::BeginMenu(\"Graphics Settings\"))\n\t\t{\n\t\t\tif (frame_graph->HasTask<wr::RTAOData>())\n\t\t\t{\n\t\t\t\tImGui::MenuItem(\"RTAO Settings\", nullptr, &window::rtao_settings_open);\n\t\t\t}\n\t\t\tif (frame_graph->HasTask<wr::HBAOData>())\n\t\t\t{\n\t\t\t\tImGui::MenuItem(\"HBAO Settings\", nullptr, &window::hbao_settings_open);\n\t\t\t}\n\t\t\tif (frame_graph->HasTask<wr::AnselData>())\n\t\t\t{\n\t\t\t\tImGui::MenuItem(\"Ansel Settings\", nullptr, &window::ansel_settings_open);\n\t\t\t}\n\t\t\tif (frame_graph->HasTask<wr::ASBuildData>())\n\t\t\t{\n\t\t\t\tImGui::MenuItem(\"AS Build Settings\", nullptr, &window::asbuild_settings_open);\n\t\t\t}\n\t\t\tif (frame_graph->HasTask<wr::RTShadowData>())\n\t\t\t{\n\t\t\t\tImGui::MenuItem(\"Shadow Settings\", nullptr, &window::shadow_settings_open);\n\t\t\t}\n\t\t\tImGui::EndMenu();\n\t\t}\n\t}\n}// namespace imgui::menu\n"
  },
  {
    "path": "src/imgui_tools.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"imgui_tools.hpp\"\n\n#include \"imgui/imgui_internal.hpp\"\n\n#include <sstream>\n#include <optional>\n#include <filesystem>\n\n#include \"scene_graph/camera_node.hpp\"\n#include \"shader_registry.hpp\"\n#include \"pipeline_registry.hpp\"\n#include \"root_signature_registry.hpp\"\n#include \"d3d12/d3d12_renderer.hpp\"\n#include \"scene_graph/scene_graph.hpp\"\n#include \"scene_graph/light_node.hpp\"\n#include \"scene_graph/mesh_node.hpp\"\n#include \"scene_graph/skybox_node.hpp\"\n#include \"model_pool.hpp\"\n#include \"shader_registry.hpp\"\n#include \"rt_pipeline_registry.hpp\"\n#include \"pipeline_registry.hpp\"\n#include \"imgui/ImGuizmo.h\"\n\nnamespace wr::imgui::internal\n{\n\ttemplate <typename T>\n\tinline std::string ConvertPointerToStringAddress(const T* obj)\n\t{\n#pragma warning( push )\n#pragma warning( disable : 4311)\n\t\tauto address = reinterpret_cast<std::uint64_t>(std::addressof(obj));\n#pragma warning( pop ) \n\t\tstd::stringstream ss;\n\t\tss << std::hex << address;\n\t\treturn ss.str();\n\t}\n\n\ttemplate <typename T>\n\tinline std::string DecimalToHex(const T decimal)\n\t{\n\t\tstd::stringstream ss;\n\t\tss << std::hex << decimal;\n\t\treturn ss.str();\n\t}\n\n\tinline std::string ShaderTypeToStr(ShaderType type)\n\t{\n\t\tswitch (type)\n\t\t{\n\t\tcase ShaderType::VERTEX_SHADER: return \"Vertex Shader\";\n\t\tcase ShaderType::PIXEL_SHADER: return \"Pixel Shader\";\n\t\tdefault: return \"Unknown\";\n\t\t}\n\t}\n\n\tinline std::string FeatureLevelToStr(D3D_FEATURE_LEVEL level)\n\t{\n\t\tswitch (level)\n\t\t{\n\t\tcase D3D_FEATURE_LEVEL_11_0: return \"D3D_FEATURE_LEVEL_11_0\";\n\t\tcase D3D_FEATURE_LEVEL_11_1: return \"D3D_FEATURE_LEVEL_11_1\";\n\t\tcase D3D_FEATURE_LEVEL_12_0: return \"D3D_FEATURE_LEVEL_12_0\";\n\t\tcase D3D_FEATURE_LEVEL_12_1: return \"D3D_FEATURE_LEVEL_12_1\";\n\t\tdefault: return \"Unknown\";\n\t\t}\n\t}\n\n\ttemplate<typename T>\n\tinline void AddressText(T* obj)\n\t{\n\t\tif (obj)\n\t\t{\n\t\t\tImGui::Text(\"Address: %s\", internal::ConvertPointerToStringAddress(obj).c_str());\n\t\t}\n\t\telse\n\t\t{\n\t\t\tImGui::Text(\"Address: std::nullptr\");\n\t\t}\n\t}\n\n\tinline std::string BooltoStr(bool val)\n\t{\n\t\treturn val ? \"True\" : \"False\";\n\t}\n\n\tvoid ManipulateNode(wr::Node* node, SceneGraph* scene_graph, ImVec2 viewport_pos, ImVec2 viewport_size)\n\t{\n\t\tDirectX::XMFLOAT4X4 rmat;\n\t\tauto mat = DirectX::XMMatrixTranslationFromVector(node->m_position);\n\t\tDirectX::XMStoreFloat4x4(&rmat, mat);\n\n\t\tauto cam = scene_graph->GetActiveCamera();\n\t\tDirectX::XMFLOAT4X4 rview;\n\t\tDirectX::XMFLOAT4X4 rproj;\n\t\tauto view = cam->m_view;\n\t\tDirectX::XMStoreFloat4x4(&rproj, cam->m_projection);\n\t\tDirectX::XMStoreFloat4x4(&rview, view);\n\n\t\tImGuizmo::SetRect(viewport_pos.x, viewport_pos.y, viewport_size.x, viewport_size.y);\n\t\tImGuizmo::Manipulate(&rview._11, &rproj._11, ImGuizmo::OPERATION::TRANSLATE, ImGuizmo::MODE::WORLD, &rmat._11, NULL, NULL);\n\n\t\tnode->m_position = { rmat._41, rmat._42, rmat._43 };\n\n\t\tnode->SignalTransformChange();\n\t\tnode->SignalChange();\n\t}\n}\n\nnamespace wr::imgui::menu\n{\n\n\tvoid Registries()\n\t{\n\t\tif (ImGui::BeginMenu(\"Registries\"))\n\t\t{\n\t\t\tImGui::MenuItem(\"Shader Registry\", nullptr, &window::open_shader_registry);\n\t\t\tImGui::MenuItem(\"Pipeline Registry\", nullptr, &window::open_pipeline_registry);\n\t\t\tImGui::MenuItem(\"RootSignatureRegistry\", nullptr, &window::open_root_signature_registry);\n\t\t\tImGui::EndMenu();\n\t\t}\n\t}\n\n} /* wr::imgui::menu */\n\nnamespace wr::imgui::window\n{\n\n\tvoid Stats::Draw(D3D12RenderSystem& render_system, ImVec2 viewport_pos)\n\t{\n\t\tif (m_open)\n\t\t{\n\t\t\tauto& io = ImGui::GetIO();\n\t\t\tauto os_info = render_system.m_device->m_sys_info;\n\t\t\tauto dx_info = render_system.m_device->m_adapter_info;\n\n\t\t\tstd::wstring wdesc(dx_info.Description);\n\t\t\tstd::string desc(wdesc.begin(), wdesc.end());\n\n\t\t\tImGui::SetNextWindowPos({ viewport_pos.x + 10, viewport_pos.y + 10 });\n\t\t\tImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0, 0, 0, 0.5));\n\t\t\tImGui::Begin(\"Stats\", &m_open, ImGuiWindowFlags_AlwaysAutoResize |\n\t\t\t\tImGuiWindowFlags_NoResize |\n\t\t\t\tImGuiWindowFlags_NoSavedSettings |\n\t\t\t\tImGuiWindowFlags_NoInputs |\n\t\t\t\tImGuiWindowFlags_NoNav |\n\t\t\t\tImGuiWindowFlags_NoDocking |\n\t\t\t\tImGuiWindowFlags_NoCollapse |\n\t\t\t\tImGuiWindowFlags_NoDecoration |\n\t\t\t\tImGuiWindowFlags_NoMove\n\t\t\t);\n\t\t\tImGui::Text(desc.c_str());\n\t\t\tImGui::Separator();\n\t\t\tImGui::Text(\"Framerate: %.1f\", io.Framerate);\n\t\t\tImGui::Text(\"Delta: %f\", io.DeltaTime);\n\t\t\tImGui::Text(\"Display Size: (%.0f, %.0f)\", io.DisplaySize.x, io.DisplaySize.y);\n\t\t\tImGui::End();\n\t\t\tImGui::PopStyleColor();\n\t\t}\n\t}\n\n\tvoid D3D12HardwareInfo(D3D12RenderSystem& render_system)\n\t{\n\t\tauto os_info = render_system.m_device->m_sys_info;\n\t\tauto dx_info = render_system.m_device->m_adapter_info;\n\n\t\tif (open_hardware_info)\n\t\t{\n\t\t\tImGui::Begin(\"Hardware Info\", &open_hardware_info);\n\t\t\tif (ImGui::CollapsingHeader(\"System Information\", ImGuiTreeNodeFlags_DefaultOpen))\n\t\t\t{\n\t\t\t\tImGui::Text(\"Page Size: %i\", os_info.dwPageSize);\n\t\t\t\tImGui::Text(\"Application Address Range: %s - %s\", internal::DecimalToHex(os_info.lpMinimumApplicationAddress).c_str(), internal::DecimalToHex(os_info.lpMaximumApplicationAddress).c_str());\n\t\t\t\tImGui::Text(\"Active Processor Mask: %i\", os_info.dwActiveProcessorMask);\n\t\t\t\tImGui::Text(\"Processor Count: %i\", os_info.dwNumberOfProcessors);\n\n\t\t\t\tswitch (os_info.wProcessorArchitecture)\n\t\t\t\t{\n\t\t\t\tcase 9: ImGui::Text(\"Processor Architecture: %s\", \"PROCESSOR_ARCHITECTURE_AMD64\"); break;\n\t\t\t\tcase 5: ImGui::Text(\"Processor Architecture: %s\", \"PROCESSOR_ARCHITECTURE_ARM\"); break;\n\t\t\t\tcase 6: ImGui::Text(\"Processor Architecture: %s\", \"PROCESSOR_ARCHITECTURE_IA64\"); break;\n\t\t\t\tcase 0: ImGui::Text(\"Processor Architecture: %s\", \"PROCESSOR_ARCHITECTURE_INTEL\"); break;\n\t\t\t\tdefault: ImGui::Text(\"Processor Architecture: %s\", \"Unknown\"); break;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (ImGui::CollapsingHeader(\"Graphics Adapter Information\", ImGuiTreeNodeFlags_DefaultOpen))\n\t\t\t{\n\t\t\t\tstd::wstring wdesc(dx_info.Description);\n\t\t\t\tstd::string desc(wdesc.begin(), wdesc.end());\n\t\t\t\tauto device = render_system.m_device;\n\n\t\t\t\tImGui::Text(\"Description: %s\", desc.c_str());\n\t\t\t\tImGui::Text(\"Feature Level: %s\", internal::FeatureLevelToStr(device->m_feature_level).c_str());\n\t\t\t\tImGui::Text(\"DXR Support: %s\", internal::BooltoStr(device->m_dxr_support).c_str());\n\t\t\t\tImGui::Text(\"DXR Fallback Support: %s\", internal::BooltoStr(device->m_dxr_fallback_support).c_str());\n\t\t\t\tImGui::Text(\"Vendor ID: %i\", dx_info.VendorId);\n\t\t\t\tImGui::Text(\"Device ID: %i\", dx_info.DeviceId);\n\t\t\t\tImGui::Text(\"Subsystem ID: %i\", dx_info.SubSysId);\n\t\t\t\tImGui::Text(\"Dedicated Video Memory: %.3f\", dx_info.DedicatedVideoMemory / 1024.f / 1024.f / 1024.f);\n\t\t\t\tImGui::Text(\"Dedicated System Memory: %.3f\", dx_info.DedicatedSystemMemory);\n\t\t\t\tImGui::Text(\"Shared System Memory: %.3f\", dx_info.SharedSystemMemory / 1024.f / 1024.f / 1024.f);\n\t\t\t}\n\n\t\t\tImGui::End();\n\t\t}\n\t}\n\n\tvoid D3D12Settings()\n\t{\n\t\tif (open_d3d12_settings)\n\t\t{\n\t\t\tImGui::Begin(\"DirectX 12 Settings\", &open_d3d12_settings);\n\t\t\tImGui::Text(\"Num back buffers: %d\", d3d12::settings::num_back_buffers);\n\t\t\tImGui::Text(\"Back buffer format: %s\", FormatToStr(d3d12::settings::back_buffer_format).c_str());\n\t\t\tImGui::Text(\"Shader Model: %s\", d3d12::settings::default_shader_model);\n\t\t\tImGui::Text(\"Debug Factory: %s\", internal::BooltoStr(d3d12::settings::enable_debug_factory).c_str());\n\t\t\tImGui::Text(\"Enable GPU Timeout: %s\", internal::BooltoStr(d3d12::settings::enable_gpu_timeout).c_str());\n\t\t\tImGui::Text(\"Num instances per batch: %d\", d3d12::settings::num_instances_per_batch);\n\t\t\tImGui::End();\n\t\t}\n\t}\n\n\ttemplate<typename T>\n\tstatic bool DefaultContextMenu(std::shared_ptr<T> node, SceneGraph* scene_graph)\n\t{\n\t\tif (ImGui::Button(\"Teleport To\"))\n\t\t{\n\t\t\tscene_graph->GetActiveCamera()->SetPosition(node->m_position);\n\n\t\t\treturn true; // close popup.\n\t\t}\n\n\t\tImGui::Separator();\n\n\t\tif (ImGui::Button(\"Remove\"))\n\t\t{\n\t\t\tscene_graph->DestroyNode(node);\n\n\t\t\treturn true; // close popup.\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tdecltype(SceneGraphEditorDetails::sg_editor_type_names) SceneGraphEditorDetails::sg_editor_type_names =\n\t{\n\t\t{ typeid(LightNode), [](std::shared_ptr<Node> node) -> std::string\n\t\t\t{ \n\t\t\t\tauto light_node = std::static_pointer_cast<LightNode>(node);\n\t\t\t\tswitch (light_node->GetType())\n\t\t\t\t{\n\t\t\t\t\tcase LightType::DIRECTIONAL: return \"Directional Light\";\n\t\t\t\t\tcase LightType::POINT: return \"Point Light\";\n\t\t\t\t\tcase LightType::SPOT: return \"Spot Light\";\n\t\t\t\t\tdefault: return \"Light\";\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t{ typeid(MeshNode), [](std::shared_ptr<Node> node) -> std::string\n\t\t\t{\n\t\t\t\tauto mesh_node = std::static_pointer_cast<MeshNode>(node);\n\t\t\t\tauto model_path = mesh_node->m_model->m_model_name;\n\n\t\t\t\t// Remove everything except for the filename.\n\t\t\t\tauto last_slash = model_path.find_last_of('/');\n\t\t\t\tif (last_slash != std::string::npos)\n\t\t\t\t{\n\t\t\t\t\tmodel_path.erase(0, last_slash + 1);\n\t\t\t\t}\n\n\t\t\t\tstd::string prefix = (mesh_node->m_visible ? \"\" : \"[H] \");\n\t\t\t\treturn prefix + \"Mesh (\" + model_path + \")\";\n\t\t\t}\n\t\t},\n\t\t{ typeid(CameraNode), [](std::shared_ptr<Node> node) -> std::string { return \"Camera Node\"; } },\n\t\t{ typeid(SkyboxNode), [](std::shared_ptr<Node> node) -> std::string { return \"Skybox Node\"; } },\n\t};\n\n\tdecltype(SceneGraphEditorDetails::sg_editor_type_inspect) SceneGraphEditorDetails::sg_editor_type_inspect =\n\t{\n\t\t{ typeid(LightNode),\n\t\t\t[](std::shared_ptr<Node> node, SceneGraph* scene_graph)\n\t\t\t{\n\t\t\t\tauto light_node = std::static_pointer_cast<LightNode>(node);\n\t\t\t\tauto& light = *light_node->m_light;\n\n\t\t\t\tconst char* listbox_items[] = { \"Point Light\", \"Directional Light\", \"Spot Light\" };\n\t\t\t\tint type = (int)light.tid & 3;\n\t\t\t\tImGui::Combo(\"Type\", &type, listbox_items, 3);\n\t\t\t\tlight.tid = type;\n\n\t\t\t\tImGui::ColorEdit3(\"Color\", &light.col.x, ImGuiColorEditFlags_HDR);\n\t\t\t\tImGui::DragFloat3(\"Position\", light_node->m_position.m128_f32, 0.25f);\n\n\t\t\t\tif (type != (uint32_t)LightType::POINT)\n\t\t\t\t{\n\t\t\t\t\tfloat rot[3] = { DirectX::XMConvertToDegrees(DirectX::XMVectorGetX(light_node->m_rotation_radians)),\n\t\t\t\t\tDirectX::XMConvertToDegrees(DirectX::XMVectorGetY(light_node->m_rotation_radians)),\n\t\t\t\t\tDirectX::XMConvertToDegrees(DirectX::XMVectorGetZ(light_node->m_rotation_radians)) };\n\t\t\t\t\tImGui::DragFloat3(\"Rotation\", rot, 0.01f);\n\t\t\t\t\tlight_node->SetRotation(DirectX::XMVectorSet(DirectX::XMConvertToRadians(rot[0]), DirectX::XMConvertToRadians(rot[1]), DirectX::XMConvertToRadians(rot[2]), 0));\n\n\t\t\t\t}\n\n\t\t\t\tif (type != (uint32_t)LightType::DIRECTIONAL)\n\t\t\t\t{\n\t\t\t\t\tImGui::DragFloat(\"Radius\", &light.rad, 0.25f);\n\t\t\t\t}\n\n\t\t\t\tif (type == (uint32_t)LightType::SPOT)\n\t\t\t\t{\n\t\t\t\t\tlight.ang = light.ang * 180.f / 3.1415926535f;\n\t\t\t\t\tImGui::DragFloat(\"Angle\", &light.ang);\n\t\t\t\t\tlight.ang = light.ang / 180.f * 3.1415926535f;\n\t\t\t\t}\n\n\t\t\t\tstatic float light_size_temp = 0.0f;\n\t\t\t\tImGui::DragFloat(\"Light Size\", &light_size_temp, 0.1f, 0.0f, 5.0f);\n\t\t\t\tlight_node->SetLightSize(light_size_temp);\n\t\t\t\t\n\n\t\t\t\tif (ImGui::Button(\"Take Camera Transform\"))\n\t\t\t\t{\n\t\t\t\t\tlight_node->SetPosition(scene_graph->GetActiveCamera()->m_position);\n\t\t\t\t\tlight_node->SetRotation(scene_graph->GetActiveCamera()->m_rotation_radians);\n\t\t\t\t}\n\n\t\t\t\tlight_node->SignalTransformChange();\n\t\t\t\tlight_node->SignalChange();\n\t\t\t}\n\t\t},\n\t\t{ typeid(MeshNode),\n\t\t\t[](std::shared_ptr<Node> node, SceneGraph* scene_graph)\n\t\t\t{\n\t\t\t\tauto model_node = std::static_pointer_cast<MeshNode>(node);\n\t\t\t\tauto* model = model_node->m_model;\n\t\t\t\tauto& materials = model_node->GetMaterials();\n\n\t\t\t\tImGui::Text(\"Path: %s\", model->m_model_name.c_str());\n\n\t\t\t\tImGui::Separator();\n\n\t\t\t\tImGui::DragFloat3(\"Position\", model_node->m_position.m128_f32, 0.25f);\n\n\t\t\t\tfloat rot[3] = { DirectX::XMConvertToDegrees(DirectX::XMVectorGetX(model_node->m_rotation_radians)),\n\t\t\t\tDirectX::XMConvertToDegrees(DirectX::XMVectorGetY(model_node->m_rotation_radians)),\n\t\t\t\tDirectX::XMConvertToDegrees(DirectX::XMVectorGetZ(model_node->m_rotation_radians)) };\n\t\t\t\tImGui::DragFloat3(\"Rotation\", rot, 0.1f);\n\t\t\t\tmodel_node->SetRotation(DirectX::XMVectorSet(DirectX::XMConvertToRadians(rot[0]), DirectX::XMConvertToRadians(rot[1]), DirectX::XMConvertToRadians(rot[2]), 0));\n\n\t\t\t\tfloat scale[3] = { DirectX::XMVectorGetX(model_node->m_scale),\n\t\t\t\tDirectX::XMVectorGetY(model_node->m_scale),\n\t\t\t\tDirectX::XMVectorGetZ(model_node->m_scale) };\n\t\t\t\tImGui::DragFloat3(\"Scale\", scale, 0.01f);\n\t\t\t\tmodel_node->SetScale(DirectX::XMVectorSet(scale[0], scale[1], scale[2], 1.f));\n\n\t\t\t\tif (ImGui::Button(\"Take Camera Transform\"))\n\t\t\t\t{\n\t\t\t\t\tmodel_node->SetPosition(scene_graph->GetActiveCamera()->m_position);\n\t\t\t\t\tmodel_node->SetRotation(scene_graph->GetActiveCamera()->m_rotation_radians);\n\t\t\t\t}\n\n\t\t\t\t// Material Settings\n\t\t\t\tif (ImGui::CollapsingHeader(\"Material Settings\", ImGuiTreeNodeFlags_None))\n\t\t\t\t{\n\t\t\t\t\tif (ImGui::Button(\"Add User-defined Material\"))\n\t\t\t\t\t{\n\t\t\t\t\t\tmaterials.emplace_back(model->m_meshes[0].second);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (std::size_t mat_i = 0; mat_i < materials.size(); mat_i++)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto& material = materials[mat_i];\n\t\t\t\t\t\tauto prev_material = material;\n\n\t\t\t\t\t\tImGui::InputInt((\"Remove##\" + std::to_string(mat_i)).c_str(), reinterpret_cast<int*>(&material.m_id));\n\n\t\t\t\t\t\tif (!material.m_pool->HasMaterial(material))\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmaterial = prev_material;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tmodel_node->SignalTransformChange();\n\t\t\t\tmodel_node->SignalChange();\n\t\t\t}\n\t\t},\n\t\t{ typeid(SkyboxNode),\n\t\t\t[](std::shared_ptr<Node> node, SceneGraph* scene_graph)\n\t\t\t{\n\t\t\t\tstd::vector<std::string> hdr_maps;\n\t\t\t\tstd::string path = \"resources/materials/\";\n\n\n\t\t\t\tfor (const auto& entry : std::filesystem::directory_iterator(path))\n\t\t\t\t{\n\t\t\t\t\tstd::filesystem::path ext(entry.path().extension());\n\n\t\t\t\t\tif (ext.compare(L\".hdr\") == 0)\n\t\t\t\t\t{\n\t\t\t\t\t\thdr_maps.push_back(entry.path().filename().string());\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t//Lambda to get char* out of the string vector for ImGui\n\t\t\t\tstatic auto vector_getter = [](void* vec, int idx, const char** out_text)\n\t\t\t\t{\n\t\t\t\t\tauto& vector = *static_cast<std::vector<std::string>*>(vec);\n\t\t\t\t\tif (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }\n\t\t\t\t\t*out_text = vector.at(idx).c_str();\n\t\t\t\t\treturn true;\n\t\t\t\t};\n\n\t\t\t\tstatic int selected_item = 0;\n\n\t\t\t\tImGui::ListBox(\"Environment Maps\", &selected_item, vector_getter, static_cast<void*>(&hdr_maps), hdr_maps.size());\n\n\t\t\t\tauto skybox_node = std::static_pointer_cast<SkyboxNode>(node);\n\t\t\t\t\n\t\t\t\tif (ImGui::Button(\"Change Sky\"))\n\t\t\t\t{\n\t\t\t\t\tpath += hdr_maps[selected_item];\n\n\t\t\t\t\tTextureHandle new_texture = skybox_node->m_skybox.value().m_pool->LoadFromFile(path, false, false);\n\n\t\t\t\t\tscene_graph->UpdateSkyboxNode(skybox_node, new_texture);\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t};\n\n\tdecltype(SceneGraphEditorDetails::sg_editor_type_context_menu) SceneGraphEditorDetails::sg_editor_type_context_menu =\n\t{\n\t\t{ typeid(MeshNode),\n\t\t\t[](std::shared_ptr<Node> node, SceneGraph* scene_graph)\n\t\t\t{\n\t\t\t\tauto mesh_node = std::static_pointer_cast<MeshNode>(node);\n\n\t\t\t\tif (ImGui::Checkbox(\"Visibile\", &mesh_node->m_visible))\n\t\t\t\t{\n\t\t\t\t\treturn true; // close popup.\n\t\t\t\t}\n\n\t\t\t\treturn DefaultContextMenu(mesh_node, scene_graph);\n\t\t\t}\n\t\t},\n\t\t{ typeid(LightNode),\n\t\t\t[](std::shared_ptr<Node> node, SceneGraph* scene_graph)\n\t\t\t{\n\t\t\t\tauto light_node = std::static_pointer_cast<LightNode>(node);\n\n\t\t\t\treturn DefaultContextMenu(light_node, scene_graph);\n\t\t\t}\n\t\t},\n\t\t{ typeid(SkyboxNode),\n\t\t\t[](std::shared_ptr<Node> node, SceneGraph* scene_graph)\n\t\t\t{\n\t\t\t\tauto sky_node = std::static_pointer_cast<SkyboxNode>(node);\n\n\t\t\t\treturn DefaultContextMenu(sky_node, scene_graph);\n\t\t\t}\n\t\t},\n\t};\n\n\tvoid SceneGraphEditor(SceneGraph* scene_graph)\n\t{\n\t\tif (open_scene_graph_editor)\n\t\t{\n\t\t\tImGui::Begin(\"Scene Graph Editor\", &open_scene_graph_editor);\n\n\t\t\tif (ImGui::Button(\"Add Light\"))\n\t\t\t{\n\t\t\t\tscene_graph->CreateChild<LightNode>(nullptr, wr::LightType::POINT, DirectX::XMVECTOR{ 1, 1, 1 });\n\t\t\t}\n\n\t\t\tauto root = scene_graph->GetRootNode();\n\n\t\t\tstatic ImGuiTextFilter filter;\n\n\t\t\tImGui::PushItemWidth(-1.f);\n\t\t\tfilter.Draw(\"##\");\n\t\t\tImVec2 size = ImGui::GetContentRegionAvail();\n\t\t\tsize.y -= ImGui::GetItemsLineHeightWithSpacing();\n\t\t\tif (ImGui::ListBoxHeader(\"##\", size))\n\t\t\t{\n\t\t\t\tfor (auto child_i = 0; child_i < root->m_children.size(); child_i++)\n\t\t\t\t{\n\t\t\t\t\tauto& node = root->m_children[child_i];\n\n\t\t\t\t\tauto name_prefix = SceneGraphEditorDetails::GetNodeName(node).value_or(\"Node\");\n\n\t\t\t\t\tauto node_name = name_prefix + \"##\" + std::to_string(child_i);\n\n\t\t\t\t\t// Skip node if its not part of the filter.\n\t\t\t\t\tif (!filter.PassFilter(node_name.c_str())) continue;\n\n\t\t\t\t\tbool pressed = ImGui::Selectable(node_name.c_str(), selected_node == node);\n\n\t\t\t\t\t// if we don't have that node selected.\n\t\t\t\t\tif (pressed && selected_node != node)\n\t\t\t\t\t{\n\t\t\t\t\t\tselected_node = node;\n\t\t\t\t\t}\n\t\t\t\t\t// if we already have that node selected.\n\t\t\t\t\telse if (pressed && selected_node == node)\n\t\t\t\t\t{\n\t\t\t\t\t\tselected_node = nullptr;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Right click menu\n\t\t\t\t\tif (ImGui::BeginPopupContextItem())\n\t\t\t\t\t{\n\t\t\t\t\t\tauto right_clicked_node = root->m_children[child_i];\n\t\t\t\t\t\tauto node_cm_function = SceneGraphEditorDetails::GetNodeContextMenuFunction(right_clicked_node).value_or(nullptr);\n\n\t\t\t\t\t\tbool close_popup = true;\n\t\t\t\t\t\tif (node_cm_function)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tclose_popup = node_cm_function(right_clicked_node, scene_graph);\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tclose_popup = DefaultContextMenu<Node>(right_clicked_node, scene_graph);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (close_popup)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tImGui::CloseCurrentPopup();\n\t\t\t\t\t\t\tImGui::EndPopup();\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tImGui::EndPopup();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tImGui::ListBoxFooter();\n\t\t\t}\n\t\t\tImGui::End();\n\t\t}\n\t}\n\n\tvoid Inspector(SceneGraph* scene_graph, ImVec2 viewport_pos, ImVec2 viewport_size)\n\t{\n\t\tif (open_inspector)\n\t\t{\n\t\t\tImGui::Begin(\"Inspector\", &open_inspector);\n\n\t\t\tif (selected_node)\n\t\t\t{\n\t\t\t\tauto node_inspect_function = SceneGraphEditorDetails::GetNodeInspectFunction(selected_node).value_or(nullptr);\n\n\t\t\t\tif (node_inspect_function)\n\t\t\t\t{\n\t\t\t\t\tnode_inspect_function(selected_node, scene_graph);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tImGui::DragFloat3(\"Position\", selected_node->m_position.m128_f32, 0.25f);\n\n\t\t\t\t\tfloat rot[3] = { DirectX::XMConvertToDegrees(DirectX::XMVectorGetX(selected_node->m_rotation_radians)),\n\t\t\t\t\tDirectX::XMConvertToDegrees(DirectX::XMVectorGetY(selected_node->m_rotation_radians)),\n\t\t\t\t\tDirectX::XMConvertToDegrees(DirectX::XMVectorGetZ(selected_node->m_rotation_radians)) };\n\t\t\t\t\tImGui::DragFloat3(\"Rotation\", rot, 0.1f);\n\t\t\t\t\tselected_node->SetRotation(DirectX::XMVectorSet(DirectX::XMConvertToRadians(rot[0]), DirectX::XMConvertToRadians(rot[1]), DirectX::XMConvertToRadians(rot[2]), 0));\n\n\t\t\t\t\tImGui::DragFloat3(\"Scale\", selected_node->m_scale.m128_f32, 0.01f);\n\n\t\t\t\t\tif (ImGui::Button(\"Take Camera Transform\"))\n\t\t\t\t\t{\n\t\t\t\t\t\tselected_node->SetPosition(scene_graph->GetActiveCamera()->m_position);\n\t\t\t\t\t\tselected_node->SetRotation(scene_graph->GetActiveCamera()->m_rotation_radians);\n\t\t\t\t\t}\n\n\t\t\t\t\tselected_node->SignalChange();\n\t\t\t\t\tselected_node->SignalTransformChange();\n\t\t\t\t}\n\n\t\t\t\tinternal::ManipulateNode(selected_node.get(), scene_graph, viewport_pos, viewport_size);\n\t\t\t}\n\n\t\t\tImGui::End();\n\t\t}\n\t}\n\n\tvoid ShaderRegistry()\n\t{\n\t\tif (open_shader_registry)\n\t\t{\n\t\t\tauto& registry = ShaderRegistry::Get();\n\n\t\t\tImGui::Begin(\"Shader Registry\", &open_shader_registry);\n\n\t\t\tImGui::Text(\"Num descriptors: %d\", registry.m_descriptions.size());\n\t\t\tImGui::Text(\"Num objects: %d\", registry.m_objects.size());\n\t\t\tImGui::Separator();\n\n\t\t\tfor (auto desc : registry.m_descriptions)\n\t\t\t{\n\t\t\t\tShader* obj = nullptr;\n\t\t\t\tauto obj_it = registry.m_objects.find(desc.first);\n\t\t\t\tif (obj_it != registry.m_objects.end())\n\t\t\t\t{\n\t\t\t\t\tobj = obj_it->second;\n\t\t\t\t}\n\n\t\t\t\tstd::string tree_name = internal::ShaderTypeToStr(desc.second.type) + \"[\" + std::to_string(desc.first) + \"]: \" + desc.second.path;\n\t\t\t\tif (ImGui::TreeNode(tree_name.c_str()))\n\t\t\t\t{\n\t\t\t\t\tif (ImGui::TreeNode(\"Description\"))\n\t\t\t\t\t{\n\t\t\t\t\t\tImGui::Text(\"ID: %d\", desc.first);\n\t\t\t\t\t\tImGui::Text(\"Path: %s\", desc.second.path.c_str());\n\t\t\t\t\t\tImGui::Text(\"Entry: %s\", desc.second.entry.c_str());\n\t\t\t\t\t\tImGui::Text(\"Type: %s\", internal::ShaderTypeToStr(desc.second.type).c_str());\n\n\t\t\t\t\t\tImGui::TreePop();\n\t\t\t\t\t}\n\n\t\t\t\t\tinternal::AddressText(obj);\n\n\t\t\t\t\tImGui::TreePop();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tImGui::End();\n\t\t}\n\n\t}\n\n\tvoid PipelineRegistry()\n\t{\n\t\tif (open_pipeline_registry)\n\t\t{\n\t\t\tauto& registry = PipelineRegistry::Get();\n\n\t\t\tImGui::Begin(\"Pipeline Registry\", &open_pipeline_registry);\n\n\t\t\tImGui::Text(\"Num descriptions: %d\", registry.m_descriptions.size());\n\t\t\tImGui::Text(\"Num objects: %d\", registry.m_objects.size());\n\t\t\tImGui::Separator();\n\n\t\t\tfor (auto desc : registry.m_descriptions)\n\t\t\t{\n\t\t\t\tPipeline* obj = nullptr;\n\t\t\t\tauto obj_it = registry.m_objects.find(desc.first);\n\t\t\t\tif (obj_it != registry.m_objects.end())\n\t\t\t\t{\n\t\t\t\t\tobj = obj_it->second;\n\t\t\t\t}\n\n\t\t\t\tstd::string tree_name = \"Pipeline[\" + std::to_string(desc.first) + \"]\";\n\t\t\t\tif (ImGui::TreeNode(tree_name.c_str()))\n\t\t\t\t{\n\t\t\t\t\tif (ImGui::TreeNode(\"Description\"))\n\t\t\t\t\t{\n\t\t\t\t\t\tImGui::Text(\"ID: %d\", desc.first);\n\n\t\t\t\t\t\tauto text_handle = [](std::string handle_name, std::optional<int> handle)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif (handle.has_value())\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tstd::string text = handle_name + \" Handle: %d\";\n\t\t\t\t\t\t\t\tImGui::Text(text.c_str(), handle);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\ttext_handle(\"Vertex Shader\", desc.second.m_vertex_shader_handle);\n\t\t\t\t\t\ttext_handle(\"Pixel Shader\", desc.second.m_pixel_shader_handle);\n\t\t\t\t\t\ttext_handle(\"Compute Shader\", desc.second.m_compute_shader_handle);\n\t\t\t\t\t\ttext_handle(\"RootSignature\", desc.second.m_root_signature_handle);\n\t\t\t\t\t\t\n\t\t\t\t\t\tImGui::Text(\"Depth Enabled: %s\", internal::BooltoStr(desc.second.m_depth_enabled).c_str());\n\t\t\t\t\t\tImGui::Text(\"Counter Clockwise Winding Order: %s\", internal::BooltoStr(desc.second.m_counter_clockwise).c_str());\n\t\t\t\t\t\tImGui::Text(\"Num RTV Formats: %d\", desc.second.m_num_rtv_formats);\n\t\t\t\t\t\tfor (auto i = 0u; i < desc.second.m_num_rtv_formats; i++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tstd::string text = \"Format[\" + std::to_string(i) + \"]: %s\";\n\t\t\t\t\t\t\tImGui::Text(text.c_str(), FormatToStr(desc.second.m_rtv_formats[i]).c_str());\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tImGui::TreePop();\n\t\t\t\t\t}\n\n\t\t\t\t\tinternal::AddressText(obj);\n\n\t\t\t\t\tImGui::TreePop();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tauto& rt_registry = RTPipelineRegistry::Get();\n\n\t\t\tImGui::Separator();\n\n\t\t\tImGui::Text(\"Num raytracing descriptions: %d\", rt_registry.m_descriptions.size());\n\t\t\tImGui::Text(\"Num raytracing objects: %d\", rt_registry.m_objects.size());\n\t\t\tImGui::Separator();\n\n\t\t\tfor (auto desc : rt_registry.m_descriptions)\n\t\t\t{\n\t\t\t\tStateObject* obj = nullptr;\n\t\t\t\tauto obj_it = rt_registry.m_objects.find(desc.first);\n\t\t\t\tif (obj_it != rt_registry.m_objects.end())\n\t\t\t\t{\n\t\t\t\t\tobj = obj_it->second;\n\t\t\t\t}\n\n\t\t\t\tstd::string tree_name = \"Raytracing Pipeline[\" + std::to_string(desc.first) + \"]\";\n\t\t\t\tif (ImGui::TreeNode(tree_name.c_str()))\n\t\t\t\t{\n\t\t\t\t\tif (ImGui::TreeNode(\"Description\"))\n\t\t\t\t\t{\n\t\t\t\t\t\tImGui::Text(\"ID: %d\", desc.first);\n\n\t\t\t\t\t\tauto text_handle = [](std::string handle_name, std::optional<int> handle)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif (handle.has_value())\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tstd::string text = handle_name + \" Handle: %d\";\n\t\t\t\t\t\t\t\tImGui::Text(text.c_str(), handle);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\ttext_handle(\"Library Shader\", desc.second.library_desc.shader_handle);\n\t\t\t\t\t\ttext_handle(\"Global RootSignature\", desc.second.global_root_signature.value_or(-1));\n\n\t\t\t\t\t\tfor (auto i = 0; i < desc.second.local_root_signatures.size(); i++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tImGui::Text(\"Local Root Signature [%d] = %d\", i, desc.second.local_root_signatures[i]);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tImGui::TreePop();\n\t\t\t\t\t}\n\n\t\t\t\t\tinternal::AddressText(obj);\n\n\t\t\t\t\tImGui::TreePop();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tImGui::End();\n\t\t}\n\t}\n\n\tvoid RootSignatureRegistry()\n\t{\n\t\tif (open_root_signature_registry)\n\t\t{\n\t\t\tauto& registry = RootSignatureRegistry::Get();\n\n\t\t\tImGui::Begin(\"Root Signature Registry\", &open_root_signature_registry);\n\n\t\t\tImGui::Text(\"Num descriptors: %d\", registry.m_descriptions.size());\n\t\t\tImGui::Text(\"Num objects: %d\", registry.m_objects.size());\n\t\t\tImGui::Separator();\n\n\t\t\tfor (auto desc : registry.m_descriptions)\n\t\t\t{\n\t\t\t\tRootSignature* obj = nullptr;\n\t\t\t\tauto obj_it = registry.m_objects.find(desc.first);\n\t\t\t\tif (obj_it != registry.m_objects.end())\n\t\t\t\t{\n\t\t\t\t\tobj = obj_it->second;\n\t\t\t\t}\n\n\t\t\t\tstd::string tree_name = \"Root Signature[\" + std::to_string(desc.first) + \"]\";\n\t\t\t\tif (ImGui::TreeNode(tree_name.c_str()))\n\t\t\t\t{\n\t\t\t\t\tif (ImGui::TreeNode(\"Description\"))\n\t\t\t\t\t{\n\t\t\t\t\t\tImGui::Text(\"ID: %d\", desc.first);\n\n\t\t\t\t\t\tauto text_handle = [](std::string handle_name, std::optional<int> handle)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif (handle.has_value())\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tstd::string text = handle_name + \" Handle: %d\";\n\t\t\t\t\t\t\t\tImGui::Text(text.c_str(), handle);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\tImGui::Text(\"Num samplers: %d\", desc.second.m_samplers.size());\n\t\t\t\t\t\tImGui::Text(\"Num parameters: %d\", desc.second.m_parameters.size());\n\n\t\t\t\t\t\tImGui::TreePop();\n\t\t\t\t\t}\n\n\t\t\t\t\tinternal::AddressText(obj);\n\n\t\t\t\t\tImGui::TreePop();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tImGui::End();\n\t\t}\n\t}\n\n} /* wr::imgui::window */\n\nnamespace wr::imgui::special\n{\n\n\tDebugConsole::DebugConsole()\n\t{\n\t\tClearLog();\n\t\tmemset(InputBuf, 0, sizeof(InputBuf));\n\t\tHistoryPos = -1;\n\n\t\tAddCommand(\"help\",\n\t\t\t[&](DebugConsole&, std::string const &)\n\t\t\t{\n\t\t\t\tAddLog(\"Commands:\");\n\t\t\t\tfor (auto& cmd : m_commands)\n\t\t\t\t{\n\t\t\t\t\tif (cmd.m_starts_with)\n\t\t\t\t\t{\n\t\t\t\t\t\tAddLog(\"- %s[param] // %s\", cmd.m_name.c_str(), cmd.m_description.c_str());\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tAddLog(\"- %s // %s\", cmd.m_name.c_str(), cmd.m_description.c_str());\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"Shows all available commands.\"\n\t\t);\n\n\t\tAddCommand(\"clear\",\n\t\t\t[&](DebugConsole&, std::string const &) { ClearLog(); }, \"Clears the console.\"\n\t\t);\n\t\tAddCommand(\"cls\",\n\t\t\t[&](DebugConsole&, std::string const &) { ClearLog(); }, \"Clears the console.\"\n\t\t);\n\n\n\t\tAddCommand(\"history\",\n\t\t\t[&](DebugConsole&, std::string const &)\n\t\t\t{\n\t\t\t\tint first = History.Size - 10;\n\t\t\t\tfor (int i = first > 0 ? first : 0; i < History.Size; i++)\n\t\t\t\t{\n\t\t\t\t\tAddLog(\"%3d: %s\\n\", i, History[i]);\n\t\t\t\t}\n\t\t\t},\n\t\t\t\"Shows command history.\"\n\t\t);\n\n\t\tAddCommand(\"filter\",\n\t\t\t[&](DebugConsole&, std::string const &)\n\t\t\t{\n\t\t\t\tfilter = ImGuiTextFilter(nullptr);\n\t\t\t\tAddLog(\"Cleared Filter\");\n\t\t\t},\n\t\t\t\"Clears the filter.\",\n\t\t\tfalse\n\t\t);\n\n\t\tAddCommand(\"filter \",\n\t\t\t[&](DebugConsole&, std::string const & str)\n\t\t\t{\n\t\t\t\tauto params = str;\n\t\t\t\tparams.erase(0, 7);\n\n\t\t\t\tfilter = ImGuiTextFilter(params.c_str());\n\n\t\t\t\tAddLog(\"Set filter to: %s\", params.c_str());\n\t\t\t},\n\t\t\t\"Sets the parameter as a filter for to console.\",\n\t\t\ttrue\n\t\t);\n\n\t}\n\n\tDebugConsole::~DebugConsole()\n\t{\n\t\tClearLog();\n\t\tfor (int i = 0; i < History.Size; i++)\n\t\t\tfree(History[i]);\n\t}\n\n\tvoid DebugConsole::EmptyInput()\n\t{\n\t\tmemset(InputBuf, 0, sizeof(InputBuf));\n\t}\n\n\t// Portable helpers\n\tstatic bool StrEquals(std::string const & a, std::string const & b)\n\t{\n\t\tstd::string la(a);\n\t\tstd::string lb(b);\n\t\tstd::transform(la.begin(), la.end(), la.begin(), ::tolower);\n\t\tstd::transform(lb.begin(), lb.end(), lb.begin(), ::tolower);\n\n\t\treturn la == lb;\n\t}\n\n\tstatic bool StrStartsWith(std::string const & str, std::string const & other)\n\t{\n\t\tstd::string lstr(str);\n\t\tstd::transform(lstr.begin(), lstr.end(), lstr.begin(), ::tolower);\n\n\t\treturn (lstr.find(other) == 0);\n\t}\n\n\tstatic int   Stricmp(const char* str1, const char* str2) { int d; while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } return d; }\n\tstatic int   Strnicmp(const char* str1, const char* str2, int n) { int d = 0; while (n > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; n--; } return d; }\n\tstatic char* Strdup(const char *str) { size_t len = strlen(str) + 1; void* buff = malloc(len); return (char*)memcpy(buff, (const void*)str, len); }\n\tstatic void  Strtrim(char* str) { char* str_end = str + strlen(str); while (str_end > str && str_end[-1] == ' ') str_end--; *str_end = 0; }\n\n\tvoid DebugConsole::ClearLog()\n\t{\n\t\tfor (int i = 0; i < Items.Size; i++)\n\t\t\tfree(Items[i]);\n\t\tItems.clear();\n\t\tScrollToBottom = true;\n\t}\n\n\tvoid DebugConsole::AddLog(const char* fmt, ...)\n\t{\n\t\t// FIXME-OPT\n\t\tchar buf[1024];\n\t\tva_list args;\n\t\tva_start(args, fmt);\n\t\tvsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);\n\t\tbuf[IM_ARRAYSIZE(buf) - 1] = 0;\n\t\tva_end(args);\n\t\tItems.push_back(Strdup(buf));\n\t\tScrollToBottom = true;\n\t}\n\n\tvoid DebugConsole::Draw(const char* title, bool* p_open)\n\t{\n\t\tif (!*p_open) return;\n\n\t\tImVec4 text_col(1, 1, 1, 1);\n\t\tImVec4 bg_col(0.f, 0.f, 0.f, 0.5f);\n\t\tImVec4 frame_col(0, 0, 0, 0);\n\n\t\tImGuiContext& g = *ImGui::GetCurrentContext();\n\t\tImGuiViewport* viewport = g.Viewports[0];\n\n\t\tImGui::SetNextWindowSize(ImVec2(viewport->Size.x, 200), ImGuiCond_FirstUseEver);\n\t\tImGui::SetNextWindowPos(ImVec2(0, 0));\n\n\t\tImGui::PushStyleColor(ImGuiCol_WindowBg, bg_col);\n\t\tImGui::PushStyleColor(ImGuiCol_FrameBg, frame_col);\n\n\t\tif (!ImGui::Begin(title, p_open,\n\t\t\tImGuiWindowFlags_NoDocking |\n\t\t\tImGuiWindowFlags_NoCollapse |\n\t\t\tImGuiWindowFlags_NoMove |\n\t\t\tImGuiWindowFlags_NoSavedSettings |\n\t\t\tImGuiWindowFlags_NoTitleBar |\n\t\t\tImGuiWindowFlags_NoResize))\n\t\t{\n\t\t\tImGui::End();\n\t\t\treturn;\n\t\t}\n\n\t\tImGui::SetWindowFocus();\n\n\t\t// As a specific feature guaranteed by the library, after calling Begin() the last Item represent the title bar. So e.g. IsItemHovered() will return true when hovering the title bar.\n\t\t// Here we create a context menu only available from the title bar.\n\t\tif (ImGui::BeginPopupContextItem())\n\t\t{\n\t\t\tif (ImGui::MenuItem(\"Close Console\"))\n\t\t\t\t*p_open = false;\n\t\t\tImGui::EndPopup();\n\t\t}\n\n\t\tconst float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); // 1 separator, 1 input text\n\t\tImGui::BeginChild(\"ScrollingRegion\", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar); // Leave room for 1 separator + 1 InputText\n\t\tif (ImGui::BeginPopupContextWindow())\n\t\t{\n\t\t\tif (ImGui::Selectable(\"Clear\")) ClearLog();\n\t\t\tImGui::EndPopup();\n\t\t}\n\n\t\t// Display every line as a separate entry so we can change their color or add custom widgets. If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end());\n\t\t// NB- if you have thousands of entries this approach may be too inefficient and may require user-side clipping to only process visible items.\n\t\t// You can seek and display only the lines that are visible using the ImGuiListClipper helper, if your elements are evenly spaced and you have cheap random access to the elements.\n\t\t// To use the clipper we could replace the 'for (int i = 0; i < Items.Size; i++)' loop with:\n\t\t//     ImGuiListClipper clipper(Items.Size);\n\t\t//     while (clipper.Step())\n\t\t//         for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)\n\t\t// However, note that you can not use this code as is if a filter is active because it breaks the 'cheap random-access' property. We would need random-access on the post-filtered list.\n\t\t// A typical application wanting coarse clipping and filtering may want to pre-compute an array of indices that passed the filtering test, recomputing this array when user changes the filter,\n\t\t// and appending newly elements as they are inserted. This is left as a task to the user until we can manage to improve this example code!\n\t\t// If your items are of variable size you may want to implement code similar to what ImGuiListClipper does. Or split your data into fixed height items to allow random-seeking into your list.\n\t\tImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing\n\n\t\tfor (int i = 0; i < Items.Size; i++)\n\t\t{\n\t\t\tconst char* item = Items[i];\n\t\t\tif (!filter.PassFilter(item))\n\t\t\t\tcontinue;\n\n\t\t\tImVec4 col = text_col;\n\t\t\tif (strstr(item, \"[W]\")) col = ImColor(1.f, 128.f / 255.f, 0.f, 1.0f);\n\t\t\tif (strstr(item, \"[C]\")) col = ImColor(1.0f, 0.4f, 0.4f, 1.0f);\n\t\t\tif (strstr(item, \"[E]\")) col = ImColor(1.0f, 0.4f, 0.4f, 1.0f);\n\t\t\telse if (strncmp(item, \"# \", 2) == 0) col = ImColor(1.0f, 0.78f, 0.58f, 1.0f);\n\t\t\tImGui::PushStyleColor(ImGuiCol_Text, col);\n\t\t\tImGui::TextUnformatted(item);\n\t\t\tImGui::PopStyleColor();\n\t\t}\n\n\t\tif (ScrollToBottom)\n\t\t\tImGui::SetScrollHereY(1.0f);\n\t\tScrollToBottom = false;\n\t\tImGui::PopStyleVar();\n\t\tImGui::EndChild();\n\t\tImGui::Separator();\n\n\t\t// Command-line\n\t\tImGui::SetKeyboardFocusHere(0);\n\t\tif (ImGui::InputText(\"\", InputBuf, IM_ARRAYSIZE(InputBuf), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory, &TextEditCallbackStub, (void*)this))\n\t\t{\n\t\t\tchar* s = InputBuf;\n\t\t\tStrtrim(s);\n\t\t\tif (s[0])\n\t\t\t\tExecCommand(s);\n\t\t\tstrcpy(s, \"\");\n\n\t\t}\n\n\t\tImGui::End();\n\t\tImGui::PopStyleColor();\n\t\tImGui::PopStyleColor();\n\t}\n\n\tvoid DebugConsole::AddCommand(std::string name, Command::func_t func, std::string desc, bool starts_with)\n\t{\n\t\tm_commands.push_back({name, desc, func, starts_with});\n\t}\n\n\tvoid DebugConsole::ExecCommand(const char* command_line)\n\t{\n\t\tAddLog(\"# %s\\n\", command_line);\n\n\t\t// Insert into history. First find match and delete it so it can be pushed to the back. This isn't trying to be smart or optimal.\n\t\tHistoryPos = -1;\n\t\tfor (int i = History.Size - 1; i >= 0; i--)\n\t\t\tif (Stricmp(History[i], command_line) == 0)\n\t\t\t{\n\t\t\t\tfree(History[i]);\n\t\t\t\tHistory.erase(History.begin() + i);\n\t\t\t\tbreak;\n\t\t\t}\n\t\tHistory.push_back(Strdup(command_line));\n\n\t\tfor (auto& cmd : m_commands)\n\t\t{\n\t\t\tif (!cmd.m_starts_with && StrEquals(command_line, cmd.m_name))\n\t\t\t{\n\t\t\t\tcmd.m_function(*this, command_line);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse if (cmd.m_starts_with && StrStartsWith(command_line, cmd.m_name))\n\t\t\t{\n\t\t\t\tcmd.m_function(*this, command_line);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\tAddLog(\"Unknown command: '%s'\\n\", command_line);\n\t}\n\n\tint DebugConsole::TextEditCallbackStub(ImGuiInputTextCallbackData* data) // In C++11 you are better off using lambdas for this sort of forwarding callbacks\n\t{\n\t\tDebugConsole* console = (DebugConsole*)data->UserData;\n\t\treturn console->TextEditCallback(data);\n\t}\n\n\tint DebugConsole::TextEditCallback(ImGuiInputTextCallbackData* data)\n\t{\n\t\t//AddLog(\"cursor: %d, selection: %d-%d\", data->CursorPos, data->SelectionStart, data->SelectionEnd);\n\t\tswitch (data->EventFlag)\n\t\t{\n\t\tcase ImGuiInputTextFlags_CallbackCompletion:\n\t\t{\n\t\t\t// Example of TEXT COMPLETION\n\n\t\t\t// Locate beginning of current word\n\t\t\tconst char* word_end = data->Buf + data->CursorPos;\n\t\t\tconst char* word_start = word_end;\n\t\t\twhile (word_start > data->Buf)\n\t\t\t{\n\t\t\t\tconst char c = word_start[-1];\n\t\t\t\tif (c == ' ' || c == '\\t' || c == ',' || c == ';')\n\t\t\t\t\tbreak;\n\t\t\t\tword_start--;\n\t\t\t}\n\n\t\t\t// Build a list of candidates\n\t\t\tImVector<const char*> candidates;\n\t\t\tfor (int i = 0; i < m_commands.size(); i++)\n\t\t\t\tif (Strnicmp(m_commands[i].m_name.c_str(), word_start, (int)(word_end - word_start)) == 0)\n\t\t\t\t\tcandidates.push_back(m_commands[i].m_name.c_str());\n\n\t\t\tif (candidates.Size == 0)\n\t\t\t{\n\t\t\t\t// No match\n\t\t\t\tAddLog(\"No match for \\\"%.*s\\\"!\\n\", (int)(word_end - word_start), word_start);\n\t\t\t}\n\t\t\telse if (candidates.Size == 1)\n\t\t\t{\n\t\t\t\t// Single match. Delete the beginning of the word and replace it entirely so we've got nice casing\n\t\t\t\tdata->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));\n\t\t\t\tdata->InsertChars(data->CursorPos, candidates[0]);\n\t\t\t\tdata->InsertChars(data->CursorPos, \" \");\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t// Multiple matches. Complete as much as we can, so inputing \"C\" will complete to \"CL\" and display \"CLEAR\" and \"CLASSIFY\"\n\t\t\t\tint match_len = (int)(word_end - word_start);\n\t\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\t\tint c = 0;\n\t\t\t\t\tbool all_candidates_matches = true;\n\t\t\t\t\tfor (int i = 0; i < candidates.Size && all_candidates_matches; i++)\n\t\t\t\t\t\tif (i == 0)\n\t\t\t\t\t\t\tc = toupper(candidates[i][match_len]);\n\t\t\t\t\t\telse if (c == 0 || c != toupper(candidates[i][match_len]))\n\t\t\t\t\t\t\tall_candidates_matches = false;\n\t\t\t\t\tif (!all_candidates_matches)\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tmatch_len++;\n\t\t\t\t}\n\n\t\t\t\tif (match_len > 0)\n\t\t\t\t{\n\t\t\t\t\tdata->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));\n\t\t\t\t\tdata->InsertChars(data->CursorPos, candidates[0], candidates[0] + match_len);\n\t\t\t\t}\n\n\t\t\t\t// List matches\n\t\t\t\tAddLog(\"Possible matches:\\n\");\n\t\t\t\tfor (int i = 0; i < candidates.Size; i++)\n\t\t\t\t\tAddLog(\"- %s\\n\", candidates[i]);\n\t\t\t}\n\n\t\t\tbreak;\n\t\t}\n\t\tcase ImGuiInputTextFlags_CallbackHistory:\n\t\t{\n\t\t\t// Example of HISTORY\n\t\t\tconst int prev_history_pos = HistoryPos;\n\t\t\tif (data->EventKey == ImGuiKey_UpArrow)\n\t\t\t{\n\t\t\t\tif (HistoryPos == -1)\n\t\t\t\t\tHistoryPos = History.Size - 1;\n\t\t\t\telse if (HistoryPos > 0)\n\t\t\t\t\tHistoryPos--;\n\t\t\t}\n\t\t\telse if (data->EventKey == ImGuiKey_DownArrow)\n\t\t\t{\n\t\t\t\tif (HistoryPos != -1)\n\t\t\t\t\tif (++HistoryPos >= History.Size)\n\t\t\t\t\t\tHistoryPos = -1;\n\t\t\t}\n\n\t\t\t// A better implementation would preserve the data on the current input line along with cursor position.\n\t\t\tif (prev_history_pos != HistoryPos)\n\t\t\t{\n\t\t\t\tconst char* history_str = (HistoryPos >= 0) ? History[HistoryPos] : \"\";\n\t\t\t\tdata->DeleteChars(0, data->BufTextLen);\n\t\t\t\tdata->InsertChars(0, history_str);\n\t\t\t}\n\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\n} /* wr::imgui::special */\n"
  },
  {
    "path": "src/imgui_tools.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <vector>\n#include <functional>\n#include <typeindex>\n\n#include \"imgui/imgui.hpp\"\n#include \"wisprenderer_export.hpp\"\n#include \"scene_graph/light_node.hpp\"\n\nnamespace wr\n{\n\tclass D3D12RenderSystem;\n\tclass SceneGraph;\n}\n\nnamespace wr::imgui\n{\n\n\tnamespace menu\n\t{\n\t\tvoid Registries();\n\t}\n\n\tnamespace window\n\t{\n\t\tstruct Stats\n\t\t{\n\t\t\tvoid Draw(D3D12RenderSystem& render_system, ImVec2 viewport_pos);\n\t\t\tbool m_open;\n\t\t};\n\n\t\tvoid ShaderRegistry();\n\t\tvoid PipelineRegistry();\n\t\tvoid RootSignatureRegistry();\n\t\tvoid D3D12HardwareInfo(D3D12RenderSystem& render_system);\n\t\tvoid D3D12Settings();\n\t\tvoid SceneGraphEditor(SceneGraph* scene_graph);\n\t\tvoid Inspector(SceneGraph* scene_graph, ImVec2 viewport_pos, ImVec2 viewport_size);\n\n\t\tstatic bool open_hardware_info = true;\n\t\tstatic bool open_d3d12_settings = true;\n\t\tstatic bool open_shader_registry = false;\n\t\tstatic bool open_shader_compiler_popup = false;\n\t\tstatic std::string shader_compiler_error = \"No shader error\";\n\t\tstatic bool open_pipeline_registry = false;\n\t\tstatic bool open_root_signature_registry = false;\n\t\tstatic bool open_scene_graph_editor = true;\n\t\tstatic bool open_inspector = true;\n\t\tstatic wr::LightNode* selected_light = nullptr;\n\t\tstatic bool light_selected = false;\n\n\t\tstatic std::shared_ptr<Node> selected_node = nullptr;\n\n\t\tstruct SceneGraphEditorDetails\n\t\t{\n\t\t\tusing name_func_t = std::function<std::string(std::shared_ptr<Node>)>;\n\t\t\tusing inspect_func_t = std::function<void(std::shared_ptr<Node>, SceneGraph*)>;\n\t\t\tusing context_menu_func_t = std::function<bool(std::shared_ptr<Node>, SceneGraph*)>;\n\t\t\tWISPRENDERER_EXPORT static std::unordered_map<std::type_index, name_func_t> sg_editor_type_names;\n\t\t\tWISPRENDERER_EXPORT static std::unordered_map<std::type_index, inspect_func_t> sg_editor_type_inspect;\n\t\t\tWISPRENDERER_EXPORT static std::unordered_map<std::type_index, context_menu_func_t> sg_editor_type_context_menu;\n\n\t\t\tWISPRENDERER_EXPORT static std::optional<std::string> GetNodeName(std::shared_ptr<Node> node)\n\t\t\t{\n\t\t\t\tif (auto it = SceneGraphEditorDetails::sg_editor_type_names.find(node->m_type_info); it != SceneGraphEditorDetails::sg_editor_type_names.end())\n\t\t\t\t{\n\t\t\t\t\treturn it->second(node);\n\t\t\t\t}\n\n\t\t\t\treturn std::nullopt;\n\t\t\t}\n\n\t\t\tWISPRENDERER_EXPORT static std::optional<inspect_func_t> GetNodeInspectFunction(std::shared_ptr<Node> node)\n\t\t\t{\n\t\t\t\tif (auto it = SceneGraphEditorDetails::sg_editor_type_inspect.find(node->m_type_info); it != SceneGraphEditorDetails::sg_editor_type_inspect.end())\n\t\t\t\t{\n\t\t\t\t\treturn it->second;\n\t\t\t\t}\n\n\t\t\t\treturn std::nullopt;\n\t\t\t}\n\n\t\t\tWISPRENDERER_EXPORT static std::optional<context_menu_func_t> GetNodeContextMenuFunction(std::shared_ptr<Node> node)\n\t\t\t{\n\t\t\t\tif (auto it = SceneGraphEditorDetails::sg_editor_type_context_menu.find(node->m_type_info); it != SceneGraphEditorDetails::sg_editor_type_context_menu.end())\n\t\t\t\t{\n\t\t\t\t\treturn it->second;\n\t\t\t\t}\n\n\t\t\t\treturn std::nullopt;\n\t\t\t}\n\t\t};\n\t}\n\n\tnamespace special\n\t{\n\t\tstruct DebugConsole\n\t\t{\n\t\t\tstruct Command\n\t\t\t{\n\t\t\t\tusing func_t = std::function<void(DebugConsole&, std::string const &)>;\n\t\t\t\tstd::string m_name;\n\t\t\t\tstd::string m_description;\n\t\t\t\tfunc_t m_function;\n\t\t\t\tbool m_starts_with = false;\n\t\t\t};\n\n\t\t\tchar                  InputBuf[256];\n\t\t\tImVector<char*>       Items;\n\t\t\tbool                  ScrollToBottom;\n\t\t\tImVector<char*>       History;\n\t\t\tint                   HistoryPos;    // -1: new line, 0..History.Size-1 browsing history.\n\t\t\tstd::vector<Command> m_commands;\n\t\t\tImGuiTextFilter filter;\n\n\t\t\tDebugConsole();\n\t\t\t~DebugConsole();\n\n\t\t\tvoid EmptyInput();\n\n\t\t\tvoid ClearLog();\n\n\t\t\tvoid AddLog(const char* fmt, ...);\n\n\t\t\tvoid Draw(const char* title, bool* p_open);\n\n\t\t\tvoid AddCommand(std::string name, Command::func_t func, std::string desc = \"\", bool starts_with = false);\n\n\t\tprivate:\n\t\t\tvoid ExecCommand(const char* command_line);\n\n\t\t\tstatic int TextEditCallbackStub(ImGuiInputTextCallbackData* data);\n\n\t\t\tint TextEditCallback(ImGuiInputTextCallbackData* data);\n\t\t};\n\t}\n\n} /* wr::imgui */\n"
  },
  {
    "path": "src/material_pool.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#define NOMINMAX\n\n#include \"resource_pool_texture.hpp\"\n#include \"material_pool.hpp\"\n#include \"util/log.hpp\"\n\n#include <assimp/material.h>\n\nnamespace wr \n{\n\t//////////////////////////////\n\t//\t  MATERIAL FUNCTIONS\t//\n\t//////////////////////////////\n\n\tMaterial::Material(TexturePool* pool): m_texture_pool(pool)\n\t{\n\t\tmemset(&m_material_data, 0, sizeof(m_material_data));\n\n\t\tSetConstant<MaterialConstant::EMISSIVE_MULTIPLIER>(1.0f);\n\t\tSetConstant<MaterialConstant::ALBEDO_UV_SCALE>(1.0f);\n\t\tSetConstant<MaterialConstant::NORMAL_UV_SCALE>(1.0f);\n\t\tSetConstant<MaterialConstant::ROUGHNESS_UV_SCALE>(1.0f);\n\t\tSetConstant<MaterialConstant::METALLIC_UV_SCALE>(1.0f);\n\t\tSetConstant<MaterialConstant::EMISSIVE_UV_SCALE>(1.0f);\n\t\tSetConstant<MaterialConstant::AO_UV_SCALE>(1.0f);\n\t}\n\n\tMaterial::Material(TexturePool *pool,\n\t\t\t\t\t   TextureHandle albedo,\n\t\t\t\t\t   TextureHandle normal,\n\t\t\t\t\t   TextureHandle roughness,\n\t\t\t\t\t   TextureHandle metallic,\n\t\t\t\t\t   TextureHandle emissive,\n\t\t               TextureHandle ao,\n\t\t\t\t\t   MaterialUVScales scales,\n\t\t               bool alpha_masked,\n\t\t\t\t\t   bool double_sided): Material(pool)\n\t{\n\n\t\tSetTexture(TextureType::ALBEDO, albedo);\n\t\tSetTexture(TextureType::NORMAL, normal);\n\t\tSetTexture(TextureType::ROUGHNESS, roughness);\n\t\tSetTexture(TextureType::METALLIC, metallic);\n\t\tSetTexture(TextureType::EMISSIVE, emissive);\n\t\tSetTexture(TextureType::AO, ao);\n\n\t\tm_material_data.albedo_scale = scales.m_albedo_scale;\n\t\tm_material_data.normal_scale = scales.m_normal_scale;\n\t\tm_material_data.roughness_scale = scales.m_roughness_scale;\n\t\tm_material_data.metallic_scale = scales.m_metallic_scale;\n\t\tm_material_data.emissive_scale = scales.m_emissive_scale;\n\t\tm_material_data.ao_scale = scales.m_ao_scale;\n\n\t\tSetConstant<MaterialConstant::ALBEDO_UV_SCALE>(scales.m_albedo_scale);\n\t\tSetConstant<MaterialConstant::NORMAL_UV_SCALE>(scales.m_normal_scale);\n\t\tSetConstant<MaterialConstant::ROUGHNESS_UV_SCALE>(scales.m_roughness_scale);\n\t\tSetConstant<MaterialConstant::METALLIC_UV_SCALE>(scales.m_metallic_scale);\n\t\tSetConstant<MaterialConstant::EMISSIVE_UV_SCALE>(scales.m_emissive_scale);\n\t\tSetConstant<MaterialConstant::AO_UV_SCALE>(scales.m_ao_scale);\n\t\tSetConstant<MaterialConstant::IS_ALPHA_MASKED>(alpha_masked);\n\t\tSetConstant<MaterialConstant::IS_DOUBLE_SIDED>(double_sided);\n\t\tSetConstant<MaterialConstant::EMISSIVE_MULTIPLIER>(1.0f);\n\t}\n\n\tTextureHandle Material::GetTexture(TextureType type) { \n\t\treturn m_textures[size_t(type) % size_t(TextureType::COUNT)]; \n\t}\n\n\tvoid Material::SetTexture(TextureType type, TextureHandle handle)\n\t{\n\t\tif (handle.m_pool != m_texture_pool || !m_texture_pool)\n\t\t{\n\t\t\tClearTexture(type);\n\t\t\t//LOGW(\"Textures in a material need to belong to the same texture pool\");\n\t\t}\n\t\telse\n\t\t{\n\t\t\tm_textures[size_t(type) % size_t(TextureType::COUNT)] = handle;\n\t\t\tm_material_data.m_material_flags.m_value = uint32_t(m_material_data.m_material_flags.m_value) | (1 << uint32_t(type));\n\t\t}\n\t}\n\n\n\tvoid Material::ClearTexture(TextureType type) {\n\t\tm_textures[size_t(type) % size_t(TextureType::COUNT)] = { nullptr, 0 };\n\t\tm_material_data.m_material_flags.m_value = uint32_t(m_material_data.m_material_flags.m_value) & (~(1 << uint32_t(type)));\n\t}\n\n\tbool Material::HasTexture(TextureType type) {\n\t\treturn m_textures[size_t(type) % size_t(TextureType::COUNT)].m_pool;\n\t}\n\n\tMaterial::~Material()\n\t{\n\t\tm_constant_buffer_handle->m_pool->Destroy(m_constant_buffer_handle);\n\t}\n\n\tvoid Material::UpdateConstantBuffer()\n\t{\n\n\t\tm_constant_buffer_handle->m_pool->Update(\n\t\t\tm_constant_buffer_handle,\n\t\t\tsizeof(MaterialData),\n\t\t\t0,\n\t\t\treinterpret_cast<std::uint8_t*>(&m_material_data));\n\t}\n\n\t//////////////////////////////\n\t//\tMATERIAL POOL FUNCTIONS\t//\n\t//////////////////////////////\n\n\tMaterialPool::MaterialPool() {}\n\n\tMaterialPool::~MaterialPool()\n\t{\n\t\tfor(auto& m : m_materials)\n\t\t{\n\t\t\tdelete m.second;\n\t\t}\n\t}\n\n\tMaterialHandle MaterialPool::Create(TexturePool* pool)\n\t{\n\t\tMaterialHandle handle;\n\t\thandle.m_pool = this;\n\t\thandle.m_id = m_id_factory.GetUnusedID();\n\n\t\tMaterial* mat = new Material(pool);\n\t\tmat->SetConstantBufferHandle(m_constant_buffer_pool->Create(sizeof(Material::MaterialData)));\n\n\t\tm_materials.insert(std::make_pair(handle.m_id, mat));\n\n\t\treturn handle;\n\t}\n\n\tMaterialHandle MaterialPool::Create(TexturePool* pool, TextureHandle& albedo, TextureHandle& normal,\n\t\t\t\t\t\t\t\t\t\tTextureHandle& roughness, TextureHandle& metallic, TextureHandle& emissive,\n\t\t\t\t\t\t\t\t\t\tTextureHandle& ao, MaterialUVScales& mat_scales, bool is_alpha_masked, bool is_double_sided)\n\t{\n\t\tMaterialHandle handle = {};\n\t\thandle.m_pool = this;\n\t\thandle.m_id = m_id_factory.GetUnusedID();\n\n\t\tMaterial* mat = new Material(pool, albedo, normal, roughness, metallic, emissive, ao, mat_scales, is_alpha_masked, is_double_sided);\n\t\tmat->SetConstantBufferHandle(m_constant_buffer_pool->Create(sizeof(Material::MaterialData)));\n\t\tmat->UpdateConstantBuffer();\n\n\t\tm_materials.insert(std::make_pair(handle.m_id, mat));\n\n\t\treturn handle;\n\t}\n\n\tMaterial* MaterialPool::GetMaterial(MaterialHandle handle)\n\t{\n\t\t// Return the material if available.\n\t\tif (auto it = m_materials.find(handle.m_id); it != m_materials.end())\n\t\t{\n\t\t\treturn it->second;\n\t\t}\n\n\t\tLOGE(\"Failed to obtain a material from pool.\");\n\t\treturn nullptr;\n\t}\n\n\tvoid MaterialPool::DestroyMaterial(MaterialHandle handle)\n\t{\n\t\tauto it = m_materials.find(handle.m_id);\n\n\t\tif (it == m_materials.end())\n\t\t{\n\t\t\treturn;\n\t\t\t//LOGC(\"Can't destroy material; it's not part of the material pool\");\n\t\t}\n\n\t\tauto *ptr = it->second;\n\t\tm_materials.erase(it);\n\t\tdelete ptr;\n\n\t}\n\n\tbool MaterialPool::HasMaterial(MaterialHandle handle) const\n\t{\n\t\treturn m_materials.find(handle.m_id) != m_materials.end();\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/material_pool.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <string_view>\n#include <unordered_map>\n#include <vector>\n#include <optional>\n#include <stdint.h>\n#include <d3d12.h>\n#include <DirectXMath.h>\n#include <memory>\n#include <array>\n\n#include \"structs.hpp\"\n#include \"util/defines.hpp\"\n#include \"util/log.hpp\"\n#include \"id_factory.hpp\"\n#include \"constant_buffer_pool.hpp\"\n\nstruct aiMaterial;\n\nnamespace wr\n{\n\tenum class TextureType : size_t\n\t{\n\t\tALBEDO = 0,\n\t\tNORMAL,\n\t\tROUGHNESS,\n\t\tMETALLIC,\n\t\tEMISSIVE,\n\t\tAO,\n\t\tCOUNT\n\t};\n\n\t//u32 type = { u16 offset (in floats), u16 isArray (0 = a constant, 1 = an array) }\n\tenum class MaterialConstant : uint32_t\n\t{\n\t\tCOLOR = (0 << 16) | 1,\n\t\tR = (0 << 16) | 0,\n\t\tG = (1 << 16) | 0,\n\t\tB = (2 << 16) | 0,\n\t\tMETALLIC = (3 << 16) | 0,\n\t\tROUGHNESS = (4 << 16) | 0,\n\t\tEMISSIVE_MULTIPLIER = (5 << 16) | 0,\n\t\tIS_DOUBLE_SIDED = (6 << 16) | 0,\n\t\tIS_ALPHA_MASKED = (7 << 16) | 0,\n\t\tALBEDO_UV_SCALE = (8 << 16) | 0,\n\t\tNORMAL_UV_SCALE = (9 << 16) | 0,\n\t\tROUGHNESS_UV_SCALE = (10 << 16) | 0,\n\t\tMETALLIC_UV_SCALE = (11 << 16) | 0,\n\t\tEMISSIVE_UV_SCALE = (12 << 16) | 0,\n\t\tAO_UV_SCALE = (13 << 16) | 0,\n\t\tMAX_OFFSET = 14\n\t};\n\n\tclass Material\n\t{\n\tpublic:\n\n\t\tMaterial(TexturePool* pool);\n\t\tMaterial(TexturePool* pool,\n\t\t\t\t TextureHandle albedo, \n\t\t\t\t TextureHandle normal, \n\t\t\t\t TextureHandle roughness,\n\t\t\t\t TextureHandle metallic,\n\t\t\t\t TextureHandle emissive,\n\t\t\t\t TextureHandle ao, \n\t\t\t\t MaterialUVScales mat_scales,\n\t\t\t\t bool alpha_masked = false,\n\t\t\t\t bool double_sided = false);\n\n\t\tMaterial(const Material& rhs) = default;\n\n\t\t~Material();\n\n\t\tTextureHandle GetTexture(TextureType type);\n\t\tvoid SetTexture(TextureType type, TextureHandle handle);\n\t\tvoid ClearTexture(TextureType type);\n\t\tbool HasTexture(TextureType type);\n\n\t\ttemplate<MaterialConstant type>\n\t\ttypename std::enable_if<uint16_t(type) == 0, float>::type GetConstant() {\n\t\t\treturn m_material_data.m_constant_data[uint32_t(type) >> 16];\n\t\t}\n\n\t\ttemplate<MaterialConstant type>\n\t\ttypename std::enable_if<uint16_t(type) != 0, std::array<float, 3>>::type GetConstant() {\n\t\t\tfloat* arr = m_material_data.m_constant_data;\n\t\t\tuint32_t i = uint32_t(type) >> 16;\n\t\t\treturn { arr[i], arr[i + 1], arr[i + 2] };\n\t\t}\n\n\t\ttemplate<MaterialConstant type>\n\t\tvoid SetConstant(typename std::enable_if<uint16_t(type) == 0, float>::type x) {\n\t\t\tm_material_data.m_constant_data[uint32_t(type) >> 16] = x;\n\t\t}\n\n\t\ttemplate<MaterialConstant type>\n\t\tvoid SetConstant(const typename std::enable_if<uint16_t(type) != 0, std::array<float, 3>>::type& val) {\n\t\t\tfloat* arr = m_material_data.m_constant_data;\n\t\t\tuint32_t i = uint32_t(type) >> 16;\n\t\t\tmemcpy(arr, val.data(), sizeof(val));\n\t\t}\n\n\t\tTexturePool* const GetTexturePool() { return m_texture_pool; }\n\n\t\tConstantBufferHandle* const GetConstantBufferHandle() const { return m_constant_buffer_handle; };\n\t\tvoid SetConstantBufferHandle(ConstantBufferHandle* handle) { m_constant_buffer_handle = handle; };\n\n\t\tvoid UpdateConstantBuffer();\n\n\t\tunion TextureFlags\n\t\t{\n\t\t\tstruct {\n\t\t\t\tuint32_t m_has_albedo_texture : 1;\n\t\t\t\tuint32_t m_has_normal_texture : 1;\n\t\t\t\tuint32_t m_has_roughness_texture : 1;\n\t\t\t\tuint32_t m_has_metallic_texture : 1;\n\t\t\t\tuint32_t m_has_emissive_texture : 1;\n\t\t\t\tuint32_t m_has_ao_texture : 1;\n\t\t\t};\n\n\t\t\tuint32_t m_value;\n\t\t};\n\n\t\tunion MaterialData\n\t\t{\n\t\t\tstruct {\n\n\t\t\t\tfloat m_color[3];\n\t\t\t\tfloat m_metallic;\n\n\t\t\t\tfloat m_roughness;\n\t\t\t\tfloat m_emissive_multiplier;\n\t\t\t\tfloat m_is_double_sided;\n\t\t\t\tfloat m_use_alpha_constant;\n\n\t\t\t\tfloat albedo_scale;\n\t\t\t\tfloat normal_scale;\n\t\t\t\tfloat roughness_scale;\n\t\t\t\tfloat metallic_scale;\n\n\t\t\t\tfloat emissive_scale;\n\t\t\t\tfloat ao_scale;\n\t\t\t\tfloat m_padding;\n\t\t\t\tTextureFlags m_material_flags;\n\n\t\t\t};\n\n\t\t\tfloat m_constant_data[size_t(MaterialConstant::MAX_OFFSET)]{};\n\n\t\t};\n\n\t\tMaterialData GetMaterialData() const { return m_material_data; }\n\n\tprotected:\n\n\t\tunion {\n\n\t\t\tTextureHandle m_textures[size_t(TextureType::COUNT)]{};\n\n\t\t\tstruct {\n\t\t\t\tTextureHandle m_albedo;\n\t\t\t\tTextureHandle m_normal;\n\t\t\t\tTextureHandle m_roughness;\n\t\t\t\tTextureHandle m_metallic;\n\t\t\t\tTextureHandle m_emissive;\n\t\t\t\tTextureHandle m_ao;\n\t\t\t};\n\n\t\t};\n\n\t\tTexturePool* m_texture_pool = nullptr;\n\n\t\tConstantBufferHandle* m_constant_buffer_handle;\n\n\t\tMaterialData m_material_data;\n\t};\n\n\tclass MaterialPool\n\t{\n\tpublic:\n\t\texplicit MaterialPool();\n\t\tvirtual ~MaterialPool();\n\n\t\tMaterialPool(MaterialPool const &) = delete;\n\t\tMaterialPool& operator=(MaterialPool const &) = delete;\n\t\tMaterialPool(MaterialPool&&) = delete;\n\t\tMaterialPool& operator=(MaterialPool&&) = delete;\n\n\t\t//Creates an empty material. The user is responsible of filling in the texture handles.\n\t\t//TODO: Give Materials default textures\n\t\t[[nodiscard]] MaterialHandle Create(TexturePool* pool);\n\t\t[[nodiscard]] MaterialHandle Create(TexturePool* pool,\n\t\t\t\t\t\t\t\t\t\t\tTextureHandle& albedo,\n\t\t\t\t\t\t\t\t\t\t\tTextureHandle& normal,\n\t\t\t\t\t\t\t\t\t\t\tTextureHandle& roughness,\n\t\t\t\t\t\t\t\t\t\t\tTextureHandle& metallic,\n\t\t\t\t\t\t\t\t\t\t\tTextureHandle& emissive,\n\t\t\t\t\t\t\t\t\t\t\tTextureHandle& ao,\n\t\t\t\t\t\t\t\t\t\t\tMaterialUVScales& mat_scales,\n\t\t\t\t\t\t\t\t\t\t\tbool is_alpha_masked = false, \n\t\t\t\t\t\t\t\t\t\t\tbool is_double_sided = false);\n\n\n\n\t\tvirtual void Evict() {}\n\t\tvirtual void MakeResident() {}\n\n\t\t/*! Obtain a material from the pool. */\n\t\t/*!\n\t\t\tThrows an error if no material was found.\n\t\t*/\n\t\tvirtual Material* GetMaterial(MaterialHandle handle);\n\t\t/*! Check if the material owns a material with the specified handle */\n\t\tbool HasMaterial(MaterialHandle handle) const;\n\n\t\tvoid DestroyMaterial(MaterialHandle handle);\n\n\tprotected:\n\t\tstd::shared_ptr<ConstantBufferPool> m_constant_buffer_pool;\n\n\t\tstd::unordered_map<uint64_t, Material*> m_materials;\n\n\t\tIDFactory m_id_factory;\n\t};\n\n\n} /* wr */"
  },
  {
    "path": "src/model_loader.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"model_loader.hpp\"\n\nnamespace wr\n{\n\tWISPRENDERER_EXPORT std::vector<ModelLoader*> ModelLoader::m_registered_model_loaders = std::vector<ModelLoader*>();\n\n\tModelLoader::ModelLoader()\n\t{\n\t\tm_registered_model_loaders.push_back(this);\n\t}\n\n\tModelLoader::~ModelLoader()\n\t{\n\t\tstd::vector<ModelData*> temp = m_loaded_models;\n\t\tfor (ModelData* model : temp)\n\t\t{\n\t\t\tDeleteModel(model);\n\t\t}\n\n\t\tfor (std::vector<ModelLoader*>::iterator it = m_registered_model_loaders.begin();\n\t\t\tit != m_registered_model_loaders.end(); ++it)\n\t\t{\n\t\t\tif ((*it) == this)\n\t\t\t{\n\t\t\t\tm_registered_model_loaders.erase(it);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tModelData * ModelLoader::Load(std::string_view model_path)\n\t{\n\t\tModelData* model = LoadModel(model_path);\n\t\tm_loaded_models.push_back(model);\n\t\treturn model;\n\t}\n\n\tModelData * ModelLoader::Load(void * data, std::size_t length, std::string format)\n\t{\n\t\tModelData* model = LoadModel(data, length, format);\n\t\tm_loaded_models.push_back(model);\n\t\treturn model;\n\t}\n\n\tvoid ModelLoader::DeleteModel(ModelData * model)\n\t{\n\t\tstd::vector<ModelData*>::iterator it = std::find(m_loaded_models.begin(), m_loaded_models.end(), model);\n\n\t\tif (it == m_loaded_models.end())\n\t\t{\n\t\t\treturn;\n\t\t}\n\n\t\tm_loaded_models.erase(it);\n\n\t\tfor (int i = 0; i < model->m_meshes.size(); ++i) \n\t\t{\n\t\t\tdelete model->m_meshes[i];\n\t\t}\n\n\t\tfor (int i = 0; i < model->m_materials.size(); ++i) \n\t\t{\n\t\t\tdelete model->m_materials[i];\n\t\t}\n\n\t\tfor (int i = 0; i < model->m_embedded_textures.size(); ++i)\n\t\t{\n\t\t\tdelete model->m_embedded_textures[i];\n\t\t}\n\n\t\tfor (int i = 0; i < model->m_skeleton_data->m_animations.size(); ++i)\n\t\t{\n\t\t\tdelete model->m_skeleton_data->m_animations[i];\n\t\t}\n\n\t\tfor (int i = 0; i < model->m_skeleton_data->m_bones.size(); ++i)\n\t\t{\n\t\t\tdelete model->m_skeleton_data->m_bones[i];\n\t\t}\n\n\t\tdelete model->m_skeleton_data;\n\t\tdelete model;\n\t}\n\n\tstd::vector<std::string> ModelLoader::SupportedModelFormats()\n\t{\n\t\treturn m_supported_model_formats;\n\t}\n\n\tbool ModelLoader::SupportsModelFormat(const std::string_view& model_format)\n\t{\n\t\tfor (int i = 0; i < m_supported_model_formats.size(); ++i)\n\t\t{\n\t\t\tif (m_supported_model_formats[i].compare(model_format) == 0)\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\tModelLoader * ModelLoader::FindFittingModelLoader(const std::string_view & model_format)\n\t{\n\t\tfor (int i = 0; i < m_registered_model_loaders.size(); ++i)\n\t\t{\n\t\t\tif (m_registered_model_loaders[i]->SupportsModelFormat(model_format))\n\t\t\t{\n\t\t\t\treturn m_registered_model_loaders[i];\n\t\t\t}\n\t\t}\n\t\treturn nullptr;\n\t}\n\n}"
  },
  {
    "path": "src/model_loader.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <string>\n#include <vector>\n#include <DirectXMath.h>\n\n#include \"wisprenderer_export.hpp\"\n\nnamespace wr\n{\n\tstruct ModelMeshData;\n\tstruct ModelMaterialData;\n\tstruct ModelData;\n\tstruct ModelSkeletonData;\n\tstruct ModelBoneData;\n\tstruct ModelAnimationData;\n\n\tstruct EmbeddedTexture;\n\n\tenum class TextureLocation\n\t{\n\t\tEXTERNAL = 0,\n\t\tEMBEDDED,\n\t\tNON_EXISTENT\n\t};\n\n\tstruct ModelData\n\t{\n\t\tstd::vector<ModelMeshData*> m_meshes;\n\t\tstd::vector<ModelMaterialData*> m_materials;\n\t\tstd::vector<EmbeddedTexture*> m_embedded_textures;\n\t\tModelSkeletonData* m_skeleton_data;\n\n\t\ttemplate<typename TV> size_t GetTotalVertexSize()\n\t\t{\n\t\t\tsize_t size = 0;\n\t\t\tfor (auto& mesh : m_meshes)\n\t\t\t{\n\t\t\t\tsize += mesh->GetTotalVertexSize<TV>();\n\t\t\t}\n\t\t\treturn size;\n\t\t};\n\n\t\ttemplate<typename TI> size_t GetTotalIndexSize()\n\t\t{\n\t\t\tsize_t size = 0;\n\t\t\tfor (auto& mesh : m_meshes)\n\t\t\t{\n\t\t\t\tsize += mesh->GetTotalVertexSize<TI>();\n\t\t\t}\n\t\t\treturn size;\n\t\t};\n\t};\n\n\tstruct ModelMeshData\n\t{\n\t\tstd::vector<DirectX::XMFLOAT3> m_positions;\n\t\tstd::vector<DirectX::XMFLOAT3> m_colors;\n\t\tstd::vector<DirectX::XMFLOAT3> m_normals;\n\t\tstd::vector<DirectX::XMFLOAT3> m_uvw;\n\t\tstd::vector<DirectX::XMFLOAT3> m_tangents;\n\t\tstd::vector<DirectX::XMFLOAT3> m_bitangents;\n\n\t\tstd::vector<DirectX::XMFLOAT4> m_bone_weights;\n\n\t\tstd::vector<DirectX::XMFLOAT4> m_bone_ids;\n\n\t\tstd::vector<std::uint32_t> m_indices;\n\n\t\ttemplate<typename TV> size_t GetTotalVertexSize()\n\t\t{\n\t\t\treturn m_positions.size() * sizeof(TV);\n\t\t};\n\n\t\ttemplate<typename TI> size_t GetTotalIndexSize()\n\t\t{\n\t\t\treturn m_indices.size() * sizeof(TI);\n\t\t};\n\n\t\tint m_material_id;\n\t};\n\n\tstruct ModelMaterialData \n\t{\n\t\tstd::string m_albedo_texture;\n\t\tstd::size_t m_albedo_embedded_texture;\n\t\tTextureLocation m_albedo_texture_location;\n\n\t\tstd::string m_metallic_texture;\n\t\tstd::size_t m_metallic_embedded_texture;\n\t\tTextureLocation m_metallic_texture_location;\n\n\t\tstd::string m_roughness_texture;\n\t\tstd::size_t m_roughness_embedded_texture;\n\t\tTextureLocation m_roughness_texture_location;\n\n\t\tstd::string m_ambient_occlusion_texture;\n\t\tstd::size_t m_ambient_occlusion_embedded_texture;\n\t\tTextureLocation m_ambient_occlusion_texture_location;\n\n\t\tstd::string m_normal_map_texture;\n\t\tstd::size_t m_normal_map_embedded_texture;\n\t\tTextureLocation m_normal_map_texture_location;\n\n\t\tstd::string m_emissive_texture;\n\t\tstd::size_t m_emissive_embedded_texture;\n\t\tTextureLocation m_emissive_texture_location;\n\n\t\tfloat m_base_color[3];\n\t\tfloat m_base_metallic;\n\t\tfloat m_base_roughness;\n\t\tfloat m_base_transparency;\n\t\tfloat m_base_emissive;\n\t\t\n\t\tbool m_two_sided;\n\t};\n\n\tstruct ModelBoneData\n\t{\n\n\t};\n\n\tstruct ModelAnimationData\n\t{\n\n\t};\n\n\tstruct ModelSkeletonData \n\t{\n\t\tstd::vector<ModelBoneData*> m_bones;\n\t\tstd::vector<ModelAnimationData*> m_animations;\n\t};\n\n\tstruct EmbeddedTexture \n\t{\n\t\tstd::vector<unsigned char> m_data;\n\n\t\tstd::uint32_t m_width;\n\t\tstd::uint32_t m_height;\n\n\t\tbool m_compressed;\n\n\t\tstd::string m_format;\n\t};\n\n\tclass ModelLoader\n\t{\n\tpublic:\n\t\tModelLoader();\n\t\tvirtual ~ModelLoader();\n\n\t\t[[nodiscard]] ModelData* Load(std::string_view model_path);\n\t\t[[nodiscard]] ModelData* Load(void* data, std::size_t length, std::string format);\n\n\t\t// Cleans up the allocated memory for the model data. \n\t\t// Call this after you've finished interprenting the data. \n\t\t// Destroying the loader works as well.\n\t\tvoid DeleteModel(ModelData* model);\n\n\t\tstd::vector<std::string> SupportedModelFormats();\n\n\t\tbool SupportsModelFormat(const std::string_view& model_format);\n\n\t\tWISPRENDERER_EXPORT static ModelLoader* FindFittingModelLoader(const std::string_view& model_format);\n\n\t\tWISPRENDERER_EXPORT static std::vector<ModelLoader*> m_registered_model_loaders;\n\n\tprotected:\n\t\tvirtual ModelData* LoadModel(std::string_view model_path) = 0;\n\t\tvirtual ModelData* LoadModel(void* data, std::size_t length, std::string format) = 0;\n\n\t\tstd::vector<ModelData*> m_loaded_models;\n\t\tstd::vector<std::string> m_supported_model_formats;\n\t};\n} /* wr */\n"
  },
  {
    "path": "src/model_loader_assimp.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"model_loader_assimp.hpp\"\n\n#include \"util/log.hpp\"\n\nnamespace wr\n{\n\n\tAssimpModelLoader::AssimpModelLoader()\n\t{\n\t\tm_supported_model_formats = { \"obj\",\"fbx\",\"raw\",\"sib\",\"smd\" };\n\t}\n\n\tAssimpModelLoader::~AssimpModelLoader()\n\t{\n\t}\n\n\tModelData * AssimpModelLoader::LoadModel(std::string_view model_path)\n\t{\n\t\tAssimp::Importer importer;\n\t\tconst aiScene* scene = importer.ReadFile(model_path.data(),\n\t\t\taiProcess_Triangulate |\n\t\t\taiProcess_CalcTangentSpace |\n\t\t\taiProcess_JoinIdenticalVertices |\n\t\t\taiProcess_OptimizeMeshes |\n\t\t\taiProcess_ImproveCacheLocality | \n\t\t\taiProcess_MakeLeftHanded);\n\n\t\tif (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)\n\t\t{\n\t\t\tLOGW(std::string(\"Loading model \") +\n\t\t\t\tmodel_path.data() +\n\t\t\t\tstd::string(\" failed with error \") +\n\t\t\t\timporter.GetErrorString());\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tModelData* model = new ModelData;\n\n\t\tif (scene->HasTextures())\n\t\t{\n\t\t\tLoadEmbeddedTextures(model, scene);\n\t\t}\n\n\t\tLoadMaterials(model, scene);\n\t\tLoadMeshes(model, scene, scene->mRootNode);\n\n\t\tmodel->m_skeleton_data = new ModelSkeletonData();\n\n\t\treturn model;\n\t}\n\n\tModelData * AssimpModelLoader::LoadModel(void * data, std::size_t length, std::string format)\n\t{\n\t\tAssimp::Importer importer;\n\t\tconst aiScene* scene = importer.ReadFileFromMemory(data,\n\t\t\tlength,\n\t\t\taiProcess_Triangulate |\n\t\t\taiProcess_CalcTangentSpace |\n\t\t\taiProcess_JoinIdenticalVertices |\n\t\t\taiProcess_OptimizeMeshes |\n\t\t\taiProcess_ImproveCacheLocality |\n\t\t\taiProcess_MakeLeftHanded,\n\t\t\tformat.c_str());\n\n\t\tif (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)\n\t\t{\n\t\t\tLOGW(std::string(\"Loading model \") +\n\t\t\t\tstd::string(\"failed with error \") +\n\t\t\t\timporter.GetErrorString());\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tModelData* model = new ModelData;\n\n\t\tif (scene->HasTextures())\n\t\t{\n\t\t\tLoadEmbeddedTextures(model, scene);\n\t\t}\n\n\t\tLoadMaterials(model, scene);\n\t\tLoadMeshes(model, scene, scene->mRootNode);\n\n\t\tmodel->m_skeleton_data = new ModelSkeletonData();\n\n\t\treturn model;\n\t}\n\n\tvoid AssimpModelLoader::LoadMeshes(ModelData * model, const aiScene * scene, aiNode * node)\n\t{\n\t\tfor (unsigned int i = 0; i < node->mNumMeshes; ++i)\n\t\t{\n\t\t\tModelMeshData* mesh_data = new ModelMeshData();\n\t\t\taiMesh* mesh = scene->mMeshes[node->mMeshes[i]];\n\n\t\t\t//Copy data\n\n\t\t\tmesh_data->m_positions.resize(mesh->mNumVertices);\n\t\t\tmesh_data->m_normals.resize(mesh->mNumVertices);\n\t\t\tmesh_data->m_uvw.resize(mesh->mNumVertices);\n\t\t\tmesh_data->m_colors.resize(mesh->mNumVertices);\n\t\t\tmesh_data->m_tangents.resize(mesh->mNumVertices);\n\t\t\tmesh_data->m_bitangents.resize(mesh->mNumVertices);\n\t\t\tmesh_data->m_bone_weights.resize(mesh->mNumVertices);\n\t\t\tmesh_data->m_bone_ids.resize(mesh->mNumVertices);\n\n\t\t\t#ifdef ASSIMP_DOUBLE_PRECISION\n\t\t\tstatic_assert(false, \"Assimp should use single precision, since model_loader_assimp.cpp requires floats\");\n\t\t\t#endif\n\n\t\t\tconst size_t float3 = sizeof(float) * 3, float3s = mesh->mNumVertices * float3;\n\n\t\t\tmemcpy(mesh_data->m_positions.data(), mesh->mVertices, float3s);\n\n\t\t\tif (mesh->HasNormals())\n\t\t\t{\n\t\t\t\tmemcpy(mesh_data->m_normals.data(), mesh->mNormals, float3s);\n\t\t\t}\n\n\t\t\tif (mesh->HasTangentsAndBitangents())\n\t\t\t{\n\t\t\t\tmemcpy(mesh_data->m_tangents.data(), mesh->mTangents, float3s);\n\t\t\t\tmemcpy(mesh_data->m_bitangents.data(), mesh->mBitangents, float3s);\n\t\t\t}\n\n\t\t\tif (mesh->HasTextureCoords(0))\n\t\t\t{\n\t\t\t\tmemcpy(mesh_data->m_uvw.data(), mesh->mTextureCoords[0], float3s);\n\t\t\t}\n\n\t\t\tif (mesh->HasVertexColors(0))\n\t\t\t{\n\t\t\t\tfor (unsigned int j = 0; j < mesh->mNumVertices; ++j)\n\t\t\t\t{\n\t\t\t\t\tmemcpy(&mesh_data->m_colors[j], &mesh->mColors[0][j], float3);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t//Copy indices\n\n\t\t\tsize_t count = 0;\n\n\t\t\tfor (size_t j = 0; j < mesh->mNumFaces; ++j)\n\t\t\t{\n\t\t\t\taiFace *face = &mesh->mFaces[j];\n\t\t\t\tcount += face->mNumIndices;\n\t\t\t}\n\n\t\t\tmesh_data->m_indices.resize(count);\n\t\t\tcount = 0;\n\n\t\t\tfor (size_t j = 0; j < mesh->mNumFaces; ++j)\n\t\t\t{\n\t\t\t\taiFace *face = &mesh->mFaces[j];\n\t\t\t\tmemcpy(mesh_data->m_indices.data() + count, face->mIndices, face->mNumIndices * 4);\n\t\t\t\tcount += face->mNumIndices;\n\t\t\t}\n\n\t\t\tmesh_data->m_material_id = mesh->mMaterialIndex;\n\n\t\t\tmodel->m_meshes.push_back(mesh_data);\n\t\t}\n\n\t\tfor (unsigned int i = 0; i < node->mNumChildren; ++i)\n\t\t{\n\t\t\tLoadMeshes(model, scene, node->mChildren[i]);\n\t\t}\n\t}\n\n\tvoid AssimpModelLoader::LoadMaterials(ModelData * model, const aiScene * scene)\n\t{\n\t\tmodel->m_materials.resize(scene->mNumMaterials);\n\t\tfor (unsigned int i = 0; i < scene->mNumMaterials; ++i)\n\t\t{\n\t\t\taiMaterial* material = scene->mMaterials[i];\n\n\t\t\tModelMaterialData* material_data = new ModelMaterialData;\n\n\t\t\tif (material->GetTextureCount(aiTextureType_DIFFUSE) > 0)\n\t\t\t{\n\t\t\t\taiString path;\n\t\t\t\tmaterial->GetTexture(aiTextureType_DIFFUSE, 0, &path);\n\n\t\t\t\tif (path.data[0] == '*')\n\t\t\t\t{\n\t\t\t\t\tuint32_t index = atoi(path.C_Str() + 1);\n\t\t\t\t\tmaterial_data->m_albedo_embedded_texture = index;\n\t\t\t\t\tmaterial_data->m_albedo_texture_location = TextureLocation::EMBEDDED;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmaterial_data->m_albedo_texture = std::string(path.C_Str());\n\t\t\t\t\tmaterial_data->m_albedo_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmaterial_data->m_albedo_texture_location = TextureLocation::NON_EXISTENT;\n\t\t\t}\n\n\t\t\tif (material->GetTextureCount(aiTextureType_SPECULAR) > 0)\n\t\t\t{\n\t\t\t\taiString path;\n\t\t\t\tmaterial->GetTexture(aiTextureType_SPECULAR, 0, &path);\n\n\t\t\t\tif (path.data[0] == '*')\n\t\t\t\t{\n\t\t\t\t\tuint32_t index = atoi(path.C_Str() + 1);\n\t\t\t\t\tmaterial_data->m_metallic_embedded_texture = index;\n\t\t\t\t\tmaterial_data->m_metallic_texture_location = TextureLocation::EMBEDDED;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmaterial_data->m_metallic_texture = std::string(path.C_Str());\n\t\t\t\t\tmaterial_data->m_metallic_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmaterial_data->m_metallic_texture_location = TextureLocation::NON_EXISTENT;\n\t\t\t}\n\n\t\t\tif (material->GetTextureCount(aiTextureType_SHININESS) > 0)\n\t\t\t{\n\t\t\t\taiString path;\n\t\t\t\tmaterial->GetTexture(aiTextureType_SHININESS, 0, &path);\n\n\t\t\t\tif (path.data[0] == '*')\n\t\t\t\t{\n\t\t\t\t\tuint32_t index = atoi(path.C_Str() + 1);\n\t\t\t\t\tmaterial_data->m_roughness_embedded_texture = index;\n\t\t\t\t\tmaterial_data->m_roughness_texture_location = TextureLocation::EMBEDDED;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmaterial_data->m_roughness_texture = std::string(path.C_Str());\n\t\t\t\t\tmaterial_data->m_roughness_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmaterial_data->m_roughness_texture_location = TextureLocation::NON_EXISTENT;\n\t\t\t}\n\n\t\t\tif (material->GetTextureCount(aiTextureType_AMBIENT) > 0)\n\t\t\t{\n\t\t\t\taiString path;\n\t\t\t\tmaterial->GetTexture(aiTextureType_AMBIENT, 0, &path);\n\n\t\t\t\tif (path.data[0] == '*')\n\t\t\t\t{\n\t\t\t\t\tuint32_t index = atoi(path.C_Str() + 1);\n\t\t\t\t\tmaterial_data->m_ambient_occlusion_embedded_texture = index;\n\t\t\t\t\tmaterial_data->m_ambient_occlusion_texture_location = TextureLocation::EMBEDDED;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmaterial_data->m_ambient_occlusion_texture = std::string(path.C_Str());\n\t\t\t\t\tmaterial_data->m_ambient_occlusion_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmaterial_data->m_ambient_occlusion_texture_location = TextureLocation::NON_EXISTENT;\n\t\t\t}\n\n\t\t\tif (material->GetTextureCount(aiTextureType_NORMALS) > 0)\n\t\t\t{\n\t\t\t\taiString path;\n\t\t\t\tmaterial->GetTexture(aiTextureType_NORMALS, 0, &path);\n\n\t\t\t\tif (path.data[0] == '*')\n\t\t\t\t{\n\t\t\t\t\tuint32_t index = atoi(path.C_Str() + 1);\n\t\t\t\t\tmaterial_data->m_normal_map_embedded_texture = index;\n\t\t\t\t\tmaterial_data->m_normal_map_texture_location = TextureLocation::EMBEDDED;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmaterial_data->m_normal_map_texture = std::string(path.C_Str());\n\t\t\t\t\tmaterial_data->m_normal_map_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmaterial_data->m_normal_map_texture_location = TextureLocation::NON_EXISTENT;\n\t\t\t}\n\n\t\t\tif (material->GetTextureCount(aiTextureType_EMISSIVE) > 0)\n\t\t\t{\n\t\t\t\taiString path;\n\t\t\t\tmaterial->GetTexture(aiTextureType_EMISSIVE, 0, &path);\n\n\t\t\t\tif (path.data[0] == '*')\n\t\t\t\t{\n\t\t\t\t\tuint32_t index = atoi(path.C_Str() + 1);\n\t\t\t\t\tmaterial_data->m_emissive_embedded_texture = index;\n\t\t\t\t\tmaterial_data->m_emissive_texture_location = TextureLocation::EMBEDDED;\n\t\t\t\t\tmaterial_data->m_base_emissive = 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmaterial_data->m_emissive_texture = std::string(path.C_Str());\n\t\t\t\t\tmaterial_data->m_emissive_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\t\tmaterial_data->m_base_emissive = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tmaterial_data->m_emissive_texture_location = TextureLocation::NON_EXISTENT;\n\t\t\t\tmaterial_data->m_base_emissive = 0;\n\t\t\t}\n\n\t\t\taiColor3D color;\n\t\t\tmaterial->Get(AI_MATKEY_COLOR_DIFFUSE, color);\n\n\t\t\tmemcpy(&material_data->m_base_color, &color, sizeof(color));\n\n\t\t\tmaterial->Get(AI_MATKEY_COLOR_SPECULAR, color);\n\n\t\t\tmemcpy(&material_data->m_base_metallic, &color, sizeof(color));\n\n\t\t\tfloat roughness;\n\t\t\tmaterial->Get(AI_MATKEY_SHININESS, roughness);\n\t\t\tmaterial_data->m_base_roughness = roughness;\n\n\t\t\tfloat opacity;\n\t\t\tmaterial->Get(AI_MATKEY_OPACITY, opacity);\n\t\t\tmaterial_data->m_base_transparency = opacity;\n\n\t\t\tint two_sided;\n\t\t\tmaterial->Get(AI_MATKEY_TWOSIDED, two_sided);\n\t\t\tmaterial_data->m_two_sided = two_sided > 0;\n\n\t\t\tmodel->m_materials[i] = material_data;\n\t\t}\n\t}\n\n\tvoid AssimpModelLoader::LoadEmbeddedTextures(ModelData * model, const aiScene * scene)\n\t{\n\t\tmodel->m_embedded_textures.resize(scene->mNumTextures);\n\t\tfor (unsigned int i = 0; i < scene->mNumTextures; ++i) \n\t\t{\n\t\t\tEmbeddedTexture* texture = new EmbeddedTexture;\n\n\t\t\ttexture->m_compressed = scene->mTextures[i]->mHeight == 0 ? true : false;\n\t\t\ttexture->m_format = std::string(scene->mTextures[i]->achFormatHint);\n\n\t\t\ttexture->m_width = scene->mTextures[i]->mWidth;\n\t\t\ttexture->m_height = scene->mTextures[i]->mHeight;\n\n\t\t\tif (texture->m_compressed)\n\t\t\t{\n\t\t\t\ttexture->m_data.resize(texture->m_width);\n\t\t\t\tmemcpy(texture->m_data.data(), scene->mTextures[i]->pcData, texture->m_width);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\ttexture->m_data.resize(texture->m_width*texture->m_height * 4);\n\t\t\t\tfor (unsigned int j = 0; j < texture->m_width*texture->m_height; ++j)\n\t\t\t\t{\n\t\t\t\t\ttexture->m_data[j * 4 + 0] = scene->mTextures[i]->pcData[j].r;\n\t\t\t\t\ttexture->m_data[j * 4 + 1] = scene->mTextures[i]->pcData[j].g;\n\t\t\t\t\ttexture->m_data[j * 4 + 2] = scene->mTextures[i]->pcData[j].b;\n\t\t\t\t\ttexture->m_data[j * 4 + 3] = scene->mTextures[i]->pcData[j].a;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tmodel->m_embedded_textures[i] = texture;\n\t\t}\n\t}\n\n}"
  },
  {
    "path": "src/model_loader_assimp.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"model_loader.hpp\"\n\n#undef min\n#undef max\n#include <assimp/Importer.hpp>\n#include <assimp/scene.h>\n#include <assimp/postprocess.h>\n\nnamespace wr\n{\n\tclass AssimpModelLoader : public ModelLoader\n\t{\n\tpublic:\n\t\tAssimpModelLoader();\n\n\t\t~AssimpModelLoader() final;\n\n\tprotected:\n\t\tModelData* LoadModel(std::string_view model_path) final;\n\t\tModelData* LoadModel(void* data, std::size_t length, std::string format) final;\n\n\tprivate:\n\t\tvoid LoadMeshes(ModelData* model, const aiScene* scene, aiNode* node);\n\t\tvoid LoadMaterials(ModelData* model, const aiScene* scene);\n\t\tvoid LoadEmbeddedTextures(ModelData* model, const aiScene* scene);\n\t};\n}"
  },
  {
    "path": "src/model_loader_tinygltf.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"model_loader_tinygltf.hpp\"\n\n#include \"util/log.hpp\"\n\n#define TINYGLTF_IMPLEMENTATION\n#define STB_IMAGE_IMPLEMENTATION\n#define STB_IMAGE_WRITE_IMPLEMENTATION\n#include <tiny_gltf.h>\n\nnamespace wr\n{\n\n\tTinyGLTFModelLoader::TinyGLTFModelLoader()\n\t{\n\t\tm_supported_model_formats = { \"gltf\" };\n\t}\n\n\tTinyGLTFModelLoader::~TinyGLTFModelLoader()\n\t{\n\t}\n\n\tstd::pair<std::vector<DirectX::XMFLOAT3>, std::vector<DirectX::XMFLOAT3>> ComputeTangents(ModelMeshData* mesh_data)\n\t{\n\t\tsize_t vtxCount = mesh_data->m_positions.size();\n\t\tstd::vector<DirectX::XMFLOAT3> tanA(vtxCount, { 0, 0, 0 });\n\t\tstd::vector<DirectX::XMFLOAT3> tanB(vtxCount, { 0, 0, 0 });\n\n\t\tif (mesh_data->m_uvw.empty())\n\t\t{\n\t\t\treturn { tanA, tanB };\n\t\t}\n\n\t\t// (1)\n\t\tsize_t indexCount = mesh_data->m_indices.size();\n\t\tfor (size_t i = 0; i < mesh_data->m_indices.size(); i += 3) {\n\t\t\tsize_t i0 = mesh_data->m_indices[i];\n\t\t\tsize_t i1 = mesh_data->m_indices[i + 1];\n\t\t\tsize_t i2 = mesh_data->m_indices[i + 2];\n\n\t\t\tauto pos0 = DirectX::XMLoadFloat3(&mesh_data->m_positions[i0]);\n\t\t\tauto pos1 = DirectX::XMLoadFloat3(&mesh_data->m_positions[i1]);\n\t\t\tauto pos2 = DirectX::XMLoadFloat3(&mesh_data->m_positions[i2]);\n\n\t\t\tauto tex0 = DirectX::XMLoadFloat2(&DirectX::XMFLOAT2{ mesh_data->m_uvw[i0].x, mesh_data->m_uvw[i0].y });\n\t\t\tauto tex1 = DirectX::XMLoadFloat2(&DirectX::XMFLOAT2{ mesh_data->m_uvw[i1].x, mesh_data->m_uvw[i1].y });\n\t\t\tauto tex2 = DirectX::XMLoadFloat2(&DirectX::XMFLOAT2{ mesh_data->m_uvw[i2].x, mesh_data->m_uvw[i2].y });\n\n\t\t\tDirectX::XMFLOAT3 edge1, edge2;\n\t\t\tDirectX::XMStoreFloat3(&edge1, DirectX::XMVectorSubtract(pos1, pos0));\n\t\t\tDirectX::XMStoreFloat3(&edge2, DirectX::XMVectorSubtract(pos2, pos0));\n\n\t\t\tDirectX::XMFLOAT2 uv1, uv2;\n\t\t\tDirectX::XMStoreFloat2(&uv1, DirectX::XMVectorSubtract(tex1, tex0));\n\t\t\tDirectX::XMStoreFloat2(&uv2, DirectX::XMVectorSubtract(tex2, tex0));\n\n\t\t\tfloat r = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x);\n\n\t\t\tDirectX::XMFLOAT3 tangent(\n\t\t\t\t((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r,\n\t\t\t\t((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r,\n\t\t\t\t((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r\n\t\t\t);\n\n\t\t\tDirectX::XMFLOAT3 bitangent(\n\t\t\t\t((edge1.x * uv2.x) - (edge2.x * uv1.x)) * r,\n\t\t\t\t((edge1.y * uv2.x) - (edge2.y * uv1.x)) * r,\n\t\t\t\t((edge1.z * uv2.x) - (edge2.z * uv1.x)) * r\n\t\t\t);\n\n\t\t\ttanA[i0] = tangent;\n\t\t\ttanA[i1] = tangent;\n\t\t\ttanA[i2] = tangent;\n\n\t\t\ttanB[i0] = bitangent;\n\t\t\ttanB[i1] = bitangent;\n\t\t\ttanB[i2] = bitangent;\n\n\t\t}\n\n\t\treturn { tanA, tanB };\n\t}\n\n\tinline void LoadMaterial(ModelData* model, tinygltf::Model tg_model, tinygltf::Material mat)\n\t{\n\t\tModelMaterialData* mat_data = new ModelMaterialData();\n\t\t\t\t\n\t\tmat_data->m_albedo_texture_location = TextureLocation::NON_EXISTENT;\n\t\tmat_data->m_normal_map_texture_location = TextureLocation::NON_EXISTENT;\n\t\tmat_data->m_roughness_texture_location = TextureLocation::NON_EXISTENT;\n\t\tmat_data->m_metallic_texture_location = TextureLocation::NON_EXISTENT;\n\t\tmat_data->m_emissive_texture_location = TextureLocation::NON_EXISTENT;\n\t\tmat_data->m_ambient_occlusion_texture_location = TextureLocation::NON_EXISTENT;\n\n\t\tfor (auto value : mat.values)\n\t\t{\n\t\t\tif (value.first == \"baseColorTexture\")\n\t\t\t{\n\t\t\t\tauto img = tg_model.images[value.second.json_double_value.begin()->second];\t\t\t\t\t\t\n\n\t\t\t\tmat_data->m_albedo_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\tmat_data->m_albedo_texture = img.uri;\n\t\t\t}\n\t\t\telse if (value.first == \"metallicRoughnessTexture\")\n\t\t\t{\n\t\t\t\tauto img = tg_model.images[value.second.json_double_value.begin()->second];\n\n\t\t\t\tmat_data->m_metallic_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\tmat_data->m_metallic_texture = img.uri;\n\t\t\t\tmat_data->m_roughness_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\tmat_data->m_roughness_texture = img.uri;\n\t\t\t}\n\t\t\telse if (value.first == \"roughnessFactor\")\n\t\t\t{\n\t\t\t\tauto factor = value.second.Factor();\n\t\t\t\tmat_data->m_base_roughness = factor;\n\t\t\t}\n\t\t\telse if (value.first == \"metallicFactor\")\n\t\t\t{\n\t\t\t\tauto factor = value.second.Factor();\n\t\t\t\tmat_data->m_base_metallic = factor;\n\t\t\t}\n\t\t}\n\n\t\tfor (auto value : mat.additionalValues)\n\t\t{\n\t\t\tif (value.first == \"normalTexture\")\n\t\t\t{\n\t\t\t\tauto img = tg_model.images[value.second.json_double_value.begin()->second];\n\n\t\t\t\tmat_data->m_normal_map_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\tmat_data->m_normal_map_texture = img.uri;\n\t\t\t}\n\t\t\telse if (value.first == \"occlusionTexture\")\n\t\t\t{\n\t\t\t\tauto img = tg_model.images[value.second.json_double_value.begin()->second];\n\n\t\t\t\tmat_data->m_ambient_occlusion_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\tmat_data->m_ambient_occlusion_texture = img.uri;\n\t\t\t}\n\t\t\telse if (value.first == \"emissiveTexture\")\n\t\t\t{\n\t\t\t\tauto img = tg_model.images[value.second.json_double_value.begin()->second];\n\n\t\t\t\tmat_data->m_emissive_texture_location = TextureLocation::EXTERNAL;\n\t\t\t\tmat_data->m_emissive_texture = img.uri;\n\t\t\t}\n\t\t\telse if (value.first == \"emissiveFactor\")\n\t\t\t{\n\t\t\t\tauto factor = value.second.ColorFactor();\n\t\t\t\tmat_data->m_base_emissive = factor[0] + factor[1] + factor[2];\n\t\t\t}\n\t\t}\n\n\t\tmodel->m_materials.push_back(mat_data);\n\t}\n\n\tinline void LoadMesh(ModelData* model, tinygltf::Model tg_model, tinygltf::Node node, DirectX::XMMATRIX parent_transform)\n\t{\n\t\tauto mesh = tg_model.meshes[node.mesh];\n\n\t\tauto translation = node.translation;\n\t\tauto rotation = node.rotation;\n\t\tauto scale = node.scale;\n\t\tauto matrix = node.matrix;\n\n\t\tfor (auto primitive : mesh.primitives)\n\t\t{\n\t\t\tstd::size_t idx_buffer_offset = 0;\n\t\t\tstd::size_t uv_buffer_offset = 0;\n\t\t\tstd::size_t position_buffer_offset = 0;\n\t\t\tstd::size_t normal_buffer_offset = 0;\n\t\t\tModelMeshData* mesh_data = new ModelMeshData();\n\n\t\t\t{ // GET INDICES\n\t\t\t\tauto idx_accessor = tg_model.accessors[primitive.indices];\n\n\t\t\t\tconst auto& buffer_view = tg_model.bufferViews[idx_accessor.bufferView];\n\t\t\t\tconst auto& buffer = tg_model.buffers[buffer_view.buffer];\n\t\t\t\tconst auto data_address = buffer.data.data() + buffer_view.byteOffset + idx_accessor.byteOffset;\n\t\t\t\tconst auto byte_stride = idx_accessor.ByteStride(buffer_view);\n\n\t\t\t\tmesh_data->m_indices.resize(idx_buffer_offset + idx_accessor.count);\n\n\t\t\t\tmemcpy(mesh_data->m_indices.data() + idx_buffer_offset, data_address, idx_accessor.count * byte_stride);\n\t\t\t\tidx_buffer_offset += idx_accessor.count * byte_stride;\n\t\t\t}\n\n\t\t\t// Get other attributes\n\t\t\tfor (const auto& attrib : primitive.attributes)\n\t\t\t{\n\t\t\t\tconst auto attrib_accessor = tg_model.accessors[attrib.second];\n\t\t\t\tconst auto& buffer_view = tg_model.bufferViews[attrib_accessor.bufferView];\n\t\t\t\tconst auto& buffer = tg_model.buffers[buffer_view.buffer];\n\t\t\t\tconst auto data_address = buffer.data.data() + buffer_view.byteOffset + attrib_accessor.byteOffset;\n\t\t\t\tconst auto byte_stride = attrib_accessor.ByteStride(buffer_view);\n\t\t\t\tconst auto count = attrib_accessor.count;\n\t\t\t\tif (attrib.first == \"POSITION\")\n\t\t\t\t{\n\t\t\t\t\tmesh_data->m_positions.resize(position_buffer_offset + count);\n\t\t\t\t\tmesh_data->m_colors.resize(position_buffer_offset + count);\n\n\t\t\t\t\tmemcpy(mesh_data->m_positions.data() + position_buffer_offset, data_address, count * byte_stride);\n\t\t\t\t\tposition_buffer_offset += count * byte_stride;\n\t\t\t\t}\n\t\t\t\telse if (attrib.first == \"NORMAL\")\n\t\t\t\t{\n\t\t\t\t\tmesh_data->m_normals.resize(normal_buffer_offset + count);\n\n\t\t\t\t\tmemcpy(mesh_data->m_normals.data() + normal_buffer_offset, data_address, count * byte_stride);\n\t\t\t\t\tnormal_buffer_offset += count * byte_stride;\n\t\t\t\t}\n\t\t\t\telse if (attrib.first == \"TEXCOORD_0\")\n\t\t\t\t{\n\t\t\t\t\tstd::vector<DirectX::XMFLOAT2> f2_data;\n\t\t\t\t\tf2_data.resize(count);\n\n\t\t\t\t\tmemcpy(f2_data.data(), data_address, count * byte_stride);\n\n\t\t\t\t\tfor (auto& f2 : f2_data)\n\t\t\t\t\t{\n\t\t\t\t\t\tmesh_data->m_uvw.push_back({ f2.x, f2.y*-1, 0 });\n\t\t\t\t\t}\n\n\t\t\t\t\tuv_buffer_offset += count * sizeof(DirectX::XMFLOAT3);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Apply Transformation\n\t\t\tfor (auto& position : mesh_data->m_positions)\n\t\t\t{\n\t\t\t\tauto transformed_pos = DirectX::XMVector3Transform(DirectX::XMLoadFloat3(&position), parent_transform);\n\t\t\t\tDirectX::XMStoreFloat3(&position, transformed_pos);\n\t\t\t}\n\n\t\t\tauto tangent_bitangent = ComputeTangents(mesh_data);\n\t\t\tmesh_data->m_tangents = tangent_bitangent.first;\n\t\t\tmesh_data->m_bitangents = tangent_bitangent.second;\n\t\t\tmesh_data->m_uvw.resize(mesh_data->m_positions.size());\n\t\t\tmesh_data->m_material_id = primitive.material;\n\n\t\t\tmodel->m_meshes.push_back(mesh_data);\n\t\t}\n\t}\n\n\tModelData* TinyGLTFModelLoader::LoadModel(std::string_view model_path)\n\t{\n\t\ttinygltf::Model tg_model;\n\t\ttinygltf::TinyGLTF loader;\n\t\tstd::string err;\n\t\tstd::string warn;\n\t\tstd::string path = std::string(model_path);\n\n\t\tif (!loader.LoadASCIIFromFile(&tg_model, &err, &warn, path))\n\t\t{\n\t\t\tLOGC(\"TinyGLTF Parsing Failed\");\n\t\t}\n\n\t\tif (!warn.empty())\n\t\t{\n\t\t\tLOGW(\"TinyGLTF Warning: {}\", warn);\n\t\t}\n\n\t\tif (!err.empty())\n\t\t{\n\t\t\tLOGE(\"TinyGLTF Error: {}\", err);\n\t\t}\n\n\t\tauto model = new ModelData();\n\t\tmodel->m_skeleton_data = new wr::ModelSkeletonData();\n\n\t\tfor (auto mat : tg_model.materials)\n\t\t{\n\t\t\tLoadMaterial(model, tg_model, mat);\n\t\t}\n\n\t\tstd::function<void(int, DirectX::XMMATRIX)> recursive_func = [&](int node_id, DirectX::XMMATRIX parent_transform)\n\t\t{\n\t\t\tauto node = tg_model.nodes[node_id];\n\n\t\t\tauto translation = node.translation;\n\t\t\tauto scale = node.scale;\n\t\t\tauto rotation = node.rotation;\n\t\t\tauto matrix = node.matrix;\n\n\t\t\tDirectX::XMMATRIX transform;\n\n\t\t\tif (matrix.empty())\n\t\t\t{\n\t\t\t\tDirectX::XMMATRIX translation_mat = translation.empty() ? DirectX::XMMatrixIdentity() : DirectX::XMMatrixTranslationFromVector({ (float)translation[0], (float)translation[1], (float)translation[2] });\n\t\t\t\tDirectX::XMMATRIX rotation_mat = rotation.empty() ? DirectX::XMMatrixIdentity() : DirectX::XMMatrixRotationQuaternion({ (float)rotation[0], (float)rotation[1], (float)rotation[2], (float)rotation[3] });\n\t\t\t\tDirectX::XMMATRIX scale_mat = scale.empty() ? DirectX::XMMatrixIdentity() : DirectX::XMMatrixScalingFromVector({ (float)scale[0], (float)scale[1], (float)scale[2] });\n\t\t\t\ttransform = scale_mat * rotation_mat * translation_mat;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\ttransform.r[0].m128_f32[0] = matrix[0];\n\t\t\t\ttransform.r[0].m128_f32[1] = matrix[1];\n\t\t\t\ttransform.r[0].m128_f32[2] = matrix[2];\n\t\t\t\ttransform.r[0].m128_f32[3] = matrix[3];\n\n\t\t\t\ttransform.r[1].m128_f32[0] = matrix[4];\n\t\t\t\ttransform.r[1].m128_f32[1] = matrix[5];\n\t\t\t\ttransform.r[1].m128_f32[2] = matrix[6];\n\t\t\t\ttransform.r[1].m128_f32[3] = matrix[7];\n\n\t\t\t\ttransform.r[2].m128_f32[0] = matrix[8];\n\t\t\t\ttransform.r[2].m128_f32[1] = matrix[9];\n\t\t\t\ttransform.r[2].m128_f32[2] = matrix[10];\n\t\t\t\ttransform.r[2].m128_f32[3] = matrix[11];\n\n\t\t\t\ttransform.r[3].m128_f32[0] = matrix[12];\n\t\t\t\ttransform.r[3].m128_f32[1] = matrix[13];\n\t\t\t\ttransform.r[3].m128_f32[2] = matrix[14];\n\t\t\t\ttransform.r[3].m128_f32[3] = matrix[15];\n\n\t\t\t}\n\n\t\t\tparent_transform = parent_transform * transform;\n\n\t\t\tif (node.mesh > -1)\n\t\t\t{\n\t\t\t\tLoadMesh(model, tg_model, node, parent_transform);\n\t\t\t}\n\n\t\t\tfor (auto child_id : node.children)\n\t\t\t{\n\t\t\t\trecursive_func(child_id, parent_transform);\n\t\t\t}\n\t\t};\n\n\t\tDirectX::XMMATRIX parent_transform = DirectX::XMMatrixIdentity();\n\t\tfor (auto node_id : tg_model.scenes[tg_model.defaultScene].nodes)\n\t\t{\n\t\t\trecursive_func(node_id, parent_transform);\n\t\t}\n\n\t\treturn model;\n\t}\n\n\tModelData* TinyGLTFModelLoader::LoadModel(void* data, std::size_t length, std::string format)\n\t{\n\t\treturn new ModelData();\n\t}\n\n}"
  },
  {
    "path": "src/model_loader_tinygltf.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"model_loader.hpp\"\n\nnamespace wr\n{\n\tclass TinyGLTFModelLoader : public ModelLoader\n\t{\n\tpublic:\n\t\tTinyGLTFModelLoader();\n\n\t\t~TinyGLTFModelLoader() final;\n\n\tprotected:\n\t\tModelData* LoadModel(std::string_view model_path) final;\n\t\tModelData* LoadModel(void* data, std::size_t length, std::string format) final;\n\n\tprivate:\n\t\t//void LoadMeshes(ModelData* model, tinygltf::Model tg_model, tinygltf::Node node);\n\t\t//void LoadMaterials(ModelData* model, const aiScene* scene);\n\t\t//void LoadEmbeddedTextures(ModelData* model, const aiScene* scene);\n\t};\n}"
  },
  {
    "path": "src/model_pool.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"model_pool.hpp\"\n#include <utility>\n\nnamespace wr\n{\n\n\tvoid Model::Expand(float(&pos)[3])\n\t{\n\t\tm_box.Expand(pos);\n\t}\n\n\tModelPool::ModelPool(std::size_t vertex_buffer_pool_size_in_bytes,\n\t\tstd::size_t index_buffer_pool_size_in_bytes) : \n\t\tm_vertex_buffer_pool_size_in_bytes(vertex_buffer_pool_size_in_bytes),\n\t\tm_index_buffer_pool_size_in_bytes(index_buffer_pool_size_in_bytes),\n\t\tm_current_id(0)\n\t{\n\t}\n\n\tvoid ModelPool::Destroy(Model * model)\n\t{\n\t\tDestroyModel(model);\n\t}\n\n\tvoid ModelPool::Destroy(internal::MeshInternal * mesh)\n\t{\n\t\tDestroyMesh(mesh);\n\t}\n\n\ttemplate<>\n\tint ModelPool::LoadNodeMeshes<Vertex, std::uint32_t>(ModelData* data, Model* model, MaterialHandle default_material)\n\t{\n\t\tmodel->m_meshes.reserve(data->m_meshes.size());\n\n\t\tfor (unsigned int i = 0; i < data->m_meshes.size(); ++i)\n\t\t{\n\t\t\tModelMeshData* mesh = data->m_meshes[i];\n\n\t\t\tstd::vector<Vertex> vertices(mesh->m_positions.size());\n\t\t\tstd::vector<std::uint32_t> indices(mesh->m_indices.size());\n\n\t\t\tMesh* mesh_handle = new Mesh();\n\n\t\t\tfor (unsigned int j = 0; j < mesh->m_positions.size(); ++j)\n\t\t\t{\n\t\t\t\tVertex &vertex = vertices[j];\n\n\t\t\t\tmemcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos));\n\t\t\t\tmemcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal));\n\t\t\t\tmemcpy(vertex.m_tangent, &mesh->m_tangents[j], sizeof(vertex.m_tangent));\n\t\t\t\tmemcpy(vertex.m_bitangent, &mesh->m_bitangents[j], sizeof(vertex.m_bitangent));\n\t\t\t\tmemcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv));\n\n\t\t\t\tmodel->Expand(vertex.m_pos);\n\n\t\t\t}\n\n\t\t\tmemcpy(indices.data(), mesh->m_indices.data(), mesh->m_indices.size() * 4);\n\n\t\t\tinternal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices(\n\t\t\t\tvertices.data(),\n\t\t\t\tvertices.size(),\n\t\t\t\tsizeof(Vertex),\n\t\t\t\tindices.data(),\n\t\t\t\tindices.size(),\n\t\t\t\tsizeof(std::uint32_t));\n\n\t\t\tif (mesh_data == nullptr)\n\t\t\t{\n\t\t\t\tfor (auto &elem : model->m_meshes)\n\t\t\t\t\tdelete elem.first;\n\n\t\t\t\tdelete model;\n\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\tstd::uint64_t id = GetNewID();\n\t\t\tm_loaded_meshes[id] = mesh_data;\n\t\t\tmesh_handle->id = id;\n\n\t\t\tMaterialHandle material_handle = default_material;\n\n\t\t\tstd::pair<Mesh*, MaterialHandle> n_mesh = std::make_pair(\n\t\t\t\tmesh_handle,\n\t\t\t\tmaterial_handle);\n\n\n\t\t\tmodel->m_meshes.push_back(n_mesh);\n\t\t}\n\n\t\treturn 0;\n\t}\n\t\n\ttemplate<>\n\tint ModelPool::LoadNodeMeshes<VertexColor, std::uint32_t>(ModelData* data, Model* model, MaterialHandle default_material)\n\t{\n\t\tmodel->m_meshes.reserve(data->m_meshes.size());\n\n\t\tfor (unsigned int i = 0; i < data->m_meshes.size(); ++i)\n\t\t{\n\t\t\tModelMeshData* mesh = data->m_meshes[i];\n\n\t\t\tstd::vector<VertexColor> vertices(mesh->m_positions.size());\n\t\t\tstd::vector<std::uint32_t> indices(mesh->m_indices.size());\n\n\t\t\tMesh* mesh_handle = new Mesh();\n\n\t\t\tfor (unsigned int j = 0; j < mesh->m_positions.size(); ++j)\n\t\t\t{\n\t\t\t\tVertexColor &vertex = vertices[j];\n\n\t\t\t\tmemcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos));\n\t\t\t\tmemcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal));\n\t\t\t\tmemcpy(vertex.m_tangent, &mesh->m_tangents[j], sizeof(vertex.m_tangent));\n\t\t\t\tmemcpy(vertex.m_bitangent, &mesh->m_bitangents[j], sizeof(vertex.m_bitangent));\n\t\t\t\tmemcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv));\n\t\t\t\tmemcpy(vertex.m_color, &mesh->m_colors[j], sizeof(vertex.m_color));\n\n\t\t\t\tmodel->Expand(vertex.m_pos);\n\n\t\t\t}\n\n\t\t\tmemcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size());\n\n\t\t\tinternal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices(\n\t\t\t\tvertices.data(),\n\t\t\t\tvertices.size(),\n\t\t\t\tsizeof(VertexColor),\n\t\t\t\tindices.data(),\n\t\t\t\tindices.size(),\n\t\t\t\tsizeof(std::uint32_t));\n\n\t\t\tif (mesh_data == nullptr)\n\t\t\t{\n\t\t\t\tfor (auto &elem : model->m_meshes)\n\t\t\t\t\tdelete elem.first;\n\n\t\t\t\tdelete model;\n\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\tstd::uint64_t id = GetNewID();\n\t\t\tm_loaded_meshes[id] = mesh_data;\n\t\t\tmesh_handle->id = id;\n\n\t\t\tMaterialHandle material_handle = default_material;\n\n\t\t\tstd::pair<Mesh*, MaterialHandle> n_mesh = std::make_pair(\n\t\t\t\tmesh_handle,\n\t\t\t\tmaterial_handle);\n\n\n\t\t\tmodel->m_meshes.push_back(n_mesh);\n\t\t}\n\n\t\treturn 0;\n\t}\n\n\ttemplate<>\n\tint ModelPool::LoadNodeMeshes<VertexNoTangent, std::uint32_t>(ModelData* data, Model* model, MaterialHandle default_material)\n\t{\n\t\tmodel->m_meshes.reserve(data->m_meshes.size());\n\n\t\tfor (unsigned int i = 0; i < data->m_meshes.size(); ++i)\n\t\t{\n\t\t\tModelMeshData* mesh = data->m_meshes[i];\n\n\t\t\tstd::vector<VertexNoTangent> vertices(mesh->m_positions.size());\n\t\t\tstd::vector<std::uint32_t> indices(mesh->m_indices.size());\n\n\t\t\tMesh* mesh_handle = new Mesh();\n\n\t\t\tfor (unsigned int j = 0; j < mesh->m_positions.size(); ++j)\n\t\t\t{\n\t\t\t\tVertexNoTangent &vertex = vertices[j];\n\n\t\t\t\tmemcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos));\n\t\t\t\tmemcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal));\n\t\t\t\tmemcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv));\n\n\t\t\t\tmodel->Expand(vertex.m_pos);\n\n\t\t\t}\n\n\t\t\tmemcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size());\n\n\t\t\tinternal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices(\n\t\t\t\tvertices.data(),\n\t\t\t\tvertices.size(),\n\t\t\t\tsizeof(VertexNoTangent),\n\t\t\t\tindices.data(),\n\t\t\t\tindices.size(),\n\t\t\t\tsizeof(std::uint32_t));\n\n\t\t\tif (mesh_data == nullptr)\n\t\t\t{\n\t\t\t\tfor (auto &elem : model->m_meshes)\n\t\t\t\t\tdelete elem.first;\n\n\t\t\t\tdelete model;\n\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\tstd::uint64_t id = GetNewID();\n\t\t\tm_loaded_meshes[id] = mesh_data;\n\t\t\tmesh_handle->id = id;\n\n\t\t\tMaterialHandle material_handle = default_material;\n\n\t\t\tstd::pair<Mesh*, MaterialHandle> n_mesh = std::make_pair(\n\t\t\t\tmesh_handle,\n\t\t\t\tmaterial_handle);\n\n\t\t\tmodel->m_meshes.push_back(n_mesh);\n\t\t}\n\n\t\treturn 0;\n\t}\n\n\ttemplate<>\n\tint ModelPool::LoadNodeMeshesWithMaterials<Vertex, std::uint32_t>(ModelData* data, Model * model, std::vector<MaterialHandle> materials)\n\t{\n\t\tmodel->m_meshes.reserve(data->m_meshes.size());\n\n\t\tfor (unsigned int i = 0; i < data->m_meshes.size(); ++i)\n\t\t{\n\t\t\tModelMeshData* mesh = data->m_meshes[i];\n\n\t\t\tstd::vector<Vertex> vertices(mesh->m_positions.size());\n\t\t\tstd::vector<std::uint32_t> indices(mesh->m_indices.size());\n\n\t\t\tMesh* mesh_handle = new Mesh();\n\n\t\t\tfor (unsigned int j = 0; j < mesh->m_positions.size(); ++j)\n\t\t\t{\n\t\t\t\tVertex &vertex = vertices[j];\n\n\t\t\t\tmemcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos));\n\t\t\t\tmemcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal));\n\t\t\t\tmemcpy(vertex.m_tangent, &mesh->m_tangents[j], sizeof(vertex.m_tangent));\n\t\t\t\tmemcpy(vertex.m_bitangent, &mesh->m_bitangents[j], sizeof(vertex.m_bitangent));\n\t\t\t\tmemcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv));\n\n\t\t\t\tmodel->Expand(vertex.m_pos);\n\n\t\t\t}\n\n\t\t\tmemcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size());\n\n\t\t\tinternal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices(\n\t\t\t\tvertices.data(),\n\t\t\t\tvertices.size(),\n\t\t\t\tsizeof(Vertex),\n\t\t\t\tindices.data(),\n\t\t\t\tindices.size(),\n\t\t\t\tsizeof(std::uint32_t));\n\n\t\t\tif (mesh_data == nullptr)\n\t\t\t{\n\t\t\t\tfor (auto &elem : model->m_meshes)\n\t\t\t\t\tdelete elem.first;\n\n\t\t\t\tdelete model;\n\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\tstd::uint64_t id = GetNewID();\n\t\t\tm_loaded_meshes[id] = mesh_data;\n\t\t\tmesh_handle->id = id;\n\n\t\t\tMaterialHandle material_handle = materials[mesh->m_material_id];\n\n\t\t\tstd::pair<Mesh*, MaterialHandle> n_mesh = std::make_pair(\n\t\t\t\tmesh_handle,\n\t\t\t\tmaterial_handle);\n\t\t\t\n\t\t\tmodel->m_meshes.push_back(n_mesh);\n\t\t}\n\n\t\treturn 0;\n\t}\n\n\ttemplate<>\n\tvoid ModelPool::UpdateModelBoundingBoxes<Vertex>(Model * model, std::vector<Vertex> vertices_data)\n\t{\n\t\tfor (int i = 0; i < vertices_data.size(); ++i)\n\t\t{\n\t\t\tmodel->Expand(vertices_data[i].m_pos);\n\t\t}\n\t}\n\n\ttemplate<>\n\tvoid ModelPool::UpdateModelBoundingBoxes<VertexNoTangent>(Model * model, std::vector<VertexNoTangent> vertices_data)\n\t{\n\t\tfor (int i = 0; i < vertices_data.size(); ++i)\n\t\t{\n\t\t\tmodel->Expand(vertices_data[i].m_pos);\n\t\t}\n\t}\n\n\ttemplate<>\n\tvoid ModelPool::UpdateModelBoundingBoxes<VertexColor>(Model * model, std::vector<VertexColor> vertices_data)\n\t{\n\t\tfor (int i = 0; i < vertices_data.size(); ++i)\n\t\t{\n\t\t\tmodel->Expand(vertices_data[i].m_pos);\n\t\t}\n\t}\n\t\n\ttemplate<>\n\tint ModelPool::LoadNodeMeshesWithMaterials<VertexColor, std::uint32_t>(ModelData* data, Model * model, std::vector<MaterialHandle> materials)\n\t{\n\t\tmodel->m_meshes.reserve(data->m_meshes.size());\n\n\t\tfor (unsigned int i = 0; i < data->m_meshes.size(); ++i)\n\t\t{\n\t\t\tModelMeshData* mesh = data->m_meshes[i];\n\n\t\t\tstd::vector<VertexColor> vertices(mesh->m_positions.size());\n\t\t\tstd::vector<std::uint32_t> indices(mesh->m_indices.size());\n\n\t\t\tMesh* mesh_handle = new Mesh();\n\n\t\t\tfor (unsigned int j = 0; j < mesh->m_positions.size(); ++j)\n\t\t\t{\n\t\t\t\tVertexColor &vertex = vertices[j];\n\n\t\t\t\tmemcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos));\n\t\t\t\tmemcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal));\n\t\t\t\tmemcpy(vertex.m_tangent, &mesh->m_tangents[j], sizeof(vertex.m_tangent));\n\t\t\t\tmemcpy(vertex.m_bitangent, &mesh->m_bitangents[j], sizeof(vertex.m_bitangent));\n\t\t\t\tmemcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv));\n\t\t\t\tmemcpy(vertex.m_color, &mesh->m_colors[j], sizeof(vertex.m_color));\n\n\t\t\t\tmodel->Expand(vertex.m_pos);\n\n\t\t\t}\n\n\t\t\tmemcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size());\n\n\t\t\tinternal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices(\n\t\t\t\tvertices.data(),\n\t\t\t\tvertices.size(),\n\t\t\t\tsizeof(VertexColor),\n\t\t\t\tindices.data(),\n\t\t\t\tindices.size(),\n\t\t\t\tsizeof(std::uint32_t));\n\n\t\t\tif (mesh_data == nullptr)\n\t\t\t{\n\t\t\t\tfor (auto &elem : model->m_meshes)\n\t\t\t\t\tdelete elem.first;\n\n\t\t\t\tdelete model;\n\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\tstd::uint64_t id = GetNewID();\n\t\t\tm_loaded_meshes[id] = mesh_data;\n\t\t\tmesh_handle->id = id;\n\n\t\t\tMaterialHandle material_handle = materials[mesh->m_material_id];\n\n\t\t\tstd::pair<Mesh*, MaterialHandle> n_mesh = std::make_pair(\n\t\t\t\tmesh_handle,\n\t\t\t\tmaterial_handle);\n\t\t\t\n\t\t\tmodel->m_meshes.push_back(n_mesh);\n\t\t}\n\n\t\treturn 0;\n\t}\n\n\ttemplate<>\n\tint ModelPool::LoadNodeMeshesWithMaterials<VertexNoTangent, std::uint32_t>(ModelData* data, Model * model, std::vector<MaterialHandle> materials)\n\t{\n\t\tmodel->m_meshes.reserve(data->m_meshes.size());\n\n\t\tfor (unsigned int i = 0; i < data->m_meshes.size(); ++i)\n\t\t{\n\t\t\tModelMeshData* mesh = data->m_meshes[i];\n\n\t\t\tstd::vector<VertexNoTangent> vertices(mesh->m_positions.size());\n\t\t\tstd::vector<std::uint32_t> indices(mesh->m_indices.size());\n\n\t\t\tMesh* mesh_handle = new Mesh();\n\n\t\t\tfor (unsigned int j = 0; j < mesh->m_positions.size(); ++j)\n\t\t\t{\n\t\t\t\tVertexNoTangent &vertex = vertices[j];\n\n\t\t\t\tmemcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos));\n\t\t\t\tmemcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal));\n\t\t\t\tmemcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv));\n\n\t\t\t\tmodel->Expand(vertex.m_pos);\n\n\t\t\t}\n\n\t\t\tmemcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size());\n\n\t\t\tinternal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices(\n\t\t\t\tvertices.data(),\n\t\t\t\tvertices.size(),\n\t\t\t\tsizeof(VertexNoTangent),\n\t\t\t\tindices.data(),\n\t\t\t\tindices.size(),\n\t\t\t\tsizeof(std::uint32_t));\n\n\t\t\tif (mesh_data == nullptr)\n\t\t\t{\n\t\t\t\tfor (auto &elem : model->m_meshes)\n\t\t\t\t\tdelete elem.first;\n\n\t\t\t\tdelete model;\n\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\tstd::uint64_t id = GetNewID();\n\t\t\tm_loaded_meshes[id] = mesh_data;\n\t\t\tmesh_handle->id = id;\n\n\t\t\tMaterialHandle material_handle = materials[mesh->m_material_id];\n\n\t\t\tstd::pair<Mesh*, MaterialHandle> n_mesh = std::make_pair(\n\t\t\t\tmesh_handle,\n\t\t\t\tmaterial_handle);\n\n\t\t\tmodel->m_meshes.push_back(n_mesh);\n\t\t}\n\n\t\treturn 0;\n\t}\n\n\tstd::uint64_t ModelPool::GetNewID()\n\t{\n\t\tstd::uint64_t id;\n\t\tif (m_freed_ids.size() > 0)\n\t\t{\n\t\t\tid = m_freed_ids.top();\n\t\t\tm_freed_ids.pop();\n\t\t}\n\t\telse\n\t\t{\n\t\t\tid = m_current_id;\n\t\t\tm_current_id++;\n\t\t}\n\t\treturn id;\n\t}\n\n\tvoid ModelPool::FreeID(std::uint64_t id)\n\t{\n\t\tm_freed_ids.push(id);\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/model_pool.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <string_view>\n#include <vector>\n#include <optional>\n#include <map>\n#include <stack>\n//#include <d3d12.h>\n#include <DirectXMath.h>\n\n#include \"util/defines.hpp\"\n#include \"material_pool.hpp\"\n#include \"resource_pool_texture.hpp\"\n\n#include \"model_loader.hpp\" \n#include \"model_loader_assimp.hpp\"\n\n#include \"util/log.hpp\"\n#include \"util/aabb.hpp\"\n#include \"vertex.hpp\"\n\nstruct aiScene;\nstruct aiNode;\n\nnamespace wr\n{\n\tclass ModelPool;\n\n\tnamespace internal \n\t{\n\t\tstruct MeshInternal\n\t\t{\n\n\t\t};\n\t}\n\n\tstruct Mesh\n\t{\n\t\tstd::uint64_t id;\n\t\t//Box m_box;\n\t};\n\n\ttemplate<typename TV, typename TI = std::uint32_t>\n\tstruct MeshData\n\t{\n\t\tstd::vector<TV> m_vertices;\n\t\tstd::optional<std::vector<TI>> m_indices;\n\t};\n\n\tstruct Model\n\t{\n\t\tstd::vector<std::pair<Mesh*, MaterialHandle>> m_meshes;\n\t\tModelPool* m_model_pool = nullptr;\n\t\tstd::string m_model_name;\n\t\tbool m_owns_materials = false;\t\t\t//Whether the model has to take care of the materials\n\n\t\tBox m_box;\n\n\t\tvoid Expand(float (&pos)[3]);\n\t};\n\n\tclass ModelPool\n\t{\n\tpublic:\n\t\texplicit ModelPool(std::size_t vertex_buffer_pool_size_in_bytes,\n\t\t\tstd::size_t index_buffer_pool_size_in_bytes);\n\t\tvirtual ~ModelPool() = default;\n\n\t\tModelPool(ModelPool const &) = delete;\n\t\tModelPool& operator=(ModelPool const &) = delete;\n\t\tModelPool(ModelPool&&) = delete;\n\t\tModelPool& operator=(ModelPool&&) = delete;\n\n\t\ttemplate<typename TV, typename TI = std::uint32_t>\n\t\t[[nodiscard]] Model* Load(MaterialPool* material_pool, TexturePool* texture_pool, std::string_view path, std::optional<ModelData**> out_model_data = std::nullopt);\n\t\ttemplate<typename TV, typename TI = std::uint32_t>\n\t\t[[nodiscard]] Model* LoadWithMaterials(MaterialPool* material_pool, TexturePool* texture_pool, std::string_view path, bool flip_normals = false, std::optional<ModelData**> out_model_data = std::nullopt);\n\t\ttemplate<typename TV, typename TI = std::uint32_t>\n\t\t[[nodiscard]] Model* LoadCustom(std::vector<MeshData<TV, TI>> meshes);\n\n\t\tvoid Destroy(Model* model);\n\t\tvoid Destroy(internal::MeshInternal* mesh);\n\n\t\t// Shrinks down both heaps to the minimum size required. \n\t\t// Does not rearrange the contents of the heaps, meaning that it doesn't shrink to the absolute minimum size.\n\t\t// To do that, call Defragment first.\n\t\tvirtual void ShrinkToFit() = 0;\n\t\tvirtual void ShrinkVertexHeapToFit() = 0;\n\t\tvirtual void ShrinkIndexHeapToFit() = 0;\n\n\t\t// Removes any holes in the memory, stitching all allocations back together to maximize the amount of contiguous free space.\n\t\t// These functions are called automatically if the allocator has enough free space but no large enough free blocks.\n\t\tvirtual void Defragment() = 0;\n\t\tvirtual void DefragmentVertexHeap() = 0;\n\t\tvirtual void DefragmentIndexHeap() = 0;\n\n\t\tvirtual size_t GetVertexHeapOccupiedSpace() = 0;\n\t\tvirtual size_t GetIndexHeapOccupiedSpace() = 0;\n\n\t\tvirtual size_t GetVertexHeapFreeSpace() = 0;\n\t\tvirtual size_t GetIndexHeapFreeSpace() = 0;\n\n\t\tvirtual size_t GetVertexHeapSize() = 0;\n\t\tvirtual size_t GetIndexHeapSize() = 0;\n\n\t\t// Resizes both heaps to the supplied sizes. \n\t\t// If the supplied size is smaller than the required size the heaps will resize to the required size instead.\n\t\tvirtual void Resize(size_t vertex_heap_new_size, size_t index_heap_new_size) = 0;\n\t\tvirtual void ResizeVertexHeap(size_t vertex_heap_new_size) = 0;\n\t\tvirtual void ResizeIndexHeap(size_t index_heap_new_size) = 0;\n\n\t\ttemplate<typename TV, typename TI> void EditMesh(Mesh* mesh, std::vector<TV> vertices, std::vector<TI> indices);\n\n\n\t\tvirtual void Evict() = 0;\n\t\tvirtual void MakeResident() = 0;\n\n\t\tvirtual void MakeSpaceForModel(size_t vertex_size, size_t index_size) = 0;\n\n\tprotected:\n\t\tvirtual internal::MeshInternal* LoadCustom_VerticesAndIndices(void* vertices_data, std::size_t num_vertices, std::size_t vertex_size, void* indices_data, std::size_t num_indices, std::size_t index_size) = 0;\n\t\tvirtual internal::MeshInternal* LoadCustom_VerticesOnly(void* vertices_data, std::size_t num_vertices, std::size_t vertex_size) = 0;\n\n\t\tvirtual void UpdateMeshData(Mesh* mesh, void* vertices_data, std::size_t num_vertices, std::size_t vertex_size, void* indices_data, std::size_t num_indices, std::size_t index_size) = 0;\n\n\t\tvirtual void DestroyModel(Model* model) = 0;\n\t\tvirtual void DestroyMesh(internal::MeshInternal* mesh) = 0;\n\n\t\ttemplate<typename TV, typename TI = std::uint32_t>\n\t\tint LoadNodeMeshes(ModelData* data, Model* model, MaterialHandle default_material);\n\t\ttemplate<typename TV, typename TI = std::uint32_t>\n\t\tint LoadNodeMeshesWithMaterials(ModelData* data, Model* model, std::vector<MaterialHandle> materials);\n\n\t\ttemplate<typename TV>\n\t\tvoid UpdateModelBoundingBoxes(Model* model, std::vector<TV> vertices_data);\n\n\t\tstd::size_t m_vertex_buffer_pool_size_in_bytes;\n\t\tstd::size_t m_index_buffer_pool_size_in_bytes;\n\n\t\tstd::map<std::uint64_t, internal::MeshInternal*> m_loaded_meshes;\n\t\tstd::stack<std::uint64_t> m_freed_ids;\n\n\t\tstd::uint64_t m_current_id;\n\n\t\tstd::uint64_t GetNewID();\n\t\tvoid FreeID(std::uint64_t id);\n\n\t\tstd::vector<Model*> m_loaded_models;\n\n\t};\n\n\ttemplate<typename TV, typename TI>\n\tModel* ModelPool::LoadCustom(std::vector<MeshData<TV, TI>> meshes)\n\t{\n\t\tIS_PROPER_VERTEX_CLASS(TV);\n\n\t\tauto model = new Model();\n\n\t\tstd::size_t total_vertex_size = 0;\n\t\tstd::size_t total_index_size = 0;\n\n\t\tfor (int i = 0; i < meshes.size(); ++i)\n\t\t{\n\t\t\ttotal_vertex_size += meshes[i].m_vertices.size() * sizeof(TV);\n\t\t\tif (meshes[i].m_indices.has_value())\n\t\t\t{\n\t\t\t\ttotal_index_size += meshes[i].m_indices.value().size() * sizeof(TI);\n\t\t\t}\n\t\t}\n\n\t\tMakeSpaceForModel(total_vertex_size, total_index_size);\n\n\t\tfor (int i = 0; i < meshes.size(); ++i)\n\t\t{\n\t\t\tMesh* mesh = new Mesh();\n\t\t\tif (meshes[i].m_indices.has_value())\n\t\t\t{\n\t\t\t\tinternal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices(\n\t\t\t\t\tmeshes[i].m_vertices.data(),\n\t\t\t\t\tmeshes[i].m_vertices.size(),\n\t\t\t\t\tsizeof(TV),\n\t\t\t\t\tmeshes[i].m_indices.value().data(),\n\t\t\t\t\tmeshes[i].m_indices.value().size(),\n\t\t\t\t\tsizeof(TI));\n\n\t\t\t\tstd::uint64_t id = GetNewID();\n\t\t\t\tm_loaded_meshes[id] = mesh_data;\n\t\t\t\tmesh->id = id;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tinternal::MeshInternal* mesh_data = LoadCustom_VerticesOnly(\n\t\t\t\t\tmeshes[i].m_vertices.data(),\n\t\t\t\t\tmeshes[i].m_vertices.size(),\n\t\t\t\t\tsizeof(TV));\n\n\t\t\t\tstd::uint64_t id = GetNewID();\n\t\t\t\tm_loaded_meshes[id] = mesh_data;\n\t\t\t\tmesh->id = id;\n\t\t\t}\n\t\t\tMaterialHandle handle = { nullptr, 0 };\n\t\t\tmodel->m_meshes.push_back(\n\t\t\t\tstd::make_pair(mesh, handle));\n\n\t\t\tif constexpr (std::is_same<TV, Vertex>::value || std::is_same<TV, VertexNoTangent>::value)\n\t\t\t{\n\t\t\t\tfor (uint32_t j = 0, k = (uint32_t) meshes[i].m_vertices.size(); j < k; ++j)\n\t\t\t\t{\n\t\t\t\t\tmodel->Expand(meshes[i].m_vertices[j].m_pos);\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\n\t\tmodel->m_model_pool = this;\n\n\t\tm_loaded_models.push_back(model);\n\n\t\treturn model;\n\t}\n\n\t//! Loads a model without materials\n\ttemplate<typename TV, typename TI>\n\tModel* ModelPool::Load(MaterialPool* material_pool, TexturePool* texture_pool, std::string_view path, std::optional<ModelData**> out_model_data)\n\t{\n\t\tIS_PROPER_VERTEX_CLASS(TV);\n\n\t\tModelLoader* loader = ModelLoader::FindFittingModelLoader(\n\t\t\tpath.substr(path.find_last_of(\".\") + 1).data());\n\n\t\tif (loader == nullptr)\n\t\t{\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tModelData* data = loader->Load(path);\n\n\t\tModel* model = new Model;\n\n\t\tMaterialHandle default_material = { nullptr, 0 };\n\n\t\t// TODO: Create default material\n\n\t\tMakeSpaceForModel(data->GetTotalVertexSize<TV>(), data->GetTotalIndexSize<TI>());\n\n\t\tint ret = LoadNodeMeshes<TV, TI>(data, model, default_material);\n\n\t\tif (ret == 1)\n\t\t{\n\t\t\tDestroyModel(model);\n\n\t\t\tloader->DeleteModel(data);\n\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tif (out_model_data.has_value())\n\t\t{\n\t\t\t(*out_model_data.value()) = data;\n\n\t\t}\n\t\telse\n\t\t{\n\t\t\tloader->DeleteModel(data);\n\t\t}\n\n\t\tmodel->m_model_name = path.data();\n\t\tmodel->m_model_pool = this;\n\n\t\tm_loaded_models.push_back(model);\n\n\t\treturn model;\n\t}\n\n\t//! Loads a model with materials\n\ttemplate<typename TV, typename TI>\n\tModel* ModelPool::LoadWithMaterials(MaterialPool* material_pool, TexturePool* texture_pool, std::string_view path, bool flip_normals, std::optional<ModelData**> out_model_data)\n\t{\n\t\tIS_PROPER_VERTEX_CLASS(TV);\n\n\t\tModelLoader* loader = ModelLoader::FindFittingModelLoader(\n\t\t\tpath.substr(path.find_last_of(\".\") + 1).data());\n\n\t\tif (loader == nullptr)\n\t\t{\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tModelData* data = loader->Load(path);\n\n\t\tif (flip_normals)\n\t\t{\n\t\t\tfor (auto* meshes : data->m_meshes)\n\t\t\t{\n\t\t\t\tfor (auto& normals : meshes->m_normals)\n\t\t\t\t{\n\t\t\t\t\tnormals.x = -1.f * normals.x;\n\t\t\t\t\tnormals.y = -1.f * normals.y;\n\t\t\t\t\tnormals.z = -1.f * normals.z;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Find directory\n\t\tstd::string dir = std::string(path);\n\t\tdir.erase(dir.begin() + dir.find_last_of('/') + 1, dir.end());\n\n\t\tModel* model = new Model;\n\t\tmodel->m_owns_materials = true;\n\t\tstd::vector<MaterialHandle> material_handles;\n\n\t\tfor (int i = 0; i < data->m_materials.size(); ++i)\n\t\t{\n\t\t\tTextureHandle albedo, normals, metallic, roughness, emissive, ambient_occlusion;\n\n\t\t\tModelMaterialData* material = data->m_materials[i];\n\n\t\t\t// This lambda loads a texture either from memory or from disc.\n\t\t\tauto load_material_texture = [&](auto texture_location, auto embedded_texture_idx, std::string &texture_path, TextureHandle &handle, bool srgb, bool gen_mips)\n\t\t\t{\n\t\t\t\tif (texture_location == TextureLocation::EMBEDDED)\n\t\t\t\t{\n\t\t\t\t\tEmbeddedTexture* texture = data->m_embedded_textures[embedded_texture_idx];\n\n\t\t\t\t\tif (texture->m_compressed)\n\t\t\t\t\t{\n\t\t\t\t\t\thandle = texture_pool->LoadFromMemory(texture->m_data.data(), texture->m_width, texture->m_height, texture->m_format, srgb, gen_mips);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\thandle = texture_pool->LoadFromMemory(texture->m_data.data(), texture->m_width, texture->m_height, wr::TextureFormat::RAW, srgb, gen_mips);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse if (texture_location == TextureLocation::EXTERNAL)\n\t\t\t\t{\n\t\t\t\t\thandle = texture_pool->LoadFromFile(dir + texture_path, srgb, gen_mips);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\t//TODO: Maya team integrate texture scales in loading\n\t\t\t// Currently default scales are set to 1 for all materials.\n\t\t\tMaterialUVScales default_scales;\n\n\t\t\tauto new_handle = material_pool->Create(texture_pool);\n\t\t\tMaterial* mat = material_pool->GetMaterial(new_handle);\n\n\t\t\tif (material->m_albedo_texture_location!=TextureLocation::NON_EXISTENT)\n\t\t\t{\n\t\t\t\tload_material_texture(material->m_albedo_texture_location, material->m_albedo_embedded_texture, material->m_albedo_texture, albedo, true, true);\n\t\t\t\tmat->SetTexture(TextureType::ALBEDO, albedo);\n\t\t\t}\n\n\t\t\tif (material->m_normal_map_texture_location != TextureLocation::NON_EXISTENT)\n\t\t\t{\n\t\t\t\tload_material_texture(material->m_normal_map_texture_location, material->m_normal_map_embedded_texture, material->m_normal_map_texture, normals, false, true);\n\t\t\t\tmat->SetTexture(TextureType::NORMAL, normals);\n\t\t\t}\n\n\t\t\tif (material->m_metallic_texture_location != TextureLocation::NON_EXISTENT)\n\t\t\t{\n\t\t\t\tload_material_texture(material->m_metallic_texture_location, material->m_metallic_embedded_texture, material->m_metallic_texture, metallic, false, true);\n\t\t\t\tmat->SetTexture(TextureType::METALLIC, metallic);\n\t\t\t}\n\n\t\t\tif (material->m_roughness_texture_location != TextureLocation::NON_EXISTENT)\n\t\t\t{\n\t\t\t\tload_material_texture(material->m_roughness_texture_location, material->m_roughness_embedded_texture, material->m_roughness_texture, roughness, false, true);\n\t\t\t\tmat->SetTexture(TextureType::ROUGHNESS, roughness);\n\t\t\t}\n\n\t\t\tif (material->m_emissive_texture_location != TextureLocation::NON_EXISTENT)\n\t\t\t{\n\t\t\t\tload_material_texture(material->m_emissive_texture_location, material->m_emissive_embedded_texture, material->m_emissive_texture, emissive, true, true);\n\t\t\t\tmat->SetTexture(TextureType::EMISSIVE, emissive);\n\t\t\t}\n\n\t\t\tif (material->m_ambient_occlusion_texture_location != TextureLocation::NON_EXISTENT)\n\t\t\t{\n\t\t\t\tload_material_texture(material->m_ambient_occlusion_texture_location, material->m_ambient_occlusion_embedded_texture, material->m_ambient_occlusion_texture, ambient_occlusion, false, true);\n\t\t\t\tmat->SetTexture(TextureType::AO, ambient_occlusion);\n\t\t\t}\n\n\t\t\tbool two_sided = material->m_two_sided;\n\n\t\t\tfloat opacity = material->m_base_transparency;\n\n\t\t\tmat->SetConstant<MaterialConstant::COLOR>({ material->m_base_color[0], material->m_base_color[1], material->m_base_color[2] });\n\t\t\tmat->SetConstant<MaterialConstant::METALLIC>(material->m_base_metallic);\n\t\t\tmat->SetConstant<MaterialConstant::EMISSIVE_MULTIPLIER>(material->m_base_emissive);\n\t\t\tmat->SetConstant<MaterialConstant::ROUGHNESS>(material->m_base_roughness);\n\t\t\tmat->SetConstant<MaterialConstant::IS_ALPHA_MASKED>(false);\n\t\t\tmat->SetConstant<MaterialConstant::IS_DOUBLE_SIDED>(false);\n\n\t\t\tmaterial_handles.push_back(new_handle);\n\t\t}\n\n\t\tMakeSpaceForModel(data->GetTotalVertexSize<TV>(), data->GetTotalIndexSize<TI>());\n\n\t\tint ret = LoadNodeMeshesWithMaterials<TV, TI>(data, model, material_handles);\n\n\t\tif (ret == 1)\n\t\t{\n\t\t\tDestroyModel(model);\n\t\t\tloader->DeleteModel(data);\n\t\t\treturn nullptr;\n\t\t}\n\n\t\tif (out_model_data.has_value())\n\t\t{\n\t\t\t(*out_model_data.value()) = data;\n\n\t\t}\n\t\telse\n\t\t{\n\t\t\tloader->DeleteModel(data);\n\t\t}\n\n\t\tmodel->m_model_name = path.data();\n\t\tmodel->m_model_pool = this;\n\n\t\tm_loaded_models.push_back(model);\n\n\t\treturn model;\n\t}\n\n\ttemplate<typename TV, typename TI>\n\tvoid ModelPool::EditMesh(Mesh* mesh, std::vector<TV> vertices, std::vector<TI> indices)\n\t{\n\t\tUpdateMeshData(mesh,\n\t\t\tvertices.data(),\n\t\t\tvertices.size(),\n\t\t\tsizeof(TV),\n\t\t\tindices.data(),\n\t\t\tindices.size(),\n\t\t\tsizeof(TI));\n\n\t\tfor (auto model : m_loaded_models)\n\t\t{\n\t\t\tfor (auto mesh_material : model->m_meshes)\n\t\t\t{\n\t\t\t\tif (mesh_material.first->id == mesh->id)\n\t\t\t\t{\n\t\t\t\t\tUpdateModelBoundingBoxes<TV>(model, vertices);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n} /* wr */\n"
  },
  {
    "path": "src/pipeline_registry.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"pipeline_registry.hpp\"\n\nnamespace wr\n{\n\n\tPipelineRegistry::PipelineRegistry() : Registry<PipelineRegistry, Pipeline, PipelineDescription>()\n\t{\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/pipeline_registry.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"registry.hpp\"\n\n#include <array>\n#include <optional>\n\n#include \"vertex.hpp\"\n#include \"util/named_type.hpp\"\n#include \"d3d12/d3d12_enums.hpp\"\n\nnamespace wr\n{\n\n\tusing Pipeline = void;\n\n\tstruct PipelineDescription\n\t{\n\t\tstd::optional<RegistryHandle> m_vertex_shader_handle;\n\t\tstd::optional<RegistryHandle> m_pixel_shader_handle;\n\t\tstd::optional<RegistryHandle> m_compute_shader_handle;\n\t\tRegistryHandle m_root_signature_handle;\n\n\t\tFormat m_dsv_format;\n\t\tstd::array<Format, 8> m_rtv_formats;\n\t\tunsigned int m_num_rtv_formats;\n\n\t\tPipelineType m_type = PipelineType::GRAPHICS_PIPELINE;\n\t\tCullMode m_cull_mode = wr::CullMode::CULL_BACK;\n\t\tbool m_depth_enabled = false;\n\t\tbool m_counter_clockwise = false;\n\t\tTopologyType m_topology_type = wr::TopologyType::TRIANGLE;\n\n\t\tstd::vector<D3D12_INPUT_ELEMENT_DESC> m_input_layout = {};\n\t};\n\n\tclass PipelineRegistry : public internal::Registry<PipelineRegistry, Pipeline, PipelineDescription>\n\t{\n\tpublic:\n\t\tPipelineRegistry();\n\t\tvirtual ~PipelineRegistry() = default;\n\n\t\ttemplate<typename VT>\n\t\tRegistryHandle Register(PipelineDescription description);\n\t};\n\n\ttemplate<typename VT>\n\tRegistryHandle PipelineRegistry::Register(PipelineDescription description)\n\t{\n\t\tIS_PROPER_VERTEX_CLASS(VT)\n\n\t\tauto handle = m_next_handle;\n\n\t\tdescription.m_input_layout = VT::GetInputLayout();\n\t\tm_descriptions.insert({ handle, description });\n\n\t\tm_next_handle++;\n\t\treturn handle;\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/platform_independend_structs.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <optional>\n#include <array>\n#include <DirectXMath.h>\n\n#include \"d3d12/d3d12_enums.hpp\"\n#include \"util/named_type.hpp\"\n#include \"util/user_literals.hpp\"\n\nnamespace wr\n{\n\tusing CommandList = void;\n\tusing RenderTarget = void;\n\n\tstruct RenderTargetProperties\n\t{\n\t\tusing IsRenderWindow = util::NamedType<bool>;\n\t\tusing Width = util::NamedType<std::optional<unsigned int>>;\n\t\tusing Height = util::NamedType<std::optional<unsigned int>>;\n\t\tusing ExecuteResourceState = util::NamedType<std::optional<ResourceState>>;\n\t\tusing FinishedResourceState = util::NamedType<std::optional<wr::ResourceState>>;\n\t\tusing CreateDSVBuffer = util::NamedType<bool>;\n\t\tusing DSVFormat = util::NamedType<Format>;\n\t\tusing RTVFormats = util::NamedType<std::array<Format, 8>>;\n\t\tusing NumRTVFormats = util::NamedType<unsigned int>;\n\t\tusing Clear = util::NamedType<bool>;\n\t\tusing ClearDepth = util::NamedType<bool>;\n\t\tusing ResolutionScalar = util::NamedType<float>;\n\n\t\tIsRenderWindow m_is_render_window;\n\t\tWidth m_width;\n\t\tHeight m_height;\n\t\tExecuteResourceState m_state_execute;\n\t\tFinishedResourceState m_state_finished;\n\t\tCreateDSVBuffer m_create_dsv_buffer;\n\t\tDSVFormat m_dsv_format;\n\t\tRTVFormats m_rtv_formats;\n\t\tNumRTVFormats m_num_rtv_formats;\n\n\t\tClear m_clear = Clear(false);\n\t\tClearDepth m_clear_depth = ClearDepth(false);\n\n\t\tResolutionScalar m_resolution_scale = ResolutionScalar(1.0f);\n\t};\n\n\tenum class LightType : int\n\t{\n\t\tPOINT, DIRECTIONAL, SPOT, FREE\n\t};\n\n\tstruct Light\n\t{\n\t\tDirectX::XMFLOAT3 pos = { 0, 0, 0 };\t\t\t//Position in world space for spot & point\n\t\tfloat rad = 5.f;\t\t\t\t\t\t\t\t//Radius for point, height for spot\n\n\t\tDirectX::XMFLOAT3 col = { 1, 1, 1 };\t\t\t//Color (and strength)\n\t\tuint32_t tid = (uint32_t)LightType::FREE;\t\t//Type id; LightType::x\n\n\t\tDirectX::XMFLOAT3 dir = { 0, 0, 1 };\t\t\t//Direction for spot & directional\n\t\tfloat ang = 40._deg;\t\t\t\t\t\t\t//Angle for spot; in radians\n\n\t\tDirectX::XMFLOAT3 padding;\n\t\tfloat light_size = 0.0f;\n\t};\n\n} /* wr */"
  },
  {
    "path": "src/registry.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <map>\n#include <mutex>\n\n#include \"util/log.hpp\"\n\n#undef GetObject // Prevent macro collision\n\nnamespace wr\n{\n\tusing RegistryHandle = std::uint64_t;\n}\n\nnamespace wr::internal\n{\n\ttemplate<typename C, typename TO, typename TD>\n\tclass Registry\n\t{\n\tprotected:\n\t\tRegistry() : m_next_handle(0) { }\n\n\tpublic:\n\t\tvirtual ~Registry() = default;\n\n\t\tTO* Find(RegistryHandle handle)\n\t\t{\n\t\t\tauto it = m_objects.find(handle);\n\t\t\tif (it != m_objects.end())\n\t\t\t{\n\t\t\t\treturn (*it).second;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tLOGE(\"Failed to find registry object related to registry handle.\");\n\t\t\t\treturn nullptr;\n\t\t\t}\n\t\t}\n\n\t\tstatic C& Get()\n\t\t{\n\t\t\tstatic C instance = {};\n\t\t\treturn instance;\n\t\t}\n\n\t\tvoid Lock()\n\t\t{\n\t\t\tm_mutex.lock();\n\t\t}\n\n\t\tvoid Unlock()\n\t\t{\n\t\t\tm_mutex.unlock();\n\t\t}\n\n\t\tvoid ClearReloadRequests()\n\t\t{\n\t\t\tm_requested_reload.clear();\n\t\t}\n\n\t\tstd::vector<RegistryHandle> const & GetReloadRequests()\n\t\t{\n\t\t\treturn m_requested_reload;\n\t\t}\n\n\t\tvoid RequestReload(RegistryHandle handle)\n\t\t{\n\t\t\tLock();\n\t\t\tm_requested_reload.push_back(handle);\n\t\t\tUnlock();\n\t\t}\n\n\t\tstd::map<RegistryHandle, TD> m_descriptions;\n\t\tstd::map<RegistryHandle, TO*> m_objects;\n\n\tprotected:\n\t\tstd::vector<RegistryHandle> m_requested_reload;\n\t\tstd::mutex m_mutex;\n\t\tRegistryHandle m_next_handle;\n\t};\n\n} /* wr::internal */"
  },
  {
    "path": "src/render_tasks/d3d12_accumulation.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"d3d12_raytracing_task.hpp\"\n#include \"d3d12_path_tracer.hpp\"\n\nnamespace wr\n{\n\tstruct AccumulationData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\n\t\ttemplate<typename T>\n\t\tinline void SetupAccumulationTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<AccumulationData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\t//Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors\n\t\t\tstd::shared_ptr<D3D12TexturePool> texture_pool = std::static_pointer_cast<D3D12TexturePool>(n_render_system.m_texture_pools[0]);\n\t\t\tif (!texture_pool)\n\t\t\t{\n\t\t\t\tLOGC(\"Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool\");\n\t\t\t}\n\n\t\t\tdata.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\tdata.out_allocation = data.out_allocator->Allocate(2);\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::accumulation));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\tfor (auto frame_idx = 0; frame_idx < 1; frame_idx++)\n\t\t\t{\n\t\t\t\t// Destination\n\t\t\t\t{\n\t\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::accumulation, params::AccumulationE::DEST);\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, frame_idx, n_render_target->m_create_info.m_rtv_formats[frame_idx]);\n\t\t\t\t}\n\t\t\t\t// Source\n\t\t\t\t{\n\t\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::accumulation, params::AccumulationE::SOURCE);\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, frame_idx, source_rt->m_create_info.m_rtv_formats[frame_idx]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tinline void ExecuteAccumulationTask(RenderSystem& rs, FrameGraph& fg, SceneGraph&, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<AccumulationData>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\tconstexpr unsigned int uav_idx = rs_layout::GetHeapLoc(params::accumulation, params::AccumulationE::DEST);\n\t\t\td3d12::DescHeapCPUHandle uav_handle = data.out_allocation.GetDescriptorHandle(uav_idx);\n\t\t\td3d12::SetShaderUAV(cmd_list, 0, uav_idx, uav_handle);\n\n\t\t\tconstexpr unsigned int srv_idx = rs_layout::GetHeapLoc(params::accumulation, params::AccumulationE::SOURCE);\n\t\t\td3d12::DescHeapCPUHandle srv_handle = data.out_allocation.GetDescriptorHandle(srv_idx);\n\t\t\td3d12::SetShaderSRV(cmd_list, 0, srv_idx, srv_handle);\n\n\t\t\td3d12::BindDescriptorHeaps(cmd_list);\n\n\t\t\t{\n\t\t\t\tauto barrier = CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % 1 /*versions*/]);\n\t\t\t\tcmd_list->m_native->ResourceBarrier(1, &barrier);\n\t\t\t}\n\n\t\t\tfg.WaitForPredecessorTask<PathTracerData>();\n\n\t\t\tfloat samples = n_render_system.temp_rough;\n\t\t\td3d12::BindCompute32BitConstants(cmd_list, &samples, 1, 0, 1);\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyAccumulation(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddAccumulationTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupAccumulationTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteAccumulationTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyAccumulation(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<AccumulationData>(desc, L\"Accumulation Task\", FG_DEPS<T>());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_ansel.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"d3d12_raytracing_task.hpp\"\n#include \"d3d12_deferred_main.hpp\"\n\n#ifdef NVIDIA_GAMEWORKS_ANSEL\n#include <AnselSDK.h>\n#endif\n\nnamespace wr\n{\n\tstruct AnselSettings\n\t{\n\t\tstruct Setup\n\t\t{\n\t\t\tfloat m_meters_in_world_unit = 0.5f; // Game scale, the size of a world unit measured in meters. Viktor: Honestly I don't get this value. All speeds are set in unit space not meters...\n\t\t};\n\n\t\tstruct Runtime\n\t\t{\n\t\t\tbool m_allow_translation = true; // User can move the camera during session\n\t\t\tbool m_allow_rotation = true; // Camera can be rotated during session\n\t\t\tbool m_allow_fov = true; // FoV can be modified during session\n\t\t\tbool m_allow_mono_360 = true; // Game allows 360 capture during session\n\t\t\tbool m_allow_stero_360 = true; // Game allows 360 stereo capture during session\n\t\t\tbool m_allow_raw = false; // Game allows capturing pre-tonemapping raw HDR buffer\n\t\t\tbool m_allow_pause = true; // Game is paused during capture\n\t\t\tbool m_allow_highres = true; // Game allows highres capture during session\n\t\t\tfloat m_translation_speed_in_world_units_per_sec = 2.f; // The speed at which camera moves in the world, initialized with a value given in Configuration\n\t\t\tfloat m_rotation_speed_in_deg_per_second = 90.f; // The speed at which camera rotates, initialized with a value given in Configuration\n\t\t\tfloat m_maximum_fov_in_deg = 179; // The maximum FoV value in degrees displayed in the Ansel UI. Any value in the range [140, 179] can be specified and values outside will be clamped to this range.\n\t\t};\n\n\t\tSetup m_setup;\n\t\tRuntime m_runtime;\n\t};\n\n\tstruct AnselData\n\t{\n\t\tbool out_ansel_support = false;\n#ifdef NVIDIA_GAMEWORKS_ANSEL\n\t\tstd::optional<ansel::Camera> out_original_camera; // Used to restore the camera to the original settings when exiting Ansel.\n#endif\n\t};\n\n\t// Static variables so we can access them from the callbacks.\n\tstatic AnselSettings ansel_settings = AnselSettings();\n\tstatic bool ansel_session_running = false;\n\n\tnamespace internal\n\t{\n#ifdef NVIDIA_GAMEWORKS_ANSEL\n\t\tauto fill_ansel_config_obj = [](auto & conf)\n\t\t{\n\t\t\tconf.isTranslationAllowed = ansel_settings.m_runtime.m_allow_translation;\n\t\t\tconf.isRotationAllowed = ansel_settings.m_runtime.m_allow_rotation;\n\t\t\tconf.isFovChangeAllowed = ansel_settings.m_runtime.m_allow_fov;\n\t\t\tconf.is360MonoAllowed = ansel_settings.m_runtime.m_allow_mono_360;\n\t\t\tconf.is360StereoAllowed = ansel_settings.m_runtime.m_allow_stero_360;\n\t\t\tconf.isHighresAllowed = ansel_settings.m_runtime.m_allow_highres;\n\t\t\tconf.isRawAllowed = ansel_settings.m_runtime.m_allow_raw;\n\t\t\tconf.isPauseAllowed = ansel_settings.m_runtime.m_allow_pause;\n\t\t\tconf.translationalSpeedInWorldUnitsPerSecond = ansel_settings.m_runtime.m_translation_speed_in_world_units_per_sec;\n\t\t\tconf.rotationalSpeedInDegreesPerSecond = ansel_settings.m_runtime.m_rotation_speed_in_deg_per_second;\n\t\t\tconf.maximumFovInDegrees = ansel_settings.m_runtime.m_maximum_fov_in_deg;\n\t\t};\n\n\t\tansel::StartSessionStatus startAnselSessionCallback(ansel::SessionConfiguration& conf, void* userPointer)\n\t\t{\n\t\t\tansel_session_running = true;\n\n\t\t\tfill_ansel_config_obj(conf);\n\n\n\t\t\treturn ansel::kAllowed;\n\t\t}\n\n\t\tvoid stopAnselSessionCallback(void* userPointer)\n\t\t{\n\t\t\tansel_session_running = false;\n\t\t}\n\n\t\tvoid startAnselCaptureCallback(ansel::CaptureConfiguration const & conf, void* userPointer)\n\t\t{\n\t\t}\n\n\t\tvoid stopAnselCaptureCallback(void* userPointer)\n\t\t{\n\t\t}\n\n\n\t\tansel::Camera NativeToAnselCamera(std::shared_ptr<CameraNode> const & camera)\n\t\t{\n\t\t\tauto proj_offset = camera->GetProjectionOffset();\n\n\t\t\tansel::Camera ansel_camera{};\n\t\t\tansel_camera.aspectRatio = camera->m_aspect_ratio;\n\t\t\tansel_camera.fov = DirectX::XMConvertToDegrees(camera->m_fov.m_fov);\n\t\t\tansel_camera.position = { camera->m_position.m128_f32[0], camera->m_position.m128_f32[1], camera->m_position.m128_f32[2] };\n\t\t\tansel_camera.rotation = { camera->m_rotation.m128_f32[0], camera->m_rotation.m128_f32[1], camera->m_rotation.m128_f32[2], camera->m_rotation.m128_f32[3] };\n\t\t\tansel_camera.nearPlane = camera->m_frustum_near;\n\t\t\tansel_camera.farPlane = camera->m_frustum_far;\n\t\t\tansel_camera.projectionOffsetX = proj_offset.first;\n\t\t\tansel_camera.projectionOffsetY = proj_offset.second;\n\n\t\t\treturn ansel_camera;\n\t\t}\n\n\t\tvoid AnselToNativeCamera(ansel::Camera const & ansel_camera, std::shared_ptr<CameraNode> camera)\n\t\t{\n\t\t\tcamera->SetProjectionOffset(ansel_camera.projectionOffsetX, ansel_camera.projectionOffsetY);\n\t\t\tcamera->SetPosition({ ansel_camera.position.x, ansel_camera.position.y, ansel_camera.position.z });\n\t\t\tcamera->SetRotationQuaternion({ ansel_camera.rotation.x, ansel_camera.rotation.y , ansel_camera.rotation.z , ansel_camera.rotation.w });\n\t\t\tcamera->m_fov = wr::CameraNode::FoV(wr::CameraNode::FovDefault(ansel_camera.fov));\n\t\t\tcamera->m_frustum_near = ansel_camera.nearPlane;\n\t\t\tcamera->m_frustum_far = ansel_camera.farPlane;\n\t\t}\n#endif\n\n\t\tinline void SetupAnselTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n#ifdef NVIDIA_GAMEWORKS_ANSEL\n\t\t\tif (resize)\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<AnselData>(handle);\n\t\t\tansel_settings = fg.GetSettings<AnselSettings>(handle);\n\n\t\t\tif (!n_render_system.m_window.has_value())\n\t\t\t{\n\t\t\t\tLOGW(\"Tried initializing ansel without a render window! Ansel is not supported for headless rendering.\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tdata.out_ansel_support = ansel::isAnselAvailable();\n\t\t\tif (!data.out_ansel_support)\n\t\t\t{\n\t\t\t\tLOGW(\"Your machine doesn't seem to support NVIDIA Ansel\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tauto window = n_render_system.m_window.value();\n\n\t\t\tansel::Configuration config;\n\t\t\tconfig.translationalSpeedInWorldUnitsPerSecond = ansel_settings.m_runtime.m_translation_speed_in_world_units_per_sec;\n\t\t\tconfig.metersInWorldUnit = ansel_settings.m_setup.m_meters_in_world_unit;\n\t\t\tconfig.right = { 1, 0, 0 };\n\t\t\tconfig.up = { 0, 1, 0 };\n\t\t\tconfig.forward = { 0, 0, -1 };\n\t\t\tconfig.fovType = ansel::kVerticalFov;\n\t\t\tconfig.isCameraOffcenteredProjectionSupported = true;\n\t\t\tconfig.isCameraRotationSupported = true;\n\t\t\tconfig.isCameraTranslationSupported = true;\n\t\t\tconfig.isCameraFovSupported = true;\n\t\t\tconfig.gameWindowHandle = window->GetWindowHandle();\n\t\t\tconfig.titleNameUtf8 = window->GetTitle().c_str();\n\n\t\t\tconfig.startSessionCallback = startAnselSessionCallback;\n\t\t\tconfig.stopSessionCallback = stopAnselSessionCallback;\n\t\t\tconfig.startCaptureCallback = startAnselCaptureCallback;\n\t\t\tconfig.stopCaptureCallback = stopAnselCaptureCallback;\n\n\t\t\tauto status = ansel::setConfiguration(config);\n\t\t\tif (status != ansel::kSetConfigurationSuccess)\n\t\t\t{\n\t\t\t\tLOGW(\"Failed to initialize NVIDIA Ansel.\");\n\t\t\t}\n\n\t\t\tLOG(\"NVIDIA Ansel is initialized\");\n#endif\n\t\t}\n\n\t\tinline void ExecuteAnselTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n#ifdef NVIDIA_GAMEWORKS_ANSEL\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<AnselData>(handle);\n\t\t\tansel_settings = fg.GetSettings<AnselSettings>(handle);\n\n\t\t\tif (!data.out_ansel_support)\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (!n_render_system.m_render_window.has_value())\n\t\t\t{\n\t\t\t\tLOGW(\"Tried initializing ansel without a render window! Ansel is not supported for headless rendering.\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (ansel_session_running)\n\t\t\t{\n\t\t\t\tauto camera = sg.GetActiveCamera();\n\t\t\t\tauto ansel_camera = NativeToAnselCamera(camera);\n\n\t\t\t\tif (!data.out_original_camera.has_value())\n\t\t\t\t{\n\t\t\t\t\tdata.out_original_camera = ansel_camera;\n\t\t\t\t}\n\n\t\t\t\tansel::updateCamera(ansel_camera);\n\n\t\t\t\tAnselToNativeCamera(ansel_camera, camera);\n\t\t\t}\n\t\t\telse if (data.out_original_camera.has_value())\n\t\t\t{\n\t\t\t\tauto camera = sg.GetActiveCamera();\n\t\t\t\tauto ansel_camera = data.out_original_camera.value();\n\n\t\t\t\tAnselToNativeCamera(ansel_camera, camera);\n\n\t\t\t\tdata.out_original_camera = std::nullopt;\n\t\t\t}\n#endif\n\t\t}\n\n\t\tinline void DestroyAnselTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n#ifdef NVIDIA_GAMEWORKS_ANSEL\n#endif\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddAnselTask(FrameGraph& frame_graph)\n\t{\n#ifndef NVIDIA_GAMEWORKS_ANSEL\n\t\tLOGW(\"Ansel task has been added to the frame graph. But `NVIDIA_GAMEWORKS_ANSEL` is not defined.\");\n#endif\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ d3d12::settings::back_buffer_format }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupAnselTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteAnselTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyAnselTask(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<AnselData>(desc, L\"NVIDIA Ansel\");\n\t\tframe_graph.UpdateSettings<AnselData>(AnselSettings());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_bloom_composition.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\n\tstruct BloomSettings\n\t{\n\t\tstruct Runtime\n\t\t{\n\t\t\tfloat m_sigma = 2.0f;\n\t\t\tfloat m_bloom_intensity = 1.0f;\n\t\t\tfloat m_luminance_threshold = 1.0f;\n\t\t\tbool m_enable_bloom = true; \n\t\t};\n\n\t\tRuntime m_runtime;\n\t};\n\n\tstruct BloomCompostionData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_bloom_half_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_bloom_quarter_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_bloom_eighth_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_bloom_sixteenth_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\n\t\tstd::shared_ptr<ConstantBufferPool> bloom_cb_pool;\n\t\tD3D12ConstantBufferHandle* cb_handle = nullptr;\n\t};\n\n\tnamespace internal\n\t{\n\t\ttemplate<typename T, typename T1>\n\t\tinline void SetupBloomCompositionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<BloomCompostionData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(5);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::bloom_composition));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto bloom_rt_half = data.out_source_bloom_half_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\n\t\t\t// Destination near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source main\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition , params::BloomCompositionE::SOURCE_MAIN)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source bloom half\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_HALF)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(bloom_rt_half, cpu_handle, 0, bloom_rt_half->m_create_info.m_rtv_formats[0]);\n\t\t\t}\t\t\n\t\t\t// Source bloom qes\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_QES)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(bloom_rt_half, cpu_handle, 1, bloom_rt_half->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\ttemplate<typename T, typename T1>\n\t\tinline void ExecuteBloomCompositionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<BloomCompostionData>(handle);\n\t\t\tauto settings = fg.GetSettings<BloomSettings>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto bloom_rt_half = data.out_source_bloom_half_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\t\t\t\n\n\t\t\t// Destination near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source main\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_MAIN)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source bloom half\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_HALF)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(bloom_rt_half, cpu_handle, 0, bloom_rt_half->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source bloom qes\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_QES)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(bloom_rt_half, cpu_handle, 1, bloom_rt_half->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\n\n\n\t\t\tint enable_bloom = settings.m_runtime.m_enable_bloom;\n\t\t\td3d12::BindCompute32BitConstants(cmd_list, &enable_bloom, 1, 0, 1);\n\n\t\t\t//cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(0, 0, 3, data.out_allocation.GetDescriptorHandle());\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::OUTPUT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_main_idx = rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_MAIN);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_main_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_main_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_bloom_half_idx = rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_HALF);\n\t\t\t\tauto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_bloom_half_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_bloom_half_idx, handle_b_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_bloom_qes_idx = rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_QES);\n\t\t\t\tauto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_bloom_qes_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_bloom_qes_idx, handle_b_srv);\n\t\t\t}\n\n\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyBloomCompositionTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<BloomCompostionData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T, typename T1>\n\tinline void AddBloomCompositionTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(1.0f)\n\t\t};\n\n\t\tRenderTaskDesc desc; \n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupBloomCompositionTask<T, T1>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteBloomCompositionTask<T, T1>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyBloomCompositionTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<BloomCompostionData>(desc, L\"Bloom Composition\");\n\t\tframe_graph.UpdateSettings<BloomCompostionData>(BloomSettings());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_bloom_extract_bright.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct BloomExtractBrightData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_emissive = nullptr;\n\t\td3d12::RenderTarget* out_source_coc = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\t\n\t\ttemplate<typename T, typename T1>\n\t\tinline void SetupBloomExtractBrightTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<BloomExtractBrightData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(4);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::bloom_extract_bright));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto source_emissive = data.out_source_emissive = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\n\t\t\t// Bright output for bloom\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::OUTPUT_BRIGHT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t// Depth buffer\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_DEPTH)));\n\t\t\t\td3d12::CreateSRVFromDSV(source_emissive, cpu_handle);\n\t\t\t}\n\n\t\t\t// Source emissive\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_EMISSIVE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_emissive, cpu_handle, 2, source_emissive->m_create_info.m_rtv_formats[2]);\n\t\t\t}\n\t\t}\n\n\t\ttemplate<typename T, typename T1>\n\t\tinline void ExecuteBloomExtractBrightTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<BloomExtractBrightData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto source_emissive = data.out_source_emissive = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\n\t\t\t// Bright output for bloom\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::OUTPUT_BRIGHT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Depth buffer\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_DEPTH)));\n\t\t\t\td3d12::CreateSRVFromDSV(source_emissive, cpu_handle);\n\t\t\t}\n\n\t\t\t// Source emissive\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_EMISSIVE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_emissive, cpu_handle, 2, source_emissive->m_create_info.m_rtv_formats[2]);\n\t\t\t}\n\n\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_b_idx = rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::OUTPUT_BRIGHT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_b_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_b_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::SOURCE);\n\t\t\t\tauto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_b_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_EMISSIVE);\n\t\t\t\tauto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_b_srv);\n\t\t\t}\n\t\t\t\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_DEPTH);\n\t\t\t\tauto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_b_srv);\n\t\t\t}\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyBloomExtractBrightTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<BloomExtractBrightData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T, typename T1>\n\tinline void AddBloomExtractBrightTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.5f)\n\t\t};\n\n\t\tRenderTaskDesc desc; \n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupBloomExtractBrightTask<T, T1>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteBloomExtractBrightTask<T, T1>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyBloomExtractBrightTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<BloomExtractBrightData>(desc, L\"extract bright\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_bloom_horizontal_blur.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\n\tstruct BloomBlurHorizontalData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_bloom_qes_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\n\t\tstd::shared_ptr<ConstantBufferPool> cb_pool;\n\t\tD3D12ConstantBufferHandle* cb_handle = nullptr;\n\t};\n\n\tnamespace internal\n\t{\n\n\t\tstruct Bloomblur_CB\n\t\t{\n\t\t\tDirectX::XMFLOAT2 blur_dir;\n\t\t\tfloat _pad = 0.f;\n\t\t\tfloat sigma;\n\t\t};\n\n\t\ttemplate<typename T>\n\t\tinline void SetupBloomBlurHorizontalTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<BloomBlurHorizontalData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(4);\n\n\t\t\t\tdata.cb_pool = rs.CreateConstantBufferPool(2);\n\t\t\t\tdata.cb_handle = static_cast<D3D12ConstantBufferHandle*>(data.cb_pool->Create(sizeof(Bloomblur_CB)));\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::bloom_blur_horizontal));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Output half \n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Output quarter/ eighth / sixteenth\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT_QES)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Source main\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::SOURCE_MAIN)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecuteBloomBlurHorizontalTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<BloomBlurHorizontalData>(handle);\n\t\t\tauto settings = fg.GetSettings<BloomSettings>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Output half \n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Output quarter/ eighth / sixteenth\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT_QES)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Source main\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::SOURCE_MAIN)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\tBloomblur_CB cb_data;\n\t\t\tcb_data.blur_dir = DirectX::XMFLOAT2(1.0f, 0.0f);\n\t\t\tcb_data.sigma = 2.0f;\n\n\t\t\tdata.cb_handle->m_pool->Update(data.cb_handle, sizeof(Bloomblur_CB), 0, frame_idx, (uint8_t*)& cb_data);\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_blur_horizontal , params::BloomBlurHorizontalE::OUTPUT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT_QES);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_main_idx = rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::SOURCE_MAIN);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_main_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_main_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\t\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\tfloat width = n_render_system.m_viewport.m_viewport.Width;\n\t\t\tfloat height = n_render_system.m_viewport.m_viewport.Height;\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(width / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(width / 2.0f / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(width / 4 / 16.f)) +\n\t\t\t\tstatic_cast<int>(std::ceil(width / 8 / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(height / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(height / 2.0f / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(height / 4.0f / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(height / 8.0f / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyBloomBlurHorizontalTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<BloomBlurHorizontalData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddBloomBlurHorizontalTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(2),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.5f)\n\t\t};\n\n\t\tRenderTaskDesc desc; \n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupBloomBlurHorizontalTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteBloomBlurHorizontalTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyBloomBlurHorizontalTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<BloomBlurHorizontalData>(desc, L\"Bloom blur test\");\n\t\tframe_graph.UpdateSettings<BloomBlurHorizontalData>(BloomSettings());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_bloom_vertical_blur.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n#include \"..//render_tasks/d3d12_bloom_horizontal_blur.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\n\tstruct BloomBlurVerticalData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_bloom_qes_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\n\t\tstd::shared_ptr<ConstantBufferPool> cb_pool;\n\t\tD3D12ConstantBufferHandle* cb_handle = nullptr;\n\t};\n\n\tnamespace internal\n\t{\n\n\t\ttemplate<typename T>\n\t\tinline void SetupBloomBlurVerticalTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<BloomBlurVerticalData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(5);\n\n\t\t\t\tdata.cb_pool = rs.CreateConstantBufferPool(2);\n\t\t\t\tdata.cb_handle = static_cast<D3D12ConstantBufferHandle*>(data.cb_pool->Create(sizeof(Bloomblur_CB)));\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::bloom_blur_vertical));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Output half \n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Output quarter/ eighth / sixteenth\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT_QES)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Source main\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_MAIN)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t// Source quarter eighth sixteeth\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_QES)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecuteBloomBlurVerticalTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<BloomBlurVerticalData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Output half \n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Output quarter/ eighth / sixteenth\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT_QES)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Source main\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_MAIN)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source quarter eighth sixteeth\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_QES)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\n\t\t\tBloomblur_CB cb_data;\n\t\t\tcb_data.blur_dir = DirectX::XMFLOAT2(0.0f, 1.0f);\n\t\t\tcb_data.sigma = 2.0f;\n\n\t\t\tdata.cb_handle->m_pool->Update(data.cb_handle, sizeof(Bloomblur_CB), 0, frame_idx, (uint8_t*)& cb_data);\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT_QES);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_main_idx = rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_MAIN);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_main_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_main_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_main_idx = rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_QES);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_main_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_main_idx, handle_m_srv);\n\t\t\t}\n\t\t\t\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\tfloat width = n_render_system.m_viewport.m_viewport.Width;\n\t\t\tfloat height = n_render_system.m_viewport.m_viewport.Height;\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(width / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(width / 2.0f / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(width / 4 / 16.f)) +\n\t\t\t\tstatic_cast<int>(std::ceil(width / 8 / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(height / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(height / 2.0f / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(height / 4.0f / 16.f)) + \n\t\t\t\tstatic_cast<int>(std::ceil(height / 8.0f / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyBloomBlurVerticalTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<BloomBlurVerticalData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddBloomBlurVerticalTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(2),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.5f)\n\t\t};\n\n\t\tRenderTaskDesc desc; \n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupBloomBlurVerticalTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteBloomBlurVerticalTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyBloomBlurVerticalTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<BloomBlurVerticalData>(desc, L\"Bloom blur test\");\n\t\t//frame_graph.UpdateSettings<BloomBlurVerticalData>(BloomSettings());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_brdf_lut_precalculation.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_defines.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../pipeline_registry.hpp\"\n#include \"../engine_registry.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_descriptors_allocations.hpp\"\n#include \"../d3d12/d3d12_resource_pool_texture.hpp\"\n\nnamespace wr\n{\n\tstruct BrdfLutTaskData\n\t{\n\t\td3d12::PipelineState* in_pipeline;\n\t};\n\n\tnamespace internal\n\t{\n\t\tinline void SetupBrdfLutPrecalculationTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<BrdfLutTaskData>(handle);\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::brdf_lut_precalculation);\n\n\t\t\tif (!resize && !n_render_system.m_brdf_lut.has_value())\n\t\t\t{\n\t\t\t\t//Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors\n\t\t\t\tstd::shared_ptr<D3D12TexturePool> texture_pool = std::static_pointer_cast<D3D12TexturePool>(n_render_system.m_texture_pools[0]);\n\t\t\t\tif (!texture_pool)\n\t\t\t\t{\n\t\t\t\t\tLOGC(\"Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool\");\n\t\t\t\t}\n\n\t\t\t\tif (n_render_system.m_brdf_lut == std::nullopt)\n\t\t\t\t{\n\t\t\t\t\tn_render_system.m_brdf_lut = texture_pool->CreateTexture(\"BRDF LUT 2D\", 512, 512, 1, wr::Format::R16G16_FLOAT, false);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tinline void ExecuteBrdfLutPrecalculationTask(RenderSystem& rs, FrameGraph& fg, SceneGraph&, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<BrdfLutTaskData>(handle);\n\n\t\t\tif (!n_render_system.m_brdf_lut_generated)\n\t\t\t{\n\t\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\n\t\t\t\td3d12::BindComputePipeline(cmd_list, data.in_pipeline);\n\n\t\t\t\tauto* brdf_lut = static_cast<d3d12::TextureResource*>(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value()));\n\n\t\t\t\td3d12::Transition(cmd_list, brdf_lut, ResourceState::COPY_DEST, ResourceState::UNORDERED_ACCESS);\n\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::brdf_lut, params::BRDF_LutE::OUTPUT)), brdf_lut);\n\n\t\t\t\td3d12::Dispatch(cmd_list, static_cast<int>(512 / 16), static_cast<int>(512 / 16.f), 1);\n\n\t\t\t\td3d12::Transition(cmd_list, brdf_lut, ResourceState::UNORDERED_ACCESS, ResourceState::PIXEL_SHADER_RESOURCE);\n\n\t\t\t\tn_render_system.m_brdf_lut_generated = true;\n\t\t\t\tfg.SetShouldExecute(handle, false);\n\t\t\t}\n\t\t}\n\t}\n\n\tinline void AddBrdfLutPrecalculationTask(FrameGraph& fg)\n\t{\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupBrdfLutPrecalculationTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteBrdfLutPrecalculationTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph&, RenderTaskHandle, bool) {\n\t\t};\n\n\t\tdesc.m_properties = std::nullopt;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tfg.AddTask<BrdfLutTaskData>(desc, L\"BRDF LUT Precalculation\");\n\t}\n}"
  },
  {
    "path": "src/render_tasks/d3d12_build_acceleration_structures.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_rt_descriptor_heap.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../imgui_tools.hpp\"\n\nnamespace wr\n{\n\n\tstruct ASBuildSettings\n\t{\n\t\tstruct Runtime\n\t\t{\n\t\t\tbool m_rebuild_as = false;\n\t\t};\n\t\tRuntime m_runtime;\n\t};\n\n\tstruct ASBuildData\n\t{\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_scene_ib_alloc;\n\t\tDescriptorAllocation out_scene_mat_alloc;\n\t\tDescriptorAllocation out_scene_offset_alloc;\n\n\t\td3d12::AccelerationStructure out_tlas = {};\n\t\tD3D12StructuredBufferHandle* out_sb_material_handle = nullptr;\n\t\tD3D12StructuredBufferHandle* out_sb_offset_handle = nullptr;\n\t\tstd::vector<d3d12::desc::BlasDesc> out_blas_list;\n\t\tstd::vector<temp::RayTracingMaterial_CBData> out_materials;\n\t\tstd::vector<temp::RayTracingOffset_CBData> out_offsets;\n\t\tstd::unordered_map<std::uint64_t, std::uint64_t> out_parsed_materials;\n\t\tstd::vector<MaterialHandle> out_material_handles;\n\t\td3d12::StagingBuffer* out_scene_ib;\n\t\td3d12::StagingBuffer* out_scene_vb;\n\n\t\tstd::unordered_map<std::uint64_t, d3d12::AccelerationStructure> blasses;\n\n\t\tstd::vector<std::vector<d3d12::AccelerationStructure>> old_blasses;\n\t\tstd::vector<d3d12::AccelerationStructure> old_tlas;\n\n\t\tunsigned int previous_frame_index = 0;\n\t\tunsigned int current_frame_index = 0;\n\n\t\tbool out_init = true;\n\t\tbool out_materials_require_update = true;\n\t};\n\n\tnamespace internal\n\t{\n\n\t\tinline void SetupBuildASTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (resize) return;\n\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<ASBuildData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\td3d12::SetName(n_render_target, L\"Raytracing Target\");\n\n\t\t\tdata.out_init = true;\n\t\t\tdata.out_materials_require_update = true;\n\n\t\t\t// Structured buffer for the materials.\n\t\t\tdata.out_sb_material_handle = static_cast<D3D12StructuredBufferHandle*>(n_render_system.m_raytracing_material_sb_pool->Create(sizeof(temp::RayTracingMaterial_CBData) * d3d12::settings::num_max_rt_materials, sizeof(temp::RayTracingMaterial_CBData), false));\n\n\t\t\t// Structured buffer for the materials.\n\t\t\tdata.out_sb_offset_handle = static_cast<D3D12StructuredBufferHandle*>(n_render_system.m_raytracing_offset_sb_pool->Create(sizeof(temp::RayTracingOffset_CBData) * d3d12::settings::num_max_rt_materials, sizeof(temp::RayTracingOffset_CBData), false));\n\n\t\t\t// Resize the materials\n\t\t\tdata.out_materials.reserve(d3d12::settings::num_max_rt_materials);\n\t\t\tdata.out_offsets.reserve(d3d12::settings::num_max_rt_materials);\n\t\t\tdata.out_parsed_materials.reserve(d3d12::settings::num_max_rt_materials);\n\n\t\t\t\n\t\t\tauto texture_pool = std::static_pointer_cast<D3D12TexturePool>(n_render_system.GetDefaultTexturePool());\n\n\t\t\tdata.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\n\t\t\tdata.out_scene_ib_alloc = std::move(data.out_allocator->Allocate());\n\t\t\tdata.out_scene_mat_alloc = std::move(data.out_allocator->Allocate());\n\t\t\tdata.out_scene_offset_alloc = std::move(data.out_allocator->Allocate());\n\n\t\t\tdata.old_blasses.resize(d3d12::settings::num_back_buffers);\n\t\t\tdata.old_tlas.resize(d3d12::settings::num_back_buffers);\n\t\t}\n\n\t\tnamespace internal\n\t\t{\n\n\t\t\t//! Get a material id from a mesh.\n\t\t\tinline unsigned int ExtractMaterialFromMesh(ASBuildData& data, MaterialHandle material_handle)\n\t\t\t{\n\t\t\t\tstd::size_t material_id = 0;\n\t\t\t\tif (data.out_parsed_materials.find(material_handle.m_id) == data.out_parsed_materials.end())\n\t\t\t\t{\n\t\t\t\t\tmaterial_id = data.out_materials.size();\n\n\t\t\t\t\tauto* material_internal = material_handle.m_pool->GetMaterial(material_handle);\n\n\t\t\t\t\t// Build material\n\t\t\t\t\twr::temp::RayTracingMaterial_CBData material;\n\t\t\t\t\tmaterial.albedo_id = material_internal->GetTexture(wr::TextureType::ALBEDO).m_id;\n\t\t\t\t\tmaterial.normal_id = material_internal->GetTexture(wr::TextureType::NORMAL).m_id;\n\t\t\t\t\tmaterial.roughness_id = material_internal->GetTexture(wr::TextureType::ROUGHNESS).m_id;\n\t\t\t\t\tmaterial.metallicness_id = material_internal->GetTexture(wr::TextureType::METALLIC).m_id;\n\t\t\t\t\tmaterial.emissive_id = material_internal->GetTexture(wr::TextureType::EMISSIVE).m_id;\n\t\t\t\t\tmaterial.ao_id = material_internal->GetTexture(wr::TextureType::AO).m_id;\n\t\t\t\t\tmaterial.material_data = material_internal->GetMaterialData();\n\t\t\t\t\tdata.out_materials.push_back(material);\n\t\t\t\t\tdata.out_parsed_materials[material_handle.m_id] = material_id;\n\n\t\t\t\t\tdata.out_materials_require_update = true;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tmaterial_id = data.out_parsed_materials[material_handle.m_id];\n\t\t\t\t}\n\n\t\t\t\treturn static_cast<std::uint32_t>(material_id);\n\t\t\t}\n\n\t\t\t//! Add a data struct describing the mesh data offset and the material idx to `out_offsets`\n\t\t\tinline void AppendOffset(ASBuildData& data, wr::internal::D3D12MeshInternal* mesh, unsigned int material_id)\n\t\t\t{\n\t\t\t\twr::temp::RayTracingOffset_CBData offset;\n\t\t\t\toffset.material_idx = material_id;\n\t\t\t\toffset.idx_offset = static_cast<std::uint32_t>(mesh->m_index_staging_buffer_offset);\n\t\t\t\toffset.vertex_offset = static_cast<std::uint32_t>(mesh->m_vertex_staging_buffer_offset);\n\t\t\t\tdata.out_offsets.push_back(offset);\n\t\t\t}\n\n\t\t\tinline d3d12::desc::GeometryDesc CreateGeometryDescFromMesh(D3D12MeshInternal* mesh, d3d12::StagingBuffer* vb, d3d12::StagingBuffer* ib)\n\t\t\t{\n\t\t\t\td3d12::desc::GeometryDesc obj;\n\t\t\t\tobj.index_buffer = ib;\n\t\t\t\tobj.vertex_buffer = vb;\n\n\t\t\t\tobj.m_indices_offset = static_cast<std::uint32_t>(mesh->m_index_staging_buffer_offset);\n\t\t\t\tobj.m_num_indices = static_cast<std::uint32_t>(mesh->m_index_count);\n\t\t\t\tobj.m_vertices_offset = static_cast<std::uint32_t>(mesh->m_vertex_staging_buffer_offset);\n\t\t\t\tobj.m_num_vertices = static_cast<std::uint32_t>(mesh->m_vertex_count);\n\t\t\t\tobj.m_vertex_stride = static_cast<std::uint32_t>(mesh->m_vertex_staging_buffer_stride);\n\n\t\t\t\treturn obj;\n\t\t\t}\n\n\t\t\tinline void BuildBLASSingle(d3d12::Device* device, d3d12::CommandList* cmd_list, Model* model, std::pair<Mesh*, MaterialHandle> mesh_material, ASBuildData& data, std::uint32_t frame_idx)\n\t\t\t{\n\t\t\t\tauto n_model_pool = static_cast<D3D12ModelPool*>(model->m_model_pool);\n\t\t\t\tauto vb = n_model_pool->GetVertexStagingBuffer();\n\t\t\t\tauto ib = n_model_pool->GetIndexStagingBuffer();\n\n\t\t\t\td3d12::DescriptorHeap* out_heap = cmd_list->m_rt_descriptor_heap->GetHeap();\n\t\t\t\t\n\t\t\t\tdata.out_scene_ib = ib;\n\t\t\t\tdata.out_scene_vb = vb;\n\n\t\t\t\tauto n_mesh = static_cast<D3D12ModelPool*>(model->m_model_pool)->GetMeshData(mesh_material.first->id);\n\n\t\t\t\td3d12::desc::GeometryDesc obj = CreateGeometryDescFromMesh(n_mesh, vb, ib);\n\n\t\t\t\t// Build Bottom level BVH\n\t\t\t\tauto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj });\n\t\t\t\td3d12::UAVBarrierAS(cmd_list, blas, frame_idx);\n\t\t\t\td3d12::SetName(blas, L\"Bottom Level Acceleration Structure\");\n\n\t\t\t\tdata.blasses.insert({ mesh_material.first->id, blas });\n\t\t\t\t\n\t\t\t\tdata.out_material_handles.push_back(mesh_material.second); // Used to st eal the textures from the texture pool.\n\n\t\t\t\tauto material_id = ExtractMaterialFromMesh(data, mesh_material.second);\n\n\t\t\t\tAppendOffset(data, n_mesh, material_id);\n\t\t\t}\n\n\t\t\tinline void BuildBLASList(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, std::uint32_t frame_idx)\n\t\t\t{\n\t\t\t\tdata.out_materials.clear();\n\t\t\t\tdata.out_material_handles.clear();\n\t\t\t\tdata.out_offsets.clear();\n\t\t\t\tdata.out_parsed_materials.clear();\n\n\t\t\t\td3d12::DescriptorHeap* out_heap = cmd_list->m_rt_descriptor_heap->GetHeap();\n\n\t\t\t\tfor (auto it = data.blasses.begin(); it != data.blasses.end(); ++it)\n\t\t\t\t{\n\t\t\t\t\tdata.old_blasses[data.current_frame_index].push_back((*it).second);\n\t\t\t\t}\n\n\t\t\t\tdata.blasses.clear();\n\t\t\t\tdata.out_blas_list.clear();\n\n\t\t\t\tauto& batches = scene_graph.GetGlobalBatches();\n\t\t\t\tconst auto& batchInfo = scene_graph.GetBatches();\n\n\t\t\t\tunsigned int offset_id = 0;\n\n\t\t\t\tfor (auto& batch : batches)\n\t\t\t\t{\n\t\t\t\t\tauto model = batch.first.first;\n\t\t\t\t\tauto materials = batch.first.second;\n\t\t\t\t\tauto n_model_pool = static_cast<D3D12ModelPool*>(model->m_model_pool);\n\t\t\t\t\tauto vb = n_model_pool->GetVertexStagingBuffer();\n\t\t\t\t\tauto ib = n_model_pool->GetIndexStagingBuffer();\n\n\t\t\t\t\tdata.out_scene_ib = ib;\n\t\t\t\t\tdata.out_scene_vb = vb;\n\n\t\t\t\t\tfor (std::size_t mesh_i = 0; mesh_i < model->m_meshes.size(); mesh_i++)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto mesh = model->m_meshes[mesh_i];\n\t\t\t\t\t\tauto n_mesh = static_cast<D3D12ModelPool*>(model->m_model_pool)->GetMeshData(mesh.first->id);\n\n\t\t\t\t\t\tauto material_handle = mesh.second;\n\t\t\t\t\t\tif (materials.size() > mesh_i)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmaterial_handle = materials[mesh_i];\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\td3d12::desc::GeometryDesc obj = CreateGeometryDescFromMesh(n_mesh, vb, ib);\n\n\t\t\t\t\t\t// Build Bottom level BVH\n\t\t\t\t\t\tauto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj });\n\t\t\t\t\t\td3d12::UAVBarrierAS(cmd_list, blas, frame_idx);\n\t\t\t\t\t\td3d12::SetName(blas, L\"Bottom Level Acceleration Structure\");\n\n\t\t\t\t\t\tdata.blasses.insert({ mesh.first->id, blas });\n\n\t\t\t\t\t\tdata.out_material_handles.push_back(material_handle); // Used to st eal the textures from the texture pool.\n\n\t\t\t\t\t\tauto material_id = ExtractMaterialFromMesh(data, material_handle);\n\n\t\t\t\t\t\tAppendOffset(data, n_mesh, material_id);\n\n\t\t\t\t\t\tauto batch_it = batchInfo.find({ model, materials });\n\n\t\t\t\t\t\tassert(batch_it != batchInfo.end() && \"Batch was found in global array, but not in local\");\n\n\t\t\t\t\t\t// Push instances into a array for later use.\n\t\t\t\t\t\tfor (uint32_t i = 0U, j = (uint32_t)batch_it->second.num_global_instances; i < j; i++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tauto transform = batch.second[i].m_model;\n\n\t\t\t\t\t\t\tdata.out_blas_list.push_back({ blas, offset_id, transform });\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\toffset_id++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Make sure our gathered data isn't out of bounds.\n\t\t\t\tif (data.out_offsets.size() > d3d12::settings::num_max_rt_materials)\n\t\t\t\t{\n\t\t\t\t\tLOGE(\"There are to many offsets stored for ray tracing!\");\n\t\t\t\t}\n\t\t\t\tif (data.out_materials.size() > d3d12::settings::num_max_rt_materials)\n\t\t\t\t{\n\t\t\t\t\tLOGE(\"There are to many materials stored for ray tracing!\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tinline void CreateSRVs(ASBuildData& data)\n\t\t\t{\n\t\t\t\tfor (auto i = 0; i < d3d12::settings::num_back_buffers; i++)\n\t\t\t\t{\n\t\t\t\t\t// Create BYTE ADDRESS buffer view into a staging buffer. Hopefully this works.\n\t\t\t\t\t{\n\t\t\t\t\t\tauto cpu_handle = data.out_scene_ib_alloc.GetDescriptorHandle();\n\t\t\t\t\t\td3d12::CreateRawSRVFromStagingBuffer(data.out_scene_ib, cpu_handle, static_cast<std::uint32_t>(data.out_scene_ib->m_size / data.out_scene_ib->m_stride_in_bytes));\n\t\t\t\t\t}\n\n\t\t\t\t\t// Create material structured buffer view\n\t\t\t\t\t{\n\t\t\t\t\t\tauto cpu_handle = data.out_scene_mat_alloc.GetDescriptorHandle();\n\t\t\t\t\t\td3d12::CreateSRVFromStructuredBuffer(data.out_sb_material_handle->m_native, cpu_handle, 0);\n\t\t\t\t\t}\n\n\t\t\t\t\t// Create offset structured buffer view\n\t\t\t\t\t{\n\t\t\t\t\t\tauto cpu_handle = data.out_scene_offset_alloc.GetDescriptorHandle();\n\t\t\t\t\t\td3d12::CreateSRVFromStructuredBuffer(data.out_sb_offset_handle->m_native, cpu_handle, 0);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t/*inline bool ReconstructBLASsIfNeeded(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data)\n\t\t\t{\n\t\t\t\tauto batches = scene_graph.GetGlobalBatches();\n\t\t\t\tbool needs_reconstruction = false;\n\n\t\t\t\tstd::vector<D3D12ModelPool*> model_pools;\n\n\t\t\t\tfor (auto& batch : batches)\n\t\t\t\t{\n\t\t\t\t\tauto model = batch.first.first;\n\t\t\t\t\tauto materials = batch.first.second;\n\n\t\t\t\t\tbool model_pool_loaded = false;\n\n\t\t\t\t\tfor (int i = 0; i < model_pools.size(); ++i)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (model_pools[i] == model->m_model_pool)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmodel_pool_loaded = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!model_pool_loaded)\n\t\t\t\t\t{\n\t\t\t\t\t\tmodel_pools.push_back(static_cast<D3D12ModelPool*>(model->m_model_pool));\n\t\t\t\t\t\tif (static_cast<D3D12ModelPool*>(model->m_model_pool)->IsUpdated())\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tneeds_reconstruction = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (needs_reconstruction)\n\t\t\t\t{\n\t\t\t\t\t// Transition all model pools for accel structure creation\n\t\t\t\t\tfor (auto& pool : model_pools)\n\t\t\t\t\t{\n\t\t\t\t\t\td3d12::Transition(cmd_list, pool->GetVertexStagingBuffer(), ResourceState::VERTEX_AND_CONSTANT_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\t\td3d12::Transition(cmd_list, pool->GetIndexStagingBuffer(), ResourceState::INDEX_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\t}\n\n\t\t\t\t\tBuildBLASList(device, cmd_list, scene_graph, data);\n\n\t\t\t\t\tfor (auto& pool : model_pools)\n\t\t\t\t\t{\n\t\t\t\t\t\td3d12::Transition(cmd_list, pool->GetVertexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::VERTEX_AND_CONSTANT_BUFFER);\n\t\t\t\t\t\td3d12::Transition(cmd_list, pool->GetIndexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::INDEX_BUFFER);\n\t\t\t\t\t}\n\n\t\t\t\t\tCreateSRVs(data);\n\t\t\t\t}\n\n\t\t\t\treturn needs_reconstruction;\n\t\t\t}*/\n\n\t\t\tinline void UpdateTLAS(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, std::uint32_t frame_idx)\n\t\t\t{\n\t\t\t\tdata.out_materials.clear();\n\t\t\t\tdata.out_offsets.clear();\n\t\t\t\tdata.out_parsed_materials.clear();\n\n\t\t\t\td3d12::DescriptorHeap* out_heap = cmd_list->m_rt_descriptor_heap->GetHeap();\n\n\t\t\t\tauto& batches = scene_graph.GetGlobalBatches();\n\t\t\t\tconst auto& batchInfo = scene_graph.GetBatches();\n\n\t\t\t\tauto prev_size = data.out_blas_list.size();\n\t\t\t\tdata.out_blas_list.clear();\n\t\t\t\tdata.out_blas_list.reserve(prev_size);\n\n\t\t\t\tunsigned int offset_id = 0;\n\n\t\t\t\t//ReconstructBLASsIfNeeded(device, cmd_list, scene_graph, data);\n\n\t\t\t\t// Update transformations // TODO: This might be unnessessary if reconstrblasifneeded return true.\n\t\t\t\tfor (auto& batch : batches)\n\t\t\t\t{\n\t\t\t\t\tauto model = batch.first.first;\n\t\t\t\t\tauto materials = batch.first.second;\n\t\t\t\t\tauto n_model_pool = static_cast<D3D12ModelPool*>(model->m_model_pool);\n\n\t\t\t\t\tfor (std::size_t mesh_i = 0; mesh_i < model->m_meshes.size(); mesh_i++)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto mesh = model->m_meshes[mesh_i];\n\t\t\t\t\t\tauto n_mesh = static_cast<D3D12ModelPool*>(model->m_model_pool)->GetMeshData(mesh.first->id);\n\n\t\t\t\t\t\t// Pick the standard material or if available a user defined material.\n\t\t\t\t\t\tauto material_handle = mesh.second;\n\t\t\t\t\t\tif (materials.size() > mesh_i)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tmaterial_handle = materials[mesh_i];\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\tstd::unordered_map<uint64_t, d3d12::AccelerationStructure>::iterator blas_iterator = data.blasses.find(mesh.first->id);\n\n\t\t\t\t\t\tif (blas_iterator == data.blasses.end() || n_mesh->data_changed)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// Check if data changed in the mesh and if the blas iterator isn't an end iterator,\n\t\t\t\t\t\t\t// since it will be dereferenced (end iterators can't be dereferenced).\n\t\t\t\t\t\t\tif (n_mesh->data_changed && blas_iterator != data.blasses.end())\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tdata.old_blasses[data.current_frame_index].push_back((*blas_iterator).second);\n\t\t\t\t\t\t\t\tdata.blasses.erase(blas_iterator);\n\t\t\t\t\t\t\t\tn_mesh->data_changed = false;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\td3d12::Transition(cmd_list, n_model_pool->GetVertexStagingBuffer(), ResourceState::VERTEX_AND_CONSTANT_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\t\t\td3d12::Transition(cmd_list, n_model_pool->GetIndexStagingBuffer(), ResourceState::INDEX_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t\t\t\t\tBuildBLASSingle(device, cmd_list, model, { mesh.first, material_handle }, data, frame_idx);\n\n\t\t\t\t\t\t\td3d12::Transition(cmd_list, n_model_pool->GetVertexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::VERTEX_AND_CONSTANT_BUFFER);\n\t\t\t\t\t\t\td3d12::Transition(cmd_list, n_model_pool->GetIndexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::INDEX_BUFFER);\n\n\t\t\t\t\t\t\tCreateSRVs(data);\n\n\t\t\t\t\t\t\tblas_iterator = data.blasses.find(mesh.first->id);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tauto blas = (*blas_iterator).second;\n\n\t\t\t\t\t\tauto material_id = ExtractMaterialFromMesh(data, material_handle);\n\n\t\t\t\t\t\tAppendOffset(data, n_mesh, material_id);\n\n\t\t\t\t\t\tauto it = batchInfo.find({ model, materials });\n\n\t\t\t\t\t\tassert(it != batchInfo.end() && \"Batch was found in global array, but not in local\");\n\n\t\t\t\t\t\t// Push instances into a array for later use.\n\t\t\t\t\t\tfor (uint32_t i = 0U, j = (uint32_t)it->second.num_global_instances; i < j; i++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tauto transform = batch.second[i].m_model;\n\n\t\t\t\t\t\t\tdata.out_blas_list.push_back({ blas, offset_id, transform });\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\toffset_id++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\td3d12::AccelerationStructure old_accel = data.out_tlas;\n\t\t\t\td3d12::UpdateTopLevelAccelerationStructure(data.out_tlas, device, cmd_list, out_heap, data.out_blas_list, frame_idx);\n\n\t\t\t\tif (old_accel.m_scratch != data.out_tlas.m_scratch &&\n\t\t\t\t\told_accel.m_natives[frame_idx] != data.out_tlas.m_natives[frame_idx] &&\n\t\t\t\t\told_accel.m_instance_descs[frame_idx] != data.out_tlas.m_instance_descs[frame_idx])\n\t\t\t\t{\n\t\t\t\t\tdata.old_tlas[data.current_frame_index] = old_accel;\n\t\t\t\t}\n\t\t\t}\n\n\t\t} /* internal */\n\n\t\tinline void ExecuteBuildASTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& data = fg.GetData<ASBuildData>(handle);\n\t\t\tauto settings = fg.GetSettings<ASBuildSettings>(handle);\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto device = n_render_system.m_device;\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\tdata.out_materials_require_update = false;\n\n\t\t\tdata.current_frame_index = n_render_system.GetFrameIdx();\n\n\t\t\td3d12::DescriptorHeap* out_heap = cmd_list->m_rt_descriptor_heap->GetHeap();\n\n\t\t\t// Initialize requirements\n\t\t\tif (data.out_init)\n\t\t\t{\n\t\t\t\tstd::vector<std::shared_ptr<D3D12ModelPool>> model_pools = n_render_system.m_model_pools;\n\t\t\t\t// Transition all model pools for accel structure creation\n\t\t\t\tfor (auto& pool : model_pools)\n\t\t\t\t{\n\t\t\t\t\td3d12::Transition(cmd_list, pool->GetVertexStagingBuffer(), ResourceState::VERTEX_AND_CONSTANT_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\td3d12::Transition(cmd_list, pool->GetIndexStagingBuffer(), ResourceState::INDEX_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t}\n\n\t\t\t\t// List all materials used by meshes\n\t\t\t\tinternal::BuildBLASList(device, cmd_list, scene_graph, data, frame_idx);\n\n\t\t\t\tdata.out_tlas = d3d12::CreateTopLevelAccelerationStructure(device, cmd_list, out_heap, data.out_blas_list);\n\t\t\t\td3d12::SetName(data.out_tlas, L\"Top Level Acceleration Structure\");\n\n\t\t\t\t// Transition all model pools back to whatever they were.\n\t\t\t\tfor (auto& pool : model_pools)\n\t\t\t\t{\n\t\t\t\t\td3d12::Transition(cmd_list, pool->GetVertexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::VERTEX_AND_CONSTANT_BUFFER);\n\t\t\t\t\td3d12::Transition(cmd_list, pool->GetIndexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::INDEX_BUFFER);\n\t\t\t\t}\n\n\t\t\t\tif (!data.out_blas_list.empty())\n\t\t\t\t{\n\t\t\t\t\tinternal::CreateSRVs(data);\n\t\t\t\t}\n\n\t\t\t\tdata.out_init = false;\n\t\t\t}\n\t\t\telse if (!settings.m_runtime.m_rebuild_as)\n\t\t\t{\n\t\t\t\tif (data.current_frame_index != data.previous_frame_index)\n\t\t\t\t{\n\t\t\t\t\tfor (int i = 0; i < data.old_blasses[data.current_frame_index].size(); ++i)\n\t\t\t\t\t{\n\t\t\t\t\t\td3d12::DestroyAccelerationStructure(data.old_blasses[data.current_frame_index][i]);\n\t\t\t\t\t}\n\t\t\t\t\tdata.old_blasses[data.current_frame_index].clear();\n\n\t\t\t\t\td3d12::DestroyAccelerationStructure(data.old_tlas[data.current_frame_index]);\n\n\t\t\t\t\tdata.old_tlas[data.current_frame_index] = {};\n\t\t\t\t}\n\n\t\t\t\tinternal::UpdateTLAS(device, cmd_list, scene_graph, data, frame_idx);\n\t\t\t}\n\n\t\t\tdata.previous_frame_index = n_render_system.GetFrameIdx();\n\t\t}\n\n\t\tinline void DestroyBuildASTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddBuildAccelerationStructuresTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ Format::UNKNOWN }),\n\t\t\tRenderTargetProperties::NumRTVFormats(0),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupBuildASTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteBuildASTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyBuildASTask(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<ASBuildData>(desc, L\"Acceleration Structure Builder\");\n\t\tframe_graph.UpdateSettings<ASBuildData>(ASBuildSettings());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_cubemap_convolution.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_defines.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_model_pool.hpp\"\n#include \"../d3d12/d3d12_resource_pool_texture.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../scene_graph/skybox_node.hpp\"\n#include \"../pipeline_registry.hpp\"\n\n#include \"../platform_independend_structs.hpp\"\n#include \"d3d12_imgui_render_task.hpp\"\n#include \"d3d12_equirect_to_cubemap.hpp\"\n\nnamespace wr\n{\n\tstruct CubemapConvolutionSettings\n\t{\n\t\tstruct Runtime\n\t\t{\n\t\t\tint m_resolution[2] = {128, 128};\n\t\t};\n\n\t\tRuntime m_runtime;\n\t};\n\n\tstruct CubemapConvolutionTaskData\n\t{\n\t\td3d12::PipelineState* in_pipeline = nullptr;\n\n\t\tTextureHandle in_radiance = {};\n\t\tTextureHandle out_irradiance = {};\n\n\t\tstd::shared_ptr<ConstantBufferPool> camera_cb_pool;\n\t\tD3D12ConstantBufferHandle* cb_handle;\n\n\t\tDirectX::XMMATRIX proj_mat = { DirectX::XMMatrixIdentity() };\n\t\tDirectX::XMMATRIX view_mat[6] = { };\n\t};\n\n\tnamespace internal\n\t{\n\t\tstruct ProjectionView_CBuffer\n\t\t{\n\t\t\tDirectX::XMMATRIX m_projection;\n\t\t\tDirectX::XMMATRIX m_view[6];\n\t\t};\n\n\t\tinline void SetupCubemapConvolutionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (resize)\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tauto& data = fg.GetData<CubemapConvolutionTaskData>(handle);\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::cubemap_convolution);\n\n\t\t\tdata.camera_cb_pool = rs.CreateConstantBufferPool(2_mb);\n\t\t\tdata.cb_handle = static_cast<D3D12ConstantBufferHandle*>(data.camera_cb_pool->Create(sizeof(ProjectionView_CBuffer)));\n\n\t\t\tdata.proj_mat = DirectX::XMMatrixPerspectiveFovRH(DirectX::XMConvertToRadians(90.0f), 1.0f, 0.1f, 10.0f);\n\n\t\t\tdata.view_mat[0] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(1.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f));\n\n\t\t\tdata.view_mat[1] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(-1.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f));\n\n\t\t\tdata.view_mat[2] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f));\n\n\t\t\tdata.view_mat[3] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f));\n\n\t\t\tdata.view_mat[4] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f));\n\n\t\t\tdata.view_mat[5] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 0.0f, -1.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f));\n\t\t}\n\n\t\tinline void ExecuteCubemapConvolutionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<CubemapConvolutionTaskData>(handle);\n\t\t\tauto settings = fg.GetSettings<CubemapConvolutionSettings>(handle);\n\n\t\t\tauto& pred_data = fg.GetPredecessorData<EquirectToCubemapTaskData>();\n\n\t\t\tauto skybox_node = scene_graph.GetCurrentSkybox();\n\t\t\tif (!skybox_node)\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t//Does it need convolution? And does it have a cubemap already?\n\t\t\tif (skybox_node->m_irradiance != std::nullopt && skybox_node->m_skybox != std::nullopt)\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tdata.in_radiance = pred_data.out_cubemap;\n\n\t\t\tskybox_node->m_irradiance = skybox_node->m_skybox.value().m_pool->CreateCubemap(\"ConvolutedMap\", settings.m_runtime.m_resolution[0], settings.m_runtime.m_resolution[1], 1, wr::Format::R16G16B16A16_FLOAT, true);;\n\n\t\t\tdata.out_irradiance = skybox_node->m_irradiance.value();\n\n\t\t\td3d12::TextureResource* radiance = static_cast<d3d12::TextureResource*>(data.in_radiance.m_pool->GetTextureResource(data.in_radiance));\n\t\t\td3d12::TextureResource* irradiance = static_cast<d3d12::TextureResource*>(data.out_irradiance.m_pool->GetTextureResource(data.out_irradiance));\n\n\t\t\tif (radiance->m_is_staged)\n\t\t\t{\n\t\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t\t{\n\t\t\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\t\t\tconst auto viewport = d3d12::CreateViewport(static_cast<int>(irradiance->m_width), static_cast<int>(irradiance->m_height));\n\t\t\t\t\tconst auto frame_idx = n_render_system.GetRenderWindow()->m_frame_idx;\n\n\t\t\t\t\td3d12::BindViewport(cmd_list, viewport);\n\t\t\t\t\td3d12::BindPipeline(cmd_list, data.in_pipeline);\n\t\t\t\t\td3d12::SetPrimitiveTopology(cmd_list, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);\n\n\t\t\t\t\tProjectionView_CBuffer cb_data;\n\t\t\t\t\tcb_data.m_view[0] = data.view_mat[0];\n\t\t\t\t\tcb_data.m_view[1] = data.view_mat[1];\n\t\t\t\t\tcb_data.m_view[2] = data.view_mat[2];\n\t\t\t\t\tcb_data.m_view[3] = data.view_mat[3];\n\t\t\t\t\tcb_data.m_view[4] = data.view_mat[4];\n\t\t\t\t\tcb_data.m_view[5] = data.view_mat[5];\n\t\t\t\t\tcb_data.m_projection = data.proj_mat;\n\n\t\t\t\t\tdata.cb_handle->m_pool->Update(data.cb_handle, sizeof(ProjectionView_CBuffer), 0, frame_idx, (uint8_t*)&cb_data);\n\n\t\t\t\t\td3d12::BindConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx);\n\n\t\t\t\t\tfor (uint32_t i = 0; i < 6; ++i)\n\t\t\t\t\t{\n\t\t\t\t\t\t//Get render target handle.\n\t\t\t\t\t\td3d12::DescHeapCPUHandle rtv_handle = irradiance->m_rtv_allocation->GetDescriptorHandle(i);\n\n\t\t\t\t\t\tcmd_list->m_native->OMSetRenderTargets(1, &rtv_handle.m_native, false, nullptr);\n\n\t\t\t\t\t\td3d12::Bind32BitConstants(cmd_list, &i, 1, 0, 0);\n\n\t\t\t\t\t\t//bind cube and render\n\t\t\t\t\t\tModel* cube_model = rs.GetSimpleShape(RenderSystem::SimpleShapes::CUBE);\n\n\t\t\t\t\t\t//Render meshes\n\t\t\t\t\t\tfor (auto& mesh : cube_model->m_meshes)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tauto n_mesh = static_cast<D3D12ModelPool*>(cube_model->m_model_pool)->GetMeshData(mesh.first->id);\n\n\t\t\t\t\t\t\tD3D12ModelPool* model_pool = static_cast<D3D12ModelPool*>(cube_model->m_model_pool);\n\n\t\t\t\t\t\t\td3d12::BindVertexBuffer(cmd_list, static_cast<D3D12ModelPool*>(cube_model->m_model_pool)->GetVertexStagingBuffer(),\n\t\t\t\t\t\t\t\t0, model_pool->GetVertexStagingBuffer()->m_size,\n\t\t\t\t\t\t\t\tn_mesh->m_vertex_staging_buffer_stride);\n\n\t\t\t\t\t\t\td3d12::BindIndexBuffer(cmd_list, static_cast<D3D12ModelPool*>(cube_model->m_model_pool)->GetIndexStagingBuffer(),\n\t\t\t\t\t\t\t\t0, static_cast<std::uint32_t>(model_pool->GetIndexStagingBuffer()->m_size));\n\n\t\t\t\t\t\t\tconstexpr unsigned int env_idx = rs_layout::GetHeapLoc(params::cubemap_convolution, params::CubemapConvolutionE::ENVIRONMENT_CUBEMAP);\n\t\t\t\t\t\t\td3d12::SetShaderSRV(cmd_list, 2, env_idx, radiance);\n\t\t\t\t\t\t\td3d12::BindDescriptorHeaps(cmd_list);\n\n\t\t\t\t\t\t\tif (n_mesh->m_index_count != 0)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\td3d12::DrawIndexed(cmd_list, static_cast<std::uint32_t>(n_mesh->m_index_count), 1, static_cast<std::uint32_t>(n_mesh->m_index_staging_buffer_offset), static_cast<std::uint32_t>(n_mesh->m_vertex_staging_buffer_offset));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\td3d12::Draw(cmd_list, static_cast<std::uint32_t>(n_mesh->m_vertex_count), 1, static_cast<std::uint32_t>(n_mesh->m_vertex_staging_buffer_offset));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\td3d12::Transition(cmd_list, irradiance, irradiance->m_subresource_states[0], ResourceState::PIXEL_SHADER_RESOURCE);\n\n\t\t\t\t\t//Once we're done we can mark the equirectangular texture for deletion in the next frame as it won't be used anymore\n\t\t\t\t\t//Mark for unload makes the m_hdr handle invalid, if it's used anywhere else the program will probably break.\n\t\t\t\t\t//If users want to keep using the equirectangular texture afterward, the following line of code can be removed.\n\t\t\t\t\tskybox_node->m_hdr.m_pool->MarkForUnload(skybox_node->m_hdr, frame_idx);\n\n\t\t\t\t\tfg.SetShouldExecute(handle, false);\n\t\t\t\t}\n\t\t\t}\n\t\t\t//if (data.should_run)\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddCubemapConvolutionTask(FrameGraph& fg)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::NON_PIXEL_SHADER_RESOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(true),\n\t\t\tRenderTargetProperties::DSVFormat(Format::D32_FLOAT),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(true),\n\t\t\tRenderTargetProperties::ClearDepth(true),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [&](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupCubemapConvolutionTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteCubemapConvolutionTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::DIRECT;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tfg.AddTask<CubemapConvolutionTaskData>(desc, L\"Cubemap Convolution\");\n\t\tfg.UpdateSettings<CubemapConvolutionTaskData>(CubemapConvolutionSettings());\n\t}\n\n} /* wr */\n\n\n/*\nOLD COMPUTE CODE, KEEPING IT HERE FOR POSSIBLE FUTURE USAGE.\n*/\n/*\n\t\t\tstruct CubemapConvolutionTaskData\n\t{\n\t\tD3D12Pipeline* in_pipeline;\n\n\t\td3d12::TextureResource* in_radiance; //The skybox hdr cubemap\n\t\td3d12::TextureResource* out_irradiance;\n\n\t\tbool should_run = true;\n\t};\n\n\tnamespace internal\n\t{\n\t\tinline void SetupCubemapConvolutionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, TextureHandle in_radiance, TextureHandle out_irradiance)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<CubemapConvolutionTaskData>(handle);\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.in_pipeline = (D3D12Pipeline*)ps_registry.Find(pipelines::cubemap_convolution);\n\n\t\t\tdata.in_radiance = static_cast<d3d12::TextureResource*>(in_radiance.m_pool->GetTexture(in_radiance.m_id));\n\t\t\tdata.out_irradiance = static_cast<d3d12::TextureResource*>(out_irradiance.m_pool->GetTexture(out_irradiance.m_id));\n\n\t\t\tD3D12TexturePool* pool = static_cast<D3D12TexturePool*>(out_irradiance.m_pool);\n\n\t\t\td3d12::DescHeapCPUHandle uav_handle = data.out_irradiance->m_uav_allocation.GetDescriptorHandle();\n\n\t\t\td3d12::CreateUAVFromTexture(data.out_irradiance, uav_handle, 0);\n\t\t}\n\n\t\tinline void ExecuteCubemapConvolutionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<CubemapConvolutionTaskData>(handle);\n\n\t\t\tif (data.should_run)\n\t\t\t{\n\t\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t\t{\n\t\t\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\t\t\tconst auto frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\t\t\td3d12::BindComputePipeline(cmd_list, data.in_pipeline->m_native);\n\n\t\t\t\t\t//Transition and bind SRV\n\t\t\t\t\td3d12::Transition(cmd_list, data.in_radiance, data.in_radiance->m_current_state, ResourceState::PIXEL_SHADER_RESOURCE);\n\t\t\t\t\td3d12::SetShaderTexture(cmd_list, 0, 0, data.in_radiance);\n\n\t\t\t\t\t//Transition and bind UAV\n\t\t\t\t\td3d12::Transition(cmd_list, data.out_irradiance, data.out_irradiance->m_current_state, ResourceState::UNORDERED_ACCESS);\n\t\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, 1, data.out_irradiance);\n\n\t\t\t\t\td3d12::BindDescriptorHeaps(cmd_list, frame_idx);\n\n\t\t\t\t\td3d12::Dispatch(cmd_list, data.out_irradiance->m_width/32, data.out_irradiance->m_height/32, 6);\n\n\t\t\t\t\td3d12::Transition(cmd_list, data.out_irradiance, data.out_irradiance->m_current_state, ResourceState::PIXEL_SHADER_RESOURCE);\n\n\t\t\t\t\tdata.should_run = false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t}\n\t*/"
  },
  {
    "path": "src/render_tasks/d3d12_deferred_composition.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include \"../render_tasks/d3d12_deferred_composition.hpp\"\n\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../scene_graph/skybox_node.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"../render_tasks/d3d12_brdf_lut_precalculation.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_spatial_reconstruction.hpp\"\n#include \"../render_tasks/d3d12_reflection_denoiser.hpp\"\n#include \"../render_tasks/d3d12_cubemap_convolution.hpp\"\n#include \"../render_tasks/d3d12_rt_shadow_task.hpp\"\n#include \"../render_tasks/d3d12_rt_reflection_task.hpp\"\n#include \"../render_tasks/d3d12_shadow_denoiser_task.hpp\"\n#include \"../render_tasks/d3d12_path_tracer.hpp\"\n#include \"../render_tasks/d3d12_accumulation.hpp\"\n#include \"../render_tasks/d3d12_rtao_task.hpp\"\n#include \"d3d12_hbao.hpp\"\n\nnamespace wr\n{\n\tnamespace internal\n\t{\n\n\t\tvoid RecordDrawCommands(D3D12RenderSystem& render_system, d3d12::CommandList* cmd_list, d3d12::HeapResource* camera_cb, wr::DeferredCompositionTaskData const& data, unsigned int frame_idx)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(render_system);\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.in_pipeline);\n\n\t\t\tbool is_fallback = d3d12::GetRaytracingType(render_system.m_device) == RaytracingType::FALLBACK;\n\t\t\td3d12::BindDescriptorHeaps(cmd_list, is_fallback);\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, camera_cb, 0, frame_idx);\n\n\t\t\tconstexpr unsigned int albedo_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::GBUFFER_ALBEDO_ROUGHNESS);\n\t\t\td3d12::DescHeapCPUHandle albedo_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle(frame_idx);\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, albedo_loc, albedo_handle);\n\n\t\t\tconstexpr unsigned int normal_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::GBUFFER_NORMAL_METALLIC);\n\t\t\td3d12::DescHeapCPUHandle normal_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(frame_idx);\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, normal_loc, normal_handle);\n\n\t\t\tconstexpr unsigned int emissive_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::GBUFFER_EMISSIVE_AO);\n\t\t\td3d12::DescHeapCPUHandle emissive_handle = data.out_gbuffer_emissive_alloc.GetDescriptorHandle(frame_idx);\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, emissive_loc, emissive_handle);\n\n\t\t\tconstexpr unsigned int depth_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::GBUFFER_DEPTH);\n\t\t\td3d12::DescHeapCPUHandle depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle();\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, depth_loc, depth_handle);\n\n\t\t\tconstexpr unsigned int lights_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::LIGHT_BUFFER);\n\t\t\td3d12::DescHeapCPUHandle lights_handle = data.out_lights_alloc.GetDescriptorHandle();\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, lights_loc, lights_handle);\n\n\t\t\tconstexpr unsigned int skybox = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::SKY_BOX);\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, skybox, data.out_skybox);\n\n\t\t\tconstexpr unsigned int irradiance = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::IRRADIANCE_MAP);\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, irradiance, data.out_irradiance);\n\n\t\t\tconstexpr unsigned int pref_env = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::PREF_ENV_MAP);\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, pref_env, data.out_pref_env_map);\n\n\t\t\tconstexpr unsigned int brdf_lut_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BRDF_LUT);\n\t\t\tauto* brdf_lut_text = static_cast<d3d12::TextureResource*>(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value()));\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, brdf_lut_loc, brdf_lut_text);\n\n\t\t\tconstexpr unsigned int reflection = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_REFLECTION);\n\t\t\td3d12::DescHeapCPUHandle reflection_handle = data.out_buffer_refl_alloc.GetDescriptorHandle();\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, reflection, reflection_handle);\n\t\t\t\n\t\t\tconstexpr unsigned int shadow = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_SHADOW);\n\t\t\td3d12::DescHeapCPUHandle shadow_handle = data.out_buffer_shadow_alloc.GetDescriptorHandle();\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, shadow, shadow_handle);\n\n\t\t\tconstexpr unsigned int sp_irradiance_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_SCREEN_SPACE_IRRADIANCE);\n\t\t\td3d12::DescHeapCPUHandle sp_irradiance_handle = data.out_screen_space_irradiance_alloc.GetDescriptorHandle();\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, sp_irradiance_loc, sp_irradiance_handle);\n\n\t\t\tconstexpr unsigned int ao_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_AO);\n\t\t\td3d12::DescHeapCPUHandle ao_handle = data.out_screen_space_ao_alloc.GetDescriptorHandle();\n\t\t\td3d12::SetShaderSRV(cmd_list, 1, ao_loc, ao_handle);\n\n\t\t\tconstexpr unsigned int output_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::OUTPUT);\n\t\t\td3d12::DescHeapCPUHandle output_handle = data.out_output_alloc.GetDescriptorHandle();\n\t\t\td3d12::SetShaderUAV(cmd_list, 1, output_loc, output_handle);\n\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tvoid SetupDeferredCompositionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DeferredCompositionTaskData>(handle);\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::deferred_composition);\n\n\t\t\t// Check if the current frame graph contains the hybrid task to know if it is hybrid or not.\n\t\t\tdata.is_path_tracer = fg.HasTask<wr::PathTracerData>();\n\t\t\tdata.is_rtao = fg.HasTask<wr::RTAOData>();\n\t\t\tdata.is_hbao = fg.HasTask<wr::HBAOData>() && !data.is_rtao; //Don't use HBAO when RTAO is active\n\t\t\tdata.is_hybrid = fg.HasTask<wr::RTShadowData>() || fg.HasTask<wr::RTReflectionData>() || fg.HasTask<wr::ShadowDenoiserData>();\n\t\t\tdata.has_rt_shadows = fg.HasTask<wr::RTShadowData>();\n\t\t\tdata.has_rt_reflection = fg.HasTask<wr::RTReflectionData>();\n\n\t\t\t//Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors\n\t\t\tstd::shared_ptr<D3D12TexturePool> texture_pool = std::static_pointer_cast<D3D12TexturePool>(n_render_system.m_texture_pools[0]);\n\t\t\tif (!texture_pool)\n\t\t\t{\n\t\t\t\tLOGC(\"Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool\");\n\t\t\t}\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\n\t\t\t\tdata.out_gbuffer_albedo_alloc = std::move(data.out_allocator->Allocate(d3d12::settings::num_back_buffers));\n\t\t\t\tdata.out_gbuffer_normal_alloc = std::move(data.out_allocator->Allocate(d3d12::settings::num_back_buffers));\n\t\t\t\tdata.out_gbuffer_emissive_alloc = std::move(data.out_allocator->Allocate(d3d12::settings::num_back_buffers));\n\t\t\t\tdata.out_gbuffer_depth_alloc = std::move(data.out_allocator->Allocate());\n\t\t\t\tdata.out_lights_alloc = std::move(data.out_allocator->Allocate());\n\t\t\t\tdata.out_buffer_refl_alloc = std::move(data.out_allocator->Allocate());\n\t\t\t\tdata.out_buffer_shadow_alloc = std::move(data.out_allocator->Allocate());\n\t\t\t\tdata.out_screen_space_irradiance_alloc = std::move(data.out_allocator->Allocate());\n\t\t\t\tdata.out_screen_space_ao_alloc = std::move(data.out_allocator->Allocate());\n\t\t\t\tdata.out_output_alloc = std::move(data.out_allocator->Allocate());\n\t\t\t}\n\n\t\t\tfor (uint32_t i = 0; i < d3d12::settings::num_back_buffers; ++i)\n\t\t\t{\n\t\t\t\tauto albedo_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle(i);\n\t\t\t\tauto normal_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(i);\n\t\t\t\tauto emissive_handle = data.out_gbuffer_emissive_alloc.GetDescriptorHandle(i);\n\t\t\t\tauto depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle();\n\n\t\t\t\tauto deferred_main_rt = data.out_deferred_main_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<DeferredMainTaskData>());\n\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, albedo_handle, 0, deferred_main_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats[1]);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, emissive_handle, 2, deferred_main_rt->m_create_info.m_rtv_formats[2]);\n\t\t\t\t\n\t\t\t\td3d12::CreateSRVFromDSV(deferred_main_rt, depth_handle);\n\n\t\t\t\t// Bind output(s) from hybrid render task, if the composition task is executed in the hybrid frame graph\n\t\t\t\tif (data.is_hybrid)\n\t\t\t\t{\n\t\t\t\t\tconstexpr auto reflection_id = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_REFLECTION);\n\t\t\t\t\tauto reflection_handle = data.out_buffer_refl_alloc.GetDescriptorHandle();\n\t\t\t\t\t\n\t\t\t\t\tif (fg.HasTask<wr::ReflectionDenoiserData>())\n\t\t\t\t\t{\n\t\t\t\t\t\tauto reflection_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::ReflectionDenoiserData>());\n\t\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(reflection_rt, reflection_handle, 0, reflection_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t\t}\n\t\t\t\t\tif (fg.HasTask<wr::SpatialReconstructionData>())\n\t\t\t\t\t{\n\t\t\t\t\t\tauto reflection_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::SpatialReconstructionData>());\n\t\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(reflection_rt, reflection_handle, 0, reflection_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t\t}\n\t\t\t\t\telse if (data.has_rt_reflection)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto reflection_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::RTReflectionData>());\n\t\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(reflection_rt, reflection_handle, 0, reflection_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t\t}\n\t\t\t\t\telse if(data.has_rt_shadows)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto reflection_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::RTShadowData>());\n\t\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(reflection_rt, reflection_handle, 0, reflection_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t\t}\n\n\t\t\t\t\tconstexpr auto shadow_id = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_SHADOW);\n\t\t\t\t\tauto shadow_handle = data.out_buffer_shadow_alloc.GetDescriptorHandle();\n\n\t\t\t\t\tif (fg.HasTask<wr::ShadowDenoiserData>())\n\t\t\t\t\t{\n\t\t\t\t\t\tauto shadow_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::ShadowDenoiserData>());\n\t\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(shadow_rt, shadow_handle, 0, shadow_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t\t\tdata.has_rt_shadows_denoiser = true;\n\t\t\t\t\t}\n\t\t\t\t\telse if(fg.HasTask<wr::RTShadowData>())\n\t\t\t\t\t{\n\t\t\t\t\t\tauto shadow_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::RTShadowData>());\n\t\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(shadow_rt, shadow_handle, 0, shadow_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t\t}\n\t\t\t\t\telse if(data.has_rt_reflection)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto shadow_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::RTReflectionData>());\n\t\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(shadow_rt, shadow_handle, 0, shadow_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t\t}\n\t\t\t\t\tif (data.is_rtao)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto ao_handle = data.out_screen_space_ao_alloc.GetDescriptorHandle();\n\n\t\t\t\t\t\tauto ao_buffer = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::RTAOData>());\n\t\t\t\t\t\td3d12::CreateSRVFromRTV(ao_buffer, ao_handle, 1, ao_buffer->m_create_info.m_rtv_formats.data());\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tvoid ExecuteDeferredCompositionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DeferredCompositionTaskData>(handle);\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tauto render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tfg.WaitForPredecessorTask<CubemapConvolutionTaskData>();\n      \n\t\t\tif (data.is_hybrid)\n\t\t\t{\n\t\t\t\tif (data.has_rt_reflection)\n\t\t\t\t{\n\t\t\t\t\t// Wait on rt reflection task\n\t\t\t\t\tconst auto& reflection_data = fg.GetPredecessorData<RTReflectionData>();\n\t\t\t\t}\n\t\t\t\tif (data.has_rt_shadows)\n\t\t\t\t{\n\t\t\t\t\t// Wait on rt shadow task\n\t\t\t\t\tconst auto& shadow_data = fg.GetPredecessorData<RTShadowData>();\n\t\t\t\t}\n\t\t\t\tif (data.has_rt_shadows_denoiser)\n\t\t\t\t{\n\t\t\t\t\t// Wait on shadow denoiser task\n\t\t\t\t\tconst auto& denoiser_data = fg.GetPredecessorData<ShadowDenoiserData>();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t{\n\t\t\t\tconst auto viewport = n_render_system.m_viewport;\n\t\t\t\tconst auto frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\t\t// Update camera constant buffer pool\n\t\t\t\tauto active_camera = scene_graph.GetActiveCamera();\n\n\t\t\t\ttemp::ProjectionView_CBData camera_data{};\n\t\t\t\tcamera_data.m_projection = active_camera->m_projection;\n\t\t\t\tcamera_data.m_inverse_projection = active_camera->m_inverse_projection;\n\t\t\t\tcamera_data.m_prev_projection = active_camera->m_prev_projection;\n\t\t\t\tcamera_data.m_view = active_camera->m_view;\n\t\t\t\tcamera_data.m_inverse_view = active_camera->m_inverse_view;\n\t\t\t\tcamera_data.m_prev_view = active_camera->m_prev_view;\n\t\t\t\tcamera_data.m_is_hybrid = data.is_hybrid;\n\t\t\t\tcamera_data.m_has_reflections = data.has_rt_reflection;\n\t\t\t\tcamera_data.m_has_shadows = data.has_rt_shadows || data.has_rt_shadows_denoiser;\n\t\t\t\tcamera_data.m_is_path_tracer = data.is_path_tracer;\n\t\t\t\tif (data.is_rtao)\n\t\t\t\t{\n\t\t\t\t\tcamera_data.m_is_ao = true;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n#ifdef NVIDIA_GAMEWORKS_HBAO\n\t\t\t\t\tcamera_data.m_is_ao = data.is_hbao;\n#else\n\t\t\t\t\tcamera_data.m_is_ao = false;\n#endif\n\t\t\t\t}\n\t\t\t\tactive_camera->m_camera_cb->m_pool->Update(active_camera->m_camera_cb, sizeof(temp::ProjectionView_CBData), 0, (uint8_t*)&camera_data);\n\t\t\t\tconst auto camera_cb = static_cast<D3D12ConstantBufferHandle*>(active_camera->m_camera_cb);\n\n\t\t\t\tif (static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE)\n\t\t\t\t{\n\t\t\t\t\tstatic_cast<D3D12StructuredBufferPool*>(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t//Get light buffer\n\t\t\t\t{\n\t\t\t\t\td3d12::DescHeapCPUHandle srv_struct_buffer_handle = data.out_lights_alloc.GetDescriptorHandle();\n\t\t\t\t\td3d12::CreateSRVFromStructuredBuffer(static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native, srv_struct_buffer_handle, frame_idx);\n\t\t\t\t}\n\n\t\t\t\t//GetSkybox\n\t\t\t\tauto skybox = scene_graph.GetCurrentSkybox();\n\t\t\t\tif (skybox)\n\t\t\t\t{\n\t\t\t\t\t//data.out_skybox = static_cast<wr::d3d12::TextureResource*>(pred_data.in_radiance.m_pool->GetTexture(pred_data.in_radiance.m_id));\n\t\t\t\t\tdata.out_skybox = static_cast<wr::d3d12::TextureResource*>(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value()));\n\t\t\t\t\td3d12::CreateSRVFromTexture(data.out_skybox);\n\n\t\t\t\t\tdata.out_irradiance = static_cast<wr::d3d12::TextureResource*>(skybox->m_irradiance->m_pool->GetTextureResource(skybox->m_irradiance.value()));\n\t\t\t\t\td3d12::CreateSRVFromTexture(data.out_irradiance);\n\n\t\t\t\t\tdata.out_pref_env_map = static_cast<wr::d3d12::TextureResource*>(skybox->m_prefiltered_env_map->m_pool->GetTextureResource(skybox->m_prefiltered_env_map.value()));\n\t\t\t\t\td3d12::CreateSRVFromTexture(data.out_pref_env_map);\n\t\t\t\t}\n\n\t\t\t\t// Get Screen Space Environment Texture\n\t\t\t\tif (data.is_path_tracer)\n\t\t\t\t{\n\t\t\t\t\td3d12::DescHeapCPUHandle irradiance_handle = data.out_screen_space_irradiance_alloc.GetDescriptorHandle();\n\t\t\t\t\td3d12::RenderTarget* pred_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::AccumulationData>());\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(pred_rt, irradiance_handle, 0, pred_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t}\n\n\t\t\t\t// Get HBAO+ Texture\n\t\t\t\tif (data.is_hbao)\n\t\t\t\t{\n\t\t\t\t\td3d12::DescHeapCPUHandle desc_handle = data.out_screen_space_ao_alloc.GetDescriptorHandle();\n\t\t\t\t\td3d12::RenderTarget* ao_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<wr::HBAOData>());\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(ao_rt, desc_handle, 0, ao_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t}\n\n\t\t\t\t// Output UAV\n\t\t\t\t{\n\t\t\t\t\td3d12::DescHeapCPUHandle rtv_out_uav_handle = data.out_output_alloc.GetDescriptorHandle();\n\t\t\t\t\tstd::vector<Format> formats = { Format::R16G16B16A16_FLOAT };\n\t\t\t\t\td3d12::CreateUAVFromRTV(render_target, rtv_out_uav_handle, 1, formats.data());\n\t\t\t\t}\n\n\t\t\t\tif constexpr (d3d12::settings::use_bundles)\n\t\t\t\t{\n\t\t\t\t\t// Record all bundles again if required.\n\t\t\t\t\tif (data.out_requires_bundle_recording)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor (auto i = 0; i < data.out_bundle_cmd_lists.size(); i++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\td3d12::Begin(data.out_bundle_cmd_lists[i], 0);\n\t\t\t\t\t\t\tRecordDrawCommands(n_render_system, data.out_bundle_cmd_lists[i], static_cast<D3D12ConstantBufferHandle*>(camera_cb)->m_native, data, i);\n\t\t\t\t\t\t\td3d12::End(data.out_bundle_cmd_lists[i]);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tdata.out_requires_bundle_recording = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t//Render deferred\n\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t\td3d12::BindViewport(cmd_list, viewport);\n\n\t\t\t\td3d12::Transition(cmd_list,\n\t\t\t\t\trender_target,\n\t\t\t\t\twr::ResourceState::COPY_SOURCE,\n\t\t\t\t\twr::ResourceState::UNORDERED_ACCESS);\n\n\t\t\t\tif constexpr (d3d12::settings::use_bundles)\n\t\t\t\t{\n\t\t\t\t\tbool is_fallback = d3d12::GetRaytracingType(n_render_system.m_device) == RaytracingType::FALLBACK;\n\t\t\t\t\td3d12::BindDescriptorHeaps(cmd_list, is_fallback);\n\t\t\t\t\td3d12::ExecuteBundle(cmd_list, data.out_bundle_cmd_lists[frame_idx]);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tRecordDrawCommands(n_render_system, cmd_list, static_cast<D3D12ConstantBufferHandle*>(camera_cb)->m_native, data, frame_idx);\n\t\t\t\t}\n\n\t\t\t\td3d12::Transition(cmd_list,\n\t\t\t\t\trender_target,\n\t\t\t\t\twr::ResourceState::UNORDERED_ACCESS,\n\t\t\t\t\twr::ResourceState::COPY_SOURCE);\n\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE);\n\t\t\t}\n\t\t}\n\n\t\tvoid DestroyDeferredCompositionTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t}\n\n\t}\n\n\tvoid AddDeferredCompositionTask(FrameGraph& fg, std::optional<unsigned int> target_width, std::optional<unsigned int> target_height)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(target_width),\n\t\t\tRenderTargetProperties::Height(target_height),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDeferredCompositionTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDeferredCompositionTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyDeferredCompositionTask(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tfg.AddTask<DeferredCompositionTaskData>(desc, L\"Deferred Composition\", FG_DEPS<DeferredMainTaskData, CubemapConvolutionTaskData>());\n\t}\n\n}\n"
  },
  {
    "path": "src/render_tasks/d3d12_deferred_composition.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../pipeline_registry.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_descriptors_allocations.hpp\"\n#include \"../d3d12/d3d12_texture_resources.hpp\"\n\nnamespace wr\n{\n\tstruct DeferredCompositionTaskData\n\t{\n\t\td3d12::PipelineState* in_pipeline = nullptr;\n\t\td3d12::RenderTarget* out_deferred_main_rt = nullptr;\n\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\n\t\tDescriptorAllocation out_gbuffer_albedo_alloc;\n\t\tDescriptorAllocation out_gbuffer_normal_alloc;\n\t\tDescriptorAllocation out_gbuffer_emissive_alloc;\n\t\tDescriptorAllocation out_gbuffer_depth_alloc;\n\t\tDescriptorAllocation out_lights_alloc;\n\t\tDescriptorAllocation out_buffer_refl_alloc;\n\t\tDescriptorAllocation out_buffer_shadow_alloc;\n\t\tDescriptorAllocation out_screen_space_irradiance_alloc;\n\t\tDescriptorAllocation out_screen_space_ao_alloc;\n\t\tDescriptorAllocation out_output_alloc;\n\n\t\td3d12::TextureResource* out_skybox = nullptr;\n\t\td3d12::TextureResource* out_irradiance = nullptr;\n\t\td3d12::TextureResource* out_pref_env_map = nullptr;\n\n\t\tstd::array<d3d12::CommandList*, d3d12::settings::num_back_buffers> out_bundle_cmd_lists = {};\n\t\tbool out_requires_bundle_recording = false;\n\n\t\tbool is_hybrid = false;\n\t\tbool has_rt_reflection = false;\n\t\tbool has_rt_shadows = false;\n\t\tbool has_rt_shadows_denoiser = false;\n\t\tbool is_path_tracer = false;\n\t\tbool is_rtao = false;\n\t\tbool is_hbao = false;\n\t};\n\n\tnamespace internal\n\t{\n\t\tvoid RecordDrawCommands(D3D12RenderSystem& render_system, d3d12::CommandList* cmd_list, d3d12::HeapResource* camera_cb, DeferredCompositionTaskData const & data, unsigned int frame_idx);\n\n\t\tvoid SetupDeferredCompositionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle);\n\t\tvoid ExecuteDeferredCompositionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle);\n\t\tvoid DestroyDeferredCompositionTask(FrameGraph& fg, RenderTaskHandle handle);\n\n\t}\n\n\tvoid AddDeferredCompositionTask(FrameGraph& fg, std::optional<unsigned int> target_width, std::optional<unsigned int> target_height);\n\n}"
  },
  {
    "path": "src/render_tasks/d3d12_deferred_main.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_model_pool.hpp\"\n#include \"../d3d12/d3d12_resource_pool_texture.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../pipeline_registry.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"../platform_independend_structs.hpp\"\n#include \"d3d12_imgui_render_task.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n\nnamespace wr\n{\n\n\tstruct DeferredMainTaskData\n\t{\n\t\td3d12::PipelineState* in_pipeline;\n\t\tbool is_hybrid;\n\t};\n\t\n\tnamespace internal\n\t{\n\n\t\tinline void SetupDeferredTask(RenderSystem&, FrameGraph& fg, RenderTaskHandle handle, bool resized, bool is_hybrid)\n\t\t{\n\t\t\tauto& data = fg.GetData<DeferredMainTaskData>(handle);\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\n\t\t\tdata.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(is_hybrid ? pipelines::basic_hybrid : pipelines::basic_deferred);\n\t\t\tdata.is_hybrid = is_hybrid;\n\t\t}\n\n\t\tinline void ExecuteDeferredTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DeferredMainTaskData>(handle);\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\n\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t{\n\t\t\t\tconst auto viewport = n_render_system.m_viewport;\n\t\t\t\tconst auto frame_idx = n_render_system.GetRenderWindow()->m_frame_idx;\n\n\t\t\t\td3d12::BindViewport(cmd_list, viewport);\n\t\t\t\td3d12::BindPipeline(cmd_list, data.in_pipeline);\n\t\t\t\td3d12::SetPrimitiveTopology(cmd_list, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);\n\n\t\t\t\tauto d3d12_cb_handle = static_cast<D3D12ConstantBufferHandle*>(scene_graph.GetActiveCamera()->m_camera_cb);\n\t\t\t\td3d12::BindConstantBuffer(cmd_list, d3d12_cb_handle->m_native, 0, frame_idx);\n\n\t\t\t\tscene_graph.Render(cmd_list, scene_graph.GetActiveCamera().get());\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddDeferredMainTask(FrameGraph& fg, std::optional<unsigned int> target_width, std::optional<unsigned int> target_height, bool is_hybrid)\n\t{\n\t\tstd::wstring name(L\"Deferred Main\");\n\n\t\tRenderTargetProperties rt_properties_deferred\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(target_width),\n\t\t\tRenderTargetProperties::Height(target_height),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::NON_PIXEL_SHADER_RESOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(true),\n\t\t\tRenderTargetProperties::DSVFormat(Format::D32_FLOAT),\n\t\t\tRenderTargetProperties::RTVFormats({ Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(3),\n\t\t\tRenderTargetProperties::Clear(true),\n\t\t\tRenderTargetProperties::ClearDepth(true),\n\t\t};\n\n\t\tRenderTargetProperties rt_properties_hybrid\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(target_width),\n\t\t\tRenderTargetProperties::Height(target_height),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::NON_PIXEL_SHADER_RESOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(true),\n\t\t\tRenderTargetProperties::DSVFormat(Format::D32_FLOAT),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT, wr::Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT, wr::Format::R16G16B16A16_FLOAT, wr::Format::R32G32B32A32_FLOAT, wr::Format::R32G32B32A32_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(6),\n\t\t\tRenderTargetProperties::Clear(true),\n\t\t\tRenderTargetProperties::ClearDepth(true)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [is_hybrid](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resized) {\n\t\t\tinternal::SetupDeferredTask(rs, fg, handle, resized, is_hybrid);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDeferredTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph&, RenderTaskHandle, bool) {\n\t\t\t// Nothing to destroy\n\t\t};\n\n\t\tdesc.m_properties = is_hybrid ? rt_properties_hybrid : rt_properties_deferred;\n\t\tdesc.m_type = RenderTaskType::DIRECT;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tfg.AddTask<DeferredMainTaskData>(desc, L\"Deferred Main\");\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/render_tasks/d3d12_deferred_render_target_copy.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include <locale>\n#include <codecvt>\n#include <string>\n\nnamespace wr\n{\n\tstruct RenderTargetCopyTaskData\n\t{\n\t\td3d12::RenderTarget* out_rt;\n\t};\n\n\tnamespace internal\n\t{\n\t\ttemplate<typename T>\n\t\tinline void SetupCopyTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& data = fg.GetData<RenderTargetCopyTaskData>(handle);\n\t\t\tdata.out_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t}\n\n\t\tinline void ExecuteCopyTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<RenderTargetCopyTaskData>(handle);\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tauto render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tconst auto frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\tD3D12_TEXTURE_COPY_LOCATION dst = {};\n\t\t\tdst.pResource = render_target->m_render_targets[frame_idx % render_target->m_render_targets.size()];\n\t\t\tdst.Type = D3D12_TEXTURE_COPY_TYPE::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;\n\t\t\tdst.SubresourceIndex = 0;\n\n\t\t\tD3D12_TEXTURE_COPY_LOCATION src = {};\n\t\t\tsrc.pResource = data.out_rt->m_render_targets[0];\n\t\t\tsrc.Type = D3D12_TEXTURE_COPY_TYPE::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;\n\t\t\tsrc.SubresourceIndex = 0;\n\n\t\t\tcmd_list->m_native->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddRenderTargetCopyTask(FrameGraph& frame_graph)\n\t{\n\t\tstd::string name_temp = std::string(\"Render Target (\") + std::string(typeid(T).name()) + std::string(\") Copy task\");\n\t\tstd::wstring w_name = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().from_bytes(name_temp);\n\n\t\tRenderTargetProperties rt_properties{\n\t\t\tRenderTargetProperties::IsRenderWindow(true),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::COPY_DEST),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::PRESENT),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ Format::R8G8B8A8_UNORM }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool) {\n\t\t\tinternal::SetupCopyTask<T>(rs, fg, handle);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteCopyTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph&, RenderTaskHandle, bool) {\n\t\t\t// Nothing to destroy\n\t\t};\n\t\t\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COPY;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<RenderTargetCopyTaskData>(desc, w_name, FG_DEPS<T>());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_dof_bokeh.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct DoFBokehData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_coc_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\n\t\tstd::shared_ptr<ConstantBufferPool> camera_cb_pool;\n\t\tD3D12ConstantBufferHandle* cb_handle = nullptr;\n\t};\n\n\tnamespace internal\n\t{\n\t\tstruct BokehShapeModifier\n\t\t{\n\t\t\tfloat x = 1.0f;\n\t\t\tfloat y = 1.0f;\n\t\t};\n\n\t\tstruct Bokeh_CB\n\t\t{\n\t\t\tfloat m_f_number = 0.0f;\n\t\t\tfloat m_shape = 0.0f;\n\t\t\tfloat m_bokeh_poly = 0.0f;\n\t\t\tuint32_t m_blades = 0u;\n\t\t\tfloat _padding;\n\t\t\tBokehShapeModifier m_bokeh_shape_modifier;\n\t\t\tint m_enable_dof = false;\n\t\t};\n\n\t\ttemplate<typename T, typename T1>\n\t\tinline void SetupDoFBokehTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DoFBokehData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(5);\n\n\t\t\t\tdata.camera_cb_pool = rs.CreateConstantBufferPool(2);\n\t\t\t\tdata.cb_handle = static_cast<D3D12ConstantBufferHandle*>(data.camera_cb_pool->Create(sizeof(Bokeh_CB)));\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_bokeh));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto source_coc_rt = data.out_source_coc_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\n\t\t\tfor (auto frame_idx = 0; frame_idx < versions; frame_idx++)\n\t\t\t{\n\t\t\t\t// Destination\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_NEAR)));\n\t\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t\t}\n\t\t\t\t// Destination far\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_FAR)));\n\t\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t\t}\n\t\t\t\t// Source near\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_NEAR)));\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t}\n\t\t\t\t// Source far\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_FAR)));\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]);\n\t\t\t\t}\n\t\t\t\t// Dilated COC\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::COC)));\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_coc_rt, cpu_handle, 0, source_coc_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\ttemplate<typename T, typename T1>\n\t\tinline void ExecuteDoFBokehTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<DoFBokehData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto source_coc_rt = data.out_source_coc_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_NEAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Destination far\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_FAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Source near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_NEAR)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source far\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_FAR)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Dilated COC\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::COC)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_coc_rt, cpu_handle, 0, source_coc_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\tBokehShapeModifier shape_modifier;\n\t\t\tBokeh_CB cb_data;\n\t\t\tcb_data.m_f_number = sg.GetActiveCamera()->m_f_number;\n\t\t\tcb_data.m_shape = sg.GetActiveCamera()->m_shape_amt;\n\t\t\tcb_data.m_blades = sg.GetActiveCamera()->m_aperture_blades;\n\t\t\tcb_data.m_bokeh_poly = sqrt(std::clamp((sg.GetActiveCamera()->m_f_number - 1.8f) / (7.0f - 1.8f), 0.f, 1.f));\n\t\t\tcb_data.m_enable_dof = sg.GetActiveCamera()->m_enable_dof;\n\t\t\tcb_data.m_bokeh_shape_modifier = shape_modifier;\n\n\t\t\tdata.cb_handle->m_pool->Update(data.cb_handle, sizeof(Bokeh_CB), 0, frame_idx, (uint8_t*)&cb_data);\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_n_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_NEAR);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_n_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_n_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_f_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_FAR);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_f_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_f_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_near_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_NEAR);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_near_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_near_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_far_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_FAR);\n\t\t\t\tauto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_far_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_far_idx, handle_b_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_coc_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::COC);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_coc_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_coc_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\t//TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyDoFBokehTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<DoFBokehData>(handle);\n\t\t\t\tdata.camera_cb_pool->Destroy(data.cb_handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T, typename T1>\n\tinline void AddDoFBokehTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(2),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.5f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDoFBokehTask<T, T1>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDoFBokehTask<T, T1>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyDoFBokehTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<DoFBokehData>(desc, L\"DoF Bokeh Pass\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_dof_bokeh_postfilter.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct DoFBokehPostFilterData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\n\t\ttemplate<typename T>\n\t\tinline void SetupDoFBokehPostFilterTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DoFBokehPostFilterData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(4);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_bokeh_post_filter));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Source near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_NEAR)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source far\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_FAR)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Destination near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_NEAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Destination far\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_FAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecuteDoFBokehPostFilterTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<DoFBokehPostFilterData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Source near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_NEAR)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source far\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_FAR)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Destination near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_NEAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Destination far\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_FAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_n_idx = rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_NEAR);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_n_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_n_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_f_idx = rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_FAR);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_f_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_f_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_near_idx = rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_NEAR);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_near_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_near_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_far_idx = rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_FAR);\n\t\t\t\tauto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_far_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_far_idx, handle_b_srv);\n\t\t\t}\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\t//TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyDoFBokehPostFilterTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<DoFBokehPostFilterData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddDoFBokehPostFilterTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(2),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.5f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDoFBokehPostFilterTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDoFBokehPostFilterTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyDoFBokehPostFilterTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<DoFBokehPostFilterData>(desc, L\"DoF Bokeh Post Filter\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_dof_coc.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct DoFCoCData\n\t{\n\t\td3d12::RenderTarget* out_source_dsv = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\n\t\tstd::shared_ptr<ConstantBufferPool> camera_cb_pool;\n\t\tD3D12ConstantBufferHandle* cb_handle = nullptr;\n\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\t\tstruct DoFProperties_CB\n\t\t{\n\t\t\tDirectX::XMMATRIX m_projection = DirectX::XMMatrixIdentity();\n\t\t\tfloat m_focal_length = 0.0f;\n\t\t\tfloat m_f_number = 0.0f;\n\t\t\tfloat m_film_size = 0.0f;\n\t\t\tfloat m_focus_dist = 0.0f;\n\t\t\tfloat m_pad[2];\n\t\t\tint m_enable_dof = 0;\n\t\t\tfloat m_dof_range = 1.0f;\n\t\t};\n\n\t\ttemplate<typename T>\n\t\tinline void SetupDoFCoCTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DoFCoCData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(2);\n\n\t\t\t\tdata.camera_cb_pool = rs.CreateConstantBufferPool(2);\n\t\t\t\tdata.cb_handle = static_cast<D3D12ConstantBufferHandle*>(data.camera_cb_pool->Create(sizeof(DoFProperties_CB)));\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_coc));\n\n\t\t\tauto source_dsv = data.out_source_dsv = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::GDEPTH)));\n\t\t\t\td3d12::CreateSRVFromDSV(source_dsv, cpu_handle);\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecuteDoFCoCTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<DoFCoCData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tdata.out_source_dsv = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\tconst auto camera_cb = static_cast<D3D12ConstantBufferHandle*>(sg.GetActiveCamera()->m_camera_cb);\n\t\t\t\n\t\t\tDoFProperties_CB cb_data;\n\t\t\tcb_data.m_projection = sg.GetActiveCamera()->m_projection;\n\t\t\tcb_data.m_film_size = sg.GetActiveCamera()->m_film_size;\n\t\t\tcb_data.m_focal_length = sg.GetActiveCamera()->m_focal_length;\n\t\t\tcb_data.m_f_number = sg.GetActiveCamera()->m_f_number;\n\t\t\tcb_data.m_focus_dist = sg.GetActiveCamera()->m_focus_dist;\n\t\t\tcb_data.m_enable_dof = sg.GetActiveCamera()->m_enable_dof;\n\t\t\tcb_data.m_dof_range = sg.GetActiveCamera()->m_dof_range;\n\n\t\t\tdata.cb_handle->m_pool->Update(data.cb_handle, sizeof(DoFProperties_CB), 0, frame_idx, (uint8_t*)&cb_data);\n\t\t\t\n\t\t\tauto source_dsv = data.out_source_dsv = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::GDEPTH)));\n\t\t\t\td3d12::CreateSRVFromDSV(source_dsv, cpu_handle);\n\t\t\t}\n\t\t\t\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::OUTPUT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::GDEPTH);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_dsv->m_render_targets[frame_idx % versions]));\n\n\t\t\t//TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyCoCTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<DoFCoCData>(handle);\n\n\t\t\t\t// Small hack to force the allocations to go out of scope, which will tell the allocator to free them\n\t\t\t\tDescriptorAllocation temp1 = std::move(data.out_allocation);\n\t\t\t\tdata.camera_cb_pool->Destroy(data.cb_handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddDoFCoCTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc; \n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDoFCoCTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDoFCoCTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyCoCTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<DoFCoCData>(desc, L\"DoF Cone of Confusion\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_dof_composition.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_deferred_composition.hpp\"\n#include \"../render_tasks/d3d12_dof_bokeh_postfilter.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct DoFCompositionData\n\t{\n\t\td3d12::RenderTarget* out_source_rt_comp = nullptr;\n\t\td3d12::RenderTarget* out_source_bokeh_filtered = nullptr;\n\t\td3d12::RenderTarget* out_source_coc = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\n\t\ttemplate<typename T, typename T1, typename T2>\n\t\tinline void SetupDoFCompositionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DoFCompositionData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(5);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_composition));\n\n\t\t\tauto source_rt_comp = data.out_source_rt_comp = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto source_rt_bokeh_filtered = data.out_source_bokeh_filtered = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\t\t\tauto source_coc = data.out_source_coc = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T2>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt_comp, cpu_handle, 0, source_rt_comp->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Bokeh near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_NEAR)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt_bokeh_filtered, cpu_handle, 0, source_rt_bokeh_filtered->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Bokeh far\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_FAR)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt_bokeh_filtered, cpu_handle, 1, source_rt_bokeh_filtered->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Cone of confusion\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::COC)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_coc, cpu_handle, 0, source_coc->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\ttemplate<typename T, typename T1, typename T2>\n\t\tinline void ExecuteDoFCompositionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<DoFCompositionData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\tauto source_rt_comp = data.out_source_rt_comp = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto source_rt_bokeh_filtered = data.out_source_bokeh_filtered = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\t\t\tauto source_coc = data.out_source_coc = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T2>());\n\n\t\t\tfor (auto frame_idx = 0; frame_idx < versions; frame_idx++)\n\t\t\t{\n\t\t\t\t// Destination\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::OUTPUT)));\n\t\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, frame_idx, n_render_target->m_create_info.m_rtv_formats[frame_idx]);\n\t\t\t\t}\n\t\t\t\t// Source\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::SOURCE)));\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt_comp, cpu_handle, 0, source_rt_comp->m_create_info.m_rtv_formats[0]);\n\t\t\t\t}\n\t\t\t\t// Bokeh near\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_NEAR)));\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt_bokeh_filtered, cpu_handle, 0, source_rt_bokeh_filtered->m_create_info.m_rtv_formats[0]);\n\t\t\t\t}\n\t\t\t\t// Bokeh far\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_FAR)));\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt_bokeh_filtered, cpu_handle, 1, source_rt_bokeh_filtered->m_create_info.m_rtv_formats[1]);\n\t\t\t\t}\n\t\t\t\t// Cone of confusion\n\t\t\t\t{\n\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::COC)));\n\t\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_coc, cpu_handle, frame_idx, source_coc->m_create_info.m_rtv_formats[frame_idx]);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_n_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::OUTPUT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_n_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_n_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::SOURCE);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_near_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_NEAR);\n\t\t\t\tauto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_near_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_near_idx, handle_b_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_far_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_FAR);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_far_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_far_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_coc_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::COC);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_coc_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_coc_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt_comp->m_render_targets[frame_idx % versions]));\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyDoFCompositionTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<DoFCompositionData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T, typename T1, typename T2>\n\tinline void AddDoFCompositionTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDoFCompositionTask<T, T1, T2>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDoFCompositionTask<T, T1, T2>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyDoFCompositionTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<DoFCompositionData>(desc, L\"DoF Composition\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_dof_compute_near_mask.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct DoFNearMaskData\n\t{\n\t\td3d12::RenderTarget* out_source_dsv = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\n\t\tstd::shared_ptr<ConstantBufferPool> camera_cb_pool;\n\t\tD3D12ConstantBufferHandle* cb_handle = nullptr;\n\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\n\t\ttemplate<typename T>\n\t\tinline void SetupDoFNearMaskTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DoFNearMaskData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(2);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_near_mask));\n\n\t\t\tauto source_dsv = data.out_source_dsv = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::INPUT)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_dsv, cpu_handle, 0, source_dsv->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecuteDoFNearMaskTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<DoFNearMaskData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tdata.out_source_dsv = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\tauto source_dsv = data.out_source_dsv = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::INPUT)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_dsv, cpu_handle, 0, source_dsv->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\t\t\t\n\t\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::OUTPUT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::INPUT);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_m_srv);\n\t\t\t}\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_dsv->m_render_targets[frame_idx % versions]));\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil((n_render_system.m_viewport.m_viewport.Width + 31.0f )/ 32.f)),\n\t\t\t\tstatic_cast<int>(std::ceil((n_render_system.m_viewport.m_viewport.Height + 31.0f ) / 32.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyDoFNearMaskTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<DoFNearMaskData>(handle);\n\n\t\t\t\t// Small hack to force the allocations to go out of scope, which will tell the allocator to free them\n\t\t\t\tDescriptorAllocation temp1 = std::move(data.out_allocation);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddDoFNearMaskTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.03125f)\n\t\t\t\n\t\t};\n\n\t\tRenderTaskDesc desc; \n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDoFNearMaskTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDoFNearMaskTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyDoFNearMaskTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<DoFNearMaskData>(desc, L\"DoF near mask\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_dof_dilate_flatten.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct DoFDilateFlattenData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_coc_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\t\ttemplate<typename T>\n\t\tinline void SetupDoFDilateFlattenTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DoFDilateFlattenData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(2);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_dilate_flatten));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecuteDoFDilateFlattenTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<DoFDilateFlattenData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::OUTPUT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::SOURCE);\n\t\t\t\tauto handle_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_srv);\n\t\t\t}\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\t//TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyDoFDilateFlattenTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<DoFDilateFlattenData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddDoFDilateFlattenTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({wr::Format::R16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.125f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDoFDilateFlattenTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDoFDilateFlattenTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyDoFDilateFlattenTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<DoFDilateFlattenData>(desc, L\"DoF coc dilate flatten horizontal\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_dof_dilate_flatten_second_pass.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct DoFDilateFlattenHData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_coc_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\t\ttemplate<typename T>\n\t\tinline void SetupDoFDilateFlattenHTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DoFDilateFlattenHData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\t\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(2);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_dilate_flatten_h));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecuteDoFDilateFlattenHTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<DoFDilateFlattenHData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::OUTPUT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::SOURCE);\n\t\t\t\tauto handle_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_srv);\n\t\t\t}\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\t//TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyDoFDilateFlattenHTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<DoFDilateFlattenHData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddDoFDilateFlattenHTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({wr::Format::R16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.125f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDoFDilateFlattenHTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDoFDilateFlattenHTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyDoFDilateFlattenHTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<DoFDilateFlattenHData>(desc, L\"DoF dilate flatten vertical pass\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_dof_dilate_near.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct DoFDilateData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_coc_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\t\ttemplate<typename T>\n\t\tinline void SetupDoFDilateTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DoFDilateData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(2);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_dilate));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecuteDoFDilateTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<DoFDilateData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::OUTPUT)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::OUTPUT);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::SOURCE);\n\t\t\t\tauto handle_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_srv);\n\t\t\t}\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\t//TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyDoFDilateTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<DoFDilateData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddDoFDilateTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({wr::Format::R16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.03125f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDoFDilateTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDoFDilateTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyDoFDilateTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<DoFDilateData>(desc, L\"DoF Dilate\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_down_scale.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_post_processing.hpp\"\n\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct DownScaleData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::RenderTarget* out_source_emissive = nullptr;\n\t\td3d12::RenderTarget* out_source_coc = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\t\tID3D12Resource* out_previous = nullptr;\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tnamespace internal\n\t{\n\t\n\t\ttemplate<typename T, typename T1>\n\t\tinline void SetupDownScaleTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<DownScaleData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.out_allocation = data.out_allocator->Allocate(4);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::down_scale));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto source_coc = data.out_source_coc = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\n\t\t\t// Destination near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_NEAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Destination far\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_FAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t// Cone of confusion\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::COC)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_coc, cpu_handle, 0, source_coc->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t}\n\n\t\ttemplate<typename T, typename T1>\n\t\tinline void ExecuteDownScaleTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<DownScaleData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\tauto source_coc = data.out_source_coc = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T1>());\n\n\t\t\t// Destination near\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_NEAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Destination far\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_FAR)));\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::SOURCE)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Cone of confusion\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::COC)));\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_coc, cpu_handle, 0, source_coc->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_n_idx = rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_NEAR);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_n_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_n_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_f_idx = rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_FAR);\n\t\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_f_idx);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_f_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::SOURCE);\n\t\t\t\tauto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_b_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_coc_idx = rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::COC);\n\t\t\t\tauto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_coc_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_coc_idx, handle_m_srv);\n\t\t\t}\t\t\t\n\t\t\t\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyDownScaleTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<DownScaleData>(handle);\n\t\t\t\tdelete data.out_allocator;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T, typename T1>\n\tinline void AddDownScaleTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}),\n\t\t\tRenderTargetProperties::NumRTVFormats(2),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.5f)\n\t\t};\n\n\t\tRenderTaskDesc desc; \n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupDownScaleTask<T, T1>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteDownScaleTask<T, T1>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyDownScaleTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<DownScaleData>(desc, L\"Down Scale\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_equirect_to_cubemap.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_defines.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_resource_pool_texture.hpp\"\n#include \"../d3d12/d3d12_model_pool.hpp\"\n#include \"../d3d12/d3d12_resource_pool_texture.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../pipeline_registry.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"../platform_independend_structs.hpp\"\n#include \"d3d12_imgui_render_task.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n\nnamespace wr\n{\n\tstruct EquirectToCubemapTaskData\n\t{\n\t\td3d12::PipelineState* in_pipeline = nullptr;\n\n\t\tTextureHandle in_equirect = {};\n\t\tTextureHandle out_cubemap = {};\n\t\tTextureHandle out_pref_env = {};\n\n\t\tstd::shared_ptr<ConstantBufferPool> camera_cb_pool;\n\t\tD3D12ConstantBufferHandle* cb_handle = nullptr;\n\n\t\tDirectX::XMMATRIX proj_mat = { DirectX::XMMatrixIdentity() };\n\t\tDirectX::XMMATRIX view_mat[6] = {};\n\n\t\tstd::uint32_t build_frame_idx = -1;\n\t};\n\n\tnamespace internal\n\t{\n\t\tstruct ProjectionView_CB\n\t\t{\n\t\t\tDirectX::XMMATRIX m_projection;\n\t\t\tDirectX::XMMATRIX m_view[6];\n\t\t};\n\n\t\tstruct PrefilterEnv_CB\n\t\t{\n\t\t\tDirectX::XMFLOAT2 texture_size;\n\t\t\tDirectX::XMFLOAT2 skybox_res;\n\t\t\tfloat\t roughness;\n\t\t\tuint32_t cubemap_face;\n\t\t};\n\n\t\tinline void PrefilterCubemapFace(d3d12::TextureResource* source_cubemap, d3d12::TextureResource* dest_cubemap, CommandList* cmd_list, DescriptorAllocator* alloc, unsigned int array_slice)\n\t\t{\n\t\t\twr::d3d12::CommandList* d3d12_cmd_list = static_cast<wr::d3d12::CommandList*>(cmd_list);\n\n\t\t\t//Create shader resource view for the source texture in the descriptor heap\n\t\t\tDescriptorAllocation srv_alloc = alloc->Allocate();\n\t\t\td3d12::DescHeapCPUHandle srv_handle = srv_alloc.GetDescriptorHandle();\n\n\t\t\td3d12::CreateSRVFromTexture(source_cubemap, srv_handle);\n\n\t\t\tPrefilterEnv_CB prefilter_env_cb;\n\n\t\t\tfor (uint32_t src_mip = 0; src_mip < dest_cubemap->m_mip_levels; ++src_mip)\n\t\t\t{\n\t\t\t\tuint32_t width = static_cast<std::uint32_t>(dest_cubemap->m_width) >> src_mip;\n\t\t\t\tuint32_t height = static_cast<std::uint32_t>(dest_cubemap->m_height) >> src_mip;\n\n\t\t\t\tprefilter_env_cb.texture_size = DirectX::XMFLOAT2(static_cast<float>(width), static_cast<float>(height));\n\t\t\t\tprefilter_env_cb.skybox_res = DirectX::XMFLOAT2(static_cast<float>(source_cubemap->m_width), static_cast<float>(source_cubemap->m_height));\n\t\t\t\tprefilter_env_cb.roughness = static_cast<float>(src_mip) / static_cast<float>(dest_cubemap->m_mip_levels);\n\t\t\t\tprefilter_env_cb.cubemap_face = array_slice;\n\n\t\t\t\tDescriptorAllocation uav_alloc = alloc->Allocate();\n\t\t\t\td3d12::DescHeapCPUHandle uav_handle = uav_alloc.GetDescriptorHandle();\n\n\t\t\t\td3d12::CreateUAVFromCubemapFace(dest_cubemap, uav_handle, src_mip, array_slice);\n\n\t\t\t\t//Set shader variables\n\t\t\t\td3d12::BindCompute32BitConstants(d3d12_cmd_list, &prefilter_env_cb, sizeof(PrefilterEnv_CB) / sizeof(uint32_t), 0, 0);\n\n\t\t\t\td3d12::Transition(d3d12_cmd_list, source_cubemap, source_cubemap->m_subresource_states[src_mip], ResourceState::PIXEL_SHADER_RESOURCE, src_mip, 1);\n\t\t\t\td3d12::SetShaderSRV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::SOURCE)), srv_handle);\n\n\t\t\t\td3d12::Transition(d3d12_cmd_list, dest_cubemap, dest_cubemap->m_subresource_states[src_mip], ResourceState::UNORDERED_ACCESS, src_mip, 1);\n\t\t\t\td3d12::SetShaderUAV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)), uav_handle);\n\n\t\t\t\td3d12::Dispatch(d3d12_cmd_list, ((width + 8 - 1) / 8), ((height + 8 - 1) / 8), 1u);\n\n\t\t\t\t//Wait for all accesses to the destination texture UAV to be finished before generating the next mipmap, as it will be the source texture for the next mipmap\n\t\t\t\td3d12::UAVBarrier(d3d12_cmd_list, { dest_cubemap });\n\n\t\t\t\td3d12::Transition(d3d12_cmd_list, dest_cubemap, dest_cubemap->m_subresource_states[src_mip], ResourceState::PIXEL_SHADER_RESOURCE, src_mip, 1);\n\t\t\t}\n\n\t\t\tdest_cubemap->m_need_mips = false;\n\t\t}\n\n\n\t\tinline void PrefilterCubemap(d3d12::CommandList* cmd_list, wr::TextureHandle src_texture, wr::TextureHandle dst_texture)\n\t\t{\n\t\t\tauto pipeline = static_cast<d3d12::PipelineState*>(PipelineRegistry::Get().Find(pipelines::cubemap_prefiltering));\n\n\t\t\td3d12::BindComputePipeline(cmd_list, pipeline);\n\t\t\t\n\t\t\t//Get allocator from pool\n\t\t\tauto allocator = static_cast<D3D12TexturePool*>(src_texture.m_pool)->GetMipmappingAllocator();\n\n\t\t\td3d12::TextureResource* cubemap_text = static_cast<d3d12::TextureResource*>(src_texture.m_pool->GetTextureResource(src_texture));\n\t\t\td3d12::TextureResource* envmap_text = static_cast<d3d12::TextureResource*>(dst_texture.m_pool->GetTextureResource(dst_texture));\n\n\t\t\tfor (uint32_t i = 0; i < 6; ++i)\n\t\t\t{\n\t\t\t\tPrefilterCubemapFace(cubemap_text, envmap_text, cmd_list, allocator, i);\n\t\t\t}\n\t\t}\n\n\n\t\tinline void SetupEquirectToCubemapTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif (resize)\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tauto& data = fg.GetData<EquirectToCubemapTaskData>(handle);\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::equirect_to_cubemap);\n\n\t\t\tdata.camera_cb_pool = rs.CreateConstantBufferPool(2_mb);\n\t\t\tdata.cb_handle = static_cast<D3D12ConstantBufferHandle*>(data.camera_cb_pool->Create(sizeof(ProjectionView_CB)));\n\n\t\t\tdata.proj_mat = DirectX::XMMatrixPerspectiveFovRH(DirectX::XMConvertToRadians(90.0f), 1.0f, 0.1f, 10.0f);\n\n\t\t\tdata.view_mat[0] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(1.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f));\n\n\t\t\tdata.view_mat[1] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(-1.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f));\n\n\t\t\tdata.view_mat[2] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f));\n\n\t\t\tdata.view_mat[3] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f));\n\n\t\t\tdata.view_mat[4] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f));\n\n\t\t\tdata.view_mat[5] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, 0.0f, -1.0f, 1.0f),\n\t\t\t\tDirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f));\n\t\t}\n\n\t\tinline void ExecuteEquirectToCubemapTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<EquirectToCubemapTaskData>(handle);\n\n\t\t\tauto skybox_node = scene_graph.GetCurrentSkybox();\n\t\t\tif (!skybox_node)\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\n\n\t\t\t//Does it need conversion?\n\t\t\tif (skybox_node->m_skybox != std::nullopt)\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tdata.build_frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\tdata.in_equirect = skybox_node->m_hdr;\n\t\t\t\n\t\t\tskybox_node->m_skybox = skybox_node->m_hdr.m_pool->CreateCubemap(\"Skybox\", d3d12::settings::res_skybox, d3d12::settings::res_skybox, 0, wr::Format::R16G16B16A16_FLOAT, true);\n\n\t\t\tskybox_node->m_prefiltered_env_map = skybox_node->m_hdr.m_pool->CreateCubemap(\"FilteredEnvMap\", d3d12::settings::res_envmap, d3d12::settings::res_envmap, 6, wr::Format::R16G16B16A16_FLOAT, true);\n\n\n\t\t\tdata.out_cubemap = skybox_node->m_skybox.value();\n\t\t\tdata.out_pref_env = skybox_node->m_prefiltered_env_map.value();\n\n\t\t\td3d12::TextureResource* equirect_text = static_cast<d3d12::TextureResource*>(data.in_equirect.m_pool->GetTextureResource(data.in_equirect));\n\t\t\td3d12::TextureResource* cubemap_text = static_cast<d3d12::TextureResource*>(data.out_cubemap.m_pool->GetTextureResource(data.out_cubemap));\n\n\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t{\n\t\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\t\tconst auto viewport = d3d12::CreateViewport(static_cast<int>(cubemap_text->m_width), static_cast<int>(cubemap_text->m_height));\n\t\t\t\tconst auto frame_idx = n_render_system.GetRenderWindow()->m_frame_idx;\n\n\t\t\t\td3d12::BindViewport(cmd_list, viewport);\n\t\t\t\td3d12::BindPipeline(cmd_list, data.in_pipeline);\n\t\t\t\td3d12::SetPrimitiveTopology(cmd_list, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);\n\n\t\t\t\tProjectionView_CB cb_data;\n\t\t\t\tcb_data.m_view[0] = data.view_mat[0];\n\t\t\t\tcb_data.m_view[1] = data.view_mat[1];\n\t\t\t\tcb_data.m_view[2] = data.view_mat[2];\n\t\t\t\tcb_data.m_view[3] = data.view_mat[3];\n\t\t\t\tcb_data.m_view[4] = data.view_mat[4];\n\t\t\t\tcb_data.m_view[5] = data.view_mat[5];\n\t\t\t\tcb_data.m_projection = data.proj_mat;\n\n\t\t\t\tdata.cb_handle->m_pool->Update(data.cb_handle, sizeof(ProjectionView_CB), 0, frame_idx, (uint8_t*)&cb_data);\n\n\t\t\t\td3d12::BindConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx);\n\n\t\t\t\tfor (uint32_t i = 0; i < 6; ++i)\n\t\t\t\t{\n\t\t\t\t\t//Get render target handle.\n\t\t\t\t\td3d12::DescHeapCPUHandle rtv_handle = cubemap_text->m_rtv_allocation->GetDescriptorHandle(i);\n\n\t\t\t\t\tcmd_list->m_native->OMSetRenderTargets(1, &rtv_handle.m_native, false, nullptr);\n\n\t\t\t\t\td3d12::Bind32BitConstants(cmd_list, &i, 1, 0, 0);\n\n\t\t\t\t\t//bind cube and render\n\t\t\t\t\tModel* cube_model = rs.GetSimpleShape(RenderSystem::SimpleShapes::CUBE);\n\n\t\t\t\t\t//Render meshes\n\t\t\t\t\tfor (auto& mesh : cube_model->m_meshes)\n\t\t\t\t\t{\n\t\t\t\t\t\twr::D3D12ModelPool* pool = static_cast<D3D12ModelPool*>(cube_model->m_model_pool);\n\n\t\t\t\t\t\tauto n_mesh = pool->GetMeshData(mesh.first->id);\n\n\t\t\t\t\t\td3d12::BindVertexBuffer(cmd_list, pool->GetVertexStagingBuffer(), 0, pool->GetVertexStagingBuffer()->m_size, n_mesh->m_vertex_staging_buffer_stride);\n\n\t\t\t\t\t\td3d12::BindIndexBuffer(cmd_list, pool->GetIndexStagingBuffer(), 0, static_cast<std::uint32_t>(pool->GetIndexStagingBuffer()->m_size));\n\n\t\t\t\t\t\tconstexpr unsigned int srv_idx = rs_layout::GetHeapLoc(params::cubemap_conversion, params::CubemapConversionE::EQUIRECTANGULAR_TEXTURE);\n\t\t\t\t\t\td3d12::SetShaderSRV(cmd_list, 2, srv_idx, equirect_text);\n\n\t\t\t\t\t\td3d12::BindDescriptorHeaps(cmd_list);\n\n\t\t\t\t\t\tif (n_mesh->m_index_count != 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\td3d12::DrawIndexed(cmd_list, static_cast<std::uint32_t>(n_mesh->m_index_count), 1, static_cast<std::uint32_t>(n_mesh->m_index_staging_buffer_offset), static_cast<std::uint32_t>(n_mesh->m_vertex_staging_buffer_offset));\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\td3d12::Draw(cmd_list, static_cast<std::uint32_t>(n_mesh->m_vertex_count), 1, static_cast<std::uint32_t>(n_mesh->m_vertex_staging_buffer_offset));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\td3d12::Transition(cmd_list, cubemap_text, cubemap_text->m_subresource_states[0], ResourceState::PIXEL_SHADER_RESOURCE);\n\n\t\t\t\t//Mipmap the skybox\n\t\t\t\tauto pipeline = static_cast<d3d12::PipelineState*>(PipelineRegistry::Get().Find(pipelines::mip_mapping));\n\n\t\t\t\td3d12::BindComputePipeline(cmd_list, pipeline);\n\n\t\t\t\tauto texture_pool = std::static_pointer_cast<D3D12TexturePool>(n_render_system.m_texture_pools[0]);\n\n\t\t\t\ttexture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 0);\n\t\t\t\ttexture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 1);\n\t\t\t\ttexture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 2);\n\t\t\t\ttexture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 3);\n\t\t\t\ttexture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 4);\n\t\t\t\ttexture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 5);\n\n\n\t\t\t\t//Prefilter environment map\n\t\t\t\tPrefilterCubemap(cmd_list, data.out_cubemap, data.out_pref_env);\n\n\t\t\t\tfg.SetShouldExecute(handle, false);\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddEquirectToCubemapTask(FrameGraph& fg)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::NON_PIXEL_SHADER_RESOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(true),\n\t\t\tRenderTargetProperties::DSVFormat(Format::D32_FLOAT),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(true),\n\t\t\tRenderTargetProperties::ClearDepth(true),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [&](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupEquirectToCubemapTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteEquirectToCubemapTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tif(!resize)\n\t\t\t{\n\t\t\t\tauto &data = fg.GetData<EquirectToCubemapTaskData>(handle);\n\t\t\t\tdata.camera_cb_pool.reset();\n\t\t\t}\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::DIRECT;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tfg.AddTask<EquirectToCubemapTaskData>(desc, L\"Equire To Cubemap\");\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/render_tasks/d3d12_hbao.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"d3d12_raytracing_task.hpp\"\n#include \"d3d12_deferred_main.hpp\"\n\n#ifdef NVIDIA_GAMEWORKS_HBAO\n#include <GFSDK_SSAO.h>\n#endif\n\nnamespace wr\n{\n\tstruct HBAOSettings\n\t{\n\t\tstruct Runtime\n\t\t{\n\t\t\tfloat m_meters_to_view_space_units = 1; // DistanceInViewSpaceUnits = MetersToViewSpaceUnits * DistanceInMeters\n\t\t\tfloat m_radius = 2.f; // The AO radius in meters\n\t\t\tfloat m_bias = 0.1f;  // To hide low-tessellation artifacts // 0.0~0.5\n\t\t\tfloat m_power_exp = 2.f; // The final AO output is pow(AO, powerExponent) // 1.0~4.0\n\t\t\tbool m_enable_blur = true; // To blur the AO with an edge-preserving blur\n\t\t\tfloat m_blur_sharpness = 16.f; // The higher, the more the blur preserves edges // 0.0~16.0\n\t\t};\n\n\t\tRuntime m_runtime;\n\t};\n\n\tstruct HBAOData\n\t{\n\t\td3d12::RenderTarget* out_deferred_main_rt = nullptr;\n\t\td3d12::DescriptorHeap* out_descriptor_heap_srv = nullptr;\n\t\td3d12::DescriptorHeap* out_descriptor_heap_rtv = nullptr;\n\n#ifdef NVIDIA_GAMEWORKS_HBAO\n\t\tGFSDK_SSAO_Context_D3D12* ssao_context = nullptr;\n\t\tGFSDK_SSAO_InputData_D3D12 ssao_input_data;\n#endif\n\n\t\td3d12::DescHeapCPUHandle cpu_depth_handle{};\n\t\td3d12::DescHeapGPUHandle gpu_depth_handle{};\n\t\td3d12::DescHeapCPUHandle cpu_normal_handle{};\n\t\td3d12::DescHeapGPUHandle gpu_normal_handle{};\n\t\td3d12::DescHeapCPUHandle cpu_target_handle{};\n\t};\n\n\tnamespace internal\n\t{\n\n\t\tinline void SetupHBAOTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool)\n\t\t{\n#ifdef NVIDIA_GAMEWORKS_HBAO\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto n_device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<HBAOData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\t// shader resoruce view heap.\n\t\t\t{\n\t\t\t\td3d12::desc::DescriptorHeapDesc heap_desc;\n\t\t\t\theap_desc.m_num_descriptors = 2 + GFSDK_SSAO_NUM_DESCRIPTORS_CBV_SRV_UAV_HEAP_D3D12;\n\t\t\t\theap_desc.m_type = DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV;\n\t\t\t\theap_desc.m_versions = 1;\n\t\t\t\tdata.out_descriptor_heap_srv = d3d12::CreateDescriptorHeap(n_device, heap_desc);\n\n\t\t\t\tdata.gpu_depth_handle = d3d12::GetGPUHandle(data.out_descriptor_heap_srv, 0);\n\t\t\t\tdata.cpu_depth_handle = d3d12::GetCPUHandle(data.out_descriptor_heap_srv, 0);\n\t\t\t\tdata.gpu_normal_handle = d3d12::GetGPUHandle(data.out_descriptor_heap_srv, 0, 1);\n\t\t\t\tdata.cpu_normal_handle = d3d12::GetCPUHandle(data.out_descriptor_heap_srv, 0, 1);\n\t\t\t}\n\n\t\t\t// render target view heap.\n\t\t\t{\n\t\t\t\td3d12::desc::DescriptorHeapDesc heap_desc;\n\t\t\t\theap_desc.m_num_descriptors = GFSDK_SSAO_NUM_DESCRIPTORS_RTV_HEAP_D3D12;\n\t\t\t\theap_desc.m_type = DescriptorHeapType::DESC_HEAP_TYPE_RTV;\n\t\t\t\theap_desc.m_shader_visible = false;\n\t\t\t\theap_desc.m_versions = 1;\n\t\t\t\tdata.out_descriptor_heap_rtv = d3d12::CreateDescriptorHeap(n_device, heap_desc);\n\n\t\t\t\tdata.cpu_target_handle = d3d12::GetCPUHandle(data.out_descriptor_heap_rtv, 0);\n\t\t\t}\n\n\t\t\t// depth & normal\n\t\t\t{\n\t\t\t\tauto deferred_main_rt = data.out_deferred_main_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<DeferredMainTaskData>());\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, data.cpu_normal_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats[1]);\n\t\t\t\td3d12::CreateSRVFromDSV(deferred_main_rt, data.cpu_depth_handle);\n\t\t\t}\n\n\t\t\t// target\n\t\t\t{\n\t\t\t\tn_render_target->m_rtv_descriptor_increment_size = n_device->m_native->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);\n\t\t\t\tn_device->m_native->CreateRenderTargetView(n_render_target->m_render_targets[0], nullptr, data.cpu_target_handle.m_native);\n\t\t\t}\n\n\t\t\tdata.ssao_input_data = {};\n\t\t\tdata.ssao_input_data.DepthData.DepthTextureType = GFSDK_SSAO_HARDWARE_DEPTHS;\n\t\t\tdata.ssao_input_data.DepthData.FullResDepthTextureSRV.pResource = data.out_deferred_main_rt->m_depth_stencil_buffer;\n\t\t\tdata.ssao_input_data.DepthData.FullResDepthTextureSRV.GpuHandle = data.gpu_depth_handle.m_native.ptr;\n\t\t\tdata.ssao_input_data.NormalData.Enable = false;\n\t\t\tdata.ssao_input_data.NormalData.FullResNormalTextureSRV.pResource = data.out_deferred_main_rt->m_render_targets[1];\n\t\t\tdata.ssao_input_data.NormalData.FullResNormalTextureSRV.GpuHandle = data.gpu_normal_handle.m_native.ptr;\n\n\t\t\tGFSDK_SSAO_CustomHeap custom_heap;\n\t\t\tcustom_heap.new_ = ::operator new;\n\t\t\tcustom_heap.delete_ = ::operator delete;\n\n\t\t\t// ao descriptor heap description\n\t\t\tGFSDK_SSAO_DescriptorHeaps_D3D12 DescriptorHeaps;\n\t\t\tDescriptorHeaps.CBV_SRV_UAV.pDescHeap = data.out_descriptor_heap_srv->m_native[0];\n\t\t\tDescriptorHeaps.CBV_SRV_UAV.BaseIndex = 2;\n\t\t\tDescriptorHeaps.RTV.pDescHeap = data.out_descriptor_heap_rtv->m_native[0];\n\t\t\tDescriptorHeaps.RTV.BaseIndex = 0;\n\n\t\t\t// initialize the ao context\n\t\t\tGFSDK_SSAO_Status status = GFSDK_SSAO_CreateContext_D3D12(n_device->m_native, 1, DescriptorHeaps, &data.ssao_context, &custom_heap);\n\t\t\tif (status != GFSDK_SSAO_Status::GFSDK_SSAO_OK)\n\t\t\t{\n\t\t\t\tLOGW(\"Failed to initialize the NVIDIA HBAO+ context.\")\n\t\t\t}\n#endif\n\t\t}\n\n\t\tinline void ExecuteHBAOTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n#ifdef NVIDIA_GAMEWORKS_HBAO\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<HBAOData>(handle);\n\t\t\tauto settings = fg.GetSettings<HBAOSettings>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tdata.ssao_input_data.DepthData.ProjectionMatrix.Data = GFSDK_SSAO_Float4x4((const GFSDK_SSAO_FLOAT*)& sg.GetActiveCamera()->m_projection);\n\t\t\tdata.ssao_input_data.DepthData.ProjectionMatrix.Layout = GFSDK_SSAO_ROW_MAJOR_ORDER;\n\t\t\tdata.ssao_input_data.NormalData.WorldToViewMatrix.Data = GFSDK_SSAO_Float4x4((const GFSDK_SSAO_FLOAT*)& sg.GetActiveCamera()->m_inverse_view);\n\t\t\tdata.ssao_input_data.NormalData.WorldToViewMatrix.Layout = GFSDK_SSAO_ROW_MAJOR_ORDER;\n\t\t\tdata.ssao_input_data.DepthData.MetersToViewSpaceUnits = settings.m_runtime.m_meters_to_view_space_units;\n\n\t\t\t// Set the viewport\n\t\t\tdata.ssao_input_data.DepthData.Viewport.Enable = true;\n\t\t\tdata.ssao_input_data.DepthData.Viewport.Height = static_cast<GFSDK_SSAO_UINT>(viewport.m_viewport.Height);\n\t\t\tdata.ssao_input_data.DepthData.Viewport.Width = static_cast<GFSDK_SSAO_UINT>(viewport.m_viewport.Width);\n\t\t\tdata.ssao_input_data.DepthData.Viewport.TopLeftX = static_cast<GFSDK_SSAO_UINT>(viewport.m_viewport.TopLeftX);\n\t\t\tdata.ssao_input_data.DepthData.Viewport.TopLeftY = static_cast<GFSDK_SSAO_UINT>(viewport.m_viewport.TopLeftY);\n\t\t\tdata.ssao_input_data.DepthData.Viewport.MinDepth = static_cast<GFSDK_SSAO_FLOAT>(viewport.m_viewport.MinDepth);\n\t\t\tdata.ssao_input_data.DepthData.Viewport.MaxDepth = static_cast<GFSDK_SSAO_FLOAT>(viewport.m_viewport.MaxDepth);\n\n\t\t\tGFSDK_SSAO_Parameters ao_parameters = {};\n\t\t\tao_parameters.Radius = settings.m_runtime.m_radius;\n\t\t\tao_parameters.Bias = settings.m_runtime.m_bias;\n\t\t\tao_parameters.PowerExponent = settings.m_runtime.m_power_exp;\n\t\t\tao_parameters.Blur.Enable = settings.m_runtime.m_enable_blur;\n\t\t\tao_parameters.Blur.Radius = GFSDK_SSAO_BLUR_RADIUS_4;\n\t\t\tao_parameters.Blur.Sharpness = settings.m_runtime.m_blur_sharpness;\n\t\t\tao_parameters.EnableDualLayerAO = false;\n\n\t\t\tGFSDK_SSAO_RenderMask render_mask = GFSDK_SSAO_RENDER_AO;\n\n\t\t\tGFSDK_SSAO_RenderTargetView_D3D12 rtv {};\n\t\t\trtv.pResource = n_render_target->m_render_targets[0];\n\t\t\trtv.CpuHandle = n_render_target->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart().ptr;\n\t\t\t\n\t\t\tGFSDK_SSAO_Output_D3D12 output;\n\t\t\toutput.pRenderTargetView = &rtv;\n\n\t\t\td3d12::Transition(cmd_list, n_render_target, ResourceState::COPY_SOURCE, ResourceState::RENDER_TARGET);\n\n\t\t\td3d12::BindDescriptorHeap(cmd_list, data.out_descriptor_heap_srv, DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, 0);\n\n\t\t\tGFSDK_SSAO_Status status = data.ssao_context->RenderAO(n_render_system.m_direct_queue->m_native, cmd_list->m_native, data.ssao_input_data, ao_parameters, output, render_mask);\n\t\t\tif (status != GFSDK_SSAO_Status::GFSDK_SSAO_OK)\n\t\t\t{\n\t\t\t\tLOGW(\"Failed to perform NVIDIA HBAO+\");\n\t\t\t}\n\n\t\t\td3d12::Transition(cmd_list, n_render_target, ResourceState::RENDER_TARGET, ResourceState::COPY_SOURCE);\n#endif\n\t\t}\n\n\t\tinline void DestroyHBAOTask(FrameGraph& fg, RenderTaskHandle handle, bool)\n\t\t{\n#ifdef NVIDIA_GAMEWORKS_HBAO\n\t\t\tauto& data = fg.GetData<HBAOData>(handle);\n\t\t\td3d12::Destroy(data.out_descriptor_heap_srv);\n\t\t\td3d12::Destroy(data.out_descriptor_heap_rtv);\n\n\t\t\tdelete data.ssao_context;\n#endif\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddHBAOTask(FrameGraph& frame_graph)\n\t{\n#ifndef NVIDIA_GAMEWORKS_HBAO\n\t\tLOGW(\"HBAO+ task has been added to the frame graph. But `NVIDIA_GAMEWORKS_HBAO` is not defined.\");\n#endif\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ d3d12::settings::back_buffer_format }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupHBAOTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteHBAOTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyHBAOTask(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<HBAOData>(desc, L\"NVIDIA HBAO+\", FG_DEPS<DeferredMainTaskData>());\n\t\tframe_graph.UpdateSettings<HBAOData>(HBAOSettings());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_imgui_render_task.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../window.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../imgui/ImGuizmo.h\"\n\n#include \"../imgui/imgui.hpp\"\n#include \"../imgui/imgui_impl_win32.hpp\"\n#include \"../imgui/imgui_impl_dx12.hpp\"\n\nnamespace wr\n{\n\n\tstruct ImGuiTaskData\n\t{\n\t\tstd::function<void(ImTextureID)> in_imgui_func;\n\t\tinline static d3d12::DescriptorHeap* out_descriptor_heap = nullptr;\n\t};\n\n\tnamespace internal\n\t{\n\n\t\tinline void SetupImGuiTask(RenderSystem& rs, FrameGraph&, RenderTaskHandle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\n\t\t\tif (!n_render_system.m_window.has_value())\n\t\t\t{\n\t\t\t\tLOGC(\"Tried using imgui without a window!\");\n\t\t\t}\n\n\t\t\tif (resize)\n\t\t\t{\n\t\t\t\tImGui_ImplDX12_CreateDeviceObjects();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (ImGui_ImplDX12_IsInitialized())\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\td3d12::desc::DescriptorHeapDesc heap_desc;\n\t\t\theap_desc.m_num_descriptors = 2;\n\t\t\theap_desc.m_shader_visible = true;\n\t\t\theap_desc.m_type = DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV;\n            ImGuiTaskData::out_descriptor_heap = d3d12::CreateDescriptorHeap(n_render_system.m_device, heap_desc);\n\t\t\tSetName(ImGuiTaskData::out_descriptor_heap, L\"ImGui Descriptor Heap\");\n\n\t\t\tIMGUI_CHECKVERSION();\n\t\t\tImGui::CreateContext();\n\t\t\tImGuiIO& io = ImGui::GetIO(); (void)io;\n\t\t\tio.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;       // Enable Keyboard Controls\n\t\t\tio.ConfigFlags |= ImGuiConfigFlags_DockingEnable;           // Enable Docking\n\t\t\tio.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;\t\t// Enable Gamepad Controls\n\t\t\t//io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;         // Enable Multi-Viewport / Platform Windows (FIXME: Currently broken in DX12 back-end, need some work!)\n\t\t\tio.ConfigDockingWithShift = true;\n\n\t\t\tImGui_ImplWin32_Init(n_render_system.m_window.value()->GetWindowHandle());\n\t\t\tImGui_ImplDX12_Init(n_render_system.m_device->m_native,\n\t\t\t\td3d12::settings::num_back_buffers,\n\t\t\t\t(DXGI_FORMAT)d3d12::settings::back_buffer_format,\n\t\t\t\td3d12::GetCPUHandle(ImGuiTaskData::out_descriptor_heap, 0 /* TODO: Solve versioning for ImGui */).m_native,\n\t\t\t\td3d12::GetGPUHandle(ImGuiTaskData::out_descriptor_heap, 0 /* TODO: Solve versioning for ImGui */).m_native);\n\n\t\t\tImGui::StyleColorsCherry();\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecuteImGuiTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<ImGuiTaskData>(handle);\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\n\t\t\t// Temp rendering\n\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t{\n\t\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\t\t// Create handle to the render target you want to display. and put it in descriptor slot 2.\n\t\t\t\tauto display_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\t\t\t\tauto cpu_handle = d3d12::GetCPUHandle(data.out_descriptor_heap, frame_idx, 1);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(display_rt, cpu_handle, 0, display_rt->m_create_info.m_rtv_formats[frame_idx]);\n\n\t\t\t\t// Prepare imgui\n\t\t\t\tImGui_ImplDX12_NewFrame();\n\t\t\t\tImGui_ImplWin32_NewFrame();\n\t\t\t\tImGui::NewFrame();\n\t\t\t\tImGuizmo::BeginFrame();\n\n\t\t\t\tdata.in_imgui_func(ImTextureID(d3d12::GetGPUHandle(data.out_descriptor_heap, frame_idx, 1).m_native.ptr));\n\n\t\t\t\t// Render imgui\n\t\t\t\td3d12::Transition(cmd_list, display_rt, wr::ResourceState::COPY_SOURCE, wr::ResourceState::PIXEL_SHADER_RESOURCE);\n\n\t\t\t\t//EXCEPTION CODE START\n\t\t\t\td3d12::BindDescriptorHeap(cmd_list, data.out_descriptor_heap, data.out_descriptor_heap->m_create_info.m_type, n_render_system.GetFrameIdx());\n\t\t\t\td3d12::BindDescriptorHeaps(cmd_list);\n\n\t\t\t\tfor (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i)\n\t\t\t\t{\n\t\t\t\t\tcmd_list->m_dynamic_descriptor_heaps[i]->CommitStagedDescriptorsForDraw(*cmd_list);\n\t\t\t\t}\n\t\t\t\t//EXCEPTION CODE END. Don't copy this outside of imgui render task. Since imgui doesn't use our Draw functions, this is needed to make it work with the dynamic descriptor heaps.\n\t\t\t\t\n\t\t\t\tImGui::Render();\n\t\t\t\tImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), cmd_list->m_native);\n\n\t\t\t\t// Update and Render additional Platform Windows (Beta-Viewport)\n\t\t\t\tif (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)\n\t\t\t\t{\n\t\t\t\t\tImGui::UpdatePlatformWindows();\n\t\t\t\t\tImGui::RenderPlatformWindowsDefault(NULL, (void*)cmd_list->m_native);\n\t\t\t\t}\n\n\t\t\t\td3d12::Transition(cmd_list, display_rt, wr::ResourceState::PIXEL_SHADER_RESOURCE, wr::ResourceState::COPY_SOURCE);\n\t\t\t}\n\t\t}\n\n\t\tinline void DestroyImGuiTask(FrameGraph&, RenderTaskHandle, bool resize)\n\t\t{\n\t\t\tif (resize)\n\t\t\t{\n\t\t\t\tImGui_ImplDX12_InvalidateDeviceObjects();\n\t\t\t}\n\t\t\telse if (ImGui_ImplDX12_IsInitialized())\n\t\t\t{\n\t\t\t\tImGui_ImplDX12_Shutdown();\n\t\t\t\tImGui_ImplWin32_Shutdown();\n\t\t\t\tImGui::DestroyContext();\n\t\t\t\tdelete ImGuiTaskData::out_descriptor_heap;\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\t[[nodiscard]] inline RenderTaskDesc GetImGuiTask(std::function<void(ImTextureID)> imgui_func)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(true),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(std::nullopt),\n\t\t\tRenderTargetProperties::FinishedResourceState(std::nullopt),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [imgui_func](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tfg.GetData<ImGuiTaskData>(handle).in_imgui_func = imgui_func;\n\t\t\tinternal::SetupImGuiTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteImGuiTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyImGuiTask(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::DIRECT;\n\t\tdesc.m_allow_multithreading = false;\n\n\t\treturn desc;\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/render_tasks/d3d12_path_tracer.hpp",
    "content": "/*!\r\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\r\n *\r\n * Licensed under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n *     http://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an \"AS IS\" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n */\r\n#pragma once\r\n\r\n#include \"../d3d12/d3d12_renderer.hpp\"\r\n#include \"../d3d12/d3d12_functions.hpp\"\r\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\r\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\r\n#include \"../frame_graph/frame_graph.hpp\"\r\n#include \"../scene_graph/camera_node.hpp\"\r\n#include \"../rt_pipeline_registry.hpp\"\r\n#include \"../root_signature_registry.hpp\"\r\n#include \"../engine_registry.hpp\"\r\n\r\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\r\n#include \"../render_tasks/d3d12_rt_shadow_task.hpp\"\r\n#include \"../render_tasks/d3d12_rt_reflection_task.hpp\"\r\n#include \"../render_tasks/d3d12_build_acceleration_structures.hpp\"\r\n#include \"../imgui_tools.hpp\"\r\n\r\nnamespace wr\r\n{\r\n\t//TODO, this struct is unpadded, manual padding might be usefull.\r\n\tstruct PathTracerData\r\n\t{\r\n\t\td3d12::AccelerationStructure out_tlas = {};\r\n\r\n\t\t// Shader tables\r\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> out_raygen_shader_table = { nullptr, nullptr, nullptr };\r\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> out_miss_shader_table = { nullptr, nullptr, nullptr };\r\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> out_hitgroup_shader_table = { nullptr, nullptr, nullptr };\r\n\r\n\t\t// Pipeline objects\r\n\t\td3d12::StateObject* out_state_object = nullptr;\r\n\r\n\t\t// Structures and buffers\r\n\t\tD3D12ConstantBufferHandle* out_cb_camera_handle = nullptr;\r\n\t\td3d12::RenderTarget* out_deferred_main_rt = nullptr;\r\n\r\n\t\tDirectX::XMVECTOR last_cam_pos = {};\r\n\t\tDirectX::XMVECTOR last_cam_rot = {};\r\n\r\n\t\tDescriptorAllocation out_output_alloc;\r\n\t\tDescriptorAllocation out_gbuffer_albedo_alloc;\r\n\t\tDescriptorAllocation out_gbuffer_normal_alloc;\r\n\t\tDescriptorAllocation out_gbuffer_emissive_alloc;\r\n\t\tDescriptorAllocation out_gbuffer_depth_alloc;\r\n\r\n\t\tbool tlas_requires_init = true;\r\n\t};\r\n\r\n\tnamespace internal\r\n\t{\r\n\r\n\t\tinline void CreateShaderTables(d3d12::Device* device, PathTracerData& data, int frame_idx)\r\n\t\t{\r\n\t\t\t// Delete existing shader table\r\n\t\t\tif (data.out_miss_shader_table[frame_idx])\r\n\t\t\t{\r\n\t\t\t\td3d12::Destroy(data.out_miss_shader_table[frame_idx]);\r\n\t\t\t}\r\n\t\t\tif (data.out_hitgroup_shader_table[frame_idx])\r\n\t\t\t{\r\n\t\t\t\td3d12::Destroy(data.out_hitgroup_shader_table[frame_idx]);\r\n\t\t\t}\r\n\t\t\tif (data.out_raygen_shader_table[frame_idx])\r\n\t\t\t{\r\n\t\t\t\td3d12::Destroy(data.out_raygen_shader_table[frame_idx]);\r\n\t\t\t}\r\n\r\n\t\t\t// Set up Raygen Shader Table\r\n\t\t\t{\r\n\t\t\t\t// Create Record(s)\r\n\t\t\t\tstd::uint32_t shader_record_count = 1;\r\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\r\n\t\t\t\tauto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"RaygenEntry\");\r\n\r\n\t\t\t\tauto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size);\r\n\r\n\t\t\t\t// Create Table\r\n\t\t\t\tdata.out_raygen_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\r\n\t\t\t\td3d12::AddShaderRecord(data.out_raygen_shader_table[frame_idx], shader_record);\r\n\t\t\t}\r\n\r\n\t\t\t// Set up Miss Shader Table\r\n\t\t\t{\r\n\t\t\t\t// Create Record(s)\r\n\t\t\t\tstd::uint32_t shader_record_count = 2;\r\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\r\n\r\n\t\t\t\tauto shadow_miss_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"ShadowMissEntry\");\r\n\t\t\t\tauto shadow_miss_record = d3d12::CreateShaderRecord(shadow_miss_identifier, shader_identifier_size);\r\n\r\n\t\t\t\tauto reflection_miss_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"ReflectionMiss\");\r\n\t\t\t\tauto reflection_miss_record = d3d12::CreateShaderRecord(reflection_miss_identifier, shader_identifier_size);\r\n\r\n\t\t\t\t// Create Table(s)\r\n\t\t\t\tdata.out_miss_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\r\n\t\t\t\td3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], reflection_miss_record);\r\n\t\t\t\td3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], shadow_miss_record);\r\n\t\t\t}\r\n\r\n\t\t\t// Set up Hit Group Shader Table\r\n\t\t\t{\r\n\t\t\t\t// Create Record(s)\r\n\t\t\t\tstd::uint32_t shader_record_count = 2;\r\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\r\n\r\n\t\t\t\tauto shadow_hit_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"ShadowHitGroup\");\r\n\t\t\t\tauto shadow_hit_record = d3d12::CreateShaderRecord(shadow_hit_identifier, shader_identifier_size);\r\n\r\n\t\t\t\tauto reflection_hit_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"ReflectionHitGroup\");\r\n\t\t\t\tauto reflection_hit_record = d3d12::CreateShaderRecord(reflection_hit_identifier, shader_identifier_size);\r\n\r\n\t\t\t\t// Create Table(s)\r\n\t\t\t\tdata.out_hitgroup_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\r\n\t\t\t\td3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], reflection_hit_record);\r\n\t\t\t\td3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], shadow_hit_record);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tinline void SetupPathTracerTask(RenderSystem & render_system, FrameGraph & fg, RenderTaskHandle & handle, bool resize)\r\n\t\t{\r\n\t\t\tif (fg.HasTask<RTShadowData>())\r\n\t\t\t{\r\n\t\t\t\tfg.WaitForPredecessorTask<RTShadowData>();\r\n\t\t\t}\r\n\t\t\tif (fg.HasTask<RTReflectionData>())\r\n\t\t\t{\r\n\t\t\t\tfg.WaitForPredecessorTask<RTReflectionData>();\r\n\t\t\t}\r\n\r\n\t\t\t// Initialize variables\r\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(render_system);\r\n\t\t\tauto& device = n_render_system.m_device;\r\n\t\t\tauto& data = fg.GetData<PathTracerData>(handle);\r\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\r\n\t\t\td3d12::SetName(n_render_target, L\"Path Tracing Render Target\");\r\n\r\n\t\t\tif (!resize)\r\n\t\t\t{\r\n\t\t\t\t// Get AS build data\r\n\t\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\r\n\r\n\t\t\t\tdata.out_output_alloc = std::move(as_build_data.out_allocator->Allocate());\r\n\t\t\t\tdata.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate());\r\n\t\t\t\tdata.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate());\r\n\t\t\t\tdata.out_gbuffer_emissive_alloc = std::move(as_build_data.out_allocator->Allocate());\r\n\t\t\t\tdata.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate());\r\n\r\n\t\t\t\tdata.tlas_requires_init = true;\r\n\t\t\t}\r\n\r\n\t\t\t// Versioning\r\n\t\t\tfor (int frame_idx = 0; frame_idx < 1; ++frame_idx)\r\n\t\t\t{\r\n\t\t\t\t// Bind output texture\r\n\t\t\t\td3d12::DescHeapCPUHandle rtv_handle = data.out_output_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, frame_idx, n_render_target->m_create_info.m_rtv_formats[frame_idx]);\r\n\r\n\t\t\t\t// Bind g-buffers (albedo, normal, depth)\r\n\t\t\t\tauto albedo_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle();\r\n\t\t\t\tauto normal_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle();\r\n\t\t\t\tauto emissive_handle = data.out_gbuffer_emissive_alloc.GetDescriptorHandle();\r\n\t\t\t\tauto depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle();\r\n\r\n\t\t\t\tauto deferred_main_rt = data.out_deferred_main_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<DeferredMainTaskData>());\r\n\r\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, albedo_handle, 0, deferred_main_rt->m_create_info.m_rtv_formats[0]);\r\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats[1]);\r\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, emissive_handle, 2, deferred_main_rt->m_create_info.m_rtv_formats[2]);\r\n\r\n\t\t\t\td3d12::CreateSRVFromDSV(deferred_main_rt, depth_handle);\r\n\t\t\t}\r\n\r\n\t\t\tif (!resize)\r\n\t\t\t{\r\n\t\t\t\t// Camera constant buffer\r\n\t\t\t\tdata.out_cb_camera_handle = static_cast<D3D12ConstantBufferHandle*>(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData)));\r\n\r\n\t\t\t\t// Pipeline State Object\r\n\t\t\t\tauto& rt_registry = RTPipelineRegistry::Get();\r\n\t\t\t\tdata.out_state_object = static_cast<d3d12::StateObject*>(rt_registry.Find(state_objects::path_tracer_state_object));\r\n\r\n\t\t\t\t// Create Shader Tables\r\n\t\t\t\tCreateShaderTables(device, data, 0);\r\n\t\t\t\tCreateShaderTables(device, data, 1);\r\n\t\t\t\tCreateShaderTables(device, data, 2);\r\n\t\t\t}\r\n\r\n\t\t}\r\n\r\n\t\tinline void ExecutePathTracerTask(RenderSystem& render_system, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle& handle)\r\n\t\t{\r\n\t\t\tif (fg.HasTask<RTShadowData>())\r\n\t\t\t{\r\n\t\t\t\tfg.WaitForPredecessorTask<RTShadowData>();\r\n\t\t\t}\r\n\t\t\tif (fg.HasTask<RTReflectionData>())\r\n\t\t\t{\r\n\t\t\t\tfg.WaitForPredecessorTask<RTReflectionData>();\r\n\t\t\t}\r\n\r\n\t\t\t// Initialize variables\r\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(render_system);\r\n\t\t\tauto window = n_render_system.m_window.value();\r\n\t\t\tauto render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\r\n\t\t\tauto device = n_render_system.m_device;\r\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\r\n\t\t\tauto& data = fg.GetData<PathTracerData>(handle);\r\n\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\r\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\r\n\r\n\t\t\t// Rebuild acceleratrion structure a 2e time for fallback\r\n\t\t\tif (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK)\r\n\t\t\t{\r\n\t\t\t\td3d12::CreateOrUpdateTLAS(device, cmd_list, data.tlas_requires_init, data.out_tlas, as_build_data.out_blas_list, frame_idx);\r\n\t\t\t}\r\n\r\n\t\t\t// Reset accmulation if nessessary\r\n\t\t\tif (DirectX::XMVector3Length(DirectX::XMVectorSubtract(scene_graph.GetActiveCamera()->m_position, data.last_cam_pos)).m128_f32[0] > 0.01)\r\n\t\t\t{\r\n\t\t\t\tdata.last_cam_pos = scene_graph.GetActiveCamera()->m_position;\r\n\t\t\t\tn_render_system.temp_rough = -1;\r\n\t\t\t}\r\n\r\n\t\t\tif (DirectX::XMVector3Length(DirectX::XMVectorSubtract(scene_graph.GetActiveCamera()->m_rotation_radians, data.last_cam_rot)).m128_f32[0] > 0.001)\r\n\t\t\t{\r\n\t\t\t\tdata.last_cam_rot = scene_graph.GetActiveCamera()->m_rotation_radians;\r\n\t\t\t\tn_render_system.temp_rough = -1;\r\n\t\t\t}\r\n\r\n\t\t\t// Wait for AS to be built\r\n\t\t\td3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx);\r\n\r\n\t\t\tif (n_render_system.m_render_window.has_value())\r\n\t\t\t{\r\n\t\t\t\td3d12::BindRaytracingPipeline(cmd_list, data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\r\n\r\n\t\t\t\t// Bind output, indices and materials, offsets, etc\r\n\t\t\t\tauto out_uav_handle = data.out_output_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::OUTPUT)), out_uav_handle);\r\n\r\n\t\t\t\tauto out_scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::INDICES)), out_scene_ib_handle);\r\n\r\n\t\t\t\tauto out_scene_mat_handle = as_build_data.out_scene_mat_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::MATERIALS)), out_scene_mat_handle);\r\n\r\n\t\t\t\tauto out_scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::OFFSETS)), out_scene_offset_handle);\r\n\r\n\t\t\t\tauto out_albedo_gbuffer_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::GBUFFERS)) + 0, out_albedo_gbuffer_handle);\r\n\r\n\t\t\t\tauto out_normal_gbuffer_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::GBUFFERS)) + 1, out_normal_gbuffer_handle);\r\n\r\n\t\t\t\tauto out_emissive_gbuffer_handle = data.out_gbuffer_emissive_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::GBUFFERS)) + 2, out_emissive_gbuffer_handle);\r\n\r\n\t\t\t\tauto out_scene_depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::GBUFFERS)) + 3, out_scene_depth_handle);\r\n\r\n\t\t\t\t/*\r\n\t\t\t\tTo keep the CopyDescriptors function happy, we need to fill the descriptor table with valid descriptors\r\n\t\t\t\tWe fill the table with a single descriptor, then overwrite some spots with the he correct textures\r\n\t\t\t\tIf a spot is unused, then a default descriptor will be still bound, but not used in the shaders.\r\n\t\t\t\tSince the renderer creates a texture pool that can be used by the render tasks, and\r\n\t\t\t\tthe texture pool also has default textures for albedo/roughness/etc... one of those textures is a good\r\n\t\t\t\tcandidate for this.\r\n\t\t\t\t*/\r\n\t\t\t\t{\r\n\t\t\t\t\tauto texture_handle = render_system.GetDefaultAlbedo();\r\n\t\t\t\t\tauto* texture_resource = static_cast<wr::d3d12::TextureResource*>(texture_handle.m_pool->GetTextureResource(texture_handle));\r\n\r\n\t\t\t\t\tsize_t num_textures_in_heap = COMPILATION_EVAL(rs_layout::GetSize(params::path_tracing, params::PathTracingE::TEXTURES));\r\n\t\t\t\t\tunsigned int heap_loc_start = COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::TEXTURES));\r\n\r\n\t\t\t\t\tfor (size_t i = 0; i < num_textures_in_heap; ++i)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, static_cast<std::uint32_t>(heap_loc_start + i), texture_resource);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// Fill descriptor heap with textures used by the scene\r\n\t\t\t\tfor (auto material_handle : as_build_data.out_material_handles)\r\n\t\t\t\t{\r\n\t\t\t\t\tauto* material_internal = material_handle.m_pool->GetMaterial(material_handle);\r\n\r\n\t\t\t\t\tauto set_srv = [&data, material_internal, cmd_list](auto texture_handle)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (!texture_handle.m_pool)\r\n\t\t\t\t\t\t\treturn;\r\n\r\n\t\t\t\t\t\tauto* texture_internal = static_cast<wr::d3d12::TextureResource*>(texture_handle.m_pool->GetTextureResource(texture_handle));\r\n\r\n\t\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES)) + static_cast<std::uint32_t>(texture_handle.m_id), texture_internal);\r\n\t\t\t\t\t};\r\n\r\n\t\t\t\t\tstd::array<TextureType, static_cast<size_t>(TextureType::COUNT)> types = { TextureType::ALBEDO, TextureType::NORMAL, \r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   TextureType::ROUGHNESS, TextureType::METALLIC, \r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   TextureType::EMISSIVE, TextureType::AO };\r\n\r\n\t\t\t\t\tfor (auto t : types)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif (material_internal->HasTexture(t))\r\n\t\t\t\t\t\t\tset_srv(material_internal->GetTexture(t));\r\n\t\t\t\t\t}\r\n\r\n\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// Get light buffer\r\n\t\t\t\tif (static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE)\r\n\t\t\t\t{\r\n\t\t\t\t\tstatic_cast<D3D12StructuredBufferPool*>(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tDescriptorAllocation light_alloc = std::move(as_build_data.out_allocator->Allocate());\r\n\t\t\t\td3d12::DescHeapCPUHandle light_handle = light_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::CreateSRVFromStructuredBuffer(static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native, light_handle, frame_idx);\r\n\r\n\t\t\t\td3d12::DescHeapCPUHandle light_handle2 = light_alloc.GetDescriptorHandle();\r\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::LIGHTS)), light_handle2);\r\n\r\n\t\t\t\t// Update offset data\r\n\t\t\t\tn_render_system.m_raytracing_offset_sb_pool->Update(as_build_data.out_sb_offset_handle, (void*)as_build_data.out_offsets.data(), sizeof(temp::RayTracingOffset_CBData) * as_build_data.out_offsets.size(), 0);\r\n\r\n\t\t\t\t// Update material data\r\n\t\t\t\tif (as_build_data.out_materials_require_update)\r\n\t\t\t\t{\r\n\t\t\t\t\tn_render_system.m_raytracing_material_sb_pool->Update(as_build_data.out_sb_material_handle, (void*)as_build_data.out_materials.data(), sizeof(temp::RayTracingMaterial_CBData) * as_build_data.out_materials.size(), 0);\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// Update camera constant buffer\r\n\t\t\t\tauto camera = scene_graph.GetActiveCamera();\r\n\t\t\t\ttemp::RTHybridCamera_CBData cam_data{};\r\n\t\t\t\tcam_data.m_inverse_view = DirectX::XMMatrixInverse(nullptr, camera->m_view);\r\n\t\t\t\tcam_data.m_inverse_projection = DirectX::XMMatrixInverse(nullptr, camera->m_projection);\r\n\t\t\t\tcam_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection);\r\n\t\t\t\tcam_data.m_intensity = n_render_system.temp_intensity;\r\n\t\t\t\tcam_data.m_frame_idx = ++n_render_system.temp_rough;\r\n\t\t\t\tn_render_system.m_camera_pool->Update(data.out_cb_camera_handle, sizeof(temp::RTHybridCamera_CBData), 0, frame_idx, (std::uint8_t*)&cam_data); // FIXME: Uhh wrong pool?\r\n\r\n\t\t\t\t// Make sure the convolution pass wrote to the skybox.\r\n\t\t\t\tfg.WaitForPredecessorTask<CubemapConvolutionTaskData>();\r\n\r\n                // Get skybox\r\n\t\t\t\tif (SkyboxNode *skybox = scene_graph.GetCurrentSkybox().get())\r\n\t\t\t\t{\r\n\t\t\t\t\tauto skybox_t = static_cast<d3d12::TextureResource*>(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value()));\r\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::SKYBOX)), skybox_t);\r\n\t\t\t\t\t\r\n\t\t\t\t\t// Get Pre-filtered environment\r\n\t\t\t\t\tauto irradiance_t = static_cast<d3d12::TextureResource*>(skybox->m_prefiltered_env_map->m_pool->GetTextureResource(skybox->m_prefiltered_env_map.value()));\r\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::PREF_ENV_MAP)), irradiance_t);\r\n\r\n\t\t\t\t\t// Get Environment Map\r\n\t\t\t\t\tirradiance_t = static_cast<d3d12::TextureResource*>(skybox->m_irradiance->m_pool->GetTextureResource(skybox->m_irradiance.value()));\r\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::IRRADIANCE_MAP)), irradiance_t);\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// Get brdf lookup texture\r\n\t\t\t\tauto brdf_lut_text = static_cast<d3d12::TextureResource*>(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value()));\r\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::BRDF_LUT)), brdf_lut_text);\r\n\r\n\r\n\t\t\t\t// Transition depth to NON_PIXEL_RESOURCE\r\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE);\r\n\r\n\t\t\t\td3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\r\n\t\t\t\td3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\r\n\t\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_camera_handle->m_native, 2, frame_idx);\r\n\r\n\t\t\t\tif (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE)\r\n\t\t\t\t{\r\n\t\t\t\t\td3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1);\r\n\t\t\t\t}\r\n\t\t\t\telse if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK)\r\n\t\t\t\t{\r\n\t\t\t\t\tcmd_list->m_native_fallback->SetTopLevelAccelerationStructure(0, as_build_data.out_tlas.m_fallback_tlas_ptr);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (!as_build_data.out_blas_list.empty())\r\n\t\t\t\t{\r\n\t\t\t\t\td3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3);\r\n\t\t\t\t}\r\n\r\n\t\t\t\t//#ifdef _DEBUG\r\n\t\t\t\tCreateShaderTables(device, data, frame_idx);\r\n\t\t\t\t//#endif\r\n\r\n\t\t\t\t// Dispatch hybrid ray tracing rays\r\n\t\t\t\td3d12::DispatchRays(\r\n\t\t\t\t\tcmd_list,\r\n\t\t\t\t\tdata.out_hitgroup_shader_table[frame_idx],\r\n\t\t\t\t\tdata.out_miss_shader_table[frame_idx],\r\n\t\t\t\t\tdata.out_raygen_shader_table[frame_idx],\r\n\t\t\t\t\td3d12::GetRenderTargetWidth(render_target),\r\n\t\t\t\t\td3d12::GetRenderTargetHeight(render_target),\r\n\t\t\t\t\t1,\r\n\t\t\t\t\tframe_idx);\r\n\r\n\t\t\t\t// Transition depth back to DEPTH_WRITE\r\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tinline void DestroyPathTracerTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\r\n\t\t{\r\n\t\t\tif(!resize)\r\n\t\t\t{\r\n\t\t\t\tPathTracerData& data = fg.GetData<PathTracerData>(handle);\r\n\r\n\t\t\t\tfor (d3d12::ShaderTable* shader : data.out_raygen_shader_table)\r\n\t\t\t\t{\r\n\t\t\t\t\tdelete shader;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tfor (d3d12::ShaderTable* shader : data.out_miss_shader_table)\r\n\t\t\t\t{\r\n\t\t\t\t\tdelete shader;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tfor (d3d12::ShaderTable* shader : data.out_hitgroup_shader_table)\r\n\t\t\t\t{\r\n\t\t\t\t\tdelete shader;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\r\n\t} /* internal */\r\n\r\n\tinline void AddPathTracerTask(FrameGraph& fg)\r\n\t{\r\n\t\tRenderTargetProperties rt_properties\r\n\t\t{\r\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\r\n\t\t\tRenderTargetProperties::Width(std::nullopt),\r\n\t\t\tRenderTargetProperties::Height(std::nullopt),\r\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\r\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\r\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\r\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\r\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_UNORM }),\r\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\r\n\t\t\tRenderTargetProperties::Clear(false),\r\n\t\t\tRenderTargetProperties::ClearDepth(false),\r\n\t\t};\r\n\r\n\t\tRenderTaskDesc desc;\r\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\r\n\t\t{\r\n\t\t\tinternal::SetupPathTracerTask(rs, fg, handle, resize);\r\n\t\t};\r\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\r\n\t\t{\r\n\t\t\tinternal::ExecutePathTracerTask(rs, fg, sg, handle);\r\n\t\t};\r\n\t\tdesc.m_destroy_func = [](FrameGraph & fg, RenderTaskHandle handle, bool resize)\r\n\t\t{\r\n\t\t\tinternal::DestroyPathTracerTask(fg, handle, resize);\r\n\t\t};\r\n\t\tdesc.m_properties = rt_properties;\r\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\r\n\t\tdesc.m_allow_multithreading = true;\r\n\r\n\t\tfg.AddTask<PathTracerData>(desc, L\"Path Traced Global Illumination\", FG_DEPS<DeferredMainTaskData>());\r\n\t}\r\n\r\n} /* wr */\r\n"
  },
  {
    "path": "src/render_tasks/d3d12_post_processing.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"d3d12_raytracing_task.hpp\"\n\nnamespace wr\n{\n\tstruct PostProcessingData\n\t{\n\t\td3d12::RenderTarget* out_source_rt = nullptr;\n\t\td3d12::PipelineState* out_pipeline = nullptr;\n\n\t\tDescriptorAllocator* out_allocator = nullptr;\n\t\tDescriptorAllocation out_allocation;\n\t};\n\n\tconst constexpr int versions = 1;\n\n\tnamespace internal\n\t{\n\n\t\ttemplate<typename T>\n\t\tinline void SetupPostProcessingTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<PostProcessingData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\t//Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors\n\t\t\tstd::shared_ptr<D3D12TexturePool> texture_pool = std::static_pointer_cast<D3D12TexturePool>(n_render_system.m_texture_pools[0]);\n\t\t\tif (!texture_pool)\n\t\t\t{\n\t\t\t\tLOGC(\"Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool\");\n\t\t\t}\n\n\n\t\t\tdata.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\tdata.out_allocation = data.out_allocator->Allocate(2);\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::post_processing));\n\n\t\t\tauto source_rt = data.out_source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::DEST);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::SOURCE);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t}\n\n\t\ttemplate<typename T>\n\t\tinline void ExecutePostProcessingTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<PostProcessingData>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.out_pipeline);\n\n\t\t\tauto source_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<T>());\n\n\t\t\t// Destination\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::DEST);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t// Source\n\t\t\t{\n\t\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::SOURCE);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t\t\n\t\t\tauto hdr_type = static_cast<float>(d3d12::settings::output_hdr);\n\t\t\td3d12::BindCompute32BitConstants(cmd_list, &hdr_type, 1, 0, 1);\n\n\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::DEST);\n\t\t\tauto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx);\n\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\n\t\t\tconstexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::SOURCE);\n\t\t\tauto handle_srv = data.out_allocation.GetDescriptorHandle(source_idx);\n\t\t\td3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_srv);\n\n\t\t\tcmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions]));\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void DestroyPostProcessing(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t}\n\n\t} /* internal */\n\n\ttemplate<typename T>\n\tinline void AddPostProcessingTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({d3d12::settings::back_buffer_format}),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupPostProcessingTask<T>(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecutePostProcessingTask<T>(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyPostProcessing(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<PostProcessingData>(desc, L\"Post Processing\");\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_raytracing_task.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../rt_pipeline_registry.hpp\"\n#include \"../root_signature_registry.hpp\"\n\n#include \"../render_tasks/d3d12_build_acceleration_structures.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"../scene_graph/skybox_node.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../imgui_tools.hpp\"\n#include \"../render_tasks/d3d12_cubemap_convolution.hpp\"\n\nnamespace wr\n{\n\tstruct RaytracingData\n\t{\n\t\td3d12::AccelerationStructure out_tlas;\n\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> out_raygen_shader_table = { nullptr, nullptr, nullptr };\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> out_miss_shader_table = { nullptr, nullptr, nullptr };\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> out_hitgroup_shader_table = { nullptr, nullptr, nullptr };\n\t\td3d12::StateObject* out_state_object = nullptr;\n\t\td3d12::RootSignature* out_root_signature = nullptr;\n\t\tD3D12ConstantBufferHandle* out_cb_camera_handle = nullptr;\n\n\t\tDescriptorAllocation out_uav_from_rtv;\n\n\t\tbool tlas_requires_init = false;\n\t\tDirectX::XMVECTOR last_cam_pos = {};\n\t\tDirectX::XMVECTOR last_cam_rot = {};\n\t};\n\n\tnamespace internal\n\t{\n\n\t\tinline void CreateShaderTables(d3d12::Device* device, RaytracingData& data, int frame_idx)\n\t\t{\n\t\t\tif (data.out_miss_shader_table[frame_idx])\n\t\t\t{\n\t\t\t\td3d12::Destroy(data.out_miss_shader_table[frame_idx]);\n\t\t\t}\n\t\t\tif (data.out_hitgroup_shader_table[frame_idx])\n\t\t\t{\n\t\t\t\td3d12::Destroy(data.out_hitgroup_shader_table[frame_idx]);\n\t\t\t}\n\t\t\tif (data.out_raygen_shader_table[frame_idx])\n\t\t\t{\n\t\t\t\td3d12::Destroy(data.out_raygen_shader_table[frame_idx]);\n\t\t\t}\n\n\t\t\t// Raygen Shader Table\n\t\t\t{\n\t\t\t\t// Create Record(s)\n\t\t\t\tstd::uint32_t shader_record_count = 1;\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\n\t\t\t\tauto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"RaygenEntry\");\n\n\t\t\t\tauto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size);\n\n\t\t\t\t// Create Table\n\t\t\t\tdata.out_raygen_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\n\t\t\t\td3d12::AddShaderRecord(data.out_raygen_shader_table[frame_idx], shader_record);\n\t\t\t}\n\n\t\t\t// Miss Shader Table\n\t\t\t{\n\t\t\t\t// Create Record(s)\n\t\t\t\tstd::uint32_t shader_record_count = 2;\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\n\n\t\t\t\tauto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"MissEntry\");\n\t\t\t\tauto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size);\n\n\t\t\t\tauto shadow_shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"ShadowMissEntry\");\n\t\t\t\tauto shadow_shader_record = d3d12::CreateShaderRecord(shadow_shader_identifier, shader_identifier_size);\n\n\t\t\t\t// Create Table\n\t\t\t\tdata.out_miss_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\n\n\t\t\t\td3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], shader_record);\n\t\t\t\td3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], shadow_shader_record);\n\t\t\t}\n\n\t\t\t// Hit Group Shader Table\n\t\t\t{\n\t\t\t\t// Create Record(s)\n\t\t\t\tstd::uint32_t shader_record_count = 2;\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\n\n\t\t\t\tauto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"MyHitGroup\");\n\t\t\t\tauto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size);\n\n\t\t\t\tauto shadow_shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, \"ShadowHitGroup\");\n\t\t\t\tauto shadow_shader_record = d3d12::CreateShaderRecord(shadow_shader_identifier, shader_identifier_size);\n\n\t\t\t\t// Create Table\n\t\t\t\tdata.out_hitgroup_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count,\n\t\t\t\t\tshader_identifier_size);\n\n\t\t\t\td3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], shader_record);\n\t\t\t\td3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], shadow_shader_record);\n\t\t\t}\n\t\t}\n\n\t\tinline void SetupRaytracingTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<RaytracingData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\td3d12::SetName(n_render_target, L\"Raytracing Target\");\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\t// Pipeline State Object\n\t\t\t\tauto& rt_registry = RTPipelineRegistry::Get();\n\t\t\t\tdata.out_state_object = static_cast<d3d12::StateObject*>(rt_registry.Find(state_objects::state_object));\n\n\t\t\t\t// Root Signature\n\t\t\t\tauto& rs_registry = RootSignatureRegistry::Get();\n\t\t\t\tdata.out_root_signature = static_cast<d3d12::RootSignature*>(rs_registry.Find(root_signatures::rt_test_global));\n\n\t\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\n\n\t\t\t\t// Camera constant buffer\n\t\t\t\tdata.out_cb_camera_handle = static_cast<D3D12ConstantBufferHandle*>(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RayTracingCamera_CBData)));\n\n\t\t\t\tdata.out_uav_from_rtv = std::move(as_build_data.out_allocator->Allocate());\n\n\t\t\t\tdata.tlas_requires_init = true;\n\n\t\t\t\tCreateShaderTables(device, data, 0);\n\t\t\t\tCreateShaderTables(device, data, 1);\n\t\t\t\tCreateShaderTables(device, data, 2);\n\t\t\t}\n\n\t\t\tfor (auto frame_idx = 0; frame_idx < 1; frame_idx++)\n\t\t\t{\n\t\t\t\td3d12::DescHeapCPUHandle desc_handle = data.out_uav_from_rtv.GetDescriptorHandle();\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, desc_handle, frame_idx, n_render_target->m_create_info.m_rtv_formats[frame_idx]);\n\t\t\t}\n\t\t}\n\n\t\tinline void ExecuteRaytracingTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto window = n_render_system.m_window.value();\n\t\t\tauto render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto device = n_render_system.m_device;\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tauto& data = fg.GetData<RaytracingData>(handle);\n\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\n\t\t\tfg.WaitForPredecessorTask<CubemapConvolutionTaskData>();\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tfloat scalar = 1.0f;\n\n\t\t\t// Rebuild acceleratrion structure a 2e time for fallback\n\t\t\tif (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\td3d12::CreateOrUpdateTLAS(device, cmd_list, data.tlas_requires_init, data.out_tlas, as_build_data.out_blas_list, frame_idx);\n\n\t\t\t\td3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx);\n\t\t\t}\n\n\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t{\n\n\t\t\t\td3d12::BindRaytracingPipeline(cmd_list, data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\n\n\t\t\t\tauto uav_from_rtv_handle = data.out_uav_from_rtv.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::OUTPUT)), uav_from_rtv_handle);\n\t\t\t\tauto scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::INDICES)), scene_ib_handle);\n\t\t\t\tauto scene_mat_handle = as_build_data.out_scene_mat_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::MATERIALS)), scene_mat_handle);\n\t\t\t\tauto scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::OFFSETS)), scene_offset_handle);\n\n\t\t\t\t// Reset accmulation if nessessary\n\t\t\t\tif (DirectX::XMVector3Length(DirectX::XMVectorSubtract(scene_graph.GetActiveCamera()->m_position, data.last_cam_pos)).m128_f32[0] > 0.01)\n\t\t\t\t{\n\t\t\t\t\tdata.last_cam_pos = scene_graph.GetActiveCamera()->m_position;\n\t\t\t\t\tn_render_system.temp_rough = -1;\n\t\t\t\t}\n\n\t\t\t\tif (DirectX::XMVector3Length(DirectX::XMVectorSubtract(scene_graph.GetActiveCamera()->m_rotation_radians, data.last_cam_rot)).m128_f32[0] > 0.001)\n\t\t\t\t{\n\t\t\t\t\tdata.last_cam_rot = scene_graph.GetActiveCamera()->m_rotation_radians;\n\t\t\t\t\tn_render_system.temp_rough = -1;\n\t\t\t\t}\n\n\t\t\t\t/*\n\t\t\t\tTo keep the CopyDescriptors function happy, we need to fill the descriptor table with valid descriptors\n\t\t\t\tWe fill the table with a single descriptor, then overwrite some spots with the he correct textures\n\t\t\t\tIf a spot is unused, then a default descriptor will be still bound, but not used in the shaders.\n\t\t\t\tSince the renderer creates a texture pool that can be used by the render tasks, and\n\t\t\t\tthe texture pool also has default textures for albedo/roughness/etc... one of those textures is a good\n\t\t\t\tcandidate for this.\n\t\t\t\t*/\n\t\t\t\t{\n\t\t\t\t\tauto texture_handle = n_render_system.GetDefaultAlbedo();\n\t\t\t\t\tauto* texture_resource = static_cast<wr::d3d12::TextureResource*>(texture_handle.m_pool->GetTextureResource(texture_handle));\n\n\t\t\t\t\tsize_t num_textures_in_heap = COMPILATION_EVAL(rs_layout::GetSize(params::full_raytracing, params::FullRaytracingE::TEXTURES));\n\t\t\t\t\tunsigned int heap_loc_start = COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::TEXTURES));\n\n\t\t\t\t\tfor (size_t i = 0; i < num_textures_in_heap; ++i)\n\t\t\t\t\t{\n\t\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, static_cast<std::uint32_t>(heap_loc_start + i), texture_resource);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Fill descriptor heap with textures used by the scene\n\t\t\t\tfor (auto material_handle : as_build_data.out_material_handles)\n\t\t\t\t{\n\t\t\t\t\tauto* material_internal = material_handle.m_pool->GetMaterial(material_handle);\n\n\t\t\t\t\tauto set_srv = [&data, material_internal, cmd_list](auto texture_handle)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto* texture_internal = static_cast<wr::d3d12::TextureResource*>(texture_handle.m_pool->GetTextureResource(texture_handle));\n\n\t\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::TEXTURES)) + static_cast<std::uint32_t>(texture_handle.m_id), texture_internal);\n\t\t\t\t\t};\n\n\t\t\t\t\tstd::array<TextureType, static_cast<size_t>(TextureType::COUNT)> types = { TextureType::ALBEDO, TextureType::NORMAL,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   TextureType::ROUGHNESS, TextureType::METALLIC,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   TextureType::EMISSIVE, TextureType::AO };\n\n\t\t\t\t\tfor (auto t : types)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (material_internal->HasTexture(t))\n\t\t\t\t\t\t\tset_srv(material_internal->GetTexture(t));\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Get light buffer\n\t\t\t\t{\n\t\t\t\t\tif (static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE)\n\t\t\t\t\t{\n\t\t\t\t\t\tstatic_cast<D3D12StructuredBufferPool*>(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\t}\n\n\t\t\t\t\tDescriptorAllocation light_alloc = std::move(as_build_data.out_allocator->Allocate());\n\t\t\t\t\td3d12::DescHeapCPUHandle light_handle = light_alloc.GetDescriptorHandle();\n\t\t\t\t\td3d12::CreateSRVFromStructuredBuffer(static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native, light_handle, frame_idx);\n\n\t\t\t\t\td3d12::DescHeapCPUHandle light_handle2 = light_alloc.GetDescriptorHandle();\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::LIGHTS)), light_handle2);\n\t\t\t\t}\n\n\n\n\t\t\t\t// Get skybox\n\t\t\t\tif (SkyboxNode* skybox = scene_graph.GetCurrentSkybox().get())\n\t\t\t\t{\n\t\t\t\t\tauto skybox_t = static_cast<d3d12::TextureResource*>(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value()));\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::SKYBOX)), skybox_t);\n\n\t\t\t\t\t// Get Environment Map\n\t\t\t\t\tauto irradiance_t = static_cast<d3d12::TextureResource*>(skybox->m_irradiance->m_pool->GetTextureResource(skybox->m_irradiance.value()));\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::IRRADIANCE_MAP)), irradiance_t);\n\t\t\t\t}\n\n\t\t\t\t{\n\t\t\t\t\t// Get brdf lookup texture\n\t\t\t\t\tauto brdf_lut_texture = static_cast<d3d12::TextureResource*>(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value()));\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::BRDF_LUT)), brdf_lut_texture);\n\t\t\t\t}\n\n\t\t\t\t// Update offset data\n\t\t\t\tn_render_system.m_raytracing_offset_sb_pool->Update(as_build_data.out_sb_offset_handle, (void*)as_build_data.out_offsets.data(), sizeof(temp::RayTracingOffset_CBData) * as_build_data.out_offsets.size(), 0);\n\n\t\t\t\t// Update material data\n\t\t\t\tif (as_build_data.out_materials_require_update)\n\t\t\t\t{\n\t\t\t\t\tn_render_system.m_raytracing_material_sb_pool->Update(as_build_data.out_sb_material_handle, (void*)as_build_data.out_materials.data(), sizeof(temp::RayTracingMaterial_CBData) * as_build_data.out_materials.size(), 0);\n\t\t\t\t}\n\n\t\t\t\t// Update camera constant buffer\n\t\t\t\tauto camera = scene_graph.GetActiveCamera();\n\t\t\t\ttemp::RayTracingCamera_CBData cam_data;\n\t\t\t\tcam_data.m_view = camera->m_view;\n\t\t\t\tcam_data.m_camera_position = camera->m_position;\n\t\t\t\tcam_data.m_inverse_view_projection = DirectX::XMMatrixTranspose(DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection));\n\t\t\t\tcam_data.focal_radius = camera->m_f_number;\n\t\t\t\tcam_data.focal_length = camera->m_focal_length;\n\t\t\t\tcam_data.frame_idx = ++n_render_system.temp_rough;\n\t\t\t\tcam_data.intensity = n_render_system.temp_intensity;\n\t\t\t\tn_render_system.m_camera_pool->Update(data.out_cb_camera_handle, sizeof(temp::RayTracingCamera_CBData), 0, frame_idx, (std::uint8_t*)&cam_data); // FIXME: Uhh wrong pool?\n\n\t\t\t\td3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\n\t\t\t\td3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\n\t\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_camera_handle->m_native, 2, frame_idx);\n\n\t\t\t\tif (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t\t\t{\n\t\t\t\t\td3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1);\n\t\t\t\t}\n\t\t\t\telse if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t\t{\n\t\t\t\t\tcmd_list->m_native_fallback->SetTopLevelAccelerationStructure(0, as_build_data.out_tlas.m_fallback_tlas_ptr);\n\t\t\t\t}\n\n\t\t\t\t/*unsigned int verts_loc = rs_layout::GetHeapLoc(params::FullRaytracingE, params::FullRaytracingE::VERTICES);\n\t\t\t\tThis should be the Parameter index not the heap location, it was only working due to a ridiculous amount of luck and should be fixed, or we completely missunderstand this stuff...\n\t\t\t\tMuch love, Meine and Florian*/\n\t\t\t\tif (!as_build_data.out_blas_list.empty())\n\t\t\t\t{\n\t\t\t\t\td3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3);\n\t\t\t\t}\n\n//#ifdef _DEBUG\n\t\t\t\tCreateShaderTables(device, data, frame_idx);\n//#endif\n\t\t\t\td3d12::DispatchRays(cmd_list,\n\t\t\t\t\tdata.out_hitgroup_shader_table[frame_idx], \n\t\t\t\t\tdata.out_miss_shader_table[frame_idx], \n\t\t\t\t\tdata.out_raygen_shader_table[frame_idx], \n\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(d3d12::GetRenderTargetWidth(render_target))),\n\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(d3d12::GetRenderTargetHeight(render_target))),\n\t\t\t\t\t1, \n\t\t\t\t\t0);\n\t\t\t}\n\t\t}\n\n\t\tinline void DestroyRaytracingTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif(!resize)\n\t\t\t{\n\t\t\t\tRaytracingData& data = fg.GetData<RaytracingData>(handle);\n\n\t\t\t\tfor (d3d12::ShaderTable* shader : data.out_raygen_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\n\t\t\t\tfor (d3d12::ShaderTable* shader : data.out_miss_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\n\t\t\t\tfor (d3d12::ShaderTable* shader : data.out_hitgroup_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddRaytracingTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_UNORM }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupRaytracingTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteRaytracingTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyRaytracingTask(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<RaytracingData>(desc, L\"Full Raytracing\");\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/render_tasks/d3d12_reflection_denoiser.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include \"../render_tasks/d3d12_spatial_reconstruction.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_rt_reflection_task.hpp\"\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n\nnamespace wr\n{\n\tstruct ReflectionDenoiserData\n\t{\n\t\td3d12::PipelineState* m_temporal_denoiser_pipeline;\n\t\td3d12::PipelineState* m_variance_estimator_pipeline;\n\t\td3d12::PipelineState* m_spatial_denoiser_pipeline;\n\n\t\tDescriptorAllocator* m_descriptor_allocator;\n\t\tDescriptorAllocation m_input_allocation;\n\t\tDescriptorAllocation m_ray_raw_allocation;\n\t\tDescriptorAllocation m_ray_dir_allocation;\n\t\tDescriptorAllocation m_albedo_roughness_allocation;\n\t\tDescriptorAllocation m_normal_metallic_allocation;\n\t\tDescriptorAllocation m_motion_allocation;\n\t\tDescriptorAllocation m_linear_depth_allocation;\n\t\tDescriptorAllocation m_world_pos_allocation;\n\n\t\tDescriptorAllocation m_in_history_allocation;\n\n\t\tDescriptorAllocation m_accum_allocation;\n\t\tDescriptorAllocation m_prev_normal_allocation;\n\t\tDescriptorAllocation m_prev_depth_allocation;\n\t\tDescriptorAllocation m_in_moments_allocation;\n\n\t\tDescriptorAllocation m_output_allocation;\n\t\tDescriptorAllocation m_out_history_allocation;\n\t\tDescriptorAllocation m_out_moments_allocation;\n\n\t\tDescriptorAllocation m_ping_pong_allocation;\n\n\t\td3d12::RenderTarget* m_spatial_reconstruction_render_target;\n\t\td3d12::RenderTarget* m_rt_reflection_render_target;\n\t\td3d12::RenderTarget* m_gbuffer_render_target;\n\t\td3d12::RenderTarget* m_output_render_target;\n\t\td3d12::RenderTarget* m_prev_data_render_target;\n\n\t\td3d12::RenderTarget* m_in_history_render_target;\n\t\td3d12::RenderTarget* m_out_history_render_target;\n\t\t\n\t\td3d12::RenderTarget* m_in_moments_render_target;\n\t\td3d12::RenderTarget* m_out_moments_render_target;\n\n\t\td3d12::RenderTarget* m_ping_pong_render_target;\n\n\t\ttemp::ReflectionDenoiserSettings_CBData m_denoiser_settings;\n\n\t\tstd::shared_ptr<ConstantBufferPool> m_constant_buffer_pool;\n\t\tConstantBufferHandle* m_denoiser_settings_buffer;\n\t\tstd::array<ConstantBufferHandle*, d3d12::settings::shadow_denoiser_wavelet_iterations> m_wavelet_sizes;\n\t};\n\n\tnamespace internal\n\t{\n\t\tinline void SetupReflectionDenoiserTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<ReflectionDenoiserData>(handle);\n\n\t\t\tdata.m_output_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tdata.m_spatial_reconstruction_render_target = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<SpatialReconstructionData>());\n\t\t\tdata.m_rt_reflection_render_target = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<RTReflectionData>());\n\t\t\tdata.m_gbuffer_render_target = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<DeferredMainTaskData>());\n\n\t\t\t//Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors\n\t\t\tstd::shared_ptr<D3D12TexturePool> texture_pool = std::static_pointer_cast<D3D12TexturePool>(n_render_system.m_texture_pools[0]);\n\t\t\tif (!texture_pool)\n\t\t\t{\n\t\t\t\tLOGC(\"Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool\");\n\t\t\t}\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.m_descriptor_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\n\t\t\t\tdata.m_input_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_ray_raw_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_ray_dir_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_albedo_roughness_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_normal_metallic_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_motion_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_linear_depth_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_world_pos_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\n\t\t\t\tdata.m_in_history_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\n\t\t\t\tdata.m_accum_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_prev_normal_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_prev_depth_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_in_moments_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\n\t\t\t\tdata.m_output_allocation = std::move(data.m_descriptor_allocator->Allocate(2));\n\t\t\t\tdata.m_out_history_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\tdata.m_out_moments_allocation = std::move(data.m_descriptor_allocator->Allocate());\n\t\t\t\t\n\t\t\t\tdata.m_ping_pong_allocation = std::move(data.m_descriptor_allocator->Allocate(2));\n\n\t\t\t\tdata.m_constant_buffer_pool = n_render_system.CreateConstantBufferPool(\n\t\t\t\t\tSizeAlignTwoPower(sizeof(temp::ReflectionDenoiserSettings_CBData), 256) * d3d12::settings::num_back_buffers + \n\t\t\t\t\tSizeAlignTwoPower(sizeof(float), 256) * d3d12::settings::num_back_buffers * d3d12::settings::shadow_denoiser_wavelet_iterations);\n\t\t\t\tdata.m_denoiser_settings_buffer = data.m_constant_buffer_pool->Create(sizeof(temp::ReflectionDenoiserSettings_CBData));\n\n\t\t\t\tdata.m_denoiser_settings = {};\n\t\t\t\tdata.m_denoiser_settings.m_color_integration_alpha = 0.04f;\n\t\t\t\tdata.m_denoiser_settings.m_moments_integration_alpha = 0.2f;\n\t\t\t\tdata.m_denoiser_settings.m_variance_clipping_sigma = 4.0f;\n\t\t\t\tdata.m_denoiser_settings.m_roughness_reprojection_threshold = 0.2f;\n\t\t\t\tdata.m_denoiser_settings.m_max_history_samples = 48;\n\t\t\t\tdata.m_denoiser_settings.m_n_phi = 128.f;\n\t\t\t\tdata.m_denoiser_settings.m_z_phi = 1.f;\n\t\t\t\tdata.m_denoiser_settings.m_l_phi = 4.f;\n\n\t\t\t\tfor (int i = 0; i < data.m_wavelet_sizes.size(); ++i)\n\t\t\t\t{\n\t\t\t\t\tdata.m_wavelet_sizes[i] = data.m_constant_buffer_pool->Create(sizeof(float));\n\n\t\t\t\t\tfloat size = 1 << i;\n\t\t\t\t\tfor (int j = 0; j < d3d12::settings::num_back_buffers; ++j)\n\t\t\t\t\t{\n\t\t\t\t\t\tdata.m_constant_buffer_pool->Update(data.m_wavelet_sizes[i], sizeof(float), 0, j, (std::uint8_t*)&size);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\n\t\t\td3d12::desc::RenderTargetDesc render_target_desc = {};\n\t\t\trender_target_desc.m_clear_color[0] = 0.f;\n\t\t\trender_target_desc.m_clear_color[1] = 0.f;\n\t\t\trender_target_desc.m_clear_color[2] = 0.f;\n\t\t\trender_target_desc.m_clear_color[3] = 0.f;\n\t\t\trender_target_desc.m_create_dsv_buffer = false;\n\t\t\trender_target_desc.m_dsv_format = Format::UNKNOWN;\n\t\t\trender_target_desc.m_initial_state = ResourceState::NON_PIXEL_SHADER_RESOURCE;\n\t\t\trender_target_desc.m_num_rtv_formats = 3;\n\t\t\trender_target_desc.m_rtv_formats[0] = data.m_spatial_reconstruction_render_target->m_create_info.m_rtv_formats[0];\n\t\t\trender_target_desc.m_rtv_formats[1] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[1];\n\t\t\trender_target_desc.m_rtv_formats[2] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[4];\n\n\t\t\tdata.m_prev_data_render_target = d3d12::CreateRenderTarget(n_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc);\n\n\t\t\trender_target_desc.m_num_rtv_formats = 1;\n\t\t\trender_target_desc.m_rtv_formats[0] = Format::R16_FLOAT;\n\n\t\t\tdata.m_in_history_render_target = d3d12::CreateRenderTarget(n_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = Format::R16G16_FLOAT;\n\n\t\t\tdata.m_in_moments_render_target = d3d12::CreateRenderTarget(n_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = data.m_output_render_target->m_create_info.m_rtv_formats[0];\n\n\t\t\tdata.m_ping_pong_render_target = d3d12::CreateRenderTarget(n_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc);\n\n\t\t\trender_target_desc.m_initial_state = ResourceState::UNORDERED_ACCESS;\n\t\t\trender_target_desc.m_num_rtv_formats = 1;\n\t\t\trender_target_desc.m_rtv_formats[0] = data.m_in_history_render_target->m_create_info.m_rtv_formats[0];\n\n\t\t\tdata.m_out_history_render_target = d3d12::CreateRenderTarget(n_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = data.m_in_moments_render_target->m_create_info.m_rtv_formats[0];\n\n\t\t\tdata.m_out_moments_render_target = d3d12::CreateRenderTarget(n_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc);\n\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_input_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_spatial_reconstruction_render_target, cpu_handle, 0, data.m_spatial_reconstruction_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_ray_raw_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_rt_reflection_render_target, cpu_handle, 0, data.m_rt_reflection_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_ray_dir_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_rt_reflection_render_target, cpu_handle, 2, data.m_rt_reflection_render_target->m_create_info.m_rtv_formats[2]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_albedo_roughness_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 0, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_normal_metallic_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 1, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_motion_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 3, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[3]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_linear_depth_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 4, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[4]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_world_pos_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 5, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[5]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_in_history_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_in_history_render_target, cpu_handle, 0, data.m_in_history_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_accum_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_prev_data_render_target, cpu_handle, 0, data.m_prev_data_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_prev_normal_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_prev_data_render_target, cpu_handle, 1, data.m_prev_data_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_prev_depth_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_prev_data_render_target, cpu_handle, 2, data.m_prev_data_render_target->m_create_info.m_rtv_formats[2]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_in_moments_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_in_moments_render_target, cpu_handle, 0, data.m_in_moments_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_output_allocation.GetDescriptorHandle(0);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(data.m_output_render_target, cpu_handle, 0, data.m_output_render_target->m_create_info.m_rtv_formats[0]);\n\n\t\t\t\tcpu_handle = data.m_output_allocation.GetDescriptorHandle(1);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_output_render_target, cpu_handle, 0, data.m_output_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_out_history_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(data.m_out_history_render_target, cpu_handle, 0, data.m_out_history_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_out_moments_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(data.m_out_moments_render_target, cpu_handle, 0, data.m_out_moments_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(0);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(data.m_ping_pong_render_target, cpu_handle, 0, data.m_ping_pong_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t\t\n\t\t\t\tcpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(1);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_ping_pong_render_target, cpu_handle, 0, data.m_ping_pong_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.m_temporal_denoiser_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::reflection_temporal_denoiser);\n\t\t\tdata.m_variance_estimator_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::reflection_variance_estimator);\n\t\t\tdata.m_spatial_denoiser_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::reflection_spatial_denoiser);\n\t\t}\n\n\t\tinline void BindRenderTargets(ReflectionDenoiserData& data, d3d12::CommandList* cmd_list)\n\t\t{\n\t\t\t{\n\t\t\t\tconstexpr unsigned int input = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::INPUT);\n\t\t\t\tauto cpu_handle = data.m_input_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int ray_raw = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::RAY_RAW);\n\t\t\t\tauto cpu_handle = data.m_ray_raw_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, ray_raw, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int ray_dir = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::RAY_DIR);\n\t\t\t\tauto cpu_handle = data.m_ray_dir_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, ray_dir, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int albedo_roughness = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::ALBEDO_ROUGHNESS);\n\t\t\t\tauto cpu_handle = data.m_albedo_roughness_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, albedo_roughness, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int normal_metallic = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::NORMAL_METALLIC);\n\t\t\t\tauto cpu_handle = data.m_normal_metallic_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, normal_metallic, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int motion = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::MOTION);\n\t\t\t\tauto cpu_handle = data.m_motion_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, motion, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int linear_depth = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::LINEAR_DEPTH);\n\t\t\t\tauto cpu_handle = data.m_linear_depth_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, linear_depth, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int world_pos = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::WORLD_POS);\n\t\t\t\tauto cpu_handle = data.m_world_pos_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, world_pos, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int in_hist = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::IN_HISTORY);\n\t\t\t\tauto cpu_handle = data.m_in_history_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, in_hist, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int accum = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::ACCUM);\n\t\t\t\tauto cpu_handle = data.m_accum_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, accum, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_normal = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::PREV_NORMAL);\n\t\t\t\tauto cpu_handle = data.m_prev_normal_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, prev_normal, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_depth = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::PREV_DEPTH);\n\t\t\t\tauto cpu_handle = data.m_prev_depth_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, prev_depth, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int in_moments = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::IN_MOMENTS);\n\t\t\t\tauto cpu_handle = data.m_in_moments_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, in_moments, cpu_handle);\n\t\t\t}\n\t\t\t{\n\t\t\t\tconstexpr unsigned int output = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUTPUT);\n\t\t\t\tauto cpu_handle = data.m_output_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, output, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int out_hist = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUT_HISTORY);\n\t\t\t\tauto cpu_handle = data.m_out_history_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, out_hist, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int out_moments = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUT_MOMENTS);\n\t\t\t\tauto cpu_handle = data.m_out_moments_allocation.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, out_moments, cpu_handle);\n\t\t\t}\n\t\t}\n\n\t\tinline void TemporalDenoiser(D3D12RenderSystem& n_render_system, SceneGraph& sg, ReflectionDenoiserData& data, d3d12::CommandList* cmd_list)\n\t\t{\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.m_temporal_denoiser_pipeline);\n\n\t\t\tBindRenderTargets(data, cmd_list);\n\n\t\t\td3d12::HeapResource* camera_buffer = static_cast<D3D12ConstantBufferHandle*>(sg.GetActiveCamera()->m_camera_cb)->m_native;\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, camera_buffer, 1, frame_idx);\n\n\t\t\td3d12::HeapResource* denoiser_settings = static_cast<D3D12ConstantBufferHandle*>(data.m_denoiser_settings_buffer)->m_native;\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, denoiser_settings, 2, frame_idx);\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tuint32_t(std::ceil(viewport.m_viewport.Width / 16.f)),\n\t\t\t\tuint32_t(std::ceil(viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void VarianceEstimator(D3D12RenderSystem& n_render_system, SceneGraph& sg, ReflectionDenoiserData& data, d3d12::CommandList* cmd_list)\n\t\t{\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.m_variance_estimator_pipeline);\n\n\t\t\tBindRenderTargets(data, cmd_list);\n\n\t\t\td3d12::HeapResource* camera_buffer = static_cast<D3D12ConstantBufferHandle*>(sg.GetActiveCamera()->m_camera_cb)->m_native;\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, camera_buffer, 1, frame_idx);\n\n\t\t\td3d12::HeapResource* denoiser_settings = static_cast<D3D12ConstantBufferHandle*>(data.m_denoiser_settings_buffer)->m_native;\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, denoiser_settings, 2, frame_idx);\n\n\t\t\td3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int input = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::INPUT);\n\t\t\t\tauto cpu_handle = data.m_output_allocation.GetDescriptorHandle(1);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle);\n\t\t\t}\n\n\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int output = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUTPUT);\n\t\t\t\tauto cpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(0);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, output, cpu_handle);\n\t\t\t}\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tuint32_t(std::ceil(viewport.m_viewport.Width / 16.f)),\n\t\t\t\tuint32_t(std::ceil(viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void SpatialDenoiser(D3D12RenderSystem& n_render_system, SceneGraph& sg, ReflectionDenoiserData& data, d3d12::CommandList* cmd_list)\n\t\t{\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.m_spatial_denoiser_pipeline);\n\n\t\t\tBindRenderTargets(data, cmd_list);\n\n\t\t\td3d12::HeapResource* camera_buffer = static_cast<D3D12ConstantBufferHandle*>(sg.GetActiveCamera()->m_camera_cb)->m_native;\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, camera_buffer, 1, frame_idx);\n\n\t\t\td3d12::HeapResource* denoiser_settings = static_cast<D3D12ConstantBufferHandle*>(data.m_denoiser_settings_buffer)->m_native;\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, denoiser_settings, 2, frame_idx);\n\n\t\t\tfor (int i = 0; i < data.m_wavelet_sizes.size(); ++i)\n\t\t\t{\n\t\t\t\td3d12::HeapResource* wavelet_size = static_cast<D3D12ConstantBufferHandle*>(data.m_wavelet_sizes[i])->m_native;\n\t\t\t\td3d12::BindComputeConstantBuffer(cmd_list, wavelet_size, 3, frame_idx);\n\n\t\t\t\tif (i % 2 != 0)\n\t\t\t\t{\n\t\t\t\t\td3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t\t\t{\n\t\t\t\t\t\tconstexpr unsigned int input = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::INPUT);\n\t\t\t\t\t\tauto cpu_handle = data.m_output_allocation.GetDescriptorHandle(1);\n\t\t\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle);\n\t\t\t\t\t}\n\n\t\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS);\n\n\t\t\t\t\t{\n\t\t\t\t\t\tconstexpr unsigned int output = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUTPUT);\n\t\t\t\t\t\tauto cpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(0);\n\t\t\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, output, cpu_handle);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t\t\t{\n\t\t\t\t\t\tconstexpr unsigned int input = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::INPUT);\n\t\t\t\t\t\tauto cpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(1);\n\t\t\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle);\n\t\t\t\t\t}\n\n\t\t\t\t\td3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS);\n\n\t\t\t\t\t{\n\t\t\t\t\t\tconstexpr unsigned int output = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUTPUT);\n\t\t\t\t\t\tauto cpu_handle = data.m_output_allocation.GetDescriptorHandle(0);\n\t\t\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, output, cpu_handle);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\t\tuint32_t(std::ceil(viewport.m_viewport.Width / 16.f)),\n\t\t\t\t\tuint32_t(std::ceil(viewport.m_viewport.Height / 16.f)),\n\t\t\t\t\t1);\n\n\t\t\t\tif (i == d3d12::settings::shadow_denoiser_feedback_tap)\n\t\t\t\t{\n\t\t\t\t\tif (i % 2 != 0)\n\t\t\t\t\t{\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\n\t\t\t\t\t\tcmd_list->m_native->CopyResource(data.m_prev_data_render_target->m_render_targets[0], data.m_ping_pong_render_target->m_render_targets[0]);\n\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\n\t\t\t\t\t\tcmd_list->m_native->CopyResource(data.m_prev_data_render_target->m_render_targets[0], data.m_output_render_target->m_render_targets[0]);\n\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (data.m_wavelet_sizes.size() % 2 != 0)\n\t\t\t{\n\t\t\t\td3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\n\t\t\t\tcmd_list->m_native->CopyResource(data.m_output_render_target->m_render_targets[0], data.m_ping_pong_render_target->m_render_targets[0]);\n\n\t\t\t\td3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::COPY_DEST, ResourceState::UNORDERED_ACCESS);\n\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::COPY_SOURCE, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t}\n\t\t}\t\t\n\n\t\tinline void ExecuteReflectionDenoiserTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& n_device = n_render_system.m_device->m_native;\n\t\t\tauto& data = fg.GetData<ReflectionDenoiserData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tfg.WaitForPredecessorTask<SpatialReconstructionData>();\n\n\t\t\tdata.m_constant_buffer_pool->Update(data.m_denoiser_settings_buffer, sizeof(data.m_denoiser_settings), 0, frame_idx, (uint8_t*)&data.m_denoiser_settings);\n\n\t\t\td3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\n\t\t\tTemporalDenoiser(n_render_system, sg, data, cmd_list);\n\n\t\t\td3d12::Transition(cmd_list, data.m_gbuffer_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_SOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_rt_reflection_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_SOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\t\t\td3d12::Transition(cmd_list, data.m_in_history_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\t\t\td3d12::Transition(cmd_list, data.m_out_history_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_in_moments_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\t\t\td3d12::Transition(cmd_list, data.m_out_moments_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\n\t\t\tcmd_list->m_native->CopyResource(data.m_prev_data_render_target->m_render_targets[1], data.m_gbuffer_render_target->m_render_targets[1]);\n\t\t\tcmd_list->m_native->CopyResource(data.m_prev_data_render_target->m_render_targets[2], data.m_gbuffer_render_target->m_render_targets[4]);\n\t\t\tcmd_list->m_native->CopyResource(data.m_in_history_render_target->m_render_targets[0], data.m_out_history_render_target->m_render_targets[0]);\n\t\t\tcmd_list->m_native->CopyResource(data.m_in_moments_render_target->m_render_targets[0], data.m_out_moments_render_target->m_render_targets[0]);\n\n\t\t\td3d12::Transition(cmd_list, data.m_gbuffer_render_target, ResourceState::COPY_SOURCE, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_rt_reflection_render_target, ResourceState::COPY_SOURCE, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_in_history_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_out_history_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\t\t\td3d12::Transition(cmd_list, data.m_in_moments_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_out_moments_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\n\t\t\tVarianceEstimator(n_render_system, sg, data, cmd_list);\n\n\t\t\tSpatialDenoiser(n_render_system, sg, data, cmd_list);\n\n\t\t\td3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\t\t}\n\n\t\tinline void DestroyReflectionDenoiserTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& data = fg.GetData<ReflectionDenoiserData>(handle);\n\t\t\t\n\t\t\td3d12::Destroy(data.m_prev_data_render_target);\n\t\t\td3d12::Destroy(data.m_in_history_render_target);\n\t\t\td3d12::Destroy(data.m_out_history_render_target);\n\t\t\td3d12::Destroy(data.m_in_moments_render_target);\n\t\t\td3d12::Destroy(data.m_out_moments_render_target);\n\t\t\td3d12::Destroy(data.m_ping_pong_render_target);\n\n\n\t\t}\n\t} /* internal */\n\n\tinline void AddReflectionDenoiserTask(FrameGraph& frame_graph)\n\t{\n\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t  RenderTargetProperties::IsRenderWindow(false),\n\t\t  RenderTargetProperties::Width(std::nullopt),\n\t\t  RenderTargetProperties::Height(std::nullopt),\n\t\t  RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t  RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t  RenderTargetProperties::CreateDSVBuffer(false),\n\t\t  RenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t  RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }),\n\t\t  RenderTargetProperties::NumRTVFormats(1),\n\t\t  RenderTargetProperties::Clear(false),\n\t\t  RenderTargetProperties::ClearDepth(false),\n\t\t  RenderTargetProperties::ResolutionScalar(1.f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupReflectionDenoiserTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteReflectionDenoiserTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::DestroyReflectionDenoiserTask(fg, handle, resize);\n\t\t};\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<ReflectionDenoiserData>(desc, L\"Reflection denoiser\", FG_DEPS<SpatialReconstructionData>());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/render_tasks/d3d12_rt_hybrid_helpers.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"d3d12_deferred_main.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../rt_pipeline_registry.hpp\"\n#include \"../root_signature_registry.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_build_acceleration_structures.hpp\"\n#include \"../render_tasks/d3d12_cubemap_convolution.hpp\"\n#include \"../imgui_tools.hpp\"\n\n#include <string>\n\nnamespace wr\n{\n\tstruct RTHybrid_BaseData\n\t{\n\t\td3d12::AccelerationStructure out_tlas = {};\n\n\t\t// Shader tables\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> out_raygen_shader_table = { nullptr, nullptr, nullptr };\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> out_miss_shader_table = { nullptr, nullptr, nullptr };\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> out_hitgroup_shader_table = { nullptr, nullptr, nullptr };\n\n\n\t\t// Pipeline objects\n\t\td3d12::StateObject* out_state_object = nullptr;\n\t\td3d12::RootSignature* out_root_signature = nullptr;\n\n\t\t// Structures and buffers\n\t\tD3D12ConstantBufferHandle* out_cb_camera_handle = nullptr;\n\t\td3d12::RenderTarget* out_deferred_main_rt = nullptr;\n\n\t\tunsigned int frame_idx = 0;\n\n\t\tDescriptorAllocation out_output_alloc;\n\t\tDescriptorAllocation out_gbuffer_albedo_alloc;\n\t\tDescriptorAllocation out_gbuffer_normal_alloc;\n\t\tDescriptorAllocation out_gbuffer_depth_alloc;\n\n\t\tbool tlas_requires_init = false;\n\t};\n\n\tnamespace internal\n\t{\n\t\tinline void CreateShaderTables(d3d12::Device* device, RTHybrid_BaseData& data, const std::string& raygen_entry, \n\t\t\t\t\t\t\t\t\t   const std::vector<std::string>& miss_entries, const std::vector<std::string>& hit_groups, int frame_idx)\n\t\t{\n\t\t\t// Delete existing shader table\n\t\t\tif (data.out_miss_shader_table[frame_idx])\n\t\t\t{\n\t\t\t\td3d12::Destroy(data.out_miss_shader_table[frame_idx]);\n\t\t\t}\n\t\t\tif (data.out_hitgroup_shader_table[frame_idx])\n\t\t\t{\n\t\t\t\td3d12::Destroy(data.out_hitgroup_shader_table[frame_idx]);\n\t\t\t}\n\t\t\tif (data.out_raygen_shader_table[frame_idx])\n\t\t\t{\n\t\t\t\td3d12::Destroy(data.out_raygen_shader_table[frame_idx]);\n\t\t\t}\n\n\t\t\t// Set up Raygen Shader Table\n\t\t\t{\n\t\t\t\t// Create Record(s)\n\t\t\t\tstd::uint32_t shader_record_count = 1;\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\n\t\t\t\tauto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, raygen_entry);\n\n\t\t\t\tauto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size);\n\n\t\t\t\t// Create Table\n\t\t\t\tdata.out_raygen_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\n\t\t\t\td3d12::AddShaderRecord(data.out_raygen_shader_table[frame_idx], shader_record);\n\t\t\t}\n\n\t\t\t// Set up Miss Shader Table\n\t\t\t{\n\t\t\t\t// Create Record(s) and Table(s)\n\t\t\t\tstd::uint32_t shader_record_count = miss_entries.size();\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\n\n\t\t\t\tdata.out_miss_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\n\t\t\t\t\n\t\t\t\tfor (auto& entry : miss_entries)\n\t\t\t\t{\n\t\t\t\t\tauto miss_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, entry);\n\t\t\t\t\tauto miss_record = d3d12::CreateShaderRecord(miss_identifier, shader_identifier_size);\n\t\t\t\t\td3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], miss_record);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Set up Hit Group Shader Table\n\t\t\t{\n\t\t\t\t// Create Record(s)\n\t\t\t\tstd::uint32_t shader_record_count = hit_groups.size();\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\n\n\t\t\t\tdata.out_hitgroup_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\n\t\t\t\t\n\t\t\t\tfor (auto& entry : hit_groups)\n\t\t\t\t{\n\t\t\t\t\tauto hit_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, entry);\n\t\t\t\t\tauto hit_record = d3d12::CreateShaderRecord(hit_identifier, shader_identifier_size);\n\t\t\t\t\td3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], hit_record);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\n\t\tinline void CreateUAVsAndSRVs(FrameGraph& fg, RTHybrid_BaseData& data, d3d12::RenderTarget* n_render_target)\n\t\t{\n\t\t\t// Versioning\n\t\t\tfor (int frame_idx = 0; frame_idx < 1; ++frame_idx)\n\t\t\t{\n\t\t\t\t// Bind output texture\n\t\t\t\td3d12::DescHeapCPUHandle rtv_handle = data.out_output_alloc.GetDescriptorHandle(0);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t\trtv_handle = data.out_output_alloc.GetDescriptorHandle(1);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t\trtv_handle = data.out_output_alloc.GetDescriptorHandle(2);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 2, n_render_target->m_create_info.m_rtv_formats[2]);\n\n\t\t\t\t// Bind g-buffers (albedo, normal, depth)\n\t\t\t\tauto albedo_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle();\n\t\t\t\tauto normal_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle();\n\t\t\t\tauto depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle();\n\n\t\t\t\tauto deferred_main_rt = data.out_deferred_main_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<DeferredMainTaskData>());\n\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, albedo_handle, 0, deferred_main_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats[1]);\n\n\t\t\t\td3d12::CreateSRVFromDSV(deferred_main_rt, depth_handle);\n\t\t\t}\n\t\t}\n\t} /* internal */\n} /* wr */"
  },
  {
    "path": "src/render_tasks/d3d12_rt_reflection_task.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../scene_graph/scene_graph.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"d3d12_deferred_main.hpp\"\n#include \"d3d12_build_acceleration_structures.hpp\"\n#include \"d3d12_rt_hybrid_helpers.hpp\"\n#include \"../imgui_tools.hpp\"\n\nnamespace wr\n{\n\tstruct RTReflectionData\n\t{\n\t\tRTHybrid_BaseData base_data;\n\t};\n\n\tnamespace internal\n\t{\n\t\tinline void SetupRTReflectionTask(RenderSystem& render_system, FrameGraph & fg, RenderTaskHandle & handle, bool resize)\n\t\t{\n\t\t\t// Initialize variables\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(render_system);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<RTReflectionData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\td3d12::SetName(n_render_target, L\"Raytracing Reflection Target\");\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\n\t\t\t\t// Get AS build data\n\t\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\n\n\t\t\t\tdata.base_data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate(3));\n\t\t\t\tdata.base_data.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate());\n\t\t\t\tdata.base_data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate());\n\t\t\t\tdata.base_data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate());\n\t\t\t\t\n\t\t\t\tdata.base_data.tlas_requires_init = true;\n\t\t\t}\n\n\t\t\tCreateUAVsAndSRVs(fg, data.base_data, n_render_target);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\t// Camera constant buffer\n\t\t\t\tdata.base_data.out_cb_camera_handle = static_cast<D3D12ConstantBufferHandle*>(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData)));\n\n\t\t\t\t// Pipeline State Object\n\t\t\t\tauto& rt_registry = RTPipelineRegistry::Get();\n\t\t\t\tdata.base_data.out_state_object = static_cast<d3d12::StateObject*>(rt_registry.Find(state_objects::rt_reflection_state_object));\n\n\t\t\t\t// Root Signature\n\t\t\t\tauto& rs_registry = RootSignatureRegistry::Get();\n\t\t\t\tdata.base_data.out_root_signature = static_cast<d3d12::RootSignature*>(rs_registry.Find(root_signatures::rt_hybrid_global));\n\t\t\t}\n\n\t\t\t// Create Shader Tables\n\t\t\tfor (int i = 0; i < d3d12::settings::num_back_buffers; ++i)\n\t\t\t{\n\t\t\t\tCreateShaderTables(device, data.base_data, \"ReflectionRaygenEntry\", \n\t\t\t\t\t\t\t\t\t{ \"ReflectionMiss\", \"ShadowMissEntry\" },\n\t\t\t\t\t\t\t\t\t{ \"ReflectionHitGroup\", \"ShadowHitGroup\" }, i);\n\t\t\t}\n\n\t\t\t// Setup frame index\n\t\t\tdata.base_data.frame_idx = 0;\n\t\t}\n\n\t\tinline void ExecuteRTReflectionTask(RenderSystem & render_system, FrameGraph & fg, SceneGraph & scene_graph, RenderTaskHandle & handle)\n\t\t{\n\t\t\t// Initialize variables\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(render_system);\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tauto& data = fg.GetData<RTReflectionData>(handle);\n\t\t\t\n\t\t\tauto window = n_render_system.m_window.value();\n\t\t\tauto render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto device = n_render_system.m_device;\n\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tfloat scalar = 1.0f;\n\t\t\tfg.WaitForPredecessorTask<CubemapConvolutionTaskData>();\n\n\t\t\t// Rebuild acceleratrion structure a 2e time for fallback\n\t\t\tif (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\td3d12::CreateOrUpdateTLAS(device, cmd_list, data.base_data.tlas_requires_init, data.base_data.out_tlas, as_build_data.out_blas_list, frame_idx);\n\t\t\t\td3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx);\n\t\t\t}\n\n\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t{\n\t\t\t\td3d12::BindRaytracingPipeline(cmd_list, data.base_data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\n\n\t\t\t\t// Bind output, indices and materials, offsets, etc\n\t\t\t\tauto out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(0);\n\t\t\t\td3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)), out_uav_handle);\n\t\t\t\tout_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(1);\n\t\t\t\td3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)) + 1, out_uav_handle);\n\t\t\t\tout_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(2);\n\t\t\t\td3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)) + 2, out_uav_handle);\n\n\t\t\t\tauto out_scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::INDICES)), out_scene_ib_handle);\n\n\t\t\t\tauto out_scene_mat_handle = as_build_data.out_scene_mat_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::MATERIALS)), out_scene_mat_handle);\n\n\t\t\t\tauto out_scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OFFSETS)), out_scene_offset_handle);\n\n\t\t\t\tauto out_albedo_gbuffer_handle = data.base_data.out_gbuffer_albedo_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 0, out_albedo_gbuffer_handle);\n\n\t\t\t\tauto out_normal_gbuffer_handle = data.base_data.out_gbuffer_normal_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 1, out_normal_gbuffer_handle);\n\n\t\t\t\tauto out_scene_depth_handle = data.base_data.out_gbuffer_depth_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 2, out_scene_depth_handle);\n\n\t\t\t\t/*\n\t\t\t\tTo keep the CopyDescriptors function happy, we need to fill the descriptor table with valid descriptors\n\t\t\t\tWe fill the table with a single descriptor, then overwrite some spots with the he correct textures\n\t\t\t\tIf a spot is unused, then a default descriptor will be still bound, but not used in the shaders.\n\t\t\t\tSince the renderer creates a texture pool that can be used by the render tasks, and\n\t\t\t\tthe texture pool also has default textures for albedo/roughness/etc... one of those textures is a good\n\t\t\t\tcandidate for this.\n\t\t\t\t*/\n\t\t\t\t{\n\t\t\t\t\tauto texture_handle = n_render_system.GetDefaultAlbedo();\n\t\t\t\t\tauto* texture_resource = static_cast<wr::d3d12::TextureResource*>(texture_handle.m_pool->GetTextureResource(texture_handle));\n\n\t\t\t\t\tsize_t num_textures_in_heap = COMPILATION_EVAL(rs_layout::GetSize(params::rt_hybrid, params::RTHybridE::TEXTURES));\n\t\t\t\t\tunsigned int heap_loc_start = COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES));\n\n\t\t\t\t\tfor (size_t i = 0; i < num_textures_in_heap; ++i)\n\t\t\t\t\t{\n\t\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, static_cast<std::uint32_t>(heap_loc_start + i), texture_resource);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Fill descriptor heap with textures used by the scene\n\t\t\t\tfor (auto material_handle : as_build_data.out_material_handles)\n\t\t\t\t{\n\t\t\t\t\tauto* material_internal = material_handle.m_pool->GetMaterial(material_handle);\n\n\t\t\t\t\tauto set_srv = [&data, material_internal, cmd_list](auto texture_handle)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto* texture_internal = static_cast<wr::d3d12::TextureResource*>(texture_handle.m_pool->GetTextureResource(texture_handle));\n\n\t\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES)) + static_cast<std::uint32_t>(texture_handle.m_id), texture_internal);\n\t\t\t\t\t};\n\n\t\t\t\t\tstd::array<TextureType, static_cast<size_t>(TextureType::COUNT)> types = { TextureType::ALBEDO, TextureType::NORMAL,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   TextureType::ROUGHNESS, TextureType::METALLIC,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   TextureType::EMISSIVE, TextureType::AO };\n\n\t\t\t\t\tfor (auto t : types)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (material_internal->HasTexture(t))\n\t\t\t\t\t\t\tset_srv(material_internal->GetTexture(t));\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Get light buffer\n\t\t\t\tif (static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE)\n\t\t\t\t{\n\t\t\t\t\tstatic_cast<D3D12StructuredBufferPool*>(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t}\n\n\t\t\t\tDescriptorAllocation light_alloc = std::move(as_build_data.out_allocator->Allocate());\n\t\t\t\td3d12::DescHeapCPUHandle light_handle = light_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromStructuredBuffer(static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native, light_handle, frame_idx);\n\n\t\t\t\td3d12::DescHeapCPUHandle light_handle2 = light_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::LIGHTS)), light_handle2);\n\n\n\t\t\t\t// Update offset data\n\t\t\t\tn_render_system.m_raytracing_offset_sb_pool->Update(as_build_data.out_sb_offset_handle, (void*)as_build_data.out_offsets.data(), sizeof(temp::RayTracingOffset_CBData) * as_build_data.out_offsets.size(), 0);\n\n\t\t\t\t// Update material data\n\t\t\t\tif (as_build_data.out_materials_require_update)\n\t\t\t\t{\n\t\t\t\t\tn_render_system.m_raytracing_material_sb_pool->Update(as_build_data.out_sb_material_handle, (void*)as_build_data.out_materials.data(), sizeof(temp::RayTracingMaterial_CBData) * as_build_data.out_materials.size(), 0);\n\t\t\t\t}\n\n\t\t\t\t// Update camera constant buffer\n\t\t\t\tauto camera = scene_graph.GetActiveCamera();\n\t\t\t\ttemp::RTHybridCamera_CBData cam_data;\n\t\t\t\tcam_data.m_inverse_view = DirectX::XMMatrixInverse(nullptr, camera->m_view);\n\t\t\t\tcam_data.m_inverse_projection = DirectX::XMMatrixInverse(nullptr, camera->m_projection);\n\t\t\t\tcam_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection);\n\t\t\t\tcam_data.m_intensity = n_render_system.temp_intensity;\n\t\t\t\tcam_data.m_frame_idx = static_cast<float>(++data.base_data.frame_idx);\n\t\t\t\tn_render_system.m_camera_pool->Update(data.base_data.out_cb_camera_handle, sizeof(temp::RTHybridCamera_CBData), 0, frame_idx, (std::uint8_t*) & cam_data); // FIXME: Uhh wrong pool?\n\n\t\t\t\t// Make sure the convolution pass wrote to the skybox.\n\t\t\t\tfg.WaitForPredecessorTask<CubemapConvolutionTaskData>();\n\n\t\t\t\t// Get skybox\n\t\t\t\tif (SkyboxNode * skybox = scene_graph.GetCurrentSkybox().get())\n\t\t\t\t{\n\t\t\t\t\tauto skybox_t = static_cast<d3d12::TextureResource*>(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value()));\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::SKYBOX)), skybox_t);\n\n\t\t\t\t\t// Get Pre-filtered environment\n\t\t\t\t\tauto irradiance_t = static_cast<d3d12::TextureResource*>(skybox->m_prefiltered_env_map->m_pool->GetTextureResource(skybox->m_prefiltered_env_map.value()));\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::PREF_ENV_MAP)), irradiance_t);\n\n\t\t\t\t\t// Get Environment Map\n\t\t\t\t\tirradiance_t = static_cast<d3d12::TextureResource*>(scene_graph.GetCurrentSkybox()->m_irradiance->m_pool->GetTextureResource(scene_graph.GetCurrentSkybox()->m_irradiance.value()));\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::IRRADIANCE_MAP)), irradiance_t);\n\t\t\t\t}\n\n\t\t\t\t// Get brdf lookup texture\n\t\t\t\tauto brdf_lut_text = static_cast<d3d12::TextureResource*>(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value()));\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::BRDF_LUT)), brdf_lut_text);\n\n\t\t\t\t// Transition depth to NON_PIXEL_RESOURCE\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t\td3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\n\t\t\t\td3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\n\t\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.base_data.out_cb_camera_handle->m_native, 2, frame_idx);\n\n\t\t\t\tif (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t\t\t{\n\t\t\t\t\td3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1);\n\t\t\t\t}\n\t\t\t\telse if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t\t{\n\t\t\t\t\tcmd_list->m_native_fallback->SetTopLevelAccelerationStructure(0, as_build_data.out_tlas.m_fallback_tlas_ptr);\n\t\t\t\t}\n\n\t\t\t\t/*unsigned int verts_loc = 3; rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::VERTICES);\n\t\t\t\tThis should be the Parameter index not the heap location, it was only working due to a ridiculous amount of luck and should be fixed, or we completely missunderstand this stuff...\n\t\t\t\tMuch love, Meine and Florian*/\n\t\t\t\td3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3);\n\n\t\t\t\t//#ifdef _DEBUG\n\t\t\t\tCreateShaderTables(device, data.base_data, \"ReflectionRaygenEntry\",\n\t\t\t\t\t{ \"ReflectionMiss\", \"ShadowMissEntry\" },\n\t\t\t\t\t{ \"ReflectionHitGroup\", \"ShadowHitGroup\" }, frame_idx);\n\t\t\t\t//#endif\n\n\t\t\t\t// Dispatch hybrid ray tracing rays\n\t\t\t\td3d12::DispatchRays(cmd_list,\n\t\t\t\t\tdata.base_data.out_hitgroup_shader_table[frame_idx],\n\t\t\t\t\tdata.base_data.out_miss_shader_table[frame_idx],\n\t\t\t\t\tdata.base_data.out_raygen_shader_table[frame_idx],\n\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(d3d12::GetRenderTargetWidth(render_target))),\n\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(d3d12::GetRenderTargetHeight(render_target))),\n\t\t\t\t\t1,\n\t\t\t\t\tframe_idx);\n\n\t\t\t\t// Transition depth back to DEPTH_WRITE\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE);\n\t\t\t}\n\t\t}\n\n\t\tinline void DestroyRTReflectionTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif(!resize)\n\t\t\t{\n\t\t\t\tRTReflectionData& data = fg.GetData<RTReflectionData>(handle);\n\n\t\t\t\tfor(d3d12::ShaderTable* shader : data.base_data.out_raygen_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\n\t\t\t\tfor(d3d12::ShaderTable* shader : data.base_data.out_miss_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\n\t\t\t\tfor(d3d12::ShaderTable* shader : data.base_data.out_hitgroup_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddRTReflectionTask(FrameGraph& fg)\n\t{\n\t\tstd::wstring name(L\"Hybrid raytracing reflections\");\n\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT, wr::Format::R8_UNORM, wr::Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(3),\n\t\t\tRenderTargetProperties::Clear(true),\n\t\t\tRenderTargetProperties::ClearDepth(true),\n\t\t\tRenderTargetProperties::ResolutionScalar(0.5f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tinternal::SetupRTReflectionTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tinternal::ExecuteRTReflectionTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tinternal::DestroyRTReflectionTask(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tfg.AddTask<RTReflectionData>(desc, name, FG_DEPS<DeferredMainTaskData>());\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/render_tasks/d3d12_rt_shadow_task.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../scene_graph/scene_graph.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"d3d12_build_acceleration_structures.hpp\"\n#include \"d3d12_deferred_main.hpp\"\n#include \"d3d12_rt_hybrid_helpers.hpp\"\n\n#include \"../imgui_tools.hpp\"\n\nnamespace wr\n{\n\tstruct RTShadowSettings\n\t{\n\t\tstruct Runtime\n\t\t{\n\t\t\tfloat m_epsilon = 0.01f;\n\t\t\tint m_sample_count = 1;\n\t\t};\n\n\t\tRuntime m_runtime;\n\t};\n\n\tstruct RTShadowData\n\t{\n\t\tRTHybrid_BaseData base_data;\n\t};\n\n\tnamespace internal\n\t{\n\t\tinline void SetupRTShadowTask(RenderSystem & render_system, FrameGraph & fg, RenderTaskHandle & handle, bool resize)\n\t\t{\n\t\t\t// Initialize variables\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(render_system);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<RTShadowData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\td3d12::SetName(n_render_target, L\"Raytracing Shadow Target\");\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\n\t\t\t\t// Get AS build data\n\t\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\n\n\t\t\t\tdata.base_data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate(3));\n\t\t\t\tdata.base_data.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate());\n\t\t\t\tdata.base_data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate());\n\t\t\t\tdata.base_data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate());\n\n\t\t\t\tdata.base_data.tlas_requires_init = true;\n\t\t\t}\n\n\t\t\t// Versioning\n\t\t\tCreateUAVsAndSRVs(fg, data.base_data, n_render_target);\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\t// Camera constant buffer\n\t\t\t\tdata.base_data.out_cb_camera_handle = static_cast<D3D12ConstantBufferHandle*>(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData)));\n\n\t\t\t\t// Pipeline State Object\n\t\t\t\tauto& rt_registry = RTPipelineRegistry::Get();\n\t\t\t\tdata.base_data.out_state_object = static_cast<d3d12::StateObject*>(rt_registry.Find(state_objects::rt_shadow_state_object));\n\n\t\t\t\t// Root Signature\n\t\t\t\tauto& rs_registry = RootSignatureRegistry::Get();\n\t\t\t\tdata.base_data.out_root_signature = static_cast<d3d12::RootSignature*>(rs_registry.Find(root_signatures::rt_hybrid_global));\n\t\t\t}\n\n\t\t\t// Create Shader Tables\n\t\t\tfor (int i = 0; i < d3d12::settings::num_back_buffers; ++i)\n\t\t\t{\n\t\t\t\tCreateShaderTables(device, data.base_data, \"ShadowRaygenEntry\",\n\t\t\t\t\t\t\t\t\t{ \"ShadowMissEntry\" },\n\t\t\t\t\t\t\t\t\t{ \"ShadowHitGroup\" }, i);\n\t\t\t}\n\n\t\t\t// Setup frame index\n\t\t\tdata.base_data.frame_idx = 0;\n\t\t}\n\n\t\tinline void ExecuteRTShadowTask(RenderSystem & render_system, FrameGraph & fg, SceneGraph & scene_graph, RenderTaskHandle & handle)\n\t\t{\n\t\t\t// Initialize variables\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(render_system);\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tauto& data = fg.GetData<RTShadowData>(handle);\n\n\t\t\tauto window = n_render_system.m_window.value();\n\t\t\tauto render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto device = n_render_system.m_device;\n\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto settings = fg.GetSettings<RTShadowData, RTShadowSettings>();\n\t\t\tfloat scalar = 1.0f;\n\n\t\t\tfg.WaitForPredecessorTask<CubemapConvolutionTaskData>();\n\n\t\t\t// Rebuild acceleratrion structure a 2e time for fallback\n\t\t\tif (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t{\n\t\t\t\td3d12::CreateOrUpdateTLAS(device, cmd_list, data.base_data.tlas_requires_init, data.base_data.out_tlas, as_build_data.out_blas_list, frame_idx);\n\t\t\t\td3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx);\n\t\t\t}\n\n\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t{\n\t\t\t\td3d12::BindRaytracingPipeline(cmd_list, data.base_data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\n\n\t\t\t\t// Bind output, indices and materials, offsets, etc\n\t\t\t\tauto out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)), out_uav_handle);\n\t\t\t\tout_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(1);\n\t\t\t\td3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)) + 1, out_uav_handle);\n\t\t\t\tout_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(2);\n\t\t\t\td3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)) + 2, out_uav_handle);\n\n\t\t\t\tauto out_scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::INDICES)), out_scene_ib_handle);\n\n\t\t\t\tauto out_scene_mat_handle = as_build_data.out_scene_mat_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::MATERIALS)), out_scene_mat_handle);\n\n\t\t\t\tauto out_scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OFFSETS)), out_scene_offset_handle);\n\n\t\t\t\tauto out_albedo_gbuffer_handle = data.base_data.out_gbuffer_albedo_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 0, out_albedo_gbuffer_handle);\n\n\t\t\t\tauto out_normal_gbuffer_handle = data.base_data.out_gbuffer_normal_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 1, out_normal_gbuffer_handle);\n\n\t\t\t\tauto out_scene_depth_handle = data.base_data.out_gbuffer_depth_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 2, out_scene_depth_handle);\n\n\t\t\t\t/*\n\t\t\t\tTo keep the CopyDescriptors function happy, we need to fill the descriptor table with valid descriptors\n\t\t\t\tWe fill the table with a single descriptor, then overwrite some spots with the he correct textures\n\t\t\t\tIf a spot is unused, then a default descriptor will be still bound, but not used in the shaders.\n\t\t\t\tSince the renderer creates a texture pool that can be used by the render tasks, and\n\t\t\t\tthe texture pool also has default textures for albedo/roughness/etc... one of those textures is a good\n\t\t\t\tcandidate for this.\n\t\t\t\t*/\n\t\t\t\t{\n\t\t\t\t\tauto texture_handle = n_render_system.GetDefaultAlbedo();\n\t\t\t\t\tauto* texture_resource = static_cast<wr::d3d12::TextureResource*>(texture_handle.m_pool->GetTextureResource(texture_handle));\n\n\t\t\t\t\tsize_t num_textures_in_heap = COMPILATION_EVAL(rs_layout::GetSize(params::rt_hybrid, params::RTHybridE::TEXTURES));\n\t\t\t\t\tunsigned int heap_loc_start = COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES));\n\n\t\t\t\t\tfor (size_t i = 0; i < num_textures_in_heap; ++i)\n\t\t\t\t\t{\n\t\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, static_cast<std::uint32_t>(heap_loc_start + i), texture_resource);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Fill descriptor heap with textures used by the scene\n\t\t\t\tfor (auto material_handle : as_build_data.out_material_handles)\n\t\t\t\t{\n\t\t\t\t\tauto* material_internal = material_handle.m_pool->GetMaterial(material_handle);\n\n\t\t\t\t\tauto set_srv = [&data, material_internal, cmd_list](auto texture_handle)\n\t\t\t\t\t{\n\t\t\t\t\t\tauto* texture_internal = static_cast<wr::d3d12::TextureResource*>(texture_handle.m_pool->GetTextureResource(texture_handle));\n\n\t\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES)) + static_cast<std::uint32_t>(texture_handle.m_id), texture_internal);\n\t\t\t\t\t};\n\n\t\t\t\t\tstd::array<TextureType, static_cast<size_t>(TextureType::COUNT)> types = { TextureType::ALBEDO, TextureType::NORMAL,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   TextureType::ROUGHNESS, TextureType::METALLIC,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t   TextureType::EMISSIVE, TextureType::AO };\n\n\t\t\t\t\tfor (auto t : types)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (material_internal->HasTexture(t))\n\t\t\t\t\t\t\tset_srv(material_internal->GetTexture(t));\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Get light buffer\n\t\t\t\tif (static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE)\n\t\t\t\t{\n\t\t\t\t\tstatic_cast<D3D12StructuredBufferPool*>(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t}\n\n\t\t\t\tDescriptorAllocation light_alloc = std::move(as_build_data.out_allocator->Allocate());\n\t\t\t\td3d12::DescHeapCPUHandle light_handle = light_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromStructuredBuffer(static_cast<D3D12StructuredBufferHandle*>(scene_graph.GetLightBuffer())->m_native, light_handle, frame_idx);\n\n\t\t\t\td3d12::DescHeapCPUHandle light_handle2 = light_alloc.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::LIGHTS)), light_handle2);\n\n\n\t\t\t\t// Update offset data\n\t\t\t\tn_render_system.m_raytracing_offset_sb_pool->Update(as_build_data.out_sb_offset_handle, (void*)as_build_data.out_offsets.data(), sizeof(temp::RayTracingOffset_CBData) * as_build_data.out_offsets.size(), 0);\n\n\t\t\t\t// Update material data\n\t\t\t\tif (as_build_data.out_materials_require_update)\n\t\t\t\t{\n\t\t\t\t\tn_render_system.m_raytracing_material_sb_pool->Update(as_build_data.out_sb_material_handle, (void*)as_build_data.out_materials.data(), sizeof(temp::RayTracingMaterial_CBData) * as_build_data.out_materials.size(), 0);\n\t\t\t\t}\n\n\t\t\t\t// Update camera constant buffer\n\t\t\t\tauto camera = scene_graph.GetActiveCamera();\n\t\t\t\ttemp::RTHybridCamera_CBData cam_data;\n\t\t\t\tcam_data.m_inverse_view = DirectX::XMMatrixInverse(nullptr, camera->m_view);\n\t\t\t\tcam_data.m_inverse_projection = DirectX::XMMatrixInverse(nullptr, camera->m_projection);\n\t\t\t\tcam_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection);\n\t\t\t\tcam_data.m_intensity = n_render_system.temp_intensity;\n\t\t\t\tcam_data.m_frame_idx = static_cast<float>(++data.base_data.frame_idx);\n\t\t\t\tcam_data.m_epsilon = settings.m_runtime.m_epsilon;\n\t\t\t\tcam_data.m_sample_count = settings.m_runtime.m_sample_count;\n\t\t\t\tn_render_system.m_camera_pool->Update(data.base_data.out_cb_camera_handle, sizeof(temp::RTHybridCamera_CBData), 0, frame_idx, (std::uint8_t*) & cam_data); // FIXME: Uhh wrong pool?\n\n\t\t\t\t// Make sure the convolution pass wrote to the skybox.\n\t\t\t\tfg.WaitForPredecessorTask<CubemapConvolutionTaskData>();\n\n\t\t\t\t// Get skybox\n\t\t\t\tif (SkyboxNode * skybox = scene_graph.GetCurrentSkybox().get())\n\t\t\t\t{\n\t\t\t\t\tauto skybox_t = static_cast<d3d12::TextureResource*>(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value()));\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::SKYBOX)), skybox_t);\n\n\t\t\t\t\t// Get Pre-filtered environment\n\t\t\t\t\tauto irradiance_t = static_cast<d3d12::TextureResource*>(skybox->m_prefiltered_env_map->m_pool->GetTextureResource(skybox->m_prefiltered_env_map.value()));\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::PREF_ENV_MAP)), irradiance_t);\n\n\t\t\t\t\t// Get Environment Map\n\t\t\t\t\tirradiance_t = static_cast<d3d12::TextureResource*>(scene_graph.GetCurrentSkybox()->m_irradiance->m_pool->GetTextureResource(scene_graph.GetCurrentSkybox()->m_irradiance.value()));\n\t\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::IRRADIANCE_MAP)), irradiance_t);\n\t\t\t\t}\n\n\t\t\t\t// Get brdf lookup texture\n\t\t\t\tauto brdf_lut_text = static_cast<d3d12::TextureResource*>(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value()));\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::BRDF_LUT)), brdf_lut_text);\n\n\t\t\t\t// Transition depth to NON_PIXEL_RESOURCE\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t\td3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\n\t\t\t\td3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK);\n\t\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.base_data.out_cb_camera_handle->m_native, 2, frame_idx);\n\n\t\t\t\tif (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE)\n\t\t\t\t{\n\t\t\t\t\td3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1);\n\t\t\t\t}\n\t\t\t\telse if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK)\n\t\t\t\t{\n\t\t\t\t\tcmd_list->m_native_fallback->SetTopLevelAccelerationStructure(0, as_build_data.out_tlas.m_fallback_tlas_ptr);\n\t\t\t\t}\n\n\t\t\t\t/*unsigned int verts_loc = 3; rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::VERTICES);\n\t\t\t\tThis should be the Parameter index not the heap location, it was only working due to a ridiculous amount of luck and should be fixed, or we completely missunderstand this stuff...\n\t\t\t\tMuch love, Meine and Florian*/\n\t\t\t\td3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3);\n\n\t\t\t\t//#ifdef _DEBUG\n\t\t\t\tCreateShaderTables(device, data.base_data, \"ShadowRaygenEntry\",\n\t\t\t\t\t\t\t\t{ \"ShadowMissEntry\" },\n\t\t\t\t\t\t\t\t{ \"ShadowHitGroup\" }, frame_idx);\n\t\t\t\t//#endif\n\n\t\t\t\t// Dispatch hybrid ray tracing rays\n\t\t\t\td3d12::DispatchRays(cmd_list,\n\t\t\t\t\tdata.base_data.out_hitgroup_shader_table[frame_idx],\n\t\t\t\t\tdata.base_data.out_miss_shader_table[frame_idx],\n\t\t\t\t\tdata.base_data.out_raygen_shader_table[frame_idx],\n\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(d3d12::GetRenderTargetWidth(render_target))),\n\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(d3d12::GetRenderTargetHeight(render_target))),\n\t\t\t\t\t1,\n\t\t\t\t\tframe_idx);\n\n\t\t\t\t// Transition depth back to DEPTH_WRITE\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE);\n\t\t\t}\n\t\t}\n\n\t\tinline void DestroyRTShadowTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tif(!resize)\n\t\t\t{\n\t\t\t\tRTShadowData& data = fg.GetData<RTShadowData>(handle);\n\n\t\t\t\tfor (d3d12::ShaderTable* shader : data.base_data.out_raygen_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\n\t\t\t\tfor (d3d12::ShaderTable* shader : data.base_data.out_miss_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\n\t\t\t\tfor (d3d12::ShaderTable* shader : data.base_data.out_hitgroup_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddRTShadowTask(FrameGraph& fg)\n\t{\n\t\tstd::wstring name(L\"Hybrid raytracing shadows\");\n\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT, wr::Format::R8_UNORM, wr::Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(3),\n\t\t\tRenderTargetProperties::Clear(true),\n\t\t\tRenderTargetProperties::ClearDepth(true),\n\t\t\tRenderTargetProperties::ResolutionScalar(1.0f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tinternal::SetupRTShadowTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tinternal::ExecuteRTShadowTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tinternal::DestroyRTShadowTask(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tfg.AddTask<RTShadowData>(desc, name, FG_DEPS<DeferredMainTaskData>());\n\t\tfg.UpdateSettings<RTShadowData>(RTShadowSettings());\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/render_tasks/d3d12_rtao_task.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../rt_pipeline_registry.hpp\"\n#include \"../root_signature_registry.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_build_acceleration_structures.hpp\"\n\nnamespace wr\n{\n\tstruct RTAOSettings\n\t{\n\t\tstruct Runtime\n\t\t{\n\t\t\tfloat bias = 0.05f;\n\t\t\tfloat radius = 0.5f;\n\t\t\tfloat power = 1.f; // The final AO output is pow(AO, powerExponent) // 1.0~4.0\n\t\t\tfloat max_distance = 20.f;\n\t\t\tint sample_count = 8;\n\t\t};//Currently setup to make work with retardedly small scenes\n\n\t\tRuntime m_runtime;\n\t};\n\n\tstruct RTAOData\n\t{\n\n\t\td3d12::AccelerationStructure out_tlas = {};\n\n\t\t// Shader tables\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> in_raygen_shader_table = { nullptr, nullptr, nullptr };\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> in_miss_shader_table = { nullptr, nullptr, nullptr };\n\t\tstd::array<d3d12::ShaderTable*, d3d12::settings::num_back_buffers> in_hitgroup_shader_table = { nullptr, nullptr, nullptr };\n\n\t\t// Pipeline objects\n\t\td3d12::StateObject* in_state_object;\n\t\td3d12::RootSignature* in_root_signature;\n\n\t\tD3D12ConstantBufferHandle* out_cb_handle;\n\t\td3d12::RenderTarget* in_deferred_main_rt;\n\n\t\tDescriptorAllocation out_uav_from_rtv;\n\t\tDescriptorAllocation in_gbuffers;\n\t\tDescriptorAllocation in_depthbuffer;\n\n\t\tbool tlas_requires_init = false;\n\n\t};\n\t\n\tnamespace internal\n\t{\n\t\tinline void CreateShaderTables(d3d12::Device* device, RTAOData& data, int frame_idx)\n\t\t{\n\t\t\t// Delete existing shader table\n\t\t\tif (data.in_miss_shader_table[frame_idx])\n\t\t\t{\n\t\t\t\td3d12::Destroy(data.in_miss_shader_table[frame_idx]);\n\t\t\t}\n\t\t\tif (data.in_hitgroup_shader_table[frame_idx])\n\t\t\t{\n\t\t\t\td3d12::Destroy(data.in_hitgroup_shader_table[frame_idx]);\n\t\t\t}\n\t\t\tif (data.in_raygen_shader_table[frame_idx])\n\t\t\t{\n\t\t\t\td3d12::Destroy(data.in_raygen_shader_table[frame_idx]);\n\t\t\t}\n\n\t\t\t// Set up Raygen Shader Table\n\t\t\t{\n\t\t\t\t// Create Record(s)\n\t\t\t\tUINT shader_record_count = 1;\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\n\t\t\t\tauto shader_identifier = d3d12::GetShaderIdentifier(device, data.in_state_object, \"AORaygenEntry\");\n\n\t\t\t\tauto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size);\n\n\t\t\t\t// Create Table\n\t\t\t\tdata.in_raygen_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\n\t\t\t\td3d12::AddShaderRecord(data.in_raygen_shader_table[frame_idx], shader_record);\n\t\t\t}\n\n\t\t\t// Set up Miss Shader Table\n\t\t\t{\n\t\t\t\t// Create Record(s)\n\t\t\t\tUINT shader_record_count = 1;\n\t\t\t\tauto shader_identifier_size = d3d12::GetShaderIdentifierSize(device);\n\n\t\t\t\tauto miss_identifier = d3d12::GetShaderIdentifier(device, data.in_state_object, \"MissEntry\");\n\t\t\t\tauto miss_record = d3d12::CreateShaderRecord(miss_identifier, shader_identifier_size);\n\n\t\t\t\t// Create Table(s)\n\t\t\t\tdata.in_miss_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size);\n\t\t\t\td3d12::AddShaderRecord(data.in_miss_shader_table[frame_idx], miss_record);\n\t\t\t}\n\t\t}\n\n\t\tinline void SetupAOTask(RenderSystem& render_system, FrameGraph& fg, RenderTaskHandle& handle, bool resize)\n\t\t{\n\t\t\t// Initialize variables\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(render_system);\n\t\t\tauto& device = n_render_system.m_device;\n\t\t\tauto& data = fg.GetData<RTAOData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\td3d12::SetName(n_render_target, L\"AO Target\");\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\n\n\t\t\t\tdata.out_uav_from_rtv = std::move(as_build_data.out_allocator->Allocate(1));\n\t\t\t\tdata.in_gbuffers = std::move(as_build_data.out_allocator->Allocate(1));\n\t\t\t\tdata.in_depthbuffer = std::move(as_build_data.out_allocator->Allocate(1));\n\t\t\t}\n\n\t\t\t// Versioning\n\t\t\tfor (int frame_idx = 0; frame_idx < 1; ++frame_idx)\n\t\t\t{\n\t\t\t\t// Bind output texture\n\t\t\t\td3d12::DescHeapCPUHandle rtv_handle = data.out_uav_from_rtv.GetDescriptorHandle();\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\n\t\t\t\t// Bind g-buffers\n\t\t\t\td3d12::DescHeapCPUHandle normal_gbuffer_handle = data.in_gbuffers.GetDescriptorHandle(0);\n\t\t\t\td3d12::DescHeapCPUHandle depth_buffer_handle = data.in_depthbuffer.GetDescriptorHandle(0);\n\t\t\t\t\n\t\t\t\tauto deferred_main_rt = data.in_deferred_main_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<DeferredMainTaskData>());\n\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_gbuffer_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats.data()[1]);\n\t\t\t\td3d12::CreateSRVFromDSV(deferred_main_rt, depth_buffer_handle);\n\t\t\t}\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\t// Camera constant buffer\n\t\t\t\tdata.out_cb_handle = static_cast<D3D12ConstantBufferHandle*>(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTAO_CBData)));\n\n\t\t\t\t// Pipeline State Object\n\t\t\t\tauto& rt_registry = RTPipelineRegistry::Get();\n\t\t\t\tdata.in_state_object = static_cast<d3d12::StateObject*>(rt_registry.Find(state_objects::rt_ao_state_opbject));\n\n\t\t\t\t// Root Signature\n\t\t\t\tauto& rs_registry = RootSignatureRegistry::Get();\n\t\t\t\tdata.in_root_signature = static_cast<d3d12::RootSignature*>(rs_registry.Find(root_signatures::rt_ao_global));\n\n\t\t\t\t// Create Shader Tables\n\t\t\t\tCreateShaderTables(device, data, 0);\n\t\t\t\tCreateShaderTables(device, data, 1);\n\t\t\t\tCreateShaderTables(device, data, 2);\n\t\t\t}\n\t\t}\n\n\t\tinline void ExecuteAOTask(RenderSystem & render_system, FrameGraph & fg, SceneGraph & scene_graph, RenderTaskHandle & handle)\n\t\t{\n\t\t\t// Initialize variables\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(render_system);\n\t\t\tauto window = n_render_system.m_window.value();\n\t\t\tauto render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto device = n_render_system.m_device;\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tauto& data = fg.GetData<RTAOData>(handle);\n\t\t\tauto& as_build_data = fg.GetPredecessorData<wr::ASBuildData>();\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto settings = fg.GetSettings<RTAOData, RTAOSettings>();\n\t\t\tfg.WaitForPredecessorTask<CubemapConvolutionTaskData>();\n\t\t\tfloat scalar = 1.0f;\n\n\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t{\n\n\t\t\t\td3d12::BindRaytracingPipeline(cmd_list, data.in_state_object, false);\n\n\t\t\t\t// Bind output, indices and materials, offsets, etc\n\t\t\t\tauto out_uav_handle = data.out_uav_from_rtv.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::OUTPUT)), out_uav_handle);\n\n\t\t\t\tauto in_scene_normal_gbuffer_handle = data.in_gbuffers.GetDescriptorHandle(0);\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::GBUFFERS)) + 0, in_scene_normal_gbuffer_handle);\n\t\t\t\t\n\t\t\t\tauto in_scene_depth_handle = data.in_depthbuffer.GetDescriptorHandle();\n\t\t\t\td3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::GBUFFERS)) + 1, in_scene_depth_handle);\n\n\t\t\t\t// Update offset data\n\t\t\t\tn_render_system.m_raytracing_offset_sb_pool->Update(as_build_data.out_sb_offset_handle, (void*)as_build_data.out_offsets.data(), sizeof(temp::RayTracingOffset_CBData) * as_build_data.out_offsets.size(), 0);\n\n\t\t\t\t// Update material data\n\t\t\t\tif (as_build_data.out_materials_require_update)\n\t\t\t\t{\n\t\t\t\t\tn_render_system.m_raytracing_material_sb_pool->Update(as_build_data.out_sb_material_handle, (void*)as_build_data.out_materials.data(), sizeof(temp::RayTracingMaterial_CBData) * as_build_data.out_materials.size(), 0);\n\t\t\t\t}\n\n\t\t\t\t// Update constant buffer\n\t\t\t\tauto camera = scene_graph.GetActiveCamera();\n\t\t\t\ttemp::RTAO_CBData cb_data;\n\t\t\t\tcb_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection);\n\t\t\t\tcb_data.m_inv_view = DirectX::XMMatrixInverse(nullptr, camera->m_view);\n\t\t\t\tcb_data.m_bias = settings.m_runtime.bias;\n\t\t\t\tcb_data.m_radius = settings.m_runtime.radius;\n\t\t\t\tcb_data.m_power = settings.m_runtime.power;\n\t\t\t\tcb_data.m_max_distance = settings.m_runtime.max_distance;\n\t\t\t\tcb_data.m_frame_idx = frame_idx;\n\t\t\t\tcb_data.m_sample_count = static_cast<unsigned int>(settings.m_runtime.sample_count);\n\n\t\t\t\tn_render_system.m_camera_pool->Update(data.out_cb_handle, sizeof(temp::RTAO_CBData), 0, frame_idx, (std::uint8_t*)& cb_data); // FIXME: Uhh wrong pool?\n\n\t\t\t\t// Transition depth to NON_PIXEL_RESOURCE\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.in_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t\td3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, false);\n\t\t\t\td3d12::BindDescriptorHeaps(cmd_list, false);\n\t\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_handle->m_native, 2, frame_idx);\n\n\t\t\t\tif (!as_build_data.out_blas_list.empty())\n\t\t\t\t{\n\t\t\t\t\td3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1);\n\t\t\t\t}\n\t\t\t\t\n#ifdef _DEBUG\n\t\t\t\tCreateShaderTables(device, data, frame_idx);\n#endif // _DEBUG\n\n\t\t\t\t// Dispatch hybrid ray tracing rays\n\t\t\t\td3d12::DispatchRays(cmd_list, \n\t\t\t\t\tdata.in_hitgroup_shader_table[frame_idx], \n\t\t\t\t\tdata.in_miss_shader_table[frame_idx], \n\t\t\t\t\tdata.in_raygen_shader_table[frame_idx], \n\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(d3d12::GetRenderTargetWidth(render_target))),\n\t\t\t\t\tstatic_cast<std::uint32_t>(std::ceil(d3d12::GetRenderTargetHeight(render_target))),\n\t\t\t\t\t1,\n\t\t\t\t\tframe_idx);\n\n\t\t\t\t// Transition depth back to DEPTH_WRITE\n\t\t\t\td3d12::TransitionDepth(cmd_list, data.in_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE);\n\t\t\t}\n\t\t}\n\n\t\tinline void DestroyAOTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) \n\t\t{\n\t\t\tif(!resize)\n\t\t\t{\n\t\t\t\tRTAOData& data = fg.GetData<RTAOData>(handle);\n\n\t\t\t\tfor(d3d12::ShaderTable* shader : data.in_raygen_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\n\t\t\t\tfor(d3d12::ShaderTable* shader : data.in_miss_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\n\t\t\t\tfor(d3d12::ShaderTable* shader : data.in_hitgroup_shader_table)\n\t\t\t\t{\n\t\t\t\t\tdelete shader;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tinline void AddRTAOTask(FrameGraph& fg, d3d12::Device* device)\n\t{\n\t\tif (wr::d3d12::GetRaytracingType(device) == wr::RaytracingType::NATIVE)//We do not support fallback layer for shadow rays.\n\t\t{\n\t\t\tRenderTargetProperties rt_properties\n\t\t\t{\n\t\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\t\tRenderTargetProperties::RTVFormats({ Format::R8_UNORM}),\n\t\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\t\tRenderTargetProperties::Clear(true),\n\t\t\t\tRenderTargetProperties::ClearDepth(true),\n\t\t\t};\n\n\t\t\tRenderTaskDesc desc;\n\t\t\tdesc.m_setup_func = [](RenderSystem & rs, FrameGraph & fg, RenderTaskHandle handle, bool resize)\n\t\t\t{\n\t\t\t\tinternal::SetupAOTask(rs, fg, handle, resize);\n\t\t\t};\n\t\t\tdesc.m_execute_func = [](RenderSystem & rs, FrameGraph & fg, SceneGraph & sg, RenderTaskHandle handle)\n\t\t\t{\n\t\t\t\tinternal::ExecuteAOTask(rs, fg, sg, handle);\n\t\t\t};\n\t\t\tdesc.m_destroy_func = [](FrameGraph & fg, RenderTaskHandle handle, bool resize)\n\t\t\t{\n\t\t\t\tinternal::DestroyAOTask(fg, handle, resize);\n\t\t\t};\n\n\t\t\tdesc.m_properties = rt_properties;\n\t\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\t\tdesc.m_allow_multithreading = true;\n\n\t\t\tfg.AddTask<RTAOData>(desc, L\"Ray Traced Ambient Occlusion\");\n\t\t\tfg.UpdateSettings<RTAOData>(RTAOSettings());\n\t\t}\n\t\telse\n\t\t{\n\t\t\tLOGW(\"RTAO task was not added since the fallback layer is not supported for RTAO. Consider using HBAO+ instead.\")\n\t\t}\n\t}\n}// namespace wr\n"
  },
  {
    "path": "src/render_tasks/d3d12_shadow_denoiser_task.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../d3d12/d3d12_structured_buffer_pool.hpp\"\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../scene_graph/scene_graph.hpp\"\n#include \"../scene_graph/camera_node.hpp\"\n#include \"../engine_registry.hpp\"\n\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n#include \"../render_tasks/d3d12_rt_shadow_task.hpp\"\n\n#include <optional>\n\nnamespace wr\n{\n\tstruct ShadowDenoiserSettings\n\t{\n\t\tstruct Runtime\n\t\t{\n\t\t\tfloat m_alpha = 0.05f;\n\t\t\tfloat m_moments_alpha = 0.2f;\n\t\t\tfloat m_l_phi = 4.f;\n\t\t\tfloat m_n_phi = 128.f;\n\t\t\tfloat m_z_phi = 1.0f;\n\t\t};\n\t\tRuntime m_runtime;\n\t};\n\tstruct ShadowDenoiserData\n\t{\t\t\n\t\td3d12::PipelineState* m_reprojection_pipeline;\n\t\td3d12::PipelineState* m_filter_moments_pipeline;\n\t\td3d12::PipelineState* m_wavelet_filter_pipeline;\n\n\t\tDescriptorAllocator* out_allocator;\n\t\tDescriptorAllocation out_allocation;\n\n\t\tstd::shared_ptr<ConstantBufferPool> m_constant_buffer_pool;\n\n\t\ttemp::ShadowDenoiserSettings_CBData m_denoiser_settings;\n\t\tstd::array<ConstantBufferHandle*, d3d12::settings::shadow_denoiser_wavelet_iterations> m_denoiser_settings_buffer;\n\n\t\tConstantBufferHandle* m_denoiser_camera;\n\n\t\td3d12::RenderTarget* m_input_render_target;\n\t\td3d12::RenderTarget* m_gbuffer_render_target;\n\n\t\td3d12::RenderTarget* m_in_hist_length;\n\t\t\n\t\td3d12::RenderTarget* m_in_prev_color;\n\t\td3d12::RenderTarget* m_in_prev_moments;\n\t\td3d12::RenderTarget* m_in_prev_normals;\n\t\td3d12::RenderTarget* m_in_prev_depth;\n\n\t\td3d12::RenderTarget* m_out_color_render_target;\n\t\td3d12::RenderTarget* m_out_moments_render_target;\n\t\td3d12::RenderTarget* m_out_hist_length_render_target;\n\n\t\td3d12::RenderTarget* m_ping_pong_render_target;\n\t};\n\n\tnamespace internal\n\t{\n\t\tinline void SetupShadowDenoiserTask(RenderSystem& rs, FrameGraph&  fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<ShadowDenoiserData>(handle);\n\n\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\tdata.m_reprojection_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::svgf_denoiser_reprojection);\n\t\t\tdata.m_filter_moments_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::svgf_denoiser_filter_moments);\n\t\t\tdata.m_wavelet_filter_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::svgf_denoiser_wavelet_filter);\n\n\t\t\t//Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors\n\t\t\tstd::shared_ptr<D3D12TexturePool> texture_pool = std::static_pointer_cast<D3D12TexturePool>(n_render_system.m_texture_pools[0]);\n\t\t\tif (!texture_pool)\n\t\t\t{\n\t\t\t\tLOGC(\"Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool\");\n\t\t\t}\n\n\t\t\tdata.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\tdata.out_allocation = std::move(data.out_allocator->Allocate(15));\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.m_constant_buffer_pool = n_render_system.CreateConstantBufferPool(\n\t\t\t\t\tSizeAlignTwoPower(\n\t\t\t\t\t\tsizeof(temp::ShadowDenoiserSettings_CBData), 256) * d3d12::settings::num_back_buffers * data.m_denoiser_settings_buffer.size());\n\t\t\t\tfor (int i = 0; i < data.m_denoiser_settings_buffer.size(); ++i)\n\t\t\t\t{\n\t\t\t\t\tdata.m_denoiser_settings_buffer[i] = data.m_constant_buffer_pool->Create(sizeof(temp::ShadowDenoiserSettings_CBData));\n\t\t\t\t}\t\t\t\n\t\t\t}\n\n\t\t\tdata.m_input_render_target = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<RTShadowData>());\n\n\t\t\tdata.m_gbuffer_render_target = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<DeferredMainTaskData>());\n\n\t\t\td3d12::desc::RenderTargetDesc render_target_desc = {};\n\t\t\trender_target_desc.m_clear_color[0] = 0.f;\n\t\t\trender_target_desc.m_clear_color[1] = 0.f;\n\t\t\trender_target_desc.m_clear_color[2] = 0.f;\n\t\t\trender_target_desc.m_clear_color[3] = 0.f;\n\t\t\trender_target_desc.m_create_dsv_buffer = false;\n\t\t\trender_target_desc.m_dsv_format = Format::UNKNOWN;\n\t\t\trender_target_desc.m_initial_state = ResourceState::NON_PIXEL_SHADER_RESOURCE;\n\t\t\trender_target_desc.m_num_rtv_formats = 1;\n\t\t\trender_target_desc.m_rtv_formats[0] = Format::R16_FLOAT;\n\n\t\t\tdata.m_in_hist_length = d3d12::CreateRenderTarget(\n\t\t\t\tn_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc\n\t\t\t);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[0];\n\n\t\t\tdata.m_in_prev_color = d3d12::CreateRenderTarget(\n\t\t\t\tn_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc\n\t\t\t);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = Format::R32G32_FLOAT;\n\t\t\trender_target_desc.m_create_dsv_buffer = false;\n\n\t\t\tdata.m_in_prev_moments = d3d12::CreateRenderTarget(\n\t\t\t\tn_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc\n\t\t\t);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[1];\n\n\t\t\tdata.m_in_prev_normals = d3d12::CreateRenderTarget(\n\t\t\t\tn_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc\n\t\t\t);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[4];\n\n\t\t\tdata.m_in_prev_depth = d3d12::CreateRenderTarget(\n\t\t\t\tn_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc\n\t\t\t);\n\n\t\t\tdata.m_out_color_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = Format::R32G32_FLOAT;\n\t\t\trender_target_desc.m_initial_state = ResourceState::UNORDERED_ACCESS;\n\n\t\t\tdata.m_out_moments_render_target = d3d12::CreateRenderTarget(\n\t\t\t\tn_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc\n\t\t\t);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = data.m_in_hist_length->m_create_info.m_rtv_formats[0];\n\n\t\t\tdata.m_out_hist_length_render_target = d3d12::CreateRenderTarget(\n\t\t\t\tn_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc\n\t\t\t);\n\n\t\t\trender_target_desc.m_rtv_formats[0] = data.m_out_color_render_target->m_create_info.m_rtv_formats[0];\n\n\t\t\tdata.m_ping_pong_render_target = d3d12::CreateRenderTarget(\n\t\t\t\tn_render_system.m_device,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Width,\n\t\t\t\tn_render_system.m_viewport.m_viewport.Height,\n\t\t\t\trender_target_desc\n\t\t\t);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int input = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(input);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_input_render_target, cpu_handle, 0, data.m_input_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int motion = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::MOTION);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(motion);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 3, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[3]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int normals = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::NORMAL);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(normals);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 1, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[1]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int depth = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::DEPTH);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(depth);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 4, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[4]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int hist_length = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::IN_HIST_LENGTH);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(hist_length);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_in_hist_length, cpu_handle, 0, data.m_in_hist_length->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_color = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_INPUT);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_color);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_in_prev_color, cpu_handle, 0, data.m_in_prev_color->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_moments = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_MOMENTS);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_moments);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_in_prev_moments, cpu_handle, 0, data.m_in_prev_moments->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_normals = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_NORMAL);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_normals);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_in_prev_normals, cpu_handle, 0, data.m_in_prev_normals->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_depth = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_DEPTH);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_depth);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_in_prev_depth, cpu_handle, 0, data.m_in_prev_depth->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int out_color = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(out_color);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(data.m_out_color_render_target, cpu_handle, 0, data.m_out_color_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int out_moments = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_MOMENTS);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(out_moments);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(data.m_out_moments_render_target, cpu_handle, 0, data.m_out_moments_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int out_hist_length = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_HIST_LENGTH);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(out_hist_length);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(data.m_out_hist_length_render_target, cpu_handle, 0, data.m_out_hist_length_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int input_uav = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_UAV);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(input_uav);\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(data.m_ping_pong_render_target, cpu_handle, 0, data.m_ping_pong_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int input_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_SRV);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(input_srv);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_ping_pong_render_target, cpu_handle, 0, data.m_ping_pong_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int output_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUTPUT_SRV);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(output_srv);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(data.m_out_color_render_target, cpu_handle, 0, data.m_out_color_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\t\t}\n\n\t\tinline void BindResources(D3D12RenderSystem& n_render_system, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback)\n\t\t{\n\t\t\td3d12::BindDescriptorHeaps(cmd_list, is_fallback);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int input = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(input);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int motion = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::MOTION);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(motion);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, motion, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int normals = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::NORMAL);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(normals);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, normals, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int depth = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::DEPTH);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(depth);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, depth, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int hist_length = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::IN_HIST_LENGTH);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(hist_length);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, hist_length, cpu_handle);\n\t\t\t}\n\t\t\t\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_color = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_INPUT);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_color);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, prev_color, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_moments = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_MOMENTS);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_moments);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, prev_moments, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_normals = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_NORMAL);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_normals);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, prev_normals, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int prev_depth = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_DEPTH);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_depth);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, prev_depth, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int out_color = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(out_color);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, out_color, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int out_moments = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_MOMENTS);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(out_moments);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, out_moments, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int out_hist = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_HIST_LENGTH);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(out_hist);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, out_hist, cpu_handle);\n\t\t\t}\n\t\t}\n\n\t\tinline void Reproject(D3D12RenderSystem& n_render_system, SceneGraph& sg, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback)\n\t\t{\n\t\t\tunsigned int frame_idx = n_render_system.GetFrameIdx();\n\t\t\t\t\n\t\t\tconst auto camera_cb = static_cast<D3D12ConstantBufferHandle*>(data.m_denoiser_camera);\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.m_reprojection_pipeline);\n\n\t\t\tBindResources(n_render_system, cmd_list, data, is_fallback);\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, static_cast<D3D12ConstantBufferHandle*>(data.m_denoiser_settings_buffer[0])->m_native, 1, frame_idx);\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\n\t\t}\n\n\t\tinline void StoreBuffers(D3D12RenderSystem& n_render_system, SceneGraph& sg, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback)\n\t\t{\n\t\t\td3d12::Transition(cmd_list, data.m_gbuffer_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_SOURCE);\n\n\t\t\td3d12::Transition(cmd_list, data.m_out_moments_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_out_hist_length_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\n\t\t\td3d12::Transition(cmd_list, data.m_in_prev_moments, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\t\t\td3d12::Transition(cmd_list, data.m_in_prev_normals, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\t\t\td3d12::Transition(cmd_list, data.m_in_prev_depth, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\n\t\t\td3d12::Transition(cmd_list, data.m_in_hist_length, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\n\t\t\tcmd_list->m_native->CopyResource(data.m_in_prev_moments->m_render_targets[0], data.m_out_moments_render_target->m_render_targets[0]);\n\t\t\tcmd_list->m_native->CopyResource(data.m_in_prev_normals->m_render_targets[0], data.m_gbuffer_render_target->m_render_targets[1]);\n\t\t\tcmd_list->m_native->CopyResource(data.m_in_prev_depth->m_render_targets[0], data.m_gbuffer_render_target->m_render_targets[4]);\n\t\t\tcmd_list->m_native->CopyResource(data.m_in_hist_length->m_render_targets[0], data.m_out_hist_length_render_target->m_render_targets[0]);\n\n\t\t\td3d12::Transition(cmd_list, data.m_gbuffer_render_target, ResourceState::COPY_SOURCE, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\td3d12::Transition(cmd_list, data.m_out_moments_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\t\t\td3d12::Transition(cmd_list, data.m_out_hist_length_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\n\t\t\td3d12::Transition(cmd_list, data.m_in_prev_moments, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_in_prev_normals, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\td3d12::Transition(cmd_list, data.m_in_prev_depth, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\td3d12::Transition(cmd_list, data.m_in_hist_length, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t}\n\n\t\tinline void FilterMoments(D3D12RenderSystem& n_render_system, SceneGraph& sg, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback)\n\t\t{\n\t\t\tunsigned int frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\tconst auto camera_cb = static_cast<D3D12ConstantBufferHandle*>(data.m_denoiser_camera);\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.m_filter_moments_pipeline);\n\n\t\t\tBindResources(n_render_system, cmd_list, data, is_fallback);\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, static_cast<D3D12ConstantBufferHandle*>(data.m_denoiser_settings_buffer[0])->m_native, 1, frame_idx);\n\n\t\t\td3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int input_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT);\n\t\t\t\tconstexpr unsigned int output_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUTPUT_SRV);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(output_srv);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, input_id, cpu_handle);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int output_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR);\n\t\t\t\tconstexpr unsigned int ping_ping_uav = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_UAV);\n\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(ping_ping_uav);\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, output_id, cpu_handle);\n\t\t\t}\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t\tinline void WaveletFilter(D3D12RenderSystem& n_render_system, SceneGraph& sg, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback)\n\t\t{\n\t\t\tunsigned int frame_idx = n_render_system.GetFrameIdx();\n\n\t\t\tconst auto camera_cb = static_cast<D3D12ConstantBufferHandle*>(data.m_denoiser_camera);\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.m_wavelet_filter_pipeline);\n\n\t\t\tBindResources(n_render_system, cmd_list, data, is_fallback);\n\n\t\t\tfor (int i = 0; i < data.m_denoiser_settings_buffer.size(); ++i)\n\t\t\t{\n\t\t\t\td3d12::BindComputeConstantBuffer(cmd_list, static_cast<D3D12ConstantBufferHandle*>(data.m_denoiser_settings_buffer[i])->m_native, 1, frame_idx);\n\n\t\t\t\tif (i % 2 == 0)\n\t\t\t\t{\n\t\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\td3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS);\n\n\t\t\t\t\t{\n\t\t\t\t\t\tconstexpr unsigned int input_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT);\n\t\t\t\t\t\tconstexpr unsigned int ping_pong_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_SRV);\n\t\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(ping_pong_srv);\n\t\t\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, input_id, cpu_handle);\n\t\t\t\t\t}\n\n\t\t\t\t\t{\n\t\t\t\t\t\tconstexpr unsigned int output_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR);\n\t\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(output_id);\n\t\t\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, output_id, cpu_handle);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS);\n\t\t\t\t\td3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\n\t\t\t\t\t{\n\t\t\t\t\t\tconstexpr unsigned int input_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT);\n\t\t\t\t\t\tconstexpr unsigned int output_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUTPUT_SRV);\n\t\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(output_srv);\n\t\t\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, input_id, cpu_handle);\n\t\t\t\t\t}\n\n\t\t\t\t\t{\n\t\t\t\t\t\tconstexpr unsigned int output_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR);\n\t\t\t\t\t\tconstexpr unsigned int ping_ping_uav = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_UAV);\n\t\t\t\t\t\tauto cpu_handle = data.out_allocation.GetDescriptorHandle(ping_ping_uav);\n\t\t\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, output_id, cpu_handle);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)),\n\t\t\t\t\tstatic_cast<int>(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)),\n\t\t\t\t\t1);\n\n\t\t\t\tif (i == d3d12::settings::shadow_denoiser_feedback_tap)\n\t\t\t\t{\n\t\t\t\t\tif (i % 2 == 0)\n\t\t\t\t\t{\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_in_prev_color, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\n\t\t\t\t\t\tcmd_list->m_native->CopyResource(data.m_in_prev_color->m_render_targets[0], data.m_out_color_render_target->m_render_targets[0]);\n\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_in_prev_color, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_in_prev_color, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\n\t\t\t\t\t\tcmd_list->m_native->CopyResource(data.m_in_prev_color->m_render_targets[0], data.m_ping_pong_render_target->m_render_targets[0]);\n\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\t\t\t\t\t\td3d12::Transition(cmd_list, data.m_in_prev_color, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (d3d12::settings::shadow_denoiser_wavelet_iterations % 2 == 0)\n\t\t\t{\n\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\t\t\t\td3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST);\n\n\t\t\t\tcmd_list->m_native->CopyResource(data.m_out_color_render_target->m_render_targets[0], data.m_ping_pong_render_target->m_render_targets[0]);\n\n\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\t\t\t\td3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::COPY_DEST, ResourceState::UNORDERED_ACCESS);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\td3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS);\n\t\t\t}\n\t\t}\n\n\t\tinline void ExecuteShadowDenoiserTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<ShadowDenoiserData>(handle);\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tauto render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tbool is_fallback = d3d12::GetRaytracingType(n_render_system.m_device) == RaytracingType::FALLBACK;\n\t\t\tauto settings = fg.GetSettings<ShadowDenoiserData, ShadowDenoiserSettings>();\n\n\t\t\t//Populating setting cb\n\t\t\tdata.m_denoiser_settings.m_alpha\t\t\t= settings.m_runtime.m_alpha;\n\t\t\tdata.m_denoiser_settings.m_moments_alpha\t= settings.m_runtime.m_moments_alpha;\n\t\t\tdata.m_denoiser_settings.m_l_phi\t\t\t= settings.m_runtime.m_l_phi;\n\t\t\tdata.m_denoiser_settings.m_z_phi\t\t\t= settings.m_runtime.m_z_phi;\n\t\t\tdata.m_denoiser_settings.m_n_phi\t\t\t= settings.m_runtime.m_n_phi;\n\n\t\t\tfor (int i = 0; i < data.m_denoiser_settings_buffer.size(); ++i)\n\t\t\t{\n\t\t\t\tdata.m_denoiser_settings.m_step_distance = (float)(1 << i);\n\t\t\t\t\n\t\t\t\tdata.m_constant_buffer_pool->Update(data.m_denoiser_settings_buffer[i], sizeof(temp::ShadowDenoiserSettings_CBData), 0, n_render_system.GetFrameIdx(), (uint8_t*)& data.m_denoiser_settings);\n\t\t\t}\n\n\t\t\tif (n_render_system.m_render_window.has_value())\n\t\t\t{\n\t\t\t\tconst auto viewport = n_render_system.m_viewport;\n\t\t\t\tconst auto frame_idx = n_render_system.GetFrameIdx();\n\t\t\t\t\n\t\t\t\td3d12::Transition(cmd_list, render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS);\n\n\t\t\t\tReproject(n_render_system, sg, cmd_list, data, is_fallback);\n\n\t\t\t\tStoreBuffers(n_render_system, sg, cmd_list, data, is_fallback);\n\n\t\t\t\tFilterMoments(n_render_system, sg, cmd_list, data, is_fallback);\n\n\t\t\t\tWaveletFilter(n_render_system, sg, cmd_list, data, is_fallback);\n\t\t\t\t\n\t\t\t\td3d12::Transition(cmd_list, render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE);\n\t\t\t}\n\t\t}\n\n\t\tinline void DestroyShadowDenoiserTask(FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& data = fg.GetData<ShadowDenoiserData>(handle);\n\n\t\t\td3d12::Destroy(data.m_in_hist_length);\n\t\t\td3d12::Destroy(data.m_in_prev_color);\n\t\t\td3d12::Destroy(data.m_in_prev_moments);\n\t\t\td3d12::Destroy(data.m_in_prev_normals);\n\t\t\td3d12::Destroy(data.m_in_prev_depth);\n\t\t\td3d12::Destroy(data.m_out_hist_length_render_target);\n\t\t\td3d12::Destroy(data.m_out_moments_render_target);\n\t\t\td3d12::Destroy(data.m_ping_pong_render_target);\n\n\t\t\tif (!resize) {\n\t\t\t\tfor (auto buffer : data.m_denoiser_settings_buffer)\n\t\t\t\t{\n\t\t\t\t\tdata.m_constant_buffer_pool->Destroy(buffer);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t} /* internal */\n\n\t// pass a path to a texture location to load a custom denoiser kernel\n\tinline void AddShadowDenoiserTask(FrameGraph& fg)\n\t{\n\t\tstd::wstring name(L\"Shadow Denoiser\");\n\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(true),\n\t\t\tRenderTargetProperties::ClearDepth(true),\n\t\t\tRenderTargetProperties::ResolutionScalar(1.0f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tinternal::SetupShadowDenoiserTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tinternal::ExecuteShadowDenoiserTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tinternal::DestroyShadowDenoiserTask(fg, handle, resize);\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tfg.AddTask<ShadowDenoiserData>(desc, name, FG_DEPS<RTShadowData>());\n\t\tfg.UpdateSettings<ShadowDenoiserData>(ShadowDenoiserSettings());\n\t}\n\n}/* wr */"
  },
  {
    "path": "src/render_tasks/d3d12_spatial_reconstruction.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../frame_graph/frame_graph.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n#include \"../render_tasks/d3d12_rt_reflection_task.hpp\"\n#include \"../render_tasks/d3d12_deferred_main.hpp\"\n\nnamespace wr\n{\n\tstruct SpatialReconstructionData\n\t{\n\t\td3d12::PipelineState* pipeline;\n\t\tstd::shared_ptr<ConstantBufferPool> m_cb_pool;\n\t\tD3D12ConstantBufferHandle* camera_cb;\n\t\tDescriptorAllocator* allocator;\n\t\tDescriptorAllocation output;\n\t\tDescriptorAllocation reflection_buffer;\n\t\tDescriptorAllocation gbuffer_normal;\n\t\tDescriptorAllocation gbuffer_roughness;\n\t\tDescriptorAllocation gbuffer_depth;\n\t\tstd::uint32_t frame_idx;\n\t};\n\n\tnamespace temp\n\t{\n\n\t\tstruct SpatialReconstructionCameraData\n\t\t{\n\t\t\tDirectX::XMMATRIX inv_vp;\n\t\t\tDirectX::XMMATRIX inv_view;\n\n\t\t\tfloat padding;\n\t\t\tstd::uint32_t frame_idx;\n\t\t\tfloat near_plane, far_plane;\n\t\t};\n\n\t}\n\n\tnamespace internal\n\t{\n\n\t\tinline void SetupSpatialReconstructionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& data = fg.GetData<SpatialReconstructionData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto hybrid_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<RTReflectionData>());\n\t\t\tauto gbuffer_rt = static_cast<d3d12::RenderTarget*>(fg.GetPredecessorRenderTarget<DeferredMainTaskData>());\n\n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tdata.frame_idx = 0;\n\t\t\t\tdata.allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV);\n\t\t\t\tdata.output = data.allocator->Allocate();\n\t\t\t\tdata.reflection_buffer = data.allocator->Allocate(2);\n\t\t\t\tdata.gbuffer_normal = data.allocator->Allocate(d3d12::settings::num_back_buffers);\n\t\t\t\tdata.gbuffer_roughness = data.allocator->Allocate(d3d12::settings::num_back_buffers);\n\t\t\t\tdata.gbuffer_depth = data.allocator->Allocate();\n\n\t\t\t\tauto& ps_registry = PipelineRegistry::Get();\n\t\t\t\tdata.pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::spatial_reconstruction);\n\n\t\t\t\tdata.m_cb_pool = n_render_system.CreateConstantBufferPool(sizeof(temp::SpatialReconstructionCameraData) * d3d12::settings::num_back_buffers);\n\n\t\t\t\tdata.camera_cb = static_cast<D3D12ConstantBufferHandle*>(data.m_cb_pool->Create(sizeof(temp::SpatialReconstructionCameraData)));\n\t\t\t}\n\n\t\t\t// Deferred data\n\n\t\t\tfor (uint32_t i = 0; i < d3d12::settings::num_back_buffers; ++i)\n\t\t\t{\n\t\t\t\tauto albedo_handle = data.gbuffer_roughness.GetDescriptorHandle(i);\n\t\t\t\tauto normal_handle = data.gbuffer_normal.GetDescriptorHandle(i);\n\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(gbuffer_rt, albedo_handle, 0, gbuffer_rt->m_create_info.m_rtv_formats[0]);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(gbuffer_rt, normal_handle, 1, gbuffer_rt->m_create_info.m_rtv_formats[1]);\n\n\t\t\t}\n\n\t\t\tauto depth_handle = data.gbuffer_depth.GetDescriptorHandle();\n\t\t\td3d12::CreateSRVFromDSV(gbuffer_rt, depth_handle);\n\n\n\t\t\t// Filtered output\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.output.GetDescriptorHandle();\n\t\t\t\td3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]);\n\t\t\t}\n\n\t\t\t// Reflection buffer input\n\t\t\t{\n\t\t\t\tauto cpu_handle = data.reflection_buffer.GetDescriptorHandle();\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(hybrid_rt, cpu_handle, 0, hybrid_rt->m_create_info.m_rtv_formats[0]);\n\n\t\t\t\tcpu_handle = data.reflection_buffer.GetDescriptorHandle(1);\n\t\t\t\td3d12::CreateSRVFromSpecificRTV(hybrid_rt, cpu_handle, 2, hybrid_rt->m_create_info.m_rtv_formats[2]);\n\t\t\t}\n\n\t\t}\n\n\t\tinline void ExecuteSpatialReconstructionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle)\n\t\t{\n\t\t\tauto& n_render_system = static_cast<D3D12RenderSystem&>(rs);\n\t\t\tauto& n_device = n_render_system.m_device->m_native;\n\t\t\tauto& data = fg.GetData<SpatialReconstructionData>(handle);\n\t\t\tauto n_render_target = fg.GetRenderTarget<d3d12::RenderTarget>(handle);\n\t\t\tauto frame_idx = n_render_system.GetFrameIdx();\n\t\t\tauto cmd_list = fg.GetCommandList<d3d12::CommandList>(handle);\n\t\t\tconst auto viewport = n_render_system.m_viewport;\n\n\t\t\tfg.WaitForPredecessorTask<DeferredMainTaskData>();\n\t\t\tfg.WaitForPredecessorTask<RTReflectionData>();\n\n\t\t\td3d12::BindComputePipeline(cmd_list, data.pipeline);\n\n\t\t\t// Update camera constant buffer\n\t\t\tauto camera = sg.GetActiveCamera();\n\t\t\ttemp::SpatialReconstructionCameraData cam_data;\n\t\t\tcam_data.inv_view = DirectX::XMMatrixInverse(nullptr, camera->m_view);\n\t\t\tcam_data.inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection);\n\t\t\tcam_data.near_plane = camera->m_frustum_near;\n\t\t\tcam_data.far_plane = camera->m_frustum_far;\n\t\t\tcam_data.frame_idx = ++data.frame_idx;\n\t\t\tdata.m_cb_pool->Update(data.camera_cb, sizeof(temp::SpatialReconstructionCameraData), 0, frame_idx, (std::uint8_t*)&cam_data);\n\n\t\t\td3d12::BindComputeConstantBuffer(cmd_list, data.camera_cb->m_native, 1, frame_idx);\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::spatial_reconstruction, params::SpatialReconstructionE::OUTPUT);\n\t\t\t\tauto handle_uav = data.output.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::spatial_reconstruction, params::SpatialReconstructionE::REFLECTION_BUFFER);\n\t\t\t\tauto handle_srv = data.reflection_buffer.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, dest_idx, handle_srv);\n\t\t\t\thandle_srv = data.reflection_buffer.GetDescriptorHandle(1);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, dest_idx + 1, handle_srv);\n\t\t\t}\n\n\t\t\t{\n\t\t\t\tconstexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::spatial_reconstruction, params::SpatialReconstructionE::GBUFFERS); \n\n\t\t\t\td3d12::DescHeapCPUHandle albedo_handle = data.gbuffer_roughness.GetDescriptorHandle(frame_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, dest_idx, albedo_handle);\n\t\t\t\t\n\t\t\t\td3d12::DescHeapCPUHandle normal_handle = data.gbuffer_normal.GetDescriptorHandle(frame_idx);\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, dest_idx + 1, normal_handle);\n\t\t\t\t\n\t\t\t\td3d12::DescHeapCPUHandle depth_handle = data.gbuffer_depth.GetDescriptorHandle();\n\t\t\t\td3d12::SetShaderSRV(cmd_list, 0, dest_idx + 2, depth_handle);\n\t\t\t}\n\n\t\t\td3d12::Dispatch(cmd_list,\n\t\t\t\tuint32_t(std::ceil(viewport.m_viewport.Width / 16.f)),\n\t\t\t\tuint32_t(std::ceil(viewport.m_viewport.Height / 16.f)),\n\t\t\t\t1);\n\t\t}\n\n\t} /* internal */\n\n\tinline void AddSpatialReconstructionTask(FrameGraph& frame_graph)\n\t{\n\t\tRenderTargetProperties rt_properties\n\t\t{\n\t\t\tRenderTargetProperties::IsRenderWindow(false),\n\t\t\tRenderTargetProperties::Width(std::nullopt),\n\t\t\tRenderTargetProperties::Height(std::nullopt),\n\t\t\tRenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS),\n\t\t\tRenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE),\n\t\t\tRenderTargetProperties::CreateDSVBuffer(false),\n\t\t\tRenderTargetProperties::DSVFormat(Format::UNKNOWN),\n\t\t\tRenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }),\n\t\t\tRenderTargetProperties::NumRTVFormats(1),\n\t\t\tRenderTargetProperties::Clear(false),\n\t\t\tRenderTargetProperties::ClearDepth(false),\n\t\t\tRenderTargetProperties::ResolutionScalar(1.f)\n\t\t};\n\n\t\tRenderTaskDesc desc;\n\t\tdesc.m_setup_func = [](RenderSystem & rs, FrameGraph & fg, RenderTaskHandle handle, bool resize) {\n\t\t\tinternal::SetupSpatialReconstructionTask(rs, fg, handle, resize);\n\t\t};\n\t\tdesc.m_execute_func = [](RenderSystem & rs, FrameGraph & fg, SceneGraph & sg, RenderTaskHandle handle) {\n\t\t\tinternal::ExecuteSpatialReconstructionTask(rs, fg, sg, handle);\n\t\t};\n\t\tdesc.m_destroy_func = [](FrameGraph & fg, RenderTaskHandle handle, bool resize) { \n\t\t\tif (!resize)\n\t\t\t{\n\t\t\t\tauto& data = fg.GetData<SpatialReconstructionData>(handle);\n\t\t\t\tdelete data.allocator;\n\t\t\t\tdata.m_cb_pool.reset();\n\t\t\t}\n\t\t};\n\n\t\tdesc.m_properties = rt_properties;\n\t\tdesc.m_type = RenderTaskType::COMPUTE;\n\t\tdesc.m_allow_multithreading = true;\n\n\t\tframe_graph.AddTask<SpatialReconstructionData>(desc, L\"Spatial reconstruction\", FG_DEPS<DeferredMainTaskData, RTReflectionData>());\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/renderer.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"renderer.hpp\"\n\nvoid wr::RenderSystem::RequestRenderTargetSaveToDisc(std::string const & path, RenderTarget* render_target, unsigned int index)\n{\n\tm_requested_rt_saves.emplace(SaveRenderTargetRequest{ path, render_target, index });\n}\n"
  },
  {
    "path": "src/renderer.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <optional>\n#include <memory>\n#include <queue>\n\n#include \"engine_registry.hpp\"\n#include \"platform_independend_structs.hpp\"\n#include \"structs.hpp\"\n\nnamespace wr\n{\n\tstruct Model;\n\tstruct CPUTextures;\n\n\tclass Window;\n\tclass SceneGraph;\n\tclass TexturePool;\n\tclass MaterialPool;\n\tclass ModelPool;\n\tclass ConstantBufferPool;\n\tclass StructuredBufferPool;\n\tclass FrameGraph;\n\n\tclass RenderSystem\n\t{\n\tpublic:\n\t\tRenderSystem() = default;\n\t\tvirtual ~RenderSystem() = default;\n\n\t\tRenderSystem(RenderSystem const &) = delete;\n\t\tRenderSystem& operator=(RenderSystem const &) = delete;\n\t\tRenderSystem(RenderSystem&&) = delete;\n\t\tRenderSystem& operator=(RenderSystem&&) = delete;\n\n\t\tvirtual std::shared_ptr<TexturePool> CreateTexturePool() = 0;\n\t\tvirtual std::shared_ptr<MaterialPool> CreateMaterialPool(std::size_t size_in_mb) = 0;\n\t\tvirtual std::shared_ptr<ModelPool> CreateModelPool(std::size_t vertex_buffer_pool_size_in_mb, std::size_t index_buffer_pool_size_in_mb) = 0;\n\t\tvirtual std::shared_ptr<ConstantBufferPool> CreateConstantBufferPool(std::size_t size_in_mb) = 0;\n\t\tvirtual std::shared_ptr<StructuredBufferPool> CreateStructuredBufferPool(std::size_t size_in_mb) = 0;\n\n\t\tvirtual std::shared_ptr<TexturePool> GetDefaultTexturePool() = 0;\n\n\t\tvirtual void PrepareRootSignatureRegistry() = 0;\n\t\tvirtual void PrepareShaderRegistry() = 0;\n\t\tvirtual void PreparePipelineRegistry() = 0;\n\t\tvirtual void PrepareRTPipelineRegistry() = 0;\n\t\tvirtual void DestroyRootSignatureRegistry() = 0;\n\t\tvirtual void DestroyShaderRegistry() = 0;\n\t\tvirtual void DestroyPipelineRegistry() = 0;\n\t\tvirtual void DestroyRTPipelineRegistry() = 0;\n\n\t\tvirtual void WaitForAllPreviousWork() = 0;\n\n\t\tvirtual CommandList* GetDirectCommandList(unsigned int num_allocators) = 0;\n\t\tvirtual CommandList* GetBundleCommandList(unsigned int num_allocators) = 0;\n\t\tvirtual CommandList* GetComputeCommandList(unsigned int num_allocators) = 0;\n\t\tvirtual CommandList* GetCopyCommandList(unsigned int num_allocators) = 0;\n\t\tvirtual void SetCommandListName(CommandList* cmd_list, std::wstring const & name) = 0;\n\t\tvirtual void DestroyCommandList(CommandList* cmd_list) = 0;\n\t\tvirtual RenderTarget* GetRenderTarget(RenderTargetProperties properties) = 0;\n\t\tvirtual void SetRenderTargetName(RenderTarget* cmd_list, std::wstring const & name) = 0;\n\t\tvirtual void ResizeRenderTarget(RenderTarget** render_target, std::uint32_t width, std::uint32_t height) = 0;\n\t\tvirtual void DestroyRenderTarget(RenderTarget** render_target) = 0;\n\n\t\tvirtual void ResetCommandList(CommandList* cmd_list) = 0;\n\t\tvirtual void CloseCommandList(CommandList* cmd_list) = 0;\n\t\tvirtual void StartRenderTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target) = 0;\n\t\tvirtual void StopRenderTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target) = 0;\n\t\tvirtual void StartComputeTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target) = 0;\n\t\tvirtual void StopComputeTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target) = 0;\n\t\tvirtual void StartCopyTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target) = 0;\n\t\tvirtual void StopCopyTask(CommandList* cmd_list, std::pair<RenderTarget*, RenderTargetProperties> render_target) = 0;\n\n\t\tvirtual void Init(std::optional<Window*> window) = 0;\n\t\tvirtual CPUTextures Render(SceneGraph & scene_graph, FrameGraph & frame_graph) = 0;\n\t\tvirtual void Resize(std::uint32_t width, std::uint32_t height) = 0;\n\t\tvoid RequestRenderTargetSaveToDisc(std::string const& path, RenderTarget* render_target, unsigned int index);\n\t\t\n\t\tvirtual unsigned int GetFrameIdx() = 0;\n\t\tvirtual void RequestSkyboxReload() = 0;\n\n\t\tstd::optional<Window*> m_window;\n\n\t\tenum class SimpleShapes : std::size_t\n\t\t{\n\t\t\tCUBE,\n\t\t\tPLANE,\n\n\t\t\tCOUNT\n\t\t};\n\n\t\t//SimpleShapes don't have a material attached to them. The user is expected to provide one.\n\t\tvirtual wr::Model* GetSimpleShape(SimpleShapes type) = 0;\n\n\t\tTextureHandle GetDefaultAlbedo() const { return m_default_albedo; }\n\t\tTextureHandle GetDefaultNormal() const { return m_default_normal; }\n\t\tTextureHandle GetDefaultRoughness() const { return m_default_white; }\n\t\tTextureHandle GetDefaultMetalic() const { return m_default_black; }\n\t\tTextureHandle GetDefaultAO() const { return m_default_white; }\n\t\tTextureHandle GetDefaultEmissive() const { return m_default_black; }\n\n\t\tstd::shared_ptr<ModelPool> m_shapes_pool;\n\t\tstd::array<wr::Model*, static_cast<std::size_t>(SimpleShapes::COUNT)> m_simple_shapes;\n\n\t\twr::TextureHandle m_default_cubemap;\n\t\twr::TextureHandle m_default_albedo;\n\t\twr::TextureHandle m_default_normal;\n\t\twr::TextureHandle m_default_white;\n\t\twr::TextureHandle m_default_black;\n\n\tprotected:\n\t\tstruct SaveRenderTargetRequest\n\t\t{\n\t\t\tstd::string m_path;\n\t\t\tRenderTarget* m_render_target;\n\t\t\tunsigned int m_index;\n\t\t};\n\n\t\tvirtual void SaveRenderTargetToDisc(std::string const & path, RenderTarget* render_target, unsigned int index) = 0;\n\n\t\tstd::queue<SaveRenderTargetRequest> m_requested_rt_saves;\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/resource_pool_texture.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"resource_pool_texture.hpp\"\n#include \"util/log.hpp\"\n#include \"util/strings.hpp\"\n\nnamespace wr\n{\n\n\tTexturePool::TexturePool()\n\t{\n#ifdef _DEBUG\n\t\tstd::lock_guard<std::mutex> lock(m_mutex);\n\n\t\tstatic uint16_t pool_count = 0u;\n\t\t\n\t\tm_name = \"TexturePool_\" + std::to_string(pool_count);\n\n\t\tpool_count++;\n#endif\n\t}\n\n\tTextureHandle TexturePool::LoadFromMemory(unsigned char* data, size_t width, size_t height, const std::string& texture_extension, bool srgb, bool generate_mips)\n\t{\n\t\tstd::string new_str = texture_extension;\n\n\t\tstd::transform(texture_extension.begin(), texture_extension.end(), new_str.begin(), ::tolower);\n\n\t\tTextureFormat type;\n\n\t\tif (new_str == \"png\"|| new_str == \"jpg\"\n\t\t\t|| new_str == \"jpeg\" || new_str == \"bmp\")\n\t\t{\n\t\t\ttype = TextureFormat::WIC;\n\t\t}\n\t\telse if (new_str.compare(\"dds\") == 0)\n\t\t{\n\t\t\ttype = TextureFormat::DDS;\n\t\t}\n\t\telse if (new_str.compare(\"hdr\") == 0)\n\t\t{\n\t\t\ttype = TextureFormat::HDR;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tLOGC(\"[ERROR]: Texture format not supported.\");\n\t\t\treturn {};\n\t\t}\n\n\t\tstd::lock_guard<std::mutex> lock(m_mutex);\n\t\treturn LoadFromMemory(data, width, height, type, srgb, generate_mips);\n\t}\n}"
  },
  {
    "path": "src/resource_pool_texture.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <string_view>\n#include <vector>\n#include <unordered_map>\n#include <optional>\n#include <d3d12.h>\n#include <mutex>\n\n#include \"structs.hpp\"\n#include \"util/defines.hpp\"\n#include \"util/strings.hpp\"\n#include \"platform_independend_structs.hpp\"\n#include \"id_factory.hpp\"\n\nnamespace wr\n{\n\tenum class TextureFormat\n\t{\n\t\tWIC,\n\t\tDDS,\n\t\tHDR,\n\t\tRAW\n\t};\n\n\tclass TexturePool\n\t{\n\tpublic:\n\t\texplicit TexturePool();\n\t\tvirtual ~TexturePool() = default;\n\n\t\tTexturePool(TexturePool const &) = delete;\n\t\tTexturePool& operator=(TexturePool const &) = delete;\n\t\tTexturePool(TexturePool&&) = delete;\n\t\tTexturePool& operator=(TexturePool&&) = delete;\n\n\t\t[[nodiscard]] virtual TextureHandle LoadFromFile(std::string_view path, bool srgb, bool generate_mips) = 0;\n\t\t[[nodiscard]] virtual TextureHandle LoadFromMemory(unsigned char* data, size_t width, size_t height, const std::string& texture_extension, bool srgb, bool generate_mips);\n\t\t[[nodiscard]] virtual TextureHandle LoadFromMemory(unsigned char* data, size_t width, size_t height, TextureFormat type, bool srgb, bool generate_mips) = 0;\n\t\t[[nodiscard]] virtual TextureHandle CreateCubemap(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest) = 0;\n\t\t[[nodiscard]] virtual TextureHandle CreateTexture(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest) = 0;\n\t\tvirtual void MarkForUnload(TextureHandle& handle, unsigned int frame_idx) = 0;\n\t\tvirtual void UnloadTextures(unsigned int frame_idx) = 0;\n\n\t\tvirtual void Evict() = 0;\n\t\tvirtual void MakeResident() = 0;\n\t\tvirtual void Stage(CommandList* cmd_list) = 0;\n\t\tvirtual void PostStageClear() = 0;\n\t\tvirtual void ReleaseTemporaryResources() = 0;\n\n\t\tvirtual Texture* GetTextureResource(TextureHandle handle) = 0;\n\n\tprotected:\n\n\t\tstd::size_t m_loaded_textures = 0;\n\t\tstd::mutex m_mutex;\n\n\t\tIDFactory m_id_factory;\n\n#ifdef _DEBUG\n\t\tstd::string m_name;\n#endif\n\t};\n\n\n}\n"
  },
  {
    "path": "src/root_signature_registry.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"root_signature_registry.hpp\"\n\nnamespace wr\n{\n\n\tRootSignatureRegistry::RootSignatureRegistry() : Registry<RootSignatureRegistry, RootSignature, RootSignatureDescription>()\n\t{\n\t}\n\n\tRegistryHandle RootSignatureRegistry::Register(RootSignatureDescription description)\n\t{\n\t\tauto handle = m_next_handle;\n\n\t\tm_descriptions.insert({ handle, description });\n\n\t\tm_next_handle++;\n\t\treturn handle;\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/root_signature_registry.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"registry.hpp\"\n\n#include <vector>\n\n#include \"d3d12/d3d12_enums.hpp\"\n#include \"d3d12/d3d12_structs.hpp\"\n#include \"d3d12/d3dx12.hpp\"\n#include \"util/named_type.hpp\"\n\nnamespace wr\n{\n\tusing RootSignature = void;\n\n\tstruct RootSignatureDescription\n\t{\n\t\tstd::vector<CD3DX12_ROOT_PARAMETER> m_parameters; // TODO: Write platform independend version.\n\t\tstd::vector<d3d12::desc::SamplerDesc> m_samplers; // TODO: Move to platform independed location\n\t\tbool m_rtx = false;\n\t\tbool m_rtx_local = false;\n\t\tstd::wstring name = L\"Unknown root signature\";\n\t};\n\n\tclass RootSignatureRegistry : public internal::Registry<RootSignatureRegistry, RootSignature, RootSignatureDescription>\n\t{\n\tpublic:\n\t\tRootSignatureRegistry();\n\t\tvirtual ~RootSignatureRegistry() = default;\n\n\t\tRegistryHandle Register(RootSignatureDescription description);\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/rt_pipeline_registry.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"rt_pipeline_registry.hpp\"\n\nnamespace wr\n{\n\n\tRTPipelineRegistry::RTPipelineRegistry() : Registry<RTPipelineRegistry, StateObject, StateObjectDescription>()\n\t{\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/rt_pipeline_registry.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"registry.hpp\"\n\n#include <array>\n#include <optional>\n\n#include \"vertex.hpp\"\n#include \"d3d12/d3dx12.hpp\"\n#include \"d3d12/d3d12_enums.hpp\"\n#include \"util/named_type.hpp\"\n\nnamespace wr\n{\n\n\tusing StateObject = void;\n\n\tstruct StateObjectDescription\n\t{\n\t\tstruct LibraryDesc\n\t\t{\n\t\t\tRegistryHandle shader_handle;\n\t\t\tstd::vector<std::wstring> exports;\n\t\t\tstd::vector<std::pair<std::wstring, std::wstring>> m_hit_groups; // first = hit group | second = entry\n\t\t};\n\n\t\tCD3DX12_STATE_OBJECT_DESC desc;\n\t\tLibraryDesc library_desc;\n\n\t\tstd::uint64_t max_payload_size;\n\t\tstd::uint64_t max_attributes_size;\n\t\tstd::uint64_t max_recursion_depth;\n\n\t\tstd::optional<RegistryHandle> global_root_signature;\n\t\tstd::vector<RegistryHandle> local_root_signatures;\n\t};\n\n\tclass RTPipelineRegistry : public internal::Registry<RTPipelineRegistry, StateObject, StateObjectDescription>\n\t{\n\tpublic:\n\t\tRTPipelineRegistry();\n\t\tvirtual ~RTPipelineRegistry() = default;\n\n\t\tRegistryHandle Register(StateObjectDescription description)\n\t\t{\n\t\t\tauto handle = m_next_handle;\n\n\t\t\tm_descriptions.insert({ handle, description });\n\n\t\t\tm_next_handle++;\n\t\t\treturn handle;\n\t\t}\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/scene_graph/camera_node.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"camera_node.hpp\"\n\n#include \"../util/aabb.hpp\"\n#include \"mesh_node.hpp\"\n\nnamespace wr\n{\n\n\tvoid CameraNode::SetFov(float deg)\n\t{\n\t\tm_fov.m_fov = deg / 180.0f * 3.1415926535f;\n\t\tSignalChange();\n\t}\n\n\tvoid CameraNode::SetFovFromFocalLength(float aspect_ratio, float filmSize)\n\t{\n\t\tfloat verticalSize = filmSize / aspect_ratio;\n\t\tm_fov.m_fov = 2.0f * std::atan2(verticalSize, 2.0f * m_focal_length);\n\t\tSignalChange();\n\t}\n\n\tvoid CameraNode::SetAspectRatio(float ratio)\n\t{\n\t\tm_aspect_ratio = ratio;\n\t\tSignalChange();\n\t}\n\n\tvoid CameraNode::SetFrustumNear(float value) noexcept\n\t{\n\t\tm_frustum_near = value;\n\t\tSignalChange();\n\t}\n\n\tvoid CameraNode::SetFrustumFar(float value) noexcept\n\t{\n\t\tm_frustum_far = value;\n\t\tSignalChange();\n\t}\n\n\tvoid CameraNode::SetFocalLength(float length)\n\t{\n\t\tm_focal_length = length;\n\t\tSetFovFromFocalLength(m_aspect_ratio, m_film_size);\n\t\tSignalChange();\n\t}\n\n\tvoid CameraNode::SetProjectionOffset(float x, float y)\n\t{\n\t\tm_projection_offset_x = x;\n\t\tm_projection_offset_y = y;\n\t}\n\n\tstd::pair<float, float> CameraNode::GetProjectionOffset()\n\t{\n\t\treturn std::pair<float, float>(m_projection_offset_x, m_projection_offset_y);\n\t}\n\n\tvoid CameraNode::SetOrthographicResolution(std::uint32_t width, std::uint32_t height)\n\t{\n\t\tm_ortho_res.m_width = static_cast<int>(width);\n\n\t\t//Window resolution counts in negative on Y axis, therefore conversion to signed is needed.\n\t\tm_ortho_res.m_height = static_cast<int>(height) * -1;\n\n\t\tSignalChange();\n\t}\n\n\tvoid CameraNode::UpdateTemp(unsigned int frame_idx)\n\t{\n\t\tDirectX::XMVECTOR pos = { m_transform.r[3].m128_f32[0], m_transform.r[3].m128_f32[1], m_transform.r[3].m128_f32[2] };\n\n\t\tDirectX::XMVECTOR up = DirectX::XMVector3Normalize(m_transform.r[1]);\n\t\tDirectX::XMVECTOR forward = DirectX::XMVectorNegate(DirectX::XMVector3Normalize(m_transform.r[2]));\n\t\tDirectX::XMVECTOR right = DirectX::XMVector3Normalize(m_transform.r[0]);\n\n\t\tm_prev_view = m_view;\n\t\tm_prev_projection = m_projection;\n\n\t\tm_view = DirectX::XMMatrixLookToRH(pos, forward, up);\n\t\t\n\t\tif (!m_override_projection)\n\t\t{\n\t\t\tm_projection = DirectX::XMMatrixPerspectiveFovRH(m_fov.m_fov, m_aspect_ratio, m_frustum_near, m_frustum_far);\n\n\t\t\tif (m_enable_orthographic)\n\t\t\t{\n\t\t\t\tm_projection = DirectX::XMMatrixOrthographicOffCenterRH(\n\t\t\t\t\t0,\n\t\t\t\t\tstatic_cast<float>(m_ortho_res.m_width), \n\t\t\t\t\tstatic_cast<float>(m_ortho_res.m_height), \n\t\t\t\t\t0,\n\t\t\t\t\tm_frustum_near,\n\t\t\t\t\tm_frustum_far);\n\t\t\t}\n\n\t\t\tm_projection.r[2].m128_f32[0] += m_projection_offset_x;\n\t\t\tm_projection.r[2].m128_f32[1] += m_projection_offset_y;\n\t\t}\n\n\t\tm_view_projection = m_view * m_projection;\n\t\tm_inverse_projection = DirectX::XMMatrixInverse(nullptr, m_projection);\n\t\tm_inverse_view = DirectX::XMMatrixInverse(nullptr, m_view);\n\n\t\tCalculatePlanes();\n\n\t\tSignalUpdate(frame_idx);\n\t}\n\n\t//Frustum culling code;\n\t//optimized and refactored version of\n\t//https://www.braynzarsoft.net/viewtutorial/q16390-34-aabb-cpu-side-frustum-culling\n\tvoid CameraNode::CalculatePlanes()\n\t{\n\t\t//Left plane\n\n\t\tm_planes[0] = DirectX::XMPlaneNormalize({\n\t\t\tm_view_projection.r[0].m128_f32[3] + *m_view_projection.r[0].m128_f32,\n\t\t\tm_view_projection.r[1].m128_f32[3] + *m_view_projection.r[1].m128_f32,\n\t\t\tm_view_projection.r[2].m128_f32[3] + *m_view_projection.r[2].m128_f32,\n\t\t\tm_view_projection.r[3].m128_f32[3] + *m_view_projection.r[3].m128_f32\n\t\t\t});\n\n\t\t//Right plane\n\n\t\tm_planes[1] = DirectX::XMPlaneNormalize({\n\t\t\tm_view_projection.r[0].m128_f32[3] - *m_view_projection.r[0].m128_f32,\n\t\t\tm_view_projection.r[1].m128_f32[3] - *m_view_projection.r[1].m128_f32,\n\t\t\tm_view_projection.r[2].m128_f32[3] - *m_view_projection.r[2].m128_f32,\n\t\t\tm_view_projection.r[3].m128_f32[3] - *m_view_projection.r[3].m128_f32\n\t\t\t});\n\n\t\t//Top plane\n\n\t\tm_planes[2] = DirectX::XMPlaneNormalize({\n\t\t\tm_view_projection.r[0].m128_f32[3] - m_view_projection.r[0].m128_f32[1],\n\t\t\tm_view_projection.r[1].m128_f32[3] - m_view_projection.r[1].m128_f32[1],\n\t\t\tm_view_projection.r[2].m128_f32[3] - m_view_projection.r[2].m128_f32[1],\n\t\t\tm_view_projection.r[3].m128_f32[3] - m_view_projection.r[3].m128_f32[1]\n\t\t\t});\n\n\t\t//Bottom plane\n\n\t\tm_planes[3] = DirectX::XMPlaneNormalize({\n\t\t\tm_view_projection.r[0].m128_f32[3] + m_view_projection.r[0].m128_f32[1],\n\t\t\tm_view_projection.r[1].m128_f32[3] + m_view_projection.r[1].m128_f32[1],\n\t\t\tm_view_projection.r[2].m128_f32[3] + m_view_projection.r[2].m128_f32[1],\n\t\t\tm_view_projection.r[3].m128_f32[3] + m_view_projection.r[3].m128_f32[1]\n\t\t\t});\n\n\t\t//Near plane\n\n\t\tm_planes[4] = DirectX::XMPlaneNormalize({\n\t\t\tm_view_projection.r[0].m128_f32[2],\n\t\t\tm_view_projection.r[1].m128_f32[2],\n\t\t\tm_view_projection.r[2].m128_f32[2],\n\t\t\tm_view_projection.r[3].m128_f32[2]\n\t\t\t});\n\n\t\t//Far plane\n\n\t\tm_planes[5] = DirectX::XMPlaneNormalize({\n\t\t\tm_view_projection.r[0].m128_f32[3] - m_view_projection.r[0].m128_f32[2],\n\t\t\tm_view_projection.r[1].m128_f32[3] - m_view_projection.r[1].m128_f32[2],\n\t\t\tm_view_projection.r[2].m128_f32[3] - m_view_projection.r[2].m128_f32[2],\n\t\t\tm_view_projection.r[3].m128_f32[3] - m_view_projection.r[3].m128_f32[2]\n\t\t});\n\n\t}\n\n\tbool CameraNode::InView(const std::shared_ptr<MeshNode>& node) const\n\t{\n\t\tconst AABB &aabb = node->m_aabb;\n\t\treturn aabb.InFrustum(m_planes);\n\t}\n\n\tbool CameraNode::InRange(const std::shared_ptr<MeshNode> &node, const float dist) const\n\t{\n\t\tconst AABB &aabb = node->m_aabb;\n\t\tconst Sphere sphere{ m_position, dist };\n\t\treturn aabb.Contains(sphere);\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/scene_graph/camera_node.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"node.hpp\"\n\n#include <array>\n\n#include \"../util/named_type.hpp\"\n#include \"../constant_buffer_pool.hpp\"\n\nnamespace wr\n{\n\n\tstruct MeshNode;\n\n\tstruct CameraNode : Node\n\t{\n\t\tusing FovDefault = util::NamedType<float>;\n\t\tusing FovFocalLength = util::NamedType<float>;\n\t\tusing FovAspectRatio = util::NamedType<float>;\n\t\tusing FovFilmSize = util::NamedType<float>;\n\t\tusing OrthographicWidth = util::NamedType<int>;\n\t\tusing OrthographicHeight = util::NamedType<int>;\n\n\t\tstruct FoV\n\t\t{\n\t\t\texplicit FoV(FovDefault deg) : m_fov(deg.Get() / 180.0f * 3.1415926535f)\n\t\t\t{\n\t\t\t}\n\n\t\t\tFoV(FovFocalLength focal_length, FovAspectRatio aspect_ratio, FovFilmSize film_size) :\n\t\t\t\tm_fov(2.0f * std::atan2(film_size.Get() / aspect_ratio.Get(), 2.0f * focal_length.Get()))\n\t\t\t{\n\t\t\t}\n\n\t\t\tfloat m_fov;\n\t\t};\n\n\t\tstruct OrthographicResolution\n\t\t{\n\t\t\texplicit OrthographicResolution(OrthographicWidth width, OrthographicHeight height) : m_width(width), m_height(height * -1)\n\t\t\t{\n\t\t\t}\n\n\t\t\tint m_width;\n\t\t\tint m_height;\n\t\t};\n\n\t\tCameraNode(float aspect_ratio)\n\t\t\t: Node(typeid(CameraNode)),\n\t\t\tm_active(true),\n\t\t\tm_frustum_near(0.1f),\n\t\t\tm_frustum_far(10000.0f),\n\t\t\tm_aspect_ratio(aspect_ratio),\n\t\t\tm_focal_length(35.0f),\n\t\t\tm_film_size(45.0f),\n\t\t\tm_fov(FovFocalLength(m_focal_length), FovAspectRatio(aspect_ratio), FovFilmSize(m_film_size)),\n\t\t\tm_f_number(32.0f),\n\t\t\tm_shape_amt(0.0f),\n\t\t\tm_aperture_blades(5),\n\t\t\tm_focus_dist(0),\n\t\t\tm_dof_range(1.0f),\n\t\t\tm_override_projection(false),\n\t\t\tm_projection_offset_x(0),\n\t\t\tm_projection_offset_y(0),\n\t\t\tm_view(),\n\t\t\tm_inverse_view(),\n\t\t\tm_projection(),\n\t\t\tm_inverse_projection(),\n\t\t\tm_view_projection(),\n\t\t\tm_camera_cb(),\n\t\t\tm_planes(),\n\t\t\tm_ortho_res(OrthographicWidth(1280), OrthographicHeight(720))\n\t\t{\n\t\t}\n\n\t\tvoid SetFov(float deg);\n\t\tvoid SetFovFromFocalLength(float aspect_ratio, float filmSize);\n\t\tvoid SetAspectRatio(float ratio);\n\t\tvoid SetFocalLength(float length);\n\t\tvoid SetFrustumNear(float value) noexcept;\n\t\tvoid SetFrustumFar(float value) noexcept;\n\t\tvoid SetProjectionOffset(float x, float y);\n\n\t\tvoid SetOrthographicResolution(std::uint32_t width, std::uint32_t height);\n\n\t\tstd::pair<float, float> GetProjectionOffset();\n\t\t\n\t\tvoid UpdateTemp(unsigned int frame_idx);\n\t\tbool InView(const std::shared_ptr<MeshNode>& node) const;\n\t\tbool InRange(const std::shared_ptr<MeshNode>& node, const float dist) const;\n\t\tvoid CalculatePlanes();\n\n\t\tbool m_active;\n\n\t\tfloat m_frustum_near;\n\t\tfloat m_frustum_far;\n\t\tfloat m_aspect_ratio;\n\t\tfloat m_focal_length;\n\t\tfloat m_film_size;\n\t\tfloat m_f_number;\n\t\tfloat m_focus_dist;\n\t\tfloat m_shape_amt;\n\t\tfloat m_dof_range;\n\t\tint m_aperture_blades;\n\t\tbool m_enable_dof = false;\n\t\tbool m_override_projection = false;\n\t\tbool m_enable_orthographic = false;\n\n\t\tFoV m_fov;\n\t\tOrthographicResolution m_ortho_res;\n\n\t\tDirectX::XMMATRIX m_view;\n\t\tDirectX::XMMATRIX m_projection;\n\t\tDirectX::XMMATRIX m_prev_projection;\n\t\tDirectX::XMMATRIX m_prev_view;\n\t\tDirectX::XMMATRIX m_view_projection;\n\t\tDirectX::XMMATRIX m_inverse_projection;\n\t\tDirectX::XMMATRIX m_inverse_view;\n\t\tfloat m_projection_offset_x; // Used By Ansel For Super Resolution\n\t\tfloat m_projection_offset_y; // Used By Ansel For Super Resolution\n\n\t\tstd::array<DirectX::XMVECTOR, 6> m_planes;\n\n\t\tConstantBufferHandle* m_camera_cb;\n\n\t\tint m_window_resolution[2] = { 0,0 };\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/scene_graph/light_node.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"light_node.hpp\"\n\nnamespace wr\n{\n\n\tLightNode::LightNode(LightType tid, DirectX::XMVECTOR col) : Node(typeid(LightNode)), m_light(&m_temp)\n\t{\n\t\tSetType(tid);\n\t\tSetColor(col);\n\t}\n\n\tLightNode::LightNode(DirectX::XMVECTOR dir, DirectX::XMVECTOR col) : Node(typeid(LightNode)), m_light(&m_temp)\n\t{\n\t\tSetDirectional(dir, col);\n\t}\n\n\tLightNode::LightNode(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR col) : Node(typeid(LightNode)), m_light(&m_temp)\n\t{\n\t\tSetPoint(pos, rad, col);\n\t}\n\n\tLightNode::LightNode(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR dir, float ang, DirectX::XMVECTOR col) : Node(typeid(LightNode)), m_light(&m_temp)\n\t{\n\t\tSetSpot(pos, rad, dir, ang, col);\n\t}\n\n\tLightNode::LightNode(const LightNode& old) : Node(typeid(LightNode))\n\t{\n\t\tm_temp = old.m_temp;\n\t\tm_temp.tid &= 0x3;\n\t\tm_light = &m_temp;\n\t}\n\n\tLightNode& LightNode::operator=(const LightNode& old)\n\t{\n\t\tm_temp = old.m_temp;\n\t\tm_temp.tid &= 0x3;\n\t\tm_light = &m_temp;\n\t\treturn *this;\n\t}\n\n\tvoid LightNode::SetAngle(float ang)\n\t{\n\t\tm_light->ang = ang;\n\t\tSignalChange();\n\t}\n\n\tvoid LightNode::SetRadius(float rad)\n\t{\n\t\tm_light->rad = rad;\n\t\tSignalChange();\n\t}\n\n\tvoid LightNode::SetType(LightType tid)\n\t{\n\t\tm_light->tid = (uint32_t) tid;\n\t\tSignalChange();\n\t}\n\n\tvoid LightNode::SetColor(DirectX::XMVECTOR col)\n\t{\n\t\tmemcpy(&m_light->col, &col, 12);\n\t\tSignalChange();\n\t}\n\n\tvoid LightNode::SetDirectional(DirectX::XMVECTOR rot, DirectX::XMVECTOR col) \n\t{\n\t\tSetType(LightType::DIRECTIONAL);\n\t\tSetRotation(rot);\n\t\tSetColor(col);\n\t}\n\n\tvoid LightNode::SetPoint(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR col)\n\t{\n\t\tSetType(LightType::POINT);\n\t\tSetPosition(pos);\n\t\tSetRadius(rad);\n\t\tSetColor(col);\n\t}\n\n\tvoid LightNode::SetSpot(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR rot, float ang, DirectX::XMVECTOR col)\n\t{\n\t\tSetType(LightType::SPOT);\n\t\tSetPosition(pos);\n\t\tSetRadius(rad);\n\t\tSetRotation(rot);\n\t\tSetAngle(ang);\n\t\tSetColor(col);\n\t}\n\n\tvoid LightNode::SetLightSize(float size_in_deg)\n\t{\n\t\tm_light->light_size = DirectX::XMConvertToRadians(size_in_deg);\n\t\tSignalChange();\n\t}\n\n\tvoid LightNode::Update(uint32_t frame_idx)\n\t{\n\t\tDirectX::XMVECTOR position = { m_transform.r[3].m128_f32[0], m_transform.r[3].m128_f32[1], m_transform.r[3].m128_f32[2] };\n\t\tmemcpy(&m_light->pos, &position, 12);\n\n\t\tDirectX::XMVECTOR forward = DirectX::XMVector3Normalize(m_transform.r[2]);\n\t\tmemcpy(&m_light->dir, &forward, 12);\n\n\t\tSignalUpdate(frame_idx);\n\t}\n\n\tLightType LightNode::GetType()\n\t{\n\t\treturn (LightType)(m_light->tid & 0x3);\n\t}\n\n}"
  },
  {
    "path": "src/scene_graph/light_node.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"node.hpp\"\n\n#include \"../platform_independend_structs.hpp\"\n\nnamespace wr\n{\n\n\tstruct LightNode : Node\n\t{\n\t\texplicit LightNode(LightType tid, DirectX::XMVECTOR col = { 1, 1, 1 });\n\t\texplicit LightNode(DirectX::XMVECTOR rot, DirectX::XMVECTOR col = { 1, 1, 1 });\n\t\tLightNode(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR col = { 1, 1, 1 });\n\t\tLightNode(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR rot, float ang, DirectX::XMVECTOR col = { 1, 1, 1 });\n\n\t\tLightNode(const LightNode& old);\n\t\tLightNode& operator=(const LightNode& old);\n\n\t\t//! Set angle\n\t\tvoid SetAngle(float ang);\n\n\t\t//! Set radius\n\t\tvoid SetRadius(float rad);\n\n\t\t//! Set type\n\t\tvoid SetType(LightType tid);\n\n\t\t//! Sets color\n\t\tvoid SetColor(DirectX::XMVECTOR col);\n\n\t\t//! Set data for a directional light\n\t\tvoid SetDirectional(DirectX::XMVECTOR rot, DirectX::XMVECTOR col = { 1, 1, 1 });\n\n\t\t//! Set data for a point light\n\t\tvoid SetPoint(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR col = { 1, 1, 1 });\n\n\t\t//! Set data for a spot light\n\t\tvoid SetSpot(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR rot, float ang, DirectX::XMVECTOR col = { 1, 1, 1 });\n\n\t\t//! Set data for light physical size\n\t\tvoid SetLightSize(float size);\n\n\t\t//! Update\n\t\tvoid Update(uint32_t frame_idx);\n\n\t\t//! Helper for getting the LightType (doesn't include light count for the first light)\n\t\tLightType GetType();\n\n\t\t//! Allocated data (either temp or array data)\n\t\tLight* m_light;\n\n\t\t//! Physical data\n\t\tLight m_temp;\n\n\t};\n\n} /* wr */"
  },
  {
    "path": "src/scene_graph/mesh_node.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"mesh_node.hpp\"\n\nnamespace wr {\n\n\tMeshNode::MeshNode(Model* model) : Node(typeid(MeshNode)), m_model(model), m_materials(), m_visible(true)\n\t{\n\t}\n\n\tvoid MeshNode::Update(uint32_t frame_idx)\n\t{\n\t\tm_aabb = AABB::FromTransform(m_model->m_box, m_transform);\n\n\t\tSignalUpdate(frame_idx);\n\t}\n\n\tvoid MeshNode::AddMaterial(MaterialHandle handle)\n\t{\n\t\tm_materials.push_back(handle);\n\n\t\tCheckMaterialCount();\n\t}\n\n\tstd::vector<MaterialHandle>& MeshNode::GetMaterials()\n\t{\n\t\treturn m_materials;\n\t}\n\n\tvoid MeshNode::SetMaterials(std::vector<MaterialHandle> const & materials)\n\t{\n\t\tm_materials = materials;\n\n\t\tCheckMaterialCount();\n\t}\n\n\tvoid MeshNode::ClearMaterials()\n\t{\n\t\tm_materials.clear();\n\t}\n\n\tvoid MeshNode::CheckMaterialCount() const\n\t{\n\t\tif (m_materials.size() > m_model->m_meshes.size())\n\t\t{\n\t\t\tLOGW(\"A mesh node has more materials than meshes.\")\n\t\t}\n\t}\n\n}"
  },
  {
    "path": "src/scene_graph/mesh_node.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"node.hpp\"\n#include \"../model_pool.hpp\"\n#include \"../util/aabb.hpp\"\n\nnamespace wr\n{\n\n\tstruct MeshNode : Node\n\t{\n\t\texplicit MeshNode(Model* model);\n\n\t\tvoid Update(uint32_t frame_idx);\n\t\t/*! Add a material */\n\t\t/*!\n\t\t\tYou can add a material for every single sub-mesh.\n\t\t*/\n\t\tvoid AddMaterial(MaterialHandle handle);\n\t\t/*! Get all materials */\n\t\tstd::vector<MaterialHandle>& GetMaterials();\n\t\t/*! Set the materials */\n\t\tvoid SetMaterials(std::vector<MaterialHandle> const & materials);\n\t\t/*! Remove materials */\n\t\tvoid ClearMaterials();\n\n\t\tModel* m_model;\n\t\tAABB m_aabb;\n\t\tstd::vector<MaterialHandle> m_materials;\n\t\tbool m_visible;\n\n\tprivate:\n\t\t/*! Check whether their are more materials than meshes */\n\t\t/*!\n\t\t\tIf there are more materials than meshes.\n\t\t\tThis function will throw a warning.\n\t\t*/\n\t\tvoid CheckMaterialCount() const;\n\t};\n\n} /* wr */"
  },
  {
    "path": "src/scene_graph/node.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"node.hpp\"\n\n#include \"../util/log.hpp\"\n\nnamespace wr\n{\n\tNode::Node() : m_type_info(typeid(Node))\n\t{\n\t\tSignalTransformChange();\n\t}\n\n\tNode::Node(std::type_info const & type_info) : m_type_info(type_info)\n\t{\n\t\tSignalTransformChange();\n\t}\n\n\tvoid Node::SignalChange()\n\t{\n\t\tm_requires_update[0] = m_requires_update[1] = m_requires_update[2] = true;\n\t}\n\n\tvoid Node::SignalTransformChange()\n\t{\n\t\tm_requires_transform_update[0] = m_requires_transform_update[1] = m_requires_transform_update[2] = true;\n\n\t\tfor (std::shared_ptr<Node>& child : m_children)\n\t\t{\n\t\t\tchild->SignalTransformChange();\n\t\t}\n\t}\n\n\tvoid Node::SignalUpdate(unsigned int frame_idx)\n\t{\n\t\tm_requires_update[frame_idx] = false;\n\t}\n\n\tbool Node::RequiresUpdate(unsigned int frame_idx)\n\t{\n\t\treturn m_requires_update[frame_idx];\n\t}\n\n\tvoid Node::SignalTransformUpdate(unsigned int frame_idx)\n\t{\n\t\tm_requires_transform_update[frame_idx] = false;\n\t}\n\n\tbool Node::RequiresTransformUpdate(unsigned int frame_idx)\n\t{\n\t\treturn m_requires_transform_update[frame_idx];\n\t}\n\n\tvoid Node::SetRotation(DirectX::XMVECTOR roll_pitch_yaw)\n\t{\n\t\tm_rotation_radians = roll_pitch_yaw;\n\t\tm_use_quaternion = false;\n\t\tSignalTransformChange();\n\t}\n\n\tvoid Node::SetRotationQuaternion(DirectX::XMVECTOR rotation)\n\t{\n\t\tm_rotation = rotation;\n\t\tm_use_quaternion = true;\n\t\tSignalTransformChange();\n\t}\n\n\tvoid Node::SetQuaternionRotation( float x, float y, float z, float w )\n\t{\n\t\tm_rotation = { x,y,z,w };\n\t\tm_use_quaternion = true;\n\t\tSignalTransformChange();\n\t}\n\n\tvoid Node::SetPosition(DirectX::XMVECTOR position)\n\t{\n\t\tm_position = position;\n\t\tSignalTransformChange();\n\t}\n\n\tvoid Node::SetScale(DirectX::XMVECTOR scale)\n\t{\n\t\tm_scale = scale;\n\t\tSignalTransformChange();\n\t}\n\n\tvoid Node::SetTransform(DirectX::XMVECTOR position, DirectX::XMVECTOR rotation, DirectX::XMVECTOR scale)\n\t{\n\t\tSetPosition(position);\n\t\tSetRotation(rotation);\n\t\tSetScale(scale);\n\t}\n\n\tvoid Node::UpdateTransform()\n\t{\n\t\tif (!m_use_quaternion)\n\t\t{\n\t\t\tm_rotation = DirectX::XMQuaternionRotationRollPitchYawFromVector(m_rotation_radians);\n\t\t}\n\t\tm_prev_transform = m_transform;\n\n\t\tDirectX::XMMATRIX translation_mat = DirectX::XMMatrixTranslationFromVector(m_position);\n\t\tDirectX::XMMATRIX rotation_mat = DirectX::XMMatrixRotationQuaternion(m_rotation);\n\t\tDirectX::XMMATRIX scale_mat = DirectX::XMMatrixScalingFromVector(m_scale);\n\t\tm_transform = m_local_transform = scale_mat * rotation_mat * translation_mat;\n\n\t\tif (m_parent)\n\t\t\tm_transform *= m_parent->m_transform;\n\n\t\tSignalChange();\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/scene_graph/node.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <bitset>\n#include <functional>\n#include <memory>\n#include <DirectXMath.h>\n\nnamespace wr\n{\n\tstruct Node : std::enable_shared_from_this<Node>\n\t{\n\t\tNode();\n\t\texplicit Node(std::type_info const & type_info);\n\n\t\tvoid SignalChange();\n\t\tvoid SignalUpdate(unsigned int frame_idx);\n\t\tbool RequiresUpdate(unsigned int frame_idx);\n\n\t\tvoid SignalTransformChange();\n\t\tvoid SignalTransformUpdate(unsigned int frame_idx);\n\t\tbool RequiresTransformUpdate(unsigned int frame_idx);\n\n\t\t//Takes roll, pitch and yaw and converts it to quaternion\n\t\tvirtual void SetRotation(DirectX::XMVECTOR roll_pitch_yaw);\n\t\tvirtual void SetRotationQuaternion(DirectX::XMVECTOR rotation);\n\n\t\t//Takes raw values of a quaternion\n\t\tvirtual void SetQuaternionRotation( float x, float y, float z, float w );\n\n\t\t//Sets position\n\t\tvirtual void SetPosition(DirectX::XMVECTOR position);\n\n\t\t//Sets scale\n\t\tvirtual void SetScale(DirectX::XMVECTOR scale);\n\n\t\t//Position, rotation (roll, pitch, yaw) and scale\n\t\tvirtual void SetTransform(DirectX::XMVECTOR position, DirectX::XMVECTOR rotation, DirectX::XMVECTOR scale);\n\n\t\t//Update the transform; done automatically when SignalChange is called\n\t\tvoid UpdateTransform();\n\n\t\tstd::shared_ptr<Node> m_parent;\n\t\tstd::vector<std::shared_ptr<Node>> m_children;\n\n\t\t//Translation of mesh node\n\t\tDirectX::XMVECTOR m_position = { 0, 0, 0, 1 };\n\n\t\t//Rotation as quaternion\n\t\tDirectX::XMVECTOR m_rotation;\n\n\t\t//Rotation in radians\n\t\tDirectX::XMVECTOR m_rotation_radians = { 0,0,0 };\n\n\t\t//Scale\n\t\tDirectX::XMVECTOR m_scale = { 1, 1, 1, 0 };\n\n\t\t//Transformation\n\t\tDirectX::XMMATRIX m_local_transform, m_transform, m_prev_transform;\n\n\t\tconst std::type_info& m_type_info;\n\n\tprotected:\n\t\tbool m_use_quaternion = false;\n\n\tprivate:\n\t\tstd::bitset<3> m_requires_update;\n\t\tstd::bitset<3> m_requires_transform_update;\n\t};\n} // namespace wr\n"
  },
  {
    "path": "src/scene_graph/scene_graph.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"scene_graph.hpp\"\n\n#include <algorithm>\n\n#include \"../renderer.hpp\"\n#include \"../util/log.hpp\"\n\n#include \"camera_node.hpp\"\n#include \"mesh_node.hpp\"\n#include \"skybox_node.hpp\"\n#include \"light_node.hpp\"\n\n//TODO: Make platform independent\n#include \"../d3d12/d3d12_defines.hpp\"\n#include \"../d3d12/d3d12_functions.hpp\"\n#include \"../d3d12/d3d12_renderer.hpp\"\n#include \"../d3d12/d3d12_constant_buffer_pool.hpp\"\n\nnamespace wr\n{\n\n\tSceneGraph::SceneGraph(RenderSystem* render_system) :\n\t    m_render_system(render_system),\n\t\tm_root(std::make_shared<Node>()),\n\t\tm_light_buffer()\n\t{\n\t\tm_lights.resize(d3d12::settings::num_lights);\n\n\t\tm_default_skybox = std::make_shared<wr::SkyboxNode>(render_system->m_default_cubemap);\n\t\tm_default_skybox->m_skybox = m_default_skybox->m_prefiltered_env_map = m_default_skybox->m_irradiance = render_system->m_default_cubemap;\n\t}\n\n\tSceneGraph::~SceneGraph()\n\t{\n\t\tRemoveChildren(GetRootNode());\n\t}\n\n\t//! Used to obtain the root node.\n\tstd::shared_ptr<Node> SceneGraph::GetRootNode() const\n\t{\n\t\treturn m_root;\n\t}\n\n\t//! Used to obtain the children of a node.\n\tstd::vector<std::shared_ptr<Node>> SceneGraph::GetChildren(std::shared_ptr<Node> const & parent)\n\t{\n\t\treturn parent ? parent->m_children : m_root->m_children;\n\t}\n\n\t//! Used to remove the children of a node.\n\tvoid SceneGraph::RemoveChildren(std::shared_ptr<Node> const & parent)\n\t{\n\t\tparent->m_children.clear();\n\t}\n\n\t//! Returns the active camera.\n\t/*!\n\t\tIf there are multiple active cameras it will return the first one.\n\t*/\n\tstd::shared_ptr<CameraNode> SceneGraph::GetActiveCamera()\n\t{\n\t\tfor (auto& camera_node : m_camera_nodes)\n\t\t{\n\t\t\tif (camera_node->m_active)\n\t\t\t{\n\t\t\t\treturn camera_node;\n\t\t\t}\n\t\t}\n\n\t\tLOGW(\"Failed to obtain a active camera node.\");\n\t\treturn nullptr;\n\t}\n\n\tstd::vector<std::shared_ptr<LightNode>>& SceneGraph::GetLightNodes()\n\t{\n\t\treturn m_light_nodes;\n\t}\n\n\tstd::vector<std::shared_ptr<MeshNode>>& SceneGraph::GetMeshNodes()\n\t{\n\t\treturn m_mesh_nodes;\n\t}\n\n\tstd::shared_ptr<SkyboxNode> SceneGraph::GetCurrentSkybox()\n\t{\n\t\tif (m_current_skybox)\n\t\t{\n\t\t\treturn m_current_skybox;\n\t\t}\n\t\t\n\t\treturn m_default_skybox;\n\t}\n\n\tvoid SceneGraph::UpdateSkyboxNode(std::shared_ptr<SkyboxNode> node, TextureHandle new_equirectangular)\n\t{\n\t\tnode->UpdateSkybox(new_equirectangular, m_render_system->GetFrameIdx());\n\n\t\tm_render_system->RequestSkyboxReload();\n\t}\n\n\t//! Initialize the scene graph\n\tvoid SceneGraph::Init()\n\t{\n\t\tm_init_meshes_func_impl(m_render_system, m_mesh_nodes);\n\n\t\t// Create constant buffer pool\n\n\t\tconstexpr auto model_size = sizeof(temp::ObjectData) * d3d12::settings::num_instances_per_batch;\n\t\tconstexpr auto model_cbs_size = SizeAlignTwoPower(model_size, 256) * d3d12::settings::num_back_buffers;\n\n\t\tm_constant_buffer_pool = m_render_system->CreateConstantBufferPool((uint32_t) model_cbs_size*1024);\n\n\t\t// Initialize cameras\n\n\t\tm_init_cameras_func_impl(m_render_system, m_camera_nodes);\n\n\t\t// Create Light Buffer\n\n\t\tstd::uint64_t light_count = (std::uint64_t) m_lights.size();\n        std::uint64_t light_buffer_stride = sizeof(Light), light_buffer_size = light_buffer_stride * light_count;\n        std::uint64_t light_buffer_aligned_size = SizeAlignTwoPower(light_buffer_size, 65536) * d3d12::settings::num_back_buffers;\n\n\t\tm_structured_buffer = m_render_system->CreateStructuredBufferPool((size_t)light_buffer_aligned_size );\n\t\tm_light_buffer = m_structured_buffer->Create(light_buffer_size, light_buffer_stride, false);\n\n\t\t//Initialize lights\n\n\t\tm_init_lights_func_impl(m_render_system, m_light_nodes, m_lights);\n\n\t}\n\n\t//! Update the scene graph\n\tvoid SceneGraph::Update()\n\t{\n\t\tm_update_transforms_func_impl(m_render_system, *this, m_root);\n\t\tm_update_cameras_func_impl(m_render_system, m_camera_nodes);\n\t\tm_update_meshes_func_impl(m_render_system, m_mesh_nodes);\n\t\tm_update_lights_func_impl(m_render_system, *this);\n\t}\n\n\t//! Render the scene graph\n\t/*!\n\t\tThe user is expected to call `Optimize`. If they don't this function will do it manually.\n\t*/\n\tvoid SceneGraph::Render(CommandList* cmd_list, CameraNode* camera)\n\t{\n\t\tm_render_meshes_func_impl(m_render_system, m_batches, camera, cmd_list);\n\t}\n\n\ttemp::MeshBatches& SceneGraph::GetBatches()\n\t{ \n\t\treturn m_batches;\n\t}\n\n\tstd::unordered_map<temp::BatchKey, std::vector<temp::ObjectData>, util::PairHash>& SceneGraph::GetGlobalBatches()\n\t{\n\t\treturn m_objects;\n\t}\n\n\tStructuredBufferHandle* SceneGraph::GetLightBuffer()\n\t{\n\t\treturn m_light_buffer;\n\t}\n\n\tuint32_t SceneGraph::GetCurrentLightSize()\n\t{\n\t\treturn m_next_light_id;\n\t}\n\n\tfloat SceneGraph::GetRTCullingDistance()\n\t{\n\t\treturn std::fabs(m_rt_culling_distance);\n\t}\n\n\tbool SceneGraph::GetRTCullingEnabled()\n\t{\n\t\treturn m_rt_culling_distance > 0;\n\t}\n\n\tvoid SceneGraph::SetRTCullingDistance(float dist)\n\t{\n\t\tm_rt_culling_distance = dist * (GetRTCullingEnabled() * 2 - 1);\n\t}\n\n\tvoid SceneGraph::SetRTCullingEnable(bool b)\n\t{\n\t\tm_rt_culling_distance = GetRTCullingDistance() * (b * 2 - 1);\n\t}\n\n\tLight* SceneGraph::GetLight(uint32_t offset)\n\t{\n\t\treturn offset >= m_next_light_id ? m_lights.data() : m_lights.data() + offset;\n\t}\n\n\tvoid SceneGraph::RegisterLight(std::shared_ptr<LightNode>& new_node)\n\t{\n\t\t//Allocate a light into the array\n\n\t\tif (m_next_light_id == (uint32_t)m_lights.size())\n\t\t\tLOGE(\"Couldn't allocate light node; out of memory\");\n\n\t\tnew_node->m_light = m_lights.data() + m_next_light_id;\n\t\tmemcpy(new_node->m_light, &new_node->m_temp, sizeof(new_node->m_temp));\n\t\t++m_next_light_id;\n\n\t\t//Update light count\n\n\t\tif (!m_lights.empty())\n\t\t{\n\t\t\tm_lights[0].tid &= 0x3;\t\t\t\t\t\t\t\t\t\t\t//Keep id\n\t\t\tm_lights[0].tid |= uint32_t(m_light_nodes.size() + 1) << 2;\t\t//Set lights\n\n\t\t\tif (!m_light_nodes.empty())\n\t\t\t{\n\t\t\t\tm_light_nodes[0]->SignalChange();\n\t\t\t}\n\t\t}\n\n\t\t//Track the node\n\n\t\tm_light_nodes.push_back(new_node);\n\t}\n\n\tvoid SceneGraph::Optimize() \n\t{\n\t\t//Update batches\n\n\t\tbool should_update = m_batches.empty();\n\n\t\tfor (auto& elem : m_batches)\n\t\t{\n\t\t\tif (elem.second.num_instances == 0)\n\t\t\t{\n\t\t\t\tshould_update = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (should_update)\n\t\t{\n\t\t\tconstexpr uint32_t max_size = d3d12::settings::num_instances_per_batch;\n\t\t\tconstexpr auto model_size = sizeof(temp::ObjectData) * max_size;\n\n\t\t\tfor (auto& node : m_mesh_nodes)\n\t\t\t{\n\n\t\t\t\tauto mesh_materials_pair = std::make_pair(node->m_model, node->m_materials);\n\n\t\t\t\tauto it = m_batches.find(mesh_materials_pair);\n\n\t\t\t\t//It won't keep track of anything if it has no model\n\t\t\t\tif (node->m_model == nullptr)\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tauto obj = m_objects.find(mesh_materials_pair);\n\n\t\t\t\t//Insert new if doesn't exist\n\t\t\t\tif (it == m_batches.end())\n\t\t\t\t{\n\t\t\t\t\tConstantBufferHandle* object_buffer = m_constant_buffer_pool->Create(model_size);\n\n\t\t\t\t\tauto& batch = m_batches[mesh_materials_pair];\n\t\t\t\t\tbatch.batch_buffer = object_buffer;\n\t\t\t\t\tbatch.m_materials = node->GetMaterials();\n\t\t\t\t\tbatch.data.objects.resize(d3d12::settings::num_instances_per_batch);\n\t\t\t\t\t\n\t\t\t\t\tif (obj == m_objects.end()) {\n\t\t\t\t\t\tm_objects[mesh_materials_pair] = std::vector<temp::ObjectData>(d3d12::settings::num_instances_per_batch);\n\t\t\t\t\t\tobj = m_objects.find(mesh_materials_pair);\n\t\t\t\t\t}\n\n\t\t\t\t\tit = m_batches.find(mesh_materials_pair);\n\t\t\t\t}\n\n\t\t\t\t//Mark batch as \"active\" and keep track of instances\n\t\t\t\t++it->second.num_total_instances;\n\n\t\t\t\t//Model should remain loaded, but not rendered\n\t\t\t\tif (!node->m_visible)\n\t\t\t\t{\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\ttemp::MeshBatch& batch = it->second;\n\t\t\t\tbatch.m_materials = node->GetMaterials();\n\n\t\t\t\t//Cull for rasterizer\n\t\t\t\tif (!d3d12::settings::enable_object_culling || GetActiveCamera()->InView(node))\n\t\t\t\t{\n\t\t\t\t\tunsigned int& offset = batch.num_instances;\n\t\t\t\t\tbatch.data.objects[offset] = { node->m_transform, node->m_prev_transform };\n\t\t\t\t\t++offset;\n\t\t\t\t}\n\n\t\t\t\t//Cull for raytracer\n\t\t\t\tif (!GetRTCullingEnabled() || GetActiveCamera()->InRange(node, GetRTCullingDistance()))\n\t\t\t\t{\n\t\t\t\t\tunsigned int& globalOffset = batch.num_global_instances;\n\t\t\t\t\tobj->second[globalOffset] = { node->m_transform, node->m_prev_transform };\n\t\t\t\t\t++globalOffset;\n\t\t\t\t}\n\n\t\t\t}\n\n\t\t\tstd::queue<wr::temp::BatchKey> m_to_remove;\n\n\t\t\tfor (auto& elem : m_batches)\n\t\t\t{\n\t\t\t\t//Release empty batches\n\t\t\t\tif (elem.second.num_total_instances == 0)\n\t\t\t\t{\n\t\t\t\t\tm_to_remove.push(elem.first);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\t//Update object data\n\t\t\t\ttemp::MeshBatch& batch = elem.second;\n\t\t\t\tm_constant_buffer_pool->Update(batch.batch_buffer, sizeof(temp::ObjectData) * elem.second.num_instances, 0, (uint8_t*)batch.data.objects.data());\n\t\t\t\telem.second.num_total_instances = 0;\t//Clear for future use\n\t\t\t}\n\n\t\t\twhile(!m_to_remove.empty())\n\t\t\t{\n\t\t\t\twr::temp::BatchKey &key = m_to_remove.front();\n\n\t\t\t\tif (m_batches[key].batch_buffer)\n\t\t\t\t{\n\t\t\t\t\tm_constant_buffer_pool->Destroy(m_batches[key].batch_buffer);\n\t\t\t\t}\n\n\t\t\t\tm_objects.erase(key);\n\t\t\t\tm_batches.erase(key);\n\t\t\t\tm_to_remove.pop();\n\t\t\t}\n\n\t\t}\n\t\t\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/scene_graph/scene_graph.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <bitset>\n#include <functional>\n#include <memory>\n#include <DirectXMath.h>\n#include <cstdint>\n\n#include \"node.hpp\"\n#include \"light_node.hpp\"\n#include \"../platform_independend_structs.hpp\"\n#include \"../util/user_literals.hpp\"\n#include \"../util/defines.hpp\"\n#include \"../util/log.hpp\"\n#include \"../model_pool.hpp\"\n#include \"../constant_buffer_pool.hpp\"\n#include \"../structured_buffer_pool.hpp\"\n#include \"../model_pool.hpp\"\n#include \"../util/delegate.hpp\"\n#include \"../util/pair_hash.hpp\"\n\nnamespace wr\n{\n\tclass RenderSystem;\n\tstruct CameraNode;\n\tusing CommandList = void;\n\n\tstruct CameraNode;\n\tstruct MeshNode;\n\tstruct SkyboxNode;\n\n\tnamespace temp {\n\n\t\tstruct ObjectData {\n\t\t\tDirectX::XMMATRIX m_model;\n\t\t\tDirectX::XMMATRIX m_prev_model;\n\t\t};\n\n\t\tstruct MeshBatch_CBData\n\t\t{\n\t\t\tstd::vector<ObjectData> objects;\n\t\t};\n\n\t\tstruct MeshBatch\n\t\t{\n\t\t\tunsigned int num_instances = 0, num_global_instances = 0, num_total_instances = 0;\n\t\t\tConstantBufferHandle* batch_buffer;\n\t\t\tMeshBatch_CBData data;\n\t\t\tstd::vector<MaterialHandle> m_materials;\n\t\t};\n\n\t\tusing BatchKey = std::pair<Model*, std::vector<MaterialHandle>>;\n\t\tusing MeshBatches = std::unordered_map<BatchKey, MeshBatch, util::PairHash>;\n\n\t}\n\n\tclass SceneGraph\n\t{\n\tpublic:\n\t\texplicit SceneGraph(RenderSystem* render_system);\n\t\t~SceneGraph();\n\n\t\t// Impl Functions\n\t\tstatic util::Delegate<void(RenderSystem*, temp::MeshBatches&, CameraNode* camera, CommandList*)> m_render_meshes_func_impl;\n\t\tstatic util::Delegate<void(RenderSystem*, std::vector<std::shared_ptr<MeshNode>>&)> m_init_meshes_func_impl;\n\t\tstatic util::Delegate<void(RenderSystem*, std::vector<std::shared_ptr<CameraNode>>&)> m_init_cameras_func_impl;\n\t\tstatic util::Delegate<void(RenderSystem*, std::vector<std::shared_ptr<LightNode>>&, std::vector<Light>&)> m_init_lights_func_impl;\n\t\tstatic util::Delegate<void(RenderSystem*, std::vector<std::shared_ptr<MeshNode>>&)> m_update_meshes_func_impl;\n\t\tstatic util::Delegate<void(RenderSystem*, std::vector<std::shared_ptr<CameraNode>>&)> m_update_cameras_func_impl;\n\t\tstatic util::Delegate<void(RenderSystem* render_system, SceneGraph& scene_graph)> m_update_lights_func_impl;\n\t\tstatic util::Delegate<void(RenderSystem* render_system, SceneGraph& scene_graph, std::shared_ptr<Node>&)> m_update_transforms_func_impl;\n\t\tstatic util::Delegate<void(RenderSystem* render_system, SceneGraph& scene_graph, std::shared_ptr<SkyboxNode>&)> m_delete_skybox_func_impl;\n\n\t\tSceneGraph(SceneGraph&&) = delete;\n\t\tSceneGraph(SceneGraph const &) = delete;\n\t\tSceneGraph& operator=(SceneGraph&&) = delete;\n\t\tSceneGraph& operator=(SceneGraph const &) = delete;\n\n\t\tstd::shared_ptr<Node> GetRootNode() const;\n\t\ttemplate<typename T, typename... Args>\n\t\tstd::shared_ptr<T> CreateChild(std::shared_ptr<Node> const & parent = nullptr, Args... args);\n\t\tstd::vector<std::shared_ptr<Node>> GetChildren(std::shared_ptr<Node> const & parent = nullptr);\n\t\tstatic void RemoveChildren(std::shared_ptr<Node> const & parent);\n\t\tstd::shared_ptr<CameraNode> GetActiveCamera();\n\n\t\tstd::vector<std::shared_ptr<LightNode>>& GetLightNodes();\n\t\tstd::vector<std::shared_ptr<MeshNode>>& GetMeshNodes();\n\t\tstd::shared_ptr<SkyboxNode> GetCurrentSkybox();\n\n\t\tvoid UpdateSkyboxNode(std::shared_ptr<SkyboxNode> node, TextureHandle new_equirectangular);\n\n\t\tvoid Init();\n\t\tvoid Update();\n\t\tvoid Render(CommandList* cmd_list, CameraNode* camera);\n\n\t\ttemplate<typename T>\n\t\tvoid DestroyNode(std::shared_ptr<T> node);\n\n\t\tvoid Optimize();\n\t\ttemp::MeshBatches& GetBatches();\n\t\tstd::unordered_map<temp::BatchKey, std::vector<temp::ObjectData>, util::PairHash>& GetGlobalBatches();\n\n\t\tStructuredBufferHandle* GetLightBuffer();\n\t\tLight* GetLight(uint32_t offset);\t\t\t//Returns nullptr when out of bounds\n\n\t\tuint32_t GetCurrentLightSize();\n\t\tfloat GetRTCullingDistance();\n\t\tbool GetRTCullingEnabled();\n\n\t\tvoid SetRTCullingDistance(float dist);\n\t\tvoid SetRTCullingEnable(bool b);\n\n\tprotected:\n\n\t\tvoid RegisterLight(std::shared_ptr<LightNode>& light_node);\n\n\tprivate:\n\n\t\tRenderSystem* m_render_system;\n\t\t//! The root node of the hiararchical tree.\n\t\tstd::shared_ptr<Node> m_root;\n\n\t\ttemp::MeshBatches m_batches;\n\t\tstd::unordered_map<temp::BatchKey, std::vector<temp::ObjectData>, util::PairHash> m_objects;\n\n\t\tstd::vector<Light> m_lights;\n\n\t\tstd::shared_ptr<StructuredBufferPool> m_structured_buffer;\n\t\tstd::shared_ptr<ConstantBufferPool> m_constant_buffer_pool;\n\n\t\tStructuredBufferHandle* m_light_buffer;\n\n\t\tstd::vector<std::shared_ptr<CameraNode>> m_camera_nodes;\n\t\tstd::vector<std::shared_ptr<MeshNode>> m_mesh_nodes;\n\t\tstd::vector<std::shared_ptr<LightNode>> m_light_nodes;\n\t\tstd::vector< std::shared_ptr<SkyboxNode>> m_skybox_nodes;\n\n\t\tstd::shared_ptr<SkyboxNode> m_default_skybox = nullptr;\n\n\t\tstd::shared_ptr<SkyboxNode> m_current_skybox = nullptr;\n\n\t\tuint32_t m_next_light_id = 0;\n\t\tfloat m_rt_culling_distance = -1;\n\t};\n\n\t//! Creates a child into the scene graph\n\t/*\n\t  If the parent is a nullptr the child will be created on the root node.\n\t*/\n\ttemplate<typename T, typename... Args>\n\tstd::shared_ptr<T> SceneGraph::CreateChild(std::shared_ptr<Node> const & parent, Args... args)\n\t{\n\t\tauto p = parent ? parent : m_root;\n\n\t\tauto new_node = std::make_shared<T>(args...);\n\t\tp->m_children.push_back(new_node);\n\t\tnew_node->m_parent = p;\n\n\t\tif constexpr (std::is_base_of<CameraNode, T>::value)\n\t\t{\n\t\t\tm_camera_nodes.push_back(new_node);\n\t\t}\n\t\telse if constexpr (std::is_base_of<MeshNode, T>::value)\n\t\t{\n\t\t\tm_mesh_nodes.push_back(new_node);\n\t\t}\n\t\telse if constexpr (std::is_base_of<LightNode, T>::value)\n\t\t{\n\t\t\tRegisterLight(new_node);\n\t\t}\n\t\telse if constexpr (std::is_same<T, SkyboxNode>::value)\n\t\t{\n\t\t\tm_skybox_nodes.push_back(new_node);\n\n\t\t\t//This matches Maya's behaviour of always showing the latest skybox created.\n\t\t\tm_current_skybox = new_node;\n\t\t}\n\n\t\treturn new_node;\n\t}\n\n\ttemplate<typename T>\n\tvoid SceneGraph::DestroyNode(std::shared_ptr<T> node) \n\t{\n\t\tif constexpr (std::is_base_of<CameraNode, T>::value)\n\t\t{\n\t\t\tfor (size_t i = 0, j = m_camera_nodes.size(); i < j; ++i)\n\t\t\t{\n\t\t\t\tif (m_camera_nodes[i] == node)\n\t\t\t\t{\n\t\t\t\t\tm_camera_nodes.erase(m_camera_nodes.begin() + i);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if constexpr (std::is_base_of<MeshNode, T>::value)\n\t\t{\n\t\t\tfor (size_t i = 0, j = m_mesh_nodes.size(); i < j; ++i)\n\t\t\t{\n\t\t\t\tif (m_mesh_nodes[i] == node)\n\t\t\t\t{\n\t\t\t\t\tm_mesh_nodes.erase(m_mesh_nodes.begin() + i);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if constexpr (std::is_base_of<LightNode, T>::value)\n\t\t{\n\t\t\tfor (size_t i = 0, j = m_light_nodes.size(); i < j; ++i)\n\t\t\t{\n\t\t\t\tif (m_light_nodes[i] == node)\n\t\t\t\t{\n\t\t\t\t\t//Move everything after this light back one light, so the memory is one filled array\n\n\t\t\t\t\tfor (size_t k = i + 1; k < j; ++k)\n\t\t\t\t\t{\n\t\t\t\t\t\tmemcpy(m_light_nodes[k - 1]->m_light, m_light_nodes[k]->m_light, sizeof(Light));\n\t\t\t\t\t\t--m_light_nodes[k]->m_light;\n\t\t\t\t\t\tm_light_nodes[k]->SignalChange();\n\t\t\t\t\t}\n\n\t\t\t\t\t//Update light count\n\n\t\t\t\t\tif (m_lights.size() != 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tm_lights[0].tid &= 0x3;\t\t\t\t\t\t\t\t\t\t\t//Keep id\n\t\t\t\t\t\tm_lights[0].tid |= uint32_t(m_light_nodes.size() - 1) << 2;\t\t//Set lights\n\t\t\t\t\t}\n\n\t\t\t\t\t//Stop tracking the node\n\n\t\t\t\t\tm_light_nodes.erase(m_light_nodes.begin() + i);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse if constexpr (std::is_base_of<SkyboxNode, T>::value)\n\t\t{\n\t\t\tfor (size_t i = 0, j = m_skybox_nodes.size(); i < j; ++i)\n\t\t\t{\n\t\t\t\tif (m_skybox_nodes[i] == node)\n\t\t\t\t{\n\t\t\t\t\tif (m_current_skybox == m_skybox_nodes[i])\n\t\t\t\t\t{\n\t\t\t\t\t\tsize_t num_nodes = m_skybox_nodes.size();\n\n\t\t\t\t\t\tif (num_nodes > 1)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tm_current_skybox = m_skybox_nodes[num_nodes - 1];\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tm_current_skybox = nullptr;\n\t\t\t\t\t\t\tLOGW(\"[WARNING]: Last skybox node deleted, m_current_skybox is now a nullptr\")\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tm_delete_skybox_func_impl(m_render_system, *this, node);\n\n\t\t\t\t\tm_skybox_nodes.erase(m_skybox_nodes.begin() + i);\n\t\t\t\t\t\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tm_next_light_id = (uint32_t) m_light_nodes.size();\n\n\t\tnode->m_parent->m_children.erase(std::remove(node->m_parent->m_children.begin(), node->m_parent->m_children.end(), node), node->m_parent->m_children.end());\n\n\t\tnode.reset();\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/scene_graph/skybox_node.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"skybox_node.hpp\"\n\nnamespace wr\n{\n\tvoid SkyboxNode::UpdateSkybox(TextureHandle new_equirectangular, unsigned int frame_idx)\n\t{\n\t\tm_irradiance.value().m_pool->MarkForUnload(m_irradiance.value(), frame_idx);\n\t\tm_irradiance = std::nullopt;\n\n\t\tm_skybox.value().m_pool->MarkForUnload(m_skybox.value(), frame_idx);\n\t\tm_skybox = std::nullopt;\n\n\t\tm_prefiltered_env_map.value().m_pool->MarkForUnload(m_prefiltered_env_map.value(), frame_idx);\n\t\tm_prefiltered_env_map = std::nullopt;\n\n\t\tif (m_hdr.m_pool)\n\t\t{\n\t\t\tm_hdr.m_pool->MarkForUnload(m_hdr, frame_idx);\n\n#ifdef _DEBUG\n\t\t\t//Decide if we want to break when developing in case we enter this function\n\t\t\t//as in theory m_hdr needed to be cleaned from the render task, if that's not the case something went wrong.\n\t\t\tLOGC(\"[ERROR]: M_HDR is supposed to be an invalid handle at this stage. If that's not the case, the next line of code could leak memory\");\n#endif // DEBUG\n\t\t}\n\n\t\tm_hdr = new_equirectangular;\n\t}\n} /* wr */\n"
  },
  {
    "path": "src/scene_graph/skybox_node.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include \"scene_graph.hpp\"\n\nnamespace wr {\n\n\tstruct SkyboxNode : Node\n\t{\n\t\texplicit SkyboxNode(wr::TextureHandle hdr_texture, std::optional<wr::TextureHandle> cubemap = std::nullopt, std::optional<wr::TextureHandle> irradiance = std::nullopt)\n\t\t\t: Node::Node(typeid(SkyboxNode))\n\t\t\t, m_hdr(hdr_texture) \n\t\t\t, m_skybox(cubemap)\n\t\t\t, m_irradiance(irradiance)\n\t\t{}\n\n\t\tvoid UpdateSkybox(TextureHandle new_equirectangular, unsigned int frame_idx);\n\n\t\twr::TextureHandle m_hdr;\n\t\tstd::optional<wr::TextureHandle> m_skybox;\n\t\tstd::optional<wr::TextureHandle> m_irradiance;\n\t\tstd::optional<wr::TextureHandle> m_prefiltered_env_map;\n\t};\n\n};// namespace wr "
  },
  {
    "path": "src/settings.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\nnamespace wr::settings\n{\n\n\tstatic const constexpr bool use_multithreading = true;\n\tstatic const constexpr unsigned int num_frame_graph_threads = 4;\n\n\tstatic const constexpr std::uint8_t default_textures_count = 5;\n\tstatic const constexpr std::uint32_t default_textures_size_in_bytes = 4ul * 1024ul * 1024ul;\n\tstatic constexpr const char* default_albedo_path = \"resources/materials/metalgrid2_basecolor.png\";\n\tstatic constexpr const char* default_normal_path = \"resources/materials/flat_normal.png\";\n\tstatic constexpr const char* default_white_texture = \"resources/materials/white.png\";\n\tstatic constexpr const char* default_black_texture = \"resources/materials/black.png\";\n\n} /* wr::settings */\n"
  },
  {
    "path": "src/shader_registry.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"shader_registry.hpp\"\n\nnamespace wr\n{\n\n\tShaderRegistry::ShaderRegistry() : Registry<ShaderRegistry, Shader, ShaderDescription>()\n\t{\n\t}\n\n\tRegistryHandle ShaderRegistry::Register(ShaderDescription description)\n\t{\n\t\tauto handle = m_next_handle;\n\n\t\tm_descriptions.insert({ handle, description });\n\n\t\tm_next_handle++;\n\t\treturn handle;\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/shader_registry.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"registry.hpp\"\n#include \"d3d12/d3d12_enums.hpp\"\n#include \"util/named_type.hpp\"\n\nnamespace wr\n{\n\tusing Shader = void;\n\n\tstruct ShaderDescription\n\t{\n\t\tstd::string path;\n\t\tstd::string entry;\n\t\tShaderType type;\n\t\tstd::vector<std::pair<std::wstring, std::wstring>> defines;\n\t};\n\n\tclass ShaderRegistry : public internal::Registry<ShaderRegistry, Shader, ShaderDescription>\n\t{\n\tpublic:\n\t\tShaderRegistry();\n\t\tvirtual ~ShaderRegistry() = default;\n\n\t\tRegistryHandle Register(ShaderDescription description);\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/structs.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include <stdint.h>\n#include <cstdint>\n\nnamespace wr\n{\n\n\tstruct Texture { };\n\n\tstruct ReadbackBuffer { };\n\n\tclass TexturePool;\n\n\tstruct TextureHandle\n\t{\n\t\tTexturePool* m_pool = nullptr;\n\t\tstd::uint32_t m_id = 0;\n\t};\n\n\tstruct MaterialUVScales\n\t{\n\t\tfloat m_albedo_scale = 1.0f;\n\t\tfloat m_normal_scale = 1.0f;\n\t\tfloat m_roughness_scale = 1.0f;\n\t\tfloat m_metallic_scale = 1.0f;\n\t\tfloat m_emissive_scale = 1.0f;\n\t\tfloat m_ao_scale = 1.0f;\n\t};\n\n\tstruct CPUTexture\n\t{\n\t\tfloat* m_data = nullptr;\n\t\tunsigned int m_buffer_width;\n\t\tunsigned int m_buffer_height;\n\t\tunsigned int m_bytes_per_pixel;\n\t};\n\n\tclass MaterialPool;\n\n\tstruct MaterialHandle\n\t{\n\t\tMaterialPool* m_pool;\n\t\tstd::uint32_t m_id;\n\n\t\tfriend bool operator ==(MaterialHandle const & lhs, MaterialHandle const & rhs)\n\t\t{\n\t\t\treturn lhs.m_pool == rhs.m_pool && lhs.m_id == rhs.m_id;\n\t\t}\n\n\t\tfriend bool operator!=(MaterialHandle const& lhs, MaterialHandle const& rhs)\n\t\t{\n\t\t\treturn lhs.m_pool != rhs.m_pool || lhs.m_id != rhs.m_id;\n\t\t}\n\t};\n\n}"
  },
  {
    "path": "src/structured_buffer_pool.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"structured_buffer_pool.hpp\"\n\nnamespace wr\n{\n\tStructuredBufferPool::StructuredBufferPool(std::size_t size_in_bytes) :\n\t\tm_size_in_bytes(size_in_bytes)\n\t{\n\t}\n\n\tStructuredBufferHandle * StructuredBufferPool::Create(std::size_t size, std::size_t stride, bool used_as_uav)\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_mutex);\n\n\t\treturn CreateBuffer(size, stride, used_as_uav);\n\t}\n\n\tvoid StructuredBufferPool::Destroy(StructuredBufferHandle * handle)\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_mutex);\n\n\t\tDestroyBuffer(handle);\n\t}\n\n\tvoid StructuredBufferPool::Update(StructuredBufferHandle * handle, void * data, std::size_t size, std::size_t offset)\n\t{\n\t\tstd::lock_guard<std::mutex> lock(m_mutex);\n\n\t\tUpdateBuffer(handle, data, size, offset);\n\t}\n\n} /* wr */"
  },
  {
    "path": "src/structured_buffer_pool.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <vector>\n#include <mutex>\n\nnamespace wr\n{\n\tclass StructuredBufferPool;\n\n\tstruct StructuredBufferHandle\n\t{\n\t\tStructuredBufferPool* m_pool;\n\t};\n\n\tclass StructuredBufferPool\n\t{\n\tpublic:\n\t\texplicit StructuredBufferPool(std::size_t size_in_bytes);\n\t\tvirtual ~StructuredBufferPool() = default;\n\n\t\tStructuredBufferPool(StructuredBufferPool const &) = delete;\n\t\tStructuredBufferPool& operator=(StructuredBufferPool const &) = delete;\n\t\tStructuredBufferPool(StructuredBufferPool&&) = delete;\n\t\tStructuredBufferPool& operator=(StructuredBufferPool&&) = delete;\n\n\t\t[[nodiscard]] StructuredBufferHandle* Create(std::size_t size, std::size_t stride, bool used_as_uav);\n\t\tvoid Destroy(StructuredBufferHandle* handle);\n\n\t\tvoid Update(StructuredBufferHandle* handle, void* data, std::size_t size, std::size_t offset);\n\n\t\tvirtual void Evict() = 0;\n\t\tvirtual void MakeResident() = 0;\n\n\tprotected:\n\t\t[[nodiscard]] virtual StructuredBufferHandle* CreateBuffer(std::size_t size, std::size_t stride, bool used_as_uav) = 0;\n\t\tvirtual void DestroyBuffer(StructuredBufferHandle* handle) = 0;\n\t\tvirtual void UpdateBuffer(StructuredBufferHandle* handle, void* data, std::size_t size, std::size_t offset) = 0;\n\n\t\tstd::size_t m_size_in_bytes;\n\t\tstd::mutex m_mutex;\n\t};\n\n\n} /* wr */"
  },
  {
    "path": "src/util/aabb.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"aabb.hpp\"\n\nnamespace wr\n{\n\n\tBox::Box() : \n\t\tm_data\n\t\t{ \n\t\t\t{\n\t\t\t\tstd::numeric_limits<float>::max(),\n\t\t\t\tstd::numeric_limits<float>::max(),\n\t\t\t\tstd::numeric_limits<float>::max()\n\t\t\t},\n\t\t\t{\n\t\t\t\t-std::numeric_limits<float>::max(),\n\t\t\t\t-std::numeric_limits<float>::max(),\n\t\t\t\t-std::numeric_limits<float>::max()\n\t\t\t},\n\t\t\t{\n\t\t\t\tstd::numeric_limits<float>::max(),\n\t\t\t\tstd::numeric_limits<float>::max(),\n\t\t\t\tstd::numeric_limits<float>::max()\n\t\t\t},\n\t\t\t{\n\t\t\t\t-std::numeric_limits<float>::max(),\n\t\t\t\t-std::numeric_limits<float>::max(),\n\t\t\t\t-std::numeric_limits<float>::max()\n\t\t\t},\n\t\t\t{\n\t\t\t\tstd::numeric_limits<float>::max(),\n\t\t\t\tstd::numeric_limits<float>::max(),\n\t\t\t\tstd::numeric_limits<float>::max()\n\t\t\t},\n\t\t\t{\n\t\t\t\t-std::numeric_limits<float>::max(),\n\t\t\t\t-std::numeric_limits<float>::max(),\n\t\t\t\t-std::numeric_limits<float>::max()\n\t\t\t} \n\t\t}\n\t{\n\n\t}\n\n\tBox::Box(DirectX::XMVECTOR(&corners)[6])\n\t{\n\t\tmemcpy(m_data, corners, sizeof(corners));\n\t}\n\n\tDirectX::XMVECTOR& Box::operator[](size_t i)\n\t{\n\t\treturn m_data[i];\n\t}\n\n\tDirectX::XMVECTOR& AABB::operator[](size_t i)\n\t{\n\t\treturn m_data[i];\n\t}\n\n\tvoid AABB::Expand(DirectX::XMVECTOR pos)\n\t{\n\t\tm_min =\n\t\t{\n\t\t\tstd::min(pos.m128_f32[0], *m_minf),\n\t\t\tstd::min(pos.m128_f32[1], m_minf[1]),\n\t\t\tstd::min(pos.m128_f32[2], m_minf[2]),\n\t\t\t1\n\t\t};\n\n\t\tm_max =\n\t\t{\n\t\t\tstd::max(pos.m128_f32[0], *m_maxf),\n\t\t\tstd::max(pos.m128_f32[1], m_maxf[1]),\n\t\t\tstd::max(pos.m128_f32[2], m_maxf[2]),\n\t\t\t1\n\t\t};\n\t}\n\n\tAABB::AABB() : \n\t\tm_data\n\t\t{\n\t\t\t{\n\t\t\t\tstd::numeric_limits<float>::max(),\n\t\t\t\tstd::numeric_limits<float>::max(),\n\t\t\t\tstd::numeric_limits<float>::max(),\n\t\t\t\t1\n\t\t\t},\n\t\t\t{\n\t\t\t\t-std::numeric_limits<float>::max(),\n\t\t\t\t-std::numeric_limits<float>::max(),\n\t\t\t\t-std::numeric_limits<float>::max(),\n\t\t\t\t1\n\t\t\t}\n\t\t}\n\t{\n\n\t}\n\n\tAABB::AABB(DirectX::XMVECTOR min, DirectX::XMVECTOR max) : m_data{ min, max }\n\t{\n\t}\n\n\tAABB AABB::FromTransform(Box box, DirectX::XMMATRIX transform)\n\t{\n\t\tAABB aabb;\n\n\t\t//Transform all coords from model to world space\n\t\t//Pick the min/max bounds\n\n\t\tfor (DirectX::XMVECTOR& vec : box.m_data)\n\t\t{\n\t\t\tDirectX::XMVECTOR tvec = DirectX::XMVector4Transform(vec, transform);\n\t\t\taabb.Expand(tvec);\n\t\t}\n\n\t\treturn aabb;\n\t}\n\n\tvoid Box::ExpandFromVector(DirectX::XMVECTOR pos)\n\t{\n\n\t\tif (pos.m128_f32[0] < m_corners.m_xmin.m128_f32[0])\n\t\t{\n\t\t\tm_corners.m_xmin = pos;\n\t\t}\n\n\t\tif (pos.m128_f32[0] > m_corners.m_xmax.m128_f32[0])\n\t\t{\n\t\t\tm_corners.m_xmax = pos;\n\t\t}\n\n\t\tif (pos.m128_f32[1] < m_corners.m_ymin.m128_f32[1])\n\t\t{\n\t\t\tm_corners.m_ymin = pos;\n\t\t}\n\n\t\tif (pos.m128_f32[1] > m_corners.m_ymax.m128_f32[1])\n\t\t{\n\t\t\tm_corners.m_ymax = pos;\n\t\t}\n\n\t\tif (pos.m128_f32[2] < m_corners.m_zmin.m128_f32[2])\n\t\t{\n\t\t\tm_corners.m_zmin = pos;\n\t\t}\n\n\t\tif (pos.m128_f32[2] > m_corners.m_zmax.m128_f32[2])\n\t\t{\n\t\t\tm_corners.m_zmax = pos;\n\t\t}\n\t}\n\n\tvoid Box::Expand(float(&pos)[3])\n\t{\n\t\tExpandFromVector(DirectX::XMVECTOR{ pos[0], pos[1], pos[2], 1 });\n\t}\n\n\tSphere::Sphere(): m_sphere { \n\t\tstd::numeric_limits<float>::max(), \n\t\tstd::numeric_limits<float>::max(), \n\t\tstd::numeric_limits<float>::max(), \n\t\t-std::numeric_limits<float>::max()\n\t}{ }\n\n\tSphere::Sphere(DirectX::XMVECTOR center, float radius): m_sphere {\n\t\tcenter.m128_f32[0],\n\t\tcenter.m128_f32[1],\n\t\tcenter.m128_f32[2],\n\t\tradius\n\t}{ }\n\n\tbool AABB::InFrustum(const std::array<DirectX::XMVECTOR, 6>& planes) const\n\t{\n\t\tfor (const DirectX::XMVECTOR& plane : planes)\n\t\t{\n\t\t\t/* Get point of AABB that's into the plane the most */\n\n\t\t\tDirectX::XMVECTOR axis_vert = {\n\t\t\t\t*m_data[*plane.m128_f32 >= 0].m128_f32,\n\t\t\t\tm_data[plane.m128_f32[1] >= 0].m128_f32[1],\n\t\t\t\tm_data[plane.m128_f32[2] >= 0].m128_f32[2]\n\t\t\t};\n\n\t\t\t/* Check if it's outside */\n\n\t\t\tif (*DirectX::XMVector3Dot(plane, axis_vert).m128_f32 + plane.m128_f32[3] < 0)\n\t\t\t\treturn false;\n\n\t\t}\n\n\t\treturn true;\n\n\t}\n\n\tbool AABB::Contains(const Sphere& sphere) const\n\t{\n\t\tfloat square_dist = 0;\n\n\t\tfor(size_t i = 0; i < 3; ++i)\n\t\t{\n\t\t\tif(sphere.m_data[i] < m_minf[i])\n\t\t\t{\n\t\t\t\tsquare_dist += pow(sphere.m_data[i] - m_minf[i], 2);\n\t\t\t}\n\t\t\telse if(sphere.m_data[i] > m_maxf[i])\n\t\t\t{\n\t\t\t\tsquare_dist += pow(sphere.m_data[i] - m_maxf[i], 2);\n\t\t\t}\n\t\t}\n\n\t\tconst float r_squared = sphere.m_radius * sphere.m_radius;\n\t\treturn square_dist <= r_squared;\n\t}\n\n}"
  },
  {
    "path": "src/util/aabb.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include <DirectXMath.h>\n#include <limits>\n#include <algorithm>\n#include <array>\n\nnamespace wr\n{\n\tstruct Box\n\t{\n\t\tstruct Corners\n\t\t{\n\t\t\tDirectX::XMVECTOR m_xmin, m_xmax, m_ymin, m_ymax, m_zmin, m_zmax;\n\t\t};\n\n\t\tunion\n\t\t{\n\t\t\tCorners m_corners;\n\t\t\tDirectX::XMVECTOR m_data[6];\n\t\t};\n\n\t\t//Max bounds on each corner\n\t\tBox();\n\n\t\tBox(DirectX::XMVECTOR (&corners)[6]);\n\n\t\tDirectX::XMVECTOR &operator[](size_t i);\n\n\t\t//Expand bounds using position\n\t\tvoid ExpandFromVector(DirectX::XMVECTOR pos);\n\n\t\tvoid Expand(float(&pos)[3]);\n\n\t};\n\n\tstruct Sphere \n\t{\n\t\t\n\t\tunion\n\t\t{\n\t\t\tDirectX::XMVECTOR m_sphere;\n\n\t\t\tstruct\n\t\t\t{\n\t\t\t\tDirectX::XMFLOAT3 m_center;\n\t\t\t\tfloat m_radius;\n\t\t\t};\n\n\t\t\tstruct {\n\t\t\t\tfloat m_data[4];\n\t\t\t};\n\n\t\t};\n\n\t\tSphere();\n\t\tSphere(DirectX::XMVECTOR center, float radius);\n\n\t};\n\n\tstruct AABB\n\t{\n\t\tunion\n\t\t{\n\n\t\t\tDirectX::XMVECTOR m_data[2];\n\n\t\t\tstruct\n\t\t\t{\n\t\t\t\tDirectX::XMVECTOR m_min, m_max;\n\t\t\t};\n\n\t\t\tstruct {\n\n\t\t\t\tfloat m_minf[3];\n\t\t\t\tfloat m_pad0;\n\n\t\t\t\tfloat m_maxf[3];\n\t\t\t\tfloat m_pad1;\n\n\t\t\t};\n\n\t\t};\n\n\t\tAABB();\n\t\tAABB(DirectX::XMVECTOR min, DirectX::XMVECTOR max);\n\n\t\tDirectX::XMVECTOR& operator[](size_t i);\n\n\t\t//Uses position to expand the AABB\n\t\tvoid Expand(DirectX::XMVECTOR pos);\n\n\t\t//Check if the frustum planes intersect with the AABB\n\t\tbool InFrustum(const std::array<DirectX::XMVECTOR, 6>& planes) const;\n\n\t\t//Check if the sphere intersects with the AABB\n\t\tbool Contains(const Sphere& sphere) const;\n\n\t\t//Generates AABB from transform and box\n\t\tstatic AABB FromTransform(Box box, DirectX::XMMATRIX transform);\n\n\t};\n\n}"
  },
  {
    "path": "src/util/bitmap_allocator.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <cstdint>\n#include <vector>\n#include <optional>\n\nnamespace util\n{\n\tinline std::uint64_t IndexFromBit(std::uint64_t frame)\n\t{\n\t\treturn frame / (8 * 8);\n\t}\n\n\tinline std::uint64_t OffsetFromBit(std::uint64_t frame)\n\t{\n\t\treturn frame % (8 * 8);\n\t}\n\n\tinline void SetPage(std::vector<uint64_t> & bitmap, std::uint64_t frame)\n\t{\n\t\tconst std::uint64_t idx = IndexFromBit(frame);\n\t\tconst std::uint64_t off = OffsetFromBit(frame);\n\t\tbitmap[idx] |= (1Ui64 << off);\n\t}\n\n\tinline void ClearPage(std::vector<uint64_t> & bitmap, std::uint64_t frame)\n\t{\n\t\tconst std::uint64_t idx = IndexFromBit(frame);\n\t\tconst std::uint64_t off = OffsetFromBit(frame);\n\t\tbitmap[idx] &= ~(1Ui64 << off);\n\t}\n\n\tinline bool TestPage(std::vector<uint64_t> const & bitmap, std::uint64_t frame)\n\t{\n\t\tconst std::uint64_t idx = IndexFromBit(frame);\n\t\tconst std::uint64_t off = OffsetFromBit(frame);\n\n\t\treturn (bitmap[idx] & (1Ui64 << off));\n\t}\n\n\tinline std::optional<std::uint64_t> FindFreePage(std::vector<std::uint64_t> const & bitmap, std::size_t const frame_count, std::uint64_t needed_frames)\n\t{\n\t\tbool counting = false;\n\t\tbool found = false;\n\n\t\tstd::uint64_t start_frame = 0;\n\t\tstd::uint64_t free_frames = 0;\n\n\t\t//Go through all all pages to find free ones.\n\t\t//Because we're storing the data in a single bit we can compare an entire int64 at once to speed things up\n\t\tfor (std::uint64_t i = 0; i < bitmap.size(); ++i)\n\t\t{\n\t\t\t//Check 64 pages at once\n\t\t\tif (bitmap[i] != 0Ui64)\n\t\t\t{\n\t\t\t\t//At least a single page is free, so check all 64 pages\n\t\t\t\tfor (std::uint64_t j = 0; j < 64; ++j)\n\t\t\t\t{\n\t\t\t\t\t//If the current page being checked is larger than the amount of available pages, break\n\t\t\t\t\tif (i * 64 + j >= frame_count)\n\t\t\t\t\t{\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\t//Set bit corresponding to page being checked in temporary variable\n\t\t\t\t\tstd::uint64_t to_test = 1Ui64 << j;\n\n\t\t\t\t\t//Run an 'and' against the temporary variable to see if the page is available\n\t\t\t\t\tif ((bitmap[i] & to_test))\n\t\t\t\t\t{\n\t\t\t\t\t\t//Is this the first free page found?\n\t\t\t\t\t\tif (counting)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t//Not the first free page, so increment the counter of consecutive free pages\n\t\t\t\t\t\t\tfree_frames++;\n\t\t\t\t\t\t\t//Have we found the nessecary amount of free pages? If so, indicate that and break\n\t\t\t\t\t\t\tif (free_frames == needed_frames)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tfound = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t//This is the first free page\n\t\t\t\t\t\t\tif (needed_frames == 1)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t//We only need a single page, so store the number of the page and break\n\t\t\t\t\t\t\t\tfound = true;\n\t\t\t\t\t\t\t\tstart_frame = i * 64 + j;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t//We need multiple pages, so store the number of this page as the start page and continue\n\t\t\t\t\t\t\t\tstart_frame = i * 64 + j;\n\t\t\t\t\t\t\t\tcounting = true;\n\t\t\t\t\t\t\t\tfree_frames = 1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\t//The page wasn't free, so stop counting.\n\t\t\t\t\t\tcounting = 0;\n\t\t\t\t\t\tfree_frames = 0;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t//All 64 pages are occupied, so stop counting\n\t\t\t\tcounting = false;\n\t\t\t\tfree_frames = 0;\n\t\t\t}\n\n\t\t\t//Have we found enough free pages? If so, break\n\t\t\tif (found)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\treturn start_frame;\n\t}\n} /* util */\n"
  },
  {
    "path": "src/util/defines.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n//! A Scene Graph Implementation Binder Macro\n/*!\n  This macro is used to link a initialization and a render function to a specific node type.\n*/\n#define LINK_NODE_FUNCTION(renderer_type, node_type, update_function) \\\ndecltype(node_type::update_func_impl) node_type::update_func_impl = [](wr::RenderSystem* render_system, Node* node) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->update_function(static_cast<node_type*>(node)); \\\n};\n\n//! World Up\nstatic constexpr float world_up[3] = {0, 1, 0};\n\n//! Used to check whether a method exists in a class.\n#define DEFINE_HAS_METHOD(f) \\\ntemplate<typename, typename T> \\\nstruct HasMethod_##f { \\\n    static_assert( \\\n        std::integral_constant<T, false>::value, \\\n        \"Second template parameter needs to be of function type.\"); \\\n}; \\\n \\\ntemplate<typename C, typename Ret, typename... Args> \\\nstruct HasMethod_##f<C, Ret(Args...)> { \\\nprivate: \\\n    template<typename T> \\\n    static constexpr auto Check(T*) \\\n    -> typename \\\n        std::is_same< \\\n            decltype( std::declval<T>().f( std::declval<Args>()... ) ), \\\n            Ret \\\n        >::type; \\\n \\\n    template<typename> \\\n    static constexpr std::false_type Check(...); \\\n \\\n    typedef decltype(Check<C>(0)) type; \\\n \\\npublic: \\\n    static constexpr bool value = type::value; \\\n};\n\nDEFINE_HAS_METHOD(GetInputLayout)\n\n#define IS_PROPER_VERTEX_CLASS(type) static_assert(HasMethod_GetInputLayout<type, std::vector<D3D12_INPUT_ELEMENT_DESC>()>::value, \"Could not locate the required type::GetInputLayout function. If intelisense gives you this error ignore it.\");\n\n//! Defines to make linking to sg easier.\n#define LINK_SG_RENDER_MESHES(renderer_type, function) \\\ndecltype(wr::SceneGraph::m_render_meshes_func_impl) wr::SceneGraph::m_render_meshes_func_impl = [](wr::RenderSystem* render_system, wr::temp::MeshBatches& nodes, wr::CameraNode* camera, wr::CommandList* cmd_list) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->function(nodes, camera, cmd_list); \\\n};\n\n#define LINK_SG_INIT_MESHES(renderer_type, function) \\\ndecltype(wr::SceneGraph::m_init_meshes_func_impl) wr::SceneGraph::m_init_meshes_func_impl = [](wr::RenderSystem* render_system, std::vector<std::shared_ptr<wr::MeshNode>>& nodes) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->function(nodes); \\\n};\n#define LINK_SG_INIT_CAMERAS(renderer_type, function) \\\ndecltype(wr::SceneGraph::m_init_cameras_func_impl) wr::SceneGraph::m_init_cameras_func_impl = [](wr::RenderSystem* render_system, std::vector<std::shared_ptr<wr::CameraNode>>& nodes) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->function(nodes); \\\n};\n#define LINK_SG_INIT_LIGHTS(renderer_type, function) \\\ndecltype(wr::SceneGraph::m_init_lights_func_impl) wr::SceneGraph::m_init_lights_func_impl = [](wr::RenderSystem* render_system, std::vector<std::shared_ptr<wr::LightNode>>& nodes, std::vector<Light>& lights) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->function(nodes, lights); \\\n};\n\n#define LINK_SG_UPDATE_MESHES(renderer_type, function) \\\ndecltype(wr::SceneGraph::m_update_meshes_func_impl) wr::SceneGraph::m_update_meshes_func_impl = [](wr::RenderSystem* render_system, std::vector<std::shared_ptr<wr::MeshNode>>& nodes) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->function(nodes); \\\n};\n#define LINK_SG_UPDATE_CAMERAS(renderer_type, function) \\\ndecltype(wr::SceneGraph::m_update_cameras_func_impl) wr::SceneGraph::m_update_cameras_func_impl = [](wr::RenderSystem* render_system, std::vector<std::shared_ptr<wr::CameraNode>>& nodes) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->function(nodes); \\\n};\n#define LINK_SG_UPDATE_LIGHTS(renderer_type, function) \\\ndecltype(wr::SceneGraph::m_update_lights_func_impl) wr::SceneGraph::m_update_lights_func_impl = [](wr::RenderSystem* render_system, wr::SceneGraph& scene_graph) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->function(scene_graph); \\\n};\n#define LINK_SG_UPDATE_TRANSFORMS(renderer_type, function) \\\ndecltype(wr::SceneGraph::m_update_transforms_func_impl) wr::SceneGraph::m_update_transforms_func_impl = [](wr::RenderSystem* render_system, wr::SceneGraph& scene_graph, std::shared_ptr<wr::Node>& node) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->function(scene_graph, node); \\\n};\n#define LINK_SG_DELETE_SKYBOX(renderer_type, function) \\\ndecltype(wr::SceneGraph::m_delete_skybox_func_impl) wr::SceneGraph::m_delete_skybox_func_impl = [](wr::RenderSystem* render_system, wr::SceneGraph& scene_graph, std::shared_ptr<wr::SkyboxNode>& skybox_node) \\\n{ \\\n\tstatic_cast<renderer_type*>(render_system)->function(scene_graph, skybox_node); \\\n};"
  },
  {
    "path": "src/util/delegate.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <cassert>\n#include <memory>\n#include <new>\n#include <type_traits>\n#include <utility>\n\nnamespace util\n{\n\n\ttemplate <typename T> class Delegate;\n\n\ttemplate<class R, class ...A>\n\tclass Delegate<R(A...)>\n\t{\n\t\tusing stub_ptr_type = R(*)(void*, A&&...);\n\n\t\tDelegate(void* const o, stub_ptr_type const m) noexcept :\n\t\t\tm_object_ptr(o),\n\t\t\tm_stub_ptr(m)\n\t\t{\n\t\t}\n\n\tpublic:\n\t\tDelegate() :\n\t\t\tm_object_ptr(),\n\t\t\tm_deleter(),\n\t\t\tm_store_size()\n\t\t{\n\t\t}\n\n\t\tDelegate(Delegate const&) = default;\n\n\t\tDelegate(Delegate&&) = default;\n\n\t\tDelegate(::std::nullptr_t const) noexcept : Delegate() { }\n\n\t\ttemplate <class C, typename =\n\t\t\ttypename ::std::enable_if < ::std::is_class<C>{} > ::type >\n\t\t\texplicit Delegate(C const* const o) noexcept :\n\t\t\tm_object_ptr(const_cast<C*>(o))\n\t\t{\n\t\t}\n\n\t\ttemplate <class C, typename =\n\t\t\ttypename ::std::enable_if < ::std::is_class<C>{} > ::type >\n\t\t\texplicit Delegate(C const& o) noexcept :\n\t\t\tm_object_ptr(const_cast<C*>(&o))\n\t\t{\n\t\t}\n\n\t\ttemplate <class C>\n\t\tDelegate(C* const object_ptr, R(C::* const method_ptr)(A...))\n\t\t{\n\t\t\t*this = from(object_ptr, method_ptr);\n\t\t}\n\n\t\ttemplate <class C>\n\t\tDelegate(C* const object_ptr, R(C::* const method_ptr)(A...) const)\n\t\t{\n\t\t\t*this = from(object_ptr, method_ptr);\n\t\t}\n\n\t\ttemplate <class C>\n\t\tDelegate(C& object, R(C::* const method_ptr)(A...))\n\t\t{\n\t\t\t*this = from(object, method_ptr);\n\t\t}\n\n\t\ttemplate <class C>\n\t\tDelegate(C const& object, R(C::* const method_ptr)(A...) const)\n\t\t{\n\t\t\t*this = from(object, method_ptr);\n\t\t}\n\n\t\ttemplate <\n\t\t\ttypename T,\n\t\t\ttypename = typename ::std::enable_if <\n\t\t\t!::std::is_same<Delegate, typename ::std::decay<T>::type>{}\n\t\t\t> ::type\n\t\t>\n\t\t\t\tDelegate(T&& f) :\n\t\t\t\tm_store(operator new(sizeof(typename ::std::decay<T>::type)),\n\t\t\t\t\tfunctor_deleter<typename ::std::decay<T>::type>),\n\t\t\t\tm_store_size(sizeof(typename ::std::decay<T>::type))\n\t\t\t{\n\t\t\t\tusing functor_type = typename ::std::decay<T>::type;\n\n\t\t\t\tnew (m_store.get()) functor_type(::std::forward<T>(f));\n\n\t\t\t\tm_object_ptr = m_store.get();\n\n\t\t\t\tm_stub_ptr = functor_stub<functor_type>;\n\n\t\t\t\tm_deleter = deleter_stub<functor_type>;\n\t\t\t}\n\n\t\t\tDelegate& operator=(Delegate const&) = default;\n\n\t\t\tDelegate& operator=(Delegate&&) = default;\n\n\t\t\ttemplate <class C>\n\t\t\tDelegate& operator=(R(C::* const rhs)(A...))\n\t\t\t{\n\t\t\t\treturn *this = from(static_cast<C*>(m_object_ptr), rhs);\n\t\t\t}\n\n\t\t\ttemplate <class C>\n\t\t\tDelegate& operator=(R(C::* const rhs)(A...) const)\n\t\t\t{\n\t\t\t\treturn *this = from(static_cast<C const*>(m_object_ptr), rhs);\n\t\t\t}\n\n\t\t\ttemplate <\n\t\t\t\ttypename T,\n\t\t\t\ttypename = typename ::std::enable_if <\n\t\t\t\t!::std::is_same<Delegate, typename ::std::decay<T>::type>{}\n\t\t\t\t> ::type\n\t\t\t>\n\t\t\t\t\tDelegate& operator=(T&& f)\n\t\t\t\t{\n\t\t\t\t\tusing functor_type = typename ::std::decay<T>::type;\n\n\t\t\t\t\t// Note that use_count is an approximation in multithreaded environments.\n\t\t\t\t\tif ((sizeof(functor_type) > m_store_size) || m_store.use_count() != 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tm_store.reset(operator new(sizeof(functor_type)),\n\t\t\t\t\t\t\tfunctor_deleter<functor_type>);\n\n\t\t\t\t\t\tm_store_size = sizeof(functor_type);\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tm_deleter(m_store.get());\n\t\t\t\t\t}\n\n\t\t\t\t\tnew (m_store.get()) functor_type(::std::forward<T>(f));\n\n\t\t\t\t\tm_object_ptr = m_store.get();\n\n\t\t\t\t\tm_stub_ptr = functor_stub<functor_type>;\n\n\t\t\t\t\tm_deleter = deleter_stub<functor_type>;\n\n\t\t\t\t\treturn *this;\n\t\t\t\t}\n\n\t\t\t\ttemplate <R(*const function_ptr)(A...)>\n\t\t\t\tstatic Delegate from() noexcept\n\t\t\t\t{\n\t\t\t\t\treturn { nullptr, function_stub<function_ptr> };\n\t\t\t\t}\n\n\t\t\t\ttemplate <class C, R(C::* const method_ptr)(A...)>\n\t\t\t\tstatic Delegate from(C* const object_ptr) noexcept\n\t\t\t\t{\n\t\t\t\t\treturn { object_ptr, method_stub<C, method_ptr> };\n\t\t\t\t}\n\n\t\t\t\ttemplate <class C, R(C::* const method_ptr)(A...) const>\n\t\t\t\tstatic Delegate from(C const* const object_ptr) noexcept\n\t\t\t\t{\n\t\t\t\t\treturn { const_cast<C*>(object_ptr), const_method_stub<C, method_ptr> };\n\t\t\t\t}\n\n\t\t\t\ttemplate <class C, R(C::* const method_ptr)(A...)>\n\t\t\t\tstatic Delegate from(C& object) noexcept\n\t\t\t\t{\n\t\t\t\t\treturn { &object, method_stub<C, method_ptr> };\n\t\t\t\t}\n\n\t\t\t\ttemplate <class C, R(C::* const method_ptr)(A...) const>\n\t\t\t\tstatic Delegate from(C const& object) noexcept\n\t\t\t\t{\n\t\t\t\t\treturn { const_cast<C*>(&object), const_method_stub<C, method_ptr> };\n\t\t\t\t}\n\n\t\t\t\ttemplate <typename T>\n\t\t\t\tstatic Delegate from(T&& f)\n\t\t\t\t{\n\t\t\t\t\treturn ::std::forward<T>(f);\n\t\t\t\t}\n\n\t\t\t\tstatic Delegate from(R(*const function_ptr)(A...))\n\t\t\t\t{\n\t\t\t\t\treturn function_ptr;\n\t\t\t\t}\n\n\t\t\t\ttemplate <class C>\n\t\t\t\tusing member_pair =\n\t\t\t\t\t::std::pair<C* const, R(C::* const)(A...)>;\n\n\t\t\t\ttemplate <class C>\n\t\t\t\tusing const_member_pair =\n\t\t\t\t\t::std::pair<C const* const, R(C::* const)(A...) const>;\n\n\t\t\t\ttemplate <class C>\n\t\t\t\tstatic Delegate from(C* const object_ptr,\n\t\t\t\t\tR(C::* const method_ptr)(A...))\n\t\t\t\t{\n\t\t\t\t\treturn member_pair<C>(object_ptr, method_ptr);\n\t\t\t\t}\n\n\t\t\t\ttemplate <class C>\n\t\t\t\tstatic Delegate from(C const* const object_ptr,\n\t\t\t\t\tR(C::* const method_ptr)(A...) const)\n\t\t\t\t{\n\t\t\t\t\treturn const_member_pair<C>(object_ptr, method_ptr);\n\t\t\t\t}\n\n\t\t\t\ttemplate <class C>\n\t\t\t\tstatic Delegate from(C& object, R(C::* const method_ptr)(A...))\n\t\t\t\t{\n\t\t\t\t\treturn member_pair<C>(&object, method_ptr);\n\t\t\t\t}\n\n\t\t\t\ttemplate <class C>\n\t\t\t\tstatic Delegate from(C const& object,\n\t\t\t\t\tR(C::* const method_ptr)(A...) const)\n\t\t\t\t{\n\t\t\t\t\treturn const_member_pair<C>(&object, method_ptr);\n\t\t\t\t}\n\n\t\t\t\tvoid reset() { m_stub_ptr = nullptr; m_store.reset(); }\n\n\t\t\t\tvoid reset_stub() noexcept { m_stub_ptr = nullptr; }\n\n\t\t\t\tvoid swap(Delegate& other) noexcept { ::std::swap(*this, other); }\n\n\t\t\t\tbool operator==(Delegate const& rhs) const noexcept\n\t\t\t\t{\n\t\t\t\t\treturn (m_object_ptr == rhs.m_object_ptr) && (m_stub_ptr == rhs.m_stub_ptr);\n\t\t\t\t}\n\n\t\t\t\tbool operator!=(Delegate const& rhs) const noexcept\n\t\t\t\t{\n\t\t\t\t\treturn !operator==(rhs);\n\t\t\t\t}\n\n\t\t\t\tbool operator<(Delegate const& rhs) const noexcept\n\t\t\t\t{\n\t\t\t\t\treturn (m_object_ptr < rhs.m_object_ptr) ||\n\t\t\t\t\t\t((m_object_ptr == rhs.m_object_ptr) && (m_stub_ptr < rhs.m_stub_ptr));\n\t\t\t\t}\n\n\t\t\t\tbool operator==(::std::nullptr_t const) const noexcept\n\t\t\t\t{\n\t\t\t\t\treturn !m_stub_ptr;\n\t\t\t\t}\n\n\t\t\t\tbool operator!=(::std::nullptr_t const) const noexcept\n\t\t\t\t{\n\t\t\t\t\treturn m_stub_ptr;\n\t\t\t\t}\n\n\t\t\t\texplicit operator bool() const noexcept { return m_stub_ptr; }\n\n\t\t\t\tR operator()(A... args) const\n\t\t\t\t{\n\t\t\t\t\t//  assert(stub_ptr);\n\t\t\t\t\treturn m_stub_ptr(m_object_ptr, ::std::forward<A>(args)...);\n\t\t\t\t}\n\n\tprivate:\n\t\tfriend struct ::std::hash<Delegate>;\n\n\t\tusing deleter_type = void(*)(void*);\n\n\t\tvoid* m_object_ptr;\n\t\tstub_ptr_type m_stub_ptr{};\n\n\t\tdeleter_type m_deleter;\n\n\t\t::std::shared_ptr<void> m_store;\n\t\t::std::size_t m_store_size;\n\n\t\ttemplate <class T>\n\t\tstatic void functor_deleter(void* const p)\n\t\t{\n\t\t\tstatic_cast<T*>(p)->~T();\n\n\t\t\toperator delete(p);\n\t\t}\n\n\t\ttemplate <class T>\n\t\tstatic void deleter_stub(void* const p)\n\t\t{\n\t\t\tstatic_cast<T*>(p)->~T();\n\t\t}\n\n\t\ttemplate <R(*function_ptr)(A...)>\n\t\tstatic R function_stub(void* const, A&&... args)\n\t\t{\n\t\t\treturn function_ptr(::std::forward<A>(args)...);\n\t\t}\n\n\t\ttemplate <class C, R(C::*method_ptr)(A...)>\n\t\tstatic R method_stub(void* const object_ptr, A&&... args)\n\t\t{\n\t\t\treturn (static_cast<C*>(object_ptr)->*method_ptr)(\n\t\t\t\t::std::forward<A>(args)...);\n\t\t}\n\n\t\ttemplate <class C, R(C::*method_ptr)(A...) const>\n\t\tstatic R const_method_stub(void* const object_ptr, A&&... args)\n\t\t{\n\t\t\treturn (static_cast<C const*>(object_ptr)->*method_ptr)(\n\t\t\t\t::std::forward<A>(args)...);\n\t\t}\n\n\t\ttemplate <typename>\n\t\tstruct is_member_pair : std::false_type { };\n\n\t\ttemplate <class C>\n\t\tstruct is_member_pair< ::std::pair<C* const,\n\t\t\tR(C::* const)(A...)> > : std::true_type\n\t\t{\n\t\t};\n\n\t\ttemplate <typename>\n\t\tstruct is_const_member_pair : std::false_type { };\n\n\t\ttemplate <class C>\n\t\tstruct is_const_member_pair< ::std::pair<C const* const,\n\t\t\tR(C::* const)(A...) const> > : std::true_type\n\t\t{\n\t\t};\n\n\t\ttemplate <typename T>\n\t\tstatic typename ::std::enable_if <\n\t\t\t!(is_member_pair<T>() ||\n\t\t\t\tis_const_member_pair<T>()),\n\t\t\tR\n\t\t> ::type\n\t\t\tfunctor_stub(void* const object_ptr, A&&... args)\n\t\t{\n\t\t\treturn (*static_cast<T*>(object_ptr))(::std::forward<A>(args)...);\n\t\t}\n\n\t\ttemplate <typename T>\n\t\tstatic typename ::std::enable_if <\n\t\t\tis_member_pair<T>() ||\n\t\t\tis_const_member_pair<T>(),\n\t\t\tR\n\t\t> ::type\n\t\t\tfunctor_stub(void* const object_ptr, A&&... args)\n\t\t{\n\t\t\treturn (static_cast<T*>(object_ptr)->first->*\n\t\t\t\tstatic_cast<T*>(object_ptr)->second)(::std::forward<A>(args)...);\n\t\t}\n\t};\n} /* util */\n\nnamespace std\n{\n\ttemplate <typename R, typename ...A>\n\tstruct hash<::util::Delegate<R(A...)> >\n\t{\n\t\tsize_t operator()(::util::Delegate<R(A...)> const& d) const noexcept\n\t\t{\n\t\t\tauto const seed(hash<void*>()(d.object_ptr_));\n\n\t\t\treturn hash<typename ::util::Delegate<R(A...)>::stub_ptr_type>()(\n\t\t\t\td.stub_ptr_) + 0x9e3779b9 + (seed << 6) + (seed >> 2);\n\t\t}\n\t};\n}"
  },
  {
    "path": "src/util/file_watcher.cpp",
    "content": "/*!\r\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\r\n *\r\n * Licensed under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n *     http://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an \"AS IS\" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n */\r\n#include \"file_watcher.hpp\"\r\n\r\n#include <thread>\r\n#include \"log.hpp\"\r\n\r\nnamespace util\r\n{\r\n\r\n\tFileWatcher::FileWatcher(std::string const & path,\r\n\t\tconst std::chrono::milliseconds delay,\r\n\t\tbool regular_files_only)\r\n\t\t: m_watch_path(path),\r\n\t\tm_delay(delay),\r\n\t\tm_regular_files_only(regular_files_only),\r\n\t\tm_running(false)\r\n\t{\r\n\t\t// Record all files\r\n\t\tfor (auto &file : std::filesystem::recursive_directory_iterator(path)) {\r\n\t\t\tm_paths[file.path().string()] = std::filesystem::last_write_time(file);\r\n\t\t}\r\n\t}\r\n\r\n\tFileWatcher::~FileWatcher()\r\n\t{\r\n\t\tif (m_thread.joinable())\r\n\t\t{\r\n\t\t\tm_running = false;\r\n\t\t\tm_thread.join();\r\n\t\t}\r\n\t}\r\n\r\n\tvoid FileWatcher::Start(util::Delegate<void(std::string const &, FileWatcher::FileStatus)> const & callback)\r\n\t{\r\n\t\tif (m_running)\r\n\t\t{\r\n\t\t\tLOGW(\"Tried to start the file watcher while it is already running\");\r\n\t\t\treturn;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tm_running = true;\r\n\t\t}\r\n\r\n\t\t// Lambda that checks whether the callback should be called or not based on settings.\r\n\t\tauto run_callback = [this, callback](std::string path, FileStatus status) {\r\n\t\t\tbool is_special = (!std::filesystem::is_regular_file(std::filesystem::path(path)) && status != FileStatus::ERASED);\r\n\t\t\tif (m_regular_files_only && is_special)\r\n\t\t\t{\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\r\n\t\t\tcallback(path, status);\r\n\t\t};\r\n\r\n\t\twhile (m_running)\r\n\t\t{\r\n\t\t\tstd::this_thread::sleep_for(m_delay);\r\n\r\n\t\t\t// Check for file ereasure.\r\n\t\t\tfor (auto& it : m_paths)\r\n\t\t\t{\r\n\t\t\t\tif (!std::filesystem::exists(it.first))\r\n\t\t\t\t{\r\n\t\t\t\t\trun_callback(it.first, FileStatus::ERASED);\r\n\t\t\t\t\tm_paths.erase(it.first);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\t// Check if file was created or modified.\r\n\t\t\tfor (auto& file : std::filesystem::recursive_directory_iterator(m_watch_path))\r\n\t\t\t{\r\n\t\t\t\tauto last_write_time = std::filesystem::last_write_time(file);\r\n\t\t\t\tauto file_path = file.path().string();\r\n\r\n\t\t\t\t// File Created\r\n\t\t\t\tif (!m_paths.contains(file_path))\r\n\t\t\t\t{\r\n\t\t\t\t\tm_paths[file_path] = last_write_time;\r\n\t\t\t\t\trun_callback(file_path, FileStatus::CREATED);\r\n\t\t\t\t}\r\n\t\t\t\t// File Modified\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tif (m_paths[file_path] != last_write_time)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tm_paths[file_path] = last_write_time;\r\n\t\t\t\t\t\trun_callback(file_path, FileStatus::MODIFIED);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tvoid FileWatcher::StartAsync(util::Delegate<void(std::string const & path, FileStatus)> const & callback)\r\n\t{\r\n\t\tif (m_running)\r\n\t\t{\r\n\t\t\tLOGW(\"Tried to start the file watcher while it is already running\");\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tm_thread = std::thread([this, callback]() {\r\n\t\t\tStart(callback);\r\n\t\t});\r\n\t}\r\n\r\n} /* util */\r\n"
  },
  {
    "path": "src/util/file_watcher.hpp",
    "content": "/*!\r\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\r\n *\r\n * Licensed under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n *     http://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an \"AS IS\" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n */\r\n#pragma once\r\n\r\n#include <string>\r\n#include <unordered_map>\r\n#include <filesystem>\r\n#include <functional>\r\n#include <chrono>\r\n#include <thread>\r\n\r\n#include \"delegate.hpp\"\r\n\r\nnamespace util\r\n{\r\n\r\n\t//! FileWatcher\r\n\t/*!\r\n\t\tThis is a simple file watcher that checks every X milliseconds whether a file has been created, modified or erased\r\n\t\tand gives you a callback if a change is detected.\r\n\t\tThis file watcher can run both asyncronously and syncrounously.\r\n\t*/\r\n\tclass FileWatcher\r\n\t{\r\n\tpublic:\r\n\t\tenum class FileStatus\r\n\t\t{\r\n\t\t\tMODIFIED,\r\n\t\t\tCREATED,\r\n\t\t\tERASED\r\n\t\t};\r\n\r\n\t\t//! FileWatcher constructor\r\n\t\t/*!\r\n\t\t\t\\param path The path to watch for changes.\r\n\t\t\t\\param delay The interval used to check changes.\r\n\t\t\t\\param regular_files_only Setting this to true will ignore all non-regular files.\r\n\t\t*/\r\n\t\tFileWatcher(std::string const & path, const std::chrono::milliseconds delay, bool regular_files_only = false);\r\n\t\t~FileWatcher();\r\n\r\n\t\t//! Launches the file watcher. This will stall the current thread.\r\n\t\tvoid Start(util::Delegate<void(std::string const &, FileStatus)> const & callback);\r\n\t\t//! Launches the file watcher asynchrounously. When the file watcher goes out of scope the thread gets killed.\r\n\t\tvoid StartAsync(util::Delegate<void(std::string const &, FileStatus)> const & callback);\r\n\r\n\tprivate:\r\n\t\tbool m_running;\r\n\t\tbool m_regular_files_only;\r\n\t\tconst std::chrono::milliseconds m_delay;\r\n\t\tstd::string m_watch_path;\r\n\t\tstd::unordered_map<std::string, std::filesystem::file_time_type> m_paths;\r\n\t\tstd::thread m_thread;\r\n\t};\r\n\r\n} /* util */\r\n"
  },
  {
    "path": "src/util/log.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"log.hpp\"\n\nnamespace util\n{\n\tstd::function<void(std::string const &)> log_callback::impl = nullptr;\n\twr::LogfileHandler* log_file_handler = nullptr;\n}\n"
  },
  {
    "path": "src/util/log.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"../wisprenderer_export.hpp\"\n#include \"logfile_handler.hpp\"\n\n#define LOG_PRINT_TIME\n//#define LOG_PRINT_THREAD\n#define LOG_CALLBACK\n//#define LOG_PRINT_LOC\n#define LOG_PRINT_COLORS\n//#define LOG_PRINT_TO_OUTPUT\n\n#ifndef _DEBUG\n#define LOG_TO_FILE\n#define LOG_PRINT_LOC\n#endif // DEBUG\n\n#if defined(LOG_PRINT_COLORS) && defined(_WIN32)\n#include <Windows.h>\n#endif\n#ifdef LOG_PRINT_THREAD\n#include <thread>\n#include <sstream>\n#endif\n\n#include <fmt/format.h>\n#include <fmt/chrono.h>\n\n#ifdef LOG_CALLBACK\n#include <functional>\n#endif\n\n#include <filesystem>\n#include <ctime>\n\n#define LOG_BREAK DebugBreak();\n\n#ifdef LOG_CALLBACK\nnamespace util\n{\n\tstruct log_callback\n\t{\n\t\tWISPRENDERER_EXPORT static std::function<void(std::string const &)> impl;\n\t};\n\n\tWISPRENDERER_EXPORT extern wr::LogfileHandler* log_file_handler;\n\n};\n#endif\n\n#pragma warning(push)\n#pragma warning(disable : 4244)\n#pragma warning(disable : 4100)\n\nnamespace util::internal\n{\n\tenum class MSGB_ICON\n\t{\n\t\tCRITICAL_ERROR = (unsigned)MB_OK | (unsigned)MB_ICONERROR\n\t};\n\n    template <typename S, typename... Args>\n\tinline void log_impl(int color, char type, std::string file, std::string func, int line, S const & format, Args const &... args)\n\t{\n\t\tstd::string str = \"\";\n\n#ifdef LOG_PRINT_TIME\n\t\tstd::tm s;\n\t\tstd::time_t t = std::time(nullptr);\n\t\tlocaltime_s(&s, &t);\n\n\t\tstr += fmt::format(\"[{:%H:%M:%S}]\", s) + \" [\" + type + \"] \";\n#endif\n#ifdef LOG_PRINT_THREAD\n\t\tauto thread_id = std::this_thread::get_id();\n\t\tstd::stringstream ss;\n\t\tss << thread_id;\n\t\tstd::string thread_id_str = ss.str();\n\n\t\tif (thread_id_str != \"1\")\n\t\t{\n\t\t\tstr += \"[thread:\" + thread_id_str + \"] \";\n\t\t}\n#endif\n\t\tstr += format;\n#ifdef LOG_PRINT_LOC\n\t\tstr += \"\t\"; // add tab to make it easier to read.\n\t\tauto found = file.find_last_of('/\\\\');\n\t\tauto file_name = file.substr(found + 1); //remove path from file name.\n\t\tstr += \"[\" + file + \":\" + func + \":\" + std::to_string(line) + \"] \";\n#endif\n\t\tstr += \"\\n\";\n\n#if defined(LOG_PRINT_COLORS) && defined(_WIN32)\n\t\tif (color != 0)\n\t\t{\n\t\t\tauto console = GetStdHandle(STD_OUTPUT_HANDLE);\n\t\t\tSetConsoleTextAttribute(console, color);\n\t\t}\n#endif\n\n#ifdef WISPRENDERER_LOG_TO_STDOUT\n\t\tfmt::print(stdout, str, args...);\n#endif\n\t\t#if defined(LOG_PRINT_TO_OUTPUT) && defined(_WIN32)\n\t\t\tOutputDebugStringA(str.c_str());\n\t\t#endif\n\t\tif (log_file_handler != nullptr)\n\t\t{\n\t\t\tfmt::print(log_file_handler->GetFilePtr(), str, args...);\n\t\t\tfflush(log_file_handler->GetFilePtr());\n\t\t}\n#if defined(LOG_PRINT_COLORS) && defined(_WIN32)\n\t\tif (color != 0)\n\t\t{\n\t\t\tauto console = GetStdHandle(STD_OUTPUT_HANDLE);\n\t\t\tSetConsoleTextAttribute(console, 7);\n\t\t}\n#endif\n\n#ifdef LOG_CALLBACK\n\t\tauto f_str = fmt::format(str, args...);\n\t\tif (log_callback::impl != nullptr) log_callback::impl(f_str);\n#endif\n\t}\n\n\ttemplate <typename S, typename... Args>\n\tinline void log_msgb_impl(MSGB_ICON icon, std::string file, std::string func, int line, S const & format, Args const &... args)\n\t{\n\t\tstd::string str = \"\";\n\n#ifdef LOG_PRINT_TIME\n\t\tstd::tm s;\n\t\tstd::time_t t = std::time(nullptr);\n\t\tlocaltime_s(&s, &t);\n\n\t\tstr += fmt::format(\"[{:%H:%M}]\\n\", s);\n#endif\n#ifdef LOG_PRINT_THREAD\n\t\tauto thread_id = std::this_thread::get_id();\n\t\tstd::stringstream ss;\n\t\tss << thread_id;\n\t\tstd::string thread_id_str = ss.str();\n\n\t\tif (thread_id_str != \"1\")\n\t\t{\n\t\t\tstr += \"[thread:\" + thread_id_str + \"]\\n\";\n\t\t}\n#endif\n#ifdef LOG_PRINT_LOC\n\t\tstr += \"[\" + file + \":\" + func + \":\" + std::to_string(line) + \"] \";\n#endif\n\t\tstr += format;\n\t\tstr += \"\\n\";\n\n\t\tswitch(icon)\n\t\t{\n\t\tcase MSGB_ICON::CRITICAL_ERROR:\n\t\t\tMessageBox(0, str.c_str(), \"Critical Error\", (int)icon);\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tMessageBox(0, str.c_str(), \"Unkown Error\", MB_OK | MB_ICONQUESTION);\n\t\t\tbreak;\n\t\t}\n\t}\n} /* internal */\n\n#pragma warning( pop )\n\n#define LOG(csr, ...)  { util::internal::log_impl(7, 'I', __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); }\n#define LOGW(csr, ...) { util::internal::log_impl(6, 'W', __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); }\n#define LOGE(csr, ...) { util::internal::log_impl(4, 'E', __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); }\n#define LOGC(csr, ...) { util::internal::log_impl(71, 'C', __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); LOG_BREAK }\n//#define LOGC(csr, ...) { util::internal::log_msgb_impl(util::internal::MSGB_ICON::CRITICAL_ERROR, __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); }\n"
  },
  {
    "path": "src/util/logfile_handler.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"logfile_handler.hpp\"\n\n#include <sstream>\n\nwr::LogfileHandler::LogfileHandler()\n{\n\tstd::filesystem::path path(\"./logs/\");\n\tif (!std::filesystem::exists(path))\n\t{\n\t\tstd::filesystem::create_directory(path);\n\t}\n\tstd::stringstream ss;\n\tss << \"log-default\";\n\tpath /= ss.str();\n\tstd::filesystem::create_directory(path);\n\tpath /= \"default-wisp.log\";\n\tm_file = fopen(path.string().c_str(), \"w\");\n}\n\nwr::LogfileHandler::LogfileHandler(std::filesystem::path& dir_path, std::string& file_name)\n{\n\tstd::filesystem::path path(\"./logs/\");\n\tif (!std::filesystem::exists(path))\n\t{\n\t\tstd::filesystem::create_directory(path);\n\t}\n\tpath /= dir_path;\n\tstd::filesystem::create_directory(path);\n\tpath /= file_name;\n\tm_file = fopen(path.string().c_str(), \"w\");\n}\n\nstd::FILE* wr::LogfileHandler::GetFilePtr()\n{\n\treturn m_file;\n}\n\nwr::LogfileHandler::~LogfileHandler()\n{\n\tfflush(m_file);\n\tfclose(m_file);\n}\n\nconst std::filesystem::path& wr::LogfileHandler::GetDirPath()\n{\n\treturn dir_path;\n}\n"
  },
  {
    "path": "src/util/logfile_handler.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include <filesystem>\n\nnamespace wr\n{\n\tclass LogfileHandler {\n\tpublic:\n\t\tLogfileHandler();\n\t\tLogfileHandler(std::filesystem::path& dir_path, std::string& file_name);\n\t\t~LogfileHandler();\n\n\t\tstd::FILE* GetFilePtr();\n\t\tconst std::filesystem::path& GetDirPath();\n\n\tprivate:\n\t\tstd::FILE* m_file = nullptr;\n\t\tstd::filesystem::path dir_path;\n\t};\n}\n"
  },
  {
    "path": "src/util/named_type.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\nnamespace util\n{\n\n\t//!  Named Type\n\t/*!\n\t  This is a naive named type implementation.\n\t  Its only use is for member variables.\n\t  For a more detailed implementation see Jonathan Boccara's general purpose implementation\n\t*/\n\ttemplate <typename T>\n\tclass NamedType\n\t{\n\tpublic:\n\t\texplicit constexpr NamedType(T const& value) : m_value(value)\n\t\t{ }\n\n\t\tconstexpr T& Get()\n\t\t{\n\t\t\treturn m_value;\n\t\t}\n\t\tconstexpr T const& Get() const\n\t\t{\n\t\t\treturn m_value;\n\t\t}\n\n\t\toperator T&()\n\t\t{\n\t\t\treturn m_value;\n\t\t}\n\n\tprivate:\n\t\tT m_value;\n\t};\n\n} /* util */"
  },
  {
    "path": "src/util/pair_hash.hpp",
    "content": "/*!\r\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\r\n *\r\n * Licensed under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n *     http://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an \"AS IS\" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n */\r\n#pragma once\r\n\r\nnamespace util\r\n{\r\n\r\n\tstruct PairHash {\r\n\t\ttemplate <class T1, class T2>\r\n\t\tstd::size_t operator() (const std::pair<T1, T2> &pair) const\r\n\t\t{\r\n\t\t\tstd::uint64_t hash = 0;\r\n\t\t\tfor(int i=0;i<pair.second.size();i++) {\r\n\t\t\t\thash += pair.second[i].m_id + (std::intptr_t)pair.second[i].m_pool; // Can be anything\r\n\t\t\t}\r\n\r\n\t\t\treturn std::hash<T1>()(pair.first) ^ hash;\r\n\t\t}\r\n\t};\r\n\r\n} /* util */\r\n"
  },
  {
    "path": "src/util/strings.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n#include <string_view>\n\nnamespace util\n{\n\tinline std::optional<std::string_view> GetFileExtension(std::string_view path)\n\t{\n\t\tstd::size_t pos = path.find_last_of(\".\");\n\n\t\tif (pos != std::string_view::npos)\n\t\t{\n\t\t\tstd::size_t length = path.length();\n\t\t\tstd::string_view extension = path.substr(pos, length);\n\n\t\t\treturn extension;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn {};\n\t\t}\n\t}\n\n\tinline bool MatchFileExtension(std::string_view path, std::string_view extension)\n\t{\n\t\tstd::size_t dot_position = path.find_last_of(\".\");\n\n\t\tif (dot_position == std::string_view::npos)\n\t\t\treturn false;\n\n\t\tstd::size_t last_occurrence_pos = path.rfind(extension);\n\t\t\n\t\tif (last_occurrence_pos == std::string_view::npos)\n\t\t\treturn false;\n\n\t\treturn last_occurrence_pos >= dot_position;\n\t}\n}"
  },
  {
    "path": "src/util/thread_pool.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n/*\nThis thread pool is a modified version of https://github.com/progschj/ThreadPool.\nIt is adjusted to fit our project structure better.\n\nOriginal licesne:\nCopyright (c) 2012 Jakob Progsch, Václav Zeman\n\nThis software is provided 'as-is', without any express or implied\nwarranty. In no event will the authors be held liable for any damages\narising from the use of this software.\n\nPermission is granted to anyone to use this software for any purpose,\nincluding commercial applications, and to alter it and redistribute it\nfreely, subject to the following restrictions:\n\n   1. The origin of this software must not be misrepresented; you must not\n   claim that you wrote the original software. If you use this software\n   in a product, an acknowledgment in the product documentation would be\n   appreciated but is not required.\n\n   2. Altered source versions must be plainly marked as such, and must not be\n   misrepresented as being the original software.\n\n   3. This notice may not be removed or altered from any source\n   distribution.\n*/\n\n#pragma once\n\n#include <vector>\n#include <queue>\n#include <memory>\n#include <thread>\n#include <mutex>\n#include <condition_variable>\n#include <future>\n#include <functional>\n#include <stdexcept>\n\n#include \"delegate.hpp\"\n\nnamespace util\n{\n\n\tclass ThreadPool\n\t{\n\tpublic:\n\t\tThreadPool(size_t);\n\t\ttemplate<class F, class... Args>\n\t\tdecltype(auto) Enqueue(F&& f, Args&&... args);\n\t\t~ThreadPool();\n\n\tprivate:\n\t\t// need to keep track of threads so we can join them\n\t\tstd::vector<std::thread> m_workers;\n\t\t// the task queue\n\t\tstd::queue<Delegate<void()>> m_tasks;\n\n\t\t// synchronization\n\t\tstd::mutex m_queue_mutex;\n\t\tstd::condition_variable m_condition;\n\t\tbool m_stop;\n\t};\n\n\t// the constructor just launches some amount of workers\n\tinline ThreadPool::ThreadPool(std::size_t threads)\n\t\t: m_stop(false)\n\t{\n\t\tfor (decltype(threads) i = 0; i < threads; ++i)\n\t\t\tm_workers.emplace_back(\n\t\t\t\t[this]\n\t\t{\n\t\t\tfor (;;)\n\t\t\t{\n\t\t\t\tDelegate<void()> task;\n\n\t\t\t\t{\n\t\t\t\t\tstd::unique_lock<std::mutex> lock(m_queue_mutex);\n\n\t\t\t\t\tm_condition.wait(lock,\n\t\t\t\t\t\t[this] { return m_stop || !m_tasks.empty(); });\n\t\t\t\t\tif (m_stop && m_tasks.empty())\n\t\t\t\t\t\treturn;\n\t\t\t\t\ttask = std::move(m_tasks.front());\n\t\t\t\t\tm_tasks.pop();\n\t\t\t\t}\n\n\t\t\t\ttask();\n\t\t\t}\n\t\t}\n\t\t);\n\t}\n\n\t// add new work item to the pool\n\ttemplate<class F, class... Args>\n\tdecltype(auto) ThreadPool::Enqueue(F&& f, Args&&... args)\n\t{\n\t\tusing return_type = typename std::result_of<F(Args...)>::type;\n\n\t\tauto task = std::make_shared< std::packaged_task<return_type()> >(\n\t\t\tstd::bind(std::forward<F>(f), std::forward<Args>(args)...)\n\t\t\t);\n\n\t\tstd::future<return_type> res = task->get_future();\n\t\t{\n\t\t\tstd::unique_lock<std::mutex> lock(m_queue_mutex);\n\n\t\t\t// don't allow enqueueing after stopping the pool\n\t\t\tif (m_stop)\n\t\t\t\tthrow std::runtime_error(\"enqueue on stopped ThreadPool\");\n\n\t\t\tm_tasks.emplace([task]() { (*task)(); });\n\t\t}\n\t\tm_condition.notify_one();\n\t\treturn res;\n\t}\n\n\t// the destructor joins all threads\n\tinline ThreadPool::~ThreadPool()\n\t{\n\t\t{\n\t\t\tstd::unique_lock<std::mutex> lock(m_queue_mutex);\n\t\t\tm_stop = true;\n\t\t}\n\n\t\tm_condition.notify_all();\n\t\tfor (std::thread& worker : m_workers)\n\t\t{\n\t\t\tworker.join();\n\t\t}\n\t}\n\n}"
  },
  {
    "path": "src/util/user_literals.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\nconstexpr float operator\"\" _deg(long double deg)\n{\n\treturn DirectX::XMConvertToRadians(static_cast<float>(deg));\n}\n\nconstexpr float operator\"\" _rad(long double rad)\n{\n\treturn DirectX::XMConvertToDegrees(static_cast<float>(rad));\n}\n\nconstexpr float operator\"\" _deg(unsigned long long int deg)\n{\n\treturn DirectX::XMConvertToRadians(static_cast<float>(deg));\n}\n\nconstexpr float operator\"\" _rad(unsigned long long int rad)\n{\n\treturn DirectX::XMConvertToDegrees(static_cast<float>(rad));\n}\n\nconstexpr std::size_t operator\"\" _kb(std::size_t kilobytes)\n{\n\treturn kilobytes * 1024;\n}\n\nconstexpr std::size_t operator\"\" _mb(std::size_t megabytes)\n{\n\treturn megabytes * 1024 * 1024;\n}"
  },
  {
    "path": "src/version.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n/*!\nA constexpr way to obtain program version information.\nCopyright © 2019, Team Wisp\n\nPermission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n*/\n\n#define VERSION_FILE \"../wisp.version\"\n\n#include <cctype>\n#include <cstring>\n#include <cstdint>\n#include <cstdio>\n#include <stdexcept>\n\nnamespace wr\n{\n\n\tstruct Version\n\t{\n\t\tstd::uint32_t m_major = 0, m_minor = 0, m_patch = 0;\n\t};\n\n\tnamespace std_cexpr\n\t{\n\t\tconstexpr size_t strlen(const char* str)\n\t\t{\n\t\t\treturn (*str == 0) ? 0 : strlen(str + 1) + 1;\n\t\t}\n\n\t\tconstexpr bool isdigit(char c)\n\t\t{\n\t\t\treturn c <= '9' && c >= '0';\n\t\t}\n\n\t\tconstexpr std::uint32_t stoi_impl(const char* str, std::uint32_t max, std::uint32_t n = 0, std::uint32_t value = 0)\n\t\t{\n\t\t\tif (n >= max)\n\t\t\t\treturn value;\n\n\t\t\treturn *str ?\n\t\t\t\tisdigit(*str) ?\n\t\t\t\tstoi_impl(str + 1, max, ++n, (*str - '0') + value * 10)\n\t\t\t\t: value\n\t\t\t\t: value;\n\t\t}\n\n\t\tconstexpr std::uint32_t stoi(const char* str, std::uint32_t max) {\n\t\t\treturn stoi_impl(str, max);\n\t\t}\n\t}\n\n\tconstexpr Version GetVersion()\n\t{\n\t\tconst char* str = static_cast<const char*>(\n\t\t\t#include VERSION_FILE\n\t\t);\n\n\t\tchar major_buf[128] = \"\";\n\t\tchar minor_buf[128] = \"\";\n\t\tchar patch_buf[128] = \"\";\n\n\t\tint major_buf_size = 0;\n\t\tint minor_buf_size = 0;\n\t\tint patch_buf_size = 0;\n\n\t\tstd::uint32_t num = 0;\n\t\tstd::uint32_t itt = 0;\n\t\tfor (std::uint32_t i = 0; i < std_cexpr::strlen(str); i++)\n\t\t{\n\t\t\tif (str[i] == '.')\n\t\t\t{\n\t\t\t\tnum++;\n\t\t\t\titt = 0;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\telse if (num == 0)\n\t\t\t{\n\t\t\t\tmajor_buf[itt] = str[i];\n\t\t\t\tmajor_buf_size++;\n\t\t\t}\n\t\t\telse if (num == 1)\n\t\t\t{\n\t\t\t\tminor_buf[itt] = str[i];\n\t\t\t\tminor_buf_size++;\n\t\t\t}\n\t\t\telse if (num == 2) {\n\t\t\t\tpatch_buf[itt] = str[i];\n\t\t\t\tpatch_buf_size++;\n\t\t\t}\n\n\t\t\titt++;\n\t\t}\n\n\t\tif (num != 2)\n\t\t{\n\t\t\tthrow \"compile-time-error: Incorrect length\";\n\t\t}\n\n\t\treturn { std_cexpr::stoi(major_buf, major_buf_size), std_cexpr::stoi(minor_buf, minor_buf_size), std_cexpr::stoi(patch_buf, patch_buf_size) };\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/vertex.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <d3d12.h>\n#include <vector>\n\n#include \"util/defines.hpp\"\n\nnamespace wr\n{\n\n\t//! 2D vertex (in clip space)\n\tstruct Vertex2D \n\t{\n\t\tfloat m_pos[2];\n\n\t\tstatic std::vector<D3D12_INPUT_ELEMENT_DESC> GetInputLayout()\n\t\t{\n\t\t\tstd::vector<D3D12_INPUT_ELEMENT_DESC> layout = {\n\t\t\t\t{ \"POSITION\", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(Vertex2D, m_pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }\n\t\t\t};\n\n\t\t\treturn layout;\n\t\t}\n\n\t};\n\n\t//! Default Vertex\n\tstruct Vertex\n\t{\n\t\tfloat m_pos[3];\n\t\tfloat m_uv[2];\n\t\tfloat m_normal[3];\n\t\tfloat m_tangent[3];\n\t\tfloat m_bitangent[3];\n\n\t\tstatic std::vector<D3D12_INPUT_ELEMENT_DESC> GetInputLayout()\n\t\t{\n\t\t\tstd::vector<D3D12_INPUT_ELEMENT_DESC> layout = {\n\t\t\t\t{ \"POSITION\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n\t\t\t\t{ \"TEXCOORD\", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(Vertex, m_uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n\t\t\t\t{ \"NORMAL\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_normal), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n\t\t\t\t{ \"TANGENT\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_tangent), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},\n\t\t\t\t{ \"BITANGENT\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_bitangent), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}\n\t\t\t};\n\n\t\t\treturn layout;\n\t\t}\n\t};\n\n\t//! Default Vertex\n\tstruct VertexColor\n\t{\n\t\tfloat m_pos[3];\n\t\tfloat m_uv[2];\n\t\tfloat m_normal[3];\n\t\tfloat m_tangent[3];\n\t\tfloat m_bitangent[3];\n\t\tfloat m_color[3];\n\n\t\tstatic std::vector<D3D12_INPUT_ELEMENT_DESC> GetInputLayout()\n\t\t{\n\t\t\tstd::vector<D3D12_INPUT_ELEMENT_DESC> layout = {\n\t\t\t\t{ \"POSITION\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n\t\t\t\t{ \"TEXCOORD\", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(VertexColor, m_uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n\t\t\t\t{ \"NORMAL\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_normal), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n\t\t\t\t{ \"TANGENT\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_tangent), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},\n\t\t\t\t{ \"BITANGENT\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_bitangent), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},\n\t\t\t\t{ \"COLOR\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_color), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}\n\t\t\t};\n\n\t\t\treturn layout;\n\t\t}\n\t};\n\n\t//! Default Vertex\n\tstruct VertexNoTangent\n\t{\n\t\tfloat m_pos[3];\n\t\tfloat m_uv[2];\n\t\tfloat m_normal[3];\n\n\t\tstatic std::vector<D3D12_INPUT_ELEMENT_DESC> GetInputLayout()\n\t\t{\n\t\t\tstd::vector<D3D12_INPUT_ELEMENT_DESC> layout = {\n\t\t\t\t{ \"POSITION\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n\t\t\t\t{ \"TEXCOORD\", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(Vertex, m_uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },\n\t\t\t\t{ \"NORMAL\", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_normal), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }\n\t\t\t};\n\n\t\t\treturn layout;\n\t\t}\n\t};\n\n\tIS_PROPER_VERTEX_CLASS(Vertex)\n\tIS_PROPER_VERTEX_CLASS(VertexNoTangent)\n\tIS_PROPER_VERTEX_CLASS(Vertex2D)\n\n} /* wr */"
  },
  {
    "path": "src/window.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"window.hpp\"\n\n#include \"util/log.hpp\"\n\n#include \"imgui/imgui.hpp\"\n#include \"imgui/imgui_impl_win32.hpp\"\n\n#include <algorithm>\n\nnamespace wr\n{\n\n\n\tWindow::Window(HINSTANCE instance, int show_cmd, std::string const & name, std::uint32_t width, std::uint32_t height)\n\t\t: m_title(name), m_instance(instance)\n\t{\n\t\tWNDCLASSEX wc;\n\t\twc.cbSize = sizeof(WNDCLASSEX);\n\t\twc.style = CS_HREDRAW | CS_VREDRAW;\n\t\twc.lpfnWndProc = &Window::WindowProc;\n\t\twc.cbClsExtra = NULL;\n\t\twc.cbWndExtra = NULL;\n\t\twc.hInstance = instance;\n\t\twc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);\n\t\twc.hCursor = LoadCursor(nullptr, IDC_ARROW);\n\t\twc.hbrBackground = (HBRUSH)(COLOR_WINDOW);\n\t\twc.lpszMenuName = nullptr;\n\t\twc.lpszClassName = name.c_str();\n\t\twc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);\n\n\t\tm_window_width = width;\n\t\tm_window_height = height;\n\n\t\tif (!RegisterClassEx(&wc))\n\t\t{\n\t\t\tLOGC(\"Failed to register extended window class: \");\n\t\t}\n\n\t\tauto window_style = WS_OVERLAPPEDWINDOW;\n\n\t\tif (/*!allow_resizing*/ false)\n\t\t{\n\t\t\twindow_style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);\n\t\t}\n\n\t\tRECT client_rect;\n\t\tclient_rect.left = 0;\n\t\tclient_rect.right = width;\n\t\tclient_rect.top = 0;\n\t\tclient_rect.bottom = height;\n\t\tAdjustWindowRectEx(&client_rect, window_style, FALSE, wc.style);\n\n\t\tm_handle = CreateWindowEx(\n\t\t\twc.style,\n\t\t\tname.c_str(),\n\t\t\tname.c_str(),\n\t\t\twindow_style,\n\t\t\tCW_USEDEFAULT, CW_USEDEFAULT,\n\t\t\tclient_rect.right - client_rect.left, client_rect.bottom - client_rect.top,\n\t\t\tnullptr,\n\t\t\tnullptr,\n\t\t\tinstance,\n\t\t\tnullptr\n\t\t);\n\n\t\tif (!m_handle)\n\t\t{\n\t\t\tLOGC(\"Failed to create window.\" + GetLastError())\n\t\t}\n\n\t\tSetWindowLongPtr(m_handle, GWLP_USERDATA, (LONG_PTR)this);\n\n\t\tShowWindow(m_handle, show_cmd);\n\t\tUpdateWindow(m_handle);\n\n\t\tm_running = true;\n\t}\n\n\tWindow::Window(HINSTANCE instance, std::string const& name, std::uint32_t width, std::uint32_t height, bool show)\n\t\t: Window(instance, show ? SW_SHOWNORMAL : SW_HIDE, name, width, height)\n\t{\n\n\t}\n\n\tWindow::~Window()\n\t{\n\t\tStop();\n\t\tUnregisterClassA(m_title.c_str(), m_instance);\n\t}\n\n\tvoid Window::PollEvents()\n\t{\n\t\tMSG msg;\n\t\tif (PeekMessage(&msg, m_handle, 0, 0, PM_REMOVE))\n\t\t{\n\t\t\tif (msg.message == WM_QUIT)\n\t\t\t\tm_running = false;\n\n\t\t\tTranslateMessage(&msg);\n\t\t\tDispatchMessage(&msg);\n\t\t}\n\t}\n\n\tvoid Window::Show()\n\t{\n\t\tShowWindow(m_handle, SW_SHOW);\n\t}\n\n\tvoid Window::Stop()\n\t{\n\t\tDestroyWindow(m_handle);\n\t}\n\n\tvoid Window::SetRenderLoop(std::function<void()> render_func)\n\t{\n\t\tm_render_func = std::move(render_func);\n\t}\n\n\tvoid Window::StartRenderLoop()\n\t{\n\t\twhile (IsRunning())\n\t\t{\n\t\t\tPollEvents();\n\t\t}\n\n\t\tUnregisterClassA(m_title.c_str(), m_instance);\n\t}\n\n\tvoid Window::SetKeyCallback(KeyCallback callback)\n\t{\n\t\tm_key_callback = std::move(callback);\n\t}\n\n\tvoid Window::SetMouseCallback(MouseCallback callback)\n\t{\n\t\tm_mouse_callback = std::move(callback);\n\t}\n\n\tvoid Window::SetMouseWheelCallback(MouseWheelCallback callback)\n\t{\n\t\tm_mouse_wheel_callback = std::move(callback);\n\t}\n\n\tvoid Window::SetResizeCallback(ResizeCallback callback)\n\t{\n\t\tm_resize_callback = std::move(callback);\n\t}\n\n\tbool Window::IsRunning() const\n\t{\n\t\treturn m_running;\n\t}\n\n\tstd::int32_t Window::GetWidth() const\n\t{\n\t\tRECT r;\n\t\tGetClientRect(m_handle, &r);\n\t\treturn static_cast<std::int32_t>(r.right - r.left);\n\t}\n\n\tstd::int32_t Window::GetHeight() const\n\t{\n\t\tRECT r;\n\t\tGetClientRect(m_handle, &r);\n\t\treturn static_cast<std::int32_t>(r.bottom - r.top);\n\t}\n\n\tstd::string Window::GetTitle() const\n\t{\n\t\treturn m_title;\n\t}\n\n\tHWND Window::GetWindowHandle() const\n\t{\n\t\treturn m_handle;\n\t}\n\n\tbool Window::IsFullscreen() const\n\t{\n\t\tRECT a, b;\n\t\tGetWindowRect(m_handle, &a);\n\t\tGetWindowRect(GetDesktopWindow(), &b);\n\t\treturn (a.left == b.left  &&\n\t\t\ta.top == b.top   &&\n\t\t\ta.right == b.right &&\n\t\t\ta.bottom == b.bottom);\n\t}\n\n\tLRESULT CALLBACK Window::WindowProc(HWND handle, UINT msg, WPARAM w_param, LPARAM l_param)\n\t{\n\t\textern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);\n\t\tif (ImGui_ImplWin32_WndProcHandler(handle, msg, w_param, l_param))\n\t\t\treturn true;\n\n\t\tauto window = (Window*)GetWindowLongPtr(handle, GWLP_USERDATA);\n\t\tif (window) return window->WindowProc_Impl(handle, msg, w_param, l_param);\n\n\t\treturn DefWindowProc(handle, msg, w_param, l_param);\n\t}\n\n\tLRESULT CALLBACK Window::WindowProc_Impl(HWND handle, UINT msg, WPARAM w_param, LPARAM l_param)\n\t{\n\t\tswitch (msg)\n\t\t{\n\t\tcase WM_PAINT:\n\t\t\tif (m_render_func)\n\t\t\t{\n\t\t\t\tm_render_func();\n\t\t\t}\n\t\t\treturn 0;\n\t\tcase WM_DESTROY:\n\t\t\tm_running = false;\n\t\t\treturn 0;\n\t\tcase WM_LBUTTONDOWN:\n\t\tcase WM_LBUTTONUP:\n\t\tcase WM_RBUTTONDOWN:\n\t\tcase WM_RBUTTONUP:\n\t\t\tif (m_mouse_callback)\n\t\t\t{\n\t\t\t\tm_mouse_callback((int)w_param, msg, (int)l_param);\n\t\t\t}\n\t\t\treturn 0;\n\t\tcase WM_KEYDOWN:\n\t\tcase WM_KEYUP:\n\t\t\tif (m_key_callback)\n\t\t\t{\n\t\t\t\tm_key_callback((int)w_param, msg, (int)l_param);\n\t\t\t}\n\n\t\t\treturn 0;\n\n\t\tcase WM_MOUSEWHEEL:\n\t\t\tif (m_mouse_wheel_callback)\n\t\t\t{\n\t\t\t\tm_mouse_wheel_callback((int)w_param, msg, (int)l_param);\n\t\t\t}\n\n\t\t\treturn 0;\n\n\t\tcase WM_SIZE:\n\t\t\tif (w_param != SIZE_MINIMIZED)\n\t\t\t{\n\t\t\t\tif (RECT rect; m_resize_callback && GetClientRect(handle, &rect))\n\t\t\t\t{\n\t\t\t\t\tint width = rect.right - rect.left;\n\t\t\t\t\tint height = rect.bottom - rect.top;\n\n\t\t\t\t\tbool has_changed = width != m_window_width || height != m_window_height;\n\t\t\t\t\tbool is_valid_size = width > 16 && height > 16;\n\n\t\t\t\t\tif (has_changed && is_valid_size)\n\t\t\t\t\t{\n\t\t\t\t\t\tm_resize_callback(static_cast<std::uint32_t>(width), static_cast<std::uint32_t>(height));\n\n\t\t\t\t\t\tm_window_width = width;\n\t\t\t\t\t\tm_window_height = height;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn 0;\n\t\t}\n\n\t\treturn DefWindowProc(handle, msg, w_param, l_param);\n\t}\n\n} /* wr */\n"
  },
  {
    "path": "src/window.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <Windows.h>\n#include <functional>\n\nnamespace wr\n{\n\n\tclass Window\n\t{\n\t\tusing KeyCallback = std::function<void(int key, int action, int mods)>;\n\t\tusing MouseCallback = std::function<void(int key, int action, int mods)>;\n\t\tusing ResizeCallback = std::function<void(std::uint32_t width, std::uint32_t height)>;\n\t\tusing MouseWheelCallback = std::function<void(int key, int action, int mods)>;\n\tpublic:\n\t\t/*!\n\t\t* @param instance A handle to the current instance of the application.\n\t\t* @param name Window title.\n\t\t* @param width Initial window width.\n\t\t* @param height Initial window height.\n\t\t* @param show Controls whether the window will be shown. Default is true.\n\t\t*/\n\t\tWindow(HINSTANCE instance, std::string const& name, std::uint32_t width, std::uint32_t height, bool show = true);\n\t\tWindow(HINSTANCE instance, int show_cmd, std::string const& name, std::uint32_t width, std::uint32_t height);\n\t\t~Window();\n\n\t\tWindow(const Window&) = delete;\n\t\tWindow& operator=(const Window&) = delete;\n\t\tWindow(Window&&) = delete;\n\t\tWindow& operator=(Window&&) = delete;\n\n\t\t/*! Handles window events. Should be called every frame */\n\t\tvoid PollEvents();\n\t\t/*! Shows the window if it was hidden */\n\t\tvoid Show();\n\t\t/*! Requests to close the window */\n\t\tvoid Stop();\n\n\t\t/*! Give the window a function to call on repaint */\n\t\tvoid SetRenderLoop(std::function<void()> render_func);\n\t\t/*! Start a loop that runs until the window is closed. */\n\t\tvoid StartRenderLoop();\n\n\t\t/*! Used to set the key callback function */\n\t\tvoid SetKeyCallback(KeyCallback callback);\n\t\t/*! Used to set the mouse callback function */\n\t\tvoid SetMouseCallback(MouseCallback callback);\n\t\t/*! Used to set the mouse wheel callback function */\n\t\tvoid SetMouseWheelCallback(MouseWheelCallback callback);\n\t\t/*! Used to set the resize callback function */\n\t\tvoid SetResizeCallback(ResizeCallback callback);\n\n\t\t/*! Returns whether the application is running. (used for the main loop) */\n\t\tbool IsRunning() const;\n\t\t/* Returns the client width */\n\t\tstd::int32_t GetWidth() const;\n\t\t/* Returns the client height */\n\t\tstd::int32_t GetHeight() const;\n\t\t/* Returns the title of the window. */\n\t\tstd::string GetTitle() const;\n\t\t/*! Returns the native window handle (HWND)*/\n\t\tHWND GetWindowHandle() const;\n\t\t/*! Checks whether the window is fullscreen */\n\t\tbool IsFullscreen() const;\n\n\tprivate:\n\t\t/*! WindowProc that calls `WindowProc_Impl` */\n\t\tstatic LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);\n\t\t/*! Main WindowProc function */\n\t\tLRESULT CALLBACK WindowProc_Impl(HWND, UINT, WPARAM, LPARAM);\n\n\t\tKeyCallback m_key_callback;\n\t\tMouseCallback m_mouse_callback;\n\t\tResizeCallback m_resize_callback;\n\t\tMouseWheelCallback m_mouse_wheel_callback;\n\n\t\tstd::function<void()> m_render_func;\n\n\t\tstd::string m_title;\n\n\t\tbool m_running;\n\t\tHWND m_handle;\n\t\tHINSTANCE m_instance;\n\n\t\tstd::int32_t m_window_width = 0;\n\t\tstd::int32_t m_window_height = 0;\n\t};\n\n} /* wr */\n"
  },
  {
    "path": "src/wisp.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n// Core\n#include \"entry.hpp\"\n#include \"window.hpp\"\n#include \"vertex.hpp\"\n#include \"renderer.hpp\"\n\n// Scene Graph\n#include \"scene_graph/scene_graph.hpp\"\n#include \"scene_graph/mesh_node.hpp\"\n#include \"scene_graph/camera_node.hpp\"\n#include \"scene_graph/skybox_node.hpp\"\n\n// Frame Graph\n#include \"frame_graph/frame_graph.hpp\""
  },
  {
    "path": "src/wisprenderer_export.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n#ifndef WISPRENDERER_EXPORT_H\n#define WISPRENDERER_EXPORT_H\n\n#ifdef WISPRENDERER_STATIC_DEFINE\n#  define WISPRENDERER_EXPORT\n#  define WISPRENDERER_NO_EXPORT\n#else\n#  ifndef WISPRENDERER_EXPORT\n#    ifdef WispRenderer_EXPORTS\n        /* We are building this library */\n#      define WISPRENDERER_EXPORT __declspec(dllexport)\n#    else\n        /* We are using this library */\n#      define WISPRENDERER_EXPORT __declspec(dllimport)\n#    endif\n#  endif\n\n#  ifndef WISPRENDERER_NO_EXPORT\n#    define WISPRENDERER_NO_EXPORT \n#  endif\n#endif\n\n#ifndef WISPRENDERER_DEPRECATED\n#  define WISPRENDERER_DEPRECATED __declspec(deprecated)\n#endif\n\n#ifndef WISPRENDERER_DEPRECATED_EXPORT\n#  define WISPRENDERER_DEPRECATED_EXPORT WISPRENDERER_EXPORT WISPRENDERER_DEPRECATED\n#endif\n\n#ifndef WISPRENDERER_DEPRECATED_NO_EXPORT\n#  define WISPRENDERER_DEPRECATED_NO_EXPORT WISPRENDERER_NO_EXPORT WISPRENDERER_DEPRECATED\n#endif\n\n#if 0 /* DEFINE_NO_DEPRECATED */\n#  ifndef WISPRENDERER_NO_DEPRECATED\n#    define WISPRENDERER_NO_DEPRECATED\n#  endif\n#endif\n\n#endif /* WISPRENDERER_EXPORT_H */\n"
  },
  {
    "path": "tests/CMakeLists.txt",
    "content": "# Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nfile(GLOB COMMON_SOURCES \"common/*.cpp\")\nfile(GLOB COMMON_HEADERS \"common/*.hpp\")\n\nfunction(add_test TEST_DIR TEST_NAME)\n\tmessage(STATUS \"Configuring example ${TEST_NAME} in ${TEST_DIR}\")\n \n\t# source\n\tfile(GLOB SOURCES \"${TEST_DIR}/*.cpp\")\n\tfile(GLOB HEADERS \"${TEST_DIR}/*.hpp\")\n \n\tadd_executable(${TEST_NAME} ${HEADERS} ${SOURCES} ${COMMON_HEADERS} ${COMMON_SOURCES})\n\ttarget_include_directories(${TEST_NAME} PUBLIC ../src/)\n\ttarget_link_libraries(${TEST_NAME} WispRenderer BulletCollision BulletDynamics LinearMath)\n\tset_target_properties(${TEST_NAME} PROPERTIES CXX_STANDARD 20)\n\tset_target_properties(${TEST_NAME} PROPERTIES CXX_EXTENSIONS OFF)\n\tset_target_properties(${TEST_NAME} PROPERTIES CMAKE_CXX_STANDARD_REQUIRED ON)\n\tset_target_properties(${TEST_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY \"${CMAKE_BINARY_DIR}/../\")\nendfunction(add_test)\n\nfunction(add_example EXAMPLE_DIR EXAMPLE_NAME)\n\tmessage(STATUS \"Configuring example ${EXAMPLE_NAME} in ${EXAMPLE_DIR}\")\n \n\t# source\n\tfile(GLOB SOURCES \"${EXAMPLE_DIR}/*.cpp\")\n\tfile(GLOB HEADERS \"${EXAMPLE_DIR}/*.hpp\")\n \n\tadd_executable(${EXAMPLE_NAME} WIN32 ${HEADERS} ${SOURCES} ${COMMON_HEADERS} ${COMMON_SOURCES})\n\ttarget_include_directories(${EXAMPLE_NAME} PUBLIC ../src/)\n\ttarget_link_libraries(${EXAMPLE_NAME} WispRenderer BulletCollision BulletDynamics LinearMath)\n\tset_target_properties(${EXAMPLE_NAME} PROPERTIES CXX_STANDARD 20)\n\tset_target_properties(${EXAMPLE_NAME} PROPERTIES CXX_EXTENSIONS OFF)\n\tset_target_properties(${EXAMPLE_NAME} PROPERTIES CMAKE_CXX_STANDARD_REQUIRED ON)\n\tset_target_properties(${EXAMPLE_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY \"${CMAKE_BINARY_DIR}/../\")\nendfunction(add_example)\n\nadd_test(demo Demo)\nadd_test(graphics_benchmark GraphicsBenchmark)\n"
  },
  {
    "path": "tests/common/scene.cpp",
    "content": "/*!\r\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\r\n *\r\n * Licensed under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *\r\n *     http://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an \"AS IS\" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n */\r\n#include \"scene.hpp\"\r\n\r\n#include <DirectXMath.h>\r\n#include <fstream>\r\n\r\n#include \"json.hpp\"\r\n\r\nScene::Scene(std::size_t material_pool_size,\r\n\t\t\t std::size_t model_pool_vb_size,\r\n\t\t\t std::size_t model_pool_ib_size) :\r\n\tm_material_pool_size(material_pool_size),\r\n\tm_model_pool_vb_size(model_pool_vb_size),\r\n\tm_model_pool_ib_size(model_pool_ib_size)\r\n{\r\n\r\n}\r\n\r\nScene::~Scene()\r\n{\r\n\r\n}\r\n\r\nvoid Scene::Init(wr::D3D12RenderSystem* rs, unsigned int width, unsigned int height, void* extra)\r\n{\r\n\tm_texture_pool = rs->CreateTexturePool();\r\n\tm_material_pool = rs->CreateMaterialPool(m_material_pool_size);\r\n\tm_model_pool = rs->CreateModelPool(m_model_pool_vb_size, m_model_pool_ib_size);\r\n\r\n\tLoadResources();\r\n\r\n\tm_scene_graph = std::make_shared<wr::SceneGraph>(rs);\r\n\tBuildScene(width, height, extra);\r\n\trs->InitSceneGraph(*m_scene_graph.get());\r\n}\r\n\r\nstd::shared_ptr<wr::SceneGraph> Scene::GetSceneGraph()\r\n{\r\n\treturn m_scene_graph;\r\n}\r\n\r\nvoid Scene::LoadLightsFromJSON()\r\n{\r\n\tif (!m_lights_path.has_value())\r\n\t{\r\n\t\tLOGW(\"Tried to load lights from json without a path specified.\");\r\n\t\treturn;\r\n\t}\r\n\r\n\t// Read JSON file.\r\n\tstd::ifstream f(m_lights_path.value());\r\n\tnlohmann::json json;\r\n\tf >> json;\r\n\r\n\t// Loop over lights\r\n\tauto j_lights = json[\"lights\"];\r\n\r\n\tLOG(\"Number of lights: {}\", j_lights.size() );\r\n\r\n\tfor (std::size_t i = 0; i < j_lights.size(); i++)\r\n\t{\r\n\t\tauto j_light = j_lights[i];\r\n\r\n\r\n\t\tauto type = j_light[\"type\"].get<int>();\r\n\t\tauto color = j_light[\"color\"].get<std::vector<float>>();\r\n\t\tauto pos = j_light[\"pos\"].get<std::vector<float>>();\r\n\t\tauto rot = j_light[\"rot\"].get<std::vector<float>>();\r\n\t\tauto size = j_light[\"size\"].get<int>();\r\n\t\tauto radius = j_light[\"radius\"].get<int>();\r\n\t\tauto angle = j_light[\"angle\"].get<int>();\r\n\r\n\t\tauto light = m_scene_graph->CreateChild<wr::LightNode>(nullptr, (wr::LightType)type);\r\n\t\tlight->SetColor({ color[0], color[1], color[2] });\r\n\t\tlight->SetPosition({ pos[0], pos[1], pos[2] });\r\n\t\tlight->SetRotation({ rot[0], rot[1], rot[2] });\r\n\t\tlight->SetLightSize(size);\r\n\t\tlight->SetRadius(radius);\r\n\t\tlight->SetAngle(angle);\r\n\t}\r\n}\r\n\r\nvoid Scene::SaveLightsToJSON()\r\n{\r\n\tif (!m_lights_path.has_value())\r\n\t{\r\n\t\tLOGW(\"Tried to save lights to json without a path specified.\");\r\n\t\treturn;\r\n\t}\r\n\r\n\tnlohmann::json j_lights = nlohmann::json::array();\r\n\r\n\tauto lights = m_scene_graph->GetLightNodes();\r\n\r\n\tfor (auto& light : lights)\r\n\t{\r\n\t\tnlohmann::json j_light = nlohmann::json::object();\r\n\r\n\t\tj_light[\"type\"] = (int)light->GetType();\r\n\t\tj_light[\"color\"] = { light->m_light->col.x, light->m_light->col.y, light->m_light->col.z };\r\n\t\tj_light[\"pos\"] = light->m_position.m128_f32;\r\n\t\tj_light[\"rot\"] = light->m_rotation_radians.m128_f32;\r\n\t\tj_light[\"size\"] = light->m_light->light_size;\r\n\t\tj_light[\"radius\"] = light->m_light->rad;\r\n\t\tj_light[\"angle\"] = light->m_light->ang;\r\n\r\n\t\tj_lights.push_back(j_light);\r\n\t}\r\n\r\n\t// write JSON to  file\r\n\tnlohmann::json json;\r\n\tjson[\"lights\"] = j_lights;\r\n\r\n\tstd::ofstream o(m_lights_path.value());\r\n\to << std::setw(4) << json << std::endl;\r\n}"
  },
  {
    "path": "tests/common/scene.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <memory>\n\n#include \"wisp.hpp\"\n#include \"d3d12/d3d12_renderer.hpp\"\n#include \"util/user_literals.hpp\"\n\nclass Scene\n{\npublic:\n\tScene(std::size_t material_pool_size,\n\t\t  std::size_t model_pool_vb_size,\n\t\t  std::size_t model_pool_ib_size);\n\tvirtual ~Scene();\n\n\tvirtual void Init(wr::D3D12RenderSystem* rs, unsigned int width, unsigned int height, void* extra = nullptr);\n\tvirtual void Update(float delta = 0) = 0;\n\tstd::shared_ptr<wr::SceneGraph> GetSceneGraph();\n\ttemplate<typename T>\n\tstd::shared_ptr<T> GetCamera();\n\n\tvoid LoadLightsFromJSON();\n\tvoid SaveLightsToJSON();\n\nprotected:\n\tvirtual void LoadResources() = 0;\n\tvirtual void BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) = 0;\n\n\tconst std::size_t m_material_pool_size;\n\tconst std::size_t m_model_pool_vb_size;\n\tconst std::size_t m_model_pool_ib_size;\n\n\tstd::shared_ptr<wr::SceneGraph> m_scene_graph;\n\tstd::shared_ptr<wr::ModelPool> m_model_pool;\n\tstd::shared_ptr<wr::TexturePool> m_texture_pool;\n\tstd::shared_ptr<wr::MaterialPool> m_material_pool;\n\n\tstd::optional<std::string> m_lights_path;\n};\n\ntemplate<typename T>\nstd::shared_ptr<T> Scene::GetCamera()\n{\n\treturn std::static_pointer_cast<T>(m_scene_graph->GetActiveCamera());\n}\n"
  },
  {
    "path": "tests/demo/debug_camera.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <scene_graph/camera_node.hpp>\n#include <windef.h>\n#include <windowsx.h>\n\nclass DebugCamera : public wr::CameraNode\n{\npublic:\n\tDebugCamera(float fov, float aspect_ratio)\n\t\t: wr::CameraNode(aspect_ratio), m_forward_axis(0), m_right_axis(0), m_up_axis(0), m_rmb_down(false), m_speed(1), m_sensitivity(0.01f), m_position_lerp_speed(10.f), m_rotation_lerp_speed(5.f)\n\t{\n\t\tGetCursorPos(&m_last_cursor_pos);\n\t\tm_target_rotation_radians = m_rotation_radians;\n\t\tm_target_position = m_position;\n\t}\n\n\t//Takes roll, pitch and yaw and converts it to quaternion\n\tvoid SetRotation(DirectX::XMVECTOR roll_pitch_yaw) override\n\t{\n\t\tm_rotation_radians = roll_pitch_yaw;\n\t\tm_use_quaternion = false;\n\t\tm_target_rotation_radians = roll_pitch_yaw;\n\t}\n\n\t//Sets position\n\tvoid SetPosition(DirectX::XMVECTOR position) override\n\t{\n\t\tm_position = position;\n\t\tm_target_position = position;\n\t}\n\n\tvirtual void SetSpeed(float speed)\n\t{\n\t\tm_speed = speed;\n\t}\n\n\tvirtual void Update(float delta)\n\t{\n\t\tPOINT cursor_pos;\n\t\tGetCursorPos(&cursor_pos);\n\n\t\tif (m_rmb_down)\n\t\t{\n\t\t\t// Translation\n\t\t\tm_right_axis = std::min(m_right_axis, 1.f);\n\t\t\tm_right_axis = std::max(m_right_axis, -1.f);\n\n\t\t\tm_forward_axis = std::min(m_forward_axis, 1.f);\n\t\t\tm_forward_axis = std::max(m_forward_axis, -1.f);\n\n\t\t\tm_up_axis = std::min(m_up_axis, 1.f);\n\t\t\tm_up_axis = std::max(m_up_axis, -1.f);\n\n\t\t\tDirectX::XMVECTOR forward = DirectX::XMVector3Normalize(m_transform.r[2]);\n\t\t\tDirectX::XMVECTOR up = DirectX::XMVector3Normalize(m_transform.r[1]);\n\t\t\tDirectX::XMVECTOR right = DirectX::XMVector3Normalize(m_transform.r[0]);\n\n\t\t\tm_target_position = DirectX::XMVectorAdd(m_target_position, DirectX::XMVectorScale(forward, delta * m_speed * m_forward_axis));\n\t\t\tm_target_position = DirectX::XMVectorAdd(m_target_position, DirectX::XMVectorScale(up, delta * m_speed * m_up_axis));\n\t\t\tm_target_position = DirectX::XMVectorAdd(m_target_position, DirectX::XMVectorScale(right, delta * m_speed * m_right_axis));\n\n\t\t\t// Rotation\n\t\t\tDirectX::XMVECTOR new_rot{ static_cast<float>(cursor_pos.y - m_last_cursor_pos.y), static_cast<float>(cursor_pos.x - m_last_cursor_pos.x) };\n\t\t\tSetRotation(DirectX::XMVectorSubtract(m_target_rotation_radians, DirectX::XMVectorScale(new_rot, m_sensitivity)));\n\t\t}\n\t\telse\n\t\t{\n\t\t\tm_forward_axis = 0;\n\t\t\tm_right_axis = 0;\n\t\t\tm_up_axis = 0;\n\t\t}\n\n\t\tm_position = DirectX::XMVectorLerp(m_position, m_target_position, delta * m_position_lerp_speed);\n\t\tSetRotation(DirectX::XMVectorLerp(m_rotation_radians, m_target_rotation_radians, delta * m_rotation_lerp_speed));\n\t\tSignalTransformChange();\n\n\t\tm_last_cursor_pos = cursor_pos;\n\t}\n\n\tvoid MouseAction(int key, int action)\n\t{\n\t\tif (action == WM_RBUTTONDOWN)\n\t\t{\n\t\t\tm_rmb_down = true;\n\t\t}\n\t\telse if (action == WM_RBUTTONUP)\n\t\t{\n\t\t\tm_rmb_down = false;\n\t\t}\n\t}\n\n\tconst float m_scroll_speed = 0.25f;\n\n\tvoid MouseWheel(int amount)\n\t{\n\t\tif (m_rmb_down)\n\t\t{\n\t\t\tfloat percent = (float) GET_WHEEL_DELTA_WPARAM(amount) / WHEEL_DELTA * m_scroll_speed;\n\n\t\t\tif (percent + m_speed > 0)\n\t\t\t{\n\t\t\t\tm_speed += percent;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Due to the lack of a input manager I cheat input like this.\n\tvoid KeyAction(int key, int action)\n\t{\n\t\tif (action == WM_KEYDOWN)\n\t\t{\n\t\t\tif (key == 'W')\n\t\t\t{\n\t\t\t\tm_forward_axis += -1;\n\t\t\t}\n\t\t\tif (key == 'S')\n\t\t\t{\n\t\t\t\tm_forward_axis += 1;\n\t\t\t}\n\t\t\tif (key == 'A')\n\t\t\t{\n\t\t\t\tm_right_axis += -1;\n\t\t\t}\n\t\t\tif (key == 'D')\n\t\t\t{\n\t\t\t\tm_right_axis += 1;\n\t\t\t}\n\t\t\tif (key == VK_SPACE)\n\t\t\t{\n\t\t\t\tm_up_axis += 1;\n\t\t\t}\n\t\t\tif (key == VK_CONTROL)\n\t\t\t{\n\t\t\t\tm_up_axis += -1;\n\t\t\t}\n\t\t}\n\t\t\n\t\telse if (action == WM_KEYUP)\n\t\t{\n\t\t\tif (key == 'W')\n\t\t\t{\n\t\t\t\tm_forward_axis -= -1;\n\t\t\t}\n\t\t\tif (key == 'S')\n\t\t\t{\n\t\t\t\tm_forward_axis -= 1;\n\t\t\t}\n\t\t\tif (key == 'A')\n\t\t\t{\n\t\t\t\tm_right_axis -= -1;\n\t\t\t}\n\t\t\tif (key == 'D')\n\t\t\t{\n\t\t\t\tm_right_axis -= 1;\n\t\t\t}\n\t\t\tif (key == VK_SPACE)\n\t\t\t{\n\t\t\t\tm_up_axis -= 1;\n\t\t\t}\n\t\t\tif (key == VK_CONTROL)\n\t\t\t{\n\t\t\t\tm_up_axis -= -1;\n\t\t\t}\n\t\t}\n\t}\n\nprivate:\n\tfloat m_position_lerp_speed;\n\tfloat m_rotation_lerp_speed;\n\tDirectX::XMVECTOR m_target_rotation_radians;\n\tDirectX::XMVECTOR m_target_position;\n\tPOINT m_last_cursor_pos;\n\tbool m_rmb_down;\n\tfloat m_speed;\n\tfloat m_sensitivity;\n\tfloat m_forward_axis;\n\tfloat m_right_axis;\n\tfloat m_up_axis;\n};"
  },
  {
    "path": "tests/demo/demo.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include <memory>\n#include <algorithm>\n#include <thread>\n#include <chrono>\n#include <map>\n#include <string>\n#include <vector>\n#include <ctime>\n#include <iostream>\n\n#include \"wisp.hpp\"\n#include \"version.hpp\"\n#include \"demo_frame_graphs.hpp\"\n#include \"util/file_watcher.hpp\"\n\n//Crashpad includes\n#include \"client/crashpad_client.h\"\n#include \"client/settings.h\"\n#include \"client/crash_report_database.h\"\n\n#include \"engine_interface.hpp\"\n#include \"physics_engine.hpp\"\n#include \"scene_viknell.hpp\"\n#include \"scene_alien.hpp\"\n#include \"scene_emibl.hpp\"\n#include \"scene_sponza.hpp\"\n\n#include \"model_loader_assimp.hpp\"\n#include \"model_loader_tinygltf.hpp\"\n#include \"d3d12/d3d12_dynamic_descriptor_heap.hpp\"\n\nusing DefaultScene = ViknellScene;\n//#define ENABLE_PHYSICS\n\nstd::unique_ptr<wr::D3D12RenderSystem> render_system;\nScene* current_scene = nullptr;\nScene* new_scene = nullptr;\n\nvoid RenderEditor(ImTextureID output)\n{\n\tengine::RenderEngine(output, render_system.get(), current_scene, &new_scene);\n}\n\nvoid ShaderDirChangeDetected(std::string const & path, util::FileWatcher::FileStatus status)\n{\n\tauto& registry = wr::PipelineRegistry::Get();\n\tauto& rt_registry = wr::RTPipelineRegistry::Get();\n\n\tif (status == util::FileWatcher::FileStatus::MODIFIED)\n\t{\n\t\tLOG(\"Change detected in the shader directory. Reloading pipelines and shaders.\");\n\n\t\tfor (auto it : registry.m_objects)\n\t\t{\n\t\t\tregistry.RequestReload(it.first);\n\t\t}\n\n\t\tfor (auto it : rt_registry.m_objects)\n\t\t{\n\t\t\trt_registry.RequestReload(it.first);\n\t\t}\n\t}\n}\n\nint WispEntry()\n{\n\tconstexpr auto version = wr::GetVersion();\n\tLOG(\"Wisp Version {}.{}.{}\", version.m_major, version.m_minor, version.m_patch);\n\n\t// ImGui Logging\n\tutil::log_callback::impl = [&](std::string const & str)\n\t{\n\t\tengine::debug_console.AddLog(str.c_str());\n\t};\n\n\trender_system = std::make_unique<wr::D3D12RenderSystem>();\n\n\tphys::PhysicsEngine phys_engine;\n\n\tauto window = std::make_unique<wr::Window>(GetModuleHandleA(nullptr), \"D3D12 Test App\", 1280, 720);\n\n\twindow->SetKeyCallback([](int key, int action, int mods)\n\t{\n\t\tcurrent_scene->GetCamera<DebugCamera>()->KeyAction(key, action);\n\n\t\tif (action == WM_KEYUP && key == 0xC0)\n\t\t{\n\t\t\tengine::open_console = !engine::open_console;\n\t\t\tengine::debug_console.EmptyInput();\n\t\t}\n\t\tif (action == WM_KEYUP && key == VK_F1)\n\t\t{\n\t\t\tengine::show_imgui = !engine::show_imgui;\n\t\t}\n\t\tif (action == WM_KEYUP && key == VK_F2)\n\t\t{\n\t\t\tfg_manager::Next();\n\t\t}\n\t\tif (action == WM_KEYUP && key == VK_F3)\n\t\t{\n\t\t\tfg_manager::Prev();\n\t\t}\n\t});\n\n\twindow->SetMouseCallback([](int key, int action, int mods)\n\t{\n\t\tcurrent_scene->GetCamera<DebugCamera>()->MouseAction(key, action);\n\t});\n\n\twindow->SetMouseWheelCallback([](int amount, int action, int mods)\n\t{\n\t\tcurrent_scene->GetCamera<DebugCamera>()->MouseWheel(amount);\n\t});\n\n\twr::ModelLoader* assimp_model_loader = new wr::AssimpModelLoader();\n\twr::ModelLoader* gltf_model_loader = new wr::TinyGLTFModelLoader();\n\n\trender_system->Init(window.get());\t\n\n\tphys_engine.CreatePhysicsWorld();\n\n\tcurrent_scene = new DefaultScene();\n\tcurrent_scene->Init(render_system.get(), window->GetWidth(), window->GetHeight(), &phys_engine);\n\n\tfg_manager::Setup(*render_system, &RenderEditor);\n\n\twindow->SetResizeCallback([&](std::uint32_t width, std::uint32_t height)\n\t{\n\t\trender_system->WaitForAllPreviousWork();\n\t\trender_system->Resize(width, height);\n\t\tcurrent_scene->GetCamera<wr::CameraNode>()->SetAspectRatio((float)width / (float)height);\n\t\tcurrent_scene->GetCamera<wr::CameraNode>()->SetOrthographicResolution(width, height);\n\t\tfg_manager::Resize(*render_system, width, height);\n\t});\n\n\tauto file_watcher = new util::FileWatcher(\"resources/shaders\", std::chrono::milliseconds(100));\n\tfile_watcher->StartAsync(&ShaderDirChangeDetected);\n\n\twindow->SetRenderLoop([&]() {\n\t\t// Find delta\n\t\tfloat delta = ImGui::GetIO().DeltaTime;\n\t\tbool capture_frame = engine::recorder.ShouldCaptureAndIncrement(delta);\n\t\tif (capture_frame)\n\t\t{\n\t\t\tfg_manager::Get()->SaveTaskToDisc<wr::PostProcessingData>(engine::recorder.GetNextFilename(\".tga\"), 0);\n\t\t}\n\n\t\tif (new_scene && new_scene != current_scene)\n\t\t{\n\t\t\trender_system->WaitForAllPreviousWork();\n\t\t\tdelete current_scene;\n\t\t\tcurrent_scene = new_scene;\n\t\t\tcurrent_scene->Init(render_system.get(), window->GetWidth(), window->GetHeight(), &phys_engine);\n\t\t\tfg_manager::Get()->SetShouldExecute<wr::EquirectToCubemapTaskData>(true);\n\t\t\tfg_manager::Get()->SetShouldExecute<wr::CubemapConvolutionTaskData>(true);\n\t\t}\n\n\t\tcurrent_scene->Update(delta);\n\n#ifdef ENABLE_PHYSICS\n\t\tphys_engine.UpdateSim(delta, *current_scene->GetSceneGraph());\n#endif\n\n\t\tauto texture = render_system->Render(*current_scene->GetSceneGraph(), *fg_manager::Get());\n\n\t});\n\n\twindow->StartRenderLoop();\n\n\tdelete assimp_model_loader;\n\tdelete gltf_model_loader;\n\n\trender_system->WaitForAllPreviousWork(); // Make sure GPU is finished before destruction.\n\n\tdelete current_scene;\n\n\tfg_manager::Destroy();\n\trender_system.reset();\n\tdelete file_watcher;\n\n\treturn 0;\n}\n\nWISP_ENTRY(WispEntry)\n"
  },
  {
    "path": "tests/demo/demo_frame_graphs.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"frame_graph/frame_graph.hpp\"\n#include \"settings.hpp\"\n#include \"render_tasks/d3d12_imgui_render_task.hpp\"\n#include \"render_tasks/d3d12_brdf_lut_precalculation.hpp\"\n#include \"render_tasks/d3d12_deferred_main.hpp\"\n#include \"render_tasks/d3d12_deferred_composition.hpp\"\n#include \"render_tasks/d3d12_deferred_render_target_copy.hpp\"\n#include \"render_tasks/d3d12_raytracing_task.hpp\"\n#include \"render_tasks/d3d12_rt_reflection_task.hpp\"\n#include \"render_tasks/d3d12_rt_shadow_task.hpp\"\n#include \"render_tasks/d3d12_shadow_denoiser_task.hpp\"\n#include \"render_tasks/d3d12_equirect_to_cubemap.hpp\"\n#include \"render_tasks/d3d12_cubemap_convolution.hpp\"\n#include \"render_tasks/d3d12_spatial_reconstruction.hpp\"\n#include \"render_tasks/d3d12_reflection_denoiser.hpp\"\n#include \"render_tasks/d3d12_rtao_task.hpp\"\n#include \"render_tasks/d3d12_post_processing.hpp\"\n#include \"render_tasks/d3d12_build_acceleration_structures.hpp\"\n#include \"render_tasks/d3d12_path_tracer.hpp\"\n#include \"render_tasks/d3d12_accumulation.hpp\"\n#include \"render_tasks/d3d12_dof_bokeh.hpp\"\n#include \"render_tasks/d3d12_dof_bokeh_postfilter.hpp\"\n#include \"render_tasks/d3d12_dof_coc.hpp\"\n#include \"render_tasks/d3d12_dof_compute_near_mask.hpp\"\n#include \"render_tasks/d3d12_down_scale.hpp\"\n#include \"render_tasks/d3d12_dof_composition.hpp\"\n#include \"render_tasks/d3d12_dof_dilate_near.hpp\"\n#include \"render_tasks/d3d12_hbao.hpp\"\n#include \"render_tasks/d3d12_ansel.hpp\"\n#include \"render_tasks/d3d12_bloom_extract_bright.hpp\"\n#include \"render_tasks/d3d12_bloom_composition.hpp\"\n#include \"render_tasks/d3d12_bloom_horizontal_blur.hpp\"\n#include \"render_tasks/d3d12_bloom_vertical_blur.hpp\"\n\nnamespace fg_manager\n{\n\n\tenum class PrebuildFrameGraph : std::uint32_t\n\t{\n\t\tDEFERRED = 0,\n\t\tRT_HYBRID = 1,\n\t\tRAYTRACING = 2,\n\t\tPATH_TRACER = 3,\n\t};\n\n\tinline std::string GetFrameGraphName(PrebuildFrameGraph id)\n\t{\n\t\tswitch (id)\n\t\t{\n\t\tcase PrebuildFrameGraph::DEFERRED:\n\t\t\treturn \"Deferred\";\n\t\tcase PrebuildFrameGraph::RT_HYBRID:\n\t\t\treturn \"Hybrid\";\n\t\tcase PrebuildFrameGraph::RAYTRACING:\n\t\t\treturn \"Full Raytracing\";\n\t\tcase PrebuildFrameGraph::PATH_TRACER:\n\t\t\treturn \"Path Tracer\";\n\t\tdefault:\n\t\t\treturn \"Unknown\";\n\t\t}\n\t}\n\n\tstatic PrebuildFrameGraph current = fg_manager::PrebuildFrameGraph::DEFERRED;\n\tstatic std::array<wr::FrameGraph*, 4> frame_graphs = {};\n\n\tinline void Setup(wr::RenderSystem& rs, util::Delegate<void(ImTextureID)> const& imgui_func)\n\t{\n\t\t// Raytracing\n\t\t{\n\t\t\tauto& fg = frame_graphs[(int)PrebuildFrameGraph::RAYTRACING];\n\t\t\tfg = new wr::FrameGraph(4);\n\n\t\t\twr::AddBuildAccelerationStructuresTask(*fg);\n\t\t\twr::AddEquirectToCubemapTask(*fg);\n\t\t\twr::AddCubemapConvolutionTask(*fg);\n\t\t\twr::AddRaytracingTask(*fg);\n\n\t\t\twr::AddPostProcessingTask<wr::RaytracingData>(*fg);\n\t\t\t\n\t\t\t// Copy the scene render pixel data to the final render target\n\t\t\twr::AddRenderTargetCopyTask<wr::PostProcessingData>(*fg);\n\n\t\t\t// Display ImGui\n\t\t\tfg->AddTask<wr::ImGuiTaskData>(wr::GetImGuiTask<wr::PostProcessingData>(imgui_func), L\"ImGui\");\n\n\t\t\tfg->Setup(rs);\n\t\t}\n\n\t\t// Deferred\n\t\t{\n\t\t\tauto& fg = frame_graphs[(int)PrebuildFrameGraph::DEFERRED];\n\t\t\tfg = new wr::FrameGraph(24);\n\t\t\t\n\t\t\twr::AddBrdfLutPrecalculationTask(*fg);\n\t\t\twr::AddEquirectToCubemapTask(*fg);\n\t\t\twr::AddCubemapConvolutionTask(*fg);\n\t\t\twr::AddDeferredMainTask(*fg, std::nullopt, std::nullopt, false);\n\t\t\twr::AddHBAOTask(*fg);\n\t\t\twr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt);\n\n\t\t\t//High quality bloom pass\n\t\t\twr::AddBloomExtractBrightTask<wr::DeferredCompositionTaskData, wr::DeferredMainTaskData>(*fg);\n\t\t\twr::AddBloomBlurHorizontalTask<wr::BloomExtractBrightData>(*fg);\n\t\t\twr::AddBloomBlurVerticalTask<wr::BloomBlurHorizontalData>(*fg);\n\t\t\twr::AddBloomCompositionTask<wr::DeferredCompositionTaskData, wr::BloomBlurVerticalData>(*fg);\n\n\t\t\t// Do Depth of field task\n\t\t\twr::AddDoFCoCTask<wr::DeferredMainTaskData>(*fg);\n\t\t\twr::AddDownScaleTask<wr::BloomCompostionData, wr::DoFCoCData>(*fg);\n\t\t\twr::AddDoFNearMaskTask<wr::DownScaleData>(*fg);\n\t\t\twr::AddDoFDilateTask<wr::DoFNearMaskData>(*fg);\n\t\t\twr::AddDoFBokehTask<wr::DownScaleData, wr::DoFDilateData>(*fg);\n\t\t\twr::AddDoFBokehPostFilterTask<wr::DoFBokehData>(*fg);\n\n\t\t\twr::AddDoFCompositionTask<wr::BloomCompostionData, wr::DoFBokehPostFilterData, wr::DoFCoCData>(*fg);\n\n\t\t\twr::AddPostProcessingTask<wr::DoFCompositionData>(*fg);\n\n\t\t\t// Copy the scene render pixel data to the final render target\n\t\t\twr::AddRenderTargetCopyTask<wr::PostProcessingData>(*fg);\n\n\t\t\twr::AddAnselTask(*fg);\n\n\t\t\t// Display ImGui\n\t\t\tfg->AddTask<wr::ImGuiTaskData>(wr::GetImGuiTask<wr::PostProcessingData>(imgui_func), L\"ImGui\");\n\n\t\t\tfg->Setup(rs);\n\t\t}\n\n\t\t// Path Tracer\n\t\t{\n\t\t\tauto& fg = frame_graphs[(int)PrebuildFrameGraph::PATH_TRACER];\n\t\t\tfg = new wr::FrameGraph(18);\n\n\t\t\t// Precalculate BRDF Lut\n\t\t\twr::AddBrdfLutPrecalculationTask(*fg);\n\n\t\t\twr::AddEquirectToCubemapTask(*fg);\n\t\t\twr::AddCubemapConvolutionTask(*fg);\n\n\t\t\t// Construct the G-buffer\n\t\t\twr::AddDeferredMainTask(*fg, std::nullopt, std::nullopt, true);\n\n\t\t\twr::AddHBAOTask(*fg);\n\n\t\t\t// Build Acceleration Structure\n\t\t\twr::AddBuildAccelerationStructuresTask(*fg);\n\n\t\t\t// Raytracing task\n\t\t\twr::AddRTReflectionTask(*fg);\n\t\t\twr::AddRTShadowTask(*fg);\n\n\t\t\twr::AddShadowDenoiserTask(*fg);\n\t\t\twr::AddSpatialReconstructionTask(*fg);\n\t\t\twr::AddReflectionDenoiserTask(*fg);\n\n\t\t\t// Global Illumination Path Tracing\n\t\t\twr::AddPathTracerTask(*fg);\n\t\t\twr::AddAccumulationTask<wr::PathTracerData>(*fg);\n\n\t\t\twr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt);\n\n\t\t\t// Do some post processing\n\t\t\twr::AddPostProcessingTask<wr::DeferredCompositionTaskData>(*fg);\n\n\t\t\t// Copy the raytracing pixel data to the final render target\n\t\t\twr::AddRenderTargetCopyTask<wr::PostProcessingData>(*fg);\n\n\t\t\twr::AddAnselTask(*fg);\n\n\t\t\t// Display ImGui\n\t\t\tfg->AddTask<wr::ImGuiTaskData>(wr::GetImGuiTask<wr::PostProcessingData>(imgui_func), L\"ImGui\");\n\n\t\t\t// Finalize the frame graph\n\t\t\tfg->Setup(rs);\n\t\t}\n\n\t\t// Hybrid raytracing\n\t\t{\n\t\t\tauto& fg = frame_graphs[(int) PrebuildFrameGraph::RT_HYBRID];\n\t\t\tfg = new wr::FrameGraph(31);\n\n\t\t\t// Precalculate BRDF Lut\n\t\t\twr::AddBrdfLutPrecalculationTask(*fg);\n\n\t\t\twr::AddEquirectToCubemapTask(*fg);\n\t\t\twr::AddCubemapConvolutionTask(*fg);\n\t\t\t // Construct the G-buffer\n\t\t\twr::AddDeferredMainTask(*fg, std::nullopt, std::nullopt, true);\n\n\t\t\t// Build Acceleration Structure\n\t\t\twr::AddBuildAccelerationStructuresTask(*fg);\n\n\t\t\t// Raytracing task\n\t\t\twr::AddRTReflectionTask(*fg);\n\t\t\twr::AddRTShadowTask(*fg);\n\n\t\t\twr::AddShadowDenoiserTask(*fg);\n\t\t\twr::AddSpatialReconstructionTask(*fg);\n\n\t\t\twr::AddReflectionDenoiserTask(*fg);\n\n\t\t\t//Raytraced Ambient Occlusion task\n\t\t\twr::AddRTAOTask(*fg, static_cast<wr::D3D12RenderSystem&>(rs).m_device);\n\n\t\t\twr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt);\n\n\t\t\t//High quality bloom pass\n\t\t\twr::AddBloomExtractBrightTask<wr::DeferredCompositionTaskData, wr::DeferredMainTaskData>(*fg);\n\t\t\twr::AddBloomBlurHorizontalTask<wr::BloomExtractBrightData>(*fg);\n\t\t\twr::AddBloomBlurVerticalTask<wr::BloomBlurHorizontalData>(*fg);\n\t\t\twr::AddBloomCompositionTask<wr::DeferredCompositionTaskData, wr::BloomBlurVerticalData>(*fg);\n\n\t\t\t// Do Depth of field task\n\t\t\twr::AddDoFCoCTask<wr::DeferredMainTaskData>(*fg);\n\t\t\twr::AddDownScaleTask<wr::BloomCompostionData, wr::DoFCoCData>(*fg);\n\t\t\twr::AddDoFNearMaskTask<wr::DownScaleData>(*fg);\n\t\t\twr::AddDoFDilateTask<wr::DoFNearMaskData>(*fg);\n\t\t\twr::AddDoFBokehTask<wr::DownScaleData, wr::DoFDilateData>(*fg);\n\t\t\twr::AddDoFBokehPostFilterTask<wr::DoFBokehData>(*fg);\n\t\t\twr::AddDoFCompositionTask<wr::BloomCompostionData, wr::DoFBokehPostFilterData, wr::DoFCoCData>(*fg);\n\n\t\t\twr::AddPostProcessingTask<wr::DoFCompositionData>(*fg);\n\n\t\t\t// Copy the scene render pixel data to the final render target\n\t\t\twr::AddRenderTargetCopyTask<wr::PostProcessingData>(*fg);\n\n\t\t\twr::AddAnselTask(*fg);\n\n\t\t\t// Display ImGui\n\t\t\tfg->AddTask<wr::ImGuiTaskData>(wr::GetImGuiTask<wr::PostProcessingData>(imgui_func), L\"ImGui\");\n\n\t\t\t// Finalize the frame graph\n\t\t\tfg->Setup(rs);\n\t\t}\n\t}\n\n\tvoid Resize(wr::RenderSystem& render_system, std::uint32_t width, std::uint32_t height)\n\t{\n\t\tfor (int i = 0; i < frame_graphs.size(); ++i)\n\t\t{\n\t\t\tframe_graphs[i]->Resize(width, height);\n\t\t}\n\t}\n\n\tinline wr::FrameGraph* Get()\n\t{\n\t\treturn frame_graphs[(int)current];\n\t}\n\n\tinline void Next()\n\t{\n\t\tcurrent = (PrebuildFrameGraph)((static_cast<std::uint32_t>(current) + 1ull) % frame_graphs.size());\n\t}\n\n\tinline void Prev()\n\t{\n\t\tcurrent = (PrebuildFrameGraph)((static_cast<std::uint32_t>(current) - 1ull) % frame_graphs.size());\n\t}\n\n\tinline void Set(PrebuildFrameGraph value)\n\t{\n\t\tcurrent = value;\n\t}\n\n\tinline void Destroy()\n\t{\n\t\tfor (auto& fg : frame_graphs)\n\t\t{\n\t\t\tdelete fg;\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "tests/demo/engine_interface.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <algorithm>\n\n#include \"wisp.hpp\"\n#include \"window.hpp\"\n#include \"imgui_tools.hpp\"\n#include \"scene_graph/scene_graph.hpp\"\n#include \"d3d12/d3d12_renderer.hpp\"\n#include \"imgui/ImGuizmo.h\"\n#include \"demo_frame_graphs.hpp\"\n#include \"../common/scene.hpp\"\n#include \"scene_emibl.hpp\"\n#include \"scene_viknell.hpp\"\n#include \"scene_sponza.hpp\"\n#include \"scene_alien.hpp\"\n#include \"imgui_graphics_settings.hpp\"\n\nnamespace engine\n{\n\n\tstatic bool main_menu = true;\n\tstatic bool keep_aspect_ratio = false;\n\tstatic float custom_aspect_ratio = 1280.f/720.f;\n\tstatic bool open0 = true;\n\tstatic bool open_viewport = true;\n\tstatic bool open1 = true;\n\tstatic bool open_console = false;\n\tstatic bool open_scene = true;\n\tstatic bool open_recorder = true;\n\tstatic char recorder_name[256] = \"unamed\";\n\tstatic char recorder_base_dir[256] = \"D:\\\\WispRecorder\\\\\";\n\tstatic int selected_scene = 0;\n\tstatic bool show_imgui = true;\n\tstatic bool fullscreen = false;\n\tstatic wr::imgui::window::Stats stats_window { false };\n\tstatic char message_buffer[600];\n\n\tstatic wr::imgui::special::DebugConsole debug_console;\n\n\tstruct Recorder\n\t{\n\t\tbool m_recording = false;\n\t\tint m_target_framerate = 30;\n\t\tint m_record_frame_inverval = 1;\n\t\tint m_frames_since_last_capture = 0;\n\t\tint m_frames_recorded = 0;\n\t\tstd::string m_output_dir;\n\t\tstd::string m_name;\n\n\t\tvoid Start(std::string name, std::string base_output_dir = \"D:\\\\WispRecorder\\\\\")\n\t\t{\n\t\t\tm_frames_recorded = 0;\n\t\t\tm_frames_since_last_capture = 0;\n\n\t\t\tm_output_dir = base_output_dir + name;\n\t\t\tm_name = name;\n\n\t\t\tstd::filesystem::create_directory(m_output_dir);\n\n\t\t\tshow_imgui = false;\n\n\t\t\tm_recording = true;\n\t\t}\n\n\t\tvoid Stop()\n\t\t{\n\t\t\tm_recording = false;\n\t\t}\n\n\t\tstd::string GetNextFilename(std::string ext)\n\t\t{\n\t\t\tm_frames_recorded++;\n\t\t\treturn m_output_dir + \"\\\\\" + m_name + \"_\"+ std::to_string(m_target_framerate) + \"fps_frame\" + std::to_string(m_frames_recorded) + ext;\n\t\t}\n\n\t\tbool ShouldCaptureAndIncrement(float& out_delta)\n\t\t{\n\t\t\tif (!m_recording) return false;\n\n\t\t\tbool retval = false;\n\n\t\t\tif (m_frames_since_last_capture == m_record_frame_inverval)\n\t\t\t{\n\t\t\t\tretval = true;\n\t\t\t\tm_frames_since_last_capture = 0;\n\t\t\t\tout_delta = 1.f / (float)m_target_framerate;;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tm_frames_since_last_capture++;\n\t\t\t\tout_delta = 0;\n\t\t\t}\n\n\t\t\treturn retval;\n\t\t}\n\n\t\tbool IsRecording()\n\t\t{\n\t\t\treturn m_recording;\n\t\t}\n\t};\n\n\tstatic Recorder recorder;\n\n\tvoid RenderEngine(ImTextureID output, wr::D3D12RenderSystem* render_system, Scene* scene, Scene** new_scene)\n\t{\n\t\tauto sg = scene->GetSceneGraph();\n\n\t\tImVec2 viewport_pos(0, 0);\n\t\tImVec2 viewport_size(0, 0);\n\n\t\tdebug_console.AddCommand(\"stats\",\n\t\t\t[](wr::imgui::special::DebugConsole& console, std::string const&)\n\t\t\t{\n\t\t\t\tstats_window.m_open = !stats_window.m_open;\n\t\t\t\tconsole.AddLog(\"Toggled the stats window.\");\n\t\t\t},\n\t\t\"Toggle the statistics window\");\n\n\t\tdebug_console.Draw(\"Console\", &open_console);\n\n\t\tif (!show_imgui)\n\t\t{\n\t\t\tstats_window.Draw(*render_system, viewport_pos);\n\t\t\tsg->GetActiveCamera()->SetAspectRatio(render_system->m_viewport.m_viewport.Width / render_system->m_viewport.m_viewport.Height);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (main_menu && ImGui::BeginMainMenuBar())\n\t\t\t{\n\t\t\t\tif (ImGui::BeginMenu(\"File\"))\n\t\t\t\t{\n\t\t\t\t\tif (ImGui::MenuItem(\"Exit\", \"ALT+F4\")) std::exit(0);\n\t\t\t\t\tif (ImGui::MenuItem(\"Hide ImGui\", \"F1\")) show_imgui = false;\n\t\t\t\t\tImGui::EndMenu();\n\t\t\t\t}\n\t\t\t\tif (ImGui::BeginMenu(\"Window\"))\n\t\t\t\t{\n\t\t\t\t\tImGui::MenuItem(\"Viewport\", nullptr, &open_viewport);\n\t\t\t\t\tImGui::MenuItem(\"Scene Graph Editor\", nullptr, &wr::imgui::window::open_scene_graph_editor);\n\t\t\t\t\tImGui::MenuItem(\"Inspector\", nullptr, &wr::imgui::window::open_inspector);\n\t\t\t\t\tImGui::MenuItem(\"Hardware Info\", nullptr, &wr::imgui::window::open_hardware_info);\n\t\t\t\t\tImGui::MenuItem(\"DirectX 12 Settings\", nullptr, &wr::imgui::window::open_d3d12_settings);\n\t\t\t\t\tImGui::Separator();\n\t\t\t\t\twr::imgui::menu::Registries();\n\t\t\t\t\tImGui::Separator();\n\t\t\t\t\tImGui::MenuItem(\"Theme\", nullptr, &open0);\n\t\t\t\t\tImGui::MenuItem(\"Statistics\", nullptr, &stats_window.m_open);\n\t\t\t\t\tImGui::MenuItem(\"Camera Settings\", nullptr, &open1);\n\t\t\t\t\twr::imgui::menu::GraphicsSettingsMenu(fg_manager::Get());\n\t\t\t\t\tImGui::EndMenu();\n\t\t\t\t}\n\n\t\t\t\tImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.f, 1.f, 1.f, 0.4f));\n\t\t\t\tImGui::Text(\"Current Frame Graph: %s\", fg_manager::GetFrameGraphName(fg_manager::current).c_str());\n\t\t\t\tImGui::PopStyleColor();\n\n\t\t\t\tImGui::EndMainMenuBar();\n\t\t\t}\n\n\t\t\tImGui::DockSpaceOverViewport(main_menu, ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode);\n\n\t\t\tauto& io = ImGui::GetIO();\n\n\t\t\tif (open_viewport)\n\t\t\t{\n\t\t\t\tImGui::Begin(\"Viewport\");\n\n\t\t\t\tImGui::Checkbox(\"Stats\", &stats_window.m_open);\n\t\t\t\tImGui::SameLine();\n\t\t\t\tImGui::SetNextItemWidth(80);\n\t\t\t\tImGui::Checkbox(\"Force Aspect Ratio\", &keep_aspect_ratio);\n\t\t\t\tImGui::SameLine();\n\t\t\t\tImGui::InputFloat(\"##\", &custom_aspect_ratio);\n\n\t\t\t\tviewport_pos = ImGui::GetCursorScreenPos();\n\t\t\t\tviewport_size = ImGui::GetContentRegionAvail();\n\n\t\t\t\tif (!keep_aspect_ratio && viewport_size.x > 10 && viewport_size.y > 10)\n\t\t\t\t{\n\t\t\t\t\tsg->GetActiveCamera()->SetAspectRatio(viewport_size.x / viewport_size.y);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tsg->GetActiveCamera()->SetAspectRatio(custom_aspect_ratio);\n\t\t\t\t}\n\n\t\t\t\tImGui::Image(output, viewport_size);\n\t\t\t\tImGui::End();\n\t\t\t}\n\n\t\t\tif (open_scene)\n\t\t\t{\n\t\t\t\tImGui::Begin(\"Demo Scene\", &open_scene);\n\t\t\t\tif (ImGui::Button(\"Save Lights\"))\n\t\t\t\t{\n\t\t\t\t\tscene->SaveLightsToJSON();\n\t\t\t\t}\n\n\t\t\t\tImGui::Separator();\n\n\t\t\t\tconst char* items[] = { \"Viknell\", \"Emibl\", \"Sponza\", \"Alien\" };\n\n\t\t\t\tImGui::Combo(\"##\", &selected_scene, items, IM_ARRAYSIZE(items));\n\t\t\t\tImGui::SameLine();\n\t\t\t\tif (ImGui::Button(\"Load\"))\n\t\t\t\t{\n\t\t\t\t\tswitch (selected_scene)\n\t\t\t\t\t{\n\t\t\t\t\t\tcase 0: (*new_scene) = new ViknellScene(); break;\n\t\t\t\t\t\tcase 1: (*new_scene) = new EmiblScene(); break;\n\t\t\t\t\t\tcase 2: (*new_scene) = new SponzaScene(); break;\n\t\t\t\t\t\tcase 3: (*new_scene) = new AlienScene(); break;\n\t\t\t\t\t\tdefault: LOGW(\"Tried to load a scene that is not supported\"); break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tImGui::End();\n\t\t\t}\n\n\t\t\tif (open_recorder)\n\t\t\t{\n\t\t\t\trecorder.Stop(); // don't record while in imgui.\n\n\t\t\t\tImGui::Begin(\"Recorder\", &open_recorder);\n\t\t\t\tif (ImGui::Button(\"Record\"))\n\t\t\t\t{\n\t\t\t\t\trecorder.Start(recorder_name, recorder_base_dir);\n\t\t\t\t}\n\n\t\t\t\tImGui::InputText(\"Recording Name\", recorder_name, IM_ARRAYSIZE(recorder_name));\n\t\t\t\tImGui::InputText(\"Base Output Dir\", recorder_base_dir, IM_ARRAYSIZE(recorder_base_dir));\n\t\t\t\tImGui::InputInt(\"Target Framerate\", &recorder.m_target_framerate);\n\t\t\t\tImGui::InputInt(\"Frame Interval\", &recorder.m_record_frame_inverval);\n\n\t\t\t\tImGui::End();\n\t\t\t}\n\n\t\t\tif (open0)\n\t\t\t{\n\t\t\t\tImGui::Begin(\"Theme\", &open0);\n\t\t\t\tif (ImGui::Button(\"Cherry\")) ImGui::StyleColorsCherry();\n\t\t\t\tif (ImGui::Button(\"Unreal Engine\")) ImGui::StyleColorsUE();\n\t\t\t\tif (ImGui::Button(\"Light Green\")) ImGui::StyleColorsLightGreen();\n\t\t\t\tif (ImGui::Button(\"Light\")) ImGui::StyleColorsLight();\n\t\t\t\tif (ImGui::Button(\"Dark\")) ImGui::StyleColorsDark();\n\t\t\t\tif (ImGui::Button(\"Dark2\")) ImGui::StyleColorsDarkCodz1();\n\t\t\t\tif (ImGui::Button(\"Corporate Grey\")) ImGui::StyleCorporateGrey();\n\t\t\t\tImGui::End();\n\t\t\t}\n\n\t\t\tif (open1)\n\t\t\t{\n\t\t\t\tImGui::Begin(\"Camera Settings\", &open1);\n\n\t\t\t\tauto pos = sg->GetActiveCamera()->m_position;\n\t\t\t\tImGui::DragFloat3(\"Position\", pos.m128_f32, 0.5f);\n\n\t\t\t\tfloat rot[3] = { DirectX::XMConvertToDegrees(DirectX::XMVectorGetX(sg->GetActiveCamera()->m_rotation_radians)),\n\t\t\t\t\tDirectX::XMConvertToDegrees(DirectX::XMVectorGetY(sg->GetActiveCamera()->m_rotation_radians)),\n\t\t\t\t\tDirectX::XMConvertToDegrees(DirectX::XMVectorGetZ(sg->GetActiveCamera()->m_rotation_radians)) };\n\n\t\t\t\tImGui::DragFloat3(\"Rotation\", rot, 0.01f);\n\n\t\t\t\tif (!ImGui::IsMouseDown(1))\n\t\t\t\t{\n\t\t\t\t\tsg->GetActiveCamera()->SetPosition(pos);\n\t\t\t\t\tsg->GetActiveCamera()->SetRotation(DirectX::XMVectorSet(DirectX::XMConvertToRadians(rot[0]), DirectX::XMConvertToRadians(rot[1]), DirectX::XMConvertToRadians(rot[2]), 0));\n\n\t\t\t\t\tsg->GetActiveCamera()->SignalTransformChange();\n\t\t\t\t\tsg->GetActiveCamera()->SignalChange();\n\t\t\t\t}\n\n\t\t\t\tImGui::Separator();\n\n\t\t\t\tfloat frustum_near = sg->GetActiveCamera()->m_frustum_near;\n\t\t\t\tfloat frustum_far = sg->GetActiveCamera()->m_frustum_far;\n\t\t\t\tImGui::DragFloatRange2(\"Frustum Near/Far\", &frustum_near, &frustum_far, 1, 0.0001f, std::numeric_limits<float>::max());\n\t\t\t\tsg->GetActiveCamera()->SetFrustumNear(std::max(frustum_near, 0.0000001f));\n\t\t\t\tsg->GetActiveCamera()->SetFrustumFar(std::max(frustum_far, 0.0000001f));\n\n\t\t\t\tImGui::Separator();\n\t\t\t\t\n\t\t\t\tImGui::MenuItem(\"Enable DOF\", nullptr, &sg->GetActiveCamera()->m_enable_dof);\n\t\t\t\tImGui::MenuItem(\"Enable Orthographic view\", nullptr, &sg->GetActiveCamera()->m_enable_orthographic);\n\t\t\t\tImGui::DragFloat(\"F number\", &sg->GetActiveCamera()->m_f_number, 1.f, 1.f, 128.f);\n\t\t\t\tImGui::DragFloat(\"Film size\", &sg->GetActiveCamera()->m_film_size, 1.f, 25.f, 100.f);\n\t\t\t\tImGui::DragFloat(\"Bokeh Shape amount\", &sg->GetActiveCamera()->m_shape_amt, 0.005f, 0.f, 1.f);\n\t\t\t\tImGui::DragInt(\"Aperture blades\", &sg->GetActiveCamera()->m_aperture_blades, 1, 3, 7);\n\t\t\t\tImGui::DragFloat(\"Focal Length\", &sg->GetActiveCamera()->m_focal_length, 1.f, 1.f, 300.f);\n\t\t\t\tImGui::DragFloat(\"Focal plane distance\", &sg->GetActiveCamera()->m_focus_dist, 1.f, 0.f, 10000.f);\n\t\t\t\tImGui::DragFloat(\"DoF Range multiplier\", &sg->GetActiveCamera()->m_dof_range, 0.05f, 0.f, 10000.f);\n\t\t\t\t\n\n\t\t\t\tsg->GetActiveCamera()->SetFovFromFocalLength(sg->GetActiveCamera()->m_aspect_ratio, sg->GetActiveCamera()->m_film_size);\n\n\t\t\t\tImGui::End();\n\t\t\t}\n\n\t\t\tstats_window.Draw(*render_system, viewport_pos);\n\t\t\twr::imgui::window::SceneGraphEditor(sg.get());\n\t\t\twr::imgui::window::Inspector(sg.get(), viewport_pos, viewport_size);\n\t\t\twr::imgui::window::ShaderRegistry();\n\t\t\twr::imgui::window::PipelineRegistry();\n\t\t\twr::imgui::window::RootSignatureRegistry();\n\t\t\twr::imgui::window::D3D12HardwareInfo(*render_system);\n\t\t\twr::imgui::window::D3D12Settings();\n\t\t\twr::imgui::window::GraphicsSettings(fg_manager::Get());\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "tests/demo/physics_engine.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"physics_engine.hpp\"\n\n#include \"physics_node.hpp\"\n#include \"debug_camera.hpp\"\n#include \"model_pool.hpp\"\n\nnamespace phys\n{\n\tvoid PhysicsEngine::CreatePhysicsWorld()\n\t{\n\t\t///collision configuration contains default setup for memory, collision setup\n\t\tcollision_config = new btDefaultCollisionConfiguration();\n\t\t//m_collisionConfiguration->setConvexConvexMultipointIterations();\n\n\t\t///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)\n\t\tcoll_dispatcher = new btCollisionDispatcher(collision_config);\n\n\t\tbroadphase = new btDbvtBroadphase();\n\n\t\t///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)\n\t\tconstraint_solver = new btSequentialImpulseConstraintSolver();\n\n\t\tphys_world = new btDiscreteDynamicsWorld(coll_dispatcher, broadphase, constraint_solver, collision_config);\n\t\tphys_world->setGravity(btVector3(0, -9.8, 0));\n\n\t}\n\n\tbtSphereShape* PhysicsEngine::CreateSphereShape(const float radius)\n\t{\n\t\tauto shape = new btSphereShape(radius);\n\t\tcollision_shapes.push_back(shape);\n\t\treturn shape;\n\t}\n\n\tbtCapsuleShape* PhysicsEngine::CreateCapsuleShape(const float width, const float height)\n\t{\n\t\tauto shape = new btCapsuleShape(width, height);\n\t\tcollision_shapes.push_back(shape);\n\t\treturn shape;\n\t}\n\n\tstd::vector<btConvexHullShape*> PhysicsEngine::CreateConvexShape(wr::ModelData* model)\n\t{\n\t\tstd::vector<btConvexHullShape*> hulls;\n\n\t\tfor (auto& mesh_data : model->m_meshes)\n\t\t{\n\t\t\tbtConvexHullShape* shape = new btConvexHullShape();\n\n\n\t\t\tfor (auto& idx : mesh_data->m_indices)\n\t\t\t{\n\n\t\t\t\tauto pos = mesh_data->m_positions[idx];\n\t\t\t\tshape->addPoint(btVector3(pos.x, pos.y, pos.z), false);\n\t\t\t}\n\t\t\tshape->recalcLocalAabb();\n\n\t\t\tcollision_shapes.push_back(shape);\n\t\t\thulls.push_back(shape);\n\t\t}\n\n\t\treturn hulls;\n\t}\n\n\tstd::vector<btBvhTriangleMeshShape*> PhysicsEngine::CreateTriangleMeshShape(wr::ModelData* model)\n\t{\n\t\tstd::vector<btBvhTriangleMeshShape*> hulls;\n\n\t\tfor (auto& mesh_data : model->m_meshes)\n\t\t{\n\t\t\tbtTriangleIndexVertexArray* va = new btTriangleIndexVertexArray(mesh_data->m_indices.size() / 3,\r\n\t\t\t\treinterpret_cast<int*>(mesh_data->m_indices.data()),\r\n\t\t\t\t3 * sizeof(std::uint32_t),\r\n\t\t\t\tmesh_data->m_positions.size(), reinterpret_cast<btScalar*>(mesh_data->m_positions.data()), sizeof(DirectX::XMFLOAT3));\n\n\t\t\tbtBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(va, true);\n\t\t\tcollision_shapes.push_back(shape);\n\t\t\thulls.push_back(shape);\n\t\t}\n\n\t\treturn hulls;\n\t}\n\n\tbtBoxShape* PhysicsEngine::CreateBoxShape(const btVector3& halfExtents)\n\t{\n\t\tauto shape = new btBoxShape(halfExtents);\n\t\tcollision_shapes.push_back(shape);\n\t\treturn shape;\n\t}\n\n\tbtRigidBody* PhysicsEngine::CreateRigidBody(float mass, const btTransform& startTransform, btCollisionShape* shape)\n\t{\n\t\tbtAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));\n\n\t\t//rigidbody is dynamic if and only if mass is non zero, otherwise static\n\t\tbool is_dynamic = (mass != 0.f);\n\n\t\tbtVector3 local_inertia(0, 0, 0);\n\t\tif (is_dynamic)\n\t\t\tshape->calculateLocalInertia(mass, local_inertia);\n\n\t\t//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects\n\n#ifdef USE_MOTIONSTATE\n\t\tbtDefaultMotionState* motion_state = new btDefaultMotionState(startTransform);\n\n\t\tbtRigidBody::btRigidBodyConstructionInfo cInfo(mass, motion_state, shape, local_inertia);\n\t\tbtRigidBody* body = new btRigidBody(cInfo);\n\t\t//body->setContactProcessingThreshold(m_defaultContactProcessingThreshold);\n\n#else\n\t\tbtRigidBody* body = new btRigidBody(mass, 0, shape, localInertia);\n\t\tbody->setWorldTransform(startTransform);\n#endif \n\n\t\tbody->setUserIndex(-1);\n\t\tphys_world->addRigidBody(body);\n\t\treturn body;\n\t}\n\n\tvoid PhysicsEngine::UpdateSim(float delta, wr::SceneGraph& sg)\n\t{\n\t\tphys_world->stepSimulation(delta);\n\n\t\tfor (auto& n : sg.GetMeshNodes())\n\t\t{\n\t\t\tif (auto & node = std::dynamic_pointer_cast<PhysicsMeshNode>(n))\n\t\t\t{\n\t\t\t\tif (!node->m_rigid_bodies.has_value() && node->m_rigid_body)\n\t\t\t\t{\n\t\t\t\t\tauto world_position = node->m_rigid_body->getWorldTransform().getOrigin();\n\t\t\t\t\tnode->m_position = util::BV3toDXV3(world_position);\n\t\t\t\t\tnode->SignalTransformChange();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tPhysicsEngine::~PhysicsEngine()\n\t{\n\t\tdelete phys_world;\n\t\tdelete broadphase;\n\t\tdelete coll_dispatcher;\n\t\tdelete constraint_solver;\n\t\tdelete collision_config;\n\t}\n\n} /* phys*/"
  },
  {
    "path": "tests/demo/physics_engine.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"wisp.hpp\"\n\n#include <btBulletDynamicsCommon.h>\n#include <LinearMath/btVector3.h>\n#include <LinearMath/btAlignedObjectArray.h>\n#include <btBulletDynamicsCommon.h>\n#include <scene_graph/scene_graph.hpp>\n\n#define USE_MOTIONSTATE 1\n\nnamespace wr\n{\n\tstruct Model;\n}\n\nnamespace phys\n{\n\tnamespace util\n\t{\n\t\tinline btVector3 DXV3toBV3(DirectX::XMVECTOR v)\n\t\t{\n\t\t\treturn btVector3(v.m128_f32[0], v.m128_f32[1], v.m128_f32[2]);\n\t\t}\n\t\tinline DirectX::XMVECTOR BV3toDXV3(btVector3 v)\n\t\t{\n\t\t\treturn { v.x(), v.y(), v.z() };\n\t\t}\n\t}\n\n\tstruct PhysicsEngine\n\t{\n\t\tbtAlignedObjectArray<btCollisionShape*> collision_shapes;\n\t\tbtBroadphaseInterface* broadphase;\n\t\tbtCollisionDispatcher* coll_dispatcher;\n\t\tbtConstraintSolver* constraint_solver;\n\t\tbtDefaultCollisionConfiguration* collision_config;\n\t\tbtDiscreteDynamicsWorld* phys_world;\n\n\t\tvoid CreatePhysicsWorld();\n\n\t\tbtBoxShape* CreateBoxShape(const btVector3& halfExtents);\n\t\tbtSphereShape* CreateSphereShape(const float radius);\n\t\tbtCapsuleShape* CreateCapsuleShape(const float width, const float height);\n\t\tstd::vector<btConvexHullShape*> CreateConvexShape(wr::ModelData* model);\n\t\tstd::vector<btBvhTriangleMeshShape*> CreateTriangleMeshShape(wr::ModelData* model);\n\n\t\tbtRigidBody* CreateRigidBody(float mass, const btTransform& startTransform, btCollisionShape* shape);\n\n\t\tvoid UpdateSim(float delta, wr::SceneGraph& sg);\n\n\t\t~PhysicsEngine();\n\t};\n\n} /* phys*/"
  },
  {
    "path": "tests/demo/physics_node.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"physics_node.hpp\"\n#include \"physics_engine.hpp\"\n\nPhysicsMeshNode::~PhysicsMeshNode()\n{\n\tif(m_shape)\n\t{\n\t\tdelete m_shape;\n\t}\n\t\n\tif(m_rigid_body)\n\t{\n\t\tdelete m_rigid_body->getMotionState();\n\t\tm_phys_engine.phys_world->removeRigidBody(m_rigid_body);\n\t\tdelete m_rigid_body;\n\t}\n\t\n\tif(m_shapes.has_value())\n\t{\n\t\tfor(auto* shape : m_shapes.value())\n\t\t{\n\t\t\tdelete shape;\n\t\t}\n\t}\n\n\tif(m_rigid_bodies.has_value())\n\t{\n\t\tfor(auto* rigid_body : m_rigid_bodies.value())\n\t\t{\n\t\t\tdelete rigid_body->getMotionState();\n\t\t\tm_phys_engine.phys_world->removeRigidBody(rigid_body);\n\t\t\tdelete rigid_body;\n\t\t}\n\t}\n}\n\nvoid PhysicsMeshNode::SetMass(float mass)\n{\n\tm_mass = mass;\n\n\tif (m_rigid_body)\n\t{\n\t\tbtVector3 local_intertia(0, 0, 0);\n\t\tm_shape->calculateLocalInertia(mass, local_intertia);\n\t\tm_rigid_body->setMassProps(mass, local_intertia);\n\t}\n}\n\nvoid PhysicsMeshNode::SetRestitution(float value)\n{\n\tif (m_rigid_bodies.has_value())\n\t{\n\t\tfor (auto& body : m_rigid_bodies.value())\n\t\t{\n\t\t\tbody->setRestitution(value);\n\t\t}\n\t}\n\telse\n\t{\n\t\tm_rigid_body->setRestitution(value);\n\t}\n}\n\nvoid PhysicsMeshNode::SetupSimpleBoxColl(phys::PhysicsEngine& phys_engine, DirectX::XMVECTOR scale)\n{\n\tbtTransform transform;\n\ttransform.setIdentity();\n\n\tm_shape = phys_engine.CreateBoxShape(phys::util::DXV3toBV3(scale));\n\tm_rigid_body = phys_engine.CreateRigidBody(btScalar(m_mass), transform, m_shape);\n}\n\nvoid PhysicsMeshNode::SetupSimpleSphereColl(phys::PhysicsEngine& phys_engine, float radius)\n{\n\tbtTransform transform;\n\ttransform.setIdentity();\n\n\tm_shape = phys_engine.CreateSphereShape(radius);\n\tm_rigid_body = phys_engine.CreateRigidBody(btScalar(m_mass), transform, m_shape);\n}\n\nvoid PhysicsMeshNode::SetupConvex(phys::PhysicsEngine& phys_engine, wr::ModelData* model)\n{\n\tm_rigid_bodies = std::vector<btRigidBody*>();\n\tm_shapes = std::vector<btCollisionShape*>();\n\n\tauto hulls = phys_engine.CreateConvexShape(model);\n\n\tfor (auto& hull : hulls)\n\t{\n\t\tm_shapes->push_back(hull);\n\n\t\tbtTransform transform;\n\t\ttransform.setIdentity();\n\t\tauto body = phys_engine.CreateRigidBody(0.f, transform, hull);\n\t\tm_rigid_bodies->push_back(body);\n\t}\n}\n\nvoid PhysicsMeshNode::SetupTriangleMesh(phys::PhysicsEngine& phys_engine, wr::ModelData* model)\n{\n\tm_rigid_bodies = std::vector<btRigidBody*>();\n\tm_shapes = std::vector<btCollisionShape*>();\n\n\tauto hulls = phys_engine.CreateTriangleMeshShape(model);\n\n\tfor (auto& hull : hulls)\n\t{\n\t\tm_shapes->push_back(hull);\n\n\t\tbtTransform transform;\n\t\ttransform.setIdentity();\n\t\tauto body = phys_engine.CreateRigidBody(0.f, transform, hull);\n\t\tm_rigid_bodies->push_back(body);\n\t}\n}\n\nvoid PhysicsMeshNode::SetPosition(DirectX::XMVECTOR position)\n{\n\tif (m_rigid_bodies.has_value())\n\t{\n\t\tfor (auto& body : m_rigid_bodies.value())\n\t\t{\n\t\t\tauto& world_trans = body->getWorldTransform();\n\t\t\tworld_trans.setOrigin(phys::util::DXV3toBV3(position));\n\t\t}\n\t}\n\telse if (m_rigid_body)\n\t{\n\t\tauto& world_trans = m_rigid_body->getWorldTransform();\n\t\tworld_trans.setOrigin(phys::util::DXV3toBV3(position));\n\t}\n\n\tm_position = position;\n\tSignalTransformChange();\n}\n\nvoid PhysicsMeshNode::SetRotation(DirectX::XMVECTOR roll_pitch_yaw)\n{\n\tauto quat = DirectX::XMQuaternionRotationRollPitchYawFromVector(roll_pitch_yaw);\n\tif (m_rigid_bodies.has_value())\n\t{\n\t\tfor (auto& body : m_rigid_bodies.value())\n\t\t{\n\t\t\tauto& world_trans = body->getWorldTransform();\n\t\t\tworld_trans.setRotation(btQuaternion(quat.m128_f32[0], quat.m128_f32[1], quat.m128_f32[2], quat.m128_f32[3]));\n\t\t}\n\t}\n\telse if (m_rigid_body)\n\t{\n\t\tauto& world_trans = m_rigid_body->getWorldTransform();\n\t\tworld_trans.setRotation(btQuaternion(quat.m128_f32[0], quat.m128_f32[1], quat.m128_f32[2], quat.m128_f32[3]));\n\t}\n\n\tm_rotation_radians = roll_pitch_yaw;\n\tSignalTransformChange();\n}\n\nvoid PhysicsMeshNode::SetScale(DirectX::XMVECTOR scale)\n{\n\tif (m_rigid_bodies.has_value())\n\t{\n\t\tfor (auto& shape : m_shapes.value())\n\t\t{\n\t\t\tshape->setLocalScaling(phys::util::DXV3toBV3(scale));\n\t\t}\n\t}\n\telse if (m_shape)\n\t{\n\t\tm_shape->setLocalScaling(phys::util::DXV3toBV3(scale));\n\t}\n\n\tm_scale = scale;\n\tSignalTransformChange();\n}"
  },
  {
    "path": "tests/demo/physics_node.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <btBulletDynamicsCommon.h>\n#include <LinearMath/btVector3.h>\n#include <LinearMath/btAlignedObjectArray.h>\n#include <btBulletDynamicsCommon.h>\n\n#include \"scene_graph/mesh_node.hpp\"\n\nnamespace phys\n{\n\tstruct PhysicsEngine;\n}\n\nstruct PhysicsMeshNode : public wr::MeshNode\n{\n\tbtCollisionShape* m_shape = nullptr;\n\tbtRigidBody* m_rigid_body = nullptr;\n\n\tstd::optional<std::vector<btCollisionShape*>> m_shapes;\n\tstd::optional<std::vector<btRigidBody*>> m_rigid_bodies;\n\n\tphys::PhysicsEngine& m_phys_engine;\n\n\tfloat m_mass = 0;\n\n\tPhysicsMeshNode(phys::PhysicsEngine* phys_engine, wr::Model* model) : MeshNode(model), m_phys_engine(*phys_engine) { }\n\t~PhysicsMeshNode();\n\n\tvoid SetMass(float mass);\n\tvoid SetRestitution(float value);\n\n\tvoid SetupSimpleBoxColl(phys::PhysicsEngine& phys_engine, DirectX::XMVECTOR scale);\n\tvoid SetupSimpleSphereColl(phys::PhysicsEngine& phys_engine, float radius);\n\tvoid SetupConvex(phys::PhysicsEngine& phys_engine, wr::ModelData* model);\n\tvoid SetupTriangleMesh(phys::PhysicsEngine& phys_engine, wr::ModelData* model);\n\n\tvoid SetPosition(DirectX::XMVECTOR position) override;\n\tvoid SetRotation(DirectX::XMVECTOR roll_pitch_yaw) override;\n\tvoid SetScale(DirectX::XMVECTOR scale) override;\n};"
  },
  {
    "path": "tests/demo/scene_alien.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"scene_alien.hpp\"\n\nAlienScene::AlienScene() :\n\tScene(256, 20_mb, 20_mb),\n\tm_alien_model(nullptr),\n\tm_skybox({})\n{\n\tm_lights_path = \"resources/alien_lights.json\";\n}\n\nvoid AlienScene::LoadResources()\n{\n\t// Models\n\tm_alien_model = m_model_pool->LoadWithMaterials<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/alien/scene.gltf\");\n\n\t// Textures\n\tm_skybox = m_texture_pool->LoadFromFile(\"resources/materials/Ice_Lake_Ref.hdr\", false, false);\n}\n\nvoid AlienScene::BuildScene(unsigned int width, unsigned int height, void* extra)\n{\n\tm_camera = m_scene_graph->CreateChild<DebugCamera>(nullptr, 90.f, (float)width / (float)height);\n\tm_camera->SetPosition({ 1.243, 1.025, -0.640 });\n\tm_camera->SetRotation({ 1.719_deg, 117.456_deg, 0 });\n\tm_camera->SetSpeed(5);\n\n\tm_camera_spline_node = m_scene_graph->CreateChild<SplineNode>(nullptr, \"Camera Spline\", false);\n\n\tauto skybox = m_scene_graph->CreateChild<wr::SkyboxNode>(nullptr, m_skybox);\n\n\t// Geometry\n\tauto alien = m_scene_graph->CreateChild<wr::MeshNode>(nullptr, m_alien_model);\n\talien->SetPosition({ 0, -1, 0 });\n\talien->SetRotation({ 0, 90_deg, 0 });\n\talien->SetScale({ 0.01f,0.01f,0.01f });\n\n\t// Lights\n\tLoadLightsFromJSON();\n}\n\nvoid AlienScene::Update(float delta)\n{\n\tm_camera->Update(delta);\n\tm_camera_spline_node->UpdateSplineNode(delta, m_camera);\n}\n"
  },
  {
    "path": "tests/demo/scene_alien.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n#pragma once\n\n#include \"wisp.hpp\"\n#include \"window.hpp\"\n#include \"scene_graph/scene_graph.hpp\"\n#include \"imgui/imgui.hpp\"\n#include \"physics_node.hpp\"\n#include \"debug_camera.hpp\"\n#include \"spline_node.hpp\"\n#include \"../common/scene.hpp\"\n\nclass AlienScene : public Scene\n{\npublic:\n\tAlienScene();\n\n\tvoid Update(float delta = 0) final;\n\nprotected:\n\tvoid LoadResources() final;\n\tvoid BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) final;\n\nprivate:\n\t// Models\n\twr::Model* m_alien_model;\n\n\t// Textures\n\twr::TextureHandle m_skybox;\n\n\t// Nodes\n\tstd::shared_ptr<DebugCamera> m_camera;\n\tstd::shared_ptr<SplineNode> m_camera_spline_node;\n};\n"
  },
  {
    "path": "tests/demo/scene_emibl.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"scene_emibl.hpp\"\n\nEmiblScene::EmiblScene() :\n\tScene(256, 2_mb, 2_mb),\n\tm_cube_model(nullptr),\n\tm_plane_model(nullptr),\n\tm_material_knob_model(nullptr),\n\tm_rusty_metal_material(),\n\tm_knob_material(),\n\tm_skybox({})\n{\n\n}\n\nEmiblScene::~EmiblScene()\n{\n\tif (m_plane_model)\n\t{\n\t\tm_model_pool->Destroy(m_plane_model);\n\t\tm_model_pool->Destroy(m_material_knob_model);\n\t\tm_model_pool->Destroy(m_cube_model);\n\t}\n\n\tfor (wr::MaterialHandle m : m_material_handles)\n\t{\n\t\tm_material_pool->DestroyMaterial(m);\n\t}\n\tm_material_pool->DestroyMaterial(m_rusty_metal_material);\n}\n\nvoid EmiblScene::LoadResources()\n{\n\t// Textures\n\twr::TextureHandle metal_splotchy_albedo = m_texture_pool->LoadFromFile(\"resources/materials/metal-splotchy-albedo.png\", true, true);\n\twr::TextureHandle metal_splotchy_normal = m_texture_pool->LoadFromFile(\"resources/materials/metal-splotchy-normal-dx.png\", false, true);\n\twr::TextureHandle metal_splotchy_roughness = m_texture_pool->LoadFromFile(\"resources/materials/metal-splotchy-rough.png\", false, true);\n\twr::TextureHandle metal_splotchy_metallic = m_texture_pool->LoadFromFile(\"resources/materials/metal-splotchy-metal.png\", false, true);\n\n\twr::TextureHandle mahogfloor_albedo = m_texture_pool->LoadFromFile(\"resources/materials/harsh_bricks/albedo.png\", true, true);\n\twr::TextureHandle mahogfloor_normal = m_texture_pool->LoadFromFile(\"resources/materials/harsh_bricks/normal.png\", false, true);\n\twr::TextureHandle mahogfloor_roughness = m_texture_pool->LoadFromFile(\"resources/materials/harsh_bricks/roughness.png\", false, true);\n\twr::TextureHandle mahogfloor_metallic = m_texture_pool->LoadFromFile(\"resources/materials/harsh_bricks/metallic.png\", false, true);\n\twr::TextureHandle mahogfloor_ao = m_texture_pool->LoadFromFile(\"resources/materials/harsh_bricks/ao.png\", false, true);\n\n\twr::TextureHandle red_black_albedo = m_texture_pool->LoadFromFile(\"resources/materials/dragon_egg/albedo.png\", true, true);\n\twr::TextureHandle red_black_normal = m_texture_pool->LoadFromFile(\"resources/materials/dragon_egg/normal.png\", false, true);\n\twr::TextureHandle red_black_roughness = m_texture_pool->LoadFromFile(\"resources/materials/dragon_egg/roughness.png\", false, true);\n\twr::TextureHandle red_black_metallic = m_texture_pool->LoadFromFile(\"resources/materials/dragon_egg/metallic.png\", false, true);\n\n\twr::TextureHandle metal_albedo = m_texture_pool->LoadFromFile(\"resources/materials/greasy_pan/albedo.png\", true, true);\n\twr::TextureHandle metal_normal = m_texture_pool->LoadFromFile(\"resources/materials/greasy_pan/normal.png\", false, true);\n\twr::TextureHandle metal_roughness = m_texture_pool->LoadFromFile(\"resources/materials/greasy_pan/roughness.png\", false, true);\n\twr::TextureHandle metal_metallic = m_texture_pool->LoadFromFile(\"resources/materials/greasy_pan/metallic.png\", false, true);\n\n\twr::TextureHandle brick_tiles_albedo = m_texture_pool->LoadFromFile(\"resources/materials/brick_tiles/albedo.png\", true, true);\n\twr::TextureHandle brick_tiles_normal = m_texture_pool->LoadFromFile(\"resources/materials/brick_tiles/normal.png\", false, true);\n\twr::TextureHandle brick_tiles_roughness = m_texture_pool->LoadFromFile(\"resources/materials/brick_tiles/roughness.png\", false, true);\n\twr::TextureHandle brick_tiles_metallic = m_texture_pool->LoadFromFile(\"resources/materials/brick_tiles/metallic.png\", false, true);\n\n\twr::TextureHandle leather_albedo = m_texture_pool->LoadFromFile(\"resources/materials/leather_with_metal/albedo.png\", true, true);\n\twr::TextureHandle leather_normal = m_texture_pool->LoadFromFile(\"resources/materials/leather_with_metal/normal.png\", false, true);\n\twr::TextureHandle leather_roughness = m_texture_pool->LoadFromFile(\"resources/materials/leather_with_metal/roughness.png\", false, true);\n\twr::TextureHandle leather_metallic = m_texture_pool->LoadFromFile(\"resources/materials/leather_with_metal/metallic.png\", false, true);\n\n\twr::TextureHandle blue_tiles_albedo = m_texture_pool->LoadFromFile(\"resources/materials/blue_tiles/albedo.png\", true, true);\n\twr::TextureHandle blue_tiles_normal = m_texture_pool->LoadFromFile(\"resources/materials/blue_tiles/normal.png\", false, true);\n\twr::TextureHandle blue_tiles_roughness = m_texture_pool->LoadFromFile(\"resources/materials/blue_tiles/roughness.png\", false, true);\n\twr::TextureHandle blue_tiles_metallic = m_texture_pool->LoadFromFile(\"resources/materials/blue_tiles/metallic.png\", false, true);\n\n\twr::TextureHandle gold_albedo = m_texture_pool->LoadFromFile(\"resources/materials/gold_scuffed/albedo.png\", true, true);\n\twr::TextureHandle gold_normal = m_texture_pool->LoadFromFile(\"resources/materials/gold_scuffed/normal.png\", false, true);\n\twr::TextureHandle gold_roughness = m_texture_pool->LoadFromFile(\"resources/materials/gold_scuffed/roughness.png\", false, true);\n\twr::TextureHandle gold_metallic = m_texture_pool->LoadFromFile(\"resources/materials/gold_scuffed/metallic.png\", false, true);\n\n\twr::TextureHandle marble_albedo = m_texture_pool->LoadFromFile(\"resources/materials/marble_speckled/albedo.png\", true, true);\n\twr::TextureHandle marble_normal = m_texture_pool->LoadFromFile(\"resources/materials/marble_speckled/normal.png\", false, true);\n\twr::TextureHandle marble_roughness = m_texture_pool->LoadFromFile(\"resources/materials/marble_speckled/roughness.png\", false, true);\n\twr::TextureHandle marble_metallic = m_texture_pool->LoadFromFile(\"resources/materials/marble_speckled/metallic.png\", false, true);\n\n\n\twr::TextureHandle floreal_tiles_albedo = m_texture_pool->LoadFromFile(\"resources/materials/floreal_tiles/albedo.png\", true, true);\n\twr::TextureHandle floreal_tiles_normal = m_texture_pool->LoadFromFile(\"resources/materials/floreal_tiles/normal.png\", false, true);\n\twr::TextureHandle floreal_tiles_roughness = m_texture_pool->LoadFromFile(\"resources/materials/floreal_tiles/roughness.png\", false, true);\n\twr::TextureHandle floreal_tiles_metallic = m_texture_pool->LoadFromFile(\"resources/materials/floreal_tiles/metallic.png\", false, true);\n\n\twr::TextureHandle bw_tiles_albedo = m_texture_pool->LoadFromFile(\"resources/materials/bw_tiles_gold_lining/albedo.png\", true, true);\n\twr::TextureHandle bw_tiles_normal = m_texture_pool->LoadFromFile(\"resources/materials/bw_tiles_gold_lining/normal.png\", false, true);\n\twr::TextureHandle bw_tiles_roughness = m_texture_pool->LoadFromFile(\"resources/materials/bw_tiles_gold_lining/roughness.png\", false, true);\n\twr::TextureHandle bw_tiles_metallic = m_texture_pool->LoadFromFile(\"resources/materials/bw_tiles_gold_lining/metallic.png\", false, true);\n\twr::TextureHandle bw_tiles_emissive = m_texture_pool->LoadFromFile(\"resources/materials/bw_tiles_gold_lining/emissive.png\", true, true);\n\n\tm_skybox = m_texture_pool->LoadFromFile(\"resources/materials/Arches_E_PineTree_3k.hdr\", false, false);\n\n\t{\n\t\t// Create Material\n\t\tm_rusty_metal_material = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* rusty_metal_internal = m_material_pool->GetMaterial(m_rusty_metal_material);\n\n\t\trusty_metal_internal->SetTexture(wr::TextureType::ALBEDO, metal_splotchy_albedo);\n\t\trusty_metal_internal->SetTexture(wr::TextureType::NORMAL, metal_splotchy_normal);\n\t\trusty_metal_internal->SetTexture(wr::TextureType::ROUGHNESS, metal_splotchy_roughness);\n\t\trusty_metal_internal->SetTexture(wr::TextureType::METALLIC, metal_splotchy_metallic);\n\n\t\t// Create Material\n\t\tm_material_handles[0] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* mahogfloor_material_internal = m_material_pool->GetMaterial(m_material_handles[0]);\n\n\t\tmahogfloor_material_internal->SetTexture(wr::TextureType::ALBEDO, mahogfloor_albedo);\n\t\tmahogfloor_material_internal->SetTexture(wr::TextureType::NORMAL, mahogfloor_normal);\n\t\tmahogfloor_material_internal->SetTexture(wr::TextureType::ROUGHNESS, mahogfloor_roughness);\n\t\tmahogfloor_material_internal->SetTexture(wr::TextureType::METALLIC, mahogfloor_metallic);\n\t\tmahogfloor_material_internal->SetTexture(wr::TextureType::AO, mahogfloor_ao);\n\n\t\t// Create Material\n\t\tm_material_handles[1] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* red_black_pattern_internal = m_material_pool->GetMaterial(m_material_handles[1]);\n\n\t\tred_black_pattern_internal->SetTexture(wr::TextureType::ALBEDO, red_black_albedo);\n\t\tred_black_pattern_internal->SetTexture(wr::TextureType::NORMAL, red_black_normal);\n\t\tred_black_pattern_internal->SetTexture(wr::TextureType::ROUGHNESS, red_black_roughness);\n\t\tred_black_pattern_internal->SetTexture(wr::TextureType::METALLIC, red_black_metallic);\n\n\t\t// Create Material\n\t\tm_material_handles[2] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* metal_material_internal = m_material_pool->GetMaterial(m_material_handles[2]);\n\n\t\tmetal_material_internal->SetTexture(wr::TextureType::ALBEDO, metal_albedo);\n\t\tmetal_material_internal->SetTexture(wr::TextureType::NORMAL, metal_normal);\n\t\tmetal_material_internal->SetTexture(wr::TextureType::ROUGHNESS, metal_roughness);\n\t\tmetal_material_internal->SetTexture(wr::TextureType::METALLIC, metal_metallic);\n\n\t\tm_material_handles[3] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* brick_tiles_mat_internal = m_material_pool->GetMaterial(m_material_handles[3]);\n\n\t\tbrick_tiles_mat_internal->SetTexture(wr::TextureType::ALBEDO, brick_tiles_albedo);\n\t\tbrick_tiles_mat_internal->SetTexture(wr::TextureType::NORMAL, brick_tiles_normal);\n\t\tbrick_tiles_mat_internal->SetTexture(wr::TextureType::ROUGHNESS, brick_tiles_roughness);\n\t\tbrick_tiles_mat_internal->SetTexture(wr::TextureType::METALLIC, brick_tiles_metallic);\n\n\t\tm_material_handles[4] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* leather_material_internal = m_material_pool->GetMaterial(m_material_handles[4]);\n\n\t\tleather_material_internal->SetTexture(wr::TextureType::ALBEDO, leather_albedo);\n\t\tleather_material_internal->SetTexture(wr::TextureType::NORMAL, leather_normal);\n\t\tleather_material_internal->SetTexture(wr::TextureType::ROUGHNESS, leather_roughness);\n\t\tleather_material_internal->SetTexture(wr::TextureType::METALLIC, leather_metallic);\n\n\t\tm_material_handles[5] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* blue_tiles_material_internal = m_material_pool->GetMaterial(m_material_handles[5]);\n\n\t\tblue_tiles_material_internal->SetTexture(wr::TextureType::ALBEDO, blue_tiles_albedo);\n\t\tblue_tiles_material_internal->SetTexture(wr::TextureType::NORMAL, blue_tiles_normal);\n\t\tblue_tiles_material_internal->SetTexture(wr::TextureType::ROUGHNESS, blue_tiles_roughness);\n\t\tblue_tiles_material_internal->SetTexture(wr::TextureType::METALLIC, blue_tiles_metallic);\n\n\t\tm_material_handles[6] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* gold_material_internal = m_material_pool->GetMaterial(m_material_handles[6]);\n\n\t\tgold_material_internal->SetTexture(wr::TextureType::ALBEDO, gold_albedo);\n\t\tgold_material_internal->SetTexture(wr::TextureType::NORMAL, gold_normal);\n\t\tgold_material_internal->SetTexture(wr::TextureType::ROUGHNESS, gold_roughness);\n\t\tgold_material_internal->SetTexture(wr::TextureType::METALLIC, gold_metallic);\n\n\t\tm_material_handles[7] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* marble_material_internal = m_material_pool->GetMaterial(m_material_handles[7]);\n\n\t\tmarble_material_internal->SetTexture(wr::TextureType::ALBEDO, marble_albedo);\n\t\tmarble_material_internal->SetTexture(wr::TextureType::NORMAL, marble_normal);\n\t\tmarble_material_internal->SetTexture(wr::TextureType::ROUGHNESS, marble_roughness);\n\t\tmarble_material_internal->SetTexture(wr::TextureType::METALLIC, marble_metallic);\n\n\t\tm_material_handles[8] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* floreal_tiles_internal = m_material_pool->GetMaterial(m_material_handles[8]);\n\n\t\tfloreal_tiles_internal->SetTexture(wr::TextureType::ALBEDO, floreal_tiles_albedo);\n\t\tfloreal_tiles_internal->SetTexture(wr::TextureType::NORMAL, floreal_tiles_normal);\n\t\tfloreal_tiles_internal->SetTexture(wr::TextureType::ROUGHNESS, floreal_tiles_roughness);\n\t\tfloreal_tiles_internal->SetTexture(wr::TextureType::METALLIC, floreal_tiles_metallic);\n\n\t\tm_material_handles[9] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* bw_tiles_internal = m_material_pool->GetMaterial(m_material_handles[9]);\n\n\t\tbw_tiles_internal->SetTexture(wr::TextureType::ALBEDO, bw_tiles_albedo);\n\t\tbw_tiles_internal->SetTexture(wr::TextureType::NORMAL, bw_tiles_normal);\n\t\tbw_tiles_internal->SetTexture(wr::TextureType::ROUGHNESS, bw_tiles_roughness);\n\t\tbw_tiles_internal->SetTexture(wr::TextureType::METALLIC, bw_tiles_metallic);\n\t\tbw_tiles_internal->SetTexture(wr::TextureType::EMISSIVE, bw_tiles_emissive);\n\n\t\tm_material_handles[10] = m_material_pool->Create(m_texture_pool.get());\n\n\t\twr::Material* mirror_internal = m_material_pool->GetMaterial(m_material_handles[10]);\n\n\t\tmirror_internal->SetConstant<wr::MaterialConstant::COLOR>({ 1.f, 1.f, 1.f });\n\t\tmirror_internal->SetConstant<wr::MaterialConstant::METALLIC>(1.f);\n\t\tmirror_internal->SetConstant<wr::MaterialConstant::ROUGHNESS>(0.f);\n\t}\n\n\t{\n\t\twr::MeshData<wr::Vertex> mesh;\n\n\t\tmesh.m_indices = {\n\t\t\t2, 1, 0, 3, 2, 0\n\t\t};\n\n\t\tmesh.m_vertices = {\n\t\t\t//POS                UV           NORMAL               TANGENT         BINORMAL\n\t\t\t{  1,  1,  0,        1, 1,        0, 0, -1,            0, 0, 1,        0, 1, 0, },\n\t\t\t{  1, -1,  0,        1, 0,        0, 0, -1,            0, 0, 1,        0, 1, 0, },\n\t\t\t{ -1, -1,  0,        0, 0,        0, 0, -1,            0, 0, 1,        0, 1, 0, },\n\t\t\t{ -1,  1,  0,        0, 1,        0, 0, -1,            0, 0, 1,        0, 1, 0, },\n\t\t};\n\n\t\tm_plane_model = m_model_pool->Load<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/plane.fbx\");\n\n\t\tfor (auto& m : m_plane_model->m_meshes)\n\t\t{\n\t\t\tm.second = m_material_handles[0];\n\t\t}\n\t}\n\n\t{\n\t\t{\n\t\t\tm_cube_model = m_model_pool->Load<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/cube.fbx\");\n\t\t\tfor (auto& m : m_cube_model->m_meshes)\n\t\t\t{\n\t\t\t\tm.second = m_material_handles[0];\n\t\t\t}\n\n\t\t}\n\n\t\t{\n\t\t\tm_material_knob_model = m_model_pool->Load<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/material_ball.fbx\");\n\t\t\tfor (auto& m : m_material_knob_model->m_meshes)\n\t\t\t{\n\t\t\t\tm.second = m_material_handles[0];\n\t\t\t}\n\t\t}\n\t}\n}\n\nvoid EmiblScene::BuildScene(unsigned int width, unsigned int height, void* extra)\n{\n\tm_camera = m_scene_graph->CreateChild<DebugCamera>(nullptr, 90.f, (float)width / (float)height);\n\tm_camera->SetPosition({ 500.0f, 60.0f, 260.0f });\n\tm_camera->SetRotation({ -16._deg, 0._deg, 0._deg });\n\tm_camera->SetSpeed(100.0f);\n\n\tauto skybox = m_scene_graph->CreateChild<wr::SkyboxNode>(nullptr, m_skybox);\n\n\t// Geometry\n\tfor (size_t i = 0; i < 10; ++i)\n\t{\n\t\tstd::vector<wr::MaterialHandle> mat_handles(m_material_knob_model->m_meshes.size(), m_material_handles[i]);\n\n\t\tm_models[i] = m_scene_graph->CreateChild<wr::MeshNode>(nullptr, m_material_knob_model);\n\t\tm_models[i]->SetMaterials(mat_handles);\n\t\tm_platforms[i] = m_scene_graph->CreateChild<wr::MeshNode>(nullptr, m_cube_model);\n\t\tm_platforms[i]->SetMaterials(mat_handles);\n\t\tm_platforms[i]->SetScale({ 38, 1, 38 });\n\t}\n\n\tm_models[9]->SetPosition({ -500,\t0\t,\t-160 });\n\tm_platforms[9]->SetPosition({ -500, -3, -160 });\n\n\tm_models[8]->SetPosition({ -250,\t0\t,\t-160 });\n\tm_platforms[8]->SetPosition({ -250,\t-3\t,\t-160 });\n\n\tm_models[7]->SetPosition({ 0,\t\t0\t,\t-160 });\n\tm_platforms[7]->SetPosition({ 0,\t\t-3\t,\t-160 });\n\n\tm_models[6]->SetPosition({ +250,\t\t0\t,-160 });\n\tm_platforms[6]->SetPosition({ +250,\t\t-3\t,-160 });\n\n\tm_models[5]->SetPosition({ +500,\t\t0\t,\t-160 });\n\tm_platforms[5]->SetPosition({ +500,\t\t-3\t,\t-160 });\n\n\tm_models[4]->SetPosition({ -500,0\t,\t160 });\n\tm_platforms[4]->SetPosition({ -500, -3 ,\t160 });\n\n\tm_models[3]->SetPosition({ -250,\t\t0\t,\t160 });\n\tm_platforms[3]->SetPosition({ -250,\t\t-3\t,\t160 });\n\n\tm_models[2]->SetPosition({ 0,\t\t\t\t0\t,160 });\n\tm_platforms[2]->SetPosition({ 0,\t\t\t\t-3\t,160 });\n\n\tm_models[1]->SetPosition({ +250,0\t,\t160 });\n\tm_platforms[1]->SetPosition({ +250, -3\t,\t160 });\n\n\tm_models[0]->SetPosition({ +500,\t\t0\t,\t160 });\n\tm_platforms[0]->SetPosition({ +500,\t\t-3\t,\t160 });\n\n\tauto dir_light = m_scene_graph->CreateChild<wr::LightNode>(nullptr, wr::LightType::DIRECTIONAL, DirectX::XMVECTOR{ 0, 1, 0 });\n\tdir_light->SetDirectional({ 360_deg - 136_deg, 0, 0 }, { 4, 4, 4 });\n}\n\nvoid EmiblScene::Update(float delta)\n{\n\tm_camera->Update(delta);\n}"
  },
  {
    "path": "tests/demo/scene_emibl.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n#pragma once\n\n#include \"wisp.hpp\"\n#include \"window.hpp\"\n#include \"scene_graph/scene_graph.hpp\"\n#include \"imgui/imgui.hpp\"\n#include \"debug_camera.hpp\"\n#include \"../common/scene.hpp\"\n\nclass EmiblScene : public Scene\n{\npublic:\n\tEmiblScene();\n\t~EmiblScene();\n\n\tvoid Update(float delta = 0) final;\n\nprotected:\n\tvoid LoadResources() final;\n\tvoid BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) final;\n\nprivate:\n\t// Models\n\twr::Model* m_cube_model;\n\twr::Model* m_plane_model;\n\twr::Model* m_material_knob_model;\n\n\t// Textures\n\twr::TextureHandle m_skybox;\n\n\t// Materials\n\twr::MaterialHandle m_rusty_metal_material;\n\twr::MaterialHandle m_knob_material;\n\twr::MaterialHandle m_material_handles[11];\n\n\t// Nodes\n\tstd::shared_ptr<DebugCamera> m_camera;\n\tstd::shared_ptr<wr::MeshNode> m_models[10];\n\tstd::shared_ptr<wr::MeshNode> m_platforms[10];\n};"
  },
  {
    "path": "tests/demo/scene_sponza.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"scene_sponza.hpp\"\n\nstatic constexpr bool spawn_physics_balls = false;\nstatic constexpr int num_materials = 30;\nstatic constexpr int num_balls = 600;\n\nSponzaScene::SponzaScene() :\n\tScene(256, 20_mb, 20_mb),\n\tm_sponza_model(nullptr),\n\tm_skybox({}),\n\tm_time(0)\n{\n\tm_lights_path = \"resources/sponza_lights.json\";\n}\n\n\ninline float RandRange(float min, float max)\n{\n\treturn min + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (max - min)));\n}\n\ninline float RandRangeI(float min, float max)\n{\n\treturn min + static_cast <int> (rand()) / (static_cast <int> (RAND_MAX / (max - min)));\n}\n\nSponzaScene::~SponzaScene()\n{\n\tif(m_sphere_model)\n\t{\n\t\tm_model_pool->Destroy(m_sphere_model);\n\t\tm_model_pool->Destroy(m_sponza_model);\n\t\tfor(auto& material : m_mirror_materials)\n\t\t{\n\t\t\tm_material_pool->DestroyMaterial(material);\n\t\t}\n\t}\n}\n\nvoid SponzaScene::LoadResources()\n{\n\t// Models\n\tm_sphere_model = m_model_pool->Load<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/sphere.fbx\");\n\tm_sponza_model = m_model_pool->LoadWithMaterials<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/sponza/sponza.obj\", false, &m_sponza_model_data);\n\n\t// Textures\n\tm_skybox = m_texture_pool->LoadFromFile(\"resources/materials/Barce_Rooftop_C_3k.hdr\", false, false);\n\n\t// Materials\n\tfor (auto i = 0; i < num_materials; i++)\n\t{\n\t\tauto mat = m_material_pool->Create(m_texture_pool.get());\n\t\twr::Material* mat_internal = m_material_pool->GetMaterial(mat);\n\t\tmat_internal->SetConstant<wr::MaterialConstant::ROUGHNESS>(RandRange(0, 0.5));\n\t\tmat_internal->SetConstant<wr::MaterialConstant::METALLIC>(RandRange(0.5, 1));\n\t\tmat_internal->SetConstant<wr::MaterialConstant::EMISSIVE_MULTIPLIER>(RandRange(0, 1) > 0.8 ? 5 : 0);\n\t\tmat_internal->SetConstant<wr::MaterialConstant::COLOR>({ RandRange(0.3, 1), RandRange(0.3, 1), RandRange(0.3, 1) });\n\t\tm_mirror_materials.push_back(mat);\n\t}\n}\n\nvoid SponzaScene::BuildScene(unsigned int width, unsigned int height, void* extra)\n{\n\tauto& phys_engine = *reinterpret_cast<phys::PhysicsEngine*>(extra);\n\n\tm_camera = m_scene_graph->CreateChild<DebugCamera>(nullptr, 90.f, (float)width / (float)height);\n\tm_camera->SetPosition({ 0, 1, 11 });\n\tm_camera->SetSpeed(10);\n\n\tm_camera_spline_node = m_scene_graph->CreateChild<SplineNode>(nullptr, \"Camera Spline\", false);\n\n\tauto skybox = m_scene_graph->CreateChild<wr::SkyboxNode>(nullptr, m_skybox);\n\n\t// Geometry\n\tm_sponza_node = m_scene_graph->CreateChild<PhysicsMeshNode>(nullptr, &phys_engine, m_sponza_model);\n\tm_sponza_node->SetupConvex(phys_engine, m_sponza_model_data);\n\tm_sponza_node->SetRestitution(1.f);\n\tm_sponza_node->SetPosition({ 0, -1, 0 });\n\tm_sponza_node->SetRotation({ 0, 90_deg, 0 });\n\tm_sponza_node->SetScale({ 0.01f,0.01f,0.01f });\n\n\tif constexpr (spawn_physics_balls)\n\t{\n\t\t// Left Ballfall\n\t\tfor (auto i = 0; i < num_balls / 2; i++)\n\t\t{\n\t\t\tauto ball = m_scene_graph->CreateChild<PhysicsMeshNode>(nullptr, &phys_engine, m_sphere_model);\n\t\t\tball->SetMass(0.004f);\n\t\t\tball->SetupSimpleSphereColl(phys_engine, 1.f);\n\t\t\tball->m_rigid_body->setRestitution(0.5);\n\t\t\tball->m_rigid_body->setFriction(0.2);\n\t\t\tball->m_rigid_body->setLinearVelocity({ 2, 0, 0 });\n\t\t\tball->m_rigid_body->setRollingFriction(0);\n\t\t\tball->m_rigid_body->setSpinningFriction(0);\n\t\t\tball->SetPosition({ -2.440, 5.5, RandRange(-7.7, 8.8) });\n\t\t\tball->SetScale({ 0.2f, 0.2f, 0.2f });\n\t\t\tball->AddMaterial(m_mirror_materials[RandRangeI(0, num_materials - 1)]);\n\t\t\tball->m_rigid_body->activate(true);\n\t\t}\n\t\t// Right Ballfall\n\t\tfor (auto i = 0; i < num_balls / 2; i++)\n\t\t{\n\t\t\tauto ball = m_scene_graph->CreateChild<PhysicsMeshNode>(nullptr, &phys_engine, m_sphere_model);\n\t\t\tball->SetMass(0.004f);\n\t\t\tball->SetupSimpleSphereColl(phys_engine, 1.f);\n\t\t\tball->m_rigid_body->setRestitution(0.5);\n\t\t\tball->m_rigid_body->setFriction(0.2);\n\t\t\tball->m_rigid_body->setLinearVelocity({ -2, 0, 0 });\n\t\t\tball->m_rigid_body->setRollingFriction(0);\n\t\t\tball->m_rigid_body->setSpinningFriction(0);\n\t\t\tball->SetPosition({ 2.440, 5.5, RandRange(-7.7, 8.8) });\n\t\t\tball->SetScale({ 0.2f, 0.2f, 0.2f });\n\t\t\tball->AddMaterial(m_mirror_materials[RandRangeI(0, num_materials - 1)]);\n\t\t\tball->m_rigid_body->activate(true);\n\t\t}\n\t}\n\n\t// Lights\n\tLoadLightsFromJSON();\n}\n\nvoid SponzaScene::Update(float delta)\n{\n\tm_camera->Update(delta);\n\tm_camera_spline_node->UpdateSplineNode(delta, m_camera);\n}"
  },
  {
    "path": "tests/demo/scene_sponza.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n#pragma once\n\n#include \"wisp.hpp\"\n#include \"window.hpp\"\n#include \"scene_graph/scene_graph.hpp\"\n#include \"imgui/imgui.hpp\"\n#include \"physics_node.hpp\"\n#include \"debug_camera.hpp\"\n#include \"spline_node.hpp\"\n#include \"../common/scene.hpp\"\n\nclass SponzaScene : public Scene\n{\npublic:\n\tSponzaScene();\n\t~SponzaScene();\n\n\tvoid Update(float delta = 0) final;\n\nprotected:\n\tvoid LoadResources() final;\n\tvoid BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) final;\n\nprivate:\n\t// Models\n\twr::Model* m_sphere_model;\n\twr::Model* m_sponza_model;\n\twr::ModelData* m_sponza_model_data;\n\n\t// Textures\n\twr::TextureHandle m_skybox;\n\n\t// Materials\n\tstd::vector<wr::MaterialHandle> m_mirror_materials;\n\n\t// Nodes\n\tstd::shared_ptr<DebugCamera> m_camera;\n\tstd::shared_ptr<SplineNode> m_camera_spline_node;\n\tstd::shared_ptr<PhysicsMeshNode> m_sponza_node;\n\n\tfloat m_time;\n};"
  },
  {
    "path": "tests/demo/scene_viknell.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include \"scene_viknell.hpp\"\n\nViknellScene::ViknellScene() :\n\tScene(256, 2_mb, 2_mb),\n\tm_sphere_model(nullptr),\n\tm_plane_model(nullptr),\n\tm_xbot_model(nullptr),\n\tm_bamboo_material(),\n\tm_skybox({}),\n\tm_plane_model_data(nullptr),\n\tm_time(0)\n{\n\tm_lights_path = \"resources/viknell_lights.json\";\n}\n\nViknellScene::~ViknellScene()\n{\n\tif(m_plane_model)\n\t{\n\t\tm_model_pool->Destroy(m_plane_model);\n\t\tm_model_pool->Destroy(m_xbot_model);\n\t\tm_model_pool->Destroy(m_sphere_model);\n\t\tm_material_pool->DestroyMaterial(m_bamboo_material);\n\t\tm_material_pool->DestroyMaterial(m_mirror_material);\n\t}\n}\n\nvoid ViknellScene::LoadResources()\n{\n\t// Models\n\tm_plane_model = m_model_pool->Load<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/plane.fbx\", &m_plane_model_data);\n\tm_xbot_model = m_model_pool->LoadWithMaterials<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/xbot.fbx\");\n\tm_sphere_model = m_model_pool->Load<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/sphere.fbx\");\n\n\t// Textures\n\twr::TextureHandle bamboo_albedo = m_texture_pool->LoadFromFile(\"resources/materials/bamboo/bamboo-wood-semigloss-albedo.png\", true, true);\n\twr::TextureHandle bamboo_normal = m_texture_pool->LoadFromFile(\"resources/materials/bamboo/bamboo-wood-semigloss-normal.png\", false, true);\n\twr::TextureHandle bamboo_roughness = m_texture_pool->LoadFromFile(\"resources/materials/bamboo/bamboo-wood-semigloss-roughness.png\", false, true);\n\twr::TextureHandle bamboo_metallic = m_texture_pool->LoadFromFile(\"resources/materials/bamboo/bamboo-wood-semigloss-metal.png\", false, true);\n\tm_skybox = m_texture_pool->LoadFromFile(\"resources/materials/Barce_Rooftop_C_3k.hdr\", false, false);\n\n\t// Materials\n\tm_mirror_material = m_material_pool->Create(m_texture_pool.get());\n\twr::Material* mirror_internal = m_material_pool->GetMaterial(m_mirror_material);\n\tmirror_internal->SetConstant<wr::MaterialConstant::ROUGHNESS>(0);\n\tmirror_internal->SetConstant<wr::MaterialConstant::METALLIC>(1);\n\tmirror_internal->SetConstant<wr::MaterialConstant::COLOR>({ 1, 1, 1 });\n\n\tm_bamboo_material = m_material_pool->Create(m_texture_pool.get());\n\twr::Material* bamboo_material_internal = m_material_pool->GetMaterial(m_bamboo_material);\n\tbamboo_material_internal->SetTexture(wr::TextureType::ALBEDO, bamboo_albedo);\n\tbamboo_material_internal->SetTexture(wr::TextureType::NORMAL, bamboo_normal);\n\tbamboo_material_internal->SetTexture(wr::TextureType::ROUGHNESS, bamboo_roughness);\n\tbamboo_material_internal->SetTexture(wr::TextureType::METALLIC, bamboo_metallic);\n}\n\nvoid ViknellScene::BuildScene(unsigned int width, unsigned int height, void* extra)\n{\n\tauto& phys_engine = *reinterpret_cast<phys::PhysicsEngine*>(extra);\n\n\tm_camera = m_scene_graph->CreateChild<DebugCamera>(nullptr, 90.f, (float)width / (float)height);\n\tm_camera->SetPosition({ 0, 0, 2 });\n\tm_camera->SetSpeed(10);\n\n\tm_camera_spline_node = m_scene_graph->CreateChild<SplineNode>(nullptr, \"Camera Spline\", false);\n\n\tauto skybox = m_scene_graph->CreateChild<wr::SkyboxNode>(nullptr, m_skybox);\n\n\t// Geometry\n\tauto floor = m_scene_graph->CreateChild<PhysicsMeshNode>(nullptr, &phys_engine, m_plane_model);\n\tauto roof = m_scene_graph->CreateChild<wr::MeshNode>(nullptr, m_plane_model);\n\tauto back_wall = m_scene_graph->CreateChild<wr::MeshNode>(nullptr, m_plane_model);\n\tauto left_wall = m_scene_graph->CreateChild<wr::MeshNode>(nullptr, m_plane_model);\n\tauto right_wall = m_scene_graph->CreateChild<wr::MeshNode>(nullptr, m_plane_model);\n\tm_xbot_node = m_scene_graph->CreateChild<PhysicsMeshNode>(nullptr, &phys_engine, m_xbot_model);\n\tauto sphere = m_scene_graph->CreateChild<wr::MeshNode>(nullptr, m_sphere_model);\n\n\tfloor->SetupConvex(phys_engine, m_plane_model_data);\n\tfloor->SetRestitution(1.f);\n\tfloor->SetPosition({ 0, -1, 0 });\n\tfloor->SetRotation({ 90_deg, 0, 0 });\n\tfloor->AddMaterial(m_bamboo_material);\n\tsphere->SetPosition({ 1, -1, -1 });\n\tsphere->SetScale({ 0.6f, 0.6f, 0.6f });\n\tsphere->AddMaterial(m_mirror_material);\n\troof->SetPosition({ 0, 1, 0 });\n\troof->SetRotation({ -90_deg, 0, 0 });\n\troof->AddMaterial(m_bamboo_material);\n\tback_wall->SetPosition({ 0, 0, -1 });\n\tback_wall->SetRotation({ 0, 180_deg, 0 });\n\tback_wall->AddMaterial(m_bamboo_material);\n\tleft_wall->SetPosition({ -1, 0, 0 });\n\tleft_wall->SetRotation({ 0, -90_deg, 0 });\n\tleft_wall->AddMaterial(m_bamboo_material);\n\tright_wall->SetPosition({ 1, 0, 0 });\n\tright_wall->SetRotation({ 0, 90_deg, 0 });\n\tright_wall->AddMaterial(m_bamboo_material);\n\n\tm_xbot_node->SetMass(0.01f);\n\tm_xbot_node->SetupSimpleSphereColl(phys_engine, 0.5);\n\tm_xbot_node->m_rigid_body->setRestitution(0.4);\n\tm_xbot_node->m_rigid_body->setFriction(0);\n\tm_xbot_node->m_rigid_body->setRollingFriction(0);\n\tm_xbot_node->m_rigid_body->setSpinningFriction(0);\n\tm_xbot_node->SetPosition({ 0, -1, 0 });\n\tm_xbot_node->SetRotation({ 0, 180_deg, 0 });\n\tm_xbot_node->SetScale({ 0.01f,0.01f,0.01f });\n\tm_xbot_node->m_rigid_body->activate(true);\n\n\t// Lights\n\t/*auto point_light_0 = m_scene_graph->CreateChild<wr::LightNode>(nullptr, wr::LightType::DIRECTIONAL, DirectX::XMVECTOR{ 1, 1, 1 });\n\tpoint_light_0->SetRotation({ 20.950f, 0.98f, 0.f });\n\tpoint_light_0->SetPosition({ -0.002f, 0.080f, 1.404f });\n\n\tauto point_light_1 = m_scene_graph->CreateChild<wr::LightNode>(nullptr, wr::LightType::POINT, DirectX::XMVECTOR{ 1, 0, 0 });\n\tpoint_light_1->SetRadius(5.0f);\n\tpoint_light_1->SetPosition({ 0.5f, 0.f, -0.3f });\n\n\tauto point_light_2 = m_scene_graph->CreateChild<wr::LightNode>(nullptr, wr::LightType::POINT, DirectX::XMVECTOR{ 0, 0, 1 });\n\tpoint_light_2->SetRadius(5.0f);\n\tpoint_light_2->SetPosition({ -0.5f, 0.5f, -0.3f });*/\n\n\tLoadLightsFromJSON();\n}\n\nvoid ViknellScene::Update(float delta)\n{\n\tm_camera->Update(delta);\n\tm_camera_spline_node->UpdateSplineNode(ImGui::GetIO().DeltaTime, m_camera);\n}"
  },
  {
    "path": "tests/demo/scene_viknell.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n#pragma once\n\n#include \"wisp.hpp\"\n#include \"window.hpp\"\n#include \"scene_graph/scene_graph.hpp\"\n#include \"imgui/imgui.hpp\"\n#include \"physics_node.hpp\"\n#include \"debug_camera.hpp\"\n#include \"spline_node.hpp\"\n#include \"../common/scene.hpp\"\n\nclass ViknellScene : public Scene\n{\npublic:\n\tViknellScene();\n\t~ViknellScene();\n\n\tvoid Update(float delta = 0) final;\n\nprotected:\n\tvoid LoadResources() final;\n\tvoid BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) final;\n\nprivate:\n\t// Models\n\twr::Model* m_sphere_model;\n\twr::Model* m_plane_model;\n\twr::ModelData* m_plane_model_data;\n\twr::Model* m_xbot_model;\n\n\t// Textures\n\twr::TextureHandle m_skybox;\n\n\t// Materials\n\twr::MaterialHandle m_bamboo_material;\n\twr::MaterialHandle m_mirror_material;\n\n\t// Nodes\n\tstd::shared_ptr<DebugCamera> m_camera;\n\tstd::shared_ptr<SplineNode> m_camera_spline_node;\n\tstd::shared_ptr<PhysicsMeshNode> m_xbot_node;\n\n\tfloat m_time;\n};"
  },
  {
    "path": "tests/demo/spline_library/LICENSE",
    "content": "Mozilla Public License, version 2.0\n\n1. Definitions\n\n1.1. “Contributor”\n\n     means each individual or legal entity that creates, contributes to the\n     creation of, or owns Covered Software.\n\n1.2. “Contributor Version”\n\n     means the combination of the Contributions of others (if any) used by a\n     Contributor and that particular Contributor’s Contribution.\n\n1.3. “Contribution”\n\n     means Covered Software of a particular Contributor.\n\n1.4. “Covered Software”\n\n     means Source Code Form to which the initial Contributor has attached the\n     notice in Exhibit A, the Executable Form of such Source Code Form, and\n     Modifications of such Source Code Form, in each case including portions\n     thereof.\n\n1.5. “Incompatible With Secondary Licenses”\n     means\n\n     a. that the initial Contributor has attached the notice described in\n        Exhibit B to the Covered Software; or\n\n     b. that the Covered Software was made available under the terms of version\n        1.1 or earlier of the License, but not also under the terms of a\n        Secondary License.\n\n1.6. “Executable Form”\n\n     means any form of the work other than Source Code Form.\n\n1.7. “Larger Work”\n\n     means a work that combines Covered Software with other material, in a separate\n     file or files, that is not Covered Software.\n\n1.8. “License”\n\n     means this document.\n\n1.9. “Licensable”\n\n     means having the right to grant, to the maximum extent possible, whether at the\n     time of the initial grant or subsequently, any and all of the rights conveyed by\n     this License.\n\n1.10. “Modifications”\n\n     means any of the following:\n\n     a. any file in Source Code Form that results from an addition to, deletion\n        from, or modification of the contents of Covered Software; or\n\n     b. any new file in Source Code Form that contains any Covered Software.\n\n1.11. “Patent Claims” of a Contributor\n\n      means any patent claim(s), including without limitation, method, process,\n      and apparatus claims, in any patent Licensable by such Contributor that\n      would be infringed, but for the grant of the License, by the making,\n      using, selling, offering for sale, having made, import, or transfer of\n      either its Contributions or its Contributor Version.\n\n1.12. “Secondary License”\n\n      means either the GNU General Public License, Version 2.0, the GNU Lesser\n      General Public License, Version 2.1, the GNU Affero General Public\n      License, Version 3.0, or any later versions of those licenses.\n\n1.13. “Source Code Form”\n\n      means the form of the work preferred for making modifications.\n\n1.14. “You” (or “Your”)\n\n      means an individual or a legal entity exercising rights under this\n      License. For legal entities, “You” includes any entity that controls, is\n      controlled by, or is under common control with You. For purposes of this\n      definition, “control” means (a) the power, direct or indirect, to cause\n      the direction or management of such entity, whether by contract or\n      otherwise, or (b) ownership of more than fifty percent (50%) of the\n      outstanding shares or beneficial ownership of such entity.\n\n\n2. License Grants and Conditions\n\n2.1. Grants\n\n     Each Contributor hereby grants You a world-wide, royalty-free,\n     non-exclusive license:\n\n     a. under intellectual property rights (other than patent or trademark)\n        Licensable by such Contributor to use, reproduce, make available,\n        modify, display, perform, distribute, and otherwise exploit its\n        Contributions, either on an unmodified basis, with Modifications, or as\n        part of a Larger Work; and\n\n     b. under Patent Claims of such Contributor to make, use, sell, offer for\n        sale, have made, import, and otherwise transfer either its Contributions\n        or its Contributor Version.\n\n2.2. Effective Date\n\n     The licenses granted in Section 2.1 with respect to any Contribution become\n     effective for each Contribution on the date the Contributor first distributes\n     such Contribution.\n\n2.3. Limitations on Grant Scope\n\n     The licenses granted in this Section 2 are the only rights granted under this\n     License. No additional rights or licenses will be implied from the distribution\n     or licensing of Covered Software under this License. Notwithstanding Section\n     2.1(b) above, no patent license is granted by a Contributor:\n\n     a. for any code that a Contributor has removed from Covered Software; or\n\n     b. for infringements caused by: (i) Your and any other third party’s\n        modifications of Covered Software, or (ii) the combination of its\n        Contributions with other software (except as part of its Contributor\n        Version); or\n\n     c. under Patent Claims infringed by Covered Software in the absence of its\n        Contributions.\n\n     This License does not grant any rights in the trademarks, service marks, or\n     logos of any Contributor (except as may be necessary to comply with the\n     notice requirements in Section 3.4).\n\n2.4. Subsequent Licenses\n\n     No Contributor makes additional grants as a result of Your choice to\n     distribute the Covered Software under a subsequent version of this License\n     (see Section 10.2) or under the terms of a Secondary License (if permitted\n     under the terms of Section 3.3).\n\n2.5. Representation\n\n     Each Contributor represents that the Contributor believes its Contributions\n     are its original creation(s) or it has sufficient rights to grant the\n     rights to its Contributions conveyed by this License.\n\n2.6. Fair Use\n\n     This License is not intended to limit any rights You have under applicable\n     copyright doctrines of fair use, fair dealing, or other equivalents.\n\n2.7. Conditions\n\n     Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in\n     Section 2.1.\n\n\n3. Responsibilities\n\n3.1. Distribution of Source Form\n\n     All distribution of Covered Software in Source Code Form, including any\n     Modifications that You create or to which You contribute, must be under the\n     terms of this License. You must inform recipients that the Source Code Form\n     of the Covered Software is governed by the terms of this License, and how\n     they can obtain a copy of this License. You may not attempt to alter or\n     restrict the recipients’ rights in the Source Code Form.\n\n3.2. Distribution of Executable Form\n\n     If You distribute Covered Software in Executable Form then:\n\n     a. such Covered Software must also be made available in Source Code Form,\n        as described in Section 3.1, and You must inform recipients of the\n        Executable Form how they can obtain a copy of such Source Code Form by\n        reasonable means in a timely manner, at a charge no more than the cost\n        of distribution to the recipient; and\n\n     b. You may distribute such Executable Form under the terms of this License,\n        or sublicense it under different terms, provided that the license for\n        the Executable Form does not attempt to limit or alter the recipients’\n        rights in the Source Code Form under this License.\n\n3.3. Distribution of a Larger Work\n\n     You may create and distribute a Larger Work under terms of Your choice,\n     provided that You also comply with the requirements of this License for the\n     Covered Software. If the Larger Work is a combination of Covered Software\n     with a work governed by one or more Secondary Licenses, and the Covered\n     Software is not Incompatible With Secondary Licenses, this License permits\n     You to additionally distribute such Covered Software under the terms of\n     such Secondary License(s), so that the recipient of the Larger Work may, at\n     their option, further distribute the Covered Software under the terms of\n     either this License or such Secondary License(s).\n\n3.4. Notices\n\n     You may not remove or alter the substance of any license notices (including\n     copyright notices, patent notices, disclaimers of warranty, or limitations\n     of liability) contained within the Source Code Form of the Covered\n     Software, except that You may alter any license notices to the extent\n     required to remedy known factual inaccuracies.\n\n3.5. Application of Additional Terms\n\n     You may choose to offer, and to charge a fee for, warranty, support,\n     indemnity or liability obligations to one or more recipients of Covered\n     Software. However, You may do so only on Your own behalf, and not on behalf\n     of any Contributor. You must make it absolutely clear that any such\n     warranty, support, indemnity, or liability obligation is offered by You\n     alone, and You hereby agree to indemnify every Contributor for any\n     liability incurred by such Contributor as a result of warranty, support,\n     indemnity or liability terms You offer. You may include additional\n     disclaimers of warranty and limitations of liability specific to any\n     jurisdiction.\n\n4. Inability to Comply Due to Statute or Regulation\n\n   If it is impossible for You to comply with any of the terms of this License\n   with respect to some or all of the Covered Software due to statute, judicial\n   order, or regulation then You must: (a) comply with the terms of this License\n   to the maximum extent possible; and (b) describe the limitations and the code\n   they affect. Such description must be placed in a text file included with all\n   distributions of the Covered Software under this License. Except to the\n   extent prohibited by statute or regulation, such description must be\n   sufficiently detailed for a recipient of ordinary skill to be able to\n   understand it.\n\n5. Termination\n\n5.1. The rights granted under this License will terminate automatically if You\n     fail to comply with any of its terms. However, if You become compliant,\n     then the rights granted under this License from a particular Contributor\n     are reinstated (a) provisionally, unless and until such Contributor\n     explicitly and finally terminates Your grants, and (b) on an ongoing basis,\n     if such Contributor fails to notify You of the non-compliance by some\n     reasonable means prior to 60 days after You have come back into compliance.\n     Moreover, Your grants from a particular Contributor are reinstated on an\n     ongoing basis if such Contributor notifies You of the non-compliance by\n     some reasonable means, this is the first time You have received notice of\n     non-compliance with this License from such Contributor, and You become\n     compliant prior to 30 days after Your receipt of the notice.\n\n5.2. If You initiate litigation against any entity by asserting a patent\n     infringement claim (excluding declaratory judgment actions, counter-claims,\n     and cross-claims) alleging that a Contributor Version directly or\n     indirectly infringes any patent, then the rights granted to You by any and\n     all Contributors for the Covered Software under Section 2.1 of this License\n     shall terminate.\n\n5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user\n     license agreements (excluding distributors and resellers) which have been\n     validly granted by You or Your distributors under this License prior to\n     termination shall survive termination.\n\n6. Disclaimer of Warranty\n\n   Covered Software is provided under this License on an “as is” basis, without\n   warranty of any kind, either expressed, implied, or statutory, including,\n   without limitation, warranties that the Covered Software is free of defects,\n   merchantable, fit for a particular purpose or non-infringing. The entire\n   risk as to the quality and performance of the Covered Software is with You.\n   Should any Covered Software prove defective in any respect, You (not any\n   Contributor) assume the cost of any necessary servicing, repair, or\n   correction. This disclaimer of warranty constitutes an essential part of this\n   License. No use of  any Covered Software is authorized under this License\n   except under this disclaimer.\n\n7. Limitation of Liability\n\n   Under no circumstances and under no legal theory, whether tort (including\n   negligence), contract, or otherwise, shall any Contributor, or anyone who\n   distributes Covered Software as permitted above, be liable to You for any\n   direct, indirect, special, incidental, or consequential damages of any\n   character including, without limitation, damages for lost profits, loss of\n   goodwill, work stoppage, computer failure or malfunction, or any and all\n   other commercial damages or losses, even if such party shall have been\n   informed of the possibility of such damages. This limitation of liability\n   shall not apply to liability for death or personal injury resulting from such\n   party’s negligence to the extent applicable law prohibits such limitation.\n   Some jurisdictions do not allow the exclusion or limitation of incidental or\n   consequential damages, so this exclusion and limitation may not apply to You.\n\n8. Litigation\n\n   Any litigation relating to this License may be brought only in the courts of\n   a jurisdiction where the defendant maintains its principal place of business\n   and such litigation shall be governed by laws of that jurisdiction, without\n   reference to its conflict-of-law provisions. Nothing in this Section shall\n   prevent a party’s ability to bring cross-claims or counter-claims.\n\n9. Miscellaneous\n\n   This License represents the complete agreement concerning the subject matter\n   hereof. If any provision of this License is held to be unenforceable, such\n   provision shall be reformed only to the extent necessary to make it\n   enforceable. Any law or regulation which provides that the language of a\n   contract shall be construed against the drafter shall not be used to construe\n   this License against a Contributor.\n\n\n10. Versions of the License\n\n10.1. New Versions\n\n      Mozilla Foundation is the license steward. Except as provided in Section\n      10.3, no one other than the license steward has the right to modify or\n      publish new versions of this License. Each version will be given a\n      distinguishing version number.\n\n10.2. Effect of New Versions\n\n      You may distribute the Covered Software under the terms of the version of\n      the License under which You originally received the Covered Software, or\n      under the terms of any subsequent version published by the license\n      steward.\n\n10.3. Modified Versions\n\n      If you create software not governed by this License, and you want to\n      create a new license for such software, you may create and use a modified\n      version of this License if you rename the license and remove any\n      references to the name of the license steward (except to note that such\n      modified license differs from this License).\n\n10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses\n      If You choose to distribute Source Code Form that is Incompatible With\n      Secondary Licenses under the terms of this version of the License, the\n      notice described in Exhibit B of this License must be attached.\n\nExhibit A - Source Code Form License Notice\n\n      This Source Code Form is subject to the\n      terms of the Mozilla Public License, v.\n      2.0. If a copy of the MPL was not\n      distributed with this file, You can\n      obtain one at\n      http://mozilla.org/MPL/2.0/.\n\nIf it is not possible or desirable to put the notice in a particular file, then\nYou may include the notice in a location (such as a LICENSE file in a relevant\ndirectory) where a recipient would be likely to look for such a notice.\n\nYou may add additional accurate notices of copyright ownership.\n\nExhibit B - “Incompatible With Secondary Licenses” Notice\n\n      This Source Code Form is “Incompatible\n      With Secondary Licenses”, as defined by\n      the Mozilla Public License, v. 2.0.\n\n"
  },
  {
    "path": "tests/demo/spline_library/spline.h",
    "content": "#pragma once\n\n#include <vector>\n\n#include \"utils/spline_common.h\"\n#include \"utils/calculus.h\"\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass Spline\n{\npublic:\n    Spline(std::vector<InterpolationType> originalPoints, floating_t maxT)\n        :maxT(maxT), originalPoints(std::move(originalPoints))\n    {}\n\npublic:\n    struct InterpolatedPT;\n\n    struct InterpolatedPTC;\n\n    struct InterpolatedPTCW;\n\n    virtual InterpolationType getPosition(floating_t x) const = 0;\n    virtual InterpolatedPT getTangent(floating_t x) const = 0;\n    virtual InterpolatedPTC getCurvature(floating_t x) const = 0;\n    virtual InterpolatedPTCW getWiggle(floating_t x) const = 0;\n\n    virtual floating_t arcLength(floating_t a, floating_t b) const = 0;\n    virtual floating_t totalLength(void) const = 0;\n    inline floating_t getMaxT(void) const { return maxT; }\n\n    const std::vector<InterpolationType> &getOriginalPoints(void) const { return originalPoints; }\n    virtual bool isLooping(void) const = 0;\n\n    //lower level functions\n    virtual size_t segmentCount(void) const = 0;\n    virtual size_t segmentForT(floating_t t) const = 0;\n    virtual floating_t segmentT(size_t segmentIndex) const = 0;\n    virtual floating_t segmentArcLength(size_t segmentIndex, floating_t a, floating_t b) const = 0;\n\nprotected:\n    const floating_t maxT;\n\nprivate:\n    const std::vector<InterpolationType> originalPoints;\n};\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass LoopingSpline: public Spline<InterpolationType, floating_t>\n{\npublic:\n    LoopingSpline(std::vector<InterpolationType> originalPoints, floating_t maxT)\n        :Spline<InterpolationType, floating_t>(std::move(originalPoints), maxT)\n    {}\n\n    inline floating_t wrapT(floating_t t) const {\n        float wrappedT = std::fmod(t, this->maxT);\n        if(wrappedT < 0)\n            return wrappedT + this->maxT;\n        else\n            return wrappedT;\n    }\n    virtual floating_t cyclicArcLength(floating_t a, floating_t b) const = 0;\n};\n\n\n\n\ntemplate<template<class, typename> class SplineCore, class InterpolationType, typename floating_t>\nclass SplineImpl: public Spline<InterpolationType, floating_t>\n{\npublic:\n    InterpolationType getPosition(floating_t t) const override { return common.getPosition(t); }\n    typename Spline<InterpolationType,floating_t>::InterpolatedPT getTangent(floating_t t) const override { return common.getTangent(t); }\n    typename Spline<InterpolationType,floating_t>::InterpolatedPTC getCurvature(floating_t t) const override { return common.getCurvature(t); }\n    typename Spline<InterpolationType,floating_t>::InterpolatedPTCW getWiggle(floating_t t) const override { return common.getWiggle(t); }\n\n    floating_t arcLength(floating_t a, floating_t b) const override;\n    floating_t totalLength(void) const override;\n\n    bool isLooping(void) const override { return false; }\n\n    size_t segmentCount(void) const override { return common.segmentCount(); }\n    size_t segmentForT(floating_t t) const override { return common.segmentForT(t); }\n    floating_t segmentT(size_t segmentIndex) const override { return common.segmentT(segmentIndex); }\n    floating_t segmentArcLength(size_t segmentIndex, floating_t a, floating_t b) const override { return common.segmentLength(segmentIndex, a, b); }\n\nprotected:\n    //protected constructor and destructor, so that this class can only be used as a parent class, even though it won't have any pure virtual methods\n    SplineImpl(std::vector<InterpolationType> originalPoints, floating_t maxT)\n        :Spline<InterpolationType, floating_t>(std::move(originalPoints), maxT)\n    {}\n    ~SplineImpl(void) = default;\n\n    SplineCore<InterpolationType, floating_t> common;\n};\n\n\n\ntemplate<template<class, typename> class SplineCore, class InterpolationType, typename floating_t>\nclass SplineLoopingImpl: public LoopingSpline<InterpolationType, floating_t>\n{\npublic:\n    InterpolationType getPosition(floating_t globalT) const override { return common.getPosition(this->wrapT(globalT)); }\n    typename Spline<InterpolationType,floating_t>::InterpolatedPT getTangent(floating_t globalT) const override { return common.getTangent(this->wrapT(globalT)); }\n    typename Spline<InterpolationType,floating_t>::InterpolatedPTC getCurvature(floating_t globalT) const override { return common.getCurvature(this->wrapT(globalT)); }\n    typename Spline<InterpolationType,floating_t>::InterpolatedPTCW getWiggle(floating_t globalT) const override { return common.getWiggle(this->wrapT(globalT)); }\n\n    floating_t arcLength(floating_t a, floating_t b) const override;\n    floating_t cyclicArcLength(floating_t a, floating_t b) const override;\n    floating_t totalLength(void) const override;\n\n    bool isLooping(void) const override { return true; }\n\n    size_t segmentCount(void) const override { return common.segmentCount(); }\n    size_t segmentForT(floating_t t) const override { return common.segmentForT(this->wrapT(t)); }\n    floating_t segmentT(size_t segmentIndex) const override { return common.segmentT(segmentIndex); }\n    floating_t segmentArcLength(size_t segmentIndex, floating_t a, floating_t b) const override { return common.segmentLength(segmentIndex, a, b); }\n\nprotected:\n    //protected constructor and destructor, so that this class can only be used as a parent class, even though it won't have any pure virtual methods\n    SplineLoopingImpl(std::vector<InterpolationType> originalPoints, floating_t maxT)\n        :LoopingSpline<InterpolationType, floating_t>(std::move(originalPoints), maxT)\n    {}\n    ~SplineLoopingImpl(void) = default;\n\n    SplineCore<InterpolationType, floating_t> common;\n};\n\n\n\n\n\ntemplate<class InterpolationType, typename floating_t>\nstruct Spline<InterpolationType,floating_t>::InterpolatedPT\n{\n    InterpolationType position;\n    InterpolationType tangent;\n\n    InterpolatedPT(const InterpolationType &p, const InterpolationType &t)\n        :position(p),tangent(t)\n    {}\n};\n\ntemplate<class InterpolationType, typename floating_t>\nstruct Spline<InterpolationType,floating_t>::InterpolatedPTC\n{\n    InterpolationType position;\n    InterpolationType tangent;\n    InterpolationType curvature;\n\n    InterpolatedPTC(const InterpolationType &p, const InterpolationType &t, const InterpolationType &c)\n        :position(p),tangent(t),curvature(c)\n    {}\n};\n\ntemplate<class InterpolationType, typename floating_t>\nstruct Spline<InterpolationType,floating_t>::InterpolatedPTCW\n{\n    InterpolationType position;\n    InterpolationType tangent;\n    InterpolationType curvature;\n    InterpolationType wiggle;\n\n    InterpolatedPTCW(const InterpolationType &p, const InterpolationType &t, const InterpolationType &c, const InterpolationType &w)\n        :position(p),tangent(t),curvature(c), wiggle(w)\n    {}\n};\n\ntemplate<template<class, typename> class SplineCore, class InterpolationType, typename floating_t>\nfloating_t SplineImpl<SplineCore, InterpolationType, floating_t>::arcLength(floating_t a, floating_t b) const\n{\n    if(a > b) {\n        std::swap(a,b);\n    }\n\n    //get the knot indices for the beginning and end\n    size_t aIndex = common.segmentForT(a);\n    size_t bIndex = common.segmentForT(b);\n\n    //if a and b occur inside the same segment, compute the length within that segment\n    //but excude cases where a > b, because that means we need to wrap around\n    if(aIndex == bIndex) {\n        return common.segmentLength(aIndex, a, b);\n    }\n    else {\n        //a and b occur in different segments, so compute one length for every segment\n        floating_t result{0};\n\n        //first segment\n        floating_t aEnd = common.segmentT(aIndex + 1);\n        result += common.segmentLength(aIndex, a, aEnd);\n\n        //middle segments\n        for(size_t i = aIndex + 1; i < bIndex; i++) {\n            result += common.segmentLength(i, common.segmentT(i), common.segmentT(i + 1));\n        }\n\n        //last segment\n        floating_t bBegin = common.segmentT(bIndex);\n        result += common.segmentLength(bIndex, bBegin, b);\n\n        return result;\n    }\n}\n\ntemplate<template<class, typename> class SplineCore, class InterpolationType, typename floating_t>\nfloating_t SplineImpl<SplineCore, InterpolationType, floating_t>::totalLength(void) const\n{\n    floating_t result{0};\n    for(size_t i = 0; i < common.segmentCount(); i++) {\n        result += common.segmentLength(i, common.segmentT(i), common.segmentT(i+1));\n    }\n    return result;\n}\n\n\ntemplate<template<class, typename> class SplineCore, class InterpolationType, typename floating_t>\nfloating_t SplineLoopingImpl<SplineCore, InterpolationType, floating_t>::arcLength(floating_t a, floating_t b) const\n{\n    a = this->wrapT(a);\n    b = this->wrapT(b);\n\n    if(a > b) {\n        std::swap(a,b);\n    }\n\n    //get the knot indices for the beginning and end\n    size_t aIndex = common.segmentForT(a);\n    size_t bIndex = common.segmentForT(b);\n\n    //if a and b occur inside the same segment, compute the length within that segment\n    //but excude cases where a > b, because that means we need to wrap around\n    if(aIndex == bIndex) {\n        return common.segmentLength(aIndex, a, b);\n    }\n    else {\n        //a and b occur in different segments, so compute one length for every segment\n        floating_t result{0};\n\n        //first segment\n        floating_t aEnd = common.segmentT(aIndex + 1);\n        result += common.segmentLength(aIndex, a, aEnd);\n\n        //middle segments\n        for(size_t i = aIndex + 1; i < bIndex; i++) {\n            result += common.segmentLength(i, common.segmentT(i), common.segmentT(i + 1));\n        }\n\n        //last segment\n        floating_t bBegin = common.segmentT(bIndex);\n        result += common.segmentLength(bIndex, bBegin, b);\n\n        return result;\n    }\n}\n\n//compute the arc length from a to b on the given spline, using wrapping/cyclic logic\n//for cyclic splines only!\ntemplate<template <class, typename> class CyclicSplineT, class InterpolationType, typename floating_t>\nfloating_t SplineLoopingImpl<CyclicSplineT, InterpolationType, floating_t>::cyclicArcLength(floating_t a, floating_t b) const\n{\n    floating_t wrappedA = this->wrapT(a);\n    floating_t wrappedB = this->wrapT(b);\n\n    //if wrapped A is less than wrapped B, then we can use the normal arc legth formula\n    if(wrappedA <= wrappedB)\n    {\n        return arcLength(wrappedA, wrappedB);\n    }\n    else\n    {\n        //get the knot indices for the beginning and end\n        size_t aIndex = common.segmentForT(wrappedA);\n        size_t bIndex = common.segmentForT(wrappedB);\n\n        floating_t result{0};\n\n        //first segment\n        floating_t aEnd = common.segmentT(aIndex + 1);\n        result += common.segmentLength(aIndex, wrappedA, aEnd);\n\n        //for the \"middle\" segments. we're going to wrap around -- go from the segment after a to the end, then go from 0 to the segment before b\n        for(size_t i = aIndex + 1; i < common.segmentCount(); i++) {\n            result += common.segmentLength(i, common.segmentT(i), common.segmentT(i + 1));\n        }\n\n        //special case: if \"b\" is a multiple of maxT, then wrappedB wil be 0 and we don't need to bother computing the segments from T=0 to T=wrappedB\n        if(wrappedB > 0)\n        {\n            for(size_t i = 0; i < bIndex; i++) {\n                result += common.segmentLength(i, common.segmentT(i), common.segmentT(i + 1));\n            }\n\n            //last segment. if wrappedB == 0 then we've got a special case where b is maxT and was wrapped to 0, so we shouldn't compute the segment\n            floating_t bBegin = common.segmentT(bIndex);\n            result += common.segmentLength(bIndex, bBegin, wrappedB);\n        }\n\n        return result;\n    }\n}\n\ntemplate<template<class, typename> class SplineCore, class InterpolationType, typename floating_t>\nfloating_t SplineLoopingImpl<SplineCore, InterpolationType, floating_t>::totalLength(void) const\n{\n    floating_t result{0};\n    for(size_t i = 0; i < common.segmentCount(); i++) {\n        result += common.segmentLength(i, common.segmentT(i), common.segmentT(i+1));\n    }\n    return result;\n}\n"
  },
  {
    "path": "tests/demo/spline_library/splines/cubic_hermite_spline.h",
    "content": "#pragma once\n\n#include <cassert>\n\n#include \"../spline.h\"\n\ntemplate<class InterpolationType, typename floating_t>\nclass CubicHermiteSplineCommon\n{\npublic:\n    struct alignas(8) CubicHermiteSplinePoint\n    {\n        InterpolationType position, tangent;\n    };\n\n    inline CubicHermiteSplineCommon(void) = default;\n    inline CubicHermiteSplineCommon(std::vector<CubicHermiteSplinePoint> points, std::vector<floating_t> knots)\n        :points(std::move(points)), knots(std::move(knots))\n    {}\n\n    inline size_t segmentCount(void) const\n    {\n        return points.size() - 1;\n    }\n\n    inline size_t segmentForT(floating_t t) const\n    {\n        size_t segmentIndex = SplineCommon::getIndexForT(knots, t);\n        if(segmentIndex > segmentCount() - 1)\n            return segmentCount() - 1;\n        else\n            return segmentIndex;\n    }\n\n    inline floating_t segmentT(size_t segmentIndex) const\n    {\n        return knots[segmentIndex];\n    }\n\n    inline InterpolationType getPosition(floating_t globalT) const\n    {\n        size_t knotIndex = segmentForT(globalT);\n\n        floating_t tDiff = (knots[knotIndex + 1] - knots[knotIndex]);\n        floating_t localT = (globalT - knots[knotIndex]) / tDiff;\n\n        return computePosition(knotIndex, tDiff, localT);\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPT getTangent(floating_t globalT) const\n    {\n        size_t knotIndex = segmentForT(globalT);\n\n        floating_t tDiff = (knots[knotIndex + 1] - knots[knotIndex]);\n        floating_t localT = (globalT - knots[knotIndex]) / tDiff;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPT(\n                    computePosition(knotIndex, tDiff, localT),\n                    computeTangent(knotIndex, tDiff, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTC getCurvature(floating_t globalT) const\n    {\n        size_t knotIndex = segmentForT(globalT);\n\n        floating_t tDiff = (knots[knotIndex + 1] - knots[knotIndex]);\n        floating_t localT = (globalT - knots[knotIndex]) / tDiff;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTC(\n                    computePosition(knotIndex, tDiff, localT),\n                    computeTangent(knotIndex, tDiff, localT),\n                    computeCurvature(knotIndex, tDiff, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTCW getWiggle(floating_t globalT) const\n    {\n        size_t knotIndex = segmentForT(globalT);\n\n        floating_t tDiff = knots[knotIndex + 1] - knots[knotIndex];\n        floating_t localT = (globalT - knots[knotIndex]) / tDiff;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTCW(\n                    computePosition(knotIndex, tDiff, localT),\n                    computeTangent(knotIndex, tDiff, localT),\n                    computeCurvature(knotIndex, tDiff, localT),\n                    computeWiggle(knotIndex, tDiff)\n                    );\n    }\n\n    inline floating_t segmentLength(size_t index, floating_t a, floating_t b) const\n    {\n        floating_t tDiff = knots[index + 1] - knots[index];\n        auto segmentFunction = [this, index, tDiff](floating_t t) -> floating_t {\n            auto tangent = computeTangent(index, tDiff, t);\n            return tangent.length();\n        };\n\n        floating_t localA = (a - knots[index]) / tDiff;\n        floating_t localB = (b - knots[index]) / tDiff;\n\n        return tDiff * SplineLibraryCalculus::gaussLegendreQuadratureIntegral<floating_t>(segmentFunction, localA, localB);\n    }\n\n\nprivate: //methods\n    inline InterpolationType computePosition(size_t index, floating_t tDiff, floating_t t) const\n    {\n        auto oneMinusT = 1 - t;\n\n        auto basis00 = (1 + 2*t) * oneMinusT * oneMinusT;\n        auto basis10 = t * oneMinusT * oneMinusT;\n\n        auto basis11 = t * t * -oneMinusT;\n        auto basis01 = t * t * (3 - 2*t);\n\n        return\n                basis00 * points[index].position +\n                basis10 * tDiff * points[index].tangent +\n\n                basis11 * tDiff * points[index + 1].tangent +\n                basis01 * points[index + 1].position;\n    }\n\n    inline InterpolationType computeTangent(size_t index, floating_t tDiff, floating_t t) const\n    {\n        auto oneMinusT = 1 - t;\n\n        auto d_basis00 = 6 * t * (t - 1);\n        auto d_basis10 = (1 - 3*t) * oneMinusT;\n\n        auto d_basis11 = t * (3 * t - 2);\n        auto d_basis01 = -d_basis00;\n\n        //tests and such have shown that we have to scale this by the inverse of the t distance, and i'm not sure why\n        //intuitively it would just be the derivative of the position function and nothing else\n        //if you know why please let me know\n        return (\n                d_basis00 * points[index].position +\n                d_basis10 * tDiff * points[index].tangent +\n\n                d_basis11 * tDiff * points[index + 1].tangent +\n                d_basis01 * points[index + 1].position\n                ) / tDiff;\n    }\n\n    inline InterpolationType computeCurvature(size_t index, floating_t tDiff, floating_t t) const\n    {\n        auto d2_basis00 = 6 * (2 * t - 1);\n        auto d2_basis10 = 2 * (3 * t - 2);\n\n        auto d2_basis11 = 2 * (3 * t - 1);\n        auto d2_basis01 = -d2_basis00;\n\n        //tests and such have shown that we have to scale this by the inverse of the t distance, and i'm not sure why\n        //intuitively it would just be the 2nd derivative of the position function and nothing else\n        //if you know why please let me know\n        return (\n                d2_basis00 * points[index].position +\n                d2_basis10 * tDiff * points[index].tangent +\n\n                d2_basis11 * tDiff * points[index + 1].tangent +\n                d2_basis01 * points[index + 1].position\n                ) / (tDiff * tDiff);\n    }\n\n    inline InterpolationType computeWiggle(size_t index, floating_t tDiff) const\n    {\n        //tests and such have shown that we have to scale this by the inverse of the t distance, and i'm not sure why\n        //intuitively it would just be the 2nd derivative of the position function and nothing else\n        //if you know why please let me know\n        return (\n                    floating_t(12) * (points[index].position - points[index + 1].position) + floating_t(6) * tDiff * (points[index].tangent + points[index + 1].tangent)\n                ) / (tDiff * tDiff * tDiff);\n    }\n\nprivate: //data\n    std::vector<CubicHermiteSplinePoint> points;\n    std::vector<floating_t> knots;\n};\n\n\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass CubicHermiteSpline final : public SplineImpl<CubicHermiteSplineCommon, InterpolationType, floating_t>\n{\n//constructors\npublic:\n    CubicHermiteSpline(const std::vector<InterpolationType> &points, const std::vector<InterpolationType> &tangents, floating_t alpha = 0.0)\n        :SplineImpl<CubicHermiteSplineCommon, InterpolationType,floating_t>(points, points.size() - 1)\n    {\n        assert(points.size() >= 2);\n        assert(points.size() == tangents.size());\n\n        size_t firstTangent = 0;\n        size_t numSegments = points.size() - 1;\n\n        //compute the T values for each point\n        std::vector<floating_t> knots = SplineCommon::computeTValuesWithInnerPadding(points, alpha, firstTangent);\n\n        //pre-arrange the data needed for interpolation\n        std::vector<typename CubicHermiteSplineCommon<InterpolationType, floating_t>::CubicHermiteSplinePoint> positionData(numSegments + 1);\n        for(size_t i = 0; i < positionData.size(); i++)\n        {\n            positionData[i].position = points[i];\n            positionData[i].tangent = tangents[i];\n        }\n\n        this->common = CubicHermiteSplineCommon<InterpolationType, floating_t>(std::move(positionData), std::move(knots));\n    }\n\n    CubicHermiteSpline(const std::vector<InterpolationType> &points, floating_t alpha = 0.0)\n        :SplineImpl<CubicHermiteSplineCommon, InterpolationType,floating_t>(points, points.size() - 3)\n    {\n        assert(points.size() >= 4);\n\n        size_t firstTangent = 1;\n        size_t numSegments = points.size() - 3;\n\n        //compute the T values for each point\n        std::vector<floating_t> paddedKnots = SplineCommon::computeTValuesWithInnerPadding(points, alpha, firstTangent);\n\n        //compute the tangents\n        std::vector<InterpolationType> tangents(points.size());\n        for(size_t i = firstTangent; i < firstTangent + numSegments + 1; i++)\n        {\n            floating_t tPrev = paddedKnots[i - 1];\n            floating_t tCurrent = paddedKnots[i];\n            floating_t tNext = paddedKnots[i + 1];\n\n            InterpolationType pPrev = points.at(i - 1);\n            InterpolationType pCurrent = points.at(i);\n            InterpolationType pNext = points.at(i + 1);\n\n            //the tangent is the standard catmull-rom spline tangent calculation\n            tangents[i] =\n                                pPrev * (tCurrent - tNext) / ((tNext - tPrev) * (tCurrent - tPrev))\n                                + pNext * (tCurrent - tPrev) / ((tNext - tPrev) * (tNext - tCurrent))\n\n                             //plus a little something extra - this is derived from the pyramid contruction\n                             //when the t values are evenly spaced (ie when alpha is 0), this whole line collapses to 0,\n                             //yielding the standard catmull-rom formula\n                                - pCurrent * ((tCurrent - tPrev) - (tNext - tCurrent)) / ((tNext - tCurrent) * (tCurrent - tPrev));\n        }\n\n        //pre-arrange the data needed for interpolation\n        std::vector<floating_t> knots(numSegments + 1);\n        std::vector<typename CubicHermiteSplineCommon<InterpolationType, floating_t>::CubicHermiteSplinePoint> positionData(numSegments + 1);\n        for(size_t i = 0; i < positionData.size(); i++)\n        {\n            knots[i] = paddedKnots[i + firstTangent];\n            positionData[i].position = points[i + firstTangent];\n            positionData[i].tangent = tangents[i + firstTangent];\n        }\n\n        this->common = CubicHermiteSplineCommon<InterpolationType, floating_t>(std::move(positionData), std::move(knots));\n    }\n};\n\n\n\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass LoopingCubicHermiteSpline final : public SplineLoopingImpl<CubicHermiteSplineCommon, InterpolationType, floating_t>\n{\n//constructors\npublic:\n    LoopingCubicHermiteSpline(const std::vector<InterpolationType> &points, const std::vector<InterpolationType> &tangents, floating_t alpha = 0.0)\n        :SplineLoopingImpl<CubicHermiteSplineCommon, InterpolationType,floating_t>(points, points.size())\n    {\n        assert(points.size() >= 2);\n        assert(points.size() == tangents.size());\n\n        //compute the T values for each point\n        std::vector<floating_t> knots = SplineCommon::computeLoopingTValues(points, alpha, 0);\n\n        //pre-arrange the data needed for interpolation\n        std::vector<typename CubicHermiteSplineCommon<InterpolationType, floating_t>::CubicHermiteSplinePoint> positionData(points.size() + 1);\n        for(size_t i = 0; i < points.size(); i++)\n        {\n            positionData[i].position = points[i];\n            positionData[i].tangent = tangents[i];\n        }\n        positionData[points.size()] = positionData[0];\n\n        this->common = CubicHermiteSplineCommon<InterpolationType, floating_t>(std::move(positionData), std::move(knots));\n    }\n\n    LoopingCubicHermiteSpline(const std::vector<InterpolationType> &points, floating_t alpha = 0.0)\n        :SplineLoopingImpl<CubicHermiteSplineCommon, InterpolationType,floating_t>(points, points.size())\n    {\n        assert(points.size() >= 4);\n\n        size_t size = points.size();\n\n        //compute the T values for each point\n        size_t padding = 1;\n        std::vector<floating_t> paddedKnots = SplineCommon::computeLoopingTValues(points, alpha, padding);\n\n        //compute the tangents\n        std::vector<InterpolationType> tangents(size);\n        for(size_t i = 0; i < size; i++)\n        {\n            floating_t tPrev = paddedKnots[i - 1 + padding];\n            floating_t tCurrent = paddedKnots[i + padding];\n            floating_t tNext = paddedKnots[i + 1 + padding];\n\n            InterpolationType pPrev = points[(i - 1 + size)%size];\n            InterpolationType pCurrent = points[i];\n            InterpolationType pNext = points[(i + 1)%size];\n\n            //the tangent is the standard catmull-rom spline tangent calculation\n            tangents[i] =\n                                pPrev * (tCurrent - tNext) / ((tNext - tPrev) * (tCurrent - tPrev))\n                                + pNext * (tCurrent - tPrev) / ((tNext - tPrev) * (tNext - tCurrent))\n\n                             //plus a little something extra - this is derived from the pyramid contruction\n                             //when the t values are evenly spaced (ie when alpha is 0), this whole line collapses to 0,\n                             //yielding the standard catmull-rom formula\n                                - pCurrent * ((tCurrent - tPrev) - (tNext - tCurrent)) / ((tNext - tCurrent) * (tCurrent - tPrev));\n        }\n\n        //pre-arrange the data needed for interpolation\n        std::vector<floating_t> knots(size + 1);\n        std::vector<typename CubicHermiteSplineCommon<InterpolationType, floating_t>::CubicHermiteSplinePoint> positionData(size + 1);\n        for(size_t i = 0; i < size; i++)\n        {\n            knots[i] = paddedKnots[i + padding];\n            positionData[i].position = points[i];\n            positionData[i].tangent = tangents[i];\n        }\n        positionData[size] = positionData[0];\n        knots[size] = paddedKnots[size + padding];\n\n        this->common = CubicHermiteSplineCommon<InterpolationType, floating_t>(std::move(positionData), std::move(knots));\n    }\n};\n"
  },
  {
    "path": "tests/demo/spline_library/splines/generic_b_spline.h",
    "content": "#pragma once\n\n#include <cassert>\n\n#include \"../spline.h\"\n\ntemplate<class InterpolationType, typename floating_t>\nclass GenericBSplineCommon\n{\npublic:\n    inline GenericBSplineCommon(void) = default;\n    inline GenericBSplineCommon(std::vector<InterpolationType> positions, std::vector<floating_t> knots, size_t splineDegree)\n        :positions(std::move(positions)), knots(std::move(knots)), splineDegree(splineDegree)\n    {}\n\n    inline size_t segmentCount(void) const\n    {\n        return positions.size() - splineDegree;\n    }\n\n    inline size_t segmentForT(floating_t t) const\n    {\n        if(t < 0) {\n            return 0;\n        }\n\n        size_t segmentIndex = SplineCommon::getIndexForT(knots, t) - (splineDegree - 1);\n        if(segmentIndex > segmentCount() - 1)\n        {\n            return segmentCount() - 1;\n        }\n        else\n        {\n            return segmentIndex;\n        }\n    }\n\n    inline floating_t segmentT(size_t segmentIndex) const\n    {\n        return knots[segmentIndex + splineDegree - 1];\n    }\n\n    inline InterpolationType getPosition(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        size_t innerIndex = segmentIndex + (splineDegree - 1);\n\n        return computeDeboor(innerIndex + 1, splineDegree, globalT);\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPT getTangent(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        size_t innerIndex = segmentIndex + (splineDegree - 1);\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPT(\n                    computeDeboor(innerIndex + 1, splineDegree, globalT),\n                    computeDeboorDerivative(innerIndex + 1, splineDegree, globalT, 1)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTC getCurvature(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        size_t innerIndex = segmentIndex + (splineDegree - 1);\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTC(\n                    computeDeboor(innerIndex + 1, splineDegree, globalT),\n                    computeDeboorDerivative(innerIndex + 1, splineDegree, globalT, 1),\n                    computeDeboorDerivative(innerIndex + 1, splineDegree, globalT, 2)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTCW getWiggle(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        size_t innerIndex = segmentIndex + (splineDegree - 1);\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTCW(\n                    computeDeboor(innerIndex + 1, splineDegree, globalT),\n                    computeDeboorDerivative(innerIndex + 1, splineDegree, globalT, 1),\n                    computeDeboorDerivative(innerIndex + 1, splineDegree, globalT, 2),\n                    computeDeboorDerivative(innerIndex + 1, splineDegree, globalT, 3)\n                    );\n    }\n\n    inline floating_t segmentLength(size_t segmentIndex, floating_t a, floating_t b) const {\n\n        auto innerIndex = segmentIndex + splineDegree - 1;\n\n        floating_t tDistance = knots[innerIndex + 1] - knots[innerIndex];\n\n        //it's perfectly legal for Bspline segments to have a T distance of 0, in which case the arc length is 0\n        if(tDistance > 0)\n        {\n            auto segmentFunction = [this, innerIndex](floating_t t) -> floating_t {\n                auto tangent = computeDeboorDerivative(innerIndex + 1, splineDegree, t, 1);\n                return tangent.length();\n            };\n\n            return SplineLibraryCalculus::gaussLegendreQuadratureIntegral<floating_t>(segmentFunction, a, b);\n        }\n        else\n        {\n            return 0;\n        }\n    }\n\nprivate: //methods\n    InterpolationType computeDeboor(size_t knotIndex, size_t degree, float globalT) const;\n    InterpolationType computeDeboorDerivative(size_t knotIndex, size_t degree, float globalT, int derivativeLevel) const;\n\nprivate: //data\n    std::vector<InterpolationType> positions;\n    std::vector<floating_t> knots;\n    size_t splineDegree;\n};\n\ntemplate<class InterpolationType, typename floating_t>\nInterpolationType GenericBSplineCommon<InterpolationType,floating_t>::computeDeboor(size_t knotIndex, size_t degree, float globalT) const\n{\n    if(degree == 0)\n    {\n        return positions[knotIndex];\n    }\n    else\n    {\n        floating_t alpha = (globalT - knots[knotIndex - 1]) / (knots[knotIndex + splineDegree - degree] - knots[knotIndex - 1]);\n\n        InterpolationType leftRecursive = computeDeboor(knotIndex - 1, degree - 1, globalT);\n        InterpolationType rightRecursive = computeDeboor(knotIndex, degree - 1, globalT);\n\n        InterpolationType blended = leftRecursive * (1 - alpha) + rightRecursive * alpha;\n\n        return blended;\n    }\n}\n\ntemplate<class InterpolationType, typename floating_t>\nInterpolationType GenericBSplineCommon<InterpolationType,floating_t>::computeDeboorDerivative(size_t knotIndex, size_t degree, float globalT, int derivativeLevel) const\n{\n    if(degree == 0)\n    {\n        //if we hit degree 0 before derivative level 0, then this spline's\n        //degree isn't high enough to support whatever derivative level was requested\n        return InterpolationType();\n    }\n    else\n    {\n        floating_t multiplier = degree / (knots[knotIndex + splineDegree - degree] - knots[knotIndex - 1]);\n\n        if(derivativeLevel <= 1)\n        {\n            //once we reach this point, the derivative calculation is \"complete\"\n            //in that from here, we go back to the normal deboor calculation deeper in the recursive tree\n            return multiplier *\n                    (computeDeboor(knotIndex, degree - 1, globalT)\n                   - computeDeboor(knotIndex - 1, degree - 1, globalT)\n                     );\n        }\n        else\n        {\n            //recursively call the derivative function to compute a higher derivative\n            return multiplier *\n                    (computeDeboorDerivative(knotIndex, degree - 1, globalT, derivativeLevel - 1)\n                   - computeDeboorDerivative(knotIndex - 1, degree - 1, globalT, derivativeLevel - 1)\n                     );\n        }\n    }\n}\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass GenericBSpline final : public SplineImpl<GenericBSplineCommon, InterpolationType, floating_t>\n{\n//constructors\npublic:\n    GenericBSpline(const std::vector<InterpolationType> &points, size_t degree)\n        :SplineImpl<GenericBSplineCommon, InterpolationType,floating_t>(points, points.size() - degree)\n    {\n        assert(points.size() > degree);\n\n        std::vector<floating_t> knots(points.size() + degree - 1);\n        for(size_t i = 0; i < knots.size(); i++)\n        {\n            knots[i] = floating_t(i) - floating_t(degree - 1);\n        }\n\n        this->common = GenericBSplineCommon<InterpolationType, floating_t>(points, std::move(knots), degree);\n    }\n};\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass LoopingGenericBSpline final : public SplineLoopingImpl<GenericBSplineCommon, InterpolationType, floating_t>\n{\n//constructors\npublic:\n    LoopingGenericBSpline(const std::vector<InterpolationType> &points, size_t degree)\n        :SplineLoopingImpl<GenericBSplineCommon, InterpolationType,floating_t>(points, points.size())\n    {\n        assert(points.size() > degree);\n\n        std::vector<floating_t> knots(points.size() + degree * 2 - 1);\n        for(size_t i = 0; i < knots.size(); i++)\n        {\n            knots[i] = floating_t(i) - floating_t(degree - 1);\n        }\n\n        //it would be easiest to just copy the points vector to the position vector, then copy the first 'degree' elements again\n        //this DOES work, but interpolation begins in the wrong place (ie getPosition(0) occurs at the wrong place on the spline)\n        //to fix this, we effectively \"rotate\" the position vector backwards, by copying point[size-1] to the beginning\n        //then copying the points vector in after, then copying degree-1 elements from the beginning\n        std::vector<InterpolationType> positions(points.size() + degree);\n\n        size_t padding = degree - 1;\n        positions[0] = points[points.size() - 1];\n        std::copy(points.begin(), points.end(), positions.begin() + 1);\n        std::copy_n(points.begin(), padding, positions.end() - padding);\n\n        this->common = GenericBSplineCommon<InterpolationType, floating_t>(std::move(positions), std::move(knots), degree);\n    }\n};\n\n"
  },
  {
    "path": "tests/demo/spline_library/splines/natural_spline.h",
    "content": "#pragma once\n\n#include <cassert>\n\n#include \"../spline.h\"\n#include \"../utils/linearalgebra.h\"\n\ntemplate<class InterpolationType, typename floating_t>\nclass NaturalSplineCommon\n{\npublic:\n    struct alignas(16) NaturalSplineSegment\n    {\n        InterpolationType a, c;\n    };\n\n    inline NaturalSplineCommon(void) = default;\n    inline NaturalSplineCommon(std::vector<NaturalSplineSegment> segments, std::vector<floating_t> knots)\n        :segments(std::move(segments)), knots(std::move(knots))\n    {}\n\n    inline size_t segmentCount(void) const\n    {\n        return segments.size() - 1;\n    }\n\n    inline size_t segmentForT(floating_t t) const\n    {\n        size_t segmentIndex = SplineCommon::getIndexForT(knots, t);\n        if(segmentIndex >= segmentCount())\n            return segmentCount() - 1;\n        else\n            return segmentIndex;\n    }\n\n    inline floating_t segmentT(size_t segmentIndex) const\n    {\n        return knots[segmentIndex];\n    }\n\n    inline InterpolationType getPosition(floating_t globalT) const\n    {\n        size_t segmentIndex = SplineCommon::getIndexForT(knots, globalT);\n        if(segmentIndex >= knots.size() - 1)\n            segmentIndex--;\n\n        floating_t localT = globalT - knots[segmentIndex];\n        floating_t tDiff = knots[segmentIndex + 1] - knots[segmentIndex];\n\n        return computePosition(segmentIndex, tDiff, localT);\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPT getTangent(floating_t globalT) const\n    {\n        size_t segmentIndex = SplineCommon::getIndexForT(knots, globalT);\n        if(segmentIndex >= knots.size() - 1)\n            segmentIndex--;\n\n        floating_t localT = globalT - knots[segmentIndex];\n        floating_t tDiff = knots[segmentIndex + 1] - knots[segmentIndex];\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPT(\n                    computePosition(segmentIndex, tDiff, localT),\n                    computeTangent(segmentIndex, tDiff, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTC getCurvature(floating_t globalT) const\n    {\n        size_t segmentIndex = SplineCommon::getIndexForT(knots, globalT);\n        if(segmentIndex >= knots.size() - 1)\n            segmentIndex--;\n\n        floating_t localT = globalT - knots[segmentIndex];\n        floating_t tDiff = knots[segmentIndex + 1] - knots[segmentIndex];\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTC(\n                    computePosition(segmentIndex, tDiff, localT),\n                    computeTangent(segmentIndex, tDiff, localT),\n                    computeCurvature(segmentIndex, tDiff, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTCW getWiggle(floating_t globalT) const\n    {\n        size_t segmentIndex = SplineCommon::getIndexForT(knots, globalT);\n        if(segmentIndex >= knots.size() - 1)\n            segmentIndex--;\n\n        floating_t localT = globalT - knots[segmentIndex];\n        floating_t tDiff = knots[segmentIndex + 1] - knots[segmentIndex];\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTCW(\n                    computePosition(segmentIndex, tDiff, localT),\n                    computeTangent(segmentIndex, tDiff, localT),\n                    computeCurvature(segmentIndex, tDiff, localT),\n                    computeWiggle(segmentIndex, tDiff)\n                    );\n    }\n\n    inline floating_t segmentLength(size_t segmentIndex, floating_t a, floating_t b) const {\n\n        floating_t tDiff = knots[segmentIndex + 1] - knots[segmentIndex];\n        auto segmentFunction = [=](floating_t t) -> floating_t {\n            auto tangent = computeTangent(segmentIndex, tDiff, t);\n            return tangent.length();\n        };\n\n        floating_t localA = a - knots[segmentIndex];\n        floating_t localB = b - knots[segmentIndex];\n\n        return SplineLibraryCalculus::gaussLegendreQuadratureIntegral<floating_t>(segmentFunction, localA, localB);\n    }\n\nprivate: //methods\n    inline InterpolationType computePosition(size_t index, floating_t tDiff, floating_t t) const\n    {\n        auto b = computeB(index, tDiff);\n        auto d = computeD(index, tDiff);\n\n        return segments[index].a + t * (b + t * (segments[index].c + t * d));\n    }\n\n    inline InterpolationType computeTangent(size_t index, floating_t tDiff, floating_t t) const\n    {\n        auto b = computeB(index, tDiff);\n        auto d = computeD(index, tDiff);\n\n        //compute the derivative of the position function\n        return b + t * (floating_t(2) * segments[index].c + (3 * t) * d);\n    }\n\n    inline InterpolationType computeCurvature(size_t index, floating_t tDiff, floating_t t) const\n    {\n        auto d = computeD(index, tDiff);\n\n        //compute the 2nd derivative of the position function\n        return floating_t(2) * segments[index].c + (6 * t) * d;\n    }\n\n    inline InterpolationType computeWiggle(size_t index, floating_t tDiff) const\n    {\n        auto d = computeD(index, tDiff);\n\n        //compute the 3rd derivative of the position function\n        return floating_t(6) * d;\n    }\n\n\n    //B is the tangent at t=0 for a segment, and D is effectively the wiggle for a segment\n    //we COULD precompute these and store them alongside a and c in the segment\n    //testing shows that it's faster (because of cache, and pipelining, etc) to just recompute them every time\n    inline InterpolationType computeB(size_t index, floating_t tDiff) const\n    {\n        return (segments[index+1].a - segments[index].a) / tDiff - (tDiff / 3) * (segments[index+1].c + floating_t(2)*segments[index].c);\n    }\n    inline InterpolationType computeD(size_t index, floating_t tDiff) const\n    {\n        return (segments[index+1].c - segments[index].c) / (3 * tDiff);\n    }\n\n\n\n    inline floating_t computeSegmentLength(size_t index, floating_t from, floating_t to) const\n    {\n\n    }\n\nprivate: //data\n    std::vector<NaturalSplineSegment> segments;\n    std::vector<floating_t> knots;\n};\n\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass NaturalSpline final : public SplineImpl<NaturalSplineCommon, InterpolationType, floating_t>\n{\npublic:\n    enum EndConditions { Natural, NotAKnot };\n\n//constructors\npublic:\n    NaturalSpline(const std::vector<InterpolationType> &points,\n                  bool includeEndpoints = true,\n                  floating_t alpha = 0.0,\n                  EndConditions endConditions = Natural)\n        :SplineImpl<NaturalSplineCommon, InterpolationType,floating_t>(points, includeEndpoints ? points.size()- 1 : points.size() - 3)\n    {\n        size_t size = points.size();\n        size_t firstPoint;\n        size_t numSegments;\n\n        if(includeEndpoints)\n        {\n            assert(points.size() >= 3);\n            numSegments = size - 1;\n            firstPoint = 0;\n        }\n        else\n        {\n            assert(points.size() >= 4);\n            numSegments = size - 3;\n            firstPoint = 1;\n        }\n\n        //compute the T values for each point\n        std::vector<floating_t> paddedKnots = SplineCommon::computeTValuesWithInnerPadding(points, alpha, firstPoint);\n\n        //next we compute curvatures\n        std::vector<InterpolationType> curvatures;\n        if(endConditions == Natural)\n            curvatures = computeCurvaturesNatural(paddedKnots);\n        else\n            curvatures = computeCurvaturesNotAKnot(paddedKnots);\n\n        //we now have 0 curvature for index 0 and n - 1, and the final (usually nonzero) curvature for every other point\n        //use this curvature to determine a,b,c,and d to build each segment\n        std::vector<floating_t> knots(numSegments + 1);\n        std::vector<typename NaturalSplineCommon<InterpolationType, floating_t>::NaturalSplineSegment> segments(numSegments + 1);\n        for(size_t i = firstPoint; i < numSegments + firstPoint + 1; i++) {\n\n            knots[i - firstPoint] = paddedKnots[i];\n            segments[i - firstPoint].a = points.at(i);\n            segments[i - firstPoint].c = curvatures.at(i);\n        }\n\n        this->common = NaturalSplineCommon<InterpolationType, floating_t>(std::move(segments), std::move(knots));\n    }\nprivate:\n    std::vector<InterpolationType> computeCurvaturesNatural(const std::vector<floating_t> tValues) const;\n    std::vector<InterpolationType> computeCurvaturesNotAKnot(const std::vector<floating_t> tValues) const;\n};\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass LoopingNaturalSpline final : public SplineLoopingImpl<NaturalSplineCommon, InterpolationType, floating_t>\n{\n//constructors\npublic:\n    LoopingNaturalSpline(const std::vector<InterpolationType> &points, floating_t alpha = 0.0)\n        :SplineLoopingImpl<NaturalSplineCommon, InterpolationType,floating_t>(points, points.size())\n    {\n        size_t size = points.size();\n\n        //compute the T values for each point\n        std::vector<floating_t> knots = SplineCommon::computeLoopingTValues(points, alpha, 0);\n\n        //now that we know the t values, we need to prepare the tridiagonal matrix calculation\n        //note that there several ways to formulate this matrix - i chose the following:\n        // http://www-hagen.informatik.uni-kl.de/~alggeom/pdf/ws1213/alggeom_script_ws12_02.pdf\n\n        //the tridiagonal matrix's main diagonal will be neighborDeltaT, and the secondary diagonals will be deltaT\n        //the list of values to solve for will be neighborDeltaPoint\n\n        //create an array of the differences in T between one point and the next\n        std::vector<floating_t> upperDiagonal(size);\n        for(size_t i = 0; i < size; i++)\n        {\n            floating_t delta = knots[i + 1] - knots[i];\n            upperDiagonal[i] = delta;\n        }\n\n        //create an array that stores 2 * (deltaT.at(i - 1) + deltaT.at(i))\n        //when i = 0, wrap i - 1 back around to the end of the list\n        std::vector<floating_t> diagonal(size);\n        for(size_t i = 0; i < size; i++)\n        {\n            floating_t neighborDelta = 2 * (upperDiagonal.at((i - 1 + size)%size) + upperDiagonal.at(i));\n            diagonal[i] = neighborDelta;\n        }\n\n        //create an array of displacement between each point, divided by delta t\n        std::vector<InterpolationType> deltaPoint(size);\n        for(size_t i = 0; i < size; i++)\n        {\n            InterpolationType displacement = points.at((i + 1)%size) - points.at(i);\n            deltaPoint[i] = displacement / upperDiagonal.at(i);\n        }\n\n        //create an array that stores 3 * (deltaPoint(i - 1) + deltaPoint(i))\n        //when i = 0, wrap i - 1 back around to the end of the list\n        std::vector<InterpolationType> inputVector(size);\n        for(size_t i = 0; i < size; i++)\n        {\n            InterpolationType neighborDelta = floating_t(3) * (deltaPoint.at(i) - deltaPoint.at((i - 1 + size) % size));\n            inputVector[i] = neighborDelta;\n        }\n\n        //solve the cyclic tridiagonal system to get the curvature at each point\n        std::vector<InterpolationType> curvatures = LinearAlgebra::solveCyclicSymmetricTridiagonal(\n                    std::move(diagonal),\n                    std::move(upperDiagonal),\n                    std::move(inputVector)\n                    );\n\n        //we now have the curvature for every point\n        //use this curvature to determine a,b,c,and d to build each segment\n        std::vector<typename NaturalSplineCommon<InterpolationType, floating_t>::NaturalSplineSegment> segments(size + 1);\n        for(size_t i = 0; i < size + 1; i++)\n        {\n            segments[i].a = points.at(i%size);\n            segments[i].c = curvatures.at(i%size);\n        }\n\n        this->common = NaturalSplineCommon<InterpolationType, floating_t>(std::move(segments), std::move(knots));\n    }\n};\n\ntemplate<class InterpolationType, typename floating_t>\nstd::vector<InterpolationType> NaturalSpline<InterpolationType,floating_t>::computeCurvaturesNatural(const std::vector<floating_t> tValues) const\n{\n\n    //now that we know the t values, we need to prepare the tridiagonal matrix calculation\n    //note that there several ways to formulate this matrix - for the \"natural boundary conditions\" i chose the following:\n    // http://www-hagen.informatik.uni-kl.de/~alggeom/pdf/ws1213/alggeom_script_ws12_02.pdf\n\n    //the tridiagonal matrix's main diagonal will be neighborDeltaT, and the secondary diagonals will be deltaT\n    //the list of values to solve for will be neighborDeltaPoint\n\n    size_t loop_limit = this->getOriginalPoints().size() - 1;\n\n    //create an array of the differences in T between one point and the next\n    std::vector<floating_t> upperDiagonal(loop_limit);\n    for(size_t i = 0; i < loop_limit; i++)\n    {\n        floating_t delta = tValues[i + 1] - tValues[i];\n        upperDiagonal[i] = delta;\n    }\n\n    //create an array that stores 2 * (deltaT.at(i - 1) + deltaT.at(i))\n    std::vector<floating_t> diagonal(loop_limit - 1);\n    for(size_t i = 1; i < loop_limit; i++)\n    {\n        floating_t neighborDelta = floating_t(2) * (upperDiagonal[i - 1] + upperDiagonal[i]);\n        diagonal[i - 1] = neighborDelta;\n    }\n\n    //create an array of displacement between each point, divided by delta t\n    std::vector<InterpolationType> deltaPoint(loop_limit);\n    for(size_t i = 0; i < loop_limit; i++)\n    {\n        InterpolationType displacement = this->getOriginalPoints()[i + 1] - this->getOriginalPoints()[i];\n        deltaPoint[i] = displacement / upperDiagonal[i];\n    }\n\n    //create an array that stores 3 * (deltaPoint(i - 1) + deltaPoint(i))\n    std::vector<InterpolationType> inputVector(loop_limit - 1);\n    for(size_t i = 1; i < loop_limit; i++)\n    {\n        InterpolationType neighborDelta = floating_t(3) * (deltaPoint[i] - deltaPoint[i - 1]);\n        inputVector[i - 1] = neighborDelta;\n    }\n\n    //the first element in upperDiagonal is garbage, so remove it\n    upperDiagonal.erase(upperDiagonal.begin());\n\n    //solve the tridiagonal system to get the curvature at each point\n    std::vector<InterpolationType> curvatures = LinearAlgebra::solveSymmetricTridiagonal(\n                std::move(diagonal),\n                std::move(upperDiagonal),\n                std::move(inputVector)\n                );\n\n    //we didn't compute the first or last curvature, which will be 0\n    curvatures.insert(curvatures.begin(), InterpolationType());\n    curvatures.push_back(InterpolationType());\n\n    return curvatures;\n}\n\n\ntemplate<class InterpolationType, typename floating_t>\nstd::vector<InterpolationType> NaturalSpline<InterpolationType,floating_t>::computeCurvaturesNotAKnot(const std::vector<floating_t> tValues) const\n{\n    //now that we know the t values, we need to prepare the tridiagonal matrix calculation\n    //note that there several ways to formulate this matrix; for \"not a knot\" i chose the following:\n    // http://sepwww.stanford.edu/data/media/public/sep//sergey/128A/answers6.pdf\n\n    //the tridiagonal matrix's main diagonal will be neighborDeltaT, and the secondary diagonals will be deltaT\n    //the list of values to solve for will be neighborDeltaPoint\n\n    size_t size = this->getOriginalPoints().size() - 1;\n\n    //create an array of the differences in T between one point and the next\n    std::vector<floating_t> deltaT(size);\n    for(size_t i = 0; i < size; i++)\n    {\n        deltaT[i] = tValues[i + 1] - tValues[i];\n    }\n\n    //the main diagonal of the tridiagonal will be 2 * (deltaT[i] + deltaT[i + 1])\n    float mainDiagonalSize = size - 1;\n    std::vector<floating_t> mainDiagonal(mainDiagonalSize);\n    for(size_t i = 0; i < mainDiagonalSize; i++)\n    {\n        mainDiagonal[i] = 2 * (deltaT[i] + deltaT[i + 1]);\n    }\n\n    //the upper diagonal will just be deltaT[i + 1]\n    float secondaryDiagonalSize = size - 2;\n    std::vector<floating_t> upperDiagonal(secondaryDiagonalSize);\n    for(size_t i = 0; i < secondaryDiagonalSize; i++)\n    {\n        upperDiagonal[i] = deltaT[i + 1];\n    }\n\n    //the lower diagonal is just a copy of the upper diagonal\n    std::vector<floating_t> lowerDiagonal = upperDiagonal;\n\n    //create an array of displacement between each point, divided by delta t\n    std::vector<InterpolationType> deltaPoint(size);\n    for(size_t i = 0; i < size; i++)\n    {\n        InterpolationType displacement = this->getOriginalPoints()[i + 1] - this->getOriginalPoints()[i];\n        deltaPoint[i] = displacement / deltaT[i];\n    }\n\n    //create an array that stores 3 * (deltaPoint(i - 1) + deltaPoint(i))\n    std::vector<InterpolationType> inputVector(mainDiagonalSize);\n    for(size_t i = 0; i < mainDiagonalSize; i++)\n    {\n        inputVector[i] = floating_t(3) * (deltaPoint[i + 1] - deltaPoint[i]);\n    }\n\n    //the first and last of the values in maindiagonalare different than normal\n    mainDiagonal[0] = 3*deltaT[0] + 2*deltaT[1] + deltaT[0]*deltaT[0]/deltaT[1];\n    mainDiagonal[mainDiagonalSize - 1] = 3*deltaT[size - 1] + 2*deltaT[size - 2] + deltaT[size - 1]*deltaT[size - 1]/deltaT[size - 2];\n\n    //the first value in the upper diagonal is different than normal\n    upperDiagonal[0] = deltaT[1] - deltaT[0]*deltaT[0]/deltaT[1];\n\n    //the last value in the upper diagonal is different than normal\n    lowerDiagonal[secondaryDiagonalSize - 1] = deltaT[size - 2] - deltaT[size - 1]*deltaT[size - 1]/deltaT[size - 2];\n\n    //solve the tridiagonal system to get the curvature at each point\n    std::vector<InterpolationType> curvatures = LinearAlgebra::solveTridiagonal(\n                std::move(mainDiagonal),\n                std::move(upperDiagonal),\n                std::move(lowerDiagonal),\n                std::move(inputVector)\n                );\n\n    //we didn't compute the first or last curvature, which will be calculated based on the others\n    curvatures.insert(curvatures.begin(), curvatures[0] * (1 + deltaT[0]/deltaT[1]) - curvatures[1] * (deltaT[0]/deltaT[1]));\n    curvatures.push_back(curvatures.back() * (1 + deltaT[size - 1]/deltaT[size - 2])\n            - curvatures[curvatures.size() - 2] * (deltaT[size - 1]/deltaT[size - 2]));\n\n    return curvatures;\n}\n"
  },
  {
    "path": "tests/demo/spline_library/splines/quintic_hermite_spline.h",
    "content": "#pragma once\n\n#include <cassert>\n\n#include \"../spline.h\"\n\ntemplate<class InterpolationType, typename floating_t>\nclass QuinticHermiteSplineCommon\n{\npublic:\n    struct alignas(16) QuinticHermiteSplinePoint\n    {\n        InterpolationType position, tangent, curvature;\n    };\n\n    inline QuinticHermiteSplineCommon(void) = default;\n    inline QuinticHermiteSplineCommon(std::vector<QuinticHermiteSplinePoint> points, std::vector<floating_t> knots)\n        :points(std::move(points)), knots(std::move(knots))\n    {}\n\n    inline size_t segmentCount(void) const\n    {\n        return points.size() - 1;\n    }\n\n    inline size_t segmentForT(floating_t t) const\n    {\n        size_t segmentIndex = SplineCommon::getIndexForT(knots, t);\n        if(segmentIndex >= segmentCount())\n            return segmentCount() - 1;\n        else\n            return segmentIndex;\n    }\n\n    inline floating_t segmentT(size_t segmentIndex) const\n    {\n        return knots[segmentIndex];\n    }\n\n    inline InterpolationType getPosition(floating_t globalT) const\n    {\n        //get the knot index. if it's the final knot, back it up by one\n        size_t knotIndex = SplineCommon::getIndexForT(knots, globalT);\n        if(knotIndex >= knots.size() - 1)\n            knotIndex--;\n\n        floating_t tDiff = (knots[knotIndex + 1] - knots[knotIndex]);\n        floating_t localT = (globalT - knots[knotIndex]) / tDiff;\n\n        return computePosition(knotIndex, tDiff, localT);\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPT getTangent(floating_t globalT) const\n    {\n        //get the knot index. if it's the final knot, back it up by one\n        size_t knotIndex = SplineCommon::getIndexForT(knots, globalT);\n        if(knotIndex >= knots.size() - 1)\n            knotIndex--;\n\n        floating_t tDiff = (knots[knotIndex + 1] - knots[knotIndex]);\n        floating_t localT = (globalT - knots[knotIndex]) / tDiff;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPT(\n                    computePosition(knotIndex, tDiff, localT),\n                    computeTangent(knotIndex, tDiff, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTC getCurvature(floating_t globalT) const\n    {\n        //get the knot index. if it's the final knot, back it up by one\n        size_t knotIndex = SplineCommon::getIndexForT(knots, globalT);\n        if(knotIndex >= knots.size() - 1)\n            knotIndex--;\n\n        floating_t tDiff = (knots[knotIndex + 1] - knots[knotIndex]);\n        floating_t localT = (globalT - knots[knotIndex]) / tDiff;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTC(\n                    computePosition(knotIndex, tDiff, localT),\n                    computeTangent(knotIndex, tDiff, localT),\n                    computeCurvature(knotIndex, tDiff, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTCW getWiggle(floating_t globalT) const\n    {\n        //get the knot index. if it's the final knot, back it up by one\n        size_t knotIndex = SplineCommon::getIndexForT(knots, globalT);\n        if(knotIndex >= knots.size() - 1)\n            knotIndex--;\n\n        floating_t tDiff = (knots[knotIndex + 1] - knots[knotIndex]);\n        floating_t localT = (globalT - knots[knotIndex]) / tDiff;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTCW(\n                    computePosition(knotIndex, tDiff, localT),\n                    computeTangent(knotIndex, tDiff, localT),\n                    computeCurvature(knotIndex, tDiff, localT),\n                    computeWiggle(knotIndex, tDiff, localT)\n                    );\n    }\n\n    inline floating_t segmentLength(size_t index, floating_t a, floating_t b) const\n    {\n        floating_t tDiff = knots[index + 1] - knots[index];\n        auto segmentFunction = [this, index, tDiff](floating_t t) -> floating_t {\n            auto tangent = computeTangent(index, tDiff, t);\n            return tangent.length();\n        };\n\n        floating_t localA = (a - knots[index]) / tDiff;\n        floating_t localB = (b - knots[index]) / tDiff;\n\n        return tDiff * SplineLibraryCalculus::gaussLegendreQuadratureIntegral<floating_t>(segmentFunction, localA, localB);\n    }\n\nprivate: //methods\n    inline InterpolationType computePosition(size_t index, floating_t tDiff, floating_t t) const\n    {\n        //this is a logical extension of the cubic hermite spline's basis functions\n        //that has one basis function for t0 position, one for t1 position\n        //one for t0 tangent (1st derivative of position), and one for t1 tangent\n        //this adds 2 more basis functions, one for t0 curvature (2nd derivative) and t1 curvature\n        //see this paper for details http://www.rose-hulman.edu/~finn/CCLI/Notes/day09.pdf\n        auto basis00 = (1 - t) * (1 - t) * (1 - t) * (t * (6 * t + 3) + 1);\n        auto basis10 = t * (1 - t) * (1 - t) * (1 - t) * (3 * t + 1);\n        auto basis20 = floating_t(0.5) * (1 - t) * (1 - t) * (1 - t) * t * t;\n        auto basis21 = floating_t(0.5) * (1 - t) * (1 - t) * t * t * t;\n        auto basis11 = t * t * t * (1 - t) * (t * 3 - 4);\n        auto basis01 = t * t * t * (t * (6 * t - 15) + 10);\n\n        return\n                basis00 * points[index].position +\n                basis10 * tDiff * points[index].tangent +\n                basis20 * tDiff * tDiff * points[index].curvature +\n\n                basis21 * tDiff * tDiff * points[index + 1].curvature +\n                basis11 * tDiff * points[index + 1].tangent +\n                basis01 * points[index + 1].position;\n    }\n\n    inline InterpolationType computeTangent(size_t index, floating_t tDiff, floating_t t) const\n    {\n        //we're computing the derivative of the computePosition function with respect to t\n        //we can do this by computing the derivatives of each of its basis functions.\n        //thankfully this can easily be done analytically since they're polynomials!\n        auto d_basis00 =  (-30) * (1 - t) * (1 - t) * t * t;\n        auto d_basis10 = (1 - t) * (1 - t) * (1 - 3 * t) * (5 * t + 1);\n        auto d_basis20 = floating_t(-0.5) * (1 - t) * (1 - t) * t * (5 * t - 2);\n        auto d_basis21 = floating_t(0.5) * (1 - t) * t * t * (3 - 5 * t);\n        auto d_basis11 = t * t * (2 - 3 * t) * (5 * t - 6);\n        auto d_basis01 = 30 * (t - 1) * (t - 1) * t * t;\n\n        //tests and such have shown that we have to scale this by the inverse of the t distance, and i'm not sure why\n        //intuitively it would just be the derivative of the position function and nothing else\n        //if you know why please let me know\n        return (\n                    d_basis00 * points[index].position +\n                    d_basis10 * tDiff * points[index].tangent +\n                    d_basis20 * tDiff * tDiff * points[index].curvature +\n\n                    d_basis21 * tDiff * tDiff * points[index + 1].curvature +\n                    d_basis11 * tDiff * points[index + 1].tangent +\n                    d_basis01 * points[index + 1].position\n                ) / tDiff;\n    }\n\n    inline InterpolationType computeCurvature(size_t index, floating_t tDiff, floating_t t) const\n    {\n        //we're computing the second derivative of the computePosition function with respect to t\n        //we can do this by computing the second derivatives of each of its basis functions.\n        //thankfully this can easily be done analytically since they're polynomials!\n        auto d2_basis00 = t * ((180 - 120 * t) * t - 60);\n        auto d2_basis10 = t * ((96 - 60 * t) * t - 36);\n        auto d2_basis20 = t * ((18  -10 * t) * t - 9) + 1;\n        auto d2_basis21 = t * (t * (10 * t - 12) + 3);\n        auto d2_basis11 = t * ((84 - 60 * t) * t - 24);\n        auto d2_basis01 = -d2_basis00;\n\n        //tests and such have shown that we have to scale this by the inverse of the t distance, and i'm not sure why\n        //intuitively it would just be the 2nd derivative of the position function and nothing else\n        //if you know why please let me know\n        return (\n                    d2_basis00 * points[index].position +\n                    d2_basis10 * tDiff * points[index].tangent +\n                    d2_basis20 * tDiff * tDiff * points[index].curvature +\n\n                    d2_basis21 * tDiff * tDiff * points[index + 1].curvature +\n                    d2_basis11 * tDiff * points[index + 1].tangent +\n                    d2_basis01 * points[index + 1].position\n                ) / (tDiff * tDiff);\n    }\n\n    inline InterpolationType computeWiggle(size_t index, floating_t tDiff, floating_t t) const\n    {\n        //we're computing the third derivative of the computePosition function with respect to t\n        auto d3_basis00 = (360 - 360*t) * t - 60;\n        auto d3_basis10 = (192 - 180*t) * t - 36;\n        auto d3_basis20 = (36 - 30 * t) * t - 9;\n        auto d3_basis21 = (30 * t - 24) * t + 3;\n        auto d3_basis11 = (168 - 180*t) * t - 24;\n        auto d3_basis01 = -d3_basis00;\n\n        //tests and such have shown that we have to scale this by the inverse of the t distance, and i'm not sure why\n        //intuitively it would just be the 2nd derivative of the position function and nothing else\n        //if you know why please let me know\n        return (\n                    d3_basis00 * points[index].position +\n                    d3_basis10 * tDiff * points[index].tangent +\n                    d3_basis20 * tDiff * tDiff * points[index].curvature +\n\n                    d3_basis21 * tDiff * tDiff * points[index + 1].curvature +\n                    d3_basis11 * tDiff * points[index + 1].tangent +\n                    d3_basis01 * points[index + 1].position\n                ) / (tDiff * tDiff * tDiff);\n    }\n\nprivate: //data\n    std::vector<QuinticHermiteSplinePoint> points;\n    std::vector<floating_t> knots;\n};\n\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass QuinticHermiteSpline final : public SplineImpl<QuinticHermiteSplineCommon, InterpolationType, floating_t>\n{\n//constructors\npublic:\n    QuinticHermiteSpline(const std::vector<InterpolationType> &points,\n                         const std::vector<InterpolationType> &tangents,\n                         const std::vector<InterpolationType> &curvatures,\n                         floating_t alpha = 0.0\n                         )\n        :SplineImpl<QuinticHermiteSplineCommon, InterpolationType,floating_t>(points, points.size() - 1)\n    {\n        assert(points.size() >= 2);\n        assert(points.size() == tangents.size());\n        assert(points.size() == curvatures.size());\n\n        //compute the T values for each point\n        std::vector<floating_t> knots = SplineCommon::computeTValuesWithInnerPadding(points, alpha, 0);\n\n        //pre-arrange the data needed for interpolation\n        std::vector<typename QuinticHermiteSplineCommon<InterpolationType, floating_t>::QuinticHermiteSplinePoint> positionData(points.size());\n        for(size_t i = 0; i < points.size(); i++)\n        {\n            positionData[i].position = points.at(i);\n            positionData[i].tangent = tangents.at(i);\n            positionData[i].curvature = curvatures.at(i);\n        }\n\n        this->common = QuinticHermiteSplineCommon<InterpolationType, floating_t>(std::move(positionData), std::move(knots));\n    }\n\n    QuinticHermiteSpline(const std::vector<InterpolationType> &points, floating_t alpha = 0.0f)\n        :SplineImpl<QuinticHermiteSplineCommon, InterpolationType,floating_t>(points, points.size() - 5)\n    {\n        assert(points.size() >= 6);\n\n        size_t size = points.size();\n        size_t numSegments = points.size() - 5;\n\n        //compute the T values for each point\n        size_t padding = 2;\n        std::vector<floating_t> paddedKnots = SplineCommon::computeTValuesWithInnerPadding(points, alpha, padding);\n\n        //compute the tangents\n        std::vector<InterpolationType> tangents(size);\n        size_t firstTangent = 1;\n        size_t lastTangent = points.size() - 2;\n        for(size_t i = firstTangent; i <= lastTangent; i++)\n        {\n            floating_t tPrev = paddedKnots[i - 1];\n            floating_t tCurrent = paddedKnots[i];\n            floating_t tNext = paddedKnots[i + 1];\n\n            InterpolationType pPrev = points.at(i - 1);\n            InterpolationType pCurrent = points.at(i);\n            InterpolationType pNext = points.at(i + 1);\n\n            //the tangent is the standard catmull-rom spline tangent calculation\n            tangents[i] =\n                      pPrev * (tCurrent - tNext) / ((tNext - tPrev) * (tCurrent - tPrev))\n                    + pNext * (tCurrent - tPrev) / ((tNext - tPrev) * (tNext - tCurrent))\n\n                 //plus a little something extra - this is derived from the pyramid contruction\n                 //when the t values are evenly spaced (ie when alpha is 0), this whole line collapses to 0,\n                 //yielding the standard catmull-rom formula\n                    - pCurrent * ((tCurrent - tPrev) - (tNext - tCurrent)) / ((tNext - tCurrent) * (tCurrent - tPrev));\n        }\n\n        //compute the curvatures\n        std::vector<InterpolationType> curves(size);\n        size_t firstCurvature = padding = 2;\n        size_t lastCurvature = points.size() - 3;\n        for(size_t i = firstCurvature; i <= lastCurvature; i++)\n        {\n            floating_t tPrev = paddedKnots[i - 1];\n            floating_t tCurrent = paddedKnots[i];\n            floating_t tNext = paddedKnots[i + 1];\n\n            InterpolationType pPrev = tangents.at(i - 1);\n            InterpolationType pCurrent = tangents.at(i);\n            InterpolationType pNext = tangents.at(i + 1);\n\n            //the tangent is the standard catmull-rom spline tangent calculation\n            curves[i] =\n                      pPrev * (tCurrent - tNext) / ((tNext - tPrev) * (tCurrent - tPrev))\n                    + pNext * (tCurrent - tPrev) / ((tNext - tPrev) * (tNext - tCurrent))\n\n                 //plus a little something extra - this is derived from the pyramid contruction\n                 //when the t values are evenly spaced (ie when alpha is 0), this whole line collapses to 0,\n                 //yielding the standard catmull-rom formula\n                    - pCurrent * ((tCurrent - tPrev) - (tNext - tCurrent)) / ((tNext - tCurrent) * (tCurrent - tPrev));\n        }\n\n\n        //pre-arrange the data needed for interpolation\n        std::vector<floating_t> knots(numSegments + 1);\n        std::vector<typename QuinticHermiteSplineCommon<InterpolationType, floating_t>::QuinticHermiteSplinePoint> positionData(numSegments + 1);\n        for(size_t i = 0; i < positionData.size(); i++)\n        {\n            knots[i] = paddedKnots[i + padding];\n\n            positionData[i].position = points[i + padding];\n            positionData[i].tangent = tangents[i + padding];\n            positionData[i].curvature = curves[i + padding];\n        }\n        this->common = QuinticHermiteSplineCommon<InterpolationType, floating_t>(std::move(positionData), std::move(knots));\n    }\n};\n\n\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass LoopingQuinticHermiteSpline final : public SplineLoopingImpl<QuinticHermiteSplineCommon, InterpolationType, floating_t>\n{\n//constructors\npublic:\n    LoopingQuinticHermiteSpline(const std::vector<InterpolationType> &points,\n                                const std::vector<InterpolationType> &tangents,\n                                const std::vector<InterpolationType> &curvatures,\n                                floating_t alpha = 0.0\n                                )\n        :SplineLoopingImpl<QuinticHermiteSplineCommon, InterpolationType,floating_t>(points, points.size())\n    {\n        assert(points.size() >= 2);\n        assert(points.size() == tangents.size());\n        assert(points.size() == curvatures.size());\n\n        //compute the T values for each point\n        std::vector<floating_t> knots = SplineCommon::computeLoopingTValues(points, alpha, 0);\n\n        //pre-arrange the data needed for interpolation\n        std::vector<typename QuinticHermiteSplineCommon<InterpolationType, floating_t>::QuinticHermiteSplinePoint> positionData(points.size() + 1);\n        for(size_t i = 0; i < points.size(); i++)\n        {\n            positionData[i].position = points.at(i);\n            positionData[i].tangent = tangents.at(i);\n            positionData[i].curvature = curvatures.at(i);\n        }\n        positionData[points.size()] = positionData[0];\n\n        this->common = QuinticHermiteSplineCommon<InterpolationType, floating_t>(std::move(positionData), std::move(knots));\n    }\n\n    LoopingQuinticHermiteSpline(const std::vector<InterpolationType> &points, floating_t alpha = 0.0)\n        :SplineLoopingImpl<QuinticHermiteSplineCommon, InterpolationType,floating_t>(points, points.size())\n    {\n        assert(points.size() >= 3);\n\n        int size = int(points.size());\n\n        //compute the T values for each point\n        size_t padding = 2;\n        std::vector<floating_t> paddedKnots = SplineCommon::computeLoopingTValues(points, alpha, padding);\n\n        //compute the tangents\n        std::vector<InterpolationType> tangents(size);\n        for(int i = 0; i < size; i++)\n        {\n            floating_t tPrev = paddedKnots[i - 1 + padding];\n            floating_t tCurrent = paddedKnots[i + padding];\n            floating_t tNext = paddedKnots[i + 1 + padding];\n\n            InterpolationType pPrev = points[(i - 1 + size)%size];\n            InterpolationType pCurrent = points[i];\n            InterpolationType pNext = points[(i + 1)%size];\n\n            //the tangent is the standard catmull-rom spline tangent calculation\n            tangents[i] =\n                      pPrev * (tCurrent - tNext) / ((tNext - tPrev) * (tCurrent - tPrev))\n                    + pNext * (tCurrent - tPrev) / ((tNext - tPrev) * (tNext - tCurrent))\n\n                 //plus a little something extra - this is derived from the pyramid contruction\n                 //when the t values are evenly spaced (ie when alpha is 0), this whole line collapses to 0,\n                 //yielding the standard catmull-rom formula\n                    - pCurrent * ((tCurrent - tPrev) - (tNext - tCurrent)) / ((tNext - tCurrent) * (tCurrent - tPrev));\n        }\n\n        //compute the curvatures\n        std::vector<InterpolationType> curves(size);\n        for(int i = 0; i < size; i++)\n        {\n            floating_t tPrev = paddedKnots[i - 1 + padding];\n            floating_t tCurrent = paddedKnots[i + padding];\n            floating_t tNext = paddedKnots[i + 1 + padding];\n\n            InterpolationType pPrev = tangents[(i - 1 + size)%size];\n            InterpolationType pCurrent = tangents[i];\n            InterpolationType pNext = tangents[(i + 1)%size];\n\n            //the tangent is the standard catmull-rom spline tangent calculation\n            curves[i] =\n                      pPrev * (tCurrent - tNext) / ((tNext - tPrev) * (tCurrent - tPrev))\n                    + pNext * (tCurrent - tPrev) / ((tNext - tPrev) * (tNext - tCurrent))\n\n                 //plus a little something extra - this is derived from the pyramid contruction\n                 //when the t values are evenly spaced (ie when alpha is 0), this whole line collapses to 0,\n                 //yielding the standard catmull-rom formula\n                    - pCurrent * ((tCurrent - tPrev) - (tNext - tCurrent)) / ((tNext - tCurrent) * (tCurrent - tPrev));\n        }\n\n\n        //pre-arrange the data needed for interpolation\n        std::vector<floating_t> knots(size + 1);\n        std::vector<typename QuinticHermiteSplineCommon<InterpolationType, floating_t>::QuinticHermiteSplinePoint> positionData(size + 1);\n        for(int i = 0; i < size; i++)\n        {\n            knots[i] = paddedKnots[i + padding];\n            positionData[i].position = points[i];\n            positionData[i].tangent = tangents[i];\n            positionData[i].curvature = curves[i];\n        }\n        positionData[size] = positionData[0];\n        knots[size] = paddedKnots[size + padding];\n\n        this->common = QuinticHermiteSplineCommon<InterpolationType, floating_t>(std::move(positionData), std::move(knots));\n    }\n};\n"
  },
  {
    "path": "tests/demo/spline_library/splines/uniform_cr_spline.h",
    "content": "#pragma once\n\n#include <cassert>\n\n#include \"../spline.h\"\n\ntemplate<class InterpolationType, typename floating_t>\nclass UniformCRSplineCommon\n{\npublic:\n\n    inline UniformCRSplineCommon(void) = default;\n    inline UniformCRSplineCommon(std::vector<InterpolationType> points)\n        :points(std::move(points))\n    {}\n\n    inline size_t segmentCount(void) const\n    {\n        return points.size() - 3;\n    }\n\n    inline size_t segmentForT(floating_t t) const\n    {\n        if(t < 0)\n            return 0;\n\n        size_t segmentIndex = size_t(t);\n        if(segmentIndex > segmentCount() - 1)\n            return segmentCount() - 1;\n        else\n            return segmentIndex;\n    }\n\n    inline floating_t segmentT(size_t segmentIndex) const\n    {\n        return segmentIndex;\n    }\n\n\n    inline InterpolationType getPosition(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        floating_t localT = globalT - segmentIndex;\n\n        return computePosition(segmentIndex + 1, localT);\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPT getTangent(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        floating_t localT = globalT - segmentIndex;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPT(\n                    computePosition(segmentIndex + 1, localT),\n                    computeTangent(segmentIndex + 1, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTC getCurvature(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        floating_t localT = globalT - segmentIndex;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTC(\n                    computePosition(segmentIndex + 1, localT),\n                    computeTangent(segmentIndex + 1, localT),\n                    computeCurvature(segmentIndex + 1, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTCW getWiggle(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        floating_t localT = globalT - segmentIndex;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTCW(\n                    computePosition(segmentIndex + 1, localT),\n                    computeTangent(segmentIndex + 1, localT),\n                    computeCurvature(segmentIndex + 1, localT),\n                    computeWiggle(segmentIndex + 1)\n                    );\n    }\n\n    inline floating_t segmentLength(size_t index, floating_t a, floating_t b) const\n    {\n        auto segmentFunction = [this, index](floating_t t) -> floating_t {\n            auto tangent = computeTangent(index + 1, t);\n            return tangent.length();\n        };\n\n        floating_t localA = a - index;\n        floating_t localB = b - index;\n\n        return SplineLibraryCalculus::gaussLegendreQuadratureIntegral<floating_t>(segmentFunction, localA, localB);\n    }\n\n\nprivate: //methods\n    inline InterpolationType computePosition(size_t index, floating_t t) const\n    {\n        auto beforeTangent = computeTangentAtIndex(index);\n        auto afterTangent = computeTangentAtIndex(index + 1);\n\n        auto oneMinusT = 1 - t;\n\n        auto basis00 = (1 + 2*t) * oneMinusT * oneMinusT;\n        auto basis10 = t * oneMinusT * oneMinusT;\n\n        auto basis11 = t * t * -oneMinusT;\n        auto basis01 = t * t * (3 - 2*t);\n\n        return\n                basis00 * points[index] +\n                basis10 * beforeTangent +\n\n                basis11 * afterTangent +\n                basis01 * points[index + 1];\n    }\n\n    inline InterpolationType computeTangent(size_t index, floating_t t) const\n    {\n        auto beforeTangent = computeTangentAtIndex(index);\n        auto afterTangent = computeTangentAtIndex(index + 1);\n\n        auto oneMinusT = 1 - t;\n\n        auto d_basis00 = 6 * t * (t - 1);\n        auto d_basis10 = (1 - 3*t) * oneMinusT;\n\n        auto d_basis11 = t * (3 * t - 2);\n        auto d_basis01 = -d_basis00;\n\n        //tests and such have shown that we have to scale this by the inverse of the t distance, and i'm not sure why\n        //intuitively it would just be the derivative of the position function and nothing else\n        //if you know why please let me know\n        return\n                d_basis00 * points[index] +\n                d_basis10 * beforeTangent +\n\n                d_basis11 * afterTangent +\n                d_basis01 * points[index + 1];\n    }\n\n    inline InterpolationType computeCurvature(size_t index, floating_t t) const\n    {\n        auto beforeTangent = computeTangentAtIndex(index);\n        auto afterTangent = computeTangentAtIndex(index + 1);\n\n        auto d2_basis00 = 6 * (2 * t - 1);\n        auto d2_basis10 = 2 * (3 * t - 2);\n\n        auto d2_basis11 = 2 * (3 * t - 1);\n        auto d2_basis01 = -d2_basis00;\n\n        //tests and such have shown that we have to scale this by the inverse of the t distance, and i'm not sure why\n        //intuitively it would just be the 2nd derivative of the position function and nothing else\n        //if you know why please let me know\n        return\n                d2_basis00 * points[index] +\n                d2_basis10 * beforeTangent +\n\n                d2_basis11 * afterTangent +\n                d2_basis01 * points[index + 1];\n    }\n\n    inline InterpolationType computeWiggle(size_t index) const\n    {\n        auto beforeTangent = computeTangentAtIndex(index);\n        auto afterTangent = computeTangentAtIndex(index + 1);\n\n        //tests and such have shown that we have to scale this by the inverse of the t distance, and i'm not sure why\n        //intuitively it would just be the 2nd derivative of the position function and nothing else\n        //if you know why please let me know\n        return floating_t(12) * (points[index] - points[index + 1]) + floating_t(6) * (beforeTangent + afterTangent);\n    }\n\n    inline InterpolationType computeTangentAtIndex(size_t i) const\n    {\n        return (points[i + 1] - points[i - 1]) / floating_t(2);\n    }\n\nprivate: //data\n    std::vector<InterpolationType> points;\n};\n\n\n\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass UniformCRSpline final : public SplineImpl<UniformCRSplineCommon, InterpolationType, floating_t>\n{\n//constructors\npublic:\n    UniformCRSpline(const std::vector<InterpolationType> &points)\n        :SplineImpl<UniformCRSplineCommon, InterpolationType, floating_t>(points, points.size() - 3)\n    {\n        assert(points.size() >= 4);\n\n        this->common = UniformCRSplineCommon<InterpolationType, floating_t>(points);\n    }\n};\n\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass LoopingUniformCRSpline final : public SplineLoopingImpl<UniformCRSplineCommon, InterpolationType, floating_t>\n{\n//constructors\npublic:\n    LoopingUniformCRSpline(const std::vector<InterpolationType> &points)\n        :SplineLoopingImpl<UniformCRSplineCommon, InterpolationType,floating_t>(points, points.size())\n    {\n        assert(points.size() >= 4);\n\n        //we need enough space to repeat the last 'degree' elements\n        std::vector<InterpolationType> positions(points.size() + 3);\n\n        //it would be easiest to just copy the points vector to the position vector, then copy the first 'degree' elements again\n        //this DOES work, but interpolation begins in the wrong place (ie getPosition(0) occurs at the wrong place on the spline)\n        //to fix this, we effectively \"rotate\" the position vector backwards, by copying point[size-1] to the beginning\n        //then copying the points vector in after, then copying degree-1 elements from the beginning\n        positions[0] = points.back();\n        std::copy(points.begin(), points.end(), positions.begin() + 1);\n        std::copy_n(points.begin(), 2, positions.end() - 2);\n\n        this->common = UniformCRSplineCommon<InterpolationType, floating_t>(std::move(positions));\n    }\n};\n"
  },
  {
    "path": "tests/demo/spline_library/splines/uniform_cubic_bspline.h",
    "content": "#pragma once\n\n#include <cassert>\n\n#include \"../spline.h\"\n\ntemplate<class InterpolationType, typename floating_t>\nclass UniformCubicBSplineCommon\n{\npublic:\n\n    inline UniformCubicBSplineCommon(void) = default;\n    inline UniformCubicBSplineCommon(std::vector<InterpolationType> points)\n        :points(std::move(points))\n    {}\n\n    inline size_t segmentCount(void) const\n    {\n        return points.size() - 3;\n    }\n\n    inline size_t segmentForT(floating_t t) const\n    {\n        if(t < 0)\n            return 0;\n\n        size_t segmentIndex = size_t(t);\n        if(segmentIndex > segmentCount() - 1)\n            return segmentCount() - 1;\n        else\n            return segmentIndex;\n    }\n\n    inline floating_t segmentT(size_t segmentIndex) const\n    {\n        return segmentIndex;\n    }\n\n    inline InterpolationType getPosition(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        floating_t localT = globalT - segmentIndex;\n\n        return computePosition(segmentIndex, localT);\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPT getTangent(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        floating_t localT = globalT - segmentIndex;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPT(\n                    computePosition(segmentIndex, localT),\n                    computeTangent(segmentIndex, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTC getCurvature(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        floating_t localT = globalT - segmentIndex;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTC(\n                    computePosition(segmentIndex, localT),\n                    computeTangent(segmentIndex, localT),\n                    computeCurvature(segmentIndex, localT)\n                    );\n    }\n\n    inline typename Spline<InterpolationType,floating_t>::InterpolatedPTCW getWiggle(floating_t globalT) const\n    {\n        size_t segmentIndex = segmentForT(globalT);\n        floating_t localT = globalT - segmentIndex;\n\n        return typename Spline<InterpolationType,floating_t>::InterpolatedPTCW(\n                    computePosition(segmentIndex, localT),\n                    computeTangent(segmentIndex, localT),\n                    computeCurvature(segmentIndex, localT),\n                    computeWiggle(segmentIndex)\n                    );\n    }\n\n    inline floating_t segmentLength(size_t index, floating_t a, floating_t b) const\n    {\n        auto segmentFunction = [this, index](floating_t t) -> floating_t {\n            auto tangent = computeTangent(index, t);\n            return tangent.length();\n        };\n\n        floating_t localA = a - index;\n        floating_t localB = b - index;\n\n        return SplineLibraryCalculus::gaussLegendreQuadratureIntegral<floating_t>(segmentFunction, localA, localB);\n    }\n\nprivate: //methods\n    inline InterpolationType computePosition(size_t index, floating_t t) const\n    {\n        return (\n                    points[index] * ((1 - t) * (1 - t) * (1 - t)) +\n                    points[index + 1] * (t * t * 3 * (t - 2) + 4) +\n                    points[index + 2] * (t * (t * (-3 * t + 3) + 3) + 1) +\n                    points[index + 3] * (t * t * t)\n                ) / floating_t(6);\n    }\n\n    inline InterpolationType computeTangent(size_t index, floating_t t) const\n    {\n        return (\n                    points[index] * (-(1 - t) * (1 - t)) +\n                    points[index + 1] * (t * (3 * t - 4)) +\n                    points[index + 2] * ((3 * t + 1) * (1 - t)) +\n                    points[index + 3] * (t * t)\n                ) / floating_t(2);\n    }\n\n    inline InterpolationType computeCurvature(size_t index, floating_t t) const\n    {\n        return (\n                    points[index] * (1 - t) +\n                    points[index + 1] * (3 * t - 2) +\n                    points[index + 2] * (1 - 3 * t) +\n                    points[index + 3] * (t)\n                );\n    }\n\n    inline InterpolationType computeWiggle(size_t index) const\n    {\n        return floating_t(3) * (points[index + 1] - points[index + 2]) + (points[index + 3] - points[index]);\n    }\n\nprivate: //data\n    std::vector<InterpolationType> points;\n};\n\n\n\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass UniformCubicBSpline final : public SplineImpl<UniformCubicBSplineCommon, InterpolationType, floating_t>\n{\npublic:\n    UniformCubicBSpline(const std::vector<InterpolationType> &points)\n        :SplineImpl<UniformCubicBSplineCommon, InterpolationType,floating_t>(points, points.size() - 3)\n    {\n        assert(points.size() >= 4);\n\n        this->common = UniformCubicBSplineCommon<InterpolationType, floating_t>(points);\n    }\n};\n\n\n\ntemplate<class InterpolationType, typename floating_t=float>\nclass LoopingUniformCubicBSpline final : public SplineLoopingImpl<UniformCubicBSplineCommon, InterpolationType, floating_t>\n{\npublic:\n    LoopingUniformCubicBSpline(const std::vector<InterpolationType> &points)\n        :SplineLoopingImpl<UniformCubicBSplineCommon, InterpolationType,floating_t>(points, points.size())\n    {\n        size_t degree = 3;\n\n        assert(points.size() >= degree);\n\n        //we need enough space to repeat the last 'degree' elements\n        std::vector<InterpolationType> positions(points.size() + degree);\n\n        //it would be easiest to just copy the points vector to the position vector, then copy the first 'degree' elements again\n        //this DOES work, but interpolation begins in the wrong place (ie getPosition(0) occurs at the wrong place on the spline)\n        //to fix this, we effectively \"rotate\" the position vector backwards, by copying point[size-1] to the beginning\n        //then copying the points vector in after, then copying degree-1 elements from the beginning\n        positions[0] = points[points.size() - 1];\n        std::copy(points.begin(), points.end(), positions.begin() + 1);\n        std::copy_n(points.begin(), degree - 1, positions.end() - (degree - 1));\n\n        this->common = UniformCubicBSplineCommon<InterpolationType, floating_t>(positions);\n    }\n};\n"
  },
  {
    "path": "tests/demo/spline_library/utils/arclength.h",
    "content": "#pragma once\n\n#include <boost/math/tools/roots.hpp>\n\n#include \"spline_common.h\"\n\nnamespace __ArcLengthSolvePrivate\n{\n    //solve the arc length for a single spline segment\n    template<template <class, typename> class Spline, class InterpolationType, typename floating_t>\n    floating_t solveSegment(const Spline<InterpolationType, floating_t>& spline, size_t segmentIndex, floating_t desiredLength, floating_t maxLength, floating_t segmentA)\n    {\n        //we can use the lengths we've calculated to formulate a pretty solid guess\n        //if desired length is x% of the bLength, then our guess will be x% of the way from aPercent to 1\n        floating_t desiredPercent = desiredLength / maxLength;\n        floating_t bEnd = spline.segmentT(segmentIndex + 1);\n        floating_t bGuess = segmentA + desiredPercent * (bEnd - segmentA);\n\n        auto solveFunction = [&](floating_t b) {\n            floating_t value = spline.segmentArcLength(segmentIndex, segmentA, b) - desiredLength;\n\n            //the derivative will be the length of the tangent\n            auto interpolationResult = spline.getCurvature(b);\n            floating_t tangentLength = interpolationResult.tangent.length();\n\n            //the second derivative will be the curvature projected onto the tangent\n            interpolationResult.tangent /= tangentLength;\n            floating_t secondDerivative = InterpolationType::dotProduct(interpolationResult.tangent, interpolationResult.curvature);\n\n            return std::make_tuple(value, tangentLength, secondDerivative);\n        };\n\n        return boost::math::tools::halley_iterate(solveFunction, bGuess, segmentA, bEnd, int(std::numeric_limits<floating_t>::digits * 0.5));\n    }\n}\n\nnamespace ArcLength\n{\n    //compute b such that arcLength(a,b) == desiredLength\n    template<template <class, typename> class SplineT, class InterpolationType, typename floating_t>\n    floating_t solveLength(const SplineT<InterpolationType, floating_t>& spline, floating_t a, floating_t desiredLength)\n    {\n        size_t index = spline.segmentForT(a);\n\n        floating_t segmentLength;\n        floating_t segmentBegin = a;\n\n        //scan through the spline's segments until we find the segment that contains b\n        do\n        {\n            segmentLength = spline.segmentArcLength(index, segmentBegin, spline.segmentT(index + 1));\n\n            if(segmentLength < desiredLength)\n            {\n                index++;\n                desiredLength -= segmentLength;\n                segmentBegin = spline.segmentT(index);\n            }\n            else\n            {\n                break;\n            }\n        }\n        while(index < spline.segmentCount());\n\n        //if bIndex is equal to the segment count, we've hit the end of the spline, so return maxT\n        if(index == spline.segmentCount())\n        {\n            return spline.getMaxT();\n        }\n\n        return __ArcLengthSolvePrivate::solveSegment(spline, index, desiredLength, segmentLength, segmentBegin);\n    }\n\n    //compute b such that cyclicArcLength(a,b) == desiredLength, respecting the cyclic semantics of a looping spline\n    //IE, a can be out of range, if desiredLength is totalLength*2 + 1, the result will be equal to solveCyclic(a,1) + maxT*2\n    template<template <class, typename> class LoopingSplineT, class InterpolationType, typename floating_t>\n    floating_t solveLengthCyclic(const LoopingSplineT<InterpolationType, floating_t>& spline, floating_t a, floating_t desiredLength)\n    {\n        size_t index = spline.segmentForT(a);\n\n        floating_t wrappedA = spline.wrapT(a);\n        floating_t segmentBegin = wrappedA;\n        floating_t segmentLength = spline.segmentArcLength(index, segmentBegin, spline.segmentT(index + 1));\n\n        //scan through the spline's segments until we find the segment that contains b\n        while(segmentLength < desiredLength)\n        {\n            index++;\n            size_t wrappedIndex = index % spline.segmentCount();\n            desiredLength -= segmentLength;\n            segmentBegin = spline.segmentT(wrappedIndex);\n            segmentLength = spline.segmentArcLength(wrappedIndex, segmentBegin, spline.segmentT(wrappedIndex + 1));\n        }\n\n        //index % segmentCount is the segment that contains b, now solve for b within this segment\n        floating_t wrappedB = __ArcLengthSolvePrivate::solveSegment(spline, index % spline.segmentCount(), desiredLength, segmentLength, segmentBegin);\n\n        //we now have to \"unwrap\" b\n        floating_t initialWrap = a - wrappedA;\n        size_t numCycles = index / spline.segmentCount();\n\n        return wrappedB + initialWrap + numCycles * spline.getMaxT();\n    }\n\n\n    //subdivide the spline into pieces such that the arc length of each pieces is equal to desiredLength\n    //returns a list of t values marking the boundaries of each piece\n    //the first entry is always 0. the final entry is the T value that marks the end of the last cleanly-dividible piece\n    //The remainder that could not be divided is the piece between the last entry and maxT\n    template<template <class, typename> class Spline, class InterpolationType, typename floating_t>\n    std::vector<floating_t> partition(const Spline<InterpolationType, floating_t>& spline, floating_t lengthPerPiece)\n    {\n        //first, compute total arc length and arc length for each segment\n        std::vector<floating_t> segmentLengths(spline.segmentCount());\n        floating_t totalArcLength(0);\n        for(size_t i = 0; i < spline.segmentCount(); i++)\n        {\n            floating_t segmentLength = spline.segmentArcLength(i, spline.segmentT(i), spline.segmentT(i+1));\n            totalArcLength += segmentLength;\n            segmentLengths[i] = segmentLength;\n        }\n\n        size_t n = size_t(totalArcLength / lengthPerPiece) + 1;\n        std::vector<floating_t> pieces(n);\n\n        //set up the inter-piece state\n        floating_t segmentRemainder = segmentLengths[0];\n        size_t segmentIndex = 0;\n\n        //for each piece, perform the same algorithm as the \"solve\" method, except re-use work between segments by referring to the segmentLengths array\n        for(size_t i = 1; i < n; i++)\n        {\n            floating_t desiredLength = lengthPerPiece;\n            floating_t segmentBegin = pieces[i - 1];\n\n            //if the segment length is less than desiredLength, B will be in a different segment than A, so search though the spline until we find B's segment\n            while(segmentRemainder < desiredLength)\n            {\n                segmentIndex++;\n                desiredLength -= segmentRemainder;\n                segmentRemainder = segmentLengths[segmentIndex];\n                segmentBegin = spline.segmentT(segmentIndex);\n            }\n\n            //we've found the segment that b lies in, so solve for the remaining arc length within this segment\n            pieces[i] = __ArcLengthSolvePrivate::solveSegment(spline, segmentIndex, desiredLength, segmentRemainder, segmentBegin);\n\n            //set up the next iteration of the loop\n            segmentRemainder = segmentRemainder - desiredLength;\n        }\n        return pieces;\n    }\n\n    //subdivide the spline into N pieces such that each piece has the same arc length\n    //returns a list of N+1 T values, where return[i] is the T value of the beginning of a piece and return[i+1] is the T value of the end of a piece\n    //the first element in the returned list is always 0, and the last element is always spline.getMaxT()\n    template<template <class, typename> class Spline, class InterpolationType, typename floating_t>\n    std::vector<floating_t> partitionN(const Spline<InterpolationType, floating_t>& spline, size_t n)\n    {\n        //first, compute total arc length and arc length for each segment\n        std::vector<floating_t> segmentLengths(spline.segmentCount());\n        floating_t totalArcLength(0);\n        for(size_t i = 0; i < spline.segmentCount(); i++)\n        {\n            floating_t segmentLength = spline.segmentArcLength(i, spline.segmentT(i), spline.segmentT(i+1));\n            totalArcLength += segmentLength;\n            segmentLengths[i] = segmentLength;\n        }\n        const floating_t lengthPerPiece = totalArcLength / n;\n\n        //set up the result vector\n        std::vector<floating_t> pieces(n + 1);\n\n        //set up the inter-piece state\n        floating_t segmentRemainder = segmentLengths[0];\n        size_t segmentIndex = 0;\n\n        //for each piece, perform the same algorithm as the \"solve\" method, except re-use work between segments by referring to the segmentLengths array\n        for(size_t i = 1; i < n; i++)\n        {\n            floating_t desiredLength = lengthPerPiece;\n            floating_t segmentBegin = pieces[i - 1];\n\n            //if the segment length is less than desiredLength, B will be in a different segment than A, so search though the spline until we find B's segment\n            while(segmentRemainder < desiredLength)\n            {\n                segmentIndex++;\n                desiredLength -= segmentRemainder;\n                segmentRemainder = segmentLengths[segmentIndex];\n                segmentBegin = spline.segmentT(segmentIndex);\n            }\n\n            //we've found the segment that b lies in, so solve for the remaining arc length within this segment\n            pieces[i] = __ArcLengthSolvePrivate::solveSegment(spline, segmentIndex, desiredLength, segmentRemainder, segmentBegin);\n\n            //set up the next iteration of the loop\n            segmentRemainder = segmentRemainder - desiredLength;\n        }\n\n        pieces[n] = spline.getMaxT();\n        return pieces;\n    }\n}\n"
  },
  {
    "path": "tests/demo/spline_library/utils/calculus.h",
    "content": "#pragma once\n\n#include <cmath>\n#include <array>\n\nclass SplineLibraryCalculus {\nprivate:\n    SplineLibraryCalculus() = default;\n\npublic:\n    //use the gauss-legendre quadrature algorithm to numerically integrate f from a to b\n    //as of this writing, hardcoded to use 13 points\n    template<class IntegrandType, class Function, typename floating_t>\n    inline static IntegrandType gaussLegendreQuadratureIntegral(Function f, floating_t a, floating_t b)\n    {\n        const size_t NUM_POINTS = 13;\n\n        //these are precomputed :( It would be cool to compute these at compile time, but apparently\n        //it's not easy to compute the points/weights just given the number of points.\n        //it involves computing every root of a polynomial. which can obviously be done, but not in a reasonable amount of code\n        std::array<floating_t, NUM_POINTS> quadraturePoints = {\n            floating_t( 0.0000000000000000),\n            floating_t(-0.2304583159551348),\n            floating_t( 0.2304583159551348),\n            floating_t(-0.4484927510364469),\n            floating_t( 0.4484927510364469),\n            floating_t(-0.6423493394403402),\n            floating_t( 0.6423493394403402),\n            floating_t(-0.8015780907333099),\n            floating_t( 0.8015780907333099),\n            floating_t(-0.9175983992229779),\n            floating_t( 0.9175983992229779),\n            floating_t(-0.9841830547185881),\n            floating_t( 0.9841830547185881)\n        };\n\n        std::array<floating_t, NUM_POINTS> quadratureWeights = {\n            floating_t(0.2325515532308739),\n            floating_t(0.2262831802628972),\n            floating_t(0.2262831802628972),\n            floating_t(0.2078160475368885),\n            floating_t(0.2078160475368885),\n            floating_t(0.1781459807619457),\n            floating_t(0.1781459807619457),\n            floating_t(0.1388735102197872),\n            floating_t(0.1388735102197872),\n            floating_t(0.0921214998377285),\n            floating_t(0.0921214998377285),\n            floating_t(0.0404840047653159),\n            floating_t(0.0404840047653159)\n        };\n\n        floating_t halfDiff = (b - a) / 2;\n        floating_t halfSum = (a + b) / 2;\n\n        IntegrandType sum{};\n        for(size_t i = 0; i < NUM_POINTS; i++)\n        {\n            sum += quadratureWeights[i] * f(halfDiff * quadraturePoints[i] + halfSum);\n        }\n        return halfDiff * sum;\n    }\n};\n"
  },
  {
    "path": "tests/demo/spline_library/utils/linearalgebra.h",
    "content": "#pragma once\n\n#include <vector>\n\nclass LinearAlgebra\n{\nprivate:\n    LinearAlgebra() = default;\n\npublic:\n\n    //solve the given tridiagonal matrix system, with the assumption that the lower diagonal and upper diagonal (ie secondaryDiagonal) are identical.\n    //in other words, assume that the matrix is symmetric\n    template<class OutputType, typename floating_t>\n    static std::vector<OutputType> solveSymmetricTridiagonal(\n\n            std::vector<floating_t> mainDiagonal,\n            const std::vector<floating_t> secondaryDiagonal,\n            std::vector<OutputType> inputVector);\n\n    //solve the given tridiagonal matrix system\n    template<class OutputType, typename floating_t>\n    static std::vector<OutputType> solveTridiagonal(\n\n            std::vector<floating_t> mainDiagonal,\n            const std::vector<floating_t> upperDiagonal,\n            const std::vector<floating_t> lowerDiagonal,\n            std::vector<OutputType> inputVector);\n\n    //solve the given cyclic tridiagonal matrix system, with the assumption that the lower diagonal and upper diagonal (ie secondaryDiagonal) are identical\n    //in other words, assume that the matrix is symmetric\n    template<class OutputType, typename floating_t>\n    static std::vector<OutputType> solveCyclicSymmetricTridiagonal(\n\n            std::vector<floating_t> mainDiagonal,\n            std::vector<floating_t> secondaryDiagonal,\n            std::vector<OutputType> inputVector);\n};\n\ntemplate<class OutputType, typename floating_t>\nstd::vector<OutputType> LinearAlgebra::solveTridiagonal(\n\n        std::vector<floating_t> mainDiagonal,\n        const std::vector<floating_t> upperDiagonal,\n        const std::vector<floating_t> lowerDiagonal,\n        std::vector<OutputType> inputVector)\n{\n    //use the thomas algorithm to solve the tridiagonal matrix\n    // http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm\n\n    //forward sweep\n    for(size_t i = 1; i < inputVector.size(); i++)\n    {\n        floating_t m = lowerDiagonal[i - 1] / mainDiagonal[i - 1];\n        mainDiagonal[i] -= m * upperDiagonal[i - 1];\n        inputVector[i] -= m * inputVector[i - 1];\n    }\n\n    //back substitution\n    size_t finalIndex = inputVector.size();\n    inputVector[finalIndex - 1] /= mainDiagonal[finalIndex - 1];\n\n    for(size_t i = finalIndex - 1; i > 0; i--)\n    {\n        inputVector[i - 1] = (inputVector[i - 1] - upperDiagonal[i - 1] * inputVector[i]) / mainDiagonal[i - 1];\n    }\n\n    return inputVector;\n}\n\ntemplate<class OutputType, typename floating_t>\nstd::vector<OutputType> LinearAlgebra::solveSymmetricTridiagonal(\n\n        std::vector<floating_t> mainDiagonal,\n        const std::vector<floating_t> secondaryDiagonal,\n        std::vector<OutputType> inputVector)\n{\n    //use the thomas algorithm to solve the tridiagonal matrix\n    // http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm\n\n    //forward sweep\n    for(size_t i = 1; i < inputVector.size(); i++)\n    {\n        floating_t m = secondaryDiagonal[i - 1] / mainDiagonal[i - 1];\n        mainDiagonal[i] -= m * secondaryDiagonal[i - 1];\n        inputVector[i] -= m * inputVector[i - 1];\n    }\n\n    //back substitution\n    size_t finalIndex = inputVector.size();\n    inputVector[finalIndex - 1] /= mainDiagonal[finalIndex - 1];\n\n    for(size_t i = finalIndex - 1; i > 0; i--)\n    {\n        inputVector[i - 1] = (inputVector[i - 1] - secondaryDiagonal[i - 1] * inputVector[i]) / mainDiagonal[i - 1];\n    }\n\n    return inputVector;\n}\n\ntemplate<class OutputType, typename floating_t>\nstd::vector<OutputType> LinearAlgebra::solveCyclicSymmetricTridiagonal(\n\n        std::vector<floating_t> mainDiagonal,\n        std::vector<floating_t> secondaryDiagonal,\n        std::vector<OutputType> inputVector)\n{\n    //apply the sherman-morrison algorithm to the cyclic tridiagonal matrix so that we can use the standard tridiagonal algorithm\n    //we're getting this algorithm from http://www.cs.princeton.edu/courses/archive/fall11/cos323/notes/cos323_f11_lecture06_linsys2.pdf\n    //basically, we're going to solve two different non-cyclic versions of this system and then combine the results\n\n    size_t size = inputVector.size();\n\n\n    //the value at the upper right and lower left of the input matrix. it's at the end of the secondary diagonal array because almost all\n    //cyclic tridiagonal papers treat it as an extension of the secondary diagonals\n    floating_t cornerValue = secondaryDiagonal.at(size - 1);\n\n    //gamma value - doesn't affect actual output (the algorithm makes sure it cancels out), but a good choice for this value can reduce floating point errors\n    floating_t gamma = -mainDiagonal.at(0);\n    floating_t cornerMultiplier = cornerValue/gamma;\n\n    //corrective vector U: should be all 0, except for gamma in the first element, and cornerValue at the end\n    std::vector<floating_t> correctionInputU(size);\n    correctionInputU[0] = gamma;\n    correctionInputU[size - 1] = cornerValue;\n\n    //modify the main diagonal of the matrix to account for the correction vector\n    mainDiagonal[0] -= gamma;\n    mainDiagonal[size - 1] -= cornerValue * cornerMultiplier;\n\n    //solve the modified system for the input vector\n    std::vector<OutputType> initialOutput = solveSymmetricTridiagonal(\n                mainDiagonal,\n                secondaryDiagonal,\n                std::move(inputVector)\n                );\n\n    //solve the modified system for the correction vector\n    std::vector<floating_t> correctionOutput = solveSymmetricTridiagonal(\n                std::move(mainDiagonal),\n                std::move(secondaryDiagonal),\n                std::move(correctionInputU)\n                );\n\n    //compute the corrective OutputType to apply to each initial output\n    //this involves a couple dot products, but all of the elements on the correctionV vector are 0 except the first and last\n    //so just compute those directly instead of looping through and multplying a bunch of 0s\n    OutputType factor = (initialOutput.at(0) + initialOutput.at(size - 1) * cornerMultiplier) / (1 + correctionOutput.at(0) + correctionOutput.at(size - 1) * cornerMultiplier);\n\n    /*std::vector<floating_t> correctionV(size);\n    correctionV[0] = 1;\n    correctionV[size - 1] = cornerMultiplier;\n    OutputType factor = vectorDotProduct(initialOutput, correctionV) / (1 + vectorDotProduct(correctionV, correctionOutput));*/\n\n    //use the correction factor to modify the result\n    for(size_t i = 0; i < size; i++)\n    {\n        initialOutput[i] -= factor * correctionOutput.at(i);\n    }\n\n    return initialOutput;\n}\n"
  },
  {
    "path": "tests/demo/spline_library/utils/nanoflann.hpp",
    "content": "/***********************************************************************\n * Software License Agreement (BSD License)\n *\n * Copyright 2008-2009  Marius Muja (mariusm@cs.ubc.ca). All rights reserved.\n * Copyright 2008-2009  David G. Lowe (lowe@cs.ubc.ca). All rights reserved.\n * Copyright 2011-2013  Jose Luis Blanco (joseluisblancoc@gmail.com).\n *   All rights reserved.\n *\n * THE BSD LICENSE\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n * 1. Redistributions of source code must retain the above copyright\n *    notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n *    notice, this list of conditions and the following disclaimer in the\n *    documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n *************************************************************************/\n\n#ifndef  NANOFLANN_HPP_\n#define  NANOFLANN_HPP_\n\n#include <vector>\n#include <cassert>\n#include <algorithm>\n#include <stdexcept>\n#include <cstdio>  // for fwrite()\n#include <cmath>   // for fabs(),...\n#include <limits>\n\n// Avoid conflicting declaration of min/max macros in windows headers\n#if !defined(NOMINMAX) && (defined(_WIN32) || defined(_WIN32_)  || defined(WIN32) || defined(_WIN64))\n# define NOMINMAX\n# ifdef max\n#  undef   max\n#  undef   min\n# endif\n#endif\n\nnamespace nanoflann\n{\n/** @addtogroup nanoflann_grp nanoflann C++ library for ANN\n  *  @{ */\n\n  \t/** Library version: 0xMmP (M=Major,m=minor,P=path) */\n\t#define NANOFLANN_VERSION 0x117\n\n\t/** @addtogroup result_sets_grp Result set classes\n\t  *  @{ */\n\ttemplate <typename DistanceType, typename IndexType = size_t, typename CountType = size_t>\n\tclass KNNResultSet\n\t{\n\t\tIndexType * indices;\n\t\tDistanceType* dists;\n\t\tCountType capacity;\n\t\tCountType count;\n\n\tpublic:\n\t\tinline KNNResultSet(CountType capacity_) : capacity(capacity_), count(0)\n\t\t{\n\t\t}\n\n\t\tinline void init(IndexType* indices_, DistanceType* dists_)\n\t\t{\n\t\t\tindices = indices_;\n\t\t\tdists = dists_;\n\t\t\tcount = 0;\n\t\t\tdists[capacity-1] = (std::numeric_limits<DistanceType>::max)();\n\t\t}\n\n\t\tinline CountType size() const\n\t\t{\n\t\t\treturn count;\n\t\t}\n\n\t\tinline bool full() const\n\t\t{\n\t\t\treturn count == capacity;\n\t\t}\n\n\n\t\tinline void addPoint(DistanceType dist, IndexType index)\n\t\t{\n\t\t\tCountType i;\n\t\t\tfor (i=count; i>0; --i) {\n#ifdef NANOFLANN_FIRST_MATCH   // If defined and two poins have the same distance, the one with the lowest-index will be returned first.\n\t\t\t\tif ( (dists[i-1]>dist) || ((dist==dists[i-1])&&(indices[i-1]>index)) ) {\n#else\n\t\t\t\tif (dists[i-1]>dist) {\n#endif\n\t\t\t\t\tif (i<capacity) {\n\t\t\t\t\t\tdists[i] = dists[i-1];\n\t\t\t\t\t\tindices[i] = indices[i-1];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse break;\n\t\t\t}\n\t\t\tif (i<capacity) {\n\t\t\t\tdists[i] = dist;\n\t\t\t\tindices[i] = index;\n\t\t\t}\n\t\t\tif (count<capacity) count++;\n\t\t}\n\n\t\tinline DistanceType worstDist() const\n\t\t{\n\t\t\treturn dists[capacity-1];\n\t\t}\n\t};\n\n\n\t/**\n\t * A result-set class used when performing a radius based search.\n\t */\n\ttemplate <typename DistanceType, typename IndexType = size_t>\n\tclass RadiusResultSet\n\t{\n\tpublic:\n\t\tconst DistanceType radius;\n\n\t\tstd::vector<std::pair<IndexType,DistanceType> >& m_indices_dists;\n\n\t\tinline RadiusResultSet(DistanceType radius_, std::vector<std::pair<IndexType,DistanceType> >& indices_dists) : radius(radius_), m_indices_dists(indices_dists)\n\t\t{\n\t\t\tinit();\n\t\t}\n\n\t\tinline ~RadiusResultSet() { }\n\n\t\tinline void init() { clear(); }\n\t\tinline void clear() { m_indices_dists.clear(); }\n\n\t\tinline size_t size() const { return m_indices_dists.size(); }\n\n\t\tinline bool full() const { return true; }\n\n\t\tinline void addPoint(DistanceType dist, IndexType index)\n\t\t{\n\t\t\tif (dist<radius)\n\t\t\t\tm_indices_dists.push_back(std::make_pair(index,dist));\n\t\t}\n\n\t\tinline DistanceType worstDist() const { return radius; }\n\n\t\t/** Clears the result set and adjusts the search radius. */\n\t\tinline void set_radius_and_clear( const DistanceType r )\n\t\t{\n\t\t\tradius = r;\n\t\t\tclear();\n\t\t}\n\n\t\t/**\n\t\t * Find the worst result (furtherest neighbor) without copying or sorting\n\t\t * Pre-conditions: size() > 0\n\t\t */\n\t\tstd::pair<IndexType,DistanceType> worst_item() const\n\t\t{\n\t\t   if (m_indices_dists.empty()) throw std::runtime_error(\"Cannot invoke RadiusResultSet::worst_item() on an empty list of results.\");\n\t\t   typedef typename std::vector<std::pair<IndexType,DistanceType> >::const_iterator DistIt;\n\t\t   DistIt it = std::max_element(m_indices_dists.begin(), m_indices_dists.end());\n\t\t   return *it;\n\t\t}\n\t};\n\n\t/** operator \"<\" for std::sort() */\n\tstruct IndexDist_Sorter\n\t{\n\t\t/** PairType will be typically: std::pair<IndexType,DistanceType> */\n\t\ttemplate <typename PairType>\n\t\tinline bool operator()(const PairType &p1, const PairType &p2) const {\n\t\t\treturn p1.second < p2.second;\n\t\t}\n\t};\n\n\t/** @} */\n\n\n\t/** @addtogroup loadsave_grp Load/save auxiliary functions\n\t  * @{ */\n\ttemplate<typename T>\n\tvoid save_value(FILE* stream, const T& value, size_t count = 1)\n\t{\n\t\tfwrite(&value, sizeof(value),count, stream);\n\t}\n\n\ttemplate<typename T>\n\tvoid save_value(FILE* stream, const std::vector<T>& value)\n\t{\n\t\tsize_t size = value.size();\n\t\tfwrite(&size, sizeof(size_t), 1, stream);\n\t\tfwrite(&value[0], sizeof(T), size, stream);\n\t}\n\n\ttemplate<typename T>\n\tvoid load_value(FILE* stream, T& value, size_t count = 1)\n\t{\n\t\tsize_t read_cnt = fread(&value, sizeof(value), count, stream);\n\t\tif (read_cnt != count) {\n\t\t\tthrow std::runtime_error(\"Cannot read from file\");\n\t\t}\n\t}\n\n\n\ttemplate<typename T>\n\tvoid load_value(FILE* stream, std::vector<T>& value)\n\t{\n\t\tsize_t size;\n\t\tsize_t read_cnt = fread(&size, sizeof(size_t), 1, stream);\n\t\tif (read_cnt!=1) {\n\t\t\tthrow std::runtime_error(\"Cannot read from file\");\n\t\t}\n\t\tvalue.resize(size);\n\t\tread_cnt = fread(&value[0], sizeof(T), size, stream);\n\t\tif (read_cnt!=size) {\n\t\t\tthrow std::runtime_error(\"Cannot read from file\");\n\t\t}\n\t}\n\t/** @} */\n\n\n\t/** @addtogroup metric_grp Metric (distance) classes\n\t  * @{ */\n\n\ttemplate<typename T> inline T abs(T x) { return (x<0) ? -x : x; }\n\ttemplate<> inline int abs<int>(int x) { return ::abs(x); }\n\ttemplate<> inline float abs<float>(float x) { return fabsf(x); }\n\ttemplate<> inline double abs<double>(double x) { return fabs(x); }\n\ttemplate<> inline long double abs<long double>(long double x) { return fabsl(x); }\n\n\t/** Manhattan distance functor (generic version, optimized for high-dimensionality data sets).\n\t  *  Corresponding distance traits: nanoflann::metric_L1\n\t  * \\tparam T Type of the elements (e.g. double, float, uint8_t)\n\t  * \\tparam DistanceType Type of distance variables (must be signed) (e.g. float, double, int64_t)\n\t  */\n\ttemplate<class T, class DataSource, typename _DistanceType = T>\n\tstruct L1_Adaptor\n\t{\n\t\ttypedef T ElementType;\n\t\ttypedef _DistanceType DistanceType;\n\n\t\tconst DataSource &data_source;\n\n\t\tL1_Adaptor(const DataSource &_data_source) : data_source(_data_source) { }\n\n\t\tinline DistanceType operator()(const T* a, const size_t b_idx, size_t size, DistanceType worst_dist = -1) const\n\t\t{\n\t\t\tDistanceType result = DistanceType();\n\t\t\tconst T* last = a + size;\n\t\t\tconst T* lastgroup = last - 3;\n\t\t\tsize_t d = 0;\n\n\t\t\t/* Process 4 items with each loop for efficiency. */\n\t\t\twhile (a < lastgroup) {\n\t\t\t\tconst DistanceType diff0 = nanoflann::abs(a[0] - data_source.kdtree_get_pt(b_idx,d++));\n\t\t\t\tconst DistanceType diff1 = nanoflann::abs(a[1] - data_source.kdtree_get_pt(b_idx,d++));\n\t\t\t\tconst DistanceType diff2 = nanoflann::abs(a[2] - data_source.kdtree_get_pt(b_idx,d++));\n\t\t\t\tconst DistanceType diff3 = nanoflann::abs(a[3] - data_source.kdtree_get_pt(b_idx,d++));\n\t\t\t\tresult += diff0 + diff1 + diff2 + diff3;\n\t\t\t\ta += 4;\n\t\t\t\tif ((worst_dist>0)&&(result>worst_dist)) {\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\t\t\t}\n\t\t\t/* Process last 0-3 components.  Not needed for standard vector lengths. */\n\t\t\twhile (a < last) {\n\t\t\t\tresult += nanoflann::abs( *a++ - data_source.kdtree_get_pt(b_idx,d++) );\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\ttemplate <typename U, typename V>\n\t\tinline DistanceType accum_dist(const U a, const V b, int ) const\n\t\t{\n\t\t\treturn nanoflann::abs(a-b);\n\t\t}\n\t};\n\n\t/** Squared Euclidean distance functor (generic version, optimized for high-dimensionality data sets).\n\t  *  Corresponding distance traits: nanoflann::metric_L2\n\t  * \\tparam T Type of the elements (e.g. double, float, uint8_t)\n\t  * \\tparam DistanceType Type of distance variables (must be signed) (e.g. float, double, int64_t)\n\t  */\n\ttemplate<class T, class DataSource, typename _DistanceType = T>\n\tstruct L2_Adaptor\n\t{\n\t\ttypedef T ElementType;\n\t\ttypedef _DistanceType DistanceType;\n\n\t\tconst DataSource &data_source;\n\n\t\tL2_Adaptor(const DataSource &_data_source) : data_source(_data_source) { }\n\n\t\tinline DistanceType operator()(const T* a, const size_t b_idx, size_t size, DistanceType worst_dist = -1) const\n\t\t{\n\t\t\tDistanceType result = DistanceType();\n\t\t\tconst T* last = a + size;\n\t\t\tconst T* lastgroup = last - 3;\n\t\t\tsize_t d = 0;\n\n\t\t\t/* Process 4 items with each loop for efficiency. */\n\t\t\twhile (a < lastgroup) {\n\t\t\t\tconst DistanceType diff0 = a[0] - data_source.kdtree_get_pt(b_idx,d++);\n\t\t\t\tconst DistanceType diff1 = a[1] - data_source.kdtree_get_pt(b_idx,d++);\n\t\t\t\tconst DistanceType diff2 = a[2] - data_source.kdtree_get_pt(b_idx,d++);\n\t\t\t\tconst DistanceType diff3 = a[3] - data_source.kdtree_get_pt(b_idx,d++);\n\t\t\t\tresult += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3;\n\t\t\t\ta += 4;\n\t\t\t\tif ((worst_dist>0)&&(result>worst_dist)) {\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\t\t\t}\n\t\t\t/* Process last 0-3 components.  Not needed for standard vector lengths. */\n\t\t\twhile (a < last) {\n\t\t\t\tconst DistanceType diff0 = *a++ - data_source.kdtree_get_pt(b_idx,d++);\n\t\t\t\tresult += diff0 * diff0;\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\ttemplate <typename U, typename V>\n\t\tinline DistanceType accum_dist(const U a, const V b, int ) const\n\t\t{\n\t\t\treturn (a-b)*(a-b);\n\t\t}\n\t};\n\n\t/** Squared Euclidean distance functor (suitable for low-dimensionality datasets, like 2D or 3D point clouds)\n\t  *  Corresponding distance traits: nanoflann::metric_L2_Simple\n\t  * \\tparam T Type of the elements (e.g. double, float, uint8_t)\n\t  * \\tparam DistanceType Type of distance variables (must be signed) (e.g. float, double, int64_t)\n\t  */\n\ttemplate<class T, class DataSource, typename _DistanceType = T>\n\tstruct L2_Simple_Adaptor\n\t{\n\t\ttypedef T ElementType;\n\t\ttypedef _DistanceType DistanceType;\n\n\t\tconst DataSource &data_source;\n\n\t\tL2_Simple_Adaptor(const DataSource &_data_source) : data_source(_data_source) { }\n\n\t\tinline DistanceType operator()(const T* a, const size_t b_idx, size_t size) const {\n\t\t\treturn data_source.kdtree_distance(a,b_idx,size);\n\t\t}\n\n\t\ttemplate <typename U, typename V>\n\t\tinline DistanceType accum_dist(const U a, const V b, int ) const\n\t\t{\n\t\t\treturn (a-b)*(a-b);\n\t\t}\n\t};\n\n\t/** Metaprogramming helper traits class for the L1 (Manhattan) metric */\n\tstruct metric_L1 {\n\t\ttemplate<class T, class DataSource>\n\t\tstruct traits {\n\t\t\ttypedef L1_Adaptor<T,DataSource> distance_t;\n\t\t};\n\t};\n\t/** Metaprogramming helper traits class for the L2 (Euclidean) metric */\n\tstruct metric_L2 {\n\t\ttemplate<class T, class DataSource>\n\t\tstruct traits {\n\t\t\ttypedef L2_Adaptor<T,DataSource> distance_t;\n\t\t};\n\t};\n\t/** Metaprogramming helper traits class for the L2_simple (Euclidean) metric */\n\tstruct metric_L2_Simple {\n\t\ttemplate<class T, class DataSource>\n\t\tstruct traits {\n\t\t\ttypedef L2_Simple_Adaptor<T,DataSource> distance_t;\n\t\t};\n\t};\n\n\t/** @} */\n\n\n\n\t/** @addtogroup param_grp Parameter structs\n\t  * @{ */\n\n\t/**  Parameters (see http://code.google.com/p/nanoflann/ for help choosing the parameters)\n\t  */\n\tstruct KDTreeSingleIndexAdaptorParams\n\t{\n\t\tKDTreeSingleIndexAdaptorParams(size_t _leaf_max_size = 10, int dim_ = -1) :\n\t\t\tleaf_max_size(_leaf_max_size), dim(dim_)\n\t\t{}\n\n\t\tsize_t leaf_max_size;\n\t\tint dim;\n\t};\n\n\t/** Search options for KDTreeSingleIndexAdaptor::findNeighbors() */\n\tstruct SearchParams\n\t{\n\t\t/** Note: The first argument (checks_IGNORED_) is ignored, but kept for compatibility with the FLANN interface */\n\t\tSearchParams(int checks_IGNORED_ = 32, float eps_ = 0, bool sorted_ = true ) :\n\t\t\tchecks(checks_IGNORED_), eps(eps_), sorted(sorted_) {}\n\n\t\tint   checks;  //!< Ignored parameter (Kept for compatibility with the FLANN interface).\n\t\tfloat eps;  //!< search for eps-approximate neighbours (default: 0)\n\t\tbool sorted; //!< only for radius search, require neighbours sorted by distance (default: true)\n\t};\n\t/** @} */\n\n\n\t/** @addtogroup memalloc_grp Memory allocation\n\t  * @{ */\n\n\t/**\n\t * Allocates (using C's malloc) a generic type T.\n\t *\n\t * Params:\n\t *     count = number of instances to allocate.\n\t * Returns: pointer (of type T*) to memory buffer\n\t */\n\ttemplate <typename T>\n\tinline T* allocate(size_t count = 1)\n\t{\n\t\tT* mem = (T*) ::malloc(sizeof(T)*count);\n\t\treturn mem;\n\t}\n\n\n\t/**\n\t * Pooled storage allocator\n\t *\n\t * The following routines allow for the efficient allocation of storage in\n\t * small chunks from a specified pool.  Rather than allowing each structure\n\t * to be freed individually, an entire pool of storage is freed at once.\n\t * This method has two advantages over just using malloc() and free().  First,\n\t * it is far more efficient for allocating small objects, as there is\n\t * no overhead for remembering all the information needed to free each\n\t * object or consolidating fragmented memory.  Second, the decision about\n\t * how long to keep an object is made at the time of allocation, and there\n\t * is no need to track down all the objects to free them.\n\t *\n\t */\n\n\tconst size_t     WORDSIZE=16;\n\tconst size_t     BLOCKSIZE=8192;\n\n\tclass PooledAllocator\n\t{\n\t\t/* We maintain memory alignment to word boundaries by requiring that all\n\t\t    allocations be in multiples of the machine wordsize.  */\n\t\t/* Size of machine word in bytes.  Must be power of 2. */\n\t\t/* Minimum number of bytes requested at a time from\tthe system.  Must be multiple of WORDSIZE. */\n\n\n\t\tsize_t  remaining;  /* Number of bytes left in current block of storage. */\n\t\tvoid*   base;     /* Pointer to base of current block of storage. */\n\t\tvoid*   loc;      /* Current location in block to next allocate memory. */\n\t\tsize_t  blocksize;\n\n\t\tvoid internal_init()\n\t\t{\n\t\t\tremaining = 0;\n\t\t\tbase = NULL;\n\t\t\tusedMemory = 0;\n\t\t\twastedMemory = 0;\n\t\t}\n\n\tpublic:\n\t\tsize_t  usedMemory;\n\t\tsize_t  wastedMemory;\n\n\t\t/**\n\t\t    Default constructor. Initializes a new pool.\n\t\t */\n\t\tPooledAllocator(const size_t blocksize_ = BLOCKSIZE) : blocksize(blocksize_) {\n\t\t\tinternal_init();\n\t\t}\n\n\t\t/**\n\t\t * Destructor. Frees all the memory allocated in this pool.\n\t\t */\n\t\t~PooledAllocator() {\n\t\t\tfree_all();\n\t\t}\n\n\t\t/** Frees all allocated memory chunks */\n\t\tvoid free_all()\n\t\t{\n\t\t\twhile (base != NULL) {\n\t\t\t\tvoid *prev = *((void**) base); /* Get pointer to prev block. */\n\t\t\t\t::free(base);\n\t\t\t\tbase = prev;\n\t\t\t}\n\t\t\tinternal_init();\n\t\t}\n\n\t\t/**\n\t\t * Returns a pointer to a piece of new memory of the given size in bytes\n\t\t * allocated from the pool.\n\t\t */\n\t\tvoid* malloc(const size_t req_size)\n\t\t{\n\t\t\t/* Round size up to a multiple of wordsize.  The following expression\n\t\t\t    only works for WORDSIZE that is a power of 2, by masking last bits of\n\t\t\t    incremented size to zero.\n\t\t\t */\n\t\t\tconst size_t size = (req_size + (WORDSIZE - 1)) & ~(WORDSIZE - 1);\n\n\t\t\t/* Check whether a new block must be allocated.  Note that the first word\n\t\t\t    of a block is reserved for a pointer to the previous block.\n\t\t\t */\n\t\t\tif (size > remaining) {\n\n\t\t\t\twastedMemory += remaining;\n\n\t\t\t\t/* Allocate new storage. */\n\t\t\t\tconst size_t blocksize = (size + sizeof(void*) + (WORDSIZE-1) > BLOCKSIZE) ?\n\t\t\t\t\t\t\tsize + sizeof(void*) + (WORDSIZE-1) : BLOCKSIZE;\n\n\t\t\t\t// use the standard C malloc to allocate memory\n\t\t\t\tvoid* m = ::malloc(blocksize);\n\t\t\t\tif (!m) {\n\t\t\t\t\tfprintf(stderr,\"Failed to allocate memory.\\n\");\n\t\t\t\t\treturn NULL;\n\t\t\t\t}\n\n\t\t\t\t/* Fill first word of new block with pointer to previous block. */\n\t\t\t\t((void**) m)[0] = base;\n\t\t\t\tbase = m;\n\n\t\t\t\tsize_t shift = 0;\n\t\t\t\t//int size_t = (WORDSIZE - ( (((size_t)m) + sizeof(void*)) & (WORDSIZE-1))) & (WORDSIZE-1);\n\n\t\t\t\tremaining = blocksize - sizeof(void*) - shift;\n\t\t\t\tloc = ((char*)m + sizeof(void*) + shift);\n\t\t\t}\n\t\t\tvoid* rloc = loc;\n\t\t\tloc = (char*)loc + size;\n\t\t\tremaining -= size;\n\n\t\t\tusedMemory += size;\n\n\t\t\treturn rloc;\n\t\t}\n\n\t\t/**\n\t\t * Allocates (using this pool) a generic type T.\n\t\t *\n\t\t * Params:\n\t\t *     count = number of instances to allocate.\n\t\t * Returns: pointer (of type T*) to memory buffer\n\t\t */\n\t\ttemplate <typename T>\n\t\tT* allocate(const size_t count = 1)\n\t\t{\n\t\t\tT* mem = (T*) this->malloc(sizeof(T)*count);\n\t\t\treturn mem;\n\t\t}\n\n\t};\n\t/** @} */\n\n\t/** @addtogroup nanoflann_metaprog_grp Auxiliary metaprogramming stuff\n\t  * @{ */\n\n\t// ----------------  CArray -------------------------\n\t/** A STL container (as wrapper) for arrays of constant size defined at compile time (class imported from the MRPT project)\n\t * This code is an adapted version from Boost, modifed for its integration\n\t *\twithin MRPT (JLBC, Dec/2009) (Renamed array -> CArray to avoid possible potential conflicts).\n\t * See\n\t *      http://www.josuttis.com/cppcode\n\t * for details and the latest version.\n\t * See\n\t *      http://www.boost.org/libs/array for Documentation.\n\t * for documentation.\n\t *\n\t * (C) Copyright Nicolai M. Josuttis 2001.\n\t * Permission to copy, use, modify, sell and distribute this software\n\t * is granted provided this copyright notice appears in all copies.\n\t * This software is provided \"as is\" without express or implied\n\t * warranty, and with no claim as to its suitability for any purpose.\n\t *\n\t * 29 Jan 2004 - minor fixes (Nico Josuttis)\n\t * 04 Dec 2003 - update to synch with library TR1 (Alisdair Meredith)\n\t * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.\n\t * 05 Aug 2001 - minor update (Nico Josuttis)\n\t * 20 Jan 2001 - STLport fix (Beman Dawes)\n\t * 29 Sep 2000 - Initial Revision (Nico Josuttis)\n\t *\n\t * Jan 30, 2004\n\t */\n    template <typename T, std::size_t N>\n    class CArray {\n      public:\n        T elems[N];    // fixed-size array of elements of type T\n\n      public:\n        // type definitions\n        typedef T              value_type;\n        typedef T*             iterator;\n        typedef const T*       const_iterator;\n        typedef T&             reference;\n        typedef const T&       const_reference;\n        typedef std::size_t    size_type;\n        typedef std::ptrdiff_t difference_type;\n\n        // iterator support\n        inline iterator begin() { return elems; }\n        inline const_iterator begin() const { return elems; }\n        inline iterator end() { return elems+N; }\n        inline const_iterator end() const { return elems+N; }\n\n        // reverse iterator support\n#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)\n        typedef std::reverse_iterator<iterator> reverse_iterator;\n        typedef std::reverse_iterator<const_iterator> const_reverse_iterator;\n#elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)\n        // workaround for broken reverse_iterator in VC7\n        typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,\n                                      reference, iterator, reference> > reverse_iterator;\n        typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,\n                                      const_reference, iterator, reference> > const_reverse_iterator;\n#else\n        // workaround for broken reverse_iterator implementations\n        typedef std::reverse_iterator<iterator,T> reverse_iterator;\n        typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;\n#endif\n\n        reverse_iterator rbegin() { return reverse_iterator(end()); }\n        const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }\n        reverse_iterator rend() { return reverse_iterator(begin()); }\n        const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }\n        // operator[]\n        inline reference operator[](size_type i) { return elems[i]; }\n        inline const_reference operator[](size_type i) const { return elems[i]; }\n        // at() with range check\n        reference at(size_type i) { rangecheck(i); return elems[i]; }\n        const_reference at(size_type i) const { rangecheck(i); return elems[i]; }\n        // front() and back()\n        reference front() { return elems[0]; }\n        const_reference front() const { return elems[0]; }\n        reference back() { return elems[N-1]; }\n        const_reference back() const { return elems[N-1]; }\n        // size is constant\n        static inline size_type size() { return N; }\n        static bool empty() { return false; }\n        static size_type max_size() { return N; }\n        enum { static_size = N };\n\t\t/** This method has no effects in this class, but raises an exception if the expected size does not match */\n\t\tinline void resize(const size_t nElements) { if (nElements!=N) throw std::logic_error(\"Try to change the size of a CArray.\"); }\n        // swap (note: linear complexity in N, constant for given instantiation)\n        void swap (CArray<T,N>& y) { std::swap_ranges(begin(),end(),y.begin()); }\n        // direct access to data (read-only)\n        const T* data() const { return elems; }\n        // use array as C array (direct read/write access to data)\n        T* data() { return elems; }\n        // assignment with type conversion\n        template <typename T2> CArray<T,N>& operator= (const CArray<T2,N>& rhs) {\n            std::copy(rhs.begin(),rhs.end(), begin());\n            return *this;\n        }\n        // assign one value to all elements\n        inline void assign (const T& value) { for (size_t i=0;i<N;i++) elems[i]=value; }\n        // assign (compatible with std::vector's one) (by JLBC for MRPT)\n        void assign (const size_t n, const T& value) { assert(N==n); for (size_t i=0;i<N;i++) elems[i]=value; }\n      private:\n        // check range (may be private because it is static)\n        static void rangecheck (size_type i) { if (i >= size()) { throw std::out_of_range(\"CArray<>: index out of range\"); } }\n    }; // end of CArray\n\n\t/** Used to declare fixed-size arrays when DIM>0, dynamically-allocated vectors when DIM=-1.\n\t  * Fixed size version for a generic DIM:\n\t  */\n\ttemplate <int DIM, typename T>\n\tstruct array_or_vector_selector\n\t{\n\t\ttypedef CArray<T,DIM> container_t;\n\t};\n\t/** Dynamic size version */\n\ttemplate <typename T>\n\tstruct array_or_vector_selector<-1,T> {\n\t\ttypedef std::vector<T> container_t;\n\t};\n\t/** @} */\n\n\t/** @addtogroup kdtrees_grp KD-tree classes and adaptors\n\t  * @{ */\n\n\t/** kd-tree index\n\t *\n\t * Contains the k-d trees and other information for indexing a set of points\n\t * for nearest-neighbor matching.\n\t *\n\t *  The class \"DatasetAdaptor\" must provide the following interface (can be non-virtual, inlined methods):\n\t *\n\t *  \\code\n\t *   // Must return the number of data points\n\t *   inline size_t kdtree_get_point_count() const { ... }\n\t *\n\t *   // Must return the Euclidean (L2) distance between the vector \"p1[0:size-1]\" and the data point with index \"idx_p2\" stored in the class:\n\t *   inline DistanceType kdtree_distance(const T *p1, const size_t idx_p2,size_t size) const { ... }\n\t *\n\t *   // Must return the dim'th component of the idx'th point in the class:\n\t *   inline T kdtree_get_pt(const size_t idx, int dim) const { ... }\n\t *\n\t *   // Optional bounding-box computation: return false to default to a standard bbox computation loop.\n\t *   //   Return true if the BBOX was already computed by the class and returned in \"bb\" so it can be avoided to redo it again.\n\t *   //   Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds)\n\t *   template <class BBOX>\n\t *   bool kdtree_get_bbox(BBOX &bb) const\n\t *   {\n\t *      bb[0].low = ...; bb[0].high = ...;  // 0th dimension limits\n\t *      bb[1].low = ...; bb[1].high = ...;  // 1st dimension limits\n\t *      ...\n\t *      return true;\n\t *   }\n\t *\n\t *  \\endcode\n\t *\n\t * \\tparam IndexType Will be typically size_t or int\n\t */\n\ttemplate <typename Distance, class DatasetAdaptor,int DIM = -1, typename IndexType = size_t>\n\tclass KDTreeSingleIndexAdaptor\n\t{\n\tpublic:\n\t\ttypedef typename Distance::ElementType  ElementType;\n\t\ttypedef typename Distance::DistanceType DistanceType;\n\tprotected:\n\n\t\t/**\n\t\t *  Array of indices to vectors in the dataset.\n\t\t */\n\t\tstd::vector<IndexType> vind;\n\n\t\tsize_t m_leaf_max_size;\n\n\n\t\t/**\n\t\t * The dataset used by this index\n\t\t */\n\t\tconst DatasetAdaptor &dataset; //!< The source of our data\n\n\t\tconst KDTreeSingleIndexAdaptorParams index_params;\n\n\t\tsize_t m_size;\n\t\tint dim;  //!< Dimensionality of each data point\n\n\n\t\t/*--------------------- Internal Data Structures --------------------------*/\n\t\tstruct Node\n\t\t{\n\t\t\tunion {\n\t\t\t\tstruct\n\t\t\t\t{\n\t\t\t\t\t/**\n\t\t\t\t\t * Indices of points in leaf node\n\t\t\t\t\t */\n\t\t\t\t\tIndexType left, right;\n\t\t\t\t} lr;\n\t\t\t\tstruct\n\t\t\t\t{\n\t\t\t\t\t/**\n\t\t\t\t\t * Dimension used for subdivision.\n\t\t\t\t\t */\n\t\t\t\t\tint divfeat;\n\t\t\t\t\t/**\n\t\t\t\t\t * The values used for subdivision.\n\t\t\t\t\t */\n\t\t\t\t\tDistanceType divlow, divhigh;\n\t\t\t\t} sub;\n\t\t\t};\n\t\t\t/**\n\t\t\t * The child nodes.\n\t\t\t */\n\t\t\tNode* child1, * child2;\n\t\t};\n\t\ttypedef Node* NodePtr;\n\n\n\t\tstruct Interval\n\t\t{\n\t\t\tElementType low, high;\n\t\t};\n\n\t\t/** Define \"BoundingBox\" as a fixed-size or variable-size container depending on \"DIM\" */\n\t\ttypedef typename array_or_vector_selector<DIM,Interval>::container_t BoundingBox;\n\n\t\t/** Define \"distance_vector_t\" as a fixed-size or variable-size container depending on \"DIM\" */\n\t\ttypedef typename array_or_vector_selector<DIM,DistanceType>::container_t distance_vector_t;\n\n\t\t/** This record represents a branch point when finding neighbors in\n\t\t\tthe tree.  It contains a record of the minimum distance to the query\n\t\t\tpoint, as well as the node at which the search resumes.\n\t\t */\n\t\ttemplate <typename T, typename DistanceType>\n\t\tstruct BranchStruct\n\t\t{\n\t\t\tT node;           /* Tree node at which search resumes */\n\t\t\tDistanceType mindist;     /* Minimum distance to query for all nodes below. */\n\n\t\t\tBranchStruct() {}\n\t\t\tBranchStruct(const T& aNode, DistanceType dist) : node(aNode), mindist(dist) {}\n\n\t\t\tinline bool operator<(const BranchStruct<T, DistanceType>& rhs) const\n\t\t\t{\n\t\t\t\treturn mindist<rhs.mindist;\n\t\t\t}\n\t\t};\n\n\t\t/**\n\t\t * Array of k-d trees used to find neighbours.\n\t\t */\n\t\tNodePtr root_node;\n\t\ttypedef BranchStruct<NodePtr, DistanceType> BranchSt;\n\t\ttypedef BranchSt* Branch;\n\n\t\tBoundingBox root_bbox;\n\n\t\t/**\n\t\t * Pooled memory allocator.\n\t\t *\n\t\t * Using a pooled memory allocator is more efficient\n\t\t * than allocating memory directly when there is a large\n\t\t * number small of memory allocations.\n\t\t */\n\t\tPooledAllocator pool;\n\n\tpublic:\n\n\t\tDistance distance;\n\n\t\t/**\n\t\t * KDTree constructor\n\t\t *\n\t\t * Params:\n\t\t *          inputData = dataset with the input features\n\t\t *          params = parameters passed to the kdtree algorithm (see http://code.google.com/p/nanoflann/ for help choosing the parameters)\n\t\t */\n\t\tKDTreeSingleIndexAdaptor(const int dimensionality, const DatasetAdaptor& inputData, const KDTreeSingleIndexAdaptorParams& params = KDTreeSingleIndexAdaptorParams() ) :\n\t\t\tdataset(inputData), index_params(params), root_node(NULL), distance(inputData)\n\t\t{\n\t\t\tm_size = dataset.kdtree_get_point_count();\n\t\t\tdim = dimensionality;\n\t\t\tif (DIM>0) dim=DIM;\n\t\t\telse {\n\t\t\t\tif (params.dim>0) dim = params.dim;\n\t\t\t}\n\t\t\tm_leaf_max_size = params.leaf_max_size;\n\n\t\t\t// Create a permutable array of indices to the input vectors.\n\t\t\tinit_vind();\n\t\t}\n\n\t\t/**\n\t\t * Standard destructor\n\t\t */\n\t\t~KDTreeSingleIndexAdaptor()\n\t\t{\n\t\t}\n\n\t\t/** Frees the previously-built index. Automatically called within buildIndex(). */\n\t\tvoid freeIndex()\n\t\t{\n\t\t\tpool.free_all();\n\t\t\troot_node=NULL;\n\t\t}\n\n\t\t/**\n\t\t * Builds the index\n\t\t */\n\t\tvoid buildIndex()\n\t\t{\n\t\t\tinit_vind();\n\t\t\tcomputeBoundingBox(root_bbox);\n\t\t\tfreeIndex();\n\t\t\troot_node = divideTree(0, m_size, root_bbox );   // construct the tree\n\t\t}\n\n\t\t/**\n\t\t *  Returns size of index.\n\t\t */\n\t\tsize_t size() const\n\t\t{\n\t\t\treturn m_size;\n\t\t}\n\n\t\t/**\n\t\t * Returns the length of an index feature.\n\t\t */\n\t\tsize_t veclen() const\n\t\t{\n\t\t\treturn static_cast<size_t>(DIM>0 ? DIM : dim);\n\t\t}\n\n\t\t/**\n\t\t * Computes the inde memory usage\n\t\t * Returns: memory used by the index\n\t\t */\n\t\tsize_t usedMemory() const\n\t\t{\n\t\t\treturn pool.usedMemory+pool.wastedMemory+dataset.kdtree_get_point_count()*sizeof(IndexType);  // pool memory and vind array memory\n\t\t}\n\n\t\t/** \\name Query methods\n\t\t  * @{ */\n\n\t\t/**\n\t\t * Find set of nearest neighbors to vec[0:dim-1]. Their indices are stored inside\n\t\t * the result object.\n\t\t *\n\t\t * Params:\n\t\t *     result = the result object in which the indices of the nearest-neighbors are stored\n\t\t *     vec = the vector for which to search the nearest neighbors\n\t\t *\n\t\t * \\tparam RESULTSET Should be any ResultSet<DistanceType>\n\t\t * \\sa knnSearch, radiusSearch\n\t\t */\n\t\ttemplate <typename RESULTSET>\n\t\tvoid findNeighbors(RESULTSET& result, const ElementType* vec, const SearchParams& searchParams) const\n\t\t{\n\t\t\tassert(vec);\n\t\t\tif (!root_node) throw std::runtime_error(\"[nanoflann] findNeighbors() called before building the index.\");\n\t\t\tfloat epsError = 1+searchParams.eps;\n\n\t\t\tdistance_vector_t dists; // fixed or variable-sized container (depending on DIM)\n\t\t\tdists.assign((DIM>0 ? DIM : dim) ,0); // Fill it with zeros.\n\t\t\tDistanceType distsq = computeInitialDistances(vec, dists);\n\t\t\tsearchLevel(result, vec, root_node, distsq, dists, epsError);  // \"count_leaf\" parameter removed since was neither used nor returned to the user.\n\t\t}\n\n\t\t/**\n\t\t * Find the \"num_closest\" nearest neighbors to the \\a query_point[0:dim-1]. Their indices are stored inside\n\t\t * the result object.\n\t\t *  \\sa radiusSearch, findNeighbors\n\t\t * \\note nChecks_IGNORED is ignored but kept for compatibility with the original FLANN interface.\n\t\t */\n\t\tinline void knnSearch(const ElementType *query_point, const size_t num_closest, IndexType *out_indices, DistanceType *out_distances_sq, const int nChecks_IGNORED = 10) const\n\t\t{\n\t\t\tnanoflann::KNNResultSet<DistanceType,IndexType> resultSet(num_closest);\n\t\t\tresultSet.init(out_indices, out_distances_sq);\n\t\t\tthis->findNeighbors(resultSet, query_point, nanoflann::SearchParams());\n\t\t}\n\n\t\t/**\n\t\t * Find all the neighbors to \\a query_point[0:dim-1] within a maximum radius.\n\t\t *  The output is given as a vector of pairs, of which the first element is a point index and the second the corresponding distance.\n\t\t *  Previous contents of \\a IndicesDists are cleared.\n\t\t *\n\t\t *  If searchParams.sorted==true, the output list is sorted by ascending distances.\n\t\t *\n\t\t *  For a better performance, it is advisable to do a .reserve() on the vector if you have any wild guess about the number of expected matches.\n\t\t *\n\t\t *  \\sa knnSearch, findNeighbors\n\t\t * \\return The number of points within the given radius (i.e. indices.size() or dists.size() )\n\t\t */\n\t\tsize_t radiusSearch(const ElementType *query_point,const DistanceType radius, std::vector<std::pair<IndexType,DistanceType> >& IndicesDists, const SearchParams& searchParams) const\n\t\t{\n\t\t\tRadiusResultSet<DistanceType,IndexType> resultSet(radius,IndicesDists);\n\t\t\tthis->findNeighbors(resultSet, query_point, searchParams);\n\n\t\t\tif (searchParams.sorted)\n\t\t\t\tstd::sort(IndicesDists.begin(),IndicesDists.end(), IndexDist_Sorter() );\n\n\t\t\treturn resultSet.size();\n\t\t}\n\n\t\t/** @} */\n\n\tprivate:\n\t\t/** Make sure the auxiliary list \\a vind has the same size than the current dataset, and re-generate if size has changed. */\n\t\tvoid init_vind()\n\t\t{\n\t\t\t// Create a permutable array of indices to the input vectors.\n\t\t\tm_size = dataset.kdtree_get_point_count();\n\t\t\tif (vind.size()!=m_size) vind.resize(m_size);\n\t\t\tfor (size_t i = 0; i < m_size; i++) vind[i] = i;\n\t\t}\n\n\t\t/// Helper accessor to the dataset points:\n\t\tinline ElementType dataset_get(size_t idx, int component) const {\n\t\t\treturn dataset.kdtree_get_pt(idx,component);\n\t\t}\n\n\n\t\tvoid save_tree(FILE* stream, NodePtr tree)\n\t\t{\n\t\t\tsave_value(stream, *tree);\n\t\t\tif (tree->child1!=NULL) {\n\t\t\t\tsave_tree(stream, tree->child1);\n\t\t\t}\n\t\t\tif (tree->child2!=NULL) {\n\t\t\t\tsave_tree(stream, tree->child2);\n\t\t\t}\n\t\t}\n\n\n\t\tvoid load_tree(FILE* stream, NodePtr& tree)\n\t\t{\n\t\t\ttree = pool.allocate<Node>();\n\t\t\tload_value(stream, *tree);\n\t\t\tif (tree->child1!=NULL) {\n\t\t\t\tload_tree(stream, tree->child1);\n\t\t\t}\n\t\t\tif (tree->child2!=NULL) {\n\t\t\t\tload_tree(stream, tree->child2);\n\t\t\t}\n\t\t}\n\n\n\t\tvoid computeBoundingBox(BoundingBox& bbox)\n\t\t{\n\t\t\tbbox.resize((DIM>0 ? DIM : dim));\n\t\t\tif (dataset.kdtree_get_bbox(bbox))\n\t\t\t{\n\t\t\t\t// Done! It was implemented in derived class\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tfor (int i=0; i<(DIM>0 ? DIM : dim); ++i) {\n\t\t\t\t\tbbox[i].low =\n\t\t\t\t\t\tbbox[i].high = dataset_get(0,i);\n\t\t\t\t}\n\t\t\t\tconst size_t N = dataset.kdtree_get_point_count();\n\t\t\t\tfor (size_t k=1; k<N; ++k) {\n\t\t\t\t\tfor (int i=0; i<(DIM>0 ? DIM : dim); ++i) {\n\t\t\t\t\t\tif (dataset_get(k,i)<bbox[i].low) bbox[i].low = dataset_get(k,i);\n\t\t\t\t\t\tif (dataset_get(k,i)>bbox[i].high) bbox[i].high = dataset_get(k,i);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\n\t\t/**\n\t\t * Create a tree node that subdivides the list of vecs from vind[first]\n\t\t * to vind[last].  The routine is called recursively on each sublist.\n\t\t * Place a pointer to this new tree node in the location pTree.\n\t\t *\n\t\t * Params: pTree = the new node to create\n\t\t *                  first = index of the first vector\n\t\t *                  last = index of the last vector\n\t\t */\n\t\tNodePtr divideTree(const IndexType left, const IndexType right, BoundingBox& bbox)\n\t\t{\n\t\t\tNodePtr node = pool.allocate<Node>(); // allocate memory\n\n\t\t\t/* If too few exemplars remain, then make this a leaf node. */\n\t\t\tif ( (right-left) <= m_leaf_max_size) {\n\t\t\t\tnode->child1 = node->child2 = NULL;    /* Mark as leaf node. */\n\t\t\t\tnode->lr.left = left;\n\t\t\t\tnode->lr.right = right;\n\n\t\t\t\t// compute bounding-box of leaf points\n\t\t\t\tfor (int i=0; i<(DIM>0 ? DIM : dim); ++i) {\n\t\t\t\t\tbbox[i].low = dataset_get(vind[left],i);\n\t\t\t\t\tbbox[i].high = dataset_get(vind[left],i);\n\t\t\t\t}\n\t\t\t\tfor (IndexType k=left+1; k<right; ++k) {\n\t\t\t\t\tfor (int i=0; i<(DIM>0 ? DIM : dim); ++i) {\n\t\t\t\t\t\tif (bbox[i].low>dataset_get(vind[k],i)) bbox[i].low=dataset_get(vind[k],i);\n\t\t\t\t\t\tif (bbox[i].high<dataset_get(vind[k],i)) bbox[i].high=dataset_get(vind[k],i);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tIndexType idx;\n\t\t\t\tint cutfeat;\n\t\t\t\tDistanceType cutval;\n\t\t\t\tmiddleSplit_(&vind[0]+left, right-left, idx, cutfeat, cutval, bbox);\n\n\t\t\t\tnode->sub.divfeat = cutfeat;\n\n\t\t\t\tBoundingBox left_bbox(bbox);\n\t\t\t\tleft_bbox[cutfeat].high = cutval;\n\t\t\t\tnode->child1 = divideTree(left, left+idx, left_bbox);\n\n\t\t\t\tBoundingBox right_bbox(bbox);\n\t\t\t\tright_bbox[cutfeat].low = cutval;\n\t\t\t\tnode->child2 = divideTree(left+idx, right, right_bbox);\n\n\t\t\t\tnode->sub.divlow = left_bbox[cutfeat].high;\n\t\t\t\tnode->sub.divhigh = right_bbox[cutfeat].low;\n\n\t\t\t\tfor (int i=0; i<(DIM>0 ? DIM : dim); ++i) {\n\t\t\t\t\tbbox[i].low = std::min(left_bbox[i].low, right_bbox[i].low);\n\t\t\t\t\tbbox[i].high = std::max(left_bbox[i].high, right_bbox[i].high);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn node;\n\t\t}\n\n\t\tvoid computeMinMax(IndexType* ind, IndexType count, int element, ElementType& min_elem, ElementType& max_elem)\n\t\t{\n\t\t\tmin_elem = dataset_get(ind[0],element);\n\t\t\tmax_elem = dataset_get(ind[0],element);\n\t\t\tfor (IndexType i=1; i<count; ++i) {\n\t\t\t\tElementType val = dataset_get(ind[i],element);\n\t\t\t\tif (val<min_elem) min_elem = val;\n\t\t\t\tif (val>max_elem) max_elem = val;\n\t\t\t}\n\t\t}\n\n\t\tvoid middleSplit(IndexType* ind, IndexType count, IndexType& index, int& cutfeat, DistanceType& cutval, const BoundingBox& bbox)\n\t\t{\n\t\t\t// find the largest span from the approximate bounding box\n\t\t\tElementType max_span = bbox[0].high-bbox[0].low;\n\t\t\tcutfeat = 0;\n\t\t\tcutval = (bbox[0].high+bbox[0].low)/2;\n\t\t\tfor (int i=1; i<(DIM>0 ? DIM : dim); ++i) {\n\t\t\t\tElementType span = bbox[i].low-bbox[i].low;\n\t\t\t\tif (span>max_span) {\n\t\t\t\t\tmax_span = span;\n\t\t\t\t\tcutfeat = i;\n\t\t\t\t\tcutval = (bbox[i].high+bbox[i].low)/2;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// compute exact span on the found dimension\n\t\t\tElementType min_elem, max_elem;\n\t\t\tcomputeMinMax(ind, count, cutfeat, min_elem, max_elem);\n\t\t\tcutval = (min_elem+max_elem)/2;\n\t\t\tmax_span = max_elem - min_elem;\n\n\t\t\t// check if a dimension of a largest span exists\n\t\t\tsize_t k = cutfeat;\n\t\t\tfor (size_t i=0; i<(DIM>0 ? DIM : dim); ++i) {\n\t\t\t\tif (i==k) continue;\n\t\t\t\tElementType span = bbox[i].high-bbox[i].low;\n\t\t\t\tif (span>max_span) {\n\t\t\t\t\tcomputeMinMax(ind, count, i, min_elem, max_elem);\n\t\t\t\t\tspan = max_elem - min_elem;\n\t\t\t\t\tif (span>max_span) {\n\t\t\t\t\t\tmax_span = span;\n\t\t\t\t\t\tcutfeat = i;\n\t\t\t\t\t\tcutval = (min_elem+max_elem)/2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tIndexType lim1, lim2;\n\t\t\tplaneSplit(ind, count, cutfeat, cutval, lim1, lim2);\n\n\t\t\tif (lim1>count/2) index = lim1;\n\t\t\telse if (lim2<count/2) index = lim2;\n\t\t\telse index = count/2;\n\t\t}\n\n\n\t\tvoid middleSplit_(IndexType* ind, IndexType count, IndexType& index, int& cutfeat, DistanceType& cutval, const BoundingBox& bbox)\n\t\t{\n\t\t\tconst DistanceType EPS=static_cast<DistanceType>(0.00001);\n\t\t\tElementType max_span = bbox[0].high-bbox[0].low;\n\t\t\tfor (int i=1; i<(DIM>0 ? DIM : dim); ++i) {\n\t\t\t\tElementType span = bbox[i].high-bbox[i].low;\n\t\t\t\tif (span>max_span) {\n\t\t\t\t\tmax_span = span;\n\t\t\t\t}\n\t\t\t}\n\t\t\tElementType max_spread = -1;\n\t\t\tcutfeat = 0;\n\t\t\tfor (int i=0; i<(DIM>0 ? DIM : dim); ++i) {\n\t\t\t\tElementType span = bbox[i].high-bbox[i].low;\n\t\t\t\tif (span>(1-EPS)*max_span) {\n\t\t\t\t\tElementType min_elem, max_elem;\n\t\t\t\t\tcomputeMinMax(ind, count, cutfeat, min_elem, max_elem);\n\t\t\t\t\tElementType spread = max_elem-min_elem;;\n\t\t\t\t\tif (spread>max_spread) {\n\t\t\t\t\t\tcutfeat = i;\n\t\t\t\t\t\tmax_spread = spread;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\t// split in the middle\n\t\t\tDistanceType split_val = (bbox[cutfeat].low+bbox[cutfeat].high)/2;\n\t\t\tElementType min_elem, max_elem;\n\t\t\tcomputeMinMax(ind, count, cutfeat, min_elem, max_elem);\n\n\t\t\tif (split_val<min_elem) cutval = min_elem;\n\t\t\telse if (split_val>max_elem) cutval = max_elem;\n\t\t\telse cutval = split_val;\n\n\t\t\tIndexType lim1, lim2;\n\t\t\tplaneSplit(ind, count, cutfeat, cutval, lim1, lim2);\n\n\t\t\tif (lim1>count/2) index = lim1;\n\t\t\telse if (lim2<count/2) index = lim2;\n\t\t\telse index = count/2;\n\t\t}\n\n\n\t\t/**\n\t\t *  Subdivide the list of points by a plane perpendicular on axe corresponding\n\t\t *  to the 'cutfeat' dimension at 'cutval' position.\n\t\t *\n\t\t *  On return:\n\t\t *  dataset[ind[0..lim1-1]][cutfeat]<cutval\n\t\t *  dataset[ind[lim1..lim2-1]][cutfeat]==cutval\n\t\t *  dataset[ind[lim2..count]][cutfeat]>cutval\n\t\t */\n\t\tvoid planeSplit(IndexType* ind, const IndexType count, int cutfeat, DistanceType cutval, IndexType& lim1, IndexType& lim2)\n\t\t{\n\t\t\t/* Move vector indices for left subtree to front of list. */\n\t\t\tIndexType left = 0;\n\t\t\tIndexType right = count-1;\n\t\t\tfor (;; ) {\n\t\t\t\twhile (left<=right && dataset_get(ind[left],cutfeat)<cutval) ++left;\n\t\t\t\twhile (right && left<=right && dataset_get(ind[right],cutfeat)>=cutval) --right;\n\t\t\t\tif (left>right || !right) break;  // \"!right\" was added to support unsigned Index types\n\t\t\t\tstd::swap(ind[left], ind[right]);\n\t\t\t\t++left;\n\t\t\t\t--right;\n\t\t\t}\n\t\t\t/* If either list is empty, it means that all remaining features\n\t\t\t * are identical. Split in the middle to maintain a balanced tree.\n\t\t\t */\n\t\t\tlim1 = left;\n\t\t\tright = count-1;\n\t\t\tfor (;; ) {\n\t\t\t\twhile (left<=right && dataset_get(ind[left],cutfeat)<=cutval) ++left;\n\t\t\t\twhile (right && left<=right && dataset_get(ind[right],cutfeat)>cutval) --right;\n\t\t\t\tif (left>right || !right) break;  // \"!right\" was added to support unsigned Index types\n\t\t\t\tstd::swap(ind[left], ind[right]);\n\t\t\t\t++left;\n\t\t\t\t--right;\n\t\t\t}\n\t\t\tlim2 = left;\n\t\t}\n\n\t\tDistanceType computeInitialDistances(const ElementType* vec, distance_vector_t& dists) const\n\t\t{\n\t\t\tassert(vec);\n\t\t\tDistanceType distsq = 0.0;\n\n\t\t\tfor (int i = 0; i < (DIM>0 ? DIM : dim); ++i) {\n\t\t\t\tif (vec[i] < root_bbox[i].low) {\n\t\t\t\t\tdists[i] = distance.accum_dist(vec[i], root_bbox[i].low, i);\n\t\t\t\t\tdistsq += dists[i];\n\t\t\t\t}\n\t\t\t\tif (vec[i] > root_bbox[i].high) {\n\t\t\t\t\tdists[i] = distance.accum_dist(vec[i], root_bbox[i].high, i);\n\t\t\t\t\tdistsq += dists[i];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn distsq;\n\t\t}\n\n\t\t/**\n\t\t * Performs an exact search in the tree starting from a node.\n\t\t * \\tparam RESULTSET Should be any ResultSet<DistanceType>\n\t\t */\n\t\ttemplate <class RESULTSET>\n\t\tvoid searchLevel(RESULTSET& result_set, const ElementType* vec, const NodePtr node, DistanceType mindistsq,\n\t\t\t\t\t\t distance_vector_t& dists, const float epsError) const\n\t\t{\n\t\t\t/* If this is a leaf node, then do check and return. */\n\t\t\tif ((node->child1 == NULL)&&(node->child2 == NULL)) {\n\t\t\t\t//count_leaf += (node->lr.right-node->lr.left);  // Removed since was neither used nor returned to the user.\n\t\t\t\tDistanceType worst_dist = result_set.worstDist();\n\t\t\t\tfor (IndexType i=node->lr.left; i<node->lr.right; ++i) {\n\t\t\t\t\tconst IndexType index = vind[i];// reorder... : i;\n\t\t\t\t\tDistanceType dist = distance(vec, index, (DIM>0 ? DIM : dim));\n\t\t\t\t\tif (dist<worst_dist) {\n\t\t\t\t\t\tresult_set.addPoint(dist,vind[i]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t/* Which child branch should be taken first? */\n\t\t\tint idx = node->sub.divfeat;\n\t\t\tElementType val = vec[idx];\n\t\t\tDistanceType diff1 = val - node->sub.divlow;\n\t\t\tDistanceType diff2 = val - node->sub.divhigh;\n\n\t\t\tNodePtr bestChild;\n\t\t\tNodePtr otherChild;\n\t\t\tDistanceType cut_dist;\n\t\t\tif ((diff1+diff2)<0) {\n\t\t\t\tbestChild = node->child1;\n\t\t\t\totherChild = node->child2;\n\t\t\t\tcut_dist = distance.accum_dist(val, node->sub.divhigh, idx);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tbestChild = node->child2;\n\t\t\t\totherChild = node->child1;\n\t\t\t\tcut_dist = distance.accum_dist( val, node->sub.divlow, idx);\n\t\t\t}\n\n\t\t\t/* Call recursively to search next level down. */\n\t\t\tsearchLevel(result_set, vec, bestChild, mindistsq, dists, epsError);\n\n\t\t\tDistanceType dst = dists[idx];\n\t\t\tmindistsq = mindistsq + cut_dist - dst;\n\t\t\tdists[idx] = cut_dist;\n\t\t\tif (mindistsq*epsError<=result_set.worstDist()) {\n\t\t\t\tsearchLevel(result_set, vec, otherChild, mindistsq, dists, epsError);\n\t\t\t}\n\t\t\tdists[idx] = dst;\n\t\t}\n\n\tpublic:\n\t\t/**  Stores the index in a binary file.\n\t\t  *   IMPORTANT NOTE: The set of data points is NOT stored in the file, so when loading the index object it must be constructed associated to the same source of data points used while building it.\n\t\t  * See the example: examples/saveload_example.cpp\n\t\t  * \\sa loadIndex  */\n\t\tvoid saveIndex(FILE* stream)\n\t\t{\n\t\t\tsave_value(stream, m_size);\n\t\t\tsave_value(stream, dim);\n\t\t\tsave_value(stream, root_bbox);\n\t\t\tsave_value(stream, m_leaf_max_size);\n\t\t\tsave_value(stream, vind);\n\t\t\tsave_tree(stream, root_node);\n\t\t}\n\n\t\t/**  Loads a previous index from a binary file.\n\t\t  *   IMPORTANT NOTE: The set of data points is NOT stored in the file, so the index object must be constructed associated to the same source of data points used while building the index.\n\t\t  * See the example: examples/saveload_example.cpp\n\t\t  * \\sa loadIndex  */\n\t\tvoid loadIndex(FILE* stream)\n\t\t{\n\t\t\tload_value(stream, m_size);\n\t\t\tload_value(stream, dim);\n\t\t\tload_value(stream, root_bbox);\n\t\t\tload_value(stream, m_leaf_max_size);\n\t\t\tload_value(stream, vind);\n\t\t\tload_tree(stream, root_node);\n\t\t}\n\n\t};   // class KDTree\n\n\n\t/** A simple KD-tree adaptor for working with data directly stored in an Eigen Matrix, without duplicating the data storage.\n\t  *  Each row in the matrix represents a point in the state space.\n\t  *\n\t  *  Example of usage:\n\t  * \\code\n\t  * \tEigen::Matrix<num_t,Dynamic,Dynamic>  mat;\n\t  * \t// Fill out \"mat\"...\n\t  *\n\t  * \ttypedef KDTreeEigenMatrixAdaptor< Eigen::Matrix<num_t,Dynamic,Dynamic> >  my_kd_tree_t;\n\t  * \tconst int max_leaf = 10;\n\t  * \tmy_kd_tree_t   mat_index(dimdim, mat, max_leaf );\n\t  * \tmat_index.index->buildIndex();\n\t  * \tmat_index.index->...\n\t  * \\endcode\n\t  *\n\t  *  \\tparam DIM If set to >0, it specifies a compile-time fixed dimensionality for the points in the data set, allowing more compiler optimizations.\n\t  *  \\tparam Distance The distance metric to use: nanoflann::metric_L1, nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc.\n\t  *  \\tparam IndexType The type for indices in the KD-tree index (typically, size_t of int)\n\t  */\n\ttemplate <class MatrixType, int DIM = -1, class Distance = nanoflann::metric_L2, typename IndexType = size_t>\n\tstruct KDTreeEigenMatrixAdaptor\n\t{\n\t\ttypedef KDTreeEigenMatrixAdaptor<MatrixType,DIM,Distance,IndexType> self_t;\n\t\ttypedef typename MatrixType::Scalar              num_t;\n\t\ttypedef typename Distance::template traits<num_t,self_t>::distance_t metric_t;\n\t\ttypedef KDTreeSingleIndexAdaptor< metric_t,self_t,DIM,IndexType>  index_t;\n\n\t\tindex_t* index; //! The kd-tree index for the user to call its methods as usual with any other FLANN index.\n\n\t\t/// Constructor: takes a const ref to the matrix object with the data points\n\t\tKDTreeEigenMatrixAdaptor(const int dimensionality, const MatrixType &mat, const int leaf_max_size = 10) : m_data_matrix(mat)\n\t\t{\n\t\t\tconst size_t dims = mat.cols();\n\t\t\tif (DIM>0 && static_cast<int>(dims)!=DIM)\n\t\t\t\tthrow std::runtime_error(\"Data set dimensionality does not match the 'DIM' template argument\");\n\t\t\tindex = new index_t( dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leaf_max_size, dims ) );\n\t\t\tindex->buildIndex();\n\t\t}\n\n\t\t~KDTreeEigenMatrixAdaptor() {\n\t\t\tdelete index;\n\t\t}\n\n\t\tconst MatrixType &m_data_matrix;\n\n\t\t/** Query for the \\a num_closest closest points to a given point (entered as query_point[0:dim-1]).\n\t\t  *  Note that this is a short-cut method for index->findNeighbors().\n\t\t  *  The user can also call index->... methods as desired.\n\t\t  * \\note nChecks_IGNORED is ignored but kept for compatibility with the original FLANN interface.\n\t\t  */\n\t\tinline void query(const num_t *query_point, const size_t num_closest, IndexType *out_indices, num_t *out_distances_sq, const int nChecks_IGNORED = 10) const\n\t\t{\n\t\t\tnanoflann::KNNResultSet<typename MatrixType::Scalar,IndexType> resultSet(num_closest);\n\t\t\tresultSet.init(out_indices, out_distances_sq);\n\t\t\tindex->findNeighbors(resultSet, query_point, nanoflann::SearchParams());\n\t\t}\n\n\t\t/** @name Interface expected by KDTreeSingleIndexAdaptor\n\t\t  * @{ */\n\n\t\tconst self_t & derived() const {\n\t\t\treturn *this;\n\t\t}\n\t\tself_t & derived()       {\n\t\t\treturn *this;\n\t\t}\n\n\t\t// Must return the number of data points\n\t\tinline size_t kdtree_get_point_count() const {\n\t\t\treturn m_data_matrix.rows();\n\t\t}\n\n\t\t// Returns the distance between the vector \"p1[0:size-1]\" and the data point with index \"idx_p2\" stored in the class:\n\t\tinline num_t kdtree_distance(const num_t *p1, const size_t idx_p2,size_t size) const\n\t\t{\n\t\t\tnum_t s=0;\n\t\t\tfor (size_t i=0; i<size; i++) {\n\t\t\t\tconst num_t d= p1[i]-m_data_matrix.coeff(idx_p2,i);\n\t\t\t\ts+=d*d;\n\t\t\t}\n\t\t\treturn s;\n\t\t}\n\n\t\t// Returns the dim'th component of the idx'th point in the class:\n\t\tinline num_t kdtree_get_pt(const size_t idx, int dim) const {\n\t\t\treturn m_data_matrix.coeff(idx,dim);\n\t\t}\n\n\t\t// Optional bounding-box computation: return false to default to a standard bbox computation loop.\n\t\t//   Return true if the BBOX was already computed by the class and returned in \"bb\" so it can be avoided to redo it again.\n\t\t//   Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds)\n\t\ttemplate <class BBOX>\n\t\tbool kdtree_get_bbox(BBOX &bb) const {\n\t\t\treturn false;\n\t\t}\n\n\t\t/** @} */\n\n\t}; // end of KDTreeEigenMatrixAdaptor\n\t/** @} */\n\n/** @} */ // end of grouping\n} // end of NS\n\n\n#endif /* NANOFLANN_HPP_ */\n"
  },
  {
    "path": "tests/demo/spline_library/utils/spline_common.h",
    "content": "#pragma once\n\n#include <unordered_map>\n#include <vector>\n#include <cmath>\n\nnamespace SplineCommon\n{\n    //compute the T values for the given points, with the given alpha.\n    //the distance in T between adjacent points is the magitude of the distance, raised to the power alpha\n    template<class InterpolationType, typename floating_t>\n    floating_t computeTDiff(InterpolationType p1, InterpolationType p2, floating_t alpha);\n\n\n    //compute t values for the given points, based on the alpha value\n    //if innerPadding > 0, the first 'innerPadding-1' values will be negative, and the innerPadding'th value will be 0\n    //so the spline will effectively begin at innerPadding + 1\n    //this is used for splines like catmull-rom, where the first and last point are used ONLY to calculate tangent\n    template<class InterpolationType, typename floating_t>\n    std::vector<floating_t> computeTValuesWithInnerPadding(\n            const std::vector<InterpolationType> &points,\n            floating_t alpha,\n            size_t innerPadding\n            );\n\n    //compute the T values for the given points, with the given alpha, for use in a looping spline\n    //if padding is zero, this method will return points.size() + 1 points\n    //the \"extra\" point is because the first point in the list is represented at the beginning AND end\n    //if padding is > 0, this method will also compute \"extra\" T values before the beginning and after the end\n    //these won't actually add any extra information, but help simplify calculations that wrap around the loop\n    template<class InterpolationType, typename floating_t>\n    std::vector<floating_t> computeLoopingTValues(const std::vector<InterpolationType> &points, floating_t alpha, size_t padding);\n\n\n\n    //given a list of knots and a t value, return the index of the knot the t value falls within\n    template<typename floating_t>\n    size_t getIndexForT(const std::vector<floating_t> &knotData, floating_t t);\n}\n\ntemplate<class InterpolationType, typename floating_t>\nfloating_t SplineCommon::computeTDiff(InterpolationType p1, InterpolationType p2, floating_t alpha)\n{\n    if(alpha == 0)\n    {\n        return 1;\n    }\n    else\n    {\n        auto distanceSq = (p1 - p2).lengthSquared();\n\n        //if these points are right on top of each other, don't bother with the power calculation\n        if(distanceSq < .0001)\n        {\n            return 0;\n        }\n        else\n        {\n            //multiply alpha by 0.5 so that we tke the square root of distanceSq\n            //ie: result = distance ^ alpha, and distance = (distanceSq)^(0.5)\n            //so: result = (distanceSq^0.5)^(alpha) = (distanceSq)^(0.5*alpha)\n            //this way we don't have to do a pow AND a sqrt\n            return pow(distanceSq, alpha * 0.5);\n        }\n    }\n}\n\ntemplate<class InterpolationType, typename floating_t>\nstd::vector<floating_t> SplineCommon::computeTValuesWithInnerPadding(\n        const std::vector<InterpolationType> &points,\n        floating_t alpha,\n        size_t innerPadding\n        )\n{\n    size_t size = points.size();\n    size_t endPaddingIndex = size - 1 - innerPadding;\n    size_t desiredMaxT = size - 2 * innerPadding - 1;\n\n    std::vector<floating_t> tValues(size);\n\n    //we know points[padding] will have a t value of 0\n    tValues[innerPadding] = 0;\n\n    //loop backwards from padding to give the earlier points negative t values\n    for(size_t i = innerPadding; i > 0; i--)\n    {\n        //Points inside the padding will not be interpolated\n        //so give it a negative t value, so that the first actual point can have a t value of 0\n        tValues[i - 1] = tValues[i] - computeTDiff(points[i - 1], points[i], alpha);\n    }\n\n    //compute the t values of the other points\n    for(size_t i = innerPadding + 1; i < size; i++)\n    {\n        tValues[i] = tValues[i - 1] + computeTDiff(points[i], points[i - 1], alpha);\n    }\n\n    //we want to know the t value of the last segment so that we can normalize them all\n    floating_t maxTRaw = tValues[endPaddingIndex];\n\n    //now that we have all ouf our t values and indexes figured out, normalize the t values by dividing them by maxT\n    floating_t multiplier = desiredMaxT / maxTRaw;\n    for(auto &entry: tValues)\n    {\n        entry *= multiplier;\n    }\n\n    return tValues;\n}\n\ntemplate<class InterpolationType, typename floating_t>\nstd::vector<floating_t> SplineCommon::computeLoopingTValues(\n        const std::vector<InterpolationType> &points,\n        floating_t alpha,\n        size_t padding)\n{\n    size_t size = points.size();\n    floating_t maxT = floating_t(size);\n    std::vector<floating_t> tValues(size + padding * 2 + 1);\n\n    //compute the t values each point\n    tValues[padding] = 0;\n    for(size_t i = 1; i < size; i++)\n    {\n        tValues[i + padding] = tValues[i - 1 + padding] + computeTDiff(points[i], points[i - 1], alpha);\n    }\n\n    //the final t value wraps around to the beginning\n    tValues[size + padding] = tValues[size - 1 + padding] + computeTDiff(points[size - 1], points[0], alpha);\n\n    //we want to know the t value of the last segment so that we can normalize them all\n    floating_t maxTRaw = tValues[size + padding];\n\n    //now that we have all ouf our t values and indexes figured out, normalize the t values by dividing them by maxT\n    floating_t multiplier = maxT / maxTRaw;\n    for(size_t i = 1; i < size + 1; i++) {\n       tValues[i + padding] *= multiplier;\n    }\n\n    //add padding in addition to the points - 2 on each end\n    //we calculate the padding by basically wraping the difference in T values\n    for(size_t i = 0; i < padding; i++)\n    {\n        tValues[i] = tValues[i + size] - maxT;\n        tValues[i + size + padding + 1] = tValues[i + padding + 1] + maxT;\n    }\n\n    return tValues;\n}\n\n\ntemplate<typename floating_t>\nsize_t SplineCommon::getIndexForT(const std::vector<floating_t> &knotData, floating_t t)\n{\n    //we want to find the segment whos t0 and t1 values bound x\n\n    //if no segments bound x, return -1\n    if(t <= knotData.front())\n        return 0;\n    if(t >= knotData.back())\n        return knotData.size() - 1;\n\n    //our initial guess will be to subtract the minimum t value, then take the floor\n    int64_t currentIndex = std::floor(t - knotData.front());\n    int64_t size = knotData.size();\n\n    //move left or right in the array until we've found the correct index\n    int64_t searchSize = 1;\n    while(t < knotData[currentIndex])\n    {\n        while(currentIndex >= 0 && t < knotData[currentIndex])\n        {\n            searchSize++;\n            currentIndex -= searchSize;\n        }\n        if(currentIndex < 0 || t > knotData[currentIndex + 1])\n        {\n            currentIndex += searchSize;\n            searchSize /= 4;\n        }\n\n    }\n    while(t >= knotData[currentIndex + 1])\n    {\n        while(currentIndex < size && t >= knotData[currentIndex])\n        {\n            searchSize++;\n            currentIndex += searchSize;\n        }\n        if(currentIndex >= size || t < knotData[currentIndex])\n        {\n            currentIndex -= searchSize;\n            searchSize /= 4;\n        }\n    }\n    return currentIndex;\n}\n"
  },
  {
    "path": "tests/demo/spline_library/utils/splineinverter.h",
    "content": "#pragma once\n\n#include <vector>\n#include <array>\n\n#include <boost/math/tools/minima.hpp>\n\n#include \"../spline.h\"\n#include \"splinesample_adaptor.h\"\n\ntemplate<class InterpolationType, typename floating_t=float, size_t sampleDimension=2>\nclass SplineInverter\n{\npublic:\n    SplineInverter(const Spline<InterpolationType, floating_t> &spline, int samplesPerT = 10);\n\n    floating_t findClosestT(const InterpolationType &queryPoint) const;\n\nprivate: //methods\n    SplineSamples<sampleDimension, floating_t> makeSplineSamples(int samplesPerT) const;\n\n    static std::array<floating_t, sampleDimension> convertPoint(const InterpolationType &p);\n\nprivate: //data\n    const Spline<InterpolationType, floating_t> &spline;\n\n    //distance in t between samples\n    floating_t sampleStep;\n\n    SplineSampleTree<sampleDimension, floating_t> sampleTree;\n};\n\ntemplate<class InterpolationType, typename floating_t, size_t sampleDimension>\nSplineInverter<InterpolationType, floating_t, sampleDimension>::SplineInverter(\n        const Spline<InterpolationType, floating_t> &spline,\n        int samplesPerT)\n    :spline(spline), sampleStep(1.0 / samplesPerT), sampleTree(makeSplineSamples(samplesPerT))\n{\n\n}\n\ntemplate<class InterpolationType, typename floating_t, size_t sampleDimension>\nSplineSamples<sampleDimension, floating_t> SplineInverter<InterpolationType, floating_t, sampleDimension>::makeSplineSamples(int samplesPerT) const\n{\n    SplineSamples<sampleDimension, floating_t> samples;\n    floating_t maxT = spline.getMaxT();\n\n    //find the number of segments we're going to use\n    int numSegments = std::round(maxT * samplesPerT);\n\n    for(int i = 0; i < numSegments; i++)\n    {\n        floating_t currentT = i * sampleStep;\n        auto sampledPoint = convertPoint(spline.getPosition(currentT));\n        samples.pts.emplace_back(sampledPoint, currentT);\n    }\n\n    //if the spline isn't a loop, add a sample for maxT\n    if(!spline.isLooping())\n    {\n        auto sampledPoint = convertPoint(spline.getPosition(maxT));\n        samples.pts.emplace_back(sampledPoint, maxT);\n    }\n\n    return samples;\n}\n\ntemplate<class InterpolationType, typename floating_t, size_t sampleDimension>\nfloating_t SplineInverter<InterpolationType, floating_t, sampleDimension>::findClosestT(const InterpolationType &queryPoint) const\n{\n    auto convertedQueryPoint = convertPoint(queryPoint);\n    floating_t closestSampleT = sampleTree.findClosestSample(convertedQueryPoint);\n\n    //compute the first derivative of distance to spline at the sample point\n    auto sampleResult = spline.getTangent(closestSampleT);\n    InterpolationType sampleDisplacement = sampleResult.position - queryPoint;\n    floating_t sampleDistanceSlope = InterpolationType::dotProduct(sampleDisplacement.normalized(), sampleResult.tangent);\n\n    //if the spline is not a loop there are a few special cases to account for\n    if(!spline.isLooping())\n    {\n        //if closest sample T is 0, we are on an end. so if the slope is positive, we have to just return the end\n        if(closestSampleT == 0 && sampleDistanceSlope > 0)\n            return 0;\n\n        //if the closest sample T is max T we are on an end. so if the slope is negative, just return the end\n        if(closestSampleT == spline.getMaxT() && sampleDistanceSlope < 0)\n            return spline.getMaxT();\n    }\n\n    //step forwards or backwards in the spline until we find a point where the distance slope has flipped sign.\n    //because \"currentsample\" is the closest point, the \"next\" sample's slope MUST have a different sign\n    //otherwise that sample would be closer\n    //note: this assumption is only true if the samples are close together\n\n    //if sample distance slope is positive we want to move backwards in t, otherwise forwards\n    floating_t a, b;\n    if(sampleDistanceSlope > 0)\n    {\n        a = closestSampleT - sampleStep;\n        b = closestSampleT;\n    }\n    else\n    {\n        a = closestSampleT;\n        b = closestSampleT + sampleStep;\n    }\n\n    auto distanceFunction = [this, queryPoint](floating_t t) {\n        return (spline.getPosition(t) - queryPoint).lengthSquared();\n    };\n\n    //we know that the actual closest T is now between a and b\n    //use brent's method to find the actual closest point, using a and b as bounds\n    auto result = boost::math::tools::brent_find_minima(distanceFunction, a, b, 16);\n    return result.first;\n}\n\ntemplate<class InterpolationType, typename floating_t, size_t sampleDimension>\nstd::array<floating_t, sampleDimension> SplineInverter<InterpolationType, floating_t, sampleDimension>::convertPoint(const InterpolationType &p)\n{\n    std::array<floating_t, sampleDimension> result;\n    for(size_t i = 0; i < sampleDimension; i++) {\n        result[i] = p[i];\n    }\n    return result;\n}\n"
  },
  {
    "path": "tests/demo/spline_library/utils/splinesample_adaptor.h",
    "content": "#pragma once\n\n/***********************************************************************\n * Software License Agreement (BSD License)\n *\n * Copyright 2011 Jose Luis Blanco (joseluisblancoc@gmail.com).\n *   All rights reserved.\n * Copyright 2014 Elliott Mahler (join.together@gmail.com).\n *   All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n * 1. Redistributions of source code must retain the above copyright\n *    notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n *    notice, this list of conditions and the following disclaimer in the\n *    documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n *************************************************************************/\n\n#include \"nanoflann.hpp\"\n#include <vector>\n#include <array>\n\ntemplate<int dimension, typename floating_t>\nstruct SplineSamples\n{\n    typedef floating_t coord_t; //!< The type of each coordinate\n\n    struct Point\n    {\n        std::array<coord_t, dimension> coords;\n        coord_t t;\n\n        Point(const std::array<coord_t, dimension> &coords, coord_t ct)\n            :coords(coords), t(ct)\n        {}\n    };\n\n    std::vector<Point> pts;\n};\n\n\ntemplate <typename Derived, int dimension>\nstruct SplineSampleAdaptor\n{\n    typedef typename Derived::coord_t coord_t;\n\n    const Derived obj;\n\n    /// The constructor that sets the data set source\n    SplineSampleAdaptor(const Derived &obj_) : obj(obj_) {}\n\n    /// CRTP helper method\n    inline const Derived& derived() const { return obj; }\n\n    // Must return the number of data points\n    inline size_t kdtree_get_point_count() const { return derived().pts.size(); }\n\n    // Returns the distance between the vector \"p1[0:size-1]\" and the data point with index \"idx_p2\" stored in the class:\n    inline coord_t kdtree_distance(const coord_t *p1, const size_t idx_p2,size_t size) const\n    {\n        coord_t sum = 0;\n        for(size_t i = 0; i < size; i++) {\n            coord_t diff = p1[i]-derived().pts[idx_p2].coords[i];\n            sum += diff*diff;\n        }\n        return sum;\n    }\n\n    // Returns the dim'th component of the idx'th point in the class:\n    // Since this is inlined and the \"dim\" argument is typically an immediate value, the\n    //  \"if/else's\" are actually solved at compile time.\n    inline coord_t kdtree_get_pt(const size_t idx, int dim) const\n    {\n        return derived().pts[idx].coords[dim];\n    }\n\n    // Optional bounding-box computation: return false to default to a standard bbox computation loop.\n    //   Return true if the BBOX was already computed by the class and returned in \"bb\" so it can be avoided to redo it again.\n    //   Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds)\n    // bb parameter is commented out because it's unused! we get obnoxious compiler warnings if we leave it uncommented\n    template <class BBOX>\n    bool kdtree_get_bbox(BBOX &/*bb*/) const { return false; }\n};\n\ntemplate<int dimension, typename floating_t>\nclass SplineSampleTree\n{\n    typedef SplineSampleAdaptor<SplineSamples<dimension, floating_t>, dimension> AdaptorType;\n    typedef nanoflann::KDTreeSingleIndexAdaptor<\n            nanoflann::L2_Simple_Adaptor<floating_t, AdaptorType>,\n            AdaptorType, dimension>\n        TreeType;\n\npublic:\n    SplineSampleTree(const SplineSamples<dimension, floating_t> &samples)\n        :adaptor(samples), tree(dimension, adaptor)\n    {\n        tree.buildIndex();\n    }\n\n    floating_t findClosestSample(const std::array<floating_t, dimension> &queryPoint) const\n    {\n        // do a knn search\n        const size_t num_results = 1;\n        size_t ret_index;\n        floating_t out_dist_sqr;\n        nanoflann::KNNResultSet<floating_t> resultSet(num_results);\n        resultSet.init(&ret_index, &out_dist_sqr );\n        tree.findNeighbors(resultSet, queryPoint.data(), nanoflann::SearchParams());\n\n        return adaptor.derived().pts.at(ret_index).t;\n    }\n\nprivate:\n    AdaptorType adaptor;\n    TreeType tree;\n};\n"
  },
  {
    "path": "tests/demo/spline_library/vector.h",
    "content": "#pragma once\n\n#include <array>\n#include <cmath>\n\ntemplate<size_t dimension, typename floating_t=float>\nclass Vector\n{\npublic:\n    Vector(void) :data() {}\n    Vector(std::array<floating_t, dimension> data) :data(data) {}\n\n    inline floating_t& operator[](size_t index) { return data[index]; }\n    inline floating_t operator[](size_t index) const { return data[index]; }\n\n    inline Vector<dimension, floating_t> &operator+=(const Vector<dimension, floating_t> &v);\n    inline Vector<dimension, floating_t> &operator-=(const Vector<dimension, floating_t> &v);\n    inline Vector<dimension, floating_t> &operator*=(floating_t s);\n    inline Vector<dimension, floating_t> &operator/=(floating_t s);\n\n    template<size_t d, typename f> friend inline Vector<d, f> operator+(const Vector<d, f> &left, const Vector<d, f> &right);\n    template<size_t d, typename f> friend inline Vector<d, f> operator-(const Vector<d, f> &left, const Vector<d, f> &right);\n    template<size_t d, typename f> friend inline Vector<d, f> operator*(f s, const Vector<d, f> &v);\n    template<size_t d, typename f> friend inline Vector<d, f> operator*(const Vector<d, f> &v, f s);\n    template<size_t d, typename f> friend inline Vector<d, f> operator-(const Vector<d, f> &v);\n    template<size_t d, typename f> friend inline Vector<d, f> operator/(const Vector<d, f> &v, f s);\n\n\n    template<size_t d, typename f> friend inline bool operator==(const Vector<d, f> &left, const Vector<d, f> &right);\n    template<size_t d, typename f> friend inline bool operator!=(const Vector<d, f> &left, const Vector<d, f> &right);\n\n    inline floating_t length() const;\n    inline floating_t lengthSquared() const;\n\n    inline Vector<dimension, floating_t> normalized() const;\n\n    inline static floating_t dotProduct(const Vector<dimension, floating_t>& left, const Vector<dimension, floating_t>& right);\n\nprivate:\n    std::array<floating_t, dimension> data;\n};\n\ntypedef Vector<2> Vector2;\ntypedef Vector<3> Vector3;\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> &Vector<dimension, floating_t>::operator+=(const Vector<dimension, floating_t> &other)\n{\n    for(size_t i = 0; i < dimension; i++) {\n        data[i] += other.data[i];\n    }\n    return *this;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> &Vector<dimension, floating_t>::operator-=(const Vector<dimension, floating_t> &v)\n{\n    for(size_t i = 0; i < dimension; i++) {\n        data[i] -= v.data[i];\n    }\n    return *this;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> &Vector<dimension, floating_t>::operator*=(floating_t s)\n{\n    for(size_t i = 0; i < dimension; i++) {\n        data[i] *= s;\n    }\n    return *this;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> &Vector<dimension, floating_t>::operator/=(floating_t s)\n{\n    for(size_t i = 0; i < dimension; i++) {\n        data[i] /= s;\n    }\n    return *this;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> operator+(const Vector<dimension, floating_t> &left, const Vector<dimension, floating_t> &right)\n{\n    Vector<dimension, floating_t> result;\n    for(size_t i = 0; i < dimension; i++) {\n        result.data[i] = left.data[i] + right.data[i];\n    }\n    return result;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> operator-(const Vector<dimension, floating_t> &left, const Vector<dimension, floating_t> &right)\n{\n    Vector<dimension, floating_t> result;\n    for(size_t i = 0; i < dimension; i++) {\n        result.data[i] = left.data[i] - right.data[i];\n    }\n    return result;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> operator*(floating_t s, const Vector<dimension, floating_t> &v)\n{\n    Vector<dimension, floating_t> result;\n    for(size_t i = 0; i < dimension; i++) {\n        result.data[i] = s * v.data[i];\n    }\n    return result;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> operator*(const Vector<dimension, floating_t> &v, floating_t s)\n{\n    Vector<dimension, floating_t> result;\n    for(size_t i = 0; i < dimension; i++) {\n        result.data[i] = v.data[i] * s;\n    }\n    return result;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> operator-(const Vector<dimension, floating_t> &v)\n{\n    Vector<dimension, floating_t> result;\n    for(size_t i = 0; i < dimension; i++) {\n        result.data[i] = -v.data[i];\n    }\n    return result;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> operator/(const Vector<dimension, floating_t> &v, floating_t s)\n{\n    Vector<dimension, floating_t> result;\n    for(size_t i = 0; i < dimension; i++) {\n        result.data[i] = v.data[i] / s;\n    }\n    return result;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline bool operator==(const Vector<dimension, floating_t> &left, const Vector<dimension, floating_t> &right)\n{\n    for(size_t i = 0; i < dimension; i++) {\n        if(left.data[i] != right.data[i])\n            return false;\n    }\n    return true;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline bool operator!=(const Vector<dimension, floating_t> &left, const Vector<dimension, floating_t> &right)\n{\n    for(size_t i = 0; i < dimension; i++) {\n        if(left.data[i] != right.data[i])\n            return true;\n    }\n    return false;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline Vector<dimension, floating_t> Vector<dimension, floating_t>::normalized() const\n{\n    floating_t length2 = lengthSquared();\n    if (length2 == 0)\n        return Vector<dimension, floating_t>();\n    else\n    {\n        floating_t invLength = 1 / std::sqrt(length2);\n        return (*this) * invLength;\n    }\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline floating_t Vector<dimension, floating_t>::dotProduct(const Vector<dimension, floating_t>& v1, const Vector<dimension, floating_t>& v2)\n{\n    floating_t sum(0);\n    for(size_t i = 0; i < dimension; i++) {\n        sum += v1.data[i] * v2.data[i];\n    }\n    return sum;\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline floating_t Vector<dimension, floating_t>::length() const\n{\n    return std::sqrt(Vector<dimension, floating_t>::dotProduct(*this, *this));\n}\n\ntemplate<size_t dimension, typename floating_t>\ninline floating_t Vector<dimension, floating_t>::lengthSquared() const\n{\n    return Vector<dimension, floating_t>::dotProduct(*this, *this);\n}\n"
  },
  {
    "path": "tests/demo/spline_node.cpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#include \"spline_node.hpp\"\n\n#include <imgui_tools.hpp>\n#include <scene_graph/camera_node.hpp>\n#include <scene_graph/scene_graph.hpp>\n#include <fstream>\n#include <queue>\n\nstatic bool initialized = false;\n\nSplineNode::SplineNode(std::string name, bool looping) : Node(typeid(SplineNode)), m_name(name), m_animate(false), m_speed(1), m_time(0), m_spline(nullptr), m_quat_spline(nullptr), m_looping(looping)\n{\n\tif (initialized) return;\n\tinitialized = true;\n\n\twr::imgui::window::SceneGraphEditorDetails::sg_editor_type_names[typeid(SplineNode)] =\n\t{\n\t\t[](std::shared_ptr<Node> node) -> std::string {\n\t\t\tauto spline_node = std::static_pointer_cast<SplineNode>(node);\n\t\t\treturn spline_node->m_name;\n\t\t}\n\t};\n\n\twr::imgui::window::SceneGraphEditorDetails::sg_editor_type_inspect[typeid(SplineNode)] =\n\t{\n\t\t[](std::shared_ptr<Node> node, wr::SceneGraph* scene_graph) \n\t\t{\n\t\t\tauto spline_node = std::static_pointer_cast<SplineNode>(node);\n\n\t\t\tImGui::Checkbox(\"Animate\", &spline_node->m_animate);\n\t\t\tImGui::DragFloat(\"Speed\", &spline_node->m_speed);\n\t\t\tImGui::DragFloat(\"Time\t\", &spline_node->m_time);\n\n\t\t\tif (ImGui::Button(\"Save Spline\"))\n\t\t\t{\n\t\t\t\tauto result = spline_node->SaveDialog();\n\n\t\t\t\tif (result.has_value())\n\t\t\t\t{\n\t\t\t\t\tspline_node->SaveSplineToFile(result.value());\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tLOGW(\"The Open dialog didn't return a file name.\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tImGui::SameLine();\n\n\t\t\tif (ImGui::Button(\"Load Spline\"))\n\t\t\t{\n\t\t\t\tauto result = spline_node->LoadDialog();\n\n\t\t\t\tif (result.has_value())\n\t\t\t\t{\n\t\t\t\t\tspline_node->LoadSplineFromFile(result.value());\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tLOGW(\"The Open dialog didn't return a file name.\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (ImGui::Button(\"Add Control Point\"))\n\t\t\t{\n\t\t\t\tControlPoint cp{};\n\t\t\t\tcp.m_position = scene_graph->GetActiveCamera()->m_position;\n\t\t\t\tcp.m_rotation = scene_graph->GetActiveCamera()->m_rotation_radians;\n\n\t\t\t\tspline_node->m_control_points.push_back(cp);\n\t\t\t}\n\n\t\t\tImGui::Separator();\n\n\t\t\tfor (std::size_t i = 0; i < spline_node->m_control_points.size(); i++)\n\t\t\t{\n\t\t\t\tauto i_str = std::to_string(i);\n\n\t\t\t\tif (ImGui::TreeNode((\"Control Point \" + i_str).c_str()))\n\t\t\t\t{\n\t\t\t\t\tauto& cp = spline_node->m_control_points[i];\n\t\t\t\t\tImGui::DragFloat3((\"Pos##\" + i_str).c_str(), cp.m_position.m128_f32);\n\t\t\t\t\tImGui::DragFloat3((\"Rot##\" + i_str).c_str(), cp.m_rotation.m128_f32);\n\n\t\t\t\t\tif (ImGui::Button(\"Take Camera Transform\"))\n\t\t\t\t\t{\n\t\t\t\t\t\tcp.m_position = scene_graph->GetActiveCamera()->m_position;\n\t\t\t\t\t\tcp.m_rotation = scene_graph->GetActiveCamera()->m_rotation_radians;\n\t\t\t\t\t\t\n\t\t\t\t\t}\n\n\t\t\t\t\tif (ImGui::Button(\"Teleport\"))\n\t\t\t\t\t{\n\t\t\t\t\t\tscene_graph->GetActiveCamera()->SetPosition(cp.m_position);\n\t\t\t\t\t\tscene_graph->GetActiveCamera()->SetRotation(cp.m_rotation);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (ImGui::Button(\"Remove\"))\n\t\t\t\t\t{\n\t\t\t\t\t\tspline_node->m_control_points.erase(spline_node->m_control_points.begin() + i);\n\t\t\t\t\t}\n\n\t\t\t\t\tImGui::TreePop();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tspline_node->UpdateNaturalSpline();\n\t\t}\n\t};\n}\n\nSplineNode::~SplineNode()\n{\n\tdelete m_spline;\n}\n\nvoid SplineNode::UpdateSplineNode(float delta, std::shared_ptr<wr::Node> node)\n{\n\tif (!m_spline || !m_quat_spline || !m_animate) return; // Don't try to run this code if we don't have a spline.\n\n\tauto impl = [&](auto spline, auto quat_spline)\n\t{\n\t\tm_time += delta * m_speed;\n\t\tm_time = std::fmod(m_time, spline->getMaxT());\n\n\t\tauto new_pos = spline->getPosition(m_time);\n\t\tauto new_rot = quat_spline->getPosition(m_time);\n\n\t\tnode->SetPosition({ new_pos[0], new_pos[1], new_pos[2] });\n\t\tnode->SetRotationQuaternion({ new_rot[0], new_rot[1], new_rot[2], new_rot[3] });\n\t};\n\n\tif (m_looping)\n\t{\n\t\timpl(reinterpret_cast<LoopingNaturalSpline<Vector<3>>*>(m_spline), reinterpret_cast<LoopingNaturalSpline<Vector<4>>*>(m_quat_spline));\n\t}\n\telse\n\t{\n\t\timpl(reinterpret_cast<NaturalSpline<Vector<3>>*>(m_spline), reinterpret_cast<NaturalSpline<Vector<4>>*>(m_quat_spline));\n\t}\n}\n\nvoid SplineNode::UpdateNaturalSpline()\n{\n    delete m_spline;\n    m_spline = nullptr;\n\n    delete m_quat_spline;\n    m_quat_spline = nullptr;\n\n\tif (m_control_points.size() < (m_looping ? 2 : 3))\n\t{\n\t\treturn;\n\t}\n\n\t{\n\t\tstd::vector<Vector<3>> m_spline_positions;\n\n\t\tfor (auto const& cp : m_control_points)\n\t\t{\n\t\t\tm_spline_positions.push_back(Vector<3>({ cp.m_position.m128_f32[0], cp.m_position.m128_f32[1], cp.m_position.m128_f32[2] }));\n\t\t}\n\n\t\tif (m_looping)\n\t\t{\n\t\t\tm_spline = new LoopingNaturalSpline<Vector<3>>(m_spline_positions);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tm_spline = new NaturalSpline<Vector<3>>(m_spline_positions);\n\t\t}\n\t}\n\n\t{\n\t\tstd::vector<Vector<4>> m_spline_rotations;\n\n\t\tfor (auto const& cp : m_control_points)\n\t\t{\n\t\t\tauto quat = DirectX::XMQuaternionRotationRollPitchYawFromVector(cp.m_rotation);\n\t\t\tm_spline_rotations.push_back(Vector<4>({ quat.m128_f32[0], quat.m128_f32[1], quat.m128_f32[2], quat.m128_f32[3] }));\n\t\t}\n\n\t\tif (m_looping)\n\t\t{\n\t\t\tm_quat_spline = new LoopingNaturalSpline<Vector<4>>(m_spline_rotations);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tm_quat_spline = new NaturalSpline<Vector<4>>(m_spline_rotations);\n\t\t}\n\t}\n}\n\nstd::optional<std::string> SplineNode::LoadDialog()\n{\n\tchar buffer[MAX_PATH];\n\n\tOPENFILENAME dialog_args;\n\tZeroMemory(&dialog_args, sizeof(dialog_args));\n\tdialog_args.lStructSize = sizeof(OPENFILENAME);\n\tdialog_args.hwndOwner = nullptr;\n\tdialog_args.lpstrFile = buffer;\n\tdialog_args.lpstrFile[0] = '\\0';\n\tdialog_args.nMaxFile = sizeof(buffer);\n\tdialog_args.lpstrFilter = \"All\\0*.*\\0Spline\\0*.SPL\\0\";\n\tdialog_args.nFilterIndex = 1;\n\tdialog_args.lpstrTitle = \"Load Spline\";\n\tdialog_args.lpstrFileTitle = nullptr;\n\tdialog_args.nMaxFileTitle = 0;\n\tdialog_args.lpstrInitialDir = nullptr;\n\tdialog_args.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;\n\n\tbool file_selected = GetOpenFileName(&dialog_args);\n\n\tif (file_selected)\n\t{\n\t\treturn std::string(buffer);\n\t}\n\n\treturn std::nullopt;\n}\n\nstd::optional<std::string> SplineNode::SaveDialog()\n{\n\tchar buffer[MAX_PATH] = \"spline.spl\";;\n\n\tOPENFILENAME dialog_args;\n\tZeroMemory(&dialog_args, sizeof(dialog_args));\n\tdialog_args.lStructSize = sizeof(OPENFILENAME);\n\tdialog_args.hwndOwner = nullptr;\n\tdialog_args.lpstrFile = buffer;\n\t//dialog_args.lpstrFile[0] = '\\0';\n\tdialog_args.nMaxFile = sizeof(buffer);\n\tdialog_args.lpstrFilter = \"Spline\\0*.SPL\\0\";\n\tdialog_args.nFilterIndex = 1;\n\tdialog_args.lpstrFileTitle = nullptr;\n\tdialog_args.lpstrTitle = \"Save Spline\";\n\tdialog_args.nMaxFileTitle = 0;\n\tdialog_args.lpstrInitialDir = nullptr;\n\tdialog_args.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;\n\n\tbool file_selected = GetSaveFileName(&dialog_args);\n\n\tif (file_selected)\n\t{\n\t\treturn std::string(buffer);\n\t}\n\n\treturn std::nullopt;\n}\n\nvoid SplineNode::SaveSplineToFile(std::string const & path)\n{\n\tstd::ofstream file(path);\n\t\n\tfor (auto cp : m_control_points)\n\t{\n\t\tfile << cp.m_position.m128_f32[0] << \",\" << cp.m_position.m128_f32[1] << \",\" << cp.m_position.m128_f32[2] << \",\";\n\t\tfile << cp.m_rotation.m128_f32[0] << \",\" << cp.m_rotation.m128_f32[1] << \",\" << cp.m_rotation.m128_f32[2] << \",\\n\";\n\t}\n\n\tfile.close();\n}\n\nstd::vector<std::string> Split(const std::string& subject)\n{\n\tstd::istringstream ss{ subject };\n\tusing StrIt = std::istream_iterator<std::string>;\n\tstd::vector<std::string> container{ StrIt{ss}, StrIt{} };\n\treturn container;\n}\n\nvoid SplineNode::LoadSplineFromFile(std::string const& path)\n{\n\tstd::ifstream file(path);\n\n\tstd::string str;\n\tstd::deque<float> numbers;\n\twhile (std::getline(file, str, ','))\n\t{\n\t\tnumbers.push_back(std::atof(str.c_str()));\n\t}\n\n\tm_control_points.clear();\n\n\twhile (numbers.size() > 5)\n\t{\n\t\tControlPoint cp{};\n\t\tcp.m_position = { numbers[0], numbers[1], numbers[2] };\n\t\tcp.m_rotation = { numbers[3], numbers[4], numbers[5] };\n\t\tm_control_points.push_back(cp);\n\t\t \n\t\tnumbers.erase(numbers.begin(), numbers.begin() + 6);\n\t}\n\n\tfile.close();\n}\n"
  },
  {
    "path": "tests/demo/spline_node.hpp",
    "content": "/*!\n * Copyright 2019 Breda University of Applied Sciences and Team Wisp (Viktor Zoutman, Emilio Laiso, Jens Hagen, Meine Zeinstra, Tahar Meijs, Koen Buitenhuis, Niels Brunekreef, Darius Bouma, Florian Schut)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n#pragma once\n\n#include <scene_graph/node.hpp>\n#include \"spline_library/splines/natural_spline.h\"\n#include \"spline_library/vector.h\"\n#include <optional>\n\nclass SplineNode : public wr::Node\n{\n\tstruct ControlPoint\n\t{\n\t\tDirectX::XMVECTOR m_position;\n\t\tDirectX::XMVECTOR m_rotation;\n\t};\n\npublic:\n\texplicit SplineNode(std::string name, bool looping = false);\n\t~SplineNode();\n\n\tvoid UpdateSplineNode(float delta, std::shared_ptr<wr::Node> node);\n\nprivate:\n\tvoid UpdateNaturalSpline();\n\n\tstatic std::optional<std::string> LoadDialog();\n\tstatic std::optional<std::string> SaveDialog();\n\tvoid SaveSplineToFile(std::string const & path);\n\tvoid LoadSplineFromFile(std::string const & path);\n\n\tbool m_animate;\n\n\tvoid* m_spline;\n\tvoid* m_quat_spline;\n\n\tstd::vector<ControlPoint> m_control_points;\n\tfloat m_speed;\n\tfloat m_time;\n\n\tbool m_looping;\n\n\tstd::string m_name;\n};"
  },
  {
    "path": "tests/graphics_benchmark/frame_graphs.cpp",
    "content": ""
  },
  {
    "path": "tests/graphics_benchmark/frame_graphs.hpp",
    "content": "#pragma once\n\n#include \"frame_graph/frame_graph.hpp\"\n#include \"render_tasks/d3d12_imgui_render_task.hpp\"\n#include \"render_tasks/d3d12_brdf_lut_precalculation.hpp\"\n#include \"render_tasks/d3d12_deferred_main.hpp\"\n#include \"render_tasks/d3d12_deferred_composition.hpp\"\n#include \"render_tasks/d3d12_deferred_render_target_copy.hpp\"\n#include \"render_tasks/d3d12_raytracing_task.hpp\"\n#include \"render_tasks/d3d12_equirect_to_cubemap.hpp\"\n#include \"render_tasks/d3d12_cubemap_convolution.hpp\"\n#include \"render_tasks/d3d12_post_processing.hpp\"\n#include \"render_tasks/d3d12_build_acceleration_structures.hpp\"\n#include \"render_tasks/d3d12_path_tracer.hpp\"\n#include \"render_tasks/d3d12_accumulation.hpp\"\n#include \"render_tasks/d3d12_hbao.hpp\"\n#include \"render_tasks/d3d12_ansel.hpp\"\n\nusing frame_graph_setup_func_t = std::function<std::shared_ptr<wr::FrameGraph>(wr::D3D12RenderSystem&)>;\n\nenum class FGType : std::int32_t\n{\n\tPBR_BASIC,\n\tPBR_RT_REF_SHADOWS,\n\tPBR_RT_REF_SHADOWS_PATH_TRACING,\n\tCOUNT\n};\n\ninline std::string GetFrameGraphName(FGType id)\n{\n\tswitch (id)\n\t{\n\tcase FGType::PBR_BASIC: return \"PBR Basic\";\n\tcase FGType::PBR_RT_REF_SHADOWS: return \"PBR RT Reflections and Shadows\";\n\tcase FGType::PBR_RT_REF_SHADOWS_PATH_TRACING: return \"PBR RT Reflections Shadows and Path Tracing\";\n\tdefault:\n\t\treturn \"Unknown\";\n\t}\n}\n\nstatic std::array<frame_graph_setup_func_t, static_cast<std::size_t>(FGType::COUNT)> frame_graph_setup_functions =\n{\n\t// PBR Basic\n\t[] (wr::D3D12RenderSystem & rs)\n\t{\n\t\tauto fg = std::make_shared<wr::FrameGraph>(7);\n\n\t\twr::AddBrdfLutPrecalculationTask(*fg);\n\t\twr::AddEquirectToCubemapTask(*fg);\n\t\twr::AddCubemapConvolutionTask(*fg);\n\t\twr::AddDeferredMainTask(*fg, std::nullopt, std::nullopt, false);\n\t\twr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt);\n\t\twr::AddPostProcessingTask<wr::DeferredCompositionTaskData>(*fg);\n\t\twr::AddRenderTargetCopyTask<wr::PostProcessingData>(*fg);\n\n\t\tfg->Validate();\n\t\tfg->Setup(rs);\n\t\treturn fg;\n\t},\n\n\t// PBR RT Reflections and shadows\n\t[](wr::D3D12RenderSystem& rs)\n\t{\n\t\tauto fg = std::make_shared<wr::FrameGraph>(7);\n\n\t\twr::AddBrdfLutPrecalculationTask(*fg);\n\t\twr::AddEquirectToCubemapTask(*fg);\n\t\twr::AddCubemapConvolutionTask(*fg);\n\t\twr::AddDeferredMainTask(*fg, std::nullopt, std::nullopt, true);\n\t\twr::AddBuildAccelerationStructuresTask(*fg);\n\t\twr::AddRTReflectionTask(*fg);\n\t\twr::AddRTShadowTask(*fg);\n\t\twr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt);\n\t\twr::AddPostProcessingTask<wr::DeferredCompositionTaskData>(*fg);\n\t\twr::AddRenderTargetCopyTask<wr::PostProcessingData>(*fg);\n\n\t\tfg->Validate();\n\t\tfg->Setup(rs);\n\t\treturn fg;\n\t},\n\n\t// PBR RT Reflections shadows and path tracing\n\t[](wr::D3D12RenderSystem& rs)\n\t{\n\t\tauto fg = std::make_shared<wr::FrameGraph>(7);\n\n\t\twr::AddBrdfLutPrecalculationTask(*fg);\n\t\twr::AddEquirectToCubemapTask(*fg);\n\t\twr::AddCubemapConvolutionTask(*fg);\n\t\twr::AddDeferredMainTask(*fg, std::nullopt, std::nullopt, true);\n\t\twr::AddBuildAccelerationStructuresTask(*fg);\n\t\twr::AddRTReflectionTask(*fg);\n\t\twr::AddRTShadowTask(*fg);\n\t\twr::AddPathTracerTask(*fg);\n\t\twr::AddAccumulationTask<wr::PathTracerData>(*fg);\n\t\twr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt);\n\t\twr::AddPostProcessingTask<wr::DeferredCompositionTaskData>(*fg);\n\t\twr::AddRenderTargetCopyTask<wr::PostProcessingData>(*fg);\n\n\t\tfg->Validate();\n\t\tfg->Setup(rs);\n\t\treturn fg;\n\t},\n};\n\nstatic std::shared_ptr<wr::FrameGraph> CreateFrameGraph(FGType type, wr::D3D12RenderSystem& rs)\n{\n\treturn frame_graph_setup_functions[static_cast<std::int32_t>(type)](rs);\n}"
  },
  {
    "path": "tests/graphics_benchmark/graphics_benchmark.cpp",
    "content": "#include <memory>\n#include <algorithm>\n#include <thread>\n#include <filesystem>\n\n#include \"wisp.hpp\"\n#include \"d3d12/d3d12_renderer.hpp\"\n\n#include \"spheres_scene.hpp\"\n#include \"frame_graphs.hpp\"\n\nstatic const unsigned int default_window_width = 1290;\nstatic const unsigned int default_window_height = 720;\nstatic const unsigned int frames_till_capture = 3;\nstatic const std::string output_dir = \"benchmark_images/\";\nstatic int benchmark_number = 0;\n\ninline void ReplaceAll(std::string& str, std::string const & original_delimiter, std::string const & new_delimiter)\n{\n\tstd::string::size_type n = 0;\n\twhile ((n = str.find(original_delimiter, n)) != std::string::npos)\n\t{\n\t\tstr.replace(n, original_delimiter.size(), new_delimiter);\n\t\tn += new_delimiter.size();\n\t}\n}\n\ntemplate<typename S, typename O>\nvoid PerformBenchmark(FGType fg_type, unsigned int width = default_window_width, unsigned int height = default_window_height, unsigned int output_render_target_index = 0)\n{\n\t// Get benchmark information\n\tstd::string scene_name = std::string(typeid(S).name());\n\tscene_name.erase(0, 6); // Remove \"class \" from the name.\n\tstd::transform(scene_name.begin(), scene_name.end(), scene_name.begin(), ::tolower);\n\n\tstd::string rt_name = std::string(typeid(O).name()) + \"_\" + std::to_string(output_render_target_index);\n\trt_name.erase(0, 6); // Remove \"class \" from the name.\n\tstd::transform(rt_name.begin(), rt_name.end(), rt_name.begin(), ::tolower);\n\tReplaceAll(rt_name, \"::\", \"-\");\n\n\tstd::string fg_name = GetFrameGraphName(fg_type);\n\tstd::transform(fg_name.begin(), fg_name.end(), fg_name.begin(), ::tolower);\n\tReplaceAll(fg_name, \" \", \"_\");\n\n\tLOGW(\"Starting Benchmark \\\"{}\\\" for scene \\\"{}\\\" with fg \\\"{}\\\" to output \\\"{}\\\"\", benchmark_number, scene_name, fg_name, rt_name);\n\n\tauto scene = std::make_unique<S>();\n\n\t// Initialize\n\tauto render_system = std::make_unique<wr::D3D12RenderSystem>();\n\tstd::string window_title = \"Benchmark (Scene: \" + scene_name + \" Fg:\" + fg_name + \" Rt:\" + rt_name + \")\";\n\tauto window = std::make_unique<wr::Window>(GetModuleHandleA(nullptr), window_title, width, height, true);\n\n\twr::ModelLoader* assimp_model_loader = new wr::AssimpModelLoader();\n\n\trender_system->Init(window.get());\n\tscene->Init(render_system.get(), width, height);\n\n\tauto frame_graph = CreateFrameGraph(fg_type, *render_system);\n\n\t// Render\n\tunsigned int frame = 0;\n\twhile (window->IsRunning())\n\t{\n\t\twindow->PollEvents();\n\n\t\tscene->Update();\n\t\trender_system->Render(*scene->GetSceneGraph(), *frame_graph);\n\n\t\t// Capture screenshot\n\t\tif (frame == frames_till_capture)\n\t\t{\n\t\t\tstd::string path = output_dir + \"wisp_img_\" + std::to_string(benchmark_number) + \"_\" + scene_name + \"_\" + fg_name + \"_\" + rt_name + \".tga\";\n\t\t\tframe_graph->SaveTaskToDisc<O>(path, output_render_target_index);\n\t\t\tLOGW(\"Saving output to: {}\", output_dir);\n\t\t}\n\t\t// Quit after capture.\n\t\telse if (frame == frames_till_capture + 1)\n\t\t{\n\t\t\twindow->Stop();\n\t\t}\n\n\t\tframe++;\n\t}\n\n\t// Shutdown\n\trender_system->WaitForAllPreviousWork();\n\n\tdelete assimp_model_loader;\n\tscene.reset();\n\tframe_graph.reset();\n\trender_system.reset();\n\n\tbenchmark_number++;\n}\n\nint GraphicsBenchmarkEntry()\n{\n\t// Create output directory if nessessary.\n\tif (!std::filesystem::exists(output_dir))\n\t{\n\t\tstd::filesystem::create_directory(output_dir);\n\t\tLOGW(\"Created output dir {}\", output_dir);\n\t}\n\n\tLOGW(\"Starting Benchmarks\");\n\n\tPerformBenchmark<SpheresScene, wr::PostProcessingData>(FGType::PBR_BASIC, 1000, 1000);\n\tPerformBenchmark<SpheresScene, wr::PostProcessingData>(FGType::PBR_RT_REF_SHADOWS, 1000, 1000);\n\n\tLOGW(\"Benchmarks Finished\")\n\n\treturn 0;\n}\n\nWISP_ENTRY(GraphicsBenchmarkEntry)"
  },
  {
    "path": "tests/graphics_benchmark/spheres_scene.cpp",
    "content": "#pragma once\n\n#include \"spheres_scene.hpp\"\n\nSpheresScene::SpheresScene() :\n\tScene(256, 2_mb, 2_mb),\n\tm_sphere_model(nullptr),\n\tm_skybox({})\n{\n\n}\n\nvoid SpheresScene::LoadResources()\n{\n\t// Models\n\tm_sphere_model = m_model_pool->Load<wr::Vertex>(m_material_pool.get(), m_texture_pool.get(), \"resources/models/sphere.fbx\");\n\n\t// Textures\n\tm_skybox = m_texture_pool->LoadFromFile(\"resources/materials/Circus_Backstage_3k.hdr\", false, false);\n\tm_flat_normal = m_texture_pool->LoadFromFile(\"resources/materials/flat_normal.png\", false, false);\n}\n\nvoid SpheresScene::BuildScene(unsigned int width, unsigned int height)\n{\n\tauto camera = m_scene_graph->CreateChild<wr::CameraNode>(nullptr, (float)width / height);\n\tcamera->SetFov(45.f);\n\tcamera->SetPosition({ 0.0f, 0.0f, 30.f });\n\n\tm_scene_graph->CreateChild<wr::SkyboxNode>(nullptr, m_skybox);\n\n\tstd::shared_ptr<wr::MeshNode> spheres[49];\n\tfor (uint32_t i = 0; i <= 6; ++i)\n\t{\n\t\tfor (uint32_t j = 0; j <= 6; ++j)\n\t\t{\n\t\t\tspheres[i * 7 + j] = m_scene_graph->CreateChild<wr::MeshNode>(nullptr, m_sphere_model);\n\n\t\t\tfloat x_pos = Lerp(10.0f, -10.0f, (float)i / 6.f);\n\t\t\tfloat y_pos = Lerp(10.0f, -10.0f, (float)j / 6.f);\n\n\t\t\tspheres[i * 7 + j]->SetPosition({ x_pos, y_pos, 0.0f, 0.0f });\n\t\t\tspheres[i * 7 + j]->SetRotation({ 90.0_deg, 0.0_deg, 0.0_deg });\n\n\t\t\tfloat roughness = Lerp(1.0f, 0.0f, (float)i / 6);\n\t\t\tfloat metallic = Lerp(1.0f, 0.0f, (float)j / 6);\n\n\t\t\t//Create new material\n\t\t\twr::MaterialHandle mat = m_material_pool->Create(m_texture_pool.get());\n\t\t\tauto mat_internal = mat.m_pool->GetMaterial(mat);\n\n\t\t\tmat_internal->SetConstant<wr::MaterialConstant::COLOR>({ 1, 0, 0 });\n\t\t\tmat_internal->SetTexture(wr::TextureType::NORMAL, m_flat_normal);\n\t\t\tmat_internal->SetConstant<wr::MaterialConstant::ROUGHNESS>(roughness);\n\t\t\tmat_internal->SetConstant<wr::MaterialConstant::METALLIC>(metallic);\n\n\t\t\tspheres[i * 7 + j]->SetMaterials({ mat });\n\t\t}\n\t}\n\n\tauto dir_light = m_scene_graph->CreateChild<wr::LightNode>(nullptr, wr::LightType::DIRECTIONAL, DirectX::XMVECTOR{ 0, 1, 0 });\n\tdir_light->SetDirectional({ 136._deg, 0, 0 }, { 4, 4, 4 });\n}\n\nvoid SpheresScene::Update()\n{\n\n}\n\nfloat SpheresScene::Lerp(float v0, float v1, float t)\n{\n\treturn (1.0f - t) * v0 + t * v1;\n}"
  },
  {
    "path": "tests/graphics_benchmark/spheres_scene.hpp",
    "content": "#pragma once\n\n#include \"scene.hpp\"\n\nclass SpheresScene : public Scene\n{\npublic:\n\tSpheresScene();\n\n\tvoid Update() final;\n\nprotected:\n\tvoid LoadResources() final;\n\tvoid BuildScene(unsigned int width, unsigned int height) final;\n\nprivate:\n\tfloat Lerp(float v0, float v1, float t);\n\n\t// Models\n\twr::Model* m_sphere_model;\n\n\t// Textures\n\twr::TextureHandle m_skybox;\n\twr::TextureHandle m_flat_normal;\n};"
  },
  {
    "path": "wisp.version",
    "content": "\"1.3.1\"\n"
  }
]