Repository: TeamWisp/WispRenderer Branch: master Commit: 1f1493f9c903 Files: 279 Total size: 3.9 MB Directory structure: gitextract_vk1cvjis/ ├── .github/ │ ├── ISSUE_TEMPLATE/ │ │ ├── bug_report.md │ │ ├── design-change.md │ │ ├── feature_request.md │ │ └── re-factor-request.md │ └── PULL_REQUEST_TEMPLATE/ │ └── pull_request_template.md ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── CODE_OF_CONDUCT.md ├── Jenkinsfile ├── LICENSE ├── README.md ├── imgui.ini ├── resources/ │ ├── alien_lights.json │ ├── shaders/ │ │ ├── deferred_composition_pass.hlsl │ │ ├── deferred_geometry_pass.hlsl │ │ ├── denoising_SVGF.hlsl │ │ ├── denoising_reflections.hlsl │ │ ├── denoising_spatial_reconstruction.hlsl │ │ ├── dxr_ambient_occlusion.hlsl │ │ ├── dxr_functions.hlsl │ │ ├── dxr_global.hlsl │ │ ├── dxr_pathtracer_accumulation.hlsl │ │ ├── dxr_pathtracer_entries.hlsl │ │ ├── dxr_pathtracer_functions.hlsl │ │ ├── dxr_pathtracer_main.hlsl │ │ ├── dxr_raytracing.hlsl │ │ ├── dxr_reflection_entries.hlsl │ │ ├── dxr_reflection_functions.hlsl │ │ ├── dxr_reflection_main.hlsl │ │ ├── dxr_shadow_entries.hlsl │ │ ├── dxr_shadow_functions.hlsl │ │ ├── dxr_shadow_main.hlsl │ │ ├── dxr_structs.hlsl │ │ ├── dxr_texture_lod.hlsl │ │ ├── fullscreen_quad.hlsl │ │ ├── generate_mips_cs.hlsl │ │ ├── lighting.hlsl │ │ ├── material_util.hlsl │ │ ├── math.hlsl │ │ ├── pbr_brdf_lut.hlsl │ │ ├── pbr_cubemap_conversion.hlsl │ │ ├── pbr_cubemap_convolution.hlsl │ │ ├── pbr_prefilter_env_map.hlsl │ │ ├── pbr_util.hlsl │ │ ├── pp_bloom_blur.hlsl │ │ ├── pp_bloom_blur_horizontal.hlsl │ │ ├── pp_bloom_blur_vertical.hlsl │ │ ├── pp_bloom_composition.hlsl │ │ ├── pp_bloom_extract_bright.hlsl │ │ ├── pp_bloom_util.hlsl │ │ ├── pp_dof_bokeh.hlsl │ │ ├── pp_dof_bokeh_post_filter.hlsl │ │ ├── pp_dof_coc.hlsl │ │ ├── pp_dof_composition.hlsl │ │ ├── pp_dof_compute_near_mask.hlsl │ │ ├── pp_dof_dilate.hlsl │ │ ├── pp_dof_downscale.hlsl │ │ ├── pp_dof_properties.hlsl │ │ ├── pp_dof_util.hlsl │ │ ├── pp_fxaa.hlsl │ │ ├── pp_hdr_util.hlsl │ │ ├── pp_tonemapping.hlsl │ │ ├── pp_util.hlsl │ │ └── rand_util.hlsl │ ├── sponza_lights.json │ └── viknell_lights.json ├── scripts/ │ └── JenkinsWebhook.bat ├── src/ │ ├── constant_buffer_pool.cpp │ ├── constant_buffer_pool.hpp │ ├── d3d12/ │ │ ├── d3d12_acceleration_structure..cpp │ │ ├── d3d12_command_list.cpp │ │ ├── d3d12_command_queue.cpp │ │ ├── d3d12_constant_buffer_pool.cpp │ │ ├── d3d12_constant_buffer_pool.hpp │ │ ├── d3d12_defines.hpp │ │ ├── d3d12_descriptor_heap.cpp │ │ ├── d3d12_descriptors_allocations.cpp │ │ ├── d3d12_descriptors_allocations.hpp │ │ ├── d3d12_device.cpp │ │ ├── d3d12_dynamic_descriptor_heap.cpp │ │ ├── d3d12_dynamic_descriptor_heap.hpp │ │ ├── d3d12_enums.hpp │ │ ├── d3d12_fence.cpp │ │ ├── d3d12_functions.hpp │ │ ├── d3d12_heap.cpp │ │ ├── d3d12_indirect_command_buffer.cpp │ │ ├── d3d12_material_pool.cpp │ │ ├── d3d12_material_pool.hpp │ │ ├── d3d12_model_pool.cpp │ │ ├── d3d12_model_pool.hpp │ │ ├── d3d12_pipeline_state.cpp │ │ ├── d3d12_readback_buffer.cpp │ │ ├── d3d12_render_target.cpp │ │ ├── d3d12_render_window.cpp │ │ ├── d3d12_renderer.cpp │ │ ├── d3d12_renderer.hpp │ │ ├── d3d12_resource_pool_texture.cpp │ │ ├── d3d12_resource_pool_texture.hpp │ │ ├── d3d12_root_signature.cpp │ │ ├── d3d12_rt_descriptor_heap.cpp │ │ ├── d3d12_rt_descriptor_heap.hpp │ │ ├── d3d12_settings.hpp │ │ ├── d3d12_shader.cpp │ │ ├── d3d12_shader_table.cpp │ │ ├── d3d12_staging_buffer.cpp │ │ ├── d3d12_state_object.cpp │ │ ├── d3d12_structs.hpp │ │ ├── d3d12_structured_buffer.cpp │ │ ├── d3d12_structured_buffer_pool.cpp │ │ ├── d3d12_structured_buffer_pool.hpp │ │ ├── d3d12_texture_resources.hpp │ │ ├── d3d12_textures.cpp │ │ ├── d3d12_viewport.cpp │ │ └── d3dx12.hpp │ ├── engine_registry.cpp │ ├── engine_registry.hpp │ ├── entry.hpp │ ├── frame_graph/ │ │ └── frame_graph.hpp │ ├── id_factory.cpp │ ├── id_factory.hpp │ ├── imgui/ │ │ ├── ImGuizmo.cpp │ │ ├── ImGuizmo.h │ │ ├── imconfig.hpp │ │ ├── imgui.cpp │ │ ├── imgui.hpp │ │ ├── imgui_draw.cpp │ │ ├── imgui_impl_dx12.cpp │ │ ├── imgui_impl_dx12.hpp │ │ ├── imgui_impl_win32.cpp │ │ ├── imgui_impl_win32.hpp │ │ ├── imgui_internal.hpp │ │ ├── imgui_widgets.cpp │ │ ├── imstb_rectpack.hpp │ │ ├── imstb_textedit.hpp │ │ └── imstb_truetype.hpp │ ├── imgui_graphics_settings.hpp │ ├── imgui_tools.cpp │ ├── imgui_tools.hpp │ ├── material_pool.cpp │ ├── material_pool.hpp │ ├── model_loader.cpp │ ├── model_loader.hpp │ ├── model_loader_assimp.cpp │ ├── model_loader_assimp.hpp │ ├── model_loader_tinygltf.cpp │ ├── model_loader_tinygltf.hpp │ ├── model_pool.cpp │ ├── model_pool.hpp │ ├── pipeline_registry.cpp │ ├── pipeline_registry.hpp │ ├── platform_independend_structs.hpp │ ├── registry.hpp │ ├── render_tasks/ │ │ ├── d3d12_accumulation.hpp │ │ ├── d3d12_ansel.hpp │ │ ├── d3d12_bloom_composition.hpp │ │ ├── d3d12_bloom_extract_bright.hpp │ │ ├── d3d12_bloom_horizontal_blur.hpp │ │ ├── d3d12_bloom_vertical_blur.hpp │ │ ├── d3d12_brdf_lut_precalculation.hpp │ │ ├── d3d12_build_acceleration_structures.hpp │ │ ├── d3d12_cubemap_convolution.hpp │ │ ├── d3d12_deferred_composition.cpp │ │ ├── d3d12_deferred_composition.hpp │ │ ├── d3d12_deferred_main.hpp │ │ ├── d3d12_deferred_render_target_copy.hpp │ │ ├── d3d12_dof_bokeh.hpp │ │ ├── d3d12_dof_bokeh_postfilter.hpp │ │ ├── d3d12_dof_coc.hpp │ │ ├── d3d12_dof_composition.hpp │ │ ├── d3d12_dof_compute_near_mask.hpp │ │ ├── d3d12_dof_dilate_flatten.hpp │ │ ├── d3d12_dof_dilate_flatten_second_pass.hpp │ │ ├── d3d12_dof_dilate_near.hpp │ │ ├── d3d12_down_scale.hpp │ │ ├── d3d12_equirect_to_cubemap.hpp │ │ ├── d3d12_hbao.hpp │ │ ├── d3d12_imgui_render_task.hpp │ │ ├── d3d12_path_tracer.hpp │ │ ├── d3d12_post_processing.hpp │ │ ├── d3d12_raytracing_task.hpp │ │ ├── d3d12_reflection_denoiser.hpp │ │ ├── d3d12_rt_hybrid_helpers.hpp │ │ ├── d3d12_rt_reflection_task.hpp │ │ ├── d3d12_rt_shadow_task.hpp │ │ ├── d3d12_rtao_task.hpp │ │ ├── d3d12_shadow_denoiser_task.hpp │ │ └── d3d12_spatial_reconstruction.hpp │ ├── renderer.cpp │ ├── renderer.hpp │ ├── resource_pool_texture.cpp │ ├── resource_pool_texture.hpp │ ├── root_signature_registry.cpp │ ├── root_signature_registry.hpp │ ├── rt_pipeline_registry.cpp │ ├── rt_pipeline_registry.hpp │ ├── scene_graph/ │ │ ├── camera_node.cpp │ │ ├── camera_node.hpp │ │ ├── light_node.cpp │ │ ├── light_node.hpp │ │ ├── mesh_node.cpp │ │ ├── mesh_node.hpp │ │ ├── node.cpp │ │ ├── node.hpp │ │ ├── scene_graph.cpp │ │ ├── scene_graph.hpp │ │ ├── skybox_node.cpp │ │ └── skybox_node.hpp │ ├── settings.hpp │ ├── shader_registry.cpp │ ├── shader_registry.hpp │ ├── structs.hpp │ ├── structured_buffer_pool.cpp │ ├── structured_buffer_pool.hpp │ ├── util/ │ │ ├── aabb.cpp │ │ ├── aabb.hpp │ │ ├── bitmap_allocator.hpp │ │ ├── defines.hpp │ │ ├── delegate.hpp │ │ ├── file_watcher.cpp │ │ ├── file_watcher.hpp │ │ ├── log.cpp │ │ ├── log.hpp │ │ ├── logfile_handler.cpp │ │ ├── logfile_handler.hpp │ │ ├── named_type.hpp │ │ ├── pair_hash.hpp │ │ ├── strings.hpp │ │ ├── thread_pool.hpp │ │ └── user_literals.hpp │ ├── version.hpp │ ├── vertex.hpp │ ├── window.cpp │ ├── window.hpp │ ├── wisp.hpp │ └── wisprenderer_export.hpp ├── tests/ │ ├── CMakeLists.txt │ ├── common/ │ │ ├── scene.cpp │ │ └── scene.hpp │ ├── demo/ │ │ ├── debug_camera.hpp │ │ ├── demo.cpp │ │ ├── demo_frame_graphs.hpp │ │ ├── engine_interface.hpp │ │ ├── physics_engine.cpp │ │ ├── physics_engine.hpp │ │ ├── physics_node.cpp │ │ ├── physics_node.hpp │ │ ├── scene_alien.cpp │ │ ├── scene_alien.hpp │ │ ├── scene_emibl.cpp │ │ ├── scene_emibl.hpp │ │ ├── scene_sponza.cpp │ │ ├── scene_sponza.hpp │ │ ├── scene_viknell.cpp │ │ ├── scene_viknell.hpp │ │ ├── spline_library/ │ │ │ ├── LICENSE │ │ │ ├── spline.h │ │ │ ├── splines/ │ │ │ │ ├── cubic_hermite_spline.h │ │ │ │ ├── generic_b_spline.h │ │ │ │ ├── natural_spline.h │ │ │ │ ├── quintic_hermite_spline.h │ │ │ │ ├── uniform_cr_spline.h │ │ │ │ └── uniform_cubic_bspline.h │ │ │ ├── utils/ │ │ │ │ ├── arclength.h │ │ │ │ ├── calculus.h │ │ │ │ ├── linearalgebra.h │ │ │ │ ├── nanoflann.hpp │ │ │ │ ├── spline_common.h │ │ │ │ ├── splineinverter.h │ │ │ │ └── splinesample_adaptor.h │ │ │ └── vector.h │ │ ├── spline_node.cpp │ │ └── spline_node.hpp │ └── graphics_benchmark/ │ ├── frame_graphs.cpp │ ├── frame_graphs.hpp │ ├── graphics_benchmark.cpp │ ├── spheres_scene.cpp │ └── spheres_scene.hpp └── wisp.version ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/ISSUE_TEMPLATE/bug_report.md ================================================ --- name: Bug report about: Create a report to help us improve title: '' labels: bug assignees: '' --- **Describe the bug** A clear and concise description of what the bug is. **To Reproduce** Steps to reproduce the behaviour: 1. Go to '...' 2. Click on '....' 3. Scroll down to '....' 4. See error **Expected behaviour** A clear and concise description of what you expected to happen. **Screenshots** If applicable, add screenshots to help explain your problem. **Desktop (please complete the following information):** - OS: [e.g. iOS] - Version [e.g. 22] - Hardware [e.g. CPU:6700k, GPU:RTX 2080ti, RAM:1GB] **Additional context** Add any other context about the problem here. ================================================ FILE: .github/ISSUE_TEMPLATE/design-change.md ================================================ --- name: Design change about: Suggest a design change for the project title: '' labels: design change assignees: '' --- **Is your design request related to a problem? Please describe.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] **Describe the solution you'd like** A clear and concise description of what you want to happen. **Describe alternatives you've considered** A clear and concise description of any alternative solutions or designs you've considered. **Additional context** Add any other context or screenshots about the request here. ================================================ FILE: .github/ISSUE_TEMPLATE/feature_request.md ================================================ --- name: Feature request about: Suggest an idea for this project title: '' labels: '' assignees: '' --- **Is your feature request related to a problem? Please describe.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] **Describe the solution you'd like** A clear and concise description of what you want to happen. **Describe alternatives you've considered** A clear and concise description of any alternative solutions or features you've considered. **Additional context** Add any other context or screenshots about the feature request here. ================================================ FILE: .github/ISSUE_TEMPLATE/re-factor-request.md ================================================ --- name: Re-factor request about: Suggest a re-factor of code for the project title: '' labels: code change assignees: '' --- **Is your request related to a problem? Please describe.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] **Describe the solution you'd like** A clear and concise description of what you want to happen. **Describe alternatives you've considered** A clear and concise description of any alternative solutions you've considered. **Additional context** Add any other context or screenshots about the request here. ================================================ FILE: .github/PULL_REQUEST_TEMPLATE/pull_request_template.md ================================================ **Description** Give a clear description of the changes **Issues** reference related issues if any **Possible conflicts** list any possible conflicts ================================================ FILE: .gitignore ================================================ /build/ /build_vs2017_arm/ /build_vs2017_win64/ /build_vs2019_win64/ /build_vs2019_win32/ /build_vs2017_win32/ *.swp *.swo /cmake-build-debug/ /.idea/ perf_framerate.txt *.dll *.lib /resources/models /resources/materials /deps/ansel/ /deps/hbao+/ /benchmark_images/ /CrashPadDB/ /logs/ ================================================ FILE: .gitmodules ================================================ [submodule "deps/fmt"] path = deps/fmt url = https://github.com/fmtlib/fmt.git [submodule "deps/DirectXTex"] path = deps/DirectXTex url = https://github.com/TeamWisp/DirectXTex.git branch = master [submodule "deps/assimp"] path = deps/assimp url = https://github.com/assimp/assimp [submodule "deps/fallback"] path = deps/fallback url = https://github.com/TeamWisp/DXR-Fallback-Layer.git [submodule "deps/Wisp-LFS"] path = deps/Wisp-LFS url = https://github.com/TeamWisp/Wisp-LFS.git [submodule "deps/crashpad"] path = deps/crashpad url = https://github.com/TeamWisp/Crashpad.git [submodule "deps/tinygltf"] path = deps/tinygltf url = https://github.com/syoyo/tinygltf [submodule "deps/bullet3"] path = deps/bullet3 url = https://github.com/TeamWisp/bullet3 ================================================ FILE: CMakeLists.txt ================================================ cmake_minimum_required(VERSION 3.13) set_property(GLOBAL PROPERTY USE_FOLDERS ON) add_compile_definitions(_ENABLE_EXTENDED_ALIGNED_STORAGE ) project(Wisp) option(WISP_BUILD_TESTS "Enable Wisp Tests" ON) option(WISP_LOG_TO_STDOUT "Allow printing to stdout" ON) option(WISP_BUILD_SHARED "Build Wisp as a shared library" OFF) if (WISP_BUILD_SHARED) set(BUILD_SHARED_LIBS ON) message(STATUS "Building wisp as a SHARED library") else() add_definitions(-DWISPRENDERER_STATIC_DEFINE) message(STATUS "Building wisp as a STATIC library") set(BUILD_SHARED_LIBS OFF) endif() if (WISP_LOG_TO_STDOUT) message(STATUS "Wisp Renderer is allowed to print to stdout") add_definitions(-DWISPRENDERER_LOG_TO_STDOUT) endif() #Detect whether we have HBAO+ SDK available. if(EXISTS ${CMAKE_SOURCE_DIR}/deps/hbao+) message(STATUS "Found NVIDIA Gameworks HBAO+") set(NVIDIA_GAMEWORKS_HBAO_LIB ${CMAKE_SOURCE_DIR}/GFSDK_SSAO_D3D12.win64.lib) add_definitions(-DNVIDIA_GAMEWORKS_HBAO) configure_file(${CMAKE_SOURCE_DIR}/deps/hbao+/lib/GFSDK_SSAO_D3D12.win64.dll ${CMAKE_SOURCE_DIR} COPYONLY) configure_file(${CMAKE_SOURCE_DIR}/deps/hbao+/lib/GFSDK_SSAO_D3D12.win64.lib ${CMAKE_SOURCE_DIR} COPYONLY) else() message(STATUS "Failed to find NVIDIA Gameworks HBAO+") endif() #Detect whether we have the AnselSDK available. if(EXISTS ${CMAKE_SOURCE_DIR}/deps/ansel) message(STATUS "Found NVIDIA Gameworks Ansel") set(NVIDIA_GAMEWORKS_ANSEL_LIB ${CMAKE_SOURCE_DIR}/AnselSDK64.lib) add_definitions(-DNVIDIA_GAMEWORKS_ANSEL) configure_file(${CMAKE_SOURCE_DIR}/deps/ansel/lib/AnselSDK64.lib ${CMAKE_SOURCE_DIR} COPYONLY) configure_file(${CMAKE_SOURCE_DIR}/deps/ansel/redist/AnselSDK64.dll ${CMAKE_SOURCE_DIR} COPYONLY) else() message(STATUS "Failed to find NVIDIA Gameworks Ansel") endif() ##### OUTPUT DIRECTORIES ##### set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) configure_file(${CMAKE_SOURCE_DIR}/deps/fallback/Bin/dxrfallbackcompiler.dll ${CMAKE_SOURCE_DIR} COPYONLY) configure_file(${CMAKE_SOURCE_DIR}/deps/fallback/Bin/dxil.dll ${CMAKE_SOURCE_DIR} COPYONLY) configure_file(${CMAKE_SOURCE_DIR}/deps/fallback/Bin/dxcompiler.dll ${CMAKE_SOURCE_DIR} COPYONLY) ##### SOURCES ##### file(GLOB HEADERS CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp") file(GLOB SOURCES CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp") file(GLOB FG_HEADERS CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/frame_graph/*.hpp") file(GLOB FG_SOURCES CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/frame_graph/*.cpp") file(GLOB SG_HEADERS CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/scene_graph/*.hpp") file(GLOB SG_SOURCES CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/scene_graph/*.cpp") file(GLOB RT_HEADERS CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/render_tasks/*.hpp") file(GLOB RT_SOURCES CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/render_tasks/*.cpp") file(GLOB D3D12_HEADERS CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/d3d12/*.hpp") file(GLOB D3D12_SOURCES CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/d3d12/*.cpp") file(GLOB UTIL_HEADERS CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/util/*.hpp") file(GLOB UTIL_SOURCES CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/util/*.cpp") file(GLOB IMGUI_HEADERS CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/imgui/*.hpp") file(GLOB IMGUI_SOURCES CONFIGURE_DEPENDS] "${CMAKE_CURRENT_SOURCE_DIR}/src/imgui/*.cpp") source_group("High Level API" FILES ${SOURCES} ${HEADERS}) source_group("Frame Graph" FILES ${FG_SOURCES} ${FG_HEADERS}) source_group("Scene Graph" FILES ${SG_SOURCES} ${SG_HEADERS}) source_group("Render Tasks" FILES ${RT_SOURCES} ${RT_HEADERS}) source_group("D3D12" FILES ${D3D12_SOURCES} ${D3D12_HEADERS}) source_group("Utility" FILES ${UTIL_SOURCES} ${UTIL_HEADERS}) source_group("ImGui" FILES ${IMGUI_SOURCES} ${IMGUI_HEADERS}) set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT") # Bullet Options set(BUILD_CPU_DEMOSON OFF CACHE BOOL "" FORCE) set(USE_GRAPHICAL_BENCHMARKON OFF CACHE BOOL "" FORCE) set(BUILD_ENETON OFF CACHE BOOL "" FORCE) set(BUILD_CLSOCKETON OFF CACHE BOOL "" FORCE) set(BUILD_BULLET2_DEMOSON OFF CACHE BOOL "" FORCE) set(BUILD_EXTRASON OFF CACHE BOOL "" FORCE) set(BUILD_UNIT_TESTSON OFF CACHE BOOL "" FORCE) set(USE_GRAPHICAL_BENCHMARKON OFF CACHE BOOL "" FORCE) set(BUILD_STATIC_LIBON ON CACHE BOOL "" FORCE) set(NO_EXPORTON ON CACHE BOOL "" FORCE) ## dependencies ## add_subdirectory(${CMAKE_SOURCE_DIR}/deps/fmt ${CMAKE_BINARY_DIR}/fmt) add_subdirectory(${CMAKE_SOURCE_DIR}/deps/DirectXTex ${CMAKE_BINARY_DIR}/DirectXTex) add_subdirectory(${CMAKE_SOURCE_DIR}/deps/fallback ${CMAKE_BINARY_DIR}/fallback) add_subdirectory(${CMAKE_SOURCE_DIR}/deps/bullet3 ${CMAKE_BINARY_DIR}/bullet3) set(ASSIMP_BUILD_SHARED_LIBS OFF) set(ASSIMP_NO_EXPORT ON CACHE BOOL "" FORCE) set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "" FORCE) set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF) set(ASSIMP_BUILD_ALL_EXPORTERS_BY_DEFAULT OFF) #IMPORTERS set( ASSIMP_BUILD_AMF_IMPORTER OFF ) set( ASSIMP_BUILD_3DS_IMPORTER OFF ) set( ASSIMP_BUILD_AC_IMPORTER OFF ) set( ASSIMP_BUILD_ASE_IMPORTER OFF ) set( ASSIMP_BUILD_ASSBIN_IMPORTER OFF ) set( ASSIMP_BUILD_ASSXML_IMPORTER OFF ) set( ASSIMP_BUILD_B3D_IMPORTER OFF ) set( ASSIMP_BUILD_BVH_IMPORTER OFF ) set( ASSIMP_BUILD_COLLADA_IMPORTER OFF ) set( ASSIMP_BUILD_DXF_IMPORTER OFF ) set( ASSIMP_BUILD_CSM_IMPORTER OFF ) set( ASSIMP_BUILD_HMP_IMPORTER OFF ) set( ASSIMP_BUILD_IRRMESH_IMPORTER OFF ) set( ASSIMP_BUILD_IRR_IMPORTER OFF ) set( ASSIMP_BUILD_LWO_IMPORTER OFF ) set( ASSIMP_BUILD_LWS_IMPORTER OFF ) set( ASSIMP_BUILD_MD2_IMPORTER OFF ) set( ASSIMP_BUILD_MD3_IMPORTER OFF ) set( ASSIMP_BUILD_MD5_IMPORTER OFF ) set( ASSIMP_BUILD_MDC_IMPORTER OFF ) set( ASSIMP_BUILD_MDL_IMPORTER OFF ) set( ASSIMP_BUILD_NFF_IMPORTER OFF ) set( ASSIMP_BUILD_NDO_IMPORTER OFF ) set( ASSIMP_BUILD_OFF_IMPORTER OFF ) set( ASSIMMP_BUILD_OBJ_IMPORTER ON ) #ON set( ASSIMP_BUILD_OGRE_IMPORTER OFF ) set( ASSIMP_BUILD_OPENGEX_IMPORTER OFF ) set( ASSIMP_BUILD_PLY_IMPORTER OFF ) set( ASSIMP_BUILD_MS3D_IMPORTER OFF ) set( ASSIMP_BUILD_COB_IMPORTER OFF ) set( ASSIMP_BUILD_BLEND_IMPORTER OFF ) set( ASSIMP_BUILD_IFC_IMPORTER OFF ) set( ASSIMP_BUILD_XGL_IMPORTER OFF ) set( ASSIMMP_BUILD_FBX_IMPORTER ON ) #ON set( ASSIMP_BUILD_Q3D_IMPORTER OFF ) set( ASSIMP_BUILD_Q3BSP_IMPORTER OFF ) set( ASSIMP_BUILD_RAW_IMPORTER ON ) #ON set( ASSIMP_BUILD_SIB_IMPORTER ON ) #ON set( ASSIMP_BUILD_SMD_IMPORTER ON ) #ON set( ASSIMP_BUILD_STL_IMPORTER OFF ) set( ASSIMP_BUILD_TERRAGEN_IMPORTER OFF ) set( ASSIMP_BUILD_3D_IMPORTER OFF ) set( ASSIMP_BUILD_X_IMPORTER OFF ) set( ASSIMP_BUILD_X3D_IMPORTER OFF ) set( ASSIMP_BUILD_GLTF_IMPORTER ON ) #ON set( ASSIMP_BUILD_3MF_IMPORTER OFF ) set( ASSIMP_BUILD_MMD_IMPORTER OFF ) # END IMPORTERS #set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT CACHE INTERNAL FALSE) #set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF) add_subdirectory(${CMAKE_SOURCE_DIR}/deps/assimp ${CMAKE_BINARY_DIR}/assimp) ## dependencies options ## set(FMT_TEST OFF) set(FMT_DOC OFF) set(FMT_PEDANTIC OFF) set(FMT_WERROR OFF) ## dependencies sorting ## set_target_properties (fmt PROPERTIES FOLDER ThirdParty) set_target_properties (DirectXTex PROPERTIES FOLDER ThirdParty) if (MSVC) target_compile_options(fmt PRIVATE /W0) target_compile_options(DirectXTex PRIVATE /W0) endif() set_target_properties(Bullet3Common PROPERTIES FOLDER "ThirdParty/Bullet") set_target_properties(BulletCollision PROPERTIES FOLDER "ThirdParty/Bullet") set_target_properties(BulletDynamics PROPERTIES FOLDER "ThirdParty/Bullet") set_target_properties(BulletInverseDynamics PROPERTIES FOLDER "ThirdParty/Bullet") set_target_properties(BulletSoftBody PROPERTIES FOLDER "ThirdParty/Bullet") set_target_properties(LinearMath PROPERTIES FOLDER "ThirdParty/Bullet") set_target_properties(assimp PROPERTIES FOLDER ThirdParty) set_target_properties(assimp_cmd PROPERTIES FOLDER ThirdParty) set_target_properties(IrrXML PROPERTIES FOLDER ThirdParty) set_target_properties(UpdateAssimpLibsDebugSymbolsAndDLLs PROPERTIES FOLDER ThirdParty) set_target_properties(zlib PROPERTIES FOLDER ThirdParty) set_target_properties(zlibstatic PROPERTIES FOLDER ThirdParty) #set_target_properties(unit PROPERTIES FOLDER ThirdParty) set_target_properties(uninstall PROPERTIES FOLDER ThirdParty) if(WISP_BUILD_SHARED) set(WISP_LIB_TYPE SHARED) else() set(WISP_LIB_TYPE STATIC) endif() add_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}) set_target_properties(WispRenderer PROPERTIES CXX_STANDARD 20) set_target_properties(WispRenderer PROPERTIES CXX_EXTENSIONS OFF) set_target_properties(WispRenderer PROPERTIES CMAKE_CXX_STANDARD_REQUIRED ON) if (MSVC) target_compile_options(WispRenderer PRIVATE /W4 /permissive- /MP /Gm-) endif() target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/DirectXTex/DirectXTex/) target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/fmt/include) target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/tinygltf) target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/assimp/include) target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/fallback/Include) target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/hbao+/include) target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/ansel/include) target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/crashpad) target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/crashpad/third_party/mini_chromium/mini_chromium) target_include_directories(WispRenderer PUBLIC ${CMAKE_SOURCE_DIR}/deps/bullet3/src) link_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/) target_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) set_target_properties(WispRenderer PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/../") if (WISP_BUILD_TESTS) add_subdirectory(tests ${CMAKE_BINARY_DIR}/tests) set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT Demo) endif() ================================================ FILE: CODE_OF_CONDUCT.md ================================================ # Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: * Using welcoming and inclusive language * Being respectful of differing viewpoints and experiences * Gracefully accepting constructive criticism * Focusing on what is best for the community * Showing empathy towards other community members Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery and unwelcome sexual attention or advances * Trolling, insulting/derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or electronic address, without explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at viktorzoutman@vzout.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html [homepage]: https://www.contributor-covenant.org For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq ================================================ FILE: Jenkinsfile ================================================ pipeline { agent any stages { stage('Install'){ steps{ echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}" bat '''cd "%WORKSPACE%\\Scripts" call JenkinsWebhook.bat ":bulb: Building: %JOB_NAME%. Jenkins build nr: %BUILD_NUMBER%" cd "%WORKSPACE% install -remote "%WORKSPACE%" if errorlevel 1 ( cd "%WORKSPACE%\\Scripts" JenkinsWebhook ":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - install failed" EXIT 1 )''' } } stage('Build') { steps { /*bat ''' cd "%WORKSPACE%" cmake --build ./build_vs2017_win32 if errorlevel 1 ( cd "%WORKSPACE%\\Scripts" JenkinsWebhook ":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 32bit build failed" EXIT 1 ) '''*/ bat''' cd "%WORKSPACE%" cmake --build ./build_vs2017_win64 if errorlevel 1 ( cd "%WORKSPACE%\\Scripts" JenkinsWebhook ":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 64bit-debug build failed" EXIT 1 ) ''' bat''' cd "%WORKSPACE%" cmake --build ./build_vs2017_win64 --config Release if errorlevel 1 ( cd "%WORKSPACE%\\Scripts" JenkinsWebhook ":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 64bit-release build failed" EXIT 1 ) ''' } } stage('test'){ steps{ script{ def has_failed = false try{ bat''' cd "%WORKSPACE%" cd build_vs2017_win64/bin/debug WispTest.exe if errorlevel 1 ( EXIT 1 ) ''' } catch( exc ){ has_failed = true bat''' cd "%WORKSPACE%\\Scripts" JenkinsWebhook ":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 64bit-debug Tests failed" ''' } try{ bat''' cd "%WORKSPACE%" cd build_vs2017_win64/bin/release WispTest.exe if errorlevel 1 ( EXIT 1 ) ''' } catch( exc ){ has_failed = true bat''' cd "%WORKSPACE%\\Scripts" JenkinsWebhook ":x: %JOB_NAME% Build Failed!! Jenskins build nr: %BUILD_NUMBER% - 64bit-release Tests failed" ''' } if(has_failed){ EXIT 1 } } } } stage('finalize'){ steps{ bat ''' rem mkdir builds rem move ./RayTracingLib/Debug "./builds/build_%BUILD_NUMBER%" cd "%WORKSPACE%\\scripts call "JenkinsWebhook.bat" ":white_check_mark: %JOB_NAME% Build Succesfull!! Jenkins build nr: %BUILD_NUMBER%" ''' } } } } ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. 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) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: README.md ================================================ # [ WispRenderer](https://teamwisp.github.io) - Real-Time Raytracing Renderer [![Release](https://img.shields.io/github/release/TeamWisp/WispRenderer.svg)](https://github.com/TeamWisp/WispRenderer/releases) [![Issues](https://img.shields.io/github/issues/TeamWisp/WispRenderer.svg)](https://github.com/TeamWisp/WispRenderer/issues) [![License](https://img.shields.io/badge/license-EPL%202.0-red.svg)](https://opensource.org/licenses/EPL-2.0) [![Discord](https://img.shields.io/discord/486967125504688128.svg?color=blueviolet&label=Discord)](https://discord.gg/Q3vDfqR) [![Twitter](https://img.shields.io/twitter/follow/wisprenderer.svg?style=social)](https://twitter.com/wisprenderer) ![](https://img.shields.io/github/stars/TeamWisp/WispRenderer.svg?style=social) ## [What is it?](https://teamwisp.github.io/product/) Wisp 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. **Features** * Physically Based Rendering * Ray Traced Global Illumination * Path Traced Global Illumination * Ray Traced Ambient Occlusion * Ray Traced Reflections * Ray Traced Shadows * Translucency & Transparency. * NVIDIA's HBAO+ * NVIDIA's AnselSDK **Supported Rendering Backends** * DirectX 12 **Supported Platforms** * Windows 10 (Version 1903) **Supported Compilers** * Visual Studio 2017 * Visual Studio 2019 ## [Installation](https://teamwisp.github.io/workspace_setup/) ### Installer ``` git clone https://github.com/TeamWisp/WispRenderer.git ``` Then 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. ### Manual ``` git clone https://github.com/TeamWisp/WispRenderer.git cd WispRenderer mkdir build cd build cmake -G "Visual Studio 16 2019" .. ``` Now 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/). # [Documentation](https://teamwisp.github.io/) # [Example](https://github.com/TeamWisp/WispRenderer/tree/master/demo) # [Getting Involved](https://teamwisp.github.io/) Want to help us out? That's definatly possible! Check out our [contribution page](https://teamwisp.github.io/) on how. # [Development Blog](https://teamwisp.github.io/WispBlog/) # [Discord](https://discord.gg/Q3vDfqR) Need help, want to get updates as soon as they happen or just want a chat? Join our [Discord Server](https://discord.gg/Q3vDfqR)! ## Trailer ## Media ## [License (Eclipse Public License version 2.0)](https://opensource.org/licenses/EPL-2.0) ``` Copyright 2018-2019 Breda University of Applied Sciences If a Contributor Distributes the Program in any form, then: 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 b) the Contributor may Distribute the Program under a license different than this Agreement, provided that such license: 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; 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; iii) does not attempt to limit or alter the recipients' rights in the Source Code under section 3.2; and iv) requires any subsequent distribution of the Program by any party to be under a license that satisfies the requirements of this section 3. When the Program is Distributed as Source Code: 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 b) a copy of this Agreement must be included with each copy of the Program. Contributors 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. ``` ================================================ FILE: imgui.ini ================================================ [Window][DockspaceViewport_11111111] Pos=0,21 Size=1280,699 Collapsed=0 [Window][Debug##Default] Pos=1022,401 Size=228,150 Collapsed=1 [Window][Theme] Pos=1032,376 Size=248,201 Collapsed=0 DockId=0x00000009,0 [Window][ImGui Details] Pos=1032,607 Size=248,113 Collapsed=0 [Window][Camera Settings] Pos=0,342 Size=270,378 Collapsed=0 DockId=0x00000008,0 [Window][Light Editor] Pos=0,21 Size=32,32 Collapsed=0 [Window][Shader Registry] Pos=340,105 Size=590,455 Collapsed=0 [Window][Pipeline Registry] Pos=317,134 Size=312,443 Collapsed=0 [Window][Root Signature Registry] Pos=748,338 Size=169,259 Collapsed=0 [Window][DirectX 12 Settings] Pos=1032,376 Size=248,201 Collapsed=0 DockId=0x00000009,2 [Window][Hardware Info] Pos=1032,376 Size=248,201 Collapsed=0 DockId=0x00000009,1 [Window][Model Editor] Pos=0,31 Size=32,32 Collapsed=0 [Window][Light Details] Pos=0,403 Size=220,129 Collapsed=0 [Window][Inspect] Pos=26,21 Size=32,32 Collapsed=0 [Window][Viewport] Pos=272,21 Size=758,699 Collapsed=0 DockId=0x00000002,0 [Window][Scene Graph Editor] Pos=0,21 Size=270,319 Collapsed=0 DockId=0x00000007,1 [Window][Inspector] Pos=1032,21 Size=248,353 Collapsed=0 DockId=0x00000006,0 [Window][DockSpaceViewport_11111111] Pos=0,21 Size=1280,699 Collapsed=0 [Window][Stats] Pos=280,121 Size=205,85 Collapsed=0 [Window][Test popup] ViewportPos=1503,395 ViewportId=0xB4FE7E97 Size=151,177 Collapsed=0 [Window][Emissive Multiplier] Pos=944,589 Size=303,134 Collapsed=0 [Window][RTAO Settings] Pos=1032,579 Size=248,141 Collapsed=0 DockId=0x0000000A,0 [Window][HBAO+ Settings] Pos=1032,579 Size=248,141 Collapsed=0 DockId=0x0000000A,0 [Window][NVIDIA Ansel Settings] Pos=1032,376 Size=248,201 Collapsed=0 DockId=0x00000009,3 [Window][Acceleration Structure Settings] Pos=1032,579 Size=248,141 Collapsed=0 DockId=0x0000000A,1 [Window][Shadow Settings] Pos=1032,579 Size=248,141 Collapsed=0 DockId=0x0000000A,2 [Window][Demo Scene] Pos=0,21 Size=270,319 Collapsed=0 DockId=0x00000007,0 [Docking][Data] DockSpace ID=0x8B93E3BD Pos=0,21 Size=1280,699 Split=X DockNode ID=0x00000003 Parent=0x8B93E3BD SizeRef=1030,699 Split=X DockNode ID=0x00000001 Parent=0x00000003 SizeRef=270,699 Split=Y SelectedTab=0xF4865B00 DockNode ID=0x00000007 Parent=0x00000001 SizeRef=219,319 SelectedTab=0xF4865B00 DockNode ID=0x00000008 Parent=0x00000001 SizeRef=219,378 SelectedTab=0x8722C6A1 DockNode ID=0x00000002 Parent=0x00000003 SizeRef=758,699 CentralNode=1 SelectedTab=0x995B0CF8 DockNode ID=0x00000004 Parent=0x8B93E3BD SizeRef=248,699 Split=Y SelectedTab=0x070A839D DockNode ID=0x00000006 Parent=0x00000004 SizeRef=248,353 SelectedTab=0xF02CD328 DockNode ID=0x00000005 Parent=0x00000004 SizeRef=248,344 Split=Y SelectedTab=0xDA48A090 DockNode ID=0x00000009 Parent=0x00000005 SizeRef=248,201 SelectedTab=0x4BB5AED3 DockNode ID=0x0000000A Parent=0x00000005 SizeRef=248,141 SelectedTab=0xDCA1C528 ================================================ FILE: resources/alien_lights.json ================================================ { "lights": [ { "angle": 69.0, "color": [ 0.0, 7.843137264251709, 11.764705657958984 ], "pos": [ 0.5994455218315125, 1.0072081089019775, 0.7158395648002625, 0.0 ], "radius": 200.0, "rot": [ -0.1899939924478531, 0.02998405694961548, 0.0, 0.0 ], "size": 0.0872664600610733, "type": 0 }, { "angle": 0.6981316804885864, "color": [ 1.0, 0.9999899864196777, 0.9999899864196777 ], "pos": [ 0.8884486556053162, 1.0663642883300781, 0.0, 0.0 ], "radius": 0.0, "rot": [ 0.0, 0.0, 0.0, 0.0 ], "size": 0.0, "type": 0 } ] } ================================================ FILE: resources/shaders/deferred_composition_pass.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DEFERRED_COMPOSITION_PASS_HLSL__ #define __DEFERRED_COMPOSITION_PASS_HLSL__ #define LIGHTS_REGISTER register(t4) #define MAX_REFLECTION_LOD 6 #include "fullscreen_quad.hlsl" #include "rand_util.hlsl" #include "pbr_util.hlsl" #include "lighting.hlsl" Texture2D gbuffer_albedo_roughness : register(t0); Texture2D gbuffer_normal_metallic : register(t1); Texture2D gbuffer_emissive_ao : register(t2); Texture2D gbuffer_depth : register(t3); //Consider SRV for light buffer in register t4 TextureCube skybox : register(t5); TextureCube irradiance_map : register(t6); TextureCube pref_env_map : register(t7); Texture2D brdf_lut : register(t8); Texture2D buffer_reflection : register(t9); //rgb: reflection, a : 1 / pdf Texture2D buffer_shadow : register(t10); //r: shadow factor Texture2D screen_space_irradiance : register(t11); Texture2D screen_space_ao : register(t12); RWTexture2D output : register(u0); SamplerState point_sampler : register(s0); SamplerState linear_sampler : register(s1); cbuffer CameraProperties : register(b0) { float4x4 view; float4x4 projection; float4x4 inv_projection; float4x4 inv_view; float4x4 prev_projection; float4x4 prev_view; uint is_hybrid; uint is_path_tracer; uint is_ao; uint has_shadows; float3 padding1; uint has_reflections; }; static uint min_depth = 0xFFFFFFFF; static uint max_depth = 0x0; float3 unpack_position(float2 uv, float depth, float4x4 proj_inv, float4x4 view_inv) { const float4 ndc = float4(uv * 2.0f - 1.f, depth, 1.0f); const float4 pos = mul( view_inv, mul(proj_inv, ndc)); return (pos / pos.w).xyz; } [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); // added offset of 0.5f to select proper pixel to sample from. // screen coords always get floored, therefore if the coords would be (1.9, 1.0), // it would sample (1.0, 1.0) instead of the intended (2.0, 1.0). Adding the offset solves this problem. float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f; float2 uv = screen_coord / screen_size; const float depth_f = gbuffer_depth.SampleLevel(point_sampler, uv, 0.0f).r; // View position and camera position float3 pos = unpack_position(float2(uv.x, 1.f - uv.y), depth_f, inv_projection, inv_view); float3 camera_pos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]); float3 V = normalize(camera_pos - pos); float3 retval; if(depth_f != 1.0f) { // GBuffer contents float4 albedo_roughness = gbuffer_albedo_roughness.SampleLevel(point_sampler, uv, 0.0f); float3 albedo = albedo_roughness.xyz; const float roughness = albedo_roughness.w; float4 normal_metallic = gbuffer_normal_metallic.SampleLevel(point_sampler, uv, 0.0f); float3 normal = normalize(normal_metallic.xyz); const float metallic = normal_metallic.w; float4 emissive_ao = gbuffer_emissive_ao.SampleLevel(point_sampler, uv, 0.0f); float3 emissive = emissive_ao.xyz; float gbuffer_ao = emissive_ao.w; float3 flipped_N = normal; flipped_N.y *= -1.0f; const float2 sampled_brdf = brdf_lut.SampleLevel(point_sampler, float2(max(dot(normal, V), 0.01f), roughness), 0).rg; float3 sampled_environment_map = pref_env_map.SampleLevel(linear_sampler, reflect(-V, normal), roughness * MAX_REFLECTION_LOD); // Get irradiance float3 irradiance = lerp( irradiance_map.SampleLevel(linear_sampler, flipped_N, 0.0f).xyz, screen_space_irradiance.SampleLevel(point_sampler, uv, 0.0f).xyz, is_path_tracer); // Get ao float ao = lerp( 1, screen_space_ao.SampleLevel(point_sampler, uv, 0.0f).xyz, // Lerp factor (0: env map, 1: path traced) is_ao); //Ao is multiplied with material texture ao, if present ao *= gbuffer_ao; // Get shadow factor (0: fully shadowed, 1: no shadow) float3 shadow_factor = lerp( // Do deferred shadow (fully lit for now) float3(1, 1, 1), // Shadow buffer if its hybrid rendering buffer_shadow.SampleLevel(linear_sampler, uv, 0.0f).rgb, // Lerp factor (0: no hybrid, 1: hybrid) has_shadows); // Get reflection float3 reflection = lerp( // Sample from environment if it IS NOT hybrid rendering sampled_environment_map, // Reflection buffer if it IS hybrid rendering buffer_reflection.SampleLevel(linear_sampler, uv, 0).xyz, // Lerp factor (0: no hybrid, 1: hybrid) has_reflections); // Shade pixel retval = shade_pixel(pos, V, albedo, metallic, roughness, emissive, normal, irradiance, ao, reflection, sampled_brdf, shadow_factor, has_shadows); } else { retval = skybox.SampleLevel(linear_sampler, -V, 0.0f); } //Temporary hackfix for NaN pixels if (isnan(retval).x == true) { retval = float3(0.0f, 0.0f, 0.0f); } //Do shading output[int2(dispatch_thread_id.xy)] = float4(retval, 1.f); } #endif //__DEFERRED_COMPOSITION_PASS_HLSL__ ================================================ FILE: resources/shaders/deferred_geometry_pass.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DEFERRED_GEOMETRY_PASS_HLSL__ #define __DEFERRED_GEOMETRY_PASS_HLSL__ //48 KiB; 48 * 1024 / sizeof(MeshNode) //48 * 1024 / (4 * 4 * 4) = 48 * 1024 / 64 = 48 * 16 = 768 #define MAX_INSTANCES 768 #include "material_util.hlsl" struct VS_INPUT { float3 pos : POSITION; float2 uv : TEXCOORD; float3 normal : NORMAL; float3 tangent : TANGENT; float3 bitangent : BITANGENT; }; struct VS_OUTPUT { float4 pos : SV_POSITION; #ifdef IS_HYBRID float4 prev_pos : PREV_POSITION; float4 curr_pos : CURR_POSITION; float4 world_pos : WORLD_POSITION; #endif float2 uv : TEXCOORD; float3 normal : NORMAL; float3 tangent : TANGENT; float3 bitangent : BITANGENT; #ifdef IS_HYBRID float3 obj_normal : OBJECT_NORMAL; float3 obj_tangent : OBJECT_TANGENT; float3 obj_bitangent : OBJECT_BITANGENT; #endif }; cbuffer CameraProperties : register(b0) { float4x4 view; float4x4 projection; float4x4 inv_projection; float4x4 inv_view; float4x4 prev_projection; float4x4 prev_view; uint is_hybrid; uint is_path_tracer; uint is_ao; uint has_shadows; float3 padding1; uint has_reflections; }; struct ObjectData { float4x4 model; float4x4 prev_model; }; cbuffer ObjectProperties : register(b1) { ObjectData instances[MAX_INSTANCES]; }; VS_OUTPUT main_vs(VS_INPUT input, uint instid : SV_InstanceId) { VS_OUTPUT output; float3 pos = input.pos; ObjectData inst = instances[instid]; //TODO: Use precalculated MVP or at least VP float4x4 vm = mul(view, inst.model); float4x4 mvp = mul(projection, vm); float4x4 prev_mvp = mul(prev_projection, mul(prev_view, inst.prev_model)); output.pos = mul(mvp, float4(pos, 1.0f)); #ifdef IS_HYBRID output.curr_pos = output.pos; output.prev_pos = mul(prev_mvp, float4(pos, 1.0f)); output.world_pos = mul(inst.model, float4(pos, 1.0f)); #endif output.uv = float2(input.uv.x, 1.0f - input.uv.y); output.tangent = normalize(mul(inst.model, float4(input.tangent, 0))).xyz; output.bitangent = normalize(mul(inst.model, float4(input.bitangent, 0))).xyz; output.normal = normalize(mul(inst.model, float4(input.normal, 0))).xyz; #ifdef IS_HYBRID output.obj_normal = input.normal.xyz; output.obj_tangent = input.tangent.xyz; output.obj_bitangent = input.bitangent.xyz; #endif return output; } struct PS_OUTPUT { float4 albedo_roughness : SV_TARGET0; float4 normal_metallic : SV_TARGET1; float4 emissive_ao : SV_TARGET2; #ifdef IS_HYBRID float4 velocity : SV_TARGET3; float4 depth : SV_TARGET4; float4 world_position : SV_TARGET5; #endif }; Texture2D material_albedo : register(t0); Texture2D material_normal : register(t1); Texture2D material_roughness : register(t2); Texture2D material_metallic : register(t3); Texture2D material_ao : register(t4); Texture2D material_emissive : register(t5); SamplerState s0 : register(s0); cbuffer MaterialProperties : register(b2) { MaterialData data; } uint dirToOct(float3 normal) { float2 p = normal.xy * (1.0 / dot(abs(normal), 1.0.xxx)); float2 e = normal.z > 0.0 ? p : (1.0 - abs(p.yx)) * (step(0.0,p)*2.0-(float2)(1.0)); return (asuint(f32tof16(e.y)) << 16) + (asuint(f32tof16(e.x))); } PS_OUTPUT main_ps(VS_OUTPUT input) : SV_TARGET { PS_OUTPUT output; float3x3 tbn = {input.tangent, input.bitangent, input.normal}; OutputMaterialData output_data = InterpretMaterialData(data, material_albedo, material_normal, material_roughness, material_metallic, material_emissive, material_ao, s0, input.uv); if (output_data.alpha <= 0.5f) { discard; } float3 normal = normalize(mul(output_data.normal, tbn)); output.albedo_roughness = float4(output_data.albedo.xyz, output_data.roughness); output.normal_metallic = float4(normal, output_data.metallic); output.emissive_ao = float4(output_data.emissive, output_data.ao); #ifdef IS_HYBRID float3x3 obj_tbn = {input.obj_tangent, input.obj_bitangent, input.obj_normal}; float3 obj_normal = normalize(mul(output_data.normal, obj_tbn)); float2 curr_pos = float2(input.curr_pos.xy / input.curr_pos.w) * 0.5 + 0.5; float2 prev_pos = float2(input.prev_pos.xy / input.prev_pos.w) * 0.5 + 0.5; const float epsilon = 1e-5; float2 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); output.velocity = float4(motion_vec.xy, length(fwidth(input.world_pos.xyz)), length(fwidth(normal))); float linear_z = input.pos.z * input.pos.w; float prev_z = input.prev_pos.z; float max_change_z = max(abs(ddx(linear_z)), abs(ddy(linear_z))); float compressed_obj_normal = asfloat(dirToOct(normalize(obj_normal))); output.depth = float4(linear_z, max_change_z, prev_z, compressed_obj_normal); output.world_position = input.world_pos; #endif return output; } #endif //__DEFERRED_GEOMETRY_PASS_HLSL__ ================================================ FILE: resources/shaders/denoising_SVGF.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DENOISING_SVGF_HLSL__ #define __DENOISING_SVGF_HLSL__ Texture2D input_texture : register(t0); Texture2D motion_texture : register(t1); Texture2D normal_texture : register(t2); Texture2D depth_texture : register(t3); Texture2D in_hist_length_texture : register(t4); Texture2D prev_input_texture : register(t5); Texture2D prev_moments_texture : register(t6); Texture2D prev_normal_texture : register(t7); Texture2D prev_depth_texture : register(t8); RWTexture2D out_color_texture : register(u0); RWTexture2D out_moments_texture : register(u1); RWTexture2D out_hist_length_texture : register(u2); SamplerState point_sampler : register(s0); SamplerState linear_sampler : register(s1); cbuffer DenoiserSettings : register(b0) { float blending_alpha; float blending_moments_alpha; float l_phi; float n_phi; float z_phi; float step_distance; float2 padding_2; }; const static float VARIANCE_CLIPPING_GAMMA = 8.0; float3 OctToDir(uint octo) { float2 e = float2( f16tof32(octo & 0xFFFF), f16tof32((octo>>16) & 0xFFFF) ); float3 v = float3(e, 1.0 - abs(e.x) - abs(e.y)); if (v.z < 0.0) v.xy = (1.0 - abs(v.yx)) * (step(0.0, v.xy)*2.0 - (float2)(1.0)); return normalize(v); } float Luminance(float3 color) { return (color.r + color.r + color.b + color.g + color.g + color.g) / 6.0; } uint DirToOct(float3 normal) { float2 p = normal.xy * (1.0 / dot(abs(normal), 1.0.xxx)); float2 e = normal.z > 0.0 ? p : (1.0 - abs(p.yx)) * (step(0.0, p)*2.0 - (float2)(1.0)); return (asuint(f32tof16(e.y)) << 16) + (asuint(f32tof16(e.x))); } void FetchNormalAndLinearZ(in int2 ipos, out float3 norm, out float2 zLinear) { norm = normal_texture[ipos]; zLinear = depth_texture[ipos].xy; } float NormalDistanceCos(float3 n1, float3 n2, float power) { return pow(max(0.0, dot(n1, n2)), 128.0); //return pow( saturate(dot(n1,n2)), power); //return 1.0f; } float NormalDistanceTan(float3 a, float3 b) { const float d = max(1e-8, dot(a, b)); return sqrt(max(0.0, 1.0 - d * d)) / d; } float CalcWeights( float depth_center, float depth_p, float phi_depth, float3 normal_center, float3 normal_p, float norm_power, float luminance_direct_center, float luminance_direct_p, float phi_direct) { const float w_normal = NormalDistanceCos(normal_center, normal_p, norm_power); const float w_z = (phi_depth == 0) ? 0.0f : abs(depth_center - depth_p) / phi_depth; const float w_l_direct = abs(luminance_direct_center - luminance_direct_p) / phi_direct; const float w_direct = exp(0.0 - max(w_l_direct, 0.0) - max(w_z, 0.0)) * w_normal; return w_direct; } float ComputeWeightNoLuminance(float depth_center, float depth_p, float phi_depth, float3 normal_center, float3 normal_p) { const float w_normal = NormalDistanceCos(normal_center, normal_p, n_phi); const float w_z = abs(depth_center - depth_p) / phi_depth; return exp(-max(w_z, 0.0)) * w_normal; } bool IsReprojectionValid(int2 coord, float z, float z_prev, float fwidth_z, float3 normal, float3 normal_prev, float fwidth_normal) { int2 screen_size = int2(0, 0); input_texture.GetDimensions(screen_size.x, screen_size.y); bool ret = (coord.x > -1 && coord.x < screen_size.x && coord.y > -1 && coord.y < screen_size.y); ret = ret && ((abs(z_prev - z) / (fwidth_z + 1e-4)) < 2.0); ret = ret && ((distance(normal, normal_prev) / (fwidth_normal + 1e-2)) < 16.0); return ret; } bool LoadPrevData(float2 screen_coord, out float4 prev_direct, out float2 prev_moments, out float history_length) { float2 screen_size = float2(0.f, 0.f); input_texture.GetDimensions(screen_size.x, screen_size.y); float2 uv = screen_coord / screen_size; float4 motion = motion_texture.SampleLevel(point_sampler, uv, 0); float2 q_uv = uv - motion.xy; float2 prev_coords = q_uv * screen_size; float4 depth = depth_texture[prev_coords]; float3 normal = OctToDir(asuint(depth.w)); prev_direct = float4(0, 0, 0, 0); prev_moments = float2(0, 0); bool v[4]; const float2 pos_prev = prev_coords; int2 offset[4] = {int2(0, 0), int2(1, 0), int2(0, 1), int2(1, 1)}; bool valid = false; [unroll] for(int sample_idx = 0; sample_idx < 4; ++sample_idx) { int2 loc = int2(pos_prev) + offset[sample_idx]; float4 depth_prev = prev_depth_texture[loc]; float3 normal_prev = OctToDir(asuint(depth_prev.w)); v[sample_idx] = IsReprojectionValid(loc, depth.z, depth_prev.x, depth.y, normal, normal_prev, motion.w); valid = valid || v[sample_idx]; } if(valid) { float sum_weights = 0; float x = frac(pos_prev.x); float y = frac(pos_prev.y); float weights[4] = {(1 - x) * (1 - y), x * (1 - y), (1 - x) * y, x * y }; [unroll] for(int sample_idx = 0; sample_idx < 4; ++sample_idx) { int2 loc = int2(pos_prev) + offset[sample_idx]; prev_direct += weights[sample_idx] * prev_input_texture[loc] * float(v[sample_idx]); prev_moments += weights[sample_idx] * prev_moments_texture[loc] * float(v[sample_idx]); sum_weights += weights[sample_idx] * float(v[sample_idx]); } valid = (sum_weights >= 0.01); prev_direct = lerp(float4(0, 0, 0, 0), prev_direct / sum_weights, valid); prev_moments = lerp(float2(0, 0), prev_moments / sum_weights, valid); } if(!valid) { float cnt = 0.0; const int radius = 1; for(int y = -radius; y <= radius; ++y) { for(int x = -radius; x <= radius; ++x) { int2 p = prev_coords + int2(x, y); float4 depth_filter = prev_depth_texture[p]; float3 normal_filter = OctToDir(asuint(depth_filter.w)); if(IsReprojectionValid(prev_coords, depth.z, depth_filter.x, depth.y, normal, normal_filter, motion.w)) { prev_direct += prev_input_texture[p]; prev_moments += prev_moments_texture[p]; cnt += 1.0; } } } valid = cnt > 0; prev_direct /= lerp(1, cnt, cnt > 0); prev_moments /= lerp(1, cnt, cnt > 0); } if(valid) { history_length = in_hist_length_texture[prev_coords].r; } else { prev_direct = float4(0, 0, 0, 0); prev_moments = float2(0, 0); history_length = 0; } return valid; } float ComputeVarianceCenter(int2 center) { float sum = 0.0; const float kernel[2][2] = { {1.0 / 4.0, 1.0 / 8.0}, {1.0 / 8.0, 1.0 / 16.0} }; const int radius = 1; [unroll] for(int y = -radius; y <= radius; ++y) { [unroll] for(int x = -radius; x <= radius; ++x) { int2 p = center + int2(x, y); float k = kernel[abs(x)][abs(y)]; sum += input_texture[p].w * k; } } return sum; } float4 LineBoxIntersection(float3 box_min, float3 box_max, float3 c_in, float3 c_hist) { float3 p_clip = 0.5 * (box_max + box_min); float3 e_clip = 0.5 * (box_max - box_min); float3 v_clip = c_hist - p_clip; float3 v_unit = v_clip.xyz / e_clip; float3 a_unit = abs(v_unit); float ma_unit = max(a_unit.x, max(a_unit.y, a_unit.z)); if(ma_unit > 1.0) { return float4((p_clip + v_clip / ma_unit).xyz, ma_unit); } else { return float4(c_hist.xyz, ma_unit); } } [numthreads(16,16,1)] void reprojection_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); out_color_texture.GetDimensions(screen_size.x, screen_size.y); float2 uv = float2(dispatch_thread_id.x / screen_size.x, dispatch_thread_id.y / screen_size.y); float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y); float4 direct = input_texture[screen_coord]; float4 prev_direct = float4(0.0, 0.0, 0.0, 0.0); float2 prev_moments = float2(0.0, 0.0); float history_length = 0.0; bool success = LoadPrevData(screen_coord, prev_direct, prev_moments, history_length); if(isnan(prev_direct.x) || isnan(prev_direct.y) || isnan(prev_direct.z)) { success = false; prev_direct = 0.f; prev_moments = 0.f; history_length = 0.f; } float3 moment_1 = float3(0.0, 0.0, 0.0); float3 moment_2 = float3(0.0, 0.0, 0.0); float3 clamp_min = 1.0; float3 clamp_max = 0.0; [unroll] for(int y = -2; y <= 2; ++y) { [unroll] for(int x = -2; x <= 2; ++x) { float3 color = input_texture[screen_coord + int2(x, y)].xyz; moment_1 += color; moment_2 += color * color; clamp_min = min(color, clamp_min); clamp_max = max(color, clamp_max); } } float3 mu = moment_1 / 25.0; float3 sigma = sqrt(moment_2 / 25.0 - mu*mu); float3 box_min = max(mu - VARIANCE_CLIPPING_GAMMA * sigma, clamp_min); float3 box_max = min(mu + VARIANCE_CLIPPING_GAMMA * sigma, clamp_max); float4 clipped = LineBoxIntersection(box_min, box_max, direct.xyz, prev_direct.xyz); //success = clipped.w > 1.0; prev_direct = float4(clipped.xyz, prev_direct.w); history_length = min(32.0, success ? (history_length + 1.0) : 1.0); const float alpha = lerp(1.0, max(blending_alpha, 1.0/history_length), success); const float moments_alpha = lerp(1.0, max(blending_moments_alpha, 1.0/history_length), success); float2 moments = float2(0, 0); moments.r = Luminance(direct.xyz); moments.g = moments.r * moments.r; moments = lerp(prev_moments, moments, moments_alpha); out_moments_texture[screen_coord] = moments; out_hist_length_texture[screen_coord] = history_length; float variance = max(0.f, moments.g - moments.r * moments.r); direct = lerp(prev_direct, direct, alpha); out_color_texture[screen_coord] = float4(direct.xyz, variance); } [numthreads(16,16,1)] void filter_moments_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); out_color_texture.GetDimensions(screen_size.x, screen_size.y); float2 uv = float2(dispatch_thread_id.x / screen_size.x, dispatch_thread_id.y / screen_size.y); float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y); float h = in_hist_length_texture[screen_coord].r; if(h < 4.0) { float sum_weights = 0.0; float3 sum_direct = float3(0.0, 0.0, 0.0); float2 sum_moments = float2(0.0, 0.0); const float4 direct_center = input_texture[screen_coord]; const float luminance_direct_center = Luminance(direct_center.xyz); float3 normal_center = float3(0.0, 0.0, 0.0); float2 depth_center = float2(0.0, 0.0); FetchNormalAndLinearZ(screen_coord, normal_center, depth_center); if(depth_center.x < 0.0) { out_color_texture[screen_coord] = direct_center; return; } const float phi_direct = l_phi; const float phi_depth = max(depth_center.y, 1e-8) * 3.0; const int radius = 3; [unroll] for(int y = -radius; y <= radius; ++y) { [unroll] for(int x = -radius; x <= radius; ++x) { const int2 p = screen_coord + int2(x, y); const bool inside = p.x >= 0 && p.x < screen_size.x && p.y >= 0 && p.y < screen_size.y; const bool same_pixel = (x==0) && (y==0); const float kernel = 1.0; if(inside) { const float3 direct_p = input_texture[p].xyz; const float2 moments_p = prev_moments_texture[p].xy; if(isnan(direct_p.x) || isnan(direct_p.y) || isnan(direct_p.z) || isnan(moments_p.x) || isnan(moments_p.y)) { continue; } const float l_direct_p = Luminance(direct_p); float3 normal_p; float2 z_p; FetchNormalAndLinearZ(p, normal_p, z_p); const float w = CalcWeights( depth_center.x, z_p.x, phi_depth * length(float2(x, y)), normal_center, normal_p, n_phi, luminance_direct_center, l_direct_p, l_phi); if(isnan(w)) { continue; } sum_weights += w; sum_direct += direct_p * w; sum_moments += moments_p * float2(w.xx); } } } sum_weights = max(sum_weights, 1e-6f); sum_direct /= sum_weights; sum_moments /= float2(sum_weights.xx); float variance = sum_moments.y - sum_moments.x * sum_moments.x; variance *= 4.0/h; out_color_texture[screen_coord] = float4(sum_direct.xyz, variance); } else { out_color_texture[screen_coord] = input_texture[screen_coord]; } } [numthreads(16,16,1)] void wavelet_filter_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); out_color_texture.GetDimensions(screen_size.x, screen_size.y); float2 uv = float2(dispatch_thread_id.x / screen_size.x, dispatch_thread_id.y / screen_size.y); float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y); const float eps_variance = 1e-10; const float kernel_weights[3] = {1.0, 2.0 / 3.0, 1.0 / 6.0}; const float4 direct_center = input_texture[screen_coord]; const float luminance_direct_center = Luminance(direct_center.xyz); const float variance = ComputeVarianceCenter(int2(screen_coord.xy)); out_color_texture[screen_coord] = direct_center; const float history_length = in_hist_length_texture[screen_coord].r; float3 normal_center; float2 depth_center; FetchNormalAndLinearZ(screen_coord, normal_center, depth_center); if(depth_center.x < 0) { out_color_texture[screen_coord] = direct_center; return; } const float phi_l_direct = l_phi * sqrt(max(0.0, eps_variance + variance)); const float phi_depth = max(depth_center.y, 1e-8) * step_distance; float sum_weights = 1.0; float4 sum_direct = direct_center; [unroll] for(int y = -2; y <= 2; ++y) { [unroll] for(int x = -2; x <= 2; ++x) { const int2 p = int2(screen_coord.xy) + int2(x, y) * step_distance; const bool inside = p.x >= 0 && p.x < screen_size.x && p.y >= 0 && p.y < screen_size.y; const float kernel = kernel_weights[abs(x)] * kernel_weights[abs(y)]; if(inside && (x != 0 || y != 0)) { const float4 direct_p = input_texture[p]; if(isnan(direct_p.x) || isnan(direct_p.y) || isnan(direct_p.z) || isnan(direct_p.w)) { continue; } float3 normal_p; float2 depth_p; FetchNormalAndLinearZ(p, normal_p, depth_p); const float luminance_direct_p = Luminance(direct_p.xyz); const float w = CalcWeights( depth_center.x, depth_p.x, phi_depth*length(float2(x, y)), normal_center, normal_p, n_phi, luminance_direct_center, luminance_direct_p, phi_l_direct ); if(isnan(w)) { continue; } const float w_direct = w * kernel; sum_weights += w_direct; sum_direct += float4(w_direct.xxx, w_direct * w_direct) * direct_p; } } } sum_weights = max(sum_weights, 1e-6f); out_color_texture[screen_coord] = float4(sum_direct / float4(sum_weights.xxx, sum_weights * sum_weights)); } #endif //__DENOISING_SVGF_HLSL__ ================================================ FILE: resources/shaders/denoising_reflections.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DENOISING_REFLECTIONS_HLSL__ #define __DENOISING_REFLECTIONS_HLSL__ #include "pbr_util.hlsl" #include "rand_util.hlsl" Texture2D input_texture : register(t0); Texture2D ray_raw_texture : register(t1); Texture2D dir_hit_t_texture : register(t2); Texture2D albedo_roughness_texture : register(t3); Texture2D normal_metallic_texture : register(t4); Texture2D motion_texture : register(t5); // xy: motion, z: fwidth position, w: fwidth normal Texture2D linear_depth_texture : register(t6); Texture2D world_position_texture : register(t7); Texture2D in_history_texture : register(t8); Texture2D accum_texture : register(t9); Texture2D prev_normal_texture : register(t10); Texture2D prev_depth_texture : register(t11); Texture2D in_moments_texture : register(t12); RWTexture2D output_texture : register(u0); RWTexture2D out_history_texture : register(u1); RWTexture2D out_moments_texture : register(u2); SamplerState point_sampler : register(s0); SamplerState linear_sampler : register(s0); cbuffer CameraProperties : register(b0) { float4x4 view; float4x4 projection; float4x4 inv_projection; float4x4 inv_view; float4x4 prev_projection; float4x4 prev_view; uint is_hybrid; uint is_path_tracer; uint is_ao; uint has_shadows; float3 padding1; uint has_reflections; }; cbuffer DenoiserSettings : register(b1) { float color_integration_alpha; float moments_integration_alpha; float variance_clipping_sigma; float roughness_reprojection_threshold; int max_history_samples; float n_phi; float z_phi; float l_phi; }; cbuffer WaveletPass : register(b2) { float wavelet_size; } float3 OctToDir(uint octo) { float2 e = float2( f16tof32(octo & 0xFFFF), f16tof32((octo>>16) & 0xFFFF) ); float3 v = float3(e, 1.0 - abs(e.x) - abs(e.y)); if (v.z < 0.0f) v.xy = (1.0f - abs(v.yx)) * (step(0.0f, v.xy) * 2.0f - float2(1.0f,1.0f)); return normalize(v); } float Luminance(float3 color) { return (color.r + color.r + color.b + color.g + color.g + color.g) / 6.0f; } void FetchNormalAndLinearZ(in float2 ipos, out float3 norm, out float2 zLinear) { norm = normal_metallic_texture[ipos].xyz; zLinear = linear_depth_texture[ipos].xy; } float NormalDistanceCos(float3 n1, float3 n2, float power) { return pow(max(0.0f, dot(n1, n2)), power); } float ComputeWeight( float depth_center, float depth_p, float phi_depth, float3 normal_center, float3 normal_p, float norm_power, float luminance_direct_center, float luminance_direct_p, float phi_direct) { const float w_normal = NormalDistanceCos(normal_center, normal_p, norm_power); const float w_z = (phi_depth == 0.f) ? 0.0f : abs(depth_center - depth_p) / phi_depth; const float w_l_direct = abs(luminance_direct_center - luminance_direct_p) / phi_direct; const float w_direct = exp(0.0f - max(w_l_direct, 0.0f) - max(w_z, 0.0f)) * w_normal; return w_direct; } float ComputeWeightNoLuminance(float depth_center, float depth_p, float phi_depth, float3 normal_center, float3 normal_p) { const float w_normal = NormalDistanceCos(normal_center, normal_p, 128.f); const float w_z = abs(depth_center - depth_p) / phi_depth; return exp(-max(w_z, 0.0f)) * w_normal; } bool IsReprojectionValid(float2 coord, float z, float z_prev, float fwidth_z, float3 normal, float3 normal_prev, float fwidth_normal) { int2 screen_size = int2(0, 0); output_texture.GetDimensions(screen_size.x, screen_size.y); bool ret = (coord.x >= 0.f && coord.x < screen_size.x && coord.y >= 0.f && coord.y < screen_size.y); ret = ret && ((distance(normal, normal_prev) / (fwidth_normal + 1e-2)) < 16.0); return ret; } bool LoadPrevData(float2 screen_coord, inout float2 found_pos, out float4 prev_direct, out float2 prev_moments, out float history_length) { float2 screen_size = float2(0.f, 0.f); output_texture.GetDimensions(screen_size.x, screen_size.y); float2 uv = screen_coord / screen_size; float4 motion = motion_texture.SampleLevel(point_sampler, uv, 0); float2 q_uv = uv - motion.xy; float2 prev_coords = q_uv * screen_size; const float roughness = albedo_roughness_texture[screen_coord].w; float4 depth = linear_depth_texture[prev_coords]; float3 normal = OctToDir(asuint(depth.w)); prev_direct = float4(0.0f, 0.0f, 0.0f, 0.0f); prev_moments = float2(0.0f, 0.0f); bool v[4]; const float2 pos_prev = prev_coords; int2 offset[4] = {int2(0, 0), int2(1, 0), int2(0, 1), int2(1, 1)}; bool valid = false; [unroll] for(int sample_idx = 0; sample_idx < 4; ++sample_idx) { float2 loc = pos_prev + offset[sample_idx]; float4 depth_prev = prev_depth_texture[loc]; float3 normal_prev = OctToDir(asuint(depth_prev.w)); v[sample_idx] = IsReprojectionValid(loc, depth.z, depth_prev.x, depth.y, normal, normal_prev, motion.w); valid = valid || v[sample_idx]; } if(valid) { float sum_weights = 0.0f; float x = frac(pos_prev.x); float y = frac(pos_prev.y); float weights[4] = {(1.0f - x) * (1.0f - y), x * (1.0f - y), (1.0f - x) * y, x * y }; [unroll] for(int sample_idx = 0; sample_idx < 4; ++sample_idx) { float2 loc = pos_prev + offset[sample_idx]; prev_direct += weights[sample_idx] * accum_texture[loc] * float(v[sample_idx]); prev_moments += weights[sample_idx] * in_moments_texture[loc].xy * float(v[sample_idx]); sum_weights += weights[sample_idx] * float(v[sample_idx]); } valid = (sum_weights >= 0.01f); prev_direct = lerp(float4(0.0f, 0.0f, 0.0f, 0.0f), prev_direct / sum_weights, valid); prev_moments = lerp(float2(0.0f, 0.0f), prev_moments / sum_weights, valid); } if(!valid) { float cnt = 0.0f; const int radius = 1; for(int y = -radius; y <= radius; ++y) { for(int x = -radius; x <= radius; ++x) { float2 p = prev_coords + float2(x, y); float4 depth_filter = prev_depth_texture[p]; float3 normal_filter = OctToDir(asuint(depth_filter.w)); if(IsReprojectionValid(prev_coords, depth.z, depth_filter.x, depth.y, normal, normal_filter, motion.w)) { prev_direct += accum_texture[p]; prev_moments += in_moments_texture[p].xy; cnt += 1.0; } } } valid = cnt > 0; prev_direct /= lerp(1, cnt, cnt > 0); prev_moments /= lerp(1, cnt, cnt > 0); } if(valid) { history_length = in_history_texture[prev_coords].r; } else { prev_direct = float4(0.0f, 0.0f, 0.0f, 0.0f); prev_moments = float2(0.0f, 0.0f); history_length = 0; } found_pos = lerp(float2(-1.0f, -1.0f), pos_prev, valid); return valid; } float ComputeVarianceCenter(int2 center) { float sum = 0.0f; const float kernel[2][2] = { {1.0f / 4.0f, 1.0f / 8.0f}, {1.0f / 8.0f, 1.0f / 16.0f} }; const int radius = 1; [unroll] for(int y = -radius; y <= radius; ++y) { [unroll] for(int x = -radius; x <= radius; ++x) { int2 p = center + int2(x, y); float k = kernel[abs(x)][abs(y)]; sum += input_texture[p].w * k; } } return sum; } float4 LineBoxIntersection(float3 box_min, float3 box_max, float3 c_in, float3 c_hist) { float3 p_clip = 0.5f * (box_max + box_min); float3 e_clip = 0.5f * (box_max - box_min); float3 v_clip = c_hist - p_clip; float3 v_unit = v_clip.xyz / e_clip; float3 a_unit = abs(v_unit); float ma_unit = max(a_unit.x, max(a_unit.y, a_unit.z)); if(ma_unit > 1.0f) { return float4((p_clip + v_clip / ma_unit).xyz, ma_unit); } else { return float4(c_hist.xyz, ma_unit); } } [numthreads(16, 16, 1)] void temporal_denoiser_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_coord = float2(dispatch_thread_id.xy); float2 screen_size = float2(0.0f, 0.0f); output_texture.GetDimensions(screen_size.x, screen_size.y); float2 uv = screen_coord / (screen_size - 1.0f); float4 accum_color = float4(0.0f, 0.0f, 0.0f, 0.0f); float2 prev_moments = float2(0.0f, 0.0f); float history = 0; float2 prev_pos = float2(0.0f, 0.0f); bool valid = LoadPrevData(screen_coord, prev_pos, accum_color, prev_moments, history); history = lerp(0, history, valid); float4 input_color = input_texture[screen_coord]; float pdf = ray_raw_texture.SampleLevel(point_sampler, uv, 0).w; if(pdf <= 0.0f) { output_texture[screen_coord] = float4(input_color.xyz, 0.f); return; } float3 moment_1 = float3(0.0f, 0.0f, 0.0f); float3 moment_2 = float3(0.0f, 0.0f, 0.0f); float3 clamp_min = 1.0f; float3 clamp_max = 0.0f; [unroll] for(int y = -3; y <= 3; ++y) { [unroll] for(int x = -3; x <= 3; ++x) { float3 color = input_texture[screen_coord + int2(x, y)].xyz; moment_1 += color; moment_2 += color * color; clamp_min = min(color, clamp_min); clamp_max = max(color, clamp_max); } } float3 mu = moment_1 / 49.0f; float3 sigma = sqrt(moment_2 / 49.0f - mu*mu); float3 box_min = mu - variance_clipping_sigma * sigma; float3 box_max = mu + variance_clipping_sigma * sigma; float4 clipped = LineBoxIntersection(box_min, box_max, input_color.xyz, accum_color.xyz); const float roughness = albedo_roughness_texture[screen_coord].w; //perhaps replace the 0.25 with a clamping strength variable for the settings screen accum_color = lerp(clipped, accum_color, pow(clamp(roughness, 0.0f, 1.0f), 0.25f)); history = min(max_history_samples, history + 1); const float alpha = lerp(1.0f, max(color_integration_alpha, 1.0f/history), valid); const float moments_alpha = lerp(1.0f, max(moments_integration_alpha, 1.0f/history), valid); float2 cur_moments = float2(0.0f, 0.0f); cur_moments.x = Luminance(input_color.xyz); cur_moments.y = cur_moments.x * cur_moments.x; cur_moments = lerp(prev_moments, cur_moments, moments_alpha); float variance = cur_moments.y - cur_moments.x * cur_moments.x; float3 output = lerp(accum_color, input_color, alpha); output_texture[floor(screen_coord)] = float4(output.xyz, variance); out_history_texture[floor(screen_coord)] = history; out_moments_texture[floor(screen_coord)] = cur_moments; } [numthreads(16, 16, 1)] void variance_estimator_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_coord = float2(dispatch_thread_id.xy); float2 screen_size = float2(0.f, 0.f); output_texture.GetDimensions(screen_size.x, screen_size.y); float2 uv = screen_coord / screen_size; float history = in_history_texture[screen_coord].r; [branch] if(history < 5.f) { float sum_weights = 0.0f; float3 sum_direct = float3(0.0f, 0.0f, 0.0f); float2 sum_moments = float2(0.0f, 0.0f); const float4 direct_center = input_texture[screen_coord]; float3 normal_center = float3(0.0f, 0.0f, 0.0f); float2 depth_center = float2(0.0f, 0.0f); FetchNormalAndLinearZ(screen_coord, normal_center, depth_center); if(depth_center.x < 0.0f) { output_texture[screen_coord] = direct_center; return; } const float phi_direct = l_phi; const float phi_depth = max(depth_center.y, 1e-8) * 3.0f; const int radius = 3.f; [unroll] for(int y = -radius; y <= radius; ++y) { [unroll] for(int x = -radius; x <= radius; ++x) { const int2 p = screen_coord + int2(x, y); const bool inside = p.x >= 0.f && p.x < screen_size.x && p.y >= 0.f && p.y < screen_size.y; const bool same_pixel = (x==0.f) && (y==0.f); const float kernel = 1.0f; if(inside) { const float3 direct_p = input_texture[p].xyz; const float2 moments_p = in_moments_texture[p].xy; float3 normal_p; float2 z_p; FetchNormalAndLinearZ(p, normal_p, z_p); const float w = ComputeWeightNoLuminance( depth_center.x, z_p.x, phi_depth * length(float2(x, y)), normal_center, normal_p); if(isnan(direct_p.x) || isnan(direct_p.y) || isnan(direct_p.z) || isnan(w)) { continue; } sum_weights += w; sum_direct += direct_p * w; sum_moments += moments_p * float2(w.xx); } } } sum_weights = max(sum_weights, 1e-6f); sum_direct /= sum_weights; sum_moments /= float2(sum_weights.xx); float variance = sum_moments.y - sum_moments.x * sum_moments.x; variance *= 5.0f/history; output_texture[screen_coord] = float4(sum_direct.xyz, variance); } else { output_texture[screen_coord] = input_texture[screen_coord]; } } [numthreads(16, 16, 1)] void spatial_denoiser_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_coord = float2(dispatch_thread_id.xy); float2 screen_size = float2(0.0f, 0.0f); output_texture.GetDimensions(screen_size.x, screen_size.y); float2 uv = screen_coord / screen_size; const float eps_variance = 1e-10; const float kernel_weights[3] = {1.0f, 2.0f / 3.0f, 1.0f / 6.0f}; const float4 direct_center = input_texture[screen_coord]; const float luminance_direct_center = Luminance(direct_center.xyz); const float variance = ComputeVarianceCenter(int2(screen_coord.xy)); if(isnan(variance)) { output_texture[screen_coord] = float4(0.0f, 1.0f, 0.0f, 0.0f); return; } output_texture[screen_coord] = direct_center; const float history_length = in_history_texture[screen_coord].r; float3 normal_center; float2 depth_center; FetchNormalAndLinearZ(screen_coord, normal_center, depth_center); if(depth_center.x < 0.0f) { output_texture[screen_coord] = direct_center; return; } const float roughness = albedo_roughness_texture[screen_coord].w; const float phi_l_direct = l_phi * sqrt(max(0.0f, variance)); const float phi_depth = max(depth_center.y, 1e-8) * wavelet_size; float sum_weights = 1.0f; float4 sum_direct = direct_center; [unroll] for(int y = -2; y <= 2; ++y) { [unroll] for(int x = -2; x <= 2; ++x) { const int2 p = int2(screen_coord.xy) + int2(x, y) * wavelet_size; const bool inside = p.x >= 0 && p.x < screen_size.x && p.y >= 0 && p.y < screen_size.y; const float kernel = kernel_weights[abs(x)] * kernel_weights[abs(y)]; if(inside && (x != 0 || y != 0)) { const float4 direct_p = input_texture[p]; const float luminance_direct_p = Luminance(direct_p.xyz); float3 normal_p; float2 depth_p; FetchNormalAndLinearZ(p, normal_p, depth_p); const float w = ComputeWeight( depth_center.x, depth_p.x, phi_depth*length(float2(x, y)), normal_center, normal_p, n_phi, luminance_direct_center, luminance_direct_p, phi_l_direct) * sqrt(max(1e-15f, roughness)); const float w_direct = w * kernel; sum_weights += w_direct; sum_direct += float4(w_direct.xxx, w_direct * w_direct) * direct_p; } } } output_texture[screen_coord] = sum_direct / sum_weights; } #endif ================================================ FILE: resources/shaders/denoising_spatial_reconstruction.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DENOISING_SPATIAL_RECONSTRUCTION_HLSL__ #define __DENOISING_SPATIAL_RECONSTRUCTION_HLSL__ #include "pbr_util.hlsl" #include "rand_util.hlsl" cbuffer CameraProperties : register(b0) { float4x4 inv_vp; float4x4 inv_view; float padding; uint frame_idx; float near_plane, far_plane; }; RWTexture2D filtered : register(u0); Texture2D reflection_pdf : register(t0); Texture2D dir_hit_t : register(t1); Texture2D albedo_roughness : register(t2); Texture2D normal_metallic : register(t3); Texture2D depth_buffer : register(t4); SamplerState nearest_sampler : register(s0); SamplerState linear_sampler : register(s1); float linearize_depth(float D) { float z_n = D * 2.0f - 1.0f; return 2.0f * near_plane * far_plane / (far_plane + near_plane - z_n * (far_plane - near_plane)); } //Get total weight from neighbor using normal and normalized depth from both neighbor and center float neighbor_edge_weight(float3 N, float3 N_neighbor, float D, float D_neighbor, float2 uv) { return float(uv.x >= 0.f && uv.y >= 0.f && uv.x <= 1.f && uv.y <= 1.f) * (D != 1.0f && D_neighbor != 1.0f); } //Hardcode the samples for now; settings: kernelSize=5, points=16 //https://github.com/Nielsbishere/NoisePlayground/blob/master/bluenoise_test.py static const uint sample_count = 16; static const float2 samples[4][16] = { { float2(0.f , 0.f), float2(-1.f , -1.f), float2(1.f , 0.f), float2(-2.f , -1.f), float2(-2.f , 1.f), float2(0.f , -3.f), float2(2.f , -2.f), float2(1.f , 2.f), float2(0.f , 3.f), float2(-4.f , -1.f), float2(0.f , -4.f), float2(-2.f , 3.f), float2(-4.f , 1.f), float2(3.f , 1.f), float2(3.f , -3.f), float2(4.f , 0.f) }, { float2(0.f , 0.f), float2(1.f , -1.f), float2(0.f , -2.f), float2(-1.f , -2.f), float2(-1.f , 2.f), float2(0.f , 2.f), float2(2.f , 0.f), float2(-3.f , 1.f), float2(-3.f , -2.f), float2(1.f , -3.f), float2(-3.f , 2.f), float2(3.f , 0.f), float2(-2.f , -4.f), float2(1.f , 3.f), float2(-4.f , -2.f), float2(2.f , 3.f) }, { float2(0.f , 0.f), float2(-1.f , 0.f), float2(0.f , 1.f), float2(-2.f , 0.f), float2(2.f , -1.f), float2(-1.f , -3.f), float2(-3.f , -1.f), float2(2.f , 2.f), float2(2.f , -3.f), float2(3.f , -1.f), float2(-3.f , -3.f), float2(-4.f , 0.f), float2(-1.f , -4.f), float2(-4.f , 2.f), float2(-3.f , 3.f), float2(4.f , -1.f) }, { float2(0.f , 0.f), float2(0.f , -1.f), float2(-1.f , 1.f), float2(-2.f , -2.f), float2(1.f , 1.f), float2(1.f , -2.f), float2(-3.f , 0.f), float2(2.f , 1.f), float2(-2.f , -3.f), float2(-2.f , 2.f), float2(-1.f , 3.f), float2(1.f , -4.f), float2(3.f , -2.f), float2(3.f , 2.f), float2(-4.f , -3.f), float2(0.f , 4.f) } }; //Sample a neighbor; 0,0 -> 1,1; outside of that range indicates an invalid uv float2 sample_neighbor_uv(uint sampleId, uint2 full_res_pixel, uint2 resolution, float2 rand, float kernelSize) { uint pixId = full_res_pixel.x % 2 + full_res_pixel.y % 2 * 2; float2 offset = samples[pixId][sampleId] * kernelSize; offset = mul(float2x2(rand.x, rand.y, -rand.y, rand.x), offset); return (float2(full_res_pixel / 2.f) + offset) / float2(resolution / 2.f - 1.f); } static const float3 luminance = float3(0.2126f, 0.7152f, 0.0722f); float3 unpack_position(float2 uv, float depth) { // Get world space position const float4 ndc = float4(uv * 2.0f - 1.0f, depth, 1.0f); float4 wpos = mul(inv_vp, ndc); return (wpos.xyz / wpos.w).xyz; } [numthreads(16, 16, 1)] void main(int3 pix3 : SV_DispatchThreadID) { //Get dispatch dimensions uint2 pix = uint2(pix3.xy); uint width, height; depth_buffer.GetDimensions(width, height); //Get per pixel values const float depth = depth_buffer[pix].r; const float2 uv = float2(pix.xy) / float2(width - 1.f, height - 1.f); const float3 pos = unpack_position(uv, depth); const float3 camera_pos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]); const float3 V = normalize(camera_pos - pos); float roughness = albedo_roughness[pix].w; const float3 N = normalize(normal_metallic[pix].xyz); const float pdf = reflection_pdf.SampleLevel(nearest_sampler, uv, 0).w; float3 result3; //pdf < 0 disables spatial reconstruction if (pdf >= 0) { //Weigh the samples correctly float3 result = float3(0.f, 0.f, 0.f); float weight_sum = 0.0f; uint rand_seed = initRand(pix.x + pix.y * width, frame_idx); float distance = length(camera_pos - pos); float sampleCountScalar = lerp(1.f, (1.f - distance / far_plane) * roughness, 1); roughness = max(roughness, 1e-2); float kernel_size = 8.f; for (uint i = 0; i < max(ceil(kernel_size), 1); ++i) { //Get sample related data float2 random = float2(nextRand(rand_seed), nextRand(rand_seed)); const float2 neighbor_uv = sample_neighbor_uv(i, pix, uint2(width, height), random, 1); const float depth_neighbor = depth_buffer.SampleLevel(nearest_sampler, neighbor_uv, 0).r; const float3 pos_neighbor = unpack_position(neighbor_uv, depth_neighbor); const float4 hit_t = dir_hit_t.SampleLevel(nearest_sampler, neighbor_uv, 0); const float3 V_neighbor = normalize(camera_pos - pos_neighbor); const float4 reflection_pdf_neighbor = reflection_pdf.SampleLevel(nearest_sampler, neighbor_uv, 0); if(reflection_pdf_neighbor.w>0.0) { const float3 color = clamp(reflection_pdf_neighbor.xyz, 0, 1); const float3 L = hit_t.xyz; const float pdf_neighbor = max(reflection_pdf_neighbor.w, 1e-5); const float3 N_neighbor = normalize(normal_metallic.SampleLevel(nearest_sampler, neighbor_uv, 0).xyz); //Calculate weight and weight sum const float neighbor_weight = neighbor_edge_weight(N, N_neighbor, depth, depth_neighbor, neighbor_uv); float weight = (brdf_weight(-V, L, N, roughness) / pdf_neighbor) * neighbor_weight; weight = isnan(weight) || isinf(weight) ? 0 : weight; result += color * weight; weight_sum += weight; } } result3 = result / weight_sum; if(weight_sum == 0.0f) { result3 = reflection_pdf.SampleLevel(nearest_sampler, uv, 0).xyz; } if(isnan(weight_sum) == true) { result3 = float3(0, 0, 1); filtered[pix] = float4(result3, 1); return; } } else { result3 = reflection_pdf.SampleLevel(nearest_sampler, uv, 0).xyz; } filtered[pix] = float4(result3, 1); } #endif ================================================ FILE: resources/shaders/dxr_ambient_occlusion.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_AMBIENT_OCCLUSION_HLSL__ #define __DXR_AMBIENT_OCCLUSION_HLSL__ #include "rand_util.hlsl" #include "dxr_global.hlsl" RWTexture2D output : register(u0); // x: AO value Texture2D gbuffer_normal : register(t1); Texture2D gbuffer_depth : register(t2); struct AOHitInfo { float is_hit; float thisvariablesomehowmakeshybridrenderingwork_killme; }; cbuffer CBData : register(b0) { float4x4 inv_vp; float4x4 inv_view; float bias; float radius; float power; float max_distance; float2 padding; float frame_idx; unsigned int sample_count; }; struct Attributes { }; float3 unpack_position(float2 uv, float depth) { // Get world space position const float4 ndc = float4(uv * 2.0 - 1.0, depth, 1.0); float4 wpos = mul(inv_vp, ndc); return (wpos.xyz / wpos.w).xyz; } bool TraceAORay(uint idx, float3 origin, float3 direction, float far, unsigned int depth) { // Define a ray, consisting of origin, direction, and the min-max distance values RayDesc ray; ray.Origin = origin; ray.Direction = direction; ray.TMin = 0.f; ray.TMax = far; AOHitInfo payload = { 1.0f, 0.0f }; // Trace the ray TraceRay( Scene, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, ~0, // InstanceInclusionMask 0, // RayContributionToHitGroupIndex 0, // MultiplierForGeometryContributionToHitGroupIndex 0, // miss shader index is set to idx but can probably be anything. ray, payload); return payload.is_hit; } [shader("raygeneration")] void AORaygenEntry() { // Texture UV coordinates [0, 1] float2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1); uint rand_seed = initRand(DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x, frame_idx); // Screen coordinates [0, resolution] (inverted y) int2 screen_co = DispatchRaysIndex().xy; float3 normal = gbuffer_normal[screen_co].xyz; float depth = gbuffer_depth[screen_co].x; float3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth); float3 camera_pos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]); float cam_distance = length(wpos-camera_pos); if(cam_distance < max_distance) { //SPP decreases the closer a pixel is to the max distance //Total is always calculated using the full sample count to have further pixels less occluded int spp = min(sample_count, round(sample_count * ((max_distance - cam_distance)/max_distance))); int ao_value = sample_count; for(uint i = 0; i< spp; i++) { ao_value -= TraceAORay(0, wpos + normal * bias , getCosHemisphereSample(rand_seed, normal), radius, 0); } output[DispatchRaysIndex().xy].x = pow(ao_value/float(sample_count), power); } else { output[DispatchRaysIndex().xy].x = 1.f; } } [shader("miss")] void MissEntry(inout AOHitInfo hit : SV_RayPayload) { hit.is_hit = 0.0f; } #endif //__DXR_AMBIENT_OCCLUSION_HLSL__ ================================================ FILE: resources/shaders/dxr_functions.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_FUNCTIONS_HLSL__ #define __DXR_FUNCTIONS_HLSL__ float3 HitAttribute(float3 a, float3 b, float3 c, BuiltInTriangleIntersectionAttributes attr) { float3 vertexAttribute[3]; vertexAttribute[0] = a; vertexAttribute[1] = b; vertexAttribute[2] = c; return vertexAttribute[0] + attr.barycentrics.x * (vertexAttribute[1] - vertexAttribute[0]) + attr.barycentrics.y * (vertexAttribute[2] - vertexAttribute[0]); } uint3 Load3x32BitIndices(ByteAddressBuffer buffer, uint offsetBytes) { // Load first 2 indices return buffer.Load3(offsetBytes); } // Retrieve hit world position. float3 HitWorldPosition() { return WorldRayOrigin() + RayTCurrent() * WorldRayDirection(); } float3 unpack_position(float2 uv, float depth, float4x4 inv_vp) { // Get world space position const float4 ndc = float4(uv * 2.0 - 1.0, depth, 1.0); float4 wpos = mul(inv_vp, ndc); return (wpos.xyz / wpos.w).xyz; } float3 CalcPeturbedNormal(float3 normal, float3 normal_map, float3 tangent, float3 bitangent, float3 V, out float3 world_normal) { float3x4 object_to_world = ObjectToWorld3x4(); float3 N = normalize(mul(object_to_world, float4(normal, 0))); float3 T = normalize(mul(object_to_world, float4(tangent, 0))); #ifndef CALC_BITANGENT const float3 B = normalize(mul(object_to_world, float4(bitangent, 0))); #else T = normalize(T - dot(T, N) * N); float3 B = cross(N, T); #endif const float3x3 TBN = float3x3(T, B, N); float3 fN = normalize(mul(normal_map, TBN)); world_normal = N; return fN; } float3 CalcPeturbedNormal(float3 normal, float3 normal_map, float3 tangent, float3 bitangent, float3 V) { float3 temp = 0; return CalcPeturbedNormal(normal, normal_map, tangent, bitangent, V, temp); } #endif //__DXR_FUNCTIONS_HLSL__ ================================================ FILE: resources/shaders/dxr_global.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_GLOBAL_HLSL__ #define __DXR_GLOBAL_HLSL__ #define MAX_RECURSION 2 //#define FOUR_X_A //#define PATH_TRACING //#define DEPTH_OF_FIELD #define EPSILON 0.1 #define SOFT_SHADOWS #define MAX_SHADOW_SAMPLES 1 //#define PERFECT_MIRROR_REFLECTIONS #define GROUND_TRUTH_REFLECTIONS #define MAX_GT_REFLECTION_SAMPLES 4 //#define DISABLE_SPATIAL_RECONSTRUCTION #define RUSSIAN_ROULETTE #define NO_PATH_TRACED_NORMALS #define CALC_BITANGENT // calculate bitangent in the shader instead of using the bitangent uploaded #ifdef FALLBACK #undef MAX_RECURSION #define MAX_RECURSION 1 #undef GROUND_TRUTH_REFLECTIONS #undef MAX_GT_REFLECTION_SAMPLES #define MAX_GT_REFLECTION_SAMPLES 2 //#define NO_SHADOWS #endif RaytracingAccelerationStructure Scene : register(t0, space0); #endif //__DXR_GLOBAL_HLSL__ ================================================ FILE: resources/shaders/dxr_pathtracer_accumulation.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_PATHTRACER_ACCUMULATION_HLSL__ #define __DXR_PATHTRACER_ACCUMULATION_HLSL__ #include "pp_util.hlsl" #include "pp_hdr_util.hlsl" Texture2D input : register(t0); RWTexture2D output : register(u0); SamplerState s0 : register(s0); cbuffer Properties : register(b0) { float frame_idx; }; [numthreads(16, 16, 1)] void main(uint3 DTid : SV_DispatchThreadID) { float4 current = input[DTid.xy]; // Last path tracer result float4 prev = output[DTid.xy]; // Previous path tracer output float accum_count = frame_idx; // 0 to x, the number of times the accumulation has ran. float4 color = (accum_count * prev + current) / (accum_count + 1); // accumulate output[DTid.xy] = color; // update the output with the accumulated result. } #endif //__DXR_PATHTRACER_ACCUMULATION_HLSL__ ================================================ FILE: resources/shaders/dxr_pathtracer_entries.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_PATHTRACER_ENTRIES_HLSL__ #define __DXR_PATHTRACER_ENTRIES_HLSL__ #include "dxr_shadow_entries.hlsl" //Reflections [shader("closesthit")] void ReflectionHit(inout PathTracingHitInfoCone payload, in Attributes attr) { // Calculate the essentials const Offset offset = g_offsets[InstanceID()]; const Material material = g_materials[offset.material_idx]; const float3 hit_pos = HitWorldPosition(); const float index_offset = offset.idx_offset; const float vertex_offset = offset.vertex_offset; // Find first index location const uint index_size = 4; const uint indices_per_triangle = 3; const uint triangle_idx_stride = indices_per_triangle * index_size; uint base_idx = PrimitiveIndex() * triangle_idx_stride; base_idx += index_offset * 4; // offset the start uint3 indices = Load3x32BitIndices(g_indices, base_idx); indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start // Gather triangle vertices const Vertex v0 = g_vertices[indices.x]; const Vertex v1 = g_vertices[indices.y]; const Vertex v2 = g_vertices[indices.z]; // Variables const float3 V = normalize(payload.origin - hit_pos); // Calculate actual "fragment" attributes. const float3 frag_pos = HitAttribute(v0.pos, v1.pos, v2.pos, attr); const float3 normal = normalize(HitAttribute(v0.normal, v1.normal, v2.normal, attr)); const float3 tangent = HitAttribute(v0.tangent, v1.tangent, v2.tangent, attr); const float3 bitangent = HitAttribute(v0.bitangent, v1.bitangent, v2.bitangent, attr); float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; uv.y = 1.0f - uv.y; float mip_level = payload.depth + 1; OutputMaterialData output_data = InterpretMaterialDataRT(material.data, g_textures[material.albedo_id], g_textures[material.normal_id], g_textures[material.roughness_id], g_textures[material.metalicness_id], g_textures[material.emissive_id], g_textures[material.ao_id], mip_level, s0, uv); float3 albedo = output_data.albedo; float roughness = output_data.roughness; float metal = output_data.metallic; float3 emissive = output_data.emissive; float ao = output_data.ao; #ifdef NO_PATH_TRACED_NORMALS float3 N = normalize(mul(ObjectToWorld3x4(), float4(normal, 0))); float3 fN = N; #else float3 N = 0; float3 fN = CalcPeturbedNormal(normal, output_data.normal, tangent, bitangent, V, N); #endif // #################### GGX ##################### nextRand(payload.seed); payload.color = ggxIndirect(hit_pos, fN, N, V, albedo, metal, roughness, ao, payload.seed, payload.depth + 1, true, payload.cone); payload.color += ggxDirect(hit_pos, fN, N, V, albedo, metal, roughness, payload.seed, payload.depth + 1); payload.color += emissive; } //Reflection skybox [shader("miss")] void ReflectionMiss(inout PathTracingHitInfoCone payload) { payload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0).rgb; } #endif //__DXR_PATHTRACER_ENTRIES_HLSL__ ================================================ FILE: resources/shaders/dxr_pathtracer_functions.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_PATHTRACER_FUNCTIONS_HLSL__ #define __DXR_PATHTRACER_FUNCTIONS_HLSL__ #include "pbr_util.hlsl" float3 TraceColorRay(float3 origin, float3 direction, unsigned int depth, unsigned int seed) { if (depth >= MAX_RECURSION) { return float3(0, 0, 0); } // Define a ray, consisting of origin, direction, and the min-max distance values RayDesc ray; ray.Origin = origin; ray.Direction = direction; ray.TMin = 0; ray.TMax = 1.0e38f; PathTracingHitInfo payload = { float3(1, 1, 1), seed, origin, depth }; // Trace the ray TraceRay( Scene, RAY_FLAG_NONE, ~0, // InstanceInclusionMask 0, // RayContributionToHitGroupIndex 0, // MultiplierForGeometryContributionToHitGroupIndex 0, // miss shader index ray, payload); return payload.color; } float3 TraceColorRayCone(float3 origin, float3 direction, unsigned int depth, unsigned int seed, RayCone cone) { if (depth >= MAX_RECURSION) { return float3(0, 0, 0); } // Define a ray, consisting of origin, direction, and the min-max distance values RayDesc ray; ray.Origin = origin; ray.Direction = direction; ray.TMin = 0; ray.TMax = 1.0e38f; PathTracingHitInfoCone payload = { float3(1, 1, 1), seed, origin, depth, cone }; // Trace the ray TraceRay( Scene, RAY_FLAG_NONE, ~0, // InstanceInclusionMask 0, // RayContributionToHitGroupIndex 0, // MultiplierForGeometryContributionToHitGroupIndex 0, // miss shader index ray, payload); return payload.color; } // NVIDIA's luminance function inline float luminance(float3 rgb) { return dot(rgb, float3(0.2126f, 0.7152f, 0.0722f)); } // NVIDIA's probability function float probabilityToSampleDiffuse(float3 difColor, float3 specColor) { float lumDiffuse = max(0.01f, luminance(difColor.rgb)); float lumSpecular = max(0.01f, luminance(specColor.rgb)); return lumDiffuse / (lumDiffuse + lumSpecular); } static RayCone null_cone = { 0, 0 }; float3 ggxDirect(float3 hit_pos, float3 fN, float3 N, float3 V, float3 albedo, float metal, float roughness, unsigned int seed, unsigned int depth) { // #################### GGX ##################### uint light_count = lights[0].tid >> 2; if (light_count < 1) return 0; int light_to_sample = min(int(nextRand(seed) * light_count), light_count - 1); Light light = lights[light_to_sample]; float3 L = 0; float max_light_dist = 0; float3 light_intensity = 0; { // Calculate light properties uint tid = light.tid & 3; //Light direction (constant with directional, position dependent with other) L = (lerp(light.pos - hit_pos, light.dir, tid == light_type_directional)); float light_dist = length(L); L /= light_dist; //Spot intensity (only used with spot; but always calculated) float min_cos = cos(light.ang); float max_cos = lerp(min_cos, 1, 0.5f); float cos_angle = dot(light.dir, L); float spot_intensity = lerp(smoothstep(min_cos, max_cos, cos_angle), 1, tid != light_type_spot); //Attenuation & spot intensity (only used with point or spot) float attenuation = lerp(1.0f - smoothstep(0, light.rad, light_dist), 1, tid == light_type_directional); light_intensity = (light.col * spot_intensity) * attenuation; max_light_dist = lerp(light_dist, 100000, tid == light_type_directional); } float3 H = normalize(V + L); // Shadow float shadow_mult = float(light_count) * GetShadowFactor(hit_pos + (L * EPSILON), L, light.light_size, max_light_dist, MAX_SHADOW_SAMPLES, depth, CALLINGPASS_PATHTRACING, seed); // Compute some dot products needed for shading float NdotV = saturate(dot(fN, V)); float NdotL = saturate(dot(fN, L)); float NdotH = saturate(dot(fN, H)); float LdotH = saturate(dot(L, H)); // D = Normal distribution (Distribution of the microfacets) float D = D_GGX(NdotH, max(0.05, roughness)); // G = Geometric shadowing term (Microfacets shadowing) float G = G_SchlicksmithGGX(NdotL, NdotV, max(0.05, roughness)); // F = Fresnel factor (Reflectance depending on angle of incidence) float3 F = F_Schlick(LdotH, metal, albedo); //float3 F = F_ShlickSimple(metal, LdotH); float3 kS = F_SchlickRoughness(NdotV, metal, albedo, roughness); float3 kD = (1.f - kS) * (1.0 - metal); float3 spec = (D * F * G) / (4.0 * NdotV * NdotL + 0.001f); float3 lighting = (light_intensity * (NdotL * spec + NdotL * albedo / M_PI)); return (shadow_mult * lighting) * kD; } float3 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) { // #################### GGX ##################### float diffuse_probability = probabilityToSampleDiffuse(albedo, metal); float choose_diffuse = (nextRand(seed) < diffuse_probability); // Diffuse lobe if (choose_diffuse) { float3 color = (albedo / diffuse_probability) * ao; #ifdef RUSSIAN_ROULETTE // ############## RUSSIAN ROULETTE ############### // Russian Roulette // Randomly terminate a path with a probability inversely equal to the throughput float p = max(color.x, max(color.y, color.z)); // Add the energy we 'lose' by randomly terminating paths color *= 1 / p; if (nextRand(seed) > p) { return 0; } #endif // ##### BOUNCE ##### nextRand(seed); const float3 rand_dir = getCosHemisphereSample(seed, N); float3 irradiance = 0; if (use_raycone) { irradiance = TraceColorRayCone(hit_pos + (EPSILON * N), rand_dir, depth, seed, cone); } else { irradiance = TraceColorRay(hit_pos + (EPSILON * N), rand_dir, depth, seed); } if (dot(N, rand_dir) <= 0.0f) irradiance = float3(0, 0, 0); float3 kS = F_SchlickRoughness(max(dot(fN, V), 0.0f), metal, albedo, roughness); float3 kD = 1.0f - kS; kD *= 1.0f - metal; return kD * (irradiance * color); } else { nextRand(seed); float3 H = getGGXMicrofacet(seed, roughness, N); // ### BRDF ### float3 L = normalize(2.f * dot(V, H) * H - V); // Compute some dot products needed for shading float NdotV = saturate(dot(N, V)); float NdotL = saturate(dot(N, L)); float NdotH = saturate(dot(N, H)); float LdotH = saturate(dot(L, H)); // D = Normal distribution (Distribution of the microfacets) float D = D_GGX(NdotH, max(0.05, roughness)); // G = Geometric shadowing term (Microfacets shadowing) float G = G_SchlicksmithGGX(NdotL, NdotV, max(0.05, roughness)); // F = Fresnel factor (Reflectance depending on angle of incidence) float3 F = F_Schlick(NdotH, metal, albedo); float3 spec = (D * F * G) / ((4.0 * NdotL * NdotV + 0.001f)); float ggx_probability = D * NdotH / (4 * LdotH); float3 color = (NdotL * spec / (ggx_probability * (1.0f - diffuse_probability))); #ifdef RUSSIAN_ROULETTE // ############## RUSSIAN ROULETTE ############### // Russian Roulette // Randomly terminate a path with a probability inversely equal to the throughput float p = max(color.x, max(color.y, color.z)); // Add the energy we 'lose' by randomly terminating paths color *= 1 / p; if (nextRand(seed) > p) { return 0; } #endif // #### BOUNCE #### float3 irradiance = 0; if (use_raycone) { irradiance = TraceColorRayCone(hit_pos + (EPSILON * N), L, depth, seed, cone); } else { irradiance = TraceColorRay(hit_pos + (EPSILON * N), L, depth, seed); } if (dot(N, L) <= 0.0f) irradiance = float3(0, 0, 0); return color * irradiance; } } #endif //__DXR_PATHTRACER_FUNCTIONS_HLSL__ ================================================ FILE: resources/shaders/dxr_pathtracer_main.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_PATHTRACER_MAIN_HLSL__ #define __DXR_PATHTRACER_MAIN_HLSL__ #define LIGHTS_REGISTER register(t2) #include "rand_util.hlsl" #include "pbr_util.hlsl" #include "material_util.hlsl" #include "lighting.hlsl" #include "dxr_texture_lod.hlsl" // Definitions for: // - Vertex, Material, Offset // - Ray, RayCone, ReflectionHitInfo, etc #include "dxr_structs.hlsl" // Definitions for: // - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute #include "dxr_functions.hlsl" RWTexture2D output : register(u0); // xyz: reflection, a: shadow factor ByteAddressBuffer g_indices : register(t1); StructuredBuffer g_vertices : register(t3); StructuredBuffer g_materials : register(t4); StructuredBuffer g_offsets : register(t5); Texture2D g_textures[1000] : register(t10); Texture2D gbuffer_albedo : register(t1010); Texture2D gbuffer_normal : register(t1011); Texture2D gbuffer_emissive : register(t1012); Texture2D gbuffer_depth : register(t1013); TextureCube skybox : register(t6); TextureCube irradiance_map : register(t9); SamplerState s0 : register(s0); typedef BuiltInTriangleIntersectionAttributes Attributes; cbuffer CameraProperties : register(b0) { float4x4 inv_view; float4x4 inv_projection; float4x4 inv_vp; float frame_idx; float3 padding; }; #include "dxr_pathtracer_functions.hlsl" #include "dxr_pathtracer_entries.hlsl" [shader("raygeneration")] void RaygenEntry() { uint rand_seed = initRand((DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x), frame_idx); // Texture UV coordinates [0, 1] float2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1); // Screen coordinates [0, resolution] (inverted y) int2 screen_co = DispatchRaysIndex().xy; // Get g-buffer information float4 albedo_roughness = gbuffer_albedo[screen_co]; float4 normal_metallic = gbuffer_normal[screen_co]; float4 emissive_ao = gbuffer_emissive[screen_co]; // Unpack G-Buffer float depth = gbuffer_depth[screen_co].x; float3 albedo = albedo_roughness.rgb; float3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth, inv_vp); float3 normal = normalize(normal_metallic.xyz); float metallic = normal_metallic.w; float roughness = albedo_roughness.w; float3 emissive = emissive_ao.xyz; float ao = emissive_ao.w; // Do lighting float3 cpos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]); float3 V = normalize(cpos - wpos); float3 result = float3(0, 0, 0); SurfaceHit sfhit; sfhit.pos = wpos; sfhit.normal = normal; sfhit.dist = length(cpos - wpos); sfhit.surface_spread_angle = ComputeSurfaceSpreadAngle(gbuffer_depth, gbuffer_normal, inv_vp, wpos, normal); // Compute the initial ray cone from the gbuffers. RayCone cone = ComputeRayConeFromGBuffer(sfhit, 1.39626, DispatchRaysDimensions().y); nextRand(rand_seed); const float3 rand_dir = getCosHemisphereSample(rand_seed, normal); const float cos_theta = cos(dot(rand_dir, normal)); result = TraceColorRayCone(wpos + (EPSILON * normal), rand_dir, 0, rand_seed, cone); if (any(isnan(result))) { result = 0; } result = clamp(result, 0, 100); output[DispatchRaysIndex().xy] = float4(result, 1); } #endif //__DXR_PATHTRACER_MAIN_HLSL__ ================================================ FILE: resources/shaders/dxr_raytracing.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_RAYTRACING_HLSL__ #define __DXR_RAYTRACING_HLSL__ #define LIGHTS_REGISTER register(t2) #include "rand_util.hlsl" #include "pbr_util.hlsl" #include "lighting.hlsl" #include "material_util.hlsl" // Definitions for: // - Vertex, Material, Offset // - Ray, RayCone, ReflectionHitInfo #include "dxr_structs.hlsl" // Definitions for: // - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute #include "dxr_functions.hlsl" RWTexture2D gOutput : register(u0); ByteAddressBuffer g_indices : register(t1); StructuredBuffer g_vertices : register(t3); StructuredBuffer g_materials : register(t4); StructuredBuffer g_offsets : register(t5); TextureCube skybox : register(t6); Texture2D brdf_lut : register(t7); TextureCube irradiance_map : register(t8); Texture2D g_textures[1000] : register(t9); SamplerState s0 : register(s0); SamplerState point_sampler : register(s1); typedef BuiltInTriangleIntersectionAttributes MyAttributes; #include "dxr_pathtracer_functions.hlsl" cbuffer CameraProperties : register(b0) { float4x4 view; float4x4 inv_projection_view; float3 camera_position; float padding; float focal_radius; float focal_len; float frame_idx; float intensity; }; inline Ray GenerateCameraRay(uint2 index, in float3 cameraPosition, in float4x4 projectionToWorld, in float2 offset, unsigned int seed) { #ifdef DEPTH_OF_FIELD float2 pixelOff = float2(nextRand(seed), nextRand(seed)); // Random offset in pixel to reduce floating point error. float3 cameraU = float3(1, 0, 0); float3 cameraV = float3(0, 1, 0); float3 cameraW = float3(0, 0, 1); float2 xy = (index + offset + pixelOff) + 0.5f; // center in the middle of the pixel. float2 screenPos = xy / DispatchRaysDimensions().xy; // Invert Y for DirectX-style coordinates. screenPos.y = -screenPos.y; // Unproject the pixel coordinate into a world positon. float4 world = mul(float4(screenPos, 0, 1), projectionToWorld); world.xyz = world.x * cameraU + world.y * cameraV + cameraW; world.xyz /= 1; float2 pixelCenter = (index + offset) / DispatchRaysDimensions().xy; // Pixel ID -> [0..1] over screen float2 ndc = float2(2, -2) * pixelCenter + float2(-1, 1); // Convert to [-1..1] float3 rayDir = ndc.x * cameraU + ndc.y * cameraV + cameraW; // Ray to point on near plane rayDir /= 1; float focallen = focal_len; float lensrad = focal_len / (2.0f * 16); float3 focalPt = cameraPosition + focallen * world; float2 rngLens = float2(6.2831853f * nextRand(seed), lensrad * nextRand(seed)); float2 lensPos = float2(cos(rngLens.x) * rngLens.y, sin(rngLens.x) * rngLens.y); //lensPos = mul(float4(lensPos, 0, 0), projectionToWorld); Ray ray; ray.origin = cameraPosition + float3(lensPos, 0); ray.direction = normalize(focalPt.xyz - ray.origin); #else float2 xy = (index + offset) + 0.5f; // center in the middle of the pixel. float2 screenPos = xy / DispatchRaysDimensions().xy * 2.0 - 1.0; // Invert Y for DirectX-style coordinates. screenPos.y = -screenPos.y; // Unproject the pixel coordinate into a world positon. float4 world = mul(float4(screenPos, 0, 1), projectionToWorld); world.xyz /= world.w; Ray ray; ray.origin = cameraPosition; ray.direction = normalize(world.xyz - ray.origin); return ray; #endif } [shader("raygeneration")] void RaygenEntry() { uint rand_seed = initRand(DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x, frame_idx); #ifdef FOUR_X_AA Ray a = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(0.5, 0), rand_seed); Ray b = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(-0.5, 0), rand_seed); Ray c = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(0.0, 0.5), rand_seed); Ray d = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(0.0, -0.5), rand_seed); float3 result_a = TraceColorRay(a.origin, a.direction, 0, rand_seed); nextRand(rand_seed); float3 result_b = TraceColorRay(b.origin, b.direction, 0, rand_seed); nextRand(rand_seed); float3 result_c = TraceColorRay(c.origin, c.direction, 0, rand_seed); nextRand(rand_seed); float3 result_d = TraceColorRay(d.origin, d.direction, 0, rand_seed); float3 result = (result_a + result_b + result_c + result_d) / 4; #else Ray ray = GenerateCameraRay(DispatchRaysIndex().xy, camera_position, inv_projection_view, float2(0, 0), rand_seed); float3 result = TraceColorRay(ray.origin, ray.direction, 0, rand_seed); #endif if (any(isnan(result))) { result = float3(1000, 0, 0); } float4 prev = gOutput[DispatchRaysIndex().xy]; float4 color = (frame_idx * prev + float4(result, 1)) / (frame_idx + 1); // accumulate gOutput[DispatchRaysIndex().xy] = color; } [shader("miss")] void MissEntry(inout FullRTHitInfo payload) { payload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0).rgb; } [shader("closesthit")] void ClosestHitEntry(inout FullRTHitInfo payload, in MyAttributes attr) { // Calculate the essentials const Offset offset = g_offsets[InstanceID()]; const Material material = g_materials[offset.material_idx]; const float3 hit_pos = HitWorldPosition(); const float index_offset = offset.idx_offset; const float vertex_offset = offset.vertex_offset; // Find first index location const uint index_size = 4; const uint indices_per_triangle = 3; const uint triangle_idx_stride = indices_per_triangle * index_size; uint base_idx = PrimitiveIndex() * triangle_idx_stride; base_idx += index_offset * 4; // offset the start uint3 indices = Load3x32BitIndices(g_indices, base_idx); indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start // Gather triangle vertices const Vertex v0 = g_vertices[indices.x]; const Vertex v1 = g_vertices[indices.y]; const Vertex v2 = g_vertices[indices.z]; // Variables const float3 V = normalize(payload.origin - hit_pos); // Calculate actual "fragment" attributes. const float3 frag_pos = HitAttribute(v0.pos, v1.pos, v2.pos, attr); const float3 normal = normalize(HitAttribute(v0.normal, v1.normal, v2.normal, attr)); const float3 tangent = HitAttribute(v0.tangent, v1.tangent, v2.tangent, attr); const float3 bitangent = HitAttribute(v0.bitangent, v1.bitangent, v2.bitangent, attr); float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; uv.y = 1.0f - uv.y; float mip_level = payload.depth; OutputMaterialData output_data = InterpretMaterialDataRT(material.data, g_textures[material.albedo_id], g_textures[material.normal_id], g_textures[material.roughness_id], g_textures[material.metalicness_id], g_textures[material.emissive_id], g_textures[material.ao_id], mip_level, s0, uv); float3 albedo = output_data.albedo; float roughness = output_data.roughness; float metal = output_data.metallic; float3 emissive = output_data.emissive; float ao = output_data.ao; float3 N = 0; float3 fN = 0; if (payload.depth > 0) { N = normalize(mul(ObjectToWorld3x4(), float4(normal, 0))); fN = N; } else { N = 0; fN = CalcPeturbedNormal(normal, output_data.normal, tangent, bitangent, V, N); } nextRand(payload.seed); payload.color = ggxIndirect(hit_pos, fN, N, V, albedo, metal, roughness, ao, payload.seed, payload.depth + 1); payload.color += ggxDirect(hit_pos, fN, N, V, albedo, metal, roughness, payload.seed, payload.depth + 1); payload.color += emissive; } [shader("closesthit")] void ShadowClosestHitEntry(inout ShadowHitInfo hit, MyAttributes bary) { hit.is_hit = true; } [shader("miss")] void ShadowMissEntry(inout ShadowHitInfo hit : SV_RayPayload) { hit.is_hit = false; } #endif //__DXR_RAYTRACING_HLSL__ ================================================ FILE: resources/shaders/dxr_reflection_entries.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_REFLECTION_ENTRIES_HLSL__ #define __DXR_REFLECTION_ENTRIES_HLSL__ #include "rand_util.hlsl" #include "pbr_util.hlsl" #include "material_util.hlsl" #include "lighting.hlsl" #include "dxr_texture_lod.hlsl" // Definitions for: // - Vertex, Material, Offset // - Ray, RayCone, ReflectionHitInfo #include "dxr_structs.hlsl" // Definitions for: // - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute #include "dxr_functions.hlsl" //Reflections [shader("closesthit")] void ReflectionHit(inout ReflectionHitInfo payload, in Attributes attr) { // Calculate the essentials const Offset offset = g_offsets[InstanceID()]; const Material material = g_materials[offset.material_idx]; const float3 hit_pos = HitWorldPosition(); const float index_offset = offset.idx_offset; const float vertex_offset = offset.vertex_offset; const float3x4 model_matrix = ObjectToWorld3x4(); // Find first index location const uint index_size = 4; const uint indices_per_triangle = 3; const uint triangle_idx_stride = indices_per_triangle * index_size; uint base_idx = PrimitiveIndex() * triangle_idx_stride; base_idx += index_offset * 4; // offset the start uint3 indices = Load3x32BitIndices(g_indices, base_idx); indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start // Gather triangle vertices const Vertex v0 = g_vertices[indices.x]; const Vertex v1 = g_vertices[indices.y]; const Vertex v2 = g_vertices[indices.z]; //Direction & position float3 V = normalize(payload.origin - hit_pos); //Normal mapping const float3 frag_pos = HitAttribute(v0.pos, v1.pos, v2.pos, attr); float3 normal = normalize(HitAttribute(v0.normal, v1.normal, v2.normal, attr)); float3 tangent = HitAttribute(v0.tangent, v1.tangent, v2.tangent, attr); float3 bitangent = HitAttribute(v0.bitangent, v1.bitangent, v2.bitangent, attr); //Get data from VBO float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; uv.y = 1.0f - uv.y; // Propogate the ray cone payload.cone = Propagate(payload.cone, 0, length(payload.origin - hit_pos)); // Calculate the texture LOD level // float mip_level = ComputeTextureLOD( // payload.cone, // V, // normalize(mul(model_matrix, float4(normal, 0))), // mul(model_matrix, float4(v0.pos, 1)), // mul(model_matrix, float4(v1.pos, 1)), // mul(model_matrix, float4(v2.pos, 1)), // v0.uv, // v1.uv, // v2.uv, // g_textures[material.albedo_id]); //TODO: Fixme float mip_level = 0; OutputMaterialData output_data = InterpretMaterialDataRT(material.data, g_textures[material.albedo_id], g_textures[material.normal_id], g_textures[material.roughness_id], g_textures[material.metalicness_id], g_textures[material.emissive_id], g_textures[material.ao_id], mip_level, s0, uv); float3 albedo = output_data.albedo; float roughness = output_data.roughness; float metal = output_data.metallic; float3 emissive = output_data.emissive; float ao = output_data.ao; // Normals float3 fN = CalcPeturbedNormal(normal, output_data.normal, tangent, bitangent, V); float3 flipped_N = fN * -1; const float3 sampled_irradiance = irradiance_map.SampleLevel(s0, flipped_N, 0).xyz; // TODO: reflections in reflections const float3 F = F_SchlickRoughness(max(dot(fN, V), 0.0), metal, albedo, roughness); float3 kS = F; float3 kD = 1.0 - kS; kD *= 1.0 - metal; const float2 sampled_brdf = brdf_lut.SampleLevel(s0, float2(max(dot(fN, V), 0.01f), roughness), 0).rg; //Reflection in reflections float4 dir_t = float4(0, 0, 0, 0); float3 reflection = DoReflection(hit_pos, V, fN, payload.seed, payload.depth + 1, roughness, metal, payload.cone, dir_t).xyz; //Lighting #undef SOFT_SHADOWS float3 lighting = shade_pixel(hit_pos, V, albedo, metal, roughness, emissive, fN, payload.seed, shadow_sample_count, payload.depth + 1, CALLINGPASS_REFLECTIONS); #define SOFT_SHADOWS float3 specular = reflection * (kS * sampled_brdf.x + sampled_brdf.y); float3 diffuse = albedo * sampled_irradiance; float3 ambient = (kD * diffuse + specular) * ao; // Output the final reflections here payload.color = ambient + lighting; payload.hit_t = RayTCurrent(); } //Reflection skybox [shader("miss")] void ReflectionMiss(inout ReflectionHitInfo payload) { payload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0); payload.hit_t = RayTCurrent(); } #endif //__DXR_REFLECTION_ENTRIES_HLSL__ ================================================ FILE: resources/shaders/dxr_reflection_functions.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_REFLECTION_FUNCTIONS_HLSL__ #define __DXR_REFLECTION_FUNCTIONS_HLSL__ #include "dxr_global.hlsl" #include "dxr_structs.hlsl" // Definitions for: // - Vertex, Material, Offset // - Ray, RayCone, ReflectionHitInfo #include "dxr_structs.hlsl" // Definitions for: // - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute #include "dxr_functions.hlsl" #include "pbr_util.hlsl" float3 TraceReflectionRay(float3 origin, float3 norm, float3 direction, uint rand_seed, uint depth, RayCone cone, inout float4 dir_t) { if (depth >= MAX_RECURSION) { return skybox.SampleLevel(s0, direction, 0).rgb; } origin += norm * EPSILON; ReflectionHitInfo payload = {origin, float3(0,0,1), rand_seed, depth, 0, cone}; // Define a ray, consisting of origin, direction, and the min-max distance values RayDesc ray; ray.Origin = origin; ray.Direction = direction; ray.TMin = 0; ray.TMax = 10000.0; bool nan = isnan(origin.x) == true || isnan(origin.y) == true || isnan(origin.z) == true; nan = nan || isnan(direction.x) == true || isnan(direction.y) == true || isnan(direction.z) == true; if(nan) { return skybox.SampleLevel(s0, direction, 0).rgb; } // Trace the ray TraceRay( Scene, RAY_FLAG_NONE, //RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH //RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xFF, // InstanceInclusionMask 0, // RayContributionToHitGroupIndex 1, // MultiplierForGeometryContributionToHitGroupIndex 0, // miss shader index ray, payload); dir_t = float4(direction, payload.hit_t); return payload.color; } float4 DoReflection(float3 wpos, float3 V, float3 N, uint rand_seed, uint depth, float roughness, float metallic, RayCone cone, inout float4 dir_t) { // Calculate ray info float3 reflected = reflect(-V, N); // Shoot an importance sampled ray #ifndef PERFECT_MIRROR_REFLECTIONS // Shoot perfect mirror ray if enabled or if it's a recursion or it's almost a perfect mirror if (depth > 0 || roughness < 0.05f) return float4(TraceReflectionRay(wpos, N, reflected, rand_seed, depth, cone, dir_t), 1); #ifdef GROUND_TRUTH_REFLECTIONS float3 reflection = float3(0.f, 0.f, 0.f); float weight_sum = 0.f; float sampled_count = 0.f; float pdf_weight = 0.f; /* Used for further weighting by spatial reconstruction */ float3 total_hit = float3(0.f, 0.f, 0.f); //[unroll] for (uint i = 0; i < max(MAX_GT_REFLECTION_SAMPLES * metallic, 2); ++i) { nextRand(rand_seed); float2 xi = hammersley2d(rand_seed, 8192); float pdf = 0.f; float3 H = importanceSamplePdf(xi, roughness, N, pdf); float3 L = reflect(-V, H); float NdotL = max(dot(N, L), 0.f); if (NdotL <= 0.f) { nextRand(rand_seed); xi = hammersley2d(rand_seed, 8192); H = importanceSamplePdf(xi, roughness, N, pdf); L = reflect(-V, H); NdotL = max(dot(N, L), 0.f); } if (NdotL >= 0.f) { float weight = brdf_weight(V, L, N, roughness) / pdf; float4 ray_dir_t = float4(0.f, 0.f, 0.f, 0.f); reflection += TraceReflectionRay(wpos, N, L, rand_seed, depth, cone, ray_dir_t) * weight; weight_sum += weight; pdf_weight += pdf; total_hit += ray_dir_t.xyz * ray_dir_t.w; ++sampled_count; } } total_hit /= sampled_count; dir_t = float4(normalize(total_hit), length(total_hit)); return float4(reflection / weight_sum, pdf_weight / sampled_count); #else //Calculate an importance sampled ray nextRand(rand_seed); float2 xi = hammersley2d(rand_seed, 8192); float pdf = 0; float3 H = importanceSamplePdf(xi, roughness, N, pdf); float3 L = reflect(-V, H); float NdotL = max(dot(N, L), 0.f); if(NdotL <= 0.f) { nextRand(rand_seed); xi = hammersley2d(rand_seed, 8192); H = importanceSamplePdf(xi, roughness, N, pdf); L = reflect(-V, H); NdotL = max(dot(N, L), 0.f); } float3 reflection = float3(0.f, 0.f, 0.f); if (NdotL >= 0.f) { reflection = TraceReflectionRay(wpos, N, L, rand_seed, depth, cone, dir_t); } #ifndef DISABLE_SPATIAL_RECONSTRUCTION return float4(reflection, pdf); #else return float4(reflection, -1.f); #endif #endif #else // Shoot perfect mirror ray if enabled or if it's a recursion or it's almost a perfect mirror return float4(TraceReflectionRay(wpos, N, reflected, rand_seed, depth, cone, dir_t), -1.f); #endif } #endif //__DXR_REFLECTION_FUNCTIONS_HLSL__ ================================================ FILE: resources/shaders/dxr_reflection_main.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_REFLECTION_MAIN_HLSL__ #define __DXR_REFLECTION_MAIN_HLSL__ #define LIGHTS_REGISTER register(t2) #include "rand_util.hlsl" #include "pbr_util.hlsl" #include "material_util.hlsl" #include "lighting.hlsl" #include "dxr_texture_lod.hlsl" // Definitions for: // - Vertex, Material, Offset // - Ray, RayCone, ReflectionHitInfo #include "dxr_structs.hlsl" // Definitions for: // - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute #include "dxr_functions.hlsl" RWTexture2D output_reflection : register(u0); // rgb: reflection, a: pdf RWTexture2D output_shadow : register(u1); // r: shadow factor RWTexture2D output_dir_t_buffer : register(u2); // xyz: direction, w: hitT; dirT to calculate hit position ByteAddressBuffer g_indices : register(t1); StructuredBuffer g_vertices : register(t3); StructuredBuffer g_materials : register(t4); StructuredBuffer g_offsets : register(t5); Texture2D g_textures[1000] : register(t10); Texture2D gbuffer_albedo : register(t1010); Texture2D gbuffer_normal : register(t1011); Texture2D gbuffer_depth : register(t1012); TextureCube skybox : register(t6); Texture2D brdf_lut : register(t8); TextureCube irradiance_map : register(t9); SamplerState s0 : register(s0); typedef BuiltInTriangleIntersectionAttributes Attributes; cbuffer CameraProperties : register(b0) { float4x4 inv_view; float4x4 inv_projection; float4x4 inv_vp; float frame_idx; float intensity; float epsilon; unsigned int shadow_sample_count; }; #include "dxr_reflection_functions.hlsl" #include "dxr_reflection_entries.hlsl" #include "dxr_shadow_entries.hlsl" [shader("raygeneration")] void ReflectionRaygenEntry() { uint rand_seed = initRand(DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x, frame_idx); // Texture UV coordinates [0, 1] float2 uv = float2(DispatchRaysIndex().xy + 0.5) / float2(DispatchRaysDimensions().xy); // Screen coordinates [0, resolution] (inverted y) int2 screen_co = DispatchRaysIndex().xy; // Get g-buffer information float4 albedo_roughness = gbuffer_albedo.SampleLevel(s0, uv, 0); float4 normal_metallic = gbuffer_normal.SampleLevel(s0, uv, 0); // Unpack G-Buffer float depth = gbuffer_depth.SampleLevel(s0, uv, 0).x; float3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth, inv_vp); float3 albedo = albedo_roughness.rgb; float roughness = albedo_roughness.w; float3 normal = normal_metallic.xyz; float metallic = normal_metallic.w; // Do lighting float3 cpos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]); float3 V = normalize(cpos - wpos); if (length(normal) == 0) //TODO: Could be optimized by only marking pixels that need lighting, but that would require execute rays indirect { // A value of 1 in the output buffer, means that there is shadow // So, the far plane pixels are set to 0 output_reflection[screen_co] = float4(0, 0, 0, 0); output_shadow[screen_co] = 0; output_dir_t_buffer[screen_co] = float4(0, 0, 0, 0); return; } normal = lerp(normal, -normal, dot(normal, V) < 0); // Describe the surface for mip level generation SurfaceHit sfhit; sfhit.pos = wpos; sfhit.normal = normal; sfhit.dist = length(cpos - wpos); sfhit.surface_spread_angle = ComputeSurfaceSpreadAngle(gbuffer_depth, gbuffer_normal, inv_vp, wpos, normal); // Compute the initial ray cone from the gbuffers. RayCone cone = ComputeRayConeFromGBuffer(sfhit, 1.39626, DispatchRaysDimensions().y); // Get reflection result float4 dir_t = float4(0, 0, 0, 0); float4 reflection_result = min(DoReflection(wpos, V, normal, rand_seed, 0, roughness, metallic, cone, dir_t), 10000); // xyz: reflection, a: shadow factor output_reflection[screen_co] = reflection_result; output_dir_t_buffer[screen_co] = dir_t; } #endif //__DXR_REFLECTION_MAIN_HLSL__ ================================================ FILE: resources/shaders/dxr_shadow_entries.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_SHADOW_ENTRIES_HLSL__ #define __DXR_SHADOW_ENTRIES_HLSL__ #include "pbr_util.hlsl" #include "material_util.hlsl" // Definitions for: // - Vertex, Material, Offset // - Ray, RayCone, ReflectionHitInfo #include "dxr_structs.hlsl" [shader("closesthit")] void ShadowClosestHitEntry(inout ShadowHitInfo hit, Attributes bary) { hit.is_hit = true; } [shader("miss")] void ShadowMissEntry(inout ShadowHitInfo hit : SV_RayPayload) { hit.is_hit = false; } #endif //__DXR_SHADOW_ENTRIES_HLSL__ ================================================ FILE: resources/shaders/dxr_shadow_functions.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_SHADOWS_FUNCTIONS__ #define __DXR_SHADOWS_FUNCTIONS__ #include "dxr_global.hlsl" #include "dxr_structs.hlsl" bool TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pass, unsigned int depth) { if (depth >= MAX_RECURSION) { return false; } // Define a ray, consisting of origin, direction, and the min-max distance values RayDesc ray; ray.Origin = origin; ray.Direction = direction; ray.TMin = 0; ray.TMax = far; ShadowHitInfo payload = { false, 0 }; uint ray_contr_idx = 1; uint miss_idx = 1; if (calling_pass == CALLINGPASS_SHADOWS) { ray_contr_idx = 0; miss_idx = 0; } // Trace the ray TraceRay( Scene, RAY_FLAG_NONE, ~0, // InstanceInclusionMask ray_contr_idx, // RayContributionToHitGroupIndex 0, // MultiplierForGeometryContributionToHitGroupIndex miss_idx, // miss shader index is set to idx but can probably be anything. ray, payload); return payload.is_hit; } // Get shadow factor float GetShadowFactor(float3 wpos, float3 light_dir, float light_size, float t_max, uint sample_count, uint depth, uint calling_pass, inout uint rand_seed) { float shadow_factor = 0.0f; #ifdef SOFT_SHADOWS for (uint i = 0; i < sample_count; ++i) { //float3 offset = normalize(float3(nextRand(rand_seed), nextRand(rand_seed), nextRand(rand_seed))) - 0.5; float3 dir = perturbDirectionVector(rand_seed, light_dir, light_size); float3 ray_direction = normalize(dir); bool shadow = TraceShadowRay(wpos, ray_direction, t_max, calling_pass, depth); shadow_factor += lerp(1.0, 0.0, shadow); } shadow_factor /= float(sample_count); #else //SOFT_SHADOWS bool shadow = TraceShadowRay(wpos, light_dir, t_max, calling_pass, depth); shadow_factor = !shadow; #endif //SOFT_SHADOWS return shadow_factor; } #endif //__DXR_SHADOWS_FUNCTIONS__ ================================================ FILE: resources/shaders/dxr_shadow_main.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_SHADOW_MAIN_HLSL__ #define __DXR_SHADOW_MAIN_HLSL__ #define LIGHTS_REGISTER register(t2) #include "rand_util.hlsl" #include "pbr_util.hlsl" #include "math.hlsl" #include "material_util.hlsl" #include "lighting.hlsl" #include "dxr_texture_lod.hlsl" #include "dxr_global.hlsl" // Definitions for: // - Vertex, Material, Offset // - Ray, RayCone, ReflectionHitInfo #include "dxr_structs.hlsl" // Definitions for: // - HitWorldPosition, Load3x32BitIndices, unpack_position, HitAttribute #include "dxr_functions.hlsl" RWTexture2D output_refl_shadow : register(u0); // xyz: reflection, a: shadow factor ByteAddressBuffer g_indices : register(t1); StructuredBuffer g_vertices : register(t3); StructuredBuffer g_materials : register(t4); StructuredBuffer g_offsets : register(t5); Texture2D g_textures[1000] : register(t10); Texture2D gbuffer_albedo : register(t1010); Texture2D gbuffer_normal : register(t1011); Texture2D gbuffer_depth : register(t1012); TextureCube skybox : register(t6); Texture2D brdf_lut : register(t8); TextureCube irradiance_map : register(t9); SamplerState s0 : register(s0); typedef BuiltInTriangleIntersectionAttributes Attributes; cbuffer CameraProperties : register(b0) { float4x4 inv_view; float4x4 inv_projection; float4x4 inv_vp; float frame_idx; float intensity; float epsilon; unsigned int shadow_sample_count; }; #include "dxr_shadow_functions.hlsl" #include "dxr_shadow_entries.hlsl" [shader("raygeneration")] void ShadowRaygenEntry() { uint rand_seed = initRand(DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x, frame_idx); // Texture UV coordinates [0, 1] float2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1); // Screen coordinates [0, resolution] (inverted y) int2 screen_co = DispatchRaysIndex().xy; // Get g-buffer information float4 albedo_roughness = gbuffer_albedo[screen_co]; float4 normal_metallic = gbuffer_normal[screen_co]; // Unpack G-Buffer float depth = gbuffer_depth[screen_co].x; float3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth, inv_vp); float3 normal = normal_metallic.xyz; // Do lighting float3 cpos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]); float3 V = normalize(cpos - wpos); if (length(normal) == 0) //TODO: Could be optimized by only marking pixels that need lighting, but that would require execute rays indirect { // A value of 1 in the output buffer, means that there is shadow // So, the far plane pixels are set to 0 output_refl_shadow[screen_co] = float4(1, 1, 1, 1); return; } wpos += normal * epsilon; // Get shadow factor float4 shadow_result = DoShadowAllLights(wpos, V, normal, normal_metallic.w, albedo_roughness.w, albedo_roughness.xyz, shadow_sample_count, 0, CALLINGPASS_SHADOWS, rand_seed); // xyz: reflection, a: shadow factor output_refl_shadow[screen_co] = shadow_result; } #endif //__DXR_SHADOW_MAIN_HLSL__ ================================================ FILE: resources/shaders/dxr_structs.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_STRUCTS_HLSL__ #define __DXR_STRUCTS_HLSL__ struct Vertex { float3 pos; float2 uv; float3 normal; float3 tangent; float3 bitangent; }; struct MaterialData { float3 color; float metallic; float roughness; float emissive_multiplier; float is_double_sided; float use_alpha_masking; float albedo_uv_scale; float normal_uv_scale; float roughness_uv_scale; float metallic_uv_scale; float emissive_uv_scale; float ao_uv_scale; float padding; uint flags; }; struct Material { uint albedo_id; uint normal_id; uint roughness_id; uint metalicness_id; uint emissive_id; uint ao_id; float2 padding; MaterialData data; }; struct Offset { uint material_idx; uint idx_offset; uint vertex_offset; }; struct Ray { float3 origin; float3 direction; }; struct RayCone { float width; float spread_angle; }; struct ReflectionHitInfo { float3 origin; float3 color; unsigned int seed; unsigned int depth; float hit_t; RayCone cone; }; struct ShadowHitInfo { float is_hit; float thisvariablesomehowmakeshybridrenderingwork_killme; }; struct PathTracingHitInfo { float3 color; unsigned int seed; float3 origin; unsigned int depth; }; struct PathTracingHitInfoCone { float3 color; unsigned int seed; float3 origin; unsigned int depth; RayCone cone; }; struct FullRTHitInfo { float3 color; unsigned int seed; float3 origin; unsigned int depth; }; struct SurfaceHit { float3 pos; float3 normal; float surface_spread_angle; float dist; }; #endif //__DXR_STRUCTS_HLSL__ ================================================ FILE: resources/shaders/dxr_texture_lod.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __DXR_TEXTURE_LOD_HLSL__ #define __DXR_TEXTURE_LOD_HLSL__ //Definition for RayCone #include "dxr_structs.hlsl" // numbers prefixed with Cha mean its chapter x.x // numbers prefixed with Fig means its figure x.x // pa Fig 20.5 float ComputeTriangleArea(float3 P0, float3 P1, float3 P2) { return length(cross((P1 - P0), (P2 - P0))); } // ta Fig 20.4 float ComputeTextureCoordsArea(float2 UV0, float2 UV1, float2 UV2, Texture2D T) { float w, h; T.GetDimensions(w, h); return 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. return 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 } // Ch 20.6 float GetTriangleLODConstant(float3 P0, float3 P1, float3 P2, float2 UV0, float2 UV1, float2 UV2, Texture2D T) { float P_a = ComputeTriangleArea(P0, P1, P2); float T_a = ComputeTextureCoordsArea(UV0, UV1, UV2, T); return 0.5 * log2(T_a / P_a); } // Ch 20.6 float ComputeTextureLOD(RayCone cone, float3 V, float3 N, float3 P0, float3 P1, float3 P2, float2 UV0, float2 UV1, float2 UV2, Texture2D T) { float w, h; T.GetDimensions(w, h); float lambda = GetTriangleLODConstant(P0, P1, P2, UV0, UV1, UV2, T); lambda += log2(abs(cone.width)); lambda += 0.5 * log2(w * h); lambda -= log2(abs(dot(V, N))); return lambda; } // Fig 20.30 float PixelSpreadAngle(float vertical_fov, float output_height) { return atan((2.f * tan(vertical_fov / 2.f)) / output_height); } float3 dumb_ddx(Texture2D t, float3 v) { float2 pixel = DispatchRaysIndex().xy; float3 top_left = t[float2(pixel.x, pixel.y)].xyz; float3 top_right = t[float2(pixel.x+1, pixel.y)].xyz; float3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz; float3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz; float3 v1 = top_right - top_left; return v1; } float3 dumb_ddy(Texture2D t, float3 v) { float2 pixel = DispatchRaysIndex().xy; float3 top_left = t[float2(pixel.x, pixel.y)].xyz; float3 top_right = t[float2(pixel.x+1, pixel.y)].xyz; float3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz; float3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz; float3 v2 = bottom_left - top_left; return v2; } float3 dumb_ddx_depth(Texture2D t, float4x4 inv_vp, float3 v) { float2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1); uv.y = 1.f - uv.y; float2 pixel = DispatchRaysIndex().xy; float3 top_left = t[float2(pixel.x, pixel.y)].xyz; float3 top_right = t[float2(pixel.x+1, pixel.y)].xyz; float3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz; float3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz; // Get world space position const float4 ndc = float4(uv * 2.0 - 1.0, top_left.x, 1.0); float4 wpos = mul(inv_vp, ndc); float3 retval = (wpos.xyz / wpos.w).xyz; const float4 _ndc = float4(uv * 2.0 - 1.0, top_right.x, 1.0); float4 _wpos = mul(inv_vp, _ndc); float3 _retval = (_wpos.xyz / _wpos.w).xyz; return _retval - retval; } float3 dumb_ddy_depth(Texture2D t, float4x4 inv_vp, float3 v) { float2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1); uv.y = 1.f - uv.y; float2 pixel = DispatchRaysIndex().xy; float3 top_left = t[float2(pixel.x, pixel.y)].xyz; float3 top_right = t[float2(pixel.x+1, pixel.y)].xyz; float3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz; float3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz; // Get world space position const float4 ndc = float4(uv * 2.0 - 1.0, bottom_left.x, 1.0); float4 wpos = mul(inv_vp, ndc); float3 retval = (wpos.xyz / wpos.w).xyz; const float4 _ndc = float4(uv * 2.0 - 1.0, top_right.x, 1.0); float4 _wpos = mul(inv_vp, _ndc); float3 _retval = (_wpos.xyz / _wpos.w).xyz; return retval - _retval; } float3 dumb_ddx_depth2(Texture2D t, float4x4 inv_vp, float3 v) { float2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1); uv.y = 1.f - uv.y; float2 pixel = DispatchRaysIndex().xy; float3 top_left = t[float2(pixel.x, pixel.y)].xyz; float3 top_right = t[float2(pixel.x+1, pixel.y)].xyz; float3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz; float3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz; float3 v1 = top_right - top_left; // Get world space position const float4 ndc = float4(uv * 2.0 - 1.0, v1.x, 1.0); float4 wpos = mul(inv_vp, ndc); float3 retval = (wpos.xyz / wpos.w).xyz; return retval; } float3 dumb_ddy_depth2(Texture2D t, float4x4 inv_vp, float3 v) { float2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1); uv.y = 1.f - uv.y; float2 pixel = DispatchRaysIndex().xy; float3 top_left = t[float2(pixel.x, pixel.y)].xyz; float3 top_right = t[float2(pixel.x+1, pixel.y)].xyz; float3 bottom_left = t[float2(pixel.x, pixel.y+1)].xyz; float3 bottom_right = t[float2(pixel.x+1, pixel.y+1)].xyz; float3 v2 = bottom_left - top_right; // Get world space position const float4 ndc = float4(uv * 2.0 - 1.0, v2.x, 1.0); float4 wpos = mul(inv_vp, ndc); float3 retval = (wpos.xyz / wpos.w).xyz; return retval; } // Fig 20.23 float ComputeSurfaceSpreadAngle(Texture2D g_P, Texture2D g_N, /*Texture2D g_DY, Texture2D g_DY2,*/ float4x4 inv_vp, float3 P, float3 N) { float2 pixel = DispatchRaysIndex().xy; float3 aPx = dumb_ddx_depth2(g_P, inv_vp, P); float3 aPy = dumb_ddy_depth2(g_P, inv_vp, P); //float3 aPx = dumb_ddx(g_P, P); //float3 aPy = dumb_ddy(g_P, P); float3 aNx = dumb_ddx(g_N, N); float3 aNy = dumb_ddy(g_N, N); float k1 = 1; float k2 = 0; float s = sign(dot(aPx, aNx) + dot(aPy, aNy)); // s = g_DY2[pixel].x; // Sign calculated in the deferred main shader. // s = 1 // Sets the sign to convex only. can fix a lot of issues. return 2.f * k1 * s * sqrt(dot(aNx, aNx) + dot(aNy, aNy)) + k2; } // Ch 20.6 RayCone Propagate(RayCone cone, float surface_spread_angle, float hit_dist) { RayCone new_cone; new_cone.width = cone.spread_angle * hit_dist + cone.width; new_cone.spread_angle = cone.spread_angle + surface_spread_angle; return new_cone; } // Ch 20.6 RayCone ComputeRayConeFromGBuffer(SurfaceHit hit, float vertical_fov, float height) { RayCone cone; cone.width = 0; cone.spread_angle = PixelSpreadAngle(vertical_fov, height); return Propagate(cone, hit.surface_spread_angle, hit.dist); } #endif //__DXR_TEXTURE_LOD_HLSL__ ================================================ FILE: resources/shaders/fullscreen_quad.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __FULLSCREEN_QUAD_HLSL__ #define __FULLSCREEN_QUAD_HLSL__ struct VS_OUTPUT { float4 pos : SV_POSITION; float2 uv : TEXCOORD; }; VS_OUTPUT main_vs(float2 pos : POSITION) { VS_OUTPUT output; output.pos = float4(pos.x, pos.y, 0.0f, 1.0f); output.uv = 0.5 * (pos.xy + float2(1.0, 1.0)); return output; } #endif //__FULLSCREEN_QUAD_HLSL__ ================================================ FILE: resources/shaders/generate_mips_cs.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Compute shader to generate mipmaps for a given texture. * Source: https://github.com/Microsoft/DirectX-Graphics-Samples/blob/master/MiniEngine/Core/Shaders/GenerateMipsCS.hlsli */ #define BLOCK_SIZE 8 // When reducing the size of a texture, it could be that downscaling the texture // will result in a less than exactly 50% (1/2) of the original texture size. // This happens if either the width, or the height (or both) dimensions of the texture // are odd. For example, downscaling a 5x3 texture will result in a 2x1 texture which // has a 60% reduction in the texture width and 66% reduction in the height. // When this happens, we need to take more samples from the source texture to // determine the pixel value in the destination texture. #define WIDTH_HEIGHT_EVEN 0 // Both the width and the height of the texture are even. #define WIDTH_ODD_HEIGHT_EVEN 1 // The texture width is odd and the height is even. #define WIDTH_EVEN_HEIGHT_ODD 2 // The texture width is even and teh height is odd. #define WIDTH_HEIGHT_ODD 3 // Both the width and height of the texture are odd. struct ComputeShaderInput { uint3 GroupID : SV_GroupID; // 3D index of the thread group in the dispatch. uint3 GroupThreadID : SV_GroupThreadID; // 3D index of local thread ID in a thread group. uint3 DispatchThreadID : SV_DispatchThreadID; // 3D index of global thread ID in the dispatch. uint GroupIndex : SV_GroupIndex; // Flattened local index of the thread within a thread group. }; cbuffer GenerateMipsCB : register(b0) { uint SrcMipLevel; // Texture level of source mip uint NumMipLevels; // Number of OutMips to write: [1-4] uint SrcDimension; // Width and height of the source texture are even or odd. uint Padding; // Pad to 16 byte alignment. float2 TexelSize; // 1.0 / OutMip1.Dimensions } // Source mip map. Texture2D SrcMip : register(t0); // Write up to 4 mip map levels. RWTexture2D OutMip1 : register(u0); RWTexture2D OutMip2 : register(u1); RWTexture2D OutMip3 : register(u2); RWTexture2D OutMip4 : register(u3); // Linear clamp sampler. SamplerState LinearClampSampler : register(s0); // The reason for separating channels is to reduce bank conflicts in the // local data memory controller. A large stride will cause more threads // to collide on the same memory bank. groupshared float gs_R[64]; groupshared float gs_G[64]; groupshared float gs_B[64]; groupshared float gs_A[64]; void StoreColor(uint Index, float4 Color) { gs_R[Index] = Color.r; gs_G[Index] = Color.g; gs_B[Index] = Color.b; gs_A[Index] = Color.a; } float4 LoadColor(uint Index) { return float4(gs_R[Index], gs_G[Index], gs_B[Index], gs_A[Index]); } float3 LinearToSRGB(float3 x) { // This is exactly the sRGB curve //return x < 0.0031308 ? 12.92 * x : 1.055 * pow(abs(x), 1.0 / 2.4) - 0.055; // This is cheaper but nearly equivalent return x < 0.0031308 ? 12.92 * x : 1.13005 * sqrt(abs(x - 0.00228)) - 0.13448 * x + 0.005719; } float4 PackColor(float4 Linear) { #if defined(CONVERT_TO_SRGB) return float4(LinearToSRGB(Linear.rgb), Linear.a); #else return Linear; #endif } [numthreads(BLOCK_SIZE, BLOCK_SIZE, 1)] void main(ComputeShaderInput IN) { float4 Src1 = (float4)0; // One bilinear sample is insufficient when scaling down by more than 2x. // You will slightly undersample in the case where the source dimension // is odd. This is why it's a really good idea to only generate mips on // power-of-two sized textures. Trying to handle the undersampling case // will force this shader to be slower and more complicated as it will // have to take more source texture samples. // Determine the path to use based on the dimension of the // source texture. // 0b00(0): Both width and height are even. // 0b01(1): Width is odd, height is even. // 0b10(2): Width is even, height is odd. // 0b11(3): Both width and height are odd. switch (SrcDimension) { case WIDTH_HEIGHT_EVEN: { float2 UV = TexelSize * (IN.DispatchThreadID.xy + 0.5); Src1 = SrcMip.SampleLevel(LinearClampSampler, UV, SrcMipLevel); } break; case WIDTH_ODD_HEIGHT_EVEN: { // > 2:1 in X dimension // Use 2 bilinear samples to guarantee we don't undersample when downsizing by more than 2x // horizontally. float2 UV1 = TexelSize * (IN.DispatchThreadID.xy + float2(0.25, 0.5)); float2 Off = TexelSize * float2(0.5, 0.0); Src1 = 0.5 * (SrcMip.SampleLevel(LinearClampSampler, UV1, SrcMipLevel) + SrcMip.SampleLevel(LinearClampSampler, UV1 + Off, SrcMipLevel)); } break; case WIDTH_EVEN_HEIGHT_ODD: { // > 2:1 in Y dimension // Use 2 bilinear samples to guarantee we don't undersample when downsizing by more than 2x // vertically. float2 UV1 = TexelSize * (IN.DispatchThreadID.xy + float2(0.5, 0.25)); float2 Off = TexelSize * float2(0.0, 0.5); Src1 = 0.5 * (SrcMip.SampleLevel(LinearClampSampler, UV1, SrcMipLevel) + SrcMip.SampleLevel(LinearClampSampler, UV1 + Off, SrcMipLevel)); } break; case WIDTH_HEIGHT_ODD: { // > 2:1 in in both dimensions // Use 4 bilinear samples to guarantee we don't undersample when downsizing by more than 2x // in both directions. float2 UV1 = TexelSize * (IN.DispatchThreadID.xy + float2(0.25, 0.25)); float2 Off = TexelSize * 0.5; Src1 = SrcMip.SampleLevel(LinearClampSampler, UV1, SrcMipLevel); Src1 += SrcMip.SampleLevel(LinearClampSampler, UV1 + float2(Off.x, 0.0), SrcMipLevel); Src1 += SrcMip.SampleLevel(LinearClampSampler, UV1 + float2(0.0, Off.y), SrcMipLevel); Src1 += SrcMip.SampleLevel(LinearClampSampler, UV1 + float2(Off.x, Off.y), SrcMipLevel); Src1 *= 0.25; } break; } OutMip1[IN.DispatchThreadID.xy] = PackColor(Src1); // A scalar (constant) branch can exit all threads coherently. if (NumMipLevels == 1) return; // Without lane swizzle operations, the only way to share data with other // threads is through LDS. StoreColor(IN.GroupIndex, Src1); // This guarantees all LDS writes are complete and that all threads have // executed all instructions so far (and therefore have issued their LDS // write instructions.) GroupMemoryBarrierWithGroupSync(); // With low three bits for X and high three bits for Y, this bit mask // (binary: 001001) checks that X and Y are even. if ((IN.GroupIndex & 0x9) == 0) { float4 Src2 = LoadColor(IN.GroupIndex + 0x01); float4 Src3 = LoadColor(IN.GroupIndex + 0x08); float4 Src4 = LoadColor(IN.GroupIndex + 0x09); Src1 = 0.25 * (Src1 + Src2 + Src3 + Src4); OutMip2[IN.DispatchThreadID.xy / 2] = PackColor(Src1); StoreColor(IN.GroupIndex, Src1); } if (NumMipLevels == 2) return; GroupMemoryBarrierWithGroupSync(); // This bit mask (binary: 011011) checks that X and Y are multiples of four. if ((IN.GroupIndex & 0x1B) == 0) { float4 Src2 = LoadColor(IN.GroupIndex + 0x02); float4 Src3 = LoadColor(IN.GroupIndex + 0x10); float4 Src4 = LoadColor(IN.GroupIndex + 0x12); Src1 = 0.25 * (Src1 + Src2 + Src3 + Src4); OutMip3[IN.DispatchThreadID.xy / 4] = PackColor(Src1); StoreColor(IN.GroupIndex, Src1); } if (NumMipLevels == 3) return; GroupMemoryBarrierWithGroupSync(); // This bit mask would be 111111 (X & Y multiples of 8), but only one // thread fits that criteria. if (IN.GroupIndex == 0) { float4 Src2 = LoadColor(IN.GroupIndex + 0x04); float4 Src3 = LoadColor(IN.GroupIndex + 0x20); float4 Src4 = LoadColor(IN.GroupIndex + 0x24); Src1 = 0.25 * (Src1 + Src2 + Src3 + Src4); OutMip4[IN.DispatchThreadID.xy / 8] = PackColor(Src1); } } ================================================ FILE: resources/shaders/lighting.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __LIGHTING_HLSL__ #define __LIGHTING_HLSL__ #define CALLINGPASS_SHADOWS 0 #define CALLINGPASS_REFLECTIONS 1 #define CALLINGPASS_PATHTRACING 2 #define CALLINGPASS_FULLRAYTRACING 3 #include "dxr_shadow_functions.hlsl" struct Light { float3 pos; //Position in world space for spot & point float rad; //Radius for point, height for spot float3 col; //Color uint tid; //Type id; light_type_x float3 dir; //Direction for spot & directional float ang; //Angle for spot; in radians float3 padding; float light_size; }; StructuredBuffer lights : LIGHTS_REGISTER; static uint light_type_point = 0; static uint light_type_directional = 1; static uint light_type_spot = 2; float calc_attenuation(Light light, float3 L, float light_dist) { uint tid = light.tid & 3; float min_cos = cos(light.ang); float max_cos = lerp(min_cos, 1, 0.5f); float cos_angle = dot(light.dir, L); return lerp(smoothstep(min_cos, max_cos, cos_angle), 1.0f - smoothstep(0, light.rad, light_dist), tid != light_type_spot); } //Copied version for testing stuff float3 shade_light(float3 pos, float3 V, float3 albedo, float3 normal, float metallic, float roughness, Light light) { uint tid = light.tid & 3; //Light direction (constant with directional, position dependent with other) float3 L = (lerp(light.pos - pos, light.dir, tid == light_type_directional)); float light_dist = length(L); L /= light_dist; float attenuation = calc_attenuation(light, L, light_dist); float3 radiance = light.col * attenuation; float3 lighting = BRDF(L, V, normal, metallic, roughness, albedo, radiance); return lighting; } float3 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) { float3 res = float3(0.0f, 0.0f, 0.0f); uint light_count = lights[0].tid >> 2; //Light count is stored in 30 upper-bits of first light if(!uses_luminance) { for (uint i = 0; i < light_count; i++) { res += shade_light(pos, V, albedo, normal, metallic, roughness, lights[i]); } } else { res = albedo * shadow_factor; } // Ambient Lighting using Irradiance for Diffuse float3 kS = F_SchlickRoughness(max(dot(normal, V), 0.0f), metallic, albedo, roughness); float3 kD = 1.0f - kS; kD *= 1.0f - metallic; float3 diffuse = irradiance * albedo; // Image-Based Lighting using Prefiltered Environment Map and BRDF LUT for Specular float3 prefiltered_color = reflection; float2 sampled_brdf = brdf; float3 specular = prefiltered_color * (kS * sampled_brdf.x + sampled_brdf.y); //float3 specular = reflection * kS; float3 ambient = (kD * diffuse + specular) * ao; return ambient + res + emissive; } float3 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) { uint tid = light.tid & 3; //Light direction (constant with directional, position dependent with other) float3 L = (lerp(light.pos - pos, light.dir, tid == light_type_directional)); float light_dist = length(L); L /= light_dist; float attenuation = calc_attenuation(light, L, light_dist); // Maybe change hard-coded 100000 to be dynamic according to the scene size? float t_max = lerp(light_dist, 100000, tid == light_type_directional); float3 radiance = light.col * attenuation; float3 lighting = BRDF(L, V, normal, metallic, roughness, albedo, radiance); float3 wpos = pos + (normal * EPSILON); float shadow_factor = GetShadowFactor(wpos, L, light.light_size, t_max, shadow_sample_count, depth, calling_pass, rand_seed); lighting *= shadow_factor; return lighting; } float3 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) { uint light_count = lights[0].tid >> 2; //Light count is stored in 30 upper-bits of first light float3 res = float3(0, 0, 0); [unroll] for (uint i = 0; i < light_count; i++) { res += shade_light(pos, V, albedo, normal, metallic, roughness, lights[i], rand_seed, shadow_sample_count, depth, calling_pass); } return res + emissive; } float4 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) { uint light_count = lights[0].tid >> 2; //Light count is stored in 30 upper-bits of first light float4 res = float4(0.0, 0.0, 0.0, 0.0); uint sampled_lights = 0; for (uint i = 0; i < light_count; i++) { // Get light and light type Light light = lights[i]; uint tid = light.tid & 3; //Light direction (constant with directional, position dependent with other) float3 L = (lerp(light.pos - wpos, light.dir, tid == light_type_directional)); float light_dist = length(L); L /= light_dist; float dir_dot = dot(L, normal); if (dir_dot < 0.0f) { continue; } float attenuation = calc_attenuation(light, L, light_dist); if (light_dist > light.rad && tid != light_type_directional) { continue; } float3 radiance = attenuation * light.col; float3 lighting = BRDF(L, V, normal, metallic, roughness, float3(1.0, 1.0, 1.0), radiance) /* / max(albedo, float3(0.001, 0.001, 0.001)) */; // Get maxium ray length (depending on type) float t_max = lerp(light_dist, 100000, tid == light_type_directional); // Add shadow factor to final result float shadow = GetShadowFactor(wpos, L, light.light_size, t_max, shadow_sample_count, depth, calling_pass, rand_seed); res.w += shadow; res.rgb += lighting * shadow; sampled_lights++; } // return final res res.w = res.w / float(sampled_lights); return res; } #endif //__LIGHTING_HLSL__ ================================================ FILE: resources/shaders/material_util.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __MATERIAL_UTIL_HLSL__ #define __MATERIAL_UTIL_HLSL__ #define MATERIAL_HAS_ALBEDO_TEXTURE 1<<0 #define MATERIAL_HAS_NORMAL_TEXTURE 1<<1 #define MATERIAL_HAS_ROUGHNESS_TEXTURE 1<<2 #define MATERIAL_HAS_METALLIC_TEXTURE 1<<3 #define MATERIAL_HAS_EMISSIVE_TEXTURE 1<<4 #define MATERIAL_HAS_AO_TEXTURE 1<<5 #include "dxr_structs.hlsl" struct OutputMaterialData { float3 albedo; float alpha; float roughness; float3 normal; float metallic; float3 emissive; float ao; }; OutputMaterialData InterpretMaterialData(MaterialData data, Texture2D material_albedo, Texture2D material_normal, Texture2D material_roughness, Texture2D material_metallic, Texture2D material_emissive, Texture2D material_ambient_occlusion, SamplerState s0, float2 uv ) { OutputMaterialData output; float use_albedo_texture = float((data.flags & MATERIAL_HAS_ALBEDO_TEXTURE) != 0); float use_roughness_texture = float((data.flags & MATERIAL_HAS_ROUGHNESS_TEXTURE) != 0); float use_metallic_texture = float((data.flags & MATERIAL_HAS_METALLIC_TEXTURE) != 0); float use_normal_texture = float((data.flags & MATERIAL_HAS_NORMAL_TEXTURE) != 0); float use_emissive_texture = float((data.flags & MATERIAL_HAS_EMISSIVE_TEXTURE) != 0); float use_ao_texture = float((data.flags & MATERIAL_HAS_AO_TEXTURE) != 0); float4 albedo = lerp(float4(data.color, 1), material_albedo.Sample(s0, uv * data.albedo_uv_scale), use_albedo_texture); #ifdef COMPRESSED float roughness = lerp(data.roughness, max(0.05f, material_roughness.Sample(s0, uv * data.roughness_uv_scale).y), use_roughness_texture); float metallic = lerp(data.metallic, material_metallic.Sample(s0, uv * data.metallic_uv_scale).z, use_metallic_texture); #else float roughness = lerp(data.roughness, max(0.05f, material_roughness.Sample(s0, uv * data.roughness_uv_scale).x), use_roughness_texture); float metallic = lerp(data.metallic, material_metallic.Sample(s0, uv * data.metallic_uv_scale).x, use_metallic_texture); #endif float3 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); float3 emissive = lerp(float3(0.0f, 0.0f, 0.0f), material_emissive.Sample(s0, uv * data.emissive_uv_scale).xyz, use_emissive_texture); float ao = lerp(1.0f, material_ambient_occlusion.Sample(s0, uv * data.ao_uv_scale).x, use_ao_texture); output.albedo = pow(albedo.xyz, 2.2f); output.alpha = albedo.w; output.roughness = roughness; output.normal = tex_normal; output.metallic = metallic; output.emissive = pow(emissive,2.2f) * data.emissive_multiplier; output.ao = ao; return output; } OutputMaterialData InterpretMaterialDataRT(MaterialData data, Texture2D material_albedo, Texture2D material_normal, Texture2D material_roughness, Texture2D material_metallic, Texture2D material_emissive, Texture2D material_ambient_occlusion, float mip_level, SamplerState s0, float2 uv) { OutputMaterialData output; float use_albedo_texture = float((data.flags & MATERIAL_HAS_ALBEDO_TEXTURE) != 0); float use_roughness_texture = float((data.flags & MATERIAL_HAS_ROUGHNESS_TEXTURE) != 0); float use_metallic_texture = float((data.flags & MATERIAL_HAS_METALLIC_TEXTURE) != 0); float use_normal_texture = float((data.flags & MATERIAL_HAS_NORMAL_TEXTURE) != 0); float use_emissive_texture = float((data.flags & MATERIAL_HAS_EMISSIVE_TEXTURE) != 0); float use_ao_texture = float((data.flags & MATERIAL_HAS_AO_TEXTURE) != 0); const float4 albedo = lerp(float4(data.color, 1), material_albedo.SampleLevel(s0, uv * (data.albedo_uv_scale), mip_level), use_albedo_texture); #ifdef COMPRESSED const float roughness = lerp(data.roughness, max(0.05, material_roughness.SampleLevel(s0, uv * data.roughness_uv_scale, mip_level).z), use_roughness_texture); const float metallic = lerp(data.metallic, material_metallic.SampleLevel(s0, uv * data.metallic_uv_scale, mip_level).y, use_metallic_texture); #else const float roughness = lerp(data.roughness, max(0.05, material_roughness.SampleLevel(s0, uv * data.roughness_uv_scale, mip_level).x), use_roughness_texture); const float metallic = lerp(data.metallic, material_metallic.SampleLevel(s0, uv * data.metallic_uv_scale, mip_level).x, use_metallic_texture); #endif const float3 normal_t = lerp(float3(0.0, 0.0, 1.0), material_normal.SampleLevel(s0, uv * data.normal_uv_scale, mip_level).xyz * 2 - 1, use_normal_texture); float3 emissive = lerp(float3(0.0f, 0.0f, 0.0f), material_emissive.SampleLevel(s0, uv * data.emissive_uv_scale, mip_level).xyz, use_emissive_texture); float ao = lerp(1.0f, material_ambient_occlusion.SampleLevel(s0, uv * data.ao_uv_scale, mip_level).x, use_ao_texture); output.albedo = pow(albedo.xyz, 2.2f); output.alpha = albedo.w; output.roughness = roughness; output.normal = normal_t; output.metallic = metallic; output.emissive = pow(emissive, 2.2f) * data.emissive_multiplier; output.ao = ao; return output; } #endif //__MATERIAL_UTIL_HLSL__ ================================================ FILE: resources/shaders/math.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __MATH_HLSL__ #define __MATH_HLSL__ #define M_PI 3.14159265358979 float linterp(float t) { return saturate(1.0 - abs(2.0 * t - 1.0)); } float remap(float t, float a, float b) { return saturate((t - a) / (b - a)); } float4 spectrum_offset(float t) { float4 ret; float lo = step(t, 0.5); float hi = 1.0 - lo; float w = linterp(remap(t, 1.0 / 6.0, 5.0 / 6.0)); ret = float4(lo, 1.0, hi, 1.) * float4(1.0 - w, w, 1.0 - w, 1.); return pow(ret, float4(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2)); } #endif //__MATH_HLSL__ ================================================ FILE: resources/shaders/pbr_brdf_lut.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "rand_util.hlsl" #include "pbr_util.hlsl" RWTexture2D output : register(u0); float2 IntegrateBRDF(float NdotV, float roughness) { float3 V; V.x = sqrt(1.0f - NdotV * NdotV); V.y = 0.0f; V.z = NdotV; float A = 0.0f; float B = 0.0f; float3 N = float3(0.0f, 0.0f, 1.0f); const uint SAMPLE_COUNT = 1024u; for (uint i = 0; i < SAMPLE_COUNT; ++i) { float2 Xi = hammersley2d(i, SAMPLE_COUNT); float3 H = importanceSample_GGX(Xi, roughness, N); float3 L = normalize(2.0f * dot(V, H) * H - V); float NdotL = max(L.z, 0.0f); float NdotH = max(H.z, 0.0f); float VdotH = max(dot(V, H), 0.0f); float NdotV = max(dot(N, V), 0.0f); if (NdotL > 0.0f) { float G = GeometrySmith_IBL(NdotV, NdotL, roughness); float G_Vis = (G * VdotH) / (NdotH * NdotV); float Fc = pow(1.0f - VdotH, 5.0f); A += (1.0f - Fc) * G_Vis; B += Fc * G_Vis; } } A /= float(SAMPLE_COUNT); B /= float(SAMPLE_COUNT); return float2(A, B); } [numthreads(16, 16, 1)] void main_cs(uint3 dt_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); float2 screen_coord = int2(dt_id.x, dt_id.y) + 0.5f; float2 uv = screen_coord / screen_size; output[dt_id.xy] = IntegrateBRDF(uv.x, uv.y); } ================================================ FILE: resources/shaders/pbr_cubemap_conversion.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ struct VS_INPUT { float3 pos : POSITION; //float2 uv : TEXCOORD; //float3 normal : NORMAL; //float3 tangent : TANGENT; //float3 bitangent : BITANGENT; }; struct VS_OUTPUT { float4 pos : SV_POSITION; float3 local_pos : LOCPOS; }; cbuffer PassIndex : register (b0) { int idx; } cbuffer CameraProperties : register(b1) { float4x4 projection; float4x4 view[6]; }; VS_OUTPUT main_vs(VS_INPUT input) { VS_OUTPUT output; output.local_pos = input.pos.xyz; float4x4 vp = mul(projection, view[idx]); output.pos = mul(vp, float4(output.local_pos, 1.0f)); return output; } struct PS_OUTPUT { float4 color; }; Texture2D equirectangular_texture : register(t0); SamplerState s0 : register(s0); float2 SampleSphericalMap(float3 v) { float2 inv_atan = float2(0.1591f, 0.3183f); float2 uv = float2(atan2(v.z, v.x), asin(v.y)); uv *= inv_atan; uv += 0.5f; return uv; } PS_OUTPUT main_ps(VS_OUTPUT input) : SV_TARGET { PS_OUTPUT output; float2 uv = SampleSphericalMap(normalize(input.local_pos)); float3 color = equirectangular_texture.Sample(s0, uv).rgb; output.color = float4(color, 1.0f); return output; } ================================================ FILE: resources/shaders/pbr_cubemap_convolution.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ struct VS_INPUT { float3 pos : POSITION; float2 uv : TEXCOORD; float3 normal : NORMAL; float3 tangent : TANGENT; float3 bitangent : BITANGENT; }; struct VS_OUTPUT { float4 pos : SV_POSITION; float3 local_pos : LOCPOS; }; cbuffer PassIndex : register (b0) { int idx; } cbuffer CameraProperties : register(b1) { float4x4 projection; float4x4 view[6]; }; VS_OUTPUT main_vs(VS_INPUT input) { VS_OUTPUT output; output.local_pos = input.pos.xyz; float4x4 vp = mul(projection, view[idx]); output.pos = mul(vp, float4(output.local_pos, 1.0f)); return output; } struct PS_OUTPUT { float4 color; }; TextureCube environment_cubemap : register(t0); SamplerState s0 : register(s0); PS_OUTPUT main_ps(VS_OUTPUT input) : SV_TARGET { PS_OUTPUT output; const float PI = 3.14159265359f; float3 normal = normalize(input.local_pos); float3 irradiance = float3(0.0f, 0.0f, 0.0f); float3 up = float3(0.0f, 1.0f, 0.0f); float3 right = cross(up, normal); up = cross(normal, right); float sample_delta = 0.025f; float nr_samples = 0.0f; for (float phi = 0.0f; phi < 2.0f * PI; phi += sample_delta) { for (float theta = 0.0f; theta < 0.5f * PI; theta += sample_delta) { float cos_theta = cos(theta); float sin_theta = sin(theta); float3 tangent_sample = float3(sin_theta * cos(phi), sin_theta * sin(phi), cos_theta); float3 sample_vec = tangent_sample.x * right + tangent_sample.y * up + tangent_sample.z * normal; irradiance += environment_cubemap.Sample(s0, sample_vec).rgb * cos_theta * sin_theta; nr_samples++; } } irradiance = PI * irradiance * (1.0f / float(nr_samples)); output.color = float4(irradiance.rgb, 1.0f); return output; } ================================================ FILE: resources/shaders/pbr_prefilter_env_map.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "rand_util.hlsl" #include "pbr_util.hlsl" TextureCube src_texture : register(t0); RWTexture2D dst_texture : register(u0); SamplerState s0 : register(s0); cbuffer CB : register(b0) { float2 texture_size; float2 skybox_res; float roughness; uint cubemap_face; } [numthreads(8, 8, 1)] void main_cs(uint3 dt_id : SV_DispatchThreadID) { float2 position = float2(dt_id.xy + 0.5f) / texture_size; position.y = 1.0f - position.y; position = (position - 0.5f) * 2.0f; float3 direction = float3(0.0f, 0.0f, 0.0f); float3 up = float3(0.0f, 0.0f, 0.0f); switch (cubemap_face) { case 0: direction = float3(1.0f, position.y, -position.x); up = float3(0.0f, 1.0f, 0.0f); break; // +X case 1: direction = float3(-1.0f, position.y, position.x); up = float3(0.0f, 1.0f, 0.0f); break; // -X case 2: direction = float3(position.x, 1.0f, -position.y); up = float3(0.0f, 0.0f, -1.0f); break; // +Y case 3: direction = float3(position.x, -1.0f, position.y); up = float3(0.0f, 0.0f, 1.0f); break; // -Y case 4: direction = float3(position.x, position.y, 1.0f); up = float3(0.0f, 1.0f, 0.0f); break; // +Z case 5: direction = float3(-position.x, position.y, -1.0f); up = float3(0.0f, 1.0f, 0.0f); break; // -Z } float3 N = normalize(direction); float3 right = normalize(cross(up, N)); up = cross(N, right); float3 R = N; float3 V = R; const uint SAMPLE_COUNT = 1024u; float total_weight = 0.0f; float3 prefiltered_color = float3(0.0f, 0.0f, 0.0f); for (uint i = 0u; i < SAMPLE_COUNT; i++) { float2 Xi = hammersley2d(i, SAMPLE_COUNT); float3 H = importanceSample_GGX(Xi, roughness, N); float3 L = normalize(2.0f * dot(V, H) * H - V); float NdotL = max(dot(N, L), 0.0f); if (NdotL > 0.0f) { float NdotH = max(dot(N, H), 0.0f); float HdotV = max(dot(H, V), 0.0f); float D = D_GGX(NdotH, roughness); float pdf = D * NdotH / (4.0f * HdotV) + 0.0001f; float sa_texel = 4.0f * M_PI / (6.0f * skybox_res.x * skybox_res.y); float sa_sample = 1.0f / (float(SAMPLE_COUNT) * pdf + 0.0001f); float mip_level = roughness == 0.0f ? 0.0f : 0.5f * log2(sa_sample / sa_texel); prefiltered_color += src_texture.SampleLevel(s0, L, mip_level).rgb * NdotL; total_weight += NdotL; } } prefiltered_color = prefiltered_color / total_weight; //Write the final color into the destination texture. dst_texture[dt_id.xy] = float4(prefiltered_color, 1.0f); } ================================================ FILE: resources/shaders/pbr_util.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PBR_UTILS_HLSL__ #define __PBR_UTILS_HLSL__ #include "math.hlsl" #include "rand_util.hlsl" // Based omn http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/ float random(float2 co) { float a = 12.9898; float b = 78.233; float c = 43758.5453; float dt = dot(co.xy, float2(a, b)); float sn = fmod(dt, 3.14); return frac(sin(sn) * c); } // Radical inverse based on http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html float2 hammersley2d(uint i, uint num) { uint bits = (i << 16u) | (i >> 16u); bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); float rdi = float(bits) * 2.3283064365386963e-10; return float2(float(i) / float(num), rdi); } // Based on http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_slides.pdf float3 importanceSample_GGX(float2 Xi, float roughness, float3 normal) { // Maps a 2D point to a hemisphere with spread based on roughness float alpha = roughness * roughness; //float phi = 2.f * M_PI * Xi.x + random(normal.xz) * 0.1; float phi = 2.f * M_PI * Xi.x; float cosTheta = sqrt((1.f - Xi.y) / (1.f + (alpha*alpha - 1.f) * Xi.y)); float sinTheta = sqrt(1.f - cosTheta * cosTheta); float3 H; H.x = sinTheta * cos(phi); H.y = sinTheta * sin(phi); H.z = cosTheta; // Tangent space float3 up = abs(normal.z) < 0.999 ? float3(0.f, 0.f, 1.f) : float3(1.f, 0.f, 0.f); float3 tangentX = normalize(cross(up, normal)); float3 tangentY = cross(normal, tangentX); // Convert to world Space return normalize(tangentX * H.x + tangentY * H.y + normal * H.z); } // Normal distribution float D_GGX(float dotNH, float roughness) { float alpha = roughness * roughness; float alpha2 = alpha * alpha; float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0; return (alpha2)/(M_PI * denom * denom); } // Get a GGX half vector / microfacet normal, sampled according to the distribution computed by // the function ggxNormalDistribution() above. // // When using this function to sample, the probability density is pdf = D * NdotH / (4 * HdotV) float3 getGGXMicrofacet(inout uint randSeed, float roughness, float3 hitNorm) { // Get our uniform random numbers float2 randVal = float2(nextRand(randSeed), nextRand(randSeed)); // Get an orthonormal basis from the normal float3 B = getPerpendicularVector(hitNorm); float3 T = cross(B, hitNorm); // GGX NDF sampling float a2 = roughness * roughness; float cosThetaH = sqrt(max(0.0f, (1.0 - randVal.x) / ((a2 - 1.0) * randVal.x + 1))); float sinThetaH = sqrt(max(0.0f, 1.0f - cosThetaH * cosThetaH)); float phiH = randVal.y * M_PI * 2.0f; // Get our GGX NDF sample (i.e., the half vector) return T * (sinThetaH * cos(phiH)) + B * (sinThetaH * sin(phiH)) + hitNorm * cosThetaH; } // Geometric Shadowing function float G_SchlicksmithGGX(float NdotL, float NdotV, float roughness) { float r = (roughness + 1.0f); float k = (r*r) / 8.0f; float GL = NdotL / (NdotL * (1.0f - k) + k); float GV = NdotV / (NdotV * (1.0f - k) + k); return GL * GV; } float GeometrySchlickGGX_IBL(float NdotV, float roughness_squared) { // Different k for IBL float k = roughness_squared / 2.0; float nom = NdotV; float denom = NdotV * (1.0 - k) + k; return nom / denom; } // ---------------------------------------------------------------------------- float GeometrySmith_IBL(float NdotV, float NdotL, float roughness) { float roughness_squared = roughness * roughness; float ggx2 = GeometrySchlickGGX_IBL(NdotV, roughness_squared); float ggx1 = GeometrySchlickGGX_IBL(NdotL, roughness_squared); return ggx1 * ggx2; } // Fresnel function float3 F_Schlick(float cos_theta, float metallic, float3 material_color) { float3 F0 = lerp(float3(0.04, 0.04, 0.04), material_color, metallic); // * material.specular float3 F = F0 + (1.0 - F0) * pow(1.0 - cos_theta, 5.0); return F; } float3 F_SchlickRoughness(float cos_theta, float metallic, float3 material_color, float roughness) { float3 F0 = lerp(float3(0.04f, 0.04f, 0.04f), material_color, metallic); // * material.specular float3 F = F0 + (max(float3(1.0f - roughness, 1.0f - roughness, 1.0f - roughness), F0) - F0) * pow(1.0f - cos_theta, 5.0f); return F; } float3 BRDF(float3 L, float3 V, float3 N, float metallic, float roughness, float3 albedo, float3 radiance) { // Precalculate vectors and dot products float3 H = normalize(V + L); float dotNV = clamp(dot(N, V), 0.0, 1.0); float dotNL = clamp(dot(N, L), 0.0, 1.0); float dotNH = clamp(dot(N, H), 0.0, 1.0); // Light color fixed float3 color = float3(0.0, 0.0, 0.0); if (dotNL > 0.0) { float rroughness = max(0.05f, roughness); // D = Normal distribution (Distribution of the microfacets) float D = D_GGX(dotNH, roughness); // G = Geometric shadowing term (Microfacets shadowing) float G = G_SchlicksmithGGX(dotNL, dotNV, roughness); // F = Fresnel factor (Reflectance depending on angle of incidence) float3 F = F_Schlick(dotNH, metallic, albedo); float3 spec = (D * F * G) / ((4.0 * dotNL * dotNV + 0.001f)); float3 kD = (float3(1, 1, 1) - F) * (1.0 - metallic); color += (kD * albedo / M_PI + spec) * radiance * dotNL; } return color; } static const float2 inv_atan = float2(0.1591f, 0.3183f); float2 SampleSphericalMap(float3 v) { float2 uv = float2(atan2(v.z, v.x), asin(v.y * -1.f)); uv *= inv_atan; uv += 0.5f; return uv; } // Brian Karis, Epic Games "Real Shading in Unreal Engine 4" // Modified version to do pdf and tangent to world conversions float3 importanceSamplePdf(float2 xi, float a, float3 N, inout float pdf) { float m = a * a; float m2 = m * m; float phi = 2 * M_PI * xi.x; float cosTheta = sqrt((1.0 - xi.y) / (1.0 + (m2 - 1.0) * xi.y)); float sinTheta = sqrt(max(1e-5, 1.0 - cosTheta * cosTheta)); float3 H; H.x = sinTheta * cos(phi); H.y = sinTheta * sin(phi); H.z = cosTheta; float d = (cosTheta * m2 - cosTheta) * cosTheta + 1; float D = m2 / (M_PI * d * d); pdf = D * cosTheta; float3 up = lerp(float3(1.0, 0.0, 0.0), float3(0.0, 0.0, 1.0), float(abs(N.z) < 0.999)); float3 T = normalize(cross(up, N)); float3 B = cross(N, T); return normalize(T * H.x + B * H.y + N * H.z); } //Get weight from roughness, view direction, light direction and normal (view space) float brdf_weight(float3 V, float3 L, float3 N, float roughness) { float3 H = normalize(V + L); float NdotH = saturate(dot(N, H)); float NdotL = saturate(dot(N, L)); float NdotV = saturate(dot(N, V)); float G = G_SchlicksmithGGX(NdotL, NdotV, roughness); //This causes issues float D = D_GGX(NdotH, roughness); float weight = G * D * M_PI / 4; return max(weight, 1e-5); //Perfect mirrors can have weights too } #endif ================================================ FILE: resources/shaders/pp_bloom_blur.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_BLOOM_BLUR_HLSL__ #define __PP_BLOOM_BLUR_HLSL__ #include "pp_dof_util.hlsl" #include "pp_bloom_util.hlsl" Texture2D source : register(t0); RWTexture2D output : register(u0); SamplerState s0 : register(s0); cbuffer BloomDirection : register(b0) { int blur_direction; float sigma_amt; }; [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f; float2 texel_size = 1.0f / screen_size; float2 uv = (screen_coord) / screen_size; float sigma = sigma_amt - 1.0f; float2 blur_dir = float2(0.0f, 1.0f); if (blur_direction == 1) { blur_dir = float2(1.0f, 0.0f); } float4 color = 0; float weightSum = 0.0f; for (int i = -7; i < 7; i++) { float weight = CalcGaussianWeight(i, sigma); weightSum += weight; float2 o_uv = saturate((screen_coord + (blur_dir * i)) / screen_size); float4 s = source.SampleLevel(s0, o_uv, 0); color += s * weight; } output[int2(dispatch_thread_id.xy)] = color; } #endif //__PP_BLOOM_BLUR_HLSL__ ================================================ FILE: resources/shaders/pp_bloom_blur_horizontal.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_BLOOM_BLUR_HORIZONTAL_HLSL__ #define __PP_BLOOM_BLUR_HORIZONTAL_HLSL__ #include "pp_dof_util.hlsl" #include "pp_bloom_util.hlsl" Texture2D source : register(t0); RWTexture2D output : register(u0); RWTexture2D output_qes : register(u1); SamplerState s0 : register(s0); cbuffer BloomDirection : register(b0) { float2 blur_direction; float _pad; float sigma; }; float4 GetBlurFactor(float2 screen_coord, float res_scale) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); float2 blur_dir = float2(1.0f, 0.0f); float r_sigma = 3.0f; float4 color = 0; float weightSum = 0.0f; for (int i = -7; i < 7; i++) { float weight = CalcGaussianWeight(i, r_sigma); weightSum += weight; float2 o_uv = saturate((screen_coord + (blur_dir * i)) / (screen_size / res_scale)); float4 s = source.SampleLevel(s0, o_uv, 0); color += s * weight; } return color; } [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f; float4 color = 0; if (screen_coord.x > screen_size.x) { if (screen_coord.x > (screen_size.x * 1.875f)) { color = GetBlurFactor(screen_coord - screen_size * 1.875f, 16.0f); } if (screen_coord.x > (screen_size.x * 1.75)) { color = GetBlurFactor(screen_coord - screen_size * 1.75f, 8.0f); } else if (screen_coord.x > (screen_size.x * 1.5)) { color = GetBlurFactor(screen_coord - screen_size * 1.5f, 4.0f); } else { color = GetBlurFactor(screen_coord - screen_size, 2.0f); } output_qes[screen_coord - int2(screen_size)] = color; } else { color = GetBlurFactor(screen_coord, 1.0f); output[screen_coord] = color; } } #endif //__PP_BLOOM_BLUR_HORIZONTAL_HLSL__ ================================================ FILE: resources/shaders/pp_bloom_blur_vertical.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_BLOOM_BLUR_VERTICAL_HLSL__ #define __PP_BLOOM_BLUR_VERTICAL_HLSL__ #include "pp_dof_util.hlsl" #include "pp_bloom_util.hlsl" Texture2D source : register(t0); Texture2D source_qes : register(t1); RWTexture2D output : register(u0); RWTexture2D output_qes : register(u1); SamplerState s0 : register(s0); cbuffer BloomDirection : register(b0) { float2 blur_direction; float _pad; float sigma; }; float4 GetBlurFactor(float2 screen_coord, float res_scale) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); float2 blur_dir = float2(0.0f, 1.0f); float r_sigma = 3.0f; float4 color = 0; float weightSum = 0.0f; for (int i = -7; i < 7; i++) { float weight = CalcGaussianWeight(i, r_sigma); weightSum += weight; float2 o_uv = saturate((screen_coord + (blur_dir * i)) / (screen_size / res_scale)); float4 s = source.SampleLevel(s0, o_uv, 0); color += s * weight; } return color; } float4 GetBlurFactorQES(float2 screen_coord) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); float2 blur_dir = float2(0.0f, 1.0f); float r_sigma = 3.0f; float4 color = 0; float weightSum = 0.0f; for (int i = -7; i < 7; i++) { float weight = CalcGaussianWeight(i, r_sigma); weightSum += weight; float4 s = source_qes[screen_coord + blur_dir * i].rgba; color += s * weight; } return color; } [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f; float4 color = 0; if (screen_coord.x > screen_size.x) { color = GetBlurFactorQES(screen_coord - screen_size); output_qes[screen_coord - int2(screen_size)] = color; } else { color = GetBlurFactor(screen_coord, 1.0f); output[screen_coord] = color; } } #endif //__PP_BLOOM_BLUR_VERTICAL_HLSL__ ================================================ FILE: resources/shaders/pp_bloom_composition.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_BLOOM_COMPOSITION_HLSL__ #define __PP_BLOOM_COMPOSITION_HLSL__ #include "pp_hdr_util.hlsl" Texture2D source_main : register(t0); Texture2D source_bloom_half : register(t1); Texture2D source_bloom_qes : register(t2); RWTexture2D output : register(u0); SamplerState linear_sampler : register(s0); SamplerState point_sampler : register(s1); cbuffer Bloomproperties : register(b0) { int enable_bloom; }; [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f; float2 texel_size = 1.0f / screen_size; float2 uv = screen_coord / screen_size; float2 uv_half = (screen_coord / 2 ) / screen_size; float2 uv_quarter = (screen_coord / 4) / screen_size + 0.5f; float2 uv_eighth = (screen_coord / 8) / screen_size + 0.75f; float3 finalcolor = float3(0, 0, 0); float bloom_intensity = 1.f; if (enable_bloom > 0) { finalcolor += source_bloom_half.SampleLevel(linear_sampler, uv, 0).rgb; finalcolor += source_bloom_qes.SampleLevel(linear_sampler, uv_half, 0).rgb; finalcolor += source_bloom_qes.SampleLevel(linear_sampler, uv_quarter, 0).rgb; finalcolor += source_bloom_qes.SampleLevel(linear_sampler, uv_eighth, 0).rgb; finalcolor *= 0.25f; } finalcolor += source_main.SampleLevel(point_sampler, uv, 0).rgb; output[int2(dispatch_thread_id.xy)] = float4(finalcolor, 1.0f); } #endif //__PP_BLOOM_COMPOSITION_HLSL__ ================================================ FILE: resources/shaders/pp_bloom_extract_bright.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_BLOOM_EXTRACT_BRIGHT_HLSL__ #define __PP_BLOOM_EXTRACT_BRIGHT_HLSL__ #include "pp_dof_util.hlsl" Texture2D source : register(t0); Texture2D g_emissive : register(t1); Texture2D g_depth : register(t2); RWTexture2D output_bright : register(u0); SamplerState s0 : register(s0); SamplerState s1 : register(s1); [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output_bright.GetDimensions(screen_size.x, screen_size.y); float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y) + 0.5f; float2 uv = screen_coord / screen_size; float4 out_bright = float4(0.0f, 0.0f, 0.0f, 1.0f); float3 final_color = source.SampleLevel(s1, uv, 0).rgb; float brightness = dot(final_color, float3(0.2126f, 0.7152f, 0.0722f)); for (int i = -1; i < 2; ++i) { uv = float2(screen_coord.x + i, screen_coord.y + i) / screen_size; if (brightness > 1.0f && g_depth.SampleLevel(s1, uv, 0).r < 1) { out_bright = saturate(float4(final_color, 1.0f)); } out_bright += float4(g_emissive.SampleLevel(s0, uv, 0).rgb, 1.0f); } out_bright /= 3; output_bright[int2(dispatch_thread_id.xy)] = out_bright; } #endif //__PP_BLOOM_EXTRACT_BRIGHT_HLSL__ ================================================ FILE: resources/shaders/pp_bloom_util.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_BLOOM_UTIL_HLSL__ #define __PP_BLOOM_UTIL_HLSL__ static const float gaussian_weights[15] = { 0.023089f, 0.034587f, 0.048689f, 0.064408f, 0.080066f, 0.093531f, 0.102673f, 0.105915f, 0.102673f, 0.093531f, 0.080066f, 0.064408f, 0.048689f, 0.034587f, 0.023089f }; #endif //__PP_BLOOM_UTIL_HLSL__ ================================================ FILE: resources/shaders/pp_dof_bokeh.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_DOF_BOKEH_HLSL__ #define __PP_DOF_BOKEH_HLSL__ #include "pp_dof_properties.hlsl" #include "pp_dof_util.hlsl" Texture2D source_near : register(t0); Texture2D source_far : register(t1); Texture2D near_mask :register(t2); RWTexture2D output_near : register(u0); RWTexture2D output_far : register(u1); SamplerState s0 : register(s0); SamplerState s1 : register(s1); cbuffer CameraProperties : register(b0) { float f_number; float shape_curve; float bokeh_poly_amount; uint num_blades; float m_padding; float2 m_bokeh_shape_modifier; int enable_dof; }; [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); source_near.GetDimensions(screen_size.x, screen_size.y); screen_size -= 1.0f; float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y); float2 texel_size = 1.0f / screen_size; float2 uv = screen_coord / (screen_size) ; const uint NUMSAMPLES = NUMDOFSAMPLES * NUMDOFSAMPLES; const float MAXKERNELSIZE = MAXBOKEHSIZE * 0.5f; const float SHAPECURVE = 2.0f; float4 fgcolor = float4(0, 0, 0, 0); float4 bgcolor = float4(0, 0, 0, 0); //Kernel gather method credits to Matt Pettineo and David Neubelt. if (enable_dof > 0) { float far_coc = source_far.SampleLevel(s1, uv, 0).w; float near_coc = source_near.SampleLevel(s1, uv, 0).w; float kernel_radius = MAXKERNELSIZE * far_coc; [branch] if (kernel_radius > 0.5f) { float weightsum = 0.0001f; [unroll] for (uint i = 0; i < NUMSAMPLES; ++i) { float lensX = saturate((i % NUMDOFSAMPLES) / max(NUMDOFSAMPLES - 1.0f, 1.0f)); float lensY = saturate((i / NUMDOFSAMPLES) / max(NUMDOFSAMPLES - 1.0f, 1.0f)); float2 kernel_offset = SquareToConcentricDiskMapping(lensX, lensY, float(num_blades), bokeh_poly_amount) * m_bokeh_shape_modifier; float4 s = source_far.SampleLevel(s0, (screen_coord + kernel_offset * kernel_radius) / screen_size, 0.0f); float samplecoc = s.w; s *= saturate(1.0f + (samplecoc - far_coc)); s *= (1.0f - shape_curve) + pow(max(length(kernel_offset), 0.01f), SHAPECURVE) * shape_curve; bgcolor += s; } bgcolor /= NUMSAMPLES; } else { bgcolor = source_far.SampleLevel(s0, uv, 0); } float nearMask = SampleTextureBSpline(near_mask, s0, uv).x; nearMask = saturate(nearMask * 1.0f); near_coc = max(near_coc, nearMask); kernel_radius = near_coc * MAXKERNELSIZE; [branch] if (kernel_radius > 0.25f) { float weightsum = 0.0001f; [unroll] for (uint i = 0; i < NUMSAMPLES; ++i) { float lensX = saturate((i % NUMDOFSAMPLES) / max(NUMDOFSAMPLES - 1.0f, 1.0f)); float lensY = saturate((i / NUMDOFSAMPLES) / max(NUMDOFSAMPLES - 1.0f, 1.0f)); float2 kernel_offset = SquareToConcentricDiskMapping(lensX, lensY, float(num_blades), bokeh_poly_amount) * m_bokeh_shape_modifier; float4 s = source_near.SampleLevel(s0, (screen_coord + kernel_offset * kernel_radius) / screen_size , 0.0f); float samplecoc = saturate(s.w * MAXKERNELSIZE); fgcolor.xyz += s.xyz * s.w; float samplealpha = 1.0f; samplealpha *= saturate(s.w * 1.0f); fgcolor.w += samplealpha; weightsum += s.w; } fgcolor.xyz /= weightsum; fgcolor.w = saturate(fgcolor.w * (1.0f / NUMSAMPLES)); fgcolor.w = max(fgcolor.w, source_near.SampleLevel(s0, uv, 0).w); } else { fgcolor = float4(source_near.SampleLevel(s1, uv, 0).rgb, 0.0f); } } output_near[int2(dispatch_thread_id.xy)] = fgcolor; output_far[int2(dispatch_thread_id.xy)] = bgcolor; } #endif //__PP_DOF_BOKEH_HLSL__ ================================================ FILE: resources/shaders/pp_dof_bokeh_post_filter.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_DOF_BOKEH_POST_FILTER_HLSL__ #define __PP_DOF_BOKEH_POST_FILTER_HLSL__ Texture2D source_near : register(t0); Texture2D source_far : register(t1); RWTexture2D output_near : register(u0); RWTexture2D output_far : register(u1); SamplerState s0 : register(s0); SamplerState s1 : register(s1); [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output_near.GetDimensions(screen_size.x, screen_size.y); screen_size -= 1.0f; float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y); float2 uv = screen_coord / screen_size; float nearcoc = source_near.SampleLevel(s1, uv, 0).a; float farcoc = source_far.SampleLevel(s1, uv, 0).a; static const int SampleRadius = 2; static const int SampleDiameter = SampleRadius * 2 + 1; float3 near_color = 0; float3 far_color = 0; [unroll] for (int y = -SampleRadius; y <= SampleRadius; ++y) { [unroll] for (int x = -SampleRadius; x <= SampleRadius; ++x) { near_color += source_near.SampleLevel(s0, (screen_coord + float2(x, y)) / screen_size, 0).rgb; far_color += source_far.SampleLevel(s0, (screen_coord + float2(x, y)) / screen_size, 0).rgb; } } near_color /= float(SampleDiameter * SampleDiameter); far_color /= float(SampleDiameter * SampleDiameter); output_near[int2(dispatch_thread_id.xy)] = float4(near_color, nearcoc); output_far[int2(dispatch_thread_id.xy)] = float4(far_color, farcoc); } #endif //__PP_DOF_BOKEH_POST_FILTER_HLSL__ ================================================ FILE: resources/shaders/pp_dof_coc.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_DOF_COC_HLSL__ #define __PP_DOF_COC_HLSL__ #include "pp_dof_properties.hlsl" #include "pp_dof_util.hlsl" Texture2D gbuffer_depth : register(t0); RWTexture2D output : register(u0); SamplerState s0 : register(s0); cbuffer CameraProperties : register(b0) { float4x4 projection; float focal_length; float f_number; float film_size; float focus_dist; float2 m_pad; int enable_dof; float dof_range; }; float GetCoC(float lineardepth, float focusdist) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); float fstop = focal_length / f_number * 0.5f; ////// Compute CoC in meters float coc = -fstop * (focal_length * (focusdist - lineardepth)) / (lineardepth * (focusdist - focal_length)); //// Convert to pixels coc = (coc / film_size) * screen_size.x; coc = clamp(coc / (MAXCOCSIZE * dof_range), -1.f, 1.f); return coc; } float GetAutoFocusDepth(float2 screen_dimensions) { float depth_focus = gbuffer_depth[float2(0.5f * screen_dimensions.x, 0.5f * screen_dimensions.y)].r; depth_focus += gbuffer_depth[float2(0.51f * screen_dimensions.x, 0.51f * screen_dimensions.y)].r; depth_focus += gbuffer_depth[float2(0.49f * screen_dimensions.x, 0.51f * screen_dimensions.y)].r; depth_focus += gbuffer_depth[float2(0.51f * screen_dimensions.x, 0.49f * screen_dimensions.y)].r; depth_focus += gbuffer_depth[float2(0.49f * screen_dimensions.x, 0.49f * screen_dimensions.y)].r; depth_focus += gbuffer_depth[float2(0.52f * screen_dimensions.x, 0.52f * screen_dimensions.y)].r; depth_focus += gbuffer_depth[float2(0.48f * screen_dimensions.x, 0.52f * screen_dimensions.y)].r; depth_focus += gbuffer_depth[float2(0.52f * screen_dimensions.x, 0.48f * screen_dimensions.y)].r; depth_focus += gbuffer_depth[float2(0.48f * screen_dimensions.x, 0.48f * screen_dimensions.y)].r; depth_focus = (depth_focus / 9.0f); return depth_focus; } [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); screen_size -= 1.0f; float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y); float2 uv = screen_coord / screen_size; float sample_depth = gbuffer_depth.SampleLevel(s0, uv, 0).r; float focus_depth = GetAutoFocusDepth(screen_size); sample_depth = GetLinearDepth(sample_depth) * FFAR; float coc = GetCoC(sample_depth, focus_dist); if (focus_dist < 1) { focus_depth = GetLinearDepth(focus_depth) * FFAR; coc = GetCoC(sample_depth, focus_depth); } if (enable_dof == 0) { coc = 0.0f; } float2 result = float2(coc, gbuffer_depth.SampleLevel(s0, uv, 0).r); output[int2(dispatch_thread_id.xy)] = result; } #endif //__PP_DOF_COC_HLSL__ ================================================ FILE: resources/shaders/pp_dof_composition.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_DOF_COMPOSITION_HLSL__ #define __PP_DOF_COMPOSITION_HLSL__ #include "pp_dof_properties.hlsl" Texture2D source : register(t0); RWTexture2D output : register(u0); Texture2D bokeh_near : register(t1); Texture2D bokeh_far : register(t2); Texture2D coc_buffer : register(t3); SamplerState s0 : register(s0); SamplerState s1 : register(s1); float GetDownSampledCoC(float2 uv , float2 texel_size) { float4 offset = texel_size.xyxy * float2(-0.5f, 0.5f).xxyy; float coc0 = coc_buffer.SampleLevel(s1, uv + offset.xy, 0); float coc1 = coc_buffer.SampleLevel(s1, uv + offset.zy, 0); float coc2 = coc_buffer.SampleLevel(s1, uv + offset.xw, 0); float coc3 = coc_buffer.SampleLevel(s1, uv + offset.zw, 0); float coc_min = min(min(min(coc0, coc1), coc2), coc3); float coc_max = max(max(max(coc0, coc1), coc2), coc3); float coc = coc_max >= -coc_min ? coc_max : coc_min; return coc; } [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output.GetDimensions(screen_size.x, screen_size.y); screen_size -= 1.0f; float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y); float2 texel_size = 1.0 / screen_size; float2 uv = (screen_coord) / (screen_size); float coc = coc_buffer.SampleLevel(s0, uv, 0); float3 original_sample = source.SampleLevel(s0, uv, 0).rgb; float4 near_sample = bokeh_near.SampleLevel(s1, uv, 0); float4 far_sample = bokeh_far.SampleLevel(s1, uv, 0); float near_w = bokeh_near.SampleLevel(s0, uv, 0).a; float3 near = near_sample.rgb; float3 far = original_sample.rgb; if (far_sample.w > 0.0f) { far = far_sample.rgb / far_sample.w; } float far_blend = saturate(saturate(coc) * MAXCOCSIZE - 0.5f); float3 result = lerp(original_sample, far.rgb, far_blend); float near_blend = saturate(near_sample.w * 2.0f); result = lerp(result, near.rgb, smoothstep(0.0f, 1.0f, near_blend)); output[int2(dispatch_thread_id.xy)] = float4(result, coc); } #endif //__PP_DOF_COMPOSITION_HLSL__ ================================================ FILE: resources/shaders/pp_dof_compute_near_mask.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ //================================================================================================= // // Baking Lab // by MJP and David Neubelt // http://mynameismjp.wordpress.com/ // // All code licensed under the MIT license // //================================================================================================= #ifndef __PP_DOF_COMPUTE_NEAR_MASK_HLSL__ #define __PP_DOF_COMPUTE_NEAR_MASK_HLSL__ #include "pp_dof_properties.hlsl" #include "pp_dof_util.hlsl" Texture2D input : register(t0); RWTexture2D output : register(u0); SamplerState s0 : register(s0); //================================================================================================= // Constants //================================================================================================= static const uint NumThreads = 16 * 16; // -- shared memory groupshared float Samples[NumThreads]; //================================================================================================= // Computes a downscaled mask for the near field //================================================================================================= [numthreads(32, 32, 1)] void main_cs(in uint3 GroupID : SV_GroupID, in uint3 GroupThreadID : SV_GroupThreadID, in uint ThreadIndex : SV_GroupIndex) { uint2 textureSize; input.GetDimensions(textureSize.x, textureSize.y); uint2 samplePos = GroupID.xy * 16 + GroupThreadID.xy; samplePos = min(samplePos, textureSize - 1); float cocSample = input[samplePos].w; // -- store in shared memory Samples[ThreadIndex] = cocSample; GroupMemoryBarrierWithGroupSync(); // -- reduce [unroll] for (uint s = NumThreads / 2; s > 0; s >>= 1) { if (ThreadIndex < s) Samples[ThreadIndex] = max(Samples[ThreadIndex], Samples[ThreadIndex + s]); GroupMemoryBarrierWithGroupSync(); } if (ThreadIndex == 0) output[GroupID.xy] = Samples[0]; } #endif //__PP_DOF_COC_HLSL__ ================================================ FILE: resources/shaders/pp_dof_dilate.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_DOF_DILATE_HLSL__ #define __PP_DOF_DILATE_HLSL__ #include "pp_dof_properties.hlsl" #include "pp_dof_util.hlsl" Texture2D source_near : register(t0); RWTexture2D output_near : register(u0); SamplerState s0 : register(s0); [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output_near.GetDimensions(screen_size.x, screen_size.y); screen_size -= 1.0f; float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y); float2 uv = (screen_coord) / screen_size; static const int sample_radius = 3; float output = source_near.SampleLevel(s0, uv , 0).r; [unroll] for (int y = -sample_radius; y <= sample_radius; ++y) { [unroll] for (int x = -sample_radius; x <= sample_radius; ++x) { output = max(output, source_near.SampleLevel(s0, saturate((screen_coord + float2(x, y)) / screen_size), 0).r); } } output_near[int2(dispatch_thread_id.xy)] = output; } #endif //__PP_DOF_DILATE_HLSL__ ================================================ FILE: resources/shaders/pp_dof_downscale.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_DOF_DOWNSCALE_HLSL__ #define __PP_DOF_DOWNSCALE_HLSL__ #include "pp_dof_util.hlsl" Texture2D source : register(t0); RWTexture2D output_near : register(u0); RWTexture2D output_far : register(u1); Texture2D cocbuffer : register(t1); SamplerState s0 : register(s0); SamplerState s1 : register(s1); float GetDownSampledCoC(float2 uv, float2 texelSize) { float4 offset = texelSize.xyxy * float2(-0.5f, 0.5f).xxyy; float coc0 = cocbuffer.SampleLevel(s1, uv + offset.xy, 0).r; float coc1 = cocbuffer.SampleLevel(s1, uv + offset.zy, 0).r; float coc2 = cocbuffer.SampleLevel(s1, uv + offset.xw, 0).r; float coc3 = cocbuffer.SampleLevel(s1, uv + offset.zw, 0).r; float coc4 = cocbuffer.SampleLevel(s1, uv, 0).r; float cocMin = min(min(min(coc0, coc1), coc2), coc3); float cocMax = max(max(max(coc0, coc1), coc2), coc3); float coc = cocMax >= -cocMin ? cocMax : cocMin; return coc; } [numthreads(16, 16, 1)] void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) { float2 screen_size = float2(0.f, 0.f); output_far.GetDimensions(screen_size.x, screen_size.y); screen_size -= 1.0f; float2 screen_coord = int2(dispatch_thread_id.x, dispatch_thread_id.y); float2 texel_size = 1.0f / screen_size; float4 offset = texel_size.xyxy * float2(-0.5f, 0.5f).xxyy; float2 uv = screen_coord / (screen_size); float3 source11 = source.SampleLevel(s0, uv, 0).rgb; float3 source0 = source.SampleLevel(s0, uv + offset.xy, 0).rgb; float3 source1 = source.SampleLevel(s0, uv + offset.zy, 0).rgb; float3 source2 = source.SampleLevel(s0, uv + offset.xw, 0).rgb; float3 source3 = source.SampleLevel(s0, uv + offset.zw, 0).rgb; float3 finalcolor = (source11 + source0 + source1 + source2 + source3) * 0.2f; finalcolor = source.SampleLevel(s0, uv, 0).rgb; float coc = GetDownSampledCoC(uv, texel_size); float4 out_near = max(0,float4(finalcolor, 1.0f) * max(-coc, 0.0f)); out_near.rgb = finalcolor; coc = cocbuffer.SampleLevel(s1, uv, 0).r; float4 out_far = max(0,float4(finalcolor, 1.0f) * max(coc, 0.0f)); output_near[int2(dispatch_thread_id.xy)] = out_near; output_far[int2(dispatch_thread_id.xy)] = out_far; } #endif //__PP_DOF_DOWNSCALE_HLSL__ ================================================ FILE: resources/shaders/pp_dof_properties.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_DOF_PROPERTIES_HLSL__ #define __PP_DOF_PROPERTIES_HLSL__ static const float MAXBOKEHSIZE = 21.f; static const float MAXCOCSIZE = 4.0f; static const uint NUMDOFSAMPLES = 9; #endif //__PP_DOF_PROPERTIES_HLSL__ ================================================ FILE: resources/shaders/pp_dof_util.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_DOF_UTIL_HLSL__ #define __PP_DOF_UTIL_HLSL__ static const float FNEAR = 0.1f; static const float FFAR = 10000.f; static const float PI = 3.141592654f; static const float PI2 = 6.283185308f; float WeighCoC(float coc, float radius) { //return coc >= radius; return saturate((coc - radius + 2) / 2); } float WeighColor(float3 color) { return 1 / (1 + max(max(color.r, color.g), color.b)); } float GetLinearDepth(float depth) { float z = (2 * FNEAR) / (FFAR + FNEAR - (depth * (FFAR - FNEAR))); return z; } // Calculates the gaussian blur weight for a given distance and sigmas float CalcGaussianWeight(int sampleDist, float sigma) { float g = 1.0f / sqrt(2.0f * 3.14159 * sigma * sigma); return (g * exp(-(sampleDist * sampleDist) / (2 * sigma * sigma))); } // ------------------------------------------------------------------------------------------------ // Samples a texture with B-spline (bicubic) filtering // ------------------------------------------------------------------------------------------------ float4 SampleTextureBSpline(in Texture2D textureMap, in SamplerState linearSampler, in float2 uv) { float2 texSize; textureMap.GetDimensions(texSize.x, texSize.y); float2 invTexSize = 1.0f / texSize; float2 a = frac(uv * texSize - 0.5f); float2 a2 = a * a; float2 a3 = a2 * a; float2 w0 = (1.0f / 6.0f) * (-a3 + 3 * a2 - 3 * a + 1); float2 w1 = (1.0f / 6.0f) * (3 * a3 - 6 * a2 + 4); float2 w2 = (1.0f / 6.0f) * (-3 * a3 + 3 * a2 + 3 * a + 1); float2 w3 = (1.0f / 6.0f) * a3; float2 g0 = w0 + w1; float2 g1 = w2 + w3; float2 h0 = 1.0f - (w1 / (w0 + w1)) + a; float2 h1 = 1.0f - (w3 / (w2 + w3)) - a; float2 ex = float2(invTexSize.x, 0.0f); float2 ey = float2(0.0f, invTexSize.y); w0 = 0.5f; w1 = 0.5f; g0 = 0.5f; float2 uv10 = uv + h0.x * ex; float2 uv00 = uv - h1.x * ex; float2 uv11 = uv10 + h0.y * ey; float2 uv01 = uv00 + h0.y * ey; uv10 = uv10 - h1.y * ey; uv00 = uv00 - h1.y * ey; uv00 = uv + float2(-0.75f, -0.75f) * invTexSize; uv10 = uv + float2(0.75f, -0.75f) * invTexSize; uv01 = uv + float2(-0.75f, 0.75f) * invTexSize; uv11 = uv + float2(0.75f, 0.75f) * invTexSize; float4 sample00 = textureMap.SampleLevel(linearSampler, uv00, 0.0f); float4 sample10 = textureMap.SampleLevel(linearSampler, uv10, 0.0f); float4 sample01 = textureMap.SampleLevel(linearSampler, uv01, 0.0f); float4 sample11 = textureMap.SampleLevel(linearSampler, uv11, 0.0f); sample00 = lerp(sample00, sample01, g0.y); sample10 = lerp(sample10, sample11, g0.y); return lerp(sample00, sample10, g0.x); } // Maps a value inside the square [0,1]x[0,1] to a value in a disk of radius 1 using concentric squares. // This mapPIng preserves area, bi continuity, and minimizes deformation. // Based off the algorithm "A Low Distortion Map Between Disk and Square" by Peter Shirley and // Kenneth Chiu. Also includes polygon morphing modification from "CryEngine3 Graphics Gems" // by Tiago Sousa float2 SquareToConcentricDiskMapping(float x, float y, float numSides, float polygonAmount) { float phi, r; // -- (a,b) is now on [-1,1]2 float a = 2.0f * x - 1.0f; float b = 2.0f * y - 1.0f; if (a > -b) // region 1 or 2 { if (a > b) // region 1, also |a| > |b| { r = a; phi = (PI / 4.0f) * (b / a); } else // region 2, also |b| > |a| { r = b; phi = (PI / 4.0f) * (2.0f - (a / b)); } } else // region 3 or 4 { if (a < b) // region 3, also |a| >= |b|, a != 0 { r = -a; phi = (PI / 4.0f) * (4.0f + (b / a)); } else // region 4, |b| >= |a|, but a==0 and b==0 could occur. { r = -b; if (abs(b) > 0.0f) phi = (PI / 4.0f) * (6.0f - (a / b)); else phi = 0; } } const float N = numSides; float polyModifier = cos(PI / N) / cos(phi - (PI2 / N) * floor((N * phi + PI) / PI2)); r *= lerp(1.0f, polyModifier, polygonAmount); float2 result; result.x = r * cos(phi); result.y = r * sin(phi); return result; } #endif //__PP_DOF_UTIL_HLSL__ ================================================ FILE: resources/shaders/pp_fxaa.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_FXAA_HLSL__ #define __PP_FXAA_HLSL__ #ifndef FXAA_REDUCE_MIN #define FXAA_REDUCE_MIN (1.0/ 128.0) #endif #ifndef FXAA_REDUCE_MUL #define FXAA_REDUCE_MUL (1.0 / 8.0) #endif #ifndef FXAA_SPAN_MAX #define FXAA_SPAN_MAX 8.0 #endif //optimized version for mobile, where dependent //texture reads can be a bottleneck float4 fxaa(Texture2D tex, SamplerState s, float2 fragCoord, float2 resolution, float2 v_rgbNW, float2 v_rgbNE, float2 v_rgbSW, float2 v_rgbSE, float2 v_rgbM) { float4 color; float2 inverseVP = float2(1.0 / resolution.x, 1.0 / resolution.y); float3 rgbNW = tex.SampleLevel(s, v_rgbNW, 0).xyz; float3 rgbNE = tex.SampleLevel(s, v_rgbNE, 0).xyz; float3 rgbSW = tex.SampleLevel(s, v_rgbSW, 0).xyz; float3 rgbSE = tex.SampleLevel(s, v_rgbSE, 0).xyz; float4 texColor = tex.SampleLevel(s, v_rgbM, 0); float3 rgbM = texColor.xyz; float3 luma = float3(0.299, 0.587, 0.114); float lumaNW = dot(rgbNW, luma); float lumaNE = dot(rgbNE, luma); float lumaSW = dot(rgbSW, luma); float lumaSE = dot(rgbSE, luma); float lumaM = dot(rgbM, luma); float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); float2 dir; dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); dir = min(float2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), max(float2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * inverseVP; float3 rgbA = 0.5 * ( tex.SampleLevel(s, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5), 0).xyz + tex.SampleLevel(s, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5), 0).xyz); float3 rgbB = rgbA * 0.5 + 0.25 * ( tex.SampleLevel(s, fragCoord * inverseVP + dir * -0.5, 0).xyz + tex.SampleLevel(s, fragCoord * inverseVP + dir * 0.5, 0).xyz); float lumaB = dot(rgbB, luma); if ((lumaB < lumaMin) || (lumaB > lumaMax)) color = float4(rgbA, texColor.a); else color = float4(rgbB, texColor.a); return color; } void texcoords(float2 fragCoord, float2 resolution, out float2 v_rgbNW, out float2 v_rgbNE, out float2 v_rgbSW, out float2 v_rgbSE, out float2 v_rgbM) { float2 inverseVP = 1.0 / resolution.xy; v_rgbNW = (fragCoord + float2(-1.0, -1.0)) * inverseVP; v_rgbNE = (fragCoord + float2(1.0, -1.0)) * inverseVP; v_rgbSW = (fragCoord + float2(-1.0, 1.0)) * inverseVP; v_rgbSE = (fragCoord + float2(1.0, 1.0)) * inverseVP; v_rgbM = float2(fragCoord * inverseVP); } float4 SampleFXAA(Texture2D tex, SamplerState s, float2 frag_coord, float2 resolution) { float2 v_rgbNW; float2 v_rgbNE; float2 v_rgbSW; float2 v_rgbSE; float2 v_rgbM; texcoords(frag_coord, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); return fxaa(tex, s, frag_coord, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); } #endif //__PP_FXAA_HLSL__ ================================================ FILE: resources/shaders/pp_hdr_util.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_HDR_UTIL_HLSL__ #define __PP_HDR_UTIL_HLSL__ float3 linearToneMapping(float3 color, float exposure, float gamma) { color = clamp(exposure * color, 0.f, 1.f); color = pow(color, 1.f / gamma); return color; } float3 simpleReinhardToneMapping(float3 color, float exposure, float gamma) { color *= exposure / (1. + color / exposure); color = pow(color, 1. / gamma); return color; } float3 lumaBasedReinhardToneMapping(float3 color, float gamma) { float luma = dot(color, float3(0.2126, 0.7152, 0.0722)); float toneMappedLuma = luma / (1. + luma); color *= toneMappedLuma / luma; color = pow(color, (1. / gamma)); return color; } float3 whitePreservingLumaBasedReinhardToneMapping(float3 color, float gamma) { float white = 2.; float luma = dot(color, float3(0.2126, 0.7152, 0.0722)); float toneMappedLuma = luma * (1. + luma / (white*white)) / (1. + luma); color *= toneMappedLuma / luma; color = pow(color, (1. / gamma)); return color; } float3 RomBinDaHouseToneMapping(float3 color, float gamma) { color = exp(-1.0 / (2.72*color + 0.15)); color = pow(color, (1. / gamma)); return color; } float3 filmicToneMapping(float3 color) { color = max(float3(0., 0., 0.), color - float3(0.004, 0.004, 0.004)); color = (color * (6.2 * color + .5)) / (color * (6.2 * color + 1.7) + 0.06); return color; } float3 GrayscaleToneMapping(float3 color) { float gray = 0.299 * color.r + 0.587 * color.g + 0.114 * color.b; color.r = gray; color.g = gray; color.b = gray; return color; } float3 ACESToneMapping(float3 color, float exposure, float gamma) { color = clamp(exposure * color, 0.f, 1.f); color = pow(color, 1.f / gamma); float a = 2.51f; float b = 0.03f; float c = 2.43f; float d = 0.59f; float e = 0.14f; return saturate((color*(a*color+b))/(color*(c*color+d)+e)); } float3 Uncharted2ToneMapping(float3 color, float gamma, float exposure) { float A = 0.15; float B = 0.50; float C = 0.10; float D = 0.20; float E = 0.02; float F = 0.30; float W = 11.2; color *= exposure; color = (((color*2) * (A * (color*2) + C * B) + D * E) / ((color*2) * (A * (color*2) + B) + D * F)) - E / F; float white = ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F; color /= white; color = pow(color, (1. / gamma)); return color; } float4x4 brightnessMatrix(float brightness) { return float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, brightness, brightness, brightness, 1); } float3 ApplyHue(float3 color, float hue_in) { float3 result = color; const float4 kRGBToYPrime = float4(0.299, 0.587, 0.114, 0.0); const float4 kRGBToI = float4(0.596, -0.275, -0.321, 0.0); const float4 kRGBToQ = float4(0.212, -0.523, 0.311, 0.0); const float4 kYIQToR = float4(1.0, 0.956, 0.621, 0.0); const float4 kYIQToG = float4(1.0, -0.272, -0.647, 0.0); const float4 kYIQToB = float4(1.0, -1.107, 1.704, 0.0); float YPrime = dot(color, kRGBToYPrime); float I = dot(color, kRGBToI); float Q = dot(color, kRGBToQ); float hue = atan2(Q, I); float chroma = sqrt(I * I + Q * Q); hue += hue_in; Q = chroma * sin(hue); I = chroma * cos(hue); float4 yIQ = float4(YPrime, I, Q, 0.0); result.r = dot(yIQ, kYIQToR); result.g = dot(yIQ, kYIQToG); result.b = dot(yIQ, kYIQToB); return result; } float4x4 ContrastMatrix(float contrast) { float t = (1.0 - contrast) / 2.0; return float4x4(contrast, 0, 0, 0, 0, contrast, 0, 0, 0, 0, contrast, 0, t, t, t, 1); } float3 AllTonemappingAlgorithms(float3 color, float rotation, float exposure, float gamma) { float3 result = color; float n = 8; int i = int(n * (rotation / 2)); if (i == 0) result = linearToneMapping(color, exposure, gamma); if (i == 1) result = simpleReinhardToneMapping(color, exposure, gamma); if (i == 2) result = lumaBasedReinhardToneMapping(color, gamma); if (i == 3) result = whitePreservingLumaBasedReinhardToneMapping(color, gamma); if (i == 4) result = RomBinDaHouseToneMapping(color, gamma); if (i == 5) result = filmicToneMapping(color); if (i == 6) result = ACESToneMapping(color, exposure, gamma); if (i == 7) result = Uncharted2ToneMapping(color, gamma, exposure); if (i == 8) result = GrayscaleToneMapping(color); return result; } #endif //__PP_HDR_UTIL_HLSL__ ================================================ FILE: resources/shaders/pp_tonemapping.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_TONEMAPPING_HLSL__ #define __PP_TONEMAPPING_HLSL__ #include "pp_util.hlsl" #include "pp_hdr_util.hlsl" Texture2D input : register(t0); RWTexture2D output : register(u0); SamplerState s0 : register(s0); cbuffer CameraProperties : register(b0) { float hdr; }; [numthreads(16, 16, 1)] void main(uint3 DTid : SV_DispatchThreadID) { float2 resolution; input.GetDimensions(resolution.x, resolution.y); float2 uv = (float2(DTid.xy + 0.5f) / resolution); float gamma = 2.2; float exposure = 1; float3 color = input.SampleLevel(s0, uv, 0).rgb; //color = SampleFXAA(input, s0, DTid.xy + 0.5f, resolution); //uv = ZoomUV(uv, 0.75); //float3 color = input.SampleLevel(s0, BarrelDistortUV(uv, 2), 0); //float3 color = ChromaticAberrationV2(input, s0, uv, 0.2, 0.96f).rgb; if (hdr == 0) { //color = linearToneMapping(color, exposure, gamma); //color = simpleReinhardToneMapping(color, exposure, gamma); //color = lumaBasedReinhardToneMapping(color, gamma); //color = whitePreservingLumaBasedReinhardToneMapping(color, gamma); //color = RomBinDaHouseToneMapping(color, gamma); //color = filmicToneMapping(color); //color = Uncharted2ToneMapping(color, gamma, exposure); //color = GrayscaleToneMapping(color); color = ACESToneMapping(color, exposure, gamma); //color = AllTonemappingAlgorithms(color.rgb, uv.x + uv.y, exposure, gamma); //color = Vignette(color, uv, 1.5, 0.5, 0.5); } output[DTid.xy] = float4(color, 1); } #endif //__PP_TONEMAPPING_HLSL__ ================================================ FILE: resources/shaders/pp_util.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __PP_UTIL_HLSL__ #define __PP_UTIL_HLSL__ #include "pp_fxaa.hlsl" #include "math.hlsl" float3 Vignette(float3 input, float2 pos, float radius, float softness, float strength) { float len = length(pos * 2 - 1); float vignette = smoothstep(radius, radius - softness, len); return lerp(input, input * vignette, strength); } // Old lens_effects.hlsl file float2 barrelDistortion(float2 coord, float amt) { float2 cc = coord - 0.5; float dist = dot(cc, cc); return coord + cc * dist * amt; } static const int num_iter = 11; static const float reci_num_iter_f = 1.0 / float(num_iter); // Old naive chromatic aberration. float4 ChromaticAberration(Texture2D tex, SamplerState s, float2 uv, float strength) { float2 r_offset = float2(strength, 0); float2 g_offset = float2(-strength, 0); float2 b_offset = float2(0, 0); float4 r = float4(1, 1, 1, 1); r.x = tex.SampleLevel(s, uv + r_offset, 0).x; r.y = tex.SampleLevel(s, uv + g_offset, 0).y; r.z = tex.SampleLevel(s, uv + b_offset, 0).z; r.a = tex.SampleLevel(s, uv, 0).a; return r; } // Zoom into a image. float2 ZoomUV(float2 uv, float zoom) { return (uv * zoom) + ((1 - (zoom)) / 2); } float4 ChromaticAberrationV2(Texture2D tex, SamplerState s, float2 uv, float strength, float zoom) { uv = ZoomUV(uv, zoom); float4 sumcol = 0.0; float4 sumw = 0.0; float2 resolution; tex.GetDimensions(resolution.x, resolution.y); for (int i = 0; i < num_iter; ++i) { float t = float(i) * reci_num_iter_f; float4 w = spectrum_offset(t); sumw += w; //sumcol += w * tex.SampleLevel(s, barrelDistortion(uv, 0.6 * strength*t ), 0); sumcol += w * SampleFXAA(tex, s, barrelDistortion(uv, 0.6 * strength * t) * resolution, resolution); } return sumcol / sumw; } float2 BarrelDistortUV(float2 uv, float kcube) { float k = -0.15; float r2 = (uv.x - 0.5) * (uv.x - 0.5) + (uv.y - 0.5) * (uv.y - 0.5); float f = 0; //only compute the cubic distortion if necessary if (kcube == 0.0) { f = 1 + r2 * k; } else { f = 1 + r2 * (k + kcube * sqrt(r2)); }; // get the right pixel for the current position float x = f * (uv.x - 0.5) + 0.5; float y = f * (uv.y - 0.5) + 0.5; return float2(x, y); } #endif //__PP_UTIL_HLSL__ ================================================ FILE: resources/shaders/rand_util.hlsl ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __RAND_UTIL_HLSL__ #define __RAND_UTIL_HLSL__ #include "math.hlsl" // Initialize random seed uint initRand(uint val0, uint val1, uint backoff = 16) { uint v0 = val0, v1 = val1, s0 = 0; [unroll] for (uint n = 0; n < backoff; n++) { s0 += 0x9e3779b9; v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s0) ^ ((v1 >> 5) + 0xc8013ea4); v1 += ((v0 << 4) + 0xad90777d) ^ (v0 + s0) ^ ((v0 >> 5) + 0x7e95761e); } return v0; } // Get 'random' value [0, 1] float nextRand(inout uint s) { s = (1664525u * s + 1013904223u); return float(s & 0x00FFFFFF) / float(0x01000000); } float3 rand_in_unit_sphere(inout uint rng_state) { float z = nextRand(rng_state) * 2.0f - 1.0f; float t = nextRand(rng_state) * 2.0f * M_PI; float r = sqrt(max(0.0, 1.0f - z * z)); float x = r * cos(t); float y = r * sin(t); float3 res = float3(x, y, z); res *= pow(nextRand(rng_state), 1.0 / 3.0); return res; } float3 getPerpendicularVector(float3 u) { float3 a = abs(u); uint xm = ((a.x - a.y)<0 && (a.x - a.z)<0) ? 1 : 0; uint ym = (a.y - a.z)<0 ? (1 ^ xm) : 0; uint zm = 1 ^ (xm | ym); return cross(u, float3(xm, ym, zm)); } // Get a cosine-weighted random vector centered around a specified normal direction. float3 getCosHemisphereSample(inout uint randSeed, float3 hitNorm) { // Get 2 random numbers to select our sample with float2 randVal = float2(nextRand(randSeed), nextRand(randSeed)); // Cosine weighted hemisphere sample from RNG float3 bitangent = getPerpendicularVector(hitNorm); float3 tangent = cross(bitangent, hitNorm); float r = sqrt(randVal.x); float phi = 2.0f * M_PI * randVal.y; // Get our cosine-weighted hemisphere lobe sample direction return tangent * (r * cos(phi).x) + bitangent * (r * sin(phi)) + hitNorm.xyz * sqrt(max(0.0, 1.0f - randVal.x)); } // Get a uniform weighted random vector centered around a specified normal direction. float3 getUniformHemisphereSample(inout uint randSeed, float3 hitNorm) { // Get 2 random numbers to select our sample with float2 randVal = float2(nextRand(randSeed), nextRand(randSeed)); // Cosine weighted hemisphere sample from RNG float3 bitangent = getPerpendicularVector(hitNorm); float3 tangent = cross(bitangent, hitNorm); float r = sqrt(max(0.0f,1.0f - randVal.x*randVal.x)); float phi = 2.0f * M_PI * randVal.y; // Get our cosine-weighted hemisphere lobe sample direction return tangent * (r * cos(phi).x) + bitangent * (r * sin(phi)) + hitNorm.xyz * randVal.x; } float3 getUniformHemisphereSample(inout uint randSeed, float3 hitNorm, float angle) { // Get 2 random numbers to select our sample with float2 randVal = float2(nextRand(randSeed), nextRand(randSeed)); // Cosine weighted hemisphere sample from RNG float3 bitangent = getPerpendicularVector(hitNorm); float3 tangent = cross(bitangent, hitNorm); float r = sqrt(max(0.0f, 1.0f - randVal.x * randVal.x)) * sin(angle); float phi = 2.0f * M_PI * randVal.y; // Get our cosine-weighted hemisphere lobe sample direction return tangent * (r * cos(phi).x) + bitangent * (r * sin(phi)) + hitNorm.xyz * cos(asin(r)); } float3 perturbDirectionVector(inout uint randSeed, float3 direction, float angle) { float s = nextRand(randSeed); float r = nextRand(randSeed); float h = cos(angle); float phi = 2.0f * M_PI * s; float z = h + (1.0f - h) * r; float sinT = sqrt(1.0f - z * z); float x = cos(phi) * sinT; float y = sin(phi) * sinT; float3 bitangent = getPerpendicularVector(direction); float3 tangent = cross(bitangent, direction); return bitangent * x + tangent * y + direction * z; } #endif // __RAND_UTIL_HLSL__ ================================================ FILE: resources/sponza_lights.json ================================================ { "lights": [ { "angle": 69.0, "color": [ 0.9999899864196777, 0.9999899864196777, 1.0 ], "pos": [ 0.0, 3.562610149383545, 0.0, 0.0 ], "radius": 200.0, "rot": [ 0.0, 0.0, 0.0, 0.0 ], "size": 0.0, "type": 0 }, { "angle": 0.0, "color": [ 1.0, 0.0, 0.0 ], "pos": [ -4.0, 1.0, -7.193277359008789, 0.0 ], "radius": 5.0, "rot": [ -1.0399991273880005, 7.299992084503174, 0.0, 0.0 ], "size": 0.0, "type": 0 }, { "angle": 0.0, "color": [ 0.0, 1.0, 0.0 ], "pos": [ 4.0, 1.0, -7.192999839782715, 0.0 ], "radius": 5.0, "rot": [ 0.0, 0.0, 0.0, 0.0 ], "size": 0.0, "type": 0 } ] } ================================================ FILE: resources/viknell_lights.json ================================================ { "lights": [ { "angle": 69.0, "color": [ 0.0, 0.0, 1.0 ], "pos": [ 0.75, 0.0, 0.75, 0.0 ], "radius": 6.0, "rot": [ 0.0, 0.0, 0.0, 0.0 ], "size": 0.0, "type": 0 }, { "angle": 0.0, "color": [ 1.0, 0.0, 0.0 ], "pos": [ -0.75, 0.0, 0.75, 0.0 ], "radius": 5.0, "rot": [ 0.0, 0.0, 0.0, 0.0 ], "size": 0.0, "type": 0 } ] } ================================================ FILE: scripts/JenkinsWebhook.bat ================================================ @echo off set "str=%~1" "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 @echo on ================================================ FILE: src/constant_buffer_pool.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "constant_buffer_pool.hpp" namespace wr { ConstantBufferPool::ConstantBufferPool(std::size_t size_in_bytes) : m_size_in_bytes(size_in_bytes) { } ConstantBufferHandle* ConstantBufferPool::Create(std::size_t buffer_size) { std::lock_guard lock(m_mutex); return AllocateConstantBuffer(buffer_size); } void ConstantBufferPool::Update(ConstantBufferHandle* handle, size_t size, size_t offset, std::uint8_t * data) { std::lock_guard lock(m_mutex); WriteConstantBufferData(handle, size, offset, data); } void ConstantBufferPool::Destroy(ConstantBufferHandle* handle) { std::lock_guard lock(m_mutex); DeallocateConstantBuffer(handle); } void ConstantBufferPool::Update(ConstantBufferHandle * handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t * data) { std::lock_guard lock(m_mutex); WriteConstantBufferData(handle, size, offset, frame_idx, data); } } /* wr */ ================================================ FILE: src/constant_buffer_pool.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include "util/defines.hpp" namespace wr { class ConstantBufferPool; struct ConstantBufferHandle { ConstantBufferPool* m_pool; }; class ConstantBufferPool { public: explicit ConstantBufferPool(std::size_t size_in_bytes); virtual ~ConstantBufferPool() = default; ConstantBufferPool(ConstantBufferPool const &) = delete; ConstantBufferPool& operator=(ConstantBufferPool const &) = delete; ConstantBufferPool(ConstantBufferPool&&) = delete; ConstantBufferPool& operator=(ConstantBufferPool&&) = delete; [[nodiscard]] ConstantBufferHandle* Create(std::size_t buffer_size); void Update(ConstantBufferHandle* handle, size_t size, size_t offset, std::uint8_t* data); void Update(ConstantBufferHandle* handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t* data); void Destroy(ConstantBufferHandle* handle); virtual void Evict() = 0; virtual void MakeResident() = 0; protected: virtual ConstantBufferHandle* AllocateConstantBuffer(std::size_t buffer_size) = 0; virtual void WriteConstantBufferData(ConstantBufferHandle* handle, size_t size, size_t offset, std::uint8_t* data) = 0; virtual void WriteConstantBufferData(ConstantBufferHandle* handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t* data) = 0; virtual void DeallocateConstantBuffer(ConstantBufferHandle* handle) = 0; std::size_t m_size_in_bytes; std::mutex m_mutex; }; } /* wr */ ================================================ FILE: src/d3d12/d3d12_acceleration_structure..cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include #include "d3d12_defines.hpp" #include "d3d12_rt_descriptor_heap.hpp" namespace wr::d3d12 { /*! NOTE! Top level has scratch and instance descs. blas does not!!!!!!!! */ namespace internal { inline void AllocateUAVBuffer(Device* device, UINT64 size, ID3D12Resource** resource, D3D12_RESOURCE_STATES initial_state = D3D12_RESOURCE_STATE_COMMON, const wchar_t* name = nullptr) { auto heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); auto buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(size, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); TRY(device->m_native->CreateCommittedResource( &heap_properties, D3D12_HEAP_FLAG_NONE, &buffer_desc, initial_state, nullptr, IID_PPV_ARGS(resource))); if (name) { NAME_D3D12RESOURCE((*resource), name); } } inline void AllocateUploadBuffer(Device* device, void* data, UINT64 size, ID3D12Resource** resource, const wchar_t* name = nullptr) { auto heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); auto buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(size); TRY(device->m_native->CreateCommittedResource( &heap_properties, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(resource))); if (name) { NAME_D3D12RESOURCE((*resource), name); } void *mapped_data; (*resource)->Map(0, nullptr, &mapped_data); memcpy(mapped_data, data, size); (*resource)->Unmap(0, nullptr); } inline void UpdateUploadbuffer(void* data, UINT64 size, ID3D12Resource* resource) { void *mapped_data; resource->Map(0, nullptr, &mapped_data); memcpy(mapped_data, data, size); resource->Unmap(0, nullptr); } WRAPPED_GPU_POINTER CreateFallbackWrappedPointer( Device* device, DescriptorHeap* heap, std::uint32_t index, ID3D12Resource* resource, UINT buffer_num_elements) { if (GetRaytracingType(device) != RaytracingType::FALLBACK) { LOGW("CreateFallbackWrappedPointer got called but the device isn't setup for fallback."); } D3D12_UNORDERED_ACCESS_VIEW_DESC rawBufferUavDesc = {}; rawBufferUavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER; rawBufferUavDesc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW; rawBufferUavDesc.Format = DXGI_FORMAT_R32_TYPELESS; rawBufferUavDesc.Buffer.NumElements = buffer_num_elements; d3d12::DescHeapCPUHandle bottom_level_descriptor; // Only compute fallback requires a valid descriptor index when creating a wrapped pointer. UINT desc_heap_idx = index; // TODO don't hardcode this. if (!device->m_fallback_native->UsingRaytracingDriver()) { for (auto frame_idx = 0; frame_idx < d3d12::settings::num_back_buffers; frame_idx++) { bottom_level_descriptor = d3d12::GetCPUHandle(heap, frame_idx, 0); // TODO: Don't harcode this. d3d12::Offset(bottom_level_descriptor, desc_heap_idx, heap->m_increment_size); device->m_native->CreateUnorderedAccessView(resource, nullptr, &rawBufferUavDesc, bottom_level_descriptor.m_native); } } return device->m_fallback_native->GetWrappedPointerSimple(desc_heap_idx, resource->GetGPUVirtualAddress()); } inline void UpdatePrebuildInfo(Device* device, AccelerationStructure& as, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs) { if (GetRaytracingType(device) == RaytracingType::NATIVE) { device->m_native->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &as.m_prebuild_info); } else if (GetRaytracingType(device) == RaytracingType::FALLBACK) { device->m_fallback_native->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &as.m_prebuild_info); } if (!(as.m_prebuild_info.ResultDataMaxSizeInBytes > 0)) LOGW("Result data max size in bytes is more than zero. accel structure"); } inline void BuildAS(Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC const & desc) { auto BuildAccelerationStructure = [&](auto * raytracingCommandList) { raytracingCommandList->BuildRaytracingAccelerationStructure(&desc, 0, nullptr); }; // Build acceleration structure. if (GetRaytracingType(device) == RaytracingType::NATIVE) { BuildAccelerationStructure(cmd_list->m_native); } else if (GetRaytracingType(device) == RaytracingType::FALLBACK) { // Set the descriptor heaps to be used during acceleration structure build for the Fallback Layer. d3d12::BindDescriptorHeap(cmd_list, desc_heap, desc_heap->m_create_info.m_type, 0, true); //TODO: note this non frame idx BuildAccelerationStructure(cmd_list->m_native_fallback); } } inline void CreateInstancesForTLAS(Device* device, AccelerationStructure& tlas, DescriptorHeap* desc_heap, std::vector blas_list, std::uint32_t frame_idx, bool update) { // Falback layer heap offset auto fallback_heap_idx = d3d12::settings::fallback_ptrs_offset; // Create the instances to the bottom level instances. if (GetRaytracingType(device) == RaytracingType::NATIVE) { std::vector instance_descs; for (auto it : blas_list) { auto blas = it.m_as; auto material = it.m_material; auto transform = it.m_transform; D3D12_RAYTRACING_INSTANCE_DESC instance_desc = {}; XMStoreFloat3x4(reinterpret_cast(instance_desc.Transform), transform); instance_desc.InstanceMask = 1; instance_desc.InstanceID = material; instance_desc.AccelerationStructure = blas.m_natives[frame_idx]->GetGPUVirtualAddress(); instance_descs.push_back(instance_desc); } if (update) { internal::UpdateUploadbuffer(instance_descs.data(), sizeof(D3D12_RAYTRACING_INSTANCE_DESC) * blas_list.size(), tlas.m_instance_descs[frame_idx]); } else { for (auto& inst_desc : tlas.m_instance_descs) { internal::AllocateUploadBuffer(device, instance_descs.data(), sizeof(D3D12_RAYTRACING_INSTANCE_DESC) * blas_list.size(), &inst_desc, L"InstanceDescs"); } } } else if (GetRaytracingType(device) == RaytracingType::FALLBACK) { std::vector instance_descs; for (auto it : blas_list) { auto blas = it.m_as; auto material = it.m_material; auto transform = it.m_transform; D3D12_RAYTRACING_FALLBACK_INSTANCE_DESC instance_desc = {}; XMStoreFloat3x4(reinterpret_cast(instance_desc.Transform), transform); instance_desc.InstanceMask = 1; instance_desc.InstanceID = material; std::uint32_t num_buffer_elements = static_cast(blas.m_prebuild_info.ResultDataMaxSizeInBytes) / sizeof(std::uint32_t); instance_desc.AccelerationStructure = internal::CreateFallbackWrappedPointer(device, desc_heap, fallback_heap_idx, blas.m_natives[frame_idx], num_buffer_elements); instance_descs.push_back(instance_desc); fallback_heap_idx++; } if (update) { internal::UpdateUploadbuffer(instance_descs.data(), sizeof(D3D12_RAYTRACING_FALLBACK_INSTANCE_DESC) * instance_descs.size(), tlas.m_instance_descs[frame_idx]); } else { for (auto& inst_desc : tlas.m_instance_descs) { internal::AllocateUploadBuffer(device, instance_descs.data(), sizeof(D3D12_RAYTRACING_FALLBACK_INSTANCE_DESC) * instance_descs.size(), &inst_desc, L"InstanceDescs"); } } } // Create a wrapped pointer to the acceleration structure. if (GetRaytracingType(device) == RaytracingType::FALLBACK) { std::uint32_t num_buffer_elements = static_cast(tlas.m_prebuild_info.ResultDataMaxSizeInBytes) / sizeof(std::uint32_t); tlas.m_fallback_tlas_ptr = internal::CreateFallbackWrappedPointer(device, desc_heap, fallback_heap_idx, tlas.m_natives[0], num_buffer_elements); } } inline void CopyInstDescResource(CommandList* cmd_list, AccelerationStructure& as, std::uint32_t source_index, std::uint32_t target_index) { cmd_list->m_native->CopyResource(as.m_instance_descs[target_index], as.m_instance_descs[source_index]); } inline void CopyAS(Device* device, CommandList* cmd_list, AccelerationStructure& as, std::uint32_t source_index, std::uint32_t target_index) { if (GetRaytracingType(device) == RaytracingType::NATIVE) { cmd_list->m_native->CopyRaytracingAccelerationStructure(as.m_natives[target_index]->GetGPUVirtualAddress(), as.m_natives[source_index]->GetGPUVirtualAddress(), D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE); } else if (GetRaytracingType(device) == RaytracingType::FALLBACK) { cmd_list->m_native_fallback->CopyRaytracingAccelerationStructure(as.m_natives[target_index]->GetGPUVirtualAddress(), as.m_natives[source_index]->GetGPUVirtualAddress(), D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE); } } } /* internal */ #include [[nodiscard]] AccelerationStructure CreateBottomLevelAccelerationStructures(Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, std::vector geometry) { AccelerationStructure blas = {}; std::vector geometry_descs(geometry.size()); for (auto i = 0; i < geometry.size(); i++) { auto geom = geometry[i]; geometry_descs[i].Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES; if (auto index_buffer = geom.index_buffer.value_or(nullptr)) { geometry_descs[i].Triangles.IndexBuffer = index_buffer->m_buffer->GetGPUVirtualAddress() + (geom.m_indices_offset * index_buffer->m_stride_in_bytes); geometry_descs[i].Triangles.IndexCount = geom.m_num_indices; geometry_descs[i].Triangles.IndexFormat = DXGI_FORMAT_R32_UINT; geometry_descs[i].Triangles.Transform3x4 = 0; } else { geometry_descs[i].Triangles.IndexBuffer = 0; geometry_descs[i].Triangles.IndexCount = 0; geometry_descs[i].Triangles.IndexFormat = DXGI_FORMAT_UNKNOWN; geometry_descs[i].Triangles.Transform3x4 = 0; } geometry_descs[i].Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT; geometry_descs[i].Triangles.VertexCount = geom.m_num_vertices; geometry_descs[i].Triangles.VertexBuffer.StartAddress = geom.vertex_buffer->m_buffer->GetGPUVirtualAddress() + (geom.m_vertices_offset * geom.m_vertex_stride); geometry_descs[i].Triangles.VertexBuffer.StrideInBytes = geom.m_vertex_stride; geometry_descs[i].Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE; } D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS build_flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE; // Get prebuild info bottom level D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS bottom_level_inputs; bottom_level_inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; bottom_level_inputs.NumDescs = static_cast(geometry.size()); bottom_level_inputs.pGeometryDescs = geometry_descs.data(); bottom_level_inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY; bottom_level_inputs.Flags = build_flags; // Get bottom level prebuild info internal::UpdatePrebuildInfo(device, blas, bottom_level_inputs); // Allocate scratch resource internal::AllocateUAVBuffer(device, blas.m_prebuild_info.ScratchDataSizeInBytes, &blas.m_scratch, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, L"Acceleration Structure Scratch Resource"); // Allocate resources for acceleration structures. for (auto& as : blas.m_natives) { D3D12_RESOURCE_STATES initial_resource_state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE; if (GetRaytracingType(device) == RaytracingType::FALLBACK) { initial_resource_state = device->m_fallback_native->GetAccelerationStructureResourceState(); } internal::AllocateUAVBuffer(device, blas.m_prebuild_info.ResultDataMaxSizeInBytes, &as, initial_resource_state, L"BottomLevelAccelerationStructure"); } // Bottom Level Acceleration Structure desc D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC bottom_level_build_desc = {}; { bottom_level_build_desc.Inputs = bottom_level_inputs; bottom_level_build_desc.ScratchAccelerationStructureData = blas.m_scratch->GetGPUVirtualAddress(); bottom_level_build_desc.DestAccelerationStructureData = blas.m_natives[0]->GetGPUVirtualAddress(); } internal::BuildAS(device, cmd_list, desc_heap, bottom_level_build_desc); d3d12::UAVBarrierAS(cmd_list, blas, 0); for (std::uint8_t i = 1; i < settings::num_back_buffers; i++) { internal::CopyAS(device, cmd_list, blas, 0, i); } return blas; } AccelerationStructure CreateTopLevelAccelerationStructure(Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, std::vector blas_list) { AccelerationStructure tlas = {}; D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS build_flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE | D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE; D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS top_level_inputs; top_level_inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY; top_level_inputs.Flags = build_flags; top_level_inputs.NumDescs = static_cast(blas_list.size()); top_level_inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; // Get prebuild info top level internal::UpdatePrebuildInfo(device, tlas, top_level_inputs); // Allocate scratch resource internal::AllocateUAVBuffer(device, tlas.m_prebuild_info.ScratchDataSizeInBytes, &tlas.m_scratch, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, L"Acceleration Structure Scratch Resource"); // Allocate acceleration structure buffer for (auto& as : tlas.m_natives) { D3D12_RESOURCE_STATES initial_resoruce_state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE; if (GetRaytracingType(device) == RaytracingType::FALLBACK) { initial_resoruce_state = device->m_fallback_native->GetAccelerationStructureResourceState(); } internal::AllocateUAVBuffer(device, tlas.m_prebuild_info.ResultDataMaxSizeInBytes, &as, initial_resoruce_state, L"TopLevelAccelerationStructure"); } // Create the instances to the bottom level instances if (!blas_list.empty()) { internal::CreateInstancesForTLAS(device, tlas, desc_heap, blas_list, 0, false); } // Top Level Acceleration Structure desc D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC top_level_build_desc = {}; { if (!blas_list.empty()) { top_level_inputs.InstanceDescs = tlas.m_instance_descs[0]->GetGPUVirtualAddress(); } top_level_build_desc.Inputs = top_level_inputs; top_level_build_desc.DestAccelerationStructureData = tlas.m_natives[0]->GetGPUVirtualAddress(); top_level_build_desc.ScratchAccelerationStructureData = tlas.m_scratch->GetGPUVirtualAddress(); } internal::BuildAS(device, cmd_list, desc_heap, top_level_build_desc); d3d12::UAVBarrierAS(cmd_list, tlas, 0); for (std::uint8_t i = 1; i < settings::num_back_buffers; i++) { internal::CopyAS(device, cmd_list, tlas, 0, i); } return tlas; } void DestroyAccelerationStructure(AccelerationStructure& structure) { SAFE_RELEASE(structure.m_scratch); for (auto& as : structure.m_natives) { SAFE_RELEASE(as); } for (auto& inst_desc : structure.m_instance_descs) { SAFE_RELEASE(inst_desc); } } void UAVBarrierAS(CommandList* cmd_list, AccelerationStructure const & structure, std::uint32_t frame_idx) { auto barrier = CD3DX12_RESOURCE_BARRIER::UAV(structure.m_natives[frame_idx]); cmd_list->m_native->ResourceBarrier(1, &barrier); } void UpdateTopLevelAccelerationStructure(AccelerationStructure& tlas, Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, std::vector blas_list, std::uint32_t frame_idx) { D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS build_flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE | D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE; D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS top_level_inputs; top_level_inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY; top_level_inputs.Flags = build_flags; top_level_inputs.NumDescs = static_cast(blas_list.size()); top_level_inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO old_prebuild_info = tlas.m_prebuild_info; internal::UpdatePrebuildInfo(device, tlas, top_level_inputs); top_level_inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE; bool rebuild_accel_structure = old_prebuild_info.ResultDataMaxSizeInBytes != tlas.m_prebuild_info.ResultDataMaxSizeInBytes || old_prebuild_info.ScratchDataSizeInBytes != tlas.m_prebuild_info.ScratchDataSizeInBytes || old_prebuild_info.UpdateScratchDataSizeInBytes != tlas.m_prebuild_info.UpdateScratchDataSizeInBytes; if (rebuild_accel_structure) { LOGW("Complete AS rebuild triggered. This might break versioining"); tlas = CreateTopLevelAccelerationStructure(device, cmd_list, desc_heap, blas_list); } else { // Create the instances to the bottom level instances. if (!blas_list.empty()) { internal::CreateInstancesForTLAS(device, tlas, desc_heap, blas_list, frame_idx, true); } // Top Level Acceleration Structure desc D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC top_level_build_desc = {}; { auto barrier = CD3DX12_RESOURCE_BARRIER::UAV(tlas.m_natives[frame_idx]); cmd_list->m_native->ResourceBarrier(1, &barrier); if (!blas_list.empty()) { top_level_inputs.InstanceDescs = tlas.m_instance_descs[frame_idx]->GetGPUVirtualAddress(); } top_level_build_desc.Inputs = top_level_inputs; top_level_build_desc.SourceAccelerationStructureData = tlas.m_natives[frame_idx]->GetGPUVirtualAddress(); //TODO: Benchmark performance when taking the previous source. top_level_build_desc.DestAccelerationStructureData = tlas.m_natives[frame_idx]->GetGPUVirtualAddress(); top_level_build_desc.ScratchAccelerationStructureData = tlas.m_scratch->GetGPUVirtualAddress(); } internal::BuildAS(device, cmd_list, desc_heap, top_level_build_desc); } } void SetName(AccelerationStructure& acceleration_structure, std::wstring name) { for (auto& as : acceleration_structure.m_natives) { as->SetName((name + L" - Acceleration Structure").c_str()); } for (auto& inst_desc : acceleration_structure.m_instance_descs) { if (inst_desc) { inst_desc->SetName((name + L" - Acceleration Structure").c_str()); } } } void CreateOrUpdateTLAS(Device* device, CommandList* cmd_list, bool& requires_init, d3d12::AccelerationStructure& out_tlas, std::vector blas_list, std::uint32_t frame_idx) { d3d12::DescriptorHeap* heap = static_cast(cmd_list)->m_rt_descriptor_heap->GetHeap(); if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { if (requires_init) { out_tlas = d3d12::CreateTopLevelAccelerationStructure(device, cmd_list, heap, blas_list); requires_init = false; } else { d3d12::UpdateTopLevelAccelerationStructure(out_tlas, device, cmd_list, heap, blas_list, frame_idx); } } } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_command_list.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "d3d12_dynamic_descriptor_heap.hpp" #include "d3d12_rt_descriptor_heap.hpp" #include "d3d12_texture_resources.hpp" #include "../util/log.hpp" #include "d3d12_defines.hpp" namespace wr::d3d12 { CommandList* CreateCommandList(Device* device, unsigned int num_allocators, CmdListType type) { auto* cmd_list = new CommandList(); const auto n_device = device->m_native; auto& n_cmd_list = cmd_list->m_native; cmd_list->m_allocators.resize(num_allocators); // Create the allocators for (auto& allocator : cmd_list->m_allocators) { TRY_M(n_device->CreateCommandAllocator((D3D12_COMMAND_LIST_TYPE)type, IID_PPV_ARGS(&allocator)), "Failed to create command allocator"); NAME_D3D12RESOURCE(allocator); } // Create the command lists TRY_M(device->m_native->CreateCommandList( 0, (D3D12_COMMAND_LIST_TYPE)type, cmd_list->m_allocators[0], NULL, IID_PPV_ARGS(&n_cmd_list) ), "Failed to create command list"); NAME_D3D12RESOURCE(n_cmd_list); n_cmd_list->Close(); // TODO: Can be optimized away. if (GetRaytracingType(device) == RaytracingType::FALLBACK) { device->m_fallback_native->QueryRaytracingCommandList(n_cmd_list, IID_PPV_ARGS(&cmd_list->m_native_fallback)); } //Create the heaps for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { cmd_list->m_dynamic_descriptor_heaps[i] = std::make_unique(device, static_cast(i)); cmd_list->m_descriptor_heaps[i] = nullptr; } cmd_list->m_rt_descriptor_heap = std::make_shared(device, DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); return cmd_list; } void SetName(CommandList* cmd_list, std::string const& name) { SetName(cmd_list, std::wstring(name.begin(), name.end())); } void SetName(CommandList* cmd_list, std::wstring const& name) { cmd_list->m_native->SetName(name.c_str()); for (auto& allocator : cmd_list->m_allocators) { allocator->SetName((name + L" Allocator").c_str()); } } void Begin(CommandList* cmd_list, unsigned int frame_idx) { // TODO: move resetting to when the command list is executed. This is how vulkan does it. TRY_M(cmd_list->m_allocators[frame_idx]->Reset(), "Failed to reset cmd allocators"); // Only reset with pipeline state if using bundles since only then this will impact fps. // Otherwise its just easier to pass NULL and suffer the insignificant performance loss. TRY_M(cmd_list->m_native->Reset(cmd_list->m_allocators[frame_idx], NULL), "Failed to reset command list."); for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { cmd_list->m_dynamic_descriptor_heaps[i]->Reset(); cmd_list->m_descriptor_heaps[i] = nullptr; } cmd_list->m_rt_descriptor_heap->Reset(frame_idx); } void End(CommandList* cmd_list) { cmd_list->m_native->Close(); } void ExecuteBundle(CommandList* cmd_list, CommandList* bundle) { cmd_list->m_native->ExecuteBundle(bundle->m_native); } void ExecuteIndirect(CommandList* cmd_list, CommandSignature* cmd_signature, IndirectCommandBuffer* buffer, uint32_t frame_idx) { cmd_list->m_native->ExecuteIndirect(cmd_signature->m_native, static_cast(buffer->m_num_commands), buffer->m_native[frame_idx], 0, nullptr, 0); } void BindRenderTarget(CommandList* cmd_list, RenderTarget* render_target, bool clear, bool clear_depth) { std::vector handles; handles.resize(render_target->m_render_targets.size()); for (auto i = 0; i < handles.size(); i++) { handles[i] = CD3DX12_CPU_DESCRIPTOR_HANDLE(render_target->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart(), i, render_target->m_rtv_descriptor_increment_size); } CD3DX12_CPU_DESCRIPTOR_HANDLE dsv_handle; if (render_target->m_create_info.m_create_dsv_buffer) { dsv_handle = render_target->m_depth_stencil_resource_heap->GetCPUDescriptorHandleForHeapStart(); } cmd_list->m_native->OMSetRenderTargets(static_cast(handles.size()), handles.data(), false, render_target->m_create_info.m_create_dsv_buffer ? &dsv_handle : nullptr); if (clear) { for (auto& handle : handles) { cmd_list->m_native->ClearRenderTargetView(handle, render_target->m_create_info.m_clear_color, 0, nullptr); } } if (clear_depth && render_target->m_create_info.m_create_dsv_buffer) { cmd_list->m_native->ClearDepthStencilView(dsv_handle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr); } } void BindRenderTargetVersioned(CommandList* cmd_list, RenderTarget* render_target, unsigned int frame_idx, bool clear, bool clear_depth) { CD3DX12_CPU_DESCRIPTOR_HANDLE rtv_handle(render_target->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart(), frame_idx % render_target->m_render_targets.size(), render_target->m_rtv_descriptor_increment_size); CD3DX12_CPU_DESCRIPTOR_HANDLE dsv_handle; if (render_target->m_create_info.m_create_dsv_buffer) { dsv_handle = render_target->m_depth_stencil_resource_heap->GetCPUDescriptorHandleForHeapStart(); } cmd_list->m_native->OMSetRenderTargets(1, &rtv_handle, false, render_target->m_create_info.m_create_dsv_buffer ? &dsv_handle : nullptr); if (clear) { cmd_list->m_native->ClearRenderTargetView(rtv_handle, render_target->m_create_info.m_clear_color, 0, nullptr); } if (clear_depth && render_target->m_create_info.m_create_dsv_buffer) { cmd_list->m_native->ClearDepthStencilView(dsv_handle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr); } } void BindRenderTargetOnlyDepth(CommandList* cmd_list, RenderTarget* render_target, bool clear) { CD3DX12_CPU_DESCRIPTOR_HANDLE dsv_handle; if (render_target->m_create_info.m_create_dsv_buffer) { dsv_handle = render_target->m_depth_stencil_resource_heap->GetCPUDescriptorHandleForHeapStart(); } cmd_list->m_native->OMSetRenderTargets(0, nullptr, false, render_target->m_create_info.m_create_dsv_buffer ? &dsv_handle : nullptr); if (clear) { cmd_list->m_native->ClearDepthStencilView(dsv_handle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr); } } void BindPipeline(CommandList* cmd_list, PipelineState* pipeline_state) // TODO: Binding the root signature seperatly can improve perf if done right. { cmd_list->m_native->SetPipelineState(pipeline_state->m_native); cmd_list->m_native->SetGraphicsRootSignature(pipeline_state->m_root_signature->m_native); for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { cmd_list->m_dynamic_descriptor_heaps[i]->ParseRootSignature(*pipeline_state->m_root_signature); } } void BindDescriptorHeap(CommandList* cmd_list, DescriptorHeap* heap, DescriptorHeapType type, unsigned int frame_idx, bool fallback) { std::uint32_t heap_idx = frame_idx % heap->m_create_info.m_versions; if (cmd_list->m_descriptor_heaps[static_cast(type)] != heap->m_native[heap_idx]) { cmd_list->m_descriptor_heaps[static_cast(type)] = heap->m_native[heap_idx]; BindDescriptorHeaps(cmd_list, fallback); } } void BindDescriptorHeaps(CommandList* cmd_list, bool fallback) { std::uint32_t num_heaps = 0; ID3D12DescriptorHeap* n_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES] = {}; for (uint32_t i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { ID3D12DescriptorHeap* heap = cmd_list->m_descriptor_heaps[i]; if (heap) { n_heaps[num_heaps++] = heap; } } if (fallback) { cmd_list->m_native_fallback->SetDescriptorHeaps(num_heaps, n_heaps); } else { cmd_list->m_native->SetDescriptorHeaps(num_heaps, n_heaps); } } void BindComputePipeline(CommandList* cmd_list, PipelineState * pipeline_state) { if (pipeline_state->m_desc.m_type != PipelineType::COMPUTE_PIPELINE) LOGW("Tried to bind a graphics pipeline as a compute pipeline"); cmd_list->m_native->SetPipelineState(pipeline_state->m_native); cmd_list->m_native->SetComputeRootSignature(pipeline_state->m_root_signature->m_native); for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { cmd_list->m_dynamic_descriptor_heaps[i]->ParseRootSignature(*pipeline_state->m_root_signature); } } void BindRaytracingPipeline(CommandList* cmd_list, StateObject* state_object, bool fallback) { cmd_list->m_native->SetComputeRootSignature(state_object->m_global_root_signature->m_native); if (fallback) { cmd_list->m_native_fallback->SetPipelineState1(state_object->m_fallback_native); } else { cmd_list->m_native->SetPipelineState1(state_object->m_native); } cmd_list->m_rt_descriptor_heap->ParseRootSignature(*state_object->m_global_root_signature); } void BindViewport(CommandList* cmd_list, Viewport const & viewport) { cmd_list->m_native->RSSetViewports(1, &viewport.m_viewport); cmd_list->m_native->RSSetScissorRects(1, &viewport.m_scissor_rect); } void BindVertexBuffer(CommandList* cmd_list, StagingBuffer* buffer, std::size_t offset, std::size_t size, std::size_t stride) { if (!buffer->m_gpu_address) { LOGC("No GPU address found. Has the buffer beens staged?"); } D3D12_VERTEX_BUFFER_VIEW view; view.BufferLocation = buffer->m_gpu_address + offset; view.StrideInBytes = static_cast(stride); view.SizeInBytes = static_cast(size); cmd_list->m_native->IASetVertexBuffers(0, 1, &view); } void BindIndexBuffer(CommandList* cmd_list, StagingBuffer* buffer, std::uint32_t offset, std::uint32_t size) { if (!buffer->m_gpu_address) { throw "No GPU address found. Has the buffer beens staged?"; } D3D12_INDEX_BUFFER_VIEW view; view.BufferLocation = buffer->m_gpu_address + offset; view.Format = DXGI_FORMAT_R32_UINT; view.SizeInBytes = size; cmd_list->m_native->IASetIndexBuffer(&view); } void BindConstantBuffer(CommandList* cmd_list, HeapResource* buffer, unsigned int root_parameter_idx, unsigned int frame_idx) { cmd_list->m_native->SetGraphicsRootConstantBufferView(root_parameter_idx, buffer->m_gpu_addresses[frame_idx]); } void 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) { cmd_list->m_native->SetGraphicsRoot32BitConstants(root_parameter_idx, num_of_values_to_set, data_to_set, dest_offset_in_32bit_values); } void 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) { cmd_list->m_native->SetComputeRoot32BitConstants(root_parameter_idx, num_of_values_to_set, data_to_set, dest_offset_in_32bit_values); } void BindComputeConstantBuffer(CommandList * cmd_list, HeapResource* buffer, unsigned int root_parameter_idx, unsigned int frame_idx) { cmd_list->m_native->SetComputeRootConstantBufferView(root_parameter_idx, buffer->m_gpu_addresses[frame_idx]); } void BindComputeShaderResourceView(CommandList * cmd_list, ID3D12Resource* resource, unsigned int root_parameter_idx) { cmd_list->m_native->SetComputeRootShaderResourceView(root_parameter_idx, resource->GetGPUVirtualAddress()); } void BindComputeUnorederedAccessView(CommandList * cmd_list, ID3D12Resource* resource, unsigned int root_parameter_idx) { cmd_list->m_native->SetComputeRootUnorderedAccessView(root_parameter_idx, resource->GetGPUVirtualAddress()); } void BindDescriptorTable(CommandList* cmd_list, DescHeapGPUHandle& handle, unsigned int root_param_index) { cmd_list->m_native->SetGraphicsRootDescriptorTable(root_param_index, handle.m_native); } void BindComputeDescriptorTable(CommandList * cmd_list, DescHeapGPUHandle & handle, unsigned int root_param_index) { cmd_list->m_native->SetComputeRootDescriptorTable(root_param_index, handle.m_native); } void SetPrimitiveTopology(CommandList* cmd_list, D3D12_PRIMITIVE_TOPOLOGY topology) { cmd_list->m_native->IASetPrimitiveTopology(topology); } void Draw(CommandList* cmd_list, std::uint32_t vertex_count, std::uint32_t inst_count, std::uint32_t vertex_start) { for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { cmd_list->m_dynamic_descriptor_heaps[i]->CommitStagedDescriptorsForDraw(*cmd_list); } cmd_list->m_native->DrawInstanced(vertex_count, inst_count, vertex_start, 0); } void DrawIndexed(CommandList* cmd_list, std::uint32_t idx_count, std::uint32_t inst_count, std::uint32_t idx_start, std::uint32_t vertex_start) { for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { cmd_list->m_dynamic_descriptor_heaps[i]->CommitStagedDescriptorsForDraw(*cmd_list); } cmd_list->m_native->DrawIndexedInstanced(idx_count, inst_count, idx_start, vertex_start, 0); } void Dispatch(CommandList * cmd_list, unsigned int thread_group_count_x, unsigned int thread_group_count_y, unsigned int thread_group_count_z) { for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { cmd_list->m_dynamic_descriptor_heaps[i]->CommitStagedDescriptorsForDispatch(*cmd_list); } cmd_list->m_native->Dispatch(thread_group_count_x, thread_group_count_y, thread_group_count_z); } void Transition(CommandList* cmd_list, RenderTarget* render_target, unsigned int frame_index, ResourceState from, ResourceState to) { CD3DX12_RESOURCE_BARRIER end_transition = CD3DX12_RESOURCE_BARRIER::Transition( render_target->m_render_targets[frame_index % render_target->m_render_targets.size()], (D3D12_RESOURCE_STATES)from, (D3D12_RESOURCE_STATES)to ); cmd_list->m_native->ResourceBarrier(1, &end_transition); } void Transition(CommandList* cmd_list, RenderTarget* render_target, ResourceState from, ResourceState to) { std::vector barriers; barriers.resize(render_target->m_num_render_targets); for (auto i = 0u; i < render_target->m_num_render_targets; i++) { CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition( render_target->m_render_targets[i], (D3D12_RESOURCE_STATES)from, (D3D12_RESOURCE_STATES)to ); barriers[i] = barrier; } cmd_list->m_native->ResourceBarrier(static_cast(barriers.size()), barriers.data()); } void Transition(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to) { if (texture->m_subresource_states[0] != to) { CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(texture->m_resource, (D3D12_RESOURCE_STATES)from, (D3D12_RESOURCE_STATES)to); std::fill(texture->m_subresource_states.begin(), texture->m_subresource_states.end(), to); cmd_list->m_native->ResourceBarrier(1, &barrier); } } void Transition(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to, unsigned int first_subresource, unsigned int num_subresources) { if (num_subresources < D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES) { for (uint32_t i = 0; i < num_subresources; ++i) { TransitionSubresource(cmd_list, texture, from, to, first_subresource + i); } } else { Transition(cmd_list, texture, from, to); } } void TransitionSubresource(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to, unsigned int subresource) { if (texture->m_subresource_states[subresource] != to) { CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(texture->m_resource, (D3D12_RESOURCE_STATES)from, (D3D12_RESOURCE_STATES)to, subresource); texture->m_subresource_states[subresource] = to; cmd_list->m_native->ResourceBarrier(1, &barrier); } } void Transition(CommandList* cmd_list, std::vector const& textures, ResourceState from, ResourceState to) { std::vector barriers; for (auto texture : textures) { if (texture->m_subresource_states[0] != to) { CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition( texture->m_resource, (D3D12_RESOURCE_STATES)from, (D3D12_RESOURCE_STATES)to ); std::fill(texture->m_subresource_states.begin(), texture->m_subresource_states.end(), to); barriers.push_back(barrier); } } cmd_list->m_native->ResourceBarrier(static_cast(barriers.size()), barriers.data()); } void Transition(CommandList* cmd_list, IndirectCommandBuffer* buffer, ResourceState from, ResourceState to, uint32_t frame_idx) { CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition( buffer->m_native[frame_idx], (D3D12_RESOURCE_STATES)from, (D3D12_RESOURCE_STATES)to ); cmd_list->m_native->ResourceBarrier(1, &barrier); } void Transition(CommandList* cmd_list, StagingBuffer* buffer, ResourceState from, ResourceState to) { CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition( buffer->m_buffer, (D3D12_RESOURCE_STATES)from, (D3D12_RESOURCE_STATES)to ); cmd_list->m_native->ResourceBarrier(1, &barrier); } void TransitionDepth(CommandList* cmd_list, RenderTarget* render_target, ResourceState from, ResourceState to) { CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition( render_target->m_depth_stencil_buffer, (D3D12_RESOURCE_STATES)from, (D3D12_RESOURCE_STATES)to ); cmd_list->m_native->ResourceBarrier(1, &barrier); } void UAVBarrier(CommandList* cmd_list, std::vector const & resources) { std::vector barriers; barriers.reserve(resources.size()); for (auto& resource : resources) { barriers.emplace_back(CD3DX12_RESOURCE_BARRIER::UAV(resource->m_resource)); } cmd_list->m_native->ResourceBarrier(static_cast(barriers.size()), barriers.data()); } void UAVBarrier(CommandList* cmd_list, std::vector const & resources) { std::vector barriers; barriers.reserve(resources.size()); for (auto& resource : resources) { barriers.emplace_back(CD3DX12_RESOURCE_BARRIER::UAV(resource)); } cmd_list->m_native->ResourceBarrier(static_cast(barriers.size()), barriers.data()); } void Alias(CommandList* cmd_list, TextureResource* resource_before, TextureResource* resource_after) { auto before = (resource_before) ? resource_before->m_resource : nullptr; auto after = (resource_after) ? resource_after->m_resource : nullptr; CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Aliasing(before, after); cmd_list->m_native->ResourceBarrier(1, &barrier); } void Destroy(CommandList* cmd_list) { SAFE_RELEASE(cmd_list->m_native); SAFE_RELEASE(cmd_list->m_native_fallback) for (auto& allocator : cmd_list->m_allocators) { SAFE_RELEASE(allocator); } delete cmd_list; } CommandSignature* CreateCommandSignature(Device* device, RootSignature* root_signature, std::vector arg_descs, size_t byte_stride) { auto cmd_sig = new CommandSignature(); D3D12_COMMAND_SIGNATURE_DESC cmd_signature_desc = {}; cmd_signature_desc.pArgumentDescs = arg_descs.data(); cmd_signature_desc.NumArgumentDescs = static_cast(arg_descs.size()); cmd_signature_desc.ByteStride = static_cast(byte_stride); TRY_M(device->m_native->CreateCommandSignature(&cmd_signature_desc, root_signature->m_native, IID_PPV_ARGS(&cmd_sig->m_native)) , "Failed to create command signature"); return cmd_sig; } void 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) { D3D12_DISPATCH_RAYS_DESC desc = {}; if (hitgroup_table != nullptr) { desc.HitGroupTable.StartAddress = hitgroup_table->m_resource->GetGPUVirtualAddress(); desc.HitGroupTable.SizeInBytes = hitgroup_table->m_resource->GetDesc().Width; desc.HitGroupTable.StrideInBytes = hitgroup_table->m_shader_record_size; } desc.MissShaderTable.StartAddress = miss_table->m_resource->GetGPUVirtualAddress(); desc.MissShaderTable.SizeInBytes = miss_table->m_resource->GetDesc().Width; desc.MissShaderTable.StrideInBytes = miss_table->m_shader_record_size; desc.RayGenerationShaderRecord.StartAddress = raygen_table->m_resource->GetGPUVirtualAddress(); desc.RayGenerationShaderRecord.SizeInBytes = raygen_table->m_resource->GetDesc().Width; // Dimensions of the image to render, identical to a window dimensions desc.Width = width; desc.Height = height; desc.Depth = depth; cmd_list->m_rt_descriptor_heap->CommitStagedDescriptorsForDispatch(*cmd_list, frame_idx); if (cmd_list->m_native_fallback) { cmd_list->m_native_fallback->DispatchRays(&desc); } else { cmd_list->m_native->DispatchRays(&desc); } } void SetName(CommandSignature * cmd_signature, std::wstring name) { cmd_signature->m_native->SetName(name.c_str()); } void Destroy(CommandSignature* cmd_signature) { SAFE_RELEASE(cmd_signature->m_native); delete cmd_signature; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_command_queue.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "../util/log.hpp" #include "d3d12_defines.hpp" namespace wr::d3d12 { CommandQueue* CreateCommandQueue(Device* device, CmdListType type) { auto cmd_queue = new CommandQueue(); const auto n_device = device->m_native; D3D12_COMMAND_QUEUE_DESC cmd_queue_desc = {}; cmd_queue_desc.Flags = settings::enable_gpu_timeout ? D3D12_COMMAND_QUEUE_FLAG_NONE : D3D12_COMMAND_QUEUE_FLAG_DISABLE_GPU_TIMEOUT; cmd_queue_desc.Type = static_cast(type); TRY_M(n_device->CreateCommandQueue(&cmd_queue_desc, IID_PPV_ARGS(&cmd_queue->m_native)), "Failed to create DX12 command queue."); NAME_D3D12RESOURCE(cmd_queue->m_native); return cmd_queue; } void Execute(CommandQueue* cmd_queue, std::vector const & cmd_lists, Fence* fence) { std::vector native_lists; native_lists.resize(cmd_lists.size()); for (auto i = 0; i < native_lists.size(); i++) { native_lists[i] = cmd_lists[i]->m_native; } cmd_queue->m_native->ExecuteCommandLists(static_cast(native_lists.size()), native_lists.data()); fence->m_fence_value++; Signal(fence, cmd_queue); } void Destroy(CommandQueue* cmd_queue) { SAFE_RELEASE(cmd_queue->m_native); delete cmd_queue; } void SetName(CommandQueue * cmd_queue, std::wstring name) { cmd_queue->m_native->SetName(name.c_str()); } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_constant_buffer_pool.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_constant_buffer_pool.hpp" #include "d3d12_functions.hpp" #include "d3d12_settings.hpp" #include "d3d12_renderer.hpp" #include "d3d12_defines.hpp" namespace wr { D3D12ConstantBufferPool::D3D12ConstantBufferPool(D3D12RenderSystem& render_system, std::size_t size_in_bytes) : ConstantBufferPool(SizeAlignTwoPower(size_in_bytes, 65536)), m_render_system(render_system) { m_heap = d3d12::CreateHeap_SBO(render_system.m_device, SizeAlignTwoPower(size_in_bytes, 65536), ResourceType::BUFFER, d3d12::settings::num_back_buffers); SetName(m_heap, L"Default SBO Heap"); d3d12::MapHeap(m_heap); } D3D12ConstantBufferPool::~D3D12ConstantBufferPool() { d3d12::Destroy(m_heap); for (int i = 0; i < m_constant_buffer_handles.size(); ++i) { delete m_constant_buffer_handles[i]; } } void D3D12ConstantBufferPool::Evict() { d3d12::Evict(m_heap); } void D3D12ConstantBufferPool::MakeResident() { d3d12::MakeResident(m_heap); } ConstantBufferHandle* D3D12ConstantBufferPool::AllocateConstantBuffer(std::size_t buffer_size) { D3D12ConstantBufferHandle* handle = new D3D12ConstantBufferHandle(); handle->m_pool = this; handle->m_native = d3d12::AllocConstantBuffer(m_heap, buffer_size); if (handle->m_native == nullptr) { delete handle; return nullptr; } m_constant_buffer_handles.push_back(handle); return handle; } void D3D12ConstantBufferPool::WriteConstantBufferData(ConstantBufferHandle * handle, size_t size, size_t offset, std::uint8_t * data) { auto frame_index = m_render_system.GetFrameIdx(); std::uint8_t* cpu_address = static_cast(handle)->m_native->m_cpu_addresses->operator[](frame_index); memcpy(cpu_address + offset, data, size); } void D3D12ConstantBufferPool::WriteConstantBufferData(ConstantBufferHandle * handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t * data) { auto frame_index = frame_idx; std::uint8_t* cpu_address = static_cast(handle)->m_native->m_cpu_addresses->operator[](frame_index); memcpy(cpu_address + offset, data, size); } void D3D12ConstantBufferPool::DeallocateConstantBuffer(ConstantBufferHandle* handle) { d3d12::DeallocConstantBuffer(m_heap, static_cast(handle)->m_native); std::vector::iterator it; for (it = m_constant_buffer_handles.begin(); it != m_constant_buffer_handles.end(); ++it) { if ((*it) == handle) { break; } } if (it != m_constant_buffer_handles.end()) { m_constant_buffer_handles.erase(it); delete handle; } } } /* wr */ ================================================ FILE: src/d3d12/d3d12_constant_buffer_pool.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../constant_buffer_pool.hpp" #include "d3d12_structs.hpp" namespace wr { struct D3D12ConstantBufferHandle : ConstantBufferHandle { d3d12::HeapResource* m_native; }; class D3D12RenderSystem; class D3D12ConstantBufferPool : public ConstantBufferPool { public: explicit D3D12ConstantBufferPool(D3D12RenderSystem& render_system, std::size_t size_in_bytes); ~D3D12ConstantBufferPool() final; void Evict() final; void MakeResident() final; protected: ConstantBufferHandle* AllocateConstantBuffer(std::size_t buffer_size) final; void WriteConstantBufferData(ConstantBufferHandle* handle, size_t size, size_t offset, std::uint8_t* data) final; void WriteConstantBufferData(ConstantBufferHandle* handle, size_t size, size_t offset, size_t frame_idx, std::uint8_t* data) final; void DeallocateConstantBuffer(ConstantBufferHandle* handle) final; std::vector m_constant_buffer_handles; d3d12::Heap* m_heap; D3D12RenderSystem& m_render_system; }; } /* wr */ ================================================ FILE: src/d3d12/d3d12_defines.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../util/log.hpp" #pragma warning(push, 0) #define D3DX12_INC d3dx12_rt.h /*Helper function to get readable error messages from HResults code originated from https://docs.microsoft.com/en-us/windows/desktop/cossdk/interpreting-error-codes */ inline std::string HResultToString(HRESULT hr) { if (FACILITY_WINDOWS == HRESULT_FACILITY(hr)) { hr = HRESULT_CODE(hr); } TCHAR* sz_err_msg; if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&sz_err_msg, 0, NULL) != 0) { std::string retval = sz_err_msg; LocalFree(sz_err_msg); return(retval); } else { return(std::string("[Could not find a description for error # %#x.", hr)); } } //! Checks whether the d3d12 object exists before releasing it. #define SAFE_RELEASE(obj) { if ( obj ) { obj->Release(); obj = NULL; } } //! Handles a hresult. #define TRY(result) if (FAILED(result)) { LOGC("An hresult returned a error!. File: " + std::string(__FILE__) + " Line: " + std::to_string(__LINE__) + " HRResult: " + HResultToString(result)); } //! Handles a hresult and outputs a specific message. #define TRY_M(result, msg) if (FAILED(result)) { LOGC(static_cast(msg) + " HRResult: " + HResultToString(result)); } //! This macro is used to name d3d12 resources. #define NAME_D3D12RESOURCE(r, n) { auto temp = std::string(__FILE__); \ r->SetName(std::wstring(std::wstring(n) + L" (line: " + std::to_wstring(__LINE__) + L" file: " + std::wstring(temp.begin(), temp.end())).c_str()); } //! This macro is used to name d3d12 resources with a placeholder name. TODO: "Unamed Resource" should be "Unamed [typename]" #define NAME_D3D12RESOURCE(r) { auto temp = std::string(__FILE__); \ r->SetName(std::wstring(L"Unnamed Resource (line: " + std::to_wstring(__LINE__) + L" file: " + std::wstring(temp.begin(), temp.end())).c_str()); } // Particular version automatically rounds the alignment to a two power. template constexpr inline T SizeAlignTwoPower(T size, A alignment) { return (size + (alignment - 1U)) & ~(alignment - 1U); } // Particular version always aligns to the provided alignment template constexpr inline T SizeAlignAnyAlignment(T size, A alignment) { return (size / alignment + (size%alignment > 0))*alignment; } #pragma warning(pop) ================================================ FILE: src/d3d12/d3d12_descriptor_heap.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "../util/log.hpp" #include "d3d12_defines.hpp" namespace wr::d3d12 { DescriptorHeap* CreateDescriptorHeap(Device* device, desc::DescriptorHeapDesc const & descriptor) { auto heap = new DescriptorHeap(); const auto n_device = device->m_native; heap->m_create_info = descriptor; heap->m_increment_size = n_device->GetDescriptorHandleIncrementSize(static_cast(descriptor.m_type)); heap->m_native.resize(descriptor.m_versions); for (uint32_t i = 0; i < descriptor.m_versions; ++i) { D3D12_DESCRIPTOR_HEAP_DESC heap_desc; heap_desc.NumDescriptors = descriptor.m_num_descriptors; heap_desc.Type = (D3D12_DESCRIPTOR_HEAP_TYPE)descriptor.m_type; heap_desc.Flags = descriptor.m_shader_visible ? D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE : D3D12_DESCRIPTOR_HEAP_FLAG_NONE; heap_desc.NodeMask = 0; TRY_M(n_device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap->m_native[i])), "Couldn't create descriptor heap"); } return heap; } DescHeapGPUHandle GetGPUHandle(DescriptorHeap* desc_heap, unsigned int frame_idx, unsigned int index) { DescHeapGPUHandle retval; retval.m_native = desc_heap->m_native[frame_idx % desc_heap->m_create_info.m_versions]->GetGPUDescriptorHandleForHeapStart(); if (index > 0) { Offset(retval, index, desc_heap->m_increment_size); } return retval; } DescHeapCPUHandle GetCPUHandle(DescriptorHeap* desc_heap, unsigned int frame_idx, unsigned int index) { DescHeapCPUHandle retval; retval.m_native = desc_heap->m_native[frame_idx % desc_heap->m_create_info.m_versions]->GetCPUDescriptorHandleForHeapStart(); if (index > 0) { Offset(retval, index, desc_heap->m_increment_size); } return retval; } void SetName(DescriptorHeap * desc_heap, std::wstring name) { for (int i = 0; i < desc_heap->m_native.size(); i++) { desc_heap->m_native[i]->SetName((name + L" Descriptor Version " + std::to_wstring(i)).c_str()); } } void Offset(DescHeapGPUHandle& handle, unsigned int index, unsigned int increment_size) { handle.m_native.ptr += static_cast(index) * static_cast(increment_size); } void Offset(DescHeapCPUHandle& handle, unsigned int index, unsigned int increment_size) { handle.m_native.ptr += static_cast(index) * static_cast(increment_size); } void Destroy(DescriptorHeap* desc_heap) { for (auto desc : desc_heap->m_native) { SAFE_RELEASE(desc); } delete desc_heap; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_descriptors_allocations.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_descriptors_allocations.hpp" #include "d3d12_renderer.hpp" #include "d3d12_functions.hpp" #include "../util/log.hpp" // DESCRIPTOR ALLOCATION FUNCTIONS namespace wr { DescriptorAllocation::DescriptorAllocation() : m_descriptor{ 0 } , m_num_handles(0) , m_descriptor_size(0) , m_page(nullptr) {} DescriptorAllocation::DescriptorAllocation(d3d12::DescHeapCPUHandle descriptor, uint32_t num_handles, uint32_t descriptor_size, std::shared_ptr page) : m_descriptor(descriptor) , m_num_handles(num_handles) , m_descriptor_size(descriptor_size) , m_page(page) {} DescriptorAllocation::~DescriptorAllocation() { Free(); } DescriptorAllocation::DescriptorAllocation(DescriptorAllocation&& allocation) : m_descriptor(allocation.m_descriptor) , m_num_handles(allocation.m_num_handles) , m_descriptor_size(allocation.m_descriptor_size) , m_page(std::move(allocation.m_page)) { allocation.m_descriptor.m_native.ptr = 0; allocation.m_num_handles = 0; allocation.m_descriptor_size = 0; } DescriptorAllocation& DescriptorAllocation::operator=(DescriptorAllocation&& other) { // Free this descriptor if it points to anything. Free(); m_descriptor = other.m_descriptor; m_num_handles = other.m_num_handles; m_descriptor_size = other.m_descriptor_size; m_page = std::move(other.m_page); other.m_descriptor.m_native.ptr = 0; other.m_num_handles = 0; other.m_descriptor_size = 0; return *this; } void DescriptorAllocation::Free() { if (!IsNull() && m_page) { m_page->Free(std::move(*this)); m_descriptor.m_native.ptr = 0; m_num_handles = 0; m_descriptor_size = 0; m_page.reset(); } } // Check if this a valid descriptor. bool DescriptorAllocation::IsNull() const { return m_descriptor.m_native.ptr == 0; } // Get a descriptor at a particular offset in the allocation. d3d12::DescHeapCPUHandle DescriptorAllocation::GetDescriptorHandle(uint32_t offset) const { if (offset > m_num_handles) { LOGC("Specified offset is greater than the number of handles in this Descriptor Allocation"); } return { m_descriptor.m_native.ptr + (m_descriptor_size * offset) }; } uint32_t DescriptorAllocation::GetNumHandles() const { return m_num_handles; } std::shared_ptr DescriptorAllocation::GetDescriptorAllocatorPage() const { return m_page; } } // DESCRIPTOR ALLOCATOR PAGE FUNCTIONS namespace wr { DescriptorAllocatorPage::DescriptorAllocatorPage(D3D12RenderSystem& render_system, DescriptorHeapType type, uint32_t num_descriptors) : m_render_system(render_system) , m_heap_type(type) , m_num_descriptors_in_heap(num_descriptors) { d3d12::desc::DescriptorHeapDesc desc; desc.m_type = m_heap_type; desc.m_num_descriptors = m_num_descriptors_in_heap; desc.m_versions = 1; desc.m_shader_visible = false; m_descriptor_heap = d3d12::CreateDescriptorHeap(m_render_system.m_device, desc); m_base_descriptor = d3d12::GetCPUHandle(m_descriptor_heap, 0); m_num_free_handles = m_num_descriptors_in_heap; // Initialize the free lists AddNewBlock(0, m_num_free_handles); } DescriptorAllocatorPage::~DescriptorAllocatorPage() { d3d12::Destroy(m_descriptor_heap); m_num_free_handles = 0; m_num_descriptors_in_heap = 0; m_freelist_by_offset.clear(); m_freelist_by_size.clear(); } DescriptorHeapType DescriptorAllocatorPage::GetHeapType() const { return m_heap_type; } uint32_t DescriptorAllocatorPage::NumFreeHandles() const { return m_num_free_handles; } bool DescriptorAllocatorPage::HasSpace(uint32_t num_descriptors) const { return m_freelist_by_size.lower_bound(num_descriptors) != m_freelist_by_size.end(); } void DescriptorAllocatorPage::AddNewBlock(uint32_t offset, uint32_t num_descriptors) { auto offsetIt = m_freelist_by_offset.emplace(offset, num_descriptors); auto sizeIt = m_freelist_by_size.emplace(num_descriptors, offsetIt.first); offsetIt.first->second.m_freelist_by_size_itr = sizeIt; } DescriptorAllocation DescriptorAllocatorPage::Allocate(uint32_t num_descriptors) { std::lock_guard lock(m_allocation_mutex); // There are less than the requested number of descriptors left in the heap. // Return a NULL descriptor and try another heap. if (num_descriptors > m_num_free_handles) { return DescriptorAllocation(); } // Get the first block that is large enough to satisfy the request. auto smallestBlockIt = m_freelist_by_size.lower_bound(num_descriptors); if (smallestBlockIt == m_freelist_by_size.end()) { // There was no free block that could satisfy the request. return DescriptorAllocation(); } // The size of the smallest block that satisfies the request. auto blockSize = smallestBlockIt->first; // The pointer to the same entry in the FreeListByOffset map. auto offsetIt = smallestBlockIt->second; // The offset in the descriptor heap. auto offset = offsetIt->first; // Remove the existing free block from the free list. m_freelist_by_size.erase(smallestBlockIt); m_freelist_by_offset.erase(offsetIt); // Compute the new free block that results from splitting this block. auto newOffset = offset + num_descriptors; auto newSize = blockSize - num_descriptors; if (newSize > 0) { // If the allocation didn't exactly match the requested size, // return the left-over to the free list. AddNewBlock(newOffset, newSize); } // Decrement free handles. m_num_free_handles -= num_descriptors; d3d12::DescHeapCPUHandle new_handle; new_handle.m_native = CD3DX12_CPU_DESCRIPTOR_HANDLE(m_base_descriptor.m_native, offset, m_descriptor_heap->m_increment_size); return DescriptorAllocation(new_handle, num_descriptors, m_descriptor_heap->m_increment_size, shared_from_this()); } uint32_t DescriptorAllocatorPage::ComputeOffset(d3d12::DescHeapCPUHandle handle) { return static_cast(handle.m_native.ptr - m_base_descriptor.m_native.ptr) / m_descriptor_heap->m_increment_size; } void DescriptorAllocatorPage::Free(DescriptorAllocation&& allocation_handle) { // Compute the offset of the descriptor within the descriptor heap. auto offset = ComputeOffset(allocation_handle.GetDescriptorHandle()); std::lock_guard lock(m_allocation_mutex); // Don't add the block directly to the free list until the frame has completed. m_stale_descriptors.emplace(offset, allocation_handle.GetNumHandles(), m_render_system.GetFrameIdx()); } void DescriptorAllocatorPage::FreeBlock(uint32_t offset, uint32_t num_descriptors) { // Find the first element whose offset is greater than the specified offset. // This is the block that should appear after the block that is being freed. auto next_block_it = m_freelist_by_offset.upper_bound(offset); // Find the block that appears before the block being freed. auto prev_block_it = next_block_it; // If it's not the first block in the list. if (prev_block_it != m_freelist_by_offset.begin()) { // Go to the previous block in the list. --prev_block_it; } else { // Otherwise, just set it to the end of the list to indicate that no // block comes before the one being freed. prev_block_it = m_freelist_by_offset.end(); } // Add the number of free handles back to the heap. // This needs to be done before merging any blocks since merging // blocks modifies the numDescriptors variable. m_num_free_handles += num_descriptors; if (prev_block_it != m_freelist_by_offset.end() && offset == prev_block_it->first + prev_block_it->second.m_size) { // The previous block is exactly behind the block that is to be freed. // // PrevBlock.Offset Offset // | | // |<-----PrevBlock.Size----->|<------Size-------->| // // Increase the block size by the size of merging with the previous block. offset = prev_block_it->first; num_descriptors += prev_block_it->second.m_size; // Remove the previous block from the free list. m_freelist_by_size.erase(prev_block_it->second.m_freelist_by_size_itr); m_freelist_by_offset.erase(prev_block_it); } if (next_block_it != m_freelist_by_offset.end() && offset + num_descriptors == next_block_it->first) { // The next block is exactly in front of the block that is to be freed. // // Offset NextBlock.Offset // | | // |<------Size-------->|<-----NextBlock.Size----->| // Increase the block size by the size of merging with the next block. num_descriptors += next_block_it->second.m_size; // Remove the next block from the free list. m_freelist_by_size.erase(next_block_it->second.m_freelist_by_size_itr); m_freelist_by_offset.erase(next_block_it); } // Add the freed block to the free list. AddNewBlock(offset, num_descriptors); } void DescriptorAllocatorPage::ReleaseStaleDescriptors() { std::lock_guard lock(m_allocation_mutex); while (!m_stale_descriptors.empty() && m_stale_descriptors.front().m_frame_number <= m_render_system.GetFrameIdx()) { auto& staleDescriptor = m_stale_descriptors.front(); // The offset of the descriptor in the heap. auto offset = staleDescriptor.m_offset; // The number of descriptors that were allocated. auto numDescriptors = staleDescriptor.m_size; FreeBlock(offset, numDescriptors); m_stale_descriptors.pop(); } } } // DESCRIPTOR ALLOCATOR FUNCTIONS namespace wr { DescriptorAllocator::DescriptorAllocator(D3D12RenderSystem& render_system, DescriptorHeapType type, uint32_t num_descriptors_per_heap) : m_render_system(render_system) , m_heap_type(type) , m_num_descriptors_per_heap(num_descriptors_per_heap) { } DescriptorAllocator::~DescriptorAllocator() { ReleaseStaleDescriptors(); for (auto heap : m_heap_pool) { heap.reset(); } } std::shared_ptr DescriptorAllocator::CreateAllocatorPage() { auto new_page = std::make_shared(m_render_system, m_heap_type, m_num_descriptors_per_heap); m_heap_pool.emplace_back(new_page); m_available_heaps.insert(m_heap_pool.size() - 1); return new_page; } DescriptorAllocation DescriptorAllocator::Allocate(uint32_t num_dscriptors) { std::lock_guard lock(m_allocation_mutex); DescriptorAllocation allocation; for (auto iter = m_available_heaps.begin(); iter != m_available_heaps.end(); ++iter) { auto allocator_page = m_heap_pool[*iter]; allocation = allocator_page->Allocate(num_dscriptors); if (allocator_page->NumFreeHandles() == 0) { iter = m_available_heaps.erase(iter); } // A valid allocation has been found. if (!allocation.IsNull()) { break; } } // No available heap could satisfy the requested number of descriptors. if (allocation.IsNull()) { m_num_descriptors_per_heap = std::max(m_num_descriptors_per_heap, num_dscriptors); auto new_page = CreateAllocatorPage(); allocation = new_page->Allocate(num_dscriptors); } return allocation; } void DescriptorAllocator::ReleaseStaleDescriptors() { std::lock_guard lock(m_allocation_mutex); for (size_t i = 0; i < m_heap_pool.size(); ++i) { auto page = m_heap_pool[i]; page->ReleaseStaleDescriptors(); if (page->NumFreeHandles() > 0) { m_available_heaps.insert(i); } } } } ================================================ FILE: src/d3d12/d3d12_descriptors_allocations.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "d3d12_structs.hpp" #include "d3d12_enums.hpp" #include #include #include #include #include #include #include namespace wr { //Forward declaration of a page, the DescriptorAllocation class needs it. class DescriptorAllocatorPage; //Forward declaration of the render system needed by the page class D3D12RenderSystem; /////////////////////////////// // DESCRIPTOR ALLOCATION // /////////////////////////////// //Represent a single allocation of contiguous descriptors in a descriptor heap. class DescriptorAllocation { public: // Creates a NULL descriptor. DescriptorAllocation(); DescriptorAllocation(d3d12::DescHeapCPUHandle descriptor, uint32_t num_handles, uint32_t descriptor_size, std::shared_ptr page); // The destructor will automatically free the allocation. ~DescriptorAllocation(); // Only move is allowed. DescriptorAllocation(const DescriptorAllocation&) = delete; DescriptorAllocation& operator=(const DescriptorAllocation&) = delete; DescriptorAllocation(DescriptorAllocation&& allocation); DescriptorAllocation& operator=(DescriptorAllocation&& other); // Check if this a valid descriptor. bool IsNull() const; // Get a descriptor at a particular offset in the allocation. d3d12::DescHeapCPUHandle GetDescriptorHandle(uint32_t offset = 0) const; // Get the number of (consecutive) handles for this allocation. uint32_t GetNumHandles() const; // Get the heap that this allocation came from. // (For internal use only). std::shared_ptr GetDescriptorAllocatorPage() const; private: // Free the descriptor back to the heap it came from. void Free(); // The base descriptor. d3d12::DescHeapCPUHandle m_descriptor; // The number of descriptors in this allocation. uint32_t m_num_handles; // The offset to the next descriptor. uint32_t m_descriptor_size; // A pointer back to the original page where this allocation came from. std::shared_ptr m_page; }; /////////////////////////////// // DESCRIPTOR ALLOCATOR PAGE // /////////////////////////////// class DescriptorAllocatorPage : public std::enable_shared_from_this { public: DescriptorAllocatorPage(D3D12RenderSystem& render_system, DescriptorHeapType type, uint32_t num_descriptors); ~DescriptorAllocatorPage(); DescriptorHeapType GetHeapType() const; /** * Check to see if this descriptor page has a contiguous block of descriptors * large enough to satisfy the request. */ bool HasSpace(uint32_t num_descriptors) const; /** * Get the number of available handles in the heap. */ uint32_t NumFreeHandles() const; /** * Allocate a number of descriptors from this descriptor heap. * If the allocation cannot be satisfied, then a NULL descriptor * is returned. */ DescriptorAllocation Allocate(uint32_t num_descriptors); /** * Return a descriptor back to the heap. * Stale descriptors are not freed directly, but put * on a stale allocations queue. Stale allocations are returned to the heap * using the DescriptorAllocatorPage::ReleaseStaleAllocations method. */ void Free(DescriptorAllocation&& allocation_handle); /** * Returned the stale descriptors back to the descriptor heap. */ void ReleaseStaleDescriptors(); protected: // Compute the offset of the descriptor handle from the start of the heap. uint32_t ComputeOffset(d3d12::DescHeapCPUHandle handle); // Adds a new block to the free list. void AddNewBlock(uint32_t offset, uint32_t num_descriptors); // Free a block of descriptors. // This will also merge free blocks in the free list to form larger blocks // that can be reused. void FreeBlock(uint32_t offset, uint32_t num_descriptors); private: // The offset (in descriptors) within the descriptor heap. using OffsetType = uint32_t; // The number of descriptors that are available. using SizeType = uint32_t; struct FreeBlockInfo; // A map that lists the free blocks by the offset within the descriptor heap. using FreeListByOffset = std::map; // A map that lists the free blocks by size. // Needs to be a multimap since multiple blocks can have the same size. using FreeListBySize = std::multimap; struct FreeBlockInfo { FreeBlockInfo(SizeType size) : m_size(size) {} SizeType m_size; FreeListBySize::iterator m_freelist_by_size_itr; }; struct StaleDescriptorInfo { StaleDescriptorInfo(OffsetType offset, SizeType size, uint64_t frame) : m_offset(offset) , m_size(size) , m_frame_number(frame) {} // The offset within the descriptor heap. OffsetType m_offset; // The number of descriptors SizeType m_size; // The frame number that the descriptor was freed. uint64_t m_frame_number; }; // Stale descriptors are queued for release until the frame that they were freed // has completed. using StaleDescriptorQueue = std::queue; FreeListByOffset m_freelist_by_offset; FreeListBySize m_freelist_by_size; StaleDescriptorQueue m_stale_descriptors; d3d12::DescriptorHeap* m_descriptor_heap; DescriptorHeapType m_heap_type; d3d12::DescHeapCPUHandle m_base_descriptor; uint32_t m_num_descriptors_in_heap; uint32_t m_num_free_handles; std::mutex m_allocation_mutex; D3D12RenderSystem& m_render_system; }; } ////////////////////////// // DESCRIPTOR ALLOCATOR // ////////////////////////// namespace wr { class DescriptorAllocator { public: DescriptorAllocator(D3D12RenderSystem& render_system, DescriptorHeapType type, uint32_t num_descriptors_per_heap = 256); virtual ~DescriptorAllocator(); /** * Allocate a number of contiguous descriptors from a CPU visible descriptor heap. * * @param numDescriptors The number of contiguous descriptors to allocate. * Cannot be more than the number of descriptors per descriptor heap. */ DescriptorAllocation Allocate(uint32_t num_descriptors = 1); /** * When the frame has completed, the stale descriptors can be released. */ void ReleaseStaleDescriptors(); private: using DescriptorHeapPool = std::vector>; // Create a new heap with a specific number of descriptors. std::shared_ptr CreateAllocatorPage(); DescriptorHeapType m_heap_type; uint32_t m_num_descriptors_per_heap; DescriptorHeapPool m_heap_pool; // Indices of available heaps in the heap pool. std::set m_available_heaps; std::mutex m_allocation_mutex; D3D12RenderSystem& m_render_system; }; } ================================================ FILE: src/d3d12/d3d12_device.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include #include #include #include "../util/log.hpp" #include "d3d12_defines.hpp" namespace wr::d3d12 { IDxcCompiler2* Device::m_compiler = nullptr; namespace internal { void EnableDebugLayer(Device* device) { if (settings::enable_debug_layer != settings::DebugLayer::DISABLE) // If the debug layer isn't disabled { // Setup debug layers ID3D12Debug* temp_debug_controller; if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&temp_debug_controller))) && SUCCEEDED(temp_debug_controller->QueryInterface(IID_PPV_ARGS(&device->m_debug_controller)))) { if (settings::enable_debug_layer == settings::DebugLayer::ENABLE_WITH_GPU_VALIDATION) // If GPU validation is requested. { device->m_debug_controller->SetEnableSynchronizedCommandQueueValidation(true); device->m_debug_controller->SetEnableGPUBasedValidation(true); } device->m_debug_controller->EnableDebugLayer(); } } } void EnableGpuErrorBreaking(Device* device) { #ifdef _DEBUG // Set error behaviour if (SUCCEEDED(device->m_native->QueryInterface(IID_PPV_ARGS(&device->m_info_queue)))) { device->m_info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, !device->m_dxr_support); device->m_info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, !device->m_dxr_support); } #endif } void GetSysInfo(Device* device) { GetNativeSystemInfo(&device->m_sys_info); device->m_adapter->GetDesc3(&device->m_adapter_info); } void CreateFactory(Device* device) { TRY_M(CreateDXGIFactory2(settings::enable_debug_factory ? DXGI_CREATE_FACTORY_DEBUG : 0, IID_PPV_ARGS(&device->m_dxgi_factory)), "Failed to create DXGIFactory."); } void FindAdapter(Device* device) { IDXGIAdapter1* adapter = nullptr; int adapter_idx = 0; // Find a compatible adapter. //while (device->m_dxgi_factory->EnumAdapterByGpuPreference(adapter_idx, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)) != DXGI_ERROR_NOT_FOUND) while ((device->m_dxgi_factory)->EnumAdapters1(adapter_idx, &adapter) != DXGI_ERROR_NOT_FOUND) { DXGI_ADAPTER_DESC1 desc; adapter->GetDesc1(&desc); // Skip software adapters. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) { adapter_idx++; continue; } std::function recursive_dc = [&](int i) -> bool { // Create a device to test if the adapter supports the specified feature level. HRESULT hr = D3D12CreateDevice(adapter, settings::possible_feature_levels[i], _uuidof(ID3D12Device), nullptr); if (SUCCEEDED(hr)) { device->m_feature_level = settings::possible_feature_levels[i]; return true; } if (i + 1 >= settings::possible_feature_levels.size()) { return false; } else { i++; recursive_dc(i); } return true; }; if (recursive_dc(0)) { break; } adapter_idx++; } if (adapter == nullptr) { device->m_dxgi_factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter)); LOGW("Using Warp Adapter!"); device->m_feature_level = settings::possible_feature_levels[0]; TRY_M(D3D12CreateDevice(adapter, settings::possible_feature_levels[0], _uuidof(ID3D12Device), nullptr), "Failed to create warp adapter."); } if (adapter == nullptr) { LOGC("Failed to find hardware adapter or create warp adapter."); } device->m_adapter = (IDXGIAdapter4*)adapter; } void QueryForOptionalFormats(Device* device) { D3D12_FEATURE_DATA_D3D12_OPTIONS feature_data; ZeroMemory(&feature_data, sizeof(feature_data)); HRESULT hr = device->m_native->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &feature_data, sizeof(feature_data)); if (SUCCEEDED(hr)) { // TypedUAVLoadAdditionalFormats contains a Boolean that tells you whether the feature is supported or not if (feature_data.TypedUAVLoadAdditionalFormats) { // Can assume all-or-nothing subset is supported (e.g. R32G32B32A32_FLOAT) // Cannot assume other formats are supported, so we check: auto CheckAndSetFormat = [](Device* device, DXGI_FORMAT format) { D3D12_FEATURE_DATA_FORMAT_SUPPORT format_support = { format, D3D12_FORMAT_SUPPORT1_NONE, D3D12_FORMAT_SUPPORT2_NONE }; HRESULT hr = device->m_native->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &format_support, sizeof(format_support)); if (SUCCEEDED(hr) && (format_support.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD) != 0) { device->m_optional_formats.set(format); } }; CheckAndSetFormat(device, DXGI_FORMAT_R16G16B16A16_UNORM); CheckAndSetFormat(device, DXGI_FORMAT_R16G16B16A16_SNORM); CheckAndSetFormat(device, DXGI_FORMAT_R32G32_FLOAT); CheckAndSetFormat(device, DXGI_FORMAT_R32G32_UINT); CheckAndSetFormat(device, DXGI_FORMAT_R32G32_SINT); CheckAndSetFormat(device, DXGI_FORMAT_R10G10B10A2_UNORM); CheckAndSetFormat(device, DXGI_FORMAT_R10G10B10A2_UINT); CheckAndSetFormat(device, DXGI_FORMAT_R11G11B10_FLOAT); CheckAndSetFormat(device, DXGI_FORMAT_R8G8B8A8_SNORM); CheckAndSetFormat(device, DXGI_FORMAT_R16G16_FLOAT); CheckAndSetFormat(device, DXGI_FORMAT_R16G16_UNORM); CheckAndSetFormat(device, DXGI_FORMAT_R16G16_UINT); CheckAndSetFormat(device, DXGI_FORMAT_R16G16_SNORM); CheckAndSetFormat(device, DXGI_FORMAT_R16G16_SINT); CheckAndSetFormat(device, DXGI_FORMAT_R8G8_UNORM); CheckAndSetFormat(device, DXGI_FORMAT_R8G8_UINT); CheckAndSetFormat(device, DXGI_FORMAT_R8G8_SNORM); CheckAndSetFormat(device, DXGI_FORMAT_R8G8_SINT); CheckAndSetFormat(device, DXGI_FORMAT_R16_UNORM); CheckAndSetFormat(device, DXGI_FORMAT_R16_SNORM); CheckAndSetFormat(device, DXGI_FORMAT_R8_SNORM); CheckAndSetFormat(device, DXGI_FORMAT_A8_UNORM); CheckAndSetFormat(device, DXGI_FORMAT_B5G6R5_UNORM); CheckAndSetFormat(device, DXGI_FORMAT_B5G5R5A1_UNORM); CheckAndSetFormat(device, DXGI_FORMAT_B4G4R4A4_UNORM); } } } // Returns bool whether the device supports DirectX Raytracing tier. inline bool IsDXRSupported(IDXGIAdapter1* adapter) { ID3D12Device* test_device; D3D12_FEATURE_DATA_D3D12_OPTIONS5 feature_support_data = {}; auto retval = SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&test_device))) && SUCCEEDED(test_device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5, &feature_support_data, sizeof(feature_support_data))) && feature_support_data.RaytracingTier != D3D12_RAYTRACING_TIER_NOT_SUPPORTED; SAFE_RELEASE(test_device); return retval; } // Enable experimental features required for compute-based raytracing fallback. // This will set active D3D12 devices to DEVICE_REMOVED state. // Returns bool whether the call succeeded and the device supports the feature. inline bool IsDXRFallbackSupported(IDXGIAdapter1* adapter) { ID3D12Device* test_device; UUID experimentalFeatures[] = { D3D12ExperimentalShaderModels }; auto retval = SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&test_device))); SAFE_RELEASE(test_device); return retval; } inline void EnableDXRFallback() { UUID experimental_features[] = { D3D12ExperimentalShaderModels }; TRY_M(D3D12EnableExperimentalFeatures(1, experimental_features, nullptr, nullptr), "Failed to enable experimantal dxr fallback features."); } inline void SetRaytracingType(Device* device) { if (d3d12::settings::disable_rtx) { device->m_rt_type = RaytracingType::NONE; } else if (device->m_dxr_support && !d3d12::settings::force_dxr_fallback) { device->m_rt_type = RaytracingType::NATIVE; } else if (device->m_dxr_fallback_support) { device->m_rt_type = RaytracingType::FALLBACK; } else { device->m_rt_type = RaytracingType::NONE; } } } Device* CreateDevice() { auto device = new Device(); internal::EnableDebugLayer(device); internal::CreateFactory(device); internal::FindAdapter(device); device->m_dxr_support = internal::IsDXRSupported(device->m_adapter); device->m_dxr_fallback_support = internal::IsDXRFallbackSupported(device->m_adapter); internal::SetRaytracingType(device); if (!device->m_dxr_support) { LOGW( "No DXR support detected.\n" "Possible Reasons:\n" "\t 1) Wrong SDK version. (Required: `Windows 10 October 2018 Update SDK (17763)`)\n" "\t 2) Wrong OS version. (Required: 1809 (17763.107))\n" "\t 3) DX12 GPU with a incompatible DirectX Raytracing driver. (NVIDIA: driver version 415 or higher, AMD: Consult Vendor for availability)" ); } if (!device->m_dxr_fallback_support) { LOGW( "No DXR Fallback support detected.\n" "Possible Reasons:\n" "GPU without feature level 11.1 or Resource Binding Tier 3." ); } if (GetRaytracingType(device) == RaytracingType::FALLBACK) { LOGW("Enabling DXR Fallback."); //internal::EnableDXRFallback(); } TRY_M(D3D12CreateDevice(device->m_adapter, device->m_feature_level, IID_PPV_ARGS(&device->m_native)), "Failed to create D3D12Device."); if (GetRaytracingType(device) == RaytracingType::FALLBACK) { auto fallback_device_flags = d3d12::settings::force_dxr_fallback ? CreateRaytracingFallbackDeviceFlags::ForceComputeFallback : CreateRaytracingFallbackDeviceFlags::None; TRY_M(D3D12CreateRaytracingFallbackDevice(device->m_native, fallback_device_flags, 0, IID_PPV_ARGS(&device->m_fallback_native)), "Failed to create fallback layer."); } // Create shader compiler if (!Device::m_compiler) { DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&Device::m_compiler)); } internal::EnableGpuErrorBreaking(device); internal::GetSysInfo(device); std::wstring g = device->m_adapter_info.Description; LOG("{}", std::string(g.begin(), g.end())); internal::QueryForOptionalFormats(device); return device; } RaytracingType GetRaytracingType(Device* device) { return device->m_rt_type; } void Destroy(Device* device) { SAFE_RELEASE(device->m_adapter); SAFE_RELEASE(device->m_native); SAFE_RELEASE(device->m_dxgi_factory); SAFE_RELEASE(device->m_debug_controller); SAFE_RELEASE(device->m_info_queue); SAFE_RELEASE(device->m_fallback_native); SAFE_RELEASE(device->m_compiler); delete device; } void SetName(Device * device, std::wstring name) { device->m_native->SetName(name.c_str()); } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_dynamic_descriptor_heap.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_dynamic_descriptor_heap.hpp" #include "d3d12_renderer.hpp" #include "d3d12_functions.hpp" #include "../util/log.hpp" namespace wr { DynamicDescriptorHeap::DynamicDescriptorHeap(wr::d3d12::Device* device, DescriptorHeapType type, uint32_t num_descriptors_per_heap) : m_desc_heap_type(type) , m_num_descr_per_heap(num_descriptors_per_heap) , m_descriptor_table_bit_mask(0) , m_stale_descriptor_table_bit_mask(0) , m_num_free_handles(0) , m_device(device) { m_current_cpu_desc_handle.m_native.ptr = 0; m_current_gpu_desc_handle.m_native.ptr = 0; m_descriptor_handle_increment_size = m_device->m_native->GetDescriptorHandleIncrementSize((D3D12_DESCRIPTOR_HEAP_TYPE)type); // Allocate space for staging CPU visible descriptors. m_descriptor_handle_cache = std::make_unique(m_num_descr_per_heap); } DynamicDescriptorHeap::~DynamicDescriptorHeap() { while(!m_descriptor_heap_pool.empty()) { delete m_descriptor_heap_pool.front(); m_descriptor_heap_pool.pop(); } } void DynamicDescriptorHeap::ParseRootSignature(const d3d12::RootSignature& root_signature) { // If the root signature changes, all descriptors must be (re)bound to the // command list. m_stale_descriptor_table_bit_mask = 0; const auto& root_signature_desc = root_signature.m_create_info; // Get a bit mask that represents the root parameter indices that match the // descriptor heap type for this dynamic descriptor heap. switch (m_desc_heap_type) { case wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV: m_descriptor_table_bit_mask = root_signature.m_descriptor_table_bit_mask; break; case wr::DescriptorHeapType::DESC_HEAP_TYPE_SAMPLER: m_descriptor_table_bit_mask = root_signature.m_sampler_table_bit_mask; break; default: //LOGC("You can't use a different type other than CBC_SRV_UAV or SAMPLER for dynamic descriptor heaps"); break; } uint32_t descriptor_table_bitmask = m_descriptor_table_bit_mask; uint32_t current_offset = 0; DWORD root_idx; size_t num_parameters = root_signature_desc.m_parameters.size(); while (_BitScanForward(&root_idx, descriptor_table_bitmask) && root_idx < num_parameters) { //Since a 32 bit mask is used to represent the descriptor tables in the root signature //I want to break if the root_idx is bigger than 32. If that ever happens, we might consider //switching to a 64bit mask. if (root_idx > 32) { LOGC("A maximum of 32 descriptor tables are supported"); } uint32_t num_descriptors = root_signature.m_num_descriptors_per_table[root_idx]; DescriptorTableCache& descriptorTableCache = m_descriptor_table_cache[root_idx]; descriptorTableCache.m_num_descriptors = num_descriptors; descriptorTableCache.m_base_descriptor = m_descriptor_handle_cache.get() + current_offset; current_offset += num_descriptors; // Flip the descriptor table bit so it's not scanned again for the current index. descriptor_table_bitmask ^= (1 << root_idx); } // Make sure the maximum number of descriptors per descriptor heap has not been exceeded. if (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."); } } void DynamicDescriptorHeap::StageDescriptors(uint32_t root_param_idx, uint32_t offset, uint32_t num_descriptors, const d3d12::DescHeapCPUHandle src_desc) { if (num_descriptors > m_num_descr_per_heap || root_param_idx >= MaxDescriptorTables) { LOGC("Cannot stage more than the maximum number of descriptors per heap. Cannot stage more than MaxDescriptorTables root parameters"); } DescriptorTableCache& descriptor_table_cache = m_descriptor_table_cache[root_param_idx]; // Check that the number of descriptors to copy does not exceed the number // of descriptors expected in the descriptor table. if ((offset + num_descriptors) > descriptor_table_cache.m_num_descriptors) { LOGC("Number of descriptors exceeds the number of descriptors in the descriptor table."); } D3D12_CPU_DESCRIPTOR_HANDLE* dst_descriptor = (descriptor_table_cache.m_base_descriptor + offset); for (uint32_t i = 0; i < num_descriptors; ++i) { dst_descriptor[i] = CD3DX12_CPU_DESCRIPTOR_HANDLE(src_desc.m_native, i, m_descriptor_handle_increment_size); } // Set the root parameter index bit to make sure the descriptor table // at that index is bound to the command list. m_stale_descriptor_table_bit_mask |= (1 << root_param_idx); } uint32_t DynamicDescriptorHeap::ComputeStaleDescriptorCount() const { uint32_t num_stale_desc = 0; DWORD i; DWORD stale_desc_bitmask = m_stale_descriptor_table_bit_mask; while (_BitScanForward(&i, stale_desc_bitmask)) { num_stale_desc += m_descriptor_table_cache[i].m_num_descriptors; stale_desc_bitmask ^= (1 << i); } return num_stale_desc; } d3d12::DescriptorHeap* DynamicDescriptorHeap::RequestDescriptorHeap() { d3d12::DescriptorHeap* descriptor_heap; if (!m_available_descriptor_heaps.empty()) { descriptor_heap = m_available_descriptor_heaps.front(); m_available_descriptor_heaps.pop(); } else { descriptor_heap = CreateDescriptorHeap(); m_descriptor_heap_pool.push(descriptor_heap); } return descriptor_heap; } d3d12::DescriptorHeap* DynamicDescriptorHeap::CreateDescriptorHeap() { d3d12::desc::DescriptorHeapDesc desc; desc.m_type = m_desc_heap_type; desc.m_num_descriptors = m_num_descr_per_heap; desc.m_shader_visible = true; desc.m_versions = 1; d3d12::DescriptorHeap* descriptor_heap = d3d12::CreateDescriptorHeap(m_device, desc); return descriptor_heap; } void DynamicDescriptorHeap::CommitStagedDescriptorsForDraw(d3d12::CommandList& cmd_list) { // Compute the number of descriptors that need to be copied uint32_t num_descriptors_to_commit = ComputeStaleDescriptorCount(); if (num_descriptors_to_commit > 0) { if (!m_current_descriptor_heap || m_num_free_handles < num_descriptors_to_commit) { m_current_descriptor_heap = RequestDescriptorHeap(); m_current_cpu_desc_handle = d3d12::GetCPUHandle(m_current_descriptor_heap, 0); m_current_gpu_desc_handle = d3d12::GetGPUHandle(m_current_descriptor_heap, 0); m_num_free_handles = m_num_descr_per_heap; d3d12::BindDescriptorHeap(&cmd_list, m_current_descriptor_heap, m_desc_heap_type, 0); // When updating the descriptor heap on the command list, all descriptor // tables must be (re)recopied to the new descriptor heap (not just // the stale descriptor tables). m_stale_descriptor_table_bit_mask = m_descriptor_table_bit_mask; } DWORD root_idx; // Scan from LSB to MSB for a bit set in staleDescriptorsBitMask while (_BitScanForward(&root_idx, m_stale_descriptor_table_bit_mask)) { std::uint32_t num_src_desc = m_descriptor_table_cache[root_idx].m_num_descriptors; D3D12_CPU_DESCRIPTOR_HANDLE* pSrcDescriptorHandles = m_descriptor_table_cache[root_idx].m_base_descriptor; D3D12_CPU_DESCRIPTOR_HANDLE pDestDescriptorRangeStarts[] = { m_current_cpu_desc_handle.m_native }; std::uint32_t pDestDescriptorRangeSizes[] = { num_src_desc }; // Copy the staged CPU visible descriptors to the GPU visible descriptor heap. m_device->m_native->CopyDescriptors(1, pDestDescriptorRangeStarts, pDestDescriptorRangeSizes, num_src_desc, pSrcDescriptorHandles, nullptr, static_cast(m_desc_heap_type)); // Set the descriptors on the command list using the passed-in setter function. d3d12::BindDescriptorTable(&cmd_list, m_current_gpu_desc_handle, root_idx); // Offset current CPU and GPU descriptor handles. d3d12::Offset(m_current_cpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size); d3d12::Offset(m_current_gpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size); m_num_free_handles -= num_src_desc; // Flip the stale bit so the descriptor table is not recopied again unless it is updated with a new descriptor. m_stale_descriptor_table_bit_mask ^= (1 << root_idx); } } } void DynamicDescriptorHeap::CommitStagedDescriptorsForDispatch(d3d12::CommandList& cmd_list) { // Compute the number of descriptors that need to be copied std::uint32_t num_descriptors_to_commit = ComputeStaleDescriptorCount(); if (num_descriptors_to_commit > 0) { if (!m_current_descriptor_heap || m_num_free_handles < num_descriptors_to_commit) { m_current_descriptor_heap = RequestDescriptorHeap(); m_current_cpu_desc_handle = d3d12::GetCPUHandle(m_current_descriptor_heap, 0); m_current_gpu_desc_handle = d3d12::GetGPUHandle(m_current_descriptor_heap, 0); m_num_free_handles = m_num_descr_per_heap; d3d12::BindDescriptorHeap(&cmd_list, m_current_descriptor_heap, m_desc_heap_type, 0, d3d12::GetRaytracingType(m_device) == RaytracingType::FALLBACK); // When updating the descriptor heap on the command list, all descriptor // tables must be (re)recopied to the new descriptor heap (not just // the stale descriptor tables). m_stale_descriptor_table_bit_mask = m_descriptor_table_bit_mask; } DWORD root_idx; // Scan from LSB to MSB for a bit set in staleDescriptorsBitMask while (_BitScanForward(&root_idx, m_stale_descriptor_table_bit_mask)) { std::uint32_t num_src_desc = m_descriptor_table_cache[root_idx].m_num_descriptors; D3D12_CPU_DESCRIPTOR_HANDLE* pSrcDescriptorHandles = m_descriptor_table_cache[root_idx].m_base_descriptor; D3D12_CPU_DESCRIPTOR_HANDLE pDestDescriptorRangeStarts[] = { m_current_cpu_desc_handle.m_native }; std::uint32_t pDestDescriptorRangeSizes[] = { num_src_desc }; // Copy the staged CPU visible descriptors to the GPU visible descriptor heap. m_device->m_native->CopyDescriptors(1, pDestDescriptorRangeStarts, pDestDescriptorRangeSizes, num_src_desc, pSrcDescriptorHandles, nullptr, static_cast(m_desc_heap_type)); // Set the descriptors on the command list using the passed-in setter function. d3d12::BindComputeDescriptorTable(&cmd_list, m_current_gpu_desc_handle, root_idx); // Offset current CPU and GPU descriptor handles. d3d12::Offset(m_current_cpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size); d3d12::Offset(m_current_gpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size); m_num_free_handles -= num_src_desc; // Flip the stale bit so the descriptor table is not recopied again unless it is updated with a new descriptor. m_stale_descriptor_table_bit_mask ^= (1 << root_idx); } } } d3d12::DescHeapGPUHandle DynamicDescriptorHeap::CopyDescriptor(d3d12::CommandList& cmd_list, d3d12::DescHeapCPUHandle cpu_desc) { if (!m_current_descriptor_heap || m_num_free_handles < 1) { m_current_descriptor_heap = RequestDescriptorHeap(); m_current_cpu_desc_handle = d3d12::GetCPUHandle(m_current_descriptor_heap, 0); m_current_gpu_desc_handle = d3d12::GetGPUHandle(m_current_descriptor_heap, 0); m_num_free_handles = m_num_descr_per_heap; d3d12::BindDescriptorHeap(&cmd_list, m_current_descriptor_heap, m_desc_heap_type, 0); // When updating the descriptor heap on the command list, all descriptor // tables must be (re)recopied to the new descriptor heap (not just // the stale descriptor tables). m_stale_descriptor_table_bit_mask = m_descriptor_table_bit_mask; } d3d12::DescHeapGPUHandle h_gpu = m_current_gpu_desc_handle; m_device->m_native->CopyDescriptorsSimple(1, m_current_cpu_desc_handle.m_native, cpu_desc.m_native, static_cast(m_desc_heap_type)); d3d12::Offset(m_current_cpu_desc_handle, 1, m_descriptor_handle_increment_size); d3d12::Offset(m_current_gpu_desc_handle, 1, m_descriptor_handle_increment_size); m_num_free_handles -= 1; return h_gpu; } void DynamicDescriptorHeap::Reset() { m_available_descriptor_heaps = m_descriptor_heap_pool; m_current_descriptor_heap = nullptr; /*if (m_current_descriptor_heap) { ID3D12DescriptorHeap* temp = m_current_descriptor_heap->m_native[0]; if (temp) { m_current_descriptor_heap->m_native[0] = nullptr; temp->Release(); } m_current_descriptor_heap = nullptr; }*/ m_current_cpu_desc_handle.m_native = CD3DX12_CPU_DESCRIPTOR_HANDLE(D3D12_DEFAULT); m_current_gpu_desc_handle.m_native = CD3DX12_GPU_DESCRIPTOR_HANDLE(D3D12_DEFAULT); m_num_free_handles = 0; m_descriptor_table_bit_mask = 0; m_stale_descriptor_table_bit_mask = 0; // Reset the table cache for (int i = 0; i < MaxDescriptorTables; ++i) { m_descriptor_table_cache[i].Reset(); } } } ================================================ FILE: src/d3d12/d3d12_dynamic_descriptor_heap.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "d3d12_structs.hpp" #include "d3d12_enums.hpp" #include #include #include namespace wr { namespace d3d12 { struct Device; }; class DynamicDescriptorHeap { public: DynamicDescriptorHeap(wr::d3d12::Device* device, DescriptorHeapType type, uint32_t num_descriptors_per_heap = 1024); virtual ~DynamicDescriptorHeap(); /** * Stages a contiguous range of CPU visible descriptors. * Descriptors are not copied to the GPU visible descriptor heap until * the CommitStagedDescriptors function is called. */ void StageDescriptors(uint32_t root_param_idx, uint32_t offset, uint32_t num_descriptors, const d3d12::DescHeapCPUHandle src_desc); /** * Copy all of the staged descriptors to the GPU visible descriptor heap and * bind the descriptor heap and the descriptor tables to the command list. * The passed-in function object is used to set the GPU visible descriptors * on the command list. Two possible functions are: * * Before a draw : ID3D12GraphicsCommandList::SetGraphicsRootDescriptorTable * * Before a dispatch: ID3D12GraphicsCommandList::SetComputeRootDescriptorTable */ void CommitStagedDescriptorsForDraw(d3d12::CommandList& cmd_list); void CommitStagedDescriptorsForDispatch(d3d12::CommandList& cmd_list); /** * Copies a single CPU visible descriptor to a GPU visible descriptor heap. * This is useful for the * * ID3D12GraphicsCommandList::ClearUnorderedAccessViewFloat * * ID3D12GraphicsCommandList::ClearUnorderedAccessViewUint * methods which require both a CPU and GPU visible descriptors for a UAV * resource. * * @param commandList The command list is required in case the GPU visible * descriptor heap needs to be updated on the command list. * @param cpuDescriptor The CPU descriptor to copy into a GPU visible * descriptor heap. * * @return The GPU visible descriptor. */ d3d12::DescHeapGPUHandle CopyDescriptor(d3d12::CommandList& cmd_list, d3d12::DescHeapCPUHandle cpu_desc); /** * Parse the root signature to determine which root parameters contain * descriptor tables and determine the number of descriptors needed for * each table. */ void ParseRootSignature(const d3d12::RootSignature& root_signature); /** * Reset used descriptors. This should only be done if any descriptors * that are being referenced by a command list has finished executing on the * command queue. */ void Reset(); protected: private: // Request a descriptor heap if one is available. d3d12::DescriptorHeap* RequestDescriptorHeap(); // Create a new descriptor heap of no descriptor heap is available. d3d12::DescriptorHeap* CreateDescriptorHeap(); // Compute the number of stale descriptors that need to be copied // to GPU visible descriptor heap. uint32_t ComputeStaleDescriptorCount() const; /** * The maximum number of descriptor tables per root signature. * A 32-bit mask is used to keep track of the root parameter indices that * are descriptor tables. */ static const uint32_t MaxDescriptorTables = 32; /** * A structure that represents a descriptor table entry in the root signature. */ struct DescriptorTableCache { DescriptorTableCache() : m_num_descriptors(0) , m_base_descriptor(nullptr) {} // Reset the table cache. void Reset() { m_num_descriptors = 0; m_base_descriptor = nullptr; } // The number of descriptors in this descriptor table. uint32_t m_num_descriptors; // The pointer to the descriptor in the descriptor handle cache. D3D12_CPU_DESCRIPTOR_HANDLE* m_base_descriptor; }; // Describes the type of descriptors that can be staged using this // dynamic descriptor heap. // Valid values are: // * D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV // * D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER // This parameter also determines the type of GPU visible descriptor heap to // create. DescriptorHeapType m_desc_heap_type; // The number of descriptors to allocate in new GPU visible descriptor heaps. uint32_t m_num_descr_per_heap; uint32_t m_descriptor_handle_increment_size; // The descriptor handle cache. std::unique_ptr m_descriptor_handle_cache; // Descriptor handle cache per descriptor table. DescriptorTableCache m_descriptor_table_cache[MaxDescriptorTables]; // Each bit in the bit mask represents the index in the root signature // that contains a descriptor table. uint32_t m_descriptor_table_bit_mask; // Each bit set in the bit mask represents a descriptor table // in the root signature that has changed since the last time the // descriptors were copied. uint32_t m_stale_descriptor_table_bit_mask; using DescriptorHeapPool = std::queue< d3d12::DescriptorHeap* >; DescriptorHeapPool m_descriptor_heap_pool; DescriptorHeapPool m_available_descriptor_heaps; d3d12::DescriptorHeap* m_current_descriptor_heap = nullptr; d3d12::DescHeapGPUHandle m_current_gpu_desc_handle; d3d12::DescHeapCPUHandle m_current_cpu_desc_handle; uint32_t m_num_free_handles; wr::d3d12::Device* m_device; }; } ================================================ FILE: src/d3d12/d3d12_enums.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include namespace wr { enum class RaytracingType { FALLBACK, NATIVE, NONE, }; enum class PipelineType { GRAPHICS_PIPELINE, COMPUTE_PIPELINE, }; enum class ShaderType { VERTEX_SHADER, PIXEL_SHADER, DOMAIN_SHADER, GEOMETRY_SHADER, HULL_SHADER, DIRECT_COMPUTE_SHADER, LIBRARY_SHADER, }; enum class CmdListType { CMD_LIST_DIRECT = (int)D3D12_COMMAND_LIST_TYPE_DIRECT, CMD_LIST_COMPUTE = (int)D3D12_COMMAND_LIST_TYPE_COMPUTE, CMD_LIST_COPY = (int)D3D12_COMMAND_LIST_TYPE_COPY, CMD_LIST_BUNDLE = (int)D3D12_COMMAND_LIST_TYPE_BUNDLE, }; enum class StateObjType { RAYTRACING_PIPELINE = (int)D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, RAYTRACING, D3D12_STATE_OBJECT_TYPE_RAYTRACING, COLLECTION, D3D12_STATE_OBJECT_TYPE_COLLECTION, }; enum HeapOptimization { SMALL_BUFFERS = 0, SMALL_STATIC_BUFFERS = 1, BIG_BUFFERS = 2, BIG_STATIC_BUFFERS = 3, }; enum class HeapType { HEAP_DEFAULT = (int)D3D12_HEAP_TYPE_DEFAULT, HEAP_READBACK = (int)D3D12_HEAP_TYPE_READBACK, HEAP_UPLOAD = (int)D3D12_HEAP_TYPE_UPLOAD, HEAP_CUSTOM = (int)D3D12_HEAP_TYPE_CUSTOM, }; enum class ResourceType { BUFFER, TEXTURE, RT_DS, }; enum class TopologyType { TRIANGLE = (int)D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, PATCH = (int)D3D12_PRIMITIVE_TOPOLOGY_TYPE_PATCH, POINT = (int)D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT, LINE = (int)D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE, }; enum class CullMode { CULL_FRONT = (int)D3D12_CULL_MODE_FRONT, CULL_BACK = (int)D3D12_CULL_MODE_BACK, CULL_NONE = (int)D3D12_CULL_MODE_NONE, }; enum class TextureFilter { FILTER_LINEAR = (int)D3D12_FILTER_MIN_MAG_MIP_LINEAR, FILTER_POINT = (int)D3D12_FILTER_MIN_MAG_MIP_POINT, FILTER_LINEAR_POINT = (int)D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT, FILTER_ANISOTROPIC = (INT)D3D12_FILTER_ANISOTROPIC, }; enum class SRVDimension { DIMENSION_BUFFER = (int)D3D12_SRV_DIMENSION_BUFFER, DIMENSION_TEXTURE1D = (int)D3D12_SRV_DIMENSION_TEXTURE1D, DIMENSION_TEXTURE1DARRAY = (int)D3D12_SRV_DIMENSION_TEXTURE1DARRAY, DIMENSION_TEXTURE2D = (int)D3D12_SRV_DIMENSION_TEXTURE2D, DIMENSION_TEXTURE2DARRAY = (int)D3D12_SRV_DIMENSION_TEXTURE2DARRAY, DIMENSION_TEXTURE2DMS = (int)D3D12_SRV_DIMENSION_TEXTURE2DMS, DIMENSION_TEXTURE2DMSARRAY = (int)D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY, DIMENSION_TEXTURE3D = (int)D3D12_SRV_DIMENSION_TEXTURE3D, DIMENSION_TEXTURECUBE = (int)D3D12_SRV_DIMENSION_TEXTURECUBE, DIMENSION_TEXTURECUBEARRAY = (int)D3D12_SRV_DIMENSION_TEXTURECUBEARRAY, }; enum class UAVDimension { DIMENSION_BUFFER = (int)D3D12_UAV_DIMENSION_BUFFER, DIMENSION_TEXTURE1D = (int)D3D12_UAV_DIMENSION_TEXTURE1D, DIMENSION_TEXTURE1DARRAY = (int)D3D12_UAV_DIMENSION_TEXTURE1DARRAY, DIMENSION_TEXTURE2D = (int)D3D12_UAV_DIMENSION_TEXTURE2D, DIMENSION_TEXTURE2DARRAY = (int)D3D12_UAV_DIMENSION_TEXTURE2DARRAY, DIMENSION_TEXTURE3D = (int)D3D12_UAV_DIMENSION_TEXTURE3D, }; enum class TextureAddressMode { TAM_MIRROR_ONCE = (int)D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE, TAM_MIRROR = (int)D3D12_TEXTURE_ADDRESS_MODE_MIRROR, TAM_CLAMP = (int)D3D12_TEXTURE_ADDRESS_MODE_CLAMP, TAM_BORDER = (int)D3D12_TEXTURE_ADDRESS_MODE_BORDER, TAM_WRAP = (int)D3D12_TEXTURE_ADDRESS_MODE_WRAP, }; enum class BorderColor { BORDER_TRANSPARENT = (int)D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK, BORDER_BLACK = (int)D3D12_STATIC_BORDER_COLOR_OPAQUE_BLACK, BORDER_WHITE = (int)D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE, }; enum class DescriptorHeapType { DESC_HEAP_TYPE_CBV_SRV_UAV = (int)D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, DESC_HEAP_TYPE_SAMPLER = (int)D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, DESC_HEAP_TYPE_RTV = (int)D3D12_DESCRIPTOR_HEAP_TYPE_RTV, DESC_HEAP_TYPE_DSV = (int)D3D12_DESCRIPTOR_HEAP_TYPE_DSV, DESC_HEAP_TYPE_NUM_TYPES = (int)D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES, }; enum class ResourceState { VERTEX_AND_CONSTANT_BUFFER = (int)D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER, INDEX_BUFFER = (int)D3D12_RESOURCE_STATE_INDEX_BUFFER, COMMON = (int)D3D12_RESOURCE_STATE_COMMON, PRESENT = (int)D3D12_RESOURCE_STATE_PRESENT, RENDER_TARGET = (int)D3D12_RESOURCE_STATE_RENDER_TARGET, PIXEL_SHADER_RESOURCE = (int)D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, NON_PIXEL_SHADER_RESOURCE = (int)D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, UNORDERED_ACCESS = (int)D3D12_RESOURCE_STATE_UNORDERED_ACCESS, COPY_SOURCE = (int)D3D12_RESOURCE_STATE_COPY_SOURCE, COPY_DEST = (int)D3D12_RESOURCE_STATE_COPY_DEST, DEPTH_WRITE = (int)D3D12_RESOURCE_STATE_DEPTH_WRITE, DEPTH_READ = (int)D3D12_RESOURCE_STATE_DEPTH_READ, INDIRECT_ARGUMENT = (int)D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, }; enum class BufferUsageFlag { INDEX_BUFFER = (int)D3D12_RESOURCE_STATE_INDEX_BUFFER, VERTEX_BUFFER = (int)D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER, }; enum class Format { UNKNOWN = (int)DXGI_FORMAT_UNKNOWN, R10G10B10A2_UNORM = (int)DXGI_FORMAT_R10G10B10A2_UNORM, R10G10B10A2_UINT = (int)DXGI_FORMAT_R10G10B10A2_UINT, R32G32B32A32_FLOAT = (int)DXGI_FORMAT_R32G32B32A32_FLOAT, R32G32B32A32_UINT = (int)DXGI_FORMAT_R32G32B32A32_UINT, R32G32B32A32_SINT = (int)DXGI_FORMAT_R32G32B32A32_SINT, R32G32B32_FLOAT = (int)DXGI_FORMAT_R32G32B32_FLOAT, R32G32B32_UINT = (int)DXGI_FORMAT_R32G32B32_UINT, R32G32B32_SINT = (int)DXGI_FORMAT_R32G32B32_SINT, R16G16B16A16_FLOAT = (int)DXGI_FORMAT_R16G16B16A16_FLOAT, R16G16B16A16_UINT = (int)DXGI_FORMAT_R16G16B16A16_UINT, R16G16B16A16_SINT = (int)DXGI_FORMAT_R16G16B16A16_SINT, R16G16B16A16_UNORM = (int)DXGI_FORMAT_R16G16B16A16_UNORM, R16G16B16A16_SNORM = (int)DXGI_FORMAT_R16G16B16A16_SNORM, R32G32_FLOAT = (int)DXGI_FORMAT_R32G32_FLOAT, R32G32_UINT = (int)DXGI_FORMAT_R32G32_UINT, R32G32_SINT = (int)DXGI_FORMAT_R32G32_SINT, //R10G10B10_UNORM = (int)DXGI_FORMAT_R10G10B10_UNORM, //R10G10B10_UINT = (int)vk::Format::eA2R10G10B10UintPack32, //FIXME: Their are more vulcan variants? R11G11B10_FLOAT = (int)DXGI_FORMAT_R11G11B10_FLOAT, R8G8B8A8_UNORM = (int)DXGI_FORMAT_R8G8B8A8_UNORM, R8G8B8A8_UNORM_SRGB = (int)DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, R8G8B8A8_SNORM = (int)DXGI_FORMAT_R8G8B8A8_SNORM, R8G8B8A8_UINT = (int)DXGI_FORMAT_R8G8B8A8_UINT, R8G8B8A8_SINT = (int)DXGI_FORMAT_R8G8B8A8_SINT, R16G16_FLOAT = (int)DXGI_FORMAT_R16G16_FLOAT, R16G16_UINT = (int)DXGI_FORMAT_R16G16_UINT, R16G16_UNORM = (int)DXGI_FORMAT_R16G16_UNORM, R16G16_SNORM = (int)DXGI_FORMAT_R16G16_SNORM, R16G16_SINT = (int)DXGI_FORMAT_R16G16_SINT, D32_FLOAT = (int)DXGI_FORMAT_D32_FLOAT, R32_UINT = (int)DXGI_FORMAT_R32_UINT, R32_TYPELESS = (int)DXGI_FORMAT_R32_TYPELESS, R32_SINT = (int)DXGI_FORMAT_R32_SINT, R32_FLOAT = (int)DXGI_FORMAT_R32_FLOAT, R16_UNORM = (int)DXGI_FORMAT_R16_UNORM, D24_UNFORM_S8_UINT = (int)DXGI_FORMAT_D24_UNORM_S8_UINT, R8G8_UNORM = (int)DXGI_FORMAT_R8G8_UNORM, R8G8_UINT = (int)DXGI_FORMAT_R8G8_UINT, R8G8_SNORM = (int)DXGI_FORMAT_R8G8_SNORM, R8G8_SINT = (int)DXGI_FORMAT_R8G8_SINT, R16_FLOAT = (int)DXGI_FORMAT_R16_FLOAT, //D16_UNORM = (int)vk::Format::eD16Unorm, //R16_UNORM = (int)vk::Format::eR16Unorm, R16_UINT = (int)DXGI_FORMAT_R16_UINT, R16_SNORM = (int)DXGI_FORMAT_R16_SNORM, R16_SINT = (int)DXGI_FORMAT_R16_SINT, R8_UNORM = (int)DXGI_FORMAT_R8_UNORM, R8_UINT = (int)DXGI_FORMAT_R8_UINT, R8_SNORM = (int)DXGI_FORMAT_R8_SNORM, R8_SINT = (int)DXGI_FORMAT_R8_SINT, A8_UNORM = (int)DXGI_FORMAT_A8_UNORM, //BC1_UNORM = (int)vk::Format::eBc1RgbUnormBlock, //FIXME: is this correct? //BC1_UNORM_SRGB = (int)vk::Format::eBc1RgbSrgbBlock, //FIXME: is this correct? //BC2_UNORM = (int)vk::Format::eBc2UnormBlock, //BC2_UNORM_SRGB = (int)vk::Format::eBc2SrgbBlock, //BC3_UNORM = (int)vk::Format::eBc3UnormBlock, //BC3_UNORM_SRGB = (int)vk::Format::eBc3SrgbBlock, //BC4_UNORM = (int)vk::Format::eBc4UnormBlock, //BC4_SNORM = (int)vk::Format::eBc4SnormBlock, //BC5_UNORM = (int)vk::Format::eBc5UnormBlock, //BC5_SNORM = (int)vk::Format::eBc5SnormBlock, B5G6R5_UNORM = (int)DXGI_FORMAT_B5G6R5_UNORM, B5G5R5A1_UNORM = (int)DXGI_FORMAT_B5G5R5A1_UNORM, B8G8R8A8_UNORM = (int)DXGI_FORMAT_B8G8R8A8_UNORM, B8G8R8A8_UNORM_SRGB = (int)DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, //B8G8R8A8_SNORM = (int)vk::Format::eB8G8R8A8Snorm, //B8G8R8A8_UINT = (int)vk::Format::eB8G8R8A8Uint, //B8G8R8A8_SINT = (int)vk::Format::eB8G8R8A8Sint, B8G8R8X8_UNORM = (int)DXGI_FORMAT_B8G8R8X8_UNORM, B8G8R8X8_UNORM_SRGB = (int)DXGI_FORMAT_B8G8R8X8_UNORM_SRGB, //BC6H_UF16 = (int)vk::Format::eBc6HUfloatBlock, //BC6H_SF16 = (int)vk::Format::eBc6HSfloatBlock, //BC7_UNORM = (int)vk::Format::eBc7UnormBlock, //BC7_UNORM_SRGB = (int)vk::Format::eBc7SrgbBlock, B4G4R4A4_UNORM = (int)DXGI_FORMAT_B4G4R4A4_UNORM, D32_FLOAT_S8X24_UINT = (int)DXGI_FORMAT_D32_FLOAT_S8X24_UINT, }; static inline std::string FormatToStr(Format format) { switch (format) { case Format::UNKNOWN: return "UNKNOWN"; case Format::R32G32B32A32_FLOAT: return "R32G32B32A32_FLOAT"; case Format::R32G32B32A32_UINT: return "R32G32B32A32_UINT"; case Format::R32G32B32A32_SINT: return "R32G32B32A32_SINT"; case Format::R32G32B32_FLOAT: return "R32G32B32_FLOAT"; case Format::R32G32B32_UINT: return "R32G32B32_UINT"; case Format::R32G32B32_SINT: return "R32G32B32_SINT"; case Format::R16G16B16A16_FLOAT: return "R16G16B16A16_FLOAT"; case Format::R16G16B16A16_UINT: return "R16G16B16A16_UINT"; case Format::R16G16B16A16_SINT: return "R16G16B16A16_SINT"; case Format::R16G16B16A16_UNORM: return "R16G16B16A16_UNORM"; case Format::R16G16B16A16_SNORM: return "R16G16B16A16_SNORM"; case Format::R32G32_FLOAT: return "R32G32_FLOAT"; case Format::R32G32_UINT: return "R32G32_UINT"; case Format::R32G32_SINT: return "R32G32_SINT"; case Format::R8G8B8A8_UNORM: return "R8G8B8A8_UNORM"; case Format::R8G8B8A8_UNORM_SRGB: return "R8G8B8A8_UNORM_SRGB"; case Format::R8G8B8A8_SNORM: return "R8G8B8A8_SNORM"; case Format::R8G8B8A8_UINT: return "R8G8B8A8_UINT"; case Format::R8G8B8A8_SINT: return "R8G8B8A8_SINT"; case Format::D32_FLOAT: return "D32_FLOAT"; case Format::R32_UINT: return "R32_UINT"; case Format::R32_SINT: return "R32_SINT"; case Format::R32_FLOAT: return "R32_FLOAT"; case Format::D24_UNFORM_S8_UINT: return "D24_UNFORM_S8_UINT"; case Format::R8_UNORM: return "R8_UNORM"; case Format::R10G10B10A2_UNORM: return "R10G10B10A2_UNORM"; case Format::R10G10B10A2_UINT: return "R10G10B10A2_UINT"; case Format::R11G11B10_FLOAT: return "R11G11B10_FLOAT"; case Format::R16G16_FLOAT: return "R16G16_FLOAT"; case Format::R16G16_UNORM: return "R16G16_UNORM"; case Format::R16G16_UINT: return "R16G16_UINT"; case Format::R16G16_SNORM: return "R16G16_SNORM"; case Format::R16G16_SINT: return "R16G16_SINT"; case Format::R8G8_UNORM: return "R8G8_UNORM"; case Format::R8G8_UINT: return "R8G8_UINT"; case Format::R8G8_SNORM: return "R8G8_SNORM"; case Format::R8G8_SINT: return "R8G8_SINT"; case Format::R16_UNORM: return "R16_UNORM"; case Format::R16_SNORM: return "R16_SNORM"; case Format::R8_SNORM: return "R8_SNORM"; case Format::A8_UNORM: return "A8_UNORM"; case Format::B5G6R5_UNORM: return "B5G6R5_UNORM"; case Format::B5G5R5A1_UNORM: return "B5G5R5A1_UNORM"; case Format::B4G4R4A4_UNORM: return "B4G4R4A4_UNORM"; default: return "UNKNOWN (DEFAULT SWITCH STATEMENT)"; } } // From: https://github.com/Microsoft/DirectXTK/blob/master/Src/LoaderHelpers.h static inline unsigned int BitsPerPixel(Format format) { switch (format) { //case Format::R32G32B32A32_TYPELESS: case Format::R32G32B32A32_FLOAT: case Format::R32G32B32A32_UINT: case Format::R32G32B32A32_SINT: return 128; //case Format::R32G32B32_TYPELESS: case Format::R32G32B32_FLOAT: case Format::R32G32B32_UINT: case Format::R32G32B32_SINT: return 96; //case Format::R16G16B16A16_TYPELESS: case Format::R16G16B16A16_FLOAT: case Format::R16G16B16A16_UNORM: case Format::R16G16B16A16_UINT: case Format::R16G16B16A16_SNORM: case Format::R16G16B16A16_SINT: //case Format::R32G32_TYPELESS: case Format::R32G32_FLOAT: case Format::R32G32_UINT: case Format::R32G32_SINT: //case Format::R32G8X24_TYPELESS: case Format::D32_FLOAT_S8X24_UINT: //case Format::R32_FLOAT_X8X24_TYPELESS: //case Format::X32_TYPELESS_G8X24_UINT: //case Format::Y416: //case Format::Y210: //case Format::Y216: return 64; //case Format::R10G10B10A2_TYPELESS: case Format::R10G10B10A2_UNORM: case Format::R10G10B10A2_UINT: case Format::R11G11B10_FLOAT: //case Format::R8G8B8A8_TYPELESS: case Format::R8G8B8A8_UNORM: case Format::R8G8B8A8_UNORM_SRGB: case Format::R8G8B8A8_UINT: case Format::R8G8B8A8_SNORM: case Format::R8G8B8A8_SINT: //case Format::R16G16_TYPELESS: case Format::R16G16_FLOAT: case Format::R16G16_UNORM: case Format::R16G16_UINT: case Format::R16G16_SNORM: case Format::R16G16_SINT: case Format::R32_TYPELESS: case Format::D32_FLOAT: case Format::R32_FLOAT: case Format::R32_UINT: case Format::R32_SINT: //case Format::R24G8_TYPELESS: //case Format::D24_UNORM_S8_UINT: //case Format::R24_UNORM_X8_TYPELESS: //case Format::X24_TYPELESS_G8_UINT: //case Format::R9G9B9E5_SHAREDEXP: //case Format::R8G8_B8G8_UNORM: //case Format::G8R8_G8B8_UNORM: case Format::B8G8R8A8_UNORM: case Format::B8G8R8X8_UNORM: //case Format::R10G10B10_XR_BIAS_A2_UNORM: //case Format::B8G8R8A8_TYPELESS: case Format::B8G8R8A8_UNORM_SRGB: //case Format::B8G8R8X8_TYPELESS: case Format::B8G8R8X8_UNORM_SRGB: //case Format::AYUV: //case Format::Y410: //case Format::YUY2: return 32; //case Format::P010: //case Format::P016: // return 24; //case Format::R8G8_TYPELESS: case Format::R8G8_UNORM: case Format::R8G8_UINT: case Format::R8G8_SNORM: case Format::R8G8_SINT: //case Format::R16_TYPELESS: case Format::R16_FLOAT: //case Format::D16_UNORM: case Format::R16_UNORM: case Format::R16_UINT: case Format::R16_SNORM: case Format::R16_SINT: case Format::B5G6R5_UNORM: case Format::B5G5R5A1_UNORM: //case Format::A8P8: case Format::B4G4R4A4_UNORM: return 16; //case Format::NV12: //case Format::420_OPAQUE: //case Format::NV11: // return 12; //case Format::R8_TYPELESS: case Format::R8_UNORM: case Format::R8_UINT: case Format::R8_SNORM: case Format::R8_SINT: case Format::A8_UNORM: //case Format::AI44: //case Format::IA44: //case Format::P8: return 8; //case Format::R1_UNORM: // return 1; //case Format::BC1_TYPELESS: //case Format::BC1_UNORM: //case Format::BC1_UNORM_SRGB: //case Format::BC4_TYPELESS: //case Format::BC4_UNORM: //case Format::BC4_SNORM: // return 4; //case Format::BC2_TYPELESS: //case Format::BC2_UNORM: //case Format::BC2_UNORM_SRGB: //case Format::BC3_TYPELESS: //case Format::BC3_UNORM: //case Format::BC3_UNORM_SRGB: //case Format::BC5_TYPELESS: //case Format::BC5_UNORM: //case Format::BC5_SNORM: //case Format::BC6H_TYPELESS: //case Format::BC6H_UF16: //case Format::BC6H_SF16: //case Format::BC7_TYPELESS: //case Format::BC7_UNORM: //case Format::BC7_UNORM_SRGB: // return 8; //#if (_WIN32_WINNT >= _WIN32_WINNT_WIN10) // // case Format::V408: // return 24; // // case Format::P208: // case Format::V208: // return 16; // //#endif // (_WIN32_WINNT >= _WIN32_WINNT_WIN10) // //#if defined(_XBOX_ONE) && defined(_TITLE) // // case Format::R10G10B10_7E3_A2_FLOAT: // case Format::R10G10B10_6E4_A2_FLOAT: // case Format::R10G10B10_SNORM_A2_UNORM: // return 32; // // case Format::D16_UNORM_S8_UINT: // case Format::R16_UNORM_X8_TYPELESS: // case Format::X16_TYPELESS_G8_UINT: // return 24; // // case Format::R4G4_UNORM: // return 8; // //#endif // _XBOX_ONE && _TITLE default: return 0; } } static inline unsigned int BytesPerPixel(Format format) { return BitsPerPixel(format) / 8; } } /* wr */ ================================================ FILE: src/d3d12/d3d12_fence.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "../util/log.hpp" #include "d3d12_defines.hpp" namespace wr::d3d12 { Fence* CreateFence(Device* device) { auto fence = new Fence(); auto n_device = device->m_native; // create the fences TRY_M(n_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence->m_native)), "Failed to create fence"); fence->m_native->SetName(L"SimpleFence"); // create a handle to a fence event fence->m_fence_event = CreateEvent(nullptr, FALSE, FALSE, nullptr); if (fence->m_fence_event == nullptr) { LOGC("Failed to create fence event"); } return fence; } void SetName(Fence * fence, std::wstring name) { fence->m_native->SetName(name.c_str()); } void Signal(Fence* fence, CommandQueue* cmd_queue) { TRY_M(cmd_queue->m_native->Signal(fence->m_native, fence->m_fence_value), "Failed to set fence signal"); } void WaitFor(Fence* fence) { if (fence->m_native->GetCompletedValue() < fence->m_fence_value) { // we have the fence create an event which is signaled once the fence's current value is "fenceValue" TRY_M(fence->m_native->SetEventOnCompletion(fence->m_fence_value, fence->m_fence_event), "Failed to set fence event."); WaitForSingleObject(fence->m_fence_event, INFINITE); } // increment fenceValue for next frame (or usage) fence->m_fence_value++; } void Destroy(Fence* fence) { SAFE_RELEASE(fence->m_native) CloseHandle(fence->m_fence_event); delete fence; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_functions.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include "d3d12_structs.hpp" #include "d3d12_constant_buffer_pool.hpp" #include "d3d12_dynamic_descriptor_heap.hpp" #include "d3d12_descriptors_allocations.hpp" namespace wr::d3d12 { struct TextureResource; // Device [[nodiscard]] Device* CreateDevice(); RaytracingType GetRaytracingType(Device* device); void Destroy(Device* device); void SetName(Device* device, std::wstring name); // CommandQueue [[nodiscard]] CommandQueue* CreateCommandQueue(Device* device, CmdListType type); void Execute(CommandQueue* cmd_queue, std::vector const & cmd_lists, Fence* fence); void Destroy(CommandQueue* cmd_queue); void SetName(CommandQueue* cmd_queue, std::wstring name); // CommandList [[nodiscard]] CommandList* CreateCommandList(Device* device, unsigned int num_allocators, CmdListType type); void SetName(CommandList* cmd_list, std::string const & name); void SetName(CommandList* cmd_list, std::wstring const & name); void Begin(CommandList* cmd_list, unsigned int frame_idx); void End(CommandList* cmd_list); void ExecuteBundle(CommandList* cmd_list, CommandList* bundle); void ExecuteIndirect(CommandList* cmd_list, CommandSignature* cmd_signature, IndirectCommandBuffer* buffer, uint32_t frame_idx); void BindRenderTarget(CommandList* cmd_list, RenderTarget* render_target, bool clear = true, bool clear_depth = true); void BindRenderTargetVersioned(CommandList* cmd_list, RenderTarget* render_target, unsigned int frame_idx, bool clear = true, bool clear_depth = true); void BindRenderTargetOnlyDepth(CommandList* cmd_list, RenderTarget* render_target, bool clear = true); void BindViewport(CommandList* cmd_list, Viewport const & viewport); void BindPipeline(CommandList* cmd_list, PipelineState* pipeline_state); void BindComputePipeline(CommandList* cmd_list, PipelineState* pipeline_state); void BindRaytracingPipeline(CommandList* cmd_list, StateObject* state_object, bool fallback = false); void BindDescriptorHeap(CommandList* cmd_list, DescriptorHeap* heap, DescriptorHeapType type, unsigned int frame_idx, bool fallback = false); void BindDescriptorHeaps(CommandList* cmd_list, bool fallback = false); void SetPrimitiveTopology(CommandList* cmd_list, D3D12_PRIMITIVE_TOPOLOGY topology); void BindConstantBuffer(CommandList* cmd_list, HeapResource* buffer, unsigned int root_parameter_idx, unsigned int frame_idx); void 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); void 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); void BindComputeConstantBuffer(CommandList* cmd_list, HeapResource* buffer, unsigned int root_parameter_idx, unsigned int frame_idx); void BindComputeShaderResourceView(CommandList* cmd_list, ID3D12Resource* resource, unsigned int root_parameter_idx); void BindComputeUnorederedAccessView(CommandList* cmd_list, ID3D12Resource* resource, unsigned int root_parameter_idx); //void Bind(CommandList& cmd_list, TextureArray& ta, unsigned int root_param_index); void BindDescriptorTable(CommandList* cmd_list, DescHeapGPUHandle& handle, unsigned int root_param_index); void BindComputeDescriptorTable(CommandList* cmd_list, DescHeapGPUHandle& handle, unsigned int root_param_index); //void Bind(CommandList& cmd_list, std::vector const & heaps); void BindVertexBuffer(CommandList* cmd_list, StagingBuffer* buffer, std::size_t offset, std::size_t size, std::size_t m_stride); void BindIndexBuffer(CommandList* cmd_list, StagingBuffer* buffer, std::uint32_t offset, std::uint32_t size); void Draw(CommandList* cmd_list, std::uint32_t vertex_count, std::uint32_t inst_count, std::uint32_t vertex_start); void DrawIndexed(CommandList* cmd_list, std::uint32_t idx_count, std::uint32_t inst_count, std::uint32_t idx_start, std::uint32_t vertex_start); void Dispatch(CommandList* cmd_list, unsigned int thread_group_count_x, unsigned int thread_group_count_y, unsigned int thread_group_count_z); void Transition(CommandList* cmd_list, RenderTarget* render_target, unsigned int frame_index, ResourceState from, ResourceState to); void Transition(CommandList* cmd_list, RenderTarget* render_target, ResourceState from, ResourceState to); void Transition(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to); void Transition(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to, unsigned int first_subresource, unsigned int num_subresources); void TransitionSubresource(CommandList* cmd_list, TextureResource* texture, ResourceState from, ResourceState to, unsigned int subresource); void Transition(CommandList* cmd_list, std::vector const& textures, ResourceState from, ResourceState to); void Transition(CommandList* cmd_list, IndirectCommandBuffer* buffer, ResourceState from, ResourceState to, uint32_t frame_idx); void Transition(CommandList* cmd_list, StagingBuffer* buffer, ResourceState from, ResourceState to); void TransitionDepth(CommandList* cmd_list, RenderTarget* render_target, ResourceState from, ResourceState to); void Alias(CommandList* cmd_list, TextureResource* resource_before, TextureResource* resource_after); void UAVBarrier(CommandList* cmd_list, std::vector const & resources); void UAVBarrier(CommandList* cmd_list, std::vector const & resources); void 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); // void Transition(CommandList* cmd_list, Texture* texture, ResourceState from, ResourceState to); void Destroy(CommandList* cmd_list); // Command List Signature CommandSignature* CreateCommandSignature(Device* device, RootSignature* root_signature, std::vector arg_descs, size_t byte_stride); void SetName(CommandSignature* cmd_signature, std::wstring name); void Destroy(CommandSignature* cmd_signature); // RenderTarget [[nodiscard]] RenderTarget* CreateRenderTarget(Device* device, unsigned int width, unsigned int height, desc::RenderTargetDesc descriptor); void SetName(RenderTarget* render_target, std::wstring name); void SetName(RenderTarget* render_target, std::string name); unsigned int GetRenderTargetWidth(RenderTarget* render_target); unsigned int GetRenderTargetHeight(RenderTarget* render_target); void CreateRenderTargetViews(RenderTarget* render_target, Device* device); void CreateDepthStencilBuffer(RenderTarget* render_target, Device* device, unsigned int width, unsigned int height); void CreateSRVFromDSV(RenderTarget* render_target, DescHeapCPUHandle& handle); void CreateSRVFromRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int num, Format formats[8]); void CreateUAVFromRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int num, Format formats[8]); void CreateUAVFromSpecificRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int id, Format format); void CreateSRVFromSpecificRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int id, Format format); void CreateSRVFromStructuredBuffer(HeapResource* structured_buffer, DescHeapCPUHandle& handle, unsigned int id); // FIXME: Wrong location // void CreateUAVFromTexture(Texture* tex, DescHeapCPUHandle& handle, unsigned int mip_slice = 0, unsigned int array_slice = 0); // void CreateSRVFromTexture(Texture* tex, DescHeapCPUHandle& handle); void Resize(RenderTarget** render_target, Device* device, unsigned int width, unsigned int height); void IncrementFrameIdx(RenderTarget* render_target); void DestroyDepthStencilBuffer(RenderTarget* render_target); void DestroyRenderTargetViews(RenderTarget* render_target); void Destroy(RenderTarget* render_target); // Texture [[nodiscard]] TextureResource* CreateTexture(Device* device, desc::TextureDesc* description, bool allow_uav); [[nodiscard]] TextureResource* CreatePlacedTexture(Device* device, desc::TextureDesc* description, bool allow_uav, Heap* heap); void SetName(TextureResource* tex, std::wstring name); void CreateSRVFromTexture(TextureResource* tex); void CreateSRVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle); void CreateSRVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_levels, unsigned int most_detailed_mip); void CreateSRVFromCubemapFace(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_levels, unsigned int most_detailed_mip, unsigned int face_idx); void CreateUAVFromTexture(TextureResource* tex, unsigned int mip_slice); void CreateUAVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_slice); void CreateUAVFromCubemapFace(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_slice, unsigned int face_idx); void CreateRTVFromTexture2D(TextureResource* tex); void CreateRTVFromCubemap(TextureResource* tex); //void CreateUAVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_slice = 0, unsigned int array_slice = 0); void SetShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex); void SetShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count = 1); void SetShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex); void SetShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count = 1); void SetRTShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex); void SetRTShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count = 1); void SetRTShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex); void SetRTShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count = 1); void CopyResource(wr::d3d12::CommandList* cmd_list, TextureResource* src_texture, TextureResource* dst_texture); void Destroy(TextureResource* tex); // Format test and support functions bool CheckUAVCompatibility(Format format); bool CheckOptionalUAVFormat(Format format); bool CheckBGRFormat(Format format); bool CheckSRGBFormat(Format format); bool IsOptionalFormatSupported(Device* device, Format format); Format RemoveSRGB(Format format); Format BGRtoRGB(Format format); // Read-back buffer //! Create a readback buffer /*! \param aligned_buffer_size The size of the buffer you want to create aligned to 256. */ [[nodiscard]] ReadbackBufferResource* CreateReadbackBuffer(Device* device, std::uint32_t aligned_buffer_size); //! Map a readback buffer /*! \param aligned_buffer_size The size of the buffer you want to create aligned to 256. */ void* MapReadbackBuffer(ReadbackBufferResource* const readback_buffer, std::uint32_t aligned_buffer_size); //! Unmap a readback buffer void UnmapReadbackBuffer(ReadbackBufferResource* const readback_buffer); void SetName(ReadbackBufferResource* readback_buffer, std::wstring name); void Destroy(ReadbackBufferResource* readback_buffer); // RenderWindow [[nodiscard]] RenderWindow* CreateRenderWindow(Device* device, HWND window, CommandQueue* cmd_queue, unsigned int num_back_buffers); [[nodiscard]] RenderWindow* CreateRenderWindow(Device* device, IUnknown* window, CommandQueue* cmd_queue, unsigned int num_back_buffers); void Resize(RenderWindow* render_window, Device* device, unsigned int width, unsigned int height); void Present(RenderWindow* render_window); void Destroy(RenderWindow* render_window); // Descriptor Heap [[nodiscard]] DescriptorHeap* CreateDescriptorHeap(Device* device, desc::DescriptorHeapDesc const & descriptor); [[nodiscard]] DescHeapGPUHandle GetGPUHandle(DescriptorHeap* desc_heap, unsigned int frame_idx, unsigned int index = 0); [[nodiscard]] DescHeapCPUHandle GetCPUHandle(DescriptorHeap* desc_heap, unsigned int frame_idx, unsigned int index = 0); void SetName(DescriptorHeap* desc_heap, std::wstring name); void Offset(DescHeapGPUHandle& handle, unsigned int index, unsigned int increment_size); void Offset(DescHeapCPUHandle& handle, unsigned int index, unsigned int increment_size); void Destroy(DescriptorHeap* desc_heap); // Fence [[nodiscard]] Fence* CreateFence(Device* device); void SetName(Fence* fence, std::wstring name); void Signal(Fence* fence, CommandQueue* cmd_queue); void WaitFor(Fence* fence); void Destroy(Fence* fence); // Viewport [[nodiscard]] Viewport CreateViewport(int width, int height); void ResizeViewport(Viewport& viewport, int width, int height); // Shader [[nodiscard]] std::variant LoadShader(Device* device, ShaderType type, std::string const& path, std::string const& entry = "main", std::vector> defines = { {} }); void Destroy(Shader* shader); // Root Signature [[nodiscard]] RootSignature* CreateRootSignature(desc::RootSignatureDesc create_info); void SetName(RootSignature* root_signature, std::wstring name); void FinalizeRootSignature(RootSignature* root_signature, Device* device); void RefinalizeRootSignature(RootSignature* root_signature, Device* device); void Destroy(RootSignature* root_signature); // Pipeline State [[nodiscard]] PipelineState* CreatePipelineState(); // TODO: Creation of root signature and pipeline are not the same related to the descriptor. void SetName(PipelineState* pipeline_state, std::wstring name); void SetVertexShader(PipelineState* pipeline_state, Shader* shader); void SetFragmentShader(PipelineState* pipeline_state, Shader* shader); void SetComputeShader(PipelineState* pipeline_state, Shader* shader); void SetRootSignature(PipelineState* pipeline_state, RootSignature* root_signature); void FinalizePipeline(PipelineState* pipeline_state, Device* device, desc::PipelineStateDesc desc); void RefinalizePipeline(PipelineState* pipeline_state); // TODO: Deprecate this. This should be part of create so it void Destroy(PipelineState* pipeline_state); // Staging Buffer [[nodiscard]] StagingBuffer* CreateStagingBuffer(Device* device, void* data, std::uint64_t size, std::uint64_t m_stride, ResourceState resource_state); void SetName(StagingBuffer* buffer, std::wstring name); void UpdateStagingBuffer(StagingBuffer* buffer, void* data, std::uint64_t size, std::uint64_t offset); void StageBuffer(StagingBuffer* buffer, CommandList* cmd_list); void StageBufferRegion(StagingBuffer* buffer, std::uint64_t size, std::uint64_t offset, CommandList* cmd_list); void FreeStagingBuffer(StagingBuffer* buffer); void Evict(StagingBuffer* buffer); void CreateRawSRVFromStagingBuffer(StagingBuffer* buffer, DescHeapCPUHandle& handle, unsigned int count, Format format = Format::R32_TYPELESS); void CreateStructuredBufferSRVFromStagingBuffer(StagingBuffer* buffer, DescHeapCPUHandle& handle, unsigned int count, Format format = Format::R32_TYPELESS); void MakeResident(StagingBuffer* buffer); void Destroy(StagingBuffer* buffer); // Heap [[nodiscard]] Heap* CreateHeap_SBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count); [[nodiscard]] Heap* CreateHeap_BBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count); [[nodiscard]] Heap* CreateHeap_SSBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count); [[nodiscard]] Heap* CreateHeap_BSBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count); [[nodiscard]] HeapResource* AllocConstantBuffer(Heap* heap, std::uint64_t size_in_bytes); [[nodiscard]] HeapResource* AllocConstantBuffer(Heap* heap, std::uint64_t size_in_bytes); [[nodiscard]] HeapResource* AllocByteAddressBuffer(Heap* heap, std::uint64_t size_in_bytes); [[nodiscard]] HeapResource* AllocStructuredBuffer(Heap* heap, std::uint64_t size_in_bytes, std::uint64_t stride, bool used_as_uav); [[nodiscard]] HeapResource* AllocGenericBuffer(Heap* heap, std::uint64_t size_in_bytes); void SetName(Heap* heap, std::wstring name); void SetName(Heap* heap, std::wstring name); void SetName(Heap* heap, std::wstring name); void SetName(Heap* heap, std::wstring name); void DeallocConstantBuffer(Heap* heap, HeapResource* heapResource); void DeallocConstantBuffer(Heap* heap, HeapResource* heapResource); void DeallocBuffer(Heap* heap, HeapResource* heapResource); void MapHeap(Heap* heap); void MapHeap(Heap* heap); void UnmapHeap(Heap* heap); void UnmapHeap(Heap* heap); void MakeResident(Heap* heap); void MakeResident(Heap* heap); void MakeResident(Heap* heap); void MakeResident(Heap* heap); void EnqueueMakeResident(Heap* heap, Fence* fence); // Untested void EnqueueMakeResident(Heap* heap, Fence* fence); // Untested void EnqueueMakeResident(Heap* heap, Fence* fence); // Untested void EnqueueMakeResident(Heap* heap, Fence* fence); // Untested void Evict(Heap* heap); void Evict(Heap* heap); void Evict(Heap* heap); void Evict(Heap* heap); void Destroy(Heap* heap); void Destroy(Heap* heap); void Destroy(Heap* heap); void Destroy(Heap* heap); // Resources void UpdateConstantBuffer(HeapResource* buffer, unsigned int frame_idx, void* data, std::uint64_t size_in_bytes); void 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); void UpdateByteAddressBuffer(HeapResource* buffer, unsigned int frame_idx, void* data, std::uint64_t size_in_bytes); void CreateSRVFromByteAddressBuffer(HeapResource* resource, DescHeapCPUHandle& handle, unsigned int id, unsigned int count); // Indirect Command Buffer [[nodiscard]] IndirectCommandBuffer* CreateIndirectCommandBuffer(Device* device, std::size_t max_num_buffers, std::size_t command_size, uint32_t versions); void SetName(IndirectCommandBuffer* buffer, std::wstring name); void StageBuffer(CommandList* cmd_list, IndirectCommandBuffer* buffer, void* data, std::size_t num_commands, uint32_t frame_idx); // State Object [[nodiscard]] StateObject* CreateStateObject(Device* device, desc::StateObjectDesc desc); void RecreateStateObject(StateObject* state_object); void SetGlobalRootSignature(StateObject* state_object, RootSignature* global_root_signature); [[nodiscard]] std::uint64_t GetShaderIdentifierSize(Device* device); [[nodiscard]] void* GetShaderIdentifier(Device* device, StateObject* obj, std::string const & name); void SetName(StateObject* obj, std::wstring name); void Destroy(StateObject* obj); // Acceelration Structure [[nodiscard]] AccelerationStructure CreateBottomLevelAccelerationStructures(Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, std::vector geometry); [[nodiscard]] AccelerationStructure CreateTopLevelAccelerationStructure(Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, std::vector blas_list); void DestroyAccelerationStructure(AccelerationStructure& structure); void UAVBarrierAS(CommandList* cmd_list, AccelerationStructure const & structure, std::uint32_t frame_idx); void UpdateTopLevelAccelerationStructure(AccelerationStructure& tlas, Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, std::vector blas_list, std::uint32_t frame_idx); void CreateOrUpdateTLAS(Device* device, CommandList* cmd_list, bool& requires_init, d3d12::AccelerationStructure& out_tlas, std::vector blas_list, std::uint32_t frame_idx); // Shader Record [[nodiscard]] ShaderRecord CreateShaderRecord(void* identifier, std::uint64_t identifier_size, void* local_root_args = nullptr, std::uint64_t local_root_args_size = 0); void CopyShaderRecord(ShaderRecord src, void* dest); // Shader Table [[nodiscard]] ShaderTable* CreateShaderTable(Device* device, std::uint64_t num_shader_records, std::uint64_t shader_record_size); void AddShaderRecord(ShaderTable* table, ShaderRecord record); void SetName(AccelerationStructure& acceleration_structure, std::wstring name); void Destroy(ShaderTable* table); } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_heap.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "d3d12_defines.hpp" #include "../util/bitmap_allocator.hpp" namespace wr::d3d12 { namespace internal { static const auto MakeResidentSingle = [](auto heap) { decltype(Device::m_native) n_device; heap->m_native->GetDevice(IID_PPV_ARGS(&n_device)); std::array objects{ heap->m_native }; n_device->MakeResident(1, objects.data()); }; static const auto EnqueueMakeResidentSingle = [](auto heap, auto fence) { decltype(Device::m_native) n_device; heap->m_native->GetDevice(IID_PPV_ARGS(&n_device)); std::array objects{ heap->m_native }; n_device->EnqueueMakeResident(D3D12_RESIDENCY_FLAG_DENY_OVERBUDGET, 1, objects.data(), fence->m_native, fence->m_fence_value); }; static const auto EvictSingle = [](auto heap) { decltype(Device::m_native) n_device; heap->m_native->GetDevice(IID_PPV_ARGS(&n_device)); std::array objects{ heap->m_native }; n_device->Evict(1, objects.data()); }; } Heap* CreateHeap_SBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource , unsigned int versioning_count) { auto heap = new Heap(); heap->m_mapped = false; heap->m_versioning_count = versioning_count; heap->m_current_offset = 0; heap->m_alignment = 256; auto aligned_size = SizeAlignTwoPower(size_in_bytes, 65536); heap->m_heap_size = aligned_size; auto page_frame_count = SizeAlignTwoPower(heap->m_heap_size / heap->m_alignment, 64) / 64; heap->m_bitmap.resize(page_frame_count); for (int i = 0; i < heap->m_bitmap.size(); ++i) { heap->m_bitmap[i] = 0xffffffffffffffff; } auto heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); auto resource_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size, D3D12_RESOURCE_FLAG_NONE); TRY_M(device->m_native->CreateCommittedResource( &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&heap->m_native)), "Failed to create small buffer optimized heap."); return heap; } Heap* CreateHeap_BBO(Device* device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count) { auto heap = new Heap(); heap->m_mapped = false; heap->m_versioning_count = versioning_count; heap->m_current_offset = 0; heap->m_alignment = 65536; auto aligned_size = SizeAlignTwoPower(size_in_bytes, 65536); heap->m_heap_size = aligned_size; auto page_frame_count = SizeAlignTwoPower(heap->m_heap_size / heap->m_alignment, 64) / 64; heap->m_bitmap.resize(page_frame_count); for (int i = 0; i < heap->m_bitmap.size(); ++i) { heap->m_bitmap[i] = 0xffffffffffffffff; } D3D12_HEAP_PROPERTIES heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); CD3DX12_HEAP_DESC desc = {}; desc.SizeInBytes = heap->m_heap_size; desc.Properties = heap_properties; desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT; switch (resource_type) { case ResourceType::BUFFER: desc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS; break; case ResourceType::TEXTURE: desc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES; break; case ResourceType::RT_DS: desc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES; break; } TRY_M(device->m_native->CreateHeap(&desc, IID_PPV_ARGS(&heap->m_native)), "Failed to create heap."); return heap; } Heap* CreateHeap_SSBO(Device * device, std::uint64_t size_in_bytes, unsigned int versioning_count) { auto heap = new Heap(); heap->m_mapped = false; heap->m_versioning_count = versioning_count; heap->m_current_offset = 0; heap->m_alignment = 256; auto aligned_size = SizeAlignTwoPower(size_in_bytes, 65536); heap->m_heap_size = aligned_size; auto page_frame_count = SizeAlignTwoPower(heap->m_heap_size / heap->m_alignment, 64) / 64; heap->m_bitmap.resize(page_frame_count); for (int i = 0; i < heap->m_bitmap.size(); ++i) { heap->m_bitmap[i] = 0xffffffffffffffff; } auto heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); auto resource_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size, D3D12_RESOURCE_FLAG_NONE); TRY_M(device->m_native->CreateCommittedResource( &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&heap->m_native)), "Failed to create small buffer optimized heap."); heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); resource_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size, D3D12_RESOURCE_FLAG_NONE); TRY_M(device->m_native->CreateCommittedResource( &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&heap->m_staging_buffer)), "Failed to create small buffer optimized heap."); auto range = CD3DX12_RANGE(0, 0); heap->m_staging_buffer->Map(0, &range, reinterpret_cast(&heap->m_cpu_address)); return heap; } Heap* CreateHeap_BSBO(Device * device, std::uint64_t size_in_bytes, ResourceType resource_type, unsigned int versioning_count) { auto heap = new Heap(); heap->m_mapped = false; heap->m_versioning_count = versioning_count; heap->m_current_offset = 0; heap->m_alignment = 65536; auto aligned_size = SizeAlignTwoPower(size_in_bytes, 65536); heap->m_heap_size = aligned_size; auto page_frame_count = SizeAlignTwoPower(heap->m_heap_size / heap->m_alignment, 64) / 64; heap->m_bitmap.resize(page_frame_count); for (int i = 0; i < heap->m_bitmap.size(); ++i) { heap->m_bitmap[i] = 0xffffffffffffffff; } D3D12_HEAP_PROPERTIES heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_HEAP_DESC desc = {}; desc.SizeInBytes = heap->m_heap_size; desc.Properties = heap_properties; desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT; switch (resource_type) { case ResourceType::BUFFER: desc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS; break; case ResourceType::TEXTURE: desc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES; break; case ResourceType::RT_DS: desc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES; break; } TRY_M(device->m_native->CreateHeap(&desc, IID_PPV_ARGS(&heap->m_native)), "Failed to create heap."); heap_properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); auto resource_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size, D3D12_RESOURCE_FLAG_NONE); TRY_M(device->m_native->CreateCommittedResource( &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&heap->m_staging_buffer)), "Failed to create heap staging buffer."); auto range = CD3DX12_RANGE(0, 0); heap->m_staging_buffer->Map(0, &range, reinterpret_cast(&heap->m_cpu_address)); return heap; } HeapResource* AllocConstantBuffer(Heap* heap, std::uint64_t size_in_bytes) { auto cb = new HeapResource(); auto aligned_size = SizeAlignTwoPower(size_in_bytes, 256); cb->m_unaligned_size = size_in_bytes; cb->m_gpu_addresses.resize(heap->m_versioning_count); auto frame_count = heap->m_heap_size / heap->m_alignment; auto needed_frames = aligned_size / heap->m_alignment*heap->m_versioning_count; auto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames); if (!start_frame.has_value()) { delete cb; return nullptr; } for (std::uint64_t i = 0; i < needed_frames; ++i) { util::ClearPage(heap->m_bitmap, start_frame.value() + i); } heap->m_current_offset = start_frame.value() * heap->m_alignment; cb->m_begin_offset = heap->m_current_offset; for (auto i = 0u; i < heap->m_versioning_count; i++) { cb->m_gpu_addresses[i] = heap->m_native->GetGPUVirtualAddress() + heap->m_current_offset; heap->m_current_offset += aligned_size; } // If the heap is supposed to be mapped immediatly map the new resource. if (heap->m_mapped) { cb->m_cpu_addresses = std::vector(heap->m_versioning_count); auto&& addresses = cb->m_cpu_addresses.value(); for (auto i = 0u; i < heap->m_versioning_count; i++) { addresses[i] = heap->m_cpu_address + (cb->m_begin_offset + (SizeAlignTwoPower(cb->m_unaligned_size, 255) * i)); } } cb->m_heap_vector_location = heap->m_resources.size(); heap->m_resources.push_back(cb); cb->m_heap_sbo = heap; cb->m_resource_heap_optimization = HeapOptimization::SMALL_BUFFERS; return cb; } HeapResource* AllocConstantBuffer(Heap* heap, std::uint64_t size_in_bytes) { auto cb = new HeapResource(); decltype(Device::m_native) n_device; heap->m_native->GetDevice(IID_PPV_ARGS(&n_device)); cb->m_unaligned_size = size_in_bytes; auto aligned_size_in_bytes = SizeAlignTwoPower(size_in_bytes, 65536); auto frame_count = heap->m_heap_size / heap->m_alignment; auto needed_frames = aligned_size_in_bytes / heap->m_alignment*heap->m_versioning_count; auto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames); if (!start_frame.has_value()) { delete cb; return nullptr; } for (std::uint64_t i = 0; i < needed_frames; ++i) { util::ClearPage(heap->m_bitmap, start_frame.value() + i); } heap->m_current_offset = start_frame.value() * heap->m_alignment; CD3DX12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size_in_bytes, D3D12_RESOURCE_FLAG_NONE); cb->m_gpu_addresses.resize(heap->m_versioning_count); cb->m_heap_vector_location = heap->m_resources.size(); cb->m_begin_offset = heap->m_current_offset; std::vector temp_resources(heap->m_versioning_count); for (auto i = 0u; i < heap->m_versioning_count; i++) { TRY_M(n_device->CreatePlacedResource(heap->m_native, heap->m_current_offset, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&temp_resources[i])), "Failed to create constant buffer placed resource."); heap->m_current_offset += aligned_size_in_bytes; cb->m_gpu_addresses[i] = temp_resources[i]->GetGPUVirtualAddress(); } // If the heap is supposed to be mapped immediatly map the new resource. if (heap->m_mapped) { cb->m_cpu_addresses = std::vector(heap->m_versioning_count); auto&& addresses = cb->m_cpu_addresses.value(); CD3DX12_RANGE read_range(0, 0); for (auto i = 0u; i < heap->m_versioning_count; i++) { TRY_M(temp_resources[i]->Map(0, &read_range, reinterpret_cast(&addresses[i])), "Failed to map resource."); } } heap->m_resources.push_back(std::make_pair(cb, temp_resources)); cb->m_heap_bbo = heap; cb->m_resource_heap_optimization = HeapOptimization::BIG_BUFFERS; return cb; } HeapResource* AllocByteAddressBuffer(Heap* heap, std::uint64_t size_in_bytes) { auto cb = new HeapResource(); decltype(Device::m_native) n_device; heap->m_native->GetDevice(IID_PPV_ARGS(&n_device)); cb->m_unaligned_size = size_in_bytes; auto aligned_size_in_bytes = SizeAlignTwoPower(size_in_bytes, 65536); auto frame_count = heap->m_heap_size / heap->m_alignment; auto needed_frames = aligned_size_in_bytes / heap->m_alignment*heap->m_versioning_count; auto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames); if (!start_frame.has_value()) { delete cb; return nullptr; } for (std::uint64_t i = 0; i < needed_frames; ++i) { util::ClearPage(heap->m_bitmap, start_frame.value() + i); } heap->m_current_offset = start_frame.value() * heap->m_alignment; CD3DX12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size_in_bytes, D3D12_RESOURCE_FLAG_NONE); cb->m_gpu_addresses.resize(heap->m_versioning_count); cb->m_heap_vector_location = heap->m_resources.size(); cb->m_begin_offset = heap->m_current_offset; std::vector temp_resources(heap->m_versioning_count); for (auto i = 0u; i < heap->m_versioning_count; i++) { TRY_M(n_device->CreatePlacedResource(heap->m_native, heap->m_current_offset, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&temp_resources[i])), "Failed to create constant buffer placed resource."); heap->m_current_offset += aligned_size_in_bytes; cb->m_gpu_addresses[i] = temp_resources[i]->GetGPUVirtualAddress(); } // If the heap is supposed to be mapped immediatly map the new resource. if (heap->m_mapped) { cb->m_cpu_addresses = std::vector(heap->m_versioning_count); auto&& addresses = cb->m_cpu_addresses.value(); CD3DX12_RANGE read_range(0, 0); for (auto i = 0u; i < heap->m_versioning_count; i++) { TRY_M(temp_resources[i]->Map(0, &read_range, reinterpret_cast(&addresses[i])), "Failed to map resource."); } } heap->m_resources.push_back(std::make_pair(cb, temp_resources)); cb->m_heap_bbo = heap; cb->m_resource_heap_optimization = HeapOptimization::BIG_BUFFERS; return cb; } HeapResource* AllocStructuredBuffer(Heap* heap, std::uint64_t size_in_bytes, std::uint64_t stride, bool used_as_uav) { auto cb = new HeapResource(); decltype(Device::m_native) n_device; heap->m_native->GetDevice(IID_PPV_ARGS(&n_device)); cb->m_unaligned_size = size_in_bytes; cb->m_stride = stride; cb->m_used_as_uav = used_as_uav; auto aligned_size_in_bytes = SizeAlignTwoPower(size_in_bytes, 65536); auto frame_count = heap->m_heap_size / heap->m_alignment; auto needed_frames = aligned_size_in_bytes / heap->m_alignment*heap->m_versioning_count; auto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames); if (!start_frame.has_value()) { delete cb; return nullptr; } for (std::uint64_t i = 0; i < needed_frames; ++i) { util::ClearPage(heap->m_bitmap, start_frame.value() + i); } heap->m_current_offset = start_frame.value() * heap->m_alignment; CD3DX12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer( aligned_size_in_bytes, used_as_uav ? D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS : D3D12_RESOURCE_FLAG_NONE); cb->m_gpu_addresses.resize(heap->m_versioning_count); cb->m_heap_vector_location = heap->m_resources.size(); cb->m_begin_offset = heap->m_current_offset; std::vector temp_resources(heap->m_versioning_count); for (auto i = 0u; i < heap->m_versioning_count; i++) { TRY_M(n_device->CreatePlacedResource( heap->m_native, heap->m_current_offset, &desc, used_as_uav ? D3D12_RESOURCE_STATE_UNORDERED_ACCESS : D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, nullptr, IID_PPV_ARGS(&temp_resources[i])), "Failed to create constant buffer placed resource."); heap->m_current_offset += aligned_size_in_bytes; cb->m_gpu_addresses[i] = temp_resources[i]->GetGPUVirtualAddress(); cb->m_states.push_back(used_as_uav ? ResourceState::UNORDERED_ACCESS : ResourceState::PIXEL_SHADER_RESOURCE); } heap->m_resources.push_back(std::make_pair(cb, temp_resources)); cb->m_heap_bsbo = heap; cb->m_resource_heap_optimization = HeapOptimization::BIG_STATIC_BUFFERS; return cb; } HeapResource * AllocGenericBuffer(Heap* heap, std::uint64_t size_in_bytes) { auto cb = new HeapResource(); decltype(Device::m_native) n_device; heap->m_native->GetDevice(IID_PPV_ARGS(&n_device)); cb->m_unaligned_size = size_in_bytes; auto aligned_size_in_bytes = SizeAlignTwoPower(size_in_bytes, 65536); auto frame_count = heap->m_heap_size / heap->m_alignment; auto needed_frames = aligned_size_in_bytes / heap->m_alignment*heap->m_versioning_count; auto start_frame = util::FindFreePage(heap->m_bitmap, frame_count, needed_frames); if (!start_frame.has_value()) { delete cb; return nullptr; } for (std::uint64_t i = 0; i < needed_frames; ++i) { util::ClearPage(heap->m_bitmap, start_frame.value() + i); } heap->m_current_offset = start_frame.value() * heap->m_alignment; CD3DX12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_size_in_bytes, D3D12_RESOURCE_FLAG_NONE); cb->m_gpu_addresses.resize(heap->m_versioning_count); cb->m_heap_vector_location = heap->m_resources.size(); cb->m_begin_offset = heap->m_current_offset; std::vector temp_resources(heap->m_versioning_count); for (auto i = 0u; i < heap->m_versioning_count; i++) { TRY_M(n_device->CreatePlacedResource(heap->m_native, heap->m_current_offset, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&temp_resources[i])), "Failed to create constant buffer placed resource."); heap->m_current_offset += aligned_size_in_bytes; cb->m_gpu_addresses[i] = temp_resources[i]->GetGPUVirtualAddress(); } heap->m_resources.push_back(std::make_pair(cb, temp_resources)); cb->m_heap_bsbo = heap; cb->m_resource_heap_optimization = HeapOptimization::BIG_STATIC_BUFFERS; return cb; } void SetName(Heap* heap, std::wstring name) { heap->m_native->SetName(name.c_str()); } void SetName(Heap* heap, std::wstring name) { heap->m_native->SetName(name.c_str()); } void SetName(Heap* heap, std::wstring name) { heap->m_native->SetName(name.c_str()); } void SetName(Heap* heap, std::wstring name) { heap->m_native->SetName(name.c_str()); } void DeallocConstantBuffer(Heap* heap, HeapResource * heapResource) { std::vector::iterator it; for (it = heap->m_resources.begin(); it != heap->m_resources.end(); ++it) { if ((*it) == heapResource) break; } if (it == heap->m_resources.end()) { return; } heap->m_resources.erase(it); for (int i = 0; i < heap->m_resources.size(); ++i) { heap->m_resources[i]->m_heap_vector_location = i; } std::uint64_t frame = heapResource->m_begin_offset / heap->m_alignment; std::uint64_t frame_count = SizeAlignTwoPower(heapResource->m_unaligned_size, 256) / heap->m_alignment * heap->m_versioning_count; for (int i = 0; i < frame_count; ++i) { util::SetPage(heap->m_bitmap, frame + i); } delete heapResource; } void DeallocConstantBuffer(Heap* heap, HeapResource * heapResource) { std::vector>>::iterator it; for (it = heap->m_resources.begin(); it != heap->m_resources.end(); ++it) { if ((*it).first == heapResource) break; } if (it == heap->m_resources.end()) { return; } for (int i = 0; i < (*it).second.size(); ++i) { delete (*it).second[i]; } heap->m_resources.erase(it); for (int i = 0; i < heap->m_resources.size(); ++i) { heap->m_resources[i].first->m_heap_vector_location = i; } std::uint64_t frame = heapResource->m_begin_offset / heap->m_alignment; std::uint64_t frame_count = SizeAlignTwoPower(heapResource->m_unaligned_size, heap->m_alignment) / heap->m_alignment * heap->m_versioning_count; for (int i = 0; i < frame_count; ++i) { util::SetPage(heap->m_bitmap, frame + i); } delete heapResource; } void DeallocBuffer(Heap* heap, HeapResource * heapResource) { std::vector>>::iterator it; for (it = heap->m_resources.begin(); it != heap->m_resources.end(); ++it) { if ((*it).first == heapResource) break; } if (it == heap->m_resources.end()) { return; } for (int i = 0; i < (*it).second.size(); ++i) { delete (*it).second[i]; } heap->m_resources.erase(it); for (int i = 0; i < heap->m_resources.size(); ++i) { heap->m_resources[i].first->m_heap_vector_location = i; } std::uint64_t frame = heapResource->m_begin_offset / heap->m_alignment; std::uint64_t frame_count = SizeAlignTwoPower(heapResource->m_unaligned_size, heap->m_alignment) / heap->m_alignment * heap->m_versioning_count; for (int i = 0; i < frame_count; ++i) { util::SetPage(heap->m_bitmap, frame + i); } delete heapResource; } void MapHeap(Heap* heap) { heap->m_mapped = true; CD3DX12_RANGE read_range(0, 0); std::uint8_t* address; TRY_M(heap->m_native->Map(0, &read_range, reinterpret_cast(&address)), "Failed to map resource."); heap->m_cpu_address = address; for (auto& handle : heap->m_resources) { handle->m_cpu_addresses = std::vector(heap->m_versioning_count); auto&& addresses = handle->m_cpu_addresses.value(); for (auto i = 0u; i < heap->m_versioning_count; i++) { addresses[i] = address + (handle->m_begin_offset + (SizeAlignTwoPower(handle->m_unaligned_size, 255) * i)); } } } void MapHeap(Heap* heap) { heap->m_mapped = true; for (auto resource : heap->m_resources) { resource.first->m_cpu_addresses = std::vector(heap->m_versioning_count); auto&& addresses = resource.first->m_cpu_addresses.value(); CD3DX12_RANGE read_range(0, 0); for (auto i = 0u; i < heap->m_versioning_count; i++) { TRY_M(resource.second[i]->Map(0, &read_range, reinterpret_cast(&addresses[i])), "Failed to map resource."); } } } void UnmapHeap(Heap* heap) { heap->m_mapped = false; CD3DX12_RANGE read_range(0, 0); heap->m_native->Unmap(0, &read_range); // For safety set the optional cpu addresses back to nullopt. for (auto& resource : heap->m_resources) { resource->m_cpu_addresses = std::nullopt; } } void UnmapHeap(Heap* heap) { heap->m_mapped = false; CD3DX12_RANGE read_range(0, 0); for (auto& resource : heap->m_resources) { resource.first->m_cpu_addresses = std::nullopt; for (auto& n_resource : resource.second) { n_resource->Unmap(0, &read_range); } } } void MakeResident(Heap* heap) { internal::MakeResidentSingle(heap); } void MakeResident(Heap* heap) { internal::MakeResidentSingle(heap); } void MakeResident(Heap* heap) { internal::MakeResidentSingle(heap); } void MakeResident(Heap* heap) { internal::MakeResidentSingle(heap); } void EnqueueMakeResident(Heap* heap, Fence* fence) { internal::EnqueueMakeResidentSingle(heap, fence); } void EnqueueMakeResident(Heap* heap, Fence* fence) { internal::EnqueueMakeResidentSingle(heap, fence); } void EnqueueMakeResident(Heap* heap, Fence* fence) { internal::EnqueueMakeResidentSingle(heap, fence); } void EnqueueMakeResident(Heap* heap, Fence* fence) { internal::EnqueueMakeResidentSingle(heap, fence); } void Evict(Heap* heap) { internal::EvictSingle(heap); } void Evict(Heap* heap) { internal::EvictSingle(heap); } void Evict(Heap* heap) { internal::EvictSingle(heap); } void Evict(Heap* heap) { internal::EvictSingle(heap); } void Destroy(Heap* heap) { UnmapHeap(heap); SAFE_RELEASE(heap->m_native); for (auto& resource : heap->m_resources) { delete resource; } delete heap; } void Destroy(Heap* heap) { UnmapHeap(heap); SAFE_RELEASE(heap->m_native); for (auto& resource : heap->m_resources) { delete resource.first; for (auto& n_resource : resource.second) { delete n_resource; } } delete heap; } void Destroy(Heap* heap) { //UnmapHeap(heap); SAFE_RELEASE(heap->m_native); SAFE_RELEASE(heap->m_staging_buffer); for (auto& resource : heap->m_resources) { delete resource; } delete heap; } void Destroy(Heap* heap) { //UnmapHeap(heap); SAFE_RELEASE(heap->m_native); SAFE_RELEASE(heap->m_staging_buffer); for (auto& resource : heap->m_resources) { delete resource.first; for (auto& n_resource : resource.second) { SAFE_RELEASE(n_resource); } } delete heap; } void UpdateConstantBuffer(HeapResource* buffer, unsigned int frame_idx, void* data, std::uint64_t size_in_bytes) { if (buffer->m_cpu_addresses.has_value()) { auto&& addresses = buffer->m_cpu_addresses.value(); std::memcpy(addresses[frame_idx], data, size_in_bytes); } else { LOGW("Tried updating a unmapped constant buffer resource!"); } } void 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) { if (buffer->m_resource_heap_optimization != HeapOptimization::BIG_STATIC_BUFFERS) { return; } std::size_t aligned_size = SizeAlignTwoPower(buffer->m_unaligned_size, 65536); memcpy(buffer->m_heap_bsbo->m_cpu_address + buffer->m_begin_offset + offset + aligned_size * frame_idx, data, size_in_bytes); buffer->m_stride = stride; if (size_in_bytes != 0) { ID3D12Resource* resource = buffer->m_heap_bsbo->m_resources[buffer->m_heap_vector_location].second[frame_idx]; CD3DX12_RESOURCE_BARRIER resource_barrier = CD3DX12_RESOURCE_BARRIER::Transition(resource, static_cast(buffer->m_states[frame_idx]), D3D12_RESOURCE_STATE_COPY_DEST); cmd_list->m_native->ResourceBarrier(1, &resource_barrier); cmd_list->m_native->CopyBufferRegion(resource, offset, buffer->m_heap_bsbo->m_staging_buffer, buffer->m_begin_offset + offset + aligned_size * frame_idx, size_in_bytes); CD3DX12_RESOURCE_BARRIER copy_barrier = CD3DX12_RESOURCE_BARRIER::Transition(resource, D3D12_RESOURCE_STATE_COPY_DEST, static_cast(buffer->m_states[frame_idx])); cmd_list->m_native->ResourceBarrier(1, ©_barrier); } } void UpdateByteAddressBuffer(HeapResource* buffer, unsigned int frame_idx, void* data, std::uint64_t size_in_bytes) { UpdateConstantBuffer(buffer, frame_idx, data, size_in_bytes); } void CreateSRVFromByteAddressBuffer(HeapResource* resource, DescHeapCPUHandle& handle, unsigned int id, unsigned int count) { auto& n_resources = resource->m_heap_bbo->m_resources[resource->m_heap_vector_location]; auto& n_resource = n_resources.second[id]; decltype(Device::m_native) n_device; n_resource->GetDevice(IID_PPV_ARGS(&n_device)); auto increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Format = DXGI_FORMAT_R32_TYPELESS; srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; srv_desc.Buffer.NumElements = count; srv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW; n_device->CreateShaderResourceView(n_resource, &srv_desc, handle.m_native); Offset(handle, 1, increment_size); } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_indirect_command_buffer.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "../util/log.hpp" #include "d3d12_defines.hpp" namespace wr::d3d12 { IndirectCommandBuffer* CreateIndirectCommandBuffer(Device* device, std::size_t max_commands, std::size_t command_size, uint32_t versions) { auto buffer = new IndirectCommandBuffer(); buffer->m_num_max_commands = max_commands; buffer->m_num_commands = 0; buffer->m_command_size = command_size; buffer->m_native.resize(versions); buffer->m_native_upload.resize(versions); CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(max_commands * command_size); for (uint32_t i = 0; i < versions; ++i) { TRY(device->m_native->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, nullptr, IID_PPV_ARGS(buffer->m_native.data() + i))); TRY(device->m_native->CreateCommittedResource( &heap_properties_upload, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(buffer->m_native_upload.data() + i))); } return buffer; } void SetName(IndirectCommandBuffer* buffer, std::wstring name) { for (uint32_t i = 0, j = (uint32_t)buffer->m_native.size(); i < j; ++i) { buffer->m_native[i]->SetName((name + L" #" + std::to_wstring(i)).c_str()); buffer->m_native_upload[i]->SetName((name + L" #" + std::to_wstring(i) + L" Upload Buffer").c_str()); } } void StageBuffer(CommandList* cmd_list, IndirectCommandBuffer* buffer, void* data, std::size_t num_commands, uint32_t frame_idx) { D3D12_SUBRESOURCE_DATA cmd_data = {}; cmd_data.pData = reinterpret_cast(data); cmd_data.RowPitch = num_commands * buffer->m_command_size; cmd_data.SlicePitch = cmd_data.RowPitch; buffer->m_num_commands = num_commands; UpdateSubresources<1>(cmd_list->m_native, buffer->m_native[frame_idx], buffer->m_native_upload[frame_idx], 0, 0, 1, &cmd_data); } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_material_pool.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_material_pool.hpp" namespace wr { D3D12MaterialPool::D3D12MaterialPool(D3D12RenderSystem & render_system) : m_render_system(render_system) { m_constant_buffer_pool = m_render_system.CreateConstantBufferPool(1_mb); } D3D12MaterialPool::~D3D12MaterialPool() { } void D3D12MaterialPool::Evict() { m_constant_buffer_pool->Evict(); } void D3D12MaterialPool::MakeResident() { m_constant_buffer_pool->MakeResident(); } } /* wr */ ================================================ FILE: src/d3d12/d3d12_material_pool.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../material_pool.hpp" #include "d3d12_renderer.hpp" namespace wr { class D3D12MaterialPool : public MaterialPool { public: explicit D3D12MaterialPool(D3D12RenderSystem& render_system); ~D3D12MaterialPool() final; void Evict() final; void MakeResident() final; protected: D3D12RenderSystem& m_render_system; }; } /* wr */ ================================================ FILE: src/d3d12/d3d12_model_pool.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_model_pool.hpp" #include "../util/bitmap_allocator.hpp" #include "d3d12_functions.hpp" #include "d3d12_defines.hpp" #include "d3d12_renderer.hpp" namespace wr { D3D12ModelPool::D3D12ModelPool(D3D12RenderSystem& render_system, std::size_t vertex_buffer_size_in_bytes, std::size_t index_buffer_size_in_bytes) : ModelPool(SizeAlignAnyAlignment(vertex_buffer_size_in_bytes, 65536), SizeAlignAnyAlignment(index_buffer_size_in_bytes, 65536)), m_render_system(render_system), m_intermediate_size(0) { m_vertex_buffer = d3d12::CreateStagingBuffer(render_system.m_device, nullptr, SizeAlignAnyAlignment(vertex_buffer_size_in_bytes, 65536), sizeof(Vertex), ResourceState::VERTEX_AND_CONSTANT_BUFFER); SetName(m_vertex_buffer, L"Model Pool Vertex Buffer"); m_vertex_buffer_size = SizeAlignAnyAlignment(vertex_buffer_size_in_bytes, 65536); m_index_buffer = d3d12::CreateStagingBuffer(render_system.m_device, nullptr, SizeAlignAnyAlignment(index_buffer_size_in_bytes, 65536), sizeof(std::uint32_t), ResourceState::INDEX_BUFFER); SetName(m_index_buffer, L"Model Pool Index Buffer"); m_index_buffer_size = SizeAlignAnyAlignment(index_buffer_size_in_bytes, 65536); m_vertex_heap_start_block = new MemoryBlock; m_vertex_heap_start_block->m_free = true; m_vertex_heap_start_block->m_next_block = nullptr; m_vertex_heap_start_block->m_prev_block = nullptr; m_vertex_heap_start_block->m_offset = 0; m_vertex_heap_start_block->m_size = m_vertex_buffer_size; m_index_heap_start_block = new MemoryBlock; m_index_heap_start_block->m_free = true; m_index_heap_start_block->m_next_block = nullptr; m_index_heap_start_block->m_prev_block = nullptr; m_index_heap_start_block->m_offset = 0; m_index_heap_start_block->m_size = m_index_buffer_size; m_intermediate_buffer = NULL; } D3D12ModelPool::~D3D12ModelPool() { d3d12::Destroy(m_vertex_buffer); d3d12::Destroy(m_index_buffer); while (m_vertex_heap_start_block != nullptr) { MemoryBlock* temp = m_vertex_heap_start_block; m_vertex_heap_start_block = m_vertex_heap_start_block->m_next_block; delete temp; } while (m_index_heap_start_block != nullptr) { MemoryBlock* temp = m_index_heap_start_block; m_index_heap_start_block = m_index_heap_start_block->m_next_block; delete temp; } for (auto& handle : m_loaded_meshes) { delete handle.second; } } void D3D12ModelPool::Evict() { d3d12::Evict(m_vertex_buffer); d3d12::Evict(m_index_buffer); } void D3D12ModelPool::MakeResident() { d3d12::MakeResident(m_vertex_buffer); d3d12::MakeResident(m_index_buffer); } void D3D12ModelPool::StageMeshes(d3d12::CommandList * cmd_list) { while (!m_command_queue.empty()) { internal::Command* command = static_cast(m_command_queue.front()); switch (command->m_type) { case internal::CommandType::STAGE: { internal::StageCommand* stage_command = static_cast(command); d3d12::StageBufferRegion(stage_command->m_buffer, stage_command->m_size, stage_command->m_offset, cmd_list); delete stage_command; m_command_queue.pop(); break; } case internal::CommandType::COPY: { internal::CopyCommand* copy_command = static_cast(command); cmd_list->m_native->CopyBufferRegion(copy_command->m_dest, copy_command->m_dest_offset, copy_command->m_source, copy_command->m_source_offset, copy_command->m_size); delete copy_command; m_command_queue.pop(); } break; case internal::CommandType::TRANSITION: { internal::TransitionCommand* transition_command = static_cast(command); CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(transition_command->m_buffer, static_cast(transition_command->m_old_state), static_cast(transition_command->m_new_state)); cmd_list->m_native->ResourceBarrier(1, &barrier); delete transition_command; m_command_queue.pop(); } break; case internal::CommandType::READ: { internal::ReadCommand* read_command = static_cast(command); cmd_list->m_native->CopyBufferRegion(read_command->m_buffer->m_staging, read_command->m_offset, read_command->m_buffer->m_buffer, read_command->m_offset, read_command->m_size); delete read_command; m_command_queue.pop(); } break; default: { delete command; m_command_queue.pop(); } break; } } } d3d12::StagingBuffer * D3D12ModelPool::GetVertexStagingBuffer() { return m_vertex_buffer; } d3d12::StagingBuffer * D3D12ModelPool::GetIndexStagingBuffer() { return m_index_buffer; } internal::D3D12MeshInternal * D3D12ModelPool::GetMeshData(std::uint64_t mesh_handle) { return static_cast(m_loaded_meshes[mesh_handle]); } void D3D12ModelPool::ShrinkToFit() { ShrinkVertexHeapToFit(); ShrinkIndexHeapToFit(); } void D3D12ModelPool::ShrinkVertexHeapToFit() { MemoryBlock* last_occupied_block = nullptr; for (MemoryBlock* mem_block = m_vertex_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block) { if (mem_block->m_free == false) { last_occupied_block = mem_block; } } if (last_occupied_block == nullptr) { LOGW("You're trying to shrink an empty vertex heap, returning instead."); return; } size_t new_size = last_occupied_block->m_offset + last_occupied_block->m_size; new_size = SizeAlignAnyAlignment(new_size, 65536); if (new_size == m_vertex_buffer->m_size) { return; } ID3D12Resource* old_buffer = m_vertex_buffer->m_buffer; ID3D12Resource* new_buffer = nullptr; ID3D12Resource* old_staging = m_vertex_buffer->m_staging; ID3D12Resource* new_staging = nullptr; uint8_t* cpu_address; CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(new_size); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_upload, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&new_staging)); NAME_D3D12RESOURCE(new_staging); CD3DX12_RANGE read_range(0, new_size); new_staging->Map(0, &read_range, reinterpret_cast(&(cpu_address))); memcpy(cpu_address, m_vertex_buffer->m_cpu_address, new_size); m_vertex_buffer->m_size = static_cast(new_size); m_vertex_buffer->m_is_staged = true; m_vertex_buffer->m_cpu_address = cpu_address; m_render_system.WaitForAllPreviousWork(); SAFE_RELEASE(m_vertex_buffer->m_buffer); SAFE_RELEASE(m_vertex_buffer->m_staging); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &buffer_desc, static_cast(m_vertex_buffer->m_target_resource_state), nullptr, IID_PPV_ARGS(&new_buffer)); NAME_D3D12RESOURCE(new_buffer); m_vertex_buffer->m_buffer = new_buffer; m_vertex_buffer->m_staging = new_staging; m_vertex_buffer->m_gpu_address = new_buffer->GetGPUVirtualAddress(); for (int i = 0; i < m_command_queue.size(); ++i) { internal::Command* command = m_command_queue.front(); m_command_queue.pop(); m_command_queue.push(command); switch (command->m_type) { case internal::CommandType::COPY: { internal::CopyCommand* copy_command = static_cast(command); if (copy_command->m_dest == old_staging) { copy_command->m_dest = new_staging; } if (copy_command->m_dest == old_buffer) { copy_command->m_dest = new_buffer; } if (copy_command->m_source == old_staging) { copy_command->m_source = new_staging; } if (copy_command->m_source == old_buffer) { copy_command->m_source = new_buffer; } } break; case internal::CommandType::TRANSITION: { internal::TransitionCommand* transition_command = static_cast(command); if (transition_command->m_buffer == old_staging) { transition_command->m_buffer = new_staging; } if (transition_command->m_buffer == old_buffer) { transition_command->m_buffer = new_buffer; } } break; } } internal::StageCommand* stageCommand = new internal::StageCommand; stageCommand->m_type = internal::STAGE; stageCommand->m_buffer = m_vertex_buffer; stageCommand->m_offset = 0; stageCommand->m_size = new_size; m_command_queue.push(stageCommand); MemoryBlock* mem_block = last_occupied_block->m_next_block; while (mem_block != nullptr) { MemoryBlock* old_block = mem_block; mem_block = mem_block->m_next_block; if (old_block->m_free) { delete old_block; } else { LOGE("Last occupied block wasn't last occupied block."); } } last_occupied_block->m_next_block = nullptr; if (last_occupied_block->m_offset + last_occupied_block->m_size < new_size) { MemoryBlock* new_block = new MemoryBlock; ZeroMemory(new_block, sizeof(MemoryBlock)); new_block->m_alignment = 1; new_block->m_free = true; new_block->m_next_block = nullptr; new_block->m_prev_block = last_occupied_block; new_block->m_offset = last_occupied_block->m_offset + last_occupied_block->m_size; new_block->m_size = new_size - new_block->m_offset; last_occupied_block->m_next_block = new_block; } else if (last_occupied_block->m_offset + last_occupied_block->m_size > new_size) { LOGE("Vertex buffer has shrunken too much and doesn't fit."); } m_updated = true; } void D3D12ModelPool::ShrinkIndexHeapToFit() { MemoryBlock* last_occupied_block = nullptr; for (MemoryBlock* mem_block = m_index_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block) { if (mem_block->m_free == false) { last_occupied_block = mem_block; } } if (last_occupied_block == nullptr) { LOGW("You're trying to shrink an empty index heap, returning instead."); } size_t new_size = last_occupied_block->m_offset + last_occupied_block->m_size; new_size = SizeAlignAnyAlignment(new_size, 65536); if (new_size == m_index_buffer->m_size) { return; } ID3D12Resource* old_buffer = m_index_buffer->m_buffer; ID3D12Resource* new_buffer; ID3D12Resource* old_staging = m_index_buffer->m_staging; ID3D12Resource* new_staging; uint8_t* cpu_address; CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(new_size); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_upload, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&new_staging)); NAME_D3D12RESOURCE(new_staging); CD3DX12_RANGE read_range(0, new_size); new_staging->Map(0, &read_range, reinterpret_cast(&(cpu_address))); memcpy(cpu_address, m_index_buffer->m_cpu_address, new_size); m_index_buffer->m_size = static_cast(new_size); m_index_buffer->m_is_staged = true; m_index_buffer->m_cpu_address = cpu_address; m_render_system.WaitForAllPreviousWork(); SAFE_RELEASE(m_index_buffer->m_buffer); SAFE_RELEASE(m_index_buffer->m_staging); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &buffer_desc, static_cast(m_index_buffer->m_target_resource_state), nullptr, IID_PPV_ARGS(&new_buffer)); NAME_D3D12RESOURCE(new_buffer); m_index_buffer->m_buffer = new_buffer; m_index_buffer->m_staging = new_staging; m_index_buffer->m_gpu_address = new_buffer->GetGPUVirtualAddress(); for (int i = 0; i < m_command_queue.size(); ++i) { internal::Command* command = m_command_queue.front(); m_command_queue.pop(); m_command_queue.push(command); switch (command->m_type) { case internal::CommandType::COPY: { internal::CopyCommand* copy_command = static_cast(command); if (copy_command->m_dest == old_staging) { copy_command->m_dest = new_staging; } if (copy_command->m_dest == old_buffer) { copy_command->m_dest = new_buffer; } if (copy_command->m_source == old_staging) { copy_command->m_source = new_staging; } if (copy_command->m_source == old_buffer) { copy_command->m_source = new_buffer; } } break; case internal::CommandType::TRANSITION: { internal::TransitionCommand* transition_command = static_cast(command); if (transition_command->m_buffer == old_staging) { transition_command->m_buffer = new_staging; } if (transition_command->m_buffer == old_buffer) { transition_command->m_buffer = new_buffer; } } break; } } internal::StageCommand* stageCommand = new internal::StageCommand; stageCommand->m_type = internal::STAGE; stageCommand->m_buffer = m_index_buffer; stageCommand->m_offset = 0; stageCommand->m_size = new_size; m_command_queue.push(stageCommand); MemoryBlock* mem_block = last_occupied_block->m_next_block; while (mem_block != nullptr) { MemoryBlock* old_block = mem_block; mem_block = mem_block->m_next_block; if (old_block->m_free) { delete old_block; } else { LOGE("Last occupied block wasn't last occupied block."); } } last_occupied_block->m_next_block = nullptr; if (last_occupied_block->m_offset + last_occupied_block->m_size < new_size) { MemoryBlock* new_block = new MemoryBlock; ZeroMemory(new_block, sizeof(MemoryBlock)); new_block->m_alignment = 1; new_block->m_free = true; new_block->m_next_block = nullptr; new_block->m_prev_block = last_occupied_block; new_block->m_offset = last_occupied_block->m_offset + last_occupied_block->m_size; new_block->m_size = new_size - new_block->m_offset; last_occupied_block->m_next_block = new_block; } else if (last_occupied_block->m_offset + last_occupied_block->m_size > new_size) { LOGE("Vertex buffer has shrunken too much and doesn't fit."); } m_updated = true; } void D3D12ModelPool::Defragment() { DefragmentVertexHeap(); DefragmentIndexHeap(); } void D3D12ModelPool::DefragmentVertexHeap() { { internal::TransitionCommand* transition_command = new internal::TransitionCommand; transition_command->m_type = internal::CommandType::TRANSITION; transition_command->m_buffer = m_vertex_buffer->m_buffer; if (m_vertex_buffer->m_is_staged) { transition_command->m_old_state = m_vertex_buffer->m_target_resource_state; } else { transition_command->m_old_state = ResourceState::COPY_DEST; } transition_command->m_new_state = ResourceState::COPY_SOURCE; m_command_queue.push(transition_command); } MemoryBlock* largest_block = m_vertex_heap_start_block; for (MemoryBlock* mem_block = m_vertex_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block) { if (!mem_block->m_free) { if (largest_block->m_free || mem_block->m_size > largest_block->m_size) { largest_block = mem_block; } } } if (!largest_block->m_free) { if (largest_block->m_size > m_intermediate_size) { ID3D12Resource* buffer; CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(SizeAlignAnyAlignment(largest_block->m_size, 65536)); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&buffer)); buffer->SetName(L"Memory pool intermediate buffer"); m_render_system.WaitForAllPreviousWork(); for (int i = 0; i < m_command_queue.size(); ++i) { internal::Command* command = m_command_queue.front(); m_command_queue.pop(); m_command_queue.push(command); switch (command->m_type) { case internal::CommandType::COPY: { internal::CopyCommand* copy_command = static_cast(command); if (copy_command->m_dest == m_intermediate_buffer) { copy_command->m_dest = buffer; } if (copy_command->m_source == m_intermediate_buffer) { copy_command->m_source = buffer; } } break; case internal::CommandType::TRANSITION: { internal::TransitionCommand* transition_command = static_cast(command); if (transition_command->m_buffer == m_intermediate_buffer) { transition_command->m_buffer = buffer; } } break; } } SAFE_RELEASE(m_intermediate_buffer); m_intermediate_buffer = buffer; m_intermediate_size = SizeAlignAnyAlignment(largest_block->m_size, 65536); } } bool contents_changed = false; MemoryBlock* mem_block = m_vertex_heap_start_block; while (mem_block->m_next_block != nullptr) { if (mem_block->m_free) { if (mem_block->m_next_block->m_free) { mem_block->m_size += mem_block->m_next_block->m_size; if (mem_block->m_next_block->m_next_block != nullptr) { mem_block->m_next_block = mem_block->m_next_block->m_next_block; delete mem_block->m_next_block->m_prev_block; mem_block->m_next_block->m_prev_block = mem_block; } else { delete mem_block->m_next_block; mem_block->m_next_block = nullptr; } } else { MemoryBlock* next_block = mem_block->m_next_block; size_t original_offset = next_block->m_offset; if (mem_block->m_prev_block != nullptr) { mem_block->m_prev_block->m_next_block = mem_block->m_next_block; next_block->m_prev_block = mem_block->m_prev_block; } else { m_vertex_heap_start_block = next_block; next_block->m_prev_block = nullptr; } if (next_block->m_next_block != nullptr) { next_block->m_next_block->m_prev_block = mem_block; mem_block->m_next_block = next_block->m_next_block; } else { mem_block->m_next_block = nullptr; } next_block->m_next_block = mem_block; mem_block->m_prev_block = next_block; next_block->m_offset = SizeAlignAnyAlignment(mem_block->m_offset, next_block->m_alignment); if (next_block->m_offset%next_block->m_alignment != 0) { LOGW("Wrong alignment after trying to align"); } if (next_block->m_prev_block != nullptr) { next_block->m_size += SizeAlignAnyAlignment(next_block->m_offset - mem_block->m_offset, next_block->m_alignment); } mem_block->m_size -= SizeAlignAnyAlignment(next_block->m_offset - mem_block->m_offset, next_block->m_alignment); mem_block->m_offset = next_block->m_offset + next_block->m_size; std::map::iterator it = m_loaded_meshes.begin(); for (; it != m_loaded_meshes.end(); ++it) { internal::D3D12MeshInternal* mesh = static_cast((*it).second); if (mesh->m_vertex_memory_block == next_block) { mesh->m_vertex_staging_buffer_offset = next_block->m_offset / next_block->m_alignment; if (next_block->m_offset%next_block->m_alignment != 0) { LOGW("Wrong alignment"); } } } internal::CopyCommand* copy_to_intermediate_command = new internal::CopyCommand; copy_to_intermediate_command->m_type = internal::CommandType::COPY; copy_to_intermediate_command->m_source = m_vertex_buffer->m_buffer; copy_to_intermediate_command->m_dest = m_intermediate_buffer; copy_to_intermediate_command->m_source_offset = original_offset; copy_to_intermediate_command->m_dest_offset = 0; copy_to_intermediate_command->m_size = next_block->m_size; m_command_queue.push(copy_to_intermediate_command); internal::TransitionCommand* transition_intermediate_read_command = new internal::TransitionCommand; transition_intermediate_read_command->m_type = internal::TRANSITION; transition_intermediate_read_command->m_buffer = m_intermediate_buffer; transition_intermediate_read_command->m_old_state = ResourceState::COPY_DEST; transition_intermediate_read_command->m_new_state = ResourceState::COPY_SOURCE; m_command_queue.push(transition_intermediate_read_command); internal::TransitionCommand* transition_buffer_write_command = new internal::TransitionCommand; transition_buffer_write_command->m_type = internal::TRANSITION; transition_buffer_write_command->m_buffer = m_vertex_buffer->m_buffer; transition_buffer_write_command->m_old_state = ResourceState::COPY_SOURCE; transition_buffer_write_command->m_new_state = ResourceState::COPY_DEST; m_command_queue.push(transition_buffer_write_command); internal::CopyCommand* copy_to_buffer_command = new internal::CopyCommand; copy_to_buffer_command->m_type = internal::COPY; copy_to_buffer_command->m_source = m_intermediate_buffer; copy_to_buffer_command->m_source_offset = 0; copy_to_buffer_command->m_dest = m_vertex_buffer->m_buffer; copy_to_buffer_command->m_dest_offset = next_block->m_offset; copy_to_buffer_command->m_size = next_block->m_size; m_command_queue.push(copy_to_buffer_command); internal::TransitionCommand* transition_intermediate_write_command = new internal::TransitionCommand; transition_intermediate_write_command->m_type = internal::TRANSITION; transition_intermediate_write_command->m_buffer = m_intermediate_buffer; transition_intermediate_write_command->m_old_state = ResourceState::COPY_SOURCE; transition_intermediate_write_command->m_new_state = ResourceState::COPY_DEST; m_command_queue.push(transition_intermediate_write_command); internal::TransitionCommand* transition_buffer_read_command = new internal::TransitionCommand; transition_buffer_read_command->m_type = internal::TRANSITION; transition_buffer_read_command->m_buffer = m_vertex_buffer->m_buffer; transition_buffer_read_command->m_old_state = ResourceState::COPY_DEST; transition_buffer_read_command->m_new_state = ResourceState::COPY_SOURCE; m_command_queue.push(transition_buffer_read_command); memcpy(m_vertex_buffer->m_cpu_address + next_block->m_offset, m_vertex_buffer->m_cpu_address + original_offset, next_block->m_size); contents_changed = true; } } else { mem_block = mem_block->m_next_block; } } { internal::TransitionCommand* transition_command = new internal::TransitionCommand; transition_command->m_type = internal::CommandType::TRANSITION; transition_command->m_buffer = m_vertex_buffer->m_buffer; if (m_vertex_buffer->m_is_staged) { transition_command->m_new_state = m_vertex_buffer->m_target_resource_state; } else { transition_command->m_new_state = ResourceState::COPY_DEST; } transition_command->m_old_state = ResourceState::COPY_SOURCE; m_command_queue.push(transition_command); } if (contents_changed) { m_updated = true; } } void D3D12ModelPool::DefragmentIndexHeap() { { internal::TransitionCommand* transition_command = new internal::TransitionCommand; transition_command->m_type = internal::CommandType::TRANSITION; transition_command->m_buffer = m_index_buffer->m_buffer; if (m_index_buffer->m_is_staged) { transition_command->m_old_state = m_index_buffer->m_target_resource_state; } else { transition_command->m_old_state = ResourceState::COPY_DEST; } transition_command->m_new_state = ResourceState::COPY_SOURCE; m_command_queue.push(transition_command); } MemoryBlock* largest_block = m_index_heap_start_block; for (MemoryBlock* mem_block = m_index_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block) { if (!mem_block->m_free) { if (largest_block->m_free || mem_block->m_size > largest_block->m_size) { largest_block = mem_block; } } } if (!largest_block->m_free) { if (largest_block->m_size > m_intermediate_size) { ID3D12Resource* buffer; CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(SizeAlignAnyAlignment(largest_block->m_size, 65536)); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&buffer)); buffer->SetName(L"Memory pool intermediate buffer"); m_render_system.WaitForAllPreviousWork(); for (int i = 0; i < m_command_queue.size(); ++i) { internal::Command* command = m_command_queue.front(); m_command_queue.pop(); m_command_queue.push(command); switch (command->m_type) { case internal::CommandType::COPY: { internal::CopyCommand* copy_command = static_cast(command); if (copy_command->m_dest == m_intermediate_buffer) { copy_command->m_dest = buffer; } if (copy_command->m_source == m_intermediate_buffer) { copy_command->m_source = buffer; } } break; case internal::CommandType::TRANSITION: { internal::TransitionCommand* transition_command = static_cast(command); if (transition_command->m_buffer == m_intermediate_buffer) { transition_command->m_buffer = buffer; } } break; } } SAFE_RELEASE(m_intermediate_buffer); m_intermediate_buffer = buffer; m_intermediate_size = SizeAlignAnyAlignment(largest_block->m_size, 65536); } } bool contents_changed = false; MemoryBlock* mem_block = m_index_heap_start_block; while (mem_block->m_next_block != nullptr) { if (mem_block->m_free) { if (mem_block->m_next_block->m_free) { mem_block->m_size += mem_block->m_next_block->m_size; if (mem_block->m_next_block->m_next_block != nullptr) { mem_block->m_next_block = mem_block->m_next_block->m_next_block; delete mem_block->m_next_block->m_prev_block; mem_block->m_next_block->m_prev_block = mem_block; } else { delete mem_block->m_next_block; mem_block->m_next_block = nullptr; } } else { MemoryBlock* next_block = mem_block->m_next_block; size_t original_offset = next_block->m_offset; if (mem_block->m_prev_block != nullptr) { mem_block->m_prev_block->m_next_block = mem_block->m_next_block; next_block->m_prev_block = mem_block->m_prev_block; } else { m_index_heap_start_block = next_block; next_block->m_prev_block = nullptr; } if (next_block->m_next_block != nullptr) { next_block->m_next_block->m_prev_block = mem_block; mem_block->m_next_block = next_block->m_next_block; } else { mem_block->m_next_block = nullptr; } next_block->m_next_block = mem_block; mem_block->m_prev_block = next_block; next_block->m_offset = SizeAlignAnyAlignment(mem_block->m_offset, next_block->m_alignment); if (next_block->m_prev_block != nullptr) { next_block->m_size += next_block->m_offset - mem_block->m_offset; } mem_block->m_size -= next_block->m_offset - mem_block->m_offset; mem_block->m_offset = next_block->m_offset + next_block->m_size; std::map::iterator it = m_loaded_meshes.begin(); for (; it != m_loaded_meshes.end(); ++it) { internal::D3D12MeshInternal* mesh = static_cast((*it).second); if (mesh->m_index_memory_block == next_block) { mesh->m_index_staging_buffer_offset = SizeAlignAnyAlignment(next_block->m_offset, next_block->m_alignment) / next_block->m_alignment; } } internal::CopyCommand* copy_to_intermediate_command = new internal::CopyCommand; copy_to_intermediate_command->m_type = internal::CommandType::COPY; copy_to_intermediate_command->m_source = m_index_buffer->m_buffer; copy_to_intermediate_command->m_dest = m_intermediate_buffer; copy_to_intermediate_command->m_source_offset = original_offset; copy_to_intermediate_command->m_dest_offset = 0; copy_to_intermediate_command->m_size = next_block->m_size; m_command_queue.push(copy_to_intermediate_command); internal::TransitionCommand* transition_intermediate_read_command = new internal::TransitionCommand; transition_intermediate_read_command->m_type = internal::TRANSITION; transition_intermediate_read_command->m_buffer = m_intermediate_buffer; transition_intermediate_read_command->m_old_state = ResourceState::COPY_DEST; transition_intermediate_read_command->m_new_state = ResourceState::COPY_SOURCE; m_command_queue.push(transition_intermediate_read_command); internal::TransitionCommand* transition_buffer_write_command = new internal::TransitionCommand; transition_buffer_write_command->m_type = internal::TRANSITION; transition_buffer_write_command->m_buffer = m_index_buffer->m_buffer; transition_buffer_write_command->m_old_state = ResourceState::COPY_SOURCE; transition_buffer_write_command->m_new_state = ResourceState::COPY_DEST; m_command_queue.push(transition_buffer_write_command); internal::CopyCommand* copy_to_buffer_command = new internal::CopyCommand; copy_to_buffer_command->m_type = internal::COPY; copy_to_buffer_command->m_source = m_intermediate_buffer; copy_to_buffer_command->m_source_offset = 0; copy_to_buffer_command->m_dest = m_index_buffer->m_buffer; copy_to_buffer_command->m_dest_offset = next_block->m_offset; copy_to_buffer_command->m_size = next_block->m_size; m_command_queue.push(copy_to_buffer_command); internal::TransitionCommand* transition_intermediate_write_command = new internal::TransitionCommand; transition_intermediate_write_command->m_type = internal::TRANSITION; transition_intermediate_write_command->m_buffer = m_intermediate_buffer; transition_intermediate_write_command->m_old_state = ResourceState::COPY_SOURCE; transition_intermediate_write_command->m_new_state = ResourceState::COPY_DEST; m_command_queue.push(transition_intermediate_write_command); internal::TransitionCommand* transition_buffer_read_command = new internal::TransitionCommand; transition_buffer_read_command->m_type = internal::TRANSITION; transition_buffer_read_command->m_buffer = m_index_buffer->m_buffer; transition_buffer_read_command->m_old_state = ResourceState::COPY_DEST; transition_buffer_read_command->m_new_state = ResourceState::COPY_SOURCE; m_command_queue.push(transition_buffer_read_command); memcpy(m_index_buffer->m_cpu_address + next_block->m_offset, m_index_buffer->m_cpu_address + original_offset, next_block->m_size); contents_changed = true; } } else { mem_block = mem_block->m_next_block; } } { internal::TransitionCommand* transition_command = new internal::TransitionCommand; transition_command->m_type = internal::CommandType::TRANSITION; transition_command->m_buffer = m_index_buffer->m_buffer; if (m_index_buffer->m_is_staged) { transition_command->m_new_state = m_index_buffer->m_target_resource_state; } else { transition_command->m_new_state = ResourceState::COPY_DEST; } transition_command->m_old_state = ResourceState::COPY_SOURCE; m_command_queue.push(transition_command); } if (contents_changed) { m_updated = true; } } void D3D12ModelPool::Resize(size_t vertex_heap_new_size, size_t index_heap_new_size) { ResizeVertexHeap(vertex_heap_new_size); ResizeIndexHeap(index_heap_new_size); } void D3D12ModelPool::ResizeVertexHeap(size_t vertex_heap_new_size) { MemoryBlock* mem_block = m_vertex_heap_start_block; while (mem_block->m_next_block != nullptr) { if (mem_block->m_free) { if (mem_block->m_next_block->m_free) { mem_block->m_size += mem_block->m_next_block->m_size; if (mem_block->m_next_block->m_next_block != nullptr) { mem_block->m_next_block = mem_block->m_next_block->m_next_block; delete mem_block->m_next_block->m_prev_block; mem_block->m_next_block->m_prev_block = mem_block; } else { delete mem_block->m_next_block; mem_block->m_next_block = nullptr; } } else { mem_block = mem_block->m_next_block; } } else { mem_block = mem_block->m_next_block; } } MemoryBlock* last_occupied_block = nullptr; for (mem_block = m_vertex_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block) { if (mem_block->m_free == false) { last_occupied_block = mem_block; } } size_t new_size; if (last_occupied_block != nullptr) { new_size = last_occupied_block->m_offset + last_occupied_block->m_size; } else { new_size = 0; } size_t old_size = m_vertex_buffer->m_size; if (new_size > SizeAlignAnyAlignment(vertex_heap_new_size, 65536)) { ShrinkVertexHeapToFit(); } else if (SizeAlignAnyAlignment(vertex_heap_new_size, 65536) == m_vertex_buffer->m_size) { return; } else { new_size = SizeAlignAnyAlignment(vertex_heap_new_size, 65536); ID3D12Resource* old_buffer = m_vertex_buffer->m_buffer; ID3D12Resource* new_buffer; ID3D12Resource* old_staging = m_vertex_buffer->m_staging; ID3D12Resource* new_staging; uint8_t* cpu_address; CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(new_size); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_upload, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&new_staging)); NAME_D3D12RESOURCE(new_staging); CD3DX12_RANGE read_range(0, new_size); new_staging->Map(0, &read_range, reinterpret_cast(&(cpu_address))); memcpy(cpu_address, m_vertex_buffer->m_cpu_address, std::min(new_size, m_vertex_buffer->m_size)); m_vertex_buffer->m_size = static_cast(new_size); m_vertex_buffer->m_is_staged = true; m_vertex_buffer->m_cpu_address = cpu_address; m_render_system.WaitForAllPreviousWork(); SAFE_RELEASE(m_vertex_buffer->m_buffer); SAFE_RELEASE(m_vertex_buffer->m_staging); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &buffer_desc, static_cast(m_vertex_buffer->m_target_resource_state), nullptr, IID_PPV_ARGS(&new_buffer)); NAME_D3D12RESOURCE(new_buffer); m_vertex_buffer->m_buffer = new_buffer; m_vertex_buffer->m_staging = new_staging; m_vertex_buffer->m_gpu_address = m_vertex_buffer->m_buffer->GetGPUVirtualAddress(); for (int i = 0; i < m_command_queue.size(); ++i) { internal::Command* command = m_command_queue.front(); m_command_queue.pop(); m_command_queue.push(command); switch (command->m_type) { case internal::CommandType::COPY: { internal::CopyCommand* copy_command = static_cast(command); if (copy_command->m_dest == old_staging) { copy_command->m_dest = new_staging; } if (copy_command->m_dest == old_buffer) { copy_command->m_dest = new_buffer; } if (copy_command->m_source == old_staging) { copy_command->m_source = new_staging; } if (copy_command->m_source == old_buffer) { copy_command->m_source = new_buffer; } } break; case internal::CommandType::TRANSITION: { internal::TransitionCommand* transition_command = static_cast(command); if (transition_command->m_buffer == old_staging) { transition_command->m_buffer = new_staging; } if (transition_command->m_buffer == old_buffer) { transition_command->m_buffer = new_buffer; } } break; } } internal::StageCommand* stageCommand = new internal::StageCommand; stageCommand->m_type = internal::STAGE; stageCommand->m_buffer = m_vertex_buffer; stageCommand->m_offset = 0; stageCommand->m_size = new_size; m_command_queue.push(stageCommand); if (last_occupied_block != nullptr) { if (last_occupied_block->m_offset + last_occupied_block->m_size < new_size) { if (last_occupied_block->m_next_block != nullptr) { last_occupied_block->m_next_block->m_size = new_size - (last_occupied_block->m_offset + last_occupied_block->m_size); } else { MemoryBlock* new_block = new MemoryBlock; last_occupied_block->m_next_block = new_block; new_block->m_prev_block = last_occupied_block; new_block->m_free = true; new_block->m_alignment = 1; new_block->m_offset = last_occupied_block->m_offset + last_occupied_block->m_size; new_block->m_next_block = nullptr; new_block->m_size = new_size - new_block->m_offset; } } } else { m_vertex_heap_start_block->m_size = new_size; } } m_updated = true; } void D3D12ModelPool::ResizeIndexHeap(size_t index_heap_new_size) { MemoryBlock* mem_block = m_index_heap_start_block; while (mem_block->m_next_block != nullptr) { if (mem_block->m_free) { if (mem_block->m_next_block->m_free) { mem_block->m_size += mem_block->m_next_block->m_size; if (mem_block->m_next_block->m_next_block != nullptr) { mem_block->m_next_block = mem_block->m_next_block->m_next_block; delete mem_block->m_next_block->m_prev_block; mem_block->m_next_block->m_prev_block = mem_block; } else { delete mem_block->m_next_block; mem_block->m_next_block = nullptr; } } else { mem_block = mem_block->m_next_block; } } else { mem_block = mem_block->m_next_block; } } MemoryBlock* last_occupied_block = nullptr; for (mem_block = m_index_heap_start_block; mem_block != nullptr; mem_block = mem_block->m_next_block) { if (mem_block->m_free == false) { last_occupied_block = mem_block; } } size_t new_size; if (last_occupied_block != nullptr) { new_size = last_occupied_block->m_offset + last_occupied_block->m_size; } else { new_size = 0; } if (new_size > SizeAlignAnyAlignment(index_heap_new_size, 65536)) { ShrinkIndexHeapToFit(); } else if (SizeAlignAnyAlignment(index_heap_new_size, 65536) == m_index_buffer->m_size) { return; } else { new_size = SizeAlignAnyAlignment(index_heap_new_size, 65536); ID3D12Resource* old_buffer = m_index_buffer->m_buffer; ID3D12Resource* new_buffer; ID3D12Resource* old_staging = m_index_buffer->m_staging; ID3D12Resource* new_staging; uint8_t* cpu_address; CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(new_size); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_upload, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&new_staging)); NAME_D3D12RESOURCE(new_staging); CD3DX12_RANGE read_range(0, new_size); new_staging->Map(0, &read_range, reinterpret_cast(&(cpu_address))); memcpy(cpu_address, m_index_buffer->m_cpu_address, std::min(new_size, m_index_buffer->m_size)); m_index_buffer->m_size = static_cast(new_size); m_index_buffer->m_is_staged = true; m_index_buffer->m_cpu_address = cpu_address; m_render_system.WaitForAllPreviousWork(); SAFE_RELEASE(m_index_buffer->m_buffer); SAFE_RELEASE(m_index_buffer->m_staging); m_render_system.m_device->m_native->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &buffer_desc, static_cast(m_index_buffer->m_target_resource_state), nullptr, IID_PPV_ARGS(&new_buffer)); NAME_D3D12RESOURCE(new_buffer); m_index_buffer->m_buffer = new_buffer; m_index_buffer->m_staging = new_staging; m_index_buffer->m_gpu_address = m_index_buffer->m_buffer->GetGPUVirtualAddress(); for (int i = 0; i < m_command_queue.size(); ++i) { internal::Command* command = m_command_queue.front(); m_command_queue.pop(); m_command_queue.push(command); switch (command->m_type) { case internal::CommandType::COPY: { internal::CopyCommand* copy_command = static_cast(command); if (copy_command->m_dest == old_staging) { copy_command->m_dest = new_staging; } if (copy_command->m_dest == old_buffer) { copy_command->m_dest = new_buffer; } if (copy_command->m_source == old_staging) { copy_command->m_source = new_staging; } if (copy_command->m_source == old_buffer) { copy_command->m_source = new_buffer; } } break; case internal::CommandType::TRANSITION: { internal::TransitionCommand* transition_command = static_cast(command); if (transition_command->m_buffer == old_staging) { transition_command->m_buffer = new_staging; } if (transition_command->m_buffer == old_buffer) { transition_command->m_buffer = new_buffer; } } break; } } internal::StageCommand* stageCommand = new internal::StageCommand; stageCommand->m_type = internal::STAGE; stageCommand->m_buffer = m_index_buffer; stageCommand->m_offset = 0; stageCommand->m_size = new_size; m_command_queue.push(stageCommand); if (last_occupied_block != nullptr) { if (last_occupied_block->m_offset + last_occupied_block->m_size < new_size) { if (last_occupied_block->m_next_block != nullptr) { last_occupied_block->m_next_block->m_size = new_size - (last_occupied_block->m_offset + last_occupied_block->m_size); } else { MemoryBlock* new_block = new MemoryBlock; last_occupied_block->m_next_block = new_block; new_block->m_prev_block = last_occupied_block; new_block->m_free = true; new_block->m_alignment = 1; new_block->m_offset = last_occupied_block->m_offset + last_occupied_block->m_size; new_block->m_next_block = nullptr; new_block->m_size = new_size - new_block->m_offset; } } } else { m_index_heap_start_block->m_size = new_size; } } m_updated = true; } void D3D12ModelPool::MakeSpaceForModel(size_t vertex_size, size_t index_size) { if (GetVertexHeapFreeSpace() < vertex_size) { ResizeVertexHeap(vertex_size + GetVertexHeapOccupiedSpace()); } if (GetIndexHeapFreeSpace() < index_size) { ResizeIndexHeap(index_size + GetIndexHeapOccupiedSpace()); } } internal::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) { internal::D3D12MeshInternal* mesh = new internal::D3D12MeshInternal(); memset(mesh, 0, sizeof(internal::D3D12MeshInternal)); //Allocate vertex buffer memory // Find Free Page MemoryBlock* vertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size); // Check if we found a page. if (vertex_memory_block == nullptr) { LOGW("Allocating memory for vertex buffer failed, trying to defragment."); DefragmentVertexHeap(); vertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size); if (vertex_memory_block == nullptr) { LOGW("Defragmenting failed, allocating more memory."); ResizeVertexHeap(std::max(GetVertexHeapSize() + vertex_size * (num_vertices + 1), GetVertexHeapSize() * 2)); vertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size); if (vertex_memory_block == nullptr) { LOGE("Allocating memory for vertex buffer failed."); delete mesh; return nullptr; } } } //Repeat the same procedure as before, but now for the index buffer MemoryBlock* index_memory_block = AllocateMemory(m_index_heap_start_block, num_indices*index_size, index_size); // Check if we found a page. if (index_memory_block == nullptr) { LOGW("Allocating memory for index buffer failed, trying to defragment."); DefragmentIndexHeap(); index_memory_block = AllocateMemory(m_index_heap_start_block, num_indices*index_size, index_size); if (index_memory_block == nullptr) { LOGW("Defragmenting failed, allocating more memory."); ResizeIndexHeap(std::max(GetIndexHeapSize() + index_size * (num_indices + 1), GetIndexHeapSize() * 2)); index_memory_block = AllocateMemory(m_index_heap_start_block, num_indices*index_size, index_size); if (index_memory_block == nullptr) { LOGE("Allocating memory for index buffer failed."); FreeMemory(m_vertex_heap_start_block, vertex_memory_block); delete mesh; return nullptr; } } } //Store the offset of the allocated memory from the start of the staging buffer mesh->m_vertex_staging_buffer_offset = SizeAlignAnyAlignment(vertex_memory_block->m_offset, vertex_size) / vertex_size; mesh->m_vertex_staging_buffer_size = num_vertices * vertex_size; mesh->m_vertex_staging_buffer_stride = vertex_size; mesh->m_vertex_count = num_vertices; mesh->m_vertex_memory_block = vertex_memory_block; mesh->m_vertex_buffer_base_address = m_vertex_buffer->m_gpu_address; //Send the vertex data to the vertex staging buffer d3d12::UpdateStagingBuffer(m_vertex_buffer, vertices_data, num_vertices*vertex_size, mesh->m_vertex_staging_buffer_offset * vertex_size); //Store the offset of the allocated memory from the start of the staging buffer mesh->m_index_staging_buffer_offset = SizeAlignAnyAlignment(index_memory_block->m_offset, index_size) / index_size; mesh->m_index_staging_buffer_size = num_indices * index_size; mesh->m_index_count = num_indices; mesh->m_index_memory_block = index_memory_block; mesh->m_index_buffer_base_address = m_index_buffer->m_gpu_address; //Send the index data to the index staging buffer d3d12::UpdateStagingBuffer(m_index_buffer, indices_data, num_indices*index_size, mesh->m_index_staging_buffer_offset * index_size); internal::StageCommand* vertex_command = new internal::StageCommand; vertex_command->m_type = internal::CommandType::STAGE; vertex_command->m_buffer = m_vertex_buffer; vertex_command->m_offset = mesh->m_vertex_staging_buffer_offset * mesh->m_vertex_staging_buffer_stride; vertex_command->m_size = mesh->m_vertex_staging_buffer_size; m_command_queue.push(vertex_command); internal::StageCommand* index_command = new internal::StageCommand; index_command->m_type = internal::CommandType::STAGE; index_command->m_buffer = m_index_buffer; index_command->m_offset = mesh->m_index_staging_buffer_offset * index_size; index_command->m_size = mesh->m_index_staging_buffer_size; m_command_queue.push(index_command); return mesh; } internal::MeshInternal* D3D12ModelPool::LoadCustom_VerticesOnly(void* vertices_data, std::size_t num_vertices, std::size_t vertex_size) { internal::D3D12MeshInternal* mesh = new internal::D3D12MeshInternal(); memset(mesh, 0, sizeof(internal::D3D12MeshInternal)); //Allocate vertex buffer memory MemoryBlock* vertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size); //The loop has exited, see if we've found enough free pages if (vertex_memory_block == nullptr) { DefragmentVertexHeap(); vertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size); if (vertex_memory_block == nullptr) { ResizeVertexHeap(std::max(GetVertexHeapSize() + (num_vertices + 1) * vertex_size, GetVertexHeapSize() * 2)); vertex_memory_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size); if (vertex_memory_block == nullptr) { //We haven't found enough pages, so delete the mesh and return a nullptr LOGE("Allocating memory for vertex buffer failed."); delete mesh; return nullptr; } } } //Store the offset of the allocated memory from the start of the staging buffer mesh->m_vertex_staging_buffer_offset = SizeAlignAnyAlignment(vertex_memory_block->m_offset, vertex_size) / vertex_size; mesh->m_vertex_staging_buffer_size = num_vertices * vertex_size; mesh->m_vertex_staging_buffer_stride = vertex_size; mesh->m_vertex_count = num_vertices; mesh->m_vertex_memory_block = vertex_memory_block; mesh->m_vertex_buffer_base_address = m_vertex_buffer->m_gpu_address; //Send the vertex data to the vertex staging buffer d3d12::UpdateStagingBuffer(m_vertex_buffer, vertices_data, num_vertices*vertex_size, mesh->m_vertex_staging_buffer_offset*vertex_size); internal::StageCommand* vertex_command = new internal::StageCommand; vertex_command->m_type = internal::CommandType::STAGE; vertex_command->m_buffer = m_vertex_buffer; vertex_command->m_offset = mesh->m_vertex_staging_buffer_offset * mesh->m_vertex_staging_buffer_stride; vertex_command->m_size = mesh->m_vertex_staging_buffer_size; m_command_queue.push(vertex_command); return mesh; } void 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) { UpdateMeshVertexData(mesh, vertices_data, num_vertices, vertex_size); UpdateMeshIndexData(mesh, indices_data, num_indices, index_size); } void D3D12ModelPool::UpdateMeshVertexData(Mesh * mesh, void * vertices_data, std::size_t num_vertices, std::size_t vertex_size) { internal::D3D12MeshInternal* mesh_data = GetMeshData(mesh->id); if (vertex_size == mesh_data->m_vertex_staging_buffer_stride&&num_vertices == mesh_data->m_vertex_count) { d3d12::UpdateStagingBuffer(m_vertex_buffer, vertices_data, num_vertices*vertex_size, mesh_data->m_vertex_staging_buffer_offset*vertex_size); internal::StageCommand* vertex_command = new internal::StageCommand; vertex_command->m_type = internal::STAGE; vertex_command->m_buffer = m_vertex_buffer; vertex_command->m_offset = mesh_data->m_vertex_staging_buffer_offset*vertex_size; vertex_command->m_size = mesh_data->m_vertex_staging_buffer_size; m_command_queue.push(vertex_command); } else { if (num_vertices*vertex_size <= mesh_data->m_vertex_staging_buffer_size) { LOGW("New vertex count is smaller than old vertex count."); } else { LOGW("New vertex count is larger than old vertex count."); } FreeMemory(m_vertex_heap_start_block, static_cast(mesh_data->m_vertex_memory_block)); MemoryBlock* new_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size); if (new_block == nullptr) { DefragmentVertexHeap(); new_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size); if (new_block == nullptr) { ResizeVertexHeap(GetVertexHeapSize() + (num_vertices + 1) * vertex_size); new_block = AllocateMemory(m_vertex_heap_start_block, num_vertices*vertex_size, vertex_size); if (new_block == nullptr) { LOGE("Unable to allocate memory for edited mesh."); } } } mesh_data->m_vertex_memory_block = new_block; mesh_data->m_vertex_staging_buffer_offset = SizeAlignAnyAlignment(new_block->m_offset, vertex_size) / vertex_size; mesh_data->m_vertex_staging_buffer_size = num_vertices * vertex_size; mesh_data->m_vertex_staging_buffer_stride = vertex_size; mesh_data->m_vertex_count = num_vertices; d3d12::UpdateStagingBuffer(m_vertex_buffer, vertices_data, num_vertices*vertex_size, mesh_data->m_vertex_staging_buffer_offset*vertex_size); internal::StageCommand* vertex_command = new internal::StageCommand; vertex_command->m_type = internal::STAGE; vertex_command->m_buffer = m_vertex_buffer; vertex_command->m_offset = mesh_data->m_vertex_staging_buffer_offset*vertex_size; vertex_command->m_size = mesh_data->m_vertex_staging_buffer_size; m_command_queue.push(vertex_command); } mesh_data->data_changed = true; } void D3D12ModelPool::UpdateMeshIndexData(Mesh * mesh, void * indices_data, std::size_t num_indices, std::size_t indices_size) { internal::D3D12MeshInternal* mesh_data = GetMeshData(mesh->id); if (num_indices == mesh_data->m_index_count) { d3d12::UpdateStagingBuffer(m_index_buffer, indices_data, num_indices*indices_size, mesh_data->m_index_staging_buffer_offset*indices_size); internal::StageCommand* index_command = new internal::StageCommand; index_command->m_type = internal::STAGE; index_command->m_buffer = m_index_buffer; index_command->m_offset = mesh_data->m_index_staging_buffer_offset*indices_size; index_command->m_size = mesh_data->m_index_staging_buffer_size; m_command_queue.push(index_command); } else { if (num_indices*indices_size <= mesh_data->m_index_staging_buffer_size) { LOGW("New index count is smaller than old index count."); } else { LOGW("New index count is larger than old index count."); } FreeMemory(m_index_heap_start_block, static_cast(mesh_data->m_index_memory_block)); MemoryBlock* new_block = AllocateMemory(m_index_heap_start_block, num_indices*indices_size, indices_size); if (new_block == nullptr) { DefragmentIndexHeap(); new_block = AllocateMemory(m_index_heap_start_block, num_indices*indices_size, indices_size); if (new_block == nullptr) { ResizeIndexHeap(GetVertexHeapSize() + (num_indices + 1) * indices_size); new_block = AllocateMemory(m_index_heap_start_block, num_indices*indices_size, indices_size); if (new_block == nullptr) { LOGE("Unable to allocate memory for edited mesh."); } } } mesh_data->m_index_memory_block = new_block; mesh_data->m_index_staging_buffer_offset = SizeAlignAnyAlignment(new_block->m_offset, indices_size) / indices_size; mesh_data->m_index_staging_buffer_size = num_indices * indices_size; mesh_data->m_index_count = num_indices; d3d12::UpdateStagingBuffer(m_index_buffer, indices_data, num_indices*indices_size, mesh_data->m_index_staging_buffer_offset*indices_size); internal::StageCommand* index_command = new internal::StageCommand; index_command->m_type = internal::STAGE; index_command->m_buffer = m_index_buffer; index_command->m_offset = mesh_data->m_index_staging_buffer_offset*indices_size; index_command->m_size = mesh_data->m_index_staging_buffer_size; m_command_queue.push(index_command); } mesh_data->data_changed = true; } void D3D12ModelPool::DestroyModel(Model * model) { for (auto& mesh : model->m_meshes) { DestroyMesh(m_loaded_meshes[mesh.first->id]); delete mesh.first; } std::vector::iterator it = m_loaded_models.begin(); for (; (*it) != model && it != m_loaded_models.end(); ++it); if (it != m_loaded_models.end()) { m_loaded_models.erase(it); } if(model->m_owns_materials) { for(auto& mesh : model->m_meshes) { mesh.second.m_pool->DestroyMaterial(mesh.second); } } delete model; } void D3D12ModelPool::DestroyMesh(internal::MeshInternal * mesh) { //Check for null pointers if (mesh == nullptr) { LOGW("Tried to destroy a mesh that was a nullptr") return; } std::map::iterator it; for (it = m_loaded_meshes.begin(); it != m_loaded_meshes.end(); ++it) { if (it->second == mesh) break; } if (it != m_loaded_meshes.end()) { FreeID(it->first); m_loaded_meshes.erase(it); internal::D3D12MeshInternal* n_mesh = static_cast(mesh); FreeMemory(m_vertex_heap_start_block, static_cast(n_mesh->m_vertex_memory_block)); if (n_mesh->m_index_memory_block != nullptr) { FreeMemory(m_index_heap_start_block, static_cast(n_mesh->m_index_memory_block)); } m_updated = true; //Delete the mesh delete mesh; } } D3D12ModelPool::MemoryBlock * D3D12ModelPool::AllocateMemory(MemoryBlock * start_block, std::size_t size, std::size_t alignment) { while (start_block != nullptr) { if (start_block->m_size >= size + (start_block->m_offset%alignment == 0 ? 0 : (alignment - start_block->m_offset%alignment)) && start_block->m_free) { std::size_t needed_size = size + (start_block->m_offset%alignment == 0 ? 0 : (alignment - start_block->m_offset%alignment)); if (start_block->m_size == needed_size) { start_block->m_free = false; start_block->m_alignment = alignment; return start_block; } else { MemoryBlock* new_block = new MemoryBlock; new_block->m_prev_block = start_block; new_block->m_next_block = start_block->m_next_block; start_block->m_next_block = new_block; new_block->m_free = true; new_block->m_size = start_block->m_size - needed_size; new_block->m_offset = start_block->m_offset + needed_size; if (new_block->m_next_block != nullptr) { new_block->m_next_block->m_prev_block = new_block; } start_block->m_free = false; start_block->m_size = needed_size; start_block->m_alignment = alignment; return start_block; } } else { start_block = start_block->m_next_block; } } return nullptr; } void D3D12ModelPool::FreeMemory(MemoryBlock * heap_start_block, MemoryBlock * block) { MemoryBlock* heap_block = heap_start_block; while (heap_block != nullptr) { if (heap_block == block) { if (heap_block->m_prev_block != nullptr) { if (heap_block->m_prev_block->m_free) { heap_block->m_prev_block->m_size += heap_block->m_size; if (heap_block->m_next_block != nullptr) { if (heap_block->m_next_block->m_free) { heap_block->m_prev_block->m_size += heap_block->m_next_block->m_size; heap_block->m_prev_block->m_next_block = heap_block->m_next_block->m_next_block; if (heap_block->m_prev_block->m_next_block != nullptr) { heap_block->m_prev_block->m_next_block->m_prev_block = heap_block->m_prev_block; } delete heap_block->m_next_block; delete heap_block; return; } else { heap_block->m_next_block->m_prev_block = heap_block->m_prev_block; heap_block->m_prev_block->m_next_block = heap_block->m_next_block; delete heap_block; return; } } else { heap_block->m_prev_block->m_next_block = heap_block->m_next_block; delete heap_block; return; } } else { if (heap_block->m_next_block != nullptr) { if (heap_block->m_next_block->m_free) { MemoryBlock* temp = heap_block->m_next_block; heap_block->m_size += heap_block->m_next_block->m_size; heap_block->m_next_block = heap_block->m_next_block->m_next_block; if (heap_block->m_next_block != nullptr) { heap_block->m_next_block->m_prev_block = heap_block; } delete temp; } else { heap_block->m_free = true; return; } } else { heap_block->m_free = true; return; } } } else { if (heap_block->m_next_block != nullptr) { if (heap_block->m_next_block->m_free) { MemoryBlock* temp = heap_block->m_next_block; heap_block->m_size += heap_block->m_next_block->m_size; heap_block->m_next_block = heap_block->m_next_block->m_next_block; if (heap_block->m_next_block != nullptr) { heap_block->m_next_block->m_prev_block = heap_block; } delete temp; } else { heap_block->m_free = true; return; } } else { heap_block->m_free = true; return; } } } else { heap_block = heap_block->m_next_block; } } } size_t D3D12ModelPool::GetVertexHeapOccupiedSpace() { MemoryBlock* mem_block = m_vertex_heap_start_block; size_t size = 0; while (mem_block != nullptr) { if (!mem_block->m_free) { size += mem_block->m_size; } mem_block = mem_block->m_next_block; } return size; } size_t D3D12ModelPool::GetIndexHeapOccupiedSpace() { MemoryBlock* mem_block = m_index_heap_start_block; size_t size = 0; while (mem_block != nullptr) { if (!mem_block->m_free) { size += mem_block->m_size; } mem_block = mem_block->m_next_block; } return size; } size_t D3D12ModelPool::GetVertexHeapFreeSpace() { MemoryBlock* mem_block = m_vertex_heap_start_block; size_t size = 0; while (mem_block != nullptr) { if (mem_block->m_free) { size += mem_block->m_size; } mem_block = mem_block->m_next_block; } return size; } size_t D3D12ModelPool::GetIndexHeapFreeSpace() { MemoryBlock* mem_block = m_index_heap_start_block; size_t size = 0; while (mem_block != nullptr) { if (mem_block->m_free) { size += mem_block->m_size; } mem_block = mem_block->m_next_block; } return size; } size_t D3D12ModelPool::GetVertexHeapSize() { MemoryBlock* mem_block = m_vertex_heap_start_block; size_t size = 0; while (mem_block != nullptr) { size += mem_block->m_size; mem_block = mem_block->m_next_block; } return size; } size_t D3D12ModelPool::GetIndexHeapSize() { MemoryBlock* mem_block = m_index_heap_start_block; size_t size = 0; while (mem_block != nullptr) { size += mem_block->m_size; mem_block = mem_block->m_next_block; } return size; } } /* wr */ ================================================ FILE: src/d3d12/d3d12_model_pool.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../model_pool.hpp" #include "d3d12_structs.hpp" #include namespace wr::d3d12 { struct HeapResource; struct StagingBuffer; } namespace wr { class D3D12RenderSystem; namespace internal { struct D3D12MeshInternal : MeshInternal { D3D12_GPU_VIRTUAL_ADDRESS m_vertex_buffer_base_address; std::size_t m_vertex_staging_buffer_offset; std::size_t m_vertex_staging_buffer_size; std::size_t m_vertex_staging_buffer_stride; std::size_t m_vertex_count; void* m_vertex_memory_block; D3D12_GPU_VIRTUAL_ADDRESS m_index_buffer_base_address; std::size_t m_index_staging_buffer_offset; std::size_t m_index_count_offset; std::size_t m_index_staging_buffer_size; std::size_t m_index_count; void* m_index_memory_block; bool data_changed; }; enum CommandType { STAGE, COPY, READ, TRANSITION, }; struct Command { CommandType m_type; }; struct StageCommand : Command { d3d12::StagingBuffer* m_buffer; std::size_t m_size; std::size_t m_offset; }; struct CopyCommand : Command { ID3D12Resource* m_source; ID3D12Resource* m_dest; std::size_t m_size; std::size_t m_source_offset; std::size_t m_dest_offset; }; struct ReadCommand : Command { d3d12::StagingBuffer* m_buffer; std::size_t m_size; std::size_t m_offset; }; struct TransitionCommand : Command { ID3D12Resource* m_buffer; ResourceState m_old_state; ResourceState m_new_state; }; } class D3D12ModelPool : public ModelPool { public: explicit D3D12ModelPool(D3D12RenderSystem& render_system, std::size_t vertex_buffer_pool_size_in_mb, std::size_t index_buffer_pool_size_in_mb); ~D3D12ModelPool() final; void Evict() final; void MakeResident() final; void StageMeshes(d3d12::CommandList* cmd_list); d3d12::StagingBuffer* GetVertexStagingBuffer(); d3d12::StagingBuffer* GetIndexStagingBuffer(); internal::D3D12MeshInternal* GetMeshData(std::uint64_t mesh_handle); // Shrinks down both heaps to the minimum size required. // Does not rearrange the contents of the heaps, meaning that it doesn't shrink to the absolute minimum size. // To do that, call Defragment first. void ShrinkToFit() final; void ShrinkVertexHeapToFit() final; void ShrinkIndexHeapToFit() final; // Removes any holes in the memory, stitching all allocations back together to maximize the amount of contiguous free space. // These functions are called automatically if the allocator has enough free space but no large enough free blocks. void Defragment() final; void DefragmentVertexHeap() final; void DefragmentIndexHeap() final; size_t GetVertexHeapOccupiedSpace() final; size_t GetIndexHeapOccupiedSpace() final; size_t GetVertexHeapFreeSpace() final; size_t GetIndexHeapFreeSpace() final; size_t GetVertexHeapSize() final; size_t GetIndexHeapSize() final; // Resizes both heaps to the supplied sizes. // If the supplied size is smaller than the required size the heaps will resize to the required size instead. void Resize(size_t vertex_heap_new_size, size_t index_heap_new_size) final; void ResizeVertexHeap(size_t vertex_heap_new_size) final; void ResizeIndexHeap(size_t index_heap_new_size) final; // Returns if the model pool data has been changed. Use this to see if additional data needs to be altered. bool IsUpdated() { return m_updated; }; void SetUpdated(bool updated) { m_updated = updated; }; void MakeSpaceForModel(size_t vertex_size, size_t index_size) final; private: 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) final; internal::MeshInternal* LoadCustom_VerticesOnly(void* vertices_data, std::size_t num_vertices, std::size_t vertex_size) final; virtual 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; void UpdateMeshVertexData(Mesh* mesh, void* vertices_data, std::size_t num_vertices, std::size_t vertex_size); void UpdateMeshIndexData(Mesh* mesh, void* indices_data, std::size_t num_indices, std::size_t indices_size); void DestroyModel(Model* model) final; void DestroyMesh(internal::MeshInternal* mesh) final; d3d12::StagingBuffer* m_vertex_buffer; d3d12::StagingBuffer* m_index_buffer; struct MemoryBlock { MemoryBlock* m_prev_block; MemoryBlock* m_next_block; std::size_t m_size; std::size_t m_offset; std::size_t m_alignment; bool m_free; }; MemoryBlock* m_vertex_heap_start_block; MemoryBlock* m_index_heap_start_block; MemoryBlock* AllocateMemory(MemoryBlock* start_block, std::size_t size, std::size_t alignment); void FreeMemory(MemoryBlock* heap_start_block, MemoryBlock* block); std::queue m_command_queue; ID3D12Resource* m_intermediate_buffer; std::size_t m_intermediate_size; std::uint64_t m_vertex_buffer_size; std::uint64_t m_index_buffer_size; bool m_updated; D3D12RenderSystem& m_render_system; }; } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_pipeline_state.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include #include "d3d12_defines.hpp" namespace wr::d3d12 { namespace internal { [[nodiscard]] D3D12_GRAPHICS_PIPELINE_STATE_DESC GetGraphicsPipelineStateDescriptor(desc::PipelineStateDesc descriptor, std::vector const & input_layout, RootSignature* root_signature, Shader* vertex_shader, Shader* pixel_shader) { D3D12_BLEND_DESC blend_desc = CD3DX12_BLEND_DESC(D3D12_DEFAULT); D3D12_DEPTH_STENCIL_DESC depth_stencil_state = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT); if (descriptor.m_dsv_format == Format::UNKNOWN) { depth_stencil_state.DepthEnable = false; depth_stencil_state.StencilEnable = false; } D3D12_RASTERIZER_DESC rasterize_desc = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT); rasterize_desc.FrontCounterClockwise = descriptor.m_counter_clockwise; depth_stencil_state.DepthFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL; rasterize_desc.CullMode = (D3D12_CULL_MODE)descriptor.m_cull_mode; DXGI_SAMPLE_DESC sample_desc = { 1, 0 }; D3D12_INPUT_LAYOUT_DESC input_layout_desc = {}; input_layout_desc.NumElements = static_cast(input_layout.size()); input_layout_desc.pInputElementDescs = input_layout.data(); D3D12_SHADER_BYTECODE vs_bytecode = {}; vs_bytecode.BytecodeLength = vertex_shader->m_native->GetBufferSize(); vs_bytecode.pShaderBytecode = vertex_shader->m_native->GetBufferPointer(); D3D12_SHADER_BYTECODE ps_bytecode = {}; ps_bytecode.BytecodeLength = pixel_shader->m_native->GetBufferSize(); ps_bytecode.pShaderBytecode = pixel_shader->m_native->GetBufferPointer(); D3D12_GRAPHICS_PIPELINE_STATE_DESC pso_desc = {}; pso_desc.PrimitiveTopologyType = (D3D12_PRIMITIVE_TOPOLOGY_TYPE)descriptor.m_topology_type; for (auto i = 0u; i < descriptor.m_num_rtv_formats; i++) { // FIXME: memcpy pso_desc.RTVFormats[i] = (DXGI_FORMAT)descriptor.m_rtv_formats[i]; } pso_desc.DSVFormat = (DXGI_FORMAT)descriptor.m_dsv_format; pso_desc.SampleDesc = sample_desc; pso_desc.SampleMask = 0xffffffff; pso_desc.RasterizerState = rasterize_desc; pso_desc.BlendState = blend_desc; pso_desc.DepthStencilState = depth_stencil_state; pso_desc.NumRenderTargets = descriptor.m_num_rtv_formats; pso_desc.pRootSignature = root_signature->m_native; pso_desc.VS = vs_bytecode; pso_desc.PS = ps_bytecode; pso_desc.InputLayout = input_layout_desc; return pso_desc; } [[nodiscard]] D3D12_COMPUTE_PIPELINE_STATE_DESC GetComputePipelineStateDescriptor(RootSignature* root_signature, Shader* compute_shader) { D3D12_SHADER_BYTECODE cs_bytecode = {}; cs_bytecode.BytecodeLength = compute_shader->m_native->GetBufferSize(); cs_bytecode.pShaderBytecode = compute_shader->m_native->GetBufferPointer(); D3D12_COMPUTE_PIPELINE_STATE_DESC pso_desc = {}; pso_desc.pRootSignature = root_signature->m_native; pso_desc.CS = cs_bytecode; return pso_desc; } } /* internal */ PipelineState* CreatePipelineState() { return new PipelineState(); } void SetName(PipelineState * pipeline_state, std::wstring name) { pipeline_state->m_native->SetName(name.c_str()); } void SetVertexShader(PipelineState* pipeline_state, Shader* shader) { pipeline_state->m_vertex_shader = shader; } void SetFragmentShader(PipelineState* pipeline_state, Shader* shader) { pipeline_state->m_pixel_shader = shader; } void SetComputeShader(PipelineState* pipeline_state, Shader* shader) { pipeline_state->m_compute_shader = shader; } void SetRootSignature(PipelineState* pipeline_state, RootSignature* root_signature) { pipeline_state->m_root_signature = root_signature; } void FinalizePipeline(PipelineState* pipeline_state, Device* device, desc::PipelineStateDesc desc) { auto n_device = device->m_native; pipeline_state->m_device = device; pipeline_state->m_desc = desc; std::variant pso_desc; switch (desc.m_type) { case PipelineType::GRAPHICS_PIPELINE: { pso_desc = internal::GetGraphicsPipelineStateDescriptor(desc, desc.m_input_layout, pipeline_state->m_root_signature, pipeline_state->m_vertex_shader, pipeline_state->m_pixel_shader); } break; case PipelineType::COMPUTE_PIPELINE: { pso_desc = internal::GetComputePipelineStateDescriptor(pipeline_state->m_root_signature, pipeline_state->m_compute_shader); } break; } HRESULT hr = E_FAIL; if (std::holds_alternative(pso_desc)) { hr = n_device->CreateGraphicsPipelineState(&std::get(pso_desc), IID_PPV_ARGS(&pipeline_state->m_native)); } else if (std::holds_alternative(pso_desc)) { hr = n_device->CreateComputePipelineState(&std::get(pso_desc), IID_PPV_ARGS(&pipeline_state->m_native)); } else { LOGC("Variant seems to be empty..."); } if (FAILED(hr)) { LOGC("Failed to create graphics pipeline"); } NAME_D3D12RESOURCE(pipeline_state->m_native) } void RefinalizePipeline(PipelineState* pipeline_state) { FinalizePipeline(pipeline_state, pipeline_state->m_device, pipeline_state->m_desc); } void Destroy(PipelineState* pipeline_state) { SAFE_RELEASE(pipeline_state->m_native); delete pipeline_state; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_readback_buffer.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "d3d12_defines.hpp" #include "d3dx12.hpp" #include "../util/log.hpp" namespace wr::d3d12 { ReadbackBufferResource* wr::d3d12::CreateReadbackBuffer(Device* device, std::uint32_t aligned_buffer_size) { auto native_device = device->m_native; auto* readbackBuffer = new ReadbackBufferResource(); CD3DX12_HEAP_PROPERTIES heap_properties_readback = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(aligned_buffer_size); HRESULT res = native_device->CreateCommittedResource( &heap_properties_readback, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&readbackBuffer->m_resource)); if (FAILED(res)) { LOGC("Error: Could not create a readback buffer!"); } return readbackBuffer; } void* MapReadbackBuffer(ReadbackBufferResource* const readback_buffer, std::uint32_t buffer_size) { if (!readback_buffer) return nullptr; void* memory = nullptr; CD3DX12_RANGE buffer_range = CD3DX12_RANGE(0, buffer_size); readback_buffer->m_resource->Map(0, &buffer_range, &memory); return memory; } void UnmapReadbackBuffer(ReadbackBufferResource* const readback_buffer) { if (!readback_buffer) return; CD3DX12_RANGE buffer_range = CD3DX12_RANGE(0, 0); readback_buffer->m_resource->Unmap(0, &buffer_range); } void SetName(ReadbackBufferResource* readback_buffer, std::wstring name) { readback_buffer->m_resource->SetName(name.c_str()); } void Destroy(ReadbackBufferResource* readback_buffer) { SAFE_RELEASE(readback_buffer->m_resource); delete readback_buffer; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_render_target.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "../util/log.hpp" #include "d3d12_defines.hpp" #include "d3dx12.hpp" namespace wr::d3d12 { RenderTarget* CreateRenderTarget(Device* device, unsigned int width, unsigned int height, desc::RenderTargetDesc descriptor) { auto render_target = new RenderTarget(); const auto n_device = device->m_native; render_target->m_render_targets.resize(descriptor.m_num_rtv_formats); render_target->m_create_info = descriptor; render_target->m_num_render_targets = descriptor.m_num_rtv_formats; render_target->m_width = width; render_target->m_height = height; for (auto i = 0u; i < descriptor.m_num_rtv_formats; i++) { CD3DX12_RESOURCE_DESC resource_desc = CD3DX12_RESOURCE_DESC::Tex2D((DXGI_FORMAT)descriptor.m_rtv_formats[i], width, height, 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); D3D12_CLEAR_VALUE optimized_clear_value = { (DXGI_FORMAT)descriptor.m_rtv_formats[i], descriptor.m_clear_color[0], descriptor.m_clear_color[1], descriptor.m_clear_color[2], descriptor.m_clear_color[3] }; // Create default heap D3D12_RESOURCE_STATES state = (D3D12_RESOURCE_STATES)descriptor.m_initial_state; CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); TRY_M(n_device->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &resource_desc, state, &optimized_clear_value, // optimizes draw call IID_PPV_ARGS(&render_target->m_render_targets[i]) ), "Failed to create render target."); NAME_D3D12RESOURCE(render_target->m_render_targets[i], L"Unnamed Render Target (" + std::to_wstring(i).c_str() + L")"); UINT64 textureUploadBufferSize; n_device->GetCopyableFootprints(&resource_desc, 0, 1, 0, nullptr, nullptr, nullptr, &textureUploadBufferSize); } CreateRenderTargetViews(render_target, device); if (descriptor.m_create_dsv_buffer) { CreateDepthStencilBuffer(render_target, device, width, height); } return render_target; } void SetName(RenderTarget* render_target, std::wstring name) { for (auto i = 0u; i < render_target->m_num_render_targets; i++) { render_target->m_render_targets[i]->SetName((name + std::to_wstring(i)).c_str()); } } void SetName(RenderTarget* render_target, std::string name) { SetName(render_target, std::wstring(name.begin(), name.end())); } unsigned int GetRenderTargetWidth(RenderTarget* render_target) { return render_target->m_width; } unsigned int GetRenderTargetHeight(RenderTarget* render_target) { return render_target->m_height; } void CreateRenderTargetViews(RenderTarget* render_target, Device* device) { const auto n_device = device->m_native; // Create views D3D12_DESCRIPTOR_HEAP_DESC back_buffer_heap_desc = {}; back_buffer_heap_desc.NumDescriptors = render_target->m_num_render_targets; back_buffer_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; back_buffer_heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; TRY_M(n_device->CreateDescriptorHeap(&back_buffer_heap_desc, IID_PPV_ARGS(&render_target->m_rtv_descriptor_heap)), "Failed to create descriptor heap."); render_target->m_rtv_descriptor_increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); // Create render target view with the handle to the heap descriptor. render_target->m_render_targets.resize(render_target->m_num_render_targets); CD3DX12_CPU_DESCRIPTOR_HANDLE rtv_handle(render_target->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart()); for (auto& rt : render_target->m_render_targets) { n_device->CreateRenderTargetView(rt, nullptr, rtv_handle); rtv_handle.Offset(1, render_target->m_rtv_descriptor_increment_size); } } void CreateDepthStencilBuffer(RenderTarget* render_target, Device* device, unsigned int width, unsigned int height) { const auto n_device = device->m_native; auto depth_format = DXGI_FORMAT_R32_TYPELESS; // TODO: Seperate the descriptor heap because that one might not need to be recreated when resizing. // create a depth stencil descriptor heap so we can get a pointer to the depth stencil buffer D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {}; dsvHeapDesc.NumDescriptors = 1; dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV; dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; TRY_M(n_device->CreateDescriptorHeap(&dsvHeapDesc, IID_PPV_ARGS(&render_target->m_depth_stencil_resource_heap)), "Failed to create descriptor heap for depth buffer"); NAME_D3D12RESOURCE(render_target->m_depth_stencil_resource_heap) D3D12_DEPTH_STENCIL_VIEW_DESC depthStencilDesc = {}; depthStencilDesc.Format = DXGI_FORMAT_D32_FLOAT; depthStencilDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D; depthStencilDesc.Flags = D3D12_DSV_FLAG_NONE; D3D12_CLEAR_VALUE depthOptimizedClearValue = {}; depthOptimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT; depthOptimizedClearValue.DepthStencil.Depth = 1.0f; depthOptimizedClearValue.DepthStencil.Stencil = 0; CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_RESOURCE_DESC tex_desc = CD3DX12_RESOURCE_DESC::Tex2D(depth_format, width, height, 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL); TRY_M(n_device->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &tex_desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &depthOptimizedClearValue, IID_PPV_ARGS(&render_target->m_depth_stencil_buffer) ), "Failed to create commited resource."); NAME_D3D12RESOURCE(render_target->m_depth_stencil_buffer) n_device->CreateDepthStencilView(render_target->m_depth_stencil_buffer, &depthStencilDesc, render_target->m_depth_stencil_resource_heap->GetCPUDescriptorHandleForHeapStart()); } void CreateSRVFromDSV(RenderTarget* render_target, DescHeapCPUHandle& handle) { decltype(Device::m_native) n_device; render_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device)); unsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.Format = DXGI_FORMAT_R32_FLOAT; srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; srv_desc.Texture2D.MipLevels = 1; n_device->CreateShaderResourceView(render_target->m_depth_stencil_buffer, &srv_desc, handle.m_native); Offset(handle, 1, increment_size); } void CreateSRVFromRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int num, Format formats[8]) { decltype(Device::m_native) n_device; render_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device)); unsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); for (unsigned int i = 0; i < num; i++) { D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.Format = (DXGI_FORMAT)formats[i]; srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; srv_desc.Texture2D.MipLevels = 1; n_device->CreateShaderResourceView(render_target->m_render_targets[i], &srv_desc, handle.m_native); Offset(handle, 1, increment_size); } } void CreateUAVFromRTV(RenderTarget* render_target, DescHeapCPUHandle & handle, unsigned int num, Format formats[8]) { decltype(Device::m_native) n_device; render_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device)); unsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); for (unsigned int i = 0; i < num; i++) { D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {}; uav_desc.Format = (DXGI_FORMAT)formats[i]; uav_desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; uav_desc.Texture2D.MipSlice = 0; n_device->CreateUnorderedAccessView(render_target->m_render_targets[i], nullptr, &uav_desc, handle.m_native); Offset(handle, 1, increment_size); } } void CreateSRVFromSpecificRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int id, Format format) { decltype(Device::m_native) n_device; render_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device)); unsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.Format = (DXGI_FORMAT)format; srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; srv_desc.Texture2D.MipLevels = 1; n_device->CreateShaderResourceView(render_target->m_render_targets[id], &srv_desc, handle.m_native); Offset(handle, 1, increment_size); } void CreateUAVFromSpecificRTV(RenderTarget* render_target, DescHeapCPUHandle& handle, unsigned int id, Format format) { decltype(Device::m_native) n_device; render_target->m_render_targets[0]->GetDevice(IID_PPV_ARGS(&n_device)); unsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {}; uav_desc.Format = (DXGI_FORMAT)format; uav_desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; uav_desc.Texture2D.MipSlice = 0; n_device->CreateUnorderedAccessView(render_target->m_render_targets[id], nullptr, &uav_desc, handle.m_native); Offset(handle, 1, increment_size); } void Resize(RenderTarget** render_target, Device* device, unsigned int width, unsigned int height) { auto create_info = (*render_target)->m_create_info; Destroy(*render_target); (*render_target) = CreateRenderTarget(device, width, height, create_info); } void IncrementFrameIdx(RenderTarget* render_target) { render_target->m_frame_idx = (render_target->m_frame_idx + 1) % render_target->m_num_render_targets; } void DestroyDepthStencilBuffer(RenderTarget* render_target) { SAFE_RELEASE(render_target->m_depth_stencil_buffer); SAFE_RELEASE(render_target->m_depth_stencil_resource_heap); } void DestroyRenderTargetViews(RenderTarget* render_target) { SAFE_RELEASE(render_target->m_rtv_descriptor_heap); render_target->m_frame_idx = 0; for (auto i = 0; i < render_target->m_render_targets.size(); i++) { SAFE_RELEASE(render_target->m_render_targets[i]); } } void Destroy(RenderTarget* render_target) { if(!render_target) { return; } if (render_target->m_create_info.m_create_dsv_buffer) { DestroyDepthStencilBuffer(render_target); } DestroyRenderTargetViews(render_target); delete render_target; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_render_window.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "../util/log.hpp" #include "d3d12_defines.hpp" namespace wr::d3d12 { namespace internal { inline DXGI_SWAP_CHAIN_DESC1 GetSwapChainDesc(unsigned int width, unsigned int height, unsigned int num_back_buffers) { DXGI_SAMPLE_DESC sample_desc = {}; sample_desc.Count = 1; sample_desc.Quality = 0; DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {}; swap_chain_desc.Width = width; swap_chain_desc.Height = height; swap_chain_desc.Format = (DXGI_FORMAT)settings::back_buffer_format; swap_chain_desc.SampleDesc = sample_desc; swap_chain_desc.BufferCount = num_back_buffers; swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swap_chain_desc.SwapEffect = settings::flip_mode; swap_chain_desc.AlphaMode = settings::swapchain_alpha_mode; swap_chain_desc.Flags = settings::swapchain_flags; swap_chain_desc.Scaling = settings::swapchain_scaling; return swap_chain_desc; } inline void EnsureSwapchainColorSpace(RenderWindow* render_window, std::uint32_t swapchain_bit_depth, bool enable_st2084) { DXGI_COLOR_SPACE_TYPE color_space = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709; switch (swapchain_bit_depth) { default: break; case 10: color_space = enable_st2084 ? DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020 : DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709; break; case 16: color_space = DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709; break; } UINT support = 0; if (SUCCEEDED(render_window->m_swap_chain->CheckColorSpaceSupport(color_space, &support)) && ((support & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT) == DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT)) { render_window->m_swap_chain->SetColorSpace1(color_space); } } inline void SetHDRMetaData(RenderWindow* render_window, std::uint32_t swapchain_bit_depth, float max_output_nits, float min_output_nits, float max_cll, float max_fall) { struct DisplayChromacities { float m_red_x; float m_red_y; float m_green_x; float m_green_y; float m_blue_x; float m_blue_y; float m_white_x; float m_white_y; }; static const DisplayChromacities chromaticity[] = { { 0.64000f, 0.33000f, 0.30000f, 0.60000f, 0.15000f, 0.06000f, 0.31270f, 0.32900f }, // Display Gamut Rec709 { 0.70800f, 0.29200f, 0.17000f, 0.79700f, 0.13100f, 0.04600f, 0.31270f, 0.32900f }, // Display Gamut Rec2020 }; // Select the chromaticity based on HDR format of the DWM. int selected_chroma = 0; if (swapchain_bit_depth == 16) { selected_chroma = 0; } else if (swapchain_bit_depth == 10) { selected_chroma = 1; } else { // Reset the metadata since this is not a supported HDR format. TRY_M(render_window->m_swap_chain->SetHDRMetaData(DXGI_HDR_METADATA_TYPE_NONE, 0, nullptr), "Failed to reset hdr meta data"); return; } // Set HDR meta data const DisplayChromacities& chroma = chromaticity[selected_chroma]; DXGI_HDR_METADATA_HDR10 hdr10_metadata = {}; hdr10_metadata.RedPrimary[0] = static_cast(chroma.m_red_x * 50000.0f); hdr10_metadata.RedPrimary[1] = static_cast(chroma.m_red_y * 50000.0f); hdr10_metadata.GreenPrimary[0] = static_cast(chroma.m_green_x * 50000.0f); hdr10_metadata.GreenPrimary[1] = static_cast(chroma.m_green_y * 50000.0f); hdr10_metadata.BluePrimary[0] = static_cast(chroma.m_blue_x * 50000.0f); hdr10_metadata.BluePrimary[1] = static_cast(chroma.m_blue_y * 50000.0f); hdr10_metadata.WhitePoint[0] = static_cast(chroma.m_white_x * 50000.0f); hdr10_metadata.WhitePoint[1] = static_cast(chroma.m_white_y * 50000.0f); hdr10_metadata.MaxMasteringLuminance = static_cast(max_output_nits * 10000.0f); hdr10_metadata.MinMasteringLuminance = static_cast(min_output_nits * 10000.0f); hdr10_metadata.MaxContentLightLevel = static_cast(max_cll); hdr10_metadata.MaxFrameAverageLightLevel = static_cast(max_fall); TRY_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"); } } RenderWindow* CreateRenderWindow(Device* device, HWND window, CommandQueue* cmd_queue, unsigned int num_back_buffers) { auto render_window = new RenderWindow(); render_window->m_num_render_targets = num_back_buffers; // Get client area for the swap chain size RECT r; GetClientRect(window, &r); unsigned int width = static_cast(r.right - r.left); unsigned int height = static_cast(r.bottom - r.top); render_window->m_width = width; render_window->m_height = height; const auto swap_chain_desc = internal::GetSwapChainDesc(width, height, num_back_buffers); IDXGISwapChain1* temp_swap_chain; TRY_M(device->m_dxgi_factory->CreateSwapChainForHwnd( cmd_queue->m_native, window, &swap_chain_desc, NULL, NULL, &temp_swap_chain ), "Failed to create swap chain for HWND."); render_window->m_swap_chain = static_cast(temp_swap_chain); render_window->m_frame_idx = (render_window->m_swap_chain)->GetCurrentBackBufferIndex(); const float meta_data_args_pool[4][4] = { // MaxOutputNits, MinOutputNits, MaxCLL, MaxFALL // These values are made up for testing. You need to figure out those numbers for your app. { 1000.0f, 0.001f, 2000.0f, 500.0f }, { 500.0f, 0.001f, 2000.0f, 500.0f }, { 500.0f, 0.100f, 500.0f, 100.0f }, { 2000.0f, 1.000f, 2000.0f, 1000.0f } }; int meta_data_args_idx = 0; if (d3d12::settings::output_hdr) { internal::EnsureSwapchainColorSpace(render_window, 16, true); internal::SetHDRMetaData(render_window, 16, meta_data_args_pool[meta_data_args_idx][0], meta_data_args_pool[meta_data_args_idx][1], meta_data_args_pool[meta_data_args_idx][2], meta_data_args_pool[meta_data_args_idx][3]); } render_window->m_render_targets.resize(num_back_buffers); for (decltype(num_back_buffers) i = 0; i < num_back_buffers; i++) { TRY_M(render_window->m_swap_chain->GetBuffer(i, IID_PPV_ARGS(&render_window->m_render_targets[i])), "Failed to get swap chain buffer."); } CreateRenderTargetViews(render_window, device); CreateDepthStencilBuffer(render_window, device, width, height); return render_window; } RenderWindow* CreateRenderWindow(Device* device, IUnknown* window, CommandQueue* cmd_queue, unsigned int num_back_buffers) { auto render_window = new RenderWindow(); render_window->m_num_render_targets = num_back_buffers; unsigned int width = 100; unsigned int height = 100; // TODO: Get the actual size. auto swap_chain_desc = internal::GetSwapChainDesc(width, height, num_back_buffers); IDXGISwapChain1* temp_swap_chain; TRY_M(device->m_dxgi_factory->CreateSwapChainForCoreWindow( cmd_queue->m_native, window, &swap_chain_desc, NULL, &temp_swap_chain ), "Failed to create swap chain for Core Window (UWP)"); render_window->m_swap_chain = static_cast(temp_swap_chain); render_window->m_frame_idx = (render_window->m_swap_chain)->GetCurrentBackBufferIndex(); render_window->m_swap_chain->SetMaximumFrameLatency(num_back_buffers); render_window->m_render_targets.resize(num_back_buffers); for (decltype(num_back_buffers) i = 0; i < num_back_buffers; i++) { TRY_M(render_window->m_swap_chain->GetBuffer(i, IID_PPV_ARGS(&render_window->m_render_targets[i])), "Failed to get swap chain buffer."); } CreateRenderTargetViews(render_window, device); CreateDepthStencilBuffer(render_window, device, width, height); return render_window; } void Resize(RenderWindow* render_window, Device* device, unsigned int width, unsigned int height) { DestroyDepthStencilBuffer(render_window); DestroyRenderTargetViews(render_window); render_window->m_width = width; render_window->m_height = height; render_window->m_swap_chain->ResizeBuffers(render_window->m_num_render_targets, width, height, DXGI_FORMAT_UNKNOWN, 0); render_window->m_render_targets.resize(render_window->m_num_render_targets); for (auto i = 0u; i < render_window->m_num_render_targets; i++) { TRY_M(render_window->m_swap_chain->GetBuffer(i, IID_PPV_ARGS(&render_window->m_render_targets[i])), "Failed to get swap chain buffer."); } CreateRenderTargetViews(render_window, device); CreateDepthStencilBuffer(render_window, device, width, height); } void Present(RenderWindow* render_window) { render_window->m_swap_chain->Present(0, 0); render_window->m_frame_idx = render_window->m_swap_chain->GetCurrentBackBufferIndex(); } void Destroy(RenderWindow* render_window) { DestroyDepthStencilBuffer(render_window); render_window->m_rtv_descriptor_heap->Release(); render_window->m_frame_idx = 0; render_window->m_swap_chain->Release(); delete render_window; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_renderer.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_renderer.hpp" #include "../util/defines.hpp" #include "../util/log.hpp" #include "../scene_graph/scene_graph.hpp" #include "../frame_graph/frame_graph.hpp" #include "../window.hpp" #include "d3d12_defines.hpp" #include "d3d12_material_pool.hpp" #include "d3d12_resource_pool_texture.hpp" #include "d3d12_model_pool.hpp" #include "d3d12_constant_buffer_pool.hpp" #include "d3d12_structured_buffer_pool.hpp" #include "d3d12_functions.hpp" #include "../pipeline_registry.hpp" #include "../rt_pipeline_registry.hpp" #include "../shader_registry.hpp" #include "../root_signature_registry.hpp" #include "d3d12_resource_pool_texture.hpp" #include "d3d12_dynamic_descriptor_heap.hpp" #include "../scene_graph/mesh_node.hpp" #include "../scene_graph/camera_node.hpp" #include "../scene_graph/light_node.hpp" #include "../scene_graph/skybox_node.hpp" #include "../render_tasks/d3d12_equirect_to_cubemap.hpp" #include "../render_tasks/d3d12_cubemap_convolution.hpp" #include #include namespace wr { LINK_SG_RENDER_MESHES(D3D12RenderSystem, Render_MeshNodes) LINK_SG_INIT_MESHES(D3D12RenderSystem, Init_MeshNodes) LINK_SG_INIT_CAMERAS(D3D12RenderSystem, Init_CameraNodes) LINK_SG_INIT_LIGHTS(D3D12RenderSystem, Init_LightNodes) LINK_SG_UPDATE_MESHES(D3D12RenderSystem, Update_MeshNodes) LINK_SG_UPDATE_CAMERAS(D3D12RenderSystem, Update_CameraNodes) LINK_SG_UPDATE_LIGHTS(D3D12RenderSystem, Update_LightNodes) LINK_SG_UPDATE_TRANSFORMS(D3D12RenderSystem, Update_Transforms) LINK_SG_DELETE_SKYBOX(D3D12RenderSystem, Delete_Skybox) D3D12RenderSystem::~D3D12RenderSystem() { for (int i = 0; i < m_structured_buffer_pools.size(); ++i) { m_structured_buffer_pools[i].reset(); } for (auto* shape : m_simple_shapes) { m_shapes_pool->Destroy(shape); } for (int i = 0; i < m_model_pools.size(); ++i) { m_model_pools[i].reset(); } for (int i = 0; i < m_texture_pools.size(); ++i) { m_texture_pools[i].reset(); } for (auto* fence : m_fences) { SAFE_RELEASE(fence->m_native); delete fence; } DestroyShaderRegistry(); DestroyRootSignatureRegistry(); DestroyPipelineRegistry(); DestroyRTPipelineRegistry(); d3d12::Destroy(m_fullscreen_quad_vb); d3d12::Destroy(m_direct_cmd_list); d3d12::Destroy(m_device); d3d12::Destroy(m_direct_queue); d3d12::Destroy(m_copy_queue); d3d12::Destroy(m_compute_queue); if (m_render_window.has_value()) { d3d12::Destroy(m_render_window.value()); } } void D3D12RenderSystem::Init(std::optional window) { m_window = window; m_device = d3d12::CreateDevice(); SetName(m_device, L"Default D3D12 Device"); m_direct_queue = d3d12::CreateCommandQueue(m_device, CmdListType::CMD_LIST_DIRECT); m_compute_queue = d3d12::CreateCommandQueue(m_device, CmdListType::CMD_LIST_COMPUTE); m_copy_queue = d3d12::CreateCommandQueue(m_device, CmdListType::CMD_LIST_COPY); SetName(m_direct_queue, L"Default D3D12 Direct Command Queue"); SetName(m_compute_queue, L"Default D3D12 Compute Command Queue"); SetName(m_copy_queue, L"Default D3D12 Copy Command Queue"); if (window.has_value()) { m_render_window = d3d12::CreateRenderWindow(m_device, window.value()->GetWindowHandle(), m_direct_queue, d3d12::settings::num_back_buffers); } PrepareShaderRegistry(); PrepareRootSignatureRegistry(); PreparePipelineRegistry(); PrepareRTPipelineRegistry(); // Create fences for (auto i = 0; i < m_fences.size(); i++) { m_fences[i] = d3d12::CreateFence(m_device); SetName(m_fences[i], (L"Fence " + std::to_wstring(i))); } // Create viewport m_viewport = d3d12::CreateViewport(window.has_value() ? window.value()->GetWidth() : 400, window.has_value() ? window.value()->GetHeight() : 400); // Create screen quad m_fullscreen_quad_vb = d3d12::CreateStagingBuffer(m_device, (void*)temp::quad_vertices, 4 * sizeof(Vertex2D), sizeof(Vertex2D), ResourceState::VERTEX_AND_CONSTANT_BUFFER); SetName(m_fullscreen_quad_vb, L"Fullscreen quad vertex buffer"); // Create Command List m_direct_cmd_list = d3d12::CreateCommandList(m_device, d3d12::settings::num_back_buffers, CmdListType::CMD_LIST_DIRECT); SetName(m_direct_cmd_list, L"Defauld DX12 Command List"); // Raytracing cb pool m_raytracing_cb_pool = CreateConstantBufferPool(1_mb); // Simple Shapes Model Pool m_shapes_pool = CreateModelPool(8_mb, 8_mb); LoadPrimitiveShapes(); // Material raytracing sb pool size_t rt_mat_align_size = SizeAlignTwoPower((sizeof(temp::RayTracingMaterial_CBData) * d3d12::settings::num_max_rt_materials), 65536) * d3d12::settings::num_back_buffers; m_raytracing_material_sb_pool = CreateStructuredBufferPool(rt_mat_align_size); // Offset raytracing sb pool size_t rt_offset_align_size = SizeAlignTwoPower((sizeof(temp::RayTracingOffset_CBData) * d3d12::settings::num_max_rt_materials), 65536) * d3d12::settings::num_back_buffers; m_raytracing_offset_sb_pool = CreateStructuredBufferPool(rt_offset_align_size); // Begin Recording auto frame_idx = m_render_window.has_value() ? m_render_window.value()->m_frame_idx : 0; d3d12::Begin(m_direct_cmd_list, frame_idx); // Stage fullscreen quad d3d12::StageBuffer(m_fullscreen_quad_vb, m_direct_cmd_list); // Execute d3d12::End(m_direct_cmd_list); d3d12::Execute(m_direct_queue, { m_direct_cmd_list }, m_fences[frame_idx]); m_buffer_frame_graph_uids.resize(d3d12::settings::num_back_buffers); //Rendering engine creates a texture pool that will be used by the render tasks. m_texture_pools.push_back(CreateTexturePool()); CreateDefaultResources(); } CPUTextures D3D12RenderSystem::Render(SceneGraph& scene_graph, FrameGraph& frame_graph) { if (m_skybox_changed) { frame_graph.SetShouldExecute(true); frame_graph.SetShouldExecute(true); m_skybox_changed = false; } // Perform render target save requests while (!m_requested_rt_saves.empty()) { WaitForAllPreviousWork(); auto back = m_requested_rt_saves.back(); SaveRenderTargetToDisc(back.m_path, back.m_render_target, back.m_index); m_requested_rt_saves.pop(); } if (m_requested_fullscreen_state.has_value()) { WaitForAllPreviousWork(); m_render_window.value()->m_swap_chain->SetFullscreenState(m_requested_fullscreen_state.value(), nullptr); Resize(m_window.value()->GetWidth(), m_window.value()->GetHeight()); m_requested_fullscreen_state = std::nullopt; } auto frame_idx = GetFrameIdx(); d3d12::WaitFor(m_fences[frame_idx]); //Signal to the texture pool that we waited for the previous frame //so that stale descriptors and temporary textures can be freed. for (auto pool : m_texture_pools) { pool->ReleaseTemporaryResources(); pool->UnloadTextures(frame_idx); } // Perform reload requests { // Root Signatures auto& rs_registry = RootSignatureRegistry::Get(); rs_registry.Lock(); for (auto request : rs_registry.GetReloadRequests()) { ReloadRootSignatureRegistryEntry(request); } rs_registry.ClearReloadRequests(); rs_registry.Unlock(); // Shaders auto& shader_registry = ShaderRegistry::Get(); shader_registry.Lock(); for (auto request : shader_registry.GetReloadRequests()) { ReloadShaderRegistryEntry(request); } shader_registry.ClearReloadRequests(); shader_registry.Unlock(); // Pipelines auto& pipeline_registry = PipelineRegistry::Get(); pipeline_registry.Lock(); for (auto request : pipeline_registry.GetReloadRequests()) { ReloadPipelineRegistryEntry(request); } pipeline_registry.ClearReloadRequests(); pipeline_registry.Unlock(); // RT Pipelines auto& rt_pipeline_registry = RTPipelineRegistry::Get(); rt_pipeline_registry.Lock(); for (auto request : rt_pipeline_registry.GetReloadRequests()) { ReloadRTPipelineRegistryEntry(request); } rt_pipeline_registry.ClearReloadRequests(); rt_pipeline_registry.Unlock(); } bool clear_frame_buffer = false; if (frame_graph.GetUID() != m_buffer_frame_graph_uids[frame_idx]) { m_buffer_frame_graph_uids[frame_idx] = frame_graph.GetUID(); clear_frame_buffer = true; } PreparePreRenderCommands(clear_frame_buffer, frame_idx); scene_graph.Update(); scene_graph.Optimize(); frame_graph.Execute(scene_graph); auto cmd_lists = frame_graph.GetAllCommandLists(); std::vector n_cmd_lists; n_cmd_lists.reserve(cmd_lists.size()); n_cmd_lists.push_back(m_direct_cmd_list); for (auto& list : cmd_lists) { n_cmd_lists.push_back(list); } // Reset the batches. ResetBatches(scene_graph); d3d12::Execute(m_direct_queue, n_cmd_lists, m_fences[frame_idx]); if (m_render_window.has_value()) { d3d12::Present(m_render_window.value()); } m_bound_model_pool = nullptr; for (int i = 0; i < m_model_pools.size(); ++i) { m_model_pools[i]->SetUpdated(false); } // Optional CPU-visible copy of the render target pixel data const auto cpu_output_texture = frame_graph.GetOutputTexture(); // Optional CPU-visible copy of the render target pixel and/or depth data return frame_graph.GetOutputTexture(); } void D3D12RenderSystem::Resize(std::uint32_t width, std::uint32_t height) { d3d12::ResizeViewport(m_viewport, (int)width, (int)height); if (m_render_window.has_value()) { d3d12::Resize(m_render_window.value(), m_device, width, height); } } std::shared_ptr D3D12RenderSystem::CreateTexturePool() { std::shared_ptr pool = std::make_shared(*this); m_texture_pools.push_back(pool); return pool; } std::shared_ptr D3D12RenderSystem::CreateMaterialPool(std::size_t size_in_bytes) { return std::make_shared(*this); } std::shared_ptr D3D12RenderSystem::CreateModelPool(std::size_t vertex_buffer_pool_size_in_bytes, std::size_t index_buffer_pool_size_in_bytes) { std::shared_ptr pool = std::make_shared(*this, vertex_buffer_pool_size_in_bytes, index_buffer_pool_size_in_bytes); m_model_pools.push_back(pool); return pool; } std::shared_ptr D3D12RenderSystem::CreateConstantBufferPool(std::size_t size_in_bytes) { return std::make_shared(*this, size_in_bytes); } std::shared_ptr D3D12RenderSystem::CreateStructuredBufferPool(std::size_t size_in_bytes) { std::shared_ptr pool = std::make_shared(*this, size_in_bytes); m_structured_buffer_pools.push_back(pool); return pool; } std::shared_ptr D3D12RenderSystem::GetDefaultTexturePool() { if (m_texture_pools.size() > 0) { return m_texture_pools[0]; } return std::shared_ptr(); } void D3D12RenderSystem::WaitForAllPreviousWork() { for (auto& fence : m_fences) { d3d12::WaitFor(fence); Signal(fence, m_direct_queue); } } CommandList* D3D12RenderSystem::GetDirectCommandList(unsigned int num_allocators) { return d3d12::CreateCommandList(m_device, num_allocators, CmdListType::CMD_LIST_DIRECT); } CommandList* D3D12RenderSystem::GetBundleCommandList(unsigned int num_allocators) { return d3d12::CreateCommandList(m_device, num_allocators, CmdListType::CMD_LIST_BUNDLE); } CommandList* D3D12RenderSystem::GetComputeCommandList(unsigned int num_allocators) { return d3d12::CreateCommandList(m_device, num_allocators, CmdListType::CMD_LIST_DIRECT); } CommandList* D3D12RenderSystem::GetCopyCommandList(unsigned int num_allocators) { return d3d12::CreateCommandList(m_device, num_allocators, CmdListType::CMD_LIST_DIRECT); } void D3D12RenderSystem::SetCommandListName(CommandList* cmd_list, std::wstring const& name) { d3d12::SetName(static_cast(cmd_list), name); } void D3D12RenderSystem::DestroyCommandList(CommandList* cmd_list) { Destroy(static_cast(cmd_list)); } RenderTarget* D3D12RenderSystem::GetRenderTarget(RenderTargetProperties properties) { if (properties.m_is_render_window) { if (!m_render_window.has_value()) { LOGC("Tried using a render task which depends on the render window."); return nullptr; } return m_render_window.value(); } else { d3d12::desc::RenderTargetDesc desc; desc.m_initial_state = properties.m_state_finished.Get().value_or(ResourceState::RENDER_TARGET); desc.m_create_dsv_buffer = properties.m_create_dsv_buffer; desc.m_num_rtv_formats = properties.m_num_rtv_formats; desc.m_rtv_formats = properties.m_rtv_formats; desc.m_dsv_format = properties.m_dsv_format; if (properties.m_width.Get().has_value() || properties.m_height.Get().has_value()) { auto retval = d3d12::CreateRenderTarget(m_device, static_cast(properties.m_width.Get().value() * properties.m_resolution_scale.Get()), static_cast(properties.m_height.Get().value() * properties.m_resolution_scale.Get()), desc); return retval; } else if (m_window.has_value()) { auto retval = d3d12::CreateRenderTarget(m_device, static_cast(m_window.value()->GetWidth() * properties.m_resolution_scale.Get()), static_cast(m_window.value()->GetHeight() * properties.m_resolution_scale.Get()), desc); return retval; } else { LOGC("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."); return nullptr; } } } void D3D12RenderSystem::SetRenderTargetName(RenderTarget* render_target, std::wstring const& name) { d3d12::SetName(static_cast(render_target), name); } void D3D12RenderSystem::ResizeRenderTarget(RenderTarget** render_target, std::uint32_t width, std::uint32_t height) { auto n_render_target = static_cast(*render_target); d3d12::Resize((d3d12::RenderTarget**)&n_render_target, m_device, width, height); (*render_target) = n_render_target; } void D3D12RenderSystem::DestroyRenderTarget(RenderTarget** render_target) { Destroy((d3d12::RenderTarget*)*render_target); *render_target = nullptr; } void D3D12RenderSystem::RequestFullscreenChange(bool fullscreen_state) { m_requested_fullscreen_state = fullscreen_state; } void D3D12RenderSystem::ResetCommandList(CommandList* cmd_list) { auto n_cmd_list = static_cast(cmd_list); auto frame_idx = GetFrameIdx(); d3d12::Begin(n_cmd_list, frame_idx); } void D3D12RenderSystem::CloseCommandList(CommandList* cmd_list) { auto n_cmd_list = static_cast(cmd_list); d3d12::End(n_cmd_list); } void D3D12RenderSystem::StartRenderTask(CommandList* cmd_list, std::pair render_target) { auto n_cmd_list = static_cast(cmd_list); auto n_render_target = static_cast(render_target.first); auto frame_idx = GetFrameIdx(); if (render_target.second.m_is_render_window) // TODO: do once at the beginning of the frame. { d3d12::Transition(n_cmd_list, n_render_target, frame_idx, ResourceState::PRESENT, ResourceState::RENDER_TARGET); } else if (render_target.second.m_state_finished.Get().has_value() && render_target.second.m_state_execute.Get().has_value()) { d3d12::Transition(n_cmd_list, n_render_target, render_target.second.m_state_finished.Get().value(), render_target.second.m_state_execute.Get().value()); } else { LOGW("A render target has no transitions specified. Is this correct?"); } if (render_target.second.m_is_render_window) { d3d12::BindRenderTargetVersioned(n_cmd_list, n_render_target, frame_idx, render_target.second.m_clear, render_target.second.m_clear_depth); } else { d3d12::BindRenderTarget(n_cmd_list, n_render_target, render_target.second.m_clear, render_target.second.m_clear_depth); } } void D3D12RenderSystem::StopRenderTask(CommandList* cmd_list, std::pair render_target) { auto n_cmd_list = static_cast(cmd_list); auto n_render_target = static_cast(render_target.first); unsigned int frame_idx = GetFrameIdx(); if (render_target.second.m_is_render_window) { d3d12::Transition(n_cmd_list, n_render_target, frame_idx, ResourceState::RENDER_TARGET, ResourceState::PRESENT); } else if (render_target.second.m_state_finished.Get().has_value() && render_target.second.m_state_execute.Get().has_value()) { d3d12::Transition(n_cmd_list, n_render_target, render_target.second.m_state_execute.Get().value(), render_target.second.m_state_finished.Get().value()); } else { LOGW("A render target has no transitions specified. Is this correct?"); } } void D3D12RenderSystem::StartComputeTask(CommandList * cmd_list, std::pair render_target) { } void D3D12RenderSystem::StopComputeTask(CommandList * cmd_list, std::pair render_target) { } void D3D12RenderSystem::StartCopyTask(CommandList * cmd_list, std::pair render_target) { auto n_cmd_list = static_cast(cmd_list); auto n_render_target = static_cast(render_target.first); auto frame_idx = GetFrameIdx(); if (render_target.second.m_is_render_window) // TODO: do once at the beginning of the frame. { d3d12::Transition(n_cmd_list, n_render_target, frame_idx, ResourceState::PRESENT, render_target.second.m_state_execute.Get().value()); } else if (render_target.second.m_state_finished.Get().has_value() && render_target.second.m_state_execute.Get().has_value()) { d3d12::Transition(n_cmd_list, n_render_target, render_target.second.m_state_finished.Get().value(), render_target.second.m_state_execute.Get().value()); } } void D3D12RenderSystem::StopCopyTask(CommandList * cmd_list, std::pair render_target) { auto n_cmd_list = static_cast(cmd_list); auto n_render_target = static_cast(render_target.first); auto frame_idx = GetFrameIdx(); if (render_target.second.m_is_render_window) { d3d12::Transition(n_cmd_list, n_render_target, frame_idx, render_target.second.m_state_execute.Get().value(), ResourceState::PRESENT); } else if (render_target.second.m_state_finished.Get().has_value() && render_target.second.m_state_execute.Get().has_value()) { d3d12::Transition(n_cmd_list, n_render_target, render_target.second.m_state_execute.Get().value(), render_target.second.m_state_finished.Get().value()); } } void D3D12RenderSystem::SaveRenderTargetToDisc(std::string const& path, RenderTarget* render_target, unsigned int index) { auto n_render_target = static_cast(render_target); auto format = static_cast(n_render_target->m_render_targets[index]->GetDesc().Format); auto rt_resource = n_render_target->m_render_targets[index]; auto n_device = m_device->m_native; auto width = d3d12::GetRenderTargetWidth(n_render_target); auto height = d3d12::GetRenderTargetHeight(n_render_target); auto bytes_per_pixel = BytesPerPixel(format); std::uint64_t bytes_per_row = SizeAlignTwoPower(static_cast(width) * static_cast(bytes_per_pixel), 256); std::uint64_t texture_size = bytes_per_row * static_cast(height); auto queue = d3d12::CreateCommandQueue(m_device, CmdListType::CMD_LIST_DIRECT); SetName(queue, L"Screenshot Command Queue"); auto cmd_list = d3d12::CreateCommandList(m_device, 1, CmdListType::CMD_LIST_DIRECT); SetName(cmd_list, L"Screenshot Command List"); auto fence = d3d12::CreateFence(m_device); d3d12::Begin(cmd_list, 0); // Create the actual read back buffer auto readback_buffer = d3d12::CreateReadbackBuffer(m_device, texture_size); d3d12::SetName(readback_buffer, L"Texture ReadBack Buffer (Used for saving to disc)"); // Copy data to cpu D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint; auto rt_desc = rt_resource->GetDesc(); n_device->GetCopyableFootprints(&rt_desc, 0, 1, 0, &footprint, nullptr, nullptr, (std::uint64_t*)&texture_size); CD3DX12_TEXTURE_COPY_LOCATION dest_loc(readback_buffer->m_resource, footprint); CD3DX12_TEXTURE_COPY_LOCATION src_loc(rt_resource, 0); cmd_list->m_native->CopyTextureRegion(&dest_loc, 0, 0, 0, &src_loc, nullptr); d3d12::End(cmd_list); // Execute Execute(queue, { cmd_list }, fence); fence->m_fence_value++; Signal(fence, queue); WaitFor(fence); // Store data auto pixels = static_cast(d3d12::MapReadbackBuffer(readback_buffer, texture_size)); d3d12::UnmapReadbackBuffer(readback_buffer); DirectX::Image img; img.format = static_cast(format); img.width = width; img.height = height; img.rowPitch = bytes_per_row; img.slicePitch = texture_size; img.pixels = pixels; auto wpath = std::wstring(path.begin(), path.end()); TRY_M(DirectX::SaveToTGAFile(img, wpath.c_str()), "Failed to save image to disc"); d3d12::Destroy(readback_buffer); d3d12::Destroy(cmd_list); d3d12::Destroy(fence); d3d12::Destroy(queue); } void D3D12RenderSystem::PrepareRootSignatureRegistry() { auto& registry = RootSignatureRegistry::Get(); for (auto desc : registry.m_descriptions) { d3d12::desc::RootSignatureDesc n_desc; n_desc.m_parameters = desc.second.m_parameters; n_desc.m_samplers = desc.second.m_samplers; n_desc.m_rtx = desc.second.m_rtx; n_desc.m_rt_local = desc.second.m_rtx_local; auto n_rs = d3d12::CreateRootSignature(n_desc); d3d12::FinalizeRootSignature(n_rs, m_device); SetName(n_rs, (L"Root Signature " + desc.second.name)); registry.m_objects.insert({ desc.first, n_rs }); } } void D3D12RenderSystem::PrepareShaderRegistry() { auto& registry = ShaderRegistry::Get(); for (auto desc : registry.m_descriptions) { auto shader_error = d3d12::LoadShader(m_device, desc.second.type, desc.second.path, desc.second.entry, desc.second.defines); if (std::holds_alternative(shader_error)) { auto shader = std::get(shader_error); registry.m_objects.insert({ desc.first, shader }); } else { try { LOGC(std::get(shader_error)); } catch(std::exception e) { LOGW("Seems like FMT failed to format the error message. Using cout instead."); std::cerr << std::get(shader_error) << std::endl; } } } } void D3D12RenderSystem::PreparePipelineRegistry() { auto& registry = PipelineRegistry::Get(); for (auto desc : registry.m_descriptions) { d3d12::desc::PipelineStateDesc n_desc; n_desc.m_counter_clockwise = desc.second.m_counter_clockwise; n_desc.m_cull_mode = desc.second.m_cull_mode; n_desc.m_depth_enabled = desc.second.m_depth_enabled; n_desc.m_dsv_format = desc.second.m_dsv_format; n_desc.m_input_layout = desc.second.m_input_layout; n_desc.m_num_rtv_formats = desc.second.m_num_rtv_formats; n_desc.m_rtv_formats = desc.second.m_rtv_formats; n_desc.m_topology_type = desc.second.m_topology_type; n_desc.m_type = desc.second.m_type; auto n_pipeline = d3d12::CreatePipelineState(); if (desc.second.m_vertex_shader_handle.has_value()) { auto obj = ShaderRegistry::Get().Find(desc.second.m_vertex_shader_handle.value()); auto shader = static_cast(obj); d3d12::SetVertexShader(n_pipeline, shader); } if (desc.second.m_pixel_shader_handle.has_value()) { auto obj = ShaderRegistry::Get().Find(desc.second.m_pixel_shader_handle.value()); auto shader = static_cast(obj); d3d12::SetFragmentShader(n_pipeline, shader); } if (desc.second.m_compute_shader_handle.has_value()) { auto obj = ShaderRegistry::Get().Find(desc.second.m_compute_shader_handle.value()); auto shader = static_cast(obj); d3d12::SetComputeShader(n_pipeline, shader); } { auto obj = RootSignatureRegistry::Get().Find(desc.second.m_root_signature_handle); d3d12::SetRootSignature(n_pipeline, static_cast(obj)); } d3d12::FinalizePipeline(n_pipeline, m_device, n_desc); SetName(n_pipeline, L"Default pipeline state"); registry.m_objects.insert({ desc.first, n_pipeline }); } } void D3D12RenderSystem::ReloadPipelineRegistryEntry(RegistryHandle handle) { auto& registry = PipelineRegistry::Get(); std::optional error_msg = std::nullopt; auto n_pipeline = static_cast(registry.Find(handle)); auto recompile_shader = [&error_msg, this](auto& pipeline_shader) { if (!pipeline_shader) return; auto new_shader_variant = d3d12::LoadShader(m_device, pipeline_shader->m_type, pipeline_shader->m_path, pipeline_shader->m_entry, pipeline_shader->m_defines); if (std::holds_alternative(new_shader_variant)) { pipeline_shader = std::get(new_shader_variant); } else { error_msg = std::get(new_shader_variant); } }; // Vertex Shader { recompile_shader(n_pipeline->m_vertex_shader); } // Pixel Shader if (!error_msg.has_value()) { recompile_shader(n_pipeline->m_pixel_shader); } // Compute Shader if (!error_msg.has_value()) { recompile_shader(n_pipeline->m_compute_shader); } if (error_msg.has_value()) { LOGW(error_msg.value()); //open_shader_compiler_popup = true; //shader_compiler_error = error_msg.value(); } else { d3d12::RefinalizePipeline(n_pipeline); } } void D3D12RenderSystem::ReloadRTPipelineRegistryEntry(RegistryHandle handle) { auto& registry = RTPipelineRegistry::Get(); std::optional error_msg = std::nullopt; auto n_pipeline = static_cast(registry.Find(handle)); auto recompile_shader = [&error_msg, this](auto& pipeline_shader) { auto new_shader_variant = d3d12::LoadShader(m_device, pipeline_shader->m_type, pipeline_shader->m_path, pipeline_shader->m_entry, pipeline_shader->m_defines); if (std::holds_alternative(new_shader_variant)) { pipeline_shader = std::get(new_shader_variant); } else { error_msg = std::get(new_shader_variant); } }; // Library Shader { recompile_shader(n_pipeline->m_desc.m_library); } if (error_msg.has_value()) { LOGW(error_msg.value()); //open_shader_compiler_popup = true; //shader_compiler_error = error_msg.value(); } else { d3d12::RecreateStateObject(n_pipeline); } } void D3D12RenderSystem::ReloadShaderRegistryEntry(RegistryHandle handle) { auto& registry = ShaderRegistry::Get(); std::optional error_msg = std::nullopt; auto n_shader = static_cast(registry.Find(handle)); auto new_shader_variant = d3d12::LoadShader(m_device, n_shader->m_type, n_shader->m_path, n_shader->m_entry); if (std::holds_alternative(new_shader_variant)) { d3d12::Destroy(n_shader); n_shader = std::get(new_shader_variant); } else { LOGW(std::get(new_shader_variant)); } } void D3D12RenderSystem::ReloadRootSignatureRegistryEntry(RegistryHandle handle) { auto& registry = RootSignatureRegistry::Get(); auto n_root_signature = static_cast(registry.Find(handle)); d3d12::RefinalizeRootSignature(n_root_signature, m_device); } void D3D12RenderSystem::PrepareRTPipelineRegistry() { auto& registry = RTPipelineRegistry::Get(); for (auto it : registry.m_descriptions) { auto desc = it.second; auto library = static_cast(ShaderRegistry::Get().Find(desc.library_desc.shader_handle)); d3d12::desc::StateObjectDesc n_desc; n_desc.m_library = library; n_desc.m_library_exports = desc.library_desc.exports; n_desc.max_attributes_size = static_cast(desc.max_attributes_size); n_desc.max_payload_size = static_cast(desc.max_payload_size); n_desc.max_recursion_depth = static_cast(desc.max_recursion_depth); n_desc.m_hit_groups = desc.library_desc.m_hit_groups; if (auto rt_handle = desc.global_root_signature.value(); desc.global_root_signature.has_value()) { n_desc.global_root_signature = static_cast(RootSignatureRegistry::Get().Find(rt_handle)); } n_desc.local_root_signatures = std::vector(); for (auto rt_handle : desc.local_root_signatures) { auto rs = static_cast(RootSignatureRegistry::Get().Find(rt_handle)); n_desc.local_root_signatures.value().push_back(rs); } auto n_state_object = d3d12::CreateStateObject(m_device, n_desc); registry.m_objects.insert({ it.first, n_state_object }); } } namespace internal { template void DestroyGenericRegistry() { auto& registry = R::Get(); for (auto it : registry.m_objects) { auto native = static_cast(it.second); if (native) { d3d12::Destroy(native); } } } } /* internal */ void D3D12RenderSystem::DestroyRootSignatureRegistry() { internal::DestroyGenericRegistry(); } void D3D12RenderSystem::DestroyShaderRegistry() { internal::DestroyGenericRegistry(); } void D3D12RenderSystem::DestroyPipelineRegistry() { internal::DestroyGenericRegistry(); } void D3D12RenderSystem::DestroyRTPipelineRegistry() { internal::DestroyGenericRegistry(); } void D3D12RenderSystem::InitSceneGraph(SceneGraph& scene_graph) { auto frame_idx = GetFrameIdx(); d3d12::WaitFor(m_fences[frame_idx]); d3d12::Begin(m_direct_cmd_list, frame_idx); scene_graph.Init(); d3d12::End(m_direct_cmd_list); // Execute d3d12::Execute(m_direct_queue, { m_direct_cmd_list }, m_fences[frame_idx]); } void D3D12RenderSystem::Init_MeshNodes(std::vector>& nodes) { } void D3D12RenderSystem::Init_CameraNodes(std::vector>& nodes) { if (nodes.empty()) return; size_t cam_align_size = SizeAlignTwoPower(nodes.size() * sizeof(temp::ProjectionView_CBData), 256) * d3d12::settings::num_back_buffers; m_camera_pool = CreateConstantBufferPool((size_t)std::ceil(cam_align_size)); for (auto& node : nodes) { node->m_camera_cb = m_camera_pool->Create(sizeof(temp::ProjectionView_CBData)); } } void D3D12RenderSystem::Init_LightNodes(std::vector>& nodes, std::vector& lights) { } void D3D12RenderSystem::Update_Transforms(SceneGraph& scene_graph, std::shared_ptr& node) { if (node->RequiresTransformUpdate(GetFrameIdx())) { node->UpdateTransform(); node->SignalTransformUpdate(GetFrameIdx()); } auto& children = node->m_children; auto it = children.begin(); while (it != children.end()) { Update_Transforms(scene_graph, *it); ++it; } } void D3D12RenderSystem::Delete_Skybox(SceneGraph& scene_graph, std::shared_ptr& skybox_node) { unsigned int frame_idx = GetFrameIdx(); skybox_node->m_irradiance.value().m_pool->MarkForUnload(skybox_node->m_irradiance.value(), frame_idx); skybox_node->m_skybox.value().m_pool->MarkForUnload(skybox_node->m_skybox.value(), frame_idx); skybox_node->m_prefiltered_env_map.value().m_pool->MarkForUnload(skybox_node->m_prefiltered_env_map.value(), frame_idx); if (skybox_node->m_hdr.m_pool) { skybox_node->m_hdr.m_pool->MarkForUnload(skybox_node->m_hdr, frame_idx); } } void D3D12RenderSystem::PreparePreRenderCommands(bool clear_frame_buffer, int frame_idx) { d3d12::Begin(m_direct_cmd_list, frame_idx); if (clear_frame_buffer) { CD3DX12_CPU_DESCRIPTOR_HANDLE rtv_descriptor(m_render_window.value()->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart()); rtv_descriptor.Offset(frame_idx, m_render_window.value()->m_rtv_descriptor_increment_size); } for (int i = 0; i < m_structured_buffer_pools.size(); ++i) { m_structured_buffer_pools[i]->UpdateBuffers(m_direct_cmd_list, frame_idx); } for (int i = 0; i < m_model_pools.size(); ++i) { m_model_pools[i]->StageMeshes(m_direct_cmd_list); } for (auto pool : m_texture_pools) { pool->Stage(m_direct_cmd_list); } d3d12::End(m_direct_cmd_list); } void D3D12RenderSystem::Update_MeshNodes(std::vector>& nodes) { for (auto& node : nodes) { if (!node->RequiresUpdate(GetFrameIdx())) { continue; } node->Update(GetFrameIdx()); } } void D3D12RenderSystem::Update_CameraNodes(std::vector>& nodes) { for (auto& node : nodes) { if (!node->RequiresUpdate(GetFrameIdx())) { continue; } node->UpdateTemp(GetFrameIdx()); temp::ProjectionView_CBData data; data.m_projection = node->m_projection; data.m_inverse_projection = node->m_inverse_projection; data.m_prev_projection = node->m_prev_projection; data.m_view = node->m_view; data.m_inverse_view = node->m_inverse_view; data.m_prev_view = node->m_prev_view; data.m_is_hybrid = 0; node->m_camera_cb->m_pool->Update(node->m_camera_cb, sizeof(temp::ProjectionView_CBData), 0, (uint8_t*)&data); } } void D3D12RenderSystem::Update_LightNodes(SceneGraph& scene_graph) { bool should_update = false; uint32_t offset_start = 0, offset_end = 0; std::vector>& light_nodes = scene_graph.GetLightNodes(); for (uint32_t i = 0, j = (uint32_t)light_nodes.size(); i < j; ++i) { std::shared_ptr& node = light_nodes[i]; if (!node->RequiresUpdate(GetFrameIdx())) { continue; } if (!should_update) { should_update = true; offset_start = i; } node->Update(GetFrameIdx()); offset_end = i; } if (!should_update && !(offset_end == offset_start && offset_start == 0)) return; //Update light count scene_graph.GetLight(0)->tid &= 3; scene_graph.GetLight(0)->tid |= scene_graph.GetCurrentLightSize() << 2; //Update structured buffer StructuredBufferHandle* structured_buffer = scene_graph.GetLightBuffer(); structured_buffer->m_pool->Update(structured_buffer, scene_graph.GetLight(offset_start), sizeof(Light) * (offset_end - offset_start + 1), sizeof(Light) * offset_start); } void D3D12RenderSystem::Render_MeshNodes(temp::MeshBatches& batches, CameraNode* camera, CommandList* cmd_list) { auto n_cmd_list = static_cast(cmd_list); auto d3d12_camera_cb = static_cast(camera->m_camera_cb); d3d12::BindConstantBuffer(n_cmd_list, d3d12_camera_cb->m_native, 0, GetFrameIdx()); //Render batches for (auto& elem : batches) { auto model = elem.first.first; auto materials = elem.first.second; temp::MeshBatch& batch = elem.second; //Bind object data auto d3d12_cb_handle = static_cast(batch.batch_buffer); d3d12::BindConstantBuffer(n_cmd_list, d3d12_cb_handle->m_native, 1, GetFrameIdx()); //Render meshes for (std::size_t mesh_i = 0; mesh_i < model->m_meshes.size(); mesh_i++) { auto mesh = model->m_meshes[mesh_i]; auto n_mesh = static_cast(model->m_model_pool)->GetMeshData(mesh.first->id); if (model->m_model_pool != m_bound_model_pool || n_mesh->m_vertex_staging_buffer_stride != m_bound_model_pool_stride) { D3D12ModelPool* model_pool = static_cast(model->m_model_pool); d3d12::BindVertexBuffer(n_cmd_list, model_pool->GetVertexStagingBuffer(), 0, model_pool->GetVertexStagingBuffer()->m_size, n_mesh->m_vertex_staging_buffer_stride); d3d12::BindIndexBuffer(n_cmd_list, model_pool->GetIndexStagingBuffer(), 0, static_cast(model_pool->GetIndexStagingBuffer()->m_size)); m_bound_model_pool = static_cast(model->m_model_pool); m_bound_model_pool_stride = n_mesh->m_vertex_staging_buffer_stride; } d3d12::BindDescriptorHeaps(n_cmd_list); // Pick the standard material or if available a user defined material. auto material_handle = mesh.second; if (materials.size() > mesh_i) { material_handle = materials[mesh_i]; } if (material_handle != m_last_material) { m_last_material = material_handle; BindMaterial(material_handle, cmd_list); } if (n_mesh->m_index_count != 0) { d3d12::DrawIndexed(n_cmd_list, static_cast(n_mesh->m_index_count), batch.num_instances, static_cast(n_mesh->m_index_staging_buffer_offset), static_cast(n_mesh->m_vertex_staging_buffer_offset)); } else { d3d12::Draw(n_cmd_list, static_cast(n_mesh->m_vertex_count), batch.num_instances, static_cast(n_mesh->m_vertex_staging_buffer_offset)); } } } // Reset frame specific variables m_last_material.m_id = 0; m_last_material.m_pool = nullptr; } void D3D12RenderSystem::BindMaterial(MaterialHandle material_handle, CommandList* cmd_list) { auto n_cmd_list = static_cast(cmd_list); auto* material_internal = material_handle.m_pool->GetMaterial(material_handle); material_internal->UpdateConstantBuffer(); D3D12ConstantBufferHandle* handle = static_cast(material_internal->GetConstantBufferHandle()); auto albedo_handle = material_internal->GetTexture(TextureType::ALBEDO); wr::d3d12::TextureResource* albedo_internal; if (albedo_handle.m_pool == nullptr) { auto default_albedo_handle = GetDefaultAlbedo(); albedo_internal = static_cast(default_albedo_handle.m_pool->GetTextureResource(default_albedo_handle)); } else { albedo_internal = static_cast(albedo_handle.m_pool->GetTextureResource(albedo_handle)); } auto normal_handle = material_internal->GetTexture(TextureType::NORMAL); wr::d3d12::TextureResource* normal_internal; if (normal_handle.m_pool == nullptr) { auto default_normal_handle = GetDefaultNormal(); normal_internal = static_cast(default_normal_handle.m_pool->GetTextureResource(default_normal_handle)); } else { normal_internal = static_cast(normal_handle.m_pool->GetTextureResource(normal_handle)); } auto roughness_handle = material_internal->GetTexture(TextureType::ROUGHNESS); wr::d3d12::TextureResource* roughness_internal; if (roughness_handle.m_pool == nullptr) { auto default_roughness_handle = GetDefaultRoughness(); roughness_internal = static_cast(default_roughness_handle.m_pool->GetTextureResource(default_roughness_handle)); } else { roughness_internal = static_cast(roughness_handle.m_pool->GetTextureResource(roughness_handle)); } auto metallic_handle = material_internal->GetTexture(TextureType::METALLIC); wr::d3d12::TextureResource* metallic_internal; if (metallic_handle.m_pool == nullptr) { auto default_metallic_handle = GetDefaultMetalic(); metallic_internal = static_cast(default_metallic_handle.m_pool->GetTextureResource(default_metallic_handle)); } else { metallic_internal = static_cast(metallic_handle.m_pool->GetTextureResource(metallic_handle)); } auto emissive_handle = material_internal->GetTexture(TextureType::EMISSIVE); wr::d3d12::TextureResource* emissive_internal; if (emissive_handle.m_pool == nullptr) { auto default_emissive_handle = GetDefaultEmissive(); emissive_internal = static_cast(default_emissive_handle.m_pool->GetTextureResource(default_emissive_handle)); } else { emissive_internal = static_cast(emissive_handle.m_pool->GetTextureResource(emissive_handle)); } auto ao_handle = material_internal->GetTexture(TextureType::AO); wr::d3d12::TextureResource* ao_internal; if (ao_handle.m_pool == nullptr) { auto default_ao_handle = GetDefaultAO(); ao_internal = static_cast(default_ao_handle.m_pool->GetTextureResource(default_ao_handle)); } else { ao_internal = static_cast(ao_handle.m_pool->GetTextureResource(ao_handle)); } d3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::ALBEDO)), albedo_internal); d3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::NORMAL)), normal_internal); d3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::ROUGHNESS)), roughness_internal); d3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::METALLIC)), metallic_internal); d3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::EMISSIVE)), emissive_internal); d3d12::SetShaderSRV(n_cmd_list, 2, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::deferred_main, params::DeferredMainE::AMBIENT_OCCLUSION)), ao_internal); d3d12::BindConstantBuffer(n_cmd_list, handle->m_native, 3, GetFrameIdx()); } unsigned int D3D12RenderSystem::GetFrameIdx() { if (m_render_window.has_value()) { return m_render_window.value()->m_frame_idx; } else { LOGW("Called `D3D12RenderSystem::GetFrameIdx` without a window!"); return 0; } } d3d12::RenderWindow* D3D12RenderSystem::GetRenderWindow() { if (m_render_window.has_value()) { return m_render_window.value(); } else { LOGW("Called `D3D12RenderSystem::GetRenderWindow` without a window!"); return nullptr; } } void D3D12RenderSystem::RequestSkyboxReload() { m_skybox_changed = true; } wr::Model* D3D12RenderSystem::GetSimpleShape(SimpleShapes type) { if (type == SimpleShapes::COUNT) { LOGC("Nice try boiii! That's not a shape."); } return m_simple_shapes[static_cast(type)]; } void D3D12RenderSystem::ResetBatches(SceneGraph& sg) { for (auto& batch : sg.GetBatches()) { batch.second.num_instances = 0; batch.second.num_global_instances = 0; } } void D3D12RenderSystem::LoadPrimitiveShapes() { // Load Cube. { wr::MeshData mesh; mesh.m_indices = { 2, 1, 0, 3, 2, 0, 6, 5, 4, 7, 6, 4, 10, 9, 8, 11, 10, 8, 14, 13, 12, 15, 14, 12, 18, 17, 16, 19, 18, 16, 22, 21, 20, 23, 22, 20 }; mesh.m_vertices = { { 1, 1, -1, 1, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0 }, { 1, -1, -1, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0 }, { -1, -1, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0 }, { -1, 1, -1, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { -1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { -1, -1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 1, -1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 1, 1, -1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, -1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, -1, -1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, -1, -1, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0 }, { 1, -1, 1, 1, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0 }, { -1, -1, 1, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0 }, { -1, -1, -1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0 }, { -1, -1, -1, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0 }, { -1, -1, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0 }, { -1, 1, 1, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0 }, { -1, 1, -1, 1, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, -1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { -1, 1, -1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { -1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, }; m_simple_shapes[static_cast(SimpleShapes::CUBE)] = m_shapes_pool->LoadCustom({ mesh }); } { wr::MeshData mesh; mesh.m_indices = { 2, 1, 0, 3, 2, 0 }; mesh.m_vertices = { //POS UV NORMAL TANGENT BINORMAL COLOR { 1, 1, 0, 1, 1, 0, 0, -1, 0, 0, 1, 0, 1, 0 }, { 1, -1, 0, 1, 0, 0, 0, -1, 0, 0, 1, 0, 1, 0 }, { -1, -1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 1, 0 }, { -1, 1, 0, 0, 1, 0, 0, -1, 0, 0, 1, 0, 1, 0 }, }; m_simple_shapes[static_cast(SimpleShapes::PLANE)] = m_shapes_pool->LoadCustom({ mesh }); } } void D3D12RenderSystem::CreateDefaultResources() { auto default_texture_pool = m_texture_pools.at(0); m_default_cubemap = default_texture_pool->CreateCubemap("DefaultResource_Cubemap", 2, 2, 1, wr::Format::R8G8B8A8_UNORM, false); m_default_albedo = default_texture_pool->LoadFromFile(settings::default_albedo_path, false, false); m_default_normal = default_texture_pool->LoadFromFile(settings::default_normal_path, false, false); m_default_white = default_texture_pool->LoadFromFile(settings::default_white_texture, false, false); m_default_black = default_texture_pool->LoadFromFile(settings::default_black_texture, false, false); } } /* */ ================================================ FILE: src/d3d12/d3d12_renderer.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../renderer.hpp" #include #include "../scene_graph/scene_graph.hpp" #include "../scene_graph/light_node.hpp" #include "../vertex.hpp" #include "d3d12_structs.hpp" namespace wr { namespace d3d12 { struct CommandList; struct RenderTarget; } struct MeshNode; struct CameraNode; struct D3D12ConstantBufferHandle; class D3D12StructuredBufferPool; class D3D12ModelPool; class D3D12TexturePool; class DynamicDescriptorHeap; namespace temp { struct ProjectionView_CBData { DirectX::XMMATRIX m_view = DirectX::XMMatrixIdentity(); DirectX::XMMATRIX m_projection = DirectX::XMMatrixIdentity(); DirectX::XMMATRIX m_inverse_projection = DirectX::XMMatrixIdentity(); DirectX::XMMATRIX m_inverse_view = DirectX::XMMatrixIdentity(); DirectX::XMMATRIX m_prev_projection = DirectX::XMMatrixIdentity(); DirectX::XMMATRIX m_prev_view = DirectX::XMMatrixIdentity(); unsigned int m_is_hybrid = 0u; unsigned int m_is_path_tracer = 0u; unsigned int m_is_ao = 0u; unsigned int m_has_shadows = 0u; int padding[3]; unsigned int m_has_reflections = 0u; }; struct ShadowDenoiserSettings_CBData { float m_alpha; float m_moments_alpha; float m_l_phi; float m_n_phi; float m_z_phi; float m_step_distance; float m_padding[2]; }; struct ReflectionDenoiserSettings_CBData { float m_color_integration_alpha; float m_moments_integration_alpha; float m_variance_clipping_sigma; float m_roughness_reprojection_threshold; int m_max_history_samples; float m_n_phi; float m_z_phi; float m_l_phi; }; struct DenoiserCamera_CBData { DirectX::XMMATRIX m_view; DirectX::XMMATRIX m_prev_view; DirectX::XMMATRIX m_inverse_view; DirectX::XMMATRIX m_projection; DirectX::XMMATRIX m_prev_projection; DirectX::XMMATRIX m_inverse_projection; float m_padding[2]; float m_near_plane; float m_far_plane; }; struct RTHybridCamera_CBData { DirectX::XMMATRIX m_inverse_view = DirectX::XMMatrixIdentity(); DirectX::XMMATRIX m_inverse_projection = DirectX::XMMatrixIdentity(); DirectX::XMMATRIX m_inv_vp = DirectX::XMMatrixIdentity(); float m_frame_idx = 0.0f; float m_intensity = 0.0f; float m_epsilon = 0.01f; std::uint32_t m_sample_count = 1u; }; struct RTAO_CBData { DirectX::XMMATRIX m_inv_vp = DirectX::XMMatrixIdentity(); DirectX::XMMATRIX m_inv_view = DirectX::XMMatrixIdentity(); float m_bias = 0.0f; float m_radius = 0.0f; float m_power = 0.0f; float m_max_distance = 0.0f; float m_padding[2]; float m_frame_idx = 0.0f; std::uint32_t m_sample_count = 0u; }; struct RayTracingCamera_CBData { DirectX::XMMATRIX m_view = DirectX::XMMatrixIdentity(); DirectX::XMMATRIX m_inverse_view_projection = DirectX::XMMatrixIdentity(); DirectX::XMVECTOR m_camera_position = DirectX::XMVectorZero(); float focal_radius = 0.0f; float focal_length = 0.0f; float frame_idx = 0.0f; float intensity = 0.0f; }; struct RayTracingMaterial_CBData { std::uint32_t albedo_id = 0u; std::uint32_t normal_id = 0u; std::uint32_t roughness_id = 0u; std::uint32_t metallicness_id = 0u; std::uint32_t emissive_id = 0u; std::uint32_t ao_id = 0u; std::uint32_t padding[2] = { 0u }; Material::MaterialData material_data; }; struct RayTracingOffset_CBData { std::uint32_t material_idx = 0u; std::uint32_t idx_offset = 0u; std::uint32_t vertex_offset = 0u; }; static const constexpr float size = 1.0f; static const constexpr Vertex2D quad_vertices[] = { { -size, -size }, { size, -size }, { -size, size }, { size, size }, }; } /* temp */ class D3D12RenderSystem final : public RenderSystem { public: ~D3D12RenderSystem(); void Init(std::optional window); CPUTextures Render(SceneGraph& scene_graph, FrameGraph& frame_graph); void Resize(std::uint32_t width, std::uint32_t height); std::shared_ptr CreateTexturePool(); std::shared_ptr CreateMaterialPool(std::size_t size_in_bytes); std::shared_ptr CreateModelPool(std::size_t vertex_buffer_pool_size_in_bytes, std::size_t index_buffer_pool_size_in_bytes); std::shared_ptr CreateConstantBufferPool(std::size_t size_in_bytes); std::shared_ptr CreateStructuredBufferPool(std::size_t size_in_bytes); std::shared_ptr GetDefaultTexturePool(); void PrepareRootSignatureRegistry(); void PrepareShaderRegistry(); void PreparePipelineRegistry(); void PrepareRTPipelineRegistry(); void ReloadPipelineRegistryEntry(RegistryHandle handle); void ReloadRTPipelineRegistryEntry(RegistryHandle handle); void ReloadShaderRegistryEntry(RegistryHandle handle); void ReloadRootSignatureRegistryEntry(RegistryHandle handle); void DestroyRootSignatureRegistry(); void DestroyShaderRegistry(); void DestroyPipelineRegistry(); void DestroyRTPipelineRegistry(); void WaitForAllPreviousWork(); wr::CommandList* GetDirectCommandList(unsigned int num_allocators); wr::CommandList* GetBundleCommandList(unsigned int num_allocators); wr::CommandList* GetComputeCommandList(unsigned int num_allocators); wr::CommandList* GetCopyCommandList(unsigned int num_allocators); void SetCommandListName(CommandList* cmd_list, std::wstring const& name); void DestroyCommandList(CommandList* cmd_list); RenderTarget* GetRenderTarget(RenderTargetProperties properties); void SetRenderTargetName(RenderTarget* cmd_list, std::wstring const& name); void ResizeRenderTarget(RenderTarget** render_target, std::uint32_t width, std::uint32_t height); void RequestFullscreenChange(bool fullscreen_state); void DestroyRenderTarget(RenderTarget **render_target) override; void ResetCommandList(CommandList* cmd_list); void CloseCommandList(CommandList* cmd_list); void StartRenderTask(CommandList* cmd_list, std::pair render_target); void StopRenderTask(CommandList* cmd_list, std::pair render_target); void StartComputeTask(CommandList* cmd_list, std::pair render_target); void StopComputeTask(CommandList* cmd_list, std::pair render_target); void StartCopyTask(CommandList* cmd_list, std::pair render_target); void StopCopyTask(CommandList* cmd_list, std::pair render_target); void InitSceneGraph(SceneGraph& scene_graph); void Init_MeshNodes(std::vector>& nodes); void Init_CameraNodes(std::vector>& nodes); void Init_LightNodes(std::vector>& nodes, std::vector& lights); void Update_MeshNodes(std::vector>& nodes); void Update_CameraNodes(std::vector>& nodes); void Update_LightNodes(SceneGraph& scene_graph); void Update_Transforms(SceneGraph& scene_graph, std::shared_ptr& node); void Delete_Skybox(SceneGraph& scene_graph, std::shared_ptr& skybox_node); void PreparePreRenderCommands(bool clear_frame_buffer, int frame_idx); void Render_MeshNodes(temp::MeshBatches& batches, CameraNode* camera, CommandList* cmd_list); void BindMaterial(MaterialHandle material_handle, CommandList* cmd_list); unsigned int GetFrameIdx(); d3d12::RenderWindow* GetRenderWindow(); void RequestSkyboxReload(); //SimpleShapes don't have a material attached to them. The user is expected to provide one. wr::Model* GetSimpleShape(SimpleShapes type); public: d3d12::Device* m_device; std::optional m_render_window; d3d12::CommandQueue* m_direct_queue; d3d12::CommandQueue* m_compute_queue; d3d12::CommandQueue* m_copy_queue; std::array m_fences; d3d12::Viewport m_viewport; d3d12::CommandList* m_direct_cmd_list; d3d12::StagingBuffer* m_fullscreen_quad_vb; std::vector m_buffer_frame_graph_uids; std::vector > m_texture_pools; d3d12::HeapResource* m_light_buffer; std::shared_ptr m_camera_pool; // TODO: Should be part of the scene graph std::shared_ptr m_raytracing_cb_pool; std::shared_ptr m_raytracing_material_sb_pool; std::shared_ptr m_raytracing_offset_sb_pool; std::vector> m_structured_buffer_pools; std::vector> m_model_pools; D3D12ModelPool* m_bound_model_pool; std::size_t m_bound_model_pool_stride; std::optional m_brdf_lut = std::nullopt; bool m_brdf_lut_generated = false; float temp_metal = 1.0f; float temp_rough = -3; bool clear_path = false; float light_radius = 50; float temp_intensity = 1; protected: void SaveRenderTargetToDisc(std::string const& path, RenderTarget* render_target, unsigned int index); private: void ResetBatches(SceneGraph& sg); void LoadPrimitiveShapes(); void CreateDefaultResources(); d3d12::CommandSignature* m_cmd_signature; d3d12::CommandSignature* m_cmd_signature_indexed; std::optional m_requested_fullscreen_state; MaterialHandle m_last_material = { nullptr, 0 }; bool m_skybox_changed = false; }; } /* wr */ ================================================ FILE: src/d3d12/d3d12_resource_pool_texture.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* The mipmapping implementation used in this framework is a ported version of MiniEngine's implementation. */ /* The MIT License(MIT) Copyright(c) 2013 - 2015 Microsoft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "d3d12_resource_pool_texture.hpp" #include "d3d12_functions.hpp" #include "d3d12_defines.hpp" #include "d3d12_renderer.hpp" #include "d3d12_defines.hpp" #include "../renderer.hpp" #include "../settings.hpp" #include "d3d12_renderer.hpp" #include "d3d12_structs.hpp" #include "../pipeline_registry.hpp" #include "d3d12_descriptors_allocations.hpp" #include #include namespace wr { D3D12TexturePool::D3D12TexturePool(D3D12RenderSystem& render_system) : TexturePool() , m_render_system(render_system) { auto device = m_render_system.m_device; m_staging_textures.resize(d3d12::settings::num_back_buffers); //Staging heap for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { m_allocators[i] = new DescriptorAllocator(render_system, static_cast(i)); } m_mipmapping_allocator = new DescriptorAllocator(render_system, DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); // Default UAVs m_default_uav = m_mipmapping_allocator->Allocate(4); for (uint8_t i = 0; i < 4; ++i) { D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {}; uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; uavDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; uavDesc.Texture2D.MipSlice = i; uavDesc.Texture2D.PlaneSlice = 0; d3d12::DescHeapCPUHandle handle = m_default_uav.GetDescriptorHandle(i); device->m_native->CreateUnorderedAccessView(nullptr, nullptr, &uavDesc, handle.m_native); } } D3D12TexturePool::~D3D12TexturePool() { { //Let the allocation go out of scope to clear it before the texture pool and its allocators are destroyed DescriptorAllocation alloc = std::move(m_default_uav); } delete m_mipmapping_allocator; for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { delete m_allocators[i]; } while (m_unstaged_textures.size() > 0) { uint64_t texture_id = m_unstaged_textures.begin()->first; d3d12::TextureResource* texture = static_cast(m_unstaged_textures.at(texture_id).first); m_unstaged_textures.erase(texture_id); SAFE_RELEASE(texture->m_resource); SAFE_RELEASE(texture->m_intermediate); delete texture; } while(m_staged_textures.size() > 0) { TextureHandle handle = { this, static_cast(m_staged_textures.begin()->first) }; D3D12TexturePool::MarkForUnload(handle, 0); } D3D12TexturePool::UnloadTextures(0); } void D3D12TexturePool::Evict() { } void D3D12TexturePool::MakeResident() { } void D3D12TexturePool::Stage(CommandList* cmd_list) { size_t unstaged_number = m_unstaged_textures.size(); if (unstaged_number > 0) { d3d12::CommandList* cmdlist = static_cast(cmd_list); std::vector unstaged_textures; std::vector need_mipmapping; auto itr = m_unstaged_textures.begin(); for (itr; itr != m_unstaged_textures.end(); ++itr) { d3d12::TextureResource* texture = static_cast(itr->second.first); unstaged_textures.push_back(texture); decltype(d3d12::Device::m_native) n_device; texture->m_resource->GetDevice(IID_PPV_ARGS(&n_device)); DirectX::ScratchImage* image = itr->second.second; texture->m_subresources.resize(image->GetImageCount()); const DirectX::Image* pImages = image->GetImages(); for (int i = 0; i < image->GetImageCount(); ++i) { auto& subresource = texture->m_subresources[i]; subresource.RowPitch = pImages[i].rowPitch; subresource.SlicePitch = pImages[i].slicePitch; subresource.pData = pImages[i].pixels; } std::vector subresources = texture->m_subresources; UpdateSubresources(cmdlist->m_native, texture->m_resource, texture->m_intermediate, 0, 0, static_cast(subresources.size()), subresources.data()); texture->m_is_staged = true; if (texture->m_need_mips) { need_mipmapping.push_back(texture); } } d3d12::Transition(cmdlist, unstaged_textures, wr::ResourceState::COPY_DEST, wr::ResourceState::PIXEL_SHADER_RESOURCE); for (auto* t : need_mipmapping) { GenerateMips(t, cmd_list); } MoveStagedTextures(m_render_system.GetFrameIdx()); } } void D3D12TexturePool::PostStageClear() { } void D3D12TexturePool::ReleaseTemporaryResources() { m_mipmapping_allocator->ReleaseStaleDescriptors(); for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { m_allocators[i]->ReleaseStaleDescriptors(); } unsigned int frame_idx = m_render_system.GetFrameIdx(); //Release temporary textures for (auto* t : m_temporary_textures[frame_idx]) { d3d12::Destroy(t); } m_temporary_textures[frame_idx].clear(); //Release temporary heaps for (auto* h : m_temporary_heaps[frame_idx]) { d3d12::Destroy(h); } for (auto& map : m_staging_textures[frame_idx]) { auto* texture = (d3d12::TextureResource*) map.second; if (texture->m_intermediate) SAFE_RELEASE(texture->m_intermediate); ((d3d12::TextureResource*)m_staged_textures[map.first])->m_intermediate = nullptr; } m_staging_textures[frame_idx].clear(); m_temporary_heaps[frame_idx].clear(); } d3d12::TextureResource* D3D12TexturePool::GetTextureResource(TextureHandle handle) { return static_cast(m_staged_textures.at(handle.m_id)); } TextureHandle D3D12TexturePool::CreateCubemap(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest) { auto device = m_render_system.m_device; d3d12::desc::TextureDesc desc; uint32_t mip_lvl_final = mip_levels; // 0 means generate max number of mip levels if (mip_lvl_final == 0) { mip_lvl_final = static_cast(std::floor(std::log2(std::max(width, height)))) + 1; } desc.m_width = width; desc.m_height = height; desc.m_is_cubemap = true; desc.m_depth = 1; desc.m_array_size = 6; desc.m_mip_levels = mip_lvl_final; desc.m_texture_format = format; desc.m_initial_state = allow_render_dest ? ResourceState::RENDER_TARGET : ResourceState::COPY_DEST; d3d12::TextureResource* texture = d3d12::CreateTexture(device, &desc, true); texture->m_allow_render_dest = allow_render_dest; texture->m_is_staged = true; texture->m_need_mips = false; std::wstring wide_string(name.begin(), name.end()); d3d12::SetName(texture, wide_string); DescriptorAllocation srv_alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate(); DescriptorAllocation uav_alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate(); if (srv_alloc.IsNull()) { LOGC("Couldn't allocate descriptor for the texture resource"); } if (uav_alloc.IsNull()) { LOGC("Couldn't allocate descriptor for the texture resource"); } texture->m_srv_allocation = std::move(srv_alloc); texture->m_uav_allocation = std::move(uav_alloc); d3d12::CreateSRVFromTexture(texture); if (allow_render_dest) { DescriptorAllocation alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_RTV]->Allocate(6); if (alloc.IsNull()) { LOGC("Couldn't allocate descriptor for the texture resource"); } texture->m_rtv_allocation = std::move(alloc); d3d12::CreateRTVFromCubemap(texture); } m_loaded_textures++; uint64_t texture_id = m_id_factory.GetUnusedID(); TextureHandle texture_handle; texture_handle.m_pool = this; texture_handle.m_id = static_cast(texture_id); m_staged_textures.insert(std::make_pair(texture_id, texture)); return texture_handle; } TextureHandle D3D12TexturePool::CreateTexture(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest) { auto device = m_render_system.m_device; d3d12::desc::TextureDesc desc; uint32_t mip_lvl_final = mip_levels; // 0 means generate max number of mip levels if (mip_lvl_final == 0) { mip_lvl_final = static_cast(std::floor(std::log2(std::max(width, height)))) + 1; } desc.m_width = width; desc.m_height = height; desc.m_is_cubemap = false; desc.m_depth = 1; desc.m_array_size = 1; desc.m_mip_levels = mip_lvl_final; desc.m_texture_format = format; desc.m_initial_state = allow_render_dest ? ResourceState::RENDER_TARGET : ResourceState::COPY_DEST; d3d12::TextureResource* texture = d3d12::CreateTexture(device, &desc, true); texture->m_allow_render_dest = allow_render_dest; texture->m_is_staged = true; texture->m_need_mips = false; std::wstring wide_string(name.begin(), name.end()); d3d12::SetName(texture, wide_string); DescriptorAllocation srv_alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate(); DescriptorAllocation uav_alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate(); if (srv_alloc.IsNull()) { LOGC("Couldn't allocate descriptor for the texture resource"); } if (uav_alloc.IsNull()) { LOGC("Couldn't allocate descriptor for the texture resource"); } texture->m_srv_allocation = std::move(srv_alloc); texture->m_uav_allocation = std::move(uav_alloc); d3d12::CreateSRVFromTexture(texture); d3d12::CreateUAVFromTexture(texture, 0); if (allow_render_dest) { DescriptorAllocation alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_RTV]->Allocate(6); if (alloc.IsNull()) { LOGC("Couldn't allocate descriptor for the texture resource"); } texture->m_rtv_allocation = std::move(alloc); d3d12::CreateRTVFromTexture2D(texture); } m_loaded_textures++; uint64_t texture_id = m_id_factory.GetUnusedID(); TextureHandle texture_handle; texture_handle.m_pool = this; texture_handle.m_id = static_cast(texture_id); m_staged_textures.insert(std::make_pair(texture_id, texture)); return texture_handle; } DescriptorAllocator * D3D12TexturePool::GetAllocator(DescriptorHeapType type) { return m_allocators[static_cast(type)]; } void D3D12TexturePool::MarkForUnload(TextureHandle& handle, unsigned int frame_idx) { uint64_t texture_id = handle.m_id; d3d12::TextureResource* texture = static_cast(m_staged_textures.at(texture_id)); m_staged_textures.erase(texture_id); m_staging_textures.at(frame_idx).erase(texture_id); m_marked_for_unload.at(frame_idx).push_back(texture); #ifdef _DEBUG LOGW("[DEBUG MESSAGE]: Handle {} from {} invalidated.", texture_id, m_name); #endif handle.m_pool = nullptr; handle.m_id = -UINT_MAX; } void D3D12TexturePool::UnloadTextures(unsigned int frame_idx) { auto& vec = m_marked_for_unload.at(frame_idx); for (auto* texture : vec) { SAFE_RELEASE(texture->m_resource); if (texture->m_intermediate) SAFE_RELEASE(texture->m_intermediate); delete texture; } m_marked_for_unload.at(frame_idx).clear(); } TextureHandle D3D12TexturePool::LoadFromFile(std::string_view path, bool srgb, bool generate_mips) { auto device = m_render_system.m_device; DirectX::TexMetadata metadata; DirectX::ScratchImage* image = new DirectX::ScratchImage; std::optional extension = util::GetFileExtension(path); std::wstring wide_string(path.begin(), path.end()); if (std::string_view ext_int = extension.value(); extension.has_value()) { HRESULT hr = S_OK; if (ext_int.find("png") != std::string_view::npos || ext_int.find("jpeg") != std::string_view::npos || ext_int.find("jpg") != std::string_view::npos || ext_int.find("bmp") != std::string_view::npos) { hr = LoadFromWICFile(wide_string.c_str(), DirectX::WIC_FLAGS_NONE, &metadata, *image); } else if (ext_int.find("dds") != std::string_view::npos) { hr = LoadFromDDSFile(wide_string.c_str(), DirectX::DDS_FLAGS_NONE, &metadata, *image); } else if (ext_int.find("hdr") != std::string_view::npos) { hr = LoadFromHDRFile(wide_string.c_str(), &metadata, *image); } else if (ext_int.find("tga") != std::string_view::npos) { hr = DirectX::LoadFromTGAFile(wide_string.c_str(), &metadata, *image); } else { // Return an invalid texture handle when format is not supported LOGE("Texture {} not loaded. Format not supported.", path); return {}; } if (FAILED(hr)) { _com_error err(hr); LPCTSTR errMsg = err.ErrorMessage(); // Return an invalid texture handle when texture couldn't be loaded LOGE("ERROR: DirectXTex error: {}", errMsg); return {}; } } uint32_t mip_lvls; bool mip_generation = (metadata.mipLevels > 1) ? false : generate_mips; if (mip_generation) { mip_lvls = static_cast(std::floor(std::log2(std::max(metadata.width, metadata.height)))) + 1; } else { mip_lvls = static_cast(metadata.mipLevels); } Format texture_format = static_cast(metadata.format); d3d12::desc::TextureDesc desc; desc.m_width = static_cast(metadata.width); desc.m_height = static_cast(metadata.height); desc.m_is_cubemap = metadata.IsCubemap(); desc.m_depth = static_cast(metadata.depth); desc.m_array_size = static_cast(metadata.arraySize); desc.m_mip_levels = mip_lvls; desc.m_texture_format = texture_format; desc.m_initial_state = ResourceState::COPY_DEST; d3d12::TextureResource* texture = d3d12::CreateTexture(device, &desc, mip_generation); DescriptorAllocation alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate(); if (alloc.IsNull()) { LOGC("Couldn't allocate descriptor for the texture resource"); } texture->m_need_mips = mip_generation; texture->m_srv_allocation = std::move(alloc); texture->m_resource->SetName(wide_string.c_str()); d3d12::CreateSRVFromTexture(texture); m_loaded_textures++; LOG("[TEXTURE LOADED] {}", path); uint64_t texture_id = m_id_factory.GetUnusedID(); TextureHandle texture_handle; texture_handle.m_pool = this; texture_handle.m_id = static_cast(texture_id); m_unstaged_textures.insert(std::make_pair(texture_id, std::make_pair(texture, image))); return texture_handle; } TextureHandle D3D12TexturePool::LoadFromMemory(unsigned char* data, size_t width, size_t height, TextureFormat type, bool srgb, bool generate_mips) { auto device = m_render_system.m_device; DirectX::TexMetadata metadata; DirectX::ScratchImage* image = new DirectX::ScratchImage; HRESULT hr; switch (type) { case wr::TextureFormat::WIC: { //Assuming RGBA8_UNORM for the time being, need to find a solution hr = LoadFromWICMemory(data, width * height * 8/*bits*/ * 4/*channels*/, DirectX::WIC_FLAGS_NONE, &metadata, *image); break; } case wr::TextureFormat::DDS: { //Assuming RGBA8_UNORM for the time being, need to find a solution hr = LoadFromDDSMemory(data, width * height * 8/*bits*/ * 4/*channels*/, DirectX::DDS_FLAGS_NONE, &metadata, *image); break; } case wr::TextureFormat::HDR: { //Assuming RGBA16_FLOAT for the time being, need to find a solution hr = LoadFromHDRMemory(data, width * height * 16/*bits*/ * 4/*channels*/, &metadata, *image); break; } case wr::TextureFormat::RAW: { DirectX::Image temp_image = {}; temp_image.format = srgb ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM; temp_image.width = width; temp_image.height = height; temp_image.rowPitch = width * 4; temp_image.slicePitch = width * height * 4; temp_image.pixels = data; hr = image->InitializeFromImage(temp_image); metadata.width = width; metadata.height = height; metadata.arraySize = 1; metadata.format = srgb ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM; metadata.mipLevels = 1; metadata.depth = 1; metadata.dimension = DirectX::TEX_DIMENSION::TEX_DIMENSION_TEXTURE2D; metadata.miscFlags = 0; metadata.miscFlags2 = 0; break; } default: LOGC("[ERROR]: How did we even get here?") break; } if (FAILED(hr)) { _com_error err(hr); LPCTSTR errMsg = err.ErrorMessage(); LOGC("ERROR: DirectXTex error: {}", errMsg); } uint32_t mip_lvls; bool mip_generation = (metadata.mipLevels > 1) ? false : generate_mips; if (mip_generation) { mip_lvls = static_cast(std::floor(std::log2(std::max(metadata.width, metadata.height)))) + 1; } else { mip_lvls = static_cast(metadata.mipLevels); } Format texture_format = static_cast(metadata.format); d3d12::desc::TextureDesc desc; desc.m_width = static_cast(metadata.width); desc.m_height = static_cast(metadata.height); desc.m_is_cubemap = metadata.IsCubemap(); desc.m_depth = static_cast(metadata.depth); desc.m_array_size = static_cast(metadata.arraySize); desc.m_mip_levels = mip_lvls; desc.m_texture_format = texture_format; desc.m_initial_state = ResourceState::COPY_DEST; d3d12::TextureResource* texture = d3d12::CreateTexture(device, &desc, mip_generation); DescriptorAllocation alloc = m_allocators[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->Allocate(); if (alloc.IsNull()) { LOGC("Couldn't allocate descriptor for the texture resource"); } texture->m_need_mips = mip_generation; texture->m_srv_allocation = std::move(alloc); std::wstring name = L"TextureFromMemory" + std::to_wstring(m_loaded_textures); texture->m_resource->SetName(name.c_str()); d3d12::CreateSRVFromTexture(texture); m_loaded_textures++; LOG("[TEXTURE LOADED]: Texture from Memory"); uint64_t texture_id = m_id_factory.GetUnusedID(); TextureHandle texture_handle; texture_handle.m_pool = this; texture_handle.m_id = static_cast(texture_id); m_unstaged_textures.insert(std::make_pair(texture_id, std::make_pair(texture, image))); return texture_handle; } void D3D12TexturePool::MoveStagedTextures(unsigned int frame_idx) { for (auto itr = m_unstaged_textures.begin(); itr != m_unstaged_textures.end(); ++itr) { m_staged_textures.insert(std::make_pair(itr->first, itr->second.first)); m_staging_textures[frame_idx].insert(std::make_pair(itr->first, itr->second.first)); //Free the ScratchImage as it's not needed anymore after staging delete itr->second.second; } m_unstaged_textures.clear(); } void D3D12TexturePool::GenerateMips(d3d12::TextureResource* texture, CommandList* cmd_list) { wr::d3d12::CommandList* d3d12_cmd_list = static_cast(cmd_list); auto pipeline = static_cast(PipelineRegistry::Get().Find(pipelines::mip_mapping)); d3d12::BindComputePipeline(d3d12_cmd_list, pipeline); if (texture->m_need_mips) { if (d3d12::CheckUAVCompatibility(texture->m_format) || d3d12::CheckOptionalUAVFormat(texture->m_format)) { GenerateMips_UAV(texture, cmd_list); } else if (d3d12::CheckBGRFormat(texture->m_format)) { GenerateMips_BGR(texture, cmd_list); } else if (d3d12::CheckSRGBFormat(texture->m_format)) { GenerateMips_SRGB(texture, cmd_list); } else { LOGC("[ERROR]: GenerateMips-> I don't know how we ended up here!"); } } } void D3D12TexturePool::GenerateMips_UAV(d3d12::TextureResource* texture, CommandList* cmd_list) { wr::d3d12::CommandList* d3d12_cmd_list = static_cast(cmd_list); //Create shader resource view for the source texture in the descriptor heap DescriptorAllocation srv_alloc = m_mipmapping_allocator->Allocate(); d3d12::DescHeapCPUHandle srv_handle = srv_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromTexture(texture, srv_handle); MipMapping_CB generate_mips_cb; for (uint32_t src_mip = 0; src_mip < texture->m_mip_levels - 1u;) { uint32_t src_width = static_cast(texture->m_width) >> src_mip; uint32_t src_height = static_cast(texture->m_height) >> src_mip; uint32_t dst_width = src_width >> 1; uint32_t dst_height = src_height >> 1; // Determine the compute shader to use based on the dimension of the // source texture. // 0b00(0): Both width and height are even. // 0b01(1): Width is odd, height is even. // 0b10(2): Width is even, height is odd. // 0b11(3): Both width and height are odd. generate_mips_cb.src_dimension = (src_height & 1) << 1 | (src_width & 1); // Max amount of mip levels to compute in a pass is 4. DWORD mip_count; // The number of times we can half the size of the texture and get // exactly a 50% reduction in size. // A 1 bit in the width or height indicates an odd dimension. // The case where either the width or the height is exactly 1 is handled // as a special case (as the dimension does not require reduction). _BitScanForward(&mip_count, (dst_width == 1 ? dst_height : dst_width) | (dst_height == 1 ? dst_width : dst_height)); // Maximum number of mips to generate is 4. mip_count = std::min(4, mip_count + 1); // Clamp to total number of mips left over. mip_count = ((static_cast(src_mip) + mip_count) > texture->m_mip_levels) ? static_cast(texture->m_mip_levels - src_mip) : mip_count; // Dimensions should not reduce to 0. // This can happen if the width and height are not the same. dst_width = std::max(1, dst_width); dst_height = std::max(1, dst_height); generate_mips_cb.src_mip_level = src_mip; generate_mips_cb.num_mip_levels = mip_count; generate_mips_cb.texel_size.x = 1.0f / (float)dst_width; generate_mips_cb.texel_size.y = 1.0f / (float)dst_height; d3d12::BindCompute32BitConstants(d3d12_cmd_list, &generate_mips_cb, sizeof(MipMapping_CB) / sizeof(uint32_t), 0, 0); d3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[src_mip], ResourceState::PIXEL_SHADER_RESOURCE, src_mip, 1); d3d12::SetShaderSRV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::SOURCE)), srv_handle); for (uint32_t mip = 0; mip < mip_count; ++mip) { std::uint32_t idx = src_mip + mip + 1u; d3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[idx], ResourceState::UNORDERED_ACCESS, idx, 1); DescriptorAllocation uav_alloc = m_mipmapping_allocator->Allocate(); d3d12::DescHeapCPUHandle uav_handle = uav_alloc.GetDescriptorHandle(); d3d12::CreateUAVFromTexture(texture, uav_handle, src_mip + mip + 1); d3d12::SetShaderUAV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)) + mip, uav_handle); } // Pad any unused mip levels with a default UAV to keeps the DX12 runtime happy. if (mip_count < 4) { d3d12_cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors( COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)), mip_count + 1, 4 - mip_count, m_default_uav.GetDescriptorHandle()); } d3d12::Dispatch(d3d12_cmd_list, ((dst_width + 8 - 1) / 8), ((dst_height + 8 - 1) / 8), 1u); //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 d3d12::UAVBarrier(d3d12_cmd_list, { texture }); for (uint32_t mip = 0; mip < mip_count; ++mip) { std::uint32_t idx = src_mip + mip + 1u; d3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[idx], ResourceState::PIXEL_SHADER_RESOURCE, idx, 1); } src_mip += mip_count; } texture->m_need_mips = false; } void D3D12TexturePool::GenerateMips_BGR(d3d12::TextureResource* texture, CommandList* cmd_list) { auto device = m_render_system.m_device; wr::d3d12::CommandList* d3d12_cmd_list = static_cast(cmd_list); unsigned int frame_idx = m_render_system.GetFrameIdx(); //Create new resource with UAV compatible format d3d12::desc::TextureDesc copy_desc; copy_desc.m_width = static_cast(texture->m_width); copy_desc.m_height = static_cast(texture->m_height); copy_desc.m_depth = static_cast(texture->m_depth); copy_desc.m_array_size = static_cast(texture->m_array_size); copy_desc.m_initial_state = ResourceState::COMMON; copy_desc.m_is_cubemap = texture->m_is_cubemap; copy_desc.m_mip_levels = static_cast(texture->m_mip_levels); copy_desc.m_texture_format = Format::R8G8B8A8_UNORM; // Create a heap to alias the resource. This is used to copy the resource without // failing GPU validation. auto resource = texture->m_resource; auto resourceDesc = resource->GetDesc(); auto allocation_info = device->m_native->GetResourceAllocationInfo(0, 1, &resourceDesc); d3d12::Heap* heap; heap = d3d12::CreateHeap_BSBO(device, allocation_info.SizeInBytes, ResourceType::TEXTURE, 1); m_temporary_heaps[frame_idx].push_back(heap); //Create the copy resource placed in the heap. d3d12::TextureResource* copy_texture = d3d12::CreatePlacedTexture(device, ©_desc, true, heap); m_temporary_textures[frame_idx].push_back(copy_texture); // Create an alias for which to perform the copy operation. d3d12::desc::TextureDesc alias_desc = copy_desc; alias_desc.m_texture_format = (texture->m_format == Format::B8G8R8X8_UNORM || texture->m_format == Format::B8G8R8X8_UNORM_SRGB) ? Format::B8G8R8X8_UNORM : Format::B8G8R8A8_UNORM; d3d12::TextureResource* alias_texture = d3d12::CreatePlacedTexture(device, &alias_desc, true, heap); m_temporary_textures[frame_idx].push_back(alias_texture); d3d12::Alias(d3d12_cmd_list, nullptr, alias_texture); //Copy resource d3d12::CopyResource(d3d12_cmd_list, texture, alias_texture); // Alias the UAV compatible texture back. d3d12::Alias(d3d12_cmd_list, alias_texture, copy_texture); //Now use the resource copy to generate the mips GenerateMips_UAV(copy_texture, cmd_list); // Copy back to the original (via the alias to ensure GPU validation) d3d12::Alias(d3d12_cmd_list, copy_texture, alias_texture); d3d12::CopyResource(d3d12_cmd_list, alias_texture, texture); } void D3D12TexturePool::GenerateMips_SRGB(d3d12::TextureResource* texture, CommandList* cmd_list) { auto device = m_render_system.m_device; wr::d3d12::CommandList* d3d12_cmd_list = static_cast(cmd_list); //Create copy of the texture and store it for later deletion. d3d12::desc::TextureDesc copy_desc; copy_desc.m_width = static_cast(texture->m_width); copy_desc.m_height = static_cast(texture->m_height); copy_desc.m_is_cubemap = texture->m_is_cubemap; copy_desc.m_depth = static_cast(texture->m_depth); copy_desc.m_array_size = static_cast(texture->m_array_size); copy_desc.m_mip_levels = static_cast(texture->m_mip_levels); copy_desc.m_texture_format = d3d12::RemoveSRGB(texture->m_format); copy_desc.m_initial_state = ResourceState::COPY_DEST; auto texture_copy = d3d12::CreateTexture(device, ©_desc, true); unsigned int frame_idx = m_render_system.GetFrameIdx(); m_temporary_textures[frame_idx].push_back(texture_copy); d3d12::CopyResource(d3d12_cmd_list, texture, texture_copy); GenerateMips(texture_copy, cmd_list); d3d12::CopyResource(d3d12_cmd_list, texture_copy, texture); } void D3D12TexturePool::GenerateMips_Cubemap(d3d12::TextureResource* texture, CommandList* cmd_list, unsigned int array_slice) { wr::d3d12::CommandList* d3d12_cmd_list = static_cast(cmd_list); //Create shader resource view for the source texture in the descriptor heap DescriptorAllocation srv_alloc = m_mipmapping_allocator->Allocate(); d3d12::DescHeapCPUHandle srv_handle = srv_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromCubemapFace(texture, srv_handle, static_cast(texture->m_mip_levels), 0, array_slice); MipMapping_CB generate_mips_cb; for (uint32_t src_mip = 0; src_mip < texture->m_mip_levels - 1u;) { uint32_t src_width = static_cast(texture->m_width) >> src_mip; uint32_t src_height = static_cast(texture->m_height) >> src_mip; uint32_t dst_width = src_width >> 1; uint32_t dst_height = src_height >> 1; // Determine the compute shader to use based on the dimension of the // source texture. // 0b00(0): Both width and height are even. // 0b01(1): Width is odd, height is even. // 0b10(2): Width is even, height is odd. // 0b11(3): Both width and height are odd. generate_mips_cb.src_dimension = (src_height & 1) << 1 | (src_width & 1); // Max amount of mip levels to compute in a pass is 4. DWORD mip_count; // The number of times we can half the size of the texture and get // exactly a 50% reduction in size. // A 1 bit in the width or height indicates an odd dimension. // The case where either the width or the height is exactly 1 is handled // as a special case (as the dimension does not require reduction). _BitScanForward(&mip_count, (dst_width == 1 ? dst_height : dst_width) | (dst_height == 1 ? dst_width : dst_height)); // Maximum number of mips to generate is 4. mip_count = std::min(4, mip_count + 1); // Clamp to total number of mips left over. mip_count = ((static_cast(src_mip) + mip_count) > texture->m_mip_levels) ? static_cast(texture->m_mip_levels - src_mip) : mip_count; // Dimensions should not reduce to 0. // This can happen if the width and height are not the same. dst_width = std::max(1, dst_width); dst_height = std::max(1, dst_height); generate_mips_cb.src_mip_level = src_mip; generate_mips_cb.num_mip_levels = mip_count; generate_mips_cb.texel_size.x = 1.0f / (float)dst_width; generate_mips_cb.texel_size.y = 1.0f / (float)dst_height; d3d12::BindCompute32BitConstants(d3d12_cmd_list, &generate_mips_cb, sizeof(MipMapping_CB) / sizeof(uint32_t), 0, 0); d3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[src_mip], ResourceState::PIXEL_SHADER_RESOURCE, src_mip, 1); d3d12::SetShaderSRV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::SOURCE)), srv_handle); for (uint32_t mip = 0; mip < mip_count; ++mip) { uint32_t idx = src_mip + mip + 1u; d3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[idx], ResourceState::UNORDERED_ACCESS, idx, 1); DescriptorAllocation uav_alloc = m_mipmapping_allocator->Allocate(); d3d12::DescHeapCPUHandle uav_handle = uav_alloc.GetDescriptorHandle(); d3d12::CreateUAVFromCubemapFace(texture, uav_handle, src_mip + mip + 1, array_slice); d3d12::SetShaderUAV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)) + mip, uav_handle); } // Pad any unused mip levels with a default UAV to keeps the DX12 runtime happy. if (mip_count < 4) { d3d12_cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors( COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)), mip_count + 1, 4 - mip_count, m_default_uav.GetDescriptorHandle()); } d3d12::Dispatch(d3d12_cmd_list, ((dst_width + 8 - 1) / 8), ((dst_height + 8 - 1) / 8), 1u); //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 d3d12::UAVBarrier(d3d12_cmd_list, { texture }); for (uint32_t mip = 0; mip < mip_count; ++mip) { uint32_t idx = src_mip + mip + 1u; d3d12::Transition(d3d12_cmd_list, texture, texture->m_subresource_states[idx], ResourceState::PIXEL_SHADER_RESOURCE, idx, 1); } src_mip += mip_count; } texture->m_need_mips = false; } } ================================================ FILE: src/d3d12/d3d12_resource_pool_texture.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* The mipmapping implementation used in this framework is a ported version of MiniEngine's implementation. */ /* The MIT License(MIT) Copyright(c) 2013 - 2015 Microsoft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #include "../resource_pool_texture.hpp" #include "d3d12_enums.hpp" #include "d3d12_texture_resources.hpp" #include namespace wr { struct MipMapping_CB { uint32_t src_mip_level; // Texture level of source mip uint32_t num_mip_levels; // Number of OutMips to write: [1-4] uint32_t src_dimension; // Width and height of the source texture are even or odd. uint32_t padding; // Pad to 16 byte alignment. DirectX::XMFLOAT2 texel_size; // 1.0 / OutMip1.Dimensions }; class D3D12RenderSystem; class DescriptorAllocator; class D3D12TexturePool : public TexturePool { public: explicit D3D12TexturePool(D3D12RenderSystem& render_system); ~D3D12TexturePool() final; void Evict() final; void MakeResident() final; void Stage(CommandList* cmd_list) final; void PostStageClear() final; void ReleaseTemporaryResources() final; d3d12::TextureResource* GetTextureResource(TextureHandle handle) final; //! Loads a texture from file. /*! \param path std::string that contains a path to the texture file location. \param srgb Defines if the texture should be loaded as srgb texture or not (currently not supported). \param generate_mips Defines if mipmaps should be created. \return The texture handle to the loaded texture. \sa wr::TextureHandle */ [[nodiscard]] TextureHandle LoadFromFile(std::string_view path, bool srgb, bool generate_mips) final; [[nodiscard]] TextureHandle LoadFromMemory(unsigned char* data, size_t width, size_t height, TextureFormat type, bool srgb, bool generate_mips) final; [[nodiscard]] TextureHandle CreateCubemap(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest) final; [[nodiscard]] TextureHandle CreateTexture(std::string_view name, uint32_t width, uint32_t height, uint32_t mip_levels, Format format, bool allow_render_dest) final; DescriptorAllocator* GetAllocator(DescriptorHeapType type); DescriptorAllocator* GetMipmappingAllocator() { return m_mipmapping_allocator; } void MarkForUnload(TextureHandle& handle, unsigned int frame_idx) final; void UnloadTextures(unsigned int frame_idx) final; void GenerateMips_Cubemap(d3d12::TextureResource* texture, CommandList* cmd_list, unsigned int array_slice); protected: void MoveStagedTextures(unsigned int frame_idx); void GenerateMips(d3d12::TextureResource* texture, CommandList* cmd_list); void GenerateMips_UAV(d3d12::TextureResource* texture, CommandList* cmd_list); void GenerateMips_BGR(d3d12::TextureResource* texture, CommandList* cmd_list); void GenerateMips_SRGB(d3d12::TextureResource* texture, CommandList* cmd_list); //Unstaged textures are stored as pairs in a map. This removes the necessity of having a ScratchImage //pointer in the Texture struct. Once the textures are staged the ScratchImages are deleted. using UnstagedTextures = std::unordered_map>; UnstagedTextures m_unstaged_textures; using StagedTextures = std::unordered_map; StagedTextures m_staged_textures; std::vector m_staging_textures; D3D12RenderSystem& m_render_system; //CPU only visible heaps used for staging of descriptors. //Renderer will copy the descriptor it needs to the GPU visible heap used for rendering. DescriptorAllocator* m_allocators[static_cast(DescriptorHeapType::DESC_HEAP_TYPE_NUM_TYPES)]; DescriptorAllocator* m_mipmapping_allocator; //Track resources that are created in one frame and destroyed after std::array, d3d12::settings::num_back_buffers> m_temporary_textures; std::array< std::vector*>, d3d12::settings::num_back_buffers> m_temporary_heaps; //Resources marked for deletion std::array, d3d12::settings::num_back_buffers> m_marked_for_unload; //Default UAV used for padding DescriptorAllocation m_default_uav; }; } ================================================ FILE: src/d3d12/d3d12_root_signature.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "d3d12_defines.hpp" namespace wr::d3d12 { RootSignature* CreateRootSignature(desc::RootSignatureDesc create_info) { auto root_signature = new RootSignature(); root_signature->m_create_info = create_info; return root_signature; } void SetName(RootSignature * root_signature, std::wstring name) { root_signature->m_native->SetName(name.c_str()); } void FinalizeRootSignature(RootSignature* root_signature, Device* device) { auto n_device = device->m_native; auto n_fb_device = device->m_fallback_native; auto create_info = root_signature->m_create_info; // Check capabilities TODO: Actually use capabilities. D3D12_FEATURE_DATA_ROOT_SIGNATURE feature_data = {}; feature_data.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1; if (FAILED(device->m_native->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &feature_data, sizeof(feature_data)))) { feature_data.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_0; } auto num_samplers = static_cast(create_info.m_samplers.size()); std::vector samplers(num_samplers); for (auto i = 0u; i < num_samplers; i++) { auto sampler_info = create_info.m_samplers[i]; samplers[i].Filter = (D3D12_FILTER)sampler_info.m_filter; samplers[i].AddressU = (D3D12_TEXTURE_ADDRESS_MODE)sampler_info.m_address_mode; samplers[i].AddressV = (D3D12_TEXTURE_ADDRESS_MODE)sampler_info.m_address_mode; samplers[i].AddressW = (D3D12_TEXTURE_ADDRESS_MODE)sampler_info.m_address_mode; samplers[i].MipLODBias = 0; samplers[i].MaxAnisotropy = 0; samplers[i].ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER; samplers[i].BorderColor = (D3D12_STATIC_BORDER_COLOR)sampler_info.m_border_color; samplers[i].MinLOD = 0.0f; samplers[i].MaxLOD = D3D12_FLOAT32_MAX; samplers[i].ShaderRegister = i; samplers[i].RegisterSpace = 0; samplers[i].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL; // TODO: very inneficient. plz fix } D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT; if (create_info.m_rt_local) { flags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE; } CD3DX12_ROOT_SIGNATURE_DESC root_signature_desc; root_signature_desc.Init(static_cast(create_info.m_parameters.size()), create_info.m_parameters.data(), num_samplers, samplers.data(), flags); //Create root signature bit masks, used for dynamic gpu descriptor heaps auto num_params = create_info.m_parameters.size(); for (size_t i = 0; i < num_params; ++i) { auto param = create_info.m_parameters.at(i); if (param.ParameterType == D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE) { auto num_desc_ranges = param.DescriptorTable.NumDescriptorRanges; // Set the bit mask depending on the type of descriptor table. if (num_desc_ranges > 0) { switch (param.DescriptorTable.pDescriptorRanges[0].RangeType) { case D3D12_DESCRIPTOR_RANGE_TYPE_CBV: case D3D12_DESCRIPTOR_RANGE_TYPE_SRV: case D3D12_DESCRIPTOR_RANGE_TYPE_UAV: root_signature->m_descriptor_table_bit_mask |= (1 << i); break; case D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER: root_signature->m_sampler_table_bit_mask |= (1 << i); break; } } for (size_t j = 0; j < num_desc_ranges; ++j) { root_signature->m_num_descriptors_per_table[i] += param.DescriptorTable.pDescriptorRanges[j].NumDescriptors; } } } if (create_info.m_rtx && GetRaytracingType(device) == RaytracingType::FALLBACK) { // Fallback ID3DBlob* signature; ID3DBlob* error = nullptr; HRESULT hr = n_fb_device->D3D12SerializeRootSignature(&root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error); //TODO: FIX error parameter if (FAILED(hr)) { LOGC("Failed to serialize root signature. Error: \n {}", (char*)error->GetBufferPointer()); } TRY_M(n_fb_device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&root_signature->m_native)), "Failed to create root signature"); } else { ID3DBlob* signature; ID3DBlob* error = nullptr; HRESULT hr = D3D12SerializeRootSignature(&root_signature_desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error); //TODO: FIX error parameter if (FAILED(hr)) { char * err = (char*)error->GetBufferPointer(); LOGC("Failed to serialize root signature. Error: \n {}", err); } TRY_M(n_device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&root_signature->m_native)), "Failed to create root signature"); } NAME_D3D12RESOURCE(root_signature->m_native); } void RefinalizeRootSignature(RootSignature* root_signature, Device* device) { SAFE_RELEASE(root_signature->m_native) FinalizeRootSignature(root_signature, device); } void Destroy(RootSignature* root_signature) { SAFE_RELEASE(root_signature->m_native); delete root_signature; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_rt_descriptor_heap.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_rt_descriptor_heap.hpp" #include "d3d12_renderer.hpp" #include "d3d12_functions.hpp" #include "../util/log.hpp" namespace wr { RTDescriptorHeap::RTDescriptorHeap(wr::d3d12::Device* device, DescriptorHeapType type, uint32_t num_descriptors_per_heap) : m_desc_heap_type(type) , m_num_descr_per_heap(num_descriptors_per_heap) , m_descriptor_table_bit_mask(0) , m_stale_descriptor_table_bit_mask(0) , m_device(device) { m_current_cpu_desc_handle.m_native.ptr = 0; m_current_gpu_desc_handle.m_native.ptr = 0; m_descriptor_handle_increment_size = m_device->m_native->GetDescriptorHandleIncrementSize((D3D12_DESCRIPTOR_HEAP_TYPE)type); // Allocate space for staging CPU visible descriptors. m_descriptor_handle_cache = std::make_unique(m_num_descr_per_heap); for (size_t i = 0; i < d3d12::settings::num_back_buffers; ++i) { m_num_free_handles[i] = num_descriptors_per_heap; } m_heap = CreateDescriptorHeap(); } RTDescriptorHeap::~RTDescriptorHeap() { delete m_heap; } void RTDescriptorHeap::ParseRootSignature(const d3d12::RootSignature& root_signature) { // If the root signature changes, all descriptors must be (re)bound to the // command list. m_stale_descriptor_table_bit_mask = 0; const auto& root_signature_desc = root_signature.m_create_info; // Get a bit mask that represents the root parameter indices that match the // descriptor heap type for this dynamic descriptor heap. switch (m_desc_heap_type) { case wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV: m_descriptor_table_bit_mask = root_signature.m_descriptor_table_bit_mask; break; case wr::DescriptorHeapType::DESC_HEAP_TYPE_SAMPLER: m_descriptor_table_bit_mask = root_signature.m_sampler_table_bit_mask; break; default: //LOGC("You can't use a different type other than CBC_SRV_UAV or SAMPLER for dynamic descriptor heaps"); break; } uint32_t descriptor_table_bitmask = m_descriptor_table_bit_mask; uint32_t current_offset = 0; DWORD root_idx; size_t num_parameters = root_signature_desc.m_parameters.size(); while (_BitScanForward(&root_idx, descriptor_table_bitmask) && root_idx < num_parameters) { //Since a 32 bit mask is used to represent the descriptor tables in the root signature //I want to break if the root_idx is bigger than 32. If that ever happens, we might consider //switching to a 64bit mask. if (root_idx > 32) { LOGC("A maximum of 32 descriptor tables are supported"); } uint32_t num_descriptors = root_signature.m_num_descriptors_per_table[root_idx]; DescriptorTableCache& descriptorTableCache = m_descriptor_table_cache[root_idx]; descriptorTableCache.m_num_descriptors = num_descriptors; descriptorTableCache.m_base_descriptor = m_descriptor_handle_cache.get() + current_offset; current_offset += num_descriptors; // Flip the descriptor table bit so it's not scanned again for the current index. descriptor_table_bitmask ^= (1 << root_idx); } // Make sure the maximum number of descriptors per descriptor heap has not been exceeded. if (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."); } } void RTDescriptorHeap::StageDescriptors(uint32_t root_param_idx, uint32_t offset, uint32_t num_descriptors, const d3d12::DescHeapCPUHandle src_desc) { if (num_descriptors > m_num_descr_per_heap || root_param_idx >= MaxDescriptorTables) { LOGC("Cannot stage more than the maximum number of descriptors per heap. Cannot stage more than MaxDescriptorTables root parameters"); } DescriptorTableCache& descriptor_table_cache = m_descriptor_table_cache[root_param_idx]; // Check that the number of descriptors to copy does not exceed the number // of descriptors expected in the descriptor table. if ((offset + num_descriptors) > descriptor_table_cache.m_num_descriptors) { LOGC("Number of descriptors exceeds the number of descriptors in the descriptor table."); } D3D12_CPU_DESCRIPTOR_HANDLE* dst_descriptor = (descriptor_table_cache.m_base_descriptor + offset); for (uint32_t i = 0; i < num_descriptors; ++i) { dst_descriptor[i] = CD3DX12_CPU_DESCRIPTOR_HANDLE(src_desc.m_native, i, m_descriptor_handle_increment_size); } // Set the root parameter index bit to make sure the descriptor table // at that index is bound to the command list. m_stale_descriptor_table_bit_mask |= (1 << root_param_idx); } uint32_t RTDescriptorHeap::ComputeStaleDescriptorCount() const { uint32_t num_stale_desc = 0; DWORD i; DWORD stale_desc_bitmask = m_stale_descriptor_table_bit_mask; while (_BitScanForward(&i, stale_desc_bitmask)) { num_stale_desc += m_descriptor_table_cache[i].m_num_descriptors; stale_desc_bitmask ^= (1 << i); } return num_stale_desc; } d3d12::DescriptorHeap* RTDescriptorHeap::CreateDescriptorHeap() { d3d12::desc::DescriptorHeapDesc desc; desc.m_type = m_desc_heap_type; desc.m_num_descriptors = m_num_descr_per_heap; desc.m_shader_visible = true; desc.m_versions = 1; d3d12::DescriptorHeap* descriptor_heap = d3d12::CreateDescriptorHeap(m_device, desc); return descriptor_heap; } void RTDescriptorHeap::CommitStagedDescriptorsForDraw(d3d12::CommandList& cmd_list, unsigned int frame_idx) { // Compute the number of descriptors that need to be copied uint32_t num_descriptors_to_commit = ComputeStaleDescriptorCount(); if (num_descriptors_to_commit > 0) { if (m_num_free_handles[frame_idx] < num_descriptors_to_commit) { LOGC("ERROR: Descriptor Heap for RT is full"); } d3d12::BindDescriptorHeap(&cmd_list, m_heap, m_desc_heap_type, frame_idx, d3d12::GetRaytracingType(m_device) == RaytracingType::FALLBACK); DWORD root_idx; // Scan from LSB to MSB for a bit set in staleDescriptorsBitMask while (_BitScanForward(&root_idx, m_stale_descriptor_table_bit_mask)) { std::uint32_t num_src_desc = m_descriptor_table_cache[root_idx].m_num_descriptors; D3D12_CPU_DESCRIPTOR_HANDLE* pSrcDescriptorHandles = m_descriptor_table_cache[root_idx].m_base_descriptor; D3D12_CPU_DESCRIPTOR_HANDLE pDestDescriptorRangeStarts[] = { m_current_cpu_desc_handle.m_native }; std::uint32_t pDestDescriptorRangeSizes[] = { num_src_desc }; // Copy the staged CPU visible descriptors to the GPU visible descriptor heap. m_device->m_native->CopyDescriptors(1, pDestDescriptorRangeStarts, pDestDescriptorRangeSizes, num_src_desc, pSrcDescriptorHandles, nullptr, static_cast(m_desc_heap_type)); // Set the descriptors on the command list using the passed-in setter function. d3d12::BindDescriptorTable(&cmd_list, m_current_gpu_desc_handle, root_idx); // Offset current CPU and GPU descriptor handles. d3d12::Offset(m_current_cpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size); d3d12::Offset(m_current_gpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size); m_num_free_handles[frame_idx] -= num_src_desc; // Flip the stale bit so the descriptor table is not recopied again unless it is updated with a new descriptor. m_stale_descriptor_table_bit_mask ^= (1 << root_idx); } } } void RTDescriptorHeap::CommitStagedDescriptorsForDispatch(d3d12::CommandList& cmd_list, unsigned int frame_idx) { m_current_gpu_desc_handle = d3d12::GetGPUHandle(m_heap, frame_idx); m_current_cpu_desc_handle = d3d12::GetCPUHandle(m_heap, frame_idx); // Compute the number of descriptors that need to be copied uint32_t num_descriptors_to_commit = ComputeStaleDescriptorCount(); if (num_descriptors_to_commit > 0) { if (m_num_free_handles[frame_idx] < num_descriptors_to_commit) { LOGC("ERROR: Descriptor Heap for RT is full"); } d3d12::BindDescriptorHeap(&cmd_list, m_heap, m_desc_heap_type, frame_idx, d3d12::GetRaytracingType(m_device) == RaytracingType::FALLBACK); DWORD root_idx; // Scan from LSB to MSB for a bit set in staleDescriptorsBitMask while (_BitScanForward(&root_idx, m_stale_descriptor_table_bit_mask)) { std::uint32_t num_src_desc = m_descriptor_table_cache[root_idx].m_num_descriptors - 9; D3D12_CPU_DESCRIPTOR_HANDLE* pSrcDescriptorHandles = m_descriptor_table_cache[root_idx].m_base_descriptor; D3D12_CPU_DESCRIPTOR_HANDLE pDestDescriptorRangeStarts[] = { m_current_cpu_desc_handle.m_native }; std::uint32_t pDestDescriptorRangeSizes[] = { num_src_desc }; // Copy the staged CPU visible descriptors to the GPU visible descriptor heap. m_device->m_native->CopyDescriptors(1, pDestDescriptorRangeStarts, pDestDescriptorRangeSizes, num_src_desc, pSrcDescriptorHandles, nullptr, static_cast(m_desc_heap_type)); // Set the descriptors on the command list using the setter function. d3d12::BindComputeDescriptorTable(&cmd_list, m_current_gpu_desc_handle, root_idx); // Offset current CPU and GPU descriptor handles. d3d12::Offset(m_current_cpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size); d3d12::Offset(m_current_gpu_desc_handle, num_src_desc, m_descriptor_handle_increment_size); m_num_free_handles[frame_idx] -= num_src_desc; // Flip the stale bit so the descriptor table is not recopied again unless it is updated with a new descriptor. m_stale_descriptor_table_bit_mask ^= (1 << root_idx); } } } d3d12::DescHeapGPUHandle RTDescriptorHeap::CopyDescriptor(d3d12::CommandList& cmd_list, d3d12::DescHeapCPUHandle cpu_desc, unsigned int frame_idx) { if (m_num_free_handles[frame_idx] < 1) { LOGC("ERROR: Descriptor Heap for RT is full"); } d3d12::BindDescriptorHeap(&cmd_list, m_heap, m_desc_heap_type, frame_idx, d3d12::GetRaytracingType(m_device) == RaytracingType::FALLBACK); // When updating the descriptor heap on the command list, all descriptor // tables must be (re)recopied to the new descriptor heap (not just // the stale descriptor tables). m_stale_descriptor_table_bit_mask = m_descriptor_table_bit_mask; d3d12::DescHeapGPUHandle h_gpu = m_current_gpu_desc_handle; m_device->m_native->CopyDescriptorsSimple(1, m_current_cpu_desc_handle.m_native, cpu_desc.m_native, static_cast(m_desc_heap_type)); d3d12::Offset(m_current_cpu_desc_handle, 1, m_descriptor_handle_increment_size); d3d12::Offset(m_current_gpu_desc_handle, 1, m_descriptor_handle_increment_size); m_num_free_handles[frame_idx] -= 1; return h_gpu; } void RTDescriptorHeap::Reset(unsigned int frame_idx) { m_current_cpu_desc_handle.m_native = CD3DX12_CPU_DESCRIPTOR_HANDLE(D3D12_DEFAULT); m_current_gpu_desc_handle.m_native = CD3DX12_GPU_DESCRIPTOR_HANDLE(D3D12_DEFAULT); m_num_free_handles[frame_idx] = m_num_descr_per_heap; m_descriptor_table_bit_mask = 0; m_stale_descriptor_table_bit_mask = 0; // Reset the table cache for (int i = 0; i < MaxDescriptorTables; ++i) { m_descriptor_table_cache[i].Reset(); } } } ================================================ FILE: src/d3d12/d3d12_rt_descriptor_heap.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "d3d12_structs.hpp" #include "d3d12_enums.hpp" #include #include #include namespace wr { namespace d3d12 { struct Device; }; class RTDescriptorHeap { public: RTDescriptorHeap(wr::d3d12::Device* device, DescriptorHeapType type, uint32_t num_descriptors_per_heap = 5000); virtual ~RTDescriptorHeap(); /** * Stages a contiguous range of CPU visible descriptors. * Descriptors are not copied to the GPU visible descriptor heap until * the CommitStagedDescriptors function is called. */ void StageDescriptors(uint32_t root_param_idx, uint32_t offset, uint32_t num_descriptors, const d3d12::DescHeapCPUHandle src_desc); /** * Copy all of the staged descriptors to the GPU visible descriptor heap and * bind the descriptor heap and the descriptor tables to the command list. * The passed-in function object is used to set the GPU visible descriptors * on the command list. Two possible functions are: * * Before a draw : ID3D12GraphicsCommandList::SetGraphicsRootDescriptorTable * * Before a dispatch: ID3D12GraphicsCommandList::SetComputeRootDescriptorTable */ void CommitStagedDescriptorsForDraw(d3d12::CommandList& cmd_list, unsigned int frame_idx); void CommitStagedDescriptorsForDispatch(d3d12::CommandList& cmd_list, unsigned int frame_idx); /** * Copies a single CPU visible descriptor to a GPU visible descriptor heap. * This is useful for the * * ID3D12GraphicsCommandList::ClearUnorderedAccessViewFloat * * ID3D12GraphicsCommandList::ClearUnorderedAccessViewUint * methods which require both a CPU and GPU visible descriptors for a UAV * resource. * * @param commandList The command list is required in case the GPU visible * descriptor heap needs to be updated on the command list. * @param cpuDescriptor The CPU descriptor to copy into a GPU visible * descriptor heap. * * @return The GPU visible descriptor. */ d3d12::DescHeapGPUHandle CopyDescriptor(d3d12::CommandList& cmd_list, d3d12::DescHeapCPUHandle cpu_desc, unsigned int frame_idx); /** * Parse the root signature to determine which root parameters contain * descriptor tables and determine the number of descriptors needed for * each table. */ void ParseRootSignature(const d3d12::RootSignature& root_signature); /** * Reset used descriptors. This should only be done if any descriptors * that are being referenced by a command list has finished executing on the * command queue. */ void Reset(unsigned int frame_idx); /** * Ideally, this is only used when building the acceleration structures. * Don't call it otherwise. */ d3d12::DescriptorHeap* GetHeap() { return m_heap; } protected: private: // Create a new descriptor heap of no descriptor heap is available. d3d12::DescriptorHeap* CreateDescriptorHeap(); // Compute the number of stale descriptors that need to be copied // to GPU visible descriptor heap. uint32_t ComputeStaleDescriptorCount() const; /** * The maximum number of descriptor tables per root signature. * A 32-bit mask is used to keep track of the root parameter indices that * are descriptor tables. */ static const uint32_t MaxDescriptorTables = 32; /** * A structure that represents a descriptor table entry in the root signature. */ struct DescriptorTableCache { DescriptorTableCache() : m_num_descriptors(0) , m_base_descriptor(nullptr) {} // Reset the table cache. void Reset() { m_num_descriptors = 0; m_base_descriptor = nullptr; } // The number of descriptors in this descriptor table. uint32_t m_num_descriptors; // The pointer to the descriptor in the descriptor handle cache. D3D12_CPU_DESCRIPTOR_HANDLE* m_base_descriptor; }; // Describes the type of descriptors that can be staged using this // dynamic descriptor heap. // Valid values are: // * D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV // * D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER // This parameter also determines the type of GPU visible descriptor heap to // create. DescriptorHeapType m_desc_heap_type; // The number of descriptors to allocate in new GPU visible descriptor heaps. uint32_t m_num_descr_per_heap; uint32_t m_descriptor_handle_increment_size; // The descriptor handle cache. std::unique_ptr m_descriptor_handle_cache; // Descriptor handle cache per descriptor table. DescriptorTableCache m_descriptor_table_cache[MaxDescriptorTables]; // Each bit in the bit mask represents the index in the root signature // that contains a descriptor table. uint32_t m_descriptor_table_bit_mask; // Each bit set in the bit mask represents a descriptor table // in the root signature that has changed since the last time the // descriptors were copied. uint32_t m_stale_descriptor_table_bit_mask; d3d12::DescriptorHeap* m_heap; d3d12::DescHeapGPUHandle m_current_gpu_desc_handle; d3d12::DescHeapCPUHandle m_current_cpu_desc_handle; uint32_t m_num_free_handles[d3d12::settings::num_back_buffers]; wr::d3d12::Device* m_device; }; } ================================================ FILE: src/d3d12/d3d12_settings.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include "d3d12_enums.hpp" namespace wr::d3d12::settings { enum class DebugLayer { ENABLE, DISABLE, ENABLE_WITH_GPU_VALIDATION }; static const std::vector possible_feature_levels = { D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_0, D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0 }; static const constexpr bool output_hdr = false; static const constexpr Format back_buffer_format = output_hdr ? Format::R16G16B16A16_FLOAT : Format::R8G8B8A8_UNORM; static const constexpr DXGI_SWAP_EFFECT flip_mode = DXGI_SWAP_EFFECT_FLIP_DISCARD; static const constexpr DXGI_SWAP_CHAIN_FLAG swapchain_flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; static const constexpr DXGI_SCALING swapchain_scaling = DXGI_SCALING_STRETCH; static const constexpr DXGI_ALPHA_MODE swapchain_alpha_mode = DXGI_ALPHA_MODE_UNSPECIFIED; static const constexpr bool enable_gpu_timeout = false; static const constexpr bool enable_debug_factory = false; static const constexpr DebugLayer enable_debug_layer = DebugLayer::DISABLE; //Don't use ENABLE_WITH_GPU_VALIDATION (Raytracing); it breaks static const constexpr char* default_shader_model = "6_3"; static std::array debug_shader_args = { L"/O3" }; static std::array release_shader_args = { L"/O3" }; static const constexpr std::uint8_t num_back_buffers = 3; static const constexpr std::uint32_t num_instances_per_batch = 768U; //48 KiB for ObjectData[] static const constexpr std::uint32_t num_lights = 21'845; //1 MiB for StructuredBuffer static const constexpr std::uint32_t num_indirect_draw_commands = 8; //Allow 8 different meshes non-indexed static const constexpr std::uint32_t num_indirect_index_commands = 32; //Allow 32 different meshes indexed static const constexpr bool use_bundles = false; static const constexpr bool force_dxr_fallback = false; static const constexpr bool disable_rtx = false; static const constexpr bool enable_object_culling = true; static const constexpr unsigned int num_max_rt_materials = 3000; static const constexpr unsigned int num_max_rt_textures = 1000; static const constexpr unsigned int fallback_ptrs_offset = 3500; static const constexpr std::uint32_t res_skybox = 1024; static const constexpr std::uint32_t res_envmap = 512; static 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) static const constexpr unsigned int shadow_denoiser_feedback_tap = 1; // After which of the iterations should the result be stored for denoising the next frame. } /* wr::d3d12::settings */ ================================================ FILE: src/d3d12/d3d12_shader.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include #include #include "../util/log.hpp" #include "d3d12_defines.hpp" #include namespace wr::d3d12 { namespace internal { std::string ShaderTypeToString(ShaderType type) { std::string prefix = "unknown"; switch (type) { case ShaderType::VERTEX_SHADER: prefix = "vs_"; break; case ShaderType::PIXEL_SHADER: prefix = "ps_"; break; case ShaderType::DOMAIN_SHADER: prefix = "ds_"; break; case ShaderType::GEOMETRY_SHADER: prefix = "gs_"; break; case ShaderType::HULL_SHADER: prefix = "hs_"; break; case ShaderType::DIRECT_COMPUTE_SHADER: prefix = "cs_"; break; case ShaderType::LIBRARY_SHADER: prefix = "lib_"; break; } return prefix + std::string(d3d12::settings::default_shader_model); } } /* internal */ std::variant LoadShader(Device* device, ShaderType type, std::string const & path, std::string const & entry, std::vector> user_defines) { auto shader = new Shader(); shader->m_entry = entry; shader->m_path = path; shader->m_type = type; shader->m_defines = user_defines; std::wstring wpath(path.begin(), path.end()); std::wstring wentry(entry.begin(), entry.end()); std::string temp_shader_type(internal::ShaderTypeToString(type)); std::wstring wshader_type(temp_shader_type.begin(), temp_shader_type.end()); IDxcLibrary* library = nullptr; DxcCreateInstance(CLSID_DxcLibrary, __uuidof(IDxcLibrary), (void **)&library); IDxcBlobEncoding* source; std::uint32_t code_page = CP_ACP; library->CreateBlobFromFile(wpath.c_str(), &code_page, &source); IDxcIncludeHandler* include_handler; TRY_M(library->CreateIncludeHandler(&include_handler), "Failed to create default include handler."); std::vector defines; if (GetRaytracingType(device) == RaytracingType::FALLBACK) { defines.push_back({L"FALLBACK", L"1"}); } for (auto& define : user_defines) { defines.push_back({ define.first.c_str(), define.second.c_str() }); } IDxcOperationResult* result; HRESULT hr = Device::m_compiler->Compile( source, // program text wpath.c_str(), // file name, mostly for error messages wentry.c_str(), // entry point function wshader_type.c_str(), // target profile #ifdef _DEBUG d3d12::settings::debug_shader_args.data(), static_cast(d3d12::settings::debug_shader_args.size()), #else d3d12::settings::release_shader_args.data(), static_cast(d3d12::settings::release_shader_args.size()), #endif defines.data(), static_cast(defines.size()), // name/value defines and their count include_handler, // handler for #include directives &result); if (FAILED(hr)) { std::string error_msg = "Failed to compile " + path + " (entry: " + entry + "), Incorrect entry or path?"; return error_msg; } // compiler output result->GetStatus(&hr); if (FAILED(hr)) { delete shader; IDxcBlobEncoding* error; result->GetErrorBuffer(&error); return std::string((char*)error->GetBufferPointer()); } result->GetResult(&shader->m_native); return shader; } void Destroy(Shader* shader) { SAFE_RELEASE(shader->m_native); delete shader; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_shader_table.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include #include "d3d12_defines.hpp" namespace wr::d3d12 { ShaderRecord CreateShaderRecord(void* identifier, std::uint64_t identifier_size, void* local_root_args, std::uint64_t local_root_args_size) { ShaderRecord record; record.m_shader_identifier = { identifier, identifier_size }; record.m_local_root_args = { local_root_args, local_root_args_size }; return record; } // TODO: This might be problematic void CopyShaderRecord(ShaderRecord src, void* dest) { uint8_t* byte_dest = static_cast(dest); memcpy(byte_dest, src.m_shader_identifier.first, src.m_shader_identifier.second); if (src.m_local_root_args.first) { // byte_dest = static_cast(dest.m_local_root_args.first); memcpy(byte_dest + src.m_shader_identifier.second, src.m_local_root_args.first, src.m_local_root_args.second); } } ShaderTable* CreateShaderTable(Device* device, std::uint64_t num_shader_records, std::uint64_t shader_record_size) { auto table = new ShaderTable(); table->m_shader_record_size = SizeAlignTwoPower(shader_record_size, D3D12_RAYTRACING_SHADER_RECORD_BYTE_ALIGNMENT); table->m_shader_records.reserve(num_shader_records); std::uint64_t buffer_size = num_shader_records * table->m_shader_record_size; auto uploadHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); auto bufferDesc = CD3DX12_RESOURCE_DESC::Buffer(buffer_size); TRY(device->m_native->CreateCommittedResource( &uploadHeapProperties, D3D12_HEAP_FLAG_NONE, &bufferDesc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&table->m_resource))); table->m_resource->SetName(L"Shader table resource"); uint8_t* mappedData; // We don't unmap this until the app closes. Keeping buffer mapped for the lifetime of the resource is okay. CD3DX12_RANGE readRange(0, 0); // We do not intend to read from this resource on the CPU. TRY_M(table->m_resource->Map(0, &readRange, reinterpret_cast(&mappedData)), "Failed to map shader table resource"); table->m_mapped_shader_records = mappedData; return table; } void AddShaderRecord(ShaderTable* table, ShaderRecord record) { assert(table->m_shader_records.size() < table->m_shader_records.capacity()); table->m_shader_records.push_back(record); CopyShaderRecord(record, table->m_mapped_shader_records); table->m_mapped_shader_records += table->m_shader_record_size; } void Destroy(ShaderTable* table) { SAFE_RELEASE(table->m_resource); delete table; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_staging_buffer.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "d3d12_defines.hpp" namespace wr::d3d12 { StagingBuffer* CreateStagingBuffer(Device* device, void* data, uint64_t size, uint64_t stride, ResourceState resource_state) { auto buffer = new StagingBuffer(); buffer->m_target_resource_state = resource_state; buffer->m_size = size; buffer->m_stride_in_bytes = stride; buffer->m_is_staged = true; CD3DX12_HEAP_PROPERTIES heap_properties_default = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); CD3DX12_HEAP_PROPERTIES heap_properties_upload = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(size); device->m_native->CreateCommittedResource( &heap_properties_default, D3D12_HEAP_FLAG_NONE, &buffer_desc, static_cast(resource_state), nullptr, IID_PPV_ARGS(&buffer->m_buffer)); NAME_D3D12RESOURCE(buffer->m_buffer) device->m_native->CreateCommittedResource( &heap_properties_upload, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&buffer->m_staging)); NAME_D3D12RESOURCE(buffer->m_staging); CD3DX12_RANGE read_range(0, size); buffer->m_staging->Map(0, &read_range, reinterpret_cast(&(buffer->m_cpu_address))); if (data != nullptr) { memcpy(buffer->m_cpu_address, data, size); } return buffer; } void SetName(StagingBuffer * buffer, std::wstring name) { buffer->m_buffer->SetName((name + L" Destination buffer").c_str()); buffer->m_staging->SetName((name + L" Intermediate buffer").c_str()); } void UpdateStagingBuffer(StagingBuffer* buffer, void * data, std::uint64_t size, std::uint64_t offset) { memcpy(static_cast(buffer->m_cpu_address) + offset, data, size); } void StageBuffer(StagingBuffer* buffer, CommandList* cmd_list) { if (buffer->m_is_staged) { CD3DX12_RESOURCE_BARRIER resource_barrier = CD3DX12_RESOURCE_BARRIER::Transition(buffer->m_buffer, (D3D12_RESOURCE_STATES)buffer->m_target_resource_state, D3D12_RESOURCE_STATE_COPY_DEST); cmd_list->m_native->ResourceBarrier(1, &resource_barrier); } cmd_list->m_native->CopyBufferRegion(buffer->m_buffer, 0, buffer->m_staging, 0, buffer->m_size); // transition the vertex buffer data from copy destination state to vertex buffer state CD3DX12_RESOURCE_BARRIER vertex_barrier = CD3DX12_RESOURCE_BARRIER::Transition(buffer->m_buffer, D3D12_RESOURCE_STATE_COPY_DEST, (D3D12_RESOURCE_STATES)buffer->m_target_resource_state); cmd_list->m_native->ResourceBarrier(1, &vertex_barrier); buffer->m_gpu_address = buffer->m_buffer->GetGPUVirtualAddress(); buffer->m_is_staged = true; } void StageBufferRegion(StagingBuffer * buffer, std::uint64_t size, std::uint64_t offset, CommandList * cmd_list) { if (buffer->m_is_staged) { CD3DX12_RESOURCE_BARRIER resource_barrier = CD3DX12_RESOURCE_BARRIER::Transition(buffer->m_buffer, (D3D12_RESOURCE_STATES)buffer->m_target_resource_state, D3D12_RESOURCE_STATE_COPY_DEST); cmd_list->m_native->ResourceBarrier(1, &resource_barrier); } cmd_list->m_native->CopyBufferRegion(buffer->m_buffer, offset, buffer->m_staging, offset, size); // transition the vertex buffer data from copy destination state to vertex buffer state CD3DX12_RESOURCE_BARRIER vertex_barrier = CD3DX12_RESOURCE_BARRIER::Transition(buffer->m_buffer, D3D12_RESOURCE_STATE_COPY_DEST, (D3D12_RESOURCE_STATES)buffer->m_target_resource_state); cmd_list->m_native->ResourceBarrier(1, &vertex_barrier); buffer->m_gpu_address = buffer->m_buffer->GetGPUVirtualAddress(); buffer->m_is_staged = true; } void FreeStagingBuffer(StagingBuffer* buffer) { SAFE_RELEASE(buffer->m_staging); buffer->m_is_staged = false; } void Evict(StagingBuffer * buffer) { decltype(Device::m_native) n_device; buffer->m_staging->GetDevice(IID_PPV_ARGS(&n_device)); std::array objects{ buffer->m_staging, buffer->m_buffer }; n_device->Evict(static_cast(objects.size()), objects.data()); } void MakeResident(StagingBuffer * buffer) { decltype(Device::m_native) n_device; buffer->m_staging->GetDevice(IID_PPV_ARGS(&n_device)); std::array objects{ buffer->m_staging, buffer->m_buffer }; n_device->MakeResident(static_cast(objects.size()), objects.data()); } void CreateRawSRVFromStagingBuffer(StagingBuffer* buffer, DescHeapCPUHandle& handle, unsigned int count, Format format) { decltype(Device::m_native) n_device; buffer->m_buffer->GetDevice(IID_PPV_ARGS(&n_device)); auto increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Format = static_cast(format); srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; srv_desc.Buffer.NumElements = count; srv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW; n_device->CreateShaderResourceView(buffer->m_buffer, &srv_desc, handle.m_native); Offset(handle, 1, increment_size); } #include "../vertex.hpp" void CreateStructuredBufferSRVFromStagingBuffer(StagingBuffer* buffer, DescHeapCPUHandle& handle, unsigned int count, Format format) { decltype(Device::m_native) n_device; buffer->m_buffer->GetDevice(IID_PPV_ARGS(&n_device)); auto increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); auto stride = static_cast(sizeof(wr::Vertex)); D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Format = static_cast(format); srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; srv_desc.Buffer.FirstElement = 0; srv_desc.Buffer.NumElements = count; srv_desc.Buffer.StructureByteStride = stride; srv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE; n_device->CreateShaderResourceView(buffer->m_buffer, &srv_desc, handle.m_native); Offset(handle, 1, increment_size); } void Destroy(StagingBuffer* buffer) { SAFE_RELEASE(buffer->m_staging); SAFE_RELEASE(buffer->m_buffer); delete buffer; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_state_object.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "d3d12_defines.hpp" #include "d3dx12.hpp" #include namespace wr::d3d12 { [[nodiscard]] StateObject* CreateStateObject(Device* device, desc::StateObjectDesc desc) { auto state_object = new StateObject(); state_object->m_desc = desc; state_object->m_device = device; // Create CD3DX12_STATE_OBJECT_DESC CD3DX12_STATE_OBJECT_DESC n_desc = { D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE }; // Shader Library { D3D12_SHADER_BYTECODE bytecode = {}; bytecode.BytecodeLength = desc.m_library->m_native->GetBufferSize(); bytecode.pShaderBytecode = desc.m_library->m_native->GetBufferPointer(); auto lib = n_desc.CreateSubobject(); for (auto exp : desc.m_library_exports) { lib->DefineExport(exp.c_str()); } lib->SetDXILLibrary(&bytecode); } // Shader Config { auto shader_config = n_desc.CreateSubobject(); shader_config->Config(desc.max_payload_size, desc.max_attributes_size); } // Hitgroup { for (auto def : desc.m_hit_groups) { auto hitGroup = n_desc.CreateSubobject(); hitGroup->SetClosestHitShaderImport(def.second.c_str()); hitGroup->SetHitGroupExport(def.first.c_str()); hitGroup->SetHitGroupType(D3D12_HIT_GROUP_TYPE_TRIANGLES); } } // Global Root Signature if (auto rs = desc.global_root_signature.value_or(nullptr); desc.global_root_signature.has_value()) { auto global_rs = n_desc.CreateSubobject(); global_rs->SetRootSignature(rs->m_native); state_object->m_global_root_signature = rs; } // Local Root Signatures if (desc.local_root_signatures.has_value()) { for (auto& rs : desc.local_root_signatures.value()) { auto local_rs = n_desc.CreateSubobject(); local_rs->SetRootSignature(rs->m_native); // Define explicit shader association for the local root signature. { //auto rootSignatureAssociation = desc.desc.CreateSubobject(); //rootSignatureAssociation->SetSubobjectToAssociate(*local_rs); //rootSignatureAssociation->AddExport(L"MyHitGroup"); } } } // Pipeline Config { auto pipeline_config = n_desc.CreateSubobject(); pipeline_config->Config(desc.max_recursion_depth); } // Create the state object. if (GetRaytracingType(device) == RaytracingType::NATIVE) { HRESULT hr; TRY_M(hr = device->m_native->CreateStateObject(n_desc, IID_PPV_ARGS(&state_object->m_native)), "Couldn't create DirectX Raytracing state object."); // Shitty code because I don't know what As does. Microsoft::WRL::ComPtr temp_state_object(state_object->m_native); Microsoft::WRL::ComPtr temp_properties; TRY_M(temp_state_object.As(&temp_properties), "Failed to do ComPtr.As"); state_object->m_properties = temp_properties.Get(); temp_state_object.Detach(); temp_properties.Detach(); } else if(GetRaytracingType(device) == RaytracingType::FALLBACK) { TRY_M(device->m_fallback_native->CreateStateObject(n_desc, IID_PPV_ARGS(&state_object->m_fallback_native)), "Couldn't create DirectX Fallback Raytracing state object."); } n_desc.DeleteHelpers(); return state_object; } void RecreateStateObject(StateObject* state_object) { *state_object = *CreateStateObject(state_object->m_device, state_object->m_desc); } std::uint64_t GetShaderIdentifierSize(Device* device) { // Get shader identifiers. std::uint64_t retval = 0; if (GetRaytracingType(device) == RaytracingType::NATIVE) { retval = D3D12_SHADER_IDENTIFIER_SIZE_IN_BYTES; } else if (GetRaytracingType(device) == RaytracingType::FALLBACK) // DirectX Raytracing { retval = device->m_fallback_native->GetShaderIdentifierSize(); } return retval; } void SetGlobalRootSignature(StateObject* state_object, RootSignature* global_root_signature) { state_object->m_global_root_signature = global_root_signature; } [[nodiscard]] void* GetShaderIdentifier(Device* device, StateObject* obj, std::string const & name) { std::wstring wname(name.begin(), name.end()); // Reusable lambda auto GetShaderIdentifiers = [&](auto* stateObjectProperties) { return stateObjectProperties->GetShaderIdentifier(wname.c_str()); }; // Get shader identifiers. if (GetRaytracingType(device) == RaytracingType::NATIVE) { return GetShaderIdentifiers(obj->m_properties); } else if (GetRaytracingType(device) == RaytracingType::FALLBACK) // DirectX Raytracing { return GetShaderIdentifiers(obj->m_fallback_native); } LOGC("GetShaderIdentifier Called but raytracing isn't enabled!"); return nullptr; } void SetName(StateObject * obj, std::wstring name) { obj->m_native->SetName(name.c_str()); } void Destroy(StateObject* obj) { SAFE_RELEASE(obj->m_native); SAFE_RELEASE(obj->m_properties); SAFE_RELEASE(obj->m_fallback_native); delete obj; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_structs.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include "../structs.hpp" #include "d3d12_enums.hpp" #include "d3d12_settings.hpp" #include "d3dx12.hpp" namespace wr { class DynamicDescriptorHeap; class DescriptorAllocation; class RTDescriptorHeap; } namespace wr::d3d12 { // Forward declare struct StagingBuffer; struct Shader; struct RootSignature; namespace desc { struct RenderTargetDesc { ResourceState m_initial_state = ResourceState::RENDER_TARGET; bool m_create_dsv_buffer = true; Format m_dsv_format = wr::Format::UNKNOWN; std::array m_rtv_formats = { wr::Format::UNKNOWN }; std::uint32_t m_num_rtv_formats = 0u; float m_clear_color[4] = { 0.f, 0.f, 0.f, 0.f }; }; struct TextureDesc { ResourceState m_initial_state = ResourceState::COPY_DEST; Format m_texture_format = Format::UNKNOWN; std::uint32_t m_width = 0u; std::uint32_t m_height = 0u; std::uint32_t m_depth = 0u; std::uint32_t m_array_size = 0u; std::uint32_t m_mip_levels = 0u; bool m_is_cubemap = false; }; struct PipelineStateDesc { Format m_dsv_format; std::array m_rtv_formats = { wr::Format::UNKNOWN }; std::uint32_t m_num_rtv_formats = 0U; PipelineType m_type = PipelineType::GRAPHICS_PIPELINE; CullMode m_cull_mode = CullMode::CULL_BACK; bool m_depth_enabled = false; bool m_counter_clockwise = false; TopologyType m_topology_type = TopologyType::TRIANGLE; std::vector m_input_layout; }; struct SamplerDesc { TextureFilter m_filter; TextureAddressMode m_address_mode; BorderColor m_border_color = BorderColor::BORDER_WHITE; }; struct RootSignatureDesc { std::vector m_parameters; std::vector m_samplers; bool m_rtx = false; bool m_rt_local = false; }; struct DescriptorHeapDesc { std::uint32_t m_num_descriptors = 1u; DescriptorHeapType m_type = DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV; bool m_shader_visible = true; uint32_t m_versions = 1u; }; struct GeometryDesc { StagingBuffer* vertex_buffer = nullptr; std::optional index_buffer = std::nullopt; std::uint32_t m_num_vertices = 0u; std::uint32_t m_num_indices = 0u; std::uint32_t m_vertices_offset = 0u; std::uint32_t m_indices_offset = 0u; std::uint32_t m_vertex_stride = 0u; }; struct StateObjectDesc { Shader* m_library = nullptr; std::vector m_library_exports; std::vector> m_hit_groups; // first = hit group | second = entry std::uint32_t max_payload_size = 0u; std::uint32_t max_attributes_size = 0u; std::uint32_t max_recursion_depth = 0u; std::optional global_root_signature = std::nullopt; std::optional> local_root_signatures = { std::nullopt }; }; } /* desc */ struct Device { IDXGIAdapter4* m_adapter = nullptr; ID3D12Device5* m_native = nullptr; IDXGIFactory6* m_dxgi_factory = nullptr; D3D_FEATURE_LEVEL m_feature_level; SYSTEM_INFO m_sys_info; DXGI_ADAPTER_DESC3 m_adapter_info; ID3D12Debug1* m_debug_controller = nullptr; ID3D12InfoQueue* m_info_queue = nullptr; static IDxcCompiler2* m_compiler; // Optional supported formats std::bitset m_optional_formats; // Fallback bool m_dxr_fallback_support = false; bool m_dxr_support = false; RaytracingType m_rt_type; ID3D12RaytracingFallbackDevice* m_fallback_native = nullptr; bool IsFallback() { return m_rt_type == RaytracingType::FALLBACK; } }; struct CommandQueue { ID3D12CommandQueue* m_native = nullptr; }; struct CommandList { std::vector m_allocators; ID3D12GraphicsCommandList4* m_native = nullptr; ID3D12RaytracingFallbackCommandList* m_native_fallback = nullptr; // Dynamic descriptor heap where staging happens std::unique_ptr m_dynamic_descriptor_heaps[static_cast(DescriptorHeapType::DESC_HEAP_TYPE_NUM_TYPES)]; // Heap used for RT, shared between all the ray tracing command lists std::shared_ptr m_rt_descriptor_heap; // Keep track of currently bound heaps, so that we can avoid changing when not needed ID3D12DescriptorHeap* m_descriptor_heaps[static_cast(DescriptorHeapType::DESC_HEAP_TYPE_NUM_TYPES)]; }; struct CommandSignature { ID3D12CommandSignature* m_native = nullptr; }; struct RenderTarget { desc::RenderTargetDesc m_create_info; std::uint32_t m_frame_idx = 0u; std::uint32_t m_num_render_targets = 0u; std::uint32_t m_width = 0u; std::uint32_t m_height = 0u; std::vector m_render_targets; ID3D12DescriptorHeap* m_rtv_descriptor_heap = nullptr; std::uint32_t m_rtv_descriptor_increment_size = 0u; ID3D12Resource* m_depth_stencil_buffer = nullptr; ID3D12DescriptorHeap* m_depth_stencil_resource_heap = nullptr; }; struct RenderWindow : public RenderTarget { IDXGISwapChain4* m_swap_chain = nullptr; }; struct Fence { ID3D12Fence1* m_native = nullptr; HANDLE m_fence_event = nullptr; UINT64 m_fence_value = static_cast(0u); }; struct RootSignature { desc::RootSignatureDesc m_create_info; ID3D12RootSignature* m_native = nullptr; std::uint32_t m_num_descriptors_per_table[32] = { 0u }; std::uint32_t m_sampler_table_bit_mask = 0u; std::uint32_t m_descriptor_table_bit_mask = 0u; }; struct PipelineState; struct StateObject; struct Shader { IDxcBlob* m_native = nullptr; std::string m_path = ""; std::string m_entry = ""; ShaderType m_type; std::vector> m_defines; }; struct PipelineState { ID3D12PipelineState* m_native = nullptr; RootSignature* m_root_signature = nullptr; Shader* m_vertex_shader = nullptr; Shader* m_pixel_shader = nullptr; Shader* m_compute_shader = nullptr; Device* m_device = nullptr; // only used for refinalization. desc::PipelineStateDesc m_desc; }; struct Viewport { D3D12_VIEWPORT m_viewport; D3D12_RECT m_scissor_rect; }; struct DescriptorHeap { desc::DescriptorHeapDesc m_create_info; std::vector m_native; std::uint32_t m_increment_size = 0u; }; struct DescHeapCPUHandle { D3D12_CPU_DESCRIPTOR_HANDLE m_native; }; struct DescHeapGPUHandle { D3D12_GPU_DESCRIPTOR_HANDLE m_native; }; struct StagingBuffer { ID3D12Resource* m_buffer = nullptr; ID3D12Resource* m_staging = nullptr; std::uint64_t m_size = 0u; std::uint64_t m_stride_in_bytes = 0u; ResourceState m_target_resource_state; D3D12_GPU_VIRTUAL_ADDRESS m_gpu_address; std::uint8_t* m_cpu_address = nullptr; bool m_is_staged = false; }; struct ReadbackBufferResource : ReadbackBuffer { ID3D12Resource* m_resource = nullptr; }; struct HeapResource; namespace detail { /*! Fallback detail structure */ template struct Heap { }; /*! Small Buffer Optimization */ // TODO: Which one should we version? The heap or the resource? test perf! template<> struct Heap { std::vector m_resources; std::uint8_t* m_cpu_address = nullptr; ID3D12Resource* m_native = nullptr; }; /*! Big Buffer Optimization */ // TODO: Which one should we version? The heap or the resource? test perf! template<> struct Heap { std::vector>> m_resources; ID3D12Heap* m_native = nullptr; }; template<> struct Heap { std::vector m_resources; D3D12_GPU_VIRTUAL_ADDRESS m_gpu_address; ID3D12Resource* m_native = nullptr; ID3D12Resource* m_staging_buffer = nullptr; std::uint8_t* m_cpu_address = nullptr; }; template<> struct Heap { std::vector>> m_resources; ID3D12Heap* m_native = nullptr; ID3D12Resource* m_staging_buffer = nullptr; std::uint8_t* m_cpu_address = nullptr; }; } /* detail */ template struct Heap : detail::Heap { bool m_mapped; std::uint32_t m_versioning_count = 0u; std::uint64_t m_current_offset = 0u; std::uint64_t m_heap_size = 0u; std::uint64_t m_alignment = 0u; std::vector m_bitmap; }; struct HeapResource { union { Heap* m_heap_sbo; Heap* m_heap_ssbo; Heap* m_heap_bbo; Heap* m_heap_bsbo; }; std::vector m_gpu_addresses; std::optional> m_cpu_addresses; std::uint64_t m_unaligned_size = 0u; std::uint64_t m_begin_offset = 0u; std::size_t m_heap_vector_location = 0; std::size_t m_stride = 0; bool m_used_as_uav = false; HeapOptimization m_resource_heap_optimization; std::vector m_states; }; struct IndirectCommandBuffer { std::size_t m_num_commands = 0u; std::size_t m_num_max_commands = 0u; std::size_t m_command_size = 0u; std::vector m_native; std::vector m_native_upload; }; struct StateObject { ID3D12StateObject* m_native = nullptr; RootSignature* m_global_root_signature = nullptr; ID3D12StateObjectProperties* m_properties = nullptr; ID3D12RaytracingFallbackStateObject* m_fallback_native = nullptr; desc::StateObjectDesc m_desc; Device* m_device = nullptr; // Only for refinalization. }; struct AccelerationStructure { ID3D12Resource* m_scratch = nullptr; // Scratch memory for AS builder std::array m_natives = { nullptr }; // Where the AS is std::array m_instance_descs = { nullptr }; // Hold the matrices of the instances WRAPPED_GPU_POINTER m_fallback_tlas_ptr; D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO m_prebuild_info; }; namespace desc { struct BlasDesc { d3d12::AccelerationStructure m_as; std::uint64_t m_material = 0u; DirectX::XMMATRIX m_transform; }; } /* desc */ struct ShaderRecord { std::pair m_shader_identifier; std::pair m_local_root_args; }; struct ShaderTable { std::uint8_t* m_mapped_shader_records = nullptr; std::uint64_t m_shader_record_size = 0u; std::vector m_shader_records; ID3D12Resource* m_resource = nullptr; }; } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_structured_buffer.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" namespace wr::d3d12 { void CreateSRVFromStructuredBuffer(HeapResource* structured_buffer, DescHeapCPUHandle& handle, unsigned int id) { auto& resources = structured_buffer->m_heap_bsbo->m_resources[structured_buffer->m_heap_vector_location]; auto& resource = resources.second[id]; decltype(Device::m_native) n_device; resource->GetDevice(IID_PPV_ARGS(&n_device)); unsigned int increment_size = n_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER; srv_desc.Buffer.FirstElement = 0; srv_desc.Buffer.NumElements = static_cast(structured_buffer->m_unaligned_size / structured_buffer->m_stride); srv_desc.Buffer.StructureByteStride = static_cast(structured_buffer->m_stride); srv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE; n_device->CreateShaderResourceView(resource, &srv_desc, handle.m_native); Offset(handle, 1, increment_size); } } ================================================ FILE: src/d3d12/d3d12_structured_buffer_pool.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_structured_buffer_pool.hpp" #include "d3d12_functions.hpp" #include "d3d12_renderer.hpp" #include "d3d12_defines.hpp" namespace wr { D3D12StructuredBufferPool::D3D12StructuredBufferPool(D3D12RenderSystem & render_system, std::size_t size_in_bytes) : StructuredBufferPool(SizeAlignTwoPower(size_in_bytes, 65536)), m_render_system(render_system) { m_heap = d3d12::CreateHeap_BSBO(m_render_system.m_device, SizeAlignTwoPower(size_in_bytes, 65536), ResourceType::BUFFER, d3d12::settings::num_back_buffers); SetName(m_heap, L"Structured buffer heap"); m_buffer_update_queues.resize(d3d12::settings::num_back_buffers); } D3D12StructuredBufferPool::~D3D12StructuredBufferPool() { for (D3D12StructuredBufferHandle* handle : m_handles) { delete handle; } d3d12::Destroy(m_heap); } void D3D12StructuredBufferPool::UpdateBuffers(d3d12::CommandList * cmd_list, std::size_t frame_idx) { while (!m_buffer_update_queues[frame_idx].empty()) { BufferUpdateInfo info = m_buffer_update_queues[frame_idx].front(); if (info.m_data.size() > 0) { d3d12::UpdateStructuredBuffer(info.m_buffer_handle->m_native, static_cast(frame_idx), info.m_data.data(), info.m_size, info.m_offset, info.m_buffer_handle->m_native->m_stride, cmd_list); } else if (info.m_buffer_handle->m_native->m_states[frame_idx] != info.m_new_state) { ID3D12Resource * 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]; if (info.m_buffer_handle->m_native->m_states[frame_idx] != info.m_new_state) { CD3DX12_RESOURCE_BARRIER transition_barrier = CD3DX12_RESOURCE_BARRIER::Transition( resource, static_cast(info.m_buffer_handle->m_native->m_states[frame_idx]), static_cast(info.m_new_state)); cmd_list->m_native->ResourceBarrier(1, &transition_barrier); info.m_buffer_handle->m_native->m_states[frame_idx] = info.m_new_state; } else { LOG("Trying to transition a resource to a state is already in. This should be avoided."); } } m_buffer_update_queues[frame_idx].pop(); } } void D3D12StructuredBufferPool::SetBufferState(StructuredBufferHandle * handle, ResourceState state) { BufferUpdateInfo info = {}; info.m_buffer_handle = static_cast(handle); info.m_new_state = state; for (int i = 0; i < m_buffer_update_queues.size(); ++i) { m_buffer_update_queues[i].push(info); } } void D3D12StructuredBufferPool::Evict() { d3d12::Evict(m_heap); } void D3D12StructuredBufferPool::MakeResident() { d3d12::MakeResident(m_heap); } StructuredBufferHandle * D3D12StructuredBufferPool::CreateBuffer(std::size_t size, std::size_t stride, bool used_as_uav) { D3D12StructuredBufferHandle* handle = new D3D12StructuredBufferHandle(); handle->m_pool = this; handle->m_native = d3d12::AllocStructuredBuffer(m_heap, size, stride, used_as_uav); if (handle->m_native == nullptr) { delete handle; return nullptr; } m_handles.push_back(handle); return handle; } void D3D12StructuredBufferPool::DestroyBuffer(StructuredBufferHandle * handle) { std::vector::iterator it; for (it = m_handles.begin(); it != m_handles.end(); ++it) { if ((*it) == handle) { break; } } if (it == m_handles.end()) { return; } m_handles.erase(it); d3d12::DeallocBuffer(m_heap, static_cast(handle)->m_native); delete static_cast(handle); } void D3D12StructuredBufferPool::UpdateBuffer(StructuredBufferHandle * handle, void * data, std::size_t size, std::size_t offset) { if (handle->m_pool == this) { BufferUpdateInfo info = {}; info.m_buffer_handle = static_cast(handle); info.m_data.resize(size); memcpy(info.m_data.data(), data, size); info.m_size = size; info.m_offset = offset; for (int i = 0; i < d3d12::settings::num_back_buffers; ++i) { m_buffer_update_queues[i].push(info); } } } } /* wr */ ================================================ FILE: src/d3d12/d3d12_structured_buffer_pool.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../structured_buffer_pool.hpp" #include "d3d12_structs.hpp" #include namespace wr { struct D3D12StructuredBufferHandle : StructuredBufferHandle { d3d12::HeapResource* m_native; }; struct BufferUpdateInfo { D3D12StructuredBufferHandle* m_buffer_handle; std::vector m_data; std::size_t m_offset; std::size_t m_size; ResourceState m_new_state; }; class D3D12RenderSystem; class D3D12StructuredBufferPool : public StructuredBufferPool { public: explicit D3D12StructuredBufferPool(D3D12RenderSystem& render_system, std::size_t size_in_bytes); ~D3D12StructuredBufferPool() final; void UpdateBuffers(d3d12::CommandList* cmd_list, std::size_t frame_idx); void SetBufferState(StructuredBufferHandle* handle, ResourceState state); void Evict() final; void MakeResident() final; protected: [[nodiscard]] StructuredBufferHandle* CreateBuffer(std::size_t size, std::size_t stride, bool used_as_uav) final; void DestroyBuffer(StructuredBufferHandle* handle) final; void UpdateBuffer(StructuredBufferHandle* handle, void* data, std::size_t size, std::size_t offset) final; D3D12RenderSystem& m_render_system; d3d12::Heap* m_heap; std::vector m_handles; std::vector> m_buffer_update_queues; }; } /* wr */ ================================================ FILE: src/d3d12/d3d12_texture_resources.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../structs.hpp" #include "d3d12_enums.hpp" #include "d3d12_settings.hpp" #include "d3dx12.hpp" #include "d3d12_descriptors_allocations.hpp" #include namespace wr::d3d12 { struct TextureResource : Texture { std::size_t m_width = 0; std::size_t m_height = 0; std::size_t m_depth = 0; std::size_t m_array_size = 0; std::size_t m_mip_levels = 0; Format m_format = wr::Format::UNKNOWN; ID3D12Resource* m_resource = nullptr; ID3D12Resource* m_intermediate = nullptr; std::vector m_subresource_states; DescriptorAllocation m_srv_allocation; DescriptorAllocation m_uav_allocation; std::vector m_subresources; //This allocation can be either 1 or 6 continous descriptors based on m_is_cubemap std::optional m_rtv_allocation = std::nullopt; bool m_is_staged = false; bool m_need_mips = false; bool m_is_cubemap = false; bool m_allow_render_dest = false; }; } ================================================ FILE: src/d3d12/d3d12_textures.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" #include "../util/log.hpp" #include "d3d12_defines.hpp" #include "d3dx12.hpp" #include "d3d12_texture_resources.hpp" #include "d3d12_rt_descriptor_heap.hpp" namespace wr::d3d12 { TextureResource* CreateTexture(Device* device, desc::TextureDesc* description, bool allow_uav) { D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE; bool is_uav_compatible_format = (d3d12::CheckUAVCompatibility(description->m_texture_format) || d3d12::CheckOptionalUAVFormat(description->m_texture_format)); if (allow_uav && is_uav_compatible_format) { flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; } if (description->m_initial_state == ResourceState::RENDER_TARGET) { flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; } D3D12_RESOURCE_DESC desc = {}; desc.Width = description->m_width; desc.Height = description->m_height; desc.MipLevels = static_cast(description->m_mip_levels); desc.DepthOrArraySize = static_cast((description->m_depth > 1) ? description->m_depth : description->m_array_size); desc.Format = static_cast(description->m_texture_format); desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Flags = flags; desc.Dimension = (description->m_depth > 1) ? D3D12_RESOURCE_DIMENSION_TEXTURE3D : D3D12_RESOURCE_DIMENSION_TEXTURE2D; CD3DX12_HEAP_PROPERTIES defaultHeapProperties(D3D12_HEAP_TYPE_DEFAULT); auto native_device = device->m_native; ID3D12Resource* resource; HRESULT res = native_device->CreateCommittedResource(&defaultHeapProperties, D3D12_HEAP_FLAG_NONE, &desc, static_cast(description->m_initial_state), nullptr, IID_PPV_ARGS(&resource)); if (FAILED(res)) { LOGC("Error: Couldn't create texture"); } // Create intermediate resource on upload heap for staging uint64_t textureUploadBufferSize; device->m_native->GetCopyableFootprints(&desc, 0, desc.MipLevels * desc.DepthOrArraySize, 0, nullptr, nullptr, nullptr, &textureUploadBufferSize); ID3D12Resource* intermediate; CD3DX12_HEAP_PROPERTIES uploadHeapProperties(D3D12_HEAP_TYPE_UPLOAD); CD3DX12_RESOURCE_DESC buffer_desc = CD3DX12_RESOURCE_DESC::Buffer(textureUploadBufferSize); device->m_native->CreateCommittedResource( &uploadHeapProperties, D3D12_HEAP_FLAG_NONE, &buffer_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&intermediate)); TextureResource* texture = new TextureResource(); texture->m_width = description->m_width; texture->m_height = description->m_height; texture->m_depth = description->m_depth; texture->m_array_size = description->m_array_size; texture->m_mip_levels = description->m_mip_levels; texture->m_format = description->m_texture_format; texture->m_resource = resource; texture->m_intermediate = intermediate; texture->m_need_mips = (texture->m_mip_levels > 1); texture->m_is_cubemap = description->m_is_cubemap; texture->m_is_staged = false; for (uint32_t i = 0; i < description->m_mip_levels; ++i) { texture->m_subresource_states.push_back(description->m_initial_state); } return texture; } TextureResource* CreatePlacedTexture(Device* device, desc::TextureDesc* description, bool allow_uav, Heap* heap) { D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE; bool is_uav_compatible_format = (d3d12::CheckUAVCompatibility(description->m_texture_format) || d3d12::CheckOptionalUAVFormat(description->m_texture_format)); if (allow_uav && is_uav_compatible_format) { flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; } if (description->m_initial_state == ResourceState::RENDER_TARGET) { flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; } D3D12_RESOURCE_DESC desc = {}; desc.Width = description->m_width; desc.Height = description->m_height; desc.MipLevels = static_cast(description->m_mip_levels); desc.DepthOrArraySize = static_cast((description->m_depth > 1) ? description->m_depth : description->m_array_size); desc.Format = static_cast(description->m_texture_format); desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Flags = flags; desc.Dimension = (description->m_depth > 1) ? D3D12_RESOURCE_DIMENSION_TEXTURE3D : D3D12_RESOURCE_DIMENSION_TEXTURE2D; auto native_device = device->m_native; ID3D12Resource* resource; HRESULT res = native_device->CreatePlacedResource( heap->m_native, 0, &desc, static_cast(description->m_initial_state), nullptr, IID_PPV_ARGS(&resource)); if (FAILED(res)) { LOGC("Error: Couldn't create texture"); } TextureResource* texture = new TextureResource(); texture->m_width = description->m_width; texture->m_height = description->m_height; texture->m_depth = description->m_depth; texture->m_array_size = description->m_array_size; texture->m_mip_levels = description->m_mip_levels; texture->m_format = description->m_texture_format; texture->m_resource = resource; texture->m_intermediate = nullptr; texture->m_need_mips = (texture->m_mip_levels > 1); texture->m_is_cubemap = description->m_is_cubemap; texture->m_is_staged = false; for (uint32_t i = 0; i < description->m_mip_levels; ++i) { texture->m_subresource_states.push_back(description->m_initial_state); } return texture; } void SetName(TextureResource* tex, std::wstring name) { tex->m_resource->SetName(name.c_str()); } void CreateSRVFromTexture(TextureResource* tex) { d3d12::DescHeapCPUHandle handle = tex->m_srv_allocation.GetDescriptorHandle(); CreateSRVFromTexture(tex, handle); } void CreateSRVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle) { decltype(Device::m_native) n_device; tex->m_resource->GetDevice(IID_PPV_ARGS(&n_device)); D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.Format = (DXGI_FORMAT)tex->m_format; //Calculate dimension D3D12_SRV_DIMENSION dimension; if (tex->m_is_cubemap) { dimension = D3D12_SRV_DIMENSION_TEXTURECUBE; srv_desc.TextureCube.MipLevels = static_cast(tex->m_mip_levels); } else { if (tex->m_depth > 1) { //Then it's a 3D texture dimension = D3D12_SRV_DIMENSION_TEXTURE3D; srv_desc.Texture3D.MipLevels = static_cast(tex->m_mip_levels); } else { if (tex->m_height > 1) { if (tex->m_array_size > 1) { dimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; srv_desc.Texture2DArray.MipLevels = static_cast(tex->m_mip_levels); srv_desc.Texture2DArray.ArraySize = static_cast(tex->m_array_size); } else { dimension = D3D12_SRV_DIMENSION_TEXTURE2D; srv_desc.Texture2D.MipLevels = static_cast(tex->m_mip_levels); } } else { //Then it's a 1D texture if (tex->m_array_size > 1) { dimension = D3D12_SRV_DIMENSION_TEXTURE1DARRAY; srv_desc.Texture1DArray.MipLevels = static_cast(tex->m_mip_levels); srv_desc.Texture1DArray.ArraySize = static_cast(tex->m_array_size); } else { dimension = D3D12_SRV_DIMENSION_TEXTURE1D; srv_desc.Texture1D.MipLevels = static_cast(tex->m_mip_levels); } } } } srv_desc.ViewDimension = dimension; n_device->CreateShaderResourceView(tex->m_resource, &srv_desc, handle.m_native); } void CreateSRVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_levels, unsigned int most_detailed_mip) { decltype(Device::m_native) n_device; tex->m_resource->GetDevice(IID_PPV_ARGS(&n_device)); D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.Format = (DXGI_FORMAT)tex->m_format; //Calculate dimension D3D12_SRV_DIMENSION dimension; if (tex->m_is_cubemap) { dimension = D3D12_SRV_DIMENSION_TEXTURECUBE; srv_desc.TextureCube.MipLevels = mip_levels; srv_desc.TextureCube.MostDetailedMip = most_detailed_mip; } else { if (tex->m_depth > 1) { //Then it's a 3D texture dimension = D3D12_SRV_DIMENSION_TEXTURE3D; srv_desc.Texture3D.MipLevels = mip_levels; srv_desc.Texture3D.MostDetailedMip = most_detailed_mip; } else { if (tex->m_height > 1) { if (tex->m_array_size > 1) { dimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; srv_desc.Texture2DArray.MipLevels = mip_levels; srv_desc.Texture2DArray.MostDetailedMip = most_detailed_mip; srv_desc.Texture2DArray.ArraySize = static_cast(tex->m_array_size); } else { dimension = D3D12_SRV_DIMENSION_TEXTURE2D; srv_desc.Texture2D.MipLevels = mip_levels; srv_desc.Texture2D.MostDetailedMip = most_detailed_mip; } } else { //Then it's a 1D texture if (tex->m_array_size > 1) { dimension = D3D12_SRV_DIMENSION_TEXTURE1DARRAY; srv_desc.Texture1DArray.MipLevels = mip_levels; srv_desc.Texture1DArray.MostDetailedMip = most_detailed_mip; srv_desc.Texture1DArray.ArraySize = static_cast(tex->m_array_size); } else { dimension = D3D12_SRV_DIMENSION_TEXTURE1D; srv_desc.Texture1D.MipLevels = mip_levels; srv_desc.Texture1D.MostDetailedMip = most_detailed_mip; } } } } srv_desc.ViewDimension = dimension; n_device->CreateShaderResourceView(tex->m_resource, &srv_desc, handle.m_native); } void CreateSRVFromCubemapFace(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_levels, unsigned int most_detailed_mip, unsigned int face_idx) { decltype(Device::m_native) n_device; tex->m_resource->GetDevice(IID_PPV_ARGS(&n_device)); D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; srv_desc.Format = (DXGI_FORMAT)tex->m_format; //Calculate dimension D3D12_SRV_DIMENSION dimension; if (tex->m_is_cubemap) { dimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY; srv_desc.Texture2DArray.MipLevels = mip_levels; srv_desc.Texture2DArray.MostDetailedMip = most_detailed_mip; srv_desc.Texture2DArray.ArraySize = 1; srv_desc.Texture2DArray.FirstArraySlice = face_idx; } else { LOGC("[ERROR]: Texture is not a cubemap"); return; } srv_desc.ViewDimension = dimension; n_device->CreateShaderResourceView(tex->m_resource, &srv_desc, handle.m_native); } void CreateUAVFromTexture(TextureResource* tex, unsigned int mip_slice) { d3d12::DescHeapCPUHandle handle = tex->m_uav_allocation.GetDescriptorHandle(); CreateUAVFromTexture(tex, handle, mip_slice); } void CreateUAVFromTexture(TextureResource* tex, DescHeapCPUHandle& handle, unsigned int mip_slice) { decltype(Device::m_native) n_device; tex->m_resource->GetDevice(IID_PPV_ARGS(&n_device)); D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {}; uav_desc.Format = (DXGI_FORMAT)tex->m_format; //Calculate dimension D3D12_UAV_DIMENSION dimension; if (tex->m_is_cubemap) { dimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY; uav_desc.Texture2DArray.MipSlice = mip_slice; uav_desc.Texture2DArray.ArraySize = static_cast(tex->m_array_size); } else { if (tex->m_depth > 1) { //Then it's a 3D texture dimension = D3D12_UAV_DIMENSION_TEXTURE3D; uav_desc.Texture3D.MipSlice = mip_slice; } else { if (tex->m_height > 1) { if (tex->m_array_size > 1) { dimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY; uav_desc.Texture2DArray.MipSlice = mip_slice; uav_desc.Texture2DArray.ArraySize = static_cast(tex->m_array_size); } else { dimension = D3D12_UAV_DIMENSION_TEXTURE2D; uav_desc.Texture2D.MipSlice = mip_slice; } } else { //Then it's a 1D texture if (tex->m_array_size > 1) { dimension = D3D12_UAV_DIMENSION_TEXTURE1DARRAY; uav_desc.Texture1DArray.MipSlice = mip_slice; uav_desc.Texture1DArray.ArraySize = static_cast(tex->m_array_size); } else { dimension = D3D12_UAV_DIMENSION_TEXTURE1D; uav_desc.Texture1D.MipSlice = mip_slice; } } } } uav_desc.ViewDimension = dimension; n_device->CreateUnorderedAccessView(tex->m_resource, nullptr, &uav_desc, handle.m_native); } void CreateUAVFromCubemapFace(TextureResource * tex, DescHeapCPUHandle & handle, unsigned int mip_slice, unsigned int face_idx) { decltype(Device::m_native) n_device; tex->m_resource->GetDevice(IID_PPV_ARGS(&n_device)); D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {}; uav_desc.Format = (DXGI_FORMAT)tex->m_format; if (tex->m_is_cubemap) { uav_desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY; uav_desc.Texture2DArray.MipSlice = mip_slice; uav_desc.Texture2DArray.FirstArraySlice = face_idx; uav_desc.Texture2DArray.ArraySize = 1; } else { LOGC("[ERROR]: Texture is not a cubemap"); } n_device->CreateUnorderedAccessView(tex->m_resource, nullptr, &uav_desc, handle.m_native); } void CreateRTVFromTexture2D(TextureResource* tex) { decltype(Device::m_native) n_device; tex->m_resource->GetDevice(IID_PPV_ARGS(&n_device)); if (tex->m_rtv_allocation == std::nullopt) { LOGC("This texture was not created with the intent of being used as a RTV."); } D3D12_RENDER_TARGET_VIEW_DESC desc; desc.Format = static_cast(tex->m_format); desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D; desc.Texture2D.MipSlice = 0; DescHeapCPUHandle handle = tex->m_rtv_allocation->GetDescriptorHandle(); n_device->CreateRenderTargetView(tex->m_resource, &desc, handle.m_native); } void CreateRTVFromCubemap(TextureResource* tex) { decltype(Device::m_native) n_device; tex->m_resource->GetDevice(IID_PPV_ARGS(&n_device)); if (!tex->m_is_cubemap) { LOGC("This texture is not a cubemap."); } if (tex->m_rtv_allocation == std::nullopt) { LOGC("This texture was not created with the intent of being used as a RTV."); } for (uint8_t i = 0; i < 6; ++i) { D3D12_RENDER_TARGET_VIEW_DESC desc; desc.Format = static_cast(tex->m_format); desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DARRAY; desc.Texture2DArray.MipSlice = 0; desc.Texture2DArray.PlaneSlice = 0; //Render target to ith element desc.Texture2DArray.FirstArraySlice = i; //Only view one element of the array desc.Texture2DArray.ArraySize = 1; DescHeapCPUHandle handle = tex->m_rtv_allocation->GetDescriptorHandle(i); n_device->CreateRenderTargetView(tex->m_resource, &desc, handle.m_native); } } void SetShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex) { d3d12::DescHeapCPUHandle handle = tex->m_srv_allocation.GetDescriptorHandle(); cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(rootParameterIndex, descriptorOffset, 1, handle); } void SetShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count) { cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(root_parameter_index, descriptor_offset, descriptor_count, handle); } void SetShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex) { d3d12::DescHeapCPUHandle handle = tex->m_uav_allocation.GetDescriptorHandle(); cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(rootParameterIndex, descriptorOffset, 1, handle); } void SetShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count) { cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(root_parameter_index, descriptor_offset, descriptor_count, handle); } void SetRTShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex) { d3d12::DescHeapCPUHandle handle = tex->m_srv_allocation.GetDescriptorHandle(); cmd_list->m_rt_descriptor_heap->StageDescriptors(rootParameterIndex, descriptorOffset, 1, handle); } void SetRTShaderSRV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count) { cmd_list->m_rt_descriptor_heap->StageDescriptors(root_parameter_index, descriptor_offset, descriptor_count, handle); } void SetRTShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t rootParameterIndex, uint32_t descriptorOffset, TextureResource* tex) { d3d12::DescHeapCPUHandle handle = tex->m_uav_allocation.GetDescriptorHandle(); cmd_list->m_rt_descriptor_heap->StageDescriptors(rootParameterIndex, descriptorOffset, 1, handle); } void SetRTShaderUAV(wr::d3d12::CommandList* cmd_list, uint32_t root_parameter_index, uint32_t descriptor_offset, d3d12::DescHeapCPUHandle& handle, uint32_t descriptor_count) { cmd_list->m_rt_descriptor_heap->StageDescriptors(root_parameter_index, descriptor_offset, descriptor_count, handle); } void CopyResource(wr::d3d12::CommandList* cmd_list, TextureResource* src_texture, TextureResource* dst_texture) { ResourceState src_original_state = src_texture->m_subresource_states[0]; ResourceState dst_original_state = dst_texture->m_subresource_states[0]; d3d12::Transition(cmd_list, src_texture, src_original_state, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, dst_texture, dst_original_state, ResourceState::COPY_DEST); cmd_list->m_native->CopyResource(dst_texture->m_resource, src_texture->m_resource); d3d12::Transition(cmd_list, src_texture, ResourceState::COPY_SOURCE, src_original_state); d3d12::Transition(cmd_list, dst_texture, ResourceState::COPY_DEST, dst_original_state); } void Destroy(TextureResource* tex) { if (tex != nullptr) { SAFE_RELEASE(tex->m_resource); delete tex; } } bool CheckUAVCompatibility(Format format) { switch (format) { case Format::R32G32B32A32_FLOAT: case Format::R32G32B32A32_UINT: case Format::R32G32B32A32_SINT: case Format::R16G16B16A16_FLOAT: case Format::R16G16B16A16_UINT: case Format::R16G16B16A16_SINT: case Format::R8G8B8A8_UNORM: case Format::R8G8B8A8_UINT: case Format::R8G8B8A8_SINT: case Format::R32_FLOAT: case Format::R32_UINT: case Format::R32_SINT: case Format::R16_FLOAT: case Format::R16_UINT: case Format::R16_SINT: case Format::R8_UNORM: case Format::R8_UINT: case Format::R8_SINT: return true; default: return false; } } bool CheckOptionalUAVFormat(Format format) { switch (format) { case Format::R16G16B16A16_UNORM: case Format::R16G16B16A16_SNORM: case Format::R32G32_FLOAT: case Format::R32G32_UINT: case Format::R32G32_SINT: case Format::R10G10B10A2_UNORM: case Format::R10G10B10A2_UINT: case Format::R11G11B10_FLOAT: case Format::R8G8B8A8_SNORM: case Format::R16G16_FLOAT: case Format::R16G16_UNORM: case Format::R16G16_UINT: case Format::R16G16_SNORM: case Format::R16G16_SINT: case Format::R8G8_UNORM: case Format::R8G8_UINT: case Format::R8G8_SNORM: case Format::R8G8_SINT: case Format::R16_UNORM: case Format::R16_SNORM: case Format::R8_SNORM: case Format::A8_UNORM: case Format::B5G6R5_UNORM: case Format::B5G5R5A1_UNORM: case Format::B4G4R4A4_UNORM: return true; default: return false; } } bool CheckBGRFormat(Format format) { switch (format) { case Format::B8G8R8A8_UNORM: case Format::B8G8R8X8_UNORM: case Format::B8G8R8A8_UNORM_SRGB: case Format::B8G8R8X8_UNORM_SRGB: return true; default: return false; } } bool CheckSRGBFormat(Format format) { switch (format) { case Format::R8G8B8A8_UNORM_SRGB: case Format::B8G8R8A8_UNORM_SRGB: case Format::B8G8R8X8_UNORM_SRGB: return true; default: return false; } } bool IsOptionalFormatSupported(Device* device, Format format) { return device->m_optional_formats.test(static_cast(format)); } Format RemoveSRGB(Format format) { Format out_format = Format::UNKNOWN; switch (format) { case wr::Format::R8G8B8A8_UNORM_SRGB: out_format = Format::R8G8B8A8_UNORM; break; case wr::Format::B8G8R8A8_UNORM_SRGB: out_format = Format::B8G8R8A8_UNORM; break; case wr::Format::B8G8R8X8_UNORM_SRGB: out_format = Format::B8G8R8X8_UNORM; break; default: break; } return out_format; } Format BGRtoRGB(Format format) { Format out_format = Format::UNKNOWN; switch (format) { case Format::B8G8R8A8_UNORM: out_format = Format::R8G8B8A8_UNORM; break; case Format::B8G8R8X8_UNORM: out_format = Format::R8G8B8A8_UNORM; break; case Format::B8G8R8A8_UNORM_SRGB: out_format = Format::R8G8B8A8_UNORM_SRGB; break; case Format::B8G8R8X8_UNORM_SRGB: out_format = Format::R8G8B8A8_UNORM_SRGB; break; default: break; } return out_format; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3d12_viewport.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "d3d12_functions.hpp" namespace wr::d3d12 { Viewport CreateViewport(int width, int height) { Viewport viewport; // Define viewport. viewport.m_viewport.TopLeftX = 0; viewport.m_viewport.TopLeftY = 0; viewport.m_viewport.Width = static_cast(width); viewport.m_viewport.Height = static_cast(height); viewport.m_viewport.MinDepth = 0.0f; viewport.m_viewport.MaxDepth = 1.0f; // Define scissor rect viewport.m_scissor_rect.left = 0; viewport.m_scissor_rect.top = 0; viewport.m_scissor_rect.right = width; viewport.m_scissor_rect.bottom = height; return viewport; } void ResizeViewport(Viewport& viewport, int width, int height) { // Define viewport. viewport.m_viewport.Width = static_cast(width); viewport.m_viewport.Height = static_cast(height); // Define scissor rect viewport.m_scissor_rect.right = width; viewport.m_scissor_rect.bottom = height; } } /* wr::d3d12 */ ================================================ FILE: src/d3d12/d3dx12.hpp ================================================ //********************************************************* // // Copyright (c) Microsoft. All rights reserved. // This code is licensed under the MIT License (MIT). // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //********************************************************* #ifndef __D3DX12_H__ #define __D3DX12_H__ #pragma warning(push, 0) #include "d3d12.h" #if defined( __cplusplus ) struct CD3DX12_DEFAULT {}; extern const DECLSPEC_SELECTANY CD3DX12_DEFAULT D3D12_DEFAULT; //------------------------------------------------------------------------------------------------ inline bool operator==( const D3D12_VIEWPORT& l, const D3D12_VIEWPORT& r ) { return l.TopLeftX == r.TopLeftX && l.TopLeftY == r.TopLeftY && l.Width == r.Width && l.Height == r.Height && l.MinDepth == r.MinDepth && l.MaxDepth == r.MaxDepth; } //------------------------------------------------------------------------------------------------ inline bool operator!=( const D3D12_VIEWPORT& l, const D3D12_VIEWPORT& r ) { return !( l == r ); } //------------------------------------------------------------------------------------------------ struct CD3DX12_RECT : public D3D12_RECT { CD3DX12_RECT() = default; explicit CD3DX12_RECT( const D3D12_RECT& o ) : D3D12_RECT( o ) {} explicit CD3DX12_RECT( LONG Left, LONG Top, LONG Right, LONG Bottom ) { left = Left; top = Top; right = Right; bottom = Bottom; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_VIEWPORT : public D3D12_VIEWPORT { CD3DX12_VIEWPORT() = default; explicit CD3DX12_VIEWPORT( const D3D12_VIEWPORT& o ) : D3D12_VIEWPORT( o ) {} explicit CD3DX12_VIEWPORT( FLOAT topLeftX, FLOAT topLeftY, FLOAT width, FLOAT height, FLOAT minDepth = D3D12_MIN_DEPTH, FLOAT maxDepth = D3D12_MAX_DEPTH ) { TopLeftX = topLeftX; TopLeftY = topLeftY; Width = width; Height = height; MinDepth = minDepth; MaxDepth = maxDepth; } explicit CD3DX12_VIEWPORT( _In_ ID3D12Resource* pResource, UINT mipSlice = 0, FLOAT topLeftX = 0.0f, FLOAT topLeftY = 0.0f, FLOAT minDepth = D3D12_MIN_DEPTH, FLOAT maxDepth = D3D12_MAX_DEPTH ) { auto Desc = pResource->GetDesc(); const UINT64 SubresourceWidth = Desc.Width >> mipSlice; const UINT64 SubresourceHeight = Desc.Height >> mipSlice; switch (Desc.Dimension) { case D3D12_RESOURCE_DIMENSION_BUFFER: TopLeftX = topLeftX; TopLeftY = 0.0f; Width = Desc.Width - topLeftX; Height = 1.0f; break; case D3D12_RESOURCE_DIMENSION_TEXTURE1D: TopLeftX = topLeftX; TopLeftY = 0.0f; Width = (SubresourceWidth ? SubresourceWidth : 1.0f) - topLeftX; Height = 1.0f; break; case D3D12_RESOURCE_DIMENSION_TEXTURE2D: case D3D12_RESOURCE_DIMENSION_TEXTURE3D: TopLeftX = topLeftX; TopLeftY = topLeftY; Width = (SubresourceWidth ? SubresourceWidth : 1.0f) - topLeftX; Height = (SubresourceHeight ? SubresourceHeight: 1.0f) - topLeftY; break; default: break; } MinDepth = minDepth; MaxDepth = maxDepth; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_BOX : public D3D12_BOX { CD3DX12_BOX() = default; explicit CD3DX12_BOX( const D3D12_BOX& o ) : D3D12_BOX( o ) {} explicit CD3DX12_BOX( LONG Left, LONG Right ) { left = Left; top = 0; front = 0; right = Right; bottom = 1; back = 1; } explicit CD3DX12_BOX( LONG Left, LONG Top, LONG Right, LONG Bottom ) { left = Left; top = Top; front = 0; right = Right; bottom = Bottom; back = 1; } explicit CD3DX12_BOX( LONG Left, LONG Top, LONG Front, LONG Right, LONG Bottom, LONG Back ) { left = Left; top = Top; front = Front; right = Right; bottom = Bottom; back = Back; } }; inline bool operator==( const D3D12_BOX& l, const D3D12_BOX& r ) { return l.left == r.left && l.top == r.top && l.front == r.front && l.right == r.right && l.bottom == r.bottom && l.back == r.back; } inline bool operator!=( const D3D12_BOX& l, const D3D12_BOX& r ) { return !( l == r ); } //------------------------------------------------------------------------------------------------ struct CD3DX12_DEPTH_STENCIL_DESC : public D3D12_DEPTH_STENCIL_DESC { CD3DX12_DEPTH_STENCIL_DESC() = default; explicit CD3DX12_DEPTH_STENCIL_DESC( const D3D12_DEPTH_STENCIL_DESC& o ) : D3D12_DEPTH_STENCIL_DESC( o ) {} explicit CD3DX12_DEPTH_STENCIL_DESC( CD3DX12_DEFAULT ) { DepthEnable = TRUE; DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; DepthFunc = D3D12_COMPARISON_FUNC_LESS; StencilEnable = FALSE; StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK; StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK; const D3D12_DEPTH_STENCILOP_DESC defaultStencilOp = { D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS }; FrontFace = defaultStencilOp; BackFace = defaultStencilOp; } explicit CD3DX12_DEPTH_STENCIL_DESC( BOOL depthEnable, D3D12_DEPTH_WRITE_MASK depthWriteMask, D3D12_COMPARISON_FUNC depthFunc, BOOL stencilEnable, UINT8 stencilReadMask, UINT8 stencilWriteMask, D3D12_STENCIL_OP frontStencilFailOp, D3D12_STENCIL_OP frontStencilDepthFailOp, D3D12_STENCIL_OP frontStencilPassOp, D3D12_COMPARISON_FUNC frontStencilFunc, D3D12_STENCIL_OP backStencilFailOp, D3D12_STENCIL_OP backStencilDepthFailOp, D3D12_STENCIL_OP backStencilPassOp, D3D12_COMPARISON_FUNC backStencilFunc ) { DepthEnable = depthEnable; DepthWriteMask = depthWriteMask; DepthFunc = depthFunc; StencilEnable = stencilEnable; StencilReadMask = stencilReadMask; StencilWriteMask = stencilWriteMask; FrontFace.StencilFailOp = frontStencilFailOp; FrontFace.StencilDepthFailOp = frontStencilDepthFailOp; FrontFace.StencilPassOp = frontStencilPassOp; FrontFace.StencilFunc = frontStencilFunc; BackFace.StencilFailOp = backStencilFailOp; BackFace.StencilDepthFailOp = backStencilDepthFailOp; BackFace.StencilPassOp = backStencilPassOp; BackFace.StencilFunc = backStencilFunc; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_DEPTH_STENCIL_DESC1 : public D3D12_DEPTH_STENCIL_DESC1 { CD3DX12_DEPTH_STENCIL_DESC1() = default; explicit CD3DX12_DEPTH_STENCIL_DESC1( const D3D12_DEPTH_STENCIL_DESC1& o ) : D3D12_DEPTH_STENCIL_DESC1( o ) {} explicit CD3DX12_DEPTH_STENCIL_DESC1( const D3D12_DEPTH_STENCIL_DESC& o ) { DepthEnable = o.DepthEnable; DepthWriteMask = o.DepthWriteMask; DepthFunc = o.DepthFunc; StencilEnable = o.StencilEnable; StencilReadMask = o.StencilReadMask; StencilWriteMask = o.StencilWriteMask; FrontFace.StencilFailOp = o.FrontFace.StencilFailOp; FrontFace.StencilDepthFailOp = o.FrontFace.StencilDepthFailOp; FrontFace.StencilPassOp = o.FrontFace.StencilPassOp; FrontFace.StencilFunc = o.FrontFace.StencilFunc; BackFace.StencilFailOp = o.BackFace.StencilFailOp; BackFace.StencilDepthFailOp = o.BackFace.StencilDepthFailOp; BackFace.StencilPassOp = o.BackFace.StencilPassOp; BackFace.StencilFunc = o.BackFace.StencilFunc; DepthBoundsTestEnable = FALSE; } explicit CD3DX12_DEPTH_STENCIL_DESC1( CD3DX12_DEFAULT ) { DepthEnable = TRUE; DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; DepthFunc = D3D12_COMPARISON_FUNC_LESS; StencilEnable = FALSE; StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK; StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK; const D3D12_DEPTH_STENCILOP_DESC defaultStencilOp = { D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS }; FrontFace = defaultStencilOp; BackFace = defaultStencilOp; DepthBoundsTestEnable = FALSE; } explicit CD3DX12_DEPTH_STENCIL_DESC1( BOOL depthEnable, D3D12_DEPTH_WRITE_MASK depthWriteMask, D3D12_COMPARISON_FUNC depthFunc, BOOL stencilEnable, UINT8 stencilReadMask, UINT8 stencilWriteMask, D3D12_STENCIL_OP frontStencilFailOp, D3D12_STENCIL_OP frontStencilDepthFailOp, D3D12_STENCIL_OP frontStencilPassOp, D3D12_COMPARISON_FUNC frontStencilFunc, D3D12_STENCIL_OP backStencilFailOp, D3D12_STENCIL_OP backStencilDepthFailOp, D3D12_STENCIL_OP backStencilPassOp, D3D12_COMPARISON_FUNC backStencilFunc, BOOL depthBoundsTestEnable ) { DepthEnable = depthEnable; DepthWriteMask = depthWriteMask; DepthFunc = depthFunc; StencilEnable = stencilEnable; StencilReadMask = stencilReadMask; StencilWriteMask = stencilWriteMask; FrontFace.StencilFailOp = frontStencilFailOp; FrontFace.StencilDepthFailOp = frontStencilDepthFailOp; FrontFace.StencilPassOp = frontStencilPassOp; FrontFace.StencilFunc = frontStencilFunc; BackFace.StencilFailOp = backStencilFailOp; BackFace.StencilDepthFailOp = backStencilDepthFailOp; BackFace.StencilPassOp = backStencilPassOp; BackFace.StencilFunc = backStencilFunc; DepthBoundsTestEnable = depthBoundsTestEnable; } operator D3D12_DEPTH_STENCIL_DESC() const { D3D12_DEPTH_STENCIL_DESC D; D.DepthEnable = DepthEnable; D.DepthWriteMask = DepthWriteMask; D.DepthFunc = DepthFunc; D.StencilEnable = StencilEnable; D.StencilReadMask = StencilReadMask; D.StencilWriteMask = StencilWriteMask; D.FrontFace.StencilFailOp = FrontFace.StencilFailOp; D.FrontFace.StencilDepthFailOp = FrontFace.StencilDepthFailOp; D.FrontFace.StencilPassOp = FrontFace.StencilPassOp; D.FrontFace.StencilFunc = FrontFace.StencilFunc; D.BackFace.StencilFailOp = BackFace.StencilFailOp; D.BackFace.StencilDepthFailOp = BackFace.StencilDepthFailOp; D.BackFace.StencilPassOp = BackFace.StencilPassOp; D.BackFace.StencilFunc = BackFace.StencilFunc; return D; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_BLEND_DESC : public D3D12_BLEND_DESC { CD3DX12_BLEND_DESC() = default; explicit CD3DX12_BLEND_DESC( const D3D12_BLEND_DESC& o ) : D3D12_BLEND_DESC( o ) {} explicit CD3DX12_BLEND_DESC( CD3DX12_DEFAULT ) { AlphaToCoverageEnable = FALSE; IndependentBlendEnable = FALSE; const D3D12_RENDER_TARGET_BLEND_DESC defaultRenderTargetBlendDesc = { FALSE,FALSE, D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD, D3D12_LOGIC_OP_NOOP, D3D12_COLOR_WRITE_ENABLE_ALL, }; for (UINT i = 0; i < D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) RenderTarget[ i ] = defaultRenderTargetBlendDesc; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_RASTERIZER_DESC : public D3D12_RASTERIZER_DESC { CD3DX12_RASTERIZER_DESC() = default; explicit CD3DX12_RASTERIZER_DESC( const D3D12_RASTERIZER_DESC& o ) : D3D12_RASTERIZER_DESC( o ) {} explicit CD3DX12_RASTERIZER_DESC( CD3DX12_DEFAULT ) { FillMode = D3D12_FILL_MODE_SOLID; CullMode = D3D12_CULL_MODE_BACK; FrontCounterClockwise = FALSE; DepthBias = D3D12_DEFAULT_DEPTH_BIAS; DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP; SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS; DepthClipEnable = TRUE; MultisampleEnable = FALSE; AntialiasedLineEnable = FALSE; ForcedSampleCount = 0; ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF; } explicit CD3DX12_RASTERIZER_DESC( D3D12_FILL_MODE fillMode, D3D12_CULL_MODE cullMode, BOOL frontCounterClockwise, INT depthBias, FLOAT depthBiasClamp, FLOAT slopeScaledDepthBias, BOOL depthClipEnable, BOOL multisampleEnable, BOOL antialiasedLineEnable, UINT forcedSampleCount, D3D12_CONSERVATIVE_RASTERIZATION_MODE conservativeRaster) { FillMode = fillMode; CullMode = cullMode; FrontCounterClockwise = frontCounterClockwise; DepthBias = depthBias; DepthBiasClamp = depthBiasClamp; SlopeScaledDepthBias = slopeScaledDepthBias; DepthClipEnable = depthClipEnable; MultisampleEnable = multisampleEnable; AntialiasedLineEnable = antialiasedLineEnable; ForcedSampleCount = forcedSampleCount; ConservativeRaster = conservativeRaster; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_RESOURCE_ALLOCATION_INFO : public D3D12_RESOURCE_ALLOCATION_INFO { CD3DX12_RESOURCE_ALLOCATION_INFO() = default; explicit CD3DX12_RESOURCE_ALLOCATION_INFO( const D3D12_RESOURCE_ALLOCATION_INFO& o ) : D3D12_RESOURCE_ALLOCATION_INFO( o ) {} CD3DX12_RESOURCE_ALLOCATION_INFO( UINT64 size, UINT64 alignment ) { SizeInBytes = size; Alignment = alignment; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_HEAP_PROPERTIES : public D3D12_HEAP_PROPERTIES { CD3DX12_HEAP_PROPERTIES() = default; explicit CD3DX12_HEAP_PROPERTIES(const D3D12_HEAP_PROPERTIES &o) : D3D12_HEAP_PROPERTIES(o) {} CD3DX12_HEAP_PROPERTIES( D3D12_CPU_PAGE_PROPERTY cpuPageProperty, D3D12_MEMORY_POOL memoryPoolPreference, UINT creationNodeMask = 1, UINT nodeMask = 1 ) { Type = D3D12_HEAP_TYPE_CUSTOM; CPUPageProperty = cpuPageProperty; MemoryPoolPreference = memoryPoolPreference; CreationNodeMask = creationNodeMask; VisibleNodeMask = nodeMask; } explicit CD3DX12_HEAP_PROPERTIES( D3D12_HEAP_TYPE type, UINT creationNodeMask = 1, UINT nodeMask = 1 ) { Type = type; CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; CreationNodeMask = creationNodeMask; VisibleNodeMask = nodeMask; } bool IsCPUAccessible() const { return Type == D3D12_HEAP_TYPE_UPLOAD || Type == D3D12_HEAP_TYPE_READBACK || (Type == D3D12_HEAP_TYPE_CUSTOM && (CPUPageProperty == D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE || CPUPageProperty == D3D12_CPU_PAGE_PROPERTY_WRITE_BACK)); } }; inline bool operator==( const D3D12_HEAP_PROPERTIES& l, const D3D12_HEAP_PROPERTIES& r ) { return l.Type == r.Type && l.CPUPageProperty == r.CPUPageProperty && l.MemoryPoolPreference == r.MemoryPoolPreference && l.CreationNodeMask == r.CreationNodeMask && l.VisibleNodeMask == r.VisibleNodeMask; } inline bool operator!=( const D3D12_HEAP_PROPERTIES& l, const D3D12_HEAP_PROPERTIES& r ) { return !( l == r ); } //------------------------------------------------------------------------------------------------ struct CD3DX12_HEAP_DESC : public D3D12_HEAP_DESC { CD3DX12_HEAP_DESC() = default; explicit CD3DX12_HEAP_DESC(const D3D12_HEAP_DESC &o) : D3D12_HEAP_DESC(o) {} CD3DX12_HEAP_DESC( UINT64 size, D3D12_HEAP_PROPERTIES properties, UINT64 alignment = 0, D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE ) { SizeInBytes = size; Properties = properties; Alignment = alignment; Flags = flags; } CD3DX12_HEAP_DESC( UINT64 size, D3D12_HEAP_TYPE type, UINT64 alignment = 0, D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE ) { SizeInBytes = size; Properties = CD3DX12_HEAP_PROPERTIES( type ); Alignment = alignment; Flags = flags; } CD3DX12_HEAP_DESC( UINT64 size, D3D12_CPU_PAGE_PROPERTY cpuPageProperty, D3D12_MEMORY_POOL memoryPoolPreference, UINT64 alignment = 0, D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE ) { SizeInBytes = size; Properties = CD3DX12_HEAP_PROPERTIES( cpuPageProperty, memoryPoolPreference ); Alignment = alignment; Flags = flags; } CD3DX12_HEAP_DESC( const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo, D3D12_HEAP_PROPERTIES properties, D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE ) { SizeInBytes = resAllocInfo.SizeInBytes; Properties = properties; Alignment = resAllocInfo.Alignment; Flags = flags; } CD3DX12_HEAP_DESC( const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo, D3D12_HEAP_TYPE type, D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE ) { SizeInBytes = resAllocInfo.SizeInBytes; Properties = CD3DX12_HEAP_PROPERTIES( type ); Alignment = resAllocInfo.Alignment; Flags = flags; } CD3DX12_HEAP_DESC( const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo, D3D12_CPU_PAGE_PROPERTY cpuPageProperty, D3D12_MEMORY_POOL memoryPoolPreference, D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE ) { SizeInBytes = resAllocInfo.SizeInBytes; Properties = CD3DX12_HEAP_PROPERTIES( cpuPageProperty, memoryPoolPreference ); Alignment = resAllocInfo.Alignment; Flags = flags; } bool IsCPUAccessible() const { return static_cast< const CD3DX12_HEAP_PROPERTIES* >( &Properties )->IsCPUAccessible(); } }; inline bool operator==( const D3D12_HEAP_DESC& l, const D3D12_HEAP_DESC& r ) { return l.SizeInBytes == r.SizeInBytes && l.Properties == r.Properties && l.Alignment == r.Alignment && l.Flags == r.Flags; } inline bool operator!=( const D3D12_HEAP_DESC& l, const D3D12_HEAP_DESC& r ) { return !( l == r ); } //------------------------------------------------------------------------------------------------ struct CD3DX12_CLEAR_VALUE : public D3D12_CLEAR_VALUE { CD3DX12_CLEAR_VALUE() = default; explicit CD3DX12_CLEAR_VALUE(const D3D12_CLEAR_VALUE &o) : D3D12_CLEAR_VALUE(o) {} CD3DX12_CLEAR_VALUE( DXGI_FORMAT format, const FLOAT color[4] ) { Format = format; memcpy( Color, color, sizeof( Color ) ); } CD3DX12_CLEAR_VALUE( DXGI_FORMAT format, FLOAT depth, UINT8 stencil ) { Format = format; /* Use memcpy to preserve NAN values */ memcpy( &DepthStencil.Depth, &depth, sizeof( depth ) ); DepthStencil.Stencil = stencil; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_RANGE : public D3D12_RANGE { CD3DX12_RANGE() = default; explicit CD3DX12_RANGE(const D3D12_RANGE &o) : D3D12_RANGE(o) {} CD3DX12_RANGE( SIZE_T begin, SIZE_T end ) { Begin = begin; End = end; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_RANGE_UINT64 : public D3D12_RANGE_UINT64 { CD3DX12_RANGE_UINT64() = default; explicit CD3DX12_RANGE_UINT64(const D3D12_RANGE_UINT64 &o) : D3D12_RANGE_UINT64(o) {} CD3DX12_RANGE_UINT64( UINT64 begin, UINT64 end ) { Begin = begin; End = end; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_SUBRESOURCE_RANGE_UINT64 : public D3D12_SUBRESOURCE_RANGE_UINT64 { CD3DX12_SUBRESOURCE_RANGE_UINT64() = default; explicit CD3DX12_SUBRESOURCE_RANGE_UINT64(const D3D12_SUBRESOURCE_RANGE_UINT64 &o) : D3D12_SUBRESOURCE_RANGE_UINT64(o) {} CD3DX12_SUBRESOURCE_RANGE_UINT64( UINT subresource, const D3D12_RANGE_UINT64& range ) { Subresource = subresource; Range = range; } CD3DX12_SUBRESOURCE_RANGE_UINT64( UINT subresource, UINT64 begin, UINT64 end ) { Subresource = subresource; Range.Begin = begin; Range.End = end; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_SHADER_BYTECODE : public D3D12_SHADER_BYTECODE { CD3DX12_SHADER_BYTECODE() = default; explicit CD3DX12_SHADER_BYTECODE(const D3D12_SHADER_BYTECODE &o) : D3D12_SHADER_BYTECODE(o) {} CD3DX12_SHADER_BYTECODE( _In_ ID3DBlob* pShaderBlob ) { pShaderBytecode = pShaderBlob->GetBufferPointer(); BytecodeLength = pShaderBlob->GetBufferSize(); } CD3DX12_SHADER_BYTECODE( const void* _pShaderBytecode, SIZE_T bytecodeLength ) { pShaderBytecode = _pShaderBytecode; BytecodeLength = bytecodeLength; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_TILED_RESOURCE_COORDINATE : public D3D12_TILED_RESOURCE_COORDINATE { CD3DX12_TILED_RESOURCE_COORDINATE() = default; explicit CD3DX12_TILED_RESOURCE_COORDINATE(const D3D12_TILED_RESOURCE_COORDINATE &o) : D3D12_TILED_RESOURCE_COORDINATE(o) {} CD3DX12_TILED_RESOURCE_COORDINATE( UINT x, UINT y, UINT z, UINT subresource ) { X = x; Y = y; Z = z; Subresource = subresource; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_TILE_REGION_SIZE : public D3D12_TILE_REGION_SIZE { CD3DX12_TILE_REGION_SIZE() = default; explicit CD3DX12_TILE_REGION_SIZE(const D3D12_TILE_REGION_SIZE &o) : D3D12_TILE_REGION_SIZE(o) {} CD3DX12_TILE_REGION_SIZE( UINT numTiles, BOOL useBox, UINT width, UINT16 height, UINT16 depth ) { NumTiles = numTiles; UseBox = useBox; Width = width; Height = height; Depth = depth; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_SUBRESOURCE_TILING : public D3D12_SUBRESOURCE_TILING { CD3DX12_SUBRESOURCE_TILING() = default; explicit CD3DX12_SUBRESOURCE_TILING(const D3D12_SUBRESOURCE_TILING &o) : D3D12_SUBRESOURCE_TILING(o) {} CD3DX12_SUBRESOURCE_TILING( UINT widthInTiles, UINT16 heightInTiles, UINT16 depthInTiles, UINT startTileIndexInOverallResource ) { WidthInTiles = widthInTiles; HeightInTiles = heightInTiles; DepthInTiles = depthInTiles; StartTileIndexInOverallResource = startTileIndexInOverallResource; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_TILE_SHAPE : public D3D12_TILE_SHAPE { CD3DX12_TILE_SHAPE() = default; explicit CD3DX12_TILE_SHAPE(const D3D12_TILE_SHAPE &o) : D3D12_TILE_SHAPE(o) {} CD3DX12_TILE_SHAPE( UINT widthInTexels, UINT heightInTexels, UINT depthInTexels ) { WidthInTexels = widthInTexels; HeightInTexels = heightInTexels; DepthInTexels = depthInTexels; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_RESOURCE_BARRIER : public D3D12_RESOURCE_BARRIER { CD3DX12_RESOURCE_BARRIER() = default; explicit CD3DX12_RESOURCE_BARRIER(const D3D12_RESOURCE_BARRIER &o) : D3D12_RESOURCE_BARRIER(o) {} static inline CD3DX12_RESOURCE_BARRIER Transition( _In_ ID3D12Resource* pResource, D3D12_RESOURCE_STATES stateBefore, D3D12_RESOURCE_STATES stateAfter, UINT subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, D3D12_RESOURCE_BARRIER_FLAGS flags = D3D12_RESOURCE_BARRIER_FLAG_NONE) { CD3DX12_RESOURCE_BARRIER result = {}; D3D12_RESOURCE_BARRIER &barrier = result; result.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; result.Flags = flags; barrier.Transition.pResource = pResource; barrier.Transition.StateBefore = stateBefore; barrier.Transition.StateAfter = stateAfter; barrier.Transition.Subresource = subresource; return result; } static inline CD3DX12_RESOURCE_BARRIER Aliasing( _In_ ID3D12Resource* pResourceBefore, _In_ ID3D12Resource* pResourceAfter) { CD3DX12_RESOURCE_BARRIER result = {}; D3D12_RESOURCE_BARRIER &barrier = result; result.Type = D3D12_RESOURCE_BARRIER_TYPE_ALIASING; barrier.Aliasing.pResourceBefore = pResourceBefore; barrier.Aliasing.pResourceAfter = pResourceAfter; return result; } static inline CD3DX12_RESOURCE_BARRIER UAV( _In_ ID3D12Resource* pResource) { CD3DX12_RESOURCE_BARRIER result = {}; D3D12_RESOURCE_BARRIER &barrier = result; result.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; barrier.UAV.pResource = pResource; return result; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_PACKED_MIP_INFO : public D3D12_PACKED_MIP_INFO { CD3DX12_PACKED_MIP_INFO() = default; explicit CD3DX12_PACKED_MIP_INFO(const D3D12_PACKED_MIP_INFO &o) : D3D12_PACKED_MIP_INFO(o) {} CD3DX12_PACKED_MIP_INFO( UINT8 numStandardMips, UINT8 numPackedMips, UINT numTilesForPackedMips, UINT startTileIndexInOverallResource ) { NumStandardMips = numStandardMips; NumPackedMips = numPackedMips; NumTilesForPackedMips = numTilesForPackedMips; StartTileIndexInOverallResource = startTileIndexInOverallResource; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_SUBRESOURCE_FOOTPRINT : public D3D12_SUBRESOURCE_FOOTPRINT { CD3DX12_SUBRESOURCE_FOOTPRINT() = default; explicit CD3DX12_SUBRESOURCE_FOOTPRINT(const D3D12_SUBRESOURCE_FOOTPRINT &o) : D3D12_SUBRESOURCE_FOOTPRINT(o) {} CD3DX12_SUBRESOURCE_FOOTPRINT( DXGI_FORMAT format, UINT width, UINT height, UINT depth, UINT rowPitch ) { Format = format; Width = width; Height = height; Depth = depth; RowPitch = rowPitch; } explicit CD3DX12_SUBRESOURCE_FOOTPRINT( const D3D12_RESOURCE_DESC& resDesc, UINT rowPitch ) { Format = resDesc.Format; Width = UINT( resDesc.Width ); Height = resDesc.Height; Depth = (resDesc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? resDesc.DepthOrArraySize : 1); RowPitch = rowPitch; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_TEXTURE_COPY_LOCATION : public D3D12_TEXTURE_COPY_LOCATION { CD3DX12_TEXTURE_COPY_LOCATION() = default; explicit CD3DX12_TEXTURE_COPY_LOCATION(const D3D12_TEXTURE_COPY_LOCATION &o) : D3D12_TEXTURE_COPY_LOCATION(o) {} CD3DX12_TEXTURE_COPY_LOCATION(_In_ ID3D12Resource* pRes) { pResource = pRes; Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; PlacedFootprint = {}; } CD3DX12_TEXTURE_COPY_LOCATION(_In_ ID3D12Resource* pRes, D3D12_PLACED_SUBRESOURCE_FOOTPRINT const& Footprint) { pResource = pRes; Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; PlacedFootprint = Footprint; } CD3DX12_TEXTURE_COPY_LOCATION(_In_ ID3D12Resource* pRes, UINT Sub) { pResource = pRes; Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; SubresourceIndex = Sub; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_DESCRIPTOR_RANGE : public D3D12_DESCRIPTOR_RANGE { CD3DX12_DESCRIPTOR_RANGE() = default; explicit CD3DX12_DESCRIPTOR_RANGE(const D3D12_DESCRIPTOR_RANGE &o) : D3D12_DESCRIPTOR_RANGE(o) {} CD3DX12_DESCRIPTOR_RANGE( D3D12_DESCRIPTOR_RANGE_TYPE rangeType, UINT numDescriptors, UINT baseShaderRegister, UINT registerSpace = 0, UINT offsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND) { Init(rangeType, numDescriptors, baseShaderRegister, registerSpace, offsetInDescriptorsFromTableStart); } inline void Init( D3D12_DESCRIPTOR_RANGE_TYPE rangeType, UINT numDescriptors, UINT baseShaderRegister, UINT registerSpace = 0, UINT offsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND) { Init(*this, rangeType, numDescriptors, baseShaderRegister, registerSpace, offsetInDescriptorsFromTableStart); } static inline void Init( _Out_ D3D12_DESCRIPTOR_RANGE &range, D3D12_DESCRIPTOR_RANGE_TYPE rangeType, UINT numDescriptors, UINT baseShaderRegister, UINT registerSpace = 0, UINT offsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND) { range.RangeType = rangeType; range.NumDescriptors = numDescriptors; range.BaseShaderRegister = baseShaderRegister; range.RegisterSpace = registerSpace; range.OffsetInDescriptorsFromTableStart = offsetInDescriptorsFromTableStart; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_ROOT_DESCRIPTOR_TABLE : public D3D12_ROOT_DESCRIPTOR_TABLE { CD3DX12_ROOT_DESCRIPTOR_TABLE() = default; explicit CD3DX12_ROOT_DESCRIPTOR_TABLE(const D3D12_ROOT_DESCRIPTOR_TABLE &o) : D3D12_ROOT_DESCRIPTOR_TABLE(o) {} CD3DX12_ROOT_DESCRIPTOR_TABLE( UINT numDescriptorRanges, _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges) { Init(numDescriptorRanges, _pDescriptorRanges); } inline void Init( UINT numDescriptorRanges, _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges) { Init(*this, numDescriptorRanges, _pDescriptorRanges); } static inline void Init( _Out_ D3D12_ROOT_DESCRIPTOR_TABLE &rootDescriptorTable, UINT numDescriptorRanges, _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges) { rootDescriptorTable.NumDescriptorRanges = numDescriptorRanges; rootDescriptorTable.pDescriptorRanges = _pDescriptorRanges; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_ROOT_CONSTANTS : public D3D12_ROOT_CONSTANTS { CD3DX12_ROOT_CONSTANTS() = default; explicit CD3DX12_ROOT_CONSTANTS(const D3D12_ROOT_CONSTANTS &o) : D3D12_ROOT_CONSTANTS(o) {} CD3DX12_ROOT_CONSTANTS( UINT num32BitValues, UINT shaderRegister, UINT registerSpace = 0) { Init(num32BitValues, shaderRegister, registerSpace); } inline void Init( UINT num32BitValues, UINT shaderRegister, UINT registerSpace = 0) { Init(*this, num32BitValues, shaderRegister, registerSpace); } static inline void Init( _Out_ D3D12_ROOT_CONSTANTS &rootConstants, UINT num32BitValues, UINT shaderRegister, UINT registerSpace = 0) { rootConstants.Num32BitValues = num32BitValues; rootConstants.ShaderRegister = shaderRegister; rootConstants.RegisterSpace = registerSpace; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_ROOT_DESCRIPTOR : public D3D12_ROOT_DESCRIPTOR { CD3DX12_ROOT_DESCRIPTOR() = default; explicit CD3DX12_ROOT_DESCRIPTOR(const D3D12_ROOT_DESCRIPTOR &o) : D3D12_ROOT_DESCRIPTOR(o) {} CD3DX12_ROOT_DESCRIPTOR( UINT shaderRegister, UINT registerSpace = 0) { Init(shaderRegister, registerSpace); } inline void Init( UINT shaderRegister, UINT registerSpace = 0) { Init(*this, shaderRegister, registerSpace); } static inline void Init(_Out_ D3D12_ROOT_DESCRIPTOR &table, UINT shaderRegister, UINT registerSpace = 0) { table.ShaderRegister = shaderRegister; table.RegisterSpace = registerSpace; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_ROOT_PARAMETER : public D3D12_ROOT_PARAMETER { CD3DX12_ROOT_PARAMETER() = default; explicit CD3DX12_ROOT_PARAMETER(const D3D12_ROOT_PARAMETER &o) : D3D12_ROOT_PARAMETER(o) {} static inline void InitAsDescriptorTable( _Out_ D3D12_ROOT_PARAMETER &rootParam, UINT numDescriptorRanges, _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* pDescriptorRanges, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_DESCRIPTOR_TABLE::Init(rootParam.DescriptorTable, numDescriptorRanges, pDescriptorRanges); } static inline void InitAsConstants( _Out_ D3D12_ROOT_PARAMETER &rootParam, UINT num32BitValues, UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_CONSTANTS::Init(rootParam.Constants, num32BitValues, shaderRegister, registerSpace); } static inline void InitAsConstantBufferView( _Out_ D3D12_ROOT_PARAMETER &rootParam, UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_DESCRIPTOR::Init(rootParam.Descriptor, shaderRegister, registerSpace); } static inline void InitAsShaderResourceView( _Out_ D3D12_ROOT_PARAMETER &rootParam, UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_DESCRIPTOR::Init(rootParam.Descriptor, shaderRegister, registerSpace); } static inline void InitAsUnorderedAccessView( _Out_ D3D12_ROOT_PARAMETER &rootParam, UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_DESCRIPTOR::Init(rootParam.Descriptor, shaderRegister, registerSpace); } inline void InitAsDescriptorTable( UINT numDescriptorRanges, _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* pDescriptorRanges, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsDescriptorTable(*this, numDescriptorRanges, pDescriptorRanges, visibility); } inline void InitAsConstants( UINT num32BitValues, UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsConstants(*this, num32BitValues, shaderRegister, registerSpace, visibility); } inline void InitAsConstantBufferView( UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsConstantBufferView(*this, shaderRegister, registerSpace, visibility); } inline void InitAsShaderResourceView( UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsShaderResourceView(*this, shaderRegister, registerSpace, visibility); } inline void InitAsUnorderedAccessView( UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsUnorderedAccessView(*this, shaderRegister, registerSpace, visibility); } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_STATIC_SAMPLER_DESC : public D3D12_STATIC_SAMPLER_DESC { CD3DX12_STATIC_SAMPLER_DESC() = default; explicit CD3DX12_STATIC_SAMPLER_DESC(const D3D12_STATIC_SAMPLER_DESC &o) : D3D12_STATIC_SAMPLER_DESC(o) {} CD3DX12_STATIC_SAMPLER_DESC( UINT shaderRegister, D3D12_FILTER filter = D3D12_FILTER_ANISOTROPIC, D3D12_TEXTURE_ADDRESS_MODE addressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP, D3D12_TEXTURE_ADDRESS_MODE addressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP, D3D12_TEXTURE_ADDRESS_MODE addressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP, FLOAT mipLODBias = 0, UINT maxAnisotropy = 16, D3D12_COMPARISON_FUNC comparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL, D3D12_STATIC_BORDER_COLOR borderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE, FLOAT minLOD = 0.f, FLOAT maxLOD = D3D12_FLOAT32_MAX, D3D12_SHADER_VISIBILITY shaderVisibility = D3D12_SHADER_VISIBILITY_ALL, UINT registerSpace = 0) { Init( shaderRegister, filter, addressU, addressV, addressW, mipLODBias, maxAnisotropy, comparisonFunc, borderColor, minLOD, maxLOD, shaderVisibility, registerSpace); } static inline void Init( _Out_ D3D12_STATIC_SAMPLER_DESC &samplerDesc, UINT shaderRegister, D3D12_FILTER filter = D3D12_FILTER_ANISOTROPIC, D3D12_TEXTURE_ADDRESS_MODE addressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP, D3D12_TEXTURE_ADDRESS_MODE addressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP, D3D12_TEXTURE_ADDRESS_MODE addressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP, FLOAT mipLODBias = 0, UINT maxAnisotropy = 16, D3D12_COMPARISON_FUNC comparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL, D3D12_STATIC_BORDER_COLOR borderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE, FLOAT minLOD = 0.f, FLOAT maxLOD = D3D12_FLOAT32_MAX, D3D12_SHADER_VISIBILITY shaderVisibility = D3D12_SHADER_VISIBILITY_ALL, UINT registerSpace = 0) { samplerDesc.ShaderRegister = shaderRegister; samplerDesc.Filter = filter; samplerDesc.AddressU = addressU; samplerDesc.AddressV = addressV; samplerDesc.AddressW = addressW; samplerDesc.MipLODBias = mipLODBias; samplerDesc.MaxAnisotropy = maxAnisotropy; samplerDesc.ComparisonFunc = comparisonFunc; samplerDesc.BorderColor = borderColor; samplerDesc.MinLOD = minLOD; samplerDesc.MaxLOD = maxLOD; samplerDesc.ShaderVisibility = shaderVisibility; samplerDesc.RegisterSpace = registerSpace; } inline void Init( UINT shaderRegister, D3D12_FILTER filter = D3D12_FILTER_ANISOTROPIC, D3D12_TEXTURE_ADDRESS_MODE addressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP, D3D12_TEXTURE_ADDRESS_MODE addressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP, D3D12_TEXTURE_ADDRESS_MODE addressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP, FLOAT mipLODBias = 0, UINT maxAnisotropy = 16, D3D12_COMPARISON_FUNC comparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL, D3D12_STATIC_BORDER_COLOR borderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE, FLOAT minLOD = 0.f, FLOAT maxLOD = D3D12_FLOAT32_MAX, D3D12_SHADER_VISIBILITY shaderVisibility = D3D12_SHADER_VISIBILITY_ALL, UINT registerSpace = 0) { Init( *this, shaderRegister, filter, addressU, addressV, addressW, mipLODBias, maxAnisotropy, comparisonFunc, borderColor, minLOD, maxLOD, shaderVisibility, registerSpace); } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_ROOT_SIGNATURE_DESC : public D3D12_ROOT_SIGNATURE_DESC { CD3DX12_ROOT_SIGNATURE_DESC() = default; explicit CD3DX12_ROOT_SIGNATURE_DESC(const D3D12_ROOT_SIGNATURE_DESC &o) : D3D12_ROOT_SIGNATURE_DESC(o) {} CD3DX12_ROOT_SIGNATURE_DESC( UINT numParameters, _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters, UINT numStaticSamplers = 0, _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr, D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) { Init(numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags); } CD3DX12_ROOT_SIGNATURE_DESC(CD3DX12_DEFAULT) { Init(0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE); } inline void Init( UINT numParameters, _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters, UINT numStaticSamplers = 0, _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr, D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) { Init(*this, numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags); } static inline void Init( _Out_ D3D12_ROOT_SIGNATURE_DESC &desc, UINT numParameters, _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters, UINT numStaticSamplers = 0, _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr, D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) { desc.NumParameters = numParameters; desc.pParameters = _pParameters; desc.NumStaticSamplers = numStaticSamplers; desc.pStaticSamplers = _pStaticSamplers; desc.Flags = flags; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_DESCRIPTOR_RANGE1 : public D3D12_DESCRIPTOR_RANGE1 { CD3DX12_DESCRIPTOR_RANGE1() = default; explicit CD3DX12_DESCRIPTOR_RANGE1(const D3D12_DESCRIPTOR_RANGE1 &o) : D3D12_DESCRIPTOR_RANGE1(o) {} CD3DX12_DESCRIPTOR_RANGE1( D3D12_DESCRIPTOR_RANGE_TYPE rangeType, UINT numDescriptors, UINT baseShaderRegister, UINT registerSpace = 0, D3D12_DESCRIPTOR_RANGE_FLAGS flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE, UINT offsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND) { Init(rangeType, numDescriptors, baseShaderRegister, registerSpace, flags, offsetInDescriptorsFromTableStart); } inline void Init( D3D12_DESCRIPTOR_RANGE_TYPE rangeType, UINT numDescriptors, UINT baseShaderRegister, UINT registerSpace = 0, D3D12_DESCRIPTOR_RANGE_FLAGS flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE, UINT offsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND) { Init(*this, rangeType, numDescriptors, baseShaderRegister, registerSpace, flags, offsetInDescriptorsFromTableStart); } static inline void Init( _Out_ D3D12_DESCRIPTOR_RANGE1 &range, D3D12_DESCRIPTOR_RANGE_TYPE rangeType, UINT numDescriptors, UINT baseShaderRegister, UINT registerSpace = 0, D3D12_DESCRIPTOR_RANGE_FLAGS flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE, UINT offsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND) { range.RangeType = rangeType; range.NumDescriptors = numDescriptors; range.BaseShaderRegister = baseShaderRegister; range.RegisterSpace = registerSpace; range.Flags = flags; range.OffsetInDescriptorsFromTableStart = offsetInDescriptorsFromTableStart; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_ROOT_DESCRIPTOR_TABLE1 : public D3D12_ROOT_DESCRIPTOR_TABLE1 { CD3DX12_ROOT_DESCRIPTOR_TABLE1() = default; explicit CD3DX12_ROOT_DESCRIPTOR_TABLE1(const D3D12_ROOT_DESCRIPTOR_TABLE1 &o) : D3D12_ROOT_DESCRIPTOR_TABLE1(o) {} CD3DX12_ROOT_DESCRIPTOR_TABLE1( UINT numDescriptorRanges, _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* _pDescriptorRanges) { Init(numDescriptorRanges, _pDescriptorRanges); } inline void Init( UINT numDescriptorRanges, _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* _pDescriptorRanges) { Init(*this, numDescriptorRanges, _pDescriptorRanges); } static inline void Init( _Out_ D3D12_ROOT_DESCRIPTOR_TABLE1 &rootDescriptorTable, UINT numDescriptorRanges, _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* _pDescriptorRanges) { rootDescriptorTable.NumDescriptorRanges = numDescriptorRanges; rootDescriptorTable.pDescriptorRanges = _pDescriptorRanges; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_ROOT_DESCRIPTOR1 : public D3D12_ROOT_DESCRIPTOR1 { CD3DX12_ROOT_DESCRIPTOR1() = default; explicit CD3DX12_ROOT_DESCRIPTOR1(const D3D12_ROOT_DESCRIPTOR1 &o) : D3D12_ROOT_DESCRIPTOR1(o) {} CD3DX12_ROOT_DESCRIPTOR1( UINT shaderRegister, UINT registerSpace = 0, D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE) { Init(shaderRegister, registerSpace, flags); } inline void Init( UINT shaderRegister, UINT registerSpace = 0, D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE) { Init(*this, shaderRegister, registerSpace, flags); } static inline void Init( _Out_ D3D12_ROOT_DESCRIPTOR1 &table, UINT shaderRegister, UINT registerSpace = 0, D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE) { table.ShaderRegister = shaderRegister; table.RegisterSpace = registerSpace; table.Flags = flags; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_ROOT_PARAMETER1 : public D3D12_ROOT_PARAMETER1 { CD3DX12_ROOT_PARAMETER1() = default; explicit CD3DX12_ROOT_PARAMETER1(const D3D12_ROOT_PARAMETER1 &o) : D3D12_ROOT_PARAMETER1(o) {} static inline void InitAsDescriptorTable( _Out_ D3D12_ROOT_PARAMETER1 &rootParam, UINT numDescriptorRanges, _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* pDescriptorRanges, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_DESCRIPTOR_TABLE1::Init(rootParam.DescriptorTable, numDescriptorRanges, pDescriptorRanges); } static inline void InitAsConstants( _Out_ D3D12_ROOT_PARAMETER1 &rootParam, UINT num32BitValues, UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_CONSTANTS::Init(rootParam.Constants, num32BitValues, shaderRegister, registerSpace); } static inline void InitAsConstantBufferView( _Out_ D3D12_ROOT_PARAMETER1 &rootParam, UINT shaderRegister, UINT registerSpace = 0, D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_DESCRIPTOR1::Init(rootParam.Descriptor, shaderRegister, registerSpace, flags); } static inline void InitAsShaderResourceView( _Out_ D3D12_ROOT_PARAMETER1 &rootParam, UINT shaderRegister, UINT registerSpace = 0, D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_DESCRIPTOR1::Init(rootParam.Descriptor, shaderRegister, registerSpace, flags); } static inline void InitAsUnorderedAccessView( _Out_ D3D12_ROOT_PARAMETER1 &rootParam, UINT shaderRegister, UINT registerSpace = 0, D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV; rootParam.ShaderVisibility = visibility; CD3DX12_ROOT_DESCRIPTOR1::Init(rootParam.Descriptor, shaderRegister, registerSpace, flags); } inline void InitAsDescriptorTable( UINT numDescriptorRanges, _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE1* pDescriptorRanges, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsDescriptorTable(*this, numDescriptorRanges, pDescriptorRanges, visibility); } inline void InitAsConstants( UINT num32BitValues, UINT shaderRegister, UINT registerSpace = 0, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsConstants(*this, num32BitValues, shaderRegister, registerSpace, visibility); } inline void InitAsConstantBufferView( UINT shaderRegister, UINT registerSpace = 0, D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsConstantBufferView(*this, shaderRegister, registerSpace, flags, visibility); } inline void InitAsShaderResourceView( UINT shaderRegister, UINT registerSpace = 0, D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsShaderResourceView(*this, shaderRegister, registerSpace, flags, visibility); } inline void InitAsUnorderedAccessView( UINT shaderRegister, UINT registerSpace = 0, D3D12_ROOT_DESCRIPTOR_FLAGS flags = D3D12_ROOT_DESCRIPTOR_FLAG_NONE, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { InitAsUnorderedAccessView(*this, shaderRegister, registerSpace, flags, visibility); } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC : public D3D12_VERSIONED_ROOT_SIGNATURE_DESC { CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC() = default; explicit CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC &o) : D3D12_VERSIONED_ROOT_SIGNATURE_DESC(o) {} explicit CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(const D3D12_ROOT_SIGNATURE_DESC &o) { Version = D3D_ROOT_SIGNATURE_VERSION_1_0; Desc_1_0 = o; } explicit CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(const D3D12_ROOT_SIGNATURE_DESC1 &o) { Version = D3D_ROOT_SIGNATURE_VERSION_1_1; Desc_1_1 = o; } CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC( UINT numParameters, _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters, UINT numStaticSamplers = 0, _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr, D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) { Init_1_0(numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags); } CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC( UINT numParameters, _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER1* _pParameters, UINT numStaticSamplers = 0, _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr, D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) { Init_1_1(numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags); } CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC(CD3DX12_DEFAULT) { Init_1_1(0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE); } inline void Init_1_0( UINT numParameters, _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters, UINT numStaticSamplers = 0, _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr, D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) { Init_1_0(*this, numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags); } static inline void Init_1_0( _Out_ D3D12_VERSIONED_ROOT_SIGNATURE_DESC &desc, UINT numParameters, _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters, UINT numStaticSamplers = 0, _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr, D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) { desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; desc.Desc_1_0.NumParameters = numParameters; desc.Desc_1_0.pParameters = _pParameters; desc.Desc_1_0.NumStaticSamplers = numStaticSamplers; desc.Desc_1_0.pStaticSamplers = _pStaticSamplers; desc.Desc_1_0.Flags = flags; } inline void Init_1_1( UINT numParameters, _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER1* _pParameters, UINT numStaticSamplers = 0, _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr, D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) { Init_1_1(*this, numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags); } static inline void Init_1_1( _Out_ D3D12_VERSIONED_ROOT_SIGNATURE_DESC &desc, UINT numParameters, _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER1* _pParameters, UINT numStaticSamplers = 0, _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = nullptr, D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE) { desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_1; desc.Desc_1_1.NumParameters = numParameters; desc.Desc_1_1.pParameters = _pParameters; desc.Desc_1_1.NumStaticSamplers = numStaticSamplers; desc.Desc_1_1.pStaticSamplers = _pStaticSamplers; desc.Desc_1_1.Flags = flags; } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_CPU_DESCRIPTOR_HANDLE : public D3D12_CPU_DESCRIPTOR_HANDLE { CD3DX12_CPU_DESCRIPTOR_HANDLE() = default; explicit CD3DX12_CPU_DESCRIPTOR_HANDLE(const D3D12_CPU_DESCRIPTOR_HANDLE &o) : D3D12_CPU_DESCRIPTOR_HANDLE(o) {} CD3DX12_CPU_DESCRIPTOR_HANDLE(CD3DX12_DEFAULT) { ptr = 0; } CD3DX12_CPU_DESCRIPTOR_HANDLE(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &other, INT offsetScaledByIncrementSize) { InitOffsetted(other, offsetScaledByIncrementSize); } CD3DX12_CPU_DESCRIPTOR_HANDLE(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &other, INT offsetInDescriptors, UINT descriptorIncrementSize) { InitOffsetted(other, offsetInDescriptors, descriptorIncrementSize); } CD3DX12_CPU_DESCRIPTOR_HANDLE& Offset(INT offsetInDescriptors, UINT descriptorIncrementSize) { ptr += INT64(offsetInDescriptors) * UINT64(descriptorIncrementSize); return *this; } CD3DX12_CPU_DESCRIPTOR_HANDLE& Offset(INT offsetScaledByIncrementSize) { ptr += offsetScaledByIncrementSize; return *this; } bool operator==(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE& other) const { return (ptr == other.ptr); } bool operator!=(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE& other) const { return (ptr != other.ptr); } CD3DX12_CPU_DESCRIPTOR_HANDLE &operator=(const D3D12_CPU_DESCRIPTOR_HANDLE &other) { ptr = other.ptr; return *this; } inline void InitOffsetted(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize) { InitOffsetted(*this, base, offsetScaledByIncrementSize); } inline void InitOffsetted(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize) { InitOffsetted(*this, base, offsetInDescriptors, descriptorIncrementSize); } static inline void InitOffsetted(_Out_ D3D12_CPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize) { handle.ptr = base.ptr + offsetScaledByIncrementSize; } static inline void InitOffsetted(_Out_ D3D12_CPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize) { handle.ptr = static_cast(base.ptr + INT64(offsetInDescriptors) * UINT64(descriptorIncrementSize)); } }; //------------------------------------------------------------------------------------------------ struct CD3DX12_GPU_DESCRIPTOR_HANDLE : public D3D12_GPU_DESCRIPTOR_HANDLE { CD3DX12_GPU_DESCRIPTOR_HANDLE() = default; explicit CD3DX12_GPU_DESCRIPTOR_HANDLE(const D3D12_GPU_DESCRIPTOR_HANDLE &o) : D3D12_GPU_DESCRIPTOR_HANDLE(o) {} CD3DX12_GPU_DESCRIPTOR_HANDLE(CD3DX12_DEFAULT) { ptr = 0; } CD3DX12_GPU_DESCRIPTOR_HANDLE(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &other, INT offsetScaledByIncrementSize) { InitOffsetted(other, offsetScaledByIncrementSize); } CD3DX12_GPU_DESCRIPTOR_HANDLE(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &other, INT offsetInDescriptors, UINT descriptorIncrementSize) { InitOffsetted(other, offsetInDescriptors, descriptorIncrementSize); } CD3DX12_GPU_DESCRIPTOR_HANDLE& Offset(INT offsetInDescriptors, UINT descriptorIncrementSize) { ptr += INT64(offsetInDescriptors) * UINT64(descriptorIncrementSize); return *this; } CD3DX12_GPU_DESCRIPTOR_HANDLE& Offset(INT offsetScaledByIncrementSize) { ptr += offsetScaledByIncrementSize; return *this; } inline bool operator==(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE& other) const { return (ptr == other.ptr); } inline bool operator!=(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE& other) const { return (ptr != other.ptr); } CD3DX12_GPU_DESCRIPTOR_HANDLE &operator=(const D3D12_GPU_DESCRIPTOR_HANDLE &other) { ptr = other.ptr; return *this; } inline void InitOffsetted(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize) { InitOffsetted(*this, base, offsetScaledByIncrementSize); } inline void InitOffsetted(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize) { InitOffsetted(*this, base, offsetInDescriptors, descriptorIncrementSize); } static inline void InitOffsetted(_Out_ D3D12_GPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize) { handle.ptr = base.ptr + offsetScaledByIncrementSize; } static inline void InitOffsetted(_Out_ D3D12_GPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize) { handle.ptr = static_cast(base.ptr + INT64(offsetInDescriptors) * UINT64(descriptorIncrementSize)); } }; //------------------------------------------------------------------------------------------------ inline UINT D3D12CalcSubresource( UINT MipSlice, UINT ArraySlice, UINT PlaneSlice, UINT MipLevels, UINT ArraySize ) { return MipSlice + ArraySlice * MipLevels + PlaneSlice * MipLevels * ArraySize; } //------------------------------------------------------------------------------------------------ template inline void D3D12DecomposeSubresource( UINT Subresource, UINT MipLevels, UINT ArraySize, _Out_ T& MipSlice, _Out_ U& ArraySlice, _Out_ V& PlaneSlice ) { MipSlice = static_cast(Subresource % MipLevels); ArraySlice = static_cast((Subresource / MipLevels) % ArraySize); PlaneSlice = static_cast(Subresource / (MipLevels * ArraySize)); } //------------------------------------------------------------------------------------------------ inline UINT8 D3D12GetFormatPlaneCount( _In_ ID3D12Device* pDevice, DXGI_FORMAT Format ) { D3D12_FEATURE_DATA_FORMAT_INFO formatInfo = { Format, 0 }; if (FAILED(pDevice->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, &formatInfo, sizeof(formatInfo)))) { return 0; } return formatInfo.PlaneCount; } //------------------------------------------------------------------------------------------------ struct CD3DX12_RESOURCE_DESC : public D3D12_RESOURCE_DESC { CD3DX12_RESOURCE_DESC() = default; explicit CD3DX12_RESOURCE_DESC( const D3D12_RESOURCE_DESC& o ) : D3D12_RESOURCE_DESC( o ) {} CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION dimension, UINT64 alignment, UINT64 width, UINT height, UINT16 depthOrArraySize, UINT16 mipLevels, DXGI_FORMAT format, UINT sampleCount, UINT sampleQuality, D3D12_TEXTURE_LAYOUT layout, D3D12_RESOURCE_FLAGS flags ) { Dimension = dimension; Alignment = alignment; Width = width; Height = height; DepthOrArraySize = depthOrArraySize; MipLevels = mipLevels; Format = format; SampleDesc.Count = sampleCount; SampleDesc.Quality = sampleQuality; Layout = layout; Flags = flags; } static inline CD3DX12_RESOURCE_DESC Buffer( const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo, D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE ) { return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_BUFFER, resAllocInfo.Alignment, resAllocInfo.SizeInBytes, 1, 1, 1, DXGI_FORMAT_UNKNOWN, 1, 0, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, flags ); } static inline CD3DX12_RESOURCE_DESC Buffer( UINT64 width, D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE, UINT64 alignment = 0 ) { return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_BUFFER, alignment, width, 1, 1, 1, DXGI_FORMAT_UNKNOWN, 1, 0, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, flags ); } static inline CD3DX12_RESOURCE_DESC Tex1D( DXGI_FORMAT format, UINT64 width, UINT16 arraySize = 1, UINT16 mipLevels = 0, D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE, D3D12_TEXTURE_LAYOUT layout = D3D12_TEXTURE_LAYOUT_UNKNOWN, UINT64 alignment = 0 ) { return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_TEXTURE1D, alignment, width, 1, arraySize, mipLevels, format, 1, 0, layout, flags ); } static inline CD3DX12_RESOURCE_DESC Tex2D( DXGI_FORMAT format, UINT64 width, UINT height, UINT16 arraySize = 1, UINT16 mipLevels = 0, UINT sampleCount = 1, UINT sampleQuality = 0, D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE, D3D12_TEXTURE_LAYOUT layout = D3D12_TEXTURE_LAYOUT_UNKNOWN, UINT64 alignment = 0 ) { return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_TEXTURE2D, alignment, width, height, arraySize, mipLevels, format, sampleCount, sampleQuality, layout, flags ); } static inline CD3DX12_RESOURCE_DESC Tex3D( DXGI_FORMAT format, UINT64 width, UINT height, UINT16 depth, UINT16 mipLevels = 0, D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE, D3D12_TEXTURE_LAYOUT layout = D3D12_TEXTURE_LAYOUT_UNKNOWN, UINT64 alignment = 0 ) { return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_TEXTURE3D, alignment, width, height, depth, mipLevels, format, 1, 0, layout, flags ); } inline UINT16 Depth() const { return (Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? DepthOrArraySize : 1); } inline UINT16 ArraySize() const { return (Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D ? DepthOrArraySize : 1); } inline UINT8 PlaneCount(_In_ ID3D12Device* pDevice) const { return D3D12GetFormatPlaneCount(pDevice, Format); } inline UINT Subresources(_In_ ID3D12Device* pDevice) const { return MipLevels * ArraySize() * PlaneCount(pDevice); } inline UINT CalcSubresource(UINT MipSlice, UINT ArraySlice, UINT PlaneSlice) { return D3D12CalcSubresource(MipSlice, ArraySlice, PlaneSlice, MipLevels, ArraySize()); } }; inline bool operator==( const D3D12_RESOURCE_DESC& l, const D3D12_RESOURCE_DESC& r ) { return l.Dimension == r.Dimension && l.Alignment == r.Alignment && l.Width == r.Width && l.Height == r.Height && l.DepthOrArraySize == r.DepthOrArraySize && l.MipLevels == r.MipLevels && l.Format == r.Format && l.SampleDesc.Count == r.SampleDesc.Count && l.SampleDesc.Quality == r.SampleDesc.Quality && l.Layout == r.Layout && l.Flags == r.Flags; } inline bool operator!=( const D3D12_RESOURCE_DESC& l, const D3D12_RESOURCE_DESC& r ) { return !( l == r ); } //------------------------------------------------------------------------------------------------ struct CD3DX12_VIEW_INSTANCING_DESC : public D3D12_VIEW_INSTANCING_DESC { CD3DX12_VIEW_INSTANCING_DESC() = default; explicit CD3DX12_VIEW_INSTANCING_DESC( const D3D12_VIEW_INSTANCING_DESC& o ) : D3D12_VIEW_INSTANCING_DESC( o ) {} explicit CD3DX12_VIEW_INSTANCING_DESC( CD3DX12_DEFAULT ) { ViewInstanceCount = 0; pViewInstanceLocations = nullptr; Flags = D3D12_VIEW_INSTANCING_FLAG_NONE; } explicit CD3DX12_VIEW_INSTANCING_DESC( UINT InViewInstanceCount, const D3D12_VIEW_INSTANCE_LOCATION* InViewInstanceLocations, D3D12_VIEW_INSTANCING_FLAGS InFlags) { ViewInstanceCount = InViewInstanceCount; pViewInstanceLocations = InViewInstanceLocations; Flags = InFlags; } }; //------------------------------------------------------------------------------------------------ // Row-by-row memcpy inline void MemcpySubresource( _In_ const D3D12_MEMCPY_DEST* pDest, _In_ const D3D12_SUBRESOURCE_DATA* pSrc, SIZE_T RowSizeInBytes, UINT NumRows, UINT NumSlices) { for (UINT z = 0; z < NumSlices; ++z) { BYTE* pDestSlice = reinterpret_cast(pDest->pData) + pDest->SlicePitch * z; const BYTE* pSrcSlice = reinterpret_cast(pSrc->pData) + pSrc->SlicePitch * z; for (UINT y = 0; y < NumRows; ++y) { memcpy(pDestSlice + pDest->RowPitch * y, pSrcSlice + pSrc->RowPitch * y, RowSizeInBytes); } } } //------------------------------------------------------------------------------------------------ // Returns required size of a buffer to be used for data upload inline UINT64 GetRequiredIntermediateSize( _In_ ID3D12Resource* pDestinationResource, _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource, _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources) { auto Desc = pDestinationResource->GetDesc(); UINT64 RequiredSize = 0; ID3D12Device* pDevice = nullptr; pDestinationResource->GetDevice(__uuidof(*pDevice), reinterpret_cast(&pDevice)); pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, 0, nullptr, nullptr, nullptr, &RequiredSize); pDevice->Release(); return RequiredSize; } //------------------------------------------------------------------------------------------------ // All arrays must be populated (e.g. by calling GetCopyableFootprints) inline UINT64 UpdateSubresources( _In_ ID3D12GraphicsCommandList* pCmdList, _In_ ID3D12Resource* pDestinationResource, _In_ ID3D12Resource* pIntermediate, _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource, _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources, UINT64 RequiredSize, _In_reads_(NumSubresources) const D3D12_PLACED_SUBRESOURCE_FOOTPRINT* pLayouts, _In_reads_(NumSubresources) const UINT* pNumRows, _In_reads_(NumSubresources) const UINT64* pRowSizesInBytes, _In_reads_(NumSubresources) const D3D12_SUBRESOURCE_DATA* pSrcData) { // Minor validation auto IntermediateDesc = pIntermediate->GetDesc(); auto DestinationDesc = pDestinationResource->GetDesc(); if (IntermediateDesc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || IntermediateDesc.Width < RequiredSize + pLayouts[0].Offset || RequiredSize > SIZE_T(-1) || (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && (FirstSubresource != 0 || NumSubresources != 1))) { return 0; } BYTE* pData; HRESULT hr = pIntermediate->Map(0, nullptr, reinterpret_cast(&pData)); if (FAILED(hr)) { return 0; } for (UINT i = 0; i < NumSubresources; ++i) { if (pRowSizesInBytes[i] > SIZE_T(-1)) return 0; D3D12_MEMCPY_DEST DestData = { pData + pLayouts[i].Offset, pLayouts[i].Footprint.RowPitch, SIZE_T(pLayouts[i].Footprint.RowPitch) * SIZE_T(pNumRows[i]) }; MemcpySubresource(&DestData, &pSrcData[i], static_cast(pRowSizesInBytes[i]), pNumRows[i], pLayouts[i].Footprint.Depth); } pIntermediate->Unmap(0, nullptr); if (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) { pCmdList->CopyBufferRegion( pDestinationResource, 0, pIntermediate, pLayouts[0].Offset, pLayouts[0].Footprint.Width); } else { for (UINT i = 0; i < NumSubresources; ++i) { CD3DX12_TEXTURE_COPY_LOCATION Dst(pDestinationResource, i + FirstSubresource); CD3DX12_TEXTURE_COPY_LOCATION Src(pIntermediate, pLayouts[i]); pCmdList->CopyTextureRegion(&Dst, 0, 0, 0, &Src, nullptr); } } return RequiredSize; } //------------------------------------------------------------------------------------------------ // Heap-allocating UpdateSubresources implementation inline UINT64 UpdateSubresources( _In_ ID3D12GraphicsCommandList* pCmdList, _In_ ID3D12Resource* pDestinationResource, _In_ ID3D12Resource* pIntermediate, UINT64 IntermediateOffset, _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource, _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources, _In_reads_(NumSubresources) D3D12_SUBRESOURCE_DATA* pSrcData) { UINT64 RequiredSize = 0; UINT64 MemToAlloc = static_cast(sizeof(D3D12_PLACED_SUBRESOURCE_FOOTPRINT) + sizeof(UINT) + sizeof(UINT64)) * NumSubresources; if (MemToAlloc > SIZE_MAX) { return 0; } void* pMem = HeapAlloc(GetProcessHeap(), 0, static_cast(MemToAlloc)); if (pMem == nullptr) { return 0; } auto pLayouts = reinterpret_cast(pMem); UINT64* pRowSizesInBytes = reinterpret_cast(pLayouts + NumSubresources); UINT* pNumRows = reinterpret_cast(pRowSizesInBytes + NumSubresources); auto Desc = pDestinationResource->GetDesc(); ID3D12Device* pDevice = nullptr; pDestinationResource->GetDevice(__uuidof(*pDevice), reinterpret_cast(&pDevice)); pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, pLayouts, pNumRows, pRowSizesInBytes, &RequiredSize); pDevice->Release(); UINT64 Result = UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, pLayouts, pNumRows, pRowSizesInBytes, pSrcData); HeapFree(GetProcessHeap(), 0, pMem); return Result; } //------------------------------------------------------------------------------------------------ // Stack-allocating UpdateSubresources implementation template inline UINT64 UpdateSubresources( _In_ ID3D12GraphicsCommandList* pCmdList, _In_ ID3D12Resource* pDestinationResource, _In_ ID3D12Resource* pIntermediate, UINT64 IntermediateOffset, _In_range_(0, MaxSubresources) UINT FirstSubresource, _In_range_(1, MaxSubresources - FirstSubresource) UINT NumSubresources, _In_reads_(NumSubresources) D3D12_SUBRESOURCE_DATA* pSrcData) { UINT64 RequiredSize = 0; D3D12_PLACED_SUBRESOURCE_FOOTPRINT Layouts[MaxSubresources]; UINT NumRows[MaxSubresources]; UINT64 RowSizesInBytes[MaxSubresources]; auto Desc = pDestinationResource->GetDesc(); ID3D12Device* pDevice = nullptr; pDestinationResource->GetDevice(__uuidof(*pDevice), reinterpret_cast(&pDevice)); pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, Layouts, NumRows, RowSizesInBytes, &RequiredSize); pDevice->Release(); return UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, Layouts, NumRows, RowSizesInBytes, pSrcData); } //------------------------------------------------------------------------------------------------ inline bool D3D12IsLayoutOpaque( D3D12_TEXTURE_LAYOUT Layout ) { return Layout == D3D12_TEXTURE_LAYOUT_UNKNOWN || Layout == D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE; } //------------------------------------------------------------------------------------------------ template inline ID3D12CommandList * const * CommandListCast(t_CommandListType * const * pp) { // This cast is useful for passing strongly typed command list pointers into // ExecuteCommandLists. // This cast is valid as long as the const-ness is respected. D3D12 APIs do // respect the const-ness of their arguments. return reinterpret_cast(pp); } //------------------------------------------------------------------------------------------------ // D3D12 exports a new method for serializing root signatures in the Windows 10 Anniversary Update. // To help enable root signature 1.1 features when they are available and not require maintaining // two code paths for building root signatures, this helper method reconstructs a 1.0 signature when // 1.1 is not supported. inline HRESULT D3DX12SerializeVersionedRootSignature( _In_ const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* pRootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION MaxVersion, _Outptr_ ID3DBlob** ppBlob, _Always_(_Outptr_opt_result_maybenull_) ID3DBlob** ppErrorBlob) { if (ppErrorBlob != nullptr) { *ppErrorBlob = nullptr; } switch (MaxVersion) { case D3D_ROOT_SIGNATURE_VERSION_1_0: switch (pRootSignatureDesc->Version) { case D3D_ROOT_SIGNATURE_VERSION_1_0: return D3D12SerializeRootSignature(&pRootSignatureDesc->Desc_1_0, D3D_ROOT_SIGNATURE_VERSION_1, ppBlob, ppErrorBlob); case D3D_ROOT_SIGNATURE_VERSION_1_1: { HRESULT hr = S_OK; const D3D12_ROOT_SIGNATURE_DESC1& desc_1_1 = pRootSignatureDesc->Desc_1_1; const SIZE_T ParametersSize = sizeof(D3D12_ROOT_PARAMETER) * desc_1_1.NumParameters; void* pParameters = (ParametersSize > 0) ? HeapAlloc(GetProcessHeap(), 0, ParametersSize) : nullptr; if (ParametersSize > 0 && pParameters == nullptr) { hr = E_OUTOFMEMORY; } auto pParameters_1_0 = reinterpret_cast(pParameters); if (SUCCEEDED(hr)) { for (UINT n = 0; n < desc_1_1.NumParameters; n++) { __analysis_assume(ParametersSize == sizeof(D3D12_ROOT_PARAMETER) * desc_1_1.NumParameters); pParameters_1_0[n].ParameterType = desc_1_1.pParameters[n].ParameterType; pParameters_1_0[n].ShaderVisibility = desc_1_1.pParameters[n].ShaderVisibility; switch (desc_1_1.pParameters[n].ParameterType) { case D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS: pParameters_1_0[n].Constants.Num32BitValues = desc_1_1.pParameters[n].Constants.Num32BitValues; pParameters_1_0[n].Constants.RegisterSpace = desc_1_1.pParameters[n].Constants.RegisterSpace; pParameters_1_0[n].Constants.ShaderRegister = desc_1_1.pParameters[n].Constants.ShaderRegister; break; case D3D12_ROOT_PARAMETER_TYPE_CBV: case D3D12_ROOT_PARAMETER_TYPE_SRV: case D3D12_ROOT_PARAMETER_TYPE_UAV: pParameters_1_0[n].Descriptor.RegisterSpace = desc_1_1.pParameters[n].Descriptor.RegisterSpace; pParameters_1_0[n].Descriptor.ShaderRegister = desc_1_1.pParameters[n].Descriptor.ShaderRegister; break; case D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE: const D3D12_ROOT_DESCRIPTOR_TABLE1& table_1_1 = desc_1_1.pParameters[n].DescriptorTable; const SIZE_T DescriptorRangesSize = sizeof(D3D12_DESCRIPTOR_RANGE) * table_1_1.NumDescriptorRanges; void* pDescriptorRanges = (DescriptorRangesSize > 0 && SUCCEEDED(hr)) ? HeapAlloc(GetProcessHeap(), 0, DescriptorRangesSize) : nullptr; if (DescriptorRangesSize > 0 && pDescriptorRanges == nullptr) { hr = E_OUTOFMEMORY; } auto pDescriptorRanges_1_0 = reinterpret_cast(pDescriptorRanges); if (SUCCEEDED(hr)) { for (UINT x = 0; x < table_1_1.NumDescriptorRanges; x++) { __analysis_assume(DescriptorRangesSize == sizeof(D3D12_DESCRIPTOR_RANGE) * table_1_1.NumDescriptorRanges); pDescriptorRanges_1_0[x].BaseShaderRegister = table_1_1.pDescriptorRanges[x].BaseShaderRegister; pDescriptorRanges_1_0[x].NumDescriptors = table_1_1.pDescriptorRanges[x].NumDescriptors; pDescriptorRanges_1_0[x].OffsetInDescriptorsFromTableStart = table_1_1.pDescriptorRanges[x].OffsetInDescriptorsFromTableStart; pDescriptorRanges_1_0[x].RangeType = table_1_1.pDescriptorRanges[x].RangeType; pDescriptorRanges_1_0[x].RegisterSpace = table_1_1.pDescriptorRanges[x].RegisterSpace; } } D3D12_ROOT_DESCRIPTOR_TABLE& table_1_0 = pParameters_1_0[n].DescriptorTable; table_1_0.NumDescriptorRanges = table_1_1.NumDescriptorRanges; table_1_0.pDescriptorRanges = pDescriptorRanges_1_0; } } } if (SUCCEEDED(hr)) { 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); hr = D3D12SerializeRootSignature(&desc_1_0, D3D_ROOT_SIGNATURE_VERSION_1, ppBlob, ppErrorBlob); } if (pParameters) { for (UINT n = 0; n < desc_1_1.NumParameters; n++) { if (desc_1_1.pParameters[n].ParameterType == D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE) { HeapFree(GetProcessHeap(), 0, reinterpret_cast(const_cast(pParameters_1_0[n].DescriptorTable.pDescriptorRanges))); } } HeapFree(GetProcessHeap(), 0, pParameters); } return hr; } } break; case D3D_ROOT_SIGNATURE_VERSION_1_1: return D3D12SerializeVersionedRootSignature(pRootSignatureDesc, ppBlob, ppErrorBlob); } return E_INVALIDARG; } //------------------------------------------------------------------------------------------------ struct CD3DX12_RT_FORMAT_ARRAY : public D3D12_RT_FORMAT_ARRAY { CD3DX12_RT_FORMAT_ARRAY() = default; explicit CD3DX12_RT_FORMAT_ARRAY(const D3D12_RT_FORMAT_ARRAY& o) : D3D12_RT_FORMAT_ARRAY(o) {} explicit CD3DX12_RT_FORMAT_ARRAY(_In_reads_(NumFormats) const DXGI_FORMAT* pFormats, UINT NumFormats) { NumRenderTargets = NumFormats; memcpy(RTFormats, pFormats, sizeof(RTFormats)); // assumes ARRAY_SIZE(pFormats) == ARRAY_SIZE(RTFormats) } }; //------------------------------------------------------------------------------------------------ // Pipeline State Stream Helpers //------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------ // Stream Subobjects, i.e. elements of a stream struct DefaultSampleMask { operator UINT() { return UINT_MAX; } }; struct DefaultSampleDesc { operator DXGI_SAMPLE_DESC() { return DXGI_SAMPLE_DESC{1, 0}; } }; #pragma warning(push) #pragma warning(disable : 4324) template class alignas(void*) CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT { private: D3D12_PIPELINE_STATE_SUBOBJECT_TYPE _Type; InnerStructType _Inner; public: CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT() noexcept : _Type(Type), _Inner(DefaultArg()) {} CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT(InnerStructType const& i) : _Type(Type), _Inner(i) {} CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT& operator=(InnerStructType const& i) { _Inner = i; return *this; } operator InnerStructType() const { return _Inner; } operator InnerStructType&() { return _Inner; } }; #pragma warning(pop) typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_PIPELINE_STATE_FLAGS, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_FLAGS> CD3DX12_PIPELINE_STATE_STREAM_FLAGS; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< UINT, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_NODE_MASK> CD3DX12_PIPELINE_STATE_STREAM_NODE_MASK; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< ID3D12RootSignature*, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE> CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_INPUT_LAYOUT_DESC, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT> CD3DX12_PIPELINE_STATE_STREAM_INPUT_LAYOUT; typedef 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; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_PRIMITIVE_TOPOLOGY_TYPE, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY> CD3DX12_PIPELINE_STATE_STREAM_PRIMITIVE_TOPOLOGY; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS> CD3DX12_PIPELINE_STATE_STREAM_VS; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_GS> CD3DX12_PIPELINE_STATE_STREAM_GS; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_STREAM_OUTPUT_DESC, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_STREAM_OUTPUT> CD3DX12_PIPELINE_STATE_STREAM_STREAM_OUTPUT; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_HS> CD3DX12_PIPELINE_STATE_STREAM_HS; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DS> CD3DX12_PIPELINE_STATE_STREAM_DS; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS> CD3DX12_PIPELINE_STATE_STREAM_PS; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_SHADER_BYTECODE, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CS> CD3DX12_PIPELINE_STATE_STREAM_CS; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_BLEND_DESC, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND, CD3DX12_DEFAULT> CD3DX12_PIPELINE_STATE_STREAM_BLEND_DESC; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_DEPTH_STENCIL_DESC, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL, CD3DX12_DEFAULT> CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_DEPTH_STENCIL_DESC1, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1, CD3DX12_DEFAULT> CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL1; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< DXGI_FORMAT, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT> CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL_FORMAT; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_RASTERIZER_DESC, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER, CD3DX12_DEFAULT> CD3DX12_PIPELINE_STATE_STREAM_RASTERIZER; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_RT_FORMAT_ARRAY, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS> CD3DX12_PIPELINE_STATE_STREAM_RENDER_TARGET_FORMATS; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< DXGI_SAMPLE_DESC, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC, DefaultSampleDesc> CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_DESC; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< UINT, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK, DefaultSampleMask> CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_MASK; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< D3D12_CACHED_PIPELINE_STATE, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CACHED_PSO> CD3DX12_PIPELINE_STATE_STREAM_CACHED_PSO; typedef CD3DX12_PIPELINE_STATE_STREAM_SUBOBJECT< CD3DX12_VIEW_INSTANCING_DESC, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VIEW_INSTANCING, CD3DX12_DEFAULT> CD3DX12_PIPELINE_STATE_STREAM_VIEW_INSTANCING; //------------------------------------------------------------------------------------------------ // Stream Parser Helpers struct ID3DX12PipelineParserCallbacks { // Subobject Callbacks virtual void FlagsCb(D3D12_PIPELINE_STATE_FLAGS) {} virtual void NodeMaskCb(UINT) {} virtual void RootSignatureCb(ID3D12RootSignature*) {} virtual void InputLayoutCb(const D3D12_INPUT_LAYOUT_DESC&) {} virtual void IBStripCutValueCb(D3D12_INDEX_BUFFER_STRIP_CUT_VALUE) {} virtual void PrimitiveTopologyTypeCb(D3D12_PRIMITIVE_TOPOLOGY_TYPE) {} virtual void VSCb(const D3D12_SHADER_BYTECODE&) {} virtual void GSCb(const D3D12_SHADER_BYTECODE&) {} virtual void StreamOutputCb(const D3D12_STREAM_OUTPUT_DESC&) {} virtual void HSCb(const D3D12_SHADER_BYTECODE&) {} virtual void DSCb(const D3D12_SHADER_BYTECODE&) {} virtual void PSCb(const D3D12_SHADER_BYTECODE&) {} virtual void CSCb(const D3D12_SHADER_BYTECODE&) {} virtual void BlendStateCb(const D3D12_BLEND_DESC&) {} virtual void DepthStencilStateCb(const D3D12_DEPTH_STENCIL_DESC&) {} virtual void DepthStencilState1Cb(const D3D12_DEPTH_STENCIL_DESC1&) {} virtual void DSVFormatCb(DXGI_FORMAT) {} virtual void RasterizerStateCb(const D3D12_RASTERIZER_DESC&) {} virtual void RTVFormatsCb(const D3D12_RT_FORMAT_ARRAY&) {} virtual void SampleDescCb(const DXGI_SAMPLE_DESC&) {} virtual void SampleMaskCb(UINT) {} virtual void ViewInstancingCb(const D3D12_VIEW_INSTANCING_DESC&) {} virtual void CachedPSOCb(const D3D12_CACHED_PIPELINE_STATE&) {} // Error Callbacks virtual void ErrorBadInputParameter(UINT /*ParameterIndex*/) {} virtual void ErrorDuplicateSubobject(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE /*DuplicateType*/) {} virtual void ErrorUnknownSubobject(UINT /*UnknownTypeValue*/) {} virtual ~ID3DX12PipelineParserCallbacks() = default; }; // CD3DX12_PIPELINE_STATE_STREAM1 Works on RS3+ (where there is a new view instancing subobject). // Use CD3DX12_PIPELINE_STATE_STREAM for RS2+ support. struct CD3DX12_PIPELINE_STATE_STREAM1 { CD3DX12_PIPELINE_STATE_STREAM1() = default; CD3DX12_PIPELINE_STATE_STREAM1(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& Desc) : Flags(Desc.Flags) , NodeMask(Desc.NodeMask) , pRootSignature(Desc.pRootSignature) , InputLayout(Desc.InputLayout) , IBStripCutValue(Desc.IBStripCutValue) , PrimitiveTopologyType(Desc.PrimitiveTopologyType) , VS(Desc.VS) , GS(Desc.GS) , StreamOutput(Desc.StreamOutput) , HS(Desc.HS) , DS(Desc.DS) , PS(Desc.PS) , BlendState(CD3DX12_BLEND_DESC(Desc.BlendState)) , DepthStencilState(CD3DX12_DEPTH_STENCIL_DESC1(Desc.DepthStencilState)) , DSVFormat(Desc.DSVFormat) , RasterizerState(CD3DX12_RASTERIZER_DESC(Desc.RasterizerState)) , RTVFormats(CD3DX12_RT_FORMAT_ARRAY(Desc.RTVFormats, Desc.NumRenderTargets)) , SampleDesc(Desc.SampleDesc) , SampleMask(Desc.SampleMask) , CachedPSO(Desc.CachedPSO) , ViewInstancingDesc(CD3DX12_VIEW_INSTANCING_DESC(CD3DX12_DEFAULT())) {} CD3DX12_PIPELINE_STATE_STREAM1(const D3D12_COMPUTE_PIPELINE_STATE_DESC& Desc) : Flags(Desc.Flags) , NodeMask(Desc.NodeMask) , pRootSignature(Desc.pRootSignature) , CS(CD3DX12_SHADER_BYTECODE(Desc.CS)) , CachedPSO(Desc.CachedPSO) { static_cast(DepthStencilState).DepthEnable = false; } CD3DX12_PIPELINE_STATE_STREAM_FLAGS Flags; CD3DX12_PIPELINE_STATE_STREAM_NODE_MASK NodeMask; CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE pRootSignature; CD3DX12_PIPELINE_STATE_STREAM_INPUT_LAYOUT InputLayout; CD3DX12_PIPELINE_STATE_STREAM_IB_STRIP_CUT_VALUE IBStripCutValue; CD3DX12_PIPELINE_STATE_STREAM_PRIMITIVE_TOPOLOGY PrimitiveTopologyType; CD3DX12_PIPELINE_STATE_STREAM_VS VS; CD3DX12_PIPELINE_STATE_STREAM_GS GS; CD3DX12_PIPELINE_STATE_STREAM_STREAM_OUTPUT StreamOutput; CD3DX12_PIPELINE_STATE_STREAM_HS HS; CD3DX12_PIPELINE_STATE_STREAM_DS DS; CD3DX12_PIPELINE_STATE_STREAM_PS PS; CD3DX12_PIPELINE_STATE_STREAM_CS CS; CD3DX12_PIPELINE_STATE_STREAM_BLEND_DESC BlendState; CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL1 DepthStencilState; CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL_FORMAT DSVFormat; CD3DX12_PIPELINE_STATE_STREAM_RASTERIZER RasterizerState; CD3DX12_PIPELINE_STATE_STREAM_RENDER_TARGET_FORMATS RTVFormats; CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_DESC SampleDesc; CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_MASK SampleMask; CD3DX12_PIPELINE_STATE_STREAM_CACHED_PSO CachedPSO; CD3DX12_PIPELINE_STATE_STREAM_VIEW_INSTANCING ViewInstancingDesc; D3D12_GRAPHICS_PIPELINE_STATE_DESC GraphicsDescV0() const { D3D12_GRAPHICS_PIPELINE_STATE_DESC D; D.Flags = this->Flags; D.NodeMask = this->NodeMask; D.pRootSignature = this->pRootSignature; D.InputLayout = this->InputLayout; D.IBStripCutValue = this->IBStripCutValue; D.PrimitiveTopologyType = this->PrimitiveTopologyType; D.VS = this->VS; D.GS = this->GS; D.StreamOutput = this->StreamOutput; D.HS = this->HS; D.DS = this->DS; D.PS = this->PS; D.BlendState = this->BlendState; D.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC1(D3D12_DEPTH_STENCIL_DESC1(this->DepthStencilState)); D.DSVFormat = this->DSVFormat; D.RasterizerState = this->RasterizerState; D.NumRenderTargets = D3D12_RT_FORMAT_ARRAY(this->RTVFormats).NumRenderTargets; memcpy(D.RTVFormats, D3D12_RT_FORMAT_ARRAY(this->RTVFormats).RTFormats, sizeof(D.RTVFormats)); D.SampleDesc = this->SampleDesc; D.SampleMask = this->SampleMask; D.CachedPSO = this->CachedPSO; return D; } D3D12_COMPUTE_PIPELINE_STATE_DESC ComputeDescV0() const { D3D12_COMPUTE_PIPELINE_STATE_DESC D; D.Flags = this->Flags; D.NodeMask = this->NodeMask; D.pRootSignature = this->pRootSignature; D.CS = this->CS; D.CachedPSO = this->CachedPSO; return D; } }; // CD3DX12_PIPELINE_STATE_STREAM works on RS2+ but does not support new subobject(s) added in RS3+. // See CD3DX12_PIPELINE_STATE_STREAM1 for instance. struct CD3DX12_PIPELINE_STATE_STREAM { CD3DX12_PIPELINE_STATE_STREAM() = default; CD3DX12_PIPELINE_STATE_STREAM(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& Desc) : Flags(Desc.Flags) , NodeMask(Desc.NodeMask) , pRootSignature(Desc.pRootSignature) , InputLayout(Desc.InputLayout) , IBStripCutValue(Desc.IBStripCutValue) , PrimitiveTopologyType(Desc.PrimitiveTopologyType) , VS(Desc.VS) , GS(Desc.GS) , StreamOutput(Desc.StreamOutput) , HS(Desc.HS) , DS(Desc.DS) , PS(Desc.PS) , BlendState(CD3DX12_BLEND_DESC(Desc.BlendState)) , DepthStencilState(CD3DX12_DEPTH_STENCIL_DESC1(Desc.DepthStencilState)) , DSVFormat(Desc.DSVFormat) , RasterizerState(CD3DX12_RASTERIZER_DESC(Desc.RasterizerState)) , RTVFormats(CD3DX12_RT_FORMAT_ARRAY(Desc.RTVFormats, Desc.NumRenderTargets)) , SampleDesc(Desc.SampleDesc) , SampleMask(Desc.SampleMask) , CachedPSO(Desc.CachedPSO) {} CD3DX12_PIPELINE_STATE_STREAM(const D3D12_COMPUTE_PIPELINE_STATE_DESC& Desc) : Flags(Desc.Flags) , NodeMask(Desc.NodeMask) , pRootSignature(Desc.pRootSignature) , CS(CD3DX12_SHADER_BYTECODE(Desc.CS)) , CachedPSO(Desc.CachedPSO) {} CD3DX12_PIPELINE_STATE_STREAM_FLAGS Flags; CD3DX12_PIPELINE_STATE_STREAM_NODE_MASK NodeMask; CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE pRootSignature; CD3DX12_PIPELINE_STATE_STREAM_INPUT_LAYOUT InputLayout; CD3DX12_PIPELINE_STATE_STREAM_IB_STRIP_CUT_VALUE IBStripCutValue; CD3DX12_PIPELINE_STATE_STREAM_PRIMITIVE_TOPOLOGY PrimitiveTopologyType; CD3DX12_PIPELINE_STATE_STREAM_VS VS; CD3DX12_PIPELINE_STATE_STREAM_GS GS; CD3DX12_PIPELINE_STATE_STREAM_STREAM_OUTPUT StreamOutput; CD3DX12_PIPELINE_STATE_STREAM_HS HS; CD3DX12_PIPELINE_STATE_STREAM_DS DS; CD3DX12_PIPELINE_STATE_STREAM_PS PS; CD3DX12_PIPELINE_STATE_STREAM_CS CS; CD3DX12_PIPELINE_STATE_STREAM_BLEND_DESC BlendState; CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL1 DepthStencilState; CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL_FORMAT DSVFormat; CD3DX12_PIPELINE_STATE_STREAM_RASTERIZER RasterizerState; CD3DX12_PIPELINE_STATE_STREAM_RENDER_TARGET_FORMATS RTVFormats; CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_DESC SampleDesc; CD3DX12_PIPELINE_STATE_STREAM_SAMPLE_MASK SampleMask; CD3DX12_PIPELINE_STATE_STREAM_CACHED_PSO CachedPSO; D3D12_GRAPHICS_PIPELINE_STATE_DESC GraphicsDescV0() const { D3D12_GRAPHICS_PIPELINE_STATE_DESC D; D.Flags = this->Flags; D.NodeMask = this->NodeMask; D.pRootSignature = this->pRootSignature; D.InputLayout = this->InputLayout; D.IBStripCutValue = this->IBStripCutValue; D.PrimitiveTopologyType = this->PrimitiveTopologyType; D.VS = this->VS; D.GS = this->GS; D.StreamOutput = this->StreamOutput; D.HS = this->HS; D.DS = this->DS; D.PS = this->PS; D.BlendState = this->BlendState; D.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC1(D3D12_DEPTH_STENCIL_DESC1(this->DepthStencilState)); D.DSVFormat = this->DSVFormat; D.RasterizerState = this->RasterizerState; D.NumRenderTargets = D3D12_RT_FORMAT_ARRAY(this->RTVFormats).NumRenderTargets; memcpy(D.RTVFormats, D3D12_RT_FORMAT_ARRAY(this->RTVFormats).RTFormats, sizeof(D.RTVFormats)); D.SampleDesc = this->SampleDesc; D.SampleMask = this->SampleMask; D.CachedPSO = this->CachedPSO; return D; } D3D12_COMPUTE_PIPELINE_STATE_DESC ComputeDescV0() const { D3D12_COMPUTE_PIPELINE_STATE_DESC D; D.Flags = this->Flags; D.NodeMask = this->NodeMask; D.pRootSignature = this->pRootSignature; D.CS = this->CS; D.CachedPSO = this->CachedPSO; return D; } }; struct CD3DX12_PIPELINE_STATE_STREAM_PARSE_HELPER : public ID3DX12PipelineParserCallbacks { CD3DX12_PIPELINE_STATE_STREAM1 PipelineStream; CD3DX12_PIPELINE_STATE_STREAM_PARSE_HELPER() noexcept : SeenDSS(false) { // Adjust defaults to account for absent members. PipelineStream.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; // Depth disabled if no DSV format specified. static_cast(PipelineStream.DepthStencilState).DepthEnable = false; } // ID3DX12PipelineParserCallbacks void FlagsCb(D3D12_PIPELINE_STATE_FLAGS Flags) override {PipelineStream.Flags = Flags;} void NodeMaskCb(UINT NodeMask) override {PipelineStream.NodeMask = NodeMask;} void RootSignatureCb(ID3D12RootSignature* pRootSignature) override {PipelineStream.pRootSignature = pRootSignature;} void InputLayoutCb(const D3D12_INPUT_LAYOUT_DESC& InputLayout) override {PipelineStream.InputLayout = InputLayout;} void IBStripCutValueCb(D3D12_INDEX_BUFFER_STRIP_CUT_VALUE IBStripCutValue) override {PipelineStream.IBStripCutValue = IBStripCutValue;} void PrimitiveTopologyTypeCb(D3D12_PRIMITIVE_TOPOLOGY_TYPE PrimitiveTopologyType) override {PipelineStream.PrimitiveTopologyType = PrimitiveTopologyType;} void VSCb(const D3D12_SHADER_BYTECODE& VS) override {PipelineStream.VS = VS;} void GSCb(const D3D12_SHADER_BYTECODE& GS) override {PipelineStream.GS = GS;} void StreamOutputCb(const D3D12_STREAM_OUTPUT_DESC& StreamOutput) override {PipelineStream.StreamOutput = StreamOutput;} void HSCb(const D3D12_SHADER_BYTECODE& HS) override {PipelineStream.HS = HS;} void DSCb(const D3D12_SHADER_BYTECODE& DS) override {PipelineStream.DS = DS;} void PSCb(const D3D12_SHADER_BYTECODE& PS) override {PipelineStream.PS = PS;} void CSCb(const D3D12_SHADER_BYTECODE& CS) override {PipelineStream.CS = CS;} void BlendStateCb(const D3D12_BLEND_DESC& BlendState) override {PipelineStream.BlendState = CD3DX12_BLEND_DESC(BlendState);} void DepthStencilStateCb(const D3D12_DEPTH_STENCIL_DESC& DepthStencilState) override { PipelineStream.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC1(DepthStencilState); SeenDSS = true; } void DepthStencilState1Cb(const D3D12_DEPTH_STENCIL_DESC1& DepthStencilState) override { PipelineStream.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC1(DepthStencilState); SeenDSS = true; } void DSVFormatCb(DXGI_FORMAT DSVFormat) override { PipelineStream.DSVFormat = DSVFormat; if (!SeenDSS && DSVFormat != DXGI_FORMAT_UNKNOWN) { // Re-enable depth for the default state. static_cast(PipelineStream.DepthStencilState).DepthEnable = true; } } void RasterizerStateCb(const D3D12_RASTERIZER_DESC& RasterizerState) override {PipelineStream.RasterizerState = CD3DX12_RASTERIZER_DESC(RasterizerState);} void RTVFormatsCb(const D3D12_RT_FORMAT_ARRAY& RTVFormats) override {PipelineStream.RTVFormats = RTVFormats;} void SampleDescCb(const DXGI_SAMPLE_DESC& SampleDesc) override {PipelineStream.SampleDesc = SampleDesc;} void SampleMaskCb(UINT SampleMask) override {PipelineStream.SampleMask = SampleMask;} void ViewInstancingCb(const D3D12_VIEW_INSTANCING_DESC& ViewInstancingDesc) override {PipelineStream.ViewInstancingDesc = CD3DX12_VIEW_INSTANCING_DESC(ViewInstancingDesc);} void CachedPSOCb(const D3D12_CACHED_PIPELINE_STATE& CachedPSO) override {PipelineStream.CachedPSO = CachedPSO;} private: bool SeenDSS; }; inline D3D12_PIPELINE_STATE_SUBOBJECT_TYPE D3DX12GetBaseSubobjectType(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE SubobjectType) { switch (SubobjectType) { case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1: return D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL; default: return SubobjectType; } } inline HRESULT D3DX12ParsePipelineStream(const D3D12_PIPELINE_STATE_STREAM_DESC& Desc, ID3DX12PipelineParserCallbacks* pCallbacks) { if (pCallbacks == nullptr) { return E_INVALIDARG; } if (Desc.SizeInBytes == 0 || Desc.pPipelineStateSubobjectStream == nullptr) { pCallbacks->ErrorBadInputParameter(1); // first parameter issue return E_INVALIDARG; } bool SubobjectSeen[D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MAX_VALID] = {}; for (SIZE_T CurOffset = 0, SizeOfSubobject = 0; CurOffset < Desc.SizeInBytes; CurOffset += SizeOfSubobject) { BYTE* pStream = static_cast(Desc.pPipelineStateSubobjectStream)+CurOffset; auto SubobjectType = *reinterpret_cast(pStream); if (SubobjectType < 0 || SubobjectType >= D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MAX_VALID) { pCallbacks->ErrorUnknownSubobject(SubobjectType); return E_INVALIDARG; } if (SubobjectSeen[D3DX12GetBaseSubobjectType(SubobjectType)]) { pCallbacks->ErrorDuplicateSubobject(SubobjectType); return E_INVALIDARG; // disallow subobject duplicates in a stream } SubobjectSeen[SubobjectType] = true; switch (SubobjectType) { case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE: pCallbacks->RootSignatureCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::pRootSignature); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS: pCallbacks->VSCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::VS); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS: pCallbacks->PSCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::PS); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DS: pCallbacks->DSCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::DS); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_HS: pCallbacks->HSCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::HS); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_GS: pCallbacks->GSCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::GS); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CS: pCallbacks->CSCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::CS); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_STREAM_OUTPUT: pCallbacks->StreamOutputCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::StreamOutput); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND: pCallbacks->BlendStateCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::BlendState); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK: pCallbacks->SampleMaskCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::SampleMask); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER: pCallbacks->RasterizerStateCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::RasterizerState); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL: pCallbacks->DepthStencilStateCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM_DEPTH_STENCIL); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1: pCallbacks->DepthStencilState1Cb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::DepthStencilState); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT: pCallbacks->InputLayoutCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::InputLayout); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_IB_STRIP_CUT_VALUE: pCallbacks->IBStripCutValueCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::IBStripCutValue); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY: pCallbacks->PrimitiveTopologyTypeCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::PrimitiveTopologyType); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS: pCallbacks->RTVFormatsCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::RTVFormats); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT: pCallbacks->DSVFormatCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::DSVFormat); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC: pCallbacks->SampleDescCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::SampleDesc); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_NODE_MASK: pCallbacks->NodeMaskCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::NodeMask); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CACHED_PSO: pCallbacks->CachedPSOCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::CachedPSO); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_FLAGS: pCallbacks->FlagsCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM::Flags); break; case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VIEW_INSTANCING: pCallbacks->ViewInstancingCb(*reinterpret_cast(pStream)); SizeOfSubobject = sizeof(CD3DX12_PIPELINE_STATE_STREAM1::ViewInstancingDesc); break; default: pCallbacks->ErrorUnknownSubobject(SubobjectType); return E_INVALIDARG; break; } } return S_OK; } //------------------------------------------------------------------------------------------------ inline bool operator==( const D3D12_CLEAR_VALUE &a, const D3D12_CLEAR_VALUE &b) { if (a.Format != b.Format) return false; if (a.Format == DXGI_FORMAT_D24_UNORM_S8_UINT || a.Format == DXGI_FORMAT_D16_UNORM || a.Format == DXGI_FORMAT_D32_FLOAT || a.Format == DXGI_FORMAT_D32_FLOAT_S8X24_UINT) { return (a.DepthStencil.Depth == b.DepthStencil.Depth) && (a.DepthStencil.Stencil == b.DepthStencil.Stencil); } else { return (a.Color[0] == b.Color[0]) && (a.Color[1] == b.Color[1]) && (a.Color[2] == b.Color[2]) && (a.Color[3] == b.Color[3]); } } inline bool operator==( const D3D12_RENDER_PASS_BEGINNING_ACCESS_CLEAR_PARAMETERS &a, const D3D12_RENDER_PASS_BEGINNING_ACCESS_CLEAR_PARAMETERS &b) { return a.ClearValue == b.ClearValue; } inline bool operator==( const D3D12_RENDER_PASS_ENDING_ACCESS_RESOLVE_PARAMETERS &a, const D3D12_RENDER_PASS_ENDING_ACCESS_RESOLVE_PARAMETERS &b) { if (a.pSrcResource != b.pSrcResource) return false; if (a.pDstResource != b.pDstResource) return false; if (a.SubresourceCount != b.SubresourceCount) return false; if (a.Format != b.Format) return false; if (a.ResolveMode != b.ResolveMode) return false; if (a.PreserveResolveSource != b.PreserveResolveSource) return false; return true; } inline bool operator==( const D3D12_RENDER_PASS_BEGINNING_ACCESS &a, const D3D12_RENDER_PASS_BEGINNING_ACCESS &b) { if (a.Type != b.Type) return false; if (a.Type == D3D12_RENDER_PASS_BEGINNING_ACCESS_TYPE_CLEAR && !(a.Clear == b.Clear)) return false; return true; } inline bool operator==( const D3D12_RENDER_PASS_ENDING_ACCESS &a, const D3D12_RENDER_PASS_ENDING_ACCESS &b) { if (a.Type != b.Type) return false; if (a.Type == D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_RESOLVE && !(a.Resolve == b.Resolve)) return false; return true; } inline bool operator==( const D3D12_RENDER_PASS_RENDER_TARGET_DESC &a, const D3D12_RENDER_PASS_RENDER_TARGET_DESC &b) { if (a.cpuDescriptor.ptr != b.cpuDescriptor.ptr) return false; if (!(a.BeginningAccess == b.BeginningAccess)) return false; if (!(a.EndingAccess == b.EndingAccess)) return false; return true; } inline bool operator==( const D3D12_RENDER_PASS_DEPTH_STENCIL_DESC &a, const D3D12_RENDER_PASS_DEPTH_STENCIL_DESC &b) { if (a.cpuDescriptor.ptr != b.cpuDescriptor.ptr) return false; if (!(a.DepthBeginningAccess == b.DepthBeginningAccess)) return false; if (!(a.StencilBeginningAccess == b.DepthBeginningAccess)) return false; if (!(a.DepthEndingAccess == b.DepthEndingAccess)) return false; if (!(a.StencilEndingAccess == b.StencilEndingAccess)) return false; return true; } #ifndef D3DX12_NO_STATE_OBJECT_HELPERS //================================================================================================ // D3DX12 State Object Creation Helpers // // Helper classes for creating new style state objects out of an arbitrary set of subobjects. // Uses STL // // Start by instantiating CD3DX12_STATE_OBJECT_DESC (see it's public methods). // One of its methods is CreateSubobject(), which has a comment showing a couple of options for // defining subobjects using the helper classes for each subobject (CD3DX12_DXIL_LIBRARY_SUBOBJECT // etc.). The subobject helpers each have methods specific to the subobject for configuring it's // contents. // //================================================================================================ #include #include #include #include #include //------------------------------------------------------------------------------------------------ class CD3DX12_STATE_OBJECT_DESC { public: CD3DX12_STATE_OBJECT_DESC() { Init(D3D12_STATE_OBJECT_TYPE_COLLECTION); } ~CD3DX12_STATE_OBJECT_DESC() { } CD3DX12_STATE_OBJECT_DESC(D3D12_STATE_OBJECT_TYPE Type) { Init(Type); } void SetStateObjectType(D3D12_STATE_OBJECT_TYPE Type) { m_Desc.Type = Type; } operator const D3D12_STATE_OBJECT_DESC&() { // Do final preparation work m_RepointedAssociations.clear(); m_SubobjectArray.clear(); m_SubobjectArray.reserve(m_Desc.NumSubobjects); // Flatten subobjects into an array (each flattened subobject still has a // member that's a pointer to it's desc that's not flattened) for (auto Iter = m_SubobjectList.begin(); Iter != m_SubobjectList.end(); Iter++) { m_SubobjectArray.push_back(*Iter); // Store new location in array so we can redirect pointers contained in subobjects Iter->pSubobjectArrayLocation = &m_SubobjectArray.back(); } // For subobjects with pointer fields, create a new copy of those subobject definitions // with fixed pointers for (UINT i = 0; i < m_Desc.NumSubobjects; i++) { if (m_SubobjectArray[i].Type == D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION) { auto pOriginalSubobjectAssociation = reinterpret_cast(m_SubobjectArray[i].pDesc); D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION Repointed = *pOriginalSubobjectAssociation; auto pWrapper = static_cast(pOriginalSubobjectAssociation->pSubobjectToAssociate); Repointed.pSubobjectToAssociate = pWrapper->pSubobjectArrayLocation; m_RepointedAssociations.push_back(Repointed); m_SubobjectArray[i].pDesc = &m_RepointedAssociations.back(); } } // Below: using ugly way to get pointer in case .data() is not defined m_Desc.pSubobjects = m_Desc.NumSubobjects ? &m_SubobjectArray[0] : nullptr; return m_Desc; } operator const D3D12_STATE_OBJECT_DESC*() { // Cast calls the above final preparation work return &static_cast(*this); } // CreateSubobject creates a sububject helper (e.g. CD3DX12_HIT_GROUP_SUBOBJECT) // whose lifetime is owned by this class. // e.g. // // CD3DX12_STATE_OBJECT_DESC Collection1(D3D12_STATE_OBJECT_TYPE_COLLECTION); // auto Lib0 = Collection1.CreateSubobject(); // Lib0->SetDXILLibrary(&pMyAppDxilLibs[0]); // Lib0->DefineExport(L"rayGenShader0"); // in practice these export listings might be // // data/engine driven // etc. // // Alternatively, users can instantiate sububject helpers explicitly, such as via local // variables instead, passing the state object desc that should point to it into the helper // constructor (or call mySubobjectHelper.AddToStateObject(Collection1)). // In this alternative scenario, the user must keep the subobject alive as long as the state // object it is associated with is alive, else it's pointer references will be stale. // e.g. // // CD3DX12_STATE_OBJECT_DESC RaytracingState2(D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE); // CD3DX12_DXIL_LIBRARY_SUBOBJECT LibA(RaytracingState2); // LibA.SetDXILLibrary(&pMyAppDxilLibs[4]); // not manually specifying exports // // - meaning all exports in the libraries // // are exported // etc. template T* CreateSubobject() { T* pSubobject = new T(*this); m_OwnedSubobjectHelpers.emplace_back(pSubobject); return pSubobject; } private: D3D12_STATE_SUBOBJECT* TrackSubobject(D3D12_STATE_SUBOBJECT_TYPE Type, void* pDesc) { SUBOBJECT_WRAPPER Subobject; Subobject.pSubobjectArrayLocation = nullptr; Subobject.Type = Type; Subobject.pDesc = pDesc; m_SubobjectList.push_back(Subobject); m_Desc.NumSubobjects++; return &m_SubobjectList.back(); } void Init(D3D12_STATE_OBJECT_TYPE Type) { SetStateObjectType(Type); m_Desc.pSubobjects = nullptr; m_Desc.NumSubobjects = 0; m_SubobjectList.clear(); m_SubobjectArray.clear(); m_RepointedAssociations.clear(); } typedef struct SUBOBJECT_WRAPPER : public D3D12_STATE_SUBOBJECT { D3D12_STATE_SUBOBJECT* pSubobjectArrayLocation; // new location when flattened into array // for repointing pointers in subobjects } SUBOBJECT_WRAPPER; D3D12_STATE_OBJECT_DESC m_Desc; std::list m_SubobjectList; // Pointers to list nodes handed out so // these can be edited live std::vector m_SubobjectArray; // Built at the end, copying list contents std::list m_RepointedAssociations; // subobject type that contains pointers to other subobjects, // repointed to flattened array class StringContainer { public: LPCWSTR LocalCopy(LPCWSTR string, bool bSingleString = false) { if (string) { if (bSingleString) { m_Strings.clear(); m_Strings.push_back(string); } else { m_Strings.push_back(string); } return m_Strings.back().c_str(); } else { return nullptr; } } void clear() { m_Strings.clear(); } private: std::list m_Strings; }; class SUBOBJECT_HELPER_BASE { public: SUBOBJECT_HELPER_BASE() { Init(); }; virtual ~SUBOBJECT_HELPER_BASE() {}; virtual D3D12_STATE_SUBOBJECT_TYPE Type() const = 0; void AddToStateObject(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { m_pSubobject = ContainingStateObject.TrackSubobject(Type(), Data()); } protected: virtual void* Data() = 0; void Init() { m_pSubobject = nullptr; } D3D12_STATE_SUBOBJECT* m_pSubobject; }; #if(__cplusplus >= 201103L) // FIXME: This would break with the current impl std::list> m_OwnedSubobjectHelpers; #else class OWNED_HELPER { public: OWNED_HELPER(const SUBOBJECT_HELPER_BASE* pHelper) { m_pHelper = pHelper; } ~OWNED_HELPER() { /*delete m_pHelper;*/ } const SUBOBJECT_HELPER_BASE* m_pHelper; }; std::list m_OwnedSubobjectHelpers; public: void DeleteHelpers() { for (auto& it : m_OwnedSubobjectHelpers) { delete it.m_pHelper; } } private: #endif friend class CD3DX12_DXIL_LIBRARY_SUBOBJECT; friend class CD3DX12_EXISTING_COLLECTION_SUBOBJECT; friend class CD3DX12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT; friend class CD3DX12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION; friend class CD3DX12_HIT_GROUP_SUBOBJECT; friend class CD3DX12_RAYTRACING_SHADER_CONFIG_SUBOBJECT; friend class CD3DX12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT; friend class CD3DX12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT; friend class CD3DX12_LOCAL_ROOT_SIGNATURE_SUBOBJECT; friend class CD3DX12_STATE_OBJECT_CONFIG_SUBOBJECT; friend class CD3DX12_NODE_MASK_SUBOBJECT; }; //------------------------------------------------------------------------------------------------ class CD3DX12_DXIL_LIBRARY_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_DXIL_LIBRARY_SUBOBJECT() { Init(); } CD3DX12_DXIL_LIBRARY_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void SetDXILLibrary(D3D12_SHADER_BYTECODE*pCode) { static const D3D12_SHADER_BYTECODE Default = {}; m_Desc.DXILLibrary = pCode ? *pCode : Default; } void DefineExport( LPCWSTR Name, LPCWSTR ExportToRename = nullptr, D3D12_EXPORT_FLAGS Flags = D3D12_EXPORT_FLAG_NONE) { D3D12_EXPORT_DESC Export; Export.Name = m_Strings.LocalCopy(Name); Export.ExportToRename = m_Strings.LocalCopy(ExportToRename); Export.Flags = Flags; m_Exports.push_back(Export); m_Desc.pExports = &m_Exports[0]; // using ugly way to get pointer in case .data() is not defined m_Desc.NumExports = static_cast(m_Exports.size()); } template void DefineExports(LPCWSTR(&Exports)[N]) { for (UINT i = 0; i < N; i++) { DefineExport(Exports[i]); } } void DefineExports(LPCWSTR* Exports, UINT N) { for (UINT i = 0; i < N; i++) { DefineExport(Exports[i]); } } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_DXIL_LIBRARY; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator const D3D12_DXIL_LIBRARY_DESC&() const { return m_Desc; } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_Desc = {}; m_Strings.clear(); m_Exports.clear(); } void* Data() { return &m_Desc; } D3D12_DXIL_LIBRARY_DESC m_Desc; CD3DX12_STATE_OBJECT_DESC::StringContainer m_Strings; std::vector m_Exports; }; //------------------------------------------------------------------------------------------------ class CD3DX12_EXISTING_COLLECTION_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_EXISTING_COLLECTION_SUBOBJECT() { Init(); } CD3DX12_EXISTING_COLLECTION_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void SetExistingCollection(ID3D12StateObject*pExistingCollection) { m_Desc.pExistingCollection = pExistingCollection; m_CollectionRef = pExistingCollection; } void DefineExport( LPCWSTR Name, LPCWSTR ExportToRename = nullptr, D3D12_EXPORT_FLAGS Flags = D3D12_EXPORT_FLAG_NONE) { D3D12_EXPORT_DESC Export; Export.Name = m_Strings.LocalCopy(Name); Export.ExportToRename = m_Strings.LocalCopy(ExportToRename); Export.Flags = Flags; m_Exports.push_back(Export); m_Desc.pExports = &m_Exports[0]; // using ugly way to get pointer in case .data() is not defined m_Desc.NumExports = static_cast(m_Exports.size()); } template void DefineExports(LPCWSTR(&Exports)[N]) { for (UINT i = 0; i < N; i++) { DefineExport(Exports[i]); } } void DefineExports(LPCWSTR* Exports, UINT N) { for (UINT i = 0; i < N; i++) { DefineExport(Exports[i]); } } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_EXISTING_COLLECTION; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator const D3D12_EXISTING_COLLECTION_DESC&() const { return m_Desc; } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_Desc = {}; m_CollectionRef = nullptr; m_Strings.clear(); m_Exports.clear(); } void* Data() { return &m_Desc; } D3D12_EXISTING_COLLECTION_DESC m_Desc; Microsoft::WRL::ComPtr m_CollectionRef; CD3DX12_STATE_OBJECT_DESC::StringContainer m_Strings; std::vector m_Exports; }; //------------------------------------------------------------------------------------------------ class CD3DX12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT() { Init(); } CD3DX12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void SetSubobjectToAssociate(const D3D12_STATE_SUBOBJECT& SubobjectToAssociate) { m_Desc.pSubobjectToAssociate = &SubobjectToAssociate; } void AddExport(LPCWSTR Export) { m_Desc.NumExports++; m_Exports.push_back(m_Strings.LocalCopy(Export)); m_Desc.pExports = &m_Exports[0]; // using ugly way to get pointer in case .data() is not defined } template void AddExports(LPCWSTR (&Exports)[N]) { for (UINT i = 0; i < N; i++) { AddExport(Exports[i]); } } void AddExports(LPCWSTR* Exports, UINT N) { for (UINT i = 0; i < N; i++) { AddExport(Exports[i]); } } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_SUBOBJECT_TO_EXPORTS_ASSOCIATION; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator const D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION&() const { return m_Desc; } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_Desc = {}; m_Strings.clear(); m_Exports.clear(); } void* Data() { return &m_Desc; } D3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION m_Desc; CD3DX12_STATE_OBJECT_DESC::StringContainer m_Strings; std::vector m_Exports; }; //------------------------------------------------------------------------------------------------ class CD3DX12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION() { Init(); } CD3DX12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void SetSubobjectNameToAssociate(LPCWSTR SubobjectToAssociate) { m_Desc.SubobjectToAssociate = m_SubobjectName.LocalCopy(SubobjectToAssociate, true); } void AddExport(LPCWSTR Export) { m_Desc.NumExports++; m_Exports.push_back(m_Strings.LocalCopy(Export)); m_Desc.pExports = &m_Exports[0]; // using ugly way to get pointer in case .data() is not defined } template void AddExports(LPCWSTR (&Exports)[N]) { for (UINT i = 0; i < N; i++) { AddExport(Exports[i]); } } void AddExports(LPCWSTR* Exports, UINT N) { for (UINT i = 0; i < N; i++) { AddExport(Exports[i]); } } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator const D3D12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION&() const { return m_Desc; } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_Desc = {}; m_Strings.clear(); m_SubobjectName.clear(); m_Exports.clear(); } void* Data() { return &m_Desc; } D3D12_DXIL_SUBOBJECT_TO_EXPORTS_ASSOCIATION m_Desc; CD3DX12_STATE_OBJECT_DESC::StringContainer m_Strings; CD3DX12_STATE_OBJECT_DESC::StringContainer m_SubobjectName; std::vector m_Exports; }; //------------------------------------------------------------------------------------------------ class CD3DX12_HIT_GROUP_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_HIT_GROUP_SUBOBJECT() { Init(); } CD3DX12_HIT_GROUP_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void SetHitGroupExport(LPCWSTR exportName) { m_Desc.HitGroupExport = m_Strings[0].LocalCopy(exportName, true); } void SetHitGroupType(D3D12_HIT_GROUP_TYPE Type) { m_Desc.Type = Type; } void SetAnyHitShaderImport(LPCWSTR importName) { m_Desc.AnyHitShaderImport = m_Strings[1].LocalCopy(importName, true); } void SetClosestHitShaderImport(LPCWSTR importName) { m_Desc.ClosestHitShaderImport = m_Strings[2].LocalCopy(importName, true); } void SetIntersectionShaderImport(LPCWSTR importName) { m_Desc.IntersectionShaderImport = m_Strings[3].LocalCopy(importName, true); } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_HIT_GROUP; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator const D3D12_HIT_GROUP_DESC&() const { return m_Desc; } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_Desc = {}; for (UINT i = 0; i < m_NumStrings; i++) { m_Strings[i].clear(); } } void* Data() { return &m_Desc; } D3D12_HIT_GROUP_DESC m_Desc; static const UINT m_NumStrings = 4; CD3DX12_STATE_OBJECT_DESC::StringContainer m_Strings[m_NumStrings]; // one string for every entrypoint name }; //------------------------------------------------------------------------------------------------ class CD3DX12_RAYTRACING_SHADER_CONFIG_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_RAYTRACING_SHADER_CONFIG_SUBOBJECT() { Init(); } CD3DX12_RAYTRACING_SHADER_CONFIG_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void Config(UINT MaxPayloadSizeInBytes, UINT MaxAttributeSizeInBytes) { m_Desc.MaxPayloadSizeInBytes = MaxPayloadSizeInBytes; m_Desc.MaxAttributeSizeInBytes = MaxAttributeSizeInBytes; } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_SHADER_CONFIG; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator const D3D12_RAYTRACING_SHADER_CONFIG&() const { return m_Desc; } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_Desc = {}; } void* Data() { return &m_Desc; } D3D12_RAYTRACING_SHADER_CONFIG m_Desc; }; //------------------------------------------------------------------------------------------------ class CD3DX12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT() { Init(); } CD3DX12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void Config(UINT MaxTraceRecursionDepth) { m_Desc.MaxTraceRecursionDepth = MaxTraceRecursionDepth; } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_PIPELINE_CONFIG; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator const D3D12_RAYTRACING_PIPELINE_CONFIG&() const { return m_Desc; } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_Desc = {}; } void* Data() { return &m_Desc; } D3D12_RAYTRACING_PIPELINE_CONFIG m_Desc; }; //------------------------------------------------------------------------------------------------ class CD3DX12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT() { Init(); } CD3DX12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void SetRootSignature(ID3D12RootSignature* pRootSig) { m_pRootSig = pRootSig; } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_GLOBAL_ROOT_SIGNATURE; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator ID3D12RootSignature*() const { return m_pRootSig.Get(); } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_pRootSig = nullptr; } void* Data() { return m_pRootSig.GetAddressOf(); } Microsoft::WRL::ComPtr m_pRootSig; }; //------------------------------------------------------------------------------------------------ class CD3DX12_LOCAL_ROOT_SIGNATURE_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_LOCAL_ROOT_SIGNATURE_SUBOBJECT() { Init(); } CD3DX12_LOCAL_ROOT_SIGNATURE_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void SetRootSignature(ID3D12RootSignature* pRootSig) { m_pRootSig = pRootSig; } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_LOCAL_ROOT_SIGNATURE; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator ID3D12RootSignature*() const { return m_pRootSig.Get(); } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_pRootSig = nullptr; } void* Data() { return m_pRootSig.GetAddressOf(); } Microsoft::WRL::ComPtr m_pRootSig; }; //------------------------------------------------------------------------------------------------ class CD3DX12_STATE_OBJECT_CONFIG_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_STATE_OBJECT_CONFIG_SUBOBJECT() { Init(); } CD3DX12_STATE_OBJECT_CONFIG_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void SetFlags(D3D12_STATE_OBJECT_FLAGS Flags) { m_Desc.Flags = Flags; } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_STATE_OBJECT_CONFIG; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator const D3D12_STATE_OBJECT_CONFIG&() const { return m_Desc; } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_Desc = {}; } void* Data() { return &m_Desc; } D3D12_STATE_OBJECT_CONFIG m_Desc; }; //------------------------------------------------------------------------------------------------ class CD3DX12_NODE_MASK_SUBOBJECT : public CD3DX12_STATE_OBJECT_DESC::SUBOBJECT_HELPER_BASE { public: CD3DX12_NODE_MASK_SUBOBJECT() { Init(); } CD3DX12_NODE_MASK_SUBOBJECT(CD3DX12_STATE_OBJECT_DESC& ContainingStateObject) { Init(); AddToStateObject(ContainingStateObject); } void SetNodeMask(UINT NodeMask) { m_Desc.NodeMask = NodeMask; } D3D12_STATE_SUBOBJECT_TYPE Type() const { return D3D12_STATE_SUBOBJECT_TYPE_NODE_MASK; } operator const D3D12_STATE_SUBOBJECT&() const { return *m_pSubobject; } operator const D3D12_NODE_MASK&() const { return m_Desc; } private: void Init() { SUBOBJECT_HELPER_BASE::Init(); m_Desc = {}; } void* Data() { return &m_Desc; } D3D12_NODE_MASK m_Desc; }; #endif // #ifndef D3DX12_NO_STATE_OBJECT_HELPERS #endif // defined( __cplusplus ) #pragma warning(pop) #endif //__D3DX12_H__ ================================================ FILE: src/engine_registry.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "engine_registry.hpp" #include "pipeline_registry.hpp" #include "root_signature_registry.hpp" #include "shader_registry.hpp" #include "rt_pipeline_registry.hpp" #include "d3d12/d3d12_structs.hpp" // Register something into a registry. #define REGISTER(type, registry) decltype(type) type = registry::Get().Register // Decscriptor Range Array #define DESC_RANGE_ARRAY(name, ...) std::vector name { __VA_ARGS__ }; // Descriptor Range #define DESC_RANGE(...) [] { return GetRange(__VA_ARGS__); }() // Descriptor Range Hardcoded #define DESC_RANGE_H(...) [] { CD3DX12_DESCRIPTOR_RANGE macro_range; macro_range.Init(__VA_ARGS__); return macro_range; }() // Root parameter #define ROOT_PARAM(func) [] { return func; }() // Root Parameter for descriptor tables #define ROOT_PARAM_DESC_TABLE(arr, visibility) [] { CD3DX12_ROOT_PARAMETER d; d.InitAsDescriptorTable(static_cast(arr.size()), arr.data(), visibility); return d; }() namespace wr { using namespace rs_layout; //BDRF Lut Precalculation Root Signature DESC_RANGE_ARRAY(ranges_brdf, DESC_RANGE(params::brdf_lut, Type::UAV_RANGE, params::BRDF_LutE::OUTPUT), ); REGISTER(root_signatures::brdf_lut, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(ranges_brdf, D3D12_SHADER_VISIBILITY_ALL) }, .m_samplers = { } }); //Basic Deferred Pass Root Signature DESC_RANGE_ARRAY(ranges_deferred_main, DESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::ALBEDO), DESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::NORMAL), DESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::ROUGHNESS), DESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::METALLIC), DESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::AMBIENT_OCCLUSION), DESC_RANGE(params::deferred_main, Type::SRV_RANGE, params::DeferredMainE::EMISSIVE), ); REGISTER(root_signatures::basic, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM(GetCBV(params::deferred_main, params::DeferredMainE::CAMERA_PROPERTIES, D3D12_SHADER_VISIBILITY_VERTEX)), ROOT_PARAM(GetCBV(params::deferred_main, params::DeferredMainE::OBJECT_PROPERTIES, D3D12_SHADER_VISIBILITY_VERTEX)), ROOT_PARAM_DESC_TABLE(ranges_deferred_main, D3D12_SHADER_VISIBILITY_PIXEL), ROOT_PARAM(GetCBV(params::deferred_main, params::DeferredMainE::MATERIAL_PROPERTIES, D3D12_SHADER_VISIBILITY_PIXEL)), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_WRAP } } }); DESC_RANGE_ARRAY(svgf_denoiser_ranges, DESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::INPUT), DESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::MOTION), DESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::NORMAL), DESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::DEPTH), DESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::IN_HIST_LENGTH), DESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::PREV_INPUT), DESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::PREV_MOMENTS), DESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::PREV_NORMAL), DESC_RANGE(params::svgf_denoiser, Type::SRV_RANGE, params::SVGFDenoiserE::PREV_DEPTH), DESC_RANGE(params::svgf_denoiser, Type::UAV_RANGE, params::SVGFDenoiserE::OUT_COLOR), DESC_RANGE(params::svgf_denoiser, Type::UAV_RANGE, params::SVGFDenoiserE::OUT_MOMENTS), DESC_RANGE(params::svgf_denoiser, Type::UAV_RANGE, params::SVGFDenoiserE::OUT_HIST_LENGTH), ); REGISTER(root_signatures::svgf_denoiser, RootSignatureRegistry)({ .m_parameters ={ ROOT_PARAM_DESC_TABLE(svgf_denoiser_ranges, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetCBV(params::svgf_denoiser, params::SVGFDenoiserE::SVGF_PROPERTIES)), }, .m_samplers = { {TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}, {TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP} } }); //Deferred Composition Root Signature DESC_RANGE_ARRAY(srv_ranges, DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::GBUFFER_ALBEDO_ROUGHNESS), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::GBUFFER_NORMAL_METALLIC), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::GBUFFER_EMISSIVE_AO), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::GBUFFER_DEPTH), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::LIGHT_BUFFER), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::SKY_BOX), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::IRRADIANCE_MAP), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::PREF_ENV_MAP), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BRDF_LUT), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BUFFER_REFLECTION), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BUFFER_SHADOW), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BUFFER_SCREEN_SPACE_IRRADIANCE), DESC_RANGE(params::deferred_composition, Type::SRV_RANGE, params::DeferredCompositionE::BUFFER_AO), DESC_RANGE(params::deferred_composition, Type::UAV_RANGE, params::DeferredCompositionE::OUTPUT), ); REGISTER(root_signatures::deferred_composition, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM(GetCBV(params::deferred_composition, params::DeferredCompositionE::CAMERA_PROPERTIES)), ROOT_PARAM_DESC_TABLE(srv_ranges, D3D12_SHADER_VISIBILITY_ALL), }, .m_samplers = { { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP }, { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP } } }); //MipMapping Root Signature DESC_RANGE_ARRAY(mip_in_out_ranges, DESC_RANGE(params::mip_mapping, Type::SRV_RANGE, params::MipMappingE::SOURCE), DESC_RANGE(params::mip_mapping, Type::UAV_RANGE, params::MipMappingE::DEST), ); REGISTER(root_signatures::mip_mapping, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM(GetConstants(params::mip_mapping, params::MipMappingE::CBUFFER)), ROOT_PARAM_DESC_TABLE(mip_in_out_ranges, D3D12_SHADER_VISIBILITY_ALL) }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP } } }); //Prefiltering Root Signature DESC_RANGE_ARRAY(prefilter_in_out_ranges, DESC_RANGE(params::cubemap_prefiltering, Type::SRV_RANGE, params::CubemapPrefilteringE::SOURCE), DESC_RANGE(params::cubemap_prefiltering, Type::UAV_RANGE, params::CubemapPrefilteringE::DEST), ); REGISTER(root_signatures::cubemap_prefiltering, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM(GetConstants(params::cubemap_prefiltering, params::CubemapPrefilteringE::CBUFFER)), ROOT_PARAM_DESC_TABLE(prefilter_in_out_ranges, D3D12_SHADER_VISIBILITY_ALL) }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP } } }); //Cubemap conversion root signature DESC_RANGE_ARRAY(cubemap_tasks_ranges, DESC_RANGE(params::cubemap_conversion, Type::SRV_RANGE, params::CubemapConversionE::EQUIRECTANGULAR_TEXTURE), ); REGISTER(root_signatures::cubemap_conversion, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM(GetConstants(params::cubemap_conversion, params::CubemapConversionE::IDX)), ROOT_PARAM(GetCBV(params::cubemap_conversion, params::CubemapConversionE::CAMERA_PROPERTIES)), ROOT_PARAM_DESC_TABLE(cubemap_tasks_ranges, D3D12_SHADER_VISIBILITY_PIXEL), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP } } }); //Cubemap convolution root signature DESC_RANGE_ARRAY(cubemap_convolution_ranges, DESC_RANGE(params::cubemap_conversion, Type::SRV_RANGE, params::CubemapConvolutionE::ENVIRONMENT_CUBEMAP), ); REGISTER(root_signatures::cubemap_convolution, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM(GetConstants(params::cubemap_convolution, params::CubemapConvolutionE::IDX)), ROOT_PARAM(GetCBV(params::cubemap_convolution, params::CubemapConvolutionE::CAMERA_PROPERTIES)), ROOT_PARAM_DESC_TABLE(cubemap_tasks_ranges, D3D12_SHADER_VISIBILITY_PIXEL), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP } } }); REGISTER(shaders::brdf_lut_cs, ShaderRegistry)({ .path = "resources/shaders/pbr_brdf_lut.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::basic_deferred_vs, ShaderRegistry)({ .path = "resources/shaders/deferred_geometry_pass.hlsl", .entry = "main_vs", .type = ShaderType::VERTEX_SHADER, .defines = {} }); REGISTER(shaders::basic_deferred_ps, ShaderRegistry)({ .path = "resources/shaders/deferred_geometry_pass.hlsl", .entry = "main_ps", .type = ShaderType::PIXEL_SHADER, .defines = {} }); REGISTER(shaders::basic_hybrid_vs, ShaderRegistry)({ .path = "resources/shaders/deferred_geometry_pass.hlsl", .entry = "main_vs", .type = ShaderType::VERTEX_SHADER, .defines = {{L"IS_HYBRID", L"1"}} }); REGISTER(shaders::basic_hybrid_ps, ShaderRegistry)({ .path = "resources/shaders/deferred_geometry_pass.hlsl", .entry = "main_ps", .type = ShaderType::PIXEL_SHADER, .defines = {{L"IS_HYBRID", L"1"}} }); REGISTER(shaders::fullscreen_quad_vs, ShaderRegistry)({ .path = "resources/shaders/fullscreen_quad.hlsl", .entry = "main_vs", .type = ShaderType::VERTEX_SHADER, .defines = {} }); REGISTER(shaders::svgf_denoiser_reprojection_cs, ShaderRegistry)({ .path = "resources/shaders/denoising_SVGF.hlsl", .entry = "reprojection_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::svgf_denoiser_filter_moments_cs, ShaderRegistry)({ .path = "resources/shaders/denoising_SVGF.hlsl", .entry = "filter_moments_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::svgf_denoiser_wavelet_filter_cs, ShaderRegistry)({ .path = "resources/shaders/denoising_SVGF.hlsl", .entry = "wavelet_filter_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::deferred_composition_cs, ShaderRegistry)({ .path = "resources/shaders/deferred_composition_pass.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::mip_mapping_cs, ShaderRegistry)({ .path = "resources/shaders/generate_mips_cs.hlsl", .entry = "main", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::equirect_to_cubemap_vs, ShaderRegistry)({ .path = "resources/shaders/pbr_cubemap_conversion.hlsl", .entry = "main_vs", .type = ShaderType::VERTEX_SHADER, .defines = {} }); REGISTER(shaders::equirect_to_cubemap_ps, ShaderRegistry)({ .path = "resources/shaders/pbr_cubemap_conversion.hlsl", .entry = "main_ps", .type = ShaderType::PIXEL_SHADER, .defines = {} }); REGISTER(shaders::cubemap_convolution_ps, ShaderRegistry)({ .path = "resources/shaders/pbr_cubemap_convolution.hlsl", .entry = "main_ps", .type = ShaderType::PIXEL_SHADER, .defines = {} }); REGISTER(shaders::cubemap_prefiltering_cs, ShaderRegistry)({ .path = "resources/shaders/pbr_prefilter_env_map.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(pipelines::brdf_lut_precalculation, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::brdf_lut_cs, .m_root_signature_handle = root_signatures::brdf_lut, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { Format::R16G16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::basic_deferred, PipelineRegistry) < Vertex > ({ .m_vertex_shader_handle = shaders::basic_deferred_vs, .m_pixel_shader_handle = shaders::basic_deferred_ps, .m_compute_shader_handle = std::nullopt, .m_root_signature_handle = root_signatures::basic, .m_dsv_format = Format::D32_FLOAT, .m_rtv_formats = { Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 3, .m_type = PipelineType::GRAPHICS_PIPELINE, .m_cull_mode = CullMode::CULL_NONE, .m_depth_enabled = true, .m_counter_clockwise = false, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::basic_hybrid, PipelineRegistry) < Vertex > ({ .m_vertex_shader_handle = shaders::basic_hybrid_vs, .m_pixel_shader_handle = shaders::basic_hybrid_ps, .m_compute_shader_handle = std::nullopt, .m_root_signature_handle = root_signatures::basic, .m_dsv_format = Format::D32_FLOAT, .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 }, .m_num_rtv_formats = 6, .m_type = PipelineType::GRAPHICS_PIPELINE, .m_cull_mode = CullMode::CULL_NONE, .m_depth_enabled = true, .m_counter_clockwise = false, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::svgf_denoiser_reprojection, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::svgf_denoiser_reprojection_cs, .m_root_signature_handle = root_signatures::svgf_denoiser, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = {Format::R16G16B16A16_FLOAT}, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::svgf_denoiser_filter_moments, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::svgf_denoiser_filter_moments_cs, .m_root_signature_handle = root_signatures::svgf_denoiser, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = {Format::R16G16B16A16_FLOAT}, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::svgf_denoiser_wavelet_filter, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::svgf_denoiser_wavelet_filter_cs, .m_root_signature_handle = root_signatures::svgf_denoiser, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = {Format::R16G16B16A16_FLOAT}, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::deferred_composition, PipelineRegistry)({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::deferred_composition_cs, .m_root_signature_handle = root_signatures::deferred_composition, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::mip_mapping, PipelineRegistry) < Vertex > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::mip_mapping_cs, .m_root_signature_handle = root_signatures::mip_mapping, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { }, .m_num_rtv_formats = 0, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::equirect_to_cubemap, PipelineRegistry) < Vertex > ( { .m_vertex_shader_handle = shaders::equirect_to_cubemap_vs, .m_pixel_shader_handle = shaders::equirect_to_cubemap_ps, .m_compute_shader_handle = std::nullopt, .m_root_signature_handle = root_signatures::cubemap_conversion, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::GRAPHICS_PIPELINE, .m_cull_mode = CullMode::CULL_NONE, .m_depth_enabled = false, .m_counter_clockwise = false, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::cubemap_convolution, PipelineRegistry) < Vertex > ( { .m_vertex_shader_handle = shaders::equirect_to_cubemap_vs, .m_pixel_shader_handle = shaders::cubemap_convolution_ps, .m_compute_shader_handle = std::nullopt, .m_root_signature_handle = root_signatures::cubemap_convolution, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::GRAPHICS_PIPELINE, .m_cull_mode = CullMode::CULL_NONE, .m_depth_enabled = false, .m_counter_clockwise = false, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::cubemap_prefiltering, PipelineRegistry) < Vertex > ( { .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::cubemap_prefiltering_cs, .m_root_signature_handle = root_signatures::cubemap_prefiltering, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { Format::UNKNOWN }, .m_num_rtv_formats = 0, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_NONE, .m_depth_enabled = false, .m_counter_clockwise = false, .m_topology_type = TopologyType::TRIANGLE }); /* ### Raytracing ### */ REGISTER(shaders::post_processing, ShaderRegistry)({ .path = "resources/shaders/pp_tonemapping.hlsl", .entry = "main", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::accumulation, ShaderRegistry)({ .path = "resources/shaders/dxr_pathtracer_accumulation.hlsl", .entry = "main", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::rt_lib, ShaderRegistry)({ .path = "resources/shaders/dxr_raytracing.hlsl", .entry = "RaygenEntry", .type = ShaderType::LIBRARY_SHADER, .defines = {} }); DESC_RANGE_ARRAY(post_r, DESC_RANGE(params::post_processing, Type::SRV_RANGE, params::PostProcessingE::SOURCE), DESC_RANGE(params::post_processing, Type::UAV_RANGE, params::PostProcessingE::DEST), ); REGISTER(root_signatures::post_processing, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(post_r, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetConstants(params::post_processing, params::PostProcessingE::HDR_SUPPORT)), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_BORDER } } }); DESC_RANGE_ARRAY(accum_r, DESC_RANGE(params::accumulation, Type::SRV_RANGE, params::AccumulationE::SOURCE), DESC_RANGE(params::accumulation, Type::UAV_RANGE, params::AccumulationE::DEST), ); REGISTER(root_signatures::accumulation, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(accum_r, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetConstants(params::accumulation, params::AccumulationE::FRAME_IDX)), }, .m_samplers = { { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_BORDER } } }); REGISTER(pipelines::post_processing, PipelineRegistry) < Vertex2D > ( { .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::post_processing, .m_root_signature_handle = root_signatures::post_processing, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { d3d12::settings::back_buffer_format }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_NONE, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::accumulation, PipelineRegistry) < Vertex2D > ( { .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::accumulation, .m_root_signature_handle = root_signatures::accumulation, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_NONE, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); DESC_RANGE_ARRAY(r_full_rt, DESC_RANGE(params::full_raytracing, Type::UAV_RANGE, params::FullRaytracingE::OUTPUT), DESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::INDICES), DESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::LIGHTS), DESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::MATERIALS), DESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::OFFSETS), DESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::SKYBOX), DESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::BRDF_LUT), DESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::IRRADIANCE_MAP), DESC_RANGE(params::full_raytracing, Type::SRV_RANGE, params::FullRaytracingE::TEXTURES), DESC_RANGE_H(D3D12_DESCRIPTOR_RANGE_TYPE::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 5, d3d12::settings::fallback_ptrs_offset), ); REGISTER(root_signatures::rt_test_global, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(r_full_rt, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetSRV(params::full_raytracing, params::FullRaytracingE::ACCELERATION_STRUCTURE)), ROOT_PARAM(GetCBV(params::full_raytracing, params::FullRaytracingE::CAMERA_PROPERTIES)), ROOT_PARAM(GetSRV(params::full_raytracing, params::FullRaytracingE::VERTICES)), }, .m_samplers = { { TextureFilter::FILTER_ANISOTROPIC, TextureAddressMode::TAM_WRAP }, { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP } }, .m_rtx = true }); StateObjectDescription::LibraryDesc rt_full_lib = []() { StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::rt_lib; lib.exports.push_back(L"RaygenEntry"); lib.exports.push_back(L"ClosestHitEntry"); lib.exports.push_back(L"MissEntry"); lib.exports.push_back(L"ShadowClosestHitEntry"); lib.exports.push_back(L"ShadowMissEntry"); lib.m_hit_groups.push_back({ L"MyHitGroup", L"ClosestHitEntry" }); lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); return lib; }(); REGISTER(state_objects::state_object, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, .library_desc = rt_full_lib, .max_payload_size = (sizeof(float) * 7) + sizeof(unsigned int), .max_attributes_size = sizeof(float) * 4, .max_recursion_depth = 3, .global_root_signature = root_signatures::rt_test_global, .local_root_signatures = {}, }); /* ### Depth of field ### */ // Cone of confusion REGISTER(shaders::dof_coc, ShaderRegistry) ({ .path = "resources/shaders/pp_dof_coc.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(dofcoc_r, DESC_RANGE(params::dof_coc, Type::SRV_RANGE, params::DoFCoCE::GDEPTH), DESC_RANGE(params::dof_coc, Type::UAV_RANGE, params::DoFCoCE::OUTPUT), ); REGISTER(root_signatures::dof_coc, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(dofcoc_r, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetCBV(params::dof_coc, params::DoFCoCE::CAMERA_PROPERTIES)), }, .m_samplers = { { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP} } }); REGISTER(pipelines::dof_coc, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::dof_coc, .m_root_signature_handle = root_signatures::dof_coc, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { Format::R16G16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // Compute Near mask REGISTER(shaders::dof_near_mask, ShaderRegistry) ({ .path = "resources/shaders/pp_dof_compute_near_mask.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(dofnear_r, DESC_RANGE(params::dof_near_mask, Type::SRV_RANGE, params::DoFNearMaskE::INPUT), DESC_RANGE(params::dof_near_mask, Type::UAV_RANGE, params::DoFNearMaskE::OUTPUT), ); REGISTER(root_signatures::dof_near_mask, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(dofnear_r, D3D12_SHADER_VISIBILITY_ALL), }, .m_samplers = { { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP} } }); REGISTER(pipelines::dof_near_mask, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::dof_near_mask, .m_root_signature_handle = root_signatures::dof_near_mask, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { Format::R16G16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // Down Scale texture REGISTER(shaders::down_scale, ShaderRegistry)({ .path = "resources/shaders/pp_dof_downscale.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(dscale_r, DESC_RANGE(params::down_scale, Type::SRV_RANGE, params::DownScaleE::SOURCE), DESC_RANGE(params::down_scale, Type::UAV_RANGE, params::DownScaleE::OUTPUT_NEAR), DESC_RANGE(params::down_scale, Type::UAV_RANGE, params::DownScaleE::OUTPUT_FAR), DESC_RANGE(params::down_scale, Type::SRV_RANGE, params::DownScaleE::COC), ); REGISTER(root_signatures::down_scale, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(dscale_r, D3D12_SHADER_VISIBILITY_ALL), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}, { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP} } }); REGISTER(pipelines::down_scale, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::down_scale, .m_root_signature_handle = root_signatures::down_scale, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}, .m_num_rtv_formats = 2, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // dof dilate REGISTER(shaders::dof_dilate, ShaderRegistry)({ .path = "resources/shaders/pp_dof_dilate.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(dilate_r, DESC_RANGE(params::dof_dilate, Type::SRV_RANGE, params::DoFDilateE::SOURCE), DESC_RANGE(params::dof_dilate, Type::UAV_RANGE, params::DoFDilateE::OUTPUT), ); REGISTER(root_signatures::dof_dilate, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(dilate_r, D3D12_SHADER_VISIBILITY_ALL), }, .m_samplers = { { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP} } }); REGISTER(pipelines::dof_dilate, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::dof_dilate, .m_root_signature_handle = root_signatures::dof_dilate, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { Format::R32_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); //dof bokeh REGISTER(shaders::dof_bokeh, ShaderRegistry)({ .path = "resources/shaders/pp_dof_bokeh.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(dof_bokeh_r, DESC_RANGE(params::dof_bokeh, Type::SRV_RANGE, params::DoFBokehE::SOURCE_NEAR), DESC_RANGE(params::dof_bokeh, Type::SRV_RANGE, params::DoFBokehE::SOURCE_FAR), DESC_RANGE(params::dof_bokeh, Type::UAV_RANGE, params::DoFBokehE::OUTPUT_NEAR), DESC_RANGE(params::dof_bokeh, Type::UAV_RANGE, params::DoFBokehE::OUTPUT_FAR), DESC_RANGE(params::dof_bokeh, Type::SRV_RANGE, params::DoFBokehE::COC), ); REGISTER(root_signatures::dof_bokeh, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(dof_bokeh_r, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetCBV(params::dof_bokeh, params::DoFBokehE::CAMERA_PROPERTIES)), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}, { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP} } }); REGISTER(pipelines::dof_bokeh, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::dof_bokeh, .m_root_signature_handle = root_signatures::dof_bokeh, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 2, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); //dof bokeh post filter REGISTER(shaders::dof_bokeh_post_filter, ShaderRegistry)({ .path = "resources/shaders/pp_dof_bokeh_post_filter.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(dof_bokeh_post_filter_r, DESC_RANGE(params::dof_bokeh_post_filter, Type::SRV_RANGE, params::DoFBokehPostFilterE::SOURCE_NEAR), DESC_RANGE(params::dof_bokeh_post_filter, Type::SRV_RANGE, params::DoFBokehPostFilterE::SOURCE_FAR), DESC_RANGE(params::dof_bokeh_post_filter, Type::UAV_RANGE, params::DoFBokehPostFilterE::OUTPUT_NEAR), DESC_RANGE(params::dof_bokeh_post_filter, Type::UAV_RANGE, params::DoFBokehPostFilterE::OUTPUT_FAR), ); REGISTER(root_signatures::dof_bokeh_post_filter, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(dof_bokeh_post_filter_r, D3D12_SHADER_VISIBILITY_ALL), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}, { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP} } }); REGISTER(pipelines::dof_bokeh_post_filter, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::dof_bokeh_post_filter, .m_root_signature_handle = root_signatures::dof_bokeh_post_filter, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT, wr::Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 2, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // depth of field composition REGISTER(shaders::dof_composition, ShaderRegistry)({ .path = "resources/shaders/pp_dof_composition.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(dof_composition_r, DESC_RANGE(params::dof_composition, Type::SRV_RANGE, params::DoFCompositionE::SOURCE), DESC_RANGE(params::dof_composition, Type::UAV_RANGE, params::DoFCompositionE::OUTPUT), DESC_RANGE(params::dof_composition, Type::SRV_RANGE, params::DoFCompositionE::BOKEH_NEAR), DESC_RANGE(params::dof_composition, Type::SRV_RANGE, params::DoFCompositionE::BOKEH_FAR), DESC_RANGE(params::dof_composition, Type::SRV_RANGE, params::DoFCompositionE::COC), ); REGISTER(root_signatures::dof_composition, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(dof_composition_r, D3D12_SHADER_VISIBILITY_ALL), }, .m_samplers = { { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP}, { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP} } }); REGISTER(pipelines::dof_composition, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::dof_composition, .m_root_signature_handle = root_signatures::dof_composition, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // Extract bright REGISTER(shaders::bloom_extract_bright, ShaderRegistry)({ .path = "resources/shaders/pp_bloom_extract_bright.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER }); DESC_RANGE_ARRAY(bloombright_r, DESC_RANGE(params::bloom_extract_bright, Type::SRV_RANGE, params::BloomExtractBrightE::SOURCE), DESC_RANGE(params::bloom_extract_bright, Type::SRV_RANGE, params::BloomExtractBrightE::G_EMISSIVE), DESC_RANGE(params::bloom_extract_bright, Type::SRV_RANGE, params::BloomExtractBrightE::G_DEPTH), DESC_RANGE(params::bloom_extract_bright, Type::UAV_RANGE, params::BloomExtractBrightE::OUTPUT_BRIGHT), ); REGISTER(root_signatures::bloom_extract_bright, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(bloombright_r, D3D12_SHADER_VISIBILITY_ALL), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}, { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP} } }); REGISTER(pipelines::bloom_extract_bright, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::bloom_extract_bright, .m_root_signature_handle = root_signatures::bloom_extract_bright, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { wr::Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // bloom blur REGISTER(shaders::bloom_blur, ShaderRegistry)({ .path = "resources/shaders/pp_bloom_blur.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(bloom_blur_r, DESC_RANGE(params::bloom_blur, Type::SRV_RANGE, params::BloomBlurE::SOURCE), DESC_RANGE(params::bloom_blur, Type::UAV_RANGE, params::BloomBlurE::OUTPUT), ); REGISTER(root_signatures::bloom_blur, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(bloom_blur_r, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetCBV(params::bloom_blur, params::BloomBlurE::BLUR_DIRECTION)), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}, } }); REGISTER(pipelines::bloom_blur, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::bloom_blur, .m_root_signature_handle = root_signatures::bloom_blur, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = {wr::Format::R16G16B16A16_FLOAT}, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // bloom blur horizontal REGISTER(shaders::bloom_blur_horizontal, ShaderRegistry)({ .path = "resources/shaders/pp_bloom_blur_horizontal.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(bloom_blur_t_r, DESC_RANGE(params::bloom_blur_horizontal, Type::SRV_RANGE, params::BloomBlurHorizontalE::SOURCE_MAIN), DESC_RANGE(params::bloom_blur_horizontal, Type::UAV_RANGE, params::BloomBlurHorizontalE::OUTPUT), DESC_RANGE(params::bloom_blur_horizontal, Type::UAV_RANGE, params::BloomBlurHorizontalE::OUTPUT_QES), ); REGISTER(root_signatures::bloom_blur_horizontal, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(bloom_blur_t_r, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetCBV(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::BLOOM_PROPERTIES)), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}, } }); REGISTER(pipelines::bloom_blur_horizontal, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::bloom_blur_horizontal, .m_root_signature_handle = root_signatures::bloom_blur_horizontal, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = {wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}, .m_num_rtv_formats = 2, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // bloom blur test REGISTER(shaders::bloom_blur_vertical, ShaderRegistry)({ .path = "resources/shaders/pp_bloom_blur_vertical.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(bloom_blur_v_r, DESC_RANGE(params::bloom_blur_vertical, Type::SRV_RANGE, params::BloomBlurVerticalE::SOURCE_MAIN), DESC_RANGE(params::bloom_blur_vertical, Type::SRV_RANGE, params::BloomBlurVerticalE::SOURCE_QES), DESC_RANGE(params::bloom_blur_vertical, Type::UAV_RANGE, params::BloomBlurVerticalE::OUTPUT), DESC_RANGE(params::bloom_blur_vertical, Type::UAV_RANGE, params::BloomBlurVerticalE::OUTPUT_QES), ); REGISTER(root_signatures::bloom_blur_vertical, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(bloom_blur_v_r, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetCBV(params::bloom_blur_vertical, params::BloomBlurVerticalE::BLOOM_PROPERTIES)), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}, } }); REGISTER(pipelines::bloom_blur_vertical, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::bloom_blur_vertical, .m_root_signature_handle = root_signatures::bloom_blur_vertical, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = {wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}, .m_num_rtv_formats = 2, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // bloom composition REGISTER(shaders::bloom_composition, ShaderRegistry)({ .path = "resources/shaders/pp_bloom_composition.hlsl", .entry = "main_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(bloom_comp_r, DESC_RANGE(params::bloom_composition, Type::SRV_RANGE, params::BloomCompositionE::SOURCE_MAIN), DESC_RANGE(params::bloom_composition, Type::SRV_RANGE, params::BloomCompositionE::SOURCE_BLOOM_HALF), DESC_RANGE(params::bloom_composition, Type::SRV_RANGE, params::BloomCompositionE::SOURCE_BLOOM_QES), DESC_RANGE(params::bloom_composition, Type::UAV_RANGE, params::BloomCompositionE::OUTPUT), ); REGISTER(root_signatures::bloom_composition, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(bloom_comp_r, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetConstants(params::bloom_composition, params::BloomCompositionE::BLOOM_PROPERTIES)), }, .m_samplers = { { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_CLAMP}, { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_CLAMP} } }); REGISTER(pipelines::bloom_composition, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::bloom_composition, .m_root_signature_handle = root_signatures::bloom_composition, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = {wr::Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); // spatial reconstruction REGISTER(shaders::spatial_reconstruction, ShaderRegistry)({ .path = "resources/shaders/denoising_spatial_reconstruction.hlsl", .entry = "main", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(spatial_reconstruction_r, DESC_RANGE(params::spatial_reconstruction, Type::UAV_RANGE, params::SpatialReconstructionE::OUTPUT), DESC_RANGE(params::spatial_reconstruction, Type::SRV_RANGE, params::SpatialReconstructionE::REFLECTION_BUFFER), DESC_RANGE(params::spatial_reconstruction, Type::SRV_RANGE, params::SpatialReconstructionE::GBUFFERS) ); REGISTER(root_signatures::spatial_reconstruction, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(spatial_reconstruction_r, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetCBV(params::spatial_reconstruction, params::SpatialReconstructionE::CAMERA_PROPERTIES)) }, .m_samplers = { { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_BORDER }, { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_BORDER} } }); REGISTER(pipelines::spatial_reconstruction, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::spatial_reconstruction, .m_root_signature_handle = root_signatures::spatial_reconstruction, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(shaders::reflection_temporal_denoiser, ShaderRegistry)({ .path = "resources/shaders/denoising_reflections.hlsl", .entry = "temporal_denoiser_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::reflection_variance_estimator, ShaderRegistry)({ .path = "resources/shaders/denoising_reflections.hlsl", .entry = "variance_estimator_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); REGISTER(shaders::reflection_spatial_denoiser, ShaderRegistry)({ .path = "resources/shaders/denoising_reflections.hlsl", .entry = "spatial_denoiser_cs", .type = ShaderType::DIRECT_COMPUTE_SHADER, .defines = {} }); DESC_RANGE_ARRAY(reflection_denoiser_ranges, DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::INPUT), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::RAY_RAW), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::RAY_DIR), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::ALBEDO_ROUGHNESS), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::NORMAL_METALLIC), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::MOTION), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::LINEAR_DEPTH), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::WORLD_POS), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::IN_HISTORY), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::ACCUM), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::PREV_NORMAL), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::PREV_DEPTH), DESC_RANGE(params::reflection_denoiser, Type::SRV_RANGE, params::ReflectionDenoiserE::IN_MOMENTS), DESC_RANGE(params::reflection_denoiser, Type::UAV_RANGE, params::ReflectionDenoiserE::OUTPUT), DESC_RANGE(params::reflection_denoiser, Type::UAV_RANGE, params::ReflectionDenoiserE::OUT_HISTORY), DESC_RANGE(params::reflection_denoiser, Type::UAV_RANGE, params::ReflectionDenoiserE::OUT_MOMENTS) ); REGISTER(root_signatures::reflection_denoiser, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(reflection_denoiser_ranges, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetCBV(params::reflection_denoiser, params::ReflectionDenoiserE::CAMERA_PROPERTIES)), ROOT_PARAM(GetCBV(params::reflection_denoiser, params::ReflectionDenoiserE::DENOISER_SETTINGS)), ROOT_PARAM(GetCBV(params::reflection_denoiser, params::ReflectionDenoiserE::WAVELET_ITERATION)) }, .m_samplers = { { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_BORDER }, { TextureFilter::FILTER_LINEAR, TextureAddressMode::TAM_BORDER } } }); REGISTER(pipelines::reflection_temporal_denoiser, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::reflection_temporal_denoiser, .m_root_signature_handle = root_signatures::reflection_denoiser, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::reflection_variance_estimator, PipelineRegistry) < Vertex2D > ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::reflection_variance_estimator, .m_root_signature_handle = root_signatures::reflection_denoiser, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); REGISTER(pipelines::reflection_spatial_denoiser, PipelineRegistry) ({ .m_vertex_shader_handle = std::nullopt, .m_pixel_shader_handle = std::nullopt, .m_compute_shader_handle = shaders::reflection_spatial_denoiser, .m_root_signature_handle = root_signatures::reflection_denoiser, .m_dsv_format = Format::UNKNOWN, .m_rtv_formats = { Format::R16G16B16A16_FLOAT }, .m_num_rtv_formats = 1, .m_type = PipelineType::COMPUTE_PIPELINE, .m_cull_mode = CullMode::CULL_BACK, .m_depth_enabled = false, .m_counter_clockwise = true, .m_topology_type = TopologyType::TRIANGLE }); /* ### Hybrid Raytracing ### */ DESC_RANGE_ARRAY(rt_hybrid_ranges, DESC_RANGE(params::rt_hybrid, Type::UAV_RANGE, params::RTHybridE::OUTPUT), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::INDICES), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::LIGHTS), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::MATERIALS), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::OFFSETS), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::SKYBOX), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::PREF_ENV_MAP), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::BRDF_LUT), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::IRRADIANCE_MAP), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::TEXTURES), DESC_RANGE(params::rt_hybrid, Type::SRV_RANGE, params::RTHybridE::GBUFFERS), DESC_RANGE_H(D3D12_DESCRIPTOR_RANGE_TYPE::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 9, d3d12::settings::fallback_ptrs_offset), ); REGISTER(root_signatures::rt_hybrid_global, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(rt_hybrid_ranges, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetSRV(params::rt_hybrid, params::RTHybridE::ACCELERATION_STRUCTURE)), ROOT_PARAM(GetCBV(params::rt_hybrid, params::RTHybridE::CAMERA_PROPERTIES)), ROOT_PARAM(GetSRV(params::rt_hybrid, params::RTHybridE::VERTICES)), }, .m_samplers = { { TextureFilter::FILTER_ANISOTROPIC, TextureAddressMode::TAM_WRAP } }, .m_rtx = true }); /*### Raytraced Ambient Oclusion ### */ REGISTER(shaders::rt_ao_lib, ShaderRegistry)({ .path = "resources/shaders/dxr_ambient_occlusion.hlsl", .entry = "AORaygenEntry", .type = ShaderType::LIBRARY_SHADER, .defines = {} }); DESC_RANGE_ARRAY(rt_ao_ranges, DESC_RANGE(params::rt_ao, Type::UAV_RANGE, params::RTAOE::OUTPUT), DESC_RANGE(params::rt_ao, Type::SRV_RANGE, params::RTAOE::GBUFFERS), DESC_RANGE_H(D3D12_DESCRIPTOR_RANGE_TYPE::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 9, d3d12::settings::fallback_ptrs_offset), ); REGISTER(root_signatures::rt_ao_global, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(rt_ao_ranges, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetSRV(params::rt_ao, params::RTAOE::ACCELERATION_STRUCTURE)), ROOT_PARAM(GetCBV(params::rt_ao, params::RTAOE::CAMERA_PROPERTIES)), }, .m_samplers = {}, .m_rtx = true }); StateObjectDescription::LibraryDesc rt_ao_so_library = []() { StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::rt_ao_lib; lib.exports.push_back(L"AORaygenEntry"); lib.exports.push_back(L"MissEntry"); return lib; }(); REGISTER(state_objects::rt_ao_state_opbject, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, .library_desc = rt_ao_so_library, .max_payload_size = (sizeof(float) * 2), .max_attributes_size = sizeof(float) * 4, .max_recursion_depth = 1, .global_root_signature = root_signatures::rt_ao_global, .local_root_signatures = {}, }); /* ### Path Tracer ### */ REGISTER(shaders::path_tracer_lib, ShaderRegistry)({ .path = "resources/shaders/dxr_pathtracer_main.hlsl", .entry = "RaygenEntry", .type = ShaderType::LIBRARY_SHADER, .defines = {} }); DESC_RANGE_ARRAY(path_tracer_ranges, DESC_RANGE(params::path_tracing, Type::UAV_RANGE, params::PathTracingE::OUTPUT), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::INDICES), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::LIGHTS), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::MATERIALS), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::OFFSETS), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::SKYBOX), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::PREF_ENV_MAP), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::BRDF_LUT), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::IRRADIANCE_MAP), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::TEXTURES), DESC_RANGE(params::path_tracing, Type::SRV_RANGE, params::PathTracingE::GBUFFERS), DESC_RANGE_H(D3D12_DESCRIPTOR_RANGE_TYPE::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 9, d3d12::settings::fallback_ptrs_offset), ); REGISTER(root_signatures::path_tracing_global, RootSignatureRegistry)({ .m_parameters = { ROOT_PARAM_DESC_TABLE(path_tracer_ranges, D3D12_SHADER_VISIBILITY_ALL), ROOT_PARAM(GetSRV(params::path_tracing, params::PathTracingE::ACCELERATION_STRUCTURE)), ROOT_PARAM(GetCBV(params::path_tracing, params::PathTracingE::CAMERA_PROPERTIES)), ROOT_PARAM(GetSRV(params::path_tracing, params::PathTracingE::VERTICES)), }, .m_samplers = { { TextureFilter::FILTER_ANISOTROPIC, TextureAddressMode::TAM_WRAP } }, .m_rtx = true }); StateObjectDescription::LibraryDesc path_tracer_so_library = []() { StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::path_tracer_lib; lib.exports.push_back(L"RaygenEntry"); lib.exports.push_back(L"ReflectionHit"); lib.exports.push_back(L"ReflectionMiss"); lib.exports.push_back(L"ShadowClosestHitEntry"); lib.exports.push_back(L"ShadowMissEntry"); lib.m_hit_groups.push_back({ L"ReflectionHitGroup", L"ReflectionHit" }); lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); return lib; }(); REGISTER(state_objects::path_tracer_state_object, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, .library_desc = path_tracer_so_library, .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2), .max_attributes_size = sizeof(float) * 4, .max_recursion_depth = 6, .global_root_signature = root_signatures::path_tracing_global, .local_root_signatures = {}, }); /* ### Shadow Raytracing ### */ REGISTER(shaders::rt_shadow_lib, ShaderRegistry)({ .path = "resources/shaders/dxr_shadow_main.hlsl", .entry = "ShadowRaygenEntry", .type = ShaderType::LIBRARY_SHADER, .defines = {} }); StateObjectDescription::LibraryDesc rt_shadow_so_library = []() { StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::rt_shadow_lib; lib.exports.push_back(L"ShadowRaygenEntry"); lib.exports.push_back(L"ShadowClosestHitEntry"); lib.exports.push_back(L"ShadowMissEntry"); lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); return lib; }(); REGISTER(state_objects::rt_shadow_state_object, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, .library_desc = rt_shadow_so_library, .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 3), .max_attributes_size = sizeof(float) * 4, .max_recursion_depth = 1, .global_root_signature = root_signatures::rt_hybrid_global, .local_root_signatures = {} }); /* ### Reflection Raytracing ### */ REGISTER(shaders::rt_reflection_lib, ShaderRegistry)({ .path = "resources/shaders/dxr_reflection_main.hlsl", .entry = "ReflectionRaygenEntry", .type = ShaderType::LIBRARY_SHADER, .defines = {} }); StateObjectDescription::LibraryDesc rt_reflection_so_library = []() { StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::rt_reflection_lib; lib.exports.push_back(L"ReflectionRaygenEntry"); lib.exports.push_back(L"ReflectionHit"); lib.exports.push_back(L"ReflectionMiss"); lib.exports.push_back(L"ShadowClosestHitEntry"); lib.exports.push_back(L"ShadowMissEntry"); lib.m_hit_groups.push_back({ L"ReflectionHitGroup", L"ReflectionHit" }); lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); return lib; }(); REGISTER(state_objects::rt_reflection_state_object, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, .library_desc = rt_reflection_so_library, .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 3), .max_attributes_size = sizeof(float) * 4, .max_recursion_depth = 3, .global_root_signature = root_signatures::rt_hybrid_global, .local_root_signatures = {} }); } /* wr */ ================================================ FILE: src/engine_registry.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include "registry.hpp" #include "d3d12/d3dx12.hpp" #include "d3d12/d3d12_settings.hpp" #define COMPILATION_EVAL(e) (std::integral_constant::value) namespace wr { namespace rs_layout { enum class Type { SRV, SRV_RANGE, CBV_OR_CONST, CBV_RANGE, UAV, UAV_RANGE, }; struct Entry { int name; int size; Type type; }; template constexpr unsigned int GetStart(const T data, const E name) { Type type = Type::CBV_OR_CONST; unsigned int start = 0; // Find Type for (std::size_t i = 0; i < data.size(); i++) { auto entry = data[i]; if (static_cast(entry.name) == name) { type = entry.type; } } // Find Start for (std::size_t i = 0; i < data.size(); i++) { auto entry = data[i]; if (static_cast(entry.name) == name) { break; } else if (entry.type == type) { start += entry.size; } } return start; } template constexpr unsigned int GetHeapLoc(const T data, const E name) { unsigned int start = 0; // Find Start for (std::size_t i = 0; i < data.size(); i++) { auto entry = data[i]; if (static_cast(entry.name) == name) { break; } else if (entry.type == Type::CBV_RANGE || entry.type == Type::SRV_RANGE || entry.type == Type::UAV_RANGE) { start += entry.size; } } return start; } template constexpr unsigned int GetSize(const T data, const E name) { for (std::size_t i = 0; i < data.size(); i++) { auto entry = data[i]; if (static_cast(entry.name) == name) { return entry.size; } } return 0; } template constexpr CD3DX12_DESCRIPTOR_RANGE GetRange(const T data, const Type type, const E name) { unsigned int start = 0; unsigned int size = 0; // Find its range equivelant or visa versa Type other_type = Type::SRV; switch (type) { case Type::SRV: other_type = Type::SRV_RANGE; break; case Type::SRV_RANGE: other_type = Type::SRV; break; case Type::CBV_OR_CONST: other_type = Type::CBV_RANGE; break; case Type::CBV_RANGE: other_type = Type::CBV_OR_CONST; break; case Type::UAV: other_type = Type::UAV_RANGE; break; case Type::UAV_RANGE: other_type = Type::UAV; break; } // Find Start & Size for (std::size_t i = 0; i < data.size(); i++) { auto entry = data[i]; if (static_cast(entry.name) == name) { size = entry.size; break; } else if (entry.type == type || entry.type == other_type) { start += entry.size; } } D3D12_DESCRIPTOR_RANGE_TYPE d3d12_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; switch (type) { case Type::SRV_RANGE: d3d12_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; break; case Type::UAV_RANGE: d3d12_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_UAV; break; case Type::CBV_RANGE: d3d12_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_CBV; break; default: LOGW("Unknown range type"); break; } CD3DX12_DESCRIPTOR_RANGE retval; retval.Init(d3d12_range_type, size, start); return retval; } template constexpr CD3DX12_ROOT_PARAMETER GetCBV(const T data, const E name, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { unsigned int start = 0; // Find Start & Size for (std::size_t i = 0; i < data.size(); i++) { auto entry = data[i]; if (static_cast(entry.name) == name) { break; } else if (entry.type == Type::CBV_OR_CONST || entry.type == Type::CBV_RANGE) { start += entry.size; } } CD3DX12_ROOT_PARAMETER retval; retval.InitAsConstantBufferView(start, 0, visibility); return retval; } template constexpr CD3DX12_ROOT_PARAMETER GetSRV(const T data, const E name, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { unsigned int start = 0; // Find Start & Size for (std::size_t i = 0; i < data.size(); i++) { auto entry = data[i]; if (static_cast(entry.name) == name) { break; } else if (entry.type == Type::SRV || entry.type == Type::SRV_RANGE) { start += entry.size; } } CD3DX12_ROOT_PARAMETER retval; retval.InitAsShaderResourceView(start, 0, visibility); return retval; } template constexpr CD3DX12_ROOT_PARAMETER GetConstants(const T data, const E name, D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL) { unsigned int start = 0; unsigned int size = 0; // Find Start for (std::size_t i = 0; i < data.size(); i++) { auto entry = data[i]; if (static_cast(entry.name) == name) { size = entry.size; break; } else if (entry.type == Type::CBV_OR_CONST || entry.type == Type::CBV_RANGE) { start += entry.size; } } CD3DX12_ROOT_PARAMETER retval; retval.InitAsConstants(size, start, 0, visibility); return retval; } } /* rs_layout */ namespace params { enum class BRDF_LutE { OUTPUT, }; constexpr std::array brdf_lut = { rs_layout::Entry{(int)BRDF_LutE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, }; enum class DeferredMainE { CAMERA_PROPERTIES, OBJECT_PROPERTIES, ALBEDO, NORMAL, ROUGHNESS, METALLIC, AMBIENT_OCCLUSION, EMISSIVE, MATERIAL_PROPERTIES, }; constexpr std::array deferred_main = { rs_layout::Entry{(int)DeferredMainE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)DeferredMainE::OBJECT_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)DeferredMainE::ALBEDO, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredMainE::NORMAL, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredMainE::ROUGHNESS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredMainE::METALLIC, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredMainE::AMBIENT_OCCLUSION, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredMainE::EMISSIVE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredMainE::MATERIAL_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, }; enum class SVGFDenoiserE { INPUT, MOTION, NORMAL, DEPTH, IN_HIST_LENGTH, PREV_INPUT, PREV_MOMENTS, PREV_NORMAL, PREV_DEPTH, OUT_COLOR, OUT_MOMENTS, OUT_HIST_LENGTH, SVGF_PROPERTIES, PING_PONG_UAV, PING_PONG_SRV, OUTPUT_SRV, }; constexpr std::array svgf_denoiser = { rs_layout::Entry{(int)SVGFDenoiserE::INPUT, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::MOTION, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::NORMAL, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::DEPTH, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::IN_HIST_LENGTH, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::PREV_INPUT, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::PREV_MOMENTS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::PREV_NORMAL, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::PREV_DEPTH, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::OUT_COLOR, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::OUT_MOMENTS, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::OUT_HIST_LENGTH, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::SVGF_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)SVGFDenoiserE::PING_PONG_UAV, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::PING_PONG_SRV, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SVGFDenoiserE::OUTPUT_SRV, 1, rs_layout::Type::SRV_RANGE}, }; enum class DeferredCompositionE { CAMERA_PROPERTIES, GBUFFER_ALBEDO_ROUGHNESS, GBUFFER_NORMAL_METALLIC, GBUFFER_EMISSIVE_AO, GBUFFER_DEPTH, LIGHT_BUFFER, SKY_BOX, IRRADIANCE_MAP, PREF_ENV_MAP, BRDF_LUT, BUFFER_REFLECTION, BUFFER_SHADOW, BUFFER_SCREEN_SPACE_IRRADIANCE, BUFFER_AO, OUTPUT, }; constexpr std::array deferred_composition = { rs_layout::Entry{(int)DeferredCompositionE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)DeferredCompositionE::GBUFFER_ALBEDO_ROUGHNESS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::GBUFFER_NORMAL_METALLIC, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::GBUFFER_EMISSIVE_AO, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::GBUFFER_DEPTH, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::LIGHT_BUFFER, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::SKY_BOX, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::IRRADIANCE_MAP, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::PREF_ENV_MAP, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::BRDF_LUT, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::BUFFER_REFLECTION, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::BUFFER_SHADOW, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::BUFFER_SCREEN_SPACE_IRRADIANCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::BUFFER_AO, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DeferredCompositionE::OUTPUT, 1, rs_layout::Type::UAV_RANGE} }; enum class MipMappingE { SOURCE, DEST, CBUFFER, }; constexpr std::array mip_mapping = { rs_layout::Entry{(int)MipMappingE::SOURCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)MipMappingE::DEST, 4, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)MipMappingE::CBUFFER, 6, rs_layout::Type::CBV_OR_CONST}, }; enum class CubemapConversionE { EQUIRECTANGULAR_TEXTURE, IDX, CAMERA_PROPERTIES, }; constexpr std::array cubemap_conversion = { rs_layout::Entry{(int)CubemapConversionE::EQUIRECTANGULAR_TEXTURE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)CubemapConversionE::IDX, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)CubemapConversionE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, }; enum class CubemapConvolutionE { ENVIRONMENT_CUBEMAP, IDX, CAMERA_PROPERTIES, }; constexpr std::array cubemap_convolution = { rs_layout::Entry{(int)CubemapConvolutionE::ENVIRONMENT_CUBEMAP, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)CubemapConvolutionE::IDX, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)CubemapConvolutionE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, }; enum class CubemapPrefilteringE { SOURCE, DEST, CBUFFER, }; constexpr std::array cubemap_prefiltering = { rs_layout::Entry{(int)CubemapPrefilteringE::SOURCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)CubemapPrefilteringE::DEST, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)CubemapPrefilteringE::CBUFFER, 6, rs_layout::Type::CBV_OR_CONST}, }; enum class PostProcessingE { SOURCE, DEST, HDR_SUPPORT, }; constexpr std::array post_processing = { rs_layout::Entry{(int)PostProcessingE::SOURCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PostProcessingE::DEST, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)PostProcessingE::HDR_SUPPORT, 2, rs_layout::Type::CBV_OR_CONST}, }; enum class AccumulationE { SOURCE, DEST, FRAME_IDX, }; constexpr std::array accumulation = { rs_layout::Entry{(int)AccumulationE::SOURCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)AccumulationE::DEST, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)AccumulationE::FRAME_IDX, 2, rs_layout::Type::CBV_OR_CONST}, }; enum class FullRaytracingE { CAMERA_PROPERTIES, ACCELERATION_STRUCTURE, OUTPUT, INDICES, VERTICES, LIGHTS, MATERIALS, OFFSETS, SKYBOX, BRDF_LUT, IRRADIANCE_MAP, TEXTURES, FALLBACK_PTRS }; constexpr std::array full_raytracing = { rs_layout::Entry{(int)FullRaytracingE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)FullRaytracingE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)FullRaytracingE::ACCELERATION_STRUCTURE, 1, rs_layout::Type::SRV}, rs_layout::Entry{(int)FullRaytracingE::INDICES, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)FullRaytracingE::LIGHTS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)FullRaytracingE::VERTICES, 1, rs_layout::Type::SRV}, rs_layout::Entry{(int)FullRaytracingE::MATERIALS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)FullRaytracingE::OFFSETS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)FullRaytracingE::SKYBOX, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)FullRaytracingE::BRDF_LUT, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)FullRaytracingE::IRRADIANCE_MAP, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)FullRaytracingE::TEXTURES, d3d12::settings::num_max_rt_textures, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)FullRaytracingE::FALLBACK_PTRS, 5, rs_layout::Type::SRV_RANGE}, }; enum class RTHybridE { CAMERA_PROPERTIES, ACCELERATION_STRUCTURE, OUTPUT, INDICES, VERTICES, LIGHTS, MATERIALS, OFFSETS, SKYBOX, PREF_ENV_MAP, BRDF_LUT, IRRADIANCE_MAP, TEXTURES, GBUFFERS, FALLBACK_PTRS }; constexpr std::array rt_hybrid = { rs_layout::Entry{(int)RTHybridE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)RTHybridE::OUTPUT, 3, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)RTHybridE::ACCELERATION_STRUCTURE, 1, rs_layout::Type::SRV}, rs_layout::Entry{(int)RTHybridE::INDICES, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::LIGHTS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::VERTICES, 1, rs_layout::Type::SRV}, rs_layout::Entry{(int)RTHybridE::MATERIALS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::OFFSETS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::SKYBOX, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::PREF_ENV_MAP, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::BRDF_LUT, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::IRRADIANCE_MAP, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::TEXTURES, d3d12::settings::num_max_rt_textures, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::GBUFFERS, 3, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTHybridE::FALLBACK_PTRS, 9, rs_layout::Type::SRV_RANGE}, }; enum class SpatialReconstructionE { CAMERA_PROPERTIES, OUTPUT, REFLECTION_BUFFER, GBUFFERS }; constexpr std::array spatial_reconstruction = { rs_layout::Entry{(int)SpatialReconstructionE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)SpatialReconstructionE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)SpatialReconstructionE::REFLECTION_BUFFER, 2, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)SpatialReconstructionE::GBUFFERS, 3, rs_layout::Type::SRV_RANGE} }; enum class ReflectionDenoiserE { INPUT, RAY_RAW, RAY_DIR, ALBEDO_ROUGHNESS, NORMAL_METALLIC, MOTION, LINEAR_DEPTH, WORLD_POS, IN_HISTORY, ACCUM, PREV_NORMAL, PREV_DEPTH, IN_MOMENTS, OUTPUT, OUT_HISTORY, OUT_MOMENTS, CAMERA_PROPERTIES, DENOISER_SETTINGS, WAVELET_ITERATION, }; constexpr std::array reflection_denoiser = { rs_layout::Entry{(int)ReflectionDenoiserE::INPUT, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::RAY_RAW, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::RAY_DIR, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::ALBEDO_ROUGHNESS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::NORMAL_METALLIC, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::MOTION, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::LINEAR_DEPTH, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::WORLD_POS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::IN_HISTORY, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::ACCUM, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::PREV_NORMAL, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::PREV_DEPTH, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::IN_MOMENTS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::OUT_HISTORY, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::OUT_MOMENTS, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)ReflectionDenoiserE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)ReflectionDenoiserE::DENOISER_SETTINGS, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)ReflectionDenoiserE::WAVELET_ITERATION, 1, rs_layout::Type::CBV_OR_CONST}, }; enum class RTAOE { CAMERA_PROPERTIES, ACCELERATION_STRUCTURE, OUTPUT, GBUFFERS, FALLBACK_PTRS }; constexpr std::array rt_ao = { rs_layout::Entry{(int)RTAOE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_layout::Entry{(int)RTAOE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)RTAOE::ACCELERATION_STRUCTURE, 1, rs_layout::Type::SRV}, rs_layout::Entry{(int)RTAOE::GBUFFERS, 2, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)RTAOE::FALLBACK_PTRS, 9, rs_layout::Type::SRV_RANGE}, }; enum class PathTracingE { CAMERA_PROPERTIES, ACCELERATION_STRUCTURE, OUTPUT, INDICES, VERTICES, LIGHTS, MATERIALS, OFFSETS, SKYBOX, PREF_ENV_MAP, BRDF_LUT, IRRADIANCE_MAP, TEXTURES, GBUFFERS, FALLBACK_PTRS }; constexpr std::array path_tracing = { rs_layout::Entry{(int)PathTracingE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, rs_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. rs_layout::Entry{(int)PathTracingE::ACCELERATION_STRUCTURE, 1, rs_layout::Type::SRV}, rs_layout::Entry{(int)PathTracingE::INDICES, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::LIGHTS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::VERTICES, 1, rs_layout::Type::SRV}, rs_layout::Entry{(int)PathTracingE::MATERIALS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::OFFSETS, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::SKYBOX, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::PREF_ENV_MAP, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::BRDF_LUT, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::IRRADIANCE_MAP, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::TEXTURES, d3d12::settings::num_max_rt_textures, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::GBUFFERS, 4, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)PathTracingE::FALLBACK_PTRS, 9, rs_layout::Type::SRV_RANGE}, }; enum class DoFCoCE { CAMERA_PROPERTIES, GDEPTH, OUTPUT }; constexpr std::array dof_coc = { rs_layout::Entry{(int)DoFCoCE::GDEPTH, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFCoCE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)DoFCoCE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST} }; enum class DoFNearMaskE { INPUT, OUTPUT }; constexpr std::array dof_near_mask = { rs_layout::Entry{(int)DoFNearMaskE::INPUT, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFNearMaskE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, }; enum class DownScaleE { SOURCE, OUTPUT_NEAR, OUTPUT_FAR, COC, }; constexpr std::array down_scale = { rs_layout::Entry{(int)DownScaleE::SOURCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DownScaleE::OUTPUT_NEAR, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)DownScaleE::OUTPUT_FAR, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)DownScaleE::COC, 1, rs_layout::Type::SRV_RANGE}, }; enum class DoFDilateE { SOURCE, OUTPUT }; constexpr std::array dof_dilate = { rs_layout::Entry{(int)DoFDilateE::SOURCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFDilateE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, }; enum class DoFBokehE { CAMERA_PROPERTIES, SOURCE_NEAR, SOURCE_FAR, OUTPUT_NEAR, OUTPUT_FAR, COC }; constexpr std::array dof_bokeh = { rs_layout::Entry{(int)DoFBokehE::SOURCE_NEAR, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFBokehE::SOURCE_FAR, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFBokehE::OUTPUT_NEAR, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)DoFBokehE::OUTPUT_FAR, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)DoFBokehE::COC, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFBokehE::CAMERA_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, }; enum class DoFBokehPostFilterE { SOURCE_NEAR, SOURCE_FAR, OUTPUT_NEAR, OUTPUT_FAR }; constexpr std::array dof_bokeh_post_filter = { rs_layout::Entry{(int)DoFBokehPostFilterE::SOURCE_NEAR, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFBokehPostFilterE::SOURCE_FAR, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFBokehPostFilterE::OUTPUT_NEAR, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)DoFBokehPostFilterE::OUTPUT_FAR, 1, rs_layout::Type::UAV_RANGE} }; enum class DoFCompositionE { SOURCE, OUTPUT, BOKEH_NEAR, BOKEH_FAR, COC }; constexpr std::array dof_composition = { rs_layout::Entry{(int)DoFCompositionE::SOURCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFCompositionE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)DoFCompositionE::BOKEH_NEAR, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFCompositionE::BOKEH_FAR, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)DoFCompositionE::COC, 1, rs_layout::Type::SRV_RANGE}, }; enum class BloomExtractBrightE { SOURCE, G_EMISSIVE, G_DEPTH, OUTPUT_BRIGHT }; constexpr std::array bloom_extract_bright = { rs_layout::Entry{(int)BloomExtractBrightE::SOURCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomExtractBrightE::G_EMISSIVE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomExtractBrightE::G_DEPTH, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomExtractBrightE::OUTPUT_BRIGHT, 1, rs_layout::Type::UAV_RANGE}, }; enum class BloomBlurE { SOURCE, OUTPUT, BLUR_DIRECTION }; constexpr std::array bloom_blur = { rs_layout::Entry{(int)BloomBlurE::SOURCE, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomBlurE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)BloomBlurE::BLUR_DIRECTION, 1, rs_layout::Type::CBV_OR_CONST}, }; enum class BloomBlurHorizontalE { BLOOM_PROPERTIES, SOURCE_MAIN, OUTPUT, OUTPUT_QES }; constexpr std::array bloom_blur_horizontal = { rs_layout::Entry{(int)BloomBlurHorizontalE::SOURCE_MAIN, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomBlurHorizontalE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)BloomBlurHorizontalE::OUTPUT_QES, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)BloomBlurHorizontalE::BLOOM_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, }; enum class BloomBlurVerticalE { BLOOM_PROPERTIES, SOURCE_MAIN, SOURCE_QES, OUTPUT, OUTPUT_QES }; constexpr std::array bloom_blur_vertical = { rs_layout::Entry{(int)BloomBlurVerticalE::SOURCE_MAIN, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomBlurVerticalE::SOURCE_QES, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomBlurVerticalE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)BloomBlurVerticalE::OUTPUT_QES, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)BloomBlurVerticalE::BLOOM_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, }; enum class BloomCompositionE { BLOOM_PROPERTIES, SOURCE_MAIN, SOURCE_BLOOM_HALF, SOURCE_BLOOM_QES, OUTPUT }; constexpr std::array bloom_composition = { rs_layout::Entry{(int)BloomCompositionE::SOURCE_MAIN, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomCompositionE::SOURCE_BLOOM_HALF, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomCompositionE::SOURCE_BLOOM_QES, 1, rs_layout::Type::SRV_RANGE}, rs_layout::Entry{(int)BloomCompositionE::OUTPUT, 1, rs_layout::Type::UAV_RANGE}, rs_layout::Entry{(int)BloomCompositionE::BLOOM_PROPERTIES, 1, rs_layout::Type::CBV_OR_CONST}, }; } /* srv */ struct root_signatures { WISPRENDERER_EXPORT static RegistryHandle brdf_lut; WISPRENDERER_EXPORT static RegistryHandle basic; WISPRENDERER_EXPORT static RegistryHandle svgf_denoiser; WISPRENDERER_EXPORT static RegistryHandle deferred_composition; WISPRENDERER_EXPORT static RegistryHandle rt_test_global; WISPRENDERER_EXPORT static RegistryHandle mip_mapping; WISPRENDERER_EXPORT static RegistryHandle rt_hybrid_global; WISPRENDERER_EXPORT static RegistryHandle rt_ao_global; WISPRENDERER_EXPORT static RegistryHandle path_tracing_global; WISPRENDERER_EXPORT static RegistryHandle cubemap_conversion; WISPRENDERER_EXPORT static RegistryHandle cubemap_convolution; WISPRENDERER_EXPORT static RegistryHandle cubemap_prefiltering; WISPRENDERER_EXPORT static RegistryHandle post_processing; WISPRENDERER_EXPORT static RegistryHandle accumulation; WISPRENDERER_EXPORT static RegistryHandle dof_coc; WISPRENDERER_EXPORT static RegistryHandle dof_near_mask; WISPRENDERER_EXPORT static RegistryHandle down_scale; WISPRENDERER_EXPORT static RegistryHandle dof_dilate; WISPRENDERER_EXPORT static RegistryHandle dof_bokeh; WISPRENDERER_EXPORT static RegistryHandle dof_bokeh_post_filter; WISPRENDERER_EXPORT static RegistryHandle dof_composition; WISPRENDERER_EXPORT static RegistryHandle bloom_extract_bright; WISPRENDERER_EXPORT static RegistryHandle bloom_blur; WISPRENDERER_EXPORT static RegistryHandle bloom_blur_horizontal; WISPRENDERER_EXPORT static RegistryHandle bloom_blur_vertical; WISPRENDERER_EXPORT static RegistryHandle bloom_composition; WISPRENDERER_EXPORT static RegistryHandle spatial_reconstruction; WISPRENDERER_EXPORT static RegistryHandle reflection_denoiser; }; struct shaders { WISPRENDERER_EXPORT static RegistryHandle brdf_lut_cs; WISPRENDERER_EXPORT static RegistryHandle basic_deferred_vs; WISPRENDERER_EXPORT static RegistryHandle basic_deferred_ps; WISPRENDERER_EXPORT static RegistryHandle basic_hybrid_vs; WISPRENDERER_EXPORT static RegistryHandle basic_hybrid_ps; WISPRENDERER_EXPORT static RegistryHandle fullscreen_quad_vs; WISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_reprojection_cs; WISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_filter_moments_cs; WISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_wavelet_filter_cs; WISPRENDERER_EXPORT static RegistryHandle deferred_composition_cs; WISPRENDERER_EXPORT static RegistryHandle rt_lib; WISPRENDERER_EXPORT static RegistryHandle rt_ao_lib; WISPRENDERER_EXPORT static RegistryHandle path_tracer_lib; WISPRENDERER_EXPORT static RegistryHandle rt_shadow_lib; WISPRENDERER_EXPORT static RegistryHandle rt_reflection_lib; WISPRENDERER_EXPORT static RegistryHandle mip_mapping_cs; WISPRENDERER_EXPORT static RegistryHandle equirect_to_cubemap_vs; WISPRENDERER_EXPORT static RegistryHandle equirect_to_cubemap_ps; WISPRENDERER_EXPORT static RegistryHandle cubemap_convolution_ps; WISPRENDERER_EXPORT static RegistryHandle cubemap_prefiltering_cs; WISPRENDERER_EXPORT static RegistryHandle post_processing; WISPRENDERER_EXPORT static RegistryHandle accumulation; WISPRENDERER_EXPORT static RegistryHandle dof_coc; WISPRENDERER_EXPORT static RegistryHandle dof_near_mask; WISPRENDERER_EXPORT static RegistryHandle down_scale; WISPRENDERER_EXPORT static RegistryHandle dof_dilate; WISPRENDERER_EXPORT static RegistryHandle dof_bokeh; WISPRENDERER_EXPORT static RegistryHandle dof_bokeh_post_filter; WISPRENDERER_EXPORT static RegistryHandle dof_composition; WISPRENDERER_EXPORT static RegistryHandle bloom_extract_bright; WISPRENDERER_EXPORT static RegistryHandle bloom_blur; WISPRENDERER_EXPORT static RegistryHandle bloom_blur_horizontal; WISPRENDERER_EXPORT static RegistryHandle bloom_blur_vertical; WISPRENDERER_EXPORT static RegistryHandle bloom_composition; WISPRENDERER_EXPORT static RegistryHandle spatial_reconstruction; WISPRENDERER_EXPORT static RegistryHandle reflection_temporal_denoiser; WISPRENDERER_EXPORT static RegistryHandle reflection_variance_estimator; WISPRENDERER_EXPORT static RegistryHandle reflection_spatial_denoiser; }; struct pipelines { WISPRENDERER_EXPORT static RegistryHandle brdf_lut_precalculation; WISPRENDERER_EXPORT static RegistryHandle basic_deferred; WISPRENDERER_EXPORT static RegistryHandle basic_hybrid; WISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_reprojection; WISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_filter_moments; WISPRENDERER_EXPORT static RegistryHandle svgf_denoiser_wavelet_filter; WISPRENDERER_EXPORT static RegistryHandle deferred_composition; WISPRENDERER_EXPORT static RegistryHandle mip_mapping; WISPRENDERER_EXPORT static RegistryHandle equirect_to_cubemap; WISPRENDERER_EXPORT static RegistryHandle cubemap_convolution; WISPRENDERER_EXPORT static RegistryHandle cubemap_prefiltering; WISPRENDERER_EXPORT static RegistryHandle post_processing; WISPRENDERER_EXPORT static RegistryHandle accumulation; WISPRENDERER_EXPORT static RegistryHandle dof_coc; WISPRENDERER_EXPORT static RegistryHandle dof_near_mask; WISPRENDERER_EXPORT static RegistryHandle down_scale; WISPRENDERER_EXPORT static RegistryHandle dof_dilate; WISPRENDERER_EXPORT static RegistryHandle dof_bokeh; WISPRENDERER_EXPORT static RegistryHandle dof_bokeh_post_filter; WISPRENDERER_EXPORT static RegistryHandle dof_composition; WISPRENDERER_EXPORT static RegistryHandle bloom_extract_bright; WISPRENDERER_EXPORT static RegistryHandle bloom_blur; WISPRENDERER_EXPORT static RegistryHandle bloom_blur_horizontal; WISPRENDERER_EXPORT static RegistryHandle bloom_blur_vertical; WISPRENDERER_EXPORT static RegistryHandle bloom_composition; WISPRENDERER_EXPORT static RegistryHandle spatial_reconstruction; WISPRENDERER_EXPORT static RegistryHandle reflection_temporal_denoiser; WISPRENDERER_EXPORT static RegistryHandle reflection_variance_estimator; WISPRENDERER_EXPORT static RegistryHandle reflection_spatial_denoiser; }; struct state_objects { WISPRENDERER_EXPORT static RegistryHandle state_object; WISPRENDERER_EXPORT static RegistryHandle rt_ao_state_opbject; WISPRENDERER_EXPORT static RegistryHandle path_tracing_state_object; WISPRENDERER_EXPORT static RegistryHandle path_tracer_state_object; WISPRENDERER_EXPORT static RegistryHandle rt_shadow_state_object; WISPRENDERER_EXPORT static RegistryHandle rt_reflection_state_object; }; } /* wr */ ================================================ FILE: src/entry.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #define WISP_ENTRY(func) \ int main() \ { \ return func(); \ } \ int CALLBACK WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPSTR, _In_ int) \ { \ return main(); \ } ================================================ FILE: src/frame_graph/frame_graph.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #include "../util/thread_pool.hpp" #include "../util/delegate.hpp" #include "../renderer.hpp" #include "../platform_independend_structs.hpp" #include "../settings.hpp" #include "../d3d12/d3d12_settings.hpp" #include "../structs.hpp" #include "../wisprenderer_export.hpp" #ifndef _DEBUG #define FG_MAX_PERFORMANCE #endif template std::vector> FG_DEPS() { return { (typeid(Ts))... }; } namespace wr { enum class RenderTaskType { DIRECT, COMPUTE, COPY }; enum class CPUTextureType { PIXEL_DATA, DEPTH_DATA }; struct CPUTextures { std::optional pixel_data = std::nullopt; std::optional depth_data = std::nullopt; }; //! Typedef for the render task handle. using RenderTaskHandle = std::uint32_t; // Forward declarations. class FrameGraph; /*! Structure that describes a render task */ /*! All non default initialized member variables should be fully initialized to prevent undifined behaviour. */ struct RenderTaskDesc { // Typedef the function pointer types to keep the code readable. using setup_func_t = util::Delegate; using execute_func_t = util::Delegate; using destroy_func_t = util::Delegate; /*! The type of the render task.*/ RenderTaskType m_type = RenderTaskType::DIRECT; /*! The function pointers for the task.*/ setup_func_t m_setup_func; execute_func_t m_execute_func; destroy_func_t m_destroy_func; /*! The properties for the render target this task renders to. If this is `std::nullopt` no render target will be created. */ std::optional m_properties; bool m_allow_multithreading = true; }; //! Frame Graph /*! The Frame Graph is responsible for managing all tasks the renderer should perform. The idea is you can add tasks to the scene graph and when you call `RenderSystem::Render` it will run the tasks added. It will not just run tasks but will also assign command lists and render targets to the tasks. The Frame Graph is also capable of mulithreaded execution. It will split the command lists that are allowed to be multithreaded on X amount of threads specified in `settings.hpp` */ class FrameGraph { // Obtain the type definitions from `RenderTaskDesc` to keep the code readable. using setup_func_t = RenderTaskDesc::setup_func_t; using execute_func_t = RenderTaskDesc::execute_func_t; using destroy_func_t = RenderTaskDesc::destroy_func_t; public: //! Constructor. /*! This constructor is able to reserve space for render tasks. This works by calling `std::vector::reserve`. \param num_reserved_tasks Amount of tasks we should reserve space for. */ FrameGraph(std::size_t num_reserved_tasks = 1) : m_render_system(nullptr), m_num_tasks(0), m_thread_pool(new util::ThreadPool(settings::num_frame_graph_threads)), m_uid(GetFreeUID()) { // lambda to simplify reserving space. auto reserve = [num_reserved_tasks](auto v) { v.reserve(num_reserved_tasks); }; // Reserve space for all vectors. reserve(m_setup_funcs); reserve(m_execute_funcs); reserve(m_destroy_funcs); reserve(m_cmd_lists); reserve(m_render_targets); reserve(m_data); reserve(m_data_type_info); #ifndef FG_MAX_PERFORMANCE reserve(m_dependencies); reserve(m_names); #endif reserve(m_types); reserve(m_rt_properties); m_settings = decltype(m_settings)(num_reserved_tasks, std::nullopt); // Resizing so I can initialize it with null since this is an optional value. m_futures.resize(num_reserved_tasks); // std::thread doesn't allow me to reserve memory for the vector. Hence I'm resizing. } //! Destructor /*! This destructor destroys all the task data and the thread pool. If you want to reuse the frame graph I recommend calling `FrameGraph::Destroy` */ ~FrameGraph() { delete m_thread_pool; Destroy(); } FrameGraph(const FrameGraph&) = delete; FrameGraph(FrameGraph&&) = delete; FrameGraph& operator=(const FrameGraph&) = delete; FrameGraph& operator=(FrameGraph&&) = delete; //! Setup the render tasks /*! Calls all setup function pointers and obtains the required render targets and command lists. It is recommended to try to avoid calling this during runtime because it can cause stalls if the setup functions are expensive. \param render_system The render system we want to use for rendering. */ inline void Setup(RenderSystem& render_system) { bool is_valid = Validate(); if (!is_valid) { LOGE("Framegraph validation failed. Aborting setup."); return; } // Resize these vectors since we know the end size already. m_cmd_lists.resize(m_num_tasks); m_should_execute.resize(m_num_tasks, true); // All tasks should execute by default. m_render_targets.resize(m_num_tasks); m_futures.resize(m_num_tasks); m_render_system = &render_system; auto get_command_list_from_render_system = [this](auto type) { switch (type) { case RenderTaskType::DIRECT: return m_render_system->GetDirectCommandList(d3d12::settings::num_back_buffers); case RenderTaskType::COMPUTE: return m_render_system->GetComputeCommandList(d3d12::settings::num_back_buffers); case RenderTaskType::COPY: return m_render_system->GetCopyCommandList(d3d12::settings::num_back_buffers); default: LOGC("Tried creating a command list of a type that is not supported."); return static_cast(nullptr); } }; if constexpr (settings::use_multithreading) { for (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { // Get the proper command list from the render system. m_cmd_lists[i] = get_command_list_from_render_system(m_types[i]); #ifndef FG_MAX_PERFORMANCE render_system.SetCommandListName(m_cmd_lists[i], m_names[i]); #endif // Get a render target from the render system. if (m_rt_properties[i].has_value()) { m_render_targets[i] = render_system.GetRenderTarget(m_rt_properties[i].value()); #ifndef FG_MAX_PERFORMANCE render_system.SetRenderTargetName(m_render_targets[i], m_names[i]); #endif } } Setup_MT_Impl(); } else { // Itterate over all the tasks. for (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { // Get the proper command list from the render system. m_cmd_lists[i] = get_command_list_from_render_system(m_types[i]); #ifndef FG_MAX_PERFORMANCE render_system.SetCommandListName(m_cmd_lists[i], m_names[i]); #endif // Get a render target from the render system. if (m_rt_properties[i].has_value()) { m_render_targets[i] = render_system.GetRenderTarget(m_rt_properties[i].value()); #ifndef FG_MAX_PERFORMANCE render_system.SetRenderTargetName(m_render_targets[i], m_names[i]); #endif } // Call the setup function pointer. m_setup_funcs[i](render_system, *this, i, false); } } // Finish the setup before continuing for savety. for (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { WaitForCompletion(i); } } /*! Execute all render tasks */ /*! For every render task call the setup function pointers and tell the render system we started a render task of a certain type. \param render_system The render system we want to use for rendering. \param scene_graph The scene graph we want to render. */ inline void Execute(SceneGraph& scene_graph) { ResetOutputTexture(); // Check if we need to disable some tasks while (!m_should_execute_change_request.empty()) { auto front = m_should_execute_change_request.front(); m_should_execute[front.first] = front.second; m_should_execute_change_request.pop(); } if constexpr (settings::use_multithreading) { Execute_MT_Impl(scene_graph); } else { Execute_ST_Impl(scene_graph); } } /*! Resize all render tasks */ /*! This function calls resize all render tasks to a specific width and height. The width and height parameters should be the output size. Please note this function calls Destroy than setup with the resize boolean set to true. */ inline void Resize(std::uint32_t width, std::uint32_t height) { // Make sure the tasks are finished executing for (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { WaitForCompletion(i); } // Make sure the GPU has finished with the tasks m_render_system->WaitForAllPreviousWork(); for (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { m_destroy_funcs[i](*this, i, true); if (m_rt_properties[i].has_value() && !m_rt_properties[i].value().m_is_render_window) { m_render_system->ResizeRenderTarget(&m_render_targets[i], static_cast(std::ceil(width * m_rt_properties[i].value().m_resolution_scale.Get())), static_cast(std::ceil(height * m_rt_properties[i].value().m_resolution_scale.Get()))); } m_setup_funcs[i](*m_render_system, *this, i, true); } } /*! Get Resolution scale of specified Render Task */ /*! Checks if specified RenderTask has valid properties and returns it's resolution scalar. */ [[nodiscard]] inline const float GetRenderTargetResolutionScale(RenderTaskHandle handle) const { if (m_rt_properties[handle].has_value()) { return m_rt_properties[handle].value().m_resolution_scale.Get(); } else { LOGW("Error: GetResolutionScale tried accessing invalid data!") } return 1.0f; } /*! Destroy all tasks */ /*! Calls all destroy functions and release any allocated data. */ void Destroy() { // Make sure all tasks finished executing for (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { WaitForCompletion(i); } m_render_system->WaitForAllPreviousWork(); // Send the destroy events to the render tasks. for (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { m_destroy_funcs[i](*this, i, false); } // Make sure we free the data objects we allocated. for (auto& data : m_data) { data.reset(); } for (auto& cmd_list : m_cmd_lists) { m_render_system->DestroyCommandList(cmd_list); } for(decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { if(m_rt_properties[i].has_value() && !m_rt_properties[i]->m_is_render_window) { m_render_system->DestroyRenderTarget(&m_render_targets[i]); } } // Reset all members in the case of the user wanting to reuse this frame graph after `FrameGraph::Destroy`. m_setup_funcs.clear(); m_execute_funcs.clear(); m_destroy_funcs.clear(); m_cmd_lists.clear(); m_render_targets.clear(); m_data.clear(); m_data_type_info.clear(); m_settings.clear(); #ifndef FG_MAX_PERFORMANCE m_dependencies.clear(); m_names.clear(); #endif m_types.clear(); m_rt_properties.clear(); m_futures.clear(); m_num_tasks = 0; } /* Stall the current thread until the render task has finished. */ inline void WaitForCompletion(RenderTaskHandle handle) { // If we are not allowed to use multithreading let the compiler optimize this away completely. if constexpr (settings::use_multithreading) { if (auto& future = m_futures[handle]; future.valid()) { future.wait(); } } } /*! Wait for a previous task. */ /*! This function loops over all tasks and checks whether it has the same type information as the template variable. If a task was found it waits for it. If no task is found with the type specified a nullptr will be returned and a error message send to the logging system. The template parameter should be a Data struct of a the task you want to wait for. */ template inline void WaitForPredecessorTask() { static_assert(std::is_class::value || std::is_floating_point::value || std::is_integral::value, "The template variable should be a class, struct, floating point value or a integral value."); static_assert(!std::is_pointer::value, "The template variable type should not be a pointer. Its implicitly converted to a pointer."); for (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++) { if (typeid(T) == m_data_type_info[i]) { WaitForCompletion(i); return; } } LOGC("Failed to find predecessor data! Please check your task order."); return; } /*! Get the data of a task. (Modifyable) */ /*! The template variable is used to specify the type of the data structure. \param handle The handle to the render task. (Given by the `Setup`, `Execute` and `Destroy` functions) */ template [[nodiscard]] inline auto & GetData(RenderTaskHandle handle) const { static_assert(std::is_class::value || std::is_floating_point::value || std::is_integral::value, "The template variable should be a class, struct, floating point value or a integral value."); static_assert(!std::is_pointer::value, "The template variable type should not be a pointer. Its implicitly converted to a pointer."); return *static_cast(m_data[handle].get()); } /*! Get the data of a previously ran task. (Constant) */ /*! This function loops over all tasks and checks whether it has the same type information as the template variable. If no task is found with the type specified a nullptr will be returned and a error message send to the logging system. \param handle The handle to the render task. (Given by the `Setup`, `Execute` and `Destroy` functions) */ template [[nodiscard]] inline auto const & GetPredecessorData() { static_assert(std::is_class::value || std::is_floating_point::value || std::is_integral::value, "The template variable should be a class, struct, floating point value or a integral value."); static_assert(!std::is_pointer::value, "The template variable type should not be a pointer. Its implicitly converted to a pointer."); for (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++) { if (typeid(T) == m_data_type_info[i]) { WaitForCompletion(i); return *static_cast(m_data[i].get()); } } LOGC("Failed to find predecessor data! Please check your task order.") return *static_cast(nullptr); } /*! Get the render target of a previously ran task. (Constant) */ /*! This function loops over all tasks and checks whether it has the same type information as the template variable. If no task is found with the type specified a nullptr will be returned and a error message send to the logging system. */ template [[nodiscard]] inline RenderTarget* GetPredecessorRenderTarget() { static_assert(std::is_class::value, "The template variable should be a class or struct."); static_assert(!std::is_pointer::value, "The template variable type should not be a pointer. Its implicitly converted to a pointer."); for (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++) { if (typeid(T) == m_data_type_info[i]) { WaitForCompletion(i); return m_render_targets[i]; } } LOGC("Failed to find predecessor render target! Please check your task order."); return nullptr; } /*! Get the command list of a task. */ /*! The template variable allows you to cast the command list to a "non platform independent" different type. For example a `D3D12CommandList`. \param handle The handle to the render task. (Given by the `Setup`, `Execute` and `Destroy` functions) */ template [[nodiscard]] inline auto GetCommandList(RenderTaskHandle handle) const { static_assert(std::is_class::value || std::is_void::value, "The template variable should be a void, class or struct."); static_assert(!std::is_pointer::value, "The template variable type should not be a pointer. Its implicitly converted to a pointer."); return static_cast(m_cmd_lists[handle]); } /*! Get the command list of a previously ran task. */ /*! The function allows the user to get a command list from another render task. These command lists are not meant to be used as they could be closed or in flight. This function was created only so that ray tracing tasks could get the heap from the acceleration structure command list. */ template [[nodiscard]] inline wr::CommandList* GetPredecessorCommandList() { static_assert(std::is_class::value, "The template variable should be a void, class or struct."); static_assert(!std::is_pointer::value, "The template variable type should not be a pointer. Its implicitly converted to a pointer."); for (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++) { if (typeid(T) == m_data_type_info[i]) { WaitForCompletion(i); return m_cmd_lists[i]; } } LOGC("Failed to find predecessor command list! Please check your task order."); return nullptr; } template [[nodiscard]] std::vector GetAllCommandLists() { std::vector retval; retval.reserve(m_num_tasks); // TODO: Just return the fucking vector as const ref. for (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++) { // Don't return command lists from tasks that don't require to be executed. if (!m_should_execute[i]) { continue; } WaitForCompletion(i); retval.push_back(static_cast(m_cmd_lists[i])); } return retval; } /*! Get the render target of a task. */ /*! The template variable allows you to cast the render target to a "non platform independent" different type. For example a `D3D12RenderTarget`. \param handle The handle to the render task. (Given by the `Setup`, `Execute` and `Destroy` functions) */ template [[nodiscard]] inline auto GetRenderTarget(RenderTaskHandle handle) const { static_assert(std::is_class::value || std::is_void::value, "The template variable should be a void, class or struct."); static_assert(!std::is_pointer::value, "The template variable type should not be a pointer. Its implicitly converted to a pointer."); return static_cast(m_render_targets[handle]); } /*! Check if this frame graph has a task. */ /*! This checks if the frame graph has the task that has been given as the template variable. */ template inline bool HasTask() const { return GetHandleFromType().has_value(); } /*! Validates the frame graph for correctness */ /*! This function uses the dependencies to check whether the frame graph is constructed properly by the user. Note: This function only works when `FG_MAX_PERFORMANCE` is defined. */ bool Validate() { bool result = true; #ifndef FG_MAX_PERFORMANCE // Loop over all the tasks. for (decltype(m_num_tasks) handle = 0; handle < m_num_tasks; ++handle) { // Loop over the task's dependencies. for (auto dependency : m_dependencies[handle]) { bool found_dependency = false; // Loop over the predecessor tasks. for (decltype(m_num_tasks) prev_handle = 0; prev_handle < handle; ++prev_handle) { const auto& task_type_info = m_data_type_info[prev_handle].get(); if (task_type_info == dependency.get()) { found_dependency = true; } } if (!found_dependency) { LOGW("Framegraph validation: Failed to find dependency {}", dependency.get().name()); result = false; } } } #endif return result; } /*! Add a task to the Frame Graph. */ /*! This creates a new render task based on a description. The dependencies parameters can contain a list of typeid's of render tasks this task depends on. You can use the FG_DEPS macro as followed: `AddTask` \param desc A description of the render task. */ template inline void AddTask(RenderTaskDesc& desc, std::wstring const & name, std::vector> dependencies = {}) { static_assert(std::is_class::value || std::is_floating_point::value || std::is_integral::value, "The template variable should be a class, struct, floating point value or a integral value."); static_assert(std::is_default_constructible::value, "The template variable is not default constructible or nothrow consructible!"); static_assert(!std::is_pointer::value, "The template variable type should not be a pointer. Its implicitly converted to a pointer."); m_setup_funcs.emplace_back(desc.m_setup_func); m_execute_funcs.emplace_back(desc.m_execute_func); m_destroy_funcs.emplace_back(desc.m_destroy_func); #ifndef FG_MAX_PERFORMANCE m_dependencies.emplace_back(dependencies); m_names.emplace_back(name); #endif m_settings.resize(m_num_tasks + 1ull); m_types.emplace_back(desc.m_type); m_rt_properties.emplace_back(desc.m_properties); m_data.emplace_back(std::make_shared()); m_data_type_info.emplace_back(typeid(T)); // If we are allowed to do multithreading place the task in the appropriate vector if constexpr (settings::use_multithreading) { if (desc.m_allow_multithreading) { m_multi_threaded_tasks.emplace_back(m_num_tasks); } else { m_single_threaded_tasks.emplace_back(m_num_tasks); } } m_num_tasks++; } /*! Return the frame graph's unique id.*/ [[nodiscard]] const std::uint64_t GetUID() const noexcept { return m_uid; }; /*! Return the cpu texture. */ [[nodiscard]] CPUTextures const & GetOutputTexture() const noexcept { return m_output_cpu_textures; } /*! Save a render target to disc */ /* Tells the render system to save a render target to disc as a image. \param index The index of the render target from the task you want to save. */ template void SaveTaskToDisc(std::string const & path, int index = 0) { auto handle = GetHandleFromType(); if (handle.has_value()) { m_render_system->RequestRenderTargetSaveToDisc(path, m_render_targets[handle.value()], index); } else { LOGW("Failed to save render task to disc, Task was not found."); } } /*! Set the cpu texture's data. */ void SetOutputTexture(const CPUTexture& output_texture, CPUTextureType type) { switch (type) { case wr::CPUTextureType::PIXEL_DATA: if (m_output_cpu_textures.pixel_data != std::nullopt) { LOGW("Warning: CPU texture pixel data is written to more than once a frame!"); } // Save the pixel data m_output_cpu_textures.pixel_data = output_texture; break; case wr::CPUTextureType::DEPTH_DATA: if (m_output_cpu_textures.depth_data != std::nullopt) { LOGW("Warning: CPU texture depth data is written to more than once a frame!"); } // Save the depth data m_output_cpu_textures.depth_data = output_texture; break; default: // Should never happen LOGC("Invalid CPU texture type supplied!") break; } } /*! Enable or disable execution of a task. */ inline void SetShouldExecute(RenderTaskHandle handle, bool value) { m_should_execute_change_request.emplace(std::make_pair(handle, value)); } /*! Enable or disable execution of a task. Templated version */ template inline void SetShouldExecute(bool value) { auto handle = GetHandleFromType(); if (handle.has_value()) { SetShouldExecute(handle.value(), value); } else { LOGW("Failed to mark the task for execution, Task was not found."); } } /*! Update the settings of a task. */ /*! This is used to update settings of a render task. This must ge called BEFORE `FrameGraph::Setup` or `RenderSystem::Render`. */ template inline void UpdateSettings(std::any settings) { auto handle = GetHandleFromType(); if (handle.has_value()) { m_settings[handle.value()] = settings; } else { LOGW("Failed to update settings, Could not find render task"); } } /*! Gives you the settings of a task by handle. */ /*! This gives you the settings for a render task casted to `R`. Meant to be used for INSIDE the tasks. The return value can be a nullptr. \tparam T The render task data type used for identification. \tparam R The type of the settings object. */ template [[nodiscard]] inline R GetSettings() const try { static_assert(std::is_class::value || std::is_floating_point::value || std::is_integral::value, "The first template variable should be a class, struct, floating point value or a integral value."); for (decltype(m_num_tasks) i = 0; i < m_num_tasks; i++) { if (typeid(T) == m_data_type_info[i]) { return std::any_cast(m_settings[i].value()); } } LOGC("Failed to find task settings! Does your frame graph contain this task?"); return R(); } catch (const std::bad_any_cast & e) { LOGC("A task settings requested failed to cast to T. ({})", e.what()); return R(); } /*! Gives you the settings of a task by handle. */ /*! This gives you the settings for a render task casted to `T`. Meant to be used for INSIDE the tasks. The return value can be a nullptr. */ template [[nodiscard]] inline T GetSettings(RenderTaskHandle handle) const try { static_assert(std::is_class::value || std::is_floating_point::value || std::is_integral::value, "The template variable should be a class, struct, floating point value or a integral value."); return std::any_cast(m_settings[handle].value()); } catch (const std::bad_any_cast& e) { LOGW("A task settings requested failed to cast to T. ({})", e.what()); return T(); } /*! Resets the CPU texture data for this frame. */ inline void ResetOutputTexture() { // Frame has been rendered, allow a task to write to the CPU texture in the next frame m_output_cpu_textures.pixel_data = std::nullopt; m_output_cpu_textures.depth_data = std::nullopt; } private: /*! Get the handle from a task by data type */ /* This function is zero overhead with GCC-8.2, -03 */ template inline std::optional GetHandleFromType() const { for (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { if (m_data_type_info[i].get() == typeid(T)) { return i; } } return std::nullopt; } /*! Setup tasks multi threaded */ inline void Setup_MT_Impl() { // Multithreading behaviour for (const auto handle : m_multi_threaded_tasks) { m_futures[handle] = m_thread_pool->Enqueue([this, handle] { m_setup_funcs[handle](*m_render_system, *this, handle, false); }); } // Singlethreading behaviour for (const auto handle : m_single_threaded_tasks) { m_setup_funcs[handle](*m_render_system, *this, handle, false); } } /*! Execute tasks multi threaded */ inline void Execute_MT_Impl(SceneGraph& scene_graph) { // Multithreading behaviour for (const auto handle : m_multi_threaded_tasks) { // Skip this task if it doesn't need to be executed if (!m_should_execute[handle]) { continue; } m_futures[handle] = m_thread_pool->Enqueue([this, handle, &scene_graph] { ExecuteSingleTask(scene_graph, handle); }); } // Singlethreading behaviour for (const auto handle : m_single_threaded_tasks) { // Skip this task if it doesn't need to be executed if (!m_should_execute[handle]) { continue; } ExecuteSingleTask(scene_graph, handle); } } /*! Execute tasks single threaded */ inline void Execute_ST_Impl(SceneGraph& scene_graph) { for (decltype(m_num_tasks) i = 0; i < m_num_tasks; ++i) { // Skip this task if it doesn't need to be executed if (!m_should_execute[i]) { continue; } ExecuteSingleTask(scene_graph, i); } } /*! Execute a single task */ inline void ExecuteSingleTask(SceneGraph& sg, RenderTaskHandle handle) { auto cmd_list = m_cmd_lists[handle]; auto render_target = m_render_targets[handle]; auto rt_properties = m_rt_properties[handle]; m_render_system->ResetCommandList(cmd_list); switch (m_types[handle]) { case RenderTaskType::DIRECT: if (rt_properties.has_value()) { m_render_system->StartRenderTask(cmd_list, { render_target, rt_properties.value() }); } m_execute_funcs[handle](*m_render_system, *this, sg, handle); if (rt_properties.has_value()) { m_render_system->StopRenderTask(cmd_list, { render_target, rt_properties.value() }); } break; case RenderTaskType::COMPUTE: if (rt_properties.has_value()) { m_render_system->StartComputeTask(cmd_list, { render_target, rt_properties.value() }); } m_execute_funcs[handle](*m_render_system, *this, sg, handle); if (rt_properties.has_value()) { m_render_system->StopComputeTask(cmd_list, { render_target, rt_properties.value() }); } break; case RenderTaskType::COPY: if (rt_properties.has_value()) { m_render_system->StartCopyTask(cmd_list, { render_target, rt_properties.value() }); } m_execute_funcs[handle](*m_render_system, *this, sg, handle); if (rt_properties.has_value()) { m_render_system->StopCopyTask(cmd_list, { render_target, rt_properties.value() }); } break; } m_render_system->CloseCommandList(cmd_list); } /*! Get a free unique ID. */ static std::uint64_t GetFreeUID() { if (!m_free_uids.empty()) { std::uint64_t uid = m_free_uids.top(); m_free_uids.pop(); return uid; } std::uint64_t uid = m_largest_uid; m_largest_uid++; return uid; } /*! Get a release a unique ID for reuse. */ WISPRENDERER_EXPORT static void ReleaseUID(std::uint64_t uid) { m_free_uids.push(uid); } RenderSystem* m_render_system; /*! The number of tasks we have added. */ std::uint32_t m_num_tasks; /*! The thread pool used for multithreading */ util::ThreadPool* m_thread_pool; /*! Vectors which allow us to itterate over only single threader or only multithreaded tasks. */ std::vector m_multi_threaded_tasks; std::vector m_single_threaded_tasks; /*! Holds the textures that can be written to memory. */ CPUTextures m_output_cpu_textures; /*! Task function pointers. */ std::vector m_setup_funcs; std::vector m_execute_funcs; std::vector m_destroy_funcs; /*! Task target and command list. */ std::vector m_cmd_lists; std::vector m_render_targets; /*! Task data and the type information of the original data structure. */ std::vector> m_data; std::vector> m_data_type_info; /*! Task settings that can be passed to the frame graph from outside the task. */ std::vector> m_settings; /*! Defines whether a task should execute or not. */ std::vector m_should_execute; /*! Used to queue a request to change the should execute value */ std::queue> m_should_execute_change_request; /*! Descriptions of the tasks. */ #ifndef FG_MAX_PERFORMANCE /*! Stored the dependencies of a task. */ std::vector>> m_dependencies; /*! The names of the render targets meant for debugging */ std::vector m_names; #endif std::vector m_types; std::vector> m_rt_properties; std::vector> m_futures; const std::uint64_t m_uid; static inline std::uint64_t m_largest_uid = 0; static inline std::stack m_free_uids = {}; }; } /* wr */ ================================================ FILE: src/id_factory.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "id_factory.hpp" namespace wr { IDFactory::IDFactory() : m_id(0) { } void IDFactory::MakeIDAvailable(std::uint32_t unused_id) { m_unused_ids.push_back(unused_id); } std::uint32_t IDFactory::GetUnusedID() { std::uint32_t ret_id; if (m_unused_ids.empty()) { ret_id = m_id; m_id++; } else { ret_id = *(m_unused_ids.end() - 1); m_unused_ids.pop_back(); } return ret_id; } } /* wr */ ================================================ FILE: src/id_factory.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include namespace wr { class IDFactory { public: IDFactory(); virtual ~IDFactory() = default; IDFactory(IDFactory const &) = delete; IDFactory& operator=(IDFactory const &) = delete; IDFactory(IDFactory&&) = delete; IDFactory& operator=(IDFactory&&) = delete; void MakeIDAvailable(std::uint32_t unused_id); std::uint32_t GetUnusedID(); protected: std::uint32_t m_id; std::vector m_unused_ids; }; } /* wr */ ================================================ FILE: src/imgui/ImGuizmo.cpp ================================================ // The MIT License(MIT) // // Copyright(c) 2016 Cedric Guillemet // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files(the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions : // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #include "imgui.hpp" #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif #include "imgui_internal.hpp" #include "ImGuizmo.h" // includes patches for multiview from // https://github.com/CedricGuillemet/ImGuizmo/issues/15 namespace ImGuizmo { static const float ZPI = 3.14159265358979323846f; static const float RAD2DEG = (180.f / ZPI); static const float DEG2RAD = (ZPI / 180.f); static const float gGizmoSizeClipSpace = 0.1f; const float screenRotateSize = 0.06f; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // utility and math void FPU_MatrixF_x_MatrixF(const float *a, const float *b, float *r) { r[0] = a[0] * b[0] + a[1] * b[4] + a[2] * b[8] + a[3] * b[12]; r[1] = a[0] * b[1] + a[1] * b[5] + a[2] * b[9] + a[3] * b[13]; r[2] = a[0] * b[2] + a[1] * b[6] + a[2] * b[10] + a[3] * b[14]; r[3] = a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3] * b[15]; r[4] = a[4] * b[0] + a[5] * b[4] + a[6] * b[8] + a[7] * b[12]; r[5] = a[4] * b[1] + a[5] * b[5] + a[6] * b[9] + a[7] * b[13]; r[6] = a[4] * b[2] + a[5] * b[6] + a[6] * b[10] + a[7] * b[14]; r[7] = a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7] * b[15]; r[8] = a[8] * b[0] + a[9] * b[4] + a[10] * b[8] + a[11] * b[12]; r[9] = a[8] * b[1] + a[9] * b[5] + a[10] * b[9] + a[11] * b[13]; r[10] = a[8] * b[2] + a[9] * b[6] + a[10] * b[10] + a[11] * b[14]; r[11] = a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11] * b[15]; r[12] = a[12] * b[0] + a[13] * b[4] + a[14] * b[8] + a[15] * b[12]; r[13] = a[12] * b[1] + a[13] * b[5] + a[14] * b[9] + a[15] * b[13]; r[14] = a[12] * b[2] + a[13] * b[6] + a[14] * b[10] + a[15] * b[14]; r[15] = a[12] * b[3] + a[13] * b[7] + a[14] * b[11] + a[15] * b[15]; } //template T LERP(T x, T y, float z) { return (x + (y - x)*z); } template T Clamp(T x, T y, T z) { return ((xz) ? z : x)); } template T max(T x, T y) { return (x > y) ? x : y; } template T min(T x, T y) { return (x < y) ? x : y; } template bool IsWithin(T x, T y, T z) { return (x>=y) && (x<=z); } struct matrix_t; struct vec_t { public: float x, y, z, w; void Lerp(const vec_t& v, float t) { x += (v.x - x) * t; y += (v.y - y) * t; z += (v.z - z) * t; w += (v.w - w) * t; } void Set(float v) { x = y = z = w = v; } void Set(float _x, float _y, float _z = 0.f, float _w = 0.f) { x = _x; y = _y; z = _z; w = _w; } vec_t& operator -= (const vec_t& v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; } vec_t& operator += (const vec_t& v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; } vec_t& operator *= (const vec_t& v) { x *= v.x; y *= v.y; z *= v.z; w *= v.w; return *this; } vec_t& operator *= (float v) { x *= v; y *= v; z *= v; w *= v; return *this; } vec_t operator * (float f) const; vec_t operator - () const; vec_t operator - (const vec_t& v) const; vec_t operator + (const vec_t& v) const; vec_t operator * (const vec_t& v) const; const vec_t& operator + () const { return (*this); } float Length() const { return sqrtf(x*x + y*y + z*z); }; float LengthSq() const { return (x*x + y*y + z*z); }; vec_t Normalize() { (*this) *= (1.f / Length()); return (*this); } vec_t Normalize(const vec_t& v) { this->Set(v.x, v.y, v.z, v.w); this->Normalize(); return (*this); } vec_t Abs() const; void Cross(const vec_t& v) { vec_t res; res.x = y * v.z - z * v.y; res.y = z * v.x - x * v.z; res.z = x * v.y - y * v.x; x = res.x; y = res.y; z = res.z; w = 0.f; } void Cross(const vec_t& v1, const vec_t& v2) { x = v1.y * v2.z - v1.z * v2.y; y = v1.z * v2.x - v1.x * v2.z; z = v1.x * v2.y - v1.y * v2.x; w = 0.f; } float Dot(const vec_t &v) const { return (x * v.x) + (y * v.y) + (z * v.z) + (w * v.w); } float Dot3(const vec_t &v) const { return (x * v.x) + (y * v.y) + (z * v.z); } void Transform(const matrix_t& matrix); void Transform(const vec_t & s, const matrix_t& matrix); void TransformVector(const matrix_t& matrix); void TransformPoint(const matrix_t& matrix); void TransformVector(const vec_t& v, const matrix_t& matrix) { (*this) = v; this->TransformVector(matrix); } void TransformPoint(const vec_t& v, const matrix_t& matrix) { (*this) = v; this->TransformPoint(matrix); } float& operator [] (size_t index) { return ((float*)&x)[index]; } const float& operator [] (size_t index) const { return ((float*)&x)[index]; } }; 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; } 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; } vec_t vec_t::operator * (float f) const { return makeVect(x * f, y * f, z * f, w *f); } vec_t vec_t::operator - () const { return makeVect(-x, -y, -z, -w); } vec_t vec_t::operator - (const vec_t& v) const { return makeVect(x - v.x, y - v.y, z - v.z, w - v.w); } vec_t vec_t::operator + (const vec_t& v) const { return makeVect(x + v.x, y + v.y, z + v.z, w + v.w); } vec_t vec_t::operator * (const vec_t& v) const { return makeVect(x * v.x, y * v.y, z * v.z, w * v.w); } vec_t vec_t::Abs() const { return makeVect(fabsf(x), fabsf(y), fabsf(z)); } vec_t Normalized(const vec_t& v) { vec_t res; res = v; res.Normalize(); return res; } vec_t Cross(const vec_t& v1, const vec_t& v2) { vec_t res; res.x = v1.y * v2.z - v1.z * v2.y; res.y = v1.z * v2.x - v1.x * v2.z; res.z = v1.x * v2.y - v1.y * v2.x; res.w = 0.f; return res; } float Dot(const vec_t &v1, const vec_t &v2) { return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z); } vec_t BuildPlan(const vec_t & p_point1, const vec_t & p_normal) { vec_t normal, res; normal.Normalize(p_normal); res.w = normal.Dot(p_point1); res.x = normal.x; res.y = normal.y; res.z = normal.z; return res; } struct matrix_t { public: union { float m[4][4]; float m16[16]; struct { vec_t right, up, dir, position; } v; vec_t component[4]; }; matrix_t(const matrix_t& other) { memcpy(&m16[0], &other.m16[0], sizeof(float) * 16); } matrix_t() {} operator float * () { return m16; } operator const float* () const { return m16; } void Translation(float _x, float _y, float _z) { this->Translation(makeVect(_x, _y, _z)); } void Translation(const vec_t& vt) { v.right.Set(1.f, 0.f, 0.f, 0.f); v.up.Set(0.f, 1.f, 0.f, 0.f); v.dir.Set(0.f, 0.f, 1.f, 0.f); v.position.Set(vt.x, vt.y, vt.z, 1.f); } void Scale(float _x, float _y, float _z) { v.right.Set(_x, 0.f, 0.f, 0.f); v.up.Set(0.f, _y, 0.f, 0.f); v.dir.Set(0.f, 0.f, _z, 0.f); v.position.Set(0.f, 0.f, 0.f, 1.f); } void Scale(const vec_t& s) { Scale(s.x, s.y, s.z); } matrix_t& operator *= (const matrix_t& mat) { matrix_t tmpMat; tmpMat = *this; tmpMat.Multiply(mat); *this = tmpMat; return *this; } matrix_t operator * (const matrix_t& mat) const { matrix_t matT; matT.Multiply(*this, mat); return matT; } void Multiply(const matrix_t &matrix) { matrix_t tmp; tmp = *this; FPU_MatrixF_x_MatrixF((float*)&tmp, (float*)&matrix, (float*)this); } void Multiply(const matrix_t &m1, const matrix_t &m2) { FPU_MatrixF_x_MatrixF((float*)&m1, (float*)&m2, (float*)this); } float GetDeterminant() const { 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] - 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]; } float Inverse(const matrix_t &srcMatrix, bool affine = false); void SetToIdentity() { v.right.Set(1.f, 0.f, 0.f, 0.f); v.up.Set(0.f, 1.f, 0.f, 0.f); v.dir.Set(0.f, 0.f, 1.f, 0.f); v.position.Set(0.f, 0.f, 0.f, 1.f); } void Transpose() { matrix_t tmpm; for (int l = 0; l < 4; l++) { for (int c = 0; c < 4; c++) { tmpm.m[l][c] = m[c][l]; } } (*this) = tmpm; } void RotationAxis(const vec_t & axis, float angle); void OrthoNormalize() { v.right.Normalize(); v.up.Normalize(); v.dir.Normalize(); } }; void vec_t::Transform(const matrix_t& matrix) { vec_t out; out.x = x * matrix.m[0][0] + y * matrix.m[1][0] + z * matrix.m[2][0] + w * matrix.m[3][0]; out.y = x * matrix.m[0][1] + y * matrix.m[1][1] + z * matrix.m[2][1] + w * matrix.m[3][1]; out.z = x * matrix.m[0][2] + y * matrix.m[1][2] + z * matrix.m[2][2] + w * matrix.m[3][2]; out.w = x * matrix.m[0][3] + y * matrix.m[1][3] + z * matrix.m[2][3] + w * matrix.m[3][3]; x = out.x; y = out.y; z = out.z; w = out.w; } void vec_t::Transform(const vec_t & s, const matrix_t& matrix) { *this = s; Transform(matrix); } void vec_t::TransformPoint(const matrix_t& matrix) { vec_t out; out.x = x * matrix.m[0][0] + y * matrix.m[1][0] + z * matrix.m[2][0] + matrix.m[3][0]; out.y = x * matrix.m[0][1] + y * matrix.m[1][1] + z * matrix.m[2][1] + matrix.m[3][1]; out.z = x * matrix.m[0][2] + y * matrix.m[1][2] + z * matrix.m[2][2] + matrix.m[3][2]; out.w = x * matrix.m[0][3] + y * matrix.m[1][3] + z * matrix.m[2][3] + matrix.m[3][3]; x = out.x; y = out.y; z = out.z; w = out.w; } void vec_t::TransformVector(const matrix_t& matrix) { vec_t out; out.x = x * matrix.m[0][0] + y * matrix.m[1][0] + z * matrix.m[2][0]; out.y = x * matrix.m[0][1] + y * matrix.m[1][1] + z * matrix.m[2][1]; out.z = x * matrix.m[0][2] + y * matrix.m[1][2] + z * matrix.m[2][2]; out.w = x * matrix.m[0][3] + y * matrix.m[1][3] + z * matrix.m[2][3]; x = out.x; y = out.y; z = out.z; w = out.w; } float matrix_t::Inverse(const matrix_t &srcMatrix, bool affine) { float det = 0; if (affine) { det = GetDeterminant(); float s = 1 / det; m[0][0] = (srcMatrix.m[1][1] * srcMatrix.m[2][2] - srcMatrix.m[1][2] * srcMatrix.m[2][1]) * s; m[0][1] = (srcMatrix.m[2][1] * srcMatrix.m[0][2] - srcMatrix.m[2][2] * srcMatrix.m[0][1]) * s; m[0][2] = (srcMatrix.m[0][1] * srcMatrix.m[1][2] - srcMatrix.m[0][2] * srcMatrix.m[1][1]) * s; m[1][0] = (srcMatrix.m[1][2] * srcMatrix.m[2][0] - srcMatrix.m[1][0] * srcMatrix.m[2][2]) * s; m[1][1] = (srcMatrix.m[2][2] * srcMatrix.m[0][0] - srcMatrix.m[2][0] * srcMatrix.m[0][2]) * s; m[1][2] = (srcMatrix.m[0][2] * srcMatrix.m[1][0] - srcMatrix.m[0][0] * srcMatrix.m[1][2]) * s; m[2][0] = (srcMatrix.m[1][0] * srcMatrix.m[2][1] - srcMatrix.m[1][1] * srcMatrix.m[2][0]) * s; m[2][1] = (srcMatrix.m[2][0] * srcMatrix.m[0][1] - srcMatrix.m[2][1] * srcMatrix.m[0][0]) * s; m[2][2] = (srcMatrix.m[0][0] * srcMatrix.m[1][1] - srcMatrix.m[0][1] * srcMatrix.m[1][0]) * s; m[3][0] = -(m[0][0] * srcMatrix.m[3][0] + m[1][0] * srcMatrix.m[3][1] + m[2][0] * srcMatrix.m[3][2]); m[3][1] = -(m[0][1] * srcMatrix.m[3][0] + m[1][1] * srcMatrix.m[3][1] + m[2][1] * srcMatrix.m[3][2]); m[3][2] = -(m[0][2] * srcMatrix.m[3][0] + m[1][2] * srcMatrix.m[3][1] + m[2][2] * srcMatrix.m[3][2]); } else { // transpose matrix float src[16]; for (int i = 0; i < 4; ++i) { src[i] = srcMatrix.m16[i * 4]; src[i + 4] = srcMatrix.m16[i * 4 + 1]; src[i + 8] = srcMatrix.m16[i * 4 + 2]; src[i + 12] = srcMatrix.m16[i * 4 + 3]; } // calculate pairs for first 8 elements (cofactors) float tmp[12]; // temp array for pairs tmp[0] = src[10] * src[15]; tmp[1] = src[11] * src[14]; tmp[2] = src[9] * src[15]; tmp[3] = src[11] * src[13]; tmp[4] = src[9] * src[14]; tmp[5] = src[10] * src[13]; tmp[6] = src[8] * src[15]; tmp[7] = src[11] * src[12]; tmp[8] = src[8] * src[14]; tmp[9] = src[10] * src[12]; tmp[10] = src[8] * src[13]; tmp[11] = src[9] * src[12]; // calculate first 8 elements (cofactors) 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]); 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]); 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]); 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]); 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]); 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]); 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]); 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]); // calculate pairs for second 8 elements (cofactors) tmp[0] = src[2] * src[7]; tmp[1] = src[3] * src[6]; tmp[2] = src[1] * src[7]; tmp[3] = src[3] * src[5]; tmp[4] = src[1] * src[6]; tmp[5] = src[2] * src[5]; tmp[6] = src[0] * src[7]; tmp[7] = src[3] * src[4]; tmp[8] = src[0] * src[6]; tmp[9] = src[2] * src[4]; tmp[10] = src[0] * src[5]; tmp[11] = src[1] * src[4]; // calculate second 8 elements (cofactors) 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]); 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]); 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]); 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]); 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]); 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]); 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]); 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]); // calculate determinant det = src[0] * m16[0] + src[1] * m16[1] + src[2] * m16[2] + src[3] * m16[3]; // calculate matrix inverse float invdet = 1 / det; for (int j = 0; j < 16; ++j) { m16[j] *= invdet; } } return det; } void matrix_t::RotationAxis(const vec_t & axis, float angle) { float length2 = axis.LengthSq(); if (length2 < FLT_EPSILON) { SetToIdentity(); return; } vec_t n = axis * (1.f / sqrtf(length2)); float s = sinf(angle); float c = cosf(angle); float k = 1.f - c; float xx = n.x * n.x * k + c; float yy = n.y * n.y * k + c; float zz = n.z * n.z * k + c; float xy = n.x * n.y * k; float yz = n.y * n.z * k; float zx = n.z * n.x * k; float xs = n.x * s; float ys = n.y * s; float zs = n.z * s; m[0][0] = xx; m[0][1] = xy + zs; m[0][2] = zx - ys; m[0][3] = 0.f; m[1][0] = xy - zs; m[1][1] = yy; m[1][2] = yz + xs; m[1][3] = 0.f; m[2][0] = zx + ys; m[2][1] = yz - xs; m[2][2] = zz; m[2][3] = 0.f; m[3][0] = 0.f; m[3][1] = 0.f; m[3][2] = 0.f; m[3][3] = 1.f; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // enum MOVETYPE { NONE, MOVE_X, MOVE_Y, MOVE_Z, MOVE_YZ, MOVE_ZX, MOVE_XY, MOVE_SCREEN, ROTATE_X, ROTATE_Y, ROTATE_Z, ROTATE_SCREEN, SCALE_X, SCALE_Y, SCALE_Z, SCALE_XYZ }; struct Context { Context() : mbUsing(false), mbEnable(true), mbUsingBounds(false) { } ImDrawList* mDrawList; MODE mMode; matrix_t mViewMat; matrix_t mProjectionMat; matrix_t mModel; matrix_t mModelInverse; matrix_t mModelSource; matrix_t mModelSourceInverse; matrix_t mMVP; matrix_t mViewProjection; vec_t mModelScaleOrigin; vec_t mCameraEye; vec_t mCameraRight; vec_t mCameraDir; vec_t mCameraUp; vec_t mRayOrigin; vec_t mRayVector; float mRadiusSquareCenter; ImVec2 mScreenSquareCenter; ImVec2 mScreenSquareMin; ImVec2 mScreenSquareMax; float mScreenFactor; vec_t mRelativeOrigin; bool mbUsing; bool mbEnable; // translation vec_t mTranslationPlan; vec_t mTranslationPlanOrigin; vec_t mMatrixOrigin; // rotation vec_t mRotationVectorSource; float mRotationAngle; float mRotationAngleOrigin; //vec_t mWorldToLocalAxis; // scale vec_t mScale; vec_t mScaleValueOrigin; float mSaveMousePosx; // save axis factor when using gizmo bool mBelowAxisLimit[3]; bool mBelowPlaneLimit[3]; float mAxisFactor[3]; // bounds stretching vec_t mBoundsPivot; vec_t mBoundsAnchor; vec_t mBoundsPlan; vec_t mBoundsLocalPivot; int mBoundsBestAxis; int mBoundsAxis[2]; bool mbUsingBounds; matrix_t mBoundsMatrix; // int mCurrentOperation; float mX = 0.f; float mY = 0.f; float mWidth = 0.f; float mHeight = 0.f; float mXMax = 0.f; float mYMax = 0.f; float mDisplayRatio = 1.f; bool mIsOrthographic = false; }; static Context gContext; static const float angleLimit = 0.96f; static const float planeLimit = 0.2f; 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) }; static const ImU32 directionColor[3] = { 0xFF0000AA, 0xFF00AA00, 0xFFAA0000 }; // Alpha: 100%: FF, 87%: DE, 70%: B3, 54%: 8A, 50%: 80, 38%: 61, 12%: 1F static const ImU32 planeColor[3] = { 0x610000AA, 0x6100AA00, 0x61AA0000 }; static const ImU32 selectionColor = 0x8A1080FF; static const ImU32 inactiveColor = 0x99999999; static const ImU32 translationLineColor = 0xAAAAAAAA; static const char *translationInfoMask[] = { "X : %5.3f", "Y : %5.3f", "Z : %5.3f", "Y : %5.3f Z : %5.3f", "X : %5.3f Z : %5.3f", "X : %5.3f Y : %5.3f", "X : %5.3f Y : %5.3f Z : %5.3f" }; static const char *scaleInfoMask[] = { "X : %5.2f", "Y : %5.2f", "Z : %5.2f", "XYZ : %5.2f" }; 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" }; 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 }; static const float quadMin = 0.5f; static const float quadMax = 0.8f; static const float quadUV[8] = { quadMin, quadMin, quadMin, quadMax, quadMax, quadMax, quadMax, quadMin }; static const int halfCircleSegmentCount = 64; static const float snapTension = 0.5f; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // static int GetMoveType(vec_t *gizmoHitProportion); static int GetRotateType(); static int GetScaleType(); static ImVec2 worldToPos(const vec_t& worldPos, const matrix_t& mat) { vec_t trans; trans.TransformPoint(worldPos, mat); trans *= 0.5f / trans.w; trans += makeVect(0.5f, 0.5f); trans.y = 1.f - trans.y; trans.x *= gContext.mWidth; trans.y *= gContext.mHeight; trans.x += gContext.mX; trans.y += gContext.mY; return ImVec2(trans.x, trans.y); } static void ComputeCameraRay(vec_t &rayOrigin, vec_t &rayDir) { ImGuiIO& io = ImGui::GetIO(); matrix_t mViewProjInverse; mViewProjInverse.Inverse(gContext.mViewMat * gContext.mProjectionMat); float mox = ((io.MousePos.x - gContext.mX) / gContext.mWidth) * 2.f - 1.f; float moy = (1.f - ((io.MousePos.y - gContext.mY) / gContext.mHeight)) * 2.f - 1.f; rayOrigin.Transform(makeVect(mox, moy, 0.f, 1.f), mViewProjInverse); rayOrigin *= 1.f / rayOrigin.w; vec_t rayEnd; rayEnd.Transform(makeVect(mox, moy, 1.f, 1.f), mViewProjInverse); rayEnd *= 1.f / rayEnd.w; rayDir = Normalized(rayEnd - rayOrigin); } static float GetSegmentLengthClipSpace(const vec_t& start, const vec_t& end) { vec_t startOfSegment = start; startOfSegment.TransformPoint(gContext.mMVP); if (fabsf(startOfSegment.w)> FLT_EPSILON) // check for axis aligned with camera direction startOfSegment *= 1.f / startOfSegment.w; vec_t endOfSegment = end; endOfSegment.TransformPoint(gContext.mMVP); if (fabsf(endOfSegment.w)> FLT_EPSILON) // check for axis aligned with camera direction endOfSegment *= 1.f / endOfSegment.w; vec_t clipSpaceAxis = endOfSegment - startOfSegment; clipSpaceAxis.y /= gContext.mDisplayRatio; float segmentLengthInClipSpace = sqrtf(clipSpaceAxis.x*clipSpaceAxis.x + clipSpaceAxis.y*clipSpaceAxis.y); return segmentLengthInClipSpace; } static float GetParallelogram(const vec_t& ptO, const vec_t& ptA, const vec_t& ptB) { vec_t pts[] = { ptO, ptA, ptB }; for (unsigned int i = 0; i < 3; i++) { pts[i].TransformPoint(gContext.mMVP); if (fabsf(pts[i].w)> FLT_EPSILON) // check for axis aligned with camera direction pts[i] *= 1.f / pts[i].w; } vec_t segA = pts[1] - pts[0]; vec_t segB = pts[2] - pts[0]; segA.y /= gContext.mDisplayRatio; segB.y /= gContext.mDisplayRatio; vec_t segAOrtho = makeVect(-segA.y, segA.x); segAOrtho.Normalize(); float dt = segAOrtho.Dot3(segB); float surface = sqrtf(segA.x*segA.x + segA.y*segA.y) * fabsf(dt); return surface; } inline vec_t PointOnSegment(const vec_t & point, const vec_t & vertPos1, const vec_t & vertPos2) { vec_t c = point - vertPos1; vec_t V; V.Normalize(vertPos2 - vertPos1); float d = (vertPos2 - vertPos1).Length(); float t = V.Dot3(c); if (t < 0.f) return vertPos1; if (t > d) return vertPos2; return vertPos1 + V * t; } static float IntersectRayPlane(const vec_t & rOrigin, const vec_t& rVector, const vec_t& plan) { float numer = plan.Dot3(rOrigin) - plan.w; float denom = plan.Dot3(rVector); if (fabsf(denom) < FLT_EPSILON) // normal is orthogonal to vector, cant intersect return -1.0f; return -(numer / denom); } static bool IsInContextRect( ImVec2 p ) { return IsWithin( p.x, gContext.mX, gContext.mXMax ) && IsWithin(p.y, gContext.mY, gContext.mYMax ); } void SetRect(float x, float y, float width, float height) { gContext.mX = x; gContext.mY = y; gContext.mWidth = width; gContext.mHeight = height; gContext.mXMax = gContext.mX + gContext.mWidth; gContext.mYMax = gContext.mY + gContext.mXMax; gContext.mDisplayRatio = width / height; } IMGUI_API void SetOrthographic(bool isOrthographic) { gContext.mIsOrthographic = isOrthographic; } void SetDrawlist() { gContext.mDrawList = ImGui::GetWindowDrawList(); } void BeginFrame() { ImGuiIO& io = ImGui::GetIO(); const ImU32 flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoBringToFrontOnFocus; ImGui::SetNextWindowSize(io.DisplaySize); ImGui::SetNextWindowPos(ImVec2(0, 0)); ImGui::PushStyleColor(ImGuiCol_WindowBg, 0); ImGui::PushStyleColor(ImGuiCol_Border, 0); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); ImGui::Begin("gizmo", NULL, flags); gContext.mDrawList = ImGui::GetWindowDrawList(); ImGui::End(); ImGui::PopStyleVar(); ImGui::PopStyleColor(2); } bool IsUsing() { return gContext.mbUsing||gContext.mbUsingBounds; } bool IsOver() { return (GetMoveType(NULL) != NONE) || GetRotateType() != NONE || GetScaleType() != NONE || IsUsing(); } void Enable(bool enable) { gContext.mbEnable = enable; if (!enable) { gContext.mbUsing = false; gContext.mbUsingBounds = false; } } static void ComputeContext(const float *view, const float *projection, float *matrix, MODE mode) { gContext.mMode = mode; gContext.mViewMat = *(matrix_t*)view; gContext.mProjectionMat = *(matrix_t*)projection; if (mode == LOCAL) { gContext.mModel = *(matrix_t*)matrix; gContext.mModel.OrthoNormalize(); } else { gContext.mModel.Translation(((matrix_t*)matrix)->v.position); } gContext.mModelSource = *(matrix_t*)matrix; gContext.mModelScaleOrigin.Set(gContext.mModelSource.v.right.Length(), gContext.mModelSource.v.up.Length(), gContext.mModelSource.v.dir.Length()); gContext.mModelInverse.Inverse(gContext.mModel); gContext.mModelSourceInverse.Inverse(gContext.mModelSource); gContext.mViewProjection = gContext.mViewMat * gContext.mProjectionMat; gContext.mMVP = gContext.mModel * gContext.mViewProjection; matrix_t viewInverse; viewInverse.Inverse(gContext.mViewMat); gContext.mCameraDir = viewInverse.v.dir; gContext.mCameraEye = viewInverse.v.position; gContext.mCameraRight = viewInverse.v.right; gContext.mCameraUp = viewInverse.v.up; // compute scale from the size of camera right vector projected on screen at the matrix position vec_t pointRight = viewInverse.v.right; pointRight.TransformPoint(gContext.mViewProjection); gContext.mScreenFactor = gGizmoSizeClipSpace / (pointRight.x / pointRight.w - gContext.mMVP.v.position.x / gContext.mMVP.v.position.w); vec_t rightViewInverse = viewInverse.v.right; rightViewInverse.TransformVector(gContext.mModelInverse); float rightLength = GetSegmentLengthClipSpace(makeVect(0.f, 0.f), rightViewInverse); gContext.mScreenFactor = gGizmoSizeClipSpace / rightLength; ImVec2 centerSSpace = worldToPos(makeVect(0.f, 0.f), gContext.mMVP); gContext.mScreenSquareCenter = centerSSpace; gContext.mScreenSquareMin = ImVec2(centerSSpace.x - 10.f, centerSSpace.y - 10.f); gContext.mScreenSquareMax = ImVec2(centerSSpace.x + 10.f, centerSSpace.y + 10.f); ComputeCameraRay(gContext.mRayOrigin, gContext.mRayVector); } static void ComputeColors(ImU32 *colors, int type, OPERATION operation) { if (gContext.mbEnable) { switch (operation) { case TRANSLATE: colors[0] = (type == MOVE_SCREEN) ? selectionColor : 0xFFFFFFFF; for (int i = 0; i < 3; i++) { colors[i + 1] = (type == (int)(MOVE_X + i)) ? selectionColor : directionColor[i]; colors[i + 4] = (type == (int)(MOVE_YZ + i)) ? selectionColor : planeColor[i]; colors[i + 4] = (type == MOVE_SCREEN) ? selectionColor : colors[i + 4]; } break; case ROTATE: colors[0] = (type == ROTATE_SCREEN) ? selectionColor : 0xFFFFFFFF; for (int i = 0; i < 3; i++) colors[i + 1] = (type == (int)(ROTATE_X + i)) ? selectionColor : directionColor[i]; break; case SCALE: colors[0] = (type == SCALE_XYZ) ? selectionColor : 0xFFFFFFFF; for (int i = 0; i < 3; i++) colors[i + 1] = (type == (int)(SCALE_X + i)) ? selectionColor : directionColor[i]; break; case BOUNDS: break; } } else { for (int i = 0; i < 7; i++) colors[i] = inactiveColor; } } static void ComputeTripodAxisAndVisibility(int axisIndex, vec_t& dirAxis, vec_t& dirPlaneX, vec_t& dirPlaneY, bool& belowAxisLimit, bool& belowPlaneLimit) { dirAxis = directionUnary[axisIndex]; dirPlaneX = directionUnary[(axisIndex + 1) % 3]; dirPlaneY = directionUnary[(axisIndex + 2) % 3]; if (gContext.mbUsing) { // when using, use stored factors so the gizmo doesn't flip when we translate belowAxisLimit = gContext.mBelowAxisLimit[axisIndex]; belowPlaneLimit = gContext.mBelowPlaneLimit[axisIndex]; dirAxis *= gContext.mAxisFactor[axisIndex]; dirPlaneX *= gContext.mAxisFactor[(axisIndex + 1) % 3]; dirPlaneY *= gContext.mAxisFactor[(axisIndex + 2) % 3]; } else { // new method float lenDir = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), dirAxis); float lenDirMinus = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), -dirAxis); float lenDirPlaneX = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), dirPlaneX); float lenDirMinusPlaneX = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), -dirPlaneX); float lenDirPlaneY = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), dirPlaneY); float lenDirMinusPlaneY = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), -dirPlaneY); float mulAxis = (lenDir < lenDirMinus && fabsf(lenDir - lenDirMinus) > FLT_EPSILON) ? -1.f : 1.f; float mulAxisX = (lenDirPlaneX < lenDirMinusPlaneX && fabsf(lenDirPlaneX - lenDirMinusPlaneX) > FLT_EPSILON) ? -1.f : 1.f; float mulAxisY = (lenDirPlaneY < lenDirMinusPlaneY && fabsf(lenDirPlaneY - lenDirMinusPlaneY) > FLT_EPSILON) ? -1.f : 1.f; dirAxis *= mulAxis; dirPlaneX *= mulAxisX; dirPlaneY *= mulAxisY; // for axis float axisLengthInClipSpace = GetSegmentLengthClipSpace(makeVect(0.f, 0.f, 0.f), dirAxis * gContext.mScreenFactor); float paraSurf = GetParallelogram(makeVect(0.f, 0.f, 0.f), dirPlaneX * gContext.mScreenFactor, dirPlaneY * gContext.mScreenFactor); belowPlaneLimit = (paraSurf > 0.0025f); belowAxisLimit = (axisLengthInClipSpace > 0.02f); // and store values gContext.mAxisFactor[axisIndex] = mulAxis; gContext.mAxisFactor[(axisIndex + 1) % 3] = mulAxisX; gContext.mAxisFactor[(axisIndex + 2) % 3] = mulAxisY; gContext.mBelowAxisLimit[axisIndex] = belowAxisLimit; gContext.mBelowPlaneLimit[axisIndex] = belowPlaneLimit; } } static void ComputeSnap(float*value, float snap) { if (snap <= FLT_EPSILON) return; float modulo = fmodf(*value, snap); float moduloRatio = fabsf(modulo) / snap; if (moduloRatio < snapTension) *value -= modulo; else if (moduloRatio >(1.f - snapTension)) *value = *value - modulo + snap * ((*value<0.f) ? -1.f : 1.f); } static void ComputeSnap(vec_t& value, float *snap) { for (int i = 0; i < 3; i++) { ComputeSnap(&value[i], snap[i]); } } static float ComputeAngleOnPlan() { const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan); vec_t localPos = Normalized(gContext.mRayOrigin + gContext.mRayVector * len - gContext.mModel.v.position); vec_t perpendicularVector; perpendicularVector.Cross(gContext.mRotationVectorSource, gContext.mTranslationPlan); perpendicularVector.Normalize(); float acosAngle = Clamp(Dot(localPos, gContext.mRotationVectorSource), -0.9999f, 0.9999f); float angle = acosf(acosAngle); angle *= (Dot(localPos, perpendicularVector) < 0.f) ? 1.f : -1.f; return angle; } static void DrawRotationGizmo(int type) { ImDrawList* drawList = gContext.mDrawList; // colors ImU32 colors[7]; ComputeColors(colors, type, ROTATE); vec_t cameraToModelNormalized; if (gContext.mIsOrthographic) { matrix_t viewInverse; viewInverse.Inverse(*(matrix_t*)&gContext.mViewMat); cameraToModelNormalized = viewInverse.v.dir; } else { cameraToModelNormalized = Normalized(gContext.mModel.v.position - gContext.mCameraEye); } cameraToModelNormalized.TransformVector(gContext.mModelInverse); gContext.mRadiusSquareCenter = screenRotateSize * gContext.mHeight; for (int axis = 0; axis < 3; axis++) { ImVec2 circlePos[halfCircleSegmentCount]; float angleStart = atan2f(cameraToModelNormalized[(4-axis)%3], cameraToModelNormalized[(3 - axis) % 3]) + ZPI * 0.5f; for (unsigned int i = 0; i < halfCircleSegmentCount; i++) { float ng = angleStart + ZPI * ((float)i / (float)halfCircleSegmentCount); vec_t axisPos = makeVect(cosf(ng), sinf(ng), 0.f); vec_t pos = makeVect(axisPos[axis], axisPos[(axis+1)%3], axisPos[(axis+2)%3]) * gContext.mScreenFactor; circlePos[i] = worldToPos(pos, gContext.mMVP); } float radiusAxis = sqrtf( (ImLengthSqr(worldToPos(gContext.mModel.v.position, gContext.mViewProjection) - circlePos[0]) )); if(radiusAxis > gContext.mRadiusSquareCenter) gContext.mRadiusSquareCenter = radiusAxis; drawList->AddPolyline(circlePos, halfCircleSegmentCount, colors[3 - axis], false, 2); } drawList->AddCircle(worldToPos(gContext.mModel.v.position, gContext.mViewProjection), gContext.mRadiusSquareCenter, colors[0], 64, 3.f); if (gContext.mbUsing) { ImVec2 circlePos[halfCircleSegmentCount +1]; circlePos[0] = worldToPos(gContext.mModel.v.position, gContext.mViewProjection); for (unsigned int i = 1; i < halfCircleSegmentCount; i++) { float ng = gContext.mRotationAngle * ((float)(i-1) / (float)(halfCircleSegmentCount -1)); matrix_t rotateVectorMatrix; rotateVectorMatrix.RotationAxis(gContext.mTranslationPlan, ng); vec_t pos; pos.TransformPoint(gContext.mRotationVectorSource, rotateVectorMatrix); pos *= gContext.mScreenFactor; circlePos[i] = worldToPos(pos + gContext.mModel.v.position, gContext.mViewProjection); } drawList->AddConvexPolyFilled(circlePos, halfCircleSegmentCount, 0x801080FF); drawList->AddPolyline(circlePos, halfCircleSegmentCount, 0xFF1080FF, true, 2); ImVec2 destinationPosOnScreen = circlePos[1]; char tmps[512]; ImFormatString(tmps, sizeof(tmps), rotationInfoMask[type - ROTATE_X], (gContext.mRotationAngle/ZPI)*180.f, gContext.mRotationAngle); drawList->AddText(ImVec2(destinationPosOnScreen.x + 15, destinationPosOnScreen.y + 15), 0xFF000000, tmps); drawList->AddText(ImVec2(destinationPosOnScreen.x + 14, destinationPosOnScreen.y + 14), 0xFFFFFFFF, tmps); } } static void DrawHatchedAxis(const vec_t& axis) { for (int j = 1; j < 10; j++) { ImVec2 baseSSpace2 = worldToPos(axis * 0.05f * (float)(j * 2) * gContext.mScreenFactor, gContext.mMVP); ImVec2 worldDirSSpace2 = worldToPos(axis * 0.05f * (float)(j * 2 + 1) * gContext.mScreenFactor, gContext.mMVP); gContext.mDrawList->AddLine(baseSSpace2, worldDirSSpace2, 0x80000000, 6.f); } } static void DrawScaleGizmo(int type) { ImDrawList* drawList = gContext.mDrawList; // colors ImU32 colors[7]; ComputeColors(colors, type, SCALE); // draw vec_t scaleDisplay = { 1.f, 1.f, 1.f, 1.f }; if (gContext.mbUsing) scaleDisplay = gContext.mScale; for (unsigned int i = 0; i < 3; i++) { vec_t dirPlaneX, dirPlaneY, dirAxis; bool belowAxisLimit, belowPlaneLimit; ComputeTripodAxisAndVisibility(i, dirAxis, dirPlaneX, dirPlaneY, belowAxisLimit, belowPlaneLimit); // draw axis if (belowAxisLimit) { ImVec2 baseSSpace = worldToPos(dirAxis * 0.1f * gContext.mScreenFactor, gContext.mMVP); ImVec2 worldDirSSpaceNoScale = worldToPos(dirAxis * gContext.mScreenFactor, gContext.mMVP); ImVec2 worldDirSSpace = worldToPos((dirAxis * scaleDisplay[i]) * gContext.mScreenFactor, gContext.mMVP); if (gContext.mbUsing) { drawList->AddLine(baseSSpace, worldDirSSpaceNoScale, 0xFF404040, 3.f); drawList->AddCircleFilled(worldDirSSpaceNoScale, 6.f, 0xFF404040); } drawList->AddLine(baseSSpace, worldDirSSpace, colors[i + 1], 3.f); drawList->AddCircleFilled(worldDirSSpace, 6.f, colors[i + 1]); if (gContext.mAxisFactor[i] < 0.f) DrawHatchedAxis(dirAxis * scaleDisplay[i]); } } // draw screen cirle drawList->AddCircleFilled(gContext.mScreenSquareCenter, 6.f, colors[0], 32); if (gContext.mbUsing) { //ImVec2 sourcePosOnScreen = worldToPos(gContext.mMatrixOrigin, gContext.mViewProjection); ImVec2 destinationPosOnScreen = worldToPos(gContext.mModel.v.position, gContext.mViewProjection); /*vec_t dif(destinationPosOnScreen.x - sourcePosOnScreen.x, destinationPosOnScreen.y - sourcePosOnScreen.y); dif.Normalize(); dif *= 5.f; drawList->AddCircle(sourcePosOnScreen, 6.f, translationLineColor); drawList->AddCircle(destinationPosOnScreen, 6.f, translationLineColor); drawList->AddLine(ImVec2(sourcePosOnScreen.x + dif.x, sourcePosOnScreen.y + dif.y), ImVec2(destinationPosOnScreen.x - dif.x, destinationPosOnScreen.y - dif.y), translationLineColor, 2.f); */ char tmps[512]; //vec_t deltaInfo = gContext.mModel.v.position - gContext.mMatrixOrigin; int componentInfoIndex = (type - SCALE_X) * 3; ImFormatString(tmps, sizeof(tmps), scaleInfoMask[type - SCALE_X], scaleDisplay[translationInfoIndex[componentInfoIndex]]); drawList->AddText(ImVec2(destinationPosOnScreen.x + 15, destinationPosOnScreen.y + 15), 0xFF000000, tmps); drawList->AddText(ImVec2(destinationPosOnScreen.x + 14, destinationPosOnScreen.y + 14), 0xFFFFFFFF, tmps); } } static void DrawTranslationGizmo(int type) { ImDrawList* drawList = gContext.mDrawList; if (!drawList) return; // colors ImU32 colors[7]; ComputeColors(colors, type, TRANSLATE); const ImVec2 origin = worldToPos(gContext.mModel.v.position, gContext.mViewProjection); // draw bool belowAxisLimit = false; bool belowPlaneLimit = false; for (unsigned int i = 0; i < 3; ++i) { vec_t dirPlaneX, dirPlaneY, dirAxis; ComputeTripodAxisAndVisibility(i, dirAxis, dirPlaneX, dirPlaneY, belowAxisLimit, belowPlaneLimit); // draw axis if (belowAxisLimit) { ImVec2 baseSSpace = worldToPos(dirAxis * 0.1f * gContext.mScreenFactor, gContext.mMVP); ImVec2 worldDirSSpace = worldToPos(dirAxis * gContext.mScreenFactor, gContext.mMVP); drawList->AddLine(baseSSpace, worldDirSSpace, colors[i + 1], 3.f); // Arrow head begin ImVec2 dir(origin - worldDirSSpace); float d = sqrtf(ImLengthSqr(dir)); dir /= d; // Normalize dir *= 6.0f; ImVec2 ortogonalDir(dir.y, -dir.x); // Perpendicular vector ImVec2 a(worldDirSSpace + dir); drawList->AddTriangleFilled(worldDirSSpace - dir, a + ortogonalDir, a - ortogonalDir, colors[i + 1]); // Arrow head end if (gContext.mAxisFactor[i] < 0.f) DrawHatchedAxis(dirAxis); } // draw plane if (belowPlaneLimit) { ImVec2 screenQuadPts[4]; for (int j = 0; j < 4; ++j) { vec_t cornerWorldPos = (dirPlaneX * quadUV[j * 2] + dirPlaneY * quadUV[j * 2 + 1]) * gContext.mScreenFactor; screenQuadPts[j] = worldToPos(cornerWorldPos, gContext.mMVP); } drawList->AddPolyline(screenQuadPts, 4, directionColor[i], true, 1.0f); drawList->AddConvexPolyFilled(screenQuadPts, 4, colors[i + 4]); } } drawList->AddCircleFilled(gContext.mScreenSquareCenter, 6.f, colors[0], 32); if (gContext.mbUsing) { ImVec2 sourcePosOnScreen = worldToPos(gContext.mMatrixOrigin, gContext.mViewProjection); ImVec2 destinationPosOnScreen = worldToPos(gContext.mModel.v.position, gContext.mViewProjection); vec_t dif = { destinationPosOnScreen.x - sourcePosOnScreen.x, destinationPosOnScreen.y - sourcePosOnScreen.y, 0.f, 0.f }; dif.Normalize(); dif *= 5.f; drawList->AddCircle(sourcePosOnScreen, 6.f, translationLineColor); drawList->AddCircle(destinationPosOnScreen, 6.f, translationLineColor); drawList->AddLine(ImVec2(sourcePosOnScreen.x + dif.x, sourcePosOnScreen.y + dif.y), ImVec2(destinationPosOnScreen.x - dif.x, destinationPosOnScreen.y - dif.y), translationLineColor, 2.f); char tmps[512]; vec_t deltaInfo = gContext.mModel.v.position - gContext.mMatrixOrigin; int componentInfoIndex = (type - MOVE_X) * 3; ImFormatString(tmps, sizeof(tmps), translationInfoMask[type - MOVE_X], deltaInfo[translationInfoIndex[componentInfoIndex]], deltaInfo[translationInfoIndex[componentInfoIndex + 1]], deltaInfo[translationInfoIndex[componentInfoIndex + 2]]); drawList->AddText(ImVec2(destinationPosOnScreen.x + 15, destinationPosOnScreen.y + 15), 0xFF000000, tmps); drawList->AddText(ImVec2(destinationPosOnScreen.x + 14, destinationPosOnScreen.y + 14), 0xFFFFFFFF, tmps); } } static bool CanActivate() { if (ImGui::IsMouseClicked(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemActive()) return true; return false; } static void HandleAndDrawLocalBounds(float *bounds, matrix_t *matrix, float *snapValues, OPERATION operation) { ImGuiIO& io = ImGui::GetIO(); ImDrawList* drawList = gContext.mDrawList; // compute best projection axis vec_t axesWorldDirections[3]; vec_t bestAxisWorldDirection = { 0.0f, 0.0f, 0.0f, 0.0f }; int axes[3]; unsigned int numAxes = 1; axes[0] = gContext.mBoundsBestAxis; int bestAxis = axes[0]; if (!gContext.mbUsingBounds) { numAxes = 0; float bestDot = 0.f; for (unsigned int i = 0; i < 3; i++) { vec_t dirPlaneNormalWorld; dirPlaneNormalWorld.TransformVector(directionUnary[i], gContext.mModelSource); dirPlaneNormalWorld.Normalize(); float dt = fabsf( Dot(Normalized(gContext.mCameraEye - gContext.mModelSource.v.position), dirPlaneNormalWorld) ); if ( dt >= bestDot ) { bestDot = dt; bestAxis = i; bestAxisWorldDirection = dirPlaneNormalWorld; } if( dt >= 0.1f ) { axes[numAxes] = i; axesWorldDirections[numAxes] = dirPlaneNormalWorld; ++numAxes; } } } if( numAxes == 0 ) { axes[0] = bestAxis; axesWorldDirections[0] = bestAxisWorldDirection; numAxes = 1; } else if( bestAxis != axes[0] ) { unsigned int bestIndex = 0; for (unsigned int i = 0; i < numAxes; i++) { if( axes[i] == bestAxis ) { bestIndex = i; break; } } int tempAxis = axes[0]; axes[0] = axes[bestIndex]; axes[bestIndex] = tempAxis; vec_t tempDirection = axesWorldDirections[0]; axesWorldDirections[0] = axesWorldDirections[bestIndex]; axesWorldDirections[bestIndex] = tempDirection; } for (unsigned int axisIndex = 0; axisIndex < numAxes; ++axisIndex) { bestAxis = axes[axisIndex]; bestAxisWorldDirection = axesWorldDirections[axisIndex]; // corners vec_t aabb[4]; int secondAxis = (bestAxis + 1) % 3; int thirdAxis = (bestAxis + 2) % 3; for (int i = 0; i < 4; i++) { aabb[i][3] = aabb[i][bestAxis] = 0.f; aabb[i][secondAxis] = bounds[secondAxis + 3 * (i >> 1)]; aabb[i][thirdAxis] = bounds[thirdAxis + 3 * ((i >> 1) ^ (i & 1))]; } // draw bounds unsigned int anchorAlpha = gContext.mbEnable ? 0xFF000000 : 0x80000000; matrix_t boundsMVP = gContext.mModelSource * gContext.mViewProjection; for (int i = 0; i < 4;i++) { ImVec2 worldBound1 = worldToPos(aabb[i], boundsMVP); ImVec2 worldBound2 = worldToPos(aabb[(i+1)%4], boundsMVP); if( !IsInContextRect( worldBound1 ) || !IsInContextRect( worldBound2 ) ) { continue; } float boundDistance = sqrtf(ImLengthSqr(worldBound1 - worldBound2)); int stepCount = (int)(boundDistance / 10.f); stepCount = min( stepCount, 1000 ); float stepLength = 1.f / (float)stepCount; for (int j = 0; j < stepCount; j++) { float t1 = (float)j * stepLength; float t2 = (float)j * stepLength + stepLength * 0.5f; ImVec2 worldBoundSS1 = ImLerp(worldBound1, worldBound2, ImVec2(t1, t1)); ImVec2 worldBoundSS2 = ImLerp(worldBound1, worldBound2, ImVec2(t2, t2)); //drawList->AddLine(worldBoundSS1, worldBoundSS2, 0x000000 + anchorAlpha, 3.f); drawList->AddLine(worldBoundSS1, worldBoundSS2, 0xAAAAAA + anchorAlpha, 2.f); } vec_t midPoint = (aabb[i] + aabb[(i + 1) % 4] ) * 0.5f; ImVec2 midBound = worldToPos(midPoint, boundsMVP); static const float AnchorBigRadius = 8.f; static const float AnchorSmallRadius = 6.f; bool overBigAnchor = ImLengthSqr(worldBound1 - io.MousePos) <= (AnchorBigRadius*AnchorBigRadius); bool overSmallAnchor = ImLengthSqr(midBound - io.MousePos) <= (AnchorBigRadius*AnchorBigRadius); int type = NONE; vec_t gizmoHitProportion; switch (operation) { case TRANSLATE: type = GetMoveType(&gizmoHitProportion); break; case ROTATE: type = GetRotateType(); break; case SCALE: type = GetScaleType(); break; case BOUNDS: break; } if (type != NONE) { overBigAnchor = false; overSmallAnchor = false; } unsigned int bigAnchorColor = overBigAnchor ? selectionColor : (0xAAAAAA + anchorAlpha); unsigned int smallAnchorColor = overSmallAnchor ? selectionColor : (0xAAAAAA + anchorAlpha); drawList->AddCircleFilled(worldBound1, AnchorBigRadius, 0xFF000000); drawList->AddCircleFilled(worldBound1, AnchorBigRadius-1.2f, bigAnchorColor); drawList->AddCircleFilled(midBound, AnchorSmallRadius, 0xFF000000); drawList->AddCircleFilled(midBound, AnchorSmallRadius-1.2f, smallAnchorColor); int oppositeIndex = (i + 2) % 4; // big anchor on corners if (!gContext.mbUsingBounds && gContext.mbEnable && overBigAnchor && CanActivate()) { gContext.mBoundsPivot.TransformPoint(aabb[(i + 2) % 4], gContext.mModelSource); gContext.mBoundsAnchor.TransformPoint(aabb[i], gContext.mModelSource); gContext.mBoundsPlan = BuildPlan(gContext.mBoundsAnchor, bestAxisWorldDirection); gContext.mBoundsBestAxis = bestAxis; gContext.mBoundsAxis[0] = secondAxis; gContext.mBoundsAxis[1] = thirdAxis; gContext.mBoundsLocalPivot.Set(0.f); gContext.mBoundsLocalPivot[secondAxis] = aabb[oppositeIndex][secondAxis]; gContext.mBoundsLocalPivot[thirdAxis] = aabb[oppositeIndex][thirdAxis]; gContext.mbUsingBounds = true; gContext.mBoundsMatrix = gContext.mModelSource; } // small anchor on middle of segment if (!gContext.mbUsingBounds && gContext.mbEnable && overSmallAnchor && CanActivate()) { vec_t midPointOpposite = (aabb[(i + 2) % 4] + aabb[(i + 3) % 4]) * 0.5f; gContext.mBoundsPivot.TransformPoint(midPointOpposite, gContext.mModelSource); gContext.mBoundsAnchor.TransformPoint(midPoint, gContext.mModelSource); gContext.mBoundsPlan = BuildPlan(gContext.mBoundsAnchor, bestAxisWorldDirection); gContext.mBoundsBestAxis = bestAxis; int indices[] = { secondAxis , thirdAxis }; gContext.mBoundsAxis[0] = indices[i%2]; gContext.mBoundsAxis[1] = -1; gContext.mBoundsLocalPivot.Set(0.f); gContext.mBoundsLocalPivot[gContext.mBoundsAxis[0]] = aabb[oppositeIndex][indices[i % 2]];// bounds[gContext.mBoundsAxis[0]] * (((i + 1) & 2) ? 1.f : -1.f); gContext.mbUsingBounds = true; gContext.mBoundsMatrix = gContext.mModelSource; } } if (gContext.mbUsingBounds) { matrix_t scale; scale.SetToIdentity(); // compute projected mouse position on plan const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mBoundsPlan); vec_t newPos = gContext.mRayOrigin + gContext.mRayVector * len; // compute a reference and delta vectors base on mouse move vec_t deltaVector = (newPos - gContext.mBoundsPivot).Abs(); vec_t referenceVector = (gContext.mBoundsAnchor - gContext.mBoundsPivot).Abs(); // for 1 or 2 axes, compute a ratio that's used for scale and snap it based on resulting length for (int i = 0; i < 2; i++) { int axisIndex1 = gContext.mBoundsAxis[i]; if (axisIndex1 == -1) continue; float ratioAxis = 1.f; vec_t axisDir = gContext.mBoundsMatrix.component[axisIndex1].Abs(); float dtAxis = axisDir.Dot(referenceVector); float boundSize = bounds[axisIndex1 + 3] - bounds[axisIndex1]; if (dtAxis > FLT_EPSILON) ratioAxis = axisDir.Dot(deltaVector) / dtAxis; if (snapValues) { float length = boundSize * ratioAxis; ComputeSnap(&length, snapValues[axisIndex1]); if (boundSize > FLT_EPSILON) ratioAxis = length / boundSize; } scale.component[axisIndex1] *= ratioAxis; } // transform matrix matrix_t preScale, postScale; preScale.Translation(-gContext.mBoundsLocalPivot); postScale.Translation(gContext.mBoundsLocalPivot); matrix_t res = preScale * scale * postScale * gContext.mBoundsMatrix; *matrix = res; // info text char tmps[512]; ImVec2 destinationPosOnScreen = worldToPos(gContext.mModel.v.position, gContext.mViewProjection); ImFormatString(tmps, sizeof(tmps), "X: %.2f Y: %.2f Z:%.2f" , (bounds[3] - bounds[0]) * gContext.mBoundsMatrix.component[0].Length() * scale.component[0].Length() , (bounds[4] - bounds[1]) * gContext.mBoundsMatrix.component[1].Length() * scale.component[1].Length() , (bounds[5] - bounds[2]) * gContext.mBoundsMatrix.component[2].Length() * scale.component[2].Length() ); drawList->AddText(ImVec2(destinationPosOnScreen.x + 15, destinationPosOnScreen.y + 15), 0xFF000000, tmps); drawList->AddText(ImVec2(destinationPosOnScreen.x + 14, destinationPosOnScreen.y + 14), 0xFFFFFFFF, tmps); } if (!io.MouseDown[0]) gContext.mbUsingBounds = false; if( gContext.mbUsingBounds ) break; } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // static int GetScaleType() { ImGuiIO& io = ImGui::GetIO(); int type = NONE; // screen if (io.MousePos.x >= gContext.mScreenSquareMin.x && io.MousePos.x <= gContext.mScreenSquareMax.x && io.MousePos.y >= gContext.mScreenSquareMin.y && io.MousePos.y <= gContext.mScreenSquareMax.y) type = SCALE_XYZ; // compute for (unsigned int i = 0; i < 3 && type == NONE; i++) { vec_t dirPlaneX, dirPlaneY, dirAxis; bool belowAxisLimit, belowPlaneLimit; ComputeTripodAxisAndVisibility(i, dirAxis, dirPlaneX, dirPlaneY, belowAxisLimit, belowPlaneLimit); dirAxis.TransformVector(gContext.mModel); dirPlaneX.TransformVector(gContext.mModel); dirPlaneY.TransformVector(gContext.mModel); const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, BuildPlan(gContext.mModel.v.position, dirAxis)); vec_t posOnPlan = gContext.mRayOrigin + gContext.mRayVector * len; const ImVec2 posOnPlanScreen = worldToPos(posOnPlan, gContext.mViewProjection); const ImVec2 axisStartOnScreen = worldToPos(gContext.mModel.v.position + dirAxis * gContext.mScreenFactor * 0.1f, gContext.mViewProjection); const ImVec2 axisEndOnScreen = worldToPos(gContext.mModel.v.position + dirAxis * gContext.mScreenFactor, gContext.mViewProjection); vec_t closestPointOnAxis = PointOnSegment(makeVect(posOnPlanScreen), makeVect(axisStartOnScreen), makeVect(axisEndOnScreen)); if ((closestPointOnAxis - makeVect(posOnPlanScreen)).Length() < 12.f) // pixel size type = SCALE_X + i; } return type; } static int GetRotateType() { ImGuiIO& io = ImGui::GetIO(); int type = NONE; vec_t deltaScreen = { io.MousePos.x - gContext.mScreenSquareCenter.x, io.MousePos.y - gContext.mScreenSquareCenter.y, 0.f, 0.f }; float dist = deltaScreen.Length(); if (dist >= (gContext.mRadiusSquareCenter - 1.0f) && dist < (gContext.mRadiusSquareCenter + 1.0f)) type = ROTATE_SCREEN; const vec_t planNormals[] = { gContext.mModel.v.right, gContext.mModel.v.up, gContext.mModel.v.dir}; for (unsigned int i = 0; i < 3 && type == NONE; i++) { // pickup plan vec_t pickupPlan = BuildPlan(gContext.mModel.v.position, planNormals[i]); const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, pickupPlan); vec_t localPos = gContext.mRayOrigin + gContext.mRayVector * len - gContext.mModel.v.position; if (Dot(Normalized(localPos), gContext.mRayVector) > FLT_EPSILON) continue; vec_t idealPosOnCircle = Normalized(localPos); idealPosOnCircle.TransformVector(gContext.mModelInverse); ImVec2 idealPosOnCircleScreen = worldToPos(idealPosOnCircle * gContext.mScreenFactor, gContext.mMVP); //gContext.mDrawList->AddCircle(idealPosOnCircleScreen, 5.f, 0xFFFFFFFF); ImVec2 distanceOnScreen = idealPosOnCircleScreen - io.MousePos; float distance = makeVect(distanceOnScreen).Length(); if (distance < 8.f) // pixel size type = ROTATE_X + i; } return type; } static int GetMoveType(vec_t *gizmoHitProportion) { ImGuiIO& io = ImGui::GetIO(); int type = NONE; // screen if (io.MousePos.x >= gContext.mScreenSquareMin.x && io.MousePos.x <= gContext.mScreenSquareMax.x && io.MousePos.y >= gContext.mScreenSquareMin.y && io.MousePos.y <= gContext.mScreenSquareMax.y) type = MOVE_SCREEN; // compute for (unsigned int i = 0; i < 3 && type == NONE; i++) { vec_t dirPlaneX, dirPlaneY, dirAxis; bool belowAxisLimit, belowPlaneLimit; ComputeTripodAxisAndVisibility(i, dirAxis, dirPlaneX, dirPlaneY, belowAxisLimit, belowPlaneLimit); dirAxis.TransformVector(gContext.mModel); dirPlaneX.TransformVector(gContext.mModel); dirPlaneY.TransformVector(gContext.mModel); const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, BuildPlan(gContext.mModel.v.position, dirAxis)); vec_t posOnPlan = gContext.mRayOrigin + gContext.mRayVector * len; const ImVec2 posOnPlanScreen = worldToPos(posOnPlan, gContext.mViewProjection); const ImVec2 axisStartOnScreen = worldToPos(gContext.mModel.v.position + dirAxis * gContext.mScreenFactor * 0.1f, gContext.mViewProjection); const ImVec2 axisEndOnScreen = worldToPos(gContext.mModel.v.position + dirAxis * gContext.mScreenFactor, gContext.mViewProjection); vec_t closestPointOnAxis = PointOnSegment(makeVect(posOnPlanScreen), makeVect(axisStartOnScreen), makeVect(axisEndOnScreen)); if ((closestPointOnAxis - makeVect(posOnPlanScreen)).Length() < 12.f) // pixel size type = MOVE_X + i; const float dx = dirPlaneX.Dot3((posOnPlan - gContext.mModel.v.position) * (1.f / gContext.mScreenFactor)); const float dy = dirPlaneY.Dot3((posOnPlan - gContext.mModel.v.position) * (1.f / gContext.mScreenFactor)); if (belowPlaneLimit && dx >= quadUV[0] && dx <= quadUV[4] && dy >= quadUV[1] && dy <= quadUV[3]) type = MOVE_YZ + i; if (gizmoHitProportion) *gizmoHitProportion = makeVect(dx, dy, 0.f); } return type; } static void HandleTranslation(float *matrix, float *deltaMatrix, int& type, float *snap) { ImGuiIO& io = ImGui::GetIO(); bool applyRotationLocaly = gContext.mMode == LOCAL || type == MOVE_SCREEN; // move if (gContext.mbUsing) { ImGui::CaptureMouseFromApp(); const float len = fabsf(IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan)); // near plan vec_t newPos = gContext.mRayOrigin + gContext.mRayVector * len; // compute delta vec_t newOrigin = newPos - gContext.mRelativeOrigin * gContext.mScreenFactor; vec_t delta = newOrigin - gContext.mModel.v.position; // 1 axis constraint if (gContext.mCurrentOperation >= MOVE_X && gContext.mCurrentOperation <= MOVE_Z) { int axisIndex = gContext.mCurrentOperation - MOVE_X; const vec_t& axisValue = *(vec_t*)&gContext.mModel.m[axisIndex]; float lengthOnAxis = Dot(axisValue, delta); delta = axisValue * lengthOnAxis; } // snap if (snap) { vec_t cumulativeDelta = gContext.mModel.v.position + delta - gContext.mMatrixOrigin; if (applyRotationLocaly) { matrix_t modelSourceNormalized = gContext.mModelSource; modelSourceNormalized.OrthoNormalize(); matrix_t modelSourceNormalizedInverse; modelSourceNormalizedInverse.Inverse(modelSourceNormalized); cumulativeDelta.TransformVector(modelSourceNormalizedInverse); ComputeSnap(cumulativeDelta, snap); cumulativeDelta.TransformVector(modelSourceNormalized); } else { ComputeSnap(cumulativeDelta, snap); } delta = gContext.mMatrixOrigin + cumulativeDelta - gContext.mModel.v.position; } // compute matrix & delta matrix_t deltaMatrixTranslation; deltaMatrixTranslation.Translation(delta); if (deltaMatrix) memcpy(deltaMatrix, deltaMatrixTranslation.m16, sizeof(float) * 16); matrix_t res = gContext.mModelSource * deltaMatrixTranslation; *(matrix_t*)matrix = res; if (!io.MouseDown[0]) gContext.mbUsing = false; type = gContext.mCurrentOperation; } else { // find new possible way to move vec_t gizmoHitProportion; type = GetMoveType(&gizmoHitProportion); if(type != NONE) { ImGui::CaptureMouseFromApp(); } if (CanActivate() && type != NONE) { gContext.mbUsing = true; gContext.mCurrentOperation = type; vec_t movePlanNormal[] = { gContext.mModel.v.right, gContext.mModel.v.up, gContext.mModel.v.dir, gContext.mModel.v.right, gContext.mModel.v.up, gContext.mModel.v.dir, -gContext.mCameraDir }; vec_t cameraToModelNormalized = Normalized(gContext.mModel.v.position - gContext.mCameraEye); for (unsigned int i = 0; i < 3; i++) { vec_t orthoVector = Cross(movePlanNormal[i], cameraToModelNormalized); movePlanNormal[i].Cross(orthoVector); movePlanNormal[i].Normalize(); } // pickup plan gContext.mTranslationPlan = BuildPlan(gContext.mModel.v.position, movePlanNormal[type - MOVE_X]); const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan); gContext.mTranslationPlanOrigin = gContext.mRayOrigin + gContext.mRayVector * len; gContext.mMatrixOrigin = gContext.mModel.v.position; gContext.mRelativeOrigin = (gContext.mTranslationPlanOrigin - gContext.mModel.v.position) * (1.f / gContext.mScreenFactor); } } } static void HandleScale(float *matrix, float *deltaMatrix, int& type, float *snap) { ImGuiIO& io = ImGui::GetIO(); if (!gContext.mbUsing) { // find new possible way to scale type = GetScaleType(); if(type != NONE) { ImGui::CaptureMouseFromApp(); } if (CanActivate() && type != NONE) { gContext.mbUsing = true; gContext.mCurrentOperation = type; 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 }; // pickup plan gContext.mTranslationPlan = BuildPlan(gContext.mModel.v.position, movePlanNormal[type - SCALE_X]); const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan); gContext.mTranslationPlanOrigin = gContext.mRayOrigin + gContext.mRayVector * len; gContext.mMatrixOrigin = gContext.mModel.v.position; gContext.mScale.Set(1.f, 1.f, 1.f); gContext.mRelativeOrigin = (gContext.mTranslationPlanOrigin - gContext.mModel.v.position) * (1.f / gContext.mScreenFactor); gContext.mScaleValueOrigin = makeVect(gContext.mModelSource.v.right.Length(), gContext.mModelSource.v.up.Length(), gContext.mModelSource.v.dir.Length()); gContext.mSaveMousePosx = io.MousePos.x; } } // scale if (gContext.mbUsing) { ImGui::CaptureMouseFromApp(); const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan); vec_t newPos = gContext.mRayOrigin + gContext.mRayVector * len; vec_t newOrigin = newPos - gContext.mRelativeOrigin * gContext.mScreenFactor; vec_t delta = newOrigin - gContext.mModel.v.position; // 1 axis constraint if (gContext.mCurrentOperation >= SCALE_X && gContext.mCurrentOperation <= SCALE_Z) { int axisIndex = gContext.mCurrentOperation - SCALE_X; const vec_t& axisValue = *(vec_t*)&gContext.mModel.m[axisIndex]; float lengthOnAxis = Dot(axisValue, delta); delta = axisValue * lengthOnAxis; vec_t baseVector = gContext.mTranslationPlanOrigin - gContext.mModel.v.position; float ratio = Dot(axisValue, baseVector + delta) / Dot(axisValue, baseVector); gContext.mScale[axisIndex] = max(ratio, 0.001f); } else { float scaleDelta = (io.MousePos.x - gContext.mSaveMousePosx) * 0.01f; gContext.mScale.Set(max(1.f + scaleDelta, 0.001f)); } // snap if (snap) { float scaleSnap[] = { snap[0], snap[0], snap[0] }; ComputeSnap(gContext.mScale, scaleSnap); } // no 0 allowed for (int i = 0; i < 3;i++) gContext.mScale[i] = max(gContext.mScale[i], 0.001f); // compute matrix & delta matrix_t deltaMatrixScale; deltaMatrixScale.Scale(gContext.mScale * gContext.mScaleValueOrigin); matrix_t res = deltaMatrixScale * gContext.mModel; *(matrix_t*)matrix = res; if (deltaMatrix) { deltaMatrixScale.Scale(gContext.mScale); memcpy(deltaMatrix, deltaMatrixScale.m16, sizeof(float) * 16); } if (!io.MouseDown[0]) gContext.mbUsing = false; type = gContext.mCurrentOperation; } } static void HandleRotation(float *matrix, float *deltaMatrix, int& type, float *snap) { ImGuiIO& io = ImGui::GetIO(); bool applyRotationLocaly = gContext.mMode == LOCAL; if (!gContext.mbUsing) { type = GetRotateType(); if(type != NONE) { ImGui::CaptureMouseFromApp(); } if (type == ROTATE_SCREEN) { applyRotationLocaly = true; } if (CanActivate() && type != NONE) { gContext.mbUsing = true; gContext.mCurrentOperation = type; const vec_t rotatePlanNormal[] = { gContext.mModel.v.right, gContext.mModel.v.up, gContext.mModel.v.dir, -gContext.mCameraDir }; // pickup plan if (applyRotationLocaly) { gContext.mTranslationPlan = BuildPlan(gContext.mModel.v.position, rotatePlanNormal[type - ROTATE_X]); } else { gContext.mTranslationPlan = BuildPlan(gContext.mModelSource.v.position, directionUnary[type - ROTATE_X]); } const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mTranslationPlan); vec_t localPos = gContext.mRayOrigin + gContext.mRayVector * len - gContext.mModel.v.position; gContext.mRotationVectorSource = Normalized(localPos); gContext.mRotationAngleOrigin = ComputeAngleOnPlan(); } } // rotation if (gContext.mbUsing) { ImGui::CaptureMouseFromApp(); gContext.mRotationAngle = ComputeAngleOnPlan(); if (snap) { float snapInRadian = snap[0] * DEG2RAD; ComputeSnap(&gContext.mRotationAngle, snapInRadian); } vec_t rotationAxisLocalSpace; rotationAxisLocalSpace.TransformVector(makeVect(gContext.mTranslationPlan.x, gContext.mTranslationPlan.y, gContext.mTranslationPlan.z, 0.f), gContext.mModelInverse); rotationAxisLocalSpace.Normalize(); matrix_t deltaRotation; deltaRotation.RotationAxis(rotationAxisLocalSpace, gContext.mRotationAngle - gContext.mRotationAngleOrigin); gContext.mRotationAngleOrigin = gContext.mRotationAngle; matrix_t scaleOrigin; scaleOrigin.Scale(gContext.mModelScaleOrigin); if (applyRotationLocaly) { *(matrix_t*)matrix = scaleOrigin * deltaRotation * gContext.mModel; } else { matrix_t res = gContext.mModelSource; res.v.position.Set(0.f); *(matrix_t*)matrix = res * deltaRotation; ((matrix_t*)matrix)->v.position = gContext.mModelSource.v.position; } if (deltaMatrix) { *(matrix_t*)deltaMatrix = gContext.mModelInverse * deltaRotation * gContext.mModel; } if (!io.MouseDown[0]) gContext.mbUsing = false; type = gContext.mCurrentOperation; } } void DecomposeMatrixToComponents(const float *matrix, float *translation, float *rotation, float *scale) { matrix_t mat = *(matrix_t*)matrix; scale[0] = mat.v.right.Length(); scale[1] = mat.v.up.Length(); scale[2] = mat.v.dir.Length(); mat.OrthoNormalize(); rotation[0] = RAD2DEG * atan2f(mat.m[1][2], mat.m[2][2]); 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])); rotation[2] = RAD2DEG * atan2f(mat.m[0][1], mat.m[0][0]); translation[0] = mat.v.position.x; translation[1] = mat.v.position.y; translation[2] = mat.v.position.z; } void RecomposeMatrixFromComponents(const float *translation, const float *rotation, const float *scale, float *matrix) { matrix_t& mat = *(matrix_t*)matrix; matrix_t rot[3]; for (int i = 0; i < 3;i++) rot[i].RotationAxis(directionUnary[i], rotation[i] * DEG2RAD); mat = rot[0] * rot[1] * rot[2]; float validScale[3]; for (int i = 0; i < 3; i++) { if (fabsf(scale[i]) < FLT_EPSILON) validScale[i] = 0.001f; else validScale[i] = scale[i]; } mat.v.right *= validScale[0]; mat.v.up *= validScale[1]; mat.v.dir *= validScale[2]; mat.v.position.Set(translation[0], translation[1], translation[2], 1.f); } void Manipulate(const float *view, const float *projection, OPERATION operation, MODE mode, float *matrix, float *deltaMatrix, float *snap, float *localBounds, float *boundsSnap) { ComputeContext(view, projection, matrix, mode); // set delta to identity if (deltaMatrix) ((matrix_t*)deltaMatrix)->SetToIdentity(); // behind camera vec_t camSpacePosition; camSpacePosition.TransformPoint(makeVect(0.f, 0.f, 0.f), gContext.mMVP); if (!gContext.mIsOrthographic && camSpacePosition.z < 0.001f) return; // -- int type = NONE; if (gContext.mbEnable) { if (!gContext.mbUsingBounds) { switch (operation) { case ROTATE: HandleRotation(matrix, deltaMatrix, type, snap); break; case TRANSLATE: HandleTranslation(matrix, deltaMatrix, type, snap); break; case SCALE: HandleScale(matrix, deltaMatrix, type, snap); break; case BOUNDS: break; } } } if (localBounds && !gContext.mbUsing) HandleAndDrawLocalBounds(localBounds, (matrix_t*)matrix, boundsSnap, operation); if (!gContext.mbUsingBounds) { switch (operation) { case ROTATE: DrawRotationGizmo(type); break; case TRANSLATE: DrawTranslationGizmo(type); break; case SCALE: DrawScaleGizmo(type); break; case BOUNDS: break; } } } void DrawCube(const float *view, const float *projection, const float *matrix) { matrix_t viewInverse; viewInverse.Inverse(*(matrix_t*)view); const matrix_t& model = *(matrix_t*)matrix; matrix_t res = *(matrix_t*)matrix * *(matrix_t*)view * *(matrix_t*)projection; for (int iFace = 0; iFace < 6; iFace++) { const int normalIndex = (iFace % 3); const int perpXIndex = (normalIndex + 1) % 3; const int perpYIndex = (normalIndex + 2) % 3; const float invert = (iFace > 2) ? -1.f : 1.f; const vec_t faceCoords[4] = { directionUnary[normalIndex] + directionUnary[perpXIndex] + directionUnary[perpYIndex], directionUnary[normalIndex] + directionUnary[perpXIndex] - directionUnary[perpYIndex], directionUnary[normalIndex] - directionUnary[perpXIndex] - directionUnary[perpYIndex], directionUnary[normalIndex] - directionUnary[perpXIndex] + directionUnary[perpYIndex], }; // clipping bool skipFace = false; for (unsigned int iCoord = 0; iCoord < 4; iCoord++) { vec_t camSpacePosition; camSpacePosition.TransformPoint(faceCoords[iCoord] * 0.5f * invert, gContext.mMVP); if (camSpacePosition.z < 0.001f) { skipFace = true; break; } } if (skipFace) continue; // 3D->2D ImVec2 faceCoordsScreen[4]; for (unsigned int iCoord = 0; iCoord < 4; iCoord++) faceCoordsScreen[iCoord] = worldToPos(faceCoords[iCoord] * 0.5f * invert, res); // back face culling vec_t cullPos, cullNormal; cullPos.TransformPoint(faceCoords[0] * 0.5f * invert, model); cullNormal.TransformVector(directionUnary[normalIndex] * invert, model); float dt = Dot(Normalized(cullPos - viewInverse.v.position), Normalized(cullNormal)); if (dt>0.f) continue; // draw face with lighter color gContext.mDrawList->AddConvexPolyFilled(faceCoordsScreen, 4, directionColor[normalIndex] | 0x808080); } } void DrawGrid(const float *view, const float *projection, const float *matrix, const float gridSize) { matrix_t res = *(matrix_t*)matrix * *(matrix_t*)view * *(matrix_t*)projection; for (float f = -gridSize; f <= gridSize; f += 1.f) { gContext.mDrawList->AddLine(worldToPos(makeVect(f, 0.f, -gridSize), res), worldToPos(makeVect(f, 0.f, gridSize), res), 0xFF808080); gContext.mDrawList->AddLine(worldToPos(makeVect(-gridSize, 0.f, f), res), worldToPos(makeVect(gridSize, 0.f, f), res), 0xFF808080); } } }; ================================================ FILE: src/imgui/ImGuizmo.h ================================================ // https://github.com/CedricGuillemet/ImGuizmo // v 1.61 WIP // // The MIT License(MIT) // // Copyright(c) 2016 Cedric Guillemet // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files(the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions : // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. // // ------------------------------------------------------------------------------------------- // History : // 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. // 2016/09/09 Hatched negative axis. Snapping. Documentation update. // 2016/09/04 Axis switch and translation plan autohiding. Scale transform stability improved // 2016/09/01 Mogwai changed to Manipulate. Draw debug cube. Fixed inverted scale. Mixing scale and translation/rotation gives bad results. // 2016/08/31 First version // // ------------------------------------------------------------------------------------------- // Future (no order): // // - Multi view // - display rotation/translation/scale infos in local/world space and not only local // - finish local/world matrix application // - OPERATION as bitmask // // ------------------------------------------------------------------------------------------- // Example #if 0 void EditTransform(const Camera& camera, matrix_t& matrix) { static ImGuizmo::OPERATION mCurrentGizmoOperation(ImGuizmo::ROTATE); static ImGuizmo::MODE mCurrentGizmoMode(ImGuizmo::WORLD); if (ImGui::IsKeyPressed(90)) mCurrentGizmoOperation = ImGuizmo::TRANSLATE; if (ImGui::IsKeyPressed(69)) mCurrentGizmoOperation = ImGuizmo::ROTATE; if (ImGui::IsKeyPressed(82)) // r Key mCurrentGizmoOperation = ImGuizmo::SCALE; if (ImGui::RadioButton("Translate", mCurrentGizmoOperation == ImGuizmo::TRANSLATE)) mCurrentGizmoOperation = ImGuizmo::TRANSLATE; ImGui::SameLine(); if (ImGui::RadioButton("Rotate", mCurrentGizmoOperation == ImGuizmo::ROTATE)) mCurrentGizmoOperation = ImGuizmo::ROTATE; ImGui::SameLine(); if (ImGui::RadioButton("Scale", mCurrentGizmoOperation == ImGuizmo::SCALE)) mCurrentGizmoOperation = ImGuizmo::SCALE; float matrixTranslation[3], matrixRotation[3], matrixScale[3]; ImGuizmo::DecomposeMatrixToComponents(matrix.m16, matrixTranslation, matrixRotation, matrixScale); ImGui::InputFloat3("Tr", matrixTranslation, 3); ImGui::InputFloat3("Rt", matrixRotation, 3); ImGui::InputFloat3("Sc", matrixScale, 3); ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, matrix.m16); if (mCurrentGizmoOperation != ImGuizmo::SCALE) { if (ImGui::RadioButton("Local", mCurrentGizmoMode == ImGuizmo::LOCAL)) mCurrentGizmoMode = ImGuizmo::LOCAL; ImGui::SameLine(); if (ImGui::RadioButton("World", mCurrentGizmoMode == ImGuizmo::WORLD)) mCurrentGizmoMode = ImGuizmo::WORLD; } static bool useSnap(false); if (ImGui::IsKeyPressed(83)) useSnap = !useSnap; ImGui::Checkbox("", &useSnap); ImGui::SameLine(); vec_t snap; switch (mCurrentGizmoOperation) { case ImGuizmo::TRANSLATE: snap = config.mSnapTranslation; ImGui::InputFloat3("Snap", &snap.x); break; case ImGuizmo::ROTATE: snap = config.mSnapRotation; ImGui::InputFloat("Angle Snap", &snap.x); break; case ImGuizmo::SCALE: snap = config.mSnapScale; ImGui::InputFloat("Scale Snap", &snap.x); break; } ImGuiIO& io = ImGui::GetIO(); ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y); ImGuizmo::Manipulate(camera.mView.m16, camera.mProjection.m16, mCurrentGizmoOperation, mCurrentGizmoMode, matrix.m16, NULL, useSnap ? &snap.x : NULL); } #endif #pragma once #ifdef USE_IMGUI_API #include "imconfig.h" #endif #ifndef IMGUI_API #define IMGUI_API #endif namespace ImGuizmo { // call inside your own window and before Manipulate() in order to draw gizmo to that window. IMGUI_API void SetDrawlist(); // call BeginFrame right after ImGui_XXXX_NewFrame(); IMGUI_API void BeginFrame(); // return true if mouse cursor is over any gizmo control (axis, plan or screen component) IMGUI_API bool IsOver(); // return true if mouse IsOver or if the gizmo is in moving state IMGUI_API bool IsUsing(); // enable/disable the gizmo. Stay in the state until next call to Enable. // gizmo is rendered with gray half transparent color when disabled IMGUI_API void Enable(bool enable); // helper functions for manualy editing translation/rotation/scale with an input float // translation, rotation and scale float points to 3 floats each // Angles are in degrees (more suitable for human editing) // example: // float matrixTranslation[3], matrixRotation[3], matrixScale[3]; // ImGuizmo::DecomposeMatrixToComponents(gizmoMatrix.m16, matrixTranslation, matrixRotation, matrixScale); // ImGui::InputFloat3("Tr", matrixTranslation, 3); // ImGui::InputFloat3("Rt", matrixRotation, 3); // ImGui::InputFloat3("Sc", matrixScale, 3); // ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, gizmoMatrix.m16); // // These functions have some numerical stability issues for now. Use with caution. IMGUI_API void DecomposeMatrixToComponents(const float *matrix, float *translation, float *rotation, float *scale); IMGUI_API void RecomposeMatrixFromComponents(const float *translation, const float *rotation, const float *scale, float *matrix); IMGUI_API void SetRect(float x, float y, float width, float height); // default is false IMGUI_API void SetOrthographic(bool isOrthographic); // Render a cube with face color corresponding to face normal. Usefull for debug/tests IMGUI_API void DrawCube(const float *view, const float *projection, const float *matrix); IMGUI_API void DrawGrid(const float *view, const float *projection, const float *matrix, const float gridSize); // call it when you want a gizmo // Needs view and projection matrices. // matrix parameter is the source matrix (where will be gizmo be drawn) and might be transformed by the function. Return deltaMatrix is optional // translation is applied in world space enum OPERATION { TRANSLATE, ROTATE, SCALE, BOUNDS, }; enum MODE { LOCAL, WORLD }; IMGUI_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); }; ================================================ FILE: src/imgui/imconfig.hpp ================================================ //----------------------------------------------------------------------------- // COMPILE-TIME OPTIONS FOR DEAR IMGUI // Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure. // You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions. //----------------------------------------------------------------------------- // A) You may edit imconfig.h (and not overwrite it when updating imgui, or maintain a patch/branch with your modifications to imconfig.h) // B) or add configuration directives in your own file and compile with #define IMGUI_USER_CONFIG "myfilename.h" // If you do so you need to make sure that configuration settings are defined consistently _everywhere_ dear imgui is used, which include // 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. // Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts. // 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. //----------------------------------------------------------------------------- #pragma once //---- Define assertion handler. Defaults to calling assert(). //#define IM_ASSERT(_EXPR) MyAssert(_EXPR) //#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts //---- Define attributes of all API symbols declarations, e.g. for DLL under Windows. //#define IMGUI_API __declspec( dllexport ) //#define IMGUI_API __declspec( dllimport ) //---- Don't define obsolete functions/enums names. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. //#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS //---- Don't implement demo windows functionality (ShowDemoWindow()/ShowStyleEditor()/ShowUserGuide() methods will be empty) //---- It is very strongly recommended to NOT disable the demo windows during development. Please read the comments in imgui_demo.cpp. //#define IMGUI_DISABLE_DEMO_WINDOWS //---- Don't implement some functions to reduce linkage requirements. //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. //#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow. //#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function. //#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. //#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. //#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(). //---- Include imgui_user.h at the end of imgui.h as a convenience //#define IMGUI_INCLUDE_IMGUI_USER_H //---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another) //#define IMGUI_USE_BGRA_PACKED_COLOR //---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version // By default the embedded implementations are declared static and not available outside of imgui cpp files. //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION //---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4. // This will be inlined as part of ImVec2 and ImVec4 class declarations. /* #define IM_VEC2_CLASS_EXTRA \ ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \ operator MyVec2() const { return MyVec2(x,y); } #define IM_VEC4_CLASS_EXTRA \ ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \ operator MyVec4() const { return MyVec4(x,y,z,w); } */ //---- Use 32-bit vertex indices (default is 16-bit) to allow meshes with more than 64K vertices. Render function needs to support it. //#define ImDrawIdx unsigned int //---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files. /* namespace ImGui { void MyFunction(const char* name, const MyMatrix44& v); } */ ================================================ FILE: src/imgui/imgui.cpp ================================================ // dear imgui, v1.70 WIP // (main code and documentation) // Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. // Newcomers, read 'Programmer guide' below for notes on how to setup Dear ImGui in your codebase. // Get latest version at https://github.com/ocornut/imgui // Releases change-log at https://github.com/ocornut/imgui/releases // Technical Support for Getting Started https://discourse.dearimgui.org/c/getting-started // Gallery (please post your screenshots/video there!): https://github.com/ocornut/imgui/issues/1269 // Developed by Omar Cornut and every direct or indirect contributors to the GitHub. // See LICENSE.txt for copyright and licensing details (standard MIT License). // This library is free but I need your support to sustain development and maintenance. // Businesses: you can support continued maintenance and development via support contracts or sponsoring, see docs/README. // Individuals: you can support continued maintenance and development via donations or Patreon https://www.patreon.com/imgui. // It is recommended that you don't modify imgui.cpp! It will become difficult for you to update the library. // Note that 'ImGui::' being a namespace, you can add functions into the namespace from your own source files, without // modifying imgui.h or imgui.cpp. You may include imgui_internal.h to access internal data structures, but it doesn't // come with any guarantee of forward compatibility. Discussing your changes on the GitHub Issue Tracker may lead you // to a better solution or official support for them. /* Index of this file: DOCUMENTATION - MISSION STATEMENT - END-USER GUIDE - PROGRAMMER GUIDE (read me!) - Read first. - How to update to a newer version of Dear ImGui. - Getting started with integrating Dear ImGui in your code/engine. - This is how a simple application may look like (2 variations). - This is how a simple rendering function may look like. - Using gamepad/keyboard navigation controls. - API BREAKING CHANGES (read me when you update!) - FREQUENTLY ASKED QUESTIONS (FAQ), TIPS - Where is the documentation? - Which version should I get? - Who uses Dear ImGui? - Why the odd dual naming, "Dear ImGui" vs "ImGui"? - How can I tell whether to dispatch mouse/keyboard to imgui or to my application? - How can I display an image? What is ImTextureID, how does it works? - Why are multiple widgets reacting when I interact with a single one? How can I have multiple widgets with the same label or with an empty label? A primer on labels and the ID Stack... - How can I use my own math types instead of ImVec2/ImVec4? - How can I load a different font than the default? - How can I easily use icons in my application? - How can I load multiple fonts? - How can I display and input non-latin characters such as Chinese, Japanese, Korean, Cyrillic? - How can I interact with standard C++ types (such as std::string and std::vector)? - How can I use the drawing facilities without an ImGui window? (using ImDrawList API) - How can I use Dear ImGui on a platform that doesn't have a mouse or a keyboard? (input share, remoting, gamepad) - I integrated Dear ImGui in my engine and the text or lines are blurry.. - I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.. - How can I help? CODE (search for "[SECTION]" in the code to find them) // [SECTION] FORWARD DECLARATIONS // [SECTION] CONTEXT AND MEMORY ALLOCATORS // [SECTION] MAIN USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) // [SECTION] MISC HELPERS/UTILITIES (Maths, String, Format, Hash, File functions) // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) // [SECTION] MISC HELPERS/UTILITIES (Color functions) // [SECTION] ImGuiStorage // [SECTION] ImGuiTextFilter // [SECTION] ImGuiTextBuffer // [SECTION] ImGuiListClipper // [SECTION] RENDER HELPERS // [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) // [SECTION] TOOLTIPS // [SECTION] POPUPS // [SECTION] KEYBOARD/GAMEPAD NAVIGATION // [SECTION] COLUMNS // [SECTION] DRAG AND DROP // [SECTION] LOGGING/CAPTURING // [SECTION] SETTINGS // [SECTION] VIEWPORTS, PLATFORM WINDOWS // [SECTION] DOCKING // [SECTION] PLATFORM DEPENDENT HELPERS // [SECTION] METRICS/DEBUG WINDOW */ //----------------------------------------------------------------------------- // DOCUMENTATION //----------------------------------------------------------------------------- /* MISSION STATEMENT ================= - Easy to use to create code-driven and data-driven tools. - Easy to use to create ad hoc short-lived tools and long-lived, more elaborate tools. - Easy to hack and improve. - Minimize screen real-estate usage. - Minimize setup and maintenance. - Minimize state storage on user side. - Portable, minimize dependencies, run on target (consoles, phones, etc.). - Efficient runtime and memory consumption (NB- we do allocate when "growing" content e.g. creating a window,. opening a tree node for the first time, etc. but a typical frame should not allocate anything). Designed for developers and content-creators, not the typical end-user! Some of the weaknesses includes: - Doesn't look fancy, doesn't animate. - Limited layout features, intricate layouts are typically crafted in code. END-USER GUIDE ============== - Double-click on title bar to collapse window. - Click upper right corner to close a window, available when 'bool* p_open' is passed to ImGui::Begin(). - Click and drag on lower right corner to resize window (double-click to auto fit window to its contents). - Click and drag on any empty space to move window. - TAB/SHIFT+TAB to cycle through keyboard editable fields. - CTRL+Click on a slider or drag box to input value as text. - Use mouse wheel to scroll. - Text editor: - Hold SHIFT or use mouse to select text. - CTRL+Left/Right to word jump. - CTRL+Shift+Left/Right to select words. - CTRL+A our Double-Click to select all. - CTRL+X,CTRL+C,CTRL+V to use OS clipboard/ - CTRL+Z,CTRL+Y to undo/redo. - ESCAPE to revert text to its original value. - You can apply arithmetic operators +,*,/ on numerical values. Use +- to subtract (because - would set a negative value!) - Controls are automatically adjusted for OSX to match standard OSX text editing operations. - General Keyboard controls: enable with ImGuiConfigFlags_NavEnableKeyboard. - General Gamepad controls: enable with ImGuiConfigFlags_NavEnableGamepad. See suggested mappings in imgui.h ImGuiNavInput_ + download PNG/PSD at http://goo.gl/9LgVZW PROGRAMMER GUIDE ================ READ FIRST: - Read the FAQ below this section! - 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 or destruction steps, less superfluous data retention on your side, less state duplication, less state synchronization, less bugs. - Call and read ImGui::ShowDemoWindow() for demo code demonstrating most features. - The library is designed to be built from sources. Avoid pre-compiled binaries and packaged versions. See imconfig.h to configure your build. - Dear ImGui is an implementation of the IMGUI paradigm (immediate-mode graphical user interface, a term coined by Casey Muratori). You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links docs/README.md. - Dear ImGui is a "single pass" rasterizing implementation of the IMGUI paradigm, aimed at ease of use and high-performances. 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, where the UI code is called multiple times ("multiple passes") from a single entry point. There are pros and cons to both approaches. - Our origin are on the top-left. In axis aligned bounding boxes, Min = top-left, Max = bottom-right. - This codebase is also optimized to yield decent performances with typical "Debug" builds settings. - Please make sure you have asserts enabled (IM_ASSERT redirects to assert() by default, but can be redirected). If you get an assert, read the messages and comments around the assert. - 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. - C++: ImVec2/ImVec4 do not expose math operators by default, because it is expected that you use your own math types. See FAQ "How can I use my own math types instead of ImVec2/ImVec4?" for details about setting up imconfig.h for that. However, imgui_internal.h can optionally export math operators for ImVec2/ImVec4, which we use in this codebase. - C++: pay attention that ImVector<> manipulates plain-old-data and does not honor construction/destruction (avoid using it in your code!). HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI: - Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h) - Or maintain your own branch where you have imconfig.h modified. - Read the "API BREAKING CHANGES" section (below). This is where we list occasional API breaking changes. If a function/type has been renamed / or marked obsolete, try to fix the name in your code before it is permanently removed from the public API. If you have a problem with a missing function/symbols, search for its name in the code, there will likely be a comment about it. Please report any issue to the GitHub page! - Try to keep your copy of dear imgui reasonably up to date. GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE: - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library. - Add the Dear ImGui source files to your projects or using your preferred build system. It is recommended you build and statically link the .cpp files as part of your project and not as shared library (DLL). - You can later customize the imconfig.h file to tweak some compile-time behavior, such as integrating imgui types with your own maths types. - When using Dear ImGui, your programming IDE is your friend: follow the declaration of variables, functions and types to find comments about them. - Dear ImGui never touches or knows about your GPU state. The only function that knows about GPU is the draw function that you provide. Effectively it means you can create widgets at any time in your code, regardless of considerations of being in "update" vs "render" phases of your own application. All rendering informatioe are stored into command-lists that you will retrieve after calling ImGui::Render(). - Refer to the bindings and demo applications in the examples/ folder for instruction on how to setup your code. - 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. HOW A SIMPLE APPLICATION MAY LOOK LIKE: EXHIBIT 1: USING THE EXAMPLE BINDINGS (imgui_impl_XXX.cpp files from the examples/ folder). // Application init: create a dear imgui context, setup some options, load fonts ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); // TODO: Set optional io.ConfigFlags values, e.g. 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard' to enable keyboard controls. // TODO: Fill optional fields of the io structure later. // TODO: Load TTF/OTF fonts if you don't want to use the default font. // Initialize helper Platform and Renderer bindings (here we are using imgui_impl_win32 and imgui_impl_dx11) ImGui_ImplWin32_Init(hwnd); ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext); // Application main loop while (true) { // Feed inputs to dear imgui, start new frame ImGui_ImplDX11_NewFrame(); ImGui_ImplWin32_NewFrame(); ImGui::NewFrame(); // Any application code here ImGui::Text("Hello, world!"); // Render dear imgui into screen ImGui::Render(); ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); g_pSwapChain->Present(1, 0); } // Shutdown ImGui_ImplDX11_Shutdown(); ImGui_ImplWin32_Shutdown(); ImGui::DestroyContext(); HOW A SIMPLE APPLICATION MAY LOOK LIKE: EXHIBIT 2: IMPLEMENTING CUSTOM BINDING / CUSTOM ENGINE. // Application init: create a dear imgui context, setup some options, load fonts ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); // TODO: Set optional io.ConfigFlags values, e.g. 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard' to enable keyboard controls. // TODO: Fill optional fields of the io structure later. // TODO: Load TTF/OTF fonts if you don't want to use the default font. // Build and load the texture atlas into a texture // (In the examples/ app this is usually done within the ImGui_ImplXXX_Init() function from one of the demo Renderer) int width, height; unsigned char* pixels = NULL; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // At this point you've got the texture data and you need to upload that your your graphic system: // After we have created the texture, store its pointer/identifier (_in whichever format your engine uses_) in 'io.Fonts->TexID'. // This will be passed back to your via the renderer. Basically ImTextureID == void*. Read FAQ below for details about ImTextureID. MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA32) io.Fonts->TexID = (void*)texture; // Application main loop while (true) { // Setup low-level inputs, e.g. on Win32: calling GetKeyboardState(), or write to those fields from your Windows message handlers, etc. // (In the examples/ app this is usually done within the ImGui_ImplXXX_NewFrame() function from one of the demo Platform bindings) io.DeltaTime = 1.0f/60.0f; // set the time elapsed since the previous frame (in seconds) io.DisplaySize.x = 1920.0f; // set the current display width io.DisplaySize.y = 1280.0f; // set the current display height here io.MousePos = my_mouse_pos; // set the mouse position io.MouseDown[0] = my_mouse_buttons[0]; // set the mouse button states io.MouseDown[1] = my_mouse_buttons[1]; // Call NewFrame(), after this point you can use ImGui::* functions anytime // (So you want to try calling NewFrame() as early as you can in your mainloop to be able to use imgui everywhere) ImGui::NewFrame(); // Most of your application code here ImGui::Text("Hello, world!"); MyGameUpdate(); // may use any ImGui functions, e.g. ImGui::Begin("My window"); ImGui::Text("Hello, world!"); ImGui::End(); MyGameRender(); // may use any ImGui functions as well! // Render imgui, swap buffers // (You want to try calling EndFrame/Render as late as you can, to be able to use imgui in your own game rendering code) ImGui::EndFrame(); ImGui::Render(); ImDrawData* draw_data = ImGui::GetDrawData(); MyImGuiRenderFunction(draw_data); SwapBuffers(); } // Shutdown ImGui::DestroyContext(); HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE: void void MyImGuiRenderFunction(ImDrawData* draw_data) { // TODO: Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled // TODO: Setup viewport covering draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize // TODO: Setup orthographic projection matrix cover draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize // TODO: Setup shader: vertex { float2 pos, float2 uv, u32 color }, fragment shader sample color from 1 texture, multiply by vertex color. for (int n = 0; n < draw_data->CmdListsCount; n++) { const ImDrawList* cmd_list = draw_data->CmdLists[n]; const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data; // vertex buffer generated by ImGui const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data; // index buffer generated by ImGui for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; if (pcmd->UserCallback) { pcmd->UserCallback(cmd_list, pcmd); } else { // The texture for the draw call is specified by pcmd->TextureId. // The vast majority of draw calls will use the imgui texture atlas, which value you have set yourself during initialization. MyEngineBindTexture((MyTexture*)pcmd->TextureId); // We are using scissoring to clip some objects. All low-level graphics API should supports it. // - If your engine doesn't support scissoring yet, you may ignore this at first. You will get some small glitches // (some elements visible outside their bounds) but you can fix that once everything else works! // - Clipping coordinates are provided in imgui coordinates space (from draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize) // In a single viewport application, draw_data->DisplayPos will always be (0,0) and draw_data->DisplaySize will always be == io.DisplaySize. // However, in the interest of supporting multi-viewport applications in the future (see 'viewport' branch on github), // always subtract draw_data->DisplayPos from clipping bounds to convert them to your viewport space. // - Note that pcmd->ClipRect contains Min+Max bounds. Some graphics API may use Min+Max, other may use Min+Size (size being Max-Min) ImVec2 pos = draw_data->DisplayPos; 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)); // Render 'pcmd->ElemCount/3' indexed triangles. // 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. MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer, vtx_buffer); } idx_buffer += pcmd->ElemCount; } } } - The examples/ folders contains many actual implementation of the pseudo-codes above. - When calling NewFrame(), the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags are updated. They tell you if Dear ImGui intends to use your inputs. When a flag is set you want to hide the corresponding inputs 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. - Please read the FAQ below!. Amusingly, it is called a FAQ because people frequently run into the same issues! USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS - The gamepad/keyboard navigation is fairly functional and keeps being improved. - Gamepad support is particularly useful to use dear imgui on a console system (e.g. PS4, Switch, XB1) without a mouse! - You can ask questions and report issues at https://github.com/ocornut/imgui/issues/787 - The initial focus was to support game controllers, but keyboard is becoming increasingly and decently usable. - Gamepad: - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. - Backend: Set io.BackendFlags |= ImGuiBackendFlags_HasGamepad + fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame(). - See 'enum ImGuiNavInput_' in imgui.h for a description of inputs. For each entry of io.NavInputs[], set the following values: 0.0f= not held. 1.0f= fully held. Pass intermediate 0.0f..1.0f values for analog triggers/sticks. - We uses a simple >0.0f test for activation testing, and won't attempt to test for a dead-zone. 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.). - You can download PNG/PSD files depicting the gamepad controls for common controllers at: http://goo.gl/9LgVZW. - 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 to toggle the target. Please reach out if you think the game vs navigation input sharing could be improved. - Keyboard: - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. - When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), the io.WantCaptureKeyboard flag will be set. For more advanced uses, you may want to read from: - io.NavActive: true when a window is focused and it doesn't have the ImGuiWindowFlags_NoNavInputs flag set. - io.NavVisible: true when the navigation cursor is visible (and usually goes false when mouse is used). - or query focus information with e.g. IsWindowFocused(ImGuiFocusedFlags_AnyWindow), IsItemFocused() etc. functions. Please reach out if you think the game vs navigation input sharing could be improved. - Mouse: - PS4 users: Consider emulating a mouse cursor with DualShock4 touch pad or a spare analog stick as a mouse-emulation fallback. - 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. - 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. Enabling ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs dear imgui to move your mouse cursor along with navigation movements. 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. 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. (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!) (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 to set a boolean to ignore your other external mouse positions until the external source is moved again.) API BREAKING CHANGES ==================== Occasionally introducing changes that are breaking the API. We try to make the breakage minor and easy to fix. 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. 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. You can read releases logs https://github.com/ocornut/imgui/releases for more details. (Viewport Branch) - 2018/XX/XX (1.XX) - when multi-viewports are enabled, all positions will be in your natural OS coordinates space. It means that: - reference to hard-coded positions such as in SetNextWindowPos(ImVec2(0,0)) are probably not what you want anymore. you may use GetMainViewport()->Pos to offset hard-coded positions, e.g. SetNextWindowPos(GetMainViewport()->Pos) - likewise io.MousePos and GetMousePos() will use OS coordinates. If you query mouse positions to interact with non-imgui coordinates you will need to offset them, e.g. subtract GetWindowViewport()->Pos. - 2018/XX/XX (1.XX) - Moved IME support functions from io.ImeSetInputScreenPosFn, io.ImeWindowHandle to the PlatformIO api. - 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. - 2019/03/04 (1.69) - renamed GetOverlayDrawList() to GetForegroundDrawList(). Kept redirection function (will obsolete). - 2019/02/26 (1.69) - renamed ImGuiColorEditFlags_RGB/ImGuiColorEditFlags_HSV/ImGuiColorEditFlags_HEX to ImGuiColorEditFlags_DisplayRGB/ImGuiColorEditFlags_DisplayHSV/ImGuiColorEditFlags_DisplayHex. Kept redirection enums (will obsolete). - 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! - 2019/02/01 (1.68) - removed io.DisplayVisibleMin/DisplayVisibleMax (which were marked obsolete and removed from viewport/docking branch already). - 2019/01/06 (1.67) - renamed io.InputCharacters[], marked internal as was always intended. Please don't access directly, and use AddInputCharacter() instead! - 2019/01/06 (1.67) - renamed ImFontAtlas::GlyphRangesBuilder to ImFontGlyphRangesBuilder. Keep redirection typedef (will obsolete). - 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. - 2018/12/10 (1.67) - renamed io.ConfigResizeWindowsFromEdges to io.ConfigWindowsResizeFromEdges as we are doing a large pass on configuration flags. - 2018/10/12 (1.66) - renamed misc/stl/imgui_stl.* to misc/cpp/imgui_stdlib.* in prevision for other C++ helper files. - 2018/09/28 (1.66) - renamed SetScrollHere() to SetScrollHereY(). Kept redirection function (will obsolete). - 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. If you were conveniently using the imgui copy of those STB headers in your project you will have to update your include paths. - 2018/09/05 (1.65) - renamed io.OptCursorBlink/io.ConfigCursorBlink to io.ConfigInputTextCursorBlink. (#1427) - 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. NONE OF THE FUNCTIONS HAVE CHANGED. THE CODE IS SEMANTICALLY 100% IDENTICAL, BUT _EVERY_ FUNCTION HAS BEEN MOVED. Because of this, any local modifications to imgui.cpp will likely conflict when you update. Read docs/CHANGELOG.txt for suggestions. - 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). - 2018/08/21 (1.63) - renamed ImGuiTextEditCallback to ImGuiInputTextCallback, ImGuiTextEditCallbackData to ImGuiInputTextCallbackData for consistency. Kept redirection types (will obsolete). - 2018/08/21 (1.63) - removed ImGuiInputTextCallbackData::ReadOnly since it is a duplication of (ImGuiInputTextCallbackData::Flags & ImGuiInputTextFlags_ReadOnly). - 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. - 2018/08/01 (1.63) - renamed io.OptCursorBlink to io.ConfigCursorBlink [-> io.ConfigInputTextCursorBlink in 1.65], io.OptMacOSXBehaviors to ConfigMacOSXBehaviors for consistency. - 2018/07/22 (1.63) - changed ImGui::GetTime() return value from float to double to avoid accumulating floating point imprecisions over time. - 2018/07/08 (1.63) - style: renamed ImGuiCol_ModalWindowDarkening to ImGuiCol_ModalWindowDimBg for consistency with other features. Kept redirection enum (will obsolete). - 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.). old bindings will still work as is, however prefer using the separated bindings as they will be updated to support multi-viewports. when adopting new bindings follow the main.cpp code of your preferred examples/ folder to know which functions to call. in particular, note that old bindings called ImGui::NewFrame() at the end of their ImGui_ImplXXXX_NewFrame() function. - 2018/06/06 (1.62) - renamed GetGlyphRangesChinese() to GetGlyphRangesChineseFull() to distinguish other variants and discourage using the full set. - 2018/06/06 (1.62) - TreeNodeEx()/TreeNodeBehavior(): the ImGuiTreeNodeFlags_CollapsingHeader helper now include the ImGuiTreeNodeFlags_NoTreePushOnOpen flag. See Changelog for details. - 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. If you used DragInt() with custom format strings, make sure you change them to use %d or an integer-compatible format. 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. 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. - 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", consistent with other functions. Kept redirection functions (will obsolete). - 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. - 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). - 2018/03/12 (1.60) - removed ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered as the closing cross uses regular button colors now. - 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. - 2018/03/03 (1.60) - renamed ImGuiStyleVar_Count_ to ImGuiStyleVar_COUNT and ImGuiMouseCursor_Count_ to ImGuiMouseCursor_COUNT for consistency with other public enums. - 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. - 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. - 2018/02/07 (1.60) - reorganized context handling to be more explicit, - YOU NOW NEED TO CALL ImGui::CreateContext() AT THE BEGINNING OF YOUR APP, AND CALL ImGui::DestroyContext() AT THE END. - removed Shutdown() function, as DestroyContext() serve this purpose. - you may pass a ImFontAtlas* pointer to CreateContext() to share a font atlas between contexts. Otherwise CreateContext() will create its own font atlas instance. - removed allocator parameters from CreateContext(), they are now setup with SetAllocatorFunctions(), and shared by all contexts. - removed the default global context and font atlas instance, which were confusing for users of DLL reloading and users of multiple contexts. - 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. - 2018/01/11 (1.60) - obsoleted IsAnyWindowHovered() in favor of IsWindowHovered(ImGuiHoveredFlags_AnyWindow). Kept redirection function (will obsolete). - 2018/01/11 (1.60) - obsoleted IsAnyWindowFocused() in favor of IsWindowFocused(ImGuiFocusedFlags_AnyWindow). Kept redirection function (will obsolete). - 2018/01/03 (1.60) - renamed ImGuiSizeConstraintCallback to ImGuiSizeCallback, ImGuiSizeConstraintCallbackData to ImGuiSizeCallbackData. - 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. - 2017/12/24 (1.53) - renamed the emblematic ShowTestWindow() function to ShowDemoWindow(). Kept redirection function (will obsolete). - 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 - 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. - 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. - 2017/12/13 (1.53) - renamed GetItemsLineHeightWithSpacing() to GetFrameHeightWithSpacing(). Kept redirection function (will obsolete). - 2017/12/13 (1.53) - obsoleted IsRootWindowFocused() in favor of using IsWindowFocused(ImGuiFocusedFlags_RootWindow). Kept redirection function (will obsolete). - obsoleted IsRootWindowOrAnyChildFocused() in favor of using IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows). Kept redirection function (will obsolete). - 2017/12/12 (1.53) - renamed ImGuiTreeNodeFlags_AllowOverlapMode to ImGuiTreeNodeFlags_AllowItemOverlap. Kept redirection enum (will obsolete). - 2017/12/10 (1.53) - removed SetNextWindowContentWidth(), prefer using SetNextWindowContentSize(). Kept redirection function (will obsolete). - 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. - 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. 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. - 2017/11/18 (1.53) - Style: removed ImGuiCol_ComboBg in favor of combo boxes using ImGuiCol_PopupBg for consistency. - 2017/11/18 (1.53) - Style: renamed ImGuiCol_ChildWindowBg to ImGuiCol_ChildBg. - 2017/11/18 (1.53) - Style: renamed style.ChildWindowRounding to style.ChildRounding, ImGuiStyleVar_ChildWindowRounding to ImGuiStyleVar_ChildRounding. - 2017/11/02 (1.53) - obsoleted IsRootWindowOrAnyChildHovered() in favor of using IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows); - 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. - 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. - 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. removed the IsItemRectHovered()/IsWindowRectHovered() names introduced in 1.51 since they were merely more consistent names for the two functions we are now obsoleting. - 2017/10/17 (1.52) - marked the old 5-parameters version of Begin() as obsolete (still available). Use SetNextWindowSize()+Begin() instead! - 2017/10/11 (1.52) - renamed AlignFirstTextHeightToWidgets() to AlignTextToFramePadding(). Kept inline redirection function (will obsolete). - 2017/09/26 (1.52) - renamed ImFont::Glyph to ImFontGlyph. Keep redirection typedef (will obsolete). - 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). - 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)". - 2017/08/22 (1.51) - renamed IsItemHoveredRect() to IsItemRectHovered(). Kept inline redirection function (will obsolete). -> (1.52) use IsItemHovered(ImGuiHoveredFlags_RectOnly)! - renamed IsMouseHoveringAnyWindow() to IsAnyWindowHovered() for consistency. Kept inline redirection function (will obsolete). - renamed IsMouseHoveringWindow() to IsWindowRectHovered() for consistency. Kept inline redirection function (will obsolete). - 2017/08/20 (1.51) - renamed GetStyleColName() to GetStyleColorName() for consistency. - 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. - 2017/08/15 (1.51) - marked the weird IMGUI_ONCE_UPON_A_FRAME helper macro as obsolete. prefer using the more explicit ImGuiOnceUponAFrame. - 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. - 2017/08/13 (1.51) - renamed ImGuiCol_Columns*** to ImGuiCol_Separator***. Kept redirection enums (will obsolete). - 2017/08/11 (1.51) - renamed ImGuiSetCond_*** types and flags to ImGuiCond_***. Kept redirection enums (will obsolete). - 2017/08/09 (1.51) - removed ValueColor() helpers, they are equivalent to calling Text(label) + SameLine() + ColorButton(). - 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. - 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. - 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))' - 2017/07/20 (1.51) - removed IsPosHoveringAnyWindow(ImVec2), which was partly broken and misleading. ASSERT + redirect user to io.WantCaptureMouse - 2017/05/26 (1.50) - removed ImFontConfig::MergeGlyphCenterV in favor of a more multipurpose ImFontConfig::GlyphOffset. - 2017/05/01 (1.50) - renamed ImDrawList::PathFill() (rarely used directly) to ImDrawList::PathFillConvex() for clarity. - 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(). - 2016/10/15 (1.50) - avoid 'void* user_data' parameter to io.SetClipboardTextFn/io.GetClipboardTextFn pointers. We pass io.ClipboardUserData to it. - 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. - 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. - 2016/05/12 (1.49) - title bar (using ImGuiCol_TitleBg/ImGuiCol_TitleBgActive colors) isn't rendered over a window background (ImGuiCol_WindowBg color) anymore. If your TitleBg/TitleBgActive alpha was 1.0f or you are using the default theme it will not affect you. 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. 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. ImVec4 ConvertTitleBgCol(const ImVec4& win_bg_col, const ImVec4& title_bg_col) { float new_a = 1.0f - ((1.0f - win_bg_col.w) * (1.0f - title_bg_col.w)), k = title_bg_col.w / new_a; 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); } 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. - 2016/05/07 (1.49) - removed confusing set of GetInternalState(), GetInternalStateSize(), SetInternalState() functions. Now using CreateContext(), DestroyContext(), GetCurrentContext(), SetCurrentContext(). - 2016/05/02 (1.49) - renamed SetNextTreeNodeOpened() to SetNextTreeNodeOpen(), no redirection. - 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). - 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. - 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). - 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) - 2016/03/21 (1.48) - renamed GetWindowFont() to GetFont(), GetWindowFontSize() to GetFontSize(). Kept inline redirection function (will obsolete). - 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. - 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. - 2015/12/27 (1.48) - fixed ImDrawList::AddRect() which used to render a rectangle 1 px too large on each axis. - 2015/12/04 (1.47) - renamed Color() helpers to ValueColor() - dangerously named, rarely used and probably to be made obsolete. - 2015/08/29 (1.45) - with the addition of horizontal scrollbar we made various fixes to inconsistencies with dealing with cursor position. 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. 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! - 2015/08/29 (1.45) - renamed style.ScrollbarWidth to style.ScrollbarSize - 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. - 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 - 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. you need to render your textured triangles with bilinear filtering to benefit from sub-pixel positioning of text. - 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. this necessary change will break your rendering function! the fix should be very easy. sorry for that :( - 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. - the signature of the io.RenderDrawListsFn handler has changed! old: ImGui_XXXX_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) new: ImGui_XXXX_RenderDrawLists(ImDrawData* draw_data). parameters: 'cmd_lists' becomes 'draw_data->CmdLists', 'cmd_lists_count' becomes 'draw_data->CmdListsCount' ImDrawList: 'commands' becomes 'CmdBuffer', 'vtx_buffer' becomes 'VtxBuffer', 'IdxBuffer' is new. ImDrawCmd: 'vtx_count' becomes 'ElemCount', 'clip_rect' becomes 'ClipRect', 'user_callback' becomes 'UserCallback', 'texture_id' becomes 'TextureId'. - 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. - 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! - refer to code in the examples/ folder or ask on the GitHub if you are unsure of how to upgrade. please upgrade! - 2015/07/10 (1.43) - changed SameLine() parameters from int to float. - 2015/07/02 (1.42) - renamed SetScrollPosHere() to SetScrollFromCursorPos(). Kept inline redirection function (will obsolete). - 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. - 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 - 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! - 2015/05/31 (1.40) - renamed GetWindowCollapsed() to IsWindowCollapsed() for consistency. Kept inline redirection function (will obsolete). - 2015/05/31 (1.40) - renamed IsRectClipped() to IsRectVisible() for consistency. Note that return value is opposite! Kept inline redirection function (will obsolete). - 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. - 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. - 2015/05/03 (1.40) - removed style.AutoFitPadding, using style.WindowPadding makes more sense (the default values were already the same). - 2015/04/13 (1.38) - renamed IsClipped() to IsRectClipped(). Kept inline redirection function until 1.50. - 2015/04/09 (1.38) - renamed ImDrawList::AddArc() to ImDrawList::AddArcFast() for compatibility with future API - 2015/04/03 (1.38) - removed ImGuiCol_CheckHovered, ImGuiCol_CheckActive, replaced with the more general ImGuiCol_FrameBgHovered, ImGuiCol_FrameBgActive. - 2014/04/03 (1.38) - removed support for passing -FLT_MAX..+FLT_MAX as the range for a SliderFloat(). Use DragFloat() or Inputfloat() instead. - 2015/03/17 (1.36) - renamed GetItemBoxMin()/GetItemBoxMax()/IsMouseHoveringBox() to GetItemRectMin()/GetItemRectMax()/IsMouseHoveringRect(). Kept inline redirection function until 1.50. - 2015/03/15 (1.36) - renamed style.TreeNodeSpacing to style.IndentSpacing, ImGuiStyleVar_TreeNodeSpacing to ImGuiStyleVar_IndentSpacing - 2015/03/13 (1.36) - renamed GetWindowIsFocused() to IsWindowFocused(). Kept inline redirection function until 1.50. - 2015/03/08 (1.35) - renamed style.ScrollBarWidth to style.ScrollbarWidth (casing) - 2015/02/27 (1.34) - renamed OpenNextNode(bool) to SetNextTreeNodeOpened(bool, ImGuiSetCond). Kept inline redirection function until 1.50. - 2015/02/27 (1.34) - renamed ImGuiSetCondition_*** to ImGuiSetCond_***, and _FirstUseThisSession becomes _Once. - 2015/02/11 (1.32) - changed text input callback ImGuiTextEditCallback return type from void-->int. reserved for future use, return 0 for now. - 2015/02/10 (1.32) - renamed GetItemWidth() to CalcItemWidth() to clarify its evolving behavior - 2015/02/08 (1.31) - renamed GetTextLineSpacing() to GetTextLineHeightWithSpacing() - 2015/02/01 (1.31) - removed IO.MemReallocFn (unused) - 2015/01/19 (1.30) - renamed ImGuiStorage::GetIntPtr()/GetFloatPtr() to GetIntRef()/GetIntRef() because Ptr was conflicting with actual pointer storage functions. - 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader. (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels. font init: { const void* png_data; unsigned int png_size; ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); <..Upload texture to GPU..>; } became: { unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); <..Upload texture to GPU>; io.Fonts->TexId = YourTextureIdentifier; } you now have more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs. it is now recommended that you sample the font texture with bilinear interpolation. (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.Fonts->TexID. (1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix) (1.30) - removed ImGui::IsItemFocused() in favor of ImGui::IsItemActive() which handles all widgets - 2014/12/10 (1.18) - removed SetNewWindowDefaultPos() in favor of new generic API SetNextWindowPos(pos, ImGuiSetCondition_FirstUseEver) - 2014/11/28 (1.17) - moved IO.Font*** options to inside the IO.Font-> structure (FontYOffset, FontTexUvForWhite, FontBaseScale, FontFallbackGlyph) - 2014/11/26 (1.17) - reworked syntax of IMGUI_ONCE_UPON_A_FRAME helper macro to increase compiler compatibility - 2014/11/07 (1.15) - renamed IsHovered() to IsItemHovered() - 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) - 2014/09/25 (1.13) - removed 'text_end' parameter from IO.SetClipboardTextFn (the string is now always zero-terminated for simplicity) - 2014/09/24 (1.12) - renamed SetFontScale() to SetWindowFontScale() - 2014/09/24 (1.12) - moved IM_MALLOC/IM_REALLOC/IM_FREE preprocessor defines to IO.MemAllocFn/IO.MemReallocFn/IO.MemFreeFn - 2014/08/30 (1.09) - removed IO.FontHeight (now computed automatically) - 2014/08/30 (1.09) - moved IMGUI_FONT_TEX_UV_FOR_WHITE preprocessor define to IO.FontTexUvForWhite - 2014/08/28 (1.09) - changed the behavior of IO.PixelCenterOffset following various rendering fixes FREQUENTLY ASKED QUESTIONS (FAQ), TIPS ====================================== Q: Where is the documentation? A: This library is poorly documented at the moment and expects of the user to be acquainted with C/C++. - Run the examples/ and explore them. - See demo code in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. - The demo covers most features of Dear ImGui, so you can read the code and see its output. - See documentation and comments at the top of imgui.cpp + effectively imgui.h. - Dozens of standalone example applications using e.g. OpenGL/DirectX are provided in the examples/ folder to explain how to integrate Dear ImGui with your own engine/application. - Your programming IDE is your friend, find the type or function declaration to find comments associated to it. Q: Which version should I get? A: I occasionally tag Releases (https://github.com/ocornut/imgui/releases) but it is generally safe and recommended to sync to master/latest. The library is fairly stable and regressions tend to be fixed fast when reported. You may also peak at the 'docking' branch which includes: - Docking/Merging features (https://github.com/ocornut/imgui/issues/2109) - Multi-viewport features (https://github.com/ocornut/imgui/issues/1542) Many projects are using this branch and it is kept in sync with master regularly. Q: Who uses Dear ImGui? A: See "Quotes" (https://github.com/ocornut/imgui/wiki/Quotes) and "Software using Dear ImGui" (https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) Wiki pages for a list of games/software which are publicly known to use dear imgui. Please add yours if you can! Q: Why the odd dual naming, "Dear ImGui" vs "ImGui"? A: The library started its life as "ImGui" due to the fact that I didn't give it a proper name when when I released 1.0, and had no particular expectation that it would take off. However, the term IMGUI (immediate-mode graphical user interface) was coined before and is being used in variety of other situations (e.g. Unity uses it own implementation of the IMGUI paradigm). To reduce the ambiguity without affecting existing code bases, I have decided on an alternate, longer name "Dear ImGui" that people can use to refer to this specific library. Please try to refer to this library as "Dear ImGui". Q: How can I tell whether to dispatch mouse/keyboard to imgui or to my application? A: You can read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags from the ImGuiIO structure (e.g. if (ImGui::GetIO().WantCaptureMouse) { ... } ) - 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. - 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. - 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). Note: you should always pass your mouse/keyboard inputs to imgui, even when the io.WantCaptureXXX flag are set false. This is because imgui needs to detect that you clicked in the void to unfocus its own windows. Note: The 'io.WantCaptureMouse' is more accurate that any attempt to "check if the mouse is hovering a window" (don't do that!). 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. 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 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(). Note: Text input widget releases focus on "Return KeyDown", so the subsequent "Return KeyUp" event that your application receive will typically have 'io.WantCaptureKeyboard=false'. Depending on your application logic it may or not be inconvenient. You might want to track which key-downs were targeted for Dear ImGui, e.g. with an array of bool, and filter out the corresponding key-ups.) Q: How can I display an image? What is ImTextureID, how does it works? A: Short explanation: - 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. - Actual textures are identified in a way that is up to the user/engine. Those identifiers are stored and passed as ImTextureID (void*) value. - Loading image files from the disk and turning them into a texture is not within the scope of Dear ImGui (for a good reason). Please read documentations or tutorials on your graphics API to understand how to display textures on the screen before moving onward. Long explanation: - Dear ImGui's job is to create "meshes", defined in a renderer-agnostic format made of draw commands and vertices. 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 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.). - 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. We carry the information to identify a "texture" in the ImTextureID type. 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. Dear ImGui doesn't know or understand what you are storing in ImTextureID, it merely pass ImTextureID values until they reach your rendering function. - In the examples/ bindings, for each graphics API binding we decided on a type that is likely to be a good representation for specifying an image from the end-user perspective. This is what the _examples_ rendering functions are using: OpenGL: ImTextureID = GLuint (see ImGui_ImplGlfwGL3_RenderDrawData() function in imgui_impl_glfw_gl3.cpp) DirectX9: ImTextureID = LPDIRECT3DTEXTURE9 (see ImGui_ImplDX9_RenderDrawData() function in imgui_impl_dx9.cpp) DirectX11: ImTextureID = ID3D11ShaderResourceView* (see ImGui_ImplDX11_RenderDrawData() function in imgui_impl_dx11.cpp) DirectX12: ImTextureID = D3D12_GPU_DESCRIPTOR_HANDLE (see ImGui_ImplDX12_RenderDrawData() function in imgui_impl_dx12.cpp) For example, in the OpenGL example binding we store raw OpenGL texture identifier (GLuint) inside ImTextureID. Whereas in the DirectX11 example binding we store a pointer to ID3D11ShaderResourceView inside ImTextureID, which is a higher-level structure tying together both the texture and information about its format and how to read it. - 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 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 is designed. If your engine has high-level data types for "textures" and "material" then you may want to use them. 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 representation suggested by the example bindings is probably the best choice. (Advanced users may also decide to keep a low-level type in ImTextureID, and use ImDrawList callback and pass information to their renderer) User code may do: // Cast our texture type to ImTextureID / void* MyTexture* texture = g_CoffeeTableTexture; ImGui::Image((void*)texture, ImVec2(texture->Width, texture->Height)); The renderer function called after ImGui::Render() will receive that same value that the user code passed: // Cast ImTextureID / void* stored in the draw command as our texture type MyTexture* texture = (MyTexture*)pcmd->TextureId; MyEngineBindTexture2D(texture); 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. 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. 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. Here's a simplified OpenGL example using stb_image.h: // Use stb_image.h to load a PNG from disk and turn it into raw RGBA pixel data: #define STB_IMAGE_IMPLEMENTATION #include [...] int my_image_width, my_image_height; unsigned char* my_image_data = stbi_load("my_image.png", &my_image_width, &my_image_height, NULL, 4); // Turn the RGBA pixel data into an OpenGL texture: GLuint my_opengl_texture; glGenTextures(1, &my_opengl_texture); glBindTexture(GL_TEXTURE_2D, my_opengl_texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data); // 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: ImGui::Image((void*)(intptr_t)my_opengl_texture, ImVec2(my_image_width, my_image_height)); 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. Because both end-points (user code and rendering function) are under your control, you know exactly what is stored inside the ImTextureID / void*. Examples: GLuint my_tex = XXX; void* my_void_ptr; 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) my_tex = (GLuint)(intptr_t)my_void_ptr; // cast a void* into a GLuint ID3D11ShaderResourceView* my_dx11_srv = XXX; void* my_void_ptr; my_void_ptr = (void*)my_dx11_srv; // cast a ID3D11ShaderResourceView* into an opaque void* my_dx11_srv = (ID3D11ShaderResourceView*)my_void_ptr; // cast a void* into a ID3D11ShaderResourceView* Finally, you may call ImGui::ShowMetricsWindow() to explore/visualize/understand how the ImDrawList are generated. Q: Why are multiple widgets reacting when I interact with a single one? Q: How can I have multiple widgets with the same label or with an empty label? A: A primer on labels and the ID Stack... Dear ImGui internally need to uniquely identify UI elements. Elements that are typically not clickable (such as calls to the Text functions) don't need an ID. Interactive widgets (such as calls to Button buttons) need a unique ID. Unique ID are used internally to track active widgets and occasionally associate state to widgets. Unique ID are implicitly built from the hash of multiple elements that identify the "path" to the UI element. - Unique ID are often derived from a string label: Button("OK"); // Label = "OK", ID = hash of (..., "OK") Button("Cancel"); // Label = "Cancel", ID = hash of (..., "Cancel") - ID are uniquely scoped within windows, tree nodes, etc. which all pushes to the ID stack. Having two buttons labeled "OK" in different windows or different tree locations is fine. We used "..." above to signify whatever was already pushed to the ID stack previously: Begin("MyWindow"); Button("OK"); // Label = "OK", ID = hash of ("MyWindow", "OK") End(); Begin("MyOtherWindow"); Button("OK"); // Label = "OK", ID = hash of ("MyOtherWindow", "OK") End(); - If you have a same ID twice in the same location, you'll have a conflict: Button("OK"); Button("OK"); // ID collision! Interacting with either button will trigger the first one. Fear not! this is easy to solve and there are many ways to solve it! - Solving ID conflict in a simple/local context: When passing a label you can optionally specify extra ID information within string itself. Use "##" to pass a complement to the ID that won't be visible to the end-user. This helps solving the simple collision cases when you know e.g. at compilation time which items are going to be created: Begin("MyWindow"); Button("Play"); // Label = "Play", ID = hash of ("MyWindow", "Play") Button("Play##foo1"); // Label = "Play", ID = hash of ("MyWindow", "Play##foo1") // Different from above Button("Play##foo2"); // Label = "Play", ID = hash of ("MyWindow", "Play##foo2") // Different from above End(); - If you want to completely hide the label, but still need an ID: Checkbox("##On", &b); // Label = "", ID = hash of (..., "##On") // No visible label, just a checkbox! - Occasionally/rarely you might want change a label while preserving a constant ID. This allows you to animate labels. For example you may want to include varying information in a window title bar, but windows are uniquely identified by their ID. Use "###" to pass a label that isn't part of ID: Button("Hello###ID"); // Label = "Hello", ID = hash of (..., "###ID") Button("World###ID"); // Label = "World", ID = hash of (..., "###ID") // Same as above, even though the label looks different sprintf(buf, "My game (%f FPS)###MyGame", fps); Begin(buf); // Variable title, ID = hash of "MyGame" - Solving ID conflict in a more general manner: Use PushID() / PopID() to create scopes and manipulate the ID stack, as to avoid ID conflicts within the same window. This is the most convenient way of distinguishing ID when iterating and creating many UI elements programmatically. You can push a pointer, a string or an integer value into the ID stack. Remember that ID are formed from the concatenation of _everything_ pushed into the ID stack. At each level of the stack we store the seed used for items at this level of the ID stack. Begin("Window"); for (int i = 0; i < 100; i++) { PushID(i); // Push i to the id tack Button("Click"); // Label = "Click", ID = hash of ("Window", i, "Click") PopID(); } for (int i = 0; i < 100; i++) { MyObject* obj = Objects[i]; PushID(obj); Button("Click"); // Label = "Click", ID = hash of ("Window", obj pointer, "Click") PopID(); } for (int i = 0; i < 100; i++) { MyObject* obj = Objects[i]; PushID(obj->Name); Button("Click"); // Label = "Click", ID = hash of ("Window", obj->Name, "Click") PopID(); } End(); - You can stack multiple prefixes into the ID stack: Button("Click"); // Label = "Click", ID = hash of (..., "Click") PushID("node"); Button("Click"); // Label = "Click", ID = hash of (..., "node", "Click") PushID(my_ptr); Button("Click"); // Label = "Click", ID = hash of (..., "node", my_ptr, "Click") PopID(); PopID(); - Tree nodes implicitly creates a scope for you by calling PushID(). Button("Click"); // Label = "Click", ID = hash of (..., "Click") if (TreeNode("node")) // <-- this function call will do a PushID() for you (unless instructed not to, with a special flag) { Button("Click"); // Label = "Click", ID = hash of (..., "node", "Click") TreePop(); } - When working with trees, ID are used to preserve the open/close state of each tree node. Depending on your use cases you may want to use strings, indices or pointers as ID. e.g. when following a single pointer that may change over time, using a static string as ID will preserve your node open/closed state when the targeted object change. e.g. when displaying a list of objects, using indices or pointers as ID will preserve the node open/closed state differently. See what makes more sense in your situation! Q: How can I use my own math types instead of ImVec2/ImVec4? A: You can edit imconfig.h and setup the IM_VEC2_CLASS_EXTRA/IM_VEC4_CLASS_EXTRA macros to add implicit type conversions. This way you'll be able to use your own types everywhere, e.g. passing glm::vec2 to ImGui functions instead of ImVec2. Q: How can I load a different font than the default? A: Use the font atlas to load the TTF/OTF file you want: ImGuiIO& io = ImGui::GetIO(); io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels); io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8() Default is ProggyClean.ttf, monospace, rendered at size 13, embedded in dear imgui's source code. (Tip: monospace fonts are convenient because they allow to facilitate horizontal alignment directly at the string level.) (Read the 'misc/fonts/README.txt' file for more details about font loading.) New programmers: remember that in C/C++ and most programming languages if you want to use a backslash \ within a string literal, you need to write it double backslash "\\": io.Fonts->AddFontFromFileTTF("MyDataFolder\MyFontFile.ttf", size_in_pixels); // WRONG (you are escape the M here!) io.Fonts->AddFontFromFileTTF("MyDataFolder\\MyFontFile.ttf", size_in_pixels); // CORRECT io.Fonts->AddFontFromFileTTF("MyDataFolder/MyFontFile.ttf", size_in_pixels); // ALSO CORRECT Q: How can I easily use icons in my application? A: The most convenient and practical way is to merge an icon font such as FontAwesome inside you main font. Then you can refer to icons within your strings. You may want to see ImFontConfig::GlyphMinAdvanceX to make your icon look monospace to facilitate alignment. (Read the 'misc/fonts/README.txt' file for more details about icons font loading.) Q: How can I load multiple fonts? A: Use the font atlas to pack them into a single texture: (Read the 'misc/fonts/README.txt' file and the code in ImFontAtlas for more details.) ImGuiIO& io = ImGui::GetIO(); ImFont* font0 = io.Fonts->AddFontDefault(); ImFont* font1 = io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels); ImFont* font2 = io.Fonts->AddFontFromFileTTF("myfontfile2.ttf", size_in_pixels); io.Fonts->GetTexDataAsRGBA32() or GetTexDataAsAlpha8() // the first loaded font gets used by default // use ImGui::PushFont()/ImGui::PopFont() to change the font at runtime // Options ImFontConfig config; config.OversampleH = 2; config.OversampleV = 1; config.GlyphOffset.y -= 1.0f; // Move everything by 1 pixels up config.GlyphExtraSpacing.x = 1.0f; // Increase spacing between characters io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_pixels, &config); // Combine multiple fonts into one (e.g. for icon fonts) static ImWchar ranges[] = { 0xf000, 0xf3ff, 0 }; ImFontConfig config; config.MergeMode = true; io.Fonts->AddFontDefault(); io.Fonts->AddFontFromFileTTF("fontawesome-webfont.ttf", 16.0f, &config, ranges); // Merge icon font io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_pixels, NULL, &config, io.Fonts->GetGlyphRangesJapanese()); // Merge japanese glyphs Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic? A: When loading a font, pass custom Unicode ranges to specify the glyphs to load. // Add default Japanese ranges io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels, NULL, io.Fonts->GetGlyphRangesJapanese()); // 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) ImVector ranges; ImFontGlyphRangesBuilder builder; builder.AddText("Hello world"); // Add a string (here "Hello world" contains 7 unique characters) builder.AddChar(0x7262); // Add a specific character builder.AddRanges(io.Fonts->GetGlyphRangesJapanese()); // Add one of the default ranges builder.BuildRanges(&ranges); // Build the final result (ordered ranges with all the unique characters submitted) io.Fonts->AddFontFromFileTTF("myfontfile.ttf", size_in_pixels, NULL, ranges.Data); All your strings needs to use UTF-8 encoding. In C++11 you can encode a string literal in UTF-8 by using the u8"hello" syntax. Specifying literal in your source code using a local code page (such as CP-923 for Japanese or CP-1251 for Cyrillic) will NOT work! Otherwise you can convert yourself to UTF-8 or load text data from file already saved as UTF-8. Text input: it is up to your application to pass the right character code by calling io.AddInputCharacter(). The applications in examples/ are doing that. 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). You may also use MultiByteToWideChar() or ToUnicode() to retrieve Unicode codepoints from MultiByte characters or keyboard state. 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 the default implementation of io.ImeSetInputScreenPosFn() to set your Microsoft IME position correctly. Q: How can I interact with standard C++ types (such as std::string and std::vector)? A: - Being highly portable (bindings for several languages, frameworks, programming style, obscure or older platforms/compilers), and aiming for compatibility & performance suitable for every modern real-time game engines, dear imgui does not use any of std C++ types. We use raw types (e.g. char* instead of std::string) because they adapt to more use cases. - To use ImGui::InputText() with a std::string or any resizable string class, see misc/cpp/imgui_stdlib.h. - To use combo boxes and list boxes with std::vector or any other data structure: the BeginCombo()/EndCombo() API lets you iterate and submit items yourself, so does the ListBoxHeader()/ListBoxFooter() API. Prefer using them over the old and awkward Combo()/ListBox() api. - Generally for most high-level types you should be able to access the underlying data type. You may write your own one-liner wrappers to facilitate user code (tip: add new functions in ImGui:: namespace from your code). - Dear ImGui applications often need to make intensive use of strings. It is expected that many of the strings you will pass 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. Please bear in mind that using std::string on applications with large amount of UI may incur unsatisfactory performances. Modern implementations of std::string often include small-string optimization (which is often a local buffer) but those are not configurable and not the same across implementations. - If you are finding your UI traversal cost to be too large, make sure your string usage is not leading to excessive amount of heap allocations. Consider using literals, statically sized buffers and your own helper functions. A common pattern is that you will need to build lots of strings on the fly, and their maximum length can be easily be scoped ahead. One possible implementation of a helper to facilitate printf-style building of strings: https://github.com/ocornut/Str This is a small helper where you can instance strings with configurable local buffers length. Many game engines will provide similar or better string helpers. Q: How can I use the drawing facilities without an ImGui window? (using ImDrawList API) A: - You can create a dummy window. Call Begin() with the NoBackground | NoDecoration | NoSavedSettings | NoInputs flags. (The ImGuiWindowFlags_NoDecoration flag itself is a shortcut for NoTitleBar | NoResize | NoScrollbar | NoCollapse) Then you can retrieve the ImDrawList* via GetWindowDrawList() and draw to it in any way you like. - You can call ImGui::GetBackgroundDrawList() or ImGui::GetForegroundDrawList() and use those draw list to display contents behind or over every other imgui windows (one bg/fg drawlist per viewport). - You can create your own ImDrawList instance. You'll need to initialize them ImGui::GetDrawListSharedData(), or create your own ImDrawListSharedData, and then call your rendered code with your own ImDrawList or ImDrawData data. Q: How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display) A: - You can control Dear ImGui with a gamepad. Read about navigation in "Using gamepad/keyboard navigation controls". (short version: map gamepad inputs into the io.NavInputs[] array + set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad) - You can share your computer mouse seamlessly with your console/tablet/phone using Synergy (https://symless.com/synergy) This is the preferred solution for developer productivity. In particular, the "micro-synergy-client" repository (https://github.com/symless/micro-synergy-client) has simple and portable source code (uSynergy.c/.h) for a small embeddable client that you can use on any platform to connect to your host computer, based on the Synergy 1.x protocol. Make sure you download the Synergy 1 server on your computer. Console SDK also sometimes provide equivalent tooling or wrapper for Synergy-like protocols. - You may also use a third party solution such as Remote ImGui (https://github.com/JordiRos/remoteimgui) which sends the vertices to render over the local network, allowing you to use Dear ImGui even on a screen-less machine. - For touch inputs, you can increase the hit box of widgets (via the style.TouchPadding setting) to accommodate for the lack of precision of touch inputs, but it is recommended you use a mouse or gamepad to allow optimizing for screen real-estate and precision. Q: I integrated Dear ImGui in my engine and the text or lines are blurry.. A: In your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f). Also make sure your orthographic projection matrix and io.DisplaySize matches your actual framebuffer dimension. Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.. A: You are probably mishandling the clipping rectangles in your render function. Rectangles provided by ImGui are defined as (x1=left,y1=top,x2=right,y2=bottom) and NOT as (x1,y1,width,height). Q: How can I help? A: - If you are experienced with Dear ImGui and C++, look at the github issues, look at the Wiki, read docs/TODO.txt and see how you want to help and can help! - Businesses: convince your company to fund development via support contracts/sponsoring! This is among the most useful thing you can do for dear imgui. - Individuals: you can also become a Patron (http://www.patreon.com/imgui) or donate on PayPal! See README. - Disclose your usage of dear imgui via a dev blog post, a tweet, a screenshot, a mention somewhere etc. You may post screenshot or links in the gallery threads (github.com/ocornut/imgui/issues/1902). Visuals are ideal as they inspire other programmers. But even without visuals, disclosing your use of dear imgui help the library grow credibility, and help other teams and programmers with taking decisions. - 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). - tip: you can call Begin() multiple times with the same name during the same frame, it will keep appending to the same window. this is also useful to set yourself in the context of another window (to get/set other settings) - tip: you can create widgets without a Begin()/End() block, they will go in an implicit window called "Debug". - 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 of a deep nested inner loop in your code. - tip: you can call Render() multiple times (e.g for VR renders). - tip: call and read the ShowDemoWindow() code in imgui_demo.cpp for more example of how to use ImGui! */ #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS #endif #include "imgui.hpp" #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif #include "imgui_internal.hpp" #include // toupper #include // vsnprintf, sscanf, printf #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier #include // intptr_t #else #include // intptr_t #endif // Debug options #define IMGUI_DEBUG_NAV_SCORING 0 // Display navigation scoring preview when hovering items. Display last moving direction matches when holding CTRL #define IMGUI_DEBUG_NAV_RECTS 0 // Display the reference navigation rectangle for each window #define IMGUI_DEBUG_DOCKING_INI 0 // Save additional comments in .ini file (makes saving slower) // Visual Studio warnings #ifdef _MSC_VER #pragma warning (disable: 4127) // condition expression is constant #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen #endif // Clang/GCC warnings with -Weverything #ifdef __clang__ #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! #pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. #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. #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. #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. #pragma clang diagnostic ignored "-Wglobal-constructors" // warning : declaration requires a global destructor // similar to above, not sure what the exact difference is. #pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // #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. #pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning : cast to 'void *' from smaller integer type 'int' #if __has_warning("-Wzero-as-null-pointer-constant") #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 #endif #if __has_warning("-Wdouble-promotion") #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. #endif #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size #pragma GCC diagnostic ignored "-Wformat" // warning: format '%p' expects argument of type 'void*', but argument 6 has type 'ImGuiWindow*' #pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function #pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked #pragma GCC diagnostic ignored "-Wstrict-overflow" // warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false #if __GNUC__ >= 8 #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 #endif #endif // 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. static const float NAV_WINDOWING_HIGHLIGHT_DELAY = 0.20f; // Time before the highlight and screen dimming starts fading in static const float NAV_WINDOWING_LIST_APPEAR_DELAY = 0.15f; // Time before the window list starts to appear // Window resizing from edges (when io.ConfigWindowsResizeFromEdges = true and ImGuiBackendFlags_HasMouseCursors is set in io.BackendFlags by back-end) static const float WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS = 4.0f; // Extend outside and inside windows. Affect FindHoveredWindow(). static const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f; // Reduce visual noise by only highlighting the border after a certain time. // Docking static const float DOCKING_TRANSPARENT_PAYLOAD_ALPHA = 0.50f; // For use with io.ConfigDockingTransparentPayload. Apply to Viewport _or_ WindowBg in host viewport. //------------------------------------------------------------------------- // [SECTION] FORWARD DECLARATIONS //------------------------------------------------------------------------- static void SetCurrentWindow(ImGuiWindow* window); static void SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size); static void FindHoveredWindow(); static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags); static void CheckStacksSize(ImGuiWindow* window, bool write); static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges); static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list); static void AddWindowToSortBuffer(ImVector* out_sorted_windows, ImGuiWindow* window); // Settings static void* SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name); static void SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line); static void SettingsHandlerWindow_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf); // Platform Dependents default implementation for IO functions static const char* GetClipboardTextFn_DefaultImpl(void* user_data); static void SetClipboardTextFn_DefaultImpl(void* user_data, const char* text); namespace ImGui { static bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags); // Navigation static void NavUpdate(); static void NavUpdateWindowing(); static void NavUpdateWindowingList(); static void NavUpdateMoveResult(); static float NavUpdatePageUpPageDown(int allowed_dir_flags); static inline void NavUpdateAnyRequestFlag(); static void NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, ImGuiID id); static ImVec2 NavCalcPreferredRefPos(); static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); static int FindWindowFocusIndex(ImGuiWindow* window); // Misc static void UpdateMouseInputs(); static void UpdateMouseWheel(); static void UpdateManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]); static void RenderOuterBorders(ImGuiWindow* window); static void EndFrameDrawDimmedBackgrounds(); // Viewports const 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. static ImGuiViewportP* AddUpdateViewport(ImGuiWindow* window, ImGuiID id, const ImVec2& platform_pos, const ImVec2& size, ImGuiViewportFlags flags); static void UpdateViewportsNewFrame(); static void UpdateViewportsEndFrame(); static void UpdateSelectWindowViewport(ImGuiWindow* window); static bool UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImGuiViewportP* host_viewport); static void SetCurrentViewport(ImGuiWindow* window, ImGuiViewportP* viewport); static bool GetWindowAlwaysWantOwnViewport(ImGuiWindow* window); static int FindPlatformMonitorForPos(const ImVec2& pos); static int FindPlatformMonitorForRect(const ImRect& r); static void UpdateViewportPlatformMonitor(ImGuiViewportP* viewport); } //----------------------------------------------------------------------------- // [SECTION] CONTEXT AND MEMORY ALLOCATORS //----------------------------------------------------------------------------- // Current context pointer. Implicitly used by all Dear ImGui functions. Always assumed to be != NULL. // ImGui::CreateContext() will automatically set this pointer if it is NULL. Change to a different context by calling ImGui::SetCurrentContext(). // 1) Important: globals are not shared across DLL boundaries! If you use DLLs or any form of hot-reloading: you will need to call // SetCurrentContext() (with the pointer you got from CreateContext) from each unique static/DLL boundary, and after each hot-reloading. // In your debugger, add GImGui to your watch window and notice how its value changes depending on which location you are currently stepping into. // 2) Important: Dear ImGui functions are not thread-safe because of this pointer. // If you want thread-safety to allow N threads to access N different contexts, you can: // - Change this variable to use thread local storage so each thread can refer to a different context, in imconfig.h: // struct ImGuiContext; // extern thread_local ImGuiContext* MyImGuiTLS; // #define GImGui MyImGuiTLS // 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. // - Future development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586 // - If you need a finite number of contexts, you may compile and use multiple instances of the ImGui code from different namespace. #ifndef GImGui ImGuiContext* GImGui = NULL; #endif // Memory Allocator functions. Use SetAllocatorFunctions() to change them. // If you use DLL hotreloading you might need to call SetAllocatorFunctions() after reloading code from this file. // 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. #ifndef IMGUI_DISABLE_DEFAULT_ALLOCATORS static void* MallocWrapper(size_t size, void* user_data) { IM_UNUSED(user_data); return malloc(size); } static void FreeWrapper(void* ptr, void* user_data) { IM_UNUSED(user_data); free(ptr); } #else static void* MallocWrapper(size_t size, void* user_data) { IM_UNUSED(user_data); IM_UNUSED(size); IM_ASSERT(0); return NULL; } static void FreeWrapper(void* ptr, void* user_data) { IM_UNUSED(user_data); IM_UNUSED(ptr); IM_ASSERT(0); } #endif static void* (*GImAllocatorAllocFunc)(size_t size, void* user_data) = MallocWrapper; static void (*GImAllocatorFreeFunc)(void* ptr, void* user_data) = FreeWrapper; static void* GImAllocatorUserData = NULL; //----------------------------------------------------------------------------- // [SECTION] MAIN USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) //----------------------------------------------------------------------------- ImGuiStyle::ImGuiStyle() { Alpha = 1.0f; // Global alpha applies to everything in ImGui WindowPadding = ImVec2(8,8); // Padding within a window WindowRounding = 7.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows WindowBorderSize = 1.0f; // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested. WindowMinSize = ImVec2(32,32); // Minimum window size WindowTitleAlign = ImVec2(0.0f,0.5f);// Alignment for title bar text ChildRounding = 0.0f; // Radius of child window corners rounding. Set to 0.0f to have rectangular child windows ChildBorderSize = 1.0f; // Thickness of border around child windows. Generally set to 0.0f or 1.0f. Other values not well tested. PopupRounding = 0.0f; // Radius of popup window corners rounding. Set to 0.0f to have rectangular child windows PopupBorderSize = 1.0f; // Thickness of border around popup or tooltip windows. Generally set to 0.0f or 1.0f. Other values not well tested. FramePadding = ImVec2(4,3); // Padding within a framed rectangle (used by most widgets) FrameRounding = 0.0f; // Radius of frame corners rounding. Set to 0.0f to have rectangular frames (used by most widgets). FrameBorderSize = 0.0f; // Thickness of border around frames. Generally set to 0.0f or 1.0f. Other values not well tested. ItemSpacing = ImVec2(8,4); // Horizontal and vertical spacing between widgets/lines ItemInnerSpacing = ImVec2(4,4); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label) 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! IndentSpacing = 21.0f; // Horizontal spacing when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns ScrollbarSize = 16.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar GrabMinSize = 10.0f; // Minimum width/height of a grab box for slider/scrollbar GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. TabBorderSize = 0.0f; // Thickness of border around tabs. ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text when button is larger than text. 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. 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. MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. AntiAliasedLines = true; // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU. AntiAliasedFill = true; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.) 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. // Default theme ImGui::StyleColorsDark(this); } // 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. // 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. void ImGuiStyle::ScaleAllSizes(float scale_factor) { WindowPadding = ImFloor(WindowPadding * scale_factor); WindowRounding = ImFloor(WindowRounding * scale_factor); WindowMinSize = ImFloor(WindowMinSize * scale_factor); ChildRounding = ImFloor(ChildRounding * scale_factor); PopupRounding = ImFloor(PopupRounding * scale_factor); FramePadding = ImFloor(FramePadding * scale_factor); FrameRounding = ImFloor(FrameRounding * scale_factor); ItemSpacing = ImFloor(ItemSpacing * scale_factor); ItemInnerSpacing = ImFloor(ItemInnerSpacing * scale_factor); TouchExtraPadding = ImFloor(TouchExtraPadding * scale_factor); IndentSpacing = ImFloor(IndentSpacing * scale_factor); ColumnsMinSpacing = ImFloor(ColumnsMinSpacing * scale_factor); ScrollbarSize = ImFloor(ScrollbarSize * scale_factor); ScrollbarRounding = ImFloor(ScrollbarRounding * scale_factor); GrabMinSize = ImFloor(GrabMinSize * scale_factor); GrabRounding = ImFloor(GrabRounding * scale_factor); TabRounding = ImFloor(TabRounding * scale_factor); DisplayWindowPadding = ImFloor(DisplayWindowPadding * scale_factor); DisplaySafeAreaPadding = ImFloor(DisplaySafeAreaPadding * scale_factor); MouseCursorScale = ImFloor(MouseCursorScale * scale_factor); } ImGuiIO::ImGuiIO() { // Most fields are initialized with zero memset(this, 0, sizeof(*this)); // Settings ConfigFlags = ImGuiConfigFlags_None; BackendFlags = ImGuiBackendFlags_None; DisplaySize = ImVec2(-1.0f, -1.0f); DeltaTime = 1.0f/60.0f; IniSavingRate = 5.0f; IniFilename = "imgui.ini"; LogFilename = "imgui_log.txt"; MouseDoubleClickTime = 0.30f; MouseDoubleClickMaxDist = 6.0f; for (int i = 0; i < ImGuiKey_COUNT; i++) KeyMap[i] = -1; KeyRepeatDelay = 0.250f; KeyRepeatRate = 0.050f; UserData = NULL; Fonts = NULL; FontGlobalScale = 1.0f; FontDefault = NULL; FontAllowUserScaling = false; DisplayFramebufferScale = ImVec2(1.0f, 1.0f); // Docking options (when ImGuiConfigFlags_DockingEnable is set) ConfigDockingNoSplit = false; ConfigDockingWithShift = false; ConfigDockingTabBarOnSingleWindows = false; ConfigDockingTransparentPayload = false; // Viewport options (when ImGuiConfigFlags_ViewportsEnable is set) ConfigViewportsNoAutoMerge = false; ConfigViewportsNoTaskBarIcon = false; ConfigViewportsNoDecoration = true; ConfigViewportsNoDefaultParent = false; // Miscellaneous options MouseDrawCursor = false; #ifdef __APPLE__ ConfigMacOSXBehaviors = true; // Set Mac OS X style defaults based on __APPLE__ compile time flag #else ConfigMacOSXBehaviors = false; #endif ConfigInputTextCursorBlink = true; ConfigWindowsResizeFromEdges = true; ConfigWindowsMoveFromTitleBarOnly = false; // Platform Functions BackendPlatformName = BackendRendererName = NULL; BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL; GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; ClipboardUserData = NULL; #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS RenderDrawListsFn = NULL; #endif // Input (NB: we already have memset zero the entire structure!) MousePos = ImVec2(-FLT_MAX, -FLT_MAX); MousePosPrev = ImVec2(-FLT_MAX, -FLT_MAX); MouseDragThreshold = 6.0f; for (int i = 0; i < IM_ARRAYSIZE(MouseDownDuration); i++) MouseDownDuration[i] = MouseDownDurationPrev[i] = -1.0f; for (int i = 0; i < IM_ARRAYSIZE(KeysDownDuration); i++) KeysDownDuration[i] = KeysDownDurationPrev[i] = -1.0f; for (int i = 0; i < IM_ARRAYSIZE(NavInputsDownDuration); i++) NavInputsDownDuration[i] = -1.0f; } // Pass in translated ASCII characters for text input. // - with glfw you can get those from the callback set in glfwSetCharCallback() // - on Windows you can get those using ToAscii+keyboard state, or via the WM_CHAR message void ImGuiIO::AddInputCharacter(ImWchar c) { InputQueueCharacters.push_back(c); } void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) { while (*utf8_chars != 0) { unsigned int c = 0; utf8_chars += ImTextCharFromUtf8(&c, utf8_chars, NULL); if (c > 0 && c <= 0xFFFF) InputQueueCharacters.push_back((ImWchar)c); } } void ImGuiIO::ClearInputCharacters() { InputQueueCharacters.resize(0); } //----------------------------------------------------------------------------- // [SECTION] MISC HELPERS/UTILITIES (Maths, String, Format, Hash, File functions) //----------------------------------------------------------------------------- ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p) { ImVec2 ap = p - a; ImVec2 ab_dir = b - a; float dot = ap.x * ab_dir.x + ap.y * ab_dir.y; if (dot < 0.0f) return a; float ab_len_sqr = ab_dir.x * ab_dir.x + ab_dir.y * ab_dir.y; if (dot > ab_len_sqr) return b; return a + ab_dir * dot / ab_len_sqr; } bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p) { bool b1 = ((p.x - b.x) * (a.y - b.y) - (p.y - b.y) * (a.x - b.x)) < 0.0f; bool b2 = ((p.x - c.x) * (b.y - c.y) - (p.y - c.y) * (b.x - c.x)) < 0.0f; bool b3 = ((p.x - a.x) * (c.y - a.y) - (p.y - a.y) * (c.x - a.x)) < 0.0f; return ((b1 == b2) && (b2 == b3)); } void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w) { ImVec2 v0 = b - a; ImVec2 v1 = c - a; ImVec2 v2 = p - a; const float denom = v0.x * v1.y - v1.x * v0.y; out_v = (v2.x * v1.y - v1.x * v2.y) / denom; out_w = (v0.x * v2.y - v2.x * v0.y) / denom; out_u = 1.0f - out_v - out_w; } ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p) { ImVec2 proj_ab = ImLineClosestPoint(a, b, p); ImVec2 proj_bc = ImLineClosestPoint(b, c, p); ImVec2 proj_ca = ImLineClosestPoint(c, a, p); float dist2_ab = ImLengthSqr(p - proj_ab); float dist2_bc = ImLengthSqr(p - proj_bc); float dist2_ca = ImLengthSqr(p - proj_ca); float m = ImMin(dist2_ab, ImMin(dist2_bc, dist2_ca)); if (m == dist2_ab) return proj_ab; if (m == dist2_bc) return proj_bc; return proj_ca; } // Consider using _stricmp/_strnicmp under Windows or strcasecmp/strncasecmp. We don't actually use either ImStricmp/ImStrnicmp in the codebase any more. int ImStricmp(const char* str1, const char* str2) { int d; while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } return d; } int ImStrnicmp(const char* str1, const char* str2, size_t count) { int d = 0; while (count > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; count--; } return d; } void ImStrncpy(char* dst, const char* src, size_t count) { if (count < 1) return; if (count > 1) strncpy(dst, src, count - 1); dst[count - 1] = 0; } char* ImStrdup(const char* str) { size_t len = strlen(str); void* buf = IM_ALLOC(len + 1); return (char*)memcpy(buf, (const void*)str, len + 1); } char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src) { size_t dst_buf_size = p_dst_size ? *p_dst_size : strlen(dst) + 1; size_t src_size = strlen(src) + 1; if (dst_buf_size < src_size) { IM_FREE(dst); dst = (char*)IM_ALLOC(src_size); if (p_dst_size) *p_dst_size = src_size; } return (char*)memcpy(dst, (const void*)src, src_size); } const char* ImStrchrRange(const char* str, const char* str_end, char c) { const char* p = (const char*)memchr(str, (int)c, str_end - str); return p; } int ImStrlenW(const ImWchar* str) { //return (int)wcslen((const wchar_t*)str); // FIXME-OPT: Could use this when wchar_t are 16-bits int n = 0; while (*str++) n++; return n; } // Find end-of-line. Return pointer will point to either first \n, either str_end. const char* ImStreolRange(const char* str, const char* str_end) { const char* p = (const char*)memchr(str, '\n', str_end - str); return p ? p : str_end; } const ImWchar* ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin) // find beginning-of-line { while (buf_mid_line > buf_begin && buf_mid_line[-1] != '\n') buf_mid_line--; return buf_mid_line; } const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end) { if (!needle_end) needle_end = needle + strlen(needle); const char un0 = (char)toupper(*needle); while ((!haystack_end && *haystack) || (haystack_end && haystack < haystack_end)) { if (toupper(*haystack) == un0) { const char* b = needle + 1; for (const char* a = haystack + 1; b < needle_end; a++, b++) if (toupper(*a) != toupper(*b)) break; if (b == needle_end) return haystack; } haystack++; } return NULL; } // 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. void ImStrTrimBlanks(char* buf) { char* p = buf; while (p[0] == ' ' || p[0] == '\t') // Leading blanks p++; char* p_start = p; while (*p != 0) // Find end of string p++; while (p > p_start && (p[-1] == ' ' || p[-1] == '\t')) // Trailing blanks p--; if (p_start != buf) // Copy memory if we had leading blanks memmove(buf, p_start, p - p_start); buf[p - p_start] = 0; // Zero terminate } const char* ImStrSkipBlank(const char* str) { while (str[0] == ' ' || str[0] == '\t') str++; return str; } // A) MSVC version appears to return -1 on overflow, whereas glibc appears to return total count (which may be >= buf_size). // 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. // B) When buf==NULL vsnprintf() will return the output size. #ifndef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS //#define IMGUI_USE_STB_SPRINTF #ifdef IMGUI_USE_STB_SPRINTF #define STB_SPRINTF_IMPLEMENTATION #include "imstb_sprintf.h" #endif #if defined(_MSC_VER) && !defined(vsnprintf) #define vsnprintf _vsnprintf #endif int ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) { va_list args; va_start(args, fmt); #ifdef IMGUI_USE_STB_SPRINTF int w = stbsp_vsnprintf(buf, (int)buf_size, fmt, args); #else int w = vsnprintf(buf, buf_size, fmt, args); #endif va_end(args); if (buf == NULL) return w; if (w == -1 || w >= (int)buf_size) w = (int)buf_size - 1; buf[w] = 0; return w; } int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) { #ifdef IMGUI_USE_STB_SPRINTF int w = stbsp_vsnprintf(buf, (int)buf_size, fmt, args); #else int w = vsnprintf(buf, buf_size, fmt, args); #endif if (buf == NULL) return w; if (w == -1 || w >= (int)buf_size) w = (int)buf_size - 1; buf[w] = 0; return w; } #endif // #ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // CRC32 needs a 1KB lookup table (not cache friendly) // Although the code to generate the table is simple and shorter than the table itself, using a const table allows us to easily: // - avoid an unnecessary branch/memory tap, - keep the ImHashXXX functions usable by static constructors, - make it thread-safe. static const ImU32 GCrc32LookupTable[256] = { 0x00000000,0x77073096,0xEE0E612C,0x990951BA,0x076DC419,0x706AF48F,0xE963A535,0x9E6495A3,0x0EDB8832,0x79DCB8A4,0xE0D5E91E,0x97D2D988,0x09B64C2B,0x7EB17CBD,0xE7B82D07,0x90BF1D91, 0x1DB71064,0x6AB020F2,0xF3B97148,0x84BE41DE,0x1ADAD47D,0x6DDDE4EB,0xF4D4B551,0x83D385C7,0x136C9856,0x646BA8C0,0xFD62F97A,0x8A65C9EC,0x14015C4F,0x63066CD9,0xFA0F3D63,0x8D080DF5, 0x3B6E20C8,0x4C69105E,0xD56041E4,0xA2677172,0x3C03E4D1,0x4B04D447,0xD20D85FD,0xA50AB56B,0x35B5A8FA,0x42B2986C,0xDBBBC9D6,0xACBCF940,0x32D86CE3,0x45DF5C75,0xDCD60DCF,0xABD13D59, 0x26D930AC,0x51DE003A,0xC8D75180,0xBFD06116,0x21B4F4B5,0x56B3C423,0xCFBA9599,0xB8BDA50F,0x2802B89E,0x5F058808,0xC60CD9B2,0xB10BE924,0x2F6F7C87,0x58684C11,0xC1611DAB,0xB6662D3D, 0x76DC4190,0x01DB7106,0x98D220BC,0xEFD5102A,0x71B18589,0x06B6B51F,0x9FBFE4A5,0xE8B8D433,0x7807C9A2,0x0F00F934,0x9609A88E,0xE10E9818,0x7F6A0DBB,0x086D3D2D,0x91646C97,0xE6635C01, 0x6B6B51F4,0x1C6C6162,0x856530D8,0xF262004E,0x6C0695ED,0x1B01A57B,0x8208F4C1,0xF50FC457,0x65B0D9C6,0x12B7E950,0x8BBEB8EA,0xFCB9887C,0x62DD1DDF,0x15DA2D49,0x8CD37CF3,0xFBD44C65, 0x4DB26158,0x3AB551CE,0xA3BC0074,0xD4BB30E2,0x4ADFA541,0x3DD895D7,0xA4D1C46D,0xD3D6F4FB,0x4369E96A,0x346ED9FC,0xAD678846,0xDA60B8D0,0x44042D73,0x33031DE5,0xAA0A4C5F,0xDD0D7CC9, 0x5005713C,0x270241AA,0xBE0B1010,0xC90C2086,0x5768B525,0x206F85B3,0xB966D409,0xCE61E49F,0x5EDEF90E,0x29D9C998,0xB0D09822,0xC7D7A8B4,0x59B33D17,0x2EB40D81,0xB7BD5C3B,0xC0BA6CAD, 0xEDB88320,0x9ABFB3B6,0x03B6E20C,0x74B1D29A,0xEAD54739,0x9DD277AF,0x04DB2615,0x73DC1683,0xE3630B12,0x94643B84,0x0D6D6A3E,0x7A6A5AA8,0xE40ECF0B,0x9309FF9D,0x0A00AE27,0x7D079EB1, 0xF00F9344,0x8708A3D2,0x1E01F268,0x6906C2FE,0xF762575D,0x806567CB,0x196C3671,0x6E6B06E7,0xFED41B76,0x89D32BE0,0x10DA7A5A,0x67DD4ACC,0xF9B9DF6F,0x8EBEEFF9,0x17B7BE43,0x60B08ED5, 0xD6D6A3E8,0xA1D1937E,0x38D8C2C4,0x4FDFF252,0xD1BB67F1,0xA6BC5767,0x3FB506DD,0x48B2364B,0xD80D2BDA,0xAF0A1B4C,0x36034AF6,0x41047A60,0xDF60EFC3,0xA867DF55,0x316E8EEF,0x4669BE79, 0xCB61B38C,0xBC66831A,0x256FD2A0,0x5268E236,0xCC0C7795,0xBB0B4703,0x220216B9,0x5505262F,0xC5BA3BBE,0xB2BD0B28,0x2BB45A92,0x5CB36A04,0xC2D7FFA7,0xB5D0CF31,0x2CD99E8B,0x5BDEAE1D, 0x9B64C2B0,0xEC63F226,0x756AA39C,0x026D930A,0x9C0906A9,0xEB0E363F,0x72076785,0x05005713,0x95BF4A82,0xE2B87A14,0x7BB12BAE,0x0CB61B38,0x92D28E9B,0xE5D5BE0D,0x7CDCEFB7,0x0BDBDF21, 0x86D3D2D4,0xF1D4E242,0x68DDB3F8,0x1FDA836E,0x81BE16CD,0xF6B9265B,0x6FB077E1,0x18B74777,0x88085AE6,0xFF0F6A70,0x66063BCA,0x11010B5C,0x8F659EFF,0xF862AE69,0x616BFFD3,0x166CCF45, 0xA00AE278,0xD70DD2EE,0x4E048354,0x3903B3C2,0xA7672661,0xD06016F7,0x4969474D,0x3E6E77DB,0xAED16A4A,0xD9D65ADC,0x40DF0B66,0x37D83BF0,0xA9BCAE53,0xDEBB9EC5,0x47B2CF7F,0x30B5FFE9, 0xBDBDF21C,0xCABAC28A,0x53B39330,0x24B4A3A6,0xBAD03605,0xCDD70693,0x54DE5729,0x23D967BF,0xB3667A2E,0xC4614AB8,0x5D681B02,0x2A6F2B94,0xB40BBE37,0xC30C8EA1,0x5A05DF1B,0x2D02EF8D, }; // Known size hash // It is ok to call ImHashData on a string with known length but the ### operator won't be supported. // FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements. ImU32 ImHashData(const void* data_p, size_t data_size, ImU32 seed) { ImU32 crc = ~seed; const unsigned char* data = (const unsigned char*)data_p; const ImU32* crc32_lut = GCrc32LookupTable; while (data_size-- != 0) crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ *data++]; return ~crc; } // Zero-terminated string hash, with support for ### to reset back to seed value // We support a syntax of "label###id" where only "###id" is included in the hash, and only "label" gets displayed. // Because this syntax is rarely used we are optimizing for the common case. // - If we reach ### in the string we discard the hash so far and reset to the seed. // - We don't do 'current += 2; continue;' after handling ### to keep the code smaller/faster (measured ~10% diff in Debug build) // FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements. ImU32 ImHashStr(const char* data_p, size_t data_size, ImU32 seed) { seed = ~seed; ImU32 crc = seed; const unsigned char* data = (const unsigned char*)data_p; const ImU32* crc32_lut = GCrc32LookupTable; if (data_size != 0) { while (data_size-- != 0) { unsigned char c = *data++; if (c == '#' && data_size >= 2 && data[0] == '#' && data[1] == '#') crc = seed; crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ c]; } } else { while (unsigned char c = *data++) { if (c == '#' && data[0] == '#' && data[1] == '#') crc = seed; crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ c]; } } return ~crc; } FILE* ImFileOpen(const char* filename, const char* mode) { #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__GNUC__) // 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) const int filename_wsize = ImTextCountCharsFromUtf8(filename, NULL) + 1; const int mode_wsize = ImTextCountCharsFromUtf8(mode, NULL) + 1; ImVector buf; buf.resize(filename_wsize + mode_wsize); ImTextStrFromUtf8(&buf[0], filename_wsize, filename, NULL); ImTextStrFromUtf8(&buf[filename_wsize], mode_wsize, mode, NULL); return _wfopen((wchar_t*)&buf[0], (wchar_t*)&buf[filename_wsize]); #else return fopen(filename, mode); #endif } // Load file content into memory // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size, int padding_bytes) { IM_ASSERT(filename && file_open_mode); if (out_file_size) *out_file_size = 0; FILE* f; if ((f = ImFileOpen(filename, file_open_mode)) == NULL) return NULL; long file_size_signed; if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) { fclose(f); return NULL; } size_t file_size = (size_t)file_size_signed; void* file_data = IM_ALLOC(file_size + padding_bytes); if (file_data == NULL) { fclose(f); return NULL; } if (fread(file_data, 1, file_size, f) != file_size) { fclose(f); IM_FREE(file_data); return NULL; } if (padding_bytes > 0) memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); fclose(f); if (out_file_size) *out_file_size = file_size; return file_data; } //----------------------------------------------------------------------------- // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) //----------------------------------------------------------------------------- // Convert UTF-8 to 32-bits character, process single character input. // Based on stb_from_utf8() from github.com/nothings/stb/ // We handle UTF-8 decoding error by skipping forward. int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end) { unsigned int c = (unsigned int)-1; const unsigned char* str = (const unsigned char*)in_text; if (!(*str & 0x80)) { c = (unsigned int)(*str++); *out_char = c; return 1; } if ((*str & 0xe0) == 0xc0) { *out_char = 0xFFFD; // will be invalid but not end of string if (in_text_end && in_text_end - (const char*)str < 2) return 1; if (*str < 0xc2) return 2; c = (unsigned int)((*str++ & 0x1f) << 6); if ((*str & 0xc0) != 0x80) return 2; c += (*str++ & 0x3f); *out_char = c; return 2; } if ((*str & 0xf0) == 0xe0) { *out_char = 0xFFFD; // will be invalid but not end of string if (in_text_end && in_text_end - (const char*)str < 3) return 1; if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return 3; if (*str == 0xed && str[1] > 0x9f) return 3; // str[1] < 0x80 is checked below c = (unsigned int)((*str++ & 0x0f) << 12); if ((*str & 0xc0) != 0x80) return 3; c += (unsigned int)((*str++ & 0x3f) << 6); if ((*str & 0xc0) != 0x80) return 3; c += (*str++ & 0x3f); *out_char = c; return 3; } if ((*str & 0xf8) == 0xf0) { *out_char = 0xFFFD; // will be invalid but not end of string if (in_text_end && in_text_end - (const char*)str < 4) return 1; if (*str > 0xf4) return 4; if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return 4; if (*str == 0xf4 && str[1] > 0x8f) return 4; // str[1] < 0x80 is checked below c = (unsigned int)((*str++ & 0x07) << 18); if ((*str & 0xc0) != 0x80) return 4; c += (unsigned int)((*str++ & 0x3f) << 12); if ((*str & 0xc0) != 0x80) return 4; c += (unsigned int)((*str++ & 0x3f) << 6); if ((*str & 0xc0) != 0x80) return 4; c += (*str++ & 0x3f); // utf-8 encodings of values used in surrogate pairs are invalid if ((c & 0xFFFFF800) == 0xD800) return 4; *out_char = c; return 4; } *out_char = 0; return 0; } int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining) { ImWchar* buf_out = buf; ImWchar* buf_end = buf + buf_size; while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text) { unsigned int c; in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); if (c == 0) break; if (c < 0x10000) // FIXME: Losing characters that don't fit in 2 bytes *buf_out++ = (ImWchar)c; } *buf_out = 0; if (in_text_remaining) *in_text_remaining = in_text; return (int)(buf_out - buf); } int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end) { int char_count = 0; while ((!in_text_end || in_text < in_text_end) && *in_text) { unsigned int c; in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); if (c == 0) break; if (c < 0x10000) char_count++; } return char_count; } // Based on stb_to_utf8() from github.com/nothings/stb/ static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c) { if (c < 0x80) { buf[0] = (char)c; return 1; } if (c < 0x800) { if (buf_size < 2) return 0; buf[0] = (char)(0xc0 + (c >> 6)); buf[1] = (char)(0x80 + (c & 0x3f)); return 2; } if (c >= 0xdc00 && c < 0xe000) { return 0; } if (c >= 0xd800 && c < 0xdc00) { if (buf_size < 4) return 0; buf[0] = (char)(0xf0 + (c >> 18)); buf[1] = (char)(0x80 + ((c >> 12) & 0x3f)); buf[2] = (char)(0x80 + ((c >> 6) & 0x3f)); buf[3] = (char)(0x80 + ((c ) & 0x3f)); return 4; } //else if (c < 0x10000) { if (buf_size < 3) return 0; buf[0] = (char)(0xe0 + (c >> 12)); buf[1] = (char)(0x80 + ((c>> 6) & 0x3f)); buf[2] = (char)(0x80 + ((c ) & 0x3f)); return 3; } } // Not optimal but we very rarely use this function. int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end) { unsigned int dummy = 0; return ImTextCharFromUtf8(&dummy, in_text, in_text_end); } static inline int ImTextCountUtf8BytesFromChar(unsigned int c) { if (c < 0x80) return 1; if (c < 0x800) return 2; if (c >= 0xdc00 && c < 0xe000) return 0; if (c >= 0xd800 && c < 0xdc00) return 4; return 3; } int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end) { char* buf_out = buf; const char* buf_end = buf + buf_size; while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text) { unsigned int c = (unsigned int)(*in_text++); if (c < 0x80) *buf_out++ = (char)c; else buf_out += ImTextCharToUtf8(buf_out, (int)(buf_end-buf_out-1), c); } *buf_out = 0; return (int)(buf_out - buf); } int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end) { int bytes_count = 0; while ((!in_text_end || in_text < in_text_end) && *in_text) { unsigned int c = (unsigned int)(*in_text++); if (c < 0x80) bytes_count++; else bytes_count += ImTextCountUtf8BytesFromChar(c); } return bytes_count; } //----------------------------------------------------------------------------- // [SECTION] MISC HELPERS/UTILTIES (Color functions) // Note: The Convert functions are early design which are not consistent with other API. //----------------------------------------------------------------------------- ImVec4 ImGui::ColorConvertU32ToFloat4(ImU32 in) { float s = 1.0f/255.0f; return ImVec4( ((in >> IM_COL32_R_SHIFT) & 0xFF) * s, ((in >> IM_COL32_G_SHIFT) & 0xFF) * s, ((in >> IM_COL32_B_SHIFT) & 0xFF) * s, ((in >> IM_COL32_A_SHIFT) & 0xFF) * s); } ImU32 ImGui::ColorConvertFloat4ToU32(const ImVec4& in) { ImU32 out; out = ((ImU32)IM_F32_TO_INT8_SAT(in.x)) << IM_COL32_R_SHIFT; out |= ((ImU32)IM_F32_TO_INT8_SAT(in.y)) << IM_COL32_G_SHIFT; out |= ((ImU32)IM_F32_TO_INT8_SAT(in.z)) << IM_COL32_B_SHIFT; out |= ((ImU32)IM_F32_TO_INT8_SAT(in.w)) << IM_COL32_A_SHIFT; return out; } // Convert rgb floats ([0-1],[0-1],[0-1]) to hsv floats ([0-1],[0-1],[0-1]), from Foley & van Dam p592 // Optimized http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv void ImGui::ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v) { float K = 0.f; if (g < b) { ImSwap(g, b); K = -1.f; } if (r < g) { ImSwap(r, g); K = -2.f / 6.f - K; } const float chroma = r - (g < b ? g : b); out_h = ImFabs(K + (g - b) / (6.f * chroma + 1e-20f)); out_s = chroma / (r + 1e-20f); out_v = r; } // Convert hsv floats ([0-1],[0-1],[0-1]) to rgb floats ([0-1],[0-1],[0-1]), from Foley & van Dam p593 // also http://en.wikipedia.org/wiki/HSL_and_HSV void ImGui::ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b) { if (s == 0.0f) { // gray out_r = out_g = out_b = v; return; } h = ImFmod(h, 1.0f) / (60.0f/360.0f); int i = (int)h; float f = h - (float)i; float p = v * (1.0f - s); float q = v * (1.0f - s * f); float t = v * (1.0f - s * (1.0f - f)); switch (i) { case 0: out_r = v; out_g = t; out_b = p; break; case 1: out_r = q; out_g = v; out_b = p; break; case 2: out_r = p; out_g = v; out_b = t; break; case 3: out_r = p; out_g = q; out_b = v; break; case 4: out_r = t; out_g = p; out_b = v; break; case 5: default: out_r = v; out_g = p; out_b = q; break; } } ImU32 ImGui::GetColorU32(ImGuiCol idx, float alpha_mul) { ImGuiStyle& style = GImGui->Style; ImVec4 c = style.Colors[idx]; c.w *= style.Alpha * alpha_mul; return ColorConvertFloat4ToU32(c); } ImU32 ImGui::GetColorU32(const ImVec4& col) { ImGuiStyle& style = GImGui->Style; ImVec4 c = col; c.w *= style.Alpha; return ColorConvertFloat4ToU32(c); } const ImVec4& ImGui::GetStyleColorVec4(ImGuiCol idx) { ImGuiStyle& style = GImGui->Style; return style.Colors[idx]; } ImU32 ImGui::GetColorU32(ImU32 col) { float style_alpha = GImGui->Style.Alpha; if (style_alpha >= 1.0f) return col; ImU32 a = (col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT; a = (ImU32)(a * style_alpha); // We don't need to clamp 0..255 because Style.Alpha is in 0..1 range. return (col & ~IM_COL32_A_MASK) | (a << IM_COL32_A_SHIFT); } //----------------------------------------------------------------------------- // [SECTION] ImGuiStorage // Helper: Key->value storage //----------------------------------------------------------------------------- // std::lower_bound but without the bullshit static ImGuiStorage::Pair* LowerBound(ImVector& data, ImGuiID key) { ImGuiStorage::Pair* first = data.Data; ImGuiStorage::Pair* last = data.Data + data.Size; size_t count = (size_t)(last - first); while (count > 0) { size_t count2 = count >> 1; ImGuiStorage::Pair* mid = first + count2; if (mid->key < key) { first = ++mid; count -= count2 + 1; } else { count = count2; } } return first; } // For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once. void ImGuiStorage::BuildSortByKey() { struct StaticFunc { static int IMGUI_CDECL PairCompareByID(const void* lhs, const void* rhs) { // We can't just do a subtraction because qsort uses signed integers and subtracting our ID doesn't play well with that. if (((const Pair*)lhs)->key > ((const Pair*)rhs)->key) return +1; if (((const Pair*)lhs)->key < ((const Pair*)rhs)->key) return -1; return 0; } }; if (Data.Size > 1) ImQsort(Data.Data, (size_t)Data.Size, sizeof(Pair), StaticFunc::PairCompareByID); } int ImGuiStorage::GetInt(ImGuiID key, int default_val) const { ImGuiStorage::Pair* it = LowerBound(const_cast&>(Data), key); if (it == Data.end() || it->key != key) return default_val; return it->val_i; } bool ImGuiStorage::GetBool(ImGuiID key, bool default_val) const { return GetInt(key, default_val ? 1 : 0) != 0; } float ImGuiStorage::GetFloat(ImGuiID key, float default_val) const { ImGuiStorage::Pair* it = LowerBound(const_cast&>(Data), key); if (it == Data.end() || it->key != key) return default_val; return it->val_f; } void* ImGuiStorage::GetVoidPtr(ImGuiID key) const { ImGuiStorage::Pair* it = LowerBound(const_cast&>(Data), key); if (it == Data.end() || it->key != key) return NULL; return it->val_p; } // 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. int* ImGuiStorage::GetIntRef(ImGuiID key, int default_val) { ImGuiStorage::Pair* it = LowerBound(Data, key); if (it == Data.end() || it->key != key) it = Data.insert(it, Pair(key, default_val)); return &it->val_i; } bool* ImGuiStorage::GetBoolRef(ImGuiID key, bool default_val) { return (bool*)GetIntRef(key, default_val ? 1 : 0); } float* ImGuiStorage::GetFloatRef(ImGuiID key, float default_val) { ImGuiStorage::Pair* it = LowerBound(Data, key); if (it == Data.end() || it->key != key) it = Data.insert(it, Pair(key, default_val)); return &it->val_f; } void** ImGuiStorage::GetVoidPtrRef(ImGuiID key, void* default_val) { ImGuiStorage::Pair* it = LowerBound(Data, key); if (it == Data.end() || it->key != key) it = Data.insert(it, Pair(key, default_val)); return &it->val_p; } // 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) void ImGuiStorage::SetInt(ImGuiID key, int val) { ImGuiStorage::Pair* it = LowerBound(Data, key); if (it == Data.end() || it->key != key) { Data.insert(it, Pair(key, val)); return; } it->val_i = val; } void ImGuiStorage::SetBool(ImGuiID key, bool val) { SetInt(key, val ? 1 : 0); } void ImGuiStorage::SetFloat(ImGuiID key, float val) { ImGuiStorage::Pair* it = LowerBound(Data, key); if (it == Data.end() || it->key != key) { Data.insert(it, Pair(key, val)); return; } it->val_f = val; } void ImGuiStorage::SetVoidPtr(ImGuiID key, void* val) { ImGuiStorage::Pair* it = LowerBound(Data, key); if (it == Data.end() || it->key != key) { Data.insert(it, Pair(key, val)); return; } it->val_p = val; } void ImGuiStorage::SetAllInt(int v) { for (int i = 0; i < Data.Size; i++) Data[i].val_i = v; } //----------------------------------------------------------------------------- // [SECTION] ImGuiTextFilter //----------------------------------------------------------------------------- // Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]" ImGuiTextFilter::ImGuiTextFilter(const char* default_filter) { if (default_filter) { ImStrncpy(InputBuf, default_filter, IM_ARRAYSIZE(InputBuf)); Build(); } else { InputBuf[0] = 0; CountGrep = 0; } } bool ImGuiTextFilter::Draw(const char* label, float width) { if (width != 0.0f) ImGui::SetNextItemWidth(width); bool value_changed = ImGui::InputText(label, InputBuf, IM_ARRAYSIZE(InputBuf)); if (value_changed) Build(); return value_changed; } void ImGuiTextFilter::TextRange::split(char separator, ImVector* out) const { out->resize(0); const char* wb = b; const char* we = wb; while (we < e) { if (*we == separator) { out->push_back(TextRange(wb, we)); wb = we + 1; } we++; } if (wb != we) out->push_back(TextRange(wb, we)); } void ImGuiTextFilter::Build() { Filters.resize(0); TextRange input_range(InputBuf, InputBuf+strlen(InputBuf)); input_range.split(',', &Filters); CountGrep = 0; for (int i = 0; i != Filters.Size; i++) { TextRange& f = Filters[i]; while (f.b < f.e && ImCharIsBlankA(f.b[0])) f.b++; while (f.e > f.b && ImCharIsBlankA(f.e[-1])) f.e--; if (f.empty()) continue; if (Filters[i].b[0] != '-') CountGrep += 1; } } bool ImGuiTextFilter::PassFilter(const char* text, const char* text_end) const { if (Filters.empty()) return true; if (text == NULL) text = ""; for (int i = 0; i != Filters.Size; i++) { const TextRange& f = Filters[i]; if (f.empty()) continue; if (f.b[0] == '-') { // Subtract if (ImStristr(text, text_end, f.begin()+1, f.end()) != NULL) return false; } else { // Grep if (ImStristr(text, text_end, f.begin(), f.end()) != NULL) return true; } } // Implicit * grep if (CountGrep == 0) return true; return false; } //----------------------------------------------------------------------------- // [SECTION] ImGuiTextBuffer //----------------------------------------------------------------------------- // On some platform vsnprintf() takes va_list by reference and modifies it. // va_copy is the 'correct' way to copy a va_list but Visual Studio prior to 2013 doesn't have it. #ifndef va_copy #if defined(__GNUC__) || defined(__clang__) #define va_copy(dest, src) __builtin_va_copy(dest, src) #else #define va_copy(dest, src) (dest = src) #endif #endif char ImGuiTextBuffer::EmptyString[1] = { 0 }; void ImGuiTextBuffer::append(const char* str, const char* str_end) { int len = str_end ? (int)(str_end - str) : (int)strlen(str); // Add zero-terminator the first time const int write_off = (Buf.Size != 0) ? Buf.Size : 1; const int needed_sz = write_off + len; if (write_off + len >= Buf.Capacity) { int new_capacity = Buf.Capacity * 2; Buf.reserve(needed_sz > new_capacity ? needed_sz : new_capacity); } Buf.resize(needed_sz); memcpy(&Buf[write_off - 1], str, (size_t)len); Buf[write_off - 1 + len] = 0; } void ImGuiTextBuffer::appendf(const char* fmt, ...) { va_list args; va_start(args, fmt); appendfv(fmt, args); va_end(args); } // Helper: Text buffer for logging/accumulating text void ImGuiTextBuffer::appendfv(const char* fmt, va_list args) { va_list args_copy; va_copy(args_copy, args); int len = ImFormatStringV(NULL, 0, fmt, args); // FIXME-OPT: could do a first pass write attempt, likely successful on first pass. if (len <= 0) { va_end(args_copy); return; } // Add zero-terminator the first time const int write_off = (Buf.Size != 0) ? Buf.Size : 1; const int needed_sz = write_off + len; if (write_off + len >= Buf.Capacity) { int new_capacity = Buf.Capacity * 2; Buf.reserve(needed_sz > new_capacity ? needed_sz : new_capacity); } Buf.resize(needed_sz); ImFormatStringV(&Buf[write_off - 1], (size_t)len + 1, fmt, args_copy); va_end(args_copy); } //----------------------------------------------------------------------------- // [SECTION] ImGuiListClipper // This is currently not as flexible/powerful as it should be, needs some rework (see TODO) //----------------------------------------------------------------------------- static void SetCursorPosYAndSetupDummyPrevLine(float pos_y, float line_height) { // Set cursor position and a few other things so that SetScrollHereY() and Columns() can work when seeking cursor. // FIXME: It is problematic that we have to do that here, because custom/equivalent end-user code would stumble on the same issue. // The clipper should probably have a 4th step to display the last item in a regular manner. ImGui::SetCursorPosY(pos_y); ImGuiWindow* window = ImGui::GetCurrentWindow(); 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. 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. if (window->DC.CurrentColumns) window->DC.CurrentColumns->LineMinY = window->DC.CursorPos.y; // Setting this so that cell Y position are set properly } // Use case A: Begin() called from constructor with items_height<0, then called again from Sync() in StepNo 1 // Use case B: Begin() called from constructor with items_height>0 // 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. void ImGuiListClipper::Begin(int count, float items_height) { StartPosY = ImGui::GetCursorPosY(); ItemsHeight = items_height; ItemsCount = count; StepNo = 0; DisplayEnd = DisplayStart = -1; if (ItemsHeight > 0.0f) { ImGui::CalcListClipping(ItemsCount, ItemsHeight, &DisplayStart, &DisplayEnd); // calculate how many to clip/display if (DisplayStart > 0) SetCursorPosYAndSetupDummyPrevLine(StartPosY + DisplayStart * ItemsHeight, ItemsHeight); // advance cursor StepNo = 2; } } void ImGuiListClipper::End() { if (ItemsCount < 0) return; // 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. if (ItemsCount < INT_MAX) SetCursorPosYAndSetupDummyPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); // advance cursor ItemsCount = -1; StepNo = 3; } bool ImGuiListClipper::Step() { if (ItemsCount == 0 || ImGui::GetCurrentWindowRead()->SkipItems) { ItemsCount = -1; return false; } 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. { DisplayStart = 0; DisplayEnd = 1; StartPosY = ImGui::GetCursorPosY(); StepNo = 1; return true; } 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. { if (ItemsCount == 1) { ItemsCount = -1; return false; } float items_height = ImGui::GetCursorPosY() - StartPosY; IM_ASSERT(items_height > 0.0f); // If this triggers, it means Item 0 hasn't moved the cursor vertically Begin(ItemsCount-1, items_height); DisplayStart++; DisplayEnd++; StepNo = 3; return true; } 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. { IM_ASSERT(DisplayStart >= 0 && DisplayEnd >= 0); StepNo = 3; return true; } 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. End(); return false; } //----------------------------------------------------------------------------- // [SECTION] RENDER HELPERS // Those (internal) functions are currently quite a legacy mess - their signature and behavior will change. // Also see imgui_draw.cpp for some more which have been reworked to not rely on ImGui:: state. //----------------------------------------------------------------------------- const char* ImGui::FindRenderedTextEnd(const char* text, const char* text_end) { const char* text_display_end = text; if (!text_end) text_end = (const char*)-1; while (text_display_end < text_end && *text_display_end != '\0' && (text_display_end[0] != '#' || text_display_end[1] != '#')) text_display_end++; return text_display_end; } // Internal ImGui functions to render text // RenderText***() functions calls ImDrawList::AddText() calls ImBitmapFont::RenderText() void ImGui::RenderText(ImVec2 pos, const char* text, const char* text_end, bool hide_text_after_hash) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; // Hide anything after a '##' string const char* text_display_end; if (hide_text_after_hash) { text_display_end = FindRenderedTextEnd(text, text_end); } else { if (!text_end) text_end = text + strlen(text); // FIXME-OPT text_display_end = text_end; } if (text != text_display_end) { window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_display_end); if (g.LogEnabled) LogRenderedText(&pos, text, text_display_end); } } void ImGui::RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (!text_end) text_end = text + strlen(text); // FIXME-OPT if (text != text_end) { window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_end, wrap_width); if (g.LogEnabled) LogRenderedText(&pos, text, text_end); } } // Default clip_rect uses (pos_min,pos_max) // Handle clipping on CPU immediately (vs typically let the GPU clip the triangles that are overlapping the clipping rectangle edges) void 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) { // Perform CPU side clipping for single clipped element to avoid using scissor state ImVec2 pos = pos_min; const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_display_end, false, 0.0f); const ImVec2* clip_min = clip_rect ? &clip_rect->Min : &pos_min; const ImVec2* clip_max = clip_rect ? &clip_rect->Max : &pos_max; bool need_clipping = (pos.x + text_size.x >= clip_max->x) || (pos.y + text_size.y >= clip_max->y); if (clip_rect) // If we had no explicit clipping rectangle then pos==clip_min need_clipping |= (pos.x < clip_min->x) || (pos.y < clip_min->y); // Align whole block. We should defer that to the better rendering function when we'll have support for individual line alignment. if (align.x > 0.0f) pos.x = ImMax(pos.x, pos.x + (pos_max.x - pos.x - text_size.x) * align.x); if (align.y > 0.0f) pos.y = ImMax(pos.y, pos.y + (pos_max.y - pos.y - text_size.y) * align.y); // Render if (need_clipping) { ImVec4 fine_clip_rect(clip_min->x, clip_min->y, clip_max->x, clip_max->y); draw_list->AddText(NULL, 0.0f, pos, GetColorU32(ImGuiCol_Text), text, text_display_end, 0.0f, &fine_clip_rect); } else { draw_list->AddText(NULL, 0.0f, pos, GetColorU32(ImGuiCol_Text), text, text_display_end, 0.0f, NULL); } } void 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) { // Hide anything after a '##' string const char* text_display_end = FindRenderedTextEnd(text, text_end); const int text_len = (int)(text_display_end - text); if (text_len == 0) return; ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; RenderTextClippedEx(window->DrawList, pos_min, pos_max, text, text_display_end, text_size_if_known, align, clip_rect); if (g.LogEnabled) LogRenderedText(&pos_min, text, text_display_end); } // Render a rectangle shaped with optional rounding and borders void ImGui::RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; window->DrawList->AddRectFilled(p_min, p_max, fill_col, rounding); const float border_size = g.Style.FrameBorderSize; if (border && border_size > 0.0f) { window->DrawList->AddRect(p_min+ImVec2(1,1), p_max+ImVec2(1,1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); } } void ImGui::RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; const float border_size = g.Style.FrameBorderSize; if (border_size > 0.0f) { window->DrawList->AddRect(p_min+ImVec2(1,1), p_max+ImVec2(1,1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); } } // 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 void ImGui::RenderArrow(ImVec2 p_min, ImGuiDir dir, float scale) { ImGuiContext& g = *GImGui; const float h = g.FontSize * 1.00f; float r = h * 0.40f * scale; ImVec2 center = p_min + ImVec2(h * 0.50f, h * 0.50f * scale); ImVec2 a, b, c; switch (dir) { case ImGuiDir_Up: case ImGuiDir_Down: if (dir == ImGuiDir_Up) r = -r; a = ImVec2(+0.000f,+0.750f) * r; b = ImVec2(-0.866f,-0.750f) * r; c = ImVec2(+0.866f,-0.750f) * r; break; case ImGuiDir_Left: case ImGuiDir_Right: if (dir == ImGuiDir_Left) r = -r; a = ImVec2(+0.750f,+0.000f) * r; b = ImVec2(-0.750f,+0.866f) * r; c = ImVec2(-0.750f,-0.866f) * r; break; case ImGuiDir_None: case ImGuiDir_COUNT: IM_ASSERT(0); break; } g.CurrentWindow->DrawList->AddTriangleFilled(center + a, center + b, center + c, GetColorU32(ImGuiCol_Text)); } void ImGui::RenderBullet(ImVec2 pos) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; window->DrawList->AddCircleFilled(pos, g.FontSize*0.20f, GetColorU32(ImGuiCol_Text), 8); } void ImGui::RenderCheckMark(ImVec2 pos, ImU32 col, float sz) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; float thickness = ImMax(sz / 5.0f, 1.0f); sz -= thickness*0.5f; pos += ImVec2(thickness*0.25f, thickness*0.25f); float third = sz / 3.0f; float bx = pos.x + third; float by = pos.y + sz - third*0.5f; window->DrawList->PathLineTo(ImVec2(bx - third, by - third)); window->DrawList->PathLineTo(ImVec2(bx, by)); window->DrawList->PathLineTo(ImVec2(bx + third*2, by - third*2)); window->DrawList->PathStroke(col, false, thickness); } void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags) { ImGuiContext& g = *GImGui; if (id != g.NavId) return; if (g.NavDisableHighlight && !(flags & ImGuiNavHighlightFlags_AlwaysDraw)) return; ImGuiWindow* window = g.CurrentWindow; if (window->DC.NavHideHighlightOneFrame) return; float rounding = (flags & ImGuiNavHighlightFlags_NoRounding) ? 0.0f : g.Style.FrameRounding; ImRect display_rect = bb; display_rect.ClipWith(window->ClipRect); if (flags & ImGuiNavHighlightFlags_TypeDefault) { const float THICKNESS = 2.0f; const float DISTANCE = 3.0f + THICKNESS * 0.5f; display_rect.Expand(ImVec2(DISTANCE,DISTANCE)); bool fully_visible = window->ClipRect.Contains(display_rect); if (!fully_visible) window->DrawList->PushClipRect(display_rect.Min, display_rect.Max); 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); if (!fully_visible) window->DrawList->PopClipRect(); } if (flags & ImGuiNavHighlightFlags_TypeThin) { window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, ~0, 1.0f); } } //----------------------------------------------------------------------------- // [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) //----------------------------------------------------------------------------- // ImGuiWindow is mostly a dumb struct. It merely has a constructor and a few helper methods ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) : DrawListInst(&context->DrawListSharedData) { Name = ImStrdup(name); ID = ImHashStr(name); IDStack.push_back(ID); Flags = FlagsPreviousFrame = ImGuiWindowFlags_None; Viewport = NULL; ViewportId = 0; ViewportAllowPlatformMonitorExtend = -1; ViewportPos = ImVec2(FLT_MAX, FLT_MAX); Pos = ImVec2(0.0f, 0.0f); Size = SizeFull = ImVec2(0.0f, 0.0f); SizeContents = SizeContentsExplicit = ImVec2(0.0f, 0.0f); WindowPadding = ImVec2(0.0f, 0.0f); WindowRounding = 0.0f; WindowBorderSize = 0.0f; NameBufLen = (int)strlen(name) + 1; MoveId = GetID("#MOVE"); ChildId = 0; Scroll = ImVec2(0.0f, 0.0f); ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); ScrollTargetCenterRatio = ImVec2(0.5f, 0.5f); ScrollbarSizes = ImVec2(0.0f, 0.0f); ScrollbarX = ScrollbarY = false; ViewportOwned = false; Active = WasActive = false; WriteAccessed = false; Collapsed = false; WantCollapseToggle = false; SkipItems = false; Appearing = false; Hidden = false; HasCloseButton = false; ResizeBorderHeld = -1; BeginCount = 0; BeginOrderWithinParent = -1; BeginOrderWithinContext = -1; PopupId = 0; AutoFitFramesX = AutoFitFramesY = -1; AutoFitOnlyGrows = false; AutoFitChildAxises = 0x00; AutoPosLastDirection = ImGuiDir_None; HiddenFramesCanSkipItems = HiddenFramesCannotSkipItems = 0; SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = SetWindowDockAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing; SetWindowPosVal = SetWindowPosPivot = ImVec2(FLT_MAX, FLT_MAX); LastFrameActive = -1; LastFrameJustFocused = -1; ItemWidthDefault = 0.0f; FontWindowScale = FontDpiScale = 1.0f; SettingsIdx = -1; DrawList = &DrawListInst; DrawList->_OwnerName = Name; ParentWindow = NULL; RootWindow = NULL; RootWindowDockStop = NULL; RootWindowForTitleBarHighlight = NULL; RootWindowForNav = NULL; NavLastIds[0] = NavLastIds[1] = 0; NavRectRel[0] = NavRectRel[1] = ImRect(); NavLastChildNavWindow = NULL; DockNode = DockNodeAsHost = NULL; DockId = 0; DockTabItemStatusFlags = ImGuiItemStatusFlags_None; DockOrder = -1; DockIsActive = DockTabIsVisible = DockTabWantClose = false; } ImGuiWindow::~ImGuiWindow() { IM_ASSERT(DrawList == &DrawListInst); IM_DELETE(Name); for (int i = 0; i != ColumnsStorage.Size; i++) ColumnsStorage[i].~ImGuiColumns(); } ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) { ImGuiID seed = IDStack.back(); ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); ImGui::KeepAliveID(id); return id; } ImGuiID ImGuiWindow::GetID(const void* ptr) { ImGuiID seed = IDStack.back(); ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); ImGui::KeepAliveID(id); return id; } ImGuiID ImGuiWindow::GetIDNoKeepAlive(const char* str, const char* str_end) { ImGuiID seed = IDStack.back(); return ImHashStr(str, str_end ? (str_end - str) : 0, seed); } ImGuiID ImGuiWindow::GetIDNoKeepAlive(const void* ptr) { ImGuiID seed = IDStack.back(); return ImHashData(&ptr, sizeof(void*), seed); } // This is only used in rare/specific situations to manufacture an ID out of nowhere. ImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs) { ImGuiID seed = IDStack.back(); 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) }; ImGuiID id = ImHashData(&r_rel, sizeof(r_rel), seed); ImGui::KeepAliveID(id); return id; } static void SetCurrentWindow(ImGuiWindow* window) { ImGuiContext& g = *GImGui; g.CurrentWindow = window; if (window) g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); } void ImGui::SetNavID(ImGuiID id, int nav_layer) { ImGuiContext& g = *GImGui; IM_ASSERT(g.NavWindow); IM_ASSERT(nav_layer == 0 || nav_layer == 1); g.NavId = id; g.NavWindow->NavLastIds[nav_layer] = id; } void ImGui::SetNavIDWithRectRel(ImGuiID id, int nav_layer, const ImRect& rect_rel) { ImGuiContext& g = *GImGui; SetNavID(id, nav_layer); g.NavWindow->NavRectRel[nav_layer] = rect_rel; g.NavMousePosDirty = true; g.NavDisableHighlight = false; g.NavDisableMouseHover = true; } void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) { ImGuiContext& g = *GImGui; g.ActiveIdIsJustActivated = (g.ActiveId != id); if (g.ActiveIdIsJustActivated) { g.ActiveIdTimer = 0.0f; g.ActiveIdHasBeenPressed = false; g.ActiveIdHasBeenEdited = false; if (id != 0) { g.LastActiveId = id; g.LastActiveIdTimer = 0.0f; } } g.ActiveId = id; g.ActiveIdAllowNavDirFlags = 0; g.ActiveIdBlockNavInputFlags = 0; g.ActiveIdAllowOverlap = false; g.ActiveIdWindow = window; if (id) { g.ActiveIdIsAlive = id; g.ActiveIdSource = (g.NavActivateId == id || g.NavInputId == id || g.NavJustTabbedId == id || g.NavJustMovedToId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse; } } // FIXME-NAV: The existence of SetNavID/SetNavIDWithRectRel/SetFocusID is incredibly messy and confusing and needs some explanation or refactoring. void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) { ImGuiContext& g = *GImGui; IM_ASSERT(id != 0); // Assume that SetFocusID() is called in the context where its NavLayer is the current layer, which is the case everywhere we call it. const ImGuiNavLayer nav_layer = window->DC.NavLayerCurrent; if (g.NavWindow != window) g.NavInitRequest = false; g.NavId = id; g.NavWindow = window; g.NavLayer = nav_layer; window->NavLastIds[nav_layer] = id; if (window->DC.LastItemId == id) window->NavRectRel[nav_layer] = ImRect(window->DC.LastItemRect.Min - window->Pos, window->DC.LastItemRect.Max - window->Pos); if (g.ActiveIdSource == ImGuiInputSource_Nav) g.NavDisableMouseHover = true; else g.NavDisableHighlight = true; } void ImGui::ClearActiveID() { SetActiveID(0, NULL); } void ImGui::SetHoveredID(ImGuiID id) { ImGuiContext& g = *GImGui; g.HoveredId = id; g.HoveredIdAllowOverlap = false; if (id != 0 && g.HoveredIdPreviousFrame != id) g.HoveredIdTimer = g.HoveredIdNotActiveTimer = 0.0f; } ImGuiID ImGui::GetHoveredID() { ImGuiContext& g = *GImGui; return g.HoveredId ? g.HoveredId : g.HoveredIdPreviousFrame; } void ImGui::KeepAliveID(ImGuiID id) { ImGuiContext& g = *GImGui; if (g.ActiveId == id) g.ActiveIdIsAlive = id; if (g.ActiveIdPreviousFrame == id) g.ActiveIdPreviousFrameIsAlive = true; } void ImGui::MarkItemEdited(ImGuiID id) { // This marking is solely to be able to provide info for IsItemDeactivatedAfterEdit(). // 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. ImGuiContext& g = *GImGui; IM_ASSERT(g.ActiveId == id || g.ActiveId == 0 || g.DragDropActive); IM_UNUSED(id); // Avoid unused variable warnings when asserts are compiled out. //IM_ASSERT(g.CurrentWindow->DC.LastItemId == id); g.ActiveIdHasBeenEdited = true; g.CurrentWindow->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Edited; } static inline bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFlags flags) { // An active popup disable hovering on other windows (apart from its own children) // FIXME-OPT: This could be cached/stored within the window. ImGuiContext& g = *GImGui; if (g.NavWindow) if (ImGuiWindow* focused_root_window = g.NavWindow->RootWindow) if (focused_root_window->WasActive && focused_root_window != window->RootWindow) { // For the purpose of those flags we differentiate "standard popup" from "modal popup" // NB: The order of those two tests is important because Modal windows are also Popups. if (focused_root_window->Flags & ImGuiWindowFlags_Modal) return false; if ((focused_root_window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiHoveredFlags_AllowWhenBlockedByPopup)) return false; } // Filter by viewport if (window->Viewport != g.MouseViewport) if (g.MovingWindow == NULL || window->RootWindow != g.MovingWindow->RootWindow) return false; return true; } // Advance cursor given item size for layout. void ImGui::ItemSize(const ImVec2& size, float text_offset_y) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return; // Always align ourselves on pixel boundaries const float line_height = ImMax(window->DC.CurrentLineSize.y, size.y); const float text_base_offset = ImMax(window->DC.CurrentLineTextBaseOffset, text_offset_y); //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] window->DC.CursorPosPrevLine = ImVec2(window->DC.CursorPos.x + size.x, window->DC.CursorPos.y); window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); window->DC.CursorPos.y = (float)(int)(window->DC.CursorPos.y + line_height + g.Style.ItemSpacing.y); window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x); window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y); //if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG] window->DC.PrevLineSize.y = line_height; window->DC.PrevLineTextBaseOffset = text_base_offset; window->DC.CurrentLineSize.y = window->DC.CurrentLineTextBaseOffset = 0.0f; // Horizontal layout mode if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) SameLine(); } void ImGui::ItemSize(const ImRect& bb, float text_offset_y) { ItemSize(bb.GetSize(), text_offset_y); } // Declare item bounding box for clipping and interaction. // Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface // declare their minimum size requirement to ItemSize() and then use a larger region for drawing/interaction, which is passed to ItemAdd(). bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (id != 0) { // Navigation processing runs prior to clipping early-out // (a) So that NavInitRequest can be honored, for newly opened windows to select a default widget // (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. // 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. // 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) window->DC.NavLayerActiveMaskNext |= window->DC.NavLayerCurrentMask; if (g.NavId == id || g.NavAnyRequest) if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) NavProcessItem(window, nav_bb_arg ? *nav_bb_arg : bb, id); } window->DC.LastItemId = id; window->DC.LastItemRect = bb; window->DC.LastItemStatusFlags = ImGuiItemStatusFlags_None; #ifdef IMGUI_ENABLE_TEST_ENGINE if (id != 0) IMGUI_TEST_ENGINE_ITEM_ADD(nav_bb_arg ? *nav_bb_arg : bb, id); #endif // Clipping test const bool is_clipped = IsClippedEx(bb, id, false); if (is_clipped) return false; //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) if (IsMouseHoveringRect(bb.Min, bb.Max)) window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredRect; return true; } // This is roughly matching the behavior of internal-facing ItemHoverable() // - 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() // - this should work even for non-interactive items that have no ID, so we cannot use LastItemId bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (g.NavDisableMouseHover && !g.NavDisableHighlight) return IsItemFocused(); // Test for bounding box overlap, as updated as ItemAdd() if (!(window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect)) return false; IM_ASSERT((flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) == 0); // Flags not supported by this function // Test if we are hovering the right window (our window could be behind another window) // [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. // 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. //if (g.HoveredWindow != window) // return false; if (g.HoveredRootWindow != window->RootWindow && !(flags & ImGuiHoveredFlags_AllowWhenOverlapped)) return false; // Test if another item is active (e.g. being dragged) if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) if (g.ActiveId != 0 && g.ActiveId != window->DC.LastItemId && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) return false; // Test if interactions on this window are blocked by an active popup or modal. // The ImGuiHoveredFlags_AllowWhenBlockedByPopup flag will be tested here. if (!IsWindowContentHoverable(window, flags)) return false; // Test if the item is disabled if ((window->DC.ItemFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) return false; // Special handling for the dummy item after Begin() which represent the title bar or tab. // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. if ((window->DC.LastItemId == window->ID || window->DC.LastItemId == window->MoveId) && window->WriteAccessed) return false; return true; } // Internal facing ItemHoverable() used when submitting widgets. Differs slightly from IsItemHovered(). bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) { ImGuiContext& g = *GImGui; if (g.HoveredId != 0 && g.HoveredId != id && !g.HoveredIdAllowOverlap) return false; ImGuiWindow* window = g.CurrentWindow; if (g.HoveredWindow != window) return false; if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap) return false; if (!IsMouseHoveringRect(bb.Min, bb.Max)) return false; if (g.NavDisableMouseHover || !IsWindowContentHoverable(window, ImGuiHoveredFlags_None)) return false; if (window->DC.ItemFlags & ImGuiItemFlags_Disabled) return false; SetHoveredID(id); return true; } bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (!bb.Overlaps(window->ClipRect)) if (id == 0 || id != g.ActiveId) if (clip_even_when_logged || !g.LogEnabled) return true; return false; } // Process TAB/Shift+TAB. Be mindful that this function may _clear_ the ActiveID when tabbing out. bool ImGui::FocusableItemRegister(ImGuiWindow* window, ImGuiID id) { ImGuiContext& g = *GImGui; // Increment counters const bool is_tab_stop = (window->DC.ItemFlags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0; window->DC.FocusCounterAll++; if (is_tab_stop) window->DC.FocusCounterTab++; // Process TAB/Shift-TAB to tab *OUT* of the currently focused item. // (Note that we can always TAB out of a widget that doesn't allow tabbing in) if (g.ActiveId == id && g.FocusTabPressed && !(g.ActiveIdBlockNavInputFlags & (1 << ImGuiNavInput_KeyTab_)) && g.FocusRequestNextWindow == NULL) { g.FocusRequestNextWindow = window; 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. } // Handle focus requests if (g.FocusRequestCurrWindow == window) { if (window->DC.FocusCounterAll == g.FocusRequestCurrCounterAll) return true; if (is_tab_stop && window->DC.FocusCounterTab == g.FocusRequestCurrCounterTab) { g.NavJustTabbedId = id; return true; } // If another item is about to be focused, we clear our own active id if (g.ActiveId == id) ClearActiveID(); } return false; } void ImGui::FocusableItemUnregister(ImGuiWindow* window) { window->DC.FocusCounterAll--; window->DC.FocusCounterTab--; } float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) { if (wrap_pos_x < 0.0f) return 0.0f; ImGuiWindow* window = GImGui->CurrentWindow; if (wrap_pos_x == 0.0f) wrap_pos_x = GetContentRegionMaxScreen().x; else if (wrap_pos_x > 0.0f) wrap_pos_x += window->Pos.x - window->Scroll.x; // wrap_pos_x is provided is window local space return ImMax(wrap_pos_x - pos.x, 1.0f); } // IM_ALLOC() == ImGui::MemAlloc() void* ImGui::MemAlloc(size_t size) { if (ImGuiContext* ctx = GImGui) ctx->IO.MetricsActiveAllocations++; return GImAllocatorAllocFunc(size, GImAllocatorUserData); } // IM_FREE() == ImGui::MemFree() void ImGui::MemFree(void* ptr) { if (ptr) if (ImGuiContext* ctx = GImGui) ctx->IO.MetricsActiveAllocations--; return GImAllocatorFreeFunc(ptr, GImAllocatorUserData); } const char* ImGui::GetClipboardText() { return GImGui->IO.GetClipboardTextFn ? GImGui->IO.GetClipboardTextFn(GImGui->IO.ClipboardUserData) : ""; } void ImGui::SetClipboardText(const char* text) { if (GImGui->IO.SetClipboardTextFn) GImGui->IO.SetClipboardTextFn(GImGui->IO.ClipboardUserData, text); } const char* ImGui::GetVersion() { return IMGUI_VERSION; } // Internal state access - if you want to share ImGui state between modules (e.g. DLL) or allocate it yourself // 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 ImGuiContext* ImGui::GetCurrentContext() { return GImGui; } void ImGui::SetCurrentContext(ImGuiContext* ctx) { #ifdef IMGUI_SET_CURRENT_CONTEXT_FUNC IMGUI_SET_CURRENT_CONTEXT_FUNC(ctx); // For custom thread-based hackery you may want to have control over this. #else GImGui = ctx; #endif } // Helper function to verify that the type sizes are matching between the calling file's compilation unit and imgui.cpp's compilation unit // 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. bool 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) { bool error = false; if (strcmp(version, IMGUI_VERSION)!=0) { error = true; IM_ASSERT(strcmp(version,IMGUI_VERSION)==0 && "Mismatched version string!"); } if (sz_io != sizeof(ImGuiIO)) { error = true; IM_ASSERT(sz_io == sizeof(ImGuiIO) && "Mismatched struct layout!"); } if (sz_style != sizeof(ImGuiStyle)) { error = true; IM_ASSERT(sz_style == sizeof(ImGuiStyle) && "Mismatched struct layout!"); } if (sz_vec2 != sizeof(ImVec2)) { error = true; IM_ASSERT(sz_vec2 == sizeof(ImVec2) && "Mismatched struct layout!"); } if (sz_vec4 != sizeof(ImVec4)) { error = true; IM_ASSERT(sz_vec4 == sizeof(ImVec4) && "Mismatched struct layout!"); } if (sz_vert != sizeof(ImDrawVert)) { error = true; IM_ASSERT(sz_vert == sizeof(ImDrawVert) && "Mismatched struct layout!"); } return !error; } void ImGui::SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data) { GImAllocatorAllocFunc = alloc_func; GImAllocatorFreeFunc = free_func; GImAllocatorUserData = user_data; } ImGuiContext* ImGui::CreateContext(ImFontAtlas* shared_font_atlas) { ImGuiContext* ctx = IM_NEW(ImGuiContext)(shared_font_atlas); if (GImGui == NULL) SetCurrentContext(ctx); Initialize(ctx); return ctx; } void ImGui::DestroyContext(ImGuiContext* ctx) { if (ctx == NULL) ctx = GImGui; Shutdown(ctx); if (GImGui == ctx) SetCurrentContext(NULL); IM_DELETE(ctx); } ImGuiIO& ImGui::GetIO() { IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); return GImGui->IO; } ImGuiPlatformIO& ImGui::GetPlatformIO() { IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() or ImGui::SetCurrentContext()?"); return GImGui->PlatformIO; } ImGuiStyle& ImGui::GetStyle() { IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); return GImGui->Style; } // Same value as passed to the old io.RenderDrawListsFn function. Valid after Render() and until the next call to NewFrame() ImDrawData* ImGui::GetDrawData() { ImGuiContext& g = *GImGui; return g.Viewports[0]->DrawDataP.Valid ? &g.Viewports[0]->DrawDataP : NULL; } double ImGui::GetTime() { return GImGui->Time; } int ImGui::GetFrameCount() { return GImGui->FrameCount; } static ImDrawList* GetViewportDrawList(ImGuiViewportP* viewport, size_t drawlist_no, const char* drawlist_name) { // Create the draw list on demand, because they are not frequently used for all viewports ImGuiContext& g = *GImGui; IM_ASSERT(drawlist_no >= 0 && drawlist_no < IM_ARRAYSIZE(viewport->DrawLists)); ImDrawList* draw_list = viewport->DrawLists[drawlist_no]; if (draw_list == NULL) { draw_list = IM_NEW(ImDrawList)(&g.DrawListSharedData); draw_list->_OwnerName = drawlist_name; viewport->DrawLists[drawlist_no] = draw_list; } // Our ImDrawList system requires that there is always a command if (viewport->LastFrameDrawLists[drawlist_no] != g.FrameCount) { draw_list->Clear(); draw_list->PushTextureID(g.IO.Fonts->TexID); draw_list->PushClipRect(viewport->Pos, viewport->Pos + viewport->Size, false); draw_list->Flags = (g.Style.AntiAliasedLines ? ImDrawListFlags_AntiAliasedLines : 0) | (g.Style.AntiAliasedFill ? ImDrawListFlags_AntiAliasedFill : 0); viewport->LastFrameDrawLists[drawlist_no] = g.FrameCount; } return draw_list; } ImDrawList* ImGui::GetBackgroundDrawList(ImGuiViewport* viewport) { return GetViewportDrawList((ImGuiViewportP*)viewport, 0, "##Background"); } ImDrawList* ImGui::GetBackgroundDrawList() { ImGuiWindow* window = GImGui->CurrentWindow; return GetBackgroundDrawList(window->Viewport); } ImDrawList* ImGui::GetForegroundDrawList(ImGuiViewport* viewport) { return GetViewportDrawList((ImGuiViewportP*)viewport, 1, "##Foreground"); } ImDrawList* ImGui::GetForegroundDrawList() { ImGuiWindow* window = GImGui->CurrentWindow; return GetForegroundDrawList(window->Viewport); } ImDrawListSharedData* ImGui::GetDrawListSharedData() { return &GImGui->DrawListSharedData; } void ImGui::StartMouseMovingWindow(ImGuiWindow* window) { // Set ActiveId even if the _NoMove flag is set. Without it, dragging away from a window with _NoMove would activate hover on other windows. // We _also_ call this when clicking in a window empty space when io.ConfigWindowsMoveFromTitleBarOnly is set, but clear g.MovingWindow afterward. // This is because we want ActiveId to be set even when the window is not permitted to move. ImGuiContext& g = *GImGui; FocusWindow(window); SetActiveID(window->MoveId, window); g.NavDisableHighlight = true; g.ActiveIdClickOffset = g.IO.MousePos - window->RootWindow->Pos; bool can_move_window = true; if ((window->Flags & ImGuiWindowFlags_NoMove) || (window->RootWindow->Flags & ImGuiWindowFlags_NoMove)) can_move_window = false; if (ImGuiDockNode* node = window->DockNodeAsHost) if (node->VisibleWindow && (node->VisibleWindow->Flags & ImGuiWindowFlags_NoMove)) can_move_window = false; if (can_move_window) g.MovingWindow = window; } // Handle mouse moving window // Note: moving window with the navigation keys (Square + d-pad / CTRL+TAB + Arrows) are processed in NavUpdateWindowing() void ImGui::UpdateMouseMovingWindowNewFrame() { ImGuiContext& g = *GImGui; if (g.MovingWindow != NULL) { // We actually want to move the root window. g.MovingWindow == window we clicked on (could be a child window). // We track it to preserve Focus and so that generally ActiveIdWindow == MovingWindow and ActiveId == MovingWindow->MoveId for consistency. KeepAliveID(g.ActiveId); IM_ASSERT(g.MovingWindow && g.MovingWindow->RootWindow); ImGuiWindow* moving_window = g.MovingWindow->RootWindow; if (g.IO.MouseDown[0] && IsMousePosValid(&g.IO.MousePos)) { ImVec2 pos = g.IO.MousePos - g.ActiveIdClickOffset; if (moving_window->Pos.x != pos.x || moving_window->Pos.y != pos.y) { MarkIniSettingsDirty(moving_window); SetWindowPos(moving_window, pos, ImGuiCond_Always); if (moving_window->ViewportOwned) // Synchronize viewport immediately because some overlays may relies on clipping rectangle before we Begin() into the window. moving_window->Viewport->Pos = pos; } FocusWindow(g.MovingWindow); } else { // Try to merge the window back into the main viewport. // This works because MouseViewport should be != MovingWindow->Viewport on release (as per code in UpdateViewports) if (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable) UpdateTryMergeWindowIntoHostViewport(moving_window, g.MouseViewport); // Restore the mouse viewport so that we don't hover the viewport _under_ the moved window during the frame we released the mouse button. if (!IsDragDropPayloadBeingAccepted()) g.MouseViewport = moving_window->Viewport; // Clear the NoInput window flag set by the Viewport system moving_window->Viewport->Flags &= ~ImGuiViewportFlags_NoInputs; ClearActiveID(); g.MovingWindow = NULL; } } else { // When clicking/dragging from a window that has the _NoMove flag, we still set the ActiveId in order to prevent hovering others. if (g.ActiveIdWindow && g.ActiveIdWindow->MoveId == g.ActiveId) { KeepAliveID(g.ActiveId); if (!g.IO.MouseDown[0]) ClearActiveID(); } } } // Initiate moving window, handle left-click and right-click focus void ImGui::UpdateMouseMovingWindowEndFrame() { // Initiate moving window ImGuiContext& g = *GImGui; if (g.ActiveId != 0 || g.HoveredId != 0) return; // Unless we just made a window/popup appear if (g.NavWindow && g.NavWindow->Appearing) return; // Click to focus window and start moving (after we're done with all our widgets) if (g.IO.MouseClicked[0]) { if (g.HoveredRootWindow != NULL) { StartMouseMovingWindow(g.HoveredWindow); if (g.IO.ConfigWindowsMoveFromTitleBarOnly && (!(g.HoveredRootWindow->Flags & ImGuiWindowFlags_NoTitleBar) || g.HoveredWindow->RootWindowDockStop->DockIsActive)) if (!g.HoveredRootWindow->TitleBarRect().Contains(g.IO.MouseClickedPos[0])) g.MovingWindow = NULL; } else if (g.NavWindow != NULL && GetFrontMostPopupModal() == NULL) { // Clicking on void disable focus FocusWindow(NULL); } } // With right mouse button we close popups without changing focus based on where the mouse is aimed // Instead, focus will be restored to the window under the bottom-most closed popup. // (The left mouse button path calls FocusWindow on the hovered window, which will lead NewFrame->ClosePopupsOverWindow to trigger) if (g.IO.MouseClicked[1]) { // Find the top-most window between HoveredWindow and the front most Modal Window. // This is where we can trim the popup stack. ImGuiWindow* modal = GetFrontMostPopupModal(); bool hovered_window_above_modal = false; if (modal == NULL) hovered_window_above_modal = true; for (int i = g.Windows.Size - 1; i >= 0 && hovered_window_above_modal == false; i--) { ImGuiWindow* window = g.Windows[i]; if (window == modal) break; if (window == g.HoveredWindow) hovered_window_above_modal = true; } ClosePopupsOverWindow(hovered_window_above_modal ? g.HoveredWindow : modal, true); } } static void TranslateWindow(ImGuiWindow* window, const ImVec2& delta) { window->Pos += delta; window->ClipRect.Translate(delta); window->OuterRectClipped.Translate(delta); window->InnerMainRect.Translate(delta); window->DC.CursorPos += delta; window->DC.CursorStartPos += delta; window->DC.CursorMaxPos += delta; window->DC.LastItemRect.Translate(delta); window->DC.LastItemDisplayRect.Translate(delta); } static void ScaleWindow(ImGuiWindow* window, float scale) { ImVec2 origin = window->Viewport->Pos; window->Pos = ImFloor((window->Pos - origin) * scale + origin); window->Size = ImFloor(window->Size * scale); window->SizeFull = ImFloor(window->SizeFull * scale); window->SizeContents = ImFloor(window->SizeContents * scale); } static bool IsWindowActiveAndVisible(ImGuiWindow* window) { return (window->Active) && (!window->Hidden); } static void ImGui::UpdateMouseInputs() { ImGuiContext& g = *GImGui; // Round mouse position to avoid spreading non-rounded position (e.g. UpdateManualResize doesn't support them well) if (IsMousePosValid(&g.IO.MousePos)) g.IO.MousePos = g.LastValidMousePos = ImFloor(g.IO.MousePos); // If mouse just appeared or disappeared (usually denoted by -FLT_MAX components) we cancel out movement in MouseDelta if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MousePosPrev)) g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev; else g.IO.MouseDelta = ImVec2(0.0f, 0.0f); if (g.IO.MouseDelta.x != 0.0f || g.IO.MouseDelta.y != 0.0f) g.NavDisableMouseHover = false; g.IO.MousePosPrev = g.IO.MousePos; for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) { g.IO.MouseClicked[i] = g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] < 0.0f; g.IO.MouseReleased[i] = !g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] >= 0.0f; g.IO.MouseDownDurationPrev[i] = g.IO.MouseDownDuration[i]; 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; g.IO.MouseDoubleClicked[i] = false; if (g.IO.MouseClicked[i]) { if ((float)(g.Time - g.IO.MouseClickedTime[i]) < g.IO.MouseDoubleClickTime) { ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); if (ImLengthSqr(delta_from_click_pos) < g.IO.MouseDoubleClickMaxDist * g.IO.MouseDoubleClickMaxDist) g.IO.MouseDoubleClicked[i] = true; g.IO.MouseClickedTime[i] = -FLT_MAX; // so the third click isn't turned into a double-click } else { g.IO.MouseClickedTime[i] = g.Time; } g.IO.MouseClickedPos[i] = g.IO.MousePos; g.IO.MouseDragMaxDistanceAbs[i] = ImVec2(0.0f, 0.0f); g.IO.MouseDragMaxDistanceSqr[i] = 0.0f; } else if (g.IO.MouseDown[i]) { // Maintain the maximum distance we reaching from the initial click position, which is used with dragging threshold ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); g.IO.MouseDragMaxDistanceSqr[i] = ImMax(g.IO.MouseDragMaxDistanceSqr[i], ImLengthSqr(delta_from_click_pos)); 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); 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); } if (g.IO.MouseClicked[i]) // Clicking any mouse button reactivate mouse hovering which may have been deactivated by gamepad/keyboard navigation g.NavDisableMouseHover = false; } } void ImGui::UpdateMouseWheel() { ImGuiContext& g = *GImGui; if (!g.HoveredWindow || g.HoveredWindow->Collapsed) return; if (g.IO.MouseWheel == 0.0f && g.IO.MouseWheelH == 0.0f) return; ImGuiWindow* window = g.HoveredWindow; // Zoom / Scale window // FIXME-OBSOLETE: This is an old feature, it still works but pretty much nobody is using it and may be best redesigned. if (g.IO.MouseWheel != 0.0f && g.IO.KeyCtrl && g.IO.FontAllowUserScaling) { const float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f); const float scale = new_font_scale / window->FontWindowScale; window->FontWindowScale = new_font_scale; if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) { const ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size; window->Pos = ImFloor(window->Pos + offset); window->Size = ImFloor(window->Size * scale); window->SizeFull = ImFloor(window->SizeFull * scale); } return; } // Mouse wheel scrolling // 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). while ((window->Flags & ImGuiWindowFlags_ChildWindow) && (window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs) && window->ParentWindow) window = window->ParentWindow; const bool scroll_allowed = !(window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs); if (scroll_allowed && (g.IO.MouseWheel != 0.0f || g.IO.MouseWheelH != 0.0f) && !g.IO.KeyCtrl) { ImVec2 max_step = (window->ContentsRegionRect.GetSize() + window->WindowPadding * 2.0f) * 0.67f; // Vertical Mouse Wheel Scrolling (hold Shift to scroll horizontally) if (g.IO.MouseWheel != 0.0f && !g.IO.KeyShift) { float scroll_step = ImFloor(ImMin(5 * window->CalcFontSize(), max_step.y)); SetWindowScrollY(window, window->Scroll.y - g.IO.MouseWheel * scroll_step); } else if (g.IO.MouseWheel != 0.0f && g.IO.KeyShift) { float scroll_step = ImFloor(ImMin(2 * window->CalcFontSize(), max_step.x)); SetWindowScrollX(window, window->Scroll.x - g.IO.MouseWheel * scroll_step); } // Horizontal Mouse Wheel Scrolling (for hardware that supports it) if (g.IO.MouseWheelH != 0.0f && !g.IO.KeyShift) { float scroll_step = ImFloor(ImMin(2 * window->CalcFontSize(), max_step.x)); SetWindowScrollX(window, window->Scroll.x - g.IO.MouseWheelH * scroll_step); } } } // 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) void ImGui::UpdateHoveredWindowAndCaptureFlags() { ImGuiContext& g = *GImGui; // Find the window hovered by mouse: // - Child windows can extend beyond the limit of their parent so we need to derive HoveredRootWindow from HoveredWindow. // - 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. // - 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. FindHoveredWindow(); IM_ASSERT(g.HoveredWindow == NULL || g.HoveredWindow == g.MovingWindow || g.HoveredWindow->Viewport == g.MouseViewport); // Modal windows prevents cursor from hovering behind them. ImGuiWindow* modal_window = GetFrontMostPopupModal(); if (modal_window) if (g.HoveredRootWindow && !IsWindowChildOf(g.HoveredRootWindow, modal_window)) g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL; // Disabled mouse? if (g.IO.ConfigFlags & ImGuiConfigFlags_NoMouse) g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL; // 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. int mouse_earliest_button_down = -1; bool mouse_any_down = false; for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) { if (g.IO.MouseClicked[i]) g.IO.MouseDownOwned[i] = (g.HoveredWindow != NULL) || (!g.OpenPopupStack.empty()); mouse_any_down |= g.IO.MouseDown[i]; if (g.IO.MouseDown[i]) if (mouse_earliest_button_down == -1 || g.IO.MouseClickedTime[i] < g.IO.MouseClickedTime[mouse_earliest_button_down]) mouse_earliest_button_down = i; } const bool mouse_avail_to_imgui = (mouse_earliest_button_down == -1) || g.IO.MouseDownOwned[mouse_earliest_button_down]; // If mouse was first clicked outside of ImGui bounds we also cancel out hovering. // FIXME: For patterns of drag and drop across OS windows, we may need to rework/remove this test (first committed 311c0ca9 on 2015/02) const bool mouse_dragging_extern_payload = g.DragDropActive && (g.DragDropSourceFlags & ImGuiDragDropFlags_SourceExtern) != 0; if (!mouse_avail_to_imgui && !mouse_dragging_extern_payload) g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL; // Update io.WantCaptureMouse for the user application (true = dispatch mouse info to imgui, false = dispatch mouse info to imgui + app) if (g.WantCaptureMouseNextFrame != -1) g.IO.WantCaptureMouse = (g.WantCaptureMouseNextFrame != 0); else g.IO.WantCaptureMouse = (mouse_avail_to_imgui && (g.HoveredWindow != NULL || mouse_any_down)) || (!g.OpenPopupStack.empty()); // Update io.WantCaptureKeyboard for the user application (true = dispatch keyboard info to imgui, false = dispatch keyboard info to imgui + app) if (g.WantCaptureKeyboardNextFrame != -1) g.IO.WantCaptureKeyboard = (g.WantCaptureKeyboardNextFrame != 0); else g.IO.WantCaptureKeyboard = (g.ActiveId != 0) || (modal_window != NULL); if (g.IO.NavActive && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && !(g.IO.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard)) g.IO.WantCaptureKeyboard = true; // Update io.WantTextInput flag, this is to allow systems without a keyboard (e.g. mobile, hand-held) to show a software keyboard if possible g.IO.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false; } void ImGui::NewFrame() { IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); ImGuiContext& g = *GImGui; #ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiTestEngineHook_PreNewFrame(&g); #endif // Check user data // (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) IM_ASSERT(g.Initialized); IM_ASSERT((g.IO.DeltaTime > 0.0f || g.FrameCount == 0) && "Need a positive DeltaTime!"); IM_ASSERT((g.FrameCount == 0 || g.FrameCountEnded == g.FrameCount) && "Forgot to call Render() or EndFrame() at the end of the previous frame?"); IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f && "Invalid DisplaySize value!"); IM_ASSERT(g.IO.Fonts->Fonts.Size > 0 && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded() && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); 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)!"); IM_ASSERT(g.Style.WindowMinSize.x >= 1.0f && g.Style.WindowMinSize.y >= 1.0f && "Invalid style setting."); for (int n = 0; n < ImGuiKey_COUNT; 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)"); // 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) if (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) IM_ASSERT(g.IO.KeyMap[ImGuiKey_Space] != -1 && "ImGuiKey_Space is not mapped, required for keyboard navigation."); // Perform simple check: the beta io.ConfigWindowsResizeFromEdges option requires back-end to honor mouse cursor changes and set the ImGuiBackendFlags_HasMouseCursors flag accordingly. if (g.IO.ConfigWindowsResizeFromEdges && !(g.IO.BackendFlags & ImGuiBackendFlags_HasMouseCursors)) g.IO.ConfigWindowsResizeFromEdges = false; // Perform simple checks: multi-viewport and platform windows support if (g.IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { if ((g.IO.BackendFlags & ImGuiBackendFlags_PlatformHasViewports) && (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasViewports)) { IM_ASSERT((g.FrameCount == 0 || g.FrameCount == g.FrameCountPlatformEnded) && "Forgot to call UpdatePlatformWindows() in main loop after EndFrame()? Check examples/ applications for reference."); IM_ASSERT(g.PlatformIO.Platform_CreateWindow != NULL && "Platform init didn't install handlers?"); IM_ASSERT(g.PlatformIO.Platform_DestroyWindow != NULL && "Platform init didn't install handlers?"); IM_ASSERT(g.PlatformIO.Platform_GetWindowPos != NULL && "Platform init didn't install handlers?"); IM_ASSERT(g.PlatformIO.Platform_SetWindowPos != NULL && "Platform init didn't install handlers?"); IM_ASSERT(g.PlatformIO.Platform_GetWindowSize != NULL && "Platform init didn't install handlers?"); IM_ASSERT(g.PlatformIO.Platform_SetWindowSize != NULL && "Platform init didn't install handlers?"); IM_ASSERT(g.PlatformIO.Monitors.Size > 0 && "Platform init didn't setup Monitors list?"); IM_ASSERT((g.Viewports[0]->PlatformUserData != NULL || g.Viewports[0]->PlatformHandle != NULL) && "Platform init didn't setup main viewport."); if (g.IO.ConfigDockingTransparentPayload && (g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable)) IM_ASSERT(g.PlatformIO.Platform_SetWindowAlpha != NULL && "Platform_SetWindowAlpha handler is required to use io.ConfigDockingTransparent!"); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS IM_ASSERT(g.IO.RenderDrawListsFn == NULL); // Call ImGui::Render() then pass ImGui::GetDrawData() yourself to your render function! #endif } else { // Disable feature, our back-ends do not support it g.IO.ConfigFlags &= ~ImGuiConfigFlags_ViewportsEnable; } // Perform simple checks on platform monitor data + compute a total bounding box for quick early outs for (int monitor_n = 0; monitor_n < g.PlatformIO.Monitors.Size; monitor_n++) { ImGuiPlatformMonitor& mon = g.PlatformIO.Monitors[monitor_n]; IM_ASSERT(mon.MainSize.x > 0.0f && mon.MainSize.y > 0.0f && "Monitor bounds not setup properly."); 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."); IM_ASSERT(mon.DpiScale != 0.0f); } } // Load settings on first frame (if not explicitly loaded manually before) if (!g.SettingsLoaded) { IM_ASSERT(g.SettingsWindows.empty()); if (g.IO.IniFilename) LoadIniSettingsFromDisk(g.IO.IniFilename); g.SettingsLoaded = true; } // Save settings (with a delay after the last modification, so we don't spam disk too much) if (g.SettingsDirtyTimer > 0.0f) { g.SettingsDirtyTimer -= g.IO.DeltaTime; if (g.SettingsDirtyTimer <= 0.0f) { if (g.IO.IniFilename != NULL) SaveIniSettingsToDisk(g.IO.IniFilename); else g.IO.WantSaveIniSettings = true; // Let user know they can call SaveIniSettingsToMemory(). user will need to clear io.WantSaveIniSettings themselves. g.SettingsDirtyTimer = 0.0f; } } g.Time += g.IO.DeltaTime; g.FrameScopeActive = true; g.FrameCount += 1; g.TooltipOverrideCount = 0; g.WindowsActiveCount = 0; g.ConfigFlagsForFrame = g.IO.ConfigFlags; UpdateViewportsNewFrame(); // Setup current font and draw list shared data // FIXME-VIEWPORT: the concept of a single ClipRectFullscreen is not ideal! g.IO.Fonts->Locked = true; SetCurrentFont(GetDefaultFont()); IM_ASSERT(g.Font->IsLoaded()); ImRect virtual_space(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); for (int n = 0; n < g.Viewports.Size; n++) virtual_space.Add(g.Viewports[n]->GetRect()); g.DrawListSharedData.ClipRectFullscreen = ImVec4(virtual_space.Min.x, virtual_space.Min.y, virtual_space.Max.x, virtual_space.Max.y); g.DrawListSharedData.CurveTessellationTol = g.Style.CurveTessellationTol; // Mark rendering data as invalid to prevent user who may have a handle on it to use it. for (int n = 0; n < g.Viewports.Size; n++) { ImGuiViewportP* viewport = g.Viewports[n]; viewport->DrawData = NULL; viewport->DrawDataP.Clear(); } // Drag and drop keep the source ID alive so even if the source disappear our state is consistent if (g.DragDropActive && g.DragDropPayload.SourceId == g.ActiveId) KeepAliveID(g.DragDropPayload.SourceId); // Clear reference to active widget if the widget isn't alive anymore if (!g.HoveredIdPreviousFrame) g.HoveredIdTimer = 0.0f; if (!g.HoveredIdPreviousFrame || (g.HoveredId && g.ActiveId == g.HoveredId)) g.HoveredIdNotActiveTimer = 0.0f; if (g.HoveredId) g.HoveredIdTimer += g.IO.DeltaTime; if (g.HoveredId && g.ActiveId != g.HoveredId) g.HoveredIdNotActiveTimer += g.IO.DeltaTime; g.HoveredIdPreviousFrame = g.HoveredId; g.HoveredId = 0; g.HoveredIdAllowOverlap = false; if (g.ActiveIdIsAlive != g.ActiveId && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0) ClearActiveID(); if (g.ActiveId) g.ActiveIdTimer += g.IO.DeltaTime; g.LastActiveIdTimer += g.IO.DeltaTime; g.ActiveIdPreviousFrame = g.ActiveId; g.ActiveIdPreviousFrameWindow = g.ActiveIdWindow; g.ActiveIdPreviousFrameHasBeenEdited = g.ActiveIdHasBeenEdited; g.ActiveIdIsAlive = 0; g.ActiveIdPreviousFrameIsAlive = false; g.ActiveIdIsJustActivated = false; if (g.TempInputTextId != 0 && g.ActiveId != g.TempInputTextId) g.TempInputTextId = 0; // Drag and drop g.DragDropAcceptIdPrev = g.DragDropAcceptIdCurr; g.DragDropAcceptIdCurr = 0; g.DragDropAcceptIdCurrRectSurface = FLT_MAX; g.DragDropWithinSourceOrTarget = false; // Update keyboard input state memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration)); for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++) 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; // Update gamepad/keyboard directional navigation NavUpdate(); // Update mouse input state UpdateMouseInputs(); // Calculate frame-rate for the user, as a purely luxurious feature g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx]; g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx] = g.IO.DeltaTime; g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame); g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame))) : FLT_MAX; // Undocking // (needs to be before UpdateMouseMovingWindowNewFrame so the window is already offset and following the mouse on the detaching frame) DockContextUpdateUndocking(&g); // Find hovered window // (needs to be before UpdateMouseMovingWindowNewFrame so we fill g.HoveredWindowUnderMovingWindow on the mouse release frame) UpdateHoveredWindowAndCaptureFlags(); // Handle user moving window with mouse (at the beginning of the frame to avoid input lag or sheering) UpdateMouseMovingWindowNewFrame(); // Background darkening/whitening if (GetFrontMostPopupModal() != NULL || (g.NavWindowingTarget != NULL && g.NavWindowingHighlightAlpha > 0.0f)) g.DimBgRatio = ImMin(g.DimBgRatio + g.IO.DeltaTime * 6.0f, 1.0f); else g.DimBgRatio = ImMax(g.DimBgRatio - g.IO.DeltaTime * 10.0f, 0.0f); g.MouseCursor = ImGuiMouseCursor_Arrow; g.WantCaptureMouseNextFrame = g.WantCaptureKeyboardNextFrame = g.WantTextInputNextFrame = -1; g.PlatformImePos = ImVec2(1.0f, 1.0f); // OS Input Method Editor showing on top-left of our window by default g.PlatformImePosViewport = NULL; // Mouse wheel scrolling, scale UpdateMouseWheel(); // Pressing TAB activate widget focus g.FocusTabPressed = (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab)); if (g.ActiveId == 0 && g.FocusTabPressed) { // Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also // manipulate the Next fields even, even though they will be turned into Curr fields by the code below. g.FocusRequestNextWindow = g.NavWindow; g.FocusRequestNextCounterAll = INT_MAX; if (g.NavId != 0 && g.NavIdTabCounter != INT_MAX) g.FocusRequestNextCounterTab = g.NavIdTabCounter + 1 + (g.IO.KeyShift ? -1 : 1); else g.FocusRequestNextCounterTab = g.IO.KeyShift ? -1 : 0; } // Turn queued focus request into current one g.FocusRequestCurrWindow = NULL; g.FocusRequestCurrCounterAll = g.FocusRequestCurrCounterTab = INT_MAX; if (g.FocusRequestNextWindow != NULL) { ImGuiWindow* window = g.FocusRequestNextWindow; g.FocusRequestCurrWindow = window; if (g.FocusRequestNextCounterAll != INT_MAX && window->DC.FocusCounterAll != -1) g.FocusRequestCurrCounterAll = ImModPositive(g.FocusRequestNextCounterAll, window->DC.FocusCounterAll + 1); if (g.FocusRequestNextCounterTab != INT_MAX && window->DC.FocusCounterTab != -1) g.FocusRequestCurrCounterTab = ImModPositive(g.FocusRequestNextCounterTab, window->DC.FocusCounterTab + 1); g.FocusRequestNextWindow = NULL; g.FocusRequestNextCounterAll = g.FocusRequestNextCounterTab = INT_MAX; } g.NavIdTabCounter = INT_MAX; // Mark all windows as not visible IM_ASSERT(g.WindowsFocusOrder.Size == g.Windows.Size); for (int i = 0; i != g.Windows.Size; i++) { ImGuiWindow* window = g.Windows[i]; window->WasActive = window->Active; window->BeginCount = 0; window->Active = false; window->WriteAccessed = false; } // Closing the focused window restore focus to the first active root window in descending z-order if (g.NavWindow && !g.NavWindow->WasActive) FocusTopMostWindowUnderOne(NULL, NULL); // No window should be open at the beginning of the frame. // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear. g.CurrentWindowStack.resize(0); g.BeginPopupStack.resize(0); ClosePopupsOverWindow(g.NavWindow, false); // Docking DockContextUpdateDocking(&g); // Create implicit/fallback window - which we will only render it if the user has added something to it. // We don't use "Debug" to avoid colliding with user trying to create a "Debug" window with custom flags. // This fallback is particularly important as it avoid ImGui:: calls from crashing. SetNextWindowSize(ImVec2(400,400), ImGuiCond_FirstUseEver); Begin("Debug##Default"); g.FrameScopePushedImplicitWindow = true; #ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiTestEngineHook_PostNewFrame(&g); #endif } void ImGui::Initialize(ImGuiContext* context) { ImGuiContext& g = *context; IM_ASSERT(!g.Initialized && !g.SettingsLoaded); // Add .ini handle for ImGuiWindow type ImGuiSettingsHandler ini_handler; ini_handler.TypeName = "Window"; ini_handler.TypeHash = ImHashStr("Window"); ini_handler.ReadOpenFn = SettingsHandlerWindow_ReadOpen; ini_handler.ReadLineFn = SettingsHandlerWindow_ReadLine; ini_handler.WriteAllFn = SettingsHandlerWindow_WriteAll; g.SettingsHandlers.push_back(ini_handler); // Create default viewport ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); viewport->ID = IMGUI_VIEWPORT_DEFAULT_ID; viewport->Idx = 0; viewport->PlatformWindowCreated = true; g.Viewports.push_back(viewport); g.PlatformIO.MainViewport = g.Viewports[0]; // Make it accessible in public-facing GetPlatformIO() immediately (before the first call to EndFrame) g.PlatformIO.Viewports.push_back(g.Viewports[0]); // Extensions IM_ASSERT(g.DockContext == NULL); DockContextInitialize(&g); g.Initialized = true; } // This function is merely here to free heap allocations. void ImGui::Shutdown(ImGuiContext* context) { // 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) ImGuiContext& g = *context; if (g.IO.Fonts && g.FontAtlasOwnedByContext) { g.IO.Fonts->Locked = false; IM_DELETE(g.IO.Fonts); } g.IO.Fonts = NULL; // Cleanup of other data are conditional on actually having initialized ImGui. if (!g.Initialized) return; // Save settings (unless we haven't attempted to load them: CreateContext/DestroyContext without a call to NewFrame shouldn't save an empty file) if (g.SettingsLoaded && g.IO.IniFilename != NULL) { ImGuiContext* backup_context = GImGui; SetCurrentContext(context); SaveIniSettingsToDisk(g.IO.IniFilename); SetCurrentContext(backup_context); } // Destroy platform windows ImGuiContext* backup_context = ImGui::GetCurrentContext(); SetCurrentContext(context); DestroyPlatformWindows(); SetCurrentContext(backup_context); // Shutdown extensions IM_ASSERT(g.DockContext != NULL); DockContextShutdown(&g); // Clear everything else for (int i = 0; i < g.Windows.Size; i++) IM_DELETE(g.Windows[i]); g.Windows.clear(); g.WindowsFocusOrder.clear(); g.WindowsSortBuffer.clear(); g.CurrentWindow = NULL; g.CurrentWindowStack.clear(); g.WindowsById.Clear(); g.NavWindow = NULL; g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL; g.ActiveIdWindow = g.ActiveIdPreviousFrameWindow = NULL; g.MovingWindow = NULL; g.ColorModifiers.clear(); g.StyleModifiers.clear(); g.FontStack.clear(); g.OpenPopupStack.clear(); g.BeginPopupStack.clear(); g.CurrentViewport = g.MouseViewport = g.MouseLastHoveredViewport = NULL; for (int i = 0; i < g.Viewports.Size; i++) IM_DELETE(g.Viewports[i]); g.Viewports.clear(); g.PrivateClipboard.clear(); g.InputTextState.ClearFreeMemory(); for (int i = 0; i < g.SettingsWindows.Size; i++) IM_DELETE(g.SettingsWindows[i].Name); g.SettingsWindows.clear(); g.SettingsHandlers.clear(); if (g.LogFile && g.LogFile != stdout) { fclose(g.LogFile); g.LogFile = NULL; } g.LogBuffer.clear(); g.Initialized = false; } // FIXME: Add a more explicit sort order in the window structure. static int IMGUI_CDECL ChildWindowComparer(const void* lhs, const void* rhs) { const ImGuiWindow* const a = *(const ImGuiWindow* const *)lhs; const ImGuiWindow* const b = *(const ImGuiWindow* const *)rhs; if (int d = (a->Flags & ImGuiWindowFlags_Popup) - (b->Flags & ImGuiWindowFlags_Popup)) return d; if (int d = (a->Flags & ImGuiWindowFlags_Tooltip) - (b->Flags & ImGuiWindowFlags_Tooltip)) return d; return (a->BeginOrderWithinParent - b->BeginOrderWithinParent); } static void AddWindowToSortBuffer(ImVector* out_sorted_windows, ImGuiWindow* window) { out_sorted_windows->push_back(window); if (window->Active) { int count = window->DC.ChildWindows.Size; if (count > 1) ImQsort(window->DC.ChildWindows.begin(), (size_t)count, sizeof(ImGuiWindow*), ChildWindowComparer); for (int i = 0; i < count; i++) { ImGuiWindow* child = window->DC.ChildWindows[i]; if (child->Active) AddWindowToSortBuffer(out_sorted_windows, child); } } } static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list) { if (draw_list->CmdBuffer.empty()) return; // Remove trailing command if unused ImDrawCmd& last_cmd = draw_list->CmdBuffer.back(); if (last_cmd.ElemCount == 0 && last_cmd.UserCallback == NULL) { draw_list->CmdBuffer.pop_back(); if (draw_list->CmdBuffer.empty()) return; } // 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. IM_ASSERT(draw_list->VtxBuffer.Size == 0 || draw_list->_VtxWritePtr == draw_list->VtxBuffer.Data + draw_list->VtxBuffer.Size); IM_ASSERT(draw_list->IdxBuffer.Size == 0 || draw_list->_IdxWritePtr == draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size); IM_ASSERT((int)draw_list->_VtxCurrentIdx == draw_list->VtxBuffer.Size); // Check that draw_list doesn't use more vertices than indexable (default ImDrawIdx = unsigned short = 2 bytes = 64K vertices per ImDrawList = per window) // If this assert triggers because you are drawing lots of stuff manually: // 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. // 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. // 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: // glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset); // 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. // 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. if (sizeof(ImDrawIdx) == 2) IM_ASSERT(draw_list->_VtxCurrentIdx < (1 << 16) && "Too many vertices in ImDrawList using 16-bit indices. Read comment above"); out_list->push_back(draw_list); } static void AddWindowToDrawData(ImGuiWindow* window, int layer) { ImGuiContext& g = *GImGui; g.IO.MetricsRenderWindows++; AddDrawListToDrawData(&window->Viewport->DrawDataBuilder.Layers[layer], window->DrawList); for (int i = 0; i < window->DC.ChildWindows.Size; i++) { ImGuiWindow* child = window->DC.ChildWindows[i]; if (IsWindowActiveAndVisible(child)) // Clipped children may have been marked not active AddWindowToDrawData(child, layer); } } // Layer is locked for the root window, however child windows may use a different viewport (e.g. extruding menu) static void AddRootWindowToDrawData(ImGuiWindow* window) { int layer = (window->Flags & ImGuiWindowFlags_Tooltip) ? 1 : 0; AddWindowToDrawData(window, layer); } void ImDrawDataBuilder::FlattenIntoSingleLayer() { int n = Layers[0].Size; int size = n; for (int i = 1; i < IM_ARRAYSIZE(Layers); i++) size += Layers[i].Size; Layers[0].resize(size); for (int layer_n = 1; layer_n < IM_ARRAYSIZE(Layers); layer_n++) { ImVector& layer = Layers[layer_n]; if (layer.empty()) continue; memcpy(&Layers[0][n], &layer[0], layer.Size * sizeof(ImDrawList*)); n += layer.Size; layer.resize(0); } } static void SetupViewportDrawData(ImGuiViewportP* viewport, ImVector* draw_lists) { ImDrawData* draw_data = &viewport->DrawDataP; viewport->DrawData = draw_data; // Make publicly accessible draw_data->Valid = true; draw_data->CmdLists = (draw_lists->Size > 0) ? draw_lists->Data : NULL; draw_data->CmdListsCount = draw_lists->Size; draw_data->TotalVtxCount = draw_data->TotalIdxCount = 0; draw_data->DisplayPos = viewport->Pos; draw_data->DisplaySize = viewport->Size; draw_data->FramebufferScale = ImGui::GetIO().DisplayFramebufferScale; // FIXME-VIEWPORT: This may vary on a per-monitor/viewport basis? draw_data->OwnerViewport = viewport; for (int n = 0; n < draw_lists->Size; n++) { draw_data->TotalVtxCount += draw_lists->Data[n]->VtxBuffer.Size; draw_data->TotalIdxCount += draw_lists->Data[n]->IdxBuffer.Size; } } // 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. void ImGui::PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect) { ImGuiWindow* window = GetCurrentWindow(); window->DrawList->PushClipRect(clip_rect_min, clip_rect_max, intersect_with_current_clip_rect); window->ClipRect = window->DrawList->_ClipRectStack.back(); } void ImGui::PopClipRect() { ImGuiWindow* window = GetCurrentWindow(); window->DrawList->PopClipRect(); window->ClipRect = window->DrawList->_ClipRectStack.back(); } static ImGuiWindow* FindFrontMostVisibleChildWindow(ImGuiWindow* window) { for (int n = window->DC.ChildWindows.Size - 1; n >= 0; n--) if (IsWindowActiveAndVisible(window->DC.ChildWindows[n])) return FindFrontMostVisibleChildWindow(window->DC.ChildWindows[n]); return window; } static void ImGui::EndFrameDrawDimmedBackgrounds() { ImGuiContext& g = *GImGui; // Draw modal whitening background on _other_ viewports than the one the modal is one ImGuiWindow* modal_window = GetFrontMostPopupModal(); const bool dim_bg_for_modal = (modal_window != NULL); const bool dim_bg_for_window_list = (g.NavWindowingTargetAnim != NULL); if (dim_bg_for_modal || dim_bg_for_window_list) for (int viewport_n = 0; viewport_n < g.Viewports.Size; viewport_n++) { ImGuiViewportP* viewport = g.Viewports[viewport_n]; if (modal_window && viewport == modal_window->Viewport) continue; if (g.NavWindowingList && viewport == g.NavWindowingList->Viewport) continue; if (g.NavWindowingTargetAnim && viewport == g.NavWindowingTargetAnim->Viewport) continue; ImDrawList* draw_list = GetForegroundDrawList(viewport); const ImU32 dim_bg_col = GetColorU32(dim_bg_for_modal ? ImGuiCol_ModalWindowDimBg : ImGuiCol_NavWindowingDimBg, g.DimBgRatio); draw_list->AddRectFilled(viewport->Pos, viewport->Pos + viewport->Size, dim_bg_col); } // Draw modal whitening background between CTRL-TAB list if (dim_bg_for_window_list) { // Choose a draw list that will be front-most across all our children ImGuiWindow* window = g.NavWindowingTargetAnim; ImDrawList* draw_list = FindFrontMostVisibleChildWindow(window->RootWindow)->DrawList; draw_list->PushClipRectFullScreen(); // Docking: draw modal whitening background on other nodes of a same dock tree if (window->RootWindowDockStop->DockIsActive) if (window->RootWindow != window->RootWindowDockStop) RenderRectFilledWithHole(draw_list, window->RootWindow->Rect(), window->RootWindowDockStop->Rect(), GetColorU32(ImGuiCol_NavWindowingDimBg, g.DimBgRatio), g.Style.WindowRounding); // Draw navigation selection/windowing rectangle border float rounding = ImMax(window->WindowRounding, g.Style.WindowRounding); ImRect bb = window->Rect(); bb.Expand(g.FontSize); if (bb.Contains(window->Viewport->GetRect())) // If a window fits the entire viewport, adjust its highlight inward { bb.Expand(-g.FontSize - 1.0f); rounding = window->WindowRounding; } draw_list->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha), rounding, ~0, 3.0f); draw_list->PopClipRect(); } } // 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. void ImGui::EndFrame() { ImGuiContext& g = *GImGui; IM_ASSERT(g.Initialized); if (g.FrameCountEnded == g.FrameCount) // Don't process EndFrame() multiple times. return; IM_ASSERT(g.FrameScopeActive && "Forgot to call ImGui::NewFrame()?"); // Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) if (g.PlatformIO.Platform_SetImeInputPos && (g.PlatformImeLastPos.x == FLT_MAX || ImLengthSqr(g.PlatformImePos - g.PlatformImeLastPos) > 0.0001f)) if (g.PlatformImePosViewport && g.PlatformImePosViewport->PlatformWindowCreated) { g.PlatformIO.Platform_SetImeInputPos(g.PlatformImePosViewport, g.PlatformImePos); g.PlatformImeLastPos = g.PlatformImePos; g.PlatformImePosViewport = NULL; } // Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you // to always call End/EndChild even if Begin/BeginChild returns false! (this is unfortunately inconsistent with most other Begin* API). if (g.CurrentWindowStack.Size != 1) { if (g.CurrentWindowStack.Size > 1) { IM_ASSERT(g.CurrentWindowStack.Size == 1 && "Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?"); while (g.CurrentWindowStack.Size > 1) // FIXME-ERRORHANDLING End(); } else { IM_ASSERT(g.CurrentWindowStack.Size == 1 && "Mismatched Begin/BeginChild vs End/EndChild calls: did you call End/EndChild too much?"); } } // Hide implicit/fallback "Debug" window if it hasn't been used g.FrameScopePushedImplicitWindow = false; if (g.CurrentWindow && !g.CurrentWindow->WriteAccessed) g.CurrentWindow->Active = false; End(); // Draw modal whitening background on _other_ viewports than the one the modal is one EndFrameDrawDimmedBackgrounds(); // Show CTRL+TAB list window if (g.NavWindowingTarget) NavUpdateWindowingList(); SetCurrentViewport(NULL, NULL); // Drag and Drop: Elapse payload (if delivered, or if source stops being submitted) if (g.DragDropActive) { bool is_delivered = g.DragDropPayload.Delivery; bool is_elapsed = (g.DragDropPayload.DataFrameCount + 1 < g.FrameCount) && ((g.DragDropSourceFlags & ImGuiDragDropFlags_SourceAutoExpirePayload) || !IsMouseDown(g.DragDropMouseButton)); if (is_delivered || is_elapsed) ClearDragDrop(); } // Drag and Drop: Fallback for source tooltip. This is not ideal but better than nothing. if (g.DragDropActive && g.DragDropSourceFrameCount < g.FrameCount) { g.DragDropWithinSourceOrTarget = true; SetTooltip("..."); g.DragDropWithinSourceOrTarget = false; } // End frame g.FrameScopeActive = false; g.FrameCountEnded = g.FrameCount; // Initiate moving window + handle left-click and right-click focus UpdateMouseMovingWindowEndFrame(); // Update user-facing viewport list (g.Viewports -> g.PlatformIO.Viewports after filtering out some) UpdateViewportsEndFrame(); // Sort the window list so that all child windows are after their parent // We cannot do that on FocusWindow() because childs may not exist yet g.WindowsSortBuffer.resize(0); g.WindowsSortBuffer.reserve(g.Windows.Size); for (int i = 0; i != g.Windows.Size; i++) { ImGuiWindow* window = g.Windows[i]; if (window->Active && (window->Flags & ImGuiWindowFlags_ChildWindow)) // if a child is active its parent will add it continue; AddWindowToSortBuffer(&g.WindowsSortBuffer, window); } // 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. IM_ASSERT(g.Windows.Size == g.WindowsSortBuffer.Size); g.Windows.swap(g.WindowsSortBuffer); g.IO.MetricsActiveWindows = g.WindowsActiveCount; // Unlock font atlas g.IO.Fonts->Locked = false; // Clear Input data for next frame g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f; g.IO.InputQueueCharacters.resize(0); memset(g.IO.NavInputs, 0, sizeof(g.IO.NavInputs)); } void ImGui::Render() { ImGuiContext& g = *GImGui; IM_ASSERT(g.Initialized); if (g.FrameCountEnded != g.FrameCount) EndFrame(); g.FrameCountRendered = g.FrameCount; // Gather ImDrawList to render (for each active window) g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = g.IO.MetricsRenderWindows = 0; for (int n = 0; n != g.Viewports.Size; n++) { ImGuiViewportP* viewport = g.Viewports[n]; viewport->DrawDataBuilder.Clear(); if (viewport->DrawLists[0] != NULL) AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetBackgroundDrawList(viewport)); } ImGuiWindow* windows_to_render_front_most[2]; windows_to_render_front_most[0] = (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) ? g.NavWindowingTarget->RootWindow : NULL; windows_to_render_front_most[1] = g.NavWindowingTarget ? g.NavWindowingList : NULL; for (int n = 0; n != g.Windows.Size; n++) { ImGuiWindow* window = g.Windows[n]; if (IsWindowActiveAndVisible(window) && (window->Flags & ImGuiWindowFlags_ChildWindow) == 0 && window != windows_to_render_front_most[0] && window != windows_to_render_front_most[1]) AddRootWindowToDrawData(window); } for (int n = 0; n < IM_ARRAYSIZE(windows_to_render_front_most); 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 AddRootWindowToDrawData(windows_to_render_front_most[n]); // Draw software mouse cursor if requested if (g.IO.MouseDrawCursor) RenderMouseCursor(g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor); // Setup ImDrawData structures for end-user g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = 0; for (int n = 0; n < g.Viewports.Size; n++) { ImGuiViewportP* viewport = g.Viewports[n]; viewport->DrawDataBuilder.FlattenIntoSingleLayer(); if (viewport->DrawLists[1] != NULL) AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetForegroundDrawList(viewport)); SetupViewportDrawData(viewport, &viewport->DrawDataBuilder.Layers[0]); g.IO.MetricsRenderVertices += viewport->DrawData->TotalVtxCount; g.IO.MetricsRenderIndices += viewport->DrawData->TotalIdxCount; } // (Legacy) Call the Render callback function. The current prefer way is to let the user retrieve GetDrawData() and call the render function themselves. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS if (g.Viewports[0]->DrawData->CmdListsCount > 0 && g.IO.RenderDrawListsFn != NULL) g.IO.RenderDrawListsFn(g.Viewports[0]->DrawData); #endif } // Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker. // CalcTextSize("") should return ImVec2(0.0f, GImGui->FontSize) ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_text_after_double_hash, float wrap_width) { ImGuiContext& g = *GImGui; const char* text_display_end; if (hide_text_after_double_hash) text_display_end = FindRenderedTextEnd(text, text_end); // Hide anything after a '##' string else text_display_end = text_end; ImFont* font = g.Font; const float font_size = g.FontSize; if (text == text_display_end) return ImVec2(0.0f, font_size); ImVec2 text_size = font->CalcTextSizeA(font_size, FLT_MAX, wrap_width, text, text_display_end, NULL); // Round text_size.x = (float)(int)(text_size.x + 0.95f); return text_size; } // Helper to calculate coarse clipping of large list of evenly sized items. // NB: Prefer using the ImGuiListClipper higher-level helper if you can! Read comments and instructions there on how those use this sort of pattern. // NB: 'items_count' is only used to clamp the result, if you don't know your count you can use INT_MAX void ImGui::CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (g.LogEnabled) { // If logging is active, do not perform any clipping *out_items_display_start = 0; *out_items_display_end = items_count; return; } if (window->SkipItems) { *out_items_display_start = *out_items_display_end = 0; return; } // We create the union of the ClipRect and the NavScoringRect which at worst should be 1 page away from ClipRect ImRect unclipped_rect = window->ClipRect; if (g.NavMoveRequest) unclipped_rect.Add(g.NavScoringRectScreen); const ImVec2 pos = window->DC.CursorPos; int start = (int)((unclipped_rect.Min.y - pos.y) / items_height); int end = (int)((unclipped_rect.Max.y - pos.y) / items_height); // When performing a navigation request, ensure we have one item extra in the direction we are moving to if (g.NavMoveRequest && g.NavMoveClipDir == ImGuiDir_Up) start--; if (g.NavMoveRequest && g.NavMoveClipDir == ImGuiDir_Down) end++; start = ImClamp(start, 0, items_count); end = ImClamp(end + 1, start, items_count); *out_items_display_start = start; *out_items_display_end = end; } // Find window given position, search front-to-back // FIXME: Note that we have an inconsequential lag here: OuterRectClipped is updated in Begin(), so windows moved programatically // with SetWindowPos() and not SetNextWindowPos() will have that rectangle lagging by a frame at the time FindHoveredWindow() is // called, aka before the next Begin(). Moving window isn't affected. static void FindHoveredWindow() { ImGuiContext& g = *GImGui; // Special handling for the window being moved: Ignore the mouse viewport check (because it may reset/lose its viewport during the undocking frame) ImGuiViewportP* moving_window_viewport = g.MovingWindow ? g.MovingWindow->Viewport : NULL; if (g.MovingWindow) g.MovingWindow->Viewport = g.MouseViewport; ImGuiWindow* hovered_window = NULL; ImGuiWindow* hovered_window_ignoring_moving_window = NULL; if (g.MovingWindow && !(g.MovingWindow->Flags & ImGuiWindowFlags_NoMouseInputs)) hovered_window = g.MovingWindow; ImVec2 padding_regular = g.Style.TouchExtraPadding; 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; for (int i = g.Windows.Size - 1; i >= 0; i--) { ImGuiWindow* window = g.Windows[i]; if (!window->Active || window->Hidden) continue; if (window->Flags & ImGuiWindowFlags_NoMouseInputs) continue; IM_ASSERT(window->Viewport); if (window->Viewport != g.MouseViewport) continue; // Using the clipped AABB, a child window will typically be clipped by its parent (not always) ImRect bb(window->OuterRectClipped); if (window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize)) bb.Expand(padding_regular); else bb.Expand(padding_for_resize_from_edges); if (!bb.Contains(g.IO.MousePos)) continue; if (window->HitTestHoleSize.x != 0) { // FIXME: Consider generalizing hit-testing override (with more generic data, callback, etc.) (#1512) ImRect hole_bb((float)(window->HitTestHoleOffset.x), (float)(window->HitTestHoleOffset.y), (float)(window->HitTestHoleOffset.x + window->HitTestHoleSize.x), (float)(window->HitTestHoleOffset.y + window->HitTestHoleSize.y)); if (hole_bb.Contains(g.IO.MousePos - window->Pos)) continue; } if (hovered_window == NULL) hovered_window = window; if (hovered_window_ignoring_moving_window == NULL && (!g.MovingWindow || window->RootWindow != g.MovingWindow->RootWindow)) hovered_window_ignoring_moving_window = window; if (hovered_window && hovered_window_ignoring_moving_window) break; } g.HoveredWindow = hovered_window; g.HoveredRootWindow = g.HoveredWindow ? g.HoveredWindow->RootWindow : NULL; g.HoveredWindowUnderMovingWindow = hovered_window_ignoring_moving_window; if (g.MovingWindow) g.MovingWindow->Viewport = moving_window_viewport; } // Test if mouse cursor is hovering given rectangle // NB- Rectangle is clipped by our current clip setting // NB- Expand the rectangle to be generous on imprecise inputs systems (g.Style.TouchExtraPadding) bool ImGui::IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip) { ImGuiContext& g = *GImGui; // Clip ImRect rect_clipped(r_min, r_max); if (clip) rect_clipped.ClipWith(g.CurrentWindow->ClipRect); // Expand for touch input const ImRect rect_for_touch(rect_clipped.Min - g.Style.TouchExtraPadding, rect_clipped.Max + g.Style.TouchExtraPadding); if (!rect_for_touch.Contains(g.IO.MousePos)) return false; if (!g.MouseViewport->GetRect().Overlaps(rect_clipped)) return false; return true; } int ImGui::GetKeyIndex(ImGuiKey imgui_key) { IM_ASSERT(imgui_key >= 0 && imgui_key < ImGuiKey_COUNT); return GImGui->IO.KeyMap[imgui_key]; } // 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[]! bool ImGui::IsKeyDown(int user_key_index) { if (user_key_index < 0) return false; IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(GImGui->IO.KeysDown)); return GImGui->IO.KeysDown[user_key_index]; } int ImGui::CalcTypematicPressedRepeatAmount(float t, float t_prev, float repeat_delay, float repeat_rate) { if (t == 0.0f) return 1; if (t <= repeat_delay || repeat_rate <= 0.0f) return 0; const int count = (int)((t - repeat_delay) / repeat_rate) - (int)((t_prev - repeat_delay) / repeat_rate); return (count > 0) ? count : 0; } int ImGui::GetKeyPressedAmount(int key_index, float repeat_delay, float repeat_rate) { ImGuiContext& g = *GImGui; if (key_index < 0) return 0; IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown)); const float t = g.IO.KeysDownDuration[key_index]; return CalcTypematicPressedRepeatAmount(t, t - g.IO.DeltaTime, repeat_delay, repeat_rate); } bool ImGui::IsKeyPressed(int user_key_index, bool repeat) { ImGuiContext& g = *GImGui; if (user_key_index < 0) return false; IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown)); const float t = g.IO.KeysDownDuration[user_key_index]; if (t == 0.0f) return true; if (repeat && t > g.IO.KeyRepeatDelay) return GetKeyPressedAmount(user_key_index, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0; return false; } bool ImGui::IsKeyReleased(int user_key_index) { ImGuiContext& g = *GImGui; if (user_key_index < 0) return false; IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown)); return g.IO.KeysDownDurationPrev[user_key_index] >= 0.0f && !g.IO.KeysDown[user_key_index]; } bool ImGui::IsMouseDown(int button) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); return g.IO.MouseDown[button]; } bool ImGui::IsAnyMouseDown() { ImGuiContext& g = *GImGui; for (int n = 0; n < IM_ARRAYSIZE(g.IO.MouseDown); n++) if (g.IO.MouseDown[n]) return true; return false; } bool ImGui::IsMouseClicked(int button, bool repeat) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); const float t = g.IO.MouseDownDuration[button]; if (t == 0.0f) return true; if (repeat && t > g.IO.KeyRepeatDelay) { float delay = g.IO.KeyRepeatDelay, rate = g.IO.KeyRepeatRate; if ((ImFmod(t - delay, rate) > rate*0.5f) != (ImFmod(t - delay - g.IO.DeltaTime, rate) > rate*0.5f)) return true; } return false; } bool ImGui::IsMouseReleased(int button) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); return g.IO.MouseReleased[button]; } bool ImGui::IsMouseDoubleClicked(int button) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); return g.IO.MouseDoubleClicked[button]; } bool ImGui::IsMouseDragging(int button, float lock_threshold) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); if (!g.IO.MouseDown[button]) return false; if (lock_threshold < 0.0f) lock_threshold = g.IO.MouseDragThreshold; return g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold; } ImVec2 ImGui::GetMousePos() { return GImGui->IO.MousePos; } // NB: prefer to call right after BeginPopup(). At the time Selectable/MenuItem is activated, the popup is already closed! ImVec2 ImGui::GetMousePosOnOpeningCurrentPopup() { ImGuiContext& g = *GImGui; if (g.BeginPopupStack.Size > 0) return g.OpenPopupStack[g.BeginPopupStack.Size-1].OpenMousePos; return g.IO.MousePos; } // We typically use ImVec2(-FLT_MAX,-FLT_MAX) to denote an invalid mouse position. bool ImGui::IsMousePosValid(const ImVec2* mouse_pos) { // The assert is only to silence a false-positive in XCode Static Analysis. // 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). IM_ASSERT(GImGui != NULL); const float MOUSE_INVALID = -256000.0f; ImVec2 p = mouse_pos ? *mouse_pos : GImGui->IO.MousePos; return p.x >= MOUSE_INVALID && p.y >= MOUSE_INVALID; } // Return the delta from the initial clicking position while the mouse button is clicked or was just released. // This is locked and return 0.0f until the mouse moves past a distance threshold at least once. // NB: This is only valid if IsMousePosValid(). Back-ends in theory should always keep mouse position valid when dragging even outside the client window. ImVec2 ImGui::GetMouseDragDelta(int button, float lock_threshold) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); if (lock_threshold < 0.0f) lock_threshold = g.IO.MouseDragThreshold; if (g.IO.MouseDown[button] || g.IO.MouseReleased[button]) if (g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold) if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MouseClickedPos[button])) return g.IO.MousePos - g.IO.MouseClickedPos[button]; return ImVec2(0.0f, 0.0f); } void ImGui::ResetMouseDragDelta(int button) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); // NB: We don't need to reset g.IO.MouseDragMaxDistanceSqr g.IO.MouseClickedPos[button] = g.IO.MousePos; } ImGuiMouseCursor ImGui::GetMouseCursor() { return GImGui->MouseCursor; } void ImGui::SetMouseCursor(ImGuiMouseCursor cursor_type) { GImGui->MouseCursor = cursor_type; } void ImGui::CaptureKeyboardFromApp(bool capture) { GImGui->WantCaptureKeyboardNextFrame = capture ? 1 : 0; } void ImGui::CaptureMouseFromApp(bool capture) { GImGui->WantCaptureMouseNextFrame = capture ? 1 : 0; } bool ImGui::IsItemActive() { ImGuiContext& g = *GImGui; if (g.ActiveId) { ImGuiWindow* window = g.CurrentWindow; return g.ActiveId == window->DC.LastItemId; } return false; } bool ImGui::IsItemActivated() { ImGuiContext& g = *GImGui; if (g.ActiveId) { ImGuiWindow* window = g.CurrentWindow; if (g.ActiveId == window->DC.LastItemId && g.ActiveIdPreviousFrame != window->DC.LastItemId) return true; } return false; } bool ImGui::IsItemDeactivated() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; return (g.ActiveIdPreviousFrame == window->DC.LastItemId && g.ActiveIdPreviousFrame != 0 && g.ActiveId != window->DC.LastItemId); } bool ImGui::IsItemDeactivatedAfterEdit() { ImGuiContext& g = *GImGui; return IsItemDeactivated() && (g.ActiveIdPreviousFrameHasBeenEdited || (g.ActiveId == 0 && g.ActiveIdHasBeenEdited)); } bool ImGui::IsItemFocused() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (g.NavId == 0 || g.NavDisableHighlight || g.NavId != window->DC.LastItemId) return false; // Special handling for the dummy item after Begin() which represent the title bar or tab. // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. if (window->DC.LastItemId == window->ID && window->WriteAccessed) return false; return true; } bool ImGui::IsItemClicked(int mouse_button) { return IsMouseClicked(mouse_button) && IsItemHovered(ImGuiHoveredFlags_None); } bool ImGui::IsItemToggledSelection() { ImGuiContext& g = *GImGui; return (g.CurrentWindow->DC.LastItemStatusFlags & ImGuiItemStatusFlags_ToggledSelection) ? true : false; } bool ImGui::IsAnyItemHovered() { ImGuiContext& g = *GImGui; return g.HoveredId != 0 || g.HoveredIdPreviousFrame != 0; } bool ImGui::IsAnyItemActive() { ImGuiContext& g = *GImGui; return g.ActiveId != 0; } bool ImGui::IsAnyItemFocused() { ImGuiContext& g = *GImGui; return g.NavId != 0 && !g.NavDisableHighlight; } bool ImGui::IsItemVisible() { ImGuiWindow* window = GetCurrentWindowRead(); return window->ClipRect.Overlaps(window->DC.LastItemRect); } bool ImGui::IsItemEdited() { ImGuiWindow* window = GetCurrentWindowRead(); return (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_Edited) != 0; } // Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority. void ImGui::SetItemAllowOverlap() { ImGuiContext& g = *GImGui; if (g.HoveredId == g.CurrentWindow->DC.LastItemId) g.HoveredIdAllowOverlap = true; if (g.ActiveId == g.CurrentWindow->DC.LastItemId) g.ActiveIdAllowOverlap = true; } ImVec2 ImGui::GetItemRectMin() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.LastItemRect.Min; } ImVec2 ImGui::GetItemRectMax() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.LastItemRect.Max; } ImVec2 ImGui::GetItemRectSize() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.LastItemRect.GetSize(); } static bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* parent_window = g.CurrentWindow; flags |= ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_ChildWindow|ImGuiWindowFlags_NoDocking; flags |= (parent_window->Flags & ImGuiWindowFlags_NoMove); // Inherit the NoMove flag // Size const ImVec2 content_avail = GetContentRegionAvail(); ImVec2 size = ImFloor(size_arg); const int auto_fit_axises = ((size.x == 0.0f) ? (1 << ImGuiAxis_X) : 0x00) | ((size.y == 0.0f) ? (1 << ImGuiAxis_Y) : 0x00); if (size.x <= 0.0f) size.x = ImMax(content_avail.x + size.x, 4.0f); // Arbitrary minimum child size (0.0f causing too much issues) if (size.y <= 0.0f) size.y = ImMax(content_avail.y + size.y, 4.0f); SetNextWindowSize(size); // 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. char title[256]; if (name) ImFormatString(title, IM_ARRAYSIZE(title), "%s/%s_%08X", parent_window->Name, name, id); else ImFormatString(title, IM_ARRAYSIZE(title), "%s/%08X", parent_window->Name, id); const float backup_border_size = g.Style.ChildBorderSize; if (!border) g.Style.ChildBorderSize = 0.0f; bool ret = Begin(title, NULL, flags); g.Style.ChildBorderSize = backup_border_size; ImGuiWindow* child_window = g.CurrentWindow; child_window->ChildId = id; child_window->AutoFitChildAxises = auto_fit_axises; // Set the cursor to handle case where the user called SetNextWindowPos()+BeginChild() manually. // While this is not really documented/defined, it seems that the expected thing to do. if (child_window->BeginCount == 1) parent_window->DC.CursorPos = child_window->Pos; // Process navigation-in immediately so NavInit can run on first frame if (g.NavActivateId == id && !(flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayerActiveMask != 0 || child_window->DC.NavHasScroll)) { FocusWindow(child_window); NavInitWindow(child_window, false); SetActiveID(id+1, child_window); // Steal ActiveId with a dummy id so that key-press won't activate child item g.ActiveIdSource = ImGuiInputSource_Nav; } return ret; } bool ImGui::BeginChild(const char* str_id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags) { ImGuiWindow* window = GetCurrentWindow(); return BeginChildEx(str_id, window->GetID(str_id), size_arg, border, extra_flags); } bool ImGui::BeginChild(ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags) { IM_ASSERT(id != 0); return BeginChildEx(NULL, id, size_arg, border, extra_flags); } void ImGui::EndChild() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; IM_ASSERT(window->Flags & ImGuiWindowFlags_ChildWindow); // Mismatched BeginChild()/EndChild() callss if (window->BeginCount > 1) { End(); } else { ImVec2 sz = window->Size; if (window->AutoFitChildAxises & (1 << ImGuiAxis_X)) // Arbitrary minimum zero-ish child size of 4.0f causes less trouble than a 0.0f sz.x = ImMax(4.0f, sz.x); if (window->AutoFitChildAxises & (1 << ImGuiAxis_Y)) sz.y = ImMax(4.0f, sz.y); End(); ImGuiWindow* parent_window = g.CurrentWindow; ImRect bb(parent_window->DC.CursorPos, parent_window->DC.CursorPos + sz); ItemSize(sz); if ((window->DC.NavLayerActiveMask != 0 || window->DC.NavHasScroll) && !(window->Flags & ImGuiWindowFlags_NavFlattened)) { ItemAdd(bb, window->ChildId); RenderNavHighlight(bb, window->ChildId); // When browsing a window that has no activable items (scroll only) we keep a highlight on the child if (window->DC.NavLayerActiveMask == 0 && window == g.NavWindow) RenderNavHighlight(ImRect(bb.Min - ImVec2(2,2), bb.Max + ImVec2(2,2)), g.NavId, ImGuiNavHighlightFlags_TypeThin); } else { // Not navigable into ItemAdd(bb, 0); } } } // Helper to create a child window / scrolling region that looks like a normal widget frame. bool ImGui::BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags extra_flags) { ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]); PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); PushStyleVar(ImGuiStyleVar_WindowPadding, style.FramePadding); bool ret = BeginChild(id, size, true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysUseWindowPadding | extra_flags); PopStyleVar(3); PopStyleColor(); return ret; } void ImGui::EndChildFrame() { EndChild(); } // Save and compare stack sizes on Begin()/End() to detect usage errors static void CheckStacksSize(ImGuiWindow* window, bool write) { // 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) ImGuiContext& g = *GImGui; short* p_backup = &window->DC.StackSizesBackup[0]; { 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() { 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() { 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() // 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. { 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() { 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() { 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() IM_ASSERT(p_backup == window->DC.StackSizesBackup + IM_ARRAYSIZE(window->DC.StackSizesBackup)); } static void SetWindowConditionAllowFlags(ImGuiWindow* window, ImGuiCond flags, bool enabled) { window->SetWindowPosAllowFlags = enabled ? (window->SetWindowPosAllowFlags | flags) : (window->SetWindowPosAllowFlags & ~flags); window->SetWindowSizeAllowFlags = enabled ? (window->SetWindowSizeAllowFlags | flags) : (window->SetWindowSizeAllowFlags & ~flags); window->SetWindowCollapsedAllowFlags = enabled ? (window->SetWindowCollapsedAllowFlags | flags) : (window->SetWindowCollapsedAllowFlags & ~flags); window->SetWindowDockAllowFlags = enabled ? (window->SetWindowDockAllowFlags | flags) : (window->SetWindowDockAllowFlags & ~flags); } ImGuiWindow* ImGui::FindWindowByID(ImGuiID id) { ImGuiContext& g = *GImGui; return (ImGuiWindow*)g.WindowsById.GetVoidPtr(id); } ImGuiWindow* ImGui::FindWindowByName(const char* name) { ImGuiID id = ImHashStr(name); return FindWindowByID(id); } static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; // Create window the first time ImGuiWindow* window = IM_NEW(ImGuiWindow)(&g, name); window->Flags = flags; g.WindowsById.SetVoidPtr(window->ID, window); // Default/arbitrary window position. Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window. ImGuiViewport* main_viewport = ImGui::GetMainViewport(); window->Pos = main_viewport->Pos + ImVec2(60, 60); // User can disable loading and saving of settings. Tooltip and child windows also don't store settings. if (!(flags & ImGuiWindowFlags_NoSavedSettings)) if (ImGuiWindowSettings* settings = ImGui::FindWindowSettings(window->ID)) { // Retrieve settings from .ini file window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings); SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false); if (settings->ViewportId) { window->ViewportId = settings->ViewportId; window->ViewportPos = settings->ViewportPos; } else { window->ViewportPos = main_viewport->Pos; } window->Pos = ImFloor(settings->Pos + window->ViewportPos); window->Collapsed = settings->Collapsed; if (ImLengthSqr(settings->Size) > 0.00001f) size = ImFloor(settings->Size); window->DockId = settings->DockId; window->DockOrder = settings->DockOrder; } window->Size = window->SizeFull = window->SizeFullAtLastBegin = ImFloor(size); window->DC.CursorMaxPos = window->Pos; // So first call to CalcSizeContents() doesn't return crazy values if ((flags & ImGuiWindowFlags_AlwaysAutoResize) != 0) { window->AutoFitFramesX = window->AutoFitFramesY = 2; window->AutoFitOnlyGrows = false; } else { if (window->Size.x <= 0.0f) window->AutoFitFramesX = 2; if (window->Size.y <= 0.0f) window->AutoFitFramesY = 2; window->AutoFitOnlyGrows = (window->AutoFitFramesX > 0) || (window->AutoFitFramesY > 0); } g.WindowsFocusOrder.push_back(window); if (flags & ImGuiWindowFlags_NoBringToFrontOnFocus) g.Windows.push_front(window); // Quite slow but rare and only once else g.Windows.push_back(window); return window; } static ImGuiWindow* GetWindowForTitleDisplay(ImGuiWindow* window) { return window->DockNodeAsHost ? window->DockNodeAsHost->VisibleWindow : window; } static ImGuiWindow* GetWindowForTitleAndMenuHeight(ImGuiWindow* window) { return (window->DockNodeAsHost && window->DockNodeAsHost->VisibleWindow) ? window->DockNodeAsHost->VisibleWindow : window; } static ImVec2 CalcSizeAfterConstraint(ImGuiWindow* window, ImVec2 new_size) { ImGuiContext& g = *GImGui; if (g.NextWindowData.SizeConstraintCond != 0) { // Using -1,-1 on either X/Y axis to preserve the current size. ImRect cr = g.NextWindowData.SizeConstraintRect; new_size.x = (cr.Min.x >= 0 && cr.Max.x >= 0) ? ImClamp(new_size.x, cr.Min.x, cr.Max.x) : window->SizeFull.x; new_size.y = (cr.Min.y >= 0 && cr.Max.y >= 0) ? ImClamp(new_size.y, cr.Min.y, cr.Max.y) : window->SizeFull.y; if (g.NextWindowData.SizeCallback) { ImGuiSizeCallbackData data; data.UserData = g.NextWindowData.SizeCallbackUserData; data.Pos = window->Pos; data.CurrentSize = window->SizeFull; data.DesiredSize = new_size; g.NextWindowData.SizeCallback(&data); new_size = data.DesiredSize; } } // Minimum size if (!(window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysAutoResize))) { ImGuiWindow* window_for_height = GetWindowForTitleAndMenuHeight(window); new_size = ImMax(new_size, g.Style.WindowMinSize); 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 } return new_size; } static ImVec2 CalcSizeContents(ImGuiWindow* window) { if (window->Collapsed) if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) return window->SizeContents; if (window->Hidden && window->HiddenFramesCannotSkipItems == 0 && window->HiddenFramesCanSkipItems > 0) return window->SizeContents; ImVec2 sz; sz.x = (float)(int)((window->SizeContentsExplicit.x != 0.0f) ? window->SizeContentsExplicit.x : (window->DC.CursorMaxPos.x - window->Pos.x + window->Scroll.x)); sz.y = (float)(int)((window->SizeContentsExplicit.y != 0.0f) ? window->SizeContentsExplicit.y : (window->DC.CursorMaxPos.y - window->Pos.y + window->Scroll.y)); return sz + window->WindowPadding; } static ImVec2 CalcSizeAutoFit(ImGuiWindow* window, const ImVec2& size_contents) { ImGuiContext& g = *GImGui; ImGuiStyle& style = g.Style; if (window->Flags & ImGuiWindowFlags_Tooltip) { // Tooltip always resize return size_contents; } else { // Maximum window size is determined by the viewport size or monitor size const bool is_popup = (window->Flags & ImGuiWindowFlags_Popup) != 0; const bool is_menu = (window->Flags & ImGuiWindowFlags_ChildMenu) != 0; ImVec2 size_min = style.WindowMinSize; 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) size_min = ImMin(size_min, ImVec2(4.0f, 4.0f)); ImVec2 avail_size = window->Viewport->Size; if (window->ViewportOwned) avail_size = ImVec2(FLT_MAX, FLT_MAX); const int monitor_idx = window->ViewportAllowPlatformMonitorExtend; if (monitor_idx >= 0 && monitor_idx < g.PlatformIO.Monitors.Size) avail_size = g.PlatformIO.Monitors[monitor_idx].WorkSize; ImVec2 size_auto_fit = ImClamp(size_contents, size_min, ImMax(size_min, avail_size - g.Style.DisplaySafeAreaPadding * 2.0f)); // When the window cannot fit all contents (either because of constraints, either because screen is too small), // we are growing the size on the other axis to compensate for expected scrollbar. FIXME: Might turn bigger than ViewportSize-WindowPadding. ImVec2 size_auto_fit_after_constraint = CalcSizeAfterConstraint(window, size_auto_fit); if (size_auto_fit_after_constraint.x < size_contents.x && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && (window->Flags & ImGuiWindowFlags_HorizontalScrollbar)) size_auto_fit.y += style.ScrollbarSize; if (size_auto_fit_after_constraint.y < size_contents.y && !(window->Flags & ImGuiWindowFlags_NoScrollbar)) size_auto_fit.x += style.ScrollbarSize; return size_auto_fit; } } ImVec2 ImGui::CalcWindowExpectedSize(ImGuiWindow* window) { ImVec2 size_contents = CalcSizeContents(window); return CalcSizeAfterConstraint(window, CalcSizeAutoFit(window, size_contents)); } float ImGui::GetWindowScrollMaxX(ImGuiWindow* window) { return ImMax(0.0f, window->SizeContents.x - (window->SizeFull.x - window->ScrollbarSizes.x)); } float ImGui::GetWindowScrollMaxY(ImGuiWindow* window) { return ImMax(0.0f, window->SizeContents.y - (window->SizeFull.y - window->ScrollbarSizes.y)); } static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges) { ImGuiContext& g = *GImGui; ImVec2 scroll = window->Scroll; if (window->ScrollTarget.x < FLT_MAX) { float cr_x = window->ScrollTargetCenterRatio.x; scroll.x = window->ScrollTarget.x - cr_x * (window->SizeFull.x - window->ScrollbarSizes.x); } if (window->ScrollTarget.y < FLT_MAX) { // '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. float cr_y = window->ScrollTargetCenterRatio.y; float target_y = window->ScrollTarget.y; if (snap_on_edges && cr_y <= 0.0f && target_y <= window->WindowPadding.y) target_y = 0.0f; if (snap_on_edges && cr_y >= 1.0f && target_y >= window->SizeContents.y - window->WindowPadding.y + g.Style.ItemSpacing.y) target_y = window->SizeContents.y; scroll.y = target_y - (1.0f - cr_y) * (window->TitleBarHeight() + window->MenuBarHeight()) - cr_y * (window->SizeFull.y - window->ScrollbarSizes.y); } scroll = ImMax(scroll, ImVec2(0.0f, 0.0f)); if (!window->Collapsed && !window->SkipItems) { scroll.x = ImMin(scroll.x, ImGui::GetWindowScrollMaxX(window)); scroll.y = ImMin(scroll.y, ImGui::GetWindowScrollMaxY(window)); } return scroll; } static ImGuiCol GetWindowBgColorIdxFromFlags(ImGuiWindowFlags flags) { if (flags & (ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) return ImGuiCol_PopupBg; if (flags & ImGuiWindowFlags_ChildWindow) return ImGuiCol_ChildBg; return ImGuiCol_WindowBg; } static void CalcResizePosSizeFromAnyCorner(ImGuiWindow* window, const ImVec2& corner_target, const ImVec2& corner_norm, ImVec2* out_pos, ImVec2* out_size) { ImVec2 pos_min = ImLerp(corner_target, window->Pos, corner_norm); // Expected window upper-left ImVec2 pos_max = ImLerp(window->Pos + window->Size, corner_target, corner_norm); // Expected window lower-right ImVec2 size_expected = pos_max - pos_min; ImVec2 size_constrained = CalcSizeAfterConstraint(window, size_expected); *out_pos = pos_min; if (corner_norm.x == 0.0f) out_pos->x -= (size_constrained.x - size_expected.x); if (corner_norm.y == 0.0f) out_pos->y -= (size_constrained.y - size_expected.y); *out_size = size_constrained; } struct ImGuiResizeGripDef { ImVec2 CornerPosN; ImVec2 InnerDir; int AngleMin12, AngleMax12; }; static const ImGuiResizeGripDef resize_grip_def[4] = { { ImVec2(1,1), ImVec2(-1,-1), 0, 3 }, // Lower right { ImVec2(0,1), ImVec2(+1,-1), 3, 6 }, // Lower left { ImVec2(0,0), ImVec2(+1,+1), 6, 9 }, // Upper left { ImVec2(1,0), ImVec2(-1,+1), 9,12 }, // Upper right }; static ImRect GetResizeBorderRect(ImGuiWindow* window, int border_n, float perp_padding, float thickness) { ImRect rect = window->Rect(); if (thickness == 0.0f) rect.Max -= ImVec2(1,1); 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 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 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 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 IM_ASSERT(0); return ImRect(); } // Handle resize for: Resize Grips, Borders, Gamepad static void ImGui::UpdateManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]) { ImGuiContext& g = *GImGui; ImGuiWindowFlags flags = window->Flags; if ((flags & ImGuiWindowFlags_NoResize) || (flags & ImGuiWindowFlags_AlwaysAutoResize) || window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0) return; if (window->WasActive == false) // Early out to avoid running this code for e.g. an hidden implicit/fallback Debug window. return; const int resize_border_count = g.IO.ConfigWindowsResizeFromEdges ? 4 : 0; const float grip_draw_size = (float)(int)ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f); const float grip_hover_inner_size = (float)(int)(grip_draw_size * 0.75f); const float grip_hover_outer_size = g.IO.ConfigWindowsResizeFromEdges ? WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS : 0.0f; ImVec2 pos_target(FLT_MAX, FLT_MAX); ImVec2 size_target(FLT_MAX, FLT_MAX); // Resize grips and borders are on layer 1 window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); // Manual resize grips PushID("#RESIZE"); for (int resize_grip_n = 0; resize_grip_n < resize_grip_count; resize_grip_n++) { const ImGuiResizeGripDef& grip = resize_grip_def[resize_grip_n]; const ImVec2 corner = ImLerp(window->Pos, window->Pos + window->Size, grip.CornerPosN); // Using the FlattenChilds button flag we make the resize button accessible even if we are hovering over a child window ImRect resize_rect(corner - grip.InnerDir * grip_hover_outer_size, corner + grip.InnerDir * grip_hover_inner_size); if (resize_rect.Min.x > resize_rect.Max.x) ImSwap(resize_rect.Min.x, resize_rect.Max.x); if (resize_rect.Min.y > resize_rect.Max.y) ImSwap(resize_rect.Min.y, resize_rect.Max.y); bool hovered, held; ButtonBehavior(resize_rect, window->GetID((void*)(intptr_t)resize_grip_n), &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); //GetForegroundDrawList(window)->AddRect(resize_rect.Min, resize_rect.Max, IM_COL32(255, 255, 0, 255)); if (hovered || held) g.MouseCursor = (resize_grip_n & 1) ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; if (held && g.IO.MouseDoubleClicked[0] && resize_grip_n == 0) { // Manual auto-fit when double-clicking size_target = CalcSizeAfterConstraint(window, size_auto_fit); ClearActiveID(); } else if (held) { // Resize from any of the four corners // We don't use an incremental MouseDelta but rather compute an absolute target size based on mouse position 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 CalcResizePosSizeFromAnyCorner(window, corner_target, grip.CornerPosN, &pos_target, &size_target); } if (resize_grip_n == 0 || held || hovered) resize_grip_col[resize_grip_n] = GetColorU32(held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip); } for (int border_n = 0; border_n < resize_border_count; border_n++) { bool hovered, held; ImRect border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); ButtonBehavior(border_rect, window->GetID((void*)(intptr_t)(border_n + 4)), &hovered, &held, ImGuiButtonFlags_FlattenChildren); //GetForegroundDrawLists(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255)); if ((hovered && g.HoveredIdTimer > WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) || held) { g.MouseCursor = (border_n & 1) ? ImGuiMouseCursor_ResizeEW : ImGuiMouseCursor_ResizeNS; if (held) *border_held = border_n; } if (held) { ImVec2 border_target = window->Pos; ImVec2 border_posn; 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 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 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 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 CalcResizePosSizeFromAnyCorner(window, border_target, border_posn, &pos_target, &size_target); } } PopID(); // Navigation resize (keyboard/gamepad) if (g.NavWindowingTarget && g.NavWindowingTarget->RootWindow == window) { ImVec2 nav_resize_delta; if (g.NavInputSource == ImGuiInputSource_NavKeyboard && g.IO.KeyShift) nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down); if (g.NavInputSource == ImGuiInputSource_NavGamepad) nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_Down); if (nav_resize_delta.x != 0.0f || nav_resize_delta.y != 0.0f) { const float NAV_RESIZE_SPEED = 600.0f; nav_resize_delta *= ImFloor(NAV_RESIZE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y)); g.NavWindowingToggleLayer = false; g.NavDisableMouseHover = true; resize_grip_col[0] = GetColorU32(ImGuiCol_ResizeGripActive); // FIXME-NAV: Should store and accumulate into a separate size buffer to handle sizing constraints properly, right now a constraint will make us stuck. size_target = CalcSizeAfterConstraint(window, window->SizeFull + nav_resize_delta); } } // Apply back modified position/size to window if (size_target.x != FLT_MAX) { window->SizeFull = size_target; MarkIniSettingsDirty(window); } if (pos_target.x != FLT_MAX) { window->Pos = ImFloor(pos_target); MarkIniSettingsDirty(window); } // Resize nav layer window->DC.NavLayerCurrent = ImGuiNavLayer_Main; window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); window->Size = window->SizeFull; } static inline void ClampWindowRect(ImGuiWindow* window, const ImRect& rect, const ImVec2& padding) { ImGuiContext& g = *GImGui; ImVec2 size_for_clamping = (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) ? ImVec2(window->Size.x, window->TitleBarHeight()) : window->Size; window->Pos = ImMin(rect.Max - padding, ImMax(window->Pos + size_for_clamping, rect.Min + padding) - size_for_clamping); } static void ImGui::RenderOuterBorders(ImGuiWindow* window) { ImGuiContext& g = *GImGui; float rounding = window->WindowRounding; float border_size = window->WindowBorderSize; if (border_size > 0.0f && !(window->Flags & ImGuiWindowFlags_NoBackground)) window->DrawList->AddRect(window->Pos, window->Pos + window->Size, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); int border_held = window->ResizeBorderHeld; if (border_held != -1) { struct ImGuiResizeBorderDef { ImVec2 InnerDir; ImVec2 CornerPosN1, CornerPosN2; float OuterAngle; }; static const ImGuiResizeBorderDef resize_border_def[4] = { { ImVec2(0,+1), ImVec2(0,0), ImVec2(1,0), IM_PI*1.50f }, // Top { ImVec2(-1,0), ImVec2(1,0), ImVec2(1,1), IM_PI*0.00f }, // Right { ImVec2(0,-1), ImVec2(1,1), ImVec2(0,1), IM_PI*0.50f }, // Bottom { ImVec2(+1,0), ImVec2(0,1), ImVec2(0,0), IM_PI*1.00f } // Left }; const ImGuiResizeBorderDef& def = resize_border_def[border_held]; ImRect border_r = GetResizeBorderRect(window, border_held, rounding, 0.0f); 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); 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); window->DrawList->PathStroke(GetColorU32(ImGuiCol_SeparatorActive), false, ImMax(2.0f, border_size)); // Thicker than usual } if (g.Style.FrameBorderSize > 0 && !(window->Flags & ImGuiWindowFlags_NoTitleBar) && !window->DockIsActive) { float y = window->Pos.y + window->TitleBarHeight() - 1; 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); } } void ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window) { window->ParentWindow = parent_window; window->RootWindow = window->RootWindowDockStop = window->RootWindowForTitleBarHighlight = window->RootWindowForNav = window; if (parent_window && (flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Tooltip)) { window->RootWindow = parent_window->RootWindow; if (!window->DockIsActive && !(parent_window->Flags & ImGuiWindowFlags_DockNodeHost)) window->RootWindowDockStop = parent_window->RootWindowDockStop; } if (parent_window && !(flags & ImGuiWindowFlags_Modal) && (flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup))) window->RootWindowForTitleBarHighlight = parent_window->RootWindowForTitleBarHighlight; while (window->RootWindowForNav->Flags & ImGuiWindowFlags_NavFlattened) { IM_ASSERT(window->RootWindowForNav->ParentWindow != NULL); window->RootWindowForNav = window->RootWindowForNav->ParentWindow; } } // Push a new ImGui window to add widgets to. // - 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. // - Begin/End can be called multiple times during the frame with the same window name to append content. // - The window name is used as a unique identifier to preserve window information across frames (and save rudimentary information to the .ini file). // 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. // - 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. // - 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. bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; IM_ASSERT(name != NULL && name[0] != '\0'); // Window name required IM_ASSERT(g.FrameScopeActive); // Forgot to call ImGui::NewFrame() IM_ASSERT(g.FrameCountEnded != g.FrameCount); // Called ImGui::Render() or ImGui::EndFrame() and haven't called ImGui::NewFrame() again yet // Find or create ImGuiWindow* window = FindWindowByName(name); const bool window_just_created = (window == NULL); const bool window_is_fallback = (g.CurrentWindowStack.Size == 0); if (window_just_created) { 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. window = CreateNewWindow(name, size_on_first_use, flags); } // Automatically disable manual moving/resizing when NoInputs is set if ((flags & ImGuiWindowFlags_NoInputs) == ImGuiWindowFlags_NoInputs) flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize; if (flags & ImGuiWindowFlags_NavFlattened) IM_ASSERT(flags & ImGuiWindowFlags_ChildWindow); const int current_frame = g.FrameCount; const bool first_begin_of_the_frame = (window->LastFrameActive != current_frame); // Update the Appearing flag bool window_just_activated_by_user = (window->LastFrameActive < current_frame - 1); // Not using !WasActive because the implicit "Debug" window would always toggle off->on const bool window_just_appearing_after_hidden_for_resize = (window->HiddenFramesCannotSkipItems > 0); if (flags & ImGuiWindowFlags_Popup) { ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size]; window_just_activated_by_user |= (window->PopupId != popup_ref.PopupId); // We recycle popups so treat window as activated if popup id changed window_just_activated_by_user |= (window != popup_ref.Window); } window->Appearing = (window_just_activated_by_user || window_just_appearing_after_hidden_for_resize); if (window->Appearing) SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, true); // Update Flags, LastFrameActive, BeginOrderXXX fields if (first_begin_of_the_frame) { window->FlagsPreviousFrame = window->Flags; window->Flags = (ImGuiWindowFlags)flags; window->LastFrameActive = current_frame; window->BeginOrderWithinParent = 0; window->BeginOrderWithinContext = (short)(g.WindowsActiveCount++); } else { flags = window->Flags; } // Docking // (NB: during the frame dock nodes are created, it is possible that (window->DockIsActive == false) even though (window->DockNode->Windows.Size > 1) IM_ASSERT(window->DockNode == NULL || window->DockNodeAsHost == NULL); // Cannot be both if (g.NextWindowData.DockCond) SetWindowDock(window, g.NextWindowData.DockId, g.NextWindowData.DockCond); if (first_begin_of_the_frame) { bool has_dock_node = (window->DockId != 0 || window->DockNode != NULL); bool new_auto_dock_node = !has_dock_node && g.IO.ConfigDockingTabBarOnSingleWindows && !(flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDocking)) && !window_is_fallback; if (has_dock_node || new_auto_dock_node) { BeginDocked(window, p_open); flags = window->Flags; } } // 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 ImGuiWindow* parent_window_in_stack = window->DockIsActive ? window->DockNode->HostWindow : g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back(); ImGuiWindow* parent_window = first_begin_of_the_frame ? ((flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup)) ? parent_window_in_stack : NULL) : window->ParentWindow; IM_ASSERT(parent_window != NULL || !(flags & ImGuiWindowFlags_ChildWindow)); // Add to stack // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() g.CurrentWindowStack.push_back(window); g.CurrentWindow = NULL; CheckStacksSize(window, true); if (flags & ImGuiWindowFlags_Popup) { ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size]; popup_ref.Window = window; g.BeginPopupStack.push_back(popup_ref); window->PopupId = popup_ref.PopupId; } if (window_just_appearing_after_hidden_for_resize && !(flags & ImGuiWindowFlags_ChildWindow)) window->NavLastIds[0] = 0; // Process SetNextWindow***() calls bool window_pos_set_by_api = false; bool window_size_x_set_by_api = false, window_size_y_set_by_api = false; if (g.NextWindowData.PosCond) { window_pos_set_by_api = (window->SetWindowPosAllowFlags & g.NextWindowData.PosCond) != 0; if (window_pos_set_by_api && ImLengthSqr(g.NextWindowData.PosPivotVal) > 0.00001f) { // May be processed on the next frame if this is our first frame and we are measuring size // FIXME: Look into removing the branch so everything can go through this same code path for consistency. window->SetWindowPosVal = g.NextWindowData.PosVal; window->SetWindowPosPivot = g.NextWindowData.PosPivotVal; window->SetWindowPosAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); } else { SetWindowPos(window, g.NextWindowData.PosVal, g.NextWindowData.PosCond); } } if (g.NextWindowData.SizeCond) { window_size_x_set_by_api = (window->SetWindowSizeAllowFlags & g.NextWindowData.SizeCond) != 0 && (g.NextWindowData.SizeVal.x > 0.0f); window_size_y_set_by_api = (window->SetWindowSizeAllowFlags & g.NextWindowData.SizeCond) != 0 && (g.NextWindowData.SizeVal.y > 0.0f); SetWindowSize(window, g.NextWindowData.SizeVal, g.NextWindowData.SizeCond); } if (g.NextWindowData.ContentSizeCond) { // Adjust passed "client size" to become a "window size" window->SizeContentsExplicit = g.NextWindowData.ContentSizeVal; if (window->SizeContentsExplicit.y != 0.0f) window->SizeContentsExplicit.y += window->TitleBarHeight() + window->MenuBarHeight(); } else if (first_begin_of_the_frame) { window->SizeContentsExplicit = ImVec2(0.0f, 0.0f); } window->WindowClass = g.NextWindowData.WindowClass; if (g.NextWindowData.CollapsedCond) SetWindowCollapsed(window, g.NextWindowData.CollapsedVal, g.NextWindowData.CollapsedCond); if (g.NextWindowData.FocusCond) FocusWindow(window); if (window->Appearing) SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, false); // When reusing window again multiple times a frame, just append content (don't need to setup again) if (first_begin_of_the_frame) { // Initialize const bool window_is_child_tooltip = (flags & ImGuiWindowFlags_ChildWindow) && (flags & ImGuiWindowFlags_Tooltip); // FIXME-WIP: Undocumented behavior of Child+Tooltip for pinned tooltip (#1345) UpdateWindowParentAndRootLinks(window, flags, parent_window); window->Active = true; window->HasCloseButton = (p_open != NULL); window->ClipRect = ImVec4(-FLT_MAX,-FLT_MAX,+FLT_MAX,+FLT_MAX); window->IDStack.resize(1); // Update stored window name when it changes (which can _only_ happen with the "###" operator, so the ID would stay unchanged). // 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. bool window_title_visible_elsewhere = false; if ((window->Viewport && window->Viewport->Window == window) || (window->DockIsActive)) window_title_visible_elsewhere = true; else if (g.NavWindowingList != NULL && (window->Flags & ImGuiWindowFlags_NoNavFocus) == 0) // Window titles visible when using CTRL+TAB window_title_visible_elsewhere = true; if (window_title_visible_elsewhere && !window_just_created && strcmp(name, window->Name) != 0) { size_t buf_len = (size_t)window->NameBufLen; window->Name = ImStrdupcpy(window->Name, &buf_len, name); window->NameBufLen = (int)buf_len; } // UPDATE CONTENTS SIZE, UPDATE HIDDEN STATUS // Update contents size from last frame for auto-fitting (or use explicit size) window->SizeContents = CalcSizeContents(window); if (window->HiddenFramesCanSkipItems > 0) window->HiddenFramesCanSkipItems--; if (window->HiddenFramesCannotSkipItems > 0) window->HiddenFramesCannotSkipItems--; // Hide new windows for one frame until they calculate their size if (window_just_created && (!window_size_x_set_by_api || !window_size_y_set_by_api)) window->HiddenFramesCannotSkipItems = 1; // Hide popup/tooltip window when re-opening while we measure size (because we recycle the windows) // We reset Size/SizeContents for reappearing popups/tooltips early in this function, so further code won't be tempted to use the old size. if (window_just_activated_by_user && (flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) != 0) { window->HiddenFramesCannotSkipItems = 1; if (flags & ImGuiWindowFlags_AlwaysAutoResize) { if (!window_size_x_set_by_api) window->Size.x = window->SizeFull.x = 0.f; if (!window_size_y_set_by_api) window->Size.y = window->SizeFull.y = 0.f; window->SizeContents = ImVec2(0.f, 0.f); } } // SELECT VIEWPORT // We need to do this before using any style/font sizes, as viewport with a different DPI may affect font sizes. UpdateSelectWindowViewport(window); SetCurrentViewport(window, window->Viewport); window->FontDpiScale = (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleFonts) ? window->Viewport->DpiScale : 1.0f; SetCurrentWindow(window); flags = window->Flags; // Lock border size and padding for the frame (so that altering them doesn't cause inconsistencies) if (flags & ImGuiWindowFlags_ChildWindow) window->WindowBorderSize = style.ChildBorderSize; else window->WindowBorderSize = ((flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) && !(flags & ImGuiWindowFlags_Modal)) ? style.PopupBorderSize : style.WindowBorderSize; if (!window->DockIsActive && (flags & ImGuiWindowFlags_ChildWindow) && !(flags & (ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_Popup)) && window->WindowBorderSize == 0.0f) window->WindowPadding = ImVec2(0.0f, (flags & ImGuiWindowFlags_MenuBar) ? style.WindowPadding.y : 0.0f); else window->WindowPadding = style.WindowPadding; window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x); window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y; // Collapse window by double-clicking on title bar // 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 if (!(flags & ImGuiWindowFlags_NoTitleBar) && !(flags & ImGuiWindowFlags_NoCollapse) && !window->DockIsActive) { // 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. ImRect title_bar_rect = window->TitleBarRect(); if (g.HoveredWindow == window && g.HoveredId == 0 && g.HoveredIdPreviousFrame == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max) && g.IO.MouseDoubleClicked[0]) window->WantCollapseToggle = true; if (window->WantCollapseToggle) { window->Collapsed = !window->Collapsed; MarkIniSettingsDirty(window); FocusWindow(window); } } else { window->Collapsed = false; } window->WantCollapseToggle = false; // SIZE // Calculate auto-fit size, handle automatic resize const ImVec2 size_auto_fit = CalcSizeAutoFit(window, window->SizeContents); ImVec2 size_full_modified(FLT_MAX, FLT_MAX); if ((flags & ImGuiWindowFlags_AlwaysAutoResize) && !window->Collapsed) { // Using SetNextWindowSize() overrides ImGuiWindowFlags_AlwaysAutoResize, so it can be used on tooltips/popups, etc. if (!window_size_x_set_by_api) window->SizeFull.x = size_full_modified.x = size_auto_fit.x; if (!window_size_y_set_by_api) window->SizeFull.y = size_full_modified.y = size_auto_fit.y; } else if (window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0) { // Auto-fit may only grow window during the first few frames // We still process initial auto-fit on collapsed windows to get a window width, but otherwise don't honor ImGuiWindowFlags_AlwaysAutoResize when collapsed. if (!window_size_x_set_by_api && window->AutoFitFramesX > 0) window->SizeFull.x = size_full_modified.x = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.x, size_auto_fit.x) : size_auto_fit.x; if (!window_size_y_set_by_api && window->AutoFitFramesY > 0) window->SizeFull.y = size_full_modified.y = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.y, size_auto_fit.y) : size_auto_fit.y; if (!window->Collapsed) MarkIniSettingsDirty(window); } // Apply minimum/maximum window size constraints and final size window->SizeFull = CalcSizeAfterConstraint(window, window->SizeFull); window->Size = window->Collapsed && !(flags & ImGuiWindowFlags_ChildWindow) ? window->TitleBarRect().GetSize() : window->SizeFull; // SCROLLBAR STATUS // Update scrollbar status (based on the Size that was effective during last frame or the auto-resized Size). if (!window->Collapsed) { // When reading the current size we need to read it after size constraints have been applied float size_x_for_scrollbars = size_full_modified.x != FLT_MAX ? window->SizeFull.x : window->SizeFullAtLastBegin.x; float size_y_for_scrollbars = size_full_modified.y != FLT_MAX ? window->SizeFull.y : window->SizeFullAtLastBegin.y; window->ScrollbarY = (flags & ImGuiWindowFlags_AlwaysVerticalScrollbar) || ((window->SizeContents.y > size_y_for_scrollbars) && !(flags & ImGuiWindowFlags_NoScrollbar)); window->ScrollbarX = (flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar) || ((window->SizeContents.x > size_x_for_scrollbars - (window->ScrollbarY ? style.ScrollbarSize : 0.0f)) && !(flags & ImGuiWindowFlags_NoScrollbar) && (flags & ImGuiWindowFlags_HorizontalScrollbar)); if (window->ScrollbarX && !window->ScrollbarY) window->ScrollbarY = (window->SizeContents.y > size_y_for_scrollbars - style.ScrollbarSize) && !(flags & ImGuiWindowFlags_NoScrollbar); window->ScrollbarSizes = ImVec2(window->ScrollbarY ? style.ScrollbarSize : 0.0f, window->ScrollbarX ? style.ScrollbarSize : 0.0f); } // POSITION // Popup latch its initial position, will position itself when it appears next frame if (window_just_activated_by_user) { window->AutoPosLastDirection = ImGuiDir_None; if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api) window->Pos = g.BeginPopupStack.back().OpenPopupPos; } // Position child window if (flags & ImGuiWindowFlags_ChildWindow) { IM_ASSERT(parent_window && parent_window->Active); window->BeginOrderWithinParent = (short)parent_window->DC.ChildWindows.Size; parent_window->DC.ChildWindows.push_back(window); if (!(flags & ImGuiWindowFlags_Popup) && !window_pos_set_by_api && !window_is_child_tooltip) window->Pos = parent_window->DC.CursorPos; } const bool window_pos_with_pivot = (window->SetWindowPosVal.x != FLT_MAX && window->HiddenFramesCannotSkipItems == 0); if (window_pos_with_pivot) SetWindowPos(window, ImMax(style.DisplaySafeAreaPadding, window->SetWindowPosVal - window->SizeFull * window->SetWindowPosPivot), 0); // Position given a pivot (e.g. for centering) else if ((flags & ImGuiWindowFlags_ChildMenu) != 0) window->Pos = FindBestWindowPosForPopup(window); else if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api && window_just_appearing_after_hidden_for_resize) window->Pos = FindBestWindowPosForPopup(window); else if ((flags & ImGuiWindowFlags_Tooltip) != 0 && !window_pos_set_by_api && !window_is_child_tooltip) window->Pos = FindBestWindowPosForPopup(window); // Late create viewport if we don't fit within our current host viewport. if (window->ViewportAllowPlatformMonitorExtend >= 0 && !window->ViewportOwned && !(window->Viewport->Flags & ImGuiViewportFlags_Minimized)) if (!window->Viewport->GetRect().Contains(window->Rect())) { // This is based on the assumption that the DPI will be known ahead (same as the DPI of the selection done in UpdateSelectWindowViewport) //ImGuiViewport* old_viewport = window->Viewport; window->Viewport = AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_NoFocusOnAppearing); // FIXME-DPI //IM_ASSERT(old_viewport->DpiScale == window->Viewport->DpiScale); // FIXME-DPI: Something went wrong SetCurrentViewport(window, window->Viewport); window->FontDpiScale = (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleFonts) ? window->Viewport->DpiScale : 1.0f; SetCurrentWindow(window); } bool viewport_rect_changed = false; if (window->ViewportOwned) { // Synchronize window --> viewport in most situations // Synchronize viewport -> window in case the platform window has been moved or resized from the OS/WM if (window->Viewport->PlatformRequestMove) window->Pos = window->Viewport->Pos; else if (memcmp(&window->Viewport->Pos, &window->Pos, sizeof(window->Pos)) != 0) { viewport_rect_changed = true; window->Viewport->Pos = window->Pos; } if (window->Viewport->PlatformRequestResize) window->Size = window->SizeFull = window->Viewport->Size; else if (memcmp(&window->Viewport->Size, &window->Size, sizeof(window->Size)) != 0) { viewport_rect_changed = true; window->Viewport->Size = window->Size; } // The viewport may have changed monitor since the global update in UpdateViewportsNewFrame() // Either a SetNextWindowPos() call in the current frame or a SetWindowPos() call in the previous frame may have this effect. if (viewport_rect_changed) UpdateViewportPlatformMonitor(window->Viewport); // Update common viewport flags ImGuiViewportFlags viewport_flags = (window->Viewport->Flags) & ~(ImGuiViewportFlags_TopMost | ImGuiViewportFlags_NoTaskBarIcon | ImGuiViewportFlags_NoDecoration); const bool is_short_lived_floating_window = (flags & (ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) != 0; if (flags & ImGuiWindowFlags_Tooltip) viewport_flags |= ImGuiViewportFlags_TopMost; if (g.IO.ConfigViewportsNoTaskBarIcon || is_short_lived_floating_window) viewport_flags |= ImGuiViewportFlags_NoTaskBarIcon; if (g.IO.ConfigViewportsNoDecoration || is_short_lived_floating_window) viewport_flags |= ImGuiViewportFlags_NoDecoration; // For popups and menus that may be protruding out of their parent viewport, we enable _NoFocusOnClick so that clicking on them // won't steal the OS focus away from their parent window (which may be reflected in OS the title bar decoration). // 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, // but it shouldn't be much of a problem considering those are already popups that are closed when clicking elsewhere. if (is_short_lived_floating_window) viewport_flags |= ImGuiViewportFlags_NoFocusOnAppearing | ImGuiViewportFlags_NoFocusOnClick; // We can overwrite viewport flags using ImGuiWindowClass (advanced users) // We don't default to the main viewport because. if (window->WindowClass.ParentViewportId) window->Viewport->ParentViewportId = window->WindowClass.ParentViewportId; else if ((flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) && parent_window_in_stack) window->Viewport->ParentViewportId = parent_window_in_stack->Viewport->ID; else window->Viewport->ParentViewportId = g.IO.ConfigViewportsNoDefaultParent ? 0 : IMGUI_VIEWPORT_DEFAULT_ID; if (window->WindowClass.ViewportFlagsOverrideMask) viewport_flags = (viewport_flags & ~window->WindowClass.ViewportFlagsOverrideMask) | (window->WindowClass.ViewportFlagsOverrideValue & window->WindowClass.ViewportFlagsOverrideMask); // 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 viewport_flags |= ImGuiViewportFlags_NoRendererClear; window->Viewport->Flags = viewport_flags; } // Clamp position so window stays visible within its viewport or monitor // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing. ImRect viewport_rect = window->Viewport->GetRect(); if (!window_pos_set_by_api && !(flags & ImGuiWindowFlags_ChildWindow) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) { ImVec2 clamp_padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding); if (!window->ViewportOwned && viewport_rect.GetWidth() > 0 && viewport_rect.GetHeight() > 0.0f) ClampWindowRect(window, viewport_rect, clamp_padding); else if (window->ViewportOwned && g.PlatformIO.Monitors.Size > 0) { if (window->Viewport->PlatformMonitor == -1) { // Fallback for "lost" window (e.g. a monitor disconnected): we move the window back over the main viewport SetWindowPos(window, g.Viewports[0]->Pos + style.DisplayWindowPadding, ImGuiCond_Always); } else { ImGuiPlatformMonitor& monitor = g.PlatformIO.Monitors[window->Viewport->PlatformMonitor]; ClampWindowRect(window, ImRect(monitor.WorkPos, monitor.WorkPos + monitor.WorkSize), clamp_padding); } } } window->Pos = ImFloor(window->Pos); // Lock window rounding for the frame (so that altering them doesn't cause inconsistencies) window->WindowRounding = (flags & ImGuiWindowFlags_ChildWindow) ? style.ChildRounding : ((flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiWindowFlags_Modal)) ? style.PopupRounding : style.WindowRounding; if (window->ViewportOwned) window->WindowRounding = 0.0f; // Apply scrolling window->Scroll = CalcNextScrollFromScrollTargetAndClamp(window, true); window->ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); // Apply window focus (new and reactivated windows are moved to front) bool want_focus = false; if (window_just_activated_by_user && !(flags & ImGuiWindowFlags_NoFocusOnAppearing)) { if (flags & ImGuiWindowFlags_Popup) want_focus = true; else if ((window->DockIsActive || (flags & ImGuiWindowFlags_ChildWindow) == 0) && !(flags & ImGuiWindowFlags_Tooltip)) want_focus = true; } // Decide if we are going to handle borders and resize grips const bool handle_borders_and_resize_grips = (window->DockNodeAsHost || !window->DockIsActive); // Handle manual resize: Resize Grips, Borders, Gamepad int border_held = -1; ImU32 resize_grip_col[4] = { 0 }; const int resize_grip_count = g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // 4 const float grip_draw_size = (float)(int)ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f); if (handle_borders_and_resize_grips && !window->Collapsed) UpdateManualResize(window, size_auto_fit, &border_held, resize_grip_count, &resize_grip_col[0]); window->ResizeBorderHeld = (signed char)border_held; // Synchronize window --> viewport again and one last time (clamping and manual resize may have affected either) if (window->ViewportOwned) { if (!window->Viewport->PlatformRequestMove) window->Viewport->Pos = window->Pos; if (!window->Viewport->PlatformRequestResize) window->Viewport->Size = window->Size; viewport_rect = window->Viewport->GetRect(); } // Save last known viewport position within the window itself (so it can be saved in .ini file and restored) window->ViewportPos = window->Viewport->Pos; // Default item width. Make it proportional to window size if window manually resizes if (window->Size.x > 0.0f && !(flags & ImGuiWindowFlags_Tooltip) && !(flags & ImGuiWindowFlags_AlwaysAutoResize)) window->ItemWidthDefault = (float)(int)(window->Size.x * 0.65f); else window->ItemWidthDefault = (float)(int)(g.FontSize * 16.0f); // DRAWING // Setup draw list and outer clipping rectangle window->DrawList->Clear(); window->DrawList->Flags = (g.Style.AntiAliasedLines ? ImDrawListFlags_AntiAliasedLines : 0) | (g.Style.AntiAliasedFill ? ImDrawListFlags_AntiAliasedFill : 0); window->DrawList->PushTextureID(g.Font->ContainerAtlas->TexID); if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup) && !window_is_child_tooltip) PushClipRect(parent_window->ClipRect.Min, parent_window->ClipRect.Max, true); else PushClipRect(viewport_rect.Min, viewport_rect.Max, true); // Draw modal or window list full viewport dimming background (for other viewports we'll render them in EndFrame) const bool dim_bg_for_modal = (flags & ImGuiWindowFlags_Modal) && window == GetFrontMostPopupModal() && window->HiddenFramesCannotSkipItems <= 0; const bool dim_bg_for_window_list = g.NavWindowingTargetAnim && ((window == g.NavWindowingTargetAnim->RootWindow) || (g.NavWindowingList && (window == g.NavWindowingList) && g.NavWindowingList->Viewport != g.NavWindowingTargetAnim->Viewport)); if (dim_bg_for_modal || dim_bg_for_window_list) { const ImU32 dim_bg_col = GetColorU32(dim_bg_for_modal ? ImGuiCol_ModalWindowDimBg : ImGuiCol_NavWindowingDimBg, g.DimBgRatio); window->DrawList->AddRectFilled(viewport_rect.Min, viewport_rect.Max, dim_bg_col); } // Draw navigation selection/windowing rectangle background if (dim_bg_for_window_list && window == g.NavWindowingTargetAnim) { ImRect bb = window->Rect(); bb.Expand(g.FontSize); if (!bb.Contains(viewport_rect)) // Avoid drawing if the window covers all the viewport anyway window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha * 0.25f), g.Style.WindowRounding); } // Draw window + handle manual resize // 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. const float window_rounding = window->WindowRounding; const float window_border_size = window->WindowBorderSize; const ImGuiWindow* window_to_highlight = g.NavWindowingTarget ? g.NavWindowingTarget : g.NavWindow; 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))); const ImRect title_bar_rect = window->TitleBarRect(); if (window->Collapsed) { // Title bar only float backup_border_size = style.FrameBorderSize; g.Style.FrameBorderSize = window->WindowBorderSize; ImU32 title_bar_col = GetColorU32((title_bar_is_highlight && !g.NavDisableHighlight) ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBgCollapsed); RenderFrame(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, true, window_rounding); g.Style.FrameBorderSize = backup_border_size; } else { // Window background if (!(flags & ImGuiWindowFlags_NoBackground)) { bool is_docking_transparent_payload = false; if (g.DragDropActive && (g.FrameCount - g.DragDropAcceptFrameCount) <= 1 && g.IO.ConfigDockingTransparentPayload) if (g.DragDropPayload.IsDataType(IMGUI_PAYLOAD_TYPE_WINDOW) && *(ImGuiWindow**)g.DragDropPayload.Data == window) is_docking_transparent_payload = true; ImU32 bg_col = GetColorU32(GetWindowBgColorIdxFromFlags(flags)); if (window->ViewportOwned) { // No alpha bg_col = (bg_col | IM_COL32_A_MASK); if (is_docking_transparent_payload) window->Viewport->Alpha *= DOCKING_TRANSPARENT_PAYLOAD_ALPHA; } else { // Adjust alpha. For docking float alpha = 1.0f; if (g.NextWindowData.BgAlphaCond != 0) alpha = g.NextWindowData.BgAlphaVal; if (is_docking_transparent_payload) alpha *= DOCKING_TRANSPARENT_PAYLOAD_ALPHA; if (alpha != 1.0f) bg_col = (bg_col & ~IM_COL32_A_MASK) | (IM_F32_TO_INT8_SAT(alpha) << IM_COL32_A_SHIFT); } window->DrawList->AddRectFilled(window->Pos + ImVec2(0, window->TitleBarHeight()), window->Pos + window->Size, bg_col, window_rounding, (flags & ImGuiWindowFlags_NoTitleBar) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Bot); } g.NextWindowData.BgAlphaCond = 0; // Title bar // (when docked, DockNode are drawing their own title bar. Individual windows however do NOT set the _NoTitleBar flag, // in order for their pos/size to be matching their undocking state.) if (!(flags & ImGuiWindowFlags_NoTitleBar) && !window->DockIsActive) { ImU32 title_bar_col = GetColorU32(title_bar_is_highlight ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg); window->DrawList->AddRectFilled(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, window_rounding, ImDrawCornerFlags_Top); } // Menu bar if (flags & ImGuiWindowFlags_MenuBar) { ImRect menu_bar_rect = window->MenuBarRect(); 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. 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); if (style.FrameBorderSize > 0.0f && menu_bar_rect.Max.y < window->Pos.y + window->Size.y) window->DrawList->AddLine(menu_bar_rect.GetBL(), menu_bar_rect.GetBR(), GetColorU32(ImGuiCol_Border), style.FrameBorderSize); } // Docking: Unhide tab bar (small triangle in the corner) if (window->DockNode && window->DockNode->IsHiddenTabBar() && !window->DockNode->IsNoTabBar()) { float unhide_sz_draw = ImFloor(g.FontSize * 0.70f); float unhide_sz_hit = ImFloor(g.FontSize * 0.55f); ImVec2 p = window->DockNode->Pos; ImRect r(p, p + ImVec2(unhide_sz_hit, unhide_sz_hit)); bool hovered, held; if (ButtonBehavior(r, window->GetID("#UNHIDE"), &hovered, &held, ImGuiButtonFlags_FlattenChildren)) window->DockNode->WantHiddenTabBarToggle = true; // FIXME-DOCK: Ideally we'd use ImGuiCol_TitleBgActive/ImGuiCol_TitleBg here, but neither is guaranteed to be visible enough at this sort of size.. ImU32 col = GetColorU32(((held && hovered) || (window->DockNode->IsFocused && !hovered)) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); window->DrawList->AddTriangleFilled(p, p + ImVec2(unhide_sz_draw, 0.0f), p + ImVec2(0.0f, unhide_sz_draw), col); } // Scrollbars if (window->ScrollbarX) Scrollbar(ImGuiAxis_X); if (window->ScrollbarY) Scrollbar(ImGuiAxis_Y); // Render resize grips (after their input handling so we don't have a frame of latency) if (handle_borders_and_resize_grips && !(flags & ImGuiWindowFlags_NoResize)) { for (int resize_grip_n = 0; resize_grip_n < resize_grip_count; resize_grip_n++) { const ImGuiResizeGripDef& grip = resize_grip_def[resize_grip_n]; const ImVec2 corner = ImLerp(window->Pos, window->Pos + window->Size, grip.CornerPosN); window->DrawList->PathLineTo(corner + grip.InnerDir * ((resize_grip_n & 1) ? ImVec2(window_border_size, grip_draw_size) : ImVec2(grip_draw_size, window_border_size))); window->DrawList->PathLineTo(corner + grip.InnerDir * ((resize_grip_n & 1) ? ImVec2(grip_draw_size, window_border_size) : ImVec2(window_border_size, grip_draw_size))); 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); window->DrawList->PathFillConvex(resize_grip_col[resize_grip_n]); } } // Borders (for dock node host they will be rendered over after the tab bar) if (handle_borders_and_resize_grips && !window->DockNodeAsHost) RenderOuterBorders(window); } // Store a backup of SizeFull which we will use next frame to decide if we need scrollbars. window->SizeFullAtLastBegin = window->SizeFull; // Update various regions. Variables they depends on are set above in this function. // FIXME: window->ContentsRegionRect.Max is currently very misleading / partly faulty, but some BeginChild() patterns relies on it. // NB: WindowBorderSize is included in WindowPadding _and_ ScrollbarSizes so we need to cancel one out. window->ContentsRegionRect.Min.x = window->Pos.x - window->Scroll.x + window->WindowPadding.x; window->ContentsRegionRect.Min.y = window->Pos.y - window->Scroll.y + window->WindowPadding.y + window->TitleBarHeight() + window->MenuBarHeight(); 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))); 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))); // Save clipped aabb so we can access it in constant-time in FindHoveredWindow() window->OuterRectClipped = window->Rect(); if (window->DockIsActive) window->OuterRectClipped.Min.y += window->TitleBarHeight(); window->OuterRectClipped.ClipWith(window->ClipRect); // Inner rectangle // We set this up after processing the resize grip so that our clip rectangle doesn't lag by a frame // Note that if our window is collapsed we will end up with an inverted (~null) clipping rectangle which is the correct behavior. window->InnerMainRect.Min.x = title_bar_rect.Min.x + window->WindowBorderSize; window->InnerMainRect.Min.y = title_bar_rect.Max.y + window->MenuBarHeight() + (((flags & ImGuiWindowFlags_MenuBar) || !(flags & ImGuiWindowFlags_NoTitleBar)) ? style.FrameBorderSize : window->WindowBorderSize); window->InnerMainRect.Max.x = window->Pos.x + window->Size.x - ImMax(window->ScrollbarSizes.x, window->WindowBorderSize); window->InnerMainRect.Max.y = window->Pos.y + window->Size.y - ImMax(window->ScrollbarSizes.y, window->WindowBorderSize); // Inner clipping rectangle // Force round operator last to ensure that e.g. (int)(max.x-min.x) in user's render code produce correct result. window->InnerClipRect.Min.x = ImFloor(0.5f + window->InnerMainRect.Min.x + ImMax(0.0f, ImFloor(window->WindowPadding.x * 0.5f - window->WindowBorderSize))); window->InnerClipRect.Min.y = ImFloor(0.5f + window->InnerMainRect.Min.y); window->InnerClipRect.Max.x = ImFloor(0.5f + window->InnerMainRect.Max.x - ImMax(0.0f, ImFloor(window->WindowPadding.x * 0.5f - window->WindowBorderSize))); window->InnerClipRect.Max.y = ImFloor(0.5f + window->InnerMainRect.Max.y); // Setup drawing context // (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.) window->DC.Indent.x = 0.0f + window->WindowPadding.x - window->Scroll.x; window->DC.GroupOffset.x = 0.0f; window->DC.ColumnsOffset.x = 0.0f; window->DC.CursorStartPos = window->Pos + ImVec2(window->DC.Indent.x + window->DC.ColumnsOffset.x, window->TitleBarHeight() + window->MenuBarHeight() + window->WindowPadding.y - window->Scroll.y); window->DC.CursorPos = window->DC.CursorStartPos; window->DC.CursorPosPrevLine = window->DC.CursorPos; window->DC.CursorMaxPos = window->DC.CursorStartPos; window->DC.CurrentLineSize = window->DC.PrevLineSize = ImVec2(0.0f, 0.0f); window->DC.CurrentLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f; window->DC.NavHideHighlightOneFrame = false; window->DC.NavHasScroll = (GetWindowScrollMaxY(window) > 0.0f); window->DC.NavLayerActiveMask = window->DC.NavLayerActiveMaskNext; window->DC.NavLayerActiveMaskNext = 0x00; window->DC.MenuBarAppending = false; window->DC.ChildWindows.resize(0); window->DC.LayoutType = ImGuiLayoutType_Vertical; window->DC.ParentLayoutType = parent_window ? parent_window->DC.LayoutType : ImGuiLayoutType_Vertical; window->DC.FocusCounterAll = window->DC.FocusCounterTab = -1; window->DC.ItemFlags = parent_window ? parent_window->DC.ItemFlags : ImGuiItemFlags_Default_; window->DC.ItemWidth = window->ItemWidthDefault; window->DC.TextWrapPos = -1.0f; // disabled window->DC.ItemFlagsStack.resize(0); window->DC.ItemWidthStack.resize(0); window->DC.TextWrapPosStack.resize(0); window->DC.CurrentColumns = NULL; window->DC.TreeDepth = 0; window->DC.TreeStoreMayJumpToParentOnPop = 0x00; window->DC.StateStorage = &window->StateStorage; window->DC.GroupStack.resize(0); window->MenuColumns.Update(3, style.ItemSpacing.x, window_just_activated_by_user); if ((flags & ImGuiWindowFlags_ChildWindow) && (window->DC.ItemFlags != parent_window->DC.ItemFlags)) { window->DC.ItemFlags = parent_window->DC.ItemFlags; window->DC.ItemFlagsStack.push_back(window->DC.ItemFlags); } if (window->AutoFitFramesX > 0) window->AutoFitFramesX--; if (window->AutoFitFramesY > 0) window->AutoFitFramesY--; // Apply focus (we need to call FocusWindow() AFTER setting DC.CursorStartPos so our initial navigation reference rectangle can start around there) if (want_focus) { FocusWindow(window); NavInitWindow(window, false); } // Close from platform window if (p_open != NULL && window->Viewport->PlatformRequestClose && window->Viewport != GetMainViewport()) { if (!window->DockIsActive || window->DockTabIsVisible) { window->Viewport->PlatformRequestClose = false; g.NavWindowingToggleLayer = false; // Assume user mapped PlatformRequestClose on ALT-F4 so we disable ALT for menu toggle. False positive not an issue. //IMGUI_DEBUG_LOG("Window '%s' PlatformRequestClose\n", window->Name); *p_open = false; } } // Title bar if (!(flags & ImGuiWindowFlags_NoTitleBar) && !window->DockIsActive) { // Close & collapse button are on layer 1 (same as menus) and don't default focus const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags; window->DC.ItemFlags |= ImGuiItemFlags_NoNavDefaultFocus; window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); // Collapse button if (!(flags & ImGuiWindowFlags_NoCollapse)) if (CollapseButton(window->GetID("#COLLAPSE"), window->Pos, NULL)) window->WantCollapseToggle = true; // Defer collapsing to next frame as we are too far in the Begin() function // Close button if (p_open != NULL) { const float rad = g.FontSize * 0.5f; 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)) *p_open = false; } window->DC.NavLayerCurrent = ImGuiNavLayer_Main; window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); window->DC.ItemFlags = item_flags_backup; // Title bar text (with: horizontal alignment, avoiding collapse/close button, optional "unsaved document" marker) // FIXME: Refactor text alignment facilities along with RenderText helpers, this is too much code.. const char* UNSAVED_DOCUMENT_MARKER = "*"; float marker_size_x = (flags & ImGuiWindowFlags_UnsavedDocument) ? CalcTextSize(UNSAVED_DOCUMENT_MARKER, NULL, false).x : 0.0f; ImVec2 text_size = CalcTextSize(name, NULL, true) + ImVec2(marker_size_x, 0.0f); ImRect text_r = title_bar_rect; float pad_left = (flags & ImGuiWindowFlags_NoCollapse) ? style.FramePadding.x : (style.FramePadding.x + g.FontSize + style.ItemInnerSpacing.x); float pad_right = (p_open == NULL) ? style.FramePadding.x : (style.FramePadding.x + g.FontSize + style.ItemInnerSpacing.x); if (style.WindowTitleAlign.x > 0.0f) pad_right = ImLerp(pad_right, pad_left, style.WindowTitleAlign.x); text_r.Min.x += pad_left; text_r.Max.x -= pad_right; ImRect clip_rect = text_r; 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() RenderTextClipped(text_r.Min, text_r.Max, name, NULL, &text_size, style.WindowTitleAlign, &clip_rect); if (flags & ImGuiWindowFlags_UnsavedDocument) { 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); ImVec2 off = ImVec2(0.0f, (float)(int)(-g.FontSize * 0.25f)); RenderTextClipped(marker_pos + off, text_r.Max + off, UNSAVED_DOCUMENT_MARKER, NULL, NULL, ImVec2(0, style.WindowTitleAlign.y), &clip_rect); } } // Clear hit test shape every frame window->HitTestHoleSize.x = window->HitTestHoleSize.y = 0; // Pressing CTRL+C while holding on a window copy its content to the clipboard // 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. // Maybe we can support CTRL+C on every element? /* if (g.ActiveId == move_id) if (g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_C)) LogToClipboard(); */ if (g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable) { // Docking: Dragging a dockable window (or any of its child) turns it into a drag and drop source. // We need to do this _before_ we overwrite window->DC.LastItemId below because BeginAsDockableDragDropSource() also overwrites it. if ((g.ActiveId == window->MoveId) && (g.IO.ConfigDockingWithShift == g.IO.KeyShift)) if ((window->Flags & ImGuiWindowFlags_NoMove) == 0) if ((window->RootWindow->Flags & (ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDocking)) == 0) BeginAsDockableDragDropSource(window); // Docking: Any dockable window can act as a target. For dock node hosts we call BeginAsDockableDragDropTarget() in DockNodeUpdate() instead. if (g.DragDropActive && !(flags & ImGuiWindowFlags_NoDocking)) if (g.MovingWindow == NULL || g.MovingWindow->RootWindow != window) if ((window == window->RootWindow) && !(window->Flags & ImGuiWindowFlags_DockNodeHost)) BeginAsDockableDragDropTarget(window); } // We fill last item data based on Title Bar/Tab, in order for IsItemHovered() and IsItemActive() to be usable after Begin(). // This is useful to allow creating context menus on title bar only, etc. if (window->DockIsActive) { window->DC.LastItemId = window->ID; window->DC.LastItemStatusFlags = window->DockTabItemStatusFlags; window->DC.LastItemRect = window->DockTabItemRect; } else { window->DC.LastItemId = window->MoveId; window->DC.LastItemStatusFlags = IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0; window->DC.LastItemRect = title_bar_rect; } #ifdef IMGUI_ENABLE_TEST_ENGINE if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) IMGUI_TEST_ENGINE_ITEM_ADD(window->DC.LastItemRect, window->DC.LastItemId); #endif } else { // Append SetCurrentViewport(window, window->Viewport); SetCurrentWindow(window); } if (!(flags & ImGuiWindowFlags_DockNodeHost)) PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true); // 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) if (first_begin_of_the_frame) window->WriteAccessed = false; window->BeginCount++; g.NextWindowData.Clear(); // When we are about to select this tab (which will only be visible on the _next frame_), flag it with a non-zero HiddenFramesCannotSkipItems. // This will have the important effect of actually returning true in Begin() and not setting SkipItems, allowing an earlier submission of the window contents. // This is analogous to regular windows being hidden from one frame. // 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. if (window->DockIsActive && !window->DockTabIsVisible) { if (window->LastFrameJustFocused == g.FrameCount) window->HiddenFramesCannotSkipItems = 1; else window->HiddenFramesCanSkipItems = 1; } if (flags & ImGuiWindowFlags_ChildWindow) { // Child window can be out of sight and have "negative" clip windows. // Mark them as collapsed so commands are skipped earlier (we can't manually collapse them because they have no title bar). IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0 || (window->DockIsActive)); if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) window->HiddenFramesCanSkipItems = 1; // Completely hide along with parent or if parent is collapsed if (parent_window && (parent_window->Collapsed || parent_window->Hidden)) window->HiddenFramesCanSkipItems = 1; } // 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) if (style.Alpha <= 0.0f) window->HiddenFramesCanSkipItems = 1; // Update the Hidden flag window->Hidden = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0); // Update the SkipItems flag, used to early out of all items functions (no layout required) bool skip_items = false; if (window->Collapsed || !window->Active || window->Hidden) if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0 && window->HiddenFramesCannotSkipItems <= 0) skip_items = true; window->SkipItems = skip_items; return !skip_items; } // Old Begin() API with 5 parameters, avoid calling this version directly! Use SetNextWindowSize()/SetNextWindowBgAlpha() + Begin() instead. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS bool ImGui::Begin(const char* name, bool* p_open, const ImVec2& size_first_use, float bg_alpha_override, ImGuiWindowFlags flags) { // 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. if (size_first_use.x != 0.0f || size_first_use.y != 0.0f) SetNextWindowSize(size_first_use, ImGuiCond_FirstUseEver); // Old API feature: override the window background alpha with a parameter. if (bg_alpha_override >= 0.0f) SetNextWindowBgAlpha(bg_alpha_override); return Begin(name, p_open, flags); } #endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS void ImGui::End() { ImGuiContext& g = *GImGui; if (g.CurrentWindowStack.Size <= 1 && g.FrameScopePushedImplicitWindow) { IM_ASSERT(g.CurrentWindowStack.Size > 1 && "Calling End() too many times!"); return; // FIXME-ERRORHANDLING } IM_ASSERT(g.CurrentWindowStack.Size > 0); ImGuiWindow* window = g.CurrentWindow; if (window->DC.CurrentColumns != NULL) EndColumns(); if (!(window->Flags & ImGuiWindowFlags_DockNodeHost)) // Pop inner window clip rectangle PopClipRect(); // Stop logging if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging LogFinish(); // Docking: report contents sizes to parent to allow for auto-resize if (window->DockNode && window->DockTabIsVisible) if (ImGuiWindow* host_window = window->DockNode->HostWindow) // FIXME-DOCK host_window->DC.CursorMaxPos = window->DC.CursorMaxPos + window->WindowPadding - host_window->WindowPadding; // Pop from window stack g.CurrentWindowStack.pop_back(); if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); CheckStacksSize(window, false); SetCurrentWindow(g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back()); if (g.CurrentWindow) SetCurrentViewport(g.CurrentWindow, g.CurrentWindow->Viewport); } void ImGui::BringWindowToFocusFront(ImGuiWindow* window) { ImGuiContext& g = *GImGui; if (g.WindowsFocusOrder.back() == window) return; for (int i = g.WindowsFocusOrder.Size - 2; i >= 0; i--) // We can ignore the front most window if (g.WindowsFocusOrder[i] == window) { memmove(&g.WindowsFocusOrder[i], &g.WindowsFocusOrder[i + 1], (size_t)(g.WindowsFocusOrder.Size - i - 1) * sizeof(ImGuiWindow*)); g.WindowsFocusOrder[g.WindowsFocusOrder.Size - 1] = window; break; } } void ImGui::BringWindowToDisplayFront(ImGuiWindow* window) { ImGuiContext& g = *GImGui; ImGuiWindow* current_front_window = g.Windows.back(); if (current_front_window == window || current_front_window->RootWindow == window) return; for (int i = g.Windows.Size - 2; i >= 0; i--) // We can ignore the front most window if (g.Windows[i] == window) { memmove(&g.Windows[i], &g.Windows[i + 1], (size_t)(g.Windows.Size - i - 1) * sizeof(ImGuiWindow*)); g.Windows[g.Windows.Size - 1] = window; break; } } void ImGui::BringWindowToDisplayBack(ImGuiWindow* window) { ImGuiContext& g = *GImGui; if (g.Windows[0] == window) return; for (int i = 0; i < g.Windows.Size; i++) if (g.Windows[i] == window) { memmove(&g.Windows[1], &g.Windows[0], (size_t)i * sizeof(ImGuiWindow*)); g.Windows[0] = window; break; } } // Moving window to front of display and set focus (which happens to be back of our sorted list) void ImGui::FocusWindow(ImGuiWindow* window) { ImGuiContext& g = *GImGui; if (g.NavWindow != window) { g.NavWindow = window; if (window && g.NavDisableMouseHover) g.NavMousePosDirty = true; g.NavInitRequest = false; g.NavId = window ? window->NavLastIds[0] : 0; // Restore NavId g.NavIdIsAlive = false; g.NavLayer = ImGuiNavLayer_Main; //IMGUI_DEBUG_LOG("FocusWindow(\"%s\")\n", window ? window->Name : NULL); } // Close popups if any ClosePopupsOverWindow(window, false); // Passing NULL allow to disable keyboard focus if (!window) return; window->LastFrameJustFocused = g.FrameCount; // Select in dock node if (window->DockNode && window->DockNode->TabBar) window->DockNode->TabBar->SelectedTabId = window->DockNode->TabBar->NextSelectedTabId = window->ID; // Move the root window to the top of the pile if (window->RootWindow) window = window->RootWindow; // Steal focus on active widgets if (window->Flags & ImGuiWindowFlags_Popup) // FIXME: This statement should be unnecessary. Need further testing before removing it.. if (g.ActiveId != 0 && g.ActiveIdWindow && g.ActiveIdWindow->RootWindow != window) ClearActiveID(); // Bring to front BringWindowToFocusFront(window); if (!(window->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) BringWindowToDisplayFront(window); } void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window) { ImGuiContext& g = *GImGui; int start_idx = g.WindowsFocusOrder.Size - 1; if (under_this_window != NULL) { int under_this_window_idx = FindWindowFocusIndex(under_this_window); if (under_this_window_idx != -1) start_idx = under_this_window_idx - 1; } for (int i = start_idx; i >= 0; i--) { // 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. ImGuiWindow* window = g.WindowsFocusOrder[i]; if (window != ignore_window && window->WasActive && !(window->Flags & ImGuiWindowFlags_ChildWindow)) if ((window->Flags & (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) != (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) { // FIXME-DOCKING: This is failing (lagging by one frame) for docked windows. // If A and B are docked into window and B disappear, at the NewFrame() call site window->NavLastChildNavWindow will still point to B. // 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) // to tell which is the "previous" window. Or we may leverage 'LastFrameFocused/LastFrameJustFocused' and have this function handle child window itself? ImGuiWindow* focus_window = NavRestoreLastChildNavWindow(window); FocusWindow(focus_window); return; } } FocusWindow(NULL); } void ImGui::SetNextItemWidth(float item_width) { ImGuiWindow* window = GetCurrentWindow(); window->DC.NextItemWidth = item_width; } void ImGui::PushItemWidth(float item_width) { ImGuiWindow* window = GetCurrentWindow(); window->DC.ItemWidth = (item_width == 0.0f ? window->ItemWidthDefault : item_width); window->DC.ItemWidthStack.push_back(window->DC.ItemWidth); } void ImGui::PushMultiItemsWidths(int components, float w_full) { ImGuiWindow* window = GetCurrentWindow(); const ImGuiStyle& style = GImGui->Style; const float w_item_one = ImMax(1.0f, (float)(int)((w_full - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); const float w_item_last = ImMax(1.0f, (float)(int)(w_full - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); window->DC.ItemWidthStack.push_back(w_item_last); for (int i = 0; i < components-1; i++) window->DC.ItemWidthStack.push_back(w_item_one); window->DC.ItemWidth = window->DC.ItemWidthStack.back(); } void ImGui::PopItemWidth() { ImGuiWindow* window = GetCurrentWindow(); window->DC.ItemWidthStack.pop_back(); window->DC.ItemWidth = window->DC.ItemWidthStack.empty() ? window->ItemWidthDefault : window->DC.ItemWidthStack.back(); } // Calculate default item width given value passed to PushItemWidth() or SetNextItemWidth(), // Then consume the float ImGui::GetNextItemWidth() { ImGuiWindow* window = GImGui->CurrentWindow; float w; if (window->DC.NextItemWidth != FLT_MAX) { w = window->DC.NextItemWidth; window->DC.NextItemWidth = FLT_MAX; } else { w = window->DC.ItemWidth; } if (w < 0.0f) { float region_max_x = GetContentRegionMaxScreen().x; w = ImMax(1.0f, region_max_x - window->DC.CursorPos.x + w); } w = (float)(int)w; return w; } // Calculate item width *without* popping/consuming NextItemWidth if it was set. // (rarely used, which is why we avoid calling this from GetNextItemWidth() and instead do a backup/restore here) float ImGui::CalcItemWidth() { ImGuiWindow* window = GImGui->CurrentWindow; float backup_next_item_width = window->DC.NextItemWidth; float w = GetNextItemWidth(); window->DC.NextItemWidth = backup_next_item_width; return w; } // [Internal] Calculate full item size given user provided 'size' parameter and default width/height. Default width is often == GetNextItemWidth(). // Those two functions CalcItemWidth vs CalcItemSize are awkwardly named because they are not fully symmetrical. // Note that only CalcItemWidth() is publicly exposed. // The 4.0f here may be changed to match GetNextItemWidth() and/or BeginChild() (right now we have a mismatch which is harmless but undesirable) ImVec2 ImGui::CalcItemSize(ImVec2 size, float default_w, float default_h) { ImGuiWindow* window = GImGui->CurrentWindow; ImVec2 region_max; if (size.x < 0.0f || size.y < 0.0f) region_max = GetContentRegionMaxScreen(); if (size.x == 0.0f) size.x = default_w; else if (size.x < 0.0f) size.x = ImMax(4.0f, region_max.x - window->DC.CursorPos.x + size.x); if (size.y == 0.0f) size.y = default_h; else if (size.y < 0.0f) size.y = ImMax(4.0f, region_max.y - window->DC.CursorPos.y + size.y); return size; } void ImGui::SetCurrentFont(ImFont* font) { ImGuiContext& g = *GImGui; IM_ASSERT(font && font->IsLoaded()); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ? IM_ASSERT(font->Scale > 0.0f); g.Font = font; g.FontBaseSize = ImMax(1.0f, g.IO.FontGlobalScale * g.Font->FontSize * g.Font->Scale); g.FontSize = g.CurrentWindow ? g.CurrentWindow->CalcFontSize() : 0.0f; ImFontAtlas* atlas = g.Font->ContainerAtlas; g.DrawListSharedData.TexUvWhitePixel = atlas->TexUvWhitePixel; g.DrawListSharedData.Font = g.Font; g.DrawListSharedData.FontSize = g.FontSize; } void ImGui::PushFont(ImFont* font) { ImGuiContext& g = *GImGui; if (!font) font = GetDefaultFont(); SetCurrentFont(font); g.FontStack.push_back(font); g.CurrentWindow->DrawList->PushTextureID(font->ContainerAtlas->TexID); } void ImGui::PopFont() { ImGuiContext& g = *GImGui; g.CurrentWindow->DrawList->PopTextureID(); g.FontStack.pop_back(); SetCurrentFont(g.FontStack.empty() ? GetDefaultFont() : g.FontStack.back()); } void ImGui::PushItemFlag(ImGuiItemFlags option, bool enabled) { ImGuiWindow* window = GetCurrentWindow(); if (enabled) window->DC.ItemFlags |= option; else window->DC.ItemFlags &= ~option; window->DC.ItemFlagsStack.push_back(window->DC.ItemFlags); } void ImGui::PopItemFlag() { ImGuiWindow* window = GetCurrentWindow(); window->DC.ItemFlagsStack.pop_back(); window->DC.ItemFlags = window->DC.ItemFlagsStack.empty() ? ImGuiItemFlags_Default_ : window->DC.ItemFlagsStack.back(); } // FIXME: Look into renaming this once we have settled the new Focus/Activation/TabStop system. void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus) { PushItemFlag(ImGuiItemFlags_NoTabStop, !allow_keyboard_focus); } void ImGui::PopAllowKeyboardFocus() { PopItemFlag(); } void ImGui::PushButtonRepeat(bool repeat) { PushItemFlag(ImGuiItemFlags_ButtonRepeat, repeat); } void ImGui::PopButtonRepeat() { PopItemFlag(); } void ImGui::PushTextWrapPos(float wrap_pos_x) { ImGuiWindow* window = GetCurrentWindow(); window->DC.TextWrapPos = wrap_pos_x; window->DC.TextWrapPosStack.push_back(wrap_pos_x); } void ImGui::PopTextWrapPos() { ImGuiWindow* window = GetCurrentWindow(); window->DC.TextWrapPosStack.pop_back(); window->DC.TextWrapPos = window->DC.TextWrapPosStack.empty() ? -1.0f : window->DC.TextWrapPosStack.back(); } // 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 void ImGui::PushStyleColor(ImGuiCol idx, ImU32 col) { ImGuiContext& g = *GImGui; ImGuiColorMod backup; backup.Col = idx; backup.BackupValue = g.Style.Colors[idx]; g.ColorModifiers.push_back(backup); g.Style.Colors[idx] = ColorConvertU32ToFloat4(col); } void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) { ImGuiContext& g = *GImGui; ImGuiColorMod backup; backup.Col = idx; backup.BackupValue = g.Style.Colors[idx]; g.ColorModifiers.push_back(backup); g.Style.Colors[idx] = col; } void ImGui::PopStyleColor(int count) { ImGuiContext& g = *GImGui; while (count > 0) { ImGuiColorMod& backup = g.ColorModifiers.back(); g.Style.Colors[backup.Col] = backup.BackupValue; g.ColorModifiers.pop_back(); count--; } } struct ImGuiStyleVarInfo { ImGuiDataType Type; ImU32 Count; ImU32 Offset; void* GetVarPtr(ImGuiStyle* style) const { return (void*)((unsigned char*)style + Offset); } }; static const ImGuiStyleVarInfo GStyleVarInfo[] = { { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowMinSize) }, // ImGuiStyleVar_WindowMinSize { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowTitleAlign) }, // ImGuiStyleVar_WindowTitleAlign { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildRounding) }, // ImGuiStyleVar_ChildRounding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildBorderSize) }, // ImGuiStyleVar_ChildBorderSize { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupRounding) }, // ImGuiStyleVar_PopupRounding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupBorderSize) }, // ImGuiStyleVar_PopupBorderSize { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, FramePadding) }, // ImGuiStyleVar_FramePadding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameRounding) }, // ImGuiStyleVar_FrameRounding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameBorderSize) }, // ImGuiStyleVar_FrameBorderSize { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemSpacing) }, // ImGuiStyleVar_ItemSpacing { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemInnerSpacing) }, // ImGuiStyleVar_ItemInnerSpacing { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, IndentSpacing) }, // ImGuiStyleVar_IndentSpacing { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign }; static const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx) { IM_ASSERT(idx >= 0 && idx < ImGuiStyleVar_COUNT); IM_ASSERT(IM_ARRAYSIZE(GStyleVarInfo) == ImGuiStyleVar_COUNT); return &GStyleVarInfo[idx]; } void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) { const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1) { ImGuiContext& g = *GImGui; float* pvar = (float*)var_info->GetVarPtr(&g.Style); g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); *pvar = val; return; } IM_ASSERT(0 && "Called PushStyleVar() float variant but variable is not a float!"); } void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) { const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); if (var_info->Type == ImGuiDataType_Float && var_info->Count == 2) { ImGuiContext& g = *GImGui; ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); *pvar = val; return; } IM_ASSERT(0 && "Called PushStyleVar() ImVec2 variant but variable is not a ImVec2!"); } void ImGui::PopStyleVar(int count) { ImGuiContext& g = *GImGui; while (count > 0) { // We avoid a generic memcpy(data, &backup.Backup.., GDataTypeSize[info->Type] * info->Count), the overhead in Debug is not worth it. ImGuiStyleMod& backup = g.StyleModifiers.back(); const ImGuiStyleVarInfo* info = GetStyleVarInfo(backup.VarIdx); void* data = info->GetVarPtr(&g.Style); if (info->Type == ImGuiDataType_Float && info->Count == 1) { ((float*)data)[0] = backup.BackupFloat[0]; } else if (info->Type == ImGuiDataType_Float && info->Count == 2) { ((float*)data)[0] = backup.BackupFloat[0]; ((float*)data)[1] = backup.BackupFloat[1]; } g.StyleModifiers.pop_back(); count--; } } const char* ImGui::GetStyleColorName(ImGuiCol idx) { // Create switch-case from enum with regexp: ImGuiCol_{.*}, --> case ImGuiCol_\1: return "\1"; switch (idx) { case ImGuiCol_Text: return "Text"; case ImGuiCol_TextDisabled: return "TextDisabled"; case ImGuiCol_WindowBg: return "WindowBg"; case ImGuiCol_ChildBg: return "ChildBg"; case ImGuiCol_PopupBg: return "PopupBg"; case ImGuiCol_Border: return "Border"; case ImGuiCol_BorderShadow: return "BorderShadow"; case ImGuiCol_FrameBg: return "FrameBg"; case ImGuiCol_FrameBgHovered: return "FrameBgHovered"; case ImGuiCol_FrameBgActive: return "FrameBgActive"; case ImGuiCol_TitleBg: return "TitleBg"; case ImGuiCol_TitleBgActive: return "TitleBgActive"; case ImGuiCol_TitleBgCollapsed: return "TitleBgCollapsed"; case ImGuiCol_MenuBarBg: return "MenuBarBg"; case ImGuiCol_ScrollbarBg: return "ScrollbarBg"; case ImGuiCol_ScrollbarGrab: return "ScrollbarGrab"; case ImGuiCol_ScrollbarGrabHovered: return "ScrollbarGrabHovered"; case ImGuiCol_ScrollbarGrabActive: return "ScrollbarGrabActive"; case ImGuiCol_CheckMark: return "CheckMark"; case ImGuiCol_SliderGrab: return "SliderGrab"; case ImGuiCol_SliderGrabActive: return "SliderGrabActive"; case ImGuiCol_Button: return "Button"; case ImGuiCol_ButtonHovered: return "ButtonHovered"; case ImGuiCol_ButtonActive: return "ButtonActive"; case ImGuiCol_Header: return "Header"; case ImGuiCol_HeaderHovered: return "HeaderHovered"; case ImGuiCol_HeaderActive: return "HeaderActive"; case ImGuiCol_Separator: return "Separator"; case ImGuiCol_SeparatorHovered: return "SeparatorHovered"; case ImGuiCol_SeparatorActive: return "SeparatorActive"; case ImGuiCol_ResizeGrip: return "ResizeGrip"; case ImGuiCol_ResizeGripHovered: return "ResizeGripHovered"; case ImGuiCol_ResizeGripActive: return "ResizeGripActive"; case ImGuiCol_Tab: return "Tab"; case ImGuiCol_TabHovered: return "TabHovered"; case ImGuiCol_TabActive: return "TabActive"; case ImGuiCol_TabUnfocused: return "TabUnfocused"; case ImGuiCol_TabUnfocusedActive: return "TabUnfocusedActive"; case ImGuiCol_DockingPreview: return "DockingPreview"; case ImGuiCol_DockingEmptyBg: return "DockingEmptyBg"; case ImGuiCol_PlotLines: return "PlotLines"; case ImGuiCol_PlotLinesHovered: return "PlotLinesHovered"; case ImGuiCol_PlotHistogram: return "PlotHistogram"; case ImGuiCol_PlotHistogramHovered: return "PlotHistogramHovered"; case ImGuiCol_TextSelectedBg: return "TextSelectedBg"; case ImGuiCol_DragDropTarget: return "DragDropTarget"; case ImGuiCol_NavHighlight: return "NavHighlight"; case ImGuiCol_NavWindowingHighlight: return "NavWindowingHighlight"; case ImGuiCol_NavWindowingDimBg: return "NavWindowingDimBg"; case ImGuiCol_ModalWindowDimBg: return "ModalWindowDimBg"; } IM_ASSERT(0); return "Unknown"; } bool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent) { if (window->RootWindow == potential_parent) return true; while (window != NULL) { if (window == potential_parent) return true; window = window->ParentWindow; } return false; } bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) { IM_ASSERT((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0); // Flags not supported by this function ImGuiContext& g = *GImGui; if (flags & ImGuiHoveredFlags_AnyWindow) { if (g.HoveredWindow == NULL) return false; } else { switch (flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) { case ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows: if (g.HoveredWindow == NULL || g.HoveredWindow->RootWindowDockStop != g.CurrentWindow->RootWindowDockStop) return false; break; case ImGuiHoveredFlags_RootWindow: if (g.HoveredWindow != g.CurrentWindow->RootWindowDockStop) return false; break; case ImGuiHoveredFlags_ChildWindows: if (g.HoveredWindow == NULL || !IsWindowChildOf(g.HoveredWindow, g.CurrentWindow)) return false; break; default: if (g.HoveredWindow != g.CurrentWindow) return false; break; } } if (!IsWindowContentHoverable(g.HoveredWindow, flags)) return false; if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) if (g.ActiveId != 0 && !g.ActiveIdAllowOverlap && g.ActiveId != g.HoveredWindow->MoveId) return false; return true; } bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags) { ImGuiContext& g = *GImGui; if (flags & ImGuiFocusedFlags_AnyWindow) return g.NavWindow != NULL; IM_ASSERT(g.CurrentWindow); // Not inside a Begin()/End() switch (flags & (ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows)) { case ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows: return g.NavWindow && g.NavWindow->RootWindowDockStop == g.CurrentWindow->RootWindowDockStop; case ImGuiFocusedFlags_RootWindow: return g.NavWindow == g.CurrentWindow->RootWindowDockStop; case ImGuiFocusedFlags_ChildWindows: return g.NavWindow && IsWindowChildOf(g.NavWindow, g.CurrentWindow); default: return g.NavWindow == g.CurrentWindow; } } ImGuiID ImGui::GetWindowDockID() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DockId; } bool ImGui::IsWindowDocked() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DockIsActive; } // Can we focus this window with CTRL+TAB (or PadMenu + PadFocusPrev/PadFocusNext) // Note that NoNavFocus makes the window not reachable with CTRL+TAB but it can still be focused with mouse or programmaticaly. // If you want a window to never be focused, you may use the e.g. NoInputs flag. bool ImGui::IsWindowNavFocusable(ImGuiWindow* window) { return window->Active && window == window->RootWindowDockStop && !(window->Flags & ImGuiWindowFlags_NoNavFocus); } float ImGui::GetWindowWidth() { ImGuiWindow* window = GImGui->CurrentWindow; return window->Size.x; } float ImGui::GetWindowHeight() { ImGuiWindow* window = GImGui->CurrentWindow; return window->Size.y; } ImVec2 ImGui::GetWindowPos() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; return window->Pos; } void ImGui::SetWindowScrollX(ImGuiWindow* window, float new_scroll_x) { 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. window->Scroll.x = new_scroll_x; window->DC.CursorMaxPos.x -= window->Scroll.x; } void ImGui::SetWindowScrollY(ImGuiWindow* window, float new_scroll_y) { 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. window->Scroll.y = new_scroll_y; window->DC.CursorMaxPos.y -= window->Scroll.y; } void ImGui::SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond) { // Test condition (NB: bit 0 is always true) and clear flags for next time if (cond && (window->SetWindowPosAllowFlags & cond) == 0) return; IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. window->SetWindowPosAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); window->SetWindowPosVal = ImVec2(FLT_MAX, FLT_MAX); // Set const ImVec2 old_pos = window->Pos; window->Pos = ImFloor(pos); 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 window->DC.CursorMaxPos += (window->Pos - old_pos); // And more importantly we need to adjust this so size calculation doesn't get affected. } void ImGui::SetWindowPos(const ImVec2& pos, ImGuiCond cond) { ImGuiWindow* window = GetCurrentWindowRead(); SetWindowPos(window, pos, cond); } void ImGui::SetWindowPos(const char* name, const ImVec2& pos, ImGuiCond cond) { if (ImGuiWindow* window = FindWindowByName(name)) SetWindowPos(window, pos, cond); } ImVec2 ImGui::GetWindowSize() { ImGuiWindow* window = GetCurrentWindowRead(); return window->Size; } void ImGui::SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond) { // Test condition (NB: bit 0 is always true) and clear flags for next time if (cond && (window->SetWindowSizeAllowFlags & cond) == 0) return; IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. window->SetWindowSizeAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); // Set if (size.x > 0.0f) { window->AutoFitFramesX = 0; window->SizeFull.x = ImFloor(size.x); } else { window->AutoFitFramesX = 2; window->AutoFitOnlyGrows = false; } if (size.y > 0.0f) { window->AutoFitFramesY = 0; window->SizeFull.y = ImFloor(size.y); } else { window->AutoFitFramesY = 2; window->AutoFitOnlyGrows = false; } } void ImGui::SetWindowSize(const ImVec2& size, ImGuiCond cond) { SetWindowSize(GImGui->CurrentWindow, size, cond); } void ImGui::SetWindowSize(const char* name, const ImVec2& size, ImGuiCond cond) { if (ImGuiWindow* window = FindWindowByName(name)) SetWindowSize(window, size, cond); } void ImGui::SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond) { // Test condition (NB: bit 0 is always true) and clear flags for next time if (cond && (window->SetWindowCollapsedAllowFlags & cond) == 0) return; window->SetWindowCollapsedAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); // Set window->Collapsed = collapsed; } static void SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size) { IM_ASSERT(window->HitTestHoleSize.x == 0); // We don't support multiple holes/hit test filters window->HitTestHoleSize = ImVec2ih((short)size.x, (short)size.y); window->HitTestHoleOffset = ImVec2ih((short)(pos.x - window->Pos.x), (short)(pos.y - window->Pos.y)); } void ImGui::SetWindowCollapsed(bool collapsed, ImGuiCond cond) { SetWindowCollapsed(GImGui->CurrentWindow, collapsed, cond); } bool ImGui::IsWindowCollapsed() { ImGuiWindow* window = GetCurrentWindowRead(); return window->Collapsed; } bool ImGui::IsWindowAppearing() { ImGuiWindow* window = GetCurrentWindowRead(); return window->Appearing; } void ImGui::SetWindowCollapsed(const char* name, bool collapsed, ImGuiCond cond) { if (ImGuiWindow* window = FindWindowByName(name)) SetWindowCollapsed(window, collapsed, cond); } void ImGui::SetWindowFocus() { FocusWindow(GImGui->CurrentWindow); } void ImGui::SetWindowFocus(const char* name) { if (name) { if (ImGuiWindow* window = FindWindowByName(name)) FocusWindow(window); } else { FocusWindow(NULL); } } void ImGui::SetNextWindowPos(const ImVec2& pos, ImGuiCond cond, const ImVec2& pivot) { ImGuiContext& g = *GImGui; IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. g.NextWindowData.PosVal = pos; g.NextWindowData.PosPivotVal = pivot; g.NextWindowData.PosCond = cond ? cond : ImGuiCond_Always; g.NextWindowData.PosUndock = true; } #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS void ImGui::SetNextWindowPosCenter(ImGuiCond cond) { ImGuiViewport* viewport = ImGui::GetMainViewport(); SetNextWindowPos(viewport->Pos + viewport->Size * 0.5f, cond, ImVec2(0.5f, 0.5f)); SetNextWindowViewport(viewport->ID); } #endif void ImGui::SetNextWindowSize(const ImVec2& size, ImGuiCond cond) { ImGuiContext& g = *GImGui; IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. g.NextWindowData.SizeVal = size; g.NextWindowData.SizeCond = cond ? cond : ImGuiCond_Always; } void ImGui::SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback, void* custom_callback_user_data) { ImGuiContext& g = *GImGui; g.NextWindowData.SizeConstraintCond = ImGuiCond_Always; g.NextWindowData.SizeConstraintRect = ImRect(size_min, size_max); g.NextWindowData.SizeCallback = custom_callback; g.NextWindowData.SizeCallbackUserData = custom_callback_user_data; } void ImGui::SetNextWindowContentSize(const ImVec2& size) { ImGuiContext& g = *GImGui; 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. g.NextWindowData.ContentSizeCond = ImGuiCond_Always; } void ImGui::SetNextWindowCollapsed(bool collapsed, ImGuiCond cond) { ImGuiContext& g = *GImGui; IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. g.NextWindowData.CollapsedVal = collapsed; g.NextWindowData.CollapsedCond = cond ? cond : ImGuiCond_Always; } void ImGui::SetNextWindowFocus() { ImGuiContext& g = *GImGui; g.NextWindowData.FocusCond = ImGuiCond_Always; // Using a Cond member for consistency (may transition all of them to single flag set for fast Clear() op) } void ImGui::SetNextWindowBgAlpha(float alpha) { ImGuiContext& g = *GImGui; g.NextWindowData.BgAlphaVal = alpha; g.NextWindowData.BgAlphaCond = ImGuiCond_Always; // Using a Cond member for consistency (may transition all of them to single flag set for fast Clear() op) } void ImGui::SetNextWindowViewport(ImGuiID id) { ImGuiContext& g = *GImGui; g.NextWindowData.ViewportCond = ImGuiCond_Always; g.NextWindowData.ViewportId = id; } void ImGui::SetNextWindowDockID(ImGuiID id, ImGuiCond cond) { ImGuiContext& g = *GImGui; g.NextWindowData.DockCond = cond ? cond : ImGuiCond_Always; g.NextWindowData.DockId = id; } void ImGui::SetNextWindowClass(const ImGuiWindowClass* window_class) { ImGuiContext& g = *GImGui; g.NextWindowData.WindowClass = *window_class; } // FIXME: This is in window space (not screen space!). We should try to obsolete all those functions. ImVec2 ImGui::GetContentRegionMax() { ImGuiWindow* window = GImGui->CurrentWindow; ImVec2 mx = window->ContentsRegionRect.Max - window->Pos; if (window->DC.CurrentColumns) mx.x = GetColumnOffset(window->DC.CurrentColumns->Current + 1) - window->WindowPadding.x; return mx; } // [Internal] Absolute coordinate. Saner. This is not exposed until we finishing refactoring work rect features. ImVec2 ImGui::GetContentRegionMaxScreen() { ImGuiWindow* window = GImGui->CurrentWindow; ImVec2 mx = window->ContentsRegionRect.Max; if (window->DC.CurrentColumns) mx.x = window->Pos.x + GetColumnOffset(window->DC.CurrentColumns->Current + 1) - window->WindowPadding.x; return mx; } ImVec2 ImGui::GetContentRegionAvail() { ImGuiWindow* window = GImGui->CurrentWindow; return GetContentRegionMaxScreen() - window->DC.CursorPos; } float ImGui::GetContentRegionAvailWidth() { return GetContentRegionAvail().x; } // In window space (not screen space!) ImVec2 ImGui::GetWindowContentRegionMin() { ImGuiWindow* window = GetCurrentWindowRead(); return window->ContentsRegionRect.Min - window->Pos; } ImVec2 ImGui::GetWindowContentRegionMax() { ImGuiWindow* window = GetCurrentWindowRead(); return window->ContentsRegionRect.Max - window->Pos; } float ImGui::GetWindowContentRegionWidth() { ImGuiWindow* window = GetCurrentWindowRead(); return window->ContentsRegionRect.GetWidth(); } float ImGui::GetTextLineHeight() { ImGuiContext& g = *GImGui; return g.FontSize; } float ImGui::GetTextLineHeightWithSpacing() { ImGuiContext& g = *GImGui; return g.FontSize + g.Style.ItemSpacing.y; } float ImGui::GetFrameHeight() { ImGuiContext& g = *GImGui; return g.FontSize + g.Style.FramePadding.y * 2.0f; } float ImGui::GetFrameHeightWithSpacing() { ImGuiContext& g = *GImGui; return g.FontSize + g.Style.FramePadding.y * 2.0f + g.Style.ItemSpacing.y; } ImDrawList* ImGui::GetWindowDrawList() { ImGuiWindow* window = GetCurrentWindow(); return window->DrawList; } float ImGui::GetWindowDpiScale() { ImGuiContext& g = *GImGui; IM_ASSERT(g.CurrentViewport != NULL); return g.CurrentViewport->DpiScale; } ImGuiViewport* ImGui::GetWindowViewport() { ImGuiContext& g = *GImGui; IM_ASSERT(g.CurrentViewport != NULL && g.CurrentViewport == g.CurrentWindow->Viewport); return g.CurrentViewport; } ImFont* ImGui::GetFont() { return GImGui->Font; } float ImGui::GetFontSize() { return GImGui->FontSize; } ImVec2 ImGui::GetFontTexUvWhitePixel() { return GImGui->DrawListSharedData.TexUvWhitePixel; } void ImGui::SetWindowFontScale(float scale) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); window->FontWindowScale = scale; g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); } // User generally sees positions in window coordinates. Internally we store CursorPos in absolute screen coordinates because it is more convenient. // 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'. ImVec2 ImGui::GetCursorPos() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.CursorPos - window->Pos + window->Scroll; } float ImGui::GetCursorPosX() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.CursorPos.x - window->Pos.x + window->Scroll.x; } float ImGui::GetCursorPosY() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.CursorPos.y - window->Pos.y + window->Scroll.y; } void ImGui::SetCursorPos(const ImVec2& local_pos) { ImGuiWindow* window = GetCurrentWindow(); window->DC.CursorPos = window->Pos - window->Scroll + local_pos; window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); } void ImGui::SetCursorPosX(float x) { ImGuiWindow* window = GetCurrentWindow(); window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + x; window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x); } void ImGui::SetCursorPosY(float y) { ImGuiWindow* window = GetCurrentWindow(); window->DC.CursorPos.y = window->Pos.y - window->Scroll.y + y; window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y); } ImVec2 ImGui::GetCursorStartPos() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.CursorStartPos - window->Pos; } ImVec2 ImGui::GetCursorScreenPos() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.CursorPos; } void ImGui::SetCursorScreenPos(const ImVec2& pos) { ImGuiWindow* window = GetCurrentWindow(); window->DC.CursorPos = pos; window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); } float ImGui::GetScrollX() { return GImGui->CurrentWindow->Scroll.x; } float ImGui::GetScrollY() { return GImGui->CurrentWindow->Scroll.y; } float ImGui::GetScrollMaxX() { return GetWindowScrollMaxX(GImGui->CurrentWindow); } float ImGui::GetScrollMaxY() { return GetWindowScrollMaxY(GImGui->CurrentWindow); } void ImGui::SetScrollX(float scroll_x) { ImGuiWindow* window = GetCurrentWindow(); window->ScrollTarget.x = scroll_x; window->ScrollTargetCenterRatio.x = 0.0f; } void ImGui::SetScrollY(float scroll_y) { ImGuiWindow* window = GetCurrentWindow(); window->ScrollTarget.y = scroll_y + window->TitleBarHeight() + window->MenuBarHeight(); // title bar height canceled out when using ScrollTargetRelY window->ScrollTargetCenterRatio.y = 0.0f; } void ImGui::SetScrollFromPosY(float local_y, float center_y_ratio) { // We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size ImGuiWindow* window = GetCurrentWindow(); IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f); window->ScrollTarget.y = (float)(int)(local_y + window->Scroll.y); window->ScrollTargetCenterRatio.y = center_y_ratio; } // center_y_ratio: 0.0f top of last item, 0.5f vertical center of last item, 1.0f bottom of last item. void ImGui::SetScrollHereY(float center_y_ratio) { ImGuiWindow* window = GetCurrentWindow(); float target_y = window->DC.CursorPosPrevLine.y - window->Pos.y; // Top of last item, in window space 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. SetScrollFromPosY(target_y, center_y_ratio); } void ImGui::ActivateItem(ImGuiID id) { ImGuiContext& g = *GImGui; g.NavNextActivateId = id; } void ImGui::SetKeyboardFocusHere(int offset) { IM_ASSERT(offset >= -1); // -1 is allowed but not below ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; g.FocusRequestNextWindow = window; g.FocusRequestNextCounterAll = window->DC.FocusCounterAll + 1 + offset; g.FocusRequestNextCounterTab = INT_MAX; } void ImGui::SetItemDefaultFocus() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (!window->Appearing) return; if (g.NavWindow == window->RootWindowForNav && (g.NavInitRequest || g.NavInitResultId != 0) && g.NavLayer == g.NavWindow->DC.NavLayerCurrent) { g.NavInitRequest = false; g.NavInitResultId = g.NavWindow->DC.LastItemId; g.NavInitResultRectRel = ImRect(g.NavWindow->DC.LastItemRect.Min - g.NavWindow->Pos, g.NavWindow->DC.LastItemRect.Max - g.NavWindow->Pos); NavUpdateAnyRequestFlag(); if (!IsItemVisible()) SetScrollHereY(); } } void ImGui::SetStateStorage(ImGuiStorage* tree) { ImGuiWindow* window = GImGui->CurrentWindow; window->DC.StateStorage = tree ? tree : &window->StateStorage; } ImGuiStorage* ImGui::GetStateStorage() { ImGuiWindow* window = GImGui->CurrentWindow; return window->DC.StateStorage; } void ImGui::PushID(const char* str_id) { ImGuiWindow* window = GImGui->CurrentWindow; window->IDStack.push_back(window->GetIDNoKeepAlive(str_id)); } void ImGui::PushID(const char* str_id_begin, const char* str_id_end) { ImGuiWindow* window = GImGui->CurrentWindow; window->IDStack.push_back(window->GetIDNoKeepAlive(str_id_begin, str_id_end)); } void ImGui::PushID(const void* ptr_id) { ImGuiWindow* window = GImGui->CurrentWindow; window->IDStack.push_back(window->GetIDNoKeepAlive(ptr_id)); } void ImGui::PushID(int int_id) { const void* ptr_id = (void*)(intptr_t)int_id; ImGuiWindow* window = GImGui->CurrentWindow; window->IDStack.push_back(window->GetIDNoKeepAlive(ptr_id)); } // Push a given id value ignoring the ID stack as a seed. void ImGui::PushOverrideID(ImGuiID id) { ImGuiWindow* window = GImGui->CurrentWindow; window->IDStack.push_back(id); } void ImGui::PopID() { ImGuiWindow* window = GImGui->CurrentWindow; window->IDStack.pop_back(); } ImGuiID ImGui::GetID(const char* str_id) { ImGuiWindow* window = GImGui->CurrentWindow; return window->GetID(str_id); } ImGuiID ImGui::GetID(const char* str_id_begin, const char* str_id_end) { ImGuiWindow* window = GImGui->CurrentWindow; return window->GetID(str_id_begin, str_id_end); } ImGuiID ImGui::GetID(const void* ptr_id) { ImGuiWindow* window = GImGui->CurrentWindow; return window->GetID(ptr_id); } bool ImGui::IsRectVisible(const ImVec2& size) { ImGuiWindow* window = GImGui->CurrentWindow; return window->ClipRect.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size)); } bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) { ImGuiWindow* window = GImGui->CurrentWindow; return window->ClipRect.Overlaps(ImRect(rect_min, rect_max)); } // 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.) void ImGui::BeginGroup() { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); window->DC.GroupStack.resize(window->DC.GroupStack.Size + 1); ImGuiGroupData& group_data = window->DC.GroupStack.back(); group_data.BackupCursorPos = window->DC.CursorPos; group_data.BackupCursorMaxPos = window->DC.CursorMaxPos; group_data.BackupIndent = window->DC.Indent; group_data.BackupGroupOffset = window->DC.GroupOffset; group_data.BackupCurrentLineSize = window->DC.CurrentLineSize; group_data.BackupCurrentLineTextBaseOffset = window->DC.CurrentLineTextBaseOffset; group_data.BackupActiveIdIsAlive = g.ActiveIdIsAlive; group_data.BackupActiveIdPreviousFrameIsAlive = g.ActiveIdPreviousFrameIsAlive; group_data.AdvanceCursor = true; window->DC.GroupOffset.x = window->DC.CursorPos.x - window->Pos.x - window->DC.ColumnsOffset.x; window->DC.Indent = window->DC.GroupOffset; window->DC.CursorMaxPos = window->DC.CursorPos; window->DC.CurrentLineSize = ImVec2(0.0f, 0.0f); if (g.LogEnabled) g.LogLinePosY = -FLT_MAX; // To enforce Log carriage return } void ImGui::EndGroup() { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); IM_ASSERT(!window->DC.GroupStack.empty()); // Mismatched BeginGroup()/EndGroup() calls ImGuiGroupData& group_data = window->DC.GroupStack.back(); ImRect group_bb(group_data.BackupCursorPos, window->DC.CursorMaxPos); group_bb.Max = ImMax(group_bb.Min, group_bb.Max); window->DC.CursorPos = group_data.BackupCursorPos; window->DC.CursorMaxPos = ImMax(group_data.BackupCursorMaxPos, window->DC.CursorMaxPos); window->DC.Indent = group_data.BackupIndent; window->DC.GroupOffset = group_data.BackupGroupOffset; window->DC.CurrentLineSize = group_data.BackupCurrentLineSize; window->DC.CurrentLineTextBaseOffset = group_data.BackupCurrentLineTextBaseOffset; if (g.LogEnabled) g.LogLinePosY = -FLT_MAX; // To enforce Log carriage return if (group_data.AdvanceCursor) { 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. ItemSize(group_bb.GetSize(), 0.0f); ItemAdd(group_bb, 0); } // 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. // 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. // (and if you grep for LastItemId you'll notice it is only used in that context. if ((group_data.BackupActiveIdIsAlive != g.ActiveId) && (g.ActiveIdIsAlive == g.ActiveId) && g.ActiveId) // && g.ActiveIdWindow->RootWindow == window->RootWindow) window->DC.LastItemId = g.ActiveId; else if (!group_data.BackupActiveIdPreviousFrameIsAlive && g.ActiveIdPreviousFrameIsAlive) // && g.ActiveIdPreviousFrameWindow->RootWindow == window->RootWindow) window->DC.LastItemId = g.ActiveIdPreviousFrame; window->DC.LastItemRect = group_bb; window->DC.GroupStack.pop_back(); //window->DrawList->AddRect(group_bb.Min, group_bb.Max, IM_COL32(255,0,255,255)); // [Debug] } // Gets back to previous line and continue with horizontal layout // offset_from_start_x == 0 : follow right after previous item // offset_from_start_x != 0 : align to specified x position (relative to window/group left) // spacing_w < 0 : use default spacing if pos_x == 0, no spacing if pos_x != 0 // spacing_w >= 0 : enforce spacing amount void ImGui::SameLine(float offset_from_start_x, float spacing_w) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; if (offset_from_start_x != 0.0f) { if (spacing_w < 0.0f) spacing_w = 0.0f; window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + offset_from_start_x + spacing_w + window->DC.GroupOffset.x + window->DC.ColumnsOffset.x; window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; } else { if (spacing_w < 0.0f) spacing_w = g.Style.ItemSpacing.x; window->DC.CursorPos.x = window->DC.CursorPosPrevLine.x + spacing_w; window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; } window->DC.CurrentLineSize = window->DC.PrevLineSize; window->DC.CurrentLineTextBaseOffset = window->DC.PrevLineTextBaseOffset; } void ImGui::Indent(float indent_w) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); window->DC.Indent.x += (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing; window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x; } void ImGui::Unindent(float indent_w) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); window->DC.Indent.x -= (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing; window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x; } //----------------------------------------------------------------------------- // [SECTION] TOOLTIPS //----------------------------------------------------------------------------- void ImGui::BeginTooltip() { ImGuiContext& g = *GImGui; if (g.DragDropWithinSourceOrTarget) { // 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) // In the context of a dragging tooltip we try to reduce that offset and we enforce following the cursor. // 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. //ImVec2 tooltip_pos = g.IO.MousePos - g.ActiveIdClickOffset - g.Style.WindowPadding; ImVec2 tooltip_pos = g.IO.MousePos + ImVec2(16 * g.Style.MouseCursorScale, 8 * g.Style.MouseCursorScale); SetNextWindowPos(tooltip_pos); SetNextWindowBgAlpha(g.Style.Colors[ImGuiCol_PopupBg].w * 0.60f); //PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * 0.60f); // This would be nice but e.g ColorButton with checkboard has issue with transparent colors :( BeginTooltipEx(0, true); } else { BeginTooltipEx(0, false); } } // Not exposed publicly as BeginTooltip() because bool parameters are evil. Let's see if other needs arise first. void ImGui::BeginTooltipEx(ImGuiWindowFlags extra_flags, bool override_previous_tooltip) { ImGuiContext& g = *GImGui; char window_name[16]; ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", g.TooltipOverrideCount); if (override_previous_tooltip) if (ImGuiWindow* window = FindWindowByName(window_name)) if (window->Active) { // Hide previous tooltip from being displayed. We can't easily "reset" the content of a window so we create a new one. window->Hidden = true; window->HiddenFramesCanSkipItems = 1; ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", ++g.TooltipOverrideCount); } ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip|ImGuiWindowFlags_NoInputs|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoDocking; Begin(window_name, NULL, flags | extra_flags); } void ImGui::EndTooltip() { IM_ASSERT(GetCurrentWindowRead()->Flags & ImGuiWindowFlags_Tooltip); // Mismatched BeginTooltip()/EndTooltip() calls End(); } void ImGui::SetTooltipV(const char* fmt, va_list args) { ImGuiContext& g = *GImGui; if (g.DragDropWithinSourceOrTarget) BeginTooltip(); else BeginTooltipEx(0, true); TextV(fmt, args); EndTooltip(); } void ImGui::SetTooltip(const char* fmt, ...) { va_list args; va_start(args, fmt); SetTooltipV(fmt, args); va_end(args); } //----------------------------------------------------------------------------- // [SECTION] POPUPS //----------------------------------------------------------------------------- bool ImGui::IsPopupOpen(ImGuiID id) { ImGuiContext& g = *GImGui; return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == id; } bool ImGui::IsPopupOpen(const char* str_id) { ImGuiContext& g = *GImGui; return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == g.CurrentWindow->GetID(str_id); } ImGuiWindow* ImGui::GetFrontMostPopupModal() { ImGuiContext& g = *GImGui; for (int n = g.OpenPopupStack.Size-1; n >= 0; n--) if (ImGuiWindow* popup = g.OpenPopupStack.Data[n].Window) if (popup->Flags & ImGuiWindowFlags_Modal) return popup; return NULL; } void ImGui::OpenPopup(const char* str_id) { ImGuiContext& g = *GImGui; OpenPopupEx(g.CurrentWindow->GetID(str_id)); } // Mark popup as open (toggle toward open state). // Popups are closed when user click outside, or activate a pressable item, or CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. // Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). // One open popup per level of the popup hierarchy (NB: when assigning we reset the Window member of ImGuiPopupRef to NULL) void ImGui::OpenPopupEx(ImGuiID id) { ImGuiContext& g = *GImGui; ImGuiWindow* parent_window = g.CurrentWindow; int current_stack_size = g.BeginPopupStack.Size; ImGuiPopupData popup_ref; // Tagged as new ref as Window will be set back to NULL if we write this into OpenPopupStack. popup_ref.PopupId = id; popup_ref.Window = NULL; popup_ref.SourceWindow = g.NavWindow; popup_ref.OpenFrameCount = g.FrameCount; popup_ref.OpenParentId = parent_window->IDStack.back(); popup_ref.OpenPopupPos = NavCalcPreferredRefPos(); popup_ref.OpenMousePos = IsMousePosValid(&g.IO.MousePos) ? g.IO.MousePos : popup_ref.OpenPopupPos; //IMGUI_DEBUG_LOG("OpenPopupEx(0x%08X)\n", g.FrameCount, id); if (g.OpenPopupStack.Size < current_stack_size + 1) { g.OpenPopupStack.push_back(popup_ref); } else { // 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 // 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 // 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. if (g.OpenPopupStack[current_stack_size].PopupId == id && g.OpenPopupStack[current_stack_size].OpenFrameCount == g.FrameCount - 1) { g.OpenPopupStack[current_stack_size].OpenFrameCount = popup_ref.OpenFrameCount; } else { // Close child popups if any, then flag popup for open/reopen g.OpenPopupStack.resize(current_stack_size + 1); g.OpenPopupStack[current_stack_size] = popup_ref; } // When reopening a popup we first refocus its parent, otherwise if its parent is itself a popup it would get closed by ClosePopupsOverWindow(). // This is equivalent to what ClosePopupToLevel() does. //if (g.OpenPopupStack[current_stack_size].PopupId == id) // FocusWindow(parent_window); } } bool ImGui::OpenPopupOnItemClick(const char* str_id, int mouse_button) { ImGuiWindow* window = GImGui->CurrentWindow; if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) { 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! IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) OpenPopupEx(id); return true; } return false; } void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup) { ImGuiContext& g = *GImGui; if (g.OpenPopupStack.empty()) return; // When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it. // Don't close our own child popup windows. int popup_count_to_keep = 0; if (ref_window) { // Find the highest popup which is a descendant of the reference window (generally reference window = NavWindow) for (; popup_count_to_keep < g.OpenPopupStack.Size; popup_count_to_keep++) { ImGuiPopupData& popup = g.OpenPopupStack[popup_count_to_keep]; if (!popup.Window) continue; IM_ASSERT((popup.Window->Flags & ImGuiWindowFlags_Popup) != 0); if (popup.Window->Flags & ImGuiWindowFlags_ChildWindow) continue; // Trim the stack when popups are not direct descendant of the reference window (the reference window is often the NavWindow) bool popup_or_descendent_is_ref_window = false; for (int m = popup_count_to_keep; m < g.OpenPopupStack.Size && !popup_or_descendent_is_ref_window; m++) if (ImGuiWindow* popup_window = g.OpenPopupStack[m].Window) if (popup_window->RootWindow == ref_window->RootWindow) popup_or_descendent_is_ref_window = true; if (!popup_or_descendent_is_ref_window) break; } } 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 { //IMGUI_DEBUG_LOG("ClosePopupsOverWindow(%s) -> ClosePopupToLevel(%d)\n", ref_window->Name, popup_count_to_keep); ClosePopupToLevel(popup_count_to_keep, restore_focus_to_window_under_popup); } } void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup) { ImGuiContext& g = *GImGui; IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size); ImGuiWindow* focus_window = g.OpenPopupStack[remaining].SourceWindow; ImGuiWindow* popup_window = g.OpenPopupStack[remaining].Window; g.OpenPopupStack.resize(remaining); if (restore_focus_to_window_under_popup) { if (focus_window && !focus_window->WasActive && popup_window) { // Fallback FocusTopMostWindowUnderOne(popup_window, NULL); } else { if (g.NavLayer == 0 && focus_window) focus_window = NavRestoreLastChildNavWindow(focus_window); FocusWindow(focus_window); } } } // Close the popup we have begin-ed into. void ImGui::CloseCurrentPopup() { ImGuiContext& g = *GImGui; int popup_idx = g.BeginPopupStack.Size - 1; if (popup_idx < 0 || popup_idx >= g.OpenPopupStack.Size || g.BeginPopupStack[popup_idx].PopupId != g.OpenPopupStack[popup_idx].PopupId) return; // Closing a menu closes its top-most parent popup (unless a modal) while (popup_idx > 0) { ImGuiWindow* popup_window = g.OpenPopupStack[popup_idx].Window; ImGuiWindow* parent_popup_window = g.OpenPopupStack[popup_idx - 1].Window; bool close_parent = false; if (popup_window && (popup_window->Flags & ImGuiWindowFlags_ChildMenu)) if (parent_popup_window == NULL || !(parent_popup_window->Flags & ImGuiWindowFlags_Modal)) close_parent = true; if (!close_parent) break; popup_idx--; } //IMGUI_DEBUG_LOG("CloseCurrentPopup %d -> %d\n", g.BeginPopupStack.Size - 1, popup_idx); ClosePopupToLevel(popup_idx, true); // A common pattern is to close a popup when selecting a menu item/selectable that will open another window. // To improve this usage pattern, we avoid nav highlight for a single frame in the parent window. // Similarly, we could avoid mouse hover highlight in this window but it is less visually problematic. if (ImGuiWindow* window = g.NavWindow) window->DC.NavHideHighlightOneFrame = true; } bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags) { ImGuiContext& g = *GImGui; if (!IsPopupOpen(id)) { g.NextWindowData.Clear(); // We behave like Begin() and need to consume those values return false; } char name[20]; if (extra_flags & ImGuiWindowFlags_ChildMenu) ImFormatString(name, IM_ARRAYSIZE(name), "##Menu_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth else ImFormatString(name, IM_ARRAYSIZE(name), "##Popup_%08x", id); // Not recycling, so we can close/open during the same frame bool is_open = Begin(name, NULL, extra_flags | ImGuiWindowFlags_Popup); if (!is_open) // NB: Begin can return false when the popup is completely clipped (e.g. zero size display) EndPopup(); return is_open; } bool ImGui::BeginPopup(const char* str_id, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; if (g.OpenPopupStack.Size <= g.BeginPopupStack.Size) // Early out for performance { g.NextWindowData.Clear(); // We behave like Begin() and need to consume those values return false; } flags |= ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoDocking; return BeginPopupEx(g.CurrentWindow->GetID(str_id), flags); } // If 'p_open' is specified for a modal popup window, the popup will have a regular close button which will close the popup. // 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. bool ImGui::BeginPopupModal(const char* name, bool* p_open, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; const ImGuiID id = window->GetID(name); if (!IsPopupOpen(id)) { g.NextWindowData.Clear(); // We behave like Begin() and need to consume those values return false; } // Center modal windows by default // FIXME: Should test for (PosCond & window->SetWindowPosAllowFlags) with the upcoming window. if (g.NextWindowData.PosCond == 0) SetNextWindowPos(window->Viewport->GetCenter(), ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); flags |= ImGuiWindowFlags_Popup | ImGuiWindowFlags_Modal | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoDocking; const bool is_open = Begin(name, p_open, flags); if (!is_open || (p_open && !*p_open)) // NB: is_open can be 'false' when the popup is completely clipped (e.g. zero size display) { EndPopup(); if (is_open) ClosePopupToLevel(g.BeginPopupStack.Size, true); return false; } return is_open; } void ImGui::EndPopup() { ImGuiContext& g = *GImGui; IM_ASSERT(g.CurrentWindow->Flags & ImGuiWindowFlags_Popup); // Mismatched BeginPopup()/EndPopup() calls IM_ASSERT(g.BeginPopupStack.Size > 0); // Make all menus and popups wrap around for now, may need to expose that policy. NavMoveRequestTryWrapping(g.CurrentWindow, ImGuiNavMoveFlags_LoopY); End(); } // This is a helper to handle the simplest case of associating one named popup to one given widget. // You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). // You can pass a NULL str_id to use the identifier of the last item. bool ImGui::BeginPopupContextItem(const char* str_id, int mouse_button) { ImGuiWindow* window = GImGui->CurrentWindow; 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! IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) OpenPopupEx(id); return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); } bool ImGui::BeginPopupContextWindow(const char* str_id, int mouse_button, bool also_over_items) { if (!str_id) str_id = "window_context"; ImGuiID id = GImGui->CurrentWindow->GetID(str_id); if (IsMouseReleased(mouse_button) && IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) if (also_over_items || !IsAnyItemHovered()) OpenPopupEx(id); return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); } bool ImGui::BeginPopupContextVoid(const char* str_id, int mouse_button) { if (!str_id) str_id = "void_context"; ImGuiID id = GImGui->CurrentWindow->GetID(str_id); if (IsMouseReleased(mouse_button) && !IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) OpenPopupEx(id); return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); } // 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.) // 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. // (r_outer is usually equivalent to the viewport rectangle minus padding, but when multi-viewports are enabled and monitor // information are available, it may represent the entire platform monitor from the frame of reference of the current viewport. // this allows us to have tooltips/popups displayed out of the parent viewport.) ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy) { ImVec2 base_pos_clamped = ImClamp(ref_pos, r_outer.Min, r_outer.Max - size); //GetForegroundDrawList()->AddRect(r_avoid.Min, r_avoid.Max, IM_COL32(255,0,0,255)); //GetForegroundDrawList()->AddRect(r_outer.Min, r_outer.Max, IM_COL32(0,255,0,255)); // Combo Box policy (we want a connecting edge) if (policy == ImGuiPopupPositionPolicy_ComboBox) { const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Down, ImGuiDir_Right, ImGuiDir_Left, ImGuiDir_Up }; for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++) { const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n]; if (n != -1 && dir == *last_dir) // Already tried this direction? continue; ImVec2 pos; if (dir == ImGuiDir_Down) pos = ImVec2(r_avoid.Min.x, r_avoid.Max.y); // Below, Toward Right (default) if (dir == ImGuiDir_Right) pos = ImVec2(r_avoid.Min.x, r_avoid.Min.y - size.y); // Above, Toward Right if (dir == ImGuiDir_Left) pos = ImVec2(r_avoid.Max.x - size.x, r_avoid.Max.y); // Below, Toward Left if (dir == ImGuiDir_Up) pos = ImVec2(r_avoid.Max.x - size.x, r_avoid.Min.y - size.y); // Above, Toward Left if (!r_outer.Contains(ImRect(pos, pos + size))) continue; *last_dir = dir; return pos; } } // Default popup policy const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Right, ImGuiDir_Down, ImGuiDir_Up, ImGuiDir_Left }; for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++) { const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n]; if (n != -1 && dir == *last_dir) // Already tried this direction? continue; 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); 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); if (avail_w < size.x || avail_h < size.y) continue; ImVec2 pos; pos.x = (dir == ImGuiDir_Left) ? r_avoid.Min.x - size.x : (dir == ImGuiDir_Right) ? r_avoid.Max.x : base_pos_clamped.x; pos.y = (dir == ImGuiDir_Up) ? r_avoid.Min.y - size.y : (dir == ImGuiDir_Down) ? r_avoid.Max.y : base_pos_clamped.y; *last_dir = dir; return pos; } // Fallback, try to keep within display *last_dir = ImGuiDir_None; ImVec2 pos = ref_pos; pos.x = ImMax(ImMin(pos.x + size.x, r_outer.Max.x) - size.x, r_outer.Min.x); pos.y = ImMax(ImMin(pos.y + size.y, r_outer.Max.y) - size.y, r_outer.Min.y); return pos; } ImRect ImGui::GetWindowAllowedExtentRect(ImGuiWindow* window) { ImGuiContext& g = *GImGui; ImRect r_screen; if (window->ViewportAllowPlatformMonitorExtend >= 0) { // Extent with be in the frame of reference of the given viewport (so Min is likely to be negative here) const ImGuiPlatformMonitor& monitor = g.PlatformIO.Monitors[window->ViewportAllowPlatformMonitorExtend]; r_screen.Min = monitor.WorkPos; r_screen.Max = monitor.WorkPos + monitor.WorkSize; } else { r_screen.Min = window->Viewport->Pos; r_screen.Max = window->Viewport->Pos + window->Viewport->Size; } ImVec2 padding = g.Style.DisplaySafeAreaPadding; r_screen.Expand(ImVec2((r_screen.GetWidth() > padding.x * 2) ? -padding.x : 0.0f, (r_screen.GetHeight() > padding.y * 2) ? -padding.y : 0.0f)); return r_screen; } ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) { ImGuiContext& g = *GImGui; if (window->Flags & ImGuiWindowFlags_ChildMenu) { // Child menus typically request _any_ position within the parent menu item, and then we move the new menu outside the parent bounds. // This is how we end up with child menus appearing (most-commonly) on the right of the parent menu. ImGuiWindow* parent_window = window->ParentWindow; 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). ImRect r_outer = GetWindowAllowedExtentRect(window); ImRect r_avoid; if (parent_window->DC.MenuBarAppending) r_avoid = ImRect(-FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight(), FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight() + parent_window->MenuBarHeight()); else 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); return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); } if (window->Flags & ImGuiWindowFlags_Popup) { ImRect r_outer = GetWindowAllowedExtentRect(window); ImRect r_avoid = ImRect(window->Pos.x - 1, window->Pos.y - 1, window->Pos.x + 1, window->Pos.y + 1); return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); } if (window->Flags & ImGuiWindowFlags_Tooltip) { // Position tooltip (always follows mouse) float sc = g.Style.MouseCursorScale; ImVec2 ref_pos = NavCalcPreferredRefPos(); ImRect r_outer = GetWindowAllowedExtentRect(window); ImRect r_avoid; if (!g.NavDisableHighlight && g.NavDisableMouseHover && !(g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos)) r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 16, ref_pos.y + 8); else 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. ImVec2 pos = FindBestWindowPosForPopupEx(ref_pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); if (window->AutoPosLastDirection == ImGuiDir_None) 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. return pos; } IM_ASSERT(0); return window->Pos; } //----------------------------------------------------------------------------- // [SECTION] KEYBOARD/GAMEPAD NAVIGATION //----------------------------------------------------------------------------- ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy) { if (ImFabs(dx) > ImFabs(dy)) return (dx > 0.0f) ? ImGuiDir_Right : ImGuiDir_Left; return (dy > 0.0f) ? ImGuiDir_Down : ImGuiDir_Up; } static float inline NavScoreItemDistInterval(float a0, float a1, float b0, float b1) { if (a1 < b0) return a1 - b0; if (b1 < a0) return a0 - b1; return 0.0f; } static void inline NavClampRectToVisibleAreaForMoveDir(ImGuiDir move_dir, ImRect& r, const ImRect& clip_rect) { if (move_dir == ImGuiDir_Left || move_dir == ImGuiDir_Right) { r.Min.y = ImClamp(r.Min.y, clip_rect.Min.y, clip_rect.Max.y); r.Max.y = ImClamp(r.Max.y, clip_rect.Min.y, clip_rect.Max.y); } else { r.Min.x = ImClamp(r.Min.x, clip_rect.Min.x, clip_rect.Max.x); r.Max.x = ImClamp(r.Max.x, clip_rect.Min.x, clip_rect.Max.x); } } // Scoring function for directional navigation. Based on https://gist.github.com/rygorous/6981057 static bool NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (g.NavLayer != window->DC.NavLayerCurrent) return false; 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) g.NavScoringCount++; // When entering through a NavFlattened border, we consider child window items as fully clipped for scoring if (window->ParentWindow == g.NavWindow) { IM_ASSERT((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened); if (!window->ClipRect.Contains(cand)) return false; cand.ClipWithFull(window->ClipRect); // This allows the scored item to not overlap other candidates in the parent window } // 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) // For example, this ensure that items in one column are not reached when moving vertically from items in another column. NavClampRectToVisibleAreaForMoveDir(g.NavMoveClipDir, cand, window->ClipRect); // Compute distance between boxes // FIXME-NAV: Introducing biases for vertical navigation, needs to be removed. float dbx = NavScoreItemDistInterval(cand.Min.x, cand.Max.x, curr.Min.x, curr.Max.x); 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 if (dby != 0.0f && dbx != 0.0f) dbx = (dbx/1000.0f) + ((dbx > 0.0f) ? +1.0f : -1.0f); float dist_box = ImFabs(dbx) + ImFabs(dby); // 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) float dcx = (cand.Min.x + cand.Max.x) - (curr.Min.x + curr.Max.x); float dcy = (cand.Min.y + cand.Max.y) - (curr.Min.y + curr.Max.y); float dist_center = ImFabs(dcx) + ImFabs(dcy); // L1 metric (need this for our connectedness guarantee) // Determine which quadrant of 'curr' our candidate item 'cand' lies in based on distance ImGuiDir quadrant; float dax = 0.0f, day = 0.0f, dist_axial = 0.0f; if (dbx != 0.0f || dby != 0.0f) { // For non-overlapping boxes, use distance between boxes dax = dbx; day = dby; dist_axial = dist_box; quadrant = ImGetDirQuadrantFromDelta(dbx, dby); } else if (dcx != 0.0f || dcy != 0.0f) { // For overlapping boxes with different centers, use distance between centers dax = dcx; day = dcy; dist_axial = dist_center; quadrant = ImGetDirQuadrantFromDelta(dcx, dcy); } else { // 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) quadrant = (window->DC.LastItemId < g.NavId) ? ImGuiDir_Left : ImGuiDir_Right; } #if IMGUI_DEBUG_NAV_SCORING char buf[128]; if (ImGui::IsMouseHoveringRect(cand.Min, cand.Max)) { 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]); ImDrawList* draw_list = ImGui::GetForegroundDrawList(window); draw_list->AddRect(curr.Min, curr.Max, IM_COL32(255,200,0,100)); draw_list->AddRect(cand.Min, cand.Max, IM_COL32(255,255,0,200)); draw_list->AddRectFilled(cand.Max-ImVec2(4,4), cand.Max+ImGui::CalcTextSize(buf)+ImVec2(4,4), IM_COL32(40,0,0,150)); draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Max, ~0U, buf); } else if (g.IO.KeyCtrl) // Hold to preview score in matching quadrant. Press C to rotate. { if (ImGui::IsKeyPressedMap(ImGuiKey_C)) { g.NavMoveDirLast = (ImGuiDir)((g.NavMoveDirLast + 1) & 3); g.IO.KeysDownDuration[g.IO.KeyMap[ImGuiKey_C]] = 0.01f; } if (quadrant == g.NavMoveDir) { ImFormatString(buf, IM_ARRAYSIZE(buf), "%.0f/%.0f", dist_box, dist_center); ImDrawList* draw_list = ImGui::GetForegroundDrawList(window); draw_list->AddRectFilled(cand.Min, cand.Max, IM_COL32(255, 0, 0, 200)); draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Min, IM_COL32(255, 255, 255, 255), buf); } } #endif // Is it in the quadrant we're interesting in moving to? bool new_best = false; if (quadrant == g.NavMoveDir) { // Does it beat the current best candidate? if (dist_box < result->DistBox) { result->DistBox = dist_box; result->DistCenter = dist_center; return true; } if (dist_box == result->DistBox) { // Try using distance between center points to break ties if (dist_center < result->DistCenter) { result->DistCenter = dist_center; new_best = true; } else if (dist_center == result->DistCenter) { // Still tied! we need to be extra-careful to make sure everything gets linked properly. We consistently break ties by symbolically moving "later" items // (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), // 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. if (((g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) ? dby : dbx) < 0.0f) // moving bj to the right/down decreases distance new_best = true; } } } // 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 // are found, so it only augments the graph produced by the above method using extra links. (important, since it doesn't guarantee strong connectedness) // 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. // 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. // Disabling it may lead to disconnected graphs when nodes are very spaced out on different axis. Perhaps consider offering this as an option? if (result->DistBox == FLT_MAX && dist_axial < result->DistAxial) // Check axial match if (g.NavLayer == 1 && !(g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu)) 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)) { result->DistAxial = dist_axial; new_best = true; } return new_best; } // We get there when either NavId == id, or when g.NavAnyRequest is set (which is updated by NavUpdateAnyRequestFlag above) static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, const ImGuiID id) { ImGuiContext& g = *GImGui; //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. // return; const ImGuiItemFlags item_flags = window->DC.ItemFlags; const ImRect nav_bb_rel(nav_bb.Min - window->Pos, nav_bb.Max - window->Pos); // Process Init Request if (g.NavInitRequest && g.NavLayer == window->DC.NavLayerCurrent) { // Even if 'ImGuiItemFlags_NoNavDefaultFocus' is on (typically collapse/close button) we record the first ResultId so they can be used as a fallback if (!(item_flags & ImGuiItemFlags_NoNavDefaultFocus) || g.NavInitResultId == 0) { g.NavInitResultId = id; g.NavInitResultRectRel = nav_bb_rel; } if (!(item_flags & ImGuiItemFlags_NoNavDefaultFocus)) { g.NavInitRequest = false; // Found a match, clear request NavUpdateAnyRequestFlag(); } } // Process Move Request (scoring for navigation) // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRectScreen + scoring from a rect wrapped according to current wrapping policy) if ((g.NavId != id || (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled|ImGuiItemFlags_NoNav))) { ImGuiNavMoveResult* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; #if IMGUI_DEBUG_NAV_SCORING // [DEBUG] Score all items in NavWindow at all times if (!g.NavMoveRequest) g.NavMoveDir = g.NavMoveDirLast; bool new_best = NavScoreItem(result, nav_bb) && g.NavMoveRequest; #else bool new_best = g.NavMoveRequest && NavScoreItem(result, nav_bb); #endif if (new_best) { result->ID = id; result->SelectScopeId = g.MultiSelectScopeId; result->Window = window; result->RectRel = nav_bb_rel; } const float VISIBLE_RATIO = 0.70f; if ((g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) 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) if (NavScoreItem(&g.NavMoveResultLocalVisibleSet, nav_bb)) { result = &g.NavMoveResultLocalVisibleSet; result->ID = id; result->SelectScopeId = g.MultiSelectScopeId; result->Window = window; result->RectRel = nav_bb_rel; } } // Update window-relative bounding box of navigated item if (g.NavId == id) { g.NavWindow = window; // Always refresh g.NavWindow, because some operations such as FocusItem() don't have a window. g.NavLayer = window->DC.NavLayerCurrent; g.NavIdIsAlive = true; g.NavIdTabCounter = window->DC.FocusCounterTab; window->NavRectRel[window->DC.NavLayerCurrent] = nav_bb_rel; // Store item bounding box (relative to window position) } } bool ImGui::NavMoveRequestButNoResultYet() { ImGuiContext& g = *GImGui; return g.NavMoveRequest && g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0; } void ImGui::NavMoveRequestCancel() { ImGuiContext& g = *GImGui; g.NavMoveRequest = false; NavUpdateAnyRequestFlag(); } void ImGui::NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const ImRect& bb_rel, ImGuiNavMoveFlags move_flags) { ImGuiContext& g = *GImGui; IM_ASSERT(g.NavMoveRequestForward == ImGuiNavForward_None); ImGui::NavMoveRequestCancel(); g.NavMoveDir = move_dir; g.NavMoveClipDir = clip_dir; g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued; g.NavMoveRequestFlags = move_flags; g.NavWindow->NavRectRel[g.NavLayer] = bb_rel; } void ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags) { ImGuiContext& g = *GImGui; if (g.NavWindow != window || !NavMoveRequestButNoResultYet() || g.NavMoveRequestForward != ImGuiNavForward_None || g.NavLayer != 0) return; IM_ASSERT(move_flags != 0); // No points calling this with no wrapping ImRect bb_rel = window->NavRectRel[0]; ImGuiDir clip_dir = g.NavMoveDir; if (g.NavMoveDir == ImGuiDir_Left && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) { bb_rel.Min.x = bb_rel.Max.x = ImMax(window->SizeFull.x, window->SizeContents.x) - window->Scroll.x; if (move_flags & ImGuiNavMoveFlags_WrapX) { bb_rel.TranslateY(-bb_rel.GetHeight()); clip_dir = ImGuiDir_Up; } NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); } if (g.NavMoveDir == ImGuiDir_Right && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) { bb_rel.Min.x = bb_rel.Max.x = -window->Scroll.x; if (move_flags & ImGuiNavMoveFlags_WrapX) { bb_rel.TranslateY(+bb_rel.GetHeight()); clip_dir = ImGuiDir_Down; } NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); } if (g.NavMoveDir == ImGuiDir_Up && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) { bb_rel.Min.y = bb_rel.Max.y = ImMax(window->SizeFull.y, window->SizeContents.y) - window->Scroll.y; if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(-bb_rel.GetWidth()); clip_dir = ImGuiDir_Left; } NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); } if (g.NavMoveDir == ImGuiDir_Down && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) { bb_rel.Min.y = bb_rel.Max.y = -window->Scroll.y; if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(+bb_rel.GetWidth()); clip_dir = ImGuiDir_Right; } NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); } } // FIXME: This could be replaced by updating a frame number in each window when (window == NavWindow) and (NavLayer == 0). // This way we could find the last focused window among our children. It would be much less confusing this way? static void ImGui::NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window) { ImGuiWindow* parent_window = nav_window; while (parent_window && (parent_window->Flags & ImGuiWindowFlags_ChildWindow) != 0 && (parent_window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0) parent_window = parent_window->ParentWindow; if (parent_window && parent_window != nav_window) parent_window->NavLastChildNavWindow = nav_window; } // Restore the last focused child. // Call when we are expected to land on the Main Layer (0) after FocusWindow() static ImGuiWindow* ImGui::NavRestoreLastChildNavWindow(ImGuiWindow* window) { if (window->NavLastChildNavWindow && window->NavLastChildNavWindow->WasActive) return window->NavLastChildNavWindow; if (window->DockNodeAsHost && window->DockNodeAsHost->TabBar) if (ImGuiTabItem* tab = TabBarFindMostRecentlySelectedTabForActiveWindow(window->DockNodeAsHost->TabBar)) return tab->Window; return window; } static void NavRestoreLayer(ImGuiNavLayer layer) { ImGuiContext& g = *GImGui; g.NavLayer = layer; if (layer == 0) g.NavWindow = ImGui::NavRestoreLastChildNavWindow(g.NavWindow); if (g.NavWindow->NavLastIds[layer] != 0) ImGui::SetNavIDWithRectRel(g.NavWindow->NavLastIds[layer], layer, g.NavWindow->NavRectRel[layer]); else ImGui::NavInitWindow(g.NavWindow, true); } static inline void ImGui::NavUpdateAnyRequestFlag() { ImGuiContext& g = *GImGui; g.NavAnyRequest = g.NavMoveRequest || g.NavInitRequest || (IMGUI_DEBUG_NAV_SCORING && g.NavWindow != NULL); if (g.NavAnyRequest) IM_ASSERT(g.NavWindow != NULL); } // This needs to be called before we submit any widget (aka in or before Begin) void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) { ImGuiContext& g = *GImGui; IM_ASSERT(window == g.NavWindow); bool init_for_nav = false; if (!(window->Flags & ImGuiWindowFlags_NoNavInputs)) if (!(window->Flags & ImGuiWindowFlags_ChildWindow) || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastIds[0] == 0) || force_reinit) init_for_nav = true; if (init_for_nav) { SetNavID(0, g.NavLayer); g.NavInitRequest = true; g.NavInitRequestFromMove = false; g.NavInitResultId = 0; g.NavInitResultRectRel = ImRect(); NavUpdateAnyRequestFlag(); } else { g.NavId = window->NavLastIds[0]; } } static ImVec2 ImGui::NavCalcPreferredRefPos() { ImGuiContext& g = *GImGui; if (g.NavDisableHighlight || !g.NavDisableMouseHover || !g.NavWindow) { // Mouse (we need a fallback in case the mouse becomes invalid after being used) if (IsMousePosValid(&g.IO.MousePos)) return g.IO.MousePos; return g.LastValidMousePos; } else { // When navigation is active and mouse is disabled, decide on an arbitrary position around the bottom left of the currently navigated item. const ImRect& rect_rel = g.NavWindow->NavRectRel[g.NavLayer]; 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())); ImRect visible_rect = g.NavWindow->Viewport->GetRect(); 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. } } float ImGui::GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode) { ImGuiContext& g = *GImGui; if (mode == ImGuiInputReadMode_Down) return g.IO.NavInputs[n]; // Instant, read analog input (0.0f..1.0f, as provided by user) const float t = g.IO.NavInputsDownDuration[n]; if (t < 0.0f && mode == ImGuiInputReadMode_Released) // Return 1.0f when just released, no repeat, ignore analog input. return (g.IO.NavInputsDownDurationPrev[n] >= 0.0f ? 1.0f : 0.0f); if (t < 0.0f) return 0.0f; if (mode == ImGuiInputReadMode_Pressed) // Return 1.0f when just pressed, no repeat, ignore analog input. return (t == 0.0f) ? 1.0f : 0.0f; if (mode == ImGuiInputReadMode_Repeat) return (float)CalcTypematicPressedRepeatAmount(t, t - g.IO.DeltaTime, g.IO.KeyRepeatDelay * 0.80f, g.IO.KeyRepeatRate * 0.80f); if (mode == ImGuiInputReadMode_RepeatSlow) return (float)CalcTypematicPressedRepeatAmount(t, t - g.IO.DeltaTime, g.IO.KeyRepeatDelay * 1.00f, g.IO.KeyRepeatRate * 2.00f); if (mode == ImGuiInputReadMode_RepeatFast) return (float)CalcTypematicPressedRepeatAmount(t, t - g.IO.DeltaTime, g.IO.KeyRepeatDelay * 0.80f, g.IO.KeyRepeatRate * 0.30f); return 0.0f; } ImVec2 ImGui::GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor, float fast_factor) { ImVec2 delta(0.0f, 0.0f); if (dir_sources & ImGuiNavDirSourceFlags_Keyboard) delta += ImVec2(GetNavInputAmount(ImGuiNavInput_KeyRight_, mode) - GetNavInputAmount(ImGuiNavInput_KeyLeft_, mode), GetNavInputAmount(ImGuiNavInput_KeyDown_, mode) - GetNavInputAmount(ImGuiNavInput_KeyUp_, mode)); if (dir_sources & ImGuiNavDirSourceFlags_PadDPad) delta += ImVec2(GetNavInputAmount(ImGuiNavInput_DpadRight, mode) - GetNavInputAmount(ImGuiNavInput_DpadLeft, mode), GetNavInputAmount(ImGuiNavInput_DpadDown, mode) - GetNavInputAmount(ImGuiNavInput_DpadUp, mode)); if (dir_sources & ImGuiNavDirSourceFlags_PadLStick) delta += ImVec2(GetNavInputAmount(ImGuiNavInput_LStickRight, mode) - GetNavInputAmount(ImGuiNavInput_LStickLeft, mode), GetNavInputAmount(ImGuiNavInput_LStickDown, mode) - GetNavInputAmount(ImGuiNavInput_LStickUp, mode)); if (slow_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakSlow)) delta *= slow_factor; if (fast_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakFast)) delta *= fast_factor; return delta; } // Scroll to keep newly navigated item fully into view // NB: We modify rect_rel by the amount we scrolled for, so it is immediately updated. static void NavScrollToBringItemIntoView(ImGuiWindow* window, const ImRect& item_rect) { ImRect window_rect(window->InnerMainRect.Min - ImVec2(1, 1), window->InnerMainRect.Max + ImVec2(1, 1)); //GetForegroundDrawList(window)->AddRect(window_rect.Min, window_rect.Max, IM_COL32_WHITE); // [DEBUG] if (window_rect.Contains(item_rect)) return; ImGuiContext& g = *GImGui; if (window->ScrollbarX && item_rect.Min.x < window_rect.Min.x) { window->ScrollTarget.x = item_rect.Min.x - window->Pos.x + window->Scroll.x - g.Style.ItemSpacing.x; window->ScrollTargetCenterRatio.x = 0.0f; } else if (window->ScrollbarX && item_rect.Max.x >= window_rect.Max.x) { window->ScrollTarget.x = item_rect.Max.x - window->Pos.x + window->Scroll.x + g.Style.ItemSpacing.x; window->ScrollTargetCenterRatio.x = 1.0f; } if (item_rect.Min.y < window_rect.Min.y) { window->ScrollTarget.y = item_rect.Min.y - window->Pos.y + window->Scroll.y - g.Style.ItemSpacing.y; window->ScrollTargetCenterRatio.y = 0.0f; } else if (item_rect.Max.y >= window_rect.Max.y) { window->ScrollTarget.y = item_rect.Max.y - window->Pos.y + window->Scroll.y + g.Style.ItemSpacing.y; window->ScrollTargetCenterRatio.y = 1.0f; } } static void ImGui::NavUpdate() { ImGuiContext& g = *GImGui; g.IO.WantSetMousePos = false; #if 0 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); #endif // Set input source as Gamepad when buttons are pressed before we map Keyboard (some features differs when used with Gamepad vs Keyboard) bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; bool nav_gamepad_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (g.IO.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; if (nav_gamepad_active) 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) g.NavInputSource = ImGuiInputSource_NavGamepad; // Update Keyboard->Nav inputs mapping if (nav_keyboard_active) { #define NAV_MAP_KEY(_KEY, _NAV_INPUT) if (IsKeyDown(g.IO.KeyMap[_KEY])) { g.IO.NavInputs[_NAV_INPUT] = 1.0f; g.NavInputSource = ImGuiInputSource_NavKeyboard; } NAV_MAP_KEY(ImGuiKey_Space, ImGuiNavInput_Activate ); NAV_MAP_KEY(ImGuiKey_Enter, ImGuiNavInput_Input ); NAV_MAP_KEY(ImGuiKey_Escape, ImGuiNavInput_Cancel ); NAV_MAP_KEY(ImGuiKey_LeftArrow, ImGuiNavInput_KeyLeft_ ); NAV_MAP_KEY(ImGuiKey_RightArrow,ImGuiNavInput_KeyRight_); NAV_MAP_KEY(ImGuiKey_UpArrow, ImGuiNavInput_KeyUp_ ); NAV_MAP_KEY(ImGuiKey_DownArrow, ImGuiNavInput_KeyDown_ ); NAV_MAP_KEY(ImGuiKey_Tab, ImGuiNavInput_KeyTab_ ); if (g.IO.KeyCtrl) g.IO.NavInputs[ImGuiNavInput_TweakSlow] = 1.0f; if (g.IO.KeyShift) g.IO.NavInputs[ImGuiNavInput_TweakFast] = 1.0f; 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. g.IO.NavInputs[ImGuiNavInput_KeyMenu_] = 1.0f; #undef NAV_MAP_KEY } memcpy(g.IO.NavInputsDownDurationPrev, g.IO.NavInputsDownDuration, sizeof(g.IO.NavInputsDownDuration)); for (int i = 0; i < IM_ARRAYSIZE(g.IO.NavInputs); i++) 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; // Process navigation init request (select first/default focus) if (g.NavInitResultId != 0 && (!g.NavDisableHighlight || g.NavInitRequestFromMove)) { // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) IM_ASSERT(g.NavWindow); if (g.NavInitRequestFromMove) SetNavIDWithRectRel(g.NavInitResultId, g.NavLayer, g.NavInitResultRectRel); else SetNavID(g.NavInitResultId, g.NavLayer); g.NavWindow->NavRectRel[g.NavLayer] = g.NavInitResultRectRel; } g.NavInitRequest = false; g.NavInitRequestFromMove = false; g.NavInitResultId = 0; g.NavJustMovedToId = 0; // Process navigation move request if (g.NavMoveRequest) NavUpdateMoveResult(); // When a forwarded move request failed, we restore the highlight that we disabled during the forward frame if (g.NavMoveRequestForward == ImGuiNavForward_ForwardActive) { IM_ASSERT(g.NavMoveRequest); if (g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0) g.NavDisableHighlight = false; g.NavMoveRequestForward = ImGuiNavForward_None; } // Apply application mouse position movement, after we had a chance to process move request result. if (g.NavMousePosDirty && g.NavIdIsAlive) { // Set mouse position given our knowledge of the navigated item position from last frame if ((g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (g.IO.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) { if (!g.NavDisableHighlight && g.NavDisableMouseHover && g.NavWindow) { g.IO.MousePos = g.IO.MousePosPrev = NavCalcPreferredRefPos(); g.IO.WantSetMousePos = true; } } g.NavMousePosDirty = false; } g.NavIdIsAlive = false; g.NavJustTabbedId = 0; IM_ASSERT(g.NavLayer == 0 || g.NavLayer == 1); // 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 if (g.NavWindow) NavSaveLastChildNavWindowIntoParent(g.NavWindow); if (g.NavWindow && g.NavWindow->NavLastChildNavWindow != NULL && g.NavLayer == 0) g.NavWindow->NavLastChildNavWindow = NULL; // Update CTRL+TAB and Windowing features (hold Square to move/resize/etc.) NavUpdateWindowing(); // Set output flags for user application g.IO.NavActive = (nav_keyboard_active || nav_gamepad_active) && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs); g.IO.NavVisible = (g.IO.NavActive && g.NavId != 0 && !g.NavDisableHighlight) || (g.NavWindowingTarget != NULL); // Process NavCancel input (to close a popup, get back to parent, clear focus) if (IsNavInputPressed(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed)) { if (g.ActiveId != 0) { if (!(g.ActiveIdBlockNavInputFlags & (1 << ImGuiNavInput_Cancel))) ClearActiveID(); } else if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow) && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow && g.NavWindow != g.NavWindow->RootWindowDockStop) { // Exit child window ImGuiWindow* child_window = g.NavWindow; ImGuiWindow* parent_window = g.NavWindow->ParentWindow; IM_ASSERT(child_window->ChildId != 0); FocusWindow(parent_window); SetNavID(child_window->ChildId, 0); g.NavIdIsAlive = false; if (g.NavDisableMouseHover) g.NavMousePosDirty = true; } else if (g.OpenPopupStack.Size > 0) { // Close open popup/menu if (!(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) ClosePopupToLevel(g.OpenPopupStack.Size - 1, true); } else if (g.NavLayer != 0) { // Leave the "menu" layer NavRestoreLayer(ImGuiNavLayer_Main); } else { // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) g.NavWindow->NavLastIds[0] = 0; g.NavId = 0; } } // Process manual activation request g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavInputId = 0; if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { bool activate_down = IsNavInputDown(ImGuiNavInput_Activate); bool activate_pressed = activate_down && IsNavInputPressed(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed); if (g.ActiveId == 0 && activate_pressed) g.NavActivateId = g.NavId; if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_down) g.NavActivateDownId = g.NavId; if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_pressed) g.NavActivatePressedId = g.NavId; if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && IsNavInputPressed(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed)) g.NavInputId = g.NavId; } if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) g.NavDisableHighlight = true; if (g.NavActivateId != 0) IM_ASSERT(g.NavActivateDownId == g.NavActivateId); g.NavMoveRequest = false; // Process programmatic activation request if (g.NavNextActivateId != 0) g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavInputId = g.NavNextActivateId; g.NavNextActivateId = 0; // Initiate directional inputs request const int allowed_dir_flags = (g.ActiveId == 0) ? ~0 : g.ActiveIdAllowNavDirFlags; if (g.NavMoveRequestForward == ImGuiNavForward_None) { g.NavMoveDir = ImGuiDir_None; g.NavMoveRequestFlags = ImGuiNavMoveFlags_None; if (g.NavWindow && !g.NavWindowingTarget && allowed_dir_flags && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { if ((allowed_dir_flags & (1<Flags & ImGuiWindowFlags_NoNavInputs) && !g.NavWindowingTarget) { // *Fallback* manual-scroll with Nav directional keys when window has no navigable item ImGuiWindow* window = g.NavWindow; 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. if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll && g.NavMoveRequest) { if (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) SetWindowScrollX(window, ImFloor(window->Scroll.x + ((g.NavMoveDir == ImGuiDir_Left) ? -1.0f : +1.0f) * scroll_speed)); if (g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) SetWindowScrollY(window, ImFloor(window->Scroll.y + ((g.NavMoveDir == ImGuiDir_Up) ? -1.0f : +1.0f) * scroll_speed)); } // *Normal* Manual scroll with NavScrollXXX keys // Next movement request will clamp the NavId reference rectangle to the visible area, so navigation will resume within those bounds. ImVec2 scroll_dir = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down, 1.0f/10.0f, 10.0f); if (scroll_dir.x != 0.0f && window->ScrollbarX) { SetWindowScrollX(window, ImFloor(window->Scroll.x + scroll_dir.x * scroll_speed)); g.NavMoveFromClampedRefRect = true; } if (scroll_dir.y != 0.0f) { SetWindowScrollY(window, ImFloor(window->Scroll.y + scroll_dir.y * scroll_speed)); g.NavMoveFromClampedRefRect = true; } } // Reset search results g.NavMoveResultLocal.Clear(); g.NavMoveResultLocalVisibleSet.Clear(); g.NavMoveResultOther.Clear(); // 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 if (g.NavMoveRequest && g.NavMoveFromClampedRefRect && g.NavLayer == 0) { ImGuiWindow* window = g.NavWindow; ImRect window_rect_rel(window->InnerMainRect.Min - window->Pos - ImVec2(1,1), window->InnerMainRect.Max - window->Pos + ImVec2(1,1)); if (!window_rect_rel.Contains(window->NavRectRel[g.NavLayer])) { float pad = window->CalcFontSize() * 0.5f; 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 window->NavRectRel[g.NavLayer].ClipWith(window_rect_rel); g.NavId = 0; } g.NavMoveFromClampedRefRect = false; } // 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) ImRect nav_rect_rel = (g.NavWindow && !g.NavWindow->NavRectRel[g.NavLayer].IsInverted()) ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0,0,0,0); g.NavScoringRectScreen = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : ImRect(0,0,0,0); g.NavScoringRectScreen.TranslateY(nav_scoring_rect_offset_y); g.NavScoringRectScreen.Min.x = ImMin(g.NavScoringRectScreen.Min.x + 1.0f, g.NavScoringRectScreen.Max.x); g.NavScoringRectScreen.Max.x = g.NavScoringRectScreen.Min.x; 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(). //GetForegroundDrawList()->AddRect(g.NavScoringRectScreen.Min, g.NavScoringRectScreen.Max, IM_COL32(255,200,0,255)); // [DEBUG] g.NavScoringCount = 0; #if IMGUI_DEBUG_NAV_RECTS if (g.NavWindow) { ImDrawList* draw_list = GetForegroundDrawList(g.NavWindow); 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] 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); } } #endif } // Apply result from previous frame navigation directional move request static void ImGui::NavUpdateMoveResult() { ImGuiContext& g = *GImGui; if (g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0) { // 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) if (g.NavId != 0) { g.NavDisableHighlight = false; g.NavDisableMouseHover = true; } return; } // Select which result to use ImGuiNavMoveResult* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; // PageUp/PageDown behavior first jumps to the bottom/top mostly visible item, _otherwise_ use the result from the previous/next page. if (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) if (g.NavMoveResultLocalVisibleSet.ID != 0 && g.NavMoveResultLocalVisibleSet.ID != g.NavId) result = &g.NavMoveResultLocalVisibleSet; // Maybe entering a flattened child from the outside? In this case solve the tie using the regular scoring rules. if (result != &g.NavMoveResultOther && g.NavMoveResultOther.ID != 0 && g.NavMoveResultOther.Window->ParentWindow == g.NavWindow) if ((g.NavMoveResultOther.DistBox < result->DistBox) || (g.NavMoveResultOther.DistBox == result->DistBox && g.NavMoveResultOther.DistCenter < result->DistCenter)) result = &g.NavMoveResultOther; IM_ASSERT(g.NavWindow && result->Window); // Scroll to keep newly navigated item fully into view. if (g.NavLayer == 0) { ImRect rect_abs = ImRect(result->RectRel.Min + result->Window->Pos, result->RectRel.Max + result->Window->Pos); NavScrollToBringItemIntoView(result->Window, rect_abs); // Estimate upcoming scroll so we can offset our result position so mouse position can be applied immediately after in NavUpdate() ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(result->Window, false); ImVec2 delta_scroll = result->Window->Scroll - next_scroll; result->RectRel.Translate(delta_scroll); // Also scroll parent window to keep us into view if necessary (we could/should technically recurse back the whole the parent hierarchy). if (result->Window->Flags & ImGuiWindowFlags_ChildWindow) NavScrollToBringItemIntoView(result->Window->ParentWindow, ImRect(rect_abs.Min + delta_scroll, rect_abs.Max + delta_scroll)); } ClearActiveID(); g.NavWindow = result->Window; if (g.NavId != result->ID) { // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) g.NavJustMovedToId = result->ID; g.NavJustMovedToMultiSelectScopeId = result->SelectScopeId; } SetNavIDWithRectRel(result->ID, g.NavLayer, result->RectRel); g.NavMoveFromClampedRefRect = false; } static float ImGui::NavUpdatePageUpPageDown(int allowed_dir_flags) { ImGuiContext& g = *GImGui; if (g.NavMoveDir == ImGuiDir_None && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.NavWindowingTarget && g.NavLayer == 0) { ImGuiWindow* window = g.NavWindow; bool page_up_held = IsKeyDown(g.IO.KeyMap[ImGuiKey_PageUp]) && (allowed_dir_flags & (1 << ImGuiDir_Up)); bool page_down_held = IsKeyDown(g.IO.KeyMap[ImGuiKey_PageDown]) && (allowed_dir_flags & (1 << ImGuiDir_Down)); if (page_up_held != page_down_held) // If either (not both) are pressed { if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll) { // Fallback manual-scroll when window has no navigable item if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageUp], true)) SetWindowScrollY(window, window->Scroll.y - window->InnerMainRect.GetHeight()); else if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageDown], true)) SetWindowScrollY(window, window->Scroll.y + window->InnerMainRect.GetHeight()); } else { const ImRect& nav_rect_rel = window->NavRectRel[g.NavLayer]; const float page_offset_y = ImMax(0.0f, window->InnerMainRect.GetHeight() - window->CalcFontSize() * 1.0f + nav_rect_rel.GetHeight()); float nav_scoring_rect_offset_y = 0.0f; if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageUp], true)) { nav_scoring_rect_offset_y = -page_offset_y; 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) g.NavMoveClipDir = ImGuiDir_Up; g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; } else if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageDown], true)) { nav_scoring_rect_offset_y = +page_offset_y; 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) g.NavMoveClipDir = ImGuiDir_Down; g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; } return nav_scoring_rect_offset_y; } } } return 0.0f; } static int ImGui::FindWindowFocusIndex(ImGuiWindow* window) // FIXME-OPT O(N) { ImGuiContext& g = *GImGui; for (int i = g.WindowsFocusOrder.Size-1; i >= 0; i--) if (g.WindowsFocusOrder[i] == window) return i; return -1; } static ImGuiWindow* FindWindowNavFocusable(int i_start, int i_stop, int dir) // FIXME-OPT O(N) { ImGuiContext& g = *GImGui; for (int i = i_start; i >= 0 && i < g.WindowsFocusOrder.Size && i != i_stop; i += dir) if (ImGui::IsWindowNavFocusable(g.WindowsFocusOrder[i])) return g.WindowsFocusOrder[i]; return NULL; } static void NavUpdateWindowingHighlightWindow(int focus_change_dir) { ImGuiContext& g = *GImGui; IM_ASSERT(g.NavWindowingTarget); if (g.NavWindowingTarget->Flags & ImGuiWindowFlags_Modal) return; const int i_current = ImGui::FindWindowFocusIndex(g.NavWindowingTarget); ImGuiWindow* window_target = FindWindowNavFocusable(i_current + focus_change_dir, -INT_MAX, focus_change_dir); if (!window_target) window_target = FindWindowNavFocusable((focus_change_dir < 0) ? (g.WindowsFocusOrder.Size - 1) : 0, i_current, focus_change_dir); if (window_target) // Don't reset windowing target if there's a single window in the list g.NavWindowingTarget = g.NavWindowingTargetAnim = window_target; g.NavWindowingToggleLayer = false; } // Windowing management mode // Keyboard: CTRL+Tab (change focus/move/resize), Alt (toggle menu layer) // Gamepad: Hold Menu/Square (change focus/move/resize), Tap Menu/Square (toggle menu layer) static void ImGui::NavUpdateWindowing() { ImGuiContext& g = *GImGui; ImGuiWindow* apply_focus_window = NULL; bool apply_toggle_layer = false; ImGuiWindow* modal_window = GetFrontMostPopupModal(); if (modal_window != NULL) { g.NavWindowingTarget = NULL; return; } // Fade out if (g.NavWindowingTargetAnim && g.NavWindowingTarget == NULL) { g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha - g.IO.DeltaTime * 10.0f, 0.0f); if (g.DimBgRatio <= 0.0f && g.NavWindowingHighlightAlpha <= 0.0f) g.NavWindowingTargetAnim = NULL; } // Start CTRL-TAB or Square+L/R window selection bool start_windowing_with_gamepad = !g.NavWindowingTarget && IsNavInputPressed(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); bool start_windowing_with_keyboard = !g.NavWindowingTarget && g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard); if (start_windowing_with_gamepad || start_windowing_with_keyboard) if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) { g.NavWindowingTarget = g.NavWindowingTargetAnim = window; g.NavWindowingTimer = g.NavWindowingHighlightAlpha = 0.0f; g.NavWindowingToggleLayer = start_windowing_with_keyboard ? false : true; g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_NavKeyboard : ImGuiInputSource_NavGamepad; } // Gamepad update g.NavWindowingTimer += g.IO.DeltaTime; if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_NavGamepad) { // 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 g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // Select window to focus const int focus_change_dir = (int)IsNavInputPressed(ImGuiNavInput_FocusPrev, ImGuiInputReadMode_RepeatSlow) - (int)IsNavInputPressed(ImGuiNavInput_FocusNext, ImGuiInputReadMode_RepeatSlow); if (focus_change_dir != 0) { NavUpdateWindowingHighlightWindow(focus_change_dir); g.NavWindowingHighlightAlpha = 1.0f; } // Single press toggles NavLayer, long press with L/R apply actual focus on release (until then the window was merely rendered front-most) if (!IsNavInputDown(ImGuiNavInput_Menu)) { g.NavWindowingToggleLayer &= (g.NavWindowingHighlightAlpha < 1.0f); // Once button was held long enough we don't consider it a tap-to-toggle-layer press anymore. if (g.NavWindowingToggleLayer && g.NavWindow) apply_toggle_layer = true; else if (!g.NavWindowingToggleLayer) apply_focus_window = g.NavWindowingTarget; g.NavWindowingTarget = NULL; } } // Keyboard: Focus if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_NavKeyboard) { // Visuals only appears after a brief time after pressing TAB the first time, so that a fast CTRL+TAB doesn't add visual noise g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // 1.0f if (IsKeyPressedMap(ImGuiKey_Tab, true)) NavUpdateWindowingHighlightWindow(g.IO.KeyShift ? +1 : -1); if (!g.IO.KeyCtrl) apply_focus_window = g.NavWindowingTarget; } // Keyboard: Press and Release ALT to toggle menu layer // 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 if (IsNavInputPressed(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Pressed)) g.NavWindowingToggleLayer = true; if ((g.ActiveId == 0 || g.ActiveIdAllowOverlap) && g.NavWindowingToggleLayer && IsNavInputPressed(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Released)) if (IsMousePosValid(&g.IO.MousePos) == IsMousePosValid(&g.IO.MousePosPrev)) apply_toggle_layer = true; // Move window if (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoMove)) { ImVec2 move_delta; if (g.NavInputSource == ImGuiInputSource_NavKeyboard && !g.IO.KeyShift) move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down); if (g.NavInputSource == ImGuiInputSource_NavGamepad) move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down); if (move_delta.x != 0.0f || move_delta.y != 0.0f) { const float NAV_MOVE_SPEED = 800.0f; 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 g.NavWindowingTarget->RootWindow->Pos += move_delta * move_speed; g.NavDisableMouseHover = true; MarkIniSettingsDirty(g.NavWindowingTarget); } } // Apply final focus if (apply_focus_window && (g.NavWindow == NULL || apply_focus_window != g.NavWindow->RootWindowDockStop)) { ImGuiViewport* previous_viewport = g.NavWindow ? g.NavWindow->Viewport : NULL; ClearActiveID(); g.NavDisableHighlight = false; g.NavDisableMouseHover = true; apply_focus_window = NavRestoreLastChildNavWindow(apply_focus_window); ClosePopupsOverWindow(apply_focus_window, false); FocusWindow(apply_focus_window); if (apply_focus_window->NavLastIds[0] == 0) NavInitWindow(apply_focus_window, false); // If the window only has a menu layer, select it directly if (apply_focus_window->DC.NavLayerActiveMask == (1 << ImGuiNavLayer_Menu)) g.NavLayer = ImGuiNavLayer_Menu; // Request OS level focus if (apply_focus_window->Viewport != previous_viewport && g.PlatformIO.Platform_SetWindowFocus) g.PlatformIO.Platform_SetWindowFocus(apply_focus_window->Viewport); } if (apply_focus_window) g.NavWindowingTarget = NULL; // Apply menu/layer toggle if (apply_toggle_layer && g.NavWindow) { // Move to parent menu if necessary ImGuiWindow* new_nav_window = g.NavWindow; while (new_nav_window->ParentWindow && (new_nav_window->DC.NavLayerActiveMask & (1 << ImGuiNavLayer_Menu)) == 0 && (new_nav_window->Flags & ImGuiWindowFlags_ChildWindow) != 0 && (new_nav_window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0) new_nav_window = new_nav_window->ParentWindow; if (new_nav_window != g.NavWindow) { ImGuiWindow* old_nav_window = g.NavWindow; FocusWindow(new_nav_window); new_nav_window->NavLastChildNavWindow = old_nav_window; } g.NavDisableHighlight = false; g.NavDisableMouseHover = true; // When entering a regular menu bar with the Alt key, we always reinitialize the navigation ID. It however persist on docking tab tabs. const ImGuiNavLayer new_nav_layer = (g.NavWindow->DC.NavLayerActiveMask & (1 << ImGuiNavLayer_Menu)) ? (ImGuiNavLayer)((int)g.NavLayer ^ 1) : ImGuiNavLayer_Main; const bool preserve_layer_1_nav_id = (new_nav_window->DockNodeAsHost != NULL); if (new_nav_layer == ImGuiNavLayer_Menu && !preserve_layer_1_nav_id) g.NavWindow->NavLastIds[ImGuiNavLayer_Menu] = 0; NavRestoreLayer(new_nav_layer); } } // Window has already passed the IsWindowNavFocusable() static const char* GetFallbackWindowNameForWindowingList(ImGuiWindow* window) { if (window->Flags & ImGuiWindowFlags_Popup) return "(Popup)"; if ((window->Flags & ImGuiWindowFlags_MenuBar) && strcmp(window->Name, "##MainMenuBar") == 0) return "(Main menu bar)"; if (window->DockNodeAsHost) return "(Dock node)"; return "(Untitled)"; } // Overlay displayed when using CTRL+TAB. Called by EndFrame(). void ImGui::NavUpdateWindowingList() { ImGuiContext& g = *GImGui; IM_ASSERT(g.NavWindowingTarget != NULL); if (g.NavWindowingTimer < NAV_WINDOWING_LIST_APPEAR_DELAY) return; if (g.NavWindowingList == NULL) g.NavWindowingList = FindWindowByName("###NavWindowingList"); ImGuiViewportP* viewport = /*g.NavWindow ? g.NavWindow->Viewport :*/ (ImGuiViewportP*)GetMainViewport(); SetNextWindowSizeConstraints(ImVec2(viewport->Size.x * 0.20f, viewport->Size.y * 0.20f), ImVec2(FLT_MAX, FLT_MAX)); SetNextWindowPos(viewport->Pos + viewport->Size * 0.5f, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); PushStyleVar(ImGuiStyleVar_WindowPadding, g.Style.WindowPadding * 2.0f); Begin("###NavWindowingList", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings); for (int n = g.WindowsFocusOrder.Size - 1; n >= 0; n--) { ImGuiWindow* window = g.WindowsFocusOrder[n]; if (!IsWindowNavFocusable(window)) continue; const char* label = window->Name; if (label == FindRenderedTextEnd(label)) label = GetFallbackWindowNameForWindowingList(window); Selectable(label, g.NavWindowingTarget == window); } End(); PopStyleVar(); } //----------------------------------------------------------------------------- // [SECTION] COLUMNS // In the current version, Columns are very weak. Needs to be replaced with a more full-featured system. //----------------------------------------------------------------------------- void ImGui::NextColumn() { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems || window->DC.CurrentColumns == NULL) return; ImGuiContext& g = *GImGui; ImGuiColumns* columns = window->DC.CurrentColumns; if (columns->Count == 1) { window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); IM_ASSERT(columns->Current == 0); return; } PopItemWidth(); PopClipRect(); columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); if (++columns->Current < columns->Count) { // New column (columns 1+ cancels out IndentX) window->DC.ColumnsOffset.x = GetColumnOffset(columns->Current) - window->DC.Indent.x + g.Style.ItemSpacing.x; window->DrawList->ChannelsSetCurrent(columns->Current); } else { // New row/line window->DC.ColumnsOffset.x = 0.0f; window->DrawList->ChannelsSetCurrent(0); columns->Current = 0; columns->LineMinY = columns->LineMaxY; } window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); window->DC.CursorPos.y = columns->LineMinY; window->DC.CurrentLineSize = ImVec2(0.0f, 0.0f); window->DC.CurrentLineTextBaseOffset = 0.0f; PushColumnClipRect(); PushItemWidth(GetColumnWidth() * 0.65f); // FIXME-COLUMNS: Move on columns setup } int ImGui::GetColumnIndex() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.CurrentColumns ? window->DC.CurrentColumns->Current : 0; } int ImGui::GetColumnsCount() { ImGuiWindow* window = GetCurrentWindowRead(); return window->DC.CurrentColumns ? window->DC.CurrentColumns->Count : 1; } static float OffsetNormToPixels(const ImGuiColumns* columns, float offset_norm) { return offset_norm * (columns->MaxX - columns->MinX); } static float PixelsToOffsetNorm(const ImGuiColumns* columns, float offset) { return offset / (columns->MaxX - columns->MinX); } static const float COLUMNS_HIT_RECT_HALF_WIDTH = 4.0f; static float GetDraggedColumnOffset(ImGuiColumns* columns, int column_index) { // Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing // window creates a feedback loop because we store normalized positions. So while dragging we enforce absolute positioning. ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; IM_ASSERT(column_index > 0); // We are not supposed to drag column 0. IM_ASSERT(g.ActiveId == columns->ID + ImGuiID(column_index)); float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window->Pos.x; x = ImMax(x, ImGui::GetColumnOffset(column_index - 1) + g.Style.ColumnsMinSpacing); if ((columns->Flags & ImGuiColumnsFlags_NoPreserveWidths)) x = ImMin(x, ImGui::GetColumnOffset(column_index + 1) - g.Style.ColumnsMinSpacing); return x; } float ImGui::GetColumnOffset(int column_index) { ImGuiWindow* window = GetCurrentWindowRead(); ImGuiColumns* columns = window->DC.CurrentColumns; IM_ASSERT(columns != NULL); if (column_index < 0) column_index = columns->Current; IM_ASSERT(column_index < columns->Columns.Size); const float t = columns->Columns[column_index].OffsetNorm; const float x_offset = ImLerp(columns->MinX, columns->MaxX, t); return x_offset; } static float GetColumnWidthEx(ImGuiColumns* columns, int column_index, bool before_resize = false) { if (column_index < 0) column_index = columns->Current; float offset_norm; if (before_resize) offset_norm = columns->Columns[column_index + 1].OffsetNormBeforeResize - columns->Columns[column_index].OffsetNormBeforeResize; else offset_norm = columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm; return OffsetNormToPixels(columns, offset_norm); } float ImGui::GetColumnWidth(int column_index) { ImGuiWindow* window = GetCurrentWindowRead(); ImGuiColumns* columns = window->DC.CurrentColumns; IM_ASSERT(columns != NULL); if (column_index < 0) column_index = columns->Current; return OffsetNormToPixels(columns, columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm); } void ImGui::SetColumnOffset(int column_index, float offset) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImGuiColumns* columns = window->DC.CurrentColumns; IM_ASSERT(columns != NULL); if (column_index < 0) column_index = columns->Current; IM_ASSERT(column_index < columns->Columns.Size); const bool preserve_width = !(columns->Flags & ImGuiColumnsFlags_NoPreserveWidths) && (column_index < columns->Count-1); const float width = preserve_width ? GetColumnWidthEx(columns, column_index, columns->IsBeingResized) : 0.0f; if (!(columns->Flags & ImGuiColumnsFlags_NoForceWithinWindow)) offset = ImMin(offset, columns->MaxX - g.Style.ColumnsMinSpacing * (columns->Count - column_index)); columns->Columns[column_index].OffsetNorm = PixelsToOffsetNorm(columns, offset - columns->MinX); if (preserve_width) SetColumnOffset(column_index + 1, offset + ImMax(g.Style.ColumnsMinSpacing, width)); } void ImGui::SetColumnWidth(int column_index, float width) { ImGuiWindow* window = GetCurrentWindowRead(); ImGuiColumns* columns = window->DC.CurrentColumns; IM_ASSERT(columns != NULL); if (column_index < 0) column_index = columns->Current; SetColumnOffset(column_index + 1, GetColumnOffset(column_index) + width); } void ImGui::PushColumnClipRect(int column_index) { ImGuiWindow* window = GetCurrentWindowRead(); ImGuiColumns* columns = window->DC.CurrentColumns; if (column_index < 0) column_index = columns->Current; ImGuiColumnData* column = &columns->Columns[column_index]; PushClipRect(column->ClipRect.Min, column->ClipRect.Max, false); } ImGuiColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id) { // We have few columns per window so for now we don't need bother much with turning this into a faster lookup. for (int n = 0; n < window->ColumnsStorage.Size; n++) if (window->ColumnsStorage[n].ID == id) return &window->ColumnsStorage[n]; window->ColumnsStorage.push_back(ImGuiColumns()); ImGuiColumns* columns = &window->ColumnsStorage.back(); columns->ID = id; return columns; } ImGuiID ImGui::GetColumnsID(const char* str_id, int columns_count) { ImGuiWindow* window = GetCurrentWindow(); // Differentiate column ID with an arbitrary prefix for cases where users name their columns set the same as another widget. // In addition, when an identifier isn't explicitly provided we include the number of columns in the hash to make it uniquer. PushID(0x11223347 + (str_id ? 0 : columns_count)); ImGuiID id = window->GetID(str_id ? str_id : "columns"); PopID(); return id; } void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); IM_ASSERT(columns_count >= 1); IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported ImGuiID id = GetColumnsID(str_id, columns_count); // Acquire storage for the columns set ImGuiColumns* columns = FindOrCreateColumns(window, id); IM_ASSERT(columns->ID == id); columns->Current = 0; columns->Count = columns_count; columns->Flags = flags; window->DC.CurrentColumns = columns; // Set state for first column const float content_region_width = (window->SizeContentsExplicit.x != 0.0f) ? (window->SizeContentsExplicit.x) : (window->InnerClipRect.Max.x - window->Pos.x); columns->MinX = window->DC.Indent.x - g.Style.ItemSpacing.x; // Lock our horizontal range columns->MaxX = ImMax(content_region_width - window->Scroll.x, columns->MinX + 1.0f); columns->BackupCursorPosY = window->DC.CursorPos.y; columns->BackupCursorMaxPosX = window->DC.CursorMaxPos.x; columns->LineMinY = columns->LineMaxY = window->DC.CursorPos.y; window->DC.ColumnsOffset.x = 0.0f; window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); // Clear data if columns count changed if (columns->Columns.Size != 0 && columns->Columns.Size != columns_count + 1) columns->Columns.resize(0); // Initialize defaults columns->IsFirstFrame = (columns->Columns.Size == 0); if (columns->Columns.Size == 0) { columns->Columns.reserve(columns_count + 1); for (int n = 0; n < columns_count + 1; n++) { ImGuiColumnData column; column.OffsetNorm = n / (float)columns_count; columns->Columns.push_back(column); } } for (int n = 0; n < columns_count; n++) { // Compute clipping rectangle ImGuiColumnData* column = &columns->Columns[n]; float clip_x1 = ImFloor(0.5f + window->Pos.x + GetColumnOffset(n)); float clip_x2 = ImFloor(0.5f + window->Pos.x + GetColumnOffset(n + 1) - 1.0f); column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX); column->ClipRect.ClipWith(window->ClipRect); } if (columns->Count > 1) { window->DrawList->ChannelsSplit(columns->Count); PushColumnClipRect(); } PushItemWidth(GetColumnWidth() * 0.65f); } void ImGui::EndColumns() { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); ImGuiColumns* columns = window->DC.CurrentColumns; IM_ASSERT(columns != NULL); PopItemWidth(); if (columns->Count > 1) { PopClipRect(); window->DrawList->ChannelsMerge(); } columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); window->DC.CursorPos.y = columns->LineMaxY; if (!(columns->Flags & ImGuiColumnsFlags_GrowParentContentsSize)) window->DC.CursorMaxPos.x = columns->BackupCursorMaxPosX; // Restore cursor max pos, as columns don't grow parent // Draw columns borders and handle resize bool is_being_resized = false; if (!(columns->Flags & ImGuiColumnsFlags_NoBorder) && !window->SkipItems) { // We clip Y boundaries CPU side because very long triangles are mishandled by some GPU drivers. const float y1 = ImMax(columns->BackupCursorPosY, window->ClipRect.Min.y); const float y2 = ImMin(window->DC.CursorPos.y, window->ClipRect.Max.y); int dragging_column = -1; for (int n = 1; n < columns->Count; n++) { ImGuiColumnData* column = &columns->Columns[n]; float x = window->Pos.x + GetColumnOffset(n); const ImGuiID column_id = columns->ID + ImGuiID(n); const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH; const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2)); KeepAliveID(column_id); if (IsClippedEx(column_hit_rect, column_id, false)) continue; bool hovered = false, held = false; if (!(columns->Flags & ImGuiColumnsFlags_NoResize)) { ButtonBehavior(column_hit_rect, column_id, &hovered, &held); if (hovered || held) g.MouseCursor = ImGuiMouseCursor_ResizeEW; if (held && !(column->Flags & ImGuiColumnsFlags_NoResize)) dragging_column = n; } // Draw column const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : hovered ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); const float xi = (float)(int)x; window->DrawList->AddLine(ImVec2(xi, y1 + 1.0f), ImVec2(xi, y2), col); } // Apply dragging after drawing the column lines, so our rendered lines are in sync with how items were displayed during the frame. if (dragging_column != -1) { if (!columns->IsBeingResized) for (int n = 0; n < columns->Count + 1; n++) columns->Columns[n].OffsetNormBeforeResize = columns->Columns[n].OffsetNorm; columns->IsBeingResized = is_being_resized = true; float x = GetDraggedColumnOffset(columns, dragging_column); SetColumnOffset(dragging_column, x); } } columns->IsBeingResized = is_being_resized; window->DC.CurrentColumns = NULL; window->DC.ColumnsOffset.x = 0.0f; window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); } // [2018-03: This is currently the only public API, while we are working on making BeginColumns/EndColumns user-facing] void ImGui::Columns(int columns_count, const char* id, bool border) { ImGuiWindow* window = GetCurrentWindow(); IM_ASSERT(columns_count >= 1); ImGuiColumnsFlags flags = (border ? 0 : ImGuiColumnsFlags_NoBorder); //flags |= ImGuiColumnsFlags_NoPreserveWidths; // NB: Legacy behavior ImGuiColumns* columns = window->DC.CurrentColumns; if (columns != NULL && columns->Count == columns_count && columns->Flags == flags) return; if (columns != NULL) EndColumns(); if (columns_count != 1) BeginColumns(id, columns_count, flags); } //----------------------------------------------------------------------------- // [SECTION] DRAG AND DROP //----------------------------------------------------------------------------- void ImGui::ClearDragDrop() { ImGuiContext& g = *GImGui; g.DragDropActive = false; g.DragDropPayload.Clear(); g.DragDropAcceptFlags = ImGuiDragDropFlags_None; g.DragDropAcceptIdCurr = g.DragDropAcceptIdPrev = 0; g.DragDropAcceptIdCurrRectSurface = FLT_MAX; g.DragDropAcceptFrameCount = -1; g.DragDropPayloadBufHeap.clear(); memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal)); } // Call when current ID is active. // When this returns true you need to: a) call SetDragDropPayload() exactly once, b) you may render the payload visual/description, c) call EndDragDropSource() bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; bool source_drag_active = false; ImGuiID source_id = 0; ImGuiID source_parent_id = 0; int mouse_button = 0; if (!(flags & ImGuiDragDropFlags_SourceExtern)) { source_id = window->DC.LastItemId; if (source_id != 0 && g.ActiveId != source_id) // Early out for most common case return false; if (g.IO.MouseDown[mouse_button] == false) return false; if (source_id == 0) { // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to: // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag, C) Swallow your programmer pride. if (!(flags & ImGuiDragDropFlags_SourceAllowNullID)) { IM_ASSERT(0); return false; } // Magic fallback (=somehow reprehensible) to handle items with no assigned ID, e.g. Text(), Image() // We build a throwaway ID based on current ID stack + relative AABB of items in window. // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING OF THE WIDGET, so if your widget moves your dragging operation will be canceled. // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive. bool is_hovered = (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect) != 0; if (!is_hovered && (g.ActiveId == 0 || g.ActiveIdWindow != window)) return false; source_id = window->DC.LastItemId = window->GetIDFromRectangle(window->DC.LastItemRect); if (is_hovered) SetHoveredID(source_id); if (is_hovered && g.IO.MouseClicked[mouse_button]) { SetActiveID(source_id, window); FocusWindow(window); } if (g.ActiveId == source_id) // Allow the underlying widget to display/return hovered during the mouse release frame, else we would get a flicker. g.ActiveIdAllowOverlap = is_hovered; } else { g.ActiveIdAllowOverlap = false; } if (g.ActiveId != source_id) return false; source_parent_id = window->IDStack.back(); source_drag_active = IsMouseDragging(mouse_button); } else { window = NULL; source_id = ImHashStr("#SourceExtern"); source_drag_active = true; } if (source_drag_active) { if (!g.DragDropActive) { IM_ASSERT(source_id != 0); ClearDragDrop(); ImGuiPayload& payload = g.DragDropPayload; payload.SourceId = source_id; payload.SourceParentId = source_parent_id; g.DragDropActive = true; g.DragDropSourceFlags = flags; g.DragDropMouseButton = mouse_button; } g.DragDropSourceFrameCount = g.FrameCount; g.DragDropWithinSourceOrTarget = true; if (!(flags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) { // Target can request the Source to not display its tooltip (we use a dedicated flag to make this request explicit) // We unfortunately can't just modify the source flags and skip the call to BeginTooltip, as caller may be emitting contents. BeginTooltip(); if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) { ImGuiWindow* tooltip_window = g.CurrentWindow; tooltip_window->SkipItems = true; tooltip_window->HiddenFramesCanSkipItems = 1; } } if (!(flags & ImGuiDragDropFlags_SourceNoDisableHover) && !(flags & ImGuiDragDropFlags_SourceExtern)) window->DC.LastItemStatusFlags &= ~ImGuiItemStatusFlags_HoveredRect; return true; } return false; } void ImGui::EndDragDropSource() { ImGuiContext& g = *GImGui; IM_ASSERT(g.DragDropActive); IM_ASSERT(g.DragDropWithinSourceOrTarget && "Not after a BeginDragDropSource()?"); if (!(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) EndTooltip(); // Discard the drag if have not called SetDragDropPayload() if (g.DragDropPayload.DataFrameCount == -1) ClearDragDrop(); g.DragDropWithinSourceOrTarget = false; } // Use 'cond' to choose to submit payload on drag start or every frame bool ImGui::SetDragDropPayload(const char* type, const void* data, size_t data_size, ImGuiCond cond) { ImGuiContext& g = *GImGui; ImGuiPayload& payload = g.DragDropPayload; if (cond == 0) cond = ImGuiCond_Always; IM_ASSERT(type != NULL); IM_ASSERT(strlen(type) < IM_ARRAYSIZE(payload.DataType) && "Payload type can be at most 32 characters long"); IM_ASSERT((data != NULL && data_size > 0) || (data == NULL && data_size == 0)); IM_ASSERT(cond == ImGuiCond_Always || cond == ImGuiCond_Once); IM_ASSERT(payload.SourceId != 0); // Not called between BeginDragDropSource() and EndDragDropSource() if (cond == ImGuiCond_Always || payload.DataFrameCount == -1) { // Copy payload ImStrncpy(payload.DataType, type, IM_ARRAYSIZE(payload.DataType)); g.DragDropPayloadBufHeap.resize(0); if (data_size > sizeof(g.DragDropPayloadBufLocal)) { // Store in heap g.DragDropPayloadBufHeap.resize((int)data_size); payload.Data = g.DragDropPayloadBufHeap.Data; memcpy(payload.Data, data, data_size); } else if (data_size > 0) { // Store locally memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal)); payload.Data = g.DragDropPayloadBufLocal; memcpy(payload.Data, data, data_size); } else { payload.Data = NULL; } payload.DataSize = (int)data_size; } payload.DataFrameCount = g.FrameCount; return (g.DragDropAcceptFrameCount == g.FrameCount) || (g.DragDropAcceptFrameCount == g.FrameCount - 1); } bool ImGui::BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id) { ImGuiContext& g = *GImGui; if (!g.DragDropActive) return false; ImGuiWindow* window = g.CurrentWindow; if (g.HoveredWindowUnderMovingWindow == NULL || window->RootWindow != g.HoveredWindowUnderMovingWindow->RootWindow) return false; IM_ASSERT(id != 0); if (!IsMouseHoveringRect(bb.Min, bb.Max) || (id == g.DragDropPayload.SourceId)) return false; if (window->SkipItems) return false; IM_ASSERT(g.DragDropWithinSourceOrTarget == false); g.DragDropTargetRect = bb; g.DragDropTargetId = id; g.DragDropWithinSourceOrTarget = true; return true; } // We don't use BeginDragDropTargetCustom() and duplicate its code because: // 1) we use LastItemRectHoveredRect which handles items that pushes a temporarily clip rectangle in their code. Calling BeginDragDropTargetCustom(LastItemRect) would not handle them. // 2) and it's faster. as this code may be very frequently called, we want to early out as fast as we can. // Also note how the HoveredWindow test is positioned differently in both functions (in both functions we optimize for the cheapest early out case) bool ImGui::BeginDragDropTarget() { ImGuiContext& g = *GImGui; if (!g.DragDropActive) return false; ImGuiWindow* window = g.CurrentWindow; if (!(window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect)) return false; if (g.HoveredWindowUnderMovingWindow == NULL || window->RootWindow != g.HoveredWindowUnderMovingWindow->RootWindow) return false; const ImRect& display_rect = (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect) ? window->DC.LastItemDisplayRect : window->DC.LastItemRect; ImGuiID id = window->DC.LastItemId; if (id == 0) id = window->GetIDFromRectangle(display_rect); if (g.DragDropPayload.SourceId == id) return false; IM_ASSERT(g.DragDropWithinSourceOrTarget == false); g.DragDropTargetRect = display_rect; g.DragDropTargetId = id; g.DragDropWithinSourceOrTarget = true; return true; } bool ImGui::IsDragDropPayloadBeingAccepted() { ImGuiContext& g = *GImGui; return g.DragDropActive && g.DragDropAcceptIdPrev != 0; } const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDropFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImGuiPayload& payload = g.DragDropPayload; IM_ASSERT(g.DragDropActive); // Not called between BeginDragDropTarget() and EndDragDropTarget() ? IM_ASSERT(payload.DataFrameCount != -1); // Forgot to call EndDragDropTarget() ? if (type != NULL && !payload.IsDataType(type)) return NULL; // Accept smallest drag target bounding box, this allows us to nest drag targets conveniently without ordering constraints. // NB: We currently accept NULL id as target. However, overlapping targets requires a unique ID to function! const bool was_accepted_previously = (g.DragDropAcceptIdPrev == g.DragDropTargetId); ImRect r = g.DragDropTargetRect; float r_surface = r.GetWidth() * r.GetHeight(); if (r_surface < g.DragDropAcceptIdCurrRectSurface) { g.DragDropAcceptFlags = flags; g.DragDropAcceptIdCurr = g.DragDropTargetId; g.DragDropAcceptIdCurrRectSurface = r_surface; } // Render default drop visuals payload.Preview = was_accepted_previously; flags |= (g.DragDropSourceFlags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect); // Source can also inhibit the preview (useful for external sources that lives for 1 frame) if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && payload.Preview) { // FIXME-DRAG: Settle on a proper default visuals for drop target. r.Expand(3.5f); bool push_clip_rect = !window->ClipRect.Contains(r); if (push_clip_rect) window->DrawList->PushClipRect(r.Min-ImVec2(1,1), r.Max+ImVec2(1,1)); window->DrawList->AddRect(r.Min, r.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, ~0, 2.0f); if (push_clip_rect) window->DrawList->PopClipRect(); } g.DragDropAcceptFrameCount = g.FrameCount; 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() if (!payload.Delivery && !(flags & ImGuiDragDropFlags_AcceptBeforeDelivery)) return NULL; return &payload; } const ImGuiPayload* ImGui::GetDragDropPayload() { ImGuiContext& g = *GImGui; return g.DragDropActive ? &g.DragDropPayload : NULL; } // We don't really use/need this now, but added it for the sake of consistency and because we might need it later. void ImGui::EndDragDropTarget() { ImGuiContext& g = *GImGui; IM_ASSERT(g.DragDropActive); IM_ASSERT(g.DragDropWithinSourceOrTarget); g.DragDropWithinSourceOrTarget = false; } //----------------------------------------------------------------------------- // [SECTION] LOGGING/CAPTURING //----------------------------------------------------------------------------- // All text output from the interface can be captured into tty/file/clipboard. // By default, tree nodes are automatically opened during logging. //----------------------------------------------------------------------------- // Pass text data straight to log (without being displayed) void ImGui::LogText(const char* fmt, ...) { ImGuiContext& g = *GImGui; if (!g.LogEnabled) return; va_list args; va_start(args, fmt); if (g.LogFile) vfprintf(g.LogFile, fmt, args); else g.LogBuffer.appendfv(fmt, args); va_end(args); } // Internal version that takes a position to decide on newline placement and pad items according to their depth. // We split text into individual lines to add current tree level padding void ImGui::LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (!text_end) text_end = FindRenderedTextEnd(text, text_end); const bool log_new_line = ref_pos && (ref_pos->y > g.LogLinePosY + 1); if (ref_pos) g.LogLinePosY = ref_pos->y; if (log_new_line) g.LogLineFirstItem = true; const char* text_remaining = text; if (g.LogDepthRef > window->DC.TreeDepth) // Re-adjust padding if we have popped out of our starting depth g.LogDepthRef = window->DC.TreeDepth; const int tree_depth = (window->DC.TreeDepth - g.LogDepthRef); for (;;) { // Split the string. Each new line (after a '\n') is followed by spacing corresponding to the current depth of our log entry. // We don't add a trailing \n to allow a subsequent item on the same line to be captured. const char* line_start = text_remaining; const char* line_end = ImStreolRange(line_start, text_end); const bool is_first_line = (line_start == text); const bool is_last_line = (line_end == text_end); if (!is_last_line || (line_start != line_end)) { const int char_count = (int)(line_end - line_start); if (log_new_line || !is_first_line) LogText(IM_NEWLINE "%*s%.*s", tree_depth * 4, "", char_count, line_start); else if (g.LogLineFirstItem) LogText("%*s%.*s", tree_depth * 4, "", char_count, line_start); else LogText(" %.*s", char_count, line_start); g.LogLineFirstItem = false; } else if (log_new_line) { // An empty "" string at a different Y position should output a carriage return. LogText(IM_NEWLINE); break; } if (is_last_line) break; text_remaining = line_end + 1; } } // Start logging/capturing text output void ImGui::LogBegin(ImGuiLogType type, int auto_open_depth) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; IM_ASSERT(g.LogEnabled == false); IM_ASSERT(g.LogFile == NULL); IM_ASSERT(g.LogBuffer.empty()); g.LogEnabled = true; g.LogType = type; g.LogDepthRef = window->DC.TreeDepth; g.LogDepthToExpand = ((auto_open_depth >= 0) ? auto_open_depth : g.LogDepthToExpandDefault); g.LogLinePosY = FLT_MAX; g.LogLineFirstItem = true; } void ImGui::LogToTTY(int auto_open_depth) { ImGuiContext& g = *GImGui; if (g.LogEnabled) return; LogBegin(ImGuiLogType_TTY, auto_open_depth); g.LogFile = stdout; } // Start logging/capturing text output to given file void ImGui::LogToFile(int auto_open_depth, const char* filename) { ImGuiContext& g = *GImGui; if (g.LogEnabled) return; // FIXME: We could probably open the file in text mode "at", however note that clipboard/buffer logging will still // be subject to outputting OS-incompatible carriage return if within strings the user doesn't use IM_NEWLINE. // By opening the file in binary mode "ab" we have consistent output everywhere. if (!filename) filename = g.IO.LogFilename; if (!filename || !filename[0]) return; FILE* f = ImFileOpen(filename, "ab"); if (f == NULL) { IM_ASSERT(0); return; } LogBegin(ImGuiLogType_File, auto_open_depth); g.LogFile = f; } // Start logging/capturing text output to clipboard void ImGui::LogToClipboard(int auto_open_depth) { ImGuiContext& g = *GImGui; if (g.LogEnabled) return; LogBegin(ImGuiLogType_Clipboard, auto_open_depth); } void ImGui::LogToBuffer(int auto_open_depth) { ImGuiContext& g = *GImGui; if (g.LogEnabled) return; LogBegin(ImGuiLogType_Buffer, auto_open_depth); } void ImGui::LogFinish() { ImGuiContext& g = *GImGui; if (!g.LogEnabled) return; LogText(IM_NEWLINE); switch (g.LogType) { case ImGuiLogType_TTY: fflush(g.LogFile); break; case ImGuiLogType_File: fclose(g.LogFile); break; case ImGuiLogType_Buffer: break; case ImGuiLogType_Clipboard: if (!g.LogBuffer.empty()) SetClipboardText(g.LogBuffer.begin()); break; case ImGuiLogType_None: IM_ASSERT(0); break; } g.LogEnabled = false; g.LogType = ImGuiLogType_None; g.LogFile = NULL; g.LogBuffer.clear(); } // Helper to display logging buttons // FIXME-OBSOLETE: We should probably obsolete this and let the user have their own helper (this is one of the oldest function alive!) void ImGui::LogButtons() { ImGuiContext& g = *GImGui; PushID("LogButtons"); const bool log_to_tty = Button("Log To TTY"); SameLine(); const bool log_to_file = Button("Log To File"); SameLine(); const bool log_to_clipboard = Button("Log To Clipboard"); SameLine(); PushAllowKeyboardFocus(false); SetNextItemWidth(80.0f); SliderInt("Default Depth", &g.LogDepthToExpandDefault, 0, 9, NULL); PopAllowKeyboardFocus(); PopID(); // Start logging at the end of the function so that the buttons don't appear in the log if (log_to_tty) LogToTTY(); if (log_to_file) LogToFile(); if (log_to_clipboard) LogToClipboard(); } //----------------------------------------------------------------------------- // [SECTION] SETTINGS //----------------------------------------------------------------------------- void ImGui::MarkIniSettingsDirty() { ImGuiContext& g = *GImGui; if (g.SettingsDirtyTimer <= 0.0f) g.SettingsDirtyTimer = g.IO.IniSavingRate; } void ImGui::MarkIniSettingsDirty(ImGuiWindow* window) { ImGuiContext& g = *GImGui; if (!(window->Flags & ImGuiWindowFlags_NoSavedSettings)) if (g.SettingsDirtyTimer <= 0.0f) g.SettingsDirtyTimer = g.IO.IniSavingRate; } ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name) { ImGuiContext& g = *GImGui; g.SettingsWindows.push_back(ImGuiWindowSettings()); ImGuiWindowSettings* settings = &g.SettingsWindows.back(); settings->Name = ImStrdup(name); settings->ID = ImHashStr(name); return settings; } ImGuiWindowSettings* ImGui::FindWindowSettings(ImGuiID id) { ImGuiContext& g = *GImGui; for (int i = 0; i != g.SettingsWindows.Size; i++) if (g.SettingsWindows[i].ID == id) return &g.SettingsWindows[i]; return NULL; } ImGuiWindowSettings* ImGui::FindOrCreateWindowSettings(const char* name) { if (ImGuiWindowSettings* settings = FindWindowSettings(ImHashStr(name))) return settings; return CreateNewWindowSettings(name); } void ImGui::LoadIniSettingsFromDisk(const char* ini_filename) { size_t file_data_size = 0; char* file_data = (char*)ImFileLoadToMemory(ini_filename, "rb", &file_data_size); if (!file_data) return; LoadIniSettingsFromMemory(file_data, (size_t)file_data_size); IM_FREE(file_data); } ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) { ImGuiContext& g = *GImGui; const ImGuiID type_hash = ImHashStr(type_name); for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) if (g.SettingsHandlers[handler_n].TypeHash == type_hash) return &g.SettingsHandlers[handler_n]; return NULL; } // Zero-tolerance, no error reporting, cheap .ini parsing void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) { ImGuiContext& g = *GImGui; IM_ASSERT(g.Initialized); IM_ASSERT(g.SettingsLoaded == false && g.FrameCount == 0); // For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter). // 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.. if (ini_size == 0) ini_size = strlen(ini_data); char* buf = (char*)IM_ALLOC(ini_size + 1); char* buf_end = buf + ini_size; memcpy(buf, ini_data, ini_size); buf[ini_size] = 0; void* entry_data = NULL; ImGuiSettingsHandler* entry_handler = NULL; char* line_end = NULL; for (char* line = buf; line < buf_end; line = line_end + 1) { // Skip new lines markers, then find end of the line while (*line == '\n' || *line == '\r') line++; line_end = line; while (line_end < buf_end && *line_end != '\n' && *line_end != '\r') line_end++; line_end[0] = 0; if (line[0] == ';') continue; if (line[0] == '[' && line_end > line && line_end[-1] == ']') { // Parse "[Type][Name]". Note that 'Name' can itself contains [] characters, which is acceptable with the current format and parsing code. line_end[-1] = 0; const char* name_end = line_end - 1; const char* type_start = line + 1; char* type_end = (char*)(intptr_t)ImStrchrRange(type_start, name_end, ']'); const char* name_start = type_end ? ImStrchrRange(type_end + 1, name_end, '[') : NULL; if (!type_end || !name_start) { name_start = type_start; // Import legacy entries that have no type type_start = "Window"; } else { *type_end = 0; // Overwrite first ']' name_start++; // Skip second '[' } entry_handler = FindSettingsHandler(type_start); entry_data = entry_handler ? entry_handler->ReadOpenFn(&g, entry_handler, name_start) : NULL; } else if (entry_handler != NULL && entry_data != NULL) { // Let type handler parse the line entry_handler->ReadLineFn(&g, entry_handler, entry_data, line); } } IM_FREE(buf); g.SettingsLoaded = true; DockContextOnLoadSettings(&g); } void ImGui::SaveIniSettingsToDisk(const char* ini_filename) { ImGuiContext& g = *GImGui; g.SettingsDirtyTimer = 0.0f; if (!ini_filename) return; size_t ini_data_size = 0; const char* ini_data = SaveIniSettingsToMemory(&ini_data_size); FILE* f = ImFileOpen(ini_filename, "wt"); if (!f) return; fwrite(ini_data, sizeof(char), ini_data_size, f); fclose(f); } // Call registered handlers (e.g. SettingsHandlerWindow_WriteAll() + custom handlers) to write their stuff into a text buffer const char* ImGui::SaveIniSettingsToMemory(size_t* out_size) { ImGuiContext& g = *GImGui; g.SettingsDirtyTimer = 0.0f; g.SettingsIniData.Buf.resize(0); g.SettingsIniData.Buf.push_back(0); for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) { ImGuiSettingsHandler* handler = &g.SettingsHandlers[handler_n]; handler->WriteAllFn(&g, handler, &g.SettingsIniData); } if (out_size) *out_size = (size_t)g.SettingsIniData.size(); return g.SettingsIniData.c_str(); } static void* SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) { ImGuiWindowSettings* settings = ImGui::FindWindowSettings(ImHashStr(name)); if (!settings) settings = ImGui::CreateNewWindowSettings(name); return (void*)settings; } static void SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line) { ImGuiWindowSettings* settings = (ImGuiWindowSettings*)entry; float x, y; int i; ImU32 u1; if (sscanf(line, "Pos=%f,%f", &x, &y) == 2) { settings->Pos = ImVec2(x, y); } else if (sscanf(line, "Size=%f,%f", &x, &y) == 2) { settings->Size = ImMax(ImVec2(x, y), GImGui->Style.WindowMinSize); } else if (sscanf(line, "ViewportId=0x%08X", &u1) == 1) { settings->ViewportId = u1; } else if (sscanf(line, "ViewportPos=%f,%f", &x, &y) == 2) { settings->ViewportPos = ImVec2(x, y); } else if (sscanf(line, "Collapsed=%d", &i) == 1) { settings->Collapsed = (i != 0); } else if (sscanf(line, "DockId=0x%X,%d", &u1, &i) == 2) { settings->DockId = u1; settings->DockOrder = (short)i; } else if (sscanf(line, "DockId=0x%X", &u1) == 1) { settings->DockId = u1; settings->DockOrder = -1; } else if (sscanf(line, "ClassId=0x%X", &u1) == 1) { settings->ClassId = u1; } } static void SettingsHandlerWindow_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) { // Gather data from windows that were active during this session // (if a window wasn't opened in this session we preserve its settings) ImGuiContext& g = *imgui_ctx; for (int i = 0; i != g.Windows.Size; i++) { ImGuiWindow* window = g.Windows[i]; if (window->Flags & ImGuiWindowFlags_NoSavedSettings) continue; ImGuiWindowSettings* settings = (window->SettingsIdx != -1) ? &g.SettingsWindows[window->SettingsIdx] : ImGui::FindWindowSettings(window->ID); if (!settings) { settings = ImGui::CreateNewWindowSettings(window->Name); window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings); } IM_ASSERT(settings->ID == window->ID); settings->Pos = window->Pos - window->ViewportPos; settings->Size = window->SizeFull; settings->ViewportId = window->ViewportId; settings->ViewportPos = window->ViewportPos; IM_ASSERT(window->DockNode == NULL || window->DockNode->ID == window->DockId); settings->DockId = window->DockId; settings->ClassId = window->WindowClass.ClassId; settings->DockOrder = window->DockOrder; settings->Collapsed = window->Collapsed; } // Write to text buffer buf->reserve(buf->size() + g.SettingsWindows.Size * 96); // ballpark reserve for (int i = 0; i != g.SettingsWindows.Size; i++) { const ImGuiWindowSettings* settings = &g.SettingsWindows[i]; const char* name = settings->Name; if (const char* p = strstr(name, "###")) // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID() name = p; buf->appendf("[%s][%s]\n", handler->TypeName, name); if (settings->ViewportId != 0 && settings->ViewportId != ImGui::IMGUI_VIEWPORT_DEFAULT_ID) { buf->appendf("ViewportPos=%d,%d\n", (int)settings->ViewportPos.x, (int)settings->ViewportPos.y); buf->appendf("ViewportId=0x%08X\n", settings->ViewportId); } if (settings->Pos.x != 0.0f || settings->Pos.y != 0.0f || settings->ViewportId == ImGui::IMGUI_VIEWPORT_DEFAULT_ID) buf->appendf("Pos=%d,%d\n", (int)settings->Pos.x, (int)settings->Pos.y); if (settings->Size.x != 0.0f || settings->Size.y != 0.0f) buf->appendf("Size=%d,%d\n", (int)settings->Size.x, (int)settings->Size.y); buf->appendf("Collapsed=%d\n", settings->Collapsed); if (settings->DockId != 0) { // Write DockId as 4 digits if possible. Automatic DockId are small numbers, but full explicit DockSpace() are full ImGuiID range. if (settings->DockOrder == -1) buf->appendf("DockId=0x%08X\n", settings->DockId); else buf->appendf("DockId=0x%08X,%d\n", settings->DockId, settings->DockOrder); if (settings->ClassId != 0) buf->appendf("ClassId=0x%08X\n", settings->ClassId); } buf->appendf("\n"); } } //----------------------------------------------------------------------------- // [SECTION] VIEWPORTS, PLATFORM WINDOWS //----------------------------------------------------------------------------- ImGuiViewport* ImGui::GetMainViewport() { ImGuiContext& g = *GImGui; return g.Viewports[0]; } ImGuiViewport* ImGui::FindViewportByID(ImGuiID id) { ImGuiContext& g = *GImGui; for (int n = 0; n < g.Viewports.Size; n++) if (g.Viewports[n]->ID == id) return g.Viewports[n]; return NULL; } ImGuiViewport* ImGui::FindViewportByPlatformHandle(void* platform_handle) { ImGuiContext& g = *GImGui; for (int i = 0; i != g.Viewports.Size; i++) if (g.Viewports[i]->PlatformHandle == platform_handle) return g.Viewports[i]; return NULL; } void ImGui::SetCurrentViewport(ImGuiWindow* current_window, ImGuiViewportP* viewport) { ImGuiContext& g = *GImGui; (void)current_window; if (viewport) viewport->LastFrameActive = g.FrameCount; if (g.CurrentViewport == viewport) return; g.CurrentViewport = viewport; //IMGUI_DEBUG_LOG("SetCurrentViewport changed '%s' 0x%08X\n", current_window ? current_window->Name : NULL, viewport ? viewport->ID : 0); // Notify platform layer of viewport changes // FIXME-DPI: This is only currently used for experimenting with handling of multiple DPI if (g.CurrentViewport && g.PlatformIO.Platform_OnChangedViewport) g.PlatformIO.Platform_OnChangedViewport(g.CurrentViewport); } static void SetWindowViewport(ImGuiWindow* window, ImGuiViewportP* viewport) { window->Viewport = viewport; window->ViewportId = viewport->ID; window->ViewportOwned = (viewport->Window == window); } static bool ImGui::GetWindowAlwaysWantOwnViewport(ImGuiWindow* window) { // 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. ImGuiContext& g = *GImGui; if (g.IO.ConfigViewportsNoAutoMerge && (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)) if (!window->DockIsActive) if ((window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) == 0) return true; return false; } static bool ImGui::UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImGuiViewportP* viewport) { ImGuiContext& g = *GImGui; if (!(viewport->Flags & (ImGuiViewportFlags_CanHostOtherWindows | ImGuiViewportFlags_Minimized)) || window->Viewport == viewport) return false; if (!viewport->GetRect().Contains(window->Rect())) return false; if (GetWindowAlwaysWantOwnViewport(window)) return false; for (int n = 0; n < g.Windows.Size; n++) { ImGuiWindow* window_behind = g.Windows[n]; if (window_behind == window) break; if (window_behind->WasActive && window_behind->ViewportOwned && !(window_behind->Flags & ImGuiWindowFlags_ChildWindow)) if (window_behind->Viewport->GetRect().Overlaps(window->Rect())) return false; } // Move to the existing viewport, Move child/hosted windows as well (FIXME-OPT: iterate child) ImGuiViewportP* old_viewport = window->Viewport; if (window->ViewportOwned) for (int n = 0; n < g.Windows.Size; n++) if (g.Windows[n]->Viewport == old_viewport) SetWindowViewport(g.Windows[n], viewport); SetWindowViewport(window, viewport); BringWindowToDisplayFront(window); return true; } // Scale all windows (position, size). Use when e.g. changing DPI. (This is a lossy operation!) void ImGui::ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale) { ImGuiContext& g = *GImGui; if (viewport->Window) { ScaleWindow(viewport->Window, scale); } else { for (int i = 0; i != g.Windows.Size; i++) if (g.Windows[i]->Viewport == viewport) ScaleWindow(g.Windows[i], scale); } } // If the back-end doesn't set MouseLastHoveredViewport or doesn't honor ImGuiViewportFlags_NoInputs, we do a search ourselves. // A) It won't take account of the possibility that non-imgui windows may be in-between our dragged window and our target window. // B) It requires Platform_GetWindowFocus to be implemented by back-end. static ImGuiViewportP* FindViewportHoveredFromPlatformWindowStack(const ImVec2 mouse_platform_pos) { ImGuiContext& g = *GImGui; ImGuiViewportP* best_candidate = NULL; for (int n = 0; n < g.Viewports.Size; n++) { ImGuiViewportP* viewport = g.Viewports[n]; if (!(viewport->Flags & (ImGuiViewportFlags_NoInputs | ImGuiViewportFlags_Minimized)) && viewport->GetRect().Contains(mouse_platform_pos)) if (best_candidate == NULL || best_candidate->LastFrontMostStampCount < viewport->LastFrontMostStampCount) best_candidate = viewport; } return best_candidate; } static void ImGui::UpdateViewportsNewFrame() { ImGuiContext& g = *GImGui; IM_ASSERT(g.PlatformIO.Viewports.Size <= g.Viewports.Size); // Update Minimized status (we need it first in order to decide if we'll apply Pos/Size of the main viewport) for (int n = 0; n < g.Viewports.Size; n++) { ImGuiViewportP* viewport = g.Viewports[n]; const bool platform_funcs_available = viewport->PlatformWindowCreated; if ((g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)) if (g.PlatformIO.Platform_GetWindowMinimized && platform_funcs_available) { bool minimized = g.PlatformIO.Platform_GetWindowMinimized(viewport); if (minimized) viewport->Flags |= ImGuiViewportFlags_Minimized; else viewport->Flags &= ~ImGuiViewportFlags_Minimized; } } // Create/update main viewport with current platform position and size ImGuiViewportP* main_viewport = g.Viewports[0]; IM_ASSERT(main_viewport->ID == IMGUI_VIEWPORT_DEFAULT_ID); IM_ASSERT(main_viewport->Window == NULL); ImVec2 main_viewport_platform_pos = ImVec2(0.0f, 0.0f); ImVec2 main_viewport_platform_size = g.IO.DisplaySize; if (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable) main_viewport_platform_pos = (main_viewport->Flags & ImGuiViewportFlags_Minimized) ? main_viewport->Pos : g.PlatformIO.Platform_GetWindowPos(main_viewport); AddUpdateViewport(NULL, IMGUI_VIEWPORT_DEFAULT_ID, main_viewport_platform_pos, main_viewport_platform_size, ImGuiViewportFlags_CanHostOtherWindows); g.CurrentViewport = NULL; g.MouseViewport = NULL; for (int n = 0; n < g.Viewports.Size; n++) { // Erase unused viewports ImGuiViewportP* viewport = g.Viewports[n]; viewport->Idx = n; if (n > 0 && viewport->LastFrameActive < g.FrameCount - 2) { // Clear references to this viewport in windows (window->ViewportId becomes the master data) for (int window_n = 0; window_n < g.Windows.Size; window_n++) if (g.Windows[window_n]->Viewport == viewport) { g.Windows[window_n]->Viewport = NULL; g.Windows[window_n]->ViewportOwned = false; } if (viewport == g.MouseLastHoveredViewport) g.MouseLastHoveredViewport = NULL; g.Viewports.erase(g.Viewports.Data + n); // Destroy //IMGUI_DEBUG_LOG("Delete Viewport %08X (%s)\n", viewport->ID, viewport->Window ? viewport->Window->Name : "n/a"); DestroyPlatformWindow(viewport); // In most circumstances the platform window will already be destroyed here. IM_ASSERT(g.PlatformIO.Viewports.contains(viewport) == false); IM_DELETE(viewport); n--; continue; } const bool platform_funcs_available = viewport->PlatformWindowCreated; if ((g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)) { // Update Position and Size (from Platform Window to ImGui) if requested. // We do it early in the frame instead of waiting for UpdatePlatformWindows() to avoid a frame of lag when moving/resizing using OS facilities. if (!(viewport->Flags & ImGuiViewportFlags_Minimized) && platform_funcs_available) { if (viewport->PlatformRequestMove) viewport->Pos = viewport->LastPlatformPos = g.PlatformIO.Platform_GetWindowPos(viewport); if (viewport->PlatformRequestResize) viewport->Size = viewport->LastPlatformSize = g.PlatformIO.Platform_GetWindowSize(viewport); } UpdateViewportPlatformMonitor(viewport); } // Reset alpha every frame. Users of transparency (docking) needs to request a lower alpha back. viewport->Alpha = 1.0f; // Translate imgui windows when a Host Viewport has been moved // (This additionally keeps windows at the same place when ImGuiConfigFlags_ViewportsEnable is toggled!) ImVec2 viewport_delta = viewport->Pos - viewport->LastPos; if ((viewport->Flags & ImGuiViewportFlags_CanHostOtherWindows) && (viewport_delta.x != 0.0f || viewport_delta.y != 0.0f)) for (int window_n = 0; window_n < g.Windows.Size; window_n++) if (g.Windows[window_n]->Viewport == viewport || (g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable) == 0) TranslateWindow(g.Windows[window_n], viewport_delta); // Update DPI scale float new_dpi_scale; if (g.PlatformIO.Platform_GetWindowDpiScale && platform_funcs_available) new_dpi_scale = g.PlatformIO.Platform_GetWindowDpiScale(viewport); else if (viewport->PlatformMonitor != -1) new_dpi_scale = g.PlatformIO.Monitors[viewport->PlatformMonitor].DpiScale; else new_dpi_scale = (viewport->DpiScale != 0.0f) ? viewport->DpiScale : 1.0f; if (viewport->DpiScale != 0.0f && new_dpi_scale != viewport->DpiScale) { float scale_factor = new_dpi_scale / viewport->DpiScale; if (g.IO.ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports) ScaleWindowsInViewport(viewport, scale_factor); //if (viewport == GetMainViewport()) // g.PlatformInterface.SetWindowSize(viewport, viewport->Size * scale_factor); // Scale our window moving pivot so that the window will rescale roughly around the mouse position. // FIXME-VIEWPORT: This currently creates a resizing feedback loop when a window is straddling a DPI transition border. // (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.) //if (g.MovingWindow != NULL && g.MovingWindow->Viewport == viewport) // g.ActiveIdClickOffset = ImFloor(g.ActiveIdClickOffset * scale_factor); } viewport->DpiScale = new_dpi_scale; } if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)) { g.MouseViewport = main_viewport; return; } // Mouse handling: decide on the actual mouse viewport for this frame between the active/focused viewport and the hovered viewport. // Note that 'viewport_hovered' should skip over any viewport that has the ImGuiViewportFlags_NoInputs flags set. ImGuiViewportP* viewport_hovered = NULL; if (g.IO.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport) { viewport_hovered = g.IO.MouseHoveredViewport ? (ImGuiViewportP*)FindViewportByID(g.IO.MouseHoveredViewport) : NULL; if (viewport_hovered && (viewport_hovered->Flags & ImGuiViewportFlags_NoInputs)) { // Back-end failed at honoring its contract if it returned a viewport with the _NoInputs flag. IM_ASSERT(0); viewport_hovered = FindViewportHoveredFromPlatformWindowStack(g.IO.MousePos); } } else { // If the back-end doesn't know how to honor ImGuiViewportFlags_NoInputs, we do a search ourselves. Note that this search: // A) won't take account of the possibility that non-imgui windows may be in-between our dragged window and our target window. // 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) viewport_hovered = FindViewportHoveredFromPlatformWindowStack(g.IO.MousePos); } if (viewport_hovered != NULL) g.MouseLastHoveredViewport = viewport_hovered; else if (g.MouseLastHoveredViewport == NULL) g.MouseLastHoveredViewport = g.Viewports[0]; // Update mouse reference viewport // (when moving a window we aim at its viewport, but this will be overwritten below if we go in drag and drop mode) if (g.MovingWindow) g.MouseViewport = g.MovingWindow->Viewport; else g.MouseViewport = g.MouseLastHoveredViewport; // When dragging something, always refer to the last hovered viewport. // - when releasing a moving window we will revert to aiming behind (at viewport_hovered) // - 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) // - 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. const bool is_mouse_dragging_with_an_expected_destination = g.DragDropActive; if (is_mouse_dragging_with_an_expected_destination && viewport_hovered == NULL) viewport_hovered = g.MouseLastHoveredViewport; if (is_mouse_dragging_with_an_expected_destination || g.ActiveId == 0 || !IsAnyMouseDown()) if (viewport_hovered != NULL && viewport_hovered != g.MouseViewport && !(viewport_hovered->Flags & ImGuiViewportFlags_NoInputs)) g.MouseViewport = viewport_hovered; IM_ASSERT(g.MouseViewport != NULL); } // Update user-facing viewport list (g.Viewports -> g.PlatformIO.Viewports after filtering out some) static void ImGui::UpdateViewportsEndFrame() { ImGuiContext& g = *GImGui; g.PlatformIO.MainViewport = g.Viewports[0]; g.PlatformIO.Viewports.resize(0); for (int i = 0; i < g.Viewports.Size; i++) { ImGuiViewportP* viewport = g.Viewports[i]; viewport->LastPos = viewport->Pos; if (viewport->LastFrameActive < g.FrameCount || viewport->Size.x <= 0.0f || viewport->Size.y <= 0.0f) if (i > 0) // Always include main viewport in the list continue; if (viewport->Window && !IsWindowActiveAndVisible(viewport->Window)) continue; if (i > 0) IM_ASSERT(viewport->Window != NULL); g.PlatformIO.Viewports.push_back(viewport); } g.Viewports[0]->ClearRequestFlags(); // Clear main viewport flags because UpdatePlatformWindows() won't do it and may not even be called } // FIXME: We should ideally refactor the system to call this every frame (we currently don't) ImGuiViewportP* ImGui::AddUpdateViewport(ImGuiWindow* window, ImGuiID id, const ImVec2& pos, const ImVec2& size, ImGuiViewportFlags flags) { ImGuiContext& g = *GImGui; IM_ASSERT(id != 0); if (window != NULL) { if (g.MovingWindow && g.MovingWindow->RootWindow == window) flags |= ImGuiViewportFlags_NoInputs | ImGuiViewportFlags_NoFocusOnAppearing; if ((window->Flags & ImGuiWindowFlags_NoMouseInputs) && (window->Flags & ImGuiWindowFlags_NoNavInputs)) flags |= ImGuiViewportFlags_NoInputs; if (window->Flags & ImGuiWindowFlags_NoFocusOnAppearing) flags |= ImGuiViewportFlags_NoFocusOnAppearing; } ImGuiViewportP* viewport = (ImGuiViewportP*)FindViewportByID(id); if (viewport) { if (!viewport->PlatformRequestMove) viewport->Pos = pos; if (!viewport->PlatformRequestResize) viewport->Size = size; viewport->Flags = flags | (viewport->Flags & ImGuiViewportFlags_Minimized); // Preserve existing flags } else { // New viewport viewport = IM_NEW(ImGuiViewportP)(); viewport->ID = id; viewport->Idx = g.Viewports.Size; viewport->Pos = viewport->LastPos = pos; viewport->Size = size; viewport->Flags = flags; UpdateViewportPlatformMonitor(viewport); g.Viewports.push_back(viewport); //IMGUI_DEBUG_LOG("Add Viewport %08X (%s)\n", id, window->Name); // We normally setup for all viewports in NewFrame() but here need to handle the mid-frame creation of a new viewport. // We need to extend the fullscreen clip rect so the OverlayDrawList clip is correct for that the first frame g.DrawListSharedData.ClipRectFullscreen.x = ImMin(g.DrawListSharedData.ClipRectFullscreen.x, viewport->Pos.x); g.DrawListSharedData.ClipRectFullscreen.y = ImMin(g.DrawListSharedData.ClipRectFullscreen.y, viewport->Pos.y); g.DrawListSharedData.ClipRectFullscreen.z = ImMax(g.DrawListSharedData.ClipRectFullscreen.z, viewport->Pos.x + viewport->Size.x); g.DrawListSharedData.ClipRectFullscreen.w = ImMax(g.DrawListSharedData.ClipRectFullscreen.w, viewport->Pos.y + viewport->Size.y); // Store initial DpiScale before the OS platform window creation, based on expected monitor data. // This is so we can select an appropriate font size on the first frame of our window lifetime if (viewport->PlatformMonitor != -1) viewport->DpiScale = g.PlatformIO.Monitors[viewport->PlatformMonitor].DpiScale; } viewport->Window = window; viewport->LastFrameActive = g.FrameCount; IM_ASSERT(window == NULL || viewport->ID == window->ID); if (window != NULL) window->ViewportOwned = true; return viewport; } // FIXME-VIEWPORT: This is all super messy and ought to be clarified or rewritten. static void ImGui::UpdateSelectWindowViewport(ImGuiWindow* window) { ImGuiContext& g = *GImGui; ImGuiWindowFlags flags = window->Flags; window->ViewportAllowPlatformMonitorExtend = -1; // Restore main viewport if multi-viewport is not supported by the back-end ImGuiViewportP* main_viewport = g.Viewports[0]; if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)) { SetWindowViewport(window, main_viewport); return; } window->ViewportOwned = false; // Appearing popups reset their viewport so they can inherit again if ((flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) && window->Appearing) { window->Viewport = NULL; window->ViewportId = 0; } if (!g.NextWindowData.ViewportCond) { // By default inherit from parent window if (window->Viewport == NULL && window->ParentWindow) window->Viewport = window->ParentWindow->Viewport; // 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 if (window->Viewport == NULL && window->ViewportId != 0) { window->Viewport = (ImGuiViewportP*)FindViewportByID(window->ViewportId); if (window->Viewport == NULL && window->ViewportPos.x != FLT_MAX && window->ViewportPos.y != FLT_MAX) window->Viewport = AddUpdateViewport(window, window->ID, window->ViewportPos, window->Size, ImGuiViewportFlags_None); } } if (g.NextWindowData.ViewportCond) { // Code explicitly request a viewport window->Viewport = (ImGuiViewportP*)FindViewportByID(g.NextWindowData.ViewportId); window->ViewportId = g.NextWindowData.ViewportId; // Store ID even if Viewport isn't resolved yet. } else if ((flags & ImGuiWindowFlags_ChildWindow) || (flags & ImGuiWindowFlags_ChildMenu)) { // Always inherit viewport from parent window window->Viewport = window->ParentWindow->Viewport; } else if (flags & ImGuiWindowFlags_Tooltip) { window->Viewport = g.MouseViewport; } else if (GetWindowAlwaysWantOwnViewport(window)) { window->Viewport = AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_None); } else if (g.MovingWindow && g.MovingWindow->RootWindow == window && IsMousePosValid()) { if (window->Viewport != NULL && window->Viewport->Window == window) window->Viewport = AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_None); } else { // Merge into host viewport? // We cannot test window->ViewportOwned as it set lower in the function. bool try_to_merge_into_host_viewport = (window->Viewport && window == window->Viewport->Window && g.ActiveId == 0); if (try_to_merge_into_host_viewport) UpdateTryMergeWindowIntoHostViewport(window, g.Viewports[0]); } // Fallback to default viewport if (window->Viewport == NULL) window->Viewport = main_viewport; // Mark window as allowed to protrude outside of its viewport and into the current monitor if (flags & (ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) { // We need to take account of the possibility that mouse may become invalid. // Popups/Tooltip always set ViewportAllowPlatformMonitorExtend so GetWindowAllowedExtentRect() will return full monitor bounds. ImVec2 mouse_ref = (flags & ImGuiWindowFlags_Tooltip) ? g.IO.MousePos : g.BeginPopupStack.back().OpenMousePos; bool use_mouse_ref = (g.NavDisableHighlight || !g.NavDisableMouseHover || !g.NavWindow); bool mouse_valid = IsMousePosValid(&mouse_ref); if ((window->Appearing || (flags & ImGuiWindowFlags_Tooltip)) && (!use_mouse_ref || mouse_valid)) window->ViewportAllowPlatformMonitorExtend = FindPlatformMonitorForPos((use_mouse_ref && mouse_valid) ? mouse_ref : NavCalcPreferredRefPos()); else window->ViewportAllowPlatformMonitorExtend = window->Viewport->PlatformMonitor; } else if (window->Viewport && window != window->Viewport->Window && window->Viewport->Window && !(flags & ImGuiWindowFlags_ChildWindow)) { // When called from Begin() we don't have access to a proper version of the Hidden flag yet, so we replicate this code. const bool will_be_visible = (window->DockIsActive && !window->DockTabIsVisible) ? false : true; if ((window->Flags & ImGuiWindowFlags_DockNodeHost) && window->Viewport->LastFrameActive < g.FrameCount && will_be_visible) { // Steal/transfer ownership //IMGUI_DEBUG_LOG("[%05d] Window '%s' steal Viewport %08X from Window '%s'\n", g.FrameCount, window->Name, window->Viewport->ID, window->Viewport->Window->Name); window->Viewport->Window = window; window->Viewport->ID = window->ID; window->Viewport->LastNameHash = 0; } else if (!UpdateTryMergeWindowIntoHostViewport(window, g.Viewports[0])) // Merge? { // New viewport window->Viewport = AddUpdateViewport(window, window->ID, window->Pos, window->Size, ImGuiViewportFlags_NoFocusOnAppearing); } } // Regular (non-child, non-popup) windows by default are also allowed to protrude // Child windows are kept contained within their parent. else if (window->ViewportAllowPlatformMonitorExtend < 0 && (flags & ImGuiWindowFlags_ChildWindow) == 0) window->ViewportAllowPlatformMonitorExtend = window->Viewport->PlatformMonitor; // Update flags window->ViewportOwned = (window == window->Viewport->Window); window->ViewportId = window->Viewport->ID; // If the OS window has a title bar, hide our imgui title bar //if (window->ViewportOwned && !(window->Viewport->Flags & ImGuiViewportFlags_NoDecoration)) // window->Flags |= ImGuiWindowFlags_NoTitleBar; } // Called by user at the end of the main loop, after EndFrame() // This will handle the creation/update of all OS windows via function defined in the ImGuiPlatformIO api. void ImGui::UpdatePlatformWindows() { ImGuiContext& g = *GImGui; IM_ASSERT(g.FrameCountEnded == g.FrameCount && "Forgot to call Render() or EndFrame() before UpdatePlatformWindows()?"); IM_ASSERT(g.FrameCountPlatformEnded < g.FrameCount); g.FrameCountPlatformEnded = g.FrameCount; if (!(g.ConfigFlagsForFrame & ImGuiConfigFlags_ViewportsEnable)) return; // Create/resize/destroy platform windows to match each active viewport. // Skip the main viewport (index 0), which is always fully handled by the application! for (int i = 1; i < g.Viewports.Size; i++) { ImGuiViewportP* viewport = g.Viewports[i]; // Destroy platform window if the viewport hasn't been submitted or if it is hosting a hidden window // (the implicit/fallback Debug##Default window will be registering its viewport then be disabled, causing a dummy DestroyPlatformWindow to be made each frame) bool destroy_platform_window = false; destroy_platform_window |= (viewport->LastFrameActive < g.FrameCount - 1); destroy_platform_window |= (viewport->Window && !IsWindowActiveAndVisible(viewport->Window)); if (destroy_platform_window) { DestroyPlatformWindow(viewport); continue; } // New windows that appears directly in a new viewport won't always have a size on their first frame if (viewport->LastFrameActive < g.FrameCount || viewport->Size.x <= 0 || viewport->Size.y <= 0) continue; // Create window bool is_new_platform_window = (viewport->PlatformWindowCreated == false); if (is_new_platform_window) { //IMGUI_DEBUG_LOG("Create Platform Window %08X (%s)\n", viewport->ID, viewport->Window ? viewport->Window->Name : "n/a"); g.PlatformIO.Platform_CreateWindow(viewport); if (g.PlatformIO.Renderer_CreateWindow != NULL) g.PlatformIO.Renderer_CreateWindow(viewport); viewport->LastNameHash = 0; 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?) viewport->LastRendererSize = viewport->Size; // We don't need to call Renderer_SetWindowSize() as it is expected Renderer_CreateWindow() already did it. viewport->PlatformWindowCreated = true; } // Apply Position and Size (from ImGui to Platform/Renderer back-ends) if ((viewport->LastPlatformPos.x != viewport->Pos.x || viewport->LastPlatformPos.y != viewport->Pos.y) && !viewport->PlatformRequestMove) g.PlatformIO.Platform_SetWindowPos(viewport, viewport->Pos); if ((viewport->LastPlatformSize.x != viewport->Size.x || viewport->LastPlatformSize.y != viewport->Size.y) && !viewport->PlatformRequestResize) g.PlatformIO.Platform_SetWindowSize(viewport, viewport->Size); if ((viewport->LastRendererSize.x != viewport->Size.x || viewport->LastRendererSize.y != viewport->Size.y) && g.PlatformIO.Renderer_SetWindowSize) g.PlatformIO.Renderer_SetWindowSize(viewport, viewport->Size); viewport->LastPlatformPos = viewport->Pos; viewport->LastPlatformSize = viewport->LastRendererSize = viewport->Size; // Update title bar (if it changed) if (ImGuiWindow* window_for_title = GetWindowForTitleDisplay(viewport->Window)) { const char* title_begin = window_for_title->Name; char* title_end = (char*)(intptr_t)FindRenderedTextEnd(title_begin); const ImGuiID title_hash = ImHashStr(title_begin, title_end - title_begin); if (viewport->LastNameHash != title_hash) { char title_end_backup_c = *title_end; *title_end = 0; // Cut existing buffer short instead of doing an alloc/free, no small gain. g.PlatformIO.Platform_SetWindowTitle(viewport, title_begin); *title_end = title_end_backup_c; viewport->LastNameHash = title_hash; } } // Update alpha (if it changed) if (viewport->LastAlpha != viewport->Alpha && g.PlatformIO.Platform_SetWindowAlpha) g.PlatformIO.Platform_SetWindowAlpha(viewport, viewport->Alpha); viewport->LastAlpha = viewport->Alpha; // Optional, general purpose call to allow the back-end to perform general book-keeping even if things haven't changed. if (g.PlatformIO.Platform_UpdateWindow) g.PlatformIO.Platform_UpdateWindow(viewport); if (is_new_platform_window) { // 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) if (g.FrameCount < 3) viewport->Flags |= ImGuiViewportFlags_NoFocusOnAppearing; // Show window g.PlatformIO.Platform_ShowWindow(viewport); // Even without focus, we assume the window becomes front-most. // This is useful for our platform z-order heuristic when io.MouseHoveredViewport is not available. if (viewport->LastFrontMostStampCount != g.ViewportFrontMostStampCount) viewport->LastFrontMostStampCount = ++g.ViewportFrontMostStampCount; } // Clear request flags viewport->ClearRequestFlags(); } // Update our implicit z-order knowledge of platform windows, which is used when the back-end cannot provide io.MouseHoveredViewport. // When setting Platform_GetWindowFocus, it is expected that the platform back-end can handle calls without crashing if it doesn't have data stored. if (g.PlatformIO.Platform_GetWindowFocus != NULL) { ImGuiViewportP* focused_viewport = NULL; for (int n = 0; n < g.Viewports.Size && focused_viewport == NULL; n++) { ImGuiViewportP* viewport = g.Viewports[n]; if (viewport->PlatformWindowCreated) if (g.PlatformIO.Platform_GetWindowFocus(viewport)) focused_viewport = viewport; } if (focused_viewport && g.PlatformLastFocusedViewport != focused_viewport->ID) { if (focused_viewport->LastFrontMostStampCount != g.ViewportFrontMostStampCount) focused_viewport->LastFrontMostStampCount = ++g.ViewportFrontMostStampCount; g.PlatformLastFocusedViewport = focused_viewport->ID; } } } // This is a default/basic function for performing the rendering/swap of multiple Platform Windows. // Custom renderers may prefer to not call this function at all, and instead iterate the publicly exposed platform data and handle rendering/sync themselves. // The Render/Swap functions stored in ImGuiPlatformIO are merely here to allow for this helper to exist, but you can do it yourself: // // ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); // for (int i = 1; i < platform_io.Viewports.Size; i++) // if ((platform_io.Viewports[i]->Flags & ImGuiViewportFlags_Minimized) == 0) // MyRenderFunction(platform_io.Viewports[i], my_args); // for (int i = 1; i < platform_io.Viewports.Size; i++) // if ((platform_io.Viewports[i]->Flags & ImGuiViewportFlags_Minimized) == 0) // MySwapBufferFunction(platform_io.Viewports[i], my_args); // void ImGui::RenderPlatformWindowsDefault(void* platform_render_arg, void* renderer_render_arg) { // Skip the main viewport (index 0), which is always fully handled by the application! ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); for (int i = 1; i < platform_io.Viewports.Size; i++) { ImGuiViewport* viewport = platform_io.Viewports[i]; if (viewport->Flags & ImGuiViewportFlags_Minimized) continue; if (platform_io.Platform_RenderWindow) platform_io.Platform_RenderWindow(viewport, platform_render_arg); if (platform_io.Renderer_RenderWindow) platform_io.Renderer_RenderWindow(viewport, renderer_render_arg); } for (int i = 1; i < platform_io.Viewports.Size; i++) { ImGuiViewport* viewport = platform_io.Viewports[i]; if (viewport->Flags & ImGuiViewportFlags_Minimized) continue; if (platform_io.Platform_SwapBuffers) platform_io.Platform_SwapBuffers(viewport, platform_render_arg); if (platform_io.Renderer_SwapBuffers) platform_io.Renderer_SwapBuffers(viewport, renderer_render_arg); } } static int ImGui::FindPlatformMonitorForPos(const ImVec2& pos) { ImGuiContext& g = *GImGui; for (int monitor_n = 0; monitor_n < g.PlatformIO.Monitors.Size; monitor_n++) { const ImGuiPlatformMonitor& monitor = g.PlatformIO.Monitors[monitor_n]; if (ImRect(monitor.MainPos, monitor.MainPos + monitor.MainSize).Contains(pos)) return monitor_n; } return -1; } // Search for the monitor with the largest intersection area with the given rectangle // We generally try to avoid searching loops but the monitor count should be very small here // FIXME-OPT: We could test the last monitor used for that viewport first.. static int ImGui::FindPlatformMonitorForRect(const ImRect& rect) { ImGuiContext& g = *GImGui; // 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. // This is necessary for tooltips which always resize down to zero at first. const float surface_threshold = ImMax(rect.GetWidth() * rect.GetHeight() * 0.5f, 1.0f); int best_monitor_n = -1; float best_monitor_surface = 0.001f; for (int monitor_n = 0; monitor_n < g.PlatformIO.Monitors.Size && best_monitor_surface < surface_threshold; monitor_n++) { const ImGuiPlatformMonitor& monitor = g.PlatformIO.Monitors[monitor_n]; const ImRect monitor_rect = ImRect(monitor.MainPos, monitor.MainPos + monitor.MainSize); if (monitor_rect.Contains(rect)) return monitor_n; ImRect overlapping_rect = rect; overlapping_rect.ClipWithFull(monitor_rect); float overlapping_surface = overlapping_rect.GetWidth() * overlapping_rect.GetHeight(); if (overlapping_surface < best_monitor_surface) continue; best_monitor_surface = overlapping_surface; best_monitor_n = monitor_n; } return best_monitor_n; } // Update monitor from viewport rectangle (we'll use this info to clamp windows and save windows lost in a removed monitor) static void ImGui::UpdateViewportPlatformMonitor(ImGuiViewportP* viewport) { viewport->PlatformMonitor = (short)FindPlatformMonitorForRect(viewport->GetRect()); } void ImGui::DestroyPlatformWindow(ImGuiViewportP* viewport) { ImGuiContext& g = *GImGui; if (viewport->PlatformWindowCreated) { if (g.PlatformIO.Renderer_DestroyWindow) g.PlatformIO.Renderer_DestroyWindow(viewport); if (g.PlatformIO.Platform_DestroyWindow) g.PlatformIO.Platform_DestroyWindow(viewport); IM_ASSERT(viewport->RendererUserData == NULL && viewport->PlatformUserData == NULL); viewport->PlatformWindowCreated = false; } else { IM_ASSERT(viewport->RendererUserData == NULL && viewport->PlatformUserData == NULL && viewport->PlatformHandle == NULL); } viewport->RendererUserData = viewport->PlatformUserData = viewport->PlatformHandle = NULL; viewport->ClearRequestFlags(); } void ImGui::DestroyPlatformWindows() { // We call the destroy window on every viewport (including the main viewport, index 0) to give a chance to the back-end // to clear any data they may have stored in e.g. PlatformUserData, RendererUserData. // It is convenient for the platform back-end code to store something in the main viewport, in order for e.g. the mouse handling // code to operator a consistent manner. // It is expected that the back-end can handle calls to Renderer_DestroyWindow/Platform_DestroyWindow without // crashing if it doesn't have data stored. ImGuiContext& g = *GImGui; for (int i = 0; i < g.Viewports.Size; i++) DestroyPlatformWindow(g.Viewports[i]); } //----------------------------------------------------------------------------- // [SECTION] DOCKING //----------------------------------------------------------------------------- // Docking: Internal Types // Docking: Forward Declarations // Docking: ImGuiDockContext // Docking: ImGuiDockContext Docking/Undocking functions // Docking: ImGuiDockNode // Docking: ImGuiDockNode Tree manipulation functions // Docking: Public Functions (SetWindowDock, DockSpace) // Docking: Builder Functions // Docking: Begin/End Functions (called from Begin/End) // Docking: Settings //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Docking: Internal Types //----------------------------------------------------------------------------- static float IMGUI_DOCK_SPLITTER_SIZE = 2.0f; enum ImGuiDockRequestType { ImGuiDockRequestType_None = 0, ImGuiDockRequestType_Dock, ImGuiDockRequestType_Undock, ImGuiDockRequestType_Split // Split is the same as Dock but without a DockPayload }; struct ImGuiDockRequest { ImGuiDockRequestType Type; 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) ImGuiDockNode* DockTargetNode; // Destination/Target Node to dock into ImGuiWindow* DockPayload; // Source/Payload window to dock (may be a loose window or a DockNode), [Optional] ImGuiDir DockSplitDir; float DockSplitRatio; bool DockSplitOuter; ImGuiWindow* UndockTargetWindow; ImGuiDockNode* UndockTargetNode; ImGuiDockRequest() { Type = ImGuiDockRequestType_None; DockTargetWindow = DockPayload = UndockTargetWindow = NULL; DockTargetNode = UndockTargetNode = NULL; DockSplitDir = ImGuiDir_None; DockSplitRatio = 0.5f; DockSplitOuter = false; } }; struct ImGuiDockPreviewData { ImGuiDockNode FutureNode; bool IsDropAllowed; bool IsCenterAvailable; bool IsSidesAvailable; // Hold your breath, grammar freaks.. bool IsSplitDirExplicit; // Set when hovered the drop rect (vs. implicit SplitDir==None when hovered the window) ImGuiDockNode* SplitNode; ImGuiDir SplitDir; float SplitRatio; ImRect DropRectsDraw[ImGuiDir_COUNT + 1]; // May be slightly different from hit-testing drop rects used in DockNodeCalcDropRects() ImGuiDockPreviewData() : FutureNode(0) { IsDropAllowed = IsCenterAvailable = IsSidesAvailable = IsSplitDirExplicit = false; SplitNode = NULL; SplitDir = ImGuiDir_None; SplitRatio = 0.f; } }; // Persistent Settings data, stored contiguously in SettingsNodes (sizeof() ~32 bytes) struct ImGuiDockNodeSettings { ImGuiID ID; ImGuiID ParentID; ImGuiID SelectedTabID; signed char SplitAxis; char Depth; char IsDockSpace; char IsCentralNode; char IsHiddenTabBar; ImVec2ih Pos; ImVec2ih Size; ImVec2ih SizeRef; ImGuiDockNodeSettings() { ID = ParentID = SelectedTabID = 0; SplitAxis = ImGuiAxis_None; Depth = 0; IsDockSpace = IsCentralNode = IsHiddenTabBar = 0; } }; struct ImGuiDockContext { ImGuiStorage Nodes; // Map ID -> ImGuiDockNode*: Active nodes ImVector Requests; ImVector SettingsNodes; bool WantFullRebuild; ImGuiDockContext() { WantFullRebuild = false; } }; //----------------------------------------------------------------------------- // Docking: Forward Declarations //----------------------------------------------------------------------------- namespace ImGui { // ImGuiDockContext static ImGuiDockNode* DockContextAddNode(ImGuiContext* ctx, ImGuiID id); static ImGuiID DockContextGenNodeID(ImGuiContext* ctx); static void DockContextRemoveNode(ImGuiContext* ctx, ImGuiDockNode* node, bool merge_sibling_into_parent_node); static void DockContextQueueNotifyRemovedNode(ImGuiContext* ctx, ImGuiDockNode* node); static void DockContextProcessDock(ImGuiContext* ctx, ImGuiDockRequest* req); static void DockContextProcessUndockWindow(ImGuiContext* ctx, ImGuiWindow* window, bool clear_persistent_docking_ref = true); static void DockContextProcessUndockNode(ImGuiContext* ctx, ImGuiDockNode* node); static void DockContextPruneUnusedSettingsNodes(ImGuiContext* ctx); static ImGuiDockNode* DockContextFindNodeByID(ImGuiContext* ctx, ImGuiID id); static void DockContextClearNodes(ImGuiContext* ctx, ImGuiID root_id, bool clear_persistent_docking_refs); // Use root_id==0 to clear all static void DockContextBuildNodesFromSettings(ImGuiContext* ctx, ImGuiDockNodeSettings* node_settings_array, int node_settings_count); static void DockContextBuildAddWindowsToNodes(ImGuiContext* ctx, ImGuiID root_id); // Use root_id==0 to add all // ImGuiDockNode static void DockNodeAddWindow(ImGuiDockNode* node, ImGuiWindow* window, bool add_to_tab_bar); static void DockNodeMoveWindows(ImGuiDockNode* dst_node, ImGuiDockNode* src_node); static void DockNodeMoveChildNodes(ImGuiDockNode* dst_node, ImGuiDockNode* src_node); static void DockNodeApplyPosSizeToWindows(ImGuiDockNode* node); static void DockNodeRemoveWindow(ImGuiDockNode* node, ImGuiWindow* window, ImGuiID save_dock_id); static void DockNodeHideHostWindow(ImGuiDockNode* node); static void DockNodeUpdate(ImGuiDockNode* node); static void DockNodeUpdateVisibleFlagAndInactiveChilds(ImGuiDockNode* node); static void DockNodeUpdateTabBar(ImGuiDockNode* node, ImGuiWindow* host_window); static void DockNodeAddTabBar(ImGuiDockNode* node); static void DockNodeRemoveTabBar(ImGuiDockNode* node); static ImGuiID DockNodeUpdateTabListMenu(ImGuiDockNode* node, ImGuiTabBar* tab_bar); static void DockNodeUpdateVisibleFlag(ImGuiDockNode* node); static void DockNodeStartMouseMovingWindow(ImGuiDockNode* node, ImGuiWindow* window); static bool DockNodeIsDropAllowed(ImGuiWindow* host_window, ImGuiWindow* payload_window); static void DockNodePreviewDockCalc(ImGuiWindow* host_window, ImGuiDockNode* host_node, ImGuiWindow* payload_window, ImGuiDockPreviewData* preview_data, bool is_explicit_target, bool is_outer_docking); static void DockNodePreviewDockRender(ImGuiWindow* host_window, ImGuiDockNode* host_node, ImGuiWindow* payload_window, const ImGuiDockPreviewData* preview_data); static ImRect DockNodeCalcTabBarRect(const ImGuiDockNode* node); static void DockNodeCalcSplitRects(ImVec2& pos_old, ImVec2& size_old, ImVec2& pos_new, ImVec2& size_new, ImGuiDir dir, ImVec2 size_new_desired); static bool DockNodeCalcDropRectsAndTestMousePos(const ImRect& parent, ImGuiDir dir, ImRect& out_draw, bool outer_docking, ImVec2* test_mouse_pos); static const char* DockNodeGetHostWindowTitle(ImGuiDockNode* node, char* buf, int buf_size) { ImFormatString(buf, buf_size, "##DockNode_%02X", node->ID); return buf; } static int DockNodeGetDepth(const ImGuiDockNode* node) { int depth = 0; while (node->ParentNode) { node = node->ParentNode; depth++; } return depth; } static int DockNodeGetTabOrder(ImGuiWindow* window); // ImGuiDockNode tree manipulations static void DockNodeTreeSplit(ImGuiContext* ctx, ImGuiDockNode* parent_node, ImGuiAxis split_axis, int split_first_child, float split_ratio, ImGuiDockNode* new_node); static void DockNodeTreeMerge(ImGuiContext* ctx, ImGuiDockNode* parent_node, ImGuiDockNode* merge_lead_child); static void DockNodeTreeUpdatePosSize(ImGuiDockNode* node, ImVec2 pos, ImVec2 size); static void DockNodeTreeUpdateSplitter(ImGuiDockNode* node); static ImGuiDockNode* DockNodeTreeFindNodeByPos(ImGuiDockNode* node, ImVec2 pos); static ImGuiDockNode* DockNodeTreeFindFallbackLeafNode(ImGuiDockNode* node); // Settings static void DockSettingsRenameNodeReferences(ImGuiID old_node_id, ImGuiID new_node_id); static void DockSettingsRemoveNodeReferences(ImGuiID* node_ids, int node_ids_count); static ImGuiDockNodeSettings* DockSettingsFindNodeSettings(ImGuiContext* ctx, ImGuiID node_id); static void* DockSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name); static void DockSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line); static void DockSettingsHandler_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf); } //----------------------------------------------------------------------------- // Docking: ImGuiDockContext //----------------------------------------------------------------------------- // The lifetime model is different from the one of regular windows: we always create a ImGuiDockNode for each ImGuiDockNodeSettings, // 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. // At boot time only, we run a simple GC to remove nodes that have no references. // Because dock node settings (which are small, contiguous structures) are always mirrored by their corresponding dock nodes (more complete structures), // we can also very easily recreate the nodes from scratch given the settings data (this is what DockContextRebuild() does). // This is convenient as docking reconfiguration can be implemented by mostly poking at the simpler settings data. //----------------------------------------------------------------------------- void ImGui::DockContextInitialize(ImGuiContext* ctx) { ImGuiContext& g = *ctx; IM_ASSERT(g.DockContext == NULL); g.DockContext = IM_NEW(ImGuiDockContext)(); // Add .ini handle for persistent docking data ImGuiSettingsHandler ini_handler; ini_handler.TypeName = "Docking"; ini_handler.TypeHash = ImHashStr("Docking"); ini_handler.ReadOpenFn = DockSettingsHandler_ReadOpen; ini_handler.ReadLineFn = DockSettingsHandler_ReadLine; ini_handler.WriteAllFn = DockSettingsHandler_WriteAll; g.SettingsHandlers.push_back(ini_handler); } void ImGui::DockContextShutdown(ImGuiContext* ctx) { ImGuiContext& g = *ctx; ImGuiDockContext* dc = ctx->DockContext; for (int n = 0; n < dc->Nodes.Data.Size; n++) if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p) IM_DELETE(node); IM_DELETE(g.DockContext); g.DockContext = NULL; } void ImGui::DockContextOnLoadSettings(ImGuiContext* ctx) { ImGuiDockContext* dc = ctx->DockContext; DockContextPruneUnusedSettingsNodes(ctx); DockContextBuildNodesFromSettings(ctx, dc->SettingsNodes.Data, dc->SettingsNodes.Size); } void ImGui::DockContextClearNodes(ImGuiContext* ctx, ImGuiID root_id, bool clear_persistent_docking_references) { IM_ASSERT(ctx == GImGui); DockBuilderRemoveNodeDockedWindows(root_id, clear_persistent_docking_references); DockBuilderRemoveNodeChildNodes(root_id); } // This function also acts as a defacto test to make sure we can rebuild from scratch without a glitch void ImGui::DockContextRebuild(ImGuiContext* ctx) { //IMGUI_DEBUG_LOG("[docking] full rebuild\n"); ImGuiDockContext* dc = ctx->DockContext; SaveIniSettingsToMemory(); ImGuiID root_id = 0; // Rebuild all DockContextClearNodes(ctx, root_id, false); DockContextBuildNodesFromSettings(ctx, dc->SettingsNodes.Data, dc->SettingsNodes.Size); DockContextBuildAddWindowsToNodes(ctx, root_id); } // Docking context update function, called by NewFrame() void ImGui::DockContextUpdateUndocking(ImGuiContext* ctx) { ImGuiContext& g = *ctx; ImGuiDockContext* dc = ctx->DockContext; if (!(g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable)) { if (dc->Nodes.Data.Size > 0 || dc->Requests.Size > 0) DockContextClearNodes(ctx, 0, true); return; } // Setting NoSplit at runtime merges all nodes if (g.IO.ConfigDockingNoSplit) for (int n = 0; n < dc->Nodes.Data.Size; n++) if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p) if (node->IsRootNode() && node->IsSplitNode()) { DockBuilderRemoveNodeChildNodes(node->ID); //dc->WantFullRebuild = true; } // Process full rebuild #if 0 if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_C))) dc->WantFullRebuild = true; #endif if (dc->WantFullRebuild) { DockContextRebuild(ctx); dc->WantFullRebuild = false; } // Process Undocking requests (we need to process them _before_ the UpdateMouseMovingWindowNewFrame call in NewFrame) for (int n = 0; n < dc->Requests.Size; n++) { ImGuiDockRequest* req = &dc->Requests[n]; if (req->Type == ImGuiDockRequestType_Undock && req->UndockTargetWindow) DockContextProcessUndockWindow(ctx, req->UndockTargetWindow); else if (req->Type == ImGuiDockRequestType_Undock && req->UndockTargetNode) DockContextProcessUndockNode(ctx, req->UndockTargetNode); } } // Docking context update function, called by NewFrame() void ImGui::DockContextUpdateDocking(ImGuiContext* ctx) { ImGuiContext& g = *ctx; ImGuiDockContext* dc = ctx->DockContext; if (!(g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable)) return; // Process Docking requests for (int n = 0; n < dc->Requests.Size; n++) if (dc->Requests[n].Type == ImGuiDockRequestType_Dock) DockContextProcessDock(ctx, &dc->Requests[n]); dc->Requests.resize(0); // Create windows for each automatic docking nodes // 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) for (int n = 0; n < dc->Nodes.Data.Size; n++) if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p) if (node->IsRootNode() && !node->IsDockSpace()) DockNodeUpdate(node); } static ImGuiDockNode* ImGui::DockContextFindNodeByID(ImGuiContext* ctx, ImGuiID id) { return (ImGuiDockNode*)ctx->DockContext->Nodes.GetVoidPtr(id); } static ImGuiID ImGui::DockContextGenNodeID(ImGuiContext* ctx) { // Generate an ID for new node (the exact ID value doesn't matter as long as it is not already used) // 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. ImGuiID id = 0x0001; while (DockContextFindNodeByID(ctx, id) != NULL) id++; return id; } static ImGuiDockNode* ImGui::DockContextAddNode(ImGuiContext* ctx, ImGuiID id) { // 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. if (id == 0) id = DockContextGenNodeID(ctx); else IM_ASSERT(DockContextFindNodeByID(ctx, id) == NULL); ImGuiDockNode* node = IM_NEW(ImGuiDockNode)(id); ctx->DockContext->Nodes.SetVoidPtr(node->ID, node); return node; } static void ImGui::DockContextRemoveNode(ImGuiContext* ctx, ImGuiDockNode* node, bool merge_sibling_into_parent_node) { ImGuiContext& g = *ctx; ImGuiDockContext* dc = ctx->DockContext; //printf("[%05d] RemoveNode 0x%04X\n", node->ID); IM_ASSERT(DockContextFindNodeByID(ctx, node->ID) == node); IM_ASSERT(node->ChildNodes[0] == NULL && node->ChildNodes[1] == NULL); IM_ASSERT(node->Windows.Size == 0); if (node->HostWindow) node->HostWindow->DockNodeAsHost = NULL; ImGuiDockNode* parent_node = node->ParentNode; const bool merge = (merge_sibling_into_parent_node && parent_node != NULL); if (merge) { IM_ASSERT(parent_node->ChildNodes[0] == node || parent_node->ChildNodes[1] == node); ImGuiDockNode* sibling_node = (parent_node->ChildNodes[0] == node ? parent_node->ChildNodes[1] : parent_node->ChildNodes[0]); DockNodeTreeMerge(&g, parent_node, sibling_node); } else { for (int n = 0; parent_node && n < IM_ARRAYSIZE(parent_node->ChildNodes); n++) if (parent_node->ChildNodes[n] == node) node->ParentNode->ChildNodes[n] = NULL; dc->Nodes.SetVoidPtr(node->ID, NULL); IM_DELETE(node); } } static int IMGUI_CDECL DockNodeComparerDepthMostFirst(const void* lhs, const void* rhs) { const ImGuiDockNode* a = *(const ImGuiDockNode* const*)lhs; const ImGuiDockNode* b = *(const ImGuiDockNode* const*)rhs; return ImGui::DockNodeGetDepth(b) - ImGui::DockNodeGetDepth(a); } // Pre C++0x doesn't allow us to use a function-local type (without linkage) as template parameter, so we moved this here. struct ImGuiDockContextPruneNodeData { int CountWindows, CountChildWindows, CountChildNodes; ImGuiID RootID; ImGuiDockContextPruneNodeData() { CountWindows = CountChildWindows = CountChildNodes = 0; RootID = 0; } }; // Garbage collect unused nodes (run once at init time) static void ImGui::DockContextPruneUnusedSettingsNodes(ImGuiContext* ctx) { ImGuiContext& g = *ctx; ImGuiDockContext* dc = ctx->DockContext; IM_ASSERT(g.Windows.Size == 0); ImPool pool; pool.Reserve(dc->SettingsNodes.Size); // Count child nodes and compute RootID for (int settings_n = 0; settings_n < dc->SettingsNodes.Size; settings_n++) { ImGuiDockNodeSettings* settings = &dc->SettingsNodes[settings_n]; ImGuiDockContextPruneNodeData* parent_data = settings->ParentID ? pool.GetByKey(settings->ParentID) : 0; pool.GetOrAddByKey(settings->ID)->RootID = parent_data ? parent_data->RootID : settings->ID; if (settings->ParentID) pool.GetOrAddByKey(settings->ParentID)->CountChildNodes++; } // Count reference to dock ids from window settings for (int settings_n = 0; settings_n < g.SettingsWindows.Size; settings_n++) if (ImGuiID dock_id = g.SettingsWindows[settings_n].DockId) if (ImGuiDockContextPruneNodeData* data = pool.GetByKey(dock_id)) { ImGuiDockContextPruneNodeData* data_root = (data->RootID == dock_id) ? data : pool.GetByKey(data->RootID); data->CountWindows++; data_root->CountChildWindows++; } // Prune for (int settings_n = 0; settings_n < dc->SettingsNodes.Size; settings_n++) { ImGuiDockNodeSettings* settings = &dc->SettingsNodes[settings_n]; ImGuiDockContextPruneNodeData* data = pool.GetByKey(settings->ID); if (data->CountWindows > 1) continue; ImGuiDockContextPruneNodeData* data_root = (data->RootID == settings->ID) ? data : pool.GetByKey(data->RootID); bool remove = false; remove |= (data->CountWindows == 1 && settings->ParentID == 0 && data->CountChildNodes == 0 && !settings->IsCentralNode); // Floating root node with only 1 window remove |= (data->CountWindows == 0 && settings->ParentID == 0 && data->CountChildNodes == 0); // Leaf nodes with 0 window remove |= (data_root->CountChildWindows == 0); if (remove) { DockSettingsRemoveNodeReferences(&settings->ID, 1); settings->ID = 0; } } } static void ImGui::DockContextBuildNodesFromSettings(ImGuiContext* ctx, ImGuiDockNodeSettings* node_settings_array, int node_settings_count) { // Build nodes for (int node_n = 0; node_n < node_settings_count; node_n++) { ImGuiDockNodeSettings* settings = &node_settings_array[node_n]; if (settings->ID == 0) continue; ImGuiDockNode* node = DockContextAddNode(ctx, settings->ID); node->ParentNode = settings->ParentID ? DockContextFindNodeByID(ctx, settings->ParentID) : NULL; node->Pos = ImVec2(settings->Pos.x, settings->Pos.y); node->Size = ImVec2(settings->Size.x, settings->Size.y); node->SizeRef = ImVec2(settings->SizeRef.x, settings->SizeRef.y); node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_DockNode; if (node->ParentNode && node->ParentNode->ChildNodes[0] == NULL) node->ParentNode->ChildNodes[0] = node; else if (node->ParentNode && node->ParentNode->ChildNodes[1] == NULL) node->ParentNode->ChildNodes[1] = node; node->SelectedTabID = settings->SelectedTabID; node->SplitAxis = settings->SplitAxis; if (settings->IsDockSpace) node->LocalFlags |= ImGuiDockNodeFlags_DockSpace; if (settings->IsCentralNode) node->LocalFlags |= ImGuiDockNodeFlags_CentralNode; if (settings->IsHiddenTabBar) node->LocalFlags |= ImGuiDockNodeFlags_HiddenTabBar; // Bind host window immediately if it already exist (in case of a rebuild) // This is useful as the RootWindowForTitleBarHighlight links necessary to highlight the currently focused node requires node->HostWindow to be set. char host_window_title[20]; ImGuiDockNode* root_node = DockNodeGetRootNode(node); node->HostWindow = FindWindowByName(DockNodeGetHostWindowTitle(root_node, host_window_title, IM_ARRAYSIZE(host_window_title))); } } void ImGui::DockContextBuildAddWindowsToNodes(ImGuiContext* ctx, ImGuiID root_id) { // Rebind all windows to nodes (they can also lazily rebind but we'll have a visible glitch during the first frame) ImGuiContext& g = *ctx; for (int n = 0; n < g.Windows.Size; n++) { ImGuiWindow* window = g.Windows[n]; if (window->DockId == 0 || window->LastFrameActive < g.FrameCount - 1) continue; if (window->DockNode != NULL) continue; ImGuiDockNode* node = DockContextFindNodeByID(ctx, window->DockId); IM_ASSERT(node != NULL); // This should have been called after DockContextBuildNodesFromSettings() if (root_id == 0 || DockNodeGetRootNode(node)->ID == root_id) DockNodeAddWindow(node, window, true); } } //----------------------------------------------------------------------------- // Docking: ImGuiDockContext Docking/Undocking functions //----------------------------------------------------------------------------- void ImGui::DockContextQueueDock(ImGuiContext* ctx, ImGuiWindow* target, ImGuiDockNode* target_node, ImGuiWindow* payload, ImGuiDir split_dir, float split_ratio, bool split_outer) { IM_ASSERT(target != payload); ImGuiDockRequest req; req.Type = ImGuiDockRequestType_Dock; req.DockTargetWindow = target; req.DockTargetNode = target_node; req.DockPayload = payload; req.DockSplitDir = split_dir; req.DockSplitRatio = split_ratio; req.DockSplitOuter = split_outer; ctx->DockContext->Requests.push_back(req); } void ImGui::DockContextQueueUndockWindow(ImGuiContext* ctx, ImGuiWindow* window) { ImGuiDockRequest req; req.Type = ImGuiDockRequestType_Undock; req.UndockTargetWindow = window; ctx->DockContext->Requests.push_back(req); } void ImGui::DockContextQueueUndockNode(ImGuiContext* ctx, ImGuiDockNode* node) { ImGuiDockRequest req; req.Type = ImGuiDockRequestType_Undock; req.UndockTargetNode = node; ctx->DockContext->Requests.push_back(req); } void ImGui::DockContextQueueNotifyRemovedNode(ImGuiContext* ctx, ImGuiDockNode* node) { ImGuiDockContext* dc = ctx->DockContext; for (int n = 0; n < dc->Requests.Size; n++) if (dc->Requests[n].DockTargetNode == node) dc->Requests[n].Type = ImGuiDockRequestType_None; } void ImGui::DockContextProcessDock(ImGuiContext* ctx, ImGuiDockRequest* req) { IM_ASSERT((req->Type == ImGuiDockRequestType_Dock && req->DockPayload != NULL) || (req->Type == ImGuiDockRequestType_Split && req->DockPayload == NULL)); IM_ASSERT(req->DockTargetWindow != NULL || req->DockTargetNode != NULL); ImGuiContext& g = *ctx; ImGuiWindow* payload_window = req->DockPayload; // Optional ImGuiWindow* target_window = req->DockTargetWindow; ImGuiDockNode* node = req->DockTargetNode; // Decide which Tab will be selected at the end of the operation ImGuiID next_selected_id = 0; ImGuiDockNode* payload_node = NULL; if (payload_window) { payload_node = payload_window->DockNodeAsHost; payload_window->DockNodeAsHost = NULL; // Important to clear this as the node will have its life as a child which might be merged/deleted later. if (payload_node && payload_node->IsLeafNode()) next_selected_id = payload_node->TabBar->NextSelectedTabId ? payload_node->TabBar->NextSelectedTabId : payload_node->TabBar->SelectedTabId; if (payload_node == NULL) next_selected_id = payload_window->ID; } // FIXME-DOCK: When we are trying to dock an existing single-window node into a loose window, transfer Node ID as well // When processing an interactive split, usually LastFrameAlive will be < g.FrameCount. But DockBuilder operations can make it ==. if (node) IM_ASSERT(node->LastFrameAlive <= g.FrameCount); if (node && target_window && node == target_window->DockNodeAsHost) IM_ASSERT(node->Windows.Size > 0 || node->IsSplitNode() || node->IsCentralNode()); // Create new node and add existing window to it if (node == NULL) { node = DockContextAddNode(ctx, 0); node->Pos = target_window->Pos; node->Size = target_window->Size; if (target_window->DockNodeAsHost == NULL) { DockNodeAddWindow(node, target_window, true); node->TabBar->Tabs[0].Flags &= ~ImGuiTabItemFlags_Unsorted; target_window->DockIsActive = true; } } ImGuiDir split_dir = req->DockSplitDir; if (split_dir != ImGuiDir_None) { // Split into one, one side will be our payload node unless we are dropping a loose window const ImGuiAxis split_axis = (split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Right) ? ImGuiAxis_X : ImGuiAxis_Y; 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 const float split_ratio = req->DockSplitRatio; DockNodeTreeSplit(ctx, node, split_axis, split_inheritor_child_idx, split_ratio, payload_node); // payload_node may be NULL here! ImGuiDockNode* new_node = node->ChildNodes[split_inheritor_child_idx ^ 1]; new_node->HostWindow = node->HostWindow; node = new_node; } node->LocalFlags &= ~ImGuiDockNodeFlags_HiddenTabBar; if (node != payload_node) { // 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!) if (node->Windows.Size > 0 && node->TabBar == NULL) { DockNodeAddTabBar(node); for (int n = 0; n < node->Windows.Size; n++) TabBarAddTab(node->TabBar, ImGuiTabItemFlags_None, node->Windows[n]); } if (payload_node != NULL) { // Transfer full payload node (with 1+ child windows or child nodes) if (payload_node->IsSplitNode()) { if (node->Windows.Size > 0) { // 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. // In this situation, we move the windows of the target node into the currently visible node of the payload. // This allows us to preserve some of the underlying dock tree settings nicely. IM_ASSERT(payload_node->OnlyNodeWithWindows != NULL); // The docking should have been blocked by DockNodePreviewDockCalc() early on and never submitted. ImGuiDockNode* visible_node = payload_node->OnlyNodeWithWindows; if (visible_node->TabBar) IM_ASSERT(visible_node->TabBar->Tabs.Size > 0); DockNodeMoveWindows(node, visible_node); DockNodeMoveWindows(visible_node, node); DockSettingsRenameNodeReferences(node->ID, visible_node->ID); } if (node->IsCentralNode()) { // Central node property needs to be moved to a leaf node, pick the last focused one. // FIXME-DOCKING: If we had to transfer other flags here, what would the policy be? ImGuiDockNode* last_focused_node = DockContextFindNodeByID(ctx, payload_node->LastFocusedNodeID); ImGuiDockNode* last_focused_root_node = DockNodeGetRootNode(last_focused_node); IM_ASSERT(last_focused_node != NULL && last_focused_root_node == DockNodeGetRootNode(payload_node)); last_focused_node->LocalFlags |= ImGuiDockNodeFlags_CentralNode; node->LocalFlags &= ~ImGuiDockNodeFlags_CentralNode; last_focused_root_node->CentralNode = last_focused_node; } IM_ASSERT(node->Windows.Size == 0); DockNodeMoveChildNodes(node, payload_node); } else { const ImGuiID payload_dock_id = payload_node->ID; DockNodeMoveWindows(node, payload_node); DockSettingsRenameNodeReferences(payload_dock_id, node->ID); } DockContextRemoveNode(ctx, payload_node, true); } else if (payload_window) { // Transfer single window const ImGuiID payload_dock_id = payload_window->DockId; node->VisibleWindow = payload_window; DockNodeAddWindow(node, payload_window, true); if (payload_dock_id != 0) DockSettingsRenameNodeReferences(payload_dock_id, node->ID); } } // Update selection immediately if (ImGuiTabBar* tab_bar = node->TabBar) tab_bar->NextSelectedTabId = next_selected_id; MarkIniSettingsDirty(); } void ImGui::DockContextProcessUndockWindow(ImGuiContext* ctx, ImGuiWindow* window, bool clear_persistent_docking_ref) { (void)ctx; if (window->DockNode) DockNodeRemoveWindow(window->DockNode, window, clear_persistent_docking_ref ? 0 : window->DockId); else window->DockId = 0; window->Collapsed = false; window->DockIsActive = false; window->DockTabIsVisible = false; MarkIniSettingsDirty(); } void ImGui::DockContextProcessUndockNode(ImGuiContext* ctx, ImGuiDockNode* node) { IM_ASSERT(node->IsLeafNode()); IM_ASSERT(node->Windows.Size >= 1); if (node->IsRootNode() || node->IsCentralNode()) { // 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. ImGuiDockNode* new_node = DockContextAddNode(ctx, 0); DockNodeMoveWindows(new_node, node); DockSettingsRenameNodeReferences(node->ID, new_node->ID); for (int n = 0; n < new_node->Windows.Size; n++) UpdateWindowParentAndRootLinks(new_node->Windows[n], new_node->Windows[n]->Flags, NULL); new_node->WantMouseMove = true; } else { // Otherwise delete the previous node by merging the other sibling back into the parent node. IM_ASSERT(node->ParentNode->ChildNodes[0] == node || node->ParentNode->ChildNodes[1] == node); int index_in_parent = (node->ParentNode->ChildNodes[0] == node) ? 0 : 1; node->ParentNode->ChildNodes[index_in_parent] = NULL; DockNodeTreeMerge(ctx, node->ParentNode, node->ParentNode->ChildNodes[index_in_parent ^ 1]); 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 node->ParentNode = NULL; node->AuthorityForPos = node->AuthorityForSize = ImGuiDataAuthority_Window; node->WantMouseMove = true; } MarkIniSettingsDirty(); } // This is mostly used for automation. bool ImGui::DockContextCalcDropPosForDocking(ImGuiWindow* target, ImGuiDockNode* target_node, ImGuiWindow* payload, ImGuiDir split_dir, bool split_outer, ImVec2* out_pos) { if (split_outer) { IM_ASSERT(0); } else { ImGuiDockPreviewData split_data; DockNodePreviewDockCalc(target, target_node, payload, &split_data, false, split_outer); if (split_data.DropRectsDraw[split_dir+1].IsInverted()) return false; *out_pos = split_data.DropRectsDraw[split_dir+1].GetCenter(); return true; } return false; } //----------------------------------------------------------------------------- // Docking: ImGuiDockNode //----------------------------------------------------------------------------- ImGuiDockNode::ImGuiDockNode(ImGuiID id) { ID = id; SharedFlags = LocalFlags = ImGuiDockNodeFlags_None; ParentNode = ChildNodes[0] = ChildNodes[1] = NULL; TabBar = NULL; SplitAxis = ImGuiAxis_None; HostWindow = VisibleWindow = NULL; CentralNode = OnlyNodeWithWindows = NULL; LastFrameAlive = LastFrameActive = LastFrameFocused = -1; LastFocusedNodeID = 0; SelectedTabID = 0; WantCloseTabID = 0; AuthorityForPos = AuthorityForSize = ImGuiDataAuthority_DockNode; AuthorityForViewport = ImGuiDataAuthority_Auto; IsVisible = true; IsFocused = HasCloseButton = HasCollapseButton = false; WantCloseAll = WantLockSizeOnce = WantMouseMove = WantHiddenTabBarUpdate = WantHiddenTabBarToggle = false; } ImGuiDockNode::~ImGuiDockNode() { IM_DELETE(TabBar); TabBar = NULL; ChildNodes[0] = ChildNodes[1] = NULL; } int ImGui::DockNodeGetTabOrder(ImGuiWindow* window) { ImGuiTabBar* tab_bar = window->DockNode->TabBar; if (tab_bar == NULL) return -1; ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, window->ID); return tab ? tab_bar->GetTabOrder(tab) : -1; } static void ImGui::DockNodeAddWindow(ImGuiDockNode* node, ImGuiWindow* window, bool add_to_tab_bar) { ImGuiContext& g = *GImGui; (void)g; if (window->DockNode) { // Can overwrite an existing window->DockNode (e.g. pointing to a disabled DockSpace node) IM_ASSERT(window->DockNode->ID != node->ID); DockNodeRemoveWindow(window->DockNode, window, 0); } IM_ASSERT(window->DockNode == NULL || window->DockNodeAsHost == NULL); node->Windows.push_back(window); node->WantHiddenTabBarUpdate = true; window->DockNode = node; window->DockId = node->ID; window->DockIsActive = (node->Windows.Size > 1); window->DockTabWantClose = false; // If 2+ windows appeared on the same frame, creating a new DockNode+TabBar from the second window, // then we need to hide the first one after the fact otherwise it would be visible as a standalone window for one frame. if (node->HostWindow == NULL && node->Windows.Size == 2 && node->Windows[0]->WasActive == false) { node->Windows[0]->Hidden = true; node->Windows[0]->HiddenFramesCanSkipItems = 1; } // When reactivating a node with one or two loose window, the window pos/size/viewport are authoritative over the node storage. // In particular it is important we init the viewport from the first window so we don't create two viewports and drop one. if (node->HostWindow == NULL && !node->IsDockSpace() && node->IsRootNode()) { if (node->AuthorityForPos == ImGuiDataAuthority_Auto) node->AuthorityForPos = ImGuiDataAuthority_Window; if (node->AuthorityForSize == ImGuiDataAuthority_Auto) node->AuthorityForSize = ImGuiDataAuthority_Window; if (node->AuthorityForViewport == ImGuiDataAuthority_Auto) node->AuthorityForViewport = ImGuiDataAuthority_Window; } // Add to tab bar if requested if (add_to_tab_bar) { if (node->TabBar == NULL) { DockNodeAddTabBar(node); node->TabBar->SelectedTabId = node->TabBar->NextSelectedTabId = node->SelectedTabID; // Add existing windows for (int n = 0; n < node->Windows.Size - 1; n++) TabBarAddTab(node->TabBar, ImGuiTabItemFlags_None, node->Windows[n]); } TabBarAddTab(node->TabBar, ImGuiTabItemFlags_Unsorted, window); } DockNodeUpdateVisibleFlag(node); // 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. if (node->HostWindow) UpdateWindowParentAndRootLinks(window, window->Flags | ImGuiWindowFlags_ChildWindow, node->HostWindow); } static void ImGui::DockNodeRemoveWindow(ImGuiDockNode* node, ImGuiWindow* window, ImGuiID save_dock_id) { ImGuiContext& g = *GImGui; IM_ASSERT(window->DockNode == node); //IM_ASSERT(window->RootWindow == node->HostWindow); //IM_ASSERT(window->LastFrameActive < g.FrameCount); // We may call this from Begin() IM_ASSERT(save_dock_id == 0 || save_dock_id == node->ID); window->DockNode = NULL; window->DockIsActive = window->DockTabWantClose = false; window->DockId = save_dock_id; UpdateWindowParentAndRootLinks(window, window->Flags & ~ImGuiWindowFlags_ChildWindow, NULL); // Update immediately // Remove window bool erased = false; for (int n = 0; n < node->Windows.Size; n++) if (node->Windows[n] == window) { node->Windows.erase(node->Windows.Data + n); erased = true; break; } IM_ASSERT(erased); if (node->VisibleWindow == window) node->VisibleWindow = NULL; // Remove tab and possibly tab bar node->WantHiddenTabBarUpdate = true; if (node->TabBar) { TabBarRemoveTab(node->TabBar, window->ID); const int tab_count_threshold_for_tab_bar = node->IsCentralNode() ? 1 : 2; if (node->Windows.Size < tab_count_threshold_for_tab_bar) DockNodeRemoveTabBar(node); } if (node->Windows.Size == 0 && !node->IsCentralNode() && !node->IsDockSpace() && window->DockId != node->ID) { // Automatic dock node delete themselves if they are not holding at least one tab DockContextRemoveNode(&g, node, true); return; } if (node->Windows.Size == 1 && !node->IsCentralNode() && node->HostWindow) { ImGuiWindow* remaining_window = node->Windows[0]; if (node->HostWindow->ViewportOwned && node->IsRootNode()) { // Transfer viewport back to the remaining loose window IM_ASSERT(node->HostWindow->Viewport->Window == node->HostWindow); node->HostWindow->Viewport->Window = remaining_window; node->HostWindow->Viewport->ID = remaining_window->ID; } remaining_window->Collapsed = node->HostWindow->Collapsed; } // Update visibility immediately is required so the DockNodeUpdateRemoveInactiveChilds() processing can reflect changes up the tree DockNodeUpdateVisibleFlag(node); } static void ImGui::DockNodeMoveChildNodes(ImGuiDockNode* dst_node, ImGuiDockNode* src_node) { IM_ASSERT(dst_node->Windows.Size == 0); dst_node->ChildNodes[0] = src_node->ChildNodes[0]; dst_node->ChildNodes[1] = src_node->ChildNodes[1]; if (dst_node->ChildNodes[0]) dst_node->ChildNodes[0]->ParentNode = dst_node; if (dst_node->ChildNodes[1]) dst_node->ChildNodes[1]->ParentNode = dst_node; dst_node->SplitAxis = src_node->SplitAxis; dst_node->SizeRef = src_node->SizeRef; src_node->ChildNodes[0] = src_node->ChildNodes[1] = NULL; } static void ImGui::DockNodeMoveWindows(ImGuiDockNode* dst_node, ImGuiDockNode* src_node) { // Insert tabs in the same orders as currently ordered (node->Windows isn't ordered) IM_ASSERT(src_node && dst_node && dst_node != src_node); ImGuiTabBar* src_tab_bar = src_node->TabBar; if (src_tab_bar != NULL) IM_ASSERT(src_node->Windows.Size == src_node->TabBar->Tabs.Size); // If the dst_node is empty we can just move the entire tab bar (to preserve selection, scrolling, etc.) bool move_tab_bar = (src_tab_bar != NULL) && (dst_node->TabBar == NULL); if (move_tab_bar) { dst_node->TabBar = src_node->TabBar; src_node->TabBar = NULL; } for (int n = 0; n < src_node->Windows.Size; n++) { ImGuiWindow* window = src_tab_bar ? src_tab_bar->Tabs[n].Window : src_node->Windows[n]; window->DockNode = NULL; window->DockIsActive = false; DockNodeAddWindow(dst_node, window, move_tab_bar ? false : true); } src_node->Windows.clear(); if (!move_tab_bar && src_node->TabBar) { if (dst_node->TabBar) dst_node->TabBar->SelectedTabId = src_node->TabBar->SelectedTabId; DockNodeRemoveTabBar(src_node); } } static void ImGui::DockNodeApplyPosSizeToWindows(ImGuiDockNode* node) { for (int n = 0; n < node->Windows.Size; 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 SetWindowSize(node->Windows[n], node->Size, ImGuiCond_Always); } } static void ImGui::DockNodeHideHostWindow(ImGuiDockNode* node) { if (node->HostWindow) { if (node->HostWindow->DockNodeAsHost == node) node->HostWindow->DockNodeAsHost = NULL; node->HostWindow = NULL; } if (node->Windows.Size == 1) { node->VisibleWindow = node->Windows[0]; node->Windows[0]->DockIsActive = false; } if (node->TabBar) DockNodeRemoveTabBar(node); } // Search function called once by root node in DockNodeUpdate() struct ImGuiDockNodeFindInfoResults { ImGuiDockNode* CentralNode; ImGuiDockNode* FirstNodeWithWindows; int CountNodesWithWindows; //ImGuiWindowClass WindowClassForMerges; ImGuiDockNodeFindInfoResults() { CentralNode = FirstNodeWithWindows = NULL; CountNodesWithWindows = 0; } }; static void DockNodeFindInfo(ImGuiDockNode* node, ImGuiDockNodeFindInfoResults* results) { if (node->Windows.Size > 0) { if (results->FirstNodeWithWindows == NULL) results->FirstNodeWithWindows = node; results->CountNodesWithWindows++; } if (node->IsCentralNode()) { IM_ASSERT(results->CentralNode == NULL); // Should be only one IM_ASSERT(node->IsLeafNode() && "If you get this assert: please submit .ini file + repro of actions leading to this."); results->CentralNode = node; } if (results->CountNodesWithWindows > 1 && results->CentralNode != NULL) return; if (node->ChildNodes[0]) DockNodeFindInfo(node->ChildNodes[0], results); if (node->ChildNodes[1]) DockNodeFindInfo(node->ChildNodes[1], results); } // - Remove inactive windows/nodes. // - Update visibility flag. static void ImGui::DockNodeUpdateVisibleFlagAndInactiveChilds(ImGuiDockNode* node) { ImGuiContext& g = *GImGui; IM_ASSERT(node->ParentNode == NULL || node->ParentNode->ChildNodes[0] == node || node->ParentNode->ChildNodes[1] == node); // Inherit most flags if (node->ParentNode) node->SharedFlags = node->ParentNode->SharedFlags & ImGuiDockNodeFlags_SharedFlagsInheritMask_; // Recurse into children // There is the possibility that one of our child becoming empty will delete itself and moving its sibling contents into 'node'. // If 'node->ChildNode[0]' delete itself, then 'node->ChildNode[1]->Windows' will be moved into 'node' // 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) if (node->ChildNodes[0]) DockNodeUpdateVisibleFlagAndInactiveChilds(node->ChildNodes[0]); if (node->ChildNodes[1]) DockNodeUpdateVisibleFlagAndInactiveChilds(node->ChildNodes[1]); // Remove inactive windows for (int window_n = 0; window_n < node->Windows.Size; window_n++) { ImGuiWindow* window = node->Windows[window_n]; IM_ASSERT(window->DockNode == node); bool node_was_active = (node->LastFrameActive + 1 == g.FrameCount); bool remove = false; remove |= node_was_active && (window->LastFrameActive + 1 < g.FrameCount); remove |= node_was_active && (node->WantCloseAll || node->WantCloseTabID == window->ID) && window->HasCloseButton && !(window->Flags & ImGuiWindowFlags_UnsavedDocument); // Submit all _expected_ closure from last frame remove |= (window->DockTabWantClose); if (!remove) continue; window->DockTabWantClose = false; if (node->Windows.Size == 1 && !node->IsCentralNode()) { DockNodeHideHostWindow(node); DockNodeRemoveWindow(node, window, node->ID); // Will delete the node so it'll be invalid on return return; } DockNodeRemoveWindow(node, window, node->ID); window_n--; } // Auto-hide tab bar option ImGuiDockNodeFlags node_flags = node->GetMergedFlags(); if (node->WantHiddenTabBarUpdate && node->Windows.Size == 1 && (node_flags & ImGuiDockNodeFlags_AutoHideTabBar) && !node->IsHiddenTabBar()) node->WantHiddenTabBarToggle = true; node->WantHiddenTabBarUpdate = false; // Apply toggles at a single point of the frame (here!) if (node->Windows.Size > 1) node->LocalFlags &= ~ImGuiDockNodeFlags_HiddenTabBar; else if (node->WantHiddenTabBarToggle) node->LocalFlags ^= ImGuiDockNodeFlags_HiddenTabBar; node->WantHiddenTabBarToggle = false; DockNodeUpdateVisibleFlag(node); } static void ImGui::DockNodeUpdateVisibleFlag(ImGuiDockNode* node) { // Update visibility flag bool is_visible = (node->ParentNode == NULL) ? node->IsDockSpace() : node->IsCentralNode(); is_visible |= (node->Windows.Size > 0); is_visible |= (node->ChildNodes[0] && node->ChildNodes[0]->IsVisible); is_visible |= (node->ChildNodes[1] && node->ChildNodes[1]->IsVisible); node->IsVisible = is_visible; } static void ImGui::DockNodeStartMouseMovingWindow(ImGuiDockNode* node, ImGuiWindow* window) { ImGuiContext& g = *GImGui; IM_ASSERT(node->WantMouseMove == true); ImVec2 backup_active_click_offset = g.ActiveIdClickOffset; StartMouseMovingWindow(window); g.MovingWindow = window; // If we are docked into a non moveable root window, StartMouseMovingWindow() won't set g.MovingWindow. Override that decision. node->WantMouseMove = false; g.ActiveIdClickOffset = backup_active_click_offset; } static void ImGui::DockNodeUpdate(ImGuiDockNode* node) { ImGuiContext& g = *GImGui; IM_ASSERT(node->LastFrameActive != g.FrameCount); node->LastFrameAlive = g.FrameCount; node->CentralNode = node->OnlyNodeWithWindows = NULL; if (node->IsRootNode()) { DockNodeUpdateVisibleFlagAndInactiveChilds(node); // FIXME-DOCK: Merge this scan into the one above. // - Setup central node pointers // - 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!) ImGuiDockNodeFindInfoResults results; DockNodeFindInfo(node, &results); node->CentralNode = results.CentralNode; node->OnlyNodeWithWindows = (results.CountNodesWithWindows == 1) ? results.FirstNodeWithWindows : NULL; if (node->LastFocusedNodeID == 0 && results.FirstNodeWithWindows != NULL) node->LastFocusedNodeID = results.FirstNodeWithWindows->ID; // Copy the window class from of our first window so it can be used for proper dock filtering. // When node has mixed windows, prioritize the class with the most constraint (DockingAllowUnclassed = false) as the reference to copy. // FIXME-DOCK: We don't recurse properly, this code could be reworked to work from DockNodeUpdateScanRec. if (ImGuiDockNode* first_node_with_windows = results.FirstNodeWithWindows) { node->WindowClass = first_node_with_windows->Windows[0]->WindowClass; for (int n = 1; n < first_node_with_windows->Windows.Size; n++) if (first_node_with_windows->Windows[n]->WindowClass.DockingAllowUnclassed == false) { node->WindowClass = first_node_with_windows->Windows[n]->WindowClass; break; } } } // Remove tab bar if not needed if (node->TabBar && node->IsNoTabBar()) DockNodeRemoveTabBar(node); // 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) if (node->Windows.Size <= 1 && node->IsRootNode() && node->IsLeafNode() && !node->IsDockSpace() && !g.IO.ConfigDockingTabBarOnSingleWindows) { if (node->Windows.Size == 1) { // Floating window pos/size is authoritative ImGuiWindow* single_window = node->Windows[0]; node->Pos = single_window->Pos; node->Size = single_window->SizeFull; node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_Window; // Transfer focus immediately so when we revert to a regular window it is immediately selected if (node->HostWindow && g.NavWindow == node->HostWindow) FocusWindow(single_window); if (node->HostWindow) { single_window->Viewport = node->HostWindow->Viewport; single_window->ViewportId = node->HostWindow->ViewportId; if (node->HostWindow->ViewportOwned) { single_window->Viewport->Window = single_window; single_window->ViewportOwned = true; } } } DockNodeHideHostWindow(node); node->WantCloseAll = false; node->WantCloseTabID = 0; node->HasCloseButton = node->HasCollapseButton = false; node->LastFrameActive = g.FrameCount; if (node->WantMouseMove && node->Windows.Size == 1) DockNodeStartMouseMovingWindow(node, node->Windows[0]); return; } ImGuiWindow* host_window = NULL; bool beginned_into_host_window = false; if (node->IsDockSpace()) { // [Explicit root dockspace node] IM_ASSERT(node->HostWindow); node->HasCloseButton = false; node->HasCollapseButton = true; host_window = node->HostWindow; } else { // [Automatic root or child nodes] node->HasCloseButton = false; node->HasCollapseButton = (node->Windows.Size > 0); for (int window_n = 0; window_n < node->Windows.Size; window_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. ImGuiWindow* window = node->Windows[window_n]; window->DockIsActive = (node->Windows.Size > 1); node->HasCloseButton |= window->HasCloseButton; } if (node->IsRootNode() && node->IsVisible) { ImGuiWindow* ref_window = (node->Windows.Size > 0) ? node->Windows[0] : NULL; // Sync Pos if (node->AuthorityForPos == ImGuiDataAuthority_Window && ref_window) SetNextWindowPos(ref_window->Pos); else if (node->AuthorityForPos == ImGuiDataAuthority_DockNode) SetNextWindowPos(node->Pos); // Sync Size if (node->AuthorityForSize == ImGuiDataAuthority_Window && ref_window) SetNextWindowSize(ref_window->SizeFull); else if (node->AuthorityForSize == ImGuiDataAuthority_DockNode) SetNextWindowSize(node->Size); // Sync Collapsed if (node->AuthorityForSize == ImGuiDataAuthority_Window && ref_window) SetNextWindowCollapsed(ref_window->Collapsed); // Sync Viewport if (node->AuthorityForViewport == ImGuiDataAuthority_Window && ref_window) SetNextWindowViewport(ref_window->ViewportId); SetNextWindowClass(&node->WindowClass); // Begin into the host window char window_label[20]; DockNodeGetHostWindowTitle(node, window_label, IM_ARRAYSIZE(window_label)); ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_DockNodeHost; window_flags |= ImGuiWindowFlags_NoFocusOnAppearing; window_flags |= ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus | ImGuiWindowFlags_NoCollapse; window_flags |= ImGuiWindowFlags_NoTitleBar; PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); Begin(window_label, NULL, window_flags); PopStyleVar(); beginned_into_host_window = true; node->HostWindow = host_window = g.CurrentWindow; host_window->DockNodeAsHost = node; host_window->DC.CursorPos = host_window->Pos; node->Pos = host_window->Pos; node->Size = host_window->Size; // We set ImGuiWindowFlags_NoFocusOnAppearing because we don't want the host window to take full focus (e.g. steal NavWindow) // But we still it bring it to the front of display. There's no way to choose this precise behavior via window flags. // 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. // 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 // 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 // after the dock host window, losing their top-most status. if (node->HostWindow->Appearing) BringWindowToDisplayFront(node->HostWindow); node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_Auto; } else if (node->ParentNode) { node->HostWindow = host_window = node->ParentNode->HostWindow; node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_Auto; } if (node->WantMouseMove && node->HostWindow) DockNodeStartMouseMovingWindow(node, node->HostWindow); } // Update focused node (the one whose title bar is highlight) within a node tree if (node->IsSplitNode()) IM_ASSERT(node->TabBar == NULL); if (node->IsRootNode()) if (g.NavWindow && g.NavWindow->RootWindowDockStop->DockNode && g.NavWindow->RootWindowDockStop->ParentWindow == host_window) node->LastFocusedNodeID = g.NavWindow->RootWindowDockStop->DockNode->ID; // 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 // processing the resizing splitters. So we are using the DrawList channel splitting facility to submit drawing primitives out of order! const ImGuiDockNodeFlags node_flags = node->GetMergedFlags(); const bool render_dockspace_bg = node->IsRootNode() && host_window && (node_flags & ImGuiDockNodeFlags_PassthruCentralNode) != 0; if (render_dockspace_bg) { host_window->DrawList->ChannelsSplit(2); host_window->DrawList->ChannelsSetCurrent(1); } // Register a hit-test hole in the window unless we are currently dragging a window that is compatible with our dockspace const ImGuiDockNode* central_node = node->CentralNode; const bool central_node_hole = node->IsRootNode() && host_window && (node_flags & ImGuiDockNodeFlags_PassthruCentralNode) != 0 && central_node != NULL && central_node->IsEmpty(); bool central_node_hole_register_hit_test_hole = central_node_hole; if (central_node_hole) if (const ImGuiPayload* payload = ImGui::GetDragDropPayload()) if (payload->IsDataType(IMGUI_PAYLOAD_TYPE_WINDOW) && DockNodeIsDropAllowed(host_window, *(ImGuiWindow**)payload->Data)) central_node_hole_register_hit_test_hole = false; if (central_node_hole_register_hit_test_hole) { // Add a little padding to match the "resize from edges" behavior and allow grabbing the splitter easily. 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 ImRect central_hole(central_node->Pos, central_node->Pos + central_node->Size); central_hole.Expand(ImVec2(-WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS, -WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS)); if (central_node_hole && !central_hole.IsInverted()) { SetWindowHitTestHole(host_window, central_hole.Min, central_hole.Max - central_hole.Min); SetWindowHitTestHole(host_window->ParentWindow, central_hole.Min, central_hole.Max - central_hole.Min); } } // Update position/size, process and draw resizing splitters if (node->IsRootNode() && host_window) { DockNodeTreeUpdatePosSize(node, host_window->Pos, host_window->Size); DockNodeTreeUpdateSplitter(node); } // Draw empty node background (currently can only be the Central Node) if (host_window && node->IsEmpty() && node->IsVisible && !(node_flags & ImGuiDockNodeFlags_PassthruCentralNode)) host_window->DrawList->AddRectFilled(node->Pos, node->Pos + node->Size, GetColorU32(ImGuiCol_DockingEmptyBg)); // Draw whole dockspace background if ImGuiDockNodeFlags_PassthruCentralNode if set. if (render_dockspace_bg && node->IsVisible) { host_window->DrawList->ChannelsSetCurrent(0); if (central_node_hole) RenderRectFilledWithHole(host_window->DrawList, node->Rect(), central_node->Rect(), GetColorU32(ImGuiCol_WindowBg), 0.0f); else host_window->DrawList->AddRectFilled(node->Pos, node->Pos + node->Size, GetColorU32(ImGuiCol_WindowBg), 0.0f); host_window->DrawList->ChannelsMerge(); } // Draw and populate Tab Bar if (host_window && node->Windows.Size > 0) { DockNodeUpdateTabBar(node, host_window); } else { node->WantCloseAll = false; node->WantCloseTabID = 0; node->IsFocused = false; } if (node->TabBar && node->TabBar->SelectedTabId) node->SelectedTabID = node->TabBar->SelectedTabId; else if (node->Windows.Size > 0) node->SelectedTabID = node->Windows[0]->ID; // Draw payload drop target if (host_window && node->IsVisible) if (node->IsRootNode() && (g.MovingWindow == NULL || g.MovingWindow->RootWindow != host_window)) BeginAsDockableDragDropTarget(host_window); node->LastFrameActive = g.FrameCount; // Recurse into children if (host_window) { if (node->ChildNodes[0]) DockNodeUpdate(node->ChildNodes[0]); if (node->ChildNodes[1]) DockNodeUpdate(node->ChildNodes[1]); // Render outer borders last (after the tab bar) if (node->IsRootNode()) RenderOuterBorders(host_window); } // End host window if (beginned_into_host_window) //-V1020 End(); } // 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. static int IMGUI_CDECL TabItemComparerByDockOrder(const void* lhs, const void* rhs) { ImGuiWindow* a = ((const ImGuiTabItem*)lhs)->Window; ImGuiWindow* b = ((const ImGuiTabItem*)rhs)->Window; if (int d = ((a->DockOrder == -1) ? INT_MAX : a->DockOrder) - ((b->DockOrder == -1) ? INT_MAX : b->DockOrder)) return d; return (a->BeginOrderWithinContext - b->BeginOrderWithinContext); } static ImGuiID ImGui::DockNodeUpdateTabListMenu(ImGuiDockNode* node, ImGuiTabBar* tab_bar) { // Try to position the menu so it is more likely to stays within the same viewport ImGuiID ret_tab_id = 0; SetNextWindowPos(ImVec2(node->Pos.x, node->Pos.y + GetFrameHeight())); if (BeginPopup("#TabListMenu")) { node->IsFocused = true; if (tab_bar->Tabs.Size == 1) { if (MenuItem("Hide tab bar", NULL, node->IsHiddenTabBar())) node->WantHiddenTabBarToggle = true; } else { for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; IM_ASSERT(tab->Window != NULL); if (Selectable(tab->Window->Name, tab->ID == tab_bar->SelectedTabId)) ret_tab_id = tab->ID; SameLine(); Text(" "); } } EndPopup(); } return ret_tab_id; } static void ImGui::DockNodeUpdateTabBar(ImGuiDockNode* node, ImGuiWindow* host_window) { ImGuiContext& g = *GImGui; ImGuiStyle& style = g.Style; const bool node_was_active = (node->LastFrameActive + 1 == g.FrameCount); const bool closed_all = node->WantCloseAll && node_was_active; const ImGuiID closed_one = node->WantCloseTabID && node_was_active; node->WantCloseAll = false; node->WantCloseTabID = 0; // Decide if we should use a focused title bar color bool is_focused = false; ImGuiDockNode* root_node = DockNodeGetRootNode(node); if (g.NavWindowingTarget) is_focused = (g.NavWindowingTarget->DockNode == node); else if (g.NavWindow && g.NavWindow->RootWindowForTitleBarHighlight == host_window->RootWindow && root_node->LastFocusedNodeID == node->ID) is_focused = true; // Hidden tab bar will show a triangle on the upper-left (in Begin) if (node->IsHiddenTabBar() || node->IsNoTabBar()) { node->VisibleWindow = (node->Windows.Size > 0) ? node->Windows[0] : NULL; node->IsFocused = is_focused; if (is_focused) node->LastFrameFocused = g.FrameCount; if (node->VisibleWindow) { // Notify root of visible window (used to display title in OS task bar) if (is_focused || root_node->VisibleWindow == NULL) root_node->VisibleWindow = node->VisibleWindow; if (node->TabBar) node->TabBar->VisibleTabId = node->VisibleWindow->ID; } return; } // 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 bool backup_skip_item = host_window->SkipItems; if (!node->IsDockSpace()) { host_window->SkipItems = false; host_window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; host_window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); } PushID(node->ID); ImGuiTabBar* tab_bar = node->TabBar; bool tab_bar_is_recreated = (tab_bar == NULL); // Tab bar are automatically destroyed when a node gets hidden if (tab_bar == NULL) { DockNodeAddTabBar(node); tab_bar = node->TabBar; } ImGuiID focus_tab_id = 0; node->IsFocused = is_focused; // Collapse button changes shape and display a list if (IsPopupOpen("#TabListMenu")) { if (ImGuiID tab_id = DockNodeUpdateTabListMenu(node, tab_bar)) focus_tab_id = tab_bar->NextSelectedTabId = tab_id; is_focused |= node->IsFocused; } // Title bar if (is_focused) node->LastFrameFocused = g.FrameCount; ImRect title_bar_rect = ImRect(node->Pos, node->Pos + ImVec2(node->Size.x, g.FontSize + style.FramePadding.y * 2.0f)); ImU32 title_bar_col = GetColorU32(host_window->Collapsed ? ImGuiCol_TitleBgCollapsed : is_focused ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg); host_window->DrawList->AddRectFilled(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, host_window->WindowRounding, ImDrawCornerFlags_Top); // Collapse button if (CollapseButton(host_window->GetID("#COLLAPSE"), title_bar_rect.Min, node)) OpenPopup("#TabListMenu"); if (IsItemActive()) focus_tab_id = tab_bar->SelectedTabId; // 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. const int tabs_count_old = tab_bar->Tabs.Size; for (int window_n = 0; window_n < node->Windows.Size; window_n++) { ImGuiWindow* window = node->Windows[window_n]; if (g.NavWindow && g.NavWindow->RootWindowDockStop == window) tab_bar->SelectedTabId = window->ID; if (TabBarFindTabByID(tab_bar, window->ID) == NULL) TabBarAddTab(tab_bar, ImGuiTabItemFlags_Unsorted, window); } // If multiple tabs are appearing on the same frame, sort them based on their persistent DockOrder value int tabs_unsorted_start = tab_bar->Tabs.Size; for (int tab_n = tab_bar->Tabs.Size - 1; tab_n >= 0 && (tab_bar->Tabs[tab_n].Flags & ImGuiTabItemFlags_Unsorted); tab_n--) { tab_bar->Tabs[tab_n].Flags &= ~ImGuiTabItemFlags_Unsorted; tabs_unsorted_start = tab_n; } //printf("[%05d] Sorting %d new appearing tabs\n", g.FrameCount, tab_bar->Tabs.Size - tabs_unsorted_start); if (tab_bar->Tabs.Size > tabs_unsorted_start + 1) ImQsort(tab_bar->Tabs.Data + tabs_unsorted_start, tab_bar->Tabs.Size - tabs_unsorted_start, sizeof(ImGuiTabItem), TabItemComparerByDockOrder); // Selected newly added tabs, or persistent tab ID if the tab bar was just recreated if (tab_bar_is_recreated && TabBarFindTabByID(tab_bar, node->SelectedTabID) != NULL) tab_bar->SelectedTabId = tab_bar->NextSelectedTabId = node->SelectedTabID; else if (tab_bar->Tabs.Size > tabs_count_old) tab_bar->SelectedTabId = tab_bar->NextSelectedTabId = tab_bar->Tabs.back().Window->ID; // Begin tab bar const ImRect tab_bar_rect = DockNodeCalcTabBarRect(node); ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_AutoSelectNewTabs; // | ImGuiTabBarFlags_NoTabListScrollingButtons); tab_bar_flags |= ImGuiTabBarFlags_SaveSettings | ImGuiTabBarFlags_DockNode; if (!host_window->Collapsed && is_focused) tab_bar_flags |= ImGuiTabBarFlags_IsFocused; BeginTabBarEx(tab_bar, tab_bar_rect, tab_bar_flags, node); //host_window->DrawList->AddRect(tab_bar_rect.Min, tab_bar_rect.Max, IM_COL32(255,0,255,255)); // Submit actual tabs node->VisibleWindow = NULL; for (int window_n = 0; window_n < node->Windows.Size; window_n++) { ImGuiWindow* window = node->Windows[window_n]; if ((closed_all || closed_one == window->ID) && window->HasCloseButton && !(window->Flags & ImGuiWindowFlags_UnsavedDocument)) continue; if (window->LastFrameActive + 1 >= g.FrameCount || !node_was_active) { ImGuiTabItemFlags tab_item_flags = 0; if (window->Flags & ImGuiWindowFlags_UnsavedDocument) tab_item_flags |= ImGuiTabItemFlags_UnsavedDocument; if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton) tab_item_flags |= ImGuiTabItemFlags_NoCloseWithMiddleMouseButton; bool tab_open = true; TabItemEx(tab_bar, window->Name, window->HasCloseButton ? &tab_open : NULL, tab_item_flags, window); if (!tab_open) node->WantCloseTabID = window->ID; if (tab_bar->VisibleTabId == window->ID) node->VisibleWindow = window; // Store last item data so it can be queried with IsItemXXX functions after the user Begin() call window->DockTabItemStatusFlags = host_window->DC.LastItemStatusFlags; window->DockTabItemRect = host_window->DC.LastItemRect; // Update navigation ID on menu layer if (g.NavWindow && g.NavWindow->RootWindowDockStop == window && (window->DC.NavLayerActiveMask & (1 << 1)) == 0) host_window->NavLastIds[1] = window->ID; } } // Notify root of visible window (used to display title in OS task bar) if (node->VisibleWindow) if (is_focused || root_node->VisibleWindow == NULL) root_node->VisibleWindow = node->VisibleWindow; // Close button (after VisibleWindow was updated) // Note that VisibleWindow may have been overrided by CTRL+Tabbing, so VisibleWindow->ID may be != from tab_bar->SelectedTabId if (node->VisibleWindow) { if (!node->VisibleWindow->HasCloseButton) { PushItemFlag(ImGuiItemFlags_Disabled, true); PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_Text] * ImVec4(1.0f,1.0f,1.0f,0.5f)); } const float rad = g.FontSize * 0.5f; if (CloseButton(host_window->GetID("#CLOSE"), title_bar_rect.GetTR() + ImVec2(-style.FramePadding.x - rad, style.FramePadding.y + rad), rad + 1)) if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_bar->VisibleTabId)) { node->WantCloseTabID = tab->ID; TabBarCloseTab(tab_bar, tab); } //if (IsItemActive()) // focus_tab_id = tab_bar->SelectedTabId; if (!node->VisibleWindow->HasCloseButton) { PopStyleColor(); PopItemFlag(); } } // When clicking on the title bar outside of tabs, we still focus the selected tab for that node if (g.HoveredWindow == host_window && g.HoveredId == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max)) { if (IsMouseClicked(0)) { focus_tab_id = tab_bar->SelectedTabId; // Forward moving request to selected window if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, focus_tab_id)) StartMouseMovingWindow(tab->Window); } } // Forward focus from host node to selected window //if (is_focused && g.NavWindow == host_window && !g.NavWindowingTarget) // focus_tab_id = tab_bar->SelectedTabId; // When clicked on a tab we requested focus to the docked child // This overrides the value set by "forward focus from host node to selected window". if (tab_bar->NextSelectedTabId) focus_tab_id = tab_bar->NextSelectedTabId; // Apply navigation focus if (focus_tab_id != 0) if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, focus_tab_id)) { FocusWindow(tab->Window); NavInitWindow(tab->Window, false); } EndTabBar(); PopID(); // Restore SkipItems flag if (!node->IsDockSpace()) { host_window->DC.NavLayerCurrent = ImGuiNavLayer_Main; host_window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); host_window->SkipItems = backup_skip_item; } } static void ImGui::DockNodeAddTabBar(ImGuiDockNode* node) { IM_ASSERT(node->TabBar == NULL); node->TabBar = IM_NEW(ImGuiTabBar); } static void ImGui::DockNodeRemoveTabBar(ImGuiDockNode* node) { if (node->TabBar == NULL) return; IM_DELETE(node->TabBar); node->TabBar = NULL; } static bool DockNodeIsDropAllowedOne(ImGuiWindow* payload, ImGuiWindow* host_window) { if (host_window->DockNodeAsHost && host_window->DockNodeAsHost->IsDockSpace() && payload->BeginOrderWithinContext < host_window->BeginOrderWithinContext) return false; ImGuiWindowClass* host_class = host_window->DockNodeAsHost ? &host_window->DockNodeAsHost->WindowClass : &host_window->WindowClass; ImGuiWindowClass* payload_class = &payload->WindowClass; if (host_class->ClassId != payload_class->ClassId) { if (host_class->ClassId != 0 && host_class->DockingAllowUnclassed && payload_class->ClassId == 0) return true; if (payload_class->ClassId != 0 && payload_class->DockingAllowUnclassed && host_class->ClassId == 0) return true; return false; } return true; } static bool ImGui::DockNodeIsDropAllowed(ImGuiWindow* host_window, ImGuiWindow* root_payload) { if (root_payload->DockNodeAsHost && root_payload->DockNodeAsHost->IsSplitNode()) return true; const int payload_count = root_payload->DockNodeAsHost ? root_payload->DockNodeAsHost->Windows.Size : 1; for (int payload_n = 0; payload_n < payload_count; payload_n++) { ImGuiWindow* payload = root_payload->DockNodeAsHost ? root_payload->DockNodeAsHost->Windows[payload_n] : root_payload; if (DockNodeIsDropAllowedOne(payload, host_window)) return true; } return false; } static ImRect ImGui::DockNodeCalcTabBarRect(const ImGuiDockNode* node) { ImGuiContext& g = *GImGui; 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)); if (node->HasCollapseButton) 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() // In DockNodeUpdateTabBar() we currently display a disabled close button even if there is none. r.Max.x -= g.Style.FramePadding.x + g.FontSize + 1.0f; return r; } void ImGui::DockNodeCalcSplitRects(ImVec2& pos_old, ImVec2& size_old, ImVec2& pos_new, ImVec2& size_new, ImGuiDir dir, ImVec2 size_new_desired) { ImGuiContext& g = *GImGui; const float dock_spacing = g.Style.ItemInnerSpacing.x; const ImGuiAxis axis = (dir == ImGuiDir_Left || dir == ImGuiDir_Right) ? ImGuiAxis_X : ImGuiAxis_Y; pos_new[axis ^ 1] = pos_old[axis ^ 1]; size_new[axis ^ 1] = size_old[axis ^ 1]; // Distribute size on given axis (with a desired size or equally) const float w_avail = size_old[axis] - dock_spacing; if (size_new_desired[axis] > 0.0f && size_new_desired[axis] <= w_avail * 0.5f) { size_new[axis] = size_new_desired[axis]; size_old[axis] = (float)(int)(w_avail - size_new[axis]); } else { size_new[axis] = (float)(int)(w_avail * 0.5f); size_old[axis] = (float)(int)(w_avail - size_new[axis]); } // Position each node if (dir == ImGuiDir_Right || dir == ImGuiDir_Down) { pos_new[axis] = pos_old[axis] + size_old[axis] + dock_spacing; } else if (dir == ImGuiDir_Left || dir == ImGuiDir_Up) { pos_new[axis] = pos_old[axis]; pos_old[axis] = pos_new[axis] + size_new[axis] + dock_spacing; } } // Retrieve the drop rectangles for a given direction or for the center + perform hit testing. bool ImGui::DockNodeCalcDropRectsAndTestMousePos(const ImRect& parent, ImGuiDir dir, ImRect& out_r, bool outer_docking, ImVec2* test_mouse_pos) { ImGuiContext& g = *GImGui; const float parent_smaller_axis = ImMin(parent.GetWidth(), parent.GetHeight()); const float hs_for_central_nodes = ImMin(g.FontSize * 1.5f, ImMax(g.FontSize * 0.5f, parent_smaller_axis / 8.0f)); float hs_w; // Half-size, longer axis float hs_h; // Half-size, smaller axis ImVec2 off; // Distance from edge or center if (outer_docking) { //hs_w = ImFloor(ImClamp(parent_smaller_axis - hs_for_central_nodes * 4.0f, g.FontSize * 0.5f, g.FontSize * 8.0f)); //hs_h = ImFloor(hs_w * 0.15f); //off = ImVec2(ImFloor(parent.GetWidth() * 0.5f - GetFrameHeightWithSpacing() * 1.4f - hs_h), ImFloor(parent.GetHeight() * 0.5f - GetFrameHeightWithSpacing() * 1.4f - hs_h)); hs_w = ImFloor(hs_for_central_nodes * 1.50f); hs_h = ImFloor(hs_for_central_nodes * 0.80f); off = ImVec2(ImFloor(parent.GetWidth() * 0.5f - GetFrameHeightWithSpacing() * 0.0f - hs_h), ImFloor(parent.GetHeight() * 0.5f - GetFrameHeightWithSpacing() * 0.0f - hs_h)); } else { hs_w = ImFloor(hs_for_central_nodes); hs_h = ImFloor(hs_for_central_nodes * 0.90f); off = ImVec2(ImFloor(hs_w * 2.40f), ImFloor(hs_w * 2.40f)); } ImVec2 c = ImFloor(parent.GetCenter()); if (dir == ImGuiDir_None) { out_r = ImRect(c.x - hs_w, c.y - hs_w, c.x + hs_w, c.y + hs_w); } 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); } 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); } 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); } 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); } if (test_mouse_pos == NULL) return false; ImRect hit_r = out_r; if (!outer_docking) { // Custom hit testing for the 5-way selection, designed to reduce flickering when moving diagonally between sides hit_r.Expand(ImFloor(hs_w * 0.30f)); ImVec2 mouse_delta = (*test_mouse_pos - c); float mouse_delta_len2 = ImLengthSqr(mouse_delta); float r_threshold_center = hs_w * 1.4f; float r_threshold_sides = hs_w * (1.4f + 1.2f); if (mouse_delta_len2 < r_threshold_center * r_threshold_center) return (dir == ImGuiDir_None); if (mouse_delta_len2 < r_threshold_sides * r_threshold_sides) return (dir == ImGetDirQuadrantFromDelta(mouse_delta.x, mouse_delta.y)); } return hit_r.Contains(*test_mouse_pos); } // host_node may be NULL if the window doesn't have a DockNode already. static void ImGui::DockNodePreviewDockCalc(ImGuiWindow* host_window, ImGuiDockNode* host_node, ImGuiWindow* root_payload, ImGuiDockPreviewData* data, bool is_explicit_target, bool is_outer_docking) { ImGuiContext& g = *GImGui; // There is an edge case when docking into a dockspace which only has inactive nodes. // In this case DockNodeTreeFindNodeByPos() will have selected a leaf node which is inactive. // Because the inactive leaf node doesn't have proper pos/size yet, we'll use the root node as reference. ImGuiDockNode* ref_node_for_rect = (host_node && !host_node->IsVisible) ? DockNodeGetRootNode(host_node) : host_node; if (ref_node_for_rect) IM_ASSERT(ref_node_for_rect->IsVisible); // Build a tentative future node (reuse same structure because it is practical) data->FutureNode.HasCloseButton = (host_node ? host_node->HasCloseButton : host_window->HasCloseButton) || (root_payload->HasCloseButton); data->FutureNode.HasCollapseButton = host_node ? true : ((host_window->Flags & ImGuiWindowFlags_NoCollapse) == 0); data->FutureNode.Pos = host_node ? ref_node_for_rect->Pos : host_window->Pos; data->FutureNode.Size = host_node ? ref_node_for_rect->Size : host_window->Size; // Figure out here we are allowed to dock ImGuiDockNodeFlags host_node_flags = host_node ? host_node->GetMergedFlags() : 0; const bool src_is_visibly_splitted = root_payload->DockNodeAsHost && root_payload->DockNodeAsHost->IsSplitNode() && (root_payload->DockNodeAsHost->OnlyNodeWithWindows == NULL); data->IsCenterAvailable = !is_outer_docking; if (src_is_visibly_splitted && (!host_node || !host_node->IsEmpty())) data->IsCenterAvailable = false; if (host_node && (host_node_flags & ImGuiDockNodeFlags_NoDockingInCentralNode) && host_node->IsCentralNode()) data->IsCenterAvailable = false; data->IsSidesAvailable = true; if ((host_node && (host_node_flags & ImGuiDockNodeFlags_NoSplit)) || g.IO.ConfigDockingNoSplit) data->IsSidesAvailable = false; if (!is_outer_docking && host_node && host_node->ParentNode == NULL && host_node->IsCentralNode()) data->IsSidesAvailable = false; // Calculate drop shapes geometry for allowed splitting directions IM_ASSERT(ImGuiDir_None == -1); data->SplitNode = host_node; data->SplitDir = ImGuiDir_None; data->IsSplitDirExplicit = false; if (!host_window->Collapsed) for (int dir = ImGuiDir_None; dir < ImGuiDir_COUNT; dir++) { if (dir == ImGuiDir_None && !data->IsCenterAvailable) continue; if (dir != ImGuiDir_None && !data->IsSidesAvailable) continue; if (DockNodeCalcDropRectsAndTestMousePos(data->FutureNode.Rect(), (ImGuiDir)dir, data->DropRectsDraw[dir+1], is_outer_docking, &g.IO.MousePos)) { data->SplitDir = (ImGuiDir)dir; data->IsSplitDirExplicit = true; } } // When docking without holding Shift, we only allow and preview docking when hovering over a drop rect or over the title bar data->IsDropAllowed = (data->SplitDir != ImGuiDir_None) || (data->IsCenterAvailable); if (!is_explicit_target && !data->IsSplitDirExplicit && !g.IO.ConfigDockingWithShift) data->IsDropAllowed = false; // Calculate split area data->SplitRatio = 0.0f; if (data->SplitDir != ImGuiDir_None) { ImGuiDir split_dir = data->SplitDir; ImGuiAxis split_axis = (split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Right) ? ImGuiAxis_X : ImGuiAxis_Y; ImVec2 pos_new, pos_old = data->FutureNode.Pos; ImVec2 size_new, size_old = data->FutureNode.Size; DockNodeCalcSplitRects(pos_old, size_old, pos_new, size_new, split_dir, root_payload->Size); // Calculate split ratio so we can pass it down the docking request float split_ratio = ImSaturate(size_new[split_axis] / data->FutureNode.Size[split_axis]); data->FutureNode.Pos = pos_new; data->FutureNode.Size = size_new; data->SplitRatio = (split_dir == ImGuiDir_Right || split_dir == ImGuiDir_Down) ? (1.0f - split_ratio) : (split_ratio); } } static void ImGui::DockNodePreviewDockRender(ImGuiWindow* host_window, ImGuiDockNode* host_node, ImGuiWindow* root_payload, const ImGuiDockPreviewData* data) { ImGuiContext& g = *GImGui; IM_ASSERT(g.CurrentWindow == host_window); // Because we rely on font size to calculate tab sizes // With this option, we only display the preview on the target viewport, and the payload viewport is made transparent. // To compensate for the single layer obstructed by the payload, we'll increase the alpha of the preview nodes. const bool is_transparent_payload = g.IO.ConfigDockingTransparentPayload; // In case the two windows involved are on different viewports, we will draw the overlay on each of them. int overlay_draw_lists_count = 0; ImDrawList* overlay_draw_lists[2]; overlay_draw_lists[overlay_draw_lists_count++] = GetForegroundDrawList(host_window->Viewport); if (host_window->Viewport != root_payload->Viewport && !is_transparent_payload) overlay_draw_lists[overlay_draw_lists_count++] = GetForegroundDrawList(root_payload->Viewport); // Draw main preview rectangle const ImU32 overlay_col_tabs = GetColorU32(ImGuiCol_TabActive); const ImU32 overlay_col_main = GetColorU32(ImGuiCol_DockingPreview, is_transparent_payload ? 0.60f : 0.40f); const ImU32 overlay_col_drop = GetColorU32(ImGuiCol_DockingPreview, is_transparent_payload ? 0.90f : 0.70f); const ImU32 overlay_col_drop_hovered = GetColorU32(ImGuiCol_DockingPreview, is_transparent_payload ? 1.20f : 1.00f); const ImU32 overlay_col_lines = GetColorU32(ImGuiCol_NavWindowingHighlight, is_transparent_payload ? 0.80f : 0.60f); // Display area preview const bool can_preview_tabs = (root_payload->DockNodeAsHost == NULL || root_payload->DockNodeAsHost->Windows.Size > 0); if (data->IsDropAllowed) { ImRect overlay_rect = data->FutureNode.Rect(); if (data->SplitDir == ImGuiDir_None && can_preview_tabs) overlay_rect.Min.y += GetFrameHeight(); if (data->SplitDir != ImGuiDir_None || data->IsCenterAvailable) for (int overlay_n = 0; overlay_n < overlay_draw_lists_count; overlay_n++) overlay_draw_lists[overlay_n]->AddRectFilled(overlay_rect.Min, overlay_rect.Max, overlay_col_main, host_window->WindowRounding); } // Display tab shape/label preview unless we are splitting node (it generally makes the situation harder to read) if (data->IsDropAllowed && can_preview_tabs && data->SplitDir == ImGuiDir_None && data->IsCenterAvailable) { // Compute target tab bar geometry so we can locate our preview tabs ImRect tab_bar_rect = DockNodeCalcTabBarRect(&data->FutureNode); ImVec2 tab_pos = tab_bar_rect.Min; if (host_node && host_node->TabBar) { if (!host_node->IsHiddenTabBar() && !host_node->IsNoTabBar()) 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. else tab_pos.x += g.Style.ItemInnerSpacing.x + TabItemCalcSize(host_node->Windows[0]->Name, host_node->Windows[0]->HasCloseButton).x; } else if (!(host_window->Flags & ImGuiWindowFlags_DockNodeHost)) { 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 } // Draw tab shape/label preview (payload may be a loose window or a host window carrying multiple tabbed windows) if (root_payload->DockNodeAsHost) IM_ASSERT(root_payload->DockNodeAsHost->Windows.Size == root_payload->DockNodeAsHost->TabBar->Tabs.Size); const int payload_count = root_payload->DockNodeAsHost ? root_payload->DockNodeAsHost->TabBar->Tabs.Size : 1; for (int payload_n = 0; payload_n < payload_count; payload_n++) { // Calculate the tab bounding box for each payload window ImGuiWindow* payload = root_payload->DockNodeAsHost ? root_payload->DockNodeAsHost->TabBar->Tabs[payload_n].Window : root_payload; if (!DockNodeIsDropAllowedOne(payload, host_window)) continue; ImVec2 tab_size = TabItemCalcSize(payload->Name, payload->HasCloseButton); ImRect tab_bb(tab_pos.x, tab_pos.y, tab_pos.x + tab_size.x, tab_pos.y + tab_size.y); tab_pos.x += tab_size.x + g.Style.ItemInnerSpacing.x; for (int overlay_n = 0; overlay_n < overlay_draw_lists_count; overlay_n++) { ImGuiTabItemFlags tab_flags = ImGuiTabItemFlags_Preview | ((payload->Flags & ImGuiWindowFlags_UnsavedDocument) ? ImGuiTabItemFlags_UnsavedDocument : 0); if (!tab_bar_rect.Contains(tab_bb)) overlay_draw_lists[overlay_n]->PushClipRect(tab_bar_rect.Min, tab_bar_rect.Max); TabItemBackground(overlay_draw_lists[overlay_n], tab_bb, tab_flags, overlay_col_tabs); TabItemLabelAndCloseButton(overlay_draw_lists[overlay_n], tab_bb, tab_flags, g.Style.FramePadding, payload->Name, 0, 0); if (!tab_bar_rect.Contains(tab_bb)) overlay_draw_lists[overlay_n]->PopClipRect(); } } } // Display drop boxes const float overlay_rounding = ImMax(3.0f, g.Style.FrameRounding); for (int dir = ImGuiDir_None; dir < ImGuiDir_COUNT; dir++) { if (!data->DropRectsDraw[dir + 1].IsInverted()) { ImRect draw_r = data->DropRectsDraw[dir + 1]; ImRect draw_r_in = draw_r; draw_r_in.Expand(-2.0f); ImU32 overlay_col = (data->SplitDir == (ImGuiDir)dir && data->IsSplitDirExplicit) ? overlay_col_drop_hovered : overlay_col_drop; for (int overlay_n = 0; overlay_n < overlay_draw_lists_count; overlay_n++) { ImVec2 center = ImFloor(draw_r_in.GetCenter()); overlay_draw_lists[overlay_n]->AddRectFilled(draw_r.Min, draw_r.Max, overlay_col, overlay_rounding); overlay_draw_lists[overlay_n]->AddRect(draw_r_in.Min, draw_r_in.Max, overlay_col_lines, overlay_rounding); if (dir == ImGuiDir_Left || dir == ImGuiDir_Right) 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); if (dir == ImGuiDir_Up || dir == ImGuiDir_Down) 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); } } // Stop after ImGuiDir_None if ((host_node && (host_node->GetMergedFlags() & ImGuiDockNodeFlags_NoSplit)) || g.IO.ConfigDockingNoSplit) return; } } //----------------------------------------------------------------------------- // Docking: ImGuiDockNode Tree manipulation functions //----------------------------------------------------------------------------- void ImGui::DockNodeTreeSplit(ImGuiContext* ctx, ImGuiDockNode* parent_node, ImGuiAxis split_axis, int split_inheritor_child_idx, float split_ratio, ImGuiDockNode* new_node) { ImGuiContext& g = *GImGui; IM_ASSERT(split_axis != ImGuiAxis_None); ImGuiDockNode* child_0 = (new_node && split_inheritor_child_idx != 0) ? new_node : DockContextAddNode(ctx, 0); child_0->ParentNode = parent_node; ImGuiDockNode* child_1 = (new_node && split_inheritor_child_idx != 1) ? new_node : DockContextAddNode(ctx, 0); child_1->ParentNode = parent_node; ImGuiDockNode* child_inheritor = (split_inheritor_child_idx == 0) ? child_0 : child_1; DockNodeMoveChildNodes(child_inheritor, parent_node); parent_node->ChildNodes[0] = child_0; parent_node->ChildNodes[1] = child_1; parent_node->ChildNodes[split_inheritor_child_idx]->VisibleWindow = parent_node->VisibleWindow; parent_node->SplitAxis = split_axis; parent_node->VisibleWindow = NULL; parent_node->AuthorityForPos = parent_node->AuthorityForSize = ImGuiDataAuthority_DockNode; float size_avail = (parent_node->Size[split_axis] - IMGUI_DOCK_SPLITTER_SIZE); size_avail = ImMax(size_avail, g.Style.WindowMinSize[split_axis] * 2.0f); IM_ASSERT(size_avail > 0.0f); // If you created a node manually with DockBuilderAddNode(), you need to also call DockBuilderSetNodeSize() before splitting. child_0->SizeRef = child_1->SizeRef = parent_node->Size; child_0->SizeRef[split_axis] = ImFloor(size_avail * split_ratio); child_1->SizeRef[split_axis] = ImFloor(size_avail - child_0->SizeRef[split_axis]); DockNodeMoveWindows(parent_node->ChildNodes[split_inheritor_child_idx], parent_node); DockNodeTreeUpdatePosSize(parent_node, parent_node->Pos, parent_node->Size); // Flags transfer (e.g. this is where we transfer the ImGuiDockNodeFlags_CentralNode property) child_0->SharedFlags = parent_node->SharedFlags & ImGuiDockNodeFlags_SharedFlagsInheritMask_; child_1->SharedFlags = parent_node->SharedFlags & ImGuiDockNodeFlags_SharedFlagsInheritMask_; child_inheritor->LocalFlags = parent_node->LocalFlags & ImGuiDockNodeFlags_LocalFlagsTransferMask_; parent_node->LocalFlags &= ~ImGuiDockNodeFlags_LocalFlagsTransferMask_; if (child_inheritor->IsCentralNode()) DockNodeGetRootNode(parent_node)->CentralNode = child_inheritor; } void ImGui::DockNodeTreeMerge(ImGuiContext* ctx, ImGuiDockNode* parent_node, ImGuiDockNode* merge_lead_child) { // When called from DockContextProcessUndockNode() it is possible that one of the child is NULL. ImGuiDockNode* child_0 = parent_node->ChildNodes[0]; ImGuiDockNode* child_1 = parent_node->ChildNodes[1]; IM_ASSERT(child_0 || child_1); IM_ASSERT(merge_lead_child == child_0 || merge_lead_child == child_1); if ((child_0 && child_0->Windows.Size > 0) || (child_1 && child_1->Windows.Size > 0)) { IM_ASSERT(parent_node->TabBar == NULL); IM_ASSERT(parent_node->Windows.Size == 0); } ImVec2 backup_last_explicit_size = parent_node->SizeRef; DockNodeMoveChildNodes(parent_node, merge_lead_child); if (child_0) { DockNodeMoveWindows(parent_node, child_0); // Generally only 1 of the 2 child node will have windows DockSettingsRenameNodeReferences(child_0->ID, parent_node->ID); } if (child_1) { DockNodeMoveWindows(parent_node, child_1); DockSettingsRenameNodeReferences(child_1->ID, parent_node->ID); } DockNodeApplyPosSizeToWindows(parent_node); parent_node->AuthorityForPos = parent_node->AuthorityForSize = parent_node->AuthorityForViewport = ImGuiDataAuthority_Auto; parent_node->VisibleWindow = merge_lead_child->VisibleWindow; parent_node->SizeRef = backup_last_explicit_size; // Flags transfer parent_node->LocalFlags = ((child_0 ? child_0->LocalFlags : 0) | (child_1 ? child_1->LocalFlags : 0)) & ImGuiDockNodeFlags_LocalFlagsTransferMask_; if (child_0) { ctx->DockContext->Nodes.SetVoidPtr(child_0->ID, NULL); IM_DELETE(child_0); } if (child_1) { ctx->DockContext->Nodes.SetVoidPtr(child_1->ID, NULL); IM_DELETE(child_1); } } // Update Pos/Size for a node hierarchy (don't affect child Windows yet) void ImGui::DockNodeTreeUpdatePosSize(ImGuiDockNode* node, ImVec2 pos, ImVec2 size) { node->Pos = pos; node->Size = size; if (node->IsLeafNode()) return; ImGuiDockNode* child_0 = node->ChildNodes[0]; ImGuiDockNode* child_1 = node->ChildNodes[1]; ImVec2 child_0_pos = pos, child_1_pos = pos; ImVec2 child_0_size = size, child_1_size = size; if (child_0->IsVisible && child_1->IsVisible) { const float spacing = IMGUI_DOCK_SPLITTER_SIZE; const ImGuiAxis axis = (ImGuiAxis)node->SplitAxis; const float size_avail = ImMax(size[axis] - spacing, 0.0f); // Size allocation policy // 1) The first 0..WindowMinSize[axis]*2 are allocated evenly to both windows. ImGuiContext& g = *GImGui; const float size_min_each = ImFloor(ImMin(size_avail, g.Style.WindowMinSize[axis] * 2.0f) * 0.5f); // 2) Process locked absolute size (during a splitter resize we preserve the child of nodes not touching the splitter edge) IM_ASSERT(!(child_0->WantLockSizeOnce && child_1->WantLockSizeOnce)); if (child_0->WantLockSizeOnce) { child_0->WantLockSizeOnce = false; child_0_size[axis] = child_0->SizeRef[axis] = child_0->Size[axis]; child_1_size[axis] = child_1->SizeRef[axis] = (size_avail - child_0_size[axis]); } else if (child_1->WantLockSizeOnce) { child_1->WantLockSizeOnce = false; child_1_size[axis] = child_1->SizeRef[axis] = child_1->Size[axis]; child_0_size[axis] = child_0->SizeRef[axis] = (size_avail - child_1_size[axis]); } // 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 else if (child_1->IsCentralNode() && child_0->SizeRef[axis] != 0.0f) { child_0_size[axis] = ImMin(size_avail - size_min_each, child_0->SizeRef[axis]); child_1_size[axis] = (size_avail - child_0_size[axis]); } else if (child_0->IsCentralNode() && child_1->SizeRef[axis] != 0.0f) { child_1_size[axis] = ImMin(size_avail - size_min_each, child_1->SizeRef[axis]); child_0_size[axis] = (size_avail - child_1_size[axis]); } else { // 4) Otherwise distribute according to the relative ratio of each SizeRef value float split_ratio = child_0->SizeRef[axis] / (child_0->SizeRef[axis] + child_1->SizeRef[axis]); child_0_size[axis] = ImMax(size_min_each, ImFloor(size_avail * split_ratio + 0.5F)); child_1_size[axis] = (size_avail - child_0_size[axis]); } child_1_pos[axis] += spacing + child_0_size[axis]; } if (child_0->IsVisible) DockNodeTreeUpdatePosSize(child_0, child_0_pos, child_0_size); if (child_1->IsVisible) DockNodeTreeUpdatePosSize(child_1, child_1_pos, child_1_size); } static void DockNodeTreeUpdateSplitterFindTouchingNode(ImGuiDockNode* node, ImGuiAxis axis, int side, ImVector* touching_nodes) { if (node->IsLeafNode()) { touching_nodes->push_back(node); return; } if (node->ChildNodes[0]->IsVisible) if (node->SplitAxis != axis || side == 0 || !node->ChildNodes[1]->IsVisible) DockNodeTreeUpdateSplitterFindTouchingNode(node->ChildNodes[0], axis, side, touching_nodes); if (node->ChildNodes[1]->IsVisible) if (node->SplitAxis != axis || side == 1 || !node->ChildNodes[0]->IsVisible) DockNodeTreeUpdateSplitterFindTouchingNode(node->ChildNodes[1], axis, side, touching_nodes); } void ImGui::DockNodeTreeUpdateSplitter(ImGuiDockNode* node) { if (node->IsLeafNode()) return; ImGuiContext& g = *GImGui; ImGuiDockNode* child_0 = node->ChildNodes[0]; ImGuiDockNode* child_1 = node->ChildNodes[1]; if (child_0->IsVisible && child_1->IsVisible) { // Bounding box of the splitter cover the space between both nodes (w = Spacing, h = Size[xy^1] for when splitting horizontally) const ImGuiAxis axis = (ImGuiAxis)node->SplitAxis; IM_ASSERT(axis != ImGuiAxis_None); ImRect bb; bb.Min = child_0->Pos; bb.Max = child_1->Pos; bb.Min[axis] += child_0->Size[axis]; bb.Max[axis ^ 1] += child_1->Size[axis ^ 1]; //if (g.IO.KeyCtrl) GetForegroundDrawList(g.CurrentWindow->Viewport)->AddRect(bb.Min, bb.Max, IM_COL32(255,0,255,255)); if ((child_0->GetMergedFlags() | child_1->GetMergedFlags()) & ImGuiDockNodeFlags_NoResize) { ImGuiWindow* window = g.CurrentWindow; window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_Separator), g.Style.FrameRounding); } else { //bb.Min[axis] += 1; // Display a little inward so highlight doesn't connect with nearby tabs on the neighbor node. //bb.Max[axis] -= 1; PushID(node->ID); // Gather list of nodes that are touching the splitter line. Find resizing limits based on those nodes. ImVector touching_nodes[2]; float min_size = g.Style.WindowMinSize[axis]; float resize_limits[2]; resize_limits[0] = node->ChildNodes[0]->Pos[axis] + min_size; resize_limits[1] = node->ChildNodes[1]->Pos[axis] + node->ChildNodes[1]->Size[axis] - min_size; ImGuiID splitter_id = GetID("##Splitter"); if (g.ActiveId == splitter_id) { // Only process when splitter is active DockNodeTreeUpdateSplitterFindTouchingNode(child_0, axis, 1, &touching_nodes[0]); DockNodeTreeUpdateSplitterFindTouchingNode(child_1, axis, 0, &touching_nodes[1]); for (int touching_node_n = 0; touching_node_n < touching_nodes[0].Size; touching_node_n++) resize_limits[0] = ImMax(resize_limits[0], touching_nodes[0][touching_node_n]->Rect().Min[axis] + min_size); for (int touching_node_n = 0; touching_node_n < touching_nodes[1].Size; touching_node_n++) resize_limits[1] = ImMin(resize_limits[1], touching_nodes[1][touching_node_n]->Rect().Max[axis] - min_size); /* // [DEBUG] Render limits ImDrawList* draw_list = node->HostWindow ? GetOverlayDrawList(node->HostWindow) : GetOverlayDrawList((ImGuiViewportP*)GetMainViewport()); for (int n = 0; n < 2; n++) if (axis == ImGuiAxis_X) 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); else 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); */ } // Use a short delay before highlighting the splitter (and changing the mouse cursor) in order for regular mouse movement to not highlight many splitters float cur_size_0 = child_0->Size[axis]; float cur_size_1 = child_1->Size[axis]; float min_size_0 = resize_limits[0] - child_0->Pos[axis]; float min_size_1 = child_1->Pos[axis] + child_1->Size[axis] - resize_limits[1]; 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)) { if (touching_nodes[0].Size > 0 && touching_nodes[1].Size > 0) { child_0->Size[axis] = child_0->SizeRef[axis] = cur_size_0; child_1->Pos[axis] -= cur_size_1 - child_1->Size[axis]; child_1->Size[axis] = child_1->SizeRef[axis] = cur_size_1; // Lock the size of every node that is a sibling of the node we are touching // This might be less desirable if we can merge sibling of a same axis into the same parental level. for (int side_n = 0; side_n < 2; side_n++) for (int touching_node_n = 0; touching_node_n < touching_nodes[side_n].Size; touching_node_n++) { ImGuiDockNode* touching_node = touching_nodes[side_n][touching_node_n]; //ImDrawList* draw_list = node->HostWindow ? GetOverlayDrawList(node->HostWindow) : GetOverlayDrawList((ImGuiViewportP*)GetMainViewport()); //draw_list->AddRect(touching_node->Pos, touching_node->Pos + touching_node->Size, IM_COL32(255, 128, 0, 255)); while (touching_node->ParentNode != node) { if (touching_node->ParentNode->SplitAxis == axis) { // Mark other node so its size will be preserved during the upcoming call to DockNodeTreeUpdatePosSize(). ImGuiDockNode* node_to_preserve = touching_node->ParentNode->ChildNodes[side_n]; node_to_preserve->WantLockSizeOnce = true; //draw_list->AddRect(touching_node->Pos, touching_node->Rect().Max, IM_COL32(255, 0, 0, 255)); //draw_list->AddRectFilled(node_to_preserve->Pos, node_to_preserve->Rect().Max, IM_COL32(0, 255, 0, 100)); } touching_node = touching_node->ParentNode; } } DockNodeTreeUpdatePosSize(child_0, child_0->Pos, child_0->Size); DockNodeTreeUpdatePosSize(child_1, child_1->Pos, child_1->Size); MarkIniSettingsDirty(); } } PopID(); } } if (child_0->IsVisible) DockNodeTreeUpdateSplitter(child_0); if (child_1->IsVisible) DockNodeTreeUpdateSplitter(child_1); } ImGuiDockNode* ImGui::DockNodeTreeFindFallbackLeafNode(ImGuiDockNode* node) { if (node->IsLeafNode()) return node; if (ImGuiDockNode* leaf_node = DockNodeTreeFindFallbackLeafNode(node->ChildNodes[0])) return leaf_node; if (ImGuiDockNode* leaf_node = DockNodeTreeFindFallbackLeafNode(node->ChildNodes[1])) return leaf_node; return NULL; } ImGuiDockNode* ImGui::DockNodeTreeFindNodeByPos(ImGuiDockNode* node, ImVec2 pos) { if (!node->IsVisible) return NULL; ImGuiContext& g = *GImGui; const float dock_spacing = g.Style.ItemInnerSpacing.x; ImRect r(node->Pos, node->Pos + node->Size); r.Expand(dock_spacing * 0.5f); bool inside = r.Contains(pos); if (!inside) return NULL; if (node->IsLeafNode()) return node; if (ImGuiDockNode* hovered_node = DockNodeTreeFindNodeByPos(node->ChildNodes[0], pos)) return hovered_node; if (ImGuiDockNode* hovered_node = DockNodeTreeFindNodeByPos(node->ChildNodes[1], pos)) return hovered_node; // There is an edge case when docking into a dockspace which only has inactive nodes (because none of the windows are active) // In this case we need to fallback into any leaf mode, possibly the central node. if (node->IsDockSpace() && node->IsRootNode()) { if (node->CentralNode && node->IsLeafNode()) // FIXME-20181220: We should not have to test for IsLeafNode() here but we have another bug to fix first. return node->CentralNode; return DockNodeTreeFindFallbackLeafNode(node); } return NULL; } //----------------------------------------------------------------------------- // Docking: Public Functions (SetWindowDock, DockSpace, DockSpaceOverViewport) //----------------------------------------------------------------------------- void ImGui::SetWindowDock(ImGuiWindow* window, ImGuiID dock_id, ImGuiCond cond) { // Test condition (NB: bit 0 is always true) and clear flags for next time if (cond && (window->SetWindowDockAllowFlags & cond) == 0) return; window->SetWindowDockAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); if (window->DockId == dock_id) return; // If the user attempt to set a dock id that is a split node, we'll dig within to find a suitable docking spot ImGuiContext* ctx = GImGui; if (ImGuiDockNode* new_node = DockContextFindNodeByID(ctx, dock_id)) if (new_node->IsSplitNode()) { // Policy: Find central node or latest focused node. We first move back to our root node. new_node = DockNodeGetRootNode(new_node); if (new_node->CentralNode) { IM_ASSERT(new_node->CentralNode->IsCentralNode()); dock_id = new_node->CentralNode->ID; } else { dock_id = new_node->LastFocusedNodeID; } } if (window->DockId == dock_id) return; if (window->DockNode) DockNodeRemoveWindow(window->DockNode, window, 0); window->DockId = dock_id; } // Create an explicit dockspace node within an existing window. Also expose dock node flags and creates a CentralNode by default. // The Central Node is always displayed even when empty and shrink/extend according to the requested size of its neighbors. void ImGui::DockSpace(ImGuiID id, const ImVec2& size_arg, ImGuiDockNodeFlags flags, const ImGuiWindowClass* window_class) { ImGuiContext* ctx = GImGui; ImGuiContext& g = *ctx; ImGuiWindow* window = GetCurrentWindow(); if (!(g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable)) return; IM_ASSERT((flags & ImGuiDockNodeFlags_DockSpace) == 0); ImGuiDockNode* node = DockContextFindNodeByID(ctx, id); if (!node) { node = DockContextAddNode(ctx, id); node->LocalFlags |= ImGuiDockNodeFlags_CentralNode; } node->SharedFlags = flags; node->WindowClass = window_class ? *window_class : ImGuiWindowClass(); // When a DockSpace transitioned form implicit to explicit this may be called a second time // 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. if (node->LastFrameActive == g.FrameCount && !(flags & ImGuiDockNodeFlags_KeepAliveOnly)) { IM_ASSERT(node->IsDockSpace() == false && "Cannot call DockSpace() twice a frame with the same ID"); node->LocalFlags |= ImGuiDockNodeFlags_DockSpace; return; } node->LocalFlags |= ImGuiDockNodeFlags_DockSpace; // Keep alive mode, this is allow windows docked into this node so stay docked even if they are not visible if (flags & ImGuiDockNodeFlags_KeepAliveOnly) { node->LastFrameAlive = g.FrameCount; return; } const ImVec2 content_avail = GetContentRegionAvail(); ImVec2 size = ImFloor(size_arg); if (size.x <= 0.0f) size.x = ImMax(content_avail.x + size.x, 4.0f); // Arbitrary minimum child size (0.0f causing too much issues) if (size.y <= 0.0f) size.y = ImMax(content_avail.y + size.y, 4.0f); node->Pos = window->DC.CursorPos; node->Size = node->SizeRef = size; SetNextWindowPos(node->Pos); SetNextWindowSize(node->Size); g.NextWindowData.PosUndock = false; ImGuiWindowFlags window_flags = ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_DockNodeHost; window_flags |= ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar; window_flags |= ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse; char title[256]; ImFormatString(title, IM_ARRAYSIZE(title), "%s/DockSpace_%08X", window->Name, id); if (node->Windows.Size > 0 || node->IsSplitNode()) PushStyleColor(ImGuiCol_ChildBg, IM_COL32(0, 0, 0, 0)); PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0.0f); Begin(title, NULL, window_flags); PopStyleVar(); if (node->Windows.Size > 0 || node->IsSplitNode()) PopStyleColor(); ImGuiWindow* host_window = g.CurrentWindow; host_window->DockNodeAsHost = node; host_window->ChildId = window->GetID(title); node->HostWindow = host_window; node->OnlyNodeWithWindows = NULL; IM_ASSERT(node->IsRootNode()); DockNodeUpdate(node); End(); } // Tips: Use with ImGuiDockNodeFlags_PassthruCentralNode! // The limitation with this call is that your window won't have a menu bar. // 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. // 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. ImGuiID ImGui::DockSpaceOverViewport(bool has_main_menu_bar, ImGuiViewport* viewport, ImGuiDockNodeFlags dockspace_flags, const ImGuiWindowClass* window_class) { if (viewport == NULL) viewport = GetMainViewport(); auto pos = viewport->Pos; auto size = viewport->Size; if (has_main_menu_bar) { ImGuiContext& g = *GImGui; g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f)); float height_offset = g.NextWindowData.MenuBarOffsetMinVal.y + g.FontBaseSize + (g.Style.FramePadding.y * 2); pos.y += height_offset; size.y -= height_offset; } SetNextWindowPos(pos); SetNextWindowSize(size); SetNextWindowViewport(viewport->ID); ImGuiWindowFlags host_window_flags = 0; host_window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDocking; host_window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; if (dockspace_flags & ImGuiDockNodeFlags_PassthruCentralNode) host_window_flags |= ImGuiWindowFlags_NoBackground; char label[32]; ImFormatString(label, IM_ARRAYSIZE(label), "DockSpaceViewport_%08X", viewport->ID); PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); Begin(label, NULL, host_window_flags); PopStyleVar(3); ImGuiID dockspace_id = GetID("DockSpace"); DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags, window_class); End(); return dockspace_id; } //----------------------------------------------------------------------------- // Docking: Builder Functions //----------------------------------------------------------------------------- // Very early end-user API to manipulate dock nodes. // It is expected that those functions are all called _before_ the dockspace node submission. //----------------------------------------------------------------------------- void ImGui::DockBuilderDockWindow(const char* window_name, ImGuiID node_id) { // We don't preserve relative order of multiple docked windows (by clearing DockOrder back to -1) ImGuiID window_id = ImHashStr(window_name); if (ImGuiWindow* window = FindWindowByID(window_id)) { // Apply to created window SetWindowDock(window, node_id, ImGuiCond_Always); window->DockOrder = -1; } else { // Apply to settings ImGuiWindowSettings* settings = FindWindowSettings(window_id); if (settings == NULL) settings = CreateNewWindowSettings(window_name); settings->DockId = node_id; settings->DockOrder = -1; } } ImGuiDockNode* ImGui::DockBuilderGetNode(ImGuiID node_id) { ImGuiContext* ctx = GImGui; return DockContextFindNodeByID(ctx, node_id); } void ImGui::DockBuilderSetNodePos(ImGuiID node_id, ImVec2 pos) { ImGuiContext* ctx = GImGui; ImGuiDockNode* node = DockContextFindNodeByID(ctx, node_id); if (node == NULL) return; node->Pos = pos; node->AuthorityForPos = ImGuiDataAuthority_DockNode; } void ImGui::DockBuilderSetNodeSize(ImGuiID node_id, ImVec2 size) { ImGuiContext* ctx = GImGui; ImGuiDockNode* node = DockContextFindNodeByID(ctx, node_id); if (node == NULL) return; node->Size = node->SizeRef = size; node->AuthorityForSize = ImGuiDataAuthority_DockNode; } // If you create a regular node, both ref_pos/ref_size will position the window. // If you create a dockspace node: ref_pos won't be used, ref_size is useful on the first frame to... ImGuiID ImGui::DockBuilderAddNode(ImGuiID id, ImGuiDockNodeFlags flags) { ImGuiContext* ctx = GImGui; ImGuiDockNode* node = NULL; if (flags & ImGuiDockNodeFlags_DockSpace) { DockSpace(id, ImVec2(0, 0), (flags & ~ImGuiDockNodeFlags_DockSpace) | ImGuiDockNodeFlags_KeepAliveOnly); node = DockContextFindNodeByID(ctx, id); } else { if (id != 0) node = DockContextFindNodeByID(ctx, id); if (!node) node = DockContextAddNode(ctx, id); node->LocalFlags = flags; } node->LastFrameAlive = ctx->FrameCount; // Set this otherwise BeginDocked will undock during the same frame. return node->ID; } void ImGui::DockBuilderRemoveNode(ImGuiID node_id) { ImGuiContext* ctx = GImGui; ImGuiDockNode* node = DockContextFindNodeByID(ctx, node_id); if (node == NULL) return; DockBuilderRemoveNodeDockedWindows(node_id, true); DockBuilderRemoveNodeChildNodes(node_id); if (node->IsCentralNode() && node->ParentNode) node->ParentNode->LocalFlags = ImGuiDockNodeFlags_CentralNode; DockContextRemoveNode(ctx, node, true); } void ImGui::DockBuilderRemoveNodeChildNodes(ImGuiID root_id) { ImGuiContext* ctx = GImGui; ImGuiDockContext* dc = ctx->DockContext; ImGuiDockNode* root_node = root_id ? DockContextFindNodeByID(ctx, root_id) : NULL; if (root_id && root_node == NULL) return; bool has_central_node = false; ImGuiDataAuthority backup_root_node_authority_for_pos = root_node ? root_node->AuthorityForPos : ImGuiDataAuthority_Auto; ImGuiDataAuthority backup_root_node_authority_for_size = root_node ? root_node->AuthorityForSize : ImGuiDataAuthority_Auto; // Process active windows ImVector nodes_to_remove; for (int n = 0; n < dc->Nodes.Data.Size; n++) if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p) { bool want_removal = (root_id == 0) || (node->ID != root_id && DockNodeGetRootNode(node)->ID == root_id); if (want_removal) { if (node->IsCentralNode()) has_central_node = true; if (root_id != 0) DockContextQueueNotifyRemovedNode(ctx, node); if (root_node) DockNodeMoveWindows(root_node, node); nodes_to_remove.push_back(node); } } // DockNodeMoveWindows->DockNodeAddWindow will normally set those when reaching two windows (which is only adequate during interactive merge) // Make sure we don't lose our current pos/size. (FIXME-DOCK: Consider tidying up that code in DockNodeAddWindow instead) if (root_node) { root_node->AuthorityForPos = backup_root_node_authority_for_pos; root_node->AuthorityForSize = backup_root_node_authority_for_size; } // Apply to settings for (int settings_n = 0; settings_n < ctx->SettingsWindows.Size; settings_n++) if (ImGuiID window_settings_dock_id = ctx->SettingsWindows[settings_n].DockId) for (int n = 0; n < nodes_to_remove.Size; n++) if (nodes_to_remove[n]->ID == window_settings_dock_id) { ctx->SettingsWindows[settings_n].DockId = root_id; break; } // Not really efficient, but easier to destroy a whole hierarchy considering DockContextRemoveNode is attempting to merge nodes if (nodes_to_remove.Size > 1) ImQsort(nodes_to_remove.Data, nodes_to_remove.Size, sizeof(ImGuiDockNode*), DockNodeComparerDepthMostFirst); for (int n = 0; n < nodes_to_remove.Size; n++) DockContextRemoveNode(ctx, nodes_to_remove[n], false); if (root_id == 0) { dc->Nodes.Clear(); dc->Requests.clear(); } else if (has_central_node) { root_node->LocalFlags |= ImGuiDockNodeFlags_CentralNode; root_node->CentralNode = root_node; } } void ImGui::DockBuilderRemoveNodeDockedWindows(ImGuiID root_id, bool clear_persistent_docking_references) { // Clear references in settings ImGuiContext* ctx = GImGui; ImGuiContext& g = *ctx; if (clear_persistent_docking_references) { for (int n = 0; n < g.SettingsWindows.Size; n++) { ImGuiWindowSettings* settings = &g.SettingsWindows[n]; bool want_removal = (root_id == 0) || (settings->DockId == root_id); if (!want_removal && settings->DockId != 0) if (ImGuiDockNode* node = DockContextFindNodeByID(ctx, settings->DockId)) if (DockNodeGetRootNode(node)->ID == root_id) want_removal = true; if (want_removal) settings->DockId = 0; } } // Clear references in windows for (int n = 0; n < g.Windows.Size; n++) { ImGuiWindow* window = g.Windows[n]; bool want_removal = (root_id == 0) || (window->DockNode && DockNodeGetRootNode(window->DockNode)->ID == root_id) || (window->DockNodeAsHost && window->DockNodeAsHost->ID == root_id); if (want_removal) { const ImGuiID backup_dock_id = window->DockId; DockContextProcessUndockWindow(ctx, window, clear_persistent_docking_references); if (!clear_persistent_docking_references) IM_ASSERT(window->DockId == backup_dock_id); } } } // FIXME-DOCK: We are not exposing nor using split_outer. ImGuiID ImGui::DockBuilderSplitNode(ImGuiID id, ImGuiDir split_dir, float size_ratio_for_node_at_dir, ImGuiID* out_id_at_dir, ImGuiID* out_id_other) { ImGuiContext* ctx = GImGui; IM_ASSERT(split_dir != ImGuiDir_None); ImGuiDockNode* node = DockContextFindNodeByID(ctx, id); if (node == NULL) { IM_ASSERT(node != NULL); return 0; } IM_ASSERT(!node->IsSplitNode()); // Assert if already Split ImGuiDockRequest req; req.Type = ImGuiDockRequestType_Split; req.DockTargetWindow = NULL; req.DockTargetNode = node; req.DockPayload = NULL; req.DockSplitDir = split_dir; 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); req.DockSplitOuter = false; DockContextProcessDock(ctx, &req); ImGuiID id_at_dir = node->ChildNodes[(split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Up) ? 0 : 1]->ID; ImGuiID id_other = node->ChildNodes[(split_dir == ImGuiDir_Left || split_dir == ImGuiDir_Up) ? 1 : 0]->ID; if (out_id_at_dir) *out_id_at_dir = id_at_dir; if (out_id_other) *out_id_other = id_other; return id_at_dir; } static ImGuiDockNode* DockBuilderCopyNodeRec(ImGuiDockNode* src_node, ImGuiID dst_node_id_if_known, ImVector* out_node_remap_pairs) { ImGuiContext* ctx = GImGui; ImGuiDockNode* dst_node = ImGui::DockContextAddNode(ctx, dst_node_id_if_known); dst_node->SharedFlags = src_node->SharedFlags; dst_node->LocalFlags = src_node->LocalFlags; dst_node->Pos = src_node->Pos; dst_node->Size = src_node->Size; dst_node->SizeRef = src_node->SizeRef; dst_node->SplitAxis = src_node->SplitAxis; out_node_remap_pairs->push_back(src_node->ID); out_node_remap_pairs->push_back(dst_node->ID); for (int child_n = 0; child_n < IM_ARRAYSIZE(src_node->ChildNodes); child_n++) if (src_node->ChildNodes[child_n]) { dst_node->ChildNodes[child_n] = DockBuilderCopyNodeRec(src_node->ChildNodes[child_n], 0, out_node_remap_pairs); dst_node->ChildNodes[child_n]->ParentNode = dst_node; } //IMGUI_DEBUG_LOG("Fork node %08X -> %08X (%d childs)\n", src_node->ID, dst_node->ID, dst_node->IsSplitNode() ? 2 : 0); return dst_node; } void ImGui::DockBuilderCopyNode(ImGuiID src_node_id, ImGuiID dst_node_id, ImVector* out_node_remap_pairs) { ImGuiContext* ctx = GImGui; IM_ASSERT(src_node_id != 0); IM_ASSERT(dst_node_id != 0); IM_ASSERT(out_node_remap_pairs != NULL); ImGuiDockNode* src_node = DockContextFindNodeByID(ctx, src_node_id); IM_ASSERT(src_node != NULL); out_node_remap_pairs->clear(); DockBuilderRemoveNode(dst_node_id); DockBuilderCopyNodeRec(src_node, dst_node_id, out_node_remap_pairs); IM_ASSERT((out_node_remap_pairs->Size % 2) == 0); } void ImGui::DockBuilderCopyWindowSettings(const char* src_name, const char* dst_name) { ImGuiWindow* src_window = FindWindowByName(src_name); if (src_window == NULL) return; if (ImGuiWindow* dst_window = FindWindowByName(dst_name)) { dst_window->Pos = src_window->Pos; dst_window->Size = src_window->Size; dst_window->SizeFull = src_window->SizeFull; dst_window->Collapsed = src_window->Collapsed; } else if (ImGuiWindowSettings* dst_settings = FindOrCreateWindowSettings(dst_name)) { if (src_window->ViewportId != 0 && src_window->ViewportId != IMGUI_VIEWPORT_DEFAULT_ID) { dst_settings->ViewportPos = src_window->Pos; dst_settings->ViewportId = src_window->ViewportId; dst_settings->Pos = ImVec2(0.0f, 0.0f); } else { dst_settings->Pos = src_window->Pos; } dst_settings->Size = src_window->SizeFull; dst_settings->Collapsed = src_window->Collapsed; } } // FIXME: Will probably want to change this signature, in particular how the window remapping pairs are passed. void ImGui::DockBuilderCopyDockSpace(ImGuiID src_dockspace_id, ImGuiID dst_dockspace_id, ImVector* in_window_remap_pairs) { IM_ASSERT(src_dockspace_id != 0); IM_ASSERT(dst_dockspace_id != 0); IM_ASSERT(in_window_remap_pairs != NULL); IM_ASSERT((in_window_remap_pairs->Size % 2) == 0); // Duplicate entire dock // 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, // whereas we could attempt to at least keep them together in a new, same floating node. ImVector node_remap_pairs; DockBuilderCopyNode(src_dockspace_id, dst_dockspace_id, &node_remap_pairs); // Attempt to transition all the upcoming windows associated to dst_dockspace_id into the newly created hierarchy of dock nodes // (The windows associated to src_dockspace_id are staying in place) ImVector src_windows; for (int remap_window_n = 0; remap_window_n < in_window_remap_pairs->Size; remap_window_n += 2) { const char* src_window_name = (*in_window_remap_pairs)[remap_window_n]; const char* dst_window_name = (*in_window_remap_pairs)[remap_window_n + 1]; ImGuiID src_window_id = ImHashStr(src_window_name); src_windows.push_back(src_window_id); // Search in the remapping tables ImGuiID src_dock_id = 0; if (ImGuiWindow* src_window = FindWindowByID(src_window_id)) src_dock_id = src_window->DockId; else if (ImGuiWindowSettings* src_window_settings = FindWindowSettings(src_window_id)) src_dock_id = src_window_settings->DockId; ImGuiID dst_dock_id = 0; for (int dock_remap_n = 0; dock_remap_n < node_remap_pairs.Size; dock_remap_n += 2) if (node_remap_pairs[dock_remap_n] == src_dock_id) { dst_dock_id = node_remap_pairs[dock_remap_n + 1]; //node_remap_pairs[dock_remap_n] = node_remap_pairs[dock_remap_n + 1] = 0; // Clear break; } if (dst_dock_id != 0) { // Docked windows gets redocked into the new node hierarchy. //IMGUI_DEBUG_LOG("Remap window '%s' %08X -> %08X\n", dst_window_name, src_dock_id, dst_dock_id); DockBuilderDockWindow(dst_window_name, dst_dock_id); } else { // Floating windows gets their settings transferred (regardless of whether the new window already exist or not) // When this is leading to a Copy and not a Move, we would get two overlapping floating windows. Could we possibly dock them together? DockBuilderCopyWindowSettings(src_window_name, dst_window_name); } } // 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") // Find those windows and move to them to the cloned dock node. This may be optional? for (int dock_remap_n = 0; dock_remap_n < node_remap_pairs.Size; dock_remap_n += 2) if (ImGuiID src_dock_id = node_remap_pairs[dock_remap_n]) { ImGuiID dst_dock_id = node_remap_pairs[dock_remap_n + 1]; ImGuiDockNode* node = DockBuilderGetNode(src_dock_id); for (int window_n = 0; window_n < node->Windows.Size; window_n++) { ImGuiWindow* window = node->Windows[window_n]; if (src_windows.contains(window->ID)) continue; // Docked windows gets redocked into the new node hierarchy. //IMGUI_DEBUG_LOG("Remap window '%s' %08X -> %08X\n", window->Name, src_dock_id, dst_dock_id); DockBuilderDockWindow(window->Name, dst_dock_id); } } } void ImGui::DockBuilderFinish(ImGuiID root_id) { ImGuiContext* ctx = GImGui; //DockContextRebuild(ctx); DockContextBuildAddWindowsToNodes(ctx, root_id); } //----------------------------------------------------------------------------- // Docking: Begin/End Functions (called from Begin/End) //----------------------------------------------------------------------------- void ImGui::BeginDocked(ImGuiWindow* window, bool* p_open) { ImGuiContext* ctx = GImGui; ImGuiContext& g = *ctx; const bool auto_dock_node = (g.IO.ConfigDockingTabBarOnSingleWindows) && !(window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDocking)); if (auto_dock_node) { if (window->DockId == 0) { IM_ASSERT(window->DockNode == NULL); window->DockId = DockContextGenNodeID(ctx); } } else { // Calling SetNextWindowPos() undock windows by default (by setting PosUndock) bool want_undock = false; want_undock |= (window->Flags & ImGuiWindowFlags_NoDocking) != 0; want_undock |= (g.NextWindowData.PosCond && (window->SetWindowPosAllowFlags & g.NextWindowData.PosCond) && g.NextWindowData.PosUndock); g.NextWindowData.PosUndock = false; if (want_undock) { DockContextProcessUndockWindow(ctx, window); return; } } // Bind to our dock node ImGuiDockNode* node = window->DockNode; if (node != NULL) IM_ASSERT(window->DockId == node->ID); if (window->DockId != 0 && node == NULL) { node = DockContextFindNodeByID(ctx, window->DockId); // We should not be docking into a split node (SetWindowDock should avoid this) if (node && node->IsSplitNode()) { DockContextProcessUndockWindow(ctx, window); return; } // Create node if (node == NULL) { node = DockContextAddNode(ctx, window->DockId); node->AuthorityForPos = node->AuthorityForSize = node->AuthorityForViewport = ImGuiDataAuthority_Window; if (auto_dock_node) node->LastFrameAlive = g.FrameCount; } DockNodeAddWindow(node, window, true); IM_ASSERT(node == window->DockNode); } #if 0 // Undock if the ImGuiDockNodeFlags_NoDockingInCentralNode got set if (node->IsCentralNode && (node->Flags & ImGuiDockNodeFlags_NoDockingInCentralNode)) { DockContextProcessUndockWindow(ctx, window); return; } #endif // Undock if our dockspace node disappeared // Note how we are testing for LastFrameAlive and NOT LastFrameActive. A DockSpace node can be maintained alive while being inactive with ImGuiDockNodeFlags_KeepAliveOnly. if (node->LastFrameAlive < g.FrameCount) { // If the window has been orphaned, transition the docknode to an implicit node processed in DockContextUpdateDocking() ImGuiDockNode* root_node = DockNodeGetRootNode(node); if (root_node->LastFrameAlive < g.FrameCount) { DockContextProcessUndockWindow(ctx, window); } else { window->DockIsActive = true; window->DockTabIsVisible = false; } return; } // Undock if we are submitted earlier than the host window if (node->HostWindow && window->BeginOrderWithinContext < node->HostWindow->BeginOrderWithinContext) { DockContextProcessUndockWindow(ctx, window); return; } // FIXME-DOCK: replace ->HostWindow NULL compare with something more explicit (~was initially intended as a first frame test) if (node->HostWindow == NULL) { window->DockTabIsVisible = false; return; } IM_ASSERT(node->HostWindow); IM_ASSERT(node->IsLeafNode()); // Position window SetNextWindowPos(node->Pos); SetNextWindowSize(node->Size); g.NextWindowData.PosUndock = false; // Cancel implicit undocking of SetNextWindowPos() window->DockIsActive = true; window->DockTabIsVisible = false; if (node->SharedFlags & ImGuiDockNodeFlags_KeepAliveOnly) return; // When the window is selected we mark it as visible. if (node->VisibleWindow == window) window->DockTabIsVisible = true; // Update window flag IM_ASSERT((window->Flags & ImGuiWindowFlags_ChildWindow) == 0); window->Flags |= ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_NoResize; if (node->IsHiddenTabBar() || node->IsNoTabBar()) window->Flags |= ImGuiWindowFlags_NoTitleBar; else 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! // Save new dock order only if the tab bar has been visible once. // This allows multiple windows to be created in the same frame and have their respective dock orders preserved. if (node->TabBar && node->TabBar->CurrFrameVisible != -1) window->DockOrder = (short)DockNodeGetTabOrder(window); if ((node->WantCloseAll || node->WantCloseTabID == window->ID) && p_open != NULL) *p_open = false; // Update ChildId to allow returning from Child to Parent with Escape ImGuiWindow* parent_window = window->DockNode->HostWindow; window->ChildId = parent_window->GetID(window->Name); } void ImGui::BeginAsDockableDragDropSource(ImGuiWindow* window) { ImGuiContext& g = *GImGui; IM_ASSERT(g.ActiveId == window->MoveId); window->DC.LastItemId = window->MoveId; window = window->RootWindow; IM_ASSERT((window->Flags & ImGuiWindowFlags_NoDocking) == 0); bool is_drag_docking = (g.IO.ConfigDockingWithShift) || ImRect(0, 0, window->SizeFull.x, GetFrameHeight()).Contains(g.ActiveIdClickOffset); if (is_drag_docking && BeginDragDropSource(ImGuiDragDropFlags_SourceNoPreviewTooltip | ImGuiDragDropFlags_SourceNoHoldToOpenOthers | ImGuiDragDropFlags_SourceAutoExpirePayload)) { SetDragDropPayload(IMGUI_PAYLOAD_TYPE_WINDOW, &window, sizeof(window)); EndDragDropSource(); } } void ImGui::BeginAsDockableDragDropTarget(ImGuiWindow* window) { ImGuiContext* ctx = GImGui; ImGuiContext& g = *ctx; //IM_ASSERT(window->RootWindow == window); // May also be a DockSpace IM_ASSERT((window->Flags & ImGuiWindowFlags_NoDocking) == 0); if (!g.DragDropActive) return; if (!BeginDragDropTargetCustom(window->Rect(), window->ID)) return; // Peek into the payload before calling AcceptDragDropPayload() so we can handle overlapping dock nodes with filtering // (this is a little unusual pattern, normally most code would call AcceptDragDropPayload directly) const ImGuiPayload* payload = &g.DragDropPayload; if (!payload->IsDataType(IMGUI_PAYLOAD_TYPE_WINDOW) || !DockNodeIsDropAllowed(window, *(ImGuiWindow**)payload->Data)) { EndDragDropTarget(); return; } ImGuiWindow* payload_window = *(ImGuiWindow**)payload->Data; if (AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_WINDOW, ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect)) { // Select target node ImGuiDockNode* node = NULL; bool allow_null_target_node = false; if (window->DockNodeAsHost) node = DockNodeTreeFindNodeByPos(window->DockNodeAsHost, g.IO.MousePos); else if (window->DockNode) // && window->DockIsActive) node = window->DockNode; else allow_null_target_node = true; // Dock into a regular window 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())); const bool is_explicit_target = g.IO.ConfigDockingWithShift || IsMouseHoveringRect(explicit_target_rect.Min, explicit_target_rect.Max); // Preview docking request and find out split direction/ratio //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. const bool do_preview = payload->IsPreview() || payload->IsDelivery(); if (do_preview && (node != NULL || allow_null_target_node)) { ImGuiDockPreviewData split_inner, split_outer; ImGuiDockPreviewData* split_data = &split_inner; if (node && (node->ParentNode || node->IsCentralNode())) if (ImGuiDockNode* root_node = DockNodeGetRootNode(node)) { DockNodePreviewDockCalc(window, root_node, payload_window, &split_outer, is_explicit_target, true); if (split_outer.IsSplitDirExplicit) split_data = &split_outer; } DockNodePreviewDockCalc(window, node, payload_window, &split_inner, is_explicit_target, false); if (split_data == &split_outer) split_inner.IsDropAllowed = false; // Draw inner then outer, so that previewed tab (in inner data) will be behind the outer drop boxes DockNodePreviewDockRender(window, node, payload_window, &split_inner); DockNodePreviewDockRender(window, node, payload_window, &split_outer); // Queue docking request if (split_data->IsDropAllowed && payload->IsDelivery()) DockContextQueueDock(ctx, window, split_data->SplitNode, payload_window, split_data->SplitDir, split_data->SplitRatio, split_data == &split_outer); } } EndDragDropTarget(); } //----------------------------------------------------------------------------- // Docking: Settings //----------------------------------------------------------------------------- static void ImGui::DockSettingsRenameNodeReferences(ImGuiID old_node_id, ImGuiID new_node_id) { ImGuiContext& g = *GImGui; for (int window_n = 0; window_n < g.Windows.Size; window_n++) { ImGuiWindow* window = g.Windows[window_n]; if (window->DockId == old_node_id && window->DockNode == NULL) window->DockId = new_node_id; } 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 { ImGuiWindowSettings* window_settings = &g.SettingsWindows[settings_n]; if (window_settings->DockId == old_node_id) window_settings->DockId = new_node_id; } } // Remove references stored in ImGuiWindowSettings to the given ImGuiDockNodeSettings static void ImGui::DockSettingsRemoveNodeReferences(ImGuiID* node_ids, int node_ids_count) { ImGuiContext& g = *GImGui; int found = 0; 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 { ImGuiWindowSettings* window_settings = &g.SettingsWindows[settings_n]; for (int node_n = 0; node_n < node_ids_count; node_n++) if (window_settings->DockId == node_ids[node_n]) { window_settings->DockId = 0; window_settings->DockOrder = -1; if (++found < node_ids_count) break; return; } } } static ImGuiDockNodeSettings* ImGui::DockSettingsFindNodeSettings(ImGuiContext* ctx, ImGuiID id) { // FIXME-OPT ImGuiDockContext* dc = ctx->DockContext; for (int n = 0; n < dc->SettingsNodes.Size; n++) if (dc->SettingsNodes[n].ID == id) return &dc->SettingsNodes[n]; return NULL; } static void* ImGui::DockSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) { if (strcmp(name, "Data") != 0) return NULL; return (void*)1; } static void ImGui::DockSettingsHandler_ReadLine(ImGuiContext* ctx, ImGuiSettingsHandler*, void*, const char* line) { char c = 0; int x = 0, y = 0; int r = 0; // Parsing, e.g. // " DockNode ID=0x00000001 Pos=383,193 Size=201,322 Split=Y,0.506 " // " DockNode ID=0x00000002 Parent=0x00000001 " ImGuiDockNodeSettings node; line = ImStrSkipBlank(line); if (strncmp(line, "DockNode", 8) == 0) { line = ImStrSkipBlank(line + strlen("DockNode")); } else if (strncmp(line, "DockSpace", 9) == 0) { line = ImStrSkipBlank(line + strlen("DockSpace")); node.IsDockSpace = true; } else return; if (sscanf(line, "ID=0x%08X%n", &node.ID, &r) == 1) { line += r; } else return; if (sscanf(line, " Parent=0x%08X%n", &node.ParentID, &r) == 1) { line += r; if (node.ParentID == 0) return; } if (node.ParentID == 0) { if (sscanf(line, " Pos=%i,%i%n", &x, &y, &r) == 2) { line += r; node.Pos = ImVec2ih((short)x, (short)y); } else return; if (sscanf(line, " Size=%i,%i%n", &x, &y, &r) == 2) { line += r; node.Size = ImVec2ih((short)x, (short)y); } else return; } else { if (sscanf(line, " SizeRef=%i,%i%n", &x, &y, &r) == 2) { line += r; node.SizeRef = ImVec2ih((short)x, (short)y); } } 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; } if (sscanf(line, " CentralNode=%d%n", &x, &r) == 1) { line += r; node.IsCentralNode = (x != 0); } if (sscanf(line, " HiddenTabBar=%d%n", &x, &r) == 1) { line += r; node.IsHiddenTabBar = (x != 0); } if (sscanf(line, " SelectedTab=0x%08X%n", &node.SelectedTabID,&r) == 1) { line += r; } ImGuiDockContext* dc = ctx->DockContext; if (node.ParentID != 0) if (ImGuiDockNodeSettings* parent_settings = DockSettingsFindNodeSettings(ctx, node.ParentID)) node.Depth = parent_settings->Depth + 1; dc->SettingsNodes.push_back(node); } static void DockSettingsHandler_DockNodeToSettings(ImGuiDockContext* dc, ImGuiDockNode* node, int depth) { ImGuiDockNodeSettings node_settings; IM_ASSERT(depth < (1 << (sizeof(node_settings.Depth) << 3))); node_settings.ID = node->ID; node_settings.ParentID = node->ParentNode ? node->ParentNode->ID : 0; node_settings.SelectedTabID = node->SelectedTabID; node_settings.SplitAxis = node->IsSplitNode() ? (char)node->SplitAxis : ImGuiAxis_None; node_settings.Depth = (char)depth; node_settings.IsDockSpace = (char)node->IsDockSpace(); node_settings.IsCentralNode = (char)node->IsCentralNode(); node_settings.IsHiddenTabBar = (char)node->IsHiddenTabBar(); node_settings.Pos = ImVec2ih((short)node->Pos.x, (short)node->Pos.y); node_settings.Size = ImVec2ih((short)node->Size.x, (short)node->Size.y); node_settings.SizeRef = ImVec2ih((short)node->SizeRef.x, (short)node->SizeRef.y); dc->SettingsNodes.push_back(node_settings); if (node->ChildNodes[0]) DockSettingsHandler_DockNodeToSettings(dc, node->ChildNodes[0], depth + 1); if (node->ChildNodes[1]) DockSettingsHandler_DockNodeToSettings(dc, node->ChildNodes[1], depth + 1); } static void ImGui::DockSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) { ImGuiContext& g = *ctx; ImGuiDockContext* dc = g.DockContext; if (!(g.IO.ConfigFlags & ImGuiConfigFlags_DockingEnable)) return; // Gather settings data // (unlike our windows settings, because nodes are always built we can do a full rewrite of the SettingsNode buffer) dc->SettingsNodes.resize(0); dc->SettingsNodes.reserve(dc->Nodes.Data.Size); for (int n = 0; n < dc->Nodes.Data.Size; n++) if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p) if (node->IsRootNode()) DockSettingsHandler_DockNodeToSettings(dc, node, 0); int max_depth = 0; for (int node_n = 0; node_n < dc->SettingsNodes.Size; node_n++) max_depth = ImMax((int)dc->SettingsNodes[node_n].Depth, max_depth); // Write to text buffer buf->appendf("[%s][Data]\n", handler->TypeName); for (int node_n = 0; node_n < dc->SettingsNodes.Size; node_n++) { const int line_start_pos = buf->size(); (void)line_start_pos; const ImGuiDockNodeSettings* node_settings = &dc->SettingsNodes[node_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 buf->appendf(" ID=0x%08X", node_settings->ID); if (node_settings->ParentID) buf->appendf(" Parent=0x%08X SizeRef=%d,%d", node_settings->ParentID, node_settings->SizeRef.x, node_settings->SizeRef.y); else buf->appendf(" Pos=%d,%d Size=%d,%d", node_settings->Pos.x, node_settings->Pos.y, node_settings->Size.x, node_settings->Size.y); if (node_settings->SplitAxis != ImGuiAxis_None) buf->appendf(" Split=%c", (node_settings->SplitAxis == ImGuiAxis_X) ? 'X' : 'Y'); if (node_settings->IsCentralNode) buf->appendf(" CentralNode=1"); if (node_settings->IsHiddenTabBar) buf->appendf(" HiddenTabBar=1"); if (node_settings->SelectedTabID) buf->appendf(" SelectedTab=0x%08X", node_settings->SelectedTabID); #if IMGUI_DEBUG_DOCKING_INI // [DEBUG] Include comments in the .ini file to ease debugging if (ImGuiDockNode* node = DockContextFindNodeByID(ctx, node_settings->ID)) { buf->appendf("%*s", ImMax(2, (line_start_pos + 92) - buf->size()), ""); // Align everything if (node->IsDockSpace && node->HostWindow && node->HostWindow->ParentWindow) buf->appendf(" ; in '%s'", node->HostWindow->ParentWindow->Name); int contains_window = 0; for (int window_n = 0; window_n < ctx->SettingsWindows.Size; window_n++) if (ctx->SettingsWindows[window_n].DockId == node_settings->ID) { if (contains_window++ == 0) buf->appendf(" ; contains "); buf->appendf("'%s' ", ctx->SettingsWindows[window_n].Name); } } #endif buf->appendf("\n"); } buf->appendf("\n"); } //----------------------------------------------------------------------------- // [SECTION] PLATFORM DEPENDENT HELPERS //----------------------------------------------------------------------------- #if defined(_WIN32) && !defined(_WINDOWS_) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && (!defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) || !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS)) #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #ifndef __MINGW32__ #include #else #include #endif #endif // Win32 API clipboard implementation #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) #ifdef _MSC_VER #pragma comment(lib, "user32") #endif static const char* GetClipboardTextFn_DefaultImpl(void*) { static ImVector buf_local; buf_local.clear(); if (!::OpenClipboard(NULL)) return NULL; HANDLE wbuf_handle = ::GetClipboardData(CF_UNICODETEXT); if (wbuf_handle == NULL) { ::CloseClipboard(); return NULL; } if (ImWchar* wbuf_global = (ImWchar*)::GlobalLock(wbuf_handle)) { int buf_len = ImTextCountUtf8BytesFromStr(wbuf_global, NULL) + 1; buf_local.resize(buf_len); ImTextStrToUtf8(buf_local.Data, buf_len, wbuf_global, NULL); } ::GlobalUnlock(wbuf_handle); ::CloseClipboard(); return buf_local.Data; } static void SetClipboardTextFn_DefaultImpl(void*, const char* text) { if (!::OpenClipboard(NULL)) return; const int wbuf_length = ImTextCountCharsFromUtf8(text, NULL) + 1; HGLOBAL wbuf_handle = ::GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)wbuf_length * sizeof(ImWchar)); if (wbuf_handle == NULL) { ::CloseClipboard(); return; } ImWchar* wbuf_global = (ImWchar*)::GlobalLock(wbuf_handle); ImTextStrFromUtf8(wbuf_global, wbuf_length, text, NULL); ::GlobalUnlock(wbuf_handle); ::EmptyClipboard(); if (::SetClipboardData(CF_UNICODETEXT, wbuf_handle) == NULL) ::GlobalFree(wbuf_handle); ::CloseClipboard(); } #else // Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers static const char* GetClipboardTextFn_DefaultImpl(void*) { ImGuiContext& g = *GImGui; return g.PrivateClipboard.empty() ? NULL : g.PrivateClipboard.begin(); } // Local ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers static void SetClipboardTextFn_DefaultImpl(void*, const char* text) { ImGuiContext& g = *GImGui; g.PrivateClipboard.clear(); const char* text_end = text + strlen(text); g.PrivateClipboard.resize((int)(text_end - text) + 1); memcpy(&g.PrivateClipboard[0], text, (size_t)(text_end - text)); g.PrivateClipboard[(int)(text_end - text)] = 0; } #endif //----------------------------------------------------------------------------- // [SECTION] METRICS/DEBUG WINDOW //----------------------------------------------------------------------------- static void RenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* viewport, const ImRect& bb) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImVec2 scale = bb.GetSize() / viewport->Size; ImVec2 off = bb.Min - viewport->Pos * scale; float alpha_mul = (viewport->Flags & ImGuiViewportFlags_Minimized) ? 0.30f : 1.00f; window->DrawList->AddRectFilled(bb.Min, bb.Max, ImGui::GetColorU32(ImGuiCol_Border, alpha_mul * 0.40f)); for (int i = 0; i != g.Windows.Size; i++) { ImGuiWindow* thumb_window = g.Windows[i]; if (!thumb_window->WasActive || ((thumb_window->Flags & ImGuiWindowFlags_ChildWindow))) continue; if (thumb_window->SkipItems && (thumb_window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME-DOCK: Skip hidden docked windows. Identify those betters. continue; if (thumb_window->Viewport != viewport) continue; ImRect thumb_r = thumb_window->Rect(); ImRect title_r = thumb_window->TitleBarRect(); ImRect thumb_r_scaled = ImRect(ImFloor(off + thumb_r.Min * scale), ImFloor(off + thumb_r.Max * scale)); 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 thumb_r_scaled.ClipWithFull(bb); title_r_scaled.ClipWithFull(bb); const bool window_is_focused = (g.NavWindow && thumb_window->RootWindowForTitleBarHighlight == g.NavWindow->RootWindowForTitleBarHighlight); window->DrawList->AddRectFilled(thumb_r_scaled.Min, thumb_r_scaled.Max, ImGui::GetColorU32(ImGuiCol_WindowBg, alpha_mul)); window->DrawList->AddRectFilled(title_r_scaled.Min, title_r_scaled.Max, ImGui::GetColorU32(window_is_focused ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg, alpha_mul)); window->DrawList->AddRect(thumb_r_scaled.Min, thumb_r_scaled.Max, ImGui::GetColorU32(ImGuiCol_Border, alpha_mul)); if (ImGuiWindow* window_for_title = GetWindowForTitleDisplay(thumb_window)) 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)); } draw_list->AddRect(bb.Min, bb.Max, ImGui::GetColorU32(ImGuiCol_Border, alpha_mul)); } void ImGui::ShowViewportThumbnails() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; // 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. float SCALE = 1.0f / 8.0f; ImRect bb_full; //for (int n = 0; n < g.PlatformIO.Monitors.Size; n++) // bb_full.Add(GetPlatformMonitorMainRect(g.PlatformIO.Monitors[n])); for (int n = 0; n < g.Viewports.Size; n++) bb_full.Add(g.Viewports[n]->GetRect()); ImVec2 p = window->DC.CursorPos; ImVec2 off = p - bb_full.Min * SCALE; //for (int n = 0; n < g.PlatformIO.Monitors.Size; 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)); for (int n = 0; n < g.Viewports.Size; n++) { ImGuiViewportP* viewport = g.Viewports[n]; ImRect viewport_draw_bb(off + (viewport->Pos) * SCALE, off + (viewport->Pos + viewport->Size) * SCALE); RenderViewportThumbnail(window->DrawList, viewport, viewport_draw_bb); } ImGui::Dummy(bb_full.GetSize() * SCALE); } void ImGui::ShowMetricsWindow(bool* p_open) { if (!ImGui::Begin("ImGui Metrics", p_open)) { ImGui::End(); return; } enum { RT_OuterRect, RT_OuterRectClipped, RT_InnerMainRect, RT_InnerClipRect, RT_ContentsRegionRect, RT_ContentsFullRect }; static bool show_windows_begin_order = false; static bool show_windows_rects = false; static int show_windows_rect_type = RT_ContentsRegionRect; static bool show_drawcmd_clip_rects = true; ImGuiIO& io = ImGui::GetIO(); ImGui::Text("Dear ImGui %s", ImGui::GetVersion()); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); ImGui::Text("%d vertices, %d indices (%d triangles)", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3); ImGui::Text("%d active windows (%d visible)", io.MetricsActiveWindows, io.MetricsRenderWindows); ImGui::Text("%d active allocations", io.MetricsActiveAllocations); ImGui::Separator(); struct Funcs { static void NodeDrawList(ImGuiWindow* window, ImGuiViewportP* viewport, ImDrawList* draw_list, const char* label) { 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); if (draw_list == ImGui::GetWindowDrawList()) { ImGui::SameLine(); 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) if (node_open) ImGui::TreePop(); return; } ImDrawList* fg_draw_list = viewport ? GetForegroundDrawList(viewport) : NULL; // Render additional visuals into the top-most draw list if (window && fg_draw_list && ImGui::IsItemHovered()) fg_draw_list->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); if (!node_open) return; int elem_offset = 0; for (const ImDrawCmd* pcmd = draw_list->CmdBuffer.begin(); pcmd < draw_list->CmdBuffer.end(); elem_offset += pcmd->ElemCount, pcmd++) { if (pcmd->UserCallback == NULL && pcmd->ElemCount == 0) continue; if (pcmd->UserCallback) { ImGui::BulletText("Callback %p, user_data %p", pcmd->UserCallback, pcmd->UserCallbackData); continue; } ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; 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); if (show_drawcmd_clip_rects && fg_draw_list && ImGui::IsItemHovered()) { ImRect clip_rect = pcmd->ClipRect; ImRect vtxs_rect; for (int i = elem_offset; i < elem_offset + (int)pcmd->ElemCount; i++) vtxs_rect.Add(draw_list->VtxBuffer[idx_buffer ? idx_buffer[i] : i].pos); clip_rect.Floor(); fg_draw_list->AddRect(clip_rect.Min, clip_rect.Max, IM_COL32(255,255,0,255)); vtxs_rect.Floor(); fg_draw_list->AddRect(vtxs_rect.Min, vtxs_rect.Max, IM_COL32(255,0,255,255)); } if (!pcmd_node_open) continue; // Display individual triangles/vertices. Hover on to get the corresponding triangle highlighted. ImGuiListClipper clipper(pcmd->ElemCount/3); // Manually coarse clip our print out of individual vertices to save CPU, only items that may be visible. while (clipper.Step()) for (int prim = clipper.DisplayStart, idx_i = elem_offset + clipper.DisplayStart*3; prim < clipper.DisplayEnd; prim++) { char buf[300]; char *buf_p = buf, *buf_end = buf + IM_ARRAYSIZE(buf); ImVec2 triangles_pos[3]; for (int n = 0; n < 3; n++, idx_i++) { int vtx_i = idx_buffer ? idx_buffer[idx_i] : idx_i; ImDrawVert& v = draw_list->VtxBuffer[vtx_i]; triangles_pos[n] = v.pos; buf_p += ImFormatString(buf_p, buf_end - buf_p, "%s %04d: pos (%8.2f,%8.2f), uv (%.6f,%.6f), col %08X\n", (n == 0) ? "idx" : " ", idx_i, v.pos.x, v.pos.y, v.uv.x, v.uv.y, v.col); } ImGui::Selectable(buf, false); if (fg_draw_list && ImGui::IsItemHovered()) { ImDrawListFlags backup_flags = fg_draw_list->Flags; fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines at is more readable for very large and thin triangles. fg_draw_list->AddPolyline(triangles_pos, 3, IM_COL32(255,255,0,255), true, 1.0f); fg_draw_list->Flags = backup_flags; } } ImGui::TreePop(); } ImGui::TreePop(); } static void NodeColumns(const ImGuiColumns* columns) { if (!ImGui::TreeNode((void*)(uintptr_t)columns->ID, "Columns Id: 0x%08X, Count: %d, Flags: 0x%04X", columns->ID, columns->Count, columns->Flags)) return; ImGui::BulletText("Width: %.1f (MinX: %.1f, MaxX: %.1f)", columns->MaxX - columns->MinX, columns->MinX, columns->MaxX); for (int column_n = 0; column_n < columns->Columns.Size; column_n++) ImGui::BulletText("Column %02d: OffsetNorm %.3f (= %.1f px)", column_n, columns->Columns[column_n].OffsetNorm, OffsetNormToPixels(columns, columns->Columns[column_n].OffsetNorm)); ImGui::TreePop(); } static void NodeWindows(ImVector& windows, const char* label) { if (!ImGui::TreeNode(label, "%s (%d)", label, windows.Size)) return; for (int i = 0; i < windows.Size; i++) Funcs::NodeWindow(windows[i], "Window"); ImGui::TreePop(); } static void NodeWindow(ImGuiWindow* window, const char* label) { if (!ImGui::TreeNode(window, "%s '%s', %d @ 0x%p", label, window->Name, window->Active || window->WasActive, window)) return; ImGuiWindowFlags flags = window->Flags; NodeDrawList(window, window->Viewport, window->DrawList, "DrawList"); 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); ImGui::BulletText("Flags: 0x%08X (%s%s%s%s%s%s%s%s%s..)", flags, (flags & ImGuiWindowFlags_ChildWindow) ? "Child " : "", (flags & ImGuiWindowFlags_Tooltip) ? "Tooltip " : "", (flags & ImGuiWindowFlags_Popup) ? "Popup " : "", (flags & ImGuiWindowFlags_Modal) ? "Modal " : "", (flags & ImGuiWindowFlags_ChildMenu) ? "ChildMenu " : "", (flags & ImGuiWindowFlags_NoSavedSettings) ? "NoSavedSettings " : "", (flags & ImGuiWindowFlags_NoMouseInputs)? "NoMouseInputs":"", (flags & ImGuiWindowFlags_NoNavInputs) ? "NoNavInputs" : "", (flags & ImGuiWindowFlags_AlwaysAutoResize) ? "AlwaysAutoResize" : ""); ImGui::BulletText("Scroll: (%.2f/%.2f,%.2f/%.2f)", window->Scroll.x, GetWindowScrollMaxX(window), window->Scroll.y, GetWindowScrollMaxY(window)); ImGui::BulletText("Active: %d/%d, WriteAccessed: %d, BeginOrderWithinContext: %d", window->Active, window->WasActive, window->WriteAccessed, (window->Active || window->WasActive) ? window->BeginOrderWithinContext : -1); ImGui::BulletText("Appearing: %d, Hidden: %d (CanSkip %d Cannot %d), SkipItems: %d", window->Appearing, window->Hidden, window->HiddenFramesCanSkipItems, window->HiddenFramesCannotSkipItems, window->SkipItems); ImGui::BulletText("NavLastIds: 0x%08X,0x%08X, NavLayerActiveMask: %X", window->NavLastIds[0], window->NavLastIds[1], window->DC.NavLayerActiveMask); ImGui::BulletText("NavLastChildNavWindow: %s", window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL"); if (!window->NavRectRel[0].IsInverted()) 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); else ImGui::BulletText("NavRectRel[0]: "); 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); ImGui::BulletText("ViewportMonitor: %d", window->Viewport ? window->Viewport->PlatformMonitor : -1); 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); if (window->RootWindow != window) NodeWindow(window->RootWindow, "RootWindow"); if (window->RootWindowDockStop != window->RootWindow) NodeWindow(window->RootWindowDockStop, "RootWindowDockStop"); if (window->ParentWindow != NULL) NodeWindow(window->ParentWindow, "ParentWindow"); if (window->DC.ChildWindows.Size > 0) NodeWindows(window->DC.ChildWindows, "ChildWindows"); if (window->ColumnsStorage.Size > 0 && ImGui::TreeNode("Columns", "Columns sets (%d)", window->ColumnsStorage.Size)) { for (int n = 0; n < window->ColumnsStorage.Size; n++) NodeColumns(&window->ColumnsStorage[n]); ImGui::TreePop(); } ImGui::BulletText("Storage: %d bytes", window->StateStorage.Data.Size * (int)sizeof(ImGuiStorage::Pair)); ImGui::TreePop(); } static void NodeViewport(ImGuiViewportP* viewport) { ImGui::SetNextTreeNodeOpen(true, ImGuiCond_Once); 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")) { ImGuiWindowFlags flags = viewport->Flags; 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); 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); } } ImGui::BulletText("Flags: 0x%04X =%s%s%s%s%s%s", viewport->Flags, (flags & ImGuiViewportFlags_CanHostOtherWindows) ? " CanHostOtherWindows" : "", (flags & ImGuiViewportFlags_NoDecoration) ? " NoDecoration" : "", (flags & ImGuiViewportFlags_NoFocusOnAppearing) ? " NoFocusOnAppearing" : "", (flags & ImGuiViewportFlags_NoInputs) ? " NoInputs" : "", (flags & ImGuiViewportFlags_NoRendererClear) ? " NoRendererClear" : "", (flags & ImGuiViewportFlags_Minimized) ? " Minimized" : ""); for (int layer_i = 0; layer_i < IM_ARRAYSIZE(viewport->DrawDataBuilder.Layers); layer_i++) for (int draw_list_i = 0; draw_list_i < viewport->DrawDataBuilder.Layers[layer_i].Size; draw_list_i++) Funcs::NodeDrawList(NULL, viewport, viewport->DrawDataBuilder.Layers[layer_i][draw_list_i], "DrawList"); ImGui::TreePop(); } } }; // Access private state, we are going to display the draw lists from last frame ImGuiContext& g = *GImGui; Funcs::NodeWindows(g.Windows, "Windows"); if (ImGui::TreeNode("Viewport", "Viewports (%d)", g.Viewports.Size)) { ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing()); ImGui::ShowViewportThumbnails(); ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing()); if (g.PlatformIO.Monitors.Size > 0 && ImGui::TreeNode("Monitors", "Monitors (%d)", g.PlatformIO.Monitors.Size)) { ImGui::TextWrapped("(When viewports are enabled, imgui needs uses monitor data to position popup/tooltips so they don't straddle monitors.)"); for (int i = 0; i < g.PlatformIO.Monitors.Size; i++) { const ImGuiPlatformMonitor& mon = g.PlatformIO.Monitors[i]; 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)", i, mon.DpiScale * 100.0f, mon.MainPos.x, mon.MainPos.y, mon.MainPos.x + mon.MainSize.x, mon.MainPos.y + mon.MainSize.y, mon.MainSize.x, mon.MainSize.y, mon.WorkPos.x, mon.WorkPos.y, mon.WorkPos.x + mon.WorkSize.x, mon.WorkPos.y + mon.WorkSize.y, mon.WorkSize.x, mon.WorkSize.y); } ImGui::TreePop(); } for (int i = 0; i < g.Viewports.Size; i++) Funcs::NodeViewport(g.Viewports[i]); ImGui::TreePop(); } if (ImGui::TreeNode("Popups", "Popups (%d)", g.OpenPopupStack.Size)) { for (int i = 0; i < g.OpenPopupStack.Size; i++) { ImGuiWindow* window = g.OpenPopupStack[i].Window; 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" : ""); } ImGui::TreePop(); } if (ImGui::TreeNode("Docking & Tab Bars")) { ShowDockingDebug(); ImGui::TreePop(); } if (ImGui::TreeNode("Internal state")) { const char* input_source_names[] = { "None", "Mouse", "Nav", "NavKeyboard", "NavGamepad" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT); ImGui::Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL"); ImGui::Text("HoveredRootWindow: '%s'", g.HoveredRootWindow ? g.HoveredRootWindow->Name : "NULL"); ImGui::Text("HoveredWindowUnderMovingWindow: '%s'", g.HoveredWindowUnderMovingWindow ? g.HoveredWindowUnderMovingWindow->Name : "NULL"); 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 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]); ImGui::Text("ActiveIdWindow: '%s'", g.ActiveIdWindow ? g.ActiveIdWindow->Name : "NULL"); ImGui::Text("MovingWindow: '%s'", g.MovingWindow ? g.MovingWindow->Name : "NULL"); ImGui::Text("NavWindow: '%s'", g.NavWindow ? g.NavWindow->Name : "NULL"); ImGui::Text("NavId: 0x%08X, NavLayer: %d", g.NavId, g.NavLayer); ImGui::Text("NavInputSource: %s", input_source_names[g.NavInputSource]); ImGui::Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); ImGui::Text("NavActivateId: 0x%08X, NavInputId: 0x%08X", g.NavActivateId, g.NavInputId); ImGui::Text("NavDisableHighlight: %d, NavDisableMouseHover: %d", g.NavDisableHighlight, g.NavDisableMouseHover); ImGui::Text("NavWindowingTarget: '%s'", g.NavWindowingTarget ? g.NavWindowingTarget->Name : "NULL"); ImGui::Text("DragDrop: %d, SourceId = 0x%08X, Payload \"%s\" (%d bytes)", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize); ImGui::Text("MouseViewport: 0x%08X (UserHovered 0x%08X, LastHovered 0x%08X)", g.MouseViewport->ID, g.IO.MouseHoveredViewport, g.MouseLastHoveredViewport ? g.MouseLastHoveredViewport->ID : 0); ImGui::TreePop(); } if (ImGui::TreeNode("Tools")) { ImGui::Checkbox("Show windows begin order", &show_windows_begin_order); ImGui::Checkbox("Show windows rectangles", &show_windows_rects); ImGui::SameLine(); ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12); show_windows_rects |= ImGui::Combo("##rects_type", &show_windows_rect_type, "OuterRect\0" "OuterRectClipped\0" "InnerMainRect\0" "InnerClipRect\0" "ContentsRegionRect\0"); ImGui::Checkbox("Show clipping rectangle when hovering ImDrawCmd node", &show_drawcmd_clip_rects); ImGui::TreePop(); } if (show_windows_rects || show_windows_begin_order) { for (int n = 0; n < g.Windows.Size; n++) { ImGuiWindow* window = g.Windows[n]; if (!window->WasActive) continue; ImDrawList* draw_list = GetForegroundDrawList(window); if (show_windows_rects) { ImRect r; if (show_windows_rect_type == RT_OuterRect) { r = window->Rect(); } else if (show_windows_rect_type == RT_OuterRectClipped) { r = window->OuterRectClipped; } else if (show_windows_rect_type == RT_InnerMainRect) { r = window->InnerMainRect; } else if (show_windows_rect_type == RT_InnerClipRect) { r = window->InnerClipRect; } else if (show_windows_rect_type == RT_ContentsRegionRect) { r = window->ContentsRegionRect; } draw_list->AddRect(r.Min, r.Max, IM_COL32(255, 0, 128, 255)); } if (show_windows_begin_order && !(window->Flags & ImGuiWindowFlags_ChildWindow)) { char buf[32]; ImFormatString(buf, IM_ARRAYSIZE(buf), "%d", window->BeginOrderWithinContext); float font_size = ImGui::GetFontSize(); draw_list->AddRectFilled(window->Pos, window->Pos + ImVec2(font_size, font_size), IM_COL32(200, 100, 100, 255)); draw_list->AddText(window->Pos, IM_COL32(255, 255, 255, 255), buf); } } } ImGui::End(); } void ImGui::ShowDockingDebug() { ImGuiContext* ctx = GImGui; ImGuiContext& g = *ctx; ImGuiDockContext* dc = ctx->DockContext; struct Funcs { static void NodeDockNode(ImGuiDockNode* node, const char* label) { ImGuiContext& g = *GImGui; ImGui::SetNextTreeNodeOpen(true, ImGuiCond_Once); bool open; if (node->Windows.Size > 0) 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"); else 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"); if (open) { IM_ASSERT(node->ChildNodes[0] == NULL || node->ChildNodes[0]->ParentNode == node); IM_ASSERT(node->ChildNodes[1] == NULL || node->ChildNodes[1]->ParentNode == node); ImGui::BulletText("Pos (%.0f,%.0f), Size (%.0f, %.0f) Ref (%.0f, %.0f)", node->Pos.x, node->Pos.y, node->Size.x, node->Size.y, node->SizeRef.x, node->SizeRef.y); ImGui::BulletText("VisibleWindow: 0x%08X %s", node->VisibleWindow ? node->VisibleWindow->ID : 0, node->VisibleWindow ? node->VisibleWindow->Name : "NULL"); ImGui::BulletText("SelectedTabID: 0x%08X, LastFocusedNodeID: 0x%08X", node->SelectedTabID, node->LastFocusedNodeID); 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" : ""); if (ImGui::TreeNode("flags", "LocalFlags: 0x%04X SharedFlags: 0x%04X", node->LocalFlags, node->SharedFlags)) { ImGui::CheckboxFlags("LocalFlags: NoSplit", (unsigned int*)&node->LocalFlags, ImGuiDockNodeFlags_NoSplit); ImGui::CheckboxFlags("LocalFlags: NoResize", (unsigned int*)&node->LocalFlags, ImGuiDockNodeFlags_NoResize); ImGui::CheckboxFlags("LocalFlags: NoTabBar", (unsigned int*)&node->LocalFlags, ImGuiDockNodeFlags_NoTabBar); ImGui::CheckboxFlags("LocalFlags: HiddenTabBar",(unsigned int*)&node->LocalFlags, ImGuiDockNodeFlags_HiddenTabBar); ImGui::TreePop(); } if (node->ChildNodes[0]) NodeDockNode(node->ChildNodes[0], "Child[0]"); if (node->ChildNodes[1]) NodeDockNode(node->ChildNodes[1], "Child[1]"); if (node->TabBar) NodeTabBar(node->TabBar); ImGui::TreePop(); } } static void NodeTabBar(ImGuiTabBar* tab_bar) { // Standalone tab bars (not associated to docking/windows functionality) currently hold no discernible strings. char buf[256]; char* p = buf; const char* buf_end = buf + IM_ARRAYSIZE(buf); p += ImFormatString(p, buf_end - p, "TabBar (%d tabs)%s", tab_bar->Tabs.Size, (tab_bar->PrevFrameVisible < ImGui::GetFrameCount() - 2) ? " *Inactive*" : ""); if (tab_bar->Flags & ImGuiTabBarFlags_DockNode) { p += ImFormatString(p, buf_end - p, " { "); for (int tab_n = 0; tab_n < ImMin(tab_bar->Tabs.Size, 3); tab_n++) p += ImFormatString(p, buf_end - p, "%s'%s'", tab_n > 0 ? ", " : "", tab_bar->Tabs[tab_n].Window->Name); p += ImFormatString(p, buf_end - p, (tab_bar->Tabs.Size > 3) ? " ... }" : " } "); } if (ImGui::TreeNode(tab_bar, "%s", buf)) { for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { const ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; ImGui::PushID(tab); if (ImGui::SmallButton("<")) { TabBarQueueChangeTabOrder(tab_bar, tab, -1); } ImGui::SameLine(0, 2); if (ImGui::SmallButton(">")) { TabBarQueueChangeTabOrder(tab_bar, tab, +1); } ImGui::SameLine(); ImGui::Text("%02d%c Tab 0x%08X '%s'", tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, tab->Window ? tab->Window->Name : "N/A"); ImGui::PopID(); } ImGui::TreePop(); } } }; static bool show_window_dock_info = false; ImGui::Checkbox("Ctrl shows window dock info", &show_window_dock_info); if (ImGui::TreeNode("Dock nodes")) { if (ImGui::SmallButton("Clear settings")) { DockContextClearNodes(&g, 0, true); } ImGui::SameLine(); if (ImGui::SmallButton("Rebuild all")) { dc->WantFullRebuild = true; } for (int n = 0; n < dc->Nodes.Data.Size; n++) if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p) if (node->IsRootNode()) Funcs::NodeDockNode(node, "Node"); ImGui::TreePop(); } if (ImGui::TreeNode("TabBars", "Tab Bars (%d)", g.TabBars.Data.Size)) { for (int n = 0; n < g.TabBars.Data.Size; n++) Funcs::NodeTabBar(g.TabBars.GetByIndex(n)); ImGui::TreePop(); } if (ImGui::TreeNode("Settings")) { if (ImGui::SmallButton("Refresh")) SaveIniSettingsToMemory(); ImGui::SameLine(); if (ImGui::SmallButton("Save to disk")) SaveIniSettingsToDisk(g.IO.IniFilename); ImGui::Separator(); ImGui::Text("Docked Windows:"); for (int n = 0; n < g.SettingsWindows.Size; n++) if (g.SettingsWindows[n].DockId != 0) ImGui::BulletText("Window '%s' -> DockId %08X", g.SettingsWindows[n].Name, g.SettingsWindows[n].DockId); ImGui::Separator(); ImGui::Text("Dock Nodes:"); for (int n = 0; n < dc->SettingsNodes.Size; n++) { ImGuiDockNodeSettings* settings = &dc->SettingsNodes[n]; const char* selected_tab_name = NULL; if (settings->SelectedTabID) { if (ImGuiWindow* window = FindWindowByID(settings->SelectedTabID)) selected_tab_name = window->Name; else if (ImGuiWindowSettings* window_settings = FindWindowSettings(settings->SelectedTabID)) selected_tab_name = window_settings->Name; } 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" : ""); } ImGui::TreePop(); } if (g.IO.KeyCtrl && show_window_dock_info) { for (int n = 0; n < dc->Nodes.Data.Size; n++) if (ImGuiDockNode* node = (ImGuiDockNode*)dc->Nodes.Data[n].val_p) { ImGuiDockNode* root_node = DockNodeGetRootNode(node); if (ImGuiDockNode* hovered_node = DockNodeTreeFindNodeByPos(root_node, g.IO.MousePos)) if (hovered_node != node) continue; char buf[64] = ""; char* p = buf; ImDrawList* overlay_draw_list = node->HostWindow ? GetForegroundDrawList(node->HostWindow) : GetForegroundDrawList((ImGuiViewportP*)GetMainViewport()); p += ImFormatString(p, buf + IM_ARRAYSIZE(buf) - p, "DockId: %X%s\n", node->ID, node->IsCentralNode() ? " *CentralNode*" : ""); p += ImFormatString(p, buf + IM_ARRAYSIZE(buf) - p, "Size: (%.0f, %.0f)\n", node->Size.x, node->Size.y); p += ImFormatString(p, buf + IM_ARRAYSIZE(buf) - p, "SizeRef: (%.0f, %.0f)\n", node->SizeRef.x, node->SizeRef.y); int depth = DockNodeGetDepth(node); 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)); ImVec2 pos = node->Pos + ImVec2(3,3) * (float)depth; overlay_draw_list->AddRectFilled(pos - ImVec2(1, 1), pos + CalcTextSize(buf) + ImVec2(1, 1), IM_COL32(200, 100, 100, 255)); overlay_draw_list->AddText(NULL, 0.0f, pos, IM_COL32(255, 255, 255, 255), buf); } } } //----------------------------------------------------------------------------- // Include imgui_user.inl at the end of imgui.cpp to access private data/functions that aren't exposed. // 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. #ifdef IMGUI_INCLUDE_IMGUI_USER_INL #include "imgui_user.inl" #endif //----------------------------------------------------------------------------- ================================================ FILE: src/imgui/imgui.hpp ================================================ // dear imgui, v1.70 WIP // (headers) // See imgui.cpp file for documentation. // Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. // Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. // Get latest version at https://github.com/ocornut/imgui /* Index of this file: // Header mess // Forward declarations and basic types // ImGui API (Dear ImGui end-user API) // Flags & Enumerations // Memory allocations macros // ImVector<> // ImGuiStyle // ImGuiIO // Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload, ImGuiWindowClass) // Obsolete functions // Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor) // Draw List API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListFlags, ImDrawList, ImDrawData) // Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) // Platform interface for multi-viewport support (ImGuiPlatformMonitor, ImGuiPlatformIO, ImGuiViewport) */ #pragma once #pragma warning(push, 0) // Configuration file with compile-time options (edit imconfig.h or define IMGUI_USER_CONFIG to your own filename) #ifdef IMGUI_USER_CONFIG #include IMGUI_USER_CONFIG #endif #if !defined(IMGUI_DISABLE_INCLUDE_IMCONFIG_H) || defined(IMGUI_INCLUDE_IMCONFIG_H) #include "imconfig.hpp" #endif //----------------------------------------------------------------------------- // Header mess //----------------------------------------------------------------------------- #include // FLT_MAX #include // va_list #include // ptrdiff_t, NULL #include // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp // Version // (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) #define IMGUI_VERSION "1.70 WIP" #define IMGUI_VERSION_NUM 16991 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert)) #define IMGUI_HAS_VIEWPORT 1 // Viewport WIP branch #define IMGUI_HAS_DOCK 1 // Docking WIP branch // Define attributes of all API symbols declarations (e.g. for DLL under Windows) // IMGUI_API is used for core imgui functions, IMGUI_IMPL_API is used for the default bindings files (imgui_impl_xxx.h) #ifndef IMGUI_API #define IMGUI_API #endif #ifndef IMGUI_IMPL_API #define IMGUI_IMPL_API IMGUI_API #endif // Helper Macros #ifndef IM_ASSERT #include #define IM_ASSERT(_EXPR) assert(_EXPR) // You can override the default assert handler by editing imconfig.h #endif #if defined(__clang__) || defined(__GNUC__) #define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) // Apply printf-style warnings to user functions. #define IM_FMTLIST(FMT) __attribute__((format(printf, FMT, 0))) #else #define IM_FMTARGS(FMT) #define IM_FMTLIST(FMT) #endif #define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR))) // Size of a static C-style array. Don't use on pointers! #define IM_OFFSETOF(_TYPE,_MEMBER) ((size_t)&(((_TYPE*)0)->_MEMBER)) // Offset of _MEMBER within _TYPE. Standardized as offsetof() in modern C++. #define IM_UNUSED(_VAR) ((void)_VAR) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. // Warnings #if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wold-style-cast" #if __has_warning("-Wzero-as-null-pointer-constant") #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" #endif #elif defined(__GNUC__) && __GNUC__ >= 8 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wclass-memaccess" #endif //----------------------------------------------------------------------------- // Forward declarations and basic types //----------------------------------------------------------------------------- struct ImDrawChannel; // Temporary storage for ImDrawList ot output draw commands out of order, used by ImDrawList::ChannelsSplit() struct ImDrawCmd; // A single draw command within a parent ImDrawList (generally maps to 1 GPU draw call, unless it is a callback) struct ImDrawData; // All draw command lists required to render the frame + pos/size coordinates to use for the projection matrix. struct ImDrawList; // A single draw command list (generally one per window, conceptually you may see this as a dynamic "mesh" builder) struct ImDrawListSharedData; // Data shared among multiple draw lists (typically owned by parent ImGui context, but you may create one yourself) struct ImDrawVert; // A single vertex (pos + uv + col = 20 bytes by default. Override layout with IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT) struct ImFont; // Runtime data for a single font within a parent ImFontAtlas struct ImFontAtlas; // Runtime data for multiple fonts, bake multiple fonts into a single texture, TTF/OTF font loader struct ImFontConfig; // Configuration data when adding a font or merging fonts struct ImFontGlyph; // A single font glyph (code point + coordinates within in ImFontAtlas + offset) struct ImFontGlyphRangesBuilder; // Helper to build glyph ranges from text/string data struct ImColor; // Helper functions to create a color that can be converted to either u32 or float4 (*OBSOLETE* please avoid using) struct ImGuiContext; // Dear ImGui context (opaque structure, unless including imgui_internal.h) struct ImGuiIO; // Main configuration and I/O between your application and ImGui struct ImGuiInputTextCallbackData; // Shared state of InputText() when using custom ImGuiInputTextCallback (rare/advanced use) struct ImGuiListClipper; // Helper to manually clip large list of items struct ImGuiOnceUponAFrame; // Helper for running a block of code not more than once a frame, used by IMGUI_ONCE_UPON_A_FRAME macro struct ImGuiPayload; // User data payload for drag and drop operations struct ImGuiPlatformIO; // Multi-viewport support: interface for Platform/Renderer back-ends + viewports to render struct ImGuiPlatformMonitor; // Multi-viewport support: user-provided bounds for each connected monitor/display. Used when positioning popups and tooltips to avoid them straddling monitors struct ImGuiSizeCallbackData; // Callback data when using SetNextWindowSizeConstraints() (rare/advanced use) struct ImGuiStorage; // Helper for key->value storage struct ImGuiStyle; // Runtime data for styling/colors struct ImGuiTextBuffer; // Helper to hold and append into a text buffer (~string builder) struct ImGuiTextFilter; // Helper to parse and apply text filters (e.g. "aaaaa[,bbbb][,ccccc]") struct ImGuiViewport; // Viewport (generally ~1 per window to output to at the OS level. Need per-platform support to use multiple viewports) struct ImGuiWindowClass; // Window class (rare/advanced uses: provide hints to the platform back-end via altered viewport flags and parent/child info) // 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) // Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. #ifndef ImTextureID typedef 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) #endif typedef unsigned int ImGuiID; // Unique ID used by widgets (typically hashed from a stack of string) typedef unsigned short ImWchar; // A single U16 character for keyboard input/display. We encode them as multi bytes UTF-8 when used in strings. typedef int ImGuiCol; // -> enum ImGuiCol_ // Enum: A color identifier for styling typedef int ImGuiCond; // -> enum ImGuiCond_ // Enum: A condition for Set*() typedef int ImGuiDataType; // -> enum ImGuiDataType_ // Enum: A primary data type typedef int ImGuiDir; // -> enum ImGuiDir_ // Enum: A cardinal direction typedef int ImGuiKey; // -> enum ImGuiKey_ // Enum: A key identifier (ImGui-side enum) typedef int ImGuiNavInput; // -> enum ImGuiNavInput_ // Enum: An input identifier for navigation typedef int ImGuiMouseCursor; // -> enum ImGuiMouseCursor_ // Enum: A mouse cursor identifier typedef int ImGuiStyleVar; // -> enum ImGuiStyleVar_ // Enum: A variable identifier for styling typedef int ImDrawCornerFlags; // -> enum ImDrawCornerFlags_ // Flags: for ImDrawList::AddRect*() etc. typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: for ImDrawList typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas typedef int ImGuiBackendFlags; // -> enum ImGuiBackendFlags_ // Flags: for io.BackendFlags typedef int ImGuiColorEditFlags; // -> enum ImGuiColorEditFlags_ // Flags: for ColorEdit*(), ColorPicker*() typedef int ImGuiColumnsFlags; // -> enum ImGuiColumnsFlags_ // Flags: for Columns(), BeginColumns() typedef int ImGuiConfigFlags; // -> enum ImGuiConfigFlags_ // Flags: for io.ConfigFlags typedef int ImGuiComboFlags; // -> enum ImGuiComboFlags_ // Flags: for BeginCombo() typedef int ImGuiDockNodeFlags; // -> enum ImGuiDockNodeFlags_ // Flags: for DockSpace() typedef int ImGuiDragDropFlags; // -> enum ImGuiDragDropFlags_ // Flags: for *DragDrop*() typedef int ImGuiFocusedFlags; // -> enum ImGuiFocusedFlags_ // Flags: for IsWindowFocused() typedef int ImGuiHoveredFlags; // -> enum ImGuiHoveredFlags_ // Flags: for IsItemHovered(), IsWindowHovered() etc. typedef int ImGuiInputTextFlags; // -> enum ImGuiInputTextFlags_ // Flags: for InputText*() typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: for Selectable() typedef int ImGuiTabBarFlags; // -> enum ImGuiTabBarFlags_ // Flags: for BeginTabBar() typedef int ImGuiTabItemFlags; // -> enum ImGuiTabItemFlags_ // Flags: for BeginTabItem() typedef int ImGuiTreeNodeFlags; // -> enum ImGuiTreeNodeFlags_ // Flags: for TreeNode*(),CollapsingHeader() typedef int ImGuiViewportFlags; // -> enum ImGuiViewportFlags_ // Flags: for ImGuiViewport typedef int ImGuiWindowFlags; // -> enum ImGuiWindowFlags_ // Flags: for Begin*() typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData *data); typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); // Scalar data types typedef signed char ImS8; // 8-bit signed integer == char typedef unsigned char ImU8; // 8-bit unsigned integer typedef signed short ImS16; // 16-bit signed integer typedef unsigned short ImU16; // 16-bit unsigned integer typedef signed int ImS32; // 32-bit signed integer == int typedef unsigned int ImU32; // 32-bit unsigned integer (often used to store packed colors) #if defined(_MSC_VER) && !defined(__clang__) typedef signed __int64 ImS64; // 64-bit signed integer (pre and post C++11 with Visual Studio) typedef unsigned __int64 ImU64; // 64-bit unsigned integer (pre and post C++11 with Visual Studio) #elif (defined(__clang__) || defined(__GNUC__)) && (__cplusplus < 201100) #include typedef int64_t ImS64; // 64-bit signed integer (pre C++11) typedef uint64_t ImU64; // 64-bit unsigned integer (pre C++11) #else typedef signed long long ImS64; // 64-bit signed integer (post C++11) typedef unsigned long long ImU64; // 64-bit unsigned integer (post C++11) #endif // 2D vector (often used to store positions, sizes, etc.) struct ImVec2 { float x, y; ImVec2() { x = y = 0.0f; } ImVec2(float _x, float _y) { x = _x; y = _y; } float operator[] (size_t idx) const { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. float& operator[] (size_t idx) { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. #ifdef IM_VEC2_CLASS_EXTRA 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. #endif }; // 4D vector (often used to store floating-point colors) struct ImVec4 { float x, y, z, w; ImVec4() { x = y = z = w = 0.0f; } ImVec4(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; } #ifdef IM_VEC4_CLASS_EXTRA 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. #endif }; //----------------------------------------------------------------------------- // ImGui: Dear ImGui end-user API // (Inside a namespace so you can add extra functions in your own separate file. Please don't modify imgui.cpp/.h!) //----------------------------------------------------------------------------- namespace ImGui { // Context creation and access // 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. // All those functions are not reliant on the current context. IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = destroy current context IMGUI_API ImGuiContext* GetCurrentContext(); IMGUI_API void SetCurrentContext(ImGuiContext* ctx); 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); // Main IMGUI_API ImGuiIO& GetIO(); // access the IO structure (mouse/keyboard/gamepad inputs, time, various configuration options/flags) IMGUI_API ImGuiStyle& GetStyle(); // access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame. IMGUI_API void NewFrame(); // start a new Dear ImGui frame, you can submit any command from this point until Render()/EndFrame(). 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! 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.) IMGUI_API ImDrawData* GetDrawData(); // valid after Render() and until the next call to NewFrame(). this is what you have to render. // Demo, Debug, Information 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! IMGUI_API void ShowAboutWindow(bool* p_open = NULL); // create about window. display Dear ImGui version, credits and build/system information. 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. 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) IMGUI_API bool ShowStyleSelector(const char* label); // add style selector block (not a window), essentially a combo listing the default styles. IMGUI_API void ShowFontSelector(const char* label); // add font selector block (not a window), essentially a combo listing the loaded fonts. IMGUI_API void ShowUserGuide(); // add basic help/info block (not a window): how to manipulate ImGui as a end-user (mouse/keyboard controls). IMGUI_API const char* GetVersion(); // get the compiled version string e.g. "1.23" (essentially the compiled value for IMGUI_VERSION) // Styles IMGUI_API void StyleColorsDark(ImGuiStyle* dst = NULL); // new, recommended style (default) IMGUI_API void StyleColorsClassic(ImGuiStyle* dst = NULL); // classic imgui style IMGUI_API void StyleColorsDarkCodz1(ImGuiStyle* dst = NULL); // https://github.com/codz01 IMGUI_API void StyleColorsCherry(ImGuiStyle* dst = NULL); // https://github.com/ocornut/imgui/issues/707 IMGUI_API void StyleColorsLightGreen(ImGuiStyle* dst = NULL); // https://github.com/ocornut/imgui/pull/1776 IMGUI_API void StyleColorsUE(ImGuiStyle* dst = NULL); // https://github.com/ocornut/imgui/issues/707 IMGUI_API void StyleCorporateGrey(ImGuiStyle* dst = NULL); // https://github.com/ocornut/imgui/issues/707 IMGUI_API void StyleColorsLight(ImGuiStyle* dst = NULL); // best used with borders and a custom, thicker font // Windows // - Begin() = push window to the stack and start appending to it. End() = pop window from the stack. // - You may append multiple times to the same window during the same frame. // - Passing 'bool* p_open != NULL' shows a window-closing widget in the upper-right corner of the window, // which clicking will set the boolean to false when clicked. // - Begin() return false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting // anything to the window. Always call a matching End() for each Begin() 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.] // - Note that the bottom of window stack always contains a window called "Debug". IMGUI_API bool Begin(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); IMGUI_API void End(); // Child Windows // - Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window. Child windows can embed their own child. // - 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). // - BeginChild() returns false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting anything to the window. // 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.] IMGUI_API bool BeginChild(const char* str_id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0); IMGUI_API bool BeginChild(ImGuiID id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0); IMGUI_API void EndChild(); // Windows Utilities // - "current window" = the window we are appending into while inside a Begin()/End() block. "next window" = next window we will Begin() into. IMGUI_API bool IsWindowAppearing(); IMGUI_API bool IsWindowCollapsed(); IMGUI_API bool IsWindowFocused(ImGuiFocusedFlags flags=0); // is current window focused? or its root/child, depending on flags. see flags for options. 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! IMGUI_API ImDrawList* GetWindowDrawList(); // get draw list associated to the current window, to append your own drawing primitives IMGUI_API float GetWindowDpiScale(); // get DPI scale currently associated to the current window's viewport. IMGUI_API ImGuiViewport*GetWindowViewport(); // get viewport currently associated to the current window. IMGUI_API ImVec2 GetWindowPos(); // get current window position in screen space (useful if you want to do your own drawing via the DrawList API) IMGUI_API ImVec2 GetWindowSize(); // get current window size IMGUI_API float GetWindowWidth(); // get current window width (shortcut for GetWindowSize().x) IMGUI_API float GetWindowHeight(); // get current window height (shortcut for GetWindowSize().y) // Prefer using SetNextXXX functions (before Begin) rather that SetXXX functions (after Begin). 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. 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() 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. 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() IMGUI_API void SetNextWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // set next window collapsed state. call before Begin() IMGUI_API void SetNextWindowFocus(); // set next window to be focused / front-most. call before Begin() 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. IMGUI_API void SetNextWindowViewport(ImGuiID viewport_id); // set next window viewport 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. 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. IMGUI_API void SetWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // (not recommended) set current window collapsed state. prefer using SetNextWindowCollapsed(). IMGUI_API void SetWindowFocus(); // (not recommended) set current window to be focused / front-most. prefer using SetNextWindowFocus(). IMGUI_API void SetWindowFontScale(float scale); // set font scale. Adjust IO.FontGlobalScale if you want to scale all windows IMGUI_API void SetWindowPos(const char* name, const ImVec2& pos, ImGuiCond cond = 0); // set named window position. 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. IMGUI_API void SetWindowCollapsed(const char* name, bool collapsed, ImGuiCond cond = 0); // set named window collapsed state IMGUI_API void SetWindowFocus(const char* name); // set named window to be focused / front-most. use NULL to remove focus. // Content region // - Those functions are bound to be redesigned soon (they are confusing, incomplete and return values in local window coordinates which increases confusion) IMGUI_API ImVec2 GetContentRegionMax(); // current content boundaries (typically window boundaries including scrolling, or current column boundaries), in windows coordinates IMGUI_API ImVec2 GetContentRegionAvail(); // == GetContentRegionMax() - GetCursorPos() IMGUI_API float GetContentRegionAvailWidth(); // == GetContentRegionAvail().x IMGUI_API ImVec2 GetWindowContentRegionMin(); // content boundaries min (roughly (0,0)-Scroll), in window coordinates IMGUI_API ImVec2 GetWindowContentRegionMax(); // content boundaries max (roughly (0,0)+Size-Scroll) where Size can be override with SetNextWindowContentSize(), in window coordinates IMGUI_API float GetWindowContentRegionWidth(); // // Windows Scrolling IMGUI_API float GetScrollX(); // get scrolling amount [0..GetScrollMaxX()] IMGUI_API float GetScrollY(); // get scrolling amount [0..GetScrollMaxY()] IMGUI_API float GetScrollMaxX(); // get maximum scrolling amount ~~ ContentSize.X - WindowSize.X IMGUI_API float GetScrollMaxY(); // get maximum scrolling amount ~~ ContentSize.Y - WindowSize.Y IMGUI_API void SetScrollX(float scroll_x); // set scrolling amount [0..GetScrollMaxX()] IMGUI_API void SetScrollY(float scroll_y); // set scrolling amount [0..GetScrollMaxY()] 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. 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. // Parameters stacks (shared) IMGUI_API void PushFont(ImFont* font); // use NULL as a shortcut to push default font IMGUI_API void PopFont(); IMGUI_API void PushStyleColor(ImGuiCol idx, ImU32 col); IMGUI_API void PushStyleColor(ImGuiCol idx, const ImVec4& col); IMGUI_API void PopStyleColor(int count = 1); IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); IMGUI_API void PopStyleVar(int count = 1); 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. IMGUI_API ImFont* GetFont(); // get current font IMGUI_API float GetFontSize(); // get current font size (= height in pixels) of current font with current scale applied IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API IMGUI_API ImU32 GetColorU32(ImGuiCol idx, float alpha_mul = 1.0f); // retrieve given style color with style alpha applied and optional extra alpha multiplier IMGUI_API ImU32 GetColorU32(const ImVec4& col); // retrieve given color with style alpha applied IMGUI_API ImU32 GetColorU32(ImU32 col); // retrieve given color with style alpha applied // Parameters stacks (current window) 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, IMGUI_API void PopItemWidth(); 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) IMGUI_API float CalcItemWidth(); // width of item given pushed settings and current cursor position 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 IMGUI_API void PopTextWrapPos(); 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 IMGUI_API void PopAllowKeyboardFocus(); 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. IMGUI_API void PopButtonRepeat(); // Cursor / Layout // - By "cursor" we mean the current output position. // - The typical widget behavior is to output themselves at the current cursor position, then move the cursor one line down. IMGUI_API void Separator(); // separator, generally horizontal. inside a menu bar or in horizontal layout mode, this becomes a vertical separator. 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. IMGUI_API void NewLine(); // undo a SameLine() or force a new line when in an horizontal-layout context. IMGUI_API void Spacing(); // add vertical spacing. 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. IMGUI_API void Indent(float indent_w = 0.0f); // move content position toward the right, by style.IndentSpacing or indent_w if != 0 IMGUI_API void Unindent(float indent_w = 0.0f); // move content position back to the left, by style.IndentSpacing or indent_w if != 0 IMGUI_API void BeginGroup(); // lock horizontal starting position 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.) IMGUI_API ImVec2 GetCursorPos(); // cursor position in window coordinates (relative to window position) IMGUI_API float GetCursorPosX(); // (some functions are using window-relative coordinates, such as: GetCursorPos, GetCursorStartPos, GetContentRegionMax, GetWindowContentRegion* etc. IMGUI_API float GetCursorPosY(); // other functions such as GetCursorScreenPos or everything in ImDrawList:: IMGUI_API void SetCursorPos(const ImVec2& local_pos); // are using the main, absolute coordinate system. IMGUI_API void SetCursorPosX(float local_x); // GetWindowPos() + GetCursorPos() == GetCursorScreenPos() etc.) IMGUI_API void SetCursorPosY(float local_y); // IMGUI_API ImVec2 GetCursorStartPos(); // initial cursor position in window coordinates 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. IMGUI_API void SetCursorScreenPos(const ImVec2& pos); // cursor position in absolute screen coordinates (0..io.DisplaySize) or natural OS coordinates when using multiple viewport. 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) IMGUI_API float GetTextLineHeight(); // ~ FontSize IMGUI_API float GetTextLineHeightWithSpacing(); // ~ FontSize + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of text) IMGUI_API float GetFrameHeight(); // ~ FontSize + style.FramePadding.y * 2 IMGUI_API float GetFrameHeightWithSpacing(); // ~ FontSize + style.FramePadding.y * 2 + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of framed widgets) // ID stack/scopes // - Read the FAQ for more details about how ID are handled in dear imgui. If you are creating widgets in a loop you most // likely want to push a unique identifier (e.g. object pointer, loop index) to uniquely differentiate them. // - The resulting ID are hashes of the entire stack. // - You can also use the "Label##foobar" syntax within widget label to distinguish them from each others. // - In this header file we use the "label"/"name" terminology to denote a string that will be displayed and used as an ID, // whereas "str_id" denote a string that is only used as an ID and not normally displayed. IMGUI_API void PushID(const char* str_id); // push string into the ID stack (will hash string). IMGUI_API void PushID(const char* str_id_begin, const char* str_id_end); // push string into the ID stack (will hash string). IMGUI_API void PushID(const void* ptr_id); // push pointer into the ID stack (will hash pointer). IMGUI_API void PushID(int int_id); // push integer into the ID stack (will hash integer). IMGUI_API void PopID(); // pop from the ID stack. 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 IMGUI_API ImGuiID GetID(const char* str_id_begin, const char* str_id_end); IMGUI_API ImGuiID GetID(const void* ptr_id); // Widgets: Text 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. IMGUI_API void Text(const char* fmt, ...) IM_FMTARGS(1); // simple formatted text IMGUI_API void TextV(const char* fmt, va_list args) IM_FMTLIST(1); IMGUI_API void TextColored(const ImVec4& col, const char* fmt, ...) IM_FMTARGS(2); // shortcut for PushStyleColor(ImGuiCol_Text, col); Text(fmt, ...); PopStyleColor(); IMGUI_API void TextColoredV(const ImVec4& col, const char* fmt, va_list args) IM_FMTLIST(2); IMGUI_API void TextDisabled(const char* fmt, ...) IM_FMTARGS(1); // shortcut for PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); Text(fmt, ...); PopStyleColor(); IMGUI_API void TextDisabledV(const char* fmt, va_list args) IM_FMTLIST(1); 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(). IMGUI_API void TextWrappedV(const char* fmt, va_list args) IM_FMTLIST(1); IMGUI_API void LabelText(const char* label, const char* fmt, ...) IM_FMTARGS(2); // display text+label aligned the same way as value+label widgets IMGUI_API void LabelTextV(const char* label, const char* fmt, va_list args) IM_FMTLIST(2); IMGUI_API void BulletText(const char* fmt, ...) IM_FMTARGS(1); // shortcut for Bullet()+Text() IMGUI_API void BulletTextV(const char* fmt, va_list args) IM_FMTLIST(1); // Widgets: Main // - Most widgets return true when the value has been changed or when pressed/selected IMGUI_API bool Button(const char* label, const ImVec2& size = ImVec2(0,0)); // button IMGUI_API bool SmallButton(const char* label); // button with FramePadding=(0,0) to easily embed within text 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.) IMGUI_API bool ArrowButton(const char* str_id, ImGuiDir dir); // square button with an arrow shape 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)); 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 IMGUI_API bool Checkbox(const char* label, bool* v); IMGUI_API bool CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value); IMGUI_API bool RadioButton(const char* label, bool active); // use with e.g. if (RadioButton("one", my_value==1)) { my_value = 1; } IMGUI_API bool RadioButton(const char* label, int* v, int v_button); // shortcut to handle the above pattern when value is an integer IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-1,0), const char* overlay = NULL); 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 // Widgets: Combo Box // - The new BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items. // - The old Combo() api are helpers over BeginCombo()/EndCombo() which are kept available for convenience purpose. IMGUI_API bool BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags = 0); IMGUI_API void EndCombo(); // only call EndCombo() if BeginCombo() returns true! IMGUI_API bool Combo(const char* label, int* current_item, const char* const items[], int items_count, int popup_max_height_in_items = -1); 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" 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); // Widgets: Drags // - CTRL+Click on any drag box to turn them into an input box. Manually input values aren't clamped and can go off-bounds. // - 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 // - 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. // - 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). 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 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); 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); 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); 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); 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 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"); 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"); 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"); 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); 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); 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); // Widgets: Sliders // - CTRL+Click on any slider to turn them into an input box. Manually input values aren't clamped and can go off-bounds. // - 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. 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 IMGUI_API bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); IMGUI_API bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); IMGUI_API bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); 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"); IMGUI_API bool SliderInt(const char* label, int* v, int v_min, int v_max, const char* format = "%d"); IMGUI_API bool SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format = "%d"); IMGUI_API bool SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format = "%d"); IMGUI_API bool SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format = "%d"); 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); 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); 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); IMGUI_API bool VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format = "%d"); 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); // Widgets: Input with Keyboard // - If you want to use InputText() with a dynamic string type such as std::string or your own, see misc/cpp/imgui_stdlib.h // - Most of the ImGuiInputTextFlags flags are only useful for InputText() and not for InputFloatX, InputIntX, InputDouble etc. IMGUI_API bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); 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); 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); 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); IMGUI_API bool InputFloat2(const char* label, float v[2], const char* format = "%.3f", ImGuiInputTextFlags flags = 0); IMGUI_API bool InputFloat3(const char* label, float v[3], const char* format = "%.3f", ImGuiInputTextFlags flags = 0); IMGUI_API bool InputFloat4(const char* label, float v[4], const char* format = "%.3f", ImGuiInputTextFlags flags = 0); IMGUI_API bool InputInt(const char* label, int* v, int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0); IMGUI_API bool InputInt2(const char* label, int v[2], ImGuiInputTextFlags flags = 0); IMGUI_API bool InputInt3(const char* label, int v[3], ImGuiInputTextFlags flags = 0); IMGUI_API bool InputInt4(const char* label, int v[4], ImGuiInputTextFlags flags = 0); 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); 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); 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); // 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.) // - 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. // - You can pass the address of a first float element out of a contiguous structure, e.g. &myvector.x IMGUI_API bool ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags flags = 0, const float* ref_col = NULL); 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. 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. // Widgets: Trees // - 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. IMGUI_API bool TreeNode(const char* label); 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(). IMGUI_API bool TreeNode(const void* ptr_id, const char* fmt, ...) IM_FMTARGS(2); // " IMGUI_API bool TreeNodeV(const char* str_id, const char* fmt, va_list args) IM_FMTLIST(2); IMGUI_API bool TreeNodeV(const void* ptr_id, const char* fmt, va_list args) IM_FMTLIST(2); IMGUI_API bool TreeNodeEx(const char* label, ImGuiTreeNodeFlags flags = 0); IMGUI_API bool TreeNodeEx(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) IM_FMTARGS(3); IMGUI_API bool TreeNodeEx(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) IM_FMTARGS(3); IMGUI_API bool TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3); IMGUI_API bool TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3); 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. IMGUI_API void TreePush(const void* ptr_id = NULL); // " IMGUI_API void TreePop(); // ~ Unindent()+PopId() IMGUI_API void TreeAdvanceToLabelPos(); // advance cursor x position by GetTreeNodeToLabelSpacing() IMGUI_API float GetTreeNodeToLabelSpacing(); // horizontal distance preceding label when using TreeNode*() or Bullet() == (g.FontSize + style.FramePadding.x*2) for a regular unframed TreeNode IMGUI_API void SetNextTreeNodeOpen(bool is_open, ImGuiCond cond = 0); // set next TreeNode/CollapsingHeader open state. 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(). 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 // Widgets: Selectables // - A selectable highlights when hovered, and can display another color when selected. // - Neighbors selectable extend their highlight bounds in order to leave no gap between them. 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 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. // Widgets: List Boxes // - FIXME: To be consistent with all the newer API, ListBoxHeader/ListBoxFooter should in reality be called BeginListBox/EndListBox. Will rename them. IMGUI_API bool ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items = -1); 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); 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. IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // " IMGUI_API void ListBoxFooter(); // terminate the scrolling region. only call ListBoxFooter() if ListBoxHeader() returned true! // Widgets: Data Plotting 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)); 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)); 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)); 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)); // Widgets: Value() Helpers. // - 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) IMGUI_API void Value(const char* prefix, bool b); IMGUI_API void Value(const char* prefix, int v); IMGUI_API void Value(const char* prefix, unsigned int v); IMGUI_API void Value(const char* prefix, float v, const char* float_format = NULL); // Widgets: Menus IMGUI_API bool BeginMainMenuBar(); // create and append to a full screen menu-bar. IMGUI_API void EndMainMenuBar(); // only call EndMainMenuBar() if BeginMainMenuBar() returns true! IMGUI_API bool BeginMenuBar(); // append to menu-bar of current window (requires ImGuiWindowFlags_MenuBar flag set on parent window). IMGUI_API void EndMenuBar(); // only call EndMenuBar() if BeginMenuBar() returns true! IMGUI_API bool BeginMenu(const char* label, bool enabled = true); // create a sub-menu entry. only call EndMenu() if this returns true! IMGUI_API void EndMenu(); // only call EndMenu() if BeginMenu() returns true! 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 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 // Tooltips IMGUI_API void BeginTooltip(); // begin/append a tooltip window. to create full-featured tooltip (with any kind of items). IMGUI_API void EndTooltip(); 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(). IMGUI_API void SetTooltipV(const char* fmt, va_list args) IM_FMTLIST(1); // Popups, Modals // The properties of popups windows are: // - They block normal mouse hovering detection outside them. (*) // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE. // - 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. // User can manipulate the visibility state by calling OpenPopup(). // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even when normally blocked by a popup. // Those three properties are connected. The library needs to hold their visibility state because it can close popups at any time. 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). 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! 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! 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. 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). 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) IMGUI_API void EndPopup(); // only call EndPopup() if BeginPopupXXX() returns true! 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. 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. IMGUI_API void CloseCurrentPopup(); // close the popup we have begin-ed into. clicking on a MenuItem or Selectable automatically close the current popup. // Columns // - You can also use SameLine(pos_x) to mimic simplified columns. // - The columns API is work-in-progress and rather lacking (columns are arguably the worst part of dear imgui at the moment!) IMGUI_API void Columns(int count = 1, const char* id = NULL, bool border = true); IMGUI_API void NextColumn(); // next column, defaults to current row or next row if the current row is finished IMGUI_API int GetColumnIndex(); // get current column index IMGUI_API float GetColumnWidth(int column_index = -1); // get column width (in pixels). pass -1 to use current column IMGUI_API void SetColumnWidth(int column_index, float width); // set column width (in pixels). pass -1 to use current column 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 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 IMGUI_API int GetColumnsCount(); // Tab Bars, Tabs // [BETA API] API may evolve! // Note: Tabs are automatically created by the docking system. Use this to create tab bars/tabs yourself without docking being involved. IMGUI_API bool BeginTabBar(const char* str_id, ImGuiTabBarFlags flags = 0); // create and append into a TabBar IMGUI_API void EndTabBar(); // only call EndTabBar() if BeginTabBar() returns true! IMGUI_API bool BeginTabItem(const char* label, bool* p_open = NULL, ImGuiTabItemFlags flags = 0);// create a Tab. Returns true if the Tab is selected. IMGUI_API void EndTabItem(); // only call EndTabItem() if BeginTabItem() returns true! 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. // Docking // [BETA API] Enable with io.ConfigFlags |= ImGuiConfigFlags_DockingEnable. // Note: you DO NOT need to call DockSpace() to use most Docking facilities! // To dock windows: if io.ConfigDockingWithShift == false: drag window from their title bar. // To dock windows: if io.ConfigDockingWithShift == true: hold SHIFT anywhere while moving windows. // Use DockSpace() to create an explicit dock node _within_ an existing window. See Docking demo for details. IMGUI_API void DockSpace(ImGuiID id, const ImVec2& size = ImVec2(0, 0), ImGuiDockNodeFlags flags = 0, const ImGuiWindowClass* window_class = NULL); IMGUI_API ImGuiID DockSpaceOverViewport(bool has_main_menu_bar, ImGuiViewport* viewport = NULL, ImGuiDockNodeFlags flags = 0, const ImGuiWindowClass* window_class = NULL); IMGUI_API void SetNextWindowDockID(ImGuiID dock_id, ImGuiCond cond = 0); // set next window dock id (FIXME-DOCK) 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) IMGUI_API ImGuiID GetWindowDockID(); IMGUI_API bool IsWindowDocked(); // is current window docked into another window? // Logging/Capture // - All text output from the interface can be captured into tty/file/clipboard. By default, tree nodes are automatically opened during logging. IMGUI_API void LogToTTY(int auto_open_depth = -1); // start logging to tty (stdout) IMGUI_API void LogToFile(int auto_open_depth = -1, const char* filename = NULL); // start logging to file IMGUI_API void LogToClipboard(int auto_open_depth = -1); // start logging to OS clipboard IMGUI_API void LogFinish(); // stop logging (close file, etc.) IMGUI_API void LogButtons(); // helper to display buttons for logging to tty/file/clipboard IMGUI_API void LogText(const char* fmt, ...) IM_FMTARGS(1); // pass text data straight to log (without being displayed) // Drag and Drop // [BETA API] API may evolve! IMGUI_API bool BeginDragDropSource(ImGuiDragDropFlags flags = 0); // call when the current item is active. If this return true, you can call SetDragDropPayload() + EndDragDropSource() 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. IMGUI_API void EndDragDropSource(); // only call EndDragDropSource() if BeginDragDropSource() returns true! IMGUI_API bool BeginDragDropTarget(); // call after submitting an item that may receive a payload. If this returns true, you can call AcceptDragDropPayload() + EndDragDropTarget() 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. IMGUI_API void EndDragDropTarget(); // only call EndDragDropTarget() if BeginDragDropTarget() returns true! 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. // Clipping IMGUI_API void PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect); IMGUI_API void PopClipRect(); // Focus, Activation // - Prefer using "SetItemDefaultFocus()" over "if (IsWindowAppearing()) SetScrollHereY()" when applicable to signify "this is the default item" IMGUI_API void SetItemDefaultFocus(); // make last item the default focused item of a window. 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. // Item/Widgets Utilities // - Most of the functions are referring to the last/previous item we submitted. // - See Demo Window under "Widgets->Querying Status" for an interactive visualization of most of those functions. 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. 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) IMGUI_API bool IsItemFocused(); // is the last item focused for keyboard/gamepad navigation? IMGUI_API bool IsItemClicked(int mouse_button = 0); // is the last item clicked? (e.g. button/node just clicked on) == IsMouseClicked(mouse_button) && IsItemHovered() IMGUI_API bool IsItemVisible(); // is the last item visible? (items may be out of sight because of clipping/scrolling) 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. IMGUI_API bool IsItemActivated(); // was the last item just made active (item was previously inactive). 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. 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). IMGUI_API bool IsAnyItemHovered(); // is any item hovered? IMGUI_API bool IsAnyItemActive(); // is any item active? IMGUI_API bool IsAnyItemFocused(); // is any item focused? IMGUI_API ImVec2 GetItemRectMin(); // get upper-left bounding rectangle of the last item (screen space) IMGUI_API ImVec2 GetItemRectMax(); // get lower-right bounding rectangle of the last item (screen space) IMGUI_API ImVec2 GetItemRectSize(); // get size of last item 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. // Miscellaneous Utilities IMGUI_API bool IsRectVisible(const ImVec2& size); // test if rectangle (of given size, starting from cursor position) is visible / not clipped. 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. IMGUI_API double GetTime(); // get global imgui time. incremented by io.DeltaTime every frame. IMGUI_API int GetFrameCount(); // get global imgui frame count. incremented by 1 every frame. 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. 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. 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. 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. IMGUI_API ImDrawListSharedData* GetDrawListSharedData(); // you may use this when creating your own ImDrawList instances. IMGUI_API const char* GetStyleColorName(ImGuiCol idx); // get a string corresponding to the enum value (for display, saving, etc.). 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) IMGUI_API ImGuiStorage* GetStateStorage(); IMGUI_API ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); 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. 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 IMGUI_API void EndChildFrame(); // always call EndChildFrame() regardless of BeginChildFrame() return values (which indicates a collapsed/clipped window) // Color Utilities IMGUI_API ImVec4 ColorConvertU32ToFloat4(ImU32 in); IMGUI_API ImU32 ColorConvertFloat4ToU32(const ImVec4& in); IMGUI_API void ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v); IMGUI_API void ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b); // Inputs Utilities IMGUI_API int GetKeyIndex(ImGuiKey imgui_key); // map ImGuiKey_* values into user's key index. == io.KeyMap[key] 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[]! 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 IMGUI_API bool IsKeyReleased(int user_key_index); // was key released (went from Down to !Down).. 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 IMGUI_API bool IsMouseDown(int button); // is mouse button held (0=left, 1=right, 2=middle) IMGUI_API bool IsAnyMouseDown(); // is any mouse button held IMGUI_API bool IsMouseClicked(int button, bool repeat = false); // did mouse button clicked (went from !Down to Down) (0=left, 1=right, 2=middle) IMGUI_API bool IsMouseDoubleClicked(int button); // did mouse button double-clicked. a double-click returns false in IsMouseClicked(). uses io.MouseDoubleClickTime. IMGUI_API bool IsMouseReleased(int button); // did mouse button released (went from Down to !Down) IMGUI_API bool IsMouseDragging(int button = 0, float lock_threshold = -1.0f); // is mouse dragging. if lock_threshold < -1.0f uses io.MouseDraggingThreshold 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. IMGUI_API bool IsMousePosValid(const ImVec2* mouse_pos = NULL); // by convention we use (-FLT_MAX,-FLT_MAX) to denote that there is no mouse IMGUI_API ImVec2 GetMousePos(); // shortcut to ImGui::GetIO().MousePos provided by user, to be consistent with other calls IMGUI_API ImVec2 GetMousePosOnOpeningCurrentPopup(); // retrieve backup of mouse position at the time of opening popup we have BeginPopup() into 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. IMGUI_API void ResetMouseDragDelta(int button = 0); // 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 IMGUI_API void SetMouseCursor(ImGuiMouseCursor type); // set desired cursor type 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. 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. // Clipboard Utilities (also see the LogToClipboard() function to capture or output text data to the clipboard) IMGUI_API const char* GetClipboardText(); IMGUI_API void SetClipboardText(const char* text); // Settings/.Ini Utilities // - The disk functions are automatically called if io.IniFilename != NULL (default is "imgui.ini"). // - Set io.IniFilename to NULL to load/save manually. Read io.WantSaveIniSettings description about handling .ini saving manually. IMGUI_API void LoadIniSettingsFromDisk(const char* ini_filename); // call after CreateContext() and before the first call to NewFrame(). NewFrame() automatically calls LoadIniSettingsFromDisk(io.IniFilename). 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. 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). 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. // Memory Allocators // - All those functions are not reliant on the current context. // - 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. 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); IMGUI_API void* MemAlloc(size_t size); IMGUI_API void MemFree(void* ptr); // (Optional) Platform/OS interface for multi-viewport support // Note: You may use GetWindowViewport() to get the current viewport of the current window. IMGUI_API ImGuiPlatformIO& GetPlatformIO(); // platform/renderer functions, for back-end to setup + viewports list. IMGUI_API ImGuiViewport* GetMainViewport(); // main viewport. same as GetPlatformIO().MainViewport == GetPlatformIO().Viewports[0]. 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. 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. 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(). IMGUI_API ImGuiViewport* FindViewportByID(ImGuiID id); // this is a helper for back-ends. 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.) } // namespace ImGui //----------------------------------------------------------------------------- // Flags & Enumerations //----------------------------------------------------------------------------- // Flags for ImGui::Begin() enum ImGuiWindowFlags_ { ImGuiWindowFlags_None = 0, ImGuiWindowFlags_NoTitleBar = 1 << 0, // Disable title-bar ImGuiWindowFlags_NoResize = 1 << 1, // Disable user resizing with the lower-right grip ImGuiWindowFlags_NoMove = 1 << 2, // Disable user moving the window ImGuiWindowFlags_NoScrollbar = 1 << 3, // Disable scrollbars (window can still scroll with mouse or programmatically) 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. ImGuiWindowFlags_NoCollapse = 1 << 5, // Disable user collapsing window by double-clicking on it ImGuiWindowFlags_AlwaysAutoResize = 1 << 6, // Resize every window to its content every frame ImGuiWindowFlags_NoBackground = 1 << 7, // Disable drawing background color (WindowBg, etc.) and outside border. Similar as using SetNextWindowBgAlpha(0.0f). ImGuiWindowFlags_NoSavedSettings = 1 << 8, // Never load/save settings in .ini file ImGuiWindowFlags_NoMouseInputs = 1 << 9, // Disable catching mouse, hovering test with pass through. ImGuiWindowFlags_MenuBar = 1 << 10, // Has a menu-bar 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. ImGuiWindowFlags_NoFocusOnAppearing = 1 << 12, // Disable taking focus when transitioning from hidden to visible state ImGuiWindowFlags_NoBringToFrontOnFocus = 1 << 13, // Disable bringing window to front when taking focus (e.g. clicking on it or programmatically giving it focus) ImGuiWindowFlags_AlwaysVerticalScrollbar= 1 << 14, // Always show vertical scrollbar (even if ContentSize.y < Size.y) ImGuiWindowFlags_AlwaysHorizontalScrollbar=1<< 15, // Always show horizontal scrollbar (even if ContentSize.x < Size.x) ImGuiWindowFlags_AlwaysUseWindowPadding = 1 << 16, // Ensure child windows without border uses style.WindowPadding (ignored by default for non-bordered child windows, because more convenient) ImGuiWindowFlags_NoNavInputs = 1 << 18, // No gamepad/keyboard navigation within the window ImGuiWindowFlags_NoNavFocus = 1 << 19, // No focusing toward this window with gamepad/keyboard navigation (e.g. skipped by CTRL+TAB) 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. ImGuiWindowFlags_NoDocking = 1 << 21, // Disable docking of this window ImGuiWindowFlags_NoNav = ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, ImGuiWindowFlags_NoDecoration = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse, ImGuiWindowFlags_NoInputs = ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, // [Internal] 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!) ImGuiWindowFlags_ChildWindow = 1 << 24, // Don't use! For internal use by BeginChild() ImGuiWindowFlags_Tooltip = 1 << 25, // Don't use! For internal use by BeginTooltip() ImGuiWindowFlags_Popup = 1 << 26, // Don't use! For internal use by BeginPopup() ImGuiWindowFlags_Modal = 1 << 27, // Don't use! For internal use by BeginPopupModal() ImGuiWindowFlags_ChildMenu = 1 << 28, // Don't use! For internal use by BeginMenu() ImGuiWindowFlags_DockNodeHost = 1 << 29 // Don't use! For internal use by Begin()/NewFrame() // [Obsolete] //ImGuiWindowFlags_ShowBorders = 1 << 7, // --> Set style.FrameBorderSize=1.0f / style.WindowBorderSize=1.0f to enable borders around windows and items //ImGuiWindowFlags_ResizeFromAnySide = 1 << 17, // --> Set io.ConfigWindowsResizeFromEdges and make sure mouse cursors are supported by back-end (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) }; // Flags for ImGui::InputText() enum ImGuiInputTextFlags_ { ImGuiInputTextFlags_None = 0, ImGuiInputTextFlags_CharsDecimal = 1 << 0, // Allow 0123456789.+-*/ ImGuiInputTextFlags_CharsHexadecimal = 1 << 1, // Allow 0123456789ABCDEFabcdef ImGuiInputTextFlags_CharsUppercase = 1 << 2, // Turn a..z into A..Z ImGuiInputTextFlags_CharsNoBlank = 1 << 3, // Filter out spaces, tabs ImGuiInputTextFlags_AutoSelectAll = 1 << 4, // Select entire text when first taking mouse focus 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. ImGuiInputTextFlags_CallbackCompletion = 1 << 6, // Callback on pressing TAB (for completion handling) ImGuiInputTextFlags_CallbackHistory = 1 << 7, // Callback on pressing Up/Down arrows (for history handling) ImGuiInputTextFlags_CallbackAlways = 1 << 8, // Callback on each iteration. User code may query cursor position, modify text buffer. 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. ImGuiInputTextFlags_AllowTabInput = 1 << 10, // Pressing TAB input a '\t' character into the text field 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). ImGuiInputTextFlags_NoHorizontalScroll = 1 << 12, // Disable following the cursor horizontally ImGuiInputTextFlags_AlwaysInsertMode = 1 << 13, // Insert mode ImGuiInputTextFlags_ReadOnly = 1 << 14, // Read-only mode ImGuiInputTextFlags_Password = 1 << 15, // Password mode, display all characters as '*' 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(). ImGuiInputTextFlags_CharsScientific = 1 << 17, // Allow 0123456789.+-*/eE (Scientific notation input) 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) // [Internal] ImGuiInputTextFlags_Multiline = 1 << 20 // For internal use by InputTextMultiline() }; // Flags for ImGui::TreeNodeEx(), ImGui::CollapsingHeader*() enum ImGuiTreeNodeFlags_ { ImGuiTreeNodeFlags_None = 0, ImGuiTreeNodeFlags_Selected = 1 << 0, // Draw as selected ImGuiTreeNodeFlags_Framed = 1 << 1, // Full colored frame (e.g. for CollapsingHeader) ImGuiTreeNodeFlags_AllowItemOverlap = 1 << 2, // Hit testing to allow subsequent widgets to overlap this one ImGuiTreeNodeFlags_NoTreePushOnOpen = 1 << 3, // Don't do a TreePush() when open (e.g. for CollapsingHeader) = no extra indent nor pushing on ID stack ImGuiTreeNodeFlags_NoAutoOpenOnLog = 1 << 4, // Don't automatically and temporarily open node when Logging is active (by default logging will automatically open tree nodes) ImGuiTreeNodeFlags_DefaultOpen = 1 << 5, // Default node to be open ImGuiTreeNodeFlags_OpenOnDoubleClick = 1 << 6, // Need double-click to open node 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. ImGuiTreeNodeFlags_Leaf = 1 << 8, // No collapsing, no arrow (use as a convenience for leaf nodes). ImGuiTreeNodeFlags_Bullet = 1 << 9, // Display a bullet instead of arrow 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(). //ImGuiTreeNodeFlags_SpanAllAvailWidth = 1 << 11, // FIXME: TODO: Extend hit box horizontally even if not framed //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 12, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 13, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop) ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS , ImGuiTreeNodeFlags_AllowOverlapMode = ImGuiTreeNodeFlags_AllowItemOverlap #endif }; // Flags for ImGui::Selectable() enum ImGuiSelectableFlags_ { ImGuiSelectableFlags_None = 0, ImGuiSelectableFlags_DontClosePopups = 1 << 0, // Clicking this don't close parent popup window ImGuiSelectableFlags_SpanAllColumns = 1 << 1, // Selectable frame can span all columns (text will still fit in current column) ImGuiSelectableFlags_AllowDoubleClick = 1 << 2, // Generate press events on double clicks too ImGuiSelectableFlags_Disabled = 1 << 3 // Cannot be selected, display greyed out text }; // Flags for ImGui::BeginCombo() enum ImGuiComboFlags_ { ImGuiComboFlags_None = 0, ImGuiComboFlags_PopupAlignLeft = 1 << 0, // Align the popup toward the left by default 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() ImGuiComboFlags_HeightRegular = 1 << 2, // Max ~8 items visible (default) ImGuiComboFlags_HeightLarge = 1 << 3, // Max ~20 items visible ImGuiComboFlags_HeightLargest = 1 << 4, // As many fitting items as possible ImGuiComboFlags_NoArrowButton = 1 << 5, // Display on the preview box without the square arrow button ImGuiComboFlags_NoPreview = 1 << 6, // Display only a square arrow button ImGuiComboFlags_HeightMask_ = ImGuiComboFlags_HeightSmall | ImGuiComboFlags_HeightRegular | ImGuiComboFlags_HeightLarge | ImGuiComboFlags_HeightLargest }; // Flags for ImGui::BeginTabBar() enum ImGuiTabBarFlags_ { ImGuiTabBarFlags_None = 0, ImGuiTabBarFlags_Reorderable = 1 << 0, // Allow manually dragging tabs to re-order them + New tabs are appended at the end of list ImGuiTabBarFlags_AutoSelectNewTabs = 1 << 1, // Automatically select new tabs when they appear ImGuiTabBarFlags_TabListPopupButton = 1 << 2, 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. ImGuiTabBarFlags_NoTabListScrollingButtons = 1 << 4, ImGuiTabBarFlags_NoTooltip = 1 << 5, // Disable tooltips when hovering a tab ImGuiTabBarFlags_FittingPolicyResizeDown = 1 << 6, // Resize tabs when they don't fit ImGuiTabBarFlags_FittingPolicyScroll = 1 << 7, // Add scroll buttons when tabs don't fit ImGuiTabBarFlags_FittingPolicyMask_ = ImGuiTabBarFlags_FittingPolicyResizeDown | ImGuiTabBarFlags_FittingPolicyScroll, ImGuiTabBarFlags_FittingPolicyDefault_ = ImGuiTabBarFlags_FittingPolicyResizeDown }; // Flags for ImGui::BeginTabItem() enum ImGuiTabItemFlags_ { ImGuiTabItemFlags_None = 0, 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. ImGuiTabItemFlags_SetSelected = 1 << 1, // Trigger flag to programmatically make the tab selected when calling BeginTabItem() 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. ImGuiTabItemFlags_NoPushId = 1 << 3 // Don't call PushID(tab->ID)/PopID() on BeginTabItem()/EndTabItem() }; // Flags for ImGui::DockSpace(), shared/inherited by child nodes. // (Some flags can be applied to individual nodes directly) enum ImGuiDockNodeFlags_ { ImGuiDockNodeFlags_None = 0, 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. //ImGuiDockNodeFlags_NoCentralNode = 1 << 1, // Shared // Disable Central Node (the node which can stay empty) ImGuiDockNodeFlags_NoDockingInCentralNode = 1 << 2, // Shared // Disable docking inside the Central Node, which will be always kept empty. 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. 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. ImGuiDockNodeFlags_NoResize = 1 << 5, // Shared/Local // Disable resizing child nodes using the splitter/separators. Useful with programatically setup dockspaces. ImGuiDockNodeFlags_AutoHideTabBar = 1 << 6 // Shared/Local // Tab bar will automatically hide when there is a single window in the dock node. }; // Flags for ImGui::IsWindowFocused() enum ImGuiFocusedFlags_ { ImGuiFocusedFlags_None = 0, ImGuiFocusedFlags_ChildWindows = 1 << 0, // IsWindowFocused(): Return true if any children of the window is focused ImGuiFocusedFlags_RootWindow = 1 << 1, // IsWindowFocused(): Test from root window (top most parent of the current hierarchy) 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. ImGuiFocusedFlags_RootAndChildWindows = ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows }; // Flags for ImGui::IsItemHovered(), ImGui::IsWindowHovered() // 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! // Note: windows with the ImGuiWindowFlags_NoInputs flag are ignored by IsWindowHovered() calls. enum ImGuiHoveredFlags_ { 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. ImGuiHoveredFlags_ChildWindows = 1 << 0, // IsWindowHovered() only: Return true if any children of the window is hovered ImGuiHoveredFlags_RootWindow = 1 << 1, // IsWindowHovered() only: Test from root window (top most parent of the current hierarchy) ImGuiHoveredFlags_AnyWindow = 1 << 2, // IsWindowHovered() only: Return true if any window is hovered ImGuiHoveredFlags_AllowWhenBlockedByPopup = 1 << 3, // Return true even if a popup window is normally blocking access to this item/window //ImGuiHoveredFlags_AllowWhenBlockedByModal = 1 << 4, // Return true even if a modal popup window is normally blocking access to this item/window. FIXME-TODO: Unavailable yet. ImGuiHoveredFlags_AllowWhenBlockedByActiveItem = 1 << 5, // Return true even if an active item is blocking access to this item/window. Useful for Drag and Drop patterns. ImGuiHoveredFlags_AllowWhenOverlapped = 1 << 6, // Return true even if the position is overlapped by another window ImGuiHoveredFlags_AllowWhenDisabled = 1 << 7, // Return true even if the item is disabled ImGuiHoveredFlags_RectOnly = ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenOverlapped, ImGuiHoveredFlags_RootAndChildWindows = ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows }; // Flags for ImGui::BeginDragDropSource(), ImGui::AcceptDragDropPayload() enum ImGuiDragDropFlags_ { ImGuiDragDropFlags_None = 0, // BeginDragDropSource() flags 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. 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. 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. 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. 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. ImGuiDragDropFlags_SourceAutoExpirePayload = 1 << 5, // Automatically expire the payload if the source cease to be submitted (otherwise payloads are persisting while being dragged) // AcceptDragDropPayload() flags 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. ImGuiDragDropFlags_AcceptNoDrawDefaultRect = 1 << 11, // Do not draw the default highlight rectangle when hovering over target. ImGuiDragDropFlags_AcceptNoPreviewTooltip = 1 << 12, // Request hiding the BeginDragDropSource tooltip from the BeginDragDropTarget site. ImGuiDragDropFlags_AcceptPeekOnly = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect // For peeking ahead and inspecting the payload before delivery. }; // Standard Drag and Drop payload types. You can define you own payload types using short strings. Types starting with '_' are defined by Dear ImGui. #define IMGUI_PAYLOAD_TYPE_COLOR_3F "_COL3F" // float[3]: Standard type for colors, without alpha. User code may use this type. #define IMGUI_PAYLOAD_TYPE_COLOR_4F "_COL4F" // float[4]: Standard type for colors. User code may use this type. // A primary data type enum ImGuiDataType_ { ImGuiDataType_S8, // char ImGuiDataType_U8, // unsigned char ImGuiDataType_S16, // short ImGuiDataType_U16, // unsigned short ImGuiDataType_S32, // int ImGuiDataType_U32, // unsigned int ImGuiDataType_S64, // long long / __int64 ImGuiDataType_U64, // unsigned long long / unsigned __int64 ImGuiDataType_Float, // float ImGuiDataType_Double, // double ImGuiDataType_COUNT }; // A cardinal direction enum ImGuiDir_ { ImGuiDir_None = -1, ImGuiDir_Left = 0, ImGuiDir_Right = 1, ImGuiDir_Up = 2, ImGuiDir_Down = 3, ImGuiDir_COUNT }; // User fill ImGuiIO.KeyMap[] array with indices into the ImGuiIO.KeysDown[512] array enum ImGuiKey_ { ImGuiKey_Tab, ImGuiKey_LeftArrow, ImGuiKey_RightArrow, ImGuiKey_UpArrow, ImGuiKey_DownArrow, ImGuiKey_PageUp, ImGuiKey_PageDown, ImGuiKey_Home, ImGuiKey_End, ImGuiKey_Insert, ImGuiKey_Delete, ImGuiKey_Backspace, ImGuiKey_Space, ImGuiKey_Enter, ImGuiKey_Escape, ImGuiKey_A, // for text edit CTRL+A: select all ImGuiKey_C, // for text edit CTRL+C: copy ImGuiKey_V, // for text edit CTRL+V: paste ImGuiKey_X, // for text edit CTRL+X: cut ImGuiKey_Y, // for text edit CTRL+Y: redo ImGuiKey_Z, // for text edit CTRL+Z: undo ImGuiKey_COUNT }; // Gamepad/Keyboard directional navigation // Keyboard: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. // 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(). // Read instructions in imgui.cpp for more details. Download PNG/PSD at http://goo.gl/9LgVZW. enum ImGuiNavInput_ { // Gamepad Mapping ImGuiNavInput_Activate, // activate / open / toggle / tweak value // e.g. Cross (PS4), A (Xbox), A (Switch), Space (Keyboard) ImGuiNavInput_Cancel, // cancel / close / exit // e.g. Circle (PS4), B (Xbox), B (Switch), Escape (Keyboard) ImGuiNavInput_Input, // text input / on-screen keyboard // e.g. Triang.(PS4), Y (Xbox), X (Switch), Return (Keyboard) ImGuiNavInput_Menu, // tap: toggle menu / hold: focus, move, resize // e.g. Square (PS4), X (Xbox), Y (Switch), Alt (Keyboard) ImGuiNavInput_DpadLeft, // move / tweak / resize window (w/ PadMenu) // e.g. D-pad Left/Right/Up/Down (Gamepads), Arrow keys (Keyboard) ImGuiNavInput_DpadRight, // ImGuiNavInput_DpadUp, // ImGuiNavInput_DpadDown, // ImGuiNavInput_LStickLeft, // scroll / move window (w/ PadMenu) // e.g. Left Analog Stick Left/Right/Up/Down ImGuiNavInput_LStickRight, // ImGuiNavInput_LStickUp, // ImGuiNavInput_LStickDown, // ImGuiNavInput_FocusPrev, // next window (w/ PadMenu) // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch) ImGuiNavInput_FocusNext, // prev window (w/ PadMenu) // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch) ImGuiNavInput_TweakSlow, // slower tweaks // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch) ImGuiNavInput_TweakFast, // faster tweaks // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch) // [Internal] Don't use directly! This is used internally to differentiate keyboard from gamepad inputs for behaviors that require to differentiate them. // Keyboard behavior that have no corresponding gamepad mapping (e.g. CTRL+TAB) will be directly reading from io.KeysDown[] instead of io.NavInputs[]. ImGuiNavInput_KeyMenu_, // toggle menu // = io.KeyAlt ImGuiNavInput_KeyTab_, // tab // = Tab key ImGuiNavInput_KeyLeft_, // move left // = Arrow keys ImGuiNavInput_KeyRight_, // move right ImGuiNavInput_KeyUp_, // move up ImGuiNavInput_KeyDown_, // move down ImGuiNavInput_COUNT, ImGuiNavInput_InternalStart_ = ImGuiNavInput_KeyMenu_ }; // Configuration flags stored in io.ConfigFlags. Set by user/application. enum ImGuiConfigFlags_ { ImGuiConfigFlags_None = 0, ImGuiConfigFlags_NavEnableKeyboard = 1 << 0, // Master keyboard navigation enable flag. NewFrame() will automatically fill io.NavInputs[] based on io.KeysDown[]. 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. 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. ImGuiConfigFlags_NavNoCaptureKeyboard = 1 << 3, // Instruct navigation to not set the io.WantCaptureKeyboard flag when io.NavActive is set. ImGuiConfigFlags_NoMouse = 1 << 4, // Instruct imgui to clear mouse position/buttons in NewFrame(). This allows ignoring the mouse information set by the back-end. 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. // [BETA] Docking ImGuiConfigFlags_DockingEnable = 1 << 6, // Docking enable flags. Use SHIFT to dock window into another (or without SHIFT if io.ConfigDockingWithShift = false). // [BETA] Viewports // 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. ImGuiConfigFlags_ViewportsEnable = 1 << 10, // Viewport enable flags (require both ImGuiConfigFlags_PlatformHasViewports + ImGuiConfigFlags_RendererHasViewports set by the respective back-ends) 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. 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. // 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) ImGuiConfigFlags_IsSRGB = 1 << 20, // Application is SRGB-aware. ImGuiConfigFlags_IsTouchScreen = 1 << 21 // Application is using a touch screen instead of a mouse. }; // Back-end capabilities flags stored in io.BackendFlags. Set by imgui_impl_xxx or custom back-end. enum ImGuiBackendFlags_ { ImGuiBackendFlags_None = 0, ImGuiBackendFlags_HasGamepad = 1 << 0, // Back-end supports gamepad and currently has one connected. ImGuiBackendFlags_HasMouseCursors = 1 << 1, // Back-end supports honoring GetMouseCursor() value to change the OS cursor shape. ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Back-end supports io.WantSetMousePos requests to reposition the OS mouse position (only used if ImGuiConfigFlags_NavEnableSetMousePos is set). // [BETA] Viewports ImGuiBackendFlags_PlatformHasViewports = 1 << 10, // Back-end Platform supports multiple viewports. 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! ImGuiBackendFlags_RendererHasViewports = 1 << 12 // Back-end Renderer supports multiple viewports. }; // Enumeration for PushStyleColor() / PopStyleColor() enum ImGuiCol_ { ImGuiCol_Text, ImGuiCol_TextDisabled, ImGuiCol_WindowBg, // Background of normal windows ImGuiCol_ChildBg, // Background of child windows ImGuiCol_PopupBg, // Background of popups, menus, tooltips windows ImGuiCol_Border, ImGuiCol_BorderShadow, ImGuiCol_FrameBg, // Background of checkbox, radio button, plot, slider, text input ImGuiCol_FrameBgHovered, ImGuiCol_FrameBgActive, ImGuiCol_TitleBg, ImGuiCol_TitleBgActive, ImGuiCol_TitleBgCollapsed, ImGuiCol_MenuBarBg, ImGuiCol_ScrollbarBg, ImGuiCol_ScrollbarGrab, ImGuiCol_ScrollbarGrabHovered, ImGuiCol_ScrollbarGrabActive, ImGuiCol_CheckMark, ImGuiCol_SliderGrab, ImGuiCol_SliderGrabActive, ImGuiCol_Button, ImGuiCol_ButtonHovered, ImGuiCol_ButtonActive, ImGuiCol_Header, ImGuiCol_HeaderHovered, ImGuiCol_HeaderActive, ImGuiCol_Separator, ImGuiCol_SeparatorHovered, ImGuiCol_SeparatorActive, ImGuiCol_ResizeGrip, ImGuiCol_ResizeGripHovered, ImGuiCol_ResizeGripActive, ImGuiCol_Tab, ImGuiCol_TabHovered, ImGuiCol_TabActive, ImGuiCol_TabUnfocused, ImGuiCol_TabUnfocusedActive, ImGuiCol_DockingPreview, ImGuiCol_DockingEmptyBg, // Background color for empty node (e.g. CentralNode with no window docked into it) ImGuiCol_PlotLines, ImGuiCol_PlotLinesHovered, ImGuiCol_PlotHistogram, ImGuiCol_PlotHistogramHovered, ImGuiCol_TextSelectedBg, ImGuiCol_DragDropTarget, ImGuiCol_NavHighlight, // Gamepad/keyboard: current highlighted item ImGuiCol_NavWindowingHighlight, // Highlight window when using CTRL+TAB ImGuiCol_NavWindowingDimBg, // Darken/colorize entire screen behind the CTRL+TAB window list, when active ImGuiCol_ModalWindowDimBg, // Darken/colorize entire screen behind a modal window, when one is active ImGuiCol_COUNT // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS , ImGuiCol_ModalWindowDarkening = ImGuiCol_ModalWindowDimBg // [renamed in 1.63] , ImGuiCol_ChildWindowBg = ImGuiCol_ChildBg // [renamed in 1.53] , ImGuiCol_Column = ImGuiCol_Separator, ImGuiCol_ColumnHovered = ImGuiCol_SeparatorHovered, ImGuiCol_ColumnActive = ImGuiCol_SeparatorActive // [renamed in 1.51] //ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered, // [unused since 1.60+] the close button now uses regular button colors. //ImGuiCol_ComboBg, // [unused since 1.53+] ComboBg has been merged with PopupBg, so a redirect isn't accurate. #endif }; // Enumeration for PushStyleVar() / PopStyleVar() to temporarily modify the ImGuiStyle structure. // 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. // 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. enum ImGuiStyleVar_ { // Enum name --------------------- // Member in ImGuiStyle structure (see ImGuiStyle for descriptions) ImGuiStyleVar_Alpha, // float Alpha ImGuiStyleVar_WindowPadding, // ImVec2 WindowPadding ImGuiStyleVar_WindowRounding, // float WindowRounding ImGuiStyleVar_WindowBorderSize, // float WindowBorderSize ImGuiStyleVar_WindowMinSize, // ImVec2 WindowMinSize ImGuiStyleVar_WindowTitleAlign, // ImVec2 WindowTitleAlign ImGuiStyleVar_ChildRounding, // float ChildRounding ImGuiStyleVar_ChildBorderSize, // float ChildBorderSize ImGuiStyleVar_PopupRounding, // float PopupRounding ImGuiStyleVar_PopupBorderSize, // float PopupBorderSize ImGuiStyleVar_FramePadding, // ImVec2 FramePadding ImGuiStyleVar_FrameRounding, // float FrameRounding ImGuiStyleVar_FrameBorderSize, // float FrameBorderSize ImGuiStyleVar_ItemSpacing, // ImVec2 ItemSpacing ImGuiStyleVar_ItemInnerSpacing, // ImVec2 ItemInnerSpacing ImGuiStyleVar_IndentSpacing, // float IndentSpacing ImGuiStyleVar_ScrollbarSize, // float ScrollbarSize ImGuiStyleVar_ScrollbarRounding, // float ScrollbarRounding ImGuiStyleVar_GrabMinSize, // float GrabMinSize ImGuiStyleVar_GrabRounding, // float GrabRounding ImGuiStyleVar_TabRounding, // float TabRounding ImGuiStyleVar_ButtonTextAlign, // ImVec2 ButtonTextAlign ImGuiStyleVar_SelectableTextAlign, // ImVec2 SelectableTextAlign ImGuiStyleVar_COUNT // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS , ImGuiStyleVar_Count_ = ImGuiStyleVar_COUNT, ImGuiStyleVar_ChildWindowRounding = ImGuiStyleVar_ChildRounding #endif }; // Flags for ColorEdit3() / ColorEdit4() / ColorPicker3() / ColorPicker4() / ColorButton() enum ImGuiColorEditFlags_ { ImGuiColorEditFlags_None = 0, ImGuiColorEditFlags_NoAlpha = 1 << 1, // // ColorEdit, ColorPicker, ColorButton: ignore Alpha component (will only read 3 components from the input pointer). ImGuiColorEditFlags_NoPicker = 1 << 2, // // ColorEdit: disable picker when clicking on colored square. ImGuiColorEditFlags_NoOptions = 1 << 3, // // ColorEdit: disable toggling options menu when right-clicking on inputs/small preview. ImGuiColorEditFlags_NoSmallPreview = 1 << 4, // // ColorEdit, ColorPicker: disable colored square preview next to the inputs. (e.g. to show only the inputs) ImGuiColorEditFlags_NoInputs = 1 << 5, // // ColorEdit, ColorPicker: disable inputs sliders/text widgets (e.g. to show only the small preview colored square). ImGuiColorEditFlags_NoTooltip = 1 << 6, // // ColorEdit, ColorPicker, ColorButton: disable tooltip when hovering the preview. ImGuiColorEditFlags_NoLabel = 1 << 7, // // ColorEdit, ColorPicker: disable display of inline text label (the label is still forwarded to the tooltip and picker). ImGuiColorEditFlags_NoSidePreview = 1 << 8, // // ColorPicker: disable bigger color preview on right side of the picker, use small colored square preview instead. ImGuiColorEditFlags_NoDragDrop = 1 << 9, // // ColorEdit: disable drag and drop target. ColorButton: disable drag and drop source. // User Options (right-click on widget to change some of them). ImGuiColorEditFlags_AlphaBar = 1 << 16, // // ColorEdit, ColorPicker: show vertical alpha bar/gradient in picker. ImGuiColorEditFlags_AlphaPreview = 1 << 17, // // ColorEdit, ColorPicker, ColorButton: display preview as a transparent color over a checkerboard, instead of opaque. ImGuiColorEditFlags_AlphaPreviewHalf= 1 << 18, // // ColorEdit, ColorPicker, ColorButton: display half opaque / half checkerboard, instead of opaque. 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). 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. ImGuiColorEditFlags_DisplayHSV = 1 << 21, // [Display] // " ImGuiColorEditFlags_DisplayHex = 1 << 22, // [Display] // " ImGuiColorEditFlags_Uint8 = 1 << 23, // [DataType] // ColorEdit, ColorPicker, ColorButton: _display_ values formatted as 0..255. 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. ImGuiColorEditFlags_PickerHueBar = 1 << 25, // [Picker] // ColorPicker: bar for Hue, rectangle for Sat/Value. ImGuiColorEditFlags_PickerHueWheel = 1 << 26, // [Picker] // ColorPicker: wheel for Hue, triangle for Sat/Value. ImGuiColorEditFlags_InputRGB = 1 << 27, // [Input] // ColorEdit, ColorPicker: input and output data in RGB format. ImGuiColorEditFlags_InputHSV = 1 << 28, // [Input] // ColorEdit, ColorPicker: input and output data in HSV format. // Defaults Options. You can set application defaults using SetColorEditOptions(). The intent is that you probably don't want to // override them in most of your calls. Let the user choose via the option menu and/or call SetColorEditOptions() once during startup. ImGuiColorEditFlags__OptionsDefault = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_DisplayRGB|ImGuiColorEditFlags_InputRGB|ImGuiColorEditFlags_PickerHueBar, // [Internal] Masks ImGuiColorEditFlags__DisplayMask = ImGuiColorEditFlags_DisplayRGB|ImGuiColorEditFlags_DisplayHSV|ImGuiColorEditFlags_DisplayHex, ImGuiColorEditFlags__DataTypeMask = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_Float, ImGuiColorEditFlags__PickerMask = ImGuiColorEditFlags_PickerHueWheel|ImGuiColorEditFlags_PickerHueBar, ImGuiColorEditFlags__InputMask = ImGuiColorEditFlags_InputRGB|ImGuiColorEditFlags_InputHSV // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS , ImGuiColorEditFlags_RGB = ImGuiColorEditFlags_DisplayRGB, ImGuiColorEditFlags_HSV = ImGuiColorEditFlags_DisplayHSV, ImGuiColorEditFlags_HEX = ImGuiColorEditFlags_DisplayHex #endif }; // Enumeration for GetMouseCursor() // User code may request binding to display given cursor by calling SetMouseCursor(), which is why we have some cursors that are marked unused here enum ImGuiMouseCursor_ { ImGuiMouseCursor_None = -1, ImGuiMouseCursor_Arrow = 0, ImGuiMouseCursor_TextInput, // When hovering over InputText, etc. ImGuiMouseCursor_ResizeAll, // (Unused by imgui functions) ImGuiMouseCursor_ResizeNS, // When hovering over an horizontal border ImGuiMouseCursor_ResizeEW, // When hovering over a vertical border or a column ImGuiMouseCursor_ResizeNESW, // When hovering over the bottom-left corner of a window ImGuiMouseCursor_ResizeNWSE, // When hovering over the bottom-right corner of a window ImGuiMouseCursor_Hand, // (Unused by imgui functions. Use for e.g. hyperlinks) ImGuiMouseCursor_COUNT // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS , ImGuiMouseCursor_Count_ = ImGuiMouseCursor_COUNT #endif }; // Enumateration for ImGui::SetWindow***(), SetNextWindow***(), SetNextTreeNode***() functions // Represent a condition. // 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. enum ImGuiCond_ { ImGuiCond_Always = 1 << 0, // Set the variable ImGuiCond_Once = 1 << 1, // Set the variable once per runtime session (only the first call with succeed) ImGuiCond_FirstUseEver = 1 << 2, // Set the variable if the object/window has no persistently saved data (no entry in .ini file) ImGuiCond_Appearing = 1 << 3 // Set the variable if the object/window is appearing after being hidden/inactive (or the first time) // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS , ImGuiSetCond_Always = ImGuiCond_Always, ImGuiSetCond_Once = ImGuiCond_Once, ImGuiSetCond_FirstUseEver = ImGuiCond_FirstUseEver, ImGuiSetCond_Appearing = ImGuiCond_Appearing #endif }; //----------------------------------------------------------------------------- // Helpers: Memory allocations macros // IM_MALLOC(), IM_FREE(), IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE() // We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. // Defining a custom placement new() with a dummy parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. //----------------------------------------------------------------------------- struct ImNewDummy {}; inline void* operator new(size_t, ImNewDummy, void* ptr) { return ptr; } inline void operator delete(void*, ImNewDummy, void*) {} // This is only required so we can use the symmetrical new() #define IM_ALLOC(_SIZE) ImGui::MemAlloc(_SIZE) #define IM_FREE(_PTR) ImGui::MemFree(_PTR) #define IM_PLACEMENT_NEW(_PTR) new(ImNewDummy(), _PTR) #define IM_NEW(_TYPE) new(ImNewDummy(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p); } } //----------------------------------------------------------------------------- // Helper: ImVector<> // 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). // 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. // 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. // Important: our implementation does NOT call C++ constructors/destructors, we treat everything as raw data! This is intentional but be extra mindful of that, // 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. //----------------------------------------------------------------------------- template struct ImVector { int Size; int Capacity; T* Data; // Provide standard typedefs but we don't use them ourselves. typedef T value_type; typedef value_type* iterator; typedef const value_type* const_iterator; // Constructors, destructor inline ImVector() { Size = Capacity = 0; Data = NULL; } inline ImVector(const ImVector& src) { Size = Capacity = 0; Data = NULL; operator=(src); } inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; } inline ~ImVector() { if (Data) IM_FREE(Data); } inline bool empty() const { return Size == 0; } inline int size() const { return Size; } inline int size_in_bytes() const { return Size * (int)sizeof(T); } inline int capacity() const { return Capacity; } inline T& operator[](int i) { IM_ASSERT(i < Size); return Data[i]; } inline const T& operator[](int i) const { IM_ASSERT(i < Size); return Data[i]; } inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } inline T* begin() { return Data; } inline const T* begin() const { return Data; } inline T* end() { return Data + Size; } inline const T* end() const { return Data + Size; } inline T& front() { IM_ASSERT(Size > 0); return Data[0]; } inline const T& front() const { IM_ASSERT(Size > 0); return Data[0]; } inline T& back() { IM_ASSERT(Size > 0); return Data[Size - 1]; } inline const T& back() const { IM_ASSERT(Size > 0); return Data[Size - 1]; } inline void swap(ImVector& 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; } inline int _grow_capacity(int sz) const { int new_capacity = Capacity ? (Capacity + Capacity/2) : 8; return new_capacity > sz ? new_capacity : sz; } inline void resize(int new_size) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); Size = new_size; } 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; } 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; } // 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. inline void push_back(const T& v) { if (Size == Capacity) reserve(_grow_capacity(Size + 1)); memcpy(&Data[Size], &v, sizeof(v)); Size++; } inline void pop_back() { IM_ASSERT(Size > 0); Size--; } inline void push_front(const T& v) { if (Size == 0) push_back(v); else insert(Data, v); } 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; } 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; } 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; } 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; } 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; } 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; } }; //----------------------------------------------------------------------------- // ImGuiStyle // You may modify the ImGui::GetStyle() main instance during initialization and before NewFrame(). // During the frame, use ImGui::PushStyleVar(ImGuiStyleVar_XXXX)/PopStyleVar() to alter the main style values, // and ImGui::PushStyleColor(ImGuiCol_XXX)/PopStyleColor() for colors. //----------------------------------------------------------------------------- struct ImGuiStyle { float Alpha; // Global alpha applies to everything in ImGui. ImVec2 WindowPadding; // Padding within a window. float WindowRounding; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. 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). ImVec2 WindowMinSize; // Minimum window size. This is a global setting. If you want to constraint individual windows, use SetNextWindowSizeConstraints(). ImVec2 WindowTitleAlign; // Alignment for title bar text. Defaults to (0.0f,0.5f) for left-aligned,vertically centered. float ChildRounding; // Radius of child window corners rounding. Set to 0.0f to have rectangular windows. 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). float PopupRounding; // Radius of popup window corners rounding. (Note that tooltip windows use WindowRounding) 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). ImVec2 FramePadding; // Padding within a framed rectangle (used by most widgets). float FrameRounding; // Radius of frame corners rounding. Set to 0.0f to have rectangular frame (used by most widgets). 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). ImVec2 ItemSpacing; // Horizontal and vertical spacing between widgets/lines. ImVec2 ItemInnerSpacing; // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label). 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! float IndentSpacing; // Horizontal indentation when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). float ColumnsMinSpacing; // Minimum horizontal spacing between two columns. float ScrollbarSize; // Width of the vertical scrollbar, Height of the horizontal scrollbar. float ScrollbarRounding; // Radius of grab corners for scrollbar. float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar. float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. float TabBorderSize; // Thickness of border around tabs. ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered). ImVec2 SelectableTextAlign; // Alignment of selectable text when selectable is larger than text. Defaults to (0.0f, 0.0f) (top-left aligned). 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. 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! float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. bool AntiAliasedLines; // Enable anti-aliasing on lines/borders. Disable if you are really tight on CPU/GPU. bool AntiAliasedFill; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.) 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. ImVec4 Colors[ImGuiCol_COUNT]; IMGUI_API ImGuiStyle(); IMGUI_API void ScaleAllSizes(float scale_factor); }; //----------------------------------------------------------------------------- // ImGuiIO // Communicate most settings and inputs/outputs to Dear ImGui using this structure. // Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage. //----------------------------------------------------------------------------- struct ImGuiIO { //------------------------------------------------------------------ // Configuration (fill once) // Default value //------------------------------------------------------------------ ImGuiConfigFlags ConfigFlags; // = 0 // See ImGuiConfigFlags_ enum. Set by user/application. Gamepad/keyboard navigation options, etc. 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. ImVec2 DisplaySize; // // Main display size, in pixels. This is for the default viewport. Use BeginViewport() for other viewports. float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. float IniSavingRate; // = 5.0f // Minimum time between saving positions/sizes to .ini file, in seconds. 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. const char* LogFilename; // = "imgui_log.txt"// Path to .log file (default parameter to ImGui::LogToFile when no file is specified). float MouseDoubleClickTime; // = 0.30f // Time for a double-click, in seconds. float MouseDoubleClickMaxDist; // = 6.0f // Distance threshold to stay in to validate a double-click, in pixels. float MouseDragThreshold; // = 6.0f // Distance threshold before considering we are dragging. int KeyMap[ImGuiKey_COUNT]; // // Map of indices into the KeysDown[512] entries array which represent your "native" keyboard state. float KeyRepeatDelay; // = 0.250f // When holding a key/button, time before it starts repeating, in seconds (for buttons in Repeat mode, etc.). float KeyRepeatRate; // = 0.050f // When holding a key/button, rate at which it repeats, in seconds. void* UserData; // = NULL // Store your own data for retrieval by callbacks. ImFontAtlas*Fonts; // // Load, rasterize and pack one or more fonts into a single texture. float FontGlobalScale; // = 1.0f // Global scale all fonts bool FontAllowUserScaling; // = false // Allow user scaling text of individual window with CTRL+Wheel. ImFont* FontDefault; // = NULL // Font to use on NewFrame(). Use NULL to uses Fonts->Fonts[0]. 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. // Docking options (when ImGuiConfigFlags_DockingEnable is set) bool ConfigDockingNoSplit; // = false // Simplified docking mode: disable window splitting, so docking is limited to merging multiple windows together into tab-bars. bool ConfigDockingWithShift; // = false // Enable docking with holding Shift key (reduce visual noise, allows dropping in wider space) bool ConfigDockingTabBarOnSingleWindows; // = false // [BETA] Make every single floating window display within a docking node. 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. // Viewport options (when ImGuiConfigFlags_ViewportsEnable is set) 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. 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. 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). bool ConfigViewportsNoDefaultParent; // = false // Disable default OS parenting to main viewport for secondary viewports. By default, viewports are marked with ParentViewportId = , 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. // Miscellaneous options 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. 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) bool ConfigInputTextCursorBlink; // = true // Set to false to disable blinking cursor, for users who consider it distracting. (was called: io.OptCursorBlink prior to 1.63) 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) 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. //------------------------------------------------------------------ // Platform Functions // (the imgui_impl_xxxx back-end files are setting those up for you) //------------------------------------------------------------------ // 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. const char* BackendPlatformName; // = NULL const char* BackendRendererName; // = NULL void* BackendPlatformUserData; // = NULL void* BackendRendererUserData; // = NULL void* BackendLanguageUserData; // = NULL // Optional: Access OS clipboard // (default to use native Win32 clipboard on Windows, otherwise uses a private clipboard. Override to access OS clipboard on other architectures) const char* (*GetClipboardTextFn)(void* user_data); void (*SetClipboardTextFn)(void* user_data, const char* text); void* ClipboardUserData; #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS // [OBSOLETE since 1.60+] Rendering function, will be automatically called in Render(). Please call your rendering function yourself now! // You can obtain the ImDrawData* by calling ImGui::GetDrawData() after Render(). See example applications if you are unsure of how to implement this. void (*RenderDrawListsFn)(ImDrawData* data); #else // 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. void* RenderDrawListsFnUnused; #endif //------------------------------------------------------------------ // Input - Fill before calling NewFrame() //------------------------------------------------------------------ ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX,-FLT_MAX) if mouse is unavailable (on another screen, etc.) 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. float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text. float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all back-ends. 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). bool KeyCtrl; // Keyboard modifier pressed: Control bool KeyShift; // Keyboard modifier pressed: Shift bool KeyAlt; // Keyboard modifier pressed: Alt bool KeySuper; // Keyboard modifier pressed: Cmd/Super/Windows 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). float NavInputs[ImGuiNavInput_COUNT]; // Gamepad inputs. Cleared back to zero by EndFrame(). Keyboard keys will be auto-mapped and be written here by NewFrame(). // Functions IMGUI_API void AddInputCharacter(ImWchar c); // Queue new character input IMGUI_API void AddInputCharactersUTF8(const char* str); // Queue new characters input from an UTF-8 string IMGUI_API void ClearInputCharacters(); // Clear the text input buffer manually //------------------------------------------------------------------ // Output - Retrieve after calling NewFrame() //------------------------------------------------------------------ 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.). 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.). 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). bool WantSetMousePos; // MousePos has been altered, back-end should reposition mouse on next frame. Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled. 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. 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. bool NavVisible; // Directional navigation is visible and allowed (will handle ImGuiKey_NavXXX events). float Framerate; // Application framerate estimation, in frame per second. Solely for convenience. Rolling average estimation based on IO.DeltaTime over 120 frames int MetricsRenderVertices; // Vertices output during last call to Render() int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3 int MetricsRenderWindows; // Number of visible windows int MetricsActiveWindows; // Number of active windows int MetricsActiveAllocations; // Number of active allocations, updated by MemAlloc/MemFree based on current context. May be off if you have multiple imgui contexts. 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. //------------------------------------------------------------------ // [Internal] ImGui will maintain those fields. Forward compatibility not guaranteed! //------------------------------------------------------------------ ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) ImVec2 MouseClickedPos[5]; // Position at time of clicking double MouseClickedTime[5]; // Time of last click (used to figure out double-click) bool MouseClicked[5]; // Mouse button went from !Down to Down bool MouseDoubleClicked[5]; // Has mouse button been double-clicked? bool MouseReleased[5]; // Mouse button went from Down to !Down 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. float MouseDownDuration[5]; // Duration the mouse button has been down (0.0f == just clicked) float MouseDownDurationPrev[5]; // Previous time the mouse button has been down ImVec2 MouseDragMaxDistanceAbs[5]; // Maximum distance, absolute, on each axis, of how much mouse has traveled from the clicking point float MouseDragMaxDistanceSqr[5]; // Squared maximum distance of how much mouse has traveled from the clicking point float KeysDownDuration[512]; // Duration the keyboard key has been down (0.0f == just pressed) float KeysDownDurationPrev[512]; // Previous duration the key has been down float NavInputsDownDuration[ImGuiNavInput_COUNT]; float NavInputsDownDurationPrev[ImGuiNavInput_COUNT]; ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform back-end). Fill using AddInputCharacter() helper. IMGUI_API ImGuiIO(); }; //----------------------------------------------------------------------------- // Misc data structures //----------------------------------------------------------------------------- // Shared state of InputText(), passed as an argument to your callback when a ImGuiInputTextFlags_Callback* flag is used. // The callback function should return 0 by default. // Callbacks (follow a flag name and see comments in ImGuiInputTextFlags_ declarations for more details) // - ImGuiInputTextFlags_CallbackCompletion: Callback on pressing TAB // - ImGuiInputTextFlags_CallbackHistory: Callback on pressing Up/Down arrows // - ImGuiInputTextFlags_CallbackAlways: Callback on each iteration // - ImGuiInputTextFlags_CallbackCharFilter: Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard. // - ImGuiInputTextFlags_CallbackResize: Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. struct ImGuiInputTextCallbackData { ImGuiInputTextFlags EventFlag; // One ImGuiInputTextFlags_Callback* // Read-only ImGuiInputTextFlags Flags; // What user passed to InputText() // Read-only void* UserData; // What user passed to InputText() // Read-only // Arguments for the different callback events // - 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. // - 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. 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; ImGuiKey EventKey; // Key pressed (Up/Down/TAB) // Read-only // [Completion,History] char* Buf; // Text buffer // Read-write // [Resize] Can replace pointer / [Completion,History,Always] Only write to pointed data, don't replace the actual pointer! 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() 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 bool BufDirty; // Set if you modify Buf/BufTextLen! // Write // [Completion,History,Always] int CursorPos; // // Read-write // [Completion,History,Always] int SelectionStart; // // Read-write // [Completion,History,Always] == to SelectionEnd when no selection) int SelectionEnd; // // Read-write // [Completion,History,Always] // Helper functions for text manipulation. // Use those function to benefit from the CallbackResize behaviors. Calling those function reset the selection. IMGUI_API ImGuiInputTextCallbackData(); IMGUI_API void DeleteChars(int pos, int bytes_count); IMGUI_API void InsertChars(int pos, const char* text, const char* text_end = NULL); bool HasSelection() const { return SelectionStart != SelectionEnd; } }; // Resizing callback data to apply custom constraint. As enabled by SetNextWindowSizeConstraints(). Callback is called during the next Begin(). // NB: For basic min/max size constraint on each axis you don't need to use the callback! The SetNextWindowSizeConstraints() parameters are enough. struct ImGuiSizeCallbackData { void* UserData; // Read-only. What user passed to SetNextWindowSizeConstraints() ImVec2 Pos; // Read-only. Window position, for reference. ImVec2 CurrentSize; // Read-only. Current window size. ImVec2 DesiredSize; // Read-write. Desired size, based on user's mouse position. Write to this field to restrain resizing. }; // Data payload for Drag and Drop operations: AcceptDragDropPayload(), GetDragDropPayload() struct ImGuiPayload { // Members void* Data; // Data (copied and owned by dear imgui) int DataSize; // Data size // [Internal] ImGuiID SourceId; // Source item id ImGuiID SourceParentId; // Source parent id (if available) int DataFrameCount; // Data timestamp char DataType[32+1]; // Data type tag (short user-supplied string, 32 characters max) bool Preview; // Set when AcceptDragDropPayload() was called and mouse has been hovering the target item (nb: handle overlapping drag targets) bool Delivery; // Set when AcceptDragDropPayload() was called and mouse button is released over the target item. ImGuiPayload() { Clear(); } void Clear() { SourceId = SourceParentId = 0; Data = NULL; DataSize = 0; memset(DataType, 0, sizeof(DataType)); DataFrameCount = -1; Preview = Delivery = false; } bool IsDataType(const char* type) const { return DataFrameCount != -1 && strcmp(type, DataType) == 0; } bool IsPreview() const { return Preview; } bool IsDelivery() const { return Delivery; } }; // [BETA] Rarely used / very advanced uses only. Use with SetNextWindowClass() and DockSpace() functions. // 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. struct ImGuiWindowClass { ImGuiID ClassId; // User data. 0 = Default class (unclassed) 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. 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. ImGuiViewportFlags ViewportFlagsOverrideValue; // Viewport flags values to override when a window of this class owns a viewport. bool DockingAllowUnclassed; // true = can be docked/merged with an unclassed window ImGuiWindowClass() { ClassId = 0; ParentViewportId = 0; ViewportFlagsOverrideMask = ViewportFlagsOverrideValue = 0x00; DockingAllowUnclassed = true; } }; //----------------------------------------------------------------------------- // Obsolete functions (Will be removed! Read 'API BREAKING CHANGES' section in imgui.cpp for details) // Please keep your copy of dear imgui up to date! Occasionally set '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in imconfig.h to stay ahead. //----------------------------------------------------------------------------- #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { // OBSOLETED in 1.69 (from Mar 2019) static inline ImDrawList* GetOverlayDrawList() { return GetForegroundDrawList(); } // OBSOLETED in 1.66 (from Sep 2018) static inline void SetScrollHere(float center_ratio=0.5f){ SetScrollHereY(center_ratio); } // OBSOLETED in 1.63 (between Aug 2018 and Sept 2018) static inline bool IsItemDeactivatedAfterChange() { return IsItemDeactivatedAfterEdit(); } // OBSOLETED in 1.61 (between Apr 2018 and Aug 2018) 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'! IMGUI_API bool InputFloat2(const char* label, float v[2], int decimal_precision, ImGuiInputTextFlags flags = 0); IMGUI_API bool InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags flags = 0); IMGUI_API bool InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags flags = 0); // OBSOLETED in 1.60 (between Dec 2017 and Apr 2018) static inline bool IsAnyWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_AnyWindow); } static inline bool IsAnyWindowHovered() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } 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; } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) static inline void ShowTestWindow() { return ShowDemoWindow(); } static inline bool IsRootWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootWindow); } static inline bool IsRootWindowOrAnyChildFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows); } static inline void SetNextWindowContentWidth(float w) { SetNextWindowContentSize(ImVec2(w, 0.0f)); } static inline float GetItemsLineHeightWithSpacing() { return GetFrameHeightWithSpacing(); } // OBSOLETED in 1.52 (between Aug 2017 and Oct 2017) 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. static inline bool IsRootWindowOrAnyChildHovered() { return IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows); } static inline void AlignFirstTextHeightToWidgets() { AlignTextToFramePadding(); } void SetNextWindowPosCenter(ImGuiCond cond); // OBSOLETED in 1.51 (between Jun 2017 and Aug 2017) static inline bool IsItemHoveredRect() { return IsItemHovered(ImGuiHoveredFlags_RectOnly); } 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. static inline bool IsMouseHoveringAnyWindow() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } static inline bool IsMouseHoveringWindow() { return IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem); } } typedef ImGuiInputTextCallback ImGuiTextEditCallback; // OBSOLETE in 1.63 (from Aug 2018): made the names consistent typedef ImGuiInputTextCallbackData ImGuiTextEditCallbackData; #endif //----------------------------------------------------------------------------- // Helpers //----------------------------------------------------------------------------- // 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. // Usage: static ImGuiOnceUponAFrame oaf; if (oaf) ImGui::Text("This will be called only once per frame"); struct ImGuiOnceUponAFrame { ImGuiOnceUponAFrame() { RefFrame = -1; } mutable int RefFrame; operator bool() const { int current_frame = ImGui::GetFrameCount(); if (RefFrame == current_frame) return false; RefFrame = current_frame; return true; } }; // 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. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS #define IMGUI_ONCE_UPON_A_FRAME static ImGuiOnceUponAFrame imgui_oaf; if (imgui_oaf) // OBSOLETED in 1.51, will remove! #endif // Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]" struct ImGuiTextFilter { IMGUI_API ImGuiTextFilter(const char* default_filter = ""); IMGUI_API bool Draw(const char* label = "Filter (inc,-exc)", float width = 0.0f); // Helper calling InputText+Build IMGUI_API bool PassFilter(const char* text, const char* text_end = NULL) const; IMGUI_API void Build(); void Clear() { InputBuf[0] = 0; Build(); } bool IsActive() const { return !Filters.empty(); } // [Internal] struct TextRange { const char* b; const char* e; TextRange() { b = e = NULL; } TextRange(const char* _b, const char* _e) { b = _b; e = _e; } const char* begin() const { return b; } const char* end () const { return e; } bool empty() const { return b == e; } IMGUI_API void split(char separator, ImVector* out) const; }; char InputBuf[256]; ImVector Filters; int CountGrep; }; // Helper: Growable text buffer for logging/accumulating text // (this could be called 'ImGuiTextBuilder' / 'ImGuiStringBuilder') struct ImGuiTextBuffer { ImVector Buf; static char EmptyString[1]; ImGuiTextBuffer() { } inline char operator[](int i) { IM_ASSERT(Buf.Data != NULL); return Buf.Data[i]; } const char* begin() const { return Buf.Data ? &Buf.front() : EmptyString; } const char* end() const { return Buf.Data ? &Buf.back() : EmptyString; } // Buf is zero-terminated, so end() will point on the zero-terminator int size() const { return Buf.Size ? Buf.Size - 1 : 0; } bool empty() { return Buf.Size <= 1; } void clear() { Buf.clear(); } void reserve(int capacity) { Buf.reserve(capacity); } const char* c_str() const { return Buf.Data ? Buf.Data : EmptyString; } IMGUI_API void append(const char* str, const char* str_end = NULL); IMGUI_API void appendf(const char* fmt, ...) IM_FMTARGS(2); IMGUI_API void appendfv(const char* fmt, va_list args) IM_FMTLIST(2); }; // Helper: Key->Value storage // Typically you don't have to worry about this since a storage is held within each Window. // We use it to e.g. store collapse state for a tree (Int 0/1) // This is optimized for efficient lookup (dichotomy into a contiguous buffer) and rare insertion (typically tied to user interactions aka max once a frame) // You can use it as custom user storage for temporary values. Declare your own storage if, for example: // - 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). // - You want to store custom debug data easily without adding or editing structures in your code (probably not efficient, but convenient) // Types are NOT stored, so it is up to you to make sure your Key don't collide with different types. struct ImGuiStorage { struct Pair { ImGuiID key; union { int val_i; float val_f; void* val_p; }; Pair(ImGuiID _key, int _val_i) { key = _key; val_i = _val_i; } Pair(ImGuiID _key, float _val_f) { key = _key; val_f = _val_f; } Pair(ImGuiID _key, void* _val_p) { key = _key; val_p = _val_p; } }; ImVector Data; // - Get***() functions find pair, never add/allocate. Pairs are sorted so a query is O(log N) // - Set***() functions find pair, insertion on demand if missing. // - Sorted insertion is costly, paid once. A typical frame shouldn't need to insert any new pair. void Clear() { Data.clear(); } IMGUI_API int GetInt(ImGuiID key, int default_val = 0) const; IMGUI_API void SetInt(ImGuiID key, int val); IMGUI_API bool GetBool(ImGuiID key, bool default_val = false) const; IMGUI_API void SetBool(ImGuiID key, bool val); IMGUI_API float GetFloat(ImGuiID key, float default_val = 0.0f) const; IMGUI_API void SetFloat(ImGuiID key, float val); IMGUI_API void* GetVoidPtr(ImGuiID key) const; // default_val is NULL IMGUI_API void SetVoidPtr(ImGuiID key, void* val); // - Get***Ref() functions finds pair, insert on demand if missing, return pointer. Useful if you intend to do Get+Set. // - 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. // - 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) // float* pvar = ImGui::GetFloatRef(key); ImGui::SliderFloat("var", pvar, 0, 100.0f); some_var += *pvar; IMGUI_API int* GetIntRef(ImGuiID key, int default_val = 0); IMGUI_API bool* GetBoolRef(ImGuiID key, bool default_val = false); IMGUI_API float* GetFloatRef(ImGuiID key, float default_val = 0.0f); IMGUI_API void** GetVoidPtrRef(ImGuiID key, void* default_val = NULL); // Use on your own storage if you know only integer are being stored (open/close all tree nodes) IMGUI_API void SetAllInt(int val); // For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once. IMGUI_API void BuildSortByKey(); }; // Helper: Manually clip large list of items. // 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. // The clipper calculates the range of visible items and advance the cursor to compensate for the non-visible items we have skipped. // 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. // Usage: // ImGuiListClipper clipper(1000); // we have 1000 elements, evenly spaced. // while (clipper.Step()) // for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) // ImGui::Text("line number %d", i); // - 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). // - 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. // - (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.) // - 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. struct ImGuiListClipper { float StartPosY; float ItemsHeight; int ItemsCount, StepNo, DisplayStart, DisplayEnd; // 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). // items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing(). // 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(). 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). ~ImGuiListClipper() { IM_ASSERT(ItemsCount == -1); } // Assert if user forgot to call End() or Step() until false. IMGUI_API bool Step(); // Call until it returns false. The DisplayStart/DisplayEnd fields will be set and you can process/draw those items. 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. IMGUI_API void End(); // Automatically called on the last call of Step() that returns false. }; // Helpers macros to generate 32-bits encoded colors #ifdef IMGUI_USE_BGRA_PACKED_COLOR #define IM_COL32_R_SHIFT 16 #define IM_COL32_G_SHIFT 8 #define IM_COL32_B_SHIFT 0 #define IM_COL32_A_SHIFT 24 #define IM_COL32_A_MASK 0xFF000000 #else #define IM_COL32_R_SHIFT 0 #define IM_COL32_G_SHIFT 8 #define IM_COL32_B_SHIFT 16 #define IM_COL32_A_SHIFT 24 #define IM_COL32_A_MASK 0xFF000000 #endif #define IM_COL32(R,G,B,A) (((ImU32)(A)<>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; } ImColor(float r, float g, float b, float a = 1.0f) { Value.x = r; Value.y = g; Value.z = b; Value.w = a; } ImColor(const ImVec4& col) { Value = col; } inline operator ImU32() const { return ImGui::ColorConvertFloat4ToU32(Value); } inline operator ImVec4() const { return Value; } // FIXME-OBSOLETE: May need to obsolete/cleanup those helpers. 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; } 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); } }; //----------------------------------------------------------------------------- // Draw List API (ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListFlags, ImDrawList, ImDrawData) // Hold a series of drawing commands. The user provides a renderer for ImDrawData which essentially contains an array of ImDrawList. //----------------------------------------------------------------------------- // Draw callbacks for advanced uses. // NB: You most likely do NOT need to use draw callbacks just to create your own widget or customized UI rendering, // you can poke into the draw list for that! Draw callback may be useful for example to: A) Change your GPU render state, // B) render a complex 3D scene inside a UI element without an intermediate texture/render target, etc. // The expected behavior from your rendering function is 'if (cmd.UserCallback != NULL) { cmd.UserCallback(parent_list, cmd); } else { RenderTriangles() }' typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* cmd); // Typically, 1 command = 1 GPU draw call (unless command is a callback) struct ImDrawCmd { 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[]. ImVec4 ClipRect; // Clipping rectangle (x1, y1, x2, y2). Subtract ImDrawData->DisplayPos to get clipping rectangle in "viewport" coordinates 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. ImDrawCallback UserCallback; // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally. void* UserCallbackData; // The draw callback code can access this. ImDrawCmd() { ElemCount = 0; ClipRect.x = ClipRect.y = ClipRect.z = ClipRect.w = 0.0f; TextureId = (ImTextureID)NULL; UserCallback = NULL; UserCallbackData = NULL; } }; // Vertex index (override with '#define ImDrawIdx unsigned int' in imconfig.h) #ifndef ImDrawIdx typedef unsigned short ImDrawIdx; #endif // Vertex layout #ifndef IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT struct ImDrawVert { ImVec2 pos; ImVec2 uv; ImU32 col; }; #else // You can override the vertex format layout by defining IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT in imconfig.h // 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. // The type has to be described within the macro (you can either declare the struct or use a typedef) // 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. IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT; #endif // 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. // You can also use them to simulate drawing layers and submit primitives in a different order than how they will be rendered. struct ImDrawChannel { ImVector CmdBuffer; ImVector IdxBuffer; }; enum ImDrawCornerFlags_ { ImDrawCornerFlags_TopLeft = 1 << 0, // 0x1 ImDrawCornerFlags_TopRight = 1 << 1, // 0x2 ImDrawCornerFlags_BotLeft = 1 << 2, // 0x4 ImDrawCornerFlags_BotRight = 1 << 3, // 0x8 ImDrawCornerFlags_Top = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_TopRight, // 0x3 ImDrawCornerFlags_Bot = ImDrawCornerFlags_BotLeft | ImDrawCornerFlags_BotRight, // 0xC ImDrawCornerFlags_Left = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft, // 0x5 ImDrawCornerFlags_Right = ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight, // 0xA ImDrawCornerFlags_All = 0xF // In your function calls you may use ~0 (= all bits sets) instead of ImDrawCornerFlags_All, as a convenience }; enum ImDrawListFlags_ { ImDrawListFlags_None = 0, ImDrawListFlags_AntiAliasedLines = 1 << 0, // Lines are anti-aliased (*2 the number of triangles for 1.0f wide line, otherwise *3 the number of triangles) ImDrawListFlags_AntiAliasedFill = 1 << 1 // Filled shapes have anti-aliased edges (*2 the number of vertices) }; // Draw command list // 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. // Each ImGui window contains its own ImDrawList. You can use ImGui::GetWindowDrawList() to access the current window draw list and draw custom primitives. // You can interleave normal ImGui:: calls and adding primitives to the current draw list. // 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) // 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. struct ImDrawList { // This is what you have to render ImVector CmdBuffer; // Draw commands. Typically 1 command = 1 GPU draw call, unless the command is a callback. ImVector IdxBuffer; // Index buffer. Each command consume ImDrawCmd::ElemCount of those ImVector VtxBuffer; // Vertex buffer. ImDrawListFlags Flags; // Flags, you may poke into these to adjust anti-aliasing settings per-primitive. // [Internal, used while building lists] const ImDrawListSharedData* _Data; // Pointer to shared draw data (you can use ImGui::GetDrawListSharedData() to get the one from current ImGui context) const char* _OwnerName; // Pointer to owner window's name for debugging unsigned int _VtxCurrentIdx; // [Internal] == VtxBuffer.Size ImDrawVert* _VtxWritePtr; // [Internal] point within VtxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) ImDrawIdx* _IdxWritePtr; // [Internal] point within IdxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) ImVector _ClipRectStack; // [Internal] ImVector _TextureIdStack; // [Internal] ImVector _Path; // [Internal] current path building int _ChannelsCurrent; // [Internal] current channel number (0) int _ChannelsCount; // [Internal] number of active channels (1+) ImVector _Channels; // [Internal] draw channels for columns API (not resized down so _ChannelsCount may be smaller than _Channels.Size) // 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) ImDrawList(const ImDrawListSharedData* shared_data) { _Data = shared_data; _OwnerName = NULL; Clear(); } ~ImDrawList() { ClearFreeMemory(); } 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) IMGUI_API void PushClipRectFullScreen(); IMGUI_API void PopClipRect(); IMGUI_API void PushTextureID(ImTextureID texture_id); IMGUI_API void PopTextureID(); inline ImVec2 GetClipRectMin() const { const ImVec4& cr = _ClipRectStack.back(); return ImVec2(cr.x, cr.y); } inline ImVec2 GetClipRectMax() const { const ImVec4& cr = _ClipRectStack.back(); return ImVec2(cr.z, cr.w); } // Primitives IMGUI_API void AddLine(const ImVec2& a, const ImVec2& b, ImU32 col, float thickness = 1.0f); 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 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) 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); IMGUI_API void AddQuad(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, ImU32 col, float thickness = 1.0f); IMGUI_API void AddQuadFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, ImU32 col); IMGUI_API void AddTriangle(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col, float thickness = 1.0f); IMGUI_API void AddTriangleFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col); IMGUI_API void AddCircle(const ImVec2& centre, float radius, ImU32 col, int num_segments = 12, float thickness = 1.0f); IMGUI_API void AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, int num_segments = 12); IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL); 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); 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); 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); 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); IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, bool closed, float thickness); IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); // Note: Anti-aliased filling requires points to be in clockwise order. IMGUI_API void AddBezierCurve(const ImVec2& pos0, const ImVec2& cp0, const ImVec2& cp1, const ImVec2& pos1, ImU32 col, float thickness, int num_segments = 0); // Stateful path API, add points then finish with PathFillConvex() or PathStroke() inline void PathClear() { _Path.Size = 0; } inline void PathLineTo(const ImVec2& pos) { _Path.push_back(pos); } inline void PathLineToMergeDuplicate(const ImVec2& pos) { if (_Path.Size == 0 || memcmp(&_Path.Data[_Path.Size-1], &pos, 8) != 0) _Path.push_back(pos); } 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. inline void PathStroke(ImU32 col, bool closed, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, closed, thickness); _Path.Size = 0; } IMGUI_API void PathArcTo(const ImVec2& centre, float radius, float a_min, float a_max, int num_segments = 10); 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 IMGUI_API void PathBezierCurveTo(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, int num_segments = 0); IMGUI_API void PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, int rounding_corners_flags = ImDrawCornerFlags_All); // Channels // - Use to simulate layers. By switching channels to can render out-of-order (e.g. submit foreground primitives before background primitives) // - 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) IMGUI_API void ChannelsSplit(int channels_count); IMGUI_API void ChannelsMerge(); IMGUI_API void ChannelsSetCurrent(int channel_index); // Advanced 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. 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 IMGUI_API ImDrawList* CloneOutput() const; // Create a clone of the CmdBuffer/IdxBuffer/VtxBuffer. // Internal helpers // NB: all primitives needs to be reserved via PrimReserve() beforehand! IMGUI_API void Clear(); IMGUI_API void ClearFreeMemory(); IMGUI_API void PrimReserve(int idx_count, int vtx_count); IMGUI_API void PrimRect(const ImVec2& a, const ImVec2& b, ImU32 col); // Axis aligned rectangle (composed of two triangles) IMGUI_API void PrimRectUV(const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, ImU32 col); 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); inline void PrimWriteVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col){ _VtxWritePtr->pos = pos; _VtxWritePtr->uv = uv; _VtxWritePtr->col = col; _VtxWritePtr++; _VtxCurrentIdx++; } inline void PrimWriteIdx(ImDrawIdx idx) { *_IdxWritePtr = idx; _IdxWritePtr++; } inline void PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); } IMGUI_API void UpdateClipRect(); IMGUI_API void UpdateTextureID(); }; // All draw data to render an ImGui frame // (NB: the style and the naming convention here is a little inconsistent, we currently preserve them for backward compatibility purpose, // as this is one of the oldest structure exposed by the library! Basically, ImDrawList == CmdList) struct ImDrawData { bool Valid; // Only valid after Render() is called and before the next NewFrame() is called. ImDrawList** CmdLists; // Array of ImDrawList* to render. The ImDrawList are owned by ImGuiContext and only pointed to from here. int CmdListsCount; // Number of ImDrawList* to render int TotalIdxCount; // For convenience, sum of all ImDrawList's IdxBuffer.Size int TotalVtxCount; // For convenience, sum of all ImDrawList's VtxBuffer.Size ImVec2 DisplayPos; // Upper-left position of the viewport to render (== upper-left of the orthogonal projection matrix to use) 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) 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. ImGuiViewport* OwnerViewport; // Viewport carrying the ImDrawData instance, might be of use to the renderer (generally not). // Functions ImDrawData() { Valid = false; Clear(); } ~ImDrawData() { Clear(); } 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! 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! 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. }; //----------------------------------------------------------------------------- // Font API (ImFontConfig, ImFontGlyph, ImFontAtlasFlags, ImFontAtlas, ImFontGlyphRangesBuilder, ImFont) //----------------------------------------------------------------------------- struct ImFontConfig { void* FontData; // // TTF/OTF data int FontDataSize; // // TTF/OTF data size bool FontDataOwnedByAtlas; // true // TTF/OTF data ownership taken by the container ImFontAtlas (will delete memory itself). int FontNo; // 0 // Index of font within TTF/OTF file float SizePixels; // // Size in pixels for rasterizer (more or less maps to the resulting font height). 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. int OversampleV; // 1 // Rasterize at higher quality for sub-pixel positioning. We don't use sub-pixel positions on the Y axis. 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. ImVec2 GlyphExtraSpacing; // 0, 0 // Extra spacing (in pixels) between glyphs. Only X axis is supported for now. ImVec2 GlyphOffset; // 0, 0 // Offset all glyphs from this font input. 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. float GlyphMinAdvanceX; // 0 // Minimum AdvanceX for glyphs, set Min to align font icons, set both Min/Max to enforce mono-space font float GlyphMaxAdvanceX; // FLT_MAX // Maximum AdvanceX for glyphs 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. unsigned int RasterizerFlags; // 0x00 // Settings for custom font rasterizer (e.g. ImGuiFreeType). Leave as zero if you aren't using one. 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. // [Internal] char Name[40]; // Name (strictly to ease debugging) ImFont* DstFont; IMGUI_API ImFontConfig(); }; struct ImFontGlyph { ImWchar Codepoint; // 0x0000..0xFFFF float AdvanceX; // Distance to next character (= data from font + ImFontConfig::GlyphExtraSpacing.x baked in) float X0, Y0, X1, Y1; // Glyph corners float U0, V0, U1, V1; // Texture coordinates }; // Helper to build glyph ranges from text/string data. Feed your application strings/characters to it then call BuildRanges(). // This is essentially a tightly packed of vector of 64k booleans = 8KB storage. struct ImFontGlyphRangesBuilder { ImVector UsedChars; // Store 1-bit per Unicode code point (0=unused, 1=used) ImFontGlyphRangesBuilder() { UsedChars.resize(0x10000 / sizeof(int)); memset(UsedChars.Data, 0, 0x10000 / sizeof(int)); } 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 void SetBit(int n) { int off = (n >> 5); int mask = 1 << (n & 31); UsedChars[off] |= mask; } // Set bit n in the array void AddChar(ImWchar c) { SetBit(c); } // Add character IMGUI_API void AddText(const char* text, const char* text_end = NULL); // Add string (each character of the UTF-8 string are added) IMGUI_API void AddRanges(const ImWchar* ranges); // Add ranges, e.g. builder.AddRanges(ImFontAtlas::GetGlyphRangesDefault()) to force add all of ASCII/Latin+Ext IMGUI_API void BuildRanges(ImVector* out_ranges); // Output new ranges }; enum ImFontAtlasFlags_ { ImFontAtlasFlags_None = 0, ImFontAtlasFlags_NoPowerOfTwoHeight = 1 << 0, // Don't round the height to next power of two ImFontAtlasFlags_NoMouseCursors = 1 << 1 // Don't build software mouse cursors into the atlas }; // Load and rasterize multiple TTF/OTF fonts into a same texture. The font atlas will build a single texture holding: // - One or more fonts. // - Custom graphics data needed to render the shapes needed by Dear ImGui. // - Mouse cursor shapes for software cursor rendering (unless setting 'Flags |= ImFontAtlasFlags_NoMouseCursors' in the font atlas). // It is the user-code responsibility to setup/build the atlas, then upload the pixel data into a texture accessible by your graphics api. // - 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. // - Call GetTexDataAsAlpha8() or GetTexDataAsRGBA32() to build and retrieve pixels data. // - Upload the pixels data into a texture within your graphics system (see imgui_impl_xxxx.cpp examples) // - Call SetTexID(my_tex_id); and pass the pointer/identifier to your texture in a format natural to your graphics API. // This value will be passed back to you during rendering to identify the texture. Read FAQ entry about ImTextureID for more details. // Common pitfalls: // - If you pass a 'glyph_ranges' array to AddFont*** functions, you need to make sure that your array persist up until the // atlas is build (when calling GetTexData*** or Build()). We only copy the pointer, not the data. // - Important: By default, AddFontFromMemoryTTF() takes ownership of the data. Even though we are not writing to it, we will free the pointer on destruction. // You can set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed, // - Even though many functions are suffixed with "TTF", OTF data is supported just as well. // - This is an old API and it is currently awkward for those and and various other reasons! We will address them in the future! struct ImFontAtlas { IMGUI_API ImFontAtlas(); IMGUI_API ~ImFontAtlas(); IMGUI_API ImFont* AddFont(const ImFontConfig* font_cfg); IMGUI_API ImFont* AddFontDefault(const ImFontConfig* font_cfg = NULL); IMGUI_API ImFont* AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); 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. 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. 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. 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. IMGUI_API void ClearTexData(); // Clear output texture data (CPU side). Saves RAM once the texture has been copied to graphics memory. IMGUI_API void ClearFonts(); // Clear output font data (glyphs storage, UV coordinates). IMGUI_API void Clear(); // Clear all input and output. // Build atlas, retrieve pixel data. // 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(). // The pitch is always = Width * BytesPerPixels (1 or 4) // Building in RGBA32 format is provided for convenience and compatibility, but note that unless you manually manipulate or copy color data into // the texture (e.g. when using the AddCustomRect*** api), then the RGB pixels emitted will always be white (~75% of memory/bandwidth waste. IMGUI_API bool Build(); // Build pixels data. This is called automatically for you by the GetTexData*** functions. IMGUI_API void GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel IMGUI_API void GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel bool IsBuilt() { return Fonts.Size > 0 && (TexPixelsAlpha8 != NULL || TexPixelsRGBA32 != NULL); } void SetTexID(ImTextureID id) { TexID = id; } //------------------------------------------- // Glyph Ranges //------------------------------------------- // Helpers to retrieve list of common Unicode ranges (2 value per range, values are inclusive, zero-terminated list) // 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. // NB: Consider using ImFontGlyphRangesBuilder to build glyph ranges from textual data. IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin IMGUI_API const ImWchar* GetGlyphRangesKorean(); // Default + Korean characters IMGUI_API const ImWchar* GetGlyphRangesJapanese(); // Default + Hiragana, Katakana, Half-Width, Selection of 1946 Ideographs IMGUI_API const ImWchar* GetGlyphRangesChineseFull(); // Default + Half-Width + Japanese Hiragana/Katakana + full set of about 21000 CJK Unified Ideographs IMGUI_API const ImWchar* GetGlyphRangesChineseSimplifiedCommon();// Default + Half-Width + Japanese Hiragana/Katakana + set of 2500 CJK Unified Ideographs for common simplified Chinese IMGUI_API const ImWchar* GetGlyphRangesCyrillic(); // Default + about 400 Cyrillic characters IMGUI_API const ImWchar* GetGlyphRangesThai(); // Default + Thai characters IMGUI_API const ImWchar* GetGlyphRangesVietnamese(); // Default + Vietname characters //------------------------------------------- // Custom Rectangles/Glyphs API //------------------------------------------- // 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. // 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. struct CustomRect { unsigned int ID; // Input // User ID. Use <0x10000 to map into a font glyph, >=0x10000 for other/internal/custom texture data. unsigned short Width, Height; // Input // Desired rectangle dimension unsigned short X, Y; // Output // Packed position in Atlas float GlyphAdvanceX; // Input // For custom font glyphs only (ID<0x10000): glyph xadvance ImVec2 GlyphOffset; // Input // For custom font glyphs only (ID<0x10000): glyph display offset ImFont* Font; // Input // For custom font glyphs only (ID<0x10000): target font CustomRect() { ID = 0xFFFFFFFF; Width = Height = 0; X = Y = 0xFFFF; GlyphAdvanceX = 0.0f; GlyphOffset = ImVec2(0,0); Font = NULL; } bool IsPacked() const { return X != 0xFFFF; } }; IMGUI_API int AddCustomRectRegular(unsigned int id, int width, int height); // Id needs to be >= 0x10000. Id >= 0x80000000 are reserved for ImGui and ImDrawList 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. const CustomRect* GetCustomRectByIndex(int index) const { if (index < 0) return NULL; return &CustomRects[index]; } // [Internal] IMGUI_API void CalcCustomRectUV(const CustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max); IMGUI_API bool GetMouseCursorTexData(ImGuiMouseCursor cursor, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2]); //------------------------------------------- // Members //------------------------------------------- bool Locked; // Marked as Locked by ImGui::NewFrame() so attempt to modify the atlas will assert. ImFontAtlasFlags Flags; // Build flags (see ImFontAtlasFlags_) 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. 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. 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. // [Internal] // NB: Access texture data via GetTexData*() calls! Which will setup a default font for you. unsigned char* TexPixelsAlpha8; // 1 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight unsigned int* TexPixelsRGBA32; // 4 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight * 4 int TexWidth; // Texture width calculated during Build(). int TexHeight; // Texture height calculated during Build(). ImVec2 TexUvScale; // = (1.0f/TexWidth, 1.0f/TexHeight) ImVec2 TexUvWhitePixel; // Texture coordinates to a white pixel ImVector 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. ImVector CustomRects; // Rectangles for packing custom texture data into the atlas. ImVector ConfigData; // Internal data int CustomRectIds[1]; // Identifiers of custom texture rectangle used by ImFontAtlas/ImDrawList #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS typedef ImFontGlyphRangesBuilder GlyphRangesBuilder; // OBSOLETE 1.67+ #endif }; // Font runtime data and rendering // ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32(). struct ImFont { // Members: Hot ~20/24 bytes (for CalcTextSize) ImVector 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). float FallbackAdvanceX; // 4 // out // = FallbackGlyph->AdvanceX float FontSize; // 4 // in // // Height of characters/line, set during loading (don't change after loading) // Members: Hot ~36/48 bytes (for CalcTextSize + render loop) ImVector IndexLookup; // 12-16 // out // // Sparse. Index glyphs by Unicode code-point. ImVector Glyphs; // 12-16 // out // // All glyphs. const ImFontGlyph* FallbackGlyph; // 4-8 // out // = FindGlyph(FontFallbackChar) ImVec2 DisplayOffset; // 8 // in // = (0,0) // Offset font rendering by xx pixels // Members: Cold ~32/40 bytes ImFontAtlas* ContainerAtlas; // 4-8 // out // // What we has been loaded into const ImFontConfig* ConfigData; // 4-8 // in // // Pointer within ContainerAtlas->ConfigData short ConfigDataCount; // 2 // in // ~ 1 // Number of ImFontConfig involved in creating this font. Bigger than 1 when merging multiple font sources into one ImFont. ImWchar FallbackChar; // 2 // in // = '?' // Replacement glyph if one isn't found. Only set via SetFallbackChar() float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale() float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] 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) bool DirtyLookupTables; // 1 // out // // Methods IMGUI_API ImFont(); IMGUI_API ~ImFont(); IMGUI_API const ImFontGlyph*FindGlyph(ImWchar c) const; IMGUI_API const ImFontGlyph*FindGlyphNoFallback(ImWchar c) const; float GetCharAdvance(ImWchar c) const { return ((int)c < IndexAdvanceX.Size) ? IndexAdvanceX[(int)c] : FallbackAdvanceX; } bool IsLoaded() const { return ContainerAtlas != NULL; } const char* GetDebugName() const { return ConfigData ? ConfigData->Name : ""; } // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable. // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable. 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 IMGUI_API const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const; IMGUI_API void RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const; 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; // [Internal] Don't use! IMGUI_API void BuildLookupTable(); IMGUI_API void ClearOutputData(); IMGUI_API void GrowIndex(int new_size); 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); 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. IMGUI_API void SetFallbackChar(ImWchar c); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS typedef ImFontGlyph Glyph; // OBSOLETE 1.52+ #endif }; //----------------------------------------------------------------------------- // [BETA] Platform interface for multi-viewport support // - completely optional, for advanced users! // - this is used for back-ends aiming to support the seamless creation of multiple viewport (= multiple Platform/OS windows) // dear imgui manages the viewports, and the back-end create one Platform/OS windows for each secondary viewport. // - if you are new to dear imgui and trying to integrate it into your engine, you should probably ignore this for now. //----------------------------------------------------------------------------- // (Optional) This is required when enabling multi-viewport. Represent the bounds of each connected monitor/display and their DPI. // We use this information for multiple DPI support + clamping the position of popups and tooltips so they don't straddle multiple monitors. struct ImGuiPlatformMonitor { ImVec2 MainPos, MainSize; // Coordinates of the area displayed on this monitor (Min = upper left, Max = bottom right) ImVec2 WorkPos, WorkSize; // (Optional) Coordinates without task bars / side bars / menu bars. imgui uses this to avoid positioning popups/tooltips inside this region. float DpiScale; // 1.0f = 96 DPI ImGuiPlatformMonitor() { MainPos = MainSize = WorkPos = WorkSize = ImVec2(0,0); DpiScale = 1.0f; } }; // (Optional) Setup required only if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) is enabled. // Access via ImGui::GetPlatformIO(). This is designed so we can mix and match two imgui_impl_xxxx files, // one for the Platform (~window handling), one for Renderer. Custom engine back-ends will often provide // both Platform and Renderer interfaces and so may not need to use all functions. // Platform functions are typically called before their Renderer counterpart, // apart from Destroy which are called the other way. // RenderPlatformWindowsDefault() is that helper that iterate secondary viewports and call, in this order: // Platform_RenderWindow(), Renderer_RenderWindow(), Platform_SwapBuffers(), Renderer_SwapBuffers() // You may skip using RenderPlatformWindowsDefault() and call your draw/swap functions yourself if you need // specific behavior for your multi-window rendering. struct ImGuiPlatformIO { //------------------------------------------------------------------ // Input - Back-end interface/functions + Monitor List //------------------------------------------------------------------ // (Optional) Platform functions (e.g. Win32, GLFW, SDL2) // Most of them are called by ImGui::UpdatePlatformWindows() and ImGui::RenderPlatformWindowsDefault(). void (*Platform_CreateWindow)(ImGuiViewport* vp); // Create a new platform window for the given viewport void (*Platform_DestroyWindow)(ImGuiViewport* vp); void (*Platform_ShowWindow)(ImGuiViewport* vp); // Newly created windows are initially hidden so SetWindowPos/Size/Title can be called on them first void (*Platform_SetWindowPos)(ImGuiViewport* vp, ImVec2 pos); ImVec2 (*Platform_GetWindowPos)(ImGuiViewport* vp); void (*Platform_SetWindowSize)(ImGuiViewport* vp, ImVec2 size); ImVec2 (*Platform_GetWindowSize)(ImGuiViewport* vp); void (*Platform_SetWindowFocus)(ImGuiViewport* vp); // Move window to front and set input focus bool (*Platform_GetWindowFocus)(ImGuiViewport* vp); bool (*Platform_GetWindowMinimized)(ImGuiViewport* vp); void (*Platform_SetWindowTitle)(ImGuiViewport* vp, const char* title); void (*Platform_SetWindowAlpha)(ImGuiViewport* vp, float alpha); // (Optional) Setup window transparency void (*Platform_UpdateWindow)(ImGuiViewport* vp); // (Optional) Called in UpdatePlatforms(). Optional hook to allow the platform back-end from doing general book-keeping every frame. void (*Platform_RenderWindow)(ImGuiViewport* vp, void* render_arg); // (Optional) Setup for render void (*Platform_SwapBuffers)(ImGuiViewport* vp, void* render_arg); // (Optional) Call Present/SwapBuffers (platform side) float (*Platform_GetWindowDpiScale)(ImGuiViewport* vp); // (Optional) [BETA] (FIXME-DPI) DPI handling: Return DPI scale for this viewport. 1.0f = 96 DPI. 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. 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. int (*Platform_CreateVkSurface)(ImGuiViewport* vp, ImU64 vk_inst, const void* vk_allocators, ImU64* out_vk_surface); // (Optional) For Renderer to call into Platform code // (Optional) Renderer functions (e.g. DirectX, OpenGL3, Vulkan) void (*Renderer_CreateWindow)(ImGuiViewport* vp); // Create swap chains, frame buffers etc. void (*Renderer_DestroyWindow)(ImGuiViewport* vp); void (*Renderer_SetWindowSize)(ImGuiViewport* vp, ImVec2 size); // Resize swap chain, frame buffers etc. void (*Renderer_RenderWindow)(ImGuiViewport* vp, void* render_arg); // (Optional) Clear targets, Render viewport->DrawData void (*Renderer_SwapBuffers)(ImGuiViewport* vp, void* render_arg); // (Optional) Call Present/SwapBuffers (renderer side) // (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) ImVector Monitors; //------------------------------------------------------------------ // Output - List of viewports to render into platform windows //------------------------------------------------------------------ // List of viewports (the list is updated by calling ImGui::EndFrame or ImGui::Render) ImGuiViewport* MainViewport; // Guaranteed to be == Viewports[0] ImVector Viewports; // Main viewports, followed by all secondary viewports. ImGuiPlatformIO() { memset(this, 0, sizeof(*this)); } // Zero clear }; // Flags stored in ImGuiViewport::Flags, giving indications to the platform back-ends. enum ImGuiViewportFlags_ { ImGuiViewportFlags_None = 0, 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) ImGuiViewportFlags_NoTaskBarIcon = 1 << 1, // Platform Window: Disable platform task bar icon (generally set on popups/tooltips, or all windows if ImGuiConfigFlags_ViewportsNoTaskBarIcon is set) ImGuiViewportFlags_NoFocusOnAppearing = 1 << 2, // Platform Window: Don't take focus when created. ImGuiViewportFlags_NoFocusOnClick = 1 << 3, // Platform Window: Don't take focus when clicked on. ImGuiViewportFlags_NoInputs = 1 << 4, // Platform Window: Make mouse pass through so we can drag this window while peaking behind it. ImGuiViewportFlags_NoRendererClear = 1 << 5, // Platform Window: Renderer doesn't need to clear the framebuffer ahead (because we will fill it entirely). ImGuiViewportFlags_TopMost = 1 << 6, // Platform Window: Display on top (for tooltips only) 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. }; // 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. struct ImGuiViewport { ImGuiID ID; // Unique identifier for the viewport ImGuiViewportFlags Flags; // See ImGuiViewportFlags_ ImVec2 Pos; // Position of viewport both in imgui space and in OS desktop/native space ImVec2 Size; // Size of viewport in pixel float DpiScale; // 1.0f = 96 DPI = No extra scale ImDrawData* DrawData; // The ImDrawData corresponding to this viewport. Valid after Render() and until the next call to NewFrame(). ImGuiID ParentViewportId; // (Advanced) 0: no parent. Instruct the platform back-end to setup a parent/child relationship between platform windows. void* RendererUserData; // void* to hold custom data structure for the renderer (e.g. swap chain, frame-buffers etc.) void* PlatformUserData; // void* to hold custom data structure for the OS / platform (e.g. windowing info, render context) void* PlatformHandle; // void* for FindViewportByPlatformHandle(). (e.g. suggested to use natural platform handle such as HWND, GlfwWindow*, SDL_Window*) bool PlatformRequestClose; // Platform window requested closure (e.g. window was moved by the OS / host window manager, e.g. pressing ALT-F4) bool PlatformRequestMove; // Platform window requested move (e.g. window was moved by the OS / host window manager, authoritative position will be OS window position) bool PlatformRequestResize; // Platform window requested resize (e.g. window was resized by the OS / host window manager, authoritative size will be OS window size) ImGuiViewport() { ID = 0; Flags = 0; DpiScale = 0.0f; DrawData = NULL; ParentViewportId = 0; RendererUserData = PlatformUserData = PlatformHandle = NULL; PlatformRequestClose = PlatformRequestMove = PlatformRequestResize = false; } ~ImGuiViewport() { IM_ASSERT(PlatformUserData == NULL && RendererUserData == NULL); } }; #if defined(__clang__) #pragma clang diagnostic pop #elif defined(__GNUC__) && __GNUC__ >= 8 #pragma GCC diagnostic pop #endif // Include imgui_user.h at the end of imgui.h (convenient for user to only explicitly include vanilla imgui.h) #ifdef IMGUI_INCLUDE_IMGUI_USER_H #include "imgui_user.hpp" #endif #pragma warning(pop) ================================================ FILE: src/imgui/imgui_draw.cpp ================================================ // dear imgui, v1.70 WIP // (drawing and font code) /* Index of this file: // [SECTION] STB libraries implementation // [SECTION] Style functions // [SECTION] ImDrawList // [SECTION] ImDrawData // [SECTION] Helpers ShadeVertsXXX functions // [SECTION] ImFontConfig // [SECTION] ImFontAtlas // [SECTION] ImFontAtlas glyph ranges helpers // [SECTION] ImFontGlyphRangesBuilder // [SECTION] ImFont // [SECTION] Internal Render Helpers // [SECTION] Decompression code // [SECTION] Default font data (ProggyClean.ttf) */ #pragma warning(push, 0) #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS #endif #include "imgui.hpp" #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif #include "imgui_internal.hpp" #include // vsnprintf, sscanf, printf #if !defined(alloca) #if defined(__GLIBC__) || defined(__sun) || defined(__CYGWIN__) || defined(__APPLE__) #include // alloca (glibc uses . Note that Cygwin may have _WIN32 defined, so the order matters here) #elif defined(_WIN32) #include // alloca #if !defined(alloca) #define alloca _alloca // for clang with MS Codegen #endif #else #include // alloca #endif #endif // Visual Studio warnings #ifdef _MSC_VER #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff) #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen #endif // Clang/GCC warnings with -Weverything #ifdef __clang__ #pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. #pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants ok. #pragma clang diagnostic ignored "-Wglobal-constructors" // warning : declaration requires a global destructor // similar to above, not sure what the exact difference is. #pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // #if __has_warning("-Wzero-as-null-pointer-constant") #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 #endif #if __has_warning("-Wcomma") #pragma clang diagnostic ignored "-Wcomma" // warning : possible misuse of comma operator here // #endif #if __has_warning("-Wreserved-id-macro") #pragma clang diagnostic ignored "-Wreserved-id-macro" // warning : macro name is a reserved identifier // #endif #if __has_warning("-Wdouble-promotion") #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. #endif #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used #pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function #pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value #pragma GCC diagnostic ignored "-Wstack-protector" // warning: stack protector not protecting local variables: variable length buffer #if __GNUC__ >= 8 #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 #endif #endif //------------------------------------------------------------------------- // [SECTION] STB libraries implementation //------------------------------------------------------------------------- // Compile time options: //#define IMGUI_STB_NAMESPACE ImStb //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION #ifdef IMGUI_STB_NAMESPACE namespace IMGUI_STB_NAMESPACE { #endif #ifdef _MSC_VER #pragma warning (push) #pragma warning (disable: 4456) // declaration of 'xx' hides previous local declaration #endif #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-function" #pragma clang diagnostic ignored "-Wmissing-prototypes" #pragma clang diagnostic ignored "-Wimplicit-fallthrough" #pragma clang diagnostic ignored "-Wcast-qual" // warning : cast from 'const xxxx *' to 'xxx *' drops const qualifier // #endif #ifdef __GNUC__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wtype-limits" // warning: comparison is always true due to limited range of data type [-Wtype-limits] #pragma GCC diagnostic ignored "-Wcast-qual" // warning: cast from type 'const xxxx *' to type 'xxxx *' casts away qualifiers #endif #ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) #ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION #define STBRP_STATIC #define STBRP_ASSERT(x) IM_ASSERT(x) #define STBRP_SORT ImQsort #define STB_RECT_PACK_IMPLEMENTATION #endif #ifdef IMGUI_STB_RECT_PACK_FILENAME #include IMGUI_STB_RECT_PACK_FILENAME #else #include "imstb_rectpack.hpp" #endif #endif #ifndef STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) #ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION #define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x)) #define STBTT_free(x,u) ((void)(u), IM_FREE(x)) #define STBTT_assert(x) IM_ASSERT(x) #define STBTT_fmod(x,y) ImFmod(x,y) #define STBTT_sqrt(x) ImSqrt(x) #define STBTT_pow(x,y) ImPow(x,y) #define STBTT_fabs(x) ImFabs(x) #define STBTT_ifloor(x) ((int)ImFloorStd(x)) #define STBTT_iceil(x) ((int)ImCeil(x)) #define STBTT_STATIC #define STB_TRUETYPE_IMPLEMENTATION #else #define STBTT_DEF extern #endif #ifdef IMGUI_STB_TRUETYPE_FILENAME #include IMGUI_STB_TRUETYPE_FILENAME #else #include "imstb_truetype.hpp" #endif #endif #ifdef __GNUC__ #pragma GCC diagnostic pop #endif #ifdef __clang__ #pragma clang diagnostic pop #endif #ifdef _MSC_VER #pragma warning (pop) #endif #ifdef IMGUI_STB_NAMESPACE } // namespace ImStb using namespace IMGUI_STB_NAMESPACE; #endif //----------------------------------------------------------------------------- // [SECTION] Style functions //----------------------------------------------------------------------------- void ImGui::StyleColorsDark(ImGuiStyle* dst) { ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); ImVec4* colors = style->Colors; colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_FrameBg] = ImVec4(0.16f, 0.29f, 0.48f, 0.54f); colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); colors[ImGuiCol_TitleBgActive] = ImVec4(0.16f, 0.29f, 0.48f, 1.00f); colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_SliderGrab] = ImVec4(0.24f, 0.52f, 0.88f, 1.00f); colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f); colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f); colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f); colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_Separator] = colors[ImGuiCol_Border]; colors[ImGuiCol_SeparatorHovered] = ImVec4(0.10f, 0.40f, 0.75f, 0.78f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.10f, 0.40f, 0.75f, 1.00f); colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.25f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); colors[ImGuiCol_DockingPreview] = colors[ImGuiCol_HeaderActive] * ImVec4(1.0f, 1.0f, 1.0f, 0.7f); colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f); colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); } void ImGui::StyleColorsCherry(ImGuiStyle* dst) { ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); ImVec4* colors = style->Colors; // cherry colors, 3 intensities #define HI(v) ImVec4(0.502f, 0.075f, 0.256f, v) #define XHI(v) ImVec4(0.802f, 0.075f, 0.256f, v) #define MED(v) ImVec4(0.455f, 0.198f, 0.301f, v) #define LOW(v) ImVec4(0.232f, 0.201f, 0.271f, v) // backgrounds (@todo: complete with BG_MED, BG_LOW) #define BG(v) ImVec4(0.200f, 0.220f, 0.270f, v) // text #define TEXT(v) ImVec4(0.860f, 0.930f, 0.890f, v) colors[ImGuiCol_Tab] = HI(1.0f); colors[ImGuiCol_TabActive] = XHI(0.50f); colors[ImGuiCol_TabHovered] = XHI(1.0f); colors[ImGuiCol_TabUnfocused] = LOW(1.00f); colors[ImGuiCol_TabUnfocusedActive] = MED(0.60f); colors[ImGuiCol_DockingPreview] = HI(1.00f); colors[ImGuiCol_DragDropTarget] = BG(1.00f); colors[ImGuiCol_DockingEmptyBg] = BG(1.00f); colors[ImGuiCol_Text] = TEXT(0.78f); colors[ImGuiCol_TextDisabled] = TEXT(0.28f); colors[ImGuiCol_WindowBg] = ImVec4(0.13f, 0.14f, 0.17f, 1.00f); colors[ImGuiCol_ChildWindowBg] = BG(0.58f); colors[ImGuiCol_ChildBg] = ImVec4(0, 0, 0, 0); colors[ImGuiCol_PopupBg] = BG(0.9f); colors[ImGuiCol_Border] = ImVec4(0.31f, 0.31f, 1.00f, 0.00f); colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_FrameBg] = BG(1.00f); colors[ImGuiCol_FrameBgHovered] = MED(0.78f); colors[ImGuiCol_FrameBgActive] = MED(1.00f); colors[ImGuiCol_TitleBg] = LOW(1.00f); colors[ImGuiCol_TitleBgActive] = HI(1.00f); colors[ImGuiCol_TitleBgCollapsed] = BG(0.75f); colors[ImGuiCol_MenuBarBg] = BG(0.47f); colors[ImGuiCol_ScrollbarBg] = BG(1.00f); colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.09f, 0.15f, 0.16f, 1.00f); colors[ImGuiCol_ScrollbarGrabHovered] = MED(0.78f); colors[ImGuiCol_ScrollbarGrabActive] = MED(1.00f); colors[ImGuiCol_CheckMark] = ImVec4(0.71f, 0.22f, 0.27f, 1.00f); colors[ImGuiCol_SliderGrab] = ImVec4(0.47f, 0.77f, 0.83f, 0.14f); colors[ImGuiCol_SliderGrabActive] = ImVec4(0.71f, 0.22f, 0.27f, 1.00f); colors[ImGuiCol_Button] = ImVec4(0.47f, 0.77f, 0.83f, 0.14f); colors[ImGuiCol_ButtonHovered] = MED(0.86f); colors[ImGuiCol_ButtonActive] = MED(1.00f); colors[ImGuiCol_Header] = MED(0.76f); colors[ImGuiCol_HeaderHovered] = MED(0.86f); colors[ImGuiCol_HeaderActive] = HI(1.00f); colors[ImGuiCol_Column] = ImVec4(0.14f, 0.16f, 0.19f, 1.00f); colors[ImGuiCol_ColumnHovered] = MED(0.78f); colors[ImGuiCol_ColumnActive] = MED(1.00f); colors[ImGuiCol_ResizeGrip] = ImVec4(0.47f, 0.77f, 0.83f, 0.04f); colors[ImGuiCol_ResizeGripHovered] = MED(0.78f); colors[ImGuiCol_ResizeGripActive] = MED(1.00f); colors[ImGuiCol_PlotLines] = TEXT(0.63f); colors[ImGuiCol_PlotLinesHovered] = MED(1.00f); colors[ImGuiCol_PlotHistogram] = TEXT(0.63f); colors[ImGuiCol_PlotHistogramHovered] = MED(1.00f); colors[ImGuiCol_TextSelectedBg] = MED(0.43f); // [...] colors[ImGuiCol_ModalWindowDarkening] = BG(1.73f); colors[ImGuiCol_Border] = ImVec4(0.539f, 0.479f, 0.255f, 0.162f); style->TabRounding = 0.0f; style->TabBorderSize = 0.0f; style->WindowPadding = ImVec2(6, 4); style->WindowRounding = 0.0f; style->FramePadding = ImVec2(10, 4); style->FrameRounding = 3.0f; style->TouchExtraPadding = ImVec2(0, 0); style->IndentSpacing = 6.0f; style->ScrollbarSize = 12.0f; style->ScrollbarRounding = 16.0f; style->GrabMinSize = 20.0f; style->GrabRounding = 2.0f; style->ItemInnerSpacing = ImVec2(10, 1); style->ItemSpacing.x = 10; style->ItemSpacing.y = 4; style->IndentSpacing = 22; style->WindowTitleAlign.x = 0.50f; style->FrameBorderSize = 0.0f; style->WindowBorderSize = 1.0f; } void ImGui::StyleColorsUE(ImGuiStyle* dst) { ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); ImVec4* colors = style->Colors; colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); colors[ImGuiCol_ChildBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.00f); colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_FrameBg] = ImVec4(0.20f, 0.21f, 0.22f, 0.54f); colors[ImGuiCol_FrameBgHovered] = ImVec4(0.40f, 0.40f, 0.40f, 0.40f); colors[ImGuiCol_FrameBgActive] = ImVec4(0.18f, 0.18f, 0.18f, 0.67f); colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); colors[ImGuiCol_TitleBgActive] = ImVec4(0.29f, 0.29f, 0.29f, 1.00f); colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); colors[ImGuiCol_CheckMark] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f); colors[ImGuiCol_SliderGrab] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); colors[ImGuiCol_SliderGrabActive] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f); colors[ImGuiCol_Button] = ImVec4(0.44f, 0.44f, 0.44f, 0.40f); colors[ImGuiCol_ButtonHovered] = ImVec4(0.46f, 0.47f, 0.48f, 1.00f); colors[ImGuiCol_ButtonActive] = ImVec4(0.42f, 0.42f, 0.42f, 1.00f); colors[ImGuiCol_Header] = ImVec4(0.70f, 0.70f, 0.70f, 0.31f); colors[ImGuiCol_HeaderHovered] = ImVec4(0.70f, 0.70f, 0.70f, 0.80f); colors[ImGuiCol_HeaderActive] = ImVec4(0.48f, 0.50f, 0.52f, 1.00f); colors[ImGuiCol_Separator] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); colors[ImGuiCol_SeparatorHovered] = ImVec4(0.72f, 0.72f, 0.72f, 0.78f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); colors[ImGuiCol_ResizeGrip] = ImVec4(0.91f, 0.91f, 0.91f, 0.25f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.81f, 0.81f, 0.81f, 0.67f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.46f, 0.46f, 0.46f, 0.95f); colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.73f, 0.60f, 0.15f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.87f, 0.87f, 0.87f, 0.35f); colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); colors[ImGuiCol_NavHighlight] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); } IMGUI_API void ImGui::StyleCorporateGrey(ImGuiStyle* dst) { ImGuiStyle& style = ImGui::GetStyle(); ImVec4* colors = style.Colors; /// 0 = FLAT APPEARENCE /// 1 = MORE "3D" LOOK int is3D = 1; colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); colors[ImGuiCol_TextDisabled] = ImVec4(0.40f, 0.40f, 0.40f, 1.00f); colors[ImGuiCol_ChildBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); colors[ImGuiCol_WindowBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); colors[ImGuiCol_PopupBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); colors[ImGuiCol_Border] = ImVec4(0.12f, 0.12f, 0.12f, 0.71f); colors[ImGuiCol_BorderShadow] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f); colors[ImGuiCol_FrameBg] = ImVec4(0.42f, 0.42f, 0.42f, 0.54f); colors[ImGuiCol_FrameBgHovered] = ImVec4(0.42f, 0.42f, 0.42f, 0.40f); colors[ImGuiCol_FrameBgActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.67f); colors[ImGuiCol_TitleBg] = ImVec4(0.19f, 0.19f, 0.19f, 1.00f); colors[ImGuiCol_TitleBgActive] = ImVec4(0.22f, 0.22f, 0.22f, 1.00f); colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.17f, 0.17f, 0.17f, 0.90f); colors[ImGuiCol_MenuBarBg] = ImVec4(0.335f, 0.335f, 0.335f, 1.000f); colors[ImGuiCol_ScrollbarBg] = ImVec4(0.24f, 0.24f, 0.24f, 0.53f); colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.52f, 0.52f, 0.52f, 1.00f); colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.76f, 0.76f, 0.76f, 1.00f); colors[ImGuiCol_CheckMark] = ImVec4(0.65f, 0.65f, 0.65f, 1.00f); colors[ImGuiCol_SliderGrab] = ImVec4(0.52f, 0.52f, 0.52f, 1.00f); colors[ImGuiCol_SliderGrabActive] = ImVec4(0.64f, 0.64f, 0.64f, 1.00f); colors[ImGuiCol_Button] = ImVec4(0.54f, 0.54f, 0.54f, 0.35f); colors[ImGuiCol_ButtonHovered] = ImVec4(0.52f, 0.52f, 0.52f, 0.59f); colors[ImGuiCol_ButtonActive] = ImVec4(0.76f, 0.76f, 0.76f, 1.00f); colors[ImGuiCol_Header] = ImVec4(0.38f, 0.38f, 0.38f, 1.00f); colors[ImGuiCol_HeaderHovered] = ImVec4(0.47f, 0.47f, 0.47f, 1.00f); colors[ImGuiCol_HeaderActive] = ImVec4(0.76f, 0.76f, 0.76f, 0.77f); colors[ImGuiCol_Separator] = ImVec4(0.000f, 0.000f, 0.000f, 0.137f); colors[ImGuiCol_SeparatorHovered] = ImVec4(0.700f, 0.671f, 0.600f, 0.290f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.702f, 0.671f, 0.600f, 0.674f); colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.25f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.73f, 0.73f, 0.73f, 0.35f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); style.PopupRounding = 3; style.WindowPadding = ImVec2(4, 4); style.FramePadding = ImVec2(6, 4); style.ItemSpacing = ImVec2(6, 2); style.ScrollbarSize = 18; style.WindowBorderSize = 1; style.ChildBorderSize = 1; style.PopupBorderSize = 1; style.FrameBorderSize = is3D; style.WindowRounding = 3; style.ChildRounding = 3; style.FrameRounding = 3; style.ScrollbarRounding = 2; style.GrabRounding = 3; #ifdef IMGUI_HAS_DOCK style.TabBorderSize = is3D; style.TabRounding = 3; colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.38f, 0.38f, 0.38f, 1.00f); colors[ImGuiCol_Tab] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); colors[ImGuiCol_TabHovered] = ImVec4(0.40f, 0.40f, 0.40f, 1.00f); colors[ImGuiCol_TabActive] = ImVec4(0.33f, 0.33f, 0.33f, 1.00f); colors[ImGuiCol_TabUnfocused] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.33f, 0.33f, 0.33f, 1.00f); colors[ImGuiCol_DockingPreview] = ImVec4(0.85f, 0.85f, 0.85f, 0.28f); if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { style.WindowRounding = 0.0f; style.Colors[ImGuiCol_WindowBg].w = 1.0f; } #endif } void ImGui::StyleColorsDarkCodz1(ImGuiStyle* dst) { ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); ImVec4* colors = style->Colors; style->FrameBorderSize = 1.0f; style->FramePadding = ImVec2(4.0f, 2.0f); style->ItemSpacing = ImVec2(8.0f, 2.0f); style->WindowBorderSize = 1.0f; style->TabBorderSize = 1.0f; style->WindowRounding = 1.0f; style->ChildRounding = 1.0f; style->FrameRounding = 1.0f; style->ScrollbarRounding = 1.0f; style->GrabRounding = 1.0f; style->TabRounding = 1.0f; // Setup style colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 0.95f); colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); colors[ImGuiCol_WindowBg] = ImVec4(0.13f, 0.12f, 0.12f, 1.00f); colors[ImGuiCol_ChildBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.00f); colors[ImGuiCol_PopupBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.94f); colors[ImGuiCol_Border] = ImVec4(0.53f, 0.53f, 0.53f, 0.46f); colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_FrameBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.85f); colors[ImGuiCol_FrameBgHovered] = ImVec4(0.22f, 0.22f, 0.22f, 0.40f); colors[ImGuiCol_FrameBgActive] = ImVec4(0.16f, 0.16f, 0.16f, 0.53f); colors[ImGuiCol_TitleBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); colors[ImGuiCol_TitleBgActive] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); colors[ImGuiCol_MenuBarBg] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f); colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.48f, 0.48f, 0.48f, 1.00f); colors[ImGuiCol_CheckMark] = ImVec4(0.79f, 0.79f, 0.79f, 1.00f); colors[ImGuiCol_SliderGrab] = ImVec4(0.48f, 0.47f, 0.47f, 0.91f); colors[ImGuiCol_SliderGrabActive] = ImVec4(0.56f, 0.55f, 0.55f, 0.62f); colors[ImGuiCol_Button] = ImVec4(0.50f, 0.50f, 0.50f, 0.63f); colors[ImGuiCol_ButtonHovered] = ImVec4(0.67f, 0.67f, 0.68f, 0.63f); colors[ImGuiCol_ButtonActive] = ImVec4(0.26f, 0.26f, 0.26f, 0.63f); colors[ImGuiCol_Header] = ImVec4(0.54f, 0.54f, 0.54f, 0.58f); colors[ImGuiCol_HeaderHovered] = ImVec4(0.64f, 0.65f, 0.65f, 0.80f); colors[ImGuiCol_HeaderActive] = ImVec4(0.25f, 0.25f, 0.25f, 0.80f); colors[ImGuiCol_Separator] = ImVec4(0.58f, 0.58f, 0.58f, 0.50f); colors[ImGuiCol_SeparatorHovered] = ImVec4(0.81f, 0.81f, 0.81f, 0.64f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.81f, 0.81f, 0.81f, 0.64f); colors[ImGuiCol_ResizeGrip] = ImVec4(0.87f, 0.87f, 0.87f, 0.53f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.87f, 0.87f, 0.87f, 0.74f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.87f, 0.87f, 0.87f, 0.74f); colors[ImGuiCol_Tab] = ImVec4(0.01f, 0.01f, 0.01f, 0.86f); colors[ImGuiCol_TabHovered] = ImVec4(0.29f, 0.29f, 0.29f, 1.00f); colors[ImGuiCol_TabActive] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); colors[ImGuiCol_TabUnfocused] = ImVec4(0.02f, 0.02f, 0.02f, 1.00f); colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.19f, 0.19f, 0.19f, 1.00f); colors[ImGuiCol_DockingPreview] = ImVec4(0.38f, 0.48f, 0.60f, 1.00f); colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f); colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.68f, 0.68f, 0.68f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.77f, 0.33f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.87f, 0.55f, 0.08f, 1.00f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.47f, 0.60f, 0.76f, 0.47f); colors[ImGuiCol_DragDropTarget] = ImVec4(0.58f, 0.58f, 0.58f, 0.90f); colors[ImGuiCol_NavHighlight] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); } void ImGui::StyleColorsLightGreen(ImGuiStyle* dst) { ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); ImVec4* colors = style->Colors; style->WindowRounding = 2.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows style->ScrollbarRounding = 3.0f; // Radius of grab corners rounding for scrollbar style->GrabRounding = 2.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. style->AntiAliasedLines = true; style->AntiAliasedFill = true; style->WindowRounding = 2; style->ChildRounding = 2; style->ScrollbarSize = 16; style->ScrollbarRounding = 3; style->GrabRounding = 2; style->ItemSpacing.x = 10; style->ItemSpacing.y = 4; style->IndentSpacing = 22; style->FramePadding.x = 6; style->FramePadding.y = 4; style->Alpha = 1.0f; style->FrameRounding = 3.0f; colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); colors[ImGuiCol_WindowBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f); //colors[ImGuiCol_ChildWindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_PopupBg] = ImVec4(0.93f, 0.93f, 0.93f, 0.98f); colors[ImGuiCol_Border] = ImVec4(0.71f, 0.71f, 0.71f, 0.08f); colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.04f); colors[ImGuiCol_FrameBg] = ImVec4(0.71f, 0.71f, 0.71f, 0.55f); colors[ImGuiCol_FrameBgHovered] = ImVec4(0.94f, 0.94f, 0.94f, 0.55f); colors[ImGuiCol_FrameBgActive] = ImVec4(0.71f, 0.78f, 0.69f, 0.98f); colors[ImGuiCol_TitleBg] = ImVec4(0.85f, 0.85f, 0.85f, 1.00f); colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.82f, 0.78f, 0.78f, 0.51f); colors[ImGuiCol_TitleBgActive] = ImVec4(0.78f, 0.78f, 0.78f, 1.00f); colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f); colors[ImGuiCol_ScrollbarBg] = ImVec4(0.20f, 0.25f, 0.30f, 0.61f); colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.90f, 0.90f, 0.90f, 0.30f); colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.92f, 0.92f, 0.92f, 0.78f); colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); colors[ImGuiCol_CheckMark] = ImVec4(0.184f, 0.407f, 0.193f, 1.00f); colors[ImGuiCol_SliderGrab] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f); colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_Button] = ImVec4(0.71f, 0.78f, 0.69f, 0.40f); colors[ImGuiCol_ButtonHovered] = ImVec4(0.725f, 0.805f, 0.702f, 1.00f); colors[ImGuiCol_ButtonActive] = ImVec4(0.793f, 0.900f, 0.836f, 1.00f); colors[ImGuiCol_Header] = ImVec4(0.71f, 0.78f, 0.69f, 0.31f); colors[ImGuiCol_HeaderHovered] = ImVec4(0.71f, 0.78f, 0.69f, 0.80f); colors[ImGuiCol_HeaderActive] = ImVec4(0.71f, 0.78f, 0.69f, 1.00f); colors[ImGuiCol_Column] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f); colors[ImGuiCol_ColumnHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f); colors[ImGuiCol_ColumnActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_Separator] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f); colors[ImGuiCol_SeparatorHovered] = ImVec4(0.14f, 0.44f, 0.80f, 0.78f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.14f, 0.44f, 0.80f, 1.00f); colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.00f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.45f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f); colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.70f, 0.70f, 0.70f, 0.70f); } void ImGui::StyleColorsClassic(ImGuiStyle* dst) { ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); ImVec4* colors = style->Colors; colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f); colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.70f); colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_PopupBg] = ImVec4(0.11f, 0.11f, 0.14f, 0.92f); colors[ImGuiCol_Border] = ImVec4(0.50f, 0.50f, 0.50f, 0.50f); colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_FrameBg] = ImVec4(0.43f, 0.43f, 0.43f, 0.39f); colors[ImGuiCol_FrameBgHovered] = ImVec4(0.47f, 0.47f, 0.69f, 0.40f); colors[ImGuiCol_FrameBgActive] = ImVec4(0.42f, 0.41f, 0.64f, 0.69f); colors[ImGuiCol_TitleBg] = ImVec4(0.27f, 0.27f, 0.54f, 0.83f); colors[ImGuiCol_TitleBgActive] = ImVec4(0.32f, 0.32f, 0.63f, 0.87f); colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.40f, 0.80f, 0.20f); colors[ImGuiCol_MenuBarBg] = ImVec4(0.40f, 0.40f, 0.55f, 0.80f); colors[ImGuiCol_ScrollbarBg] = ImVec4(0.20f, 0.25f, 0.30f, 0.60f); colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.40f, 0.80f, 0.30f); colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.80f, 0.40f); colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f); colors[ImGuiCol_CheckMark] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f); colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f); colors[ImGuiCol_SliderGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f); colors[ImGuiCol_Button] = ImVec4(0.35f, 0.40f, 0.61f, 0.62f); colors[ImGuiCol_ButtonHovered] = ImVec4(0.40f, 0.48f, 0.71f, 0.79f); colors[ImGuiCol_ButtonActive] = ImVec4(0.46f, 0.54f, 0.80f, 1.00f); colors[ImGuiCol_Header] = ImVec4(0.40f, 0.40f, 0.90f, 0.45f); colors[ImGuiCol_HeaderHovered] = ImVec4(0.45f, 0.45f, 0.90f, 0.80f); colors[ImGuiCol_HeaderActive] = ImVec4(0.53f, 0.53f, 0.87f, 0.80f); colors[ImGuiCol_Separator] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); colors[ImGuiCol_SeparatorHovered] = ImVec4(0.60f, 0.60f, 0.70f, 1.00f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.70f, 0.70f, 0.90f, 1.00f); colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.16f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.78f, 0.82f, 1.00f, 0.60f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.78f, 0.82f, 1.00f, 0.90f); colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); colors[ImGuiCol_DockingPreview] = colors[ImGuiCol_Header] * ImVec4(1.0f, 1.0f, 1.0f, 0.7f); colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f); colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); } // Those light colors are better suited with a thicker font than the default one + FrameBorder void ImGui::StyleColorsLight(ImGuiStyle* dst) { ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); ImVec4* colors = style->Colors; colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f); colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.98f); colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.30f); colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f); colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f); colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f); colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f); colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f); colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 0.80f); colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.49f, 0.49f, 0.49f, 0.80f); colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f); colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_SliderGrab] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f); colors[ImGuiCol_SliderGrabActive] = ImVec4(0.46f, 0.54f, 0.80f, 0.60f); colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f); colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f); colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f); colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_Separator] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f); colors[ImGuiCol_SeparatorHovered] = ImVec4(0.14f, 0.44f, 0.80f, 0.78f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.14f, 0.44f, 0.80f, 1.00f); colors[ImGuiCol_ResizeGrip] = ImVec4(0.80f, 0.80f, 0.80f, 0.56f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.90f); colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); colors[ImGuiCol_DockingPreview] = colors[ImGuiCol_Header] * ImVec4(1.0f, 1.0f, 1.0f, 0.7f); colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f); colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.45f, 0.00f, 1.00f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.70f, 0.70f, 0.70f, 0.70f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.20f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); } //----------------------------------------------------------------------------- // ImDrawList //----------------------------------------------------------------------------- ImDrawListSharedData::ImDrawListSharedData() { Font = NULL; FontSize = 0.0f; CurveTessellationTol = 0.0f; ClipRectFullscreen = ImVec4(-8192.0f, -8192.0f, +8192.0f, +8192.0f); // Const data for (int i = 0; i < IM_ARRAYSIZE(CircleVtx12); i++) { const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(CircleVtx12); CircleVtx12[i] = ImVec2(ImCos(a), ImSin(a)); } } void ImDrawList::Clear() { CmdBuffer.resize(0); IdxBuffer.resize(0); VtxBuffer.resize(0); Flags = ImDrawListFlags_AntiAliasedLines | ImDrawListFlags_AntiAliasedFill; _VtxCurrentIdx = 0; _VtxWritePtr = NULL; _IdxWritePtr = NULL; _ClipRectStack.resize(0); _TextureIdStack.resize(0); _Path.resize(0); _ChannelsCurrent = 0; _ChannelsCount = 1; // NB: Do not clear channels so our allocations are re-used after the first frame. } void ImDrawList::ClearFreeMemory() { CmdBuffer.clear(); IdxBuffer.clear(); VtxBuffer.clear(); _VtxCurrentIdx = 0; _VtxWritePtr = NULL; _IdxWritePtr = NULL; _ClipRectStack.clear(); _TextureIdStack.clear(); _Path.clear(); _ChannelsCurrent = 0; _ChannelsCount = 1; for (int i = 0; i < _Channels.Size; i++) { if (i == 0) memset(&_Channels[0], 0, sizeof(_Channels[0])); // channel 0 is a copy of CmdBuffer/IdxBuffer, don't destruct again _Channels[i].CmdBuffer.clear(); _Channels[i].IdxBuffer.clear(); } _Channels.clear(); } ImDrawList* ImDrawList::CloneOutput() const { ImDrawList* dst = IM_NEW(ImDrawList(NULL)); dst->CmdBuffer = CmdBuffer; dst->IdxBuffer = IdxBuffer; dst->VtxBuffer = VtxBuffer; dst->Flags = Flags; return dst; } // Using macros because C++ is a terrible language, we want guaranteed inline, no code in header, and no overhead in Debug builds #define GetCurrentClipRect() (_ClipRectStack.Size ? _ClipRectStack.Data[_ClipRectStack.Size-1] : _Data->ClipRectFullscreen) #define GetCurrentTextureId() (_TextureIdStack.Size ? _TextureIdStack.Data[_TextureIdStack.Size-1] : (ImTextureID)NULL) void ImDrawList::AddDrawCmd() { ImDrawCmd draw_cmd; draw_cmd.ClipRect = GetCurrentClipRect(); draw_cmd.TextureId = GetCurrentTextureId(); IM_ASSERT(draw_cmd.ClipRect.x <= draw_cmd.ClipRect.z && draw_cmd.ClipRect.y <= draw_cmd.ClipRect.w); CmdBuffer.push_back(draw_cmd); } void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) { ImDrawCmd* current_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL; if (!current_cmd || current_cmd->ElemCount != 0 || current_cmd->UserCallback != NULL) { AddDrawCmd(); current_cmd = &CmdBuffer.back(); } current_cmd->UserCallback = callback; current_cmd->UserCallbackData = callback_data; AddDrawCmd(); // Force a new command after us (see comment below) } // 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. // 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. void ImDrawList::UpdateClipRect() { // If current command is used with different settings we need to add a new command const ImVec4 curr_clip_rect = GetCurrentClipRect(); ImDrawCmd* curr_cmd = CmdBuffer.Size > 0 ? &CmdBuffer.Data[CmdBuffer.Size-1] : NULL; if (!curr_cmd || (curr_cmd->ElemCount != 0 && memcmp(&curr_cmd->ClipRect, &curr_clip_rect, sizeof(ImVec4)) != 0) || curr_cmd->UserCallback != NULL) { AddDrawCmd(); return; } // Try to merge with previous command if it matches, else use current command ImDrawCmd* prev_cmd = CmdBuffer.Size > 1 ? curr_cmd - 1 : NULL; 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) CmdBuffer.pop_back(); else curr_cmd->ClipRect = curr_clip_rect; } void ImDrawList::UpdateTextureID() { // If current command is used with different settings we need to add a new command const ImTextureID curr_texture_id = GetCurrentTextureId(); ImDrawCmd* curr_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL; if (!curr_cmd || (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != curr_texture_id) || curr_cmd->UserCallback != NULL) { AddDrawCmd(); return; } // Try to merge with previous command if it matches, else use current command ImDrawCmd* prev_cmd = CmdBuffer.Size > 1 ? curr_cmd - 1 : NULL; 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) CmdBuffer.pop_back(); else curr_cmd->TextureId = curr_texture_id; } #undef GetCurrentClipRect #undef GetCurrentTextureId // 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) void ImDrawList::PushClipRect(ImVec2 cr_min, ImVec2 cr_max, bool intersect_with_current_clip_rect) { ImVec4 cr(cr_min.x, cr_min.y, cr_max.x, cr_max.y); if (intersect_with_current_clip_rect && _ClipRectStack.Size) { ImVec4 current = _ClipRectStack.Data[_ClipRectStack.Size-1]; if (cr.x < current.x) cr.x = current.x; if (cr.y < current.y) cr.y = current.y; if (cr.z > current.z) cr.z = current.z; if (cr.w > current.w) cr.w = current.w; } cr.z = ImMax(cr.x, cr.z); cr.w = ImMax(cr.y, cr.w); _ClipRectStack.push_back(cr); UpdateClipRect(); } void ImDrawList::PushClipRectFullScreen() { PushClipRect(ImVec2(_Data->ClipRectFullscreen.x, _Data->ClipRectFullscreen.y), ImVec2(_Data->ClipRectFullscreen.z, _Data->ClipRectFullscreen.w)); } void ImDrawList::PopClipRect() { IM_ASSERT(_ClipRectStack.Size > 0); _ClipRectStack.pop_back(); UpdateClipRect(); } void ImDrawList::PushTextureID(ImTextureID texture_id) { _TextureIdStack.push_back(texture_id); UpdateTextureID(); } void ImDrawList::PopTextureID() { IM_ASSERT(_TextureIdStack.Size > 0); _TextureIdStack.pop_back(); UpdateTextureID(); } void ImDrawList::ChannelsSplit(int channels_count) { IM_ASSERT(_ChannelsCurrent == 0 && _ChannelsCount == 1); int old_channels_count = _Channels.Size; if (old_channels_count < channels_count) _Channels.resize(channels_count); _ChannelsCount = channels_count; // _Channels[] (24/32 bytes each) hold storage that we'll swap with this->_CmdBuffer/_IdxBuffer // 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. // When we switch to the next channel, we'll copy _CmdBuffer/_IdxBuffer into _Channels[0] and then _Channels[1] into _CmdBuffer/_IdxBuffer memset(&_Channels[0], 0, sizeof(ImDrawChannel)); for (int i = 1; i < channels_count; i++) { if (i >= old_channels_count) { IM_PLACEMENT_NEW(&_Channels[i]) ImDrawChannel(); } else { _Channels[i].CmdBuffer.resize(0); _Channels[i].IdxBuffer.resize(0); } if (_Channels[i].CmdBuffer.Size == 0) { ImDrawCmd draw_cmd; draw_cmd.ClipRect = _ClipRectStack.back(); draw_cmd.TextureId = _TextureIdStack.back(); _Channels[i].CmdBuffer.push_back(draw_cmd); } } } void ImDrawList::ChannelsMerge() { // 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. if (_ChannelsCount <= 1) return; ChannelsSetCurrent(0); if (CmdBuffer.Size && CmdBuffer.back().ElemCount == 0) CmdBuffer.pop_back(); int new_cmd_buffer_count = 0, new_idx_buffer_count = 0; for (int i = 1; i < _ChannelsCount; i++) { ImDrawChannel& ch = _Channels[i]; if (ch.CmdBuffer.Size && ch.CmdBuffer.back().ElemCount == 0) ch.CmdBuffer.pop_back(); new_cmd_buffer_count += ch.CmdBuffer.Size; new_idx_buffer_count += ch.IdxBuffer.Size; } CmdBuffer.resize(CmdBuffer.Size + new_cmd_buffer_count); IdxBuffer.resize(IdxBuffer.Size + new_idx_buffer_count); ImDrawCmd* cmd_write = CmdBuffer.Data + CmdBuffer.Size - new_cmd_buffer_count; _IdxWritePtr = IdxBuffer.Data + IdxBuffer.Size - new_idx_buffer_count; for (int i = 1; i < _ChannelsCount; i++) { ImDrawChannel& ch = _Channels[i]; if (int sz = ch.CmdBuffer.Size) { memcpy(cmd_write, ch.CmdBuffer.Data, sz * sizeof(ImDrawCmd)); cmd_write += sz; } if (int sz = ch.IdxBuffer.Size) { memcpy(_IdxWritePtr, ch.IdxBuffer.Data, sz * sizeof(ImDrawIdx)); _IdxWritePtr += sz; } } UpdateClipRect(); // We call this instead of AddDrawCmd(), so that empty channels won't produce an extra draw call. _ChannelsCount = 1; } void ImDrawList::ChannelsSetCurrent(int idx) { IM_ASSERT(idx < _ChannelsCount); if (_ChannelsCurrent == idx) return; memcpy(&_Channels.Data[_ChannelsCurrent].CmdBuffer, &CmdBuffer, sizeof(CmdBuffer)); // copy 12 bytes, four times memcpy(&_Channels.Data[_ChannelsCurrent].IdxBuffer, &IdxBuffer, sizeof(IdxBuffer)); _ChannelsCurrent = idx; memcpy(&CmdBuffer, &_Channels.Data[_ChannelsCurrent].CmdBuffer, sizeof(CmdBuffer)); memcpy(&IdxBuffer, &_Channels.Data[_ChannelsCurrent].IdxBuffer, sizeof(IdxBuffer)); _IdxWritePtr = IdxBuffer.Data + IdxBuffer.Size; } // NB: this can be called with negative count for removing primitives (as long as the result does not underflow) void ImDrawList::PrimReserve(int idx_count, int vtx_count) { ImDrawCmd& draw_cmd = CmdBuffer.Data[CmdBuffer.Size-1]; draw_cmd.ElemCount += idx_count; int vtx_buffer_old_size = VtxBuffer.Size; VtxBuffer.resize(vtx_buffer_old_size + vtx_count); _VtxWritePtr = VtxBuffer.Data + vtx_buffer_old_size; int idx_buffer_old_size = IdxBuffer.Size; IdxBuffer.resize(idx_buffer_old_size + idx_count); _IdxWritePtr = IdxBuffer.Data + idx_buffer_old_size; } // Fully unrolled with inline call to keep our debug builds decently fast. void ImDrawList::PrimRect(const ImVec2& a, const ImVec2& c, ImU32 col) { ImVec2 b(c.x, a.y), d(a.x, c.y), uv(_Data->TexUvWhitePixel); ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col; _VtxWritePtr += 4; _VtxCurrentIdx += 4; _IdxWritePtr += 6; } void ImDrawList::PrimRectUV(const ImVec2& a, const ImVec2& c, const ImVec2& uv_a, const ImVec2& uv_c, ImU32 col) { 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); ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col; _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col; _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col; _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col; _VtxWritePtr += 4; _VtxCurrentIdx += 4; _IdxWritePtr += 6; } void 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) { ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col; _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col; _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col; _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col; _VtxWritePtr += 4; _VtxCurrentIdx += 4; _IdxWritePtr += 6; } // On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superflous function calls to optimize debug/non-inlined builds. // Those macros expects l-values. #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; } } #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; } // TODO: Thickness anti-aliased lines cap are missing their AA fringe. // We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds. void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, bool closed, float thickness) { if (points_count < 2) return; const ImVec2 uv = _Data->TexUvWhitePixel; int count = points_count; if (!closed) count = points_count-1; const bool thick_line = thickness > 1.0f; if (Flags & ImDrawListFlags_AntiAliasedLines) { // Anti-aliased stroke const float AA_SIZE = 1.0f; const ImU32 col_trans = col & ~IM_COL32_A_MASK; const int idx_count = thick_line ? count*18 : count*12; const int vtx_count = thick_line ? points_count*4 : points_count*3; PrimReserve(idx_count, vtx_count); // Temporary buffer ImVec2* temp_normals = (ImVec2*)alloca(points_count * (thick_line ? 5 : 3) * sizeof(ImVec2)); //-V630 ImVec2* temp_points = temp_normals + points_count; for (int i1 = 0; i1 < count; i1++) { const int i2 = (i1+1) == points_count ? 0 : i1+1; float dx = points[i2].x - points[i1].x; float dy = points[i2].y - points[i1].y; IM_NORMALIZE2F_OVER_ZERO(dx, dy); temp_normals[i1].x = dy; temp_normals[i1].y = -dx; } if (!closed) temp_normals[points_count-1] = temp_normals[points_count-2]; if (!thick_line) { if (!closed) { temp_points[0] = points[0] + temp_normals[0] * AA_SIZE; temp_points[1] = points[0] - temp_normals[0] * AA_SIZE; temp_points[(points_count-1)*2+0] = points[points_count-1] + temp_normals[points_count-1] * AA_SIZE; temp_points[(points_count-1)*2+1] = points[points_count-1] - temp_normals[points_count-1] * AA_SIZE; } // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. unsigned int idx1 = _VtxCurrentIdx; for (int i1 = 0; i1 < count; i1++) { const int i2 = (i1+1) == points_count ? 0 : i1+1; unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+3; // Average normals float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; IM_FIXNORMAL2F(dm_x, dm_y) dm_x *= AA_SIZE; dm_y *= AA_SIZE; // Add temporary vertexes ImVec2* out_vtx = &temp_points[i2*2]; out_vtx[0].x = points[i2].x + dm_x; out_vtx[0].y = points[i2].y + dm_y; out_vtx[1].x = points[i2].x - dm_x; out_vtx[1].y = points[i2].y - dm_y; // Add indexes _IdxWritePtr[0] = (ImDrawIdx)(idx2+0); _IdxWritePtr[1] = (ImDrawIdx)(idx1+0); _IdxWritePtr[2] = (ImDrawIdx)(idx1+2); _IdxWritePtr[3] = (ImDrawIdx)(idx1+2); _IdxWritePtr[4] = (ImDrawIdx)(idx2+2); _IdxWritePtr[5] = (ImDrawIdx)(idx2+0); _IdxWritePtr[6] = (ImDrawIdx)(idx2+1); _IdxWritePtr[7] = (ImDrawIdx)(idx1+1); _IdxWritePtr[8] = (ImDrawIdx)(idx1+0); _IdxWritePtr[9] = (ImDrawIdx)(idx1+0); _IdxWritePtr[10]= (ImDrawIdx)(idx2+0); _IdxWritePtr[11]= (ImDrawIdx)(idx2+1); _IdxWritePtr += 12; idx1 = idx2; } // Add vertexes for (int i = 0; i < points_count; i++) { _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; _VtxWritePtr[1].pos = temp_points[i*2+0]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans; _VtxWritePtr[2].pos = temp_points[i*2+1]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col_trans; _VtxWritePtr += 3; } } else { const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f; if (!closed) { temp_points[0] = points[0] + temp_normals[0] * (half_inner_thickness + AA_SIZE); temp_points[1] = points[0] + temp_normals[0] * (half_inner_thickness); temp_points[2] = points[0] - temp_normals[0] * (half_inner_thickness); temp_points[3] = points[0] - temp_normals[0] * (half_inner_thickness + AA_SIZE); temp_points[(points_count-1)*4+0] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE); temp_points[(points_count-1)*4+1] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness); temp_points[(points_count-1)*4+2] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness); temp_points[(points_count-1)*4+3] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE); } // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. unsigned int idx1 = _VtxCurrentIdx; for (int i1 = 0; i1 < count; i1++) { const int i2 = (i1+1) == points_count ? 0 : i1+1; unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+4; // Average normals float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; IM_FIXNORMAL2F(dm_x, dm_y); float dm_out_x = dm_x * (half_inner_thickness + AA_SIZE); float dm_out_y = dm_y * (half_inner_thickness + AA_SIZE); float dm_in_x = dm_x * half_inner_thickness; float dm_in_y = dm_y * half_inner_thickness; // Add temporary vertexes ImVec2* out_vtx = &temp_points[i2*4]; out_vtx[0].x = points[i2].x + dm_out_x; out_vtx[0].y = points[i2].y + dm_out_y; out_vtx[1].x = points[i2].x + dm_in_x; out_vtx[1].y = points[i2].y + dm_in_y; out_vtx[2].x = points[i2].x - dm_in_x; out_vtx[2].y = points[i2].y - dm_in_y; out_vtx[3].x = points[i2].x - dm_out_x; out_vtx[3].y = points[i2].y - dm_out_y; // Add indexes _IdxWritePtr[0] = (ImDrawIdx)(idx2+1); _IdxWritePtr[1] = (ImDrawIdx)(idx1+1); _IdxWritePtr[2] = (ImDrawIdx)(idx1+2); _IdxWritePtr[3] = (ImDrawIdx)(idx1+2); _IdxWritePtr[4] = (ImDrawIdx)(idx2+2); _IdxWritePtr[5] = (ImDrawIdx)(idx2+1); _IdxWritePtr[6] = (ImDrawIdx)(idx2+1); _IdxWritePtr[7] = (ImDrawIdx)(idx1+1); _IdxWritePtr[8] = (ImDrawIdx)(idx1+0); _IdxWritePtr[9] = (ImDrawIdx)(idx1+0); _IdxWritePtr[10] = (ImDrawIdx)(idx2+0); _IdxWritePtr[11] = (ImDrawIdx)(idx2+1); _IdxWritePtr[12] = (ImDrawIdx)(idx2+2); _IdxWritePtr[13] = (ImDrawIdx)(idx1+2); _IdxWritePtr[14] = (ImDrawIdx)(idx1+3); _IdxWritePtr[15] = (ImDrawIdx)(idx1+3); _IdxWritePtr[16] = (ImDrawIdx)(idx2+3); _IdxWritePtr[17] = (ImDrawIdx)(idx2+2); _IdxWritePtr += 18; idx1 = idx2; } // Add vertexes for (int i = 0; i < points_count; i++) { _VtxWritePtr[0].pos = temp_points[i*4+0]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col_trans; _VtxWritePtr[1].pos = temp_points[i*4+1]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; _VtxWritePtr[2].pos = temp_points[i*4+2]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; _VtxWritePtr[3].pos = temp_points[i*4+3]; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col_trans; _VtxWritePtr += 4; } } _VtxCurrentIdx += (ImDrawIdx)vtx_count; } else { // Non Anti-aliased Stroke const int idx_count = count*6; const int vtx_count = count*4; // FIXME-OPT: Not sharing edges PrimReserve(idx_count, vtx_count); for (int i1 = 0; i1 < count; i1++) { const int i2 = (i1+1) == points_count ? 0 : i1+1; const ImVec2& p1 = points[i1]; const ImVec2& p2 = points[i2]; float dx = p2.x - p1.x; float dy = p2.y - p1.y; IM_NORMALIZE2F_OVER_ZERO(dx, dy); dx *= (thickness * 0.5f); dy *= (thickness * 0.5f); _VtxWritePtr[0].pos.x = p1.x + dy; _VtxWritePtr[0].pos.y = p1.y - dx; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; _VtxWritePtr[1].pos.x = p2.x + dy; _VtxWritePtr[1].pos.y = p2.y - dx; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; _VtxWritePtr[2].pos.x = p2.x - dy; _VtxWritePtr[2].pos.y = p2.y + dx; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; _VtxWritePtr[3].pos.x = p1.x - dy; _VtxWritePtr[3].pos.y = p1.y + dx; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col; _VtxWritePtr += 4; _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+2); _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx+2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx+3); _IdxWritePtr += 6; _VtxCurrentIdx += 4; } } } // We intentionally avoid using ImVec2 and its math operators here to reduce cost to a minimum for debug/non-inlined builds. void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_count, ImU32 col) { if (points_count < 3) return; const ImVec2 uv = _Data->TexUvWhitePixel; if (Flags & ImDrawListFlags_AntiAliasedFill) { // Anti-aliased Fill const float AA_SIZE = 1.0f; const ImU32 col_trans = col & ~IM_COL32_A_MASK; const int idx_count = (points_count-2)*3 + points_count*6; const int vtx_count = (points_count*2); PrimReserve(idx_count, vtx_count); // Add indexes for fill unsigned int vtx_inner_idx = _VtxCurrentIdx; unsigned int vtx_outer_idx = _VtxCurrentIdx+1; for (int i = 2; i < points_count; i++) { _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+((i-1)<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx+(i<<1)); _IdxWritePtr += 3; } // Compute normals ImVec2* temp_normals = (ImVec2*)alloca(points_count * sizeof(ImVec2)); //-V630 for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) { const ImVec2& p0 = points[i0]; const ImVec2& p1 = points[i1]; float dx = p1.x - p0.x; float dy = p1.y - p0.y; IM_NORMALIZE2F_OVER_ZERO(dx, dy); temp_normals[i0].x = dy; temp_normals[i0].y = -dx; } for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) { // Average normals const ImVec2& n0 = temp_normals[i0]; const ImVec2& n1 = temp_normals[i1]; float dm_x = (n0.x + n1.x) * 0.5f; float dm_y = (n0.y + n1.y) * 0.5f; IM_FIXNORMAL2F(dm_x, dm_y); dm_x *= AA_SIZE * 0.5f; dm_y *= AA_SIZE * 0.5f; // Add vertices _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 _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 _VtxWritePtr += 2; // Add indexes for fringes _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx+(i1<<1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+(i0<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx+(i0<<1)); _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx+(i0<<1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx+(i1<<1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx+(i1<<1)); _IdxWritePtr += 6; } _VtxCurrentIdx += (ImDrawIdx)vtx_count; } else { // Non Anti-aliased Fill const int idx_count = (points_count-2)*3; const int vtx_count = points_count; PrimReserve(idx_count, vtx_count); for (int i = 0; i < vtx_count; i++) { _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; _VtxWritePtr++; } for (int i = 2; i < points_count; i++) { _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+i-1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+i); _IdxWritePtr += 3; } _VtxCurrentIdx += (ImDrawIdx)vtx_count; } } void ImDrawList::PathArcToFast(const ImVec2& centre, float radius, int a_min_of_12, int a_max_of_12) { if (radius == 0.0f || a_min_of_12 > a_max_of_12) { _Path.push_back(centre); return; } _Path.reserve(_Path.Size + (a_max_of_12 - a_min_of_12 + 1)); for (int a = a_min_of_12; a <= a_max_of_12; a++) { const ImVec2& c = _Data->CircleVtx12[a % IM_ARRAYSIZE(_Data->CircleVtx12)]; _Path.push_back(ImVec2(centre.x + c.x * radius, centre.y + c.y * radius)); } } void ImDrawList::PathArcTo(const ImVec2& centre, float radius, float a_min, float a_max, int num_segments) { if (radius == 0.0f) { _Path.push_back(centre); return; } // Note that we are adding a point at both a_min and a_max. // If you are trying to draw a full closed circle you don't want the overlapping points! _Path.reserve(_Path.Size + (num_segments + 1)); for (int i = 0; i <= num_segments; i++) { const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min); _Path.push_back(ImVec2(centre.x + ImCos(a) * radius, centre.y + ImSin(a) * radius)); } } static void PathBezierToCasteljau(ImVector* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) { float dx = x4 - x1; float dy = y4 - y1; float d2 = ((x2 - x4) * dy - (y2 - y4) * dx); float d3 = ((x3 - x4) * dy - (y3 - y4) * dx); d2 = (d2 >= 0) ? d2 : -d2; d3 = (d3 >= 0) ? d3 : -d3; if ((d2+d3) * (d2+d3) < tess_tol * (dx*dx + dy*dy)) { path->push_back(ImVec2(x4, y4)); } else if (level < 10) { float x12 = (x1+x2)*0.5f, y12 = (y1+y2)*0.5f; float x23 = (x2+x3)*0.5f, y23 = (y2+y3)*0.5f; float x34 = (x3+x4)*0.5f, y34 = (y3+y4)*0.5f; float x123 = (x12+x23)*0.5f, y123 = (y12+y23)*0.5f; float x234 = (x23+x34)*0.5f, y234 = (y23+y34)*0.5f; float x1234 = (x123+x234)*0.5f, y1234 = (y123+y234)*0.5f; PathBezierToCasteljau(path, x1,y1, x12,y12, x123,y123, x1234,y1234, tess_tol, level+1); PathBezierToCasteljau(path, x1234,y1234, x234,y234, x34,y34, x4,y4, tess_tol, level+1); } } void ImDrawList::PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments) { ImVec2 p1 = _Path.back(); if (num_segments == 0) { // Auto-tessellated PathBezierToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, _Data->CurveTessellationTol, 0); } else { float t_step = 1.0f / (float)num_segments; for (int i_step = 1; i_step <= num_segments; i_step++) { float t = t_step * i_step; float u = 1.0f - t; float w1 = u*u*u; float w2 = 3*u*u*t; float w3 = 3*u*t*t; float w4 = t*t*t; _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)); } } } void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, int rounding_corners) { 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); 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); if (rounding <= 0.0f || rounding_corners == 0) { PathLineTo(a); PathLineTo(ImVec2(b.x, a.y)); PathLineTo(b); PathLineTo(ImVec2(a.x, b.y)); } else { const float rounding_tl = (rounding_corners & ImDrawCornerFlags_TopLeft) ? rounding : 0.0f; const float rounding_tr = (rounding_corners & ImDrawCornerFlags_TopRight) ? rounding : 0.0f; const float rounding_br = (rounding_corners & ImDrawCornerFlags_BotRight) ? rounding : 0.0f; const float rounding_bl = (rounding_corners & ImDrawCornerFlags_BotLeft) ? rounding : 0.0f; PathArcToFast(ImVec2(a.x + rounding_tl, a.y + rounding_tl), rounding_tl, 6, 9); PathArcToFast(ImVec2(b.x - rounding_tr, a.y + rounding_tr), rounding_tr, 9, 12); PathArcToFast(ImVec2(b.x - rounding_br, b.y - rounding_br), rounding_br, 0, 3); PathArcToFast(ImVec2(a.x + rounding_bl, b.y - rounding_bl), rounding_bl, 3, 6); } } void ImDrawList::AddLine(const ImVec2& a, const ImVec2& b, ImU32 col, float thickness) { if ((col & IM_COL32_A_MASK) == 0) return; PathLineTo(a + ImVec2(0.5f,0.5f)); PathLineTo(b + ImVec2(0.5f,0.5f)); PathStroke(col, false, thickness); } // a: upper-left, b: lower-right. we don't render 1 px sized rectangles properly. void ImDrawList::AddRect(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding, int rounding_corners_flags, float thickness) { if ((col & IM_COL32_A_MASK) == 0) return; if (Flags & ImDrawListFlags_AntiAliasedLines) PathRect(a + ImVec2(0.5f,0.5f), b - ImVec2(0.50f,0.50f), rounding, rounding_corners_flags); else 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. PathStroke(col, true, thickness); } void ImDrawList::AddRectFilled(const ImVec2& a, const ImVec2& b, ImU32 col, float rounding, int rounding_corners_flags) { if ((col & IM_COL32_A_MASK) == 0) return; if (rounding > 0.0f) { PathRect(a, b, rounding, rounding_corners_flags); PathFillConvex(col); } else { PrimReserve(6, 4); PrimRect(a, b, col); } } void ImDrawList::AddRectFilledMultiColor(const ImVec2& a, const ImVec2& c, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left) { if (((col_upr_left | col_upr_right | col_bot_right | col_bot_left) & IM_COL32_A_MASK) == 0) return; const ImVec2 uv = _Data->TexUvWhitePixel; PrimReserve(6, 4); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+1)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+3)); PrimWriteVtx(a, uv, col_upr_left); PrimWriteVtx(ImVec2(c.x, a.y), uv, col_upr_right); PrimWriteVtx(c, uv, col_bot_right); PrimWriteVtx(ImVec2(a.x, c.y), uv, col_bot_left); } void ImDrawList::AddQuad(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, ImU32 col, float thickness) { if ((col & IM_COL32_A_MASK) == 0) return; PathLineTo(a); PathLineTo(b); PathLineTo(c); PathLineTo(d); PathStroke(col, true, thickness); } void ImDrawList::AddQuadFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, ImU32 col) { if ((col & IM_COL32_A_MASK) == 0) return; PathLineTo(a); PathLineTo(b); PathLineTo(c); PathLineTo(d); PathFillConvex(col); } void ImDrawList::AddTriangle(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col, float thickness) { if ((col & IM_COL32_A_MASK) == 0) return; PathLineTo(a); PathLineTo(b); PathLineTo(c); PathStroke(col, true, thickness); } void ImDrawList::AddTriangleFilled(const ImVec2& a, const ImVec2& b, const ImVec2& c, ImU32 col) { if ((col & IM_COL32_A_MASK) == 0) return; PathLineTo(a); PathLineTo(b); PathLineTo(c); PathFillConvex(col); } void ImDrawList::AddCircle(const ImVec2& centre, float radius, ImU32 col, int num_segments, float thickness) { if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2) return; // Because we are filling a closed shape we remove 1 from the count of segments/points const float a_max = IM_PI*2.0f * ((float)num_segments - 1.0f) / (float)num_segments; PathArcTo(centre, radius-0.5f, 0.0f, a_max, num_segments - 1); PathStroke(col, true, thickness); } void ImDrawList::AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, int num_segments) { if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2) return; // Because we are filling a closed shape we remove 1 from the count of segments/points const float a_max = IM_PI*2.0f * ((float)num_segments - 1.0f) / (float)num_segments; PathArcTo(centre, radius, 0.0f, a_max, num_segments - 1); PathFillConvex(col); } void ImDrawList::AddBezierCurve(const ImVec2& pos0, const ImVec2& cp0, const ImVec2& cp1, const ImVec2& pos1, ImU32 col, float thickness, int num_segments) { if ((col & IM_COL32_A_MASK) == 0) return; PathLineTo(pos0); PathBezierCurveTo(cp0, cp1, pos1, num_segments); PathStroke(col, false, thickness); } void 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) { if ((col & IM_COL32_A_MASK) == 0) return; if (text_end == NULL) text_end = text_begin + strlen(text_begin); if (text_begin == text_end) return; // Pull default font/size from the shared ImDrawListSharedData instance if (font == NULL) font = _Data->Font; if (font_size == 0.0f) font_size = _Data->FontSize; IM_ASSERT(font->ContainerAtlas->TexID == _TextureIdStack.back()); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font. ImVec4 clip_rect = _ClipRectStack.back(); if (cpu_fine_clip_rect) { clip_rect.x = ImMax(clip_rect.x, cpu_fine_clip_rect->x); clip_rect.y = ImMax(clip_rect.y, cpu_fine_clip_rect->y); clip_rect.z = ImMin(clip_rect.z, cpu_fine_clip_rect->z); clip_rect.w = ImMin(clip_rect.w, cpu_fine_clip_rect->w); } font->RenderText(this, font_size, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip_rect != NULL); } void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end) { AddText(NULL, 0.0f, pos, col, text_begin, text_end); } void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, ImU32 col) { if ((col & IM_COL32_A_MASK) == 0) return; const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back(); if (push_texture_id) PushTextureID(user_texture_id); PrimReserve(6, 4); PrimRectUV(a, b, uv_a, uv_b, col); if (push_texture_id) PopTextureID(); } void 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) { if ((col & IM_COL32_A_MASK) == 0) return; const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back(); if (push_texture_id) PushTextureID(user_texture_id); PrimReserve(6, 4); PrimQuadUV(a, b, c, d, uv_a, uv_b, uv_c, uv_d, col); if (push_texture_id) PopTextureID(); } void 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) { if ((col & IM_COL32_A_MASK) == 0) return; if (rounding <= 0.0f || (rounding_corners & ImDrawCornerFlags_All) == 0) { AddImage(user_texture_id, a, b, uv_a, uv_b, col); return; } const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back(); if (push_texture_id) PushTextureID(user_texture_id); int vert_start_idx = VtxBuffer.Size; PathRect(a, b, rounding, rounding_corners); PathFillConvex(col); int vert_end_idx = VtxBuffer.Size; ImGui::ShadeVertsLinearUV(this, vert_start_idx, vert_end_idx, a, b, uv_a, uv_b, true); if (push_texture_id) PopTextureID(); } //----------------------------------------------------------------------------- // [SECTION] ImDrawData //----------------------------------------------------------------------------- // 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! void ImDrawData::DeIndexAllBuffers() { ImVector new_vtx_buffer; TotalVtxCount = TotalIdxCount = 0; for (int i = 0; i < CmdListsCount; i++) { ImDrawList* cmd_list = CmdLists[i]; if (cmd_list->IdxBuffer.empty()) continue; new_vtx_buffer.resize(cmd_list->IdxBuffer.Size); for (int j = 0; j < cmd_list->IdxBuffer.Size; j++) new_vtx_buffer[j] = cmd_list->VtxBuffer[cmd_list->IdxBuffer[j]]; cmd_list->VtxBuffer.swap(new_vtx_buffer); cmd_list->IdxBuffer.resize(0); TotalVtxCount += cmd_list->VtxBuffer.Size; } } // Helper to scale the ClipRect field of each ImDrawCmd. // Use if your final output buffer is at a different scale than draw_data->DisplaySize, // or if there is a difference between your window resolution and framebuffer resolution. void ImDrawData::ScaleClipRects(const ImVec2& fb_scale) { for (int i = 0; i < CmdListsCount; i++) { ImDrawList* cmd_list = CmdLists[i]; for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { ImDrawCmd* cmd = &cmd_list->CmdBuffer[cmd_i]; 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); } } } //----------------------------------------------------------------------------- // [SECTION] Helpers ShadeVertsXXX functions //----------------------------------------------------------------------------- // Generic linear color gradient, write to RGB fields, leave A untouched. void ImGui::ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1) { ImVec2 gradient_extent = gradient_p1 - gradient_p0; float gradient_inv_length2 = 1.0f / ImLengthSqr(gradient_extent); ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; for (ImDrawVert* vert = vert_start; vert < vert_end; vert++) { float d = ImDot(vert->pos - gradient_p0, gradient_extent); float t = ImClamp(d * gradient_inv_length2, 0.0f, 1.0f); int r = ImLerp((int)(col0 >> IM_COL32_R_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_R_SHIFT) & 0xFF, t); int g = ImLerp((int)(col0 >> IM_COL32_G_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_G_SHIFT) & 0xFF, t); int b = ImLerp((int)(col0 >> IM_COL32_B_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_B_SHIFT) & 0xFF, t); vert->col = (r << IM_COL32_R_SHIFT) | (g << IM_COL32_G_SHIFT) | (b << IM_COL32_B_SHIFT) | (vert->col & IM_COL32_A_MASK); } } // Distribute UV over (a, b) rectangle void 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) { const ImVec2 size = b - a; const ImVec2 uv_size = uv_b - uv_a; const ImVec2 scale = ImVec2( size.x != 0.0f ? (uv_size.x / size.x) : 0.0f, size.y != 0.0f ? (uv_size.y / size.y) : 0.0f); ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; if (clamp) { const ImVec2 min = ImMin(uv_a, uv_b); const ImVec2 max = ImMax(uv_a, uv_b); for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) vertex->uv = ImClamp(uv_a + ImMul(ImVec2(vertex->pos.x, vertex->pos.y) - a, scale), min, max); } else { for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) vertex->uv = uv_a + ImMul(ImVec2(vertex->pos.x, vertex->pos.y) - a, scale); } } //----------------------------------------------------------------------------- // [SECTION] ImFontConfig //----------------------------------------------------------------------------- ImFontConfig::ImFontConfig() { FontData = NULL; FontDataSize = 0; FontDataOwnedByAtlas = true; FontNo = 0; SizePixels = 0.0f; OversampleH = 3; // FIXME: 2 may be a better default? OversampleV = 1; PixelSnapH = false; GlyphExtraSpacing = ImVec2(0.0f, 0.0f); GlyphOffset = ImVec2(0.0f, 0.0f); GlyphRanges = NULL; GlyphMinAdvanceX = 0.0f; GlyphMaxAdvanceX = FLT_MAX; MergeMode = false; RasterizerFlags = 0x00; RasterizerMultiply = 1.0f; memset(Name, 0, sizeof(Name)); DstFont = NULL; } //----------------------------------------------------------------------------- // [SECTION] ImFontAtlas //----------------------------------------------------------------------------- // A work of art lies ahead! (. = white layer, X = black layer, others are blank) // The white texels on the top left are the ones we'll use everywhere in ImGui to render filled shapes. const int FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF = 108; const int FONT_ATLAS_DEFAULT_TEX_DATA_H = 27; const unsigned int FONT_ATLAS_DEFAULT_TEX_DATA_ID = 0x80000000; static const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] = { "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX " "..- -X.....X- X.X - X.X -X.....X - X.....X- X..X " "--- -XXX.XXX- X...X - X...X -X....X - X....X- X..X " "X - X.X - X.....X - X.....X -X...X - X...X- X..X " "XX - X.X -X.......X- X.......X -X..X.X - X.X..X- X..X " "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X- X..XXX " "X..X - X.X - X.X - X.X -XX X.X - X.X XX- X..X..XXX " "X...X - X.X - X.X - XX X.X XX - X.X - X.X - X..X..X..XX " "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X - X..X..X..X.X " "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X -XXX X..X..X..X..X" "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X -X..XX........X..X" "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X -X...X...........X" "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X - X..............X" "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X - X.............X" "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X - X.............X" "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X - X............X" "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX - X...........X " "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------- X..........X " "X.X X..X - -X.......X- X.......X - XX XX - - X..........X " "XX X..X - - X.....X - X.....X - X.X X.X - - X........X " " X..X - X...X - X...X - X..X X..X - - X........X " " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - - XXXXXXXXXX " "------------ - X - X -X.....................X- ------------------" " ----------------------------------- X...XXXXXXXXXXXXX...X - " " - X..X X..X - " " - X.X X.X - " " - XX XX - " }; static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3] = { // Pos ........ Size ......... Offset ...... { ImVec2( 0,3), ImVec2(12,19), ImVec2( 0, 0) }, // ImGuiMouseCursor_Arrow { ImVec2(13,0), ImVec2( 7,16), ImVec2( 1, 8) }, // ImGuiMouseCursor_TextInput { ImVec2(31,0), ImVec2(23,23), ImVec2(11,11) }, // ImGuiMouseCursor_ResizeAll { ImVec2(21,0), ImVec2( 9,23), ImVec2( 4,11) }, // ImGuiMouseCursor_ResizeNS { ImVec2(55,18),ImVec2(23, 9), ImVec2(11, 4) }, // ImGuiMouseCursor_ResizeEW { ImVec2(73,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNESW { ImVec2(55,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNWSE { ImVec2(91,0), ImVec2(17,22), ImVec2( 5, 0) }, // ImGuiMouseCursor_Hand }; ImFontAtlas::ImFontAtlas() { Locked = false; Flags = ImFontAtlasFlags_None; TexID = (ImTextureID)NULL; TexDesiredWidth = 0; TexGlyphPadding = 1; TexPixelsAlpha8 = NULL; TexPixelsRGBA32 = NULL; TexWidth = TexHeight = 0; TexUvScale = ImVec2(0.0f, 0.0f); TexUvWhitePixel = ImVec2(0.0f, 0.0f); for (int n = 0; n < IM_ARRAYSIZE(CustomRectIds); n++) CustomRectIds[n] = -1; } ImFontAtlas::~ImFontAtlas() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); Clear(); } void ImFontAtlas::ClearInputData() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); for (int i = 0; i < ConfigData.Size; i++) if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas) { IM_FREE(ConfigData[i].FontData); ConfigData[i].FontData = NULL; } // When clearing this we lose access to the font name and other information used to build the font. for (int i = 0; i < Fonts.Size; i++) if (Fonts[i]->ConfigData >= ConfigData.Data && Fonts[i]->ConfigData < ConfigData.Data + ConfigData.Size) { Fonts[i]->ConfigData = NULL; Fonts[i]->ConfigDataCount = 0; } ConfigData.clear(); CustomRects.clear(); for (int n = 0; n < IM_ARRAYSIZE(CustomRectIds); n++) CustomRectIds[n] = -1; } void ImFontAtlas::ClearTexData() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); if (TexPixelsAlpha8) IM_FREE(TexPixelsAlpha8); if (TexPixelsRGBA32) IM_FREE(TexPixelsRGBA32); TexPixelsAlpha8 = NULL; TexPixelsRGBA32 = NULL; } void ImFontAtlas::ClearFonts() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); for (int i = 0; i < Fonts.Size; i++) IM_DELETE(Fonts[i]); Fonts.clear(); } void ImFontAtlas::Clear() { ClearInputData(); ClearTexData(); ClearFonts(); } void ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel) { // Build atlas on demand if (TexPixelsAlpha8 == NULL) { if (ConfigData.empty()) AddFontDefault(); Build(); } *out_pixels = TexPixelsAlpha8; if (out_width) *out_width = TexWidth; if (out_height) *out_height = TexHeight; if (out_bytes_per_pixel) *out_bytes_per_pixel = 1; } void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel) { // Convert to RGBA32 format on demand // Although it is likely to be the most commonly used format, our font rendering is 1 channel / 8 bpp if (!TexPixelsRGBA32) { unsigned char* pixels = NULL; GetTexDataAsAlpha8(&pixels, NULL, NULL); if (pixels) { TexPixelsRGBA32 = (unsigned int*)IM_ALLOC((size_t)TexWidth * (size_t)TexHeight * 4); const unsigned char* src = pixels; unsigned int* dst = TexPixelsRGBA32; for (int n = TexWidth * TexHeight; n > 0; n--) *dst++ = IM_COL32(255, 255, 255, (unsigned int)(*src++)); } } *out_pixels = (unsigned char*)TexPixelsRGBA32; if (out_width) *out_width = TexWidth; if (out_height) *out_height = TexHeight; if (out_bytes_per_pixel) *out_bytes_per_pixel = 4; } ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0); IM_ASSERT(font_cfg->SizePixels > 0.0f); // Create new font if (!font_cfg->MergeMode) Fonts.push_back(IM_NEW(ImFont)); else 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. ConfigData.push_back(*font_cfg); ImFontConfig& new_font_cfg = ConfigData.back(); if (new_font_cfg.DstFont == NULL) new_font_cfg.DstFont = Fonts.back(); if (!new_font_cfg.FontDataOwnedByAtlas) { new_font_cfg.FontData = IM_ALLOC(new_font_cfg.FontDataSize); new_font_cfg.FontDataOwnedByAtlas = true; memcpy(new_font_cfg.FontData, font_cfg->FontData, (size_t)new_font_cfg.FontDataSize); } // Invalidate texture ClearTexData(); return new_font_cfg.DstFont; } // Default font TTF is compressed with stb_compress then base85 encoded (see misc/fonts/binary_to_compressed_c.cpp for encoder) static unsigned int stb_decompress_length(const unsigned char *input); static unsigned int stb_decompress(unsigned char *output, const unsigned char *input, unsigned int length); static const char* GetDefaultCompressedFontDataTTFBase85(); static unsigned int Decode85Byte(char c) { return c >= '\\' ? c-36 : c-35; } static void Decode85(const unsigned char* src, unsigned char* dst) { while (*src) { unsigned int tmp = Decode85Byte(src[0]) + 85*(Decode85Byte(src[1]) + 85*(Decode85Byte(src[2]) + 85*(Decode85Byte(src[3]) + 85*Decode85Byte(src[4])))); 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. src += 5; dst += 4; } } // Load embedded ProggyClean.ttf at size 13, disable oversampling ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template) { ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); if (!font_cfg_template) { font_cfg.OversampleH = font_cfg.OversampleV = 1; font_cfg.PixelSnapH = true; } if (font_cfg.SizePixels <= 0.0f) font_cfg.SizePixels = 13.0f * 1.0f; if (font_cfg.Name[0] == '\0') ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "ProggyClean.ttf, %dpx", (int)font_cfg.SizePixels); const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85(); const ImWchar* glyph_ranges = font_cfg.GlyphRanges != NULL ? font_cfg.GlyphRanges : GetGlyphRangesDefault(); ImFont* font = AddFontFromMemoryCompressedBase85TTF(ttf_compressed_base85, font_cfg.SizePixels, &font_cfg, glyph_ranges); font->DisplayOffset.y = 1.0f; return font; } ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); size_t data_size = 0; void* data = ImFileLoadToMemory(filename, "rb", &data_size, 0); if (!data) { IM_ASSERT(0); // Could not load file. return NULL; } ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); if (font_cfg.Name[0] == '\0') { // Store a short copy of filename into into the font name for convenience const char* p; for (p = filename + strlen(filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {} ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", p, size_pixels); } return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges); } // NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build(). ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); IM_ASSERT(font_cfg.FontData == NULL); font_cfg.FontData = ttf_data; font_cfg.FontDataSize = ttf_size; font_cfg.SizePixels = size_pixels; if (glyph_ranges) font_cfg.GlyphRanges = glyph_ranges; return AddFont(&font_cfg); } ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) { const unsigned int buf_decompressed_size = stb_decompress_length((const unsigned char*)compressed_ttf_data); unsigned char* buf_decompressed_data = (unsigned char *)IM_ALLOC(buf_decompressed_size); stb_decompress(buf_decompressed_data, (const unsigned char*)compressed_ttf_data, (unsigned int)compressed_ttf_size); ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); IM_ASSERT(font_cfg.FontData == NULL); font_cfg.FontDataOwnedByAtlas = true; return AddFontFromMemoryTTF(buf_decompressed_data, (int)buf_decompressed_size, size_pixels, &font_cfg, glyph_ranges); } ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges) { int compressed_ttf_size = (((int)strlen(compressed_ttf_data_base85) + 4) / 5) * 4; void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size); Decode85((const unsigned char*)compressed_ttf_data_base85, (unsigned char*)compressed_ttf); ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges); IM_FREE(compressed_ttf); return font; } int ImFontAtlas::AddCustomRectRegular(unsigned int id, int width, int height) { IM_ASSERT(id >= 0x10000); IM_ASSERT(width > 0 && width <= 0xFFFF); IM_ASSERT(height > 0 && height <= 0xFFFF); CustomRect r; r.ID = id; r.Width = (unsigned short)width; r.Height = (unsigned short)height; CustomRects.push_back(r); return CustomRects.Size - 1; // Return index } int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset) { IM_ASSERT(font != NULL); IM_ASSERT(width > 0 && width <= 0xFFFF); IM_ASSERT(height > 0 && height <= 0xFFFF); CustomRect r; r.ID = id; r.Width = (unsigned short)width; r.Height = (unsigned short)height; r.GlyphAdvanceX = advance_x; r.GlyphOffset = offset; r.Font = font; CustomRects.push_back(r); return CustomRects.Size - 1; // Return index } void ImFontAtlas::CalcCustomRectUV(const CustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) { IM_ASSERT(TexWidth > 0 && TexHeight > 0); // Font atlas needs to be built before we can calculate UV coordinates IM_ASSERT(rect->IsPacked()); // Make sure the rectangle has been packed *out_uv_min = ImVec2((float)rect->X * TexUvScale.x, (float)rect->Y * TexUvScale.y); *out_uv_max = ImVec2((float)(rect->X + rect->Width) * TexUvScale.x, (float)(rect->Y + rect->Height) * TexUvScale.y); } bool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2]) { if (cursor_type <= ImGuiMouseCursor_None || cursor_type >= ImGuiMouseCursor_COUNT) return false; if (Flags & ImFontAtlasFlags_NoMouseCursors) return false; IM_ASSERT(CustomRectIds[0] != -1); ImFontAtlas::CustomRect& r = CustomRects[CustomRectIds[0]]; IM_ASSERT(r.ID == FONT_ATLAS_DEFAULT_TEX_DATA_ID); ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r.X, (float)r.Y); ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][1]; *out_size = size; *out_offset = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][2]; out_uv_border[0] = (pos) * TexUvScale; out_uv_border[1] = (pos + size) * TexUvScale; pos.x += FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1; out_uv_fill[0] = (pos) * TexUvScale; out_uv_fill[1] = (pos + size) * TexUvScale; return true; } bool ImFontAtlas::Build() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); return ImFontAtlasBuildWithStbTruetype(this); } void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_brighten_factor) { for (unsigned int i = 0; i < 256; i++) { unsigned int value = (unsigned int)(i * in_brighten_factor); out_table[i] = value > 255 ? 255 : (value & 0xFF); } } void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride) { unsigned char* data = pixels + x + y * stride; for (int j = h; j > 0; j--, data += stride) for (int i = 0; i < w; i++) data[i] = table[data[i]]; } // Temporary data for one source font (multiple source fonts can be merged into one destination ImFont) // (C++03 doesn't allow instancing ImVector<> with function-local types so we declare the type here.) struct ImFontBuildSrcData { stbtt_fontinfo FontInfo; stbtt_pack_range PackRange; // Hold the list of codepoints to pack (essentially points to Codepoints.Data) stbrp_rect* Rects; // Rectangle to pack. We first fill in their size and the packer will give us their position. stbtt_packedchar* PackedChars; // Output glyphs const ImWchar* SrcRanges; // Ranges as requested by user (user is allowed to request too much, e.g. 0x0020..0xFFFF) int DstIndex; // Index into atlas->Fonts[] and dst_tmp_array[] int GlyphsHighest; // Highest requested codepoint int GlyphsCount; // Glyph count (excluding missing glyphs and glyphs already set by an earlier source font) ImBoolVector GlyphsSet; // Glyph bit map (random access, 1-bit per codepoint. This will be a maximum of 8KB) ImVector GlyphsList; // Glyph codepoints list (flattened version of GlyphsMap) }; // Temporary data for one destination ImFont* (multiple source fonts can be merged into one destination ImFont) struct ImFontBuildDstData { int SrcCount; // Number of source fonts targeting this destination font. int GlyphsHighest; int GlyphsCount; ImBoolVector GlyphsSet; // This is used to resolve collision when multiple sources are merged into a same destination font. }; static void UnpackBoolVectorToFlatIndexList(const ImBoolVector* in, ImVector* out) { IM_ASSERT(sizeof(in->Storage.Data[0]) == sizeof(int)); const int* it_begin = in->Storage.begin(); const int* it_end = in->Storage.end(); for (const int* it = it_begin; it < it_end; it++) if (int entries_32 = *it) for (int bit_n = 0; bit_n < 32; bit_n++) if (entries_32 & (1 << bit_n)) out->push_back((int)((it - it_begin) << 5) + bit_n); } bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) { IM_ASSERT(atlas->ConfigData.Size > 0); ImFontAtlasBuildRegisterDefaultCustomRects(atlas); // Clear atlas atlas->TexID = (ImTextureID)NULL; atlas->TexWidth = atlas->TexHeight = 0; atlas->TexUvScale = ImVec2(0.0f, 0.0f); atlas->TexUvWhitePixel = ImVec2(0.0f, 0.0f); atlas->ClearTexData(); // Temporary storage for building ImVector src_tmp_array; ImVector dst_tmp_array; src_tmp_array.resize(atlas->ConfigData.Size); dst_tmp_array.resize(atlas->Fonts.Size); memset(src_tmp_array.Data, 0, (size_t)src_tmp_array.size_in_bytes()); memset(dst_tmp_array.Data, 0, (size_t)dst_tmp_array.size_in_bytes()); // 1. Initialize font loading structure, check font data validity for (int src_i = 0; src_i < atlas->ConfigData.Size; src_i++) { ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; ImFontConfig& cfg = atlas->ConfigData[src_i]; IM_ASSERT(cfg.DstFont && (!cfg.DstFont->IsLoaded() || cfg.DstFont->ContainerAtlas == atlas)); // Find index from cfg.DstFont (we allow the user to set cfg.DstFont. Also it makes casual debugging nicer than when storing indices) src_tmp.DstIndex = -1; for (int output_i = 0; output_i < atlas->Fonts.Size && src_tmp.DstIndex == -1; output_i++) if (cfg.DstFont == atlas->Fonts[output_i]) src_tmp.DstIndex = output_i; IM_ASSERT(src_tmp.DstIndex != -1); // cfg.DstFont not pointing within atlas->Fonts[] array? if (src_tmp.DstIndex == -1) return false; // Initialize helper structure for font loading and verify that the TTF/OTF data is correct const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)cfg.FontData, cfg.FontNo); IM_ASSERT(font_offset >= 0 && "FontData is incorrect, or FontNo cannot be found."); if (!stbtt_InitFont(&src_tmp.FontInfo, (unsigned char*)cfg.FontData, font_offset)) return false; // Measure highest codepoints ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex]; src_tmp.SrcRanges = cfg.GlyphRanges ? cfg.GlyphRanges : atlas->GetGlyphRangesDefault(); for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2) src_tmp.GlyphsHighest = ImMax(src_tmp.GlyphsHighest, (int)src_range[1]); dst_tmp.SrcCount++; dst_tmp.GlyphsHighest = ImMax(dst_tmp.GlyphsHighest, src_tmp.GlyphsHighest); } // 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. int total_glyphs_count = 0; for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) { ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex]; src_tmp.GlyphsSet.Resize(src_tmp.GlyphsHighest + 1); if (dst_tmp.GlyphsSet.Storage.empty()) dst_tmp.GlyphsSet.Resize(dst_tmp.GlyphsHighest + 1); for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2) for (int codepoint = src_range[0]; codepoint <= src_range[1]; codepoint++) { if (dst_tmp.GlyphsSet.GetBit(codepoint)) // Don't overwrite existing glyphs. We could make this an option for MergeMode (e.g. MergeOverwrite==true) continue; if (!stbtt_FindGlyphIndex(&src_tmp.FontInfo, codepoint)) // It is actually in the font? continue; // Add to avail set/counters src_tmp.GlyphsCount++; dst_tmp.GlyphsCount++; src_tmp.GlyphsSet.SetBit(codepoint, true); dst_tmp.GlyphsSet.SetBit(codepoint, true); total_glyphs_count++; } } // 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) for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) { ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; src_tmp.GlyphsList.reserve(src_tmp.GlyphsCount); UnpackBoolVectorToFlatIndexList(&src_tmp.GlyphsSet, &src_tmp.GlyphsList); src_tmp.GlyphsSet.Clear(); IM_ASSERT(src_tmp.GlyphsList.Size == src_tmp.GlyphsCount); } for (int dst_i = 0; dst_i < dst_tmp_array.Size; dst_i++) dst_tmp_array[dst_i].GlyphsSet.Clear(); dst_tmp_array.clear(); // Allocate packing character data and flag packed characters buffer as non-packed (x0=y0=x1=y1=0) // (We technically don't need to zero-clear buf_rects, but let's do it for the sake of sanity) ImVector buf_rects; ImVector buf_packedchars; buf_rects.resize(total_glyphs_count); buf_packedchars.resize(total_glyphs_count); memset(buf_rects.Data, 0, (size_t)buf_rects.size_in_bytes()); memset(buf_packedchars.Data, 0, (size_t)buf_packedchars.size_in_bytes()); // 4. Gather glyphs sizes so we can pack them in our virtual canvas. int total_surface = 0; int buf_rects_out_n = 0; int buf_packedchars_out_n = 0; for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) { ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; if (src_tmp.GlyphsCount == 0) continue; src_tmp.Rects = &buf_rects[buf_rects_out_n]; src_tmp.PackedChars = &buf_packedchars[buf_packedchars_out_n]; buf_rects_out_n += src_tmp.GlyphsCount; buf_packedchars_out_n += src_tmp.GlyphsCount; // Convert our ranges in the format stb_truetype wants ImFontConfig& cfg = atlas->ConfigData[src_i]; src_tmp.PackRange.font_size = cfg.SizePixels; src_tmp.PackRange.first_unicode_codepoint_in_range = 0; src_tmp.PackRange.array_of_unicode_codepoints = src_tmp.GlyphsList.Data; src_tmp.PackRange.num_chars = src_tmp.GlyphsList.Size; src_tmp.PackRange.chardata_for_range = src_tmp.PackedChars; src_tmp.PackRange.h_oversample = (unsigned char)cfg.OversampleH; src_tmp.PackRange.v_oversample = (unsigned char)cfg.OversampleV; // Gather the sizes of all rectangles we will need to pack (this loop is based on stbtt_PackFontRangesGatherRects) const float scale = (cfg.SizePixels > 0) ? stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, cfg.SizePixels) : stbtt_ScaleForMappingEmToPixels(&src_tmp.FontInfo, -cfg.SizePixels); const int padding = atlas->TexGlyphPadding; for (int glyph_i = 0; glyph_i < src_tmp.GlyphsList.Size; glyph_i++) { int x0, y0, x1, y1; const int glyph_index_in_font = stbtt_FindGlyphIndex(&src_tmp.FontInfo, src_tmp.GlyphsList[glyph_i]); IM_ASSERT(glyph_index_in_font != 0); stbtt_GetGlyphBitmapBoxSubpixel(&src_tmp.FontInfo, glyph_index_in_font, scale * cfg.OversampleH, scale * cfg.OversampleV, 0, 0, &x0, &y0, &x1, &y1); src_tmp.Rects[glyph_i].w = (stbrp_coord)(x1 - x0 + padding + cfg.OversampleH - 1); src_tmp.Rects[glyph_i].h = (stbrp_coord)(y1 - y0 + padding + cfg.OversampleV - 1); total_surface += src_tmp.Rects[glyph_i].w * src_tmp.Rects[glyph_i].h; } } // We need a width for the skyline algorithm, any width! // The exact width doesn't really matter much, but some API/GPU have texture size limitations and increasing width can decrease height. // User can override TexDesiredWidth and TexGlyphPadding if they wish, otherwise we use a simple heuristic to select the width based on expected surface. const int surface_sqrt = (int)ImSqrt((float)total_surface) + 1; atlas->TexHeight = 0; if (atlas->TexDesiredWidth > 0) atlas->TexWidth = atlas->TexDesiredWidth; else atlas->TexWidth = (surface_sqrt >= 4096*0.7f) ? 4096 : (surface_sqrt >= 2048*0.7f) ? 2048 : (surface_sqrt >= 1024*0.7f) ? 1024 : 512; // 5. Start packing // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values). const int TEX_HEIGHT_MAX = 1024 * 32; stbtt_pack_context spc = {}; stbtt_PackBegin(&spc, NULL, atlas->TexWidth, TEX_HEIGHT_MAX, 0, atlas->TexGlyphPadding, NULL); ImFontAtlasBuildPackCustomRects(atlas, spc.pack_info); // 6. Pack each source font. No rendering yet, we are working with rectangles in an infinitely tall texture at this point. for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) { ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; if (src_tmp.GlyphsCount == 0) continue; stbrp_pack_rects((stbrp_context*)spc.pack_info, src_tmp.Rects, src_tmp.GlyphsCount); // Extend texture height and mark missing glyphs as non-packed so we won't render them. // 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?) for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++) if (src_tmp.Rects[glyph_i].was_packed) atlas->TexHeight = ImMax(atlas->TexHeight, src_tmp.Rects[glyph_i].y + src_tmp.Rects[glyph_i].h); } // 7. Allocate texture atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(atlas->TexHeight); atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight); atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(atlas->TexWidth * atlas->TexHeight); memset(atlas->TexPixelsAlpha8, 0, atlas->TexWidth * atlas->TexHeight); spc.pixels = atlas->TexPixelsAlpha8; spc.height = atlas->TexHeight; // 8. Render/rasterize font characters into the texture for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) { ImFontConfig& cfg = atlas->ConfigData[src_i]; ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; if (src_tmp.GlyphsCount == 0) continue; stbtt_PackFontRangesRenderIntoRects(&spc, &src_tmp.FontInfo, &src_tmp.PackRange, 1, src_tmp.Rects); // Apply multiply operator if (cfg.RasterizerMultiply != 1.0f) { unsigned char multiply_table[256]; ImFontAtlasBuildMultiplyCalcLookupTable(multiply_table, cfg.RasterizerMultiply); stbrp_rect* r = &src_tmp.Rects[0]; for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++, r++) if (r->was_packed) ImFontAtlasBuildMultiplyRectAlpha8(multiply_table, atlas->TexPixelsAlpha8, r->x, r->y, r->w, r->h, atlas->TexWidth * 1); } src_tmp.Rects = NULL; } // End packing stbtt_PackEnd(&spc); buf_rects.clear(); // 9. Setup ImFont and glyphs for runtime for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) { ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; if (src_tmp.GlyphsCount == 0) continue; ImFontConfig& cfg = atlas->ConfigData[src_i]; ImFont* dst_font = cfg.DstFont; // We can have multiple input fonts writing into a same destination font (when using MergeMode=true) const float font_scale = stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, cfg.SizePixels); int unscaled_ascent, unscaled_descent, unscaled_line_gap; stbtt_GetFontVMetrics(&src_tmp.FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap); const float ascent = ImFloor(unscaled_ascent * font_scale + ((unscaled_ascent > 0.0f) ? +1 : -1)); const float descent = ImFloor(unscaled_descent * font_scale + ((unscaled_descent > 0.0f) ? +1 : -1)); ImFontAtlasBuildSetupFont(atlas, dst_font, &cfg, ascent, descent); const float font_off_x = cfg.GlyphOffset.x; const float font_off_y = cfg.GlyphOffset.y + (float)(int)(dst_font->Ascent + 0.5f); for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++) { const int codepoint = src_tmp.GlyphsList[glyph_i]; const stbtt_packedchar& pc = src_tmp.PackedChars[glyph_i]; const float char_advance_x_org = pc.xadvance; const float char_advance_x_mod = ImClamp(char_advance_x_org, cfg.GlyphMinAdvanceX, cfg.GlyphMaxAdvanceX); float char_off_x = font_off_x; if (char_advance_x_org != char_advance_x_mod) 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; // Register glyph stbtt_aligned_quad q; float dummy_x = 0.0f, dummy_y = 0.0f; stbtt_GetPackedQuad(src_tmp.PackedChars, atlas->TexWidth, atlas->TexHeight, glyph_i, &dummy_x, &dummy_y, &q, 0); 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); } } // Cleanup temporary (ImVector doesn't honor destructor) for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) src_tmp_array[src_i].~ImFontBuildSrcData(); ImFontAtlasBuildFinish(atlas); return true; } void ImFontAtlasBuildRegisterDefaultCustomRects(ImFontAtlas* atlas) { if (atlas->CustomRectIds[0] >= 0) return; if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors)) 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); else atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_ID, 2, 2); } void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent) { if (!font_config->MergeMode) { font->ClearOutputData(); font->FontSize = font_config->SizePixels; font->ConfigData = font_config; font->ContainerAtlas = atlas; font->Ascent = ascent; font->Descent = descent; } font->ConfigDataCount++; } void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque) { stbrp_context* pack_context = (stbrp_context*)stbrp_context_opaque; IM_ASSERT(pack_context != NULL); ImVector& user_rects = atlas->CustomRects; IM_ASSERT(user_rects.Size >= 1); // We expect at least the default custom rects to be registered, else something went wrong. ImVector pack_rects; pack_rects.resize(user_rects.Size); memset(pack_rects.Data, 0, (size_t)pack_rects.size_in_bytes()); for (int i = 0; i < user_rects.Size; i++) { pack_rects[i].w = user_rects[i].Width; pack_rects[i].h = user_rects[i].Height; } stbrp_pack_rects(pack_context, &pack_rects[0], pack_rects.Size); for (int i = 0; i < pack_rects.Size; i++) if (pack_rects[i].was_packed) { user_rects[i].X = pack_rects[i].x; user_rects[i].Y = pack_rects[i].y; IM_ASSERT(pack_rects[i].w == user_rects[i].Width && pack_rects[i].h == user_rects[i].Height); atlas->TexHeight = ImMax(atlas->TexHeight, pack_rects[i].y + pack_rects[i].h); } } static void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas) { IM_ASSERT(atlas->CustomRectIds[0] >= 0); IM_ASSERT(atlas->TexPixelsAlpha8 != NULL); ImFontAtlas::CustomRect& r = atlas->CustomRects[atlas->CustomRectIds[0]]; IM_ASSERT(r.ID == FONT_ATLAS_DEFAULT_TEX_DATA_ID); IM_ASSERT(r.IsPacked()); const int w = atlas->TexWidth; if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors)) { // Render/copy pixels IM_ASSERT(r.Width == FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF * 2 + 1 && r.Height == FONT_ATLAS_DEFAULT_TEX_DATA_H); for (int y = 0, n = 0; y < FONT_ATLAS_DEFAULT_TEX_DATA_H; y++) for (int x = 0; x < FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF; x++, n++) { const int offset0 = (int)(r.X + x) + (int)(r.Y + y) * w; const int offset1 = offset0 + FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1; atlas->TexPixelsAlpha8[offset0] = FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[n] == '.' ? 0xFF : 0x00; atlas->TexPixelsAlpha8[offset1] = FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[n] == 'X' ? 0xFF : 0x00; } } else { IM_ASSERT(r.Width == 2 && r.Height == 2); const int offset = (int)(r.X) + (int)(r.Y) * w; atlas->TexPixelsAlpha8[offset] = atlas->TexPixelsAlpha8[offset + 1] = atlas->TexPixelsAlpha8[offset + w] = atlas->TexPixelsAlpha8[offset + w + 1] = 0xFF; } atlas->TexUvWhitePixel = ImVec2((r.X + 0.5f) * atlas->TexUvScale.x, (r.Y + 0.5f) * atlas->TexUvScale.y); } void ImFontAtlasBuildFinish(ImFontAtlas* atlas) { // Render into our custom data block ImFontAtlasBuildRenderDefaultTexData(atlas); // Register custom rectangle glyphs for (int i = 0; i < atlas->CustomRects.Size; i++) { const ImFontAtlas::CustomRect& r = atlas->CustomRects[i]; if (r.Font == NULL || r.ID > 0x10000) continue; IM_ASSERT(r.Font->ContainerAtlas == atlas); ImVec2 uv0, uv1; atlas->CalcCustomRectUV(&r, &uv0, &uv1); 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); } // Build all fonts lookup tables for (int i = 0; i < atlas->Fonts.Size; i++) if (atlas->Fonts[i]->DirtyLookupTables) atlas->Fonts[i]->BuildLookupTable(); } // Retrieve list of range (2 int per range, values are inclusive) const ImWchar* ImFontAtlas::GetGlyphRangesDefault() { static const ImWchar ranges[] = { 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0, }; return &ranges[0]; } const ImWchar* ImFontAtlas::GetGlyphRangesKorean() { static const ImWchar ranges[] = { 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x3131, 0x3163, // Korean alphabets 0xAC00, 0xD79D, // Korean characters 0, }; return &ranges[0]; } const ImWchar* ImFontAtlas::GetGlyphRangesChineseFull() { static const ImWchar ranges[] = { 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x2000, 0x206F, // General Punctuation 0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana 0x31F0, 0x31FF, // Katakana Phonetic Extensions 0xFF00, 0xFFEF, // Half-width characters 0x4e00, 0x9FAF, // CJK Ideograms 0, }; return &ranges[0]; } static void UnpackAccumulativeOffsetsIntoRanges(int base_codepoint, const short* accumulative_offsets, int accumulative_offsets_count, ImWchar* out_ranges) { for (int n = 0; n < accumulative_offsets_count; n++, out_ranges += 2) { out_ranges[0] = out_ranges[1] = (ImWchar)(base_codepoint + accumulative_offsets[n]); base_codepoint += accumulative_offsets[n]; } out_ranges[0] = 0; } //------------------------------------------------------------------------- // [SECTION] ImFontAtlas glyph ranges helpers //------------------------------------------------------------------------- const ImWchar* ImFontAtlas::GetGlyphRangesChineseSimplifiedCommon() { // Store 2500 regularly used characters for Simplified Chinese. // 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 // This table covers 97.97% of all characters used during the month in July, 1987. // You can use ImFontGlyphRangesBuilder to create your own ranges derived from this, by merging existing ranges or adding new characters. // (Stored as accumulative offsets from the initial unicode codepoint 0x4E00. This encoding is designed to helps us compact the source code size.) static const short accumulative_offsets_from_0x4E00[] = { 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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 }; static ImWchar base_ranges[] = // not zero-terminated { 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x2000, 0x206F, // General Punctuation 0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana 0x31F0, 0x31FF, // Katakana Phonetic Extensions 0xFF00, 0xFFEF // Half-width characters }; static ImWchar full_ranges[IM_ARRAYSIZE(base_ranges) + IM_ARRAYSIZE(accumulative_offsets_from_0x4E00) * 2 + 1] = { 0 }; if (!full_ranges[0]) { memcpy(full_ranges, base_ranges, sizeof(base_ranges)); UnpackAccumulativeOffsetsIntoRanges(0x4E00, accumulative_offsets_from_0x4E00, IM_ARRAYSIZE(accumulative_offsets_from_0x4E00), full_ranges + IM_ARRAYSIZE(base_ranges)); } return &full_ranges[0]; } const ImWchar* ImFontAtlas::GetGlyphRangesJapanese() { // 1946 common ideograms code points for Japanese // Sourced from http://theinstructionlimit.com/common-kanji-character-ranges-for-xna-spritefont-rendering // FIXME: Source a list of the revised 2136 Joyo Kanji list from 2010 and rebuild this. // You can use ImFontGlyphRangesBuilder to create your own ranges derived from this, by merging existing ranges or adding new characters. // (Stored as accumulative offsets from the initial unicode codepoint 0x4E00. This encoding is designed to helps us compact the source code size.) static const short accumulative_offsets_from_0x4E00[] = { 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, }; static ImWchar base_ranges[] = // not zero-terminated { 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana 0x31F0, 0x31FF, // Katakana Phonetic Extensions 0xFF00, 0xFFEF // Half-width characters }; static ImWchar full_ranges[IM_ARRAYSIZE(base_ranges) + IM_ARRAYSIZE(accumulative_offsets_from_0x4E00)*2 + 1] = { 0 }; if (!full_ranges[0]) { memcpy(full_ranges, base_ranges, sizeof(base_ranges)); UnpackAccumulativeOffsetsIntoRanges(0x4E00, accumulative_offsets_from_0x4E00, IM_ARRAYSIZE(accumulative_offsets_from_0x4E00), full_ranges + IM_ARRAYSIZE(base_ranges)); } return &full_ranges[0]; } const ImWchar* ImFontAtlas::GetGlyphRangesCyrillic() { static const ImWchar ranges[] = { 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x0400, 0x052F, // Cyrillic + Cyrillic Supplement 0x2DE0, 0x2DFF, // Cyrillic Extended-A 0xA640, 0xA69F, // Cyrillic Extended-B 0, }; return &ranges[0]; } const ImWchar* ImFontAtlas::GetGlyphRangesThai() { static const ImWchar ranges[] = { 0x0020, 0x00FF, // Basic Latin 0x2010, 0x205E, // Punctuations 0x0E00, 0x0E7F, // Thai 0, }; return &ranges[0]; } const ImWchar* ImFontAtlas::GetGlyphRangesVietnamese() { static const ImWchar ranges[] = { 0x0020, 0x00FF, // Basic Latin 0x0102, 0x0103, 0x0110, 0x0111, 0x0128, 0x0129, 0x0168, 0x0169, 0x01A0, 0x01A1, 0x01AF, 0x01B0, 0x1EA0, 0x1EF9, 0, }; return &ranges[0]; } //----------------------------------------------------------------------------- // [SECTION] ImFontGlyphRangesBuilder //----------------------------------------------------------------------------- void ImFontGlyphRangesBuilder::AddText(const char* text, const char* text_end) { while (text_end ? (text < text_end) : *text) { unsigned int c = 0; int c_len = ImTextCharFromUtf8(&c, text, text_end); text += c_len; if (c_len == 0) break; if (c < 0x10000) AddChar((ImWchar)c); } } void ImFontGlyphRangesBuilder::AddRanges(const ImWchar* ranges) { for (; ranges[0]; ranges += 2) for (ImWchar c = ranges[0]; c <= ranges[1]; c++) AddChar(c); } void ImFontGlyphRangesBuilder::BuildRanges(ImVector* out_ranges) { for (int n = 0; n < 0x10000; n++) if (GetBit(n)) { out_ranges->push_back((ImWchar)n); while (n < 0x10000 && GetBit(n + 1)) n++; out_ranges->push_back((ImWchar)n); } out_ranges->push_back(0); } //----------------------------------------------------------------------------- // [SECTION] ImFont //----------------------------------------------------------------------------- ImFont::ImFont() { FontSize = 0.0f; FallbackAdvanceX = 0.0f; FallbackChar = (ImWchar)'?'; DisplayOffset = ImVec2(0.0f, 0.0f); FallbackGlyph = NULL; ContainerAtlas = NULL; ConfigData = NULL; ConfigDataCount = 0; DirtyLookupTables = false; Scale = 1.0f; Ascent = Descent = 0.0f; MetricsTotalSurface = 0; } ImFont::~ImFont() { ClearOutputData(); } void ImFont::ClearOutputData() { FontSize = 0.0f; FallbackAdvanceX = 0.0f; Glyphs.clear(); IndexAdvanceX.clear(); IndexLookup.clear(); FallbackGlyph = NULL; ContainerAtlas = NULL; DirtyLookupTables = true; Ascent = Descent = 0.0f; MetricsTotalSurface = 0; } void ImFont::BuildLookupTable() { int max_codepoint = 0; for (int i = 0; i != Glyphs.Size; i++) max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint); IM_ASSERT(Glyphs.Size < 0xFFFF); // -1 is reserved IndexAdvanceX.clear(); IndexLookup.clear(); DirtyLookupTables = false; GrowIndex(max_codepoint + 1); for (int i = 0; i < Glyphs.Size; i++) { int codepoint = (int)Glyphs[i].Codepoint; IndexAdvanceX[codepoint] = Glyphs[i].AdvanceX; IndexLookup[codepoint] = (ImWchar)i; } // Create a glyph to handle TAB // FIXME: Needs proper TAB handling but it needs to be contextualized (or we could arbitrary say that each string starts at "column 0" ?) if (FindGlyph((ImWchar)' ')) { if (Glyphs.back().Codepoint != '\t') // So we can call this function multiple times Glyphs.resize(Glyphs.Size + 1); ImFontGlyph& tab_glyph = Glyphs.back(); tab_glyph = *FindGlyph((ImWchar)' '); tab_glyph.Codepoint = '\t'; tab_glyph.AdvanceX *= IM_TABSIZE; IndexAdvanceX[(int)tab_glyph.Codepoint] = (float)tab_glyph.AdvanceX; IndexLookup[(int)tab_glyph.Codepoint] = (ImWchar)(Glyphs.Size-1); } FallbackGlyph = FindGlyphNoFallback(FallbackChar); FallbackAdvanceX = FallbackGlyph ? FallbackGlyph->AdvanceX : 0.0f; for (int i = 0; i < max_codepoint + 1; i++) if (IndexAdvanceX[i] < 0.0f) IndexAdvanceX[i] = FallbackAdvanceX; } void ImFont::SetFallbackChar(ImWchar c) { FallbackChar = c; BuildLookupTable(); } void ImFont::GrowIndex(int new_size) { IM_ASSERT(IndexAdvanceX.Size == IndexLookup.Size); if (new_size <= IndexLookup.Size) return; IndexAdvanceX.resize(new_size, -1.0f); IndexLookup.resize(new_size, (ImWchar)-1); } // x0/y0/x1/y1 are offset from the character upper-left layout position, in pixels. Therefore x0/y0 are often fairly close to zero. // 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). void ImFont::AddGlyph(ImWchar codepoint, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x) { Glyphs.resize(Glyphs.Size + 1); ImFontGlyph& glyph = Glyphs.back(); glyph.Codepoint = (ImWchar)codepoint; glyph.X0 = x0; glyph.Y0 = y0; glyph.X1 = x1; glyph.Y1 = y1; glyph.U0 = u0; glyph.V0 = v0; glyph.U1 = u1; glyph.V1 = v1; glyph.AdvanceX = advance_x + ConfigData->GlyphExtraSpacing.x; // Bake spacing into AdvanceX if (ConfigData->PixelSnapH) glyph.AdvanceX = (float)(int)(glyph.AdvanceX + 0.5f); // Compute rough surface usage metrics (+1 to account for average padding, +0.99 to round) DirtyLookupTables = true; MetricsTotalSurface += (int)((glyph.U1 - glyph.U0) * ContainerAtlas->TexWidth + 1.99f) * (int)((glyph.V1 - glyph.V0) * ContainerAtlas->TexHeight + 1.99f); } void ImFont::AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst) { IM_ASSERT(IndexLookup.Size > 0); // Currently this can only be called AFTER the font has been built, aka after calling ImFontAtlas::GetTexDataAs*() function. int index_size = IndexLookup.Size; if (dst < index_size && IndexLookup.Data[dst] == (ImWchar)-1 && !overwrite_dst) // 'dst' already exists return; if (src >= index_size && dst >= index_size) // both 'dst' and 'src' don't exist -> no-op return; GrowIndex(dst + 1); IndexLookup[dst] = (src < index_size) ? IndexLookup.Data[src] : (ImWchar)-1; IndexAdvanceX[dst] = (src < index_size) ? IndexAdvanceX.Data[src] : 1.0f; } const ImFontGlyph* ImFont::FindGlyph(ImWchar c) const { if (c >= IndexLookup.Size) return FallbackGlyph; const ImWchar i = IndexLookup.Data[c]; if (i == (ImWchar)-1) return FallbackGlyph; return &Glyphs.Data[i]; } const ImFontGlyph* ImFont::FindGlyphNoFallback(ImWchar c) const { if (c >= IndexLookup.Size) return NULL; const ImWchar i = IndexLookup.Data[c]; if (i == (ImWchar)-1) return NULL; return &Glyphs.Data[i]; } const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const { // Simple word-wrapping for English, not full-featured. Please submit failing cases! // 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.) // For references, possible wrap point marked with ^ // "aaa bbb, ccc,ddd. eee fff. ggg!" // ^ ^ ^ ^ ^__ ^ ^ // List of hardcoded separators: .,;!?'" // Skip extra blanks after a line returns (that includes not counting them in width computation) // e.g. "Hello world" --> "Hello" "World" // Cut words that cannot possibly fit within one line. // e.g.: "The tropical fish" with ~5 characters worth of width --> "The tr" "opical" "fish" float line_width = 0.0f; float word_width = 0.0f; float blank_width = 0.0f; wrap_width /= scale; // We work with unscaled widths to avoid scaling every characters const char* word_end = text; const char* prev_word_end = NULL; bool inside_word = true; const char* s = text; while (s < text_end) { unsigned int c = (unsigned int)*s; const char* next_s; if (c < 0x80) next_s = s + 1; else next_s = s + ImTextCharFromUtf8(&c, s, text_end); if (c == 0) break; if (c < 32) { if (c == '\n') { line_width = word_width = blank_width = 0.0f; inside_word = true; s = next_s; continue; } if (c == '\r') { s = next_s; continue; } } const float char_width = ((int)c < IndexAdvanceX.Size ? IndexAdvanceX.Data[c] : FallbackAdvanceX); if (ImCharIsBlankW(c)) { if (inside_word) { line_width += blank_width; blank_width = 0.0f; word_end = s; } blank_width += char_width; inside_word = false; } else { word_width += char_width; if (inside_word) { word_end = next_s; } else { prev_word_end = word_end; line_width += word_width + blank_width; word_width = blank_width = 0.0f; } // Allow wrapping after punctuation. inside_word = !(c == '.' || c == ',' || c == ';' || c == '!' || c == '?' || c == '\"'); } // We ignore blank width at the end of the line (they can be skipped) if (line_width + word_width >= wrap_width) { // Words that cannot possibly fit within an entire line will be cut anywhere. if (word_width < wrap_width) s = prev_word_end ? prev_word_end : word_end; break; } s = next_s; } return s; } ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const { if (!text_end) text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this. const float line_height = size; const float scale = size / FontSize; ImVec2 text_size = ImVec2(0,0); float line_width = 0.0f; const bool word_wrap_enabled = (wrap_width > 0.0f); const char* word_wrap_eol = NULL; const char* s = text_begin; while (s < text_end) { if (word_wrap_enabled) { // 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. if (!word_wrap_eol) { word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width); if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity. 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 } if (s >= word_wrap_eol) { if (text_size.x < line_width) text_size.x = line_width; text_size.y += line_height; line_width = 0.0f; word_wrap_eol = NULL; // Wrapping skips upcoming blanks while (s < text_end) { const char c = *s; if (ImCharIsBlankA(c)) { s++; } else if (c == '\n') { s++; break; } else { break; } } continue; } } // Decode and advance source const char* prev_s = s; unsigned int c = (unsigned int)*s; if (c < 0x80) { s += 1; } else { s += ImTextCharFromUtf8(&c, s, text_end); if (c == 0) // Malformed UTF-8? break; } if (c < 32) { if (c == '\n') { text_size.x = ImMax(text_size.x, line_width); text_size.y += line_height; line_width = 0.0f; continue; } if (c == '\r') continue; } const float char_width = ((int)c < IndexAdvanceX.Size ? IndexAdvanceX.Data[c] : FallbackAdvanceX) * scale; if (line_width + char_width >= max_width) { s = prev_s; break; } line_width += char_width; } if (text_size.x < line_width) text_size.x = line_width; if (line_width > 0 || text_size.y == 0.0f) text_size.y += line_height; if (remaining) *remaining = s; return text_size; } void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const { if (c == ' ' || c == '\t' || c == '\n' || c == '\r') // Match behavior of RenderText(), those 4 codepoints are hard-coded. return; if (const ImFontGlyph* glyph = FindGlyph(c)) { float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f; pos.x = (float)(int)pos.x + DisplayOffset.x; pos.y = (float)(int)pos.y + DisplayOffset.y; draw_list->PrimReserve(6, 4); 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); } } void 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 { if (!text_end) text_end = text_begin + strlen(text_begin); // ImGui functions generally already provides a valid text_end, so this is merely to handle direct calls. // Align to be pixel perfect pos.x = (float)(int)pos.x + DisplayOffset.x; pos.y = (float)(int)pos.y + DisplayOffset.y; float x = pos.x; float y = pos.y; if (y > clip_rect.w) return; const float scale = size / FontSize; const float line_height = FontSize * scale; const bool word_wrap_enabled = (wrap_width > 0.0f); const char* word_wrap_eol = NULL; // Fast-forward to first visible line const char* s = text_begin; if (y + line_height < clip_rect.y && !word_wrap_enabled) while (y + line_height < clip_rect.y && s < text_end) { s = (const char*)memchr(s, '\n', text_end - s); s = s ? s + 1 : text_end; y += line_height; } // For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve() // 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) if (text_end - s > 10000 && !word_wrap_enabled) { const char* s_end = s; float y_end = y; while (y_end < clip_rect.w && s_end < text_end) { s_end = (const char*)memchr(s_end, '\n', text_end - s_end); s_end = s_end ? s_end + 1 : text_end; y_end += line_height; } text_end = s_end; } if (s == text_end) return; // Reserve vertices for remaining worse case (over-reserving is useful and easily amortized) const int vtx_count_max = (int)(text_end - s) * 4; const int idx_count_max = (int)(text_end - s) * 6; const int idx_expected_size = draw_list->IdxBuffer.Size + idx_count_max; draw_list->PrimReserve(idx_count_max, vtx_count_max); ImDrawVert* vtx_write = draw_list->_VtxWritePtr; ImDrawIdx* idx_write = draw_list->_IdxWritePtr; unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx; while (s < text_end) { if (word_wrap_enabled) { // 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. if (!word_wrap_eol) { word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - (x - pos.x)); if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity. 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 } if (s >= word_wrap_eol) { x = pos.x; y += line_height; word_wrap_eol = NULL; // Wrapping skips upcoming blanks while (s < text_end) { const char c = *s; if (ImCharIsBlankA(c)) { s++; } else if (c == '\n') { s++; break; } else { break; } } continue; } } // Decode and advance source unsigned int c = (unsigned int)*s; if (c < 0x80) { s += 1; } else { s += ImTextCharFromUtf8(&c, s, text_end); if (c == 0) // Malformed UTF-8? break; } if (c < 32) { if (c == '\n') { x = pos.x; y += line_height; if (y > clip_rect.w) break; // break out of main loop continue; } if (c == '\r') continue; } float char_width = 0.0f; if (const ImFontGlyph* glyph = FindGlyph((ImWchar)c)) { char_width = glyph->AdvanceX * scale; // Arbitrarily assume that both space and tabs are empty glyphs as an optimization if (c != ' ' && c != '\t') { // 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 float x1 = x + glyph->X0 * scale; float x2 = x + glyph->X1 * scale; float y1 = y + glyph->Y0 * scale; float y2 = y + glyph->Y1 * scale; if (x1 <= clip_rect.z && x2 >= clip_rect.x) { // Render a character float u1 = glyph->U0; float v1 = glyph->V0; float u2 = glyph->U1; float v2 = glyph->V1; // CPU side clipping used to fit text in their frame when the frame is too small. Only does clipping for axis aligned quads. if (cpu_fine_clip) { if (x1 < clip_rect.x) { u1 = u1 + (1.0f - (x2 - clip_rect.x) / (x2 - x1)) * (u2 - u1); x1 = clip_rect.x; } if (y1 < clip_rect.y) { v1 = v1 + (1.0f - (y2 - clip_rect.y) / (y2 - y1)) * (v2 - v1); y1 = clip_rect.y; } if (x2 > clip_rect.z) { u2 = u1 + ((clip_rect.z - x1) / (x2 - x1)) * (u2 - u1); x2 = clip_rect.z; } if (y2 > clip_rect.w) { v2 = v1 + ((clip_rect.w - y1) / (y2 - y1)) * (v2 - v1); y2 = clip_rect.w; } if (y1 >= y2) { x += char_width; continue; } } // We are NOT calling PrimRectUV() here because non-inlined causes too much overhead in a debug builds. Inlined here: { idx_write[0] = (ImDrawIdx)(vtx_current_idx); idx_write[1] = (ImDrawIdx)(vtx_current_idx+1); idx_write[2] = (ImDrawIdx)(vtx_current_idx+2); idx_write[3] = (ImDrawIdx)(vtx_current_idx); idx_write[4] = (ImDrawIdx)(vtx_current_idx+2); idx_write[5] = (ImDrawIdx)(vtx_current_idx+3); 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; 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; 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; 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; vtx_write += 4; vtx_current_idx += 4; idx_write += 6; } } } } x += char_width; } // Give back unused vertices draw_list->VtxBuffer.resize((int)(vtx_write - draw_list->VtxBuffer.Data)); draw_list->IdxBuffer.resize((int)(idx_write - draw_list->IdxBuffer.Data)); draw_list->CmdBuffer[draw_list->CmdBuffer.Size-1].ElemCount -= (idx_expected_size - draw_list->IdxBuffer.Size); draw_list->_VtxWritePtr = vtx_write; draw_list->_IdxWritePtr = idx_write; draw_list->_VtxCurrentIdx = (unsigned int)draw_list->VtxBuffer.Size; } //----------------------------------------------------------------------------- // [SECTION] Internal Render Helpers // (progressively moved from imgui.cpp to here when they are redesigned to stop accessing ImGui global state) //----------------------------------------------------------------------------- // - RenderMouseCursor() // - RenderArrowDockMenu() // - RenderArrowPointingAt() // - RenderRectFilledRangeH() // - RenderRectFilledWithHole() // - RenderPixelEllipsis() //----------------------------------------------------------------------------- void ImGui::RenderMouseCursor(ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor) { if (mouse_cursor == ImGuiMouseCursor_None) return; IM_ASSERT(mouse_cursor > ImGuiMouseCursor_None && mouse_cursor < ImGuiMouseCursor_COUNT); const ImU32 col_shadow = IM_COL32(0, 0, 0, 48); const ImU32 col_border = IM_COL32(0, 0, 0, 255); // Black const ImU32 col_fill = IM_COL32(255, 255, 255, 255); // White ImGuiContext& g = *GImGui; ImFontAtlas* font_atlas = g.IO.Fonts; ImVec2 offset, size, uv[4]; if (font_atlas->GetMouseCursorTexData(mouse_cursor, &offset, &size, &uv[0], &uv[2])) { pos -= offset; const ImTextureID tex_id = font_atlas->TexID; // We need to account for the possibility of the mouse cursor straddling multiple viewports... for (int viewport_n = 0; viewport_n < g.Viewports.Size; viewport_n++) { ImGuiViewportP* viewport = g.Viewports[viewport_n]; if (!viewport->GetRect().Overlaps(ImRect(pos, pos + ImVec2(size.x + 2, size.y + 2) * scale))) continue; ImDrawList* draw_list = GetForegroundDrawList(viewport); draw_list->PushTextureID(tex_id); draw_list->AddImage(tex_id, pos + ImVec2(1,0)*scale, pos + ImVec2(1,0)*scale + size*scale, uv[2], uv[3], col_shadow); draw_list->AddImage(tex_id, pos + ImVec2(2,0)*scale, pos + ImVec2(2,0)*scale + size*scale, uv[2], uv[3], col_shadow); draw_list->AddImage(tex_id, pos, pos + size*scale, uv[2], uv[3], col_border); draw_list->AddImage(tex_id, pos, pos + size*scale, uv[0], uv[1], col_fill); draw_list->PopTextureID(); } } } // 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. void ImGui::RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col) { switch (direction) { 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; 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; 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; 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; case ImGuiDir_None: case ImGuiDir_COUNT: break; // Fix warnings } } // This is less wide than RenderArrow() and we use in dock nodes instead of the regular RenderArrow() to denote a change of functionality, // 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. void ImGui::RenderArrowDockMenu(ImDrawList* draw_list, ImVec2 p_min, float sz, ImU32 col) { draw_list->AddRectFilled(p_min + ImVec2(sz * 0.10f, sz * 0.15f), p_min + ImVec2(sz * 0.70f, sz * 0.30f), col); RenderArrowPointingAt(draw_list, p_min + ImVec2(sz * 0.40f, sz * 0.85f), ImVec2(sz * 0.30f, sz * 0.40f), ImGuiDir_Down, col); } static inline float ImAcos01(float x) { if (x <= 0.0f) return IM_PI * 0.5f; if (x >= 1.0f) return 0.0f; return ImAcos(x); //return (-0.69813170079773212f * x * x - 0.87266462599716477f) * x + 1.5707963267948966f; // Cheap approximation, may be enough for what we do. } // FIXME: Cleanup and move code to ImDrawList. void ImGui::RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding) { if (x_end_norm == x_start_norm) return; if (x_start_norm > x_end_norm) ImSwap(x_start_norm, x_end_norm); ImVec2 p0 = ImVec2(ImLerp(rect.Min.x, rect.Max.x, x_start_norm), rect.Min.y); ImVec2 p1 = ImVec2(ImLerp(rect.Min.x, rect.Max.x, x_end_norm), rect.Max.y); if (rounding == 0.0f) { draw_list->AddRectFilled(p0, p1, col, 0.0f); return; } rounding = ImClamp(ImMin((rect.Max.x - rect.Min.x) * 0.5f, (rect.Max.y - rect.Min.y) * 0.5f) - 1.0f, 0.0f, rounding); const float inv_rounding = 1.0f / rounding; const float arc0_b = ImAcos01(1.0f - (p0.x - rect.Min.x) * inv_rounding); const float arc0_e = ImAcos01(1.0f - (p1.x - rect.Min.x) * inv_rounding); const float half_pi = IM_PI * 0.5f; // We will == compare to this because we know this is the exact value ImAcos01 can return. const float x0 = ImMax(p0.x, rect.Min.x + rounding); if (arc0_b == arc0_e) { draw_list->PathLineTo(ImVec2(x0, p1.y)); draw_list->PathLineTo(ImVec2(x0, p0.y)); } else if (arc0_b == 0.0f && arc0_e == half_pi) { draw_list->PathArcToFast(ImVec2(x0, p1.y - rounding), rounding, 3, 6); // BL draw_list->PathArcToFast(ImVec2(x0, p0.y + rounding), rounding, 6, 9); // TR } else { draw_list->PathArcTo(ImVec2(x0, p1.y - rounding), rounding, IM_PI - arc0_e, IM_PI - arc0_b, 3); // BL draw_list->PathArcTo(ImVec2(x0, p0.y + rounding), rounding, IM_PI + arc0_b, IM_PI + arc0_e, 3); // TR } if (p1.x > rect.Min.x + rounding) { const float arc1_b = ImAcos01(1.0f - (rect.Max.x - p1.x) * inv_rounding); const float arc1_e = ImAcos01(1.0f - (rect.Max.x - p0.x) * inv_rounding); const float x1 = ImMin(p1.x, rect.Max.x - rounding); if (arc1_b == arc1_e) { draw_list->PathLineTo(ImVec2(x1, p0.y)); draw_list->PathLineTo(ImVec2(x1, p1.y)); } else if (arc1_b == 0.0f && arc1_e == half_pi) { draw_list->PathArcToFast(ImVec2(x1, p0.y + rounding), rounding, 9, 12); // TR draw_list->PathArcToFast(ImVec2(x1, p1.y - rounding), rounding, 0, 3); // BR } else { draw_list->PathArcTo(ImVec2(x1, p0.y + rounding), rounding, -arc1_e, -arc1_b, 3); // TR draw_list->PathArcTo(ImVec2(x1, p1.y - rounding), rounding, +arc1_b, +arc1_e, 3); // BR } } draw_list->PathFillConvex(col); } // For CTRL+TAB within a docking node we need to render the dimming background in 8 steps // (Because the root node renders the background in one shot, in order to avoid flickering when a child dock node is not submitted) void ImGui::RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect inner, ImU32 col, float rounding) { const bool fill_L = (inner.Min.x > outer.Min.x); const bool fill_R = (inner.Max.x < outer.Max.x); const bool fill_U = (inner.Min.y > outer.Min.y); const bool fill_D = (inner.Max.y < outer.Max.y); 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)); 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)); 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)); 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)); 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); 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); 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); 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); } // FIXME: Rendering an ellipsis "..." is a surprisingly tricky problem for us... we cannot rely on font glyph having it, // and regular dot are typically too wide. If we render a dot/shape ourselves it comes with the risk that it wouldn't match // the boldness or positioning of what the font uses... void ImGui::RenderPixelEllipsis(ImDrawList* draw_list, ImVec2 pos, int count, ImU32 col) { ImFont* font = draw_list->_Data->Font; const float font_scale = draw_list->_Data->FontSize / font->FontSize; pos.y += (float)(int)(font->DisplayOffset.y + font->Ascent * font_scale + 0.5f - 1.0f); for (int dot_n = 0; dot_n < count; dot_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); } //----------------------------------------------------------------------------- // [SECTION] Decompression code //----------------------------------------------------------------------------- // Compressed with stb_compress() then converted to a C array and encoded as base85. // Use the program in misc/fonts/binary_to_compressed_c.cpp to create the array from a TTF file. // The purpose of encoding as base85 instead of "0x00,0x01,..." style is only save on _source code_ size. // Decompression from stb.h (public domain) by Sean Barrett https://github.com/nothings/stb/blob/master/stb.h //----------------------------------------------------------------------------- static unsigned int stb_decompress_length(const unsigned char *input) { return (input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11]; } static unsigned char *stb__barrier_out_e, *stb__barrier_out_b; static const unsigned char *stb__barrier_in_b; static unsigned char *stb__dout; static void stb__match(const unsigned char *data, unsigned int length) { // INVERSE of memmove... write each byte before copying the next... IM_ASSERT(stb__dout + length <= stb__barrier_out_e); if (stb__dout + length > stb__barrier_out_e) { stb__dout += length; return; } if (data < stb__barrier_out_b) { stb__dout = stb__barrier_out_e+1; return; } while (length--) *stb__dout++ = *data++; } static void stb__lit(const unsigned char *data, unsigned int length) { IM_ASSERT(stb__dout + length <= stb__barrier_out_e); if (stb__dout + length > stb__barrier_out_e) { stb__dout += length; return; } if (data < stb__barrier_in_b) { stb__dout = stb__barrier_out_e+1; return; } memcpy(stb__dout, data, length); stb__dout += length; } #define stb__in2(x) ((i[x] << 8) + i[(x)+1]) #define stb__in3(x) ((i[x] << 16) + stb__in2((x)+1)) #define stb__in4(x) ((i[x] << 24) + stb__in3((x)+1)) static const unsigned char *stb_decompress_token(const unsigned char *i) { if (*i >= 0x20) { // use fewer if's for cases that expand small if (*i >= 0x80) stb__match(stb__dout-i[1]-1, i[0] - 0x80 + 1), i += 2; else if (*i >= 0x40) stb__match(stb__dout-(stb__in2(0) - 0x4000 + 1), i[2]+1), i += 3; else /* *i >= 0x20 */ stb__lit(i+1, i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1); } else { // more ifs for cases that expand large, since overhead is amortized if (*i >= 0x18) stb__match(stb__dout-(stb__in3(0) - 0x180000 + 1), i[3]+1), i += 4; else if (*i >= 0x10) stb__match(stb__dout-(stb__in3(0) - 0x100000 + 1), stb__in2(3)+1), i += 5; else if (*i >= 0x08) stb__lit(i+2, stb__in2(0) - 0x0800 + 1), i += 2 + (stb__in2(0) - 0x0800 + 1); else if (*i == 0x07) stb__lit(i+3, stb__in2(1) + 1), i += 3 + (stb__in2(1) + 1); else if (*i == 0x06) stb__match(stb__dout-(stb__in3(1)+1), i[4]+1), i += 5; else if (*i == 0x04) stb__match(stb__dout-(stb__in3(1)+1), stb__in2(4)+1), i += 6; } return i; } static unsigned int stb_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen) { const unsigned long ADLER_MOD = 65521; unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16; unsigned long blocklen, i; blocklen = buflen % 5552; while (buflen) { for (i=0; i + 7 < blocklen; i += 8) { s1 += buffer[0], s2 += s1; s1 += buffer[1], s2 += s1; s1 += buffer[2], s2 += s1; s1 += buffer[3], s2 += s1; s1 += buffer[4], s2 += s1; s1 += buffer[5], s2 += s1; s1 += buffer[6], s2 += s1; s1 += buffer[7], s2 += s1; buffer += 8; } for (; i < blocklen; ++i) s1 += *buffer++, s2 += s1; s1 %= ADLER_MOD, s2 %= ADLER_MOD; buflen -= blocklen; blocklen = 5552; } return (unsigned int)(s2 << 16) + (unsigned int)s1; } static unsigned int stb_decompress(unsigned char *output, const unsigned char *i, unsigned int /*length*/) { unsigned int olen; if (stb__in4(0) != 0x57bC0000) return 0; if (stb__in4(4) != 0) return 0; // error! stream is > 4GB olen = stb_decompress_length(i); stb__barrier_in_b = i; stb__barrier_out_e = output + olen; stb__barrier_out_b = output; i += 16; stb__dout = output; for (;;) { const unsigned char *old_i = i; i = stb_decompress_token(i); if (i == old_i) { if (*i == 0x05 && i[1] == 0xfa) { IM_ASSERT(stb__dout == output + olen); if (stb__dout != output + olen) return 0; if (stb_adler32(1, output, olen) != (unsigned int) stb__in4(2)) return 0; return olen; } else { IM_ASSERT(0); /* NOTREACHED */ return 0; } } IM_ASSERT(stb__dout <= output + olen); if (stb__dout > output + olen) return 0; } } //----------------------------------------------------------------------------- // [SECTION] Default font data (ProggyClean.ttf) //----------------------------------------------------------------------------- // ProggyClean.ttf // Copyright (c) 2004, 2005 Tristan Grimmer // MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip) // Download and more information at http://upperbounds.net //----------------------------------------------------------------------------- // File: 'ProggyClean.ttf' (41208 bytes) // Exported using misc/fonts/binary_to_compressed_c.cpp (with compression + base85 string encoding). // The purpose of encoding as base85 instead of "0x00,0x01,..." style is only save on _source code_ size. //----------------------------------------------------------------------------- static const char proggy_clean_ttf_compressed_data_base85[11980+1] = "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/" "2*>]b(MC;$jPfY.;h^`IWM9Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1=Ke$$'5F%)]0^#0X@U.a$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#W@lK.N'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;--VsM.M0rJfLH2eTM`*oJMHRC`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+`�j@'DbG&#^$PG.Ll+DNa&VZ>1i%h1S9u5o@YaaW$e+bROPOpxTO7Stwi1::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." "x]Ip.PH^'/aqUO/$1WxLoW0[iLAw=4h(9.`G" "CRUxHPeR`5Mjol(dUWxZa(>STrPkrJiWx`5U7F#.g*jrohGg`cg:lSTvEY/EV_7H4Q9[Z%cnv;JQYZ5q.l7Zeas:HOIZOB?Ggv:[7MI2k).'2($5FNP&EQ(,)" "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#" "'/###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" "_3YlQC7(p7q)&](`6_c)$/*JL(L-^(]$wIM`dPtOdGA,U3:w2M-0+WomX2u7lqM2iEumMTcsF?-aT=Z-97UEnXglEn1K-bnEO`gu" "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/" "/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[Ket`e;)f#Km8&+DC$I46>#Kr]]u-[=99tts1.qb#q72g1WJO81q+eN'03'eM>&1XxY-caEnO" "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-XTt,%OVU4)S1+R-#dg0/Nn?Ku1^0f$B*P:Rowwm-`0PKjYDDM'3]d39VZHEl4,.j']Pk-M.h^&:0FACm$maq-&sgw0t7/6(^xtk%" "LuH88Fj-ekm>GA#_>568x6(OFRl-IZp`&b,_P'$MhLbxfc$mj`,O;&%W2m`Zh:/)Uetw:aJ%]K9h:TcF]u_-Sj9,VK3M.*'&0D[Ca]J9gp8,kAW]" "%(?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" "Hg*`+RLgv>=4U8guD$I%D:W>-r5V*%j*W:Kvej.Lp$'?;++O'>()jLR-^u68PHm8ZFWe+ej8h:9r6L*0//c&iH&R8pRbA#Kjm%upV1g:" "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&VBpqB>0ie&jhZ[?iLR@@_AvA-iQC(=ksRZRVp7`.=+NpBC%rh&3]R:8XDmE5^V8O(x<-+k?'(^](H.aREZSi,#1:[IXaZFOm<-ui#qUq2$##Ri;u75OK#(RtaW-K-F`S+cF]uN`-KMQ%rP/Xri.LRcB##=YL3BgM/3M" "D?@f&1'BW-)Ju#bmmWCMkk&#TR`C,5d>g)F;t,4:@_l8G/5h4vUd%&%950:VXD'QdWoY-F$BtUwmfe$YqL'8(PWX(" "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" "bIu)'Z,*[>br5fX^:FPAWr-m2KgLQ_nN6'8uTGT5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q" "h4CB/5OvmA&,Q&QbUoi$a_%3M01H)4x7I^&KQVgtFnV+;[Pc>[m4k//,]1?#`VY[Jr*3&&slRfLiVZJ:]?=K3Sw=[$=uRB?3xk48@aege0jT6'N#(q%.O=?2S]u*(m<-" "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" "sZ88+dKQ)W6>J%CL`.d*(B`-n8D9oK-XV1q['-5k'cAZ69e;D_?$ZPP&s^+7])$*$#@QYi9,5P r+$%CE=68>K8r0=dSC%%(@p7" ".m7jilQ02'0-VWAgTlGW'b)Tq7VT9q^*^$$.:&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*" "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" "@-W$U%VEQ/,,>>#)D#%8cY#YZ?=,`Wdxu/ae&#" "w6)R89tI#6@s'(6Bf7a&?S=^ZI_kS&ai`&=tE72L_D,;^R)7[$so8lKN%5/$(vdfq7+ebA#" "u1p]ovUKW&Y%q]'>$1@-[xfn$7ZTp7mM,G,Ko7a&Gu%G[RMxJs[0MM%wci.LFDK)(%:_i2B5CsR8&9Z&#=mPEnm0f`<&c)QL5uJ#%u%lJj+D-r;BoFDoS97h5g)E#o:&S4weDF,9^Hoe`h*L+_a*NrLW-1pG_&2UdB8" "6e%B/:=>)N4xeW.*wft-;$'58-ESqr#U`'6AQ]m&6/`Z>#S?YY#Vc;r7U2&326d=w&H####?TZ`*4?&.MK?LP8Vxg>$[QXc%QJv92.(Db*B)gb*BM9dM*hJMAo*c&#" "b0v=Pjer]$gG&JXDf->'StvU7505l9$AFvgYRI^&<^b68?j#q9QX4SM'RO#&sL1IM.rJfLUAj221]d##DW=m83u5;'bYx,*Sl0hL(W;;$doB&O/TQ:(Z^xBdLjLV#*8U_72Lh+2Q8Cj0i:6hp&$C/:p(HK>T8Y[gHQ4`4)'$Ab(Nof%V'8hL&#SfD07&6D@M.*J:;$-rv29'M]8qMv-tLp,'886iaC=Hb*YJoKJ,(j%K=H`K.v9HggqBIiZu'QvBT.#=)0ukruV&.)3=(^1`o*Pj4<-#MJ+gLq9-##@HuZPN0]u:h7.T..G:;$/Usj(T7`Q8tT72LnYl<-qx8;-HV7Q-&Xdx%1a,hC=0u+HlsV>nuIQL-5" "_>@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%" "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;" "^V'9;jY@;)br#q^YQpx:X#Te$Z^'=-=bGhLf:D6&bNwZ9-ZD#n^9HhLMr5G;']d&6'wYmTFmLq9wI>P(9mI[>kC-ekLC/R&CH+s'B;K-M6$EB%is00:" "+A4[7xks.LrNk0&E)wILYF@2L'0Nb$+pv<(2.768/FrY&h$^3i&@+G%JT'<-,v`3;_)I9M^AE]CN?Cl2AZg+%4iTpT3$U4O]GKx'm9)b@p7YsvK3w^YR-" "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*" "hU%<-SRF`Tt:542R_VV$p@[p8DV[A,?1839FWdFTi1O*H&#(AL8[_P%.M>v^-))qOT*F5Cq0`Ye%+$B6i:7@0IXSsDiWP,##P`%/L-" "S(qw%sf/@%#B6;/U7K]uZbi^Oc^2n%t<)'mEVE''n`WnJra$^TKvX5B>;_aSEK',(hwa0:i4G?.Bci.(X[?b*($,=-n<.Q%`(X=?+@Am*Js0&=3bh8K]mL69=Lb,OcZV/);TTm8VI;?%OtJ<(b4mq7M6:u?KRdFl*:xP?Yb.5)%w_I?7uk5JC+FS(m#i'k.'a0i)9<7b'fs'59hq$*5Uhv##pi^8+hIEBF`nvo`;'l0.^S1<-wUK2/Coh58KKhLj" "M=SO*rfO`+qC`W-On.=AJ56>>i2@2LH6A:&5q`?9I3@@'04&p2/LVa*T-4<-i3;M9UvZd+N7>b*eIwg:CC)c<>nO&#$(>.Z-I&J(Q0Hd5Q%7Co-b`-cP)hI;*_F]u`Rb[.j8_Q/<&>uu+VsH$sM9TA%?)(vmJ80),P7E>)tjD%2L=-t#fK[%`v=Q8WlA2);Sa" ">gXm8YB`1d@K#n]76-a$U,mF%Ul:#/'xoFM9QX-$.QN'>" "[%$Z$uF6pA6Ki2O5:8w*vP1<-1`[G,)-m#>0`P&#eb#.3i)rtB61(o'$?X3B2Qft^ae_5tKL9MUe9b*sLEQ95C&`=G?@Mj=wh*'3E>=-<)Gt*Iw)'QG:`@I" "wOf7&]1i'S01B+Ev/Nac#9S;=;YQpg_6U`*kVY39xK,[/6Aj7:'1Bm-_1EYfa1+o&o4hp7KN_Q(OlIo@S%;jVdn0'1h19w,WQhLI)3S#f$2(eb,jr*b;3Vw]*7NH%$c4Vs,eD9>XW8?N]o+(*pgC%/72LV-uW%iewS8W6m2rtCpo'RS1R84=@paTKt)>=%&1[)*vp'u+x,VrwN;&]kuO9JDbg=pO$J*.jVe;u'm0dr9l,<*wMK*Oe=g8lV_KEBFkO'oU]^=[-792#ok,)" "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" "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" "iDDG)g,r%+?,$@?uou5tSe2aN_AQU*'IAO" "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(#" ";u.T%fAr%4tJ8&><1=GHZ_+m9/#H1F^R#SC#*N=BA9(D?v[UiFY>>^8p,KKF.W]L29uLkLlu/+4T" "w$)F./^n3+rlo+DB;5sIYGNk+i1t-69Jg--0pao7Sm#K)pdHW&;LuDNH@H>#/X-TI(;P>#,Gc>#0Su>#4`1?#8lC?#xL$#B.`$#F:r$#JF.%#NR@%#R_R%#Vke%#Zww%#_-4^Rh%Sflr-k'MS.o?.5/sWel/wpEM0%3'/1)K^f1-d>G21&v(35>V`39V7A4=onx4" "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#" "/QHC#3^ZC#7jmC#;v)D#?,)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP" "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp" "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#"; static const char* GetDefaultCompressedFontDataTTFBase85() { return proggy_clean_ttf_compressed_data_base85; } #pragma warning(pop) ================================================ FILE: src/imgui/imgui_impl_dx12.cpp ================================================ // dear imgui: Renderer for DirectX12 // This needs to be used along with a Platform Binding (e.g. Win32) // Implemented features: // [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. // Issues: // [ ] 64-bit only for now! (Because sizeof(ImTextureId) == sizeof(void*)). See github.com/ocornut/imgui/pull/301 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. // https://github.com/ocornut/imgui // CHANGELOG // (minor and older changes stripped away, please see git history for details) // 2019-03-29: Misc: Various minor tidying up. // 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile(). // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window. // 2018-06-12: DirectX12: Moved the ID3D12GraphicsCommandList* parameter from NewFrame() to RenderDrawData(). // 2018-06-08: Misc: Extracted imgui_impl_dx12.cpp/.h away from the old combined DX12+Win32 example. // 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). // 2018-02-22: Merged into master with all Win32 code synchronized to other examples. #include "imgui.hpp" #include "imgui_impl_dx12.hpp" // DirectX #include #include #include #ifdef _MSC_VER #pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below. #endif // DirectX data static ID3D12Device* g_pd3dDevice = NULL; static ID3D10Blob* g_pVertexShaderBlob = NULL; static ID3D10Blob* g_pPixelShaderBlob = NULL; static ID3D12RootSignature* g_pRootSignature = NULL; static ID3D12PipelineState* g_pPipelineState = NULL; static DXGI_FORMAT g_RTVFormat = DXGI_FORMAT_UNKNOWN; static ID3D12Resource* g_pFontTextureResource = NULL; static D3D12_CPU_DESCRIPTOR_HANDLE g_hFontSrvCpuDescHandle = {}; static D3D12_GPU_DESCRIPTOR_HANDLE g_hFontSrvGpuDescHandle = {}; struct FrameResources { ID3D12Resource* IndexBuffer; ID3D12Resource* VertexBuffer; int IndexBufferSize; int VertexBufferSize; }; static FrameResources* g_pFrameResources = NULL; static UINT g_numFramesInFlight = 0; static UINT g_frameIndex = UINT_MAX; static bool g_initialized = false; struct VERTEX_CONSTANT_BUFFER { float mvp[4][4]; }; // Forward Declarations static void ImGui_ImplDX12_InitPlatformInterface(); static void ImGui_ImplDX12_ShutdownPlatformInterface(); // Render function // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop) void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* ctx) { // Avoid rendering when minimized if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) return; // FIXME: I'm assuming that this only gets called once per frame! // If not, we can't just re-allocate the IB or VB, we'll have to do a proper allocator. g_frameIndex = g_frameIndex + 1; FrameResources* fr = &g_pFrameResources[g_frameIndex % g_numFramesInFlight]; // Create and grow vertex/index buffers if needed if (fr->VertexBuffer == NULL || fr->VertexBufferSize < draw_data->TotalVtxCount) { if (fr->VertexBuffer != NULL) { fr->VertexBuffer->Release(); fr->VertexBuffer = NULL; } fr->VertexBufferSize = draw_data->TotalVtxCount + 5000; D3D12_HEAP_PROPERTIES props; memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES)); props.Type = D3D12_HEAP_TYPE_UPLOAD; props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; D3D12_RESOURCE_DESC desc; memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC)); desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; desc.Width = fr->VertexBufferSize * sizeof(ImDrawVert); desc.Height = 1; desc.DepthOrArraySize = 1; desc.MipLevels = 1; desc.Format = DXGI_FORMAT_UNKNOWN; desc.SampleDesc.Count = 1; desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; desc.Flags = D3D12_RESOURCE_FLAG_NONE; if (g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&fr->VertexBuffer)) < 0) return; } if (fr->IndexBuffer == NULL || fr->IndexBufferSize < draw_data->TotalIdxCount) { if (fr->IndexBuffer != NULL) { fr->IndexBuffer->Release(); fr->IndexBuffer = NULL; } fr->IndexBufferSize = draw_data->TotalIdxCount + 10000; D3D12_HEAP_PROPERTIES props; memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES)); props.Type = D3D12_HEAP_TYPE_UPLOAD; props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; D3D12_RESOURCE_DESC desc; memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC)); desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; desc.Width = fr->IndexBufferSize * sizeof(ImDrawIdx); desc.Height = 1; desc.DepthOrArraySize = 1; desc.MipLevels = 1; desc.Format = DXGI_FORMAT_UNKNOWN; desc.SampleDesc.Count = 1; desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; desc.Flags = D3D12_RESOURCE_FLAG_NONE; if (g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&fr->IndexBuffer)) < 0) return; } // Upload vertex/index data into a single contiguous GPU buffer void* vtx_resource, *idx_resource; D3D12_RANGE range; memset(&range, 0, sizeof(D3D12_RANGE)); if (fr->VertexBuffer->Map(0, &range, &vtx_resource) != S_OK) return; if (fr->IndexBuffer->Map(0, &range, &idx_resource) != S_OK) return; ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource; ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource; for (int n = 0; n < draw_data->CmdListsCount; n++) { const ImDrawList* cmd_list = draw_data->CmdLists[n]; memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert)); memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx)); vtx_dst += cmd_list->VtxBuffer.Size; idx_dst += cmd_list->IdxBuffer.Size; } fr->VertexBuffer->Unmap(0, &range); fr->IndexBuffer->Unmap(0, &range); // Setup orthographic projection matrix into our constant buffer // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is (0,0) for single viewport apps. VERTEX_CONSTANT_BUFFER vertex_constant_buffer; { float L = draw_data->DisplayPos.x; float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; float T = draw_data->DisplayPos.y; float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; float mvp[4][4] = { { 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, { 0.0f, 2.0f/(T-B), 0.0f, 0.0f }, { 0.0f, 0.0f, 0.5f, 0.0f }, { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, }; memcpy(&vertex_constant_buffer.mvp, mvp, sizeof(mvp)); } // Setup viewport D3D12_VIEWPORT vp; memset(&vp, 0, sizeof(D3D12_VIEWPORT)); vp.Width = draw_data->DisplaySize.x; vp.Height = draw_data->DisplaySize.y; vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; vp.TopLeftX = vp.TopLeftY = 0.0f; ctx->RSSetViewports(1, &vp); // Bind shader and vertex buffers unsigned int stride = sizeof(ImDrawVert); unsigned int offset = 0; D3D12_VERTEX_BUFFER_VIEW vbv; memset(&vbv, 0, sizeof(D3D12_VERTEX_BUFFER_VIEW)); vbv.BufferLocation = fr->VertexBuffer->GetGPUVirtualAddress() + offset; vbv.SizeInBytes = fr->VertexBufferSize * stride; vbv.StrideInBytes = stride; ctx->IASetVertexBuffers(0, 1, &vbv); D3D12_INDEX_BUFFER_VIEW ibv; memset(&ibv, 0, sizeof(D3D12_INDEX_BUFFER_VIEW)); ibv.BufferLocation = fr->IndexBuffer->GetGPUVirtualAddress(); ibv.SizeInBytes = fr->IndexBufferSize * sizeof(ImDrawIdx); ibv.Format = sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT; ctx->IASetIndexBuffer(&ibv); ctx->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ctx->SetPipelineState(g_pPipelineState); ctx->SetGraphicsRootSignature(g_pRootSignature); ctx->SetGraphicsRoot32BitConstants(0, 16, &vertex_constant_buffer, 0); // Setup blend factor const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f }; ctx->OMSetBlendFactor(blend_factor); // Render command lists int vtx_offset = 0; int idx_offset = 0; ImVec2 clip_off = draw_data->DisplayPos; for (int n = 0; n < draw_data->CmdListsCount; n++) { const ImDrawList* cmd_list = draw_data->CmdLists[n]; for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; if (pcmd->UserCallback) { // User callback (registered via ImDrawList::AddCallback) pcmd->UserCallback(cmd_list, pcmd); } else { // Apply Scissor, Bind texture, Draw 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) }; ctx->SetGraphicsRootDescriptorTable(1, *(D3D12_GPU_DESCRIPTOR_HANDLE*)&pcmd->TextureId); ctx->RSSetScissorRects(1, &r); ctx->DrawIndexedInstanced(pcmd->ElemCount, 1, idx_offset, vtx_offset, 0); } idx_offset += pcmd->ElemCount; } vtx_offset += cmd_list->VtxBuffer.Size; } } static void ImGui_ImplDX12_CreateFontsTexture() { // Build texture atlas ImGuiIO& io = ImGui::GetIO(); unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Upload texture to graphics system { D3D12_HEAP_PROPERTIES props; memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES)); props.Type = D3D12_HEAP_TYPE_DEFAULT; props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; D3D12_RESOURCE_DESC desc; ZeroMemory(&desc, sizeof(desc)); desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; desc.Alignment = 0; desc.Width = width; desc.Height = height; desc.DepthOrArraySize = 1; desc.MipLevels = 1; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; desc.Flags = D3D12_RESOURCE_FLAG_NONE; ID3D12Resource* pTexture = NULL; g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COPY_DEST, NULL, IID_PPV_ARGS(&pTexture)); UINT uploadPitch = (width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u); UINT uploadSize = height * uploadPitch; desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; desc.Alignment = 0; desc.Width = uploadSize; desc.Height = 1; desc.DepthOrArraySize = 1; desc.MipLevels = 1; desc.Format = DXGI_FORMAT_UNKNOWN; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; desc.Flags = D3D12_RESOURCE_FLAG_NONE; props.Type = D3D12_HEAP_TYPE_UPLOAD; props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; ID3D12Resource* uploadBuffer = NULL; HRESULT hr = g_pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, NULL, IID_PPV_ARGS(&uploadBuffer)); IM_ASSERT(SUCCEEDED(hr)); void* mapped = NULL; D3D12_RANGE range = { 0, uploadSize }; hr = uploadBuffer->Map(0, &range, &mapped); IM_ASSERT(SUCCEEDED(hr)); for (int y = 0; y < height; y++) memcpy((void*) ((uintptr_t) mapped + y * uploadPitch), pixels + y * width * 4, width * 4); uploadBuffer->Unmap(0, &range); D3D12_TEXTURE_COPY_LOCATION srcLocation = {}; srcLocation.pResource = uploadBuffer; srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM; srcLocation.PlacedFootprint.Footprint.Width = width; srcLocation.PlacedFootprint.Footprint.Height = height; srcLocation.PlacedFootprint.Footprint.Depth = 1; srcLocation.PlacedFootprint.Footprint.RowPitch = uploadPitch; D3D12_TEXTURE_COPY_LOCATION dstLocation = {}; dstLocation.pResource = pTexture; dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; dstLocation.SubresourceIndex = 0; D3D12_RESOURCE_BARRIER barrier = {}; barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; barrier.Transition.pResource = pTexture; barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST; barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; ID3D12Fence* fence = NULL; hr = g_pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)); IM_ASSERT(SUCCEEDED(hr)); HANDLE event = CreateEvent(0, 0, 0, 0); IM_ASSERT(event != NULL); D3D12_COMMAND_QUEUE_DESC queueDesc = {}; queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; queueDesc.NodeMask = 1; ID3D12CommandQueue* cmdQueue = NULL; hr = g_pd3dDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&cmdQueue)); IM_ASSERT(SUCCEEDED(hr)); ID3D12CommandAllocator* cmdAlloc = NULL; hr = g_pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc)); IM_ASSERT(SUCCEEDED(hr)); ID3D12GraphicsCommandList* cmdList = NULL; hr = g_pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, cmdAlloc, NULL, IID_PPV_ARGS(&cmdList)); IM_ASSERT(SUCCEEDED(hr)); cmdList->CopyTextureRegion(&dstLocation, 0, 0, 0, &srcLocation, NULL); cmdList->ResourceBarrier(1, &barrier); hr = cmdList->Close(); IM_ASSERT(SUCCEEDED(hr)); cmdQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*) &cmdList); hr = cmdQueue->Signal(fence, 1); IM_ASSERT(SUCCEEDED(hr)); fence->SetEventOnCompletion(1, event); WaitForSingleObject(event, INFINITE); cmdList->Release(); cmdAlloc->Release(); cmdQueue->Release(); CloseHandle(event); fence->Release(); uploadBuffer->Release(); // Create texture view D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc; ZeroMemory(&srvDesc, sizeof(srvDesc)); srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MipLevels = desc.MipLevels; srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, g_hFontSrvCpuDescHandle); if (g_pFontTextureResource != NULL) g_pFontTextureResource->Release(); g_pFontTextureResource = pTexture; } // Store our identifier static_assert(sizeof(ImTextureID) >= sizeof(g_hFontSrvGpuDescHandle.ptr), "Can't pack descriptor handle into TexID, 32-bit not supported yet."); io.Fonts->TexID = (ImTextureID)g_hFontSrvGpuDescHandle.ptr; } bool ImGui_ImplDX12_CreateDeviceObjects() { if (!g_pd3dDevice) return false; if (g_pPipelineState) ImGui_ImplDX12_InvalidateDeviceObjects(); // Create the root signature { D3D12_DESCRIPTOR_RANGE descRange = {}; descRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; descRange.NumDescriptors = 1; descRange.BaseShaderRegister = 0; descRange.RegisterSpace = 0; descRange.OffsetInDescriptorsFromTableStart = 0; D3D12_ROOT_PARAMETER param[2] = {}; param[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; param[0].Constants.ShaderRegister = 0; param[0].Constants.RegisterSpace = 0; param[0].Constants.Num32BitValues = 16; param[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX; param[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; param[1].DescriptorTable.NumDescriptorRanges = 1; param[1].DescriptorTable.pDescriptorRanges = &descRange; param[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; D3D12_STATIC_SAMPLER_DESC staticSampler = {}; staticSampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; staticSampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP; staticSampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP; staticSampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP; staticSampler.MipLODBias = 0.f; staticSampler.MaxAnisotropy = 0; staticSampler.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; staticSampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK; staticSampler.MinLOD = 0.f; staticSampler.MaxLOD = 0.f; staticSampler.ShaderRegister = 0; staticSampler.RegisterSpace = 0; staticSampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; D3D12_ROOT_SIGNATURE_DESC desc = {}; desc.NumParameters = _countof(param); desc.pParameters = param; desc.NumStaticSamplers = 1; desc.pStaticSamplers = &staticSampler; desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT | D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS | D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS | D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS; ID3DBlob* blob = NULL; if (D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, NULL) != S_OK) return false; g_pd3dDevice->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&g_pRootSignature)); blob->Release(); } // By using D3DCompile() from / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A) // If you would like to use this DX12 sample code but remove this dependency you can: // 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution] // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL. // See https://github.com/ocornut/imgui/pull/638 for sources and details. D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc; memset(&psoDesc, 0, sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC)); psoDesc.NodeMask = 1; psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; psoDesc.pRootSignature = g_pRootSignature; psoDesc.SampleMask = UINT_MAX; psoDesc.NumRenderTargets = 1; psoDesc.RTVFormats[0] = g_RTVFormat; psoDesc.SampleDesc.Count = 1; psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE; // Create the vertex shader { static const char* vertexShader = "cbuffer vertexBuffer : register(b0) \ {\ float4x4 ProjectionMatrix; \ };\ struct VS_INPUT\ {\ float2 pos : POSITION;\ float4 col : COLOR0;\ float2 uv : TEXCOORD0;\ };\ \ struct PS_INPUT\ {\ float4 pos : SV_POSITION;\ float4 col : COLOR0;\ float2 uv : TEXCOORD0;\ };\ \ PS_INPUT main(VS_INPUT input)\ {\ PS_INPUT output;\ output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\ output.col = input.col;\ output.uv = input.uv;\ return output;\ }"; D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_5_0", 0, 0, &g_pVertexShaderBlob, NULL); if (g_pVertexShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! return false; psoDesc.VS = { g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize() }; // Create the input layout static D3D12_INPUT_ELEMENT_DESC local_layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, IM_OFFSETOF(ImDrawVert, pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, IM_OFFSETOF(ImDrawVert, uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, IM_OFFSETOF(ImDrawVert, col), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, }; psoDesc.InputLayout = { local_layout, 3 }; } // Create the pixel shader { static const char* pixelShader = "struct PS_INPUT\ {\ float4 pos : SV_POSITION;\ float4 col : COLOR0;\ float2 uv : TEXCOORD0;\ };\ SamplerState sampler0 : register(s0);\ Texture2D texture0 : register(t0);\ \ float4 main(PS_INPUT input) : SV_Target\ {\ float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \ return out_col; \ }"; D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &g_pPixelShaderBlob, NULL); if (g_pPixelShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! return false; psoDesc.PS = { g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize() }; } // Create the blending setup { D3D12_BLEND_DESC& desc = psoDesc.BlendState; desc.AlphaToCoverageEnable = false; desc.RenderTarget[0].BlendEnable = true; desc.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA; desc.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA; desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD; desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA; desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO; desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD; desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; } // Create the rasterizer state { D3D12_RASTERIZER_DESC& desc = psoDesc.RasterizerState; desc.FillMode = D3D12_FILL_MODE_SOLID; desc.CullMode = D3D12_CULL_MODE_NONE; desc.FrontCounterClockwise = FALSE; desc.DepthBias = D3D12_DEFAULT_DEPTH_BIAS; desc.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP; desc.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS; desc.DepthClipEnable = true; desc.MultisampleEnable = FALSE; desc.AntialiasedLineEnable = FALSE; desc.ForcedSampleCount = 0; desc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF; } // Create depth-stencil State { D3D12_DEPTH_STENCIL_DESC& desc = psoDesc.DepthStencilState; desc.DepthEnable = false; desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; desc.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; desc.StencilEnable = false; desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP; desc.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS; desc.BackFace = desc.FrontFace; } if (g_pd3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&g_pPipelineState)) != S_OK) return false; ImGui_ImplDX12_CreateFontsTexture(); return true; } IMGUI_IMPL_API bool ImGui_ImplDX12_IsInitialized() { return g_initialized; } void ImGui_ImplDX12_InvalidateDeviceObjects() { if (!g_pd3dDevice) return; ImGuiIO& io = ImGui::GetIO(); if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; } if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; } if (g_pRootSignature) { g_pRootSignature->Release(); g_pRootSignature = NULL; } if (g_pPipelineState) { g_pPipelineState->Release(); g_pPipelineState = NULL; } 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. for (UINT i = 0; i < g_numFramesInFlight; i++) { FrameResources* fr = &g_pFrameResources[i]; if (fr->IndexBuffer) { fr->IndexBuffer->Release(); fr->IndexBuffer = NULL; } if (fr->VertexBuffer) { fr->VertexBuffer->Release(); fr->VertexBuffer = NULL; } } } bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format, D3D12_CPU_DESCRIPTOR_HANDLE font_srv_cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE font_srv_gpu_desc_handle) { // Setup back-end capabilities flags ImGuiIO& io = ImGui::GetIO(); io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) // FIXME-VIEWPORT: Actually unfinished.. io.BackendRendererName = "imgui_impl_dx12"; g_pd3dDevice = device; g_RTVFormat = rtv_format; g_hFontSrvCpuDescHandle = font_srv_cpu_desc_handle; g_hFontSrvGpuDescHandle = font_srv_gpu_desc_handle; g_pFrameResources = new FrameResources[num_frames_in_flight]; g_numFramesInFlight = num_frames_in_flight; g_frameIndex = UINT_MAX; // Create buffers with a default size (they will later be grown as needed) for (int i = 0; i < num_frames_in_flight; i++) { FrameResources* fr = &g_pFrameResources[i]; fr->IndexBuffer = NULL; fr->VertexBuffer = NULL; fr->IndexBufferSize = 10000; fr->VertexBufferSize = 5000; } if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) ImGui_ImplDX12_InitPlatformInterface(); g_initialized = true; return true; } void ImGui_ImplDX12_Shutdown() { ImGui_ImplDX12_ShutdownPlatformInterface(); ImGui_ImplDX12_InvalidateDeviceObjects(); delete[] g_pFrameResources; g_pFrameResources = NULL; g_pd3dDevice = NULL; g_hFontSrvCpuDescHandle.ptr = 0; g_hFontSrvGpuDescHandle.ptr = 0; g_numFramesInFlight = 0; g_frameIndex = UINT_MAX; g_initialized = false; } void ImGui_ImplDX12_NewFrame() { if (!g_pPipelineState) ImGui_ImplDX12_CreateDeviceObjects(); } //-------------------------------------------------------------------------------------------------------- // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT // This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously. // 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.. //-------------------------------------------------------------------------------------------------------- struct ImGuiViewportDataDx12 { IDXGISwapChain3* SwapChain; ImGuiViewportDataDx12() { SwapChain = NULL; } ~ImGuiViewportDataDx12() { IM_ASSERT(SwapChain == NULL); } }; static void ImGui_ImplDX12_CreateWindow(ImGuiViewport* viewport) { ImGuiViewportDataDx12* data = IM_NEW(ImGuiViewportDataDx12)(); viewport->RendererUserData = data; IM_ASSERT(0); /* // FIXME-PLATFORM HWND hwnd = (HWND)viewport->PlatformHandle; IM_ASSERT(hwnd != 0); // Create swap chain DXGI_SWAP_CHAIN_DESC sd; ZeroMemory(&sd, sizeof(sd)); sd.BufferDesc.Width = (UINT)viewport->Size.x; sd.BufferDesc.Height = (UINT)viewport->Size.y; sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; sd.SampleDesc.Count = 1; sd.SampleDesc.Quality = 0; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.BufferCount = 1; sd.OutputWindow = hwnd; sd.Windowed = TRUE; sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; sd.Flags = 0; IM_ASSERT(data->SwapChain == NULL && data->RTView == NULL); g_pFactory->CreateSwapChain(g_pd3dDevice, &sd, &data->SwapChain); // Create the render target if (data->SwapChain) { ID3D11Texture2D* pBackBuffer; data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer)); g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView); pBackBuffer->Release(); } */ } static void ImGui_ImplDX12_DestroyWindow(ImGuiViewport* viewport) { // The main viewport (owned by the application) will always have RendererUserData == NULL since we didn't create the data for it. if (ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData) { IM_ASSERT(0); /* if (data->SwapChain) data->SwapChain->Release(); data->SwapChain = NULL; if (data->RTView) data->RTView->Release(); data->RTView = NULL; IM_DELETE(data); */ } viewport->RendererUserData = NULL; } static void ImGui_ImplDX12_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) { ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData; IM_ASSERT(0); (void)data; (void)size; /* if (data->RTView) { data->RTView->Release(); data->RTView = NULL; } if (data->SwapChain) { ID3D11Texture2D* pBackBuffer = NULL; data->SwapChain->ResizeBuffers(0, (UINT)size.x, (UINT)size.y, DXGI_FORMAT_UNKNOWN, 0); data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer)); g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView); pBackBuffer->Release(); } */ } // arg = ID3D12GraphicsCommandList* static void ImGui_ImplDX12_RenderWindow(ImGuiViewport* viewport, void* renderer_arg) { ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData; IM_ASSERT(0); (void)data; ID3D12GraphicsCommandList* command_list = (ID3D12GraphicsCommandList*)renderer_arg; /* ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); g_pd3dDeviceContext->OMSetRenderTargets(1, &data->RTView, NULL); if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear)) g_pd3dDeviceContext->ClearRenderTargetView(data->RTView, (float*)&clear_color); */ ImGui_ImplDX12_RenderDrawData(viewport->DrawData, command_list); } static void ImGui_ImplDX12_SwapBuffers(ImGuiViewport* viewport, void*) { ImGuiViewportDataDx12* data = (ImGuiViewportDataDx12*)viewport->RendererUserData; IM_ASSERT(0); (void)data; /* data->SwapChain->Present(0, 0); // Present without vsync */ } void ImGui_ImplDX12_InitPlatformInterface() { ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); platform_io.Renderer_CreateWindow = ImGui_ImplDX12_CreateWindow; platform_io.Renderer_DestroyWindow = ImGui_ImplDX12_DestroyWindow; platform_io.Renderer_SetWindowSize = ImGui_ImplDX12_SetWindowSize; platform_io.Renderer_RenderWindow = ImGui_ImplDX12_RenderWindow; platform_io.Renderer_SwapBuffers = ImGui_ImplDX12_SwapBuffers; } void ImGui_ImplDX12_ShutdownPlatformInterface() { ImGui::DestroyPlatformWindows(); } ================================================ FILE: src/imgui/imgui_impl_dx12.hpp ================================================ // dear imgui: Renderer for DirectX12 // This needs to be used along with a Platform Binding (e.g. Win32) // Implemented features: // [X] Renderer: User texture binding. Use 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. // Issues: // [ ] 64-bit only for now! (Because sizeof(ImTextureId) == sizeof(void*)). See github.com/ocornut/imgui/pull/301 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. // https://github.com/ocornut/imgui #pragma once #include enum DXGI_FORMAT : std::int32_t; struct ID3D12Device; struct ID3D12GraphicsCommandList; struct D3D12_CPU_DESCRIPTOR_HANDLE; struct D3D12_GPU_DESCRIPTOR_HANDLE; // cmd_list is the command list that the implementation will use to render imgui draw lists. // Before calling the render function, caller must prepare cmd_list by resetting it and setting the appropriate // render target and descriptor heap that contains font_srv_cpu_desc_handle/font_srv_gpu_desc_handle. // 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. IMGUI_IMPL_API bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format, D3D12_CPU_DESCRIPTOR_HANDLE font_srv_cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE font_srv_gpu_desc_handle); IMGUI_IMPL_API void ImGui_ImplDX12_Shutdown(); IMGUI_IMPL_API void ImGui_ImplDX12_NewFrame(); IMGUI_IMPL_API void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* graphics_command_list); // Use if you want to reset your rendering device without losing ImGui state. IMGUI_IMPL_API void ImGui_ImplDX12_InvalidateDeviceObjects(); IMGUI_IMPL_API bool ImGui_ImplDX12_CreateDeviceObjects(); // Wisp Extension IMGUI_IMPL_API bool ImGui_ImplDX12_IsInitialized(); ================================================ FILE: src/imgui/imgui_impl_win32.cpp ================================================ // dear imgui: Platform Binding for Windows (standard windows API for 32 and 64 bits applications) // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..) // Implemented features: // [X] Platform: Clipboard support (for Win32 this is actually part of core imgui) // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. // [X] Platform: Keyboard arrays indexed using VK_* Virtual Key Codes, e.g. ImGui::IsKeyPressed(VK_SPACE). // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. #include "imgui.hpp" #include "imgui_impl_win32.hpp" #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include #include #include // CHANGELOG // (minor and older changes stripped away, please see git history for details) // 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. // 2019-01-17: Misc: Using GetForegroundWindow()+IsChild() instead of GetActiveWindow() to be compatible with windows created in a different thread or parent. // 2019-01-17: Inputs: Added support for mouse buttons 4 and 5 via WM_XBUTTON* messages. // 2019-01-15: Inputs: Added support for XInput gamepads (if ImGuiConfigFlags_NavEnableGamepad is set by user application). // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor. // 2018-06-10: Inputs: Fixed handling of mouse wheel messages to support fine position messages (typically sent by track-pads). // 2018-06-08: Misc: Extracted imgui_impl_win32.cpp/.h away from the old combined DX9/DX10/DX11/DX12 examples. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling). // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space. // 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set). // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support. // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert. // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag. // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read. // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging. // 2016-11-12: Inputs: Only call Win32 ::SetCursor(NULL) when io.MouseDrawCursor is set. // Win32 Data static HWND g_hWnd = 0; static INT64 g_Time = 0; static INT64 g_TicksPerSecond = 0; static ImGuiMouseCursor g_LastMouseCursor = ImGuiMouseCursor_COUNT; static bool g_HasGamepad = false; static bool g_WantUpdateHasGamepad = true; static bool g_WantUpdateMonitors = true; // Forward Declarations static void ImGui_ImplWin32_InitPlatformInterface(); static void ImGui_ImplWin32_ShutdownPlatformInterface(); static void ImGui_ImplWin32_UpdateMonitors(); // Functions bool ImGui_ImplWin32_Init(void* hwnd) { if (!::QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond)) return false; if (!::QueryPerformanceCounter((LARGE_INTEGER *)&g_Time)) return false; // Setup back-end capabilities flags ImGuiIO& io = ImGui::GetIO(); io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy) io.BackendPlatformName = "imgui_impl_win32"; // Our mouse update function expect PlatformHandle to be filled for the main viewport g_hWnd = (HWND)hwnd; ImGuiViewport* main_viewport = ImGui::GetMainViewport(); main_viewport->PlatformHandle = (void*)g_hWnd; if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) ImGui_ImplWin32_InitPlatformInterface(); // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime. io.KeyMap[ImGuiKey_Tab] = VK_TAB; io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT; io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT; io.KeyMap[ImGuiKey_UpArrow] = VK_UP; io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN; io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR; io.KeyMap[ImGuiKey_PageDown] = VK_NEXT; io.KeyMap[ImGuiKey_Home] = VK_HOME; io.KeyMap[ImGuiKey_End] = VK_END; io.KeyMap[ImGuiKey_Insert] = VK_INSERT; io.KeyMap[ImGuiKey_Delete] = VK_DELETE; io.KeyMap[ImGuiKey_Backspace] = VK_BACK; io.KeyMap[ImGuiKey_Space] = VK_SPACE; io.KeyMap[ImGuiKey_Enter] = VK_RETURN; io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE; io.KeyMap[ImGuiKey_A] = 'A'; io.KeyMap[ImGuiKey_C] = 'C'; io.KeyMap[ImGuiKey_V] = 'V'; io.KeyMap[ImGuiKey_X] = 'X'; io.KeyMap[ImGuiKey_Y] = 'Y'; io.KeyMap[ImGuiKey_Z] = 'Z'; return true; } void ImGui_ImplWin32_Shutdown() { ImGui_ImplWin32_ShutdownPlatformInterface(); g_hWnd = (HWND)0; } static bool ImGui_ImplWin32_UpdateMouseCursor() { ImGuiIO& io = ImGui::GetIO(); if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) return false; ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) { // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor ::SetCursor(NULL); } else { // Show OS mouse cursor LPTSTR win32_cursor = IDC_ARROW; switch (imgui_cursor) { case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break; case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break; case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break; case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break; case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break; case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break; case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break; case ImGuiMouseCursor_Hand: win32_cursor = IDC_HAND; break; } ::SetCursor(::LoadCursor(NULL, win32_cursor)); } return true; } // This code supports multi-viewports (multiple OS Windows mapped into different Dear ImGui viewports) // Because of that, it is a little more complicated than your typical single-viewport binding code! static void ImGui_ImplWin32_UpdateMousePos() { ImGuiIO& io = ImGui::GetIO(); // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) // (When multi-viewports are enabled, all imgui positions are same as OS positions) if (io.WantSetMousePos) { POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y }; if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0) ::ClientToScreen(g_hWnd, &pos); ::SetCursorPos(pos.x, pos.y); } io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); io.MouseHoveredViewport = 0; // Set imgui mouse position POINT mouse_screen_pos; if (!::GetCursorPos(&mouse_screen_pos)) return; if (HWND focused_hwnd = ::GetForegroundWindow()) { if (::IsChild(focused_hwnd, g_hWnd)) focused_hwnd = g_hWnd; if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { // 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) // This is the position you can get with GetCursorPos(). In theory adding viewport->Pos is also the reverse operation of doing ScreenToClient(). if (ImGui::FindViewportByPlatformHandle((void*)focused_hwnd) != NULL) io.MousePos = ImVec2((float)mouse_screen_pos.x, (float)mouse_screen_pos.y); } else { // 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.) // This is the position you can get with GetCursorPos() + ScreenToClient() or from WM_MOUSEMOVE. if (focused_hwnd == g_hWnd) { POINT mouse_client_pos = mouse_screen_pos; ::ScreenToClient(focused_hwnd, &mouse_client_pos); io.MousePos = ImVec2((float)mouse_client_pos.x, (float)mouse_client_pos.y); } } } // (Optional) When using multiple viewports: set io.MouseHoveredViewport to the viewport the OS mouse cursor is hovering. // Important: this information is not easy to provide and many high-level windowing library won't be able to provide it correctly, because // - This is _ignoring_ viewports with the ImGuiViewportFlags_NoInputs flag (pass-through windows). // - This is _regardless_ of whether another viewport is focused or being dragged from. // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the back-end, imgui will ignore this field and infer the information by relying on the // 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. if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos)) if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hovered_hwnd)) if ((viewport->Flags & ImGuiViewportFlags_NoInputs) == 0) // FIXME: We still get our NoInputs window with WM_NCHITTEST/HTTRANSPARENT code when decorated? io.MouseHoveredViewport = viewport->ID; } #ifdef _MSC_VER #pragma comment(lib, "xinput") #endif // Gamepad navigation mapping static void ImGui_ImplWin32_UpdateGamepads() { ImGuiIO& io = ImGui::GetIO(); memset(io.NavInputs, 0, sizeof(io.NavInputs)); if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) return; // Calling XInputGetState() every frame on disconnected gamepads is unfortunately too slow. // Instead we refresh gamepad availability by calling XInputGetCapabilities() _only_ after receiving WM_DEVICECHANGE. if (g_WantUpdateHasGamepad) { XINPUT_CAPABILITIES caps; g_HasGamepad = (XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS); g_WantUpdateHasGamepad = false; } XINPUT_STATE xinput_state; io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; if (g_HasGamepad && XInputGetState(0, &xinput_state) == ERROR_SUCCESS) { const XINPUT_GAMEPAD& gamepad = xinput_state.Gamepad; io.BackendFlags |= ImGuiBackendFlags_HasGamepad; #define MAP_BUTTON(NAV_NO, BUTTON_ENUM) { io.NavInputs[NAV_NO] = (gamepad.wButtons & BUTTON_ENUM) ? 1.0f : 0.0f; } #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; } MAP_BUTTON(ImGuiNavInput_Activate, XINPUT_GAMEPAD_A); // Cross / A MAP_BUTTON(ImGuiNavInput_Cancel, XINPUT_GAMEPAD_B); // Circle / B MAP_BUTTON(ImGuiNavInput_Menu, XINPUT_GAMEPAD_X); // Square / X MAP_BUTTON(ImGuiNavInput_Input, XINPUT_GAMEPAD_Y); // Triangle / Y MAP_BUTTON(ImGuiNavInput_DpadLeft, XINPUT_GAMEPAD_DPAD_LEFT); // D-Pad Left MAP_BUTTON(ImGuiNavInput_DpadRight, XINPUT_GAMEPAD_DPAD_RIGHT); // D-Pad Right MAP_BUTTON(ImGuiNavInput_DpadUp, XINPUT_GAMEPAD_DPAD_UP); // D-Pad Up MAP_BUTTON(ImGuiNavInput_DpadDown, XINPUT_GAMEPAD_DPAD_DOWN); // D-Pad Down MAP_BUTTON(ImGuiNavInput_FocusPrev, XINPUT_GAMEPAD_LEFT_SHOULDER); // L1 / LB MAP_BUTTON(ImGuiNavInput_FocusNext, XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB MAP_BUTTON(ImGuiNavInput_TweakSlow, XINPUT_GAMEPAD_LEFT_SHOULDER); // L1 / LB MAP_BUTTON(ImGuiNavInput_TweakFast, XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB MAP_ANALOG(ImGuiNavInput_LStickLeft, gamepad.sThumbLX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768); MAP_ANALOG(ImGuiNavInput_LStickRight, gamepad.sThumbLX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); MAP_ANALOG(ImGuiNavInput_LStickUp, gamepad.sThumbLY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); MAP_ANALOG(ImGuiNavInput_LStickDown, gamepad.sThumbLY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32767); #undef MAP_BUTTON #undef MAP_ANALOG } } void ImGui_ImplWin32_NewFrame() { ImGuiIO& io = ImGui::GetIO(); 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()."); // Setup display size (every frame to accommodate for window resizing) RECT rect; ::GetClientRect(g_hWnd, &rect); io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); if (g_WantUpdateMonitors) ImGui_ImplWin32_UpdateMonitors(); // Setup time step INT64 current_time; ::QueryPerformanceCounter((LARGE_INTEGER *)¤t_time); io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond; g_Time = current_time; // Read keyboard modifiers inputs io.KeyCtrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0; io.KeyShift = (::GetKeyState(VK_SHIFT) & 0x8000) != 0; io.KeyAlt = (::GetKeyState(VK_MENU) & 0x8000) != 0; io.KeySuper = false; // io.KeysDown[], io.MousePos, io.MouseDown[], io.MouseWheel: filled by the WndProc handler below. // Update OS mouse position ImGui_ImplWin32_UpdateMousePos(); // Update OS mouse cursor with the cursor requested by imgui ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor(); if (g_LastMouseCursor != mouse_cursor) { g_LastMouseCursor = mouse_cursor; ImGui_ImplWin32_UpdateMouseCursor(); } // Update game controllers (if enabled and available) ImGui_ImplWin32_UpdateGamepads(); } // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions. #ifndef WM_MOUSEHWHEEL #define WM_MOUSEHWHEEL 0x020E #endif #ifndef DBT_DEVNODES_CHANGED #define DBT_DEVNODES_CHANGED 0x0007 #endif // Process Win32 mouse/keyboard inputs. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. // 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. // 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. IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { if (ImGui::GetCurrentContext() == NULL) return 0; ImGuiIO& io = ImGui::GetIO(); switch (msg) { case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK: case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK: case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK: case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK: { int button = 0; if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; } if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; } if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; } if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; } if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL) ::SetCapture(hwnd); io.MouseDown[button] = true; return 0; } case WM_LBUTTONUP: case WM_RBUTTONUP: case WM_MBUTTONUP: case WM_XBUTTONUP: { int button = 0; if (msg == WM_LBUTTONUP) { button = 0; } if (msg == WM_RBUTTONUP) { button = 1; } if (msg == WM_MBUTTONUP) { button = 2; } if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; } io.MouseDown[button] = false; if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd) ::ReleaseCapture(); return 0; } case WM_MOUSEWHEEL: io.MouseWheel += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA; return 0; case WM_MOUSEHWHEEL: io.MouseWheelH += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA; return 0; case WM_KEYDOWN: case WM_SYSKEYDOWN: if (wParam < 256) io.KeysDown[wParam] = 1; return 0; case WM_KEYUP: case WM_SYSKEYUP: if (wParam < 256) io.KeysDown[wParam] = 0; return 0; case WM_CHAR: // You can also use ToAscii()+GetKeyboardState() to retrieve characters. if (wParam > 0 && wParam < 0x10000) io.AddInputCharacter((unsigned short)wParam); return 0; case WM_SETCURSOR: if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor()) return 1; return 0; case WM_DEVICECHANGE: if ((UINT)wParam == DBT_DEVNODES_CHANGED) g_WantUpdateHasGamepad = true; return 0; case WM_DISPLAYCHANGE: g_WantUpdateMonitors = true; return 0; } return 0; } //-------------------------------------------------------------------------------------------------------- // DPI handling // Those in theory should be simple calls but Windows has multiple ways to handle DPI, and most of them // require recent Windows versions at runtime or recent Windows SDK at compile-time. Neither we want to depend on. // So we dynamically select and load those functions to avoid dependencies. This is the scheme successfully // used by GLFW (from which we borrowed some of the code here) and other applications aiming to be portable. //--------------------------------------------------------------------------------------------------------- // At this point ImGui_ImplWin32_EnableDpiAwareness() is just a helper called by main.cpp, we don't call it automatically. //--------------------------------------------------------------------------------------------------------- static BOOL IsWindowsVersionOrGreater(WORD major, WORD minor, WORD sp) { OSVERSIONINFOEXW osvi = { sizeof(osvi), major, minor, 0, 0,{ 0 }, sp }; DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR; ULONGLONG cond = VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL); cond = VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL); cond = VerSetConditionMask(cond, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL); return VerifyVersionInfoW(&osvi, mask, cond); } #define IsWindows8Point1OrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) // _WIN32_WINNT_WINBLUE #define IsWindows10OrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0A00), LOBYTE(0x0A00), 0) // _WIN32_WINNT_WIN10 #ifndef DPI_ENUMS_DECLARED typedef enum { PROCESS_DPI_UNAWARE = 0, PROCESS_SYSTEM_DPI_AWARE = 1, PROCESS_PER_MONITOR_DPI_AWARE = 2 } PROCESS_DPI_AWARENESS; typedef enum { MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2, MDT_DEFAULT = MDT_EFFECTIVE_DPI } MONITOR_DPI_TYPE; #endif #ifndef _DPI_AWARENESS_CONTEXTS_ DECLARE_HANDLE(DPI_AWARENESS_CONTEXT); #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE (DPI_AWARENESS_CONTEXT)-3 #endif #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (DPI_AWARENESS_CONTEXT)-4 #endif typedef HRESULT(WINAPI * PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); // Shcore.lib+dll, Windows 8.1 typedef HRESULT(WINAPI * PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); // Shcore.lib+dll, Windows 8.1 typedef DPI_AWARENESS_CONTEXT(WINAPI * PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib+dll, Windows 10 v1607 (Creators Update) void ImGui_ImplWin32_EnableDpiAwareness() { // if (IsWindows10OrGreater()) // FIXME-DPI: This needs a manifest to succeed. Instead we try to grab the function pointer. { static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll"); // Reference counted per-process if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext")) { SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); return; } } if (IsWindows8Point1OrGreater()) { static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness")) SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE); } else { SetProcessDPIAware(); } } #ifdef _MSC_VER #pragma comment(lib, "gdi32") // GetDeviceCaps() #endif float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor) { UINT xdpi = 96, ydpi = 96; if (::IsWindows8Point1OrGreater()) { static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process if (PFN_GetDpiForMonitor GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor")) GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi); } else { const HDC dc = ::GetDC(NULL); xdpi = ::GetDeviceCaps(dc, LOGPIXELSX); ydpi = ::GetDeviceCaps(dc, LOGPIXELSY); ::ReleaseDC(NULL, dc); } IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert! return xdpi / 96.0f; } float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd) { HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST); return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor); } //-------------------------------------------------------------------------------------------------------- // IME (Input Method Editor) basic support for e.g. Asian language users //-------------------------------------------------------------------------------------------------------- #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) && !defined(__GNUC__) #define HAS_WIN32_IME 1 #include #ifdef _MSC_VER #pragma comment(lib, "imm32") #endif static void ImGui_ImplWin32_SetImeInputPos(ImGuiViewport* viewport, ImVec2 pos) { COMPOSITIONFORM cf = { CFS_FORCE_POSITION,{ (LONG)(pos.x - viewport->Pos.x), (LONG)(pos.y - viewport->Pos.y) },{ 0, 0, 0, 0 } }; if (HWND hwnd = (HWND)viewport->PlatformHandle) if (HIMC himc = ::ImmGetContext(hwnd)) { ::ImmSetCompositionWindow(himc, &cf); ::ImmReleaseContext(hwnd, himc); } } #else #define HAS_WIN32_IME 0 #endif //-------------------------------------------------------------------------------------------------------- // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT // This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously. // 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.. //-------------------------------------------------------------------------------------------------------- struct ImGuiViewportDataWin32 { HWND Hwnd; bool HwndOwned; DWORD DwStyle; DWORD DwExStyle; ImGuiViewportDataWin32() { Hwnd = NULL; HwndOwned = false; DwStyle = DwExStyle = 0; } ~ImGuiViewportDataWin32() { IM_ASSERT(Hwnd == NULL); } }; static void ImGui_ImplWin32_GetWin32StyleFromViewportFlags(ImGuiViewportFlags flags, DWORD* out_style, DWORD* out_ex_style) { if (flags & ImGuiViewportFlags_NoDecoration) *out_style = WS_POPUP; else *out_style = WS_OVERLAPPEDWINDOW; if (flags & ImGuiViewportFlags_NoTaskBarIcon) *out_ex_style = WS_EX_TOOLWINDOW; else *out_ex_style = WS_EX_APPWINDOW; if (flags & ImGuiViewportFlags_TopMost) *out_ex_style |= WS_EX_TOPMOST; } static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport) { ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)(); viewport->PlatformUserData = data; // Select style and parent window ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &data->DwStyle, &data->DwExStyle); HWND parent_window = NULL; if (viewport->ParentViewportId != 0) if (ImGuiViewport* parent_viewport = ImGui::FindViewportByID(viewport->ParentViewportId)) parent_window = (HWND)parent_viewport->PlatformHandle; // Create window RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) }; ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); data->Hwnd = ::CreateWindowEx( data->DwExStyle, _T("ImGui Platform"), _T("Untitled"), data->DwStyle, // Style, class name, window name rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, // Window area parent_window, NULL, ::GetModuleHandle(NULL), NULL); // Parent window, Menu, Instance, Param data->HwndOwned = true; viewport->PlatformRequestResize = false; viewport->PlatformHandle = data->Hwnd; } static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport) { if (ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData) { if (::GetCapture() == data->Hwnd) { // Transfer capture so if we started dragging from a window that later disappears, we'll still receive the MOUSEUP event. ::ReleaseCapture(); ::SetCapture(g_hWnd); } if (data->Hwnd && data->HwndOwned) ::DestroyWindow(data->Hwnd); data->Hwnd = NULL; IM_DELETE(data); } viewport->PlatformUserData = viewport->PlatformHandle = NULL; } static void ImGui_ImplWin32_ShowWindow(ImGuiViewport* viewport) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ::ShowWindow(data->Hwnd, SW_SHOWNA); else ::ShowWindow(data->Hwnd, SW_SHOW); } static void ImGui_ImplWin32_UpdateWindow(ImGuiViewport* viewport) { // (Optional) Update Win32 style if it changed _after_ creation. // Generally they won't change unless configuration flags are changed, but advanced uses (such as manually rewriting viewport flags) make this useful. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); DWORD new_style; DWORD new_ex_style; ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &new_style, &new_ex_style); // Only reapply the flags that have been changed from our point of view (as other flags are being modified by Windows) if (data->DwStyle != new_style || data->DwExStyle != new_ex_style) { data->DwStyle = new_style; data->DwExStyle = new_ex_style; ::SetWindowLong(data->Hwnd, GWL_STYLE, data->DwStyle); ::SetWindowLong(data->Hwnd, GWL_EXSTYLE, data->DwExStyle); RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) }; ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen ::SetWindowPos(data->Hwnd, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); ::ShowWindow(data->Hwnd, SW_SHOWNA); // This is necessary when we alter the style viewport->PlatformRequestMove = viewport->PlatformRequestResize = true; } } static ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); POINT pos = { 0, 0 }; ::ClientToScreen(data->Hwnd, &pos); return ImVec2((float)pos.x, (float)pos.y); } static void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y }; ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); ::SetWindowPos(data->Hwnd, NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE); } static ImVec2 ImGui_ImplWin32_GetWindowSize(ImGuiViewport* viewport) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); RECT rect; ::GetClientRect(data->Hwnd, &rect); return ImVec2(float(rect.right - rect.left), float(rect.bottom - rect.top)); } static void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y }; ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen ::SetWindowPos(data->Hwnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE); } static void ImGui_ImplWin32_SetWindowFocus(ImGuiViewport* viewport) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); ::BringWindowToTop(data->Hwnd); ::SetForegroundWindow(data->Hwnd); ::SetFocus(data->Hwnd); } static bool ImGui_ImplWin32_GetWindowFocus(ImGuiViewport* viewport) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); return ::GetForegroundWindow() == data->Hwnd; } static bool ImGui_ImplWin32_GetWindowMinimized(ImGuiViewport* viewport) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); return ::IsIconic(data->Hwnd) != 0; } static void ImGui_ImplWin32_SetWindowTitle(ImGuiViewport* viewport, const char* title) { // ::SetWindowTextA() doesn't properly handle UTF-8 so we explicitely convert our string. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); int n = ::MultiByteToWideChar(CP_UTF8, 0, title, -1, NULL, 0); ImVector title_w; title_w.resize(n); ::MultiByteToWideChar(CP_UTF8, 0, title, -1, title_w.Data, n); ::SetWindowTextW(data->Hwnd, title_w.Data); } static void ImGui_ImplWin32_SetWindowAlpha(ImGuiViewport* viewport, float alpha) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f); if (alpha < 1.0f) { DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED; ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style); ::SetLayeredWindowAttributes(data->Hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA); } else { DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED; ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style); } } static float ImGui_ImplWin32_GetWindowDpiScale(ImGuiViewport* viewport) { ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData; IM_ASSERT(data->Hwnd != 0); return ImGui_ImplWin32_GetDpiScaleForHwnd(data->Hwnd); } // FIXME-DPI: Testing DPI related ideas static void ImGui_ImplWin32_OnChangedViewport(ImGuiViewport* viewport) { (void)viewport; #if 0 ImGuiStyle default_style; //default_style.WindowPadding = ImVec2(0, 0); //default_style.WindowBorderSize = 0.0f; //default_style.ItemSpacing.y = 3.0f; //default_style.FramePadding = ImVec2(0, 0); default_style.ScaleAllSizes(viewport->DpiScale); ImGuiStyle& style = ImGui::GetStyle(); style = default_style; #endif } static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) return true; if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd)) { switch (msg) { case WM_CLOSE: viewport->PlatformRequestClose = true; return 0; case WM_MOVE: viewport->PlatformRequestMove = true; break; case WM_SIZE: viewport->PlatformRequestResize = true; break; case WM_MOUSEACTIVATE: if (viewport->Flags & ImGuiViewportFlags_NoFocusOnClick) return MA_NOACTIVATE; break; case WM_NCHITTEST: // Let mouse pass-through the window. This will allow the back-end to set io.MouseHoveredViewport properly (which is OPTIONAL). // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging. // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system. if (viewport->Flags & ImGuiViewportFlags_NoInputs) return HTTRANSPARENT; break; } } return DefWindowProc(hWnd, msg, wParam, lParam); } static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, HDC, LPRECT, LPARAM) { MONITORINFO info = { 0 }; info.cbSize = sizeof(MONITORINFO); if (!::GetMonitorInfo(monitor, &info)) return TRUE; ImGuiPlatformMonitor imgui_monitor; imgui_monitor.MainPos = ImVec2((float)info.rcMonitor.left, (float)info.rcMonitor.top); imgui_monitor.MainSize = ImVec2((float)(info.rcMonitor.right - info.rcMonitor.left), (float)(info.rcMonitor.bottom - info.rcMonitor.top)); imgui_monitor.WorkPos = ImVec2((float)info.rcWork.left, (float)info.rcWork.top); imgui_monitor.WorkSize = ImVec2((float)(info.rcWork.right - info.rcWork.left), (float)(info.rcWork.bottom - info.rcWork.top)); imgui_monitor.DpiScale = ImGui_ImplWin32_GetDpiScaleForMonitor(monitor); ImGuiPlatformIO& io = ImGui::GetPlatformIO(); if (info.dwFlags & MONITORINFOF_PRIMARY) io.Monitors.push_front(imgui_monitor); else io.Monitors.push_back(imgui_monitor); return TRUE; } static void ImGui_ImplWin32_UpdateMonitors() { ImGui::GetPlatformIO().Monitors.resize(0); ::EnumDisplayMonitors(NULL, NULL, ImGui_ImplWin32_UpdateMonitors_EnumFunc, NULL); g_WantUpdateMonitors = false; } static void ImGui_ImplWin32_InitPlatformInterface() { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = ImGui_ImplWin32_WndProcHandler_PlatformWindow; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = ::GetModuleHandle(NULL); wcex.hIcon = NULL; wcex.hCursor = NULL; wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1); wcex.lpszMenuName = NULL; wcex.lpszClassName = _T("ImGui Platform"); wcex.hIconSm = NULL; ::RegisterClassEx(&wcex); ImGui_ImplWin32_UpdateMonitors(); // Register platform interface (will be coupled with a renderer interface) ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); platform_io.Platform_CreateWindow = ImGui_ImplWin32_CreateWindow; platform_io.Platform_DestroyWindow = ImGui_ImplWin32_DestroyWindow; platform_io.Platform_ShowWindow = ImGui_ImplWin32_ShowWindow; platform_io.Platform_SetWindowPos = ImGui_ImplWin32_SetWindowPos; platform_io.Platform_GetWindowPos = ImGui_ImplWin32_GetWindowPos; platform_io.Platform_SetWindowSize = ImGui_ImplWin32_SetWindowSize; platform_io.Platform_GetWindowSize = ImGui_ImplWin32_GetWindowSize; platform_io.Platform_SetWindowFocus = ImGui_ImplWin32_SetWindowFocus; platform_io.Platform_GetWindowFocus = ImGui_ImplWin32_GetWindowFocus; platform_io.Platform_GetWindowMinimized = ImGui_ImplWin32_GetWindowMinimized; platform_io.Platform_SetWindowTitle = ImGui_ImplWin32_SetWindowTitle; platform_io.Platform_SetWindowAlpha = ImGui_ImplWin32_SetWindowAlpha; platform_io.Platform_UpdateWindow = ImGui_ImplWin32_UpdateWindow; platform_io.Platform_GetWindowDpiScale = ImGui_ImplWin32_GetWindowDpiScale; // FIXME-DPI platform_io.Platform_OnChangedViewport = ImGui_ImplWin32_OnChangedViewport; // FIXME-DPI #if HAS_WIN32_IME platform_io.Platform_SetImeInputPos = ImGui_ImplWin32_SetImeInputPos; #endif // Register main window handle (which is owned by the main application, not by us) ImGuiViewport* main_viewport = ImGui::GetMainViewport(); ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)(); data->Hwnd = g_hWnd; data->HwndOwned = false; main_viewport->PlatformUserData = data; main_viewport->PlatformHandle = (void*)g_hWnd; } static void ImGui_ImplWin32_ShutdownPlatformInterface() { ::UnregisterClass(_T("ImGui Platform"), ::GetModuleHandle(NULL)); } ================================================ FILE: src/imgui/imgui_impl_win32.hpp ================================================ // dear imgui: Platform Binding for Windows (standard windows API for 32 and 64 bits applications) // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..) // Implemented features: // [X] Platform: Clipboard support (for Win32 this is actually part of core imgui) // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. // [X] Platform: Keyboard arrays indexed using VK_* Virtual Key Codes, e.g. ImGui::IsKeyPressed(VK_SPACE). // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. #pragma once IMGUI_IMPL_API bool ImGui_ImplWin32_Init(void* hwnd); IMGUI_IMPL_API void ImGui_ImplWin32_Shutdown(); IMGUI_IMPL_API void ImGui_ImplWin32_NewFrame(); // DPI-related helpers (which run and compile without requiring 8.1 or 10, neither Windows version, neither associated SDK) IMGUI_IMPL_API void ImGui_ImplWin32_EnableDpiAwareness(); IMGUI_IMPL_API float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd); // HWND hwnd IMGUI_IMPL_API float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor); // HMONITOR monitor // Handler for Win32 messages, update mouse/keyboard data. // You may or not need this for your implementation, but it can serve as reference for handling inputs. // Intentionally commented out to avoid dragging dependencies on types. You can COPY this line into your .cpp code instead. /* IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); */ ================================================ FILE: src/imgui/imgui_internal.hpp ================================================ // dear imgui, v1.70 WIP // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! // Set: // #define IMGUI_DEFINE_MATH_OPERATORS // 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) /* Index of this file: // Header mess // Forward declarations // STB libraries includes // Context pointer // Generic helpers // Misc data structures // Main imgui context // Tab bar, tab item // Internal API */ #pragma once //----------------------------------------------------------------------------- // Header mess //----------------------------------------------------------------------------- #ifndef IMGUI_VERSION #error Must include imgui.h before imgui_internal.h #endif #include // FILE* #include // NULL, malloc, free, qsort, atoi, atof #include // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf #include // INT_MIN, INT_MAX #ifdef _MSC_VER #pragma warning (push) #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) #endif #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-function" // for stb_textedit.h #pragma clang diagnostic ignored "-Wmissing-prototypes" // for stb_textedit.h #pragma clang diagnostic ignored "-Wold-style-cast" #if __has_warning("-Wzero-as-null-pointer-constant") #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" #endif #if __has_warning("-Wdouble-promotion") #pragma clang diagnostic ignored "-Wdouble-promotion" #endif #endif //----------------------------------------------------------------------------- // Forward declarations //----------------------------------------------------------------------------- struct ImRect; // An axis-aligned rectangle (2 points) struct ImDrawDataBuilder; // Helper to build a ImDrawData instance struct ImDrawListSharedData; // Data shared between all ImDrawList instances struct ImGuiColorMod; // Stacked color modifier, backup of modified data so we can restore it struct ImGuiColumnData; // Storage data for a single column struct ImGuiColumns; // Storage data for a columns set struct ImGuiContext; // Main imgui context struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum struct ImGuiDockContext; // Docking system context struct ImGuiDockNode; // Docking system node (hold a list of Windows OR two child dock nodes) struct ImGuiDockNodeSettings; // Storage for a dock node in .ini file (we preserve those even if the associated dock node isn't active during the session) struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box struct ImGuiItemHoveredDataBackup; // Backup and restore IsItemHovered() internal data struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only struct ImGuiNavMoveResult; // Result of a directional navigation move query result struct ImGuiNextWindowData; // Storage for SetNexWindow** functions struct ImGuiPopupData; // Storage for current popup stack struct ImGuiSettingsHandler; // Storage for one type registered in the .ini file struct ImGuiStyleMod; // Stacked style modifier, backup of modified data so we can restore it struct ImGuiTabBar; // Storage for a tab bar struct ImGuiTabItem; // Storage for a tab item (within a tab bar) struct ImGuiWindow; // Storage for one window struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame) struct 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) // Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. typedef int ImGuiDataAuthority; // -> enum ImGuiDataAuthority_ // Enum: for storing the source authority (dock node vs window) of a field typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical typedef int ImGuiButtonFlags; // -> enum ImGuiButtonFlags_ // Flags: for ButtonEx(), ButtonBehavior() typedef int ImGuiDragFlags; // -> enum ImGuiDragFlags_ // Flags: for DragBehavior() typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag() typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for DC.LastItemStatusFlags typedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight() typedef int ImGuiNavDirSourceFlags; // -> enum ImGuiNavDirSourceFlags_ // Flags: for GetNavInputAmount2d() typedef int ImGuiNavMoveFlags; // -> enum ImGuiNavMoveFlags_ // Flags: for navigation requests typedef int ImGuiSeparatorFlags; // -> enum ImGuiSeparatorFlags_ // Flags: for Separator() - internal typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // Flags: for SliderBehavior() typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx() //------------------------------------------------------------------------- // STB libraries includes //------------------------------------------------------------------------- namespace ImStb { #undef STB_TEXTEDIT_STRING #undef STB_TEXTEDIT_CHARTYPE #define STB_TEXTEDIT_STRING ImGuiInputTextState #define STB_TEXTEDIT_CHARTYPE ImWchar #define STB_TEXTEDIT_GETWIDTH_NEWLINE -1.0f #define STB_TEXTEDIT_UNDOSTATECOUNT 99 #define STB_TEXTEDIT_UNDOCHARCOUNT 999 #include "imstb_textedit.hpp" } // namespace ImStb //----------------------------------------------------------------------------- // Context pointer //----------------------------------------------------------------------------- #ifndef GImGui extern IMGUI_API ImGuiContext* GImGui; // Current implicit ImGui context pointer #endif // Internal Drag and Drop payload types. String starting with '_' are reserved for Dear ImGui. #define IMGUI_PAYLOAD_TYPE_WINDOW "_IMWINDOW" // Payload == ImGuiWindow* //----------------------------------------------------------------------------- // Generic helpers //----------------------------------------------------------------------------- #define IM_PI 3.14159265358979323846f #ifdef _WIN32 #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!) #else #define IM_NEWLINE "\n" #endif #define IM_TABSIZE (4) #define IMGUI_DEBUG_LOG(_FMT,...) printf("[%05d] " _FMT, GImGui->FrameCount, __VA_ARGS__) #define IM_STATIC_ASSERT(_COND) typedef char static_assertion_##__line__[(_COND)?1:-1] #define IM_F32_TO_INT8_UNBOUND(_VAL) ((int)((_VAL) * 255.0f + ((_VAL)>=0 ? 0.5f : -0.5f))) // Unsaturated, for display purpose #define IM_F32_TO_INT8_SAT(_VAL) ((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255 // Enforce cdecl calling convention for functions called by the standard library, in case compilation settings changed the default to e.g. __vectorcall #ifdef _MSC_VER #define IMGUI_CDECL __cdecl #else #define IMGUI_CDECL #endif // Helpers: UTF-8 <> wchar IMGUI_API int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count IMGUI_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 IMGUI_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 IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count) IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end); // return number of bytes to express one char in UTF-8 IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 // Helpers: Misc IMGUI_API ImU32 ImHashData(const void* data, size_t data_size, ImU32 seed = 0); IMGUI_API ImU32 ImHashStr(const char* data, size_t data_size = 0, ImU32 seed = 0); IMGUI_API void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size = NULL, int padding_bytes = 0); IMGUI_API FILE* ImFileOpen(const char* filename, const char* file_open_mode); static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; } static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; } static inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & (v - 1)) == 0; } static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } #define ImQsort qsort #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS static 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] #endif // Helpers: Geometry IMGUI_API ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p); IMGUI_API bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); IMGUI_API ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); IMGUI_API void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w); IMGUI_API ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy); // Helpers: String IMGUI_API int ImStricmp(const char* str1, const char* str2); IMGUI_API int ImStrnicmp(const char* str1, const char* str2, size_t count); IMGUI_API void ImStrncpy(char* dst, const char* src, size_t count); IMGUI_API char* ImStrdup(const char* str); IMGUI_API char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* str); IMGUI_API const char* ImStrchrRange(const char* str_begin, const char* str_end, char c); IMGUI_API int ImStrlenW(const ImWchar* str); IMGUI_API const char* ImStreolRange(const char* str, const char* str_end); // End end-of-line IMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line IMGUI_API const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end); IMGUI_API void ImStrTrimBlanks(char* str); IMGUI_API const char* ImStrSkipBlank(const char* str); IMGUI_API int ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) IM_FMTARGS(3); IMGUI_API int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) IM_FMTLIST(3); IMGUI_API const char* ImParseFormatFindStart(const char* format); IMGUI_API const char* ImParseFormatFindEnd(const char* format); IMGUI_API const char* ImParseFormatTrimDecorations(const char* format, char* buf, size_t buf_size); IMGUI_API int ImParseFormatPrecision(const char* format, int default_value); // Helpers: ImVec2/ImVec4 operators // 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.) // We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself. #ifdef IMGUI_DEFINE_MATH_OPERATORS static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); } static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x/rhs, lhs.y/rhs); } static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x+rhs.x, lhs.y+rhs.y); } static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); } static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); } static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); } static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; } static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; } static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; } static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; } static 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); } static 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); } static 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); } #endif // Helpers: Maths // - Wrapper for standard libs functions. (Note that imgui_demo.cpp does _not_ use them to keep the code easy to copy) #ifndef IMGUI_DISABLE_MATH_FUNCTIONS static inline float ImFabs(float x) { return fabsf(x); } static inline float ImSqrt(float x) { return sqrtf(x); } static inline float ImPow(float x, float y) { return powf(x, y); } static inline double ImPow(double x, double y) { return pow(x, y); } static inline float ImFmod(float x, float y) { return fmodf(x, y); } static inline double ImFmod(double x, double y) { return fmod(x, y); } static inline float ImCos(float x) { return cosf(x); } static inline float ImSin(float x) { return sinf(x); } static inline float ImAcos(float x) { return acosf(x); } static inline float ImAtan2(float y, float x) { return atan2f(y, x); } static inline double ImAtof(const char* s) { return atof(s); } static 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) static inline float ImCeil(float x) { return ceilf(x); } #endif // - ImMin/ImMax/ImClamp/ImLerp/ImSwap are used by widgets which support for variety of types: signed/unsigned int/long long float/double // (Exceptionally using templates here but we could also redefine them for variety of types) template static inline T ImMin(T lhs, T rhs) { return lhs < rhs ? lhs : rhs; } template static inline T ImMax(T lhs, T rhs) { return lhs >= rhs ? lhs : rhs; } template static inline T ImClamp(T v, T mn, T mx) { return (v < mn) ? mn : (v > mx) ? mx : v; } template static inline T ImLerp(T a, T b, float t) { return (T)(a + (b - a) * t); } template static inline void ImSwap(T& a, T& b) { T tmp = a; a = b; b = tmp; } template 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; } template 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; } // - Misc maths helpers static 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); } static 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); } static 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); } static 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); } static 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); } static 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); } static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; } static inline float ImLengthSqr(const ImVec2& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y; } static inline float ImLengthSqr(const ImVec4& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z + lhs.w*lhs.w; } static 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; } static inline float ImFloor(float f) { return (float)(int)f; } static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2((float)(int)v.x, (float)(int)v.y); } static inline int ImModPositive(int a, int b) { return (a + b) % b; } static inline float ImDot(const ImVec2& a, const ImVec2& b) { return a.x * b.x + a.y * b.y; } static 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); } static 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; } static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } // Helper: ImBoolVector. Store 1-bit per value. // Note that Resize() currently clears the whole vector. struct ImBoolVector { ImVector Storage; ImBoolVector() { } void Resize(int sz) { Storage.resize((sz + 31) >> 5); memset(Storage.Data, 0, (size_t)Storage.Size * sizeof(Storage.Data[0])); } void Clear() { Storage.clear(); } bool GetBit(int n) const { int off = (n >> 5); int mask = 1 << (n & 31); return (Storage[off] & mask) != 0; } void SetBit(int n, bool v) { int off = (n >> 5); int mask = 1 << (n & 31); if (v) Storage[off] |= mask; else Storage[off] &= ~mask; } }; // 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, // Honor constructor/destructor. Add/remove invalidate all pointers. Indexes have the same lifetime as the associated object. typedef int ImPoolIdx; template struct IMGUI_API ImPool { ImVector Data; // Contiguous data ImGuiStorage Map; // ID->Index ImPoolIdx FreeIdx; // Next free idx to use ImPool() { FreeIdx = 0; } ~ImPool() { Clear(); } T* GetByKey(ImGuiID key) { int idx = Map.GetInt(key, -1); return (idx != -1) ? &Data[idx] : NULL; } T* GetByIndex(ImPoolIdx n) { return &Data[n]; } ImPoolIdx GetIndex(const T* p) const { IM_ASSERT(p >= Data.Data && p < Data.Data + Data.Size); return (ImPoolIdx)(p - Data.Data); } T* GetOrAddByKey(ImGuiID key) { int* p_idx = Map.GetIntRef(key, -1); if (*p_idx != -1) return &Data[*p_idx]; *p_idx = FreeIdx; return Add(); } bool Contains(const T* p) const { return (p >= Data.Data && p < Data.Data + Data.Size); } 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; } 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]; } void Remove(ImGuiID key, const T* p) { Remove(key, GetIndex(p)); } void Remove(ImGuiID key, ImPoolIdx idx) { Data[idx].~T(); *(int*)&Data[idx] = FreeIdx; FreeIdx = idx; Map.SetInt(key, -1); } void Reserve(int capacity) { Data.reserve(capacity); Map.Data.reserve(capacity); } int GetSize() const { return Data.Size; } }; //----------------------------------------------------------------------------- // Misc data structures //----------------------------------------------------------------------------- enum ImGuiButtonFlags_ { ImGuiButtonFlags_None = 0, ImGuiButtonFlags_Repeat = 1 << 0, // hold to repeat ImGuiButtonFlags_PressedOnClickRelease = 1 << 1, // [Default] return true on click + release on same item ImGuiButtonFlags_PressedOnClick = 1 << 2, // return true on click (default requires click+release) ImGuiButtonFlags_PressedOnRelease = 1 << 3, // return true on release (default requires click+release) ImGuiButtonFlags_PressedOnDoubleClick = 1 << 4, // return true on double-click (default requires click+release) ImGuiButtonFlags_FlattenChildren = 1 << 5, // allow interactions even if a child window is overlapping ImGuiButtonFlags_AllowItemOverlap = 1 << 6, // require previous frame HoveredId to either match id or be null before being usable, use along with SetItemAllowOverlap() ImGuiButtonFlags_DontClosePopups = 1 << 7, // disable automatically closing parent popup on press // [UNUSED] ImGuiButtonFlags_Disabled = 1 << 8, // disable interactions 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 ImGuiButtonFlags_NoKeyModifiers = 1 << 10, // disable interaction if a key modifier is held ImGuiButtonFlags_NoHoldingActiveID = 1 << 11, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) ImGuiButtonFlags_PressedOnDragDropHold = 1 << 12, // press when held into while we are drag and dropping another item (used by e.g. tree nodes, collapsing headers) ImGuiButtonFlags_NoNavFocus = 1 << 13, // don't override navigation focus when activated ImGuiButtonFlags_NoHoveredOnNav = 1 << 14 // don't report as hovered when navigated on }; enum ImGuiSliderFlags_ { ImGuiSliderFlags_None = 0, ImGuiSliderFlags_Vertical = 1 << 0 }; enum ImGuiDragFlags_ { ImGuiDragFlags_None = 0, ImGuiDragFlags_Vertical = 1 << 0 }; enum ImGuiColumnsFlags_ { // Default: 0 ImGuiColumnsFlags_None = 0, ImGuiColumnsFlags_NoBorder = 1 << 0, // Disable column dividers ImGuiColumnsFlags_NoResize = 1 << 1, // Disable resizing columns when clicking on the dividers ImGuiColumnsFlags_NoPreserveWidths = 1 << 2, // Disable column width preservation when adjusting columns ImGuiColumnsFlags_NoForceWithinWindow = 1 << 3, // Disable forcing columns to fit within window 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. }; enum ImGuiSelectableFlagsPrivate_ { // NB: need to be in sync with last value of ImGuiSelectableFlags_ ImGuiSelectableFlags_NoHoldingActiveID = 1 << 10, ImGuiSelectableFlags_PressedOnClick = 1 << 11, ImGuiSelectableFlags_PressedOnRelease = 1 << 12, ImGuiSelectableFlags_DrawFillAvailWidth = 1 << 13, ImGuiSelectableFlags_AllowItemOverlap = 1 << 14 }; enum ImGuiSeparatorFlags_ { ImGuiSeparatorFlags_None = 0, ImGuiSeparatorFlags_Horizontal = 1 << 0, // Axis default to current layout type, so generally Horizontal unless e.g. in a menu bar ImGuiSeparatorFlags_Vertical = 1 << 1 }; // Transient per-window flags, reset at the beginning of the frame. For child window, inherited from parent on first Begin(). // This is going to be exposed in imgui.h when stabilized enough. enum ImGuiItemFlags_ { ImGuiItemFlags_NoTabStop = 1 << 0, // false ImGuiItemFlags_ButtonRepeat = 1 << 1, // false // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings. ImGuiItemFlags_Disabled = 1 << 2, // false // [BETA] Disable interactions but doesn't affect visuals yet. See github.com/ocornut/imgui/issues/211 ImGuiItemFlags_NoNav = 1 << 3, // false ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // MenuItem/Selectable() automatically closes current Popup window ImGuiItemFlags_Default_ = 0 }; // Storage for LastItem data enum ImGuiItemStatusFlags_ { ImGuiItemStatusFlags_None = 0, ImGuiItemStatusFlags_HoveredRect = 1 << 0, ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, ImGuiItemStatusFlags_Edited = 1 << 2, // Value exposed by item was edited in the current frame (should match the bool return value of most widgets) 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. #ifdef IMGUI_ENABLE_TEST_ENGINE , // [imgui-test only] ImGuiItemStatusFlags_Openable = 1 << 10, // ImGuiItemStatusFlags_Opened = 1 << 11, // ImGuiItemStatusFlags_Checkable = 1 << 12, // ImGuiItemStatusFlags_Checked = 1 << 13 // #endif }; enum ImGuiTextFlags_ { ImGuiTextFlags_None = 0, ImGuiTextFlags_NoWidthForLargeClippedText = 1 << 0 }; // FIXME: this is in development, not exposed/functional as a generic feature yet. // Horizontal/Vertical enums are fixed to 0/1 so they may be used to index ImVec2 enum ImGuiLayoutType_ { ImGuiLayoutType_Horizontal = 0, ImGuiLayoutType_Vertical = 1 }; enum ImGuiLogType { ImGuiLogType_None = 0, ImGuiLogType_TTY, ImGuiLogType_File, ImGuiLogType_Buffer, ImGuiLogType_Clipboard }; // X/Y enums are fixed to 0/1 so they may be used to index ImVec2 enum ImGuiAxis { ImGuiAxis_None = -1, ImGuiAxis_X = 0, ImGuiAxis_Y = 1 }; enum ImGuiPlotType { ImGuiPlotType_Lines, ImGuiPlotType_Histogram }; enum ImGuiInputSource { ImGuiInputSource_None = 0, ImGuiInputSource_Mouse, ImGuiInputSource_Nav, ImGuiInputSource_NavKeyboard, // Only used occasionally for storage, not tested/handled by most code ImGuiInputSource_NavGamepad, // " ImGuiInputSource_COUNT }; // FIXME-NAV: Clarify/expose various repeat delay/rate enum ImGuiInputReadMode { ImGuiInputReadMode_Down, ImGuiInputReadMode_Pressed, ImGuiInputReadMode_Released, ImGuiInputReadMode_Repeat, ImGuiInputReadMode_RepeatSlow, ImGuiInputReadMode_RepeatFast }; enum ImGuiNavHighlightFlags_ { ImGuiNavHighlightFlags_None = 0, ImGuiNavHighlightFlags_TypeDefault = 1 << 0, ImGuiNavHighlightFlags_TypeThin = 1 << 1, ImGuiNavHighlightFlags_AlwaysDraw = 1 << 2, // Draw rectangular highlight if (g.NavId == id) _even_ when using the mouse. ImGuiNavHighlightFlags_NoRounding = 1 << 3 }; enum ImGuiNavDirSourceFlags_ { ImGuiNavDirSourceFlags_None = 0, ImGuiNavDirSourceFlags_Keyboard = 1 << 0, ImGuiNavDirSourceFlags_PadDPad = 1 << 1, ImGuiNavDirSourceFlags_PadLStick = 1 << 2 }; enum ImGuiNavMoveFlags_ { ImGuiNavMoveFlags_None = 0, ImGuiNavMoveFlags_LoopX = 1 << 0, // On failed request, restart from opposite side ImGuiNavMoveFlags_LoopY = 1 << 1, ImGuiNavMoveFlags_WrapX = 1 << 2, // On failed request, request from opposite side one line down (when NavDir==right) or one line up (when NavDir==left) ImGuiNavMoveFlags_WrapY = 1 << 3, // This is not super useful for provided for completeness 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) ImGuiNavMoveFlags_AlsoScoreVisibleSet = 1 << 5 // Store alternate result in NavMoveResultLocalVisibleSet that only comprise elements that are already fully visible. }; enum ImGuiNavForward { ImGuiNavForward_None, ImGuiNavForward_ForwardQueued, ImGuiNavForward_ForwardActive }; enum ImGuiNavLayer { ImGuiNavLayer_Main = 0, // Main scrolling layer ImGuiNavLayer_Menu = 1, // Menu layer (access with Alt/ImGuiNavInput_Menu) ImGuiNavLayer_COUNT }; enum ImGuiPopupPositionPolicy { ImGuiPopupPositionPolicy_Default, ImGuiPopupPositionPolicy_ComboBox }; // 1D vector (this odd construct is used to facilitate the transition between 1D and 2D, and the maintenance of some branches/patches) struct ImVec1 { float x; ImVec1() { x = 0.0f; } ImVec1(float _x) { x = _x; } }; // 2D vector (half-size integer) struct ImVec2ih { short x, y; ImVec2ih() { x = y = 0; } ImVec2ih(short _x, short _y) { x = _x; y = _y; } }; // 2D axis aligned bounding-box // NB: we can't rely on ImVec2 math operators being available here struct IMGUI_API ImRect { ImVec2 Min; // Upper-left ImVec2 Max; // Lower-right ImRect() : Min(FLT_MAX,FLT_MAX), Max(-FLT_MAX,-FLT_MAX) {} ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {} ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {} ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {} ImVec2 GetCenter() const { return ImVec2((Min.x + Max.x) * 0.5f, (Min.y + Max.y) * 0.5f); } ImVec2 GetSize() const { return ImVec2(Max.x - Min.x, Max.y - Min.y); } float GetWidth() const { return Max.x - Min.x; } float GetHeight() const { return Max.y - Min.y; } ImVec2 GetTL() const { return Min; } // Top-left ImVec2 GetTR() const { return ImVec2(Max.x, Min.y); } // Top-right ImVec2 GetBL() const { return ImVec2(Min.x, Max.y); } // Bottom-left ImVec2 GetBR() const { return Max; } // Bottom-right bool Contains(const ImVec2& p) const { return p.x >= Min.x && p.y >= Min.y && p.x < Max.x && p.y < Max.y; } 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; } 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; } 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; } 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; } void Expand(const float amount) { Min.x -= amount; Min.y -= amount; Max.x += amount; Max.y += amount; } void Expand(const ImVec2& amount) { Min.x -= amount.x; Min.y -= amount.y; Max.x += amount.x; Max.y += amount.y; } void Translate(const ImVec2& d) { Min.x += d.x; Min.y += d.y; Max.x += d.x; Max.y += d.y; } void TranslateX(float dx) { Min.x += dx; Max.x += dx; } void TranslateY(float dy) { Min.y += dy; Max.y += dy; } 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. 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. 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; } bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; } }; // Type information associated to one ImGuiDataType. Retrieve with DataTypeGetInfo(). struct ImGuiDataTypeInfo { size_t Size; // Size in byte const char* PrintFmt; // Default printf format for the type const char* ScanFmt; // Default scanf format for the type }; // Stacked color modifier, backup of modified data so we can restore it struct ImGuiColorMod { ImGuiCol Col; ImVec4 BackupValue; }; // Stacked style modifier, backup of modified data so we can restore it. Data type inferred from the variable. struct ImGuiStyleMod { ImGuiStyleVar VarIdx; union { int BackupInt[2]; float BackupFloat[2]; }; ImGuiStyleMod(ImGuiStyleVar idx, int v) { VarIdx = idx; BackupInt[0] = v; } ImGuiStyleMod(ImGuiStyleVar idx, float v) { VarIdx = idx; BackupFloat[0] = v; } ImGuiStyleMod(ImGuiStyleVar idx, ImVec2 v) { VarIdx = idx; BackupFloat[0] = v.x; BackupFloat[1] = v.y; } }; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiGroupData { ImVec2 BackupCursorPos; ImVec2 BackupCursorMaxPos; ImVec1 BackupIndent; ImVec1 BackupGroupOffset; ImVec2 BackupCurrentLineSize; float BackupCurrentLineTextBaseOffset; ImGuiID BackupActiveIdIsAlive; bool BackupActiveIdPreviousFrameIsAlive; bool AdvanceCursor; }; // Simple column measurement, currently used for MenuItem() only.. This is very short-sighted/throw-away code and NOT a generic helper. struct IMGUI_API ImGuiMenuColumns { float Spacing; float Width, NextWidth; float Pos[3], NextWidths[3]; ImGuiMenuColumns(); void Update(int count, float spacing, bool clear); float DeclColumns(float w0, float w1, float w2); float CalcExtraSpace(float avail_w); }; // Internal state of the currently focused/edited text input box struct IMGUI_API ImGuiInputTextState { ImGuiID ID; // widget id owning the text state 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. ImVector TextW; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer. ImVector TextA; // temporary UTF8 buffer for callbacks and other operations. this is not updated in every code-path! size=capacity. ImVector InitialTextA; // backup of end-user buffer at the time of focus (in UTF-8, unaltered) bool TextAIsValid; // temporary UTF8 buffer is not initially valid before we make the widget active (until then we pull the data from user argument) int BufCapacityA; // end-user buffer capacity float ScrollX; // horizontal scrolling/offset ImStb::STB_TexteditState Stb; // state for stb_textedit.h float CursorAnim; // timer for cursor blink, reset on every user action so the cursor reappears immediately bool CursorFollow; // set when we want scrolling to follow the current cursor position (not always!) bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection // Temporarily set when active ImGuiInputTextFlags UserFlags; ImGuiInputTextCallback UserCallback; void* UserCallbackData; ImGuiInputTextState() { memset(this, 0, sizeof(*this)); } void ClearFreeMemory() { TextW.clear(); TextA.clear(); InitialTextA.clear(); } void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking void CursorClamp() { Stb.cursor = ImMin(Stb.cursor, CurLenW); Stb.select_start = ImMin(Stb.select_start, CurLenW); Stb.select_end = ImMin(Stb.select_end, CurLenW); } bool HasSelection() const { return Stb.select_start != Stb.select_end; } void ClearSelection() { Stb.select_start = Stb.select_end = Stb.cursor; } void SelectAll() { Stb.select_start = 0; Stb.cursor = Stb.select_end = CurLenW; Stb.has_preferred_x = 0; } int GetUndoAvailCount() const { return Stb.undostate.undo_point; } int GetRedoAvailCount() const { return STB_TEXTEDIT_UNDOSTATECOUNT - Stb.undostate.redo_point; } void OnKeyPressed(int key); // Cannot be inline because we call in code in stb_textedit.h implementation }; // Windows data saved in imgui.ini file struct ImGuiWindowSettings { char* Name; ImGuiID ID; ImVec2 Pos; // NB: Settings position are stored RELATIVE to the viewport! Whereas runtime ones are absolute positions. ImVec2 Size; ImVec2 ViewportPos; ImGuiID ViewportId; ImGuiID DockId; // ID of last known DockNode (even if the DockNode is invisible because it has only 1 active window), or 0 if none. ImGuiID ClassId; // ID of window class if specified 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. bool Collapsed; ImGuiWindowSettings() { Name = NULL; ID = 0; Pos = Size = ViewportPos = ImVec2(0, 0); ViewportId = DockId = ClassId = 0; DockOrder = -1; Collapsed = false; } }; struct ImGuiSettingsHandler { const char* TypeName; // Short description stored in .ini file. Disallowed characters: '[' ']' ImGuiID TypeHash; // == ImHashStr(TypeName) void* (*ReadOpenFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name); // Read: Called when entering into a new ini entry e.g. "[Window][Name]" void (*ReadLineFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line); // Read: Called for every line of text within an ini entry void (*WriteAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out_buf); // Write: Output every entries into 'out_buf' void* UserData; ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); } }; // Storage for current popup stack struct ImGuiPopupData { ImGuiID PopupId; // Set on OpenPopup() ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup() ImGuiWindow* SourceWindow; // Set on OpenPopup() copy of NavWindow at the time of opening the popup int OpenFrameCount; // Set on OpenPopup() 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) ImVec2 OpenPopupPos; // Set on OpenPopup(), preferred popup position (typically == OpenMousePos when using mouse) ImVec2 OpenMousePos; // Set on OpenPopup(), copy of mouse position at the time of opening popup ImGuiPopupData() { PopupId = 0; Window = SourceWindow = NULL; OpenFrameCount = -1; OpenParentId = 0; } }; struct ImGuiColumnData { float OffsetNorm; // Column start offset, normalized 0.0 (far left) -> 1.0 (far right) float OffsetNormBeforeResize; ImGuiColumnsFlags Flags; // Not exposed ImRect ClipRect; ImGuiColumnData() { OffsetNorm = OffsetNormBeforeResize = 0.0f; Flags = ImGuiColumnsFlags_None; } }; struct ImGuiColumns { ImGuiID ID; ImGuiColumnsFlags Flags; bool IsFirstFrame; bool IsBeingResized; int Current; int Count; float MinX, MaxX; float LineMinY, LineMaxY; float BackupCursorPosY; // Backup of CursorPos at the time of BeginColumns() float BackupCursorMaxPosX; // Backup of CursorMaxPos at the time of BeginColumns() ImVector Columns; ImGuiColumns() { Clear(); } void Clear() { ID = 0; Flags = ImGuiColumnsFlags_None; IsFirstFrame = false; IsBeingResized = false; Current = 0; Count = 1; MinX = MaxX = 0.0f; LineMinY = LineMaxY = 0.0f; BackupCursorPosY = 0.0f; BackupCursorMaxPosX = 0.0f; Columns.clear(); } }; // Data shared between all ImDrawList instances struct IMGUI_API ImDrawListSharedData { ImVec2 TexUvWhitePixel; // UV of white pixel in the atlas ImFont* Font; // Current/default font (optional, for simplified AddText overload) float FontSize; // Current/default font size (optional, for simplified AddText overload) float CurveTessellationTol; ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen() // Const data // FIXME: Bake rounded corners fill/borders in atlas ImVec2 CircleVtx12[12]; ImDrawListSharedData(); }; struct ImDrawDataBuilder { ImVector Layers[2]; // Global layers for: regular, tooltip void Clear() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].resize(0); } void ClearFreeMemory() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].clear(); } IMGUI_API void FlattenIntoSingleLayer(); }; enum ImGuiViewportFlagsPrivate_ { ImGuiViewportFlags_CanHostOtherWindows = 1 << 10 // Normal viewports are associated to a single window. The main viewport can host multiple windows. }; // ImGuiViewport Private/Internals fields (cardinal sin: we are using inheritance!) // Note that every instance of ImGuiViewport is in fact a ImGuiViewportP. struct ImGuiViewportP : public ImGuiViewport { int Idx; int LastFrameActive; // Last frame number this viewport was activated by a window int LastFrameDrawLists[2]; // Last frame number the background (0) and foreground (1) draw lists were used 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 ImGuiID LastNameHash; ImVec2 LastPos; float Alpha; // Window opacity (when dragging dockable windows/viewports we make them transparent) float LastAlpha; short PlatformMonitor; bool PlatformWindowCreated; ImGuiWindow* Window; // Set when the viewport is owned by a window (and ImGuiViewportFlags_CanHostOtherWindows is NOT set) 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. ImDrawData DrawDataP; ImDrawDataBuilder DrawDataBuilder; ImVec2 LastPlatformPos; ImVec2 LastPlatformSize; ImVec2 LastRendererSize; 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); } ~ImGuiViewportP() { if (DrawLists[0]) IM_DELETE(DrawLists[0]); if (DrawLists[1]) IM_DELETE(DrawLists[1]); } ImRect GetRect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } ImVec2 GetCenter() const { return ImVec2(Pos.x + Size.x * 0.5f, Pos.y + Size.y * 0.5f); } void ClearRequestFlags() { PlatformRequestClose = PlatformRequestMove = PlatformRequestResize = false; } }; struct ImGuiNavMoveResult { ImGuiID ID; // Best candidate ImGuiID SelectScopeId;// Best candidate window current selectable group ID ImGuiWindow* Window; // Best candidate window float DistBox; // Best candidate box distance to current NavId float DistCenter; // Best candidate center distance to current NavId float DistAxial; ImRect RectRel; // Best candidate bounding box in window relative space ImGuiNavMoveResult() { Clear(); } void Clear() { ID = SelectScopeId = 0; Window = NULL; DistBox = DistCenter = DistAxial = FLT_MAX; RectRel = ImRect(); } }; // Storage for SetNexWindow** functions struct ImGuiNextWindowData { ImGuiCond PosCond; ImGuiCond SizeCond; ImGuiCond ContentSizeCond; ImGuiCond CollapsedCond; ImGuiCond SizeConstraintCond; ImGuiCond FocusCond; ImGuiCond BgAlphaCond; ImGuiCond ViewportCond; ImGuiCond DockCond; ImVec2 PosVal; ImVec2 PosPivotVal; ImVec2 SizeVal; ImVec2 ContentSizeVal; bool PosUndock; bool CollapsedVal; ImRect SizeConstraintRect; ImGuiSizeCallback SizeCallback; void* SizeCallbackUserData; float BgAlphaVal; ImGuiID ViewportId; ImGuiID DockId; ImGuiWindowClass WindowClass; ImVec2 MenuBarOffsetMinVal; // This is not exposed publicly, so we don't clear it. ImGuiNextWindowData() { PosCond = SizeCond = ContentSizeCond = CollapsedCond = SizeConstraintCond = FocusCond = BgAlphaCond = ViewportCond = DockCond = 0; PosVal = PosPivotVal = SizeVal = ImVec2(0.0f, 0.0f); ContentSizeVal = ImVec2(0.0f, 0.0f); PosUndock = CollapsedVal = false; SizeConstraintRect = ImRect(); SizeCallback = NULL; SizeCallbackUserData = NULL; BgAlphaVal = FLT_MAX; ViewportId = DockId = 0; MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f); } void Clear() { PosCond = SizeCond = ContentSizeCond = CollapsedCond = SizeConstraintCond = FocusCond = BgAlphaCond = ViewportCond = DockCond = 0; WindowClass = ImGuiWindowClass(); } }; //----------------------------------------------------------------------------- // Docking, Tabs //----------------------------------------------------------------------------- struct ImGuiTabBarSortItem { int Index; float Width; }; struct ImGuiTabBarRef { ImGuiTabBar* Ptr; // Either field can be set, not both. Dock node tab bars are loose while BeginTabBar() ones are in a pool. int IndexInMainPool; ImGuiTabBarRef(ImGuiTabBar* ptr) { Ptr = ptr; IndexInMainPool = -1; } ImGuiTabBarRef(int index_in_main_pool) { Ptr = NULL; IndexInMainPool = index_in_main_pool; } }; enum ImGuiDockNodeFlagsPrivate_ { // [Internal] 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. ImGuiDockNodeFlags_CentralNode = 1 << 11, // Local ImGuiDockNodeFlags_NoTabBar = 1 << 12, // Local // Tab bar is completely unavailable. No triangle in the corner to enable it back. 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) ImGuiDockNodeFlags_SharedFlagsInheritMask_ = ~0, ImGuiDockNodeFlags_LocalFlagsMask_ = ImGuiDockNodeFlags_NoSplit | ImGuiDockNodeFlags_NoResize | ImGuiDockNodeFlags_AutoHideTabBar | ImGuiDockNodeFlags_DockSpace | ImGuiDockNodeFlags_CentralNode | ImGuiDockNodeFlags_NoTabBar | ImGuiDockNodeFlags_HiddenTabBar, ImGuiDockNodeFlags_LocalFlagsTransferMask_ = ImGuiDockNodeFlags_LocalFlagsMask_ & ~ImGuiDockNodeFlags_DockSpace // When splitting those flags are moved to the inheriting child, never duplicated }; // Store the source authority (dock node vs window) of a field enum ImGuiDataAuthority_ { ImGuiDataAuthority_Auto, ImGuiDataAuthority_DockNode, ImGuiDataAuthority_Window }; // sizeof() 116~160 struct ImGuiDockNode { ImGuiID ID; ImGuiDockNodeFlags SharedFlags; // Flags shared by all nodes of a same dockspace hierarchy (inherited from the root node) ImGuiDockNodeFlags LocalFlags; // Flags specific to this node ImGuiDockNode* ParentNode; ImGuiDockNode* ChildNodes[2]; // [Split node only] Child nodes (left/right or top/bottom). Consider switching to an array. ImVector Windows; // Note: unordered list! Iterate TabBar->Tabs for user-order. ImGuiTabBar* TabBar; ImVec2 Pos; // Current position ImVec2 Size; // Current size ImVec2 SizeRef; // [Split node only] Last explicitly written-to size (overridden when using a splitter affecting the node), used to calculate Size. int SplitAxis; // [Split node only] Split axis (X or Y) ImGuiWindowClass WindowClass; ImGuiWindow* HostWindow; ImGuiWindow* VisibleWindow; // Generally point to window which is ID is == SelectedTabID, but when CTRL+Tabbing this can be a different window. ImGuiDockNode* CentralNode; // [Root node only] Pointer to central node. ImGuiDockNode* OnlyNodeWithWindows; // [Root node only] Set when there is a single visible node within the hierarchy. int LastFrameAlive; // Last frame number the node was updated or kept alive explicitly with DockSpace() + ImGuiDockNodeFlags_KeepAliveOnly int LastFrameActive; // Last frame number the node was updated. int LastFrameFocused; // Last frame number the node was focused. ImGuiID LastFocusedNodeID; // [Root node only] Which of our child docking node (any ancestor in the hierarchy) was last focused. ImGuiID SelectedTabID; // [Tab node only] Which of our tab is selected. ImGuiID WantCloseTabID; // [Tab node only] Set when closing a specific tab. ImGuiDataAuthority AuthorityForPos :3; ImGuiDataAuthority AuthorityForSize :3; ImGuiDataAuthority AuthorityForViewport :3; bool IsVisible :1; // Set to false when the node is hidden (usually disabled as it has no active window) bool IsFocused :1; bool HasCloseButton :1; bool HasCollapseButton :1; bool WantCloseAll :1; // Set when closing all tabs at once. bool WantLockSizeOnce :1; bool WantMouseMove :1; // After a node extraction we need to transition toward moving the newly created host window bool WantHiddenTabBarUpdate :1; bool WantHiddenTabBarToggle :1; ImGuiDockNode(ImGuiID id); ~ImGuiDockNode(); bool IsRootNode() const { return ParentNode == NULL; } bool IsDockSpace() const { return (LocalFlags & ImGuiDockNodeFlags_DockSpace) != 0; } bool IsCentralNode() const { return (LocalFlags & ImGuiDockNodeFlags_CentralNode) != 0; } bool IsHiddenTabBar() const { return (LocalFlags & ImGuiDockNodeFlags_HiddenTabBar) != 0; } // Hidden tab bar can be shown back by clicking the small triangle bool IsNoTabBar() const { return (LocalFlags & ImGuiDockNodeFlags_NoTabBar) != 0; } // Never show a tab bar bool IsSplitNode() const { return ChildNodes[0] != NULL; } bool IsLeafNode() const { return ChildNodes[0] == NULL; } bool IsEmpty() const { return ChildNodes[0] == NULL && Windows.Size == 0; } ImGuiDockNodeFlags GetMergedFlags() const { return SharedFlags | LocalFlags; } ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } }; //----------------------------------------------------------------------------- // Main imgui context //----------------------------------------------------------------------------- struct ImGuiContext { bool Initialized; bool FrameScopeActive; // Set by NewFrame(), cleared by EndFrame() bool FrameScopePushedImplicitWindow; // Set by NewFrame(), cleared by EndFrame() bool FontAtlasOwnedByContext; // Io.Fonts-> is owned by the ImGuiContext and will be destructed along with it. ImGuiIO IO; ImGuiPlatformIO PlatformIO; ImGuiStyle Style; ImGuiConfigFlags ConfigFlagsForFrame; // = g.IO.ConfigFlags at the time of NewFrame() ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back() float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window. float FontBaseSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Base text height. ImDrawListSharedData DrawListSharedData; double Time; int FrameCount; int FrameCountEnded; int FrameCountPlatformEnded; int FrameCountRendered; ImVector Windows; // Windows, sorted in display order, back to front ImVector WindowsFocusOrder; // Windows, sorted in focus order, back to front ImVector WindowsSortBuffer; ImVector CurrentWindowStack; ImGuiStorage WindowsById; int WindowsActiveCount; ImGuiWindow* CurrentWindow; // Being drawn into ImGuiWindow* HoveredWindow; // Will catch mouse inputs ImGuiWindow* HoveredRootWindow; // Will catch mouse inputs (for focus/move only) ImGuiWindow* HoveredWindowUnderMovingWindow; // Hovered window ignoring MovingWindow. Only set if MovingWindow is set. ImGuiID HoveredId; // Hovered widget bool HoveredIdAllowOverlap; ImGuiID HoveredIdPreviousFrame; float HoveredIdTimer; // Measure contiguous hovering time float HoveredIdNotActiveTimer; // Measure contiguous hovering time where the item has not been active ImGuiID ActiveId; // Active widget ImGuiID ActiveIdPreviousFrame; ImGuiID ActiveIdIsAlive; // Active widget has been seen this frame (we can't use a bool as the ActiveId may change within the frame) float ActiveIdTimer; bool ActiveIdIsJustActivated; // Set at the time of activation for one frame bool ActiveIdAllowOverlap; // Active widget allows another widget to steal active id (generally for overlapping widgets, but not always) 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. bool ActiveIdHasBeenEdited; // Was the value associated to the widget Edited over the course of the Active state. bool ActiveIdPreviousFrameIsAlive; bool ActiveIdPreviousFrameHasBeenEdited; int ActiveIdAllowNavDirFlags; // Active widget allows using directional navigation (e.g. can activate a button and move away from it) int ActiveIdBlockNavInputFlags; ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior) ImGuiWindow* ActiveIdWindow; ImGuiWindow* ActiveIdPreviousFrameWindow; ImGuiInputSource ActiveIdSource; // Activating with mouse or nav (gamepad/keyboard) ImGuiID LastActiveId; // Store the last non-zero ActiveId, useful for animation. float LastActiveIdTimer; // Store the last non-zero ActiveId timer since the beginning of activation, useful for animation. ImVec2 LastValidMousePos; ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actually window that is moved is generally MovingWindow->RootWindow. ImVector ColorModifiers; // Stack for PushStyleColor()/PopStyleColor() ImVector StyleModifiers; // Stack for PushStyleVar()/PopStyleVar() ImVector FontStack; // Stack for PushFont()/PopFont() ImVectorOpenPopupStack; // Which popups are open (persistent) ImVectorBeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) ImGuiNextWindowData NextWindowData; // Storage for SetNextWindow** functions bool NextTreeNodeOpenVal; // Storage for SetNextTreeNode** functions ImGuiCond NextTreeNodeOpenCond; // Viewports ImVector Viewports; // Active viewports (always 1+, and generally 1 unless multi-viewports are enabled). Each viewports hold their copy of ImDrawData. ImGuiViewportP* CurrentViewport; // We track changes of viewport (happening in Begin) so we can call Platform_OnChangedViewport() ImGuiViewportP* MouseViewport; ImGuiViewportP* MouseLastHoveredViewport; // Last known viewport that was hovered by mouse (even if we are not hovering any viewport any more) + honoring the _NoInputs flag. ImGuiID PlatformLastFocusedViewport; // Record of last focused platform window/viewport, when this changes we stamp the viewport as front-most int ViewportFrontMostStampCount; // Every time the front-most window changes, we stamp its viewport with an incrementing counter // Navigation data (for gamepad/keyboard) ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusWindow' ImGuiID NavId; // Focused item for navigation ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0, also set when calling ActivateItem() ImGuiID NavActivateDownId; // ~~ IsNavInputDown(ImGuiNavInput_Activate) ? NavId : 0 ImGuiID NavActivatePressedId; // ~~ IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0 ImGuiID NavInputId; // ~~ IsNavInputPressed(ImGuiNavInput_Input) ? NavId : 0 ImGuiID NavJustTabbedId; // Just tabbed to this id. ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). ImGuiID NavJustMovedToMultiSelectScopeId; // Just navigated to this select scope id (result of a successfully MoveRequest). ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame. ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard. ImRect NavScoringRectScreen; // Rectangle used for scoring, in screen space. Based of window->DC.NavRefRectRel[], modified for directional navigation scoring. int NavScoringCount; // Metrics for debugging ImGuiWindow* NavWindowingTarget; // When selecting a window (holding Menu+FocusPrev/Next, or equivalent of CTRL-TAB) this window is temporarily displayed front-most. ImGuiWindow* NavWindowingTargetAnim; // Record of last valid NavWindowingTarget until DimBgRatio and NavWindowingHighlightAlpha becomes 0.0f ImGuiWindow* NavWindowingList; float NavWindowingTimer; float NavWindowingHighlightAlpha; bool NavWindowingToggleLayer; 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. int NavIdTabCounter; // == NavWindow->DC.FocusIdxTabCounter at time of NavId processing bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRefRectRel is valid bool NavMousePosDirty; // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default) 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) bool NavDisableMouseHover; // When user starts using gamepad/keyboard, we hide mouse hovering highlight until mouse is touched again. bool NavAnyRequest; // ~~ NavMoveRequest || NavInitRequest bool NavInitRequest; // Init request for appearing window to select first item bool NavInitRequestFromMove; ImGuiID NavInitResultId; ImRect NavInitResultRectRel; bool NavMoveFromClampedRefRect; // Set by manual scrolling, if we scroll to a point where NavId isn't visible we reset navigation from visible items bool NavMoveRequest; // Move request for this frame ImGuiNavMoveFlags NavMoveRequestFlags; ImGuiNavForward NavMoveRequestForward; // None / ForwardQueued / ForwardActive (this is used to navigate sibling parent menus from a child menu) ImGuiDir NavMoveDir, NavMoveDirLast; // Direction of the move request (left/right/up/down), direction of the previous move request ImGuiDir NavMoveClipDir; ImGuiNavMoveResult NavMoveResultLocal; // Best move request candidate within NavWindow ImGuiNavMoveResult NavMoveResultLocalVisibleSet; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag) ImGuiNavMoveResult NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag) // Tabbing system (older than Nav, active even if Nav is disabled. FIXME-NAV: This needs a redesign!) ImGuiWindow* FocusRequestCurrWindow; // ImGuiWindow* FocusRequestNextWindow; // 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) int FocusRequestCurrCounterTab; // Tab item being requested for focus, stored as an index int FocusRequestNextCounterAll; // Stored for next frame int FocusRequestNextCounterTab; // " bool FocusTabPressed; // // Render float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) ImGuiMouseCursor MouseCursor; // Drag and Drop bool DragDropActive; bool DragDropWithinSourceOrTarget; ImGuiDragDropFlags DragDropSourceFlags; int DragDropSourceFrameCount; int DragDropMouseButton; ImGuiPayload DragDropPayload; ImRect DragDropTargetRect; ImGuiID DragDropTargetId; ImGuiDragDropFlags DragDropAcceptFlags; float DragDropAcceptIdCurrRectSurface; // Target item surface (we resolve overlapping targets by prioritizing the smaller surface) ImGuiID DragDropAcceptIdCurr; // Target item id (set at the time of accepting the payload) ImGuiID DragDropAcceptIdPrev; // Target item id from previous frame (we need to store this to allow for overlapping drag and drop targets) int DragDropAcceptFrameCount; // Last time a target expressed a desire to accept the source ImVector DragDropPayloadBufHeap; // We don't expose the ImVector<> directly unsigned char DragDropPayloadBufLocal[8]; // Local buffer for small payloads // Tab bars ImPool TabBars; ImGuiTabBar* CurrentTabBar; ImVector CurrentTabBarStack; ImVector TabSortByWidthBuffer; // Widget state ImGuiInputTextState InputTextState; ImFont InputTextPasswordFont; ImGuiID TempInputTextId; // Temporary text input when CTRL+clicking on a slider, etc. ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets ImVec4 ColorPickerRef; bool DragCurrentAccumDirty; float DragCurrentAccum; // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio ImVec2 ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? int TooltipOverrideCount; ImVector PrivateClipboard; // If no custom clipboard handler is defined // Range-Select/Multi-Select // [This is unused in this branch, but left here to facilitate merging/syncing multiple branches] ImGuiID MultiSelectScopeId; // Platform support ImVec2 PlatformImePos; // Cursor position request & last passed to the OS Input Method Editor ImVec2 PlatformImeLastPos; ImGuiViewportP* PlatformImePosViewport; // Extensions // FIXME: We could provide an API to register one slot in an array held in ImGuiContext? ImGuiDockContext* DockContext; // Settings bool SettingsLoaded; float SettingsDirtyTimer; // Save .ini Settings to memory when time reaches zero ImGuiTextBuffer SettingsIniData; // In memory .ini settings ImVector SettingsHandlers; // List of .ini settings handlers ImVector SettingsWindows; // ImGuiWindow .ini settings entries (parsed from the last loaded .ini file and maintained on saving) // Logging bool LogEnabled; ImGuiLogType LogType; FILE* LogFile; // If != NULL log to stdout/ file ImGuiTextBuffer LogBuffer; // Accumulation buffer when log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators. float LogLinePosY; bool LogLineFirstItem; int LogDepthRef; int LogDepthToExpand; int LogDepthToExpandDefault; // Default/stored value for LogDepthMaxExpand if not specified in the LogXXX function call. // Misc float FramerateSecPerFrame[120]; // Calculate estimate of framerate for user over the last 2 seconds. int FramerateSecPerFrameIdx; float FramerateSecPerFrameAccum; int WantCaptureMouseNextFrame; // Explicit capture via CaptureKeyboardFromApp()/CaptureMouseFromApp() sets those flags int WantCaptureKeyboardNextFrame; int WantTextInputNextFrame; char TempBuffer[1024*3+1]; // Temporary text buffer ImGuiContext(ImFontAtlas* shared_font_atlas) { Initialized = false; FrameScopeActive = FrameScopePushedImplicitWindow = false; ConfigFlagsForFrame = ImGuiConfigFlags_None; Font = NULL; FontSize = FontBaseSize = 0.0f; FontAtlasOwnedByContext = shared_font_atlas ? false : true; IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)(); Time = 0.0f; FrameCount = 0; FrameCountEnded = FrameCountPlatformEnded = FrameCountRendered = -1; WindowsActiveCount = 0; CurrentWindow = NULL; HoveredWindow = NULL; HoveredRootWindow = NULL; HoveredWindowUnderMovingWindow = NULL; HoveredId = 0; HoveredIdAllowOverlap = false; HoveredIdPreviousFrame = 0; HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f; ActiveId = 0; ActiveIdPreviousFrame = 0; ActiveIdIsAlive = 0; ActiveIdTimer = 0.0f; ActiveIdIsJustActivated = false; ActiveIdAllowOverlap = false; ActiveIdHasBeenPressed = false; ActiveIdHasBeenEdited = false; ActiveIdPreviousFrameIsAlive = false; ActiveIdPreviousFrameHasBeenEdited = false; ActiveIdAllowNavDirFlags = 0x00; ActiveIdBlockNavInputFlags = 0x00; ActiveIdClickOffset = ImVec2(-1,-1); ActiveIdWindow = ActiveIdPreviousFrameWindow = NULL; ActiveIdSource = ImGuiInputSource_None; LastActiveId = 0; LastActiveIdTimer = 0.0f; LastValidMousePos = ImVec2(0.0f, 0.0f); MovingWindow = NULL; NextTreeNodeOpenVal = false; NextTreeNodeOpenCond = 0; CurrentViewport = NULL; MouseViewport = MouseLastHoveredViewport = NULL; PlatformLastFocusedViewport = 0; ViewportFrontMostStampCount = 0; NavWindow = NULL; NavId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0; NavJustTabbedId = NavJustMovedToId = NavJustMovedToMultiSelectScopeId = NavNextActivateId = 0; NavInputSource = ImGuiInputSource_None; NavScoringRectScreen = ImRect(); NavScoringCount = 0; NavWindowingTarget = NavWindowingTargetAnim = NavWindowingList = NULL; NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; NavWindowingToggleLayer = false; NavLayer = ImGuiNavLayer_Main; NavIdTabCounter = INT_MAX; NavIdIsAlive = false; NavMousePosDirty = false; NavDisableHighlight = true; NavDisableMouseHover = false; NavAnyRequest = false; NavInitRequest = false; NavInitRequestFromMove = false; NavInitResultId = 0; NavMoveFromClampedRefRect = false; NavMoveRequest = false; NavMoveRequestFlags = 0; NavMoveRequestForward = ImGuiNavForward_None; NavMoveDir = NavMoveDirLast = NavMoveClipDir = ImGuiDir_None; FocusRequestCurrWindow = FocusRequestNextWindow = NULL; FocusRequestCurrCounterAll = FocusRequestCurrCounterTab = INT_MAX; FocusRequestNextCounterAll = FocusRequestNextCounterTab = INT_MAX; FocusTabPressed = false; DimBgRatio = 0.0f; MouseCursor = ImGuiMouseCursor_Arrow; DragDropActive = DragDropWithinSourceOrTarget = false; DragDropSourceFlags = 0; DragDropSourceFrameCount = -1; DragDropMouseButton = -1; DragDropTargetId = 0; DragDropAcceptFlags = 0; DragDropAcceptIdCurrRectSurface = 0.0f; DragDropAcceptIdPrev = DragDropAcceptIdCurr = 0; DragDropAcceptFrameCount = -1; memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal)); CurrentTabBar = NULL; TempInputTextId = 0; ColorEditOptions = ImGuiColorEditFlags__OptionsDefault; DragCurrentAccumDirty = false; DragCurrentAccum = 0.0f; DragSpeedDefaultRatio = 1.0f / 100.0f; ScrollbarClickDeltaToGrabCenter = ImVec2(0.0f, 0.0f); TooltipOverrideCount = 0; MultiSelectScopeId = 0; PlatformImePos = PlatformImeLastPos = ImVec2(FLT_MAX, FLT_MAX); PlatformImePosViewport = 0; DockContext = NULL; SettingsLoaded = false; SettingsDirtyTimer = 0.0f; LogEnabled = false; LogType = ImGuiLogType_None; LogFile = NULL; LogLinePosY = FLT_MAX; LogLineFirstItem = false; LogDepthRef = 0; LogDepthToExpand = LogDepthToExpandDefault = 2; memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame)); FramerateSecPerFrameIdx = 0; FramerateSecPerFrameAccum = 0.0f; WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1; memset(TempBuffer, 0, sizeof(TempBuffer)); } }; //----------------------------------------------------------------------------- // ImGuiWindow //----------------------------------------------------------------------------- // Transient per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the DC variable name in ImGuiWindow. // FIXME: That's theory, in practice the delimitation between ImGuiWindow and ImGuiWindowTempData is quite tenuous and could be reconsidered. struct IMGUI_API ImGuiWindowTempData { ImVec2 CursorPos; ImVec2 CursorPosPrevLine; ImVec2 CursorStartPos; // Initial position in client area with padding 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 ImVec2 CurrentLineSize; float CurrentLineTextBaseOffset; ImVec2 PrevLineSize; float PrevLineTextBaseOffset; int TreeDepth; ImU32 TreeStoreMayJumpToParentOnPop; // Store a copy of !g.NavIdIsAlive for TreeDepth 0..31.. Could be turned into a ImU64 if necessary. ImGuiID LastItemId; ImGuiItemStatusFlags LastItemStatusFlags; ImRect LastItemRect; // Interaction rect ImRect LastItemDisplayRect; // End-user display rect (only valid if LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect) ImGuiNavLayer NavLayerCurrent; // Current layer, 0..31 (we currently only use 0..1) int NavLayerCurrentMask; // = (1 << NavLayerCurrent) used by ItemAdd prior to clipping. int NavLayerActiveMask; // Which layer have been written to (result from previous frame) int NavLayerActiveMaskNext; // Which layer have been written to (buffer for current frame) bool NavHideHighlightOneFrame; bool NavHasScroll; // Set when scrolling can be used (ScrollMax > 0.0f) bool MenuBarAppending; // FIXME: Remove this 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. ImVector ChildWindows; ImGuiStorage* StateStorage; ImGuiLayoutType LayoutType; ImGuiLayoutType ParentLayoutType; // Layout type of parent window at the time of Begin() int FocusCounterAll; // Counter for focus/tabbing system. Start at -1 and increase as assigned via FocusableItemRegister() (FIXME-NAV: Needs redesign) int FocusCounterTab; // (same, but only count widgets which you can Tab through) // 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. ImGuiItemFlags ItemFlags; // == ItemFlagsStack.back() [empty == ImGuiItemFlags_Default] float ItemWidth; // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window float NextItemWidth; float TextWrapPos; // == TextWrapPosStack.back() [empty == -1.0f] ImVectorItemFlagsStack; ImVector ItemWidthStack; ImVector TextWrapPosStack; ImVectorGroupStack; short StackSizesBackup[6]; // Store size of various stacks for asserting ImVec1 Indent; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.) ImVec1 GroupOffset; 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. ImGuiColumns* CurrentColumns; // Current columns set ImGuiWindowTempData() { CursorPos = CursorPosPrevLine = CursorStartPos = CursorMaxPos = ImVec2(0.0f, 0.0f); CurrentLineSize = PrevLineSize = ImVec2(0.0f, 0.0f); CurrentLineTextBaseOffset = PrevLineTextBaseOffset = 0.0f; TreeDepth = 0; TreeStoreMayJumpToParentOnPop = 0x00; LastItemId = 0; LastItemStatusFlags = 0; LastItemRect = LastItemDisplayRect = ImRect(); NavLayerActiveMask = NavLayerActiveMaskNext = 0x00; NavLayerCurrent = ImGuiNavLayer_Main; NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); NavHideHighlightOneFrame = false; NavHasScroll = false; MenuBarAppending = false; MenuBarOffset = ImVec2(0.0f, 0.0f); StateStorage = NULL; LayoutType = ParentLayoutType = ImGuiLayoutType_Vertical; FocusCounterAll = FocusCounterTab = -1; ItemFlags = ImGuiItemFlags_Default_; ItemWidth = 0.0f; NextItemWidth = +FLT_MAX; TextWrapPos = -1.0f; memset(StackSizesBackup, 0, sizeof(StackSizesBackup)); Indent = ImVec1(0.0f); GroupOffset = ImVec1(0.0f); ColumnsOffset = ImVec1(0.0f); CurrentColumns = NULL; } }; // Storage for one window struct IMGUI_API ImGuiWindow { char* Name; ImGuiID ID; // == ImHashStr(Name) ImGuiWindowFlags Flags, FlagsPreviousFrame; // See enum ImGuiWindowFlags_ ImGuiWindowClass WindowClass; // Advanced users only. Set with SetNextWindowClass() ImGuiViewportP* Viewport; // Always set in Begin(), only inactive windows may have a NULL value here ImGuiID ViewportId; // We backup the viewport id (since the viewport may disappear or never be created if the window is inactive) ImVec2 ViewportPos; // We backup the viewport position (since the viewport may disappear or never be created if the window is inactive) 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 ImVec2 Pos; // Position (always rounded-up to nearest pixel) ImVec2 Size; // Current size (==SizeFull or collapsed title bar size) ImVec2 SizeFull; // Size when non collapsed 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. ImVec2 SizeContents; // Size of contents (== extents reach of the drawing cursor) from previous frame. Include decoration, window title, border, menu, etc. ImVec2 SizeContentsExplicit; // Size of contents explicitly set by the user via SetNextWindowContentSize() ImVec2 WindowPadding; // Window padding at the time of begin. float WindowRounding; // Window rounding at the time of begin. float WindowBorderSize; // Window border size at the time of begin. int NameBufLen; // Size of buffer storing Name. May be larger than strlen(Name)! ImGuiID MoveId; // == window->GetID("#MOVE") ImGuiID ChildId; // ID of corresponding item in parent window (for navigation to return from child window to parent window) ImVec2 Scroll; 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) ImVec2 ScrollTargetCenterRatio; // 0.0f = scroll so that target position is at top, 0.5f = scroll so that target position is centered ImVec2 ScrollbarSizes; // Size taken by scrollbars on each axis bool ScrollbarX, ScrollbarY; bool ViewportOwned; bool Active; // Set to true on Begin(), unless Collapsed bool WasActive; bool WriteAccessed; // Set to true when any widget access the current window bool Collapsed; // Set when collapsing window to become only title-bar bool WantCollapseToggle; bool SkipItems; // Set when items can safely be all clipped (e.g. window not visible or collapsed) bool Appearing; // Set during the frame where the window is appearing (or re-appearing) bool Hidden; // Do not display (== (HiddenFrames*** > 0)) bool HasCloseButton; // Set when the window has a close button (p_open != NULL) signed char ResizeBorderHeld; // Current border being held for resize (-1: none, otherwise 0-3) short BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs) short BeginOrderWithinParent; // Order within immediate parent window, if we are a child window. Otherwise 0. short BeginOrderWithinContext; // Order within entire imgui context. This is mostly used for debugging submission order related issues. ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling) int AutoFitFramesX, AutoFitFramesY; bool AutoFitOnlyGrows; int AutoFitChildAxises; ImGuiDir AutoPosLastDirection; int HiddenFramesCanSkipItems; // Hide the window for N frames int HiddenFramesCannotSkipItems; // Hide the window for N frames while allowing items to be submitted so we can measure their size ImGuiCond SetWindowPosAllowFlags; // store acceptable condition flags for SetNextWindowPos() use. ImGuiCond SetWindowSizeAllowFlags; // store acceptable condition flags for SetNextWindowSize() use. ImGuiCond SetWindowCollapsedAllowFlags; // store acceptable condition flags for SetNextWindowCollapsed() use. ImGuiCond SetWindowDockAllowFlags; // store acceptable condition flags for SetNextWindowDock() use. ImVec2 SetWindowPosVal; // store window position when using a non-zero Pivot (position set needs to be processed when we know the window size) 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. ImGuiWindowTempData DC; // Temporary per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the "DC" variable name. ImVector IDStack; // ID stack. ID are hashes seeded with the value at the top of the stack ImRect ClipRect; // Current clipping rectangle. = DrawList->clip_rect_stack.back(). Scissoring / clipping rectangle. x1, y1, x2, y2. ImRect OuterRectClipped; // = WindowRect just after setup in Begin(). == window->Rect() for root window. ImRect InnerMainRect, InnerClipRect; ImVec2ih HitTestHoleSize, HitTestHoleOffset; ImRect ContentsRegionRect; // FIXME: This is currently confusing/misleading. Maximum visible content position ~~ Pos + (SizeContentsExplicit ? SizeContentsExplicit : Size - ScrollbarSizes) - CursorStartPos, per axis int LastFrameActive; // Last frame number the window was Active. int LastFrameJustFocused; // Last frame number the window was made Focused. float ItemWidthDefault; ImGuiMenuColumns MenuColumns; // Simplified columns storage for menu items ImGuiStorage StateStorage; ImVector ColumnsStorage; float FontWindowScale; // User scale multiplier per-window float FontDpiScale; int SettingsIdx; // Index into SettingsWindow[] (indices are always valid as we only grow the array from the back) ImDrawList* DrawList; // == &DrawListInst (for backward compatibility reason with code using imgui_internal.h we keep this a pointer) ImDrawList DrawListInst; ImGuiWindow* ParentWindow; // If we are a child _or_ popup window, this is pointing to our parent. Otherwise NULL. ImGuiWindow* RootWindow; // Point to ourself or first ancestor that is not a child window. 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. ImGuiWindow* RootWindowForTitleBarHighlight; // Point to ourself or first ancestor which will display TitleBgActive color when this window is active. ImGuiWindow* RootWindowForNav; // Point to ourself or first ancestor which doesn't have the NavFlattened flag. 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.) ImGuiID NavLastIds[ImGuiNavLayer_COUNT]; // Last known NavId for this window, per layer (0/1) ImRect NavRectRel[ImGuiNavLayer_COUNT]; // Reference rectangle, in window relative space // Docking ImGuiDockNode* DockNode; // Which node are we docked into ImGuiDockNode* DockNodeAsHost; // Which node are we owning (for parent windows) ImGuiID DockId; // Backup of last valid DockNode->Id, so single value remember their dock node id ImGuiItemStatusFlags DockTabItemStatusFlags; ImRect DockTabItemRect; 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. bool DockIsActive :1; // =~ (DockNode != NULL) && (DockNode->Windows.Size > 1) bool DockTabIsVisible :1; // Is the window visible this frame? =~ is the corresponding tab selected? bool DockTabWantClose :1; public: ImGuiWindow(ImGuiContext* context, const char* name); ~ImGuiWindow(); ImGuiID GetID(const char* str, const char* str_end = NULL); ImGuiID GetID(const void* ptr); ImGuiID GetIDNoKeepAlive(const char* str, const char* str_end = NULL); ImGuiID GetIDNoKeepAlive(const void* ptr); ImGuiID GetIDFromRectangle(const ImRect& r_abs); // We don't use g.FontSize because the window may be != g.CurrentWidow. ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x+Size.x, Pos.y+Size.y); } float CalcFontSize() const { return GImGui->FontBaseSize * FontWindowScale * FontDpiScale; } float TitleBarHeight() const { return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f; } ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); } float MenuBarHeight() const { return (Flags & ImGuiWindowFlags_MenuBar) ? DC.MenuBarOffset.y + CalcFontSize() + GImGui->Style.FramePadding.y * 2.0f : 0.0f; } ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); } }; // 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. struct ImGuiItemHoveredDataBackup { ImGuiID LastItemId; ImGuiItemStatusFlags LastItemStatusFlags; ImRect LastItemRect; ImRect LastItemDisplayRect; ImGuiItemHoveredDataBackup() { Backup(); } void Backup() { ImGuiWindow* window = GImGui->CurrentWindow; LastItemId = window->DC.LastItemId; LastItemStatusFlags = window->DC.LastItemStatusFlags; LastItemRect = window->DC.LastItemRect; LastItemDisplayRect = window->DC.LastItemDisplayRect; } void Restore() const { ImGuiWindow* window = GImGui->CurrentWindow; window->DC.LastItemId = LastItemId; window->DC.LastItemStatusFlags = LastItemStatusFlags; window->DC.LastItemRect = LastItemRect; window->DC.LastItemDisplayRect = LastItemDisplayRect; } }; //----------------------------------------------------------------------------- // Tab bar, tab item //----------------------------------------------------------------------------- enum ImGuiTabBarFlagsPrivate_ { 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] ImGuiTabBarFlags_IsFocused = 1 << 21, 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 }; enum ImGuiTabItemFlagsPrivate_ { ImGuiTabItemFlags_NoCloseButton = 1 << 20, // Store whether p_open is set or not, which we need to recompute WidthContents during layout. ImGuiTabItemFlags_Unsorted = 1 << 21, // [Docking] Trailing tabs with the _Unsorted flag will be sorted based on the DockOrder of their Window. ImGuiTabItemFlags_Preview = 1 << 22 // [Docking] Display tab shape for docking preview (height is adjusted slightly to compensate for the yet missing tab bar) }; // Storage for one active tab item (sizeof() 32~40 bytes) struct ImGuiTabItem { ImGuiID ID; ImGuiTabItemFlags Flags; ImGuiWindow* Window; // When TabItem is part of a DockNode's TabBar, we hold on to a window. int LastFrameVisible; int LastFrameSelected; // This allows us to infer an ordered list of the last activated tabs with little maintenance int NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames float Offset; // Position relative to beginning of tab float Width; // Width currently displayed float WidthContents; // Width of actual contents, stored during BeginTabItem() call ImGuiTabItem() { ID = Flags = 0; Window = NULL; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = WidthContents = 0.0f; } }; // Storage for a tab bar (sizeof() 92~96 bytes) struct ImGuiTabBar { ImVector Tabs; ImGuiID ID; // Zero for tab-bars used by docking ImGuiID SelectedTabId; // Selected tab ImGuiID NextSelectedTabId; ImGuiID VisibleTabId; // Can occasionally be != SelectedTabId (e.g. when previewing contents for CTRL+TAB preview) int CurrFrameVisible; int PrevFrameVisible; ImRect BarRect; float ContentsHeight; float OffsetMax; // Distance from BarRect.Min.x, locked during layout float OffsetNextTab; // Distance from BarRect.Min.x, incremented with each BeginTabItem() call, not used if ImGuiTabBarFlags_Reorderable if set. float ScrollingAnim; float ScrollingTarget; float ScrollingTargetDistToVisibility; float ScrollingSpeed; ImGuiTabBarFlags Flags; ImGuiID ReorderRequestTabId; int ReorderRequestDir; bool WantLayout; bool VisibleTabWasSubmitted; short LastTabItemIdx; // For BeginTabItem()/EndTabItem() ImVec2 FramePadding; // style.FramePadding locked at the time of BeginTabBar() ImGuiTextBuffer TabsNames; // For non-docking tab bar we re-append names in a contiguous buffer. ImGuiTabBar(); int GetTabOrder(const ImGuiTabItem* tab) const { return Tabs.index_from_ptr(tab); } const char* GetTabName(const ImGuiTabItem* tab) const { if (tab->Window) return tab->Window->Name; IM_ASSERT(tab->NameOffset != -1 && tab->NameOffset < TabsNames.Buf.Size); return TabsNames.Buf.Data + tab->NameOffset; } }; //----------------------------------------------------------------------------- // Internal API // No guarantee of forward compatibility here. //----------------------------------------------------------------------------- namespace ImGui { // We should always have a CurrentWindow in the stack (there is an implicit "Debug" window) // If this ever crash because g.CurrentWindow is NULL it means that either // - ImGui::NewFrame() has never been called, which is illegal. // - You are calling ImGui functions after ImGui::EndFrame()/ImGui::Render() and before the next ImGui::NewFrame(), which is also illegal. inline ImGuiWindow* GetCurrentWindowRead() { ImGuiContext& g = *GImGui; return g.CurrentWindow; } inline ImGuiWindow* GetCurrentWindow() { ImGuiContext& g = *GImGui; g.CurrentWindow->WriteAccessed = true; return g.CurrentWindow; } IMGUI_API ImGuiWindow* FindWindowByID(ImGuiID id); IMGUI_API ImGuiWindow* FindWindowByName(const char* name); IMGUI_API void FocusWindow(ImGuiWindow* window); IMGUI_API void FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window); IMGUI_API void BringWindowToFocusFront(ImGuiWindow* window); IMGUI_API void BringWindowToDisplayFront(ImGuiWindow* window); IMGUI_API void BringWindowToDisplayBack(ImGuiWindow* window); IMGUI_API void UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window); IMGUI_API ImVec2 CalcWindowExpectedSize(ImGuiWindow* window); IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent); IMGUI_API bool IsWindowNavFocusable(ImGuiWindow* window); IMGUI_API void SetWindowScrollX(ImGuiWindow* window, float new_scroll_x); IMGUI_API void SetWindowScrollY(ImGuiWindow* window, float new_scroll_y); IMGUI_API float GetWindowScrollMaxX(ImGuiWindow* window); IMGUI_API float GetWindowScrollMaxY(ImGuiWindow* window); IMGUI_API ImRect GetWindowAllowedExtentRect(ImGuiWindow* window); IMGUI_API void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond); IMGUI_API void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond); IMGUI_API void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond); IMGUI_API void SetCurrentFont(ImFont* font); inline ImFont* GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; } inline ImDrawList* GetForegroundDrawList(ImGuiWindow* window) { return GetForegroundDrawList(window->Viewport); } // Init IMGUI_API void Initialize(ImGuiContext* context); 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(). // NewFrame IMGUI_API void UpdateHoveredWindowAndCaptureFlags(); IMGUI_API void StartMouseMovingWindow(ImGuiWindow* window); IMGUI_API void UpdateMouseMovingWindowNewFrame(); IMGUI_API void UpdateMouseMovingWindowEndFrame(); // Viewports IMGUI_API void ScaleWindowsInViewport(ImGuiViewportP* viewport, float scale); IMGUI_API void DestroyPlatformWindow(ImGuiViewportP* viewport); IMGUI_API void ShowViewportThumbnails(); // Settings IMGUI_API void MarkIniSettingsDirty(); IMGUI_API void MarkIniSettingsDirty(ImGuiWindow* window); IMGUI_API ImGuiWindowSettings* CreateNewWindowSettings(const char* name); IMGUI_API ImGuiWindowSettings* FindWindowSettings(ImGuiID id); IMGUI_API ImGuiWindowSettings* FindOrCreateWindowSettings(const char* name); IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name); // Basic Accessors inline ImGuiID GetItemID() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemId; } inline ImGuiID GetActiveID() { ImGuiContext& g = *GImGui; return g.ActiveId; } inline ImGuiID GetFocusID() { ImGuiContext& g = *GImGui; return g.NavId; } IMGUI_API void SetActiveID(ImGuiID id, ImGuiWindow* window); IMGUI_API void SetFocusID(ImGuiID id, ImGuiWindow* window); IMGUI_API void ClearActiveID(); IMGUI_API ImGuiID GetHoveredID(); IMGUI_API void SetHoveredID(ImGuiID id); IMGUI_API void KeepAliveID(ImGuiID id); IMGUI_API void MarkItemEdited(ImGuiID id); IMGUI_API void PushOverrideID(ImGuiID id); // Basic Helpers for widget code IMGUI_API void ItemSize(const ImVec2& size, float text_offset_y = 0.0f); IMGUI_API void ItemSize(const ImRect& bb, float text_offset_y = 0.0f); IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL); IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id); IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged); IMGUI_API bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id); // Return true if focus is requested IMGUI_API void FocusableItemUnregister(ImGuiWindow* window); IMGUI_API float GetNextItemWidth(); IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_w, float default_h); IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x); IMGUI_API void PushMultiItemsWidths(int components, float width_full); IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); IMGUI_API void PopItemFlag(); 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) IMGUI_API ImVec2 GetContentRegionMaxScreen(); // Logging/Capture 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. IMGUI_API void LogToBuffer(int auto_open_depth = -1); // Start logging/capturing to internal buffer // Popups, Modals, Tooltips IMGUI_API void OpenPopupEx(ImGuiID id); IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup); IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup); 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! IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, bool override_previous_tooltip = true); IMGUI_API ImGuiWindow* GetFrontMostPopupModal(); IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); 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); // Navigation IMGUI_API void NavInitWindow(ImGuiWindow* window, bool force_reinit); IMGUI_API bool NavMoveRequestButNoResultYet(); IMGUI_API void NavMoveRequestCancel(); IMGUI_API void NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const ImRect& bb_rel, ImGuiNavMoveFlags move_flags); IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); IMGUI_API float GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode); IMGUI_API ImVec2 GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor = 0.0f, float fast_factor = 0.0f); IMGUI_API int CalcTypematicPressedRepeatAmount(float t, float t_prev, float repeat_delay, float repeat_rate); 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. IMGUI_API void SetNavID(ImGuiID id, int nav_layer); IMGUI_API void SetNavIDWithRectRel(ImGuiID id, int nav_layer, const ImRect& rect_rel); // Inputs 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; } inline bool IsNavInputDown(ImGuiNavInput n) { return GImGui->IO.NavInputs[n] > 0.0f; } inline bool IsNavInputPressed(ImGuiNavInput n, ImGuiInputReadMode mode) { return GetNavInputAmount(n, mode) > 0.0f; } inline bool IsNavInputPressedAnyOfTwo(ImGuiNavInput n1, ImGuiNavInput n2, ImGuiInputReadMode mode) { return (GetNavInputAmount(n1, mode) + GetNavInputAmount(n2, mode)) > 0.0f; } // Docking // (some functions are only declared in imgui.cpp, see Docking section) IMGUI_API void DockContextInitialize(ImGuiContext* ctx); IMGUI_API void DockContextShutdown(ImGuiContext* ctx); IMGUI_API void DockContextOnLoadSettings(ImGuiContext* ctx); IMGUI_API void DockContextRebuild(ImGuiContext* ctx); IMGUI_API void DockContextUpdateUndocking(ImGuiContext* ctx); IMGUI_API void DockContextUpdateDocking(ImGuiContext* ctx); IMGUI_API void DockContextQueueDock(ImGuiContext* ctx, ImGuiWindow* target, ImGuiDockNode* target_node, ImGuiWindow* payload, ImGuiDir split_dir, float split_ratio, bool split_outer); IMGUI_API void DockContextQueueUndockWindow(ImGuiContext* ctx, ImGuiWindow* window); IMGUI_API void DockContextQueueUndockNode(ImGuiContext* ctx, ImGuiDockNode* node); IMGUI_API bool DockContextCalcDropPosForDocking(ImGuiWindow* target, ImGuiDockNode* target_node, ImGuiWindow* payload, ImGuiDir split_dir, bool split_outer, ImVec2* out_pos); inline ImGuiDockNode* DockNodeGetRootNode(ImGuiDockNode* node) { while (node->ParentNode) node = node->ParentNode; return node; } IMGUI_API void BeginDocked(ImGuiWindow* window, bool* p_open); IMGUI_API void BeginAsDockableDragDropSource(ImGuiWindow* window); IMGUI_API void BeginAsDockableDragDropTarget(ImGuiWindow* window); IMGUI_API void SetWindowDock(ImGuiWindow* window, ImGuiID dock_id, ImGuiCond cond); IMGUI_API void ShowDockingDebug(); // Docking - Builder function needs to be generally called before the DockSpace() node is submitted. IMGUI_API void DockBuilderDockWindow(const char* window_name, ImGuiID node_id); IMGUI_API ImGuiDockNode*DockBuilderGetNode(ImGuiID node_id); // Warning: DO NOT HOLD ON ImGuiDockNode* pointer, will be invalided by any split/merge/remove operation. inline ImGuiDockNode* DockBuilderGetCentralNode(ImGuiID node_id) { ImGuiDockNode* node = DockBuilderGetNode(node_id); if (!node) return NULL; return DockNodeGetRootNode(node)->CentralNode; } 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. IMGUI_API void DockBuilderRemoveNode(ImGuiID node_id); // Remove node and all its child, undock all windows IMGUI_API void DockBuilderRemoveNodeDockedWindows(ImGuiID node_id, bool clear_persistent_docking_references = true); IMGUI_API void DockBuilderRemoveNodeChildNodes(ImGuiID node_id); // Remove all split/hierarchy. All remaining docked windows will be re-docked to the root. IMGUI_API void DockBuilderSetNodePos(ImGuiID node_id, ImVec2 pos); IMGUI_API void DockBuilderSetNodeSize(ImGuiID node_id, ImVec2 size); 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); IMGUI_API void DockBuilderCopyDockSpace(ImGuiID src_dockspace_id, ImGuiID dst_dockspace_id, ImVector* in_window_remap_pairs); IMGUI_API void DockBuilderCopyNode(ImGuiID src_node_id, ImGuiID dst_node_id, ImVector* out_node_remap_pairs); IMGUI_API void DockBuilderCopyWindowSettings(const char* src_name, const char* dst_name); IMGUI_API void DockBuilderFinish(ImGuiID node_id); // Drag and Drop IMGUI_API bool BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id); IMGUI_API void ClearDragDrop(); IMGUI_API bool IsDragDropPayloadBeingAccepted(); // New Columns API (FIXME-WIP) 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(). IMGUI_API void EndColumns(); // close columns IMGUI_API void PushColumnClipRect(int column_index = -1); IMGUI_API ImGuiID GetColumnsID(const char* str_id, int count); IMGUI_API ImGuiColumns* FindOrCreateColumns(ImGuiWindow* window, ImGuiID id); // Tab Bars IMGUI_API bool BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& bb, ImGuiTabBarFlags flags, ImGuiDockNode* dock_node); IMGUI_API ImGuiTabItem* TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id); IMGUI_API ImGuiTabItem* TabBarFindMostRecentlySelectedTabForActiveWindow(ImGuiTabBar* tab_bar); IMGUI_API void TabBarAddTab(ImGuiTabBar* tab_bar, ImGuiTabItemFlags tab_flags, ImGuiWindow* window); IMGUI_API void TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id); IMGUI_API void TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); IMGUI_API void TabBarQueueChangeTabOrder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir); IMGUI_API bool TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags, ImGuiWindow* docked_window); IMGUI_API ImVec2 TabItemCalcSize(const char* label, bool has_close_button); IMGUI_API void TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col); 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); // Render helpers // 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. // NB: All position are in absolute pixels coordinates (we are never using window coordinates internally) IMGUI_API void RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, bool hide_text_after_hash = true); IMGUI_API void RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width); 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); 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); IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f); IMGUI_API void RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f); 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); IMGUI_API void RenderArrow(ImVec2 pos, ImGuiDir dir, float scale = 1.0f); IMGUI_API void RenderBullet(ImVec2 pos); IMGUI_API void RenderCheckMark(ImVec2 pos, ImU32 col, float sz); IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight IMGUI_API void RenderMouseCursor(ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor = ImGuiMouseCursor_Arrow); IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text. IMGUI_API void LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end = NULL); // Render helpers (those functions don't access any ImGui state!) IMGUI_API void RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col); IMGUI_API void RenderArrowDockMenu(ImDrawList* draw_list, ImVec2 p_min, float sz, ImU32 col); IMGUI_API void RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding); IMGUI_API void RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect inner, ImU32 col, float rounding); IMGUI_API void RenderPixelEllipsis(ImDrawList* draw_list, ImVec2 pos, int count, ImU32 col); // Widgets IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0); IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0,0), ImGuiButtonFlags flags = 0); IMGUI_API bool CloseButton(ImGuiID id, const ImVec2& pos, float radius); IMGUI_API bool CollapseButton(ImGuiID id, const ImVec2& pos, ImGuiDockNode* dock_node); IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags); IMGUI_API void Scrollbar(ImGuiAxis axis); IMGUI_API ImGuiID GetScrollbarID(ImGuiWindow* window, ImGuiAxis axis); 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. // Widgets low-level behaviors IMGUI_API bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags = 0); 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); 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); 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); IMGUI_API bool TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end = NULL); IMGUI_API bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0); // Consume previous SetNextTreeNodeOpened() data, if any. May return true when logging IMGUI_API void TreePushOverrideID(ImGuiID id); // Template functions are instantiated in imgui_widgets.cpp for a finite number of types. // 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). // e.g. " extern template IMGUI_API float RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, float v); " template 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); template 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); template IMGUI_API float SliderCalcRatioFromValueT(ImGuiDataType data_type, T v, T v_min, T v_max, float power, float linear_zero_pos); template IMGUI_API T RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, T v); // Data type helpers IMGUI_API const ImGuiDataTypeInfo* DataTypeGetInfo(ImGuiDataType data_type); IMGUI_API int DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* data_ptr, const char* format); IMGUI_API void DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg_1, const void* arg_2); IMGUI_API bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* data_ptr, const char* format); // InputText 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); IMGUI_API bool TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* data_ptr, const char* format); inline bool TempInputTextIsActive(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.ActiveId == id && g.TempInputTextId == id); } // Color IMGUI_API void ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags); IMGUI_API void ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags); IMGUI_API void ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags); // Plot 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); // Shade functions (write over already created vertices) 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); 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); } // namespace ImGui // ImFontAtlas internals IMGUI_API bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildRegisterDefaultCustomRects(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent); IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque); IMGUI_API void ImFontAtlasBuildFinish(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor); IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride); // Test engine hooks (imgui-test) //#define IMGUI_ENABLE_TEST_ENGINE #ifdef IMGUI_ENABLE_TEST_ENGINE extern void ImGuiTestEngineHook_PreNewFrame(ImGuiContext* ctx); extern void ImGuiTestEngineHook_PostNewFrame(ImGuiContext* ctx); extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags); #define IMGUI_TEST_ENGINE_ITEM_ADD(_BB, _ID) ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID) // Register status flags #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register status flags #else #define IMGUI_TEST_ENGINE_ITEM_ADD(_BB, _ID) do { } while (0) #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS) do { } while (0) #endif #ifdef __clang__ #pragma clang diagnostic pop #endif #ifdef _MSC_VER #pragma warning (pop) #endif ================================================ FILE: src/imgui/imgui_widgets.cpp ================================================ // dear imgui, v1.70 WIP // (widgets code) /* Index of this file: // [SECTION] Forward Declarations // [SECTION] Widgets: Text, etc. // [SECTION] Widgets: Main (Button, Image, Checkbox, RadioButton, ProgressBar, Bullet, etc.) // [SECTION] Widgets: Low-level Layout helpers (Spacing, Dummy, NewLine, Separator, etc.) // [SECTION] Widgets: ComboBox // [SECTION] Data Type and Data Formatting Helpers // [SECTION] Widgets: DragScalar, DragFloat, DragInt, etc. // [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc. // [SECTION] Widgets: InputScalar, InputFloat, InputInt, etc. // [SECTION] Widgets: InputText, InputTextMultiline // [SECTION] Widgets: ColorEdit, ColorPicker, ColorButton, etc. // [SECTION] Widgets: TreeNode, CollapsingHeader, etc. // [SECTION] Widgets: Selectable // [SECTION] Widgets: ListBox // [SECTION] Widgets: PlotLines, PlotHistogram // [SECTION] Widgets: Value helpers // [SECTION] Widgets: MenuItem, BeginMenu, EndMenu, etc. // [SECTION] Widgets: BeginTabBar, EndTabBar, etc. // [SECTION] Widgets: BeginTabItem, EndTabItem, etc. */ #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS #endif #include "imgui.hpp" #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif #include "imgui_internal.hpp" #include // toupper #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier #include // intptr_t #else #include // intptr_t #endif // Visual Studio warnings #ifdef _MSC_VER #pragma warning (disable: 4127) // condition expression is constant #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen #endif // Clang/GCC warnings with -Weverything #ifdef __clang__ #pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. #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. #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. #pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // #if __has_warning("-Wzero-as-null-pointer-constant") #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 #endif #if __has_warning("-Wdouble-promotion") #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. #endif #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked #if __GNUC__ >= 8 #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 #endif #endif //------------------------------------------------------------------------- // Data //------------------------------------------------------------------------- // Those MIN/MAX values are not define because we need to point to them static const signed char IM_S8_MIN = -128; static const signed char IM_S8_MAX = 127; static const unsigned char IM_U8_MIN = 0; static const unsigned char IM_U8_MAX = 0xFF; static const signed short IM_S16_MIN = -32768; static const signed short IM_S16_MAX = 32767; static const unsigned short IM_U16_MIN = 0; static const unsigned short IM_U16_MAX = 0xFFFF; static const ImS32 IM_S32_MIN = INT_MIN; // (-2147483647 - 1), (0x80000000); static const ImS32 IM_S32_MAX = INT_MAX; // (2147483647), (0x7FFFFFFF) static const ImU32 IM_U32_MIN = 0; static const ImU32 IM_U32_MAX = UINT_MAX; // (0xFFFFFFFF) #ifdef LLONG_MIN static const ImS64 IM_S64_MIN = LLONG_MIN; // (-9223372036854775807ll - 1ll); static const ImS64 IM_S64_MAX = LLONG_MAX; // (9223372036854775807ll); #else static const ImS64 IM_S64_MIN = -9223372036854775807LL - 1; static const ImS64 IM_S64_MAX = 9223372036854775807LL; #endif static const ImU64 IM_U64_MIN = 0; #ifdef ULLONG_MAX static const ImU64 IM_U64_MAX = ULLONG_MAX; // (0xFFFFFFFFFFFFFFFFull); #else static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1); #endif //------------------------------------------------------------------------- // [SECTION] Forward Declarations //------------------------------------------------------------------------- // For InputTextEx() static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data); static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); //------------------------------------------------------------------------- // [SECTION] Widgets: Text, etc. //------------------------------------------------------------------------- // - TextUnformatted() // - Text() // - TextV() // - TextColored() // - TextColoredV() // - TextDisabled() // - TextDisabledV() // - TextWrapped() // - TextWrappedV() // - LabelText() // - LabelTextV() // - BulletText() // - BulletTextV() //------------------------------------------------------------------------- void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; IM_ASSERT(text != NULL); const char* text_begin = text; if (text_end == NULL) text_end = text + strlen(text); // FIXME-OPT const ImVec2 text_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrentLineTextBaseOffset); const float wrap_pos_x = window->DC.TextWrapPos; const bool wrap_enabled = (wrap_pos_x >= 0.0f); if (text_end - text > 2000 && !wrap_enabled) { // Long text! // Perform manual coarse clipping to optimize for long multi-line text // - From this point we will only compute the width of lines that are visible. Optimization only available when word-wrapping is disabled. // - 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. // - We use memchr(), pay attention that well optimized versions of those str/mem functions are much faster than a casually written loop. const char* line = text; const float line_height = GetTextLineHeight(); ImVec2 text_size(0,0); // Lines to skip (can't skip when logging text) ImVec2 pos = text_pos; if (!g.LogEnabled) { int lines_skippable = (int)((window->ClipRect.Min.y - text_pos.y) / line_height); if (lines_skippable > 0) { int lines_skipped = 0; while (line < text_end && lines_skipped < lines_skippable) { const char* line_end = (const char*)memchr(line, '\n', text_end - line); if (!line_end) line_end = text_end; if ((flags & ImGuiTextFlags_NoWidthForLargeClippedText) == 0) text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x); line = line_end + 1; lines_skipped++; } pos.y += lines_skipped * line_height; } } // Lines to render if (line < text_end) { ImRect line_rect(pos, pos + ImVec2(FLT_MAX, line_height)); while (line < text_end) { if (IsClippedEx(line_rect, 0, false)) break; const char* line_end = (const char*)memchr(line, '\n', text_end - line); if (!line_end) line_end = text_end; text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x); RenderText(pos, line, line_end, false); line = line_end + 1; line_rect.Min.y += line_height; line_rect.Max.y += line_height; pos.y += line_height; } // Count remaining lines int lines_skipped = 0; while (line < text_end) { const char* line_end = (const char*)memchr(line, '\n', text_end - line); if (!line_end) line_end = text_end; if ((flags & ImGuiTextFlags_NoWidthForLargeClippedText) == 0) text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x); line = line_end + 1; lines_skipped++; } pos.y += lines_skipped * line_height; } text_size.y = (pos - text_pos).y; ImRect bb(text_pos, text_pos + text_size); ItemSize(text_size); ItemAdd(bb, 0); } else { const float wrap_width = wrap_enabled ? CalcWrapWidthForPos(window->DC.CursorPos, wrap_pos_x) : 0.0f; const ImVec2 text_size = CalcTextSize(text_begin, text_end, false, wrap_width); ImRect bb(text_pos, text_pos + text_size); ItemSize(text_size); if (!ItemAdd(bb, 0)) return; // Render (we don't hide text after ## in this end-user function) RenderTextWrapped(bb.Min, text_begin, text_end, wrap_width); } } void ImGui::TextUnformatted(const char* text, const char* text_end) { TextEx(text, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); } void ImGui::Text(const char* fmt, ...) { va_list args; va_start(args, fmt); TextV(fmt, args); va_end(args); } void ImGui::TextV(const char* fmt, va_list args) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; const char* text_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); TextEx(g.TempBuffer, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); } void ImGui::TextColored(const ImVec4& col, const char* fmt, ...) { va_list args; va_start(args, fmt); TextColoredV(col, fmt, args); va_end(args); } void ImGui::TextColoredV(const ImVec4& col, const char* fmt, va_list args) { PushStyleColor(ImGuiCol_Text, col); TextV(fmt, args); PopStyleColor(); } void ImGui::TextDisabled(const char* fmt, ...) { va_list args; va_start(args, fmt); TextDisabledV(fmt, args); va_end(args); } void ImGui::TextDisabledV(const char* fmt, va_list args) { PushStyleColor(ImGuiCol_Text, GImGui->Style.Colors[ImGuiCol_TextDisabled]); TextV(fmt, args); PopStyleColor(); } void ImGui::TextWrapped(const char* fmt, ...) { va_list args; va_start(args, fmt); TextWrappedV(fmt, args); va_end(args); } void ImGui::TextWrappedV(const char* fmt, va_list args) { ImGuiWindow* window = GetCurrentWindow(); bool need_backup = (window->DC.TextWrapPos < 0.0f); // Keep existing wrap position if one is already set if (need_backup) PushTextWrapPos(0.0f); TextV(fmt, args); if (need_backup) PopTextWrapPos(); } void ImGui::LabelText(const char* label, const char* fmt, ...) { va_list args; va_start(args, fmt); LabelTextV(label, fmt, args); va_end(args); } // Add a label+text combo aligned to other label+value widgets void ImGui::LabelTextV(const char* label, const char* fmt, va_list args) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const float w = GetNextItemWidth(); const ImVec2 label_size = CalcTextSize(label, NULL, true); const ImRect value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2)); 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); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, 0)) return; // Render const char* value_text_begin = &g.TempBuffer[0]; const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); RenderTextClipped(value_bb.Min, value_bb.Max, value_text_begin, value_text_end, NULL, ImVec2(0.0f,0.5f)); if (label_size.x > 0.0f) RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y + style.FramePadding.y), label); } void ImGui::BulletText(const char* fmt, ...) { va_list args; va_start(args, fmt); BulletTextV(fmt, args); va_end(args); } // Text with a little bullet aligned to the typical tree node. void ImGui::BulletTextV(const char* fmt, va_list args) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const char* text_begin = g.TempBuffer; const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); const ImVec2 label_size = CalcTextSize(text_begin, text_end, false); const float text_base_offset_y = ImMax(0.0f, window->DC.CurrentLineTextBaseOffset); // Latch before ItemSize changes it const float line_height = ImMax(ImMin(window->DC.CurrentLineSize.y, g.FontSize + g.Style.FramePadding.y*2), g.FontSize); 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 ItemSize(bb); if (!ItemAdd(bb, 0)) return; // Render RenderBullet(bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f)); RenderText(bb.Min+ImVec2(g.FontSize + style.FramePadding.x*2, text_base_offset_y), text_begin, text_end, false); } //------------------------------------------------------------------------- // [SECTION] Widgets: Main //------------------------------------------------------------------------- // - ButtonBehavior() [Internal] // - Button() // - SmallButton() // - InvisibleButton() // - ArrowButton() // - CloseButton() [Internal] // - CollapseButton() [Internal] // - Scrollbar() [Internal] // - Image() // - ImageButton() // - Checkbox() // - CheckboxFlags() // - RadioButton() // - ProgressBar() // - Bullet() //------------------------------------------------------------------------- // The ButtonBehavior() function is key to many interactions and used by many/most widgets. // Because we handle so many cases (keyboard/gamepad navigation, drag and drop) and many specific behavior (via ImGuiButtonFlags_), // this code is a little complex. // By far the most common path is interacting with the Mouse using the default ImGuiButtonFlags_PressedOnClickRelease button behavior. // See the series of events below and the corresponding state reported by dear imgui: //------------------------------------------------------------------------------------------------------------------------------------------------ // with PressedOnClickRelease: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() // Frame N+0 (mouse is outside bb) - - - - - - // Frame N+1 (mouse moves inside bb) - true - - - - // Frame N+2 (mouse button is down) - true true true - true // Frame N+3 (mouse button is down) - true true - - - // Frame N+4 (mouse moves outside bb) - - true - - - // Frame N+5 (mouse moves inside bb) - true true - - - // Frame N+6 (mouse button is released) true true - - true - // Frame N+7 (mouse button is released) - true - - - - // Frame N+8 (mouse moves outside bb) - - - - - - //------------------------------------------------------------------------------------------------------------------------------------------------ // with PressedOnClick: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() // Frame N+2 (mouse button is down) true true true true - true // Frame N+3 (mouse button is down) - true true - - - // Frame N+6 (mouse button is released) - true - - true - // Frame N+7 (mouse button is released) - true - - - - //------------------------------------------------------------------------------------------------------------------------------------------------ // with PressedOnRelease: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() // Frame N+2 (mouse button is down) - true - - - true // Frame N+3 (mouse button is down) - true - - - - // Frame N+6 (mouse button is released) true true - - - - // Frame N+7 (mouse button is released) - true - - - - //------------------------------------------------------------------------------------------------------------------------------------------------ // with PressedOnDoubleClick: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() // Frame N+0 (mouse button is down) - true - - - true // Frame N+1 (mouse button is down) - true - - - - // Frame N+2 (mouse button is released) - true - - - - // Frame N+3 (mouse button is released) - true - - - - // Frame N+4 (mouse button is down) true true true true - true // Frame N+5 (mouse button is down) - true true - - - // Frame N+6 (mouse button is released) - true - - true - // Frame N+7 (mouse button is released) - true - - - - //------------------------------------------------------------------------------------------------------------------------------------------------ // The behavior of the return-value changes when ImGuiButtonFlags_Repeat is set: // Repeat+ Repeat+ Repeat+ Repeat+ // PressedOnClickRelease PressedOnClick PressedOnRelease PressedOnDoubleClick //------------------------------------------------------------------------------------------------------------------------------------------------- // Frame N+0 (mouse button is down) - true - true // ... - - - - // Frame N + RepeatDelay true true - true // ... - - - - // Frame N + RepeatDelay + RepeatRate*N true true - true //------------------------------------------------------------------------------------------------------------------------------------------------- bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (flags & ImGuiButtonFlags_Disabled) { if (out_hovered) *out_hovered = false; if (out_held) *out_held = false; if (g.ActiveId == id) ClearActiveID(); return false; } // Default behavior requires click+release on same spot if ((flags & (ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnRelease | ImGuiButtonFlags_PressedOnDoubleClick)) == 0) flags |= ImGuiButtonFlags_PressedOnClickRelease; ImGuiWindow* backup_hovered_window = g.HoveredWindow; const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredRootWindow == window->RootWindow; if (flatten_hovered_children) g.HoveredWindow = window; #ifdef IMGUI_ENABLE_TEST_ENGINE if (id != 0 && window->DC.LastItemId != id) ImGuiTestEngineHook_ItemAdd(&g, bb, id); #endif bool pressed = false; bool hovered = ItemHoverable(bb, id); // Drag source doesn't report as hovered if (hovered && g.DragDropActive && g.DragDropPayload.SourceId == id && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoDisableHover)) hovered = false; // Special mode for Drag and Drop where holding button pressed for a long time while dragging another item triggers the button if (g.DragDropActive && (flags & ImGuiButtonFlags_PressedOnDragDropHold) && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoHoldToOpenOthers)) if (IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) { hovered = true; SetHoveredID(id); if (CalcTypematicPressedRepeatAmount(g.HoveredIdTimer + 0.0001f, g.HoveredIdTimer + 0.0001f - g.IO.DeltaTime, 0.01f, 0.70f)) // FIXME: Our formula for CalcTypematicPressedRepeatAmount() is fishy { pressed = true; FocusWindow(window); } } if (flatten_hovered_children) g.HoveredWindow = backup_hovered_window; // 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. if (hovered && (flags & ImGuiButtonFlags_AllowItemOverlap) && (g.HoveredIdPreviousFrame != id && g.HoveredIdPreviousFrame != 0)) hovered = false; // Mouse if (hovered) { if (!(flags & ImGuiButtonFlags_NoKeyModifiers) || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt)) { if ((flags & ImGuiButtonFlags_PressedOnClickRelease) && g.IO.MouseClicked[0]) { SetActiveID(id, window); if (!(flags & ImGuiButtonFlags_NoNavFocus)) SetFocusID(id, window); FocusWindow(window); } if (((flags & ImGuiButtonFlags_PressedOnClick) && g.IO.MouseClicked[0]) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDoubleClicked[0])) { pressed = true; if (flags & ImGuiButtonFlags_NoHoldingActiveID) ClearActiveID(); else SetActiveID(id, window); // Hold on ID FocusWindow(window); } if ((flags & ImGuiButtonFlags_PressedOnRelease) && g.IO.MouseReleased[0]) { if (!((flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[0] >= g.IO.KeyRepeatDelay)) // Repeat mode trumps pressed = true; ClearActiveID(); } // 'Repeat' mode acts when held regardless of _PressedOn flags (see table above). // Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings. if ((flags & ImGuiButtonFlags_Repeat) && g.ActiveId == id && g.IO.MouseDownDuration[0] > 0.0f && IsMouseClicked(0, true)) pressed = true; } if (pressed) g.NavDisableHighlight = true; } // Gamepad/Keyboard navigation // We report navigated item as hovered but we don't set g.HoveredId to not interfere with mouse. if (g.NavId == id && !g.NavDisableHighlight && g.NavDisableMouseHover && (g.ActiveId == 0 || g.ActiveId == id || g.ActiveId == window->MoveId)) if (!(flags & ImGuiButtonFlags_NoHoveredOnNav)) hovered = true; if (g.NavActivateDownId == id) { bool nav_activated_by_code = (g.NavActivateId == id); bool nav_activated_by_inputs = IsNavInputPressed(ImGuiNavInput_Activate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed); if (nav_activated_by_code || nav_activated_by_inputs) pressed = true; if (nav_activated_by_code || nav_activated_by_inputs || g.ActiveId == id) { // Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button. g.NavActivateId = id; // This is so SetActiveId assign a Nav source SetActiveID(id, window); if ((nav_activated_by_code || nav_activated_by_inputs) && !(flags & ImGuiButtonFlags_NoNavFocus)) SetFocusID(id, window); g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right) | (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); } } bool held = false; if (g.ActiveId == id) { if (pressed) g.ActiveIdHasBeenPressed = true; if (g.ActiveIdSource == ImGuiInputSource_Mouse) { if (g.ActiveIdIsJustActivated) g.ActiveIdClickOffset = g.IO.MousePos - bb.Min; if (g.IO.MouseDown[0]) { held = true; } else { if (hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease)) if (!((flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[0] >= g.IO.KeyRepeatDelay)) // Repeat mode trumps if (!g.DragDropActive) pressed = true; ClearActiveID(); } if (!(flags & ImGuiButtonFlags_NoNavFocus)) g.NavDisableHighlight = true; } else if (g.ActiveIdSource == ImGuiInputSource_Nav) { if (g.NavActivateDownId != id) ClearActiveID(); } } if (out_hovered) *out_hovered = hovered; if (out_held) *out_held = held; return pressed; } bool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); ImVec2 pos = window->DC.CursorPos; 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) pos.y += window->DC.CurrentLineTextBaseOffset - style.FramePadding.y; ImVec2 size = CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f); const ImRect bb(pos, pos + size); ItemSize(size, style.FramePadding.y); if (!ItemAdd(bb, id)) return false; if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat) flags |= ImGuiButtonFlags_Repeat; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); if (pressed) MarkItemEdited(id); // Render const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); RenderNavHighlight(bb, id); RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, label, NULL, &label_size, style.ButtonTextAlign, &bb); // Automatically close popups //if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup)) // CloseCurrentPopup(); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags); return pressed; } bool ImGui::Button(const char* label, const ImVec2& size_arg) { return ButtonEx(label, size_arg, 0); } // Small buttons fits within text without additional vertical spacing. bool ImGui::SmallButton(const char* label) { ImGuiContext& g = *GImGui; float backup_padding_y = g.Style.FramePadding.y; g.Style.FramePadding.y = 0.0f; bool pressed = ButtonEx(label, ImVec2(0, 0), ImGuiButtonFlags_AlignTextBaseLine); g.Style.FramePadding.y = backup_padding_y; return pressed; } // Tip: use ImGui::PushID()/PopID() to push indices or pointers in the ID stack. // 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) bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; // Cannot use zero-size for InvisibleButton(). Unlike Button() there is not way to fallback using the label size. IM_ASSERT(size_arg.x != 0.0f && size_arg.y != 0.0f); const ImGuiID id = window->GetID(str_id); ImVec2 size = CalcItemSize(size_arg, 0.0f, 0.0f); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); ItemSize(size); if (!ItemAdd(bb, id)) return false; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held); return pressed; } bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiButtonFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiID id = window->GetID(str_id); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); const float default_size = GetFrameHeight(); ItemSize(size, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f); if (!ItemAdd(bb, id)) return false; if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat) flags |= ImGuiButtonFlags_Repeat; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); // Render const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); RenderNavHighlight(bb, id); RenderFrame(bb.Min, bb.Max, col, true, g.Style.FrameRounding); RenderArrow(bb.Min + ImVec2(ImMax(0.0f, (size.x - g.FontSize) * 0.5f), ImMax(0.0f, (size.y - g.FontSize) * 0.5f)), dir); return pressed; } bool ImGui::ArrowButton(const char* str_id, ImGuiDir dir) { float sz = GetFrameHeight(); return ArrowButtonEx(str_id, dir, ImVec2(sz, sz), 0); } // Button to close a window bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos, float radius) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; // We intentionally allow interaction when clipped so that a mechanical Alt,Right,Validate sequence close a window. // (this isn't the regular behavior of buttons, but it doesn't affect the user much because navigation tends to keep items visible). const ImRect bb(pos - ImVec2(radius,radius), pos + ImVec2(radius,radius)); bool is_clipped = !ItemAdd(bb, id); bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held); if (is_clipped) return pressed; // Render ImVec2 center = bb.GetCenter(); if (hovered) window->DrawList->AddCircleFilled(center, ImMax(2.0f, radius), GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered), 9); float cross_extent = (radius * 0.7071f) - 1.0f; ImU32 cross_col = GetColorU32(ImGuiCol_Text); center -= ImVec2(0.5f, 0.5f); window->DrawList->AddLine(center + ImVec2(+cross_extent,+cross_extent), center + ImVec2(-cross_extent,-cross_extent), cross_col, 1.0f); window->DrawList->AddLine(center + ImVec2(+cross_extent,-cross_extent), center + ImVec2(-cross_extent,+cross_extent), cross_col, 1.0f); return pressed; } // The Collapse button also functions as a Dock Menu button. bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos, ImGuiDockNode* dock_node) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f); ItemAdd(bb, id); bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_None); //bool is_dock_menu = (window->DockNodeAsHost && !window->Collapsed); ImVec2 off = dock_node ? ImVec2((float)(int)(-g.Style.ItemInnerSpacing.x * 0.5f) + 0.5f, 0.0f) : ImVec2(0.0f, 0.0f); ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); if (hovered || held) window->DrawList->AddCircleFilled(bb.GetCenter() + off + ImVec2(0,-0.5f), g.FontSize * 0.5f + 1.0f, col, 9); if (dock_node) RenderArrowDockMenu(window->DrawList, bb.Min + g.Style.FramePadding, g.FontSize, GetColorU32(ImGuiCol_Text)); else RenderArrow(bb.Min + g.Style.FramePadding, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); // Switch to moving the window after mouse is moved beyond the initial drag threshold if (IsItemActive() && IsMouseDragging(0)) { bool can_extract_dock_node = false; if (dock_node != NULL && dock_node->VisibleWindow && !(dock_node->VisibleWindow->Flags & ImGuiWindowFlags_NoMove)) { ImGuiDockNode* root_node = DockNodeGetRootNode(dock_node); if (root_node->OnlyNodeWithWindows != dock_node || (root_node->CentralNode != NULL)) can_extract_dock_node = true; } if (can_extract_dock_node) { float threshold_base = g.FontSize; float threshold_x = (threshold_base * 2.2f); float threshold_y = (threshold_base * 1.5f); IM_ASSERT(window->DockNodeAsHost != NULL); if (g.IO.MouseDragMaxDistanceAbs[0].x > threshold_x || g.IO.MouseDragMaxDistanceAbs[0].y > threshold_y) DockContextQueueUndockNode(&g, dock_node); } else { ImVec2 backup_active_click_offset = g.ActiveIdClickOffset; StartMouseMovingWindow(window); g.ActiveIdClickOffset = backup_active_click_offset; } } return pressed; } ImGuiID ImGui::GetScrollbarID(ImGuiWindow* window, ImGuiAxis axis) { return window->GetIDNoKeepAlive(axis == ImGuiAxis_X ? "#SCROLLX" : "#SCROLLY"); } // Vertical/Horizontal scrollbar // The entire piece of code below is rather confusing because: // - We handle absolute seeking (when first clicking outside the grab) and relative manipulation (afterward or when clicking inside the grab) // - We store values as normalized ratio and in a form that allows the window content to change while we are holding on a scrollbar // - We handle both horizontal and vertical scrollbars, which makes the terminology not ideal. void ImGui::Scrollbar(ImGuiAxis axis) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; const bool horizontal = (axis == ImGuiAxis_X); const ImGuiStyle& style = g.Style; const ImGuiID id = GetScrollbarID(window, axis); KeepAliveID(id); // Render background bool other_scrollbar = (horizontal ? window->ScrollbarY : window->ScrollbarX); float other_scrollbar_size_w = other_scrollbar ? style.ScrollbarSize : 0.0f; const ImRect host_rect = window->Rect(); const float border_size = window->WindowBorderSize; ImRect bb = horizontal ? 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) : 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); bb.Min.x = ImMax(host_rect.Min.x, bb.Min.x); // Handle case where the host rectangle is smaller than the scrollbar bb.Min.y = ImMax(host_rect.Min.y, bb.Min.y); if (!horizontal) bb.Min.y += window->TitleBarHeight() + ((window->Flags & ImGuiWindowFlags_MenuBar) ? window->MenuBarHeight() : 0.0f); // FIXME: InnerRect? const float bb_width = bb.GetWidth(); const float bb_height = bb.GetHeight(); if (bb_width <= 0.0f || bb_height <= 0.0f) return; // 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) float alpha = 1.0f; if ((axis == ImGuiAxis_Y) && bb_height < g.FontSize + g.Style.FramePadding.y * 2.0f) { alpha = ImSaturate((bb_height - g.FontSize) / (g.Style.FramePadding.y * 2.0f)); if (alpha <= 0.0f) return; } const bool allow_interaction = (alpha >= 1.0f); int window_rounding_corners; if (horizontal) window_rounding_corners = ImDrawCornerFlags_BotLeft | (other_scrollbar ? 0 : ImDrawCornerFlags_BotRight); else window_rounding_corners = (((window->Flags & ImGuiWindowFlags_NoTitleBar) && !(window->Flags & ImGuiWindowFlags_MenuBar)) ? ImDrawCornerFlags_TopRight : 0) | (other_scrollbar ? 0 : ImDrawCornerFlags_BotRight); window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_ScrollbarBg), window->WindowRounding, window_rounding_corners); 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))); // V denote the main, longer axis of the scrollbar (= height for a vertical scrollbar) float scrollbar_size_v = horizontal ? bb.GetWidth() : bb.GetHeight(); float scroll_v = horizontal ? window->Scroll.x : window->Scroll.y; float win_size_avail_v = (horizontal ? window->SizeFull.x : window->SizeFull.y) - other_scrollbar_size_w; float win_size_contents_v = horizontal ? window->SizeContents.x : window->SizeContents.y; // Calculate the height of our grabbable box. It generally represent the amount visible (vs the total scrollable amount) // But we maintain a minimum size in pixel to allow for the user to still aim inside. 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. const float win_size_v = ImMax(ImMax(win_size_contents_v, win_size_avail_v), 1.0f); const float grab_h_pixels = ImClamp(scrollbar_size_v * (win_size_avail_v / win_size_v), style.GrabMinSize, scrollbar_size_v); const float grab_h_norm = grab_h_pixels / scrollbar_size_v; // Handle input right away. None of the code of Begin() is relying on scrolling position before calling Scrollbar(). bool held = false; bool hovered = false; const bool previously_held = (g.ActiveId == id); ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_NoNavFocus); float scroll_max = ImMax(1.0f, win_size_contents_v - win_size_avail_v); float scroll_ratio = ImSaturate(scroll_v / scroll_max); float grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; if (held && allow_interaction && grab_h_norm < 1.0f) { float scrollbar_pos_v = horizontal ? bb.Min.x : bb.Min.y; float mouse_pos_v = horizontal ? g.IO.MousePos.x : g.IO.MousePos.y; float* click_delta_to_grab_center_v = horizontal ? &g.ScrollbarClickDeltaToGrabCenter.x : &g.ScrollbarClickDeltaToGrabCenter.y; // Click position in scrollbar normalized space (0.0f->1.0f) const float clicked_v_norm = ImSaturate((mouse_pos_v - scrollbar_pos_v) / scrollbar_size_v); SetHoveredID(id); bool seek_absolute = false; if (!previously_held) { // On initial click calculate the distance between mouse and the center of the grab if (clicked_v_norm >= grab_v_norm && clicked_v_norm <= grab_v_norm + grab_h_norm) { *click_delta_to_grab_center_v = clicked_v_norm - grab_v_norm - grab_h_norm*0.5f; } else { seek_absolute = true; *click_delta_to_grab_center_v = 0.0f; } } // Apply scroll // 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 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)); scroll_v = (float)(int)(0.5f + scroll_v_norm * scroll_max);//(win_size_contents_v - win_size_v)); if (horizontal) window->Scroll.x = scroll_v; else window->Scroll.y = scroll_v; // Update values for rendering scroll_ratio = ImSaturate(scroll_v / scroll_max); grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; // Update distance to grab now that we have seeked and saturated if (seek_absolute) *click_delta_to_grab_center_v = clicked_v_norm - grab_v_norm - grab_h_norm*0.5f; } // Render grab const ImU32 grab_col = GetColorU32(held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab, alpha); ImRect grab_rect; if (horizontal) 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); else 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)); window->DrawList->AddRectFilled(grab_rect.Min, grab_rect.Max, grab_col, style.ScrollbarRounding); } void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); if (border_col.w > 0.0f) bb.Max += ImVec2(2, 2); ItemSize(bb); if (!ItemAdd(bb, 0)) return; if (border_col.w > 0.0f) { window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(border_col), 0.0f); window->DrawList->AddImage(user_texture_id, bb.Min + ImVec2(1, 1), bb.Max - ImVec2(1, 1), uv0, uv1, GetColorU32(tint_col)); } else { window->DrawList->AddImage(user_texture_id, bb.Min, bb.Max, uv0, uv1, GetColorU32(tint_col)); } } // frame_padding < 0: uses FramePadding from style (default) // frame_padding = 0: no framing // frame_padding > 0: set framing size // The color used are the button colors. bool 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) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; // Default to using texture ID as ID. User can still push string/integer prefixes. // We could hash the size/uv to create a unique ID but that would prevent the user from animating UV. PushID((void*)(intptr_t)user_texture_id); const ImGuiID id = window->GetID("#image"); PopID(); const ImVec2 padding = (frame_padding >= 0) ? ImVec2((float)frame_padding, (float)frame_padding) : style.FramePadding; const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding * 2); const ImRect image_bb(window->DC.CursorPos + padding, window->DC.CursorPos + padding + size); ItemSize(bb); if (!ItemAdd(bb, id)) return false; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held); // Render const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); RenderNavHighlight(bb, id); RenderFrame(bb.Min, bb.Max, col, true, ImClamp((float)ImMin(padding.x, padding.y), 0.0f, style.FrameRounding)); if (bg_col.w > 0.0f) window->DrawList->AddRectFilled(image_bb.Min, image_bb.Max, GetColorU32(bg_col)); window->DrawList->AddImage(user_texture_id, image_bb.Min, image_bb.Max, uv0, uv1, GetColorU32(tint_col)); return pressed; } bool ImGui::Checkbox(const char* label, bool* v) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); const float square_sz = GetFrameHeight(); const ImVec2 pos = window->DC.CursorPos; 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)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, id)) return false; bool hovered, held; bool pressed = ButtonBehavior(total_bb, id, &hovered, &held); if (pressed) { *v = !(*v); MarkItemEdited(id); } const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz)); RenderNavHighlight(total_bb, id); RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding); if (*v) { const float pad = ImMax(1.0f, (float)(int)(square_sz / 6.0f)); RenderCheckMark(check_bb.Min + ImVec2(pad, pad), GetColorU32(ImGuiCol_CheckMark), square_sz - pad*2.0f); } if (g.LogEnabled) LogRenderedText(&total_bb.Min, *v ? "[x]" : "[ ]"); if (label_size.x > 0.0f) RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); return pressed; } bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value) { bool v = ((*flags & flags_value) == flags_value); bool pressed = Checkbox(label, &v); if (pressed) { if (v) *flags |= flags_value; else *flags &= ~flags_value; } return pressed; } bool ImGui::RadioButton(const char* label, bool active) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); const float square_sz = GetFrameHeight(); const ImVec2 pos = window->DC.CursorPos; const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz)); 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)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, id)) return false; ImVec2 center = check_bb.GetCenter(); center.x = (float)(int)center.x + 0.5f; center.y = (float)(int)center.y + 0.5f; const float radius = (square_sz - 1.0f) * 0.5f; bool hovered, held; bool pressed = ButtonBehavior(total_bb, id, &hovered, &held); if (pressed) MarkItemEdited(id); RenderNavHighlight(total_bb, id); window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), 16); if (active) { const float pad = ImMax(1.0f, (float)(int)(square_sz / 6.0f)); window->DrawList->AddCircleFilled(center, radius - pad, GetColorU32(ImGuiCol_CheckMark), 16); } if (style.FrameBorderSize > 0.0f) { window->DrawList->AddCircle(center + ImVec2(1,1), radius, GetColorU32(ImGuiCol_BorderShadow), 16, style.FrameBorderSize); window->DrawList->AddCircle(center, radius, GetColorU32(ImGuiCol_Border), 16, style.FrameBorderSize); } if (g.LogEnabled) LogRenderedText(&total_bb.Min, active ? "(x)" : "( )"); if (label_size.x > 0.0f) RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); return pressed; } bool ImGui::RadioButton(const char* label, int* v, int v_button) { const bool pressed = RadioButton(label, *v == v_button); if (pressed) *v = v_button; return pressed; } // size_arg (for each axis) < 0.0f: align to end, 0.0f: auto, > 0.0f: specified size void ImGui::ProgressBar(float fraction, const ImVec2& size_arg, const char* overlay) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; ImVec2 pos = window->DC.CursorPos; ImVec2 size = CalcItemSize(size_arg, GetNextItemWidth(), g.FontSize + style.FramePadding.y*2.0f); ImRect bb(pos, pos + size); ItemSize(size, style.FramePadding.y); if (!ItemAdd(bb, 0)) return; // Render fraction = ImSaturate(fraction); RenderFrame(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); bb.Expand(ImVec2(-style.FrameBorderSize, -style.FrameBorderSize)); const ImVec2 fill_br = ImVec2(ImLerp(bb.Min.x, bb.Max.x, fraction), bb.Max.y); RenderRectFilledRangeH(window->DrawList, bb, GetColorU32(ImGuiCol_PlotHistogram), 0.0f, fraction, style.FrameRounding); // Default displaying the fraction as percentage string, but user can override it char overlay_buf[32]; if (!overlay) { ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%.0f%%", fraction*100+0.01f); overlay = overlay_buf; } ImVec2 overlay_size = CalcTextSize(overlay, NULL); if (overlay_size.x > 0.0f) 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); } void ImGui::Bullet() { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const float line_height = ImMax(ImMin(window->DC.CurrentLineSize.y, g.FontSize + g.Style.FramePadding.y*2), g.FontSize); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize, line_height)); ItemSize(bb); if (!ItemAdd(bb, 0)) { SameLine(0, style.FramePadding.x*2); return; } // Render and stay on same line RenderBullet(bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f)); SameLine(0, style.FramePadding.x*2); } //------------------------------------------------------------------------- // [SECTION] Widgets: Low-level Layout helpers //------------------------------------------------------------------------- // - Spacing() // - Dummy() // - NewLine() // - AlignTextToFramePadding() // - Separator() // - VerticalSeparator() [Internal] // - SplitterBehavior() [Internal] //------------------------------------------------------------------------- void ImGui::Spacing() { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ItemSize(ImVec2(0,0)); } void ImGui::Dummy(const ImVec2& size) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); ItemSize(size); ItemAdd(bb, 0); } void ImGui::NewLine() { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; const ImGuiLayoutType backup_layout_type = window->DC.LayoutType; window->DC.LayoutType = ImGuiLayoutType_Vertical; 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. ItemSize(ImVec2(0,0)); else ItemSize(ImVec2(0.0f, g.FontSize)); window->DC.LayoutType = backup_layout_type; } void ImGui::AlignTextToFramePadding() { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; window->DC.CurrentLineSize.y = ImMax(window->DC.CurrentLineSize.y, g.FontSize + g.Style.FramePadding.y * 2); window->DC.CurrentLineTextBaseOffset = ImMax(window->DC.CurrentLineTextBaseOffset, g.Style.FramePadding.y); } // Horizontal/vertical separating line void ImGui::Separator() { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; // Those flags should eventually be overrideable by the user ImGuiSeparatorFlags flags = (window->DC.LayoutType == ImGuiLayoutType_Horizontal) ? ImGuiSeparatorFlags_Vertical : ImGuiSeparatorFlags_Horizontal; IM_ASSERT(ImIsPowerOfTwo(flags & (ImGuiSeparatorFlags_Horizontal | ImGuiSeparatorFlags_Vertical))); // Check that only 1 option is selected if (flags & ImGuiSeparatorFlags_Vertical) { VerticalSeparator(); return; } // Horizontal Separator if (window->DC.CurrentColumns) PopClipRect(); float x1 = window->Pos.x; float x2 = window->Pos.x + window->Size.x; if (!window->DC.GroupStack.empty()) x1 += window->DC.Indent.x; const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y+1.0f)); ItemSize(ImVec2(0.0f, 1.0f)); // NB: we don't provide our width so that it doesn't get feed back into AutoFit if (!ItemAdd(bb, 0)) { if (window->DC.CurrentColumns) PushColumnClipRect(); return; } window->DrawList->AddLine(bb.Min, ImVec2(bb.Max.x,bb.Min.y), GetColorU32(ImGuiCol_Separator)); if (g.LogEnabled) LogRenderedText(&bb.Min, "--------------------------------"); if (window->DC.CurrentColumns) { PushColumnClipRect(); window->DC.CurrentColumns->LineMinY = window->DC.CursorPos.y; } } void ImGui::VerticalSeparator() { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; float y1 = window->DC.CursorPos.y; float y2 = window->DC.CursorPos.y + window->DC.CurrentLineSize.y; const ImRect bb(ImVec2(window->DC.CursorPos.x, y1), ImVec2(window->DC.CursorPos.x + 1.0f, y2)); ItemSize(ImVec2(1.0f, 0.0f)); if (!ItemAdd(bb, 0)) return; window->DrawList->AddLine(ImVec2(bb.Min.x, bb.Min.y), ImVec2(bb.Min.x, bb.Max.y), GetColorU32(ImGuiCol_Separator)); if (g.LogEnabled) LogText(" |"); } // 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. bool 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) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags; window->DC.ItemFlags |= ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus; bool item_add = ItemAdd(bb, id); window->DC.ItemFlags = item_flags_backup; if (!item_add) return false; bool hovered, held; ImRect bb_interact = bb; bb_interact.Expand(axis == ImGuiAxis_Y ? ImVec2(0.0f, hover_extend) : ImVec2(hover_extend, 0.0f)); ButtonBehavior(bb_interact, id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap); if (g.ActiveId != id) SetItemAllowOverlap(); if (held || (g.HoveredId == id && g.HoveredIdPreviousFrame == id && g.HoveredIdTimer >= hover_visibility_delay)) SetMouseCursor(axis == ImGuiAxis_Y ? ImGuiMouseCursor_ResizeNS : ImGuiMouseCursor_ResizeEW); ImRect bb_render = bb; if (held) { ImVec2 mouse_delta_2d = g.IO.MousePos - g.ActiveIdClickOffset - bb_interact.Min; float mouse_delta = (axis == ImGuiAxis_Y) ? mouse_delta_2d.y : mouse_delta_2d.x; // Minimum pane size float size_1_maximum_delta = ImMax(0.0f, *size1 - min_size1); float size_2_maximum_delta = ImMax(0.0f, *size2 - min_size2); if (mouse_delta < -size_1_maximum_delta) mouse_delta = -size_1_maximum_delta; if (mouse_delta > size_2_maximum_delta) mouse_delta = size_2_maximum_delta; // Apply resize if (mouse_delta != 0.0f) { if (mouse_delta < 0.0f) IM_ASSERT(*size1 + mouse_delta >= min_size1); if (mouse_delta > 0.0f) IM_ASSERT(*size2 - mouse_delta >= min_size2); *size1 += mouse_delta; *size2 -= mouse_delta; bb_render.Translate((axis == ImGuiAxis_X) ? ImVec2(mouse_delta, 0.0f) : ImVec2(0.0f, mouse_delta)); MarkItemEdited(id); } } // Render const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : (hovered && g.HoveredIdTimer >= hover_visibility_delay) ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); window->DrawList->AddRectFilled(bb_render.Min, bb_render.Max, col, g.Style.FrameRounding); return held; } //------------------------------------------------------------------------- // [SECTION] Widgets: ComboBox //------------------------------------------------------------------------- // - BeginCombo() // - EndCombo() // - Combo() //------------------------------------------------------------------------- static float CalcMaxPopupHeightFromItemCount(int items_count) { ImGuiContext& g = *GImGui; if (items_count <= 0) return FLT_MAX; return (g.FontSize + g.Style.ItemSpacing.y) * items_count - g.Style.ItemSpacing.y + (g.Style.WindowPadding.y * 2); } bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags) { // Always consume the SetNextWindowSizeConstraint() call in our early return paths ImGuiContext& g = *GImGui; ImGuiCond backup_next_window_size_constraint = g.NextWindowData.SizeConstraintCond; g.NextWindowData.SizeConstraintCond = 0; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; IM_ASSERT((flags & (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) != (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)); // Can't use both flags together const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const float arrow_size = (flags & ImGuiComboFlags_NoArrowButton) ? 0.0f : GetFrameHeight(); const ImVec2 label_size = CalcTextSize(label, NULL, true); const float expected_w = GetNextItemWidth(); const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : expected_w; const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); 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)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, id, &frame_bb)) return false; bool hovered, held; bool pressed = ButtonBehavior(frame_bb, id, &hovered, &held); bool popup_open = IsPopupOpen(id); const ImU32 frame_col = GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); const float value_x2 = ImMax(frame_bb.Min.x, frame_bb.Max.x - arrow_size); RenderNavHighlight(frame_bb, id); if (!(flags & ImGuiComboFlags_NoPreview)) window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(value_x2, frame_bb.Max.y), frame_col, style.FrameRounding, ImDrawCornerFlags_Left); if (!(flags & ImGuiComboFlags_NoArrowButton)) { 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); RenderArrow(ImVec2(value_x2 + style.FramePadding.y, frame_bb.Min.y + style.FramePadding.y), ImGuiDir_Down); } RenderFrameBorder(frame_bb.Min, frame_bb.Max, style.FrameRounding); if (preview_value != NULL && !(flags & ImGuiComboFlags_NoPreview)) RenderTextClipped(frame_bb.Min + style.FramePadding, ImVec2(value_x2, frame_bb.Max.y), preview_value, NULL, NULL, ImVec2(0.0f,0.0f)); if (label_size.x > 0) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); if ((pressed || g.NavActivateId == id) && !popup_open) { if (window->DC.NavLayerCurrent == 0) window->NavLastIds[0] = id; OpenPopupEx(id); popup_open = true; } if (!popup_open) return false; if (backup_next_window_size_constraint) { g.NextWindowData.SizeConstraintCond = backup_next_window_size_constraint; g.NextWindowData.SizeConstraintRect.Min.x = ImMax(g.NextWindowData.SizeConstraintRect.Min.x, w); } else { if ((flags & ImGuiComboFlags_HeightMask_) == 0) flags |= ImGuiComboFlags_HeightRegular; IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiComboFlags_HeightMask_)); // Only one int popup_max_height_in_items = -1; if (flags & ImGuiComboFlags_HeightRegular) popup_max_height_in_items = 8; else if (flags & ImGuiComboFlags_HeightSmall) popup_max_height_in_items = 4; else if (flags & ImGuiComboFlags_HeightLarge) popup_max_height_in_items = 20; SetNextWindowSizeConstraints(ImVec2(w, 0.0f), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); } char name[16]; ImFormatString(name, IM_ARRAYSIZE(name), "##Combo_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth // Peak into expected window size so we can position it if (ImGuiWindow* popup_window = FindWindowByName(name)) if (popup_window->WasActive) { ImVec2 size_expected = CalcWindowExpectedSize(popup_window); if (flags & ImGuiComboFlags_PopupAlignLeft) popup_window->AutoPosLastDirection = ImGuiDir_Left; ImRect r_outer = GetWindowAllowedExtentRect(popup_window); ImVec2 pos = FindBestWindowPosForPopupEx(frame_bb.GetBL(), size_expected, &popup_window->AutoPosLastDirection, r_outer, frame_bb, ImGuiPopupPositionPolicy_ComboBox); SetNextWindowPos(pos); } // Horizontally align ourselves with the framed text ImGuiWindowFlags window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings; PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(style.FramePadding.x, style.WindowPadding.y)); bool ret = Begin(name, NULL, window_flags); PopStyleVar(); if (!ret) { EndPopup(); IM_ASSERT(0); // This should never happen as we tested for IsPopupOpen() above return false; } return true; } void ImGui::EndCombo() { EndPopup(); } // Getter for the old Combo() API: const char*[] static bool Items_ArrayGetter(void* data, int idx, const char** out_text) { const char* const* items = (const char* const*)data; if (out_text) *out_text = items[idx]; return true; } // Getter for the old Combo() API: "item1\0item2\0item3\0" static bool Items_SingleStringGetter(void* data, int idx, const char** out_text) { // FIXME-OPT: we could pre-compute the indices to fasten this. But only 1 active combo means the waste is limited. const char* items_separated_by_zeros = (const char*)data; int items_count = 0; const char* p = items_separated_by_zeros; while (*p) { if (idx == items_count) break; p += strlen(p) + 1; items_count++; } if (!*p) return false; if (out_text) *out_text = p; return true; } // Old API, prefer using BeginCombo() nowadays if you can. bool 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) { ImGuiContext& g = *GImGui; // Call the getter to obtain the preview string which is a parameter to BeginCombo() const char* preview_value = NULL; if (*current_item >= 0 && *current_item < items_count) items_getter(data, *current_item, &preview_value); // 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. if (popup_max_height_in_items != -1 && !g.NextWindowData.SizeConstraintCond) SetNextWindowSizeConstraints(ImVec2(0,0), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); if (!BeginCombo(label, preview_value, ImGuiComboFlags_None)) return false; // Display items // FIXME-OPT: Use clipper (but we need to disable it on the appearing frame to make sure our call to SetItemDefaultFocus() is processed) bool value_changed = false; for (int i = 0; i < items_count; i++) { PushID((void*)(intptr_t)i); const bool item_selected = (i == *current_item); const char* item_text; if (!items_getter(data, i, &item_text)) item_text = "*Unknown item*"; if (Selectable(item_text, item_selected)) { value_changed = true; *current_item = i; } if (item_selected) SetItemDefaultFocus(); PopID(); } EndCombo(); return value_changed; } // Combo box helper allowing to pass an array of strings. bool ImGui::Combo(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items) { const bool value_changed = Combo(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_in_items); return value_changed; } // Combo box helper allowing to pass all items in a single string literal holding multiple zero-terminated items "item1\0item2\0" bool ImGui::Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items) { int items_count = 0; const char* p = items_separated_by_zeros; // FIXME-OPT: Avoid computing this, or at least only when combo is open while (*p) { p += strlen(p) + 1; items_count++; } bool value_changed = Combo(label, current_item, Items_SingleStringGetter, (void*)items_separated_by_zeros, items_count, height_in_items); return value_changed; } //------------------------------------------------------------------------- // [SECTION] Data Type and Data Formatting Helpers [Internal] //------------------------------------------------------------------------- // - PatchFormatStringFloatToInt() // - DataTypeGetInfo() // - DataTypeFormatString() // - DataTypeApplyOp() // - DataTypeApplyOpFromText() // - GetMinimumStepAtDecimalPrecision // - RoundScalarWithFormat<>() //------------------------------------------------------------------------- static const ImGuiDataTypeInfo GDataTypeInfo[] = { { sizeof(char), "%d", "%d" }, // ImGuiDataType_S8 { sizeof(unsigned char), "%u", "%u" }, { sizeof(short), "%d", "%d" }, // ImGuiDataType_S16 { sizeof(unsigned short), "%u", "%u" }, { sizeof(int), "%d", "%d" }, // ImGuiDataType_S32 { sizeof(unsigned int), "%u", "%u" }, #ifdef _MSC_VER { sizeof(ImS64), "%I64d","%I64d" }, // ImGuiDataType_S64 { sizeof(ImU64), "%I64u","%I64u" }, #else { sizeof(ImS64), "%lld", "%lld" }, // ImGuiDataType_S64 { sizeof(ImU64), "%llu", "%llu" }, #endif { sizeof(float), "%f", "%f" }, // ImGuiDataType_Float (float are promoted to double in va_arg) { sizeof(double), "%f", "%lf" }, // ImGuiDataType_Double }; IM_STATIC_ASSERT(IM_ARRAYSIZE(GDataTypeInfo) == ImGuiDataType_COUNT); // 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". // Even though we changed the compile-time default, we expect users to have carried %f around, which would break the display of DragInt() calls. // To honor backward compatibility we are rewriting the format string, unless IMGUI_DISABLE_OBSOLETE_FUNCTIONS is enabled. What could possibly go wrong?! static const char* PatchFormatStringFloatToInt(const char* fmt) { 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. return "%d"; const char* fmt_start = ImParseFormatFindStart(fmt); // Find % (if any, and ignore %%) 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). if (fmt_end > fmt_start && fmt_end[-1] == 'f') { #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS if (fmt_start == fmt && fmt_end[0] == 0) return "%d"; ImGuiContext& g = *GImGui; 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. return g.TempBuffer; #else IM_ASSERT(0 && "DragInt(): Invalid format string!"); // Old versions used a default parameter of "%.0f", please replace with e.g. "%d" #endif } return fmt; } const ImGuiDataTypeInfo* ImGui::DataTypeGetInfo(ImGuiDataType data_type) { IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT); return &GDataTypeInfo[data_type]; } int ImGui::DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* data_ptr, const char* format) { // Signedness doesn't matter when pushing integer arguments if (data_type == ImGuiDataType_S32 || data_type == ImGuiDataType_U32) return ImFormatString(buf, buf_size, format, *(const ImU32*)data_ptr); if (data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64) return ImFormatString(buf, buf_size, format, *(const ImU64*)data_ptr); if (data_type == ImGuiDataType_Float) return ImFormatString(buf, buf_size, format, *(const float*)data_ptr); if (data_type == ImGuiDataType_Double) return ImFormatString(buf, buf_size, format, *(const double*)data_ptr); if (data_type == ImGuiDataType_S8) return ImFormatString(buf, buf_size, format, *(const ImS8*)data_ptr); if (data_type == ImGuiDataType_U8) return ImFormatString(buf, buf_size, format, *(const ImU8*)data_ptr); if (data_type == ImGuiDataType_S16) return ImFormatString(buf, buf_size, format, *(const ImS16*)data_ptr); if (data_type == ImGuiDataType_U16) return ImFormatString(buf, buf_size, format, *(const ImU16*)data_ptr); IM_ASSERT(0); return 0; } void ImGui::DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg1, const void* arg2) { IM_ASSERT(op == '+' || op == '-'); switch (data_type) { case ImGuiDataType_S8: if (op == '+') { *(ImS8*)output = ImAddClampOverflow(*(const ImS8*)arg1, *(const ImS8*)arg2, IM_S8_MIN, IM_S8_MAX); } if (op == '-') { *(ImS8*)output = ImSubClampOverflow(*(const ImS8*)arg1, *(const ImS8*)arg2, IM_S8_MIN, IM_S8_MAX); } return; case ImGuiDataType_U8: if (op == '+') { *(ImU8*)output = ImAddClampOverflow(*(const ImU8*)arg1, *(const ImU8*)arg2, IM_U8_MIN, IM_U8_MAX); } if (op == '-') { *(ImU8*)output = ImSubClampOverflow(*(const ImU8*)arg1, *(const ImU8*)arg2, IM_U8_MIN, IM_U8_MAX); } return; case ImGuiDataType_S16: if (op == '+') { *(ImS16*)output = ImAddClampOverflow(*(const ImS16*)arg1, *(const ImS16*)arg2, IM_S16_MIN, IM_S16_MAX); } if (op == '-') { *(ImS16*)output = ImSubClampOverflow(*(const ImS16*)arg1, *(const ImS16*)arg2, IM_S16_MIN, IM_S16_MAX); } return; case ImGuiDataType_U16: if (op == '+') { *(ImU16*)output = ImAddClampOverflow(*(const ImU16*)arg1, *(const ImU16*)arg2, IM_U16_MIN, IM_U16_MAX); } if (op == '-') { *(ImU16*)output = ImSubClampOverflow(*(const ImU16*)arg1, *(const ImU16*)arg2, IM_U16_MIN, IM_U16_MAX); } return; case ImGuiDataType_S32: if (op == '+') { *(ImS32*)output = ImAddClampOverflow(*(const ImS32*)arg1, *(const ImS32*)arg2, IM_S32_MIN, IM_S32_MAX); } if (op == '-') { *(ImS32*)output = ImSubClampOverflow(*(const ImS32*)arg1, *(const ImS32*)arg2, IM_S32_MIN, IM_S32_MAX); } return; case ImGuiDataType_U32: if (op == '+') { *(ImU32*)output = ImAddClampOverflow(*(const ImU32*)arg1, *(const ImU32*)arg2, IM_U32_MIN, IM_U32_MAX); } if (op == '-') { *(ImU32*)output = ImSubClampOverflow(*(const ImU32*)arg1, *(const ImU32*)arg2, IM_U32_MIN, IM_U32_MAX); } return; case ImGuiDataType_S64: if (op == '+') { *(ImS64*)output = ImAddClampOverflow(*(const ImS64*)arg1, *(const ImS64*)arg2, IM_S64_MIN, IM_S64_MAX); } if (op == '-') { *(ImS64*)output = ImSubClampOverflow(*(const ImS64*)arg1, *(const ImS64*)arg2, IM_S64_MIN, IM_S64_MAX); } return; case ImGuiDataType_U64: if (op == '+') { *(ImU64*)output = ImAddClampOverflow(*(const ImU64*)arg1, *(const ImU64*)arg2, IM_U64_MIN, IM_U64_MAX); } if (op == '-') { *(ImU64*)output = ImSubClampOverflow(*(const ImU64*)arg1, *(const ImU64*)arg2, IM_U64_MIN, IM_U64_MAX); } return; case ImGuiDataType_Float: if (op == '+') { *(float*)output = *(const float*)arg1 + *(const float*)arg2; } if (op == '-') { *(float*)output = *(const float*)arg1 - *(const float*)arg2; } return; case ImGuiDataType_Double: if (op == '+') { *(double*)output = *(const double*)arg1 + *(const double*)arg2; } if (op == '-') { *(double*)output = *(const double*)arg1 - *(const double*)arg2; } return; case ImGuiDataType_COUNT: break; } IM_ASSERT(0); } // User can input math operators (e.g. +100) to edit a numerical values. // NB: This is _not_ a full expression evaluator. We should probably add one and replace this dumb mess.. bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* data_ptr, const char* format) { while (ImCharIsBlankA(*buf)) buf++; // We don't support '-' op because it would conflict with inputing negative value. // Instead you can use +-100 to subtract from an existing value char op = buf[0]; if (op == '+' || op == '*' || op == '/') { buf++; while (ImCharIsBlankA(*buf)) buf++; } else { op = 0; } if (!buf[0]) return false; // Copy the value in an opaque buffer so we can compare at the end of the function if it changed at all. IM_ASSERT(data_type < ImGuiDataType_COUNT); int data_backup[2]; const ImGuiDataTypeInfo* type_info = ImGui::DataTypeGetInfo(data_type); IM_ASSERT(type_info->Size <= sizeof(data_backup)); memcpy(data_backup, data_ptr, type_info->Size); if (format == NULL) format = type_info->ScanFmt; // FIXME-LEGACY: The aim is to remove those operators and write a proper expression evaluator at some point.. int arg1i = 0; if (data_type == ImGuiDataType_S32) { int* v = (int*)data_ptr; int arg0i = *v; float arg1f = 0.0f; if (op && sscanf(initial_value_buf, format, &arg0i) < 1) return false; // 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 if (op == '+') { if (sscanf(buf, "%d", &arg1i)) *v = (int)(arg0i + arg1i); } // Add (use "+-" to subtract) else if (op == '*') { if (sscanf(buf, "%f", &arg1f)) *v = (int)(arg0i * arg1f); } // Multiply else if (op == '/') { if (sscanf(buf, "%f", &arg1f) && arg1f != 0.0f) *v = (int)(arg0i / arg1f); } // Divide else { if (sscanf(buf, format, &arg1i) == 1) *v = arg1i; } // Assign constant } else if (data_type == ImGuiDataType_Float) { // For floats we have to ignore format with precision (e.g. "%.2f") because sscanf doesn't take them in format = "%f"; float* v = (float*)data_ptr; float arg0f = *v, arg1f = 0.0f; if (op && sscanf(initial_value_buf, format, &arg0f) < 1) return false; if (sscanf(buf, format, &arg1f) < 1) return false; if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract) else if (op == '*') { *v = arg0f * arg1f; } // Multiply else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide else { *v = arg1f; } // Assign constant } else if (data_type == ImGuiDataType_Double) { format = "%lf"; // scanf differentiate float/double unlike printf which forces everything to double because of ellipsis double* v = (double*)data_ptr; double arg0f = *v, arg1f = 0.0; if (op && sscanf(initial_value_buf, format, &arg0f) < 1) return false; if (sscanf(buf, format, &arg1f) < 1) return false; if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract) else if (op == '*') { *v = arg0f * arg1f; } // Multiply else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide else { *v = arg1f; } // Assign constant } else if (data_type == ImGuiDataType_U32 || data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64) { // All other types assign constant // 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. sscanf(buf, format, data_ptr); } else { // Small types need a 32-bit buffer to receive the result from scanf() int v32; sscanf(buf, format, &v32); if (data_type == ImGuiDataType_S8) *(ImS8*)data_ptr = (ImS8)ImClamp(v32, (int)IM_S8_MIN, (int)IM_S8_MAX); else if (data_type == ImGuiDataType_U8) *(ImU8*)data_ptr = (ImU8)ImClamp(v32, (int)IM_U8_MIN, (int)IM_U8_MAX); else if (data_type == ImGuiDataType_S16) *(ImS16*)data_ptr = (ImS16)ImClamp(v32, (int)IM_S16_MIN, (int)IM_S16_MAX); else if (data_type == ImGuiDataType_U16) *(ImU16*)data_ptr = (ImU16)ImClamp(v32, (int)IM_U16_MIN, (int)IM_U16_MAX); else IM_ASSERT(0); } return memcmp(data_backup, data_ptr, type_info->Size) != 0; } static float GetMinimumStepAtDecimalPrecision(int decimal_precision) { 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 }; if (decimal_precision < 0) return FLT_MIN; return (decimal_precision < IM_ARRAYSIZE(min_steps)) ? min_steps[decimal_precision] : ImPow(10.0f, (float)-decimal_precision); } template static const char* ImAtoi(const char* src, TYPE* output) { int negative = 0; if (*src == '-') { negative = 1; src++; } if (*src == '+') { src++; } TYPE v = 0; while (*src >= '0' && *src <= '9') v = (v * 10) + (*src++ - '0'); *output = negative ? -v : v; return src; } template TYPE ImGui::RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, TYPE v) { const char* fmt_start = ImParseFormatFindStart(format); if (fmt_start[0] != '%' || fmt_start[1] == '%') // Don't apply if the value is not visible in the format string return v; char v_str[64]; ImFormatString(v_str, IM_ARRAYSIZE(v_str), fmt_start, v); const char* p = v_str; while (*p == ' ') p++; if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) v = (TYPE)ImAtof(p); else ImAtoi(p, (SIGNEDTYPE*)&v); return v; } //------------------------------------------------------------------------- // [SECTION] Widgets: DragScalar, DragFloat, DragInt, etc. //------------------------------------------------------------------------- // - DragBehaviorT<>() [Internal] // - DragBehavior() [Internal] // - DragScalar() // - DragScalarN() // - DragFloat() // - DragFloat2() // - DragFloat3() // - DragFloat4() // - DragFloatRange2() // - DragInt() // - DragInt2() // - DragInt3() // - DragInt4() // - DragIntRange2() //------------------------------------------------------------------------- // This is called by DragBehavior() when the widget is active (held by mouse or being manipulated with Nav controls) template bool 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) { ImGuiContext& g = *GImGui; const ImGuiAxis axis = (flags & ImGuiDragFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); const bool has_min_max = (v_min != v_max); const bool is_power = (power != 1.0f && is_decimal && has_min_max && (v_max - v_min < FLT_MAX)); // Default tweak speed if (v_speed == 0.0f && has_min_max && (v_max - v_min < FLT_MAX)) v_speed = (float)((v_max - v_min) * g.DragSpeedDefaultRatio); // Inputs accumulates into g.DragCurrentAccum, which is flushed into the current value as soon as it makes a difference with our precision settings float adjust_delta = 0.0f; if (g.ActiveIdSource == ImGuiInputSource_Mouse && IsMousePosValid() && g.IO.MouseDragMaxDistanceSqr[0] > 1.0f*1.0f) { adjust_delta = g.IO.MouseDelta[axis]; if (g.IO.KeyAlt) adjust_delta *= 1.0f / 100.0f; if (g.IO.KeyShift) adjust_delta *= 10.0f; } else if (g.ActiveIdSource == ImGuiInputSource_Nav) { int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; adjust_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 1.0f / 10.0f, 10.0f)[axis]; v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision)); } adjust_delta *= v_speed; // For vertical drag we currently assume that Up=higher value (like we do with vertical sliders). This may become a parameter. if (axis == ImGuiAxis_Y) adjust_delta = -adjust_delta; // Clear current value on activation // 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. bool is_just_activated = g.ActiveIdIsJustActivated; 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)); bool is_drag_direction_change_with_power = is_power && ((adjust_delta < 0 && g.DragCurrentAccum > 0) || (adjust_delta > 0 && g.DragCurrentAccum < 0)); if (is_just_activated || is_already_past_limits_and_pushing_outward || is_drag_direction_change_with_power) { g.DragCurrentAccum = 0.0f; g.DragCurrentAccumDirty = false; } else if (adjust_delta != 0.0f) { g.DragCurrentAccum += adjust_delta; g.DragCurrentAccumDirty = true; } if (!g.DragCurrentAccumDirty) return false; TYPE v_cur = *v; FLOATTYPE v_old_ref_for_accum_remainder = (FLOATTYPE)0.0f; if (is_power) { // 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 FLOATTYPE v_old_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power); FLOATTYPE v_new_norm_curved = v_old_norm_curved + (g.DragCurrentAccum / (v_max - v_min)); v_cur = v_min + (TYPE)ImPow(ImSaturate((float)v_new_norm_curved), power) * (v_max - v_min); v_old_ref_for_accum_remainder = v_old_norm_curved; } else { v_cur += (TYPE)g.DragCurrentAccum; } // Round to user desired precision based on format string v_cur = RoundScalarWithFormatT(format, data_type, v_cur); // Preserve remainder after rounding has been applied. This also allow slow tweaking of values. g.DragCurrentAccumDirty = false; if (is_power) { FLOATTYPE v_cur_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power); g.DragCurrentAccum -= (float)(v_cur_norm_curved - v_old_ref_for_accum_remainder); } else { g.DragCurrentAccum -= (float)((SIGNEDTYPE)v_cur - (SIGNEDTYPE)*v); } // Lose zero sign for float/double if (v_cur == (TYPE)-0) v_cur = (TYPE)0; // Clamp values (+ handle overflow/wrap-around for integer types) if (*v != v_cur && has_min_max) { if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_decimal)) v_cur = v_min; if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_decimal)) v_cur = v_max; } // Apply result if (*v == v_cur) return false; *v = v_cur; return true; } bool 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) { ImGuiContext& g = *GImGui; if (g.ActiveId == id) { if (g.ActiveIdSource == ImGuiInputSource_Mouse && !g.IO.MouseDown[0]) ClearActiveID(); else if (g.ActiveIdSource == ImGuiInputSource_Nav && g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) ClearActiveID(); } if (g.ActiveId != id) return false; switch (data_type) { case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)v; bool r = DragBehaviorT(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; } case ImGuiDataType_U8: { ImU32 v32 = (ImU32)*(ImU8*)v; bool r = DragBehaviorT(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; } case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)v; bool r = DragBehaviorT(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; } case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)v; bool r = DragBehaviorT(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; } case ImGuiDataType_S32: return DragBehaviorT(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); case ImGuiDataType_U32: return DragBehaviorT(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); case ImGuiDataType_S64: return DragBehaviorT(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); case ImGuiDataType_U64: return DragBehaviorT(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); case ImGuiDataType_Float: return DragBehaviorT(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); case ImGuiDataType_Double: return DragBehaviorT(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); case ImGuiDataType_COUNT: break; } IM_ASSERT(0); return false; } bool 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) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; if (power != 1.0f) IM_ASSERT(v_min != NULL && v_max != NULL); // When using a power curve the drag needs to have known bounds ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const float w = GetNextItemWidth(); const ImVec2 label_size = CalcTextSize(label, NULL, true); const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); 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)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, id, &frame_bb)) return false; // Default format string when passing NULL if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) format = PatchFormatStringFloatToInt(format); // Tabbing or CTRL-clicking on Drag turns it into an input box const bool hovered = ItemHoverable(frame_bb, id); bool temp_input_is_active = TempInputTextIsActive(id); bool temp_input_start = false; if (!temp_input_is_active) { const bool focus_requested = FocusableItemRegister(window, id); const bool clicked = (hovered && g.IO.MouseClicked[0]); const bool double_clicked = (hovered && g.IO.MouseDoubleClicked[0]); if (focus_requested || clicked || double_clicked || g.NavActivateId == id || g.NavInputId == id) { SetActiveID(id, window); SetFocusID(id, window); FocusWindow(window); g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); if (focus_requested || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavInputId == id) { temp_input_start = true; FocusableItemUnregister(window); } } } if (temp_input_is_active || temp_input_start) return TempInputTextScalar(frame_bb, id, label, data_type, v, format); // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); RenderNavHighlight(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding); // Drag behavior const bool value_changed = DragBehavior(id, data_type, v, v_speed, v_min, v_max, format, power, ImGuiDragFlags_None); if (value_changed) MarkItemEdited(id); // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. char value_buf[64]; const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, v, format); RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f)); if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); return value_changed; } bool 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) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; bool value_changed = false; BeginGroup(); PushID(label); PushMultiItemsWidths(components, GetNextItemWidth()); size_t type_size = GDataTypeInfo[data_type].Size; for (int i = 0; i < components; i++) { PushID(i); value_changed |= DragScalar("", data_type, v, v_speed, v_min, v_max, format, power); SameLine(0, g.Style.ItemInnerSpacing.x); PopID(); PopItemWidth(); v = (void*)((char*)v + type_size); } PopID(); TextEx(label, FindRenderedTextEnd(label)); EndGroup(); return value_changed; } bool ImGui::DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power); } bool ImGui::DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power); } bool ImGui::DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power); } bool ImGui::DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power); } bool 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) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; PushID(label); BeginGroup(); PushMultiItemsWidths(2, GetNextItemWidth()); 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); PopItemWidth(); SameLine(0, g.Style.ItemInnerSpacing.x); 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); PopItemWidth(); SameLine(0, g.Style.ItemInnerSpacing.x); TextEx(label, FindRenderedTextEnd(label)); EndGroup(); PopID(); return value_changed; } // NB: v_speed is float to allow adjusting the drag speed with more precision bool ImGui::DragInt(const char* label, int* v, float v_speed, int v_min, int v_max, const char* format) { return DragScalar(label, ImGuiDataType_S32, v, v_speed, &v_min, &v_max, format); } bool ImGui::DragInt2(const char* label, int v[2], float v_speed, int v_min, int v_max, const char* format) { return DragScalarN(label, ImGuiDataType_S32, v, 2, v_speed, &v_min, &v_max, format); } bool ImGui::DragInt3(const char* label, int v[3], float v_speed, int v_min, int v_max, const char* format) { return DragScalarN(label, ImGuiDataType_S32, v, 3, v_speed, &v_min, &v_max, format); } bool ImGui::DragInt4(const char* label, int v[4], float v_speed, int v_min, int v_max, const char* format) { return DragScalarN(label, ImGuiDataType_S32, v, 4, v_speed, &v_min, &v_max, format); } bool 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) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; PushID(label); BeginGroup(); PushMultiItemsWidths(2, GetNextItemWidth()); 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); PopItemWidth(); SameLine(0, g.Style.ItemInnerSpacing.x); 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); PopItemWidth(); SameLine(0, g.Style.ItemInnerSpacing.x); TextEx(label, FindRenderedTextEnd(label)); EndGroup(); PopID(); return value_changed; } //------------------------------------------------------------------------- // [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc. //------------------------------------------------------------------------- // - SliderBehaviorT<>() [Internal] // - SliderBehavior() [Internal] // - SliderScalar() // - SliderScalarN() // - SliderFloat() // - SliderFloat2() // - SliderFloat3() // - SliderFloat4() // - SliderAngle() // - SliderInt() // - SliderInt2() // - SliderInt3() // - SliderInt4() // - VSliderScalar() // - VSliderFloat() // - VSliderInt() //------------------------------------------------------------------------- template float ImGui::SliderCalcRatioFromValueT(ImGuiDataType data_type, TYPE v, TYPE v_min, TYPE v_max, float power, float linear_zero_pos) { if (v_min == v_max) return 0.0f; const bool is_power = (power != 1.0f) && (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double); const TYPE v_clamped = (v_min < v_max) ? ImClamp(v, v_min, v_max) : ImClamp(v, v_max, v_min); if (is_power) { if (v_clamped < 0.0f) { const float f = 1.0f - (float)((v_clamped - v_min) / (ImMin((TYPE)0, v_max) - v_min)); return (1.0f - ImPow(f, 1.0f/power)) * linear_zero_pos; } else { const float f = (float)((v_clamped - ImMax((TYPE)0, v_min)) / (v_max - ImMax((TYPE)0, v_min))); return linear_zero_pos + ImPow(f, 1.0f/power) * (1.0f - linear_zero_pos); } } // Linear slider return (float)((FLOATTYPE)(v_clamped - v_min) / (FLOATTYPE)(v_max - v_min)); } // FIXME: Move some of the code into SliderBehavior(). Current responsability is larger than what the equivalent DragBehaviorT<> does, we also do some rendering, etc. template bool 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) { ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); const bool is_power = (power != 1.0f) && is_decimal; const float grab_padding = 2.0f; const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f; float grab_sz = style.GrabMinSize; SIGNEDTYPE v_range = (v_min < v_max ? v_max - v_min : v_min - v_max); if (!is_decimal && v_range >= 0) // v_range < 0 may happen on integer overflows grab_sz = ImMax((float)(slider_sz / (v_range + 1)), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit grab_sz = ImMin(grab_sz, slider_sz); const float slider_usable_sz = slider_sz - grab_sz; const float slider_usable_pos_min = bb.Min[axis] + grab_padding + grab_sz * 0.5f; const float slider_usable_pos_max = bb.Max[axis] - grab_padding - grab_sz * 0.5f; // For power curve sliders that cross over sign boundary we want the curve to be symmetric around 0.0f float linear_zero_pos; // 0.0->1.0f if (is_power && v_min * v_max < 0.0f) { // Different sign const FLOATTYPE linear_dist_min_to_0 = ImPow(v_min >= 0 ? (FLOATTYPE)v_min : -(FLOATTYPE)v_min, (FLOATTYPE)1.0f / power); const FLOATTYPE linear_dist_max_to_0 = ImPow(v_max >= 0 ? (FLOATTYPE)v_max : -(FLOATTYPE)v_max, (FLOATTYPE)1.0f / power); linear_zero_pos = (float)(linear_dist_min_to_0 / (linear_dist_min_to_0 + linear_dist_max_to_0)); } else { // Same sign linear_zero_pos = v_min < 0.0f ? 1.0f : 0.0f; } // Process interacting with the slider bool value_changed = false; if (g.ActiveId == id) { bool set_new_value = false; float clicked_t = 0.0f; if (g.ActiveIdSource == ImGuiInputSource_Mouse) { if (!g.IO.MouseDown[0]) { ClearActiveID(); } else { const float mouse_abs_pos = g.IO.MousePos[axis]; clicked_t = (slider_usable_sz > 0.0f) ? ImClamp((mouse_abs_pos - slider_usable_pos_min) / slider_usable_sz, 0.0f, 1.0f) : 0.0f; if (axis == ImGuiAxis_Y) clicked_t = 1.0f - clicked_t; set_new_value = true; } } else if (g.ActiveIdSource == ImGuiInputSource_Nav) { const ImVec2 delta2 = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 0.0f, 0.0f); float delta = (axis == ImGuiAxis_X) ? delta2.x : -delta2.y; if (g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) { ClearActiveID(); } else if (delta != 0.0f) { clicked_t = SliderCalcRatioFromValueT(data_type, *v, v_min, v_max, power, linear_zero_pos); const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; if ((decimal_precision > 0) || is_power) { delta /= 100.0f; // Gamepad/keyboard tweak speeds in % of slider bounds if (IsNavInputDown(ImGuiNavInput_TweakSlow)) delta /= 10.0f; } else { if ((v_range >= -100.0f && v_range <= 100.0f) || IsNavInputDown(ImGuiNavInput_TweakSlow)) delta = ((delta < 0.0f) ? -1.0f : +1.0f) / (float)v_range; // Gamepad/keyboard tweak speeds in integer steps else delta /= 100.0f; } if (IsNavInputDown(ImGuiNavInput_TweakFast)) delta *= 10.0f; set_new_value = true; 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 set_new_value = false; else clicked_t = ImSaturate(clicked_t + delta); } } if (set_new_value) { TYPE v_new; if (is_power) { // Account for power curve scale on both sides of the zero if (clicked_t < linear_zero_pos) { // Negative: rescale to the negative range before powering float a = 1.0f - (clicked_t / linear_zero_pos); a = ImPow(a, power); v_new = ImLerp(ImMin(v_max, (TYPE)0), v_min, a); } else { // Positive: rescale to the positive range before powering float a; if (ImFabs(linear_zero_pos - 1.0f) > 1.e-6f) a = (clicked_t - linear_zero_pos) / (1.0f - linear_zero_pos); else a = clicked_t; a = ImPow(a, power); v_new = ImLerp(ImMax(v_min, (TYPE)0), v_max, a); } } else { // Linear slider if (is_decimal) { v_new = ImLerp(v_min, v_max, clicked_t); } else { // For integer values we want the clicking position to match the grab box so we round above // This code is carefully tuned to work with large values (e.g. high ranges of U64) while preserving this property.. FLOATTYPE v_new_off_f = (v_max - v_min) * clicked_t; TYPE v_new_off_floor = (TYPE)(v_new_off_f); TYPE v_new_off_round = (TYPE)(v_new_off_f + (FLOATTYPE)0.5); if (!is_decimal && v_new_off_floor < v_new_off_round) v_new = v_min + v_new_off_round; else v_new = v_min + v_new_off_floor; } } // Round to user desired precision based on format string v_new = RoundScalarWithFormatT(format, data_type, v_new); // Apply result if (*v != v_new) { *v = v_new; value_changed = true; } } } if (slider_sz < 1.0f) { *out_grab_bb = ImRect(bb.Min, bb.Min); } else { // Output grab position so it can be displayed by the caller float grab_t = SliderCalcRatioFromValueT(data_type, *v, v_min, v_max, power, linear_zero_pos); if (axis == ImGuiAxis_Y) grab_t = 1.0f - grab_t; const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t); if (axis == ImGuiAxis_X) *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); else *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); } return value_changed; } // For 32-bits and larger types, slider bounds are limited to half the natural type range. // 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. // It would be possible to lift that limitation with some work but it doesn't seem to be worth it for sliders. bool 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) { switch (data_type) { case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)v; bool r = SliderBehaviorT(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; } case ImGuiDataType_U8: { ImU32 v32 = (ImU32)*(ImU8*)v; bool r = SliderBehaviorT(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; } case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)v; bool r = SliderBehaviorT(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; } case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)v; bool r = SliderBehaviorT(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; } case ImGuiDataType_S32: IM_ASSERT(*(const ImS32*)v_min >= IM_S32_MIN/2 && *(const ImS32*)v_max <= IM_S32_MAX/2); return SliderBehaviorT(bb, id, data_type, (ImS32*)v, *(const ImS32*)v_min, *(const ImS32*)v_max, format, power, flags, out_grab_bb); case ImGuiDataType_U32: IM_ASSERT(*(const ImU32*)v_min <= IM_U32_MAX/2); return SliderBehaviorT(bb, id, data_type, (ImU32*)v, *(const ImU32*)v_min, *(const ImU32*)v_max, format, power, flags, out_grab_bb); case ImGuiDataType_S64: IM_ASSERT(*(const ImS64*)v_min >= IM_S64_MIN/2 && *(const ImS64*)v_max <= IM_S64_MAX/2); return SliderBehaviorT(bb, id, data_type, (ImS64*)v, *(const ImS64*)v_min, *(const ImS64*)v_max, format, power, flags, out_grab_bb); case ImGuiDataType_U64: IM_ASSERT(*(const ImU64*)v_min <= IM_U64_MAX/2); return SliderBehaviorT(bb, id, data_type, (ImU64*)v, *(const ImU64*)v_min, *(const ImU64*)v_max, format, power, flags, out_grab_bb); case ImGuiDataType_Float: IM_ASSERT(*(const float*)v_min >= -FLT_MAX/2.0f && *(const float*)v_max <= FLT_MAX/2.0f); return SliderBehaviorT(bb, id, data_type, (float*)v, *(const float*)v_min, *(const float*)v_max, format, power, flags, out_grab_bb); case ImGuiDataType_Double: IM_ASSERT(*(const double*)v_min >= -DBL_MAX/2.0f && *(const double*)v_max <= DBL_MAX/2.0f); return SliderBehaviorT(bb, id, data_type, (double*)v, *(const double*)v_min, *(const double*)v_max, format, power, flags, out_grab_bb); case ImGuiDataType_COUNT: break; } IM_ASSERT(0); return false; } bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* v, const void* v_min, const void* v_max, const char* format, float power) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const float w = GetNextItemWidth(); const ImVec2 label_size = CalcTextSize(label, NULL, true); const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); 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)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, id, &frame_bb)) return false; // Default format string when passing NULL if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) format = PatchFormatStringFloatToInt(format); // Tabbing or CTRL-clicking on Slider turns it into an input box const bool hovered = ItemHoverable(frame_bb, id); bool temp_input_is_active = TempInputTextIsActive(id); bool temp_input_start = false; if (!temp_input_is_active) { const bool focus_requested = FocusableItemRegister(window, id); const bool clicked = (hovered && g.IO.MouseClicked[0]); if (focus_requested || clicked || g.NavActivateId == id || g.NavInputId == id) { SetActiveID(id, window); SetFocusID(id, window); FocusWindow(window); g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); if (focus_requested || (clicked && g.IO.KeyCtrl) || g.NavInputId == id) { temp_input_start = true; FocusableItemUnregister(window); } } } if (temp_input_is_active || temp_input_start) return TempInputTextScalar(frame_bb, id, label, data_type, v, format); // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); RenderNavHighlight(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding); // Slider behavior ImRect grab_bb; const bool value_changed = SliderBehavior(frame_bb, id, data_type, v, v_min, v_max, format, power, ImGuiSliderFlags_None, &grab_bb); if (value_changed) MarkItemEdited(id); // Render grab if (grab_bb.Max.x > grab_bb.Min.x) window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding); // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. char value_buf[64]; const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, v, format); RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f,0.5f)); if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); return value_changed; } // Add multiple sliders on 1 line for compact edition of multiple components bool 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) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; bool value_changed = false; BeginGroup(); PushID(label); PushMultiItemsWidths(components, GetNextItemWidth()); size_t type_size = GDataTypeInfo[data_type].Size; for (int i = 0; i < components; i++) { PushID(i); value_changed |= SliderScalar("", data_type, v, v_min, v_max, format, power); SameLine(0, g.Style.ItemInnerSpacing.x); PopID(); PopItemWidth(); v = (void*)((char*)v + type_size); } PopID(); TextEx(label, FindRenderedTextEnd(label)); EndGroup(); return value_changed; } bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power) { return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power); } bool ImGui::SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power); } bool ImGui::SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power); } bool ImGui::SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power); } bool ImGui::SliderAngle(const char* label, float* v_rad, float v_degrees_min, float v_degrees_max, const char* format) { if (format == NULL) format = "%.0f deg"; float v_deg = (*v_rad) * 360.0f / (2*IM_PI); bool value_changed = SliderFloat(label, &v_deg, v_degrees_min, v_degrees_max, format, 1.0f); *v_rad = v_deg * (2*IM_PI) / 360.0f; return value_changed; } bool ImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const char* format) { return SliderScalar(label, ImGuiDataType_S32, v, &v_min, &v_max, format); } bool ImGui::SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format) { return SliderScalarN(label, ImGuiDataType_S32, v, 2, &v_min, &v_max, format); } bool ImGui::SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format) { return SliderScalarN(label, ImGuiDataType_S32, v, 3, &v_min, &v_max, format); } bool ImGui::SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format) { return SliderScalarN(label, ImGuiDataType_S32, v, 4, &v_min, &v_max, format); } bool 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) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size); 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)); ItemSize(bb, style.FramePadding.y); if (!ItemAdd(frame_bb, id)) return false; // Default format string when passing NULL if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) format = PatchFormatStringFloatToInt(format); const bool hovered = ItemHoverable(frame_bb, id); if ((hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || g.NavInputId == id) { SetActiveID(id, window); SetFocusID(id, window); FocusWindow(window); g.ActiveIdAllowNavDirFlags = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); } // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); RenderNavHighlight(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding); // Slider behavior ImRect grab_bb; const bool value_changed = SliderBehavior(frame_bb, id, data_type, v, v_min, v_max, format, power, ImGuiSliderFlags_Vertical, &grab_bb); if (value_changed) MarkItemEdited(id); // Render grab if (grab_bb.Max.y > grab_bb.Min.y) window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding); // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. // For the vertical slider we allow centered text to overlap the frame padding char value_buf[64]; const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, v, format); 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)); if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); return value_changed; } bool ImGui::VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format, float power) { return VSliderScalar(label, size, ImGuiDataType_Float, v, &v_min, &v_max, format, power); } bool ImGui::VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format) { return VSliderScalar(label, size, ImGuiDataType_S32, v, &v_min, &v_max, format); } //------------------------------------------------------------------------- // [SECTION] Widgets: InputScalar, InputFloat, InputInt, etc. //------------------------------------------------------------------------- // - ImParseFormatFindStart() [Internal] // - ImParseFormatFindEnd() [Internal] // - ImParseFormatTrimDecorations() [Internal] // - ImParseFormatPrecision() [Internal] // - TempInputTextScalar() [Internal] // - InputScalar() // - InputScalarN() // - InputFloat() // - InputFloat2() // - InputFloat3() // - InputFloat4() // - InputInt() // - InputInt2() // - InputInt3() // - InputInt4() // - InputDouble() //------------------------------------------------------------------------- // We don't use strchr() because our strings are usually very short and often start with '%' const char* ImParseFormatFindStart(const char* fmt) { while (char c = fmt[0]) { if (c == '%' && fmt[1] != '%') return fmt; else if (c == '%') fmt++; fmt++; } return fmt; } const char* ImParseFormatFindEnd(const char* fmt) { // Printf/scanf types modifiers: I/L/h/j/l/t/w/z. Other uppercase letters qualify as types aka end of the format. if (fmt[0] != '%') return fmt; const unsigned int ignored_uppercase_mask = (1 << ('I'-'A')) | (1 << ('L'-'A')); const unsigned int ignored_lowercase_mask = (1 << ('h'-'a')) | (1 << ('j'-'a')) | (1 << ('l'-'a')) | (1 << ('t'-'a')) | (1 << ('w'-'a')) | (1 << ('z'-'a')); for (char c; (c = *fmt) != 0; fmt++) { if (c >= 'A' && c <= 'Z' && ((1 << (c - 'A')) & ignored_uppercase_mask) == 0) return fmt + 1; if (c >= 'a' && c <= 'z' && ((1 << (c - 'a')) & ignored_lowercase_mask) == 0) return fmt + 1; } return fmt; } // Extract the format out of a format string with leading or trailing decorations // fmt = "blah blah" -> return fmt // fmt = "%.3f" -> return fmt // fmt = "hello %.3f" -> return fmt + 6 // fmt = "%.3f hello" -> return buf written with "%.3f" const char* ImParseFormatTrimDecorations(const char* fmt, char* buf, size_t buf_size) { const char* fmt_start = ImParseFormatFindStart(fmt); if (fmt_start[0] != '%') return fmt; const char* fmt_end = ImParseFormatFindEnd(fmt_start); if (fmt_end[0] == 0) // If we only have leading decoration, we don't need to copy the data. return fmt_start; ImStrncpy(buf, fmt_start, ImMin((size_t)(fmt_end - fmt_start) + 1, buf_size)); return buf; } // Parse display precision back from the display format string // 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. int ImParseFormatPrecision(const char* fmt, int default_precision) { fmt = ImParseFormatFindStart(fmt); if (fmt[0] != '%') return default_precision; fmt++; while (*fmt >= '0' && *fmt <= '9') fmt++; int precision = INT_MAX; if (*fmt == '.') { fmt = ImAtoi(fmt + 1, &precision); if (precision < 0 || precision > 99) precision = default_precision; } if (*fmt == 'e' || *fmt == 'E') // Maximum precision with scientific notation precision = -1; if ((*fmt == 'g' || *fmt == 'G') && precision == INT_MAX) precision = -1; return (precision == INT_MAX) ? default_precision : precision; } // Create text input in place of another active widget (e.g. used when doing a CTRL+Click on drag/slider widgets) // FIXME: Facilitate using this in variety of other situations. bool ImGui::TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* data_ptr, const char* format) { ImGuiContext& g = *GImGui; // On the first frame, g.TempInputTextId == 0, then on subsequent frames it becomes == id. // We clear ActiveID on the first frame to allow the InputText() taking it back. const bool init = (g.TempInputTextId != id); if (init) ClearActiveID(); char fmt_buf[32]; char data_buf[32]; format = ImParseFormatTrimDecorations(format, fmt_buf, IM_ARRAYSIZE(fmt_buf)); DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, data_ptr, format); ImStrTrimBlanks(data_buf); g.CurrentWindow->DC.CursorPos = bb.Min; ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ((data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) ? ImGuiInputTextFlags_CharsScientific : ImGuiInputTextFlags_CharsDecimal); bool value_changed = InputTextEx(label, NULL, data_buf, IM_ARRAYSIZE(data_buf), bb.GetSize(), flags); if (init) { // First frame we started displaying the InputText widget, we expect it to take the active id. IM_ASSERT(g.ActiveId == id); g.TempInputTextId = g.ActiveId; } if (value_changed) return DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialTextA.Data, data_type, data_ptr, NULL); return false; } bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* data_ptr, const void* step, const void* step_fast, const char* format, ImGuiInputTextFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; ImGuiStyle& style = g.Style; if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; char buf[64]; DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, data_ptr, format); bool value_changed = false; if ((flags & (ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0) flags |= ImGuiInputTextFlags_CharsDecimal; flags |= ImGuiInputTextFlags_AutoSelectAll; if (step != NULL) { const float button_size = GetFrameHeight(); BeginGroup(); // The only purpose of the group here is to allow the caller to query item data e.g. IsItemActive() PushID(label); SetNextItemWidth(ImMax(1.0f, GetNextItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2)); if (InputText("", buf, IM_ARRAYSIZE(buf), flags)) // PushId(label) + "" gives us the expected ID from outside point of view value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, data_ptr, format); // Step buttons const ImVec2 backup_frame_padding = style.FramePadding; style.FramePadding.x = style.FramePadding.y; ImGuiButtonFlags button_flags = ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups; if (flags & ImGuiInputTextFlags_ReadOnly) button_flags |= ImGuiButtonFlags_Disabled; SameLine(0, style.ItemInnerSpacing.x); if (ButtonEx("-", ImVec2(button_size, button_size), button_flags)) { DataTypeApplyOp(data_type, '-', data_ptr, data_ptr, g.IO.KeyCtrl && step_fast ? step_fast : step); value_changed = true; } SameLine(0, style.ItemInnerSpacing.x); if (ButtonEx("+", ImVec2(button_size, button_size), button_flags)) { DataTypeApplyOp(data_type, '+', data_ptr, data_ptr, g.IO.KeyCtrl && step_fast ? step_fast : step); value_changed = true; } SameLine(0, style.ItemInnerSpacing.x); TextEx(label, FindRenderedTextEnd(label)); style.FramePadding = backup_frame_padding; PopID(); EndGroup(); } else { if (InputText(label, buf, IM_ARRAYSIZE(buf), flags)) value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, data_ptr, format); } return value_changed; } bool ImGui::InputScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* step, const void* step_fast, const char* format, ImGuiInputTextFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; bool value_changed = false; BeginGroup(); PushID(label); PushMultiItemsWidths(components, GetNextItemWidth()); size_t type_size = GDataTypeInfo[data_type].Size; for (int i = 0; i < components; i++) { PushID(i); value_changed |= InputScalar("", data_type, v, step, step_fast, format, flags); SameLine(0, g.Style.ItemInnerSpacing.x); PopID(); PopItemWidth(); v = (void*)((char*)v + type_size); } PopID(); TextEx(label, FindRenderedTextEnd(label)); EndGroup(); return value_changed; } bool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, const char* format, ImGuiInputTextFlags flags) { flags |= ImGuiInputTextFlags_CharsScientific; return InputScalar(label, ImGuiDataType_Float, (void*)v, (void*)(step>0.0f ? &step : NULL), (void*)(step_fast>0.0f ? &step_fast : NULL), format, flags); } bool ImGui::InputFloat2(const char* label, float v[2], const char* format, ImGuiInputTextFlags flags) { return InputScalarN(label, ImGuiDataType_Float, v, 2, NULL, NULL, format, flags); } bool ImGui::InputFloat3(const char* label, float v[3], const char* format, ImGuiInputTextFlags flags) { return InputScalarN(label, ImGuiDataType_Float, v, 3, NULL, NULL, format, flags); } bool ImGui::InputFloat4(const char* label, float v[4], const char* format, ImGuiInputTextFlags flags) { return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, flags); } // Prefer using "const char* format" directly, which is more flexible and consistent with other API. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS bool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags flags) { char format[16] = "%f"; if (decimal_precision >= 0) ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); return InputFloat(label, v, step, step_fast, format, flags); } bool ImGui::InputFloat2(const char* label, float v[2], int decimal_precision, ImGuiInputTextFlags flags) { char format[16] = "%f"; if (decimal_precision >= 0) ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); return InputScalarN(label, ImGuiDataType_Float, v, 2, NULL, NULL, format, flags); } bool ImGui::InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags flags) { char format[16] = "%f"; if (decimal_precision >= 0) ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); return InputScalarN(label, ImGuiDataType_Float, v, 3, NULL, NULL, format, flags); } bool ImGui::InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags flags) { char format[16] = "%f"; if (decimal_precision >= 0) ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, flags); } #endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS bool ImGui::InputInt(const char* label, int* v, int step, int step_fast, ImGuiInputTextFlags flags) { // 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. const char* format = (flags & ImGuiInputTextFlags_CharsHexadecimal) ? "%08X" : "%d"; return InputScalar(label, ImGuiDataType_S32, (void*)v, (void*)(step>0 ? &step : NULL), (void*)(step_fast>0 ? &step_fast : NULL), format, flags); } bool ImGui::InputInt2(const char* label, int v[2], ImGuiInputTextFlags flags) { return InputScalarN(label, ImGuiDataType_S32, v, 2, NULL, NULL, "%d", flags); } bool ImGui::InputInt3(const char* label, int v[3], ImGuiInputTextFlags flags) { return InputScalarN(label, ImGuiDataType_S32, v, 3, NULL, NULL, "%d", flags); } bool ImGui::InputInt4(const char* label, int v[4], ImGuiInputTextFlags flags) { return InputScalarN(label, ImGuiDataType_S32, v, 4, NULL, NULL, "%d", flags); } bool ImGui::InputDouble(const char* label, double* v, double step, double step_fast, const char* format, ImGuiInputTextFlags flags) { flags |= ImGuiInputTextFlags_CharsScientific; return InputScalar(label, ImGuiDataType_Double, (void*)v, (void*)(step>0.0 ? &step : NULL), (void*)(step_fast>0.0 ? &step_fast : NULL), format, flags); } //------------------------------------------------------------------------- // [SECTION] Widgets: InputText, InputTextMultiline, InputTextWithHint //------------------------------------------------------------------------- // - InputText() // - InputTextWithHint() // - InputTextMultiline() // - InputTextEx() [Internal] //------------------------------------------------------------------------- bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline() return InputTextEx(label, NULL, buf, (int)buf_size, ImVec2(0,0), flags, callback, user_data); } bool ImGui::InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { return InputTextEx(label, NULL, buf, (int)buf_size, size, flags | ImGuiInputTextFlags_Multiline, callback, user_data); } bool ImGui::InputTextWithHint(const char* label, const char* hint, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline() return InputTextEx(label, hint, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data); } static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end) { int line_count = 0; const char* s = text_begin; while (char c = *s++) // We are only matching for \n so we can ignore UTF-8 decoding if (c == '\n') line_count++; s--; if (s[0] != '\n' && s[0] != '\r') line_count++; *out_text_end = s; return line_count; } static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining, ImVec2* out_offset, bool stop_on_new_line) { ImGuiContext& g = *GImGui; ImFont* font = g.Font; const float line_height = g.FontSize; const float scale = line_height / font->FontSize; ImVec2 text_size = ImVec2(0,0); float line_width = 0.0f; const ImWchar* s = text_begin; while (s < text_end) { unsigned int c = (unsigned int)(*s++); if (c == '\n') { text_size.x = ImMax(text_size.x, line_width); text_size.y += line_height; line_width = 0.0f; if (stop_on_new_line) break; continue; } if (c == '\r') continue; const float char_width = font->GetCharAdvance((ImWchar)c) * scale; line_width += char_width; } if (text_size.x < line_width) text_size.x = line_width; if (out_offset) *out_offset = ImVec2(line_width, text_size.y + line_height); // offset allow for the possibility of sitting after a trailing \n if (line_width > 0 || text_size.y == 0.0f) // whereas size.y will ignore the trailing \n text_size.y += line_height; if (remaining) *remaining = s; return text_size; } // 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) namespace ImStb { static int STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING* obj) { return obj->CurLenW; } static ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING* obj, int idx) { return obj->TextW[idx]; } static 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); } static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x10000 ? 0 : key; } static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx) { const ImWchar* text = obj->TextW.Data; const ImWchar* text_remaining = NULL; const ImVec2 size = InputTextCalcTextSizeW(text + line_start_idx, text + obj->CurLenW, &text_remaining, NULL, true); r->x0 = 0.0f; r->x1 = size.x; r->baseline_y_delta = size.y; r->ymin = 0.0f; r->ymax = size.y; r->num_chars = (int)(text_remaining - (text + line_start_idx)); } static bool is_separator(unsigned int c) { return ImCharIsBlankW(c) || c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; } static 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; } static 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; } #ifdef __APPLE__ // FIXME: Move setting to IO structure static 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; } static 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; } #else static 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; } #endif #define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h #define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_IMPL static void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int pos, int n) { ImWchar* dst = obj->TextW.Data + pos; // We maintain our buffer length in both UTF-8 and wchar formats obj->CurLenA -= ImTextCountUtf8BytesFromStr(dst, dst + n); obj->CurLenW -= n; // Offset remaining text (FIXME-OPT: Use memmove) const ImWchar* src = obj->TextW.Data + pos + n; while (ImWchar c = *src++) *dst++ = c; *dst = '\0'; } static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const ImWchar* new_text, int new_text_len) { const bool is_resizable = (obj->UserFlags & ImGuiInputTextFlags_CallbackResize) != 0; const int text_len = obj->CurLenW; IM_ASSERT(pos <= text_len); const int new_text_len_utf8 = ImTextCountUtf8BytesFromStr(new_text, new_text + new_text_len); if (!is_resizable && (new_text_len_utf8 + obj->CurLenA + 1 > obj->BufCapacityA)) return false; // Grow internal buffer if needed if (new_text_len + text_len + 1 > obj->TextW.Size) { if (!is_resizable) return false; IM_ASSERT(text_len < obj->TextW.Size); obj->TextW.resize(text_len + ImClamp(new_text_len * 4, 32, ImMax(256, new_text_len)) + 1); } ImWchar* text = obj->TextW.Data; if (pos != text_len) memmove(text + pos + new_text_len, text + pos, (size_t)(text_len - pos) * sizeof(ImWchar)); memcpy(text + pos, new_text, (size_t)new_text_len * sizeof(ImWchar)); obj->CurLenW += new_text_len; obj->CurLenA += new_text_len_utf8; obj->TextW[obj->CurLenW] = '\0'; return true; } // 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) #define STB_TEXTEDIT_K_LEFT 0x10000 // keyboard input to move cursor left #define STB_TEXTEDIT_K_RIGHT 0x10001 // keyboard input to move cursor right #define STB_TEXTEDIT_K_UP 0x10002 // keyboard input to move cursor up #define STB_TEXTEDIT_K_DOWN 0x10003 // keyboard input to move cursor down #define STB_TEXTEDIT_K_LINESTART 0x10004 // keyboard input to move cursor to start of line #define STB_TEXTEDIT_K_LINEEND 0x10005 // keyboard input to move cursor to end of line #define STB_TEXTEDIT_K_TEXTSTART 0x10006 // keyboard input to move cursor to start of text #define STB_TEXTEDIT_K_TEXTEND 0x10007 // keyboard input to move cursor to end of text #define STB_TEXTEDIT_K_DELETE 0x10008 // keyboard input to delete selection or character under cursor #define STB_TEXTEDIT_K_BACKSPACE 0x10009 // keyboard input to delete selection or character left of cursor #define STB_TEXTEDIT_K_UNDO 0x1000A // keyboard input to perform undo #define STB_TEXTEDIT_K_REDO 0x1000B // keyboard input to perform redo #define STB_TEXTEDIT_K_WORDLEFT 0x1000C // keyboard input to move cursor left one word #define STB_TEXTEDIT_K_WORDRIGHT 0x1000D // keyboard input to move cursor right one word #define STB_TEXTEDIT_K_SHIFT 0x20000 #define STB_TEXTEDIT_IMPLEMENTATION #include "imstb_textedit.hpp" } void ImGuiInputTextState::OnKeyPressed(int key) { stb_textedit_key(this, &Stb, key); CursorFollow = true; CursorAnimReset(); } ImGuiInputTextCallbackData::ImGuiInputTextCallbackData() { memset(this, 0, sizeof(*this)); } // Public API to manipulate UTF-8 text // We expose UTF-8 to the user (unlike the STB_TEXTEDIT_* functions which are manipulating wchar) // FIXME: The existence of this rarely exercised code path is a bit of a nuisance. void ImGuiInputTextCallbackData::DeleteChars(int pos, int bytes_count) { IM_ASSERT(pos + bytes_count <= BufTextLen); char* dst = Buf + pos; const char* src = Buf + pos + bytes_count; while (char c = *src++) *dst++ = c; *dst = '\0'; if (CursorPos + bytes_count >= pos) CursorPos -= bytes_count; else if (CursorPos >= pos) CursorPos = pos; SelectionStart = SelectionEnd = CursorPos; BufDirty = true; BufTextLen -= bytes_count; } void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, const char* new_text_end) { const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0; const int new_text_len = new_text_end ? (int)(new_text_end - new_text) : (int)strlen(new_text); if (new_text_len + BufTextLen >= BufSize) { if (!is_resizable) return; // Contrary to STB_TEXTEDIT_INSERTCHARS() this is working in the UTF8 buffer, hence the midly similar code (until we remove the U16 buffer alltogether!) ImGuiContext& g = *GImGui; ImGuiInputTextState* edit_state = &g.InputTextState; IM_ASSERT(edit_state->ID != 0 && g.ActiveId == edit_state->ID); IM_ASSERT(Buf == edit_state->TextA.Data); int new_buf_size = BufTextLen + ImClamp(new_text_len * 4, 32, ImMax(256, new_text_len)) + 1; edit_state->TextA.reserve(new_buf_size + 1); Buf = edit_state->TextA.Data; BufSize = edit_state->BufCapacityA = new_buf_size; } if (BufTextLen != pos) memmove(Buf + pos + new_text_len, Buf + pos, (size_t)(BufTextLen - pos)); memcpy(Buf + pos, new_text, (size_t)new_text_len * sizeof(char)); Buf[BufTextLen + new_text_len] = '\0'; if (CursorPos >= pos) CursorPos += new_text_len; SelectionStart = SelectionEnd = CursorPos; BufDirty = true; BufTextLen += new_text_len; } // Return false to discard a character. static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { unsigned int c = *p_char; // Filter non-printable (NB: isprint is unreliable! see #2467) if (c < 0x20) { bool pass = false; pass |= (c == '\n' && (flags & ImGuiInputTextFlags_Multiline)); pass |= (c == '\t' && (flags & ImGuiInputTextFlags_AllowTabInput)); if (!pass) return false; } // Filter private Unicode range. GLFW on OSX seems to send private characters for special keys like arrow keys (FIXME) if (c >= 0xE000 && c <= 0xF8FF) return false; // Generic named filters if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific)) { if (flags & ImGuiInputTextFlags_CharsDecimal) if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/')) return false; if (flags & ImGuiInputTextFlags_CharsScientific) if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/') && (c != 'e') && (c != 'E')) return false; if (flags & ImGuiInputTextFlags_CharsHexadecimal) if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) return false; if (flags & ImGuiInputTextFlags_CharsUppercase) if (c >= 'a' && c <= 'z') *p_char = (c += (unsigned int)('A'-'a')); if (flags & ImGuiInputTextFlags_CharsNoBlank) if (ImCharIsBlankW(c)) return false; } // Custom callback filter if (flags & ImGuiInputTextFlags_CallbackCharFilter) { ImGuiInputTextCallbackData callback_data; memset(&callback_data, 0, sizeof(ImGuiInputTextCallbackData)); callback_data.EventFlag = ImGuiInputTextFlags_CallbackCharFilter; callback_data.EventChar = (ImWchar)c; callback_data.Flags = flags; callback_data.UserData = user_data; if (callback(&callback_data) != 0) return false; *p_char = callback_data.EventChar; if (!callback_data.EventChar) return false; } return true; } // Edit a string of text // - buf_size account for the zero-terminator, so a buf_size of 6 can hold "Hello" but not "Hello!". // This is so we can easily call InputText() on static arrays using ARRAYSIZE() and to match // Note that in std::string world, capacity() would omit 1 byte used by the zero-terminator. // - 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. // - If you want to use ImGui::InputText() with std::string, see misc/cpp/imgui_stdlib.h // (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 // 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) bool 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) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline))); // Can't use both together (they both use up/down keys) IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key) ImGuiContext& g = *GImGui; ImGuiIO& io = g.IO; const ImGuiStyle& style = g.Style; const bool RENDER_SELECTION_WHEN_INACTIVE = false; const bool is_multiline = (flags & ImGuiInputTextFlags_Multiline) != 0; const bool is_readonly = (flags & ImGuiInputTextFlags_ReadOnly) != 0; const bool is_password = (flags & ImGuiInputTextFlags_Password) != 0; const bool is_undoable = (flags & ImGuiInputTextFlags_NoUndoRedo) == 0; const bool is_resizable = (flags & ImGuiInputTextFlags_CallbackResize) != 0; if (is_resizable) IM_ASSERT(callback != NULL); // Must provide a callback if you set the ImGuiInputTextFlags_CallbackResize flag! if (is_multiline) // Open group before calling GetID() because groups tracks id created within their scope, BeginGroup(); const ImGuiID id = window->GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); 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 const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size); 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)); ImGuiWindow* draw_window = window; if (is_multiline) { if (!ItemAdd(total_bb, id, &frame_bb)) { ItemSize(total_bb, style.FramePadding.y); EndGroup(); return false; } if (!BeginChildFrame(id, frame_bb.GetSize())) { EndChildFrame(); EndGroup(); return false; } draw_window = GetCurrentWindow(); draw_window->DC.NavLayerActiveMaskNext |= draw_window->DC.NavLayerCurrentMask; // This is to ensure that EndChild() will display a navigation highlight size.x -= draw_window->ScrollbarSizes.x; } else { ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, id, &frame_bb)) return false; } const bool hovered = ItemHoverable(frame_bb, id); if (hovered) g.MouseCursor = ImGuiMouseCursor_TextInput; // NB: we are only allowed to access 'edit_state' if we are the active widget. ImGuiInputTextState* state = NULL; if (g.InputTextState.ID == id) state = &g.InputTextState; const bool focus_requested = FocusableItemRegister(window, id); const bool focus_requested_by_code = focus_requested && (g.FocusRequestCurrWindow == window && g.FocusRequestCurrCounterAll == window->DC.FocusCounterAll); const bool focus_requested_by_tab = focus_requested && !focus_requested_by_code; const bool user_clicked = hovered && io.MouseClicked[0]; const bool user_nav_input_start = (g.ActiveId != id) && ((g.NavInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_NavKeyboard)); const bool user_scroll_finish = is_multiline && state != NULL && g.ActiveId == 0 && g.ActiveIdPreviousFrame == GetScrollbarID(draw_window, ImGuiAxis_Y); const bool user_scroll_active = is_multiline && state != NULL && g.ActiveId == GetScrollbarID(draw_window, ImGuiAxis_Y); bool clear_active_id = false; bool select_all = (g.ActiveId != id) && ((flags & ImGuiInputTextFlags_AutoSelectAll) != 0 || user_nav_input_start) && (!is_multiline); const bool init_make_active = (focus_requested || user_clicked || user_scroll_finish || user_nav_input_start); const bool init_state = (init_make_active || user_scroll_active); if (init_state && g.ActiveId != id) { // Access state even if we don't own it yet. state = &g.InputTextState; state->CursorAnimReset(); // Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar) // From the moment we focused we are ignoring the content of 'buf' (unless we are in read-only mode) const int buf_len = (int)strlen(buf); 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. memcpy(state->InitialTextA.Data, buf, buf_len + 1); // Start edition const char* buf_end = NULL; 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. state->TextA.resize(0); state->TextAIsValid = false; // TextA is not valid yet (we will display buf until then) state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, buf_size, buf, NULL, &buf_end); 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. // Preserve cursor position and undo/redo stack if we come back to same widget // 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. const bool recycle_state = (state->ID == id); if (recycle_state) { // Recycle existing cursor/selection/undo stack but clamp position // Note a single mouse click will override the cursor/position immediately by calling stb_textedit_click handler. state->CursorClamp(); } else { state->ID = id; state->ScrollX = 0.0f; stb_textedit_initialize_state(&state->Stb, !is_multiline); if (!is_multiline && focus_requested_by_code) select_all = true; } if (flags & ImGuiInputTextFlags_AlwaysInsertMode) state->Stb.insert_mode = 1; if (!is_multiline && (focus_requested_by_tab || (user_clicked && io.KeyCtrl))) select_all = true; } if (g.ActiveId != id && init_make_active) { IM_ASSERT(state && state->ID == id); SetActiveID(id, window); SetFocusID(id, window); FocusWindow(window); IM_ASSERT(ImGuiNavInput_COUNT < 32); g.ActiveIdBlockNavInputFlags = (1 << ImGuiNavInput_Cancel); if (flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_AllowTabInput)) // Disable keyboard tabbing out as we will use the \t character. g.ActiveIdBlockNavInputFlags |= (1 << ImGuiNavInput_KeyTab_); if (!is_multiline && !(flags & ImGuiInputTextFlags_CallbackHistory)) g.ActiveIdAllowNavDirFlags = ((1 << ImGuiDir_Up) | (1 << ImGuiDir_Down)); } // 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) if (g.ActiveId == id && state == NULL) ClearActiveID(); // Release focus when we click outside if (g.ActiveId == id && io.MouseClicked[0] && !init_state && !init_make_active) //-V560 clear_active_id = true; // Lock the decision of whether we are going to take the path displaying the cursor or selection const bool render_cursor = (g.ActiveId == id) || (state && user_scroll_active); bool render_selection = state && state->HasSelection() && (RENDER_SELECTION_WHEN_INACTIVE || render_cursor); bool value_changed = false; bool enter_pressed = false; // When read-only we always use the live data passed to the function // FIXME-OPT: Because our selection/cursor code currently needs the wide text we need to convert it when active, which is not ideal :( if (is_readonly && state != NULL && (render_cursor || render_selection)) { const char* buf_end = NULL; state->TextW.resize(buf_size + 1); state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, state->TextW.Size, buf, NULL, &buf_end); state->CurLenA = (int)(buf_end - buf); state->CursorClamp(); render_selection &= state->HasSelection(); } // Select the buffer to render. const bool buf_display_from_state = (render_cursor || render_selection || g.ActiveId == id) && !is_readonly && state && state->TextAIsValid; const bool is_displaying_hint = (hint != NULL && (buf_display_from_state ? state->TextA.Data : buf)[0] == 0); // Password pushes a temporary font with only a fallback glyph if (is_password && !is_displaying_hint) { const ImFontGlyph* glyph = g.Font->FindGlyph('*'); ImFont* password_font = &g.InputTextPasswordFont; password_font->FontSize = g.Font->FontSize; password_font->Scale = g.Font->Scale; password_font->DisplayOffset = g.Font->DisplayOffset; password_font->Ascent = g.Font->Ascent; password_font->Descent = g.Font->Descent; password_font->ContainerAtlas = g.Font->ContainerAtlas; password_font->FallbackGlyph = glyph; password_font->FallbackAdvanceX = glyph->AdvanceX; IM_ASSERT(password_font->Glyphs.empty() && password_font->IndexAdvanceX.empty() && password_font->IndexLookup.empty()); PushFont(password_font); } // Process mouse inputs and character inputs int backup_current_text_length = 0; if (g.ActiveId == id) { IM_ASSERT(state != NULL); backup_current_text_length = state->CurLenA; state->BufCapacityA = buf_size; state->UserFlags = flags; state->UserCallback = callback; state->UserCallbackData = callback_user_data; // Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget. // Down the line we should have a cleaner library-wide concept of Selected vs Active. g.ActiveIdAllowOverlap = !io.MouseDown[0]; g.WantTextInputNextFrame = 1; // Edit in progress const float mouse_x = (io.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + state->ScrollX; const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y - style.FramePadding.y) : (g.FontSize*0.5f)); const bool is_osx = io.ConfigMacOSXBehaviors; if (select_all || (hovered && !is_osx && io.MouseDoubleClicked[0])) { state->SelectAll(); state->SelectedAllMouseLock = true; } else if (hovered && is_osx && io.MouseDoubleClicked[0]) { // Double-click select a word only, OS X style (by simulating keystrokes) state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT); state->OnKeyPressed(STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT); } else if (io.MouseClicked[0] && !state->SelectedAllMouseLock) { if (hovered) { stb_textedit_click(state, &state->Stb, mouse_x, mouse_y); state->CursorAnimReset(); } } else if (io.MouseDown[0] && !state->SelectedAllMouseLock && (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f)) { stb_textedit_drag(state, &state->Stb, mouse_x, mouse_y); state->CursorAnimReset(); state->CursorFollow = true; } if (state->SelectedAllMouseLock && !io.MouseDown[0]) state->SelectedAllMouseLock = false; // It is ill-defined whether the back-end needs to send a \t character when pressing the TAB keys. // Win32 and GLFW naturally do it but not SDL. const bool ignore_char_inputs = (io.KeyCtrl && !io.KeyAlt) || (is_osx && io.KeySuper); if ((flags & ImGuiInputTextFlags_AllowTabInput) && IsKeyPressedMap(ImGuiKey_Tab) && !ignore_char_inputs && !io.KeyShift && !is_readonly) if (!io.InputQueueCharacters.contains('\t')) { unsigned int c = '\t'; // Insert TAB if (InputTextFilterCharacter(&c, flags, callback, callback_user_data)) state->OnKeyPressed((int)c); } // Process regular text input (before we check for Return because using some IME will effectively send a Return?) // 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. if (io.InputQueueCharacters.Size > 0) { if (!ignore_char_inputs && !is_readonly && !user_nav_input_start) for (int n = 0; n < io.InputQueueCharacters.Size; n++) { // Insert character if they pass filtering unsigned int c = (unsigned int)io.InputQueueCharacters[n]; if (c == '\t' && io.KeyShift) continue; if (InputTextFilterCharacter(&c, flags, callback, callback_user_data)) state->OnKeyPressed((int)c); } // Consume characters io.InputQueueCharacters.resize(0); } } // Process other shortcuts/key-presses bool cancel_edit = false; if (g.ActiveId == id && !g.ActiveIdIsJustActivated && !clear_active_id) { IM_ASSERT(state != NULL); const int k_mask = (io.KeyShift ? STB_TEXTEDIT_K_SHIFT : 0); const bool is_osx = io.ConfigMacOSXBehaviors; 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 const bool is_osx_shift_shortcut = is_osx && io.KeySuper && io.KeyShift && !io.KeyCtrl && !io.KeyAlt; const bool is_wordmove_key_down = is_osx ? io.KeyAlt : io.KeyCtrl; // OS X style: Text editing cursor movement using Alt instead of Ctrl 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 const bool is_ctrl_key_only = io.KeyCtrl && !io.KeyShift && !io.KeyAlt && !io.KeySuper; const bool is_shift_key_only = io.KeyShift && !io.KeyCtrl && !io.KeyAlt && !io.KeySuper; 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()); const bool is_copy = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_C)) || (is_ctrl_key_only && IsKeyPressedMap(ImGuiKey_Insert))) && !is_password && (!is_multiline || state->HasSelection()); const bool is_paste = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_V)) || (is_shift_key_only && IsKeyPressedMap(ImGuiKey_Insert))) && !is_readonly; const bool is_undo = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Z)) && !is_readonly && is_undoable); const bool is_redo = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Y)) || (is_osx_shift_shortcut && IsKeyPressedMap(ImGuiKey_Z))) && !is_readonly && is_undoable; 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); } 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); } 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); } 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); } else if (IsKeyPressedMap(ImGuiKey_Home)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); } else if (IsKeyPressedMap(ImGuiKey_End)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); } else if (IsKeyPressedMap(ImGuiKey_Delete) && !is_readonly) { state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); } else if (IsKeyPressedMap(ImGuiKey_Backspace) && !is_readonly) { if (!state->HasSelection()) { if (is_wordmove_key_down) state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT|STB_TEXTEDIT_K_SHIFT); else if (is_osx && io.KeySuper && !io.KeyAlt && !io.KeyCtrl) state->OnKeyPressed(STB_TEXTEDIT_K_LINESTART|STB_TEXTEDIT_K_SHIFT); } state->OnKeyPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask); } else if (IsKeyPressedMap(ImGuiKey_Enter)) { bool ctrl_enter_for_new_line = (flags & ImGuiInputTextFlags_CtrlEnterForNewLine) != 0; if (!is_multiline || (ctrl_enter_for_new_line && !io.KeyCtrl) || (!ctrl_enter_for_new_line && io.KeyCtrl)) { enter_pressed = clear_active_id = true; } else if (!is_readonly) { unsigned int c = '\n'; // Insert new line if (InputTextFilterCharacter(&c, flags, callback, callback_user_data)) state->OnKeyPressed((int)c); } } else if (IsKeyPressedMap(ImGuiKey_Escape)) { clear_active_id = cancel_edit = true; } else if (is_undo || is_redo) { state->OnKeyPressed(is_undo ? STB_TEXTEDIT_K_UNDO : STB_TEXTEDIT_K_REDO); state->ClearSelection(); } else if (is_shortcut_key && IsKeyPressedMap(ImGuiKey_A)) { state->SelectAll(); state->CursorFollow = true; } else if (is_cut || is_copy) { // Cut, Copy if (io.SetClipboardTextFn) { const int ib = state->HasSelection() ? ImMin(state->Stb.select_start, state->Stb.select_end) : 0; const int ie = state->HasSelection() ? ImMax(state->Stb.select_start, state->Stb.select_end) : state->CurLenW; const int clipboard_data_len = ImTextCountUtf8BytesFromStr(state->TextW.Data + ib, state->TextW.Data + ie) + 1; char* clipboard_data = (char*)IM_ALLOC(clipboard_data_len * sizeof(char)); ImTextStrToUtf8(clipboard_data, clipboard_data_len, state->TextW.Data + ib, state->TextW.Data + ie); SetClipboardText(clipboard_data); MemFree(clipboard_data); } if (is_cut) { if (!state->HasSelection()) state->SelectAll(); state->CursorFollow = true; stb_textedit_cut(state, &state->Stb); } } else if (is_paste) { if (const char* clipboard = GetClipboardText()) { // Filter pasted buffer const int clipboard_len = (int)strlen(clipboard); ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len+1) * sizeof(ImWchar)); int clipboard_filtered_len = 0; for (const char* s = clipboard; *s; ) { unsigned int c; s += ImTextCharFromUtf8(&c, s, NULL); if (c == 0) break; if (c >= 0x10000 || !InputTextFilterCharacter(&c, flags, callback, callback_user_data)) continue; clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c; } clipboard_filtered[clipboard_filtered_len] = 0; if (clipboard_filtered_len > 0) // If everything was filtered, ignore the pasting operation { stb_textedit_paste(state, &state->Stb, clipboard_filtered, clipboard_filtered_len); state->CursorFollow = true; } MemFree(clipboard_filtered); } } // Update render selection flag after events have been handled, so selection highlight can be displayed during the same frame. render_selection |= state->HasSelection() && (RENDER_SELECTION_WHEN_INACTIVE || render_cursor); } // Process callbacks and apply result back to user's buffer. if (g.ActiveId == id) { IM_ASSERT(state != NULL); const char* apply_new_text = NULL; int apply_new_text_length = 0; if (cancel_edit) { // Restore initial value. Only return true if restoring to the initial value changes the current buffer contents. if (!is_readonly && strcmp(buf, state->InitialTextA.Data) != 0) { apply_new_text = state->InitialTextA.Data; apply_new_text_length = state->InitialTextA.Size - 1; } } // 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. // 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. bool apply_edit_back_to_user_buffer = !cancel_edit || (enter_pressed && (flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0); if (apply_edit_back_to_user_buffer) { // Apply new value immediately - copy modified buffer back // Note that as soon as the input box is active, the in-widget value gets priority over any underlying modification of the input buffer // FIXME: We actually always render 'buf' when calling DrawList->AddText, making the comment above incorrect. // FIXME-OPT: CPU waste to do this every time the widget is active, should mark dirty state from the stb_textedit callbacks. if (!is_readonly) { state->TextAIsValid = true; state->TextA.resize(state->TextW.Size * 4 + 1); ImTextStrToUtf8(state->TextA.Data, state->TextA.Size, state->TextW.Data, NULL); } // User callback if ((flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory | ImGuiInputTextFlags_CallbackAlways)) != 0) { IM_ASSERT(callback != NULL); // The reason we specify the usage semantic (Completion/History) is that Completion needs to disable keyboard TABBING at the moment. ImGuiInputTextFlags event_flag = 0; ImGuiKey event_key = ImGuiKey_COUNT; if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && IsKeyPressedMap(ImGuiKey_Tab)) { event_flag = ImGuiInputTextFlags_CallbackCompletion; event_key = ImGuiKey_Tab; } else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_UpArrow)) { event_flag = ImGuiInputTextFlags_CallbackHistory; event_key = ImGuiKey_UpArrow; } else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_DownArrow)) { event_flag = ImGuiInputTextFlags_CallbackHistory; event_key = ImGuiKey_DownArrow; } else if (flags & ImGuiInputTextFlags_CallbackAlways) event_flag = ImGuiInputTextFlags_CallbackAlways; if (event_flag) { ImGuiInputTextCallbackData callback_data; memset(&callback_data, 0, sizeof(ImGuiInputTextCallbackData)); callback_data.EventFlag = event_flag; callback_data.Flags = flags; callback_data.UserData = callback_user_data; callback_data.EventKey = event_key; callback_data.Buf = state->TextA.Data; callback_data.BufTextLen = state->CurLenA; callback_data.BufSize = state->BufCapacityA; callback_data.BufDirty = false; // 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) ImWchar* text = state->TextW.Data; const int utf8_cursor_pos = callback_data.CursorPos = ImTextCountUtf8BytesFromStr(text, text + state->Stb.cursor); const int utf8_selection_start = callback_data.SelectionStart = ImTextCountUtf8BytesFromStr(text, text + state->Stb.select_start); const int utf8_selection_end = callback_data.SelectionEnd = ImTextCountUtf8BytesFromStr(text, text + state->Stb.select_end); // Call user code callback(&callback_data); // Read back what user may have modified IM_ASSERT(callback_data.Buf == state->TextA.Data); // Invalid to modify those fields IM_ASSERT(callback_data.BufSize == state->BufCapacityA); IM_ASSERT(callback_data.Flags == flags); if (callback_data.CursorPos != utf8_cursor_pos) { state->Stb.cursor = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.CursorPos); state->CursorFollow = true; } if (callback_data.SelectionStart != utf8_selection_start) { state->Stb.select_start = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionStart); } if (callback_data.SelectionEnd != utf8_selection_end) { state->Stb.select_end = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd); } if (callback_data.BufDirty) { IM_ASSERT(callback_data.BufTextLen == (int)strlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text! if (callback_data.BufTextLen > backup_current_text_length && is_resizable) state->TextW.resize(state->TextW.Size + (callback_data.BufTextLen - backup_current_text_length)); state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, state->TextW.Size, callback_data.Buf, NULL); state->CurLenA = callback_data.BufTextLen; // Assume correct length and valid UTF-8 from user, saves us an extra strlen() state->CursorAnimReset(); } } } // Will copy result string if modified if (!is_readonly && strcmp(state->TextA.Data, buf) != 0) { apply_new_text = state->TextA.Data; apply_new_text_length = state->CurLenA; } } // Copy result to user buffer if (apply_new_text) { IM_ASSERT(apply_new_text_length >= 0); if (backup_current_text_length != apply_new_text_length && is_resizable) { ImGuiInputTextCallbackData callback_data; callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize; callback_data.Flags = flags; callback_data.Buf = buf; callback_data.BufTextLen = apply_new_text_length; callback_data.BufSize = ImMax(buf_size, apply_new_text_length + 1); callback_data.UserData = callback_user_data; callback(&callback_data); buf = callback_data.Buf; buf_size = callback_data.BufSize; apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1); IM_ASSERT(apply_new_text_length <= buf_size); } // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size. ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size)); value_changed = true; } // Clear temporary user storage state->UserFlags = 0; state->UserCallback = NULL; state->UserCallbackData = NULL; } // Release active ID at the end of the function (so e.g. pressing Return still does a final application of the value) if (clear_active_id && g.ActiveId == id) ClearActiveID(); // Render frame if (!is_multiline) { RenderNavHighlight(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); } 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 ImVec2 draw_pos = is_multiline ? draw_window->DC.CursorPos : frame_bb.Min + style.FramePadding; ImVec2 text_size(0.0f, 0.0f); // Set upper limit of single-line InputTextEx() at 2 million characters strings. The current pathological worst case is a long line // without any carriage return, which would makes ImFont::RenderText() reserve too many vertices and probably crash. Avoid it altogether. // Note that we only use this limit on single-line InputText(), so a pathologically large line on a InputTextMultiline() would still crash. const int buf_display_max_length = 2 * 1024 * 1024; const char* buf_display = buf_display_from_state ? state->TextA.Data : buf; //-V595 const char* buf_display_end = NULL; // We have specialized paths below for setting the length if (is_displaying_hint) { buf_display = hint; buf_display_end = hint + strlen(hint); } // Render text. We currently only render selection when the widget is active or while scrolling. // FIXME: We could remove the '&& render_cursor' to keep rendering selection when inactive. if (render_cursor || render_selection) { IM_ASSERT(state != NULL); if (!is_displaying_hint) buf_display_end = buf_display + state->CurLenA; // Render text (with cursor and selection) // This is going to be messy. We need to: // - Display the text (this alone can be more easily clipped) // - Handle scrolling, highlight selection, display cursor (those all requires some form of 1d->2d cursor position calculation) // - Measure text height (for scrollbar) // 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) // FIXME: This should occur on buf_display but we'd need to maintain cursor/select_start/select_end for UTF-8. const ImWchar* text_begin = state->TextW.Data; ImVec2 cursor_offset, select_start_offset; { // Find lines numbers straddling 'cursor' (slot 0) and 'select_start' (slot 1) positions. const ImWchar* searches_input_ptr[2] = { NULL, NULL }; int searches_result_line_no[2] = { -1000, -1000 }; int searches_remaining = 0; if (render_cursor) { searches_input_ptr[0] = text_begin + state->Stb.cursor; searches_result_line_no[0] = -1; searches_remaining++; } if (render_selection) { searches_input_ptr[1] = text_begin + ImMin(state->Stb.select_start, state->Stb.select_end); searches_result_line_no[1] = -1; searches_remaining++; } // Iterate all lines to find our line numbers // In multi-line mode, we never exit the loop until all lines are counted, so add one extra to the searches_remaining counter. searches_remaining += is_multiline ? 1 : 0; int line_count = 0; //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 for (const ImWchar* s = text_begin; *s != 0; s++) if (*s == '\n') { line_count++; if (searches_result_line_no[0] == -1 && s >= searches_input_ptr[0]) { searches_result_line_no[0] = line_count; if (--searches_remaining <= 0) break; } if (searches_result_line_no[1] == -1 && s >= searches_input_ptr[1]) { searches_result_line_no[1] = line_count; if (--searches_remaining <= 0) break; } } line_count++; if (searches_result_line_no[0] == -1) searches_result_line_no[0] = line_count; if (searches_result_line_no[1] == -1) searches_result_line_no[1] = line_count; // Calculate 2d position by finding the beginning of the line and measuring distance cursor_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_ptr[0], text_begin), searches_input_ptr[0]).x; cursor_offset.y = searches_result_line_no[0] * g.FontSize; if (searches_result_line_no[1] >= 0) { select_start_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_ptr[1], text_begin), searches_input_ptr[1]).x; select_start_offset.y = searches_result_line_no[1] * g.FontSize; } // Store text height (note that we haven't calculated text width at all, see GitHub issues #383, #1224) if (is_multiline) text_size = ImVec2(size.x, line_count * g.FontSize); } // Scroll if (render_cursor && state->CursorFollow) { // Horizontal scroll in chunks of quarter width if (!(flags & ImGuiInputTextFlags_NoHorizontalScroll)) { const float scroll_increment_x = size.x * 0.25f; if (cursor_offset.x < state->ScrollX) state->ScrollX = (float)(int)ImMax(0.0f, cursor_offset.x - scroll_increment_x); else if (cursor_offset.x - size.x >= state->ScrollX) state->ScrollX = (float)(int)(cursor_offset.x - size.x + scroll_increment_x); } else { state->ScrollX = 0.0f; } // Vertical scroll if (is_multiline) { float scroll_y = draw_window->Scroll.y; if (cursor_offset.y - g.FontSize < scroll_y) scroll_y = ImMax(0.0f, cursor_offset.y - g.FontSize); else if (cursor_offset.y - size.y >= scroll_y) scroll_y = cursor_offset.y - size.y; draw_window->DC.CursorPos.y += (draw_window->Scroll.y - scroll_y); // Manipulate cursor pos immediately avoid a frame of lag draw_window->Scroll.y = scroll_y; draw_pos.y = draw_window->DC.CursorPos.y; } state->CursorFollow = false; } // Draw selection const ImVec2 draw_scroll = ImVec2(state->ScrollX, 0.0f); if (render_selection) { const ImWchar* text_selected_begin = text_begin + ImMin(state->Stb.select_start, state->Stb.select_end); const ImWchar* text_selected_end = text_begin + ImMax(state->Stb.select_start, state->Stb.select_end); 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. 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. float bg_offy_dn = is_multiline ? 0.0f : 2.0f; ImVec2 rect_pos = draw_pos + select_start_offset - draw_scroll; for (const ImWchar* p = text_selected_begin; p < text_selected_end; ) { if (rect_pos.y > clip_rect.w + g.FontSize) break; if (rect_pos.y < clip_rect.y) { //p = (const ImWchar*)wmemchr((const wchar_t*)p, '\n', text_selected_end - p); // FIXME-OPT: Could use this when wchar_t are 16-bits //p = p ? p + 1 : text_selected_end; while (p < text_selected_end) if (*p++ == '\n') break; } else { ImVec2 rect_size = InputTextCalcTextSizeW(p, text_selected_end, &p, NULL, true); if (rect_size.x <= 0.0f) rect_size.x = (float)(int)(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos +ImVec2(rect_size.x, bg_offy_dn)); rect.ClipWith(clip_rect); if (rect.Overlaps(clip_rect)) draw_window->DrawList->AddRectFilled(rect.Min, rect.Max, bg_color); } rect_pos.x = draw_pos.x - draw_scroll.x; rect_pos.y += g.FontSize; } } // 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. if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length) { ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text); 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); } // Draw blinking cursor if (render_cursor) { state->CursorAnim += io.DeltaTime; bool cursor_is_visible = (!g.IO.ConfigInputTextCursorBlink) || (state->CursorAnim <= 0.0f) || ImFmod(state->CursorAnim, 1.20f) <= 0.80f; ImVec2 cursor_screen_pos = draw_pos + cursor_offset - draw_scroll; 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); if (cursor_is_visible && cursor_screen_rect.Overlaps(clip_rect)) draw_window->DrawList->AddLine(cursor_screen_rect.Min, cursor_screen_rect.GetBL(), GetColorU32(ImGuiCol_Text)); // 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.) if (!is_readonly) { g.PlatformImePos = ImVec2(cursor_screen_pos.x - 1, cursor_screen_pos.y - g.FontSize); g.PlatformImePosViewport = window->Viewport; } } } else { // Render text only (no selection, no cursor) if (is_multiline) text_size = ImVec2(size.x, InputTextCalcTextLenAndLineCount(buf_display, &buf_display_end) * g.FontSize); // We don't need width else if (!is_displaying_hint && g.ActiveId == id) buf_display_end = buf_display + state->CurLenA; else if (!is_displaying_hint) buf_display_end = buf_display + strlen(buf_display); if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length) { ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text); draw_window->DrawList->AddText(g.Font, g.FontSize, draw_pos, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect); } } if (is_multiline) { Dummy(text_size + ImVec2(0.0f, g.FontSize)); // Always add room to scroll an extra line EndChildFrame(); EndGroup(); } if (is_password && !is_displaying_hint) PopFont(); // Log as text if (g.LogEnabled && !(is_password && !is_displaying_hint)) LogRenderedText(&draw_pos, buf_display, buf_display_end); if (label_size.x > 0) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); if (value_changed) MarkItemEdited(id); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); if ((flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0) return enter_pressed; else return value_changed; } //------------------------------------------------------------------------- // [SECTION] Widgets: ColorEdit, ColorPicker, ColorButton, etc. //------------------------------------------------------------------------- // - ColorEdit3() // - ColorEdit4() // - ColorPicker3() // - RenderColorRectWithAlphaCheckerboard() [Internal] // - ColorPicker4() // - ColorButton() // - SetColorEditOptions() // - ColorTooltip() [Internal] // - ColorEditOptionsPopup() [Internal] // - ColorPickerOptionsPopup() [Internal] //------------------------------------------------------------------------- bool ImGui::ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags) { return ColorEdit4(label, col, flags | ImGuiColorEditFlags_NoAlpha); } // Edit colors components (each component in 0.0f..1.0f range). // See enum ImGuiColorEditFlags_ for available options. e.g. Only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set. // 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. bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const float square_sz = GetFrameHeight(); const float w_extra = (flags & ImGuiColorEditFlags_NoSmallPreview) ? 0.0f : (square_sz + style.ItemInnerSpacing.x); const float w_items_all = GetNextItemWidth() - w_extra; const char* label_display_end = FindRenderedTextEnd(label); BeginGroup(); PushID(label); // If we're not showing any slider there's no point in doing any HSV conversions const ImGuiColorEditFlags flags_untouched = flags; if (flags & ImGuiColorEditFlags_NoInputs) flags = (flags & (~ImGuiColorEditFlags__DisplayMask)) | ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_NoOptions; // Context menu: display and modify options (before defaults are applied) if (!(flags & ImGuiColorEditFlags_NoOptions)) ColorEditOptionsPopup(col, flags); // Read stored options if (!(flags & ImGuiColorEditFlags__DisplayMask)) flags |= (g.ColorEditOptions & ImGuiColorEditFlags__DisplayMask); if (!(flags & ImGuiColorEditFlags__DataTypeMask)) flags |= (g.ColorEditOptions & ImGuiColorEditFlags__DataTypeMask); if (!(flags & ImGuiColorEditFlags__PickerMask)) flags |= (g.ColorEditOptions & ImGuiColorEditFlags__PickerMask); if (!(flags & ImGuiColorEditFlags__InputMask)) flags |= (g.ColorEditOptions & ImGuiColorEditFlags__InputMask); flags |= (g.ColorEditOptions & ~(ImGuiColorEditFlags__DisplayMask | ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__PickerMask | ImGuiColorEditFlags__InputMask)); IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DisplayMask)); // Check that only 1 is selected IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask)); // Check that only 1 is selected const bool alpha = (flags & ImGuiColorEditFlags_NoAlpha) == 0; const bool hdr = (flags & ImGuiColorEditFlags_HDR) != 0; const int components = alpha ? 4 : 3; // Convert to the formats we need float f[4] = { col[0], col[1], col[2], alpha ? col[3] : 1.0f }; if ((flags & ImGuiColorEditFlags_InputHSV) && (flags & ImGuiColorEditFlags_DisplayRGB)) ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]); else if ((flags & ImGuiColorEditFlags_InputRGB) && (flags & ImGuiColorEditFlags_DisplayHSV)) ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]); 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]) }; bool value_changed = false; bool value_changed_as_float = false; if ((flags & (ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV)) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) { // RGB/HSV 0..255 Sliders const float w_item_one = ImMax(1.0f, (float)(int)((w_items_all - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); const float w_item_last = ImMax(1.0f, (float)(int)(w_items_all - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); const bool hide_prefix = (w_item_one <= CalcTextSize((flags & ImGuiColorEditFlags_Float) ? "M:0.000" : "M:000").x); static const char* ids[4] = { "##X", "##Y", "##Z", "##W" }; static const char* fmt_table_int[3][4] = { { "%3d", "%3d", "%3d", "%3d" }, // Short display { "R:%3d", "G:%3d", "B:%3d", "A:%3d" }, // Long display for RGBA { "H:%3d", "S:%3d", "V:%3d", "A:%3d" } // Long display for HSVA }; static const char* fmt_table_float[3][4] = { { "%0.3f", "%0.3f", "%0.3f", "%0.3f" }, // Short display { "R:%0.3f", "G:%0.3f", "B:%0.3f", "A:%0.3f" }, // Long display for RGBA { "H:%0.3f", "S:%0.3f", "V:%0.3f", "A:%0.3f" } // Long display for HSVA }; const int fmt_idx = hide_prefix ? 0 : (flags & ImGuiColorEditFlags_DisplayHSV) ? 2 : 1; for (int n = 0; n < components; n++) { if (n > 0) SameLine(0, style.ItemInnerSpacing.x); SetNextItemWidth((n + 1 < components) ? w_item_one : w_item_last); if (flags & ImGuiColorEditFlags_Float) { value_changed |= DragFloat(ids[n], &f[n], 1.0f/255.0f, 0.0f, hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]); value_changed_as_float |= value_changed; } else { value_changed |= DragInt(ids[n], &i[n], 1.0f, 0, hdr ? 0 : 255, fmt_table_int[fmt_idx][n]); } if (!(flags & ImGuiColorEditFlags_NoOptions)) OpenPopupOnItemClick("context"); } } else if ((flags & ImGuiColorEditFlags_DisplayHex) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) { // RGB Hexadecimal Input char buf[64]; if (alpha) 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)); else ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", ImClamp(i[0],0,255), ImClamp(i[1],0,255), ImClamp(i[2],0,255)); SetNextItemWidth(w_items_all); if (InputText("##Text", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase)) { value_changed = true; char* p = buf; while (*p == '#' || ImCharIsBlankA(*p)) p++; i[0] = i[1] = i[2] = i[3] = 0; if (alpha) 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) else sscanf(p, "%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]); } if (!(flags & ImGuiColorEditFlags_NoOptions)) OpenPopupOnItemClick("context"); } ImGuiWindow* picker_active_window = NULL; if (!(flags & ImGuiColorEditFlags_NoSmallPreview)) { if (!(flags & ImGuiColorEditFlags_NoInputs)) SameLine(0, style.ItemInnerSpacing.x); const ImVec4 col_v4(col[0], col[1], col[2], alpha ? col[3] : 1.0f); if (ColorButton("##ColorButton", col_v4, flags)) { if (!(flags & ImGuiColorEditFlags_NoPicker)) { // Store current color and open a picker g.ColorPickerRef = col_v4; OpenPopup("picker"); SetNextWindowPos(window->DC.LastItemRect.GetBL() + ImVec2(-1,style.ItemSpacing.y)); } } if (!(flags & ImGuiColorEditFlags_NoOptions)) OpenPopupOnItemClick("context"); if (BeginPopup("picker")) { picker_active_window = g.CurrentWindow; if (label != label_display_end) { TextEx(label, label_display_end); Spacing(); } ImGuiColorEditFlags picker_flags_to_forward = ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__PickerMask | ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaBar; ImGuiColorEditFlags picker_flags = (flags_untouched & picker_flags_to_forward) | ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaPreviewHalf; SetNextItemWidth(square_sz * 12.0f); // Use 256 + bar sizes? value_changed |= ColorPicker4("##picker", col, picker_flags, &g.ColorPickerRef.x); EndPopup(); } } if (label != label_display_end && !(flags & ImGuiColorEditFlags_NoLabel)) { SameLine(0, style.ItemInnerSpacing.x); TextEx(label, label_display_end); } // Convert back if (value_changed && picker_active_window == NULL) { if (!value_changed_as_float) for (int n = 0; n < 4; n++) f[n] = i[n] / 255.0f; if ((flags & ImGuiColorEditFlags_DisplayHSV) && (flags & ImGuiColorEditFlags_InputRGB)) ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]); if ((flags & ImGuiColorEditFlags_DisplayRGB) && (flags & ImGuiColorEditFlags_InputHSV)) ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]); col[0] = f[0]; col[1] = f[1]; col[2] = f[2]; if (alpha) col[3] = f[3]; } PopID(); EndGroup(); // Drag and Drop Target // NB: The flag test is merely an optional micro-optimization, BeginDragDropTarget() does the same test. if ((window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect) && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropTarget()) { bool accepted_drag_drop = false; if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F)) { memcpy((float*)col, payload->Data, sizeof(float) * 3); // Preserve alpha if any //-V512 value_changed = accepted_drag_drop = true; } if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F)) { memcpy((float*)col, payload->Data, sizeof(float) * components); value_changed = accepted_drag_drop = true; } // Drag-drop payloads are always RGB if (accepted_drag_drop && (flags & ImGuiColorEditFlags_InputHSV)) ColorConvertRGBtoHSV(col[0], col[1], col[2], col[0], col[1], col[2]); EndDragDropTarget(); } // When picker is being actively used, use its active id so IsItemActive() will function on ColorEdit4(). if (picker_active_window && g.ActiveId != 0 && g.ActiveIdWindow == picker_active_window) window->DC.LastItemId = g.ActiveId; if (value_changed) MarkItemEdited(window->DC.LastItemId); return value_changed; } bool ImGui::ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags flags) { float col4[4] = { col[0], col[1], col[2], 1.0f }; if (!ColorPicker4(label, col4, flags | ImGuiColorEditFlags_NoAlpha)) return false; col[0] = col4[0]; col[1] = col4[1]; col[2] = col4[2]; return true; } static inline ImU32 ImAlphaBlendColor(ImU32 col_a, ImU32 col_b) { float t = ((col_b >> IM_COL32_A_SHIFT) & 0xFF) / 255.f; int r = ImLerp((int)(col_a >> IM_COL32_R_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_R_SHIFT) & 0xFF, t); int g = ImLerp((int)(col_a >> IM_COL32_G_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_G_SHIFT) & 0xFF, t); int b = ImLerp((int)(col_a >> IM_COL32_B_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_B_SHIFT) & 0xFF, t); return IM_COL32(r, g, b, 0xFF); } // Helper for ColorPicker4() // 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. // 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. void ImGui::RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 col, float grid_step, ImVec2 grid_off, float rounding, int rounding_corners_flags) { ImGuiWindow* window = GetCurrentWindow(); if (((col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT) < 0xFF) { ImU32 col_bg1 = GetColorU32(ImAlphaBlendColor(IM_COL32(204,204,204,255), col)); ImU32 col_bg2 = GetColorU32(ImAlphaBlendColor(IM_COL32(128,128,128,255), col)); window->DrawList->AddRectFilled(p_min, p_max, col_bg1, rounding, rounding_corners_flags); int yi = 0; for (float y = p_min.y + grid_off.y; y < p_max.y; y += grid_step, yi++) { float y1 = ImClamp(y, p_min.y, p_max.y), y2 = ImMin(y + grid_step, p_max.y); if (y2 <= y1) continue; for (float x = p_min.x + grid_off.x + (yi & 1) * grid_step; x < p_max.x; x += grid_step * 2.0f) { float x1 = ImClamp(x, p_min.x, p_max.x), x2 = ImMin(x + grid_step, p_max.x); if (x2 <= x1) continue; int rounding_corners_flags_cell = 0; 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; } 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; } rounding_corners_flags_cell &= rounding_corners_flags; window->DrawList->AddRectFilled(ImVec2(x1,y1), ImVec2(x2,y2), col_bg2, rounding_corners_flags_cell ? rounding : 0.0f, rounding_corners_flags_cell); } } } else { window->DrawList->AddRectFilled(p_min, p_max, col, rounding, rounding_corners_flags); } } // Helper for ColorPicker4() static void RenderArrowsForVerticalBar(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, float bar_w) { 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); ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + half_sz.x, pos.y), half_sz, ImGuiDir_Right, IM_COL32_WHITE); 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); ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + bar_w - half_sz.x, pos.y), half_sz, ImGuiDir_Left, IM_COL32_WHITE); } // Note: ColorPicker4() only accesses 3 floats if ImGuiColorEditFlags_NoAlpha flag is set. // (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.) // 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..) bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags flags, const float* ref_col) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImDrawList* draw_list = window->DrawList; ImGuiStyle& style = g.Style; ImGuiIO& io = g.IO; PushID(label); BeginGroup(); if (!(flags & ImGuiColorEditFlags_NoSidePreview)) flags |= ImGuiColorEditFlags_NoSmallPreview; // Context menu: display and store options. if (!(flags & ImGuiColorEditFlags_NoOptions)) ColorPickerOptionsPopup(col, flags); // Read stored options if (!(flags & ImGuiColorEditFlags__PickerMask)) flags |= ((g.ColorEditOptions & ImGuiColorEditFlags__PickerMask) ? g.ColorEditOptions : ImGuiColorEditFlags__OptionsDefault) & ImGuiColorEditFlags__PickerMask; if (!(flags & ImGuiColorEditFlags__InputMask)) flags |= ((g.ColorEditOptions & ImGuiColorEditFlags__InputMask) ? g.ColorEditOptions : ImGuiColorEditFlags__OptionsDefault) & ImGuiColorEditFlags__InputMask; IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__PickerMask)); // Check that only 1 is selected IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask)); // Check that only 1 is selected if (!(flags & ImGuiColorEditFlags_NoOptions)) flags |= (g.ColorEditOptions & ImGuiColorEditFlags_AlphaBar); // Setup int components = (flags & ImGuiColorEditFlags_NoAlpha) ? 3 : 4; bool alpha_bar = (flags & ImGuiColorEditFlags_AlphaBar) && !(flags & ImGuiColorEditFlags_NoAlpha); ImVec2 picker_pos = window->DC.CursorPos; float square_sz = GetFrameHeight(); float bars_width = square_sz; // Arbitrary smallish width of Hue/Alpha picking bars float sv_picker_size = ImMax(bars_width * 1, GetNextItemWidth() - (alpha_bar ? 2 : 1) * (bars_width + style.ItemInnerSpacing.x)); // Saturation/Value picking box float bar0_pos_x = picker_pos.x + sv_picker_size + style.ItemInnerSpacing.x; float bar1_pos_x = bar0_pos_x + bars_width + style.ItemInnerSpacing.x; float bars_triangles_half_sz = (float)(int)(bars_width * 0.20f); float backup_initial_col[4]; memcpy(backup_initial_col, col, components * sizeof(float)); float wheel_thickness = sv_picker_size * 0.08f; float wheel_r_outer = sv_picker_size * 0.50f; float wheel_r_inner = wheel_r_outer - wheel_thickness; ImVec2 wheel_center(picker_pos.x + (sv_picker_size + bars_width)*0.5f, picker_pos.y + sv_picker_size*0.5f); // Note: the triangle is displayed rotated with triangle_pa pointing to Hue, but most coordinates stays unrotated for logic. float triangle_r = wheel_r_inner - (int)(sv_picker_size * 0.027f); ImVec2 triangle_pa = ImVec2(triangle_r, 0.0f); // Hue point. ImVec2 triangle_pb = ImVec2(triangle_r * -0.5f, triangle_r * -0.866025f); // Black point. ImVec2 triangle_pc = ImVec2(triangle_r * -0.5f, triangle_r * +0.866025f); // White point. float H = col[0], S = col[1], V = col[2]; float R = col[0], G = col[1], B = col[2]; if (flags & ImGuiColorEditFlags_InputRGB) ColorConvertRGBtoHSV(R, G, B, H, S, V); else if (flags & ImGuiColorEditFlags_InputHSV) ColorConvertHSVtoRGB(H, S, V, R, G, B); bool value_changed = false, value_changed_h = false, value_changed_sv = false; PushItemFlag(ImGuiItemFlags_NoNav, true); if (flags & ImGuiColorEditFlags_PickerHueWheel) { // Hue wheel + SV triangle logic InvisibleButton("hsv", ImVec2(sv_picker_size + style.ItemInnerSpacing.x + bars_width, sv_picker_size)); if (IsItemActive()) { ImVec2 initial_off = g.IO.MouseClickedPos[0] - wheel_center; ImVec2 current_off = g.IO.MousePos - wheel_center; float initial_dist2 = ImLengthSqr(initial_off); if (initial_dist2 >= (wheel_r_inner-1)*(wheel_r_inner-1) && initial_dist2 <= (wheel_r_outer+1)*(wheel_r_outer+1)) { // Interactive with Hue wheel H = ImAtan2(current_off.y, current_off.x) / IM_PI*0.5f; if (H < 0.0f) H += 1.0f; value_changed = value_changed_h = true; } float cos_hue_angle = ImCos(-H * 2.0f * IM_PI); float sin_hue_angle = ImSin(-H * 2.0f * IM_PI); if (ImTriangleContainsPoint(triangle_pa, triangle_pb, triangle_pc, ImRotate(initial_off, cos_hue_angle, sin_hue_angle))) { // Interacting with SV triangle ImVec2 current_off_unrotated = ImRotate(current_off, cos_hue_angle, sin_hue_angle); if (!ImTriangleContainsPoint(triangle_pa, triangle_pb, triangle_pc, current_off_unrotated)) current_off_unrotated = ImTriangleClosestPoint(triangle_pa, triangle_pb, triangle_pc, current_off_unrotated); float uu, vv, ww; ImTriangleBarycentricCoords(triangle_pa, triangle_pb, triangle_pc, current_off_unrotated, uu, vv, ww); V = ImClamp(1.0f - vv, 0.0001f, 1.0f); S = ImClamp(uu / V, 0.0001f, 1.0f); value_changed = value_changed_sv = true; } } if (!(flags & ImGuiColorEditFlags_NoOptions)) OpenPopupOnItemClick("context"); } else if (flags & ImGuiColorEditFlags_PickerHueBar) { // SV rectangle logic InvisibleButton("sv", ImVec2(sv_picker_size, sv_picker_size)); if (IsItemActive()) { S = ImSaturate((io.MousePos.x - picker_pos.x) / (sv_picker_size-1)); V = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1)); value_changed = value_changed_sv = true; } if (!(flags & ImGuiColorEditFlags_NoOptions)) OpenPopupOnItemClick("context"); // Hue bar logic SetCursorScreenPos(ImVec2(bar0_pos_x, picker_pos.y)); InvisibleButton("hue", ImVec2(bars_width, sv_picker_size)); if (IsItemActive()) { H = ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1)); value_changed = value_changed_h = true; } } // Alpha bar logic if (alpha_bar) { SetCursorScreenPos(ImVec2(bar1_pos_x, picker_pos.y)); InvisibleButton("alpha", ImVec2(bars_width, sv_picker_size)); if (IsItemActive()) { col[3] = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1)); value_changed = true; } } PopItemFlag(); // ImGuiItemFlags_NoNav if (!(flags & ImGuiColorEditFlags_NoSidePreview)) { SameLine(0, style.ItemInnerSpacing.x); BeginGroup(); } if (!(flags & ImGuiColorEditFlags_NoLabel)) { const char* label_display_end = FindRenderedTextEnd(label); if (label != label_display_end) { if ((flags & ImGuiColorEditFlags_NoSidePreview)) SameLine(0, style.ItemInnerSpacing.x); TextEx(label, label_display_end); } } if (!(flags & ImGuiColorEditFlags_NoSidePreview)) { PushItemFlag(ImGuiItemFlags_NoNavDefaultFocus, true); ImVec4 col_v4(col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]); if ((flags & ImGuiColorEditFlags_NoLabel)) Text("Current"); ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf | ImGuiColorEditFlags_NoTooltip; ColorButton("##current", col_v4, (flags & sub_flags_to_forward), ImVec2(square_sz * 3, square_sz * 2)); if (ref_col != NULL) { Text("Original"); ImVec4 ref_col_v4(ref_col[0], ref_col[1], ref_col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : ref_col[3]); if (ColorButton("##original", ref_col_v4, (flags & sub_flags_to_forward), ImVec2(square_sz * 3, square_sz * 2))) { memcpy(col, ref_col, components * sizeof(float)); value_changed = true; } } PopItemFlag(); EndGroup(); } // Convert back color to RGB if (value_changed_h || value_changed_sv) { if (flags & ImGuiColorEditFlags_InputRGB) { 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]); } else if (flags & ImGuiColorEditFlags_InputHSV) { col[0] = H; col[1] = S; col[2] = V; } } // R,G,B and H,S,V slider color editor bool value_changed_fix_hue_wrap = false; if ((flags & ImGuiColorEditFlags_NoInputs) == 0) { PushItemWidth((alpha_bar ? bar1_pos_x : bar0_pos_x) + bars_width - picker_pos.x); ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoSmallPreview | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf; ImGuiColorEditFlags sub_flags = (flags & sub_flags_to_forward) | ImGuiColorEditFlags_NoPicker; if (flags & ImGuiColorEditFlags_DisplayRGB || (flags & ImGuiColorEditFlags__DisplayMask) == 0) if (ColorEdit4("##rgb", col, sub_flags | ImGuiColorEditFlags_DisplayRGB)) { // FIXME: Hackily differenciating using the DragInt (ActiveId != 0 && !ActiveIdAllowOverlap) vs. using the InputText or DropTarget. // 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) value_changed_fix_hue_wrap = (g.ActiveId != 0 && !g.ActiveIdAllowOverlap); value_changed = true; } if (flags & ImGuiColorEditFlags_DisplayHSV || (flags & ImGuiColorEditFlags__DisplayMask) == 0) value_changed |= ColorEdit4("##hsv", col, sub_flags | ImGuiColorEditFlags_DisplayHSV); if (flags & ImGuiColorEditFlags_DisplayHex || (flags & ImGuiColorEditFlags__DisplayMask) == 0) value_changed |= ColorEdit4("##hex", col, sub_flags | ImGuiColorEditFlags_DisplayHex); PopItemWidth(); } // Try to cancel hue wrap (after ColorEdit4 call), if any if (value_changed_fix_hue_wrap && (flags & ImGuiColorEditFlags_InputRGB)) { float new_H, new_S, new_V; ColorConvertRGBtoHSV(col[0], col[1], col[2], new_H, new_S, new_V); if (new_H <= 0 && H > 0) { if (new_V <= 0 && V != new_V) ColorConvertHSVtoRGB(H, S, new_V <= 0 ? V * 0.5f : new_V, col[0], col[1], col[2]); else if (new_S <= 0) ColorConvertHSVtoRGB(H, new_S <= 0 ? S * 0.5f : new_S, new_V, col[0], col[1], col[2]); } } if (value_changed) { if (flags & ImGuiColorEditFlags_InputRGB) { R = col[0]; G = col[1]; B = col[2]; ColorConvertRGBtoHSV(R, G, B, H, S, V); } else if (flags & ImGuiColorEditFlags_InputHSV) { H = col[0]; S = col[1]; V = col[2]; ColorConvertHSVtoRGB(H, S, V, R, G, B); } } ImVec4 hue_color_f(1, 1, 1, 1); ColorConvertHSVtoRGB(H, 1, 1, hue_color_f.x, hue_color_f.y, hue_color_f.z); ImU32 hue_color32 = ColorConvertFloat4ToU32(hue_color_f); ImU32 col32_no_alpha = ColorConvertFloat4ToU32(ImVec4(R, G, B, 1.0f)); 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) }; ImVec2 sv_cursor_pos; if (flags & ImGuiColorEditFlags_PickerHueWheel) { // Render Hue Wheel const float aeps = 1.5f / wheel_r_outer; // Half a pixel arc length in radians (2pi cancels out). const int segment_per_arc = ImMax(4, (int)wheel_r_outer / 12); for (int n = 0; n < 6; n++) { const float a0 = (n) /6.0f * 2.0f * IM_PI - aeps; const float a1 = (n+1.0f)/6.0f * 2.0f * IM_PI + aeps; const int vert_start_idx = draw_list->VtxBuffer.Size; draw_list->PathArcTo(wheel_center, (wheel_r_inner + wheel_r_outer)*0.5f, a0, a1, segment_per_arc); draw_list->PathStroke(IM_COL32_WHITE, false, wheel_thickness); const int vert_end_idx = draw_list->VtxBuffer.Size; // Paint colors over existing vertices ImVec2 gradient_p0(wheel_center.x + ImCos(a0) * wheel_r_inner, wheel_center.y + ImSin(a0) * wheel_r_inner); ImVec2 gradient_p1(wheel_center.x + ImCos(a1) * wheel_r_inner, wheel_center.y + ImSin(a1) * wheel_r_inner); ShadeVertsLinearColorGradientKeepAlpha(draw_list, vert_start_idx, vert_end_idx, gradient_p0, gradient_p1, hue_colors[n], hue_colors[n+1]); } // Render Cursor + preview on Hue Wheel float cos_hue_angle = ImCos(H * 2.0f * IM_PI); float sin_hue_angle = ImSin(H * 2.0f * IM_PI); 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); float hue_cursor_rad = value_changed_h ? wheel_thickness * 0.65f : wheel_thickness * 0.55f; int hue_cursor_segments = ImClamp((int)(hue_cursor_rad / 1.4f), 9, 32); draw_list->AddCircleFilled(hue_cursor_pos, hue_cursor_rad, hue_color32, hue_cursor_segments); draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad+1, IM_COL32(128,128,128,255), hue_cursor_segments); draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad, IM_COL32_WHITE, hue_cursor_segments); // Render SV triangle (rotated according to hue) ImVec2 tra = wheel_center + ImRotate(triangle_pa, cos_hue_angle, sin_hue_angle); ImVec2 trb = wheel_center + ImRotate(triangle_pb, cos_hue_angle, sin_hue_angle); ImVec2 trc = wheel_center + ImRotate(triangle_pc, cos_hue_angle, sin_hue_angle); ImVec2 uv_white = GetFontTexUvWhitePixel(); draw_list->PrimReserve(6, 6); draw_list->PrimVtx(tra, uv_white, hue_color32); draw_list->PrimVtx(trb, uv_white, hue_color32); draw_list->PrimVtx(trc, uv_white, IM_COL32_WHITE); draw_list->PrimVtx(tra, uv_white, IM_COL32_BLACK_TRANS); draw_list->PrimVtx(trb, uv_white, IM_COL32_BLACK); draw_list->PrimVtx(trc, uv_white, IM_COL32_BLACK_TRANS); draw_list->AddTriangle(tra, trb, trc, IM_COL32(128,128,128,255), 1.5f); sv_cursor_pos = ImLerp(ImLerp(trc, tra, ImSaturate(S)), trb, ImSaturate(1 - V)); } else if (flags & ImGuiColorEditFlags_PickerHueBar) { // Render SV Square draw_list->AddRectFilledMultiColor(picker_pos, picker_pos + ImVec2(sv_picker_size,sv_picker_size), IM_COL32_WHITE, hue_color32, hue_color32, IM_COL32_WHITE); 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); RenderFrameBorder(picker_pos, picker_pos + ImVec2(sv_picker_size,sv_picker_size), 0.0f); 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 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); // Render Hue Bar for (int i = 0; i < 6; ++i) 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]); float bar0_line_y = (float)(int)(picker_pos.y + H * sv_picker_size + 0.5f); RenderFrameBorder(ImVec2(bar0_pos_x, picker_pos.y), ImVec2(bar0_pos_x + bars_width, picker_pos.y + sv_picker_size), 0.0f); 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); } // Render cursor/preview circle (clamp S/V within 0..1 range because floating points colors may lead HSV values to be out of range) float sv_cursor_rad = value_changed_sv ? 10.0f : 6.0f; draw_list->AddCircleFilled(sv_cursor_pos, sv_cursor_rad, col32_no_alpha, 12); draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad+1, IM_COL32(128,128,128,255), 12); draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad, IM_COL32_WHITE, 12); // Render alpha bar if (alpha_bar) { float alpha = ImSaturate(col[3]); ImRect bar1_bb(bar1_pos_x, picker_pos.y, bar1_pos_x + bars_width, picker_pos.y + sv_picker_size); RenderColorRectWithAlphaCheckerboard(bar1_bb.Min, bar1_bb.Max, IM_COL32(0,0,0,0), bar1_bb.GetWidth() / 2.0f, ImVec2(0.0f, 0.0f)); 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); float bar1_line_y = (float)(int)(picker_pos.y + (1.0f - alpha) * sv_picker_size + 0.5f); RenderFrameBorder(bar1_bb.Min, bar1_bb.Max, 0.0f); 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); } EndGroup(); if (value_changed && memcmp(backup_initial_col, col, components * sizeof(float)) == 0) value_changed = false; if (value_changed) MarkItemEdited(window->DC.LastItemId); PopID(); return value_changed; } // A little colored square. Return true when clicked. // FIXME: May want to display/ignore the alpha component in the color display? Yet show it in the tooltip. // 'desc_id' is not called 'label' because we don't display it next to the button, but only in the tooltip. // Note that 'col' may be encoded in HSV if ImGuiColorEditFlags_InputHSV is set. bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags, ImVec2 size) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiID id = window->GetID(desc_id); float default_size = GetFrameHeight(); if (size.x == 0.0f) size.x = default_size; if (size.y == 0.0f) size.y = default_size; const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); ItemSize(bb, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f); if (!ItemAdd(bb, id)) return false; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held); if (flags & ImGuiColorEditFlags_NoAlpha) flags &= ~(ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf); ImVec4 col_rgb = col; if (flags & ImGuiColorEditFlags_InputHSV) ColorConvertHSVtoRGB(col_rgb.x, col_rgb.y, col_rgb.z, col_rgb.x, col_rgb.y, col_rgb.z); ImVec4 col_rgb_without_alpha(col_rgb.x, col_rgb.y, col_rgb.z, 1.0f); float grid_step = ImMin(size.x, size.y) / 2.99f; float rounding = ImMin(g.Style.FrameRounding, grid_step * 0.5f); ImRect bb_inner = bb; 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. bb_inner.Expand(off); if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f) { float mid_x = (float)(int)((bb_inner.Min.x + bb_inner.Max.x) * 0.5f + 0.5f); 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); window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawCornerFlags_TopLeft|ImDrawCornerFlags_BotLeft); } else { // Because GetColorU32() multiplies by the global style Alpha and we don't want to display a checkerboard if the source code had no alpha ImVec4 col_source = (flags & ImGuiColorEditFlags_AlphaPreview) ? col_rgb : col_rgb_without_alpha; if (col_source.w < 1.0f) RenderColorRectWithAlphaCheckerboard(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding); else window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding, ImDrawCornerFlags_All); } RenderNavHighlight(bb, id); if (g.Style.FrameBorderSize > 0.0f) RenderFrameBorder(bb.Min, bb.Max, rounding); else window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), rounding); // Color button are often in need of some sort of border // Drag and Drop Source // NB: The ActiveId test is merely an optional micro-optimization, BeginDragDropSource() does the same test. if (g.ActiveId == id && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropSource()) { if (flags & ImGuiColorEditFlags_NoAlpha) SetDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F, &col_rgb, sizeof(float) * 3, ImGuiCond_Once); else SetDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F, &col_rgb, sizeof(float) * 4, ImGuiCond_Once); ColorButton(desc_id, col, flags); SameLine(); TextEx("Color"); EndDragDropSource(); } // Tooltip if (!(flags & ImGuiColorEditFlags_NoTooltip) && hovered) ColorTooltip(desc_id, &col.x, flags & (ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)); if (pressed) MarkItemEdited(id); return pressed; } // Initialize/override default color options void ImGui::SetColorEditOptions(ImGuiColorEditFlags flags) { ImGuiContext& g = *GImGui; if ((flags & ImGuiColorEditFlags__DisplayMask) == 0) flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__DisplayMask; if ((flags & ImGuiColorEditFlags__DataTypeMask) == 0) flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__DataTypeMask; if ((flags & ImGuiColorEditFlags__PickerMask) == 0) flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__PickerMask; if ((flags & ImGuiColorEditFlags__InputMask) == 0) flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__InputMask; IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DisplayMask)); // Check only 1 option is selected IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DataTypeMask)); // Check only 1 option is selected IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__PickerMask)); // Check only 1 option is selected IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask)); // Check only 1 option is selected g.ColorEditOptions = flags; } // Note: only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set. void ImGui::ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags) { ImGuiContext& g = *GImGui; BeginTooltipEx(0, true); const char* text_end = text ? FindRenderedTextEnd(text, NULL) : text; if (text_end > text) { TextEx(text, text_end); Separator(); } ImVec2 sz(g.FontSize * 3 + g.Style.FramePadding.y * 2, g.FontSize * 3 + g.Style.FramePadding.y * 2); ImVec4 cf(col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]); 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]); ColorButton("##preview", cf, (flags & (ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)) | ImGuiColorEditFlags_NoTooltip, sz); SameLine(); if ((flags & ImGuiColorEditFlags_InputRGB) || !(flags & ImGuiColorEditFlags__InputMask)) { if (flags & ImGuiColorEditFlags_NoAlpha) 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]); else 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]); } else if (flags & ImGuiColorEditFlags_InputHSV) { if (flags & ImGuiColorEditFlags_NoAlpha) Text("H: %.3f, S: %.3f, V: %.3f", col[0], col[1], col[2]); else Text("H: %.3f, S: %.3f, V: %.3f, A: %.3f", col[0], col[1], col[2], col[3]); } EndTooltip(); } void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) { bool allow_opt_inputs = !(flags & ImGuiColorEditFlags__DisplayMask); bool allow_opt_datatype = !(flags & ImGuiColorEditFlags__DataTypeMask); if ((!allow_opt_inputs && !allow_opt_datatype) || !BeginPopup("context")) return; ImGuiContext& g = *GImGui; ImGuiColorEditFlags opts = g.ColorEditOptions; if (allow_opt_inputs) { if (RadioButton("RGB", (opts & ImGuiColorEditFlags_DisplayRGB) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayRGB; if (RadioButton("HSV", (opts & ImGuiColorEditFlags_DisplayHSV) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayHSV; if (RadioButton("Hex", (opts & ImGuiColorEditFlags_DisplayHex) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayHex; } if (allow_opt_datatype) { if (allow_opt_inputs) Separator(); if (RadioButton("0..255", (opts & ImGuiColorEditFlags_Uint8) != 0)) opts = (opts & ~ImGuiColorEditFlags__DataTypeMask) | ImGuiColorEditFlags_Uint8; if (RadioButton("0.00..1.00", (opts & ImGuiColorEditFlags_Float) != 0)) opts = (opts & ~ImGuiColorEditFlags__DataTypeMask) | ImGuiColorEditFlags_Float; } if (allow_opt_inputs || allow_opt_datatype) Separator(); if (Button("Copy as..", ImVec2(-1,0))) OpenPopup("Copy"); if (BeginPopup("Copy")) { 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]); char buf[64]; ImFormatString(buf, IM_ARRAYSIZE(buf), "(%.3ff, %.3ff, %.3ff, %.3ff)", col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]); if (Selectable(buf)) SetClipboardText(buf); ImFormatString(buf, IM_ARRAYSIZE(buf), "(%d,%d,%d,%d)", cr, cg, cb, ca); if (Selectable(buf)) SetClipboardText(buf); if (flags & ImGuiColorEditFlags_NoAlpha) ImFormatString(buf, IM_ARRAYSIZE(buf), "0x%02X%02X%02X", cr, cg, cb); else ImFormatString(buf, IM_ARRAYSIZE(buf), "0x%02X%02X%02X%02X", cr, cg, cb, ca); if (Selectable(buf)) SetClipboardText(buf); EndPopup(); } g.ColorEditOptions = opts; EndPopup(); } void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags) { bool allow_opt_picker = !(flags & ImGuiColorEditFlags__PickerMask); bool allow_opt_alpha_bar = !(flags & ImGuiColorEditFlags_NoAlpha) && !(flags & ImGuiColorEditFlags_AlphaBar); if ((!allow_opt_picker && !allow_opt_alpha_bar) || !BeginPopup("context")) return; ImGuiContext& g = *GImGui; if (allow_opt_picker) { 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 PushItemWidth(picker_size.x); for (int picker_type = 0; picker_type < 2; picker_type++) { // Draw small/thumbnail version of each picker type (over an invisible button for selection) if (picker_type > 0) Separator(); PushID(picker_type); ImGuiColorEditFlags picker_flags = ImGuiColorEditFlags_NoInputs|ImGuiColorEditFlags_NoOptions|ImGuiColorEditFlags_NoLabel|ImGuiColorEditFlags_NoSidePreview|(flags & ImGuiColorEditFlags_NoAlpha); if (picker_type == 0) picker_flags |= ImGuiColorEditFlags_PickerHueBar; if (picker_type == 1) picker_flags |= ImGuiColorEditFlags_PickerHueWheel; ImVec2 backup_pos = GetCursorScreenPos(); if (Selectable("##selectable", false, 0, picker_size)) // By default, Selectable() is closing popup g.ColorEditOptions = (g.ColorEditOptions & ~ImGuiColorEditFlags__PickerMask) | (picker_flags & ImGuiColorEditFlags__PickerMask); SetCursorScreenPos(backup_pos); ImVec4 dummy_ref_col; memcpy(&dummy_ref_col, ref_col, sizeof(float) * ((picker_flags & ImGuiColorEditFlags_NoAlpha) ? 3 : 4)); ColorPicker4("##dummypicker", &dummy_ref_col.x, picker_flags); PopID(); } PopItemWidth(); } if (allow_opt_alpha_bar) { if (allow_opt_picker) Separator(); CheckboxFlags("Alpha Bar", (unsigned int*)&g.ColorEditOptions, ImGuiColorEditFlags_AlphaBar); } EndPopup(); } //------------------------------------------------------------------------- // [SECTION] Widgets: TreeNode, CollapsingHeader, etc. //------------------------------------------------------------------------- // - TreeNode() // - TreeNodeV() // - TreeNodeEx() // - TreeNodeExV() // - TreeNodeBehavior() [Internal] // - TreePush() // - TreePop() // - TreeAdvanceToLabelPos() // - GetTreeNodeToLabelSpacing() // - SetNextTreeNodeOpen() // - CollapsingHeader() //------------------------------------------------------------------------- bool ImGui::TreeNode(const char* str_id, const char* fmt, ...) { va_list args; va_start(args, fmt); bool is_open = TreeNodeExV(str_id, 0, fmt, args); va_end(args); return is_open; } bool ImGui::TreeNode(const void* ptr_id, const char* fmt, ...) { va_list args; va_start(args, fmt); bool is_open = TreeNodeExV(ptr_id, 0, fmt, args); va_end(args); return is_open; } bool ImGui::TreeNode(const char* label) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; return TreeNodeBehavior(window->GetID(label), 0, label, NULL); } bool ImGui::TreeNodeV(const char* str_id, const char* fmt, va_list args) { return TreeNodeExV(str_id, 0, fmt, args); } bool ImGui::TreeNodeV(const void* ptr_id, const char* fmt, va_list args) { return TreeNodeExV(ptr_id, 0, fmt, args); } bool ImGui::TreeNodeEx(const char* label, ImGuiTreeNodeFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; return TreeNodeBehavior(window->GetID(label), flags, label, NULL); } bool ImGui::TreeNodeEx(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) { va_list args; va_start(args, fmt); bool is_open = TreeNodeExV(str_id, flags, fmt, args); va_end(args); return is_open; } bool ImGui::TreeNodeEx(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) { va_list args; va_start(args, fmt); bool is_open = TreeNodeExV(ptr_id, flags, fmt, args); va_end(args); return is_open; } bool ImGui::TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); return TreeNodeBehavior(window->GetID(str_id), flags, g.TempBuffer, label_end); } bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); return TreeNodeBehavior(window->GetID(ptr_id), flags, g.TempBuffer, label_end); } bool ImGui::TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags) { if (flags & ImGuiTreeNodeFlags_Leaf) return true; // We only write to the tree storage if the user clicks (or explicitly use SetNextTreeNode*** functions) ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImGuiStorage* storage = window->DC.StateStorage; bool is_open; if (g.NextTreeNodeOpenCond != 0) { if (g.NextTreeNodeOpenCond & ImGuiCond_Always) { is_open = g.NextTreeNodeOpenVal; storage->SetInt(id, is_open); } else { // We treat ImGuiCond_Once and ImGuiCond_FirstUseEver the same because tree node state are not saved persistently. const int stored_value = storage->GetInt(id, -1); if (stored_value == -1) { is_open = g.NextTreeNodeOpenVal; storage->SetInt(id, is_open); } else { is_open = stored_value != 0; } } g.NextTreeNodeOpenCond = 0; } else { is_open = storage->GetInt(id, (flags & ImGuiTreeNodeFlags_DefaultOpen) ? 1 : 0) != 0; } // When logging is enabled, we automatically expand tree nodes (but *NOT* collapsing headers.. seems like sensible behavior). // NB- If we are above max depth we still allow manually opened nodes to be logged. if (g.LogEnabled && !(flags & ImGuiTreeNodeFlags_NoAutoOpenOnLog) && (window->DC.TreeDepth - g.LogDepthRef) < g.LogDepthToExpand) is_open = true; return is_open; } bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const bool display_frame = (flags & ImGuiTreeNodeFlags_Framed) != 0; const ImVec2 padding = (display_frame || (flags & ImGuiTreeNodeFlags_FramePadding)) ? style.FramePadding : ImVec2(style.FramePadding.x, 0.0f); if (!label_end) label_end = FindRenderedTextEnd(label); const ImVec2 label_size = CalcTextSize(label, label_end, false); // We vertically grow up to current line height up the typical widget height. const float text_base_offset_y = ImMax(padding.y, window->DC.CurrentLineTextBaseOffset); // Latch before ItemSize changes it const float frame_height = ImMax(ImMin(window->DC.CurrentLineSize.y, g.FontSize + style.FramePadding.y*2), label_size.y + padding.y*2); ImRect frame_bb = ImRect(window->DC.CursorPos, ImVec2(GetContentRegionMaxScreen().x, window->DC.CursorPos.y + frame_height)); if (display_frame) { // Framed header expand a little outside the default padding frame_bb.Min.x -= (float)(int)(window->WindowPadding.x*0.5f) - 1; frame_bb.Max.x += (float)(int)(window->WindowPadding.x*0.5f) - 1; } const float text_offset_x = (g.FontSize + (display_frame ? padding.x*3 : padding.x*2)); // Collapser arrow width + Spacing const float text_width = g.FontSize + (label_size.x > 0.0f ? label_size.x + padding.x*2 : 0.0f); // Include collapser ItemSize(ImVec2(text_width, frame_height), text_base_offset_y); // For regular tree nodes, we arbitrary allow to click past 2 worth of ItemSpacing // (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) 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); bool is_open = TreeNodeBehaviorIsOpen(id, flags); bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0; // Store a flag for the current depth to tell if we will allow closing this node when navigating one of its child. // For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop(). // This is currently only support 32 level deep and we are fine with (1 << Depth) overflowing into a zero. if (is_open && !g.NavIdIsAlive && (flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) window->DC.TreeStoreMayJumpToParentOnPop |= (1 << window->DC.TreeDepth); bool item_add = ItemAdd(interact_bb, id); window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HasDisplayRect; window->DC.LastItemDisplayRect = frame_bb; if (!item_add) { if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) TreePushOverrideID(id); IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); return is_open; } // Flags that affects opening behavior: // - 0 (default) .................... single-click anywhere to open // - OpenOnDoubleClick .............. double-click anywhere to open // - OpenOnArrow .................... single-click on arrow to open // - OpenOnDoubleClick|OpenOnArrow .. single-click on arrow or double-click anywhere to open ImGuiButtonFlags button_flags = ImGuiButtonFlags_NoKeyModifiers; if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) button_flags |= ImGuiButtonFlags_AllowItemOverlap; if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) button_flags |= ImGuiButtonFlags_PressedOnDoubleClick | ((flags & ImGuiTreeNodeFlags_OpenOnArrow) ? ImGuiButtonFlags_PressedOnClickRelease : 0); if (!is_leaf) button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; bool selected = (flags & ImGuiTreeNodeFlags_Selected) != 0; const bool was_selected = selected; bool hovered, held; bool pressed = ButtonBehavior(interact_bb, id, &hovered, &held, button_flags); bool toggled = false; if (!is_leaf) { if (pressed) { toggled = !(flags & (ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) || (g.NavActivateId == id); if (flags & ImGuiTreeNodeFlags_OpenOnArrow) toggled |= IsMouseHoveringRect(interact_bb.Min, ImVec2(interact_bb.Min.x + text_offset_x, interact_bb.Max.y)) && (!g.NavDisableMouseHover); if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) toggled |= g.IO.MouseDoubleClicked[0]; 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. toggled = false; } if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Left && is_open) { toggled = true; NavMoveRequestCancel(); } 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? { toggled = true; NavMoveRequestCancel(); } if (toggled) { is_open = !is_open; window->DC.StateStorage->SetInt(id, is_open); } } if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) SetItemAllowOverlap(); // In this branch, TreeNodeBehavior() cannot toggle the selection so this will never trigger. if (selected != was_selected) //-V547 window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledSelection; // Render const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); const ImVec2 text_pos = frame_bb.Min + ImVec2(text_offset_x, text_base_offset_y); ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_TypeThin; if (display_frame) { // Framed type RenderFrame(frame_bb.Min, frame_bb.Max, col, true, style.FrameRounding); RenderNavHighlight(frame_bb, id, nav_highlight_flags); RenderArrow(frame_bb.Min + ImVec2(padding.x, text_base_offset_y), is_open ? ImGuiDir_Down : ImGuiDir_Right, 1.0f); if (g.LogEnabled) { // 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. const char log_prefix[] = "\n##"; const char log_suffix[] = "##"; LogRenderedText(&text_pos, log_prefix, log_prefix+3); RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); LogRenderedText(&text_pos, log_suffix, log_suffix+2); } else { RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); } } else { // Unframed typed for tree nodes if (hovered || selected) { RenderFrame(frame_bb.Min, frame_bb.Max, col, false); RenderNavHighlight(frame_bb, id, nav_highlight_flags); } if (flags & ImGuiTreeNodeFlags_Bullet) RenderBullet(frame_bb.Min + ImVec2(text_offset_x * 0.5f, g.FontSize*0.50f + text_base_offset_y)); else if (!is_leaf) RenderArrow(frame_bb.Min + ImVec2(padding.x, g.FontSize*0.15f + text_base_offset_y), is_open ? ImGuiDir_Down : ImGuiDir_Right, 0.70f); if (g.LogEnabled) LogRenderedText(&text_pos, ">"); RenderText(text_pos, label, label_end, false); } if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) TreePushOverrideID(id); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); return is_open; } void ImGui::TreePush(const char* str_id) { ImGuiWindow* window = GetCurrentWindow(); Indent(); window->DC.TreeDepth++; PushID(str_id ? str_id : "#TreePush"); } void ImGui::TreePush(const void* ptr_id) { ImGuiWindow* window = GetCurrentWindow(); Indent(); window->DC.TreeDepth++; PushID(ptr_id ? ptr_id : (const void*)"#TreePush"); } void ImGui::TreePushOverrideID(ImGuiID id) { ImGuiWindow* window = GetCurrentWindow(); Indent(); window->DC.TreeDepth++; window->IDStack.push_back(id); } void ImGui::TreePop() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; Unindent(); window->DC.TreeDepth--; if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) if (g.NavIdIsAlive && (window->DC.TreeStoreMayJumpToParentOnPop & (1 << window->DC.TreeDepth))) { SetNavID(window->IDStack.back(), g.NavLayer); NavMoveRequestCancel(); } window->DC.TreeStoreMayJumpToParentOnPop &= (1 << window->DC.TreeDepth) - 1; 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. PopID(); } void ImGui::TreeAdvanceToLabelPos() { ImGuiContext& g = *GImGui; g.CurrentWindow->DC.CursorPos.x += GetTreeNodeToLabelSpacing(); } // Horizontal distance preceding label when using TreeNode() or Bullet() float ImGui::GetTreeNodeToLabelSpacing() { ImGuiContext& g = *GImGui; return g.FontSize + (g.Style.FramePadding.x * 2.0f); } void ImGui::SetNextTreeNodeOpen(bool is_open, ImGuiCond cond) { ImGuiContext& g = *GImGui; if (g.CurrentWindow->SkipItems) return; g.NextTreeNodeOpenVal = is_open; g.NextTreeNodeOpenCond = cond ? cond : ImGuiCond_Always; } // CollapsingHeader returns true when opened but do not indent nor push into the ID stack (because of the ImGuiTreeNodeFlags_NoTreePushOnOpen flag). // 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(). bool ImGui::CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; return TreeNodeBehavior(window->GetID(label), flags | ImGuiTreeNodeFlags_CollapsingHeader, label); } bool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; if (p_open && !*p_open) return false; ImGuiID id = window->GetID(label); bool is_open = TreeNodeBehavior(id, flags | ImGuiTreeNodeFlags_CollapsingHeader | (p_open ? ImGuiTreeNodeFlags_AllowItemOverlap : 0), label); if (p_open) { // Create a small overlapping close button // FIXME: We can evolve this into user accessible helpers to add extra buttons on title bars, headers, etc. ImGuiContext& g = *GImGui; ImGuiItemHoveredDataBackup last_item_backup; float button_radius = g.FontSize * 0.5f; 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); if (CloseButton(window->GetID((void*)((intptr_t)id+1)), button_center, button_radius)) *p_open = false; last_item_backup.Restore(); } return is_open; } //------------------------------------------------------------------------- // [SECTION] Widgets: Selectable //------------------------------------------------------------------------- // - Selectable() //------------------------------------------------------------------------- // Tip: pass a non-visible label (e.g. "##dummy") then you can use the space to draw other text or image. // But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID or use ##unique_id. bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags flags, const ImVec2& size_arg) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) // FIXME-OPT: Avoid if vertically clipped. PopClipRect(); ImGuiID id = window->GetID(label); ImVec2 label_size = CalcTextSize(label, NULL, true); ImVec2 size(size_arg.x != 0.0f ? size_arg.x : label_size.x, size_arg.y != 0.0f ? size_arg.y : label_size.y); ImVec2 pos = window->DC.CursorPos; pos.y += window->DC.CurrentLineTextBaseOffset; ImRect bb_inner(pos, pos + size); ItemSize(size); // Fill horizontal space. ImVec2 window_padding = window->WindowPadding; float max_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? GetWindowContentRegionMax().x : GetContentRegionMax().x; float w_draw = ImMax(label_size.x, window->Pos.x + max_x - window_padding.x - pos.x); 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); ImRect bb(pos, pos + size_draw); if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_DrawFillAvailWidth)) bb.Max.x += window_padding.x; // Selectables are tightly packed together so we extend the box to cover spacing between selectable. const float spacing_x = style.ItemSpacing.x; const float spacing_y = style.ItemSpacing.y; const float spacing_L = (float)(int)(spacing_x * 0.50f); const float spacing_U = (float)(int)(spacing_y * 0.50f); bb.Min.x -= spacing_L; bb.Min.y -= spacing_U; bb.Max.x += (spacing_x - spacing_L); bb.Max.y += (spacing_y - spacing_U); bool item_add; if (flags & ImGuiSelectableFlags_Disabled) { ImGuiItemFlags backup_item_flags = window->DC.ItemFlags; window->DC.ItemFlags |= ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNavDefaultFocus; item_add = ItemAdd(bb, id); window->DC.ItemFlags = backup_item_flags; } else { item_add = ItemAdd(bb, id); } if (!item_add) { if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) PushColumnClipRect(); return false; } // We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries ImGuiButtonFlags button_flags = 0; if (flags & ImGuiSelectableFlags_NoHoldingActiveID) button_flags |= ImGuiButtonFlags_NoHoldingActiveID; if (flags & ImGuiSelectableFlags_PressedOnClick) button_flags |= ImGuiButtonFlags_PressedOnClick; if (flags & ImGuiSelectableFlags_PressedOnRelease) button_flags |= ImGuiButtonFlags_PressedOnRelease; if (flags & ImGuiSelectableFlags_Disabled) button_flags |= ImGuiButtonFlags_Disabled; if (flags & ImGuiSelectableFlags_AllowDoubleClick) button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; if (flags & ImGuiSelectableFlags_AllowItemOverlap) button_flags |= ImGuiButtonFlags_AllowItemOverlap; if (flags & ImGuiSelectableFlags_Disabled) selected = false; const bool was_selected = selected; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); // Hovering selectable with mouse updates NavId accordingly so navigation can be resumed with gamepad/keyboard (this doesn't happen on most widgets) if (pressed || hovered) if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent) { g.NavDisableHighlight = true; SetNavID(id, window->DC.NavLayerCurrent); } if (pressed) MarkItemEdited(id); if (flags & ImGuiSelectableFlags_AllowItemOverlap) SetItemAllowOverlap(); // In this branch, Selectable() cannot toggle the selection so this will never trigger. if (selected != was_selected) //-V547 window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledSelection; // Render if (hovered || selected) { const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); RenderFrame(bb.Min, bb.Max, col, false, 0.0f); RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); } if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) { PushColumnClipRect(); bb.Max.x -= (GetContentRegionMax().x - max_x); } if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); RenderTextClipped(bb_inner.Min, bb_inner.Max, label, NULL, &label_size, style.SelectableTextAlign, &bb); if (flags & ImGuiSelectableFlags_Disabled) PopStyleColor(); // Automatically close popups if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(window->DC.ItemFlags & ImGuiItemFlags_SelectableDontClosePopup)) CloseCurrentPopup(); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); return pressed; } bool ImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags, const ImVec2& size_arg) { if (Selectable(label, *p_selected, flags, size_arg)) { *p_selected = !*p_selected; return true; } return false; } //------------------------------------------------------------------------- // [SECTION] Widgets: ListBox //------------------------------------------------------------------------- // - ListBox() // - ListBoxHeader() // - ListBoxFooter() //------------------------------------------------------------------------- // FIXME: This is an old API. We should redesign some of it, rename ListBoxHeader->BeginListBox, ListBoxFooter->EndListBox // and promote using them over existing ListBox() functions, similarly to change with combo boxes. //------------------------------------------------------------------------- // FIXME: In principle this function should be called BeginListBox(). We should rename it after re-evaluating if we want to keep the same signature. // Helper to calculate the size of a listbox and display a label on the right. // Tip: To have a list filling the entire window width, PushItemWidth(-1) and pass an non-visible label e.g. "##empty" bool ImGui::ListBoxHeader(const char* label, const ImVec2& size_arg) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; const ImGuiStyle& style = GetStyle(); const ImGuiID id = GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); // Size default to hold ~7 items. Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar. ImVec2 size = CalcItemSize(size_arg, GetNextItemWidth(), GetTextLineHeightWithSpacing() * 7.4f + style.ItemSpacing.y); ImVec2 frame_size = ImVec2(size.x, ImMax(size.y, label_size.y)); ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); window->DC.LastItemRect = bb; // Forward storage for ListBoxFooter.. dodgy. if (!IsRectVisible(bb.Min, bb.Max)) { ItemSize(bb.GetSize(), style.FramePadding.y); ItemAdd(bb, 0, &frame_bb); return false; } BeginGroup(); if (label_size.x > 0) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); BeginChildFrame(id, frame_bb.GetSize()); return true; } // FIXME: In principle this function should be called EndListBox(). We should rename it after re-evaluating if we want to keep the same signature. bool ImGui::ListBoxHeader(const char* label, int items_count, int height_in_items) { // Size default to hold ~7.25 items. // 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. // 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. // I am expecting that someone will come and complain about this behavior in a remote future, then we can advise on a better solution. if (height_in_items < 0) height_in_items = ImMin(items_count, 7); const ImGuiStyle& style = GetStyle(); float height_in_items_f = (height_in_items < items_count) ? (height_in_items + 0.25f) : (height_in_items + 0.00f); // 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(). ImVec2 size; size.x = 0.0f; size.y = GetTextLineHeightWithSpacing() * height_in_items_f + style.FramePadding.y * 2.0f; return ListBoxHeader(label, size); } // FIXME: In principle this function should be called EndListBox(). We should rename it after re-evaluating if we want to keep the same signature. void ImGui::ListBoxFooter() { ImGuiWindow* parent_window = GetCurrentWindow()->ParentWindow; const ImRect bb = parent_window->DC.LastItemRect; const ImGuiStyle& style = GetStyle(); EndChildFrame(); // Redeclare item size so that it includes the label (we have stored the full size in LastItemRect) // We call SameLine() to restore DC.CurrentLine* data SameLine(); parent_window->DC.CursorPos = bb.Min; ItemSize(bb, style.FramePadding.y); EndGroup(); } bool ImGui::ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_items) { const bool value_changed = ListBox(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_items); return value_changed; } bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items) { if (!ListBoxHeader(label, items_count, height_in_items)) return false; // 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. ImGuiContext& g = *GImGui; bool value_changed = false; 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. while (clipper.Step()) for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) { const bool item_selected = (i == *current_item); const char* item_text; if (!items_getter(data, i, &item_text)) item_text = "*Unknown item*"; PushID(i); if (Selectable(item_text, item_selected)) { *current_item = i; value_changed = true; } if (item_selected) SetItemDefaultFocus(); PopID(); } ListBoxFooter(); if (value_changed) MarkItemEdited(g.CurrentWindow->DC.LastItemId); return value_changed; } //------------------------------------------------------------------------- // [SECTION] Widgets: PlotLines, PlotHistogram //------------------------------------------------------------------------- // - PlotEx() [Internal] // - PlotLines() // - PlotHistogram() //------------------------------------------------------------------------- void 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) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); if (frame_size.x == 0.0f) frame_size.x = GetNextItemWidth(); if (frame_size.y == 0.0f) frame_size.y = label_size.y + (style.FramePadding.y * 2); const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); 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)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, 0, &frame_bb)) return; const bool hovered = ItemHoverable(frame_bb, id); // Determine scale from values if not specified if (scale_min == FLT_MAX || scale_max == FLT_MAX) { float v_min = FLT_MAX; float v_max = -FLT_MAX; for (int i = 0; i < values_count; i++) { const float v = values_getter(data, i); if (v != v) // Ignore NaN values continue; v_min = ImMin(v_min, v); v_max = ImMax(v_max, v); } if (scale_min == FLT_MAX) scale_min = v_min; if (scale_max == FLT_MAX) scale_max = v_max; } RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); const int values_count_min = (plot_type == ImGuiPlotType_Lines) ? 2 : 1; if (values_count >= values_count_min) { int res_w = ImMin((int)frame_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); int item_count = values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); // Tooltip on hover int v_hovered = -1; if (hovered && inner_bb.Contains(g.IO.MousePos)) { const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f); const int v_idx = (int)(t * item_count); IM_ASSERT(v_idx >= 0 && v_idx < values_count); const float v0 = values_getter(data, (v_idx + values_offset) % values_count); const float v1 = values_getter(data, (v_idx + 1 + values_offset) % values_count); if (plot_type == ImGuiPlotType_Lines) SetTooltip("%d: %8.4g\n%d: %8.4g", v_idx, v0, v_idx+1, v1); else if (plot_type == ImGuiPlotType_Histogram) SetTooltip("%d: %8.4g", v_idx, v0); v_hovered = v_idx; } const float t_step = 1.0f / (float)res_w; const float inv_scale = (scale_min == scale_max) ? 0.0f : (1.0f / (scale_max - scale_min)); float v0 = values_getter(data, (0 + values_offset) % values_count); float t0 = 0.0f; ImVec2 tp0 = ImVec2( t0, 1.0f - ImSaturate((v0 - scale_min) * inv_scale) ); // Point in the normalized space of our target rectangle 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 const ImU32 col_base = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLines : ImGuiCol_PlotHistogram); const ImU32 col_hovered = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLinesHovered : ImGuiCol_PlotHistogramHovered); for (int n = 0; n < res_w; n++) { const float t1 = t0 + t_step; const int v1_idx = (int)(t0 * item_count + 0.5f); IM_ASSERT(v1_idx >= 0 && v1_idx < values_count); const float v1 = values_getter(data, (v1_idx + values_offset + 1) % values_count); const ImVec2 tp1 = ImVec2( t1, 1.0f - ImSaturate((v1 - scale_min) * inv_scale) ); // 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. ImVec2 pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0); ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, (plot_type == ImGuiPlotType_Lines) ? tp1 : ImVec2(tp1.x, histogram_zero_line_t)); if (plot_type == ImGuiPlotType_Lines) { window->DrawList->AddLine(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base); } else if (plot_type == ImGuiPlotType_Histogram) { if (pos1.x >= pos0.x + 2.0f) pos1.x -= 1.0f; window->DrawList->AddRectFilled(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base); } t0 = t1; tp0 = tp1; } } // Text overlay if (overlay_text) 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)); if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label); } struct ImGuiPlotArrayGetterData { const float* Values; int Stride; ImGuiPlotArrayGetterData(const float* values, int stride) { Values = values; Stride = stride; } }; static float Plot_ArrayGetter(void* data, int idx) { ImGuiPlotArrayGetterData* plot_data = (ImGuiPlotArrayGetterData*)data; const float v = *(const float*)(const void*)((const unsigned char*)plot_data->Values + (size_t)idx * plot_data->Stride); return v; } void 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) { ImGuiPlotArrayGetterData data(values, stride); PlotEx(ImGuiPlotType_Lines, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size); } void 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) { PlotEx(ImGuiPlotType_Lines, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size); } void 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) { ImGuiPlotArrayGetterData data(values, stride); PlotEx(ImGuiPlotType_Histogram, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size); } void 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) { PlotEx(ImGuiPlotType_Histogram, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size); } //------------------------------------------------------------------------- // [SECTION] Widgets: Value helpers // Those is not very useful, legacy API. //------------------------------------------------------------------------- // - Value() //------------------------------------------------------------------------- void ImGui::Value(const char* prefix, bool b) { Text("%s: %s", prefix, (b ? "true" : "false")); } void ImGui::Value(const char* prefix, int v) { Text("%s: %d", prefix, v); } void ImGui::Value(const char* prefix, unsigned int v) { Text("%s: %d", prefix, v); } void ImGui::Value(const char* prefix, float v, const char* float_format) { if (float_format) { char fmt[64]; ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%s: %s", float_format); Text(fmt, prefix, v); } else { Text("%s: %.3f", prefix, v); } } //------------------------------------------------------------------------- // [SECTION] MenuItem, BeginMenu, EndMenu, etc. //------------------------------------------------------------------------- // - ImGuiMenuColumns [Internal] // - BeginMainMenuBar() // - EndMainMenuBar() // - BeginMenuBar() // - EndMenuBar() // - BeginMenu() // - EndMenu() // - MenuItem() //------------------------------------------------------------------------- // Helpers for internal use ImGuiMenuColumns::ImGuiMenuColumns() { Spacing = Width = NextWidth = 0.0f; memset(Pos, 0, sizeof(Pos)); memset(NextWidths, 0, sizeof(NextWidths)); } void ImGuiMenuColumns::Update(int count, float spacing, bool clear) { IM_ASSERT(count == IM_ARRAYSIZE(Pos)); IM_UNUSED(count); Width = NextWidth = 0.0f; Spacing = spacing; if (clear) memset(NextWidths, 0, sizeof(NextWidths)); for (int i = 0; i < IM_ARRAYSIZE(Pos); i++) { if (i > 0 && NextWidths[i] > 0.0f) Width += Spacing; Pos[i] = (float)(int)Width; Width += NextWidths[i]; NextWidths[i] = 0.0f; } } float ImGuiMenuColumns::DeclColumns(float w0, float w1, float w2) // not using va_arg because they promote float to double { NextWidth = 0.0f; NextWidths[0] = ImMax(NextWidths[0], w0); NextWidths[1] = ImMax(NextWidths[1], w1); NextWidths[2] = ImMax(NextWidths[2], w2); for (int i = 0; i < IM_ARRAYSIZE(Pos); i++) NextWidth += NextWidths[i] + ((i > 0 && NextWidths[i] > 0.0f) ? Spacing : 0.0f); return ImMax(Width, NextWidth); } float ImGuiMenuColumns::CalcExtraSpace(float avail_w) { return ImMax(0.0f, avail_w - Width); } // For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set. bool ImGui::BeginMainMenuBar() { ImGuiContext& g = *GImGui; ImGuiViewport* viewport = g.Viewports[0]; g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f)); SetNextWindowPos(viewport->Pos); SetNextWindowSize(ImVec2(viewport->Size.x, g.NextWindowData.MenuBarOffsetMinVal.y + g.FontBaseSize + g.Style.FramePadding.y)); SetNextWindowViewport(viewport->ID); // Enforce viewport so we don't create our onw viewport when ImGuiConfigFlags_ViewportsNoMerge is set. PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0,0)); ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar; bool is_open = Begin("##MainMenuBar", NULL, window_flags) && BeginMenuBar(); PopStyleVar(2); g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f); if (!is_open) { End(); return false; } return true; //-V1020 } void ImGui::EndMainMenuBar() { EndMenuBar(); // When the user has left the menu layer (typically: closed menus through activation of an item), we restore focus to the previous window // FIXME: With this strategy we won't be able to restore a NULL focus. ImGuiContext& g = *GImGui; if (g.CurrentWindow == g.NavWindow && g.NavLayer == 0) FocusTopMostWindowUnderOne(g.NavWindow, NULL); End(); } bool ImGui::BeginMenuBar() { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; if (!(window->Flags & ImGuiWindowFlags_MenuBar)) return false; IM_ASSERT(!window->DC.MenuBarAppending); BeginGroup(); // Backup position on layer 0 PushID("##menubar"); // 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. // 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. ImRect bar_rect = window->MenuBarRect(); 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)); clip_rect.ClipWith(window->OuterRectClipped); PushClipRect(clip_rect.Min, clip_rect.Max, false); window->DC.CursorPos = ImVec2(bar_rect.Min.x + window->DC.MenuBarOffset.x, bar_rect.Min.y + window->DC.MenuBarOffset.y); window->DC.LayoutType = ImGuiLayoutType_Horizontal; window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); window->DC.MenuBarAppending = true; AlignTextToFramePadding(); return true; } void ImGui::EndMenuBar() { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; ImGuiContext& g = *GImGui; // Nav: When a move request within one of our child menu failed, capture the request to navigate among our siblings. if (NavMoveRequestButNoResultYet() && (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) && (g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu)) { ImGuiWindow* nav_earliest_child = g.NavWindow; while (nav_earliest_child->ParentWindow && (nav_earliest_child->ParentWindow->Flags & ImGuiWindowFlags_ChildMenu)) nav_earliest_child = nav_earliest_child->ParentWindow; if (nav_earliest_child->ParentWindow == window && nav_earliest_child->DC.ParentLayoutType == ImGuiLayoutType_Horizontal && g.NavMoveRequestForward == ImGuiNavForward_None) { // To do so we claim focus back, restore NavId and then process the movement request for yet another frame. // 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) const ImGuiNavLayer layer = ImGuiNavLayer_Menu; IM_ASSERT(window->DC.NavLayerActiveMaskNext & (1 << layer)); // Sanity check FocusWindow(window); SetNavIDWithRectRel(window->NavLastIds[layer], layer, window->NavRectRel[layer]); g.NavLayer = layer; g.NavDisableHighlight = true; // Hide highlight for the current frame so we don't see the intermediary selection. g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued; NavMoveRequestCancel(); } } IM_ASSERT(window->Flags & ImGuiWindowFlags_MenuBar); IM_ASSERT(window->DC.MenuBarAppending); PopClipRect(); PopID(); 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. window->DC.GroupStack.back().AdvanceCursor = false; EndGroup(); // Restore position on layer 0 window->DC.LayoutType = ImGuiLayoutType_Vertical; window->DC.NavLayerCurrent = ImGuiNavLayer_Main; window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); window->DC.MenuBarAppending = false; } bool ImGui::BeginMenu(const char* label, bool enabled) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); ImVec2 label_size = CalcTextSize(label, NULL, true); bool pressed; bool menu_is_open = IsPopupOpen(id); bool menuset_is_open = !(window->Flags & ImGuiWindowFlags_Popup) && (g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].OpenParentId == window->IDStack.back()); ImGuiWindow* backed_nav_window = g.NavWindow; if (menuset_is_open) g.NavWindow = window; // Odd hack to allow hovering across menus of a same menu-set (otherwise we wouldn't be able to hover parent) // The reference position stored in popup_pos will be used by Begin() to find a suitable position for the child menu, // However the final position is going to be different! It is choosen by FindBestWindowPosForPopup(). // e.g. Menus tend to overlap each other horizontally to amplify relative Z-ordering. ImVec2 popup_pos, pos = window->DC.CursorPos; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { // Menu inside an horizontal menu bar // Selectable extend their highlight by half ItemSpacing in each direction. // For ChildMenu, the popup position will be overwritten by the call to FindBestWindowPosForPopup() in Begin() popup_pos = ImVec2(pos.x - 1.0f - (float)(int)(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight()); window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); float w = label_size.x; pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_PressedOnClick | ImGuiSelectableFlags_DontClosePopups | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); PopStyleVar(); 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(). } else { // Menu inside a menu popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y); float w = window->MenuColumns.DeclColumns(label_size.x, 0.0f, (float)(int)(g.FontSize * 1.20f)); // Feedback to next frame float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w); pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_PressedOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_DrawFillAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); if (!enabled) PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); RenderArrow(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.30f, 0.0f), ImGuiDir_Right); if (!enabled) PopStyleColor(); } const bool hovered = enabled && ItemHoverable(window->DC.LastItemRect, id); if (menuset_is_open) g.NavWindow = backed_nav_window; bool want_open = false; bool want_close = false; if (window->DC.LayoutType == ImGuiLayoutType_Vertical) // (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu)) { // Close menu when not hovering it anymore unless we are moving roughly in the direction of the menu // Implement http://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown to avoid using timers, so menus feels more reactive. bool moving_toward_other_child_menu = false; ImGuiWindow* child_menu_window = (g.BeginPopupStack.Size < g.OpenPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].SourceWindow == window) ? g.OpenPopupStack[g.BeginPopupStack.Size].Window : NULL; if (g.HoveredWindow == window && child_menu_window != NULL && !(window->Flags & ImGuiWindowFlags_MenuBar)) { // FIXME-DPI: Values should be derived from a master "scale" factor. ImRect next_window_rect = child_menu_window->Rect(); ImVec2 ta = g.IO.MousePos - g.IO.MouseDelta; ImVec2 tb = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetTL() : next_window_rect.GetTR(); ImVec2 tc = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetBL() : next_window_rect.GetBR(); float extra = ImClamp(ImFabs(ta.x - tb.x) * 0.30f, 5.0f, 30.0f); // add a bit of extra slack. ta.x += (window->Pos.x < child_menu_window->Pos.x) ? -0.5f : +0.5f; // to avoid numerical issues 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? tc.y = ta.y + ImMin((tc.y + extra) - ta.y, +100.0f); moving_toward_other_child_menu = ImTriangleContainsPoint(ta, tb, tc, g.IO.MousePos); //GetForegroundDrawList()->AddTriangleFilled(ta, tb, tc, moving_within_opened_triangle ? IM_COL32(0,128,0,128) : IM_COL32(128,0,0,128)); // [DEBUG] } if (menu_is_open && !hovered && g.HoveredWindow == window && g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrame != id && !moving_toward_other_child_menu) want_close = true; if (!menu_is_open && hovered && pressed) // Click to open want_open = true; else if (!menu_is_open && hovered && !moving_toward_other_child_menu) // Hover to open want_open = true; if (g.NavActivateId == id) { want_close = menu_is_open; want_open = !menu_is_open; } if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Right) // Nav-Right to open { want_open = true; NavMoveRequestCancel(); } } else { // Menu bar if (menu_is_open && pressed && menuset_is_open) // Click an open menu again to close it { want_close = true; want_open = menu_is_open = false; } else if (pressed || (hovered && menuset_is_open && !menu_is_open)) // First click to open, then hover to open others { want_open = true; } else if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Down) // Nav-Down to open { want_open = true; NavMoveRequestCancel(); } } 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.. }' want_close = true; if (want_close && IsPopupOpen(id)) ClosePopupToLevel(g.BeginPopupStack.Size, true); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Openable | (menu_is_open ? ImGuiItemStatusFlags_Opened : 0)); if (!menu_is_open && want_open && g.OpenPopupStack.Size > g.BeginPopupStack.Size) { // Don't recycle same menu level in the same frame, first close the other menu and yield for a frame. OpenPopup(label); return false; } menu_is_open |= want_open; if (want_open) OpenPopup(label); if (menu_is_open) { // 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) SetNextWindowPos(popup_pos, ImGuiCond_Always); ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus; if (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu)) flags |= ImGuiWindowFlags_ChildWindow; menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) } return menu_is_open; } void ImGui::EndMenu() { // Nav: When a left move request _within our child menu_ failed, close ourselves (the _parent_ menu). // A menu doesn't close itself because EndMenuBar() wants the catch the last Left<>Right inputs. // 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. ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (g.NavWindow && g.NavWindow->ParentWindow == window && g.NavMoveDir == ImGuiDir_Left && NavMoveRequestButNoResultYet() && window->DC.LayoutType == ImGuiLayoutType_Vertical) { ClosePopupToLevel(g.BeginPopupStack.Size, true); NavMoveRequestCancel(); } EndPopup(); } bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, bool enabled) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; ImGuiContext& g = *GImGui; ImGuiStyle& style = g.Style; ImVec2 pos = window->DC.CursorPos; ImVec2 label_size = CalcTextSize(label, NULL, true); ImGuiSelectableFlags flags = ImGuiSelectableFlags_PressedOnRelease | (enabled ? 0 : ImGuiSelectableFlags_Disabled); bool pressed; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { // Mimic the exact layout spacing of BeginMenu() to allow MenuItem() inside a menu bar, which is a little misleading but may be useful // Note that in this situation we render neither the shortcut neither the selected tick mark float w = label_size.x; window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); pressed = Selectable(label, false, flags, ImVec2(w, 0.0f)); PopStyleVar(); 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(). } else { ImVec2 shortcut_size = shortcut ? CalcTextSize(shortcut, NULL) : ImVec2(0.0f, 0.0f); float w = window->MenuColumns.DeclColumns(label_size.x, shortcut_size.x, (float)(int)(g.FontSize * 1.20f)); // Feedback for next frame float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w); pressed = Selectable(label, false, flags | ImGuiSelectableFlags_DrawFillAvailWidth, ImVec2(w, 0.0f)); if (shortcut_size.x > 0.0f) { PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); RenderText(pos + ImVec2(window->MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false); PopStyleColor(); } if (selected) 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); } IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0)); return pressed; } bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled) { if (MenuItem(label, shortcut, p_selected ? *p_selected : false, enabled)) { if (p_selected) *p_selected = !*p_selected; return true; } return false; } //------------------------------------------------------------------------- // [SECTION] Widgets: BeginTabBar, EndTabBar, etc. //------------------------------------------------------------------------- // [BETA API] API may evolve! //------------------------------------------------------------------------- // - BeginTabBar() // - BeginTabBarEx() [Internal] // - EndTabBar() // - TabBarLayout() [Internal] // - TabBarCalcTabID() [Internal] // - TabBarCalcMaxTabWidth() [Internal] // - TabBarFindTabById() [Internal] // - TabBarAddTab() [Internal] // - TabBarRemoveTab() [Internal] // - TabBarCloseTab() [Internal] // - TabBarScrollClamp()v // - TabBarScrollToTab() [Internal] // - TabBarQueueChangeTabOrder() [Internal] // - TabBarScrollingButtons() [Internal] // - TabBarTabListPopupButton() [Internal] //------------------------------------------------------------------------- namespace ImGui { static void TabBarLayout(ImGuiTabBar* tab_bar); static ImU32 TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label); static float TabBarCalcMaxTabWidth(); static float TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling); static void TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); static ImGuiTabItem* TabBarScrollingButtons(ImGuiTabBar* tab_bar); static ImGuiTabItem* TabBarTabListPopupButton(ImGuiTabBar* tab_bar); } ImGuiTabBar::ImGuiTabBar() { ID = 0; SelectedTabId = NextSelectedTabId = VisibleTabId = 0; CurrFrameVisible = PrevFrameVisible = -1; ContentsHeight = 0.0f; OffsetMax = OffsetNextTab = 0.0f; ScrollingAnim = ScrollingTarget = ScrollingTargetDistToVisibility = ScrollingSpeed = 0.0f; Flags = ImGuiTabBarFlags_None; ReorderRequestTabId = 0; ReorderRequestDir = 0; WantLayout = VisibleTabWasSubmitted = false; LastTabItemIdx = -1; } static int IMGUI_CDECL TabItemComparerByVisibleOffset(const void* lhs, const void* rhs) { const ImGuiTabItem* a = (const ImGuiTabItem*)lhs; const ImGuiTabItem* b = (const ImGuiTabItem*)rhs; return (int)(a->Offset - b->Offset); } static int IMGUI_CDECL TabBarSortItemComparer(const void* lhs, const void* rhs) { const ImGuiTabBarSortItem* a = (const ImGuiTabBarSortItem*)lhs; const ImGuiTabBarSortItem* b = (const ImGuiTabBarSortItem*)rhs; if (int d = (int)(b->Width - a->Width)) return d; return (b->Index - a->Index); } static ImGuiTabBar* GetTabBarFromTabBarRef(const ImGuiTabBarRef& ref) { ImGuiContext& g = *GImGui; return ref.Ptr ? ref.Ptr : g.TabBars.GetByIndex(ref.IndexInMainPool); } static ImGuiTabBarRef GetTabBarRefFromTabBar(ImGuiTabBar* tab_bar) { ImGuiContext& g = *GImGui; if (g.TabBars.Contains(tab_bar)) return ImGuiTabBarRef(g.TabBars.GetIndex(tab_bar)); return ImGuiTabBarRef(tab_bar); } bool ImGui::BeginTabBar(const char* str_id, ImGuiTabBarFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return false; ImGuiID id = window->GetID(str_id); ImGuiTabBar* tab_bar = g.TabBars.GetOrAddByKey(id); 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); tab_bar->ID = id; return BeginTabBarEx(tab_bar, tab_bar_bb, flags | ImGuiTabBarFlags_IsFocused, NULL); } bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImGuiTabBarFlags flags, ImGuiDockNode* dock_node) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return false; if ((flags & ImGuiTabBarFlags_DockNode) == 0) PushOverrideID(tab_bar->ID); // Add to stack g.CurrentTabBarStack.push_back(GetTabBarRefFromTabBar(tab_bar)); g.CurrentTabBar = tab_bar; if (tab_bar->CurrFrameVisible == g.FrameCount) { //IMGUI_DEBUG_LOG("BeginTabBarEx already called this frame\n", g.FrameCount); //IM_ASSERT(0); return true; } // When toggling back from ordered to manually-reorderable, shuffle tabs to enforce the last visible order. // 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. if ((flags & ImGuiTabBarFlags_Reorderable) && !(tab_bar->Flags & ImGuiTabBarFlags_Reorderable) && tab_bar->Tabs.Size > 1 && tab_bar->PrevFrameVisible != -1) ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerByVisibleOffset); // Flags if ((flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0) flags |= ImGuiTabBarFlags_FittingPolicyDefault_; tab_bar->Flags = flags; tab_bar->BarRect = tab_bar_bb; tab_bar->WantLayout = true; // Layout will be done on the first call to ItemTab() tab_bar->PrevFrameVisible = tab_bar->CurrFrameVisible; tab_bar->CurrFrameVisible = g.FrameCount; tab_bar->FramePadding = g.Style.FramePadding; // Layout ItemSize(ImVec2(tab_bar->OffsetMax, tab_bar->BarRect.GetHeight())); window->DC.CursorPos.x = tab_bar->BarRect.Min.x; // Draw separator const ImU32 col = GetColorU32((flags & ImGuiTabBarFlags_IsFocused) ? ImGuiCol_TabActive : ImGuiCol_Tab); const float y = tab_bar->BarRect.Max.y - 1.0f; if (dock_node != NULL) { const float separator_min_x = dock_node->Pos.x + window->WindowBorderSize; const float separator_max_x = dock_node->Pos.x + dock_node->Size.x - window->WindowBorderSize; window->DrawList->AddLine(ImVec2(separator_min_x, y), ImVec2(separator_max_x, y), col, 1.0f); } else { const float separator_min_x = tab_bar->BarRect.Min.x - window->WindowPadding.x; const float separator_max_x = tab_bar->BarRect.Max.x + window->WindowPadding.x; window->DrawList->AddLine(ImVec2(separator_min_x, y), ImVec2(separator_max_x, y), col, 1.0f); } return true; } void ImGui::EndTabBar() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return; ImGuiTabBar* tab_bar = g.CurrentTabBar; if (tab_bar == NULL) { IM_ASSERT(tab_bar != NULL && "Mismatched BeginTabBar()/EndTabBar()!"); return; // FIXME-ERRORHANDLING } if (tab_bar->WantLayout) TabBarLayout(tab_bar); // Restore the last visible height if no tab is visible, this reduce vertical flicker/movement when a tabs gets removed without calling SetTabItemClosed(). const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); if (tab_bar->VisibleTabWasSubmitted || tab_bar->VisibleTabId == 0 || tab_bar_appearing) tab_bar->ContentsHeight = ImMax(window->DC.CursorPos.y - tab_bar->BarRect.Max.y, 0.0f); else window->DC.CursorPos.y = tab_bar->BarRect.Max.y + tab_bar->ContentsHeight; if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) PopID(); g.CurrentTabBarStack.pop_back(); g.CurrentTabBar = g.CurrentTabBarStack.empty() ? NULL : GetTabBarFromTabBarRef(g.CurrentTabBarStack.back()); } // This is called only once a frame before by the first call to ItemTab() // The reason we're not calling it in BeginTabBar() is to leave a chance to the user to call the SetTabItemClosed() functions. static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) { ImGuiContext& g = *GImGui; tab_bar->WantLayout = false; // Garbage collect int tab_dst_n = 0; for (int tab_src_n = 0; tab_src_n < tab_bar->Tabs.Size; tab_src_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_src_n]; if (tab->LastFrameVisible < tab_bar->PrevFrameVisible) { if (tab->ID == tab_bar->SelectedTabId) tab_bar->SelectedTabId = 0; continue; } if (tab_dst_n != tab_src_n) tab_bar->Tabs[tab_dst_n] = tab_bar->Tabs[tab_src_n]; tab_dst_n++; } if (tab_bar->Tabs.Size != tab_dst_n) tab_bar->Tabs.resize(tab_dst_n); // Setup next selected tab ImGuiID scroll_track_selected_tab_id = 0; if (tab_bar->NextSelectedTabId) { tab_bar->SelectedTabId = tab_bar->NextSelectedTabId; tab_bar->NextSelectedTabId = 0; scroll_track_selected_tab_id = tab_bar->SelectedTabId; } // Process order change request (we could probably process it when requested but it's just saner to do it in a single spot). if (tab_bar->ReorderRequestTabId != 0) { if (ImGuiTabItem* tab1 = TabBarFindTabByID(tab_bar, tab_bar->ReorderRequestTabId)) { //IM_ASSERT(tab_bar->Flags & ImGuiTabBarFlags_Reorderable); // <- this may happen when using debug tools int tab2_order = tab_bar->GetTabOrder(tab1) + tab_bar->ReorderRequestDir; if (tab2_order >= 0 && tab2_order < tab_bar->Tabs.Size) { ImGuiTabItem* tab2 = &tab_bar->Tabs[tab2_order]; ImGuiTabItem item_tmp = *tab1; *tab1 = *tab2; *tab2 = item_tmp; if (tab2->ID == tab_bar->SelectedTabId) scroll_track_selected_tab_id = tab2->ID; tab1 = tab2 = NULL; } if (tab_bar->Flags & ImGuiTabBarFlags_SaveSettings) MarkIniSettingsDirty(); } tab_bar->ReorderRequestTabId = 0; } // Tab List Popup (will alter tab_bar->BarRect and therefore the available width!) const bool tab_list_popup_button = (tab_bar->Flags & ImGuiTabBarFlags_TabListPopupButton) != 0; if (tab_list_popup_button) if (ImGuiTabItem* tab_to_select = TabBarTabListPopupButton(tab_bar)) // NB: Will alter BarRect.Max.x! scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID; ImVector& width_sort_buffer = g.TabSortByWidthBuffer; width_sort_buffer.resize(tab_bar->Tabs.Size); // Compute ideal widths float width_total_contents = 0.0f; ImGuiTabItem* most_recently_selected_tab = NULL; bool found_selected_tab_id = false; for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; IM_ASSERT(tab->LastFrameVisible >= tab_bar->PrevFrameVisible); if (most_recently_selected_tab == NULL || most_recently_selected_tab->LastFrameSelected < tab->LastFrameSelected) most_recently_selected_tab = tab; if (tab->ID == tab_bar->SelectedTabId) found_selected_tab_id = true; // Refresh tab width immediately, otherwise changes of style e.g. style.FramePadding.x would noticeably lag in the tab bar. // Additionally, when using TabBarAddTab() to manipulate tab bar order we occasionally insert new tabs that don't have a width yet, // and we cannot wait for the next BeginTabItem() call. We cannot compute this width within TabBarAddTab() because font size depends on the active window. const char* tab_name = tab_bar->GetTabName(tab); const bool has_close_button = tab->Window ? tab->Window->HasCloseButton : ((tab->Flags & ImGuiTabItemFlags_NoCloseButton) == 0); tab->WidthContents = TabItemCalcSize(tab_name, has_close_button).x; width_total_contents += (tab_n > 0 ? g.Style.ItemInnerSpacing.x : 0.0f) + tab->WidthContents; // Store data so we can build an array sorted by width if we need to shrink tabs down width_sort_buffer[tab_n].Index = tab_n; width_sort_buffer[tab_n].Width = tab->WidthContents; } // Compute width const float width_avail = tab_bar->BarRect.GetWidth(); float width_excess = (width_avail < width_total_contents) ? (width_total_contents - width_avail) : 0.0f; if (width_excess > 0.0f && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyResizeDown)) { // If we don't have enough room, resize down the largest tabs first if (tab_bar->Tabs.Size > 1) ImQsort(width_sort_buffer.Data, (size_t)width_sort_buffer.Size, sizeof(ImGuiTabBarSortItem), TabBarSortItemComparer); int tab_count_same_width = 1; while (width_excess > 0.0f && tab_count_same_width < tab_bar->Tabs.Size) { while (tab_count_same_width < tab_bar->Tabs.Size && width_sort_buffer[0].Width == width_sort_buffer[tab_count_same_width].Width) tab_count_same_width++; 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); float width_to_remove_per_tab = ImMin(width_excess / tab_count_same_width, width_to_remove_per_tab_max); for (int tab_n = 0; tab_n < tab_count_same_width; tab_n++) width_sort_buffer[tab_n].Width -= width_to_remove_per_tab; width_excess -= width_to_remove_per_tab * tab_count_same_width; } for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) tab_bar->Tabs[width_sort_buffer[tab_n].Index].Width = (float)(int)width_sort_buffer[tab_n].Width; } else { const float tab_max_width = TabBarCalcMaxTabWidth(); for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; tab->Width = ImMin(tab->WidthContents, tab_max_width); } } // Layout all active tabs float offset_x = 0.0f; for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; tab->Offset = offset_x; if (scroll_track_selected_tab_id == 0 && g.NavJustMovedToId == tab->ID) scroll_track_selected_tab_id = tab->ID; offset_x += tab->Width + g.Style.ItemInnerSpacing.x; } tab_bar->OffsetMax = ImMax(offset_x - g.Style.ItemInnerSpacing.x, 0.0f); tab_bar->OffsetNextTab = 0.0f; // Horizontal scrolling buttons 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); if (scrolling_buttons) if (ImGuiTabItem* tab_to_select = TabBarScrollingButtons(tab_bar)) // NB: Will alter BarRect.Max.x! scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID; // If we have lost the selected tab, select the next most recently active one if (found_selected_tab_id == false) tab_bar->SelectedTabId = 0; if (tab_bar->SelectedTabId == 0 && tab_bar->NextSelectedTabId == 0 && most_recently_selected_tab != NULL) scroll_track_selected_tab_id = tab_bar->SelectedTabId = most_recently_selected_tab->ID; // Lock in visible tab tab_bar->VisibleTabId = tab_bar->SelectedTabId; tab_bar->VisibleTabWasSubmitted = false; // CTRL+TAB can override visible tab temporarily if (g.NavWindowingTarget != NULL && g.NavWindowingTarget->DockNode && g.NavWindowingTarget->DockNode->TabBar == tab_bar) tab_bar->VisibleTabId = scroll_track_selected_tab_id = g.NavWindowingTarget->ID; // Update scrolling if (scroll_track_selected_tab_id) if (ImGuiTabItem* scroll_track_selected_tab = TabBarFindTabByID(tab_bar, scroll_track_selected_tab_id)) TabBarScrollToTab(tab_bar, scroll_track_selected_tab); tab_bar->ScrollingAnim = TabBarScrollClamp(tab_bar, tab_bar->ScrollingAnim); tab_bar->ScrollingTarget = TabBarScrollClamp(tab_bar, tab_bar->ScrollingTarget); if (tab_bar->ScrollingAnim != tab_bar->ScrollingTarget) { // Scrolling speed adjust itself so we can always reach our target in 1/3 seconds. // Teleport if we are aiming far off the visible line tab_bar->ScrollingSpeed = ImMax(tab_bar->ScrollingSpeed, 70.0f * g.FontSize); tab_bar->ScrollingSpeed = ImMax(tab_bar->ScrollingSpeed, ImFabs(tab_bar->ScrollingTarget - tab_bar->ScrollingAnim) / 0.3f); const bool teleport = (tab_bar->PrevFrameVisible + 1 < g.FrameCount) || (tab_bar->ScrollingTargetDistToVisibility > 10.0f * g.FontSize); tab_bar->ScrollingAnim = teleport ? tab_bar->ScrollingTarget : ImLinearSweep(tab_bar->ScrollingAnim, tab_bar->ScrollingTarget, g.IO.DeltaTime * tab_bar->ScrollingSpeed); } else { tab_bar->ScrollingSpeed = 0.0f; } // Clear name buffers if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) tab_bar->TabsNames.Buf.resize(0); } // Dockables uses Name/ID in the global namespace. Non-dockable items use the ID stack. static ImU32 ImGui::TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label) { if (tab_bar->Flags & ImGuiTabBarFlags_DockNode) { ImGuiID id = ImHashStr(label); KeepAliveID(id); return id; } else { ImGuiWindow* window = GImGui->CurrentWindow; return window->GetID(label); } } static float ImGui::TabBarCalcMaxTabWidth() { ImGuiContext& g = *GImGui; return g.FontSize * 20.0f; } ImGuiTabItem* ImGui::TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id) { if (tab_id != 0) for (int n = 0; n < tab_bar->Tabs.Size; n++) if (tab_bar->Tabs[n].ID == tab_id) return &tab_bar->Tabs[n]; return NULL; } // FIXME: See references to #2304 in TODO.txt ImGuiTabItem* ImGui::TabBarFindMostRecentlySelectedTabForActiveWindow(ImGuiTabBar* tab_bar) { ImGuiTabItem* most_recently_selected_tab = NULL; for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; if (most_recently_selected_tab == NULL || most_recently_selected_tab->LastFrameSelected < tab->LastFrameSelected) if (tab->Window && tab->Window->WasActive) most_recently_selected_tab = tab; } return most_recently_selected_tab; } // The purpose of this call is to register tab in advance so we can control their order at the time they appear. // Otherwise calling this is unnecessary as tabs are appending as needed by the BeginTabItem() function. void ImGui::TabBarAddTab(ImGuiTabBar* tab_bar, ImGuiTabItemFlags tab_flags, ImGuiWindow* window) { ImGuiContext& g = *GImGui; IM_ASSERT(TabBarFindTabByID(tab_bar, window->ID) == NULL); IM_ASSERT(g.CurrentTabBar == NULL); // Can't work while the tab bar is active as our tab doesn't have an X offset yet ImGuiTabItem new_tab; new_tab.ID = window->ID; new_tab.Flags = tab_flags; new_tab.LastFrameVisible = tab_bar->CurrFrameVisible; // Required so BeginTabBar() doesn't ditch the tab if (new_tab.LastFrameVisible == -1) new_tab.LastFrameVisible = g.FrameCount - 1; new_tab.Window = window; // Required so tab bar layout can compute the tab width before tab submission tab_bar->Tabs.push_back(new_tab); } // The *TabId fields be already set by the docking system _before_ the actual TabItem was created, so we clear them regardless. void ImGui::TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id) { if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_id)) tab_bar->Tabs.erase(tab); if (tab_bar->VisibleTabId == tab_id) { tab_bar->VisibleTabId = 0; } if (tab_bar->SelectedTabId == tab_id) { tab_bar->SelectedTabId = 0; } if (tab_bar->NextSelectedTabId == tab_id) { tab_bar->NextSelectedTabId = 0; } } // Called on manual closure attempt void ImGui::TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) { if ((tab_bar->VisibleTabId == tab->ID) && !(tab->Flags & ImGuiTabItemFlags_UnsavedDocument)) { // This will remove a frame of lag for selecting another tab on closure. // 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 tab->LastFrameVisible = -1; tab_bar->SelectedTabId = tab_bar->NextSelectedTabId = 0; } else if ((tab_bar->VisibleTabId != tab->ID) && (tab->Flags & ImGuiTabItemFlags_UnsavedDocument)) { // Actually select before expecting closure tab_bar->NextSelectedTabId = tab->ID; } } static float ImGui::TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling) { scrolling = ImMin(scrolling, tab_bar->OffsetMax - tab_bar->BarRect.GetWidth()); return ImMax(scrolling, 0.0f); } static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) { ImGuiContext& g = *GImGui; 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) int order = tab_bar->GetTabOrder(tab); float tab_x1 = tab->Offset + (order > 0 ? -margin : 0.0f); float tab_x2 = tab->Offset + tab->Width + (order + 1 < tab_bar->Tabs.Size ? margin : 1.0f); tab_bar->ScrollingTargetDistToVisibility = 0.0f; if (tab_bar->ScrollingTarget > tab_x1) { tab_bar->ScrollingTargetDistToVisibility = ImMax(tab_bar->ScrollingAnim - tab_x2, 0.0f); tab_bar->ScrollingTarget = tab_x1; } else if (tab_bar->ScrollingTarget < tab_x2 - tab_bar->BarRect.GetWidth()) { tab_bar->ScrollingTargetDistToVisibility = ImMax((tab_x1 - tab_bar->BarRect.GetWidth()) - tab_bar->ScrollingAnim, 0.0f); tab_bar->ScrollingTarget = tab_x2 - tab_bar->BarRect.GetWidth(); } } void ImGui::TabBarQueueChangeTabOrder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir) { IM_ASSERT(dir == -1 || dir == +1); IM_ASSERT(tab_bar->ReorderRequestTabId == 0); tab_bar->ReorderRequestTabId = tab->ID; tab_bar->ReorderRequestDir = dir; } static ImGuiTabItem* ImGui::TabBarScrollingButtons(ImGuiTabBar* tab_bar) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; const ImVec2 arrow_button_size(g.FontSize - 2.0f, g.FontSize + g.Style.FramePadding.y * 2.0f); const float scrolling_buttons_width = arrow_button_size.x * 2.0f; const ImVec2 backup_cursor_pos = window->DC.CursorPos; //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)); const ImRect avail_bar_rect = tab_bar->BarRect; bool want_clip_rect = !avail_bar_rect.Contains(ImRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(scrolling_buttons_width, 0.0f))); if (want_clip_rect) PushClipRect(tab_bar->BarRect.Min, tab_bar->BarRect.Max + ImVec2(g.Style.ItemInnerSpacing.x, 0.0f), true); ImGuiTabItem* tab_to_select = NULL; int select_dir = 0; ImVec4 arrow_col = g.Style.Colors[ImGuiCol_Text]; arrow_col.w *= 0.5f; PushStyleColor(ImGuiCol_Text, arrow_col); PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); const float backup_repeat_delay = g.IO.KeyRepeatDelay; const float backup_repeat_rate = g.IO.KeyRepeatRate; g.IO.KeyRepeatDelay = 0.250f; g.IO.KeyRepeatRate = 0.200f; window->DC.CursorPos = ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width, tab_bar->BarRect.Min.y); if (ArrowButtonEx("##<", ImGuiDir_Left, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat)) select_dir = -1; window->DC.CursorPos = ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width + arrow_button_size.x, tab_bar->BarRect.Min.y); if (ArrowButtonEx("##>", ImGuiDir_Right, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat)) select_dir = +1; PopStyleColor(2); g.IO.KeyRepeatRate = backup_repeat_rate; g.IO.KeyRepeatDelay = backup_repeat_delay; if (want_clip_rect) PopClipRect(); if (select_dir != 0) if (ImGuiTabItem* tab_item = TabBarFindTabByID(tab_bar, tab_bar->SelectedTabId)) { int selected_order = tab_bar->GetTabOrder(tab_item); int target_order = selected_order + select_dir; 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 } window->DC.CursorPos = backup_cursor_pos; tab_bar->BarRect.Max.x -= scrolling_buttons_width + 1.0f; return tab_to_select; } static ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; // We use g.Style.FramePadding.y to match the square ArrowButton size const float tab_list_popup_button_width = g.FontSize + g.Style.FramePadding.y; const ImVec2 backup_cursor_pos = window->DC.CursorPos; window->DC.CursorPos = ImVec2(tab_bar->BarRect.Min.x - g.Style.FramePadding.y, tab_bar->BarRect.Min.y); tab_bar->BarRect.Min.x += tab_list_popup_button_width; ImVec4 arrow_col = g.Style.Colors[ImGuiCol_Text]; arrow_col.w *= 0.5f; PushStyleColor(ImGuiCol_Text, arrow_col); PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); bool open = BeginCombo("##v", NULL, ImGuiComboFlags_NoPreview | ImGuiComboFlags_PopupAlignLeft); PopStyleColor(2); ImGuiTabItem* tab_to_select = NULL; if (open) { for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; const char* tab_name = tab_bar->GetTabName(tab); if (Selectable(tab_name, tab_bar->SelectedTabId == tab->ID)) tab_to_select = tab; } EndCombo(); } window->DC.CursorPos = backup_cursor_pos; return tab_to_select; } //------------------------------------------------------------------------- // [SECTION] Widgets: BeginTabItem, EndTabItem, etc. //------------------------------------------------------------------------- // [BETA API] API may evolve! //------------------------------------------------------------------------- // - BeginTabItem() // - EndTabItem() // - TabItemEx() [Internal] // - SetTabItemClosed() // - TabItemCalcSize() [Internal] // - TabItemBackground() [Internal] // - TabItemLabelAndCloseButton() [Internal] //------------------------------------------------------------------------- bool ImGui::BeginTabItem(const char* label, bool* p_open, ImGuiTabItemFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return false; ImGuiTabBar* tab_bar = g.CurrentTabBar; if (tab_bar == NULL) { IM_ASSERT(tab_bar && "Needs to be called between BeginTabBar() and EndTabBar()!"); return false; // FIXME-ERRORHANDLING } bool ret = TabItemEx(tab_bar, label, p_open, flags, NULL); if (ret && !(flags & ImGuiTabItemFlags_NoPushId)) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_bar->LastTabItemIdx]; PushOverrideID(tab->ID); // We already hashed 'label' so push into the ID stack directly instead of doing another hash through PushID(label) } return ret; } void ImGui::EndTabItem() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return; ImGuiTabBar* tab_bar = g.CurrentTabBar; if (tab_bar == NULL) { IM_ASSERT(tab_bar != NULL && "Needs to be called between BeginTabBar() and EndTabBar()!"); return; } IM_ASSERT(tab_bar->LastTabItemIdx >= 0); ImGuiTabItem* tab = &tab_bar->Tabs[tab_bar->LastTabItemIdx]; if (!(tab->Flags & ImGuiTabItemFlags_NoPushId)) window->IDStack.pop_back(); } bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags, ImGuiWindow* docked_window) { // Layout whole tab bar if not already done if (tab_bar->WantLayout) TabBarLayout(tab_bar); ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return false; const ImGuiStyle& style = g.Style; const ImGuiID id = TabBarCalcTabID(tab_bar, label); // 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. if (p_open && !*p_open) { PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); ItemAdd(ImRect(), id); PopItemFlag(); return false; } // Calculate tab contents size ImVec2 size = TabItemCalcSize(label, p_open != NULL); // Acquire tab data ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, id); bool tab_is_new = false; if (tab == NULL) { tab_bar->Tabs.push_back(ImGuiTabItem()); tab = &tab_bar->Tabs.back(); tab->ID = id; tab->Width = size.x; tab_is_new = true; } tab_bar->LastTabItemIdx = (short)tab_bar->Tabs.index_from_ptr(tab); tab->WidthContents = size.x; if (p_open == NULL) flags |= ImGuiTabItemFlags_NoCloseButton; const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); const bool tab_bar_focused = (tab_bar->Flags & ImGuiTabBarFlags_IsFocused) != 0; const bool tab_appearing = (tab->LastFrameVisible + 1 < g.FrameCount); tab->LastFrameVisible = g.FrameCount; tab->Flags = flags; tab->Window = docked_window; // Append name with zero-terminator if (tab_bar->Flags & ImGuiTabBarFlags_DockNode) { IM_ASSERT(tab->Window != NULL); tab->NameOffset = -1; } else { IM_ASSERT(tab->Window == NULL); tab->NameOffset = tab_bar->TabsNames.size(); tab_bar->TabsNames.append(label, label + strlen(label) + 1); // Append name _with_ the zero-terminator. } // If we are not reorderable, always reset offset based on submission order. // (We already handled layout and sizing using the previous known order, but sizing is not affected by order!) if (!tab_appearing && !(tab_bar->Flags & ImGuiTabBarFlags_Reorderable)) { tab->Offset = tab_bar->OffsetNextTab; tab_bar->OffsetNextTab += tab->Width + g.Style.ItemInnerSpacing.x; } // Update selected tab if (tab_appearing && (tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs) && tab_bar->NextSelectedTabId == 0) if (!tab_bar_appearing || tab_bar->SelectedTabId == 0) tab_bar->NextSelectedTabId = id; // New tabs gets activated if ((flags & ImGuiTabItemFlags_SetSelected) && (tab_bar->SelectedTabId != id)) // SetSelected can only be passed on explicit tab bar tab_bar->NextSelectedTabId = id; // Lock visibility bool tab_contents_visible = (tab_bar->VisibleTabId == id); if (tab_contents_visible) tab_bar->VisibleTabWasSubmitted = true; // On the very first frame of a tab bar we let first tab contents be visible to minimize appearing glitches if (!tab_contents_visible && tab_bar->SelectedTabId == 0 && tab_bar_appearing && docked_window == NULL) if (tab_bar->Tabs.Size == 1 && !(tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs)) tab_contents_visible = true; if (tab_appearing && !(tab_bar_appearing && !tab_is_new)) { PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); ItemAdd(ImRect(), id); PopItemFlag(); return tab_contents_visible; } if (tab_bar->SelectedTabId == id) tab->LastFrameSelected = g.FrameCount; // Backup current layout position const ImVec2 backup_main_cursor_pos = window->DC.CursorPos; // Layout size.x = tab->Width; window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2((float)(int)tab->Offset - tab_bar->ScrollingAnim, 0.0f); ImVec2 pos = window->DC.CursorPos; ImRect bb(pos, pos + size); // 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) bool want_clip_rect = (bb.Min.x < tab_bar->BarRect.Min.x) || (bb.Max.x >= tab_bar->BarRect.Max.x); if (want_clip_rect) 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); ItemSize(bb, style.FramePadding.y); if (!ItemAdd(bb, id)) { if (want_clip_rect) PopClipRect(); window->DC.CursorPos = backup_main_cursor_pos; return tab_contents_visible; } // Click to Select a tab ImGuiButtonFlags button_flags = (ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_AllowItemOverlap); if (g.DragDropActive && !g.DragDropPayload.IsDataType(IMGUI_PAYLOAD_TYPE_WINDOW)) button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); if (pressed) tab_bar->NextSelectedTabId = id; hovered |= (g.HoveredId == id); // Allow the close button to overlap unless we are dragging (in which case we don't want any overlapping tabs to be hovered) if (!held) SetItemAllowOverlap(); // Drag and drop a single floating window node moves it // FIXME-DOCK: In theory we shouldn't test for the ConfigDockingNodifySingleWindows flag here. // When our single window node and OnlyNodeWithWindows are working properly we may remove this check here. ImGuiDockNode* node = docked_window ? docked_window->DockNode : NULL; const bool single_floating_window_node = node && node->IsRootNode() && !node->IsDockSpace() && node->Windows.Size == 1 && g.IO.ConfigDockingTabBarOnSingleWindows; if (held && single_floating_window_node && IsMouseDragging(0, 0.0f)) { // Move StartMouseMovingWindow(docked_window); } else if (held && !tab_appearing && IsMouseDragging(0)) { // Drag and drop: re-order tabs float drag_distance_from_edge_x = 0.0f; if (!g.DragDropActive && ((tab_bar->Flags & ImGuiTabBarFlags_Reorderable) || (docked_window != NULL))) { // While moving a tab it will jump on the other side of the mouse, so we also test for MouseDelta.x if (g.IO.MouseDelta.x < 0.0f && g.IO.MousePos.x < bb.Min.x) { drag_distance_from_edge_x = bb.Min.x - g.IO.MousePos.x; if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) TabBarQueueChangeTabOrder(tab_bar, tab, -1); } else if (g.IO.MouseDelta.x > 0.0f && g.IO.MousePos.x > bb.Max.x) { drag_distance_from_edge_x = g.IO.MousePos.x - bb.Max.x; if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) TabBarQueueChangeTabOrder(tab_bar, tab, +1); } } // Extract a Dockable window out of it's tab bar if (docked_window != NULL && !(docked_window->Flags & ImGuiWindowFlags_NoMove)) { // We use a variable threshold to distinguish dragging tabs within a tab bar and extracting them out of the tab bar bool undocking_tab = (g.DragDropActive && g.DragDropPayload.SourceId == id); if (!undocking_tab) //&& (!g.IO.ConfigDockingWithShift || g.IO.KeyShift) { float threshold_base = g.FontSize; //float threshold_base = g.IO.ConfigDockingWithShift ? g.FontSize * 0.5f : g.FontSize; float threshold_x = (threshold_base * 2.2f); 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); //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] float distance_from_edge_y = ImMax(bb.Min.y - g.IO.MousePos.y, g.IO.MousePos.y - bb.Max.y); if (distance_from_edge_y >= threshold_y) undocking_tab = true; else if (drag_distance_from_edge_x > threshold_x) if ((tab_bar->ReorderRequestDir < 0 && tab_bar->GetTabOrder(tab) == 0) || (tab_bar->ReorderRequestDir > 0 && tab_bar->GetTabOrder(tab) == tab_bar->Tabs.Size - 1)) undocking_tab = true; } if (undocking_tab) { // Undock DockContextQueueUndockWindow(&g, docked_window); g.MovingWindow = docked_window; g.ActiveId = g.MovingWindow->MoveId; g.ActiveIdClickOffset -= g.MovingWindow->Pos - bb.Min; } } } #if 0 if (hovered && g.HoveredIdNotActiveTimer > 0.50f && bb.GetWidth() < tab->WidthContents) { // Enlarge tab display when hovering bb.Max.x = bb.Min.x + (float)(int)ImLerp(bb.GetWidth(), tab->WidthContents, ImSaturate((g.HoveredIdNotActiveTimer - 0.40f) * 6.0f)); display_draw_list = GetForegroundDrawList(window); TabItemBackground(display_draw_list, bb, flags, GetColorU32(ImGuiCol_TitleBgActive)); } #endif // Render tab shape ImDrawList* display_draw_list = window->DrawList; 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)); TabItemBackground(display_draw_list, bb, flags, tab_col); RenderNavHighlight(bb, id); // Select with right mouse button. This is so the common idiom for context menu automatically highlight the current widget. const bool hovered_unblocked = IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup); if (hovered_unblocked && (IsMouseClicked(1) || IsMouseReleased(1))) tab_bar->NextSelectedTabId = id; if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton) flags |= ImGuiTabItemFlags_NoCloseWithMiddleMouseButton; // Render tab label, process close button const ImGuiID close_button_id = p_open ? window->GetID((void*)((intptr_t)id + 1)) : 0; bool just_closed = TabItemLabelAndCloseButton(display_draw_list, bb, flags, tab_bar->FramePadding, label, id, close_button_id); if (just_closed && p_open != NULL) { *p_open = false; TabBarCloseTab(tab_bar, tab); } // Restore main window position so user can draw there if (want_clip_rect) PopClipRect(); window->DC.CursorPos = backup_main_cursor_pos; // Tooltip (FIXME: Won't work over the close button because ItemOverlap systems messes up with HoveredIdTimer) // We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar (which g.HoveredId ignores) if (g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > 0.50f && IsItemHovered()) if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip)) SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); return tab_contents_visible; } // [Public] This is call is 100% optional but it allows to remove some one-frame glitches when a tab has been unexpectedly removed. // To use it to need to call the function SetTabItemClosed() after BeginTabBar() and before any call to BeginTabItem() void ImGui::SetTabItemClosed(const char* label) { ImGuiContext& g = *GImGui; bool is_within_manual_tab_bar = g.CurrentTabBar && !(g.CurrentTabBar->Flags & ImGuiTabBarFlags_DockNode); if (is_within_manual_tab_bar) { ImGuiTabBar* tab_bar = g.CurrentTabBar; IM_ASSERT(tab_bar->WantLayout); // Needs to be called AFTER BeginTabBar() and BEFORE the first call to BeginTabItem() ImGuiID tab_id = TabBarCalcTabID(tab_bar, label); TabBarRemoveTab(tab_bar, tab_id); } else if (ImGuiWindow* window = FindWindowByName(label)) { if (window->DockIsActive) if (ImGuiDockNode* node = window->DockNode) { ImGuiID tab_id = TabBarCalcTabID(node->TabBar, label); TabBarRemoveTab(node->TabBar, tab_id); window->DockTabWantClose = true; } } } ImVec2 ImGui::TabItemCalcSize(const char* label, bool has_close_button) { ImGuiContext& g = *GImGui; ImVec2 label_size = CalcTextSize(label, NULL, true); ImVec2 size = ImVec2(label_size.x + g.Style.FramePadding.x, label_size.y + g.Style.FramePadding.y * 2.0f); if (has_close_button) size.x += g.Style.FramePadding.x + (g.Style.ItemInnerSpacing.x + g.FontSize); // We use Y intentionally to fit the close button circle. else size.x += g.Style.FramePadding.x + 1.0f; return ImVec2(ImMin(size.x, TabBarCalcMaxTabWidth()), size.y); } void ImGui::TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col) { // 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. ImGuiContext& g = *GImGui; const float width = bb.GetWidth(); IM_UNUSED(flags); IM_ASSERT(width > 0.0f); const float rounding = ImMax(0.0f, ImMin(g.Style.TabRounding, width * 0.5f - 1.0f)); const float y1 = bb.Min.y + 1.0f; const float y2 = bb.Max.y + ((flags & ImGuiTabItemFlags_Preview) ? 0.0f : -1.0f); draw_list->PathLineTo(ImVec2(bb.Min.x, y2)); draw_list->PathArcToFast(ImVec2(bb.Min.x + rounding, y1 + rounding), rounding, 6, 9); draw_list->PathArcToFast(ImVec2(bb.Max.x - rounding, y1 + rounding), rounding, 9, 12); draw_list->PathLineTo(ImVec2(bb.Max.x, y2)); draw_list->PathFillConvex(col); if (g.Style.TabBorderSize > 0.0f) { draw_list->PathLineTo(ImVec2(bb.Min.x + 0.5f, y2)); draw_list->PathArcToFast(ImVec2(bb.Min.x + rounding + 0.5f, y1 + rounding + 0.5f), rounding, 6, 9); draw_list->PathArcToFast(ImVec2(bb.Max.x - rounding - 0.5f, y1 + rounding + 0.5f), rounding, 9, 12); draw_list->PathLineTo(ImVec2(bb.Max.x - 0.5f, y2)); draw_list->PathStroke(GetColorU32(ImGuiCol_Border), false, g.Style.TabBorderSize); } } // Render text label (with custom clipping) + Unsaved Document marker + Close Button logic // We tend to lock style.FramePadding for a given tab-bar, hence the 'frame_padding' parameter. bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id) { ImGuiContext& g = *GImGui; ImVec2 label_size = CalcTextSize(label, NULL, true); if (bb.GetWidth() <= 1.0f) return false; // Render text label (with clipping + alpha gradient) + unsaved marker const char* TAB_UNSAVED_MARKER = "*"; 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); if (flags & ImGuiTabItemFlags_UnsavedDocument) { text_pixel_clip_bb.Max.x -= CalcTextSize(TAB_UNSAVED_MARKER, NULL, false).x; 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)); RenderTextClippedEx(draw_list, unsaved_marker_pos, bb.Max - frame_padding, TAB_UNSAVED_MARKER, NULL, NULL); } ImRect text_ellipsis_clip_bb = text_pixel_clip_bb; // Close Button // We are relying on a subtle and confusing distinction between 'hovered' and 'g.HoveredId' which happens because we are using ImGuiButtonFlags_AllowOverlapMode + SetItemAllowOverlap() // 'hovered' will be true when hovering the Tab but NOT when hovering the close button // 'g.HoveredId==id' will be true when hovering the Tab including when hovering the close button // 'g.ActiveId==close_button_id' will be true when we are holding on the close button, in which case both hovered booleans are false bool close_button_pressed = false; bool close_button_visible = false; if (close_button_id != 0) if (g.HoveredId == tab_id || g.HoveredId == close_button_id || g.ActiveId == close_button_id) close_button_visible = true; if (close_button_visible) { ImGuiItemHoveredDataBackup last_item_backup; const float close_button_sz = g.FontSize * 0.5f; 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)) close_button_pressed = true; last_item_backup.Restore(); // Close with middle mouse button if (!(flags & ImGuiTabItemFlags_NoCloseWithMiddleMouseButton) && IsMouseClicked(2)) close_button_pressed = true; text_pixel_clip_bb.Max.x -= close_button_sz * 2.0f; } // Label with ellipsis // 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 const char* label_display_end = FindRenderedTextEnd(label); if (label_size.x > text_ellipsis_clip_bb.GetWidth()) { const int ellipsis_dot_count = 3; const float ellipsis_width = (1.0f + 1.0f) * ellipsis_dot_count - 1.0f; const char* label_end = NULL; 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; if (label_end == label && label_end < label_display_end) // Always display at least 1 character if there's no room for character + ellipsis { label_end = label + ImTextCountUtf8BytesFromChar(label, label_display_end); label_size_clipped_x = g.Font->CalcTextSizeA(g.FontSize, FLT_MAX, 0.0f, label, label_end).x; } while (label_end > label && ImCharIsBlankA(label_end[-1])) // Trim trailing space { label_end--; 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 } RenderTextClippedEx(draw_list, text_pixel_clip_bb.Min, text_pixel_clip_bb.Max, label, label_end, &label_size, ImVec2(0.0f, 0.0f)); const float ellipsis_x = text_pixel_clip_bb.Min.x + label_size_clipped_x + 1.0f; if (!close_button_visible && ellipsis_x + ellipsis_width <= bb.Max.x) RenderPixelEllipsis(draw_list, ImVec2(ellipsis_x, text_pixel_clip_bb.Min.y), ellipsis_dot_count, GetColorU32(ImGuiCol_Text)); } else { RenderTextClippedEx(draw_list, text_pixel_clip_bb.Min, text_pixel_clip_bb.Max, label, label_display_end, &label_size, ImVec2(0.0f, 0.0f)); } return close_button_pressed; } ================================================ FILE: src/imgui/imstb_rectpack.hpp ================================================ // [DEAR IMGUI] // This is a slightly modified version of stb_rect_pack.h 0.99. // Those changes would need to be pushed into nothings/stb: // - Added STBRP__CDECL // Grep for [DEAR IMGUI] to find the changes. // stb_rect_pack.h - v0.99 - public domain - rectangle packing // Sean Barrett 2014 // // Useful for e.g. packing rectangular textures into an atlas. // Does not do rotation. // // Not necessarily the awesomest packing method, but better than // the totally naive one in stb_truetype (which is primarily what // this is meant to replace). // // Has only had a few tests run, may have issues. // // More docs to come. // // No memory allocations; uses qsort() and assert() from stdlib. // Can override those by defining STBRP_SORT and STBRP_ASSERT. // // This library currently uses the Skyline Bottom-Left algorithm. // // Please note: better rectangle packers are welcome! Please // implement them to the same API, but with a different init // function. // // Credits // // Library // Sean Barrett // Minor features // Martins Mozeiko // github:IntellectualKitty // // Bugfixes / warning fixes // Jeremy Jaussaud // // Version history: // // 0.99 (2019-02-07) warning fixes // 0.11 (2017-03-03) return packing success/fail result // 0.10 (2016-10-25) remove cast-away-const to avoid warnings // 0.09 (2016-08-27) fix compiler warnings // 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0) // 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0) // 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort // 0.05: added STBRP_ASSERT to allow replacing assert // 0.04: fixed minor bug in STBRP_LARGE_RECTS support // 0.01: initial release // // LICENSE // // See end of file for license information. ////////////////////////////////////////////////////////////////////////////// // // INCLUDE SECTION // #ifndef STB_INCLUDE_STB_RECT_PACK_H #define STB_INCLUDE_STB_RECT_PACK_H #define STB_RECT_PACK_VERSION 1 #ifdef STBRP_STATIC #define STBRP_DEF static #else #define STBRP_DEF extern #endif #ifdef __cplusplus extern "C" { #endif typedef struct stbrp_context stbrp_context; typedef struct stbrp_node stbrp_node; typedef struct stbrp_rect stbrp_rect; #ifdef STBRP_LARGE_RECTS typedef int stbrp_coord; #else typedef unsigned short stbrp_coord; #endif STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects); // Assign packed locations to rectangles. The rectangles are of type // 'stbrp_rect' defined below, stored in the array 'rects', and there // are 'num_rects' many of them. // // Rectangles which are successfully packed have the 'was_packed' flag // set to a non-zero value and 'x' and 'y' store the minimum location // on each axis (i.e. bottom-left in cartesian coordinates, top-left // if you imagine y increasing downwards). Rectangles which do not fit // have the 'was_packed' flag set to 0. // // You should not try to access the 'rects' array from another thread // while this function is running, as the function temporarily reorders // the array while it executes. // // To pack into another rectangle, you need to call stbrp_init_target // again. To continue packing into the same rectangle, you can call // this function again. Calling this multiple times with multiple rect // arrays will probably produce worse packing results than calling it // a single time with the full rectangle array, but the option is // available. // // The function returns 1 if all of the rectangles were successfully // packed and 0 otherwise. struct stbrp_rect { // reserved for your use: int id; // input: stbrp_coord w, h; // output: stbrp_coord x, y; int was_packed; // non-zero if valid packing }; // 16 bytes, nominally STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes); // Initialize a rectangle packer to: // pack a rectangle that is 'width' by 'height' in dimensions // using temporary storage provided by the array 'nodes', which is 'num_nodes' long // // You must call this function every time you start packing into a new target. // // There is no "shutdown" function. The 'nodes' memory must stay valid for // the following stbrp_pack_rects() call (or calls), but can be freed after // the call (or calls) finish. // // Note: to guarantee best results, either: // 1. make sure 'num_nodes' >= 'width' // or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1' // // If you don't do either of the above things, widths will be quantized to multiples // of small integers to guarantee the algorithm doesn't run out of temporary storage. // // If you do #2, then the non-quantized algorithm will be used, but the algorithm // may run out of temporary storage and be unable to pack some rectangles. STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem); // Optionally call this function after init but before doing any packing to // change the handling of the out-of-temp-memory scenario, described above. // If you call init again, this will be reset to the default (false). STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic); // Optionally select which packing heuristic the library should use. Different // heuristics will produce better/worse results for different data sets. // If you call init again, this will be reset to the default. enum { STBRP_HEURISTIC_Skyline_default=0, STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default, STBRP_HEURISTIC_Skyline_BF_sortHeight }; ////////////////////////////////////////////////////////////////////////////// // // the details of the following structures don't matter to you, but they must // be visible so you can handle the memory allocations for them struct stbrp_node { stbrp_coord x,y; stbrp_node *next; }; struct stbrp_context { int width; int height; int align; int init_mode; int heuristic; int num_nodes; stbrp_node *active_head; stbrp_node *free_head; stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' }; #ifdef __cplusplus } #endif #endif ////////////////////////////////////////////////////////////////////////////// // // IMPLEMENTATION SECTION // #ifdef STB_RECT_PACK_IMPLEMENTATION #ifndef STBRP_SORT #include #define STBRP_SORT qsort #endif #ifndef STBRP_ASSERT #include #define STBRP_ASSERT assert #endif // [DEAR IMGUI] Added STBRP__CDECL #ifdef _MSC_VER #define STBRP__NOTUSED(v) (void)(v) #define STBRP__CDECL __cdecl #else #define STBRP__NOTUSED(v) (void)sizeof(v) #define STBRP__CDECL #endif enum { STBRP__INIT_skyline = 1 }; STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic) { switch (context->init_mode) { case STBRP__INIT_skyline: STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight); context->heuristic = heuristic; break; default: STBRP_ASSERT(0); } } STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem) { if (allow_out_of_mem) // if it's ok to run out of memory, then don't bother aligning them; // this gives better packing, but may fail due to OOM (even though // the rectangles easily fit). @TODO a smarter approach would be to only // quantize once we've hit OOM, then we could get rid of this parameter. context->align = 1; else { // if it's not ok to run out of memory, then quantize the widths // so that num_nodes is always enough nodes. // // I.e. num_nodes * align >= width // align >= width / num_nodes // align = ceil(width/num_nodes) context->align = (context->width + context->num_nodes-1) / context->num_nodes; } } STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes) { int i; #ifndef STBRP_LARGE_RECTS STBRP_ASSERT(width <= 0xffff && height <= 0xffff); #endif for (i=0; i < num_nodes-1; ++i) nodes[i].next = &nodes[i+1]; nodes[i].next = NULL; context->init_mode = STBRP__INIT_skyline; context->heuristic = STBRP_HEURISTIC_Skyline_default; context->free_head = &nodes[0]; context->active_head = &context->extra[0]; context->width = width; context->height = height; context->num_nodes = num_nodes; stbrp_setup_allow_out_of_mem(context, 0); // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) context->extra[0].x = 0; context->extra[0].y = 0; context->extra[0].next = &context->extra[1]; context->extra[1].x = (stbrp_coord) width; #ifdef STBRP_LARGE_RECTS context->extra[1].y = (1<<30); #else context->extra[1].y = 65535; #endif context->extra[1].next = NULL; } // find minimum y position if it starts at x1 static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste) { stbrp_node *node = first; int x1 = x0 + width; int min_y, visited_width, waste_area; STBRP__NOTUSED(c); STBRP_ASSERT(first->x <= x0); #if 0 // skip in case we're past the node while (node->next->x <= x0) ++node; #else STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency #endif STBRP_ASSERT(node->x <= x0); min_y = 0; waste_area = 0; visited_width = 0; while (node->x < x1) { if (node->y > min_y) { // raise min_y higher. // we've accounted for all waste up to min_y, // but we'll now add more waste for everything we've visted waste_area += visited_width * (node->y - min_y); min_y = node->y; // the first time through, visited_width might be reduced if (node->x < x0) visited_width += node->next->x - x0; else visited_width += node->next->x - node->x; } else { // add waste area int under_width = node->next->x - node->x; if (under_width + visited_width > width) under_width = width - visited_width; waste_area += under_width * (min_y - node->y); visited_width += under_width; } node = node->next; } *pwaste = waste_area; return min_y; } typedef struct { int x,y; stbrp_node **prev_link; } stbrp__findresult; static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height) { int best_waste = (1<<30), best_x, best_y = (1 << 30); stbrp__findresult fr; stbrp_node **prev, *node, *tail, **best = NULL; // align to multiple of c->align width = (width + c->align - 1); width -= width % c->align; STBRP_ASSERT(width % c->align == 0); node = c->active_head; prev = &c->active_head; while (node->x + width <= c->width) { int y,waste; y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste); if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL // bottom left if (y < best_y) { best_y = y; best = prev; } } else { // best-fit if (y + height <= c->height) { // can only use it if it first vertically if (y < best_y || (y == best_y && waste < best_waste)) { best_y = y; best_waste = waste; best = prev; } } } prev = &node->next; node = node->next; } best_x = (best == NULL) ? 0 : (*best)->x; // if doing best-fit (BF), we also have to try aligning right edge to each node position // // e.g, if fitting // // ____________________ // |____________________| // // into // // | | // | ____________| // |____________| // // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned // // This makes BF take about 2x the time if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) { tail = c->active_head; node = c->active_head; prev = &c->active_head; // find first node that's admissible while (tail->x < width) tail = tail->next; while (tail) { int xpos = tail->x - width; int y,waste; STBRP_ASSERT(xpos >= 0); // find the left position that matches this while (node->next->x <= xpos) { prev = &node->next; node = node->next; } STBRP_ASSERT(node->next->x > xpos && node->x <= xpos); y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste); if (y + height < c->height) { if (y <= best_y) { if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) { best_x = xpos; STBRP_ASSERT(y <= best_y); best_y = y; best_waste = waste; best = prev; } } } tail = tail->next; } } fr.prev_link = best; fr.x = best_x; fr.y = best_y; return fr; } static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height) { // find best position according to heuristic stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height); stbrp_node *node, *cur; // bail if: // 1. it failed // 2. the best node doesn't fit (we don't always check this) // 3. we're out of memory if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) { res.prev_link = NULL; return res; } // on success, create new node node = context->free_head; node->x = (stbrp_coord) res.x; node->y = (stbrp_coord) (res.y + height); context->free_head = node->next; // insert the new node into the right starting point, and // let 'cur' point to the remaining nodes needing to be // stiched back in cur = *res.prev_link; if (cur->x < res.x) { // preserve the existing one, so start testing with the next one stbrp_node *next = cur->next; cur->next = node; cur = next; } else { *res.prev_link = node; } // from here, traverse cur and free the nodes, until we get to one // that shouldn't be freed while (cur->next && cur->next->x <= res.x + width) { stbrp_node *next = cur->next; // move the current node to the free list cur->next = context->free_head; context->free_head = cur; cur = next; } // stitch the list back in node->next = cur; if (cur->x < res.x + width) cur->x = (stbrp_coord) (res.x + width); #ifdef _DEBUG cur = context->active_head; while (cur->x < context->width) { STBRP_ASSERT(cur->x < cur->next->x); cur = cur->next; } STBRP_ASSERT(cur->next == NULL); { int count=0; cur = context->active_head; while (cur) { cur = cur->next; ++count; } cur = context->free_head; while (cur) { cur = cur->next; ++count; } STBRP_ASSERT(count == context->num_nodes+2); } #endif return res; } // [DEAR IMGUI] Added STBRP__CDECL static int STBRP__CDECL rect_height_compare(const void *a, const void *b) { const stbrp_rect *p = (const stbrp_rect *) a; const stbrp_rect *q = (const stbrp_rect *) b; if (p->h > q->h) return -1; if (p->h < q->h) return 1; return (p->w > q->w) ? -1 : (p->w < q->w); } // [DEAR IMGUI] Added STBRP__CDECL static int STBRP__CDECL rect_original_order(const void *a, const void *b) { const stbrp_rect *p = (const stbrp_rect *) a; const stbrp_rect *q = (const stbrp_rect *) b; return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed); } #ifdef STBRP_LARGE_RECTS #define STBRP__MAXVAL 0xffffffff #else #define STBRP__MAXVAL 0xffff #endif STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects) { int i, all_rects_packed = 1; // we use the 'was_packed' field internally to allow sorting/unsorting for (i=0; i < num_rects; ++i) { rects[i].was_packed = i; } // sort according to heuristic STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare); for (i=0; i < num_rects; ++i) { if (rects[i].w == 0 || rects[i].h == 0) { rects[i].x = rects[i].y = 0; // empty rect needs no space } else { stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h); if (fr.prev_link) { rects[i].x = (stbrp_coord) fr.x; rects[i].y = (stbrp_coord) fr.y; } else { rects[i].x = rects[i].y = STBRP__MAXVAL; } } } // unsort STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order); // set was_packed flags and all_rects_packed status for (i=0; i < num_rects; ++i) { rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL); if (!rects[i].was_packed) all_rects_packed = 0; } // return the all_rects_packed status return all_rects_packed; } #endif /* ------------------------------------------------------------------------------ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2017 Sean Barrett Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ ================================================ FILE: src/imgui/imstb_textedit.hpp ================================================ // [DEAR IMGUI] // This is a slightly modified version of stb_textedit.h 1.13. // Those changes would need to be pushed into nothings/stb: // - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321) // Grep for [DEAR IMGUI] to find the changes. // stb_textedit.h - v1.13 - public domain - Sean Barrett // Development of this library was sponsored by RAD Game Tools // // This C header file implements the guts of a multi-line text-editing // widget; you implement display, word-wrapping, and low-level string // insertion/deletion, and stb_textedit will map user inputs into // insertions & deletions, plus updates to the cursor position, // selection state, and undo state. // // It is intended for use in games and other systems that need to build // their own custom widgets and which do not have heavy text-editing // requirements (this library is not recommended for use for editing large // texts, as its performance does not scale and it has limited undo). // // Non-trivial behaviors are modelled after Windows text controls. // // // LICENSE // // See end of file for license information. // // // DEPENDENCIES // // Uses the C runtime function 'memmove', which you can override // by defining STB_TEXTEDIT_memmove before the implementation. // Uses no other functions. Performs no runtime allocations. // // // VERSION HISTORY // // 1.13 (2019-02-07) fix bug in undo size management // 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash // 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield // 1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual // 1.9 (2016-08-27) customizable move-by-word // 1.8 (2016-04-02) better keyboard handling when mouse button is down // 1.7 (2015-09-13) change y range handling in case baseline is non-0 // 1.6 (2015-04-15) allow STB_TEXTEDIT_memmove // 1.5 (2014-09-10) add support for secondary keys for OS X // 1.4 (2014-08-17) fix signed/unsigned warnings // 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary // 1.2 (2014-05-27) fix some RAD types that had crept into the new code // 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE ) // 1.0 (2012-07-26) improve documentation, initial public release // 0.3 (2012-02-24) bugfixes, single-line mode; insert mode // 0.2 (2011-11-28) fixes to undo/redo // 0.1 (2010-07-08) initial version // // ADDITIONAL CONTRIBUTORS // // Ulf Winklemann: move-by-word in 1.1 // Fabian Giesen: secondary key inputs in 1.5 // Martins Mozeiko: STB_TEXTEDIT_memmove in 1.6 // // Bugfixes: // Scott Graham // Daniel Keller // Omar Cornut // Dan Thompson // // USAGE // // This file behaves differently depending on what symbols you define // before including it. // // // Header-file mode: // // If you do not define STB_TEXTEDIT_IMPLEMENTATION before including this, // it will operate in "header file" mode. In this mode, it declares a // single public symbol, STB_TexteditState, which encapsulates the current // state of a text widget (except for the string, which you will store // separately). // // To compile in this mode, you must define STB_TEXTEDIT_CHARTYPE to a // primitive type that defines a single character (e.g. char, wchar_t, etc). // // To save space or increase undo-ability, you can optionally define the // following things that are used by the undo system: // // STB_TEXTEDIT_POSITIONTYPE small int type encoding a valid cursor position // STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow // STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer // // If you don't define these, they are set to permissive types and // moderate sizes. The undo system does no memory allocations, so // it grows STB_TexteditState by the worst-case storage which is (in bytes): // // [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT // + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT // // // Implementation mode: // // If you define STB_TEXTEDIT_IMPLEMENTATION before including this, it // will compile the implementation of the text edit widget, depending // on a large number of symbols which must be defined before the include. // // The implementation is defined only as static functions. You will then // need to provide your own APIs in the same file which will access the // static functions. // // The basic concept is that you provide a "string" object which // behaves like an array of characters. stb_textedit uses indices to // refer to positions in the string, implicitly representing positions // in the displayed textedit. This is true for both plain text and // rich text; even with rich text stb_truetype interacts with your // code as if there was an array of all the displayed characters. // // Symbols that must be the same in header-file and implementation mode: // // STB_TEXTEDIT_CHARTYPE the character type // STB_TEXTEDIT_POSITIONTYPE small type that is a valid cursor position // STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow // STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer // // Symbols you must define for implementation mode: // // STB_TEXTEDIT_STRING the type of object representing a string being edited, // typically this is a wrapper object with other data you need // // STB_TEXTEDIT_STRINGLEN(obj) the length of the string (ideally O(1)) // STB_TEXTEDIT_LAYOUTROW(&r,obj,n) returns the results of laying out a line of characters // starting from character #n (see discussion below) // STB_TEXTEDIT_GETWIDTH(obj,n,i) returns the pixel delta from the xpos of the i'th character // to the xpos of the i+1'th char for a line of characters // starting at character #n (i.e. accounts for kerning // with previous char) // STB_TEXTEDIT_KEYTOTEXT(k) maps a keyboard input to an insertable character // (return type is int, -1 means not valid to insert) // STB_TEXTEDIT_GETCHAR(obj,i) returns the i'th character of obj, 0-based // STB_TEXTEDIT_NEWLINE the character returned by _GETCHAR() we recognize // as manually wordwrapping for end-of-line positioning // // STB_TEXTEDIT_DELETECHARS(obj,i,n) delete n characters starting at i // STB_TEXTEDIT_INSERTCHARS(obj,i,c*,n) insert n characters at i (pointed to by STB_TEXTEDIT_CHARTYPE*) // // STB_TEXTEDIT_K_SHIFT a power of two that is or'd in to a keyboard input to represent the shift key // // STB_TEXTEDIT_K_LEFT keyboard input to move cursor left // STB_TEXTEDIT_K_RIGHT keyboard input to move cursor right // STB_TEXTEDIT_K_UP keyboard input to move cursor up // STB_TEXTEDIT_K_DOWN keyboard input to move cursor down // STB_TEXTEDIT_K_LINESTART keyboard input to move cursor to start of line // e.g. HOME // STB_TEXTEDIT_K_LINEEND keyboard input to move cursor to end of line // e.g. END // STB_TEXTEDIT_K_TEXTSTART keyboard input to move cursor to start of text // e.g. ctrl-HOME // STB_TEXTEDIT_K_TEXTEND keyboard input to move cursor to end of text // e.g. ctrl-END // STB_TEXTEDIT_K_DELETE keyboard input to delete selection or character under cursor // STB_TEXTEDIT_K_BACKSPACE keyboard input to delete selection or character left of cursor // STB_TEXTEDIT_K_UNDO keyboard input to perform undo // STB_TEXTEDIT_K_REDO keyboard input to perform redo // // Optional: // STB_TEXTEDIT_K_INSERT keyboard input to toggle insert mode // STB_TEXTEDIT_IS_SPACE(ch) true if character is whitespace (e.g. 'isspace'), // required for default WORDLEFT/WORDRIGHT handlers // STB_TEXTEDIT_MOVEWORDLEFT(obj,i) custom handler for WORDLEFT, returns index to move cursor to // STB_TEXTEDIT_MOVEWORDRIGHT(obj,i) custom handler for WORDRIGHT, returns index to move cursor to // STB_TEXTEDIT_K_WORDLEFT keyboard input to move cursor left one word // e.g. ctrl-LEFT // STB_TEXTEDIT_K_WORDRIGHT keyboard input to move cursor right one word // e.g. ctrl-RIGHT // STB_TEXTEDIT_K_LINESTART2 secondary keyboard input to move cursor to start of line // STB_TEXTEDIT_K_LINEEND2 secondary keyboard input to move cursor to end of line // STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text // STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text // // Todo: // STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page // STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page // // Keyboard input must be encoded as a single integer value; e.g. a character code // and some bitflags that represent shift states. to simplify the interface, SHIFT must // be a bitflag, so we can test the shifted state of cursor movements to allow selection, // i.e. (STB_TEXTED_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow. // // You can encode other things, such as CONTROL or ALT, in additional bits, and // then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example, // my Windows implementations add an additional CONTROL bit, and an additional KEYDOWN // bit. Then all of the STB_TEXTEDIT_K_ values bitwise-or in the KEYDOWN bit, // and I pass both WM_KEYDOWN and WM_CHAR events to the "key" function in the // API below. The control keys will only match WM_KEYDOWN events because of the // keydown bit I add, and STB_TEXTEDIT_KEYTOTEXT only tests for the KEYDOWN // bit so it only decodes WM_CHAR events. // // STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed // row of characters assuming they start on the i'th character--the width and // the height and the number of characters consumed. This allows this library // to traverse the entire layout incrementally. You need to compute word-wrapping // here. // // Each textfield keeps its own insert mode state, which is not how normal // applications work. To keep an app-wide insert mode, update/copy the // "insert_mode" field of STB_TexteditState before/after calling API functions. // // API // // void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line) // // void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) // void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) // int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) // int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len) // void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key) // // Each of these functions potentially updates the string and updates the // state. // // initialize_state: // set the textedit state to a known good default state when initially // constructing the textedit. // // click: // call this with the mouse x,y on a mouse down; it will update the cursor // and reset the selection start/end to the cursor point. the x,y must // be relative to the text widget, with (0,0) being the top left. // // drag: // call this with the mouse x,y on a mouse drag/up; it will update the // cursor and the selection end point // // cut: // call this to delete the current selection; returns true if there was // one. you should FIRST copy the current selection to the system paste buffer. // (To copy, just copy the current selection out of the string yourself.) // // paste: // call this to paste text at the current cursor point or over the current // selection if there is one. // // key: // call this for keyboard inputs sent to the textfield. you can use it // for "key down" events or for "translated" key events. if you need to // do both (as in Win32), or distinguish Unicode characters from control // inputs, set a high bit to distinguish the two; then you can define the // various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit // set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is // clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to // anything other type you wante before including. // // // When rendering, you can read the cursor position and selection state from // the STB_TexteditState. // // // Notes: // // This is designed to be usable in IMGUI, so it allows for the possibility of // running in an IMGUI that has NOT cached the multi-line layout. For this // reason, it provides an interface that is compatible with computing the // layout incrementally--we try to make sure we make as few passes through // as possible. (For example, to locate the mouse pointer in the text, we // could define functions that return the X and Y positions of characters // and binary search Y and then X, but if we're doing dynamic layout this // will run the layout algorithm many times, so instead we manually search // forward in one pass. Similar logic applies to e.g. up-arrow and // down-arrow movement.) // // If it's run in a widget that *has* cached the layout, then this is less // efficient, but it's not horrible on modern computers. But you wouldn't // want to edit million-line files with it. //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //// //// Header-file mode //// //// #ifndef INCLUDE_STB_TEXTEDIT_H #define INCLUDE_STB_TEXTEDIT_H //////////////////////////////////////////////////////////////////////// // // STB_TexteditState // // Definition of STB_TexteditState which you should store // per-textfield; it includes cursor position, selection state, // and undo state. // #ifndef STB_TEXTEDIT_UNDOSTATECOUNT #define STB_TEXTEDIT_UNDOSTATECOUNT 99 #endif #ifndef STB_TEXTEDIT_UNDOCHARCOUNT #define STB_TEXTEDIT_UNDOCHARCOUNT 999 #endif #ifndef STB_TEXTEDIT_CHARTYPE #define STB_TEXTEDIT_CHARTYPE int #endif #ifndef STB_TEXTEDIT_POSITIONTYPE #define STB_TEXTEDIT_POSITIONTYPE int #endif typedef struct { // private data STB_TEXTEDIT_POSITIONTYPE where; STB_TEXTEDIT_POSITIONTYPE insert_length; STB_TEXTEDIT_POSITIONTYPE delete_length; int char_storage; } StbUndoRecord; typedef struct { // private data StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT]; STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT]; short undo_point, redo_point; int undo_char_point, redo_char_point; } StbUndoState; typedef struct { ///////////////////// // // public data // int cursor; // position of the text cursor within the string int select_start; // selection start point int select_end; // selection start and end point in characters; if equal, no selection. // note that start may be less than or greater than end (e.g. when // dragging the mouse, start is where the initial click was, and you // can drag in either direction) unsigned char insert_mode; // each textfield keeps its own insert mode state. to keep an app-wide // insert mode, copy this value in/out of the app state ///////////////////// // // private data // unsigned char cursor_at_end_of_line; // not implemented yet unsigned char initialized; unsigned char has_preferred_x; unsigned char single_line; unsigned char padding1, padding2, padding3; float preferred_x; // this determines where the cursor up/down tries to seek to along x StbUndoState undostate; } STB_TexteditState; //////////////////////////////////////////////////////////////////////// // // StbTexteditRow // // Result of layout query, used by stb_textedit to determine where // the text in each row is. // result of layout query typedef struct { float x0,x1; // starting x location, end x location (allows for align=right, etc) float baseline_y_delta; // position of baseline relative to previous row's baseline float ymin,ymax; // height of row above and below baseline int num_chars; } StbTexteditRow; #endif //INCLUDE_STB_TEXTEDIT_H //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //// //// Implementation mode //// //// // implementation isn't include-guarded, since it might have indirectly // included just the "header" portion #ifdef STB_TEXTEDIT_IMPLEMENTATION #ifndef STB_TEXTEDIT_memmove #include #define STB_TEXTEDIT_memmove memmove #endif ///////////////////////////////////////////////////////////////////////////// // // Mouse input handling // // traverse the layout to locate the nearest character to a display position static int stb_text_locate_coord(STB_TEXTEDIT_STRING *str, float x, float y) { StbTexteditRow r; int n = STB_TEXTEDIT_STRINGLEN(str); float base_y = 0, prev_x; int i=0, k; r.x0 = r.x1 = 0; r.ymin = r.ymax = 0; r.num_chars = 0; // search rows to find one that straddles 'y' while (i < n) { STB_TEXTEDIT_LAYOUTROW(&r, str, i); if (r.num_chars <= 0) return n; if (i==0 && y < base_y + r.ymin) return 0; if (y < base_y + r.ymax) break; i += r.num_chars; base_y += r.baseline_y_delta; } // below all text, return 'after' last character if (i >= n) return n; // check if it's before the beginning of the line if (x < r.x0) return i; // check if it's before the end of the line if (x < r.x1) { // search characters in row for one that straddles 'x' prev_x = r.x0; for (k=0; k < r.num_chars; ++k) { float w = STB_TEXTEDIT_GETWIDTH(str, i, k); if (x < prev_x+w) { if (x < prev_x+w/2) return k+i; else return k+i+1; } prev_x += w; } // shouldn't happen, but if it does, fall through to end-of-line case } // if the last character is a newline, return that. otherwise return 'after' the last character if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE) return i+r.num_chars-1; else return i+r.num_chars; } // API click: on mouse down, move the cursor to the clicked location, and reset the selection static void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) { // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse // goes off the top or bottom of the text if( state->single_line ) { StbTexteditRow r; STB_TEXTEDIT_LAYOUTROW(&r, str, 0); y = r.ymin; } state->cursor = stb_text_locate_coord(str, x, y); state->select_start = state->cursor; state->select_end = state->cursor; state->has_preferred_x = 0; } // API drag: on mouse drag, move the cursor and selection endpoint to the clicked location static void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) { int p = 0; // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse // goes off the top or bottom of the text if( state->single_line ) { StbTexteditRow r; STB_TEXTEDIT_LAYOUTROW(&r, str, 0); y = r.ymin; } if (state->select_start == state->select_end) state->select_start = state->cursor; p = stb_text_locate_coord(str, x, y); state->cursor = state->select_end = p; } ///////////////////////////////////////////////////////////////////////////// // // Keyboard input handling // // forward declarations static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length); static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length); static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length); typedef struct { float x,y; // position of n'th character float height; // height of line int first_char, length; // first char of row, and length int prev_first; // first char of previous row } StbFindState; // find the x/y location of a character, and remember info about the previous row in // case we get a move-up event (for page up, we'll have to rescan) static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *str, int n, int single_line) { StbTexteditRow r; int prev_start = 0; int z = STB_TEXTEDIT_STRINGLEN(str); int i=0, first; if (n == z) { // if it's at the end, then find the last line -- simpler than trying to // explicitly handle this case in the regular code if (single_line) { STB_TEXTEDIT_LAYOUTROW(&r, str, 0); find->y = 0; find->first_char = 0; find->length = z; find->height = r.ymax - r.ymin; find->x = r.x1; } else { find->y = 0; find->x = 0; find->height = 1; while (i < z) { STB_TEXTEDIT_LAYOUTROW(&r, str, i); prev_start = i; i += r.num_chars; } find->first_char = i; find->length = 0; find->prev_first = prev_start; } return; } // search rows to find the one that straddles character n find->y = 0; for(;;) { STB_TEXTEDIT_LAYOUTROW(&r, str, i); if (n < i + r.num_chars) break; prev_start = i; i += r.num_chars; find->y += r.baseline_y_delta; } find->first_char = first = i; find->length = r.num_chars; find->height = r.ymax - r.ymin; find->prev_first = prev_start; // now scan to find xpos find->x = r.x0; for (i=0; first+i < n; ++i) find->x += STB_TEXTEDIT_GETWIDTH(str, first, i); } #define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end) // make the selection/cursor state valid if client altered the string static void stb_textedit_clamp(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) { int n = STB_TEXTEDIT_STRINGLEN(str); if (STB_TEXT_HAS_SELECTION(state)) { if (state->select_start > n) state->select_start = n; if (state->select_end > n) state->select_end = n; // if clamping forced them to be equal, move the cursor to match if (state->select_start == state->select_end) state->cursor = state->select_start; } if (state->cursor > n) state->cursor = n; } // delete characters while updating undo static void stb_textedit_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len) { stb_text_makeundo_delete(str, state, where, len); STB_TEXTEDIT_DELETECHARS(str, where, len); state->has_preferred_x = 0; } // delete the section static void stb_textedit_delete_selection(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) { stb_textedit_clamp(str, state); if (STB_TEXT_HAS_SELECTION(state)) { if (state->select_start < state->select_end) { stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start); state->select_end = state->cursor = state->select_start; } else { stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end); state->select_start = state->cursor = state->select_end; } state->has_preferred_x = 0; } } // canoncialize the selection so start <= end static void stb_textedit_sortselection(STB_TexteditState *state) { if (state->select_end < state->select_start) { int temp = state->select_end; state->select_end = state->select_start; state->select_start = temp; } } // move cursor to first character of selection static void stb_textedit_move_to_first(STB_TexteditState *state) { if (STB_TEXT_HAS_SELECTION(state)) { stb_textedit_sortselection(state); state->cursor = state->select_start; state->select_end = state->select_start; state->has_preferred_x = 0; } } // move cursor to last character of selection static void stb_textedit_move_to_last(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) { if (STB_TEXT_HAS_SELECTION(state)) { stb_textedit_sortselection(state); stb_textedit_clamp(str, state); state->cursor = state->select_end; state->select_start = state->select_end; state->has_preferred_x = 0; } } #ifdef STB_TEXTEDIT_IS_SPACE static int is_word_boundary( STB_TEXTEDIT_STRING *str, int idx ) { return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1; } #ifndef STB_TEXTEDIT_MOVEWORDLEFT static int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING *str, int c ) { --c; // always move at least one character while( c >= 0 && !is_word_boundary( str, c ) ) --c; if( c < 0 ) c = 0; return c; } #define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous #endif #ifndef STB_TEXTEDIT_MOVEWORDRIGHT static int stb_textedit_move_to_word_next( STB_TEXTEDIT_STRING *str, int c ) { const int len = STB_TEXTEDIT_STRINGLEN(str); ++c; // always move at least one character while( c < len && !is_word_boundary( str, c ) ) ++c; if( c > len ) c = len; return c; } #define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next #endif #endif // update selection and cursor to match each other static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state) { if (!STB_TEXT_HAS_SELECTION(state)) state->select_start = state->select_end = state->cursor; else state->cursor = state->select_end; } // API cut: delete selection static int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) { if (STB_TEXT_HAS_SELECTION(state)) { stb_textedit_delete_selection(str,state); // implicitly clamps state->has_preferred_x = 0; return 1; } return 0; } // API paste: replace existing selection with passed-in text static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len) { // if there's a selection, the paste should delete it stb_textedit_clamp(str, state); stb_textedit_delete_selection(str,state); // try to insert the characters if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) { stb_text_makeundo_insert(state, state->cursor, len); state->cursor += len; state->has_preferred_x = 0; return 1; } // remove the undo since we didn't actually insert the characters if (state->undostate.undo_point) --state->undostate.undo_point; return 0; } #ifndef STB_TEXTEDIT_KEYTYPE #define STB_TEXTEDIT_KEYTYPE int #endif // API key: process a keyboard input static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key) { retry: switch (key) { default: { int c = STB_TEXTEDIT_KEYTOTEXT(key); if (c > 0) { STB_TEXTEDIT_CHARTYPE ch = (STB_TEXTEDIT_CHARTYPE) c; // can't add newline in single-line mode if (c == '\n' && state->single_line) break; if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) { stb_text_makeundo_replace(str, state, state->cursor, 1, 1); STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1); if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { ++state->cursor; state->has_preferred_x = 0; } } else { stb_textedit_delete_selection(str,state); // implicitly clamps if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { stb_text_makeundo_insert(state, state->cursor, 1); ++state->cursor; state->has_preferred_x = 0; } } } break; } #ifdef STB_TEXTEDIT_K_INSERT case STB_TEXTEDIT_K_INSERT: state->insert_mode = !state->insert_mode; break; #endif case STB_TEXTEDIT_K_UNDO: stb_text_undo(str, state); state->has_preferred_x = 0; break; case STB_TEXTEDIT_K_REDO: stb_text_redo(str, state); state->has_preferred_x = 0; break; case STB_TEXTEDIT_K_LEFT: // if currently there's a selection, move cursor to start of selection if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_move_to_first(state); else if (state->cursor > 0) --state->cursor; state->has_preferred_x = 0; break; case STB_TEXTEDIT_K_RIGHT: // if currently there's a selection, move cursor to end of selection if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_move_to_last(str, state); else ++state->cursor; stb_textedit_clamp(str, state); state->has_preferred_x = 0; break; case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT: stb_textedit_clamp(str, state); stb_textedit_prep_selection_at_cursor(state); // move selection left if (state->select_end > 0) --state->select_end; state->cursor = state->select_end; state->has_preferred_x = 0; break; #ifdef STB_TEXTEDIT_MOVEWORDLEFT case STB_TEXTEDIT_K_WORDLEFT: if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_move_to_first(state); else { state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor); stb_textedit_clamp( str, state ); } break; case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT: if( !STB_TEXT_HAS_SELECTION( state ) ) stb_textedit_prep_selection_at_cursor(state); state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor); state->select_end = state->cursor; stb_textedit_clamp( str, state ); break; #endif #ifdef STB_TEXTEDIT_MOVEWORDRIGHT case STB_TEXTEDIT_K_WORDRIGHT: if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_move_to_last(str, state); else { state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor); stb_textedit_clamp( str, state ); } break; case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT: if( !STB_TEXT_HAS_SELECTION( state ) ) stb_textedit_prep_selection_at_cursor(state); state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor); state->select_end = state->cursor; stb_textedit_clamp( str, state ); break; #endif case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT: stb_textedit_prep_selection_at_cursor(state); // move selection right ++state->select_end; stb_textedit_clamp(str, state); state->cursor = state->select_end; state->has_preferred_x = 0; break; case STB_TEXTEDIT_K_DOWN: case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT: { StbFindState find; StbTexteditRow row; int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; if (state->single_line) { // on windows, up&down in single-line behave like left&right key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT); goto retry; } if (sel) stb_textedit_prep_selection_at_cursor(state); else if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_move_to_last(str,state); // compute current position of cursor point stb_textedit_clamp(str, state); stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); // now find character position down a row if (find.length) { float goal_x = state->has_preferred_x ? state->preferred_x : find.x; float x; int start = find.first_char + find.length; state->cursor = start; STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); x = row.x0; for (i=0; i < row.num_chars; ++i) { float dx = STB_TEXTEDIT_GETWIDTH(str, start, i); #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE) break; #endif x += dx; if (x > goal_x) break; ++state->cursor; } stb_textedit_clamp(str, state); state->has_preferred_x = 1; state->preferred_x = goal_x; if (sel) state->select_end = state->cursor; } break; } case STB_TEXTEDIT_K_UP: case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: { StbFindState find; StbTexteditRow row; int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; if (state->single_line) { // on windows, up&down become left&right key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT); goto retry; } if (sel) stb_textedit_prep_selection_at_cursor(state); else if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_move_to_first(state); // compute current position of cursor point stb_textedit_clamp(str, state); stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); // can only go up if there's a previous row if (find.prev_first != find.first_char) { // now find character position up a row float goal_x = state->has_preferred_x ? state->preferred_x : find.x; float x; state->cursor = find.prev_first; STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); x = row.x0; for (i=0; i < row.num_chars; ++i) { float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i); #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE) break; #endif x += dx; if (x > goal_x) break; ++state->cursor; } stb_textedit_clamp(str, state); state->has_preferred_x = 1; state->preferred_x = goal_x; if (sel) state->select_end = state->cursor; } break; } case STB_TEXTEDIT_K_DELETE: case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT: if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_delete_selection(str, state); else { int n = STB_TEXTEDIT_STRINGLEN(str); if (state->cursor < n) stb_textedit_delete(str, state, state->cursor, 1); } state->has_preferred_x = 0; break; case STB_TEXTEDIT_K_BACKSPACE: case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT: if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_delete_selection(str, state); else { stb_textedit_clamp(str, state); if (state->cursor > 0) { stb_textedit_delete(str, state, state->cursor-1, 1); --state->cursor; } } state->has_preferred_x = 0; break; #ifdef STB_TEXTEDIT_K_TEXTSTART2 case STB_TEXTEDIT_K_TEXTSTART2: #endif case STB_TEXTEDIT_K_TEXTSTART: state->cursor = state->select_start = state->select_end = 0; state->has_preferred_x = 0; break; #ifdef STB_TEXTEDIT_K_TEXTEND2 case STB_TEXTEDIT_K_TEXTEND2: #endif case STB_TEXTEDIT_K_TEXTEND: state->cursor = STB_TEXTEDIT_STRINGLEN(str); state->select_start = state->select_end = 0; state->has_preferred_x = 0; break; #ifdef STB_TEXTEDIT_K_TEXTSTART2 case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT: #endif case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT: stb_textedit_prep_selection_at_cursor(state); state->cursor = state->select_end = 0; state->has_preferred_x = 0; break; #ifdef STB_TEXTEDIT_K_TEXTEND2 case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT: #endif case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT: stb_textedit_prep_selection_at_cursor(state); state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str); state->has_preferred_x = 0; break; #ifdef STB_TEXTEDIT_K_LINESTART2 case STB_TEXTEDIT_K_LINESTART2: #endif case STB_TEXTEDIT_K_LINESTART: stb_textedit_clamp(str, state); stb_textedit_move_to_first(state); if (state->single_line) state->cursor = 0; else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE) --state->cursor; state->has_preferred_x = 0; break; #ifdef STB_TEXTEDIT_K_LINEEND2 case STB_TEXTEDIT_K_LINEEND2: #endif case STB_TEXTEDIT_K_LINEEND: { int n = STB_TEXTEDIT_STRINGLEN(str); stb_textedit_clamp(str, state); stb_textedit_move_to_first(state); if (state->single_line) state->cursor = n; else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE) ++state->cursor; state->has_preferred_x = 0; break; } #ifdef STB_TEXTEDIT_K_LINESTART2 case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT: #endif case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT: stb_textedit_clamp(str, state); stb_textedit_prep_selection_at_cursor(state); if (state->single_line) state->cursor = 0; else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE) --state->cursor; state->select_end = state->cursor; state->has_preferred_x = 0; break; #ifdef STB_TEXTEDIT_K_LINEEND2 case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT: #endif case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: { int n = STB_TEXTEDIT_STRINGLEN(str); stb_textedit_clamp(str, state); stb_textedit_prep_selection_at_cursor(state); if (state->single_line) state->cursor = n; else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE) ++state->cursor; state->select_end = state->cursor; state->has_preferred_x = 0; break; } // @TODO: // STB_TEXTEDIT_K_PGUP - move cursor up a page // STB_TEXTEDIT_K_PGDOWN - move cursor down a page } } ///////////////////////////////////////////////////////////////////////////// // // Undo processing // // @OPTIMIZE: the undo/redo buffer should be circular static void stb_textedit_flush_redo(StbUndoState *state) { state->redo_point = STB_TEXTEDIT_UNDOSTATECOUNT; state->redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT; } // discard the oldest entry in the undo list static void stb_textedit_discard_undo(StbUndoState *state) { if (state->undo_point > 0) { // if the 0th undo state has characters, clean those up if (state->undo_rec[0].char_storage >= 0) { int n = state->undo_rec[0].insert_length, i; // delete n characters from all other records state->undo_char_point -= n; STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE))); for (i=0; i < state->undo_point; ++i) if (state->undo_rec[i].char_storage >= 0) state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it } --state->undo_point; STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0]))); } } // discard the oldest entry in the redo list--it's bad if this // ever happens, but because undo & redo have to store the actual // characters in different cases, the redo character buffer can // fill up even though the undo buffer didn't static void stb_textedit_discard_redo(StbUndoState *state) { int k = STB_TEXTEDIT_UNDOSTATECOUNT-1; if (state->redo_point <= k) { // if the k'th undo state has characters, clean those up if (state->undo_rec[k].char_storage >= 0) { int n = state->undo_rec[k].insert_length, i; // move the remaining redo character data to the end of the buffer state->redo_char_point += 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))); // adjust the position of all the other records to account for above memmove for (i=state->redo_point; i < k; ++i) if (state->undo_rec[i].char_storage >= 0) state->undo_rec[i].char_storage += n; } // now move all the redo records towards the end of the buffer; the first one is at 'redo_point' // {DEAR IMGUI] size_t move_size = (size_t)((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0])); const char* buf_begin = (char*)state->undo_rec; (void)buf_begin; const char* buf_end = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end; IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin); IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end); STB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size); // now move redo_point to point to the new one ++state->redo_point; } } static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars) { // any time we create a new undo record, we discard redo stb_textedit_flush_redo(state); // if we have no free records, we have to make room, by sliding the // existing records down if (state->undo_point == STB_TEXTEDIT_UNDOSTATECOUNT) stb_textedit_discard_undo(state); // if the characters to store won't possibly fit in the buffer, we can't undo if (numchars > STB_TEXTEDIT_UNDOCHARCOUNT) { state->undo_point = 0; state->undo_char_point = 0; return NULL; } // if we don't have enough free characters in the buffer, we have to make room while (state->undo_char_point + numchars > STB_TEXTEDIT_UNDOCHARCOUNT) stb_textedit_discard_undo(state); return &state->undo_rec[state->undo_point++]; } static STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len) { StbUndoRecord *r = stb_text_create_undo_record(state, insert_len); if (r == NULL) return NULL; r->where = pos; r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len; r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len; if (insert_len == 0) { r->char_storage = -1; return NULL; } else { r->char_storage = state->undo_char_point; state->undo_char_point += insert_len; return &state->undo_char[r->char_storage]; } } static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) { StbUndoState *s = &state->undostate; StbUndoRecord u, *r; if (s->undo_point == 0) return; // we need to do two things: apply the undo record, and create a redo record u = s->undo_rec[s->undo_point-1]; r = &s->undo_rec[s->redo_point-1]; r->char_storage = -1; r->insert_length = u.delete_length; r->delete_length = u.insert_length; r->where = u.where; if (u.delete_length) { // if the undo record says to delete characters, then the redo record will // need to re-insert the characters that get deleted, so we need to store // them. // there are three cases: // there's enough room to store the characters // characters stored for *redoing* don't leave room for redo // characters stored for *undoing* don't leave room for redo // if the last is true, we have to bail if (s->undo_char_point + u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT) { // the undo records take up too much character space; there's no space to store the redo characters r->insert_length = 0; } else { int i; // there's definitely room to store the characters eventually while (s->undo_char_point + u.delete_length > s->redo_char_point) { // should never happen: if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT) return; // there's currently not enough room, so discard a redo record stb_textedit_discard_redo(s); } r = &s->undo_rec[s->redo_point-1]; r->char_storage = s->redo_char_point - u.delete_length; s->redo_char_point = s->redo_char_point - u.delete_length; // now save the characters for (i=0; i < u.delete_length; ++i) s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i); } // now we can carry out the deletion STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length); } // check type of recorded action: if (u.insert_length) { // easy case: was a deletion, so we need to insert n characters STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length); s->undo_char_point -= u.insert_length; } state->cursor = u.where + u.insert_length; s->undo_point--; s->redo_point--; } static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) { StbUndoState *s = &state->undostate; StbUndoRecord *u, r; if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT) return; // we need to do two things: apply the redo record, and create an undo record u = &s->undo_rec[s->undo_point]; r = s->undo_rec[s->redo_point]; // we KNOW there must be room for the undo record, because the redo record // was derived from an undo record u->delete_length = r.insert_length; u->insert_length = r.delete_length; u->where = r.where; u->char_storage = -1; if (r.delete_length) { // the redo record requires us to delete characters, so the undo record // needs to store the characters if (s->undo_char_point + u->insert_length > s->redo_char_point) { u->insert_length = 0; u->delete_length = 0; } else { int i; u->char_storage = s->undo_char_point; s->undo_char_point = s->undo_char_point + u->insert_length; // now save the characters for (i=0; i < u->insert_length; ++i) s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i); } STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length); } if (r.insert_length) { // easy case: need to insert n characters STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length); s->redo_char_point += r.insert_length; } state->cursor = r.where + r.insert_length; s->undo_point++; s->redo_point++; } static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length) { stb_text_createundo(&state->undostate, where, 0, length); } static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length) { int i; STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0); if (p) { for (i=0; i < length; ++i) p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); } } static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length) { int i; STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length); if (p) { for (i=0; i < old_length; ++i) p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); } } // reset the state to default static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line) { state->undostate.undo_point = 0; state->undostate.undo_char_point = 0; state->undostate.redo_point = STB_TEXTEDIT_UNDOSTATECOUNT; state->undostate.redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT; state->select_end = state->select_start = 0; state->cursor = 0; state->has_preferred_x = 0; state->preferred_x = 0; state->cursor_at_end_of_line = 0; state->initialized = 1; state->single_line = (unsigned char) is_single_line; state->insert_mode = 0; } // API initialize static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line) { stb_textedit_clear_state(state, is_single_line); } #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-qual" #endif static int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE const *ctext, int len) { return stb_textedit_paste_internal(str, state, (STB_TEXTEDIT_CHARTYPE *) ctext, len); } #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic pop #endif #endif//STB_TEXTEDIT_IMPLEMENTATION /* ------------------------------------------------------------------------------ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2017 Sean Barrett Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ ================================================ FILE: src/imgui/imstb_truetype.hpp ================================================ // [DEAR IMGUI] // This is a slightly modified version of stb_truetype.h 1.20. // Mostly fixing for compiler and static analyzer warnings. // Grep for [DEAR IMGUI] to find the changes. // stb_truetype.h - v1.20 - public domain // authored from 2009-2016 by Sean Barrett / RAD Game Tools // // This library processes TrueType files: // parse files // extract glyph metrics // extract glyph shapes // render glyphs to one-channel bitmaps with antialiasing (box filter) // render glyphs to one-channel SDF bitmaps (signed-distance field/function) // // Todo: // non-MS cmaps // crashproof on bad data // hinting? (no longer patented) // cleartype-style AA? // optimize: use simple memory allocator for intermediates // optimize: build edge-list directly from curves // optimize: rasterize directly from curves? // // ADDITIONAL CONTRIBUTORS // // Mikko Mononen: compound shape support, more cmap formats // Tor Andersson: kerning, subpixel rendering // Dougall Johnson: OpenType / Type 2 font handling // Daniel Ribeiro Maciel: basic GPOS-based kerning // // Misc other: // Ryan Gordon // Simon Glass // github:IntellectualKitty // Imanol Celaya // Daniel Ribeiro Maciel // // Bug/warning reports/fixes: // "Zer" on mollyrocket Fabian "ryg" Giesen // Cass Everitt Martins Mozeiko // stoiko (Haemimont Games) Cap Petschulat // Brian Hook Omar Cornut // Walter van Niftrik github:aloucks // David Gow Peter LaValle // David Given Sergey Popov // Ivan-Assen Ivanov Giumo X. Clanjor // Anthony Pesch Higor Euripedes // Johan Duparc Thomas Fields // Hou Qiming Derek Vinyard // Rob Loach Cort Stratton // Kenney Phillis Jr. github:oyvindjam // Brian Costabile github:vassvik // // VERSION HISTORY // // 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() // 1.19 (2018-02-11) GPOS kerning, STBTT_fmod // 1.18 (2018-01-29) add missing function // 1.17 (2017-07-23) make more arguments const; doc fix // 1.16 (2017-07-12) SDF support // 1.15 (2017-03-03) make more arguments const // 1.14 (2017-01-16) num-fonts-in-TTC function // 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts // 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual // 1.11 (2016-04-02) fix unused-variable warning // 1.10 (2016-04-02) user-defined fabs(); rare memory leak; remove duplicate typedef // 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use allocation userdata properly // 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges // 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; // variant PackFontRanges to pack and render in separate phases; // fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); // fixed an assert() bug in the new rasterizer // replace assert() with STBTT_assert() in new rasterizer // // Full history can be found at the end of this file. // // LICENSE // // See end of file for license information. // // USAGE // // Include this file in whatever places need to refer to it. In ONE C/C++ // file, write: // #define STB_TRUETYPE_IMPLEMENTATION // before the #include of this file. This expands out the actual // implementation into that C/C++ file. // // To make the implementation private to the file that generates the implementation, // #define STBTT_STATIC // // Simple 3D API (don't ship this, but it's fine for tools and quick start) // stbtt_BakeFontBitmap() -- bake a font to a bitmap for use as texture // stbtt_GetBakedQuad() -- compute quad to draw for a given char // // Improved 3D API (more shippable): // #include "stb_rect_pack.h" -- optional, but you really want it // stbtt_PackBegin() // stbtt_PackSetOversampling() -- for improved quality on small fonts // stbtt_PackFontRanges() -- pack and renders // stbtt_PackEnd() // stbtt_GetPackedQuad() // // "Load" a font file from a memory buffer (you have to keep the buffer loaded) // stbtt_InitFont() // stbtt_GetFontOffsetForIndex() -- indexing for TTC font collections // stbtt_GetNumberOfFonts() -- number of fonts for TTC font collections // // Render a unicode codepoint to a bitmap // stbtt_GetCodepointBitmap() -- allocates and returns a bitmap // stbtt_MakeCodepointBitmap() -- renders into bitmap you provide // stbtt_GetCodepointBitmapBox() -- how big the bitmap must be // // Character advance/positioning // stbtt_GetCodepointHMetrics() // stbtt_GetFontVMetrics() // stbtt_GetFontVMetricsOS2() // stbtt_GetCodepointKernAdvance() // // Starting with version 1.06, the rasterizer was replaced with a new, // faster and generally-more-precise rasterizer. The new rasterizer more // accurately measures pixel coverage for anti-aliasing, except in the case // where multiple shapes overlap, in which case it overestimates the AA pixel // coverage. Thus, anti-aliasing of intersecting shapes may look wrong. If // this turns out to be a problem, you can re-enable the old rasterizer with // #define STBTT_RASTERIZER_VERSION 1 // which will incur about a 15% speed hit. // // ADDITIONAL DOCUMENTATION // // Immediately after this block comment are a series of sample programs. // // After the sample programs is the "header file" section. This section // includes documentation for each API function. // // Some important concepts to understand to use this library: // // Codepoint // Characters are defined by unicode codepoints, e.g. 65 is // uppercase A, 231 is lowercase c with a cedilla, 0x7e30 is // the hiragana for "ma". // // Glyph // A visual character shape (every codepoint is rendered as // some glyph) // // Glyph index // A font-specific integer ID representing a glyph // // Baseline // Glyph shapes are defined relative to a baseline, which is the // bottom of uppercase characters. Characters extend both above // and below the baseline. // // Current Point // As you draw text to the screen, you keep track of a "current point" // which is the origin of each character. The current point's vertical // position is the baseline. Even "baked fonts" use this model. // // Vertical Font Metrics // The vertical qualities of the font, used to vertically position // and space the characters. See docs for stbtt_GetFontVMetrics. // // Font Size in Pixels or Points // The preferred interface for specifying font sizes in stb_truetype // is to specify how tall the font's vertical extent should be in pixels. // If that sounds good enough, skip the next paragraph. // // Most font APIs instead use "points", which are a common typographic // measurement for describing font size, defined as 72 points per inch. // stb_truetype provides a point API for compatibility. However, true // "per inch" conventions don't make much sense on computer displays // since different monitors have different number of pixels per // inch. For example, Windows traditionally uses a convention that // there are 96 pixels per inch, thus making 'inch' measurements have // nothing to do with inches, and thus effectively defining a point to // be 1.333 pixels. Additionally, the TrueType font data provides // an explicit scale factor to scale a given font's glyphs to points, // but the author has observed that this scale factor is often wrong // for non-commercial fonts, thus making fonts scaled in points // according to the TrueType spec incoherently sized in practice. // // DETAILED USAGE: // // Scale: // Select how high you want the font to be, in points or pixels. // Call ScaleForPixelHeight or ScaleForMappingEmToPixels to compute // a scale factor SF that will be used by all other functions. // // Baseline: // You need to select a y-coordinate that is the baseline of where // your text will appear. Call GetFontBoundingBox to get the baseline-relative // bounding box for all characters. SF*-y0 will be the distance in pixels // that the worst-case character could extend above the baseline, so if // you want the top edge of characters to appear at the top of the // screen where y=0, then you would set the baseline to SF*-y0. // // Current point: // Set the current point where the first character will appear. The // first character could extend left of the current point; this is font // dependent. You can either choose a current point that is the leftmost // point and hope, or add some padding, or check the bounding box or // left-side-bearing of the first character to be displayed and set // the current point based on that. // // Displaying a character: // Compute the bounding box of the character. It will contain signed values // relative to . I.e. if it returns x0,y0,x1,y1, // then the character should be displayed in the rectangle from // to = 32 && *text < 128) { stbtt_aligned_quad q; stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);//1=opengl & d3d10+,0=d3d9 glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0); glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0); glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1); glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1); } ++text; } glEnd(); } #endif // // ////////////////////////////////////////////////////////////////////////////// // // Complete program (this compiles): get a single bitmap, print as ASCII art // #if 0 #include #define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation #include "stb_truetype.h" char ttf_buffer[1<<25]; int main(int argc, char **argv) { stbtt_fontinfo font; unsigned char *bitmap; int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20); fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb")); stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0)); bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0); for (j=0; j < h; ++j) { for (i=0; i < w; ++i) putchar(" .:ioVM@"[bitmap[j*w+i]>>5]); putchar('\n'); } return 0; } #endif // // Output: // // .ii. // @@@@@@. // V@Mio@@o // :i. V@V // :oM@@M // :@@@MM@M // @@o o@M // :@@. M@M // @@@o@@@@ // :M@@V:@@. // ////////////////////////////////////////////////////////////////////////////// // // Complete program: print "Hello World!" banner, with bugs // #if 0 char buffer[24<<20]; unsigned char screen[20][79]; int main(int arg, char **argv) { stbtt_fontinfo font; int i,j,ascent,baseline,ch=0; float scale, xpos=2; // leave a little padding in case the character extends left char *text = "Heljo World!"; // intentionally misspelled to show 'lj' brokenness fread(buffer, 1, 1000000, fopen("c:/windows/fonts/arialbd.ttf", "rb")); stbtt_InitFont(&font, buffer, 0); scale = stbtt_ScaleForPixelHeight(&font, 15); stbtt_GetFontVMetrics(&font, &ascent,0,0); baseline = (int) (ascent*scale); while (text[ch]) { int advance,lsb,x0,y0,x1,y1; float x_shift = xpos - (float) floor(xpos); stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb); stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale,scale,x_shift,0, &x0,&y0,&x1,&y1); stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(int) xpos + x0], x1-x0,y1-y0, 79, scale,scale,x_shift,0, text[ch]); // note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong // because this API is really for baking character bitmaps into textures. if you want to render // a sequence of characters, you really need to render each bitmap to a temp buffer, then // "alpha blend" that into the working buffer xpos += (advance * scale); if (text[ch+1]) xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch],text[ch+1]); ++ch; } for (j=0; j < 20; ++j) { for (i=0; i < 78; ++i) putchar(" .:ioVM@"[screen[j][i]>>5]); putchar('\n'); } return 0; } #endif ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// //// //// INTEGRATION WITH YOUR CODEBASE //// //// The following sections allow you to supply alternate definitions //// of C library functions used by stb_truetype, e.g. if you don't //// link with the C runtime library. #ifdef STB_TRUETYPE_IMPLEMENTATION // #define your own (u)stbtt_int8/16/32 before including to override this #ifndef stbtt_uint8 typedef unsigned char stbtt_uint8; typedef signed char stbtt_int8; typedef unsigned short stbtt_uint16; typedef signed short stbtt_int16; typedef unsigned int stbtt_uint32; typedef signed int stbtt_int32; #endif typedef char stbtt__check_size32[sizeof(stbtt_int32)==4 ? 1 : -1]; typedef char stbtt__check_size16[sizeof(stbtt_int16)==2 ? 1 : -1]; // e.g. #define your own STBTT_ifloor/STBTT_iceil() to avoid math.h #ifndef STBTT_ifloor #include #define STBTT_ifloor(x) ((int) floor(x)) #define STBTT_iceil(x) ((int) ceil(x)) #endif #ifndef STBTT_sqrt #include #define STBTT_sqrt(x) sqrt(x) #define STBTT_pow(x,y) pow(x,y) #endif #ifndef STBTT_fmod #include #define STBTT_fmod(x,y) fmod(x,y) #endif #ifndef STBTT_cos #include #define STBTT_cos(x) cos(x) #define STBTT_acos(x) acos(x) #endif #ifndef STBTT_fabs #include #define STBTT_fabs(x) fabs(x) #endif // #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h #ifndef STBTT_malloc #include #define STBTT_malloc(x,u) ((void)(u),malloc(x)) #define STBTT_free(x,u) ((void)(u),free(x)) #endif #ifndef STBTT_assert #include #define STBTT_assert(x) assert(x) #endif #ifndef STBTT_strlen #include #define STBTT_strlen(x) strlen(x) #endif #ifndef STBTT_memcpy #include #define STBTT_memcpy memcpy #define STBTT_memset memset #endif #endif /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// //// //// INTERFACE //// //// #ifndef __STB_INCLUDE_STB_TRUETYPE_H__ #define __STB_INCLUDE_STB_TRUETYPE_H__ #ifdef STBTT_STATIC #define STBTT_DEF static #else #define STBTT_DEF extern #endif #ifdef __cplusplus extern "C" { #endif // private structure typedef struct { unsigned char *data; int cursor; int size; } stbtt__buf; ////////////////////////////////////////////////////////////////////////////// // // TEXTURE BAKING API // // If you use this API, you only have to call two functions ever. // typedef struct { unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap float xoff,yoff,xadvance; } stbtt_bakedchar; STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf) float pixel_height, // height of font in pixels unsigned char *pixels, int pw, int ph, // bitmap to be filled in int first_char, int num_chars, // characters to bake stbtt_bakedchar *chardata); // you allocate this, it's num_chars long // if return is positive, the first unused row of the bitmap // if return is negative, returns the negative of the number of characters that fit // if return is 0, no characters fit and no rows were used // This uses a very crappy packing. typedef struct { float x0,y0,s0,t0; // top-left float x1,y1,s1,t1; // bottom-right } stbtt_aligned_quad; STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar *chardata, int pw, int ph, // same data as above int char_index, // character to display float *xpos, float *ypos, // pointers to current position in screen pixel space stbtt_aligned_quad *q, // output: quad to draw int opengl_fillrule); // true if opengl fill rule; false if DX9 or earlier // Call GetBakedQuad with char_index = 'character - first_char', and it // creates the quad you need to draw and advances the current position. // // The coordinate system used assumes y increases downwards. // // Characters will extend both above and below the current position; // see discussion of "BASELINE" above. // // It's inefficient; you might want to c&p it and optimize it. STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char *fontdata, int index, float size, float *ascent, float *descent, float *lineGap); // Query the font vertical metrics without having to create a font first. ////////////////////////////////////////////////////////////////////////////// // // NEW TEXTURE BAKING API // // This provides options for packing multiple fonts into one atlas, not // perfectly but better than nothing. typedef struct { unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap float xoff,yoff,xadvance; float xoff2,yoff2; } stbtt_packedchar; typedef struct stbtt_pack_context stbtt_pack_context; typedef struct stbtt_fontinfo stbtt_fontinfo; #ifndef STB_RECT_PACK_VERSION typedef struct stbrp_rect stbrp_rect; #endif STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context); // Initializes a packing context stored in the passed-in stbtt_pack_context. // Future calls using this context will pack characters into the bitmap passed // in here: a 1-channel bitmap that is width * height. stride_in_bytes is // the distance from one row to the next (or 0 to mean they are packed tightly // together). "padding" is the amount of padding to leave between each // character (normally you want '1' for bitmaps you'll use as textures with // bilinear filtering). // // Returns 0 on failure, 1 on success. STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc); // Cleans up the packing context and frees all memory. #define STBTT_POINT_SIZE(x) (-(x)) STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, float font_size, int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range); // Creates character bitmaps from the font_index'th font found in fontdata (use // font_index=0 if you don't know what that is). It creates num_chars_in_range // bitmaps for characters with unicode values starting at first_unicode_char_in_range // and increasing. Data for how to render them is stored in chardata_for_range; // pass these to stbtt_GetPackedQuad to get back renderable quads. // // font_size is the full height of the character from ascender to descender, // as computed by stbtt_ScaleForPixelHeight. To use a point size as computed // by stbtt_ScaleForMappingEmToPixels, wrap the point size in STBTT_POINT_SIZE() // and pass that result as 'font_size': // ..., 20 , ... // font max minus min y is 20 pixels tall // ..., STBTT_POINT_SIZE(20), ... // 'M' is 20 pixels tall typedef struct { float font_size; int first_unicode_codepoint_in_range; // if non-zero, then the chars are continuous, and this is the first codepoint int *array_of_unicode_codepoints; // if non-zero, then this is an array of unicode codepoints int num_chars; stbtt_packedchar *chardata_for_range; // output unsigned char h_oversample, v_oversample; // don't set these, they're used internally } stbtt_pack_range; STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges); // Creates character bitmaps from multiple ranges of characters stored in // ranges. This will usually create a better-packed bitmap than multiple // calls to stbtt_PackFontRange. Note that you can call this multiple // times within a single PackBegin/PackEnd. STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample); // Oversampling a font increases the quality by allowing higher-quality subpixel // positioning, and is especially valuable at smaller text sizes. // // This function sets the amount of oversampling for all following calls to // stbtt_PackFontRange(s) or stbtt_PackFontRangesGatherRects for a given // pack context. The default (no oversampling) is achieved by h_oversample=1 // and v_oversample=1. The total number of pixels required is // h_oversample*v_oversample larger than the default; for example, 2x2 // oversampling requires 4x the storage of 1x1. For best results, render // oversampled textures with bilinear filtering. Look at the readme in // stb/tests/oversample for information about oversampled fonts // // To use with PackFontRangesGather etc., you must set it before calls // call to PackFontRangesGatherRects. STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip); // If skip != 0, this tells stb_truetype to skip any codepoints for which // there is no corresponding glyph. If skip=0, which is the default, then // codepoints without a glyph recived the font's "missing character" glyph, // typically an empty box by convention. STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar *chardata, int pw, int ph, // same data as above int char_index, // character to display float *xpos, float *ypos, // pointers to current position in screen pixel space stbtt_aligned_quad *q, // output: quad to draw int align_to_integer); STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects); STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects); STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects); // Calling these functions in sequence is roughly equivalent to calling // stbtt_PackFontRanges(). If you more control over the packing of multiple // fonts, or if you want to pack custom data into a font texture, take a look // at the source to of stbtt_PackFontRanges() and create a custom version // using these functions, e.g. call GatherRects multiple times, // building up a single array of rects, then call PackRects once, // then call RenderIntoRects repeatedly. This may result in a // better packing than calling PackFontRanges multiple times // (or it may not). // this is an opaque structure that you shouldn't mess with which holds // all the context needed from PackBegin to PackEnd. struct stbtt_pack_context { void *user_allocator_context; void *pack_info; int width; int height; int stride_in_bytes; int padding; int skip_missing; unsigned int h_oversample, v_oversample; unsigned char *pixels; void *nodes; }; ////////////////////////////////////////////////////////////////////////////// // // FONT LOADING // // STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data); // This function will determine the number of fonts in a font file. TrueType // collection (.ttc) files may contain multiple fonts, while TrueType font // (.ttf) files only contain one font. The number of fonts can be used for // indexing with the previous function where the index is between zero and one // less than the total fonts. If an error occurs, -1 is returned. STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index); // Each .ttf/.ttc file may have more than one font. Each font has a sequential // index number starting from 0. Call this function to get the font offset for // a given index; it returns -1 if the index is out of range. A regular .ttf // file will only define one font and it always be at offset 0, so it will // return '0' for index 0, and -1 for all other indices. // The following structure is defined publicly so you can declare one on // the stack or as a global or etc, but you should treat it as opaque. struct stbtt_fontinfo { void * userdata; unsigned char * data; // pointer to .ttf file int fontstart; // offset of start of font int numGlyphs; // number of glyphs, needed for range checking int loca,head,glyf,hhea,hmtx,kern,gpos; // table locations as offset from start of .ttf int index_map; // a cmap mapping for our chosen character encoding int indexToLocFormat; // format needed to map from glyph index to glyph stbtt__buf cff; // cff font data stbtt__buf charstrings; // the charstring index stbtt__buf gsubrs; // global charstring subroutines index stbtt__buf subrs; // private charstring subroutines index stbtt__buf fontdicts; // array of font dicts stbtt__buf fdselect; // map from glyph to fontdict }; STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset); // Given an offset into the file that defines a font, this function builds // the necessary cached info for the rest of the system. You must allocate // the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't // need to do anything special to free it, because the contents are pure // value data with no additional data structures. Returns 0 on failure. ////////////////////////////////////////////////////////////////////////////// // // CHARACTER TO GLYPH-INDEX CONVERSIOn STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint); // If you're going to perform multiple operations on the same character // and you want a speed-up, call this function with the character you're // going to process, then use glyph-based functions instead of the // codepoint-based functions. // Returns 0 if the character codepoint is not defined in the font. ////////////////////////////////////////////////////////////////////////////// // // CHARACTER PROPERTIES // STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels); // computes a scale factor to produce a font whose "height" is 'pixels' tall. // Height is measured as the distance from the highest ascender to the lowest // descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics // and computing: // scale = pixels / (ascent - descent) // so if you prefer to measure height by the ascent only, use a similar calculation. STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels); // computes a scale factor to produce a font whose EM size is mapped to // 'pixels' tall. This is probably what traditional APIs compute, but // I'm not positive. STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap); // ascent is the coordinate above the baseline the font extends; descent // is the coordinate below the baseline the font extends (i.e. it is typically negative) // lineGap is the spacing between one row's descent and the next row's ascent... // so you should advance the vertical position by "*ascent - *descent + *lineGap" // these are expressed in unscaled coordinates, so you must multiply by // the scale factor for a given size STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap); // analogous to GetFontVMetrics, but returns the "typographic" values from the OS/2 // table (specific to MS/Windows TTF files). // // Returns 1 on success (table present), 0 on failure. STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1); // the bounding box around all possible characters STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing); // leftSideBearing is the offset from the current horizontal position to the left edge of the character // advanceWidth is the offset from the current horizontal position to the next horizontal position // these are expressed in unscaled coordinates STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2); // an additional amount to add to the 'advance' value between ch1 and ch2 STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1); // Gets the bounding box of the visible part of the glyph, in unscaled coordinates STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing); STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2); STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1); // as above, but takes one or more glyph indices for greater efficiency ////////////////////////////////////////////////////////////////////////////// // // GLYPH SHAPES (you probably don't need these, but they have to go before // the bitmaps for C declaration-order reasons) // #ifndef STBTT_vmove // you can predefine these to use different values (but why?) enum { STBTT_vmove=1, STBTT_vline, STBTT_vcurve, STBTT_vcubic }; #endif #ifndef stbtt_vertex // you can predefine this to use different values // (we share this with other code at RAD) #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file typedef struct { stbtt_vertex_type x,y,cx,cy,cx1,cy1; unsigned char type,padding; } stbtt_vertex; #endif STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index); // returns non-zero if nothing is drawn for this glyph STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices); STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices); // returns # of vertices and fills *vertices with the pointer to them // these are expressed in "unscaled" coordinates // // The shape is a series of contours. Each one starts with // a STBTT_moveto, then consists of a series of mixed // STBTT_lineto and STBTT_curveto segments. A lineto // draws a line from previous endpoint to its x,y; a curveto // draws a quadratic bezier from previous endpoint to // its x,y, using cx,cy as the bezier control point. STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices); // frees the data allocated above ////////////////////////////////////////////////////////////////////////////// // // BITMAP RENDERING // STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata); // frees the bitmap allocated below STBTT_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); // allocates a large-enough single-channel 8bpp bitmap and renders the // specified character/glyph at the specified scale into it, with // antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque). // *width & *height are filled out with the width & height of the bitmap, // which is stored left-to-right, top-to-bottom. // // xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap STBTT_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); // the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel // shift for the character STBTT_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); // the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap // in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap // is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the // width and height and positioning info for it first. STBTT_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); // same as stbtt_MakeCodepointBitmap, but you can specify a subpixel // shift for the character STBTT_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); // same as stbtt_MakeCodepointBitmapSubpixel, but prefiltering // is performed (see stbtt_PackSetOversampling) STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); // get the bbox of the bitmap centered around the glyph origin; so the // bitmap width is ix1-ix0, height is iy1-iy0, and location to place // the bitmap top left is (leftSideBearing*scale,iy0). // (Note that the bitmap uses y-increases-down, but the shape uses // y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.) STBTT_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); // same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel // shift for the character // the following functions are equivalent to the above functions, but operate // on glyph indices instead of Unicode codepoints (for efficiency) STBTT_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); STBTT_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); STBTT_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); STBTT_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); STBTT_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); STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); STBTT_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); // @TODO: don't expose this structure typedef struct { int w,h,stride; unsigned char *pixels; } stbtt__bitmap; // rasterize a shape with quadratic beziers into a bitmap STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, // 1-channel bitmap to draw into float flatness_in_pixels, // allowable error of curve in pixels stbtt_vertex *vertices, // array of vertices defining shape int num_verts, // number of vertices in above array float scale_x, float scale_y, // scale applied to input vertices float shift_x, float shift_y, // translation applied to input vertices int x_off, int y_off, // another translation applied to input int invert, // if non-zero, vertically flip shape void *userdata); // context for to STBTT_MALLOC ////////////////////////////////////////////////////////////////////////////// // // Signed Distance Function (or Field) rendering STBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata); // frees the SDF bitmap allocated below STBTT_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); STBTT_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); // These functions compute a discretized SDF field for a single character, suitable for storing // in a single-channel texture, sampling with bilinear filtering, and testing against // larger than some threshold to produce scalable fonts. // info -- the font // scale -- controls the size of the resulting SDF bitmap, same as it would be creating a regular bitmap // glyph/codepoint -- the character to generate the SDF for // padding -- extra "pixels" around the character which are filled with the distance to the character (not 0), // which allows effects like bit outlines // onedge_value -- value 0-255 to test the SDF against to reconstruct the character (i.e. the isocontour of the character) // pixel_dist_scale -- what value the SDF should increase by when moving one SDF "pixel" away from the edge (on the 0..255 scale) // if positive, > onedge_value is inside; if negative, < onedge_value is inside // width,height -- output height & width of the SDF bitmap (including padding) // xoff,yoff -- output origin of the character // return value -- a 2D array of bytes 0..255, width*height in size // // pixel_dist_scale & onedge_value are a scale & bias that allows you to make // optimal use of the limited 0..255 for your application, trading off precision // and special effects. SDF values outside the range 0..255 are clamped to 0..255. // // Example: // scale = stbtt_ScaleForPixelHeight(22) // padding = 5 // onedge_value = 180 // pixel_dist_scale = 180/5.0 = 36.0 // // This will create an SDF bitmap in which the character is about 22 pixels // high but the whole bitmap is about 22+5+5=32 pixels high. To produce a filled // shape, sample the SDF at each pixel and fill the pixel if the SDF value // is greater than or equal to 180/255. (You'll actually want to antialias, // which is beyond the scope of this example.) Additionally, you can compute // offset outlines (e.g. to stroke the character border inside & outside, // or only outside). For example, to fill outside the character up to 3 SDF // pixels, you would compare against (180-36.0*3)/255 = 72/255. The above // choice of variables maps a range from 5 pixels outside the shape to // 2 pixels inside the shape to 0..255; this is intended primarily for apply // outside effects only (the interior range is needed to allow proper // antialiasing of the font at *smaller* sizes) // // The function computes the SDF analytically at each SDF pixel, not by e.g. // building a higher-res bitmap and approximating it. In theory the quality // should be as high as possible for an SDF of this size & representation, but // unclear if this is true in practice (perhaps building a higher-res bitmap // and computing from that can allow drop-out prevention). // // The algorithm has not been optimized at all, so expect it to be slow // if computing lots of characters or very large sizes. ////////////////////////////////////////////////////////////////////////////// // // Finding the right font... // // You should really just solve this offline, keep your own tables // of what font is what, and don't try to get it out of the .ttf file. // That's because getting it out of the .ttf file is really hard, because // the names in the file can appear in many possible encodings, in many // possible languages, and e.g. if you need a case-insensitive comparison, // the details of that depend on the encoding & language in a complex way // (actually underspecified in truetype, but also gigantic). // // But you can use the provided functions in two possible ways: // stbtt_FindMatchingFont() will use *case-sensitive* comparisons on // unicode-encoded names to try to find the font you want; // you can run this before calling stbtt_InitFont() // // stbtt_GetFontNameString() lets you get any of the various strings // from the file yourself and do your own comparisons on them. // You have to have called stbtt_InitFont() first. STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags); // returns the offset (not index) of the font that matches, or -1 if none // if you use STBTT_MACSTYLE_DONTCARE, use a font name like "Arial Bold". // if you use any other flag, use a font name like "Arial"; this checks // the 'macStyle' header field; i don't know if fonts set this consistently #define STBTT_MACSTYLE_DONTCARE 0 #define STBTT_MACSTYLE_BOLD 1 #define STBTT_MACSTYLE_ITALIC 2 #define STBTT_MACSTYLE_UNDERSCORE 4 #define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0 STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2); // returns 1/0 whether the first string interpreted as utf8 is identical to // the second string interpreted as big-endian utf16... useful for strings from next func STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID); // returns the string (which may be big-endian double byte, e.g. for unicode) // and puts the length in bytes in *length. // // some of the values for the IDs are below; for more see the truetype spec: // http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html // http://www.microsoft.com/typography/otspec/name.htm enum { // platformID STBTT_PLATFORM_ID_UNICODE =0, STBTT_PLATFORM_ID_MAC =1, STBTT_PLATFORM_ID_ISO =2, STBTT_PLATFORM_ID_MICROSOFT =3 }; enum { // encodingID for STBTT_PLATFORM_ID_UNICODE STBTT_UNICODE_EID_UNICODE_1_0 =0, STBTT_UNICODE_EID_UNICODE_1_1 =1, STBTT_UNICODE_EID_ISO_10646 =2, STBTT_UNICODE_EID_UNICODE_2_0_BMP=3, STBTT_UNICODE_EID_UNICODE_2_0_FULL=4 }; enum { // encodingID for STBTT_PLATFORM_ID_MICROSOFT STBTT_MS_EID_SYMBOL =0, STBTT_MS_EID_UNICODE_BMP =1, STBTT_MS_EID_SHIFTJIS =2, STBTT_MS_EID_UNICODE_FULL =10 }; enum { // encodingID for STBTT_PLATFORM_ID_MAC; same as Script Manager codes STBTT_MAC_EID_ROMAN =0, STBTT_MAC_EID_ARABIC =4, STBTT_MAC_EID_JAPANESE =1, STBTT_MAC_EID_HEBREW =5, STBTT_MAC_EID_CHINESE_TRAD =2, STBTT_MAC_EID_GREEK =6, STBTT_MAC_EID_KOREAN =3, STBTT_MAC_EID_RUSSIAN =7 }; enum { // languageID for STBTT_PLATFORM_ID_MICROSOFT; same as LCID... // problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs STBTT_MS_LANG_ENGLISH =0x0409, STBTT_MS_LANG_ITALIAN =0x0410, STBTT_MS_LANG_CHINESE =0x0804, STBTT_MS_LANG_JAPANESE =0x0411, STBTT_MS_LANG_DUTCH =0x0413, STBTT_MS_LANG_KOREAN =0x0412, STBTT_MS_LANG_FRENCH =0x040c, STBTT_MS_LANG_RUSSIAN =0x0419, STBTT_MS_LANG_GERMAN =0x0407, STBTT_MS_LANG_SPANISH =0x0409, STBTT_MS_LANG_HEBREW =0x040d, STBTT_MS_LANG_SWEDISH =0x041D }; enum { // languageID for STBTT_PLATFORM_ID_MAC STBTT_MAC_LANG_ENGLISH =0 , STBTT_MAC_LANG_JAPANESE =11, STBTT_MAC_LANG_ARABIC =12, STBTT_MAC_LANG_KOREAN =23, STBTT_MAC_LANG_DUTCH =4 , STBTT_MAC_LANG_RUSSIAN =32, STBTT_MAC_LANG_FRENCH =1 , STBTT_MAC_LANG_SPANISH =6 , STBTT_MAC_LANG_GERMAN =2 , STBTT_MAC_LANG_SWEDISH =5 , STBTT_MAC_LANG_HEBREW =10, STBTT_MAC_LANG_CHINESE_SIMPLIFIED =33, STBTT_MAC_LANG_ITALIAN =3 , STBTT_MAC_LANG_CHINESE_TRAD =19 }; #ifdef __cplusplus } #endif #endif // __STB_INCLUDE_STB_TRUETYPE_H__ /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// //// //// IMPLEMENTATION //// //// #ifdef STB_TRUETYPE_IMPLEMENTATION #ifndef STBTT_MAX_OVERSAMPLE #define STBTT_MAX_OVERSAMPLE 8 #endif #if STBTT_MAX_OVERSAMPLE > 255 #error "STBTT_MAX_OVERSAMPLE cannot be > 255" #endif typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1]; #ifndef STBTT_RASTERIZER_VERSION #define STBTT_RASTERIZER_VERSION 2 #endif #ifdef _MSC_VER #define STBTT__NOTUSED(v) (void)(v) #else #define STBTT__NOTUSED(v) (void)sizeof(v) #endif ////////////////////////////////////////////////////////////////////////// // // stbtt__buf helpers to parse data from file // static stbtt_uint8 stbtt__buf_get8(stbtt__buf *b) { if (b->cursor >= b->size) return 0; return b->data[b->cursor++]; } static stbtt_uint8 stbtt__buf_peek8(stbtt__buf *b) { if (b->cursor >= b->size) return 0; return b->data[b->cursor]; } static void stbtt__buf_seek(stbtt__buf *b, int o) { STBTT_assert(!(o > b->size || o < 0)); b->cursor = (o > b->size || o < 0) ? b->size : o; } static void stbtt__buf_skip(stbtt__buf *b, int o) { stbtt__buf_seek(b, b->cursor + o); } static stbtt_uint32 stbtt__buf_get(stbtt__buf *b, int n) { stbtt_uint32 v = 0; int i; STBTT_assert(n >= 1 && n <= 4); for (i = 0; i < n; i++) v = (v << 8) | stbtt__buf_get8(b); return v; } static stbtt__buf stbtt__new_buf(const void *p, size_t size) { stbtt__buf r; STBTT_assert(size < 0x40000000); r.data = (stbtt_uint8*) p; r.size = (int) size; r.cursor = 0; return r; } #define stbtt__buf_get16(b) stbtt__buf_get((b), 2) #define stbtt__buf_get32(b) stbtt__buf_get((b), 4) static stbtt__buf stbtt__buf_range(const stbtt__buf *b, int o, int s) { stbtt__buf r = stbtt__new_buf(NULL, 0); if (o < 0 || s < 0 || o > b->size || s > b->size - o) return r; r.data = b->data + o; r.size = s; return r; } static stbtt__buf stbtt__cff_get_index(stbtt__buf *b) { int count, start, offsize; start = b->cursor; count = stbtt__buf_get16(b); if (count) { offsize = stbtt__buf_get8(b); STBTT_assert(offsize >= 1 && offsize <= 4); stbtt__buf_skip(b, offsize * count); stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1); } return stbtt__buf_range(b, start, b->cursor - start); } static stbtt_uint32 stbtt__cff_int(stbtt__buf *b) { int b0 = stbtt__buf_get8(b); if (b0 >= 32 && b0 <= 246) return b0 - 139; else if (b0 >= 247 && b0 <= 250) return (b0 - 247)*256 + stbtt__buf_get8(b) + 108; else if (b0 >= 251 && b0 <= 254) return -(b0 - 251)*256 - stbtt__buf_get8(b) - 108; else if (b0 == 28) return stbtt__buf_get16(b); else if (b0 == 29) return stbtt__buf_get32(b); STBTT_assert(0); return 0; } static void stbtt__cff_skip_operand(stbtt__buf *b) { int v, b0 = stbtt__buf_peek8(b); STBTT_assert(b0 >= 28); if (b0 == 30) { stbtt__buf_skip(b, 1); while (b->cursor < b->size) { v = stbtt__buf_get8(b); if ((v & 0xF) == 0xF || (v >> 4) == 0xF) break; } } else { stbtt__cff_int(b); } } static stbtt__buf stbtt__dict_get(stbtt__buf *b, int key) { stbtt__buf_seek(b, 0); while (b->cursor < b->size) { int start = b->cursor, end, op; while (stbtt__buf_peek8(b) >= 28) stbtt__cff_skip_operand(b); end = b->cursor; op = stbtt__buf_get8(b); if (op == 12) op = stbtt__buf_get8(b) | 0x100; if (op == key) return stbtt__buf_range(b, start, end-start); } return stbtt__buf_range(b, 0, 0); } static void stbtt__dict_get_ints(stbtt__buf *b, int key, int outcount, stbtt_uint32 *out) { int i; stbtt__buf operands = stbtt__dict_get(b, key); for (i = 0; i < outcount && operands.cursor < operands.size; i++) out[i] = stbtt__cff_int(&operands); } static int stbtt__cff_index_count(stbtt__buf *b) { stbtt__buf_seek(b, 0); return stbtt__buf_get16(b); } static stbtt__buf stbtt__cff_index_get(stbtt__buf b, int i) { int count, offsize, start, end; stbtt__buf_seek(&b, 0); count = stbtt__buf_get16(&b); offsize = stbtt__buf_get8(&b); STBTT_assert(i >= 0 && i < count); STBTT_assert(offsize >= 1 && offsize <= 4); stbtt__buf_skip(&b, i*offsize); start = stbtt__buf_get(&b, offsize); end = stbtt__buf_get(&b, offsize); return stbtt__buf_range(&b, 2+(count+1)*offsize+start, end - start); } ////////////////////////////////////////////////////////////////////////// // // accessors to parse data from file // // on platforms that don't allow misaligned reads, if we want to allow // truetype fonts that aren't padded to alignment, define ALLOW_UNALIGNED_TRUETYPE #define ttBYTE(p) (* (stbtt_uint8 *) (p)) #define ttCHAR(p) (* (stbtt_int8 *) (p)) #define ttFixed(p) ttLONG(p) static stbtt_uint16 ttUSHORT(stbtt_uint8 *p) { return p[0]*256 + p[1]; } static stbtt_int16 ttSHORT(stbtt_uint8 *p) { return p[0]*256 + p[1]; } static stbtt_uint32 ttULONG(stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; } static stbtt_int32 ttLONG(stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; } #define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3)) #define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3]) static int stbtt__isfont(stbtt_uint8 *font) { // check the version number if (stbtt_tag4(font, '1',0,0,0)) return 1; // TrueType 1 if (stbtt_tag(font, "typ1")) return 1; // TrueType with type 1 font -- we don't support this! if (stbtt_tag(font, "OTTO")) return 1; // OpenType with CFF if (stbtt_tag4(font, 0,1,0,0)) return 1; // OpenType 1.0 if (stbtt_tag(font, "true")) return 1; // Apple specification for TrueType fonts return 0; } // @OPTIMIZE: binary search static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart, const char *tag) { stbtt_int32 num_tables = ttUSHORT(data+fontstart+4); stbtt_uint32 tabledir = fontstart + 12; stbtt_int32 i; for (i=0; i < num_tables; ++i) { stbtt_uint32 loc = tabledir + 16*i; if (stbtt_tag(data+loc+0, tag)) return ttULONG(data+loc+8); } return 0; } static int stbtt_GetFontOffsetForIndex_internal(unsigned char *font_collection, int index) { // if it's just a font, there's only one valid index if (stbtt__isfont(font_collection)) return index == 0 ? 0 : -1; // check if it's a TTC if (stbtt_tag(font_collection, "ttcf")) { // version 1? if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) { stbtt_int32 n = ttLONG(font_collection+8); if (index >= n) return -1; return ttULONG(font_collection+12+index*4); } } return -1; } static int stbtt_GetNumberOfFonts_internal(unsigned char *font_collection) { // if it's just a font, there's only one valid font if (stbtt__isfont(font_collection)) return 1; // check if it's a TTC if (stbtt_tag(font_collection, "ttcf")) { // version 1? if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) { return ttLONG(font_collection+8); } } return 0; } static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict) { stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 }; stbtt__buf pdict; stbtt__dict_get_ints(&fontdict, 18, 2, private_loc); if (!private_loc[1] || !private_loc[0]) return stbtt__new_buf(NULL, 0); pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]); stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff); if (!subrsoff) return stbtt__new_buf(NULL, 0); stbtt__buf_seek(&cff, private_loc[1]+subrsoff); return stbtt__cff_get_index(&cff); } static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart) { stbtt_uint32 cmap, t; stbtt_int32 i,numTables; info->data = data; info->fontstart = fontstart; info->cff = stbtt__new_buf(NULL, 0); cmap = stbtt__find_table(data, fontstart, "cmap"); // required info->loca = stbtt__find_table(data, fontstart, "loca"); // required info->head = stbtt__find_table(data, fontstart, "head"); // required info->glyf = stbtt__find_table(data, fontstart, "glyf"); // required info->hhea = stbtt__find_table(data, fontstart, "hhea"); // required info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); // required info->kern = stbtt__find_table(data, fontstart, "kern"); // not required info->gpos = stbtt__find_table(data, fontstart, "GPOS"); // not required if (!cmap || !info->head || !info->hhea || !info->hmtx) return 0; if (info->glyf) { // required for truetype if (!info->loca) return 0; } else { // initialization for CFF / Type2 fonts (OTF) stbtt__buf b, topdict, topdictidx; stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0; stbtt_uint32 cff; cff = stbtt__find_table(data, fontstart, "CFF "); if (!cff) return 0; info->fontdicts = stbtt__new_buf(NULL, 0); info->fdselect = stbtt__new_buf(NULL, 0); // @TODO this should use size from table (not 512MB) info->cff = stbtt__new_buf(data+cff, 512*1024*1024); b = info->cff; // read the header stbtt__buf_skip(&b, 2); stbtt__buf_seek(&b, stbtt__buf_get8(&b)); // hdrsize // @TODO the name INDEX could list multiple fonts, // but we just use the first one. stbtt__cff_get_index(&b); // name INDEX topdictidx = stbtt__cff_get_index(&b); topdict = stbtt__cff_index_get(topdictidx, 0); stbtt__cff_get_index(&b); // string INDEX info->gsubrs = stbtt__cff_get_index(&b); stbtt__dict_get_ints(&topdict, 17, 1, &charstrings); stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype); stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff); stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff); info->subrs = stbtt__get_subrs(b, topdict); // we only support Type 2 charstrings if (cstype != 2) return 0; if (charstrings == 0) return 0; if (fdarrayoff) { // looks like a CID font if (!fdselectoff) return 0; stbtt__buf_seek(&b, fdarrayoff); info->fontdicts = stbtt__cff_get_index(&b); info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size-fdselectoff); } stbtt__buf_seek(&b, charstrings); info->charstrings = stbtt__cff_get_index(&b); } t = stbtt__find_table(data, fontstart, "maxp"); if (t) info->numGlyphs = ttUSHORT(data+t+4); else info->numGlyphs = 0xffff; // find a cmap encoding table we understand *now* to avoid searching // later. (todo: could make this installable) // the same regardless of glyph. numTables = ttUSHORT(data + cmap + 2); info->index_map = 0; for (i=0; i < numTables; ++i) { stbtt_uint32 encoding_record = cmap + 4 + 8 * i; // find an encoding we understand: switch(ttUSHORT(data+encoding_record)) { case STBTT_PLATFORM_ID_MICROSOFT: switch (ttUSHORT(data+encoding_record+2)) { case STBTT_MS_EID_UNICODE_BMP: case STBTT_MS_EID_UNICODE_FULL: // MS/Unicode info->index_map = cmap + ttULONG(data+encoding_record+4); break; } break; case STBTT_PLATFORM_ID_UNICODE: // Mac/iOS has these // all the encodingIDs are unicode, so we don't bother to check it info->index_map = cmap + ttULONG(data+encoding_record+4); break; } } if (info->index_map == 0) return 0; info->indexToLocFormat = ttUSHORT(data+info->head + 50); return 1; } STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint) { stbtt_uint8 *data = info->data; stbtt_uint32 index_map = info->index_map; stbtt_uint16 format = ttUSHORT(data + index_map + 0); if (format == 0) { // apple byte encoding stbtt_int32 bytes = ttUSHORT(data + index_map + 2); if (unicode_codepoint < bytes-6) return ttBYTE(data + index_map + 6 + unicode_codepoint); return 0; } else if (format == 6) { stbtt_uint32 first = ttUSHORT(data + index_map + 6); stbtt_uint32 count = ttUSHORT(data + index_map + 8); if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count) return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2); return 0; } else if (format == 2) { STBTT_assert(0); // @TODO: high-byte mapping for japanese/chinese/korean return 0; } else if (format == 4) { // standard mapping for windows fonts: binary search collection of ranges stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1; stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1; stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10); stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1; // do a binary search of the segments stbtt_uint32 endCount = index_map + 14; stbtt_uint32 search = endCount; if (unicode_codepoint > 0xffff) return 0; // they lie from endCount .. endCount + segCount // but searchRange is the nearest power of two, so... if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2)) search += rangeShift*2; // now decrement to bias correctly to find smallest search -= 2; while (entrySelector) { stbtt_uint16 end; searchRange >>= 1; end = ttUSHORT(data + search + searchRange*2); if (unicode_codepoint > end) search += searchRange*2; --entrySelector; } search += 2; { stbtt_uint16 offset, start; stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1); STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item)); start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item); if (unicode_codepoint < start) return 0; offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item); if (offset == 0) return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item)); return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item); } } else if (format == 12 || format == 13) { stbtt_uint32 ngroups = ttULONG(data+index_map+12); stbtt_int32 low,high; low = 0; high = (stbtt_int32)ngroups; // Binary search the right group. while (low < high) { stbtt_int32 mid = low + ((high-low) >> 1); // rounds down, so low <= mid < high stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12); stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4); if ((stbtt_uint32) unicode_codepoint < start_char) high = mid; else if ((stbtt_uint32) unicode_codepoint > end_char) low = mid+1; else { stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8); if (format == 12) return start_glyph + unicode_codepoint-start_char; else // format == 13 return start_glyph; } } return 0; // not found } // @TODO STBTT_assert(0); return 0; } STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices) { return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices); } static void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy) { v->type = type; v->x = (stbtt_int16) x; v->y = (stbtt_int16) y; v->cx = (stbtt_int16) cx; v->cy = (stbtt_int16) cy; } static int stbtt__GetGlyfOffset(const stbtt_fontinfo *info, int glyph_index) { int g1,g2; STBTT_assert(!info->cff.size); if (glyph_index >= info->numGlyphs) return -1; // glyph index out of range if (info->indexToLocFormat >= 2) return -1; // unknown index->glyph map format if (info->indexToLocFormat == 0) { g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2; g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2; } else { g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4); g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4); } return g1==g2 ? -1 : g1; // if length is 0, return -1 } static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1); STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1) { if (info->cff.size) { stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1); } else { int g = stbtt__GetGlyfOffset(info, glyph_index); if (g < 0) return 0; if (x0) *x0 = ttSHORT(info->data + g + 2); if (y0) *y0 = ttSHORT(info->data + g + 4); if (x1) *x1 = ttSHORT(info->data + g + 6); if (y1) *y1 = ttSHORT(info->data + g + 8); } return 1; } STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1) { return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1); } STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index) { stbtt_int16 numberOfContours; int g; if (info->cff.size) return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0; g = stbtt__GetGlyfOffset(info, glyph_index); if (g < 0) return 1; numberOfContours = ttSHORT(info->data + g); return numberOfContours == 0; } static int stbtt__close_shape(stbtt_vertex *vertices, int num_vertices, int was_off, int start_off, stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy) { if (start_off) { if (was_off) stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy); stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx,sy,scx,scy); } else { if (was_off) stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve,sx,sy,cx,cy); else stbtt_setvertex(&vertices[num_vertices++], STBTT_vline,sx,sy,0,0); } return num_vertices; } static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices) { stbtt_int16 numberOfContours; stbtt_uint8 *endPtsOfContours; stbtt_uint8 *data = info->data; stbtt_vertex *vertices=0; int num_vertices=0; int g = stbtt__GetGlyfOffset(info, glyph_index); *pvertices = NULL; if (g < 0) return 0; numberOfContours = ttSHORT(data + g); if (numberOfContours > 0) { stbtt_uint8 flags=0,flagcount; stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0; stbtt_int32 x,y,cx,cy,sx,sy, scx,scy; stbtt_uint8 *points; endPtsOfContours = (data + g + 10); ins = ttUSHORT(data + g + 10 + numberOfContours * 2); points = data + g + 10 + numberOfContours * 2 + 2 + ins; n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2); m = n + 2*numberOfContours; // a loose bound on how many vertices we might need vertices = (stbtt_vertex *) STBTT_malloc(m * sizeof(vertices[0]), info->userdata); if (vertices == 0) return 0; next_move = 0; flagcount=0; // in first pass, we load uninterpreted data into the allocated array // above, shifted to the end of the array so we won't overwrite it when // we create our final data starting from the front off = m - n; // starting offset for uninterpreted data, regardless of how m ends up being calculated // first load flags for (i=0; i < n; ++i) { if (flagcount == 0) { flags = *points++; if (flags & 8) flagcount = *points++; } else --flagcount; vertices[off+i].type = flags; } // now load x coordinates x=0; for (i=0; i < n; ++i) { flags = vertices[off+i].type; if (flags & 2) { stbtt_int16 dx = *points++; x += (flags & 16) ? dx : -dx; // ??? } else { if (!(flags & 16)) { x = x + (stbtt_int16) (points[0]*256 + points[1]); points += 2; } } vertices[off+i].x = (stbtt_int16) x; } // now load y coordinates y=0; for (i=0; i < n; ++i) { flags = vertices[off+i].type; if (flags & 4) { stbtt_int16 dy = *points++; y += (flags & 32) ? dy : -dy; // ??? } else { if (!(flags & 32)) { y = y + (stbtt_int16) (points[0]*256 + points[1]); points += 2; } } vertices[off+i].y = (stbtt_int16) y; } // now convert them to our format num_vertices=0; sx = sy = cx = cy = scx = scy = 0; for (i=0; i < n; ++i) { flags = vertices[off+i].type; x = (stbtt_int16) vertices[off+i].x; y = (stbtt_int16) vertices[off+i].y; if (next_move == i) { if (i != 0) num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy); // now start the new one start_off = !(flags & 1); if (start_off) { // if we start off with an off-curve point, then when we need to find a point on the curve // where we can start, and we need to save some state for when we wraparound. scx = x; scy = y; if (!(vertices[off+i+1].type & 1)) { // next point is also a curve point, so interpolate an on-point curve sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1; sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1; } else { // otherwise just use the next point as our start point sx = (stbtt_int32) vertices[off+i+1].x; sy = (stbtt_int32) vertices[off+i+1].y; ++i; // we're using point i+1 as the starting point, so skip it } } else { sx = x; sy = y; } stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove,sx,sy,0,0); was_off = 0; next_move = 1 + ttUSHORT(endPtsOfContours+j*2); ++j; } else { if (!(flags & 1)) { // if it's a curve if (was_off) // two off-curve control points in a row means interpolate an on-curve midpoint stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy); cx = x; cy = y; was_off = 1; } else { if (was_off) stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x,y, cx, cy); else stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x,y,0,0); was_off = 0; } } } num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy); } else if (numberOfContours == -1) { // Compound shapes. int more = 1; stbtt_uint8 *comp = data + g + 10; num_vertices = 0; vertices = 0; while (more) { stbtt_uint16 flags, gidx; int comp_num_verts = 0, i; stbtt_vertex *comp_verts = 0, *tmp = 0; float mtx[6] = {1,0,0,1,0,0}, m, n; flags = ttSHORT(comp); comp+=2; gidx = ttSHORT(comp); comp+=2; if (flags & 2) { // XY values if (flags & 1) { // shorts mtx[4] = ttSHORT(comp); comp+=2; mtx[5] = ttSHORT(comp); comp+=2; } else { mtx[4] = ttCHAR(comp); comp+=1; mtx[5] = ttCHAR(comp); comp+=1; } } else { // @TODO handle matching point STBTT_assert(0); } if (flags & (1<<3)) { // WE_HAVE_A_SCALE mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; mtx[1] = mtx[2] = 0; } else if (flags & (1<<6)) { // WE_HAVE_AN_X_AND_YSCALE mtx[0] = ttSHORT(comp)/16384.0f; comp+=2; mtx[1] = mtx[2] = 0; mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; } else if (flags & (1<<7)) { // WE_HAVE_A_TWO_BY_TWO mtx[0] = ttSHORT(comp)/16384.0f; comp+=2; mtx[1] = ttSHORT(comp)/16384.0f; comp+=2; mtx[2] = ttSHORT(comp)/16384.0f; comp+=2; mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; } // Find transformation scales. m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]); n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]); // Get indexed glyph. comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts); if (comp_num_verts > 0) { // Transform vertices. for (i = 0; i < comp_num_verts; ++i) { stbtt_vertex* v = &comp_verts[i]; stbtt_vertex_type x,y; x=v->x; y=v->y; v->x = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4])); v->y = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5])); x=v->cx; y=v->cy; v->cx = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4])); v->cy = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5])); } // Append vertices. tmp = (stbtt_vertex*)STBTT_malloc((num_vertices+comp_num_verts)*sizeof(stbtt_vertex), info->userdata); if (!tmp) { if (vertices) STBTT_free(vertices, info->userdata); if (comp_verts) STBTT_free(comp_verts, info->userdata); return 0; } if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex)); //-V595 STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex)); if (vertices) STBTT_free(vertices, info->userdata); vertices = tmp; STBTT_free(comp_verts, info->userdata); num_vertices += comp_num_verts; } // More components ? more = flags & (1<<5); } } else if (numberOfContours < 0) { // @TODO other compound variations? STBTT_assert(0); } else { // numberOfCounters == 0, do nothing } *pvertices = vertices; return num_vertices; } typedef struct { int bounds; int started; float first_x, first_y; float x, y; stbtt_int32 min_x, max_x, min_y, max_y; stbtt_vertex *pvertices; int num_vertices; } stbtt__csctx; #define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0} static void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y) { if (x > c->max_x || !c->started) c->max_x = x; if (y > c->max_y || !c->started) c->max_y = y; if (x < c->min_x || !c->started) c->min_x = x; if (y < c->min_y || !c->started) c->min_y = y; c->started = 1; } static 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) { if (c->bounds) { stbtt__track_vertex(c, x, y); if (type == STBTT_vcubic) { stbtt__track_vertex(c, cx, cy); stbtt__track_vertex(c, cx1, cy1); } } else { stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy); c->pvertices[c->num_vertices].cx1 = (stbtt_int16) cx1; c->pvertices[c->num_vertices].cy1 = (stbtt_int16) cy1; } c->num_vertices++; } static void stbtt__csctx_close_shape(stbtt__csctx *ctx) { if (ctx->first_x != ctx->x || ctx->first_y != ctx->y) stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->first_x, (int)ctx->first_y, 0, 0, 0, 0); } static void stbtt__csctx_rmove_to(stbtt__csctx *ctx, float dx, float dy) { stbtt__csctx_close_shape(ctx); ctx->first_x = ctx->x = ctx->x + dx; ctx->first_y = ctx->y = ctx->y + dy; stbtt__csctx_v(ctx, STBTT_vmove, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0); } static void stbtt__csctx_rline_to(stbtt__csctx *ctx, float dx, float dy) { ctx->x += dx; ctx->y += dy; stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0); } static void stbtt__csctx_rccurve_to(stbtt__csctx *ctx, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3) { float cx1 = ctx->x + dx1; float cy1 = ctx->y + dy1; float cx2 = cx1 + dx2; float cy2 = cy1 + dy2; ctx->x = cx2 + dx3; ctx->y = cy2 + dy3; stbtt__csctx_v(ctx, STBTT_vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2); } static stbtt__buf stbtt__get_subr(stbtt__buf idx, int n) { int count = stbtt__cff_index_count(&idx); int bias = 107; if (count >= 33900) bias = 32768; else if (count >= 1240) bias = 1131; n += bias; if (n < 0 || n >= count) return stbtt__new_buf(NULL, 0); return stbtt__cff_index_get(idx, n); } static stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo *info, int glyph_index) { stbtt__buf fdselect = info->fdselect; int nranges, start, end, v, fmt, fdselector = -1, i; stbtt__buf_seek(&fdselect, 0); fmt = stbtt__buf_get8(&fdselect); if (fmt == 0) { // untested stbtt__buf_skip(&fdselect, glyph_index); fdselector = stbtt__buf_get8(&fdselect); } else if (fmt == 3) { nranges = stbtt__buf_get16(&fdselect); start = stbtt__buf_get16(&fdselect); for (i = 0; i < nranges; i++) { v = stbtt__buf_get8(&fdselect); end = stbtt__buf_get16(&fdselect); if (glyph_index >= start && glyph_index < end) { fdselector = v; break; } start = end; } } if (fdselector == -1) stbtt__new_buf(NULL, 0); return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector)); } static int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, stbtt__csctx *c) { int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0; int has_subrs = 0, clear_stack; float s[48]; stbtt__buf subr_stack[10], subrs = info->subrs, b; float f; #define STBTT__CSERR(s) (0) // this currently ignores the initial width value, which isn't needed if we have hmtx b = stbtt__cff_index_get(info->charstrings, glyph_index); while (b.cursor < b.size) { i = 0; clear_stack = 1; b0 = stbtt__buf_get8(&b); switch (b0) { // @TODO implement hinting case 0x13: // hintmask case 0x14: // cntrmask if (in_header) maskbits += (sp / 2); // implicit "vstem" in_header = 0; stbtt__buf_skip(&b, (maskbits + 7) / 8); break; case 0x01: // hstem case 0x03: // vstem case 0x12: // hstemhm case 0x17: // vstemhm maskbits += (sp / 2); break; case 0x15: // rmoveto in_header = 0; if (sp < 2) return STBTT__CSERR("rmoveto stack"); stbtt__csctx_rmove_to(c, s[sp-2], s[sp-1]); break; case 0x04: // vmoveto in_header = 0; if (sp < 1) return STBTT__CSERR("vmoveto stack"); stbtt__csctx_rmove_to(c, 0, s[sp-1]); break; case 0x16: // hmoveto in_header = 0; if (sp < 1) return STBTT__CSERR("hmoveto stack"); stbtt__csctx_rmove_to(c, s[sp-1], 0); break; case 0x05: // rlineto if (sp < 2) return STBTT__CSERR("rlineto stack"); for (; i + 1 < sp; i += 2) stbtt__csctx_rline_to(c, s[i], s[i+1]); break; // hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical // starting from a different place. case 0x07: // vlineto if (sp < 1) return STBTT__CSERR("vlineto stack"); goto vlineto; case 0x06: // hlineto if (sp < 1) return STBTT__CSERR("hlineto stack"); for (;;) { if (i >= sp) break; stbtt__csctx_rline_to(c, s[i], 0); i++; vlineto: if (i >= sp) break; stbtt__csctx_rline_to(c, 0, s[i]); i++; } break; case 0x1F: // hvcurveto if (sp < 4) return STBTT__CSERR("hvcurveto stack"); goto hvcurveto; case 0x1E: // vhcurveto if (sp < 4) return STBTT__CSERR("vhcurveto stack"); for (;;) { if (i + 3 >= sp) break; 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); i += 4; hvcurveto: if (i + 3 >= sp) break; 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]); i += 4; } break; case 0x08: // rrcurveto if (sp < 6) return STBTT__CSERR("rcurveline stack"); for (; i + 5 < sp; i += 6) stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]); break; case 0x18: // rcurveline if (sp < 8) return STBTT__CSERR("rcurveline stack"); for (; i + 5 < sp - 2; i += 6) stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]); if (i + 1 >= sp) return STBTT__CSERR("rcurveline stack"); stbtt__csctx_rline_to(c, s[i], s[i+1]); break; case 0x19: // rlinecurve if (sp < 8) return STBTT__CSERR("rlinecurve stack"); for (; i + 1 < sp - 6; i += 2) stbtt__csctx_rline_to(c, s[i], s[i+1]); if (i + 5 >= sp) return STBTT__CSERR("rlinecurve stack"); stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]); break; case 0x1A: // vvcurveto case 0x1B: // hhcurveto if (sp < 4) return STBTT__CSERR("(vv|hh)curveto stack"); f = 0.0; if (sp & 1) { f = s[i]; i++; } for (; i + 3 < sp; i += 4) { if (b0 == 0x1B) stbtt__csctx_rccurve_to(c, s[i], f, s[i+1], s[i+2], s[i+3], 0.0); else stbtt__csctx_rccurve_to(c, f, s[i], s[i+1], s[i+2], 0.0, s[i+3]); f = 0.0; } break; case 0x0A: // callsubr if (!has_subrs) { if (info->fdselect.size) subrs = stbtt__cid_get_glyph_subrs(info, glyph_index); has_subrs = 1; } // fallthrough case 0x1D: // callgsubr if (sp < 1) return STBTT__CSERR("call(g|)subr stack"); v = (int) s[--sp]; if (subr_stack_height >= 10) return STBTT__CSERR("recursion limit"); subr_stack[subr_stack_height++] = b; b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v); if (b.size == 0) return STBTT__CSERR("subr not found"); b.cursor = 0; clear_stack = 0; break; case 0x0B: // return if (subr_stack_height <= 0) return STBTT__CSERR("return outside subr"); b = subr_stack[--subr_stack_height]; clear_stack = 0; break; case 0x0E: // endchar stbtt__csctx_close_shape(c); return 1; case 0x0C: { // two-byte escape float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6; float dx, dy; int b1 = stbtt__buf_get8(&b); switch (b1) { // @TODO These "flex" implementations ignore the flex-depth and resolution, // and always draw beziers. case 0x22: // hflex if (sp < 7) return STBTT__CSERR("hflex stack"); dx1 = s[0]; dx2 = s[1]; dy2 = s[2]; dx3 = s[3]; dx4 = s[4]; dx5 = s[5]; dx6 = s[6]; stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0); stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0); break; case 0x23: // flex if (sp < 13) return STBTT__CSERR("flex stack"); dx1 = s[0]; dy1 = s[1]; dx2 = s[2]; dy2 = s[3]; dx3 = s[4]; dy3 = s[5]; dx4 = s[6]; dy4 = s[7]; dx5 = s[8]; dy5 = s[9]; dx6 = s[10]; dy6 = s[11]; //fd is s[12] stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3); stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6); break; case 0x24: // hflex1 if (sp < 9) return STBTT__CSERR("hflex1 stack"); dx1 = s[0]; dy1 = s[1]; dx2 = s[2]; dy2 = s[3]; dx3 = s[4]; dx4 = s[5]; dx5 = s[6]; dy5 = s[7]; dx6 = s[8]; stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0); stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1+dy2+dy5)); break; case 0x25: // flex1 if (sp < 11) return STBTT__CSERR("flex1 stack"); dx1 = s[0]; dy1 = s[1]; dx2 = s[2]; dy2 = s[3]; dx3 = s[4]; dy3 = s[5]; dx4 = s[6]; dy4 = s[7]; dx5 = s[8]; dy5 = s[9]; dx6 = dy6 = s[10]; dx = dx1+dx2+dx3+dx4+dx5; dy = dy1+dy2+dy3+dy4+dy5; if (STBTT_fabs(dx) > STBTT_fabs(dy)) dy6 = -dy; else dx6 = -dx; stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3); stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6); break; default: return STBTT__CSERR("unimplemented"); } } break; default: if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254)) //-V560 return STBTT__CSERR("reserved operator"); // push immediate if (b0 == 255) { f = (float)(stbtt_int32)stbtt__buf_get32(&b) / 0x10000; } else { stbtt__buf_skip(&b, -1); f = (float)(stbtt_int16)stbtt__cff_int(&b); } if (sp >= 48) return STBTT__CSERR("push stack overflow"); s[sp++] = f; clear_stack = 0; break; } if (clear_stack) sp = 0; } return STBTT__CSERR("no endchar"); #undef STBTT__CSERR } static int stbtt__GetGlyphShapeT2(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices) { // runs the charstring twice, once to count and once to output (to avoid realloc) stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1); stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0); if (stbtt__run_charstring(info, glyph_index, &count_ctx)) { *pvertices = (stbtt_vertex*)STBTT_malloc(count_ctx.num_vertices*sizeof(stbtt_vertex), info->userdata); output_ctx.pvertices = *pvertices; if (stbtt__run_charstring(info, glyph_index, &output_ctx)) { STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices); return output_ctx.num_vertices; } } *pvertices = NULL; return 0; } static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1) { stbtt__csctx c = STBTT__CSCTX_INIT(1); int r = stbtt__run_charstring(info, glyph_index, &c); if (x0) *x0 = r ? c.min_x : 0; if (y0) *y0 = r ? c.min_y : 0; if (x1) *x1 = r ? c.max_x : 0; if (y1) *y1 = r ? c.max_y : 0; return r ? c.num_vertices : 0; } STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices) { if (!info->cff.size) return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices); else return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices); } STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing) { stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34); if (glyph_index < numOfLongHorMetrics) { if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*glyph_index); if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2); } else { if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1)); if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics)); } } static int stbtt__GetGlyphKernInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2) { stbtt_uint8 *data = info->data + info->kern; stbtt_uint32 needle, straw; int l, r, m; // we only look at the first table. it must be 'horizontal' and format 0. if (!info->kern) return 0; if (ttUSHORT(data+2) < 1) // number of tables, need at least 1 return 0; if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format return 0; l = 0; r = ttUSHORT(data+10) - 1; needle = glyph1 << 16 | glyph2; while (l <= r) { m = (l + r) >> 1; straw = ttULONG(data+18+(m*6)); // note: unaligned read if (needle < straw) r = m - 1; else if (needle > straw) l = m + 1; else return ttSHORT(data+22+(m*6)); } return 0; } static stbtt_int32 stbtt__GetCoverageIndex(stbtt_uint8 *coverageTable, int glyph) { stbtt_uint16 coverageFormat = ttUSHORT(coverageTable); switch(coverageFormat) { case 1: { stbtt_uint16 glyphCount = ttUSHORT(coverageTable + 2); // Binary search. stbtt_int32 l=0, r=glyphCount-1, m; int straw, needle=glyph; while (l <= r) { stbtt_uint8 *glyphArray = coverageTable + 4; stbtt_uint16 glyphID; m = (l + r) >> 1; glyphID = ttUSHORT(glyphArray + 2 * m); straw = glyphID; if (needle < straw) r = m - 1; else if (needle > straw) l = m + 1; else { return m; } } } break; case 2: { stbtt_uint16 rangeCount = ttUSHORT(coverageTable + 2); stbtt_uint8 *rangeArray = coverageTable + 4; // Binary search. stbtt_int32 l=0, r=rangeCount-1, m; int strawStart, strawEnd, needle=glyph; while (l <= r) { stbtt_uint8 *rangeRecord; m = (l + r) >> 1; rangeRecord = rangeArray + 6 * m; strawStart = ttUSHORT(rangeRecord); strawEnd = ttUSHORT(rangeRecord + 2); if (needle < strawStart) r = m - 1; else if (needle > strawEnd) l = m + 1; else { stbtt_uint16 startCoverageIndex = ttUSHORT(rangeRecord + 4); return startCoverageIndex + glyph - strawStart; } } } break; default: { // There are no other cases. STBTT_assert(0); } break; } return -1; } static stbtt_int32 stbtt__GetGlyphClass(stbtt_uint8 *classDefTable, int glyph) { stbtt_uint16 classDefFormat = ttUSHORT(classDefTable); switch(classDefFormat) { case 1: { stbtt_uint16 startGlyphID = ttUSHORT(classDefTable + 2); stbtt_uint16 glyphCount = ttUSHORT(classDefTable + 4); stbtt_uint8 *classDef1ValueArray = classDefTable + 6; if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount) return (stbtt_int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID)); // [DEAR IMGUI] Commented to fix static analyzer warning //classDefTable = classDef1ValueArray + 2 * glyphCount; } break; case 2: { stbtt_uint16 classRangeCount = ttUSHORT(classDefTable + 2); stbtt_uint8 *classRangeRecords = classDefTable + 4; // Binary search. stbtt_int32 l=0, r=classRangeCount-1, m; int strawStart, strawEnd, needle=glyph; while (l <= r) { stbtt_uint8 *classRangeRecord; m = (l + r) >> 1; classRangeRecord = classRangeRecords + 6 * m; strawStart = ttUSHORT(classRangeRecord); strawEnd = ttUSHORT(classRangeRecord + 2); if (needle < strawStart) r = m - 1; else if (needle > strawEnd) l = m + 1; else return (stbtt_int32)ttUSHORT(classRangeRecord + 4); } // [DEAR IMGUI] Commented to fix static analyzer warning //classDefTable = classRangeRecords + 6 * classRangeCount; } break; default: { // There are no other cases. STBTT_assert(0); } break; } return -1; } // Define to STBTT_assert(x) if you want to break on unimplemented formats. #define STBTT_GPOS_TODO_assert(x) static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2) { stbtt_uint16 lookupListOffset; stbtt_uint8 *lookupList; stbtt_uint16 lookupCount; stbtt_uint8 *data; stbtt_int32 i; if (!info->gpos) return 0; data = info->data + info->gpos; if (ttUSHORT(data+0) != 1) return 0; // Major version 1 if (ttUSHORT(data+2) != 0) return 0; // Minor version 0 lookupListOffset = ttUSHORT(data+8); lookupList = data + lookupListOffset; lookupCount = ttUSHORT(lookupList); for (i=0; i> 1; pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m; secondGlyph = ttUSHORT(pairValue); straw = secondGlyph; if (needle < straw) r = m - 1; else if (needle > straw) l = m + 1; else { stbtt_int16 xAdvance = ttSHORT(pairValue + 2); return xAdvance; } } } break; case 2: { stbtt_uint16 valueFormat1 = ttUSHORT(table + 4); stbtt_uint16 valueFormat2 = ttUSHORT(table + 6); stbtt_uint16 classDef1Offset = ttUSHORT(table + 8); stbtt_uint16 classDef2Offset = ttUSHORT(table + 10); int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1); int glyph2class = stbtt__GetGlyphClass(table + classDef2Offset, glyph2); stbtt_uint16 class1Count = ttUSHORT(table + 12); stbtt_uint16 class2Count = ttUSHORT(table + 14); STBTT_assert(glyph1class < class1Count); STBTT_assert(glyph2class < class2Count); // TODO: Support more formats. STBTT_GPOS_TODO_assert(valueFormat1 == 4); if (valueFormat1 != 4) return 0; STBTT_GPOS_TODO_assert(valueFormat2 == 0); if (valueFormat2 != 0) return 0; if (glyph1class >= 0 && glyph1class < class1Count && glyph2class >= 0 && glyph2class < class2Count) { stbtt_uint8 *class1Records = table + 16; stbtt_uint8 *class2Records = class1Records + 2 * (glyph1class * class2Count); stbtt_int16 xAdvance = ttSHORT(class2Records + 2 * glyph2class); return xAdvance; } } break; default: { // There are no other cases. STBTT_assert(0); break; }; } } break; }; default: // TODO: Implement other stuff. break; } } return 0; } STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int g1, int g2) { int xAdvance = 0; if (info->gpos) xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2); if (info->kern) xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2); return xAdvance; } STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2) { if (!info->kern && !info->gpos) // if no kerning table, don't waste time looking up both codepoint->glyphs return 0; return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2)); } STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing) { stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing); } STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap) { if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4); if (descent) *descent = ttSHORT(info->data+info->hhea + 6); if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8); } STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap) { int tab = stbtt__find_table(info->data, info->fontstart, "OS/2"); if (!tab) return 0; if (typoAscent ) *typoAscent = ttSHORT(info->data+tab + 68); if (typoDescent) *typoDescent = ttSHORT(info->data+tab + 70); if (typoLineGap) *typoLineGap = ttSHORT(info->data+tab + 72); return 1; } STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1) { *x0 = ttSHORT(info->data + info->head + 36); *y0 = ttSHORT(info->data + info->head + 38); *x1 = ttSHORT(info->data + info->head + 40); *y1 = ttSHORT(info->data + info->head + 42); } STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height) { int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6); return (float) height / fheight; } STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels) { int unitsPerEm = ttUSHORT(info->data + info->head + 18); return pixels / unitsPerEm; } STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v) { STBTT_free(v, info->userdata); } ////////////////////////////////////////////////////////////////////////////// // // antialiasing software rasterizer // STBTT_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) { int x0=0,y0=0,x1,y1; // =0 suppresses compiler warning if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) { // e.g. space character if (ix0) *ix0 = 0; if (iy0) *iy0 = 0; if (ix1) *ix1 = 0; if (iy1) *iy1 = 0; } else { // move to integral bboxes (treating pixels as little squares, what pixels get touched)? if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x); if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y); if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x); if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y); } } STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1) { stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1); } STBTT_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) { stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1); } STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1) { stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1); } ////////////////////////////////////////////////////////////////////////////// // // Rasterizer typedef struct stbtt__hheap_chunk { struct stbtt__hheap_chunk *next; } stbtt__hheap_chunk; typedef struct stbtt__hheap { struct stbtt__hheap_chunk *head; void *first_free; int num_remaining_in_head_chunk; } stbtt__hheap; static void *stbtt__hheap_alloc(stbtt__hheap *hh, size_t size, void *userdata) { if (hh->first_free) { void *p = hh->first_free; hh->first_free = * (void **) p; return p; } else { if (hh->num_remaining_in_head_chunk == 0) { int count = (size < 32 ? 2000 : size < 128 ? 800 : 100); stbtt__hheap_chunk *c = (stbtt__hheap_chunk *) STBTT_malloc(sizeof(stbtt__hheap_chunk) + size * count, userdata); if (c == NULL) return NULL; c->next = hh->head; hh->head = c; hh->num_remaining_in_head_chunk = count; } --hh->num_remaining_in_head_chunk; return (char *) (hh->head) + sizeof(stbtt__hheap_chunk) + size * hh->num_remaining_in_head_chunk; } } static void stbtt__hheap_free(stbtt__hheap *hh, void *p) { *(void **) p = hh->first_free; hh->first_free = p; } static void stbtt__hheap_cleanup(stbtt__hheap *hh, void *userdata) { stbtt__hheap_chunk *c = hh->head; while (c) { stbtt__hheap_chunk *n = c->next; STBTT_free(c, userdata); c = n; } } typedef struct stbtt__edge { float x0,y0, x1,y1; int invert; } stbtt__edge; typedef struct stbtt__active_edge { struct stbtt__active_edge *next; #if STBTT_RASTERIZER_VERSION==1 int x,dx; float ey; int direction; #elif STBTT_RASTERIZER_VERSION==2 float fx,fdx,fdy; float direction; float sy; float ey; #else #error "Unrecognized value of STBTT_RASTERIZER_VERSION" #endif } stbtt__active_edge; #if STBTT_RASTERIZER_VERSION == 1 #define STBTT_FIXSHIFT 10 #define STBTT_FIX (1 << STBTT_FIXSHIFT) #define STBTT_FIXMASK (STBTT_FIX-1) static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata) { stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata); float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); STBTT_assert(z != NULL); if (!z) return z; // round dx down to avoid overshooting if (dxdy < 0) z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy); else z->dx = STBTT_ifloor(STBTT_FIX * dxdy); 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 z->x -= off_x * STBTT_FIX; z->ey = e->y1; z->next = 0; z->direction = e->invert ? 1 : -1; return z; } #elif STBTT_RASTERIZER_VERSION == 2 static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata) { stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata); float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); STBTT_assert(z != NULL); //STBTT_assert(e->y0 <= start_point); if (!z) return z; z->fdx = dxdy; z->fdy = dxdy != 0.0f ? (1.0f/dxdy) : 0.0f; z->fx = e->x0 + dxdy * (start_point - e->y0); z->fx -= off_x; z->direction = e->invert ? 1.0f : -1.0f; z->sy = e->y0; z->ey = e->y1; z->next = 0; return z; } #else #error "Unrecognized value of STBTT_RASTERIZER_VERSION" #endif #if STBTT_RASTERIZER_VERSION == 1 // note: this routine clips fills that extend off the edges... ideally this // wouldn't happen, but it could happen if the truetype glyph bounding boxes // are wrong, or if the user supplies a too-small bitmap static void stbtt__fill_active_edges(unsigned char *scanline, int len, stbtt__active_edge *e, int max_weight) { // non-zero winding fill int x0=0, w=0; while (e) { if (w == 0) { // if we're currently at zero, we need to record the edge start point x0 = e->x; w += e->direction; } else { int x1 = e->x; w += e->direction; // if we went to zero, we need to draw if (w == 0) { int i = x0 >> STBTT_FIXSHIFT; int j = x1 >> STBTT_FIXSHIFT; if (i < len && j >= 0) { if (i == j) { // x0,x1 are the same pixel, so compute combined coverage scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> STBTT_FIXSHIFT); } else { if (i >= 0) // add antialiasing for x0 scanline[i] = scanline[i] + (stbtt_uint8) (((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT); else i = -1; // clip if (j < len) // add antialiasing for x1 scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT); else j = len; // clip for (++i; i < j; ++i) // fill pixels between x0 and x1 scanline[i] = scanline[i] + (stbtt_uint8) max_weight; } } } } e = e->next; } } static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata) { stbtt__hheap hh = { 0, 0, 0 }; stbtt__active_edge *active = NULL; int y,j=0; int max_weight = (255 / vsubsample); // weight per vertical scanline int s; // vertical subsample index unsigned char scanline_data[512], *scanline; if (result->w > 512) scanline = (unsigned char *) STBTT_malloc(result->w, userdata); else scanline = scanline_data; y = off_y * vsubsample; e[n].y0 = (off_y + result->h) * (float) vsubsample + 1; while (j < result->h) { STBTT_memset(scanline, 0, result->w); for (s=0; s < vsubsample; ++s) { // find center of pixel for this scanline float scan_y = y + 0.5f; stbtt__active_edge **step = &active; // update all active edges; // remove all active edges that terminate before the center of this scanline while (*step) { stbtt__active_edge * z = *step; if (z->ey <= scan_y) { *step = z->next; // delete from list STBTT_assert(z->direction); z->direction = 0; stbtt__hheap_free(&hh, z); } else { z->x += z->dx; // advance to position for current scanline step = &((*step)->next); // advance through list } } // resort the list if needed for(;;) { int changed=0; step = &active; while (*step && (*step)->next) { if ((*step)->x > (*step)->next->x) { stbtt__active_edge *t = *step; stbtt__active_edge *q = t->next; t->next = q->next; q->next = t; *step = q; changed = 1; } step = &(*step)->next; } if (!changed) break; } // insert all edges that start before the center of this scanline -- omit ones that also end on this scanline while (e->y0 <= scan_y) { if (e->y1 > scan_y) { stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata); if (z != NULL) { // find insertion point if (active == NULL) active = z; else if (z->x < active->x) { // insert at front z->next = active; active = z; } else { // find thing to insert AFTER stbtt__active_edge *p = active; while (p->next && p->next->x < z->x) p = p->next; // at this point, p->next->x is NOT < z->x z->next = p->next; p->next = z; } } } ++e; } // now process all active edges in XOR fashion if (active) stbtt__fill_active_edges(scanline, result->w, active, max_weight); ++y; } STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w); ++j; } stbtt__hheap_cleanup(&hh, userdata); if (scanline != scanline_data) STBTT_free(scanline, userdata); } #elif STBTT_RASTERIZER_VERSION == 2 // the edge passed in here does not cross the vertical line at x or the vertical line at x+1 // (i.e. it has already been clipped to those) static void stbtt__handle_clipped_edge(float *scanline, int x, stbtt__active_edge *e, float x0, float y0, float x1, float y1) { if (y0 == y1) return; STBTT_assert(y0 < y1); STBTT_assert(e->sy <= e->ey); if (y0 > e->ey) return; if (y1 < e->sy) return; if (y0 < e->sy) { x0 += (x1-x0) * (e->sy - y0) / (y1-y0); y0 = e->sy; } if (y1 > e->ey) { x1 += (x1-x0) * (e->ey - y1) / (y1-y0); y1 = e->ey; } if (x0 == x) STBTT_assert(x1 <= x+1); else if (x0 == x+1) STBTT_assert(x1 >= x); else if (x0 <= x) STBTT_assert(x1 <= x); else if (x0 >= x+1) STBTT_assert(x1 >= x+1); else STBTT_assert(x1 >= x && x1 <= x+1); if (x0 <= x && x1 <= x) scanline[x] += e->direction * (y1-y0); else if (x0 >= x+1 && x1 >= x+1) ; else { STBTT_assert(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1); scanline[x] += e->direction * (y1-y0) * (1-((x0-x)+(x1-x))/2); // coverage = 1 - average x position } } static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, int len, stbtt__active_edge *e, float y_top) { float y_bottom = y_top+1; while (e) { // brute force every pixel // compute intersection points with top & bottom STBTT_assert(e->ey >= y_top); if (e->fdx == 0) { float x0 = e->fx; if (x0 < len) { if (x0 >= 0) { stbtt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom); stbtt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom); } else { stbtt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom); } } } else { float x0 = e->fx; float dx = e->fdx; float xb = x0 + dx; float x_top, x_bottom; float sy0,sy1; float dy = e->fdy; STBTT_assert(e->sy <= y_bottom && e->ey >= y_top); // compute endpoints of line segment clipped to this scanline (if the // line segment starts on this scanline. x0 is the intersection of the // line with y_top, but that may be off the line segment. if (e->sy > y_top) { x_top = x0 + dx * (e->sy - y_top); sy0 = e->sy; } else { x_top = x0; sy0 = y_top; } if (e->ey < y_bottom) { x_bottom = x0 + dx * (e->ey - y_top); sy1 = e->ey; } else { x_bottom = xb; sy1 = y_bottom; } if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) { // from here on, we don't have to range check x values if ((int) x_top == (int) x_bottom) { float height; // simple case, only spans one pixel int x = (int) x_top; height = sy1 - sy0; STBTT_assert(x >= 0 && x < len); scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height; scanline_fill[x] += e->direction * height; // everything right of this pixel is filled } else { int x,x1,x2; float y_crossing, step, sign, area; // covers 2+ pixels if (x_top > x_bottom) { // flip scanline vertically; signed area is the same float t; sy0 = y_bottom - (sy0 - y_top); sy1 = y_bottom - (sy1 - y_top); t = sy0, sy0 = sy1, sy1 = t; t = x_bottom, x_bottom = x_top, x_top = t; dx = -dx; dy = -dy; t = x0, x0 = xb, xb = t; // [DEAR IMGUI] Fix static analyzer warning (void)dx; // [ImGui: fix static analyzer warning] } x1 = (int) x_top; x2 = (int) x_bottom; // compute intersection with y axis at x1+1 y_crossing = (x1+1 - x0) * dy + y_top; sign = e->direction; // area of the rectangle covered from y0..y_crossing area = sign * (y_crossing-sy0); // area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2); step = sign * dy; for (x = x1+1; x < x2; ++x) { scanline[x] += area + step/2; area += step; } y_crossing += dy * (x2 - (x1+1)); STBTT_assert(STBTT_fabs(area) <= 1.01f); scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing); scanline_fill[x2] += sign * (sy1-sy0); } } else { // if edge goes outside of box we're drawing, we require // clipping logic. since this does not match the intended use // of this library, we use a different, very slow brute // force implementation int x; for (x=0; x < len; ++x) { // cases: // // there can be up to two intersections with the pixel. any intersection // with left or right edges can be handled by splitting into two (or three) // regions. intersections with top & bottom do not necessitate case-wise logic. // // the old way of doing this found the intersections with the left & right edges, // then used some simple logic to produce up to three segments in sorted order // from top-to-bottom. however, this had a problem: if an x edge was epsilon // across the x border, then the corresponding y position might not be distinct // from the other y segment, and it might ignored as an empty segment. to avoid // that, we need to explicitly produce segments based on x positions. // rename variables to clearly-defined pairs float y0 = y_top; float x1 = (float) (x); float x2 = (float) (x+1); float x3 = xb; float y3 = y_bottom; // x = e->x + e->dx * (y-y_top) // (y-y_top) = (x - e->x) / e->dx // y = (x - e->x) / e->dx + y_top float y1 = (x - x0) / dx + y_top; float y2 = (x+1 - x0) / dx + y_top; if (x0 < x1 && x3 > x2) { // three segments descending down-right stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x2,y2); stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); } else if (x3 < x1 && x0 > x2) { // three segments descending down-left stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x1,y1); stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); } else if (x0 < x1 && x3 > x1) { // two segments across x, down-right stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); } else if (x3 < x1 && x0 > x1) { // two segments across x, down-left stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); } else if (x0 < x2 && x3 > x2) { // two segments across x+1, down-right stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); } else if (x3 < x2 && x0 > x2) { // two segments across x+1, down-left stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); } else { // one segment stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x3,y3); } } } } e = e->next; } } // directly AA rasterize edges w/o supersampling static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata) { stbtt__hheap hh = { 0, 0, 0 }; stbtt__active_edge *active = NULL; int y,j=0, i; float scanline_data[129], *scanline, *scanline2; STBTT__NOTUSED(vsubsample); if (result->w > 64) scanline = (float *) STBTT_malloc((result->w*2+1) * sizeof(float), userdata); else scanline = scanline_data; scanline2 = scanline + result->w; y = off_y; e[n].y0 = (float) (off_y + result->h) + 1; while (j < result->h) { // find center of pixel for this scanline float scan_y_top = y + 0.0f; float scan_y_bottom = y + 1.0f; stbtt__active_edge **step = &active; STBTT_memset(scanline , 0, result->w*sizeof(scanline[0])); STBTT_memset(scanline2, 0, (result->w+1)*sizeof(scanline[0])); // update all active edges; // remove all active edges that terminate before the top of this scanline while (*step) { stbtt__active_edge * z = *step; if (z->ey <= scan_y_top) { *step = z->next; // delete from list STBTT_assert(z->direction); z->direction = 0; stbtt__hheap_free(&hh, z); } else { step = &((*step)->next); // advance through list } } // insert all edges that start before the bottom of this scanline while (e->y0 <= scan_y_bottom) { if (e->y0 != e->y1) { stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata); if (z != NULL) { if (j == 0 && off_y != 0) { if (z->ey < scan_y_top) { // this can happen due to subpixel positioning and some kind of fp rounding error i think z->ey = scan_y_top; } } STBTT_assert(z->ey >= scan_y_top); // if we get really unlucky a tiny bit of an edge can be out of bounds // insert at front z->next = active; active = z; } } ++e; } // now process all active edges if (active) stbtt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top); { float sum = 0; for (i=0; i < result->w; ++i) { float k; int m; sum += scanline2[i]; k = scanline[i] + sum; k = (float) STBTT_fabs(k)*255 + 0.5f; m = (int) k; if (m > 255) m = 255; result->pixels[j*result->stride + i] = (unsigned char) m; } } // advance all the edges step = &active; while (*step) { stbtt__active_edge *z = *step; z->fx += z->fdx; // advance to position for current scanline step = &((*step)->next); // advance through list } ++y; ++j; } stbtt__hheap_cleanup(&hh, userdata); if (scanline != scanline_data) STBTT_free(scanline, userdata); } #else #error "Unrecognized value of STBTT_RASTERIZER_VERSION" #endif #define STBTT__COMPARE(a,b) ((a)->y0 < (b)->y0) static void stbtt__sort_edges_ins_sort(stbtt__edge *p, int n) { int i,j; for (i=1; i < n; ++i) { stbtt__edge t = p[i], *a = &t; j = i; while (j > 0) { stbtt__edge *b = &p[j-1]; int c = STBTT__COMPARE(a,b); if (!c) break; p[j] = p[j-1]; --j; } if (i != j) p[j] = t; } } static void stbtt__sort_edges_quicksort(stbtt__edge *p, int n) { /* threshold for transitioning to insertion sort */ while (n > 12) { stbtt__edge t; int c01,c12,c,m,i,j; /* compute median of three */ m = n >> 1; c01 = STBTT__COMPARE(&p[0],&p[m]); c12 = STBTT__COMPARE(&p[m],&p[n-1]); /* if 0 >= mid >= end, or 0 < mid < end, then use mid */ if (c01 != c12) { /* otherwise, we'll need to swap something else to middle */ int z; c = STBTT__COMPARE(&p[0],&p[n-1]); /* 0>mid && midn => n; 0 0 */ /* 0n: 0>n => 0; 0 n */ z = (c == c12) ? 0 : n-1; t = p[z]; p[z] = p[m]; p[m] = t; } /* now p[m] is the median-of-three */ /* swap it to the beginning so it won't move around */ t = p[0]; p[0] = p[m]; p[m] = t; /* partition loop */ i=1; j=n-1; for(;;) { /* handling of equality is crucial here */ /* for sentinels & efficiency with duplicates */ for (;;++i) { if (!STBTT__COMPARE(&p[i], &p[0])) break; } for (;;--j) { if (!STBTT__COMPARE(&p[0], &p[j])) break; } /* make sure we haven't crossed */ if (i >= j) break; t = p[i]; p[i] = p[j]; p[j] = t; ++i; --j; } /* recurse on smaller side, iterate on larger */ if (j < (n-i)) { stbtt__sort_edges_quicksort(p,j); p = p+i; n = n-i; } else { stbtt__sort_edges_quicksort(p+i, n-i); n = j; } } } static void stbtt__sort_edges(stbtt__edge *p, int n) { stbtt__sort_edges_quicksort(p, n); stbtt__sort_edges_ins_sort(p, n); } typedef struct { float x,y; } stbtt__point; static 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) { float y_scale_inv = invert ? -scale_y : scale_y; stbtt__edge *e; int n,i,j,k,m; #if STBTT_RASTERIZER_VERSION == 1 int vsubsample = result->h < 8 ? 15 : 5; #elif STBTT_RASTERIZER_VERSION == 2 int vsubsample = 1; #else #error "Unrecognized value of STBTT_RASTERIZER_VERSION" #endif // vsubsample should divide 255 evenly; otherwise we won't reach full opacity // now we have to blow out the windings into explicit edge lists n = 0; for (i=0; i < windings; ++i) n += wcount[i]; e = (stbtt__edge *) STBTT_malloc(sizeof(*e) * (n+1), userdata); // add an extra one as a sentinel if (e == 0) return; n = 0; m=0; for (i=0; i < windings; ++i) { stbtt__point *p = pts + m; m += wcount[i]; j = wcount[i]-1; for (k=0; k < wcount[i]; j=k++) { int a=k,b=j; // skip the edge if horizontal if (p[j].y == p[k].y) continue; // add edge from j to k to the list e[n].invert = 0; if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) { e[n].invert = 1; a=j,b=k; } e[n].x0 = p[a].x * scale_x + shift_x; e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample; e[n].x1 = p[b].x * scale_x + shift_x; e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample; ++n; } } // now sort the edges by their highest point (should snap to integer, and then by x) //STBTT_sort(e, n, sizeof(e[0]), stbtt__edge_compare); stbtt__sort_edges(e, n); // now, traverse the scanlines and find the intersections on each scanline, use xor winding rule stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata); STBTT_free(e, userdata); } static void stbtt__add_point(stbtt__point *points, int n, float x, float y) { if (!points) return; // during first pass, it's unallocated points[n].x = x; points[n].y = y; } // tessellate until threshold p is happy... @TODO warped to compensate for non-linear stretching static 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) { // midpoint float mx = (x0 + 2*x1 + x2)/4; float my = (y0 + 2*y1 + y2)/4; // versus directly drawn line float dx = (x0+x2)/2 - mx; float dy = (y0+y2)/2 - my; if (n > 16) // 65536 segments on one curve better be enough! return 1; if (dx*dx+dy*dy > objspace_flatness_squared) { // half-pixel error allowed... need to be smaller if AA stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1); stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1); } else { stbtt__add_point(points, *num_points,x2,y2); *num_points = *num_points+1; } return 1; } static 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) { // @TODO this "flatness" calculation is just made-up nonsense that seems to work well enough float dx0 = x1-x0; float dy0 = y1-y0; float dx1 = x2-x1; float dy1 = y2-y1; float dx2 = x3-x2; float dy2 = y3-y2; float dx = x3-x0; float dy = y3-y0; float longlen = (float) (STBTT_sqrt(dx0*dx0+dy0*dy0)+STBTT_sqrt(dx1*dx1+dy1*dy1)+STBTT_sqrt(dx2*dx2+dy2*dy2)); float shortlen = (float) STBTT_sqrt(dx*dx+dy*dy); float flatness_squared = longlen*longlen-shortlen*shortlen; if (n > 16) // 65536 segments on one curve better be enough! return; if (flatness_squared > objspace_flatness_squared) { float x01 = (x0+x1)/2; float y01 = (y0+y1)/2; float x12 = (x1+x2)/2; float y12 = (y1+y2)/2; float x23 = (x2+x3)/2; float y23 = (y2+y3)/2; float xa = (x01+x12)/2; float ya = (y01+y12)/2; float xb = (x12+x23)/2; float yb = (y12+y23)/2; float mx = (xa+xb)/2; float my = (ya+yb)/2; stbtt__tesselate_cubic(points, num_points, x0,y0, x01,y01, xa,ya, mx,my, objspace_flatness_squared,n+1); stbtt__tesselate_cubic(points, num_points, mx,my, xb,yb, x23,y23, x3,y3, objspace_flatness_squared,n+1); } else { stbtt__add_point(points, *num_points,x3,y3); *num_points = *num_points+1; } } // returns number of contours static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata) { stbtt__point *points=0; int num_points=0; float objspace_flatness_squared = objspace_flatness * objspace_flatness; int i,n=0,start=0, pass; // count how many "moves" there are to get the contour count for (i=0; i < num_verts; ++i) if (vertices[i].type == STBTT_vmove) ++n; *num_contours = n; if (n == 0) return 0; *contour_lengths = (int *) STBTT_malloc(sizeof(**contour_lengths) * n, userdata); if (*contour_lengths == 0) { *num_contours = 0; return 0; } // make two passes through the points so we don't need to realloc for (pass=0; pass < 2; ++pass) { float x=0,y=0; if (pass == 1) { points = (stbtt__point *) STBTT_malloc(num_points * sizeof(points[0]), userdata); if (points == NULL) goto error; } num_points = 0; n= -1; for (i=0; i < num_verts; ++i) { switch (vertices[i].type) { case STBTT_vmove: // start the next contour if (n >= 0) (*contour_lengths)[n] = num_points - start; ++n; start = num_points; x = vertices[i].x, y = vertices[i].y; stbtt__add_point(points, num_points++, x,y); break; case STBTT_vline: x = vertices[i].x, y = vertices[i].y; stbtt__add_point(points, num_points++, x, y); break; case STBTT_vcurve: stbtt__tesselate_curve(points, &num_points, x,y, vertices[i].cx, vertices[i].cy, vertices[i].x, vertices[i].y, objspace_flatness_squared, 0); x = vertices[i].x, y = vertices[i].y; break; case STBTT_vcubic: stbtt__tesselate_cubic(points, &num_points, x,y, vertices[i].cx, vertices[i].cy, vertices[i].cx1, vertices[i].cy1, vertices[i].x, vertices[i].y, objspace_flatness_squared, 0); x = vertices[i].x, y = vertices[i].y; break; } } (*contour_lengths)[n] = num_points - start; } return points; error: STBTT_free(points, userdata); STBTT_free(*contour_lengths, userdata); *contour_lengths = 0; *num_contours = 0; return NULL; } STBTT_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) { float scale = scale_x > scale_y ? scale_y : scale_x; int winding_count = 0; int *winding_lengths = NULL; stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata); if (windings) { stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata); STBTT_free(winding_lengths, userdata); STBTT_free(windings, userdata); } } STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata) { STBTT_free(bitmap, userdata); } STBTT_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) { int ix0,iy0,ix1,iy1; stbtt__bitmap gbm; stbtt_vertex *vertices; int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); if (scale_x == 0) scale_x = scale_y; if (scale_y == 0) { if (scale_x == 0) { STBTT_free(vertices, info->userdata); return NULL; } scale_y = scale_x; } stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1); // now we get the size gbm.w = (ix1 - ix0); gbm.h = (iy1 - iy0); gbm.pixels = NULL; // in case we error if (width ) *width = gbm.w; if (height) *height = gbm.h; if (xoff ) *xoff = ix0; if (yoff ) *yoff = iy0; if (gbm.w && gbm.h) { gbm.pixels = (unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata); if (gbm.pixels) { gbm.stride = gbm.w; stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata); } } STBTT_free(vertices, info->userdata); return gbm.pixels; } STBTT_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) { return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff); } STBTT_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) { int ix0,iy0; stbtt_vertex *vertices; int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); stbtt__bitmap gbm; stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0); gbm.pixels = output; gbm.w = out_w; gbm.h = out_h; gbm.stride = out_stride; if (gbm.w && gbm.h) stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->userdata); STBTT_free(vertices, info->userdata); } STBTT_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) { stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph); } STBTT_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) { return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff); } STBTT_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) { 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)); } STBTT_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) { stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint)); } STBTT_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) { return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff); } STBTT_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) { stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint); } ////////////////////////////////////////////////////////////////////////////// // // bitmap baking // // This is SUPER-CRAPPY packing to keep source code small static int stbtt_BakeFontBitmap_internal(unsigned char *data, int offset, // font location (use offset=0 for plain .ttf) float pixel_height, // height of font in pixels unsigned char *pixels, int pw, int ph, // bitmap to be filled in int first_char, int num_chars, // characters to bake stbtt_bakedchar *chardata) { float scale; int x,y,bottom_y, i; stbtt_fontinfo f; f.userdata = NULL; if (!stbtt_InitFont(&f, data, offset)) return -1; STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels x=y=1; bottom_y = 1; scale = stbtt_ScaleForPixelHeight(&f, pixel_height); for (i=0; i < num_chars; ++i) { int advance, lsb, x0,y0,x1,y1,gw,gh; int g = stbtt_FindGlyphIndex(&f, first_char + i); stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb); stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1); gw = x1-x0; gh = y1-y0; if (x + gw + 1 >= pw) y = bottom_y, x = 1; // advance to next row if (y + gh + 1 >= ph) // check if it fits vertically AFTER potentially moving to next row return -i; STBTT_assert(x+gw < pw); STBTT_assert(y+gh < ph); stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g); chardata[i].x0 = (stbtt_int16) x; chardata[i].y0 = (stbtt_int16) y; chardata[i].x1 = (stbtt_int16) (x + gw); chardata[i].y1 = (stbtt_int16) (y + gh); chardata[i].xadvance = scale * advance; chardata[i].xoff = (float) x0; chardata[i].yoff = (float) y0; x = x + gw + 1; if (y+gh+1 > bottom_y) bottom_y = y+gh+1; } return bottom_y; } STBTT_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) { float d3d_bias = opengl_fillrule ? 0 : -0.5f; float ipw = 1.0f / pw, iph = 1.0f / ph; const stbtt_bakedchar *b = chardata + char_index; int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f); int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f); q->x0 = round_x + d3d_bias; q->y0 = round_y + d3d_bias; q->x1 = round_x + b->x1 - b->x0 + d3d_bias; q->y1 = round_y + b->y1 - b->y0 + d3d_bias; q->s0 = b->x0 * ipw; q->t0 = b->y0 * iph; q->s1 = b->x1 * ipw; q->t1 = b->y1 * iph; *xpos += b->xadvance; } ////////////////////////////////////////////////////////////////////////////// // // rectangle packing replacement routines if you don't have stb_rect_pack.h // #ifndef STB_RECT_PACK_VERSION typedef int stbrp_coord; //////////////////////////////////////////////////////////////////////////////////// // // // // // COMPILER WARNING ?!?!? // // // // // // if you get a compile warning due to these symbols being defined more than // // once, move #include "stb_rect_pack.h" before #include "stb_truetype.h" // // // //////////////////////////////////////////////////////////////////////////////////// typedef struct { int width,height; int x,y,bottom_y; } stbrp_context; typedef struct { unsigned char x; } stbrp_node; struct stbrp_rect { stbrp_coord x,y; int id,w,h,was_packed; }; static void stbrp_init_target(stbrp_context *con, int pw, int ph, stbrp_node *nodes, int num_nodes) { con->width = pw; con->height = ph; con->x = 0; con->y = 0; con->bottom_y = 0; STBTT__NOTUSED(nodes); STBTT__NOTUSED(num_nodes); } static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects, int num_rects) { int i; for (i=0; i < num_rects; ++i) { if (con->x + rects[i].w > con->width) { con->x = 0; con->y = con->bottom_y; } if (con->y + rects[i].h > con->height) break; rects[i].x = con->x; rects[i].y = con->y; rects[i].was_packed = 1; con->x += rects[i].w; if (con->y + rects[i].h > con->bottom_y) con->bottom_y = con->y + rects[i].h; } for ( ; i < num_rects; ++i) rects[i].was_packed = 0; } #endif ////////////////////////////////////////////////////////////////////////////// // // bitmap baking // // This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If // stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy. STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context) { stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context) ,alloc_context); int num_nodes = pw - padding; stbrp_node *nodes = (stbrp_node *) STBTT_malloc(sizeof(*nodes ) * num_nodes,alloc_context); if (context == NULL || nodes == NULL) { if (context != NULL) STBTT_free(context, alloc_context); if (nodes != NULL) STBTT_free(nodes , alloc_context); return 0; } spc->user_allocator_context = alloc_context; spc->width = pw; spc->height = ph; spc->pixels = pixels; spc->pack_info = context; spc->nodes = nodes; spc->padding = padding; spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw; spc->h_oversample = 1; spc->v_oversample = 1; spc->skip_missing = 0; stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes); if (pixels) STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels return 1; } STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc) { STBTT_free(spc->nodes , spc->user_allocator_context); STBTT_free(spc->pack_info, spc->user_allocator_context); } STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample) { STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE); STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE); if (h_oversample <= STBTT_MAX_OVERSAMPLE) spc->h_oversample = h_oversample; if (v_oversample <= STBTT_MAX_OVERSAMPLE) spc->v_oversample = v_oversample; } STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip) { spc->skip_missing = skip; } #define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1) static void stbtt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) { unsigned char buffer[STBTT_MAX_OVERSAMPLE]; int safe_w = w - kernel_width; int j; STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); // suppress bogus warning from VS2013 -analyze for (j=0; j < h; ++j) { int i; unsigned int total; STBTT_memset(buffer, 0, kernel_width); total = 0; // make kernel_width a constant in common cases so compiler can optimize out the divide switch (kernel_width) { case 2: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; pixels[i] = (unsigned char) (total / 2); } break; case 3: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; pixels[i] = (unsigned char) (total / 3); } break; case 4: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; pixels[i] = (unsigned char) (total / 4); } break; case 5: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; pixels[i] = (unsigned char) (total / 5); } break; default: for (i=0; i <= safe_w; ++i) { total += pixels[i] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; pixels[i] = (unsigned char) (total / kernel_width); } break; } for (; i < w; ++i) { STBTT_assert(pixels[i] == 0); total -= buffer[i & STBTT__OVER_MASK]; pixels[i] = (unsigned char) (total / kernel_width); } pixels += stride_in_bytes; } } static void stbtt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) { unsigned char buffer[STBTT_MAX_OVERSAMPLE]; int safe_h = h - kernel_width; int j; STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); // suppress bogus warning from VS2013 -analyze for (j=0; j < w; ++j) { int i; unsigned int total; STBTT_memset(buffer, 0, kernel_width); total = 0; // make kernel_width a constant in common cases so compiler can optimize out the divide switch (kernel_width) { case 2: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; pixels[i*stride_in_bytes] = (unsigned char) (total / 2); } break; case 3: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; pixels[i*stride_in_bytes] = (unsigned char) (total / 3); } break; case 4: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; pixels[i*stride_in_bytes] = (unsigned char) (total / 4); } break; case 5: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; pixels[i*stride_in_bytes] = (unsigned char) (total / 5); } break; default: for (i=0; i <= safe_h; ++i) { total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width); } break; } for (; i < h; ++i) { STBTT_assert(pixels[i*stride_in_bytes] == 0); total -= buffer[i & STBTT__OVER_MASK]; pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width); } pixels += 1; } } static float stbtt__oversample_shift(int oversample) { if (!oversample) return 0.0f; // The prefilter is a box filter of width "oversample", // which shifts phase by (oversample - 1)/2 pixels in // oversampled space. We want to shift in the opposite // direction to counter this. return (float)-(oversample - 1) / (2.0f * (float)oversample); } // rects array must be big enough to accommodate all characters in the given ranges STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects) { int i,j,k; k=0; for (i=0; i < num_ranges; ++i) { float fh = ranges[i].font_size; float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh); ranges[i].h_oversample = (unsigned char) spc->h_oversample; ranges[i].v_oversample = (unsigned char) spc->v_oversample; for (j=0; j < ranges[i].num_chars; ++j) { int x0,y0,x1,y1; int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j]; int glyph = stbtt_FindGlyphIndex(info, codepoint); if (glyph == 0 && spc->skip_missing) { rects[k].w = rects[k].h = 0; } else { stbtt_GetGlyphBitmapBoxSubpixel(info,glyph, scale * spc->h_oversample, scale * spc->v_oversample, 0,0, &x0,&y0,&x1,&y1); rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1); rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1); } ++k; } } return k; } STBTT_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) { stbtt_MakeGlyphBitmapSubpixel(info, output, out_w - (prefilter_x - 1), out_h - (prefilter_y - 1), out_stride, scale_x, scale_y, shift_x, shift_y, glyph); if (prefilter_x > 1) stbtt__h_prefilter(output, out_w, out_h, out_stride, prefilter_x); if (prefilter_y > 1) stbtt__v_prefilter(output, out_w, out_h, out_stride, prefilter_y); *sub_x = stbtt__oversample_shift(prefilter_x); *sub_y = stbtt__oversample_shift(prefilter_y); } // rects array must be big enough to accommodate all characters in the given ranges STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects) { int i,j,k, return_value = 1; // save current values int old_h_over = spc->h_oversample; int old_v_over = spc->v_oversample; k = 0; for (i=0; i < num_ranges; ++i) { float fh = ranges[i].font_size; float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh); float recip_h,recip_v,sub_x,sub_y; spc->h_oversample = ranges[i].h_oversample; spc->v_oversample = ranges[i].v_oversample; recip_h = 1.0f / spc->h_oversample; recip_v = 1.0f / spc->v_oversample; sub_x = stbtt__oversample_shift(spc->h_oversample); sub_y = stbtt__oversample_shift(spc->v_oversample); for (j=0; j < ranges[i].num_chars; ++j) { stbrp_rect *r = &rects[k]; if (r->was_packed && r->w != 0 && r->h != 0) { stbtt_packedchar *bc = &ranges[i].chardata_for_range[j]; int advance, lsb, x0,y0,x1,y1; int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j]; int glyph = stbtt_FindGlyphIndex(info, codepoint); stbrp_coord pad = (stbrp_coord) spc->padding; // pad on left and top r->x += pad; r->y += pad; r->w -= pad; r->h -= pad; stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb); stbtt_GetGlyphBitmapBox(info, glyph, scale * spc->h_oversample, scale * spc->v_oversample, &x0,&y0,&x1,&y1); stbtt_MakeGlyphBitmapSubpixel(info, spc->pixels + r->x + r->y*spc->stride_in_bytes, r->w - spc->h_oversample+1, r->h - spc->v_oversample+1, spc->stride_in_bytes, scale * spc->h_oversample, scale * spc->v_oversample, 0,0, glyph); if (spc->h_oversample > 1) stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes, r->w, r->h, spc->stride_in_bytes, spc->h_oversample); if (spc->v_oversample > 1) stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes, r->w, r->h, spc->stride_in_bytes, spc->v_oversample); bc->x0 = (stbtt_int16) r->x; bc->y0 = (stbtt_int16) r->y; bc->x1 = (stbtt_int16) (r->x + r->w); bc->y1 = (stbtt_int16) (r->y + r->h); bc->xadvance = scale * advance; bc->xoff = (float) x0 * recip_h + sub_x; bc->yoff = (float) y0 * recip_v + sub_y; bc->xoff2 = (x0 + r->w) * recip_h + sub_x; bc->yoff2 = (y0 + r->h) * recip_v + sub_y; } else { return_value = 0; // if any fail, report failure } ++k; } } // restore original values spc->h_oversample = old_h_over; spc->v_oversample = old_v_over; return return_value; } STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects) { stbrp_pack_rects((stbrp_context *) spc->pack_info, rects, num_rects); } STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges) { stbtt_fontinfo info; int i,j,n, return_value = 1; //stbrp_context *context = (stbrp_context *) spc->pack_info; stbrp_rect *rects; // flag all characters as NOT packed for (i=0; i < num_ranges; ++i) for (j=0; j < ranges[i].num_chars; ++j) ranges[i].chardata_for_range[j].x0 = ranges[i].chardata_for_range[j].y0 = ranges[i].chardata_for_range[j].x1 = ranges[i].chardata_for_range[j].y1 = 0; n = 0; for (i=0; i < num_ranges; ++i) n += ranges[i].num_chars; rects = (stbrp_rect *) STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context); if (rects == NULL) return 0; info.userdata = spc->user_allocator_context; stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index)); n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects); stbtt_PackFontRangesPackRects(spc, rects, n); return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects); STBTT_free(rects, spc->user_allocator_context); return return_value; } STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, float font_size, int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range) { stbtt_pack_range range; range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range; range.array_of_unicode_codepoints = NULL; range.num_chars = num_chars_in_range; range.chardata_for_range = chardata_for_range; range.font_size = font_size; return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1); } STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char *fontdata, int index, float size, float *ascent, float *descent, float *lineGap) { int i_ascent, i_descent, i_lineGap; float scale; stbtt_fontinfo info; stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, index)); scale = size > 0 ? stbtt_ScaleForPixelHeight(&info, size) : stbtt_ScaleForMappingEmToPixels(&info, -size); stbtt_GetFontVMetrics(&info, &i_ascent, &i_descent, &i_lineGap); *ascent = (float) i_ascent * scale; *descent = (float) i_descent * scale; *lineGap = (float) i_lineGap * scale; } STBTT_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) { float ipw = 1.0f / pw, iph = 1.0f / ph; const stbtt_packedchar *b = chardata + char_index; if (align_to_integer) { float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5f); float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5f); q->x0 = x; q->y0 = y; q->x1 = x + b->xoff2 - b->xoff; q->y1 = y + b->yoff2 - b->yoff; } else { q->x0 = *xpos + b->xoff; q->y0 = *ypos + b->yoff; q->x1 = *xpos + b->xoff2; q->y1 = *ypos + b->yoff2; } q->s0 = b->x0 * ipw; q->t0 = b->y0 * iph; q->s1 = b->x1 * ipw; q->t1 = b->y1 * iph; *xpos += b->xadvance; } ////////////////////////////////////////////////////////////////////////////// // // sdf computation // #define STBTT_min(a,b) ((a) < (b) ? (a) : (b)) #define STBTT_max(a,b) ((a) < (b) ? (b) : (a)) static int stbtt__ray_intersect_bezier(float orig[2], float ray[2], float q0[2], float q1[2], float q2[2], float hits[2][2]) { float q0perp = q0[1]*ray[0] - q0[0]*ray[1]; float q1perp = q1[1]*ray[0] - q1[0]*ray[1]; float q2perp = q2[1]*ray[0] - q2[0]*ray[1]; float roperp = orig[1]*ray[0] - orig[0]*ray[1]; float a = q0perp - 2*q1perp + q2perp; float b = q1perp - q0perp; float c = q0perp - roperp; float s0 = 0., s1 = 0.; int num_s = 0; if (a != 0.0) { float discr = b*b - a*c; if (discr > 0.0) { float rcpna = -1 / a; float d = (float) STBTT_sqrt(discr); s0 = (b+d) * rcpna; s1 = (b-d) * rcpna; if (s0 >= 0.0 && s0 <= 1.0) num_s = 1; if (d > 0.0 && s1 >= 0.0 && s1 <= 1.0) { if (num_s == 0) s0 = s1; ++num_s; } } } else { // 2*b*s + c = 0 // s = -c / (2*b) s0 = c / (-2 * b); if (s0 >= 0.0 && s0 <= 1.0) num_s = 1; } if (num_s == 0) return 0; else { float rcp_len2 = 1 / (ray[0]*ray[0] + ray[1]*ray[1]); float rayn_x = ray[0] * rcp_len2, rayn_y = ray[1] * rcp_len2; float q0d = q0[0]*rayn_x + q0[1]*rayn_y; float q1d = q1[0]*rayn_x + q1[1]*rayn_y; float q2d = q2[0]*rayn_x + q2[1]*rayn_y; float rod = orig[0]*rayn_x + orig[1]*rayn_y; float q10d = q1d - q0d; float q20d = q2d - q0d; float q0rd = q0d - rod; hits[0][0] = q0rd + s0*(2.0f - 2.0f*s0)*q10d + s0*s0*q20d; hits[0][1] = a*s0+b; if (num_s > 1) { hits[1][0] = q0rd + s1*(2.0f - 2.0f*s1)*q10d + s1*s1*q20d; hits[1][1] = a*s1+b; return 2; } else { return 1; } } } static int equal(float *a, float *b) { return (a[0] == b[0] && a[1] == b[1]); } static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex *verts) { int i; float orig[2], ray[2] = { 1, 0 }; float y_frac; int winding = 0; orig[0] = x; //orig[1] = y; // [DEAR IMGUI] commmented double assignment // make sure y never passes through a vertex of the shape y_frac = (float) STBTT_fmod(y, 1.0f); if (y_frac < 0.01f) y += 0.01f; else if (y_frac > 0.99f) y -= 0.01f; orig[1] = y; // test a ray from (-infinity,y) to (x,y) for (i=0; i < nverts; ++i) { if (verts[i].type == STBTT_vline) { int x0 = (int) verts[i-1].x, y0 = (int) verts[i-1].y; int x1 = (int) verts[i ].x, y1 = (int) verts[i ].y; if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) { float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0; if (x_inter < x) winding += (y0 < y1) ? 1 : -1; } } if (verts[i].type == STBTT_vcurve) { int x0 = (int) verts[i-1].x , y0 = (int) verts[i-1].y ; int x1 = (int) verts[i ].cx, y1 = (int) verts[i ].cy; int x2 = (int) verts[i ].x , y2 = (int) verts[i ].y ; int ax = STBTT_min(x0,STBTT_min(x1,x2)), ay = STBTT_min(y0,STBTT_min(y1,y2)); int by = STBTT_max(y0,STBTT_max(y1,y2)); if (y > ay && y < by && x > ax) { float q0[2],q1[2],q2[2]; float hits[2][2]; q0[0] = (float)x0; q0[1] = (float)y0; q1[0] = (float)x1; q1[1] = (float)y1; q2[0] = (float)x2; q2[1] = (float)y2; if (equal(q0,q1) || equal(q1,q2)) { x0 = (int)verts[i-1].x; y0 = (int)verts[i-1].y; x1 = (int)verts[i ].x; y1 = (int)verts[i ].y; if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) { float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0; if (x_inter < x) winding += (y0 < y1) ? 1 : -1; } } else { int num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits); if (num_hits >= 1) if (hits[0][0] < 0) winding += (hits[0][1] < 0 ? -1 : 1); if (num_hits >= 2) if (hits[1][0] < 0) winding += (hits[1][1] < 0 ? -1 : 1); } } } } return winding; } static float stbtt__cuberoot( float x ) { if (x<0) return -(float) STBTT_pow(-x,1.0f/3.0f); else return (float) STBTT_pow( x,1.0f/3.0f); } // x^3 + c*x^2 + b*x + a = 0 static int stbtt__solve_cubic(float a, float b, float c, float* r) { float s = -a / 3; float p = b - a*a / 3; float q = a * (2*a*a - 9*b) / 27 + c; float p3 = p*p*p; float d = q*q + 4*p3 / 27; if (d >= 0) { float z = (float) STBTT_sqrt(d); float u = (-q + z) / 2; float v = (-q - z) / 2; u = stbtt__cuberoot(u); v = stbtt__cuberoot(v); r[0] = s + u + v; return 1; } else { float u = (float) STBTT_sqrt(-p/3); float v = (float) STBTT_acos(-STBTT_sqrt(-27/p3) * q / 2) / 3; // p3 must be negative, since d is negative float m = (float) STBTT_cos(v); float n = (float) STBTT_cos(v-3.141592/2)*1.732050808f; r[0] = s + u * 2 * m; r[1] = s - u * (m + n); r[2] = s - u * (m - 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? //STBTT_assert( STBTT_fabs(((r[1]+a)*r[1]+b)*r[1]+c) < 0.05f); //STBTT_assert( STBTT_fabs(((r[2]+a)*r[2]+b)*r[2]+c) < 0.05f); return 3; } } STBTT_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) { float scale_x = scale, scale_y = scale; int ix0,iy0,ix1,iy1; int w,h; unsigned char *data; // if one scale is 0, use same scale for both if (scale_x == 0) scale_x = scale_y; if (scale_y == 0) { if (scale_x == 0) return NULL; // if both scales are 0, return NULL scale_y = scale_x; } stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f,0.0f, &ix0,&iy0,&ix1,&iy1); // if empty, return NULL if (ix0 == ix1 || iy0 == iy1) return NULL; ix0 -= padding; iy0 -= padding; ix1 += padding; iy1 += padding; w = (ix1 - ix0); h = (iy1 - iy0); if (width ) *width = w; if (height) *height = h; if (xoff ) *xoff = ix0; if (yoff ) *yoff = iy0; // invert for y-downwards bitmaps scale_y = -scale_y; { int x,y,i,j; float *precompute; stbtt_vertex *verts; int num_verts = stbtt_GetGlyphShape(info, glyph, &verts); data = (unsigned char *) STBTT_malloc(w * h, info->userdata); precompute = (float *) STBTT_malloc(num_verts * sizeof(float), info->userdata); for (i=0,j=num_verts-1; i < num_verts; j=i++) { if (verts[i].type == STBTT_vline) { float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y; float x1 = verts[j].x*scale_x, y1 = verts[j].y*scale_y; float dist = (float) STBTT_sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0)); precompute[i] = (dist == 0) ? 0.0f : 1.0f / dist; } else if (verts[i].type == STBTT_vcurve) { float x2 = verts[j].x *scale_x, y2 = verts[j].y *scale_y; float x1 = verts[i].cx*scale_x, y1 = verts[i].cy*scale_y; float x0 = verts[i].x *scale_x, y0 = verts[i].y *scale_y; float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2; float len2 = bx*bx + by*by; if (len2 != 0.0f) precompute[i] = 1.0f / (bx*bx + by*by); else precompute[i] = 0.0f; } else precompute[i] = 0.0f; } for (y=iy0; y < iy1; ++y) { for (x=ix0; x < ix1; ++x) { float val; float min_dist = 999999.0f; float sx = (float) x + 0.5f; float sy = (float) y + 0.5f; float x_gspace = (sx / scale_x); float y_gspace = (sy / scale_y); 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 for (i=0; i < num_verts; ++i) { float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y; // 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 float dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy); if (dist2 < min_dist*min_dist) min_dist = (float) STBTT_sqrt(dist2); if (verts[i].type == STBTT_vline) { float x1 = verts[i-1].x*scale_x, y1 = verts[i-1].y*scale_y; // coarse culling against bbox //if (sx > STBTT_min(x0,x1)-min_dist && sx < STBTT_max(x0,x1)+min_dist && // sy > STBTT_min(y0,y1)-min_dist && sy < STBTT_max(y0,y1)+min_dist) float dist = (float) STBTT_fabs((x1-x0)*(y0-sy) - (y1-y0)*(x0-sx)) * precompute[i]; STBTT_assert(i != 0); if (dist < min_dist) { // check position along line // x' = x0 + t*(x1-x0), y' = y0 + t*(y1-y0) // minimize (x'-sx)*(x'-sx)+(y'-sy)*(y'-sy) float dx = x1-x0, dy = y1-y0; float px = x0-sx, py = y0-sy; // 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 // derivative: 2*px*dx + 2*py*dy + (2*dx*dx+2*dy*dy)*t, set to 0 and solve float t = -(px*dx + py*dy) / (dx*dx + dy*dy); if (t >= 0.0f && t <= 1.0f) min_dist = dist; } } else if (verts[i].type == STBTT_vcurve) { float x2 = verts[i-1].x *scale_x, y2 = verts[i-1].y *scale_y; float x1 = verts[i ].cx*scale_x, y1 = verts[i ].cy*scale_y; float box_x0 = STBTT_min(STBTT_min(x0,x1),x2); float box_y0 = STBTT_min(STBTT_min(y0,y1),y2); float box_x1 = STBTT_max(STBTT_max(x0,x1),x2); float box_y1 = STBTT_max(STBTT_max(y0,y1),y2); // coarse culling against bbox to avoid computing cubic unnecessarily if (sx > box_x0-min_dist && sx < box_x1+min_dist && sy > box_y0-min_dist && sy < box_y1+min_dist) { int num=0; float ax = x1-x0, ay = y1-y0; float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2; float mx = x0 - sx, my = y0 - sy; float res[3],px,py,t,it; float a_inv = precompute[i]; if (a_inv == 0.0) { // if a_inv is 0, it's 2nd degree so use quadratic formula float a = 3*(ax*bx + ay*by); float b = 2*(ax*ax + ay*ay) + (mx*bx+my*by); float c = mx*ax+my*ay; if (a == 0.0) { // if a is 0, it's linear if (b != 0.0) { res[num++] = -c/b; } } else { float discriminant = b*b - 4*a*c; if (discriminant < 0) num = 0; else { float root = (float) STBTT_sqrt(discriminant); res[0] = (-b - root)/(2*a); res[1] = (-b + root)/(2*a); num = 2; // don't bother distinguishing 1-solution case, as code below will still work } } } else { float b = 3*(ax*bx + ay*by) * a_inv; // could precompute this as it doesn't depend on sample point float c = (2*(ax*ax + ay*ay) + (mx*bx+my*by)) * a_inv; float d = (mx*ax+my*ay) * a_inv; num = stbtt__solve_cubic(b, c, d, res); } if (num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) { t = res[0], it = 1.0f - t; px = it*it*x0 + 2*t*it*x1 + t*t*x2; py = it*it*y0 + 2*t*it*y1 + t*t*y2; dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy); if (dist2 < min_dist * min_dist) min_dist = (float) STBTT_sqrt(dist2); } if (num >= 2 && res[1] >= 0.0f && res[1] <= 1.0f) { t = res[1], it = 1.0f - t; px = it*it*x0 + 2*t*it*x1 + t*t*x2; py = it*it*y0 + 2*t*it*y1 + t*t*y2; dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy); if (dist2 < min_dist * min_dist) min_dist = (float) STBTT_sqrt(dist2); } if (num >= 3 && res[2] >= 0.0f && res[2] <= 1.0f) { t = res[2], it = 1.0f - t; px = it*it*x0 + 2*t*it*x1 + t*t*x2; py = it*it*y0 + 2*t*it*y1 + t*t*y2; dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy); if (dist2 < min_dist * min_dist) min_dist = (float) STBTT_sqrt(dist2); } } } } if (winding == 0) min_dist = -min_dist; // if outside the shape, value is negative val = onedge_value + pixel_dist_scale * min_dist; if (val < 0) val = 0; else if (val > 255) val = 255; data[(y-iy0)*w+(x-ix0)] = (unsigned char) val; } } STBTT_free(precompute, info->userdata); STBTT_free(verts, info->userdata); } return data; } STBTT_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) { return stbtt_GetGlyphSDF(info, scale, stbtt_FindGlyphIndex(info, codepoint), padding, onedge_value, pixel_dist_scale, width, height, xoff, yoff); } STBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata) { STBTT_free(bitmap, userdata); } ////////////////////////////////////////////////////////////////////////////// // // font name matching -- recommended not to use this // // check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2, stbtt_int32 len2) { stbtt_int32 i=0; // convert utf16 to utf8 and compare the results while converting while (len2) { stbtt_uint16 ch = s2[0]*256 + s2[1]; if (ch < 0x80) { if (i >= len1) return -1; if (s1[i++] != ch) return -1; } else if (ch < 0x800) { if (i+1 >= len1) return -1; if (s1[i++] != 0xc0 + (ch >> 6)) return -1; if (s1[i++] != 0x80 + (ch & 0x3f)) return -1; } else if (ch >= 0xd800 && ch < 0xdc00) { stbtt_uint32 c; stbtt_uint16 ch2 = s2[2]*256 + s2[3]; if (i+3 >= len1) return -1; c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000; if (s1[i++] != 0xf0 + (c >> 18)) return -1; if (s1[i++] != 0x80 + ((c >> 12) & 0x3f)) return -1; if (s1[i++] != 0x80 + ((c >> 6) & 0x3f)) return -1; if (s1[i++] != 0x80 + ((c ) & 0x3f)) return -1; s2 += 2; // plus another 2 below len2 -= 2; } else if (ch >= 0xdc00 && ch < 0xe000) { return -1; } else { if (i+2 >= len1) return -1; if (s1[i++] != 0xe0 + (ch >> 12)) return -1; if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f)) return -1; if (s1[i++] != 0x80 + ((ch ) & 0x3f)) return -1; } s2 += 2; len2 -= 2; } return i; } static int stbtt_CompareUTF8toUTF16_bigendian_internal(char *s1, int len1, char *s2, int len2) { return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8*) s1, len1, (stbtt_uint8*) s2, len2); } // returns results in whatever encoding you request... but note that 2-byte encodings // will be BIG-ENDIAN... use stbtt_CompareUTF8toUTF16_bigendian() to compare STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID) { stbtt_int32 i,count,stringOffset; stbtt_uint8 *fc = font->data; stbtt_uint32 offset = font->fontstart; stbtt_uint32 nm = stbtt__find_table(fc, offset, "name"); if (!nm) return NULL; count = ttUSHORT(fc+nm+2); stringOffset = nm + ttUSHORT(fc+nm+4); for (i=0; i < count; ++i) { stbtt_uint32 loc = nm + 6 + 12 * i; if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2) && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) { *length = ttUSHORT(fc+loc+8); return (const char *) (fc+stringOffset+ttUSHORT(fc+loc+10)); } } return NULL; } static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id) { stbtt_int32 i; stbtt_int32 count = ttUSHORT(fc+nm+2); stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4); for (i=0; i < count; ++i) { stbtt_uint32 loc = nm + 6 + 12 * i; stbtt_int32 id = ttUSHORT(fc+loc+6); if (id == target_id) { // find the encoding stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4); // is this a Unicode encoding? if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) { stbtt_int32 slen = ttUSHORT(fc+loc+8); stbtt_int32 off = ttUSHORT(fc+loc+10); // check if there's a prefix match stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen); if (matchlen >= 0) { // check for target_id+1 immediately following, with same encoding & language 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) { slen = ttUSHORT(fc+loc+12+8); off = ttUSHORT(fc+loc+12+10); if (slen == 0) { if (matchlen == nlen) return 1; } else if (matchlen < nlen && name[matchlen] == ' ') { ++matchlen; if (stbtt_CompareUTF8toUTF16_bigendian_internal((char*) (name+matchlen), nlen-matchlen, (char*)(fc+stringOffset+off),slen)) return 1; } } else { // if nothing immediately following if (matchlen == nlen) return 1; } } } // @TODO handle other encodings } } return 0; } static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags) { stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((char *) name); stbtt_uint32 nm,hd; if (!stbtt__isfont(fc+offset)) return 0; // check italics/bold/underline flags in macStyle... if (flags) { hd = stbtt__find_table(fc, offset, "head"); if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7)) return 0; } nm = stbtt__find_table(fc, offset, "name"); if (!nm) return 0; if (flags) { // if we checked the macStyle flags, then just check the family and ignore the subfamily if (stbtt__matchpair(fc, nm, name, nlen, 16, -1)) return 1; if (stbtt__matchpair(fc, nm, name, nlen, 1, -1)) return 1; if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1; } else { if (stbtt__matchpair(fc, nm, name, nlen, 16, 17)) return 1; if (stbtt__matchpair(fc, nm, name, nlen, 1, 2)) return 1; if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1; } return 0; } static int stbtt_FindMatchingFont_internal(unsigned char *font_collection, char *name_utf8, stbtt_int32 flags) { stbtt_int32 i; for (i=0;;++i) { stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i); if (off < 0) return off; if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags)) return off; } } #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-qual" #endif STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, float pixel_height, unsigned char *pixels, int pw, int ph, int first_char, int num_chars, stbtt_bakedchar *chardata) { return stbtt_BakeFontBitmap_internal((unsigned char *) data, offset, pixel_height, pixels, pw, ph, first_char, num_chars, chardata); } STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index) { return stbtt_GetFontOffsetForIndex_internal((unsigned char *) data, index); } STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data) { return stbtt_GetNumberOfFonts_internal((unsigned char *) data); } STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset) { return stbtt_InitFont_internal(info, (unsigned char *) data, offset); } STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags) { return stbtt_FindMatchingFont_internal((unsigned char *) fontdata, (char *) name, flags); } STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2) { return stbtt_CompareUTF8toUTF16_bigendian_internal((char *) s1, len1, (char *) s2, len2); } #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic pop #endif #endif // STB_TRUETYPE_IMPLEMENTATION // FULL VERSION HISTORY // // 1.19 (2018-02-11) OpenType GPOS kerning (horizontal only), STBTT_fmod // 1.18 (2018-01-29) add missing function // 1.17 (2017-07-23) make more arguments const; doc fix // 1.16 (2017-07-12) SDF support // 1.15 (2017-03-03) make more arguments const // 1.14 (2017-01-16) num-fonts-in-TTC function // 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts // 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual // 1.11 (2016-04-02) fix unused-variable warning // 1.10 (2016-04-02) allow user-defined fabs() replacement // fix memory leak if fontsize=0.0 // fix warning from duplicate typedef // 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use alloc userdata for PackFontRanges // 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges // 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; // allow PackFontRanges to pack and render in separate phases; // fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); // fixed an assert() bug in the new rasterizer // replace assert() with STBTT_assert() in new rasterizer // 1.06 (2015-07-14) performance improvements (~35% faster on x86 and x64 on test machine) // also more precise AA rasterizer, except if shapes overlap // remove need for STBTT_sort // 1.05 (2015-04-15) fix misplaced definitions for STBTT_STATIC // 1.04 (2015-04-15) typo in example // 1.03 (2015-04-12) STBTT_STATIC, fix memory leak in new packing, various fixes // 1.02 (2014-12-10) fix various warnings & compile issues w/ stb_rect_pack, C++ // 1.01 (2014-12-08) fix subpixel position when oversampling to exactly match // non-oversampled; STBTT_POINT_SIZE for packed case only // 1.00 (2014-12-06) add new PackBegin etc. API, w/ support for oversampling // 0.99 (2014-09-18) fix multiple bugs with subpixel rendering (ryg) // 0.9 (2014-08-07) support certain mac/iOS fonts without an MS platformID // 0.8b (2014-07-07) fix a warning // 0.8 (2014-05-25) fix a few more warnings // 0.7 (2013-09-25) bugfix: subpixel glyph bug fixed in 0.5 had come back // 0.6c (2012-07-24) improve documentation // 0.6b (2012-07-20) fix a few more warnings // 0.6 (2012-07-17) fix warnings; added stbtt_ScaleForMappingEmToPixels, // stbtt_GetFontBoundingBox, stbtt_IsGlyphEmpty // 0.5 (2011-12-09) bugfixes: // subpixel glyph renderer computed wrong bounding box // first vertex of shape can be off-curve (FreeSans) // 0.4b (2011-12-03) fixed an error in the font baking example // 0.4 (2011-12-01) kerning, subpixel rendering (tor) // bugfixes for: // codepoint-to-glyph conversion using table fmt=12 // codepoint-to-glyph conversion using table fmt=4 // stbtt_GetBakedQuad with non-square texture (Zer) // updated Hello World! sample to use kerning and subpixel // fixed some warnings // 0.3 (2009-06-24) cmap fmt=12, compound shapes (MM) // userdata, malloc-from-userdata, non-zero fill (stb) // 0.2 (2009-03-11) Fix unsigned/signed char warnings // 0.1 (2009-03-09) First public release // /* ------------------------------------------------------------------------------ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2017 Sean Barrett Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ ================================================ FILE: src/imgui_graphics_settings.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "render_tasks/d3d12_rtao_task.hpp" #include "render_tasks/d3d12_hbao.hpp" #include "render_tasks/d3d12_ansel.hpp" #include "render_tasks/d3d12_build_acceleration_structures.hpp" #include "render_tasks/d3d12_rt_shadow_task.hpp" #include "render_tasks/d3d12_shadow_denoiser_task.hpp" namespace wr::imgui::window { static bool rtao_settings_open = true; static bool hbao_settings_open = true; static bool ansel_settings_open = true; static bool asbuild_settings_open = true; static bool shadow_settings_open = true; static bool shadow_denoiser_settings_open = true; void GraphicsSettings(FrameGraph* frame_graph) { if (frame_graph->HasTask() && rtao_settings_open) { auto rtao_user_settings = frame_graph->GetSettings(); ImGui::Begin("RTAO Settings", &rtao_settings_open); ImGui::DragFloat("Bias", &rtao_user_settings.m_runtime.bias, 0.01f, 0.0f, 100.f); ImGui::DragFloat("Radius", &rtao_user_settings.m_runtime.radius, 0.1f, 0.0f, 1000.f); ImGui::DragFloat("Power", &rtao_user_settings.m_runtime.power, 0.1f, 0.0f, 10.f); ImGui::DragInt("SPP", &rtao_user_settings.m_runtime.sample_count, 1, 0, 1073741824); ImGui::End(); frame_graph->UpdateSettings(rtao_user_settings); } if (frame_graph->HasTask() && hbao_settings_open) { auto hbao_user_settings = frame_graph->GetSettings(); ImGui::Begin("HBAO+ Settings", &hbao_settings_open); ImGui::DragFloat("Meters to units", &hbao_user_settings.m_runtime.m_meters_to_view_space_units, 0.1f, 0.1f, 100.f); ImGui::DragFloat("Radius", &hbao_user_settings.m_runtime.m_radius, 0.1f, 0.0f, 100.f); ImGui::DragFloat("Bias", &hbao_user_settings.m_runtime.m_bias, 0.1f, 0.0f, 0.5f); ImGui::DragFloat("Power", &hbao_user_settings.m_runtime.m_power_exp, 0.1f, 1.f, 4.f); ImGui::Checkbox("Blur", &hbao_user_settings.m_runtime.m_enable_blur); ImGui::DragFloat("Blur Sharpness", &hbao_user_settings.m_runtime.m_blur_sharpness, 0.5f, 0.0f, 16.0f); ImGui::End(); frame_graph->UpdateSettings(hbao_user_settings); } if (frame_graph->HasTask() && ansel_settings_open) { auto ansel_user_settings = frame_graph->GetSettings(); ImGui::Begin("NVIDIA Ansel Settings", &ansel_settings_open); ImGui::Checkbox("Translation", &ansel_user_settings.m_runtime.m_allow_translation); ImGui::SameLine(); ImGui::Checkbox("Rotation", &ansel_user_settings.m_runtime.m_allow_rotation); ImGui::SameLine(); ImGui::Checkbox("FOV", &ansel_user_settings.m_runtime.m_allow_fov); ImGui::Checkbox("Mono 360", &ansel_user_settings.m_runtime.m_allow_mono_360); ImGui::SameLine(); ImGui::Checkbox("Stereo 360", &ansel_user_settings.m_runtime.m_allow_stero_360); ImGui::SameLine(); ImGui::Checkbox("Raw HDR", &ansel_user_settings.m_runtime.m_allow_raw); ImGui::Checkbox("Pause", &ansel_user_settings.m_runtime.m_allow_pause); ImGui::SameLine(); ImGui::Checkbox("High res", &ansel_user_settings.m_runtime.m_allow_highres); ImGui::DragFloat("Camera Speed", &ansel_user_settings.m_runtime.m_translation_speed_in_world_units_per_sec, 0.1f, 0.1f, 100.f); ImGui::DragFloat("Rotation Speed", &ansel_user_settings.m_runtime.m_rotation_speed_in_deg_per_second, 5.f, 5.f, 920.f); ImGui::DragFloat("Max FOV", &ansel_user_settings.m_runtime.m_maximum_fov_in_deg, 1.f, 140.f, 179.f); ImGui::End(); frame_graph->UpdateSettings(ansel_user_settings); } if (frame_graph->HasTask() && asbuild_settings_open) { auto as_build_user_settings = frame_graph->GetSettings(); ImGui::Begin("Acceleration Structure Settings", &asbuild_settings_open); ImGui::Checkbox("Disable rebuilding", &as_build_user_settings.m_runtime.m_rebuild_as); ImGui::End(); frame_graph->UpdateSettings(as_build_user_settings); } if (frame_graph->HasTask() && shadow_settings_open) { auto shadow_user_settings = frame_graph->GetSettings(); ImGui::Begin("Shadow Settings", &rtao_settings_open); ImGui::DragFloat("Epsilon", &shadow_user_settings.m_runtime.m_epsilon, 0.01f, 0.0f, 15.f); ImGui::DragInt("Sample Count", &shadow_user_settings.m_runtime.m_sample_count, 1, 1, 64); frame_graph->UpdateSettings(shadow_user_settings); if (frame_graph->HasTask()) { auto shadow_denoiser_user_settings = frame_graph->GetSettings(); ImGui::Dummy(ImVec2(0.0f, 10.0f)); ImGui::LabelText("", "Denoising"); ImGui::Separator(); ImGui::DragFloat("Alpha", &shadow_denoiser_user_settings.m_runtime.m_alpha, 0.01f, 0.001f, 1.f); ImGui::DragFloat("Moments Alpha", &shadow_denoiser_user_settings.m_runtime.m_moments_alpha, 0.01f, 0.001f, 1.f); ImGui::DragFloat("L Phi", &shadow_denoiser_user_settings.m_runtime.m_l_phi, 0.1f, 0.1f, 16.f); ImGui::DragFloat("N Phi", &shadow_denoiser_user_settings.m_runtime.m_n_phi, 1.f, 1.f, 360.f); ImGui::DragFloat("Z Phi", &shadow_denoiser_user_settings.m_runtime.m_z_phi, 0.1f, 0.1f, 16.f); frame_graph->UpdateSettings(shadow_denoiser_user_settings); } ImGui::End(); } } }// namepace imgui::window namespace wr::imgui::menu { void GraphicsSettingsMenu(wr::FrameGraph* frame_graph) { if (ImGui::BeginMenu("Graphics Settings")) { if (frame_graph->HasTask()) { ImGui::MenuItem("RTAO Settings", nullptr, &window::rtao_settings_open); } if (frame_graph->HasTask()) { ImGui::MenuItem("HBAO Settings", nullptr, &window::hbao_settings_open); } if (frame_graph->HasTask()) { ImGui::MenuItem("Ansel Settings", nullptr, &window::ansel_settings_open); } if (frame_graph->HasTask()) { ImGui::MenuItem("AS Build Settings", nullptr, &window::asbuild_settings_open); } if (frame_graph->HasTask()) { ImGui::MenuItem("Shadow Settings", nullptr, &window::shadow_settings_open); } ImGui::EndMenu(); } } }// namespace imgui::menu ================================================ FILE: src/imgui_tools.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "imgui_tools.hpp" #include "imgui/imgui_internal.hpp" #include #include #include #include "scene_graph/camera_node.hpp" #include "shader_registry.hpp" #include "pipeline_registry.hpp" #include "root_signature_registry.hpp" #include "d3d12/d3d12_renderer.hpp" #include "scene_graph/scene_graph.hpp" #include "scene_graph/light_node.hpp" #include "scene_graph/mesh_node.hpp" #include "scene_graph/skybox_node.hpp" #include "model_pool.hpp" #include "shader_registry.hpp" #include "rt_pipeline_registry.hpp" #include "pipeline_registry.hpp" #include "imgui/ImGuizmo.h" namespace wr::imgui::internal { template inline std::string ConvertPointerToStringAddress(const T* obj) { #pragma warning( push ) #pragma warning( disable : 4311) auto address = reinterpret_cast(std::addressof(obj)); #pragma warning( pop ) std::stringstream ss; ss << std::hex << address; return ss.str(); } template inline std::string DecimalToHex(const T decimal) { std::stringstream ss; ss << std::hex << decimal; return ss.str(); } inline std::string ShaderTypeToStr(ShaderType type) { switch (type) { case ShaderType::VERTEX_SHADER: return "Vertex Shader"; case ShaderType::PIXEL_SHADER: return "Pixel Shader"; default: return "Unknown"; } } inline std::string FeatureLevelToStr(D3D_FEATURE_LEVEL level) { switch (level) { case D3D_FEATURE_LEVEL_11_0: return "D3D_FEATURE_LEVEL_11_0"; case D3D_FEATURE_LEVEL_11_1: return "D3D_FEATURE_LEVEL_11_1"; case D3D_FEATURE_LEVEL_12_0: return "D3D_FEATURE_LEVEL_12_0"; case D3D_FEATURE_LEVEL_12_1: return "D3D_FEATURE_LEVEL_12_1"; default: return "Unknown"; } } template inline void AddressText(T* obj) { if (obj) { ImGui::Text("Address: %s", internal::ConvertPointerToStringAddress(obj).c_str()); } else { ImGui::Text("Address: std::nullptr"); } } inline std::string BooltoStr(bool val) { return val ? "True" : "False"; } void ManipulateNode(wr::Node* node, SceneGraph* scene_graph, ImVec2 viewport_pos, ImVec2 viewport_size) { DirectX::XMFLOAT4X4 rmat; auto mat = DirectX::XMMatrixTranslationFromVector(node->m_position); DirectX::XMStoreFloat4x4(&rmat, mat); auto cam = scene_graph->GetActiveCamera(); DirectX::XMFLOAT4X4 rview; DirectX::XMFLOAT4X4 rproj; auto view = cam->m_view; DirectX::XMStoreFloat4x4(&rproj, cam->m_projection); DirectX::XMStoreFloat4x4(&rview, view); ImGuizmo::SetRect(viewport_pos.x, viewport_pos.y, viewport_size.x, viewport_size.y); ImGuizmo::Manipulate(&rview._11, &rproj._11, ImGuizmo::OPERATION::TRANSLATE, ImGuizmo::MODE::WORLD, &rmat._11, NULL, NULL); node->m_position = { rmat._41, rmat._42, rmat._43 }; node->SignalTransformChange(); node->SignalChange(); } } namespace wr::imgui::menu { void Registries() { if (ImGui::BeginMenu("Registries")) { ImGui::MenuItem("Shader Registry", nullptr, &window::open_shader_registry); ImGui::MenuItem("Pipeline Registry", nullptr, &window::open_pipeline_registry); ImGui::MenuItem("RootSignatureRegistry", nullptr, &window::open_root_signature_registry); ImGui::EndMenu(); } } } /* wr::imgui::menu */ namespace wr::imgui::window { void Stats::Draw(D3D12RenderSystem& render_system, ImVec2 viewport_pos) { if (m_open) { auto& io = ImGui::GetIO(); auto os_info = render_system.m_device->m_sys_info; auto dx_info = render_system.m_device->m_adapter_info; std::wstring wdesc(dx_info.Description); std::string desc(wdesc.begin(), wdesc.end()); ImGui::SetNextWindowPos({ viewport_pos.x + 10, viewport_pos.y + 10 }); ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0, 0, 0, 0.5)); ImGui::Begin("Stats", &m_open, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove ); ImGui::Text(desc.c_str()); ImGui::Separator(); ImGui::Text("Framerate: %.1f", io.Framerate); ImGui::Text("Delta: %f", io.DeltaTime); ImGui::Text("Display Size: (%.0f, %.0f)", io.DisplaySize.x, io.DisplaySize.y); ImGui::End(); ImGui::PopStyleColor(); } } void D3D12HardwareInfo(D3D12RenderSystem& render_system) { auto os_info = render_system.m_device->m_sys_info; auto dx_info = render_system.m_device->m_adapter_info; if (open_hardware_info) { ImGui::Begin("Hardware Info", &open_hardware_info); if (ImGui::CollapsingHeader("System Information", ImGuiTreeNodeFlags_DefaultOpen)) { ImGui::Text("Page Size: %i", os_info.dwPageSize); ImGui::Text("Application Address Range: %s - %s", internal::DecimalToHex(os_info.lpMinimumApplicationAddress).c_str(), internal::DecimalToHex(os_info.lpMaximumApplicationAddress).c_str()); ImGui::Text("Active Processor Mask: %i", os_info.dwActiveProcessorMask); ImGui::Text("Processor Count: %i", os_info.dwNumberOfProcessors); switch (os_info.wProcessorArchitecture) { case 9: ImGui::Text("Processor Architecture: %s", "PROCESSOR_ARCHITECTURE_AMD64"); break; case 5: ImGui::Text("Processor Architecture: %s", "PROCESSOR_ARCHITECTURE_ARM"); break; case 6: ImGui::Text("Processor Architecture: %s", "PROCESSOR_ARCHITECTURE_IA64"); break; case 0: ImGui::Text("Processor Architecture: %s", "PROCESSOR_ARCHITECTURE_INTEL"); break; default: ImGui::Text("Processor Architecture: %s", "Unknown"); break; } } if (ImGui::CollapsingHeader("Graphics Adapter Information", ImGuiTreeNodeFlags_DefaultOpen)) { std::wstring wdesc(dx_info.Description); std::string desc(wdesc.begin(), wdesc.end()); auto device = render_system.m_device; ImGui::Text("Description: %s", desc.c_str()); ImGui::Text("Feature Level: %s", internal::FeatureLevelToStr(device->m_feature_level).c_str()); ImGui::Text("DXR Support: %s", internal::BooltoStr(device->m_dxr_support).c_str()); ImGui::Text("DXR Fallback Support: %s", internal::BooltoStr(device->m_dxr_fallback_support).c_str()); ImGui::Text("Vendor ID: %i", dx_info.VendorId); ImGui::Text("Device ID: %i", dx_info.DeviceId); ImGui::Text("Subsystem ID: %i", dx_info.SubSysId); ImGui::Text("Dedicated Video Memory: %.3f", dx_info.DedicatedVideoMemory / 1024.f / 1024.f / 1024.f); ImGui::Text("Dedicated System Memory: %.3f", dx_info.DedicatedSystemMemory); ImGui::Text("Shared System Memory: %.3f", dx_info.SharedSystemMemory / 1024.f / 1024.f / 1024.f); } ImGui::End(); } } void D3D12Settings() { if (open_d3d12_settings) { ImGui::Begin("DirectX 12 Settings", &open_d3d12_settings); ImGui::Text("Num back buffers: %d", d3d12::settings::num_back_buffers); ImGui::Text("Back buffer format: %s", FormatToStr(d3d12::settings::back_buffer_format).c_str()); ImGui::Text("Shader Model: %s", d3d12::settings::default_shader_model); ImGui::Text("Debug Factory: %s", internal::BooltoStr(d3d12::settings::enable_debug_factory).c_str()); ImGui::Text("Enable GPU Timeout: %s", internal::BooltoStr(d3d12::settings::enable_gpu_timeout).c_str()); ImGui::Text("Num instances per batch: %d", d3d12::settings::num_instances_per_batch); ImGui::End(); } } template static bool DefaultContextMenu(std::shared_ptr node, SceneGraph* scene_graph) { if (ImGui::Button("Teleport To")) { scene_graph->GetActiveCamera()->SetPosition(node->m_position); return true; // close popup. } ImGui::Separator(); if (ImGui::Button("Remove")) { scene_graph->DestroyNode(node); return true; // close popup. } return false; } decltype(SceneGraphEditorDetails::sg_editor_type_names) SceneGraphEditorDetails::sg_editor_type_names = { { typeid(LightNode), [](std::shared_ptr node) -> std::string { auto light_node = std::static_pointer_cast(node); switch (light_node->GetType()) { case LightType::DIRECTIONAL: return "Directional Light"; case LightType::POINT: return "Point Light"; case LightType::SPOT: return "Spot Light"; default: return "Light"; } } }, { typeid(MeshNode), [](std::shared_ptr node) -> std::string { auto mesh_node = std::static_pointer_cast(node); auto model_path = mesh_node->m_model->m_model_name; // Remove everything except for the filename. auto last_slash = model_path.find_last_of('/'); if (last_slash != std::string::npos) { model_path.erase(0, last_slash + 1); } std::string prefix = (mesh_node->m_visible ? "" : "[H] "); return prefix + "Mesh (" + model_path + ")"; } }, { typeid(CameraNode), [](std::shared_ptr node) -> std::string { return "Camera Node"; } }, { typeid(SkyboxNode), [](std::shared_ptr node) -> std::string { return "Skybox Node"; } }, }; decltype(SceneGraphEditorDetails::sg_editor_type_inspect) SceneGraphEditorDetails::sg_editor_type_inspect = { { typeid(LightNode), [](std::shared_ptr node, SceneGraph* scene_graph) { auto light_node = std::static_pointer_cast(node); auto& light = *light_node->m_light; const char* listbox_items[] = { "Point Light", "Directional Light", "Spot Light" }; int type = (int)light.tid & 3; ImGui::Combo("Type", &type, listbox_items, 3); light.tid = type; ImGui::ColorEdit3("Color", &light.col.x, ImGuiColorEditFlags_HDR); ImGui::DragFloat3("Position", light_node->m_position.m128_f32, 0.25f); if (type != (uint32_t)LightType::POINT) { float rot[3] = { DirectX::XMConvertToDegrees(DirectX::XMVectorGetX(light_node->m_rotation_radians)), DirectX::XMConvertToDegrees(DirectX::XMVectorGetY(light_node->m_rotation_radians)), DirectX::XMConvertToDegrees(DirectX::XMVectorGetZ(light_node->m_rotation_radians)) }; ImGui::DragFloat3("Rotation", rot, 0.01f); light_node->SetRotation(DirectX::XMVectorSet(DirectX::XMConvertToRadians(rot[0]), DirectX::XMConvertToRadians(rot[1]), DirectX::XMConvertToRadians(rot[2]), 0)); } if (type != (uint32_t)LightType::DIRECTIONAL) { ImGui::DragFloat("Radius", &light.rad, 0.25f); } if (type == (uint32_t)LightType::SPOT) { light.ang = light.ang * 180.f / 3.1415926535f; ImGui::DragFloat("Angle", &light.ang); light.ang = light.ang / 180.f * 3.1415926535f; } static float light_size_temp = 0.0f; ImGui::DragFloat("Light Size", &light_size_temp, 0.1f, 0.0f, 5.0f); light_node->SetLightSize(light_size_temp); if (ImGui::Button("Take Camera Transform")) { light_node->SetPosition(scene_graph->GetActiveCamera()->m_position); light_node->SetRotation(scene_graph->GetActiveCamera()->m_rotation_radians); } light_node->SignalTransformChange(); light_node->SignalChange(); } }, { typeid(MeshNode), [](std::shared_ptr node, SceneGraph* scene_graph) { auto model_node = std::static_pointer_cast(node); auto* model = model_node->m_model; auto& materials = model_node->GetMaterials(); ImGui::Text("Path: %s", model->m_model_name.c_str()); ImGui::Separator(); ImGui::DragFloat3("Position", model_node->m_position.m128_f32, 0.25f); float rot[3] = { DirectX::XMConvertToDegrees(DirectX::XMVectorGetX(model_node->m_rotation_radians)), DirectX::XMConvertToDegrees(DirectX::XMVectorGetY(model_node->m_rotation_radians)), DirectX::XMConvertToDegrees(DirectX::XMVectorGetZ(model_node->m_rotation_radians)) }; ImGui::DragFloat3("Rotation", rot, 0.1f); model_node->SetRotation(DirectX::XMVectorSet(DirectX::XMConvertToRadians(rot[0]), DirectX::XMConvertToRadians(rot[1]), DirectX::XMConvertToRadians(rot[2]), 0)); float scale[3] = { DirectX::XMVectorGetX(model_node->m_scale), DirectX::XMVectorGetY(model_node->m_scale), DirectX::XMVectorGetZ(model_node->m_scale) }; ImGui::DragFloat3("Scale", scale, 0.01f); model_node->SetScale(DirectX::XMVectorSet(scale[0], scale[1], scale[2], 1.f)); if (ImGui::Button("Take Camera Transform")) { model_node->SetPosition(scene_graph->GetActiveCamera()->m_position); model_node->SetRotation(scene_graph->GetActiveCamera()->m_rotation_radians); } // Material Settings if (ImGui::CollapsingHeader("Material Settings", ImGuiTreeNodeFlags_None)) { if (ImGui::Button("Add User-defined Material")) { materials.emplace_back(model->m_meshes[0].second); } for (std::size_t mat_i = 0; mat_i < materials.size(); mat_i++) { auto& material = materials[mat_i]; auto prev_material = material; ImGui::InputInt(("Remove##" + std::to_string(mat_i)).c_str(), reinterpret_cast(&material.m_id)); if (!material.m_pool->HasMaterial(material)) { material = prev_material; } } } model_node->SignalTransformChange(); model_node->SignalChange(); } }, { typeid(SkyboxNode), [](std::shared_ptr node, SceneGraph* scene_graph) { std::vector hdr_maps; std::string path = "resources/materials/"; for (const auto& entry : std::filesystem::directory_iterator(path)) { std::filesystem::path ext(entry.path().extension()); if (ext.compare(L".hdr") == 0) { hdr_maps.push_back(entry.path().filename().string()); } } //Lambda to get char* out of the string vector for ImGui static auto vector_getter = [](void* vec, int idx, const char** out_text) { auto& vector = *static_cast*>(vec); if (idx < 0 || idx >= static_cast(vector.size())) { return false; } *out_text = vector.at(idx).c_str(); return true; }; static int selected_item = 0; ImGui::ListBox("Environment Maps", &selected_item, vector_getter, static_cast(&hdr_maps), hdr_maps.size()); auto skybox_node = std::static_pointer_cast(node); if (ImGui::Button("Change Sky")) { path += hdr_maps[selected_item]; TextureHandle new_texture = skybox_node->m_skybox.value().m_pool->LoadFromFile(path, false, false); scene_graph->UpdateSkyboxNode(skybox_node, new_texture); } } }, }; decltype(SceneGraphEditorDetails::sg_editor_type_context_menu) SceneGraphEditorDetails::sg_editor_type_context_menu = { { typeid(MeshNode), [](std::shared_ptr node, SceneGraph* scene_graph) { auto mesh_node = std::static_pointer_cast(node); if (ImGui::Checkbox("Visibile", &mesh_node->m_visible)) { return true; // close popup. } return DefaultContextMenu(mesh_node, scene_graph); } }, { typeid(LightNode), [](std::shared_ptr node, SceneGraph* scene_graph) { auto light_node = std::static_pointer_cast(node); return DefaultContextMenu(light_node, scene_graph); } }, { typeid(SkyboxNode), [](std::shared_ptr node, SceneGraph* scene_graph) { auto sky_node = std::static_pointer_cast(node); return DefaultContextMenu(sky_node, scene_graph); } }, }; void SceneGraphEditor(SceneGraph* scene_graph) { if (open_scene_graph_editor) { ImGui::Begin("Scene Graph Editor", &open_scene_graph_editor); if (ImGui::Button("Add Light")) { scene_graph->CreateChild(nullptr, wr::LightType::POINT, DirectX::XMVECTOR{ 1, 1, 1 }); } auto root = scene_graph->GetRootNode(); static ImGuiTextFilter filter; ImGui::PushItemWidth(-1.f); filter.Draw("##"); ImVec2 size = ImGui::GetContentRegionAvail(); size.y -= ImGui::GetItemsLineHeightWithSpacing(); if (ImGui::ListBoxHeader("##", size)) { for (auto child_i = 0; child_i < root->m_children.size(); child_i++) { auto& node = root->m_children[child_i]; auto name_prefix = SceneGraphEditorDetails::GetNodeName(node).value_or("Node"); auto node_name = name_prefix + "##" + std::to_string(child_i); // Skip node if its not part of the filter. if (!filter.PassFilter(node_name.c_str())) continue; bool pressed = ImGui::Selectable(node_name.c_str(), selected_node == node); // if we don't have that node selected. if (pressed && selected_node != node) { selected_node = node; } // if we already have that node selected. else if (pressed && selected_node == node) { selected_node = nullptr; } // Right click menu if (ImGui::BeginPopupContextItem()) { auto right_clicked_node = root->m_children[child_i]; auto node_cm_function = SceneGraphEditorDetails::GetNodeContextMenuFunction(right_clicked_node).value_or(nullptr); bool close_popup = true; if (node_cm_function) { close_popup = node_cm_function(right_clicked_node, scene_graph); } else { close_popup = DefaultContextMenu(right_clicked_node, scene_graph); } if (close_popup) { ImGui::CloseCurrentPopup(); ImGui::EndPopup(); continue; } ImGui::EndPopup(); } } ImGui::ListBoxFooter(); } ImGui::End(); } } void Inspector(SceneGraph* scene_graph, ImVec2 viewport_pos, ImVec2 viewport_size) { if (open_inspector) { ImGui::Begin("Inspector", &open_inspector); if (selected_node) { auto node_inspect_function = SceneGraphEditorDetails::GetNodeInspectFunction(selected_node).value_or(nullptr); if (node_inspect_function) { node_inspect_function(selected_node, scene_graph); } else { ImGui::DragFloat3("Position", selected_node->m_position.m128_f32, 0.25f); float rot[3] = { DirectX::XMConvertToDegrees(DirectX::XMVectorGetX(selected_node->m_rotation_radians)), DirectX::XMConvertToDegrees(DirectX::XMVectorGetY(selected_node->m_rotation_radians)), DirectX::XMConvertToDegrees(DirectX::XMVectorGetZ(selected_node->m_rotation_radians)) }; ImGui::DragFloat3("Rotation", rot, 0.1f); selected_node->SetRotation(DirectX::XMVectorSet(DirectX::XMConvertToRadians(rot[0]), DirectX::XMConvertToRadians(rot[1]), DirectX::XMConvertToRadians(rot[2]), 0)); ImGui::DragFloat3("Scale", selected_node->m_scale.m128_f32, 0.01f); if (ImGui::Button("Take Camera Transform")) { selected_node->SetPosition(scene_graph->GetActiveCamera()->m_position); selected_node->SetRotation(scene_graph->GetActiveCamera()->m_rotation_radians); } selected_node->SignalChange(); selected_node->SignalTransformChange(); } internal::ManipulateNode(selected_node.get(), scene_graph, viewport_pos, viewport_size); } ImGui::End(); } } void ShaderRegistry() { if (open_shader_registry) { auto& registry = ShaderRegistry::Get(); ImGui::Begin("Shader Registry", &open_shader_registry); ImGui::Text("Num descriptors: %d", registry.m_descriptions.size()); ImGui::Text("Num objects: %d", registry.m_objects.size()); ImGui::Separator(); for (auto desc : registry.m_descriptions) { Shader* obj = nullptr; auto obj_it = registry.m_objects.find(desc.first); if (obj_it != registry.m_objects.end()) { obj = obj_it->second; } std::string tree_name = internal::ShaderTypeToStr(desc.second.type) + "[" + std::to_string(desc.first) + "]: " + desc.second.path; if (ImGui::TreeNode(tree_name.c_str())) { if (ImGui::TreeNode("Description")) { ImGui::Text("ID: %d", desc.first); ImGui::Text("Path: %s", desc.second.path.c_str()); ImGui::Text("Entry: %s", desc.second.entry.c_str()); ImGui::Text("Type: %s", internal::ShaderTypeToStr(desc.second.type).c_str()); ImGui::TreePop(); } internal::AddressText(obj); ImGui::TreePop(); } } ImGui::End(); } } void PipelineRegistry() { if (open_pipeline_registry) { auto& registry = PipelineRegistry::Get(); ImGui::Begin("Pipeline Registry", &open_pipeline_registry); ImGui::Text("Num descriptions: %d", registry.m_descriptions.size()); ImGui::Text("Num objects: %d", registry.m_objects.size()); ImGui::Separator(); for (auto desc : registry.m_descriptions) { Pipeline* obj = nullptr; auto obj_it = registry.m_objects.find(desc.first); if (obj_it != registry.m_objects.end()) { obj = obj_it->second; } std::string tree_name = "Pipeline[" + std::to_string(desc.first) + "]"; if (ImGui::TreeNode(tree_name.c_str())) { if (ImGui::TreeNode("Description")) { ImGui::Text("ID: %d", desc.first); auto text_handle = [](std::string handle_name, std::optional handle) { if (handle.has_value()) { std::string text = handle_name + " Handle: %d"; ImGui::Text(text.c_str(), handle); } }; text_handle("Vertex Shader", desc.second.m_vertex_shader_handle); text_handle("Pixel Shader", desc.second.m_pixel_shader_handle); text_handle("Compute Shader", desc.second.m_compute_shader_handle); text_handle("RootSignature", desc.second.m_root_signature_handle); ImGui::Text("Depth Enabled: %s", internal::BooltoStr(desc.second.m_depth_enabled).c_str()); ImGui::Text("Counter Clockwise Winding Order: %s", internal::BooltoStr(desc.second.m_counter_clockwise).c_str()); ImGui::Text("Num RTV Formats: %d", desc.second.m_num_rtv_formats); for (auto i = 0u; i < desc.second.m_num_rtv_formats; i++) { std::string text = "Format[" + std::to_string(i) + "]: %s"; ImGui::Text(text.c_str(), FormatToStr(desc.second.m_rtv_formats[i]).c_str()); } ImGui::TreePop(); } internal::AddressText(obj); ImGui::TreePop(); } } auto& rt_registry = RTPipelineRegistry::Get(); ImGui::Separator(); ImGui::Text("Num raytracing descriptions: %d", rt_registry.m_descriptions.size()); ImGui::Text("Num raytracing objects: %d", rt_registry.m_objects.size()); ImGui::Separator(); for (auto desc : rt_registry.m_descriptions) { StateObject* obj = nullptr; auto obj_it = rt_registry.m_objects.find(desc.first); if (obj_it != rt_registry.m_objects.end()) { obj = obj_it->second; } std::string tree_name = "Raytracing Pipeline[" + std::to_string(desc.first) + "]"; if (ImGui::TreeNode(tree_name.c_str())) { if (ImGui::TreeNode("Description")) { ImGui::Text("ID: %d", desc.first); auto text_handle = [](std::string handle_name, std::optional handle) { if (handle.has_value()) { std::string text = handle_name + " Handle: %d"; ImGui::Text(text.c_str(), handle); } }; text_handle("Library Shader", desc.second.library_desc.shader_handle); text_handle("Global RootSignature", desc.second.global_root_signature.value_or(-1)); for (auto i = 0; i < desc.second.local_root_signatures.size(); i++) { ImGui::Text("Local Root Signature [%d] = %d", i, desc.second.local_root_signatures[i]); } ImGui::TreePop(); } internal::AddressText(obj); ImGui::TreePop(); } } ImGui::End(); } } void RootSignatureRegistry() { if (open_root_signature_registry) { auto& registry = RootSignatureRegistry::Get(); ImGui::Begin("Root Signature Registry", &open_root_signature_registry); ImGui::Text("Num descriptors: %d", registry.m_descriptions.size()); ImGui::Text("Num objects: %d", registry.m_objects.size()); ImGui::Separator(); for (auto desc : registry.m_descriptions) { RootSignature* obj = nullptr; auto obj_it = registry.m_objects.find(desc.first); if (obj_it != registry.m_objects.end()) { obj = obj_it->second; } std::string tree_name = "Root Signature[" + std::to_string(desc.first) + "]"; if (ImGui::TreeNode(tree_name.c_str())) { if (ImGui::TreeNode("Description")) { ImGui::Text("ID: %d", desc.first); auto text_handle = [](std::string handle_name, std::optional handle) { if (handle.has_value()) { std::string text = handle_name + " Handle: %d"; ImGui::Text(text.c_str(), handle); } }; ImGui::Text("Num samplers: %d", desc.second.m_samplers.size()); ImGui::Text("Num parameters: %d", desc.second.m_parameters.size()); ImGui::TreePop(); } internal::AddressText(obj); ImGui::TreePop(); } } ImGui::End(); } } } /* wr::imgui::window */ namespace wr::imgui::special { DebugConsole::DebugConsole() { ClearLog(); memset(InputBuf, 0, sizeof(InputBuf)); HistoryPos = -1; AddCommand("help", [&](DebugConsole&, std::string const &) { AddLog("Commands:"); for (auto& cmd : m_commands) { if (cmd.m_starts_with) { AddLog("- %s[param] // %s", cmd.m_name.c_str(), cmd.m_description.c_str()); } else { AddLog("- %s // %s", cmd.m_name.c_str(), cmd.m_description.c_str()); } } }, "Shows all available commands." ); AddCommand("clear", [&](DebugConsole&, std::string const &) { ClearLog(); }, "Clears the console." ); AddCommand("cls", [&](DebugConsole&, std::string const &) { ClearLog(); }, "Clears the console." ); AddCommand("history", [&](DebugConsole&, std::string const &) { int first = History.Size - 10; for (int i = first > 0 ? first : 0; i < History.Size; i++) { AddLog("%3d: %s\n", i, History[i]); } }, "Shows command history." ); AddCommand("filter", [&](DebugConsole&, std::string const &) { filter = ImGuiTextFilter(nullptr); AddLog("Cleared Filter"); }, "Clears the filter.", false ); AddCommand("filter ", [&](DebugConsole&, std::string const & str) { auto params = str; params.erase(0, 7); filter = ImGuiTextFilter(params.c_str()); AddLog("Set filter to: %s", params.c_str()); }, "Sets the parameter as a filter for to console.", true ); } DebugConsole::~DebugConsole() { ClearLog(); for (int i = 0; i < History.Size; i++) free(History[i]); } void DebugConsole::EmptyInput() { memset(InputBuf, 0, sizeof(InputBuf)); } // Portable helpers static bool StrEquals(std::string const & a, std::string const & b) { std::string la(a); std::string lb(b); std::transform(la.begin(), la.end(), la.begin(), ::tolower); std::transform(lb.begin(), lb.end(), lb.begin(), ::tolower); return la == lb; } static bool StrStartsWith(std::string const & str, std::string const & other) { std::string lstr(str); std::transform(lstr.begin(), lstr.end(), lstr.begin(), ::tolower); return (lstr.find(other) == 0); } static int Stricmp(const char* str1, const char* str2) { int d; while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } return d; } static 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; } static char* Strdup(const char *str) { size_t len = strlen(str) + 1; void* buff = malloc(len); return (char*)memcpy(buff, (const void*)str, len); } static void Strtrim(char* str) { char* str_end = str + strlen(str); while (str_end > str && str_end[-1] == ' ') str_end--; *str_end = 0; } void DebugConsole::ClearLog() { for (int i = 0; i < Items.Size; i++) free(Items[i]); Items.clear(); ScrollToBottom = true; } void DebugConsole::AddLog(const char* fmt, ...) { // FIXME-OPT char buf[1024]; va_list args; va_start(args, fmt); vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); buf[IM_ARRAYSIZE(buf) - 1] = 0; va_end(args); Items.push_back(Strdup(buf)); ScrollToBottom = true; } void DebugConsole::Draw(const char* title, bool* p_open) { if (!*p_open) return; ImVec4 text_col(1, 1, 1, 1); ImVec4 bg_col(0.f, 0.f, 0.f, 0.5f); ImVec4 frame_col(0, 0, 0, 0); ImGuiContext& g = *ImGui::GetCurrentContext(); ImGuiViewport* viewport = g.Viewports[0]; ImGui::SetNextWindowSize(ImVec2(viewport->Size.x, 200), ImGuiCond_FirstUseEver); ImGui::SetNextWindowPos(ImVec2(0, 0)); ImGui::PushStyleColor(ImGuiCol_WindowBg, bg_col); ImGui::PushStyleColor(ImGuiCol_FrameBg, frame_col); if (!ImGui::Begin(title, p_open, ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize)) { ImGui::End(); return; } ImGui::SetWindowFocus(); // 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. // Here we create a context menu only available from the title bar. if (ImGui::BeginPopupContextItem()) { if (ImGui::MenuItem("Close Console")) *p_open = false; ImGui::EndPopup(); } const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); // 1 separator, 1 input text ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar); // Leave room for 1 separator + 1 InputText if (ImGui::BeginPopupContextWindow()) { if (ImGui::Selectable("Clear")) ClearLog(); ImGui::EndPopup(); } // 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()); // NB- if you have thousands of entries this approach may be too inefficient and may require user-side clipping to only process visible items. // 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. // To use the clipper we could replace the 'for (int i = 0; i < Items.Size; i++)' loop with: // ImGuiListClipper clipper(Items.Size); // while (clipper.Step()) // for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) // 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. // 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, // 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! // 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. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing for (int i = 0; i < Items.Size; i++) { const char* item = Items[i]; if (!filter.PassFilter(item)) continue; ImVec4 col = text_col; if (strstr(item, "[W]")) col = ImColor(1.f, 128.f / 255.f, 0.f, 1.0f); if (strstr(item, "[C]")) col = ImColor(1.0f, 0.4f, 0.4f, 1.0f); if (strstr(item, "[E]")) col = ImColor(1.0f, 0.4f, 0.4f, 1.0f); else if (strncmp(item, "# ", 2) == 0) col = ImColor(1.0f, 0.78f, 0.58f, 1.0f); ImGui::PushStyleColor(ImGuiCol_Text, col); ImGui::TextUnformatted(item); ImGui::PopStyleColor(); } if (ScrollToBottom) ImGui::SetScrollHereY(1.0f); ScrollToBottom = false; ImGui::PopStyleVar(); ImGui::EndChild(); ImGui::Separator(); // Command-line ImGui::SetKeyboardFocusHere(0); if (ImGui::InputText("", InputBuf, IM_ARRAYSIZE(InputBuf), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory, &TextEditCallbackStub, (void*)this)) { char* s = InputBuf; Strtrim(s); if (s[0]) ExecCommand(s); strcpy(s, ""); } ImGui::End(); ImGui::PopStyleColor(); ImGui::PopStyleColor(); } void DebugConsole::AddCommand(std::string name, Command::func_t func, std::string desc, bool starts_with) { m_commands.push_back({name, desc, func, starts_with}); } void DebugConsole::ExecCommand(const char* command_line) { AddLog("# %s\n", command_line); // 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. HistoryPos = -1; for (int i = History.Size - 1; i >= 0; i--) if (Stricmp(History[i], command_line) == 0) { free(History[i]); History.erase(History.begin() + i); break; } History.push_back(Strdup(command_line)); for (auto& cmd : m_commands) { if (!cmd.m_starts_with && StrEquals(command_line, cmd.m_name)) { cmd.m_function(*this, command_line); return; } else if (cmd.m_starts_with && StrStartsWith(command_line, cmd.m_name)) { cmd.m_function(*this, command_line); return; } } AddLog("Unknown command: '%s'\n", command_line); } int DebugConsole::TextEditCallbackStub(ImGuiInputTextCallbackData* data) // In C++11 you are better off using lambdas for this sort of forwarding callbacks { DebugConsole* console = (DebugConsole*)data->UserData; return console->TextEditCallback(data); } int DebugConsole::TextEditCallback(ImGuiInputTextCallbackData* data) { //AddLog("cursor: %d, selection: %d-%d", data->CursorPos, data->SelectionStart, data->SelectionEnd); switch (data->EventFlag) { case ImGuiInputTextFlags_CallbackCompletion: { // Example of TEXT COMPLETION // Locate beginning of current word const char* word_end = data->Buf + data->CursorPos; const char* word_start = word_end; while (word_start > data->Buf) { const char c = word_start[-1]; if (c == ' ' || c == '\t' || c == ',' || c == ';') break; word_start--; } // Build a list of candidates ImVector candidates; for (int i = 0; i < m_commands.size(); i++) if (Strnicmp(m_commands[i].m_name.c_str(), word_start, (int)(word_end - word_start)) == 0) candidates.push_back(m_commands[i].m_name.c_str()); if (candidates.Size == 0) { // No match AddLog("No match for \"%.*s\"!\n", (int)(word_end - word_start), word_start); } else if (candidates.Size == 1) { // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start)); data->InsertChars(data->CursorPos, candidates[0]); data->InsertChars(data->CursorPos, " "); } else { // Multiple matches. Complete as much as we can, so inputing "C" will complete to "CL" and display "CLEAR" and "CLASSIFY" int match_len = (int)(word_end - word_start); for (;;) { int c = 0; bool all_candidates_matches = true; for (int i = 0; i < candidates.Size && all_candidates_matches; i++) if (i == 0) c = toupper(candidates[i][match_len]); else if (c == 0 || c != toupper(candidates[i][match_len])) all_candidates_matches = false; if (!all_candidates_matches) break; match_len++; } if (match_len > 0) { data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start)); data->InsertChars(data->CursorPos, candidates[0], candidates[0] + match_len); } // List matches AddLog("Possible matches:\n"); for (int i = 0; i < candidates.Size; i++) AddLog("- %s\n", candidates[i]); } break; } case ImGuiInputTextFlags_CallbackHistory: { // Example of HISTORY const int prev_history_pos = HistoryPos; if (data->EventKey == ImGuiKey_UpArrow) { if (HistoryPos == -1) HistoryPos = History.Size - 1; else if (HistoryPos > 0) HistoryPos--; } else if (data->EventKey == ImGuiKey_DownArrow) { if (HistoryPos != -1) if (++HistoryPos >= History.Size) HistoryPos = -1; } // A better implementation would preserve the data on the current input line along with cursor position. if (prev_history_pos != HistoryPos) { const char* history_str = (HistoryPos >= 0) ? History[HistoryPos] : ""; data->DeleteChars(0, data->BufTextLen); data->InsertChars(0, history_str); } } } return 0; } } /* wr::imgui::special */ ================================================ FILE: src/imgui_tools.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include "imgui/imgui.hpp" #include "wisprenderer_export.hpp" #include "scene_graph/light_node.hpp" namespace wr { class D3D12RenderSystem; class SceneGraph; } namespace wr::imgui { namespace menu { void Registries(); } namespace window { struct Stats { void Draw(D3D12RenderSystem& render_system, ImVec2 viewport_pos); bool m_open; }; void ShaderRegistry(); void PipelineRegistry(); void RootSignatureRegistry(); void D3D12HardwareInfo(D3D12RenderSystem& render_system); void D3D12Settings(); void SceneGraphEditor(SceneGraph* scene_graph); void Inspector(SceneGraph* scene_graph, ImVec2 viewport_pos, ImVec2 viewport_size); static bool open_hardware_info = true; static bool open_d3d12_settings = true; static bool open_shader_registry = false; static bool open_shader_compiler_popup = false; static std::string shader_compiler_error = "No shader error"; static bool open_pipeline_registry = false; static bool open_root_signature_registry = false; static bool open_scene_graph_editor = true; static bool open_inspector = true; static wr::LightNode* selected_light = nullptr; static bool light_selected = false; static std::shared_ptr selected_node = nullptr; struct SceneGraphEditorDetails { using name_func_t = std::function)>; using inspect_func_t = std::function, SceneGraph*)>; using context_menu_func_t = std::function, SceneGraph*)>; WISPRENDERER_EXPORT static std::unordered_map sg_editor_type_names; WISPRENDERER_EXPORT static std::unordered_map sg_editor_type_inspect; WISPRENDERER_EXPORT static std::unordered_map sg_editor_type_context_menu; WISPRENDERER_EXPORT static std::optional GetNodeName(std::shared_ptr node) { if (auto it = SceneGraphEditorDetails::sg_editor_type_names.find(node->m_type_info); it != SceneGraphEditorDetails::sg_editor_type_names.end()) { return it->second(node); } return std::nullopt; } WISPRENDERER_EXPORT static std::optional GetNodeInspectFunction(std::shared_ptr node) { if (auto it = SceneGraphEditorDetails::sg_editor_type_inspect.find(node->m_type_info); it != SceneGraphEditorDetails::sg_editor_type_inspect.end()) { return it->second; } return std::nullopt; } WISPRENDERER_EXPORT static std::optional GetNodeContextMenuFunction(std::shared_ptr node) { if (auto it = SceneGraphEditorDetails::sg_editor_type_context_menu.find(node->m_type_info); it != SceneGraphEditorDetails::sg_editor_type_context_menu.end()) { return it->second; } return std::nullopt; } }; } namespace special { struct DebugConsole { struct Command { using func_t = std::function; std::string m_name; std::string m_description; func_t m_function; bool m_starts_with = false; }; char InputBuf[256]; ImVector Items; bool ScrollToBottom; ImVector History; int HistoryPos; // -1: new line, 0..History.Size-1 browsing history. std::vector m_commands; ImGuiTextFilter filter; DebugConsole(); ~DebugConsole(); void EmptyInput(); void ClearLog(); void AddLog(const char* fmt, ...); void Draw(const char* title, bool* p_open); void AddCommand(std::string name, Command::func_t func, std::string desc = "", bool starts_with = false); private: void ExecCommand(const char* command_line); static int TextEditCallbackStub(ImGuiInputTextCallbackData* data); int TextEditCallback(ImGuiInputTextCallbackData* data); }; } } /* wr::imgui */ ================================================ FILE: src/material_pool.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define NOMINMAX #include "resource_pool_texture.hpp" #include "material_pool.hpp" #include "util/log.hpp" #include namespace wr { ////////////////////////////// // MATERIAL FUNCTIONS // ////////////////////////////// Material::Material(TexturePool* pool): m_texture_pool(pool) { memset(&m_material_data, 0, sizeof(m_material_data)); SetConstant(1.0f); SetConstant(1.0f); SetConstant(1.0f); SetConstant(1.0f); SetConstant(1.0f); SetConstant(1.0f); SetConstant(1.0f); } Material::Material(TexturePool *pool, TextureHandle albedo, TextureHandle normal, TextureHandle roughness, TextureHandle metallic, TextureHandle emissive, TextureHandle ao, MaterialUVScales scales, bool alpha_masked, bool double_sided): Material(pool) { SetTexture(TextureType::ALBEDO, albedo); SetTexture(TextureType::NORMAL, normal); SetTexture(TextureType::ROUGHNESS, roughness); SetTexture(TextureType::METALLIC, metallic); SetTexture(TextureType::EMISSIVE, emissive); SetTexture(TextureType::AO, ao); m_material_data.albedo_scale = scales.m_albedo_scale; m_material_data.normal_scale = scales.m_normal_scale; m_material_data.roughness_scale = scales.m_roughness_scale; m_material_data.metallic_scale = scales.m_metallic_scale; m_material_data.emissive_scale = scales.m_emissive_scale; m_material_data.ao_scale = scales.m_ao_scale; SetConstant(scales.m_albedo_scale); SetConstant(scales.m_normal_scale); SetConstant(scales.m_roughness_scale); SetConstant(scales.m_metallic_scale); SetConstant(scales.m_emissive_scale); SetConstant(scales.m_ao_scale); SetConstant(alpha_masked); SetConstant(double_sided); SetConstant(1.0f); } TextureHandle Material::GetTexture(TextureType type) { return m_textures[size_t(type) % size_t(TextureType::COUNT)]; } void Material::SetTexture(TextureType type, TextureHandle handle) { if (handle.m_pool != m_texture_pool || !m_texture_pool) { ClearTexture(type); //LOGW("Textures in a material need to belong to the same texture pool"); } else { m_textures[size_t(type) % size_t(TextureType::COUNT)] = handle; m_material_data.m_material_flags.m_value = uint32_t(m_material_data.m_material_flags.m_value) | (1 << uint32_t(type)); } } void Material::ClearTexture(TextureType type) { m_textures[size_t(type) % size_t(TextureType::COUNT)] = { nullptr, 0 }; m_material_data.m_material_flags.m_value = uint32_t(m_material_data.m_material_flags.m_value) & (~(1 << uint32_t(type))); } bool Material::HasTexture(TextureType type) { return m_textures[size_t(type) % size_t(TextureType::COUNT)].m_pool; } Material::~Material() { m_constant_buffer_handle->m_pool->Destroy(m_constant_buffer_handle); } void Material::UpdateConstantBuffer() { m_constant_buffer_handle->m_pool->Update( m_constant_buffer_handle, sizeof(MaterialData), 0, reinterpret_cast(&m_material_data)); } ////////////////////////////// // MATERIAL POOL FUNCTIONS // ////////////////////////////// MaterialPool::MaterialPool() {} MaterialPool::~MaterialPool() { for(auto& m : m_materials) { delete m.second; } } MaterialHandle MaterialPool::Create(TexturePool* pool) { MaterialHandle handle; handle.m_pool = this; handle.m_id = m_id_factory.GetUnusedID(); Material* mat = new Material(pool); mat->SetConstantBufferHandle(m_constant_buffer_pool->Create(sizeof(Material::MaterialData))); m_materials.insert(std::make_pair(handle.m_id, mat)); return handle; } MaterialHandle MaterialPool::Create(TexturePool* pool, TextureHandle& albedo, TextureHandle& normal, TextureHandle& roughness, TextureHandle& metallic, TextureHandle& emissive, TextureHandle& ao, MaterialUVScales& mat_scales, bool is_alpha_masked, bool is_double_sided) { MaterialHandle handle = {}; handle.m_pool = this; handle.m_id = m_id_factory.GetUnusedID(); Material* mat = new Material(pool, albedo, normal, roughness, metallic, emissive, ao, mat_scales, is_alpha_masked, is_double_sided); mat->SetConstantBufferHandle(m_constant_buffer_pool->Create(sizeof(Material::MaterialData))); mat->UpdateConstantBuffer(); m_materials.insert(std::make_pair(handle.m_id, mat)); return handle; } Material* MaterialPool::GetMaterial(MaterialHandle handle) { // Return the material if available. if (auto it = m_materials.find(handle.m_id); it != m_materials.end()) { return it->second; } LOGE("Failed to obtain a material from pool."); return nullptr; } void MaterialPool::DestroyMaterial(MaterialHandle handle) { auto it = m_materials.find(handle.m_id); if (it == m_materials.end()) { return; //LOGC("Can't destroy material; it's not part of the material pool"); } auto *ptr = it->second; m_materials.erase(it); delete ptr; } bool MaterialPool::HasMaterial(MaterialHandle handle) const { return m_materials.find(handle.m_id) != m_materials.end(); } } /* wr */ ================================================ FILE: src/material_pool.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #include #include #include "structs.hpp" #include "util/defines.hpp" #include "util/log.hpp" #include "id_factory.hpp" #include "constant_buffer_pool.hpp" struct aiMaterial; namespace wr { enum class TextureType : size_t { ALBEDO = 0, NORMAL, ROUGHNESS, METALLIC, EMISSIVE, AO, COUNT }; //u32 type = { u16 offset (in floats), u16 isArray (0 = a constant, 1 = an array) } enum class MaterialConstant : uint32_t { COLOR = (0 << 16) | 1, R = (0 << 16) | 0, G = (1 << 16) | 0, B = (2 << 16) | 0, METALLIC = (3 << 16) | 0, ROUGHNESS = (4 << 16) | 0, EMISSIVE_MULTIPLIER = (5 << 16) | 0, IS_DOUBLE_SIDED = (6 << 16) | 0, IS_ALPHA_MASKED = (7 << 16) | 0, ALBEDO_UV_SCALE = (8 << 16) | 0, NORMAL_UV_SCALE = (9 << 16) | 0, ROUGHNESS_UV_SCALE = (10 << 16) | 0, METALLIC_UV_SCALE = (11 << 16) | 0, EMISSIVE_UV_SCALE = (12 << 16) | 0, AO_UV_SCALE = (13 << 16) | 0, MAX_OFFSET = 14 }; class Material { public: Material(TexturePool* pool); Material(TexturePool* pool, TextureHandle albedo, TextureHandle normal, TextureHandle roughness, TextureHandle metallic, TextureHandle emissive, TextureHandle ao, MaterialUVScales mat_scales, bool alpha_masked = false, bool double_sided = false); Material(const Material& rhs) = default; ~Material(); TextureHandle GetTexture(TextureType type); void SetTexture(TextureType type, TextureHandle handle); void ClearTexture(TextureType type); bool HasTexture(TextureType type); template typename std::enable_if::type GetConstant() { return m_material_data.m_constant_data[uint32_t(type) >> 16]; } template typename std::enable_if>::type GetConstant() { float* arr = m_material_data.m_constant_data; uint32_t i = uint32_t(type) >> 16; return { arr[i], arr[i + 1], arr[i + 2] }; } template void SetConstant(typename std::enable_if::type x) { m_material_data.m_constant_data[uint32_t(type) >> 16] = x; } template void SetConstant(const typename std::enable_if>::type& val) { float* arr = m_material_data.m_constant_data; uint32_t i = uint32_t(type) >> 16; memcpy(arr, val.data(), sizeof(val)); } TexturePool* const GetTexturePool() { return m_texture_pool; } ConstantBufferHandle* const GetConstantBufferHandle() const { return m_constant_buffer_handle; }; void SetConstantBufferHandle(ConstantBufferHandle* handle) { m_constant_buffer_handle = handle; }; void UpdateConstantBuffer(); union TextureFlags { struct { uint32_t m_has_albedo_texture : 1; uint32_t m_has_normal_texture : 1; uint32_t m_has_roughness_texture : 1; uint32_t m_has_metallic_texture : 1; uint32_t m_has_emissive_texture : 1; uint32_t m_has_ao_texture : 1; }; uint32_t m_value; }; union MaterialData { struct { float m_color[3]; float m_metallic; float m_roughness; float m_emissive_multiplier; float m_is_double_sided; float m_use_alpha_constant; float albedo_scale; float normal_scale; float roughness_scale; float metallic_scale; float emissive_scale; float ao_scale; float m_padding; TextureFlags m_material_flags; }; float m_constant_data[size_t(MaterialConstant::MAX_OFFSET)]{}; }; MaterialData GetMaterialData() const { return m_material_data; } protected: union { TextureHandle m_textures[size_t(TextureType::COUNT)]{}; struct { TextureHandle m_albedo; TextureHandle m_normal; TextureHandle m_roughness; TextureHandle m_metallic; TextureHandle m_emissive; TextureHandle m_ao; }; }; TexturePool* m_texture_pool = nullptr; ConstantBufferHandle* m_constant_buffer_handle; MaterialData m_material_data; }; class MaterialPool { public: explicit MaterialPool(); virtual ~MaterialPool(); MaterialPool(MaterialPool const &) = delete; MaterialPool& operator=(MaterialPool const &) = delete; MaterialPool(MaterialPool&&) = delete; MaterialPool& operator=(MaterialPool&&) = delete; //Creates an empty material. The user is responsible of filling in the texture handles. //TODO: Give Materials default textures [[nodiscard]] MaterialHandle Create(TexturePool* pool); [[nodiscard]] MaterialHandle Create(TexturePool* pool, TextureHandle& albedo, TextureHandle& normal, TextureHandle& roughness, TextureHandle& metallic, TextureHandle& emissive, TextureHandle& ao, MaterialUVScales& mat_scales, bool is_alpha_masked = false, bool is_double_sided = false); virtual void Evict() {} virtual void MakeResident() {} /*! Obtain a material from the pool. */ /*! Throws an error if no material was found. */ virtual Material* GetMaterial(MaterialHandle handle); /*! Check if the material owns a material with the specified handle */ bool HasMaterial(MaterialHandle handle) const; void DestroyMaterial(MaterialHandle handle); protected: std::shared_ptr m_constant_buffer_pool; std::unordered_map m_materials; IDFactory m_id_factory; }; } /* wr */ ================================================ FILE: src/model_loader.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "model_loader.hpp" namespace wr { WISPRENDERER_EXPORT std::vector ModelLoader::m_registered_model_loaders = std::vector(); ModelLoader::ModelLoader() { m_registered_model_loaders.push_back(this); } ModelLoader::~ModelLoader() { std::vector temp = m_loaded_models; for (ModelData* model : temp) { DeleteModel(model); } for (std::vector::iterator it = m_registered_model_loaders.begin(); it != m_registered_model_loaders.end(); ++it) { if ((*it) == this) { m_registered_model_loaders.erase(it); break; } } } ModelData * ModelLoader::Load(std::string_view model_path) { ModelData* model = LoadModel(model_path); m_loaded_models.push_back(model); return model; } ModelData * ModelLoader::Load(void * data, std::size_t length, std::string format) { ModelData* model = LoadModel(data, length, format); m_loaded_models.push_back(model); return model; } void ModelLoader::DeleteModel(ModelData * model) { std::vector::iterator it = std::find(m_loaded_models.begin(), m_loaded_models.end(), model); if (it == m_loaded_models.end()) { return; } m_loaded_models.erase(it); for (int i = 0; i < model->m_meshes.size(); ++i) { delete model->m_meshes[i]; } for (int i = 0; i < model->m_materials.size(); ++i) { delete model->m_materials[i]; } for (int i = 0; i < model->m_embedded_textures.size(); ++i) { delete model->m_embedded_textures[i]; } for (int i = 0; i < model->m_skeleton_data->m_animations.size(); ++i) { delete model->m_skeleton_data->m_animations[i]; } for (int i = 0; i < model->m_skeleton_data->m_bones.size(); ++i) { delete model->m_skeleton_data->m_bones[i]; } delete model->m_skeleton_data; delete model; } std::vector ModelLoader::SupportedModelFormats() { return m_supported_model_formats; } bool ModelLoader::SupportsModelFormat(const std::string_view& model_format) { for (int i = 0; i < m_supported_model_formats.size(); ++i) { if (m_supported_model_formats[i].compare(model_format) == 0) { return true; } } return false; } ModelLoader * ModelLoader::FindFittingModelLoader(const std::string_view & model_format) { for (int i = 0; i < m_registered_model_loaders.size(); ++i) { if (m_registered_model_loaders[i]->SupportsModelFormat(model_format)) { return m_registered_model_loaders[i]; } } return nullptr; } } ================================================ FILE: src/model_loader.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include "wisprenderer_export.hpp" namespace wr { struct ModelMeshData; struct ModelMaterialData; struct ModelData; struct ModelSkeletonData; struct ModelBoneData; struct ModelAnimationData; struct EmbeddedTexture; enum class TextureLocation { EXTERNAL = 0, EMBEDDED, NON_EXISTENT }; struct ModelData { std::vector m_meshes; std::vector m_materials; std::vector m_embedded_textures; ModelSkeletonData* m_skeleton_data; template size_t GetTotalVertexSize() { size_t size = 0; for (auto& mesh : m_meshes) { size += mesh->GetTotalVertexSize(); } return size; }; template size_t GetTotalIndexSize() { size_t size = 0; for (auto& mesh : m_meshes) { size += mesh->GetTotalVertexSize(); } return size; }; }; struct ModelMeshData { std::vector m_positions; std::vector m_colors; std::vector m_normals; std::vector m_uvw; std::vector m_tangents; std::vector m_bitangents; std::vector m_bone_weights; std::vector m_bone_ids; std::vector m_indices; template size_t GetTotalVertexSize() { return m_positions.size() * sizeof(TV); }; template size_t GetTotalIndexSize() { return m_indices.size() * sizeof(TI); }; int m_material_id; }; struct ModelMaterialData { std::string m_albedo_texture; std::size_t m_albedo_embedded_texture; TextureLocation m_albedo_texture_location; std::string m_metallic_texture; std::size_t m_metallic_embedded_texture; TextureLocation m_metallic_texture_location; std::string m_roughness_texture; std::size_t m_roughness_embedded_texture; TextureLocation m_roughness_texture_location; std::string m_ambient_occlusion_texture; std::size_t m_ambient_occlusion_embedded_texture; TextureLocation m_ambient_occlusion_texture_location; std::string m_normal_map_texture; std::size_t m_normal_map_embedded_texture; TextureLocation m_normal_map_texture_location; std::string m_emissive_texture; std::size_t m_emissive_embedded_texture; TextureLocation m_emissive_texture_location; float m_base_color[3]; float m_base_metallic; float m_base_roughness; float m_base_transparency; float m_base_emissive; bool m_two_sided; }; struct ModelBoneData { }; struct ModelAnimationData { }; struct ModelSkeletonData { std::vector m_bones; std::vector m_animations; }; struct EmbeddedTexture { std::vector m_data; std::uint32_t m_width; std::uint32_t m_height; bool m_compressed; std::string m_format; }; class ModelLoader { public: ModelLoader(); virtual ~ModelLoader(); [[nodiscard]] ModelData* Load(std::string_view model_path); [[nodiscard]] ModelData* Load(void* data, std::size_t length, std::string format); // Cleans up the allocated memory for the model data. // Call this after you've finished interprenting the data. // Destroying the loader works as well. void DeleteModel(ModelData* model); std::vector SupportedModelFormats(); bool SupportsModelFormat(const std::string_view& model_format); WISPRENDERER_EXPORT static ModelLoader* FindFittingModelLoader(const std::string_view& model_format); WISPRENDERER_EXPORT static std::vector m_registered_model_loaders; protected: virtual ModelData* LoadModel(std::string_view model_path) = 0; virtual ModelData* LoadModel(void* data, std::size_t length, std::string format) = 0; std::vector m_loaded_models; std::vector m_supported_model_formats; }; } /* wr */ ================================================ FILE: src/model_loader_assimp.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "model_loader_assimp.hpp" #include "util/log.hpp" namespace wr { AssimpModelLoader::AssimpModelLoader() { m_supported_model_formats = { "obj","fbx","raw","sib","smd" }; } AssimpModelLoader::~AssimpModelLoader() { } ModelData * AssimpModelLoader::LoadModel(std::string_view model_path) { Assimp::Importer importer; const aiScene* scene = importer.ReadFile(model_path.data(), aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_JoinIdenticalVertices | aiProcess_OptimizeMeshes | aiProcess_ImproveCacheLocality | aiProcess_MakeLeftHanded); if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { LOGW(std::string("Loading model ") + model_path.data() + std::string(" failed with error ") + importer.GetErrorString()); return nullptr; } ModelData* model = new ModelData; if (scene->HasTextures()) { LoadEmbeddedTextures(model, scene); } LoadMaterials(model, scene); LoadMeshes(model, scene, scene->mRootNode); model->m_skeleton_data = new ModelSkeletonData(); return model; } ModelData * AssimpModelLoader::LoadModel(void * data, std::size_t length, std::string format) { Assimp::Importer importer; const aiScene* scene = importer.ReadFileFromMemory(data, length, aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_JoinIdenticalVertices | aiProcess_OptimizeMeshes | aiProcess_ImproveCacheLocality | aiProcess_MakeLeftHanded, format.c_str()); if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { LOGW(std::string("Loading model ") + std::string("failed with error ") + importer.GetErrorString()); return nullptr; } ModelData* model = new ModelData; if (scene->HasTextures()) { LoadEmbeddedTextures(model, scene); } LoadMaterials(model, scene); LoadMeshes(model, scene, scene->mRootNode); model->m_skeleton_data = new ModelSkeletonData(); return model; } void AssimpModelLoader::LoadMeshes(ModelData * model, const aiScene * scene, aiNode * node) { for (unsigned int i = 0; i < node->mNumMeshes; ++i) { ModelMeshData* mesh_data = new ModelMeshData(); aiMesh* mesh = scene->mMeshes[node->mMeshes[i]]; //Copy data mesh_data->m_positions.resize(mesh->mNumVertices); mesh_data->m_normals.resize(mesh->mNumVertices); mesh_data->m_uvw.resize(mesh->mNumVertices); mesh_data->m_colors.resize(mesh->mNumVertices); mesh_data->m_tangents.resize(mesh->mNumVertices); mesh_data->m_bitangents.resize(mesh->mNumVertices); mesh_data->m_bone_weights.resize(mesh->mNumVertices); mesh_data->m_bone_ids.resize(mesh->mNumVertices); #ifdef ASSIMP_DOUBLE_PRECISION static_assert(false, "Assimp should use single precision, since model_loader_assimp.cpp requires floats"); #endif const size_t float3 = sizeof(float) * 3, float3s = mesh->mNumVertices * float3; memcpy(mesh_data->m_positions.data(), mesh->mVertices, float3s); if (mesh->HasNormals()) { memcpy(mesh_data->m_normals.data(), mesh->mNormals, float3s); } if (mesh->HasTangentsAndBitangents()) { memcpy(mesh_data->m_tangents.data(), mesh->mTangents, float3s); memcpy(mesh_data->m_bitangents.data(), mesh->mBitangents, float3s); } if (mesh->HasTextureCoords(0)) { memcpy(mesh_data->m_uvw.data(), mesh->mTextureCoords[0], float3s); } if (mesh->HasVertexColors(0)) { for (unsigned int j = 0; j < mesh->mNumVertices; ++j) { memcpy(&mesh_data->m_colors[j], &mesh->mColors[0][j], float3); } } //Copy indices size_t count = 0; for (size_t j = 0; j < mesh->mNumFaces; ++j) { aiFace *face = &mesh->mFaces[j]; count += face->mNumIndices; } mesh_data->m_indices.resize(count); count = 0; for (size_t j = 0; j < mesh->mNumFaces; ++j) { aiFace *face = &mesh->mFaces[j]; memcpy(mesh_data->m_indices.data() + count, face->mIndices, face->mNumIndices * 4); count += face->mNumIndices; } mesh_data->m_material_id = mesh->mMaterialIndex; model->m_meshes.push_back(mesh_data); } for (unsigned int i = 0; i < node->mNumChildren; ++i) { LoadMeshes(model, scene, node->mChildren[i]); } } void AssimpModelLoader::LoadMaterials(ModelData * model, const aiScene * scene) { model->m_materials.resize(scene->mNumMaterials); for (unsigned int i = 0; i < scene->mNumMaterials; ++i) { aiMaterial* material = scene->mMaterials[i]; ModelMaterialData* material_data = new ModelMaterialData; if (material->GetTextureCount(aiTextureType_DIFFUSE) > 0) { aiString path; material->GetTexture(aiTextureType_DIFFUSE, 0, &path); if (path.data[0] == '*') { uint32_t index = atoi(path.C_Str() + 1); material_data->m_albedo_embedded_texture = index; material_data->m_albedo_texture_location = TextureLocation::EMBEDDED; } else { material_data->m_albedo_texture = std::string(path.C_Str()); material_data->m_albedo_texture_location = TextureLocation::EXTERNAL; } } else { material_data->m_albedo_texture_location = TextureLocation::NON_EXISTENT; } if (material->GetTextureCount(aiTextureType_SPECULAR) > 0) { aiString path; material->GetTexture(aiTextureType_SPECULAR, 0, &path); if (path.data[0] == '*') { uint32_t index = atoi(path.C_Str() + 1); material_data->m_metallic_embedded_texture = index; material_data->m_metallic_texture_location = TextureLocation::EMBEDDED; } else { material_data->m_metallic_texture = std::string(path.C_Str()); material_data->m_metallic_texture_location = TextureLocation::EXTERNAL; } } else { material_data->m_metallic_texture_location = TextureLocation::NON_EXISTENT; } if (material->GetTextureCount(aiTextureType_SHININESS) > 0) { aiString path; material->GetTexture(aiTextureType_SHININESS, 0, &path); if (path.data[0] == '*') { uint32_t index = atoi(path.C_Str() + 1); material_data->m_roughness_embedded_texture = index; material_data->m_roughness_texture_location = TextureLocation::EMBEDDED; } else { material_data->m_roughness_texture = std::string(path.C_Str()); material_data->m_roughness_texture_location = TextureLocation::EXTERNAL; } } else { material_data->m_roughness_texture_location = TextureLocation::NON_EXISTENT; } if (material->GetTextureCount(aiTextureType_AMBIENT) > 0) { aiString path; material->GetTexture(aiTextureType_AMBIENT, 0, &path); if (path.data[0] == '*') { uint32_t index = atoi(path.C_Str() + 1); material_data->m_ambient_occlusion_embedded_texture = index; material_data->m_ambient_occlusion_texture_location = TextureLocation::EMBEDDED; } else { material_data->m_ambient_occlusion_texture = std::string(path.C_Str()); material_data->m_ambient_occlusion_texture_location = TextureLocation::EXTERNAL; } } else { material_data->m_ambient_occlusion_texture_location = TextureLocation::NON_EXISTENT; } if (material->GetTextureCount(aiTextureType_NORMALS) > 0) { aiString path; material->GetTexture(aiTextureType_NORMALS, 0, &path); if (path.data[0] == '*') { uint32_t index = atoi(path.C_Str() + 1); material_data->m_normal_map_embedded_texture = index; material_data->m_normal_map_texture_location = TextureLocation::EMBEDDED; } else { material_data->m_normal_map_texture = std::string(path.C_Str()); material_data->m_normal_map_texture_location = TextureLocation::EXTERNAL; } } else { material_data->m_normal_map_texture_location = TextureLocation::NON_EXISTENT; } if (material->GetTextureCount(aiTextureType_EMISSIVE) > 0) { aiString path; material->GetTexture(aiTextureType_EMISSIVE, 0, &path); if (path.data[0] == '*') { uint32_t index = atoi(path.C_Str() + 1); material_data->m_emissive_embedded_texture = index; material_data->m_emissive_texture_location = TextureLocation::EMBEDDED; material_data->m_base_emissive = 1; } else { material_data->m_emissive_texture = std::string(path.C_Str()); material_data->m_emissive_texture_location = TextureLocation::EXTERNAL; material_data->m_base_emissive = 1; } } else { material_data->m_emissive_texture_location = TextureLocation::NON_EXISTENT; material_data->m_base_emissive = 0; } aiColor3D color; material->Get(AI_MATKEY_COLOR_DIFFUSE, color); memcpy(&material_data->m_base_color, &color, sizeof(color)); material->Get(AI_MATKEY_COLOR_SPECULAR, color); memcpy(&material_data->m_base_metallic, &color, sizeof(color)); float roughness; material->Get(AI_MATKEY_SHININESS, roughness); material_data->m_base_roughness = roughness; float opacity; material->Get(AI_MATKEY_OPACITY, opacity); material_data->m_base_transparency = opacity; int two_sided; material->Get(AI_MATKEY_TWOSIDED, two_sided); material_data->m_two_sided = two_sided > 0; model->m_materials[i] = material_data; } } void AssimpModelLoader::LoadEmbeddedTextures(ModelData * model, const aiScene * scene) { model->m_embedded_textures.resize(scene->mNumTextures); for (unsigned int i = 0; i < scene->mNumTextures; ++i) { EmbeddedTexture* texture = new EmbeddedTexture; texture->m_compressed = scene->mTextures[i]->mHeight == 0 ? true : false; texture->m_format = std::string(scene->mTextures[i]->achFormatHint); texture->m_width = scene->mTextures[i]->mWidth; texture->m_height = scene->mTextures[i]->mHeight; if (texture->m_compressed) { texture->m_data.resize(texture->m_width); memcpy(texture->m_data.data(), scene->mTextures[i]->pcData, texture->m_width); } else { texture->m_data.resize(texture->m_width*texture->m_height * 4); for (unsigned int j = 0; j < texture->m_width*texture->m_height; ++j) { texture->m_data[j * 4 + 0] = scene->mTextures[i]->pcData[j].r; texture->m_data[j * 4 + 1] = scene->mTextures[i]->pcData[j].g; texture->m_data[j * 4 + 2] = scene->mTextures[i]->pcData[j].b; texture->m_data[j * 4 + 3] = scene->mTextures[i]->pcData[j].a; } } model->m_embedded_textures[i] = texture; } } } ================================================ FILE: src/model_loader_assimp.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "model_loader.hpp" #undef min #undef max #include #include #include namespace wr { class AssimpModelLoader : public ModelLoader { public: AssimpModelLoader(); ~AssimpModelLoader() final; protected: ModelData* LoadModel(std::string_view model_path) final; ModelData* LoadModel(void* data, std::size_t length, std::string format) final; private: void LoadMeshes(ModelData* model, const aiScene* scene, aiNode* node); void LoadMaterials(ModelData* model, const aiScene* scene); void LoadEmbeddedTextures(ModelData* model, const aiScene* scene); }; } ================================================ FILE: src/model_loader_tinygltf.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "model_loader_tinygltf.hpp" #include "util/log.hpp" #define TINYGLTF_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION #include namespace wr { TinyGLTFModelLoader::TinyGLTFModelLoader() { m_supported_model_formats = { "gltf" }; } TinyGLTFModelLoader::~TinyGLTFModelLoader() { } std::pair, std::vector> ComputeTangents(ModelMeshData* mesh_data) { size_t vtxCount = mesh_data->m_positions.size(); std::vector tanA(vtxCount, { 0, 0, 0 }); std::vector tanB(vtxCount, { 0, 0, 0 }); if (mesh_data->m_uvw.empty()) { return { tanA, tanB }; } // (1) size_t indexCount = mesh_data->m_indices.size(); for (size_t i = 0; i < mesh_data->m_indices.size(); i += 3) { size_t i0 = mesh_data->m_indices[i]; size_t i1 = mesh_data->m_indices[i + 1]; size_t i2 = mesh_data->m_indices[i + 2]; auto pos0 = DirectX::XMLoadFloat3(&mesh_data->m_positions[i0]); auto pos1 = DirectX::XMLoadFloat3(&mesh_data->m_positions[i1]); auto pos2 = DirectX::XMLoadFloat3(&mesh_data->m_positions[i2]); auto tex0 = DirectX::XMLoadFloat2(&DirectX::XMFLOAT2{ mesh_data->m_uvw[i0].x, mesh_data->m_uvw[i0].y }); auto tex1 = DirectX::XMLoadFloat2(&DirectX::XMFLOAT2{ mesh_data->m_uvw[i1].x, mesh_data->m_uvw[i1].y }); auto tex2 = DirectX::XMLoadFloat2(&DirectX::XMFLOAT2{ mesh_data->m_uvw[i2].x, mesh_data->m_uvw[i2].y }); DirectX::XMFLOAT3 edge1, edge2; DirectX::XMStoreFloat3(&edge1, DirectX::XMVectorSubtract(pos1, pos0)); DirectX::XMStoreFloat3(&edge2, DirectX::XMVectorSubtract(pos2, pos0)); DirectX::XMFLOAT2 uv1, uv2; DirectX::XMStoreFloat2(&uv1, DirectX::XMVectorSubtract(tex1, tex0)); DirectX::XMStoreFloat2(&uv2, DirectX::XMVectorSubtract(tex2, tex0)); float r = 1.0f / (uv1.x * uv2.y - uv1.y * uv2.x); DirectX::XMFLOAT3 tangent( ((edge1.x * uv2.y) - (edge2.x * uv1.y)) * r, ((edge1.y * uv2.y) - (edge2.y * uv1.y)) * r, ((edge1.z * uv2.y) - (edge2.z * uv1.y)) * r ); DirectX::XMFLOAT3 bitangent( ((edge1.x * uv2.x) - (edge2.x * uv1.x)) * r, ((edge1.y * uv2.x) - (edge2.y * uv1.x)) * r, ((edge1.z * uv2.x) - (edge2.z * uv1.x)) * r ); tanA[i0] = tangent; tanA[i1] = tangent; tanA[i2] = tangent; tanB[i0] = bitangent; tanB[i1] = bitangent; tanB[i2] = bitangent; } return { tanA, tanB }; } inline void LoadMaterial(ModelData* model, tinygltf::Model tg_model, tinygltf::Material mat) { ModelMaterialData* mat_data = new ModelMaterialData(); mat_data->m_albedo_texture_location = TextureLocation::NON_EXISTENT; mat_data->m_normal_map_texture_location = TextureLocation::NON_EXISTENT; mat_data->m_roughness_texture_location = TextureLocation::NON_EXISTENT; mat_data->m_metallic_texture_location = TextureLocation::NON_EXISTENT; mat_data->m_emissive_texture_location = TextureLocation::NON_EXISTENT; mat_data->m_ambient_occlusion_texture_location = TextureLocation::NON_EXISTENT; for (auto value : mat.values) { if (value.first == "baseColorTexture") { auto img = tg_model.images[value.second.json_double_value.begin()->second]; mat_data->m_albedo_texture_location = TextureLocation::EXTERNAL; mat_data->m_albedo_texture = img.uri; } else if (value.first == "metallicRoughnessTexture") { auto img = tg_model.images[value.second.json_double_value.begin()->second]; mat_data->m_metallic_texture_location = TextureLocation::EXTERNAL; mat_data->m_metallic_texture = img.uri; mat_data->m_roughness_texture_location = TextureLocation::EXTERNAL; mat_data->m_roughness_texture = img.uri; } else if (value.first == "roughnessFactor") { auto factor = value.second.Factor(); mat_data->m_base_roughness = factor; } else if (value.first == "metallicFactor") { auto factor = value.second.Factor(); mat_data->m_base_metallic = factor; } } for (auto value : mat.additionalValues) { if (value.first == "normalTexture") { auto img = tg_model.images[value.second.json_double_value.begin()->second]; mat_data->m_normal_map_texture_location = TextureLocation::EXTERNAL; mat_data->m_normal_map_texture = img.uri; } else if (value.first == "occlusionTexture") { auto img = tg_model.images[value.second.json_double_value.begin()->second]; mat_data->m_ambient_occlusion_texture_location = TextureLocation::EXTERNAL; mat_data->m_ambient_occlusion_texture = img.uri; } else if (value.first == "emissiveTexture") { auto img = tg_model.images[value.second.json_double_value.begin()->second]; mat_data->m_emissive_texture_location = TextureLocation::EXTERNAL; mat_data->m_emissive_texture = img.uri; } else if (value.first == "emissiveFactor") { auto factor = value.second.ColorFactor(); mat_data->m_base_emissive = factor[0] + factor[1] + factor[2]; } } model->m_materials.push_back(mat_data); } inline void LoadMesh(ModelData* model, tinygltf::Model tg_model, tinygltf::Node node, DirectX::XMMATRIX parent_transform) { auto mesh = tg_model.meshes[node.mesh]; auto translation = node.translation; auto rotation = node.rotation; auto scale = node.scale; auto matrix = node.matrix; for (auto primitive : mesh.primitives) { std::size_t idx_buffer_offset = 0; std::size_t uv_buffer_offset = 0; std::size_t position_buffer_offset = 0; std::size_t normal_buffer_offset = 0; ModelMeshData* mesh_data = new ModelMeshData(); { // GET INDICES auto idx_accessor = tg_model.accessors[primitive.indices]; const auto& buffer_view = tg_model.bufferViews[idx_accessor.bufferView]; const auto& buffer = tg_model.buffers[buffer_view.buffer]; const auto data_address = buffer.data.data() + buffer_view.byteOffset + idx_accessor.byteOffset; const auto byte_stride = idx_accessor.ByteStride(buffer_view); mesh_data->m_indices.resize(idx_buffer_offset + idx_accessor.count); memcpy(mesh_data->m_indices.data() + idx_buffer_offset, data_address, idx_accessor.count * byte_stride); idx_buffer_offset += idx_accessor.count * byte_stride; } // Get other attributes for (const auto& attrib : primitive.attributes) { const auto attrib_accessor = tg_model.accessors[attrib.second]; const auto& buffer_view = tg_model.bufferViews[attrib_accessor.bufferView]; const auto& buffer = tg_model.buffers[buffer_view.buffer]; const auto data_address = buffer.data.data() + buffer_view.byteOffset + attrib_accessor.byteOffset; const auto byte_stride = attrib_accessor.ByteStride(buffer_view); const auto count = attrib_accessor.count; if (attrib.first == "POSITION") { mesh_data->m_positions.resize(position_buffer_offset + count); mesh_data->m_colors.resize(position_buffer_offset + count); memcpy(mesh_data->m_positions.data() + position_buffer_offset, data_address, count * byte_stride); position_buffer_offset += count * byte_stride; } else if (attrib.first == "NORMAL") { mesh_data->m_normals.resize(normal_buffer_offset + count); memcpy(mesh_data->m_normals.data() + normal_buffer_offset, data_address, count * byte_stride); normal_buffer_offset += count * byte_stride; } else if (attrib.first == "TEXCOORD_0") { std::vector f2_data; f2_data.resize(count); memcpy(f2_data.data(), data_address, count * byte_stride); for (auto& f2 : f2_data) { mesh_data->m_uvw.push_back({ f2.x, f2.y*-1, 0 }); } uv_buffer_offset += count * sizeof(DirectX::XMFLOAT3); } } // Apply Transformation for (auto& position : mesh_data->m_positions) { auto transformed_pos = DirectX::XMVector3Transform(DirectX::XMLoadFloat3(&position), parent_transform); DirectX::XMStoreFloat3(&position, transformed_pos); } auto tangent_bitangent = ComputeTangents(mesh_data); mesh_data->m_tangents = tangent_bitangent.first; mesh_data->m_bitangents = tangent_bitangent.second; mesh_data->m_uvw.resize(mesh_data->m_positions.size()); mesh_data->m_material_id = primitive.material; model->m_meshes.push_back(mesh_data); } } ModelData* TinyGLTFModelLoader::LoadModel(std::string_view model_path) { tinygltf::Model tg_model; tinygltf::TinyGLTF loader; std::string err; std::string warn; std::string path = std::string(model_path); if (!loader.LoadASCIIFromFile(&tg_model, &err, &warn, path)) { LOGC("TinyGLTF Parsing Failed"); } if (!warn.empty()) { LOGW("TinyGLTF Warning: {}", warn); } if (!err.empty()) { LOGE("TinyGLTF Error: {}", err); } auto model = new ModelData(); model->m_skeleton_data = new wr::ModelSkeletonData(); for (auto mat : tg_model.materials) { LoadMaterial(model, tg_model, mat); } std::function recursive_func = [&](int node_id, DirectX::XMMATRIX parent_transform) { auto node = tg_model.nodes[node_id]; auto translation = node.translation; auto scale = node.scale; auto rotation = node.rotation; auto matrix = node.matrix; DirectX::XMMATRIX transform; if (matrix.empty()) { DirectX::XMMATRIX translation_mat = translation.empty() ? DirectX::XMMatrixIdentity() : DirectX::XMMatrixTranslationFromVector({ (float)translation[0], (float)translation[1], (float)translation[2] }); DirectX::XMMATRIX rotation_mat = rotation.empty() ? DirectX::XMMatrixIdentity() : DirectX::XMMatrixRotationQuaternion({ (float)rotation[0], (float)rotation[1], (float)rotation[2], (float)rotation[3] }); DirectX::XMMATRIX scale_mat = scale.empty() ? DirectX::XMMatrixIdentity() : DirectX::XMMatrixScalingFromVector({ (float)scale[0], (float)scale[1], (float)scale[2] }); transform = scale_mat * rotation_mat * translation_mat; } else { transform.r[0].m128_f32[0] = matrix[0]; transform.r[0].m128_f32[1] = matrix[1]; transform.r[0].m128_f32[2] = matrix[2]; transform.r[0].m128_f32[3] = matrix[3]; transform.r[1].m128_f32[0] = matrix[4]; transform.r[1].m128_f32[1] = matrix[5]; transform.r[1].m128_f32[2] = matrix[6]; transform.r[1].m128_f32[3] = matrix[7]; transform.r[2].m128_f32[0] = matrix[8]; transform.r[2].m128_f32[1] = matrix[9]; transform.r[2].m128_f32[2] = matrix[10]; transform.r[2].m128_f32[3] = matrix[11]; transform.r[3].m128_f32[0] = matrix[12]; transform.r[3].m128_f32[1] = matrix[13]; transform.r[3].m128_f32[2] = matrix[14]; transform.r[3].m128_f32[3] = matrix[15]; } parent_transform = parent_transform * transform; if (node.mesh > -1) { LoadMesh(model, tg_model, node, parent_transform); } for (auto child_id : node.children) { recursive_func(child_id, parent_transform); } }; DirectX::XMMATRIX parent_transform = DirectX::XMMatrixIdentity(); for (auto node_id : tg_model.scenes[tg_model.defaultScene].nodes) { recursive_func(node_id, parent_transform); } return model; } ModelData* TinyGLTFModelLoader::LoadModel(void* data, std::size_t length, std::string format) { return new ModelData(); } } ================================================ FILE: src/model_loader_tinygltf.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "model_loader.hpp" namespace wr { class TinyGLTFModelLoader : public ModelLoader { public: TinyGLTFModelLoader(); ~TinyGLTFModelLoader() final; protected: ModelData* LoadModel(std::string_view model_path) final; ModelData* LoadModel(void* data, std::size_t length, std::string format) final; private: //void LoadMeshes(ModelData* model, tinygltf::Model tg_model, tinygltf::Node node); //void LoadMaterials(ModelData* model, const aiScene* scene); //void LoadEmbeddedTextures(ModelData* model, const aiScene* scene); }; } ================================================ FILE: src/model_pool.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "model_pool.hpp" #include namespace wr { void Model::Expand(float(&pos)[3]) { m_box.Expand(pos); } ModelPool::ModelPool(std::size_t vertex_buffer_pool_size_in_bytes, std::size_t index_buffer_pool_size_in_bytes) : m_vertex_buffer_pool_size_in_bytes(vertex_buffer_pool_size_in_bytes), m_index_buffer_pool_size_in_bytes(index_buffer_pool_size_in_bytes), m_current_id(0) { } void ModelPool::Destroy(Model * model) { DestroyModel(model); } void ModelPool::Destroy(internal::MeshInternal * mesh) { DestroyMesh(mesh); } template<> int ModelPool::LoadNodeMeshes(ModelData* data, Model* model, MaterialHandle default_material) { model->m_meshes.reserve(data->m_meshes.size()); for (unsigned int i = 0; i < data->m_meshes.size(); ++i) { ModelMeshData* mesh = data->m_meshes[i]; std::vector vertices(mesh->m_positions.size()); std::vector indices(mesh->m_indices.size()); Mesh* mesh_handle = new Mesh(); for (unsigned int j = 0; j < mesh->m_positions.size(); ++j) { Vertex &vertex = vertices[j]; memcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos)); memcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal)); memcpy(vertex.m_tangent, &mesh->m_tangents[j], sizeof(vertex.m_tangent)); memcpy(vertex.m_bitangent, &mesh->m_bitangents[j], sizeof(vertex.m_bitangent)); memcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv)); model->Expand(vertex.m_pos); } memcpy(indices.data(), mesh->m_indices.data(), mesh->m_indices.size() * 4); internal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices( vertices.data(), vertices.size(), sizeof(Vertex), indices.data(), indices.size(), sizeof(std::uint32_t)); if (mesh_data == nullptr) { for (auto &elem : model->m_meshes) delete elem.first; delete model; return 1; } std::uint64_t id = GetNewID(); m_loaded_meshes[id] = mesh_data; mesh_handle->id = id; MaterialHandle material_handle = default_material; std::pair n_mesh = std::make_pair( mesh_handle, material_handle); model->m_meshes.push_back(n_mesh); } return 0; } template<> int ModelPool::LoadNodeMeshes(ModelData* data, Model* model, MaterialHandle default_material) { model->m_meshes.reserve(data->m_meshes.size()); for (unsigned int i = 0; i < data->m_meshes.size(); ++i) { ModelMeshData* mesh = data->m_meshes[i]; std::vector vertices(mesh->m_positions.size()); std::vector indices(mesh->m_indices.size()); Mesh* mesh_handle = new Mesh(); for (unsigned int j = 0; j < mesh->m_positions.size(); ++j) { VertexColor &vertex = vertices[j]; memcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos)); memcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal)); memcpy(vertex.m_tangent, &mesh->m_tangents[j], sizeof(vertex.m_tangent)); memcpy(vertex.m_bitangent, &mesh->m_bitangents[j], sizeof(vertex.m_bitangent)); memcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv)); memcpy(vertex.m_color, &mesh->m_colors[j], sizeof(vertex.m_color)); model->Expand(vertex.m_pos); } memcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size()); internal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices( vertices.data(), vertices.size(), sizeof(VertexColor), indices.data(), indices.size(), sizeof(std::uint32_t)); if (mesh_data == nullptr) { for (auto &elem : model->m_meshes) delete elem.first; delete model; return 1; } std::uint64_t id = GetNewID(); m_loaded_meshes[id] = mesh_data; mesh_handle->id = id; MaterialHandle material_handle = default_material; std::pair n_mesh = std::make_pair( mesh_handle, material_handle); model->m_meshes.push_back(n_mesh); } return 0; } template<> int ModelPool::LoadNodeMeshes(ModelData* data, Model* model, MaterialHandle default_material) { model->m_meshes.reserve(data->m_meshes.size()); for (unsigned int i = 0; i < data->m_meshes.size(); ++i) { ModelMeshData* mesh = data->m_meshes[i]; std::vector vertices(mesh->m_positions.size()); std::vector indices(mesh->m_indices.size()); Mesh* mesh_handle = new Mesh(); for (unsigned int j = 0; j < mesh->m_positions.size(); ++j) { VertexNoTangent &vertex = vertices[j]; memcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos)); memcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal)); memcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv)); model->Expand(vertex.m_pos); } memcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size()); internal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices( vertices.data(), vertices.size(), sizeof(VertexNoTangent), indices.data(), indices.size(), sizeof(std::uint32_t)); if (mesh_data == nullptr) { for (auto &elem : model->m_meshes) delete elem.first; delete model; return 1; } std::uint64_t id = GetNewID(); m_loaded_meshes[id] = mesh_data; mesh_handle->id = id; MaterialHandle material_handle = default_material; std::pair n_mesh = std::make_pair( mesh_handle, material_handle); model->m_meshes.push_back(n_mesh); } return 0; } template<> int ModelPool::LoadNodeMeshesWithMaterials(ModelData* data, Model * model, std::vector materials) { model->m_meshes.reserve(data->m_meshes.size()); for (unsigned int i = 0; i < data->m_meshes.size(); ++i) { ModelMeshData* mesh = data->m_meshes[i]; std::vector vertices(mesh->m_positions.size()); std::vector indices(mesh->m_indices.size()); Mesh* mesh_handle = new Mesh(); for (unsigned int j = 0; j < mesh->m_positions.size(); ++j) { Vertex &vertex = vertices[j]; memcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos)); memcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal)); memcpy(vertex.m_tangent, &mesh->m_tangents[j], sizeof(vertex.m_tangent)); memcpy(vertex.m_bitangent, &mesh->m_bitangents[j], sizeof(vertex.m_bitangent)); memcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv)); model->Expand(vertex.m_pos); } memcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size()); internal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices( vertices.data(), vertices.size(), sizeof(Vertex), indices.data(), indices.size(), sizeof(std::uint32_t)); if (mesh_data == nullptr) { for (auto &elem : model->m_meshes) delete elem.first; delete model; return 1; } std::uint64_t id = GetNewID(); m_loaded_meshes[id] = mesh_data; mesh_handle->id = id; MaterialHandle material_handle = materials[mesh->m_material_id]; std::pair n_mesh = std::make_pair( mesh_handle, material_handle); model->m_meshes.push_back(n_mesh); } return 0; } template<> void ModelPool::UpdateModelBoundingBoxes(Model * model, std::vector vertices_data) { for (int i = 0; i < vertices_data.size(); ++i) { model->Expand(vertices_data[i].m_pos); } } template<> void ModelPool::UpdateModelBoundingBoxes(Model * model, std::vector vertices_data) { for (int i = 0; i < vertices_data.size(); ++i) { model->Expand(vertices_data[i].m_pos); } } template<> void ModelPool::UpdateModelBoundingBoxes(Model * model, std::vector vertices_data) { for (int i = 0; i < vertices_data.size(); ++i) { model->Expand(vertices_data[i].m_pos); } } template<> int ModelPool::LoadNodeMeshesWithMaterials(ModelData* data, Model * model, std::vector materials) { model->m_meshes.reserve(data->m_meshes.size()); for (unsigned int i = 0; i < data->m_meshes.size(); ++i) { ModelMeshData* mesh = data->m_meshes[i]; std::vector vertices(mesh->m_positions.size()); std::vector indices(mesh->m_indices.size()); Mesh* mesh_handle = new Mesh(); for (unsigned int j = 0; j < mesh->m_positions.size(); ++j) { VertexColor &vertex = vertices[j]; memcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos)); memcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal)); memcpy(vertex.m_tangent, &mesh->m_tangents[j], sizeof(vertex.m_tangent)); memcpy(vertex.m_bitangent, &mesh->m_bitangents[j], sizeof(vertex.m_bitangent)); memcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv)); memcpy(vertex.m_color, &mesh->m_colors[j], sizeof(vertex.m_color)); model->Expand(vertex.m_pos); } memcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size()); internal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices( vertices.data(), vertices.size(), sizeof(VertexColor), indices.data(), indices.size(), sizeof(std::uint32_t)); if (mesh_data == nullptr) { for (auto &elem : model->m_meshes) delete elem.first; delete model; return 1; } std::uint64_t id = GetNewID(); m_loaded_meshes[id] = mesh_data; mesh_handle->id = id; MaterialHandle material_handle = materials[mesh->m_material_id]; std::pair n_mesh = std::make_pair( mesh_handle, material_handle); model->m_meshes.push_back(n_mesh); } return 0; } template<> int ModelPool::LoadNodeMeshesWithMaterials(ModelData* data, Model * model, std::vector materials) { model->m_meshes.reserve(data->m_meshes.size()); for (unsigned int i = 0; i < data->m_meshes.size(); ++i) { ModelMeshData* mesh = data->m_meshes[i]; std::vector vertices(mesh->m_positions.size()); std::vector indices(mesh->m_indices.size()); Mesh* mesh_handle = new Mesh(); for (unsigned int j = 0; j < mesh->m_positions.size(); ++j) { VertexNoTangent &vertex = vertices[j]; memcpy(vertex.m_pos, &mesh->m_positions[j], sizeof(vertex.m_pos)); memcpy(vertex.m_normal, &mesh->m_normals[j], sizeof(vertex.m_normal)); memcpy(vertex.m_uv, &mesh->m_uvw[j], sizeof(vertex.m_uv)); model->Expand(vertex.m_pos); } memcpy(indices.data(), mesh->m_indices.data(), sizeof(std::uint32_t)*mesh->m_indices.size()); internal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices( vertices.data(), vertices.size(), sizeof(VertexNoTangent), indices.data(), indices.size(), sizeof(std::uint32_t)); if (mesh_data == nullptr) { for (auto &elem : model->m_meshes) delete elem.first; delete model; return 1; } std::uint64_t id = GetNewID(); m_loaded_meshes[id] = mesh_data; mesh_handle->id = id; MaterialHandle material_handle = materials[mesh->m_material_id]; std::pair n_mesh = std::make_pair( mesh_handle, material_handle); model->m_meshes.push_back(n_mesh); } return 0; } std::uint64_t ModelPool::GetNewID() { std::uint64_t id; if (m_freed_ids.size() > 0) { id = m_freed_ids.top(); m_freed_ids.pop(); } else { id = m_current_id; m_current_id++; } return id; } void ModelPool::FreeID(std::uint64_t id) { m_freed_ids.push(id); } } /* wr */ ================================================ FILE: src/model_pool.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include //#include #include #include "util/defines.hpp" #include "material_pool.hpp" #include "resource_pool_texture.hpp" #include "model_loader.hpp" #include "model_loader_assimp.hpp" #include "util/log.hpp" #include "util/aabb.hpp" #include "vertex.hpp" struct aiScene; struct aiNode; namespace wr { class ModelPool; namespace internal { struct MeshInternal { }; } struct Mesh { std::uint64_t id; //Box m_box; }; template struct MeshData { std::vector m_vertices; std::optional> m_indices; }; struct Model { std::vector> m_meshes; ModelPool* m_model_pool = nullptr; std::string m_model_name; bool m_owns_materials = false; //Whether the model has to take care of the materials Box m_box; void Expand(float (&pos)[3]); }; class ModelPool { public: explicit ModelPool(std::size_t vertex_buffer_pool_size_in_bytes, std::size_t index_buffer_pool_size_in_bytes); virtual ~ModelPool() = default; ModelPool(ModelPool const &) = delete; ModelPool& operator=(ModelPool const &) = delete; ModelPool(ModelPool&&) = delete; ModelPool& operator=(ModelPool&&) = delete; template [[nodiscard]] Model* Load(MaterialPool* material_pool, TexturePool* texture_pool, std::string_view path, std::optional out_model_data = std::nullopt); template [[nodiscard]] Model* LoadWithMaterials(MaterialPool* material_pool, TexturePool* texture_pool, std::string_view path, bool flip_normals = false, std::optional out_model_data = std::nullopt); template [[nodiscard]] Model* LoadCustom(std::vector> meshes); void Destroy(Model* model); void Destroy(internal::MeshInternal* mesh); // Shrinks down both heaps to the minimum size required. // Does not rearrange the contents of the heaps, meaning that it doesn't shrink to the absolute minimum size. // To do that, call Defragment first. virtual void ShrinkToFit() = 0; virtual void ShrinkVertexHeapToFit() = 0; virtual void ShrinkIndexHeapToFit() = 0; // Removes any holes in the memory, stitching all allocations back together to maximize the amount of contiguous free space. // These functions are called automatically if the allocator has enough free space but no large enough free blocks. virtual void Defragment() = 0; virtual void DefragmentVertexHeap() = 0; virtual void DefragmentIndexHeap() = 0; virtual size_t GetVertexHeapOccupiedSpace() = 0; virtual size_t GetIndexHeapOccupiedSpace() = 0; virtual size_t GetVertexHeapFreeSpace() = 0; virtual size_t GetIndexHeapFreeSpace() = 0; virtual size_t GetVertexHeapSize() = 0; virtual size_t GetIndexHeapSize() = 0; // Resizes both heaps to the supplied sizes. // If the supplied size is smaller than the required size the heaps will resize to the required size instead. virtual void Resize(size_t vertex_heap_new_size, size_t index_heap_new_size) = 0; virtual void ResizeVertexHeap(size_t vertex_heap_new_size) = 0; virtual void ResizeIndexHeap(size_t index_heap_new_size) = 0; template void EditMesh(Mesh* mesh, std::vector vertices, std::vector indices); virtual void Evict() = 0; virtual void MakeResident() = 0; virtual void MakeSpaceForModel(size_t vertex_size, size_t index_size) = 0; protected: virtual 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; virtual internal::MeshInternal* LoadCustom_VerticesOnly(void* vertices_data, std::size_t num_vertices, std::size_t vertex_size) = 0; virtual 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; virtual void DestroyModel(Model* model) = 0; virtual void DestroyMesh(internal::MeshInternal* mesh) = 0; template int LoadNodeMeshes(ModelData* data, Model* model, MaterialHandle default_material); template int LoadNodeMeshesWithMaterials(ModelData* data, Model* model, std::vector materials); template void UpdateModelBoundingBoxes(Model* model, std::vector vertices_data); std::size_t m_vertex_buffer_pool_size_in_bytes; std::size_t m_index_buffer_pool_size_in_bytes; std::map m_loaded_meshes; std::stack m_freed_ids; std::uint64_t m_current_id; std::uint64_t GetNewID(); void FreeID(std::uint64_t id); std::vector m_loaded_models; }; template Model* ModelPool::LoadCustom(std::vector> meshes) { IS_PROPER_VERTEX_CLASS(TV); auto model = new Model(); std::size_t total_vertex_size = 0; std::size_t total_index_size = 0; for (int i = 0; i < meshes.size(); ++i) { total_vertex_size += meshes[i].m_vertices.size() * sizeof(TV); if (meshes[i].m_indices.has_value()) { total_index_size += meshes[i].m_indices.value().size() * sizeof(TI); } } MakeSpaceForModel(total_vertex_size, total_index_size); for (int i = 0; i < meshes.size(); ++i) { Mesh* mesh = new Mesh(); if (meshes[i].m_indices.has_value()) { internal::MeshInternal* mesh_data = LoadCustom_VerticesAndIndices( meshes[i].m_vertices.data(), meshes[i].m_vertices.size(), sizeof(TV), meshes[i].m_indices.value().data(), meshes[i].m_indices.value().size(), sizeof(TI)); std::uint64_t id = GetNewID(); m_loaded_meshes[id] = mesh_data; mesh->id = id; } else { internal::MeshInternal* mesh_data = LoadCustom_VerticesOnly( meshes[i].m_vertices.data(), meshes[i].m_vertices.size(), sizeof(TV)); std::uint64_t id = GetNewID(); m_loaded_meshes[id] = mesh_data; mesh->id = id; } MaterialHandle handle = { nullptr, 0 }; model->m_meshes.push_back( std::make_pair(mesh, handle)); if constexpr (std::is_same::value || std::is_same::value) { for (uint32_t j = 0, k = (uint32_t) meshes[i].m_vertices.size(); j < k; ++j) { model->Expand(meshes[i].m_vertices[j].m_pos); } } } model->m_model_pool = this; m_loaded_models.push_back(model); return model; } //! Loads a model without materials template Model* ModelPool::Load(MaterialPool* material_pool, TexturePool* texture_pool, std::string_view path, std::optional out_model_data) { IS_PROPER_VERTEX_CLASS(TV); ModelLoader* loader = ModelLoader::FindFittingModelLoader( path.substr(path.find_last_of(".") + 1).data()); if (loader == nullptr) { return nullptr; } ModelData* data = loader->Load(path); Model* model = new Model; MaterialHandle default_material = { nullptr, 0 }; // TODO: Create default material MakeSpaceForModel(data->GetTotalVertexSize(), data->GetTotalIndexSize()); int ret = LoadNodeMeshes(data, model, default_material); if (ret == 1) { DestroyModel(model); loader->DeleteModel(data); return nullptr; } if (out_model_data.has_value()) { (*out_model_data.value()) = data; } else { loader->DeleteModel(data); } model->m_model_name = path.data(); model->m_model_pool = this; m_loaded_models.push_back(model); return model; } //! Loads a model with materials template Model* ModelPool::LoadWithMaterials(MaterialPool* material_pool, TexturePool* texture_pool, std::string_view path, bool flip_normals, std::optional out_model_data) { IS_PROPER_VERTEX_CLASS(TV); ModelLoader* loader = ModelLoader::FindFittingModelLoader( path.substr(path.find_last_of(".") + 1).data()); if (loader == nullptr) { return nullptr; } ModelData* data = loader->Load(path); if (flip_normals) { for (auto* meshes : data->m_meshes) { for (auto& normals : meshes->m_normals) { normals.x = -1.f * normals.x; normals.y = -1.f * normals.y; normals.z = -1.f * normals.z; } } } // Find directory std::string dir = std::string(path); dir.erase(dir.begin() + dir.find_last_of('/') + 1, dir.end()); Model* model = new Model; model->m_owns_materials = true; std::vector material_handles; for (int i = 0; i < data->m_materials.size(); ++i) { TextureHandle albedo, normals, metallic, roughness, emissive, ambient_occlusion; ModelMaterialData* material = data->m_materials[i]; // This lambda loads a texture either from memory or from disc. auto load_material_texture = [&](auto texture_location, auto embedded_texture_idx, std::string &texture_path, TextureHandle &handle, bool srgb, bool gen_mips) { if (texture_location == TextureLocation::EMBEDDED) { EmbeddedTexture* texture = data->m_embedded_textures[embedded_texture_idx]; if (texture->m_compressed) { handle = texture_pool->LoadFromMemory(texture->m_data.data(), texture->m_width, texture->m_height, texture->m_format, srgb, gen_mips); } else { handle = texture_pool->LoadFromMemory(texture->m_data.data(), texture->m_width, texture->m_height, wr::TextureFormat::RAW, srgb, gen_mips); } } else if (texture_location == TextureLocation::EXTERNAL) { handle = texture_pool->LoadFromFile(dir + texture_path, srgb, gen_mips); } }; //TODO: Maya team integrate texture scales in loading // Currently default scales are set to 1 for all materials. MaterialUVScales default_scales; auto new_handle = material_pool->Create(texture_pool); Material* mat = material_pool->GetMaterial(new_handle); if (material->m_albedo_texture_location!=TextureLocation::NON_EXISTENT) { load_material_texture(material->m_albedo_texture_location, material->m_albedo_embedded_texture, material->m_albedo_texture, albedo, true, true); mat->SetTexture(TextureType::ALBEDO, albedo); } if (material->m_normal_map_texture_location != TextureLocation::NON_EXISTENT) { load_material_texture(material->m_normal_map_texture_location, material->m_normal_map_embedded_texture, material->m_normal_map_texture, normals, false, true); mat->SetTexture(TextureType::NORMAL, normals); } if (material->m_metallic_texture_location != TextureLocation::NON_EXISTENT) { load_material_texture(material->m_metallic_texture_location, material->m_metallic_embedded_texture, material->m_metallic_texture, metallic, false, true); mat->SetTexture(TextureType::METALLIC, metallic); } if (material->m_roughness_texture_location != TextureLocation::NON_EXISTENT) { load_material_texture(material->m_roughness_texture_location, material->m_roughness_embedded_texture, material->m_roughness_texture, roughness, false, true); mat->SetTexture(TextureType::ROUGHNESS, roughness); } if (material->m_emissive_texture_location != TextureLocation::NON_EXISTENT) { load_material_texture(material->m_emissive_texture_location, material->m_emissive_embedded_texture, material->m_emissive_texture, emissive, true, true); mat->SetTexture(TextureType::EMISSIVE, emissive); } if (material->m_ambient_occlusion_texture_location != TextureLocation::NON_EXISTENT) { load_material_texture(material->m_ambient_occlusion_texture_location, material->m_ambient_occlusion_embedded_texture, material->m_ambient_occlusion_texture, ambient_occlusion, false, true); mat->SetTexture(TextureType::AO, ambient_occlusion); } bool two_sided = material->m_two_sided; float opacity = material->m_base_transparency; mat->SetConstant({ material->m_base_color[0], material->m_base_color[1], material->m_base_color[2] }); mat->SetConstant(material->m_base_metallic); mat->SetConstant(material->m_base_emissive); mat->SetConstant(material->m_base_roughness); mat->SetConstant(false); mat->SetConstant(false); material_handles.push_back(new_handle); } MakeSpaceForModel(data->GetTotalVertexSize(), data->GetTotalIndexSize()); int ret = LoadNodeMeshesWithMaterials(data, model, material_handles); if (ret == 1) { DestroyModel(model); loader->DeleteModel(data); return nullptr; } if (out_model_data.has_value()) { (*out_model_data.value()) = data; } else { loader->DeleteModel(data); } model->m_model_name = path.data(); model->m_model_pool = this; m_loaded_models.push_back(model); return model; } template void ModelPool::EditMesh(Mesh* mesh, std::vector vertices, std::vector indices) { UpdateMeshData(mesh, vertices.data(), vertices.size(), sizeof(TV), indices.data(), indices.size(), sizeof(TI)); for (auto model : m_loaded_models) { for (auto mesh_material : model->m_meshes) { if (mesh_material.first->id == mesh->id) { UpdateModelBoundingBoxes(model, vertices); } } } } } /* wr */ ================================================ FILE: src/pipeline_registry.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "pipeline_registry.hpp" namespace wr { PipelineRegistry::PipelineRegistry() : Registry() { } } /* wr */ ================================================ FILE: src/pipeline_registry.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "registry.hpp" #include #include #include "vertex.hpp" #include "util/named_type.hpp" #include "d3d12/d3d12_enums.hpp" namespace wr { using Pipeline = void; struct PipelineDescription { std::optional m_vertex_shader_handle; std::optional m_pixel_shader_handle; std::optional m_compute_shader_handle; RegistryHandle m_root_signature_handle; Format m_dsv_format; std::array m_rtv_formats; unsigned int m_num_rtv_formats; PipelineType m_type = PipelineType::GRAPHICS_PIPELINE; CullMode m_cull_mode = wr::CullMode::CULL_BACK; bool m_depth_enabled = false; bool m_counter_clockwise = false; TopologyType m_topology_type = wr::TopologyType::TRIANGLE; std::vector m_input_layout = {}; }; class PipelineRegistry : public internal::Registry { public: PipelineRegistry(); virtual ~PipelineRegistry() = default; template RegistryHandle Register(PipelineDescription description); }; template RegistryHandle PipelineRegistry::Register(PipelineDescription description) { IS_PROPER_VERTEX_CLASS(VT) auto handle = m_next_handle; description.m_input_layout = VT::GetInputLayout(); m_descriptions.insert({ handle, description }); m_next_handle++; return handle; } } /* wr */ ================================================ FILE: src/platform_independend_structs.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include "d3d12/d3d12_enums.hpp" #include "util/named_type.hpp" #include "util/user_literals.hpp" namespace wr { using CommandList = void; using RenderTarget = void; struct RenderTargetProperties { using IsRenderWindow = util::NamedType; using Width = util::NamedType>; using Height = util::NamedType>; using ExecuteResourceState = util::NamedType>; using FinishedResourceState = util::NamedType>; using CreateDSVBuffer = util::NamedType; using DSVFormat = util::NamedType; using RTVFormats = util::NamedType>; using NumRTVFormats = util::NamedType; using Clear = util::NamedType; using ClearDepth = util::NamedType; using ResolutionScalar = util::NamedType; IsRenderWindow m_is_render_window; Width m_width; Height m_height; ExecuteResourceState m_state_execute; FinishedResourceState m_state_finished; CreateDSVBuffer m_create_dsv_buffer; DSVFormat m_dsv_format; RTVFormats m_rtv_formats; NumRTVFormats m_num_rtv_formats; Clear m_clear = Clear(false); ClearDepth m_clear_depth = ClearDepth(false); ResolutionScalar m_resolution_scale = ResolutionScalar(1.0f); }; enum class LightType : int { POINT, DIRECTIONAL, SPOT, FREE }; struct Light { DirectX::XMFLOAT3 pos = { 0, 0, 0 }; //Position in world space for spot & point float rad = 5.f; //Radius for point, height for spot DirectX::XMFLOAT3 col = { 1, 1, 1 }; //Color (and strength) uint32_t tid = (uint32_t)LightType::FREE; //Type id; LightType::x DirectX::XMFLOAT3 dir = { 0, 0, 1 }; //Direction for spot & directional float ang = 40._deg; //Angle for spot; in radians DirectX::XMFLOAT3 padding; float light_size = 0.0f; }; } /* wr */ ================================================ FILE: src/registry.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include "util/log.hpp" #undef GetObject // Prevent macro collision namespace wr { using RegistryHandle = std::uint64_t; } namespace wr::internal { template class Registry { protected: Registry() : m_next_handle(0) { } public: virtual ~Registry() = default; TO* Find(RegistryHandle handle) { auto it = m_objects.find(handle); if (it != m_objects.end()) { return (*it).second; } else { LOGE("Failed to find registry object related to registry handle."); return nullptr; } } static C& Get() { static C instance = {}; return instance; } void Lock() { m_mutex.lock(); } void Unlock() { m_mutex.unlock(); } void ClearReloadRequests() { m_requested_reload.clear(); } std::vector const & GetReloadRequests() { return m_requested_reload; } void RequestReload(RegistryHandle handle) { Lock(); m_requested_reload.push_back(handle); Unlock(); } std::map m_descriptions; std::map m_objects; protected: std::vector m_requested_reload; std::mutex m_mutex; RegistryHandle m_next_handle; }; } /* wr::internal */ ================================================ FILE: src/render_tasks/d3d12_accumulation.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "d3d12_raytracing_task.hpp" #include "d3d12_path_tracer.hpp" namespace wr { struct AccumulationData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { template inline void SetupAccumulationTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); //Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors std::shared_ptr texture_pool = std::static_pointer_cast(n_render_system.m_texture_pools[0]); if (!texture_pool) { LOGC("Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool"); } data.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(2); auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::accumulation)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); for (auto frame_idx = 0; frame_idx < 1; frame_idx++) { // Destination { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::accumulation, params::AccumulationE::DEST); auto cpu_handle = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, frame_idx, n_render_target->m_create_info.m_rtv_formats[frame_idx]); } // Source { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::accumulation, params::AccumulationE::SOURCE); auto cpu_handle = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, frame_idx, source_rt->m_create_info.m_rtv_formats[frame_idx]); } } } inline void ExecuteAccumulationTask(RenderSystem& rs, FrameGraph& fg, SceneGraph&, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); d3d12::BindComputePipeline(cmd_list, data.out_pipeline); constexpr unsigned int uav_idx = rs_layout::GetHeapLoc(params::accumulation, params::AccumulationE::DEST); d3d12::DescHeapCPUHandle uav_handle = data.out_allocation.GetDescriptorHandle(uav_idx); d3d12::SetShaderUAV(cmd_list, 0, uav_idx, uav_handle); constexpr unsigned int srv_idx = rs_layout::GetHeapLoc(params::accumulation, params::AccumulationE::SOURCE); d3d12::DescHeapCPUHandle srv_handle = data.out_allocation.GetDescriptorHandle(srv_idx); d3d12::SetShaderSRV(cmd_list, 0, srv_idx, srv_handle); d3d12::BindDescriptorHeaps(cmd_list); { auto barrier = CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % 1 /*versions*/]); cmd_list->m_native->ResourceBarrier(1, &barrier); } fg.WaitForPredecessorTask(); float samples = n_render_system.temp_rough; d3d12::BindCompute32BitConstants(cmd_list, &samples, 1, 0, 1); d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyAccumulation(FrameGraph& fg, RenderTaskHandle handle, bool resize) { } } /* internal */ template inline void AddAccumulationTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupAccumulationTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteAccumulationTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyAccumulation(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Accumulation Task", FG_DEPS()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_ansel.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "d3d12_raytracing_task.hpp" #include "d3d12_deferred_main.hpp" #ifdef NVIDIA_GAMEWORKS_ANSEL #include #endif namespace wr { struct AnselSettings { struct Setup { float 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... }; struct Runtime { bool m_allow_translation = true; // User can move the camera during session bool m_allow_rotation = true; // Camera can be rotated during session bool m_allow_fov = true; // FoV can be modified during session bool m_allow_mono_360 = true; // Game allows 360 capture during session bool m_allow_stero_360 = true; // Game allows 360 stereo capture during session bool m_allow_raw = false; // Game allows capturing pre-tonemapping raw HDR buffer bool m_allow_pause = true; // Game is paused during capture bool m_allow_highres = true; // Game allows highres capture during session float 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 float m_rotation_speed_in_deg_per_second = 90.f; // The speed at which camera rotates, initialized with a value given in Configuration float 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. }; Setup m_setup; Runtime m_runtime; }; struct AnselData { bool out_ansel_support = false; #ifdef NVIDIA_GAMEWORKS_ANSEL std::optional out_original_camera; // Used to restore the camera to the original settings when exiting Ansel. #endif }; // Static variables so we can access them from the callbacks. static AnselSettings ansel_settings = AnselSettings(); static bool ansel_session_running = false; namespace internal { #ifdef NVIDIA_GAMEWORKS_ANSEL auto fill_ansel_config_obj = [](auto & conf) { conf.isTranslationAllowed = ansel_settings.m_runtime.m_allow_translation; conf.isRotationAllowed = ansel_settings.m_runtime.m_allow_rotation; conf.isFovChangeAllowed = ansel_settings.m_runtime.m_allow_fov; conf.is360MonoAllowed = ansel_settings.m_runtime.m_allow_mono_360; conf.is360StereoAllowed = ansel_settings.m_runtime.m_allow_stero_360; conf.isHighresAllowed = ansel_settings.m_runtime.m_allow_highres; conf.isRawAllowed = ansel_settings.m_runtime.m_allow_raw; conf.isPauseAllowed = ansel_settings.m_runtime.m_allow_pause; conf.translationalSpeedInWorldUnitsPerSecond = ansel_settings.m_runtime.m_translation_speed_in_world_units_per_sec; conf.rotationalSpeedInDegreesPerSecond = ansel_settings.m_runtime.m_rotation_speed_in_deg_per_second; conf.maximumFovInDegrees = ansel_settings.m_runtime.m_maximum_fov_in_deg; }; ansel::StartSessionStatus startAnselSessionCallback(ansel::SessionConfiguration& conf, void* userPointer) { ansel_session_running = true; fill_ansel_config_obj(conf); return ansel::kAllowed; } void stopAnselSessionCallback(void* userPointer) { ansel_session_running = false; } void startAnselCaptureCallback(ansel::CaptureConfiguration const & conf, void* userPointer) { } void stopAnselCaptureCallback(void* userPointer) { } ansel::Camera NativeToAnselCamera(std::shared_ptr const & camera) { auto proj_offset = camera->GetProjectionOffset(); ansel::Camera ansel_camera{}; ansel_camera.aspectRatio = camera->m_aspect_ratio; ansel_camera.fov = DirectX::XMConvertToDegrees(camera->m_fov.m_fov); ansel_camera.position = { camera->m_position.m128_f32[0], camera->m_position.m128_f32[1], camera->m_position.m128_f32[2] }; ansel_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] }; ansel_camera.nearPlane = camera->m_frustum_near; ansel_camera.farPlane = camera->m_frustum_far; ansel_camera.projectionOffsetX = proj_offset.first; ansel_camera.projectionOffsetY = proj_offset.second; return ansel_camera; } void AnselToNativeCamera(ansel::Camera const & ansel_camera, std::shared_ptr camera) { camera->SetProjectionOffset(ansel_camera.projectionOffsetX, ansel_camera.projectionOffsetY); camera->SetPosition({ ansel_camera.position.x, ansel_camera.position.y, ansel_camera.position.z }); camera->SetRotationQuaternion({ ansel_camera.rotation.x, ansel_camera.rotation.y , ansel_camera.rotation.z , ansel_camera.rotation.w }); camera->m_fov = wr::CameraNode::FoV(wr::CameraNode::FovDefault(ansel_camera.fov)); camera->m_frustum_near = ansel_camera.nearPlane; camera->m_frustum_far = ansel_camera.farPlane; } #endif inline void SetupAnselTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { #ifdef NVIDIA_GAMEWORKS_ANSEL if (resize) { return; } auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); ansel_settings = fg.GetSettings(handle); if (!n_render_system.m_window.has_value()) { LOGW("Tried initializing ansel without a render window! Ansel is not supported for headless rendering."); return; } data.out_ansel_support = ansel::isAnselAvailable(); if (!data.out_ansel_support) { LOGW("Your machine doesn't seem to support NVIDIA Ansel"); return; } auto window = n_render_system.m_window.value(); ansel::Configuration config; config.translationalSpeedInWorldUnitsPerSecond = ansel_settings.m_runtime.m_translation_speed_in_world_units_per_sec; config.metersInWorldUnit = ansel_settings.m_setup.m_meters_in_world_unit; config.right = { 1, 0, 0 }; config.up = { 0, 1, 0 }; config.forward = { 0, 0, -1 }; config.fovType = ansel::kVerticalFov; config.isCameraOffcenteredProjectionSupported = true; config.isCameraRotationSupported = true; config.isCameraTranslationSupported = true; config.isCameraFovSupported = true; config.gameWindowHandle = window->GetWindowHandle(); config.titleNameUtf8 = window->GetTitle().c_str(); config.startSessionCallback = startAnselSessionCallback; config.stopSessionCallback = stopAnselSessionCallback; config.startCaptureCallback = startAnselCaptureCallback; config.stopCaptureCallback = stopAnselCaptureCallback; auto status = ansel::setConfiguration(config); if (status != ansel::kSetConfigurationSuccess) { LOGW("Failed to initialize NVIDIA Ansel."); } LOG("NVIDIA Ansel is initialized"); #endif } inline void ExecuteAnselTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { #ifdef NVIDIA_GAMEWORKS_ANSEL auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); ansel_settings = fg.GetSettings(handle); if (!data.out_ansel_support) { return; } if (!n_render_system.m_render_window.has_value()) { LOGW("Tried initializing ansel without a render window! Ansel is not supported for headless rendering."); return; } if (ansel_session_running) { auto camera = sg.GetActiveCamera(); auto ansel_camera = NativeToAnselCamera(camera); if (!data.out_original_camera.has_value()) { data.out_original_camera = ansel_camera; } ansel::updateCamera(ansel_camera); AnselToNativeCamera(ansel_camera, camera); } else if (data.out_original_camera.has_value()) { auto camera = sg.GetActiveCamera(); auto ansel_camera = data.out_original_camera.value(); AnselToNativeCamera(ansel_camera, camera); data.out_original_camera = std::nullopt; } #endif } inline void DestroyAnselTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { #ifdef NVIDIA_GAMEWORKS_ANSEL #endif } } /* internal */ inline void AddAnselTask(FrameGraph& frame_graph) { #ifndef NVIDIA_GAMEWORKS_ANSEL LOGW("Ansel task has been added to the frame graph. But `NVIDIA_GAMEWORKS_ANSEL` is not defined."); #endif RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ d3d12::settings::back_buffer_format }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupAnselTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteAnselTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyAnselTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"NVIDIA Ansel"); frame_graph.UpdateSettings(AnselSettings()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_bloom_composition.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct BloomSettings { struct Runtime { float m_sigma = 2.0f; float m_bloom_intensity = 1.0f; float m_luminance_threshold = 1.0f; bool m_enable_bloom = true; }; Runtime m_runtime; }; struct BloomCompostionData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::RenderTarget* out_source_bloom_half_rt = nullptr; d3d12::RenderTarget* out_source_bloom_quarter_rt = nullptr; d3d12::RenderTarget* out_source_bloom_eighth_rt = nullptr; d3d12::RenderTarget* out_source_bloom_sixteenth_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; std::shared_ptr bloom_cb_pool; D3D12ConstantBufferHandle* cb_handle = nullptr; }; namespace internal { template inline void SetupBloomCompositionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(5); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::bloom_composition)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); auto bloom_rt_half = data.out_source_bloom_half_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source main { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition , params::BloomCompositionE::SOURCE_MAIN))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Source bloom half { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_HALF))); d3d12::CreateSRVFromSpecificRTV(bloom_rt_half, cpu_handle, 0, bloom_rt_half->m_create_info.m_rtv_formats[0]); } // Source bloom qes { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_QES))); d3d12::CreateSRVFromSpecificRTV(bloom_rt_half, cpu_handle, 1, bloom_rt_half->m_create_info.m_rtv_formats[1]); } } template inline void ExecuteBloomCompositionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto settings = fg.GetSettings(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; d3d12::BindComputePipeline(cmd_list, data.out_pipeline); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); auto bloom_rt_half = data.out_source_bloom_half_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source main { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_MAIN))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Source bloom half { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_HALF))); d3d12::CreateSRVFromSpecificRTV(bloom_rt_half, cpu_handle, 0, bloom_rt_half->m_create_info.m_rtv_formats[0]); } // Source bloom qes { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_QES))); d3d12::CreateSRVFromSpecificRTV(bloom_rt_half, cpu_handle, 1, bloom_rt_half->m_create_info.m_rtv_formats[1]); } int enable_bloom = settings.m_runtime.m_enable_bloom; d3d12::BindCompute32BitConstants(cmd_list, &enable_bloom, 1, 0, 1); //cmd_list->m_dynamic_descriptor_heaps[D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV]->StageDescriptors(0, 0, 3, data.out_allocation.GetDescriptorHandle()); { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::OUTPUT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int source_main_idx = rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_MAIN); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_main_idx); d3d12::SetShaderSRV(cmd_list, 0, source_main_idx, handle_m_srv); } { constexpr unsigned int source_bloom_half_idx = rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_HALF); auto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_bloom_half_idx); d3d12::SetShaderSRV(cmd_list, 0, source_bloom_half_idx, handle_b_srv); } { constexpr unsigned int source_bloom_qes_idx = rs_layout::GetHeapLoc(params::bloom_composition, params::BloomCompositionE::SOURCE_BLOOM_QES); auto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_bloom_qes_idx); d3d12::SetShaderSRV(cmd_list, 0, source_bloom_qes_idx, handle_b_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyBloomCompositionTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddBloomCompositionTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_FLOAT}), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(1.0f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupBloomCompositionTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteBloomCompositionTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyBloomCompositionTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Bloom Composition"); frame_graph.UpdateSettings(BloomSettings()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_bloom_extract_bright.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct BloomExtractBrightData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::RenderTarget* out_source_emissive = nullptr; d3d12::RenderTarget* out_source_coc = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { template inline void SetupBloomExtractBrightTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(4); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::bloom_extract_bright)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); auto source_emissive = data.out_source_emissive = static_cast(fg.GetPredecessorRenderTarget()); // Bright output for bloom { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::OUTPUT_BRIGHT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Depth buffer { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_DEPTH))); d3d12::CreateSRVFromDSV(source_emissive, cpu_handle); } // Source emissive { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_EMISSIVE))); d3d12::CreateSRVFromSpecificRTV(source_emissive, cpu_handle, 2, source_emissive->m_create_info.m_rtv_formats[2]); } } template inline void ExecuteBloomExtractBrightTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); auto source_emissive = data.out_source_emissive = static_cast(fg.GetPredecessorRenderTarget()); // Bright output for bloom { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::OUTPUT_BRIGHT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Depth buffer { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_DEPTH))); d3d12::CreateSRVFromDSV(source_emissive, cpu_handle); } // Source emissive { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_EMISSIVE))); d3d12::CreateSRVFromSpecificRTV(source_emissive, cpu_handle, 2, source_emissive->m_create_info.m_rtv_formats[2]); } d3d12::BindComputePipeline(cmd_list, data.out_pipeline); { constexpr unsigned int dest_b_idx = rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::OUTPUT_BRIGHT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_b_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_b_idx, handle_uav); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::SOURCE); auto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_b_srv); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_EMISSIVE); auto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_b_srv); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::bloom_extract_bright, params::BloomExtractBrightE::G_DEPTH); auto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_b_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyBloomExtractBrightTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddBloomExtractBrightTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT}), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.5f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupBloomExtractBrightTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteBloomExtractBrightTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyBloomExtractBrightTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"extract bright"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_bloom_horizontal_blur.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct BloomBlurHorizontalData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::RenderTarget* out_source_bloom_qes_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; std::shared_ptr cb_pool; D3D12ConstantBufferHandle* cb_handle = nullptr; }; namespace internal { struct Bloomblur_CB { DirectX::XMFLOAT2 blur_dir; float _pad = 0.f; float sigma; }; template inline void SetupBloomBlurHorizontalTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(4); data.cb_pool = rs.CreateConstantBufferPool(2); data.cb_handle = static_cast(data.cb_pool->Create(sizeof(Bloomblur_CB))); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::bloom_blur_horizontal)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Output half { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Output quarter/ eighth / sixteenth { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT_QES))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } // Source main { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::SOURCE_MAIN))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } } template inline void ExecuteBloomBlurHorizontalTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto settings = fg.GetSettings(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; d3d12::BindComputePipeline(cmd_list, data.out_pipeline); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Output half { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Output quarter/ eighth / sixteenth { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT_QES))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } // Source main { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::SOURCE_MAIN))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } Bloomblur_CB cb_data; cb_data.blur_dir = DirectX::XMFLOAT2(1.0f, 0.0f); cb_data.sigma = 2.0f; data.cb_handle->m_pool->Update(data.cb_handle, sizeof(Bloomblur_CB), 0, frame_idx, (uint8_t*)& cb_data); d3d12::BindComputeConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx); { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_blur_horizontal , params::BloomBlurHorizontalE::OUTPUT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::OUTPUT_QES); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int source_main_idx = rs_layout::GetHeapLoc(params::bloom_blur_horizontal, params::BloomBlurHorizontalE::SOURCE_MAIN); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_main_idx); d3d12::SetShaderSRV(cmd_list, 0, source_main_idx, handle_m_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); float width = n_render_system.m_viewport.m_viewport.Width; float height = n_render_system.m_viewport.m_viewport.Height; d3d12::Dispatch(cmd_list, static_cast(std::ceil(width / 16.f)) + static_cast(std::ceil(width / 2.0f / 16.f)) + static_cast(std::ceil(width / 4 / 16.f)) + static_cast(std::ceil(width / 8 / 16.f)), static_cast(std::ceil(height / 16.f)) + static_cast(std::ceil(height / 2.0f / 16.f)) + static_cast(std::ceil(height / 4.0f / 16.f)) + static_cast(std::ceil(height / 8.0f / 16.f)), 1); } inline void DestroyBloomBlurHorizontalTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddBloomBlurHorizontalTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}), RenderTargetProperties::NumRTVFormats(2), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.5f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupBloomBlurHorizontalTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteBloomBlurHorizontalTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyBloomBlurHorizontalTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Bloom blur test"); frame_graph.UpdateSettings(BloomSettings()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_bloom_vertical_blur.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "..//render_tasks/d3d12_bloom_horizontal_blur.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct BloomBlurVerticalData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::RenderTarget* out_source_bloom_qes_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; std::shared_ptr cb_pool; D3D12ConstantBufferHandle* cb_handle = nullptr; }; namespace internal { template inline void SetupBloomBlurVerticalTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(5); data.cb_pool = rs.CreateConstantBufferPool(2); data.cb_handle = static_cast(data.cb_pool->Create(sizeof(Bloomblur_CB))); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::bloom_blur_vertical)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Output half { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Output quarter/ eighth / sixteenth { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT_QES))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } // Source main { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_MAIN))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Source quarter eighth sixteeth { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_QES))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]); } } template inline void ExecuteBloomBlurVerticalTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; d3d12::BindComputePipeline(cmd_list, data.out_pipeline); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Output half { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Output quarter/ eighth / sixteenth { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT_QES))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } // Source main { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_MAIN))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Source quarter eighth sixteeth { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_QES))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]); } Bloomblur_CB cb_data; cb_data.blur_dir = DirectX::XMFLOAT2(0.0f, 1.0f); cb_data.sigma = 2.0f; data.cb_handle->m_pool->Update(data.cb_handle, sizeof(Bloomblur_CB), 0, frame_idx, (uint8_t*)& cb_data); d3d12::BindComputeConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx); { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::OUTPUT_QES); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int source_main_idx = rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_MAIN); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_main_idx); d3d12::SetShaderSRV(cmd_list, 0, source_main_idx, handle_m_srv); } { constexpr unsigned int source_main_idx = rs_layout::GetHeapLoc(params::bloom_blur_vertical, params::BloomBlurVerticalE::SOURCE_QES); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_main_idx); d3d12::SetShaderSRV(cmd_list, 0, source_main_idx, handle_m_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); float width = n_render_system.m_viewport.m_viewport.Width; float height = n_render_system.m_viewport.m_viewport.Height; d3d12::Dispatch(cmd_list, static_cast(std::ceil(width / 16.f)) + static_cast(std::ceil(width / 2.0f / 16.f)) + static_cast(std::ceil(width / 4 / 16.f)) + static_cast(std::ceil(width / 8 / 16.f)), static_cast(std::ceil(height / 16.f)) + static_cast(std::ceil(height / 2.0f / 16.f)) + static_cast(std::ceil(height / 4.0f / 16.f)) + static_cast(std::ceil(height / 8.0f / 16.f)), 1); } inline void DestroyBloomBlurVerticalTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddBloomBlurVerticalTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}), RenderTargetProperties::NumRTVFormats(2), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.5f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupBloomBlurVerticalTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteBloomBlurVerticalTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyBloomBlurVerticalTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Bloom blur test"); //frame_graph.UpdateSettings(BloomSettings()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_brdf_lut_precalculation.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_defines.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../pipeline_registry.hpp" #include "../engine_registry.hpp" #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_descriptors_allocations.hpp" #include "../d3d12/d3d12_resource_pool_texture.hpp" namespace wr { struct BrdfLutTaskData { d3d12::PipelineState* in_pipeline; }; namespace internal { inline void SetupBrdfLutPrecalculationTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto& ps_registry = PipelineRegistry::Get(); data.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::brdf_lut_precalculation); if (!resize && !n_render_system.m_brdf_lut.has_value()) { //Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors std::shared_ptr texture_pool = std::static_pointer_cast(n_render_system.m_texture_pools[0]); if (!texture_pool) { LOGC("Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool"); } if (n_render_system.m_brdf_lut == std::nullopt) { n_render_system.m_brdf_lut = texture_pool->CreateTexture("BRDF LUT 2D", 512, 512, 1, wr::Format::R16G16_FLOAT, false); } } } inline void ExecuteBrdfLutPrecalculationTask(RenderSystem& rs, FrameGraph& fg, SceneGraph&, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); if (!n_render_system.m_brdf_lut_generated) { auto cmd_list = fg.GetCommandList(handle); d3d12::BindComputePipeline(cmd_list, data.in_pipeline); auto* brdf_lut = static_cast(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value())); d3d12::Transition(cmd_list, brdf_lut, ResourceState::COPY_DEST, ResourceState::UNORDERED_ACCESS); d3d12::SetShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::brdf_lut, params::BRDF_LutE::OUTPUT)), brdf_lut); d3d12::Dispatch(cmd_list, static_cast(512 / 16), static_cast(512 / 16.f), 1); d3d12::Transition(cmd_list, brdf_lut, ResourceState::UNORDERED_ACCESS, ResourceState::PIXEL_SHADER_RESOURCE); n_render_system.m_brdf_lut_generated = true; fg.SetShouldExecute(handle, false); } } } inline void AddBrdfLutPrecalculationTask(FrameGraph& fg) { RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupBrdfLutPrecalculationTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteBrdfLutPrecalculationTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph&, RenderTaskHandle, bool) { }; desc.m_properties = std::nullopt; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; fg.AddTask(desc, L"BRDF LUT Precalculation"); } } ================================================ FILE: src/render_tasks/d3d12_build_acceleration_structures.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../d3d12/d3d12_rt_descriptor_heap.hpp" #include "../frame_graph/frame_graph.hpp" #include "../engine_registry.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../imgui_tools.hpp" namespace wr { struct ASBuildSettings { struct Runtime { bool m_rebuild_as = false; }; Runtime m_runtime; }; struct ASBuildData { DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_scene_ib_alloc; DescriptorAllocation out_scene_mat_alloc; DescriptorAllocation out_scene_offset_alloc; d3d12::AccelerationStructure out_tlas = {}; D3D12StructuredBufferHandle* out_sb_material_handle = nullptr; D3D12StructuredBufferHandle* out_sb_offset_handle = nullptr; std::vector out_blas_list; std::vector out_materials; std::vector out_offsets; std::unordered_map out_parsed_materials; std::vector out_material_handles; d3d12::StagingBuffer* out_scene_ib; d3d12::StagingBuffer* out_scene_vb; std::unordered_map blasses; std::vector> old_blasses; std::vector old_tlas; unsigned int previous_frame_index = 0; unsigned int current_frame_index = 0; bool out_init = true; bool out_materials_require_update = true; }; namespace internal { inline void SetupBuildASTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (resize) return; auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); d3d12::SetName(n_render_target, L"Raytracing Target"); data.out_init = true; data.out_materials_require_update = true; // Structured buffer for the materials. data.out_sb_material_handle = static_cast(n_render_system.m_raytracing_material_sb_pool->Create(sizeof(temp::RayTracingMaterial_CBData) * d3d12::settings::num_max_rt_materials, sizeof(temp::RayTracingMaterial_CBData), false)); // Structured buffer for the materials. data.out_sb_offset_handle = static_cast(n_render_system.m_raytracing_offset_sb_pool->Create(sizeof(temp::RayTracingOffset_CBData) * d3d12::settings::num_max_rt_materials, sizeof(temp::RayTracingOffset_CBData), false)); // Resize the materials data.out_materials.reserve(d3d12::settings::num_max_rt_materials); data.out_offsets.reserve(d3d12::settings::num_max_rt_materials); data.out_parsed_materials.reserve(d3d12::settings::num_max_rt_materials); auto texture_pool = std::static_pointer_cast(n_render_system.GetDefaultTexturePool()); data.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_scene_ib_alloc = std::move(data.out_allocator->Allocate()); data.out_scene_mat_alloc = std::move(data.out_allocator->Allocate()); data.out_scene_offset_alloc = std::move(data.out_allocator->Allocate()); data.old_blasses.resize(d3d12::settings::num_back_buffers); data.old_tlas.resize(d3d12::settings::num_back_buffers); } namespace internal { //! Get a material id from a mesh. inline unsigned int ExtractMaterialFromMesh(ASBuildData& data, MaterialHandle material_handle) { std::size_t material_id = 0; if (data.out_parsed_materials.find(material_handle.m_id) == data.out_parsed_materials.end()) { material_id = data.out_materials.size(); auto* material_internal = material_handle.m_pool->GetMaterial(material_handle); // Build material wr::temp::RayTracingMaterial_CBData material; material.albedo_id = material_internal->GetTexture(wr::TextureType::ALBEDO).m_id; material.normal_id = material_internal->GetTexture(wr::TextureType::NORMAL).m_id; material.roughness_id = material_internal->GetTexture(wr::TextureType::ROUGHNESS).m_id; material.metallicness_id = material_internal->GetTexture(wr::TextureType::METALLIC).m_id; material.emissive_id = material_internal->GetTexture(wr::TextureType::EMISSIVE).m_id; material.ao_id = material_internal->GetTexture(wr::TextureType::AO).m_id; material.material_data = material_internal->GetMaterialData(); data.out_materials.push_back(material); data.out_parsed_materials[material_handle.m_id] = material_id; data.out_materials_require_update = true; } else { material_id = data.out_parsed_materials[material_handle.m_id]; } return static_cast(material_id); } //! Add a data struct describing the mesh data offset and the material idx to `out_offsets` inline void AppendOffset(ASBuildData& data, wr::internal::D3D12MeshInternal* mesh, unsigned int material_id) { wr::temp::RayTracingOffset_CBData offset; offset.material_idx = material_id; offset.idx_offset = static_cast(mesh->m_index_staging_buffer_offset); offset.vertex_offset = static_cast(mesh->m_vertex_staging_buffer_offset); data.out_offsets.push_back(offset); } inline d3d12::desc::GeometryDesc CreateGeometryDescFromMesh(D3D12MeshInternal* mesh, d3d12::StagingBuffer* vb, d3d12::StagingBuffer* ib) { d3d12::desc::GeometryDesc obj; obj.index_buffer = ib; obj.vertex_buffer = vb; obj.m_indices_offset = static_cast(mesh->m_index_staging_buffer_offset); obj.m_num_indices = static_cast(mesh->m_index_count); obj.m_vertices_offset = static_cast(mesh->m_vertex_staging_buffer_offset); obj.m_num_vertices = static_cast(mesh->m_vertex_count); obj.m_vertex_stride = static_cast(mesh->m_vertex_staging_buffer_stride); return obj; } inline void BuildBLASSingle(d3d12::Device* device, d3d12::CommandList* cmd_list, Model* model, std::pair mesh_material, ASBuildData& data, std::uint32_t frame_idx) { auto n_model_pool = static_cast(model->m_model_pool); auto vb = n_model_pool->GetVertexStagingBuffer(); auto ib = n_model_pool->GetIndexStagingBuffer(); d3d12::DescriptorHeap* out_heap = cmd_list->m_rt_descriptor_heap->GetHeap(); data.out_scene_ib = ib; data.out_scene_vb = vb; auto n_mesh = static_cast(model->m_model_pool)->GetMeshData(mesh_material.first->id); d3d12::desc::GeometryDesc obj = CreateGeometryDescFromMesh(n_mesh, vb, ib); // Build Bottom level BVH auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }); d3d12::UAVBarrierAS(cmd_list, blas, frame_idx); d3d12::SetName(blas, L"Bottom Level Acceleration Structure"); data.blasses.insert({ mesh_material.first->id, blas }); data.out_material_handles.push_back(mesh_material.second); // Used to st eal the textures from the texture pool. auto material_id = ExtractMaterialFromMesh(data, mesh_material.second); AppendOffset(data, n_mesh, material_id); } inline void BuildBLASList(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, std::uint32_t frame_idx) { data.out_materials.clear(); data.out_material_handles.clear(); data.out_offsets.clear(); data.out_parsed_materials.clear(); d3d12::DescriptorHeap* out_heap = cmd_list->m_rt_descriptor_heap->GetHeap(); for (auto it = data.blasses.begin(); it != data.blasses.end(); ++it) { data.old_blasses[data.current_frame_index].push_back((*it).second); } data.blasses.clear(); data.out_blas_list.clear(); auto& batches = scene_graph.GetGlobalBatches(); const auto& batchInfo = scene_graph.GetBatches(); unsigned int offset_id = 0; for (auto& batch : batches) { auto model = batch.first.first; auto materials = batch.first.second; auto n_model_pool = static_cast(model->m_model_pool); auto vb = n_model_pool->GetVertexStagingBuffer(); auto ib = n_model_pool->GetIndexStagingBuffer(); data.out_scene_ib = ib; data.out_scene_vb = vb; for (std::size_t mesh_i = 0; mesh_i < model->m_meshes.size(); mesh_i++) { auto mesh = model->m_meshes[mesh_i]; auto n_mesh = static_cast(model->m_model_pool)->GetMeshData(mesh.first->id); auto material_handle = mesh.second; if (materials.size() > mesh_i) { material_handle = materials[mesh_i]; } d3d12::desc::GeometryDesc obj = CreateGeometryDescFromMesh(n_mesh, vb, ib); // Build Bottom level BVH auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }); d3d12::UAVBarrierAS(cmd_list, blas, frame_idx); d3d12::SetName(blas, L"Bottom Level Acceleration Structure"); data.blasses.insert({ mesh.first->id, blas }); data.out_material_handles.push_back(material_handle); // Used to st eal the textures from the texture pool. auto material_id = ExtractMaterialFromMesh(data, material_handle); AppendOffset(data, n_mesh, material_id); auto batch_it = batchInfo.find({ model, materials }); assert(batch_it != batchInfo.end() && "Batch was found in global array, but not in local"); // Push instances into a array for later use. for (uint32_t i = 0U, j = (uint32_t)batch_it->second.num_global_instances; i < j; i++) { auto transform = batch.second[i].m_model; data.out_blas_list.push_back({ blas, offset_id, transform }); } offset_id++; } } // Make sure our gathered data isn't out of bounds. if (data.out_offsets.size() > d3d12::settings::num_max_rt_materials) { LOGE("There are to many offsets stored for ray tracing!"); } if (data.out_materials.size() > d3d12::settings::num_max_rt_materials) { LOGE("There are to many materials stored for ray tracing!"); } } inline void CreateSRVs(ASBuildData& data) { for (auto i = 0; i < d3d12::settings::num_back_buffers; i++) { // Create BYTE ADDRESS buffer view into a staging buffer. Hopefully this works. { auto cpu_handle = data.out_scene_ib_alloc.GetDescriptorHandle(); d3d12::CreateRawSRVFromStagingBuffer(data.out_scene_ib, cpu_handle, static_cast(data.out_scene_ib->m_size / data.out_scene_ib->m_stride_in_bytes)); } // Create material structured buffer view { auto cpu_handle = data.out_scene_mat_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromStructuredBuffer(data.out_sb_material_handle->m_native, cpu_handle, 0); } // Create offset structured buffer view { auto cpu_handle = data.out_scene_offset_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromStructuredBuffer(data.out_sb_offset_handle->m_native, cpu_handle, 0); } } } /*inline bool ReconstructBLASsIfNeeded(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data) { auto batches = scene_graph.GetGlobalBatches(); bool needs_reconstruction = false; std::vector model_pools; for (auto& batch : batches) { auto model = batch.first.first; auto materials = batch.first.second; bool model_pool_loaded = false; for (int i = 0; i < model_pools.size(); ++i) { if (model_pools[i] == model->m_model_pool) { model_pool_loaded = true; } } if (!model_pool_loaded) { model_pools.push_back(static_cast(model->m_model_pool)); if (static_cast(model->m_model_pool)->IsUpdated()) { needs_reconstruction = true; } } } if (needs_reconstruction) { // Transition all model pools for accel structure creation for (auto& pool : model_pools) { d3d12::Transition(cmd_list, pool->GetVertexStagingBuffer(), ResourceState::VERTEX_AND_CONSTANT_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, pool->GetIndexStagingBuffer(), ResourceState::INDEX_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); } BuildBLASList(device, cmd_list, scene_graph, data); for (auto& pool : model_pools) { d3d12::Transition(cmd_list, pool->GetVertexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::VERTEX_AND_CONSTANT_BUFFER); d3d12::Transition(cmd_list, pool->GetIndexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::INDEX_BUFFER); } CreateSRVs(data); } return needs_reconstruction; }*/ inline void UpdateTLAS(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, std::uint32_t frame_idx) { data.out_materials.clear(); data.out_offsets.clear(); data.out_parsed_materials.clear(); d3d12::DescriptorHeap* out_heap = cmd_list->m_rt_descriptor_heap->GetHeap(); auto& batches = scene_graph.GetGlobalBatches(); const auto& batchInfo = scene_graph.GetBatches(); auto prev_size = data.out_blas_list.size(); data.out_blas_list.clear(); data.out_blas_list.reserve(prev_size); unsigned int offset_id = 0; //ReconstructBLASsIfNeeded(device, cmd_list, scene_graph, data); // Update transformations // TODO: This might be unnessessary if reconstrblasifneeded return true. for (auto& batch : batches) { auto model = batch.first.first; auto materials = batch.first.second; auto n_model_pool = static_cast(model->m_model_pool); for (std::size_t mesh_i = 0; mesh_i < model->m_meshes.size(); mesh_i++) { auto mesh = model->m_meshes[mesh_i]; auto n_mesh = static_cast(model->m_model_pool)->GetMeshData(mesh.first->id); // Pick the standard material or if available a user defined material. auto material_handle = mesh.second; if (materials.size() > mesh_i) { material_handle = materials[mesh_i]; } std::unordered_map::iterator blas_iterator = data.blasses.find(mesh.first->id); if (blas_iterator == data.blasses.end() || n_mesh->data_changed) { // Check if data changed in the mesh and if the blas iterator isn't an end iterator, // since it will be dereferenced (end iterators can't be dereferenced). if (n_mesh->data_changed && blas_iterator != data.blasses.end()) { data.old_blasses[data.current_frame_index].push_back((*blas_iterator).second); data.blasses.erase(blas_iterator); n_mesh->data_changed = false; } d3d12::Transition(cmd_list, n_model_pool->GetVertexStagingBuffer(), ResourceState::VERTEX_AND_CONSTANT_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, n_model_pool->GetIndexStagingBuffer(), ResourceState::INDEX_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); BuildBLASSingle(device, cmd_list, model, { mesh.first, material_handle }, data, frame_idx); d3d12::Transition(cmd_list, n_model_pool->GetVertexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::VERTEX_AND_CONSTANT_BUFFER); d3d12::Transition(cmd_list, n_model_pool->GetIndexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::INDEX_BUFFER); CreateSRVs(data); blas_iterator = data.blasses.find(mesh.first->id); } auto blas = (*blas_iterator).second; auto material_id = ExtractMaterialFromMesh(data, material_handle); AppendOffset(data, n_mesh, material_id); auto it = batchInfo.find({ model, materials }); assert(it != batchInfo.end() && "Batch was found in global array, but not in local"); // Push instances into a array for later use. for (uint32_t i = 0U, j = (uint32_t)it->second.num_global_instances; i < j; i++) { auto transform = batch.second[i].m_model; data.out_blas_list.push_back({ blas, offset_id, transform }); } offset_id++; } } d3d12::AccelerationStructure old_accel = data.out_tlas; d3d12::UpdateTopLevelAccelerationStructure(data.out_tlas, device, cmd_list, out_heap, data.out_blas_list, frame_idx); if (old_accel.m_scratch != data.out_tlas.m_scratch && old_accel.m_natives[frame_idx] != data.out_tlas.m_natives[frame_idx] && old_accel.m_instance_descs[frame_idx] != data.out_tlas.m_instance_descs[frame_idx]) { data.old_tlas[data.current_frame_index] = old_accel; } } } /* internal */ inline void ExecuteBuildASTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle) { auto& data = fg.GetData(handle); auto settings = fg.GetSettings(handle); auto cmd_list = fg.GetCommandList(handle); auto& n_render_system = static_cast(rs); auto device = n_render_system.m_device; auto frame_idx = n_render_system.GetFrameIdx(); data.out_materials_require_update = false; data.current_frame_index = n_render_system.GetFrameIdx(); d3d12::DescriptorHeap* out_heap = cmd_list->m_rt_descriptor_heap->GetHeap(); // Initialize requirements if (data.out_init) { std::vector> model_pools = n_render_system.m_model_pools; // Transition all model pools for accel structure creation for (auto& pool : model_pools) { d3d12::Transition(cmd_list, pool->GetVertexStagingBuffer(), ResourceState::VERTEX_AND_CONSTANT_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, pool->GetIndexStagingBuffer(), ResourceState::INDEX_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); } // List all materials used by meshes internal::BuildBLASList(device, cmd_list, scene_graph, data, frame_idx); data.out_tlas = d3d12::CreateTopLevelAccelerationStructure(device, cmd_list, out_heap, data.out_blas_list); d3d12::SetName(data.out_tlas, L"Top Level Acceleration Structure"); // Transition all model pools back to whatever they were. for (auto& pool : model_pools) { d3d12::Transition(cmd_list, pool->GetVertexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::VERTEX_AND_CONSTANT_BUFFER); d3d12::Transition(cmd_list, pool->GetIndexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::INDEX_BUFFER); } if (!data.out_blas_list.empty()) { internal::CreateSRVs(data); } data.out_init = false; } else if (!settings.m_runtime.m_rebuild_as) { if (data.current_frame_index != data.previous_frame_index) { for (int i = 0; i < data.old_blasses[data.current_frame_index].size(); ++i) { d3d12::DestroyAccelerationStructure(data.old_blasses[data.current_frame_index][i]); } data.old_blasses[data.current_frame_index].clear(); d3d12::DestroyAccelerationStructure(data.old_tlas[data.current_frame_index]); data.old_tlas[data.current_frame_index] = {}; } internal::UpdateTLAS(device, cmd_list, scene_graph, data, frame_idx); } data.previous_frame_index = n_render_system.GetFrameIdx(); } inline void DestroyBuildASTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { } } /* internal */ inline void AddBuildAccelerationStructuresTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ Format::UNKNOWN }), RenderTargetProperties::NumRTVFormats(0), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupBuildASTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteBuildASTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyBuildASTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Acceleration Structure Builder"); frame_graph.UpdateSettings(ASBuildSettings()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_cubemap_convolution.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_defines.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../d3d12/d3d12_model_pool.hpp" #include "../d3d12/d3d12_resource_pool_texture.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../scene_graph/skybox_node.hpp" #include "../pipeline_registry.hpp" #include "../platform_independend_structs.hpp" #include "d3d12_imgui_render_task.hpp" #include "d3d12_equirect_to_cubemap.hpp" namespace wr { struct CubemapConvolutionSettings { struct Runtime { int m_resolution[2] = {128, 128}; }; Runtime m_runtime; }; struct CubemapConvolutionTaskData { d3d12::PipelineState* in_pipeline = nullptr; TextureHandle in_radiance = {}; TextureHandle out_irradiance = {}; std::shared_ptr camera_cb_pool; D3D12ConstantBufferHandle* cb_handle; DirectX::XMMATRIX proj_mat = { DirectX::XMMatrixIdentity() }; DirectX::XMMATRIX view_mat[6] = { }; }; namespace internal { struct ProjectionView_CBuffer { DirectX::XMMATRIX m_projection; DirectX::XMMATRIX m_view[6]; }; inline void SetupCubemapConvolutionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (resize) { return; } auto& data = fg.GetData(handle); auto& ps_registry = PipelineRegistry::Get(); data.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::cubemap_convolution); data.camera_cb_pool = rs.CreateConstantBufferPool(2_mb); data.cb_handle = static_cast(data.camera_cb_pool->Create(sizeof(ProjectionView_CBuffer))); data.proj_mat = DirectX::XMMatrixPerspectiveFovRH(DirectX::XMConvertToRadians(90.0f), 1.0f, 0.1f, 10.0f); data.view_mat[0] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(1.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f)); data.view_mat[1] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(-1.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f)); data.view_mat[2] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f)); data.view_mat[3] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f)); data.view_mat[4] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f)); data.view_mat[5] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 0.0f, -1.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f)); } inline void ExecuteCubemapConvolutionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto settings = fg.GetSettings(handle); auto& pred_data = fg.GetPredecessorData(); auto skybox_node = scene_graph.GetCurrentSkybox(); if (!skybox_node) { return; } //Does it need convolution? And does it have a cubemap already? if (skybox_node->m_irradiance != std::nullopt && skybox_node->m_skybox != std::nullopt) { return; } data.in_radiance = pred_data.out_cubemap; skybox_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);; data.out_irradiance = skybox_node->m_irradiance.value(); d3d12::TextureResource* radiance = static_cast(data.in_radiance.m_pool->GetTextureResource(data.in_radiance)); d3d12::TextureResource* irradiance = static_cast(data.out_irradiance.m_pool->GetTextureResource(data.out_irradiance)); if (radiance->m_is_staged) { if (n_render_system.m_render_window.has_value()) { auto cmd_list = fg.GetCommandList(handle); const auto viewport = d3d12::CreateViewport(static_cast(irradiance->m_width), static_cast(irradiance->m_height)); const auto frame_idx = n_render_system.GetRenderWindow()->m_frame_idx; d3d12::BindViewport(cmd_list, viewport); d3d12::BindPipeline(cmd_list, data.in_pipeline); d3d12::SetPrimitiveTopology(cmd_list, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ProjectionView_CBuffer cb_data; cb_data.m_view[0] = data.view_mat[0]; cb_data.m_view[1] = data.view_mat[1]; cb_data.m_view[2] = data.view_mat[2]; cb_data.m_view[3] = data.view_mat[3]; cb_data.m_view[4] = data.view_mat[4]; cb_data.m_view[5] = data.view_mat[5]; cb_data.m_projection = data.proj_mat; data.cb_handle->m_pool->Update(data.cb_handle, sizeof(ProjectionView_CBuffer), 0, frame_idx, (uint8_t*)&cb_data); d3d12::BindConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx); for (uint32_t i = 0; i < 6; ++i) { //Get render target handle. d3d12::DescHeapCPUHandle rtv_handle = irradiance->m_rtv_allocation->GetDescriptorHandle(i); cmd_list->m_native->OMSetRenderTargets(1, &rtv_handle.m_native, false, nullptr); d3d12::Bind32BitConstants(cmd_list, &i, 1, 0, 0); //bind cube and render Model* cube_model = rs.GetSimpleShape(RenderSystem::SimpleShapes::CUBE); //Render meshes for (auto& mesh : cube_model->m_meshes) { auto n_mesh = static_cast(cube_model->m_model_pool)->GetMeshData(mesh.first->id); D3D12ModelPool* model_pool = static_cast(cube_model->m_model_pool); d3d12::BindVertexBuffer(cmd_list, static_cast(cube_model->m_model_pool)->GetVertexStagingBuffer(), 0, model_pool->GetVertexStagingBuffer()->m_size, n_mesh->m_vertex_staging_buffer_stride); d3d12::BindIndexBuffer(cmd_list, static_cast(cube_model->m_model_pool)->GetIndexStagingBuffer(), 0, static_cast(model_pool->GetIndexStagingBuffer()->m_size)); constexpr unsigned int env_idx = rs_layout::GetHeapLoc(params::cubemap_convolution, params::CubemapConvolutionE::ENVIRONMENT_CUBEMAP); d3d12::SetShaderSRV(cmd_list, 2, env_idx, radiance); d3d12::BindDescriptorHeaps(cmd_list); if (n_mesh->m_index_count != 0) { d3d12::DrawIndexed(cmd_list, static_cast(n_mesh->m_index_count), 1, static_cast(n_mesh->m_index_staging_buffer_offset), static_cast(n_mesh->m_vertex_staging_buffer_offset)); } else { d3d12::Draw(cmd_list, static_cast(n_mesh->m_vertex_count), 1, static_cast(n_mesh->m_vertex_staging_buffer_offset)); } } } d3d12::Transition(cmd_list, irradiance, irradiance->m_subresource_states[0], ResourceState::PIXEL_SHADER_RESOURCE); //Once we're done we can mark the equirectangular texture for deletion in the next frame as it won't be used anymore //Mark for unload makes the m_hdr handle invalid, if it's used anywhere else the program will probably break. //If users want to keep using the equirectangular texture afterward, the following line of code can be removed. skybox_node->m_hdr.m_pool->MarkForUnload(skybox_node->m_hdr, frame_idx); fg.SetShouldExecute(handle, false); } } //if (data.should_run) } } /* internal */ inline void AddCubemapConvolutionTask(FrameGraph& fg) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET), RenderTargetProperties::FinishedResourceState(ResourceState::NON_PIXEL_SHADER_RESOURCE), RenderTargetProperties::CreateDSVBuffer(true), RenderTargetProperties::DSVFormat(Format::D32_FLOAT), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(true), RenderTargetProperties::ClearDepth(true), }; RenderTaskDesc desc; desc.m_setup_func = [&](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupCubemapConvolutionTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteCubemapConvolutionTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::DIRECT; desc.m_allow_multithreading = true; fg.AddTask(desc, L"Cubemap Convolution"); fg.UpdateSettings(CubemapConvolutionSettings()); } } /* wr */ /* OLD COMPUTE CODE, KEEPING IT HERE FOR POSSIBLE FUTURE USAGE. */ /* struct CubemapConvolutionTaskData { D3D12Pipeline* in_pipeline; d3d12::TextureResource* in_radiance; //The skybox hdr cubemap d3d12::TextureResource* out_irradiance; bool should_run = true; }; namespace internal { inline void SetupCubemapConvolutionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, TextureHandle in_radiance, TextureHandle out_irradiance) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto& ps_registry = PipelineRegistry::Get(); data.in_pipeline = (D3D12Pipeline*)ps_registry.Find(pipelines::cubemap_convolution); data.in_radiance = static_cast(in_radiance.m_pool->GetTexture(in_radiance.m_id)); data.out_irradiance = static_cast(out_irradiance.m_pool->GetTexture(out_irradiance.m_id)); D3D12TexturePool* pool = static_cast(out_irradiance.m_pool); d3d12::DescHeapCPUHandle uav_handle = data.out_irradiance->m_uav_allocation.GetDescriptorHandle(); d3d12::CreateUAVFromTexture(data.out_irradiance, uav_handle, 0); } inline void ExecuteCubemapConvolutionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); if (data.should_run) { if (n_render_system.m_render_window.has_value()) { auto cmd_list = fg.GetCommandList(handle); const auto frame_idx = n_render_system.GetFrameIdx(); d3d12::BindComputePipeline(cmd_list, data.in_pipeline->m_native); //Transition and bind SRV d3d12::Transition(cmd_list, data.in_radiance, data.in_radiance->m_current_state, ResourceState::PIXEL_SHADER_RESOURCE); d3d12::SetShaderTexture(cmd_list, 0, 0, data.in_radiance); //Transition and bind UAV d3d12::Transition(cmd_list, data.out_irradiance, data.out_irradiance->m_current_state, ResourceState::UNORDERED_ACCESS); d3d12::SetShaderUAV(cmd_list, 0, 1, data.out_irradiance); d3d12::BindDescriptorHeaps(cmd_list, frame_idx); d3d12::Dispatch(cmd_list, data.out_irradiance->m_width/32, data.out_irradiance->m_height/32, 6); d3d12::Transition(cmd_list, data.out_irradiance, data.out_irradiance->m_current_state, ResourceState::PIXEL_SHADER_RESOURCE); data.should_run = false; } } } } */ ================================================ FILE: src/render_tasks/d3d12_deferred_composition.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../render_tasks/d3d12_deferred_composition.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../scene_graph/camera_node.hpp" #include "../scene_graph/skybox_node.hpp" #include "../engine_registry.hpp" #include "../render_tasks/d3d12_brdf_lut_precalculation.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_spatial_reconstruction.hpp" #include "../render_tasks/d3d12_reflection_denoiser.hpp" #include "../render_tasks/d3d12_cubemap_convolution.hpp" #include "../render_tasks/d3d12_rt_shadow_task.hpp" #include "../render_tasks/d3d12_rt_reflection_task.hpp" #include "../render_tasks/d3d12_shadow_denoiser_task.hpp" #include "../render_tasks/d3d12_path_tracer.hpp" #include "../render_tasks/d3d12_accumulation.hpp" #include "../render_tasks/d3d12_rtao_task.hpp" #include "d3d12_hbao.hpp" namespace wr { namespace internal { void RecordDrawCommands(D3D12RenderSystem& render_system, d3d12::CommandList* cmd_list, d3d12::HeapResource* camera_cb, wr::DeferredCompositionTaskData const& data, unsigned int frame_idx) { auto& n_render_system = static_cast(render_system); d3d12::BindComputePipeline(cmd_list, data.in_pipeline); bool is_fallback = d3d12::GetRaytracingType(render_system.m_device) == RaytracingType::FALLBACK; d3d12::BindDescriptorHeaps(cmd_list, is_fallback); d3d12::BindComputeConstantBuffer(cmd_list, camera_cb, 0, frame_idx); constexpr unsigned int albedo_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::GBUFFER_ALBEDO_ROUGHNESS); d3d12::DescHeapCPUHandle albedo_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle(frame_idx); d3d12::SetShaderSRV(cmd_list, 1, albedo_loc, albedo_handle); constexpr unsigned int normal_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::GBUFFER_NORMAL_METALLIC); d3d12::DescHeapCPUHandle normal_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(frame_idx); d3d12::SetShaderSRV(cmd_list, 1, normal_loc, normal_handle); constexpr unsigned int emissive_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::GBUFFER_EMISSIVE_AO); d3d12::DescHeapCPUHandle emissive_handle = data.out_gbuffer_emissive_alloc.GetDescriptorHandle(frame_idx); d3d12::SetShaderSRV(cmd_list, 1, emissive_loc, emissive_handle); constexpr unsigned int depth_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::GBUFFER_DEPTH); d3d12::DescHeapCPUHandle depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 1, depth_loc, depth_handle); constexpr unsigned int lights_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::LIGHT_BUFFER); d3d12::DescHeapCPUHandle lights_handle = data.out_lights_alloc.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 1, lights_loc, lights_handle); constexpr unsigned int skybox = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::SKY_BOX); d3d12::SetShaderSRV(cmd_list, 1, skybox, data.out_skybox); constexpr unsigned int irradiance = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::IRRADIANCE_MAP); d3d12::SetShaderSRV(cmd_list, 1, irradiance, data.out_irradiance); constexpr unsigned int pref_env = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::PREF_ENV_MAP); d3d12::SetShaderSRV(cmd_list, 1, pref_env, data.out_pref_env_map); constexpr unsigned int brdf_lut_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BRDF_LUT); auto* brdf_lut_text = static_cast(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value())); d3d12::SetShaderSRV(cmd_list, 1, brdf_lut_loc, brdf_lut_text); constexpr unsigned int reflection = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_REFLECTION); d3d12::DescHeapCPUHandle reflection_handle = data.out_buffer_refl_alloc.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 1, reflection, reflection_handle); constexpr unsigned int shadow = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_SHADOW); d3d12::DescHeapCPUHandle shadow_handle = data.out_buffer_shadow_alloc.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 1, shadow, shadow_handle); constexpr unsigned int sp_irradiance_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_SCREEN_SPACE_IRRADIANCE); d3d12::DescHeapCPUHandle sp_irradiance_handle = data.out_screen_space_irradiance_alloc.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 1, sp_irradiance_loc, sp_irradiance_handle); constexpr unsigned int ao_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_AO); d3d12::DescHeapCPUHandle ao_handle = data.out_screen_space_ao_alloc.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 1, ao_loc, ao_handle); constexpr unsigned int output_loc = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::OUTPUT); d3d12::DescHeapCPUHandle output_handle = data.out_output_alloc.GetDescriptorHandle(); d3d12::SetShaderUAV(cmd_list, 1, output_loc, output_handle); d3d12::Dispatch(cmd_list, static_cast(std::ceil(render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(render_system.m_viewport.m_viewport.Height / 16.f)), 1); } void SetupDeferredCompositionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto& ps_registry = PipelineRegistry::Get(); data.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::deferred_composition); // Check if the current frame graph contains the hybrid task to know if it is hybrid or not. data.is_path_tracer = fg.HasTask(); data.is_rtao = fg.HasTask(); data.is_hbao = fg.HasTask() && !data.is_rtao; //Don't use HBAO when RTAO is active data.is_hybrid = fg.HasTask() || fg.HasTask() || fg.HasTask(); data.has_rt_shadows = fg.HasTask(); data.has_rt_reflection = fg.HasTask(); //Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors std::shared_ptr texture_pool = std::static_pointer_cast(n_render_system.m_texture_pools[0]); if (!texture_pool) { LOGC("Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool"); } if (!resize) { data.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_gbuffer_albedo_alloc = std::move(data.out_allocator->Allocate(d3d12::settings::num_back_buffers)); data.out_gbuffer_normal_alloc = std::move(data.out_allocator->Allocate(d3d12::settings::num_back_buffers)); data.out_gbuffer_emissive_alloc = std::move(data.out_allocator->Allocate(d3d12::settings::num_back_buffers)); data.out_gbuffer_depth_alloc = std::move(data.out_allocator->Allocate()); data.out_lights_alloc = std::move(data.out_allocator->Allocate()); data.out_buffer_refl_alloc = std::move(data.out_allocator->Allocate()); data.out_buffer_shadow_alloc = std::move(data.out_allocator->Allocate()); data.out_screen_space_irradiance_alloc = std::move(data.out_allocator->Allocate()); data.out_screen_space_ao_alloc = std::move(data.out_allocator->Allocate()); data.out_output_alloc = std::move(data.out_allocator->Allocate()); } for (uint32_t i = 0; i < d3d12::settings::num_back_buffers; ++i) { auto albedo_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle(i); auto normal_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(i); auto emissive_handle = data.out_gbuffer_emissive_alloc.GetDescriptorHandle(i); auto depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle(); auto deferred_main_rt = data.out_deferred_main_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, albedo_handle, 0, deferred_main_rt->m_create_info.m_rtv_formats[0]); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats[1]); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, emissive_handle, 2, deferred_main_rt->m_create_info.m_rtv_formats[2]); d3d12::CreateSRVFromDSV(deferred_main_rt, depth_handle); // Bind output(s) from hybrid render task, if the composition task is executed in the hybrid frame graph if (data.is_hybrid) { constexpr auto reflection_id = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_REFLECTION); auto reflection_handle = data.out_buffer_refl_alloc.GetDescriptorHandle(); if (fg.HasTask()) { auto reflection_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(reflection_rt, reflection_handle, 0, reflection_rt->m_create_info.m_rtv_formats[0]); } if (fg.HasTask()) { auto reflection_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(reflection_rt, reflection_handle, 0, reflection_rt->m_create_info.m_rtv_formats[0]); } else if (data.has_rt_reflection) { auto reflection_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(reflection_rt, reflection_handle, 0, reflection_rt->m_create_info.m_rtv_formats[0]); } else if(data.has_rt_shadows) { auto reflection_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(reflection_rt, reflection_handle, 0, reflection_rt->m_create_info.m_rtv_formats[0]); } constexpr auto shadow_id = rs_layout::GetHeapLoc(params::deferred_composition, params::DeferredCompositionE::BUFFER_SHADOW); auto shadow_handle = data.out_buffer_shadow_alloc.GetDescriptorHandle(); if (fg.HasTask()) { auto shadow_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(shadow_rt, shadow_handle, 0, shadow_rt->m_create_info.m_rtv_formats[0]); data.has_rt_shadows_denoiser = true; } else if(fg.HasTask()) { auto shadow_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(shadow_rt, shadow_handle, 0, shadow_rt->m_create_info.m_rtv_formats[0]); } else if(data.has_rt_reflection) { auto shadow_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(shadow_rt, shadow_handle, 0, shadow_rt->m_create_info.m_rtv_formats[0]); } if (data.is_rtao) { auto ao_handle = data.out_screen_space_ao_alloc.GetDescriptorHandle(); auto ao_buffer = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromRTV(ao_buffer, ao_handle, 1, ao_buffer->m_create_info.m_rtv_formats.data()); } } } } void ExecuteDeferredCompositionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto cmd_list = fg.GetCommandList(handle); auto render_target = fg.GetRenderTarget(handle); fg.WaitForPredecessorTask(); if (data.is_hybrid) { if (data.has_rt_reflection) { // Wait on rt reflection task const auto& reflection_data = fg.GetPredecessorData(); } if (data.has_rt_shadows) { // Wait on rt shadow task const auto& shadow_data = fg.GetPredecessorData(); } if (data.has_rt_shadows_denoiser) { // Wait on shadow denoiser task const auto& denoiser_data = fg.GetPredecessorData(); } } if (n_render_system.m_render_window.has_value()) { const auto viewport = n_render_system.m_viewport; const auto frame_idx = n_render_system.GetFrameIdx(); // Update camera constant buffer pool auto active_camera = scene_graph.GetActiveCamera(); temp::ProjectionView_CBData camera_data{}; camera_data.m_projection = active_camera->m_projection; camera_data.m_inverse_projection = active_camera->m_inverse_projection; camera_data.m_prev_projection = active_camera->m_prev_projection; camera_data.m_view = active_camera->m_view; camera_data.m_inverse_view = active_camera->m_inverse_view; camera_data.m_prev_view = active_camera->m_prev_view; camera_data.m_is_hybrid = data.is_hybrid; camera_data.m_has_reflections = data.has_rt_reflection; camera_data.m_has_shadows = data.has_rt_shadows || data.has_rt_shadows_denoiser; camera_data.m_is_path_tracer = data.is_path_tracer; if (data.is_rtao) { camera_data.m_is_ao = true; } else { #ifdef NVIDIA_GAMEWORKS_HBAO camera_data.m_is_ao = data.is_hbao; #else camera_data.m_is_ao = false; #endif } active_camera->m_camera_cb->m_pool->Update(active_camera->m_camera_cb, sizeof(temp::ProjectionView_CBData), 0, (uint8_t*)&camera_data); const auto camera_cb = static_cast(active_camera->m_camera_cb); if (static_cast(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE) { static_cast(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE); return; } //Get light buffer { d3d12::DescHeapCPUHandle srv_struct_buffer_handle = data.out_lights_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromStructuredBuffer(static_cast(scene_graph.GetLightBuffer())->m_native, srv_struct_buffer_handle, frame_idx); } //GetSkybox auto skybox = scene_graph.GetCurrentSkybox(); if (skybox) { //data.out_skybox = static_cast(pred_data.in_radiance.m_pool->GetTexture(pred_data.in_radiance.m_id)); data.out_skybox = static_cast(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value())); d3d12::CreateSRVFromTexture(data.out_skybox); data.out_irradiance = static_cast(skybox->m_irradiance->m_pool->GetTextureResource(skybox->m_irradiance.value())); d3d12::CreateSRVFromTexture(data.out_irradiance); data.out_pref_env_map = static_cast(skybox->m_prefiltered_env_map->m_pool->GetTextureResource(skybox->m_prefiltered_env_map.value())); d3d12::CreateSRVFromTexture(data.out_pref_env_map); } // Get Screen Space Environment Texture if (data.is_path_tracer) { d3d12::DescHeapCPUHandle irradiance_handle = data.out_screen_space_irradiance_alloc.GetDescriptorHandle(); d3d12::RenderTarget* pred_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(pred_rt, irradiance_handle, 0, pred_rt->m_create_info.m_rtv_formats[0]); } // Get HBAO+ Texture if (data.is_hbao) { d3d12::DescHeapCPUHandle desc_handle = data.out_screen_space_ao_alloc.GetDescriptorHandle(); d3d12::RenderTarget* ao_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(ao_rt, desc_handle, 0, ao_rt->m_create_info.m_rtv_formats[0]); } // Output UAV { d3d12::DescHeapCPUHandle rtv_out_uav_handle = data.out_output_alloc.GetDescriptorHandle(); std::vector formats = { Format::R16G16B16A16_FLOAT }; d3d12::CreateUAVFromRTV(render_target, rtv_out_uav_handle, 1, formats.data()); } if constexpr (d3d12::settings::use_bundles) { // Record all bundles again if required. if (data.out_requires_bundle_recording) { for (auto i = 0; i < data.out_bundle_cmd_lists.size(); i++) { d3d12::Begin(data.out_bundle_cmd_lists[i], 0); RecordDrawCommands(n_render_system, data.out_bundle_cmd_lists[i], static_cast(camera_cb)->m_native, data, i); d3d12::End(data.out_bundle_cmd_lists[i]); } data.out_requires_bundle_recording = false; } } //Render deferred d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::BindViewport(cmd_list, viewport); d3d12::Transition(cmd_list, render_target, wr::ResourceState::COPY_SOURCE, wr::ResourceState::UNORDERED_ACCESS); if constexpr (d3d12::settings::use_bundles) { bool is_fallback = d3d12::GetRaytracingType(n_render_system.m_device) == RaytracingType::FALLBACK; d3d12::BindDescriptorHeaps(cmd_list, is_fallback); d3d12::ExecuteBundle(cmd_list, data.out_bundle_cmd_lists[frame_idx]); } else { RecordDrawCommands(n_render_system, cmd_list, static_cast(camera_cb)->m_native, data, frame_idx); } d3d12::Transition(cmd_list, render_target, wr::ResourceState::UNORDERED_ACCESS, wr::ResourceState::COPY_SOURCE); d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); } } void DestroyDeferredCompositionTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { } } void AddDeferredCompositionTask(FrameGraph& fg, std::optional target_width, std::optional target_height) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(target_width), RenderTargetProperties::Height(target_height), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDeferredCompositionTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDeferredCompositionTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyDeferredCompositionTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; fg.AddTask(desc, L"Deferred Composition", FG_DEPS()); } } ================================================ FILE: src/render_tasks/d3d12_deferred_composition.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../pipeline_registry.hpp" #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_descriptors_allocations.hpp" #include "../d3d12/d3d12_texture_resources.hpp" namespace wr { struct DeferredCompositionTaskData { d3d12::PipelineState* in_pipeline = nullptr; d3d12::RenderTarget* out_deferred_main_rt = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_gbuffer_albedo_alloc; DescriptorAllocation out_gbuffer_normal_alloc; DescriptorAllocation out_gbuffer_emissive_alloc; DescriptorAllocation out_gbuffer_depth_alloc; DescriptorAllocation out_lights_alloc; DescriptorAllocation out_buffer_refl_alloc; DescriptorAllocation out_buffer_shadow_alloc; DescriptorAllocation out_screen_space_irradiance_alloc; DescriptorAllocation out_screen_space_ao_alloc; DescriptorAllocation out_output_alloc; d3d12::TextureResource* out_skybox = nullptr; d3d12::TextureResource* out_irradiance = nullptr; d3d12::TextureResource* out_pref_env_map = nullptr; std::array out_bundle_cmd_lists = {}; bool out_requires_bundle_recording = false; bool is_hybrid = false; bool has_rt_reflection = false; bool has_rt_shadows = false; bool has_rt_shadows_denoiser = false; bool is_path_tracer = false; bool is_rtao = false; bool is_hbao = false; }; namespace internal { void RecordDrawCommands(D3D12RenderSystem& render_system, d3d12::CommandList* cmd_list, d3d12::HeapResource* camera_cb, DeferredCompositionTaskData const & data, unsigned int frame_idx); void SetupDeferredCompositionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle); void ExecuteDeferredCompositionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle); void DestroyDeferredCompositionTask(FrameGraph& fg, RenderTaskHandle handle); } void AddDeferredCompositionTask(FrameGraph& fg, std::optional target_width, std::optional target_height); } ================================================ FILE: src/render_tasks/d3d12_deferred_main.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../d3d12/d3d12_model_pool.hpp" #include "../d3d12/d3d12_resource_pool_texture.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../pipeline_registry.hpp" #include "../engine_registry.hpp" #include "../platform_independend_structs.hpp" #include "d3d12_imgui_render_task.hpp" #include "../scene_graph/camera_node.hpp" namespace wr { struct DeferredMainTaskData { d3d12::PipelineState* in_pipeline; bool is_hybrid; }; namespace internal { inline void SetupDeferredTask(RenderSystem&, FrameGraph& fg, RenderTaskHandle handle, bool resized, bool is_hybrid) { auto& data = fg.GetData(handle); auto& ps_registry = PipelineRegistry::Get(); data.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(is_hybrid ? pipelines::basic_hybrid : pipelines::basic_deferred); data.is_hybrid = is_hybrid; } inline void ExecuteDeferredTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto cmd_list = fg.GetCommandList(handle); if (n_render_system.m_render_window.has_value()) { const auto viewport = n_render_system.m_viewport; const auto frame_idx = n_render_system.GetRenderWindow()->m_frame_idx; d3d12::BindViewport(cmd_list, viewport); d3d12::BindPipeline(cmd_list, data.in_pipeline); d3d12::SetPrimitiveTopology(cmd_list, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); auto d3d12_cb_handle = static_cast(scene_graph.GetActiveCamera()->m_camera_cb); d3d12::BindConstantBuffer(cmd_list, d3d12_cb_handle->m_native, 0, frame_idx); scene_graph.Render(cmd_list, scene_graph.GetActiveCamera().get()); } } } /* internal */ inline void AddDeferredMainTask(FrameGraph& fg, std::optional target_width, std::optional target_height, bool is_hybrid) { std::wstring name(L"Deferred Main"); RenderTargetProperties rt_properties_deferred { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(target_width), RenderTargetProperties::Height(target_height), RenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET), RenderTargetProperties::FinishedResourceState(ResourceState::NON_PIXEL_SHADER_RESOURCE), RenderTargetProperties::CreateDSVBuffer(true), RenderTargetProperties::DSVFormat(Format::D32_FLOAT), RenderTargetProperties::RTVFormats({ Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT}), RenderTargetProperties::NumRTVFormats(3), RenderTargetProperties::Clear(true), RenderTargetProperties::ClearDepth(true), }; RenderTargetProperties rt_properties_hybrid { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(target_width), RenderTargetProperties::Height(target_height), RenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET), RenderTargetProperties::FinishedResourceState(ResourceState::NON_PIXEL_SHADER_RESOURCE), RenderTargetProperties::CreateDSVBuffer(true), RenderTargetProperties::DSVFormat(Format::D32_FLOAT), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT, wr::Format::R16G16B16A16_FLOAT, Format::R16G16B16A16_FLOAT, wr::Format::R16G16B16A16_FLOAT, wr::Format::R32G32B32A32_FLOAT, wr::Format::R32G32B32A32_FLOAT }), RenderTargetProperties::NumRTVFormats(6), RenderTargetProperties::Clear(true), RenderTargetProperties::ClearDepth(true) }; RenderTaskDesc desc; desc.m_setup_func = [is_hybrid](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resized) { internal::SetupDeferredTask(rs, fg, handle, resized, is_hybrid); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDeferredTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph&, RenderTaskHandle, bool) { // Nothing to destroy }; desc.m_properties = is_hybrid ? rt_properties_hybrid : rt_properties_deferred; desc.m_type = RenderTaskType::DIRECT; desc.m_allow_multithreading = true; fg.AddTask(desc, L"Deferred Main"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_deferred_render_target_copy.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../frame_graph/frame_graph.hpp" #include #include #include namespace wr { struct RenderTargetCopyTaskData { d3d12::RenderTarget* out_rt; }; namespace internal { template inline void SetupCopyTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle) { auto& data = fg.GetData(handle); data.out_rt = static_cast(fg.GetPredecessorRenderTarget()); } inline void ExecuteCopyTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto cmd_list = fg.GetCommandList(handle); auto render_target = fg.GetRenderTarget(handle); const auto frame_idx = n_render_system.GetFrameIdx(); D3D12_TEXTURE_COPY_LOCATION dst = {}; dst.pResource = render_target->m_render_targets[frame_idx % render_target->m_render_targets.size()]; dst.Type = D3D12_TEXTURE_COPY_TYPE::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; dst.SubresourceIndex = 0; D3D12_TEXTURE_COPY_LOCATION src = {}; src.pResource = data.out_rt->m_render_targets[0]; src.Type = D3D12_TEXTURE_COPY_TYPE::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; src.SubresourceIndex = 0; cmd_list->m_native->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); } } /* internal */ template inline void AddRenderTargetCopyTask(FrameGraph& frame_graph) { std::string name_temp = std::string("Render Target (") + std::string(typeid(T).name()) + std::string(") Copy task"); std::wstring w_name = std::wstring_convert>().from_bytes(name_temp); RenderTargetProperties rt_properties{ RenderTargetProperties::IsRenderWindow(true), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::COPY_DEST), RenderTargetProperties::FinishedResourceState(ResourceState::PRESENT), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ Format::R8G8B8A8_UNORM }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool) { internal::SetupCopyTask(rs, fg, handle); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteCopyTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph&, RenderTaskHandle, bool) { // Nothing to destroy }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COPY; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, w_name, FG_DEPS()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_dof_bokeh.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct DoFBokehData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::RenderTarget* out_source_coc_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; std::shared_ptr camera_cb_pool; D3D12ConstantBufferHandle* cb_handle = nullptr; }; namespace internal { struct BokehShapeModifier { float x = 1.0f; float y = 1.0f; }; struct Bokeh_CB { float m_f_number = 0.0f; float m_shape = 0.0f; float m_bokeh_poly = 0.0f; uint32_t m_blades = 0u; float _padding; BokehShapeModifier m_bokeh_shape_modifier; int m_enable_dof = false; }; template inline void SetupDoFBokehTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(5); data.camera_cb_pool = rs.CreateConstantBufferPool(2); data.cb_handle = static_cast(data.camera_cb_pool->Create(sizeof(Bokeh_CB))); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_bokeh)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); auto source_coc_rt = data.out_source_coc_rt = static_cast(fg.GetPredecessorRenderTarget()); for (auto frame_idx = 0; frame_idx < versions; frame_idx++) { // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_NEAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Destination far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_FAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_NEAR))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Source far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_FAR))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]); } // Dilated COC { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::COC))); d3d12::CreateSRVFromSpecificRTV(source_coc_rt, cpu_handle, 0, source_coc_rt->m_create_info.m_rtv_formats[0]); } } } template inline void ExecuteDoFBokehTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); auto source_coc_rt = data.out_source_coc_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_NEAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Destination far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_FAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_NEAR))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Source far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_FAR))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]); } // Dilated COC { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::COC))); d3d12::CreateSRVFromSpecificRTV(source_coc_rt, cpu_handle, 0, source_coc_rt->m_create_info.m_rtv_formats[0]); } d3d12::BindComputePipeline(cmd_list, data.out_pipeline); BokehShapeModifier shape_modifier; Bokeh_CB cb_data; cb_data.m_f_number = sg.GetActiveCamera()->m_f_number; cb_data.m_shape = sg.GetActiveCamera()->m_shape_amt; cb_data.m_blades = sg.GetActiveCamera()->m_aperture_blades; cb_data.m_bokeh_poly = sqrt(std::clamp((sg.GetActiveCamera()->m_f_number - 1.8f) / (7.0f - 1.8f), 0.f, 1.f)); cb_data.m_enable_dof = sg.GetActiveCamera()->m_enable_dof; cb_data.m_bokeh_shape_modifier = shape_modifier; data.cb_handle->m_pool->Update(data.cb_handle, sizeof(Bokeh_CB), 0, frame_idx, (uint8_t*)&cb_data); d3d12::BindComputeConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx); { constexpr unsigned int dest_n_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_NEAR); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_n_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_n_idx, handle_uav); } { constexpr unsigned int dest_f_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::OUTPUT_FAR); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_f_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_f_idx, handle_uav); } { constexpr unsigned int source_near_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_NEAR); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_near_idx); d3d12::SetShaderSRV(cmd_list, 0, source_near_idx, handle_m_srv); } { constexpr unsigned int source_far_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::SOURCE_FAR); auto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_far_idx); d3d12::SetShaderSRV(cmd_list, 0, source_far_idx, handle_b_srv); } { constexpr unsigned int source_coc_idx = rs_layout::GetHeapLoc(params::dof_bokeh, params::DoFBokehE::COC); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_coc_idx); d3d12::SetShaderSRV(cmd_list, 0, source_coc_idx, handle_m_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); //TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyDoFBokehTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); data.camera_cb_pool->Destroy(data.cb_handle); delete data.out_allocator; } } } /* internal */ template inline void AddDoFBokehTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}), RenderTargetProperties::NumRTVFormats(2), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.5f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDoFBokehTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDoFBokehTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyDoFBokehTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"DoF Bokeh Pass"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_dof_bokeh_postfilter.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct DoFBokehPostFilterData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { template inline void SetupDoFBokehPostFilterTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(4); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_bokeh_post_filter)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_NEAR))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Source far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_FAR))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]); } // Destination near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_NEAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Destination far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_FAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } } template inline void ExecuteDoFBokehPostFilterTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_NEAR))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Source far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_FAR))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 1, source_rt->m_create_info.m_rtv_formats[1]); } // Destination near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_NEAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Destination far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_FAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } d3d12::BindComputePipeline(cmd_list, data.out_pipeline); { constexpr unsigned int dest_n_idx = rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_NEAR); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_n_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_n_idx, handle_uav); } { constexpr unsigned int dest_f_idx = rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::OUTPUT_FAR); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_f_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_f_idx, handle_uav); } { constexpr unsigned int source_near_idx = rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_NEAR); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_near_idx); d3d12::SetShaderSRV(cmd_list, 0, source_near_idx, handle_m_srv); } { constexpr unsigned int source_far_idx = rs_layout::GetHeapLoc(params::dof_bokeh_post_filter, params::DoFBokehPostFilterE::SOURCE_FAR); auto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_far_idx); d3d12::SetShaderSRV(cmd_list, 0, source_far_idx, handle_b_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); //TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyDoFBokehPostFilterTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddDoFBokehPostFilterTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(2), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.5f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDoFBokehPostFilterTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDoFBokehPostFilterTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyDoFBokehPostFilterTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"DoF Bokeh Post Filter"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_dof_coc.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct DoFCoCData { d3d12::RenderTarget* out_source_dsv = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; std::shared_ptr camera_cb_pool; D3D12ConstantBufferHandle* cb_handle = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { struct DoFProperties_CB { DirectX::XMMATRIX m_projection = DirectX::XMMatrixIdentity(); float m_focal_length = 0.0f; float m_f_number = 0.0f; float m_film_size = 0.0f; float m_focus_dist = 0.0f; float m_pad[2]; int m_enable_dof = 0; float m_dof_range = 1.0f; }; template inline void SetupDoFCoCTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(2); data.camera_cb_pool = rs.CreateConstantBufferPool(2); data.cb_handle = static_cast(data.camera_cb_pool->Create(sizeof(DoFProperties_CB))); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_coc)); auto source_dsv = data.out_source_dsv = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::GDEPTH))); d3d12::CreateSRVFromDSV(source_dsv, cpu_handle); } } template inline void ExecuteDoFCoCTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; data.out_source_dsv = static_cast(fg.GetPredecessorRenderTarget()); const auto camera_cb = static_cast(sg.GetActiveCamera()->m_camera_cb); DoFProperties_CB cb_data; cb_data.m_projection = sg.GetActiveCamera()->m_projection; cb_data.m_film_size = sg.GetActiveCamera()->m_film_size; cb_data.m_focal_length = sg.GetActiveCamera()->m_focal_length; cb_data.m_f_number = sg.GetActiveCamera()->m_f_number; cb_data.m_focus_dist = sg.GetActiveCamera()->m_focus_dist; cb_data.m_enable_dof = sg.GetActiveCamera()->m_enable_dof; cb_data.m_dof_range = sg.GetActiveCamera()->m_dof_range; data.cb_handle->m_pool->Update(data.cb_handle, sizeof(DoFProperties_CB), 0, frame_idx, (uint8_t*)&cb_data); auto source_dsv = data.out_source_dsv = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::GDEPTH))); d3d12::CreateSRVFromDSV(source_dsv, cpu_handle); } d3d12::BindComputePipeline(cmd_list, data.out_pipeline); d3d12::BindComputeConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx); { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::OUTPUT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_coc, params::DoFCoCE::GDEPTH); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_m_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_dsv->m_render_targets[frame_idx % versions])); //TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyCoCTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); // Small hack to force the allocations to go out of scope, which will tell the allocator to free them DescriptorAllocation temp1 = std::move(data.out_allocation); data.camera_cb_pool->Destroy(data.cb_handle); delete data.out_allocator; } } } /* internal */ template inline void AddDoFCoCTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDoFCoCTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDoFCoCTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyCoCTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"DoF Cone of Confusion"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_dof_composition.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_deferred_composition.hpp" #include "../render_tasks/d3d12_dof_bokeh_postfilter.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct DoFCompositionData { d3d12::RenderTarget* out_source_rt_comp = nullptr; d3d12::RenderTarget* out_source_bokeh_filtered = nullptr; d3d12::RenderTarget* out_source_coc = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { template inline void SetupDoFCompositionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(5); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_composition)); auto source_rt_comp = data.out_source_rt_comp = static_cast(fg.GetPredecessorRenderTarget()); auto source_rt_bokeh_filtered = data.out_source_bokeh_filtered = static_cast(fg.GetPredecessorRenderTarget()); auto source_coc = data.out_source_coc = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt_comp, cpu_handle, 0, source_rt_comp->m_create_info.m_rtv_formats[0]); } // Bokeh near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_NEAR))); d3d12::CreateSRVFromSpecificRTV(source_rt_bokeh_filtered, cpu_handle, 0, source_rt_bokeh_filtered->m_create_info.m_rtv_formats[0]); } // Bokeh far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_FAR))); d3d12::CreateSRVFromSpecificRTV(source_rt_bokeh_filtered, cpu_handle, 1, source_rt_bokeh_filtered->m_create_info.m_rtv_formats[1]); } // Cone of confusion { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::COC))); d3d12::CreateSRVFromSpecificRTV(source_coc, cpu_handle, 0, source_coc->m_create_info.m_rtv_formats[0]); } } template inline void ExecuteDoFCompositionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; d3d12::BindComputePipeline(cmd_list, data.out_pipeline); auto source_rt_comp = data.out_source_rt_comp = static_cast(fg.GetPredecessorRenderTarget()); auto source_rt_bokeh_filtered = data.out_source_bokeh_filtered = static_cast(fg.GetPredecessorRenderTarget()); auto source_coc = data.out_source_coc = static_cast(fg.GetPredecessorRenderTarget()); for (auto frame_idx = 0; frame_idx < versions; frame_idx++) { // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, frame_idx, n_render_target->m_create_info.m_rtv_formats[frame_idx]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt_comp, cpu_handle, 0, source_rt_comp->m_create_info.m_rtv_formats[0]); } // Bokeh near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_NEAR))); d3d12::CreateSRVFromSpecificRTV(source_rt_bokeh_filtered, cpu_handle, 0, source_rt_bokeh_filtered->m_create_info.m_rtv_formats[0]); } // Bokeh far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_FAR))); d3d12::CreateSRVFromSpecificRTV(source_rt_bokeh_filtered, cpu_handle, 1, source_rt_bokeh_filtered->m_create_info.m_rtv_formats[1]); } // Cone of confusion { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::COC))); d3d12::CreateSRVFromSpecificRTV(source_coc, cpu_handle, frame_idx, source_coc->m_create_info.m_rtv_formats[frame_idx]); } } { constexpr unsigned int dest_n_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::OUTPUT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_n_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_n_idx, handle_uav); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::SOURCE); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_m_srv); } { constexpr unsigned int source_near_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_NEAR); auto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_near_idx); d3d12::SetShaderSRV(cmd_list, 0, source_near_idx, handle_b_srv); } { constexpr unsigned int source_far_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::BOKEH_FAR); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_far_idx); d3d12::SetShaderSRV(cmd_list, 0, source_far_idx, handle_m_srv); } { constexpr unsigned int source_coc_idx = rs_layout::GetHeapLoc(params::dof_composition, params::DoFCompositionE::COC); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_coc_idx); d3d12::SetShaderSRV(cmd_list, 0, source_coc_idx, handle_m_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt_comp->m_render_targets[frame_idx % versions])); d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyDoFCompositionTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddDoFCompositionTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT}), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDoFCompositionTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDoFCompositionTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyDoFCompositionTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"DoF Composition"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_dof_compute_near_mask.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct DoFNearMaskData { d3d12::RenderTarget* out_source_dsv = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; std::shared_ptr camera_cb_pool; D3D12ConstantBufferHandle* cb_handle = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { template inline void SetupDoFNearMaskTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(2); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_near_mask)); auto source_dsv = data.out_source_dsv = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::INPUT))); d3d12::CreateSRVFromSpecificRTV(source_dsv, cpu_handle, 0, source_dsv->m_create_info.m_rtv_formats[0]); } } template inline void ExecuteDoFNearMaskTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; data.out_source_dsv = static_cast(fg.GetPredecessorRenderTarget()); auto source_dsv = data.out_source_dsv = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::INPUT))); d3d12::CreateSRVFromSpecificRTV(source_dsv, cpu_handle, 0, source_dsv->m_create_info.m_rtv_formats[0]); } d3d12::BindComputePipeline(cmd_list, data.out_pipeline); { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::OUTPUT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_near_mask, params::DoFNearMaskE::INPUT); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_m_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_dsv->m_render_targets[frame_idx % versions])); d3d12::Dispatch(cmd_list, static_cast(std::ceil((n_render_system.m_viewport.m_viewport.Width + 31.0f )/ 32.f)), static_cast(std::ceil((n_render_system.m_viewport.m_viewport.Height + 31.0f ) / 32.f)), 1); } inline void DestroyDoFNearMaskTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); // Small hack to force the allocations to go out of scope, which will tell the allocator to free them DescriptorAllocation temp1 = std::move(data.out_allocation); delete data.out_allocator; } } } /* internal */ template inline void AddDoFNearMaskTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.03125f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDoFNearMaskTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDoFNearMaskTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyDoFNearMaskTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"DoF near mask"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_dof_dilate_flatten.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct DoFDilateFlattenData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::RenderTarget* out_source_coc_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { template inline void SetupDoFDilateFlattenTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(2); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_dilate_flatten)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } } template inline void ExecuteDoFDilateFlattenTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } d3d12::BindComputePipeline(cmd_list, data.out_pipeline); { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::OUTPUT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_dilate_flatten, params::DoFDilateFlattenE::SOURCE); auto handle_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); //TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyDoFDilateFlattenTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddDoFDilateFlattenTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({wr::Format::R16_FLOAT}), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.125f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDoFDilateFlattenTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDoFDilateFlattenTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyDoFDilateFlattenTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"DoF coc dilate flatten horizontal"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_dof_dilate_flatten_second_pass.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct DoFDilateFlattenHData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::RenderTarget* out_source_coc_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { template inline void SetupDoFDilateFlattenHTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(2); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_dilate_flatten_h)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } } template inline void ExecuteDoFDilateFlattenHTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } d3d12::BindComputePipeline(cmd_list, data.out_pipeline); { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::OUTPUT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_dilate_flatten_h, params::DoFDilateFlattenHE::SOURCE); auto handle_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); //TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyDoFDilateFlattenHTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddDoFDilateFlattenHTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({wr::Format::R16_FLOAT}), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.125f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDoFDilateFlattenHTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDoFDilateFlattenHTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyDoFDilateFlattenHTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"DoF dilate flatten vertical pass"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_dof_dilate_near.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct DoFDilateData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::RenderTarget* out_source_coc_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { template inline void SetupDoFDilateTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(2); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::dof_dilate)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } } template inline void ExecuteDoFDilateTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::OUTPUT))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } d3d12::BindComputePipeline(cmd_list, data.out_pipeline); { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::OUTPUT); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::dof_dilate, params::DoFDilateE::SOURCE); auto handle_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); //TODO: numthreads is currently hardcoded to half resolution, change when dimensions are done properly d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyDoFDilateTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddDoFDilateTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({wr::Format::R16_FLOAT}), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.03125f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDoFDilateTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDoFDilateTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyDoFDilateTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"DoF Dilate"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_down_scale.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_post_processing.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct DownScaleData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::RenderTarget* out_source_emissive = nullptr; d3d12::RenderTarget* out_source_coc = nullptr; d3d12::PipelineState* out_pipeline = nullptr; ID3D12Resource* out_previous = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; namespace internal { template inline void SetupDownScaleTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); if (!resize) { data.out_allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(4); } auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::down_scale)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); auto source_coc = data.out_source_coc = static_cast(fg.GetPredecessorRenderTarget()); // Destination near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_NEAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Destination far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_FAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Cone of confusion { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::COC))); d3d12::CreateSRVFromSpecificRTV(source_coc, cpu_handle, 0, source_coc->m_create_info.m_rtv_formats[0]); } } template inline void ExecuteDownScaleTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); auto source_coc = data.out_source_coc = static_cast(fg.GetPredecessorRenderTarget()); // Destination near { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_NEAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Destination far { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_FAR))); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); } // Source { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::SOURCE))); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } // Cone of confusion { auto cpu_handle = data.out_allocation.GetDescriptorHandle(COMPILATION_EVAL(rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::COC))); d3d12::CreateSRVFromSpecificRTV(source_coc, cpu_handle, 0, source_coc->m_create_info.m_rtv_formats[0]); } d3d12::BindComputePipeline(cmd_list, data.out_pipeline); { constexpr unsigned int dest_n_idx = rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_NEAR); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_n_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_n_idx, handle_uav); } { constexpr unsigned int dest_f_idx = rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::OUTPUT_FAR); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_f_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_f_idx, handle_uav); } { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::SOURCE); auto handle_b_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_b_srv); } { constexpr unsigned int source_coc_idx = rs_layout::GetHeapLoc(params::down_scale, params::DownScaleE::COC); auto handle_m_srv = data.out_allocation.GetDescriptorHandle(source_coc_idx); d3d12::SetShaderSRV(cmd_list, 0, source_coc_idx, handle_m_srv); } cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyDownScaleTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.out_allocator; } } } /* internal */ template inline void AddDownScaleTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT,wr::Format::R16G16B16A16_FLOAT}), RenderTargetProperties::NumRTVFormats(2), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(0.5f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupDownScaleTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteDownScaleTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyDownScaleTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Down Scale"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_equirect_to_cubemap.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_defines.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../d3d12/d3d12_resource_pool_texture.hpp" #include "../d3d12/d3d12_model_pool.hpp" #include "../d3d12/d3d12_resource_pool_texture.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../pipeline_registry.hpp" #include "../engine_registry.hpp" #include "../platform_independend_structs.hpp" #include "d3d12_imgui_render_task.hpp" #include "../scene_graph/camera_node.hpp" namespace wr { struct EquirectToCubemapTaskData { d3d12::PipelineState* in_pipeline = nullptr; TextureHandle in_equirect = {}; TextureHandle out_cubemap = {}; TextureHandle out_pref_env = {}; std::shared_ptr camera_cb_pool; D3D12ConstantBufferHandle* cb_handle = nullptr; DirectX::XMMATRIX proj_mat = { DirectX::XMMatrixIdentity() }; DirectX::XMMATRIX view_mat[6] = {}; std::uint32_t build_frame_idx = -1; }; namespace internal { struct ProjectionView_CB { DirectX::XMMATRIX m_projection; DirectX::XMMATRIX m_view[6]; }; struct PrefilterEnv_CB { DirectX::XMFLOAT2 texture_size; DirectX::XMFLOAT2 skybox_res; float roughness; uint32_t cubemap_face; }; inline void PrefilterCubemapFace(d3d12::TextureResource* source_cubemap, d3d12::TextureResource* dest_cubemap, CommandList* cmd_list, DescriptorAllocator* alloc, unsigned int array_slice) { wr::d3d12::CommandList* d3d12_cmd_list = static_cast(cmd_list); //Create shader resource view for the source texture in the descriptor heap DescriptorAllocation srv_alloc = alloc->Allocate(); d3d12::DescHeapCPUHandle srv_handle = srv_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromTexture(source_cubemap, srv_handle); PrefilterEnv_CB prefilter_env_cb; for (uint32_t src_mip = 0; src_mip < dest_cubemap->m_mip_levels; ++src_mip) { uint32_t width = static_cast(dest_cubemap->m_width) >> src_mip; uint32_t height = static_cast(dest_cubemap->m_height) >> src_mip; prefilter_env_cb.texture_size = DirectX::XMFLOAT2(static_cast(width), static_cast(height)); prefilter_env_cb.skybox_res = DirectX::XMFLOAT2(static_cast(source_cubemap->m_width), static_cast(source_cubemap->m_height)); prefilter_env_cb.roughness = static_cast(src_mip) / static_cast(dest_cubemap->m_mip_levels); prefilter_env_cb.cubemap_face = array_slice; DescriptorAllocation uav_alloc = alloc->Allocate(); d3d12::DescHeapCPUHandle uav_handle = uav_alloc.GetDescriptorHandle(); d3d12::CreateUAVFromCubemapFace(dest_cubemap, uav_handle, src_mip, array_slice); //Set shader variables d3d12::BindCompute32BitConstants(d3d12_cmd_list, &prefilter_env_cb, sizeof(PrefilterEnv_CB) / sizeof(uint32_t), 0, 0); d3d12::Transition(d3d12_cmd_list, source_cubemap, source_cubemap->m_subresource_states[src_mip], ResourceState::PIXEL_SHADER_RESOURCE, src_mip, 1); d3d12::SetShaderSRV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::SOURCE)), srv_handle); d3d12::Transition(d3d12_cmd_list, dest_cubemap, dest_cubemap->m_subresource_states[src_mip], ResourceState::UNORDERED_ACCESS, src_mip, 1); d3d12::SetShaderUAV(d3d12_cmd_list, 1, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::mip_mapping, params::MipMappingE::DEST)), uav_handle); d3d12::Dispatch(d3d12_cmd_list, ((width + 8 - 1) / 8), ((height + 8 - 1) / 8), 1u); //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 d3d12::UAVBarrier(d3d12_cmd_list, { dest_cubemap }); d3d12::Transition(d3d12_cmd_list, dest_cubemap, dest_cubemap->m_subresource_states[src_mip], ResourceState::PIXEL_SHADER_RESOURCE, src_mip, 1); } dest_cubemap->m_need_mips = false; } inline void PrefilterCubemap(d3d12::CommandList* cmd_list, wr::TextureHandle src_texture, wr::TextureHandle dst_texture) { auto pipeline = static_cast(PipelineRegistry::Get().Find(pipelines::cubemap_prefiltering)); d3d12::BindComputePipeline(cmd_list, pipeline); //Get allocator from pool auto allocator = static_cast(src_texture.m_pool)->GetMipmappingAllocator(); d3d12::TextureResource* cubemap_text = static_cast(src_texture.m_pool->GetTextureResource(src_texture)); d3d12::TextureResource* envmap_text = static_cast(dst_texture.m_pool->GetTextureResource(dst_texture)); for (uint32_t i = 0; i < 6; ++i) { PrefilterCubemapFace(cubemap_text, envmap_text, cmd_list, allocator, i); } } inline void SetupEquirectToCubemapTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { if (resize) { return; } auto& data = fg.GetData(handle); auto& ps_registry = PipelineRegistry::Get(); data.in_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::equirect_to_cubemap); data.camera_cb_pool = rs.CreateConstantBufferPool(2_mb); data.cb_handle = static_cast(data.camera_cb_pool->Create(sizeof(ProjectionView_CB))); data.proj_mat = DirectX::XMMatrixPerspectiveFovRH(DirectX::XMConvertToRadians(90.0f), 1.0f, 0.1f, 10.0f); data.view_mat[0] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(1.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f)); data.view_mat[1] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(-1.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f)); data.view_mat[2] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f)); data.view_mat[3] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f)); data.view_mat[4] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 0.0f, 1.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f)); data.view_mat[5] = DirectX::XMMatrixLookAtRH(DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), DirectX::XMVectorSet(0.0f, 0.0f, -1.0f, 1.0f), DirectX::XMVectorSet(0.0f, -1.0f, 0.0f, 0.0f)); } inline void ExecuteEquirectToCubemapTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto skybox_node = scene_graph.GetCurrentSkybox(); if (!skybox_node) { return; } //Does it need conversion? if (skybox_node->m_skybox != std::nullopt) { return; } data.build_frame_idx = n_render_system.GetFrameIdx(); data.in_equirect = skybox_node->m_hdr; skybox_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); skybox_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); data.out_cubemap = skybox_node->m_skybox.value(); data.out_pref_env = skybox_node->m_prefiltered_env_map.value(); d3d12::TextureResource* equirect_text = static_cast(data.in_equirect.m_pool->GetTextureResource(data.in_equirect)); d3d12::TextureResource* cubemap_text = static_cast(data.out_cubemap.m_pool->GetTextureResource(data.out_cubemap)); if (n_render_system.m_render_window.has_value()) { auto cmd_list = fg.GetCommandList(handle); const auto viewport = d3d12::CreateViewport(static_cast(cubemap_text->m_width), static_cast(cubemap_text->m_height)); const auto frame_idx = n_render_system.GetRenderWindow()->m_frame_idx; d3d12::BindViewport(cmd_list, viewport); d3d12::BindPipeline(cmd_list, data.in_pipeline); d3d12::SetPrimitiveTopology(cmd_list, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); ProjectionView_CB cb_data; cb_data.m_view[0] = data.view_mat[0]; cb_data.m_view[1] = data.view_mat[1]; cb_data.m_view[2] = data.view_mat[2]; cb_data.m_view[3] = data.view_mat[3]; cb_data.m_view[4] = data.view_mat[4]; cb_data.m_view[5] = data.view_mat[5]; cb_data.m_projection = data.proj_mat; data.cb_handle->m_pool->Update(data.cb_handle, sizeof(ProjectionView_CB), 0, frame_idx, (uint8_t*)&cb_data); d3d12::BindConstantBuffer(cmd_list, data.cb_handle->m_native, 1, frame_idx); for (uint32_t i = 0; i < 6; ++i) { //Get render target handle. d3d12::DescHeapCPUHandle rtv_handle = cubemap_text->m_rtv_allocation->GetDescriptorHandle(i); cmd_list->m_native->OMSetRenderTargets(1, &rtv_handle.m_native, false, nullptr); d3d12::Bind32BitConstants(cmd_list, &i, 1, 0, 0); //bind cube and render Model* cube_model = rs.GetSimpleShape(RenderSystem::SimpleShapes::CUBE); //Render meshes for (auto& mesh : cube_model->m_meshes) { wr::D3D12ModelPool* pool = static_cast(cube_model->m_model_pool); auto n_mesh = pool->GetMeshData(mesh.first->id); d3d12::BindVertexBuffer(cmd_list, pool->GetVertexStagingBuffer(), 0, pool->GetVertexStagingBuffer()->m_size, n_mesh->m_vertex_staging_buffer_stride); d3d12::BindIndexBuffer(cmd_list, pool->GetIndexStagingBuffer(), 0, static_cast(pool->GetIndexStagingBuffer()->m_size)); constexpr unsigned int srv_idx = rs_layout::GetHeapLoc(params::cubemap_conversion, params::CubemapConversionE::EQUIRECTANGULAR_TEXTURE); d3d12::SetShaderSRV(cmd_list, 2, srv_idx, equirect_text); d3d12::BindDescriptorHeaps(cmd_list); if (n_mesh->m_index_count != 0) { d3d12::DrawIndexed(cmd_list, static_cast(n_mesh->m_index_count), 1, static_cast(n_mesh->m_index_staging_buffer_offset), static_cast(n_mesh->m_vertex_staging_buffer_offset)); } else { d3d12::Draw(cmd_list, static_cast(n_mesh->m_vertex_count), 1, static_cast(n_mesh->m_vertex_staging_buffer_offset)); } } } d3d12::Transition(cmd_list, cubemap_text, cubemap_text->m_subresource_states[0], ResourceState::PIXEL_SHADER_RESOURCE); //Mipmap the skybox auto pipeline = static_cast(PipelineRegistry::Get().Find(pipelines::mip_mapping)); d3d12::BindComputePipeline(cmd_list, pipeline); auto texture_pool = std::static_pointer_cast(n_render_system.m_texture_pools[0]); texture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 0); texture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 1); texture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 2); texture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 3); texture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 4); texture_pool->GenerateMips_Cubemap(cubemap_text, cmd_list, 5); //Prefilter environment map PrefilterCubemap(cmd_list, data.out_cubemap, data.out_pref_env); fg.SetShouldExecute(handle, false); } } } /* internal */ inline void AddEquirectToCubemapTask(FrameGraph& fg) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET), RenderTargetProperties::FinishedResourceState(ResourceState::NON_PIXEL_SHADER_RESOURCE), RenderTargetProperties::CreateDSVBuffer(true), RenderTargetProperties::DSVFormat(Format::D32_FLOAT), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(true), RenderTargetProperties::ClearDepth(true), }; RenderTaskDesc desc; desc.m_setup_func = [&](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupEquirectToCubemapTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteEquirectToCubemapTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { if(!resize) { auto &data = fg.GetData(handle); data.camera_cb_pool.reset(); } }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::DIRECT; desc.m_allow_multithreading = true; fg.AddTask(desc, L"Equire To Cubemap"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_hbao.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "d3d12_raytracing_task.hpp" #include "d3d12_deferred_main.hpp" #ifdef NVIDIA_GAMEWORKS_HBAO #include #endif namespace wr { struct HBAOSettings { struct Runtime { float m_meters_to_view_space_units = 1; // DistanceInViewSpaceUnits = MetersToViewSpaceUnits * DistanceInMeters float m_radius = 2.f; // The AO radius in meters float m_bias = 0.1f; // To hide low-tessellation artifacts // 0.0~0.5 float m_power_exp = 2.f; // The final AO output is pow(AO, powerExponent) // 1.0~4.0 bool m_enable_blur = true; // To blur the AO with an edge-preserving blur float m_blur_sharpness = 16.f; // The higher, the more the blur preserves edges // 0.0~16.0 }; Runtime m_runtime; }; struct HBAOData { d3d12::RenderTarget* out_deferred_main_rt = nullptr; d3d12::DescriptorHeap* out_descriptor_heap_srv = nullptr; d3d12::DescriptorHeap* out_descriptor_heap_rtv = nullptr; #ifdef NVIDIA_GAMEWORKS_HBAO GFSDK_SSAO_Context_D3D12* ssao_context = nullptr; GFSDK_SSAO_InputData_D3D12 ssao_input_data; #endif d3d12::DescHeapCPUHandle cpu_depth_handle{}; d3d12::DescHeapGPUHandle gpu_depth_handle{}; d3d12::DescHeapCPUHandle cpu_normal_handle{}; d3d12::DescHeapGPUHandle gpu_normal_handle{}; d3d12::DescHeapCPUHandle cpu_target_handle{}; }; namespace internal { inline void SetupHBAOTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool) { #ifdef NVIDIA_GAMEWORKS_HBAO auto& n_render_system = static_cast(rs); auto n_device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); // shader resoruce view heap. { d3d12::desc::DescriptorHeapDesc heap_desc; heap_desc.m_num_descriptors = 2 + GFSDK_SSAO_NUM_DESCRIPTORS_CBV_SRV_UAV_HEAP_D3D12; heap_desc.m_type = DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV; heap_desc.m_versions = 1; data.out_descriptor_heap_srv = d3d12::CreateDescriptorHeap(n_device, heap_desc); data.gpu_depth_handle = d3d12::GetGPUHandle(data.out_descriptor_heap_srv, 0); data.cpu_depth_handle = d3d12::GetCPUHandle(data.out_descriptor_heap_srv, 0); data.gpu_normal_handle = d3d12::GetGPUHandle(data.out_descriptor_heap_srv, 0, 1); data.cpu_normal_handle = d3d12::GetCPUHandle(data.out_descriptor_heap_srv, 0, 1); } // render target view heap. { d3d12::desc::DescriptorHeapDesc heap_desc; heap_desc.m_num_descriptors = GFSDK_SSAO_NUM_DESCRIPTORS_RTV_HEAP_D3D12; heap_desc.m_type = DescriptorHeapType::DESC_HEAP_TYPE_RTV; heap_desc.m_shader_visible = false; heap_desc.m_versions = 1; data.out_descriptor_heap_rtv = d3d12::CreateDescriptorHeap(n_device, heap_desc); data.cpu_target_handle = d3d12::GetCPUHandle(data.out_descriptor_heap_rtv, 0); } // depth & normal { auto deferred_main_rt = data.out_deferred_main_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, data.cpu_normal_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats[1]); d3d12::CreateSRVFromDSV(deferred_main_rt, data.cpu_depth_handle); } // target { n_render_target->m_rtv_descriptor_increment_size = n_device->m_native->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); n_device->m_native->CreateRenderTargetView(n_render_target->m_render_targets[0], nullptr, data.cpu_target_handle.m_native); } data.ssao_input_data = {}; data.ssao_input_data.DepthData.DepthTextureType = GFSDK_SSAO_HARDWARE_DEPTHS; data.ssao_input_data.DepthData.FullResDepthTextureSRV.pResource = data.out_deferred_main_rt->m_depth_stencil_buffer; data.ssao_input_data.DepthData.FullResDepthTextureSRV.GpuHandle = data.gpu_depth_handle.m_native.ptr; data.ssao_input_data.NormalData.Enable = false; data.ssao_input_data.NormalData.FullResNormalTextureSRV.pResource = data.out_deferred_main_rt->m_render_targets[1]; data.ssao_input_data.NormalData.FullResNormalTextureSRV.GpuHandle = data.gpu_normal_handle.m_native.ptr; GFSDK_SSAO_CustomHeap custom_heap; custom_heap.new_ = ::operator new; custom_heap.delete_ = ::operator delete; // ao descriptor heap description GFSDK_SSAO_DescriptorHeaps_D3D12 DescriptorHeaps; DescriptorHeaps.CBV_SRV_UAV.pDescHeap = data.out_descriptor_heap_srv->m_native[0]; DescriptorHeaps.CBV_SRV_UAV.BaseIndex = 2; DescriptorHeaps.RTV.pDescHeap = data.out_descriptor_heap_rtv->m_native[0]; DescriptorHeaps.RTV.BaseIndex = 0; // initialize the ao context GFSDK_SSAO_Status status = GFSDK_SSAO_CreateContext_D3D12(n_device->m_native, 1, DescriptorHeaps, &data.ssao_context, &custom_heap); if (status != GFSDK_SSAO_Status::GFSDK_SSAO_OK) { LOGW("Failed to initialize the NVIDIA HBAO+ context.") } #endif } inline void ExecuteHBAOTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { #ifdef NVIDIA_GAMEWORKS_HBAO auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto settings = fg.GetSettings(handle); auto n_render_target = fg.GetRenderTarget(handle); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; data.ssao_input_data.DepthData.ProjectionMatrix.Data = GFSDK_SSAO_Float4x4((const GFSDK_SSAO_FLOAT*)& sg.GetActiveCamera()->m_projection); data.ssao_input_data.DepthData.ProjectionMatrix.Layout = GFSDK_SSAO_ROW_MAJOR_ORDER; data.ssao_input_data.NormalData.WorldToViewMatrix.Data = GFSDK_SSAO_Float4x4((const GFSDK_SSAO_FLOAT*)& sg.GetActiveCamera()->m_inverse_view); data.ssao_input_data.NormalData.WorldToViewMatrix.Layout = GFSDK_SSAO_ROW_MAJOR_ORDER; data.ssao_input_data.DepthData.MetersToViewSpaceUnits = settings.m_runtime.m_meters_to_view_space_units; // Set the viewport data.ssao_input_data.DepthData.Viewport.Enable = true; data.ssao_input_data.DepthData.Viewport.Height = static_cast(viewport.m_viewport.Height); data.ssao_input_data.DepthData.Viewport.Width = static_cast(viewport.m_viewport.Width); data.ssao_input_data.DepthData.Viewport.TopLeftX = static_cast(viewport.m_viewport.TopLeftX); data.ssao_input_data.DepthData.Viewport.TopLeftY = static_cast(viewport.m_viewport.TopLeftY); data.ssao_input_data.DepthData.Viewport.MinDepth = static_cast(viewport.m_viewport.MinDepth); data.ssao_input_data.DepthData.Viewport.MaxDepth = static_cast(viewport.m_viewport.MaxDepth); GFSDK_SSAO_Parameters ao_parameters = {}; ao_parameters.Radius = settings.m_runtime.m_radius; ao_parameters.Bias = settings.m_runtime.m_bias; ao_parameters.PowerExponent = settings.m_runtime.m_power_exp; ao_parameters.Blur.Enable = settings.m_runtime.m_enable_blur; ao_parameters.Blur.Radius = GFSDK_SSAO_BLUR_RADIUS_4; ao_parameters.Blur.Sharpness = settings.m_runtime.m_blur_sharpness; ao_parameters.EnableDualLayerAO = false; GFSDK_SSAO_RenderMask render_mask = GFSDK_SSAO_RENDER_AO; GFSDK_SSAO_RenderTargetView_D3D12 rtv {}; rtv.pResource = n_render_target->m_render_targets[0]; rtv.CpuHandle = n_render_target->m_rtv_descriptor_heap->GetCPUDescriptorHandleForHeapStart().ptr; GFSDK_SSAO_Output_D3D12 output; output.pRenderTargetView = &rtv; d3d12::Transition(cmd_list, n_render_target, ResourceState::COPY_SOURCE, ResourceState::RENDER_TARGET); d3d12::BindDescriptorHeap(cmd_list, data.out_descriptor_heap_srv, DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, 0); GFSDK_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); if (status != GFSDK_SSAO_Status::GFSDK_SSAO_OK) { LOGW("Failed to perform NVIDIA HBAO+"); } d3d12::Transition(cmd_list, n_render_target, ResourceState::RENDER_TARGET, ResourceState::COPY_SOURCE); #endif } inline void DestroyHBAOTask(FrameGraph& fg, RenderTaskHandle handle, bool) { #ifdef NVIDIA_GAMEWORKS_HBAO auto& data = fg.GetData(handle); d3d12::Destroy(data.out_descriptor_heap_srv); d3d12::Destroy(data.out_descriptor_heap_rtv); delete data.ssao_context; #endif } } /* internal */ inline void AddHBAOTask(FrameGraph& frame_graph) { #ifndef NVIDIA_GAMEWORKS_HBAO LOGW("HBAO+ task has been added to the frame graph. But `NVIDIA_GAMEWORKS_HBAO` is not defined."); #endif RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::RENDER_TARGET), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ d3d12::settings::back_buffer_format }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupHBAOTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteHBAOTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyHBAOTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"NVIDIA HBAO+", FG_DEPS()); frame_graph.UpdateSettings(HBAOSettings()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_imgui_render_task.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../window.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../frame_graph/frame_graph.hpp" #include "../imgui/ImGuizmo.h" #include "../imgui/imgui.hpp" #include "../imgui/imgui_impl_win32.hpp" #include "../imgui/imgui_impl_dx12.hpp" namespace wr { struct ImGuiTaskData { std::function in_imgui_func; inline static d3d12::DescriptorHeap* out_descriptor_heap = nullptr; }; namespace internal { inline void SetupImGuiTask(RenderSystem& rs, FrameGraph&, RenderTaskHandle, bool resize) { auto& n_render_system = static_cast(rs); if (!n_render_system.m_window.has_value()) { LOGC("Tried using imgui without a window!"); } if (resize) { ImGui_ImplDX12_CreateDeviceObjects(); return; } if (ImGui_ImplDX12_IsInitialized()) { return; } d3d12::desc::DescriptorHeapDesc heap_desc; heap_desc.m_num_descriptors = 2; heap_desc.m_shader_visible = true; heap_desc.m_type = DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV; ImGuiTaskData::out_descriptor_heap = d3d12::CreateDescriptorHeap(n_render_system.m_device, heap_desc); SetName(ImGuiTaskData::out_descriptor_heap, L"ImGui Descriptor Heap"); IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows (FIXME: Currently broken in DX12 back-end, need some work!) io.ConfigDockingWithShift = true; ImGui_ImplWin32_Init(n_render_system.m_window.value()->GetWindowHandle()); ImGui_ImplDX12_Init(n_render_system.m_device->m_native, d3d12::settings::num_back_buffers, (DXGI_FORMAT)d3d12::settings::back_buffer_format, d3d12::GetCPUHandle(ImGuiTaskData::out_descriptor_heap, 0 /* TODO: Solve versioning for ImGui */).m_native, d3d12::GetGPUHandle(ImGuiTaskData::out_descriptor_heap, 0 /* TODO: Solve versioning for ImGui */).m_native); ImGui::StyleColorsCherry(); } template inline void ExecuteImGuiTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto cmd_list = fg.GetCommandList(handle); // Temp rendering if (n_render_system.m_render_window.has_value()) { auto frame_idx = n_render_system.GetFrameIdx(); // Create handle to the render target you want to display. and put it in descriptor slot 2. auto display_rt = static_cast(fg.GetPredecessorRenderTarget()); auto cpu_handle = d3d12::GetCPUHandle(data.out_descriptor_heap, frame_idx, 1); d3d12::CreateSRVFromSpecificRTV(display_rt, cpu_handle, 0, display_rt->m_create_info.m_rtv_formats[frame_idx]); // Prepare imgui ImGui_ImplDX12_NewFrame(); ImGui_ImplWin32_NewFrame(); ImGui::NewFrame(); ImGuizmo::BeginFrame(); data.in_imgui_func(ImTextureID(d3d12::GetGPUHandle(data.out_descriptor_heap, frame_idx, 1).m_native.ptr)); // Render imgui d3d12::Transition(cmd_list, display_rt, wr::ResourceState::COPY_SOURCE, wr::ResourceState::PIXEL_SHADER_RESOURCE); //EXCEPTION CODE START d3d12::BindDescriptorHeap(cmd_list, data.out_descriptor_heap, data.out_descriptor_heap->m_create_info.m_type, n_render_system.GetFrameIdx()); d3d12::BindDescriptorHeaps(cmd_list); for (int i = 0; i < D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES; ++i) { cmd_list->m_dynamic_descriptor_heaps[i]->CommitStagedDescriptorsForDraw(*cmd_list); } //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. ImGui::Render(); ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), cmd_list->m_native); // Update and Render additional Platform Windows (Beta-Viewport) if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { ImGui::UpdatePlatformWindows(); ImGui::RenderPlatformWindowsDefault(NULL, (void*)cmd_list->m_native); } d3d12::Transition(cmd_list, display_rt, wr::ResourceState::PIXEL_SHADER_RESOURCE, wr::ResourceState::COPY_SOURCE); } } inline void DestroyImGuiTask(FrameGraph&, RenderTaskHandle, bool resize) { if (resize) { ImGui_ImplDX12_InvalidateDeviceObjects(); } else if (ImGui_ImplDX12_IsInitialized()) { ImGui_ImplDX12_Shutdown(); ImGui_ImplWin32_Shutdown(); ImGui::DestroyContext(); delete ImGuiTaskData::out_descriptor_heap; } } } /* internal */ template [[nodiscard]] inline RenderTaskDesc GetImGuiTask(std::function imgui_func) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(true), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(std::nullopt), RenderTargetProperties::FinishedResourceState(std::nullopt), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [imgui_func](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { fg.GetData(handle).in_imgui_func = imgui_func; internal::SetupImGuiTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteImGuiTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyImGuiTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::DIRECT; desc.m_allow_multithreading = false; return desc; } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_path_tracer.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../rt_pipeline_registry.hpp" #include "../root_signature_registry.hpp" #include "../engine_registry.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_rt_shadow_task.hpp" #include "../render_tasks/d3d12_rt_reflection_task.hpp" #include "../render_tasks/d3d12_build_acceleration_structures.hpp" #include "../imgui_tools.hpp" namespace wr { //TODO, this struct is unpadded, manual padding might be usefull. struct PathTracerData { d3d12::AccelerationStructure out_tlas = {}; // Shader tables std::array out_raygen_shader_table = { nullptr, nullptr, nullptr }; std::array out_miss_shader_table = { nullptr, nullptr, nullptr }; std::array out_hitgroup_shader_table = { nullptr, nullptr, nullptr }; // Pipeline objects d3d12::StateObject* out_state_object = nullptr; // Structures and buffers D3D12ConstantBufferHandle* out_cb_camera_handle = nullptr; d3d12::RenderTarget* out_deferred_main_rt = nullptr; DirectX::XMVECTOR last_cam_pos = {}; DirectX::XMVECTOR last_cam_rot = {}; DescriptorAllocation out_output_alloc; DescriptorAllocation out_gbuffer_albedo_alloc; DescriptorAllocation out_gbuffer_normal_alloc; DescriptorAllocation out_gbuffer_emissive_alloc; DescriptorAllocation out_gbuffer_depth_alloc; bool tlas_requires_init = true; }; namespace internal { inline void CreateShaderTables(d3d12::Device* device, PathTracerData& data, int frame_idx) { // Delete existing shader table if (data.out_miss_shader_table[frame_idx]) { d3d12::Destroy(data.out_miss_shader_table[frame_idx]); } if (data.out_hitgroup_shader_table[frame_idx]) { d3d12::Destroy(data.out_hitgroup_shader_table[frame_idx]); } if (data.out_raygen_shader_table[frame_idx]) { d3d12::Destroy(data.out_raygen_shader_table[frame_idx]); } // Set up Raygen Shader Table { // Create Record(s) std::uint32_t shader_record_count = 1; auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); auto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "RaygenEntry"); auto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size); // Create Table data.out_raygen_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); d3d12::AddShaderRecord(data.out_raygen_shader_table[frame_idx], shader_record); } // Set up Miss Shader Table { // Create Record(s) std::uint32_t shader_record_count = 2; auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); auto shadow_miss_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "ShadowMissEntry"); auto shadow_miss_record = d3d12::CreateShaderRecord(shadow_miss_identifier, shader_identifier_size); auto reflection_miss_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "ReflectionMiss"); auto reflection_miss_record = d3d12::CreateShaderRecord(reflection_miss_identifier, shader_identifier_size); // Create Table(s) data.out_miss_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); d3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], reflection_miss_record); d3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], shadow_miss_record); } // Set up Hit Group Shader Table { // Create Record(s) std::uint32_t shader_record_count = 2; auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); auto shadow_hit_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "ShadowHitGroup"); auto shadow_hit_record = d3d12::CreateShaderRecord(shadow_hit_identifier, shader_identifier_size); auto reflection_hit_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "ReflectionHitGroup"); auto reflection_hit_record = d3d12::CreateShaderRecord(reflection_hit_identifier, shader_identifier_size); // Create Table(s) data.out_hitgroup_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); d3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], reflection_hit_record); d3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], shadow_hit_record); } } inline void SetupPathTracerTask(RenderSystem & render_system, FrameGraph & fg, RenderTaskHandle & handle, bool resize) { if (fg.HasTask()) { fg.WaitForPredecessorTask(); } if (fg.HasTask()) { fg.WaitForPredecessorTask(); } // Initialize variables auto& n_render_system = static_cast(render_system); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); d3d12::SetName(n_render_target, L"Path Tracing Render Target"); if (!resize) { // Get AS build data auto& as_build_data = fg.GetPredecessorData(); data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate()); data.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate()); data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate()); data.out_gbuffer_emissive_alloc = std::move(as_build_data.out_allocator->Allocate()); data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate()); data.tlas_requires_init = true; } // Versioning for (int frame_idx = 0; frame_idx < 1; ++frame_idx) { // Bind output texture d3d12::DescHeapCPUHandle rtv_handle = data.out_output_alloc.GetDescriptorHandle(); d3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, frame_idx, n_render_target->m_create_info.m_rtv_formats[frame_idx]); // Bind g-buffers (albedo, normal, depth) auto albedo_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle(); auto normal_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(); auto emissive_handle = data.out_gbuffer_emissive_alloc.GetDescriptorHandle(); auto depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle(); auto deferred_main_rt = data.out_deferred_main_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, albedo_handle, 0, deferred_main_rt->m_create_info.m_rtv_formats[0]); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats[1]); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, emissive_handle, 2, deferred_main_rt->m_create_info.m_rtv_formats[2]); d3d12::CreateSRVFromDSV(deferred_main_rt, depth_handle); } if (!resize) { // Camera constant buffer data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData))); // Pipeline State Object auto& rt_registry = RTPipelineRegistry::Get(); data.out_state_object = static_cast(rt_registry.Find(state_objects::path_tracer_state_object)); // Create Shader Tables CreateShaderTables(device, data, 0); CreateShaderTables(device, data, 1); CreateShaderTables(device, data, 2); } } inline void ExecutePathTracerTask(RenderSystem& render_system, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle& handle) { if (fg.HasTask()) { fg.WaitForPredecessorTask(); } if (fg.HasTask()) { fg.WaitForPredecessorTask(); } // Initialize variables auto& n_render_system = static_cast(render_system); auto window = n_render_system.m_window.value(); auto render_target = fg.GetRenderTarget(handle); auto device = n_render_system.m_device; auto cmd_list = fg.GetCommandList(handle); auto& data = fg.GetData(handle); auto& as_build_data = fg.GetPredecessorData(); auto frame_idx = n_render_system.GetFrameIdx(); // Rebuild acceleratrion structure a 2e time for fallback if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { d3d12::CreateOrUpdateTLAS(device, cmd_list, data.tlas_requires_init, data.out_tlas, as_build_data.out_blas_list, frame_idx); } // Reset accmulation if nessessary if (DirectX::XMVector3Length(DirectX::XMVectorSubtract(scene_graph.GetActiveCamera()->m_position, data.last_cam_pos)).m128_f32[0] > 0.01) { data.last_cam_pos = scene_graph.GetActiveCamera()->m_position; n_render_system.temp_rough = -1; } if (DirectX::XMVector3Length(DirectX::XMVectorSubtract(scene_graph.GetActiveCamera()->m_rotation_radians, data.last_cam_rot)).m128_f32[0] > 0.001) { data.last_cam_rot = scene_graph.GetActiveCamera()->m_rotation_radians; n_render_system.temp_rough = -1; } // Wait for AS to be built d3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx); if (n_render_system.m_render_window.has_value()) { d3d12::BindRaytracingPipeline(cmd_list, data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); // Bind output, indices and materials, offsets, etc auto out_uav_handle = data.out_output_alloc.GetDescriptorHandle(); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::OUTPUT)), out_uav_handle); auto out_scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::INDICES)), out_scene_ib_handle); auto out_scene_mat_handle = as_build_data.out_scene_mat_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::MATERIALS)), out_scene_mat_handle); auto out_scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::OFFSETS)), out_scene_offset_handle); auto out_albedo_gbuffer_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::GBUFFERS)) + 0, out_albedo_gbuffer_handle); auto out_normal_gbuffer_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::GBUFFERS)) + 1, out_normal_gbuffer_handle); auto out_emissive_gbuffer_handle = data.out_gbuffer_emissive_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::GBUFFERS)) + 2, out_emissive_gbuffer_handle); auto out_scene_depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::GBUFFERS)) + 3, out_scene_depth_handle); /* To keep the CopyDescriptors function happy, we need to fill the descriptor table with valid descriptors We fill the table with a single descriptor, then overwrite some spots with the he correct textures If a spot is unused, then a default descriptor will be still bound, but not used in the shaders. Since the renderer creates a texture pool that can be used by the render tasks, and the texture pool also has default textures for albedo/roughness/etc... one of those textures is a good candidate for this. */ { auto texture_handle = render_system.GetDefaultAlbedo(); auto* texture_resource = static_cast(texture_handle.m_pool->GetTextureResource(texture_handle)); size_t num_textures_in_heap = COMPILATION_EVAL(rs_layout::GetSize(params::path_tracing, params::PathTracingE::TEXTURES)); unsigned int heap_loc_start = COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::TEXTURES)); for (size_t i = 0; i < num_textures_in_heap; ++i) { d3d12::SetRTShaderSRV(cmd_list, 0, static_cast(heap_loc_start + i), texture_resource); } } // Fill descriptor heap with textures used by the scene for (auto material_handle : as_build_data.out_material_handles) { auto* material_internal = material_handle.m_pool->GetMaterial(material_handle); auto set_srv = [&data, material_internal, cmd_list](auto texture_handle) { if (!texture_handle.m_pool) return; auto* texture_internal = static_cast(texture_handle.m_pool->GetTextureResource(texture_handle)); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES)) + static_cast(texture_handle.m_id), texture_internal); }; std::array(TextureType::COUNT)> types = { TextureType::ALBEDO, TextureType::NORMAL, TextureType::ROUGHNESS, TextureType::METALLIC, TextureType::EMISSIVE, TextureType::AO }; for (auto t : types) { if (material_internal->HasTexture(t)) set_srv(material_internal->GetTexture(t)); } } // Get light buffer if (static_cast(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE) { static_cast(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE); } DescriptorAllocation light_alloc = std::move(as_build_data.out_allocator->Allocate()); d3d12::DescHeapCPUHandle light_handle = light_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromStructuredBuffer(static_cast(scene_graph.GetLightBuffer())->m_native, light_handle, frame_idx); d3d12::DescHeapCPUHandle light_handle2 = light_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::LIGHTS)), light_handle2); // Update offset data n_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); // Update material data if (as_build_data.out_materials_require_update) { n_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); } // Update camera constant buffer auto camera = scene_graph.GetActiveCamera(); temp::RTHybridCamera_CBData cam_data{}; cam_data.m_inverse_view = DirectX::XMMatrixInverse(nullptr, camera->m_view); cam_data.m_inverse_projection = DirectX::XMMatrixInverse(nullptr, camera->m_projection); cam_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection); cam_data.m_intensity = n_render_system.temp_intensity; cam_data.m_frame_idx = ++n_render_system.temp_rough; n_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? // Make sure the convolution pass wrote to the skybox. fg.WaitForPredecessorTask(); // Get skybox if (SkyboxNode *skybox = scene_graph.GetCurrentSkybox().get()) { auto skybox_t = static_cast(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::SKYBOX)), skybox_t); // Get Pre-filtered environment auto irradiance_t = static_cast(skybox->m_prefiltered_env_map->m_pool->GetTextureResource(skybox->m_prefiltered_env_map.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::PREF_ENV_MAP)), irradiance_t); // Get Environment Map irradiance_t = static_cast(skybox->m_irradiance->m_pool->GetTextureResource(skybox->m_irradiance.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::IRRADIANCE_MAP)), irradiance_t); } // Get brdf lookup texture auto brdf_lut_text = static_cast(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::path_tracing, params::PathTracingE::BRDF_LUT)), brdf_lut_text); // Transition depth to NON_PIXEL_RESOURCE d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::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); d3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); d3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_camera_handle->m_native, 2, frame_idx); if (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE) { d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1); } else if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { cmd_list->m_native_fallback->SetTopLevelAccelerationStructure(0, as_build_data.out_tlas.m_fallback_tlas_ptr); } if (!as_build_data.out_blas_list.empty()) { d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3); } //#ifdef _DEBUG CreateShaderTables(device, data, frame_idx); //#endif // Dispatch hybrid ray tracing rays d3d12::DispatchRays( cmd_list, data.out_hitgroup_shader_table[frame_idx], data.out_miss_shader_table[frame_idx], data.out_raygen_shader_table[frame_idx], d3d12::GetRenderTargetWidth(render_target), d3d12::GetRenderTargetHeight(render_target), 1, frame_idx); // Transition depth back to DEPTH_WRITE d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); } } inline void DestroyPathTracerTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if(!resize) { PathTracerData& data = fg.GetData(handle); for (d3d12::ShaderTable* shader : data.out_raygen_shader_table) { delete shader; } for (d3d12::ShaderTable* shader : data.out_miss_shader_table) { delete shader; } for (d3d12::ShaderTable* shader : data.out_hitgroup_shader_table) { delete shader; } } } } /* internal */ inline void AddPathTracerTask(FrameGraph& fg) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_UNORM }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupPathTracerTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecutePathTracerTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph & fg, RenderTaskHandle handle, bool resize) { internal::DestroyPathTracerTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; fg.AddTask(desc, L"Path Traced Global Illumination", FG_DEPS()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_post_processing.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "d3d12_raytracing_task.hpp" namespace wr { struct PostProcessingData { d3d12::RenderTarget* out_source_rt = nullptr; d3d12::PipelineState* out_pipeline = nullptr; DescriptorAllocator* out_allocator = nullptr; DescriptorAllocation out_allocation; }; const constexpr int versions = 1; namespace internal { template inline void SetupPostProcessingTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); //Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors std::shared_ptr texture_pool = std::static_pointer_cast(n_render_system.m_texture_pools[0]); if (!texture_pool) { LOGC("Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool"); } data.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = data.out_allocator->Allocate(2); auto& ps_registry = PipelineRegistry::Get(); data.out_pipeline = ((d3d12::PipelineState*)ps_registry.Find(pipelines::post_processing)); auto source_rt = data.out_source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::DEST); auto cpu_handle = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::SOURCE); auto cpu_handle = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } } template inline void ExecutePostProcessingTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); auto n_render_target = fg.GetRenderTarget(handle); d3d12::BindComputePipeline(cmd_list, data.out_pipeline); auto source_rt = static_cast(fg.GetPredecessorRenderTarget()); // Destination { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::DEST); auto cpu_handle = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Source { constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::SOURCE); auto cpu_handle = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::CreateSRVFromSpecificRTV(source_rt, cpu_handle, 0, source_rt->m_create_info.m_rtv_formats[0]); } auto hdr_type = static_cast(d3d12::settings::output_hdr); d3d12::BindCompute32BitConstants(cmd_list, &hdr_type, 1, 0, 1); constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::DEST); auto handle_uav = data.out_allocation.GetDescriptorHandle(dest_idx); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); constexpr unsigned int source_idx = rs_layout::GetHeapLoc(params::post_processing, params::PostProcessingE::SOURCE); auto handle_srv = data.out_allocation.GetDescriptorHandle(source_idx); d3d12::SetShaderSRV(cmd_list, 0, source_idx, handle_srv); cmd_list->m_native->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(data.out_source_rt->m_render_targets[frame_idx % versions])); d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void DestroyPostProcessing(FrameGraph& fg, RenderTaskHandle handle, bool resize) { } } /* internal */ template inline void AddPostProcessingTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({d3d12::settings::back_buffer_format}), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupPostProcessingTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecutePostProcessingTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyPostProcessing(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Post Processing"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_raytracing_task.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../rt_pipeline_registry.hpp" #include "../root_signature_registry.hpp" #include "../render_tasks/d3d12_build_acceleration_structures.hpp" #include "../engine_registry.hpp" #include "../scene_graph/skybox_node.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../imgui_tools.hpp" #include "../render_tasks/d3d12_cubemap_convolution.hpp" namespace wr { struct RaytracingData { d3d12::AccelerationStructure out_tlas; std::array out_raygen_shader_table = { nullptr, nullptr, nullptr }; std::array out_miss_shader_table = { nullptr, nullptr, nullptr }; std::array out_hitgroup_shader_table = { nullptr, nullptr, nullptr }; d3d12::StateObject* out_state_object = nullptr; d3d12::RootSignature* out_root_signature = nullptr; D3D12ConstantBufferHandle* out_cb_camera_handle = nullptr; DescriptorAllocation out_uav_from_rtv; bool tlas_requires_init = false; DirectX::XMVECTOR last_cam_pos = {}; DirectX::XMVECTOR last_cam_rot = {}; }; namespace internal { inline void CreateShaderTables(d3d12::Device* device, RaytracingData& data, int frame_idx) { if (data.out_miss_shader_table[frame_idx]) { d3d12::Destroy(data.out_miss_shader_table[frame_idx]); } if (data.out_hitgroup_shader_table[frame_idx]) { d3d12::Destroy(data.out_hitgroup_shader_table[frame_idx]); } if (data.out_raygen_shader_table[frame_idx]) { d3d12::Destroy(data.out_raygen_shader_table[frame_idx]); } // Raygen Shader Table { // Create Record(s) std::uint32_t shader_record_count = 1; auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); auto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "RaygenEntry"); auto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size); // Create Table data.out_raygen_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); d3d12::AddShaderRecord(data.out_raygen_shader_table[frame_idx], shader_record); } // Miss Shader Table { // Create Record(s) std::uint32_t shader_record_count = 2; auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); auto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "MissEntry"); auto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size); auto shadow_shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "ShadowMissEntry"); auto shadow_shader_record = d3d12::CreateShaderRecord(shadow_shader_identifier, shader_identifier_size); // Create Table data.out_miss_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); d3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], shader_record); d3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], shadow_shader_record); } // Hit Group Shader Table { // Create Record(s) std::uint32_t shader_record_count = 2; auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); auto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "MyHitGroup"); auto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size); auto shadow_shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, "ShadowHitGroup"); auto shadow_shader_record = d3d12::CreateShaderRecord(shadow_shader_identifier, shader_identifier_size); // Create Table data.out_hitgroup_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); d3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], shader_record); d3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], shadow_shader_record); } } inline void SetupRaytracingTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); d3d12::SetName(n_render_target, L"Raytracing Target"); if (!resize) { // Pipeline State Object auto& rt_registry = RTPipelineRegistry::Get(); data.out_state_object = static_cast(rt_registry.Find(state_objects::state_object)); // Root Signature auto& rs_registry = RootSignatureRegistry::Get(); data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_test_global)); auto& as_build_data = fg.GetPredecessorData(); // Camera constant buffer data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RayTracingCamera_CBData))); data.out_uav_from_rtv = std::move(as_build_data.out_allocator->Allocate()); data.tlas_requires_init = true; CreateShaderTables(device, data, 0); CreateShaderTables(device, data, 1); CreateShaderTables(device, data, 2); } for (auto frame_idx = 0; frame_idx < 1; frame_idx++) { d3d12::DescHeapCPUHandle desc_handle = data.out_uav_from_rtv.GetDescriptorHandle(); d3d12::CreateUAVFromSpecificRTV(n_render_target, desc_handle, frame_idx, n_render_target->m_create_info.m_rtv_formats[frame_idx]); } } inline void ExecuteRaytracingTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto window = n_render_system.m_window.value(); auto render_target = fg.GetRenderTarget(handle); auto device = n_render_system.m_device; auto cmd_list = fg.GetCommandList(handle); auto& data = fg.GetData(handle); auto& as_build_data = fg.GetPredecessorData(); fg.WaitForPredecessorTask(); auto frame_idx = n_render_system.GetFrameIdx(); float scalar = 1.0f; // Rebuild acceleratrion structure a 2e time for fallback if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { d3d12::CreateOrUpdateTLAS(device, cmd_list, data.tlas_requires_init, data.out_tlas, as_build_data.out_blas_list, frame_idx); d3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx); } if (n_render_system.m_render_window.has_value()) { d3d12::BindRaytracingPipeline(cmd_list, data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); auto uav_from_rtv_handle = data.out_uav_from_rtv.GetDescriptorHandle(); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::OUTPUT)), uav_from_rtv_handle); auto scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::INDICES)), scene_ib_handle); auto scene_mat_handle = as_build_data.out_scene_mat_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::MATERIALS)), scene_mat_handle); auto scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::OFFSETS)), scene_offset_handle); // Reset accmulation if nessessary if (DirectX::XMVector3Length(DirectX::XMVectorSubtract(scene_graph.GetActiveCamera()->m_position, data.last_cam_pos)).m128_f32[0] > 0.01) { data.last_cam_pos = scene_graph.GetActiveCamera()->m_position; n_render_system.temp_rough = -1; } if (DirectX::XMVector3Length(DirectX::XMVectorSubtract(scene_graph.GetActiveCamera()->m_rotation_radians, data.last_cam_rot)).m128_f32[0] > 0.001) { data.last_cam_rot = scene_graph.GetActiveCamera()->m_rotation_radians; n_render_system.temp_rough = -1; } /* To keep the CopyDescriptors function happy, we need to fill the descriptor table with valid descriptors We fill the table with a single descriptor, then overwrite some spots with the he correct textures If a spot is unused, then a default descriptor will be still bound, but not used in the shaders. Since the renderer creates a texture pool that can be used by the render tasks, and the texture pool also has default textures for albedo/roughness/etc... one of those textures is a good candidate for this. */ { auto texture_handle = n_render_system.GetDefaultAlbedo(); auto* texture_resource = static_cast(texture_handle.m_pool->GetTextureResource(texture_handle)); size_t num_textures_in_heap = COMPILATION_EVAL(rs_layout::GetSize(params::full_raytracing, params::FullRaytracingE::TEXTURES)); unsigned int heap_loc_start = COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::TEXTURES)); for (size_t i = 0; i < num_textures_in_heap; ++i) { d3d12::SetRTShaderSRV(cmd_list, 0, static_cast(heap_loc_start + i), texture_resource); } } // Fill descriptor heap with textures used by the scene for (auto material_handle : as_build_data.out_material_handles) { auto* material_internal = material_handle.m_pool->GetMaterial(material_handle); auto set_srv = [&data, material_internal, cmd_list](auto texture_handle) { auto* texture_internal = static_cast(texture_handle.m_pool->GetTextureResource(texture_handle)); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::TEXTURES)) + static_cast(texture_handle.m_id), texture_internal); }; std::array(TextureType::COUNT)> types = { TextureType::ALBEDO, TextureType::NORMAL, TextureType::ROUGHNESS, TextureType::METALLIC, TextureType::EMISSIVE, TextureType::AO }; for (auto t : types) { if (material_internal->HasTexture(t)) set_srv(material_internal->GetTexture(t)); } } // Get light buffer { if (static_cast(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE) { static_cast(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE); } DescriptorAllocation light_alloc = std::move(as_build_data.out_allocator->Allocate()); d3d12::DescHeapCPUHandle light_handle = light_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromStructuredBuffer(static_cast(scene_graph.GetLightBuffer())->m_native, light_handle, frame_idx); d3d12::DescHeapCPUHandle light_handle2 = light_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::LIGHTS)), light_handle2); } // Get skybox if (SkyboxNode* skybox = scene_graph.GetCurrentSkybox().get()) { auto skybox_t = static_cast(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::SKYBOX)), skybox_t); // Get Environment Map auto irradiance_t = static_cast(skybox->m_irradiance->m_pool->GetTextureResource(skybox->m_irradiance.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::IRRADIANCE_MAP)), irradiance_t); } { // Get brdf lookup texture auto brdf_lut_texture = static_cast(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::full_raytracing, params::FullRaytracingE::BRDF_LUT)), brdf_lut_texture); } // Update offset data n_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); // Update material data if (as_build_data.out_materials_require_update) { n_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); } // Update camera constant buffer auto camera = scene_graph.GetActiveCamera(); temp::RayTracingCamera_CBData cam_data; cam_data.m_view = camera->m_view; cam_data.m_camera_position = camera->m_position; cam_data.m_inverse_view_projection = DirectX::XMMatrixTranspose(DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection)); cam_data.focal_radius = camera->m_f_number; cam_data.focal_length = camera->m_focal_length; cam_data.frame_idx = ++n_render_system.temp_rough; cam_data.intensity = n_render_system.temp_intensity; n_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? d3d12::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); d3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); d3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_camera_handle->m_native, 2, frame_idx); if (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE) { d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1); } else if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { cmd_list->m_native_fallback->SetTopLevelAccelerationStructure(0, as_build_data.out_tlas.m_fallback_tlas_ptr); } /*unsigned int verts_loc = rs_layout::GetHeapLoc(params::FullRaytracingE, params::FullRaytracingE::VERTICES); This 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... Much love, Meine and Florian*/ if (!as_build_data.out_blas_list.empty()) { d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3); } //#ifdef _DEBUG CreateShaderTables(device, data, frame_idx); //#endif d3d12::DispatchRays(cmd_list, data.out_hitgroup_shader_table[frame_idx], data.out_miss_shader_table[frame_idx], data.out_raygen_shader_table[frame_idx], static_cast(std::ceil(d3d12::GetRenderTargetWidth(render_target))), static_cast(std::ceil(d3d12::GetRenderTargetHeight(render_target))), 1, 0); } } inline void DestroyRaytracingTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if(!resize) { RaytracingData& data = fg.GetData(handle); for (d3d12::ShaderTable* shader : data.out_raygen_shader_table) { delete shader; } for (d3d12::ShaderTable* shader : data.out_miss_shader_table) { delete shader; } for (d3d12::ShaderTable* shader : data.out_hitgroup_shader_table) { delete shader; } } } } /* internal */ inline void AddRaytracingTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({wr::Format::R16G16B16A16_UNORM }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupRaytracingTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteRaytracingTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyRaytracingTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Full Raytracing"); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_reflection_denoiser.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../render_tasks/d3d12_spatial_reconstruction.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_rt_reflection_task.hpp" #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" namespace wr { struct ReflectionDenoiserData { d3d12::PipelineState* m_temporal_denoiser_pipeline; d3d12::PipelineState* m_variance_estimator_pipeline; d3d12::PipelineState* m_spatial_denoiser_pipeline; DescriptorAllocator* m_descriptor_allocator; DescriptorAllocation m_input_allocation; DescriptorAllocation m_ray_raw_allocation; DescriptorAllocation m_ray_dir_allocation; DescriptorAllocation m_albedo_roughness_allocation; DescriptorAllocation m_normal_metallic_allocation; DescriptorAllocation m_motion_allocation; DescriptorAllocation m_linear_depth_allocation; DescriptorAllocation m_world_pos_allocation; DescriptorAllocation m_in_history_allocation; DescriptorAllocation m_accum_allocation; DescriptorAllocation m_prev_normal_allocation; DescriptorAllocation m_prev_depth_allocation; DescriptorAllocation m_in_moments_allocation; DescriptorAllocation m_output_allocation; DescriptorAllocation m_out_history_allocation; DescriptorAllocation m_out_moments_allocation; DescriptorAllocation m_ping_pong_allocation; d3d12::RenderTarget* m_spatial_reconstruction_render_target; d3d12::RenderTarget* m_rt_reflection_render_target; d3d12::RenderTarget* m_gbuffer_render_target; d3d12::RenderTarget* m_output_render_target; d3d12::RenderTarget* m_prev_data_render_target; d3d12::RenderTarget* m_in_history_render_target; d3d12::RenderTarget* m_out_history_render_target; d3d12::RenderTarget* m_in_moments_render_target; d3d12::RenderTarget* m_out_moments_render_target; d3d12::RenderTarget* m_ping_pong_render_target; temp::ReflectionDenoiserSettings_CBData m_denoiser_settings; std::shared_ptr m_constant_buffer_pool; ConstantBufferHandle* m_denoiser_settings_buffer; std::array m_wavelet_sizes; }; namespace internal { inline void SetupReflectionDenoiserTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); data.m_output_render_target = fg.GetRenderTarget(handle); data.m_spatial_reconstruction_render_target = static_cast(fg.GetPredecessorRenderTarget()); data.m_rt_reflection_render_target = static_cast(fg.GetPredecessorRenderTarget()); data.m_gbuffer_render_target = static_cast(fg.GetPredecessorRenderTarget()); //Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors std::shared_ptr texture_pool = std::static_pointer_cast(n_render_system.m_texture_pools[0]); if (!texture_pool) { LOGC("Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool"); } if (!resize) { data.m_descriptor_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.m_input_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_ray_raw_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_ray_dir_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_albedo_roughness_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_normal_metallic_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_motion_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_linear_depth_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_world_pos_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_in_history_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_accum_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_prev_normal_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_prev_depth_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_in_moments_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_output_allocation = std::move(data.m_descriptor_allocator->Allocate(2)); data.m_out_history_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_out_moments_allocation = std::move(data.m_descriptor_allocator->Allocate()); data.m_ping_pong_allocation = std::move(data.m_descriptor_allocator->Allocate(2)); data.m_constant_buffer_pool = n_render_system.CreateConstantBufferPool( SizeAlignTwoPower(sizeof(temp::ReflectionDenoiserSettings_CBData), 256) * d3d12::settings::num_back_buffers + SizeAlignTwoPower(sizeof(float), 256) * d3d12::settings::num_back_buffers * d3d12::settings::shadow_denoiser_wavelet_iterations); data.m_denoiser_settings_buffer = data.m_constant_buffer_pool->Create(sizeof(temp::ReflectionDenoiserSettings_CBData)); data.m_denoiser_settings = {}; data.m_denoiser_settings.m_color_integration_alpha = 0.04f; data.m_denoiser_settings.m_moments_integration_alpha = 0.2f; data.m_denoiser_settings.m_variance_clipping_sigma = 4.0f; data.m_denoiser_settings.m_roughness_reprojection_threshold = 0.2f; data.m_denoiser_settings.m_max_history_samples = 48; data.m_denoiser_settings.m_n_phi = 128.f; data.m_denoiser_settings.m_z_phi = 1.f; data.m_denoiser_settings.m_l_phi = 4.f; for (int i = 0; i < data.m_wavelet_sizes.size(); ++i) { data.m_wavelet_sizes[i] = data.m_constant_buffer_pool->Create(sizeof(float)); float size = 1 << i; for (int j = 0; j < d3d12::settings::num_back_buffers; ++j) { data.m_constant_buffer_pool->Update(data.m_wavelet_sizes[i], sizeof(float), 0, j, (std::uint8_t*)&size); } } } d3d12::desc::RenderTargetDesc render_target_desc = {}; render_target_desc.m_clear_color[0] = 0.f; render_target_desc.m_clear_color[1] = 0.f; render_target_desc.m_clear_color[2] = 0.f; render_target_desc.m_clear_color[3] = 0.f; render_target_desc.m_create_dsv_buffer = false; render_target_desc.m_dsv_format = Format::UNKNOWN; render_target_desc.m_initial_state = ResourceState::NON_PIXEL_SHADER_RESOURCE; render_target_desc.m_num_rtv_formats = 3; render_target_desc.m_rtv_formats[0] = data.m_spatial_reconstruction_render_target->m_create_info.m_rtv_formats[0]; render_target_desc.m_rtv_formats[1] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[1]; render_target_desc.m_rtv_formats[2] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[4]; data.m_prev_data_render_target = d3d12::CreateRenderTarget(n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc); render_target_desc.m_num_rtv_formats = 1; render_target_desc.m_rtv_formats[0] = Format::R16_FLOAT; data.m_in_history_render_target = d3d12::CreateRenderTarget(n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc); render_target_desc.m_rtv_formats[0] = Format::R16G16_FLOAT; data.m_in_moments_render_target = d3d12::CreateRenderTarget(n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc); render_target_desc.m_rtv_formats[0] = data.m_output_render_target->m_create_info.m_rtv_formats[0]; data.m_ping_pong_render_target = d3d12::CreateRenderTarget(n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc); render_target_desc.m_initial_state = ResourceState::UNORDERED_ACCESS; render_target_desc.m_num_rtv_formats = 1; render_target_desc.m_rtv_formats[0] = data.m_in_history_render_target->m_create_info.m_rtv_formats[0]; data.m_out_history_render_target = d3d12::CreateRenderTarget(n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc); render_target_desc.m_rtv_formats[0] = data.m_in_moments_render_target->m_create_info.m_rtv_formats[0]; data.m_out_moments_render_target = d3d12::CreateRenderTarget(n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc); { auto cpu_handle = data.m_input_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_spatial_reconstruction_render_target, cpu_handle, 0, data.m_spatial_reconstruction_render_target->m_create_info.m_rtv_formats[0]); } { auto cpu_handle = data.m_ray_raw_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_rt_reflection_render_target, cpu_handle, 0, data.m_rt_reflection_render_target->m_create_info.m_rtv_formats[0]); } { auto cpu_handle = data.m_ray_dir_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_rt_reflection_render_target, cpu_handle, 2, data.m_rt_reflection_render_target->m_create_info.m_rtv_formats[2]); } { auto cpu_handle = data.m_albedo_roughness_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 0, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[0]); } { auto cpu_handle = data.m_normal_metallic_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 1, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[1]); } { auto cpu_handle = data.m_motion_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 3, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[3]); } { auto cpu_handle = data.m_linear_depth_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 4, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[4]); } { auto cpu_handle = data.m_world_pos_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 5, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[5]); } { auto cpu_handle = data.m_in_history_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_in_history_render_target, cpu_handle, 0, data.m_in_history_render_target->m_create_info.m_rtv_formats[0]); } { auto cpu_handle = data.m_accum_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_prev_data_render_target, cpu_handle, 0, data.m_prev_data_render_target->m_create_info.m_rtv_formats[0]); } { auto cpu_handle = data.m_prev_normal_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_prev_data_render_target, cpu_handle, 1, data.m_prev_data_render_target->m_create_info.m_rtv_formats[1]); } { auto cpu_handle = data.m_prev_depth_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_prev_data_render_target, cpu_handle, 2, data.m_prev_data_render_target->m_create_info.m_rtv_formats[2]); } { auto cpu_handle = data.m_in_moments_allocation.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(data.m_in_moments_render_target, cpu_handle, 0, data.m_in_moments_render_target->m_create_info.m_rtv_formats[0]); } { auto cpu_handle = data.m_output_allocation.GetDescriptorHandle(0); d3d12::CreateUAVFromSpecificRTV(data.m_output_render_target, cpu_handle, 0, data.m_output_render_target->m_create_info.m_rtv_formats[0]); cpu_handle = data.m_output_allocation.GetDescriptorHandle(1); d3d12::CreateSRVFromSpecificRTV(data.m_output_render_target, cpu_handle, 0, data.m_output_render_target->m_create_info.m_rtv_formats[0]); } { auto cpu_handle = data.m_out_history_allocation.GetDescriptorHandle(); d3d12::CreateUAVFromSpecificRTV(data.m_out_history_render_target, cpu_handle, 0, data.m_out_history_render_target->m_create_info.m_rtv_formats[0]); } { auto cpu_handle = data.m_out_moments_allocation.GetDescriptorHandle(); d3d12::CreateUAVFromSpecificRTV(data.m_out_moments_render_target, cpu_handle, 0, data.m_out_moments_render_target->m_create_info.m_rtv_formats[0]); } { auto cpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(0); d3d12::CreateUAVFromSpecificRTV(data.m_ping_pong_render_target, cpu_handle, 0, data.m_ping_pong_render_target->m_create_info.m_rtv_formats[0]); cpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(1); d3d12::CreateSRVFromSpecificRTV(data.m_ping_pong_render_target, cpu_handle, 0, data.m_ping_pong_render_target->m_create_info.m_rtv_formats[0]); } auto& ps_registry = PipelineRegistry::Get(); data.m_temporal_denoiser_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::reflection_temporal_denoiser); data.m_variance_estimator_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::reflection_variance_estimator); data.m_spatial_denoiser_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::reflection_spatial_denoiser); } inline void BindRenderTargets(ReflectionDenoiserData& data, d3d12::CommandList* cmd_list) { { constexpr unsigned int input = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::INPUT); auto cpu_handle = data.m_input_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle); } { constexpr unsigned int ray_raw = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::RAY_RAW); auto cpu_handle = data.m_ray_raw_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, ray_raw, cpu_handle); } { constexpr unsigned int ray_dir = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::RAY_DIR); auto cpu_handle = data.m_ray_dir_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, ray_dir, cpu_handle); } { constexpr unsigned int albedo_roughness = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::ALBEDO_ROUGHNESS); auto cpu_handle = data.m_albedo_roughness_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, albedo_roughness, cpu_handle); } { constexpr unsigned int normal_metallic = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::NORMAL_METALLIC); auto cpu_handle = data.m_normal_metallic_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, normal_metallic, cpu_handle); } { constexpr unsigned int motion = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::MOTION); auto cpu_handle = data.m_motion_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, motion, cpu_handle); } { constexpr unsigned int linear_depth = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::LINEAR_DEPTH); auto cpu_handle = data.m_linear_depth_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, linear_depth, cpu_handle); } { constexpr unsigned int world_pos = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::WORLD_POS); auto cpu_handle = data.m_world_pos_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, world_pos, cpu_handle); } { constexpr unsigned int in_hist = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::IN_HISTORY); auto cpu_handle = data.m_in_history_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, in_hist, cpu_handle); } { constexpr unsigned int accum = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::ACCUM); auto cpu_handle = data.m_accum_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, accum, cpu_handle); } { constexpr unsigned int prev_normal = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::PREV_NORMAL); auto cpu_handle = data.m_prev_normal_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, prev_normal, cpu_handle); } { constexpr unsigned int prev_depth = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::PREV_DEPTH); auto cpu_handle = data.m_prev_depth_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, prev_depth, cpu_handle); } { constexpr unsigned int in_moments = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::IN_MOMENTS); auto cpu_handle = data.m_in_moments_allocation.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, in_moments, cpu_handle); } { constexpr unsigned int output = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUTPUT); auto cpu_handle = data.m_output_allocation.GetDescriptorHandle(); d3d12::SetShaderUAV(cmd_list, 0, output, cpu_handle); } { constexpr unsigned int out_hist = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUT_HISTORY); auto cpu_handle = data.m_out_history_allocation.GetDescriptorHandle(); d3d12::SetShaderUAV(cmd_list, 0, out_hist, cpu_handle); } { constexpr unsigned int out_moments = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUT_MOMENTS); auto cpu_handle = data.m_out_moments_allocation.GetDescriptorHandle(); d3d12::SetShaderUAV(cmd_list, 0, out_moments, cpu_handle); } } inline void TemporalDenoiser(D3D12RenderSystem& n_render_system, SceneGraph& sg, ReflectionDenoiserData& data, d3d12::CommandList* cmd_list) { const auto viewport = n_render_system.m_viewport; auto frame_idx = n_render_system.GetFrameIdx(); d3d12::BindComputePipeline(cmd_list, data.m_temporal_denoiser_pipeline); BindRenderTargets(data, cmd_list); d3d12::HeapResource* camera_buffer = static_cast(sg.GetActiveCamera()->m_camera_cb)->m_native; d3d12::BindComputeConstantBuffer(cmd_list, camera_buffer, 1, frame_idx); d3d12::HeapResource* denoiser_settings = static_cast(data.m_denoiser_settings_buffer)->m_native; d3d12::BindComputeConstantBuffer(cmd_list, denoiser_settings, 2, frame_idx); d3d12::Dispatch(cmd_list, uint32_t(std::ceil(viewport.m_viewport.Width / 16.f)), uint32_t(std::ceil(viewport.m_viewport.Height / 16.f)), 1); } inline void VarianceEstimator(D3D12RenderSystem& n_render_system, SceneGraph& sg, ReflectionDenoiserData& data, d3d12::CommandList* cmd_list) { const auto viewport = n_render_system.m_viewport; auto frame_idx = n_render_system.GetFrameIdx(); d3d12::BindComputePipeline(cmd_list, data.m_variance_estimator_pipeline); BindRenderTargets(data, cmd_list); d3d12::HeapResource* camera_buffer = static_cast(sg.GetActiveCamera()->m_camera_cb)->m_native; d3d12::BindComputeConstantBuffer(cmd_list, camera_buffer, 1, frame_idx); d3d12::HeapResource* denoiser_settings = static_cast(data.m_denoiser_settings_buffer)->m_native; d3d12::BindComputeConstantBuffer(cmd_list, denoiser_settings, 2, frame_idx); d3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE); { constexpr unsigned int input = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::INPUT); auto cpu_handle = data.m_output_allocation.GetDescriptorHandle(1); d3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle); } d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS); { constexpr unsigned int output = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUTPUT); auto cpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(0); d3d12::SetShaderUAV(cmd_list, 0, output, cpu_handle); } d3d12::Dispatch(cmd_list, uint32_t(std::ceil(viewport.m_viewport.Width / 16.f)), uint32_t(std::ceil(viewport.m_viewport.Height / 16.f)), 1); } inline void SpatialDenoiser(D3D12RenderSystem& n_render_system, SceneGraph& sg, ReflectionDenoiserData& data, d3d12::CommandList* cmd_list) { const auto viewport = n_render_system.m_viewport; auto frame_idx = n_render_system.GetFrameIdx(); d3d12::BindComputePipeline(cmd_list, data.m_spatial_denoiser_pipeline); BindRenderTargets(data, cmd_list); d3d12::HeapResource* camera_buffer = static_cast(sg.GetActiveCamera()->m_camera_cb)->m_native; d3d12::BindComputeConstantBuffer(cmd_list, camera_buffer, 1, frame_idx); d3d12::HeapResource* denoiser_settings = static_cast(data.m_denoiser_settings_buffer)->m_native; d3d12::BindComputeConstantBuffer(cmd_list, denoiser_settings, 2, frame_idx); for (int i = 0; i < data.m_wavelet_sizes.size(); ++i) { d3d12::HeapResource* wavelet_size = static_cast(data.m_wavelet_sizes[i])->m_native; d3d12::BindComputeConstantBuffer(cmd_list, wavelet_size, 3, frame_idx); if (i % 2 != 0) { d3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE); { constexpr unsigned int input = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::INPUT); auto cpu_handle = data.m_output_allocation.GetDescriptorHandle(1); d3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle); } d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS); { constexpr unsigned int output = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUTPUT); auto cpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(0); d3d12::SetShaderUAV(cmd_list, 0, output, cpu_handle); } } else { d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE); { constexpr unsigned int input = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::INPUT); auto cpu_handle = data.m_ping_pong_allocation.GetDescriptorHandle(1); d3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle); } d3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS); { constexpr unsigned int output = rs_layout::GetHeapLoc(params::reflection_denoiser, params::ReflectionDenoiserE::OUTPUT); auto cpu_handle = data.m_output_allocation.GetDescriptorHandle(0); d3d12::SetShaderUAV(cmd_list, 0, output, cpu_handle); } } d3d12::Dispatch(cmd_list, uint32_t(std::ceil(viewport.m_viewport.Width / 16.f)), uint32_t(std::ceil(viewport.m_viewport.Height / 16.f)), 1); if (i == d3d12::settings::shadow_denoiser_feedback_tap) { if (i % 2 != 0) { d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); cmd_list->m_native->CopyResource(data.m_prev_data_render_target->m_render_targets[0], data.m_ping_pong_render_target->m_render_targets[0]); d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); } else { d3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); cmd_list->m_native->CopyResource(data.m_prev_data_render_target->m_render_targets[0], data.m_output_render_target->m_render_targets[0]); d3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); } } } if (data.m_wavelet_sizes.size() % 2 != 0) { d3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); cmd_list->m_native->CopyResource(data.m_output_render_target->m_render_targets[0], data.m_ping_pong_render_target->m_render_targets[0]); d3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::COPY_DEST, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::COPY_SOURCE, ResourceState::NON_PIXEL_SHADER_RESOURCE); } } inline void ExecuteReflectionDenoiserTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& n_device = n_render_system.m_device->m_native; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; fg.WaitForPredecessorTask(); data.m_constant_buffer_pool->Update(data.m_denoiser_settings_buffer, sizeof(data.m_denoiser_settings), 0, frame_idx, (uint8_t*)&data.m_denoiser_settings); d3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); TemporalDenoiser(n_render_system, sg, data, cmd_list); d3d12::Transition(cmd_list, data.m_gbuffer_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_rt_reflection_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); d3d12::Transition(cmd_list, data.m_in_history_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); d3d12::Transition(cmd_list, data.m_out_history_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_in_moments_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); d3d12::Transition(cmd_list, data.m_out_moments_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); cmd_list->m_native->CopyResource(data.m_prev_data_render_target->m_render_targets[1], data.m_gbuffer_render_target->m_render_targets[1]); cmd_list->m_native->CopyResource(data.m_prev_data_render_target->m_render_targets[2], data.m_gbuffer_render_target->m_render_targets[4]); cmd_list->m_native->CopyResource(data.m_in_history_render_target->m_render_targets[0], data.m_out_history_render_target->m_render_targets[0]); cmd_list->m_native->CopyResource(data.m_in_moments_render_target->m_render_targets[0], data.m_out_moments_render_target->m_render_targets[0]); d3d12::Transition(cmd_list, data.m_gbuffer_render_target, ResourceState::COPY_SOURCE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_rt_reflection_render_target, ResourceState::COPY_SOURCE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_prev_data_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_in_history_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_out_history_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_in_moments_render_target, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_out_moments_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); VarianceEstimator(n_render_system, sg, data, cmd_list); SpatialDenoiser(n_render_system, sg, data, cmd_list); d3d12::Transition(cmd_list, data.m_output_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); } inline void DestroyReflectionDenoiserTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& data = fg.GetData(handle); d3d12::Destroy(data.m_prev_data_render_target); d3d12::Destroy(data.m_in_history_render_target); d3d12::Destroy(data.m_out_history_render_target); d3d12::Destroy(data.m_in_moments_render_target); d3d12::Destroy(data.m_out_moments_render_target); d3d12::Destroy(data.m_ping_pong_render_target); } } /* internal */ inline void AddReflectionDenoiserTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(1.f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupReflectionDenoiserTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteReflectionDenoiserTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyReflectionDenoiserTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Reflection denoiser", FG_DEPS()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_rt_hybrid_helpers.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "d3d12_deferred_main.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../rt_pipeline_registry.hpp" #include "../root_signature_registry.hpp" #include "../engine_registry.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_build_acceleration_structures.hpp" #include "../render_tasks/d3d12_cubemap_convolution.hpp" #include "../imgui_tools.hpp" #include namespace wr { struct RTHybrid_BaseData { d3d12::AccelerationStructure out_tlas = {}; // Shader tables std::array out_raygen_shader_table = { nullptr, nullptr, nullptr }; std::array out_miss_shader_table = { nullptr, nullptr, nullptr }; std::array out_hitgroup_shader_table = { nullptr, nullptr, nullptr }; // Pipeline objects d3d12::StateObject* out_state_object = nullptr; d3d12::RootSignature* out_root_signature = nullptr; // Structures and buffers D3D12ConstantBufferHandle* out_cb_camera_handle = nullptr; d3d12::RenderTarget* out_deferred_main_rt = nullptr; unsigned int frame_idx = 0; DescriptorAllocation out_output_alloc; DescriptorAllocation out_gbuffer_albedo_alloc; DescriptorAllocation out_gbuffer_normal_alloc; DescriptorAllocation out_gbuffer_depth_alloc; bool tlas_requires_init = false; }; namespace internal { inline void CreateShaderTables(d3d12::Device* device, RTHybrid_BaseData& data, const std::string& raygen_entry, const std::vector& miss_entries, const std::vector& hit_groups, int frame_idx) { // Delete existing shader table if (data.out_miss_shader_table[frame_idx]) { d3d12::Destroy(data.out_miss_shader_table[frame_idx]); } if (data.out_hitgroup_shader_table[frame_idx]) { d3d12::Destroy(data.out_hitgroup_shader_table[frame_idx]); } if (data.out_raygen_shader_table[frame_idx]) { d3d12::Destroy(data.out_raygen_shader_table[frame_idx]); } // Set up Raygen Shader Table { // Create Record(s) std::uint32_t shader_record_count = 1; auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); auto shader_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, raygen_entry); auto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size); // Create Table data.out_raygen_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); d3d12::AddShaderRecord(data.out_raygen_shader_table[frame_idx], shader_record); } // Set up Miss Shader Table { // Create Record(s) and Table(s) std::uint32_t shader_record_count = miss_entries.size(); auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); data.out_miss_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); for (auto& entry : miss_entries) { auto miss_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, entry); auto miss_record = d3d12::CreateShaderRecord(miss_identifier, shader_identifier_size); d3d12::AddShaderRecord(data.out_miss_shader_table[frame_idx], miss_record); } } // Set up Hit Group Shader Table { // Create Record(s) std::uint32_t shader_record_count = hit_groups.size(); auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); data.out_hitgroup_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); for (auto& entry : hit_groups) { auto hit_identifier = d3d12::GetShaderIdentifier(device, data.out_state_object, entry); auto hit_record = d3d12::CreateShaderRecord(hit_identifier, shader_identifier_size); d3d12::AddShaderRecord(data.out_hitgroup_shader_table[frame_idx], hit_record); } } } inline void CreateUAVsAndSRVs(FrameGraph& fg, RTHybrid_BaseData& data, d3d12::RenderTarget* n_render_target) { // Versioning for (int frame_idx = 0; frame_idx < 1; ++frame_idx) { // Bind output texture d3d12::DescHeapCPUHandle rtv_handle = data.out_output_alloc.GetDescriptorHandle(0); d3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); rtv_handle = data.out_output_alloc.GetDescriptorHandle(1); d3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 1, n_render_target->m_create_info.m_rtv_formats[1]); rtv_handle = data.out_output_alloc.GetDescriptorHandle(2); d3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 2, n_render_target->m_create_info.m_rtv_formats[2]); // Bind g-buffers (albedo, normal, depth) auto albedo_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle(); auto normal_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(); auto depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle(); auto deferred_main_rt = data.out_deferred_main_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, albedo_handle, 0, deferred_main_rt->m_create_info.m_rtv_formats[0]); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats[1]); d3d12::CreateSRVFromDSV(deferred_main_rt, depth_handle); } } } /* internal */ } /* wr */ ================================================ FILE: src/render_tasks/d3d12_rt_reflection_task.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/scene_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../engine_registry.hpp" #include "d3d12_deferred_main.hpp" #include "d3d12_build_acceleration_structures.hpp" #include "d3d12_rt_hybrid_helpers.hpp" #include "../imgui_tools.hpp" namespace wr { struct RTReflectionData { RTHybrid_BaseData base_data; }; namespace internal { inline void SetupRTReflectionTask(RenderSystem& render_system, FrameGraph & fg, RenderTaskHandle & handle, bool resize) { // Initialize variables auto& n_render_system = static_cast(render_system); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); d3d12::SetName(n_render_target, L"Raytracing Reflection Target"); if (!resize) { auto cmd_list = fg.GetCommandList(handle); // Get AS build data auto& as_build_data = fg.GetPredecessorData(); data.base_data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate(3)); data.base_data.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate()); data.base_data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate()); data.base_data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate()); data.base_data.tlas_requires_init = true; } CreateUAVsAndSRVs(fg, data.base_data, n_render_target); if (!resize) { // Camera constant buffer data.base_data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData))); // Pipeline State Object auto& rt_registry = RTPipelineRegistry::Get(); data.base_data.out_state_object = static_cast(rt_registry.Find(state_objects::rt_reflection_state_object)); // Root Signature auto& rs_registry = RootSignatureRegistry::Get(); data.base_data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_hybrid_global)); } // Create Shader Tables for (int i = 0; i < d3d12::settings::num_back_buffers; ++i) { CreateShaderTables(device, data.base_data, "ReflectionRaygenEntry", { "ReflectionMiss", "ShadowMissEntry" }, { "ReflectionHitGroup", "ShadowHitGroup" }, i); } // Setup frame index data.base_data.frame_idx = 0; } inline void ExecuteRTReflectionTask(RenderSystem & render_system, FrameGraph & fg, SceneGraph & scene_graph, RenderTaskHandle & handle) { // Initialize variables auto& n_render_system = static_cast(render_system); auto cmd_list = fg.GetCommandList(handle); auto& data = fg.GetData(handle); auto window = n_render_system.m_window.value(); auto render_target = fg.GetRenderTarget(handle); auto device = n_render_system.m_device; auto& as_build_data = fg.GetPredecessorData(); auto frame_idx = n_render_system.GetFrameIdx(); float scalar = 1.0f; fg.WaitForPredecessorTask(); // Rebuild acceleratrion structure a 2e time for fallback if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { d3d12::CreateOrUpdateTLAS(device, cmd_list, data.base_data.tlas_requires_init, data.base_data.out_tlas, as_build_data.out_blas_list, frame_idx); d3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx); } if (n_render_system.m_render_window.has_value()) { d3d12::BindRaytracingPipeline(cmd_list, data.base_data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); // Bind output, indices and materials, offsets, etc auto out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(0); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)), out_uav_handle); out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(1); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)) + 1, out_uav_handle); out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(2); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)) + 2, out_uav_handle); auto out_scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::INDICES)), out_scene_ib_handle); auto out_scene_mat_handle = as_build_data.out_scene_mat_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::MATERIALS)), out_scene_mat_handle); auto out_scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OFFSETS)), out_scene_offset_handle); auto out_albedo_gbuffer_handle = data.base_data.out_gbuffer_albedo_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 0, out_albedo_gbuffer_handle); auto out_normal_gbuffer_handle = data.base_data.out_gbuffer_normal_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 1, out_normal_gbuffer_handle); auto out_scene_depth_handle = data.base_data.out_gbuffer_depth_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 2, out_scene_depth_handle); /* To keep the CopyDescriptors function happy, we need to fill the descriptor table with valid descriptors We fill the table with a single descriptor, then overwrite some spots with the he correct textures If a spot is unused, then a default descriptor will be still bound, but not used in the shaders. Since the renderer creates a texture pool that can be used by the render tasks, and the texture pool also has default textures for albedo/roughness/etc... one of those textures is a good candidate for this. */ { auto texture_handle = n_render_system.GetDefaultAlbedo(); auto* texture_resource = static_cast(texture_handle.m_pool->GetTextureResource(texture_handle)); size_t num_textures_in_heap = COMPILATION_EVAL(rs_layout::GetSize(params::rt_hybrid, params::RTHybridE::TEXTURES)); unsigned int heap_loc_start = COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES)); for (size_t i = 0; i < num_textures_in_heap; ++i) { d3d12::SetRTShaderSRV(cmd_list, 0, static_cast(heap_loc_start + i), texture_resource); } } // Fill descriptor heap with textures used by the scene for (auto material_handle : as_build_data.out_material_handles) { auto* material_internal = material_handle.m_pool->GetMaterial(material_handle); auto set_srv = [&data, material_internal, cmd_list](auto texture_handle) { auto* texture_internal = static_cast(texture_handle.m_pool->GetTextureResource(texture_handle)); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES)) + static_cast(texture_handle.m_id), texture_internal); }; std::array(TextureType::COUNT)> types = { TextureType::ALBEDO, TextureType::NORMAL, TextureType::ROUGHNESS, TextureType::METALLIC, TextureType::EMISSIVE, TextureType::AO }; for (auto t : types) { if (material_internal->HasTexture(t)) set_srv(material_internal->GetTexture(t)); } } // Get light buffer if (static_cast(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE) { static_cast(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE); } DescriptorAllocation light_alloc = std::move(as_build_data.out_allocator->Allocate()); d3d12::DescHeapCPUHandle light_handle = light_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromStructuredBuffer(static_cast(scene_graph.GetLightBuffer())->m_native, light_handle, frame_idx); d3d12::DescHeapCPUHandle light_handle2 = light_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::LIGHTS)), light_handle2); // Update offset data n_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); // Update material data if (as_build_data.out_materials_require_update) { n_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); } // Update camera constant buffer auto camera = scene_graph.GetActiveCamera(); temp::RTHybridCamera_CBData cam_data; cam_data.m_inverse_view = DirectX::XMMatrixInverse(nullptr, camera->m_view); cam_data.m_inverse_projection = DirectX::XMMatrixInverse(nullptr, camera->m_projection); cam_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection); cam_data.m_intensity = n_render_system.temp_intensity; cam_data.m_frame_idx = static_cast(++data.base_data.frame_idx); n_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? // Make sure the convolution pass wrote to the skybox. fg.WaitForPredecessorTask(); // Get skybox if (SkyboxNode * skybox = scene_graph.GetCurrentSkybox().get()) { auto skybox_t = static_cast(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::SKYBOX)), skybox_t); // Get Pre-filtered environment auto irradiance_t = static_cast(skybox->m_prefiltered_env_map->m_pool->GetTextureResource(skybox->m_prefiltered_env_map.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::PREF_ENV_MAP)), irradiance_t); // Get Environment Map irradiance_t = static_cast(scene_graph.GetCurrentSkybox()->m_irradiance->m_pool->GetTextureResource(scene_graph.GetCurrentSkybox()->m_irradiance.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::IRRADIANCE_MAP)), irradiance_t); } // Get brdf lookup texture auto brdf_lut_text = static_cast(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::BRDF_LUT)), brdf_lut_text); // Transition depth to NON_PIXEL_RESOURCE d3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::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); d3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); d3d12::BindComputeConstantBuffer(cmd_list, data.base_data.out_cb_camera_handle->m_native, 2, frame_idx); if (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE) { d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1); } else if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { cmd_list->m_native_fallback->SetTopLevelAccelerationStructure(0, as_build_data.out_tlas.m_fallback_tlas_ptr); } /*unsigned int verts_loc = 3; rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::VERTICES); This 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... Much love, Meine and Florian*/ d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3); //#ifdef _DEBUG CreateShaderTables(device, data.base_data, "ReflectionRaygenEntry", { "ReflectionMiss", "ShadowMissEntry" }, { "ReflectionHitGroup", "ShadowHitGroup" }, frame_idx); //#endif // Dispatch hybrid ray tracing rays d3d12::DispatchRays(cmd_list, data.base_data.out_hitgroup_shader_table[frame_idx], data.base_data.out_miss_shader_table[frame_idx], data.base_data.out_raygen_shader_table[frame_idx], static_cast(std::ceil(d3d12::GetRenderTargetWidth(render_target))), static_cast(std::ceil(d3d12::GetRenderTargetHeight(render_target))), 1, frame_idx); // Transition depth back to DEPTH_WRITE d3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); } } inline void DestroyRTReflectionTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if(!resize) { RTReflectionData& data = fg.GetData(handle); for(d3d12::ShaderTable* shader : data.base_data.out_raygen_shader_table) { delete shader; } for(d3d12::ShaderTable* shader : data.base_data.out_miss_shader_table) { delete shader; } for(d3d12::ShaderTable* shader : data.base_data.out_hitgroup_shader_table) { delete shader; } } } } /* internal */ inline void AddRTReflectionTask(FrameGraph& fg) { std::wstring name(L"Hybrid raytracing reflections"); RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT, wr::Format::R8_UNORM, wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(3), RenderTargetProperties::Clear(true), RenderTargetProperties::ClearDepth(true), RenderTargetProperties::ResolutionScalar(0.5f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupRTReflectionTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteRTReflectionTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyRTReflectionTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; fg.AddTask(desc, name, FG_DEPS()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_rt_shadow_task.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/scene_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../engine_registry.hpp" #include "d3d12_build_acceleration_structures.hpp" #include "d3d12_deferred_main.hpp" #include "d3d12_rt_hybrid_helpers.hpp" #include "../imgui_tools.hpp" namespace wr { struct RTShadowSettings { struct Runtime { float m_epsilon = 0.01f; int m_sample_count = 1; }; Runtime m_runtime; }; struct RTShadowData { RTHybrid_BaseData base_data; }; namespace internal { inline void SetupRTShadowTask(RenderSystem & render_system, FrameGraph & fg, RenderTaskHandle & handle, bool resize) { // Initialize variables auto& n_render_system = static_cast(render_system); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); d3d12::SetName(n_render_target, L"Raytracing Shadow Target"); if (!resize) { auto cmd_list = fg.GetCommandList(handle); // Get AS build data auto& as_build_data = fg.GetPredecessorData(); data.base_data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate(3)); data.base_data.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate()); data.base_data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate()); data.base_data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate()); data.base_data.tlas_requires_init = true; } // Versioning CreateUAVsAndSRVs(fg, data.base_data, n_render_target); if (!resize) { // Camera constant buffer data.base_data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData))); // Pipeline State Object auto& rt_registry = RTPipelineRegistry::Get(); data.base_data.out_state_object = static_cast(rt_registry.Find(state_objects::rt_shadow_state_object)); // Root Signature auto& rs_registry = RootSignatureRegistry::Get(); data.base_data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_hybrid_global)); } // Create Shader Tables for (int i = 0; i < d3d12::settings::num_back_buffers; ++i) { CreateShaderTables(device, data.base_data, "ShadowRaygenEntry", { "ShadowMissEntry" }, { "ShadowHitGroup" }, i); } // Setup frame index data.base_data.frame_idx = 0; } inline void ExecuteRTShadowTask(RenderSystem & render_system, FrameGraph & fg, SceneGraph & scene_graph, RenderTaskHandle & handle) { // Initialize variables auto& n_render_system = static_cast(render_system); auto cmd_list = fg.GetCommandList(handle); auto& data = fg.GetData(handle); auto window = n_render_system.m_window.value(); auto render_target = fg.GetRenderTarget(handle); auto device = n_render_system.m_device; auto& as_build_data = fg.GetPredecessorData(); auto frame_idx = n_render_system.GetFrameIdx(); auto settings = fg.GetSettings(); float scalar = 1.0f; fg.WaitForPredecessorTask(); // Rebuild acceleratrion structure a 2e time for fallback if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { d3d12::CreateOrUpdateTLAS(device, cmd_list, data.base_data.tlas_requires_init, data.base_data.out_tlas, as_build_data.out_blas_list, frame_idx); d3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx); } if (n_render_system.m_render_window.has_value()) { d3d12::BindRaytracingPipeline(cmd_list, data.base_data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); // Bind output, indices and materials, offsets, etc auto out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)), out_uav_handle); out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(1); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)) + 1, out_uav_handle); out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(2); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)) + 2, out_uav_handle); auto out_scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::INDICES)), out_scene_ib_handle); auto out_scene_mat_handle = as_build_data.out_scene_mat_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::MATERIALS)), out_scene_mat_handle); auto out_scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OFFSETS)), out_scene_offset_handle); auto out_albedo_gbuffer_handle = data.base_data.out_gbuffer_albedo_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 0, out_albedo_gbuffer_handle); auto out_normal_gbuffer_handle = data.base_data.out_gbuffer_normal_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 1, out_normal_gbuffer_handle); auto out_scene_depth_handle = data.base_data.out_gbuffer_depth_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 2, out_scene_depth_handle); /* To keep the CopyDescriptors function happy, we need to fill the descriptor table with valid descriptors We fill the table with a single descriptor, then overwrite some spots with the he correct textures If a spot is unused, then a default descriptor will be still bound, but not used in the shaders. Since the renderer creates a texture pool that can be used by the render tasks, and the texture pool also has default textures for albedo/roughness/etc... one of those textures is a good candidate for this. */ { auto texture_handle = n_render_system.GetDefaultAlbedo(); auto* texture_resource = static_cast(texture_handle.m_pool->GetTextureResource(texture_handle)); size_t num_textures_in_heap = COMPILATION_EVAL(rs_layout::GetSize(params::rt_hybrid, params::RTHybridE::TEXTURES)); unsigned int heap_loc_start = COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES)); for (size_t i = 0; i < num_textures_in_heap; ++i) { d3d12::SetRTShaderSRV(cmd_list, 0, static_cast(heap_loc_start + i), texture_resource); } } // Fill descriptor heap with textures used by the scene for (auto material_handle : as_build_data.out_material_handles) { auto* material_internal = material_handle.m_pool->GetMaterial(material_handle); auto set_srv = [&data, material_internal, cmd_list](auto texture_handle) { auto* texture_internal = static_cast(texture_handle.m_pool->GetTextureResource(texture_handle)); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::TEXTURES)) + static_cast(texture_handle.m_id), texture_internal); }; std::array(TextureType::COUNT)> types = { TextureType::ALBEDO, TextureType::NORMAL, TextureType::ROUGHNESS, TextureType::METALLIC, TextureType::EMISSIVE, TextureType::AO }; for (auto t : types) { if (material_internal->HasTexture(t)) set_srv(material_internal->GetTexture(t)); } } // Get light buffer if (static_cast(scene_graph.GetLightBuffer())->m_native->m_states[frame_idx] != ResourceState::NON_PIXEL_SHADER_RESOURCE) { static_cast(scene_graph.GetLightBuffer()->m_pool)->SetBufferState(scene_graph.GetLightBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE); } DescriptorAllocation light_alloc = std::move(as_build_data.out_allocator->Allocate()); d3d12::DescHeapCPUHandle light_handle = light_alloc.GetDescriptorHandle(); d3d12::CreateSRVFromStructuredBuffer(static_cast(scene_graph.GetLightBuffer())->m_native, light_handle, frame_idx); d3d12::DescHeapCPUHandle light_handle2 = light_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::LIGHTS)), light_handle2); // Update offset data n_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); // Update material data if (as_build_data.out_materials_require_update) { n_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); } // Update camera constant buffer auto camera = scene_graph.GetActiveCamera(); temp::RTHybridCamera_CBData cam_data; cam_data.m_inverse_view = DirectX::XMMatrixInverse(nullptr, camera->m_view); cam_data.m_inverse_projection = DirectX::XMMatrixInverse(nullptr, camera->m_projection); cam_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection); cam_data.m_intensity = n_render_system.temp_intensity; cam_data.m_frame_idx = static_cast(++data.base_data.frame_idx); cam_data.m_epsilon = settings.m_runtime.m_epsilon; cam_data.m_sample_count = settings.m_runtime.m_sample_count; n_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? // Make sure the convolution pass wrote to the skybox. fg.WaitForPredecessorTask(); // Get skybox if (SkyboxNode * skybox = scene_graph.GetCurrentSkybox().get()) { auto skybox_t = static_cast(skybox->m_skybox->m_pool->GetTextureResource(skybox->m_skybox.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::SKYBOX)), skybox_t); // Get Pre-filtered environment auto irradiance_t = static_cast(skybox->m_prefiltered_env_map->m_pool->GetTextureResource(skybox->m_prefiltered_env_map.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::PREF_ENV_MAP)), irradiance_t); // Get Environment Map irradiance_t = static_cast(scene_graph.GetCurrentSkybox()->m_irradiance->m_pool->GetTextureResource(scene_graph.GetCurrentSkybox()->m_irradiance.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::IRRADIANCE_MAP)), irradiance_t); } // Get brdf lookup texture auto brdf_lut_text = static_cast(n_render_system.m_brdf_lut.value().m_pool->GetTextureResource(n_render_system.m_brdf_lut.value())); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::BRDF_LUT)), brdf_lut_text); // Transition depth to NON_PIXEL_RESOURCE d3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::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); d3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); d3d12::BindComputeConstantBuffer(cmd_list, data.base_data.out_cb_camera_handle->m_native, 2, frame_idx); if (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE) { d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1); } else if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { cmd_list->m_native_fallback->SetTopLevelAccelerationStructure(0, as_build_data.out_tlas.m_fallback_tlas_ptr); } /*unsigned int verts_loc = 3; rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::VERTICES); This 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... Much love, Meine and Florian*/ d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3); //#ifdef _DEBUG CreateShaderTables(device, data.base_data, "ShadowRaygenEntry", { "ShadowMissEntry" }, { "ShadowHitGroup" }, frame_idx); //#endif // Dispatch hybrid ray tracing rays d3d12::DispatchRays(cmd_list, data.base_data.out_hitgroup_shader_table[frame_idx], data.base_data.out_miss_shader_table[frame_idx], data.base_data.out_raygen_shader_table[frame_idx], static_cast(std::ceil(d3d12::GetRenderTargetWidth(render_target))), static_cast(std::ceil(d3d12::GetRenderTargetHeight(render_target))), 1, frame_idx); // Transition depth back to DEPTH_WRITE d3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); } } inline void DestroyRTShadowTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if(!resize) { RTShadowData& data = fg.GetData(handle); for (d3d12::ShaderTable* shader : data.base_data.out_raygen_shader_table) { delete shader; } for (d3d12::ShaderTable* shader : data.base_data.out_miss_shader_table) { delete shader; } for (d3d12::ShaderTable* shader : data.base_data.out_hitgroup_shader_table) { delete shader; } } } } /* internal */ inline void AddRTShadowTask(FrameGraph& fg) { std::wstring name(L"Hybrid raytracing shadows"); RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT, wr::Format::R8_UNORM, wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(3), RenderTargetProperties::Clear(true), RenderTargetProperties::ClearDepth(true), RenderTargetProperties::ResolutionScalar(1.0f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupRTShadowTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteRTShadowTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyRTShadowTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; fg.AddTask(desc, name, FG_DEPS()); fg.UpdateSettings(RTShadowSettings()); } } /* wr */ ================================================ FILE: src/render_tasks/d3d12_rtao_task.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../rt_pipeline_registry.hpp" #include "../root_signature_registry.hpp" #include "../engine_registry.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_build_acceleration_structures.hpp" namespace wr { struct RTAOSettings { struct Runtime { float bias = 0.05f; float radius = 0.5f; float power = 1.f; // The final AO output is pow(AO, powerExponent) // 1.0~4.0 float max_distance = 20.f; int sample_count = 8; };//Currently setup to make work with retardedly small scenes Runtime m_runtime; }; struct RTAOData { d3d12::AccelerationStructure out_tlas = {}; // Shader tables std::array in_raygen_shader_table = { nullptr, nullptr, nullptr }; std::array in_miss_shader_table = { nullptr, nullptr, nullptr }; std::array in_hitgroup_shader_table = { nullptr, nullptr, nullptr }; // Pipeline objects d3d12::StateObject* in_state_object; d3d12::RootSignature* in_root_signature; D3D12ConstantBufferHandle* out_cb_handle; d3d12::RenderTarget* in_deferred_main_rt; DescriptorAllocation out_uav_from_rtv; DescriptorAllocation in_gbuffers; DescriptorAllocation in_depthbuffer; bool tlas_requires_init = false; }; namespace internal { inline void CreateShaderTables(d3d12::Device* device, RTAOData& data, int frame_idx) { // Delete existing shader table if (data.in_miss_shader_table[frame_idx]) { d3d12::Destroy(data.in_miss_shader_table[frame_idx]); } if (data.in_hitgroup_shader_table[frame_idx]) { d3d12::Destroy(data.in_hitgroup_shader_table[frame_idx]); } if (data.in_raygen_shader_table[frame_idx]) { d3d12::Destroy(data.in_raygen_shader_table[frame_idx]); } // Set up Raygen Shader Table { // Create Record(s) UINT shader_record_count = 1; auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); auto shader_identifier = d3d12::GetShaderIdentifier(device, data.in_state_object, "AORaygenEntry"); auto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size); // Create Table data.in_raygen_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); d3d12::AddShaderRecord(data.in_raygen_shader_table[frame_idx], shader_record); } // Set up Miss Shader Table { // Create Record(s) UINT shader_record_count = 1; auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); auto miss_identifier = d3d12::GetShaderIdentifier(device, data.in_state_object, "MissEntry"); auto miss_record = d3d12::CreateShaderRecord(miss_identifier, shader_identifier_size); // Create Table(s) data.in_miss_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); d3d12::AddShaderRecord(data.in_miss_shader_table[frame_idx], miss_record); } } inline void SetupAOTask(RenderSystem& render_system, FrameGraph& fg, RenderTaskHandle& handle, bool resize) { // Initialize variables auto& n_render_system = static_cast(render_system); auto& device = n_render_system.m_device; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); d3d12::SetName(n_render_target, L"AO Target"); if (!resize) { auto& as_build_data = fg.GetPredecessorData(); data.out_uav_from_rtv = std::move(as_build_data.out_allocator->Allocate(1)); data.in_gbuffers = std::move(as_build_data.out_allocator->Allocate(1)); data.in_depthbuffer = std::move(as_build_data.out_allocator->Allocate(1)); } // Versioning for (int frame_idx = 0; frame_idx < 1; ++frame_idx) { // Bind output texture d3d12::DescHeapCPUHandle rtv_handle = data.out_uav_from_rtv.GetDescriptorHandle(); d3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); // Bind g-buffers d3d12::DescHeapCPUHandle normal_gbuffer_handle = data.in_gbuffers.GetDescriptorHandle(0); d3d12::DescHeapCPUHandle depth_buffer_handle = data.in_depthbuffer.GetDescriptorHandle(0); auto deferred_main_rt = data.in_deferred_main_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_gbuffer_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats.data()[1]); d3d12::CreateSRVFromDSV(deferred_main_rt, depth_buffer_handle); } if (!resize) { // Camera constant buffer data.out_cb_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTAO_CBData))); // Pipeline State Object auto& rt_registry = RTPipelineRegistry::Get(); data.in_state_object = static_cast(rt_registry.Find(state_objects::rt_ao_state_opbject)); // Root Signature auto& rs_registry = RootSignatureRegistry::Get(); data.in_root_signature = static_cast(rs_registry.Find(root_signatures::rt_ao_global)); // Create Shader Tables CreateShaderTables(device, data, 0); CreateShaderTables(device, data, 1); CreateShaderTables(device, data, 2); } } inline void ExecuteAOTask(RenderSystem & render_system, FrameGraph & fg, SceneGraph & scene_graph, RenderTaskHandle & handle) { // Initialize variables auto& n_render_system = static_cast(render_system); auto window = n_render_system.m_window.value(); auto render_target = fg.GetRenderTarget(handle); auto device = n_render_system.m_device; auto cmd_list = fg.GetCommandList(handle); auto& data = fg.GetData(handle); auto& as_build_data = fg.GetPredecessorData(); auto frame_idx = n_render_system.GetFrameIdx(); auto settings = fg.GetSettings(); fg.WaitForPredecessorTask(); float scalar = 1.0f; if (n_render_system.m_render_window.has_value()) { d3d12::BindRaytracingPipeline(cmd_list, data.in_state_object, false); // Bind output, indices and materials, offsets, etc auto out_uav_handle = data.out_uav_from_rtv.GetDescriptorHandle(); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::OUTPUT)), out_uav_handle); auto in_scene_normal_gbuffer_handle = data.in_gbuffers.GetDescriptorHandle(0); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::GBUFFERS)) + 0, in_scene_normal_gbuffer_handle); auto in_scene_depth_handle = data.in_depthbuffer.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::GBUFFERS)) + 1, in_scene_depth_handle); // Update offset data n_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); // Update material data if (as_build_data.out_materials_require_update) { n_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); } // Update constant buffer auto camera = scene_graph.GetActiveCamera(); temp::RTAO_CBData cb_data; cb_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection); cb_data.m_inv_view = DirectX::XMMatrixInverse(nullptr, camera->m_view); cb_data.m_bias = settings.m_runtime.bias; cb_data.m_radius = settings.m_runtime.radius; cb_data.m_power = settings.m_runtime.power; cb_data.m_max_distance = settings.m_runtime.max_distance; cb_data.m_frame_idx = frame_idx; cb_data.m_sample_count = static_cast(settings.m_runtime.sample_count); n_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? // Transition depth to NON_PIXEL_RESOURCE d3d12::TransitionDepth(cmd_list, data.in_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, false); d3d12::BindDescriptorHeaps(cmd_list, false); d3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_handle->m_native, 2, frame_idx); if (!as_build_data.out_blas_list.empty()) { d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_tlas.m_natives[frame_idx], 1); } #ifdef _DEBUG CreateShaderTables(device, data, frame_idx); #endif // _DEBUG // Dispatch hybrid ray tracing rays d3d12::DispatchRays(cmd_list, data.in_hitgroup_shader_table[frame_idx], data.in_miss_shader_table[frame_idx], data.in_raygen_shader_table[frame_idx], static_cast(std::ceil(d3d12::GetRenderTargetWidth(render_target))), static_cast(std::ceil(d3d12::GetRenderTargetHeight(render_target))), 1, frame_idx); // Transition depth back to DEPTH_WRITE d3d12::TransitionDepth(cmd_list, data.in_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); } } inline void DestroyAOTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { if(!resize) { RTAOData& data = fg.GetData(handle); for(d3d12::ShaderTable* shader : data.in_raygen_shader_table) { delete shader; } for(d3d12::ShaderTable* shader : data.in_miss_shader_table) { delete shader; } for(d3d12::ShaderTable* shader : data.in_hitgroup_shader_table) { delete shader; } } } } inline void AddRTAOTask(FrameGraph& fg, d3d12::Device* device) { if (wr::d3d12::GetRaytracingType(device) == wr::RaytracingType::NATIVE)//We do not support fallback layer for shadow rays. { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ Format::R8_UNORM}), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(true), RenderTargetProperties::ClearDepth(true), }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem & rs, FrameGraph & fg, RenderTaskHandle handle, bool resize) { internal::SetupAOTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem & rs, FrameGraph & fg, SceneGraph & sg, RenderTaskHandle handle) { internal::ExecuteAOTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph & fg, RenderTaskHandle handle, bool resize) { internal::DestroyAOTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; fg.AddTask(desc, L"Ray Traced Ambient Occlusion"); fg.UpdateSettings(RTAOSettings()); } else { LOGW("RTAO task was not added since the fallback layer is not supported for RTAO. Consider using HBAO+ instead.") } } }// namespace wr ================================================ FILE: src/render_tasks/d3d12_shadow_denoiser_task.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../d3d12/d3d12_structured_buffer_pool.hpp" #include "../frame_graph/frame_graph.hpp" #include "../scene_graph/scene_graph.hpp" #include "../scene_graph/camera_node.hpp" #include "../engine_registry.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_rt_shadow_task.hpp" #include namespace wr { struct ShadowDenoiserSettings { struct Runtime { float m_alpha = 0.05f; float m_moments_alpha = 0.2f; float m_l_phi = 4.f; float m_n_phi = 128.f; float m_z_phi = 1.0f; }; Runtime m_runtime; }; struct ShadowDenoiserData { d3d12::PipelineState* m_reprojection_pipeline; d3d12::PipelineState* m_filter_moments_pipeline; d3d12::PipelineState* m_wavelet_filter_pipeline; DescriptorAllocator* out_allocator; DescriptorAllocation out_allocation; std::shared_ptr m_constant_buffer_pool; temp::ShadowDenoiserSettings_CBData m_denoiser_settings; std::array m_denoiser_settings_buffer; ConstantBufferHandle* m_denoiser_camera; d3d12::RenderTarget* m_input_render_target; d3d12::RenderTarget* m_gbuffer_render_target; d3d12::RenderTarget* m_in_hist_length; d3d12::RenderTarget* m_in_prev_color; d3d12::RenderTarget* m_in_prev_moments; d3d12::RenderTarget* m_in_prev_normals; d3d12::RenderTarget* m_in_prev_depth; d3d12::RenderTarget* m_out_color_render_target; d3d12::RenderTarget* m_out_moments_render_target; d3d12::RenderTarget* m_out_hist_length_render_target; d3d12::RenderTarget* m_ping_pong_render_target; }; namespace internal { inline void SetupShadowDenoiserTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto& ps_registry = PipelineRegistry::Get(); data.m_reprojection_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::svgf_denoiser_reprojection); data.m_filter_moments_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::svgf_denoiser_filter_moments); data.m_wavelet_filter_pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::svgf_denoiser_wavelet_filter); //Retrieve the texture pool from the render system. It will be used to allocate temporary cpu visible descriptors std::shared_ptr texture_pool = std::static_pointer_cast(n_render_system.m_texture_pools[0]); if (!texture_pool) { LOGC("Texture pool is nullptr. This shouldn't happen as the render system should always create the first texture pool"); } data.out_allocator = texture_pool->GetAllocator(DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.out_allocation = std::move(data.out_allocator->Allocate(15)); if (!resize) { data.m_constant_buffer_pool = n_render_system.CreateConstantBufferPool( SizeAlignTwoPower( sizeof(temp::ShadowDenoiserSettings_CBData), 256) * d3d12::settings::num_back_buffers * data.m_denoiser_settings_buffer.size()); for (int i = 0; i < data.m_denoiser_settings_buffer.size(); ++i) { data.m_denoiser_settings_buffer[i] = data.m_constant_buffer_pool->Create(sizeof(temp::ShadowDenoiserSettings_CBData)); } } data.m_input_render_target = static_cast(fg.GetPredecessorRenderTarget()); data.m_gbuffer_render_target = static_cast(fg.GetPredecessorRenderTarget()); d3d12::desc::RenderTargetDesc render_target_desc = {}; render_target_desc.m_clear_color[0] = 0.f; render_target_desc.m_clear_color[1] = 0.f; render_target_desc.m_clear_color[2] = 0.f; render_target_desc.m_clear_color[3] = 0.f; render_target_desc.m_create_dsv_buffer = false; render_target_desc.m_dsv_format = Format::UNKNOWN; render_target_desc.m_initial_state = ResourceState::NON_PIXEL_SHADER_RESOURCE; render_target_desc.m_num_rtv_formats = 1; render_target_desc.m_rtv_formats[0] = Format::R16_FLOAT; data.m_in_hist_length = d3d12::CreateRenderTarget( n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc ); render_target_desc.m_rtv_formats[0] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[0]; data.m_in_prev_color = d3d12::CreateRenderTarget( n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc ); render_target_desc.m_rtv_formats[0] = Format::R32G32_FLOAT; render_target_desc.m_create_dsv_buffer = false; data.m_in_prev_moments = d3d12::CreateRenderTarget( n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc ); render_target_desc.m_rtv_formats[0] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[1]; data.m_in_prev_normals = d3d12::CreateRenderTarget( n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc ); render_target_desc.m_rtv_formats[0] = data.m_gbuffer_render_target->m_create_info.m_rtv_formats[4]; data.m_in_prev_depth = d3d12::CreateRenderTarget( n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc ); data.m_out_color_render_target = fg.GetRenderTarget(handle); render_target_desc.m_rtv_formats[0] = Format::R32G32_FLOAT; render_target_desc.m_initial_state = ResourceState::UNORDERED_ACCESS; data.m_out_moments_render_target = d3d12::CreateRenderTarget( n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc ); render_target_desc.m_rtv_formats[0] = data.m_in_hist_length->m_create_info.m_rtv_formats[0]; data.m_out_hist_length_render_target = d3d12::CreateRenderTarget( n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc ); render_target_desc.m_rtv_formats[0] = data.m_out_color_render_target->m_create_info.m_rtv_formats[0]; data.m_ping_pong_render_target = d3d12::CreateRenderTarget( n_render_system.m_device, n_render_system.m_viewport.m_viewport.Width, n_render_system.m_viewport.m_viewport.Height, render_target_desc ); { constexpr unsigned int input = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT); auto cpu_handle = data.out_allocation.GetDescriptorHandle(input); d3d12::CreateSRVFromSpecificRTV(data.m_input_render_target, cpu_handle, 0, data.m_input_render_target->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int motion = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::MOTION); auto cpu_handle = data.out_allocation.GetDescriptorHandle(motion); d3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 3, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[3]); } { constexpr unsigned int normals = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::NORMAL); auto cpu_handle = data.out_allocation.GetDescriptorHandle(normals); d3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 1, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[1]); } { constexpr unsigned int depth = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::DEPTH); auto cpu_handle = data.out_allocation.GetDescriptorHandle(depth); d3d12::CreateSRVFromSpecificRTV(data.m_gbuffer_render_target, cpu_handle, 4, data.m_gbuffer_render_target->m_create_info.m_rtv_formats[4]); } { constexpr unsigned int hist_length = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::IN_HIST_LENGTH); auto cpu_handle = data.out_allocation.GetDescriptorHandle(hist_length); d3d12::CreateSRVFromSpecificRTV(data.m_in_hist_length, cpu_handle, 0, data.m_in_hist_length->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int prev_color = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_INPUT); auto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_color); d3d12::CreateSRVFromSpecificRTV(data.m_in_prev_color, cpu_handle, 0, data.m_in_prev_color->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int prev_moments = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_MOMENTS); auto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_moments); d3d12::CreateSRVFromSpecificRTV(data.m_in_prev_moments, cpu_handle, 0, data.m_in_prev_moments->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int prev_normals = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_NORMAL); auto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_normals); d3d12::CreateSRVFromSpecificRTV(data.m_in_prev_normals, cpu_handle, 0, data.m_in_prev_normals->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int prev_depth = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_DEPTH); auto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_depth); d3d12::CreateSRVFromSpecificRTV(data.m_in_prev_depth, cpu_handle, 0, data.m_in_prev_depth->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int out_color = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR); auto cpu_handle = data.out_allocation.GetDescriptorHandle(out_color); d3d12::CreateUAVFromSpecificRTV(data.m_out_color_render_target, cpu_handle, 0, data.m_out_color_render_target->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int out_moments = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_MOMENTS); auto cpu_handle = data.out_allocation.GetDescriptorHandle(out_moments); d3d12::CreateUAVFromSpecificRTV(data.m_out_moments_render_target, cpu_handle, 0, data.m_out_moments_render_target->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int out_hist_length = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_HIST_LENGTH); auto cpu_handle = data.out_allocation.GetDescriptorHandle(out_hist_length); d3d12::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]); } { constexpr unsigned int input_uav = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_UAV); auto cpu_handle = data.out_allocation.GetDescriptorHandle(input_uav); d3d12::CreateUAVFromSpecificRTV(data.m_ping_pong_render_target, cpu_handle, 0, data.m_ping_pong_render_target->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int input_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_SRV); auto cpu_handle = data.out_allocation.GetDescriptorHandle(input_srv); d3d12::CreateSRVFromSpecificRTV(data.m_ping_pong_render_target, cpu_handle, 0, data.m_ping_pong_render_target->m_create_info.m_rtv_formats[0]); } { constexpr unsigned int output_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUTPUT_SRV); auto cpu_handle = data.out_allocation.GetDescriptorHandle(output_srv); d3d12::CreateSRVFromSpecificRTV(data.m_out_color_render_target, cpu_handle, 0, data.m_out_color_render_target->m_create_info.m_rtv_formats[0]); } } inline void BindResources(D3D12RenderSystem& n_render_system, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback) { d3d12::BindDescriptorHeaps(cmd_list, is_fallback); { constexpr unsigned int input = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT); auto cpu_handle = data.out_allocation.GetDescriptorHandle(input); d3d12::SetShaderSRV(cmd_list, 0, input, cpu_handle); } { constexpr unsigned int motion = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::MOTION); auto cpu_handle = data.out_allocation.GetDescriptorHandle(motion); d3d12::SetShaderSRV(cmd_list, 0, motion, cpu_handle); } { constexpr unsigned int normals = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::NORMAL); auto cpu_handle = data.out_allocation.GetDescriptorHandle(normals); d3d12::SetShaderSRV(cmd_list, 0, normals, cpu_handle); } { constexpr unsigned int depth = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::DEPTH); auto cpu_handle = data.out_allocation.GetDescriptorHandle(depth); d3d12::SetShaderSRV(cmd_list, 0, depth, cpu_handle); } { constexpr unsigned int hist_length = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::IN_HIST_LENGTH); auto cpu_handle = data.out_allocation.GetDescriptorHandle(hist_length); d3d12::SetShaderSRV(cmd_list, 0, hist_length, cpu_handle); } { constexpr unsigned int prev_color = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_INPUT); auto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_color); d3d12::SetShaderSRV(cmd_list, 0, prev_color, cpu_handle); } { constexpr unsigned int prev_moments = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_MOMENTS); auto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_moments); d3d12::SetShaderSRV(cmd_list, 0, prev_moments, cpu_handle); } { constexpr unsigned int prev_normals = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_NORMAL); auto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_normals); d3d12::SetShaderSRV(cmd_list, 0, prev_normals, cpu_handle); } { constexpr unsigned int prev_depth = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PREV_DEPTH); auto cpu_handle = data.out_allocation.GetDescriptorHandle(prev_depth); d3d12::SetShaderSRV(cmd_list, 0, prev_depth, cpu_handle); } { constexpr unsigned int out_color = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR); auto cpu_handle = data.out_allocation.GetDescriptorHandle(out_color); d3d12::SetShaderUAV(cmd_list, 0, out_color, cpu_handle); } { constexpr unsigned int out_moments = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_MOMENTS); auto cpu_handle = data.out_allocation.GetDescriptorHandle(out_moments); d3d12::SetShaderUAV(cmd_list, 0, out_moments, cpu_handle); } { constexpr unsigned int out_hist = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_HIST_LENGTH); auto cpu_handle = data.out_allocation.GetDescriptorHandle(out_hist); d3d12::SetShaderUAV(cmd_list, 0, out_hist, cpu_handle); } } inline void Reproject(D3D12RenderSystem& n_render_system, SceneGraph& sg, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback) { unsigned int frame_idx = n_render_system.GetFrameIdx(); const auto camera_cb = static_cast(data.m_denoiser_camera); d3d12::BindComputePipeline(cmd_list, data.m_reprojection_pipeline); BindResources(n_render_system, cmd_list, data, is_fallback); d3d12::BindComputeConstantBuffer(cmd_list, static_cast(data.m_denoiser_settings_buffer[0])->m_native, 1, frame_idx); d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void StoreBuffers(D3D12RenderSystem& n_render_system, SceneGraph& sg, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback) { d3d12::Transition(cmd_list, data.m_gbuffer_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_out_moments_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_out_hist_length_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_in_prev_moments, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); d3d12::Transition(cmd_list, data.m_in_prev_normals, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); d3d12::Transition(cmd_list, data.m_in_prev_depth, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); d3d12::Transition(cmd_list, data.m_in_hist_length, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); cmd_list->m_native->CopyResource(data.m_in_prev_moments->m_render_targets[0], data.m_out_moments_render_target->m_render_targets[0]); cmd_list->m_native->CopyResource(data.m_in_prev_normals->m_render_targets[0], data.m_gbuffer_render_target->m_render_targets[1]); cmd_list->m_native->CopyResource(data.m_in_prev_depth->m_render_targets[0], data.m_gbuffer_render_target->m_render_targets[4]); cmd_list->m_native->CopyResource(data.m_in_hist_length->m_render_targets[0], data.m_out_hist_length_render_target->m_render_targets[0]); d3d12::Transition(cmd_list, data.m_gbuffer_render_target, ResourceState::COPY_SOURCE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_out_moments_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_out_hist_length_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_in_prev_moments, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_in_prev_normals, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_in_prev_depth, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_in_hist_length, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); } inline void FilterMoments(D3D12RenderSystem& n_render_system, SceneGraph& sg, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback) { unsigned int frame_idx = n_render_system.GetFrameIdx(); const auto camera_cb = static_cast(data.m_denoiser_camera); d3d12::BindComputePipeline(cmd_list, data.m_filter_moments_pipeline); BindResources(n_render_system, cmd_list, data, is_fallback); d3d12::BindComputeConstantBuffer(cmd_list, static_cast(data.m_denoiser_settings_buffer[0])->m_native, 1, frame_idx); d3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE); { constexpr unsigned int input_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT); constexpr unsigned int output_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUTPUT_SRV); auto cpu_handle = data.out_allocation.GetDescriptorHandle(output_srv); d3d12::SetShaderSRV(cmd_list, 0, input_id, cpu_handle); } { constexpr unsigned int output_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR); constexpr unsigned int ping_ping_uav = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_UAV); auto cpu_handle = data.out_allocation.GetDescriptorHandle(ping_ping_uav); d3d12::SetShaderUAV(cmd_list, 0, output_id, cpu_handle); } d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); } inline void WaveletFilter(D3D12RenderSystem& n_render_system, SceneGraph& sg, d3d12::CommandList* cmd_list, ShadowDenoiserData& data, bool is_fallback) { unsigned int frame_idx = n_render_system.GetFrameIdx(); const auto camera_cb = static_cast(data.m_denoiser_camera); d3d12::BindComputePipeline(cmd_list, data.m_wavelet_filter_pipeline); BindResources(n_render_system, cmd_list, data, is_fallback); for (int i = 0; i < data.m_denoiser_settings_buffer.size(); ++i) { d3d12::BindComputeConstantBuffer(cmd_list, static_cast(data.m_denoiser_settings_buffer[i])->m_native, 1, frame_idx); if (i % 2 == 0) { d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS); { constexpr unsigned int input_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT); constexpr unsigned int ping_pong_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_SRV); auto cpu_handle = data.out_allocation.GetDescriptorHandle(ping_pong_srv); d3d12::SetShaderSRV(cmd_list, 0, input_id, cpu_handle); } { constexpr unsigned int output_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR); auto cpu_handle = data.out_allocation.GetDescriptorHandle(output_id); d3d12::SetShaderUAV(cmd_list, 0, output_id, cpu_handle); } } else { d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::NON_PIXEL_SHADER_RESOURCE); { constexpr unsigned int input_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::INPUT); constexpr unsigned int output_srv = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUTPUT_SRV); auto cpu_handle = data.out_allocation.GetDescriptorHandle(output_srv); d3d12::SetShaderSRV(cmd_list, 0, input_id, cpu_handle); } { constexpr unsigned int output_id = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::OUT_COLOR); constexpr unsigned int ping_ping_uav = rs_layout::GetHeapLoc(params::svgf_denoiser, params::SVGFDenoiserE::PING_PONG_UAV); auto cpu_handle = data.out_allocation.GetDescriptorHandle(ping_ping_uav); d3d12::SetShaderUAV(cmd_list, 0, output_id, cpu_handle); } } d3d12::Dispatch(cmd_list, static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Width / 16.f)), static_cast(std::ceil(n_render_system.m_viewport.m_viewport.Height / 16.f)), 1); if (i == d3d12::settings::shadow_denoiser_feedback_tap) { if (i % 2 == 0) { d3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_in_prev_color, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); cmd_list->m_native->CopyResource(data.m_in_prev_color->m_render_targets[0], data.m_out_color_render_target->m_render_targets[0]); d3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_in_prev_color, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); } else { d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_in_prev_color, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); cmd_list->m_native->CopyResource(data.m_in_prev_color->m_render_targets[0], data.m_ping_pong_render_target->m_render_targets[0]); d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_in_prev_color, ResourceState::COPY_DEST, ResourceState::NON_PIXEL_SHADER_RESOURCE); } } } if (d3d12::settings::shadow_denoiser_wavelet_iterations % 2 == 0) { d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); d3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::COPY_DEST); cmd_list->m_native->CopyResource(data.m_out_color_render_target->m_render_targets[0], data.m_ping_pong_render_target->m_render_targets[0]); d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); d3d12::Transition(cmd_list, data.m_out_color_render_target, ResourceState::COPY_DEST, ResourceState::UNORDERED_ACCESS); } else { d3d12::Transition(cmd_list, data.m_ping_pong_render_target, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::UNORDERED_ACCESS); } } inline void ExecuteShadowDenoiserTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto cmd_list = fg.GetCommandList(handle); auto render_target = fg.GetRenderTarget(handle); bool is_fallback = d3d12::GetRaytracingType(n_render_system.m_device) == RaytracingType::FALLBACK; auto settings = fg.GetSettings(); //Populating setting cb data.m_denoiser_settings.m_alpha = settings.m_runtime.m_alpha; data.m_denoiser_settings.m_moments_alpha = settings.m_runtime.m_moments_alpha; data.m_denoiser_settings.m_l_phi = settings.m_runtime.m_l_phi; data.m_denoiser_settings.m_z_phi = settings.m_runtime.m_z_phi; data.m_denoiser_settings.m_n_phi = settings.m_runtime.m_n_phi; for (int i = 0; i < data.m_denoiser_settings_buffer.size(); ++i) { data.m_denoiser_settings.m_step_distance = (float)(1 << i); data.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); } if (n_render_system.m_render_window.has_value()) { const auto viewport = n_render_system.m_viewport; const auto frame_idx = n_render_system.GetFrameIdx(); d3d12::Transition(cmd_list, render_target, ResourceState::COPY_SOURCE, ResourceState::UNORDERED_ACCESS); Reproject(n_render_system, sg, cmd_list, data, is_fallback); StoreBuffers(n_render_system, sg, cmd_list, data, is_fallback); FilterMoments(n_render_system, sg, cmd_list, data, is_fallback); WaveletFilter(n_render_system, sg, cmd_list, data, is_fallback); d3d12::Transition(cmd_list, render_target, ResourceState::UNORDERED_ACCESS, ResourceState::COPY_SOURCE); } } inline void DestroyShadowDenoiserTask(FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& data = fg.GetData(handle); d3d12::Destroy(data.m_in_hist_length); d3d12::Destroy(data.m_in_prev_color); d3d12::Destroy(data.m_in_prev_moments); d3d12::Destroy(data.m_in_prev_normals); d3d12::Destroy(data.m_in_prev_depth); d3d12::Destroy(data.m_out_hist_length_render_target); d3d12::Destroy(data.m_out_moments_render_target); d3d12::Destroy(data.m_ping_pong_render_target); if (!resize) { for (auto buffer : data.m_denoiser_settings_buffer) { data.m_constant_buffer_pool->Destroy(buffer); } } } } /* internal */ // pass a path to a texture location to load a custom denoiser kernel inline void AddShadowDenoiserTask(FrameGraph& fg) { std::wstring name(L"Shadow Denoiser"); RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(true), RenderTargetProperties::ClearDepth(true), RenderTargetProperties::ResolutionScalar(1.0f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::SetupShadowDenoiserTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { internal::ExecuteShadowDenoiserTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph& fg, RenderTaskHandle handle, bool resize) { internal::DestroyShadowDenoiserTask(fg, handle, resize); }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; fg.AddTask(desc, name, FG_DEPS()); fg.UpdateSettings(ShadowDenoiserSettings()); } }/* wr */ ================================================ FILE: src/render_tasks/d3d12_spatial_reconstruction.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../frame_graph/frame_graph.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" #include "../render_tasks/d3d12_rt_reflection_task.hpp" #include "../render_tasks/d3d12_deferred_main.hpp" namespace wr { struct SpatialReconstructionData { d3d12::PipelineState* pipeline; std::shared_ptr m_cb_pool; D3D12ConstantBufferHandle* camera_cb; DescriptorAllocator* allocator; DescriptorAllocation output; DescriptorAllocation reflection_buffer; DescriptorAllocation gbuffer_normal; DescriptorAllocation gbuffer_roughness; DescriptorAllocation gbuffer_depth; std::uint32_t frame_idx; }; namespace temp { struct SpatialReconstructionCameraData { DirectX::XMMATRIX inv_vp; DirectX::XMMATRIX inv_view; float padding; std::uint32_t frame_idx; float near_plane, far_plane; }; } namespace internal { inline void SetupSpatialReconstructionTask(RenderSystem& rs, FrameGraph& fg, RenderTaskHandle handle, bool resize) { auto& n_render_system = static_cast(rs); auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto hybrid_rt = static_cast(fg.GetPredecessorRenderTarget()); auto gbuffer_rt = static_cast(fg.GetPredecessorRenderTarget()); if (!resize) { data.frame_idx = 0; data.allocator = new DescriptorAllocator(n_render_system, wr::DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV); data.output = data.allocator->Allocate(); data.reflection_buffer = data.allocator->Allocate(2); data.gbuffer_normal = data.allocator->Allocate(d3d12::settings::num_back_buffers); data.gbuffer_roughness = data.allocator->Allocate(d3d12::settings::num_back_buffers); data.gbuffer_depth = data.allocator->Allocate(); auto& ps_registry = PipelineRegistry::Get(); data.pipeline = (d3d12::PipelineState*)ps_registry.Find(pipelines::spatial_reconstruction); data.m_cb_pool = n_render_system.CreateConstantBufferPool(sizeof(temp::SpatialReconstructionCameraData) * d3d12::settings::num_back_buffers); data.camera_cb = static_cast(data.m_cb_pool->Create(sizeof(temp::SpatialReconstructionCameraData))); } // Deferred data for (uint32_t i = 0; i < d3d12::settings::num_back_buffers; ++i) { auto albedo_handle = data.gbuffer_roughness.GetDescriptorHandle(i); auto normal_handle = data.gbuffer_normal.GetDescriptorHandle(i); d3d12::CreateSRVFromSpecificRTV(gbuffer_rt, albedo_handle, 0, gbuffer_rt->m_create_info.m_rtv_formats[0]); d3d12::CreateSRVFromSpecificRTV(gbuffer_rt, normal_handle, 1, gbuffer_rt->m_create_info.m_rtv_formats[1]); } auto depth_handle = data.gbuffer_depth.GetDescriptorHandle(); d3d12::CreateSRVFromDSV(gbuffer_rt, depth_handle); // Filtered output { auto cpu_handle = data.output.GetDescriptorHandle(); d3d12::CreateUAVFromSpecificRTV(n_render_target, cpu_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); } // Reflection buffer input { auto cpu_handle = data.reflection_buffer.GetDescriptorHandle(); d3d12::CreateSRVFromSpecificRTV(hybrid_rt, cpu_handle, 0, hybrid_rt->m_create_info.m_rtv_formats[0]); cpu_handle = data.reflection_buffer.GetDescriptorHandle(1); d3d12::CreateSRVFromSpecificRTV(hybrid_rt, cpu_handle, 2, hybrid_rt->m_create_info.m_rtv_formats[2]); } } inline void ExecuteSpatialReconstructionTask(RenderSystem& rs, FrameGraph& fg, SceneGraph& sg, RenderTaskHandle handle) { auto& n_render_system = static_cast(rs); auto& n_device = n_render_system.m_device->m_native; auto& data = fg.GetData(handle); auto n_render_target = fg.GetRenderTarget(handle); auto frame_idx = n_render_system.GetFrameIdx(); auto cmd_list = fg.GetCommandList(handle); const auto viewport = n_render_system.m_viewport; fg.WaitForPredecessorTask(); fg.WaitForPredecessorTask(); d3d12::BindComputePipeline(cmd_list, data.pipeline); // Update camera constant buffer auto camera = sg.GetActiveCamera(); temp::SpatialReconstructionCameraData cam_data; cam_data.inv_view = DirectX::XMMatrixInverse(nullptr, camera->m_view); cam_data.inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection); cam_data.near_plane = camera->m_frustum_near; cam_data.far_plane = camera->m_frustum_far; cam_data.frame_idx = ++data.frame_idx; data.m_cb_pool->Update(data.camera_cb, sizeof(temp::SpatialReconstructionCameraData), 0, frame_idx, (std::uint8_t*)&cam_data); d3d12::BindComputeConstantBuffer(cmd_list, data.camera_cb->m_native, 1, frame_idx); { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::spatial_reconstruction, params::SpatialReconstructionE::OUTPUT); auto handle_uav = data.output.GetDescriptorHandle(); d3d12::SetShaderUAV(cmd_list, 0, dest_idx, handle_uav); } { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::spatial_reconstruction, params::SpatialReconstructionE::REFLECTION_BUFFER); auto handle_srv = data.reflection_buffer.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, dest_idx, handle_srv); handle_srv = data.reflection_buffer.GetDescriptorHandle(1); d3d12::SetShaderSRV(cmd_list, 0, dest_idx + 1, handle_srv); } { constexpr unsigned int dest_idx = rs_layout::GetHeapLoc(params::spatial_reconstruction, params::SpatialReconstructionE::GBUFFERS); d3d12::DescHeapCPUHandle albedo_handle = data.gbuffer_roughness.GetDescriptorHandle(frame_idx); d3d12::SetShaderSRV(cmd_list, 0, dest_idx, albedo_handle); d3d12::DescHeapCPUHandle normal_handle = data.gbuffer_normal.GetDescriptorHandle(frame_idx); d3d12::SetShaderSRV(cmd_list, 0, dest_idx + 1, normal_handle); d3d12::DescHeapCPUHandle depth_handle = data.gbuffer_depth.GetDescriptorHandle(); d3d12::SetShaderSRV(cmd_list, 0, dest_idx + 2, depth_handle); } d3d12::Dispatch(cmd_list, uint32_t(std::ceil(viewport.m_viewport.Width / 16.f)), uint32_t(std::ceil(viewport.m_viewport.Height / 16.f)), 1); } } /* internal */ inline void AddSpatialReconstructionTask(FrameGraph& frame_graph) { RenderTargetProperties rt_properties { RenderTargetProperties::IsRenderWindow(false), RenderTargetProperties::Width(std::nullopt), RenderTargetProperties::Height(std::nullopt), RenderTargetProperties::ExecuteResourceState(ResourceState::UNORDERED_ACCESS), RenderTargetProperties::FinishedResourceState(ResourceState::COPY_SOURCE), RenderTargetProperties::CreateDSVBuffer(false), RenderTargetProperties::DSVFormat(Format::UNKNOWN), RenderTargetProperties::RTVFormats({ wr::Format::R16G16B16A16_FLOAT }), RenderTargetProperties::NumRTVFormats(1), RenderTargetProperties::Clear(false), RenderTargetProperties::ClearDepth(false), RenderTargetProperties::ResolutionScalar(1.f) }; RenderTaskDesc desc; desc.m_setup_func = [](RenderSystem & rs, FrameGraph & fg, RenderTaskHandle handle, bool resize) { internal::SetupSpatialReconstructionTask(rs, fg, handle, resize); }; desc.m_execute_func = [](RenderSystem & rs, FrameGraph & fg, SceneGraph & sg, RenderTaskHandle handle) { internal::ExecuteSpatialReconstructionTask(rs, fg, sg, handle); }; desc.m_destroy_func = [](FrameGraph & fg, RenderTaskHandle handle, bool resize) { if (!resize) { auto& data = fg.GetData(handle); delete data.allocator; data.m_cb_pool.reset(); } }; desc.m_properties = rt_properties; desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; frame_graph.AddTask(desc, L"Spatial reconstruction", FG_DEPS()); } } /* wr */ ================================================ FILE: src/renderer.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "renderer.hpp" void wr::RenderSystem::RequestRenderTargetSaveToDisc(std::string const & path, RenderTarget* render_target, unsigned int index) { m_requested_rt_saves.emplace(SaveRenderTargetRequest{ path, render_target, index }); } ================================================ FILE: src/renderer.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include "engine_registry.hpp" #include "platform_independend_structs.hpp" #include "structs.hpp" namespace wr { struct Model; struct CPUTextures; class Window; class SceneGraph; class TexturePool; class MaterialPool; class ModelPool; class ConstantBufferPool; class StructuredBufferPool; class FrameGraph; class RenderSystem { public: RenderSystem() = default; virtual ~RenderSystem() = default; RenderSystem(RenderSystem const &) = delete; RenderSystem& operator=(RenderSystem const &) = delete; RenderSystem(RenderSystem&&) = delete; RenderSystem& operator=(RenderSystem&&) = delete; virtual std::shared_ptr CreateTexturePool() = 0; virtual std::shared_ptr CreateMaterialPool(std::size_t size_in_mb) = 0; virtual std::shared_ptr CreateModelPool(std::size_t vertex_buffer_pool_size_in_mb, std::size_t index_buffer_pool_size_in_mb) = 0; virtual std::shared_ptr CreateConstantBufferPool(std::size_t size_in_mb) = 0; virtual std::shared_ptr CreateStructuredBufferPool(std::size_t size_in_mb) = 0; virtual std::shared_ptr GetDefaultTexturePool() = 0; virtual void PrepareRootSignatureRegistry() = 0; virtual void PrepareShaderRegistry() = 0; virtual void PreparePipelineRegistry() = 0; virtual void PrepareRTPipelineRegistry() = 0; virtual void DestroyRootSignatureRegistry() = 0; virtual void DestroyShaderRegistry() = 0; virtual void DestroyPipelineRegistry() = 0; virtual void DestroyRTPipelineRegistry() = 0; virtual void WaitForAllPreviousWork() = 0; virtual CommandList* GetDirectCommandList(unsigned int num_allocators) = 0; virtual CommandList* GetBundleCommandList(unsigned int num_allocators) = 0; virtual CommandList* GetComputeCommandList(unsigned int num_allocators) = 0; virtual CommandList* GetCopyCommandList(unsigned int num_allocators) = 0; virtual void SetCommandListName(CommandList* cmd_list, std::wstring const & name) = 0; virtual void DestroyCommandList(CommandList* cmd_list) = 0; virtual RenderTarget* GetRenderTarget(RenderTargetProperties properties) = 0; virtual void SetRenderTargetName(RenderTarget* cmd_list, std::wstring const & name) = 0; virtual void ResizeRenderTarget(RenderTarget** render_target, std::uint32_t width, std::uint32_t height) = 0; virtual void DestroyRenderTarget(RenderTarget** render_target) = 0; virtual void ResetCommandList(CommandList* cmd_list) = 0; virtual void CloseCommandList(CommandList* cmd_list) = 0; virtual void StartRenderTask(CommandList* cmd_list, std::pair render_target) = 0; virtual void StopRenderTask(CommandList* cmd_list, std::pair render_target) = 0; virtual void StartComputeTask(CommandList* cmd_list, std::pair render_target) = 0; virtual void StopComputeTask(CommandList* cmd_list, std::pair render_target) = 0; virtual void StartCopyTask(CommandList* cmd_list, std::pair render_target) = 0; virtual void StopCopyTask(CommandList* cmd_list, std::pair render_target) = 0; virtual void Init(std::optional window) = 0; virtual CPUTextures Render(SceneGraph & scene_graph, FrameGraph & frame_graph) = 0; virtual void Resize(std::uint32_t width, std::uint32_t height) = 0; void RequestRenderTargetSaveToDisc(std::string const& path, RenderTarget* render_target, unsigned int index); virtual unsigned int GetFrameIdx() = 0; virtual void RequestSkyboxReload() = 0; std::optional m_window; enum class SimpleShapes : std::size_t { CUBE, PLANE, COUNT }; //SimpleShapes don't have a material attached to them. The user is expected to provide one. virtual wr::Model* GetSimpleShape(SimpleShapes type) = 0; TextureHandle GetDefaultAlbedo() const { return m_default_albedo; } TextureHandle GetDefaultNormal() const { return m_default_normal; } TextureHandle GetDefaultRoughness() const { return m_default_white; } TextureHandle GetDefaultMetalic() const { return m_default_black; } TextureHandle GetDefaultAO() const { return m_default_white; } TextureHandle GetDefaultEmissive() const { return m_default_black; } std::shared_ptr m_shapes_pool; std::array(SimpleShapes::COUNT)> m_simple_shapes; wr::TextureHandle m_default_cubemap; wr::TextureHandle m_default_albedo; wr::TextureHandle m_default_normal; wr::TextureHandle m_default_white; wr::TextureHandle m_default_black; protected: struct SaveRenderTargetRequest { std::string m_path; RenderTarget* m_render_target; unsigned int m_index; }; virtual void SaveRenderTargetToDisc(std::string const & path, RenderTarget* render_target, unsigned int index) = 0; std::queue m_requested_rt_saves; }; } /* wr */ ================================================ FILE: src/resource_pool_texture.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "resource_pool_texture.hpp" #include "util/log.hpp" #include "util/strings.hpp" namespace wr { TexturePool::TexturePool() { #ifdef _DEBUG std::lock_guard lock(m_mutex); static uint16_t pool_count = 0u; m_name = "TexturePool_" + std::to_string(pool_count); pool_count++; #endif } TextureHandle TexturePool::LoadFromMemory(unsigned char* data, size_t width, size_t height, const std::string& texture_extension, bool srgb, bool generate_mips) { std::string new_str = texture_extension; std::transform(texture_extension.begin(), texture_extension.end(), new_str.begin(), ::tolower); TextureFormat type; if (new_str == "png"|| new_str == "jpg" || new_str == "jpeg" || new_str == "bmp") { type = TextureFormat::WIC; } else if (new_str.compare("dds") == 0) { type = TextureFormat::DDS; } else if (new_str.compare("hdr") == 0) { type = TextureFormat::HDR; } else { LOGC("[ERROR]: Texture format not supported."); return {}; } std::lock_guard lock(m_mutex); return LoadFromMemory(data, width, height, type, srgb, generate_mips); } } ================================================ FILE: src/resource_pool_texture.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include "structs.hpp" #include "util/defines.hpp" #include "util/strings.hpp" #include "platform_independend_structs.hpp" #include "id_factory.hpp" namespace wr { enum class TextureFormat { WIC, DDS, HDR, RAW }; class TexturePool { public: explicit TexturePool(); virtual ~TexturePool() = default; TexturePool(TexturePool const &) = delete; TexturePool& operator=(TexturePool const &) = delete; TexturePool(TexturePool&&) = delete; TexturePool& operator=(TexturePool&&) = delete; [[nodiscard]] virtual TextureHandle LoadFromFile(std::string_view path, bool srgb, bool generate_mips) = 0; [[nodiscard]] virtual TextureHandle LoadFromMemory(unsigned char* data, size_t width, size_t height, const std::string& texture_extension, bool srgb, bool generate_mips); [[nodiscard]] virtual TextureHandle LoadFromMemory(unsigned char* data, size_t width, size_t height, TextureFormat type, bool srgb, bool generate_mips) = 0; [[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; [[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; virtual void MarkForUnload(TextureHandle& handle, unsigned int frame_idx) = 0; virtual void UnloadTextures(unsigned int frame_idx) = 0; virtual void Evict() = 0; virtual void MakeResident() = 0; virtual void Stage(CommandList* cmd_list) = 0; virtual void PostStageClear() = 0; virtual void ReleaseTemporaryResources() = 0; virtual Texture* GetTextureResource(TextureHandle handle) = 0; protected: std::size_t m_loaded_textures = 0; std::mutex m_mutex; IDFactory m_id_factory; #ifdef _DEBUG std::string m_name; #endif }; } ================================================ FILE: src/root_signature_registry.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "root_signature_registry.hpp" namespace wr { RootSignatureRegistry::RootSignatureRegistry() : Registry() { } RegistryHandle RootSignatureRegistry::Register(RootSignatureDescription description) { auto handle = m_next_handle; m_descriptions.insert({ handle, description }); m_next_handle++; return handle; } } /* wr */ ================================================ FILE: src/root_signature_registry.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "registry.hpp" #include #include "d3d12/d3d12_enums.hpp" #include "d3d12/d3d12_structs.hpp" #include "d3d12/d3dx12.hpp" #include "util/named_type.hpp" namespace wr { using RootSignature = void; struct RootSignatureDescription { std::vector m_parameters; // TODO: Write platform independend version. std::vector m_samplers; // TODO: Move to platform independed location bool m_rtx = false; bool m_rtx_local = false; std::wstring name = L"Unknown root signature"; }; class RootSignatureRegistry : public internal::Registry { public: RootSignatureRegistry(); virtual ~RootSignatureRegistry() = default; RegistryHandle Register(RootSignatureDescription description); }; } /* wr */ ================================================ FILE: src/rt_pipeline_registry.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "rt_pipeline_registry.hpp" namespace wr { RTPipelineRegistry::RTPipelineRegistry() : Registry() { } } /* wr */ ================================================ FILE: src/rt_pipeline_registry.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "registry.hpp" #include #include #include "vertex.hpp" #include "d3d12/d3dx12.hpp" #include "d3d12/d3d12_enums.hpp" #include "util/named_type.hpp" namespace wr { using StateObject = void; struct StateObjectDescription { struct LibraryDesc { RegistryHandle shader_handle; std::vector exports; std::vector> m_hit_groups; // first = hit group | second = entry }; CD3DX12_STATE_OBJECT_DESC desc; LibraryDesc library_desc; std::uint64_t max_payload_size; std::uint64_t max_attributes_size; std::uint64_t max_recursion_depth; std::optional global_root_signature; std::vector local_root_signatures; }; class RTPipelineRegistry : public internal::Registry { public: RTPipelineRegistry(); virtual ~RTPipelineRegistry() = default; RegistryHandle Register(StateObjectDescription description) { auto handle = m_next_handle; m_descriptions.insert({ handle, description }); m_next_handle++; return handle; } }; } /* wr */ ================================================ FILE: src/scene_graph/camera_node.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "camera_node.hpp" #include "../util/aabb.hpp" #include "mesh_node.hpp" namespace wr { void CameraNode::SetFov(float deg) { m_fov.m_fov = deg / 180.0f * 3.1415926535f; SignalChange(); } void CameraNode::SetFovFromFocalLength(float aspect_ratio, float filmSize) { float verticalSize = filmSize / aspect_ratio; m_fov.m_fov = 2.0f * std::atan2(verticalSize, 2.0f * m_focal_length); SignalChange(); } void CameraNode::SetAspectRatio(float ratio) { m_aspect_ratio = ratio; SignalChange(); } void CameraNode::SetFrustumNear(float value) noexcept { m_frustum_near = value; SignalChange(); } void CameraNode::SetFrustumFar(float value) noexcept { m_frustum_far = value; SignalChange(); } void CameraNode::SetFocalLength(float length) { m_focal_length = length; SetFovFromFocalLength(m_aspect_ratio, m_film_size); SignalChange(); } void CameraNode::SetProjectionOffset(float x, float y) { m_projection_offset_x = x; m_projection_offset_y = y; } std::pair CameraNode::GetProjectionOffset() { return std::pair(m_projection_offset_x, m_projection_offset_y); } void CameraNode::SetOrthographicResolution(std::uint32_t width, std::uint32_t height) { m_ortho_res.m_width = static_cast(width); //Window resolution counts in negative on Y axis, therefore conversion to signed is needed. m_ortho_res.m_height = static_cast(height) * -1; SignalChange(); } void CameraNode::UpdateTemp(unsigned int frame_idx) { DirectX::XMVECTOR pos = { m_transform.r[3].m128_f32[0], m_transform.r[3].m128_f32[1], m_transform.r[3].m128_f32[2] }; DirectX::XMVECTOR up = DirectX::XMVector3Normalize(m_transform.r[1]); DirectX::XMVECTOR forward = DirectX::XMVectorNegate(DirectX::XMVector3Normalize(m_transform.r[2])); DirectX::XMVECTOR right = DirectX::XMVector3Normalize(m_transform.r[0]); m_prev_view = m_view; m_prev_projection = m_projection; m_view = DirectX::XMMatrixLookToRH(pos, forward, up); if (!m_override_projection) { m_projection = DirectX::XMMatrixPerspectiveFovRH(m_fov.m_fov, m_aspect_ratio, m_frustum_near, m_frustum_far); if (m_enable_orthographic) { m_projection = DirectX::XMMatrixOrthographicOffCenterRH( 0, static_cast(m_ortho_res.m_width), static_cast(m_ortho_res.m_height), 0, m_frustum_near, m_frustum_far); } m_projection.r[2].m128_f32[0] += m_projection_offset_x; m_projection.r[2].m128_f32[1] += m_projection_offset_y; } m_view_projection = m_view * m_projection; m_inverse_projection = DirectX::XMMatrixInverse(nullptr, m_projection); m_inverse_view = DirectX::XMMatrixInverse(nullptr, m_view); CalculatePlanes(); SignalUpdate(frame_idx); } //Frustum culling code; //optimized and refactored version of //https://www.braynzarsoft.net/viewtutorial/q16390-34-aabb-cpu-side-frustum-culling void CameraNode::CalculatePlanes() { //Left plane m_planes[0] = DirectX::XMPlaneNormalize({ m_view_projection.r[0].m128_f32[3] + *m_view_projection.r[0].m128_f32, m_view_projection.r[1].m128_f32[3] + *m_view_projection.r[1].m128_f32, m_view_projection.r[2].m128_f32[3] + *m_view_projection.r[2].m128_f32, m_view_projection.r[3].m128_f32[3] + *m_view_projection.r[3].m128_f32 }); //Right plane m_planes[1] = DirectX::XMPlaneNormalize({ m_view_projection.r[0].m128_f32[3] - *m_view_projection.r[0].m128_f32, m_view_projection.r[1].m128_f32[3] - *m_view_projection.r[1].m128_f32, m_view_projection.r[2].m128_f32[3] - *m_view_projection.r[2].m128_f32, m_view_projection.r[3].m128_f32[3] - *m_view_projection.r[3].m128_f32 }); //Top plane m_planes[2] = DirectX::XMPlaneNormalize({ m_view_projection.r[0].m128_f32[3] - m_view_projection.r[0].m128_f32[1], m_view_projection.r[1].m128_f32[3] - m_view_projection.r[1].m128_f32[1], m_view_projection.r[2].m128_f32[3] - m_view_projection.r[2].m128_f32[1], m_view_projection.r[3].m128_f32[3] - m_view_projection.r[3].m128_f32[1] }); //Bottom plane m_planes[3] = DirectX::XMPlaneNormalize({ m_view_projection.r[0].m128_f32[3] + m_view_projection.r[0].m128_f32[1], m_view_projection.r[1].m128_f32[3] + m_view_projection.r[1].m128_f32[1], m_view_projection.r[2].m128_f32[3] + m_view_projection.r[2].m128_f32[1], m_view_projection.r[3].m128_f32[3] + m_view_projection.r[3].m128_f32[1] }); //Near plane m_planes[4] = DirectX::XMPlaneNormalize({ m_view_projection.r[0].m128_f32[2], m_view_projection.r[1].m128_f32[2], m_view_projection.r[2].m128_f32[2], m_view_projection.r[3].m128_f32[2] }); //Far plane m_planes[5] = DirectX::XMPlaneNormalize({ m_view_projection.r[0].m128_f32[3] - m_view_projection.r[0].m128_f32[2], m_view_projection.r[1].m128_f32[3] - m_view_projection.r[1].m128_f32[2], m_view_projection.r[2].m128_f32[3] - m_view_projection.r[2].m128_f32[2], m_view_projection.r[3].m128_f32[3] - m_view_projection.r[3].m128_f32[2] }); } bool CameraNode::InView(const std::shared_ptr& node) const { const AABB &aabb = node->m_aabb; return aabb.InFrustum(m_planes); } bool CameraNode::InRange(const std::shared_ptr &node, const float dist) const { const AABB &aabb = node->m_aabb; const Sphere sphere{ m_position, dist }; return aabb.Contains(sphere); } } /* wr */ ================================================ FILE: src/scene_graph/camera_node.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "node.hpp" #include #include "../util/named_type.hpp" #include "../constant_buffer_pool.hpp" namespace wr { struct MeshNode; struct CameraNode : Node { using FovDefault = util::NamedType; using FovFocalLength = util::NamedType; using FovAspectRatio = util::NamedType; using FovFilmSize = util::NamedType; using OrthographicWidth = util::NamedType; using OrthographicHeight = util::NamedType; struct FoV { explicit FoV(FovDefault deg) : m_fov(deg.Get() / 180.0f * 3.1415926535f) { } FoV(FovFocalLength focal_length, FovAspectRatio aspect_ratio, FovFilmSize film_size) : m_fov(2.0f * std::atan2(film_size.Get() / aspect_ratio.Get(), 2.0f * focal_length.Get())) { } float m_fov; }; struct OrthographicResolution { explicit OrthographicResolution(OrthographicWidth width, OrthographicHeight height) : m_width(width), m_height(height * -1) { } int m_width; int m_height; }; CameraNode(float aspect_ratio) : Node(typeid(CameraNode)), m_active(true), m_frustum_near(0.1f), m_frustum_far(10000.0f), m_aspect_ratio(aspect_ratio), m_focal_length(35.0f), m_film_size(45.0f), m_fov(FovFocalLength(m_focal_length), FovAspectRatio(aspect_ratio), FovFilmSize(m_film_size)), m_f_number(32.0f), m_shape_amt(0.0f), m_aperture_blades(5), m_focus_dist(0), m_dof_range(1.0f), m_override_projection(false), m_projection_offset_x(0), m_projection_offset_y(0), m_view(), m_inverse_view(), m_projection(), m_inverse_projection(), m_view_projection(), m_camera_cb(), m_planes(), m_ortho_res(OrthographicWidth(1280), OrthographicHeight(720)) { } void SetFov(float deg); void SetFovFromFocalLength(float aspect_ratio, float filmSize); void SetAspectRatio(float ratio); void SetFocalLength(float length); void SetFrustumNear(float value) noexcept; void SetFrustumFar(float value) noexcept; void SetProjectionOffset(float x, float y); void SetOrthographicResolution(std::uint32_t width, std::uint32_t height); std::pair GetProjectionOffset(); void UpdateTemp(unsigned int frame_idx); bool InView(const std::shared_ptr& node) const; bool InRange(const std::shared_ptr& node, const float dist) const; void CalculatePlanes(); bool m_active; float m_frustum_near; float m_frustum_far; float m_aspect_ratio; float m_focal_length; float m_film_size; float m_f_number; float m_focus_dist; float m_shape_amt; float m_dof_range; int m_aperture_blades; bool m_enable_dof = false; bool m_override_projection = false; bool m_enable_orthographic = false; FoV m_fov; OrthographicResolution m_ortho_res; DirectX::XMMATRIX m_view; DirectX::XMMATRIX m_projection; DirectX::XMMATRIX m_prev_projection; DirectX::XMMATRIX m_prev_view; DirectX::XMMATRIX m_view_projection; DirectX::XMMATRIX m_inverse_projection; DirectX::XMMATRIX m_inverse_view; float m_projection_offset_x; // Used By Ansel For Super Resolution float m_projection_offset_y; // Used By Ansel For Super Resolution std::array m_planes; ConstantBufferHandle* m_camera_cb; int m_window_resolution[2] = { 0,0 }; }; } /* wr */ ================================================ FILE: src/scene_graph/light_node.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "light_node.hpp" namespace wr { LightNode::LightNode(LightType tid, DirectX::XMVECTOR col) : Node(typeid(LightNode)), m_light(&m_temp) { SetType(tid); SetColor(col); } LightNode::LightNode(DirectX::XMVECTOR dir, DirectX::XMVECTOR col) : Node(typeid(LightNode)), m_light(&m_temp) { SetDirectional(dir, col); } LightNode::LightNode(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR col) : Node(typeid(LightNode)), m_light(&m_temp) { SetPoint(pos, rad, col); } LightNode::LightNode(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR dir, float ang, DirectX::XMVECTOR col) : Node(typeid(LightNode)), m_light(&m_temp) { SetSpot(pos, rad, dir, ang, col); } LightNode::LightNode(const LightNode& old) : Node(typeid(LightNode)) { m_temp = old.m_temp; m_temp.tid &= 0x3; m_light = &m_temp; } LightNode& LightNode::operator=(const LightNode& old) { m_temp = old.m_temp; m_temp.tid &= 0x3; m_light = &m_temp; return *this; } void LightNode::SetAngle(float ang) { m_light->ang = ang; SignalChange(); } void LightNode::SetRadius(float rad) { m_light->rad = rad; SignalChange(); } void LightNode::SetType(LightType tid) { m_light->tid = (uint32_t) tid; SignalChange(); } void LightNode::SetColor(DirectX::XMVECTOR col) { memcpy(&m_light->col, &col, 12); SignalChange(); } void LightNode::SetDirectional(DirectX::XMVECTOR rot, DirectX::XMVECTOR col) { SetType(LightType::DIRECTIONAL); SetRotation(rot); SetColor(col); } void LightNode::SetPoint(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR col) { SetType(LightType::POINT); SetPosition(pos); SetRadius(rad); SetColor(col); } void LightNode::SetSpot(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR rot, float ang, DirectX::XMVECTOR col) { SetType(LightType::SPOT); SetPosition(pos); SetRadius(rad); SetRotation(rot); SetAngle(ang); SetColor(col); } void LightNode::SetLightSize(float size_in_deg) { m_light->light_size = DirectX::XMConvertToRadians(size_in_deg); SignalChange(); } void LightNode::Update(uint32_t frame_idx) { DirectX::XMVECTOR position = { m_transform.r[3].m128_f32[0], m_transform.r[3].m128_f32[1], m_transform.r[3].m128_f32[2] }; memcpy(&m_light->pos, &position, 12); DirectX::XMVECTOR forward = DirectX::XMVector3Normalize(m_transform.r[2]); memcpy(&m_light->dir, &forward, 12); SignalUpdate(frame_idx); } LightType LightNode::GetType() { return (LightType)(m_light->tid & 0x3); } } ================================================ FILE: src/scene_graph/light_node.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "node.hpp" #include "../platform_independend_structs.hpp" namespace wr { struct LightNode : Node { explicit LightNode(LightType tid, DirectX::XMVECTOR col = { 1, 1, 1 }); explicit LightNode(DirectX::XMVECTOR rot, DirectX::XMVECTOR col = { 1, 1, 1 }); LightNode(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR col = { 1, 1, 1 }); LightNode(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR rot, float ang, DirectX::XMVECTOR col = { 1, 1, 1 }); LightNode(const LightNode& old); LightNode& operator=(const LightNode& old); //! Set angle void SetAngle(float ang); //! Set radius void SetRadius(float rad); //! Set type void SetType(LightType tid); //! Sets color void SetColor(DirectX::XMVECTOR col); //! Set data for a directional light void SetDirectional(DirectX::XMVECTOR rot, DirectX::XMVECTOR col = { 1, 1, 1 }); //! Set data for a point light void SetPoint(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR col = { 1, 1, 1 }); //! Set data for a spot light void SetSpot(DirectX::XMVECTOR pos, float rad, DirectX::XMVECTOR rot, float ang, DirectX::XMVECTOR col = { 1, 1, 1 }); //! Set data for light physical size void SetLightSize(float size); //! Update void Update(uint32_t frame_idx); //! Helper for getting the LightType (doesn't include light count for the first light) LightType GetType(); //! Allocated data (either temp or array data) Light* m_light; //! Physical data Light m_temp; }; } /* wr */ ================================================ FILE: src/scene_graph/mesh_node.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mesh_node.hpp" namespace wr { MeshNode::MeshNode(Model* model) : Node(typeid(MeshNode)), m_model(model), m_materials(), m_visible(true) { } void MeshNode::Update(uint32_t frame_idx) { m_aabb = AABB::FromTransform(m_model->m_box, m_transform); SignalUpdate(frame_idx); } void MeshNode::AddMaterial(MaterialHandle handle) { m_materials.push_back(handle); CheckMaterialCount(); } std::vector& MeshNode::GetMaterials() { return m_materials; } void MeshNode::SetMaterials(std::vector const & materials) { m_materials = materials; CheckMaterialCount(); } void MeshNode::ClearMaterials() { m_materials.clear(); } void MeshNode::CheckMaterialCount() const { if (m_materials.size() > m_model->m_meshes.size()) { LOGW("A mesh node has more materials than meshes.") } } } ================================================ FILE: src/scene_graph/mesh_node.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "node.hpp" #include "../model_pool.hpp" #include "../util/aabb.hpp" namespace wr { struct MeshNode : Node { explicit MeshNode(Model* model); void Update(uint32_t frame_idx); /*! Add a material */ /*! You can add a material for every single sub-mesh. */ void AddMaterial(MaterialHandle handle); /*! Get all materials */ std::vector& GetMaterials(); /*! Set the materials */ void SetMaterials(std::vector const & materials); /*! Remove materials */ void ClearMaterials(); Model* m_model; AABB m_aabb; std::vector m_materials; bool m_visible; private: /*! Check whether their are more materials than meshes */ /*! If there are more materials than meshes. This function will throw a warning. */ void CheckMaterialCount() const; }; } /* wr */ ================================================ FILE: src/scene_graph/node.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "node.hpp" #include "../util/log.hpp" namespace wr { Node::Node() : m_type_info(typeid(Node)) { SignalTransformChange(); } Node::Node(std::type_info const & type_info) : m_type_info(type_info) { SignalTransformChange(); } void Node::SignalChange() { m_requires_update[0] = m_requires_update[1] = m_requires_update[2] = true; } void Node::SignalTransformChange() { m_requires_transform_update[0] = m_requires_transform_update[1] = m_requires_transform_update[2] = true; for (std::shared_ptr& child : m_children) { child->SignalTransformChange(); } } void Node::SignalUpdate(unsigned int frame_idx) { m_requires_update[frame_idx] = false; } bool Node::RequiresUpdate(unsigned int frame_idx) { return m_requires_update[frame_idx]; } void Node::SignalTransformUpdate(unsigned int frame_idx) { m_requires_transform_update[frame_idx] = false; } bool Node::RequiresTransformUpdate(unsigned int frame_idx) { return m_requires_transform_update[frame_idx]; } void Node::SetRotation(DirectX::XMVECTOR roll_pitch_yaw) { m_rotation_radians = roll_pitch_yaw; m_use_quaternion = false; SignalTransformChange(); } void Node::SetRotationQuaternion(DirectX::XMVECTOR rotation) { m_rotation = rotation; m_use_quaternion = true; SignalTransformChange(); } void Node::SetQuaternionRotation( float x, float y, float z, float w ) { m_rotation = { x,y,z,w }; m_use_quaternion = true; SignalTransformChange(); } void Node::SetPosition(DirectX::XMVECTOR position) { m_position = position; SignalTransformChange(); } void Node::SetScale(DirectX::XMVECTOR scale) { m_scale = scale; SignalTransformChange(); } void Node::SetTransform(DirectX::XMVECTOR position, DirectX::XMVECTOR rotation, DirectX::XMVECTOR scale) { SetPosition(position); SetRotation(rotation); SetScale(scale); } void Node::UpdateTransform() { if (!m_use_quaternion) { m_rotation = DirectX::XMQuaternionRotationRollPitchYawFromVector(m_rotation_radians); } m_prev_transform = m_transform; DirectX::XMMATRIX translation_mat = DirectX::XMMatrixTranslationFromVector(m_position); DirectX::XMMATRIX rotation_mat = DirectX::XMMatrixRotationQuaternion(m_rotation); DirectX::XMMATRIX scale_mat = DirectX::XMMatrixScalingFromVector(m_scale); m_transform = m_local_transform = scale_mat * rotation_mat * translation_mat; if (m_parent) m_transform *= m_parent->m_transform; SignalChange(); } } /* wr */ ================================================ FILE: src/scene_graph/node.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include namespace wr { struct Node : std::enable_shared_from_this { Node(); explicit Node(std::type_info const & type_info); void SignalChange(); void SignalUpdate(unsigned int frame_idx); bool RequiresUpdate(unsigned int frame_idx); void SignalTransformChange(); void SignalTransformUpdate(unsigned int frame_idx); bool RequiresTransformUpdate(unsigned int frame_idx); //Takes roll, pitch and yaw and converts it to quaternion virtual void SetRotation(DirectX::XMVECTOR roll_pitch_yaw); virtual void SetRotationQuaternion(DirectX::XMVECTOR rotation); //Takes raw values of a quaternion virtual void SetQuaternionRotation( float x, float y, float z, float w ); //Sets position virtual void SetPosition(DirectX::XMVECTOR position); //Sets scale virtual void SetScale(DirectX::XMVECTOR scale); //Position, rotation (roll, pitch, yaw) and scale virtual void SetTransform(DirectX::XMVECTOR position, DirectX::XMVECTOR rotation, DirectX::XMVECTOR scale); //Update the transform; done automatically when SignalChange is called void UpdateTransform(); std::shared_ptr m_parent; std::vector> m_children; //Translation of mesh node DirectX::XMVECTOR m_position = { 0, 0, 0, 1 }; //Rotation as quaternion DirectX::XMVECTOR m_rotation; //Rotation in radians DirectX::XMVECTOR m_rotation_radians = { 0,0,0 }; //Scale DirectX::XMVECTOR m_scale = { 1, 1, 1, 0 }; //Transformation DirectX::XMMATRIX m_local_transform, m_transform, m_prev_transform; const std::type_info& m_type_info; protected: bool m_use_quaternion = false; private: std::bitset<3> m_requires_update; std::bitset<3> m_requires_transform_update; }; } // namespace wr ================================================ FILE: src/scene_graph/scene_graph.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "scene_graph.hpp" #include #include "../renderer.hpp" #include "../util/log.hpp" #include "camera_node.hpp" #include "mesh_node.hpp" #include "skybox_node.hpp" #include "light_node.hpp" //TODO: Make platform independent #include "../d3d12/d3d12_defines.hpp" #include "../d3d12/d3d12_functions.hpp" #include "../d3d12/d3d12_renderer.hpp" #include "../d3d12/d3d12_constant_buffer_pool.hpp" namespace wr { SceneGraph::SceneGraph(RenderSystem* render_system) : m_render_system(render_system), m_root(std::make_shared()), m_light_buffer() { m_lights.resize(d3d12::settings::num_lights); m_default_skybox = std::make_shared(render_system->m_default_cubemap); m_default_skybox->m_skybox = m_default_skybox->m_prefiltered_env_map = m_default_skybox->m_irradiance = render_system->m_default_cubemap; } SceneGraph::~SceneGraph() { RemoveChildren(GetRootNode()); } //! Used to obtain the root node. std::shared_ptr SceneGraph::GetRootNode() const { return m_root; } //! Used to obtain the children of a node. std::vector> SceneGraph::GetChildren(std::shared_ptr const & parent) { return parent ? parent->m_children : m_root->m_children; } //! Used to remove the children of a node. void SceneGraph::RemoveChildren(std::shared_ptr const & parent) { parent->m_children.clear(); } //! Returns the active camera. /*! If there are multiple active cameras it will return the first one. */ std::shared_ptr SceneGraph::GetActiveCamera() { for (auto& camera_node : m_camera_nodes) { if (camera_node->m_active) { return camera_node; } } LOGW("Failed to obtain a active camera node."); return nullptr; } std::vector>& SceneGraph::GetLightNodes() { return m_light_nodes; } std::vector>& SceneGraph::GetMeshNodes() { return m_mesh_nodes; } std::shared_ptr SceneGraph::GetCurrentSkybox() { if (m_current_skybox) { return m_current_skybox; } return m_default_skybox; } void SceneGraph::UpdateSkyboxNode(std::shared_ptr node, TextureHandle new_equirectangular) { node->UpdateSkybox(new_equirectangular, m_render_system->GetFrameIdx()); m_render_system->RequestSkyboxReload(); } //! Initialize the scene graph void SceneGraph::Init() { m_init_meshes_func_impl(m_render_system, m_mesh_nodes); // Create constant buffer pool constexpr auto model_size = sizeof(temp::ObjectData) * d3d12::settings::num_instances_per_batch; constexpr auto model_cbs_size = SizeAlignTwoPower(model_size, 256) * d3d12::settings::num_back_buffers; m_constant_buffer_pool = m_render_system->CreateConstantBufferPool((uint32_t) model_cbs_size*1024); // Initialize cameras m_init_cameras_func_impl(m_render_system, m_camera_nodes); // Create Light Buffer std::uint64_t light_count = (std::uint64_t) m_lights.size(); std::uint64_t light_buffer_stride = sizeof(Light), light_buffer_size = light_buffer_stride * light_count; std::uint64_t light_buffer_aligned_size = SizeAlignTwoPower(light_buffer_size, 65536) * d3d12::settings::num_back_buffers; m_structured_buffer = m_render_system->CreateStructuredBufferPool((size_t)light_buffer_aligned_size ); m_light_buffer = m_structured_buffer->Create(light_buffer_size, light_buffer_stride, false); //Initialize lights m_init_lights_func_impl(m_render_system, m_light_nodes, m_lights); } //! Update the scene graph void SceneGraph::Update() { m_update_transforms_func_impl(m_render_system, *this, m_root); m_update_cameras_func_impl(m_render_system, m_camera_nodes); m_update_meshes_func_impl(m_render_system, m_mesh_nodes); m_update_lights_func_impl(m_render_system, *this); } //! Render the scene graph /*! The user is expected to call `Optimize`. If they don't this function will do it manually. */ void SceneGraph::Render(CommandList* cmd_list, CameraNode* camera) { m_render_meshes_func_impl(m_render_system, m_batches, camera, cmd_list); } temp::MeshBatches& SceneGraph::GetBatches() { return m_batches; } std::unordered_map, util::PairHash>& SceneGraph::GetGlobalBatches() { return m_objects; } StructuredBufferHandle* SceneGraph::GetLightBuffer() { return m_light_buffer; } uint32_t SceneGraph::GetCurrentLightSize() { return m_next_light_id; } float SceneGraph::GetRTCullingDistance() { return std::fabs(m_rt_culling_distance); } bool SceneGraph::GetRTCullingEnabled() { return m_rt_culling_distance > 0; } void SceneGraph::SetRTCullingDistance(float dist) { m_rt_culling_distance = dist * (GetRTCullingEnabled() * 2 - 1); } void SceneGraph::SetRTCullingEnable(bool b) { m_rt_culling_distance = GetRTCullingDistance() * (b * 2 - 1); } Light* SceneGraph::GetLight(uint32_t offset) { return offset >= m_next_light_id ? m_lights.data() : m_lights.data() + offset; } void SceneGraph::RegisterLight(std::shared_ptr& new_node) { //Allocate a light into the array if (m_next_light_id == (uint32_t)m_lights.size()) LOGE("Couldn't allocate light node; out of memory"); new_node->m_light = m_lights.data() + m_next_light_id; memcpy(new_node->m_light, &new_node->m_temp, sizeof(new_node->m_temp)); ++m_next_light_id; //Update light count if (!m_lights.empty()) { m_lights[0].tid &= 0x3; //Keep id m_lights[0].tid |= uint32_t(m_light_nodes.size() + 1) << 2; //Set lights if (!m_light_nodes.empty()) { m_light_nodes[0]->SignalChange(); } } //Track the node m_light_nodes.push_back(new_node); } void SceneGraph::Optimize() { //Update batches bool should_update = m_batches.empty(); for (auto& elem : m_batches) { if (elem.second.num_instances == 0) { should_update = true; break; } } if (should_update) { constexpr uint32_t max_size = d3d12::settings::num_instances_per_batch; constexpr auto model_size = sizeof(temp::ObjectData) * max_size; for (auto& node : m_mesh_nodes) { auto mesh_materials_pair = std::make_pair(node->m_model, node->m_materials); auto it = m_batches.find(mesh_materials_pair); //It won't keep track of anything if it has no model if (node->m_model == nullptr) { continue; } auto obj = m_objects.find(mesh_materials_pair); //Insert new if doesn't exist if (it == m_batches.end()) { ConstantBufferHandle* object_buffer = m_constant_buffer_pool->Create(model_size); auto& batch = m_batches[mesh_materials_pair]; batch.batch_buffer = object_buffer; batch.m_materials = node->GetMaterials(); batch.data.objects.resize(d3d12::settings::num_instances_per_batch); if (obj == m_objects.end()) { m_objects[mesh_materials_pair] = std::vector(d3d12::settings::num_instances_per_batch); obj = m_objects.find(mesh_materials_pair); } it = m_batches.find(mesh_materials_pair); } //Mark batch as "active" and keep track of instances ++it->second.num_total_instances; //Model should remain loaded, but not rendered if (!node->m_visible) { continue; } temp::MeshBatch& batch = it->second; batch.m_materials = node->GetMaterials(); //Cull for rasterizer if (!d3d12::settings::enable_object_culling || GetActiveCamera()->InView(node)) { unsigned int& offset = batch.num_instances; batch.data.objects[offset] = { node->m_transform, node->m_prev_transform }; ++offset; } //Cull for raytracer if (!GetRTCullingEnabled() || GetActiveCamera()->InRange(node, GetRTCullingDistance())) { unsigned int& globalOffset = batch.num_global_instances; obj->second[globalOffset] = { node->m_transform, node->m_prev_transform }; ++globalOffset; } } std::queue m_to_remove; for (auto& elem : m_batches) { //Release empty batches if (elem.second.num_total_instances == 0) { m_to_remove.push(elem.first); continue; } //Update object data temp::MeshBatch& batch = elem.second; m_constant_buffer_pool->Update(batch.batch_buffer, sizeof(temp::ObjectData) * elem.second.num_instances, 0, (uint8_t*)batch.data.objects.data()); elem.second.num_total_instances = 0; //Clear for future use } while(!m_to_remove.empty()) { wr::temp::BatchKey &key = m_to_remove.front(); if (m_batches[key].batch_buffer) { m_constant_buffer_pool->Destroy(m_batches[key].batch_buffer); } m_objects.erase(key); m_batches.erase(key); m_to_remove.pop(); } } } } /* wr */ ================================================ FILE: src/scene_graph/scene_graph.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include "node.hpp" #include "light_node.hpp" #include "../platform_independend_structs.hpp" #include "../util/user_literals.hpp" #include "../util/defines.hpp" #include "../util/log.hpp" #include "../model_pool.hpp" #include "../constant_buffer_pool.hpp" #include "../structured_buffer_pool.hpp" #include "../model_pool.hpp" #include "../util/delegate.hpp" #include "../util/pair_hash.hpp" namespace wr { class RenderSystem; struct CameraNode; using CommandList = void; struct CameraNode; struct MeshNode; struct SkyboxNode; namespace temp { struct ObjectData { DirectX::XMMATRIX m_model; DirectX::XMMATRIX m_prev_model; }; struct MeshBatch_CBData { std::vector objects; }; struct MeshBatch { unsigned int num_instances = 0, num_global_instances = 0, num_total_instances = 0; ConstantBufferHandle* batch_buffer; MeshBatch_CBData data; std::vector m_materials; }; using BatchKey = std::pair>; using MeshBatches = std::unordered_map; } class SceneGraph { public: explicit SceneGraph(RenderSystem* render_system); ~SceneGraph(); // Impl Functions static util::Delegate m_render_meshes_func_impl; static util::Delegate>&)> m_init_meshes_func_impl; static util::Delegate>&)> m_init_cameras_func_impl; static util::Delegate>&, std::vector&)> m_init_lights_func_impl; static util::Delegate>&)> m_update_meshes_func_impl; static util::Delegate>&)> m_update_cameras_func_impl; static util::Delegate m_update_lights_func_impl; static util::Delegate&)> m_update_transforms_func_impl; static util::Delegate&)> m_delete_skybox_func_impl; SceneGraph(SceneGraph&&) = delete; SceneGraph(SceneGraph const &) = delete; SceneGraph& operator=(SceneGraph&&) = delete; SceneGraph& operator=(SceneGraph const &) = delete; std::shared_ptr GetRootNode() const; template std::shared_ptr CreateChild(std::shared_ptr const & parent = nullptr, Args... args); std::vector> GetChildren(std::shared_ptr const & parent = nullptr); static void RemoveChildren(std::shared_ptr const & parent); std::shared_ptr GetActiveCamera(); std::vector>& GetLightNodes(); std::vector>& GetMeshNodes(); std::shared_ptr GetCurrentSkybox(); void UpdateSkyboxNode(std::shared_ptr node, TextureHandle new_equirectangular); void Init(); void Update(); void Render(CommandList* cmd_list, CameraNode* camera); template void DestroyNode(std::shared_ptr node); void Optimize(); temp::MeshBatches& GetBatches(); std::unordered_map, util::PairHash>& GetGlobalBatches(); StructuredBufferHandle* GetLightBuffer(); Light* GetLight(uint32_t offset); //Returns nullptr when out of bounds uint32_t GetCurrentLightSize(); float GetRTCullingDistance(); bool GetRTCullingEnabled(); void SetRTCullingDistance(float dist); void SetRTCullingEnable(bool b); protected: void RegisterLight(std::shared_ptr& light_node); private: RenderSystem* m_render_system; //! The root node of the hiararchical tree. std::shared_ptr m_root; temp::MeshBatches m_batches; std::unordered_map, util::PairHash> m_objects; std::vector m_lights; std::shared_ptr m_structured_buffer; std::shared_ptr m_constant_buffer_pool; StructuredBufferHandle* m_light_buffer; std::vector> m_camera_nodes; std::vector> m_mesh_nodes; std::vector> m_light_nodes; std::vector< std::shared_ptr> m_skybox_nodes; std::shared_ptr m_default_skybox = nullptr; std::shared_ptr m_current_skybox = nullptr; uint32_t m_next_light_id = 0; float m_rt_culling_distance = -1; }; //! Creates a child into the scene graph /* If the parent is a nullptr the child will be created on the root node. */ template std::shared_ptr SceneGraph::CreateChild(std::shared_ptr const & parent, Args... args) { auto p = parent ? parent : m_root; auto new_node = std::make_shared(args...); p->m_children.push_back(new_node); new_node->m_parent = p; if constexpr (std::is_base_of::value) { m_camera_nodes.push_back(new_node); } else if constexpr (std::is_base_of::value) { m_mesh_nodes.push_back(new_node); } else if constexpr (std::is_base_of::value) { RegisterLight(new_node); } else if constexpr (std::is_same::value) { m_skybox_nodes.push_back(new_node); //This matches Maya's behaviour of always showing the latest skybox created. m_current_skybox = new_node; } return new_node; } template void SceneGraph::DestroyNode(std::shared_ptr node) { if constexpr (std::is_base_of::value) { for (size_t i = 0, j = m_camera_nodes.size(); i < j; ++i) { if (m_camera_nodes[i] == node) { m_camera_nodes.erase(m_camera_nodes.begin() + i); break; } } } else if constexpr (std::is_base_of::value) { for (size_t i = 0, j = m_mesh_nodes.size(); i < j; ++i) { if (m_mesh_nodes[i] == node) { m_mesh_nodes.erase(m_mesh_nodes.begin() + i); break; } } } else if constexpr (std::is_base_of::value) { for (size_t i = 0, j = m_light_nodes.size(); i < j; ++i) { if (m_light_nodes[i] == node) { //Move everything after this light back one light, so the memory is one filled array for (size_t k = i + 1; k < j; ++k) { memcpy(m_light_nodes[k - 1]->m_light, m_light_nodes[k]->m_light, sizeof(Light)); --m_light_nodes[k]->m_light; m_light_nodes[k]->SignalChange(); } //Update light count if (m_lights.size() != 0) { m_lights[0].tid &= 0x3; //Keep id m_lights[0].tid |= uint32_t(m_light_nodes.size() - 1) << 2; //Set lights } //Stop tracking the node m_light_nodes.erase(m_light_nodes.begin() + i); break; } } } else if constexpr (std::is_base_of::value) { for (size_t i = 0, j = m_skybox_nodes.size(); i < j; ++i) { if (m_skybox_nodes[i] == node) { if (m_current_skybox == m_skybox_nodes[i]) { size_t num_nodes = m_skybox_nodes.size(); if (num_nodes > 1) { m_current_skybox = m_skybox_nodes[num_nodes - 1]; } else { m_current_skybox = nullptr; LOGW("[WARNING]: Last skybox node deleted, m_current_skybox is now a nullptr") } } m_delete_skybox_func_impl(m_render_system, *this, node); m_skybox_nodes.erase(m_skybox_nodes.begin() + i); break; } } } m_next_light_id = (uint32_t) m_light_nodes.size(); node->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()); node.reset(); } } /* wr */ ================================================ FILE: src/scene_graph/skybox_node.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "skybox_node.hpp" namespace wr { void SkyboxNode::UpdateSkybox(TextureHandle new_equirectangular, unsigned int frame_idx) { m_irradiance.value().m_pool->MarkForUnload(m_irradiance.value(), frame_idx); m_irradiance = std::nullopt; m_skybox.value().m_pool->MarkForUnload(m_skybox.value(), frame_idx); m_skybox = std::nullopt; m_prefiltered_env_map.value().m_pool->MarkForUnload(m_prefiltered_env_map.value(), frame_idx); m_prefiltered_env_map = std::nullopt; if (m_hdr.m_pool) { m_hdr.m_pool->MarkForUnload(m_hdr, frame_idx); #ifdef _DEBUG //Decide if we want to break when developing in case we enter this function //as in theory m_hdr needed to be cleaned from the render task, if that's not the case something went wrong. LOGC("[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"); #endif // DEBUG } m_hdr = new_equirectangular; } } /* wr */ ================================================ FILE: src/scene_graph/skybox_node.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "scene_graph.hpp" namespace wr { struct SkyboxNode : Node { explicit SkyboxNode(wr::TextureHandle hdr_texture, std::optional cubemap = std::nullopt, std::optional irradiance = std::nullopt) : Node::Node(typeid(SkyboxNode)) , m_hdr(hdr_texture) , m_skybox(cubemap) , m_irradiance(irradiance) {} void UpdateSkybox(TextureHandle new_equirectangular, unsigned int frame_idx); wr::TextureHandle m_hdr; std::optional m_skybox; std::optional m_irradiance; std::optional m_prefiltered_env_map; }; };// namespace wr ================================================ FILE: src/settings.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once namespace wr::settings { static const constexpr bool use_multithreading = true; static const constexpr unsigned int num_frame_graph_threads = 4; static const constexpr std::uint8_t default_textures_count = 5; static const constexpr std::uint32_t default_textures_size_in_bytes = 4ul * 1024ul * 1024ul; static constexpr const char* default_albedo_path = "resources/materials/metalgrid2_basecolor.png"; static constexpr const char* default_normal_path = "resources/materials/flat_normal.png"; static constexpr const char* default_white_texture = "resources/materials/white.png"; static constexpr const char* default_black_texture = "resources/materials/black.png"; } /* wr::settings */ ================================================ FILE: src/shader_registry.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "shader_registry.hpp" namespace wr { ShaderRegistry::ShaderRegistry() : Registry() { } RegistryHandle ShaderRegistry::Register(ShaderDescription description) { auto handle = m_next_handle; m_descriptions.insert({ handle, description }); m_next_handle++; return handle; } } /* wr */ ================================================ FILE: src/shader_registry.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "registry.hpp" #include "d3d12/d3d12_enums.hpp" #include "util/named_type.hpp" namespace wr { using Shader = void; struct ShaderDescription { std::string path; std::string entry; ShaderType type; std::vector> defines; }; class ShaderRegistry : public internal::Registry { public: ShaderRegistry(); virtual ~ShaderRegistry() = default; RegistryHandle Register(ShaderDescription description); }; } /* wr */ ================================================ FILE: src/structs.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include namespace wr { struct Texture { }; struct ReadbackBuffer { }; class TexturePool; struct TextureHandle { TexturePool* m_pool = nullptr; std::uint32_t m_id = 0; }; struct MaterialUVScales { float m_albedo_scale = 1.0f; float m_normal_scale = 1.0f; float m_roughness_scale = 1.0f; float m_metallic_scale = 1.0f; float m_emissive_scale = 1.0f; float m_ao_scale = 1.0f; }; struct CPUTexture { float* m_data = nullptr; unsigned int m_buffer_width; unsigned int m_buffer_height; unsigned int m_bytes_per_pixel; }; class MaterialPool; struct MaterialHandle { MaterialPool* m_pool; std::uint32_t m_id; friend bool operator ==(MaterialHandle const & lhs, MaterialHandle const & rhs) { return lhs.m_pool == rhs.m_pool && lhs.m_id == rhs.m_id; } friend bool operator!=(MaterialHandle const& lhs, MaterialHandle const& rhs) { return lhs.m_pool != rhs.m_pool || lhs.m_id != rhs.m_id; } }; } ================================================ FILE: src/structured_buffer_pool.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "structured_buffer_pool.hpp" namespace wr { StructuredBufferPool::StructuredBufferPool(std::size_t size_in_bytes) : m_size_in_bytes(size_in_bytes) { } StructuredBufferHandle * StructuredBufferPool::Create(std::size_t size, std::size_t stride, bool used_as_uav) { std::lock_guard lock(m_mutex); return CreateBuffer(size, stride, used_as_uav); } void StructuredBufferPool::Destroy(StructuredBufferHandle * handle) { std::lock_guard lock(m_mutex); DestroyBuffer(handle); } void StructuredBufferPool::Update(StructuredBufferHandle * handle, void * data, std::size_t size, std::size_t offset) { std::lock_guard lock(m_mutex); UpdateBuffer(handle, data, size, offset); } } /* wr */ ================================================ FILE: src/structured_buffer_pool.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include namespace wr { class StructuredBufferPool; struct StructuredBufferHandle { StructuredBufferPool* m_pool; }; class StructuredBufferPool { public: explicit StructuredBufferPool(std::size_t size_in_bytes); virtual ~StructuredBufferPool() = default; StructuredBufferPool(StructuredBufferPool const &) = delete; StructuredBufferPool& operator=(StructuredBufferPool const &) = delete; StructuredBufferPool(StructuredBufferPool&&) = delete; StructuredBufferPool& operator=(StructuredBufferPool&&) = delete; [[nodiscard]] StructuredBufferHandle* Create(std::size_t size, std::size_t stride, bool used_as_uav); void Destroy(StructuredBufferHandle* handle); void Update(StructuredBufferHandle* handle, void* data, std::size_t size, std::size_t offset); virtual void Evict() = 0; virtual void MakeResident() = 0; protected: [[nodiscard]] virtual StructuredBufferHandle* CreateBuffer(std::size_t size, std::size_t stride, bool used_as_uav) = 0; virtual void DestroyBuffer(StructuredBufferHandle* handle) = 0; virtual void UpdateBuffer(StructuredBufferHandle* handle, void* data, std::size_t size, std::size_t offset) = 0; std::size_t m_size_in_bytes; std::mutex m_mutex; }; } /* wr */ ================================================ FILE: src/util/aabb.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "aabb.hpp" namespace wr { Box::Box() : m_data { { std::numeric_limits::max(), std::numeric_limits::max(), std::numeric_limits::max() }, { -std::numeric_limits::max(), -std::numeric_limits::max(), -std::numeric_limits::max() }, { std::numeric_limits::max(), std::numeric_limits::max(), std::numeric_limits::max() }, { -std::numeric_limits::max(), -std::numeric_limits::max(), -std::numeric_limits::max() }, { std::numeric_limits::max(), std::numeric_limits::max(), std::numeric_limits::max() }, { -std::numeric_limits::max(), -std::numeric_limits::max(), -std::numeric_limits::max() } } { } Box::Box(DirectX::XMVECTOR(&corners)[6]) { memcpy(m_data, corners, sizeof(corners)); } DirectX::XMVECTOR& Box::operator[](size_t i) { return m_data[i]; } DirectX::XMVECTOR& AABB::operator[](size_t i) { return m_data[i]; } void AABB::Expand(DirectX::XMVECTOR pos) { m_min = { std::min(pos.m128_f32[0], *m_minf), std::min(pos.m128_f32[1], m_minf[1]), std::min(pos.m128_f32[2], m_minf[2]), 1 }; m_max = { std::max(pos.m128_f32[0], *m_maxf), std::max(pos.m128_f32[1], m_maxf[1]), std::max(pos.m128_f32[2], m_maxf[2]), 1 }; } AABB::AABB() : m_data { { std::numeric_limits::max(), std::numeric_limits::max(), std::numeric_limits::max(), 1 }, { -std::numeric_limits::max(), -std::numeric_limits::max(), -std::numeric_limits::max(), 1 } } { } AABB::AABB(DirectX::XMVECTOR min, DirectX::XMVECTOR max) : m_data{ min, max } { } AABB AABB::FromTransform(Box box, DirectX::XMMATRIX transform) { AABB aabb; //Transform all coords from model to world space //Pick the min/max bounds for (DirectX::XMVECTOR& vec : box.m_data) { DirectX::XMVECTOR tvec = DirectX::XMVector4Transform(vec, transform); aabb.Expand(tvec); } return aabb; } void Box::ExpandFromVector(DirectX::XMVECTOR pos) { if (pos.m128_f32[0] < m_corners.m_xmin.m128_f32[0]) { m_corners.m_xmin = pos; } if (pos.m128_f32[0] > m_corners.m_xmax.m128_f32[0]) { m_corners.m_xmax = pos; } if (pos.m128_f32[1] < m_corners.m_ymin.m128_f32[1]) { m_corners.m_ymin = pos; } if (pos.m128_f32[1] > m_corners.m_ymax.m128_f32[1]) { m_corners.m_ymax = pos; } if (pos.m128_f32[2] < m_corners.m_zmin.m128_f32[2]) { m_corners.m_zmin = pos; } if (pos.m128_f32[2] > m_corners.m_zmax.m128_f32[2]) { m_corners.m_zmax = pos; } } void Box::Expand(float(&pos)[3]) { ExpandFromVector(DirectX::XMVECTOR{ pos[0], pos[1], pos[2], 1 }); } Sphere::Sphere(): m_sphere { std::numeric_limits::max(), std::numeric_limits::max(), std::numeric_limits::max(), -std::numeric_limits::max() }{ } Sphere::Sphere(DirectX::XMVECTOR center, float radius): m_sphere { center.m128_f32[0], center.m128_f32[1], center.m128_f32[2], radius }{ } bool AABB::InFrustum(const std::array& planes) const { for (const DirectX::XMVECTOR& plane : planes) { /* Get point of AABB that's into the plane the most */ DirectX::XMVECTOR axis_vert = { *m_data[*plane.m128_f32 >= 0].m128_f32, m_data[plane.m128_f32[1] >= 0].m128_f32[1], m_data[plane.m128_f32[2] >= 0].m128_f32[2] }; /* Check if it's outside */ if (*DirectX::XMVector3Dot(plane, axis_vert).m128_f32 + plane.m128_f32[3] < 0) return false; } return true; } bool AABB::Contains(const Sphere& sphere) const { float square_dist = 0; for(size_t i = 0; i < 3; ++i) { if(sphere.m_data[i] < m_minf[i]) { square_dist += pow(sphere.m_data[i] - m_minf[i], 2); } else if(sphere.m_data[i] > m_maxf[i]) { square_dist += pow(sphere.m_data[i] - m_maxf[i], 2); } } const float r_squared = sphere.m_radius * sphere.m_radius; return square_dist <= r_squared; } } ================================================ FILE: src/util/aabb.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include namespace wr { struct Box { struct Corners { DirectX::XMVECTOR m_xmin, m_xmax, m_ymin, m_ymax, m_zmin, m_zmax; }; union { Corners m_corners; DirectX::XMVECTOR m_data[6]; }; //Max bounds on each corner Box(); Box(DirectX::XMVECTOR (&corners)[6]); DirectX::XMVECTOR &operator[](size_t i); //Expand bounds using position void ExpandFromVector(DirectX::XMVECTOR pos); void Expand(float(&pos)[3]); }; struct Sphere { union { DirectX::XMVECTOR m_sphere; struct { DirectX::XMFLOAT3 m_center; float m_radius; }; struct { float m_data[4]; }; }; Sphere(); Sphere(DirectX::XMVECTOR center, float radius); }; struct AABB { union { DirectX::XMVECTOR m_data[2]; struct { DirectX::XMVECTOR m_min, m_max; }; struct { float m_minf[3]; float m_pad0; float m_maxf[3]; float m_pad1; }; }; AABB(); AABB(DirectX::XMVECTOR min, DirectX::XMVECTOR max); DirectX::XMVECTOR& operator[](size_t i); //Uses position to expand the AABB void Expand(DirectX::XMVECTOR pos); //Check if the frustum planes intersect with the AABB bool InFrustum(const std::array& planes) const; //Check if the sphere intersects with the AABB bool Contains(const Sphere& sphere) const; //Generates AABB from transform and box static AABB FromTransform(Box box, DirectX::XMMATRIX transform); }; } ================================================ FILE: src/util/bitmap_allocator.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include namespace util { inline std::uint64_t IndexFromBit(std::uint64_t frame) { return frame / (8 * 8); } inline std::uint64_t OffsetFromBit(std::uint64_t frame) { return frame % (8 * 8); } inline void SetPage(std::vector & bitmap, std::uint64_t frame) { const std::uint64_t idx = IndexFromBit(frame); const std::uint64_t off = OffsetFromBit(frame); bitmap[idx] |= (1Ui64 << off); } inline void ClearPage(std::vector & bitmap, std::uint64_t frame) { const std::uint64_t idx = IndexFromBit(frame); const std::uint64_t off = OffsetFromBit(frame); bitmap[idx] &= ~(1Ui64 << off); } inline bool TestPage(std::vector const & bitmap, std::uint64_t frame) { const std::uint64_t idx = IndexFromBit(frame); const std::uint64_t off = OffsetFromBit(frame); return (bitmap[idx] & (1Ui64 << off)); } inline std::optional FindFreePage(std::vector const & bitmap, std::size_t const frame_count, std::uint64_t needed_frames) { bool counting = false; bool found = false; std::uint64_t start_frame = 0; std::uint64_t free_frames = 0; //Go through all all pages to find free ones. //Because we're storing the data in a single bit we can compare an entire int64 at once to speed things up for (std::uint64_t i = 0; i < bitmap.size(); ++i) { //Check 64 pages at once if (bitmap[i] != 0Ui64) { //At least a single page is free, so check all 64 pages for (std::uint64_t j = 0; j < 64; ++j) { //If the current page being checked is larger than the amount of available pages, break if (i * 64 + j >= frame_count) { break; } //Set bit corresponding to page being checked in temporary variable std::uint64_t to_test = 1Ui64 << j; //Run an 'and' against the temporary variable to see if the page is available if ((bitmap[i] & to_test)) { //Is this the first free page found? if (counting) { //Not the first free page, so increment the counter of consecutive free pages free_frames++; //Have we found the nessecary amount of free pages? If so, indicate that and break if (free_frames == needed_frames) { found = true; break; } } else { //This is the first free page if (needed_frames == 1) { //We only need a single page, so store the number of the page and break found = true; start_frame = i * 64 + j; break; } else { //We need multiple pages, so store the number of this page as the start page and continue start_frame = i * 64 + j; counting = true; free_frames = 1; } } } else { //The page wasn't free, so stop counting. counting = 0; free_frames = 0; } } } else { //All 64 pages are occupied, so stop counting counting = false; free_frames = 0; } //Have we found enough free pages? If so, break if (found) { break; } } return start_frame; } } /* util */ ================================================ FILE: src/util/defines.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once //! A Scene Graph Implementation Binder Macro /*! This macro is used to link a initialization and a render function to a specific node type. */ #define LINK_NODE_FUNCTION(renderer_type, node_type, update_function) \ decltype(node_type::update_func_impl) node_type::update_func_impl = [](wr::RenderSystem* render_system, Node* node) \ { \ static_cast(render_system)->update_function(static_cast(node)); \ }; //! World Up static constexpr float world_up[3] = {0, 1, 0}; //! Used to check whether a method exists in a class. #define DEFINE_HAS_METHOD(f) \ template \ struct HasMethod_##f { \ static_assert( \ std::integral_constant::value, \ "Second template parameter needs to be of function type."); \ }; \ \ template \ struct HasMethod_##f { \ private: \ template \ static constexpr auto Check(T*) \ -> typename \ std::is_same< \ decltype( std::declval().f( std::declval()... ) ), \ Ret \ >::type; \ \ template \ static constexpr std::false_type Check(...); \ \ typedef decltype(Check(0)) type; \ \ public: \ static constexpr bool value = type::value; \ }; DEFINE_HAS_METHOD(GetInputLayout) #define IS_PROPER_VERTEX_CLASS(type) static_assert(HasMethod_GetInputLayout()>::value, "Could not locate the required type::GetInputLayout function. If intelisense gives you this error ignore it."); //! Defines to make linking to sg easier. #define LINK_SG_RENDER_MESHES(renderer_type, function) \ decltype(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) \ { \ static_cast(render_system)->function(nodes, camera, cmd_list); \ }; #define LINK_SG_INIT_MESHES(renderer_type, function) \ decltype(wr::SceneGraph::m_init_meshes_func_impl) wr::SceneGraph::m_init_meshes_func_impl = [](wr::RenderSystem* render_system, std::vector>& nodes) \ { \ static_cast(render_system)->function(nodes); \ }; #define LINK_SG_INIT_CAMERAS(renderer_type, function) \ decltype(wr::SceneGraph::m_init_cameras_func_impl) wr::SceneGraph::m_init_cameras_func_impl = [](wr::RenderSystem* render_system, std::vector>& nodes) \ { \ static_cast(render_system)->function(nodes); \ }; #define LINK_SG_INIT_LIGHTS(renderer_type, function) \ decltype(wr::SceneGraph::m_init_lights_func_impl) wr::SceneGraph::m_init_lights_func_impl = [](wr::RenderSystem* render_system, std::vector>& nodes, std::vector& lights) \ { \ static_cast(render_system)->function(nodes, lights); \ }; #define LINK_SG_UPDATE_MESHES(renderer_type, function) \ decltype(wr::SceneGraph::m_update_meshes_func_impl) wr::SceneGraph::m_update_meshes_func_impl = [](wr::RenderSystem* render_system, std::vector>& nodes) \ { \ static_cast(render_system)->function(nodes); \ }; #define LINK_SG_UPDATE_CAMERAS(renderer_type, function) \ decltype(wr::SceneGraph::m_update_cameras_func_impl) wr::SceneGraph::m_update_cameras_func_impl = [](wr::RenderSystem* render_system, std::vector>& nodes) \ { \ static_cast(render_system)->function(nodes); \ }; #define LINK_SG_UPDATE_LIGHTS(renderer_type, function) \ decltype(wr::SceneGraph::m_update_lights_func_impl) wr::SceneGraph::m_update_lights_func_impl = [](wr::RenderSystem* render_system, wr::SceneGraph& scene_graph) \ { \ static_cast(render_system)->function(scene_graph); \ }; #define LINK_SG_UPDATE_TRANSFORMS(renderer_type, function) \ decltype(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& node) \ { \ static_cast(render_system)->function(scene_graph, node); \ }; #define LINK_SG_DELETE_SKYBOX(renderer_type, function) \ decltype(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& skybox_node) \ { \ static_cast(render_system)->function(scene_graph, skybox_node); \ }; ================================================ FILE: src/util/delegate.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include namespace util { template class Delegate; template class Delegate { using stub_ptr_type = R(*)(void*, A&&...); Delegate(void* const o, stub_ptr_type const m) noexcept : m_object_ptr(o), m_stub_ptr(m) { } public: Delegate() : m_object_ptr(), m_deleter(), m_store_size() { } Delegate(Delegate const&) = default; Delegate(Delegate&&) = default; Delegate(::std::nullptr_t const) noexcept : Delegate() { } template {} > ::type > explicit Delegate(C const* const o) noexcept : m_object_ptr(const_cast(o)) { } template {} > ::type > explicit Delegate(C const& o) noexcept : m_object_ptr(const_cast(&o)) { } template Delegate(C* const object_ptr, R(C::* const method_ptr)(A...)) { *this = from(object_ptr, method_ptr); } template Delegate(C* const object_ptr, R(C::* const method_ptr)(A...) const) { *this = from(object_ptr, method_ptr); } template Delegate(C& object, R(C::* const method_ptr)(A...)) { *this = from(object, method_ptr); } template Delegate(C const& object, R(C::* const method_ptr)(A...) const) { *this = from(object, method_ptr); } template < typename T, typename = typename ::std::enable_if < !::std::is_same::type>{} > ::type > Delegate(T&& f) : m_store(operator new(sizeof(typename ::std::decay::type)), functor_deleter::type>), m_store_size(sizeof(typename ::std::decay::type)) { using functor_type = typename ::std::decay::type; new (m_store.get()) functor_type(::std::forward(f)); m_object_ptr = m_store.get(); m_stub_ptr = functor_stub; m_deleter = deleter_stub; } Delegate& operator=(Delegate const&) = default; Delegate& operator=(Delegate&&) = default; template Delegate& operator=(R(C::* const rhs)(A...)) { return *this = from(static_cast(m_object_ptr), rhs); } template Delegate& operator=(R(C::* const rhs)(A...) const) { return *this = from(static_cast(m_object_ptr), rhs); } template < typename T, typename = typename ::std::enable_if < !::std::is_same::type>{} > ::type > Delegate& operator=(T&& f) { using functor_type = typename ::std::decay::type; // Note that use_count is an approximation in multithreaded environments. if ((sizeof(functor_type) > m_store_size) || m_store.use_count() != 1) { m_store.reset(operator new(sizeof(functor_type)), functor_deleter); m_store_size = sizeof(functor_type); } else { m_deleter(m_store.get()); } new (m_store.get()) functor_type(::std::forward(f)); m_object_ptr = m_store.get(); m_stub_ptr = functor_stub; m_deleter = deleter_stub; return *this; } template static Delegate from() noexcept { return { nullptr, function_stub }; } template static Delegate from(C* const object_ptr) noexcept { return { object_ptr, method_stub }; } template static Delegate from(C const* const object_ptr) noexcept { return { const_cast(object_ptr), const_method_stub }; } template static Delegate from(C& object) noexcept { return { &object, method_stub }; } template static Delegate from(C const& object) noexcept { return { const_cast(&object), const_method_stub }; } template static Delegate from(T&& f) { return ::std::forward(f); } static Delegate from(R(*const function_ptr)(A...)) { return function_ptr; } template using member_pair = ::std::pair; template using const_member_pair = ::std::pair; template static Delegate from(C* const object_ptr, R(C::* const method_ptr)(A...)) { return member_pair(object_ptr, method_ptr); } template static Delegate from(C const* const object_ptr, R(C::* const method_ptr)(A...) const) { return const_member_pair(object_ptr, method_ptr); } template static Delegate from(C& object, R(C::* const method_ptr)(A...)) { return member_pair(&object, method_ptr); } template static Delegate from(C const& object, R(C::* const method_ptr)(A...) const) { return const_member_pair(&object, method_ptr); } void reset() { m_stub_ptr = nullptr; m_store.reset(); } void reset_stub() noexcept { m_stub_ptr = nullptr; } void swap(Delegate& other) noexcept { ::std::swap(*this, other); } bool operator==(Delegate const& rhs) const noexcept { return (m_object_ptr == rhs.m_object_ptr) && (m_stub_ptr == rhs.m_stub_ptr); } bool operator!=(Delegate const& rhs) const noexcept { return !operator==(rhs); } bool operator<(Delegate const& rhs) const noexcept { return (m_object_ptr < rhs.m_object_ptr) || ((m_object_ptr == rhs.m_object_ptr) && (m_stub_ptr < rhs.m_stub_ptr)); } bool operator==(::std::nullptr_t const) const noexcept { return !m_stub_ptr; } bool operator!=(::std::nullptr_t const) const noexcept { return m_stub_ptr; } explicit operator bool() const noexcept { return m_stub_ptr; } R operator()(A... args) const { // assert(stub_ptr); return m_stub_ptr(m_object_ptr, ::std::forward(args)...); } private: friend struct ::std::hash; using deleter_type = void(*)(void*); void* m_object_ptr; stub_ptr_type m_stub_ptr{}; deleter_type m_deleter; ::std::shared_ptr m_store; ::std::size_t m_store_size; template static void functor_deleter(void* const p) { static_cast(p)->~T(); operator delete(p); } template static void deleter_stub(void* const p) { static_cast(p)->~T(); } template static R function_stub(void* const, A&&... args) { return function_ptr(::std::forward(args)...); } template static R method_stub(void* const object_ptr, A&&... args) { return (static_cast(object_ptr)->*method_ptr)( ::std::forward(args)...); } template static R const_method_stub(void* const object_ptr, A&&... args) { return (static_cast(object_ptr)->*method_ptr)( ::std::forward(args)...); } template struct is_member_pair : std::false_type { }; template struct is_member_pair< ::std::pair > : std::true_type { }; template struct is_const_member_pair : std::false_type { }; template struct is_const_member_pair< ::std::pair > : std::true_type { }; template static typename ::std::enable_if < !(is_member_pair() || is_const_member_pair()), R > ::type functor_stub(void* const object_ptr, A&&... args) { return (*static_cast(object_ptr))(::std::forward(args)...); } template static typename ::std::enable_if < is_member_pair() || is_const_member_pair(), R > ::type functor_stub(void* const object_ptr, A&&... args) { return (static_cast(object_ptr)->first->* static_cast(object_ptr)->second)(::std::forward(args)...); } }; } /* util */ namespace std { template struct hash<::util::Delegate > { size_t operator()(::util::Delegate const& d) const noexcept { auto const seed(hash()(d.object_ptr_)); return hash::stub_ptr_type>()( d.stub_ptr_) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } }; } ================================================ FILE: src/util/file_watcher.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "file_watcher.hpp" #include #include "log.hpp" namespace util { FileWatcher::FileWatcher(std::string const & path, const std::chrono::milliseconds delay, bool regular_files_only) : m_watch_path(path), m_delay(delay), m_regular_files_only(regular_files_only), m_running(false) { // Record all files for (auto &file : std::filesystem::recursive_directory_iterator(path)) { m_paths[file.path().string()] = std::filesystem::last_write_time(file); } } FileWatcher::~FileWatcher() { if (m_thread.joinable()) { m_running = false; m_thread.join(); } } void FileWatcher::Start(util::Delegate const & callback) { if (m_running) { LOGW("Tried to start the file watcher while it is already running"); return; } else { m_running = true; } // Lambda that checks whether the callback should be called or not based on settings. auto run_callback = [this, callback](std::string path, FileStatus status) { bool is_special = (!std::filesystem::is_regular_file(std::filesystem::path(path)) && status != FileStatus::ERASED); if (m_regular_files_only && is_special) { return; } callback(path, status); }; while (m_running) { std::this_thread::sleep_for(m_delay); // Check for file ereasure. for (auto& it : m_paths) { if (!std::filesystem::exists(it.first)) { run_callback(it.first, FileStatus::ERASED); m_paths.erase(it.first); } } // Check if file was created or modified. for (auto& file : std::filesystem::recursive_directory_iterator(m_watch_path)) { auto last_write_time = std::filesystem::last_write_time(file); auto file_path = file.path().string(); // File Created if (!m_paths.contains(file_path)) { m_paths[file_path] = last_write_time; run_callback(file_path, FileStatus::CREATED); } // File Modified else { if (m_paths[file_path] != last_write_time) { m_paths[file_path] = last_write_time; run_callback(file_path, FileStatus::MODIFIED); } } } } } void FileWatcher::StartAsync(util::Delegate const & callback) { if (m_running) { LOGW("Tried to start the file watcher while it is already running"); return; } m_thread = std::thread([this, callback]() { Start(callback); }); } } /* util */ ================================================ FILE: src/util/file_watcher.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include "delegate.hpp" namespace util { //! FileWatcher /*! This is a simple file watcher that checks every X milliseconds whether a file has been created, modified or erased and gives you a callback if a change is detected. This file watcher can run both asyncronously and syncrounously. */ class FileWatcher { public: enum class FileStatus { MODIFIED, CREATED, ERASED }; //! FileWatcher constructor /*! \param path The path to watch for changes. \param delay The interval used to check changes. \param regular_files_only Setting this to true will ignore all non-regular files. */ FileWatcher(std::string const & path, const std::chrono::milliseconds delay, bool regular_files_only = false); ~FileWatcher(); //! Launches the file watcher. This will stall the current thread. void Start(util::Delegate const & callback); //! Launches the file watcher asynchrounously. When the file watcher goes out of scope the thread gets killed. void StartAsync(util::Delegate const & callback); private: bool m_running; bool m_regular_files_only; const std::chrono::milliseconds m_delay; std::string m_watch_path; std::unordered_map m_paths; std::thread m_thread; }; } /* util */ ================================================ FILE: src/util/log.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "log.hpp" namespace util { std::function log_callback::impl = nullptr; wr::LogfileHandler* log_file_handler = nullptr; } ================================================ FILE: src/util/log.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "../wisprenderer_export.hpp" #include "logfile_handler.hpp" #define LOG_PRINT_TIME //#define LOG_PRINT_THREAD #define LOG_CALLBACK //#define LOG_PRINT_LOC #define LOG_PRINT_COLORS //#define LOG_PRINT_TO_OUTPUT #ifndef _DEBUG #define LOG_TO_FILE #define LOG_PRINT_LOC #endif // DEBUG #if defined(LOG_PRINT_COLORS) && defined(_WIN32) #include #endif #ifdef LOG_PRINT_THREAD #include #include #endif #include #include #ifdef LOG_CALLBACK #include #endif #include #include #define LOG_BREAK DebugBreak(); #ifdef LOG_CALLBACK namespace util { struct log_callback { WISPRENDERER_EXPORT static std::function impl; }; WISPRENDERER_EXPORT extern wr::LogfileHandler* log_file_handler; }; #endif #pragma warning(push) #pragma warning(disable : 4244) #pragma warning(disable : 4100) namespace util::internal { enum class MSGB_ICON { CRITICAL_ERROR = (unsigned)MB_OK | (unsigned)MB_ICONERROR }; template inline void log_impl(int color, char type, std::string file, std::string func, int line, S const & format, Args const &... args) { std::string str = ""; #ifdef LOG_PRINT_TIME std::tm s; std::time_t t = std::time(nullptr); localtime_s(&s, &t); str += fmt::format("[{:%H:%M:%S}]", s) + " [" + type + "] "; #endif #ifdef LOG_PRINT_THREAD auto thread_id = std::this_thread::get_id(); std::stringstream ss; ss << thread_id; std::string thread_id_str = ss.str(); if (thread_id_str != "1") { str += "[thread:" + thread_id_str + "] "; } #endif str += format; #ifdef LOG_PRINT_LOC str += " "; // add tab to make it easier to read. auto found = file.find_last_of('/\\'); auto file_name = file.substr(found + 1); //remove path from file name. str += "[" + file + ":" + func + ":" + std::to_string(line) + "] "; #endif str += "\n"; #if defined(LOG_PRINT_COLORS) && defined(_WIN32) if (color != 0) { auto console = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(console, color); } #endif #ifdef WISPRENDERER_LOG_TO_STDOUT fmt::print(stdout, str, args...); #endif #if defined(LOG_PRINT_TO_OUTPUT) && defined(_WIN32) OutputDebugStringA(str.c_str()); #endif if (log_file_handler != nullptr) { fmt::print(log_file_handler->GetFilePtr(), str, args...); fflush(log_file_handler->GetFilePtr()); } #if defined(LOG_PRINT_COLORS) && defined(_WIN32) if (color != 0) { auto console = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(console, 7); } #endif #ifdef LOG_CALLBACK auto f_str = fmt::format(str, args...); if (log_callback::impl != nullptr) log_callback::impl(f_str); #endif } template inline void log_msgb_impl(MSGB_ICON icon, std::string file, std::string func, int line, S const & format, Args const &... args) { std::string str = ""; #ifdef LOG_PRINT_TIME std::tm s; std::time_t t = std::time(nullptr); localtime_s(&s, &t); str += fmt::format("[{:%H:%M}]\n", s); #endif #ifdef LOG_PRINT_THREAD auto thread_id = std::this_thread::get_id(); std::stringstream ss; ss << thread_id; std::string thread_id_str = ss.str(); if (thread_id_str != "1") { str += "[thread:" + thread_id_str + "]\n"; } #endif #ifdef LOG_PRINT_LOC str += "[" + file + ":" + func + ":" + std::to_string(line) + "] "; #endif str += format; str += "\n"; switch(icon) { case MSGB_ICON::CRITICAL_ERROR: MessageBox(0, str.c_str(), "Critical Error", (int)icon); break; default: MessageBox(0, str.c_str(), "Unkown Error", MB_OK | MB_ICONQUESTION); break; } } } /* internal */ #pragma warning( pop ) #define LOG(csr, ...) { util::internal::log_impl(7, 'I', __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); } #define LOGW(csr, ...) { util::internal::log_impl(6, 'W', __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); } #define LOGE(csr, ...) { util::internal::log_impl(4, 'E', __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); } #define LOGC(csr, ...) { util::internal::log_impl(71, 'C', __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); LOG_BREAK } //#define LOGC(csr, ...) { util::internal::log_msgb_impl(util::internal::MSGB_ICON::CRITICAL_ERROR, __FILE__, __func__, __LINE__, csr, ##__VA_ARGS__); } ================================================ FILE: src/util/logfile_handler.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "logfile_handler.hpp" #include wr::LogfileHandler::LogfileHandler() { std::filesystem::path path("./logs/"); if (!std::filesystem::exists(path)) { std::filesystem::create_directory(path); } std::stringstream ss; ss << "log-default"; path /= ss.str(); std::filesystem::create_directory(path); path /= "default-wisp.log"; m_file = fopen(path.string().c_str(), "w"); } wr::LogfileHandler::LogfileHandler(std::filesystem::path& dir_path, std::string& file_name) { std::filesystem::path path("./logs/"); if (!std::filesystem::exists(path)) { std::filesystem::create_directory(path); } path /= dir_path; std::filesystem::create_directory(path); path /= file_name; m_file = fopen(path.string().c_str(), "w"); } std::FILE* wr::LogfileHandler::GetFilePtr() { return m_file; } wr::LogfileHandler::~LogfileHandler() { fflush(m_file); fclose(m_file); } const std::filesystem::path& wr::LogfileHandler::GetDirPath() { return dir_path; } ================================================ FILE: src/util/logfile_handler.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include namespace wr { class LogfileHandler { public: LogfileHandler(); LogfileHandler(std::filesystem::path& dir_path, std::string& file_name); ~LogfileHandler(); std::FILE* GetFilePtr(); const std::filesystem::path& GetDirPath(); private: std::FILE* m_file = nullptr; std::filesystem::path dir_path; }; } ================================================ FILE: src/util/named_type.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once namespace util { //! Named Type /*! This is a naive named type implementation. Its only use is for member variables. For a more detailed implementation see Jonathan Boccara's general purpose implementation */ template class NamedType { public: explicit constexpr NamedType(T const& value) : m_value(value) { } constexpr T& Get() { return m_value; } constexpr T const& Get() const { return m_value; } operator T&() { return m_value; } private: T m_value; }; } /* util */ ================================================ FILE: src/util/pair_hash.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once namespace util { struct PairHash { template std::size_t operator() (const std::pair &pair) const { std::uint64_t hash = 0; for(int i=0;i()(pair.first) ^ hash; } }; } /* util */ ================================================ FILE: src/util/strings.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include namespace util { inline std::optional GetFileExtension(std::string_view path) { std::size_t pos = path.find_last_of("."); if (pos != std::string_view::npos) { std::size_t length = path.length(); std::string_view extension = path.substr(pos, length); return extension; } else { return {}; } } inline bool MatchFileExtension(std::string_view path, std::string_view extension) { std::size_t dot_position = path.find_last_of("."); if (dot_position == std::string_view::npos) return false; std::size_t last_occurrence_pos = path.rfind(extension); if (last_occurrence_pos == std::string_view::npos) return false; return last_occurrence_pos >= dot_position; } } ================================================ FILE: src/util/thread_pool.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* This thread pool is a modified version of https://github.com/progschj/ThreadPool. It is adjusted to fit our project structure better. Original licesne: Copyright (c) 2012 Jakob Progsch, Václav Zeman This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ #pragma once #include #include #include #include #include #include #include #include #include #include "delegate.hpp" namespace util { class ThreadPool { public: ThreadPool(size_t); template decltype(auto) Enqueue(F&& f, Args&&... args); ~ThreadPool(); private: // need to keep track of threads so we can join them std::vector m_workers; // the task queue std::queue> m_tasks; // synchronization std::mutex m_queue_mutex; std::condition_variable m_condition; bool m_stop; }; // the constructor just launches some amount of workers inline ThreadPool::ThreadPool(std::size_t threads) : m_stop(false) { for (decltype(threads) i = 0; i < threads; ++i) m_workers.emplace_back( [this] { for (;;) { Delegate task; { std::unique_lock lock(m_queue_mutex); m_condition.wait(lock, [this] { return m_stop || !m_tasks.empty(); }); if (m_stop && m_tasks.empty()) return; task = std::move(m_tasks.front()); m_tasks.pop(); } task(); } } ); } // add new work item to the pool template decltype(auto) ThreadPool::Enqueue(F&& f, Args&&... args) { using return_type = typename std::result_of::type; auto task = std::make_shared< std::packaged_task >( std::bind(std::forward(f), std::forward(args)...) ); std::future res = task->get_future(); { std::unique_lock lock(m_queue_mutex); // don't allow enqueueing after stopping the pool if (m_stop) throw std::runtime_error("enqueue on stopped ThreadPool"); m_tasks.emplace([task]() { (*task)(); }); } m_condition.notify_one(); return res; } // the destructor joins all threads inline ThreadPool::~ThreadPool() { { std::unique_lock lock(m_queue_mutex); m_stop = true; } m_condition.notify_all(); for (std::thread& worker : m_workers) { worker.join(); } } } ================================================ FILE: src/util/user_literals.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once constexpr float operator"" _deg(long double deg) { return DirectX::XMConvertToRadians(static_cast(deg)); } constexpr float operator"" _rad(long double rad) { return DirectX::XMConvertToDegrees(static_cast(rad)); } constexpr float operator"" _deg(unsigned long long int deg) { return DirectX::XMConvertToRadians(static_cast(deg)); } constexpr float operator"" _rad(unsigned long long int rad) { return DirectX::XMConvertToDegrees(static_cast(rad)); } constexpr std::size_t operator"" _kb(std::size_t kilobytes) { return kilobytes * 1024; } constexpr std::size_t operator"" _mb(std::size_t megabytes) { return megabytes * 1024 * 1024; } ================================================ FILE: src/version.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*! A constexpr way to obtain program version information. Copyright © 2019, Team Wisp Permission 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. THE 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. */ #define VERSION_FILE "../wisp.version" #include #include #include #include #include namespace wr { struct Version { std::uint32_t m_major = 0, m_minor = 0, m_patch = 0; }; namespace std_cexpr { constexpr size_t strlen(const char* str) { return (*str == 0) ? 0 : strlen(str + 1) + 1; } constexpr bool isdigit(char c) { return c <= '9' && c >= '0'; } constexpr std::uint32_t stoi_impl(const char* str, std::uint32_t max, std::uint32_t n = 0, std::uint32_t value = 0) { if (n >= max) return value; return *str ? isdigit(*str) ? stoi_impl(str + 1, max, ++n, (*str - '0') + value * 10) : value : value; } constexpr std::uint32_t stoi(const char* str, std::uint32_t max) { return stoi_impl(str, max); } } constexpr Version GetVersion() { const char* str = static_cast( #include VERSION_FILE ); char major_buf[128] = ""; char minor_buf[128] = ""; char patch_buf[128] = ""; int major_buf_size = 0; int minor_buf_size = 0; int patch_buf_size = 0; std::uint32_t num = 0; std::uint32_t itt = 0; for (std::uint32_t i = 0; i < std_cexpr::strlen(str); i++) { if (str[i] == '.') { num++; itt = 0; continue; } else if (num == 0) { major_buf[itt] = str[i]; major_buf_size++; } else if (num == 1) { minor_buf[itt] = str[i]; minor_buf_size++; } else if (num == 2) { patch_buf[itt] = str[i]; patch_buf_size++; } itt++; } if (num != 2) { throw "compile-time-error: Incorrect length"; } return { std_cexpr::stoi(major_buf, major_buf_size), std_cexpr::stoi(minor_buf, minor_buf_size), std_cexpr::stoi(patch_buf, patch_buf_size) }; } } /* wr */ ================================================ FILE: src/vertex.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include "util/defines.hpp" namespace wr { //! 2D vertex (in clip space) struct Vertex2D { float m_pos[2]; static std::vector GetInputLayout() { std::vector layout = { { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(Vertex2D, m_pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 } }; return layout; } }; //! Default Vertex struct Vertex { float m_pos[3]; float m_uv[2]; float m_normal[3]; float m_tangent[3]; float m_bitangent[3]; static std::vector GetInputLayout() { std::vector layout = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(Vertex, m_uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_normal), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_tangent), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}, { "BITANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_bitangent), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0} }; return layout; } }; //! Default Vertex struct VertexColor { float m_pos[3]; float m_uv[2]; float m_normal[3]; float m_tangent[3]; float m_bitangent[3]; float m_color[3]; static std::vector GetInputLayout() { std::vector layout = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(VertexColor, m_uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_normal), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_tangent), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}, { "BITANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_bitangent), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}, { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(VertexColor, m_color), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0} }; return layout; } }; //! Default Vertex struct VertexNoTangent { float m_pos[3]; float m_uv[2]; float m_normal[3]; static std::vector GetInputLayout() { std::vector layout = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(Vertex, m_uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(Vertex, m_normal), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 } }; return layout; } }; IS_PROPER_VERTEX_CLASS(Vertex) IS_PROPER_VERTEX_CLASS(VertexNoTangent) IS_PROPER_VERTEX_CLASS(Vertex2D) } /* wr */ ================================================ FILE: src/window.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "window.hpp" #include "util/log.hpp" #include "imgui/imgui.hpp" #include "imgui/imgui_impl_win32.hpp" #include namespace wr { Window::Window(HINSTANCE instance, int show_cmd, std::string const & name, std::uint32_t width, std::uint32_t height) : m_title(name), m_instance(instance) { WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = &Window::WindowProc; wc.cbClsExtra = NULL; wc.cbWndExtra = NULL; wc.hInstance = instance; wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION); wc.hCursor = LoadCursor(nullptr, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW); wc.lpszMenuName = nullptr; wc.lpszClassName = name.c_str(); wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION); m_window_width = width; m_window_height = height; if (!RegisterClassEx(&wc)) { LOGC("Failed to register extended window class: "); } auto window_style = WS_OVERLAPPEDWINDOW; if (/*!allow_resizing*/ false) { window_style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX); } RECT client_rect; client_rect.left = 0; client_rect.right = width; client_rect.top = 0; client_rect.bottom = height; AdjustWindowRectEx(&client_rect, window_style, FALSE, wc.style); m_handle = CreateWindowEx( wc.style, name.c_str(), name.c_str(), window_style, CW_USEDEFAULT, CW_USEDEFAULT, client_rect.right - client_rect.left, client_rect.bottom - client_rect.top, nullptr, nullptr, instance, nullptr ); if (!m_handle) { LOGC("Failed to create window." + GetLastError()) } SetWindowLongPtr(m_handle, GWLP_USERDATA, (LONG_PTR)this); ShowWindow(m_handle, show_cmd); UpdateWindow(m_handle); m_running = true; } Window::Window(HINSTANCE instance, std::string const& name, std::uint32_t width, std::uint32_t height, bool show) : Window(instance, show ? SW_SHOWNORMAL : SW_HIDE, name, width, height) { } Window::~Window() { Stop(); UnregisterClassA(m_title.c_str(), m_instance); } void Window::PollEvents() { MSG msg; if (PeekMessage(&msg, m_handle, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) m_running = false; TranslateMessage(&msg); DispatchMessage(&msg); } } void Window::Show() { ShowWindow(m_handle, SW_SHOW); } void Window::Stop() { DestroyWindow(m_handle); } void Window::SetRenderLoop(std::function render_func) { m_render_func = std::move(render_func); } void Window::StartRenderLoop() { while (IsRunning()) { PollEvents(); } UnregisterClassA(m_title.c_str(), m_instance); } void Window::SetKeyCallback(KeyCallback callback) { m_key_callback = std::move(callback); } void Window::SetMouseCallback(MouseCallback callback) { m_mouse_callback = std::move(callback); } void Window::SetMouseWheelCallback(MouseWheelCallback callback) { m_mouse_wheel_callback = std::move(callback); } void Window::SetResizeCallback(ResizeCallback callback) { m_resize_callback = std::move(callback); } bool Window::IsRunning() const { return m_running; } std::int32_t Window::GetWidth() const { RECT r; GetClientRect(m_handle, &r); return static_cast(r.right - r.left); } std::int32_t Window::GetHeight() const { RECT r; GetClientRect(m_handle, &r); return static_cast(r.bottom - r.top); } std::string Window::GetTitle() const { return m_title; } HWND Window::GetWindowHandle() const { return m_handle; } bool Window::IsFullscreen() const { RECT a, b; GetWindowRect(m_handle, &a); GetWindowRect(GetDesktopWindow(), &b); return (a.left == b.left && a.top == b.top && a.right == b.right && a.bottom == b.bottom); } LRESULT CALLBACK Window::WindowProc(HWND handle, UINT msg, WPARAM w_param, LPARAM l_param) { extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); if (ImGui_ImplWin32_WndProcHandler(handle, msg, w_param, l_param)) return true; auto window = (Window*)GetWindowLongPtr(handle, GWLP_USERDATA); if (window) return window->WindowProc_Impl(handle, msg, w_param, l_param); return DefWindowProc(handle, msg, w_param, l_param); } LRESULT CALLBACK Window::WindowProc_Impl(HWND handle, UINT msg, WPARAM w_param, LPARAM l_param) { switch (msg) { case WM_PAINT: if (m_render_func) { m_render_func(); } return 0; case WM_DESTROY: m_running = false; return 0; case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_RBUTTONDOWN: case WM_RBUTTONUP: if (m_mouse_callback) { m_mouse_callback((int)w_param, msg, (int)l_param); } return 0; case WM_KEYDOWN: case WM_KEYUP: if (m_key_callback) { m_key_callback((int)w_param, msg, (int)l_param); } return 0; case WM_MOUSEWHEEL: if (m_mouse_wheel_callback) { m_mouse_wheel_callback((int)w_param, msg, (int)l_param); } return 0; case WM_SIZE: if (w_param != SIZE_MINIMIZED) { if (RECT rect; m_resize_callback && GetClientRect(handle, &rect)) { int width = rect.right - rect.left; int height = rect.bottom - rect.top; bool has_changed = width != m_window_width || height != m_window_height; bool is_valid_size = width > 16 && height > 16; if (has_changed && is_valid_size) { m_resize_callback(static_cast(width), static_cast(height)); m_window_width = width; m_window_height = height; } } } return 0; } return DefWindowProc(handle, msg, w_param, l_param); } } /* wr */ ================================================ FILE: src/window.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include namespace wr { class Window { using KeyCallback = std::function; using MouseCallback = std::function; using ResizeCallback = std::function; using MouseWheelCallback = std::function; public: /*! * @param instance A handle to the current instance of the application. * @param name Window title. * @param width Initial window width. * @param height Initial window height. * @param show Controls whether the window will be shown. Default is true. */ Window(HINSTANCE instance, std::string const& name, std::uint32_t width, std::uint32_t height, bool show = true); Window(HINSTANCE instance, int show_cmd, std::string const& name, std::uint32_t width, std::uint32_t height); ~Window(); Window(const Window&) = delete; Window& operator=(const Window&) = delete; Window(Window&&) = delete; Window& operator=(Window&&) = delete; /*! Handles window events. Should be called every frame */ void PollEvents(); /*! Shows the window if it was hidden */ void Show(); /*! Requests to close the window */ void Stop(); /*! Give the window a function to call on repaint */ void SetRenderLoop(std::function render_func); /*! Start a loop that runs until the window is closed. */ void StartRenderLoop(); /*! Used to set the key callback function */ void SetKeyCallback(KeyCallback callback); /*! Used to set the mouse callback function */ void SetMouseCallback(MouseCallback callback); /*! Used to set the mouse wheel callback function */ void SetMouseWheelCallback(MouseWheelCallback callback); /*! Used to set the resize callback function */ void SetResizeCallback(ResizeCallback callback); /*! Returns whether the application is running. (used for the main loop) */ bool IsRunning() const; /* Returns the client width */ std::int32_t GetWidth() const; /* Returns the client height */ std::int32_t GetHeight() const; /* Returns the title of the window. */ std::string GetTitle() const; /*! Returns the native window handle (HWND)*/ HWND GetWindowHandle() const; /*! Checks whether the window is fullscreen */ bool IsFullscreen() const; private: /*! WindowProc that calls `WindowProc_Impl` */ static LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM); /*! Main WindowProc function */ LRESULT CALLBACK WindowProc_Impl(HWND, UINT, WPARAM, LPARAM); KeyCallback m_key_callback; MouseCallback m_mouse_callback; ResizeCallback m_resize_callback; MouseWheelCallback m_mouse_wheel_callback; std::function m_render_func; std::string m_title; bool m_running; HWND m_handle; HINSTANCE m_instance; std::int32_t m_window_width = 0; std::int32_t m_window_height = 0; }; } /* wr */ ================================================ FILE: src/wisp.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once // Core #include "entry.hpp" #include "window.hpp" #include "vertex.hpp" #include "renderer.hpp" // Scene Graph #include "scene_graph/scene_graph.hpp" #include "scene_graph/mesh_node.hpp" #include "scene_graph/camera_node.hpp" #include "scene_graph/skybox_node.hpp" // Frame Graph #include "frame_graph/frame_graph.hpp" ================================================ FILE: src/wisprenderer_export.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef WISPRENDERER_EXPORT_H #define WISPRENDERER_EXPORT_H #ifdef WISPRENDERER_STATIC_DEFINE # define WISPRENDERER_EXPORT # define WISPRENDERER_NO_EXPORT #else # ifndef WISPRENDERER_EXPORT # ifdef WispRenderer_EXPORTS /* We are building this library */ # define WISPRENDERER_EXPORT __declspec(dllexport) # else /* We are using this library */ # define WISPRENDERER_EXPORT __declspec(dllimport) # endif # endif # ifndef WISPRENDERER_NO_EXPORT # define WISPRENDERER_NO_EXPORT # endif #endif #ifndef WISPRENDERER_DEPRECATED # define WISPRENDERER_DEPRECATED __declspec(deprecated) #endif #ifndef WISPRENDERER_DEPRECATED_EXPORT # define WISPRENDERER_DEPRECATED_EXPORT WISPRENDERER_EXPORT WISPRENDERER_DEPRECATED #endif #ifndef WISPRENDERER_DEPRECATED_NO_EXPORT # define WISPRENDERER_DEPRECATED_NO_EXPORT WISPRENDERER_NO_EXPORT WISPRENDERER_DEPRECATED #endif #if 0 /* DEFINE_NO_DEPRECATED */ # ifndef WISPRENDERER_NO_DEPRECATED # define WISPRENDERER_NO_DEPRECATED # endif #endif #endif /* WISPRENDERER_EXPORT_H */ ================================================ FILE: tests/CMakeLists.txt ================================================ # 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) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. file(GLOB COMMON_SOURCES "common/*.cpp") file(GLOB COMMON_HEADERS "common/*.hpp") function(add_test TEST_DIR TEST_NAME) message(STATUS "Configuring example ${TEST_NAME} in ${TEST_DIR}") # source file(GLOB SOURCES "${TEST_DIR}/*.cpp") file(GLOB HEADERS "${TEST_DIR}/*.hpp") add_executable(${TEST_NAME} ${HEADERS} ${SOURCES} ${COMMON_HEADERS} ${COMMON_SOURCES}) target_include_directories(${TEST_NAME} PUBLIC ../src/) target_link_libraries(${TEST_NAME} WispRenderer BulletCollision BulletDynamics LinearMath) set_target_properties(${TEST_NAME} PROPERTIES CXX_STANDARD 20) set_target_properties(${TEST_NAME} PROPERTIES CXX_EXTENSIONS OFF) set_target_properties(${TEST_NAME} PROPERTIES CMAKE_CXX_STANDARD_REQUIRED ON) set_target_properties(${TEST_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/../") endfunction(add_test) function(add_example EXAMPLE_DIR EXAMPLE_NAME) message(STATUS "Configuring example ${EXAMPLE_NAME} in ${EXAMPLE_DIR}") # source file(GLOB SOURCES "${EXAMPLE_DIR}/*.cpp") file(GLOB HEADERS "${EXAMPLE_DIR}/*.hpp") add_executable(${EXAMPLE_NAME} WIN32 ${HEADERS} ${SOURCES} ${COMMON_HEADERS} ${COMMON_SOURCES}) target_include_directories(${EXAMPLE_NAME} PUBLIC ../src/) target_link_libraries(${EXAMPLE_NAME} WispRenderer BulletCollision BulletDynamics LinearMath) set_target_properties(${EXAMPLE_NAME} PROPERTIES CXX_STANDARD 20) set_target_properties(${EXAMPLE_NAME} PROPERTIES CXX_EXTENSIONS OFF) set_target_properties(${EXAMPLE_NAME} PROPERTIES CMAKE_CXX_STANDARD_REQUIRED ON) set_target_properties(${EXAMPLE_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/../") endfunction(add_example) add_test(demo Demo) add_test(graphics_benchmark GraphicsBenchmark) ================================================ FILE: tests/common/scene.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "scene.hpp" #include #include #include "json.hpp" Scene::Scene(std::size_t material_pool_size, std::size_t model_pool_vb_size, std::size_t model_pool_ib_size) : m_material_pool_size(material_pool_size), m_model_pool_vb_size(model_pool_vb_size), m_model_pool_ib_size(model_pool_ib_size) { } Scene::~Scene() { } void Scene::Init(wr::D3D12RenderSystem* rs, unsigned int width, unsigned int height, void* extra) { m_texture_pool = rs->CreateTexturePool(); m_material_pool = rs->CreateMaterialPool(m_material_pool_size); m_model_pool = rs->CreateModelPool(m_model_pool_vb_size, m_model_pool_ib_size); LoadResources(); m_scene_graph = std::make_shared(rs); BuildScene(width, height, extra); rs->InitSceneGraph(*m_scene_graph.get()); } std::shared_ptr Scene::GetSceneGraph() { return m_scene_graph; } void Scene::LoadLightsFromJSON() { if (!m_lights_path.has_value()) { LOGW("Tried to load lights from json without a path specified."); return; } // Read JSON file. std::ifstream f(m_lights_path.value()); nlohmann::json json; f >> json; // Loop over lights auto j_lights = json["lights"]; LOG("Number of lights: {}", j_lights.size() ); for (std::size_t i = 0; i < j_lights.size(); i++) { auto j_light = j_lights[i]; auto type = j_light["type"].get(); auto color = j_light["color"].get>(); auto pos = j_light["pos"].get>(); auto rot = j_light["rot"].get>(); auto size = j_light["size"].get(); auto radius = j_light["radius"].get(); auto angle = j_light["angle"].get(); auto light = m_scene_graph->CreateChild(nullptr, (wr::LightType)type); light->SetColor({ color[0], color[1], color[2] }); light->SetPosition({ pos[0], pos[1], pos[2] }); light->SetRotation({ rot[0], rot[1], rot[2] }); light->SetLightSize(size); light->SetRadius(radius); light->SetAngle(angle); } } void Scene::SaveLightsToJSON() { if (!m_lights_path.has_value()) { LOGW("Tried to save lights to json without a path specified."); return; } nlohmann::json j_lights = nlohmann::json::array(); auto lights = m_scene_graph->GetLightNodes(); for (auto& light : lights) { nlohmann::json j_light = nlohmann::json::object(); j_light["type"] = (int)light->GetType(); j_light["color"] = { light->m_light->col.x, light->m_light->col.y, light->m_light->col.z }; j_light["pos"] = light->m_position.m128_f32; j_light["rot"] = light->m_rotation_radians.m128_f32; j_light["size"] = light->m_light->light_size; j_light["radius"] = light->m_light->rad; j_light["angle"] = light->m_light->ang; j_lights.push_back(j_light); } // write JSON to file nlohmann::json json; json["lights"] = j_lights; std::ofstream o(m_lights_path.value()); o << std::setw(4) << json << std::endl; } ================================================ FILE: tests/common/scene.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include "wisp.hpp" #include "d3d12/d3d12_renderer.hpp" #include "util/user_literals.hpp" class Scene { public: Scene(std::size_t material_pool_size, std::size_t model_pool_vb_size, std::size_t model_pool_ib_size); virtual ~Scene(); virtual void Init(wr::D3D12RenderSystem* rs, unsigned int width, unsigned int height, void* extra = nullptr); virtual void Update(float delta = 0) = 0; std::shared_ptr GetSceneGraph(); template std::shared_ptr GetCamera(); void LoadLightsFromJSON(); void SaveLightsToJSON(); protected: virtual void LoadResources() = 0; virtual void BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) = 0; const std::size_t m_material_pool_size; const std::size_t m_model_pool_vb_size; const std::size_t m_model_pool_ib_size; std::shared_ptr m_scene_graph; std::shared_ptr m_model_pool; std::shared_ptr m_texture_pool; std::shared_ptr m_material_pool; std::optional m_lights_path; }; template std::shared_ptr Scene::GetCamera() { return std::static_pointer_cast(m_scene_graph->GetActiveCamera()); } ================================================ FILE: tests/demo/debug_camera.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include class DebugCamera : public wr::CameraNode { public: DebugCamera(float fov, float aspect_ratio) : 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) { GetCursorPos(&m_last_cursor_pos); m_target_rotation_radians = m_rotation_radians; m_target_position = m_position; } //Takes roll, pitch and yaw and converts it to quaternion void SetRotation(DirectX::XMVECTOR roll_pitch_yaw) override { m_rotation_radians = roll_pitch_yaw; m_use_quaternion = false; m_target_rotation_radians = roll_pitch_yaw; } //Sets position void SetPosition(DirectX::XMVECTOR position) override { m_position = position; m_target_position = position; } virtual void SetSpeed(float speed) { m_speed = speed; } virtual void Update(float delta) { POINT cursor_pos; GetCursorPos(&cursor_pos); if (m_rmb_down) { // Translation m_right_axis = std::min(m_right_axis, 1.f); m_right_axis = std::max(m_right_axis, -1.f); m_forward_axis = std::min(m_forward_axis, 1.f); m_forward_axis = std::max(m_forward_axis, -1.f); m_up_axis = std::min(m_up_axis, 1.f); m_up_axis = std::max(m_up_axis, -1.f); DirectX::XMVECTOR forward = DirectX::XMVector3Normalize(m_transform.r[2]); DirectX::XMVECTOR up = DirectX::XMVector3Normalize(m_transform.r[1]); DirectX::XMVECTOR right = DirectX::XMVector3Normalize(m_transform.r[0]); m_target_position = DirectX::XMVectorAdd(m_target_position, DirectX::XMVectorScale(forward, delta * m_speed * m_forward_axis)); m_target_position = DirectX::XMVectorAdd(m_target_position, DirectX::XMVectorScale(up, delta * m_speed * m_up_axis)); m_target_position = DirectX::XMVectorAdd(m_target_position, DirectX::XMVectorScale(right, delta * m_speed * m_right_axis)); // Rotation DirectX::XMVECTOR new_rot{ static_cast(cursor_pos.y - m_last_cursor_pos.y), static_cast(cursor_pos.x - m_last_cursor_pos.x) }; SetRotation(DirectX::XMVectorSubtract(m_target_rotation_radians, DirectX::XMVectorScale(new_rot, m_sensitivity))); } else { m_forward_axis = 0; m_right_axis = 0; m_up_axis = 0; } m_position = DirectX::XMVectorLerp(m_position, m_target_position, delta * m_position_lerp_speed); SetRotation(DirectX::XMVectorLerp(m_rotation_radians, m_target_rotation_radians, delta * m_rotation_lerp_speed)); SignalTransformChange(); m_last_cursor_pos = cursor_pos; } void MouseAction(int key, int action) { if (action == WM_RBUTTONDOWN) { m_rmb_down = true; } else if (action == WM_RBUTTONUP) { m_rmb_down = false; } } const float m_scroll_speed = 0.25f; void MouseWheel(int amount) { if (m_rmb_down) { float percent = (float) GET_WHEEL_DELTA_WPARAM(amount) / WHEEL_DELTA * m_scroll_speed; if (percent + m_speed > 0) { m_speed += percent; } } } // Due to the lack of a input manager I cheat input like this. void KeyAction(int key, int action) { if (action == WM_KEYDOWN) { if (key == 'W') { m_forward_axis += -1; } if (key == 'S') { m_forward_axis += 1; } if (key == 'A') { m_right_axis += -1; } if (key == 'D') { m_right_axis += 1; } if (key == VK_SPACE) { m_up_axis += 1; } if (key == VK_CONTROL) { m_up_axis += -1; } } else if (action == WM_KEYUP) { if (key == 'W') { m_forward_axis -= -1; } if (key == 'S') { m_forward_axis -= 1; } if (key == 'A') { m_right_axis -= -1; } if (key == 'D') { m_right_axis -= 1; } if (key == VK_SPACE) { m_up_axis -= 1; } if (key == VK_CONTROL) { m_up_axis -= -1; } } } private: float m_position_lerp_speed; float m_rotation_lerp_speed; DirectX::XMVECTOR m_target_rotation_radians; DirectX::XMVECTOR m_target_position; POINT m_last_cursor_pos; bool m_rmb_down; float m_speed; float m_sensitivity; float m_forward_axis; float m_right_axis; float m_up_axis; }; ================================================ FILE: tests/demo/demo.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include #include #include "wisp.hpp" #include "version.hpp" #include "demo_frame_graphs.hpp" #include "util/file_watcher.hpp" //Crashpad includes #include "client/crashpad_client.h" #include "client/settings.h" #include "client/crash_report_database.h" #include "engine_interface.hpp" #include "physics_engine.hpp" #include "scene_viknell.hpp" #include "scene_alien.hpp" #include "scene_emibl.hpp" #include "scene_sponza.hpp" #include "model_loader_assimp.hpp" #include "model_loader_tinygltf.hpp" #include "d3d12/d3d12_dynamic_descriptor_heap.hpp" using DefaultScene = ViknellScene; //#define ENABLE_PHYSICS std::unique_ptr render_system; Scene* current_scene = nullptr; Scene* new_scene = nullptr; void RenderEditor(ImTextureID output) { engine::RenderEngine(output, render_system.get(), current_scene, &new_scene); } void ShaderDirChangeDetected(std::string const & path, util::FileWatcher::FileStatus status) { auto& registry = wr::PipelineRegistry::Get(); auto& rt_registry = wr::RTPipelineRegistry::Get(); if (status == util::FileWatcher::FileStatus::MODIFIED) { LOG("Change detected in the shader directory. Reloading pipelines and shaders."); for (auto it : registry.m_objects) { registry.RequestReload(it.first); } for (auto it : rt_registry.m_objects) { rt_registry.RequestReload(it.first); } } } int WispEntry() { constexpr auto version = wr::GetVersion(); LOG("Wisp Version {}.{}.{}", version.m_major, version.m_minor, version.m_patch); // ImGui Logging util::log_callback::impl = [&](std::string const & str) { engine::debug_console.AddLog(str.c_str()); }; render_system = std::make_unique(); phys::PhysicsEngine phys_engine; auto window = std::make_unique(GetModuleHandleA(nullptr), "D3D12 Test App", 1280, 720); window->SetKeyCallback([](int key, int action, int mods) { current_scene->GetCamera()->KeyAction(key, action); if (action == WM_KEYUP && key == 0xC0) { engine::open_console = !engine::open_console; engine::debug_console.EmptyInput(); } if (action == WM_KEYUP && key == VK_F1) { engine::show_imgui = !engine::show_imgui; } if (action == WM_KEYUP && key == VK_F2) { fg_manager::Next(); } if (action == WM_KEYUP && key == VK_F3) { fg_manager::Prev(); } }); window->SetMouseCallback([](int key, int action, int mods) { current_scene->GetCamera()->MouseAction(key, action); }); window->SetMouseWheelCallback([](int amount, int action, int mods) { current_scene->GetCamera()->MouseWheel(amount); }); wr::ModelLoader* assimp_model_loader = new wr::AssimpModelLoader(); wr::ModelLoader* gltf_model_loader = new wr::TinyGLTFModelLoader(); render_system->Init(window.get()); phys_engine.CreatePhysicsWorld(); current_scene = new DefaultScene(); current_scene->Init(render_system.get(), window->GetWidth(), window->GetHeight(), &phys_engine); fg_manager::Setup(*render_system, &RenderEditor); window->SetResizeCallback([&](std::uint32_t width, std::uint32_t height) { render_system->WaitForAllPreviousWork(); render_system->Resize(width, height); current_scene->GetCamera()->SetAspectRatio((float)width / (float)height); current_scene->GetCamera()->SetOrthographicResolution(width, height); fg_manager::Resize(*render_system, width, height); }); auto file_watcher = new util::FileWatcher("resources/shaders", std::chrono::milliseconds(100)); file_watcher->StartAsync(&ShaderDirChangeDetected); window->SetRenderLoop([&]() { // Find delta float delta = ImGui::GetIO().DeltaTime; bool capture_frame = engine::recorder.ShouldCaptureAndIncrement(delta); if (capture_frame) { fg_manager::Get()->SaveTaskToDisc(engine::recorder.GetNextFilename(".tga"), 0); } if (new_scene && new_scene != current_scene) { render_system->WaitForAllPreviousWork(); delete current_scene; current_scene = new_scene; current_scene->Init(render_system.get(), window->GetWidth(), window->GetHeight(), &phys_engine); fg_manager::Get()->SetShouldExecute(true); fg_manager::Get()->SetShouldExecute(true); } current_scene->Update(delta); #ifdef ENABLE_PHYSICS phys_engine.UpdateSim(delta, *current_scene->GetSceneGraph()); #endif auto texture = render_system->Render(*current_scene->GetSceneGraph(), *fg_manager::Get()); }); window->StartRenderLoop(); delete assimp_model_loader; delete gltf_model_loader; render_system->WaitForAllPreviousWork(); // Make sure GPU is finished before destruction. delete current_scene; fg_manager::Destroy(); render_system.reset(); delete file_watcher; return 0; } WISP_ENTRY(WispEntry) ================================================ FILE: tests/demo/demo_frame_graphs.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "frame_graph/frame_graph.hpp" #include "settings.hpp" #include "render_tasks/d3d12_imgui_render_task.hpp" #include "render_tasks/d3d12_brdf_lut_precalculation.hpp" #include "render_tasks/d3d12_deferred_main.hpp" #include "render_tasks/d3d12_deferred_composition.hpp" #include "render_tasks/d3d12_deferred_render_target_copy.hpp" #include "render_tasks/d3d12_raytracing_task.hpp" #include "render_tasks/d3d12_rt_reflection_task.hpp" #include "render_tasks/d3d12_rt_shadow_task.hpp" #include "render_tasks/d3d12_shadow_denoiser_task.hpp" #include "render_tasks/d3d12_equirect_to_cubemap.hpp" #include "render_tasks/d3d12_cubemap_convolution.hpp" #include "render_tasks/d3d12_spatial_reconstruction.hpp" #include "render_tasks/d3d12_reflection_denoiser.hpp" #include "render_tasks/d3d12_rtao_task.hpp" #include "render_tasks/d3d12_post_processing.hpp" #include "render_tasks/d3d12_build_acceleration_structures.hpp" #include "render_tasks/d3d12_path_tracer.hpp" #include "render_tasks/d3d12_accumulation.hpp" #include "render_tasks/d3d12_dof_bokeh.hpp" #include "render_tasks/d3d12_dof_bokeh_postfilter.hpp" #include "render_tasks/d3d12_dof_coc.hpp" #include "render_tasks/d3d12_dof_compute_near_mask.hpp" #include "render_tasks/d3d12_down_scale.hpp" #include "render_tasks/d3d12_dof_composition.hpp" #include "render_tasks/d3d12_dof_dilate_near.hpp" #include "render_tasks/d3d12_hbao.hpp" #include "render_tasks/d3d12_ansel.hpp" #include "render_tasks/d3d12_bloom_extract_bright.hpp" #include "render_tasks/d3d12_bloom_composition.hpp" #include "render_tasks/d3d12_bloom_horizontal_blur.hpp" #include "render_tasks/d3d12_bloom_vertical_blur.hpp" namespace fg_manager { enum class PrebuildFrameGraph : std::uint32_t { DEFERRED = 0, RT_HYBRID = 1, RAYTRACING = 2, PATH_TRACER = 3, }; inline std::string GetFrameGraphName(PrebuildFrameGraph id) { switch (id) { case PrebuildFrameGraph::DEFERRED: return "Deferred"; case PrebuildFrameGraph::RT_HYBRID: return "Hybrid"; case PrebuildFrameGraph::RAYTRACING: return "Full Raytracing"; case PrebuildFrameGraph::PATH_TRACER: return "Path Tracer"; default: return "Unknown"; } } static PrebuildFrameGraph current = fg_manager::PrebuildFrameGraph::DEFERRED; static std::array frame_graphs = {}; inline void Setup(wr::RenderSystem& rs, util::Delegate const& imgui_func) { // Raytracing { auto& fg = frame_graphs[(int)PrebuildFrameGraph::RAYTRACING]; fg = new wr::FrameGraph(4); wr::AddBuildAccelerationStructuresTask(*fg); wr::AddEquirectToCubemapTask(*fg); wr::AddCubemapConvolutionTask(*fg); wr::AddRaytracingTask(*fg); wr::AddPostProcessingTask(*fg); // Copy the scene render pixel data to the final render target wr::AddRenderTargetCopyTask(*fg); // Display ImGui fg->AddTask(wr::GetImGuiTask(imgui_func), L"ImGui"); fg->Setup(rs); } // Deferred { auto& fg = frame_graphs[(int)PrebuildFrameGraph::DEFERRED]; fg = new wr::FrameGraph(24); wr::AddBrdfLutPrecalculationTask(*fg); wr::AddEquirectToCubemapTask(*fg); wr::AddCubemapConvolutionTask(*fg); wr::AddDeferredMainTask(*fg, std::nullopt, std::nullopt, false); wr::AddHBAOTask(*fg); wr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt); //High quality bloom pass wr::AddBloomExtractBrightTask(*fg); wr::AddBloomBlurHorizontalTask(*fg); wr::AddBloomBlurVerticalTask(*fg); wr::AddBloomCompositionTask(*fg); // Do Depth of field task wr::AddDoFCoCTask(*fg); wr::AddDownScaleTask(*fg); wr::AddDoFNearMaskTask(*fg); wr::AddDoFDilateTask(*fg); wr::AddDoFBokehTask(*fg); wr::AddDoFBokehPostFilterTask(*fg); wr::AddDoFCompositionTask(*fg); wr::AddPostProcessingTask(*fg); // Copy the scene render pixel data to the final render target wr::AddRenderTargetCopyTask(*fg); wr::AddAnselTask(*fg); // Display ImGui fg->AddTask(wr::GetImGuiTask(imgui_func), L"ImGui"); fg->Setup(rs); } // Path Tracer { auto& fg = frame_graphs[(int)PrebuildFrameGraph::PATH_TRACER]; fg = new wr::FrameGraph(18); // Precalculate BRDF Lut wr::AddBrdfLutPrecalculationTask(*fg); wr::AddEquirectToCubemapTask(*fg); wr::AddCubemapConvolutionTask(*fg); // Construct the G-buffer wr::AddDeferredMainTask(*fg, std::nullopt, std::nullopt, true); wr::AddHBAOTask(*fg); // Build Acceleration Structure wr::AddBuildAccelerationStructuresTask(*fg); // Raytracing task wr::AddRTReflectionTask(*fg); wr::AddRTShadowTask(*fg); wr::AddShadowDenoiserTask(*fg); wr::AddSpatialReconstructionTask(*fg); wr::AddReflectionDenoiserTask(*fg); // Global Illumination Path Tracing wr::AddPathTracerTask(*fg); wr::AddAccumulationTask(*fg); wr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt); // Do some post processing wr::AddPostProcessingTask(*fg); // Copy the raytracing pixel data to the final render target wr::AddRenderTargetCopyTask(*fg); wr::AddAnselTask(*fg); // Display ImGui fg->AddTask(wr::GetImGuiTask(imgui_func), L"ImGui"); // Finalize the frame graph fg->Setup(rs); } // Hybrid raytracing { auto& fg = frame_graphs[(int) PrebuildFrameGraph::RT_HYBRID]; fg = new wr::FrameGraph(31); // Precalculate BRDF Lut wr::AddBrdfLutPrecalculationTask(*fg); wr::AddEquirectToCubemapTask(*fg); wr::AddCubemapConvolutionTask(*fg); // Construct the G-buffer wr::AddDeferredMainTask(*fg, std::nullopt, std::nullopt, true); // Build Acceleration Structure wr::AddBuildAccelerationStructuresTask(*fg); // Raytracing task wr::AddRTReflectionTask(*fg); wr::AddRTShadowTask(*fg); wr::AddShadowDenoiserTask(*fg); wr::AddSpatialReconstructionTask(*fg); wr::AddReflectionDenoiserTask(*fg); //Raytraced Ambient Occlusion task wr::AddRTAOTask(*fg, static_cast(rs).m_device); wr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt); //High quality bloom pass wr::AddBloomExtractBrightTask(*fg); wr::AddBloomBlurHorizontalTask(*fg); wr::AddBloomBlurVerticalTask(*fg); wr::AddBloomCompositionTask(*fg); // Do Depth of field task wr::AddDoFCoCTask(*fg); wr::AddDownScaleTask(*fg); wr::AddDoFNearMaskTask(*fg); wr::AddDoFDilateTask(*fg); wr::AddDoFBokehTask(*fg); wr::AddDoFBokehPostFilterTask(*fg); wr::AddDoFCompositionTask(*fg); wr::AddPostProcessingTask(*fg); // Copy the scene render pixel data to the final render target wr::AddRenderTargetCopyTask(*fg); wr::AddAnselTask(*fg); // Display ImGui fg->AddTask(wr::GetImGuiTask(imgui_func), L"ImGui"); // Finalize the frame graph fg->Setup(rs); } } void Resize(wr::RenderSystem& render_system, std::uint32_t width, std::uint32_t height) { for (int i = 0; i < frame_graphs.size(); ++i) { frame_graphs[i]->Resize(width, height); } } inline wr::FrameGraph* Get() { return frame_graphs[(int)current]; } inline void Next() { current = (PrebuildFrameGraph)((static_cast(current) + 1ull) % frame_graphs.size()); } inline void Prev() { current = (PrebuildFrameGraph)((static_cast(current) - 1ull) % frame_graphs.size()); } inline void Set(PrebuildFrameGraph value) { current = value; } inline void Destroy() { for (auto& fg : frame_graphs) { delete fg; } } } ================================================ FILE: tests/demo/engine_interface.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include "wisp.hpp" #include "window.hpp" #include "imgui_tools.hpp" #include "scene_graph/scene_graph.hpp" #include "d3d12/d3d12_renderer.hpp" #include "imgui/ImGuizmo.h" #include "demo_frame_graphs.hpp" #include "../common/scene.hpp" #include "scene_emibl.hpp" #include "scene_viknell.hpp" #include "scene_sponza.hpp" #include "scene_alien.hpp" #include "imgui_graphics_settings.hpp" namespace engine { static bool main_menu = true; static bool keep_aspect_ratio = false; static float custom_aspect_ratio = 1280.f/720.f; static bool open0 = true; static bool open_viewport = true; static bool open1 = true; static bool open_console = false; static bool open_scene = true; static bool open_recorder = true; static char recorder_name[256] = "unamed"; static char recorder_base_dir[256] = "D:\\WispRecorder\\"; static int selected_scene = 0; static bool show_imgui = true; static bool fullscreen = false; static wr::imgui::window::Stats stats_window { false }; static char message_buffer[600]; static wr::imgui::special::DebugConsole debug_console; struct Recorder { bool m_recording = false; int m_target_framerate = 30; int m_record_frame_inverval = 1; int m_frames_since_last_capture = 0; int m_frames_recorded = 0; std::string m_output_dir; std::string m_name; void Start(std::string name, std::string base_output_dir = "D:\\WispRecorder\\") { m_frames_recorded = 0; m_frames_since_last_capture = 0; m_output_dir = base_output_dir + name; m_name = name; std::filesystem::create_directory(m_output_dir); show_imgui = false; m_recording = true; } void Stop() { m_recording = false; } std::string GetNextFilename(std::string ext) { m_frames_recorded++; return m_output_dir + "\\" + m_name + "_"+ std::to_string(m_target_framerate) + "fps_frame" + std::to_string(m_frames_recorded) + ext; } bool ShouldCaptureAndIncrement(float& out_delta) { if (!m_recording) return false; bool retval = false; if (m_frames_since_last_capture == m_record_frame_inverval) { retval = true; m_frames_since_last_capture = 0; out_delta = 1.f / (float)m_target_framerate;; } else { m_frames_since_last_capture++; out_delta = 0; } return retval; } bool IsRecording() { return m_recording; } }; static Recorder recorder; void RenderEngine(ImTextureID output, wr::D3D12RenderSystem* render_system, Scene* scene, Scene** new_scene) { auto sg = scene->GetSceneGraph(); ImVec2 viewport_pos(0, 0); ImVec2 viewport_size(0, 0); debug_console.AddCommand("stats", [](wr::imgui::special::DebugConsole& console, std::string const&) { stats_window.m_open = !stats_window.m_open; console.AddLog("Toggled the stats window."); }, "Toggle the statistics window"); debug_console.Draw("Console", &open_console); if (!show_imgui) { stats_window.Draw(*render_system, viewport_pos); sg->GetActiveCamera()->SetAspectRatio(render_system->m_viewport.m_viewport.Width / render_system->m_viewport.m_viewport.Height); } else { if (main_menu && ImGui::BeginMainMenuBar()) { if (ImGui::BeginMenu("File")) { if (ImGui::MenuItem("Exit", "ALT+F4")) std::exit(0); if (ImGui::MenuItem("Hide ImGui", "F1")) show_imgui = false; ImGui::EndMenu(); } if (ImGui::BeginMenu("Window")) { ImGui::MenuItem("Viewport", nullptr, &open_viewport); ImGui::MenuItem("Scene Graph Editor", nullptr, &wr::imgui::window::open_scene_graph_editor); ImGui::MenuItem("Inspector", nullptr, &wr::imgui::window::open_inspector); ImGui::MenuItem("Hardware Info", nullptr, &wr::imgui::window::open_hardware_info); ImGui::MenuItem("DirectX 12 Settings", nullptr, &wr::imgui::window::open_d3d12_settings); ImGui::Separator(); wr::imgui::menu::Registries(); ImGui::Separator(); ImGui::MenuItem("Theme", nullptr, &open0); ImGui::MenuItem("Statistics", nullptr, &stats_window.m_open); ImGui::MenuItem("Camera Settings", nullptr, &open1); wr::imgui::menu::GraphicsSettingsMenu(fg_manager::Get()); ImGui::EndMenu(); } ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.f, 1.f, 1.f, 0.4f)); ImGui::Text("Current Frame Graph: %s", fg_manager::GetFrameGraphName(fg_manager::current).c_str()); ImGui::PopStyleColor(); ImGui::EndMainMenuBar(); } ImGui::DockSpaceOverViewport(main_menu, ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode); auto& io = ImGui::GetIO(); if (open_viewport) { ImGui::Begin("Viewport"); ImGui::Checkbox("Stats", &stats_window.m_open); ImGui::SameLine(); ImGui::SetNextItemWidth(80); ImGui::Checkbox("Force Aspect Ratio", &keep_aspect_ratio); ImGui::SameLine(); ImGui::InputFloat("##", &custom_aspect_ratio); viewport_pos = ImGui::GetCursorScreenPos(); viewport_size = ImGui::GetContentRegionAvail(); if (!keep_aspect_ratio && viewport_size.x > 10 && viewport_size.y > 10) { sg->GetActiveCamera()->SetAspectRatio(viewport_size.x / viewport_size.y); } else { sg->GetActiveCamera()->SetAspectRatio(custom_aspect_ratio); } ImGui::Image(output, viewport_size); ImGui::End(); } if (open_scene) { ImGui::Begin("Demo Scene", &open_scene); if (ImGui::Button("Save Lights")) { scene->SaveLightsToJSON(); } ImGui::Separator(); const char* items[] = { "Viknell", "Emibl", "Sponza", "Alien" }; ImGui::Combo("##", &selected_scene, items, IM_ARRAYSIZE(items)); ImGui::SameLine(); if (ImGui::Button("Load")) { switch (selected_scene) { case 0: (*new_scene) = new ViknellScene(); break; case 1: (*new_scene) = new EmiblScene(); break; case 2: (*new_scene) = new SponzaScene(); break; case 3: (*new_scene) = new AlienScene(); break; default: LOGW("Tried to load a scene that is not supported"); break; } } ImGui::End(); } if (open_recorder) { recorder.Stop(); // don't record while in imgui. ImGui::Begin("Recorder", &open_recorder); if (ImGui::Button("Record")) { recorder.Start(recorder_name, recorder_base_dir); } ImGui::InputText("Recording Name", recorder_name, IM_ARRAYSIZE(recorder_name)); ImGui::InputText("Base Output Dir", recorder_base_dir, IM_ARRAYSIZE(recorder_base_dir)); ImGui::InputInt("Target Framerate", &recorder.m_target_framerate); ImGui::InputInt("Frame Interval", &recorder.m_record_frame_inverval); ImGui::End(); } if (open0) { ImGui::Begin("Theme", &open0); if (ImGui::Button("Cherry")) ImGui::StyleColorsCherry(); if (ImGui::Button("Unreal Engine")) ImGui::StyleColorsUE(); if (ImGui::Button("Light Green")) ImGui::StyleColorsLightGreen(); if (ImGui::Button("Light")) ImGui::StyleColorsLight(); if (ImGui::Button("Dark")) ImGui::StyleColorsDark(); if (ImGui::Button("Dark2")) ImGui::StyleColorsDarkCodz1(); if (ImGui::Button("Corporate Grey")) ImGui::StyleCorporateGrey(); ImGui::End(); } if (open1) { ImGui::Begin("Camera Settings", &open1); auto pos = sg->GetActiveCamera()->m_position; ImGui::DragFloat3("Position", pos.m128_f32, 0.5f); float rot[3] = { DirectX::XMConvertToDegrees(DirectX::XMVectorGetX(sg->GetActiveCamera()->m_rotation_radians)), DirectX::XMConvertToDegrees(DirectX::XMVectorGetY(sg->GetActiveCamera()->m_rotation_radians)), DirectX::XMConvertToDegrees(DirectX::XMVectorGetZ(sg->GetActiveCamera()->m_rotation_radians)) }; ImGui::DragFloat3("Rotation", rot, 0.01f); if (!ImGui::IsMouseDown(1)) { sg->GetActiveCamera()->SetPosition(pos); sg->GetActiveCamera()->SetRotation(DirectX::XMVectorSet(DirectX::XMConvertToRadians(rot[0]), DirectX::XMConvertToRadians(rot[1]), DirectX::XMConvertToRadians(rot[2]), 0)); sg->GetActiveCamera()->SignalTransformChange(); sg->GetActiveCamera()->SignalChange(); } ImGui::Separator(); float frustum_near = sg->GetActiveCamera()->m_frustum_near; float frustum_far = sg->GetActiveCamera()->m_frustum_far; ImGui::DragFloatRange2("Frustum Near/Far", &frustum_near, &frustum_far, 1, 0.0001f, std::numeric_limits::max()); sg->GetActiveCamera()->SetFrustumNear(std::max(frustum_near, 0.0000001f)); sg->GetActiveCamera()->SetFrustumFar(std::max(frustum_far, 0.0000001f)); ImGui::Separator(); ImGui::MenuItem("Enable DOF", nullptr, &sg->GetActiveCamera()->m_enable_dof); ImGui::MenuItem("Enable Orthographic view", nullptr, &sg->GetActiveCamera()->m_enable_orthographic); ImGui::DragFloat("F number", &sg->GetActiveCamera()->m_f_number, 1.f, 1.f, 128.f); ImGui::DragFloat("Film size", &sg->GetActiveCamera()->m_film_size, 1.f, 25.f, 100.f); ImGui::DragFloat("Bokeh Shape amount", &sg->GetActiveCamera()->m_shape_amt, 0.005f, 0.f, 1.f); ImGui::DragInt("Aperture blades", &sg->GetActiveCamera()->m_aperture_blades, 1, 3, 7); ImGui::DragFloat("Focal Length", &sg->GetActiveCamera()->m_focal_length, 1.f, 1.f, 300.f); ImGui::DragFloat("Focal plane distance", &sg->GetActiveCamera()->m_focus_dist, 1.f, 0.f, 10000.f); ImGui::DragFloat("DoF Range multiplier", &sg->GetActiveCamera()->m_dof_range, 0.05f, 0.f, 10000.f); sg->GetActiveCamera()->SetFovFromFocalLength(sg->GetActiveCamera()->m_aspect_ratio, sg->GetActiveCamera()->m_film_size); ImGui::End(); } stats_window.Draw(*render_system, viewport_pos); wr::imgui::window::SceneGraphEditor(sg.get()); wr::imgui::window::Inspector(sg.get(), viewport_pos, viewport_size); wr::imgui::window::ShaderRegistry(); wr::imgui::window::PipelineRegistry(); wr::imgui::window::RootSignatureRegistry(); wr::imgui::window::D3D12HardwareInfo(*render_system); wr::imgui::window::D3D12Settings(); wr::imgui::window::GraphicsSettings(fg_manager::Get()); } } } ================================================ FILE: tests/demo/physics_engine.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "physics_engine.hpp" #include "physics_node.hpp" #include "debug_camera.hpp" #include "model_pool.hpp" namespace phys { void PhysicsEngine::CreatePhysicsWorld() { ///collision configuration contains default setup for memory, collision setup collision_config = new btDefaultCollisionConfiguration(); //m_collisionConfiguration->setConvexConvexMultipointIterations(); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) coll_dispatcher = new btCollisionDispatcher(collision_config); broadphase = new btDbvtBroadphase(); ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) constraint_solver = new btSequentialImpulseConstraintSolver(); phys_world = new btDiscreteDynamicsWorld(coll_dispatcher, broadphase, constraint_solver, collision_config); phys_world->setGravity(btVector3(0, -9.8, 0)); } btSphereShape* PhysicsEngine::CreateSphereShape(const float radius) { auto shape = new btSphereShape(radius); collision_shapes.push_back(shape); return shape; } btCapsuleShape* PhysicsEngine::CreateCapsuleShape(const float width, const float height) { auto shape = new btCapsuleShape(width, height); collision_shapes.push_back(shape); return shape; } std::vector PhysicsEngine::CreateConvexShape(wr::ModelData* model) { std::vector hulls; for (auto& mesh_data : model->m_meshes) { btConvexHullShape* shape = new btConvexHullShape(); for (auto& idx : mesh_data->m_indices) { auto pos = mesh_data->m_positions[idx]; shape->addPoint(btVector3(pos.x, pos.y, pos.z), false); } shape->recalcLocalAabb(); collision_shapes.push_back(shape); hulls.push_back(shape); } return hulls; } std::vector PhysicsEngine::CreateTriangleMeshShape(wr::ModelData* model) { std::vector hulls; for (auto& mesh_data : model->m_meshes) { btTriangleIndexVertexArray* va = new btTriangleIndexVertexArray(mesh_data->m_indices.size() / 3, reinterpret_cast(mesh_data->m_indices.data()), 3 * sizeof(std::uint32_t), mesh_data->m_positions.size(), reinterpret_cast(mesh_data->m_positions.data()), sizeof(DirectX::XMFLOAT3)); btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(va, true); collision_shapes.push_back(shape); hulls.push_back(shape); } return hulls; } btBoxShape* PhysicsEngine::CreateBoxShape(const btVector3& halfExtents) { auto shape = new btBoxShape(halfExtents); collision_shapes.push_back(shape); return shape; } btRigidBody* PhysicsEngine::CreateRigidBody(float mass, const btTransform& startTransform, btCollisionShape* shape) { btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE)); //rigidbody is dynamic if and only if mass is non zero, otherwise static bool is_dynamic = (mass != 0.f); btVector3 local_inertia(0, 0, 0); if (is_dynamic) shape->calculateLocalInertia(mass, local_inertia); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects #ifdef USE_MOTIONSTATE btDefaultMotionState* motion_state = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo cInfo(mass, motion_state, shape, local_inertia); btRigidBody* body = new btRigidBody(cInfo); //body->setContactProcessingThreshold(m_defaultContactProcessingThreshold); #else btRigidBody* body = new btRigidBody(mass, 0, shape, localInertia); body->setWorldTransform(startTransform); #endif body->setUserIndex(-1); phys_world->addRigidBody(body); return body; } void PhysicsEngine::UpdateSim(float delta, wr::SceneGraph& sg) { phys_world->stepSimulation(delta); for (auto& n : sg.GetMeshNodes()) { if (auto & node = std::dynamic_pointer_cast(n)) { if (!node->m_rigid_bodies.has_value() && node->m_rigid_body) { auto world_position = node->m_rigid_body->getWorldTransform().getOrigin(); node->m_position = util::BV3toDXV3(world_position); node->SignalTransformChange(); } } } } PhysicsEngine::~PhysicsEngine() { delete phys_world; delete broadphase; delete coll_dispatcher; delete constraint_solver; delete collision_config; } } /* phys*/ ================================================ FILE: tests/demo/physics_engine.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "wisp.hpp" #include #include #include #include #include #define USE_MOTIONSTATE 1 namespace wr { struct Model; } namespace phys { namespace util { inline btVector3 DXV3toBV3(DirectX::XMVECTOR v) { return btVector3(v.m128_f32[0], v.m128_f32[1], v.m128_f32[2]); } inline DirectX::XMVECTOR BV3toDXV3(btVector3 v) { return { v.x(), v.y(), v.z() }; } } struct PhysicsEngine { btAlignedObjectArray collision_shapes; btBroadphaseInterface* broadphase; btCollisionDispatcher* coll_dispatcher; btConstraintSolver* constraint_solver; btDefaultCollisionConfiguration* collision_config; btDiscreteDynamicsWorld* phys_world; void CreatePhysicsWorld(); btBoxShape* CreateBoxShape(const btVector3& halfExtents); btSphereShape* CreateSphereShape(const float radius); btCapsuleShape* CreateCapsuleShape(const float width, const float height); std::vector CreateConvexShape(wr::ModelData* model); std::vector CreateTriangleMeshShape(wr::ModelData* model); btRigidBody* CreateRigidBody(float mass, const btTransform& startTransform, btCollisionShape* shape); void UpdateSim(float delta, wr::SceneGraph& sg); ~PhysicsEngine(); }; } /* phys*/ ================================================ FILE: tests/demo/physics_node.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "physics_node.hpp" #include "physics_engine.hpp" PhysicsMeshNode::~PhysicsMeshNode() { if(m_shape) { delete m_shape; } if(m_rigid_body) { delete m_rigid_body->getMotionState(); m_phys_engine.phys_world->removeRigidBody(m_rigid_body); delete m_rigid_body; } if(m_shapes.has_value()) { for(auto* shape : m_shapes.value()) { delete shape; } } if(m_rigid_bodies.has_value()) { for(auto* rigid_body : m_rigid_bodies.value()) { delete rigid_body->getMotionState(); m_phys_engine.phys_world->removeRigidBody(rigid_body); delete rigid_body; } } } void PhysicsMeshNode::SetMass(float mass) { m_mass = mass; if (m_rigid_body) { btVector3 local_intertia(0, 0, 0); m_shape->calculateLocalInertia(mass, local_intertia); m_rigid_body->setMassProps(mass, local_intertia); } } void PhysicsMeshNode::SetRestitution(float value) { if (m_rigid_bodies.has_value()) { for (auto& body : m_rigid_bodies.value()) { body->setRestitution(value); } } else { m_rigid_body->setRestitution(value); } } void PhysicsMeshNode::SetupSimpleBoxColl(phys::PhysicsEngine& phys_engine, DirectX::XMVECTOR scale) { btTransform transform; transform.setIdentity(); m_shape = phys_engine.CreateBoxShape(phys::util::DXV3toBV3(scale)); m_rigid_body = phys_engine.CreateRigidBody(btScalar(m_mass), transform, m_shape); } void PhysicsMeshNode::SetupSimpleSphereColl(phys::PhysicsEngine& phys_engine, float radius) { btTransform transform; transform.setIdentity(); m_shape = phys_engine.CreateSphereShape(radius); m_rigid_body = phys_engine.CreateRigidBody(btScalar(m_mass), transform, m_shape); } void PhysicsMeshNode::SetupConvex(phys::PhysicsEngine& phys_engine, wr::ModelData* model) { m_rigid_bodies = std::vector(); m_shapes = std::vector(); auto hulls = phys_engine.CreateConvexShape(model); for (auto& hull : hulls) { m_shapes->push_back(hull); btTransform transform; transform.setIdentity(); auto body = phys_engine.CreateRigidBody(0.f, transform, hull); m_rigid_bodies->push_back(body); } } void PhysicsMeshNode::SetupTriangleMesh(phys::PhysicsEngine& phys_engine, wr::ModelData* model) { m_rigid_bodies = std::vector(); m_shapes = std::vector(); auto hulls = phys_engine.CreateTriangleMeshShape(model); for (auto& hull : hulls) { m_shapes->push_back(hull); btTransform transform; transform.setIdentity(); auto body = phys_engine.CreateRigidBody(0.f, transform, hull); m_rigid_bodies->push_back(body); } } void PhysicsMeshNode::SetPosition(DirectX::XMVECTOR position) { if (m_rigid_bodies.has_value()) { for (auto& body : m_rigid_bodies.value()) { auto& world_trans = body->getWorldTransform(); world_trans.setOrigin(phys::util::DXV3toBV3(position)); } } else if (m_rigid_body) { auto& world_trans = m_rigid_body->getWorldTransform(); world_trans.setOrigin(phys::util::DXV3toBV3(position)); } m_position = position; SignalTransformChange(); } void PhysicsMeshNode::SetRotation(DirectX::XMVECTOR roll_pitch_yaw) { auto quat = DirectX::XMQuaternionRotationRollPitchYawFromVector(roll_pitch_yaw); if (m_rigid_bodies.has_value()) { for (auto& body : m_rigid_bodies.value()) { auto& world_trans = body->getWorldTransform(); world_trans.setRotation(btQuaternion(quat.m128_f32[0], quat.m128_f32[1], quat.m128_f32[2], quat.m128_f32[3])); } } else if (m_rigid_body) { auto& world_trans = m_rigid_body->getWorldTransform(); world_trans.setRotation(btQuaternion(quat.m128_f32[0], quat.m128_f32[1], quat.m128_f32[2], quat.m128_f32[3])); } m_rotation_radians = roll_pitch_yaw; SignalTransformChange(); } void PhysicsMeshNode::SetScale(DirectX::XMVECTOR scale) { if (m_rigid_bodies.has_value()) { for (auto& shape : m_shapes.value()) { shape->setLocalScaling(phys::util::DXV3toBV3(scale)); } } else if (m_shape) { m_shape->setLocalScaling(phys::util::DXV3toBV3(scale)); } m_scale = scale; SignalTransformChange(); } ================================================ FILE: tests/demo/physics_node.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include "scene_graph/mesh_node.hpp" namespace phys { struct PhysicsEngine; } struct PhysicsMeshNode : public wr::MeshNode { btCollisionShape* m_shape = nullptr; btRigidBody* m_rigid_body = nullptr; std::optional> m_shapes; std::optional> m_rigid_bodies; phys::PhysicsEngine& m_phys_engine; float m_mass = 0; PhysicsMeshNode(phys::PhysicsEngine* phys_engine, wr::Model* model) : MeshNode(model), m_phys_engine(*phys_engine) { } ~PhysicsMeshNode(); void SetMass(float mass); void SetRestitution(float value); void SetupSimpleBoxColl(phys::PhysicsEngine& phys_engine, DirectX::XMVECTOR scale); void SetupSimpleSphereColl(phys::PhysicsEngine& phys_engine, float radius); void SetupConvex(phys::PhysicsEngine& phys_engine, wr::ModelData* model); void SetupTriangleMesh(phys::PhysicsEngine& phys_engine, wr::ModelData* model); void SetPosition(DirectX::XMVECTOR position) override; void SetRotation(DirectX::XMVECTOR roll_pitch_yaw) override; void SetScale(DirectX::XMVECTOR scale) override; }; ================================================ FILE: tests/demo/scene_alien.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "scene_alien.hpp" AlienScene::AlienScene() : Scene(256, 20_mb, 20_mb), m_alien_model(nullptr), m_skybox({}) { m_lights_path = "resources/alien_lights.json"; } void AlienScene::LoadResources() { // Models m_alien_model = m_model_pool->LoadWithMaterials(m_material_pool.get(), m_texture_pool.get(), "resources/models/alien/scene.gltf"); // Textures m_skybox = m_texture_pool->LoadFromFile("resources/materials/Ice_Lake_Ref.hdr", false, false); } void AlienScene::BuildScene(unsigned int width, unsigned int height, void* extra) { m_camera = m_scene_graph->CreateChild(nullptr, 90.f, (float)width / (float)height); m_camera->SetPosition({ 1.243, 1.025, -0.640 }); m_camera->SetRotation({ 1.719_deg, 117.456_deg, 0 }); m_camera->SetSpeed(5); m_camera_spline_node = m_scene_graph->CreateChild(nullptr, "Camera Spline", false); auto skybox = m_scene_graph->CreateChild(nullptr, m_skybox); // Geometry auto alien = m_scene_graph->CreateChild(nullptr, m_alien_model); alien->SetPosition({ 0, -1, 0 }); alien->SetRotation({ 0, 90_deg, 0 }); alien->SetScale({ 0.01f,0.01f,0.01f }); // Lights LoadLightsFromJSON(); } void AlienScene::Update(float delta) { m_camera->Update(delta); m_camera_spline_node->UpdateSplineNode(delta, m_camera); } ================================================ FILE: tests/demo/scene_alien.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "wisp.hpp" #include "window.hpp" #include "scene_graph/scene_graph.hpp" #include "imgui/imgui.hpp" #include "physics_node.hpp" #include "debug_camera.hpp" #include "spline_node.hpp" #include "../common/scene.hpp" class AlienScene : public Scene { public: AlienScene(); void Update(float delta = 0) final; protected: void LoadResources() final; void BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) final; private: // Models wr::Model* m_alien_model; // Textures wr::TextureHandle m_skybox; // Nodes std::shared_ptr m_camera; std::shared_ptr m_camera_spline_node; }; ================================================ FILE: tests/demo/scene_emibl.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "scene_emibl.hpp" EmiblScene::EmiblScene() : Scene(256, 2_mb, 2_mb), m_cube_model(nullptr), m_plane_model(nullptr), m_material_knob_model(nullptr), m_rusty_metal_material(), m_knob_material(), m_skybox({}) { } EmiblScene::~EmiblScene() { if (m_plane_model) { m_model_pool->Destroy(m_plane_model); m_model_pool->Destroy(m_material_knob_model); m_model_pool->Destroy(m_cube_model); } for (wr::MaterialHandle m : m_material_handles) { m_material_pool->DestroyMaterial(m); } m_material_pool->DestroyMaterial(m_rusty_metal_material); } void EmiblScene::LoadResources() { // Textures wr::TextureHandle metal_splotchy_albedo = m_texture_pool->LoadFromFile("resources/materials/metal-splotchy-albedo.png", true, true); wr::TextureHandle metal_splotchy_normal = m_texture_pool->LoadFromFile("resources/materials/metal-splotchy-normal-dx.png", false, true); wr::TextureHandle metal_splotchy_roughness = m_texture_pool->LoadFromFile("resources/materials/metal-splotchy-rough.png", false, true); wr::TextureHandle metal_splotchy_metallic = m_texture_pool->LoadFromFile("resources/materials/metal-splotchy-metal.png", false, true); wr::TextureHandle mahogfloor_albedo = m_texture_pool->LoadFromFile("resources/materials/harsh_bricks/albedo.png", true, true); wr::TextureHandle mahogfloor_normal = m_texture_pool->LoadFromFile("resources/materials/harsh_bricks/normal.png", false, true); wr::TextureHandle mahogfloor_roughness = m_texture_pool->LoadFromFile("resources/materials/harsh_bricks/roughness.png", false, true); wr::TextureHandle mahogfloor_metallic = m_texture_pool->LoadFromFile("resources/materials/harsh_bricks/metallic.png", false, true); wr::TextureHandle mahogfloor_ao = m_texture_pool->LoadFromFile("resources/materials/harsh_bricks/ao.png", false, true); wr::TextureHandle red_black_albedo = m_texture_pool->LoadFromFile("resources/materials/dragon_egg/albedo.png", true, true); wr::TextureHandle red_black_normal = m_texture_pool->LoadFromFile("resources/materials/dragon_egg/normal.png", false, true); wr::TextureHandle red_black_roughness = m_texture_pool->LoadFromFile("resources/materials/dragon_egg/roughness.png", false, true); wr::TextureHandle red_black_metallic = m_texture_pool->LoadFromFile("resources/materials/dragon_egg/metallic.png", false, true); wr::TextureHandle metal_albedo = m_texture_pool->LoadFromFile("resources/materials/greasy_pan/albedo.png", true, true); wr::TextureHandle metal_normal = m_texture_pool->LoadFromFile("resources/materials/greasy_pan/normal.png", false, true); wr::TextureHandle metal_roughness = m_texture_pool->LoadFromFile("resources/materials/greasy_pan/roughness.png", false, true); wr::TextureHandle metal_metallic = m_texture_pool->LoadFromFile("resources/materials/greasy_pan/metallic.png", false, true); wr::TextureHandle brick_tiles_albedo = m_texture_pool->LoadFromFile("resources/materials/brick_tiles/albedo.png", true, true); wr::TextureHandle brick_tiles_normal = m_texture_pool->LoadFromFile("resources/materials/brick_tiles/normal.png", false, true); wr::TextureHandle brick_tiles_roughness = m_texture_pool->LoadFromFile("resources/materials/brick_tiles/roughness.png", false, true); wr::TextureHandle brick_tiles_metallic = m_texture_pool->LoadFromFile("resources/materials/brick_tiles/metallic.png", false, true); wr::TextureHandle leather_albedo = m_texture_pool->LoadFromFile("resources/materials/leather_with_metal/albedo.png", true, true); wr::TextureHandle leather_normal = m_texture_pool->LoadFromFile("resources/materials/leather_with_metal/normal.png", false, true); wr::TextureHandle leather_roughness = m_texture_pool->LoadFromFile("resources/materials/leather_with_metal/roughness.png", false, true); wr::TextureHandle leather_metallic = m_texture_pool->LoadFromFile("resources/materials/leather_with_metal/metallic.png", false, true); wr::TextureHandle blue_tiles_albedo = m_texture_pool->LoadFromFile("resources/materials/blue_tiles/albedo.png", true, true); wr::TextureHandle blue_tiles_normal = m_texture_pool->LoadFromFile("resources/materials/blue_tiles/normal.png", false, true); wr::TextureHandle blue_tiles_roughness = m_texture_pool->LoadFromFile("resources/materials/blue_tiles/roughness.png", false, true); wr::TextureHandle blue_tiles_metallic = m_texture_pool->LoadFromFile("resources/materials/blue_tiles/metallic.png", false, true); wr::TextureHandle gold_albedo = m_texture_pool->LoadFromFile("resources/materials/gold_scuffed/albedo.png", true, true); wr::TextureHandle gold_normal = m_texture_pool->LoadFromFile("resources/materials/gold_scuffed/normal.png", false, true); wr::TextureHandle gold_roughness = m_texture_pool->LoadFromFile("resources/materials/gold_scuffed/roughness.png", false, true); wr::TextureHandle gold_metallic = m_texture_pool->LoadFromFile("resources/materials/gold_scuffed/metallic.png", false, true); wr::TextureHandle marble_albedo = m_texture_pool->LoadFromFile("resources/materials/marble_speckled/albedo.png", true, true); wr::TextureHandle marble_normal = m_texture_pool->LoadFromFile("resources/materials/marble_speckled/normal.png", false, true); wr::TextureHandle marble_roughness = m_texture_pool->LoadFromFile("resources/materials/marble_speckled/roughness.png", false, true); wr::TextureHandle marble_metallic = m_texture_pool->LoadFromFile("resources/materials/marble_speckled/metallic.png", false, true); wr::TextureHandle floreal_tiles_albedo = m_texture_pool->LoadFromFile("resources/materials/floreal_tiles/albedo.png", true, true); wr::TextureHandle floreal_tiles_normal = m_texture_pool->LoadFromFile("resources/materials/floreal_tiles/normal.png", false, true); wr::TextureHandle floreal_tiles_roughness = m_texture_pool->LoadFromFile("resources/materials/floreal_tiles/roughness.png", false, true); wr::TextureHandle floreal_tiles_metallic = m_texture_pool->LoadFromFile("resources/materials/floreal_tiles/metallic.png", false, true); wr::TextureHandle bw_tiles_albedo = m_texture_pool->LoadFromFile("resources/materials/bw_tiles_gold_lining/albedo.png", true, true); wr::TextureHandle bw_tiles_normal = m_texture_pool->LoadFromFile("resources/materials/bw_tiles_gold_lining/normal.png", false, true); wr::TextureHandle bw_tiles_roughness = m_texture_pool->LoadFromFile("resources/materials/bw_tiles_gold_lining/roughness.png", false, true); wr::TextureHandle bw_tiles_metallic = m_texture_pool->LoadFromFile("resources/materials/bw_tiles_gold_lining/metallic.png", false, true); wr::TextureHandle bw_tiles_emissive = m_texture_pool->LoadFromFile("resources/materials/bw_tiles_gold_lining/emissive.png", true, true); m_skybox = m_texture_pool->LoadFromFile("resources/materials/Arches_E_PineTree_3k.hdr", false, false); { // Create Material m_rusty_metal_material = m_material_pool->Create(m_texture_pool.get()); wr::Material* rusty_metal_internal = m_material_pool->GetMaterial(m_rusty_metal_material); rusty_metal_internal->SetTexture(wr::TextureType::ALBEDO, metal_splotchy_albedo); rusty_metal_internal->SetTexture(wr::TextureType::NORMAL, metal_splotchy_normal); rusty_metal_internal->SetTexture(wr::TextureType::ROUGHNESS, metal_splotchy_roughness); rusty_metal_internal->SetTexture(wr::TextureType::METALLIC, metal_splotchy_metallic); // Create Material m_material_handles[0] = m_material_pool->Create(m_texture_pool.get()); wr::Material* mahogfloor_material_internal = m_material_pool->GetMaterial(m_material_handles[0]); mahogfloor_material_internal->SetTexture(wr::TextureType::ALBEDO, mahogfloor_albedo); mahogfloor_material_internal->SetTexture(wr::TextureType::NORMAL, mahogfloor_normal); mahogfloor_material_internal->SetTexture(wr::TextureType::ROUGHNESS, mahogfloor_roughness); mahogfloor_material_internal->SetTexture(wr::TextureType::METALLIC, mahogfloor_metallic); mahogfloor_material_internal->SetTexture(wr::TextureType::AO, mahogfloor_ao); // Create Material m_material_handles[1] = m_material_pool->Create(m_texture_pool.get()); wr::Material* red_black_pattern_internal = m_material_pool->GetMaterial(m_material_handles[1]); red_black_pattern_internal->SetTexture(wr::TextureType::ALBEDO, red_black_albedo); red_black_pattern_internal->SetTexture(wr::TextureType::NORMAL, red_black_normal); red_black_pattern_internal->SetTexture(wr::TextureType::ROUGHNESS, red_black_roughness); red_black_pattern_internal->SetTexture(wr::TextureType::METALLIC, red_black_metallic); // Create Material m_material_handles[2] = m_material_pool->Create(m_texture_pool.get()); wr::Material* metal_material_internal = m_material_pool->GetMaterial(m_material_handles[2]); metal_material_internal->SetTexture(wr::TextureType::ALBEDO, metal_albedo); metal_material_internal->SetTexture(wr::TextureType::NORMAL, metal_normal); metal_material_internal->SetTexture(wr::TextureType::ROUGHNESS, metal_roughness); metal_material_internal->SetTexture(wr::TextureType::METALLIC, metal_metallic); m_material_handles[3] = m_material_pool->Create(m_texture_pool.get()); wr::Material* brick_tiles_mat_internal = m_material_pool->GetMaterial(m_material_handles[3]); brick_tiles_mat_internal->SetTexture(wr::TextureType::ALBEDO, brick_tiles_albedo); brick_tiles_mat_internal->SetTexture(wr::TextureType::NORMAL, brick_tiles_normal); brick_tiles_mat_internal->SetTexture(wr::TextureType::ROUGHNESS, brick_tiles_roughness); brick_tiles_mat_internal->SetTexture(wr::TextureType::METALLIC, brick_tiles_metallic); m_material_handles[4] = m_material_pool->Create(m_texture_pool.get()); wr::Material* leather_material_internal = m_material_pool->GetMaterial(m_material_handles[4]); leather_material_internal->SetTexture(wr::TextureType::ALBEDO, leather_albedo); leather_material_internal->SetTexture(wr::TextureType::NORMAL, leather_normal); leather_material_internal->SetTexture(wr::TextureType::ROUGHNESS, leather_roughness); leather_material_internal->SetTexture(wr::TextureType::METALLIC, leather_metallic); m_material_handles[5] = m_material_pool->Create(m_texture_pool.get()); wr::Material* blue_tiles_material_internal = m_material_pool->GetMaterial(m_material_handles[5]); blue_tiles_material_internal->SetTexture(wr::TextureType::ALBEDO, blue_tiles_albedo); blue_tiles_material_internal->SetTexture(wr::TextureType::NORMAL, blue_tiles_normal); blue_tiles_material_internal->SetTexture(wr::TextureType::ROUGHNESS, blue_tiles_roughness); blue_tiles_material_internal->SetTexture(wr::TextureType::METALLIC, blue_tiles_metallic); m_material_handles[6] = m_material_pool->Create(m_texture_pool.get()); wr::Material* gold_material_internal = m_material_pool->GetMaterial(m_material_handles[6]); gold_material_internal->SetTexture(wr::TextureType::ALBEDO, gold_albedo); gold_material_internal->SetTexture(wr::TextureType::NORMAL, gold_normal); gold_material_internal->SetTexture(wr::TextureType::ROUGHNESS, gold_roughness); gold_material_internal->SetTexture(wr::TextureType::METALLIC, gold_metallic); m_material_handles[7] = m_material_pool->Create(m_texture_pool.get()); wr::Material* marble_material_internal = m_material_pool->GetMaterial(m_material_handles[7]); marble_material_internal->SetTexture(wr::TextureType::ALBEDO, marble_albedo); marble_material_internal->SetTexture(wr::TextureType::NORMAL, marble_normal); marble_material_internal->SetTexture(wr::TextureType::ROUGHNESS, marble_roughness); marble_material_internal->SetTexture(wr::TextureType::METALLIC, marble_metallic); m_material_handles[8] = m_material_pool->Create(m_texture_pool.get()); wr::Material* floreal_tiles_internal = m_material_pool->GetMaterial(m_material_handles[8]); floreal_tiles_internal->SetTexture(wr::TextureType::ALBEDO, floreal_tiles_albedo); floreal_tiles_internal->SetTexture(wr::TextureType::NORMAL, floreal_tiles_normal); floreal_tiles_internal->SetTexture(wr::TextureType::ROUGHNESS, floreal_tiles_roughness); floreal_tiles_internal->SetTexture(wr::TextureType::METALLIC, floreal_tiles_metallic); m_material_handles[9] = m_material_pool->Create(m_texture_pool.get()); wr::Material* bw_tiles_internal = m_material_pool->GetMaterial(m_material_handles[9]); bw_tiles_internal->SetTexture(wr::TextureType::ALBEDO, bw_tiles_albedo); bw_tiles_internal->SetTexture(wr::TextureType::NORMAL, bw_tiles_normal); bw_tiles_internal->SetTexture(wr::TextureType::ROUGHNESS, bw_tiles_roughness); bw_tiles_internal->SetTexture(wr::TextureType::METALLIC, bw_tiles_metallic); bw_tiles_internal->SetTexture(wr::TextureType::EMISSIVE, bw_tiles_emissive); m_material_handles[10] = m_material_pool->Create(m_texture_pool.get()); wr::Material* mirror_internal = m_material_pool->GetMaterial(m_material_handles[10]); mirror_internal->SetConstant({ 1.f, 1.f, 1.f }); mirror_internal->SetConstant(1.f); mirror_internal->SetConstant(0.f); } { wr::MeshData mesh; mesh.m_indices = { 2, 1, 0, 3, 2, 0 }; mesh.m_vertices = { //POS UV NORMAL TANGENT BINORMAL { 1, 1, 0, 1, 1, 0, 0, -1, 0, 0, 1, 0, 1, 0, }, { 1, -1, 0, 1, 0, 0, 0, -1, 0, 0, 1, 0, 1, 0, }, { -1, -1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 1, 0, }, { -1, 1, 0, 0, 1, 0, 0, -1, 0, 0, 1, 0, 1, 0, }, }; m_plane_model = m_model_pool->Load(m_material_pool.get(), m_texture_pool.get(), "resources/models/plane.fbx"); for (auto& m : m_plane_model->m_meshes) { m.second = m_material_handles[0]; } } { { m_cube_model = m_model_pool->Load(m_material_pool.get(), m_texture_pool.get(), "resources/models/cube.fbx"); for (auto& m : m_cube_model->m_meshes) { m.second = m_material_handles[0]; } } { m_material_knob_model = m_model_pool->Load(m_material_pool.get(), m_texture_pool.get(), "resources/models/material_ball.fbx"); for (auto& m : m_material_knob_model->m_meshes) { m.second = m_material_handles[0]; } } } } void EmiblScene::BuildScene(unsigned int width, unsigned int height, void* extra) { m_camera = m_scene_graph->CreateChild(nullptr, 90.f, (float)width / (float)height); m_camera->SetPosition({ 500.0f, 60.0f, 260.0f }); m_camera->SetRotation({ -16._deg, 0._deg, 0._deg }); m_camera->SetSpeed(100.0f); auto skybox = m_scene_graph->CreateChild(nullptr, m_skybox); // Geometry for (size_t i = 0; i < 10; ++i) { std::vector mat_handles(m_material_knob_model->m_meshes.size(), m_material_handles[i]); m_models[i] = m_scene_graph->CreateChild(nullptr, m_material_knob_model); m_models[i]->SetMaterials(mat_handles); m_platforms[i] = m_scene_graph->CreateChild(nullptr, m_cube_model); m_platforms[i]->SetMaterials(mat_handles); m_platforms[i]->SetScale({ 38, 1, 38 }); } m_models[9]->SetPosition({ -500, 0 , -160 }); m_platforms[9]->SetPosition({ -500, -3, -160 }); m_models[8]->SetPosition({ -250, 0 , -160 }); m_platforms[8]->SetPosition({ -250, -3 , -160 }); m_models[7]->SetPosition({ 0, 0 , -160 }); m_platforms[7]->SetPosition({ 0, -3 , -160 }); m_models[6]->SetPosition({ +250, 0 ,-160 }); m_platforms[6]->SetPosition({ +250, -3 ,-160 }); m_models[5]->SetPosition({ +500, 0 , -160 }); m_platforms[5]->SetPosition({ +500, -3 , -160 }); m_models[4]->SetPosition({ -500,0 , 160 }); m_platforms[4]->SetPosition({ -500, -3 , 160 }); m_models[3]->SetPosition({ -250, 0 , 160 }); m_platforms[3]->SetPosition({ -250, -3 , 160 }); m_models[2]->SetPosition({ 0, 0 ,160 }); m_platforms[2]->SetPosition({ 0, -3 ,160 }); m_models[1]->SetPosition({ +250,0 , 160 }); m_platforms[1]->SetPosition({ +250, -3 , 160 }); m_models[0]->SetPosition({ +500, 0 , 160 }); m_platforms[0]->SetPosition({ +500, -3 , 160 }); auto dir_light = m_scene_graph->CreateChild(nullptr, wr::LightType::DIRECTIONAL, DirectX::XMVECTOR{ 0, 1, 0 }); dir_light->SetDirectional({ 360_deg - 136_deg, 0, 0 }, { 4, 4, 4 }); } void EmiblScene::Update(float delta) { m_camera->Update(delta); } ================================================ FILE: tests/demo/scene_emibl.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "wisp.hpp" #include "window.hpp" #include "scene_graph/scene_graph.hpp" #include "imgui/imgui.hpp" #include "debug_camera.hpp" #include "../common/scene.hpp" class EmiblScene : public Scene { public: EmiblScene(); ~EmiblScene(); void Update(float delta = 0) final; protected: void LoadResources() final; void BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) final; private: // Models wr::Model* m_cube_model; wr::Model* m_plane_model; wr::Model* m_material_knob_model; // Textures wr::TextureHandle m_skybox; // Materials wr::MaterialHandle m_rusty_metal_material; wr::MaterialHandle m_knob_material; wr::MaterialHandle m_material_handles[11]; // Nodes std::shared_ptr m_camera; std::shared_ptr m_models[10]; std::shared_ptr m_platforms[10]; }; ================================================ FILE: tests/demo/scene_sponza.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "scene_sponza.hpp" static constexpr bool spawn_physics_balls = false; static constexpr int num_materials = 30; static constexpr int num_balls = 600; SponzaScene::SponzaScene() : Scene(256, 20_mb, 20_mb), m_sponza_model(nullptr), m_skybox({}), m_time(0) { m_lights_path = "resources/sponza_lights.json"; } inline float RandRange(float min, float max) { return min + static_cast (rand()) / (static_cast (RAND_MAX / (max - min))); } inline float RandRangeI(float min, float max) { return min + static_cast (rand()) / (static_cast (RAND_MAX / (max - min))); } SponzaScene::~SponzaScene() { if(m_sphere_model) { m_model_pool->Destroy(m_sphere_model); m_model_pool->Destroy(m_sponza_model); for(auto& material : m_mirror_materials) { m_material_pool->DestroyMaterial(material); } } } void SponzaScene::LoadResources() { // Models m_sphere_model = m_model_pool->Load(m_material_pool.get(), m_texture_pool.get(), "resources/models/sphere.fbx"); m_sponza_model = m_model_pool->LoadWithMaterials(m_material_pool.get(), m_texture_pool.get(), "resources/models/sponza/sponza.obj", false, &m_sponza_model_data); // Textures m_skybox = m_texture_pool->LoadFromFile("resources/materials/Barce_Rooftop_C_3k.hdr", false, false); // Materials for (auto i = 0; i < num_materials; i++) { auto mat = m_material_pool->Create(m_texture_pool.get()); wr::Material* mat_internal = m_material_pool->GetMaterial(mat); mat_internal->SetConstant(RandRange(0, 0.5)); mat_internal->SetConstant(RandRange(0.5, 1)); mat_internal->SetConstant(RandRange(0, 1) > 0.8 ? 5 : 0); mat_internal->SetConstant({ RandRange(0.3, 1), RandRange(0.3, 1), RandRange(0.3, 1) }); m_mirror_materials.push_back(mat); } } void SponzaScene::BuildScene(unsigned int width, unsigned int height, void* extra) { auto& phys_engine = *reinterpret_cast(extra); m_camera = m_scene_graph->CreateChild(nullptr, 90.f, (float)width / (float)height); m_camera->SetPosition({ 0, 1, 11 }); m_camera->SetSpeed(10); m_camera_spline_node = m_scene_graph->CreateChild(nullptr, "Camera Spline", false); auto skybox = m_scene_graph->CreateChild(nullptr, m_skybox); // Geometry m_sponza_node = m_scene_graph->CreateChild(nullptr, &phys_engine, m_sponza_model); m_sponza_node->SetupConvex(phys_engine, m_sponza_model_data); m_sponza_node->SetRestitution(1.f); m_sponza_node->SetPosition({ 0, -1, 0 }); m_sponza_node->SetRotation({ 0, 90_deg, 0 }); m_sponza_node->SetScale({ 0.01f,0.01f,0.01f }); if constexpr (spawn_physics_balls) { // Left Ballfall for (auto i = 0; i < num_balls / 2; i++) { auto ball = m_scene_graph->CreateChild(nullptr, &phys_engine, m_sphere_model); ball->SetMass(0.004f); ball->SetupSimpleSphereColl(phys_engine, 1.f); ball->m_rigid_body->setRestitution(0.5); ball->m_rigid_body->setFriction(0.2); ball->m_rigid_body->setLinearVelocity({ 2, 0, 0 }); ball->m_rigid_body->setRollingFriction(0); ball->m_rigid_body->setSpinningFriction(0); ball->SetPosition({ -2.440, 5.5, RandRange(-7.7, 8.8) }); ball->SetScale({ 0.2f, 0.2f, 0.2f }); ball->AddMaterial(m_mirror_materials[RandRangeI(0, num_materials - 1)]); ball->m_rigid_body->activate(true); } // Right Ballfall for (auto i = 0; i < num_balls / 2; i++) { auto ball = m_scene_graph->CreateChild(nullptr, &phys_engine, m_sphere_model); ball->SetMass(0.004f); ball->SetupSimpleSphereColl(phys_engine, 1.f); ball->m_rigid_body->setRestitution(0.5); ball->m_rigid_body->setFriction(0.2); ball->m_rigid_body->setLinearVelocity({ -2, 0, 0 }); ball->m_rigid_body->setRollingFriction(0); ball->m_rigid_body->setSpinningFriction(0); ball->SetPosition({ 2.440, 5.5, RandRange(-7.7, 8.8) }); ball->SetScale({ 0.2f, 0.2f, 0.2f }); ball->AddMaterial(m_mirror_materials[RandRangeI(0, num_materials - 1)]); ball->m_rigid_body->activate(true); } } // Lights LoadLightsFromJSON(); } void SponzaScene::Update(float delta) { m_camera->Update(delta); m_camera_spline_node->UpdateSplineNode(delta, m_camera); } ================================================ FILE: tests/demo/scene_sponza.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "wisp.hpp" #include "window.hpp" #include "scene_graph/scene_graph.hpp" #include "imgui/imgui.hpp" #include "physics_node.hpp" #include "debug_camera.hpp" #include "spline_node.hpp" #include "../common/scene.hpp" class SponzaScene : public Scene { public: SponzaScene(); ~SponzaScene(); void Update(float delta = 0) final; protected: void LoadResources() final; void BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) final; private: // Models wr::Model* m_sphere_model; wr::Model* m_sponza_model; wr::ModelData* m_sponza_model_data; // Textures wr::TextureHandle m_skybox; // Materials std::vector m_mirror_materials; // Nodes std::shared_ptr m_camera; std::shared_ptr m_camera_spline_node; std::shared_ptr m_sponza_node; float m_time; }; ================================================ FILE: tests/demo/scene_viknell.cpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "scene_viknell.hpp" ViknellScene::ViknellScene() : Scene(256, 2_mb, 2_mb), m_sphere_model(nullptr), m_plane_model(nullptr), m_xbot_model(nullptr), m_bamboo_material(), m_skybox({}), m_plane_model_data(nullptr), m_time(0) { m_lights_path = "resources/viknell_lights.json"; } ViknellScene::~ViknellScene() { if(m_plane_model) { m_model_pool->Destroy(m_plane_model); m_model_pool->Destroy(m_xbot_model); m_model_pool->Destroy(m_sphere_model); m_material_pool->DestroyMaterial(m_bamboo_material); m_material_pool->DestroyMaterial(m_mirror_material); } } void ViknellScene::LoadResources() { // Models m_plane_model = m_model_pool->Load(m_material_pool.get(), m_texture_pool.get(), "resources/models/plane.fbx", &m_plane_model_data); m_xbot_model = m_model_pool->LoadWithMaterials(m_material_pool.get(), m_texture_pool.get(), "resources/models/xbot.fbx"); m_sphere_model = m_model_pool->Load(m_material_pool.get(), m_texture_pool.get(), "resources/models/sphere.fbx"); // Textures wr::TextureHandle bamboo_albedo = m_texture_pool->LoadFromFile("resources/materials/bamboo/bamboo-wood-semigloss-albedo.png", true, true); wr::TextureHandle bamboo_normal = m_texture_pool->LoadFromFile("resources/materials/bamboo/bamboo-wood-semigloss-normal.png", false, true); wr::TextureHandle bamboo_roughness = m_texture_pool->LoadFromFile("resources/materials/bamboo/bamboo-wood-semigloss-roughness.png", false, true); wr::TextureHandle bamboo_metallic = m_texture_pool->LoadFromFile("resources/materials/bamboo/bamboo-wood-semigloss-metal.png", false, true); m_skybox = m_texture_pool->LoadFromFile("resources/materials/Barce_Rooftop_C_3k.hdr", false, false); // Materials m_mirror_material = m_material_pool->Create(m_texture_pool.get()); wr::Material* mirror_internal = m_material_pool->GetMaterial(m_mirror_material); mirror_internal->SetConstant(0); mirror_internal->SetConstant(1); mirror_internal->SetConstant({ 1, 1, 1 }); m_bamboo_material = m_material_pool->Create(m_texture_pool.get()); wr::Material* bamboo_material_internal = m_material_pool->GetMaterial(m_bamboo_material); bamboo_material_internal->SetTexture(wr::TextureType::ALBEDO, bamboo_albedo); bamboo_material_internal->SetTexture(wr::TextureType::NORMAL, bamboo_normal); bamboo_material_internal->SetTexture(wr::TextureType::ROUGHNESS, bamboo_roughness); bamboo_material_internal->SetTexture(wr::TextureType::METALLIC, bamboo_metallic); } void ViknellScene::BuildScene(unsigned int width, unsigned int height, void* extra) { auto& phys_engine = *reinterpret_cast(extra); m_camera = m_scene_graph->CreateChild(nullptr, 90.f, (float)width / (float)height); m_camera->SetPosition({ 0, 0, 2 }); m_camera->SetSpeed(10); m_camera_spline_node = m_scene_graph->CreateChild(nullptr, "Camera Spline", false); auto skybox = m_scene_graph->CreateChild(nullptr, m_skybox); // Geometry auto floor = m_scene_graph->CreateChild(nullptr, &phys_engine, m_plane_model); auto roof = m_scene_graph->CreateChild(nullptr, m_plane_model); auto back_wall = m_scene_graph->CreateChild(nullptr, m_plane_model); auto left_wall = m_scene_graph->CreateChild(nullptr, m_plane_model); auto right_wall = m_scene_graph->CreateChild(nullptr, m_plane_model); m_xbot_node = m_scene_graph->CreateChild(nullptr, &phys_engine, m_xbot_model); auto sphere = m_scene_graph->CreateChild(nullptr, m_sphere_model); floor->SetupConvex(phys_engine, m_plane_model_data); floor->SetRestitution(1.f); floor->SetPosition({ 0, -1, 0 }); floor->SetRotation({ 90_deg, 0, 0 }); floor->AddMaterial(m_bamboo_material); sphere->SetPosition({ 1, -1, -1 }); sphere->SetScale({ 0.6f, 0.6f, 0.6f }); sphere->AddMaterial(m_mirror_material); roof->SetPosition({ 0, 1, 0 }); roof->SetRotation({ -90_deg, 0, 0 }); roof->AddMaterial(m_bamboo_material); back_wall->SetPosition({ 0, 0, -1 }); back_wall->SetRotation({ 0, 180_deg, 0 }); back_wall->AddMaterial(m_bamboo_material); left_wall->SetPosition({ -1, 0, 0 }); left_wall->SetRotation({ 0, -90_deg, 0 }); left_wall->AddMaterial(m_bamboo_material); right_wall->SetPosition({ 1, 0, 0 }); right_wall->SetRotation({ 0, 90_deg, 0 }); right_wall->AddMaterial(m_bamboo_material); m_xbot_node->SetMass(0.01f); m_xbot_node->SetupSimpleSphereColl(phys_engine, 0.5); m_xbot_node->m_rigid_body->setRestitution(0.4); m_xbot_node->m_rigid_body->setFriction(0); m_xbot_node->m_rigid_body->setRollingFriction(0); m_xbot_node->m_rigid_body->setSpinningFriction(0); m_xbot_node->SetPosition({ 0, -1, 0 }); m_xbot_node->SetRotation({ 0, 180_deg, 0 }); m_xbot_node->SetScale({ 0.01f,0.01f,0.01f }); m_xbot_node->m_rigid_body->activate(true); // Lights /*auto point_light_0 = m_scene_graph->CreateChild(nullptr, wr::LightType::DIRECTIONAL, DirectX::XMVECTOR{ 1, 1, 1 }); point_light_0->SetRotation({ 20.950f, 0.98f, 0.f }); point_light_0->SetPosition({ -0.002f, 0.080f, 1.404f }); auto point_light_1 = m_scene_graph->CreateChild(nullptr, wr::LightType::POINT, DirectX::XMVECTOR{ 1, 0, 0 }); point_light_1->SetRadius(5.0f); point_light_1->SetPosition({ 0.5f, 0.f, -0.3f }); auto point_light_2 = m_scene_graph->CreateChild(nullptr, wr::LightType::POINT, DirectX::XMVECTOR{ 0, 0, 1 }); point_light_2->SetRadius(5.0f); point_light_2->SetPosition({ -0.5f, 0.5f, -0.3f });*/ LoadLightsFromJSON(); } void ViknellScene::Update(float delta) { m_camera->Update(delta); m_camera_spline_node->UpdateSplineNode(ImGui::GetIO().DeltaTime, m_camera); } ================================================ FILE: tests/demo/scene_viknell.hpp ================================================ /*! * 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) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "wisp.hpp" #include "window.hpp" #include "scene_graph/scene_graph.hpp" #include "imgui/imgui.hpp" #include "physics_node.hpp" #include "debug_camera.hpp" #include "spline_node.hpp" #include "../common/scene.hpp" class ViknellScene : public Scene { public: ViknellScene(); ~ViknellScene(); void Update(float delta = 0) final; protected: void LoadResources() final; void BuildScene(unsigned int width, unsigned int height, void* extra = nullptr) final; private: // Models wr::Model* m_sphere_model; wr::Model* m_plane_model; wr::ModelData* m_plane_model_data; wr::Model* m_xbot_model; // Textures wr::TextureHandle m_skybox; // Materials wr::MaterialHandle m_bamboo_material; wr::MaterialHandle m_mirror_material; // Nodes std::shared_ptr m_camera; std::shared_ptr m_camera_spline_node; std::shared_ptr m_xbot_node; float m_time; }; ================================================ FILE: tests/demo/spline_library/LICENSE ================================================ Mozilla Public License, version 2.0 1. Definitions 1.1. “Contributor” means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software. 1.2. “Contributor Version” means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor’s Contribution. 1.3. “Contribution” means Covered Software of a particular Contributor. 1.4. “Covered Software” means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof. 1.5. “Incompatible With Secondary Licenses” means a. that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or b. that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License. 1.6. “Executable Form” means any form of the work other than Source Code Form. 1.7. “Larger Work” means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software. 1.8. “License” means this document. 1.9. “Licensable” means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License. 1.10. “Modifications” means any of the following: a. any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or b. any new file in Source Code Form that contains any Covered Software. 1.11. “Patent Claims” of a Contributor means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version. 1.12. “Secondary License” means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses. 1.13. “Source Code Form” means the form of the work preferred for making modifications. 1.14. “You” (or “Your”) means an individual or a legal entity exercising rights under this License. For legal entities, “You” includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, “control” means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity. 2. License Grants and Conditions 2.1. Grants Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license: a. under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and b. under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version. 2.2. Effective Date The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution. 2.3. Limitations on Grant Scope The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor: a. for any code that a Contributor has removed from Covered Software; or b. for infringements caused by: (i) Your and any other third party’s modifications of Covered Software, or (ii) the combination of its Contributions with other software (except as part of its Contributor Version); or c. under Patent Claims infringed by Covered Software in the absence of its Contributions. This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4). 2.4. Subsequent Licenses No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3). 2.5. Representation Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License. 2.6. Fair Use This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents. 2.7. Conditions Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1. 3. Responsibilities 3.1. Distribution of Source Form All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients’ rights in the Source Code Form. 3.2. Distribution of Executable Form If You distribute Covered Software in Executable Form then: a. such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and b. You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients’ rights in the Source Code Form under this License. 3.3. Distribution of a Larger Work You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s). 3.4. Notices You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies. 3.5. Application of Additional Terms You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction. 4. Inability to Comply Due to Statute or Regulation If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: (a) comply with the terms of this License to the maximum extent possible; and (b) describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it. 5. Termination 5.1. The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated (a) provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice. 5.2. If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate. 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination. 6. Disclaimer of Warranty Covered Software is provided under this License on an “as is” basis, without warranty of any kind, either expressed, implied, or statutory, including, without limitation, warranties that the Covered Software is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire risk as to the quality and performance of the Covered Software is with You. Should any Covered Software prove defective in any respect, You (not any Contributor) assume the cost of any necessary servicing, repair, or correction. This disclaimer of warranty constitutes an essential part of this License. No use of any Covered Software is authorized under this License except under this disclaimer. 7. Limitation of Liability Under no circumstances and under no legal theory, whether tort (including negligence), contract, or otherwise, shall any Contributor, or anyone who distributes Covered Software as permitted above, be liable to You for any direct, indirect, special, incidental, or consequential damages of any character including, without limitation, damages for lost profits, loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses, even if such party shall have been informed of the possibility of such damages. This limitation of liability shall not apply to liability for death or personal injury resulting from such party’s negligence to the extent applicable law prohibits such limitation. Some jurisdictions do not allow the exclusion or limitation of incidental or consequential damages, so this exclusion and limitation may not apply to You. 8. Litigation Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party’s ability to bring cross-claims or counter-claims. 9. Miscellaneous This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor. 10. Versions of the License 10.1. New Versions Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number. 10.2. Effect of New Versions You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward. 10.3. Modified Versions If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License). 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached. Exhibit A - Source Code Form License Notice This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. You may add additional accurate notices of copyright ownership. Exhibit B - “Incompatible With Secondary Licenses” Notice This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0. ================================================ FILE: tests/demo/spline_library/spline.h ================================================ #pragma once #include #include "utils/spline_common.h" #include "utils/calculus.h" template class Spline { public: Spline(std::vector originalPoints, floating_t maxT) :maxT(maxT), originalPoints(std::move(originalPoints)) {} public: struct InterpolatedPT; struct InterpolatedPTC; struct InterpolatedPTCW; virtual InterpolationType getPosition(floating_t x) const = 0; virtual InterpolatedPT getTangent(floating_t x) const = 0; virtual InterpolatedPTC getCurvature(floating_t x) const = 0; virtual InterpolatedPTCW getWiggle(floating_t x) const = 0; virtual floating_t arcLength(floating_t a, floating_t b) const = 0; virtual floating_t totalLength(void) const = 0; inline floating_t getMaxT(void) const { return maxT; } const std::vector &getOriginalPoints(void) const { return originalPoints; } virtual bool isLooping(void) const = 0; //lower level functions virtual size_t segmentCount(void) const = 0; virtual size_t segmentForT(floating_t t) const = 0; virtual floating_t segmentT(size_t segmentIndex) const = 0; virtual floating_t segmentArcLength(size_t segmentIndex, floating_t a, floating_t b) const = 0; protected: const floating_t maxT; private: const std::vector originalPoints; }; template class LoopingSpline: public Spline { public: LoopingSpline(std::vector originalPoints, floating_t maxT) :Spline(std::move(originalPoints), maxT) {} inline floating_t wrapT(floating_t t) const { float wrappedT = std::fmod(t, this->maxT); if(wrappedT < 0) return wrappedT + this->maxT; else return wrappedT; } virtual floating_t cyclicArcLength(floating_t a, floating_t b) const = 0; }; template class SplineCore, class InterpolationType, typename floating_t> class SplineImpl: public Spline { public: InterpolationType getPosition(floating_t t) const override { return common.getPosition(t); } typename Spline::InterpolatedPT getTangent(floating_t t) const override { return common.getTangent(t); } typename Spline::InterpolatedPTC getCurvature(floating_t t) const override { return common.getCurvature(t); } typename Spline::InterpolatedPTCW getWiggle(floating_t t) const override { return common.getWiggle(t); } floating_t arcLength(floating_t a, floating_t b) const override; floating_t totalLength(void) const override; bool isLooping(void) const override { return false; } size_t segmentCount(void) const override { return common.segmentCount(); } size_t segmentForT(floating_t t) const override { return common.segmentForT(t); } floating_t segmentT(size_t segmentIndex) const override { return common.segmentT(segmentIndex); } floating_t segmentArcLength(size_t segmentIndex, floating_t a, floating_t b) const override { return common.segmentLength(segmentIndex, a, b); } protected: //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 SplineImpl(std::vector originalPoints, floating_t maxT) :Spline(std::move(originalPoints), maxT) {} ~SplineImpl(void) = default; SplineCore common; }; template class SplineCore, class InterpolationType, typename floating_t> class SplineLoopingImpl: public LoopingSpline { public: InterpolationType getPosition(floating_t globalT) const override { return common.getPosition(this->wrapT(globalT)); } typename Spline::InterpolatedPT getTangent(floating_t globalT) const override { return common.getTangent(this->wrapT(globalT)); } typename Spline::InterpolatedPTC getCurvature(floating_t globalT) const override { return common.getCurvature(this->wrapT(globalT)); } typename Spline::InterpolatedPTCW getWiggle(floating_t globalT) const override { return common.getWiggle(this->wrapT(globalT)); } floating_t arcLength(floating_t a, floating_t b) const override; floating_t cyclicArcLength(floating_t a, floating_t b) const override; floating_t totalLength(void) const override; bool isLooping(void) const override { return true; } size_t segmentCount(void) const override { return common.segmentCount(); } size_t segmentForT(floating_t t) const override { return common.segmentForT(this->wrapT(t)); } floating_t segmentT(size_t segmentIndex) const override { return common.segmentT(segmentIndex); } floating_t segmentArcLength(size_t segmentIndex, floating_t a, floating_t b) const override { return common.segmentLength(segmentIndex, a, b); } protected: //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 SplineLoopingImpl(std::vector originalPoints, floating_t maxT) :LoopingSpline(std::move(originalPoints), maxT) {} ~SplineLoopingImpl(void) = default; SplineCore common; }; template struct Spline::InterpolatedPT { InterpolationType position; InterpolationType tangent; InterpolatedPT(const InterpolationType &p, const InterpolationType &t) :position(p),tangent(t) {} }; template struct Spline::InterpolatedPTC { InterpolationType position; InterpolationType tangent; InterpolationType curvature; InterpolatedPTC(const InterpolationType &p, const InterpolationType &t, const InterpolationType &c) :position(p),tangent(t),curvature(c) {} }; template struct Spline::InterpolatedPTCW { InterpolationType position; InterpolationType tangent; InterpolationType curvature; InterpolationType wiggle; InterpolatedPTCW(const InterpolationType &p, const InterpolationType &t, const InterpolationType &c, const InterpolationType &w) :position(p),tangent(t),curvature(c), wiggle(w) {} }; template class SplineCore, class InterpolationType, typename floating_t> floating_t SplineImpl::arcLength(floating_t a, floating_t b) const { if(a > b) { std::swap(a,b); } //get the knot indices for the beginning and end size_t aIndex = common.segmentForT(a); size_t bIndex = common.segmentForT(b); //if a and b occur inside the same segment, compute the length within that segment //but excude cases where a > b, because that means we need to wrap around if(aIndex == bIndex) { return common.segmentLength(aIndex, a, b); } else { //a and b occur in different segments, so compute one length for every segment floating_t result{0}; //first segment floating_t aEnd = common.segmentT(aIndex + 1); result += common.segmentLength(aIndex, a, aEnd); //middle segments for(size_t i = aIndex + 1; i < bIndex; i++) { result += common.segmentLength(i, common.segmentT(i), common.segmentT(i + 1)); } //last segment floating_t bBegin = common.segmentT(bIndex); result += common.segmentLength(bIndex, bBegin, b); return result; } } template class SplineCore, class InterpolationType, typename floating_t> floating_t SplineImpl::totalLength(void) const { floating_t result{0}; for(size_t i = 0; i < common.segmentCount(); i++) { result += common.segmentLength(i, common.segmentT(i), common.segmentT(i+1)); } return result; } template class SplineCore, class InterpolationType, typename floating_t> floating_t SplineLoopingImpl::arcLength(floating_t a, floating_t b) const { a = this->wrapT(a); b = this->wrapT(b); if(a > b) { std::swap(a,b); } //get the knot indices for the beginning and end size_t aIndex = common.segmentForT(a); size_t bIndex = common.segmentForT(b); //if a and b occur inside the same segment, compute the length within that segment //but excude cases where a > b, because that means we need to wrap around if(aIndex == bIndex) { return common.segmentLength(aIndex, a, b); } else { //a and b occur in different segments, so compute one length for every segment floating_t result{0}; //first segment floating_t aEnd = common.segmentT(aIndex + 1); result += common.segmentLength(aIndex, a, aEnd); //middle segments for(size_t i = aIndex + 1; i < bIndex; i++) { result += common.segmentLength(i, common.segmentT(i), common.segmentT(i + 1)); } //last segment floating_t bBegin = common.segmentT(bIndex); result += common.segmentLength(bIndex, bBegin, b); return result; } } //compute the arc length from a to b on the given spline, using wrapping/cyclic logic //for cyclic splines only! template