From feab1fe3fbabd9ae30cfc537014f9e943b4247aa Mon Sep 17 00:00:00 2001 From: Qingnan Zhou Date: Wed, 6 Oct 2021 13:35:00 -0400 Subject: [PATCH] Sync with addfe23 --- .gitignore | 5 +- CMakeLists.txt | 43 +++++- LagrangeOptions.cmake.sample | 10 +- cmake/lagrange/lagrange_add_example.cmake | 2 +- cmake/lagrange/lagrange_add_module.cmake | 50 ++++++ cmake/lagrange/lagrange_add_performance.cmake | 2 +- cmake/lagrange/lagrange_add_test.cmake | 2 +- cmake/lagrange/lagrange_include_modules.cmake | 6 - .../lagrange/lagrange_limit_parallelism.cmake | 99 ++++++++++++ .../lagrange_runtime_dependencies.cmake | 24 +-- cmake/recipes/external/boost.cmake | 6 + cmake/recipes/external/eigen.cmake | 2 +- cmake/recipes/external/ilmbase.cmake | 2 + cmake/recipes/external/mdl.cmake | 47 +++++- cmake/recipes/external/mkl-2020.4.cmake | 41 ----- cmake/recipes/external/mkl-2021.3.0.cmake | 41 ----- cmake/recipes/external/mkl.cmake | 96 +++++++++++- cmake/recipes/external/openvdb.cmake | 2 + cmake/recipes/external/tbb.cmake | 15 +- modules/core/CMakeLists.txt | 60 ++++---- modules/core/include/lagrange/Mesh.h | 9 ++ modules/core/include/lagrange/create_mesh.h | 7 +- .../core/include/lagrange/utils/geometry3d.h | 1 + modules/core/performance/components.cpp | 1 + modules/core/performance/connectivity.cpp | 1 + .../core/performance/experimental/array.cpp | 1 + .../core/performance/mesh_initialization.cpp | 1 + .../util/default.frag => core/src/Mesh.cpp} | 16 +- modules/core/src/create_mesh.cpp | 43 +++--- modules/core/src/predicates.cpp | 40 ++++- modules/core/tests/CMakeLists.txt | 7 + .../tests/test_compute_tangent_bitangent.cpp | 2 +- .../tests/test_corner_to_edge_mapping.cpp | 1 + modules/core/tests/test_quad_to_tri.cpp | 1 + modules/fs/CMakeLists.txt | 21 +-- modules/io/CMakeLists.txt | 26 +--- modules/io/include/lagrange/io/load_mesh.h | 74 ++------- .../io/include/lagrange/io/load_mesh.impl.h | 84 ++++++++++ modules/io/src/load_mesh.cpp | 31 ++++ modules/io/tests/test_mesh_io.cpp | 4 +- modules/testing/CMakeLists.txt | 27 ++-- .../testing/include/lagrange/testing/common.h | 5 +- modules/testing/src/common.cpp | 6 + modules/ui/CMakeLists.txt | 33 +--- .../ui/examples/ui_callbacks/CMakeLists.txt | 9 -- .../examples/ui_dynamic_mesh/CMakeLists.txt | 9 -- .../ui/examples/ui_playground/CMakeLists.txt | 9 -- modules/ui/examples/ui_playground/main.cpp | 13 +- modules/ui/examples/ui_scene/CMakeLists.txt | 9 -- .../examples/ui_show_attribute/CMakeLists.txt | 9 -- .../ui/examples/ui_treenode/CMakeLists.txt | 9 -- modules/ui/include/lagrange/ui/Viewer.h | 38 ++++- .../include/lagrange/ui/components/Viewport.h | 1 + .../ui/include/lagrange/ui/default_entities.h | 13 +- .../ui/include/lagrange/ui/default_shaders.h | 1 + .../ui/include/lagrange/ui/types/Material.h | 2 +- modules/ui/include/lagrange/ui/types/Shader.h | 1 + .../include/lagrange/ui/types/ShaderLoader.h | 14 +- .../ui/include/lagrange/ui/types/Systems.h | 5 + modules/ui/src/Viewer.cpp | 144 +++++++++++------- modules/ui/src/default_components.cpp | 126 ++++++++------- modules/ui/src/default_entities.cpp | 53 +++++-- modules/ui/src/default_shaders.cpp | 8 + modules/ui/src/default_tools.cpp | 35 +++-- modules/ui/src/panels/ScenePanel.cpp | 2 +- modules/ui/src/panels/ViewportPanel.cpp | 6 +- modules/ui/src/shaders/common.glsl.deprecated | 12 -- .../ui/src/shaders/cubemap/convolve.shader | 4 +- .../ui/src/shaders/cubemap/specular.shader | 8 +- modules/ui/src/shaders/cubemap/to_cube.shader | 2 +- .../ui/src/shaders/default.shader.deprecated | 72 --------- .../ui/src/shaders/depth/to_cubemap.shader | 19 +-- .../ui/src/shaders/depth/to_texture.shader | 32 +--- modules/ui/src/shaders/face_id.shader | 68 ++++----- .../layout/default_fragment_layout.glsl | 11 ++ .../default_vertex_layout.glsl} | 19 ++- .../shaders/lines/attribute_to_lines.shader | 22 +-- .../ui/src/shaders/lines/edge_to_line.shader | 2 +- .../shaders/lines/triangle_to_lines.shader | 45 +++--- .../ui/src/shaders/normals/triangle.shader | 10 +- modules/ui/src/shaders/normals/vertex.shader | 10 +- modules/ui/src/shaders/outline.shader | 69 --------- modules/ui/src/shaders/points/points.shader | 36 ++--- modules/ui/src/shaders/post/FXAA.shader | 16 +- modules/ui/src/shaders/post/outline.shader | 12 +- modules/ui/src/shaders/post/tonemap.shader | 2 +- .../ui/src/shaders/procedural/ground.shader | 44 +++--- modules/ui/src/shaders/skybox.shader | 10 +- modules/ui/src/shaders/surface/colormap.frag | 14 +- .../src/shaders/surface/edge_attribute.shader | 12 +- .../shaders/surface/edge_to_surface.shader | 4 +- modules/ui/src/shaders/surface/flat.frag | 12 +- .../ui/src/shaders/surface/objectid.shader | 18 +-- modules/ui/src/shaders/surface/pbr.frag | 16 +- modules/ui/src/shaders/surface/phong.frag | 80 +++++----- modules/ui/src/shaders/surface/simple.shader | 17 +-- modules/ui/src/shaders/surface/surface.shader | 5 - .../shaders/surface/vertex_attribute.shader | 12 +- modules/ui/src/shaders/test.shader | 92 ----------- modules/ui/src/shaders/texture.shader | 18 +-- modules/ui/src/shaders/uniforms/common.glsl | 1 + modules/ui/src/shaders/uniforms/lights.glsl | 58 +++---- .../ui/src/shaders/uniforms/materials.glsl | 29 ++-- modules/ui/src/shaders/util/brdf_lut.shader | 20 +-- modules/ui/src/shaders/util/default.vertex | 34 +---- modules/ui/src/shaders/util/light.glsl | 46 +++--- modules/ui/src/shaders/util/multisample.glsl | 6 +- modules/ui/src/shaders/util/pbr.glsl | 14 +- modules/ui/src/shaders/util/pbr_shading.glsl | 26 ++-- modules/ui/src/shaders/util/phong.glsl | 30 ++-- modules/ui/src/shaders/util/skeletal.vertex | 56 ------- .../ui/src/shaders/util/split_by_edge.geom | 2 +- modules/ui/src/systems/render_background.cpp | 10 +- modules/ui/src/systems/render_geometry.cpp | 85 +++++++---- modules/ui/src/systems/render_shadowmaps.cpp | 90 +++++------ modules/ui/src/systems/render_viewports.cpp | 17 ++- modules/ui/src/systems/update_gizmo.cpp | 4 +- .../ui/src/systems/update_mesh_buffers.cpp | 22 ++- .../ui/src/systems/update_mesh_hovered.cpp | 2 +- .../ui/src/systems/update_scene_bounds.cpp | 8 +- modules/ui/src/types/Camera.cpp | 3 + modules/ui/src/types/MDL.cpp | 8 +- modules/ui/src/types/Material.cpp | 17 ++- modules/ui/src/types/ShaderLoader.cpp | 64 +++++++- modules/ui/src/types/Systems.cpp | 13 ++ modules/ui/src/types/Texture.cpp | 1 + modules/ui/src/utils/io.cpp | 29 +++- modules/ui/src/utils/layer.cpp | 2 +- modules/ui/src/utils/render.cpp | 6 +- 129 files changed, 1582 insertions(+), 1459 deletions(-) create mode 100644 cmake/lagrange/lagrange_add_module.cmake create mode 100644 cmake/lagrange/lagrange_limit_parallelism.cmake delete mode 100644 cmake/recipes/external/mkl-2020.4.cmake delete mode 100644 cmake/recipes/external/mkl-2021.3.0.cmake rename modules/{ui/src/shaders/util/default.frag => core/src/Mesh.cpp} (61%) create mode 100644 modules/io/include/lagrange/io/load_mesh.impl.h create mode 100644 modules/io/src/load_mesh.cpp delete mode 100644 modules/ui/src/shaders/common.glsl.deprecated delete mode 100644 modules/ui/src/shaders/default.shader.deprecated create mode 100644 modules/ui/src/shaders/layout/default_fragment_layout.glsl rename modules/ui/src/shaders/{util/vertex_layout.glsl => layout/default_vertex_layout.glsl} (69%) delete mode 100644 modules/ui/src/shaders/outline.shader delete mode 100644 modules/ui/src/shaders/test.shader delete mode 100644 modules/ui/src/shaders/util/skeletal.vertex diff --git a/.gitignore b/.gitignore index e5acc9a8..9d8220ea 100644 --- a/.gitignore +++ b/.gitignore @@ -58,4 +58,7 @@ LagrangeOptions.cmake /external /dist -/scripts/.miniconda3 \ No newline at end of file +/scripts/.miniconda3 + +# CMake user presets +/CMakeUserPresets.json diff --git a/CMakeLists.txt b/CMakeLists.txt index de37bc56..526f7593 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ else() endif() # Check required CMake version -set(REQUIRED_CMAKE_VERSION "3.16.0") +set(REQUIRED_CMAKE_VERSION "3.20.0") if(LAGRANGE_TOPLEVEL_PROJECT) cmake_minimum_required(VERSION ${REQUIRED_CMAKE_VERSION}) else() @@ -40,7 +40,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/LagrangeOptions.cmake) endif() ################################################################################ -project(Lagrange VERSION 5.3.0) +project(Lagrange VERSION 5.4.0) # Detects whether internal libs are available if(IS_DIRECTORY "${PROJECT_SOURCE_DIR}/cmake/recipes/internal") @@ -52,12 +52,27 @@ endif() # Meta target: ALL includes all optional modules and UI. option(LAGRANGE_ALL "Build all lagrange modules" OFF) -# When building anorigami module, defaults to using Eigen with MKL-dynamic and TBB-dynamic +# Temporary workaround for Linux-related build issues. +if(CMAKE_HOST_SYSTEM_NAME MATCHES Linux) + # Unfortunately, on CentOS 7, the pre-compiled version of libarpack.so provided by + # anorigami is somehow incompatible with our built-from-source TBB. Indeed, their + # libarpack.so is looking for libtbb.so.2, but our CMake only produces a libtbb.so. + # Until we figure this out, we have to build arpack ourselves from source on CentOS. + option(ARPACK_BUILD_FROM_SOURCE "Build arpack from source using a Fortran compiler" ON) + # Builds are super slow on Linux VM, so we keep the old TBB for now on Linux... + option(LAGRANGE_WITH_ONETBB "Build Lagrange with OneTBB 2021 rather than TBB 2020" OFF) +endif() + +# When building anorigami or deformers module, enable Eigen with MKL +if(LAGRANGE_ANORIGAMI OR LAGRANGE_DEFORMERS OR (LAGRANGE_ALL AND NOT LAGRANGE_NO_INTERNAL)) + option(EIGEN_WITH_MKL "Use Eigen with MKL" ON) +endif() + +# When building anorigami module, additionally defaults to MKL-dynamic and TBB-dynamic if(LAGRANGE_ANORIGAMI OR (LAGRANGE_ALL AND NOT LAGRANGE_NO_INTERNAL)) set(MKL_LINKING "dynamic" CACHE STRING "Linking strategy to use with MKL (static, dynamic or sdl)") - option(EIGEN_WITH_MKL "Use Eigen with MKL" ON) - option(TBB_PREFER_STATIC "Build with static TBB" OFF) - option(TBB_BUILD_TBBMALLOC "Build TBB malloc library" ON) + option(TBB_PREFER_STATIC "Build with static TBB" OFF) + option(TBB_BUILD_TBBMALLOC "Build TBB malloc library" ON) endif() # When building Arpack from source, we need to enable Fortran support in the top-level directory @@ -66,7 +81,6 @@ if(ARPACK_BUILD_FROM_SOURCE) endif() # General CMake options -option(LAGRANGE_CI_BUILD "Building for continuous integration" OFF) option(LAGRANGE_COPY_RUNTIME_DEPS "Copy runtime dependencies into executable folders" ${LAGRANGE_TOPLEVEL_PROJECT}) option(LAGRANGE_DOCUMENTATION "Build Doxygen documentation" OFF) option(LAGRANGE_EXAMPLES "Build all examples" ${LAGRANGE_TOPLEVEL_PROJECT}) @@ -76,6 +90,7 @@ option(LAGRANGE_UNIT_TESTS "Build all unit tests" option(LAGRANGE_USE_PCH "Enable precompiled headers" OFF) option(LAGRANGE_WITH_ONETBB "Build Lagrange with OneTBB 2021 rather than TBB 2020" ON) option(LAGRANGE_WITH_TRACY "Build tracy client with Lagrange" OFF) +option(LAGRANGE_LIMIT_PARALLELISM "Limit parallelism according to available cpu/memory" OFF) # Allowlist/blocklist restricting which modules are allowed in the sub-project. This is an advanced # option, targeted at users who wish to have explicit knowledge of which module is implicitly @@ -112,20 +127,32 @@ else() endif() list(PREPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/lagrange/") +# Lagrange folder prefix for IDE +if(LAGRANGE_TOPLEVEL_PROJECT) + set(LAGRANGE_IDE_PREFIX "") +else() + set(LAGRANGE_IDE_PREFIX "third_party/") +endif() -include(sanitizers) include(lagrange_add_example) +include(lagrange_add_module) include(lagrange_add_performance) include(lagrange_add_test) include(lagrange_global_flags) include(lagrange_include_modules) include(lagrange_install) include(lagrange_warnings) +include(lagrange_limit_parallelism) +include(sanitizers) set(CMAKE_MACOSX_RPATH 1) # Set rpath for mac set_property(GLOBAL PROPERTY USE_FOLDERS ON) +if(LAGRANGE_LIMIT_PARALLELISM) + lagrange_limit_parallelism() +endif() + find_package(Threads REQUIRED) # Include OneTBB early to provide TBB::tbb target diff --git a/LagrangeOptions.cmake.sample b/LagrangeOptions.cmake.sample index e3ce648c..f88bc6f8 100644 --- a/LagrangeOptions.cmake.sample +++ b/LagrangeOptions.cmake.sample @@ -53,9 +53,9 @@ # option(LAGRANGE_ALL "Build all lagrange modules" ON) # Optional modules in alphabetical order. -# option(LAGRANGE_FS "Build module lagrange::fs" ON) -# option(LAGRANGE_IO "Build module lagrange::io" ON) -# option(LAGRANGE_UI "Build module lagrange::ui" ON) +# option(LAGRANGE_FS "Build module lagrange::fs" ON) +# option(LAGRANGE_IO "Build module lagrange::io" ON) +# option(LAGRANGE_UI "Build module lagrange::ui" ON) # UI # option(LAGRANGE_UI_USE_MDL "Use Material Definition Library" ON) @@ -93,7 +93,7 @@ # FetchContent Options ################################################################################ -# option(FETCHCONTENT_FULLY_DISCONNECTED "Disables all attempts to download or update content and assumes source dirs already exist") -# option(FETCHCONTENT_UPDATES_DISCONNECTED "Enables UPDATE_DISCONNECTED behavior for all content population") +# option(FETCHCONTENT_FULLY_DISCONNECTED "Disables all attempts to download or update content and assumes source dirs already exist" ON) +# option(FETCHCONTENT_UPDATES_DISCONNECTED "Enables UPDATE_DISCONNECTED behavior for all content population" ON) # option(FETCHCONTENT_QUIET "Enables QUIET option for all content population" ON) # set(FETCHCONTENT_BASE_DIR "${CMAKE_BINARY_DIR}/_deps" CACHE PATH "Directory under which to collect all populated content") diff --git a/cmake/lagrange/lagrange_add_example.cmake b/cmake/lagrange/lagrange_add_example.cmake index 8dbf09fb..60fdbcb8 100644 --- a/cmake/lagrange/lagrange_add_example.cmake +++ b/cmake/lagrange/lagrange_add_example.cmake @@ -14,7 +14,7 @@ function(lagrange_add_example name) lagrange_add_executable(${name} ${ARGN}) # Folder in IDE - set_target_properties(${name} PROPERTIES FOLDER "Lagrange//Examples") + set_target_properties(${name} PROPERTIES FOLDER "${LAGRANGE_IDE_PREFIX}Lagrange//Examples") # Output directory on disk set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/examples") diff --git a/cmake/lagrange/lagrange_add_module.cmake b/cmake/lagrange/lagrange_add_module.cmake new file mode 100644 index 00000000..8bab5210 --- /dev/null +++ b/cmake/lagrange/lagrange_add_module.cmake @@ -0,0 +1,50 @@ +# +# Copyright 2021 Adobe. All rights reserved. +# This file is licensed to you 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 REPRESENTATIONS +# OF ANY KIND, either express or implied. See the License for the specific language +# governing permissions and limitations under the License. +# +function(lagrange_add_module) + # Retrieve module name + get_filename_component(module_path "${CMAKE_CURRENT_SOURCE_DIR}" REALPATH) + get_filename_component(module_name "${module_path}" NAME) + + # Retrieve options + set(options INTERFACE) + set(oneValueArgs "") + set(multiValueArgs "") + cmake_parse_arguments(OPTIONS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + # Define module + if(OPTIONS_INTERFACE) + add_library(lagrange_${module_name} INTERFACE) + set(module_scope INTERFACE) + else() + add_library(lagrange_${module_name}) + set(module_scope PUBLIC) + endif() + add_library(lagrange::${module_name} ALIAS lagrange_${module_name}) + message(STATUS "Lagrange: creating target 'lagrange::${module_name}'") + + include(GNUInstallDirs) + target_include_directories(lagrange_${module_name} ${module_scope} + $ + $ + ) + + # Target sources + file(GLOB_RECURSE INC_FILES "include/*.h") + file(GLOB_RECURSE SRC_FILES "src/*.cpp") + source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/include/" PREFIX "Header Files" FILES ${INC_FILES}) + source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src/" PREFIX "Source Files" FILES ${SRC_FILES}) + target_sources(lagrange_${module_name} PRIVATE ${INC_FILES} ${SRC_FILES}) + + # Target folder for IDE + set_target_properties(lagrange_${module_name} PROPERTIES FOLDER "${LAGRANGE_IDE_PREFIX}Lagrange") + +endfunction() diff --git a/cmake/lagrange/lagrange_add_performance.cmake b/cmake/lagrange/lagrange_add_performance.cmake index 94d95b58..52bca644 100644 --- a/cmake/lagrange/lagrange_add_performance.cmake +++ b/cmake/lagrange/lagrange_add_performance.cmake @@ -14,7 +14,7 @@ function(lagrange_add_performance name) lagrange_add_executable(${name} ${ARGN}) # Folder in IDE - set_target_properties(${name} PROPERTIES FOLDER "Lagrange//Performance") + set_target_properties(${name} PROPERTIES FOLDER "${LAGRANGE_IDE_PREFIX}Lagrange//Performance") # Output directory on disk set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/performance") diff --git a/cmake/lagrange/lagrange_add_test.cmake b/cmake/lagrange/lagrange_add_test.cmake index ccce0447..9e970444 100644 --- a/cmake/lagrange/lagrange_add_test.cmake +++ b/cmake/lagrange/lagrange_add_test.cmake @@ -19,7 +19,7 @@ function(lagrange_add_test) include(lagrange_add_executable) set(test_target "test_lagrange_${module_name}") lagrange_add_executable(${test_target} ${SRC_FILES}) - set_target_properties(${test_target} PROPERTIES FOLDER "Lagrange//Tests") + set_target_properties(${test_target} PROPERTIES FOLDER "${LAGRANGE_IDE_PREFIX}Lagrange//Tests") # Dependencies lagrange_include_modules(testing) diff --git a/cmake/lagrange/lagrange_include_modules.cmake b/cmake/lagrange/lagrange_include_modules.cmake index 6a4ce989..d8924e2c 100644 --- a/cmake/lagrange/lagrange_include_modules.cmake +++ b/cmake/lagrange/lagrange_include_modules.cmake @@ -25,11 +25,5 @@ function(lagrange_include_modules) if(NOT TARGET lagrange::${name}) message(FATAL_ERROR "Failed to create lagrange module: ${name}") endif() - - if(TARGET lagrange_${name}_) - set_target_properties(lagrange_${name}_ PROPERTIES FOLDER "Lagrange") - elseif(TARGET lagrange_${name}) - set_target_properties(lagrange_${name} PROPERTIES FOLDER "Lagrange") - endif() endforeach() endfunction() diff --git a/cmake/lagrange/lagrange_limit_parallelism.cmake b/cmake/lagrange/lagrange_limit_parallelism.cmake new file mode 100644 index 00000000..d672e86a --- /dev/null +++ b/cmake/lagrange/lagrange_limit_parallelism.cmake @@ -0,0 +1,99 @@ +# +# Copyright 2021 Adobe. All rights reserved. +# This file is licensed to you 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 REPRESENTATIONS +# OF ANY KIND, either express or implied. See the License for the specific language +# governing permissions and limitations under the License. +# +function(lagrange_limit_parallelism) + # Query system information + cmake_host_system_information(RESULT NUMBER_OF_PHYSICAL_CORES QUERY NUMBER_OF_PHYSICAL_CORES) + cmake_host_system_information(RESULT AVAILABLE_PHYSICAL_MEMORY QUERY AVAILABLE_PHYSICAL_MEMORY) + cmake_host_system_information(RESULT AVAILABLE_VIRTUAL_MEMORY QUERY AVAILABLE_VIRTUAL_MEMORY) + cmake_host_system_information(RESULT TOTAL_VIRTUAL_MEMORY QUERY TOTAL_VIRTUAL_MEMORY) + cmake_host_system_information(RESULT TOTAL_PHYSICAL_MEMORY QUERY TOTAL_PHYSICAL_MEMORY) + + # Peak memory computed "manually" for each platform (in MB) + # Use a hard coded limit of 2 parallel linking jobs + set(max_rss_linux_debug 3000) + set(max_rss_linux_release 2100) + set(max_rss_darwin_debug 729) + set(max_rss_darwin_release 395) + set(max_rss_windows_debug 2100) + set(max_rss_windows_release 1300) + + # Use "release" limit only for matching single-config mode + if(GENERATOR_IS_MULTI_CONFIG OR NOT DEFINED CMAKE_BUILD_TYPE) + message(STATUS "Defaulting to debug") + set(_postfix "debug") + else() + string(TOLOWER ${CMAKE_BUILD_TYPE} _type) + if(_type STREQUAL release) + set(_postfix "release") + else() + set(_postfix "debug") + endif() + endif() + + string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} _system) + + # Use a 3/2 factor safety margin compared to observed memory usage + math(EXPR num_cpu_memory "${AVAILABLE_PHYSICAL_MEMORY} * 3 / 2 / ${max_rss_${_system}_${_postfix}}") + + # Compute limits for link/compile steps + set(num_cpu_link 2) + set(num_cpu_compile ${NUMBER_OF_PHYSICAL_CORES}) + if(num_cpu_link GREATER num_cpu_memory) + set(num_cpu_link ${num_cpu_memory}) + endif() + if(num_cpu_compile GREATER num_cpu_memory) + set(num_cpu_compile ${num_cpu_memory}) + endif() + + if(CMAKE_SCRIPT_MODE_FILE) + # The message() command, without any mode, will print to stderr. But jenkins only allows us + # to capture stdout. To print a clean message without hyphens, we use cmake's echo command. + execute_process(COMMAND ${CMAKE_COMMAND} -E echo "${num_cpu_compile}") + else() + message(STATUS "Parallelism: Available physical memory: ${AVAILABLE_PHYSICAL_MEMORY} / ${TOTAL_PHYSICAL_MEMORY}") + message(STATUS "Parallelism: Available virtual memory: ${AVAILABLE_VIRTUAL_MEMORY} / ${TOTAL_VIRTUAL_MEMORY}") + message(STATUS "Parallelism: Number of physical cores: ${NUMBER_OF_PHYSICAL_CORES}") + message(STATUS "Parallelism: Limiting link pool to ${num_cpu_link}") + message(STATUS "Parallelism: Limiting compile pool to ${num_cpu_compile}") + endif() + + # Limit parallelism based on number of physical cores + available memory. + set_property(GLOBAL PROPERTY JOB_POOLS + pool-link=${num_cpu_link} + pool-compile=${num_cpu_compile} + pool-precompile-header=${num_cpu_compile} + ) + set(CMAKE_JOB_POOL_LINK "pool-link" CACHE STRING "Job pool for linking" FORCE) + set(CMAKE_JOB_POOL_COMPILE "pool-compile" CACHE STRING "Job pool for compiling" FORCE) + set(CMAKE_JOB_POOL_PRECOMPILE_HEADER "pool-precompile-header" CACHE STRING "Job pool for generating pre-compiled headers" FORCE) + + # Note: We cannot set directly CMAKE_BUILD_PARALLEL_LEVEL or CTEST_PARALLEL_LEVEL from this CMake file, + # since those are environment variables [1]: they are not cached and do not affect subsequent CMake calls. + # In practice, the parallelism for Ninja should be limited by our job pools, so the only thing we need is + # to run ctest in parallel. + # [1]: https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#cmake-language-environment-variables +endfunction() + +# If this file is run in script mode, calling this function will simply echo the total number of +# cores that we desire to build/test with. +if(CMAKE_SCRIPT_MODE_FILE) + if(DEFINED CMAKE_ARGV3) + # We need to extract build type from preset + execute_process(COMMAND ${CMAKE_COMMAND} --preset ${CMAKE_ARGV3} -N OUTPUT_VARIABLE output_log COMMAND_ERROR_IS_FATAL ANY) + if(output_log MATCHES "CMAKE_BUILD_TYPE=\"([a-zA-Z]+)\"") + set(CMAKE_BUILD_TYPE ${CMAKE_MATCH_1}) + else() + message(FATAL_ERROR "Could not find CMAKE_BUILD_TYPE from preset") + endif() + endif() + lagrange_limit_parallelism() +endif() diff --git a/cmake/lagrange/lagrange_runtime_dependencies.cmake b/cmake/lagrange/lagrange_runtime_dependencies.cmake index e6a2a81c..45ae4363 100644 --- a/cmake/lagrange/lagrange_runtime_dependencies.cmake +++ b/cmake/lagrange/lagrange_runtime_dependencies.cmake @@ -19,17 +19,23 @@ function(lagrange_get_dependencies_recursive OUTPUT_VARIABLE TARGET) get_target_property(_IMPORTED ${TARGET} IMPORTED) get_target_property(_TYPE ${TARGET} TYPE) - if(_IMPORTED OR (${_TYPE} STREQUAL "INTERFACE_LIBRARY")) - get_target_property(TARGET_DEPENDENCIES ${TARGET} INTERFACE_LINK_LIBRARIES) - else() - get_target_property(TARGET_DEPENDENCIES ${TARGET} LINK_LIBRARIES) - endif() - # MKL-specific list of runtime dependencies - get_property(RUNTIME_DEPENDENCIES TARGET ${TARGET} PROPERTY mkl_RUNTIME_DEPENDENCIES) - if(RUNTIME_DEPENDENCIES) - list(APPEND TARGET_DEPENDENCIES ${RUNTIME_DEPENDENCIES}) + # List both public and private dependencies, regardless of target type + unset(TARGET_DEPENDENCIES) + set(properties + INTERFACE_LINK_LIBRARIES + mkl_RUNTIME_DEPENDENCIES # MKL-specific list of runtime dependencies + ) + if(NOT ${_TYPE} STREQUAL "INTERFACE_LIBRARY") + list(APPEND properties LINK_LIBRARIES) endif() + foreach(property IN ITEMS ${properties}) + get_target_property(deps ${TARGET} ${property}) + if(deps) + list(APPEND TARGET_DEPENDENCIES ${deps}) + endif() + endforeach() + list(REMOVE_DUPLICATES TARGET_DEPENDENCIES) set(VISITED_TARGETS ${${OUTPUT_VARIABLE}}) foreach(DEPENDENCY IN ITEMS ${TARGET_DEPENDENCIES}) diff --git a/cmake/recipes/external/boost.cmake b/cmake/recipes/external/boost.cmake index d217f3dd..6c1e2be3 100644 --- a/cmake/recipes/external/boost.cmake +++ b/cmake/recipes/external/boost.cmake @@ -38,3 +38,9 @@ endif() set(CMAKE_POSITION_INDEPENDENT_CODE ${OLD_CMAKE_POSITION_INDEPENDENT_CODE}) set(CMAKE_CXX_FLAGS "${PREVIOUS_CMAKE_CXX_FLAGS}") + +foreach(name IN ITEMS chrono container iostreams system thread timer) + if(TARGET Boost_${name}) + set_target_properties(Boost_${name} PROPERTIES FOLDER third_party/boost) + endif() +endforeach() diff --git a/cmake/recipes/external/eigen.cmake b/cmake/recipes/external/eigen.cmake index a04001fd..f602cc54 100644 --- a/cmake/recipes/external/eigen.cmake +++ b/cmake/recipes/external/eigen.cmake @@ -24,7 +24,7 @@ else() include(FetchContent) FetchContent_Declare( eigen - GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git + GIT_REPOSITORY https://github.com/libigl/eigen-git-mirror.git GIT_TAG tags/3.3.7 GIT_SHALLOW TRUE ) diff --git a/cmake/recipes/external/ilmbase.cmake b/cmake/recipes/external/ilmbase.cmake index 1d9bc7d9..560cc654 100644 --- a/cmake/recipes/external/ilmbase.cmake +++ b/cmake/recipes/external/ilmbase.cmake @@ -49,3 +49,5 @@ target_include_directories(IlmBase_Half PUBLIC ) target_compile_features(IlmBase_Half PUBLIC cxx_std_14) + +set_target_properties(IlmBase_Half PROPERTIES FOLDER third_party) diff --git a/cmake/recipes/external/mdl.cmake b/cmake/recipes/external/mdl.cmake index 2dfe784a..1f710b58 100644 --- a/cmake/recipes/external/mdl.cmake +++ b/cmake/recipes/external/mdl.cmake @@ -23,8 +23,6 @@ set(MDL_FOLDER "${FETCHCONTENT_BASE_DIR}/mdl/${MDL_VERSION}") set(MDL_ARCHIVE "${MDL_FOLDER}/archive.zip") if(NOT EXISTS ${MDL_FOLDER}) - unset(MDL_DYNAMIC_LIBS CACHE) - file(MAKE_DIRECTORY ${MDL_FOLDER}) if(NOT EXISTS ${MDL_ARCHIVE}) @@ -53,16 +51,51 @@ if(NOT EXISTS ${MDL_FOLDER}) file(REMOVE ${MDL_ARCHIVE}) endif() +unset(MDL_PREFIX) +unset(MDL_SUFFIX) if(WIN32) - file(GLOB __MDL_DYNAMIC_LIBS "${MDL_FOLDER}/nt-x86-64/lib/*.dll") + set(MDL_PREFIX "nt-x86-64") + set(MDL_SUFFIX ".dll") elseif(APPLE) - file(GLOB __MDL_DYNAMIC_LIBS "${MDL_FOLDER}/macosx-x86-64/lib/*.so") + set(MDL_PREFIX "macosx-x86-64") + set(MDL_SUFFIX ".so") elseif(UNIX) - file(GLOB __MDL_DYNAMIC_LIBS "${MDL_FOLDER}/linux-x86-64/lib/*.so") + set(MDL_PREFIX "linux-x86-64") + set(MDL_SUFFIX ".so") endif() -set(MDL_DYNAMIC_LIBS ${__MDL_DYNAMIC_LIBS} CACHE STRING "MDL_DYNAMIC_LIBS") +# Creates one IMPORTED target for each requested library +function(mdl_add_imported_library name) + # Create imported target for library + add_library(mdl::${name} MODULE IMPORTED GLOBAL) + set_target_properties(mdl::${name} PROPERTIES IMPORTED_LINK_INTERFACE_LANGUAGES CXX) + + # Important to avoid absolute path referencing of the libraries in the executables + set_target_properties(mdl::${name} PROPERTIES IMPORTED_NO_SONAME TRUE) + + # Find library file + string(TOUPPER mdl_${name}_lib LIBVAR) + find_file(${LIBVAR} + NAMES ${name}${MDL_SUFFIX} + HINTS "${MDL_FOLDER}/${MDL_PREFIX}/lib" + NO_DEFAULT_PATH + REQUIRED + ) + message(STATUS "Creating target mdl::${name} for file: ${${LIBVAR}}") + + # Set imported location + set_target_properties(mdl::${name} PROPERTIES + IMPORTED_LOCATION "${${LIBVAR}}" + ) + + # Add as dependency to the meta target mdl::mdl + target_link_libraries(mdl::mdl INTERFACE mdl::${name}) +endfunction() add_library(mdl::mdl INTERFACE IMPORTED GLOBAL) -message(STATUS "MDL Path: ${MDL_FOLDER}") +message(STATUS "MDL include dir: ${MDL_FOLDER}/include") target_include_directories(mdl::mdl SYSTEM INTERFACE ${MDL_FOLDER}/include) + +foreach(name IN ITEMS dds libmdl_sdk nv_freeimage) + mdl_add_imported_library(${name}) +endforeach() diff --git a/cmake/recipes/external/mkl-2020.4.cmake b/cmake/recipes/external/mkl-2020.4.cmake deleted file mode 100644 index 82abf84d..00000000 --- a/cmake/recipes/external/mkl-2020.4.cmake +++ /dev/null @@ -1,41 +0,0 @@ -# -# Copyright 2021 Adobe. All rights reserved. -# This file is licensed to you 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 REPRESENTATIONS -# OF ANY KIND, either express or implied. See the License for the specific language -# governing permissions and limitations under the License. -# - -# To compute the md5 checksums for each lib, use the following bash script (replace the target version number): -# for f in mkl mkl-include mkl-static mkl-devel; do for os in linux osx win; do cat <(printf "$f-$os-64-md5") <(conda search --override-channel --channel intel $f=2020.4 --platform $os-64 -i | grep md5 | cut -d : -f 2); done; done -set(mkl-linux-64-md5 f85891f97a04c7b2fbf619d1b903d7f5) -set(mkl-osx-64-md5 735d7f93c7fbcffe658f1ccf67418cb3) -set(mkl-win-64-md5 37ade09cace5cd73053b16574a3ee3c3) -set(mkl-include-linux-64-md5 8b2bf0e42bd95dd700d9877add1ca6de) -set(mkl-include-osx-64-md5 26043328553952cdb064c5aab8b50c78) -set(mkl-include-win-64-md5 87e9a73a6e6757a8ed0dbc87d50d7f60) -set(mkl-static-linux-64-md5 9f589a1508fb083c3e73427db459ca4c) -set(mkl-static-osx-64-md5 2f9e1b8b6d6b0903e81a573084e4494f) -set(mkl-static-win-64-md5 5ae780c06edd0be62966c6d8ab47d5fb) -set(mkl-devel-linux-64-md5 b571698ef237c0e61abe15b7d300f157) -set(mkl-devel-osx-64-md5 ee58da0463676d910eeab9aec0470f0e) -set(mkl-devel-win-64-md5 8a7736b81b9bc2d5c044b88d6ac8af6e) - -# To compute file names, we use the following bash script: -# for f in mkl mkl-include mkl-static mkl-devel; do for os in linux osx win; do cat <(printf "$f-$os-64-file") <(conda search --override-channel --channel intel $f=2020.4 --platform $os-64 -i | grep file | cut -d : -f 2); done; done -set(mkl-linux-64-file mkl-2020.4-intel_304.tar.bz2) -set(mkl-osx-64-file mkl-2020.4-intel_301.tar.bz2) -set(mkl-win-64-file mkl-2020.4-intel_311.tar.bz2) -set(mkl-include-linux-64-file mkl-include-2020.4-intel_304.tar.bz2) -set(mkl-include-osx-64-file mkl-include-2020.4-intel_301.tar.bz2) -set(mkl-include-win-64-file mkl-include-2020.4-intel_311.tar.bz2) -set(mkl-static-linux-64-file mkl-static-2020.4-intel_304.tar.bz2) -set(mkl-static-osx-64-file mkl-static-2020.4-intel_301.tar.bz2) -set(mkl-static-win-64-file mkl-static-2020.4-intel_311.tar.bz2) -set(mkl-devel-linux-64-file mkl-devel-2020.4-intel_304.tar.bz2) -set(mkl-devel-osx-64-file mkl-devel-2020.4-intel_301.tar.bz2) -set(mkl-devel-win-64-file mkl-devel-2020.4-intel_311.tar.bz2) diff --git a/cmake/recipes/external/mkl-2021.3.0.cmake b/cmake/recipes/external/mkl-2021.3.0.cmake deleted file mode 100644 index 2c8d8b88..00000000 --- a/cmake/recipes/external/mkl-2021.3.0.cmake +++ /dev/null @@ -1,41 +0,0 @@ -# -# Copyright 2021 Adobe. All rights reserved. -# This file is licensed to you 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 REPRESENTATIONS -# OF ANY KIND, either express or implied. See the License for the specific language -# governing permissions and limitations under the License. -# - -# To compute the md5 checksums for each lib, use the following bash script (replace the target version number): -# for f in mkl mkl-include mkl-static mkl-devel; do for os in linux osx win; do cat <(printf "$f-$os-64-md5") <(conda search --override-channel --channel intel $f=2021.3.0 --platform $os-64 -i | grep md5 | cut -d : -f 2); done; done -set(mkl-linux-64-md5 2501643729c00b24fddb9530b339aea7) -set(mkl-osx-64-md5 d6129ae9dfba58671667a65c160d0776) -set(mkl-win-64-md5 264213ea4c5cb6b6d81ea97f59e757ab) -set(mkl-include-linux-64-md5 70b4f9a53401a3d11ce27d7ddb0e2511) -set(mkl-include-osx-64-md5 6da50c06992b78c4127a1881d39c1804) -set(mkl-include-win-64-md5 28d785eb22d28512d4e40e5890a817dc) -set(mkl-static-linux-64-md5 1469ad60a34269d4d0c5666bc131b82a) -set(mkl-static-osx-64-md5 4a099581ba95cc50bb538598b26389e4) -set(mkl-static-win-64-md5 69aef10428893314bc486e81397e1b25) -set(mkl-devel-linux-64-md5 2432ad963e3f7e4619ffc7f896178fbe) -set(mkl-devel-osx-64-md5 61b84a60715a3855a2097a3b619a00c8) -set(mkl-devel-win-64-md5 6128dee67d2b20ff534cf54757f623e0) - -# To compute file names, we use the following bash script: -# for f in mkl mkl-include mkl-static mkl-devel; do for os in linux osx win; do cat <(printf "$f-$os-64-file") <(conda search --override-channel --channel intel $f=2021.3.0 --platform $os-64 -i | grep file | cut -d : -f 2); done; done -set(mkl-linux-64-file mkl-2021.3.0-intel_520.tar.bz2) -set(mkl-osx-64-file mkl-2021.3.0-intel_517.tar.bz2) -set(mkl-win-64-file mkl-2021.3.0-intel_524.tar.bz2) -set(mkl-include-linux-64-file mkl-include-2021.3.0-intel_520.tar.bz2) -set(mkl-include-osx-64-file mkl-include-2021.3.0-intel_517.tar.bz2) -set(mkl-include-win-64-file mkl-include-2021.3.0-intel_524.tar.bz2) -set(mkl-static-linux-64-file mkl-static-2021.3.0-intel_520.tar.bz2) -set(mkl-static-osx-64-file mkl-static-2021.3.0-intel_517.tar.bz2) -set(mkl-static-win-64-file mkl-static-2021.3.0-intel_524.tar.bz2) -set(mkl-devel-linux-64-file mkl-devel-2021.3.0-intel_520.tar.bz2) -set(mkl-devel-osx-64-file mkl-devel-2021.3.0-intel_517.tar.bz2) -set(mkl-devel-win-64-file mkl-devel-2021.3.0-intel_524.tar.bz2) diff --git a/cmake/recipes/external/mkl.cmake b/cmake/recipes/external/mkl.cmake index a4fbad9d..451af31e 100644 --- a/cmake/recipes/external/mkl.cmake +++ b/cmake/recipes/external/mkl.cmake @@ -78,8 +78,67 @@ elseif(UNIX) set(MKL_PLATFORM linux-64) endif() -# Include pre-computed md5 checksums -include(mkl-${MKL_VERSION}) +if(MKL_VERSION VERSION_EQUAL 2020.4) + # To compute the md5 checksums for each lib, use the following bash script (replace the target version number): + # for f in mkl mkl-include mkl-static mkl-devel; do for os in linux osx win; do cat <(printf "$f-$os-64-md5") <(conda search --override-channel --channel intel $f=2020.4 --platform $os-64 -i | grep md5 | cut -d : -f 2); done; done + set(mkl-linux-64-md5 f85891f97a04c7b2fbf619d1b903d7f5) + set(mkl-osx-64-md5 735d7f93c7fbcffe658f1ccf67418cb3) + set(mkl-win-64-md5 37ade09cace5cd73053b16574a3ee3c3) + set(mkl-include-linux-64-md5 8b2bf0e42bd95dd700d9877add1ca6de) + set(mkl-include-osx-64-md5 26043328553952cdb064c5aab8b50c78) + set(mkl-include-win-64-md5 87e9a73a6e6757a8ed0dbc87d50d7f60) + set(mkl-static-linux-64-md5 9f589a1508fb083c3e73427db459ca4c) + set(mkl-static-osx-64-md5 2f9e1b8b6d6b0903e81a573084e4494f) + set(mkl-static-win-64-md5 5ae780c06edd0be62966c6d8ab47d5fb) + set(mkl-devel-linux-64-md5 b571698ef237c0e61abe15b7d300f157) + set(mkl-devel-osx-64-md5 ee58da0463676d910eeab9aec0470f0e) + set(mkl-devel-win-64-md5 8a7736b81b9bc2d5c044b88d6ac8af6e) + + # To compute file names, we use the following bash script: + # for f in mkl mkl-include mkl-static mkl-devel; do for os in linux osx win; do cat <(printf "$f-$os-64-file") <(conda search --override-channel --channel intel $f=2020.4 --platform $os-64 -i | grep file | cut -d : -f 2); done; done + set(mkl-linux-64-file mkl-2020.4-intel_304.tar.bz2) + set(mkl-osx-64-file mkl-2020.4-intel_301.tar.bz2) + set(mkl-win-64-file mkl-2020.4-intel_311.tar.bz2) + set(mkl-include-linux-64-file mkl-include-2020.4-intel_304.tar.bz2) + set(mkl-include-osx-64-file mkl-include-2020.4-intel_301.tar.bz2) + set(mkl-include-win-64-file mkl-include-2020.4-intel_311.tar.bz2) + set(mkl-static-linux-64-file mkl-static-2020.4-intel_304.tar.bz2) + set(mkl-static-osx-64-file mkl-static-2020.4-intel_301.tar.bz2) + set(mkl-static-win-64-file mkl-static-2020.4-intel_311.tar.bz2) + set(mkl-devel-linux-64-file mkl-devel-2020.4-intel_304.tar.bz2) + set(mkl-devel-osx-64-file mkl-devel-2020.4-intel_301.tar.bz2) + set(mkl-devel-win-64-file mkl-devel-2020.4-intel_311.tar.bz2) +elseif(MKL_VERSION VERSION_EQUAL 2021.3.0) + # To compute the md5 checksums for each lib, use the following bash script (replace the target version number): + # for f in mkl mkl-include mkl-static mkl-devel; do for os in linux osx win; do cat <(printf "$f-$os-64-md5") <(conda search --override-channel --channel intel $f=2021.3.0 --platform $os-64 -i | grep md5 | cut -d : -f 2); done; done + set(mkl-linux-64-md5 2501643729c00b24fddb9530b339aea7) + set(mkl-osx-64-md5 d6129ae9dfba58671667a65c160d0776) + set(mkl-win-64-md5 264213ea4c5cb6b6d81ea97f59e757ab) + set(mkl-include-linux-64-md5 70b4f9a53401a3d11ce27d7ddb0e2511) + set(mkl-include-osx-64-md5 6da50c06992b78c4127a1881d39c1804) + set(mkl-include-win-64-md5 28d785eb22d28512d4e40e5890a817dc) + set(mkl-static-linux-64-md5 1469ad60a34269d4d0c5666bc131b82a) + set(mkl-static-osx-64-md5 4a099581ba95cc50bb538598b26389e4) + set(mkl-static-win-64-md5 69aef10428893314bc486e81397e1b25) + set(mkl-devel-linux-64-md5 2432ad963e3f7e4619ffc7f896178fbe) + set(mkl-devel-osx-64-md5 61b84a60715a3855a2097a3b619a00c8) + set(mkl-devel-win-64-md5 6128dee67d2b20ff534cf54757f623e0) + + # To compute file names, we use the following bash script: + # for f in mkl mkl-include mkl-static mkl-devel; do for os in linux osx win; do cat <(printf "$f-$os-64-file") <(conda search --override-channel --channel intel $f=2021.3.0 --platform $os-64 -i | grep file | cut -d : -f 2); done; done + set(mkl-linux-64-file mkl-2021.3.0-intel_520.tar.bz2) + set(mkl-osx-64-file mkl-2021.3.0-intel_517.tar.bz2) + set(mkl-win-64-file mkl-2021.3.0-intel_524.tar.bz2) + set(mkl-include-linux-64-file mkl-include-2021.3.0-intel_520.tar.bz2) + set(mkl-include-osx-64-file mkl-include-2021.3.0-intel_517.tar.bz2) + set(mkl-include-win-64-file mkl-include-2021.3.0-intel_524.tar.bz2) + set(mkl-static-linux-64-file mkl-static-2021.3.0-intel_520.tar.bz2) + set(mkl-static-osx-64-file mkl-static-2021.3.0-intel_517.tar.bz2) + set(mkl-static-win-64-file mkl-static-2021.3.0-intel_524.tar.bz2) + set(mkl-devel-linux-64-file mkl-devel-2021.3.0-intel_520.tar.bz2) + set(mkl-devel-osx-64-file mkl-devel-2021.3.0-intel_517.tar.bz2) + set(mkl-devel-win-64-file mkl-devel-2021.3.0-intel_524.tar.bz2) +endif() # On Windows, `mkl-devel` contains the .lib files (needed at link time), # while `mkl` contains the .dll files (needed at run time). On macOS/Linux, @@ -110,6 +169,7 @@ if(MKL_LINKING STREQUAL static) list(APPEND MKL_LIB_HINTS "${mkl-static_SOURCE_DIR}/lib" "${mkl-static_SOURCE_DIR}/Library/bin" + "${mkl-static_SOURCE_DIR}/Library/lib" ) else() if(WIN32) @@ -144,6 +204,8 @@ function(mkl_add_imported_library name) # Define type of imported library if(MKL_LINKING STREQUAL static) set(MKL_TYPE STATIC) + elseif(ARGS_NO_DLL) + set(MKL_TYPE STATIC) else() set(MKL_TYPE SHARED) endif() @@ -171,7 +233,7 @@ function(mkl_add_imported_library name) if(ARGS_NO_DLL) # Interface library only, no .dll set_target_properties(mkl::${name} PROPERTIES - IMPORTED_IMPLIB "${${LIBVAR}}" + IMPORTED_LOCATION "${${LIBVAR}}" ) else() # Find dll file and set IMPORTED_LOCATION to the .dll file @@ -243,6 +305,32 @@ function(mkl_add_shared_libraries) endforeach() endfunction() +# Set cyclic dependencies between mkl targets. CMake handles cyclic dependencies between static libraries by repeating +# them on the linker line. See [1] for more details. Note that since our targets are IMPORTED, we have to set the +# property directly rather than rely on target_link_libraries(). +# +# [1]: https://cmake.org/cmake/help/latest/command/target_link_libraries.html#cyclic-dependencies-of-static-libraries +function(mkl_set_dependencies) + # We create a strongly connected dependency graph between mkl imported static libraries. Then, we use + # IMPORTED_LINK_INTERFACE_MULTIPLICITY to tell CMake how many time each item in the connected component needs to be + # repeated on the command-line. See relevant CMake issue: + # https://gitlab.kitware.com/cmake/cmake/-/issues/17964 + # + # Note: For some reason, creating a fully connected graph between all the list items would lead CMake to *not* + # repeat the first item in the component. I had to create a proper cordless cycle to make this work. + if(MKL_LINKING STREQUAL static) + set(args_shifted ${ARGN}) + list(POP_FRONT args_shifted first_item) + list(APPEND args_shifted ${first_item}) + foreach(a b IN ZIP_LISTS ARGN args_shifted) + set_target_properties(mkl::${a} PROPERTIES INTERFACE_LINK_LIBRARIES mkl::${b}) + endforeach() + + # Manually increase until all cyclic dependencies are resolved. + set_property(TARGET mkl::${first_item} PROPERTY IMPORTED_LINK_INTERFACE_MULTIPLICITY 4) + endif() +endfunction() + ################################################################################ @@ -276,8 +364,10 @@ else() # Thread library if(MKL_THREADING STREQUAL sequential) mkl_add_imported_library(sequential) + mkl_set_dependencies(core intel_${MKL_INTERFACE} sequential) else() mkl_add_imported_library(tbb_thread) + mkl_set_dependencies(core intel_${MKL_INTERFACE} tbb_thread) endif() # Instructions sets specific libraries diff --git a/cmake/recipes/external/openvdb.cmake b/cmake/recipes/external/openvdb.cmake index 557d622f..1f392717 100644 --- a/cmake/recipes/external/openvdb.cmake +++ b/cmake/recipes/external/openvdb.cmake @@ -130,3 +130,5 @@ endfunction() # Call via a proper function in order to scope variables such as CMAKE_FIND_PACKAGE_PREFER_CONFIG and TBB_DIR openvdb_import_target() + +set_target_properties(openvdb_static PROPERTIES FOLDER third_party) diff --git a/cmake/recipes/external/tbb.cmake b/cmake/recipes/external/tbb.cmake index 72e29c45..5243d688 100644 --- a/cmake/recipes/external/tbb.cmake +++ b/cmake/recipes/external/tbb.cmake @@ -114,8 +114,19 @@ function(tbb_fix_include_dirs) endforeach() endfunction() -# Meta-target that brings both tbb and tbbmalloc -add_library(tbb_tbb INTERFACE) +# Generate a dummy .cpp so we can create a "real" target that depends on both tbb and tbbmalloc +# This is needed to trick embree 3.13.0 on Windows, which tries to access the location of the target TBB::tbb +# https://github.com/embree/embree/blob/12b99393438a4cc9e478e33459eed78bec6233fd/common/tasking/CMakeLists.txt#L42 +file(WRITE "${tbb_BINARY_DIR}/tbb_dummy.cpp.in" [[ + namespace { + void dummy() { } + } +]]) + +configure_file(${tbb_BINARY_DIR}/tbb_dummy.cpp.in ${tbb_BINARY_DIR}/tbb_dummy.cpp COPYONLY) + +# Meta-target that brings both tbb and tbbmalloc. +add_library(tbb_tbb ${tbb_BINARY_DIR}/tbb_dummy.cpp) add_library(TBB::tbb ALIAS tbb_tbb) if(TBB_PREFER_STATIC) target_link_libraries(tbb_tbb INTERFACE tbb_static tbbmalloc_static) diff --git a/modules/core/CMakeLists.txt b/modules/core/CMakeLists.txt index 306160df..a6ab1d63 100644 --- a/modules/core/CMakeLists.txt +++ b/modules/core/CMakeLists.txt @@ -11,44 +11,40 @@ # # 1. define module -add_library(lagrange_core) -add_library(lagrange::core ALIAS lagrange_core) -message(STATUS "Lagrange: creating target 'lagrange::core'") - -include(GNUInstallDirs) -target_include_directories(lagrange_core PUBLIC - $ - $ -) - +lagrange_add_module() target_compile_features(lagrange_core PUBLIC cxx_std_17) set_target_properties(lagrange_core PROPERTIES POSITION_INDEPENDENT_CODE ON) -option(LAGRANGE_KEEP_TRANSITION_CODE "Keep transition code kept for backward compatibility with the old edge API." ON) -if(LAGRANGE_KEEP_TRANSITION_CODE) - target_compile_definitions(lagrange_core PUBLIC -DLA_KEEP_TRANSITION_CODE) -endif() - option(LAGRANGE_DISABLE_FPE "Disable floating point exception code" OFF) if(LAGRANGE_DISABLE_FPE) target_compile_definitions(lagrange_core PRIVATE -DLA_DISABLE_FPE) else() # Try to compile fpe.cpp, and disable if not supported (e.g. emscripten, apple m1, etc.) - try_compile(LAGRANGE_SUPPORT_FPE ${CMAKE_CURRENT_BINARY_DIR}/lagrange_fpe ${CMAKE_CURRENT_LIST_DIR}/src/fpe.cpp) + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/lagrange_fpe_main.cpp.in" [[ + int main(void) { + return 0; + } + ]]) + configure_file( + ${CMAKE_CURRENT_BINARY_DIR}/lagrange_fpe_main.cpp.in + ${CMAKE_CURRENT_BINARY_DIR}/lagrange_fpe_main.cpp + COPYONLY + ) + try_compile( + LAGRANGE_SUPPORT_FPE + ${CMAKE_CURRENT_BINARY_DIR}/lagrange_fpe + SOURCES + ${CMAKE_CURRENT_LIST_DIR}/src/fpe.cpp + ${CMAKE_CURRENT_BINARY_DIR}/lagrange_fpe_main.cpp + ) if(NOT LAGRANGE_SUPPORT_FPE) message(WARNING "Lagrange: disabling unsupported FPE code") target_compile_definitions(lagrange_core PRIVATE -DLA_DISABLE_FPE) endif() endif() -# 2. target sources -file(GLOB_RECURSE INC_FILES "include/*.h") -file(GLOB_RECURSE SRC_FILES "src/*.cpp") -source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/include/" FILES ${INC_FILES}) -target_sources(lagrange_core PRIVATE ${INC_FILES} ${SRC_FILES}) - -# 3. dependencies +# 2. dependencies include(eigen) include(libigl) include(spdlog) @@ -69,9 +65,8 @@ endif() # precompile headers target. Link to this target in tests or modules with # lots of compilation units (.cpp files) to speed up compilation time! if(LAGRANGE_USE_PCH) - add_library(lagrange_shared_pch INTERFACE) - add_library(lagrange::shared_pch ALIAS lagrange_shared_pch) - target_precompile_headers(lagrange_shared_pch INTERFACE + add_library(lagrange_core_pch INTERFACE) + target_precompile_headers(lagrange_core_pch INTERFACE # This list should contain files that are used often, ideally by # at least a third of the compilation units (directly or indirectly). @@ -102,12 +97,9 @@ if(LAGRANGE_USE_PCH) # Third party libraries - ) - - add_library(lagrange_core_pch INTERFACE) - add_library(lagrange::core_pch ALIAS lagrange_core_pch) - target_link_libraries(lagrange_core_pch INTERFACE lagrange_shared_pch) - target_precompile_headers(lagrange_core_pch INTERFACE + + # Lagrange core + @@ -116,13 +108,13 @@ if(LAGRANGE_USE_PCH) ) endif() -# 4. installation +# 3. installation if(LAGRANGE_INSTALL) set_target_properties(lagrange_core PROPERTIES EXPORT_NAME core) lagrange_install(lagrange_core) endif() -# 5. unit tests and examples +# 4. unit tests and examples if(LAGRANGE_UNIT_TESTS) add_subdirectory(tests) endif() diff --git a/modules/core/include/lagrange/Mesh.h b/modules/core/include/lagrange/Mesh.h index ab98e886..3c0ba724 100644 --- a/modules/core/include/lagrange/Mesh.h +++ b/modules/core/include/lagrange/Mesh.h @@ -1071,4 +1071,13 @@ void serialize(Mesh<_VertexArray, _FacetArray>& mesh, Archive& ar) mesh.serialize_impl(ar); } +extern template class Mesh; +extern template class Mesh; +extern template class Mesh; +extern template class Mesh; +extern template class Mesh; +extern template class Mesh; +extern template class Mesh; +extern template class Mesh; + } // namespace lagrange diff --git a/modules/core/include/lagrange/create_mesh.h b/modules/core/include/lagrange/create_mesh.h index 15c73cab..a55cdeeb 100644 --- a/modules/core/include/lagrange/create_mesh.h +++ b/modules/core/include/lagrange/create_mesh.h @@ -148,11 +148,10 @@ void wrap_with_mesh( } -std::unique_ptr> create_cube(); +std::unique_ptr create_cube(); -std::unique_ptr> create_quad(const bool with_center_vertex); +std::unique_ptr create_quad(bool with_center_vertex); -std::unique_ptr> create_sphere( - typename Triangles::Scalar refine_order = 2); +std::unique_ptr create_sphere(double refine_order = 2); } // namespace lagrange diff --git a/modules/core/include/lagrange/utils/geometry3d.h b/modules/core/include/lagrange/utils/geometry3d.h index 41b4b053..bafe685b 100644 --- a/modules/core/include/lagrange/utils/geometry3d.h +++ b/modules/core/include/lagrange/utils/geometry3d.h @@ -12,6 +12,7 @@ #pragma once #include +#include namespace lagrange { diff --git a/modules/core/performance/components.cpp b/modules/core/performance/components.cpp index c027aed0..0144a417 100644 --- a/modules/core/performance/components.cpp +++ b/modules/core/performance/components.cpp @@ -15,6 +15,7 @@ #include #include #include +#include int main(int argc, char** argv) { diff --git a/modules/core/performance/connectivity.cpp b/modules/core/performance/connectivity.cpp index 44a39108..955a22c2 100644 --- a/modules/core/performance/connectivity.cpp +++ b/modules/core/performance/connectivity.cpp @@ -14,6 +14,7 @@ #include #include #include +#include int main(int argc, char** argv) { diff --git a/modules/core/performance/experimental/array.cpp b/modules/core/performance/experimental/array.cpp index fd56e346..e7db376e 100644 --- a/modules/core/performance/experimental/array.cpp +++ b/modules/core/performance/experimental/array.cpp @@ -10,6 +10,7 @@ * governing permissions and limitations under the License. */ #include +#include #include #include #include diff --git a/modules/core/performance/mesh_initialization.cpp b/modules/core/performance/mesh_initialization.cpp index a12119ab..1bb46998 100644 --- a/modules/core/performance/mesh_initialization.cpp +++ b/modules/core/performance/mesh_initialization.cpp @@ -14,6 +14,7 @@ #include #include #include +#include using namespace lagrange; diff --git a/modules/ui/src/shaders/util/default.frag b/modules/core/src/Mesh.cpp similarity index 61% rename from modules/ui/src/shaders/util/default.frag rename to modules/core/src/Mesh.cpp index e927d7fe..09efc5f7 100644 --- a/modules/ui/src/shaders/util/default.frag +++ b/modules/core/src/Mesh.cpp @@ -9,4 +9,18 @@ * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ -//todo, rename in VARYING to FS_IN + +#include + +namespace lagrange { + +template class Mesh; +template class Mesh; +template class Mesh; +template class Mesh; +template class Mesh; +template class Mesh; +template class Mesh; +template class Mesh; + +} diff --git a/modules/core/src/create_mesh.cpp b/modules/core/src/create_mesh.cpp index d373b6e4..11b8ecba 100644 --- a/modules/core/src/create_mesh.cpp +++ b/modules/core/src/create_mesh.cpp @@ -16,9 +16,11 @@ #include #include -std::unique_ptr> lagrange::create_cube() +namespace lagrange { + +std::unique_ptr create_cube() { - lagrange::Vertices3D vertices(8, 3); + Vertices3D vertices(8, 3); vertices.row(0) << -1, -1, -1; vertices.row(1) << 1, -1, -1; vertices.row(2) << 1, 1, -1; @@ -28,60 +30,59 @@ std::unique_ptr> lagra vertices.row(6) << 1, 1, 1; vertices.row(7) << -1, 1, 1; - lagrange::Triangles facets(12, 3); + Triangles facets(12, 3); facets << 0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 1, 2, 6, 1, 6, 5, 3, 0, 7, 7, 0, 4, 2, 3, 7, 2, 7, 6, 0, 1, 4, 4, 1, 5; - lagrange::Vertices2D uvs(14, 2); + Vertices2D uvs(14, 2); uvs << 0.25, 0.0, 0.5, 0.0, 0.0, 0.25, 0.25, 0.25, 0.5, 0.25, 0.75, 0.25, 0.0, 0.5, 0.25, 0.5, 0.5, 0.5, 0.75, 0.5, 0.25, 0.75, 0.5, 0.75, 0.25, 1.0, 0.5, 1.0; - lagrange::Triangles uv_indices(12, 3); + Triangles uv_indices(12, 3); uv_indices << 10, 13, 11, 10, 12, 13, 7, 8, 4, 7, 4, 3, 9, 5, 4, 9, 4, 8, 2, 6, 3, 3, 6, 7, 1, 0, 3, 1, 3, 4, 10, 11, 7, 7, 11, 8; - auto mesh = lagrange::create_mesh(vertices, facets); + + auto mesh = create_mesh(vertices, facets); mesh->initialize_uv(uvs, uv_indices); return mesh; } -std::unique_ptr> lagrange::create_quad( - const bool with_center_vertex) +std::unique_ptr create_quad(bool with_center_vertex) { - std::unique_ptr> mesh = nullptr; + std::unique_ptr mesh = nullptr; if (with_center_vertex) { - lagrange::Vertices3D vertices(5, 3); + Vertices3D vertices(5, 3); vertices << -1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0, 0, 0, 0; - lagrange::Triangles facets(4, 3); + Triangles facets(4, 3); facets << 0, 1, 4, 1, 2, 4, 2, 3, 4, 3, 0, 4; - mesh = lagrange::create_mesh(vertices, facets); + mesh = create_mesh(vertices, facets); } else { - lagrange::Vertices3D vertices(4, 3); + Vertices3D vertices(4, 3); vertices << -1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0; - lagrange::Triangles facets(2, 3); + Triangles facets(2, 3); facets << 0, 1, 2, 0, 2, 3; - mesh = lagrange::create_mesh(vertices, facets); + mesh = create_mesh(vertices, facets); } assert(mesh); - lagrange::compute_facet_area(*mesh); + compute_facet_area(*mesh); return mesh; } -std::unique_ptr> lagrange::create_sphere( - typename lagrange::Triangles::Scalar refine_order) +std::unique_ptr create_sphere(double refine_order) { using MeshType = TriangleMesh3D; using Index = typename MeshType::Index; - lagrange::Vertices3D vertices(12, 3); + Vertices3D vertices(12, 3); double t = (1.0 + sqrt(5.0)) / 2.0; vertices << -1, t, 0, 1, t, 0, -1, -t, 0, 1, -t, 0, 0, -1, t, 0, 1, t, 0, -1, -t, 0, 1, -t, t, 0, -1, t, 0, 1, -t, 0, -1, -t, 0, 1; - lagrange::Triangles facets(20, 3); + Triangles facets(20, 3); facets << 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11, 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8, 3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1; @@ -156,3 +157,5 @@ std::unique_ptr> lagra normalize(*mesh); return mesh; } + +} // namespace lagrange diff --git a/modules/core/src/predicates.cpp b/modules/core/src/predicates.cpp index 37e0faff..210f62b1 100644 --- a/modules/core/src/predicates.cpp +++ b/modules/core/src/predicates.cpp @@ -916,19 +916,31 @@ int fast_expansion_sum_zeroelim(int elen, REAL* e, int flen, REAL* f, REAL* h) eindex = findex = 0; if ((fnow > enow) == (fnow > -enow)) { Q = enow; - enow = e[++eindex]; + ++eindex; + if (eindex < elen) { + enow = e[eindex]; + } } else { Q = fnow; - fnow = f[++findex]; + ++findex; + if (findex < flen) { + fnow = f[findex]; + } } hindex = 0; if ((eindex < elen) && (findex < flen)) { if ((fnow > enow) == (fnow > -enow)) { Fast_Two_Sum(enow, Q, Qnew, hh); - enow = e[++eindex]; + ++eindex; + if (eindex < elen) { + enow = e[eindex]; + } } else { Fast_Two_Sum(fnow, Q, Qnew, hh); - fnow = f[++findex]; + ++findex; + if (findex < flen) { + fnow = f[findex]; + } } Q = Qnew; if (hh != 0.0) { @@ -937,10 +949,16 @@ int fast_expansion_sum_zeroelim(int elen, REAL* e, int flen, REAL* f, REAL* h) while ((eindex < elen) && (findex < flen)) { if ((fnow > enow) == (fnow > -enow)) { Two_Sum(Q, enow, Qnew, hh); - enow = e[++eindex]; + ++eindex; + if (eindex < elen) { + enow = e[eindex]; + } } else { Two_Sum(Q, fnow, Qnew, hh); - fnow = f[++findex]; + ++findex; + if (findex < flen) { + fnow = f[findex]; + } } Q = Qnew; if (hh != 0.0) { @@ -950,7 +968,10 @@ int fast_expansion_sum_zeroelim(int elen, REAL* e, int flen, REAL* f, REAL* h) } while (eindex < elen) { Two_Sum(Q, enow, Qnew, hh); - enow = e[++eindex]; + ++eindex; + if (eindex < elen) { + enow = e[eindex]; + } Q = Qnew; if (hh != 0.0) { h[hindex++] = hh; @@ -958,7 +979,10 @@ int fast_expansion_sum_zeroelim(int elen, REAL* e, int flen, REAL* f, REAL* h) } while (findex < flen) { Two_Sum(Q, fnow, Qnew, hh); - fnow = f[++findex]; + ++findex; + if (findex < flen) { + fnow = f[findex]; + } Q = Qnew; if (hh != 0.0) { h[hindex++] = hh; diff --git a/modules/core/tests/CMakeLists.txt b/modules/core/tests/CMakeLists.txt index 568dde1d..11a66f8d 100644 --- a/modules/core/tests/CMakeLists.txt +++ b/modules/core/tests/CMakeLists.txt @@ -10,3 +10,10 @@ # governing permissions and limitations under the License. # lagrange_add_test() + +if(LAGRANGE_USE_PCH) + # Use precompiled headers in core tests since there are a lot of cpp files in core tests. + # Check the timing if you want to add this to your own module, it may or may not be worth it. + target_link_libraries(test_lagrange_core PRIVATE lagrange_testing_pch) + set_source_files_properties("${CMAKE_CURRENT_SOURCE_DIR}/main.cpp" PROPERTIES SKIP_PRECOMPILE_HEADERS ON) +endif() diff --git a/modules/core/tests/test_compute_tangent_bitangent.cpp b/modules/core/tests/test_compute_tangent_bitangent.cpp index 426e3eff..2e42e935 100644 --- a/modules/core/tests/test_compute_tangent_bitangent.cpp +++ b/modules/core/tests/test_compute_tangent_bitangent.cpp @@ -13,7 +13,7 @@ #include #include #include - +#include #include diff --git a/modules/core/tests/test_corner_to_edge_mapping.cpp b/modules/core/tests/test_corner_to_edge_mapping.cpp index 2c326232..e4950532 100644 --- a/modules/core/tests/test_corner_to_edge_mapping.cpp +++ b/modules/core/tests/test_corner_to_edge_mapping.cpp @@ -11,6 +11,7 @@ */ #include #include +#include // clang-format off diff --git a/modules/core/tests/test_quad_to_tri.cpp b/modules/core/tests/test_quad_to_tri.cpp index f4c264b9..ffa401d6 100644 --- a/modules/core/tests/test_quad_to_tri.cpp +++ b/modules/core/tests/test_quad_to_tri.cpp @@ -11,6 +11,7 @@ */ #include #include +#include #include #include #include diff --git a/modules/fs/CMakeLists.txt b/modules/fs/CMakeLists.txt index 6a34d309..b529a34c 100644 --- a/modules/fs/CMakeLists.txt +++ b/modules/fs/CMakeLists.txt @@ -11,22 +11,9 @@ # # 1. define module -add_library(lagrange_fs) -add_library(lagrange::fs ALIAS lagrange_fs) -message(STATUS "Lagrange: creating target 'lagrange::fs'") +lagrange_add_module() -include(GNUInstallDirs) -target_include_directories(lagrange_fs PUBLIC - $ - $ -) - -# 2. target sources -file(GLOB_RECURSE INC_FILES "include/*.h") -file(GLOB_RECURSE SRC_FILES "src/*.cpp") -target_sources(lagrange_fs PRIVATE ${INC_FILES} ${SRC_FILES}) - -# 3. dependencies +# 2. dependencies lagrange_include_modules(core) target_link_libraries(lagrange_fs PUBLIC lagrange::core @@ -58,13 +45,13 @@ else() message(FATAL_ERROR "Unsupported LAGRANGE_FS_BACKEND: ${LAGRANGE_FS_BACKEND}!") endif() -# 4. installation +# 3. installation if(LAGRANGE_INSTALL) set_target_properties(lagrange_fs PROPERTIES EXPORT_NAME fs) lagrange_install(lagrange_fs) endif() -# 5. unit tests and examples +# 4. unit tests and examples if(LAGRANGE_UNIT_TESTS) add_subdirectory(tests) endif() diff --git a/modules/io/CMakeLists.txt b/modules/io/CMakeLists.txt index a628f88d..7ec128c6 100644 --- a/modules/io/CMakeLists.txt +++ b/modules/io/CMakeLists.txt @@ -11,25 +11,13 @@ # # 1. define module -add_library(lagrange_io INTERFACE) -add_library(lagrange::io ALIAS lagrange_io) -message(STATUS "Lagrange: creating target 'lagrange::io'") +lagrange_add_module() -include(GNUInstallDirs) -target_include_directories(lagrange_io INTERFACE - $ - $ -) - -# 2. add a custom target to show headers in IDE -file(GLOB_RECURSE INC_FILES "include/*.h") -add_custom_target(lagrange_io_ SOURCES ${INC_FILES}) - -# 3. dependencies +# 2. dependencies lagrange_include_modules(core fs) include(tinyobjloader) include(libigl) # TODO: remove libigl later -target_link_libraries(lagrange_io INTERFACE +target_link_libraries(lagrange_io PUBLIC lagrange::core lagrange::fs tinyobjloader::tinyobjloader @@ -39,17 +27,17 @@ target_link_libraries(lagrange_io INTERFACE option(LAGRANGE_WITH_ASSIMP "Add assimp functionality to lagrange::io" FALSE) if(LAGRANGE_WITH_ASSIMP) include(assimp) - target_link_libraries(lagrange_io INTERFACE assimp::assimp) - target_compile_definitions(lagrange_io INTERFACE LAGRANGE_WITH_ASSIMP) + target_link_libraries(lagrange_io PUBLIC assimp::assimp) + target_compile_definitions(lagrange_io LAGRANGE_WITH_ASSIMP) endif() -# 4. installation +# 3. installation if(LAGRANGE_INSTALL) set_target_properties(lagrange_io PROPERTIES EXPORT_NAME io) lagrange_install(lagrange_io) endif() -# 5. unit tests and examples +# 4. unit tests and examples if(LAGRANGE_UNIT_TESTS) add_subdirectory(tests) endif() diff --git a/modules/io/include/lagrange/io/load_mesh.h b/modules/io/include/lagrange/io/load_mesh.h index 0efbe53f..f87b41c1 100644 --- a/modules/io/include/lagrange/io/load_mesh.h +++ b/modules/io/include/lagrange/io/load_mesh.h @@ -11,79 +11,33 @@ */ #pragma once -#include -#include +/* +* This file does not contain function definition because those require other expensive headers +* and we want to minimize the size of this header, as it's used in almost all tests. +* +* If you have any issues with a load_mesh function not being defined for your specific +* mesh type, you can #include instead. +*/ -#include #include - -#include -#include -#include - -// clang-format off -#include -#include -#include -#include -// clang-format on +#include +#include +#include namespace lagrange { namespace io { template -std::unique_ptr load_mesh_basic(const fs::path& filename) -{ - static_assert(MeshTrait::is_mesh(), "Input type is not Mesh"); - typename MeshType::VertexArray vertices; - typename MeshType::FacetArray facets; - - igl::read_triangle_mesh(filename.string(), vertices, facets); - - return create_mesh(vertices, facets); -} +std::unique_ptr load_mesh_basic(const fs::path& filename); template -std::vector> load_obj_meshes(const fs::path& filename) -{ - static_assert(MeshTrait::is_mesh(), "Input type is not Mesh"); - - if (!std::is_same::value) { - logger().warn("Tinyobjloader compiled to use float, loss of precision may occur."); - } - - return load_mesh_ext(filename).meshes; -} +std::vector> load_obj_meshes(const fs::path& filename); template -std::unique_ptr load_obj_mesh(const fs::path& filename) -{ - static_assert(MeshTrait::is_mesh(), "Input type is not Mesh"); - - auto res = load_obj_meshes(filename); - if (res.empty()) { - return nullptr; - } else if (res.size() == 1) { - return std::move(res[0]); - } else { - logger().debug("Combining {} meshes into one.", res.size()); - return combine_mesh_list(res); - } -} +std::unique_ptr load_obj_mesh(const fs::path& filename); template -std::unique_ptr load_mesh(const fs::path& filename) -{ - static_assert(MeshTrait::is_mesh(), "Input type is not Mesh"); - - if (filename.extension() == ".obj") { - return load_obj_mesh(filename); - } else if (filename.extension() == ".ply") { - return load_mesh_ply(filename); - } else { - return load_mesh_basic(filename); - } -} +std::unique_ptr load_mesh(const fs::path& filename); } // namespace io } // namespace lagrange diff --git a/modules/io/include/lagrange/io/load_mesh.impl.h b/modules/io/include/lagrange/io/load_mesh.impl.h new file mode 100644 index 00000000..69f45d6e --- /dev/null +++ b/modules/io/include/lagrange/io/load_mesh.impl.h @@ -0,0 +1,84 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace lagrange { +namespace io { + +template +std::unique_ptr load_mesh_basic(const fs::path& filename) +{ + static_assert(MeshTrait::is_mesh(), "Input type is not Mesh"); + typename MeshType::VertexArray vertices; + typename MeshType::FacetArray facets; + + igl::read_triangle_mesh(filename.string(), vertices, facets); + + return create_mesh(vertices, facets); +} +extern template std::unique_ptr load_mesh_basic(const fs::path&); +extern template std::unique_ptr load_mesh_basic(const fs::path&); + +template +std::vector> load_obj_meshes(const fs::path& filename) +{ + static_assert(MeshTrait::is_mesh(), "Input type is not Mesh"); + + if (!std::is_same::value) { + logger().warn("Tinyobjloader compiled to use float, loss of precision may occur."); + } + + return load_mesh_ext(filename).meshes; +} +extern template std::vector> load_obj_meshes(const fs::path&); +extern template std::vector> load_obj_meshes(const fs::path&); + +template +std::unique_ptr load_obj_mesh(const fs::path& filename) +{ + static_assert(MeshTrait::is_mesh(), "Input type is not Mesh"); + + auto res = load_obj_meshes(filename); + if (res.empty()) { + return nullptr; + } else if (res.size() == 1) { + return std::move(res[0]); + } else { + logger().debug("Combining {} meshes into one.", res.size()); + return combine_mesh_list(res); + } +} +extern template std::unique_ptr load_obj_mesh(const fs::path&); +extern template std::unique_ptr load_obj_mesh(const fs::path&); + +template +std::unique_ptr load_mesh(const fs::path& filename) +{ + static_assert(MeshTrait::is_mesh(), "Input type is not Mesh"); + + if (filename.extension() == ".obj") { + return load_obj_mesh(filename); + } else if (filename.extension() == ".ply") { + return load_mesh_ply(filename); + } else { + return load_mesh_basic(filename); + } +} +extern template std::unique_ptr load_mesh(const fs::path&); +extern template std::unique_ptr load_mesh(const fs::path&); + + +} // namespace io +} // namespace lagrange diff --git a/modules/io/src/load_mesh.cpp b/modules/io/src/load_mesh.cpp new file mode 100644 index 00000000..34bff758 --- /dev/null +++ b/modules/io/src/load_mesh.cpp @@ -0,0 +1,31 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you 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 REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +#include + +namespace lagrange { +namespace io { + +template std::unique_ptr load_mesh_basic(const fs::path&); +template std::unique_ptr load_mesh_basic(const fs::path&); + +template std::vector> load_obj_meshes(const fs::path&); +template std::vector> load_obj_meshes(const fs::path&); + +template std::unique_ptr load_obj_mesh(const fs::path&); +template std::unique_ptr load_obj_mesh(const fs::path&); + +template std::unique_ptr load_mesh(const fs::path&); +template std::unique_ptr load_mesh(const fs::path&); +template std::unique_ptr load_mesh(const fs::path&); +template std::unique_ptr> load_mesh(const fs::path&); +} +} // namespace lagrange diff --git a/modules/io/tests/test_mesh_io.cpp b/modules/io/tests/test_mesh_io.cpp index c7cd32df..66bdcda2 100644 --- a/modules/io/tests/test_mesh_io.cpp +++ b/modules/io/tests/test_mesh_io.cpp @@ -11,9 +11,11 @@ */ #include #include -#include #include +#include +#include + TEST_CASE("drop", "[mesh][io]") { diff --git a/modules/testing/CMakeLists.txt b/modules/testing/CMakeLists.txt index c14b16fe..7a13ff40 100644 --- a/modules/testing/CMakeLists.txt +++ b/modules/testing/CMakeLists.txt @@ -11,23 +11,10 @@ # # 1. define module -add_library(lagrange_testing) -add_library(lagrange::testing ALIAS lagrange_testing) -message(STATUS "Lagrange: creating target 'lagrange::testing'") +lagrange_add_module() +set_target_properties(lagrange_testing PROPERTIES FOLDER "${LAGRANGE_IDE_PREFIX}Lagrange//Tests") -include(GNUInstallDirs) -target_include_directories(lagrange_testing PUBLIC - $ - $ -) - -# 2. target sources -file(GLOB_RECURSE INC_FILES "include/*.h") -file(GLOB_RECURSE SRC_FILES "src/*.cpp") -target_sources(lagrange_testing PRIVATE ${INC_FILES} ${SRC_FILES}) -set_target_properties(lagrange_testing PROPERTIES FOLDER "Lagrange//Tests") - -# 3. dependencies +# 2. dependencies lagrange_include_modules(io) target_link_libraries(lagrange_testing PUBLIC Catch2::Catch2 @@ -35,7 +22,7 @@ target_link_libraries(lagrange_testing PUBLIC lagrange::io ) -# 4. test-specific properties +# 3. test-specific properties include(lagrange_download_data) add_dependencies(lagrange_testing lagrange_download_data) @@ -47,7 +34,11 @@ target_compile_definitions(lagrange_testing ) if(LAGRANGE_USE_PCH) - target_link_libraries(lagrange_testing PUBLIC lagrange::core_pch) + add_library(lagrange_testing_pch INTERFACE) + target_link_libraries(lagrange_testing_pch INTERFACE lagrange_core_pch) + target_precompile_headers(lagrange_testing_pch INTERFACE + + ) endif() # Set flag to hide unit tests depending on private data availability diff --git a/modules/testing/include/lagrange/testing/common.h b/modules/testing/include/lagrange/testing/common.h index 4cb0ec9c..a9459ed4 100644 --- a/modules/testing/include/lagrange/testing/common.h +++ b/modules/testing/include/lagrange/testing/common.h @@ -17,6 +17,7 @@ #include #endif +#include #include #include @@ -44,12 +45,14 @@ fs::path get_data_path(const fs::path &relative_path); /// @return A unique_ptr to the newly allocated mesh. /// template -std::unique_ptr load_mesh(const fs::path &relative_path) +std::unique_ptr load_mesh(const fs::path& relative_path) { auto result = lagrange::io::load_mesh(get_data_path(relative_path)); REQUIRE(result); return result; } +extern template std::unique_ptr load_mesh(const fs::path&); +extern template std::unique_ptr load_mesh(const fs::path&); /// /// Set up MKL Conditional Numerical Reproducibility to ensure maximum compatibility between diff --git a/modules/testing/src/common.cpp b/modules/testing/src/common.cpp index 35638e55..877e832d 100644 --- a/modules/testing/src/common.cpp +++ b/modules/testing/src/common.cpp @@ -11,6 +11,9 @@ */ #include +#include +#include + #include #ifdef EIGEN_USE_MKL_ALL @@ -38,6 +41,9 @@ fs::path get_data_path(const fs::path& relative_path) return absolute_path; } +template std::unique_ptr load_mesh(const fs::path&); +template std::unique_ptr load_mesh(const fs::path&); + void setup_mkl_reproducibility() { #ifdef EIGEN_USE_MKL_ALL diff --git a/modules/ui/CMakeLists.txt b/modules/ui/CMakeLists.txt index 0f1fc924..d4e98ed9 100644 --- a/modules/ui/CMakeLists.txt +++ b/modules/ui/CMakeLists.txt @@ -12,9 +12,7 @@ option(LAGRANGE_UI_USE_MDL "Use Material Definition Library" OFF) # 1. define module -add_library(lagrange_ui) -add_library(lagrange::ui ALIAS lagrange_ui) -message(STATUS "Lagrange: creating target 'lagrange::ui'") +lagrange_add_module() if(WIN32) target_compile_definitions(lagrange_ui PUBLIC -DNOMINMAX -D_SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING) @@ -22,23 +20,7 @@ endif() target_compile_features(lagrange_ui PUBLIC cxx_std_17) -include(GNUInstallDirs) -target_include_directories(lagrange_ui PUBLIC - $ - $ -) - -# 2. target sources -file(GLOB_RECURSE INC_FILES "include/*.h") -file(GLOB_RECURSE SRC_FILES "src/*.cpp") - -source_group( - TREE "${PROJECT_SOURCE_DIR}" - FILES ${INC_FILES} ${SRC_FILES}) - -target_sources(lagrange_ui PRIVATE ${INC_FILES} ${SRC_FILES}) - -# 3. dependencies +# 2. dependencies lagrange_include_modules(io) include(nanoflann) include(gl3w) @@ -91,7 +73,7 @@ if(LAGRANGE_DEBUG_SHADERS) target_compile_definitions(lagrange_ui PRIVATE DEFAULT_SHADERS_USE_REAL_PATH="${CMAKE_CURRENT_SOURCE_DIR}/src/shaders/") endif() -# 4. automatic shader generation +# 3. automatic shader generation set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated") file(MAKE_DIRECTORY "${generated_dir}") target_include_directories(lagrange_ui PUBLIC "${generated_dir}") @@ -109,8 +91,6 @@ add_dependencies(lagrange_ui lagrange_ui_generate_shaders) # PCH if(LAGRANGE_USE_PCH) add_library(lagrange_ui_pch INTERFACE) - - add_library(lagrange::ui_pch ALIAS lagrange_ui_pch) target_link_libraries(lagrange_ui_pch INTERFACE lagrange_core_pch) target_precompile_headers(lagrange_ui_pch INTERFACE @@ -118,17 +98,16 @@ if(LAGRANGE_USE_PCH) ) target_compile_features(lagrange_ui_pch INTERFACE cxx_std_17) - - target_link_libraries(lagrange_ui PRIVATE lagrange::ui_pch lagrange::core_pch) + target_link_libraries(lagrange_ui PRIVATE lagrange_ui_pch) endif() -# 5. installation +# 4. installation # if(LAGRANGE_INSTALL) # set_target_properties(lagrange_ui PROPERTIES EXPORT_NAME ui) # lagrange_install(lagrange_ui) # endif() -# 6. unit tests and examples +# 5. unit tests and examples if(LAGRANGE_EXAMPLES) add_subdirectory(examples) endif() diff --git a/modules/ui/examples/ui_callbacks/CMakeLists.txt b/modules/ui/examples/ui_callbacks/CMakeLists.txt index 75b86066..ce139e35 100644 --- a/modules/ui/examples/ui_callbacks/CMakeLists.txt +++ b/modules/ui/examples/ui_callbacks/CMakeLists.txt @@ -17,12 +17,3 @@ lagrange_add_example(ui_callbacks ) target_link_libraries(ui_callbacks lagrange::ui lagrange::io CLI11::CLI11) - -if(LAGRANGE_UI_USE_MDL) - add_custom_command(TARGET ui_callbacks POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${MDL_DYNAMIC_LIBS} - $ - ) -endif() - diff --git a/modules/ui/examples/ui_dynamic_mesh/CMakeLists.txt b/modules/ui/examples/ui_dynamic_mesh/CMakeLists.txt index 51555e5e..089ffe8b 100644 --- a/modules/ui/examples/ui_dynamic_mesh/CMakeLists.txt +++ b/modules/ui/examples/ui_dynamic_mesh/CMakeLists.txt @@ -17,12 +17,3 @@ lagrange_add_example(ui_dynamic_mesh ) target_link_libraries(ui_dynamic_mesh lagrange::ui lagrange::io CLI11::CLI11) - -if(LAGRANGE_UI_USE_MDL) - add_custom_command(TARGET ui_dynamic_mesh POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${MDL_DYNAMIC_LIBS} - $ - ) -endif() - diff --git a/modules/ui/examples/ui_playground/CMakeLists.txt b/modules/ui/examples/ui_playground/CMakeLists.txt index eb3b1cda..6d662550 100644 --- a/modules/ui/examples/ui_playground/CMakeLists.txt +++ b/modules/ui/examples/ui_playground/CMakeLists.txt @@ -17,12 +17,3 @@ lagrange_add_example(ui_playground ) target_link_libraries(ui_playground lagrange::ui lagrange::io CLI11::CLI11) - -if(LAGRANGE_UI_USE_MDL) - add_custom_command(TARGET ui_playground POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${MDL_DYNAMIC_LIBS} - $ - ) -endif() - diff --git a/modules/ui/examples/ui_playground/main.cpp b/modules/ui/examples/ui_playground/main.cpp index d3e53867..451a1732 100644 --- a/modules/ui/examples/ui_playground/main.cpp +++ b/modules/ui/examples/ui_playground/main.cpp @@ -101,6 +101,16 @@ int main(int argc, char** argv) if (!viewer.is_initialized()) return 1; + // Test shaders + { + auto & shaders = ui::get_registered_shaders(viewer); + for (auto & it : shaders) { + //This will invoke shader compilation i + auto tmp_mat = ui::create_material(viewer, it.first); + } + } + + // Test mesh types and meta { ui::MeshData d; @@ -234,7 +244,8 @@ int main(int argc, char** argv) // Creates a default visualization (PBR) of the mesh entity - auto obj_pbr = ui::show_mesh(registry, my_mesh, ui::DefaultShaders::PBRSkeletal); + auto obj_pbr = + ui::show_mesh(registry, my_mesh, ui::DefaultShaders::PBR, {{"SKELETAL", {"On"}}}); registry.emplace_or_replace(obj_pbr, "root"); diff --git a/modules/ui/examples/ui_scene/CMakeLists.txt b/modules/ui/examples/ui_scene/CMakeLists.txt index edce93e5..8824e453 100644 --- a/modules/ui/examples/ui_scene/CMakeLists.txt +++ b/modules/ui/examples/ui_scene/CMakeLists.txt @@ -17,12 +17,3 @@ lagrange_add_example(ui_scene ) target_link_libraries(ui_scene lagrange::ui lagrange::io CLI11::CLI11) - -if(LAGRANGE_UI_USE_MDL) - add_custom_command(TARGET ui_scene POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${MDL_DYNAMIC_LIBS} - $ - ) -endif() - diff --git a/modules/ui/examples/ui_show_attribute/CMakeLists.txt b/modules/ui/examples/ui_show_attribute/CMakeLists.txt index e08232fc..26623718 100644 --- a/modules/ui/examples/ui_show_attribute/CMakeLists.txt +++ b/modules/ui/examples/ui_show_attribute/CMakeLists.txt @@ -17,12 +17,3 @@ lagrange_add_example(ui_show_attribute ) target_link_libraries(ui_show_attribute lagrange::ui lagrange::io CLI11::CLI11) - -if(LAGRANGE_UI_USE_MDL) - add_custom_command(TARGET ui_show_attribute POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${MDL_DYNAMIC_LIBS} - $ - ) -endif() - diff --git a/modules/ui/examples/ui_treenode/CMakeLists.txt b/modules/ui/examples/ui_treenode/CMakeLists.txt index 4d251b23..52db264a 100644 --- a/modules/ui/examples/ui_treenode/CMakeLists.txt +++ b/modules/ui/examples/ui_treenode/CMakeLists.txt @@ -17,12 +17,3 @@ lagrange_add_example(ui_treenode ) target_link_libraries(ui_treenode lagrange::ui lagrange::io CLI11::CLI11) - -if(LAGRANGE_UI_USE_MDL) - add_custom_command(TARGET ui_treenode POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${MDL_DYNAMIC_LIBS} - $ - ) -endif() - diff --git a/modules/ui/include/lagrange/ui/Viewer.h b/modules/ui/include/lagrange/ui/Viewer.h index 09e7888a..18b776ef 100644 --- a/modules/ui/include/lagrange/ui/Viewer.h +++ b/modules/ui/include/lagrange/ui/Viewer.h @@ -13,18 +13,20 @@ #include -#include -#include #include #include +#include #include +#include +#include +#include #include #include -#include -#include #include +#include #include +#include #include #include #include @@ -103,7 +105,7 @@ class Viewer /// Path to imgui .ini file /// If not set, the .ini file will be at %APPDATA%/_window_title.ini - /// + /// std::string imgui_ini_path = ""; }; @@ -181,6 +183,23 @@ class Viewer /// Returns reference to the systems Systems& systems() { return m_systems; } + /// Enqueues `fn` to be run on the main thread + std::future run_on_main_thread(std::function fn); + + /// Returns the limit of how many enqueued functions can be run during a single frame on the main thread + unsigned int get_main_thread_max_func_per_frame() const + { + return m_main_thread_max_func_per_frame; + } + + /// Sets the limit of how many enqueued functions can be run during a single frame on the main thread + /// Use std::numeric_limits::max() for no limit + void set_main_thread_max_func_per_frame(unsigned int limit) + { + m_main_thread_max_func_per_frame = limit; + } + + private: bool init_glfw(const WindowOptions& options); bool init_imgui(); @@ -194,7 +213,7 @@ class Viewer static std::string get_config_folder(); - + void process_input(); void update_time(); @@ -244,6 +263,13 @@ class Viewer Systems m_systems; Entity m_main_viewport; + + // Functions that will be run on next frame on the main thread + // Used for code interacting with OpenGL context + std::mutex m_mutex; + std::queue, std::function>> m_main_thread_fn; + std::atomic_uint m_main_thread_max_func_per_frame = + std::numeric_limits::max(); }; } // namespace ui diff --git a/modules/ui/include/lagrange/ui/components/Viewport.h b/modules/ui/include/lagrange/ui/components/Viewport.h index 8dcce7ab..af7011b1 100644 --- a/modules/ui/include/lagrange/ui/components/Viewport.h +++ b/modules/ui/include/lagrange/ui/components/Viewport.h @@ -52,6 +52,7 @@ struct ViewportComponent std::unordered_map> post_process_effects; Color background = Color(0, 0, 0, 0); + GLbitfield clear_bits = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT; }; inline Camera& get_viewport_camera(Registry& r, ViewportComponent& viewport) diff --git a/modules/ui/include/lagrange/ui/default_entities.h b/modules/ui/include/lagrange/ui/default_entities.h index 4ca6ef8b..e9c34fe1 100644 --- a/modules/ui/include/lagrange/ui/default_entities.h +++ b/modules/ui/include/lagrange/ui/default_entities.h @@ -22,7 +22,11 @@ namespace ui { void set_material(Registry& r, Entity meshrender_entity, std::shared_ptr mat); -Entity show_mesh(Registry& r, const Entity& mesh_entity, StringID shader = DefaultShaders::PBR); +Entity show_mesh( + Registry& r, + const Entity& mesh_entity, + StringID shader = DefaultShaders::PBR, + const ShaderDefines& shader_defines = {}); Entity show_submesh( Registry& r, const Entity& mesh_entity, @@ -33,7 +37,8 @@ Entity show_mesh( Registry& r, Entity mesh_entity, Entity scene_node_entity, - StringID shader = DefaultShaders::PBR); + StringID shader = DefaultShaders::PBR, + const ShaderDefines& shader_defines = {}); Entity show_mesh( Registry& r, @@ -176,7 +181,6 @@ Entity load_mesh( } return e; } - } return NullEntity; @@ -203,7 +207,8 @@ MeshData& get_meshdata(Registry& r, Entity scene_or_mesh_entity); /* Material */ -std::shared_ptr create_material(Registry& r, entt::id_type shader_id); +std::shared_ptr +create_material(Registry& r, entt::id_type shader_id, const ShaderDefines& shader_defines = {}); Entity add_camera(Registry& r, const Camera& camera = Camera::default_camera(1, 1)); diff --git a/modules/ui/include/lagrange/ui/default_shaders.h b/modules/ui/include/lagrange/ui/default_shaders.h index 1d64a09a..96a3375b 100644 --- a/modules/ui/include/lagrange/ui/default_shaders.h +++ b/modules/ui/include/lagrange/ui/default_shaders.h @@ -33,6 +33,7 @@ struct DefaultShaders "SurfaceEdgeAttribute"_hs; // Uses special edge attribute interpolation constexpr static const StringID ObjectID = "ObjectID"_hs; constexpr static const StringID MeshElementID = "MeshElementID"_hs; + constexpr static const StringID Skybox = "Skybox"_hs; }; struct ColormapShaderMode diff --git a/modules/ui/include/lagrange/ui/types/Material.h b/modules/ui/include/lagrange/ui/types/Material.h index 46a23251..1fa030b8 100644 --- a/modules/ui/include/lagrange/ui/types/Material.h +++ b/modules/ui/include/lagrange/ui/types/Material.h @@ -21,7 +21,7 @@ namespace ui { class Material { public: - Material(Registry& r, StringID shader_id); + Material(Registry& r, StringID shader_id, const ShaderDefines & shader_defines = {}); StringID shader_id() const; diff --git a/modules/ui/include/lagrange/ui/types/Shader.h b/modules/ui/include/lagrange/ui/types/Shader.h index 73c73a77..f50d9b28 100644 --- a/modules/ui/include/lagrange/ui/types/Shader.h +++ b/modules/ui/include/lagrange/ui/types/Shader.h @@ -86,6 +86,7 @@ struct RasterizerOptions { constexpr static const StringID DepthTest = "DepthTest"_hs; constexpr static const StringID DepthMask = "DepthMask"_hs; + constexpr static const StringID DepthFunc = "DepthFunc"_hs; constexpr static const StringID BlendEquation = "BlendEquation"_hs; constexpr static const StringID DrawBuffer = "DrawBuffer"_hs; constexpr static const StringID ReadBuffer = "ReadBuffer"_hs; diff --git a/modules/ui/include/lagrange/ui/types/ShaderLoader.h b/modules/ui/include/lagrange/ui/types/ShaderLoader.h index 73f4667b..c9373b32 100644 --- a/modules/ui/include/lagrange/ui/types/ShaderLoader.h +++ b/modules/ui/include/lagrange/ui/types/ShaderLoader.h @@ -22,7 +22,7 @@ struct ShaderLoader : entt::resource_loader enum class PathType { REAL, VIRTUAL }; std::shared_ptr - load(const std::string& generic_path, PathType pathtype, ShaderDefines defines = {}) const; + load(const std::string& generic_path, PathType pathtype, const ShaderDefines & defines = {}) const; }; using ShaderCache = entt::resource_cache; using ShaderResource = entt::resource_handle; @@ -46,12 +46,24 @@ entt::id_type register_shader(Registry& r, const ShaderDefinition& def); entt::id_type register_shader(Registry& r, const std::string& path, const std::string& display_name); +entt::id_type register_shader_variant(Registry& r, entt::id_type id, const ShaderDefines & shader_defines); + ShaderResource get_shader(Registry& r, entt::id_type id); + RegisteredShaders& get_registered_shaders(Registry& r); ShaderCache& get_shader_cache(Registry& r); +/// Creates a file using `virtual_path` with `contents` in the shader virtual file system. +/// This file will be visible to the ShaderLoader, to be directly loaded as a shader +/// or to be included in another shader via #include "virtual/fs/path/.." +/// Returns true if written successfully. +/// Returns false if 1. No virtual fs is used (DEFAULT_SHADERS_USE_REAL_PATH is defined), 2. File already exists and overwrite==false +bool add_file_to_shader_virtual_fs( + const std::string& virtual_path, + const std::string& contents, + bool overwrite = false); } // namespace ui } // namespace lagrange \ No newline at end of file diff --git a/modules/ui/include/lagrange/ui/types/Systems.h b/modules/ui/include/lagrange/ui/types/Systems.h index a43322b2..c193d2ff 100644 --- a/modules/ui/include/lagrange/ui/types/Systems.h +++ b/modules/ui/include/lagrange/ui/types/Systems.h @@ -59,6 +59,11 @@ class Systems /// @return true on success, false if `system_id` or `after_id` do not exist bool succeeds(StringID system_id, StringID after_id); + /// @brief Removes system identifier with `id` + /// @param id + /// @return true if system existed and was removed + bool remove(StringID id); + private: struct SystemItem { diff --git a/modules/ui/src/Viewer.cpp b/modules/ui/src/Viewer.cpp index 5f235f41..712cc27c 100644 --- a/modules/ui/src/Viewer.cpp +++ b/modules/ui/src/Viewer.cpp @@ -104,8 +104,7 @@ Viewer::Viewer(const WindowOptions& window_options) : m_initial_window_options(window_options) { if (window_options.imgui_ini_path.empty()) { - m_imgui_ini_path = - get_config_folder() + m_initial_window_options.window_title + ".ini"; + m_imgui_ini_path = get_config_folder() + m_initial_window_options.window_title + ".ini"; } else { m_imgui_ini_path = window_options.imgui_ini_path; } @@ -433,80 +432,98 @@ bool Viewer::run(const std::function& main_loop) if (!is_initialized()) return false; while (!should_close()) { - /* - Initialization systems - */ - m_systems.run(Systems::Stage::Init, registry()); - - - start_imgui_frame(); + for (unsigned int fn_counter = 0; fn_counter < m_main_thread_max_func_per_frame; fn_counter++) { + std::pair, std::function> item; + { + // Consume queue item + std::lock_guard lock(m_mutex); + if (m_main_thread_fn.empty()) break; + item = std::move(m_main_thread_fn.front()); + m_main_thread_fn.pop(); + } + // Run function + item.second(); + // Set promise value + item.first.set_value(); + } - draw_menu(); - // Dock space { - start_dockspace(); - if (m_show_imgui_demo) { - ImGui::ShowDemoWindow(&m_show_imgui_demo); - } + /* + Initialization systems + */ + m_systems.run(Systems::Stage::Init, registry()); - if (m_show_imgui_style) { - ImGui::Begin("Style Editor", &m_show_imgui_style); - ImGui::ShowStyleEditor(); - ImGui::End(); - } - m_systems.run(Systems::Stage::Interface, registry()); + start_imgui_frame(); - if (main_loop) { - if (!main_loop(registry())) glfwSetWindowShouldClose(m_window, true); - } - show_last_shader_error(); - end_dockspace(); - } + draw_menu(); + // Dock space + { + start_dockspace(); + if (m_show_imgui_demo) { + ImGui::ShowDemoWindow(&m_show_imgui_demo); + } - end_imgui_frame(); + if (m_show_imgui_style) { + ImGui::Begin("Style Editor", &m_show_imgui_style); + ImGui::ShowStyleEditor(); + ImGui::End(); + } + m_systems.run(Systems::Stage::Interface, registry()); - m_systems.run(Systems::Stage::Simulation, registry()); + if (main_loop) { + if (!main_loop(registry())) glfwSetWindowShouldClose(m_window, true); + } + show_last_shader_error(); + end_dockspace(); + } - // All rendering goes here - { - /* - Render to texture - */ - try { - m_last_shader_error = ""; - m_last_shader_error_desc = ""; + end_imgui_frame(); - m_systems.run(Systems::Stage::Render, registry()); - } catch (ShaderException& ex) { - m_last_shader_error = ex.what(); - m_last_shader_error_desc = ex.get_desc(); - lagrange::logger().error("{}", m_last_shader_error); - } + m_systems.run(Systems::Stage::Simulation, registry()); - /* - Clear default framebuffer - */ - GLScope gl; + + // All rendering goes here { - gl(glBindFramebuffer, GL_FRAMEBUFFER, 0); - gl(glViewport, 0, 0, m_width, m_height); - Color bgcolor = Color(0, 0, 0, 0); - gl(glClearColor, bgcolor.x(), bgcolor.y(), bgcolor.z(), bgcolor.a()); - gl(glClear, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - } + /* + Render to texture + */ + try { + m_last_shader_error = ""; + m_last_shader_error_desc = ""; + + m_systems.run(Systems::Stage::Render, registry()); + + } catch (ShaderException& ex) { + m_last_shader_error = ex.what(); + m_last_shader_error_desc = ex.get_desc(); + lagrange::logger().error("{}", m_last_shader_error); + } + + /* + Clear default framebuffer + */ + GLScope gl; + { + gl(glBindFramebuffer, GL_FRAMEBUFFER, 0); + gl(glViewport, 0, 0, m_width, m_height); + Color bgcolor = Color(0, 0, 0, 0); + gl(glClearColor, bgcolor.x(), bgcolor.y(), bgcolor.z(), bgcolor.a()); + gl(glClear, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); // render to screen buffer - } + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); // render to screen buffer + } - glfwSwapBuffers(m_window); + glfwSwapBuffers(m_window); + } m_systems.run(Systems::Stage::Post, registry()); @@ -518,7 +535,7 @@ bool Viewer::run(const std::function& main_loop) bool Viewer::run(const std::function& main_loop /*= {}*/) { - return run([=](Registry& /*r*/) -> bool { + return run([=](Registry & /*r*/) -> bool { if (main_loop) main_loop(); return true; }); @@ -976,6 +993,19 @@ void Viewer::make_current() glfwMakeContextCurrent(m_window); } + +std::future Viewer::run_on_main_thread(std::function fn) +{ + std::promise fn_promise; + auto future = fn_promise.get_future(); + + { + std::lock_guard lock(m_mutex); + m_main_thread_fn.push({std::move(fn_promise), std::move(fn)}); + } + return future; +} + void Viewer::draw_menu() { auto& r = registry(); diff --git a/modules/ui/src/default_components.cpp b/modules/ui/src/default_components.cpp index 400933dc..f3b88127 100644 --- a/modules/ui/src/default_components.cpp +++ b/modules/ui/src/default_components.cpp @@ -490,86 +490,94 @@ void show_material(Registry* rptr, Material& mat) for (auto& it : values) { const auto& id = it.first; - if (shader->texture_properties().find(id) == shader->texture_properties().end()) { - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); - ImGui::Text("Unknown texture property"); - continue; - } + + auto& val = it.second; + + auto has_property = + shader->texture_properties().find(id) != shader->texture_properties().end(); ImGui::TableNextRow(); ImGui::PushID(it.first); - const auto& prop = shader->texture_properties().at(id); - auto& val = it.second; - ImGui::TableSetColumnIndex(0); bool browse_clicked = browse_texture_widget(val.texture, tex_size); ImGui::TableSetColumnIndex(1); - ImGui::Text("%s", prop.display_name.c_str()); + if (has_property) { + const auto& prop = shader->texture_properties().at(id); - if (prop.colormap) { - if (ImGui::BeginPopup("###Choose Colormap")) { - if (show_colormap_popup(*rptr, mat, it.first)) ImGui::CloseCurrentPopup(); - ImGui::EndPopup(); - } + ImGui::Text("%s", prop.display_name.c_str()); - if (ImGui::Button("Choose Colormap " ICON_FA_CARET_DOWN)) { - ImGui::OpenPopup("###Choose Colormap"); - } - ImGui::SameLine(); - } + if (prop.colormap) { + if (ImGui::BeginPopup("###Choose Colormap")) { + if (show_colormap_popup(*rptr, mat, it.first)) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } - if (val.texture) { - if (ImGui::Button("Clear texture")) { - val.texture = nullptr; + if (ImGui::Button("Choose Colormap " ICON_FA_CARET_DOWN)) { + ImGui::OpenPopup("###Choose Colormap"); + } + ImGui::SameLine(); } - } else { - if (prop.value_dimension == 1) { - ImGui::SliderFloat("##value", val.color.data(), 0, 1); - } else if (prop.value_dimension == 2) { - UIWidget("##value")(reinterpret_cast(val.color)); - } else if (prop.value_dimension == 3) { - Color c = Color(val.color.x(), val.color.y(), val.color.z(), 1.0f); - if (UIWidget("##value")(c)) { - val.color = c; + + if (val.texture) { + if (ImGui::Button("Clear texture")) { + val.texture = nullptr; } - } else if (prop.value_dimension == 4) { - Color c = val.color; - if (UIWidget("##value")(c)) { - val.color = c; + } else { + if (prop.value_dimension == 1) { + ImGui::SliderFloat("##value", val.color.data(), 0, 1); + } else if (prop.value_dimension == 2) { + UIWidget("##value")(reinterpret_cast(val.color)); + } else if (prop.value_dimension == 3) { + Color c = Color(val.color.x(), val.color.y(), val.color.z(), 1.0f); + if (UIWidget("##value")(c)) { + val.color = c; + } + } else if (prop.value_dimension == 4) { + Color c = val.color; + if (UIWidget("##value")(c)) { + val.color = c; + } } } - } - if (browse_clicked) { - Texture::Params p; - // todo srgb for base color? - - // If shader specifies value dimension, override texture upload defaults - if (prop.value_dimension == 1) { - p.format = GL_RED; - } else if (prop.value_dimension == 2) { - p.format = GL_RG; - } else if (prop.value_dimension == 3) { - p.format = GL_RGB; - } else if (prop.value_dimension == 4) { - p.format = GL_RGBA; + if (browse_clicked) { + Texture::Params p; + // todo srgb for base color? + + // If shader specifies value dimension, override texture upload defaults + if (prop.value_dimension == 1) { + p.format = GL_RED; + } else if (prop.value_dimension == 2) { + p.format = GL_RG; + } else if (prop.value_dimension == 3) { + p.format = GL_RGB; + } else if (prop.value_dimension == 4) { + p.format = GL_RGBA; + } + auto new_tex = load_texture_dialog(p); + if (new_tex) val.texture = new_tex; } - auto new_tex = load_texture_dialog(p); - if (new_tex) val.texture = new_tex; - } - if (prop.transformable) { - ImGui::Text("Transform:"); - UIWidget("rotation")(val.transform.rotation); - UIWidget("offset")(val.transform.offset); - UIWidget("scale")(val.transform.scale); + if (prop.transformable) { + ImGui::Text("Transform:"); + UIWidget("rotation")(val.transform.rotation); + UIWidget("offset")(val.transform.offset); + UIWidget("scale")(val.transform.scale); + } + } else { + ImGui::Text("%s", "Unknown texture property"); + if (val.texture) { + if (ImGui::Button("Clear texture")) { + val.texture = nullptr; + } + } } ImGui::PopID(); @@ -1040,7 +1048,7 @@ void show_layer(Registry* rptr, Entity e) for (size_t i = 0; i < get_max_layers(); i++) { - const auto& name = get_layer_name(*rptr, i); + const auto& name = get_layer_name(*rptr, LayerIndex(i)); if (name.length() == 0) continue; bool value = layer.test(i); if (ImGui::Checkbox(name.c_str(), &value)) { diff --git a/modules/ui/src/default_entities.cpp b/modules/ui/src/default_entities.cpp index 1adc7379..a7b87223 100644 --- a/modules/ui/src/default_entities.cpp +++ b/modules/ui/src/default_entities.cpp @@ -21,12 +21,14 @@ namespace lagrange { namespace ui { -std::shared_ptr create_material(Registry& r, entt::id_type shader_id) +std::shared_ptr +create_material(Registry& r, entt::id_type shader_id, const ShaderDefines& shader_defines) { - return std::make_shared(r, shader_id); + return std::make_shared(r, shader_id, shader_defines); } -PrimitiveType get_raster_primitive(Glyph g) { +PrimitiveType get_raster_primitive(Glyph g) +{ switch (g) { case lagrange::ui::Glyph::Surface: return PrimitiveType::TRIANGLES; case lagrange::ui::Glyph::Line: return PrimitiveType::LINES; @@ -64,8 +66,7 @@ Entity show_attribute_surface( if (raster_primitive == PrimitiveType::LINES) { mat.set_int(RasterizerOptions::PolygonMode, GL_LINE); - } - else if (raster_primitive == PrimitiveType::POINTS) { + } else if (raster_primitive == PrimitiveType::POINTS) { mat.set_int(RasterizerOptions::PolygonMode, GL_POINT); mat.set_int(RasterizerOptions::PointSize, 3); } else { @@ -208,8 +209,13 @@ Entity show_vertex_attribute( Entity e = ui::NullEntity; - if (glyph == Glyph::Surface || glyph == Glyph::Point) { - e = show_attribute_surface(registry, mesh_entity, IndexingMode::VERTEX, attribute, get_raster_primitive(glyph)); + if (glyph == Glyph::Surface || glyph == Glyph::Point) { + e = show_attribute_surface( + registry, + mesh_entity, + IndexingMode::VERTEX, + attribute, + get_raster_primitive(glyph)); } else if (glyph == Glyph::Line) { e = show_attribute_surface( registry, @@ -245,7 +251,12 @@ Entity show_facet_attribute( Entity e = ui::NullEntity; if (glyph == Glyph::Surface || glyph == Glyph::Point) { - e = show_attribute_surface(registry, mesh_entity, IndexingMode::FACE, attribute, get_raster_primitive(glyph)); + e = show_attribute_surface( + registry, + mesh_entity, + IndexingMode::FACE, + attribute, + get_raster_primitive(glyph)); } else if (glyph == Glyph::Line) { e = show_attribute_surface( registry, @@ -280,7 +291,7 @@ Entity show_corner_attribute( Entity e = ui::NullEntity; - if (glyph == Glyph::Surface ||glyph == Glyph::Point) { + if (glyph == Glyph::Surface || glyph == Glyph::Point) { e = show_attribute_surface( registry, mesh_entity, @@ -418,19 +429,23 @@ void clear_scene(Registry& r) }); } -Entity show_mesh(Registry& registry, const Entity& mesh_entity, StringID shader_id) +Entity show_mesh( + Registry& registry, + const Entity& mesh_entity, + StringID shader_id, + const ShaderDefines& shader_defines) { - return show_mesh(registry, mesh_entity, NullEntity, shader_id); + return show_mesh(registry, mesh_entity, NullEntity, shader_id, shader_defines); } Entity show_submesh( Registry& r, const Entity& mesh_entity, std::shared_ptr material, - entt::id_type material_id) + entt::id_type submesh_id) { auto e = show_mesh(r, mesh_entity, NullEntity, std::move(material)); - r.get(e).submesh_index = material_id; + r.get(e).submesh_index = submesh_id; return e; } @@ -439,9 +454,14 @@ Entity show_mesh( Registry& r, Entity mesh_entity, Entity scene_node_entity, - StringID shader /*= DefaultShaders::PBR*/) + StringID shader, + const ShaderDefines& shader_defines) { - return show_mesh(r, mesh_entity, scene_node_entity, ui::create_material(r, shader)); + return show_mesh( + r, + mesh_entity, + scene_node_entity, + ui::create_material(r, shader, shader_defines)); } Entity show_mesh( @@ -450,7 +470,8 @@ Entity show_mesh( Entity scene_node_entity, std::shared_ptr material) { - if (scene_node_entity == NullEntity) scene_node_entity = create_scene_node(r, ui::get_name(r, mesh_entity)); + if (scene_node_entity == NullEntity) + scene_node_entity = create_scene_node(r, ui::get_name(r, mesh_entity)); r.emplace(scene_node_entity, mesh_entity); MeshRender mr; diff --git a/modules/ui/src/default_shaders.cpp b/modules/ui/src/default_shaders.cpp index 0c914143..b1bc5a65 100644 --- a/modules/ui/src/default_shaders.cpp +++ b/modules/ui/src/default_shaders.cpp @@ -130,6 +130,14 @@ void register_default_shaders(Registry& r) d.display_name = "MeshElementID"; register_shader_as(r, DefaultShaders::MeshElementID, d); } + + { + ShaderDefinition d; + d.path = "skybox.shader"; + d.path_type = ShaderLoader::PathType::VIRTUAL; + d.display_name = "Skybox"; + register_shader_as(r, DefaultShaders::Skybox, d); + } } } // namespace ui diff --git a/modules/ui/src/default_tools.cpp b/modules/ui/src/default_tools.cpp index a378cb6e..043baad9 100644 --- a/modules/ui/src/default_tools.cpp +++ b/modules/ui/src/default_tools.cpp @@ -252,30 +252,29 @@ void register_default_tools(Tools& tools) tools.register_tool([](Registry& r) { select(r, select_element); }); - tools.register_tool([](Registry& r) { select(r, select_element); }); - tools.register_tool( [](Registry& r) { select(r, select_element); }); - /*tools.register_tool( - [](Registry& r) { - transform_tool_impl(r, GizmoMode::TRANSLATE); - }); - tools.register_tool( - [](Registry& r) { - transform_tool_impl(r, GizmoMode::ROTATE); - }); - tools.register_tool( - [](Registry& r) { - transform_tool_impl(r, GizmoMode::SCALE); - }); -*/ - + // Translation, pass through to selection + tools.register_tool( + [](Registry& r) { select(r, select_element); }); + tools.register_tool( + [](Registry& r) { select(r, select_element); }); + tools.register_tool( + [](Registry& r) { select(r, select_element); }); - /*tools.register_tool(select_object); - tools.register_tool(select_object);*/ + // Rotation, pass through to selection + tools.register_tool([](Registry& r) { select(r, select_element); }); + tools.register_tool([](Registry& r) { select(r, select_element); }); + tools.register_tool( + [](Registry& r) { select(r, select_element); }); + // Scale, pass through to selection + tools.register_tool([](Registry& r) { select(r, select_element); }); + tools.register_tool([](Registry& r) { select(r, select_element); }); + tools.register_tool( + [](Registry& r) { select(r, select_element); }); tools.set_current_element_type(entt::resolve().id()); tools.set_current_tool_type(entt::resolve().id()); diff --git a/modules/ui/src/panels/ScenePanel.cpp b/modules/ui/src/panels/ScenePanel.cpp index e9d62ff0..26d74c74 100644 --- a/modules/ui/src/panels/ScenePanel.cpp +++ b/modules/ui/src/panels/ScenePanel.cpp @@ -273,7 +273,7 @@ void scene_panel_system(Registry& registry, Entity panel_entity) if (ImGui::CollapsingHeader("Scene Panel Layers")) { ImGui::Indent(); // align with the treeview above for (size_t i = 0; i < get_max_layers(); i++) { - const auto& name = get_layer_name(registry, i); + const auto& name = get_layer_name(registry, LayerIndex(i)); if (name.length() == 0) continue; bool value = scene_panel_data.visible_layers.test(i); diff --git a/modules/ui/src/panels/ViewportPanel.cpp b/modules/ui/src/panels/ViewportPanel.cpp index 3813e293..6a1ad3c2 100644 --- a/modules/ui/src/panels/ViewportPanel.cpp +++ b/modules/ui/src/panels/ViewportPanel.cpp @@ -247,7 +247,7 @@ void draw_framebuffer_popup(Registry& registry, ViewportPanel& data) if (ImGui::CollapsingHeader("Layer visibility")) { for (size_t i = 0; i < get_max_layers(); i++) { - const auto& name = get_layer_name(registry, i); + const auto& name = get_layer_name(registry, LayerIndex(i)); if (name.length() == 0) continue; bool value = v.visible_layers.test(i); @@ -675,6 +675,10 @@ void viewport_panel_system(Registry& registry, Entity e) { ViewportPanel& data = registry.get(e); auto& input = get_input(registry); + if (!registry.valid(data.viewport) || !registry.has(data.viewport)) { + ImGui::Text("Invalid viewport"); + return; + } auto& viewport = registry.get(data.viewport); diff --git a/modules/ui/src/shaders/common.glsl.deprecated b/modules/ui/src/shaders/common.glsl.deprecated deleted file mode 100644 index 6bdb0994..00000000 --- a/modules/ui/src/shaders/common.glsl.deprecated +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright 2020 Adobe. All rights reserved. - * This file is licensed to you 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 REPRESENTATIONS - * OF ANY KIND, either express or implied. See the License for the specific language - * governing permissions and limitations under the License. - */ -#define M_PI 3.1415926535897932384626433832795 diff --git a/modules/ui/src/shaders/cubemap/convolve.shader b/modules/ui/src/shaders/cubemap/convolve.shader index 87e6499b..f820fdb7 100644 --- a/modules/ui/src/shaders/cubemap/convolve.shader +++ b/modules/ui/src/shaders/cubemap/convolve.shader @@ -33,11 +33,11 @@ uniform samplerCube texCube; void main(){ - + vec3 irradiance = vec3(0); - vec3 basis_Z = normalize(fs_in.pos); + vec3 basis_Z = normalize(fs_in.pos); vec3 basis_X = cross(vec3(0,1,0), basis_Z); vec3 basis_Y = cross(basis_Z, basis_X); diff --git a/modules/ui/src/shaders/cubemap/specular.shader b/modules/ui/src/shaders/cubemap/specular.shader index eb373561..8e571417 100644 --- a/modules/ui/src/shaders/cubemap/specular.shader +++ b/modules/ui/src/shaders/cubemap/specular.shader @@ -39,16 +39,16 @@ uniform float roughness = 0.0; //https://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf void main(){ - - vec3 normal = normalize(fs_in.pos); + + vec3 normal = normalize(fs_in.pos); vec3 lightIn = normal; vec3 lightOut = lightIn; - + float totalWeight = 0; vec3 color = vec3(0); for (uint i=0u; i < sampleCount; i++){ - vec2 sampleSpherical = hammersley2d(i,sampleCount); + vec2 sampleSpherical = hammersley2d(i,sampleCount); vec3 sampleDir = GGX_sample_dir(sampleSpherical, normal, roughness); vec3 lightDir = -reflect(lightOut, sampleDir); diff --git a/modules/ui/src/shaders/cubemap/to_cube.shader b/modules/ui/src/shaders/cubemap/to_cube.shader index 7df47f4a..30044294 100644 --- a/modules/ui/src/shaders/cubemap/to_cube.shader +++ b/modules/ui/src/shaders/cubemap/to_cube.shader @@ -32,7 +32,7 @@ uniform sampler2D texRectangular; void main(){ vec3 p = normalize(fs_in.pos); - + vec2 UV = vec2(atan(p.z,p.x),asin(p.y)); UV *= vec2(0.1591,0.3183); UV += vec2(0.5); diff --git a/modules/ui/src/shaders/default.shader.deprecated b/modules/ui/src/shaders/default.shader.deprecated deleted file mode 100644 index 4566af12..00000000 --- a/modules/ui/src/shaders/default.shader.deprecated +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2020 Adobe. All rights reserved. - * This file is licensed to you 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 REPRESENTATIONS - * OF ANY KIND, either express or implied. See the License for the specific language - * governing permissions and limitations under the License. - */ -#include "common.glsl" -#include "uniforms/common.glsl" - -#pragma VERTEX -#include "util/default.vertex" - - -#pragma FRAGMENT -layout(location = 0) out vec4 fragColor; - -in VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; -} fs_in; - -//Camera position in world space - -#include "material.glsl" -uniform MaterialPhong material; - -#include "phong.glsl" - - - -void main(){ - - vec3 lightPos = vec3(-5,5,0); - vec3 lightDir = normalize(lightPos - fs_in.pos); - vec3 viewDir = normalize(cameraPos - fs_in.pos); - vec3 N = normalize(fs_in.normal); - - - /*vec3 finalColor = vec3(0.0); - for(int i=0; i < lightsNum; i++){ - finalColor += phong(lights[i],material,fs_in.pos,viewDir,N); - }*/ - - vec3 finalColor = phong( - lightDir, viewDir, normalize(fs_in.normal) - ); - - - fragColor = vec4(finalColor.xyz, material.opacity); - //fragColor = vec4((N + vec3(1.0f)) * 0.5f,1); - - //vec3 testColor = texture2D(material.diffuse.texture,fs_in.uv).xyz; - //fragColor = vec4(testColor,1.0); - //fragColor = vec4(fs_in.uv,0,1.0); - - - - //normal - //fragColor = vec4(normalize(normal), 1); - - //fragColor = vec4(vec3(dot(reflect(-lightDir, N),viewDir)), 1); - - //depth - //fragColor = vec4(1.0 - vec3(length(cameraPos - pos)) / 3, material.opacity); -} diff --git a/modules/ui/src/shaders/depth/to_cubemap.shader b/modules/ui/src/shaders/depth/to_cubemap.shader index 7830d4d6..5a8c7eca 100644 --- a/modules/ui/src/shaders/depth/to_cubemap.shader +++ b/modules/ui/src/shaders/depth/to_cubemap.shader @@ -9,17 +9,10 @@ * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ +#include "uniforms/common.glsl" #pragma VERTEX - -in vec3 position; -uniform mat4 M; - -void main() -{ - gl_Position = M * vec4(position,1.0); -} - +#include "util/default.vertex" #pragma GEOMETRY @@ -34,7 +27,7 @@ void main(){ for(int f = 0; f < 6; f++){ - + gl_Layer = f; for(int i=0; i < 3; i++){ vposition = gl_in[i].gl_Position; @@ -42,7 +35,7 @@ void main(){ EmitVertex(); } EndPrimitive(); - + } } @@ -60,7 +53,7 @@ void main(){ //Distance to object float d = length(originPos - vposition.xyz); - //Normalize depth - gl_FragDepth = (d - near) / (far-near); + //Normalize depth + gl_FragDepth = (d - near) / (far-near); } diff --git a/modules/ui/src/shaders/depth/to_texture.shader b/modules/ui/src/shaders/depth/to_texture.shader index 5807ca97..2375ef76 100644 --- a/modules/ui/src/shaders/depth/to_texture.shader +++ b/modules/ui/src/shaders/depth/to_texture.shader @@ -9,37 +9,17 @@ * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ -#pragma VERTEX - -in vec3 position; -out vec4 vposition; - -uniform mat4 M; -uniform mat4 PV; - -void main() -{ - vposition = M * vec4(position,1.0); - gl_Position = PV * vposition; -} +#include "uniforms/common.glsl" +#pragma VERTEX +#include "util/default.vertex" #pragma FRAGMENT -in vec4 vposition; - - -uniform vec3 originPos; -uniform float far; -uniform float near; +#include "layout/default_fragment_layout.glsl" void main(){ - //Distance to object - //float d = length(originPos - vposition.xyz); - - //Normalize depth - //gl_FragDepth = d;//(d - near) / (far-near); } - - + + diff --git a/modules/ui/src/shaders/face_id.shader b/modules/ui/src/shaders/face_id.shader index 823a887e..3efabacd 100644 --- a/modules/ui/src/shaders/face_id.shader +++ b/modules/ui/src/shaders/face_id.shader @@ -13,46 +13,33 @@ #include "uniforms/common.glsl" #pragma VERTEX -#include "util/vertex_layout.glsl" +#include "layout/default_vertex_layout.glsl" -out VARYING { +out VARYING_ID { vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; flat int vertex_id; -} vs_out; - - +} vs_out_id; void main() -{ +{ //Pos and normal to world space - vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; - vs_out.normal = (NMat * vec4(in_normal,0.0)).xyz; - vs_out.uv = in_uv; - vs_out.color = in_color; - vs_out.vertex_id = gl_VertexID; + vs_out_id.pos = (M * vec4(in_pos, 1.0)).xyz; + vs_out_id.vertex_id = gl_VertexID; //To clip space gl_Position = PV * vec4(vs_out.pos,1.0); } - - - #pragma GEOMETRY layout (triangles) in; layout(triangle_strip, max_vertices = 4) out; -in VARYING { + +in VARYING_ID { vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; flat int vertex_id; -} gs_in[]; +} vs_in_id[]; out VARYING_GEOM { vec3 bary_pos; @@ -60,27 +47,25 @@ out VARYING_GEOM { flat ivec3 vertex_ids; } gs_out; - - void main() { gs_out.vertex_ids = ivec3( - gs_in[0].vertex_id, - gs_in[1].vertex_id, - gs_in[2].vertex_id + vs_in_id[0].vertex_id, + vs_in_id[1].vertex_id, + vs_in_id[2].vertex_id ); gs_out.primitive_id = gl_PrimitiveIDIn; - - gs_out.bary_pos = vec3(1,0,0); - gl_Position = PV * vec4(gs_in[0].pos,1); + + gs_out.bary_pos = vec3(1,0,0); + gl_Position = PV * vec4(vs_in_id[0].pos,1); EmitVertex(); gs_out.bary_pos = vec3(0,1,0); - gl_Position = PV * vec4(gs_in[1].pos,1); + gl_Position = PV * vec4(vs_in_id[1].pos,1); EmitVertex(); gs_out.bary_pos = vec3(0,0,1); - gl_Position = PV * vec4(gs_in[2].pos,1); + gl_Position = PV * vec4(vs_in_id[2].pos,1); EmitVertex(); EndPrimitive(); @@ -116,11 +101,12 @@ uniform bool debug_output = false; void main(){ + vec3 b = fs_in.bary_pos; //Get closest vertex id color (largest bary coord) if(element_mode == ELEMENT_VERTEX){ - + if(b.x > b.y){ if(b.x > b.z){ fragColor = index_to_color(fs_in.vertex_ids[0]); @@ -131,7 +117,7 @@ void main(){ } else { if(b.y > b.z){ - fragColor = index_to_color(fs_in.vertex_ids[1]); + fragColor = index_to_color(fs_in.vertex_ids[1]); } else{ fragColor = index_to_color(fs_in.vertex_ids[2]); @@ -141,19 +127,19 @@ void main(){ } //Get closest edge id color (smallest bary coord) //Indexed as 3*face_id + edge_id, where edge_id = 0,1,2 - else if(element_mode == ELEMENT_EDGE){ - + else if(element_mode == ELEMENT_EDGE){ + int edge_id = 3 * fs_in.primitive_id; if(b.x < b.y){ if(b.x < b.z){ - //fragColor = vec4(1,0,0,1); + //fragColor = vec4(1,0,0,1); edge_id += 0; if(b.x > 0.1){ discard; } } else { - //fragColor = vec4(0,0,1,1); + //fragColor = vec4(0,0,1,1); edge_id += 2; if(b.z > 0.1){ discard; @@ -162,14 +148,14 @@ void main(){ } else { if(b.y < b.z){ - //fragColor = vec4(0,1,0,1); + //fragColor = vec4(0,1,0,1); edge_id += 1; if(b.y > 0.1){ discard; } } else{ - //fragColor = vec4(0,0,1,1); + //fragColor = vec4(0,0,1,1); edge_id += 2; if(b.z > 0.1){ discard; @@ -185,7 +171,7 @@ void main(){ if(debug_output){ fragColor.xyz *= 5; - + vec2 d = vec2( length(dFdx(fragColor.xyz)), length(dFdy(fragColor.xyz)) diff --git a/modules/ui/src/shaders/layout/default_fragment_layout.glsl b/modules/ui/src/shaders/layout/default_fragment_layout.glsl new file mode 100644 index 00000000..52b5f1ca --- /dev/null +++ b/modules/ui/src/shaders/layout/default_fragment_layout.glsl @@ -0,0 +1,11 @@ + +layout(location = 0) out vec4 fragColor; + +in VARYING { + vec3 pos; + vec3 normal; + vec2 uv; + vec4 color; + vec3 tangent; + vec3 bitangent; +} fs_in; \ No newline at end of file diff --git a/modules/ui/src/shaders/util/vertex_layout.glsl b/modules/ui/src/shaders/layout/default_vertex_layout.glsl similarity index 69% rename from modules/ui/src/shaders/util/vertex_layout.glsl rename to modules/ui/src/shaders/layout/default_vertex_layout.glsl index 1a31e85a..a00c895c 100644 --- a/modules/ui/src/shaders/util/vertex_layout.glsl +++ b/modules/ui/src/shaders/layout/default_vertex_layout.glsl @@ -12,6 +12,19 @@ layout (location = 0) in vec3 in_pos; layout (location = 1) in vec3 in_normal; layout (location = 2) in vec2 in_uv; -layout (location = 3) in vec4 in_color; -layout (location = 4) in vec4 in_tangent; -layout (location = 5) in vec4 in_bitangent; +layout (location = 3) in vec4 in_tangent; +layout (location = 4) in vec4 in_bitangent; + +#ifdef SKELETAL +layout (location = 5) in vec4 in_bone_ids; +layout (location = 6) in vec4 in_bone_weights; +#endif + +out VARYING { + vec3 pos; + vec3 normal; + vec2 uv; + vec4 color; + vec3 tangent; + vec3 bitangent; +} vs_out; \ No newline at end of file diff --git a/modules/ui/src/shaders/lines/attribute_to_lines.shader b/modules/ui/src/shaders/lines/attribute_to_lines.shader index 33f25f8b..ab58e2e0 100644 --- a/modules/ui/src/shaders/lines/attribute_to_lines.shader +++ b/modules/ui/src/shaders/lines/attribute_to_lines.shader @@ -18,7 +18,7 @@ layout (location = 0) in vec3 in_pos; layout (location = 1) in vec4 in_value; layout (location = 2) in vec3 in_normal; -out VARYING { +out VARYING { vec3 pos; vec3 normal; vec4 value; @@ -26,10 +26,10 @@ out VARYING { } vs_out; void main() -{ +{ //Pos and normal to world space - vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; - vs_out.value = in_value; + vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; + vs_out.value = in_value; vs_out.normal = (NMat * vec4(in_normal,0.0)).xyz; vec4 clip_space = PV * vec4(vs_out.pos,1.0); @@ -71,15 +71,15 @@ void main() { gs_out.value = gs_in[k].value; gs_out.normal = gs_in[k].normal; - + gs_out.pos = gs_in[k].pos; gl_Position = PV * vec4(gs_out.pos,1); h *= gl_Position.w; gs_out.edge_dist = vec3(0); gs_out.edge_dist[k] = h; - - EmitVertex(); + + EmitVertex(); } EndPrimitive(); } @@ -98,19 +98,19 @@ in VARYING_GEOM { #include "surface/colormap.frag" -#pragma property line_width "Line Width" float(1,0,100) -#pragma property opacity "Opacity" float(1,0,1) +#pragma property line_width "Line Width" float(1,0,100) +#pragma property opacity "Opacity" float(1,0,1) void main(){ float d = min(fs_in.edge_dist[0],min(fs_in.edge_dist[1],fs_in.edge_dist[2])); - d = max(d - line_width, 0); //expand width + d = max(d - line_width, 0); //expand width d *= gl_FragCoord.w; //perspective correction d = exp2(-2 * d * d); //edge function - + //fragColor = fs_in.value; fragColor = colormapped_fragment_color(fs_in.value); fragColor.a *= d * opacity; diff --git a/modules/ui/src/shaders/lines/edge_to_line.shader b/modules/ui/src/shaders/lines/edge_to_line.shader index ad74f2bd..98f87c81 100644 --- a/modules/ui/src/shaders/lines/edge_to_line.shader +++ b/modules/ui/src/shaders/lines/edge_to_line.shader @@ -12,7 +12,7 @@ #include "uniforms/common.glsl" #pragma VERTEX -#include "util/vertex_layout.glsl" +#include "layout/default_vertex_layout.glsl" out VARYING { diff --git a/modules/ui/src/shaders/lines/triangle_to_lines.shader b/modules/ui/src/shaders/lines/triangle_to_lines.shader index 317756a7..a98b7921 100644 --- a/modules/ui/src/shaders/lines/triangle_to_lines.shader +++ b/modules/ui/src/shaders/lines/triangle_to_lines.shader @@ -13,29 +13,26 @@ #pragma VERTEX -#include "util/vertex_layout.glsl" +#include "layout/default_vertex_layout.glsl" -//Default color when attribute is not set -#pragma property triangle_color "Triangle Color" Color(0,0,0,0) +//Default color when attribute is not set +#pragma property in_color "Default Color" Color(0,0,0,0) -out VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; + +out VARYING_SCREEN{ vec2 screen_pos; -} vs_out; +} screen_out; void main() -{ +{ //Pos and normal to world space - vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; + vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; vs_out.normal = (NMat * vec4(in_normal,0.0)).xyz; vs_out.uv = in_uv; - vs_out.color = has_color_attrib ? in_color : triangle_color; + vs_out.color = in_color; vec4 clip_space = PV * vec4(vs_out.pos,1.0); - vs_out.screen_pos = screen_size * 0.5f * (clip_space.xy / clip_space.w); + screen_out.screen_pos = screen_size * 0.5f * (clip_space.xy / clip_space.w); //To clip space gl_Position = clip_space; @@ -51,15 +48,21 @@ in VARYING { vec3 normal; vec2 uv; vec4 color; - vec2 screen_pos; + vec3 tangent; + vec3 bitangent; + } gs_in[]; +in VARYING_SCREEN { + vec2 screen_pos; +} gs_screen_in[]; + out VARYING_GEOM { vec3 pos; vec3 normal; vec2 uv; - vec4 color; + vec4 color; vec3 edge_dist; } gs_out; @@ -67,23 +70,23 @@ out VARYING_GEOM { void main() { for(int k = 0; k < 3; k++){ - vec2 u = gs_in[(k+1) % 3].screen_pos - gs_in[k].screen_pos; - vec2 v = gs_in[(k+2) % 3].screen_pos - gs_in[k].screen_pos; + vec2 u = gs_screen_in[(k+1) % 3].screen_pos - gs_screen_in[k].screen_pos; + vec2 v = gs_screen_in[(k+2) % 3].screen_pos - gs_screen_in[k].screen_pos; float area_squared = abs(u.x*v.y - u.y*v.x); float h = area_squared / length(u-v); gs_out.color = gs_in[k].color; gs_out.normal = gs_in[k].normal; gs_out.color.a *= alpha_multiplier; - + gs_out.pos = gs_in[k].pos; gl_Position = PV * vec4(gs_out.pos,1); h *= gl_Position.w; gs_out.edge_dist = vec3(0); gs_out.edge_dist[k] = abs(h); // absolute value is needed when vertex is behind camera - - EmitVertex(); + + EmitVertex(); } EndPrimitive(); } @@ -100,7 +103,7 @@ in VARYING_GEOM { vec3 edge_dist; } fs_in; -#pragma property line_width "Line Width" float(1,1,10) +#pragma property line_width "Line Width" float(1,1,10) #pragma property line_color "Line Color" Color(0,0,0,1) void main(){ diff --git a/modules/ui/src/shaders/normals/triangle.shader b/modules/ui/src/shaders/normals/triangle.shader index 3924c6af..49b9b6b3 100644 --- a/modules/ui/src/shaders/normals/triangle.shader +++ b/modules/ui/src/shaders/normals/triangle.shader @@ -52,15 +52,15 @@ void main() { vec3 n = normalize(cross(u,v)); vec3 end = center + n * line_length; - + gs_out.pos_a = center; gs_out.pos_b = end; gs_out.n = n; - - gl_Position = PV * vec4(center,1); + + gl_Position = PV * vec4(center,1); EmitVertex(); - gl_Position = PV * vec4(end,1); + gl_Position = PV * vec4(end,1); EmitVertex(); EndPrimitive(); @@ -87,5 +87,5 @@ void main(){ if(use_direction_color) fragColor = vec4(fs_in.n, color.a); else - fragColor = color; + fragColor = color; } diff --git a/modules/ui/src/shaders/normals/vertex.shader b/modules/ui/src/shaders/normals/vertex.shader index 04920cd5..4a5910e4 100644 --- a/modules/ui/src/shaders/normals/vertex.shader +++ b/modules/ui/src/shaders/normals/vertex.shader @@ -42,15 +42,15 @@ void main() { vec3 center = gs_in[0].pos; vec3 n = normalize(gs_in[0].normal); vec3 end = center + n * line_length; - + gs_out.pos_a = center; gs_out.pos_b = end; gs_out.n = n; - - gl_Position = PV * vec4(center,1); + + gl_Position = PV * vec4(center,1); EmitVertex(); - gl_Position = PV * vec4(end,1); + gl_Position = PV * vec4(end,1); EmitVertex(); EndPrimitive(); @@ -74,5 +74,5 @@ void main(){ if(use_direction_color) fragColor = vec4(fs_in.n, color.a); else - fragColor = color; + fragColor = color; } diff --git a/modules/ui/src/shaders/outline.shader b/modules/ui/src/shaders/outline.shader deleted file mode 100644 index 1f79dbf4..00000000 --- a/modules/ui/src/shaders/outline.shader +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2020 Adobe. All rights reserved. - * This file is licensed to you 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 REPRESENTATIONS - * OF ANY KIND, either express or implied. See the License for the specific language - * governing permissions and limitations under the License. - */ -#pragma VERTEX - -in vec3 position; -out vec3 vposition; - -void main() { - gl_Position = vec4(position,1); - vposition = position; -} - -#pragma FRAGMENT - -in vec3 vposition; -uniform sampler2D tex; -uniform vec4 outline_color; -uniform vec4 fill_color; -out vec4 fragColor; - -void main(){ - - - vec2 coord = (vposition.xy + vec2(1)) * 0.5; - ivec2 screen_size = textureSize(tex,0); - - vec2 dx = vec2(1.0 / screen_size.x, 0); - vec2 dy = vec2(0, 1.0 / screen_size.y); - - - float t = texture(tex,coord).x; - float tup = texture(tex,coord -dy).x; - float tdown = texture(tex,coord + dy ).x; - float tleft = texture(tex,coord - dx).x; - float tright = texture(tex,coord + dx).x; - - - float val = 0; - val = abs(tup - tdown) + abs(tdown - tup); - val += abs(tleft - tright) + abs(tright - tleft); - - bool has_diff = (t != tup) || (t != tdown) || (t != tleft) || (t != tright); - - if(has_diff){ - fragColor = outline_color; - if(!(t > 0)){ - fragColor.a = 0.5; - } - } - else if(t > 0 && fill_color.a > 0){ - fragColor = fill_color; - } - else{ - discard; - } - - - - -} diff --git a/modules/ui/src/shaders/points/points.shader b/modules/ui/src/shaders/points/points.shader index 4d9d7e75..3315a8d2 100644 --- a/modules/ui/src/shaders/points/points.shader +++ b/modules/ui/src/shaders/points/points.shader @@ -13,7 +13,7 @@ #pragma VERTEX -#include "util/vertex_layout.glsl" +#include "layout/default_vertex_layout.glsl" out VARYING { vec3 pos; @@ -24,13 +24,13 @@ out VARYING { } vs_out; void main() -{ +{ //Pos and normal to world space vs_out.pos += normalize(cameraPos - in_pos); - vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; + vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; vs_out.normal = (NMat * vec4(in_normal,0.0)).xyz; vs_out.uv = in_uv; - vs_out.color = in_color; + vs_out.color = in_color; vec4 clip_space = PV * vec4(vs_out.pos,1.0); vs_out.screen_pos = screen_size * 0.5f * (clip_space.xy / clip_space.w); @@ -57,7 +57,7 @@ out VARYING_GEOM { vec3 pos; vec3 normal; vec2 uv; - vec4 color; + vec4 color; vec4 dist; vec2 center; } gs_out; @@ -67,19 +67,19 @@ uniform float point_size = 5.0f; void main() { - + //for(int k=0; k < 1; k++){ vec4 clip = PV * vec4(gs_in[0].pos,1); //vec4 clip_max = PV * vec4(gs_in[0].pos + (cameraPos - gs_in[0].pos),1); - - + + vec2 screen = (clip.xy / clip.w) * screen_size; gs_out.center = screen; gs_out.color = gs_in[0].color; /*vec3 u = gs_in[0].pos - gs_in[1].pos; vec3 v = gs_in[0].pos - gs_in[2].pos; - + vec3 n = cross(u,v);*/ vec3 n = gs_in[0].normal; float dir = dot(gs_in[0].pos - cameraPos,n); @@ -105,30 +105,30 @@ void main() { //backface if(dir > 0){ gl_Position = ca; - EmitVertex(); + EmitVertex(); gl_Position = cb; - EmitVertex(); + EmitVertex(); gl_Position = cd; - EmitVertex(); + EmitVertex(); gl_Position = cc; - EmitVertex(); + EmitVertex(); } //Frontface else{ gl_Position = ca; - EmitVertex(); + EmitVertex(); gl_Position = cd; - EmitVertex(); + EmitVertex(); gl_Position = cb; - EmitVertex(); + EmitVertex(); gl_Position = cc; - EmitVertex(); + EmitVertex(); } EndPrimitive(); @@ -155,7 +155,7 @@ void main(){ vec2 center = (((fs_in.center / screen_size) + 1) / 2.0) * screen_size; float d = length(center - gl_FragCoord.xy); - + d = d / (0.4 * point_size); // ~0-1 radius float d2 = d*d; float d4= d2*d2; diff --git a/modules/ui/src/shaders/post/FXAA.shader b/modules/ui/src/shaders/post/FXAA.shader index fdb28206..e3dc4314 100644 --- a/modules/ui/src/shaders/post/FXAA.shader +++ b/modules/ui/src/shaders/post/FXAA.shader @@ -42,11 +42,11 @@ layout(location = 0) out vec4 fragColor; #define FxaaTexLod0(t, p) textureLod(t, p, 0.0) #define FxaaTexOff(t, p, o, r) textureLodOffset(t, p, 0.0, o) -vec3 FxaaPixelShader( +vec3 FxaaPixelShader( vec4 posPos, // Output of FxaaVertexShader interpolated across screen. sampler2D tex, // Input texture. vec2 rcpFrame) // Constant {1.0/frameWidth, 1.0/frameHeight}. -{ +{ /*---------------------------------------------------------*/ #define FXAA_REDUCE_MIN (1.0/128.0) //#define FXAA_REDUCE_MUL (1.0/8.0) @@ -68,7 +68,7 @@ vec3 FxaaPixelShader( float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); /*---------------------------------------------------------*/ - vec2 dir; + vec2 dir; dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); /*---------------------------------------------------------*/ @@ -76,8 +76,8 @@ vec3 FxaaPixelShader( (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(FxaaFloat2( FXAA_SPAN_MAX, FXAA_SPAN_MAX), - max(FxaaFloat2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), + dir = min(FxaaFloat2( FXAA_SPAN_MAX, FXAA_SPAN_MAX), + max(FxaaFloat2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), dir * rcpDirMin)) * rcpFrame.xy; /*--------------------------------------------------------*/ vec3 rgbA = (1.0/2.0) * ( @@ -98,9 +98,9 @@ vec4 PostFX(sampler2D tex, vec2 uv, float time) c.a = 1.0; return c; } - -void main() -{ + +void main() +{ //vec2 uv = gl_TexCoord[0].st; fragColor = PostFX(tex0, uv, 0.0); } diff --git a/modules/ui/src/shaders/post/outline.shader b/modules/ui/src/shaders/post/outline.shader index 671cb6be..296e5f94 100644 --- a/modules/ui/src/shaders/post/outline.shader +++ b/modules/ui/src/shaders/post/outline.shader @@ -20,7 +20,7 @@ out vec4 vcolor; void main() { gl_Position = vec4(in_pos,1); - vposition = in_pos; + vposition = in_pos; vcolor = in_color; } @@ -44,17 +44,17 @@ void main(){ vec2 coord = (vposition.xy + vec2(1)) * 0.5; float depth = texture(depth_tex, coord).r; - + ivec2 screen_size = textureSize(color_tex,0); vec2 dx = vec2(1.0 / screen_size.x, 0) * dpos; vec2 dy = vec2(0, 1.0 / screen_size.y) * dpos; - vec3 t = texture(color_tex,coord).rgb; - vec3 tup = texture(color_tex,coord -dy).rgb; + vec3 t = texture(color_tex,coord).rgb; + vec3 tup = texture(color_tex,coord -dy).rgb; vec3 tdown = texture(color_tex,coord + dy ).rgb; vec3 tleft = texture(color_tex,coord - dx).rgb; vec3 tright = texture(color_tex,coord + dx).rgb; - + float d = 0.0001; bool has_diff = len2(t - tup) > d || len2(t - tdown) > d || len2(t - tleft) > d || len2(t - tright) > d; @@ -67,7 +67,7 @@ void main(){ else{ discard; } - + gl_FragDepth = depth; } diff --git a/modules/ui/src/shaders/post/tonemap.shader b/modules/ui/src/shaders/post/tonemap.shader index f35c62dc..78a55a17 100644 --- a/modules/ui/src/shaders/post/tonemap.shader +++ b/modules/ui/src/shaders/post/tonemap.shader @@ -16,7 +16,7 @@ out vec3 vposition; void main() { gl_Position = vec4(position,1); - vposition = position; + vposition = position; } diff --git a/modules/ui/src/shaders/procedural/ground.shader b/modules/ui/src/shaders/procedural/ground.shader index 3a690c48..64719ef7 100644 --- a/modules/ui/src/shaders/procedural/ground.shader +++ b/modules/ui/src/shaders/procedural/ground.shader @@ -16,7 +16,7 @@ //Takes default vertex data #pragma VERTEX -#include "util/vertex_layout.glsl" +#include "layout/default_vertex_layout.glsl" //assumes "uniforms_common" has been included out VARYING { vec3 pos; @@ -29,14 +29,14 @@ out VARYING { void main() -{ - +{ + vs_out.pos = vec3(in_pos); vs_out.normal = in_normal; vs_out.uv = in_uv; - vs_out.color = in_color; + vs_out.color = in_color; vs_out.tangent = vec3(in_tangent); - vs_out.bitangent = vec3(in_bitangent); + vs_out.bitangent = vec3(in_bitangent); gl_Position = vec4(vs_out.pos,1); } @@ -88,7 +88,7 @@ float compute_line(vec2 uv, float period, float width){ return line; } -vec2 compute_axes(vec2 uv, float period, float width){ +vec2 compute_axes(vec2 uv, float period, float width){ vec2 coord = abs(uv) / period; vec2 grid = coord; grid = grid / fwidth(coord * width); @@ -150,7 +150,7 @@ vec4 color_at(vec3 pos){ vec2 uv = pos.xz; - if(show_grid){ + if(show_grid){ float l0 = compute_line(uv, period_primary, 3); float l1 = compute_line(uv, period_secondary, 1); @@ -162,7 +162,7 @@ vec4 color_at(vec3 pos){ if(show_axes){ vec2 ax = compute_axes(uv,period_primary,2); - + color.xyz = mix(color.xyz, vec3(1,0,0), ax.x); color.a = mix(color.a, ax.x, ax.x); @@ -175,7 +175,7 @@ vec4 color_at(vec3 pos){ vec4 sample_between(vec3 pos, vec3 posX, vec3 posY, int maxSamples){ - + vec4 color = vec4(0); vec3 dx = posX - pos; vec3 dy = posY - pos; @@ -196,9 +196,9 @@ float recover_depth(vec3 pos){ float near = 0.0f; float far = 1.0f; - + float clip_depth = pos_clip.z / pos_clip.w; - float depth_ndc = (1.0 - 0.0) * 0.5 * clip_depth + (1.0 + 0.0) * 0.5; + float depth_ndc = (1.0 - 0.0) * 0.5 * clip_depth + (1.0 + 0.0) * 0.5; return depth_ndc; } @@ -206,17 +206,17 @@ float recover_depth(vec3 pos){ void main(){ vec2 xy = gl_FragCoord.xy * screen_size_inv; - + //Get primary ray vec3 rayOrigWorld; vec3 rayWorld = calc_ray(xy, rayOrigWorld); vec3 pos; vec3 N; - + float t = isect_plane(rayOrigWorld, rayWorld, pos, N); - - + + if(camera_is_ortho){ t = abs(t); } @@ -224,7 +224,7 @@ void main(){ if(t <= 0) discard; } - + //Get rays for position occupancy vec3 rayOrigX,rayOrigY; vec3 rayX = calc_ray(xy + vec2(1,0) * screen_size_inv,rayOrigX); @@ -236,14 +236,14 @@ void main(){ //Sample between positions vec4 color = sample_between(pos,posX,posY,10); - + if(shadow_catcher){ - + float attenuation = 1.0; for(int i = 0; i < spot_lights_count; i++){ attenuation *= get_shadow_at(pos, spot_lights[i]); - } + } for(int i = 0; i < directional_lights_count; i++){ attenuation *= get_shadow_at(pos, directional_lights[i]); @@ -253,8 +253,8 @@ void main(){ attenuation *= get_shadow_at(pos, point_lights[i]); } - float shadow = 1 - attenuation; - shadow *= shadow_catcher_alpha; + float shadow = 1 - attenuation; + shadow *= shadow_catcher_alpha; vec4 shadow_color = vec4(vec3(1 - shadow), 1); color = vec4(mix(shadow_color.xyz, color.xyz, color.a), 1); } @@ -269,7 +269,7 @@ void main(){ discard; } - fragColor = vec4(color); + fragColor = vec4(color); gl_FragDepth = recover_depth(pos); } diff --git a/modules/ui/src/shaders/skybox.shader b/modules/ui/src/shaders/skybox.shader index dd457df1..9f86369d 100644 --- a/modules/ui/src/shaders/skybox.shader +++ b/modules/ui/src/shaders/skybox.shader @@ -14,14 +14,14 @@ layout (location = 0) in vec3 in_pos; out VARYING { - vec3 pos; + vec3 pos; } vs_out; uniform mat4 PV; void main() -{ +{ vs_out.pos = in_pos; gl_Position = (PV * vec4(vs_out.pos,1.0)).xyww; } @@ -40,11 +40,11 @@ uniform float mip_level = 0.0f; void main(){ vec3 p = normalize(fs_in.pos); - - fragColor = textureLod(texCubemap, p.xyz, mip_level); + + fragColor = textureLod(texCubemap, p.xyz, mip_level); //gamma correction const float gamma = 2.2; fragColor.xyz = pow(fragColor.xyz, vec3(1.0 / gamma)); - + } diff --git a/modules/ui/src/shaders/surface/colormap.frag b/modules/ui/src/shaders/surface/colormap.frag index 59697aa1..95ac3b9d 100644 --- a/modules/ui/src/shaders/surface/colormap.frag +++ b/modules/ui/src/shaders/surface/colormap.frag @@ -1,6 +1,6 @@ -#pragma property range_min "RangeMin" Vector(0,0,0,0) -#pragma property range_max "RangeMax" Vector(1,1,1,1) +#pragma property range_min "RangeMin" Vector(0,0,0,0) +#pragma property range_max "RangeMax" Vector(1,1,1,1) #pragma property colormap "Colormap" Texture2D() [colormap] vec4 colormap_fn(vec4 value){ @@ -38,7 +38,7 @@ vec4 colormap_fn(vec2 value){ vec4 colormap_fn(float value){ float nval = (value - range_min.x) / (range_max.x - range_min.x); - + if(!colormap_texture_bound){ return vec4(nval, nval, nval, 1); } @@ -49,15 +49,15 @@ vec4 colormap_fn(float value){ vec4 colormapped_fragment_color(vec4 value){ vec4 out_color = vec4(0,0,0,1); - + if(range_min.w == range_max.w){ if(range_min.z == range_max.z){ if(range_min.y == range_max.y){ - out_color = colormap_fn(value.x); + out_color = colormap_fn(value.x); } else { - out_color = colormap_fn(vec2(value)); + out_color = colormap_fn(vec2(value)); } } else { @@ -67,7 +67,7 @@ vec4 colormapped_fragment_color(vec4 value){ else { out_color = colormap_fn(value); } - + return out_color; diff --git a/modules/ui/src/shaders/surface/edge_attribute.shader b/modules/ui/src/shaders/surface/edge_attribute.shader index a618102f..1c4f4583 100644 --- a/modules/ui/src/shaders/surface/edge_attribute.shader +++ b/modules/ui/src/shaders/surface/edge_attribute.shader @@ -22,10 +22,10 @@ out VARYING { } vs_out; void main() -{ +{ //Pos and normal to world space - vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; - vs_out.value = in_value; + vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; + vs_out.value = in_value; //To clip space gl_Position = PV * vec4(vs_out.pos,1.0); @@ -77,7 +77,7 @@ void main(){ emit(0,2); emit(center_pos,center_value); EndPrimitive(); - + } #pragma FRAGMENT @@ -91,10 +91,10 @@ in VARYING_GEOM { #include "surface/colormap.frag" -#pragma property opacity "Opacity" float(1,0,1) +#pragma property opacity "Opacity" float(1,0,1) void main(){ - + fragColor = colormapped_fragment_color(fs_in.value); fragColor.a *= opacity; } diff --git a/modules/ui/src/shaders/surface/edge_to_surface.shader b/modules/ui/src/shaders/surface/edge_to_surface.shader index 0017cbad..87426c48 100644 --- a/modules/ui/src/shaders/surface/edge_to_surface.shader +++ b/modules/ui/src/shaders/surface/edge_to_surface.shader @@ -14,8 +14,6 @@ #pragma VERTEX #include "util/default.vertex" - - #pragma GEOMETRY layout(triangles) in; @@ -78,7 +76,7 @@ void main(){ emit(0,2); emit(center_pos,center_color,center_normal,center_uv,center_tangent, center_bitangent); EndPrimitive(); - + } #pragma FRAGMENT diff --git a/modules/ui/src/shaders/surface/flat.frag b/modules/ui/src/shaders/surface/flat.frag index ac88aae1..aae3ebcd 100644 --- a/modules/ui/src/shaders/surface/flat.frag +++ b/modules/ui/src/shaders/surface/flat.frag @@ -9,17 +9,7 @@ * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ -layout(location = 0) out vec4 fragColor; - -in VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; - vec3 tangent; - vec3 bitangent; -} fs_in; - +#include "layout/default_fragment_layout.glsl" void main(){ if(has_color_attrib) diff --git a/modules/ui/src/shaders/surface/objectid.shader b/modules/ui/src/shaders/surface/objectid.shader index dbfaf6f8..fe0ac995 100644 --- a/modules/ui/src/shaders/surface/objectid.shader +++ b/modules/ui/src/shaders/surface/objectid.shader @@ -14,12 +14,8 @@ #pragma VERTEX #include "util/default.vertex" - - #pragma FRAGMENT -layout(location = 0) out vec4 fragColor; - - +#include "layout/default_fragment_layout.glsl" vec4 index_to_color(int i){ int r = (i & 0x000000FF); @@ -28,18 +24,6 @@ vec4 index_to_color(int i){ return vec4(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f); } - - - -in VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; - vec3 tangent; - vec3 bitangent; -} fs_in; - void main(){ fragColor = index_to_color(object_id); } diff --git a/modules/ui/src/shaders/surface/pbr.frag b/modules/ui/src/shaders/surface/pbr.frag index b65d125e..3d007aa9 100644 --- a/modules/ui/src/shaders/surface/pbr.frag +++ b/modules/ui/src/shaders/surface/pbr.frag @@ -9,25 +9,11 @@ * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ -layout(location = 0) out vec4 fragColor; - -in VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; - vec3 tangent; - vec3 bitangent; -} fs_in; +#include "layout/default_fragment_layout.glsl" #include "util/pbr_shading.glsl" void main(){ - - //discard; fragColor = pbr(fs_in.pos, fs_in.normal, fs_in.uv, fs_in.color, fs_in.tangent, fs_in.bitangent); fragColor.a *= alpha_multiplier; - - - } \ No newline at end of file diff --git a/modules/ui/src/shaders/surface/phong.frag b/modules/ui/src/shaders/surface/phong.frag index 902d8ed8..84104ded 100644 --- a/modules/ui/src/shaders/surface/phong.frag +++ b/modules/ui/src/shaders/surface/phong.frag @@ -9,18 +9,8 @@ * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ -layout(location = 0) out vec4 fragColor; - -in VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; - vec3 tangent; - vec3 bitangent; -} fs_in; - +#include "layout/default_fragment_layout.glsl" #include "uniforms/lights.glsl" #pragma property material_base_color "Base Color" Texture2D(0.7,0.7,0.7,1) @@ -32,10 +22,10 @@ uniform samplerCube ibl_diffuse; void main(){ - vec4 baseColor_ = material_base_color_texture_bound ? + vec4 baseColor_ = material_base_color_texture_bound ? texture(material_base_color, fs_in.uv) : material_base_color_default_value; - + vec3 baseColor = baseColor_.xyz; if(has_color_attrib){ @@ -45,7 +35,7 @@ void main(){ baseColor = uniform_color.xyz; } - + fragColor = vec4(0,0,0,0); @@ -58,30 +48,30 @@ void main(){ if(has_ibl) irradiance_diffuse = texture(ibl_diffuse, ibl_dir).xyz; - + //Perturb normal in tangent space if(material_normal_texture_bound){ vec3 T = normalize(fs_in.tangent); vec3 BT = normalize(fs_in.bitangent); - mat3 TBN = mat3(T, BT, N); + mat3 TBN = mat3(T, BT, N); vec3 N_in_TBN = TBN * N; vec3 Ntex = texture(material_normal, fs_in.uv).xyz * 2.0f - vec3(1.0f); vec3 Ntex_in_world = TBN * Ntex; - N = normalize(Ntex_in_world); + N = normalize(Ntex_in_world); } - + vec3 color = vec3(0); - + vec3 lightOut = normalize(cameraPos - fs_in.pos); float cos_out = max(0.0, dot(lightOut,N)); //n dot v - - + + for(int light_type = 0; light_type < 3; light_type++){ @@ -99,33 +89,33 @@ void main(){ vec3 lightIn = vec3(0); switch(light_type){ - case LIGHT_TYPE_SPOT: - lightPos = spot_lights[i].position; - lightIn = normalize(lightPos - fs_in.pos); - intensity = spot_lights[i].intensity; + case LIGHT_TYPE_SPOT: + lightPos = spot_lights[i].position; + lightIn = normalize(lightPos - fs_in.pos); + intensity = spot_lights[i].intensity; break; - case LIGHT_TYPE_POINT: - lightPos = point_lights[i].position; - lightIn = normalize(lightPos - fs_in.pos); - intensity = point_lights[i].intensity; + case LIGHT_TYPE_POINT: + lightPos = point_lights[i].position; + lightIn = normalize(lightPos - fs_in.pos); + intensity = point_lights[i].intensity; break; - case LIGHT_TYPE_DIRECTIONAL: + case LIGHT_TYPE_DIRECTIONAL: lightIn = normalize(-directional_lights[i].direction); - intensity = directional_lights[i].intensity; + intensity = directional_lights[i].intensity; break; }; - + vec3 lightHalf = normalize(lightIn + lightOut); float lightDistance = length(lightPos - fs_in.pos); - + float attenuation = 1.0f; float shadow_attenuation = 1.0f; //Compute attenuation + shadow if(light_type == LIGHT_TYPE_SPOT){ shadow_attenuation = getShadowSquare( - fs_in.pos, + fs_in.pos, spot_lights[i].PV, spot_lights[i].shadow_map, spot_lights[i].shadow_near, @@ -141,7 +131,7 @@ void main(){ } else if(light_type == LIGHT_TYPE_DIRECTIONAL){ shadow_attenuation = getShadowSquare( - fs_in.pos, + fs_in.pos, directional_lights[i].PV, directional_lights[i].shadow_map, directional_lights[i].shadow_near, @@ -150,8 +140,8 @@ void main(){ } else if(light_type == LIGHT_TYPE_POINT){ shadow_attenuation = getShadowCube( - fs_in.pos, - lightPos, + fs_in.pos, + lightPos, point_lights[i].shadow_map, point_lights[i].shadow_near, point_lights[i].shadow_far @@ -160,19 +150,19 @@ void main(){ } vec3 radiance = attenuation * shadow_attenuation * intensity; - - float cos_in = max(0.0, dot(lightIn,N)); //n dot l - - vec3 reflect_dir = reflect(-lightIn,N); + + float cos_in = max(0.0, dot(lightIn,N)); //n dot l + + vec3 reflect_dir = reflect(-lightIn,N); float spec = pow(max(dot(lightOut,reflect_dir),0),32); vec3 specular = vec3(spec); - vec3 diffuse = cos_in * baseColor; + vec3 diffuse = cos_in * baseColor; color += (diffuse + specular) * radiance; } } - + color += irradiance_diffuse * baseColor; @@ -181,8 +171,8 @@ void main(){ color = pow(color, vec3(1.0 / gamma)); - fragColor.xyz = color; + fragColor.xyz = color; fragColor.a = baseColor_.a; fragColor.a *= alpha_multiplier; - + } \ No newline at end of file diff --git a/modules/ui/src/shaders/surface/simple.shader b/modules/ui/src/shaders/surface/simple.shader index 1ef3d36a..5d63d03f 100644 --- a/modules/ui/src/shaders/surface/simple.shader +++ b/modules/ui/src/shaders/surface/simple.shader @@ -14,24 +14,13 @@ #pragma VERTEX #include "util/default.vertex" - - #pragma FRAGMENT -layout(location = 0) out vec4 fragColor; +#include "layout/default_fragment_layout.glsl" -in VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; - vec3 tangent; - vec3 bitangent; -} fs_in; - -#pragma property opacity "Opacity" float(1,0,1) +#pragma property opacity "Opacity" float(1,0,1) void main(){ - + fragColor = fs_in.color; fragColor.a *= opacity; } diff --git a/modules/ui/src/shaders/surface/surface.shader b/modules/ui/src/shaders/surface/surface.shader index 6ed830df..ce8a070b 100644 --- a/modules/ui/src/shaders/surface/surface.shader +++ b/modules/ui/src/shaders/surface/surface.shader @@ -14,13 +14,8 @@ #pragma VERTEX - - - #include "util/default.vertex" - - #pragma FRAGMENT #ifdef SHADING_FLAT diff --git a/modules/ui/src/shaders/surface/vertex_attribute.shader b/modules/ui/src/shaders/surface/vertex_attribute.shader index 004c91ba..583f6388 100644 --- a/modules/ui/src/shaders/surface/vertex_attribute.shader +++ b/modules/ui/src/shaders/surface/vertex_attribute.shader @@ -16,16 +16,16 @@ layout (location = 0) in vec3 in_pos; layout (location = 1) in vec4 in_value; -out VARYING { +out VARYING { vec3 pos; vec4 value; } vs_out; void main() -{ +{ //Pos and normal to world space - vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; - vs_out.value = in_value; + vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; + vs_out.value = in_value; //To clip space gl_Position = PV * vec4(vs_out.pos,1.0); @@ -43,10 +43,10 @@ in VARYING { #include "surface/colormap.frag" -#pragma property opacity "Opacity" float(1,0,1) +#pragma property opacity "Opacity" float(1,0,1) void main(){ - + fragColor = colormapped_fragment_color(fs_in.value); fragColor.a *= opacity; } diff --git a/modules/ui/src/shaders/test.shader b/modules/ui/src/shaders/test.shader deleted file mode 100644 index c7be9f3b..00000000 --- a/modules/ui/src/shaders/test.shader +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2020 Adobe. All rights reserved. - * This file is licensed to you 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 REPRESENTATIONS - * OF ANY KIND, either express or implied. See the License for the specific language - * governing permissions and limitations under the License. - */ - -#include "uniforms/common.glsl" - -#pragma VERTEX - -layout (location = 0) in vec3 in_pos; -layout (location = 1) in vec3 in_normal; -layout (location = 2) in vec4 in_tangent; -layout (location = 3) in vec4 in_bitangent; -layout (location = 4) in vec2 in_uv; - -#ifdef ATTRIB_QUANTITY_1D -layout (location = 5) in float in_quantity; -#endif - -#ifdef ATTRIB_QUANTITY_2D -layout (location = 5) in vec2 in_quantity; -#endif - -#ifdef ATTRIB_QUANTITY_3D -layout (location = 5) in vec3 in_quantity; -#endif - -#ifdef ATTRIB_QUANTITY_4D -layout (location = 5) in vec4 in_quantity; -#endif - -#ifdef ATTRIB_QUANTITY_3x3 -layout (location = 5) in vec3 in_quantity_0; -layout (location = 6) in vec3 in_quantity_1; -layout (location = 7) in vec3 in_quantity_2; -#endif - - -void main() -{ - //Pos and normal to world space - vs_out.pos = (M * vec4(in_pos, 1.0)).xyz; - vs_out.normal = (NMat * vec4(in_normal,0.0)).xyz; - vs_out.tangent = (NMat * vec4(in_tangent)).xyz;; - vs_out.bitangent = (NMat * vec4(in_bitangent)).xyz;; - vs_out.uv = in_uv; - - //To clip space - gl_Position = PV * vec4(vs_out.pos,1.0); -} - -out VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; - vec3 tangent; - vec3 bitangent; -} vs_out; - - -#ifdef INDEXING_EDGE -#ifdef PRIMITIVE_TRIANGLE - -#pragma GEOMETRY -#include "util/split_by_edge.geom" - -#endif -#endif - - - -#pragma FRAGMENT - -layout(location = 0) out vec4 fragColor; - -in VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; - vec3 tangent; - vec3 bitangent; -} fs_in; - diff --git a/modules/ui/src/shaders/texture.shader b/modules/ui/src/shaders/texture.shader index 035f0007..0a1a78c0 100644 --- a/modules/ui/src/shaders/texture.shader +++ b/modules/ui/src/shaders/texture.shader @@ -20,7 +20,7 @@ uniform mat4 M = mat4(1.0); void main() { gl_Position = PV * M * vec4(in_pos,1); - vertex_uv = vec3(in_uv,0); + vertex_uv = vec3(in_uv,0); } #pragma FRAGMENT @@ -61,7 +61,7 @@ float linearize_depth(float depth, float near, float far) void main(){ - + vec2 coord = vertex_uv.xy; /*fragColor.xy = coord; fragColor.z = 0; @@ -95,10 +95,10 @@ void main(){ break; } cube_coord = normalize(cube_coord); - t = texture(tex_cube, cube_coord); + t = texture(tex_cube, cube_coord); } else { - t = texture(tex,coord); + t = texture(tex,coord); } @@ -120,17 +120,17 @@ void main(){ if(is_depth){ float d = linearize_depth(t.x, depth_near, depth_far); - fragColor = vec4(vec3(d*bias),opacity); + fragColor = vec4(vec3(d*bias),opacity); return; } else if(singleChannel){ - + if(zeroIsTransparent){ fragColor = vec4(vec3(t.x,0,0),(t.x > 0) ? 0.4f : 0.0f); } else{ - fragColor = vec4(vec3(t.x*bias),opacity); - } + fragColor = vec4(vec3(t.x*bias),opacity); + } } if(showOnlyAlpha) @@ -141,5 +141,5 @@ void main(){ } - + } diff --git a/modules/ui/src/shaders/uniforms/common.glsl b/modules/ui/src/shaders/uniforms/common.glsl index e1a62706..56c5de80 100644 --- a/modules/ui/src/shaders/uniforms/common.glsl +++ b/modules/ui/src/shaders/uniforms/common.glsl @@ -13,6 +13,7 @@ /* Transformation / Projection */ + //Global uniform mat4 PV; uniform vec3 cameraPos; diff --git a/modules/ui/src/shaders/uniforms/lights.glsl b/modules/ui/src/shaders/uniforms/lights.glsl index d1b3bd15..b598774e 100644 --- a/modules/ui/src/shaders/uniforms/lights.glsl +++ b/modules/ui/src/shaders/uniforms/lights.glsl @@ -25,7 +25,7 @@ uniform int directional_lights_count = 0; float get_shadow_at(vec3 surface_pos, SpotLight L){ return getShadowSquare( - surface_pos, + surface_pos, L.PV, L.shadow_map, L.shadow_near, @@ -35,7 +35,7 @@ float get_shadow_at(vec3 surface_pos, SpotLight L){ float get_shadow_at(vec3 surface_pos, DirectionalLight L){ return getShadowSquare( - surface_pos, + surface_pos, L.PV, L.shadow_map, L.shadow_near, @@ -45,7 +45,7 @@ float get_shadow_at(vec3 surface_pos, DirectionalLight L){ float get_shadow_at(vec3 surface_pos, PointLight L){ return getShadowCube( - surface_pos, + surface_pos, L.position, L.shadow_map, L.shadow_near, @@ -55,14 +55,14 @@ float get_shadow_at(vec3 surface_pos, PointLight L){ LightAtSurface get_light_at_surface( - SpotLight spot, + SpotLight spot, vec3 surface_pos, vec3 lightOut, float cos_out ){ - vec3 lightPos = spot.position; - vec3 intensity = spot.intensity; + vec3 lightPos = spot.position; + vec3 intensity = spot.intensity; float lightDistance = length(lightPos - surface_pos); LightAtSurface L; @@ -72,32 +72,32 @@ LightAtSurface get_light_at_surface( L.lightHalf = normalize(L.lightIn + L.lightOut); - float shadow_attenuation = get_shadow_at(surface_pos, spot); - float attenuation = 1.0 / (lightDistance*lightDistance); - + L.shadow_attenuation = get_shadow_at(surface_pos, spot); + L.attenuation = max(1.0 / (lightDistance*lightDistance),1.0); //Apply spot factor - attenuation *= calculate_spot_factor( + L.attenuation *= calculate_spot_factor( lightPos, L.lightIn, spot.direction, spot.cone_angle ); - L.radiance = attenuation * shadow_attenuation * intensity; - - + + L.radiance = L.attenuation * L.shadow_attenuation * intensity; + + return L; } LightAtSurface get_light_at_surface( - DirectionalLight dir, + DirectionalLight dir, vec3 surface_pos, vec3 lightOut, float cos_out ){ - - - vec3 intensity = dir.intensity; - + + + vec3 intensity = dir.intensity; + LightAtSurface L; L.lightOut = lightOut; @@ -105,23 +105,23 @@ LightAtSurface get_light_at_surface( L.lightIn = normalize(-dir.direction); L.lightHalf = normalize(L.lightIn + L.lightOut); - float shadow_attenuation = get_shadow_at(surface_pos, dir); - - L.radiance = shadow_attenuation * intensity; + L.attenuation = 1.0; + L.shadow_attenuation = get_shadow_at(surface_pos, dir); + L.radiance = L.shadow_attenuation * intensity; return L; } LightAtSurface get_light_at_surface( - PointLight point, + PointLight point, vec3 surface_pos, vec3 lightOut, float cos_out ){ - vec3 lightPos = point.position; - vec3 intensity = point.intensity; + vec3 lightPos = point.position; + vec3 intensity = point.intensity; float lightDistance = length(lightPos - surface_pos); LightAtSurface L; @@ -130,11 +130,11 @@ LightAtSurface get_light_at_surface( L.lightIn = normalize(lightPos - surface_pos); L.lightHalf = normalize(L.lightIn + L.lightOut); - float shadow_attenuation = get_shadow_at(surface_pos, point); - float attenuation = 1.0 / (lightDistance*lightDistance); + L.shadow_attenuation = get_shadow_at(surface_pos, point); + L.attenuation = max(1.0 / (lightDistance*lightDistance), 1.0); + + L.radiance = L.attenuation * L.shadow_attenuation * intensity; + - L.radiance = attenuation * shadow_attenuation * intensity; - - return L; } \ No newline at end of file diff --git a/modules/ui/src/shaders/uniforms/materials.glsl b/modules/ui/src/shaders/uniforms/materials.glsl index 53b2d8d0..1f2c4b03 100644 --- a/modules/ui/src/shaders/uniforms/materials.glsl +++ b/modules/ui/src/shaders/uniforms/materials.glsl @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ #include "util/material.glsl" -//uniform MaterialAdobe material; //pragma uniform SHADER_NAME DISPLAY_NAME TYPE(DEFAULT_VALUE) [tag0,tag1] //For Texture, the value is used as a fallback value -> it will generate name_texture_bound and name_default_value @@ -18,26 +17,26 @@ #pragma property material_roughness "Roughness" Texture2D(0.4) #pragma property material_metallic "Metallic" Texture2D(0.1) #pragma property material_normal "Normal" Texture2D [normal] -#pragma property material_opacity "Opacity" float(1,0,1) +#pragma property material_opacity "Opacity" float(1,0,1) //Will read individual material properties, either from texture or from constant value void read_material( - in vec2 uv, - out vec3 baseColor, - out float metallic, + in vec2 uv, + out vec3 baseColor, + out float metallic, out float roughness, out float opacity ){ - vec4 baseColor_a = material_base_color_texture_bound ? - texture(material_base_color, uv) : material_base_color_default_value; - + vec4 baseColor_a = material_base_color_texture_bound ? + texture(material_base_color, uv) : material_base_color_default_value; + baseColor = baseColor_a.xyz; - metallic = material_metallic_texture_bound ? + metallic = material_metallic_texture_bound ? texture(material_metallic, uv).x : material_metallic_default_value; - roughness = material_roughness_texture_bound ? + roughness = material_roughness_texture_bound ? texture(material_roughness, uv).x : material_roughness_default_value; opacity = baseColor_a.a * material_opacity; @@ -64,23 +63,23 @@ vec3 adjust_color( vec3 adjust_normal(vec3 N, vec3 T, vec3 BT, vec2 uv){ N = normalize(N); - + if(material_normal_texture_bound){ T = normalize(T); BT = normalize(BT); - mat3 TBN = mat3(T, BT, N); + mat3 TBN = mat3(T, BT, N); vec3 N_in_TBN = TBN * N; vec3 Ntex = texture(material_normal, uv).xyz * 2.0f - vec3(1.0f); vec3 Ntex_in_world = TBN * Ntex; - N = normalize(Ntex_in_world); + N = normalize(Ntex_in_world); } - + return N; - + } \ No newline at end of file diff --git a/modules/ui/src/shaders/util/brdf_lut.shader b/modules/ui/src/shaders/util/brdf_lut.shader index eaa60be4..5fe80825 100644 --- a/modules/ui/src/shaders/util/brdf_lut.shader +++ b/modules/ui/src/shaders/util/brdf_lut.shader @@ -16,7 +16,7 @@ out vec3 vposition; void main() { gl_Position = vec4(position,1); - vposition = position; + vposition = position; } #pragma FRAGMENT @@ -50,30 +50,30 @@ vec2 IntegrateBRDF(float NdotV, float roughness) float NdotL = max(L.z, 0.0); float NdotH = max(H.z, 0.0); float VdotH = max(dot(V, H), 0.0); - float NdotV = max(dot(N, V), 0.0); + float NdotV = max(dot(N, V), 0.0); if(NdotL > 0.0) - { + { float G = G_Smith_GGX_Schlick(NdotV, NdotL, roughness); - - float G_Vis = (G * VdotH) / (NdotH * NdotV); + + float G_Vis = (G * VdotH) / (NdotH * NdotV); float Fc = pow(1.0 - VdotH, 5.0); A += (1.0 - Fc) * G_Vis; B += Fc * G_Vis; } } - + A /= float(SAMPLE_COUNT); - B /= float(SAMPLE_COUNT); + B /= float(SAMPLE_COUNT); return vec2(A, B); } void main(){ - vec2 coord = (vposition.xy + vec2(1)) * 0.5; //to [0,1] range - fragColor.xy = IntegrateBRDF(coord.x,coord.y); - fragColor.z = 0; + vec2 coord = (vposition.xy + vec2(1)) * 0.5; //to [0,1] range + fragColor.xy = IntegrateBRDF(coord.x,coord.y); + fragColor.z = 0; fragColor.a = 1.0; } diff --git a/modules/ui/src/shaders/util/default.vertex b/modules/ui/src/shaders/util/default.vertex index 8990602c..e2fa7d71 100644 --- a/modules/ui/src/shaders/util/default.vertex +++ b/modules/ui/src/shaders/util/default.vertex @@ -10,36 +10,18 @@ * governing permissions and limitations under the License. */ -layout (location = 0) in vec3 in_pos; -layout (location = 1) in vec3 in_normal; -layout (location = 2) in vec2 in_uv; -layout (location = 3) in vec4 in_tangent; -layout (location = 4) in vec4 in_bitangent; - -#ifdef SKELETAL -layout (location = 5) in vec4 in_bone_ids; -layout (location = 6) in vec4 in_bone_weights; -#endif - -out VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; - vec3 tangent; - vec3 bitangent; -} vs_out; + #include "layout/default_vertex_layout.glsl" #ifdef SKELETAL const int MAX_NUM_BONES = 32; uniform mat4 bones[MAX_NUM_BONES]; #endif -//Default color when attribute is not set + #pragma property in_color "Default Color" Color(1,0,0,1) void main() -{ +{ vec4 model_pos = vec4(in_pos, 1); #ifdef SKELETAL @@ -48,15 +30,15 @@ void main() + bones[int(in_bone_ids[2])] * model_pos * in_bone_weights[2] + bones[int(in_bone_ids[3])] * model_pos * in_bone_weights[3]; #endif - - + + //Pos and normal to world space - vs_out.pos = (M * model_pos).xyz; + vs_out.pos = (M * model_pos).xyz; vs_out.normal = (NMat * vec4(in_normal,0.0)).xyz; vs_out.uv = in_uv; - vs_out.color = in_color; + vs_out.color = in_color; vs_out.tangent = (NMat * vec4(in_tangent)).xyz;; - vs_out.bitangent = (NMat * vec4(in_bitangent)).xyz;; + vs_out.bitangent = (NMat * vec4(in_bitangent)).xyz;; //To clip space gl_Position = PV * vec4(vs_out.pos,1.0); diff --git a/modules/ui/src/shaders/util/light.glsl b/modules/ui/src/shaders/util/light.glsl index 1756c202..e544de77 100644 --- a/modules/ui/src/shaders/util/light.glsl +++ b/modules/ui/src/shaders/util/light.glsl @@ -14,6 +14,7 @@ #define LIGHT_TYPE_DIRECTIONAL 2 uniform float shadow_square_samples = 8; +uniform float shadow_square_radius = 1.0; uniform float shadow_cube_samples = 4.0; struct SpotLight { @@ -54,6 +55,8 @@ struct LightAtSurface { vec3 lightHalf; float cos_out; vec3 radiance; + float attenuation; //Light attenuation (spot/point) + float shadow_attenuation; //Shadow attenuation }; @@ -65,7 +68,7 @@ float calculate_spot_factor(vec3 pos, vec3 lightDir, vec3 spotDir, float angle){ else { float d = (coneAngle / angle); return (1.0 - d*d*d); - } + } return 0.0; } @@ -75,55 +78,60 @@ float recoverShadowDepth(float depth, float near, float far){ } float getShadowSquare(vec3 pos, mat4 PV, sampler2D shadowMap, float near, float far){ - + //Project to light space vec4 lightSpace = PV * (vec4(pos,1)); //Clip space [-1,1] - vec3 coords = lightSpace.xyz / lightSpace.w; + vec3 coords = lightSpace.xyz / lightSpace.w; //NDC space [0,1] - coords = coords * 0.5 + 0.5; + coords = coords * 0.5 + 0.5; //Clamp light space z position coords.z = min(1, coords.z); coords.z = max(0, coords.z); - float currentDepth = coords.z; + float currentDepth = coords.z; - float bias = 0.000; + float bias = 0.000; float shadow = 0.0; - + + //TODO add step size + float samples = shadow_square_samples; vec2 texelSize = 2.0 / textureSize(shadowMap,0); + int sample_count = 0; + for(float x = -texelSize.x; x < texelSize.x; x += texelSize.x / (samples * 0.5)) { for(float y = -texelSize.y; y < texelSize.y; y += texelSize.y / (samples * 0.5)) - { - float closestDepth = texture(shadowMap,coords.xy + vec2(x,y)).x; + { + sample_count += 1; + float closestDepth = texture(shadowMap,coords.xy + vec2(x,y) * shadow_square_radius).x; if(currentDepth - bias > closestDepth) shadow += 1.0; } } - shadow /= (samples * samples); + shadow /= sample_count; return 1.0 - shadow; } float getShadowCube( - vec3 pos, - vec3 lightPos, + vec3 pos, + vec3 lightPos, samplerCube shadowMap, - float near, + float near, float far){ - + vec3 toL = pos - lightPos; float curD = length(toL); - - + + float shadow = 0.0; - float bias = 0.01; + float bias = 0.01; float samples = shadow_cube_samples; float offset = 0.2 * curD / (far - near); @@ -146,12 +154,12 @@ float getShadowCube( float closestDepth = map_value * far; if(curD - bias > closestDepth) - shadow += 1.0; + shadow += 1.0; } } } shadow /= total_samples;; - + return 1.0f - shadow; } diff --git a/modules/ui/src/shaders/util/multisample.glsl b/modules/ui/src/shaders/util/multisample.glsl index 0cb6deac..fd59ce32 100644 --- a/modules/ui/src/shaders/util/multisample.glsl +++ b/modules/ui/src/shaders/util/multisample.glsl @@ -16,13 +16,13 @@ vec4 multisample(sampler2DMS s, vec2 uv) { ivec2 texel = ivec2(floor(textureSize(s) * uv)); - vec4 color = vec4(0.0); + vec4 color = vec4(0.0); for (int i = 0; i < num_samples; i++) { color += texelFetch(s, texel, i); } - + color /= float(num_samples); - + return color; } diff --git a/modules/ui/src/shaders/util/pbr.glsl b/modules/ui/src/shaders/util/pbr.glsl index 247dd1b2..5089a944 100644 --- a/modules/ui/src/shaders/util/pbr.glsl +++ b/modules/ui/src/shaders/util/pbr.glsl @@ -29,7 +29,7 @@ float G_Schlick(float cos_, float k){ return cos_ / (cos_ * (1.0 - k) + k); } -float G_Smith_GGX_Schlick(float cos_in_N, float cos_out_N, float roughness){ +float G_Smith_GGX_Schlick(float cos_in_N, float cos_out_N, float roughness){ float alpha = roughness*roughness; float k = alpha / 2.0; //float alpha = roughness + 1.0; //alternative version @@ -67,7 +67,7 @@ vec3 F_schlick(vec3 f_0, float cos_l_h){ //float cos_l_h = max(0.0,dot(l,h)); cos_l_h = min(cos_l_h,1.0); return f_0 + (vec3(1.0) - f_0) * pow(1.0 - cos_l_h, 5.0); - + } @@ -91,15 +91,15 @@ vec3 get_f0(in vec3 baseColor, in float metallic){ } vec3 pbr_color( - in LightAtSurface L, + in LightAtSurface L, in vec3 f_0, in vec3 N, in float roughness, in float metallic, in vec3 baseColor ){ - - float cos_in = max(0.0, dot(L.lightIn,N)); //n dot l + + float cos_in = max(0.0, dot(L.lightIn,N)); //n dot l float cos_half = max(0.0, dot(L.lightHalf,N)); float cos_diff = max(0.0, dot(L.lightHalf,L.lightIn)); float cos_diff2 = max(0.0, dot(L.lightHalf,L.lightOut)); @@ -112,8 +112,8 @@ vec3 pbr_color( //Geometry Shadowing/Masking function float G = G_Smith_GGX_Schlick(cos_in, L.cos_out, roughness); - - //Denominator + + //Denominator float denom = max(4 * cos_in * L.cos_out,0.00001); //Final specular direct light component diff --git a/modules/ui/src/shaders/util/pbr_shading.glsl b/modules/ui/src/shaders/util/pbr_shading.glsl index fbfb955f..a085cbc1 100644 --- a/modules/ui/src/shaders/util/pbr_shading.glsl +++ b/modules/ui/src/shaders/util/pbr_shading.glsl @@ -24,17 +24,17 @@ vec3 pbr_ibl_color(vec3 N, float cos_out, vec3 lightOut, vec3 f_0, float metalli vec3 specularDir = (2.0 * cos_out * N - lightOut); //Sample diffuse irradiance - vec3 irradiance_diffuse = has_ibl ? + vec3 irradiance_diffuse = has_ibl ? texture(ibl_diffuse, ibl_dir).xyz : ambient_default; //Sample specular irradiance - vec3 irradiance_specular = has_ibl ? + vec3 irradiance_specular = has_ibl ? textureLod(ibl_specular, specularDir, roughness * ibl_specular_levels).rgb : ambient_default; - - //Diffuse irradiance - vec3 F = F_schlick(f_0, cos_out); + + //Diffuse irradiance + vec3 F = F_schlick(f_0, cos_out); vec3 diffuse_fraction = mix(vec3(1.0) - F, vec3(0.0), metallic); vec3 diffuse = diffuse_fraction * baseColor * irradiance_diffuse; @@ -55,7 +55,7 @@ vec3 pbr_ibl_color(vec3 N, float cos_out, vec3 lightOut, vec3 f_0, float metalli vec4 pbr( vec3 in_pos, vec3 in_normal, vec2 in_uv, vec4 in_color, vec3 in_tangent, vec3 in_bitangent ){ - + vec3 baseColor; float metallic, roughness, opacity; read_material(in_uv, baseColor, metallic, roughness, opacity); @@ -63,7 +63,7 @@ vec4 pbr( baseColor = adjust_color(has_color_attrib, in_color, uniform_color, baseColor); vec3 N = adjust_normal(in_normal, in_tangent, in_bitangent, in_uv); - + vec3 f_0 = get_f0(baseColor, metallic); vec3 color = vec3(0); @@ -78,7 +78,7 @@ vec4 pbr( ); color += pbr_color(L, f_0, N, roughness, metallic, baseColor); - } + } for(int i = 0; i < directional_lights_count; i++){ @@ -94,17 +94,17 @@ vec4 pbr( LightAtSurface L = get_light_at_surface( point_lights[i], in_pos, lightOut, cos_out ); - + color += pbr_color(L, f_0, N, roughness, metallic, baseColor); } - + color += pbr_ibl_color(N, cos_out, lightOut, f_0, metallic, roughness, baseColor); - + color = gamma_correction(color); - - + + return vec4(color, opacity); } \ No newline at end of file diff --git a/modules/ui/src/shaders/util/phong.glsl b/modules/ui/src/shaders/util/phong.glsl index 086c9282..f2ddc112 100644 --- a/modules/ui/src/shaders/util/phong.glsl +++ b/modules/ui/src/shaders/util/phong.glsl @@ -15,32 +15,32 @@ vec3 phong( - const in vec3 lightDir, - const in vec3 viewDir, - const in vec3 normal + const in vec3 lightDir, + const in vec3 viewDir, + const in vec3 normal ){ - + float diffuseMagnitude = max(dot(normal,lightDir),0.0); - float ambientMagnitude = 1; - + float ambientMagnitude = 1; + vec3 mat_ambient = material.ambient.has_texture ? texture(material.ambient.texture, fs_in.uv).xyz : material.ambient.value.xyz; vec3 mat_diffuse = material.diffuse.has_texture ? texture(material.diffuse.texture, fs_in.uv).xyz : material.diffuse.value.xyz; vec3 mat_specular = material.specular.has_texture ? texture(material.specular.texture, fs_in.uv).xyz : material.specular.value.xyz; mat_ambient = vec3(0.15); - //Ambient + //Ambient vec3 ambient = mat_ambient * ambientMagnitude; //Diffuse - vec3 diffuse = clamp(mat_diffuse * diffuseMagnitude,0,1); + vec3 diffuse = clamp(mat_diffuse * diffuseMagnitude,0,1); //Specular - vec3 specular = vec3(0); - if (dot(normal, lightDir) >= 0.0){ - float specularMagnitude = + vec3 specular = vec3(0); + if (dot(normal, lightDir) >= 0.0){ + float specularMagnitude = pow( - max(0.0, dot(reflect(-lightDir, normal),viewDir)), + max(0.0, dot(reflect(-lightDir, normal),viewDir)), material.shininess ); @@ -50,8 +50,8 @@ vec3 phong( return ambient + (diffuse + specular); } -float lightOmniAttenuation(vec3 pos, vec3 lightPos, float intensity, float attenuation){ - float d = length(lightPos - pos); +float lightOmniAttenuation(vec3 pos, vec3 lightPos, float intensity, float attenuation){ + float d = length(lightPos - pos); //return 1.0 / (1.0 + ) return intensity * clamp(1.0 - d / attenuation, 0.0,1.0); } @@ -64,6 +64,6 @@ float lightSpotAttenuation(vec3 pos, vec3 lightDir, vec3 spotDir, float angle, else { float d = (coneAngle / angle); return (1.0 - d*d*d) * intensity; - } + } } diff --git a/modules/ui/src/shaders/util/skeletal.vertex b/modules/ui/src/shaders/util/skeletal.vertex deleted file mode 100644 index ce4e846a..00000000 --- a/modules/ui/src/shaders/util/skeletal.vertex +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2020 Adobe. All rights reserved. - * This file is licensed to you 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 REPRESENTATIONS - * OF ANY KIND, either express or implied. See the License for the specific language - * governing permissions and limitations under the License. - */ - -layout (location = 0) in vec3 in_pos; -layout (location = 1) in vec3 in_normal; -layout (location = 2) in vec2 in_uv; -layout (location = 3) in vec4 in_bone_ids; -layout (location = 3) in vec4 in_bone_weights; -layout (location = 4) in vec4 in_tangent; -layout (location = 5) in vec4 in_bitangent; - -out VARYING { - vec3 pos; - vec3 normal; - vec2 uv; - vec4 color; - vec3 tangent; - vec3 bitangent; -} vs_out; - -const int MAX_NUM_BONES = 32 -uniform mat4 bones[MAX_NUM_BONES]; - -//Default color when attribute is not set -#pragma property uniform_color "Default Color" Color(1,0,0,1) - -void main() -{ - - vec4 model_pos = vec4(in_pos, 1); - vec4 skinned_pos = bones[in_bone_ids[0]] * model_pos * in_bone_weights[0] - + bones[in_bone_ids[1]] * model_pos * in_bone_weights[1] - + bones[in_bone_ids[2]] * model_pos * in_bone_weights[2] - + bones[in_bone_ids[3]] * model_pos * in_bone_weights[3]; - - - //Pos and normal to world space - vs_out.pos = (M * skinned_pos).xyz; - vs_out.normal = (NMat * vec4(in_normal,0.0)).xyz; - vs_out.uv = in_uv; - vs_out.color = uniform_color; - vs_out.tangent = (NMat * vec4(in_tangent)).xyz;; - vs_out.bitangent = (NMat * vec4(in_bitangent)).xyz;; - - //To clip space - gl_Position = PV * vec4(vs_out.pos,1.0); -} \ No newline at end of file diff --git a/modules/ui/src/shaders/util/split_by_edge.geom b/modules/ui/src/shaders/util/split_by_edge.geom index 078cab41..2cb0c188 100644 --- a/modules/ui/src/shaders/util/split_by_edge.geom +++ b/modules/ui/src/shaders/util/split_by_edge.geom @@ -69,5 +69,5 @@ void main(){ emit(0,2); emit(center_pos,center_color,center_normal,center_uv,center_tangent, center_bitangent); EndPrimitive(); - + } diff --git a/modules/ui/src/systems/render_background.cpp b/modules/ui/src/systems/render_background.cpp index f1f42e72..c098545b 100644 --- a/modules/ui/src/systems/render_background.cpp +++ b/modules/ui/src/systems/render_background.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include @@ -40,11 +42,10 @@ void render_background(Registry& r) auto& shaders = r.ctx_or_set>(); const auto& camera = viewport.computed_camera; - auto shader_res = get_or_load_shader(shaders, "skybox.shader", true); + auto shader_res = get_shader(r, DefaultShaders::Skybox); if (!shader_res) return; auto shader = *shader_res; - auto& skybox_data = r.ctx_or_set(); if (!skybox_data.vertex_data) skybox_data.vertex_data = generate_cube_vertex_data(); @@ -54,14 +55,13 @@ void render_background(Registry& r) gl(glFrontFace, GL_CW); - - r.view().each([&](const Entity& entity, IBL& ibl) { + r.view().each([&](const Entity& entity, IBL& ibl) { if (!ui::is_visible_in(r, entity, viewport.visible_layers, viewport.hidden_layers)) return; if (!ibl.show_skybox) return; - + // Do not show in selection if (viewport.visible_layers.test(DefaultLayers::Selection)) return; diff --git a/modules/ui/src/systems/render_geometry.cpp b/modules/ui/src/systems/render_geometry.cpp index 97cbab75..d619ea0c 100644 --- a/modules/ui/src/systems/render_geometry.cpp +++ b/modules/ui/src/systems/render_geometry.cpp @@ -43,22 +43,6 @@ void setup_vertex_data(Registry& r) }); - // Setup default mesh rendering attribute arrays - r.view().each( - [&](Entity /*e*/, VertexData& glvd, MeshRender& render, const MeshGeometry& geom_entity) { - if (!r.valid(geom_entity.entity)) return; - if (!r.has(geom_entity.entity)) return; - if (!render.material) return; - - auto shader_handle = ui::get_shader(r, render.material->shader_id()); - if (!shader_handle) return; - - auto& glmesh = r.get(geom_entity.entity); - auto& shader = *shader_handle; - - update_vertex_data(glmesh, shader, glvd, render.indexing, geom_entity.submesh_index); - }); - // Setup attribute renders r.view().each( [&](Entity /*e*/, VertexData& glvd, MeshRender& render, AttributeRender& ar, MeshGeometry& mg) { @@ -139,6 +123,22 @@ void setup_vertex_data(Registry& r) data.rows(), element_size); }); + + // Setup default mesh rendering attribute arrays + r.view().each( + [&](Entity e, VertexData& glvd, MeshRender& render, const MeshGeometry& geom_entity) { + if (!r.valid(geom_entity.entity)) return; + if (!r.has(geom_entity.entity)) return; + if (!render.material) return; + + auto shader_handle = ui::get_shader(r, render.material->shader_id()); + if (!shader_handle) return; + + auto& glmesh = r.get(geom_entity.entity); + auto& shader = *shader_handle; + + update_vertex_data(glmesh, shader, glvd, render.indexing, geom_entity.submesh_index); + }); } @@ -393,7 +393,7 @@ void sort_gl_render_queue(Registry& r) [](const GLRenderQueueItem& a, const GLRenderQueueItem& b) { const float a_cam_dist_inverse = -a.camera_distance; const float b_cam_dist_inverse = -b.camera_distance; - + return std::tie(a.pass, a.shader, a_cam_dist_inverse, a.material, a.glvd) < std::tie(b.pass, b.shader, b_cam_dist_inverse, b.material, b.glvd); }); @@ -591,6 +591,10 @@ void render_gl_render_queue(Registry& r) gl(glPointSize, float(material.int_values.at(RasterizerOptions::PointSize))); } + if (material.int_values.count(RasterizerOptions::DepthFunc)) { + gl(glDepthFunc, material.int_values.at(RasterizerOptions::DepthFunc)); + } + for (auto& it : material.float_values) { shader.uniform(it.first) = it.second; @@ -625,6 +629,7 @@ void render_gl_render_queue(Registry& r) for (auto& it : material.texture_values) { const auto& tex_val = it.second; + // Find the texture unit auto it_samplers = shader.sampler_indices().find(it.first); @@ -633,6 +638,7 @@ void render_gl_render_queue(Registry& r) continue; } + const auto& name = shader.name(it.first); // If texture is set and sampler exists in shader @@ -642,21 +648,29 @@ void render_gl_render_queue(Registry& r) shader.uniform(name + "_texture_bound") = true; } else { shader.uniform(name + "_texture_bound") = false; - auto prop = shader.texture_properties().at(it.first); - - // Set default color if there's no texture - if (prop.value_dimension == 1) { - shader.uniform(name + "_default_value") = it.second.color.x(); - } else if (prop.value_dimension == 2) { - shader.uniform(name + "_default_value") = - Eigen::Vector2f(it.second.color.x(), it.second.color.y()); - } else if (prop.value_dimension == 3) { - shader.uniform(name + "_default_value") = Eigen::Vector3f( - it.second.color.x(), - it.second.color.y(), - it.second.color.z()); - } else if (prop.value_dimension == 4) { - shader.uniform(name + "_default_value") = it.second.color; + const auto it_prop = shader.texture_properties().find(it.first); + + if (it_prop != shader.texture_properties().end()) { + const auto& prop = it_prop->second; + // Set default color if there's no texture + if (prop.value_dimension == 1) { + shader.uniform(name + "_default_value") = it.second.color.x(); + } else if (prop.value_dimension == 2) { + shader.uniform(name + "_default_value") = + Eigen::Vector2f(it.second.color.x(), it.second.color.y()); + } else if (prop.value_dimension == 3) { + shader.uniform(name + "_default_value") = Eigen::Vector3f( + it.second.color.x(), + it.second.color.y(), + it.second.color.z()); + } else if (prop.value_dimension == 4) { + shader.uniform(name + "_default_value") = it.second.color; + } + } else { + lagrange::logger().warn( + "There is no texture sampler for {} in the shader (Shader ID {})", + name, + material.shader_id()); } } } @@ -761,6 +775,13 @@ void render_post_process(Registry& r) attrib_bindings[mat.get()] = VertexData{}; + for (auto& tex : mat->texture_values) { + if (tex.second.texture && tex.second.texture->get_params().type == GL_TEXTURE_2D) { + tex.second.texture->bind(); + glGenerateMipmap(GL_TEXTURE_2D); + } + } + update_vertex_data( ppquad, shader, diff --git a/modules/ui/src/systems/render_shadowmaps.cpp b/modules/ui/src/systems/render_shadowmaps.cpp index 704ee6e1..e558e9b3 100644 --- a/modules/ui/src/systems/render_shadowmaps.cpp +++ b/modules/ui/src/systems/render_shadowmaps.cpp @@ -12,7 +12,6 @@ #include -#include #include #include #include @@ -20,10 +19,11 @@ #include #include #include +#include +#include #include #include #include -#include #include @@ -141,55 +141,46 @@ void render_shadowmaps(Registry& r) Eigen::Matrix4f PV; const Eigen::AlignedBox3f shadow_box = get_scene_bounds(r).global; - { + if (!shadow_box.isEmpty()) { const auto plane = utils::render::compute_perpendicular_plane(dir); - if (shadow_box.isEmpty()) { - /*const float range = (shadowmap.far_plane - shadowmap.near_plane) / 2.0f; - const auto P = ortho(-range, range, -range, range, shadowmap.near_plane, - shadowmap.far_plane); pos = (camera.get_lookat() - dir * shadowmap.far_plane - * 0.5f); const auto V = look_at(pos, pos + dir, plane.first); PV = (P * - V);*/ - // lagrange::logger().debug("Neeed shadowbox"); - - } else { - // Transformation to light space - Eigen::Matrix3f dir_proj; - dir_proj.col(0) = plane.first; - dir_proj.col(1) = plane.second; - dir_proj.col(2) = dir; - - // Transform shadow box to light space and find its AABB - AABB proj_bbox; - for (int i = 0; i < 8; ++i) { - auto p = shadow_box.corner(static_cast(i)); - proj_bbox.extend(dir_proj * p); - } - - // Shadow map render position in light space - Eigen::Vector3f cam_proj_center = Eigen::Vector3f( - proj_bbox.center().x(), - proj_bbox.center().y(), - proj_bbox.min().z()); - - // Shadow map render position in world space - Eigen::Vector3f cam_reproj_center = dir_proj.inverse() * cam_proj_center; - - // Projection - Eigen::Vector3f range = proj_bbox.diagonal(); - auto P = ortho( - -range.x() * 0.5f, - range.x() * 0.5f, - -range.y() * 0.5f, - range.y() * 0.5f, - 0, - range.z()); - - // View - auto V = look_at(cam_reproj_center, cam_reproj_center + dir, plane.second); - - PV = (P * V); - pos = cam_reproj_center; + // Transformation to light space + Eigen::Matrix3f dir_proj; + dir_proj.col(0) = plane.first; + dir_proj.col(1) = plane.second; + dir_proj.col(2) = dir; + + // Transform shadow box to light space and find its AABB + AABB proj_bbox; + for (int i = 0; i < 8; ++i) { + auto p = shadow_box.corner(static_cast(i)); + proj_bbox.extend(dir_proj * p); } + + // Shadow map render position in light space + Eigen::Vector3f cam_proj_center = Eigen::Vector3f( + proj_bbox.center().x(), + proj_bbox.center().y(), + proj_bbox.min().z()); + + // Shadow map render position in world space + Eigen::Vector3f cam_reproj_center = dir_proj.inverse() * cam_proj_center; + + // Projection + Eigen::Vector3f range = proj_bbox.diagonal(); + auto P = ortho( + -range.x() * 0.5f, + range.x() * 0.5f, + -range.y() * 0.5f, + range.y() * 0.5f, + 0, + range.z()); + + // View + auto V = look_at(cam_reproj_center, cam_reproj_center + dir, plane.second); + + PV = (P * V); + pos = cam_reproj_center; + shadowmap.PV = PV; } viewport.material_override->set_mat4("PV", PV); @@ -221,7 +212,6 @@ void render_shadowmaps(Registry& r) viewport.material_override->set_int(RasterizerOptions::ReadBuffer, GL_NONE); viewport.material_override->set_int(RasterizerOptions::CullFace, GL_FRONT); viewport.material_override->set_int(RasterizerOptions::CullFaceEnabled, GL_TRUE); - }); } diff --git a/modules/ui/src/systems/render_viewports.cpp b/modules/ui/src/systems/render_viewports.cpp index 7eeccc24..d08889d1 100644 --- a/modules/ui/src/systems/render_viewports.cpp +++ b/modules/ui/src/systems/render_viewports.cpp @@ -100,12 +100,17 @@ void render_viewport(Registry& r, Entity e) 0, int(viewport.computed_camera.get_window_width()), int(viewport.computed_camera.get_window_height())); - gl(glClearColor, - viewport.background.x(), - viewport.background.y(), - viewport.background.z(), - viewport.background.w()); - gl(glClear, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + + if (viewport.clear_bits != 0) { + gl(glClearColor, + viewport.background.x(), + viewport.background.y(), + viewport.background.z(), + viewport.background.w()); + + gl(glClear, viewport.clear_bits); + } } // TODO as geometry (skybox)! diff --git a/modules/ui/src/systems/update_gizmo.cpp b/modules/ui/src/systems/update_gizmo.cpp index f36a97f6..3f1b0cc3 100644 --- a/modules/ui/src/systems/update_gizmo.cpp +++ b/modules/ui/src/systems/update_gizmo.cpp @@ -108,7 +108,7 @@ void gizmo_system( auto bounds = registry.try_get(e); if (bounds) { - avg_pos += bounds->global.center(); + avg_pos += bounds->bvh_node.center(); } else { avg_pos += t.global.matrix().col(3).hnormalized(); } @@ -145,7 +145,7 @@ void gizmo_system( AABB bbox; view.each([&](Entity e, const Selected& /*sel*/, Transform& /*t*/) { auto bb = registry.try_get(e); - if (bb) bbox.extend(bb->global); + if (bb) bbox.extend(bb->bvh_node); }); diff --git a/modules/ui/src/systems/update_mesh_buffers.cpp b/modules/ui/src/systems/update_mesh_buffers.cpp index d9c1a562..9ea2d192 100644 --- a/modules/ui/src/systems/update_mesh_buffers.cpp +++ b/modules/ui/src/systems/update_mesh_buffers.cpp @@ -10,11 +10,11 @@ * governing permissions and limitations under the License. */ +#include #include #include #include #include -#include #include #include @@ -57,11 +57,11 @@ void update_mesh_buffers_system(Registry& r) const auto& mr = view.get(e); const auto& mesh_entity = view.get(e).entity; - LA_ASSERT(r.valid(mesh_entity), "Invalid mesh entity " + ui::get_name(r,e)); + LA_ASSERT(r.valid(mesh_entity), "Invalid mesh entity " + ui::get_name(r, e)); auto& md = r.get(mesh_entity); - - //No material set + + // No material set if (!mr.material) continue; auto shader_ref = ui::get_shader(r, mr.material->shader_id()); @@ -94,7 +94,14 @@ void update_mesh_buffers_system(Registry& r) for (auto e : view) { auto& meshdata = view.get(e); - if (!r.has(e)) r.emplace(e); + if (!r.has(e)) { + r.emplace(e); + } else { + // Skip non-dirty mesh entities that already have glmesh + if (!r.has(e)) { + continue; + } + } auto& glmesh = r.get(e); @@ -106,7 +113,7 @@ void update_mesh_buffers_system(Registry& r) *glmesh.attribute_buffers[DefaultShaderAtrribNames::Position]); } - if (!glmesh.get_index_buffer(DefaultShaderIndicesNames::TriangleIndices)) { + if (!glmesh.get_index_buffer(DefaultShaderIndicesNames::TriangleIndices)) { glmesh.index_buffers[DefaultShaderIndicesNames::TriangleIndices] = std::make_shared(GL_ELEMENT_ARRAY_BUFFER); upload_mesh_triangles( @@ -146,12 +153,11 @@ void update_mesh_buffers_system(Registry& r) } } - // Material id + // Material id if (has_mesh_facet_attribute(meshdata, "material_id") && glmesh.submesh_indices.size() == 0) { glmesh.submesh_indices = upload_submesh_indices(meshdata, "material_id"); } - } } } diff --git a/modules/ui/src/systems/update_mesh_hovered.cpp b/modules/ui/src/systems/update_mesh_hovered.cpp index 6e4811f6..e995dbf7 100644 --- a/modules/ui/src/systems/update_mesh_hovered.cpp +++ b/modules/ui/src/systems/update_mesh_hovered.cpp @@ -77,7 +77,7 @@ void update_mesh_hovered_GL(Registry& r, const SelectionContext & sel_ctx) buffer[pixel_size * i + 2]); if (!is_id_background(id) && r.valid(Entity(id))) { - r.emplace(Entity(id)); + r.emplace_or_replace(Entity(id)); } } diff --git a/modules/ui/src/systems/update_scene_bounds.cpp b/modules/ui/src/systems/update_scene_bounds.cpp index 9ff1c8eb..bc07c562 100644 --- a/modules/ui/src/systems/update_scene_bounds.cpp +++ b/modules/ui/src/systems/update_scene_bounds.cpp @@ -40,7 +40,13 @@ void update_scene_bounds_system(Registry& registry) auto view = registry.view(); for (auto e : view) { // Skip inner - if (view.get(e).num_children != 0) continue; + if (view.get(e).num_children != 0) { + //Clear previous bvh values + if (registry.has(e)) { + registry.get(e).bvh_node = AABB{}; + } + continue; + } // Skip non bounded entities if (!registry.has(e)) continue; diff --git a/modules/ui/src/types/Camera.cpp b/modules/ui/src/types/Camera.cpp index fad44df1..29c7fa4d 100644 --- a/modules/ui/src/types/Camera.cpp +++ b/modules/ui/src/types/Camera.cpp @@ -493,6 +493,9 @@ Camera Camera::transformed(const ViewportTransform& vt) const cam.get_window_width() * vt.scale.x(), cam.get_window_height() * vt.scale.y()); } else { + cam.set_aspect_ratio( + cam.get_window_width() * vt.scale.x(), + cam.get_window_height() * vt.scale.y()); const Eigen::Translation3f half = Eigen::Translation3f(Eigen::Vector3f(1.0f, 1.0f, 0)); const Eigen::Vector2f translate_flip = Eigen::Vector2f(vt.translate.x(), 1.0f - vt.scale.y() - vt.translate.y()); const Eigen::Vector2f offset = translate_flip * 2.0f - Eigen::Vector2f::Ones(); diff --git a/modules/ui/src/types/MDL.cpp b/modules/ui/src/types/MDL.cpp index a37c99d2..2385d4a8 100644 --- a/modules/ui/src/types/MDL.cpp +++ b/modules/ui/src/types/MDL.cpp @@ -77,19 +77,19 @@ struct MDLImpl auto context = handle_of(state.factory->create_execution_context()); { const std::string mtl = -#include "adobe_mdl/mtl.mdl" +#include "../adobe_mdl/mtl.mdl" ; const std::string util = -#include "adobe_mdl/util.mdl" +#include "../adobe_mdl/util.mdl" ; const std::string annotations = -#include "adobe_mdl/annotations.mdl" +#include "../adobe_mdl/annotations.mdl" ; const std::string convert = -#include "adobe_mdl/convert.mdl" +#include "../adobe_mdl/convert.mdl" ; mi::Sint32 err_code; diff --git a/modules/ui/src/types/Material.cpp b/modules/ui/src/types/Material.cpp index be2cc430..5ea1ee15 100644 --- a/modules/ui/src/types/Material.cpp +++ b/modules/ui/src/types/Material.cpp @@ -15,10 +15,15 @@ namespace lagrange { namespace ui { -Material::Material(Registry& r, StringID shader_id) +Material::Material(Registry& r, StringID shader_id, const ShaderDefines& shader_defines) : m_shader_id(shader_id) { - auto shader = ui::get_shader(r, shader_id); + if (shader_defines.size() > 0) { + m_shader_id = register_shader_variant(r, shader_id, shader_defines); + } + + auto shader = ui::get_shader(r, m_shader_id); + if (!shader) { return; } @@ -109,8 +114,12 @@ void Material::set_color(const std::string& name, Color color) void Material::set_texture(StringID id, std::shared_ptr texture) { auto it_tex = texture_values.find(id); - if (it_tex == texture_values.end()) return; - it_tex->second.texture = std::move(texture); + if (it_tex == texture_values.end()) { + //Create new even though it's not exposed + texture_values[id].texture = std::move(texture); + } else { + it_tex->second.texture = std::move(texture); + } } void Material::set_texture(const std::string& name, std::shared_ptr texture) diff --git a/modules/ui/src/types/ShaderLoader.cpp b/modules/ui/src/types/ShaderLoader.cpp index d1ef2cef..95f3b94b 100644 --- a/modules/ui/src/types/ShaderLoader.cpp +++ b/modules/ui/src/types/ShaderLoader.cpp @@ -22,11 +22,20 @@ namespace lagrange { namespace ui { +entt::id_type hash_shader_definition(const ShaderDefinition& def) +{ + std::string hash_str = def.path + (def.path_type == ShaderLoader::PathType::REAL ? "0" : "1"); + for (const auto& d : def.defines) { + hash_str += d.first + d.second; + } + return entt::hashed_string(hash_str.c_str()); +} + std::shared_ptr ShaderLoader::load( const std::string& generic_path, PathType pathtype, - ShaderDefines defines /*= {}*/) const + const ShaderDefines & defines /*= {}*/) const { try { if (pathtype == PathType::REAL) { @@ -48,12 +57,36 @@ std::shared_ptr ShaderLoader::load( return nullptr; } +entt::id_type +register_shader_variant(Registry& r, entt::id_type id, const ShaderDefines& shader_defines) +{ + + if (shader_defines.size() == 0) return id; + + auto& registered = r.ctx_or_set(); + auto def_it = registered.find(id); + if (def_it == registered.end()) return entt::null; + + // Add to existing defines + ShaderDefinition new_sdef = def_it->second; + for (auto& define : shader_defines) { + new_sdef.defines.push_back(define); + } + + // Caculate alternative cache key + auto alternate_id = hash_shader_definition(new_sdef); + return register_shader_as(r, alternate_id, new_sdef); + +} + ShaderResource get_shader(Registry& registry, entt::id_type id) { auto& cache = registry.ctx_or_set(); + if (cache.contains(id)) return cache.handle(id); + //Shader not registered, return empty handle auto& registered = registry.ctx_or_set(); auto def_it = registered.find(id); if (def_it == registered.end()) return ShaderResource(); @@ -75,6 +108,24 @@ ShaderCache& get_shader_cache(Registry& registry) return registry.ctx_or_set(); } +bool add_file_to_shader_virtual_fs( + const std::string& virtual_path, + const std::string& contents, + bool overwrite) +{ +#ifndef DEFAULT_SHADERS_USE_REAL_PATH + if (!overwrite) { + auto res = VIRTUAL_SHADERS.insert({virtual_path, contents}); + return res.second; + } else { + VIRTUAL_SHADERS[virtual_path] = contents; + return true; + } +#else + return false; +#endif +} + entt::id_type register_shader_as(Registry& registry, entt::id_type id, const ShaderDefinition& def) { auto& registered = registry.ctx_or_set(); @@ -82,14 +133,10 @@ entt::id_type register_shader_as(Registry& registry, entt::id_type id, const Sha return id; } -entt::id_type register_shader(Registry& registry, const ShaderDefinition& def) -{ - std::string hash_str = def.path + (def.path_type == ShaderLoader::PathType::REAL ? "0" : "1"); - for (const auto& d : def.defines) { - hash_str += d.first + d.second; - } - return register_shader_as(registry, entt::hashed_string(hash_str.c_str()), def); +entt::id_type register_shader(Registry& registry, const ShaderDefinition& def) +{ + return register_shader_as(registry, hash_shader_definition(def), def); } entt::id_type @@ -101,5 +148,6 @@ register_shader(Registry& registry, const std::string& path, const std::string& return register_shader(registry, def); } + } // namespace ui } // namespace lagrange \ No newline at end of file diff --git a/modules/ui/src/types/Systems.cpp b/modules/ui/src/types/Systems.cpp index f70356f6..afaff3d7 100644 --- a/modules/ui/src/types/Systems.cpp +++ b/modules/ui/src/types/Systems.cpp @@ -86,6 +86,19 @@ bool Systems::succeeds(StringID system, StringID after) return true; } +bool Systems::remove(StringID id) { + + auto it = std::find_if(m_items.begin(), m_items.end(), [&](const SystemItem& item) { + return item.id == id; + }); + + if (it == m_items.end()) return false; + + m_items.erase(it); + + return true; +} + StringID Systems::new_id() { return m_id_counter++; diff --git a/modules/ui/src/types/Texture.cpp b/modules/ui/src/types/Texture.cpp index f53b7582..23341e7e 100644 --- a/modules/ui/src/types/Texture.cpp +++ b/modules/ui/src/types/Texture.cpp @@ -207,6 +207,7 @@ bool Texture::save_to( auto comp_cnt = [](GLenum gl_type) { if (gl_type == GL_RGB) return 3u; if (gl_type == GL_RED) return 1u; + if (gl_type == GL_DEPTH_COMPONENT) return 1u; if (gl_type == GL_RGBA) return 4u; return 0u; }; diff --git a/modules/ui/src/utils/io.cpp b/modules/ui/src/utils/io.cpp index fbf379be..904b4f5d 100644 --- a/modules/ui/src/utils/io.cpp +++ b/modules/ui/src/utils/io.cpp @@ -221,13 +221,20 @@ Entity detail::load_scene_impl( { aiColor4D color(0.f, 0.f, 0.f, 1.f); if (amat->Get(AI_MATKEY_COLOR_DIFFUSE, color) == aiReturn_SUCCESS) { - mat->set_color(PBRMaterial::BaseColor, reinterpret_cast(color)); + ui::Color c; + c.r() = color.r; + c.g() = color.g; + c.b() = color.b; + c.a() = color.a; + mat->set_color(PBRMaterial::BaseColor, c); + + lagrange::logger().trace("Material index {} name {} base color {}",i, matname.C_Str(), c); } } // Base color texture { aiString dpath; - if (amat->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), dpath) == aiReturn_SUCCESS && + if (amat->Get(AI_MATKEY_TEXTURE_DIFFUSE(0), dpath) == aiReturn_SUCCESS && dpath.length > 0) { mat->set_texture(PBRMaterial::BaseColor, load_texture(resolve_texture_path(dpath))); } @@ -275,10 +282,10 @@ Entity detail::load_scene_impl( } } -#ifdef LAGRANGE_UI_LIST_ASSIMP_MAT_PROPERTIES - for (auto i = 0; i < amat->mNumProperties; i++) { + + for (size_t i = 0; i < amat->mNumProperties; i++) { auto prop = amat->mProperties[i]; - lagrange::logger().info( + lagrange::logger().trace( "key: {} length: {}, type: {:02X}, index: {}", prop->mKey.C_Str(), prop->mDataLength, @@ -286,7 +293,6 @@ Entity detail::load_scene_impl( prop->mIndex); } -#endif materials.push_back(mat); } @@ -317,8 +323,17 @@ Entity detail::load_scene_impl( meshes[node->mMeshes[mesh_index]], DefaultShaders::PBR); // todo PBRSkeletal if has bones + lagrange::logger().trace( + "Mesh index {}, name parent {}, material index {}", + mesh_index, + node->mName.C_Str(), + scene->mMeshes[node->mMeshes[mesh_index]]->mMaterialIndex); + // Material - ui::set_material(r, mesh_e, materials[scene->mMeshes[mesh_index]->mMaterialIndex]); + ui::set_material( + r, + mesh_e, + materials[scene->mMeshes[node->mMeshes[mesh_index]]->mMaterialIndex]); ui::set_parent(r, mesh_e, e); } diff --git a/modules/ui/src/utils/layer.cpp b/modules/ui/src/utils/layer.cpp index c915b8b1..0a3f0309 100644 --- a/modules/ui/src/utils/layer.cpp +++ b/modules/ui/src/utils/layer.cpp @@ -99,7 +99,7 @@ bool is_visible_in( LayerIndex get_next_available_layer_index(Registry& r) { const auto& names = r.ctx(); for (size_t i = 0; i < names.size(); i++) { - if (names[i].length() == 0) return i; + if (names[i].length() == 0) return LayerIndex(i); } return 0; } diff --git a/modules/ui/src/utils/render.cpp b/modules/ui/src/utils/render.cpp index 3794e5dc..e7ea5f3b 100644 --- a/modules/ui/src/utils/render.cpp +++ b/modules/ui/src/utils/render.cpp @@ -343,7 +343,7 @@ void update_vao(VertexData& vd) auto& buffer = vd.attribute_buffers[i]; if (!buffer) { - GL(glDisableVertexAttribArray(i)); + GL(glDisableVertexAttribArray(GLuint(i))); continue; } @@ -358,8 +358,8 @@ void update_vao(VertexData& vd) "Attribute must be of dimension 1,2,3, or 4"); GL(glBindBuffer(vbo.target, vbo.id)); - GL(glVertexAttribPointer(i, vd.attribute_dimensions[i], vbo.glType, GL_FALSE, 0, 0)); - GL(glEnableVertexAttribArray(i)); + GL(glVertexAttribPointer(GLuint(i), vd.attribute_dimensions[i], vbo.glType, GL_FALSE, 0, 0)); + GL(glEnableVertexAttribArray(GLuint(i))); #ifdef LAGRANGE_TRACE_BUFFERS lagrange::logger().trace(