├── .clang-format ├── .gitignore ├── .gitmodules ├── AUTHORS.txt ├── CMake ├── .gitignore ├── CPackConfig.cmake ├── CPackReadme.txt ├── FindExtrae.cmake └── FindOsgGL3.cmake ├── CMakeLists.txt ├── LICENSE.txt ├── README.md ├── doc ├── Changelog.md ├── RelNotes.template └── Tutorial.md ├── examples ├── CMakeLists.txt └── example.cpp ├── include └── GL │ ├── glcorearb.h │ └── glext.h ├── osgTransparency ├── BaseParameters.cpp ├── BaseParameters.h ├── BaseRenderBin.cpp ├── BaseRenderBin.h ├── CMakeLists.txt ├── DepthPeelingBin.cpp ├── DepthPeelingBin.h ├── FragmentListOITBin.cpp ├── FragmentListOITBin.h ├── MultiLayerDepthPeelingBin.cpp ├── MultiLayerDepthPeelingBin.h ├── MultiLayerParameters.h ├── OcclusionQueryGroup.cpp ├── OcclusionQueryGroup.h ├── TextureBuffer.cpp ├── TextureBuffer.h ├── files.cmake ├── multilayer │ ├── Canvas.cpp │ ├── Canvas.h │ ├── Context.cpp │ ├── Context.h │ ├── DepthPartitioner.cpp │ ├── DepthPartitioner.h │ ├── DepthPeelingBin.cpp │ ├── DepthPeelingBin.h │ ├── GL3IterativeDepthPartitioner.cpp │ ├── GL3IterativeDepthPartitioner.h │ ├── IterativeDepthPartitioner.cpp │ ├── IterativeDepthPartitioner.h │ └── Parameters.cpp ├── shaders │ ├── GL2 │ │ ├── multilayer │ │ │ ├── blend.frag │ │ │ ├── depth_partition │ │ │ │ ├── check_partition.frag │ │ │ │ ├── iterative │ │ │ │ │ ├── alpha_adjusted_first_find_quantiles_interval.frag │ │ │ │ │ ├── count_iteration.frag │ │ │ │ │ ├── final_reprojection.frag │ │ │ │ │ ├── find_interval.frag │ │ │ │ │ ├── find_quantiles_interval.frag │ │ │ │ │ ├── first_count.frag │ │ │ │ │ ├── first_count_with_alpha.frag │ │ │ │ │ ├── first_find_quantiles_interval.frag │ │ │ │ │ └── minmax.frag │ │ │ │ └── profile.frag │ │ │ ├── final_pass.frag │ │ │ ├── first_pass.frag │ │ │ └── peel.frag │ │ └── simple │ │ │ └── peel.frag │ └── GL3 │ │ ├── fragment_list │ │ ├── fragment_count_filtering.frag │ │ ├── save_fragments.frag │ │ └── sort_and_display.frag │ │ ├── multilayer │ │ ├── depth_partition │ │ │ ├── check_partition.frag │ │ │ ├── iterative │ │ │ │ ├── alpha_adjusted_first_find_quantiles_interval.frag │ │ │ │ ├── count_iteration.frag │ │ │ │ ├── final_reprojection.frag │ │ │ │ ├── find_interval.frag │ │ │ │ ├── find_quantiles_interval.frag │ │ │ │ ├── first_count.frag │ │ │ │ ├── first_find_quantiles_interval.frag │ │ │ │ └── minmax.frag │ │ │ └── profile.frag │ │ ├── final_pass.frag │ │ ├── first_pass.frag │ │ └── peel.frag │ │ └── simple │ │ └── peel.frag ├── types.h └── util │ ├── GPUTimer.cpp │ ├── GPUTimer.h │ ├── ShapeData.h │ ├── Stats.h │ ├── TextureDebugger.cpp │ ├── TextureDebugger.h │ ├── constants.cpp │ ├── constants.h │ ├── extensions.h │ ├── glerrors.cpp │ ├── glerrors.h │ ├── helpers.cpp │ ├── helpers.h │ ├── loaders.cpp │ ├── loaders.h │ ├── paths.in.h │ ├── strings_array.h │ ├── trace.cpp │ └── trace.h └── tests ├── CMakeLists.txt ├── test_graphics_context.cpp └── unit ├── BaseRenderBin.cpp ├── RenderContext.cpp ├── RenderContext.h ├── compare.cpp ├── compare.h ├── img ├── small_triangle.png └── triangle.png ├── shaders └── GL2 │ ├── main.frag │ ├── main.vert │ ├── stretch.vert │ ├── trivial.frag │ └── trivial.vert ├── util.cpp └── util.h /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | AccessModifierOffset: -4 3 | AlignConsecutiveAssignments: false 4 | AlignConsecutiveDeclarations: false 5 | AlignEscapedNewlinesLeft: true 6 | AlignOperands: true 7 | AlignTrailingComments: true 8 | AllowAllParametersOfDeclarationOnNextLine: true 9 | AllowShortFunctionsOnASingleLine: Inline 10 | AllowShortIfStatementsOnASingleLine: false 11 | AllowShortLoopsOnASingleLine: false 12 | AlwaysBreakBeforeMultilineStrings: true 13 | AlwaysBreakTemplateDeclarations: true 14 | BinPackParameters: true 15 | BreakBeforeBinaryOperators: false 16 | BreakBeforeBraces: Allman # brace on new line 17 | BreakBeforeTernaryOperators: true 18 | BreakConstructorInitializersBeforeComma: true 19 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 20 | ConstructorInitializerIndentWidth: 4 21 | ContinuationIndentWidth: 4 22 | Cpp11BracedListStyle: true 23 | DerivePointerBinding: true 24 | ExperimentalAutoDetectBinPacking: false 25 | IndentCaseLabels: false 26 | IndentFunctionDeclarationAfterType: true 27 | IndentWidth: 4 28 | KeepEmptyLinesAtTheStartOfBlocks: false 29 | Language: Cpp 30 | MaxEmptyLinesToKeep: 1 31 | NamespaceIndentation: None 32 | PenaltyBreakBeforeFirstCallParameter: 100 33 | PenaltyBreakComment: 60 34 | PenaltyBreakFirstLessLess: 120 35 | PenaltyBreakString: 1000 36 | PenaltyExcessCharacter: 1000000 37 | PenaltyReturnTypeOnItsOwnLine: 200 38 | PointerBindsToType: true 39 | SortIncludes: true 40 | SpaceAfterControlStatementKeyword: true 41 | SpaceBeforeAssignmentOperators: true 42 | SpaceBeforeParens: ControlStatements 43 | SpaceInEmptyParentheses: false 44 | SpacesBeforeTrailingComments: 1 45 | SpacesInAngles: false # '< ' style 46 | SpacesInCStyleCastParentheses: false 47 | SpacesInParentheses: false # '(' style 48 | Standard: Cpp11 49 | TabWidth: 4 50 | UseTab: Never 51 | ... 52 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | \#* 2 | .\#* 3 | .gdb_history 4 | GNUmakefile 5 | CMakeLists.txt.user* 6 | .travis.yml 7 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "CMake/common"] 2 | path = CMake/common 3 | url = https://github.com/Eyescale/CMake 4 | -------------------------------------------------------------------------------- /AUTHORS.txt: -------------------------------------------------------------------------------- 1 | Juan Bautista Hernando Vieites 2 | 3 | Carlos Duelo (contributor) 4 | Daniel Nachbaur (contributor) 5 | Judit Planas Carbonell (contributor) 6 | -------------------------------------------------------------------------------- /CMake/.gitignore: -------------------------------------------------------------------------------- 1 | Makefile 2 | -------------------------------------------------------------------------------- /CMake/CPackConfig.cmake: -------------------------------------------------------------------------------- 1 | ## 2 | ## OSG Transparency 3 | ## 4 | ## Copyright (c) 2012-2017 Cajal Blue Brain, BBP/EPFL 5 | ## All rights reserved. Do not distribute without permission. 6 | ## 7 | ## Responsible Author: Juan Hernando Vieites (JHV) 8 | ## contact: jhernando@fi.upm.es 9 | 10 | set(CPACK_PACKAGE_DESCRIPTION_FILE 11 | "${PROJECT_SOURCE_DIR}/CMake/CPackReadme.txt" ) 12 | set(CPACK_PACKAGE_NAME "libosgtransparency") 13 | set(CPACK_PROJECT_NAME "OSG Transparency") 14 | 15 | set(CPACK_DEBIAN_PACKAGE_DEPENDS "libboost-filesystem-dev, libopenscenegraph-dev") 16 | 17 | include(CommonCPack) 18 | -------------------------------------------------------------------------------- /CMake/CPackReadme.txt: -------------------------------------------------------------------------------- 1 | osgTransparency 2 | -------------------------------------------------------------------------------- 3 | 4 | This package includes a C++ library with three RenderBins classes that implement 5 | algorithms for order-independent transparency in OpenSceneGraph: simple depth 6 | peeling, multi-layer depth peeling and fragment linked lists. 7 | -------------------------------------------------------------------------------- /CMake/FindExtrae.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | # Blue Brain Project and 3 | # Universidad Politécnica de Madrid (UPM) 4 | # Juan Hernando 5 | # 6 | # This file is part of osgTransparency 7 | # 8 | # 9 | # This library is free software; you can redistribute it and/or modify it under 10 | # the terms of the GNU Lesser General Public License version 3.0 as published 11 | # by the Free Software Foundation. 12 | # 13 | # This library is distributed in the hope that it will be useful, but WITHOUT 14 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | # details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with this library; if not, write to the Free Software Foundation, Inc., 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | # 22 | #================================== 23 | # 24 | # - Find Extrae 25 | # This module searches for the Extrae library 26 | # 27 | #================================== 28 | # 29 | # The following environment variables are respected for finding Extrae. 30 | # CMAKE_PREFIX_PATH can also be used for this (see find_library() CMake 31 | # documentation). 32 | # 33 | # EXTRAE_ROOT 34 | # 35 | # This module defines the following output variables: 36 | # 37 | # EXTRAE_FOUND - Was Extrae and all of the specified components found? 38 | # 39 | # EXTRAE_VERSION - The version of Extrae which was found 40 | # 41 | # EXTRAE_INCLUDE_DIRS - Where to find the headers 42 | # 43 | # EXTRAE_LIBRARIES - The Extrae libraries 44 | # 45 | #================================== 46 | # Example Usage: 47 | # 48 | # find_package(Extrae 0.3.0 REQUIRED) 49 | # include_directories(${EXTRAE_INCLUDE_DIRS}) 50 | # 51 | # add_executable(foo foo.cc) 52 | # target_link_libraries(foo ${EXTRAE_LIBRARIES}) 53 | # 54 | #================================== 55 | # Naming convention: 56 | # Local variables of the form _Extrae_foo 57 | # Input variables of the form Extrae_FOO 58 | # Output variables of the form EXTRAE_FOO 59 | # 60 | 61 | list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/oss) 62 | include(FindPackageHandleStandardArgs) 63 | 64 | # find and parse extra_version.h 65 | if(NOT _extrae_INCLUDE_DIR OR 66 | NOT EXISTS "${_extrae_INCLUDE_DIR}/extrae_version.h") 67 | find_path(_extrae_INCLUDE_DIR extrae_version.h 68 | HINTS ${CMAKE_SOURCE_DIR}/../../.. $ENV{EXTRAE_ROOT} ${EXTRAE_ROOT} 69 | PATH_SUFFIXES include 70 | PATHS /usr /usr/local /opt/local /opt) 71 | endif() 72 | 73 | if(_extrae_INCLUDE_DIR) 74 | set(_extrae_Version_file "${_extrae_INCLUDE_DIR}/extrae_version.h") 75 | endif() 76 | 77 | if(_extrae_Version_file) 78 | file(READ "${_extrae_Version_file}" _extrae_Version_contents) 79 | string(REGEX MATCH "EXTRAE_VERSION_NUMBER\\([0-9]+,[0-9]+,[0-9]+\\)" 80 | _extrae_VERSION ${_extrae_Version_contents}) 81 | string(REGEX MATCH "[0-9]+,[0-9]+,[0-9]+" 82 | _extrae_VERSION ${_extrae_VERSION}) 83 | string(REGEX REPLACE "([0-9]+),([0-9]+),([0-9]+)" "\\1" 84 | _extrae_VERSION_MAJOR ${_extrae_VERSION}) 85 | string(REGEX REPLACE "([0-9]+),([0-9]+),([0-9]+)" "\\2" 86 | _extrae_VERSION_MINOR ${_extrae_VERSION}) 87 | string(REGEX REPLACE "([0-9]+),([0-9]+),([0-9]+)" "\\1.\\2.\\3" 88 | EXTRAE_VERSION ${_extrae_VERSION}) 89 | endif() 90 | 91 | if (NOT Extrae_FIND_COMPONENTS) 92 | # Defaulting to pptrace 93 | set(Extrae_FIND_COMPONENTS pptrace) 94 | endif() 95 | 96 | set(EXTRAE_LIBRARIES) 97 | set(_extrae_component_library_vars) 98 | foreach(_extrae_component ${Extrae_FIND_COMPONENTS}) 99 | list(APPEND _extrae_component_library_vars ${_extrae_component}_LIBRARY) 100 | find_library(${_extrae_component}_LIBRARY 101 | NAMES ${_extrae_component}-${_extrae_VERSION_MAJOR}.${_extrae_VERSION_MINOR} 102 | ${_extrae_component} 103 | HINTS $ENV{EXTRAE_ROOT} ${EXTRAE_ROOT} 104 | PATHS /usr /usr/local /opt/local /opt 105 | PATH_SUFFIXES lib) 106 | 107 | if (${_extrae_component}_LIBRARY) 108 | list(APPEND EXTRAE_LIBRARIES ${${_extrae_component}_LIBRARY}) 109 | endif() 110 | endforeach() 111 | 112 | set(EXTRAE_INCLUDE_DIRS ${_extrae_INCLUDE_DIR}) 113 | 114 | find_package_handle_standard_args(Extrae 115 | REQUIRED_VARS EXTRAE_LIBRARIES ${_extrae_component_library_vars} 116 | EXTRAE_INCLUDE_DIRS 117 | VERSION_VAR EXTRAE_VERSION) 118 | 119 | if(EXTRAE_FOUND) 120 | message(STATUS "Found Extrae ${EXTRAE_VERSION} " 121 | "in ${EXTRAE_INCLUDE_DIRS}:${EXTRAE_LIBRARIES}") 122 | endif() 123 | -------------------------------------------------------------------------------- /CMake/FindOsgGL3.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | # Blue Brain Project and 3 | # Universidad Politécnica de Madrid (UPM) 4 | # Juan Hernando 5 | # 6 | # This file is part of osgTransparency 7 | # 8 | # 9 | # This library is free software; you can redistribute it and/or modify it under 10 | # the terms of the GNU Lesser General Public License version 3.0 as published 11 | # by the Free Software Foundation. 12 | # 13 | # This library is distributed in the hope that it will be useful, but WITHOUT 14 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | # details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with this library; if not, write to the Free Software Foundation, Inc., 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | 22 | # When OpenSceneGraph is available, either OSG_GL2_AVAILABLE or 23 | # OSG_GL3_AVAILABLE are set to indicate the GL version for which it has been 24 | # built. 25 | # 26 | # This script can be used to make common_find_package_post indicate if a GL3 27 | # build of OpenSceneGraph has been found. 28 | 29 | if(NOT OpenSceneGraph_FOUND) 30 | return() 31 | endif() 32 | 33 | # Detecting whether OSG is compiled with GL 3 support or not 34 | file(READ ${OSG_INCLUDE_DIR}/osg/GL OPENSCENEGRAPH_CONFIG) 35 | string(REGEX MATCH "define OSG_GL3_AVAILABLE" OSG_GL3_AVAILABLE 36 | ${OPENSCENEGRAPH_CONFIG}) 37 | string(REGEX MATCH "define OSG_GL2_AVAILABLE" OSG_GL2_AVAILABLE 38 | ${OPENSCENEGRAPH_CONFIG}) 39 | 40 | if(OSG_GL3_AVAILABLE) 41 | set(OsgGL3_FOUND TRUE) 42 | endif() -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | # Blue Brain Project and 3 | # Universidad Politécnica de Madrid (UPM) 4 | # Juan Hernando 5 | # 6 | # This file is part of osgTransparency 7 | # 8 | # 9 | # This library is free software; you can redistribute it and/or modify it under 10 | # the terms of the GNU Lesser General Public License version 3.0 as published 11 | # by the Free Software Foundation. 12 | # 13 | # This library is distributed in the hope that it will be useful, but WITHOUT 14 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | # details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with this library; if not, write to the Free Software Foundation, Inc., 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | 22 | cmake_minimum_required(VERSION 3.1 FATAL_ERROR) 23 | 24 | project(osgTransparency VERSION 0.8.1) 25 | 26 | list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMake" 27 | "${CMAKE_SOURCE_DIR}/CMake/common") 28 | if(NOT EXISTS ${CMAKE_SOURCE_DIR}/CMake/common/Common.cmake) 29 | message(FATAL_ERROR "CMake/common missing, run: git submodule update --init") 30 | endif() 31 | 32 | set(osgTransparency_VERSION_ABI 8) 33 | 34 | set(OSGTRANSPARENCY_DESCRIPTION 35 | "OSG library for transparency rendering algorithms") 36 | set(OSGTRANSPARENCY_MAINTAINER_NAME "Blue Brain Project") 37 | set(OSGTRANSPARENCY_MAINTAINER_EMAIL "") 38 | set(OSGTRANSPARENCY_MAINTAINER_MAINTAINER "${OSGTRANSPARENCY_MAINTAINER_NAME} ${OSGTRANSPARENCY_MAINTAINER_EMAIL}") 39 | set(OSGTRANSPARENCY_LICENSE GPLv3) 40 | set(OSGTRANSPARENCY_INCLUDE_NAME osgTransparency) 41 | set(OSGTRANSPARENCY_DEB_DEPENDS libopenscenegraph-dev libboost-filesystem-dev) 42 | 43 | set(COMMON_PROJECT_DOMAIN ch.epfl.bluebrain) 44 | include(Common) 45 | 46 | if(NOT IS_ABSOLUTE ${CMAKE_INSTALL_PREFIX}) 47 | set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_PREFIX}) 48 | endif() 49 | 50 | common_find_package(Boost 1.41.0 REQUIRED COMPONENTS filesystem system 51 | unit_test_framework) 52 | common_find_package(OpenGL REQUIRED) 53 | common_find_package(OpenSceneGraph 3.0 REQUIRED COMPONENTS osgGA osgText osgUtil 54 | osgDB osgViewer) 55 | common_find_package(OsgGL3) 56 | common_find_package_post() 57 | set(OSGTRANSPARENCY_DEPENDENT_LIBRARIES Boost) 58 | 59 | if(OSG_GL3_AVAILABLE) 60 | include_directories(${PROJECT_SOURCE_DIR}/include) 61 | endif() 62 | 63 | include_directories(${PROJECT_SOURCE_DIR}/lib/ 64 | ${PROJECT_BINARY_DIR}/lib/ 65 | ${EXTRA_INCLUDE_DIRS} 66 | SYSTEM ${OPENSCENEGRAPH_INCLUDE_DIRS} 67 | ${OPENTHREADS_INCLUDE_PATH} 68 | ${Boost_INCLUDE_DIRS}) 69 | 70 | add_subdirectory(osgTransparency) 71 | add_subdirectory(examples) 72 | add_subdirectory(tests) 73 | 74 | include(CPackConfig) 75 | 76 | set(DOXYGEN_MAINPAGE_MD README.md) 77 | set(DOXYGEN_EXTRA_INPUT ${PROJECT_SOURCE_DIR}/README.md) 78 | set(DOXYGEN_PROJECT_NAME "OSG Transparency") 79 | include(DoxygenRule) 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | Welcome to OSG Transparency, a C++ library that provides 3 algorithms to render 4 | transparent geometry in OpenSceneGraph (OSG) in an improved way compared to 5 | simple back-to-front sorting at object level (what OSG does by default). 6 | 7 | There are several online resources available which may be of interest to 8 | users: 9 | * The [git repository](ssh://github.com/BlueBrain/osgTransparency.git) 10 | * The online copy of this [documentation](https://bluebrain.github.io/osgTransparency-0.8/) 11 | To keep track of the changes between releases check the [changelog](@ref Changelog). 12 | 13 | For code examples on how to use the library check the @ref tutorial. 14 | 15 | # Features 16 | 17 | This library provides 3 algorithm for order-independent-transparency when built 18 | with GL2 support, plus a fourth one when built with GL3 support. The algorithms 19 | are: 20 | * Depth peeling: Simple multi-pass algorithm that sorts the fragment layers 21 | one by one ([original paper](http://developer.download.nvidia.com/SDK/10/opengl/src/dual_depth_peeling/doc/DualDepthPeeling.pdf)). 22 | * Multi-layer depth peeling: Extension of depth peeling to peel several 23 | slices at the same time. The slices are load-balanced in a previous step. 24 | This algorithm is correct as depth peeling and also faster, but slower 25 | than bucket depth peeling. 26 | * Fragment-linked-lists: An A-buffer implementation using lists of fragments. 27 | This algorithm makes use of the GL extension for random access image buffer 28 | objects and so, it is only available in the GL3 build. 29 | 30 | All algorithms are implemented as classes that inherit from osgUtil::RenderBin. 31 | 32 | # Building 33 | 34 | OSG Transparency is a cross-platform library, designed to run on any modern 35 | operating system, including all Unix variants. OSG Transparency uses CMake to 36 | create a platform-specific build environment. The following platforms and build 37 | environments are tested: 38 | 39 | * Linux: Ubuntu 16.04, RHEL 6.8 (Makefile, x64) 40 | 41 | This library depends on Boost and OpenSceneGraph, which are available as binary 42 | packages for Windows and system packages for RedHat and Debian based 43 | distributions. Once the dependencies are installed, building the GL2 version 44 | from source is as simple as: 45 | 46 | git clone --recursive https://github.com/BlueBrain/osgTransparency.git 47 | mkdir osgTransparency/build 48 | cd osgTransparency/build 49 | cmake .. 50 | make 51 | 52 | For the GL3 version, OpenSceneGraph needs to be compiled with GL3 support, 53 | refer to the OpenSceneGraph documentation for compilation instructions. 54 | 55 | # Funding & Acknowledgment 56 | 57 | The development of this software was supported by funding to the Blue Brain Project, 58 | a research center of the École polytechnique fédérale de Lausanne (EPFL), from the 59 | Swiss government’s ETH Board of the Swiss Federal Institutes of Technology. 60 | 61 | # License 62 | 63 | osgTransparency is licensed under the LGPL, unless noted otherwise, e.g., for external 64 | dependencies. See file LICENSE.txt for the full license. 65 | 66 | Copyright (c) 2006-2022 Blue Brain Project/EPFL and Universidad Politécnica de Madrid (UPM) 67 | 68 | This library is free software; you can redistribute it and/or modify it under the terms of 69 | the GNU Lesser General Public License version 3 as published by the Free Software Foundation. 70 | 71 | This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 72 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 73 | See the GNU Lesser General Public License for more details. 74 | 75 | You should have received a copy of the GNU Lesser General Public License along with this 76 | library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 77 | Boston, MA 02110-1301 USA 78 | 79 | -------------------------------------------------------------------------------- /doc/RelNotes.template: -------------------------------------------------------------------------------- 1 | * New Features 2 | * Enhancements 3 | * Optimizations 4 | * Examples 5 | * Tools 6 | * API Changes 7 | * Deprecation 8 | * Documentation 9 | * Bug Fixes 10 | - 11 | OSGTR-XYZ description 12 | * Known Bugs 13 | -------------------------------------------------------------------------------- /doc/Tutorial.md: -------------------------------------------------------------------------------- 1 | Tutorial {#tutorial} 2 | ======== 3 | This brief tutorial shows the key steps to set up multi-layer depth peeling 4 | with 4 slices per pass. 5 | 6 | The first step is to add the render bin to use to the set of render bins 7 | known to OSG. 8 | 9 | \code 10 | bbp::osgTransparency::MultiLayerDepthPeelingBin *renderBin = 11 | renderBin = new bbp::osgTransparency::MultiLayerDepthPeelingBin(); 12 | osgUtil::RenderBin::addRenderBinPrototype("alphaBlended", renderBin); 13 | renderBin->setDefaultNumSlices(4); 14 | \endcode 15 | 16 | Next, the objects to render must include at least a vertex 17 | and fragment shader inside an osg::Program part of their osg::StateSet. 18 | These shaders provide functions needed by the render bins to complete the 19 | GLSL programs used. The function addExtraShadersForState is used to inform 20 | our MultiLayerDepthPeelingBin about which shaders to use for a osg::StateSet. 21 | \code 22 | osg::Program *program = new osg::Program; 23 | // ... create the extra shaders and add them to the program 24 | renderBin->addExtraShadersForState(stateSet, program); 25 | \endcode 26 | Warning, the program object must not be part of the osg::StateSet to add. 27 | 28 | And example of basic vertex shader is: 29 | \code 30 | #version 130 31 | out vec3 normal; 32 | 33 | // Writes to gl_Position. 34 | void shadeVertex() 35 | { 36 | gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; 37 | normal = gl_NormalMatrix * gl_Normal; 38 | } 39 | 40 | // A potentially simplified version of the funcion above to be used 41 | // in passes that only need to know the final fragment depth. 42 | void trivialShadeVertex() 43 | { 44 | shadeVertex(); 45 | } 46 | \endcode 47 | 48 | And for the fragment shader: 49 | \code 50 | #version 130 51 | in vec3 normal; 52 | 53 | // Returns the final color to apply for a shaded fragment 54 | vec4 shadeFragment() 55 | { 56 | vec4 color = vec4(1, 0, 0, 0.2); 57 | vec3 n = normalize(normal); 58 | const vec3 l = vec3(0.0, 0.0, 1.0); 59 | float lambertTerm = dot(n, l); 60 | if (lambertTerm < 0.0) 61 | lambertTerm = -lambertTerm; 62 | vec3 shadedColor = color.rgb * lambertTerm; 63 | return vec4(shadedColor, color.a); 64 | } 65 | 66 | // Returns the alpha value of the fragment. 67 | // This method is used in some optimizations which are not enabled by 68 | // default. It is safe to return 0 as a fallback value. 69 | float fragmentAlpha() 70 | { 71 | return color.a; 72 | } 73 | 74 | // Returns the depth value of the fragment. 75 | // This functions is provided for shaders which can modify the fragment depth 76 | // from the value given in gl_FragCoord.z 77 | float fragmentDepth() 78 | { 79 | return gl_FragCoord.z; 80 | } 81 | \endcode 82 | 83 | Note that there is no main in any of the shaders above (each step of every 84 | algorithm provides their own ones). The functions and prototypes in the 85 | examples above are the interface that the internal shaders expect for each 86 | state set that has been enabled. 87 | 88 | Geometry shaders are also supported without any special consideration. 89 | The current implementation cannot mix opaque and transparent geometry 90 | properly. The main reason is that the z-buffer of the render bins is 91 | independent from the z-buffer of the framebuffer. Future releases will 92 | address this problem. 93 | 94 | Finally, a complete code example can be found [here](https://bbpteam.epfl.ch/reps/viz/osgTransparency.git/tree/examples/example.cpp) 95 | 96 | -------------------------------------------------------------------------------- /examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | # Blue Brain Project and 3 | # Universidad Politécnica de Madrid (UPM) 4 | # Juan Hernando 5 | # 6 | # This file is part of osgTransparency 7 | # 8 | # 9 | # This library is free software; you can redistribute it and/or modify it under 10 | # the terms of the GNU Lesser General Public License version 3.0 as published 11 | # by the Free Software Foundation. 12 | # 13 | # This library is distributed in the hope that it will be useful, but WITHOUT 14 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | # details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with this library; if not, write to the Free Software Foundation, Inc., 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | 22 | set(OSGTRANSPARENCY-EXAMPLE_SOURCES example.cpp) 23 | set(OSGTRANSPARENCY-EXAMPLE_LINK_LIBRARIES osgTransparency) 24 | 25 | common_application(osgTransparency-example NOHELP) 26 | -------------------------------------------------------------------------------- /osgTransparency/BaseParameters.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include "BaseParameters.h" 24 | 25 | namespace bbp 26 | { 27 | namespace osgTransparency 28 | { 29 | /* 30 | Static definitions and constants 31 | */ 32 | bool s_singleQueryPerPass = ::getenv("OSGTRANSPARENCY_SINGLE_QUERY") != 0; 33 | 34 | /* 35 | Constructor 36 | */ 37 | BaseRenderBin::Parameters::Parameters(OptUInt superSampling_) 38 | : maximumPasses(100) 39 | , samplesCutoff(0) 40 | , reservedTextureUnits(4) 41 | , superSampling(superSampling_) 42 | , singleQueryPerPass(s_singleQueryPerPass) 43 | { 44 | } 45 | 46 | /* 47 | Member functions 48 | */ 49 | bool BaseRenderBin::Parameters::update(const Parameters &other) 50 | { 51 | maximumPasses = other.maximumPasses; 52 | samplesCutoff = other.samplesCutoff; 53 | return true; 54 | } 55 | 56 | boost::shared_ptr BaseRenderBin::Parameters::clone() 57 | const 58 | { 59 | return boost::shared_ptr(new Parameters(*this)); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /osgTransparency/BaseParameters.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef TRANSPARENCY_BASEPARAMETERS_H 24 | #define TRANSPARENCY_BASEPARAMETERS_H 25 | 26 | #include "BaseRenderBin.h" 27 | 28 | namespace bbp 29 | { 30 | namespace osgTransparency 31 | { 32 | class OSGTRANSPARENCY_API BaseRenderBin::Parameters 33 | { 34 | public: 35 | /*--- Public declarations ---*/ 36 | 37 | template 38 | class Optional 39 | { 40 | public: 41 | Optional() 42 | : _value() 43 | , _valid(false) 44 | { 45 | } 46 | /* Convenience constructor used to reduce typing length when an 47 | optional parameter needs to be left undefined in the middle 48 | of a function call NULL or (void*)0 can be used to select this 49 | conversion constructor. */ 50 | Optional(const void *) 51 | : _value() 52 | , _valid(false) 53 | { 54 | } 55 | /* non explicit on purpose */ 56 | Optional(const T &v) 57 | : _value(v) 58 | , _valid(true) 59 | { 60 | } 61 | operator T() const 62 | { 63 | assert(_valid); 64 | return _value; 65 | } 66 | bool valid() const { return _valid; } 67 | private: 68 | const T _value; 69 | const bool _valid; 70 | }; 71 | typedef Optional OptUInt; 72 | typedef Optional OptBool; 73 | typedef Optional OptFloat; 74 | 75 | /*--- Public attributes ---*/ 76 | 77 | /** Maximum number of peeling passes for multi-pass algorithms. 78 | 0 for unlimited passes. */ 79 | unsigned int maximumPasses; 80 | /** When the number of samples that have passed through the graphics 81 | pipeline is less or equal to this number no more passes are 82 | performed. */ 83 | unsigned int samplesCutoff; 84 | /** Index of the first texture unit that can be used by the algorithms 85 | in steps that depend on user given shaders. */ 86 | unsigned int reservedTextureUnits; 87 | const unsigned int superSampling; //!< @todo 88 | 89 | bool singleQueryPerPass; //!< @internal 90 | 91 | /*--- Public constructors/detructor ---*/ 92 | 93 | /** 94 | Creates a Parameters object with the following default values: 95 | * maximumPasses: 100 96 | * samplesCutoff: 0 97 | */ 98 | Parameters(OptUInt superSampling = 1); 99 | 100 | virtual ~Parameters() {} 101 | /** @internal */ 102 | /* Updates these parameters with the attributes from other object unless 103 | they are incompatible. 104 | Parameters are compatible when the const declared attributes are equal 105 | (this options require shared recompilation and buffer reallocation to 106 | be changed). 107 | @return True if objects are compatible and attributes updated. 108 | */ 109 | bool update(const Parameters &other); 110 | 111 | virtual boost::shared_ptr clone() const; 112 | }; 113 | } 114 | } 115 | #endif 116 | -------------------------------------------------------------------------------- /osgTransparency/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | # Blue Brain Project and 3 | # Universidad Politécnica de Madrid (UPM) 4 | # Juan Hernando 5 | # 6 | # This file is part of osgTransparency 7 | # 8 | # 9 | # This library is free software; you can redistribute it and/or modify it under 10 | # the terms of the GNU Lesser General Public License version 3.0 as published 11 | # by the Free Software Foundation. 12 | # 13 | # This library is distributed in the hope that it will be useful, but WITHOUT 14 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | # details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with this library; if not, write to the Free Software Foundation, Inc., 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | 22 | include(files.cmake) 23 | 24 | set(OSGTRANSPARENCY_LINK_LIBRARIES 25 | PUBLIC ${Boost_LIBRARIES} ${OPENSCENEGRAPH_LIBRARIES} 26 | PRIVATE ${EXTRAE_LIBRARIES} ${OPENGL_LIBRARIES}) 27 | 28 | common_library(osgTransparency) 29 | 30 | install(DIRECTORY shaders DESTINATION share/osgTransparency) 31 | -------------------------------------------------------------------------------- /osgTransparency/DepthPeelingBin.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_DEPTHPEELINGBIN_H 24 | #define OSGTRANSPARENCY_DEPTHPEELINGBIN_H 25 | 26 | #include "BaseRenderBin.h" 27 | 28 | namespace bbp 29 | { 30 | namespace osgTransparency 31 | { 32 | class OSGTRANSPARENCY_API DepthPeelingBin : public BaseRenderBin 33 | { 34 | public: 35 | /*--- Public constructors/destructor ---*/ 36 | 37 | DepthPeelingBin(); 38 | 39 | DepthPeelingBin(const Parameters ¶meters); 40 | 41 | DepthPeelingBin(const DepthPeelingBin &renderBin, 42 | const osg::CopyOp ©op); 43 | 44 | /*--- Public member functions ---*/ 45 | 46 | META_Object(osg, DepthPeelingBin); 47 | 48 | const Parameters &getParameters() const { return *_parameters; } 49 | Parameters &getParameters() { return *_parameters; } 50 | virtual void sort() {} 51 | /*--- Protected member functions ---*/ 52 | protected: 53 | virtual void drawImplementation(osg::RenderInfo &renderInfo, 54 | osgUtil::RenderLeaf *&previous); 55 | 56 | private: 57 | /*--- Private declarations ---*/ 58 | class _Impl; 59 | }; 60 | } 61 | } 62 | #endif 63 | -------------------------------------------------------------------------------- /osgTransparency/MultiLayerDepthPeelingBin.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include "MultiLayerParameters.h" 24 | #include "multilayer/Canvas.h" 25 | #include "multilayer/Context.h" 26 | #include "multilayer/DepthPeelingBin.h" 27 | #include "util/constants.h" 28 | #include "util/trace.h" 29 | 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | namespace bbp 37 | { 38 | namespace osgTransparency 39 | { 40 | /* 41 | Constructors/destructor 42 | */ 43 | MultiLayerDepthPeelingBin::MultiLayerDepthPeelingBin() 44 | { 45 | } 46 | 47 | MultiLayerDepthPeelingBin::MultiLayerDepthPeelingBin( 48 | const Parameters& parameters) 49 | : BaseRenderBin(boost::shared_ptr(new Parameters(parameters))) 50 | { 51 | } 52 | 53 | MultiLayerDepthPeelingBin::MultiLayerDepthPeelingBin( 54 | const MultiLayerDepthPeelingBin& renderBin, const osg::CopyOp& copyop) 55 | : BaseRenderBin(renderBin, copyop) 56 | { 57 | } 58 | 59 | MultiLayerDepthPeelingBin::~MultiLayerDepthPeelingBin() 60 | { 61 | } 62 | 63 | /* 64 | Member functions 65 | */ 66 | 67 | const MultiLayerDepthPeelingBin::Parameters& 68 | MultiLayerDepthPeelingBin::getParameters() const 69 | { 70 | return static_cast(*_parameters); 71 | } 72 | 73 | MultiLayerDepthPeelingBin::Parameters& 74 | MultiLayerDepthPeelingBin::getParameters() 75 | { 76 | return static_cast(*_parameters); 77 | } 78 | 79 | void MultiLayerDepthPeelingBin::sort() 80 | { 81 | /* Do nothing as sortByState does nothing in the original implementation 82 | from OSG */ 83 | } 84 | 85 | void MultiLayerDepthPeelingBin::drawImplementation( 86 | osg::RenderInfo& renderInfo, osgUtil::RenderLeaf*& previous) 87 | { 88 | OSGTRANSPARENCY_TRACE_FUNCTION(); 89 | 90 | /* This render bin must be transparent to the state management. 91 | Our goal here is that OSG has a consistent State object and that 92 | the following algorithm is transparent to any calling prerender bins. 93 | That means that they have to find the state stack and the current 94 | OpenGL state in the state you can expect from a regular bin. */ 95 | 96 | /* Each frame a new alphablend::MultiLayerDepthPeelingBin is created by OSG 97 | internals we can't rely on the attributes of this class and have to store 98 | frame to frame state in an static per context map. */ 99 | 100 | /** \bug The current implementation doesn't support recursive calls 101 | resulting from nested render bins */ 102 | osg::State& state = *renderInfo.getState(); 103 | multilayer::Context& context = multilayer::DepthPeelingBin::getContext( 104 | state, static_cast(*_parameters)); 105 | 106 | context.startFrame(this, renderInfo, previous); 107 | 108 | multilayer::Canvas* canvas = context.getCanvas(); 109 | while (!canvas->checkFinished()) 110 | { 111 | canvas->peel(this, renderInfo); 112 | canvas->blend(renderInfo); 113 | } 114 | 115 | context.finishFrame(renderInfo, previous); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /osgTransparency/MultiLayerDepthPeelingBin.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_MULTILAYERDEPTHPEELINGBIN_H 24 | #define OSGTRANSPARENCY_MULTILAYERDEPTHPEELINGBIN_H 25 | 26 | #include "BaseRenderBin.h" 27 | 28 | namespace bbp 29 | { 30 | namespace osgTransparency 31 | { 32 | namespace multilayer 33 | { 34 | class Canvas; 35 | class DepthPartitioner; 36 | } 37 | 38 | class OSGTRANSPARENCY_API MultiLayerDepthPeelingBin : public BaseRenderBin 39 | { 40 | public: 41 | friend class multilayer::Canvas; 42 | friend class multilayer::DepthPartitioner; 43 | 44 | /*--- Public declarations ---*/ 45 | 46 | class Parameters; 47 | 48 | /*--- Public constructors/destructor */ 49 | 50 | MultiLayerDepthPeelingBin(); 51 | 52 | // cppcheck-suppress passedByValue 53 | MultiLayerDepthPeelingBin(const Parameters ¶maters); 54 | 55 | MultiLayerDepthPeelingBin(const MultiLayerDepthPeelingBin &renderBin, 56 | const osg::CopyOp ©op); 57 | 58 | ~MultiLayerDepthPeelingBin(); 59 | 60 | /*--- Public member functions ---*/ 61 | 62 | META_Object(osgTransparency, MultiLayerDepthPeelingBin); 63 | 64 | const Parameters &getParameters() const; 65 | 66 | Parameters &getParameters(); 67 | 68 | virtual void sort(); 69 | 70 | protected: 71 | /*--- Protected member functions ---*/ 72 | 73 | virtual void drawImplementation(osg::RenderInfo &renderInfo, 74 | osgUtil::RenderLeaf *&previous); 75 | 76 | private: 77 | /*--- Private member functions ---*/ 78 | 79 | // These functions are to be called from Canvas 80 | using BaseRenderBin::render; 81 | }; 82 | } 83 | } 84 | #endif 85 | -------------------------------------------------------------------------------- /osgTransparency/MultiLayerParameters.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_MULTILAYERPARAMETERS_H 24 | #define OSGTRANSPARENCY_MULTILAYERPARAMETERS_H 25 | 26 | #include "BaseParameters.h" 27 | #include "MultiLayerDepthPeelingBin.h" 28 | 29 | namespace bbp 30 | { 31 | namespace osgTransparency 32 | { 33 | class OSGTRANSPARENCY_API MultiLayerDepthPeelingBin::Parameters 34 | : public BaseRenderBin::Parameters 35 | { 36 | public: 37 | /*--- Public declarations ---*/ 38 | 39 | typedef std::vector QuantileList; 40 | 41 | /*--- Public members ---*/ 42 | 43 | const float opacityThreshold; 44 | const QuantileList splitPointQuantiles; 45 | const bool unprojectDepths; 46 | const bool alphaAwarePartition; 47 | 48 | /*--- Public constructors/destructor ---*/ 49 | 50 | /** 51 | Creates a Parameters object 52 | 53 | @param slices Number of depth slices to use for depth peeling. A number 54 | between 1 and 8. 55 | @param unprojectDepths Whether use unprojected z values or not. 56 | @param alphaAwarePartition Adjust the quatiles for the depth partition 57 | based on alpha values (This only makes sense if slices >= 2). 58 | @param superSampling Unimplemented 59 | @param opacityThreshold The accumulated opacity at a fragment at which 60 | fragments behind can be considered completely occluded. 61 | 62 | Default values for unspecified arguments are 63 | * opacityThreshold: 0.99 64 | * splitPointQuantiles: [0.5] 65 | * unprojectDepths: false 66 | * alphaAwarePartition: false 67 | 68 | The following environmental variables are looked up to override 69 | defaults: 70 | * OSGTRANSPARENCY_REPROJECT_QUANTILES 71 | * OSGTRANSPARENCY_ALPHA_AWARE_PARTITION 72 | * OSGTRANSPARENCY_OPACITY_THRESHOLD 73 | * OSGTRANSPARENCY_SPLIT_QUANTILES: A list of exact quantiles to use 74 | instead of even spacing. 75 | */ 76 | Parameters(OptUInt slices = OptUInt(), OptBool unprojectDepths = OptBool(), 77 | OptBool alphaAwarePartition = OptBool(), 78 | OptUInt superSampling = 1, 79 | OptFloat opacityThreshold = OptFloat()); 80 | 81 | /*--- Public member functions ---*/ 82 | 83 | /** @sa BaseRenderBin::Parameters::update */ 84 | bool update(const Parameters &other); 85 | 86 | /** @internal */ 87 | unsigned int getNumSlices() const { return splitPointQuantiles.size() + 1; } 88 | /** @internal */ 89 | unsigned int getAdjustedNumPoints() const 90 | { 91 | if (alphaAwarePartition) 92 | /* Adding an extra split point to detect which fragments fall 93 | beyond the depth at which opacity has been found to 94 | accumulate over the threshold. */ 95 | return splitPointQuantiles.size() + 1; 96 | else 97 | return splitPointQuantiles.size(); 98 | } 99 | }; 100 | } 101 | } 102 | #endif 103 | -------------------------------------------------------------------------------- /osgTransparency/OcclusionQueryGroup.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_OCCLUSIONQUERYGROUP_H 24 | #define OSGTRANSPARENCY_OCCLUSIONQUERYGROUP_H 25 | 26 | #include 27 | 28 | #include 29 | 30 | namespace osg 31 | { 32 | class RenderInfo; 33 | } 34 | 35 | namespace bbp 36 | { 37 | namespace osgTransparency 38 | { 39 | class OcclusionQueryGroup : public osg::Referenced 40 | { 41 | public: 42 | /*--- Public declarations ---*/ 43 | 44 | /** @internal */ 45 | class PerContextQueries; 46 | 47 | /*--- Public constructors/destructor ---*/ 48 | 49 | /** 50 | Must be created inside a valid OpenGL context. 51 | */ 52 | OcclusionQueryGroup(osg::RenderInfo& renderInfo); 53 | 54 | ~OcclusionQueryGroup(); 55 | 56 | /*--- Public member functions ----*/ 57 | 58 | void reset(); 59 | 60 | /** 61 | Increments the pass number. 62 | */ 63 | void beginPass(); 64 | 65 | void beginQuery(unsigned int index); 66 | 67 | void endQuery(); 68 | 69 | /** @param pass Maximum pass with all queries completed since 70 | last reset. 71 | @param samples Total number of samples for the last completed 72 | pass for each query index. 73 | @param latency Maximum difference allowed between the current pass 74 | and the pass of pending queries. 75 | @return true when pass and samples are written valid information 76 | and false when they are left unmodified. */ 77 | bool checkQueries(unsigned int& pass, unsigned int& samples, 78 | unsigned int latency = 1); 79 | 80 | bool lastestSamplesPassed(unsigned int index, unsigned int& samples); 81 | 82 | private: 83 | /*--- Private member attributes ---*/ 84 | class Impl; 85 | Impl* _impl; 86 | }; 87 | } 88 | } 89 | #endif 90 | -------------------------------------------------------------------------------- /osgTransparency/files.cmake: -------------------------------------------------------------------------------- 1 | ## 2 | ## OSG Transparency 3 | ## 4 | ## Copyright (c) 2012-2016 Cajal Blue Brain, BBP/EPFL 5 | ## All rights reserved. Do not distribute without permission. 6 | ## 7 | ## Responsible Author: Juan Hernando Vieites (JHV) 8 | ## contact: jhernando@fi.upm.es 9 | 10 | set(OSGTRANSPARENCY_PUBLIC_HEADERS 11 | BaseRenderBin.h 12 | BaseParameters.h 13 | DepthPeelingBin.h 14 | FragmentListOITBin.h 15 | MultiLayerDepthPeelingBin.h 16 | MultiLayerParameters.h 17 | types.h 18 | ) 19 | 20 | set(OSGTRANSPARENCY_HEADERS 21 | OcclusionQueryGroup.h 22 | multilayer/Canvas.h 23 | multilayer/Context.h 24 | multilayer/DepthPartitioner.h 25 | multilayer/DepthPeelingBin.h 26 | multilayer/GL3IterativeDepthPartitioner.h 27 | multilayer/IterativeDepthPartitioner.h 28 | util/Stats.h 29 | util/ShapeData.h 30 | util/TextureDebugger.h 31 | util/constants.h 32 | util/extensions.h 33 | util/glerrors.h 34 | util/helpers.h 35 | util/loaders.h 36 | util/paths.in.h 37 | util/strings_array.h 38 | util/trace.h 39 | ) 40 | 41 | set(OSGTRANSPARENCY_SOURCES 42 | ${OSGTRANSPARENCY_HEADERS} 43 | BaseParameters.cpp 44 | BaseRenderBin.cpp 45 | DepthPeelingBin.cpp 46 | FragmentListOITBin.cpp 47 | OcclusionQueryGroup.cpp 48 | MultiLayerDepthPeelingBin.cpp 49 | multilayer/DepthPartitioner.cpp 50 | multilayer/DepthPeelingBin.cpp 51 | multilayer/Canvas.cpp 52 | multilayer/Context.cpp 53 | multilayer/Parameters.cpp 54 | util/GPUTimer.cpp 55 | util/TextureDebugger.cpp 56 | util/constants.cpp 57 | util/glerrors.cpp 58 | util/helpers.cpp 59 | util/loaders.cpp 60 | util/trace.cpp 61 | ) 62 | 63 | configure_file(util/paths.in.h 64 | ${PROJECT_BINARY_DIR}/lib/osgTransparency/util/paths.h) 65 | list(APPEND OSGTRANSPARENCY_SOURCES 66 | ${PROJECT_BINARY_DIR}/lib/osgTransparency/util/paths.h) 67 | 68 | if(OSG_GL3_AVAILABLE) 69 | list(APPEND OSGTRANSPARENCY_SOURCES 70 | multilayer/GL3IterativeDepthPartitioner.cpp 71 | TextureBuffer.cpp) 72 | 73 | list(APPEND OSGTRANSPARENCY_PUBLIC_HEADERS 74 | TextureBuffer.h) 75 | 76 | else() 77 | list(APPEND OSGTRANSPARENCY_SOURCES 78 | multilayer/IterativeDepthPartitioner.cpp) 79 | endif() 80 | -------------------------------------------------------------------------------- /osgTransparency/multilayer/Context.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_MULTILAYER_CONTEXT_H 24 | #define OSGTRANSPARENCY_MULTILAYER_CONTEXT_H 25 | 26 | #include "DepthPeelingBin.h" 27 | 28 | #include "osgTransparency/MultiLayerParameters.h" 29 | #include "osgTransparency/util/constants.h" 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | namespace bbp 36 | { 37 | namespace osgTransparency 38 | { 39 | namespace multilayer 40 | { 41 | class Canvas; 42 | 43 | class Context 44 | { 45 | public: 46 | /*--- Public declarations ---*/ 47 | 48 | osgUtil::RenderLeaf* oldPrevious; 49 | 50 | /*--- Public constructors/destructor ---*/ 51 | 52 | Context(const Parameters& parameters); 53 | 54 | ~Context(); 55 | 56 | /*--- Public member functions ---*/ 57 | 58 | const Parameters& getParameters() const { return _parameters; } 59 | /** Returns false if new parameters are incompatible with this context */ 60 | bool updateParameters(const Parameters& parameters); 61 | 62 | Canvas* getCanvas() { return _canvas.get(); } 63 | unsigned int getID() { return _id; } 64 | void startFrame(MultiLayerDepthPeelingBin* bin, osg::RenderInfo& renderInfo, 65 | osgUtil::RenderLeaf*& previous); 66 | 67 | void finishFrame(osg::RenderInfo& renderInfo, 68 | osgUtil::RenderLeaf*& previous); 69 | 70 | const osg::FrameStamp* getFrameStamp() { return _frameStamp.get(); } 71 | private: 72 | /*--- Private member variables ---*/ 73 | 74 | unsigned int _id; 75 | osg::ref_ptr _canvas; 76 | 77 | GLint _previousFBO; 78 | unsigned int _savedStackPosition; 79 | 80 | Parameters _parameters; 81 | 82 | #ifndef NDEBUG 83 | osg::ref_ptr _oldState; 84 | #endif 85 | 86 | osg::ref_ptr _frameStamp; 87 | 88 | /*--- Private member functions ---*/ 89 | 90 | /** 91 | Recovers the frame stamp from the osgUtil::SceneView object that is 92 | being rendered. 93 | renderInfo.getState().getFrameStamp() has race conditions in draw thread 94 | per context configurations. 95 | This function accesses the framestamp from the current SceneView, 96 | which can be thread safe if a different FrameStamp object is used each 97 | frame. 98 | */ 99 | void _updateFrameStamp(BaseRenderBin* bin, osg::RenderInfo& renderInfo); 100 | }; 101 | } 102 | } 103 | } 104 | #endif 105 | -------------------------------------------------------------------------------- /osgTransparency/multilayer/DepthPartitioner.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_MULTILAYER_DEPTHPARTITIONER_H 24 | #define OSGTRANSPARENCY_MULTILAYER_DEPTHPARTITIONER_H 25 | 26 | #include "DepthPeelingBin.h" 27 | 28 | namespace osg 29 | { 30 | class FrameBufferObject; 31 | class RenderInfo; 32 | class TextureRectangle; 33 | } 34 | 35 | namespace bbp 36 | { 37 | namespace osgTransparency 38 | { 39 | class MultiLayerDepthPeelingBin; 40 | 41 | namespace multilayer 42 | { 43 | class DepthPartitioner : public osg::Referenced 44 | { 45 | public: 46 | /*--- Public constructors and destructor ---*/ 47 | 48 | /* Stores a reference to the parameters */ 49 | DepthPartitioner(const Parameters& parameters); 50 | 51 | virtual ~DepthPartitioner(); 52 | 53 | /*--- Public member functions ---*/ 54 | 55 | virtual void createBuffersAndTextures(unsigned int maxWidth, 56 | unsigned int maxHeight) = 0; 57 | 58 | virtual void createStateSets() = 0; 59 | 60 | void updateProjectionUniforms(const osg::Matrix& projection); 61 | 62 | virtual void updateShaderPrograms(const ProgramMap& extraShaders) = 0; 63 | 64 | virtual void computeDepthPartition(MultiLayerDepthPeelingBin* bin, 65 | osg::RenderInfo& renderInfo, 66 | osgUtil::RenderLeaf*& previous) = 0; 67 | 68 | /** 69 | Adds the depthPartition uniform to the stateset 70 | */ 71 | virtual void addDepthPartitionExtraState(osg::StateSet* stateSet, 72 | unsigned int textureUnitHint); 73 | /** 74 | Completes the programs with functions specific to this depth partition. 75 | */ 76 | virtual void addDepthPartitionExtraShaders(ProgramMap& programs); 77 | 78 | virtual size_t computeMaxDepthComplexity(MultiLayerDepthPeelingBin* bin, 79 | osg::RenderInfo& renderInfo, 80 | osgUtil::RenderLeaf*& previous); 81 | 82 | virtual void profileDepthPartition(MultiLayerDepthPeelingBin* bin, 83 | osg::RenderInfo& renderInfo, 84 | osgUtil::RenderLeaf*& previous); 85 | 86 | virtual osg::ref_ptr* 87 | getDepthPartitionTextureArray() = 0; 88 | 89 | protected: 90 | /*--- Protected member attributes ---*/ 91 | 92 | const Parameters& _parameters; 93 | 94 | osg::ref_ptr _projection_33; 95 | osg::ref_ptr _projection_34; 96 | 97 | /*--- Protected member functions ---*/ 98 | void render(MultiLayerDepthPeelingBin* bin, osg::RenderInfo& renderInfo, 99 | osgUtil::RenderLeaf*& previous, osg::StateSet* baseStateSet, 100 | ProgramMap& programs, osg::RefMatrix* projection = 0, 101 | OcclusionQueryGroup* queryGroup = 0) 102 | { 103 | bin->render(renderInfo, previous, baseStateSet, programs, projection, 104 | queryGroup); 105 | } 106 | 107 | void renderBounds(MultiLayerDepthPeelingBin* bin, 108 | osg::RenderInfo& renderInfo, osg::StateSet* baseStateSet, 109 | ProgramMap& programs, BoundShapesStorage& shapes, 110 | osg::RefMatrix* projection = 0) 111 | { 112 | bin->renderBounds(renderInfo, baseStateSet, programs, shapes, 113 | projection); 114 | } 115 | }; 116 | } 117 | } 118 | } 119 | #endif 120 | -------------------------------------------------------------------------------- /osgTransparency/multilayer/DepthPeelingBin.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include "DepthPeelingBin.h" 24 | #include "Context.h" 25 | 26 | #include 27 | 28 | namespace bbp 29 | { 30 | namespace osgTransparency 31 | { 32 | namespace multilayer 33 | { 34 | /* 35 | Static definitions 36 | */ 37 | 38 | namespace 39 | { 40 | OpenThreads::Mutex _contextMapMutex; 41 | } 42 | 43 | const bool DepthPeelingBin::COMPUTE_MAX_DEPTH_COMPLEXITY = 44 | ::getenv("OSGTRANSPARENCY_COMPUTE_DEPTH_COMPLEXITY") != 0; 45 | const bool DepthPeelingBin::PROFILE_DEPTH_PARTITION = 46 | ::getenv("OSGTRANSPARENCY_PROFILE_DEPTH_PARTITION") != 0; 47 | const DepthPeelingBin::DebugPartition DepthPeelingBin::DEBUG_PARTITION; 48 | 49 | DepthPeelingBin::ContextMap DepthPeelingBin::s_contextMap; 50 | 51 | /* 52 | Helper classes functions 53 | */ 54 | 55 | class DepthPeelingBin::StateObserver : public osg::Observer 56 | { 57 | public: 58 | void objectDeleted(void *object) final 59 | { 60 | OpenThreads::ScopedLock lock(_contextMapMutex); 61 | s_contextMap.erase(object); 62 | } 63 | } s_contextObserver; 64 | 65 | DepthPeelingBin::DebugPartition::DebugPartition() 66 | : column(-1) 67 | , row(-1) 68 | , badPixelFeatures(std::make_pair(-1, -1)) 69 | { 70 | const char *debug = ::getenv("OSGTRANSPARENCY_DEBUG_PARTITION"); 71 | if (debug != 0) 72 | { 73 | if (!strncmp(debug, "find=", 5)) 74 | { 75 | for (debug += 5, badPixelFeatures.first = 0; *debug == '.'; 76 | ++debug, ++badPixelFeatures.first) 77 | ; 78 | badPixelFeatures.second = strtol(debug, 0, 10); 79 | } 80 | else 81 | { 82 | char *endptr; 83 | column = strtol(debug, &endptr, 10); 84 | row = strtol(endptr + 1, &endptr, 10); 85 | } 86 | } 87 | } 88 | 89 | bool DepthPeelingBin::DebugPartition::debugPixel() const 90 | { 91 | return column != -1 && row != -1; 92 | } 93 | 94 | bool DepthPeelingBin::DebugPartition::findBadPixel() const 95 | { 96 | return badPixelFeatures.first != -1; 97 | } 98 | 99 | bool DepthPeelingBin::DebugPartition::debugAlphaAccumulation() const 100 | { 101 | static bool debug = 102 | ::getenv("OSGTRANSPARENCY_DEBUG_ALPHA_ACCUMULATION") != 0; 103 | return debug; 104 | } 105 | 106 | Context &DepthPeelingBin::getContext(const osg::State &state, 107 | const Parameters ¶meters) 108 | { 109 | /* Multiple draw threads might be trying to create their own 110 | alpha-blending context */ 111 | OpenThreads::ScopedLock lock(_contextMapMutex); 112 | ContextPtr &context = s_contextMap[&state]; 113 | 114 | if (!context) 115 | state.addObserver(&s_contextObserver); 116 | 117 | if (!context || !context->updateParameters(parameters)) 118 | context.reset(new Context(parameters)); 119 | 120 | return *context; 121 | } 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /osgTransparency/multilayer/DepthPeelingBin.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_MULTILAYER_DEPTHPEELINGBIN_H 24 | #define OSGTRANSPARENCY_MULTILAYER_DEPTHPEELINGBIN_H 25 | 26 | #include "osgTransparency/MultiLayerDepthPeelingBin.h" 27 | 28 | namespace bbp 29 | { 30 | namespace osgTransparency 31 | { 32 | namespace multilayer 33 | { 34 | typedef MultiLayerDepthPeelingBin::Parameters Parameters; 35 | 36 | class Context; 37 | 38 | class DepthPeelingBin 39 | { 40 | /*-- Public declarations ---*/ 41 | public: 42 | class StateObserver; 43 | 44 | /* Constants from environmental variables */ 45 | static const bool COMPUTE_MAX_DEPTH_COMPLEXITY; 46 | static const bool PROFILE_DEPTH_PARTITION; 47 | struct DebugPartition 48 | { 49 | DebugPartition(); 50 | bool debugPixel() const; 51 | bool findBadPixel() const; 52 | bool debugAlphaAccumulation() const; 53 | int column; 54 | int row; 55 | std::pair badPixelFeatures; /* slice, size */ 56 | } static const DEBUG_PARTITION; // Name class with macro 57 | 58 | /*--- Public member functions ---*/ 59 | public: 60 | static Context &getContext(const osg::State &state, 61 | const Parameters &options); 62 | 63 | private: 64 | /*--- Private member attributes ---*/ 65 | typedef boost::shared_ptr ContextPtr; 66 | /* The void pointer is the osg::State* from the osg::GraphicsContext. 67 | We use a void pointer to avoid the temptation of using the osg::State*, 68 | for example when the osg::State signals that it's to be deleted 69 | (the signal comes from ~Referenced, so the osg::State is already 70 | destroyed and we shouldn't use it) */ 71 | typedef std::map ContextMap; 72 | static ContextMap s_contextMap; 73 | }; 74 | } 75 | } 76 | } 77 | #endif 78 | -------------------------------------------------------------------------------- /osgTransparency/multilayer/GL3IterativeDepthPartitioner.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_MULTILAYER_GL3ITERATIVEDEPTHPARTITIONER_H 24 | #define OSGTRANSPARENCY_MULTILAYER_GL3ITERATIVEDEPTHPARTITIONER_H 25 | 26 | #include "DepthPartitioner.h" 27 | 28 | namespace bbp 29 | { 30 | namespace osgTransparency 31 | { 32 | namespace multilayer 33 | { 34 | class GL3IterativeDepthPartitioner : public DepthPartitioner 35 | { 36 | public: 37 | /*--- Public constructors/destructor ---*/ 38 | 39 | GL3IterativeDepthPartitioner(const Parameters& parameters); 40 | 41 | /*--- Public member functions ---*/ 42 | 43 | void createBuffersAndTextures(unsigned int maxWidth, 44 | unsigned int maxHeight) final; 45 | 46 | void createStateSets() final; 47 | 48 | virtual void updateShaderPrograms(const ProgramMap& extraShaders) final; 49 | 50 | void computeDepthPartition(MultiLayerDepthPeelingBin* bin, 51 | osg::RenderInfo& renderInfo, 52 | osgUtil::RenderLeaf*& previous) final; 53 | 54 | osg::ref_ptr* getDepthPartitionTextureArray() 55 | { 56 | return _depthPartitionTexture; 57 | } 58 | 59 | private: 60 | /*--- Private member attributes ---*/ 61 | static std::string _shaderPath; 62 | 63 | /* State sets with the different operations for depth partitioning */ 64 | osg::ref_ptr _minMax; 65 | osg::ref_ptr _firstCount; 66 | osg::ref_ptr _firstFindQuantileIntervals; 67 | osg::ref_ptr _countIteration; 68 | osg::ref_ptr _findQuantileIntervals; 69 | osg::ref_ptr _finalProjection; 70 | 71 | osg::ref_ptr _viewport; 72 | 73 | osg::ref_ptr _iteration; 74 | osg::ref_ptr _quantiles; 75 | osg::ref_ptr _quantiles2; 76 | 77 | osg::ref_ptr _minMaxTexture; 78 | std::vector> _countTextures; 79 | osg::ref_ptr _codedIntervalsTextures[2]; 80 | osg::ref_ptr _leftAccumTextures[2]; 81 | osg::ref_ptr _totalCountsTexture; 82 | 83 | osg::ref_ptr _depthPartitionTexture[2]; 84 | 85 | osg::ref_ptr _auxiliaryBuffer; 86 | 87 | osg::ref_ptr _quad; 88 | 89 | ProgramMap _minMaxPrograms; 90 | ProgramMap _firstCountPrograms; 91 | ProgramMap _countIterationPrograms; 92 | 93 | BoundShapesStorage _shapes; 94 | 95 | struct DebugHelpers; 96 | 97 | /*--- Private member functions ---*/ 98 | 99 | void _createMinMaxCalculationStateSet(); 100 | void _createFirstCountStateSet(); 101 | void _createCountIterationStateSet(size_t points); 102 | void _createFirstFindQuantileIntervalsStateSet( 103 | const std::vector& quantiles); 104 | void _createFindQuantileIntervalsStateSet( 105 | const std::vector& quantiles); 106 | void _createFinalReprojectionStateSet(unsigned int points); 107 | 108 | void _updateMinMaxCalculationPrograms(const ProgramMap& extraShaders); 109 | void _updateFirstCountPrograms(const ProgramMap& extraShaders); 110 | void _updateCountIterationPrograms(const ProgramMap& extraShaders, 111 | size_t points); 112 | }; 113 | } 114 | } 115 | } 116 | #endif 117 | -------------------------------------------------------------------------------- /osgTransparency/multilayer/IterativeDepthPartitioner.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_MULTILAYER_ITERATIVEDEPTHPARTITIONER_H 24 | #define OSGTRANSPARENCY_MULTILAYER_ITERATIVEDEPTHPARTITIONER_H 25 | 26 | namespace osg 27 | { 28 | class FrameBufferObject; 29 | class RenderInfo; 30 | class TextureRectangle; 31 | } 32 | 33 | namespace osgUtil 34 | { 35 | class RenderLeaf; 36 | } 37 | 38 | #include "DepthPartitioner.h" 39 | #include "DepthPeelingBin.h" 40 | 41 | #include 42 | 43 | namespace bbp 44 | { 45 | namespace osgTransparency 46 | { 47 | namespace multilayer 48 | { 49 | class Context; 50 | 51 | class IterativeDepthPartitioner : public DepthPartitioner 52 | { 53 | public: 54 | /*--- Public constructors/destructor ---*/ 55 | 56 | IterativeDepthPartitioner(const Parameters& parameters); 57 | 58 | ~IterativeDepthPartitioner(); 59 | 60 | /*--- Public member functions ---*/ 61 | 62 | void createBuffersAndTextures(unsigned int maxWidth, 63 | unsigned int maxHeight) final; 64 | 65 | void createStateSets() final; 66 | 67 | void updateShaderPrograms(const ProgramMap& extraShaders) final; 68 | 69 | void computeDepthPartition(MultiLayerDepthPeelingBin* bin, 70 | osg::RenderInfo& renderInfo, 71 | osgUtil::RenderLeaf*& previous) final; 72 | 73 | osg::ref_ptr* getDepthPartitionTextureArray() final 74 | { 75 | return _depthPartitionTexture; 76 | } 77 | 78 | private: 79 | /*--- Private member attributes ---*/ 80 | 81 | static std::string _shaderPath; 82 | 83 | /* State sets with the different operations for depth partitioning */ 84 | osg::ref_ptr _minMax; 85 | osg::ref_ptr _firstCount; 86 | osg::ref_ptr _countIteration; 87 | osg::ref_ptr _firstFindQuantileIntervals; 88 | osg::ref_ptr _findQuantileIntervals; 89 | osg::ref_ptr _finalProjection; 90 | 91 | osg::ref_ptr _viewport; 92 | 93 | osg::ref_ptr _iteration; 94 | osg::ref_ptr _quantiles; 95 | osg::ref_ptr _quantiles2; 96 | 97 | osg::ref_ptr _countTextures[8]; 98 | osg::ref_ptr _codedIntervalsTextures[2]; 99 | osg::ref_ptr _leftAccumTextures[2]; 100 | osg::ref_ptr _minMaxTexture; 101 | osg::ref_ptr _totalCountsTexture; 102 | 103 | osg::ref_ptr _depthPartitionTexture[2]; 104 | 105 | osg::ref_ptr _auxiliaryBuffer; 106 | 107 | osg::ref_ptr _quad; 108 | 109 | ProgramMap _minMaxPrograms; 110 | ProgramMap _firstCountPrograms; 111 | ProgramMap _countIterationPrograms; 112 | 113 | BoundShapesStorage _shapes; 114 | 115 | struct DebugHelpers; 116 | 117 | void _createMinMaxCalculationStateSet(); 118 | void _createFirstCountStateSet(); 119 | void _createCountIterationStateSet(size_t points); 120 | void _createFirstFindQuantileIntervalsStateSet( 121 | const std::vector& quantiles); 122 | void _createFindQuantileIntervalsStateSet( 123 | const std::vector& quantiles); 124 | void _createFinalReprojectionStateSet(unsigned int points); 125 | 126 | void _updateMinMaxCalculationPrograms(const ProgramMap& extraShaders); 127 | void _updateFirstCountPrograms(const ProgramMap& extraShaders); 128 | void _updateCountIterationPrograms(const ProgramMap& extraShaders, 129 | size_t points); 130 | }; 131 | } 132 | } 133 | } 134 | #endif 135 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/blend.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #extension GL_EXT_gpu_shader4 : enable 24 | #extension GL_ARB_draw_buffers : enable 25 | #extension GL_ARB_texture_rectangle : enable 26 | #extension GL_ARB_shader_bit_encoding : enable 27 | 28 | $DEFINES 29 | // External defines: 30 | // SLICES x 31 | 32 | /* This shader is used alternativly for the front and back layers */ 33 | 34 | uniform sampler2DRect colorTextures[(SLICES + 3) / 4]; 35 | 36 | void main(void) 37 | { 38 | for (int i = 0; i < (SLICES + 3) / 4; ++i) 39 | { 40 | /** \todo Avoid 4 channel texture reads when possible */ 41 | vec4 layerColors = texture2DRect(colorTextures[i], gl_FragCoord.xy); 42 | for (int j = 0; j < 4 && j + (i * 4) < SLICES; ++j) 43 | { 44 | int color = int(floatBitsToInt(layerColors[j])); 45 | int index = i * 4 + j; 46 | if (color != int(0xFF800000)) 47 | { 48 | // We use unsigned numbers because otherwise >> operator carries 49 | // the sign bit to the right. 50 | unsigned int alpha = unsigned int(color) >> 24; 51 | if (alpha >= 129u) 52 | alpha -= 2; 53 | vec4 rgba = 54 | vec4(float((color >> 16) & 255) / 255.0, 55 | float((color >> 8) & 255) / 255.0, 56 | float(color & 255) / 255.0, float(alpha) / 253.0); 57 | gl_FragData[index] = vec4(rgba.rgb * rgba.a, rgba.a); 58 | } 59 | else 60 | { 61 | gl_FragData[index] = vec4(0); 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/depth_partition/check_partition.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | #extension GL_ARB_texture_rectangle : enable 25 | 26 | $DEFINES 27 | //#define SLICES n 28 | //#define ADJUST_QUANTILES_WITH_ALPHA? 29 | 30 | #ifdef ADJUST_QUANTILES_WITH_ALPHA 31 | #define POINTS SLICES 32 | #else 33 | #define POINTS SLICES - 1 34 | #endif 35 | #define SPLITS SLICES - 1 36 | 37 | #if POINTS >= 1 38 | uniform sampler2DRect depthPartitionTextures[(POINTS + 3) / 4]; 39 | #endif 40 | 41 | /* Define for the return type and the value for the discard case. */ 42 | #if SPLITS == 1 43 | #define RETURN_TYPE float 44 | #elif SPLITS == 2 45 | #define RETURN_TYPE vec2 46 | #elif SPLITS == 3 47 | #define RETURN_TYPE vec3 48 | #elif SPLITS == 4 49 | #define RETURN_TYPE vec4 50 | #else 51 | #define RETURN_TYPE bool 52 | #endif 53 | 54 | /* Defines for the vector type and swizzle declarations for the last texture 55 | fetch and the return value. */ 56 | /* Macro arithmetics are working funny */ 57 | #if POINTS == 1 || POINTS == 5 58 | #define CHANNELS r 59 | #elif POINTS == 2 || POINTS == 6 60 | #define CHANNELS rg 61 | #elif POINTS == 3 || POINTS == 7 62 | #define CHANNELS rgb 63 | #elif POINTS == 4 || POINTS == 8 64 | #define CHANNELS rgba 65 | #elif POINTS > 8 66 | #error "Unsupported number of split points" 67 | #endif 68 | 69 | #if SPLITS > 4 70 | /* Arrays seems to be passed by value and not be reference (?) so we need 71 | to use a global variable in this cases. */ 72 | float splitPoints[SPLITS]; 73 | #endif 74 | /* Function prototype. */ 75 | RETURN_TYPE checkPartitionAndGetSplitPoints(const vec2 coord, const float depth) 76 | { 77 | /* Reading depth partition textures. */ 78 | #if POINTS > 4 79 | vec4 points[2]; 80 | points[0] = texture2DRect(depthPartitionTextures[0], coord).rgba; 81 | points[1].CHANNELS = 82 | texture2DRect(depthPartitionTextures[1], coord).CHANNELS; 83 | #elif POINTS != 0 84 | vec4 points[1]; 85 | points[0].CHANNELS = 86 | texture2DRect(depthPartitionTextures[0], coord).CHANNELS; 87 | #endif 88 | 89 | #if defined ADJUST_QUANTILES_WITH_ALPHA 90 | #if POINTS < 5 91 | if (depth > points[0][POINTS - 1]) 92 | discard; 93 | #else 94 | if (depth > points[1][POINTS - 5]) 95 | discard; 96 | #endif 97 | #endif 98 | 99 | #if SPLITS == 0 100 | return true; 101 | #elif SPLITS == 1 102 | return points[0].r; 103 | #elif SPLITS == 2 104 | return points[0].rg; 105 | #elif SPLITS == 3 106 | return points[0].rgb; 107 | #elif SPLITS == 4 108 | return points[0]; 109 | #else 110 | for (int i = 0; i < 4; ++i) 111 | splitPoints[i] = points[0][i]; 112 | for (int i = 4; i < SPLITS; ++i) 113 | splitPoints[i] = points[1][i - 4]; 114 | return true; 115 | #endif 116 | } 117 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/depth_partition/iterative/alpha_adjusted_first_find_quantiles_interval.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | #extension GL_ARB_texture_rectangle : enable 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | //#define POINTS x 28 | //#define OPACITY_THRESHOLD x 29 | //#define ROUND round or #define ROUND 30 | $DEFINES 31 | $OUT_BUFFERS_DECLARATION 32 | 33 | #ifndef OPACITY_THRESHOLD 34 | #define OPACITY_THRESHOLD 0.99 35 | #endif 36 | 37 | uniform sampler2DRect countTextures[8]; 38 | uniform float quantiles[POINTS]; 39 | 40 | int approx_required_layers(float alphas[8], float counts[24]) 41 | { 42 | float totals[8]; 43 | for (int i = 0; i < 8; i++) 44 | { 45 | totals[i] = 0; 46 | for (int j = i * 3; j < i * 3 + 3; ++j) 47 | totals[i] += counts[j]; 48 | } 49 | 50 | for (int i = 1; i < 8; ++i) 51 | { 52 | alphas[i] = alphas[i - 1] + (1 - alphas[i - 1]) * alphas[i]; 53 | } 54 | 55 | int layers = 0; 56 | int k = 0; 57 | bool done = false; 58 | for (int i = 0; i < 8; ++i) 59 | { 60 | if (!done) 61 | { 62 | layers += int(totals[i]); 63 | k = i * 3; 64 | } 65 | done = alphas[i] > OPACITY_THRESHOLD; 66 | } 67 | /* Saving the number of interval where alpha has gone above the opacity 68 | threshold */ 69 | #if POINTS > 4 70 | gl_FragData[4][1] = k + 3; 71 | #else 72 | gl_FragData[2][1] = k + 3; 73 | #endif 74 | return layers; 75 | } 76 | 77 | void main() 78 | { 79 | float counts[24]; 80 | float alphas[8]; 81 | float total = 0; 82 | for (int i = 0; i < 8; ++i) 83 | { 84 | vec4 t = texture2DRect(countTextures[i], gl_FragCoord.xy).rgba; 85 | for (int j = 0; j < 3; ++j) 86 | { 87 | total += t[j]; 88 | counts[i * 3 + j] = t[j]; 89 | } 90 | alphas[i] = t.a; 91 | } 92 | if (total == 0) 93 | discard; 94 | total = approx_required_layers(alphas, counts); 95 | 96 | float target_counts[8]; 97 | bool written[8]; 98 | for (int i = 0; i < 8 && i < POINTS; ++i) 99 | { 100 | target_counts[i] = ROUND(quantiles[i] * total); 101 | written[i] = false; 102 | } 103 | float left_accumulation = 0; 104 | /* Loop over all the histogram intervals */ 105 | for (int i = 0; i < 24; ++i) 106 | { 107 | float current_count = left_accumulation + counts[i]; 108 | #if POINTS < 5 109 | /* Checking which quantiles are found in the current index */ 110 | for (int j = 0; j < POINTS; ++j) 111 | { 112 | if (!written[j] && current_count > target_counts[j]) 113 | { 114 | gl_FragData[0][j] = float(i); 115 | gl_FragData[1][j] = left_accumulation; 116 | written[j] = true; 117 | } 118 | } 119 | #else 120 | for (int j = 0; j < 4; ++j) 121 | { 122 | if (!written[j] && current_count > target_counts[j]) 123 | { 124 | gl_FragData[0][j] = float(i); 125 | gl_FragData[1][j] = left_accumulation; 126 | written[j] = true; 127 | } 128 | } 129 | for (int j = 4; j < POINTS; ++j) 130 | { 131 | if (!written[j] && current_count > target_counts[j]) 132 | { 133 | gl_FragData[2][j - 4] = float(i); 134 | gl_FragData[3][j - 4] = left_accumulation; 135 | written[j] = true; 136 | } 137 | } 138 | #endif 139 | left_accumulation = current_count; 140 | } 141 | 142 | /* Saving the total per pixel fragment count in its texture for 143 | further access */ 144 | #if POINTS > 4 145 | const int buff = 4; 146 | #else 147 | const int buff = 2; 148 | #endif 149 | gl_FragData[buff].r = total; 150 | #if POINTS > 8 151 | #error "Unsupported number of simultaneous quantiles" 152 | #endif 153 | } 154 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/depth_partition/iterative/count_iteration.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #extension GL_ARB_texture_rectangle : enable 24 | 25 | uniform float proj33; 26 | uniform float proj34; 27 | float unproject(float x) 28 | { 29 | return -proj34 / (proj33 + 2.0 * x - 1.0); 30 | } 31 | float reproject(float x) 32 | { 33 | return 0.5 * (1.0 - proj33 - proj34 / x); 34 | } 35 | 36 | $DEFINES 37 | $OUT_BUFFERS_DECLARATION 38 | 39 | uniform sampler2DRect minMaxTexture; 40 | uniform sampler2DRect codedPreviousIntervalsTextures[(POINTS + 3) / 4]; 41 | 42 | int findInterval(float depth, float abs_min, float abs_max, 43 | int previous_intervals); 44 | 45 | float fragmentDepth(); 46 | 47 | void main() 48 | { 49 | vec2 minMax = texture2DRect(minMaxTexture, gl_FragCoord.xy).rg; 50 | float min = -minMax[0]; 51 | float max = minMax[1]; 52 | 53 | float depth = -unproject(fragmentDepth()); 54 | 55 | int codes[8]; 56 | vec4 code1 = 57 | texture2DRect(codedPreviousIntervalsTextures[0], gl_FragCoord.xy).rgba; 58 | for (int i = 0; i < 4; ++i) 59 | codes[i] = int(code1[i]); 60 | #if POINTS > 4 61 | vec4 code2 = 62 | texture2DRect(codedPreviousIntervalsTextures[1], gl_FragCoord.xy).rgba; 63 | for (int i = 0; i < 4; ++i) 64 | codes[4 + i] = int(code2[i]); 65 | #endif 66 | 67 | /* Writing default output values. */ 68 | #if POINTS > 4 || !defined DOUBLE_WIDTH 69 | for (int i = 0; i < POINTS; ++i) 70 | gl_FragData[i] = vec4(0); 71 | #else 72 | for (int i = 0; i < POINTS * 2; ++i) 73 | gl_FragData[i] = vec4(0); 74 | #endif 75 | 76 | #if POINTS > 4 || !defined DOUBLE_WIDTH 77 | for (int i = 0; i < POINTS; ++i) 78 | { 79 | int t = findInterval(depth, min, max, codes[i]); 80 | if (t != -1) 81 | gl_FragData[i] = vec4(float(t == 0), float(t == 1), float(t == 2), 82 | float(t == 3)); 83 | } 84 | #else 85 | for (int i = 0; i < POINTS; ++i) 86 | { 87 | int t = findInterval(depth, min, max, codes[i]); 88 | if (t > 3) 89 | { 90 | vec4 output = vec4(t == 4, t == 5, t == 6, t == 7); 91 | gl_FragData[i * 2 + 1] = output; 92 | } 93 | else if (t != -1) 94 | { 95 | vec4 output = vec4(t == 0, t == 1, t == 2, t == 3); 96 | gl_FragData[i * 2] = output; 97 | } 98 | } 99 | #endif 100 | 101 | #if POINTS > 8 102 | #error "Unsupported number of simultaneous quantiles" 103 | #endif 104 | } 105 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/depth_partition/iterative/find_interval.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #extension GL_EXT_gpu_shader4 : enable 24 | 25 | uniform int iteration; 26 | 27 | //#define DOUBLE_WIDTH ? 28 | //#define FIRST_INTERVAL_SEARCH_WIDTH x 29 | $DEFINES 30 | 31 | int findInterval(float depth, float abs_min, float abs_max, 32 | int previous_intervals) 33 | { 34 | if (depth > abs_max || depth <= abs_min) 35 | return -1; 36 | #if DOUBLE_WIDTH 37 | const float width_reduction = 0.125; // 1 / 8.0 38 | const int shift = 3; 39 | const int intervals = 8; 40 | int mask = 0x07; 41 | #else 42 | const float width_reduction = 0.25; // 1 / 4.0; 43 | const int shift = 2; 44 | const int intervals = 4; 45 | int mask = 0x03; 46 | #endif 47 | 48 | float width = (abs_max - abs_min) * FIRST_INTERVAL_SEARCH_WIDTH; 49 | int interval_index = previous_intervals & 0x1F; /* 5 lower bits */ 50 | float start = abs_min + width * float(interval_index); 51 | 52 | /* At each iteration we take the next 2-3 bits (depending on the number 53 | of intervals per point for iterations after the first) from the 54 | search code. 55 | These bits represent the interval in which a particular quantile point 56 | has been classified in previous iterations. Using that index we 57 | advance the starting point for the next interval set and divide the 58 | width according by the number of intervals. */ 59 | previous_intervals >>= 5; 60 | for (int i = 1; i < iteration; ++i) 61 | { 62 | width *= width_reduction; 63 | int interval_index = previous_intervals & mask; 64 | start += width * float(interval_index); 65 | previous_intervals >>= shift; 66 | } 67 | /* Finally we compute the interval width for the current search range. */ 68 | width *= width_reduction; 69 | 70 | /* Due to the limited precision, there's a chance that fragment depths 71 | fall onto one of the interval edges. It's been checked that with 72 | certain quantile quantities (0, 1) this can cause redundant count 73 | of points as both on the left of the range and on the first interval. 74 | There's little that can be done except from re-doing the left count 75 | for each range, however it requires either extra buffer memory or an 76 | additional pass, therefore it's not an option. 77 | Doing depth <= start seems to behave worse, and that's the only reason 78 | for considering an open search interval. */ 79 | if (depth < start) 80 | /* This is a fragment at the left of the current search range. */ 81 | return -1; 82 | /* Conversion from float to int drops the fractional part. */ 83 | int interval = int((depth - start) / width); 84 | if (interval >= intervals) 85 | return -1; 86 | return interval; 87 | } 88 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/depth_partition/iterative/first_count.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | #extension GL_ARB_texture_rectangle : enable 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | uniform float proj33; 28 | uniform float proj34; 29 | float unproject(float x) 30 | { 31 | return -proj34 / (proj33 + 2.0 * x - 1.0); 32 | } 33 | float reproject(float x) 34 | { 35 | return 0.5 * (1.0 - proj33 - proj34 / x); 36 | } 37 | 38 | vec4 gl_FragData[8]; 39 | 40 | uniform sampler2DRect minMaxTexture; 41 | float fragmentDepth(); 42 | 43 | void main() 44 | { 45 | /* We count unprojected depth values */ 46 | float depth = -unproject(fragmentDepth()); 47 | vec2 minMax = texture2DRect(minMaxTexture, gl_FragCoord.xy).rg; 48 | float abs_min = -minMax.r; 49 | float abs_max = minMax.g; 50 | 51 | float width = (abs_max - abs_min) * 0.03125; 52 | int interval = min(int((depth - abs_min) / width), 31); 53 | 54 | int buff = interval >> 2; 55 | int index = interval & 3; 56 | 57 | /* As odd as it seems this code is used to prevent the Nvidia compiler 58 | to reorder the assembly code in such a way that next shaders obtain 59 | different results for the same calculations. What we really want to 60 | avoid is that some shaders use RCP and others DIV to calculate 61 | unproject. We try to stay to DIV since it looks more accurate than RCP. 62 | (the assembler instruction names follow NVidia conventions). */ 63 | if (abs_min > depth) 64 | { 65 | buff = interval / 4; 66 | index = interval % 4; 67 | } 68 | vec4 color = vec4(index == 0, index == 1, index == 2, index == 3); 69 | 70 | for (int i = 0; i < 8; ++i) 71 | gl_FragData[i] = vec4(0); 72 | gl_FragData[buff] = color; 73 | gl_FragData[7].w = 1.0; 74 | } 75 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/depth_partition/iterative/first_count_with_alpha.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | #extension GL_ARB_texture_rectangle : enable 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | $DEFINES 28 | 29 | uniform float proj33; 30 | uniform float proj34; 31 | float unproject(float x) 32 | { 33 | return -proj34 / (proj33 + 2.0 * x - 1.0); 34 | } 35 | float reproject(float x) 36 | { 37 | return 0.5 * (1.0 - proj33 - proj34 / x); 38 | } 39 | 40 | vec4 gl_FragData[8]; 41 | 42 | uniform sampler2DRect minMaxTexture; 43 | float fragmentDepth(); 44 | float fragmentAlpha(); 45 | 46 | void main() 47 | { 48 | /* We count unprojected depth values */ 49 | float depth = -unproject(fragmentDepth()); 50 | vec2 minMax = texture2DRect(minMaxTexture, gl_FragCoord.xy).rg; 51 | float abs_min = -minMax.r; 52 | float abs_max = minMax.g; 53 | 54 | float width = (abs_max - abs_min) * 0.041666666666666664; 55 | int interval = min(int((depth - abs_min) / width), 24); 56 | interval += interval / 3; 57 | int buff = interval >> 2; 58 | int index = interval & 3; 59 | 60 | /* As odd as it seems this code is used to prevent the Nvidia compiler 61 | to reorder the assembly code in such a way that next shaders obtain 62 | different results for the same calculations. What we really want to 63 | avoid is that some shaders use RCP and others DIV to calculate 64 | unproject. We try to stay to DIV since it looks more accurate than RCP. 65 | (the assembler instruction names follow NVidia conventions). */ 66 | if (abs_min > depth) 67 | { 68 | buff = interval / 4; 69 | index = interval % 4; 70 | } 71 | vec4 color = vec4(index == 0, index == 1, index == 2, fragmentAlpha()); 72 | 73 | for (int i = 0; i < 8; ++i) 74 | gl_FragData[i] = vec4(0); 75 | gl_FragData[buff] = color; 76 | gl_FragData[7].w = 1.0; 77 | } 78 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/depth_partition/iterative/first_find_quantiles_interval.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | #extension GL_ARB_texture_rectangle : enable 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | //#define POINTS x 28 | //#define OPACITY_THRESHOLD x 29 | //#define ROUND round or #define ROUND 30 | $DEFINES 31 | $OUT_BUFFERS_DECLARATION 32 | 33 | #ifndef OPACITY_THRESHOLD 34 | #define OPACITY_THRESHOLD 0.99 35 | #endif 36 | 37 | uniform sampler2DRect countTextures[8]; 38 | uniform float quantiles[POINTS]; 39 | 40 | void main() 41 | { 42 | float counts[32]; 43 | float accum = 0; 44 | float total; 45 | for (int i = 0; i < 8; ++i) 46 | { 47 | vec4 t = texture2DRect(countTextures[i], gl_FragCoord.xy).rgba; 48 | for (int j = 0; j < 4; ++j) 49 | counts[i * 4 + j] = t[j]; 50 | } 51 | total = counts[31]; 52 | 53 | if (total == 0) 54 | discard; 55 | 56 | float target_counts[8]; 57 | bool written[8]; 58 | for (int i = 0; i < 8 && i < POINTS; ++i) 59 | { 60 | target_counts[i] = ROUND(quantiles[i] * total); 61 | written[i] = false; 62 | } 63 | float left_accumulation = 0; 64 | for (int i = 0; i < 32; ++i) 65 | { 66 | float current_count = left_accumulation + counts[i]; 67 | #if POINTS < 5 68 | for (int j = 0; j < POINTS; ++j) 69 | { 70 | if (!written[j] && current_count > target_counts[j]) 71 | { 72 | gl_FragData[0][j] = float(i); 73 | gl_FragData[1][j] = left_accumulation; 74 | written[j] = true; 75 | } 76 | } 77 | #else 78 | for (int j = 0; j < 4; ++j) 79 | { 80 | if (!written[j] && current_count > target_counts[j]) 81 | { 82 | gl_FragData[0][j] = float(i); 83 | gl_FragData[1][j] = left_accumulation; 84 | written[j] = true; 85 | } 86 | } 87 | for (int j = 4; j < POINTS; ++j) 88 | { 89 | if (!written[j] && current_count > target_counts[j]) 90 | { 91 | gl_FragData[2][j - 4] = float(i); 92 | gl_FragData[3][j - 4] = left_accumulation; 93 | written[j] = true; 94 | } 95 | } 96 | #endif 97 | left_accumulation = current_count; 98 | } 99 | 100 | /* Saving the total per pixel fragment count in its texture for 101 | further access */ 102 | #if POINTS > 4 103 | const int buff = 4; 104 | #else 105 | const int buff = 2; 106 | #endif 107 | gl_FragData[buff].r = total; 108 | 109 | #if POINTS > 8 110 | #error "Unsupported number of simultaneous quantiles" 111 | #endif 112 | } 113 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/depth_partition/iterative/minmax.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #extension GL_ARB_texture_rectangle : enable 24 | 25 | $DEFINES 26 | 27 | uniform float proj33; 28 | uniform float proj34; 29 | float unproject(float x) 30 | { 31 | return -proj34 / (proj33 + 2.0 * x - 1.0); 32 | } 33 | float reproject(float x) 34 | { 35 | return 0.5 * (1.0 - proj33 - proj34 / x); 36 | } 37 | 38 | float fragmentDepth(); 39 | 40 | void main() 41 | { 42 | /* We want to compute: 43 | min = unproject(max_i{x_i} + delta) 44 | max = unproject(min_i{x_i} - delta) 45 | (Note that unproject is monotonically 46 | decreasing in [0,1] -> [-near,-far], that's the reason for using min_i 47 | for max and vice versa). 48 | However that would require two passes, so instead we try: 49 | min = min_i{unproject(x_i + delta)} 50 | max = max_i{unproject(x_i - delta)} */ 51 | 52 | /* This code is being optimized to use RCP instead of DIV for reproject. 53 | The end result is that some values computed in the next steps' shaders 54 | lie outside the computed range. 55 | Forcing cgc to use the best precision is not working, so the current 56 | workaround is shifting the values with a reasonable offset for our 57 | scene dimensions. */ 58 | float depth = -unproject(fragmentDepth()); 59 | #ifdef HALF_FLOAT_MINMAX 60 | float min = depth - 1; 61 | float max = depth + 1; 62 | #else 63 | float min = depth - 0.005; 64 | float max = depth + 0.005; 65 | #endif 66 | gl_FragColor.rg = vec2(-min, max); 67 | } 68 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/depth_partition/profile.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | #extension GL_ARB_texture_rectangle : enable 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | $DEFINES 28 | #define SLICES $SLICES 29 | 30 | uniform float proj33; 31 | uniform float proj34; 32 | 33 | vec4 gl_FragData[(SLICES - 1) / 4 + 1]; 34 | 35 | float unproject(float x) 36 | { 37 | return -proj34 / (proj33 + 2.0 * x - 1.0); 38 | } 39 | 40 | #if SLICES == 2 41 | #define RETURN_TYPE float 42 | #elif SLICES == 3 43 | #define RETURN_TYPE vec2 44 | #elif SLICES == 4 45 | #define RETURN_TYPE vec3 46 | #elif SLICES == 5 47 | #define RETURN_TYPE vec4 48 | #else 49 | #define RETURN_TYPE bool 50 | #endif 51 | #if SLICES > 5 52 | /* Arrays seems to be passed by value and not be reference (?) so we need 53 | to use a global variable in this cases. */ 54 | extern float splitPoints[SLICES - 1]; 55 | #endif 56 | RETURN_TYPE checkPartitionAndGetSplitPoints(const vec2 coord, float depth); 57 | 58 | float fragmentDepth(); 59 | 60 | void main() 61 | { 62 | vec2 coord = gl_FragCoord.xy; 63 | 64 | #ifdef UNPROJECT_DEPTH 65 | float depth = -unproject(fragmentDepth()); 66 | #else 67 | float depth = fragmentDepth(); 68 | #endif 69 | 70 | #if SLICES == 1 71 | checkPartitionAndGetSplitPoints(coord, depth); 72 | gl_FragData[0][0] = 1.0; 73 | #else 74 | #if SLICES == 2 75 | float splitPoints[1]; 76 | splitPoints[0] = checkPartitionAndGetSplitPoints(coord, depth); 77 | #elif SLICES < 6 78 | RETURN_TYPE splitPoints = checkPartitionAndGetSplitPoints(coord, depth); 79 | #else 80 | checkPartitionAndGetSplitPoints(coord, depth); 81 | #endif 82 | for (int i = 0; i < SLICES - 1; ++i) 83 | { 84 | if (depth < splitPoints[i]) 85 | { 86 | gl_FragData[i / 4][i % 4] = 1.0; 87 | return; 88 | } 89 | } 90 | gl_FragData[(SLICES - 1) / 4][(SLICES - 1) % 4] = 1.0; 91 | #endif 92 | } 93 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/final_pass.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #extension GL_ARB_draw_buffers : enable 24 | #extension GL_ARB_texture_rectangle : enable 25 | 26 | $DEFINES 27 | 28 | uniform sampler2DRect blendBuffers[SLICES * 2]; 29 | 30 | void main(void) 31 | { 32 | /* Reading each layer colors */ 33 | gl_FragColor = vec4(0.0); 34 | for (int i = 0; i < SLICES * 2; ++i) 35 | { 36 | vec4 color = texture2DRect(blendBuffers[i], gl_FragCoord.xy); 37 | /* 1 - buffer0.a contains Pi(1 - a_i) for all layers in the first 38 | slice. Front to back blending of slices is associative, so we blend 39 | the composition of layers for slice 1 and slice 2 using the front 40 | to back formula, the result with the next slice and so on so 41 | forth.*/ 42 | gl_FragColor += color * (1.0 - gl_FragColor.a); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/multilayer/first_pass.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | #extension GL_EXT_gpu_shader4 : enable 25 | 26 | $DEFINES 27 | #define SLICES $SLICES 28 | #define DEPTH_BUFFERS ((SLICES + 1) / 2) 29 | 30 | uniform float proj33; 31 | uniform float proj34; 32 | float unproject(float x) 33 | { 34 | return -proj34 / (proj33 + 2.0 * x - 1.0); 35 | } 36 | 37 | #if SLICES == 2 38 | #define RETURN_TYPE float 39 | #elif SLICES == 3 40 | #define RETURN_TYPE vec2 41 | #elif SLICES == 4 42 | #define RETURN_TYPE vec3 43 | #elif SLICES == 5 44 | #define RETURN_TYPE vec4 45 | #else 46 | #define RETURN_TYPE bool 47 | #endif 48 | 49 | float fragmentDepth(); 50 | vec4 shadeFragment(); 51 | #if SLICES > 5 52 | extern float splitPoints[SLICES - 1]; 53 | #endif 54 | RETURN_TYPE checkPartitionAndGetSplitPoints(const vec2 coord, 55 | const float depth); 56 | 57 | vec4 gl_FragData[(SLICES + 1) / 2]; 58 | 59 | void main(void) 60 | { 61 | vec2 coord = gl_FragCoord.xy; 62 | 63 | #ifdef UNPROJECT_DEPTH 64 | float depth = -unproject(fragmentDepth()); 65 | #else 66 | float depth = fragmentDepth(); 67 | #endif 68 | 69 | #if SLICES == 2 70 | float splitPoint = checkPartitionAndGetSplitPoints(coord, depth); 71 | #elif SLICES < 6 72 | RETURN_TYPE splitPoints = checkPartitionAndGetSplitPoints(coord, depth); 73 | #else 74 | checkPartitionAndGetSplitPoints(coord, depth); 75 | #endif 76 | 77 | vec2 depths = vec2(-depth, depth); 78 | #if SLICES == 1 79 | gl_FragData[0].xy = depths; 80 | #elif SLICES == 2 81 | if (depth < splitPoint) 82 | { 83 | gl_FragData[0].xy = depths; 84 | gl_FragData[0].zw = vec2(-1.0, 0); 85 | } 86 | else 87 | { 88 | gl_FragData[0].xy = vec2(-1.0, 0); 89 | gl_FragData[0].zw = depths; 90 | } 91 | #elif SLICES > 2 92 | for (int i = 0; i < DEPTH_BUFFERS; ++i) 93 | gl_FragData[i] = vec4(-1.0, 0.0, -1.0, 0.0); 94 | 95 | for (int i = 0; i < SLICES; ++i) 96 | { 97 | if (i == SLICES - 1 || depth < splitPoints[min(i, SLICES - 2)]) 98 | { 99 | if (i % 2 == 0) 100 | gl_FragData[i / 2].xy = depths; 101 | else 102 | gl_FragData[i / 2].zw = depths; 103 | return; 104 | } 105 | } 106 | #endif 107 | } 108 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL2/simple/peel.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | // XXX 24 | //#define ALPHA_TEST_DISCARD 25 | 26 | #extension GL_ARB_draw_buffers : enable 27 | #extension GL_ARB_texture_rectangle : enable 28 | 29 | uniform sampler2DRect depthBuffer; 30 | 31 | /* Buffer where the front layer of the front half is being blended */ 32 | uniform sampler2DRect blendedBuffer; 33 | 34 | uniform vec2 offset; 35 | uniform vec2 scaling; 36 | 37 | vec4 shadeFragment(); 38 | 39 | float fragmentDepth(); 40 | 41 | uniform float proj33; 42 | uniform float proj34; 43 | float unproject(float x) 44 | { 45 | return -proj34 / (proj33 + 2.0 * x - 1.0); 46 | } 47 | 48 | void main(void) 49 | { 50 | float frontDepth = -texture2DRect(depthBuffer, gl_FragCoord.xy).r; 51 | float depth = fragmentDepth(); 52 | 53 | /* Checking if the opacity of frontmost layer is above the discard 54 | threshold. */ 55 | #ifdef ALPHA_TEST_DISCARD 56 | vec2 coord = gl_FragCoord.xy * scaling + offset; 57 | if (textureRect(blendedBuffer, coord).a >= 0.95) 58 | discard; 59 | #endif 60 | 61 | if (depth < frontDepth) 62 | discard; 63 | if (depth == frontDepth) 64 | { 65 | /* Shading fragment peeled front to back */ 66 | vec4 color = shadeFragment(); 67 | color.rgb *= color.a; 68 | gl_FragData[1] = color; 69 | gl_FragData[0].r = -1.0; 70 | } 71 | else 72 | { 73 | /* Peeling fragment */ 74 | gl_FragData[1] = vec4(0); 75 | gl_FragData[0].r = -depth; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/fragment_list/fragment_count_filtering.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | uniform usampler2DRect fragmentCounts; 28 | uniform uvec2 fragmentCountRange; 29 | /* The lower left corner of the camera viewport */ 30 | uniform vec2 corner; 31 | 32 | void main(void) 33 | { 34 | const vec2 coord = gl_FragCoord.xy - corner; 35 | const unsigned int count = texture(fragmentCounts, coord).r; 36 | if (count < fragmentCountRange[0] || count > fragmentCountRange[1]) 37 | discard; 38 | } 39 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/fragment_list/save_fragments.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | $DEFINES 28 | 29 | layout(size1x32) restrict uniform uimage2DRect listHead; 30 | layout(size1x32) restrict uniform uimageBuffer fragmentBuffer; 31 | layout(size1x32) restrict uniform uimage2DRect fragmentCounts; 32 | 33 | layout(binding = 0) uniform atomic_uint counter; 34 | 35 | #ifdef USE_ALPHA_CUTOFF 36 | layout(size1x32) restrict uniform uimage2DRect depthTranspBuffer; 37 | #endif 38 | 39 | uniform float minTransparency; 40 | 41 | float fragmentDepth(); 42 | vec4 shadeFragment(); 43 | 44 | void main(void) 45 | { 46 | /* This has to be done before writing anything, as the client code in 47 | fragmentDepth and shadeFragment may discard the fragmet. */ 48 | const float depth = fragmentDepth(); 49 | 50 | #ifdef USE_ALPHA_CUTOFF 51 | /* Checking the depth to decide if the fragment must be discarded. */ 52 | int i = 0; 53 | uint depthTransp = imageLoad(depthTranspBuffer, ivec2(gl_FragCoord.xy)).r; 54 | uint maxDepthi = depthTransp & 0x00FFFFFF; 55 | uint transparency = depthTransp >> 24; 56 | const uint depthi = uint(floor(depth * 0x00FFFFFF)); 57 | 58 | if (transparency <= minTransparency * 255.0 && depthi > maxDepthi) 59 | discard; 60 | #endif 61 | 62 | /* Color clamping needed to ensure that each channel is within [0, 1]. */ 63 | const vec4 color = clamp(shadeFragment(), vec4(0.0), vec4(1.0)); 64 | 65 | #ifdef USE_ALPHA_CUTOFF 66 | /* The formula below is an approximation of the more ideal t = t (1 - a) 67 | where t and a are floats in [0, 1]. The most conservative approach would 68 | be without 0.25, but the resulting formula converges to 0 very badly. 69 | The biased rounding provided by adding 0.25 overestimates the 70 | transparency in general, so it's still quite conservative while it 71 | converges much faster that using floor alone. */ 72 | transparency -= uint(floor(transparency * color.a + 0.25)); 73 | 74 | if (maxDepthi < depthi) 75 | maxDepthi = depthi; 76 | /* There's an anavoidable race condition here, the min operation tries 77 | to minimize the side effects. In the worst case scenario, which is 78 | rendering a primitive which has lots of overlapping triangles on the 79 | z-axis, the convergence of the transparency to 0 will happen much 80 | more slowly. */ 81 | imageAtomicMin(depthTranspBuffer, ivec2(gl_FragCoord.xy), 82 | maxDepthi | transparency << 24); 83 | #endif 84 | 85 | /* Taking a new fragment from the fragment buffer. */ 86 | uint index = atomicCounterIncrement(counter); 87 | 88 | /* Linking this fragment with the previous one. */ 89 | uint next = imageAtomicExchange(listHead, ivec2(gl_FragCoord.xy), index); 90 | 91 | /* Storing the fragment info. 92 | Alpha channel goes without premultiplication. */ 93 | const int offset = int(index) * 3; 94 | imageStore(fragmentBuffer, offset, uvec4(next)); 95 | const uint idepth = floatBitsToUint(depth); 96 | imageStore(fragmentBuffer, offset + 1, uvec4(idepth)); 97 | const uint icolor = uint(color[0] * 255) + (uint(color[1] * 255) << 8u) + 98 | (uint(color[2] * 255) << 16u) + 99 | (uint(color[3] * 255) << 24u); 100 | imageStore(fragmentBuffer, offset + 2, uvec4(icolor)); 101 | 102 | /* Increasing the fragment count */ 103 | imageAtomicAdd(fragmentCounts, ivec2(gl_FragCoord.xy), 1u); 104 | } 105 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/depth_partition/check_partition.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | #extension GL_ARB_texture_rectangle : enable 25 | 26 | $DEFINES 27 | //#define SLICES n 28 | //#define ADJUST_QUANTILES_WITH_ALPHA? 29 | 30 | #if defined ADJUST_QUANTILES_WITH_ALPHA 31 | #define POINTS SLICES 32 | #else 33 | #define POINTS SLICES - 1 34 | #endif 35 | #define SPLITS SLICES - 1 36 | 37 | #if POINTS >= 1 38 | uniform sampler2DRect depthPartitionTextures[(POINTS + 3) / 4]; 39 | #endif 40 | 41 | /* Define for the return type and the value for the discard case. */ 42 | #if SPLITS == 1 43 | #define RETURN_TYPE float 44 | #elif SPLITS == 2 45 | #define RETURN_TYPE vec2 46 | #elif SPLITS == 3 47 | #define RETURN_TYPE vec3 48 | #elif SPLITS == 4 49 | #define RETURN_TYPE vec4 50 | #else 51 | #define RETURN_TYPE bool 52 | #endif 53 | 54 | /* Defines for the vector type and swizzle declarations for the last texture 55 | fetch and the return value. */ 56 | /* Macro arithmetics are working funny */ 57 | #if POINTS == 1 || POINTS == 5 58 | #define CHANNELS r 59 | #elif POINTS == 2 || POINTS == 6 60 | #define CHANNELS rg 61 | #elif POINTS == 3 || POINTS == 7 62 | #define CHANNELS rgb 63 | #elif POINTS == 4 || POINTS == 8 64 | #define CHANNELS rgba 65 | #elif POINTS > 8 66 | #error "Unsupported number of split points" 67 | #endif 68 | 69 | #if SPLITS > 4 70 | /* Arrays seems to be passed by value and not be reference (?) so we need 71 | to use a global variable in this cases. */ 72 | float splitPoints[SPLITS]; 73 | #endif 74 | /* Function prototype. */ 75 | RETURN_TYPE checkPartitionAndGetSplitPoints(const vec2 coord, float depth) 76 | { 77 | /* Reading depth partition textures. */ 78 | #if POINTS > 4 79 | vec4 points[2]; 80 | points[0] = texture2DRect(depthPartitionTextures[0], coord).rgba; 81 | points[1].CHANNELS = 82 | texture2DRect(depthPartitionTextures[1], coord).CHANNELS; 83 | #elif POINTS != 0 84 | vec4 points[1]; 85 | points[0].CHANNELS = 86 | texture2DRect(depthPartitionTextures[0], coord).CHANNELS; 87 | #endif 88 | 89 | #if defined ADJUST_QUANTILES_WITH_ALPHA 90 | 91 | #if POINTS < 5 92 | if (depth > points[0][POINTS - 1]) 93 | discard; 94 | #else 95 | if (depth > points[1][POINTS - 5]) 96 | discard; 97 | #endif 98 | 99 | #if SPLITS == 0 100 | return true; 101 | #elif SPLITS == 1 102 | return points[0][0]; 103 | #elif SPLITS == 2 104 | return points[0].rg; 105 | #elif SPLITS == 3 106 | return points[0].rgb; 107 | #elif SPLITS == 4 108 | return points[0]; 109 | #else 110 | for (int i = 0; i < 4; ++i) 111 | splitPoints[i] = points[0][i]; 112 | for (int i = 4; i < SPLITS; ++i) 113 | splitPoints[i] = points[1][i - 4]; 114 | return true; 115 | #endif 116 | 117 | #else 118 | 119 | #if SPLITS == 0 120 | return true; 121 | #elif SPLITS == 1 122 | return points[0].r; 123 | #elif SPLITS == 2 124 | return points[0].rg; 125 | #elif SPLITS == 3 126 | return points[0].rgb; 127 | #elif SPLITS == 4 128 | return points[0].rgba; 129 | #else 130 | for (int i = 0; i < 4; ++i) 131 | splitPoints[i] = points[0][i]; 132 | for (int i = 4; i < SPLITS; ++i) 133 | splitPoints[i] = points[1][i - 4]; 134 | return true; 135 | #endif 136 | #endif 137 | } 138 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/depth_partition/iterative/alpha_adjusted_first_find_quantiles_interval.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | #extension GL_ARB_texture_rectangle : enable 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | //#define POINTS x 28 | //#define OPACITY_THRESHOLD x 29 | //#define ROUND round or #define ROUND 30 | $DEFINES 31 | $OUT_BUFFERS_DECLARATION 32 | 33 | #ifndef OPACITY_THRESHOLD 34 | #define OPACITY_THRESHOLD 0.99 35 | #endif 36 | 37 | uniform sampler2DRect countTextures[8]; 38 | uniform float quantiles[POINTS]; 39 | 40 | int approx_required_layers(float alphas[8], float counts[24]) 41 | { 42 | float totals[8]; 43 | for (int i = 0; i < 8; i++) 44 | { 45 | totals[i] = 0; 46 | for (int j = i * 3; j < i * 3 + 3; ++j) 47 | totals[i] += counts[j]; 48 | } 49 | 50 | for (int i = 1; i < 8; ++i) 51 | { 52 | alphas[i] = alphas[i - 1] + (1 - alphas[i - 1]) * alphas[i]; 53 | } 54 | 55 | int layers = 0; 56 | int k = 0; 57 | bool done = false; 58 | for (int i = 0; i < 8; ++i) 59 | { 60 | if (!done) 61 | { 62 | layers += int(totals[i]); 63 | k = i * 3; 64 | } 65 | done = alphas[i] > OPACITY_THRESHOLD; 66 | } 67 | /* Saving the number of interval where alpha has gone above the opacity 68 | threshold */ 69 | #if POINTS > 4 70 | gl_FragData[4][1] = k + 3; 71 | #else 72 | gl_FragData[2][1] = k + 3; 73 | #endif 74 | return layers; 75 | } 76 | 77 | void main() 78 | { 79 | float counts[24]; 80 | float alphas[8]; 81 | float total = 0; 82 | for (int i = 0; i < 8; ++i) 83 | { 84 | vec4 t = texture2DRect(countTextures[i], gl_FragCoord.xy).rgba; 85 | for (int j = 0; j < 3; ++j) 86 | { 87 | total += t[j]; 88 | counts[i * 3 + j] = t[j]; 89 | } 90 | alphas[i] = t.a; 91 | } 92 | if (total == 0) 93 | discard; 94 | total = approx_required_layers(alphas, counts); 95 | 96 | float target_counts[8]; 97 | bool written[8]; 98 | for (int i = 0; i < 8 && i < POINTS; ++i) 99 | { 100 | target_counts[i] = ROUND(quantiles[i] * total); 101 | written[i] = false; 102 | } 103 | float left_accumulation = 0; 104 | /* Loop over all the histogram intervals */ 105 | for (int i = 0; i < 24; ++i) 106 | { 107 | float current_count = left_accumulation + counts[i]; 108 | #if POINTS < 5 109 | /* Checking which quantiles are found in the current index */ 110 | for (int j = 0; j < POINTS; ++j) 111 | { 112 | if (!written[j] && current_count > target_counts[j]) 113 | { 114 | gl_FragData[0][j] = float(i); 115 | gl_FragData[1][j] = left_accumulation; 116 | written[j] = true; 117 | } 118 | } 119 | #else 120 | for (int j = 0; j < 4; ++j) 121 | { 122 | if (!written[j] && current_count > target_counts[j]) 123 | { 124 | gl_FragData[0][j] = float(i); 125 | gl_FragData[1][j] = left_accumulation; 126 | written[j] = true; 127 | } 128 | } 129 | for (int j = 4; j < POINTS; ++j) 130 | { 131 | if (!written[j] && current_count > target_counts[j]) 132 | { 133 | gl_FragData[2][j - 4] = float(i); 134 | gl_FragData[3][j - 4] = left_accumulation; 135 | written[j] = true; 136 | } 137 | } 138 | #endif 139 | left_accumulation = current_count; 140 | } 141 | 142 | /* Saving the total per pixel fragment count in its texture for 143 | further access */ 144 | #if POINTS > 4 145 | const int buff = 4; 146 | #else 147 | const int buff = 2; 148 | #endif 149 | gl_FragData[buff].r = total; 150 | #if POINTS > 8 151 | #error "Unsupported number of simultaneous quantiles" 152 | #endif 153 | } 154 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/depth_partition/iterative/count_iteration.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | 25 | #extension GL_EXT_shader_image_load_store : enable 26 | #extension GL_ARB_texture_rectangle : enable 27 | 28 | uniform float proj33; 29 | uniform float proj34; 30 | float unproject(float x) 31 | { 32 | return -proj34 / (proj33 + 2.0 * x - 1.0); 33 | } 34 | float reproject(float x) 35 | { 36 | return 0.5 * (1.0 - proj33 - proj34 / x); 37 | } 38 | 39 | $DEFINES 40 | 41 | layout(r32ui) restrict uniform uimage2DRect counts[POINTS]; 42 | 43 | uniform sampler2DRect minMaxTexture; 44 | uniform sampler2DRect codedPreviousIntervalsTexture1; 45 | #if POINTS > 4 46 | uniform sampler2DRect codedPreviousIntervalsTexture2; 47 | #endif 48 | 49 | int findInterval(float depth, float abs_min, float abs_max, 50 | int previous_intervals); 51 | 52 | float fragmentDepth(); 53 | 54 | void main() 55 | { 56 | vec2 minMax = texture2DRect(minMaxTexture, gl_FragCoord.xy).rg; 57 | float min = -minMax[0]; 58 | float max = minMax[1]; 59 | 60 | float depth = -unproject(fragmentDepth()); 61 | 62 | vec4 code1 = 63 | texture2DRect(codedPreviousIntervalsTexture1, gl_FragCoord.xy).rgba; 64 | 65 | for (int i = 0; i < 4 && i < POINTS; ++i) 66 | { 67 | int code = int(code1[i]); 68 | int t = findInterval(depth, min, max, code); 69 | if (t != -1) 70 | { 71 | imageAtomicAdd(counts[i], ivec2(gl_FragCoord.xy), 1 << (t * 8)); 72 | } 73 | } 74 | 75 | #if POINTS > 4 76 | vec4 code2 = 77 | texture2DRect(codedPreviousIntervalsTexture2, gl_FragCoord.xy).rgba; 78 | for (int i = 4; i < POINTS; ++i) 79 | { 80 | int code = int(code2[i - 4]); 81 | int t = findInterval(depth, min, max, code); 82 | if (t != -1) 83 | { 84 | imageAtomicAdd(counts[i], ivec2(gl_FragCoord.xy), 1 << (t * 8)); 85 | } 86 | } 87 | #endif 88 | 89 | #if POINTS > 8 90 | #error "Unsupported number of simultaneous quantiles" 91 | #endif 92 | } 93 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/depth_partition/iterative/find_interval.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | 25 | uniform int iteration; 26 | 27 | $DEFINES 28 | 29 | int findInterval(float depth, float abs_min, float abs_max, 30 | int previous_intervals) 31 | { 32 | const float width_reduction = 0.25; // 1 / 4.0; 33 | const int shift = 2; 34 | const int intervals = 4; 35 | int mask = 0x03; 36 | 37 | float width = float(abs_max - abs_min) * 0.03125; 38 | int interval_index = previous_intervals & 0x1F; /* 5 lower bits */ 39 | float start = abs_min + width * float(interval_index); 40 | 41 | /* At each iteration we take the next 2-3 bits (depending on the number 42 | of intervals per point for iterations after the first) from the 43 | search code. 44 | These bits represent the interval in which a particular quantile point 45 | has been classified in previous iterations. Using that index we 46 | advance the starting point for the next interval set and divide the 47 | width according by the number of intervals. */ 48 | previous_intervals >>= 5; 49 | for (int i = 1; i < iteration; ++i) 50 | { 51 | width *= width_reduction; 52 | int interval_index = previous_intervals & mask; 53 | start += width * float(interval_index); 54 | previous_intervals >>= shift; 55 | } 56 | /* Finally we compute the interval width for the current search range. */ 57 | width *= width_reduction; 58 | 59 | /* Due to the limited precision, there's a chance that fragment depths 60 | fall onto one of the interval edges. It's been checked that with 61 | certain quantile quantities (0, 1) this can cause redundant count 62 | of points as both on the left of the range and on the first interval. 63 | There's little that can be done except from re-doing the left count 64 | for each range, however it requires either extra buffer memory or an 65 | additional pass, therefore it's not an option. 66 | Doing depth <= start seems to behave worse, and that's the only reason 67 | for considering an open search interval. */ 68 | if (depth < start) 69 | /* This is a fragment at the left of the current search range. */ 70 | return -1; 71 | /* Conversion from float to int drops the fractional part. */ 72 | int interval = int((depth - start) / width); 73 | if (interval >= intervals) 74 | return -1; 75 | return interval; 76 | } 77 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/depth_partition/iterative/first_count.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | 25 | #extension GL_ARB_texture_rectangle : enable 26 | #extension GL_EXT_shader_image_load_store : enable 27 | 28 | //#define ACCUMULATE_ALPHA? 29 | //#define COUNT_TEXTURES x "Must be a power of 2" 30 | $DEFINES 31 | 32 | out float total; 33 | 34 | layout(r32ui) restrict uniform uimage2DRect counts[COUNT_TEXTURES]; 35 | 36 | uniform float proj33; 37 | uniform float proj34; 38 | float unproject(float x) 39 | { 40 | return -proj34 / (proj33 + 2.0 * x - 1.0); 41 | } 42 | float reproject(float x) 43 | { 44 | return 0.5 * (1.0 - proj33 - proj34 / x); 45 | } 46 | 47 | uniform sampler2DRect minMaxTexture; 48 | float fragmentDepth(); 49 | 50 | /* http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightLinear */ 51 | uint index_of_first_one(uint v) 52 | { 53 | unsigned int c = 32; // c will be the number of zero bits on the right 54 | v &= -int(v); 55 | if (v != 0) 56 | c--; 57 | if ((v & 0x0000FFFF) != 0) 58 | c -= 16; 59 | if ((v & 0x00FF00FF) != 0) 60 | c -= 8; 61 | if ((v & 0x0F0F0F0F) != 0) 62 | c -= 4; 63 | if ((v & 0x33333333) != 0) 64 | c -= 2; 65 | if ((v & 0x55555555) != 0) 66 | c -= 1; 67 | return c; 68 | } 69 | 70 | void main() 71 | { 72 | /* We count unprojected depth values */ 73 | float depth = -unproject(fragmentDepth()); 74 | vec2 minMax = texture2DRect(minMaxTexture, gl_FragCoord.xy).rg; 75 | float abs_min = -minMax.r; 76 | float abs_max = minMax.g; 77 | 78 | double width = double(abs_max - abs_min) * 0.03125; 79 | int interval = min(int((depth - abs_min) / width), 31); 80 | 81 | /* Histogram bins are interleaved to avoid colissions in the atomic 82 | operations */ 83 | uint buffer = interval & (COUNT_TEXTURES - 1); 84 | uint index = interval >> index_of_first_one(COUNT_TEXTURES); 85 | uint bit = 1u << (COUNT_TEXTURES * index); 86 | /* Increasing the count of the bin in which this fragment's depth falls. */ 87 | imageAtomicAdd(counts[buffer], ivec2(gl_FragCoord.xy), bit); 88 | /* Additive blending is used to count the total number of fragments. */ 89 | total = 1; 90 | } 91 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/depth_partition/iterative/first_find_quantiles_interval.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | #extension GL_ARB_texture_rectangle : enable 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | //#define POINTS x 28 | //#define OPACITY_THRESHOLD x 29 | $DEFINES 30 | 31 | #ifndef OPACITY_THRESHOLD 32 | #define OPACITY_THRESHOLD 0.99 33 | #endif 34 | 35 | uniform usampler2DRect countTextures[8]; 36 | uniform sampler2DRect totalCountsTexture; 37 | uniform float quantiles[POINTS]; 38 | 39 | out vec4 codedIntervals[(POINTS + 3) / 4]; 40 | out vec4 leftAccumulations[(POINTS + 3) / 4]; 41 | 42 | uint counts[32]; 43 | bool read[8]; 44 | 45 | uint readCount(uint i) 46 | { 47 | /* If the texture for this interval count has not been read, 48 | access it. 49 | Notice that histogram bins have been interleaved in such a way 50 | that bin 0 is texture 0 chunk 0, bin 1 is texture 1 chunk 0, bin 8 51 | is texture 0 chunk 1 and so on so forth. */ 52 | uint index = i & 7; 53 | if (!read[index]) 54 | { 55 | uint x = texture2DRect(countTextures[index], gl_FragCoord.xy).r; 56 | counts[index] = x & 0xFF; 57 | counts[index + 8] = (x >> 8u) & 0xFF; 58 | counts[index + 16] = (x >> 16u) & 0xFF; 59 | counts[index + 24] = (x >> 24u) & 0xFF; 60 | read[index] = true; 61 | } 62 | return counts[i]; 63 | } 64 | 65 | void main() 66 | { 67 | uint total = uint(texture2DRect(totalCountsTexture, gl_FragCoord.xy).r); 68 | 69 | if (total == 0) 70 | discard; 71 | 72 | /* Clearing the flags for which count textures have been read. */ 73 | for (int i = 0; i < 8; ++i) 74 | read[i] = false; 75 | 76 | /* Computing the target fragment counts for the required quantiles. */ 77 | float target_counts[8]; 78 | bool written[8]; 79 | for (int i = 0; i < 8 && i < POINTS; ++i) 80 | { 81 | target_counts[i] = quantiles[i] * total; 82 | written[i] = false; 83 | } 84 | 85 | float left_accumulation = 0; 86 | for (int i = 0; i < 32; ++i) 87 | { 88 | float current_count = left_accumulation + readCount(i); 89 | #if POINTS < 5 90 | for (int j = 0; j < POINTS; ++j) 91 | { 92 | if (!written[j] && current_count > target_counts[j]) 93 | { 94 | codedIntervals[0][j] = float(i); 95 | leftAccumulations[0][j] = left_accumulation; 96 | written[j] = true; 97 | } 98 | } 99 | #else 100 | for (int j = 0; j < 4; ++j) 101 | { 102 | if (!written[j] && current_count > target_counts[j]) 103 | { 104 | codedIntervals[0][j] = float(i); 105 | leftAccumulations[0][j] = left_accumulation; 106 | written[j] = true; 107 | } 108 | } 109 | for (int j = 4; j < POINTS; ++j) 110 | { 111 | if (!written[j] && current_count > target_counts[j]) 112 | { 113 | codedIntervals[1][j - 4] = float(i); 114 | leftAccumulations[1][j - 4] = left_accumulation; 115 | written[j] = true; 116 | } 117 | } 118 | #endif 119 | left_accumulation = current_count; 120 | } 121 | 122 | #if POINTS > 8 123 | #error "Unsupported number of simultaneous quantiles" 124 | #endif 125 | } 126 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/depth_partition/iterative/minmax.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | 25 | $DEFINES 26 | 27 | out vec2 minMax; 28 | 29 | uniform float proj33; 30 | uniform float proj34; 31 | float unproject(float x) 32 | { 33 | return -proj34 / (proj33 + 2.0 * x - 1.0); 34 | } 35 | float reproject(float x) 36 | { 37 | return 0.5 * (1.0 - proj33 - proj34 / x); 38 | } 39 | 40 | float fragmentDepth(); 41 | 42 | void main() 43 | { 44 | /* We want to compute: 45 | min = unproject(max_i{x_i} + delta) 46 | max = unproject(min_i{x_i} - delta) 47 | (Note that unproject is monotonically 48 | decreasing in [0,1] -> [-near,-far], that's the reason for using min_i 49 | for max and vice versa). 50 | However that would require two passes, so instead we try: 51 | min = min_i{unproject(x_i + delta)} 52 | max = max_i{unproject(x_i - delta)} */ 53 | 54 | /* This code is being optimized to use RCP instead of DIV for reproject. 55 | The end result is that some values computed in the next steps' shaders 56 | lie outside the computed range. 57 | Forcing cgc to use the best precision is not working, so the current 58 | workaround is shifting the values with a reasonable offset for our 59 | scene dimensions. */ 60 | const float depth = -unproject(fragmentDepth()); 61 | #ifdef HALF_FLOAT_MINMAX 62 | const float min = depth - 1; 63 | const float max = depth + 1; 64 | #else 65 | const float min = depth - 0.005; 66 | const float max = depth + 0.005; 67 | #endif 68 | minMax = vec2(-min, max); 69 | } 70 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/depth_partition/profile.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | #extension GL_ARB_texture_rectangle : enable 25 | #extension GL_EXT_gpu_shader4 : enable 26 | 27 | $DEFINES 28 | #define SLICES $SLICES 29 | 30 | uniform float proj33; 31 | uniform float proj34; 32 | 33 | out vec4 counts[(SLICES - 1) / 4 + 1]; 34 | 35 | float unproject(float x) 36 | { 37 | return -proj34 / (proj33 + 2.0 * x - 1.0); 38 | } 39 | 40 | #if SLICES == 2 41 | #define RETURN_TYPE float 42 | #elif SLICES == 3 43 | #define RETURN_TYPE vec2 44 | #elif SLICES == 4 45 | #define RETURN_TYPE vec3 46 | #elif SLICES == 5 47 | #define RETURN_TYPE vec4 48 | #else 49 | #define RETURN_TYPE bool 50 | #endif 51 | #if SLICES > 5 52 | /* Arrays seems to be passed by value and not be reference (?) so we need 53 | to use a global variable in this cases. */ 54 | extern float splitPoints[SLICES - 1]; 55 | #endif 56 | RETURN_TYPE checkPartitionAndGetSplitPoints(const vec2 coord, float depth); 57 | 58 | float fragmentDepth(); 59 | 60 | void main() 61 | { 62 | for (int i = 0; i <= (SLICES - 1) / 4; ++i) 63 | counts[i] = vec4(0.0); 64 | 65 | vec2 coord = gl_FragCoord.xy; 66 | 67 | #ifdef UNPROJECT_DEPTH 68 | float depth = -unproject(fragmentDepth()); 69 | #else 70 | float depth = fragmentDepth(); 71 | #endif 72 | 73 | #if SLICES == 1 74 | checkPartitionAndGetSplitPoints(coord, depth); 75 | counts[0][0] = 1.0; 76 | #else 77 | #if SLICES == 2 78 | float splitPoints[1]; 79 | splitPoints[0] = checkPartitionAndGetSplitPoints(coord, depth); 80 | #elif SLICES < 6 81 | RETURN_TYPE splitPoints = checkPartitionAndGetSplitPoints(coord, depth); 82 | #else 83 | checkPartitionAndGetSplitPoints(coord, depth); 84 | #endif 85 | 86 | for (unsigned int i = 0; i < SLICES - 1; ++i) 87 | { 88 | if (depth < splitPoints[i]) 89 | { 90 | counts[i / 4][i % 4] = 1.0; 91 | return; 92 | } 93 | } 94 | counts[(SLICES - 1) / 4][(SLICES - 1) % 4] = 1.0; 95 | #endif 96 | } 97 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/final_pass.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | #extension GL_ARB_texture_rectangle : enable 25 | 26 | $DEFINES 27 | 28 | uniform sampler2DArray frontColors; 29 | uniform sampler2DArray backColors; 30 | 31 | /* The lower left corner of the camera viewport */ 32 | uniform vec2 corner; 33 | 34 | out vec4 outColor; 35 | 36 | void main(void) 37 | { 38 | /* Reading each layer colors */ 39 | vec2 coord = gl_FragCoord.xy - corner; 40 | outColor = vec4(0.0); 41 | 42 | for (int i = 0; i < SLICES; ++i) 43 | { 44 | vec4 front = texelFetch(frontColors, ivec3(coord, i), 0); 45 | vec4 back = texelFetch(backColors, ivec3(coord, i), 0); 46 | 47 | /* 1 - buffer0.a contains Pi(1 - a_i) for all layers in the first 48 | slice. Front to back blending of slices is associative, so we blend 49 | the composition of layers using the front to back formula, the 50 | result with the next layer and so on so forth.*/ 51 | outColor += front * (1.0 - outColor.a); 52 | outColor += back * (1.0 - outColor.a); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/multilayer/first_pass.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | 25 | $DEFINES 26 | #define SLICES $SLICES 27 | #define DEPTH_BUFFERS ((SLICES + 1) / 2) 28 | 29 | uniform float proj33; 30 | uniform float proj34; 31 | float unproject(float x) 32 | { 33 | return -proj34 / (proj33 + 2.0 * x - 1.0); 34 | } 35 | 36 | #if SLICES == 2 37 | #define RETURN_TYPE float 38 | #elif SLICES == 3 39 | #define RETURN_TYPE vec2 40 | #elif SLICES == 4 41 | #define RETURN_TYPE vec3 42 | #elif SLICES == 5 43 | #define RETURN_TYPE vec4 44 | #else 45 | #define RETURN_TYPE bool 46 | #endif 47 | 48 | float fragmentDepth(); 49 | vec4 shadeFragment(); 50 | #if SLICES > 5 51 | extern float splitPoints[SLICES - 1]; 52 | #endif 53 | RETURN_TYPE checkPartitionAndGetSplitPoints(const vec2 coord, float depth); 54 | 55 | out vec4 layerDepths[(SLICES + 1) / 2]; 56 | 57 | void main(void) 58 | { 59 | vec2 coord = gl_FragCoord.xy; 60 | 61 | #ifdef UNPROJECT_DEPTH 62 | float depth = -unproject(fragmentDepth()); 63 | #else 64 | float depth = fragmentDepth(); 65 | #endif 66 | 67 | #if SLICES == 1 68 | if (!checkPartitionAndGetSplitPoints(coord, depth)) 69 | discard; 70 | #elif SLICES == 2 71 | float splitPoint = checkPartitionAndGetSplitPoints(coord, depth); 72 | if (splitPoint == -1.0) 73 | discard; 74 | #elif SLICES < 6 75 | RETURN_TYPE splitPoints = checkPartitionAndGetSplitPoints(coord, depth); 76 | if (splitPoints[0] == -1.0) 77 | discard; 78 | #else 79 | if (!checkPartitionAndGetSplitPoints(coord, depth)) 80 | discard; 81 | #endif 82 | 83 | vec2 depths = vec2(-depth, depth); 84 | #if SLICES == 1 85 | layerDepths[0].xy = depths; 86 | #elif SLICES == 2 87 | 88 | if (depth < splitPoint) 89 | { 90 | layerDepths[0].xy = depths; 91 | layerDepths[0].zw = vec2(-1.0, 0); 92 | } 93 | else 94 | { 95 | layerDepths[0].xy = vec2(-1.0, 0); 96 | layerDepths[0].zw = depths; 97 | } 98 | #elif SLICES > 2 99 | for (int i = 0; i < DEPTH_BUFFERS; ++i) 100 | layerDepths[i] = vec4(-1.0, 0.0, -1.0, 0.0); 101 | 102 | for (int i = 0; i < SLICES; ++i) 103 | { 104 | if (i == SLICES - 1 || depth < splitPoints[min(i, SLICES - 2)]) 105 | { 106 | if (i % 2 == 0) 107 | layerDepths[i / 2].xy = depths; 108 | else 109 | layerDepths[i / 2].zw = depths; 110 | return; 111 | } 112 | } 113 | #endif 114 | } 115 | -------------------------------------------------------------------------------- /osgTransparency/shaders/GL3/simple/peel.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 420 24 | 25 | // XXX 26 | //#define ALPHA_TEST_DISCARD 27 | 28 | #extension GL_ARB_draw_buffers : enable 29 | #extension GL_ARB_texture_rectangle : enable 30 | 31 | uniform sampler2DRect depthBuffer; 32 | 33 | /* Buffer where the front layer of the front half is being blended */ 34 | uniform sampler2DRect blendedBuffer; 35 | 36 | uniform vec2 offset; 37 | uniform vec2 scaling; 38 | 39 | vec4 shadeFragment(); 40 | 41 | float fragmentDepth(); 42 | 43 | layout(location = 0) out float outDepth; 44 | layout(location = 1) out vec4 outColor; 45 | 46 | uniform float proj33; 47 | uniform float proj34; 48 | float unproject(float x) 49 | { 50 | return -proj34 / (proj33 + 2.0 * x - 1.0); 51 | } 52 | 53 | void main(void) 54 | { 55 | float frontDepth = -texture2DRect(depthBuffer, gl_FragCoord.xy).r; 56 | float depth = fragmentDepth(); 57 | 58 | /* Checking if the opacity of frontmost layer is above the discard 59 | threshold. */ 60 | #ifdef ALPHA_TEST_DISCARD 61 | vec2 coord = gl_FragCoord.xy * scaling + offset; 62 | if (textureRect(blendedBuffer, coord).a >= 0.95) 63 | discard; 64 | #endif 65 | 66 | if (depth < frontDepth) 67 | { 68 | discard; 69 | } 70 | if (depth == frontDepth) 71 | { 72 | /* Shading fragment peeled front to back */ 73 | vec4 color = shadeFragment(); 74 | 75 | color.rgb *= color.a; 76 | outColor = color; 77 | outDepth = -1; 78 | } 79 | else 80 | { 81 | /* Peeling fragment */ 82 | outColor = vec4(0.0); 83 | outDepth = -depth; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /osgTransparency/types.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_TYPES_H 24 | #define OSGTRANSPARENCY_TYPES_H 25 | 26 | #include 27 | namespace osg 28 | { 29 | class Drawable; 30 | class Program; 31 | class RenderInfo; 32 | class StateSet; 33 | class ShapeDrawable; 34 | 35 | template 36 | class ref_ptr; 37 | } 38 | 39 | namespace osgUtil 40 | { 41 | class RenderLeaf; 42 | } 43 | 44 | namespace bbp 45 | { 46 | namespace osgTransparency 47 | { 48 | /* Interal implementation types */ 49 | 50 | /** @internal */ 51 | class ShapeData; 52 | typedef std::map BoundShapesStorage; 53 | 54 | typedef osg::ref_ptr ProgramPtr; 55 | typedef std::map ProgramMap; 56 | } 57 | } 58 | #endif 59 | -------------------------------------------------------------------------------- /osgTransparency/util/GPUTimer.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_GPUTIMER_H 24 | #define OSGTRANSPARENCY_GPUTIMER_H 25 | 26 | #include "Stats.h" 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | namespace bbp 36 | { 37 | namespace osgTransparency 38 | { 39 | /** A timer using OpenGL timer queries. 40 | 41 | The timer is associated to a graphics context at construction and can only 42 | be used when that context is current. 43 | 44 | The stats gathered by the timer are printed when either the context is 45 | destroyed or the report() function is called. 46 | */ 47 | class GPUTimer 48 | { 49 | /*--- Public constructors/destructor */ 50 | public: 51 | /** Create a GPU timer associated with a given OpenGL state. 52 | 53 | For proper cleanup, the timer addds itself to the list of observers of 54 | the state object and keeps a reference to the state to remove itself 55 | from the observer list at destruction. */ 56 | GPUTimer(osg::State* state); 57 | 58 | ~GPUTimer(); 59 | 60 | /*--- Public member functions ---*/ 61 | 62 | /** Start a new time query. 63 | 64 | If this function is called again without calling stop first the second 65 | call will have no effect. 66 | The information provided as parameters will be copied over to the 67 | result once the query is completed. 68 | 69 | @param name The name that will identify this timer query. 70 | @param frameNumber The frame in which the timer is started. 71 | */ 72 | void start(const std::string& name, const unsigned int frameNumber); 73 | 74 | /** End the current timer query. 75 | 76 | Calling stop without having called start has undefined behaviour. 77 | */ 78 | void stop(); 79 | 80 | /** Check if timer queries are completed an retreive results. 81 | 82 | @param waitForCompletion Wait for all pending queries to complete. 83 | */ 84 | bool checkQueries(const bool waitForCompletion = false); 85 | 86 | struct Result 87 | { 88 | std::string name; 89 | unsigned int frame; 90 | float milliseconds; 91 | }; 92 | typedef std::vector Results; 93 | 94 | /** Return the list of timer queries completed in last call to 95 | checkQueries. 96 | */ 97 | const Results& getCompleted() const; 98 | 99 | /** Print to the given stream the list of timer queries completed in 100 | last call to checkQueries 101 | */ 102 | void reportCompleted(std::ostream& out) const; 103 | 104 | /** Return the minimum frame number of all the queries which have not been 105 | resolved yet. 106 | */ 107 | bool pendingQueriesMinimumFrame(unsigned int& frame) const; 108 | 109 | private: 110 | class Impl; 111 | Impl* _impl; 112 | }; 113 | } 114 | } 115 | #endif 116 | -------------------------------------------------------------------------------- /osgTransparency/util/ShapeData.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_UTIL_SHAPEDATA_H 24 | #define OSGTRANSPARENCY_UTIL_SHAPEDATA_H 25 | 26 | #include 27 | 28 | namespace bbp 29 | { 30 | namespace osgTransparency 31 | { 32 | struct ShapeData 33 | { 34 | ShapeData() 35 | : list(0) 36 | { 37 | } 38 | 39 | osg::ref_ptr shape; 40 | GLuint list; 41 | }; 42 | } 43 | } 44 | #endif 45 | -------------------------------------------------------------------------------- /osgTransparency/util/TextureDebugger.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_UTIL_TEXTUREDEBUGGER_H 24 | #define OSGTRANSPARENCY_UTIL_TEXTUREDEBUGGER_H 25 | 26 | #include 27 | 28 | #include 29 | 30 | namespace osg 31 | { 32 | class GraphicsContext; 33 | class TextureRectangle; 34 | class Texture2DArray; 35 | } 36 | 37 | class TextureDebugger : boost::noncopyable 38 | { 39 | public: 40 | TextureDebugger(); 41 | 42 | ~TextureDebugger(); 43 | 44 | void setWindowName(const std::string& name); 45 | 46 | void renderTexture(osg::GraphicsContext* context, 47 | osg::TextureRectangle* texture, 48 | const std::string& code = ""); 49 | 50 | void renderTexture(osg::GraphicsContext* context, 51 | osg::Texture2DArray* texture, const unsigned int layer, 52 | const std::string& code = ""); 53 | 54 | /* Fuction prototype must be either ivec4 transform(ivec4 sample) or 55 | vec4 transform(vec4 sample) depending on whether it's going to 56 | be used with a integer texture or not */ 57 | static std::string GLSL(const std::string& function, bool integer = false, 58 | const std::string& preamble = ""); 59 | 60 | protected: 61 | struct Impl; 62 | Impl* _impl; 63 | }; 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /osgTransparency/util/constants.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include 24 | 25 | #include "constants.h" 26 | 27 | namespace bbp 28 | { 29 | namespace osgTransparency 30 | { 31 | /* 32 | Compile time constants 33 | */ 34 | #ifdef OSG_GL3_AVAILABLE 35 | const std::string BYPASS_VERT_SHADER = 36 | "//BYPASS\n" 37 | "#version 410\n" 38 | "in vec4 osg_Vertex;\n" 39 | "void main() { gl_Position = osg_Vertex; }"; 40 | const std::string TRIVIAL_VERT_SHADER = 41 | "//TRIVIAL\n" 42 | "#version 410\n" 43 | "uniform mat4x4 osg_ModelViewProjectionMatrix;\n" 44 | "in vec4 osg_Vertex;\n" 45 | "void main() { gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex; }"; 46 | #else 47 | const std::string BYPASS_VERT_SHADER = 48 | "//BYPASS\nvoid main() { gl_Position = gl_Vertex; }"; 49 | const std::string TRIVIAL_VERT_SHADER = 50 | "//TRIVIAL\nvoid main() { gl_Position = ftransform(); }"; 51 | #endif 52 | 53 | const std::string PROJECT_UNPROJECT_SNIPPET = 54 | "uniform float proj33;\n" 55 | "uniform float proj34;\n" 56 | "float unproject(float x) { return -proj34 / (proj33 + 2.0 * x - 1.0); }\n" 57 | "float reproject(float x) { return 0.5 * (1.0 - proj33 - proj34 / x); }\n"; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /osgTransparency/util/constants.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_UTIL_CONSTANTS_H 24 | #define OSGTRANSPARENCY_UTIL_CONSTANTS_H 25 | 26 | // Including GL headers for various OpenGL tokens 27 | #ifdef _WIN32 28 | #define GL_GLEXT_PROTOTYPES 29 | #include 30 | #include 31 | #endif 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | namespace bbp 39 | { 40 | namespace osgTransparency 41 | { 42 | /* 43 | Compile time constants 44 | */ 45 | static const unsigned int SUPERSAMPLING_FACTOR = 3; 46 | static const unsigned int MAX_SLICES = 8; 47 | static const unsigned int MAX_DEPTH_BUFFERS = MAX_SLICES / 2; 48 | 49 | /* Some declarations used to enhance readbility */ 50 | static const int OFF_OVERRIDE = 51 | osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF; 52 | static const int ON_OVERRIDE = 53 | osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON; 54 | static const int OFF_PROTECTED = 55 | osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF; 56 | static const int ON_PROTECTED = 57 | osg::StateAttribute::PROTECTED | osg::StateAttribute::ON; 58 | static const int ON_OVERRIDE_PROTECTED = osg::StateAttribute::PROTECTED | 59 | osg::StateAttribute::ON | 60 | osg::StateAttribute::OVERRIDE; 61 | static const int OFF = osg::StateAttribute::OFF; 62 | static const int ON = osg::StateAttribute::ON; 63 | static const osg::BlendEquation::Equation FUNC_ADD = 64 | osg::BlendEquation::FUNC_ADD; 65 | static const osg::BlendEquation::Equation RGBA_MAX = 66 | osg::BlendEquation::RGBA_MAX; 67 | 68 | static const osg::Camera::BufferComponent COLOR_BUFFERS[] = { 69 | osg::Camera::COLOR_BUFFER0, osg::Camera::COLOR_BUFFER1, 70 | osg::Camera::COLOR_BUFFER2, osg::Camera::COLOR_BUFFER3, 71 | osg::Camera::COLOR_BUFFER4, osg::Camera::COLOR_BUFFER5, 72 | osg::Camera::COLOR_BUFFER6, osg::Camera::COLOR_BUFFER7}; 73 | 74 | static const GLenum GL_BUFFER_NAMES[] = { 75 | GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, 76 | GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5, 77 | GL_COLOR_ATTACHMENT6, GL_COLOR_ATTACHMENT7}; 78 | 79 | inline std::string sm(const char *main) 80 | { 81 | /* The forward declaration of shadeVertex is used for trivial shaders that 82 | forward the vertex shading to the default vertex shading contained in 83 | an external StateSet */ 84 | static std::string preamble( 85 | "//sm\nvoid shadeVertex(); void trivialShadeVertex();" 86 | "void main() {"); 87 | return preamble + main + "}"; 88 | } 89 | 90 | extern const std::string BYPASS_VERT_SHADER; 91 | extern const std::string TRIVIAL_VERT_SHADER; 92 | extern const std::string PROJECT_UNPROJECT_SNIPPET; 93 | 94 | /* 95 | Run-time time constants depending on environmental variables 96 | */ 97 | extern bool ADJUST_QUANTILES_WITH_ALPHA; 98 | extern bool COMPUTE_MAX_DEPTH_COMPLEXITY; 99 | extern bool PROFILE_DEPTH_PARTITION; 100 | extern float OPACITY_THRESHOLD; 101 | } 102 | } 103 | #endif 104 | -------------------------------------------------------------------------------- /osgTransparency/util/extensions.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_UTIL_EXTENSIONS_H 24 | #define OSGTRANSPARENCY_UTIL_EXTENSIONS_H 25 | 26 | #include 27 | 28 | #if OSG_VERSION_GREATER_OR_EQUAL(3, 3, 3) 29 | #include 30 | #else 31 | #include 32 | #include 33 | #include 34 | #include 35 | #endif 36 | 37 | namespace bbp 38 | { 39 | namespace osgTransparency 40 | { 41 | #if OSG_VERSION_GREATER_OR_EQUAL(3, 3, 3) 42 | 43 | typedef osg::GLExtensions DrawExtensions; 44 | inline osg::GLExtensions* getDrawExtensions(const unsigned int contextID) 45 | { 46 | return osg::GLExtensions::Get(contextID, true); 47 | } 48 | 49 | typedef osg::GLExtensions FBOExtensions; 50 | inline osg::GLExtensions* getFBOExtensions(const unsigned int contextID) 51 | { 52 | return osg::GLExtensions::Get(contextID, true); 53 | } 54 | 55 | typedef osg::GLExtensions BufferExtensions; 56 | inline osg::GLExtensions* getBufferExtensions(const unsigned int contextID) 57 | { 58 | return osg::GLExtensions::Get(contextID, true); 59 | } 60 | 61 | typedef osg::GLExtensions GL2Extensions; 62 | inline osg::GLExtensions* getGL2Extensions(const unsigned int contextID) 63 | { 64 | return osg::GLExtensions::Get(contextID, true); 65 | } 66 | 67 | #else 68 | 69 | typedef osg::Drawable::Extensions DrawExtensions; 70 | 71 | inline osg::Drawable::Extensions* getDrawExtensions( 72 | const unsigned int contextID) 73 | { 74 | return osg::Drawable::getExtensions(contextID, true); 75 | } 76 | 77 | typedef osg::FBOExtensions FBOExtensions; 78 | inline osg::FBOExtensions* getFBOExtensions(const unsigned int contextID) 79 | { 80 | return osg::FBOExtensions::instance(contextID, true); 81 | } 82 | 83 | typedef osg::GLBufferObject::Extensions BufferExtensions; 84 | inline osg::GLBufferObject::Extensions* getBufferExtensions( 85 | const unsigned int contextID) 86 | { 87 | return osg::GLBufferObject::getExtensions(contextID, true); 88 | } 89 | 90 | typedef osg::GL2Extensions GL2Extensions; 91 | inline osg::GL2Extensions* getGL2Extensions(const unsigned int contextID) 92 | { 93 | return osg::GL2Extensions::Get(contextID, true); 94 | } 95 | 96 | #endif 97 | } 98 | } 99 | #endif 100 | -------------------------------------------------------------------------------- /osgTransparency/util/glerrors.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef NDEBUG 24 | #include "glerrors.h" 25 | 26 | #define GL_GLEXT_PROTOTYPES 27 | #include 28 | 29 | #include 30 | 31 | #if defined(__APPLE__) 32 | #include 33 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" 34 | #else 35 | #include 36 | #endif 37 | 38 | #include 39 | 40 | namespace bbp 41 | { 42 | namespace osgTransparency 43 | { 44 | void checkGLErrors(const std::string &message) 45 | { 46 | GLenum error = glGetError(); 47 | if (error != GL_NO_ERROR) 48 | std::cout << "OpenGL error detected: " << gluErrorString(error) << ", " 49 | << message << std::endl; 50 | } 51 | } 52 | } 53 | #endif 54 | -------------------------------------------------------------------------------- /osgTransparency/util/glerrors.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_UTIL_GLERRORS_H 24 | #define OSGTRANSPARENCY_UTIL_GLERRORS_H 25 | 26 | #ifndef NDEBUG 27 | #include 28 | #endif 29 | 30 | namespace bbp 31 | { 32 | namespace osgTransparency 33 | { 34 | #ifndef NDEBUG 35 | void checkGLErrors(const std::string& message); 36 | #else 37 | inline void checkGLErrors(const std::string&) 38 | { 39 | } 40 | #endif 41 | } 42 | } 43 | #endif 44 | -------------------------------------------------------------------------------- /osgTransparency/util/loaders.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_UTIL_LOADERS_H 24 | #define OSGTRANSPARENCY_UTIL_LOADERS_H 25 | 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | namespace bbp 34 | { 35 | namespace osgTransparency 36 | { 37 | /** 38 | Creates a GLSL program using the shader sources indicated. 39 | If the sourceFiles vector is emtpy, then returns an empty program. 40 | */ 41 | osg::Program *loadProgram( 42 | const std::vector &sourceFiles, 43 | const std::map &vars = 44 | std::map(), 45 | const std::vector &extraPaths = 46 | std::vector()); 47 | 48 | /** 49 | Adds shader sources to a GLSL program using the file names indicated. 50 | */ 51 | void addShaders(osg::Program *program, 52 | const std::vector &sourceFiles, 53 | const std::map &vars = 54 | std::map(), 55 | const std::vector &extraPaths = 56 | std::vector()); 57 | 58 | /** 59 | Loads a shader source file and replaces $varname words with the 60 | contents of the table inside the cell with the same name (but no dollar 61 | symbol). 62 | If any error is detected an empty string is returned. 63 | */ 64 | std::string readSourceAndReplaceVariables( 65 | const boost::filesystem::path &shaderFile, 66 | const std::map &vars, 67 | const std::vector &extraPaths = 68 | std::vector()); 69 | } 70 | } 71 | #endif 72 | -------------------------------------------------------------------------------- /osgTransparency/util/paths.in.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef OSGTRANSPARENCY_PATHS_H 24 | #define OSGTRANSPARENCY_PATHS_H 25 | 26 | #define SOURCE_PATH "${PROJECT_SOURCE_DIR}" 27 | #define INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}" 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /osgTransparency/util/strings_array.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #ifndef STRINGS_ARRAY_H 24 | #define STRINGS_ARRAY_H 25 | 26 | #include 27 | #include 28 | 29 | namespace bbp 30 | { 31 | namespace osgTransparency 32 | { 33 | inline boost::array strings(const std::string &s1) 34 | { 35 | boost::array array; 36 | array[0] = s1; 37 | return array; 38 | } 39 | 40 | inline boost::array strings(const std::string &s1, 41 | const std::string &s2) 42 | { 43 | boost::array array; 44 | array[0] = s1; 45 | array[1] = s2; 46 | return array; 47 | } 48 | 49 | inline boost::array strings(const std::string &s1, 50 | const std::string &s2, 51 | const std::string &s3) 52 | { 53 | boost::array array; 54 | array[0] = s1; 55 | array[1] = s2; 56 | array[2] = s3; 57 | return array; 58 | } 59 | 60 | inline boost::array strings(const std::string &s1, 61 | const std::string &s2, 62 | const std::string &s3, 63 | const std::string &s4) 64 | { 65 | boost::array array; 66 | array[0] = s1; 67 | array[1] = s2; 68 | array[2] = s3; 69 | array[3] = s4; 70 | return array; 71 | } 72 | 73 | inline boost::array strings(const std::string &s1, 74 | const std::string &s2, 75 | const std::string &s3, 76 | const std::string &s4, 77 | const std::string &s5) 78 | { 79 | boost::array array; 80 | array[0] = s1; 81 | array[1] = s2; 82 | array[2] = s3; 83 | array[3] = s4; 84 | array[4] = s5; 85 | return array; 86 | } 87 | } 88 | } 89 | #endif 90 | -------------------------------------------------------------------------------- /osgTransparency/util/trace.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include "trace.h" 24 | 25 | #ifdef OSGTRANSPARENCY_USE_EXTRAE 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | namespace bbp 35 | { 36 | namespace osgTransparency 37 | { 38 | namespace detail 39 | { 40 | class TranslationTable 41 | { 42 | public: 43 | TranslationTable() 44 | : _offset(1) 45 | { 46 | if (::getenv("OSGTRANSPARENCY_EXTRAE_VALUE_OFFSET") != 0) 47 | { 48 | const char *value = ::getenv("OSGTRANSPARENCY_EXTRAE_VALUE_OFFSET"); 49 | int n = strtol(value, 0, 10); 50 | if (n > 0) 51 | _offset = n; 52 | } 53 | } 54 | 55 | ~TranslationTable() { dumpSymbols(); } 56 | void dumpSymbols() 57 | { 58 | if (_functions.size() == 0) 59 | return; 60 | 61 | /* Dumping the translations */ 62 | std::ofstream labels("EXTRAE_osgTransparency_labels.pcf"); 63 | labels << "EVENT_TYPE" << std::endl; 64 | labels << "0 " << FunctionTraceID::extraeType << " User function" 65 | << std::endl; 66 | labels << "VALUES" << std::endl; 67 | for (size_t i = 0; i != _functions.size(); ++i) 68 | { 69 | labels << (_offset + i) << ' ' << _functions[i] << std::endl; 70 | } 71 | 72 | std::vector values; 73 | std::vector names; 74 | names.push_back((char *)"End"); 75 | values.push_back(0); 76 | for (size_t i = 0; i != _functions.size(); ++i) 77 | { 78 | values.push_back(_offset + i); 79 | names.push_back((char *)_functions[i].c_str()); 80 | } 81 | Extrae_define_event_type(FunctionTraceID::extraeType, 82 | (char *)"User function", names.size(), 83 | &values[0], &names[0]); 84 | 85 | _functions.clear(); 86 | } 87 | 88 | int addFunction(const char *function) 89 | { 90 | std::mutex::scoped_lock lock(_mutex); 91 | std::string name(function); 92 | name = name.substr(0, name.find_first_of("(")); 93 | if (name.find_first_of(" ") != std::string::npos) 94 | name = name.substr(name.find_first_of(" ") + 1, std::string::npos); 95 | int id = _functions.size() + _offset; 96 | _functions.push_back(name); 97 | return id; 98 | } 99 | 100 | private: 101 | int _offset; 102 | std::mutex _mutex; 103 | std::vector _functions; 104 | }; 105 | 106 | TranslationTable &getTranslationTable() 107 | { 108 | static TranslationTable table; 109 | return table; 110 | } 111 | 112 | FunctionTraceID::FunctionTraceID(const char *function) 113 | { 114 | _id = getTranslationTable().addFunction(function); 115 | } 116 | 117 | FunctionTraceID::operator int() const 118 | { 119 | return _id; 120 | } 121 | 122 | void FunctionTraceID::dumpSymbols() 123 | { 124 | getTranslationTable().dumpSymbols(); 125 | } 126 | } 127 | } 128 | } 129 | #endif 130 | -------------------------------------------------------------------------------- /osgTransparency/util/trace.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | // Contributing Authors: 23 | // 24 | // $URL$ 25 | // $Revision$ 26 | // 27 | // last changed: $Date$ 28 | // by $Author$ 29 | ////////////////////////////////////////////////////////////////////// 30 | 31 | #ifndef OSGTRANSPARENCY_UTIL_TRACE_H 32 | #define OSGTRANSPARENCY_UTIL_TRACE_H 33 | 34 | #if defined OSGTRANSPARENCY_USE_EXTRAE 35 | 36 | #include 37 | #include 38 | 39 | #define OSGTRANSPARENCY_TRACE_FUNCTION() \ 40 | Extrae_user_function(1); \ 41 | struct EndFunction \ 42 | { \ 43 | EndFunction() {} \ 44 | ~EndFunction() { Extrae_user_function(0); } \ 45 | } token__ 46 | /* The empty contructor works around gcc warning token__ not being used */ 47 | 48 | namespace bbp 49 | { 50 | namespace osgTransparency 51 | { 52 | namespace detail 53 | { 54 | struct FunctionTraceID 55 | { 56 | static const extrae_type_t extraeType = 50000000; 57 | 58 | FunctionTraceID(const char *function); 59 | 60 | operator int() const; 61 | 62 | static void dumpSymbols(); 63 | 64 | private: 65 | int _id; 66 | }; 67 | } 68 | } 69 | } 70 | 71 | #define OSGTRANSPARENCY_TRACE_FUNCTION() \ 72 | { \ 73 | static osgTransparency::detail::FunctionTraceID id( \ 74 | BOOST_CURRENT_FUNCTION); \ 75 | Extrae_event(osgTransparency::detail::FunctionTraceID::extraeType, \ 76 | id); \ 77 | } \ 78 | struct EndFunction \ 79 | { \ 80 | EndFunction() {} \ 81 | ~EndFunction() \ 82 | { \ 83 | Extrae_event(osgTransparency::detail::FunctionTraceID::extraeType, \ 84 | 0); \ 85 | } \ 86 | } token__ 87 | 88 | #else 89 | 90 | #define OSGTRANSPARENCY_TRACE_FUNCTION() 91 | #define OSGTRANSPARENCY_TRACE_FUNCTION2() 92 | 93 | #endif 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | # Blue Brain Project and 3 | # Universidad Politécnica de Madrid (UPM) 4 | # Juan Hernando 5 | # 6 | # This file is part of osgTransparency 7 | # 8 | # 9 | # This library is free software; you can redistribute it and/or modify it under 10 | # the terms of the GNU Lesser General Public License version 3.0 as published 11 | # by the Free Software Foundation. 12 | # 13 | # This library is distributed in the hope that it will be useful, but WITHOUT 14 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | # details. 17 | # 18 | # You should have received a copy of the GNU Lesser General Public License 19 | # along with this library; if not, write to the Free Software Foundation, Inc., 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | 22 | try_run(TEST_GRAPHICS_CONTEXT_RESULT _dummy 23 | ${PROJECT_BINARY_DIR}/tests/test_graphics_context 24 | ${CMAKE_CURRENT_SOURCE_DIR}/test_graphics_context.cpp 25 | CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${OPENSCENEGRAPH_INCLUDE_DIRS}" 26 | LINK_LIBRARIES "${OPENSCENEGRAPH_LIBRARIES}") 27 | if(TEST_GRAPHICS_CONTEXT_RESULT MATCHES FAILED_TO_RUN) 28 | # Since all tests require a GPU, we skip them completely 29 | if(NOT TARGET tests) 30 | add_custom_target(tests) 31 | endif() 32 | if(NOT TARGET ${PROJECT_NAME}-tests) 33 | add_custom_target(${PROJECT_NAME}-tests) 34 | endif() 35 | return() 36 | endif() 37 | 38 | set(COMMON_TEST_SOURCES 39 | unit/RenderContext.cpp 40 | unit/compare.cpp 41 | unit/util.cpp) 42 | set(TEST_LIBRARIES osgTransparency 43 | ${Boost_LIBRARIES} 44 | ${OPENSCENEGRAPH_LIBRARIES} 45 | ${OPENGL_LIBRARIES}) 46 | 47 | add_library(testCommon ${COMMON_TEST_SOURCES}) 48 | target_link_libraries(testCommon "${TEST_LIBRARIES}") 49 | list(APPEND TEST_LIBRARIES testCommon) 50 | 51 | set(EXCLUDE_FROM_TESTS ${COMMON_TEST_SOURCES} test_graphics_context.cpp) 52 | include(CommonCTest) 53 | -------------------------------------------------------------------------------- /tests/test_graphics_context.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | namespace 27 | { 28 | /* Forcing the invokation of the static constructors of osgViewer 29 | to initialize the Windowing System Interface (WSI), otherwise 30 | GraphicsContext::realize seg faults */ 31 | const osgViewer::Viewer viewer; 32 | } 33 | 34 | int main(int, char *[]) 35 | { 36 | osg::ref_ptr traits = 37 | new osg::GraphicsContext::Traits; 38 | traits->width = 600; 39 | traits->height = 480; 40 | traits->windowDecoration = false; 41 | traits->red = 8; 42 | traits->green = 8; 43 | traits->blue = 8; 44 | traits->alpha = 8; 45 | traits->windowDecoration = false; 46 | traits->pbuffer = true; 47 | traits->doubleBuffer = false; 48 | traits->sharedContext = 0; 49 | 50 | osg::ref_ptr context = 51 | osg::GraphicsContext::createGraphicsContext(traits); 52 | if (!context->realize()) 53 | abort(); 54 | if (!context->makeCurrent()) 55 | abort(); 56 | if (!context->releaseContext()) 57 | abort(); 58 | context->close(); 59 | } 60 | -------------------------------------------------------------------------------- /tests/unit/RenderContext.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include "RenderContext.h" 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | 31 | namespace bbp 32 | { 33 | namespace osgTransparency 34 | { 35 | namespace test 36 | { 37 | namespace 38 | { 39 | /* Just to force the invokation of the static constructors of osgViewer 40 | that will initialize the Windowing System Interface (WSI). */ 41 | const osgViewer::Viewer viewer; 42 | } 43 | 44 | namespace detail 45 | { 46 | class RenderContext 47 | { 48 | public: 49 | RenderContext() 50 | : savedStateSet(new osg::StateSet) 51 | { 52 | } 53 | 54 | osg::ref_ptr savedStateSet; 55 | }; 56 | } 57 | 58 | RenderContext::RenderContext(const unsigned int width_, 59 | const unsigned int height_) 60 | : width(width_) 61 | , height(height_) 62 | , previousLeaf(0) 63 | , _impl(new detail::RenderContext) 64 | { 65 | osg::ref_ptr traits = 66 | new osg::GraphicsContext::Traits(); 67 | traits->x = 0; 68 | traits->y = 0; 69 | traits->width = width; 70 | traits->height = height; 71 | traits->red = 8; 72 | traits->green = 8; 73 | traits->blue = 8; 74 | traits->alpha = 8; 75 | traits->windowDecoration = false; 76 | traits->pbuffer = true; 77 | traits->doubleBuffer = false; 78 | traits->sharedContext = 0; 79 | 80 | context = osg::GraphicsContext::createGraphicsContext(traits); 81 | BOOST_CHECK(context); 82 | context->realize(); 83 | context->makeCurrent(); 84 | 85 | state = context->getState(); 86 | BOOST_CHECK(state); 87 | renderInfo = new osg::RenderInfo(state, 0); 88 | } 89 | 90 | RenderContext::~RenderContext() 91 | { 92 | delete renderInfo; 93 | context->releaseContext(); 94 | context->close(); 95 | } 96 | 97 | void RenderContext::saveCurrentState(const std::string &filename) 98 | { 99 | state->captureCurrentState(*_impl->savedStateSet); 100 | if (!filename.empty()) 101 | { 102 | osgDB::writeObjectFile(*_impl->savedStateSet, filename); 103 | } 104 | } 105 | 106 | void RenderContext::compareCurrentAndSavedState() 107 | { 108 | osg::ref_ptr current(new osg::StateSet); 109 | state->captureCurrentState(*current); 110 | 111 | BOOST_CHECK(*current == *_impl->savedStateSet); 112 | } 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /tests/unit/RenderContext.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | 27 | namespace osg 28 | { 29 | class GraphicsContext; 30 | class State; 31 | class RenderInfo; 32 | } 33 | 34 | namespace osgUtil 35 | { 36 | class RenderLeaf; 37 | } 38 | 39 | namespace bbp 40 | { 41 | namespace osgTransparency 42 | { 43 | namespace test 44 | { 45 | namespace detail 46 | { 47 | class RenderContext; 48 | } 49 | 50 | struct RenderContext 51 | { 52 | osg::ref_ptr context; 53 | const unsigned int width; 54 | const unsigned int height; 55 | osgUtil::RenderLeaf *previousLeaf; 56 | 57 | osg::RenderInfo *renderInfo; 58 | osg::ref_ptr state; 59 | 60 | /** Creates and makes current and OpenGL context with an offsreen 61 | framebuffer of the desired size. */ 62 | RenderContext(const unsigned int width, const unsigned int height); 63 | virtual ~RenderContext(); 64 | 65 | void saveCurrentState(const std::string &filename = ""); 66 | void compareCurrentAndSavedState(); 67 | 68 | private: 69 | RenderContext(const RenderContext &); 70 | RenderContext &operator=(const RenderContext &); 71 | 72 | detail::RenderContext *_impl; 73 | }; 74 | 75 | template 76 | struct SizedRenderContext : public RenderContext 77 | { 78 | SizedRenderContext() 79 | : RenderContext(WIDTH, HEIGHT) 80 | { 81 | } 82 | }; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /tests/unit/compare.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include 24 | 25 | namespace osg 26 | { 27 | class Viewport; 28 | } 29 | 30 | namespace bbp 31 | { 32 | namespace osgTransparency 33 | { 34 | namespace test 35 | { 36 | bool compare(const osg::Image &image1, const osg::Image &image2, 37 | const float channelThreshold = 1.0); 38 | 39 | bool compare(const std::string &referenceFile, const int x, const int y, 40 | const int width, const int height, 41 | const GLenum pixelFormat = GL_RGBA, 42 | const GLenum type = GL_UNSIGNED_BYTE, 43 | const float channelThreshold = 1.0); 44 | 45 | bool compare(const std::string &referenceFile, const osg::Viewport &viewport, 46 | const GLenum pixelFormat = GL_RGBA, 47 | const GLenum type = GL_UNSIGNED_BYTE, 48 | const float channelThreshold = 1.0); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/unit/img/small_triangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlueBrain/osgTransparency/ce3b100615912ddf4db03c403c23af77b27d0f45/tests/unit/img/small_triangle.png -------------------------------------------------------------------------------- /tests/unit/img/triangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlueBrain/osgTransparency/ce3b100615912ddf4db03c403c23af77b27d0f45/tests/unit/img/triangle.png -------------------------------------------------------------------------------- /tests/unit/shaders/GL2/main.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | 25 | vec4 shadeFragment(); 26 | 27 | void main() 28 | { 29 | gl_FragColor = shadeFragment(); 30 | } -------------------------------------------------------------------------------- /tests/unit/shaders/GL2/main.vert: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | 25 | void shadeVertex(); 26 | 27 | void main() 28 | { 29 | shadeVertex(); 30 | } -------------------------------------------------------------------------------- /tests/unit/shaders/GL2/stretch.vert: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | 25 | uniform vec4 color; 26 | $DEFINES 27 | 28 | #ifndef LEFT 29 | #define LEFT -1 30 | #endif 31 | #ifndef RIGHT 32 | #define RIGHT 1 33 | #endif 34 | #ifndef BOTTOM 35 | #define BOTTOM -1 36 | #endif 37 | #ifndef TOP 38 | #define TOP 1 39 | #endif 40 | 41 | void shadeVertex() 42 | { 43 | gl_FrontColor = color; 44 | gl_Position.x = (gl_Vertex.x + 1) / 2.0 * (RIGHT - LEFT) + LEFT; 45 | gl_Position.y = (gl_Vertex.y + 1) / 2.0 * (TOP - BOTTOM) + BOTTOM; 46 | gl_Position.zw = gl_Vertex.zw; 47 | } 48 | -------------------------------------------------------------------------------- /tests/unit/shaders/GL2/trivial.frag: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | 25 | vec4 shadeFragment() 26 | { 27 | return vec4(1.0); 28 | } 29 | -------------------------------------------------------------------------------- /tests/unit/shaders/GL2/trivial.vert: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #version 120 24 | 25 | void shadeVertex() 26 | { 27 | gl_FrontColor = gl_Color; 28 | gl_Position = gl_Vertex; 29 | } 30 | -------------------------------------------------------------------------------- /tests/unit/util.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | #include "osgTransparency/util/paths.h" 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | namespace bbp 33 | { 34 | namespace osgTransparency 35 | { 36 | namespace test 37 | { 38 | namespace util 39 | { 40 | namespace 41 | { 42 | typedef std::vector Strings; 43 | typedef std::map Vars; 44 | 45 | osg::Program *_loadProgram(const Strings &sources, const Vars &vars = Vars()) 46 | { 47 | typedef boost::filesystem::path Path; 48 | typedef std::vector Paths; 49 | 50 | Paths paths; 51 | #ifdef OSG_GL3_AVAILABLE 52 | paths.push_back(Path(SOURCE_PATH) / "tests/unit/shaders/GL3"); 53 | #else 54 | paths.push_back(Path(SOURCE_PATH) / "tests/unit/shaders/GL2"); 55 | #endif 56 | 57 | return loadProgram(sources, vars, paths); 58 | } 59 | } 60 | 61 | osg::Program *createTrivialProgram(const bool withMain) 62 | { 63 | Strings sources; 64 | sources.push_back("trivial.vert"); 65 | sources.push_back("trivial.frag"); 66 | if (withMain) 67 | { 68 | sources.push_back("main.vert"); 69 | sources.push_back("main.frag"); 70 | } 71 | return _loadProgram(sources); 72 | } 73 | 74 | osg::Program *createStretchProgram(const float left, const float right, 75 | const float bottom, const float top, 76 | const bool withMain) 77 | { 78 | Strings sources; 79 | sources.push_back("stretch.vert"); 80 | sources.push_back("trivial.frag"); 81 | if (withMain) 82 | { 83 | sources.push_back("main.vert"); 84 | sources.push_back("main.frag"); 85 | } 86 | using boost::str; 87 | using boost::format; 88 | Vars vars; 89 | vars["DEFINES"] = str(format("#define LEFT %1%\n#define RIGHT %2%\n" 90 | "#define BOTTOM %3%\n#define TOP %4%\n") % 91 | left % right % bottom % top); 92 | return _loadProgram(sources, vars); 93 | } 94 | 95 | osg::Program *createShallowMainProgram() 96 | { 97 | Strings sources; 98 | sources.push_back("main.vert"); 99 | sources.push_back("main.frag"); 100 | return _loadProgram(sources); 101 | } 102 | } 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /tests/unit/util.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2006-2018, École Polytechnique Fédérale de Lausanne (EPFL) / 2 | * Blue Brain Project and 3 | * Universidad Politécnica de Madrid (UPM) 4 | * Juan Hernando 5 | * 6 | * This file is part of osgTransparency 7 | * 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under 10 | * the terms of the GNU Lesser General Public License version 3.0 as published 11 | * by the Free Software Foundation. 12 | * 13 | * This library is distributed in the hope that it will be useful, but WITHOUT 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 16 | * details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public License 19 | * along with this library; if not, write to the Free Software Foundation, Inc., 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | */ 22 | 23 | namespace osg 24 | { 25 | class Program; 26 | } 27 | 28 | namespace bbp 29 | { 30 | namespace osgTransparency 31 | { 32 | namespace test 33 | { 34 | namespace util 35 | { 36 | osg::Program *createTrivialProgram(const bool withMain = true); 37 | osg::Program *createStretchProgram(const float left, const float right, 38 | const float bottom, const float top, 39 | const bool withMain = true); 40 | osg::Program *createShallowMainProgram(); 41 | } 42 | } 43 | } 44 | } 45 | --------------------------------------------------------------------------------